]> gcc.gnu.org Git - gcc.git/blame - gcc/cp/decl.c
* configure: Rebuild.
[gcc.git] / gcc / cp / decl.c
CommitLineData
3fd5abcf 1/* Process declarations and variables for C++ compiler.
d363e7bf 2 Copyright (C) 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
3fd5abcf 3 2001, 2002, 2003 Free Software Foundation, Inc.
e5e809f4 4 Contributed by Michael Tiemann (tiemann@cygnus.com)
8d08fdba 5
1c313945 6This file is part of GCC.
8d08fdba 7
1c313945 8GCC is free software; you can redistribute it and/or modify
8d08fdba
MS
9it under the terms of the GNU General Public License as published by
10the Free Software Foundation; either version 2, or (at your option)
11any later version.
12
1c313945 13GCC is distributed in the hope that it will be useful,
8d08fdba
MS
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16GNU General Public License for more details.
17
18You should have received a copy of the GNU General Public License
1c313945 19along with GCC; see the file COPYING. If not, write to
e9fa0c7c
RK
20the Free Software Foundation, 59 Temple Place - Suite 330,
21Boston, MA 02111-1307, USA. */
8d08fdba
MS
22
23
1c313945 24/* Process declarations and symbol lookup for C++ front end.
8d08fdba
MS
25 Also constructs types; the standard scalar types at initialization,
26 and structure, union, array and enum types when they are declared. */
27
28/* ??? not all decl nodes are given the most useful possible
29 line numbers. For example, the CONST_DECLs for enum values. */
30
8d08fdba 31#include "config.h"
8d052bc7 32#include "system.h"
4977bab6
ZW
33#include "coretypes.h"
34#include "tm.h"
8d08fdba
MS
35#include "tree.h"
36#include "rtl.h"
3bdf5ad1 37#include "expr.h"
8d08fdba
MS
38#include "flags.h"
39#include "cp-tree.h"
25af8512 40#include "tree-inline.h"
8d08fdba
MS
41#include "decl.h"
42#include "lex.h"
49c249e1
JM
43#include "output.h"
44#include "except.h"
54f92bfb 45#include "toplev.h"
e2500fed 46#include "hashtab.h"
0e9295cf 47#include "tm_p.h"
672a6f42 48#include "target.h"
26f943fd 49#include "c-common.h"
ecb0eece 50#include "c-pragma.h"
7437519c 51#include "diagnostic.h"
e2500fed 52#include "debug.h"
22ffcc6f 53#include "timevar.h"
8d08fdba 54
11f6b451
NN
55static tree grokparms (tree);
56static const char *redeclaration_error_message (tree, tree);
57
11f6b451 58static void resume_binding_level (struct cp_binding_level *);
11f6b451
NN
59static int decl_jump_unsafe (tree);
60static void storedecls (tree);
61static void require_complete_types_for_parms (tree);
62static int ambi_op_p (enum tree_code);
63static int unary_op_p (enum tree_code);
000d38ea 64static cxx_saved_binding *store_bindings (tree, cxx_saved_binding *);
11f6b451
NN
65static tree lookup_tag_reverse (tree, tree);
66static void push_local_name (tree);
67static void warn_extern_redeclared_static (tree, tree);
7e99327d 68static tree grok_reference_init (tree, tree, tree, tree *);
11f6b451
NN
69static tree grokfndecl (tree, tree, tree, tree, int,
70 enum overload_flags, tree,
71 tree, int, int, int, int, int, int, tree);
72static tree grokvardecl (tree, tree, RID_BIT_TYPE *, int, int, tree);
73static tree follow_tag_typedef (tree);
74static tree lookup_tag (enum tree_code, tree,
75 struct cp_binding_level *, int);
49c249e1 76static void set_identifier_type_value_with_scope
11f6b451
NN
77 (tree, tree, struct cp_binding_level *);
78static void record_unknown_type (tree, const char *);
79static tree builtin_function_1 (const char *, tree, tree, int,
80 enum built_in_class, const char *,
81 tree);
82static tree build_library_fn_1 (tree, enum tree_code, tree);
83static int member_function_or_else (tree, tree, enum overload_flags);
84static void bad_specifiers (tree, const char *, int, int, int, int,
85 int);
86static tree maybe_process_template_type_declaration
87 (tree, int, struct cp_binding_level*);
88static void check_for_uninitialized_const_var (tree);
89static hashval_t typename_hash (const void *);
90static int typename_compare (const void *, const void *);
91static void push_binding (tree, tree, struct cp_binding_level*);
11f6b451
NN
92static void pop_binding (tree, tree);
93static tree local_variable_p_walkfn (tree *, int *, void *);
af6fd53f 94static tree select_decl (cxx_binding *, int);
11f6b451
NN
95static int lookup_flags (int, int);
96static tree qualify_lookup (tree, int);
97static tree record_builtin_java_type (const char *, int);
98static const char *tag_name (enum tag_types code);
11f6b451 99static struct cp_binding_level *innermost_nonclass_level (void);
11f6b451
NN
100static int walk_namespaces_r (tree, walk_namespaces_fn, void *);
101static int walk_globals_r (tree, void*);
102static int walk_vtables_r (tree, void*);
103static void add_decl_to_level (tree, struct cp_binding_level *);
104static tree make_label_decl (tree, int);
105static void use_label (tree);
106static void check_previous_goto_1 (tree, struct cp_binding_level *, tree,
152c16a9 107 const location_t *);
11f6b451
NN
108static void check_previous_goto (struct named_label_use_list *);
109static void check_switch_goto (struct cp_binding_level *);
110static void check_previous_gotos (tree);
111static void pop_label (tree, tree);
112static void pop_labels (tree);
113static void maybe_deduce_size_from_array_init (tree, tree);
114static void layout_var_decl (tree);
115static void maybe_commonize_var (tree);
7e99327d 116static tree check_initializer (tree, tree, int, tree *);
11f6b451
NN
117static void make_rtl_for_nonlocal_decl (tree, tree, const char *);
118static void save_function_data (tree);
119static void check_function_type (tree, tree);
120static void begin_constructor_body (void);
121static void finish_constructor_body (void);
122static void begin_destructor_body (void);
123static void finish_destructor_body (void);
124static tree create_array_type_for_decl (tree, tree, tree);
125static tree get_atexit_node (void);
126static tree get_dso_handle_node (void);
127static tree start_cleanup_fn (void);
128static void end_cleanup_fn (void);
129static tree cp_make_fname_decl (tree, int);
130static void initialize_predefined_identifiers (void);
131static tree check_special_function_return_type
132 (special_function_kind, tree, tree);
133static tree push_cp_library_fn (enum tree_code, tree);
134static tree build_cp_library_fn (tree, enum tree_code, tree);
135static void store_parm_decls (tree);
136static int cp_missing_noreturn_ok_p (tree);
8e3df2de
MM
137static void initialize_local_var (tree, tree);
138static void expand_static_init (tree, tree);
139static tree next_initializable_field (tree);
140static tree reshape_init (tree, tree *);
8fbc5ae7 141static tree build_typename_type (tree, tree, tree);
8d08fdba 142
8d08fdba
MS
143/* Erroneous argument lists can use this *IFF* they do not modify it. */
144tree error_mark_list;
145
7f4edbcb 146/* The following symbols are subsumed in the cp_global_trees array, and
68642fb6 147 listed here individually for documentation purposes.
8d08fdba 148
7f4edbcb
BS
149 C++ extensions
150 tree wchar_decl_node;
37c46b43 151
7f4edbcb
BS
152 tree vtable_entry_type;
153 tree delta_type_node;
7f4edbcb 154 tree __t_desc_type_node;
db1147b2 155 tree ti_desc_type_node;
1f4cb92b 156 tree bltn_desc_type_node, ptr_desc_type_node;
db1147b2
NS
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;
387769ed 159 tree ptm_desc_type_node;
db1147b2 160 tree base_desc_type_node;
8d08fdba 161
7f4edbcb
BS
162 tree class_type_node, record_type_node, union_type_node, enum_type_node;
163 tree unknown_type_node;
8d08fdba 164
7f4edbcb 165 Array type `vtable_entry_type[]'
8d08fdba 166
7f4edbcb
BS
167 tree vtbl_type_node;
168 tree vtbl_ptr_type_node;
8d08fdba 169
2854d3c6 170 Namespaces,
8d08fdba 171
7f4edbcb 172 tree std_node;
2854d3c6 173 tree abi_node;
8d08fdba 174
7f4edbcb
BS
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.
8d08fdba 178
7f4edbcb 179 tree abort_fndecl;
8d08fdba 180
7f4edbcb 181 The FUNCTION_DECL for the default `::operator delete'.
2986ae00 182
7f4edbcb 183 tree global_delete_fndecl;
8d08fdba 184
7f4edbcb 185 Used by RTTI
669ec2b4
JM
186 tree type_info_type_node, tinfo_decl_id, tinfo_decl_type;
187 tree tinfo_var_id;
188
7f4edbcb 189*/
8d08fdba 190
7f4edbcb 191tree cp_global_trees[CPTI_MAX];
8d08fdba 192
2c73f9f5 193/* Indicates that there is a type value in some namespace, although
7f4edbcb 194 that is not necessarily in scope at the moment. */
2c73f9f5 195
e2500fed 196static GTY(()) tree global_type_node;
2c73f9f5 197
82a2669e
GDR
198/* The node that holds the "name" of the global scope. */
199static GTY(()) tree global_scope_name;
200
6625cdb5
JM
201/* Used only for jumps to as-yet undefined labels, since jumps to
202 defined labels can have their validity checked immediately. */
203
e2500fed 204struct named_label_use_list GTY(())
e349ee73 205{
e2500fed 206 struct cp_binding_level *binding_level;
e349ee73
MS
207 tree names_in_scope;
208 tree label_decl;
152c16a9 209 location_t o_goto_locus;
6625cdb5 210 struct named_label_use_list *next;
e349ee73
MS
211};
212
ed5511d9 213#define named_label_uses cp_function_chain->x_named_label_uses
8d08fdba 214
8e4ce833
JJ
215#define local_names cp_function_chain->x_local_names
216
8d08fdba
MS
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. */
221tree static_aggregates;
222
8d08fdba
MS
223/* -- end of C++ */
224
81b3411c 225/* A node for the integer constants 2, and 3. */
d11ad92e 226
81b3411c 227tree integer_two_node, integer_three_node;
8d08fdba 228
8d08fdba 229/* Similar, for last_function_parm_tags. */
9cd64686 230tree last_function_parms;
8d08fdba 231
6625cdb5
JM
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. */
235
e2500fed 236struct named_label_list GTY(())
6625cdb5 237{
e2500fed 238 struct cp_binding_level *binding_level;
6625cdb5
JM
239 tree names_in_scope;
240 tree old_value;
241 tree label_decl;
242 tree bad_decls;
6625cdb5 243 struct named_label_list *next;
826840d9
RH
244 unsigned int in_try_scope : 1;
245 unsigned int in_catch_scope : 1;
6625cdb5 246};
8d08fdba 247
4519c0a8 248#define named_labels cp_function_chain->x_named_labels
8d08fdba 249\f
0c8feefe
MM
250/* The name of the anonymous namespace, throughout this translation
251 unit. */
252tree anonymous_namespace_name;
253
b2244c65
MM
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.) */
257int function_depth;
e23bd218
IR
258
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. */
263
264enum deprecated_states {
265 DEPRECATED_NORMAL,
266 DEPRECATED_SUPPRESS
267};
268
269static enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
270
271/* Set by add_implicitly_declared_members() to keep those members from
272 being flagged as deprecated or reported as using deprecated
273 types. */
274int adding_implicit_members = 0;
594bb0e7
MM
275
276/* True if a declaration with an `extern' linkage specifier is being
277 processed. */
278bool have_extern_spec;
279
8d08fdba
MS
280\f
281/* For each binding contour we allocate a binding_level structure
e92cc029
MS
282 which records the names defined in that contour.
283 Contours include:
284 0) the global one
285 1) one for each function definition,
286 where internal declarations of the parameters appear.
287 2) one for each compound statement,
288 to record its declarations.
289
290 The current meaning of a name can be found by searching the levels
291 from the current one out to the global one.
292
293 Off to the side, may be the class_binding_level. This exists only
294 to catch class-local declarations. It is otherwise nonexistent.
295
296 Also there may be binding levels that catch cleanups that must be
d8f8dca1
MM
297 run when exceptions occur. Thus, to see whether a name is bound in
298 the current scope, it is not enough to look in the
299 CURRENT_BINDING_LEVEL. You should use lookup_name_current_level
300 instead. */
8d08fdba
MS
301
302/* Note that the information in the `names' component of the global contour
303 is duplicated in the IDENTIFIER_GLOBAL_VALUEs of all identifiers. */
304
e2500fed 305struct cp_binding_level GTY(())
8d08fdba
MS
306 {
307 /* A chain of _DECL nodes for all variables, constants, functions,
e92cc029 308 and typedef types. These are in the reverse of the order
f181d4ae
MM
309 supplied. There may be OVERLOADs on this list, too, but they
310 are wrapped in TREE_LISTs; the TREE_VALUE is the OVERLOAD. */
8d08fdba
MS
311 tree names;
312
70b76b34
DP
313 /* Count of elements in names chain. */
314 size_t names_size;
315
316 /* A chain of NAMESPACE_DECL nodes. */
317 tree namespaces;
318
1d555f7a
MA
319 /* An array of static functions and variables (for namespaces only) */
320 varray_type static_decls;
321
70b76b34
DP
322 /* A chain of VTABLE_DECL nodes. */
323 tree vtables;
324
5e0c54e5
GDR
325 /* A dictionary for looking up user-defined-types. */
326 binding_table type_decls;
8d08fdba 327
a1c65f9f 328 /* A list of USING_DECL nodes. */
2c73f9f5
ML
329 tree usings;
330
ea9635c7 331 /* A list of used namespaces. PURPOSE is the namespace,
a1c65f9f 332 VALUE the common ancestor with this binding_level's namespace. */
ea9635c7
ML
333 tree using_directives;
334
f181d4ae
MM
335 /* If this binding level is the binding level for a class, then
336 class_shadowed is a TREE_LIST. The TREE_PURPOSE of each node
76191fdd 337 is the name of an entity bound in the class. The TREE_TYPE is
d8f8dca1 338 the DECL bound by this name in the class. */
8d08fdba
MS
339 tree class_shadowed;
340
f181d4ae 341 /* Similar to class_shadowed, but for IDENTIFIER_TYPE_VALUE, and
76191fdd
NS
342 is used for all binding levels. In addition the TREE_VALUE is the
343 IDENTIFIER_TYPE_VALUE before we entered the class. */
8d08fdba
MS
344 tree type_shadowed;
345
acef433b
MM
346 /* A TREE_LIST. Each TREE_VALUE is the LABEL_DECL for a local
347 label in this scope. The TREE_PURPOSE is the previous value of
348 the IDENTIFIER_LABEL VALUE. */
349 tree shadowed_labels;
350
8d08fdba
MS
351 /* For each level (except not the global one),
352 a chain of BLOCK nodes for all the levels
353 that were entered and exited one level down. */
354 tree blocks;
355
82a2669e
GDR
356 /* The entity (namespace, class, function) the scope of which this
357 binding contour corresponds to. Otherwise NULL. */
358 tree this_entity;
70adf8a9 359
8d08fdba 360 /* The binding level which this one is contained in (inherits from). */
e2500fed 361 struct cp_binding_level *level_chain;
8d08fdba 362
8d6e462b 363 /* List of VAR_DECLS saved from a previous for statement.
cab1f180 364 These would be dead in ISO-conforming code, but might
f181d4ae
MM
365 be referenced in ARM-era code. These are stored in a
366 TREE_LIST; the TREE_VALUE is the actual declaration. */
8d6e462b
PB
367 tree dead_vars_from_for;
368
a7e8c268 369 /* Binding depth at which this level began. */
e7ab53ea 370 int binding_depth;
a7e8c268
MM
371
372 /* The kind of scope that this object represents. However, a
373 SK_TEMPLATE_SPEC scope is represented with KIND set to
374 SK_TEMPALTE_PARMS and EXPLICIT_SPEC_P set to true. */
375 enum scope_kind kind : 4;
376
377 /* True if this scope is an SK_TEMPLATE_SPEC scope. This field is
378 only valid if KIND == SK_TEMPLATE_PARMS. */
379 bool explicit_spec_p : 1;
8d08fdba 380
ac20c67a
GDR
381 /* true means make a BLOCK for this level regardless of all else. */
382 unsigned keep : 1;
8d08fdba 383
8d08fdba
MS
384 /* Nonzero if this level can safely have additional
385 cleanup-needing variables added to it. */
386 unsigned more_cleanups_ok : 1;
387 unsigned have_cleanups : 1;
388
ac20c67a 389 /* 22 bits left to fill a 32-bit word. */
8d08fdba
MS
390 };
391
e2500fed 392#define NULL_BINDING_LEVEL ((struct cp_binding_level *) NULL)
68642fb6 393
8f032717 394/* The binding level currently in effect. */
8d08fdba 395
a8f73d4b 396#define current_binding_level \
ff955512
KG
397 (*(cfun && cp_function_chain->bindings \
398 ? &cp_function_chain->bindings \
399 : &scope_chain->bindings))
de94b46c 400
8d08fdba
MS
401/* The binding level of the current class, if any. */
402
9cd64686 403#define class_binding_level scope_chain->class_bindings
8d08fdba 404
8d08fdba
MS
405/* A chain of binding_level structures awaiting reuse. */
406
e2500fed 407static GTY((deletable (""))) struct cp_binding_level *free_binding_level;
8d08fdba 408
ac20c67a 409/* true means unconditionally make a BLOCK for the next level pushed. */
8d08fdba 410
ac20c67a 411static bool keep_next_level_flag;
8d08fdba 412
0154eaa8
MM
413/* A TREE_LIST of VAR_DECLs. The TREE_PURPOSE is a RECORD_TYPE or
414 UNION_TYPE; the TREE_VALUE is a VAR_DECL with that type. At the
415 time the VAR_DECL was declared, the type was incomplete. */
416
e2500fed 417static GTY(()) tree incomplete_vars;
0154eaa8 418
f37e2274
GDR
419#ifndef ENABLE_SCOPE_CHECKING
420# define ENABLE_SCOPE_CHECKING 0
421#else
422# define ENABLE_SCOPE_CHECKING 1
423#endif
424
8d08fdba
MS
425static int binding_depth = 0;
426static int is_class_level = 0;
427
428static void
f37e2274 429indent (int depth)
8d08fdba 430{
f37e2274 431 int i;
8d08fdba 432
f37e2274 433 for (i = 0; i < depth * 2; i++)
8d08fdba
MS
434 putc (' ', stderr);
435}
8d08fdba 436
11f6b451 437static tree pushdecl_with_scope (tree, struct cp_binding_level *);
8d08fdba 438
82a2669e
GDR
439/* Return a string describing the kind of SCOPE we have. */
440static const char *
441cxx_scope_descriptor (cxx_scope *scope)
442{
a7e8c268
MM
443 /* The order of this table must match the "scope_kind"
444 enumerators. */
445 static const char* scope_kind_names[] = {
446 "block-scope",
ac20c67a 447 "cleanup-scope",
a7e8c268
MM
448 "try-scope",
449 "catch-scope",
450 "for-scope",
e7ab53ea
GDR
451 "function-parameter-scope",
452 "class-scope",
453 "namespace-scope",
a7e8c268
MM
454 "template-parameter-scope",
455 "template-explicit-spec-scope"
456 };
ac20c67a
GDR
457 const scope_kind kind = scope->explicit_spec_p
458 ? sk_template_spec : scope->kind;
82a2669e 459
ac20c67a 460 return scope_kind_names[kind];
82a2669e
GDR
461}
462
463/* Output a debugging information about SCOPE when performning
464 ACTION at LINE. */
8d08fdba 465static void
82a2669e
GDR
466cxx_scope_debug (cxx_scope *scope, int line, const char *action)
467{
468 const char *desc = cxx_scope_descriptor (scope);
469 if (scope->this_entity)
470 verbatim ("%s %s(%E) %p %d\n", action, desc,
471 scope->this_entity, (void *) scope, line);
472 else
473 verbatim ("%s %s %p %d\n", action, desc, (void *) scope, line);
474}
475
ac20c67a
GDR
476/* Return the estimated initial size of the hashtable of a NAMESPACE
477 scope. */
478
479static inline size_t
480namespace_scope_ht_size (tree ns)
481{
482 tree name = DECL_NAME (ns);
483
484 return name == std_identifier
485 ? NAMESPACE_STD_HT_SIZE
486 : (name == global_scope_name
487 ? GLOBAL_SCOPE_HT_SIZE
488 : NAMESPACE_ORDINARY_HT_SIZE);
489}
490
491/* Create a new KIND scope and make it the top of the active scopes stack.
492 ENTITY is the scope of the associated C++ entity (namespace, class,
493 function); it is NULL otherwise. */
494
495cxx_scope *
496begin_scope (scope_kind kind, tree entity)
82a2669e
GDR
497{
498 cxx_scope *scope;
499
500 /* Reuse or create a struct for this binding level. */
501 if (!ENABLE_SCOPE_CHECKING && free_binding_level)
502 {
503 scope = free_binding_level;
504 free_binding_level = scope->level_chain;
505 }
506 else
507 scope = ggc_alloc (sizeof (cxx_scope));
82a2669e 508 memset (scope, 0, sizeof (cxx_scope));
ac20c67a
GDR
509
510 scope->this_entity = entity;
82a2669e 511 scope->more_cleanups_ok = true;
ac20c67a
GDR
512 switch (kind)
513 {
514 case sk_cleanup:
515 scope->keep = true;
516 break;
517
518 case sk_template_spec:
519 scope->explicit_spec_p = true;
520 kind = sk_template_parms;
521 /* fall through */
522 case sk_template_parms:
523 case sk_block:
524 case sk_try:
525 case sk_catch:
526 case sk_for:
527 case sk_class:
528 case sk_function_parms:
529 scope->keep = keep_next_level_flag;
530 break;
82a2669e 531
ac20c67a
GDR
532 case sk_namespace:
533 scope->type_decls = binding_table_new (namespace_scope_ht_size (entity));
534 NAMESPACE_LEVEL (entity) = scope;
535 VARRAY_TREE_INIT (scope->static_decls,
536 DECL_NAME (entity) == std_identifier
537 || DECL_NAME (entity) == global_scope_name
538 ? 200 : 10,
539 "Static declarations");
540 break;
82a2669e 541
ac20c67a
GDR
542 default:
543 /* Should not happen. */
544 my_friendly_assert (false, 20030922);
545 break;
546 }
547 scope->kind = kind;
548
549 /* Add it to the front of currently active scopes stack. */
550 scope->level_chain = current_binding_level;
551 current_binding_level = scope;
552 keep_next_level_flag = false;
227cf171 553
f37e2274
GDR
554 if (ENABLE_SCOPE_CHECKING)
555 {
ac20c67a 556 scope->binding_depth = binding_depth;
f37e2274 557 indent (binding_depth);
ac20c67a 558 cxx_scope_debug (scope, input_location.line, "push");
f37e2274
GDR
559 is_class_level = 0;
560 binding_depth++;
561 }
ac20c67a
GDR
562
563 return scope;
8d08fdba
MS
564}
565
1a6daf15
GDR
566/* We're about to leave current scope. Pop the top of the stack of
567 currently active scopes. Return the enclosing scope, now active. */
8f032717 568
1a6daf15
GDR
569static cxx_scope *
570leave_scope (void)
8f032717 571{
1a6daf15 572 cxx_scope *scope = current_binding_level;
8f032717 573
1a6daf15
GDR
574 if (scope->kind == sk_namespace && class_binding_level)
575 current_binding_level = class_binding_level;
8f032717 576
1a6daf15 577 /* We cannot leave a scope, if there are none left. */
5f52c0e0 578 if (NAMESPACE_LEVEL (global_namespace))
1a6daf15
GDR
579 my_friendly_assert (!global_scope_p (scope), 20030527);
580
f37e2274 581 if (ENABLE_SCOPE_CHECKING)
9e9ff709 582 {
f37e2274 583 indent (--binding_depth);
1a6daf15
GDR
584 cxx_scope_debug (scope, input_location.line, "leave");
585 if (is_class_level != (scope == class_binding_level))
f37e2274
GDR
586 {
587 indent (binding_depth);
1a6daf15 588 verbatim ("XXX is_class_level != (current_scope == class_scope)\n");
f37e2274
GDR
589 }
590 is_class_level = 0;
9e9ff709 591 }
a9aedbc2 592
1a6daf15
GDR
593 /* Move one nesting level up. */
594 current_binding_level = scope->level_chain;
a9aedbc2 595
1a6daf15
GDR
596 /* Namespace-scopes are left most probably temporarily, not completely;
597 they can be reopen later, e.g. in namespace-extension or any name
598 binding acttivity that requires us to resume a namespace. For other
599 scopes, we just make the structure available for reuse. */
600 if (scope->kind != sk_namespace)
9e9ff709 601 {
1a6daf15
GDR
602 scope->level_chain = free_binding_level;
603 if (scope->kind == sk_class)
604 scope->type_decls = NULL;
605 else
606 binding_table_free (scope->type_decls);
607 my_friendly_assert (!ENABLE_SCOPE_CHECKING
608 || scope->binding_depth == binding_depth,
609 20030529);
610 free_binding_level = scope;
9e9ff709 611 }
1a6daf15
GDR
612
613 /* Find the innermost enclosing class scope, and reset
614 CLASS_BINDING_LEVEL appropriately. */
615 for (scope = current_binding_level;
616 scope && scope->kind != sk_class;
617 scope = scope->level_chain)
618 ;
619 class_binding_level = scope && scope->kind == sk_class ? scope : NULL;
620
621 return current_binding_level;
a9aedbc2
MS
622}
623
824b9a4c 624static void
11f6b451 625resume_binding_level (struct cp_binding_level* b)
a9aedbc2 626{
2c73f9f5 627 /* Resuming binding levels is meant only for namespaces,
a1c65f9f 628 and those cannot nest into classes. */
2c73f9f5
ML
629 my_friendly_assert(!class_binding_level, 386);
630 /* Also, resuming a non-directly nested namespace is a no-no. */
631 my_friendly_assert(b->level_chain == current_binding_level, 386);
ff955512 632 current_binding_level = b;
f37e2274
GDR
633 if (ENABLE_SCOPE_CHECKING)
634 {
635 b->binding_depth = binding_depth;
636 indent (binding_depth);
82a2669e 637 cxx_scope_debug (b, input_location.line, "resume");
f37e2274
GDR
638 is_class_level = 0;
639 binding_depth++;
640 }
a9aedbc2 641}
8d08fdba
MS
642\f
643/* Nonzero if we are currently in the global binding level. */
644
645int
11f6b451 646global_bindings_p (void)
8d08fdba 647{
5f52c0e0 648 return global_scope_p (current_binding_level);
8d08fdba
MS
649}
650
8f032717
MM
651/* Return the innermost binding level that is not for a class scope. */
652
e2500fed 653static struct cp_binding_level *
11f6b451 654innermost_nonclass_level (void)
8f032717 655{
e2500fed 656 struct cp_binding_level *b;
8f032717
MM
657
658 b = current_binding_level;
a7e8c268 659 while (b->kind == sk_class)
8f032717
MM
660 b = b->level_chain;
661
662 return b;
663}
664
a9aedbc2
MS
665/* Nonzero if we are currently in a toplevel binding level. This
666 means either the global binding level or a namespace in a toplevel
8f032717 667 binding level. Since there are no non-toplevel namespace levels,
74b846e0
MM
668 this really means any namespace or template parameter level. We
669 also include a class whose context is toplevel. */
a9aedbc2
MS
670
671int
11f6b451 672toplevel_bindings_p (void)
a9aedbc2 673{
e2500fed 674 struct cp_binding_level *b = innermost_nonclass_level ();
8f032717 675
a7e8c268 676 return b->kind == sk_namespace || b->kind == sk_template_parms;
a9aedbc2
MS
677}
678
8f032717
MM
679/* Nonzero if this is a namespace scope, or if we are defining a class
680 which is itself at namespace scope, or whose enclosing class is
681 such a class, etc. */
a9aedbc2 682
7bdbfa05 683int
11f6b451 684namespace_bindings_p (void)
a9aedbc2 685{
e2500fed 686 struct cp_binding_level *b = innermost_nonclass_level ();
8f032717 687
a7e8c268 688 return b->kind == sk_namespace;
a9aedbc2
MS
689}
690
ac20c67a 691/* If KEEP is true, make a BLOCK node for the next binding level,
f1dedc31
MM
692 unconditionally. Otherwise, use the normal logic to decide whether
693 or not to create a BLOCK. */
694
8d08fdba 695void
ac20c67a 696keep_next_level (bool keep)
8d08fdba 697{
f1dedc31 698 keep_next_level_flag = keep;
8d08fdba
MS
699}
700
701/* Nonzero if the current level needs to have a BLOCK made. */
702
703int
11f6b451 704kept_level_p (void)
8d08fdba
MS
705{
706 return (current_binding_level->blocks != NULL_TREE
707 || current_binding_level->keep
ac20c67a 708 || current_binding_level->kind == sk_cleanup
8d08fdba 709 || current_binding_level->names != NULL_TREE
ac20c67a 710 || current_binding_level->type_decls != NULL);
8d08fdba
MS
711}
712
a7e8c268
MM
713/* Returns the kind of the innermost scope. */
714
715scope_kind
716innermost_scope_kind (void)
717{
718 return current_binding_level->kind;
719}
720
838dfd8a 721/* Returns nonzero if this scope was created to store template
74b846e0
MM
722 parameters. */
723
8d08fdba 724int
11f6b451 725template_parm_scope_p (void)
8d08fdba 726{
a7e8c268 727 return innermost_scope_kind () == sk_template_parms;
74b846e0
MM
728}
729
730/* Returns the kind of template specialization we are currently
731 processing, given that it's declaration contained N_CLASS_SCOPES
732 explicit scope qualifications. */
733
734tmpl_spec_kind
11f6b451 735current_tmpl_spec_kind (int n_class_scopes)
74b846e0
MM
736{
737 int n_template_parm_scopes = 0;
738 int seen_specialization_p = 0;
739 int innermost_specialization_p = 0;
e2500fed 740 struct cp_binding_level *b;
74b846e0
MM
741
742 /* Scan through the template parameter scopes. */
a7e8c268
MM
743 for (b = current_binding_level;
744 b->kind == sk_template_parms;
745 b = b->level_chain)
74b846e0
MM
746 {
747 /* If we see a specialization scope inside a parameter scope,
748 then something is wrong. That corresponds to a declaration
749 like:
750
751 template <class T> template <> ...
752
0e339752 753 which is always invalid since [temp.expl.spec] forbids the
74b846e0
MM
754 specialization of a class member template if the enclosing
755 class templates are not explicitly specialized as well. */
a7e8c268 756 if (b->explicit_spec_p)
74b846e0
MM
757 {
758 if (n_template_parm_scopes == 0)
759 innermost_specialization_p = 1;
760 else
761 seen_specialization_p = 1;
762 }
763 else if (seen_specialization_p == 1)
764 return tsk_invalid_member_spec;
765
766 ++n_template_parm_scopes;
767 }
768
769 /* Handle explicit instantiations. */
770 if (processing_explicit_instantiation)
771 {
772 if (n_template_parm_scopes != 0)
773 /* We've seen a template parameter list during an explicit
774 instantiation. For example:
775
776 template <class T> template void f(int);
777
778 This is erroneous. */
779 return tsk_invalid_expl_inst;
780 else
781 return tsk_expl_inst;
782 }
783
784 if (n_template_parm_scopes < n_class_scopes)
785 /* We've not seen enough template headers to match all the
786 specialized classes present. For example:
787
788 template <class T> void R<T>::S<T>::f(int);
789
0e339752 790 This is invalid; there needs to be one set of template
74b846e0
MM
791 parameters for each class. */
792 return tsk_insufficient_parms;
793 else if (n_template_parm_scopes == n_class_scopes)
794 /* We're processing a non-template declaration (even though it may
795 be a member of a template class.) For example:
796
797 template <class T> void S<T>::f(int);
798
799 The `class T' maches the `S<T>', leaving no template headers
800 corresponding to the `f'. */
801 return tsk_none;
802 else if (n_template_parm_scopes > n_class_scopes + 1)
803 /* We've got too many template headers. For example:
804
805 template <> template <class T> void f (T);
806
807 There need to be more enclosing classes. */
808 return tsk_excessive_parms;
809 else
810 /* This must be a template. It's of the form:
811
812 template <class T> template <class U> void S<T>::f(U);
813
814 This is a specialization if the innermost level was a
815 specialization; otherwise it's just a definition of the
816 template. */
817 return innermost_specialization_p ? tsk_expl_spec : tsk_template;
8d08fdba
MS
818}
819
820void
11f6b451 821set_class_shadows (tree shadows)
8d08fdba
MS
822{
823 class_binding_level->class_shadowed = shadows;
824}
825
bd0d5d4a
JM
826/* We're defining an object of type TYPE. If it needs a cleanup, but
827 we're not allowed to add any more objects with cleanups to the current
828 scope, create a new binding level. */
829
830void
11f6b451 831maybe_push_cleanup_level (tree type)
bd0d5d4a
JM
832{
833 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
834 && current_binding_level->more_cleanups_ok == 0)
835 {
ac20c67a 836 begin_scope (sk_cleanup, NULL);
bd0d5d4a
JM
837 clear_last_expr ();
838 add_scope_stmt (/*begin_p=*/1, /*partial_p=*/1);
839 }
840}
74b846e0
MM
841
842/* Exit the current scope. */
843
844void
11f6b451 845finish_scope (void)
74b846e0
MM
846{
847 poplevel (0, 0, 0);
848}
849
f181d4ae
MM
850/* Make DECL the innermost binding for ID. The LEVEL is the binding
851 level at which this declaration is being bound. */
852
853static void
ed3cf953 854push_binding (tree id, tree decl, cxx_scope* level)
f181d4ae 855{
aed81407 856 cxx_binding *binding = cxx_binding_make (decl, NULL);
f181d4ae
MM
857
858 /* Now, fill in the binding information. */
af6fd53f 859 binding->previous = IDENTIFIER_BINDING (id);
147135cc 860 binding->scope = level;
8f032717 861 INHERITED_VALUE_BINDING_P (binding) = 0;
f181d4ae
MM
862 LOCAL_BINDING_P (binding) = (level != class_binding_level);
863
c45df9c1 864 /* And put it on the front of the list of bindings for ID. */
f181d4ae
MM
865 IDENTIFIER_BINDING (id) = binding;
866}
867
cfe507be
MM
868/* Add DECL to the list of things declared in B. */
869
870static void
11f6b451
NN
871add_decl_to_level (tree decl,
872 struct cp_binding_level* b)
cfe507be 873{
70b76b34
DP
874 if (TREE_CODE (decl) == NAMESPACE_DECL
875 && !DECL_NAMESPACE_ALIAS (decl))
876 {
877 TREE_CHAIN (decl) = b->namespaces;
878 b->namespaces = decl;
879 }
880 else if (TREE_CODE (decl) == VAR_DECL && DECL_VIRTUAL_P (decl))
881 {
882 TREE_CHAIN (decl) = b->vtables;
883 b->vtables = decl;
884 }
885 else
886 {
887 /* We build up the list in reverse order, and reverse it later if
888 necessary. */
889 TREE_CHAIN (decl) = b->names;
890 b->names = decl;
891 b->names_size++;
1d555f7a
MA
892
893 /* If appropriate, add decl to separate list of statics */
a7e8c268 894 if (b->kind == sk_namespace)
1d555f7a
MA
895 if ((TREE_CODE (decl) == VAR_DECL && TREE_STATIC (decl))
896 || (TREE_CODE (decl) == FUNCTION_DECL
897 && (!TREE_PUBLIC (decl) || DECL_DECLARED_INLINE_P (decl))))
898 VARRAY_PUSH_TREE (b->static_decls, decl);
70b76b34 899 }
cfe507be
MM
900}
901
902/* Bind DECL to ID in the current_binding_level, assumed to be a local
903 binding level. If PUSH_USING is set in FLAGS, we know that DECL
904 doesn't really belong to this binding level, that it got here
905 through a using-declaration. */
f181d4ae
MM
906
907void
11f6b451 908push_local_binding (tree id, tree decl, int flags)
f181d4ae 909{
e2500fed 910 struct cp_binding_level *b;
8f032717
MM
911
912 /* Skip over any local classes. This makes sense if we call
913 push_local_binding with a friend decl of a local class. */
a7e8c268 914 b = innermost_nonclass_level ();
f181d4ae 915
d8f8dca1 916 if (lookup_name_current_level (id))
8f032717
MM
917 {
918 /* Supplement the existing binding. */
c87ceb13 919 if (!supplement_binding (IDENTIFIER_BINDING (id), decl))
8f032717
MM
920 /* It didn't work. Something else must be bound at this
921 level. Do not add DECL to the list of things to pop
922 later. */
923 return;
924 }
d8f8dca1
MM
925 else
926 /* Create a new binding. */
8f032717 927 push_binding (id, decl, b);
f181d4ae 928
0034cf72 929 if (TREE_CODE (decl) == OVERLOAD || (flags & PUSH_USING))
a06d48ef
JM
930 /* We must put the OVERLOAD into a TREE_LIST since the
931 TREE_CHAIN of an OVERLOAD is already used. Similarly for
932 decls that got here through a using-declaration. */
933 decl = build_tree_list (NULL_TREE, decl);
934
f181d4ae
MM
935 /* And put DECL on the list of things declared by the current
936 binding level. */
cfe507be 937 add_decl_to_level (decl, b);
f181d4ae
MM
938}
939
8f032717
MM
940/* Bind DECL to ID in the class_binding_level. Returns nonzero if the
941 binding was successful. */
f181d4ae 942
8f032717 943int
11f6b451 944push_class_binding (tree id, tree decl)
f181d4ae 945{
8f032717 946 int result = 1;
af6fd53f 947 cxx_binding *binding = IDENTIFIER_BINDING (id);
8f032717
MM
948 tree context;
949
7d9f2df2 950 timevar_push (TV_NAME_LOOKUP);
8f032717 951 /* Note that we declared this value so that we can issue an error if
0e339752 952 this is an invalid redeclaration of a name already used for some
8f032717
MM
953 other purpose. */
954 note_name_declared_in_class (id, decl);
955
147135cc 956 if (binding && binding->scope == class_binding_level)
d8f8dca1 957 /* Supplement the existing binding. */
c87ceb13 958 result = supplement_binding (IDENTIFIER_BINDING (id), decl);
d8f8dca1
MM
959 else
960 /* Create a new binding. */
961 push_binding (id, decl, class_binding_level);
962
963 /* Update the IDENTIFIER_CLASS_VALUE for this ID to be the
964 class-level declaration. Note that we do not use DECL here
965 because of the possibility of the `struct stat' hack; if DECL is
966 a class-name or enum-name we might prefer a field-name, or some
967 such. */
147135cc 968 IDENTIFIER_CLASS_VALUE (id) = IDENTIFIER_BINDING (id)->value;
8f032717
MM
969
970 /* If this is a binding from a base class, mark it as such. */
971 binding = IDENTIFIER_BINDING (id);
147135cc 972 if (binding->value == decl && TREE_CODE (decl) != TREE_LIST)
8f032717 973 {
8fbc5ae7
MM
974 if (TREE_CODE (decl) == OVERLOAD)
975 context = CP_DECL_CONTEXT (OVL_CURRENT (decl));
8f032717
MM
976 else
977 {
8fbc5ae7
MM
978 my_friendly_assert (DECL_P (decl), 0);
979 context = context_for_name_lookup (decl);
280f9385 980 }
8fbc5ae7
MM
981
982 if (is_properly_derived_from (current_class_type, context))
983 INHERITED_VALUE_BINDING_P (binding) = 1;
984 else
985 INHERITED_VALUE_BINDING_P (binding) = 0;
8f032717 986 }
147135cc 987 else if (binding->value == decl)
8f032717
MM
988 /* We only encounter a TREE_LIST when push_class_decls detects an
989 ambiguity. Such an ambiguity can be overridden by a definition
990 in this class. */
991 INHERITED_VALUE_BINDING_P (binding) = 1;
992
7d9f2df2 993 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, result);
f181d4ae
MM
994}
995
d8f8dca1
MM
996/* Remove the binding for DECL which should be the innermost binding
997 for ID. */
f181d4ae 998
68642fb6 999static void
11f6b451 1000pop_binding (tree id, tree decl)
f181d4ae 1001{
af6fd53f 1002 cxx_binding *binding;
68642fb6 1003
f181d4ae
MM
1004 if (id == NULL_TREE)
1005 /* It's easiest to write the loops that call this function without
1006 checking whether or not the entities involved have names. We
1007 get here for such an entity. */
1008 return;
1009
d8f8dca1 1010 /* Get the innermost binding for ID. */
f181d4ae 1011 binding = IDENTIFIER_BINDING (id);
f181d4ae 1012
d8f8dca1 1013 /* The name should be bound. */
af6fd53f 1014 my_friendly_assert (binding != NULL, 0);
d8f8dca1
MM
1015
1016 /* The DECL will be either the ordinary binding or the type
1017 binding for this identifier. Remove that binding. */
147135cc
GDR
1018 if (binding->value == decl)
1019 binding->value = NULL_TREE;
1020 else if (binding->type == decl)
1021 binding->type = NULL_TREE;
d8f8dca1 1022 else
a98facb0 1023 abort ();
d8f8dca1 1024
147135cc 1025 if (!binding->value && !binding->type)
4890c2f4
MM
1026 {
1027 /* We're completely done with the innermost binding for this
1028 identifier. Unhook it from the list of bindings. */
af6fd53f 1029 IDENTIFIER_BINDING (id) = binding->previous;
4890c2f4
MM
1030
1031 /* Add it to the free list. */
aed81407 1032 cxx_binding_free (binding);
4890c2f4 1033 }
f181d4ae
MM
1034}
1035
acef433b
MM
1036/* When a label goes out of scope, check to see if that label was used
1037 in a valid manner, and issue any appropriate warnings or errors. */
1038
1039static void
11f6b451 1040pop_label (tree label, tree old_value)
acef433b 1041{
a7e8c268 1042 if (!processing_template_decl)
acef433b 1043 {
88848bde
MM
1044 if (DECL_INITIAL (label) == NULL_TREE)
1045 {
5b030314
NS
1046 location_t location;
1047
88848bde 1048 cp_error_at ("label `%D' used but not defined", label);
5b030314
NS
1049 location.file = input_filename;
1050 location.line = 0;
88848bde 1051 /* Avoid crashing later. */
5b030314 1052 define_label (location, DECL_NAME (label));
88848bde 1053 }
078721e1 1054 else if (warn_unused_label && !TREE_USED (label))
88848bde 1055 cp_warning_at ("label `%D' defined but not used", label);
acef433b 1056 }
acef433b 1057
6625cdb5 1058 SET_IDENTIFIER_LABEL_VALUE (DECL_NAME (label), old_value);
acef433b
MM
1059}
1060
0811ea8f 1061/* At the end of a function, all labels declared within the function
68642fb6 1062 go out of scope. BLOCK is the top-level block for the
acef433b
MM
1063 function. */
1064
1065static void
11f6b451 1066pop_labels (tree block)
acef433b 1067{
6625cdb5 1068 struct named_label_list *link;
acef433b
MM
1069
1070 /* Clear out the definitions of all label names, since their scopes
1071 end here. */
6625cdb5 1072 for (link = named_labels; link; link = link->next)
acef433b 1073 {
6625cdb5 1074 pop_label (link->label_decl, link->old_value);
acef433b
MM
1075 /* Put the labels into the "variables" of the top-level block,
1076 so debugger can see them. */
6625cdb5
JM
1077 TREE_CHAIN (link->label_decl) = BLOCK_VARS (block);
1078 BLOCK_VARS (block) = link->label_decl;
acef433b
MM
1079 }
1080
6625cdb5 1081 named_labels = NULL;
acef433b
MM
1082}
1083
8d08fdba
MS
1084/* Exit a binding level.
1085 Pop the level off, and restore the state of the identifier-decl mappings
1086 that were in effect when this level was entered.
1087
1088 If KEEP == 1, this level had explicit declarations, so
1089 and create a "block" (a BLOCK node) for the level
1090 to record its declarations and subblocks for symbol table output.
1091
8d08fdba
MS
1092 If FUNCTIONBODY is nonzero, this level is the body of a function,
1093 so create a block as if KEEP were set and also clear out all
1094 label names.
1095
1096 If REVERSE is nonzero, reverse the order of decls before putting
1097 them into the BLOCK. */
1098
1099tree
11f6b451 1100poplevel (int keep, int reverse, int functionbody)
8d08fdba
MS
1101{
1102 register tree link;
1103 /* The chain of decls was accumulated in reverse order.
1104 Put it into forward order, just for cleanliness. */
1105 tree decls;
1106 int tmp = functionbody;
b35d4555 1107 int real_functionbody;
b35d4555 1108 tree subblocks;
8d08fdba
MS
1109 tree block = NULL_TREE;
1110 tree decl;
f181d4ae 1111 int leaving_for_scope;
ac20c67a 1112 scope_kind kind;
f181d4ae 1113
22ffcc6f 1114 timevar_push (TV_NAME_LOOKUP);
b35d4555 1115
a7e8c268 1116 my_friendly_assert (current_binding_level->kind != sk_class, 19990916);
b74a0560 1117
ac20c67a 1118 real_functionbody = (current_binding_level->kind == sk_cleanup
b35d4555 1119 ? ((functionbody = 0), tmp) : functionbody);
b35d4555
MM
1120 subblocks = functionbody >= 0 ? current_binding_level->blocks : 0;
1121
b74a0560
MM
1122 my_friendly_assert (!current_binding_level->class_shadowed,
1123 19990414);
8d08fdba 1124
536333d4
MM
1125 /* We used to use KEEP == 2 to indicate that the new block should go
1126 at the beginning of the list of blocks at this binding level,
1127 rather than the end. This hack is no longer used. */
1128 my_friendly_assert (keep == 0 || keep == 1, 0);
1129
ac20c67a 1130 if (current_binding_level->keep)
8d08fdba
MS
1131 keep = 1;
1132
6625cdb5
JM
1133 /* Any uses of undefined labels, and any defined labels, now operate
1134 under constraints of next binding contour. */
1135 if (cfun && !functionbody)
1136 {
e2500fed 1137 struct cp_binding_level *level_chain;
6625cdb5
JM
1138 level_chain = current_binding_level->level_chain;
1139 if (level_chain)
1140 {
1141 struct named_label_use_list *uses;
1142 struct named_label_list *labels;
1143 for (labels = named_labels; labels; labels = labels->next)
1144 if (labels->binding_level == current_binding_level)
1145 {
1146 tree decl;
a7e8c268 1147 if (current_binding_level->kind == sk_try)
826840d9 1148 labels->in_try_scope = 1;
a7e8c268 1149 if (current_binding_level->kind == sk_catch)
826840d9 1150 labels->in_catch_scope = 1;
6625cdb5
JM
1151 for (decl = labels->names_in_scope; decl;
1152 decl = TREE_CHAIN (decl))
1153 if (decl_jump_unsafe (decl))
1154 labels->bad_decls = tree_cons (NULL_TREE, decl,
1155 labels->bad_decls);
1156 labels->binding_level = level_chain;
1157 labels->names_in_scope = level_chain->names;
1158 }
1159
1160 for (uses = named_label_uses; uses; uses = uses->next)
1161 if (uses->binding_level == current_binding_level)
1162 {
1163 uses->binding_level = level_chain;
1164 uses->names_in_scope = level_chain->names;
1165 }
1166 }
1167 }
1168
8d08fdba
MS
1169 /* Get the decls in the order they were written.
1170 Usually current_binding_level->names is in reverse order.
1171 But parameter decls were previously put in forward order. */
1172
1173 if (reverse)
1174 current_binding_level->names
1175 = decls = nreverse (current_binding_level->names);
1176 else
1177 decls = current_binding_level->names;
1178
1179 /* Output any nested inline functions within this block
1180 if they weren't already output. */
8d08fdba
MS
1181 for (decl = decls; decl; decl = TREE_CHAIN (decl))
1182 if (TREE_CODE (decl) == FUNCTION_DECL
1183 && ! TREE_ASM_WRITTEN (decl)
1184 && DECL_INITIAL (decl) != NULL_TREE
6060a796
MS
1185 && TREE_ADDRESSABLE (decl)
1186 && decl_function_context (decl) == current_function_decl)
8d08fdba
MS
1187 {
1188 /* If this decl was copied from a file-scope decl
1189 on account of a block-scope extern decl,
1190 propagate TREE_ADDRESSABLE to the file-scope decl. */
1191 if (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE)
1192 TREE_ADDRESSABLE (DECL_ABSTRACT_ORIGIN (decl)) = 1;
1193 else
1194 {
1195 push_function_context ();
1196 output_inline_function (decl);
1197 pop_function_context ();
1198 }
1199 }
1200
d9b2d9da
MM
1201 /* When not in function-at-a-time mode, expand_end_bindings will
1202 warn about unused variables. But, in function-at-a-time mode
1203 expand_end_bindings is not passed the list of variables in the
1204 current scope, and therefore no warning is emitted. So, we
1205 explicitly warn here. */
1206 if (!processing_template_decl)
1207 warn_about_unused_variables (getdecls ());
1208
8d08fdba
MS
1209 /* If there were any declarations or structure tags in that level,
1210 or if this level is a function body,
1211 create a BLOCK to record them for the life of this function. */
8d08fdba 1212 block = NULL_TREE;
f444e36b 1213 if (keep == 1 || functionbody)
8d08fdba
MS
1214 block = make_node (BLOCK);
1215 if (block != NULL_TREE)
1216 {
f444e36b
MM
1217 BLOCK_VARS (block) = decls;
1218 BLOCK_SUBBLOCKS (block) = subblocks;
8d08fdba
MS
1219 }
1220
1221 /* In each subblock, record that this is its superior. */
8d08fdba
MS
1222 if (keep >= 0)
1223 for (link = subblocks; link; link = TREE_CHAIN (link))
1224 BLOCK_SUPERCONTEXT (link) = block;
1225
f181d4ae
MM
1226 /* We still support the old for-scope rules, whereby the variables
1227 in a for-init statement were in scope after the for-statement
92bc1323 1228 ended. We only use the new rules if flag_new_for_scope is
f181d4ae 1229 nonzero. */
68642fb6 1230 leaving_for_scope
a7e8c268 1231 = current_binding_level->kind == sk_for && flag_new_for_scope == 1;
f181d4ae
MM
1232
1233 /* Remove declarations for all the DECLs in this level. */
1234 for (link = decls; link; link = TREE_CHAIN (link))
1235 {
c3783399
NS
1236 if (leaving_for_scope && TREE_CODE (link) == VAR_DECL
1237 && DECL_NAME (link))
f181d4ae 1238 {
af6fd53f
GDR
1239 cxx_binding *outer_binding
1240 = IDENTIFIER_BINDING (DECL_NAME (link))->previous;
f181d4ae
MM
1241 tree ns_binding;
1242
1243 if (!outer_binding)
1244 ns_binding = IDENTIFIER_NAMESPACE_VALUE (DECL_NAME (link));
c7dda1e3
MM
1245 else
1246 ns_binding = NULL_TREE;
1247
68642fb6 1248 if (outer_binding
147135cc 1249 && outer_binding->scope == current_binding_level->level_chain)
f181d4ae 1250 /* We have something like:
68642fb6 1251
f181d4ae
MM
1252 int i;
1253 for (int i; ;);
68642fb6 1254
f181d4ae
MM
1255 and we are leaving the `for' scope. There's no reason to
1256 keep the binding of the inner `i' in this case. */
d8f8dca1 1257 pop_binding (DECL_NAME (link), link);
68642fb6 1258 else if ((outer_binding
147135cc
GDR
1259 && (TREE_CODE (outer_binding->value) == TYPE_DECL))
1260 || (ns_binding && TREE_CODE (ns_binding) == TYPE_DECL))
f181d4ae
MM
1261 /* Here, we have something like:
1262
1263 typedef int I;
1264
1265 void f () {
1266 for (int I; ;);
1267 }
1268
1269 We must pop the for-scope binding so we know what's a
1270 type and what isn't. */
d8f8dca1 1271 pop_binding (DECL_NAME (link), link);
e76a2646 1272 else
e76a2646 1273 {
f181d4ae
MM
1274 /* Mark this VAR_DECL as dead so that we can tell we left it
1275 there only for backward compatibility. */
1276 DECL_DEAD_FOR_LOCAL (link) = 1;
68642fb6 1277
517f3315 1278 /* Keep track of what should have happened when we
f181d4ae 1279 popped the binding. */
147135cc
GDR
1280 if (outer_binding && outer_binding->value)
1281 DECL_SHADOWED_FOR_VAR (link) = outer_binding->value;
f181d4ae
MM
1282
1283 /* Add it to the list of dead variables in the next
1284 outermost binding to that we can remove these when we
1285 leave that binding. */
1286 current_binding_level->level_chain->dead_vars_from_for
1287 = tree_cons (NULL_TREE, link,
1288 current_binding_level->level_chain->
1289 dead_vars_from_for);
1290
af6fd53f 1291 /* Although we don't pop the cxx_binding, we do clear
147135cc
GDR
1292 its SCOPE since the scope is going away now. */
1293 IDENTIFIER_BINDING (DECL_NAME (link))->scope = NULL;
e76a2646 1294 }
2ee887f2 1295 }
68642fb6 1296 else
8d6e462b 1297 {
f181d4ae 1298 /* Remove the binding. */
0034cf72
JM
1299 decl = link;
1300 if (TREE_CODE (decl) == TREE_LIST)
1301 decl = TREE_VALUE (decl);
2f939d94 1302 if (DECL_P (decl))
0034cf72
JM
1303 pop_binding (DECL_NAME (decl), decl);
1304 else if (TREE_CODE (decl) == OVERLOAD)
1305 pop_binding (DECL_NAME (OVL_FUNCTION (decl)), decl);
68642fb6 1306 else
a98facb0 1307 abort ();
8d08fdba 1308 }
f181d4ae 1309 }
8d08fdba 1310
f181d4ae
MM
1311 /* Remove declarations for any `for' variables from inner scopes
1312 that we kept around. */
1313 for (link = current_binding_level->dead_vars_from_for;
1314 link; link = TREE_CHAIN (link))
d8f8dca1 1315 pop_binding (DECL_NAME (TREE_VALUE (link)), TREE_VALUE (link));
2ee887f2 1316
f181d4ae
MM
1317 /* Restore the IDENTIFIER_TYPE_VALUEs. */
1318 for (link = current_binding_level->type_shadowed;
1319 link; link = TREE_CHAIN (link))
1320 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (link), TREE_VALUE (link));
acef433b
MM
1321
1322 /* Restore the IDENTIFIER_LABEL_VALUEs for local labels. */
1323 for (link = current_binding_level->shadowed_labels;
68642fb6 1324 link;
acef433b 1325 link = TREE_CHAIN (link))
6625cdb5 1326 pop_label (TREE_VALUE (link), TREE_PURPOSE (link));
acef433b 1327
f181d4ae
MM
1328 /* There may be OVERLOADs (wrapped in TREE_LISTs) on the BLOCK_VARs
1329 list if a `using' declaration put them there. The debugging
1330 back-ends won't understand OVERLOAD, so we remove them here.
1331 Because the BLOCK_VARS are (temporarily) shared with
1332 CURRENT_BINDING_LEVEL->NAMES we must do this fixup after we have
1333 popped all the bindings. */
1334 if (block)
1335 {
1336 tree* d;
8d6e462b 1337
a06d48ef
JM
1338 for (d = &BLOCK_VARS (block); *d; )
1339 {
1340 if (TREE_CODE (*d) == TREE_LIST)
1341 *d = TREE_CHAIN (*d);
1342 else
1343 d = &TREE_CHAIN (*d);
1344 }
8d6e462b 1345 }
8d08fdba
MS
1346
1347 /* If the level being exited is the top level of a function,
1348 check over all the labels. */
8d08fdba
MS
1349 if (functionbody)
1350 {
acef433b
MM
1351 /* Since this is the top level block of a function, the vars are
1352 the function's parameters. Don't leave them in the BLOCK
1353 because they are found in the FUNCTION_DECL instead. */
8d08fdba 1354 BLOCK_VARS (block) = 0;
acef433b 1355 pop_labels (block);
8d08fdba
MS
1356 }
1357
ac20c67a 1358 kind = current_binding_level->kind;
8d08fdba 1359
1a6daf15 1360 leave_scope ();
8d08fdba
MS
1361 if (functionbody)
1362 DECL_INITIAL (current_function_decl) = block;
1363 else if (block)
f444e36b
MM
1364 current_binding_level->blocks
1365 = chainon (current_binding_level->blocks, block);
1366
8d08fdba
MS
1367 /* If we did not make a block for the level just exited,
1368 any blocks made for inner levels
1369 (since they cannot be recorded as subblocks in that level)
1370 must be carried forward so they will later become subblocks
1371 of something else. */
1372 else if (subblocks)
536333d4
MM
1373 current_binding_level->blocks
1374 = chainon (current_binding_level->blocks, subblocks);
8d08fdba 1375
d9b2d9da
MM
1376 /* Each and every BLOCK node created here in `poplevel' is important
1377 (e.g. for proper debugging information) so if we created one
1378 earlier, mark it as "used". */
1379 if (block)
1380 TREE_USED (block) = 1;
1381
8d08fdba 1382 /* Take care of compiler's internal binding structures. */
ac20c67a 1383 if (kind == sk_cleanup)
8d08fdba 1384 {
d9b2d9da
MM
1385 tree scope_stmts;
1386
68642fb6 1387 scope_stmts
d9b2d9da 1388 = add_scope_stmt (/*begin_p=*/0, /*partial_p=*/1);
8d08fdba 1389 if (block)
d9b2d9da
MM
1390 {
1391 SCOPE_STMT_BLOCK (TREE_PURPOSE (scope_stmts)) = block;
1392 SCOPE_STMT_BLOCK (TREE_VALUE (scope_stmts)) = block;
1393 }
1394
8f471b0d 1395 block = poplevel (keep, reverse, functionbody);
8d08fdba
MS
1396 }
1397
22ffcc6f 1398 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, block);
8d08fdba
MS
1399}
1400
1401/* Delete the node BLOCK from the current binding level.
1402 This is used for the block inside a stmt expr ({...})
1403 so that the block can be reinserted where appropriate. */
1404
1405void
11f6b451 1406delete_block (tree block)
8d08fdba
MS
1407{
1408 tree t;
1409 if (current_binding_level->blocks == block)
1410 current_binding_level->blocks = TREE_CHAIN (block);
1411 for (t = current_binding_level->blocks; t;)
1412 {
1413 if (TREE_CHAIN (t) == block)
1414 TREE_CHAIN (t) = TREE_CHAIN (block);
1415 else
1416 t = TREE_CHAIN (t);
1417 }
1418 TREE_CHAIN (block) = NULL_TREE;
1419 /* Clear TREE_USED which is always set by poplevel.
1420 The flag is set again if insert_block is called. */
1421 TREE_USED (block) = 0;
1422}
1423
1424/* Insert BLOCK at the end of the list of subblocks of the
1425 current binding level. This is used when a BIND_EXPR is expanded,
1426 to handle the BLOCK node inside the BIND_EXPR. */
1427
1428void
11f6b451 1429insert_block (tree block)
8d08fdba
MS
1430{
1431 TREE_USED (block) = 1;
1432 current_binding_level->blocks
1433 = chainon (current_binding_level->blocks, block);
1434}
1435
8d08fdba
MS
1436/* Set the BLOCK node for the innermost scope
1437 (the one we are currently in). */
1438
1439void
11f6b451 1440set_block (tree block ATTRIBUTE_UNUSED )
8d08fdba 1441{
f444e36b
MM
1442 /* The RTL expansion machinery requires us to provide this callback,
1443 but it is not applicable in function-at-a-time mode. */
8d08fdba
MS
1444}
1445
1446/* Do a pushlevel for class declarations. */
e92cc029 1447
8d08fdba 1448void
11f6b451 1449pushlevel_class (void)
8d08fdba 1450{
f37e2274
GDR
1451 if (ENABLE_SCOPE_CHECKING)
1452 is_class_level = 1;
8d08fdba 1453
ac20c67a 1454 class_binding_level = begin_scope (sk_class, current_class_type);
8d08fdba
MS
1455}
1456
b74a0560 1457/* ...and a poplevel for class declarations. */
e92cc029 1458
273a708f 1459void
11f6b451 1460poplevel_class (void)
8d08fdba 1461{
e2500fed 1462 register struct cp_binding_level *level = class_binding_level;
8d08fdba
MS
1463 tree shadowed;
1464
22ffcc6f 1465 timevar_push (TV_NAME_LOOKUP);
8d08fdba 1466 my_friendly_assert (level != 0, 354);
68642fb6 1467
8d08fdba 1468 /* If we're leaving a toplevel class, don't bother to do the setting
ddd5a7c1 1469 of IDENTIFIER_CLASS_VALUE to NULL_TREE, since first of all this slot
8d08fdba 1470 shouldn't even be used when current_class_type isn't set, and second,
ddd5a7c1 1471 if we don't touch it here, we're able to use the cache effect if the
8d08fdba 1472 next time we're entering a class scope, it is the same class. */
b74a0560 1473 if (current_class_depth != 1)
8f032717 1474 {
e2500fed 1475 struct cp_binding_level* b;
8f032717
MM
1476
1477 /* Clear out our IDENTIFIER_CLASS_VALUEs. */
1478 for (shadowed = level->class_shadowed;
1479 shadowed;
1480 shadowed = TREE_CHAIN (shadowed))
1481 IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (shadowed)) = NULL_TREE;
68642fb6 1482
8f032717
MM
1483 /* Find the next enclosing class, and recreate
1484 IDENTIFIER_CLASS_VALUEs appropriate for that class. */
1485 b = level->level_chain;
a7e8c268 1486 while (b && b->kind != sk_class)
8f032717
MM
1487 b = b->level_chain;
1488
1489 if (b)
68642fb6
UD
1490 for (shadowed = b->class_shadowed;
1491 shadowed;
8f032717
MM
1492 shadowed = TREE_CHAIN (shadowed))
1493 {
af6fd53f
GDR
1494 cxx_binding *binding;
1495
1496 binding = IDENTIFIER_BINDING (TREE_PURPOSE (shadowed));
147135cc 1497 while (binding && binding->scope != b)
af6fd53f 1498 binding = binding->previous;
68642fb6 1499
af6fd53f 1500 if (binding)
68642fb6 1501 IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (shadowed))
147135cc 1502 = binding->value;
8f032717
MM
1503 }
1504 }
8d08fdba
MS
1505 else
1506 /* Remember to save what IDENTIFIER's were bound in this scope so we
1507 can recover from cache misses. */
e76a2646
MS
1508 {
1509 previous_class_type = current_class_type;
1510 previous_class_values = class_binding_level->class_shadowed;
1511 }
8d08fdba
MS
1512 for (shadowed = level->type_shadowed;
1513 shadowed;
1514 shadowed = TREE_CHAIN (shadowed))
2c73f9f5 1515 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (shadowed), TREE_VALUE (shadowed));
8d08fdba 1516
f181d4ae 1517 /* Remove the bindings for all of the class-level declarations. */
68642fb6
UD
1518 for (shadowed = level->class_shadowed;
1519 shadowed;
f181d4ae 1520 shadowed = TREE_CHAIN (shadowed))
d8f8dca1 1521 pop_binding (TREE_PURPOSE (shadowed), TREE_TYPE (shadowed));
f181d4ae 1522
38e01259 1523 /* Now, pop out of the binding level which we created up in the
8d08fdba 1524 `pushlevel_class' routine. */
f37e2274
GDR
1525 if (ENABLE_SCOPE_CHECKING)
1526 is_class_level = 1;
8d08fdba 1527
1a6daf15 1528 leave_scope ();
22ffcc6f 1529 timevar_pop (TV_NAME_LOOKUP);
8d08fdba 1530}
8f032717
MM
1531
1532/* We are entering the scope of a class. Clear IDENTIFIER_CLASS_VALUE
1533 for any names in enclosing classes. */
1534
1535void
11f6b451 1536clear_identifier_class_values (void)
8f032717
MM
1537{
1538 tree t;
1539
1540 if (!class_binding_level)
1541 return;
1542
1543 for (t = class_binding_level->class_shadowed;
1544 t;
1545 t = TREE_CHAIN (t))
1546 IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (t)) = NULL_TREE;
1547}
1548
838dfd8a 1549/* Returns nonzero if T is a virtual function table. */
0aafb128
MM
1550
1551int
11f6b451 1552vtable_decl_p (tree t, void* data ATTRIBUTE_UNUSED )
0aafb128
MM
1553{
1554 return (TREE_CODE (t) == VAR_DECL && DECL_VIRTUAL_P (t));
1555}
1556
838dfd8a 1557/* Returns nonzero if T is a TYPE_DECL for a type with virtual
0aafb128
MM
1558 functions. */
1559
1560int
11f6b451 1561vtype_decl_p (tree t, void *data ATTRIBUTE_UNUSED )
0aafb128
MM
1562{
1563 return (TREE_CODE (t) == TYPE_DECL
34249844
NS
1564 && TREE_CODE (TREE_TYPE (t)) == RECORD_TYPE
1565 && TYPE_POLYMORPHIC_P (TREE_TYPE (t)));
0aafb128
MM
1566}
1567
f71f87f9
MM
1568/* Return the declarations that are members of the namespace NS. */
1569
1570tree
11f6b451 1571cp_namespace_decls (tree ns)
f71f87f9
MM
1572{
1573 return NAMESPACE_LEVEL (ns)->names;
1574}
1575
70b76b34
DP
1576struct walk_globals_data {
1577 walk_globals_pred p;
1578 walk_globals_fn f;
1579 void *data;
1580};
1581
1582/* Walk the vtable declarations in NAMESPACE. Whenever one is found
838dfd8a
KH
1583 for which P returns nonzero, call F with its address. If any call
1584 to F returns a nonzero value, return a nonzero value. */
70b76b34
DP
1585
1586static int
11f6b451 1587walk_vtables_r (tree namespace, void* data)
70b76b34
DP
1588{
1589 struct walk_globals_data* wgd = (struct walk_globals_data *) data;
1590 walk_globals_fn f = wgd->f;
1591 void *d = wgd->data;
1592 tree decl = NAMESPACE_LEVEL (namespace)->vtables;
1593 int result = 0;
1594
1595 for (; decl ; decl = TREE_CHAIN (decl))
0246d49e 1596 result |= (*f) (&decl, d);
70b76b34
DP
1597
1598 return result;
1599}
1600
1601/* Walk the vtable declarations. Whenever one is found for which P
838dfd8a
KH
1602 returns nonzero, call F with its address. If any call to F
1603 returns a nonzero value, return a nonzero value. */
848eed92
GDR
1604bool
1605walk_vtables (walk_globals_pred p, walk_globals_fn f, void *data)
70b76b34
DP
1606{
1607 struct walk_globals_data wgd;
1608 wgd.p = p;
1609 wgd.f = f;
1610 wgd.data = data;
1611
1612 return walk_namespaces (walk_vtables_r, &wgd);
1613}
1614
0aafb128
MM
1615/* Walk all the namespaces contained NAMESPACE, including NAMESPACE
1616 itself, calling F for each. The DATA is passed to F as well. */
1617
1618static int
11f6b451 1619walk_namespaces_r (tree namespace, walk_namespaces_fn f, void* data)
0aafb128 1620{
0aafb128 1621 int result = 0;
70b76b34 1622 tree current = NAMESPACE_LEVEL (namespace)->namespaces;
0aafb128
MM
1623
1624 result |= (*f) (namespace, data);
1625
70b76b34
DP
1626 for (; current; current = TREE_CHAIN (current))
1627 result |= walk_namespaces_r (current, f, data);
0aafb128
MM
1628
1629 return result;
1630}
1631
1632/* Walk all the namespaces, calling F for each. The DATA is passed to
1633 F as well. */
1634
1635int
11f6b451 1636walk_namespaces (walk_namespaces_fn f, void* data)
0aafb128
MM
1637{
1638 return walk_namespaces_r (global_namespace, f, data);
1639}
1640
0aafb128 1641/* Walk the global declarations in NAMESPACE. Whenever one is found
838dfd8a
KH
1642 for which P returns nonzero, call F with its address. If any call
1643 to F returns a nonzero value, return a nonzero value. */
0aafb128 1644
68642fb6 1645static int
11f6b451 1646walk_globals_r (tree namespace, void* data)
0aafb128
MM
1647{
1648 struct walk_globals_data* wgd = (struct walk_globals_data *) data;
1649 walk_globals_pred p = wgd->p;
1650 walk_globals_fn f = wgd->f;
1651 void *d = wgd->data;
1652 tree *t;
1653 int result = 0;
1654
1655 t = &NAMESPACE_LEVEL (namespace)->names;
1656
1657 while (*t)
1658 {
1659 tree glbl = *t;
1660
1661 if ((*p) (glbl, d))
1662 result |= (*f) (t, d);
1663
1664 /* If F changed *T, then *T still points at the next item to
1665 examine. */
1666 if (*t == glbl)
1667 t = &TREE_CHAIN (*t);
1668 }
1669
1670 return result;
1671}
1672
1673/* Walk the global declarations. Whenever one is found for which P
848eed92
GDR
1674 returns true, call F with its address. If any call to F
1675 returns true, return true. */
0aafb128 1676
848eed92
GDR
1677bool
1678walk_globals (walk_globals_pred p, walk_globals_fn f, void *data)
0aafb128
MM
1679{
1680 struct walk_globals_data wgd;
1681 wgd.p = p;
1682 wgd.f = f;
1683 wgd.data = data;
1684
1685 return walk_namespaces (walk_globals_r, &wgd);
1686}
1687
1688/* Call wrapup_globals_declarations for the globals in NAMESPACE. If
1689 DATA is non-NULL, this is the last time we will call
1690 wrapup_global_declarations for this NAMESPACE. */
1691
1692int
11f6b451 1693wrapup_globals_for_namespace (tree namespace, void* data)
0aafb128 1694{
1d555f7a
MA
1695 struct cp_binding_level *level = NAMESPACE_LEVEL (namespace);
1696 varray_type statics = level->static_decls;
1697 tree *vec = &VARRAY_TREE (statics, 0);
1698 int len = VARRAY_ACTIVE_SIZE (statics);
0aafb128
MM
1699 int last_time = (data != 0);
1700
0352cfc8
MM
1701 if (last_time)
1702 {
1703 check_global_declarations (vec, len);
1704 return 0;
1705 }
1706
0352cfc8 1707 /* Write out any globals that need to be output. */
1d555f7a 1708 return wrapup_global_declarations (vec, len);
0aafb128
MM
1709}
1710
8d08fdba
MS
1711\f
1712/* For debugging. */
5566b478
MS
1713static int no_print_functions = 0;
1714static int no_print_builtins = 0;
8d08fdba 1715
5e0c54e5
GDR
1716/* Called from print_binding_level through binding_table_foreach to
1717 print the content of binding ENTRY. DATA is a pointer to line offset
1718 marker. */
1719static void
1720bt_print_entry (binding_entry entry, void *data)
1721{
1722 int *p = (int *) data;
1723 int len;
1724
1725 if (entry->name == NULL)
1726 len = 3;
1727 else if (entry->name == TYPE_IDENTIFIER (entry->type))
1728 len = 2;
1729 else
1730 len = 4;
1731 len = 4;
1732
1733 *p += len;
1734
1735 if (*p > 5)
1736 {
1737 fprintf (stderr, "\n\t");
1738 *p = len;
1739 }
1740 if (entry->name == NULL)
1741 {
1742 print_node_brief (stderr, "<unnamed-typedef", entry->type, 0);
1743 fprintf (stderr, ">");
1744 }
1745 else if (entry->name == TYPE_IDENTIFIER (entry->type))
1746 print_node_brief (stderr, "", entry->type, 0);
1747 else
1748 {
1749 print_node_brief (stderr, "<typedef", entry->name, 0);
1750 print_node_brief (stderr, "", entry->type, 0);
1751 fprintf (stderr, ">");
1752 }
1753}
1754
8d08fdba 1755void
11f6b451 1756print_binding_level (struct cp_binding_level* lvl)
8d08fdba
MS
1757{
1758 tree t;
1759 int i = 0, len;
75b6f3fd 1760 fprintf (stderr, " blocks=" HOST_PTR_PRINTF, (void *) lvl->blocks);
8d08fdba
MS
1761 if (lvl->more_cleanups_ok)
1762 fprintf (stderr, " more-cleanups-ok");
1763 if (lvl->have_cleanups)
1764 fprintf (stderr, " have-cleanups");
8d08fdba
MS
1765 fprintf (stderr, "\n");
1766 if (lvl->names)
1767 {
1768 fprintf (stderr, " names:\t");
1769 /* We can probably fit 3 names to a line? */
1770 for (t = lvl->names; t; t = TREE_CHAIN (t))
1771 {
68642fb6 1772 if (no_print_functions && (TREE_CODE (t) == FUNCTION_DECL))
8d08fdba
MS
1773 continue;
1774 if (no_print_builtins
fc378698 1775 && (TREE_CODE (t) == TYPE_DECL)
f31686a3 1776 && (!strcmp (DECL_SOURCE_FILE (t),"<built-in>")))
8d08fdba
MS
1777 continue;
1778
1779 /* Function decls tend to have longer names. */
1780 if (TREE_CODE (t) == FUNCTION_DECL)
1781 len = 3;
1782 else
1783 len = 2;
1784 i += len;
1785 if (i > 6)
1786 {
1787 fprintf (stderr, "\n\t");
1788 i = len;
1789 }
1790 print_node_brief (stderr, "", t, 0);
bd6dd845 1791 if (t == error_mark_node)
8d08fdba
MS
1792 break;
1793 }
1794 if (i)
1795 fprintf (stderr, "\n");
1796 }
5e0c54e5 1797 if (lvl->type_decls)
8d08fdba
MS
1798 {
1799 fprintf (stderr, " tags:\t");
1800 i = 0;
5e0c54e5 1801 binding_table_foreach (lvl->type_decls, bt_print_entry, &i);
8d08fdba
MS
1802 if (i)
1803 fprintf (stderr, "\n");
1804 }
8d08fdba
MS
1805 if (lvl->class_shadowed)
1806 {
1807 fprintf (stderr, " class-shadowed:");
1808 for (t = lvl->class_shadowed; t; t = TREE_CHAIN (t))
1809 {
1810 fprintf (stderr, " %s ", IDENTIFIER_POINTER (TREE_PURPOSE (t)));
1811 }
1812 fprintf (stderr, "\n");
1813 }
1814 if (lvl->type_shadowed)
1815 {
1816 fprintf (stderr, " type-shadowed:");
1817 for (t = lvl->type_shadowed; t; t = TREE_CHAIN (t))
1818 {
8d08fdba 1819 fprintf (stderr, " %s ", IDENTIFIER_POINTER (TREE_PURPOSE (t)));
8d08fdba
MS
1820 }
1821 fprintf (stderr, "\n");
1822 }
1823}
1824
1825void
11f6b451 1826print_other_binding_stack (struct cp_binding_level *stack)
8d08fdba 1827{
e2500fed 1828 struct cp_binding_level *level;
5f52c0e0 1829 for (level = stack; !global_scope_p (level); level = level->level_chain)
8d08fdba 1830 {
75b6f3fd 1831 fprintf (stderr, "binding level " HOST_PTR_PRINTF "\n", (void *) level);
8d08fdba
MS
1832 print_binding_level (level);
1833 }
1834}
1835
1836void
11f6b451 1837print_binding_stack (void)
8d08fdba 1838{
e2500fed 1839 struct cp_binding_level *b;
75b6f3fd
KG
1840 fprintf (stderr, "current_binding_level=" HOST_PTR_PRINTF
1841 "\nclass_binding_level=" HOST_PTR_PRINTF
1842 "\nNAMESPACE_LEVEL (global_namespace)=" HOST_PTR_PRINTF "\n",
1843 (void *) current_binding_level, (void *) class_binding_level,
5f52c0e0 1844 (void *) NAMESPACE_LEVEL (global_namespace));
8d08fdba
MS
1845 if (class_binding_level)
1846 {
1847 for (b = class_binding_level; b; b = b->level_chain)
1848 if (b == current_binding_level)
1849 break;
1850 if (b)
1851 b = class_binding_level;
1852 else
1853 b = current_binding_level;
1854 }
1855 else
1856 b = current_binding_level;
1857 print_other_binding_stack (b);
1858 fprintf (stderr, "global:\n");
5f52c0e0 1859 print_binding_level (NAMESPACE_LEVEL (global_namespace));
8d08fdba 1860}
a9aedbc2 1861
2c73f9f5
ML
1862/* Namespace binding access routines: The namespace_bindings field of
1863 the identifier is polymorphic, with three possible values:
af6fd53f 1864 NULL_TREE, a list of "cxx_binding"s. */
30394414 1865
dff6b454
RK
1866/* Push into the scope of the NAME namespace. If NAME is NULL_TREE, then we
1867 select a name that is unique to this compilation unit. */
e92cc029 1868
a9aedbc2 1869void
11f6b451 1870push_namespace (tree name)
a9aedbc2 1871{
b370501f 1872 tree d = NULL_TREE;
30394414
JM
1873 int need_new = 1;
1874 int implicit_use = 0;
22ffcc6f
GDR
1875
1876 timevar_push (TV_NAME_LOOKUP);
1877
82a2669e
GDR
1878 /* We should not get here if the global_namespace is not yet constructed
1879 nor if NAME designates the global namespace: The global scope is
1880 constructed elsewhere. */
1881 my_friendly_assert (global_namespace != NULL && name != global_scope_name,
1882 20030531);
1883
1884 if (!name)
30394414 1885 {
3ab52652
ML
1886 /* The name of anonymous namespace is unique for the translation
1887 unit. */
0c8feefe
MM
1888 if (!anonymous_namespace_name)
1889 anonymous_namespace_name = get_file_function_name ('N');
1890 name = anonymous_namespace_name;
3ab52652
ML
1891 d = IDENTIFIER_NAMESPACE_VALUE (name);
1892 if (d)
1893 /* Reopening anonymous namespace. */
1894 need_new = 0;
30394414 1895 implicit_use = 1;
2c73f9f5 1896 }
30394414
JM
1897 else
1898 {
a1c65f9f 1899 /* Check whether this is an extended namespace definition. */
30394414
JM
1900 d = IDENTIFIER_NAMESPACE_VALUE (name);
1901 if (d != NULL_TREE && TREE_CODE (d) == NAMESPACE_DECL)
3e3f722c
ML
1902 {
1903 need_new = 0;
1904 if (DECL_NAMESPACE_ALIAS (d))
1905 {
33bd39a2 1906 error ("namespace alias `%D' not allowed here, assuming `%D'",
3e3f722c
ML
1907 d, DECL_NAMESPACE_ALIAS (d));
1908 d = DECL_NAMESPACE_ALIAS (d);
1909 }
1910 }
30394414 1911 }
68642fb6 1912
30394414
JM
1913 if (need_new)
1914 {
a1c65f9f 1915 /* Make a new namespace, binding the name to it. */
6b57ac29 1916 d = build_lang_decl (NAMESPACE_DECL, name, void_type_node);
82a2669e
GDR
1917 DECL_CONTEXT (d) = FROB_CONTEXT (current_namespace);
1918 d = pushdecl (d);
ac20c67a 1919 begin_scope (sk_namespace, d);
30394414 1920 }
2c73f9f5
ML
1921 else
1922 resume_binding_level (NAMESPACE_LEVEL (d));
1923
30394414
JM
1924 if (implicit_use)
1925 do_using_directive (d);
a1c65f9f 1926 /* Enter the name space. */
30394414 1927 current_namespace = d;
22ffcc6f
GDR
1928
1929 timevar_pop (TV_NAME_LOOKUP);
a9aedbc2
MS
1930}
1931
1932/* Pop from the scope of the current namespace. */
e92cc029 1933
a9aedbc2 1934void
11f6b451 1935pop_namespace (void)
a9aedbc2 1936{
1dbb6023 1937 my_friendly_assert (current_namespace != global_namespace, 20010801);
cb0dbb9a 1938 current_namespace = CP_DECL_CONTEXT (current_namespace);
2c73f9f5 1939 /* The binding level is not popped, as it might be re-opened later. */
1a6daf15 1940 leave_scope ();
2c73f9f5 1941}
a9aedbc2 1942
61fc8c9e
MM
1943/* Push into the scope of the namespace NS, even if it is deeply
1944 nested within another namespace. */
1945
1946void
11f6b451 1947push_nested_namespace (tree ns)
61fc8c9e
MM
1948{
1949 if (ns == global_namespace)
1950 push_to_top_level ();
1951 else
1952 {
1953 push_nested_namespace (CP_DECL_CONTEXT (ns));
1954 push_namespace (DECL_NAME (ns));
1955 }
1956}
1957
1958/* Pop back from the scope of the namespace NS, which was previously
1959 entered with push_nested_namespace. */
68642fb6 1960
61fc8c9e 1961void
11f6b451 1962pop_nested_namespace (tree ns)
61fc8c9e 1963{
22ffcc6f 1964 timevar_push (TV_NAME_LOOKUP);
61fc8c9e
MM
1965 while (ns != global_namespace)
1966 {
1967 pop_namespace ();
1968 ns = CP_DECL_CONTEXT (ns);
1969 }
1970
1971 pop_from_top_level ();
22ffcc6f 1972 timevar_pop (TV_NAME_LOOKUP);
61fc8c9e
MM
1973}
1974
8d08fdba 1975\f
000d38ea
GDR
1976/* Allocate storage for saving a C++ binding. */
1977#define cxx_saved_binding_make() \
1978 (ggc_alloc (sizeof (cxx_saved_binding)))
1979
1980struct cxx_saved_binding GTY(())
1981{
1982 /* Link that chains saved C++ bindings for a given name into a stack. */
1983 cxx_saved_binding *previous;
1984 /* The name of the current binding. */
1985 tree identifier;
1986 /* The binding we're saving. */
af6fd53f 1987 cxx_binding *binding;
000d38ea
GDR
1988 tree class_value;
1989 tree real_type_value;
1990};
1991
8d08fdba
MS
1992/* Subroutines for reverting temporarily to top-level for instantiation
1993 of templates and such. We actually need to clear out the class- and
1994 local-value slots of all identifiers, so that only the global values
1995 are at all visible. Simply setting current_binding_level to the global
1996 scope isn't enough, because more binding levels may be pushed. */
9cd64686 1997struct saved_scope *scope_chain;
8d08fdba 1998
000d38ea
GDR
1999static cxx_saved_binding *
2000store_bindings (tree names, cxx_saved_binding *old_bindings)
45537677
MS
2001{
2002 tree t;
000d38ea 2003 cxx_saved_binding *search_bindings = old_bindings;
c00996a3 2004
22ffcc6f 2005 timevar_push (TV_NAME_LOOKUP);
45537677
MS
2006 for (t = names; t; t = TREE_CHAIN (t))
2007 {
000d38ea
GDR
2008 tree id;
2009 cxx_saved_binding *saved;
2010 cxx_saved_binding *t1;
45537677
MS
2011
2012 if (TREE_CODE (t) == TREE_LIST)
2013 id = TREE_PURPOSE (t);
2014 else
2015 id = DECL_NAME (t);
2016
68642fb6 2017 if (!id
f181d4ae
MM
2018 /* Note that we may have an IDENTIFIER_CLASS_VALUE even when
2019 we have no IDENTIFIER_BINDING if we have left the class
2020 scope, but cached the class-level declarations. */
2021 || !(IDENTIFIER_BINDING (id) || IDENTIFIER_CLASS_VALUE (id)))
45537677
MS
2022 continue;
2023
000d38ea
GDR
2024 for (t1 = search_bindings; t1; t1 = t1->previous)
2025 if (t1->identifier == id)
45537677 2026 goto skip_it;
78957a2a 2027
31a714f6 2028 my_friendly_assert (TREE_CODE (id) == IDENTIFIER_NODE, 135);
000d38ea
GDR
2029 saved = cxx_saved_binding_make ();
2030 saved->previous = old_bindings;
2031 saved->identifier = id;
2032 saved->binding = IDENTIFIER_BINDING (id);
2033 saved->class_value = IDENTIFIER_CLASS_VALUE (id);;
2034 saved->real_type_value = REAL_IDENTIFIER_TYPE_VALUE (id);
af6fd53f 2035 IDENTIFIER_BINDING (id) = NULL;
31a714f6 2036 IDENTIFIER_CLASS_VALUE (id) = NULL_TREE;
000d38ea 2037 old_bindings = saved;
45537677
MS
2038 skip_it:
2039 ;
2040 }
22ffcc6f 2041 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, old_bindings);
45537677
MS
2042}
2043
8d08fdba 2044void
11f6b451 2045maybe_push_to_top_level (int pseudo)
8d08fdba 2046{
a8f73d4b 2047 struct saved_scope *s;
e2500fed 2048 struct cp_binding_level *b;
000d38ea 2049 cxx_saved_binding *old_bindings;
a8f73d4b
MM
2050 int need_pop;
2051
22ffcc6f 2052 timevar_push (TV_NAME_LOOKUP);
c68b0a84 2053 s = ggc_alloc_cleared (sizeof (struct saved_scope));
8d08fdba 2054
9cd64686
MM
2055 b = scope_chain ? current_binding_level : 0;
2056
a8f73d4b 2057 /* If we're in the middle of some function, save our state. */
01d939e8 2058 if (cfun)
a8f73d4b
MM
2059 {
2060 need_pop = 1;
2061 push_function_context_to (NULL_TREE);
2062 }
2063 else
2064 need_pop = 0;
e349ee73 2065
000d38ea 2066 old_bindings = NULL;
9cd64686 2067 if (scope_chain && previous_class_type)
a50f0918
MS
2068 old_bindings = store_bindings (previous_class_values, old_bindings);
2069
5f52c0e0 2070 /* Have to include the global scope, because class-scope decls
8d08fdba
MS
2071 aren't listed anywhere useful. */
2072 for (; b; b = b->level_chain)
2073 {
2074 tree t;
2075
2c73f9f5
ML
2076 /* Template IDs are inserted into the global level. If they were
2077 inserted into namespace level, finish_file wouldn't find them
2078 when doing pending instantiations. Therefore, don't stop at
2079 namespace level, but continue until :: . */
a7e8c268 2080 if (global_scope_p (b) || (pseudo && b->kind == sk_template_parms))
5566b478 2081 break;
8d08fdba 2082
45537677 2083 old_bindings = store_bindings (b->names, old_bindings);
cffa8729 2084 /* We also need to check class_shadowed to save class-level type
45537677 2085 bindings, since pushclass doesn't fill in b->names. */
a7e8c268 2086 if (b->kind == sk_class)
cffa8729 2087 old_bindings = store_bindings (b->class_shadowed, old_bindings);
8d08fdba 2088
8d08fdba
MS
2089 /* Unwind type-value slots back to top level. */
2090 for (t = b->type_shadowed; t; t = TREE_CHAIN (t))
2091 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (t), TREE_VALUE (t));
2092 }
fc0e7bf5
MM
2093 s->prev = scope_chain;
2094 s->old_bindings = old_bindings;
a8f73d4b
MM
2095 s->bindings = b;
2096 s->need_pop_function_context = need_pop;
2097 s->function_decl = current_function_decl;
0f4237c2 2098 s->last_parms = last_function_parms;
fc0e7bf5
MM
2099
2100 scope_chain = s;
8d08fdba 2101 current_function_decl = NULL_TREE;
9cd64686 2102 VARRAY_TREE_INIT (current_lang_base, 10, "current_lang_base");
51c184be 2103 current_lang_name = lang_name_cplusplus;
2c73f9f5 2104 current_namespace = global_namespace;
22ffcc6f 2105 timevar_pop (TV_NAME_LOOKUP);
5566b478
MS
2106}
2107
2108void
11f6b451 2109push_to_top_level (void)
5566b478
MS
2110{
2111 maybe_push_to_top_level (0);
8d08fdba
MS
2112}
2113
2114void
11f6b451 2115pop_from_top_level (void)
8d08fdba 2116{
9cd64686 2117 struct saved_scope *s = scope_chain;
000d38ea 2118 cxx_saved_binding *saved;
8d08fdba 2119
22ffcc6f 2120 timevar_push (TV_NAME_LOOKUP);
e76a2646 2121 /* Clear out class-level bindings cache. */
8d08fdba 2122 if (previous_class_type)
8f032717 2123 invalidate_class_lookup_cache ();
8d08fdba 2124
e2500fed 2125 current_lang_base = 0;
9cd64686
MM
2126
2127 scope_chain = s->prev;
000d38ea 2128 for (saved = s->old_bindings; saved; saved = saved->previous)
8d08fdba 2129 {
000d38ea 2130 tree id = saved->identifier;
31a714f6 2131
000d38ea
GDR
2132 IDENTIFIER_BINDING (id) = saved->binding;
2133 IDENTIFIER_CLASS_VALUE (id) = saved->class_value;
2134 SET_IDENTIFIER_TYPE_VALUE (id, saved->real_type_value);
8d08fdba 2135 }
9cd64686 2136
a8f73d4b
MM
2137 /* If we were in the middle of compiling a function, restore our
2138 state. */
2139 if (s->need_pop_function_context)
2140 pop_function_context_from (NULL_TREE);
2141 current_function_decl = s->function_decl;
0f4237c2 2142 last_function_parms = s->last_parms;
22ffcc6f 2143 timevar_pop (TV_NAME_LOOKUP);
8d08fdba
MS
2144}
2145\f
4b0d3cbe
MM
2146/* Push a definition of struct, union or enum tag named ID. into
2147 binding_level B. DECL is a TYPE_DECL for the type. We assume that
2148 the tag ID is not already defined. */
8d08fdba
MS
2149
2150static void
11f6b451 2151set_identifier_type_value_with_scope (tree id,
4b0d3cbe 2152 tree decl,
11f6b451 2153 struct cp_binding_level* b)
8d08fdba 2154{
4b0d3cbe
MM
2155 tree type;
2156
a7e8c268 2157 if (b->kind != sk_namespace)
8d08fdba 2158 {
2c73f9f5 2159 /* Shadow the marker, not the real thing, so that the marker
a1c65f9f 2160 gets restored later. */
2c73f9f5 2161 tree old_type_value = REAL_IDENTIFIER_TYPE_VALUE (id);
8d08fdba
MS
2162 b->type_shadowed
2163 = tree_cons (id, old_type_value, b->type_shadowed);
4b0d3cbe 2164 type = decl ? TREE_TYPE (decl) : NULL_TREE;
8d08fdba 2165 }
2c73f9f5
ML
2166 else
2167 {
ed3cf953 2168 cxx_binding *binding =
4b0d3cbe
MM
2169 binding_for_name (NAMESPACE_LEVEL (current_namespace), id);
2170 if (decl)
2171 {
147135cc 2172 if (binding->value)
c87ceb13 2173 supplement_binding (binding, decl);
4b0d3cbe 2174 else
147135cc 2175 binding->value = decl;
4b0d3cbe
MM
2176 }
2177 else
2178 abort ();
a1c65f9f 2179 /* Store marker instead of real type. */
2c73f9f5
ML
2180 type = global_type_node;
2181 }
8d08fdba
MS
2182 SET_IDENTIFIER_TYPE_VALUE (id, type);
2183}
2184
8f032717 2185/* As set_identifier_type_value_with_scope, but using current_binding_level. */
8d08fdba
MS
2186
2187void
4b0d3cbe 2188set_identifier_type_value (tree id, tree decl)
8d08fdba 2189{
4b0d3cbe 2190 set_identifier_type_value_with_scope (id, decl, current_binding_level);
8d08fdba
MS
2191}
2192
a1c65f9f 2193/* Return the type associated with id. */
2c73f9f5
ML
2194
2195tree
11f6b451 2196identifier_type_value (tree id)
2c73f9f5 2197{
22ffcc6f 2198 timevar_push (TV_NAME_LOOKUP);
a1c65f9f 2199 /* There is no type with that name, anywhere. */
2c73f9f5 2200 if (REAL_IDENTIFIER_TYPE_VALUE (id) == NULL_TREE)
22ffcc6f 2201 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
a1c65f9f 2202 /* This is not the type marker, but the real thing. */
2c73f9f5 2203 if (REAL_IDENTIFIER_TYPE_VALUE (id) != global_type_node)
22ffcc6f 2204 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, REAL_IDENTIFIER_TYPE_VALUE (id));
2c73f9f5 2205 /* Have to search for it. It must be on the global level, now.
9bcb9aae 2206 Ask lookup_name not to return non-types. */
a723baf1 2207 id = lookup_name_real (id, 2, 1, 0, LOOKUP_COMPLAIN);
2c73f9f5 2208 if (id)
22ffcc6f
GDR
2209 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, TREE_TYPE (id));
2210 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
2c73f9f5
ML
2211}
2212
a9aedbc2
MS
2213/* Pop off extraneous binding levels left over due to syntax errors.
2214
2215 We don't pop past namespaces, as they might be valid. */
e92cc029 2216
8926095f 2217void
11f6b451 2218pop_everything (void)
8926095f 2219{
f37e2274
GDR
2220 if (ENABLE_SCOPE_CHECKING)
2221 verbatim ("XXX entering pop_everything ()\n");
8f032717 2222 while (!toplevel_bindings_p ())
8926095f 2223 {
a7e8c268 2224 if (current_binding_level->kind == sk_class)
b74a0560 2225 pop_nested_class ();
8926095f
MS
2226 else
2227 poplevel (0, 0, 0);
2228 }
f37e2274
GDR
2229 if (ENABLE_SCOPE_CHECKING)
2230 verbatim ("XXX leaving pop_everything ()\n");
8926095f
MS
2231}
2232
39c01e4c
MM
2233/* The type TYPE is being declared. If it is a class template, or a
2234 specialization of a class template, do any processing required and
838dfd8a 2235 perform error-checking. If IS_FRIEND is nonzero, this TYPE is
39c01e4c
MM
2236 being declared a friend. B is the binding level at which this TYPE
2237 should be bound.
2238
2239 Returns the TYPE_DECL for TYPE, which may have been altered by this
2240 processing. */
2241
68642fb6 2242static tree
11f6b451
NN
2243maybe_process_template_type_declaration (tree type,
2244 int globalize,
2245 struct cp_binding_level* b)
39c01e4c
MM
2246{
2247 tree decl = TYPE_NAME (type);
68642fb6 2248
39c01e4c
MM
2249 if (processing_template_parmlist)
2250 /* You can't declare a new template type in a template parameter
2251 list. But, you can declare a non-template type:
68642fb6 2252
39c01e4c 2253 template <class A*> struct S;
68642fb6 2254
39c01e4c
MM
2255 is a forward-declaration of `A'. */
2256 ;
68642fb6 2257 else
39c01e4c
MM
2258 {
2259 maybe_check_template_type (type);
2260
68642fb6 2261 my_friendly_assert (IS_AGGR_TYPE (type)
ed44da02 2262 || TREE_CODE (type) == ENUMERAL_TYPE, 0);
68642fb6
UD
2263
2264
3ebc5c52 2265 if (processing_template_decl)
39c01e4c
MM
2266 {
2267 /* This may change after the call to
2268 push_template_decl_real, but we want the original value. */
2269 tree name = DECL_NAME (decl);
2270
2271 decl = push_template_decl_real (decl, globalize);
2272 /* If the current binding level is the binding level for the
2273 template parameters (see the comment in
2274 begin_template_parm_list) and the enclosing level is a class
2275 scope, and we're not looking at a friend, push the
2276 declaration of the member class into the class scope. In the
2277 friend case, push_template_decl will already have put the
2278 friend into global scope, if appropriate. */
ed44da02 2279 if (TREE_CODE (type) != ENUMERAL_TYPE
a7e8c268
MM
2280 && !globalize && b->kind == sk_template_parms
2281 && b->level_chain->kind == sk_class)
39c01e4c 2282 {
61a127b3 2283 finish_member_declaration (CLASSTYPE_TI_TEMPLATE (type));
5e0c54e5 2284 /* Put this UDT in the table of UDTs for the class, since
39c01e4c
MM
2285 that won't happen below because B is not the class
2286 binding level, but is instead the pseudo-global level. */
5e0c54e5
GDR
2287 if (b->level_chain->type_decls == NULL)
2288 b->level_chain->type_decls =
2289 binding_table_new (SCOPE_DEFAULT_HT_SIZE);
2290 binding_table_insert (b->level_chain->type_decls, name, type);
d0f062fb 2291 if (!COMPLETE_TYPE_P (current_class_type))
7088fca9
KL
2292 {
2293 maybe_add_class_template_decl_list (current_class_type,
2294 type, /*friend_p=*/0);
5e0c54e5
GDR
2295 CLASSTYPE_NESTED_UTDS (current_class_type) =
2296 b->level_chain->type_decls;
7088fca9 2297 }
39c01e4c
MM
2298 }
2299 }
2300 }
2301
2302 return decl;
2303}
2304
9188c363
MM
2305/* In C++, you don't have to write `struct S' to refer to `S'; you
2306 can just use `S'. We accomplish this by creating a TYPE_DECL as
2307 if the user had written `typedef struct S S'. Create and return
2308 the TYPE_DECL for TYPE. */
2309
2310tree
11f6b451 2311create_implicit_typedef (tree name, tree type)
9188c363
MM
2312{
2313 tree decl;
2314
2315 decl = build_decl (TYPE_DECL, name, type);
c727aa5e 2316 DECL_ARTIFICIAL (decl) = 1;
9188c363
MM
2317 /* There are other implicit type declarations, like the one *within*
2318 a class that allows you to write `S::S'. We must distinguish
2319 amongst these. */
2320 SET_DECL_IMPLICIT_TYPEDEF_P (decl);
2321 TYPE_NAME (type) = decl;
2322
2323 return decl;
2324}
2325
8e4ce833
JJ
2326/* Remember a local name for name-mangling purposes. */
2327
2328static void
11f6b451 2329push_local_name (tree decl)
8e4ce833
JJ
2330{
2331 size_t i, nelts;
2332 tree t, name;
2333
22ffcc6f 2334 timevar_push (TV_NAME_LOOKUP);
8e4ce833
JJ
2335 if (!local_names)
2336 VARRAY_TREE_INIT (local_names, 8, "local_names");
2337
2338 name = DECL_NAME (decl);
2339
2340 nelts = VARRAY_ACTIVE_SIZE (local_names);
2341 for (i = 0; i < nelts; i++)
2342 {
2343 t = VARRAY_TREE (local_names, i);
2344 if (DECL_NAME (t) == name)
2345 {
2346 if (!DECL_LANG_SPECIFIC (decl))
2347 retrofit_lang_decl (decl);
e2500fed 2348 DECL_LANG_SPECIFIC (decl)->decl_flags.u2sel = 1;
8e4ce833
JJ
2349 if (DECL_LANG_SPECIFIC (t))
2350 DECL_DISCRIMINATOR (decl) = DECL_DISCRIMINATOR (t) + 1;
2351 else
2352 DECL_DISCRIMINATOR (decl) = 1;
2353
2354 VARRAY_TREE (local_names, i) = decl;
de94b46c
KG
2355 timevar_pop (TV_NAME_LOOKUP);
2356 return;
8e4ce833
JJ
2357 }
2358 }
2359
2360 VARRAY_PUSH_TREE (local_names, decl);
22ffcc6f 2361 timevar_pop (TV_NAME_LOOKUP);
8e4ce833
JJ
2362}
2363
8d08fdba 2364/* Push a tag name NAME for struct/class/union/enum type TYPE.
ac20c67a 2365 Normally put it into the inner-most non-sk_cleanup scope,
8d08fdba 2366 but if GLOBALIZE is true, put it in the inner-most non-class scope.
e92cc029 2367 The latter is needed for implicit declarations. */
8d08fdba
MS
2368
2369void
11f6b451 2370pushtag (tree name, tree type, int globalize)
8d08fdba 2371{
e2500fed 2372 register struct cp_binding_level *b;
8d08fdba 2373
22ffcc6f 2374 timevar_push (TV_NAME_LOOKUP);
8f032717 2375 b = current_binding_level;
ac20c67a 2376 while (b->kind == sk_cleanup
a7e8c268 2377 || (b->kind == sk_class
fcebb2dd
NS
2378 && (globalize
2379 /* We may be defining a new type in the initializer
2380 of a static member variable. We allow this when
2381 not pedantic, and it is particularly useful for
a1c65f9f 2382 type punning via an anonymous union. */
82a2669e 2383 || COMPLETE_TYPE_P (b->this_entity))))
8d08fdba
MS
2384 b = b->level_chain;
2385
5e0c54e5
GDR
2386 if (b->type_decls == NULL)
2387 b->type_decls = binding_table_new (SCOPE_DEFAULT_HT_SIZE);
2388 binding_table_insert (b->type_decls, name, type);
8d08fdba
MS
2389
2390 if (name)
2391 {
8d08fdba 2392 /* Do C++ gratuitous typedefing. */
db5ae43f 2393 if (IDENTIFIER_TYPE_VALUE (name) != type)
8d08fdba 2394 {
93cdc044 2395 register tree d = NULL_TREE;
848cf1e0 2396 int in_class = 0;
9188c363 2397 tree context = TYPE_CONTEXT (type);
280f9385 2398
280f9385
MM
2399 if (! context)
2400 {
2401 tree cs = current_scope ();
2402
2403 if (! globalize)
2404 context = cs;
2f939d94 2405 else if (cs != NULL_TREE && TYPE_P (cs))
280f9385
MM
2406 /* When declaring a friend class of a local class, we want
2407 to inject the newly named class into the scope
2408 containing the local class, not the namespace scope. */
4f1c5b7d 2409 context = decl_function_context (get_type_decl (cs));
280f9385 2410 }
280f9385
MM
2411 if (!context)
2412 context = current_namespace;
8d08fdba 2413
a7e8c268
MM
2414 if (b->kind == sk_class
2415 || (b->kind == sk_template_parms
2416 && b->level_chain->kind == sk_class))
93cdc044 2417 in_class = 1;
93cdc044 2418
848cf1e0
MM
2419 if (current_lang_name == lang_name_java)
2420 TYPE_FOR_JAVA (type) = 1;
93cdc044 2421
9188c363 2422 d = create_implicit_typedef (name, type);
cb0dbb9a 2423 DECL_CONTEXT (d) = FROB_CONTEXT (context);
9188c363 2424 if (! in_class)
4b0d3cbe 2425 set_identifier_type_value_with_scope (name, d, b);
e1cd6e56 2426
39c01e4c
MM
2427 d = maybe_process_template_type_declaration (type,
2428 globalize, b);
93cdc044 2429
a7e8c268 2430 if (b->kind == sk_class)
61a127b3 2431 {
848cf1e0 2432 if (!PROCESSING_REAL_TEMPLATE_DECL_P ())
61a127b3
MM
2433 /* Put this TYPE_DECL on the TYPE_FIELDS list for the
2434 class. But if it's a member template class, we
2435 want the TEMPLATE_DECL, not the TYPE_DECL, so this
2436 is done later. */
2437 finish_member_declaration (d);
8f032717
MM
2438 else
2439 pushdecl_class_level (d);
61a127b3 2440 }
93cdc044
JM
2441 else
2442 d = pushdecl_with_scope (d, b);
2443
1951a1b6 2444 /* FIXME what if it gets a name from typedef? */
848cf1e0
MM
2445 if (ANON_AGGRNAME_P (name))
2446 DECL_IGNORED_P (d) = 1;
2447
2448 TYPE_CONTEXT (type) = DECL_CONTEXT (d);
1f6e1acc
AS
2449
2450 /* If this is a local class, keep track of it. We need this
2451 information for name-mangling, and so that it is possible to find
2452 all function definitions in a translation unit in a convenient
2453 way. (It's otherwise tricky to find a member function definition
2454 it's only pointed to from within a local class.) */
5362b086 2455 if (TYPE_CONTEXT (type)
089acd57
MM
2456 && TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL
2457 && !processing_template_decl)
1f6e1acc 2458 VARRAY_PUSH_TREE (local_classes, type);
8d08fdba 2459 }
a7e8c268
MM
2460 if (b->kind == sk_class
2461 && !COMPLETE_TYPE_P (current_class_type))
8d08fdba 2462 {
a7e8c268
MM
2463 maybe_add_class_template_decl_list (current_class_type,
2464 type, /*friend_p=*/0);
2465 CLASSTYPE_NESTED_UTDS (current_class_type) = b->type_decls;
8d08fdba
MS
2466 }
2467 }
2468
2469 if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL)
2470 /* Use the canonical TYPE_DECL for this node. */
2471 TYPE_STUB_DECL (type) = TYPE_NAME (type);
2472 else
2473 {
2474 /* Create a fake NULL-named TYPE_DECL node whose TREE_TYPE
2475 will be the tagged type we just added to the current
2476 binding level. This fake NULL-named TYPE_DECL node helps
2477 dwarfout.c to know when it needs to output a
2478 representation of a tagged type, and it also gives us a
2479 convenient place to record the "scope start" address for
2480 the tagged type. */
2481
8d08fdba 2482 tree d = build_decl (TYPE_DECL, NULL_TREE, type);
8d08fdba
MS
2483 TYPE_STUB_DECL (type) = pushdecl_with_scope (d, b);
2484 }
22ffcc6f 2485 timevar_pop (TV_NAME_LOOKUP);
8d08fdba
MS
2486}
2487
2488/* Counter used to create anonymous type names. */
e92cc029 2489
17211ab5 2490static GTY(()) int anon_cnt;
8d08fdba
MS
2491
2492/* Return an IDENTIFIER which can be used as a name for
2493 anonymous structs and unions. */
e92cc029 2494
8d08fdba 2495tree
11f6b451 2496make_anon_name (void)
8d08fdba
MS
2497{
2498 char buf[32];
2499
2500 sprintf (buf, ANON_AGGRNAME_FORMAT, anon_cnt++);
2501 return get_identifier (buf);
2502}
2503
5e0c54e5 2504/* Clear the TREE_PURPOSE slot of UTDs which have anonymous typenames.
8d08fdba 2505 This keeps dbxout from getting confused. */
e92cc029 2506
8d08fdba 2507void
11f6b451 2508clear_anon_tags (void)
8d08fdba 2509{
e2500fed 2510 register struct cp_binding_level *b;
8d08fdba
MS
2511 static int last_cnt = 0;
2512
2513 /* Fast out if no new anon names were declared. */
2514 if (last_cnt == anon_cnt)
2515 return;
2516
2517 b = current_binding_level;
ac20c67a 2518 while (b->kind == sk_cleanup)
8d08fdba 2519 b = b->level_chain;
5e0c54e5
GDR
2520 if (b->type_decls != NULL)
2521 binding_table_remove_anonymous_types (b->type_decls);
8d08fdba
MS
2522 last_cnt = anon_cnt;
2523}
2524\f
2525/* Subroutine of duplicate_decls: return truthvalue of whether
2526 or not types of these decls match.
2527
2528 For C++, we must compare the parameter list so that `int' can match
2529 `int&' in a parameter position, but `int&' is not confused with
2530 `const int&'. */
e92cc029 2531
6060a796 2532int
11f6b451 2533decls_match (tree newdecl, tree olddecl)
8d08fdba
MS
2534{
2535 int types_match;
2536
347d73d7
ML
2537 if (newdecl == olddecl)
2538 return 1;
2539
6b4b3deb
MM
2540 if (TREE_CODE (newdecl) != TREE_CODE (olddecl))
2541 /* If the two DECLs are not even the same kind of thing, we're not
2542 interested in their types. */
2543 return 0;
2544
2545 if (TREE_CODE (newdecl) == FUNCTION_DECL)
8d08fdba
MS
2546 {
2547 tree f1 = TREE_TYPE (newdecl);
2548 tree f2 = TREE_TYPE (olddecl);
2549 tree p1 = TYPE_ARG_TYPES (f1);
2550 tree p2 = TYPE_ARG_TYPES (f2);
2551
4f1c5b7d 2552 if (CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl)
eb68cb58
MM
2553 && ! (DECL_EXTERN_C_P (newdecl)
2554 && DECL_EXTERN_C_P (olddecl)))
c5a6fc45
JM
2555 return 0;
2556
8d08fdba 2557 if (TREE_CODE (f1) != TREE_CODE (f2))
e867a179 2558 return 0;
8d08fdba 2559
3bfdc719 2560 if (same_type_p (TREE_TYPE (f1), TREE_TYPE (f2)))
8926095f 2561 {
8b27e9ef
NS
2562 if (p2 == NULL_TREE && DECL_EXTERN_C_P (olddecl)
2563 && (DECL_BUILT_IN (olddecl)
2564#ifndef NO_IMPLICIT_EXTERN_C
2565 || (DECL_IN_SYSTEM_HEADER (newdecl) && !DECL_CLASS_SCOPE_P (newdecl))
2566 || (DECL_IN_SYSTEM_HEADER (olddecl) && !DECL_CLASS_SCOPE_P (olddecl))
2567#endif
2568 ))
a28e3c7f
MS
2569 {
2570 types_match = self_promoting_args_p (p1);
2571 if (p1 == void_list_node)
2572 TREE_TYPE (newdecl) = TREE_TYPE (olddecl);
2573 }
8b27e9ef
NS
2574#ifndef NO_IMPLICIT_EXTERN_C
2575 else if (p1 == NULL_TREE
2576 && (DECL_EXTERN_C_P (olddecl)
2577 && DECL_IN_SYSTEM_HEADER (olddecl)
2578 && !DECL_CLASS_SCOPE_P (olddecl))
2579 && (DECL_EXTERN_C_P (newdecl)
2580 && DECL_IN_SYSTEM_HEADER (newdecl)
2581 && !DECL_CLASS_SCOPE_P (newdecl)))
a28e3c7f
MS
2582 {
2583 types_match = self_promoting_args_p (p2);
2584 TREE_TYPE (newdecl) = TREE_TYPE (olddecl);
2585 }
8b27e9ef 2586#endif
8926095f 2587 else
91063b51 2588 types_match = compparms (p1, p2);
8926095f 2589 }
8d08fdba
MS
2590 else
2591 types_match = 0;
2592 }
6b4b3deb 2593 else if (TREE_CODE (newdecl) == TEMPLATE_DECL)
51c184be 2594 {
2bb5d995
JM
2595 if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl))
2596 != TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)))
2597 return 0;
2598
ee935db4
MM
2599 if (!comp_template_parms (DECL_TEMPLATE_PARMS (newdecl),
2600 DECL_TEMPLATE_PARMS (olddecl)))
2601 return 0;
2602
f84b4be9 2603 if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL)
ee935db4
MM
2604 types_match = same_type_p (TREE_TYPE (DECL_TEMPLATE_RESULT (olddecl)),
2605 TREE_TYPE (DECL_TEMPLATE_RESULT (newdecl)));
f84b4be9
JM
2606 else
2607 types_match = decls_match (DECL_TEMPLATE_RESULT (olddecl),
2608 DECL_TEMPLATE_RESULT (newdecl));
51c184be 2609 }
8d08fdba
MS
2610 else
2611 {
2612 if (TREE_TYPE (newdecl) == error_mark_node)
2613 types_match = TREE_TYPE (olddecl) == error_mark_node;
2614 else if (TREE_TYPE (olddecl) == NULL_TREE)
2615 types_match = TREE_TYPE (newdecl) == NULL_TREE;
a0a33927
MS
2616 else if (TREE_TYPE (newdecl) == NULL_TREE)
2617 types_match = 0;
8d08fdba 2618 else
01240200 2619 types_match = comptypes (TREE_TYPE (newdecl),
3bfdc719
MM
2620 TREE_TYPE (olddecl),
2621 COMPARE_REDECLARATION);
8d08fdba
MS
2622 }
2623
2624 return types_match;
2625}
2626
2627/* If NEWDECL is `static' and an `extern' was seen previously,
24f30ed4 2628 warn about it. OLDDECL is the previous declaration.
8d08fdba
MS
2629
2630 Note that this does not apply to the C++ case of declaring
2631 a variable `extern const' and then later `const'.
2632
8d08fdba
MS
2633 Don't complain about built-in functions, since they are beyond
2634 the user's control. */
2635
2636static void
11f6b451 2637warn_extern_redeclared_static (tree newdecl, tree olddecl)
8d08fdba 2638{
83182544 2639 static const char *const explicit_extern_static_warning
8251199e 2640 = "`%D' was declared `extern' and later `static'";
83182544 2641 static const char *const implicit_extern_static_warning
8251199e
JM
2642 = "`%D' was declared implicitly `extern' and later `static'";
2643
24f30ed4
MM
2644 tree name;
2645
68642fb6 2646 if (TREE_CODE (newdecl) == TYPE_DECL
655dc6ee 2647 || TREE_CODE (newdecl) == TEMPLATE_DECL
a2a9e21c
GDR
2648 || TREE_CODE (newdecl) == CONST_DECL
2649 || TREE_CODE (newdecl) == NAMESPACE_DECL)
8d08fdba 2650 return;
68642fb6 2651
963d5758
MM
2652 /* Don't get confused by static member functions; that's a different
2653 use of `static'. */
2654 if (TREE_CODE (newdecl) == FUNCTION_DECL
2655 && DECL_STATIC_FUNCTION_P (newdecl))
2656 return;
8d08fdba 2657
24f30ed4
MM
2658 /* If the old declaration was `static', or the new one isn't, then
2659 then everything is OK. */
2660 if (DECL_THIS_STATIC (olddecl) || !DECL_THIS_STATIC (newdecl))
2661 return;
2662
2663 /* It's OK to declare a builtin function as `static'. */
2664 if (TREE_CODE (olddecl) == FUNCTION_DECL
2665 && DECL_ARTIFICIAL (olddecl))
2666 return;
2667
8d08fdba 2668 name = DECL_ASSEMBLER_NAME (newdecl);
33bd39a2 2669 pedwarn (IDENTIFIER_IMPLICIT_DECL (name)
24f30ed4
MM
2670 ? implicit_extern_static_warning
2671 : explicit_extern_static_warning, newdecl);
2672 cp_pedwarn_at ("previous declaration of `%D'", olddecl);
8d08fdba
MS
2673}
2674
2675/* Handle when a new declaration NEWDECL has the same name as an old
2676 one OLDDECL in the same binding contour. Prints an error message
2677 if appropriate.
2678
2679 If safely possible, alter OLDDECL to look like NEWDECL, and return 1.
2680 Otherwise, return 0. */
2681
51c184be 2682int
11f6b451 2683duplicate_decls (tree newdecl, tree olddecl)
8d08fdba 2684{
8d08fdba
MS
2685 unsigned olddecl_uid = DECL_UID (olddecl);
2686 int olddecl_friend = 0, types_match = 0;
0b60dfe3 2687 int new_defines_function = 0;
5566b478
MS
2688
2689 if (newdecl == olddecl)
2690 return 1;
8d08fdba 2691
8926095f 2692 types_match = decls_match (newdecl, olddecl);
8d08fdba 2693
8d08fdba
MS
2694 /* If either the type of the new decl or the type of the old decl is an
2695 error_mark_node, then that implies that we have already issued an
2696 error (earlier) for some bogus type specification, and in that case,
2697 it is rather pointless to harass the user with yet more error message
0b60dfe3 2698 about the same declaration, so just pretend the types match here. */
bd6dd845
MS
2699 if (TREE_TYPE (newdecl) == error_mark_node
2700 || TREE_TYPE (olddecl) == error_mark_node)
8d08fdba 2701 types_match = 1;
68642fb6 2702
97055d5c
AO
2703 if (DECL_P (olddecl)
2704 && TREE_CODE (newdecl) == FUNCTION_DECL
2705 && TREE_CODE (olddecl) == FUNCTION_DECL
2706 && (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl)))
2707 {
2708 if (DECL_DECLARED_INLINE_P (newdecl)
2709 && DECL_UNINLINABLE (newdecl)
2710 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
2711 /* Already warned elsewhere. */;
2712 else if (DECL_DECLARED_INLINE_P (olddecl)
2713 && DECL_UNINLINABLE (olddecl)
2714 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
2715 /* Already warned. */;
2716 else if (DECL_DECLARED_INLINE_P (newdecl)
2717 && DECL_UNINLINABLE (olddecl)
2718 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
2719 {
ddd2d57e
RH
2720 warning ("%Jfunction '%D' redeclared as inline", newdecl, newdecl);
2721 warning ("%Jprevious declaration of '%D' with attribute noinline",
2722 olddecl, olddecl);
97055d5c
AO
2723 }
2724 else if (DECL_DECLARED_INLINE_P (olddecl)
2725 && DECL_UNINLINABLE (newdecl)
2726 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
2727 {
ddd2d57e
RH
2728 warning ("%Jfunction '%D' redeclared with attribute noinline",
2729 newdecl, newdecl);
2730 warning ("%Jprevious declaration of '%D' was inline",
2731 olddecl, olddecl);
97055d5c
AO
2732 }
2733 }
2734
a1c65f9f 2735 /* Check for redeclaration and other discrepancies. */
d22c8596 2736 if (TREE_CODE (olddecl) == FUNCTION_DECL
9f33663b
JM
2737 && DECL_ARTIFICIAL (olddecl))
2738 {
2739 if (TREE_CODE (newdecl) != FUNCTION_DECL)
2740 {
d52e4867
RS
2741 /* Avoid warnings redeclaring anticipated built-ins. */
2742 if (DECL_ANTICIPATED (olddecl))
2743 return 0;
2744
9f33663b
JM
2745 /* If you declare a built-in or predefined function name as static,
2746 the old definition is overridden, but optionally warn this was a
2747 bad choice of name. */
2748 if (! TREE_PUBLIC (newdecl))
2749 {
2750 if (warn_shadow)
33bd39a2 2751 warning ("shadowing %s function `%#D'",
9f33663b
JM
2752 DECL_BUILT_IN (olddecl) ? "built-in" : "library",
2753 olddecl);
2754 /* Discard the old built-in function. */
2755 return 0;
2756 }
2757 /* If the built-in is not ansi, then programs can override
2758 it even globally without an error. */
2759 else if (! DECL_BUILT_IN (olddecl))
33bd39a2 2760 warning ("library function `%#D' redeclared as non-function `%#D'",
9f33663b
JM
2761 olddecl, newdecl);
2762 else
2763 {
33bd39a2
ZW
2764 error ("declaration of `%#D'", newdecl);
2765 error ("conflicts with built-in declaration `%#D'",
a4443a08 2766 olddecl);
9f33663b 2767 }
a4443a08
MS
2768 return 0;
2769 }
9f33663b 2770 else if (!types_match)
8d08fdba 2771 {
d52e4867
RS
2772 /* Avoid warnings redeclaring anticipated built-ins. */
2773 if (DECL_ANTICIPATED (olddecl))
2774 ; /* Do nothing yet. */
2775 else if ((DECL_EXTERN_C_P (newdecl)
7e99327d
MM
2776 && DECL_EXTERN_C_P (olddecl))
2777 || compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
2778 TYPE_ARG_TYPES (TREE_TYPE (olddecl))))
a4443a08 2779 {
9f33663b
JM
2780 /* A near match; override the builtin. */
2781
2782 if (TREE_PUBLIC (newdecl))
a4443a08 2783 {
33bd39a2
ZW
2784 warning ("new declaration `%#D'", newdecl);
2785 warning ("ambiguates built-in declaration `%#D'",
9f33663b 2786 olddecl);
a4443a08 2787 }
9f33663b 2788 else if (warn_shadow)
33bd39a2 2789 warning ("shadowing %s function `%#D'",
9f33663b
JM
2790 DECL_BUILT_IN (olddecl) ? "built-in" : "library",
2791 olddecl);
a4443a08 2792 }
9f33663b
JM
2793 else
2794 /* Discard the old built-in function. */
2795 return 0;
04a9d080
RS
2796
2797 /* Replace the old RTL to avoid problems with inlining. */
2798 SET_DECL_RTL (olddecl, DECL_RTL (newdecl));
8d08fdba 2799 }
10326855 2800 /* Even if the types match, prefer the new declarations type
7df87825 2801 for anticipated built-ins, for exception lists, etc... */
10326855
RS
2802 else if (DECL_ANTICIPATED (olddecl))
2803 TREE_TYPE (olddecl) = TREE_TYPE (newdecl);
68642fb6 2804
7e99327d
MM
2805 /* Whether or not the builtin can throw exceptions has no
2806 bearing on this declarator. */
2807 TREE_NOTHROW (olddecl) = 0;
2808
24f30ed4
MM
2809 if (DECL_THIS_STATIC (newdecl) && !DECL_THIS_STATIC (olddecl))
2810 {
2811 /* If a builtin function is redeclared as `static', merge
2812 the declarations, but make the original one static. */
2813 DECL_THIS_STATIC (olddecl) = 1;
2814 TREE_PUBLIC (olddecl) = 0;
9bfadf57 2815
421844e7
MM
2816 /* Make the old declaration consistent with the new one so
2817 that all remnants of the builtin-ness of this function
2818 will be banished. */
5d2ed28c 2819 SET_DECL_LANGUAGE (olddecl, DECL_LANGUAGE (newdecl));
19e7881c 2820 SET_DECL_RTL (olddecl, DECL_RTL (newdecl));
24f30ed4 2821 }
39211cd5
MS
2822 }
2823 else if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
2824 {
9ed182dc
JM
2825 if ((TREE_CODE (olddecl) == TYPE_DECL && DECL_ARTIFICIAL (olddecl)
2826 && TREE_CODE (newdecl) != TYPE_DECL
2827 && ! (TREE_CODE (newdecl) == TEMPLATE_DECL
2828 && TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL))
2829 || (TREE_CODE (newdecl) == TYPE_DECL && DECL_ARTIFICIAL (newdecl)
2830 && TREE_CODE (olddecl) != TYPE_DECL
2831 && ! (TREE_CODE (olddecl) == TEMPLATE_DECL
2832 && (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl))
2833 == TYPE_DECL))))
2834 {
2835 /* We do nothing special here, because C++ does such nasty
2836 things with TYPE_DECLs. Instead, just let the TYPE_DECL
2837 get shadowed, and know that if we need to find a TYPE_DECL
2838 for a given name, we can look in the IDENTIFIER_TYPE_VALUE
2839 slot of the identifier. */
2840 return 0;
2841 }
2842
39211cd5 2843 if ((TREE_CODE (newdecl) == FUNCTION_DECL
5566b478 2844 && DECL_FUNCTION_TEMPLATE_P (olddecl))
39211cd5 2845 || (TREE_CODE (olddecl) == FUNCTION_DECL
5566b478 2846 && DECL_FUNCTION_TEMPLATE_P (newdecl)))
39211cd5 2847 return 0;
9ed182dc 2848
33bd39a2 2849 error ("`%#D' redeclared as different kind of symbol", newdecl);
39211cd5
MS
2850 if (TREE_CODE (olddecl) == TREE_LIST)
2851 olddecl = TREE_VALUE (olddecl);
8251199e 2852 cp_error_at ("previous declaration of `%#D'", olddecl);
39211cd5
MS
2853
2854 /* New decl is completely inconsistent with the old one =>
2855 tell caller to replace the old one. */
2856
2857 return 0;
8d08fdba 2858 }
8d08fdba
MS
2859 else if (!types_match)
2860 {
4f1c5b7d 2861 if (CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl))
7bdbfa05
MM
2862 /* These are certainly not duplicate declarations; they're
2863 from different scopes. */
2864 return 0;
2865
8926095f 2866 if (TREE_CODE (newdecl) == TEMPLATE_DECL)
f0e01782
MS
2867 {
2868 /* The name of a class template may not be declared to refer to
2869 any other template, class, function, object, namespace, value,
e92cc029 2870 or type in the same scope. */
5566b478
MS
2871 if (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) == TYPE_DECL
2872 || TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL)
f0e01782 2873 {
33bd39a2 2874 error ("declaration of template `%#D'", newdecl);
8251199e 2875 cp_error_at ("conflicts with previous declaration `%#D'",
f0e01782
MS
2876 olddecl);
2877 }
ec255269
MS
2878 else if (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) == FUNCTION_DECL
2879 && TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == FUNCTION_DECL
2880 && compparms (TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (olddecl))),
91063b51 2881 TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (newdecl))))
75650646 2882 && comp_template_parms (DECL_TEMPLATE_PARMS (newdecl),
b9201622
NS
2883 DECL_TEMPLATE_PARMS (olddecl))
2884 /* Template functions can be disambiguated by
2885 return type. */
2886 && same_type_p (TREE_TYPE (TREE_TYPE (newdecl)),
2887 TREE_TYPE (TREE_TYPE (olddecl))))
ec255269 2888 {
33bd39a2 2889 error ("new declaration `%#D'", newdecl);
8251199e 2890 cp_error_at ("ambiguates old declaration `%#D'", olddecl);
ec255269 2891 }
f0e01782
MS
2892 return 0;
2893 }
8926095f
MS
2894 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2895 {
eb68cb58 2896 if (DECL_EXTERN_C_P (newdecl) && DECL_EXTERN_C_P (olddecl))
8926095f 2897 {
33bd39a2 2898 error ("declaration of C function `%#D' conflicts with",
8926095f 2899 newdecl);
8251199e 2900 cp_error_at ("previous declaration `%#D' here", olddecl);
8926095f 2901 }
00595019 2902 else if (compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
91063b51 2903 TYPE_ARG_TYPES (TREE_TYPE (olddecl))))
700f8a87 2904 {
33bd39a2 2905 error ("new declaration `%#D'", newdecl);
8251199e 2906 cp_error_at ("ambiguates old declaration `%#D'", olddecl);
700f8a87
MS
2907 }
2908 else
2909 return 0;
8926095f 2910 }
8d08fdba
MS
2911
2912 /* Already complained about this, so don't do so again. */
a4443a08 2913 else if (current_class_type == NULL_TREE
8d08fdba
MS
2914 || IDENTIFIER_ERROR_LOCUS (DECL_ASSEMBLER_NAME (newdecl)) != current_class_type)
2915 {
a2a9e21c
GDR
2916 error ("conflicting declaration '%#D'", newdecl);
2917 cp_error_at ("'%D' has a previous declaration as `%#D'",
2918 olddecl, olddecl);
2919 return false;
8d08fdba
MS
2920 }
2921 }
68642fb6 2922 else if (TREE_CODE (newdecl) == FUNCTION_DECL
75650646
MM
2923 && ((DECL_TEMPLATE_SPECIALIZATION (olddecl)
2924 && (!DECL_TEMPLATE_INFO (newdecl)
68642fb6 2925 || (DECL_TI_TEMPLATE (newdecl)
75650646
MM
2926 != DECL_TI_TEMPLATE (olddecl))))
2927 || (DECL_TEMPLATE_SPECIALIZATION (newdecl)
2928 && (!DECL_TEMPLATE_INFO (olddecl)
68642fb6 2929 || (DECL_TI_TEMPLATE (olddecl)
75650646 2930 != DECL_TI_TEMPLATE (newdecl))))))
386b8a85
JM
2931 /* It's OK to have a template specialization and a non-template
2932 with the same type, or to have specializations of two
75650646
MM
2933 different templates with the same type. Note that if one is a
2934 specialization, and the other is an instantiation of the same
2935 template, that we do not exit at this point. That situation
2936 can occur if we instantiate a template class, and then
0e339752 2937 specialize one of its methods. This situation is valid, but
75650646
MM
2938 the declarations must be merged in the usual way. */
2939 return 0;
68642fb6
UD
2940 else if (TREE_CODE (newdecl) == FUNCTION_DECL
2941 && ((DECL_TEMPLATE_INSTANTIATION (olddecl)
75650646
MM
2942 && !DECL_USE_TEMPLATE (newdecl))
2943 || (DECL_TEMPLATE_INSTANTIATION (newdecl)
2944 && !DECL_USE_TEMPLATE (olddecl))))
2945 /* One of the declarations is a template instantiation, and the
2946 other is not a template at all. That's OK. */
386b8a85 2947 return 0;
85c6cbaf
ML
2948 else if (TREE_CODE (newdecl) == NAMESPACE_DECL
2949 && DECL_NAMESPACE_ALIAS (newdecl)
2950 && DECL_NAMESPACE_ALIAS (newdecl) == DECL_NAMESPACE_ALIAS (olddecl))
a1c65f9f 2951 /* Redeclaration of namespace alias, ignore it. */
85c6cbaf 2952 return 1;
8d08fdba
MS
2953 else
2954 {
d8e178a0 2955 const char *errmsg = redeclaration_error_message (newdecl, olddecl);
8251199e 2956 if (errmsg)
8d08fdba 2957 {
33bd39a2 2958 error (errmsg, newdecl);
8d08fdba
MS
2959 if (DECL_NAME (olddecl) != NULL_TREE)
2960 cp_error_at ((DECL_INITIAL (olddecl)
2c73f9f5 2961 && namespace_bindings_p ())
8251199e
JM
2962 ? "`%#D' previously defined here"
2963 : "`%#D' previously declared here", olddecl);
00fae85d 2964 return 0;
8d08fdba
MS
2965 }
2966 else if (TREE_CODE (olddecl) == FUNCTION_DECL
2967 && DECL_INITIAL (olddecl) != NULL_TREE
2968 && TYPE_ARG_TYPES (TREE_TYPE (olddecl)) == NULL_TREE
2969 && TYPE_ARG_TYPES (TREE_TYPE (newdecl)) != NULL_TREE)
2970 {
2971 /* Prototype decl follows defn w/o prototype. */
8251199e 2972 cp_warning_at ("prototype for `%#D'", newdecl);
ddd2d57e 2973 warning ("%Jfollows non-prototype definition here", olddecl);
8d08fdba
MS
2974 }
2975 else if (TREE_CODE (olddecl) == FUNCTION_DECL
2976 && DECL_LANGUAGE (newdecl) != DECL_LANGUAGE (olddecl))
8926095f
MS
2977 {
2978 /* extern "C" int foo ();
2979 int foo () { bar (); }
2980 is OK. */
46ccf50a 2981 if (current_lang_depth () == 0)
5d2ed28c 2982 SET_DECL_LANGUAGE (newdecl, DECL_LANGUAGE (olddecl));
8926095f
MS
2983 else
2984 {
8251199e 2985 cp_error_at ("previous declaration of `%#D' with %L linkage",
8926095f 2986 olddecl, DECL_LANGUAGE (olddecl));
33bd39a2 2987 error ("conflicts with new declaration with %L linkage",
8926095f
MS
2988 DECL_LANGUAGE (newdecl));
2989 }
2990 }
e1cd6e56 2991
a6f02587 2992 if (DECL_LANG_SPECIFIC (olddecl) && DECL_USE_TEMPLATE (olddecl))
c32381b1
JM
2993 ;
2994 else if (TREE_CODE (olddecl) == FUNCTION_DECL)
e1cd6e56
MS
2995 {
2996 tree t1 = TYPE_ARG_TYPES (TREE_TYPE (olddecl));
2997 tree t2 = TYPE_ARG_TYPES (TREE_TYPE (newdecl));
2998 int i = 1;
2999
3000 if (TREE_CODE (TREE_TYPE (newdecl)) == METHOD_TYPE)
3001 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2);
68642fb6 3002
e1cd6e56
MS
3003 for (; t1 && t1 != void_list_node;
3004 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2), i++)
3005 if (TREE_PURPOSE (t1) && TREE_PURPOSE (t2))
3006 {
2507f3b5
RK
3007 if (1 == simple_cst_equal (TREE_PURPOSE (t1),
3008 TREE_PURPOSE (t2)))
e1cd6e56 3009 {
624a5800
CR
3010 pedwarn ("default argument given for parameter %d of `%#D'",
3011 i, newdecl);
3012 cp_pedwarn_at ("after previous specification in `%#D'",
3013 olddecl);
e1cd6e56
MS
3014 }
3015 else
3016 {
33bd39a2 3017 error ("default argument given for parameter %d of `%#D'",
e1cd6e56 3018 i, newdecl);
8251199e 3019 cp_error_at ("after previous specification in `%#D'",
e1cd6e56
MS
3020 olddecl);
3021 }
3022 }
a5894242 3023
79065db2
MM
3024 if (DECL_DECLARED_INLINE_P (newdecl)
3025 && ! DECL_DECLARED_INLINE_P (olddecl)
7fcdf4c2 3026 && TREE_ADDRESSABLE (olddecl) && warn_inline)
dff6b454 3027 {
ddd2d57e
RH
3028 warning ("`%#D' was used before it was declared inline", newdecl);
3029 warning ("%Jprevious non-inline declaration here", olddecl);
dff6b454 3030 }
e1cd6e56 3031 }
8d08fdba
MS
3032 }
3033
4782bd5b
RS
3034 /* Do not merge an implicit typedef with an explicit one. In:
3035
3036 class A;
3037 ...
3038 typedef class A A __attribute__ ((foo));
3039
3040 the attribute should apply only to the typedef. */
3041 if (TREE_CODE (olddecl) == TYPE_DECL
3042 && (DECL_IMPLICIT_TYPEDEF_P (olddecl)
3043 || DECL_IMPLICIT_TYPEDEF_P (newdecl)))
3044 return 0;
3045
8d08fdba
MS
3046 /* If new decl is `static' and an `extern' was seen previously,
3047 warn about it. */
3048 warn_extern_redeclared_static (newdecl, olddecl);
3049
e92cc029 3050 /* We have committed to returning 1 at this point. */
8d08fdba
MS
3051 if (TREE_CODE (newdecl) == FUNCTION_DECL)
3052 {
3053 /* Now that functions must hold information normally held
3054 by field decls, there is extra work to do so that
3055 declaration information does not get destroyed during
3056 definition. */
3057 if (DECL_VINDEX (olddecl))
3058 DECL_VINDEX (newdecl) = DECL_VINDEX (olddecl);
3059 if (DECL_CONTEXT (olddecl))
3060 DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
e1cd6e56
MS
3061 DECL_STATIC_CONSTRUCTOR (newdecl) |= DECL_STATIC_CONSTRUCTOR (olddecl);
3062 DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
fee7654e 3063 DECL_PURE_VIRTUAL_P (newdecl) |= DECL_PURE_VIRTUAL_P (olddecl);
864b83b9 3064 DECL_VIRTUAL_P (newdecl) |= DECL_VIRTUAL_P (olddecl);
4a67c9e9 3065 DECL_NEEDS_FINAL_OVERRIDER_P (newdecl) |= DECL_NEEDS_FINAL_OVERRIDER_P (olddecl);
963d5758 3066 DECL_THIS_STATIC (newdecl) |= DECL_THIS_STATIC (olddecl);
e0fff4b3
JM
3067 if (DECL_OVERLOADED_OPERATOR_P (olddecl) != ERROR_MARK)
3068 SET_OVERLOADED_OPERATOR_CODE
3069 (newdecl, DECL_OVERLOADED_OPERATOR_P (olddecl));
0b60dfe3 3070 new_defines_function = DECL_INITIAL (newdecl) != NULL_TREE;
68642fb6 3071
0b60dfe3
BK
3072 /* Optionally warn about more than one declaration for the same
3073 name, but don't warn about a function declaration followed by a
3074 definition. */
3075 if (warn_redundant_decls && ! DECL_ARTIFICIAL (olddecl)
3076 && !(new_defines_function && DECL_INITIAL (olddecl) == NULL_TREE)
a1c65f9f 3077 /* Don't warn about extern decl followed by definition. */
0b60dfe3 3078 && !(DECL_EXTERNAL (olddecl) && ! DECL_EXTERNAL (newdecl))
a1c65f9f 3079 /* Don't warn about friends, let add_friend take care of it. */
3581fae0 3080 && ! (DECL_FRIEND_P (newdecl) || DECL_FRIEND_P (olddecl)))
0b60dfe3 3081 {
33bd39a2 3082 warning ("redundant redeclaration of `%D' in same scope", newdecl);
8251199e 3083 cp_warning_at ("previous declaration of `%D'", olddecl);
0b60dfe3 3084 }
8d08fdba
MS
3085 }
3086
3087 /* Deal with C++: must preserve virtual function table size. */
3088 if (TREE_CODE (olddecl) == TYPE_DECL)
3089 {
3090 register tree newtype = TREE_TYPE (newdecl);
3091 register tree oldtype = TREE_TYPE (olddecl);
3092
3093 if (newtype != error_mark_node && oldtype != error_mark_node
3094 && TYPE_LANG_SPECIFIC (newtype) && TYPE_LANG_SPECIFIC (oldtype))
e93ee644
MM
3095 CLASSTYPE_FRIEND_CLASSES (newtype)
3096 = CLASSTYPE_FRIEND_CLASSES (oldtype);
7df87825 3097
970d6386 3098 DECL_ORIGINAL_TYPE (newdecl) = DECL_ORIGINAL_TYPE (olddecl);
8d08fdba
MS
3099 }
3100
8d08fdba
MS
3101 /* Copy all the DECL_... slots specified in the new decl
3102 except for any that we copy here from the old type. */
91d231cb 3103 DECL_ATTRIBUTES (newdecl)
f6897b10 3104 = (*targetm.merge_decl_attributes) (olddecl, newdecl);
8d08fdba 3105
5566b478
MS
3106 if (TREE_CODE (newdecl) == TEMPLATE_DECL)
3107 {
4d85e00e 3108 TREE_TYPE (olddecl) = TREE_TYPE (DECL_TEMPLATE_RESULT (olddecl));
68642fb6 3109 DECL_TEMPLATE_SPECIALIZATIONS (olddecl)
4d85e00e
MM
3110 = chainon (DECL_TEMPLATE_SPECIALIZATIONS (olddecl),
3111 DECL_TEMPLATE_SPECIALIZATIONS (newdecl));
68642fb6 3112
daa8df65
MM
3113 /* If the new declaration is a definition, update the file and
3114 line information on the declaration. */
06d5e633
NS
3115 if (DECL_INITIAL (DECL_TEMPLATE_RESULT (olddecl)) == NULL_TREE
3116 && DECL_INITIAL (DECL_TEMPLATE_RESULT (newdecl)) != NULL_TREE)
daa8df65 3117 {
f31686a3
RH
3118 DECL_SOURCE_LOCATION (olddecl)
3119 = DECL_SOURCE_LOCATION (DECL_TEMPLATE_RESULT (olddecl))
3120 = DECL_SOURCE_LOCATION (newdecl);
daa8df65
MM
3121 }
3122
5566b478
MS
3123 return 1;
3124 }
68642fb6 3125
8d08fdba
MS
3126 if (types_match)
3127 {
3128 /* Automatically handles default parameters. */
3129 tree oldtype = TREE_TYPE (olddecl);
e1cd6e56 3130 tree newtype;
8d08fdba 3131
e1cd6e56 3132 /* Merge the data types specified in the two decls. */
6da794e8 3133 newtype = merge_types (TREE_TYPE (newdecl), TREE_TYPE (olddecl));
e1cd6e56 3134
6da794e8 3135 /* If merge_types produces a non-typedef type, just use the old type. */
9076e292
JM
3136 if (TREE_CODE (newdecl) == TYPE_DECL
3137 && newtype == DECL_ORIGINAL_TYPE (newdecl))
3138 newtype = oldtype;
3139
8d08fdba 3140 if (TREE_CODE (newdecl) == VAR_DECL)
17bbb839
MM
3141 {
3142 DECL_THIS_EXTERN (newdecl) |= DECL_THIS_EXTERN (olddecl);
3143 DECL_INITIALIZED_P (newdecl) |= DECL_INITIALIZED_P (olddecl);
39703eb9
MM
3144 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (newdecl)
3145 |= DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (olddecl);
17bbb839
MM
3146 }
3147
6da794e8 3148 /* Do this after calling `merge_types' so that default
8d08fdba
MS
3149 parameters don't confuse us. */
3150 else if (TREE_CODE (newdecl) == FUNCTION_DECL
3151 && (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (newdecl))
3152 != TYPE_RAISES_EXCEPTIONS (TREE_TYPE (olddecl))))
3153 {
f30432d7 3154 TREE_TYPE (newdecl) = build_exception_variant (newtype,
8d08fdba 3155 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (newdecl)));
f30432d7 3156 TREE_TYPE (olddecl) = build_exception_variant (newtype,
8d08fdba
MS
3157 TYPE_RAISES_EXCEPTIONS (oldtype));
3158
9a224b4a 3159 if ((pedantic || ! DECL_IN_SYSTEM_HEADER (olddecl))
f31686a3 3160 && DECL_SOURCE_LINE (olddecl) != 0
da20811c 3161 && flag_exceptions
4cc1d462
NS
3162 && !comp_except_specs (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (newdecl)),
3163 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (olddecl)), 1))
8d08fdba 3164 {
33bd39a2 3165 error ("declaration of `%F' throws different exceptions",
f31686a3 3166 newdecl);
a09ba2e0 3167 cp_error_at ("than previous declaration `%F'", olddecl);
8d08fdba
MS
3168 }
3169 }
3170 TREE_TYPE (newdecl) = TREE_TYPE (olddecl) = newtype;
3171
3172 /* Lay the type out, unless already done. */
407f03b8 3173 if (! same_type_p (newtype, oldtype)
5566b478 3174 && TREE_TYPE (newdecl) != error_mark_node
5156628f 3175 && !(processing_template_decl && uses_template_parms (newdecl)))
b7484fbe
MS
3176 layout_type (TREE_TYPE (newdecl));
3177
5566b478
MS
3178 if ((TREE_CODE (newdecl) == VAR_DECL
3179 || TREE_CODE (newdecl) == PARM_DECL
3180 || TREE_CODE (newdecl) == RESULT_DECL
3181 || TREE_CODE (newdecl) == FIELD_DECL
3182 || TREE_CODE (newdecl) == TYPE_DECL)
5156628f 3183 && !(processing_template_decl && uses_template_parms (newdecl)))
b7484fbe 3184 layout_decl (newdecl, 0);
8d08fdba
MS
3185
3186 /* Merge the type qualifiers. */
3187 if (TREE_READONLY (newdecl))
3188 TREE_READONLY (olddecl) = 1;
3189 if (TREE_THIS_VOLATILE (newdecl))
3190 TREE_THIS_VOLATILE (olddecl) = 1;
3191
3192 /* Merge the initialization information. */
8926095f
MS
3193 if (DECL_INITIAL (newdecl) == NULL_TREE
3194 && DECL_INITIAL (olddecl) != NULL_TREE)
3195 {
3196 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
f31686a3 3197 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
b0d06515
MM
3198 if (CAN_HAVE_FULL_LANG_DECL_P (newdecl)
3199 && DECL_LANG_SPECIFIC (newdecl)
4d85e00e 3200 && DECL_LANG_SPECIFIC (olddecl))
69ee8086
RH
3201 {
3202 DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
3203 DECL_SAVED_INSNS (newdecl) = DECL_SAVED_INSNS (olddecl);
3204 }
8926095f 3205 }
39211cd5
MS
3206
3207 /* Merge the section attribute.
3208 We want to issue an error if the sections conflict but that must be
3209 done later in decl_attributes since we are called before attributes
3210 are assigned. */
3211 if (DECL_SECTION_NAME (newdecl) == NULL_TREE)
3212 DECL_SECTION_NAME (newdecl) = DECL_SECTION_NAME (olddecl);
3213
a157febd
GK
3214 if (TREE_CODE (newdecl) == FUNCTION_DECL)
3215 {
3216 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
3217 |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
1331d16f 3218 DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
220a35cc
RS
3219 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
3220 TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
3221 TREE_NOTHROW (newdecl) |= TREE_NOTHROW (olddecl);
1331d16f
RS
3222 DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
3223 DECL_IS_PURE (newdecl) |= DECL_IS_PURE (olddecl);
c740732f
MM
3224 /* Keep the old RTL. */
3225 COPY_DECL_RTL (olddecl, newdecl);
3226 }
3227 else if (TREE_CODE (newdecl) == VAR_DECL
3228 && (DECL_SIZE (olddecl) || !DECL_SIZE (newdecl)))
3229 {
3230 /* Keep the old RTL. We cannot keep the old RTL if the old
3231 declaration was for an incomplete object and the new
3232 declaration is not since many attributes of the RTL will
3233 change. */
3234 COPY_DECL_RTL (olddecl, newdecl);
a157febd 3235 }
8d08fdba
MS
3236 }
3237 /* If cannot merge, then use the new type and qualifiers,
3238 and don't preserve the old rtl. */
3239 else
3240 {
3241 /* Clean out any memory we had of the old declaration. */
3242 tree oldstatic = value_member (olddecl, static_aggregates);
3243 if (oldstatic)
3244 TREE_VALUE (oldstatic) = error_mark_node;
3245
3246 TREE_TYPE (olddecl) = TREE_TYPE (newdecl);
3247 TREE_READONLY (olddecl) = TREE_READONLY (newdecl);
3248 TREE_THIS_VOLATILE (olddecl) = TREE_THIS_VOLATILE (newdecl);
3249 TREE_SIDE_EFFECTS (olddecl) = TREE_SIDE_EFFECTS (newdecl);
3250 }
3251
3252 /* Merge the storage class information. */
45806a3f
FS
3253 merge_weak (newdecl, olddecl);
3254
e92cc029 3255 DECL_ONE_ONLY (newdecl) |= DECL_ONE_ONLY (olddecl);
794d4a61 3256 DECL_DEFER_OUTPUT (newdecl) |= DECL_DEFER_OUTPUT (olddecl);
a9aedbc2
MS
3257 TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
3258 TREE_STATIC (olddecl) = TREE_STATIC (newdecl) |= TREE_STATIC (olddecl);
3259 if (! DECL_EXTERNAL (olddecl))
3260 DECL_EXTERNAL (newdecl) = 0;
68642fb6 3261
0b60dfe3 3262 if (DECL_LANG_SPECIFIC (newdecl) && DECL_LANG_SPECIFIC (olddecl))
8d08fdba 3263 {
a9aedbc2
MS
3264 DECL_INTERFACE_KNOWN (newdecl) |= DECL_INTERFACE_KNOWN (olddecl);
3265 DECL_NOT_REALLY_EXTERN (newdecl) |= DECL_NOT_REALLY_EXTERN (olddecl);
77be6f82 3266 DECL_COMDAT (newdecl) |= DECL_COMDAT (olddecl);
68642fb6 3267 DECL_TEMPLATE_INSTANTIATED (newdecl)
2b0a63a3 3268 |= DECL_TEMPLATE_INSTANTIATED (olddecl);
0b60dfe3
BK
3269 /* Don't really know how much of the language-specific
3270 values we should copy from old to new. */
3271 DECL_IN_AGGR_P (newdecl) = DECL_IN_AGGR_P (olddecl);
e2500fed
GK
3272 DECL_LANG_SPECIFIC (newdecl)->decl_flags.u2 =
3273 DECL_LANG_SPECIFIC (olddecl)->decl_flags.u2;
0b60dfe3 3274 DECL_NONCONVERTING_P (newdecl) = DECL_NONCONVERTING_P (olddecl);
fbf1c34b 3275 DECL_TEMPLATE_INFO (newdecl) = DECL_TEMPLATE_INFO (olddecl);
3febd123
NS
3276 DECL_INITIALIZED_IN_CLASS_P (newdecl)
3277 |= DECL_INITIALIZED_IN_CLASS_P (olddecl);
0b60dfe3 3278 olddecl_friend = DECL_FRIEND_P (olddecl);
6a629cac
MM
3279
3280 /* Only functions have DECL_BEFRIENDING_CLASSES. */
3281 if (TREE_CODE (newdecl) == FUNCTION_DECL
3282 || DECL_FUNCTION_TEMPLATE_P (newdecl))
bb5e8a7f
MM
3283 {
3284 DECL_BEFRIENDING_CLASSES (newdecl)
3285 = chainon (DECL_BEFRIENDING_CLASSES (newdecl),
3286 DECL_BEFRIENDING_CLASSES (olddecl));
ca9e1382
KL
3287 /* DECL_THUNKS is only valid for virtual functions,
3288 otherwise it is a DECL_FRIEND_CONTEXT. */
3289 if (DECL_VIRTUAL_P (newdecl))
3290 DECL_THUNKS (newdecl) = DECL_THUNKS (olddecl);
bb5e8a7f 3291 }
8d08fdba
MS
3292 }
3293
8d08fdba
MS
3294 if (TREE_CODE (newdecl) == FUNCTION_DECL)
3295 {
68642fb6
UD
3296 if (DECL_TEMPLATE_INSTANTIATION (olddecl)
3297 && !DECL_TEMPLATE_INSTANTIATION (newdecl))
75650646
MM
3298 {
3299 /* If newdecl is not a specialization, then it is not a
3300 template-related function at all. And that means that we
bf7c02dd 3301 should have exited above, returning 0. */
75650646
MM
3302 my_friendly_assert (DECL_TEMPLATE_SPECIALIZATION (newdecl),
3303 0);
3304
68642fb6 3305 if (TREE_USED (olddecl))
75650646 3306 /* From [temp.expl.spec]:
68642fb6 3307
75650646
MM
3308 If a template, a member template or the member of a class
3309 template is explicitly specialized then that
3310 specialization shall be declared before the first use of
3311 that specialization that would cause an implicit
3312 instantiation to take place, in every translation unit in
3313 which such a use occurs. */
33bd39a2 3314 error ("explicit specialization of %D after first use",
75650646
MM
3315 olddecl);
3316
3317 SET_DECL_TEMPLATE_SPECIALIZATION (olddecl);
d03d18e8
KL
3318
3319 /* [temp.expl.spec/14] We don't inline explicit specialization
3320 just because the primary template says so. */
75650646 3321 }
d03d18e8
KL
3322 else
3323 {
3324 if (DECL_PENDING_INLINE_INFO (newdecl) == 0)
3325 DECL_PENDING_INLINE_INFO (newdecl) = DECL_PENDING_INLINE_INFO (olddecl);
faae18ab 3326
d03d18e8 3327 DECL_DECLARED_INLINE_P (newdecl) |= DECL_DECLARED_INLINE_P (olddecl);
faae18ab 3328
d03d18e8
KL
3329 /* If either decl says `inline', this fn is inline, unless
3330 its definition was passed already. */
3331 if (DECL_INLINE (newdecl) && DECL_INITIAL (olddecl) == NULL_TREE)
3332 DECL_INLINE (olddecl) = 1;
3333 DECL_INLINE (newdecl) = DECL_INLINE (olddecl);
3334
3335 DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
3336 = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
3337 }
97055d5c 3338
5daf7c0a
JM
3339 /* Preserve abstractness on cloned [cd]tors. */
3340 DECL_ABSTRACT (newdecl) = DECL_ABSTRACT (olddecl);
3341
700f8a87
MS
3342 if (! types_match)
3343 {
5d2ed28c 3344 SET_DECL_LANGUAGE (olddecl, DECL_LANGUAGE (newdecl));
92643fea 3345 COPY_DECL_ASSEMBLER_NAME (newdecl, olddecl);
19e7881c 3346 SET_DECL_RTL (olddecl, DECL_RTL (newdecl));
5566b478
MS
3347 }
3348 if (! types_match || new_defines_function)
3349 {
6f1b4c42
JM
3350 /* These need to be copied so that the names are available.
3351 Note that if the types do match, we'll preserve inline
3352 info and other bits, but if not, we won't. */
700f8a87
MS
3353 DECL_ARGUMENTS (olddecl) = DECL_ARGUMENTS (newdecl);
3354 DECL_RESULT (olddecl) = DECL_RESULT (newdecl);
700f8a87 3355 }
8d08fdba
MS
3356 if (new_defines_function)
3357 /* If defining a function declared with other language
3358 linkage, use the previously declared language linkage. */
5d2ed28c 3359 SET_DECL_LANGUAGE (newdecl, DECL_LANGUAGE (olddecl));
6f1b4c42 3360 else if (types_match)
8d08fdba
MS
3361 {
3362 /* If redeclaring a builtin function, and not a definition,
3363 it stays built in. */
3364 if (DECL_BUILT_IN (olddecl))
3365 {
26db82d8 3366 DECL_BUILT_IN_CLASS (newdecl) = DECL_BUILT_IN_CLASS (olddecl);
39211cd5 3367 DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl);
8d08fdba
MS
3368 /* If we're keeping the built-in definition, keep the rtl,
3369 regardless of declaration matches. */
19e7881c 3370 SET_DECL_RTL (newdecl, DECL_RTL (olddecl));
8d08fdba
MS
3371 }
3372 else
084c1779 3373 DECL_ESTIMATED_INSNS (newdecl) = DECL_ESTIMATED_INSNS (olddecl);
8d08fdba
MS
3374
3375 DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
8d08fdba
MS
3376 /* Don't clear out the arguments if we're redefining a function. */
3377 if (DECL_ARGUMENTS (olddecl))
3378 DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl);
3379 }
3380 }
0b1161fc
MM
3381 else if (TREE_CODE (newdecl) == NAMESPACE_DECL)
3382 NAMESPACE_LEVEL (newdecl) = NAMESPACE_LEVEL (olddecl);
a9aedbc2 3383
8d08fdba
MS
3384 /* Now preserve various other info from the definition. */
3385 TREE_ADDRESSABLE (newdecl) = TREE_ADDRESSABLE (olddecl);
3386 TREE_ASM_WRITTEN (newdecl) = TREE_ASM_WRITTEN (olddecl);
3387 DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
92643fea 3388 COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
8d08fdba 3389
8d08fdba
MS
3390 if (TREE_CODE (newdecl) == FUNCTION_DECL)
3391 {
3392 int function_size;
8d08fdba
MS
3393
3394 function_size = sizeof (struct tree_decl);
3395
730e1556
KG
3396 memcpy ((char *) olddecl + sizeof (struct tree_common),
3397 (char *) newdecl + sizeof (struct tree_common),
3398 function_size - sizeof (struct tree_common));
8d08fdba 3399
75650646 3400 if (DECL_TEMPLATE_INSTANTIATION (newdecl))
b3445994
MM
3401 /* If newdecl is a template instantiation, it is possible that
3402 the following sequence of events has occurred:
3403
3404 o A friend function was declared in a class template. The
3405 class template was instantiated.
3406
3407 o The instantiation of the friend declaration was
3408 recorded on the instantiation list, and is newdecl.
3409
3410 o Later, however, instantiate_class_template called pushdecl
3411 on the newdecl to perform name injection. But, pushdecl in
3412 turn called duplicate_decls when it discovered that another
3413 declaration of a global function with the same name already
3414 existed.
3415
3416 o Here, in duplicate_decls, we decided to clobber newdecl.
3417
3418 If we're going to do that, we'd better make sure that
3419 olddecl, and not newdecl, is on the list of
3420 instantiations so that if we try to do the instantiation
3421 again we won't get the clobbered declaration. */
3422 reregister_specialization (newdecl,
3423 DECL_TI_TEMPLATE (newdecl),
3424 olddecl);
8d08fdba
MS
3425 }
3426 else
3427 {
730e1556
KG
3428 memcpy ((char *) olddecl + sizeof (struct tree_common),
3429 (char *) newdecl + sizeof (struct tree_common),
3430 sizeof (struct tree_decl) - sizeof (struct tree_common)
2f9834e8 3431 + TREE_CODE_LENGTH (TREE_CODE (newdecl)) * sizeof (char *));
8d08fdba
MS
3432 }
3433
3434 DECL_UID (olddecl) = olddecl_uid;
3435 if (olddecl_friend)
3436 DECL_FRIEND_P (olddecl) = 1;
3437
d9525bec
BK
3438 /* NEWDECL contains the merged attribute lists.
3439 Update OLDDECL to be the same. */
91d231cb 3440 DECL_ATTRIBUTES (olddecl) = DECL_ATTRIBUTES (newdecl);
d9525bec 3441
16e31964
DS
3442 /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
3443 so that encode_section_info has a chance to look at the new decl
3444 flags and attributes. */
3445 if (DECL_RTL_SET_P (olddecl)
3446 && (TREE_CODE (olddecl) == FUNCTION_DECL
3447 || (TREE_CODE (olddecl) == VAR_DECL
3448 && TREE_STATIC (olddecl))))
3449 make_decl_rtl (olddecl, NULL);
3450
8d08fdba
MS
3451 return 1;
3452}
3453
3454/* Record a decl-node X as belonging to the current lexical scope.
3455 Check for errors (such as an incompatible declaration for the same
3456 name already seen in the same scope).
3457
3458 Returns either X or an old decl for the same name.
3459 If an old decl is returned, it may have been smashed
3460 to agree with what X says. */
3461
3462tree
11f6b451 3463pushdecl (tree x)
8d08fdba
MS
3464{
3465 register tree t;
b35d4555
MM
3466 register tree name;
3467 int need_new_binding;
3468
22ffcc6f 3469 timevar_push (TV_NAME_LOOKUP);
b35d4555 3470
b35d4555 3471 need_new_binding = 1;
8d08fdba 3472
50714e79
MM
3473 if (DECL_TEMPLATE_PARM_P (x))
3474 /* Template parameters have no context; they are not X::T even
3475 when declared within a class or namespace. */
3476 ;
3477 else
3478 {
3479 if (current_function_decl && x != current_function_decl
3480 /* A local declaration for a function doesn't constitute
3481 nesting. */
f39ee884
MM
3482 && !(TREE_CODE (x) == FUNCTION_DECL && !DECL_INITIAL (x))
3483 /* A local declaration for an `extern' variable is in the
94dfccd1 3484 scope of the current namespace, not the current
f39ee884
MM
3485 function. */
3486 && !(TREE_CODE (x) == VAR_DECL && DECL_EXTERNAL (x))
50714e79
MM
3487 && !DECL_CONTEXT (x))
3488 DECL_CONTEXT (x) = current_function_decl;
cd9f6678
MM
3489
3490 /* If this is the declaration for a namespace-scope function,
3491 but the declaration itself is in a local scope, mark the
3492 declaration. */
68642fb6 3493 if (TREE_CODE (x) == FUNCTION_DECL
cd9f6678
MM
3494 && DECL_NAMESPACE_SCOPE_P (x)
3495 && current_function_decl
3496 && x != current_function_decl)
3497 DECL_LOCAL_FUNCTION_P (x) = 1;
50714e79 3498 }
8d08fdba 3499
fe8fadc1 3500 name = DECL_NAME (x);
8d08fdba
MS
3501 if (name)
3502 {
94dfccd1
JM
3503 int different_binding_level = 0;
3504
386b8a85
JM
3505 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
3506 name = TREE_OPERAND (name, 0);
68642fb6 3507
94dfccd1
JM
3508 /* In case this decl was explicitly namespace-qualified, look it
3509 up in its namespace context. */
3510 if (TREE_CODE (x) == VAR_DECL && DECL_NAMESPACE_SCOPE_P (x)
3511 && namespace_bindings_p ())
2c73f9f5
ML
3512 t = namespace_binding (name, DECL_CONTEXT (x));
3513 else
3514 t = lookup_name_current_level (name);
fe8fadc1 3515
94dfccd1
JM
3516 /* [basic.link] If there is a visible declaration of an entity
3517 with linkage having the same name and type, ignoring entities
3518 declared outside the innermost enclosing namespace scope, the
3519 block scope declaration declares that same entity and
3520 receives the linkage of the previous declaration. */
3521 if (! t && current_function_decl && x != current_function_decl
3522 && (TREE_CODE (x) == FUNCTION_DECL || TREE_CODE (x) == VAR_DECL)
3523 && DECL_EXTERNAL (x))
3524 {
3525 /* Look in block scope. */
3526 t = IDENTIFIER_VALUE (name);
3527 /* Or in the innermost namespace. */
3528 if (! t)
3529 t = namespace_binding (name, DECL_CONTEXT (x));
fab09a24
JM
3530 /* Does it have linkage? Note that if this isn't a DECL, it's an
3531 OVERLOAD, which is OK. */
3532 if (t && DECL_P (t) && ! (TREE_STATIC (t) || DECL_EXTERNAL (t)))
94dfccd1
JM
3533 t = NULL_TREE;
3534 if (t)
3535 different_binding_level = 1;
3536 }
3537
fe8fadc1
MM
3538 /* If we are declaring a function, and the result of name-lookup
3539 was an OVERLOAD, look for an overloaded instance that is
3540 actually the same as the function we are declaring. (If
3541 there is one, we have to merge our declaration with the
3542 previous declaration.) */
655dc6ee 3543 if (t && TREE_CODE (t) == OVERLOAD)
fe8fadc1
MM
3544 {
3545 tree match;
3546
655dc6ee
JM
3547 if (TREE_CODE (x) == FUNCTION_DECL)
3548 for (match = t; match; match = OVL_NEXT (match))
3549 {
92643fea 3550 if (decls_match (OVL_CURRENT (match), x))
655dc6ee
JM
3551 break;
3552 }
3553 else
3554 /* Just choose one. */
3555 match = t;
fe8fadc1
MM
3556
3557 if (match)
3558 t = OVL_CURRENT (match);
3559 else
3560 t = NULL_TREE;
3561 }
3562
8d08fdba
MS
3563 if (t == error_mark_node)
3564 {
3565 /* error_mark_node is 0 for a while during initialization! */
3566 t = NULL_TREE;
8251199e 3567 cp_error_at ("`%#D' used prior to declaration", x);
8d08fdba 3568 }
51c184be 3569 else if (t != NULL_TREE)
8d08fdba 3570 {
94dfccd1
JM
3571 if (different_binding_level)
3572 {
3573 if (decls_match (x, t))
3574 /* The standard only says that the local extern
3575 inherits linkage from the previous decl; in
3576 particular, default args are not shared. It would
3577 be nice to propagate inlining info, though. FIXME. */
3578 TREE_PUBLIC (x) = TREE_PUBLIC (t);
3579 }
3580 else if (TREE_CODE (t) == PARM_DECL)
8d08fdba
MS
3581 {
3582 if (DECL_CONTEXT (t) == NULL_TREE)
34cd5ae7 3583 /* This is probably caused by too many errors, but calling
400500c4
RK
3584 abort will say that if errors have occurred. */
3585 abort ();
be99da77 3586
e92cc029 3587 /* Check for duplicate params. */
be99da77 3588 if (duplicate_decls (x, t))
22ffcc6f 3589 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
8d08fdba 3590 }
eb68cb58 3591 else if ((DECL_EXTERN_C_FUNCTION_P (x)
5566b478
MS
3592 || DECL_FUNCTION_TEMPLATE_P (x))
3593 && is_overloaded_fn (t))
a1c65f9f 3594 /* Don't do anything just yet. */;
e1cd6e56
MS
3595 else if (t == wchar_decl_node)
3596 {
3597 if (pedantic && ! DECL_IN_SYSTEM_HEADER (x))
33bd39a2 3598 pedwarn ("redeclaration of `wchar_t' as `%T'",
2b0cbc5d 3599 TREE_TYPE (x));
e1cd6e56
MS
3600
3601 /* Throw away the redeclaration. */
22ffcc6f 3602 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
e1cd6e56 3603 }
8926095f 3604 else if (TREE_CODE (t) != TREE_CODE (x))
8d08fdba 3605 {
9ed182dc 3606 if (duplicate_decls (x, t))
22ffcc6f 3607 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
8d08fdba
MS
3608 }
3609 else if (duplicate_decls (x, t))
51c184be 3610 {
7177d104
MS
3611 if (TREE_CODE (t) == TYPE_DECL)
3612 SET_IDENTIFIER_TYPE_VALUE (name, TREE_TYPE (t));
f30432d7
MS
3613 else if (TREE_CODE (t) == FUNCTION_DECL)
3614 check_default_args (t);
7177d104 3615
22ffcc6f 3616 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
51c184be 3617 }
35680744
MM
3618 else if (DECL_MAIN_P (x))
3619 {
3620 /* A redeclaration of main, but not a duplicate of the
68642fb6 3621 previous one.
35680744
MM
3622
3623 [basic.start.main]
3624
3625 This function shall not be overloaded. */
8251199e 3626 cp_error_at ("invalid redeclaration of `%D'", t);
33bd39a2 3627 error ("as `%D'", x);
35680744
MM
3628 /* We don't try to push this declaration since that
3629 causes a crash. */
22ffcc6f 3630 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, x);
35680744 3631 }
8d08fdba 3632 }
8926095f 3633
f3400fe2
JM
3634 check_template_shadow (x);
3635
fcfcdfc8
JM
3636 /* If this is a function conjured up by the backend, massage it
3637 so it looks friendly. */
eb68cb58 3638 if (DECL_NON_THUNK_FUNCTION_P (x) && ! DECL_LANG_SPECIFIC (x))
fcfcdfc8
JM
3639 {
3640 retrofit_lang_decl (x);
5d2ed28c 3641 SET_DECL_LANGUAGE (x, lang_c);
fcfcdfc8
JM
3642 }
3643
eb68cb58 3644 if (DECL_NON_THUNK_FUNCTION_P (x) && ! DECL_FUNCTION_MEMBER_P (x))
8926095f 3645 {
7bdbfa05 3646 t = push_overloaded_decl (x, PUSH_LOCAL);
5fdaba89 3647 if (t != x)
22ffcc6f 3648 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
f181d4ae
MM
3649 if (!namespace_bindings_p ())
3650 /* We do not need to create a binding for this name;
3651 push_overloaded_decl will have already done so if
3652 necessary. */
3653 need_new_binding = 0;
8926095f 3654 }
6eb3bb27 3655 else if (DECL_FUNCTION_TEMPLATE_P (x) && DECL_NAMESPACE_SCOPE_P (x))
cfe507be
MM
3656 {
3657 t = push_overloaded_decl (x, PUSH_GLOBAL);
3658 if (t == x)
3659 add_decl_to_level (x, NAMESPACE_LEVEL (CP_DECL_CONTEXT (t)));
22ffcc6f 3660 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
cfe507be 3661 }
8d08fdba 3662
a1774733
BK
3663 /* If declaring a type as a typedef, copy the type (unless we're
3664 at line 0), and install this TYPE_DECL as the new type's typedef
a1c65f9f 3665 name. See the extensive comment in ../c-decl.c (pushdecl). */
8d08fdba
MS
3666 if (TREE_CODE (x) == TYPE_DECL)
3667 {
3668 tree type = TREE_TYPE (x);
f31686a3 3669 if (DECL_SOURCE_LINE (x) == 0)
a1774733
BK
3670 {
3671 if (TYPE_NAME (type) == 0)
3672 TYPE_NAME (type) = x;
3673 }
1c80fb65
MM
3674 else if (type != error_mark_node && TYPE_NAME (type) != x
3675 /* We don't want to copy the type when all we're
3676 doing is making a TYPE_DECL for the purposes of
3677 inlining. */
68642fb6 3678 && (!TYPE_NAME (type)
1c80fb65 3679 || TYPE_NAME (type) != DECL_ABSTRACT_ORIGIN (x)))
a1774733
BK
3680 {
3681 DECL_ORIGINAL_TYPE (x) = type;
3682 type = build_type_copy (type);
3683 TYPE_STUB_DECL (type) = TYPE_STUB_DECL (DECL_ORIGINAL_TYPE (x));
3684 TYPE_NAME (type) = x;
3685 TREE_TYPE (x) = type;
3686 }
8d08fdba 3687
8d08fdba
MS
3688 if (type != error_mark_node
3689 && TYPE_NAME (type)
3690 && TYPE_IDENTIFIER (type))
4b0d3cbe 3691 set_identifier_type_value (DECL_NAME (x), x);
8d08fdba
MS
3692 }
3693
3694 /* Multiple external decls of the same identifier ought to match.
3695
3696 We get warnings about inline functions where they are defined.
39211cd5 3697 We get warnings about other functions from push_overloaded_decl.
68642fb6 3698
8d08fdba 3699 Avoid duplicate warnings where they are used. */
39211cd5 3700 if (TREE_PUBLIC (x) && TREE_CODE (x) != FUNCTION_DECL)
8d08fdba
MS
3701 {
3702 tree decl;
3703
f49fad00
JM
3704 decl = IDENTIFIER_NAMESPACE_VALUE (name);
3705 if (decl && TREE_CODE (decl) == OVERLOAD)
3706 decl = OVL_FUNCTION (decl);
8d08fdba 3707
f49fad00
JM
3708 if (decl && decl != error_mark_node
3709 && (DECL_EXTERNAL (decl) || TREE_PUBLIC (decl))
8d08fdba
MS
3710 /* If different sort of thing, we already gave an error. */
3711 && TREE_CODE (decl) == TREE_CODE (x)
3bfdc719 3712 && !same_type_p (TREE_TYPE (x), TREE_TYPE (decl)))
8d08fdba 3713 {
04d6ccbd 3714 pedwarn ("type mismatch with previous external decl of `%#D'", x);
8251199e 3715 cp_pedwarn_at ("previous external decl of `%#D'", decl);
8d08fdba
MS
3716 }
3717 }
3718
8d08fdba
MS
3719 /* This name is new in its binding level.
3720 Install the new declaration and return it. */
2c73f9f5 3721 if (namespace_bindings_p ())
8d08fdba
MS
3722 {
3723 /* Install a global value. */
3724
8d08fdba
MS
3725 /* If the first global decl has external linkage,
3726 warn if we later see static one. */
31928556 3727 if (IDENTIFIER_GLOBAL_VALUE (name) == NULL_TREE && TREE_PUBLIC (x))
8d08fdba 3728 TREE_PUBLIC (name) = 1;
5362b086 3729
92643fea
MM
3730 /* Bind the name for the entity. */
3731 if (!(TREE_CODE (x) == TYPE_DECL && DECL_ARTIFICIAL (x)
3732 && t != NULL_TREE)
3733 && (TREE_CODE (x) == TYPE_DECL
3734 || TREE_CODE (x) == VAR_DECL
0ca7178c 3735 || TREE_CODE (x) == ALIAS_DECL
92643fea
MM
3736 || TREE_CODE (x) == NAMESPACE_DECL
3737 || TREE_CODE (x) == CONST_DECL
3738 || TREE_CODE (x) == TEMPLATE_DECL))
3739 SET_IDENTIFIER_NAMESPACE_VALUE (name, x);
8d08fdba
MS
3740
3741 /* Don't forget if the function was used via an implicit decl. */
3742 if (IDENTIFIER_IMPLICIT_DECL (name)
3743 && TREE_USED (IDENTIFIER_IMPLICIT_DECL (name)))
3744 TREE_USED (x) = 1;
3745
3746 /* Don't forget if its address was taken in that way. */
3747 if (IDENTIFIER_IMPLICIT_DECL (name)
3748 && TREE_ADDRESSABLE (IDENTIFIER_IMPLICIT_DECL (name)))
3749 TREE_ADDRESSABLE (x) = 1;
3750
3751 /* Warn about mismatches against previous implicit decl. */
3752 if (IDENTIFIER_IMPLICIT_DECL (name) != NULL_TREE
3753 /* If this real decl matches the implicit, don't complain. */
3754 && ! (TREE_CODE (x) == FUNCTION_DECL
3755 && TREE_TYPE (TREE_TYPE (x)) == integer_type_node))
33bd39a2 3756 warning
8251199e 3757 ("`%D' was previously implicitly declared to return `int'", x);
8d08fdba
MS
3758
3759 /* If new decl is `static' and an `extern' was seen previously,
3760 warn about it. */
a0a33927
MS
3761 if (x != NULL_TREE && t != NULL_TREE && decls_match (x, t))
3762 warn_extern_redeclared_static (x, t);
8d08fdba
MS
3763 }
3764 else
3765 {
3766 /* Here to install a non-global value. */
f181d4ae 3767 tree oldlocal = IDENTIFIER_VALUE (name);
31928556 3768 tree oldglobal = IDENTIFIER_NAMESPACE_VALUE (name);
8d08fdba 3769
f181d4ae
MM
3770 if (need_new_binding)
3771 {
0034cf72 3772 push_local_binding (name, x, 0);
f181d4ae
MM
3773 /* Because push_local_binding will hook X on to the
3774 current_binding_level's name list, we don't want to
3775 do that again below. */
3776 need_new_binding = 0;
3777 }
8d08fdba
MS
3778
3779 /* If this is a TYPE_DECL, push it into the type value slot. */
3780 if (TREE_CODE (x) == TYPE_DECL)
4b0d3cbe 3781 set_identifier_type_value (name, x);
8d08fdba 3782
a9aedbc2
MS
3783 /* Clear out any TYPE_DECL shadowed by a namespace so that
3784 we won't think this is a type. The C struct hack doesn't
3785 go through namespaces. */
3786 if (TREE_CODE (x) == NAMESPACE_DECL)
4b0d3cbe 3787 set_identifier_type_value (name, NULL_TREE);
a9aedbc2 3788
e905ac8a
MS
3789 if (oldlocal)
3790 {
3791 tree d = oldlocal;
17aec3eb 3792
74dc0d8c
MS
3793 while (oldlocal
3794 && TREE_CODE (oldlocal) == VAR_DECL
3795 && DECL_DEAD_FOR_LOCAL (oldlocal))
17aec3eb
RK
3796 oldlocal = DECL_SHADOWED_FOR_VAR (oldlocal);
3797
e905ac8a
MS
3798 if (oldlocal == NULL_TREE)
3799 oldlocal = IDENTIFIER_NAMESPACE_VALUE (DECL_NAME (d));
3800 }
3801
8d08fdba
MS
3802 /* If this is an extern function declaration, see if we
3803 have a global definition or declaration for the function. */
3804 if (oldlocal == NULL_TREE
faae18ab 3805 && DECL_EXTERNAL (x)
31928556 3806 && oldglobal != NULL_TREE
8d08fdba 3807 && TREE_CODE (x) == FUNCTION_DECL
31928556 3808 && TREE_CODE (oldglobal) == FUNCTION_DECL)
8d08fdba
MS
3809 {
3810 /* We have one. Their types must agree. */
31928556 3811 if (decls_match (x, oldglobal))
6060a796
MS
3812 /* OK */;
3813 else
8d08fdba 3814 {
33bd39a2 3815 warning ("extern declaration of `%#D' doesn't match", x);
8251199e 3816 cp_warning_at ("global declaration `%#D'", oldglobal);
8d08fdba 3817 }
8d08fdba
MS
3818 }
3819 /* If we have a local external declaration,
3820 and no file-scope declaration has yet been seen,
3821 then if we later have a file-scope decl it must not be static. */
3822 if (oldlocal == NULL_TREE
31928556 3823 && oldglobal == NULL_TREE
8d08fdba
MS
3824 && DECL_EXTERNAL (x)
3825 && TREE_PUBLIC (x))
f181d4ae 3826 TREE_PUBLIC (name) = 1;
8d08fdba 3827
8d08fdba 3828 /* Warn if shadowing an argument at the top level of the body. */
e905ac8a
MS
3829 if (oldlocal != NULL_TREE && !DECL_EXTERNAL (x)
3830 /* Inline decls shadow nothing. */
3831 && !DECL_FROM_INLINE (x)
07681121
MM
3832 && TREE_CODE (oldlocal) == PARM_DECL
3833 /* Don't check the `this' parameter. */
3834 && !DECL_ARTIFICIAL (oldlocal))
8d08fdba 3835 {
8ac9ea61 3836 bool err = false;
8d08fdba 3837
8ac9ea61
NB
3838 /* Don't complain if it's from an enclosing function. */
3839 if (DECL_CONTEXT (oldlocal) == current_function_decl
3840 && TREE_CODE (x) != PARM_DECL)
3841 {
3842 /* Go to where the parms should be and see if we find
3843 them there. */
e2500fed 3844 struct cp_binding_level *b = current_binding_level->level_chain;
8ac9ea61 3845
9f175208
JM
3846 /* Skip the ctor/dtor cleanup level. */
3847 b = b->level_chain;
3848
8ac9ea61 3849 /* ARM $8.3 */
a7e8c268 3850 if (b->kind == sk_function_parms)
8ac9ea61 3851 {
33bd39a2 3852 error ("declaration of `%#D' shadows a parameter",
a7e8c268 3853 name);
8ac9ea61
NB
3854 err = true;
3855 }
3856 }
8d08fdba 3857
8ac9ea61 3858 if (warn_shadow && !err)
339a28b9 3859 shadow_warning (SW_PARAM,
d9b2742a 3860 IDENTIFIER_POINTER (name), oldlocal);
8d08fdba 3861 }
e905ac8a 3862
8d08fdba 3863 /* Maybe warn if shadowing something else. */
8ac9ea61 3864 else if (warn_shadow && !DECL_EXTERNAL (x)
e905ac8a
MS
3865 /* No shadow warnings for internally generated vars. */
3866 && ! DECL_ARTIFICIAL (x)
3867 /* No shadow warnings for vars made for inlining. */
3868 && ! DECL_FROM_INLINE (x))
8d08fdba 3869 {
8ac9ea61 3870 if (IDENTIFIER_CLASS_VALUE (name) != NULL_TREE
4ac14744 3871 && current_class_ptr
8d08fdba 3872 && !TREE_STATIC (name))
33bd39a2 3873 warning ("declaration of `%s' shadows a member of `this'",
8ac9ea61
NB
3874 IDENTIFIER_POINTER (name));
3875 else if (oldlocal != NULL_TREE
3876 && TREE_CODE (oldlocal) == VAR_DECL)
339a28b9 3877 shadow_warning (SW_LOCAL,
d9b2742a 3878 IDENTIFIER_POINTER (name), oldlocal);
8ac9ea61
NB
3879 else if (oldglobal != NULL_TREE
3880 && TREE_CODE (oldglobal) == VAR_DECL)
30394414 3881 /* XXX shadow warnings in outer-more namespaces */
339a28b9 3882 shadow_warning (SW_GLOBAL,
d9b2742a 3883 IDENTIFIER_POINTER (name), oldglobal);
8d08fdba 3884 }
e1cd6e56 3885 }
8d08fdba 3886
e1cd6e56 3887 if (TREE_CODE (x) == FUNCTION_DECL)
f30432d7 3888 check_default_args (x);
8145f082 3889
0154eaa8
MM
3890 if (TREE_CODE (x) == VAR_DECL)
3891 maybe_register_incomplete_var (x);
8d08fdba
MS
3892 }
3893
f181d4ae 3894 if (need_new_binding)
68642fb6 3895 add_decl_to_level (x,
efee38a9
MM
3896 DECL_NAMESPACE_SCOPE_P (x)
3897 ? NAMESPACE_LEVEL (CP_DECL_CONTEXT (x))
3898 : current_binding_level);
8d08fdba 3899
22ffcc6f 3900 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, x);
8d08fdba
MS
3901}
3902
5566b478
MS
3903/* Same as pushdecl, but define X in binding-level LEVEL. We rely on the
3904 caller to set DECL_CONTEXT properly. */
8d08fdba
MS
3905
3906static tree
11f6b451 3907pushdecl_with_scope (tree x, struct cp_binding_level* level)
8d08fdba 3908{
e2500fed 3909 register struct cp_binding_level *b;
5566b478 3910 tree function_decl = current_function_decl;
8d08fdba 3911
22ffcc6f 3912 timevar_push (TV_NAME_LOOKUP);
5566b478 3913 current_function_decl = NULL_TREE;
a7e8c268 3914 if (level->kind == sk_class)
8d019cef
JM
3915 {
3916 b = class_binding_level;
3917 class_binding_level = level;
3918 pushdecl_class_level (x);
3919 class_binding_level = b;
3920 }
3921 else
3922 {
3923 b = current_binding_level;
ff955512 3924 current_binding_level = level;
8d019cef 3925 x = pushdecl (x);
ff955512 3926 current_binding_level = b;
8d019cef 3927 }
5566b478 3928 current_function_decl = function_decl;
22ffcc6f 3929 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, x);
8d08fdba
MS
3930}
3931
2c73f9f5 3932/* Like pushdecl, only it places X in the current namespace,
8d08fdba 3933 if appropriate. */
e92cc029 3934
8d08fdba 3935tree
11f6b451 3936pushdecl_namespace_level (tree x)
8d08fdba 3937{
e2500fed 3938 register struct cp_binding_level *b = current_binding_level;
2c73f9f5
ML
3939 register tree t;
3940
22ffcc6f 3941 timevar_push (TV_NAME_LOOKUP);
2c73f9f5 3942 t = pushdecl_with_scope (x, NAMESPACE_LEVEL (current_namespace));
8d08fdba
MS
3943
3944 /* Now, the type_shadowed stack may screw us. Munge it so it does
3945 what we want. */
3946 if (TREE_CODE (x) == TYPE_DECL)
3947 {
3948 tree name = DECL_NAME (x);
3949 tree newval;
3950 tree *ptr = (tree *)0;
5f52c0e0 3951 for (; !global_scope_p (b); b = b->level_chain)
8d08fdba
MS
3952 {
3953 tree shadowed = b->type_shadowed;
3954 for (; shadowed; shadowed = TREE_CHAIN (shadowed))
3955 if (TREE_PURPOSE (shadowed) == name)
3956 {
3957 ptr = &TREE_VALUE (shadowed);
3958 /* Can't break out of the loop here because sometimes
3959 a binding level will have duplicate bindings for
3960 PT names. It's gross, but I haven't time to fix it. */
3961 }
3962 }
3963 newval = TREE_TYPE (x);
3964 if (ptr == (tree *)0)
3965 {
3966 /* @@ This shouldn't be needed. My test case "zstring.cc" trips
3967 up here if this is changed to an assertion. --KR */
4b0d3cbe 3968 SET_IDENTIFIER_TYPE_VALUE (name, x);
8d08fdba
MS
3969 }
3970 else
3971 {
8d08fdba
MS
3972 *ptr = newval;
3973 }
3974 }
22ffcc6f 3975 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
8d08fdba
MS
3976}
3977
170b020f
MM
3978/* Like pushdecl, only it places X in the global scope if appropriate.
3979 Calls cp_finish_decl to register the variable, initializing it with
3980 *INIT, if INIT is non-NULL. */
2c73f9f5 3981
170b020f
MM
3982static tree
3983pushdecl_top_level_1 (tree x, tree *init)
2c73f9f5 3984{
22ffcc6f 3985 timevar_push (TV_NAME_LOOKUP);
b35d4555 3986 push_to_top_level ();
2c73f9f5 3987 x = pushdecl_namespace_level (x);
170b020f
MM
3988 if (init)
3989 cp_finish_decl (x, *init, NULL_TREE, 0);
b35d4555 3990 pop_from_top_level ();
22ffcc6f 3991 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, x);
2c73f9f5
ML
3992}
3993
170b020f
MM
3994/* Like pushdecl, only it places X in the global scope if appropriate. */
3995
3996tree
3997pushdecl_top_level (tree x)
3998{
3999 return pushdecl_top_level_1 (x, NULL);
4000}
4001
4002/* Like pushdecl, only it places X in the global scope if
4003 appropriate. Calls cp_finish_decl to register the variable,
4004 initializing it with INIT. */
4005
4006tree
4007pushdecl_top_level_and_finish (tree x, tree init)
4008{
4009 return pushdecl_top_level_1 (x, &init);
4010}
4011
8d08fdba 4012/* Make the declaration of X appear in CLASS scope. */
e92cc029 4013
f139561c 4014bool
11f6b451 4015pushdecl_class_level (tree x)
8d08fdba 4016{
22eadedb 4017 tree name;
f139561c 4018 bool is_valid = true;
8f032717 4019
22ffcc6f 4020 timevar_push (TV_NAME_LOOKUP);
22eadedb 4021 /* Get the name of X. */
8f032717 4022 if (TREE_CODE (x) == OVERLOAD)
22eadedb
MM
4023 name = DECL_NAME (get_first_fn (x));
4024 else
4025 name = DECL_NAME (x);
8d08fdba
MS
4026
4027 if (name)
4028 {
f139561c 4029 is_valid = push_class_level_binding (name, x);
8d08fdba 4030 if (TREE_CODE (x) == TYPE_DECL)
4b0d3cbe 4031 set_identifier_type_value (name, x);
8d08fdba 4032 }
6bdb8141 4033 else if (ANON_AGGR_TYPE_P (TREE_TYPE (x)))
8f032717 4034 {
22eadedb
MM
4035 /* If X is an anonymous aggregate, all of its members are
4036 treated as if they were members of the class containing the
4037 aggregate, for naming purposes. */
8f032717
MM
4038 tree f;
4039
22eadedb 4040 for (f = TYPE_FIELDS (TREE_TYPE (x)); f; f = TREE_CHAIN (f))
f139561c 4041 {
b3179b0d 4042 location_t save_location = input_location;
f31686a3 4043 input_location = DECL_SOURCE_LOCATION (f);
f139561c
MM
4044 if (!pushdecl_class_level (f))
4045 is_valid = false;
b3179b0d 4046 input_location = save_location;
f139561c 4047 }
8f032717 4048 }
22ffcc6f 4049 timevar_pop (TV_NAME_LOOKUP);
f139561c
MM
4050
4051 return is_valid;
8d08fdba
MS
4052}
4053
9188c363
MM
4054/* Enter DECL into the symbol table, if that's appropriate. Returns
4055 DECL, or a modified version thereof. */
4056
4057tree
11f6b451 4058maybe_push_decl (tree decl)
9188c363
MM
4059{
4060 tree type = TREE_TYPE (decl);
4061
4062 /* Add this decl to the current binding level, but not if it comes
4063 from another scope, e.g. a static member variable. TEM may equal
4064 DECL or it may be a previous decl of the same name. */
07c88314
MM
4065 if (decl == error_mark_node
4066 || (TREE_CODE (decl) != PARM_DECL
4067 && DECL_CONTEXT (decl) != NULL_TREE
4068 /* Definitions of namespace members outside their namespace are
a1c65f9f 4069 possible. */
07c88314 4070 && TREE_CODE (DECL_CONTEXT (decl)) != NAMESPACE_DECL)
9188c363
MM
4071 || (TREE_CODE (decl) == TEMPLATE_DECL && !namespace_bindings_p ())
4072 || TREE_CODE (type) == UNKNOWN_TYPE
558475f0 4073 /* The declaration of a template specialization does not affect
9188c363
MM
4074 the functions available for overload resolution, so we do not
4075 call pushdecl. */
4076 || (TREE_CODE (decl) == FUNCTION_DECL
4077 && DECL_TEMPLATE_SPECIALIZATION (decl)))
4078 return decl;
4079 else
4080 return pushdecl (decl);
4081}
4082
f139561c
MM
4083/* Make the declaration(s) of X appear in CLASS scope under the name
4084 NAME. Returns true if the binding is valid. */
e92cc029 4085
f139561c 4086bool
11f6b451 4087push_class_level_binding (tree name, tree x)
8d08fdba 4088{
af6fd53f 4089 cxx_binding *binding;
f139561c 4090
22ffcc6f 4091 timevar_push (TV_NAME_LOOKUP);
68642fb6 4092 /* The class_binding_level will be NULL if x is a template
98c1c668
JM
4093 parameter name in a member template. */
4094 if (!class_binding_level)
f139561c 4095 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, true);
98c1c668 4096
908c4e83
MM
4097 /* Make sure that this new member does not have the same name
4098 as a template parameter. */
4099 if (TYPE_BEING_DEFINED (current_class_type))
4100 check_template_shadow (x);
4101
f181d4ae
MM
4102 /* If this declaration shadows a declaration from an enclosing
4103 class, then we will need to restore IDENTIFIER_CLASS_VALUE when
4104 we leave this class. Record the shadowed declaration here. */
8f032717 4105 binding = IDENTIFIER_BINDING (name);
147135cc 4106 if (binding && binding->value)
8f032717 4107 {
147135cc 4108 tree bval = binding->value;
399dedb9
NS
4109 tree old_decl = NULL_TREE;
4110
4111 if (INHERITED_VALUE_BINDING_P (binding))
8f032717 4112 {
399dedb9
NS
4113 /* If the old binding was from a base class, and was for a
4114 tag name, slide it over to make room for the new binding.
4115 The old binding is still visible if explicitly qualified
4116 with a class-key. */
4117 if (TREE_CODE (bval) == TYPE_DECL && DECL_ARTIFICIAL (bval)
4118 && !(TREE_CODE (x) == TYPE_DECL && DECL_ARTIFICIAL (x)))
4119 {
147135cc
GDR
4120 old_decl = binding->type;
4121 binding->type = bval;
4122 binding->value = NULL_TREE;
399dedb9
NS
4123 INHERITED_VALUE_BINDING_P (binding) = 0;
4124 }
6d9a0a36
NS
4125 else
4126 old_decl = bval;
399dedb9
NS
4127 }
4128 else if (TREE_CODE (x) == OVERLOAD && is_overloaded_fn (bval))
4129 old_decl = bval;
4130 else if (TREE_CODE (x) == USING_DECL && TREE_CODE (bval) == USING_DECL)
4131 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, true);
4132 else if (TREE_CODE (x) == USING_DECL && is_overloaded_fn (bval))
4133 old_decl = bval;
4134 else if (TREE_CODE (bval) == USING_DECL && is_overloaded_fn (x))
4135 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, true);
4136
4137 if (old_decl)
4138 {
4139 tree shadow;
4140
4141 /* Find the previous binding of name on the class-shadowed
4142 list, and update it. */
4143 for (shadow = class_binding_level->class_shadowed;
4144 shadow;
4145 shadow = TREE_CHAIN (shadow))
4146 if (TREE_PURPOSE (shadow) == name
4147 && TREE_TYPE (shadow) == old_decl)
4148 {
147135cc 4149 binding->value = x;
399dedb9
NS
4150 INHERITED_VALUE_BINDING_P (binding) = 0;
4151 TREE_TYPE (shadow) = x;
4152 IDENTIFIER_CLASS_VALUE (name) = x;
4153 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, true);
4154 }
8f032717 4155 }
8f032717 4156 }
f181d4ae 4157
8f032717 4158 /* If we didn't replace an existing binding, put the binding on the
76191fdd 4159 stack of bindings for the identifier, and update the shadowed list. */
8f032717
MM
4160 if (push_class_binding (name, x))
4161 {
8f032717 4162 class_binding_level->class_shadowed
76191fdd 4163 = tree_cons (name, NULL,
8f032717 4164 class_binding_level->class_shadowed);
8f032717
MM
4165 /* Record the value we are binding NAME to so that we can know
4166 what to pop later. */
4167 TREE_TYPE (class_binding_level->class_shadowed) = x;
f139561c 4168 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, true);
8f032717 4169 }
f139561c
MM
4170
4171 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, false);
8d08fdba
MS
4172}
4173
dd4fae80
ML
4174/* Insert another USING_DECL into the current binding level, returning
4175 this declaration. If this is a redeclaration, do nothing, and
4176 return NULL_TREE if this not in namespace scope (in namespace
4177 scope, a using decl might extend any previous bindings). */
e92cc029 4178
2c73f9f5 4179tree
11f6b451 4180push_using_decl (tree scope, tree name)
8d08fdba 4181{
2c73f9f5 4182 tree decl;
68642fb6 4183
22ffcc6f 4184 timevar_push (TV_NAME_LOOKUP);
2c73f9f5
ML
4185 my_friendly_assert (TREE_CODE (scope) == NAMESPACE_DECL, 383);
4186 my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, 384);
4187 for (decl = current_binding_level->usings; decl; decl = TREE_CHAIN (decl))
4188 if (DECL_INITIAL (decl) == scope && DECL_NAME (decl) == name)
4189 break;
4190 if (decl)
22ffcc6f
GDR
4191 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP,
4192 namespace_bindings_p () ? decl : NULL_TREE);
4ce3d537 4193 decl = build_lang_decl (USING_DECL, name, void_type_node);
2c73f9f5
ML
4194 DECL_INITIAL (decl) = scope;
4195 TREE_CHAIN (decl) = current_binding_level->usings;
4196 current_binding_level->usings = decl;
22ffcc6f 4197 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
8d08fdba
MS
4198}
4199
ea9635c7
ML
4200/* Add namespace to using_directives. Return NULL_TREE if nothing was
4201 changed (i.e. there was already a directive), or the fresh
4202 TREE_LIST otherwise. */
4203
4204tree
11f6b451 4205push_using_directive (tree used)
ea9635c7
ML
4206{
4207 tree ud = current_binding_level->using_directives;
9ed182dc 4208 tree iter, ancestor;
68642fb6 4209
22ffcc6f 4210 timevar_push (TV_NAME_LOOKUP);
a1c65f9f 4211 /* Check if we already have this. */
ea9635c7 4212 if (purpose_member (used, ud) != NULL_TREE)
22ffcc6f 4213 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
00dc6358 4214
9ed182dc 4215 ancestor = namespace_ancestor (current_decl_namespace (), used);
00dc6358 4216 ud = current_binding_level->using_directives;
e1b3e07d 4217 ud = tree_cons (used, ancestor, ud);
ea9635c7 4218 current_binding_level->using_directives = ud;
70f0e288
JM
4219
4220 /* Recursively add all namespaces used. */
4221 for (iter = DECL_NAMESPACE_USING (used); iter; iter = TREE_CHAIN (iter))
4222 push_using_directive (TREE_PURPOSE (iter));
4223
22ffcc6f 4224 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, ud);
ea9635c7
ML
4225}
4226
f181d4ae
MM
4227/* DECL is a FUNCTION_DECL for a non-member function, which may have
4228 other definitions already in place. We get around this by making
4229 the value of the identifier point to a list of all the things that
4230 want to be referenced by that name. It is then up to the users of
4231 that name to decide what to do with that list.
8d08fdba 4232
17aec3eb
RK
4233 DECL may also be a TEMPLATE_DECL, with a FUNCTION_DECL in its
4234 DECL_TEMPLATE_RESULT. It is dealt with the same way.
8d08fdba 4235
7bdbfa05
MM
4236 FLAGS is a bitwise-or of the following values:
4237 PUSH_LOCAL: Bind DECL in the current scope, rather than at
4238 namespace scope.
4239 PUSH_USING: DECL is being pushed as the result of a using
68642fb6 4240 declaration.
7bdbfa05 4241
8d08fdba
MS
4242 The value returned may be a previous declaration if we guessed wrong
4243 about what language DECL should belong to (C or C++). Otherwise,
4244 it's always DECL (and never something that's not a _DECL). */
e92cc029 4245
7bdbfa05 4246tree
11f6b451 4247push_overloaded_decl (tree decl, int flags)
8d08fdba 4248{
f181d4ae 4249 tree name = DECL_NAME (decl);
700f8a87 4250 tree old;
f181d4ae 4251 tree new_binding;
7bdbfa05 4252 int doing_global = (namespace_bindings_p () || !(flags & PUSH_LOCAL));
700f8a87 4253
22ffcc6f 4254 timevar_push (TV_NAME_LOOKUP);
700f8a87 4255 if (doing_global)
9f33663b 4256 old = namespace_binding (name, DECL_CONTEXT (decl));
700f8a87 4257 else
f181d4ae 4258 old = lookup_name_current_level (name);
8d08fdba 4259
700f8a87 4260 if (old)
8d08fdba 4261 {
e1cd6e56 4262 if (TREE_CODE (old) == TYPE_DECL && DECL_ARTIFICIAL (old))
8926095f 4263 {
700f8a87 4264 tree t = TREE_TYPE (old);
cdf5b885
MS
4265 if (IS_AGGR_TYPE (t) && warn_shadow
4266 && (! DECL_IN_SYSTEM_HEADER (decl)
4267 || ! DECL_IN_SYSTEM_HEADER (old)))
33bd39a2 4268 warning ("`%#D' hides constructor for `%#T'", decl, t);
5b605f68 4269 old = NULL_TREE;
8926095f 4270 }
700f8a87 4271 else if (is_overloaded_fn (old))
8d08fdba 4272 {
8d08fdba 4273 tree tmp;
68642fb6 4274
2c73f9f5 4275 for (tmp = old; tmp; tmp = OVL_NEXT (tmp))
7bdbfa05
MM
4276 {
4277 tree fn = OVL_CURRENT (tmp);
4278
4279 if (TREE_CODE (tmp) == OVERLOAD && OVL_USED (tmp)
4280 && !(flags & PUSH_USING)
4281 && compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
4282 TYPE_ARG_TYPES (TREE_TYPE (decl))))
33bd39a2 4283 error ("`%#D' conflicts with previous using declaration `%#D'",
7bdbfa05 4284 decl, fn);
68642fb6 4285
7bdbfa05 4286 if (duplicate_decls (decl, fn))
22ffcc6f 4287 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, fn);
7bdbfa05 4288 }
8d08fdba 4289 }
655dc6ee
JM
4290 else if (old == error_mark_node)
4291 /* Ignore the undefined symbol marker. */
4292 old = NULL_TREE;
e1cd6e56
MS
4293 else
4294 {
8251199e 4295 cp_error_at ("previous non-function declaration `%#D'", old);
33bd39a2 4296 error ("conflicts with function declaration `%#D'", decl);
22ffcc6f 4297 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
e1cd6e56 4298 }
8d08fdba 4299 }
7177d104 4300
700f8a87 4301 if (old || TREE_CODE (decl) == TEMPLATE_DECL)
8d08fdba 4302 {
2c73f9f5 4303 if (old && TREE_CODE (old) != OVERLOAD)
f181d4ae
MM
4304 new_binding = ovl_cons (decl, ovl_cons (old, NULL_TREE));
4305 else
4306 new_binding = ovl_cons (decl, old);
347d73d7
ML
4307 if (flags & PUSH_USING)
4308 OVL_USED (new_binding) = 1;
8d08fdba
MS
4309 }
4310 else
f181d4ae
MM
4311 /* NAME is not ambiguous. */
4312 new_binding = decl;
700f8a87
MS
4313
4314 if (doing_global)
f181d4ae 4315 set_namespace_binding (name, current_namespace, new_binding);
700f8a87 4316 else
f181d4ae
MM
4317 {
4318 /* We only create an OVERLOAD if there was a previous binding at
0580c9aa
ML
4319 this level, or if decl is a template. In the former case, we
4320 need to remove the old binding and replace it with the new
4321 binding. We must also run through the NAMES on the binding
4322 level where the name was bound to update the chain. */
4323
4324 if (TREE_CODE (new_binding) == OVERLOAD && old)
f181d4ae
MM
4325 {
4326 tree *d;
68642fb6 4327
147135cc 4328 for (d = &IDENTIFIER_BINDING (name)->scope->names;
f181d4ae
MM
4329 *d;
4330 d = &TREE_CHAIN (*d))
4331 if (*d == old
4332 || (TREE_CODE (*d) == TREE_LIST
4333 && TREE_VALUE (*d) == old))
4334 {
d8f8dca1
MM
4335 if (TREE_CODE (*d) == TREE_LIST)
4336 /* Just replace the old binding with the new. */
4337 TREE_VALUE (*d) = new_binding;
4338 else
4339 /* Build a TREE_LIST to wrap the OVERLOAD. */
68642fb6 4340 *d = tree_cons (NULL_TREE, new_binding,
ed5511d9 4341 TREE_CHAIN (*d));
d8f8dca1 4342
af6fd53f 4343 /* And update the cxx_binding node. */
147135cc 4344 IDENTIFIER_BINDING (name)->value = new_binding;
22ffcc6f 4345 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
f181d4ae
MM
4346 }
4347
d8f8dca1 4348 /* We should always find a previous binding in this case. */
a98facb0 4349 abort ();
f181d4ae
MM
4350 }
4351
4352 /* Install the new binding. */
0034cf72 4353 push_local_binding (name, new_binding, flags);
f181d4ae 4354 }
700f8a87 4355
22ffcc6f 4356 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
8d08fdba
MS
4357}
4358\f
4359/* Generate an implicit declaration for identifier FUNCTIONID
4360 as a function of type int (). Print a warning if appropriate. */
4361
4362tree
11f6b451 4363implicitly_declare (tree functionid)
8d08fdba
MS
4364{
4365 register tree decl;
8d08fdba
MS
4366
4367 /* We used to reuse an old implicit decl here,
4368 but this loses with inline functions because it can clobber
4369 the saved decl chains. */
4370 decl = build_lang_decl (FUNCTION_DECL, functionid, default_function_type);
4371
4372 DECL_EXTERNAL (decl) = 1;
4373 TREE_PUBLIC (decl) = 1;
4374
cab1f180 4375 /* ISO standard says implicit declarations are in the innermost block.
d22c8596 4376 So we record the decl in the standard fashion. */
8d08fdba 4377 pushdecl (decl);
3e411c3f 4378 rest_of_decl_compilation (decl, NULL, 0, 0);
8d08fdba
MS
4379
4380 if (warn_implicit
4381 /* Only one warning per identifier. */
4382 && IDENTIFIER_IMPLICIT_DECL (functionid) == NULL_TREE)
4383 {
33bd39a2 4384 pedwarn ("implicit declaration of function `%#D'", decl);
8d08fdba
MS
4385 }
4386
4387 SET_IDENTIFIER_IMPLICIT_DECL (functionid, decl);
4388
8d08fdba
MS
4389 return decl;
4390}
4391
8251199e 4392/* Return zero if the declaration NEWDECL is valid
8d08fdba
MS
4393 when the declaration OLDDECL (assumed to be for the same name)
4394 has already been seen.
4395 Otherwise return an error message format string with a %s
4396 where the identifier should go. */
4397
d8e178a0 4398static const char *
11f6b451 4399redeclaration_error_message (tree newdecl, tree olddecl)
8d08fdba
MS
4400{
4401 if (TREE_CODE (newdecl) == TYPE_DECL)
4402 {
4403 /* Because C++ can put things into name space for free,
4404 constructs like "typedef struct foo { ... } foo"
4405 would look like an erroneous redeclaration. */
3bfdc719 4406 if (same_type_p (TREE_TYPE (newdecl), TREE_TYPE (olddecl)))
8251199e 4407 return 0;
8d08fdba 4408 else
8251199e 4409 return "redefinition of `%#D'";
8d08fdba
MS
4410 }
4411 else if (TREE_CODE (newdecl) == FUNCTION_DECL)
4412 {
4413 /* If this is a pure function, its olddecl will actually be
4414 the original initialization to `0' (which we force to call
4415 abort()). Don't complain about redefinition in this case. */
fee7654e 4416 if (DECL_LANG_SPECIFIC (olddecl) && DECL_PURE_VIRTUAL_P (olddecl))
8251199e 4417 return 0;
8d08fdba 4418
2c73f9f5 4419 /* If both functions come from different namespaces, this is not
a1c65f9f 4420 a redeclaration - this is a conflict with a used function. */
6eb3bb27 4421 if (DECL_NAMESPACE_SCOPE_P (olddecl)
2c73f9f5 4422 && DECL_CONTEXT (olddecl) != DECL_CONTEXT (newdecl))
8251199e 4423 return "`%D' conflicts with used function";
2c73f9f5 4424
db5ae43f
MS
4425 /* We'll complain about linkage mismatches in
4426 warn_extern_redeclared_static. */
4427
2c73f9f5 4428 /* Defining the same name twice is no good. */
8d08fdba 4429 if (DECL_INITIAL (olddecl) != NULL_TREE
faae18ab 4430 && DECL_INITIAL (newdecl) != NULL_TREE)
8d08fdba
MS
4431 {
4432 if (DECL_NAME (olddecl) == NULL_TREE)
8251199e 4433 return "`%#D' not declared in class";
8d08fdba 4434 else
8251199e 4435 return "redefinition of `%#D'";
8d08fdba 4436 }
8251199e 4437 return 0;
8d08fdba 4438 }
51c184be
MS
4439 else if (TREE_CODE (newdecl) == TEMPLATE_DECL)
4440 {
ec255269 4441 if ((TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == FUNCTION_DECL
5362b086 4442 && (DECL_TEMPLATE_RESULT (newdecl)
f4381d62 4443 != DECL_TEMPLATE_RESULT (olddecl))
ec255269
MS
4444 && DECL_INITIAL (DECL_TEMPLATE_RESULT (newdecl))
4445 && DECL_INITIAL (DECL_TEMPLATE_RESULT (olddecl)))
4446 || (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL
d0f062fb
NS
4447 && COMPLETE_TYPE_P (TREE_TYPE (newdecl))
4448 && COMPLETE_TYPE_P (TREE_TYPE (olddecl))))
8251199e
JM
4449 return "redefinition of `%#D'";
4450 return 0;
51c184be 4451 }
1f51a992 4452 else if (toplevel_bindings_p () || DECL_NAMESPACE_SCOPE_P (newdecl))
8d08fdba
MS
4453 {
4454 /* Objects declared at top level: */
4455 /* If at least one is a reference, it's ok. */
4456 if (DECL_EXTERNAL (newdecl) || DECL_EXTERNAL (olddecl))
8251199e 4457 return 0;
8926095f 4458 /* Reject two definitions. */
8251199e 4459 return "redefinition of `%#D'";
8d08fdba
MS
4460 }
4461 else
4462 {
4463 /* Objects declared with block scope: */
4464 /* Reject two definitions, and reject a definition
4465 together with an external reference. */
4466 if (!(DECL_EXTERNAL (newdecl) && DECL_EXTERNAL (olddecl)))
8251199e
JM
4467 return "redeclaration of `%#D'";
4468 return 0;
8d08fdba
MS
4469 }
4470}
4471\f
acef433b 4472/* Create a new label, named ID. */
8d08fdba 4473
acef433b 4474static tree
11f6b451 4475make_label_decl (tree id, int local_p)
8d08fdba 4476{
acef433b 4477 tree decl;
8d08fdba 4478
acef433b 4479 decl = build_decl (LABEL_DECL, id, void_type_node);
acef433b
MM
4480
4481 DECL_CONTEXT (decl) = current_function_decl;
4482 DECL_MODE (decl) = VOIDmode;
4483 C_DECLARED_LABEL_FLAG (decl) = local_p;
8d08fdba 4484
acef433b
MM
4485 /* Say where one reference is to the label, for the sake of the
4486 error if it is not defined. */
f31686a3 4487 DECL_SOURCE_LOCATION (decl) = input_location;
acef433b
MM
4488
4489 /* Record the fact that this identifier is bound to this label. */
4490 SET_IDENTIFIER_LABEL_VALUE (id, decl);
4491
6625cdb5
JM
4492 return decl;
4493}
4494
4495/* Record this label on the list of used labels so that we can check
4496 at the end of the function to see whether or not the label was
4497 actually defined, and so we can check when the label is defined whether
4498 this use is valid. */
4499
4500static void
11f6b451 4501use_label (tree decl)
6625cdb5
JM
4502{
4503 if (named_label_uses == NULL
4504 || named_label_uses->names_in_scope != current_binding_level->names
4505 || named_label_uses->label_decl != decl)
4506 {
4507 struct named_label_use_list *new_ent;
c68b0a84 4508 new_ent = ggc_alloc (sizeof (struct named_label_use_list));
e349ee73
MS
4509 new_ent->label_decl = decl;
4510 new_ent->names_in_scope = current_binding_level->names;
4511 new_ent->binding_level = current_binding_level;
82a98427 4512 new_ent->o_goto_locus = input_location;
e349ee73
MS
4513 new_ent->next = named_label_uses;
4514 named_label_uses = new_ent;
8d08fdba 4515 }
acef433b 4516}
8d08fdba 4517
acef433b
MM
4518/* Look for a label named ID in the current function. If one cannot
4519 be found, create one. (We keep track of used, but undefined,
4520 labels, and complain about them at the end of a function.) */
8d08fdba 4521
68642fb6 4522tree
11f6b451 4523lookup_label (tree id)
acef433b
MM
4524{
4525 tree decl;
6625cdb5 4526 struct named_label_list *ent;
8d08fdba 4527
22ffcc6f 4528 timevar_push (TV_NAME_LOOKUP);
acef433b
MM
4529 /* You can't use labels at global scope. */
4530 if (current_function_decl == NULL_TREE)
4531 {
4532 error ("label `%s' referenced outside of any function",
4533 IDENTIFIER_POINTER (id));
22ffcc6f 4534 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
acef433b 4535 }
68642fb6 4536
acef433b
MM
4537 /* See if we've already got this label. */
4538 decl = IDENTIFIER_LABEL_VALUE (id);
4539 if (decl != NULL_TREE && DECL_CONTEXT (decl) == current_function_decl)
22ffcc6f 4540 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
8d08fdba 4541
acef433b
MM
4542 /* Record this label on the list of labels used in this function.
4543 We do this before calling make_label_decl so that we get the
4544 IDENTIFIER_LABEL_VALUE before the new label is declared. */
c68b0a84 4545 ent = ggc_alloc_cleared (sizeof (struct named_label_list));
6625cdb5
JM
4546 ent->old_value = IDENTIFIER_LABEL_VALUE (id);
4547 ent->next = named_labels;
4548 named_labels = ent;
4549
acef433b
MM
4550 /* We need a new label. */
4551 decl = make_label_decl (id, /*local_p=*/0);
6625cdb5 4552
acef433b 4553 /* Now fill in the information we didn't have before. */
6625cdb5 4554 ent->label_decl = decl;
8d08fdba 4555
22ffcc6f 4556 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
8d08fdba
MS
4557}
4558
acef433b 4559/* Declare a local label named ID. */
8d08fdba
MS
4560
4561tree
11f6b451 4562declare_local_label (tree id)
8d08fdba 4563{
acef433b 4564 tree decl;
8d08fdba 4565
acef433b
MM
4566 /* Add a new entry to the SHADOWED_LABELS list so that when we leave
4567 this scope we can restore the old value of
4568 IDENTIFIER_TYPE_VALUE. */
68642fb6 4569 current_binding_level->shadowed_labels
acef433b
MM
4570 = tree_cons (IDENTIFIER_LABEL_VALUE (id), NULL_TREE,
4571 current_binding_level->shadowed_labels);
4572 /* Look for the label. */
4573 decl = make_label_decl (id, /*local_p=*/1);
4574 /* Now fill in the information we didn't have before. */
4575 TREE_VALUE (current_binding_level->shadowed_labels) = decl;
68642fb6 4576
acef433b 4577 return decl;
8d08fdba
MS
4578}
4579
6625cdb5
JM
4580/* Returns nonzero if it is ill-formed to jump past the declaration of
4581 DECL. Returns 2 if it's also a real problem. */
4582
4583static int
11f6b451 4584decl_jump_unsafe (tree decl)
6625cdb5
JM
4585{
4586 if (TREE_CODE (decl) != VAR_DECL || TREE_STATIC (decl))
4587 return 0;
4588
4589 if (DECL_INITIAL (decl) == NULL_TREE
4590 && pod_type_p (TREE_TYPE (decl)))
4591 return 0;
4592
4593 /* This is really only important if we're crossing an initialization.
4594 The POD stuff is just pedantry; why should it matter if the class
4595 contains a field of pointer to member type? */
4596 if (DECL_INITIAL (decl)
4597 || (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl))))
4598 return 2;
4599 return 1;
4600}
4601
4602/* Check that a single previously seen jump to a newly defined label
4603 is OK. DECL is the LABEL_DECL or 0; LEVEL is the binding_level for
4604 the jump context; NAMES are the names in scope in LEVEL at the jump
4605 context; FILE and LINE are the source position of the jump or 0. */
4606
4607static void
11f6b451
NN
4608check_previous_goto_1 (tree decl,
4609 struct cp_binding_level* level,
152c16a9 4610 tree names, const location_t *locus)
6625cdb5
JM
4611{
4612 int identified = 0;
4613 int saw_eh = 0;
e2500fed 4614 struct cp_binding_level *b = current_binding_level;
6625cdb5
JM
4615 for (; b; b = b->level_chain)
4616 {
4617 tree new_decls = b->names;
4618 tree old_decls = (b == level ? names : NULL_TREE);
4619 for (; new_decls != old_decls;
4620 new_decls = TREE_CHAIN (new_decls))
4621 {
4622 int problem = decl_jump_unsafe (new_decls);
4623 if (! problem)
4624 continue;
4625
4626 if (! identified)
4627 {
4628 if (decl)
33bd39a2 4629 pedwarn ("jump to label `%D'", decl);
6625cdb5
JM
4630 else
4631 pedwarn ("jump to case label");
4632
152c16a9
GDR
4633 if (locus)
4634 pedwarn ("%H from here", locus);
6625cdb5
JM
4635 identified = 1;
4636 }
4637
826840d9 4638 if (problem > 1)
6625cdb5
JM
4639 cp_error_at (" crosses initialization of `%#D'",
4640 new_decls);
4641 else
4642 cp_pedwarn_at (" enters scope of non-POD `%#D'",
4643 new_decls);
4644 }
4645
4646 if (b == level)
4647 break;
a7e8c268 4648 if ((b->kind == sk_try || b->kind == sk_catch) && ! saw_eh)
6625cdb5
JM
4649 {
4650 if (! identified)
4651 {
4652 if (decl)
33bd39a2 4653 pedwarn ("jump to label `%D'", decl);
6625cdb5
JM
4654 else
4655 pedwarn ("jump to case label");
4656
152c16a9
GDR
4657 if (locus)
4658 pedwarn ("%H from here", locus);
6625cdb5
JM
4659 identified = 1;
4660 }
a7e8c268 4661 if (b->kind == sk_try)
826840d9
RH
4662 error (" enters try block");
4663 else
4664 error (" enters catch block");
6625cdb5
JM
4665 saw_eh = 1;
4666 }
4667 }
4668}
4669
4670static void
11f6b451 4671check_previous_goto (struct named_label_use_list* use)
6625cdb5
JM
4672{
4673 check_previous_goto_1 (use->label_decl, use->binding_level,
152c16a9 4674 use->names_in_scope, &use->o_goto_locus);
6625cdb5
JM
4675}
4676
4677static void
11f6b451 4678check_switch_goto (struct cp_binding_level* level)
6625cdb5 4679{
152c16a9 4680 check_previous_goto_1 (NULL_TREE, level, level->names, NULL);
6625cdb5
JM
4681}
4682
4683/* Check that any previously seen jumps to a newly defined label DECL
4684 are OK. Called by define_label. */
4685
4686static void
11f6b451 4687check_previous_gotos (tree decl)
6625cdb5
JM
4688{
4689 struct named_label_use_list **usep;
4690
4691 if (! TREE_USED (decl))
4692 return;
4693
4694 for (usep = &named_label_uses; *usep; )
4695 {
4696 struct named_label_use_list *use = *usep;
4697 if (use->label_decl == decl)
4698 {
4699 check_previous_goto (use);
4700 *usep = use->next;
4701 }
4702 else
4703 usep = &(use->next);
4704 }
4705}
4706
4707/* Check that a new jump to a label DECL is OK. Called by
4708 finish_goto_stmt. */
4709
4710void
11f6b451 4711check_goto (tree decl)
6625cdb5
JM
4712{
4713 int identified = 0;
4714 tree bad;
4715 struct named_label_list *lab;
4716
e3cd9945
APB
4717 /* We can't know where a computed goto is jumping. So we assume
4718 that it's OK. */
4719 if (! DECL_P (decl))
4720 return;
4721
6625cdb5
JM
4722 /* If the label hasn't been defined yet, defer checking. */
4723 if (! DECL_INITIAL (decl))
4724 {
4725 use_label (decl);
4726 return;
4727 }
4728
4729 for (lab = named_labels; lab; lab = lab->next)
4730 if (decl == lab->label_decl)
4731 break;
4732
4733 /* If the label is not on named_labels it's a gcc local label, so
4734 it must be in an outer scope, so jumping to it is always OK. */
4735 if (lab == 0)
4736 return;
4737
826840d9
RH
4738 if ((lab->in_try_scope || lab->in_catch_scope || lab->bad_decls)
4739 && !identified)
6625cdb5
JM
4740 {
4741 cp_pedwarn_at ("jump to label `%D'", decl);
4742 pedwarn (" from here");
4743 identified = 1;
4744 }
4745
4746 for (bad = lab->bad_decls; bad; bad = TREE_CHAIN (bad))
4747 {
4748 tree b = TREE_VALUE (bad);
4749 int u = decl_jump_unsafe (b);
4750
4751 if (u > 1 && DECL_ARTIFICIAL (b))
4752 /* Can't skip init of __exception_info. */
ddd2d57e 4753 error ("%J enters catch block", b);
6625cdb5
JM
4754 else if (u > 1)
4755 cp_error_at (" skips initialization of `%#D'", b);
4756 else
4757 cp_pedwarn_at (" enters scope of non-POD `%#D'", b);
4758 }
4759
826840d9 4760 if (lab->in_try_scope)
6625cdb5 4761 error (" enters try block");
826840d9
RH
4762 else if (lab->in_catch_scope)
4763 error (" enters catch block");
6625cdb5
JM
4764}
4765
8d08fdba 4766/* Define a label, specifying the location in the source file.
b9c87401 4767 Return the LABEL_DECL node for the label. */
8d08fdba
MS
4768
4769tree
5b030314 4770define_label (location_t location, tree name)
8d08fdba 4771{
f01b0acb 4772 tree decl = lookup_label (name);
6625cdb5 4773 struct named_label_list *ent;
e2500fed 4774 register struct cp_binding_level *p;
6625cdb5 4775
22ffcc6f 4776 timevar_push (TV_NAME_LOOKUP);
6625cdb5
JM
4777 for (ent = named_labels; ent; ent = ent->next)
4778 if (ent->label_decl == decl)
4779 break;
8d08fdba 4780
3dc9aec6 4781 /* After labels, make any new cleanups in the function go into their
8d08fdba 4782 own new (temporary) binding contour. */
a7e8c268
MM
4783 for (p = current_binding_level;
4784 p->kind != sk_function_parms;
4785 p = p->level_chain)
3dc9aec6 4786 p->more_cleanups_ok = 0;
8d08fdba 4787
e1cd6e56 4788 if (name == get_identifier ("wchar_t"))
33bd39a2 4789 pedwarn ("label named wchar_t");
e1cd6e56 4790
8d08fdba 4791 if (DECL_INITIAL (decl) != NULL_TREE)
b9c87401 4792 error ("duplicate label `%D'", decl);
8d08fdba
MS
4793 else
4794 {
8d08fdba
MS
4795 /* Mark label as having been defined. */
4796 DECL_INITIAL (decl) = error_mark_node;
4797 /* Say where in the source. */
f31686a3 4798 DECL_SOURCE_LOCATION (decl) = location;
6625cdb5
JM
4799 if (ent)
4800 {
4801 ent->names_in_scope = current_binding_level->names;
4802 ent->binding_level = current_binding_level;
4803 }
4804 check_previous_gotos (decl);
8d08fdba 4805 }
b9c87401 4806
22ffcc6f 4807 timevar_pop (TV_NAME_LOOKUP);
b9c87401 4808 return decl;
8d08fdba
MS
4809}
4810
a5894242
MS
4811struct cp_switch
4812{
e2500fed 4813 struct cp_binding_level *level;
a5894242 4814 struct cp_switch *next;
56cb9733
MM
4815 /* The SWITCH_STMT being built. */
4816 tree switch_stmt;
4817 /* A splay-tree mapping the low element of a case range to the high
4818 element, or NULL_TREE if there is no high element. Used to
4819 determine whether or not a new case label duplicates an old case
4820 label. We need a tree, rather than simply a hash table, because
4821 of the GNU case range extension. */
4822 splay_tree cases;
a5894242
MS
4823};
4824
56cb9733
MM
4825/* A stack of the currently active switch statements. The innermost
4826 switch statement is on the top of the stack. There is no need to
4827 mark the stack for garbage collection because it is only active
4828 during the processing of the body of a function, and we never
4829 collect at that point. */
5362b086 4830
a5894242
MS
4831static struct cp_switch *switch_stack;
4832
56cb9733
MM
4833/* Called right after a switch-statement condition is parsed.
4834 SWITCH_STMT is the switch statement being parsed. */
4835
a5894242 4836void
11f6b451 4837push_switch (tree switch_stmt)
a5894242 4838{
c68b0a84 4839 struct cp_switch *p = xmalloc (sizeof (struct cp_switch));
a5894242
MS
4840 p->level = current_binding_level;
4841 p->next = switch_stack;
56cb9733
MM
4842 p->switch_stmt = switch_stmt;
4843 p->cases = splay_tree_new (case_compare, NULL, NULL);
a5894242
MS
4844 switch_stack = p;
4845}
4846
4847void
11f6b451 4848pop_switch (void)
a5894242 4849{
bedda2da 4850 struct cp_switch *cs;
5362b086 4851
bedda2da 4852 cs = switch_stack;
56cb9733 4853 splay_tree_delete (cs->cases);
a5894242 4854 switch_stack = switch_stack->next;
bedda2da 4855 free (cs);
a5894242
MS
4856}
4857
b0a1da19
JM
4858/* Note that we've seen a definition of a case label, and complain if this
4859 is a bad place for one. */
e92cc029 4860
3e4d04a1 4861tree
11f6b451 4862finish_case_label (tree low_value, tree high_value)
8d08fdba 4863{
3e4d04a1 4864 tree cond, r;
e2500fed 4865 register struct cp_binding_level *p;
a5894242 4866
b0a1da19 4867 if (! switch_stack)
56cb9733
MM
4868 {
4869 if (high_value)
4870 error ("case label not within a switch statement");
4871 else if (low_value)
33bd39a2 4872 error ("case label `%E' not within a switch statement",
56cb9733
MM
4873 low_value);
4874 else
4875 error ("`default' label not within a switch statement");
3e4d04a1 4876 return NULL_TREE;
56cb9733
MM
4877 }
4878
56cb9733
MM
4879 if (processing_template_decl)
4880 {
8f17b5c5
MM
4881 tree label;
4882
56cb9733
MM
4883 /* For templates, just add the case label; we'll do semantic
4884 analysis at instantiation-time. */
8f17b5c5 4885 label = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE);
3e4d04a1 4886 return add_stmt (build_case_label (low_value, high_value, label));
56cb9733
MM
4887 }
4888
4889 /* Find the condition on which this switch statement depends. */
4890 cond = SWITCH_COND (switch_stack->switch_stmt);
4891 if (cond && TREE_CODE (cond) == TREE_LIST)
4892 cond = TREE_VALUE (cond);
56cb9733 4893
3e4d04a1 4894 r = c_add_case_label (switch_stack->cases, cond, low_value, high_value);
8d08fdba 4895
6625cdb5 4896 check_switch_goto (switch_stack->level);
a5894242 4897
3dc9aec6 4898 /* After labels, make any new cleanups in the function go into their
8d08fdba 4899 own new (temporary) binding contour. */
a7e8c268
MM
4900 for (p = current_binding_level;
4901 p->kind != sk_function_parms;
4902 p = p->level_chain)
3dc9aec6 4903 p->more_cleanups_ok = 0;
3e4d04a1
RH
4904
4905 return r;
8d08fdba
MS
4906}
4907\f
4908/* Return the list of declarations of the current level.
4909 Note that this list is in reverse order unless/until
4910 you nreverse it; and when you do nreverse it, you must
4911 store the result back using `storedecls' or you will lose. */
4912
4913tree
11f6b451 4914getdecls (void)
8d08fdba
MS
4915{
4916 return current_binding_level->names;
4917}
4918
8d08fdba
MS
4919/* Store the list of declarations of the current level.
4920 This is done for the parameter declarations of a function being defined,
4921 after they are modified in the light of any missing parameters. */
4922
4923static void
11f6b451 4924storedecls (tree decls)
8d08fdba
MS
4925{
4926 current_binding_level->names = decls;
4927}
4928
5e0c54e5
GDR
4929/* Set the current binding TABLE for type declarations.. This is a
4930 temporary workaround of the fact that the data structure classtypes
4931 does not currently carry its allocated cxx_scope structure. */
280f9385 4932void
5e0c54e5 4933cxx_remember_type_decls (binding_table table)
8d08fdba 4934{
5e0c54e5 4935 current_binding_level->type_decls = table;
8d08fdba
MS
4936}
4937\f
4782bd5b
RS
4938/* Return the type that should be used when TYPE's name is preceded
4939 by a tag such as 'struct' or 'union', or null if the name cannot
4940 be used in this way.
4941
4942 For example, when processing the third line of:
4943
4944 struct A;
4945 typedef struct A A;
4946 struct A;
4947
4948 lookup of A will find the typedef. Given A's typedef, this function
4949 will return the type associated with "struct A". For the tag to be
4950 anything other than TYPE, TYPE must be a typedef whose original type
4951 has the same name and context as TYPE itself.
4952
4953 It is not valid for a typedef of an anonymous type to be used with
4954 an explicit tag:
4955
4956 typedef struct { ... } B;
4957 struct B;
4958
4959 Return null for this case. */
4960
4961static tree
11f6b451 4962follow_tag_typedef (tree type)
4782bd5b
RS
4963{
4964 tree original;
4965
4966 original = original_type (type);
3a73bffb
JJ
4967 if (! TYPE_NAME (original))
4968 return NULL_TREE;
4782bd5b
RS
4969 if (TYPE_IDENTIFIER (original) == TYPE_IDENTIFIER (type)
4970 && (CP_DECL_CONTEXT (TYPE_NAME (original))
4971 == CP_DECL_CONTEXT (TYPE_NAME (type)))
4972 && !(CLASS_TYPE_P (original) && TYPE_WAS_ANONYMOUS (original)))
4973 return original;
4974 else
4975 return NULL_TREE;
4976}
4977
8d08fdba
MS
4978/* Given NAME, an IDENTIFIER_NODE,
4979 return the structure (or union or enum) definition for that name.
147135cc 4980 Searches binding levels from its SCOPE up to the global level.
8d08fdba 4981 If THISLEVEL_ONLY is nonzero, searches only the specified context
ac20c67a 4982 (but skips any sk_cleanup contexts to find one that is
8d08fdba
MS
4983 meaningful for tags).
4984 FORM says which kind of type the caller wants;
4985 it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
4986 If the wrong kind of type is found, and it's not a template, an error is
4987 reported. */
4988
4989static tree
af6fd53f
GDR
4990lookup_tag (enum tree_code form, tree name,
4991 struct cp_binding_level* binding_level, int thislevel_only)
8d08fdba 4992{
e2500fed 4993 register struct cp_binding_level *level;
838dfd8a 4994 /* Nonzero if, we should look past a template parameter level, even
74b846e0
MM
4995 if THISLEVEL_ONLY. */
4996 int allow_template_parms_p = 1;
5e0c54e5 4997 bool type_is_anonymous = ANON_AGGRNAME_P (name);
8d08fdba 4998
22ffcc6f 4999 timevar_push (TV_NAME_LOOKUP);
8d08fdba
MS
5000 for (level = binding_level; level; level = level->level_chain)
5001 {
5002 register tree tail;
5e0c54e5
GDR
5003 if (type_is_anonymous && level->type_decls != NULL)
5004 {
5005 tree type = binding_table_find_anon_type (level->type_decls, name);
5006 /* There is no need for error checking here, because
5007 anon names are unique throughout the compilation. */
5008 if (type != NULL)
5009 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, type);
5010 }
a7e8c268 5011 else if (level->kind == sk_namespace)
a1c65f9f 5012 /* Do namespace lookup. */
6c011b01 5013 for (tail = current_namespace; 1; tail = CP_DECL_CONTEXT (tail))
2c73f9f5 5014 {
af6fd53f 5015 cxx_binding *binding =
ed3cf953 5016 cxx_scope_find_binding_for_name (NAMESPACE_LEVEL (tail), name);
af6fd53f 5017 tree old;
36a117a5 5018
74b846e0
MM
5019 /* If we just skipped past a template parameter level,
5020 even though THISLEVEL_ONLY, and we find a template
5021 class declaration, then we use the _TYPE node for the
36a117a5 5022 template. See the example below. */
74b846e0 5023 if (thislevel_only && !allow_template_parms_p
147135cc
GDR
5024 && binding && binding->value
5025 && DECL_CLASS_TEMPLATE_P (binding->value))
5026 old = binding->value;
af6fd53f 5027 else if (binding)
4b0d3cbe 5028 old = select_decl (binding, LOOKUP_PREFER_TYPES);
af6fd53f
GDR
5029 else
5030 old = NULL_TREE;
36a117a5 5031
4782bd5b 5032 if (old)
2c73f9f5 5033 {
4782bd5b
RS
5034 /* We've found something at this binding level. If it is
5035 a typedef, extract the tag it refers to. Lookup fails
5036 if the typedef doesn't refer to a taggable type. */
4b0d3cbe 5037 old = TREE_TYPE (old);
4782bd5b
RS
5038 old = follow_tag_typedef (old);
5039 if (!old)
22ffcc6f 5040 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
4782bd5b
RS
5041 if (TREE_CODE (old) != form
5042 && (form == ENUMERAL_TYPE
5043 || TREE_CODE (old) == ENUMERAL_TYPE))
5044 {
5045 error ("`%#D' redeclared as %C", old, form);
22ffcc6f 5046 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
4782bd5b 5047 }
22ffcc6f 5048 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, old);
2c73f9f5 5049 }
2c73f9f5 5050 if (thislevel_only || tail == global_namespace)
22ffcc6f 5051 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
2c73f9f5 5052 }
5e0c54e5
GDR
5053 else if (level->type_decls != NULL)
5054 {
5055 binding_entry entry = binding_table_find (level->type_decls, name);
5056 if (entry != NULL)
5057 {
5058 enum tree_code code = TREE_CODE (entry->type);
fd87b7a7 5059
5e0c54e5
GDR
5060 if (code != form
5061 && (form == ENUMERAL_TYPE || code == ENUMERAL_TYPE))
5062 {
5063 /* Definition isn't the kind we were looking for. */
5064 error ("`%#D' redeclared as %C", entry->type, form);
5065 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
5066 }
5067 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, entry->type);
5068 }
8d08fdba 5069 }
ac20c67a 5070 if (thislevel_only && level->kind != sk_cleanup)
5566b478 5071 {
a7e8c268 5072 if (level->kind == sk_template_parms && allow_template_parms_p)
5566b478 5073 {
36a117a5 5074 /* We must deal with cases like this:
68642fb6 5075
36a117a5
MM
5076 template <class T> struct S;
5077 template <class T> struct S {};
68642fb6 5078
36a117a5
MM
5079 When looking up `S', for the second declaration, we
5080 would like to find the first declaration. But, we
5081 are in the pseudo-global level created for the
5082 template parameters, rather than the (surrounding)
5083 namespace level. Thus, we keep going one more level,
838dfd8a 5084 even though THISLEVEL_ONLY is nonzero. */
74b846e0 5085 allow_template_parms_p = 0;
36a117a5 5086 continue;
5566b478 5087 }
36a117a5 5088 else
22ffcc6f 5089 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
5566b478 5090 }
8d08fdba 5091 }
22ffcc6f 5092 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
8d08fdba
MS
5093}
5094
8d08fdba
MS
5095/* Given a type, find the tag that was defined for it and return the tag name.
5096 Otherwise return 0. However, the value can never be 0
5097 in the cases in which this is used.
5098
838dfd8a 5099 C++: If NAME is nonzero, this is the new name to install. This is
8d08fdba
MS
5100 done when replacing anonymous tags with real tag names. */
5101
5102static tree
11f6b451 5103lookup_tag_reverse (tree type, tree name)
8d08fdba 5104{
e2500fed 5105 register struct cp_binding_level *level;
8d08fdba 5106
22ffcc6f 5107 timevar_push (TV_NAME_LOOKUP);
8d08fdba
MS
5108 for (level = current_binding_level; level; level = level->level_chain)
5109 {
5e0c54e5
GDR
5110 binding_entry entry = level->type_decls == NULL
5111 ? NULL
5112 : binding_table_reverse_maybe_remap (level->type_decls, type, name);
5113 if (entry)
5114 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, entry->name);
8d08fdba 5115 }
22ffcc6f 5116 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
8d08fdba 5117}
8d08fdba 5118\f
a9aedbc2 5119/* Look up NAME in the NAMESPACE. */
e92cc029 5120
a9aedbc2 5121tree
11f6b451 5122lookup_namespace_name (tree namespace, tree name)
a9aedbc2 5123{
30394414 5124 tree val;
f30c84c9 5125 tree template_id = NULL_TREE;
af6fd53f 5126 cxx_binding binding;
2c73f9f5 5127
22ffcc6f 5128 timevar_push (TV_NAME_LOOKUP);
30394414 5129 my_friendly_assert (TREE_CODE (namespace) == NAMESPACE_DECL, 370);
1231fb96 5130
1231fb96 5131 if (TREE_CODE (name) == NAMESPACE_DECL)
a1c65f9f 5132 /* This happens for A::B<int> when B is a namespace. */
22ffcc6f 5133 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, name);
d8f8dca1
MM
5134 else if (TREE_CODE (name) == TEMPLATE_DECL)
5135 {
5136 /* This happens for A::B where B is a template, and there are no
5137 template arguments. */
33bd39a2 5138 error ("invalid use of `%D'", name);
22ffcc6f 5139 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
d8f8dca1 5140 }
1231fb96 5141
b262d64c
JM
5142 namespace = ORIGINAL_NAMESPACE (namespace);
5143
f30c84c9
MM
5144 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
5145 {
5146 template_id = name;
5147 name = TREE_OPERAND (name, 0);
5148 if (TREE_CODE (name) == OVERLOAD)
5149 name = DECL_NAME (OVL_CURRENT (name));
2f939d94 5150 else if (DECL_P (name))
f30c84c9
MM
5151 name = DECL_NAME (name);
5152 }
5153
30394414 5154 my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, 373);
68642fb6 5155
af6fd53f
GDR
5156 cxx_binding_clear (&binding);
5157 if (!qualified_lookup_using_namespace (name, namespace, &binding, 0))
22ffcc6f 5158 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
2c73f9f5 5159
af6fd53f 5160 if (binding.value)
1c35f5b6 5161 {
af6fd53f 5162 val = binding.value;
1c35f5b6 5163
f30c84c9
MM
5164 if (template_id)
5165 {
5166 if (DECL_CLASS_TEMPLATE_P (val))
68642fb6 5167 val = lookup_template_class (val,
f30c84c9
MM
5168 TREE_OPERAND (template_id, 1),
5169 /*in_decl=*/NULL_TREE,
5170 /*context=*/NULL_TREE,
f9c244b8 5171 /*entering_scope=*/0,
c2ea3a40 5172 tf_error | tf_warning);
f30c84c9
MM
5173 else if (DECL_FUNCTION_TEMPLATE_P (val)
5174 || TREE_CODE (val) == OVERLOAD)
68642fb6 5175 val = lookup_template_function (val,
f30c84c9
MM
5176 TREE_OPERAND (template_id, 1));
5177 else
5178 {
33bd39a2 5179 error ("`%D::%D' is not a template",
f30c84c9 5180 namespace, name);
22ffcc6f 5181 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
f30c84c9
MM
5182 }
5183 }
5184
1c35f5b6
JM
5185 /* If we have a single function from a using decl, pull it out. */
5186 if (TREE_CODE (val) == OVERLOAD && ! really_overloaded_fn (val))
5187 val = OVL_FUNCTION (val);
d52e4867
RS
5188
5189 /* Ignore built-in functions that haven't been prototyped yet. */
5190 if (!val || !DECL_P(val)
5191 || !DECL_LANG_SPECIFIC(val)
5192 || !DECL_ANTICIPATED (val))
22ffcc6f 5193 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val);
1c35f5b6
JM
5194 }
5195
33bd39a2 5196 error ("`%D' undeclared in namespace `%D'", name, namespace);
22ffcc6f 5197 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
a9aedbc2
MS
5198}
5199
7ddedda4
MM
5200/* Hash a TYPENAME_TYPE. K is really of type `tree'. */
5201
e2500fed 5202static hashval_t
11f6b451 5203typename_hash (const void* k)
7ddedda4 5204{
e2500fed
GK
5205 hashval_t hash;
5206 tree t = (tree) k;
7ddedda4 5207
7bdfd72e
KG
5208 hash = (htab_hash_pointer (TYPE_CONTEXT (t))
5209 ^ htab_hash_pointer (DECL_NAME (TYPE_NAME (t))));
7ddedda4
MM
5210
5211 return hash;
5212}
5213
5214/* Compare two TYPENAME_TYPEs. K1 and K2 are really of type `tree'. */
5215
e2500fed 5216static int
11f6b451 5217typename_compare (const void * k1, const void * k2)
7ddedda4
MM
5218{
5219 tree t1;
5220 tree t2;
5221 tree d1;
5222 tree d2;
5223
5224 t1 = (tree) k1;
5225 t2 = (tree) k2;
5226 d1 = TYPE_NAME (t1);
5227 d2 = TYPE_NAME (t2);
68642fb6 5228
7ddedda4 5229 return (DECL_NAME (d1) == DECL_NAME (d2)
e2500fed 5230 && TYPE_CONTEXT (t1) == TYPE_CONTEXT (t2)
68642fb6 5231 && ((TREE_TYPE (t1) != NULL_TREE)
7ddedda4
MM
5232 == (TREE_TYPE (t2) != NULL_TREE))
5233 && same_type_p (TREE_TYPE (t1), TREE_TYPE (t2))
5234 && TYPENAME_TYPE_FULLNAME (t1) == TYPENAME_TYPE_FULLNAME (t2));
5235}
5236
45869a6c
MM
5237/* Build a TYPENAME_TYPE. If the type is `typename T::t', CONTEXT is
5238 the type of `T', NAME is the IDENTIFIER_NODE for `t'. If BASE_TYPE
5239 is non-NULL, this type is being created by the implicit typename
5240 extension, and BASE_TYPE is a type named `t' in some base class of
68642fb6 5241 `T' which depends on template parameters.
45869a6c
MM
5242
5243 Returns the new TYPENAME_TYPE. */
5244
e2500fed
GK
5245static GTY ((param_is (union tree_node))) htab_t typename_htab;
5246
8ce33230 5247static tree
8fbc5ae7 5248build_typename_type (tree context, tree name, tree fullname)
45869a6c
MM
5249{
5250 tree t;
5251 tree d;
fad205ff 5252 void **e;
45869a6c 5253
e2500fed 5254 if (typename_htab == NULL)
9cd64686 5255 {
e2500fed
GK
5256 typename_htab = htab_create_ggc (61, &typename_hash,
5257 &typename_compare, NULL);
9cd64686 5258 }
45869a6c
MM
5259
5260 /* Build the TYPENAME_TYPE. */
33848bb0 5261 t = make_aggr_type (TYPENAME_TYPE);
45869a6c
MM
5262 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
5263 TYPENAME_TYPE_FULLNAME (t) = fullname;
45869a6c
MM
5264
5265 /* Build the corresponding TYPE_DECL. */
5266 d = build_decl (TYPE_DECL, name, t);
5267 TYPE_NAME (TREE_TYPE (d)) = d;
5268 TYPE_STUB_DECL (TREE_TYPE (d)) = d;
5269 DECL_CONTEXT (d) = FROB_CONTEXT (context);
2642b9bf 5270 DECL_ARTIFICIAL (d) = 1;
45869a6c 5271
7ddedda4 5272 /* See if we already have this type. */
e2500fed
GK
5273 e = htab_find_slot (typename_htab, t, INSERT);
5274 if (*e)
5275 t = (tree) *e;
7ddedda4 5276 else
e2500fed 5277 *e = t;
7ddedda4 5278
45869a6c
MM
5279 return t;
5280}
5281
3baa501d
MM
5282/* Resolve `typename CONTEXT::NAME'. Returns an appropriate type,
5283 unless an error occurs, in which case error_mark_node is returned.
4f2b0fb2
NS
5284 If we locate a non-artificial TYPE_DECL and TF_KEEP_TYPE_DECL is
5285 set, we return that, rather than the _TYPE it corresponds to, in
5286 other cases we look through the type decl. If TF_ERROR is set,
5287 complain about errors, otherwise be quiet. */
3baa501d 5288
5566b478 5289tree
11f6b451 5290make_typename_type (tree context, tree name, tsubst_flags_t complain)
5566b478 5291{
b2b7d40a 5292 tree fullname;
a80e4195 5293
5dc5d13c
KL
5294 if (name == error_mark_node
5295 || context == NULL_TREE
5296 || context == error_mark_node)
5297 return error_mark_node;
5298
2f939d94 5299 if (TYPE_P (name))
78638e24 5300 {
68642fb6
UD
5301 if (!(TYPE_LANG_SPECIFIC (name)
5302 && (CLASSTYPE_IS_TEMPLATE (name)
78638e24
MM
5303 || CLASSTYPE_USE_TEMPLATE (name))))
5304 name = TYPE_IDENTIFIER (name);
5305 else
5306 /* Create a TEMPLATE_ID_EXPR for the type. */
5307 name = build_nt (TEMPLATE_ID_EXPR,
5308 CLASSTYPE_TI_TEMPLATE (name),
5309 CLASSTYPE_TI_ARGS (name));
5310 }
653cc74a 5311 else if (TREE_CODE (name) == TYPE_DECL)
a80e4195 5312 name = DECL_NAME (name);
b2b7d40a
JM
5313
5314 fullname = name;
5315
5316 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
11686454
JM
5317 {
5318 name = TREE_OPERAND (name, 0);
5319 if (TREE_CODE (name) == TEMPLATE_DECL)
5320 name = TREE_OPERAND (fullname, 0) = DECL_NAME (name);
5321 }
8a2b77e7
JM
5322 if (TREE_CODE (name) == TEMPLATE_DECL)
5323 {
33bd39a2 5324 error ("`%D' used without template parameters", name);
8a2b77e7
JM
5325 return error_mark_node;
5326 }
5d872564
NS
5327 my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, 20030802);
5328
04ddee1b
BK
5329 if (TREE_CODE (context) == NAMESPACE_DECL)
5330 {
5331 /* We can get here from typename_sub0 in the explicit_template_type
5332 expansion. Just fail. */
4f2b0fb2 5333 if (complain & tf_error)
33bd39a2 5334 error ("no class template named `%#T' in `%#T'",
3baa501d 5335 name, context);
04ddee1b
BK
5336 return error_mark_node;
5337 }
5338
85b71cf2 5339 if (! uses_template_parms (context)
b77ead33 5340 || currently_open_class (context))
5566b478 5341 {
b2b7d40a
JM
5342 if (TREE_CODE (fullname) == TEMPLATE_ID_EXPR)
5343 {
ad810b22 5344 tree tmpl = NULL_TREE;
b2b7d40a 5345 if (IS_AGGR_TYPE (context))
86ac0575 5346 tmpl = lookup_field (context, name, 0, false);
ad810b22 5347 if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl))
b2b7d40a 5348 {
4f2b0fb2 5349 if (complain & tf_error)
33bd39a2 5350 error ("no class template named `%#T' in `%#T'",
3baa501d 5351 name, context);
b2b7d40a
JM
5352 return error_mark_node;
5353 }
ffb690bd 5354
2b59fc25 5355 if (complain & tf_error)
6df5158a 5356 perform_or_defer_access_check (TYPE_BINFO (context), tmpl);
ba59df78 5357
68642fb6 5358 return lookup_template_class (tmpl,
ad810b22 5359 TREE_OPERAND (fullname, 1),
68642fb6 5360 NULL_TREE, context,
f9c244b8 5361 /*entering_scope=*/0,
42eaed49 5362 tf_error | tf_warning | tf_user);
b2b7d40a
JM
5363 }
5364 else
5566b478 5365 {
b4f70b3d 5366 tree t;
68642fb6 5367
b4f70b3d 5368 if (!IS_AGGR_TYPE (context))
b2b7d40a 5369 {
4f2b0fb2 5370 if (complain & tf_error)
33bd39a2 5371 error ("no type named `%#T' in `%#T'", name, context);
b2b7d40a
JM
5372 return error_mark_node;
5373 }
1107c4b3 5374
86ac0575 5375 t = lookup_field (context, name, 0, true);
7d4bdeed 5376 if (t)
4f2b0fb2 5377 {
8c081e84
MM
5378 if (TREE_CODE (t) != TYPE_DECL)
5379 {
5380 if (complain & tf_error)
5381 error ("no type named `%#T' in `%#T'", name, context);
5382 return error_mark_node;
5383 }
5384
2b59fc25 5385 if (complain & tf_error)
6df5158a 5386 perform_or_defer_access_check (TYPE_BINFO (context), t);
ba59df78 5387
4f2b0fb2
NS
5388 if (DECL_ARTIFICIAL (t) || !(complain & tf_keep_type_decl))
5389 t = TREE_TYPE (t);
ba3307c0 5390
4f2b0fb2
NS
5391 return t;
5392 }
5566b478 5393 }
5566b478 5394 }
11249cf0
MM
5395
5396 /* If the CONTEXT is not a template type, then either the field is
5397 there now or its never going to be. */
b4f70b3d 5398 if (!uses_template_parms (context))
11249cf0 5399 {
4f2b0fb2 5400 if (complain & tf_error)
33bd39a2 5401 error ("no type named `%#T' in `%#T'", name, context);
11249cf0
MM
5402 return error_mark_node;
5403 }
68642fb6 5404
8fbc5ae7 5405 return build_typename_type (context, name, fullname);
5566b478
MS
5406}
5407
b8c6534b
KL
5408/* Resolve `CONTEXT::template NAME'. Returns an appropriate type,
5409 unless an error occurs, in which case error_mark_node is returned.
4f2b0fb2
NS
5410 If we locate a TYPE_DECL, we return that, rather than the _TYPE it
5411 corresponds to. If COMPLAIN zero, don't complain about any errors
5412 that occur. */
b8c6534b
KL
5413
5414tree
11f6b451 5415make_unbound_class_template (tree context, tree name, tsubst_flags_t complain)
b8c6534b
KL
5416{
5417 tree t;
5418 tree d;
5419
5420 if (TYPE_P (name))
5421 name = TYPE_IDENTIFIER (name);
5422 else if (DECL_P (name))
5423 name = DECL_NAME (name);
5424 if (TREE_CODE (name) != IDENTIFIER_NODE)
a98facb0 5425 abort ();
b8c6534b
KL
5426
5427 if (!uses_template_parms (context)
5428 || currently_open_class (context))
5429 {
5430 tree tmpl = NULL_TREE;
5431
5432 if (IS_AGGR_TYPE (context))
86ac0575 5433 tmpl = lookup_field (context, name, 0, false);
b8c6534b
KL
5434
5435 if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl))
5436 {
4f09be91 5437 if (complain & tf_error)
33bd39a2 5438 error ("no class template named `%#T' in `%#T'", name, context);
b8c6534b
KL
5439 return error_mark_node;
5440 }
5441
2b59fc25 5442 if (complain & tf_error)
6df5158a 5443 perform_or_defer_access_check (TYPE_BINFO (context), tmpl);
b8c6534b
KL
5444
5445 return tmpl;
5446 }
5447
5448 /* Build the UNBOUND_CLASS_TEMPLATE. */
5449 t = make_aggr_type (UNBOUND_CLASS_TEMPLATE);
5450 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
5451 TREE_TYPE (t) = NULL_TREE;
5452
5453 /* Build the corresponding TEMPLATE_DECL. */
5454 d = build_decl (TEMPLATE_DECL, name, t);
5455 TYPE_NAME (TREE_TYPE (d)) = d;
5456 TYPE_STUB_DECL (TREE_TYPE (d)) = d;
5457 DECL_CONTEXT (d) = FROB_CONTEXT (context);
5458 DECL_ARTIFICIAL (d) = 1;
5459
5460 return t;
5461}
5462
a1c65f9f 5463/* Select the right _DECL from multiple choices. */
2c73f9f5
ML
5464
5465static tree
af6fd53f 5466select_decl (cxx_binding *binding, int flags)
2c73f9f5
ML
5467{
5468 tree val;
147135cc 5469 val = binding->value;
97ba1e3c 5470
22ffcc6f 5471 timevar_push (TV_NAME_LOOKUP);
52c11ef6 5472 if (LOOKUP_NAMESPACES_ONLY (flags))
3e3f722c 5473 {
a1c65f9f 5474 /* We are not interested in types. */
3e3f722c 5475 if (val && TREE_CODE (val) == NAMESPACE_DECL)
22ffcc6f
GDR
5476 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val);
5477 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
3e3f722c 5478 }
68642fb6 5479
4b0d3cbe
MM
5480 /* If looking for a type, or if there is no non-type binding, select
5481 the value binding. */
147135cc
GDR
5482 if (binding->type && (!val || (flags & LOOKUP_PREFER_TYPES)))
5483 val = binding->type;
a1c65f9f 5484 /* Don't return non-types if we really prefer types. */
52c11ef6 5485 else if (val && LOOKUP_TYPES_ONLY (flags) && TREE_CODE (val) != TYPE_DECL
c592d5d2
ML
5486 && (TREE_CODE (val) != TEMPLATE_DECL
5487 || !DECL_CLASS_TEMPLATE_P (val)))
2c73f9f5 5488 val = NULL_TREE;
1c35f5b6 5489
22ffcc6f 5490 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val);
2c73f9f5
ML
5491}
5492
2c169bab
JM
5493/* Unscoped lookup of a global: iterate over current namespaces,
5494 considering using-directives. If SPACESP is non-NULL, store a list
5495 of the namespaces we've considered in it. */
ea9635c7 5496
2c169bab 5497tree
11f6b451 5498unqualified_namespace_lookup (tree name, int flags, tree* spacesp)
ea9635c7 5499{
ee8fc32b 5500 tree initial = current_decl_namespace ();
ea9635c7
ML
5501 tree scope = initial;
5502 tree siter;
e2500fed 5503 struct cp_binding_level *level;
ea9635c7 5504 tree val = NULL_TREE;
af6fd53f 5505 cxx_binding binding;
ea9635c7 5506
22ffcc6f 5507 timevar_push (TV_NAME_LOOKUP);
af6fd53f 5508 cxx_binding_clear (&binding);
2c169bab
JM
5509 if (spacesp)
5510 *spacesp = NULL_TREE;
5511
5512 for (; !val; scope = CP_DECL_CONTEXT (scope))
ea9635c7 5513 {
ed3cf953
GDR
5514 cxx_binding *b =
5515 cxx_scope_find_binding_for_name (NAMESPACE_LEVEL (scope), name);
2c169bab 5516 if (spacesp)
e1b3e07d 5517 *spacesp = tree_cons (scope, NULL_TREE, *spacesp);
ea9635c7 5518
d52e4867 5519 /* Ignore anticipated built-in functions. */
147135cc
GDR
5520 if (b && b->value && DECL_P (b->value)
5521 && DECL_LANG_SPECIFIC (b->value) && DECL_ANTICIPATED (b->value))
af6fd53f
GDR
5522 /* Keep binding cleared. */;
5523 else if (b)
d52e4867 5524 {
a1c65f9f 5525 /* Initialize binding for this context. */
147135cc
GDR
5526 binding.value = b->value;
5527 binding.type = b->type;
d52e4867 5528 }
ea9635c7 5529
a1c65f9f 5530 /* Add all _DECLs seen through local using-directives. */
68642fb6 5531 for (level = current_binding_level;
a7e8c268 5532 level->kind != sk_namespace;
ea9635c7 5533 level = level->level_chain)
af6fd53f 5534 if (!lookup_using_namespace (name, &binding, level->using_directives,
2c169bab 5535 scope, flags, spacesp))
a1c65f9f 5536 /* Give up because of error. */
22ffcc6f 5537 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
ea9635c7 5538
a1c65f9f
KH
5539 /* Add all _DECLs seen through global using-directives. */
5540 /* XXX local and global using lists should work equally. */
ea9635c7
ML
5541 siter = initial;
5542 while (1)
5543 {
af6fd53f
GDR
5544 if (!lookup_using_namespace (name, &binding,
5545 DECL_NAMESPACE_USING (siter),
2c169bab 5546 scope, flags, spacesp))
a1c65f9f 5547 /* Give up because of error. */
22ffcc6f 5548 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
ea9635c7
ML
5549 if (siter == scope) break;
5550 siter = CP_DECL_CONTEXT (siter);
5551 }
5552
af6fd53f 5553 val = select_decl (&binding, flags);
ea9635c7
ML
5554 if (scope == global_namespace)
5555 break;
ea9635c7 5556 }
22ffcc6f 5557 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val);
ea9635c7
ML
5558}
5559
52c11ef6
JM
5560/* Combine prefer_type and namespaces_only into flags. */
5561
5562static int
11f6b451 5563lookup_flags (int prefer_type, int namespaces_only)
52c11ef6
JM
5564{
5565 if (namespaces_only)
5566 return LOOKUP_PREFER_NAMESPACES;
5567 if (prefer_type > 1)
5568 return LOOKUP_PREFER_TYPES;
5569 if (prefer_type > 0)
5570 return LOOKUP_PREFER_BOTH;
5571 return 0;
5572}
5573
5574/* Given a lookup that returned VAL, use FLAGS to decide if we want to
5575 ignore it or not. Subroutine of lookup_name_real. */
5576
5577static tree
11f6b451 5578qualify_lookup (tree val, int flags)
52c11ef6
JM
5579{
5580 if (val == NULL_TREE)
5581 return val;
6b945830
JM
5582 if ((flags & LOOKUP_PREFER_NAMESPACES) && TREE_CODE (val) == NAMESPACE_DECL)
5583 return val;
cbd63935
KL
5584 if ((flags & LOOKUP_PREFER_TYPES)
5585 && (TREE_CODE (val) == TYPE_DECL || TREE_CODE (val) == TEMPLATE_DECL))
6b945830
JM
5586 return val;
5587 if (flags & (LOOKUP_PREFER_NAMESPACES | LOOKUP_PREFER_TYPES))
52c11ef6
JM
5588 return NULL_TREE;
5589 return val;
5590}
5591
a723baf1
MM
5592/* Look up NAME (an IDENTIFIER_NODE) in SCOPE (either a NAMESPACE_DECL
5593 or a class TYPE). If IS_TYPE_P is TRUE, then ignore non-type
5594 bindings.
5595
5596 Returns a DECL (or OVERLOAD, or BASELINK) representing the
5e08432e
MM
5597 declaration found. If no suitable declaration can be found,
5598 ERROR_MARK_NODE is returned. Iif COMPLAIN is true and SCOPE is
5599 neither a class-type nor a namespace a diagnostic is issued. */
a723baf1
MM
5600
5601tree
5e08432e 5602lookup_qualified_name (tree scope, tree name, bool is_type_p, bool complain)
a723baf1 5603{
ee76b931
MM
5604 int flags = 0;
5605
a723baf1
MM
5606 if (TREE_CODE (scope) == NAMESPACE_DECL)
5607 {
af6fd53f 5608 cxx_binding binding;
a723baf1 5609
af6fd53f 5610 cxx_binding_clear (&binding);
a723baf1
MM
5611 flags |= LOOKUP_COMPLAIN;
5612 if (is_type_p)
5613 flags |= LOOKUP_PREFER_TYPES;
5e08432e
MM
5614 if (qualified_lookup_using_namespace (name, scope, &binding,
5615 flags))
5616 return select_decl (&binding, flags);
a723baf1 5617 }
5e08432e
MM
5618 else if (is_aggr_type (scope, complain))
5619 {
5620 tree t;
5621 t = lookup_member (scope, name, 0, is_type_p);
5622 if (t)
5623 return t;
5624 }
5625
5626 return error_mark_node;
a723baf1
MM
5627}
5628
5f261ba9
MM
5629/* Check to see whether or not DECL is a variable that would have been
5630 in scope under the ARM, but is not in scope under the ANSI/ISO
5631 standard. If so, issue an error message. If name lookup would
5632 work in both cases, but return a different result, this function
5633 returns the result of ANSI/ISO lookup. Otherwise, it returns
5634 DECL. */
5635
5636tree
5637check_for_out_of_scope_variable (tree decl)
5638{
5639 tree shadowed;
5640
5641 /* We only care about out of scope variables. */
5642 if (!(TREE_CODE (decl) == VAR_DECL && DECL_DEAD_FOR_LOCAL (decl)))
5643 return decl;
5644
5645 shadowed = DECL_SHADOWED_FOR_VAR (decl);
5646 while (shadowed != NULL_TREE && TREE_CODE (shadowed) == VAR_DECL
5647 && DECL_DEAD_FOR_LOCAL (shadowed))
5648 shadowed = DECL_SHADOWED_FOR_VAR (shadowed);
5649 if (!shadowed)
5650 shadowed = IDENTIFIER_NAMESPACE_VALUE (DECL_NAME (decl));
5651 if (shadowed)
5652 {
5653 if (!DECL_ERROR_REPORTED (decl))
5654 {
5655 warning ("name lookup of `%D' changed",
5656 DECL_NAME (decl));
5657 cp_warning_at (" matches this `%D' under ISO standard rules",
5658 shadowed);
5659 cp_warning_at (" matches this `%D' under old rules", decl);
5660 DECL_ERROR_REPORTED (decl) = 1;
5661 }
5662 return shadowed;
5663 }
5664
5665 /* If we have already complained about this declaration, there's no
5666 need to do it again. */
5667 if (DECL_ERROR_REPORTED (decl))
5668 return decl;
5669
5670 DECL_ERROR_REPORTED (decl) = 1;
5671 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
5672 {
5673 error ("name lookup of `%D' changed for new ISO `for' scoping",
5674 DECL_NAME (decl));
5675 cp_error_at (" cannot use obsolete binding at `%D' because it has a destructor", decl);
5676 return error_mark_node;
5677 }
5678 else
5679 {
5680 pedwarn ("name lookup of `%D' changed for new ISO `for' scoping",
5681 DECL_NAME (decl));
5682 cp_pedwarn_at (" using obsolete binding at `%D'", decl);
5683 }
5684
5685 return decl;
5686}
5687
8d08fdba
MS
5688/* Look up NAME in the current binding level and its superiors in the
5689 namespace of variables, functions and typedefs. Return a ..._DECL
5690 node of some kind representing its definition if there is only one
5691 such declaration, or return a TREE_LIST with all the overloaded
5692 definitions if there are many, or return 0 if it is undefined.
5693
2c73f9f5
ML
5694 If PREFER_TYPE is > 0, we prefer TYPE_DECLs or namespaces.
5695 If PREFER_TYPE is > 1, we reject non-type decls (e.g. namespaces).
68642fb6 5696 Otherwise we prefer non-TYPE_DECLs.
f84b4be9 5697
838dfd8a 5698 If NONCLASS is nonzero, we don't look for the NAME in class scope,
f84b4be9 5699 using IDENTIFIER_CLASS_VALUE. */
8d08fdba 5700
a723baf1 5701tree
af6fd53f
GDR
5702lookup_name_real (tree name, int prefer_type, int nonclass,
5703 int namespaces_only, int flags)
8d08fdba 5704{
af6fd53f 5705 cxx_binding *iter;
235f734d 5706 tree val = NULL_TREE;
8d08fdba 5707
22ffcc6f 5708 timevar_push (TV_NAME_LOOKUP);
a723baf1
MM
5709 /* Conversion operators are handled specially because ordinary
5710 unqualified name lookup will not find template conversion
5711 operators. */
5712 if (IDENTIFIER_TYPENAME_P (name))
a28e3c7f 5713 {
a723baf1 5714 struct cp_binding_level *level;
68642fb6 5715
a723baf1 5716 for (level = current_binding_level;
a7e8c268 5717 level && level->kind != sk_namespace;
a723baf1 5718 level = level->level_chain)
a28e3c7f 5719 {
a723baf1
MM
5720 tree class_type;
5721 tree operators;
5722
5723 /* A conversion operator can only be declared in a class
5724 scope. */
a7e8c268 5725 if (level->kind != sk_class)
a723baf1
MM
5726 continue;
5727
5728 /* Lookup the conversion operator in the class. */
82a2669e 5729 class_type = level->this_entity;
a723baf1
MM
5730 operators = lookup_fnfields (class_type, name, /*protect=*/0);
5731 if (operators)
22ffcc6f 5732 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, operators);
a28e3c7f 5733 }
e1cd6e56 5734
22ffcc6f 5735 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
5b163de4 5736 }
e76a2646 5737
a723baf1
MM
5738 flags |= lookup_flags (prefer_type, namespaces_only);
5739
d8f8dca1 5740 /* First, look in non-namespace scopes. */
6f1b4c42
JM
5741
5742 if (current_class_type == NULL_TREE)
5743 nonclass = 1;
5744
af6fd53f 5745 for (iter = IDENTIFIER_BINDING (name); iter; iter = iter->previous)
d8f8dca1 5746 {
235f734d
MM
5747 tree binding;
5748
af6fd53f 5749 if (!LOCAL_BINDING_P (iter) && nonclass)
d8f8dca1
MM
5750 /* We're not looking for class-scoped bindings, so keep going. */
5751 continue;
68642fb6 5752
d8f8dca1 5753 /* If this is the kind of thing we're looking for, we're done. */
147135cc
GDR
5754 if (qualify_lookup (iter->value, flags))
5755 binding = iter->value;
68642fb6 5756 else if ((flags & LOOKUP_PREFER_TYPES)
147135cc
GDR
5757 && qualify_lookup (iter->type, flags))
5758 binding = iter->type;
235f734d
MM
5759 else
5760 binding = NULL_TREE;
5761
8fbc5ae7 5762 if (binding)
d8f8dca1 5763 {
235f734d 5764 val = binding;
8fbc5ae7 5765 break;
d8f8dca1
MM
5766 }
5767 }
f181d4ae 5768
235f734d 5769 /* Now lookup in namespace scopes. */
8fbc5ae7 5770 if (!val)
e76a2646 5771 {
af6fd53f 5772 tree t = unqualified_namespace_lookup (name, flags, 0);
235f734d 5773 if (t)
8fbc5ae7 5774 val = t;
c1def683
JM
5775 }
5776
8d08fdba
MS
5777 if (val)
5778 {
0c64a9ca
JM
5779 /* If we have a single function from a using decl, pull it out. */
5780 if (TREE_CODE (val) == OVERLOAD && ! really_overloaded_fn (val))
5781 val = OVL_FUNCTION (val);
8d08fdba 5782 }
0ca7178c 5783
22ffcc6f 5784 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val);
8d08fdba
MS
5785}
5786
700f8a87 5787tree
11f6b451 5788lookup_name_nonclass (tree name)
700f8a87 5789{
a723baf1 5790 return lookup_name_real (name, 0, 1, 0, LOOKUP_COMPLAIN);
700f8a87
MS
5791}
5792
2c73f9f5 5793tree
11f6b451 5794lookup_function_nonclass (tree name, tree args)
2c73f9f5
ML
5795{
5796 return lookup_arg_dependent (name, lookup_name_nonclass (name), args);
5797}
5798
700f8a87 5799tree
11f6b451 5800lookup_name (tree name, int prefer_type)
700f8a87 5801{
a723baf1 5802 return lookup_name_real (name, prefer_type, 0, 0, LOOKUP_COMPLAIN);
700f8a87
MS
5803}
5804
a7d2d407
MM
5805/* Similar to `lookup_name' but look only in the innermost non-class
5806 binding level. */
8d08fdba
MS
5807
5808tree
11f6b451 5809lookup_name_current_level (tree name)
8d08fdba 5810{
e2500fed 5811 struct cp_binding_level *b;
a7d2d407 5812 tree t = NULL_TREE;
8d08fdba 5813
22ffcc6f 5814 timevar_push (TV_NAME_LOOKUP);
a7e8c268 5815 b = innermost_nonclass_level ();
a7d2d407 5816
a7e8c268 5817 if (b->kind == sk_namespace)
8d08fdba 5818 {
5fdaba89 5819 t = IDENTIFIER_NAMESPACE_VALUE (name);
8d08fdba
MS
5820
5821 /* extern "C" function() */
5822 if (t != NULL_TREE && TREE_CODE (t) == TREE_LIST)
5823 t = TREE_VALUE (t);
5824 }
68642fb6 5825 else if (IDENTIFIER_BINDING (name)
f181d4ae 5826 && LOCAL_BINDING_P (IDENTIFIER_BINDING (name)))
8d08fdba 5827 {
a4443a08
MS
5828 while (1)
5829 {
147135cc 5830 if (IDENTIFIER_BINDING (name)->scope == b)
22ffcc6f 5831 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, IDENTIFIER_VALUE (name));
68642fb6 5832
ac20c67a 5833 if (b->kind == sk_cleanup)
9ed182dc
JM
5834 b = b->level_chain;
5835 else
5836 break;
5837 }
5838 }
5839
22ffcc6f 5840 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
9ed182dc
JM
5841}
5842
5843/* Like lookup_name_current_level, but for types. */
5844
5845tree
11f6b451 5846lookup_type_current_level (tree name)
9ed182dc
JM
5847{
5848 register tree t = NULL_TREE;
5849
22ffcc6f 5850 timevar_push (TV_NAME_LOOKUP);
a7e8c268
MM
5851 my_friendly_assert (current_binding_level->kind != sk_namespace,
5852 980716);
9ed182dc
JM
5853
5854 if (REAL_IDENTIFIER_TYPE_VALUE (name) != NULL_TREE
5855 && REAL_IDENTIFIER_TYPE_VALUE (name) != global_type_node)
5856 {
e2500fed 5857 struct cp_binding_level *b = current_binding_level;
9ed182dc
JM
5858 while (1)
5859 {
5860 if (purpose_member (name, b->type_shadowed))
22ffcc6f
GDR
5861 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP,
5862 REAL_IDENTIFIER_TYPE_VALUE (name));
ac20c67a 5863 if (b->kind == sk_cleanup)
a4443a08
MS
5864 b = b->level_chain;
5865 else
5866 break;
5867 }
8d08fdba
MS
5868 }
5869
22ffcc6f 5870 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
8d08fdba 5871}
3e3f722c 5872
8d08fdba 5873\f
47aa0df4
MM
5874
5875/* A chain of TYPE_DECLs for the builtin types. */
5876
5877static GTY(()) tree builtin_type_decls;
5878
5879/* Return a chain of TYPE_DECLs for the builtin types. */
5880
5881tree
edaf3e03 5882cxx_builtin_type_decls (void)
47aa0df4
MM
5883{
5884 return builtin_type_decls;
5885}
5886
8d08fdba 5887/* Push the declarations of builtin types into the namespace.
4b0d3cbe
MM
5888 RID_INDEX is the index of the builtin type in the array
5889 RID_POINTERS. NAME is the name used when looking up the builtin
5890 type. TYPE is the _TYPE node for the builtin type. */
8d08fdba 5891
eaa7c03f 5892void
11f6b451
NN
5893record_builtin_type (enum rid rid_index,
5894 const char* name,
5895 tree type)
8d08fdba
MS
5896{
5897 tree rname = NULL_TREE, tname = NULL_TREE;
a703fb38 5898 tree tdecl = NULL_TREE;
8d08fdba 5899
0e5921e8 5900 if ((int) rid_index < (int) RID_MAX)
8d08fdba
MS
5901 rname = ridpointers[(int) rid_index];
5902 if (name)
5903 tname = get_identifier (name);
5904
4b0d3cbe
MM
5905 /* The calls to SET_IDENTIFIER_GLOBAL_VALUE below should be
5906 eliminated. Built-in types should not be looked up name; their
5907 names are keywords that the parser can recognize. However, there
5908 is code in c-common.c that uses identifier_global_value to look
5909 up built-in types by name. */
8d08fdba
MS
5910 if (tname)
5911 {
4b0d3cbe
MM
5912 tdecl = build_decl (TYPE_DECL, tname, type);
5913 DECL_ARTIFICIAL (tdecl) = 1;
a7e8c268 5914 SET_IDENTIFIER_GLOBAL_VALUE (tname, tdecl);
8d08fdba 5915 }
4b0d3cbe 5916 if (rname)
8d08fdba 5917 {
4b0d3cbe 5918 if (!tdecl)
8d08fdba 5919 {
4b0d3cbe
MM
5920 tdecl = build_decl (TYPE_DECL, rname, type);
5921 DECL_ARTIFICIAL (tdecl) = 1;
8d08fdba 5922 }
4b0d3cbe 5923 SET_IDENTIFIER_GLOBAL_VALUE (rname, tdecl);
8d08fdba 5924 }
4b0d3cbe
MM
5925
5926 if (!TYPE_NAME (type))
5927 TYPE_NAME (type) = tdecl;
47aa0df4
MM
5928
5929 if (tdecl)
5930 {
5931 TREE_CHAIN (tdecl) = builtin_type_decls;
5932 builtin_type_decls = tdecl;
5933 }
8d08fdba
MS
5934}
5935
eff71ab0 5936/* Record one of the standard Java types.
4d8a1dd6
MM
5937 * Declare it as having the given NAME.
5938 * If SIZE > 0, it is the size of one of the integral types;
5939 * otherwise it is the negative of the size of one of the other types. */
eff71ab0
PB
5940
5941static tree
11f6b451 5942record_builtin_java_type (const char* name, int size)
eff71ab0
PB
5943{
5944 tree type, decl;
5945 if (size > 0)
5946 type = make_signed_type (size);
5947 else if (size > -32)
a1c65f9f 5948 { /* "__java_char" or ""__java_boolean". */
eff71ab0
PB
5949 type = make_unsigned_type (-size);
5950 /*if (size == -1) TREE_SET_CODE (type, BOOLEAN_TYPE);*/
5951 }
5952 else
a1c65f9f 5953 { /* "__java_float" or ""__java_double". */
eff71ab0
PB
5954 type = make_node (REAL_TYPE);
5955 TYPE_PRECISION (type) = - size;
5956 layout_type (type);
5957 }
0e5921e8 5958 record_builtin_type (RID_MAX, name, type);
eff71ab0 5959 decl = TYPE_NAME (type);
e229f2cd
PB
5960
5961 /* Suppress generate debug symbol entries for these types,
5962 since for normal C++ they are just clutter.
a1c65f9f 5963 However, push_lang_context undoes this if extern "Java" is seen. */
eff71ab0 5964 DECL_IGNORED_P (decl) = 1;
e229f2cd 5965
eff71ab0
PB
5966 TYPE_FOR_JAVA (type) = 1;
5967 return type;
5968}
5969
a1c65f9f 5970/* Push a type into the namespace so that the back-ends ignore it. */
036407f7
ML
5971
5972static void
11f6b451 5973record_unknown_type (tree type, const char* name)
036407f7
ML
5974{
5975 tree decl = pushdecl (build_decl (TYPE_DECL, get_identifier (name), type));
5976 /* Make sure the "unknown type" typedecl gets ignored for debug info. */
5977 DECL_IGNORED_P (decl) = 1;
5978 TYPE_DECL_SUPPRESS_DEBUG (decl) = 1;
5979 TYPE_SIZE (type) = TYPE_SIZE (void_type_node);
5980 TYPE_ALIGN (type) = 1;
11cf4d18 5981 TYPE_USER_ALIGN (type) = 0;
036407f7 5982 TYPE_MODE (type) = TYPE_MODE (void_type_node);
68642fb6 5983}
036407f7 5984
d43829f9
MM
5985/* An string for which we should create an IDENTIFIER_NODE at
5986 startup. */
5987
5988typedef struct predefined_identifier
5989{
5990 /* The name of the identifier. */
8b60264b 5991 const char *const name;
d43829f9 5992 /* The place where the IDENTIFIER_NODE should be stored. */
8b60264b 5993 tree *const node;
838dfd8a 5994 /* Nonzero if this is the name of a constructor or destructor. */
8b60264b 5995 const int ctor_or_dtor_p;
d43829f9
MM
5996} predefined_identifier;
5997
5998/* Create all the predefined identifiers. */
5999
6000static void
11f6b451 6001initialize_predefined_identifiers (void)
d43829f9 6002{
8b60264b 6003 const predefined_identifier *pid;
d43829f9
MM
6004
6005 /* A table of identifiers to create at startup. */
8b60264b 6006 static const predefined_identifier predefined_identifiers[] = {
298d6f60
MM
6007 { "C++", &lang_name_cplusplus, 0 },
6008 { "C", &lang_name_c, 0 },
6009 { "Java", &lang_name_java, 0 },
6010 { CTOR_NAME, &ctor_identifier, 1 },
6011 { "__base_ctor", &base_ctor_identifier, 1 },
6012 { "__comp_ctor", &complete_ctor_identifier, 1 },
6013 { DTOR_NAME, &dtor_identifier, 1 },
6014 { "__comp_dtor", &complete_dtor_identifier, 1 },
6015 { "__base_dtor", &base_dtor_identifier, 1 },
6016 { "__deleting_dtor", &deleting_dtor_identifier, 1 },
298d6f60 6017 { IN_CHARGE_NAME, &in_charge_identifier, 0 },
298d6f60
MM
6018 { "nelts", &nelts_identifier, 0 },
6019 { THIS_NAME, &this_identifier, 0 },
c4372ef4 6020 { VTABLE_DELTA_NAME, &delta_identifier, 0 },
298d6f60 6021 { VTABLE_PFN_NAME, &pfn_identifier, 0 },
298d6f60 6022 { "_vptr", &vptr_identifier, 0 },
3ec6bad3 6023 { "__vtt_parm", &vtt_parm_identifier, 0 },
82a2669e 6024 { "::", &global_scope_name, 0 },
1f6e1acc 6025 { "std", &std_identifier, 0 },
298d6f60 6026 { NULL, NULL, 0 }
d43829f9
MM
6027 };
6028
6029 for (pid = predefined_identifiers; pid->name; ++pid)
298d6f60
MM
6030 {
6031 *pid->node = get_identifier (pid->name);
6032 if (pid->ctor_or_dtor_p)
6033 IDENTIFIER_CTOR_OR_DTOR_P (*pid->node) = 1;
6034 }
d43829f9
MM
6035}
6036
8d08fdba
MS
6037/* Create the predefined scalar types of C,
6038 and some nodes representing standard constants (0, 1, (void *)0).
6039 Initialize the global binding level.
6040 Make definitions for built-in primitive functions. */
6041
6042void
11f6b451 6043cxx_init_decl_processing (void)
8d08fdba 6044{
10841285
MM
6045 tree void_ftype;
6046 tree void_ftype_ptr;
8d08fdba 6047
d43829f9
MM
6048 /* Create all the identifiers we need. */
6049 initialize_predefined_identifiers ();
8d08fdba 6050
8f17b5c5 6051 /* Fill in back-end hooks. */
d363e7bf 6052 lang_missing_noreturn_ok_p = &cp_missing_noreturn_ok_p;
99dccabc 6053
9cd64686
MM
6054 /* Create the global variables. */
6055 push_to_top_level ();
8012c983 6056
82a2669e
GDR
6057 current_function_decl = NULL_TREE;
6058 current_binding_level = NULL_BINDING_LEVEL;
6059 free_binding_level = NULL_BINDING_LEVEL;
a1c65f9f 6060 /* Enter the global namespace. */
30394414 6061 my_friendly_assert (global_namespace == NULL_TREE, 375);
82a2669e
GDR
6062 global_namespace = build_lang_decl (NAMESPACE_DECL, global_scope_name,
6063 void_type_node);
ac20c67a 6064 begin_scope (sk_namespace, global_namespace);
82a2669e 6065
30394414
JM
6066 current_lang_name = NULL_TREE;
6067
bccd95ae 6068 /* Adjust various flags based on command-line settings. */
4a386498 6069 if (!flag_permissive)
2642b9bf 6070 flag_pedantic_errors = 1;
bccd95ae 6071 if (!flag_no_inline)
b850de4f
MM
6072 {
6073 flag_inline_trees = 1;
6074 flag_no_inline = 1;
6075 }
acc72c37
MM
6076 if (flag_inline_functions)
6077 {
6078 flag_inline_trees = 2;
6079 flag_inline_functions = 0;
6080 }
830fcda8 6081
f963b5d9
RS
6082 /* Force minimum function alignment if using the least significant
6083 bit of function pointers to store the virtual bit. */
6084 if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_pfn
6085 && force_align_functions_log < 1)
6086 force_align_functions_log = 1;
6087
8d08fdba
MS
6088 /* Initially, C. */
6089 current_lang_name = lang_name_c;
6090
81b3411c
BS
6091 build_common_tree_nodes (flag_signed_char);
6092
8d08fdba
MS
6093 error_mark_list = build_tree_list (error_mark_node, error_mark_node);
6094 TREE_TYPE (error_mark_list) = error_mark_node;
6095
6bcedb4e 6096 /* Create the `std' namespace. */
1dbb6023
NS
6097 push_namespace (std_identifier);
6098 std_node = current_namespace;
6099 pop_namespace ();
5362b086 6100
eaa7c03f 6101 c_common_nodes_and_builtins ();
37c46b43 6102
4d8a1dd6
MM
6103 java_byte_type_node = record_builtin_java_type ("__java_byte", 8);
6104 java_short_type_node = record_builtin_java_type ("__java_short", 16);
6105 java_int_type_node = record_builtin_java_type ("__java_int", 32);
6106 java_long_type_node = record_builtin_java_type ("__java_long", 64);
6107 java_float_type_node = record_builtin_java_type ("__java_float", -32);
6108 java_double_type_node = record_builtin_java_type ("__java_double", -64);
6109 java_char_type_node = record_builtin_java_type ("__java_char", -16);
6110 java_boolean_type_node = record_builtin_java_type ("__java_boolean", -1);
eff71ab0 6111
8d08fdba
MS
6112 integer_two_node = build_int_2 (2, 0);
6113 TREE_TYPE (integer_two_node) = integer_type_node;
6114 integer_three_node = build_int_2 (3, 0);
6115 TREE_TYPE (integer_three_node) = integer_type_node;
8d08fdba 6116
255512c1 6117 record_builtin_type (RID_BOOL, "bool", boolean_type_node);
de7df9eb
JM
6118 truthvalue_type_node = boolean_type_node;
6119 truthvalue_false_node = boolean_false_node;
6120 truthvalue_true_node = boolean_true_node;
2986ae00 6121
4cc1d462 6122 empty_except_spec = build_tree_list (NULL_TREE, NULL_TREE);
5fd8e536
JM
6123
6124#if 0
3e411c3f 6125 record_builtin_type (RID_MAX, NULL, string_type_node);
5fd8e536
JM
6126#endif
6127
1f84ec23
MM
6128 delta_type_node = ptrdiff_type_node;
6129 vtable_index_type = ptrdiff_type_node;
c7e266a6 6130
3ec6bad3 6131 vtt_parm_type = build_pointer_type (const_ptr_type_node);
10841285
MM
6132 void_ftype = build_function_type (void_type_node, void_list_node);
6133 void_ftype_ptr = build_function_type (void_type_node,
6134 tree_cons (NULL_TREE,
6135 ptr_type_node,
6136 void_list_node));
824b9a4c 6137 void_ftype_ptr
4cc1d462 6138 = build_exception_variant (void_ftype_ptr, empty_except_spec);
8d08fdba 6139
8d08fdba
MS
6140 /* C++ extensions */
6141
6142 unknown_type_node = make_node (UNKNOWN_TYPE);
036407f7
ML
6143 record_unknown_type (unknown_type_node, "unknown type");
6144
8d08fdba
MS
6145 /* Indirecting an UNKNOWN_TYPE node yields an UNKNOWN_TYPE node. */
6146 TREE_TYPE (unknown_type_node) = unknown_type_node;
a6967cc0 6147
a6967cc0
JM
6148 /* Looking up TYPE_POINTER_TO and TYPE_REFERENCE_TO yield the same
6149 result. */
8d08fdba
MS
6150 TYPE_POINTER_TO (unknown_type_node) = unknown_type_node;
6151 TYPE_REFERENCE_TO (unknown_type_node) = unknown_type_node;
6152
c4372ef4
NS
6153 {
6154 /* Make sure we get a unique function type, so we can give
6155 its pointer type a name. (This wins for gdb.) */
6156 tree vfunc_type = make_node (FUNCTION_TYPE);
6157 TREE_TYPE (vfunc_type) = integer_type_node;
6158 TYPE_ARG_TYPES (vfunc_type) = NULL_TREE;
6159 layout_type (vfunc_type);
6160
6161 vtable_entry_type = build_pointer_type (vfunc_type);
6162 }
0e5921e8 6163 record_builtin_type (RID_MAX, VTBL_PTR_TYPE, vtable_entry_type);
8d08fdba 6164
8d08fdba 6165 vtbl_type_node
52bf7d5d 6166 = build_cplus_array_type (vtable_entry_type, NULL_TREE);
8d08fdba 6167 layout_type (vtbl_type_node);
91063b51 6168 vtbl_type_node = build_qualified_type (vtbl_type_node, TYPE_QUAL_CONST);
3e411c3f 6169 record_builtin_type (RID_MAX, NULL, vtbl_type_node);
849da744
MM
6170 vtbl_ptr_type_node = build_pointer_type (vtable_entry_type);
6171 layout_type (vtbl_ptr_type_node);
3e411c3f 6172 record_builtin_type (RID_MAX, NULL, vtbl_ptr_type_node);
8d08fdba 6173
1f84ec23
MM
6174 push_namespace (get_identifier ("__cxxabiv1"));
6175 abi_node = current_namespace;
6176 pop_namespace ();
6633d636 6177
2c73f9f5 6178 global_type_node = make_node (LANG_TYPE);
036407f7 6179 record_unknown_type (global_type_node, "global type");
2c73f9f5 6180
db5ae43f
MS
6181 /* Now, C++. */
6182 current_lang_name = lang_name_cplusplus;
8d08fdba 6183
ced78d8b 6184 {
4b0d3cbe
MM
6185 tree bad_alloc_id;
6186 tree bad_alloc_type_node;
6187 tree bad_alloc_decl;
6188 tree newtype, deltype;
10841285
MM
6189 tree ptr_ftype_sizetype;
6190
1dbb6023 6191 push_namespace (std_identifier);
4b0d3cbe
MM
6192 bad_alloc_id = get_identifier ("bad_alloc");
6193 bad_alloc_type_node = make_aggr_type (RECORD_TYPE);
6194 TYPE_CONTEXT (bad_alloc_type_node) = current_namespace;
6195 bad_alloc_decl
6196 = create_implicit_typedef (bad_alloc_id, bad_alloc_type_node);
6197 DECL_CONTEXT (bad_alloc_decl) = current_namespace;
6198 TYPE_STUB_DECL (bad_alloc_type_node) = bad_alloc_decl;
1dbb6023 6199 pop_namespace ();
4b0d3cbe 6200
10841285
MM
6201 ptr_ftype_sizetype
6202 = build_function_type (ptr_type_node,
6203 tree_cons (NULL_TREE,
c9f8536c 6204 size_type_node,
10841285 6205 void_list_node));
2c73f9f5 6206 newtype = build_exception_variant
0ba8a114
NS
6207 (ptr_ftype_sizetype, add_exception_specifier
6208 (NULL_TREE, bad_alloc_type_node, -1));
4cc1d462 6209 deltype = build_exception_variant (void_ftype_ptr, empty_except_spec);
596ea4e5
AS
6210 push_cp_library_fn (NEW_EXPR, newtype);
6211 push_cp_library_fn (VEC_NEW_EXPR, newtype);
6212 global_delete_fndecl = push_cp_library_fn (DELETE_EXPR, deltype);
6213 push_cp_library_fn (VEC_DELETE_EXPR, deltype);
ced78d8b 6214 }
8d08fdba
MS
6215
6216 abort_fndecl
1f84ec23 6217 = build_library_fn_ptr ("__cxa_pure_virtual", void_ftype);
8d08fdba 6218
8d08fdba
MS
6219 /* Perform other language dependent initializations. */
6220 init_class_processing ();
8d08fdba 6221 init_search_processing ();
db48b831 6222 init_rtti_processing ();
8d08fdba 6223
6467930b 6224 if (flag_exceptions)
8d2733ca 6225 init_exception_processing ();
9e9ff709 6226
7fcdf4c2 6227 if (! supports_one_only ())
72b7eeff 6228 flag_weak = 0;
8d08fdba 6229
2ce07e2d 6230 make_fname_decl = cp_make_fname_decl;
0ba8a114 6231 start_fname_decls ();
8d08fdba 6232
e9a25f70
JL
6233 /* Show we use EH for cleanups. */
6234 using_eh_for_cleanups ();
62c154ed 6235
d9cf7c82
JM
6236 /* Maintain consistency. Perhaps we should just complain if they
6237 say -fwritable-strings? */
6238 if (flag_writable_strings)
6239 flag_const_strings = 0;
62c154ed
JM
6240}
6241
0ba8a114
NS
6242/* Generate an initializer for a function naming variable from
6243 NAME. NAME may be NULL, in which case we generate a special
a1c65f9f 6244 ERROR_MARK node which should be replaced later. */
0ba8a114
NS
6245
6246tree
11f6b451 6247cp_fname_init (const char* name)
0ba8a114
NS
6248{
6249 tree domain = NULL_TREE;
6250 tree type;
6251 tree init = NULL_TREE;
6252 size_t length = 0;
6253
6254 if (name)
6255 {
6256 length = strlen (name);
6257 domain = build_index_type (size_int (length));
6258 init = build_string (length + 1, name);
6259 }
6260
6261 type = build_qualified_type (char_type_node, TYPE_QUAL_CONST);
6262 type = build_cplus_array_type (type, domain);
6263
6264 if (init)
6265 TREE_TYPE (init) = type;
6266 else
6267 /* We don't know the value until instantiation time. Make
a1c65f9f 6268 something which will be digested now, but replaced later. */
0ba8a114
NS
6269 init = build (ERROR_MARK, type);
6270
6271 return init;
6272}
6273
2ce07e2d
NS
6274/* Create the VAR_DECL for __FUNCTION__ etc. ID is the name to give the
6275 decl, NAME is the initialization string and TYPE_DEP indicates whether
6276 NAME depended on the type of the function. We make use of that to detect
0ba8a114
NS
6277 __PRETTY_FUNCTION__ inside a template fn. This is being done
6278 lazily at the point of first use, so we musn't push the decl now. */
2ce07e2d
NS
6279
6280static tree
11f6b451 6281cp_make_fname_decl (tree id, int type_dep)
2ce07e2d 6282{
83182544 6283 const char *const name = (type_dep && processing_template_decl
0ba8a114
NS
6284 ? NULL : fname_as_string (type_dep));
6285 tree init = cp_fname_init (name);
6286 tree decl = build_decl (VAR_DECL, id, TREE_TYPE (init));
2ce07e2d 6287
6cce57b0 6288 /* As we're using pushdecl_with_scope, we must set the context. */
0ba8a114
NS
6289 DECL_CONTEXT (decl) = current_function_decl;
6290 DECL_PRETTY_FUNCTION_P (decl) = type_dep;
6291
2ce07e2d
NS
6292 TREE_STATIC (decl) = 1;
6293 TREE_READONLY (decl) = 1;
2ce07e2d 6294 DECL_ARTIFICIAL (decl) = 1;
2ce07e2d 6295 DECL_INITIAL (decl) = init;
0ba8a114
NS
6296
6297 TREE_USED (decl) = 1;
5362b086 6298
6cce57b0
JM
6299 if (current_function_decl)
6300 {
6301 struct cp_binding_level *b = current_binding_level;
a7e8c268 6302 while (b->level_chain->kind != sk_function_parms)
6cce57b0
JM
6303 b = b->level_chain;
6304 pushdecl_with_scope (decl, b);
6305 }
6306
0ba8a114
NS
6307 cp_finish_decl (decl, init, NULL_TREE, LOOKUP_ONLYCONVERTING);
6308
2ce07e2d
NS
6309 return decl;
6310}
6311
d52e4867
RS
6312/* Make a definition for a builtin function named NAME in the current
6313 namespace, whose data type is TYPE and whose context is CONTEXT.
6314 TYPE should be a function type with argument types.
8d08fdba 6315
0c11ada6
JM
6316 CLASS and CODE tell later passes how to compile calls to this function.
6317 See tree.h for possible values.
6318
6319 If LIBNAME is nonzero, use that for DECL_ASSEMBLER_NAME,
6a2dd09a
RS
6320 the name to be called if we can't opencode the function.
6321 If ATTRS is nonzero, use that for the function's attribute
6322 list. */
8d08fdba 6323
d52e4867 6324static tree
11f6b451
NN
6325builtin_function_1 (const char* name,
6326 tree type,
6327 tree context,
6328 int code,
6329 enum built_in_class class,
6330 const char* libname,
6331 tree attrs)
8d08fdba 6332{
596ea4e5 6333 tree decl = build_library_fn_1 (get_identifier (name), ERROR_MARK, type);
0c11ada6
JM
6334 DECL_BUILT_IN_CLASS (decl) = class;
6335 DECL_FUNCTION_CODE (decl) = code;
d52e4867 6336 DECL_CONTEXT (decl) = context;
12a22e76 6337
6bcedb4e 6338 pushdecl (decl);
6bcedb4e 6339
8d08fdba
MS
6340 /* Since `pushdecl' relies on DECL_ASSEMBLER_NAME instead of DECL_NAME,
6341 we cannot change DECL_ASSEMBLER_NAME until we have installed this
6342 function in the namespace. */
0c11ada6 6343 if (libname)
92643fea
MM
6344 SET_DECL_ASSEMBLER_NAME (decl, get_identifier (libname));
6345 make_decl_rtl (decl, NULL);
935d1834
ZW
6346
6347 /* Warn if a function in the namespace for users
6348 is used without an occasion to consider it declared. */
6349 if (name[0] != '_' || name[1] != '_')
6350 DECL_ANTICIPATED (decl) = 1;
6351
6431177a 6352 /* Possibly apply some default attributes to this built-in function. */
6a2dd09a
RS
6353 if (attrs)
6354 decl_attributes (&decl, attrs, ATTR_FLAG_BUILT_IN);
6355 else
6356 decl_attributes (&decl, NULL_TREE, 0);
6431177a 6357
8d08fdba
MS
6358 return decl;
6359}
7f4edbcb 6360
d52e4867
RS
6361/* Entry point for the benefit of c_common_nodes_and_builtins.
6362
34cd5ae7 6363 Make a definition for a builtin function named NAME and whose data type
d52e4867
RS
6364 is TYPE. TYPE should be a function type with argument types. This
6365 function places the anticipated declaration in the global namespace
6366 and additionally in the std namespace if appropriate.
6367
6368 CLASS and CODE tell later passes how to compile calls to this function.
6369 See tree.h for possible values.
6370
6371 If LIBNAME is nonzero, use that for DECL_ASSEMBLER_NAME,
6a2dd09a
RS
6372 the name to be called if we can't opencode the function.
6373
6374 If ATTRS is nonzero, use that for the function's attribute
6375 list. */
d52e4867
RS
6376
6377tree
11f6b451
NN
6378builtin_function (const char* name,
6379 tree type,
6380 int code,
6381 enum built_in_class class,
6382 const char* libname,
6383 tree attrs)
d52e4867
RS
6384{
6385 /* All builtins that don't begin with an '_' should additionally
6386 go in the 'std' namespace. */
6387 if (name[0] != '_')
6388 {
6389 push_namespace (std_identifier);
6a2dd09a 6390 builtin_function_1 (name, type, std_node, code, class, libname, attrs);
d52e4867
RS
6391 pop_namespace ();
6392 }
6393
6a2dd09a
RS
6394 return builtin_function_1 (name, type, NULL_TREE, code,
6395 class, libname, attrs);
d52e4867
RS
6396}
6397
0c11ada6
JM
6398/* Generate a FUNCTION_DECL with the typical flags for a runtime library
6399 function. Not called directly. */
6400
6401static tree
11f6b451 6402build_library_fn_1 (tree name, enum tree_code operator_code, tree type)
0c11ada6
JM
6403{
6404 tree fn = build_lang_decl (FUNCTION_DECL, name, type);
6405 DECL_EXTERNAL (fn) = 1;
6406 TREE_PUBLIC (fn) = 1;
6407 DECL_ARTIFICIAL (fn) = 1;
6408 TREE_NOTHROW (fn) = 1;
596ea4e5 6409 SET_OVERLOADED_OPERATOR_CODE (fn, operator_code);
5d2ed28c 6410 SET_DECL_LANGUAGE (fn, lang_c);
0c11ada6
JM
6411 return fn;
6412}
c2a37c55 6413
0c11ada6
JM
6414/* Returns the _DECL for a library function with C linkage.
6415 We assume that such functions never throw; if this is incorrect,
6416 callers should unset TREE_NOTHROW. */
c2a37c55 6417
7f4edbcb 6418tree
11f6b451 6419build_library_fn (tree name, tree type)
0c11ada6 6420{
19e7881c 6421 return build_library_fn_1 (name, ERROR_MARK, type);
0c11ada6
JM
6422}
6423
6424/* Returns the _DECL for a library function with C++ linkage. */
6425
596ea4e5 6426static tree
11f6b451 6427build_cp_library_fn (tree name, enum tree_code operator_code, tree type)
0c11ada6 6428{
596ea4e5 6429 tree fn = build_library_fn_1 (name, operator_code, type);
0c11ada6 6430 TREE_NOTHROW (fn) = TYPE_NOTHROW_P (type);
6bbf1598 6431 DECL_CONTEXT (fn) = FROB_CONTEXT (current_namespace);
5d2ed28c 6432 SET_DECL_LANGUAGE (fn, lang_cplusplus);
0c11ada6 6433 set_mangled_name_for_decl (fn);
0c11ada6
JM
6434 return fn;
6435}
6436
6437/* Like build_library_fn, but takes a C string instead of an
6438 IDENTIFIER_NODE. */
6439
6440tree
11f6b451 6441build_library_fn_ptr (const char* name, tree type)
7f4edbcb 6442{
0c11ada6
JM
6443 return build_library_fn (get_identifier (name), type);
6444}
6445
6446/* Like build_cp_library_fn, but takes a C string instead of an
6447 IDENTIFIER_NODE. */
6448
6449tree
11f6b451 6450build_cp_library_fn_ptr (const char* name, tree type)
0c11ada6 6451{
596ea4e5 6452 return build_cp_library_fn (get_identifier (name), ERROR_MARK, type);
0c11ada6
JM
6453}
6454
6455/* Like build_library_fn, but also pushes the function so that we will
6456 be able to find it via IDENTIFIER_GLOBAL_VALUE. */
6457
6458tree
11f6b451 6459push_library_fn (tree name, tree type)
0c11ada6
JM
6460{
6461 tree fn = build_library_fn (name, type);
6462 pushdecl_top_level (fn);
6463 return fn;
6464}
6465
6466/* Like build_cp_library_fn, but also pushes the function so that it
6467 will be found by normal lookup. */
6468
596ea4e5 6469static tree
11f6b451 6470push_cp_library_fn (enum tree_code operator_code, tree type)
0c11ada6 6471{
5362b086 6472 tree fn = build_cp_library_fn (ansi_opname (operator_code),
596ea4e5
AS
6473 operator_code,
6474 type);
0c11ada6
JM
6475 pushdecl (fn);
6476 return fn;
6477}
6478
6479/* Like push_library_fn, but takes a TREE_LIST of parm types rather than
6480 a FUNCTION_TYPE. */
6481
6482tree
11f6b451 6483push_void_library_fn (tree name, tree parmtypes)
0c11ada6
JM
6484{
6485 tree type = build_function_type (void_type_node, parmtypes);
6486 return push_library_fn (name, type);
6487}
6488
cf74fb86 6489/* Like push_library_fn, but also note that this function throws
0c11ada6
JM
6490 and does not return. Used for __throw_foo and the like. */
6491
6492tree
11f6b451 6493push_throw_library_fn (tree name, tree type)
0c11ada6 6494{
cf74fb86 6495 tree fn = push_library_fn (name, type);
0c11ada6
JM
6496 TREE_THIS_VOLATILE (fn) = 1;
6497 TREE_NOTHROW (fn) = 0;
6498 return fn;
7f4edbcb 6499}
8d08fdba 6500\f
61a127b3
MM
6501/* When we call finish_struct for an anonymous union, we create
6502 default copy constructors and such. But, an anonymous union
6503 shouldn't have such things; this function undoes the damage to the
6504 anonymous union type T.
6505
6506 (The reason that we create the synthesized methods is that we don't
6507 distinguish `union { int i; }' from `typedef union { int i; } U'.
6508 The first is an anonymous union; the second is just an ordinary
6509 union type.) */
6510
6511void
11f6b451 6512fixup_anonymous_aggr (tree t)
61a127b3
MM
6513{
6514 tree *q;
6515
6516 /* Wipe out memory of synthesized methods */
6517 TYPE_HAS_CONSTRUCTOR (t) = 0;
6518 TYPE_HAS_DEFAULT_CONSTRUCTOR (t) = 0;
6519 TYPE_HAS_INIT_REF (t) = 0;
6520 TYPE_HAS_CONST_INIT_REF (t) = 0;
6521 TYPE_HAS_ASSIGN_REF (t) = 0;
61a127b3
MM
6522 TYPE_HAS_CONST_ASSIGN_REF (t) = 0;
6523
6524 /* Splice the implicitly generated functions out of the TYPE_METHODS
6525 list. */
6526 q = &TYPE_METHODS (t);
6527 while (*q)
6528 {
6529 if (DECL_ARTIFICIAL (*q))
6530 *q = TREE_CHAIN (*q);
6531 else
6532 q = &TREE_CHAIN (*q);
6533 }
6534
cab1f180 6535 /* ISO C++ 9.5.3. Anonymous unions may not have function members. */
61a127b3 6536 if (TYPE_METHODS (t))
ddd2d57e
RH
6537 error ("%Jan anonymous union cannot have function members",
6538 TYPE_MAIN_DECL (t));
a1c2b86d
JJ
6539
6540 /* Anonymous aggregates cannot have fields with ctors, dtors or complex
6541 assignment operators (because they cannot have these methods themselves).
6542 For anonymous unions this is already checked because they are not allowed
6543 in any union, otherwise we have to check it. */
6544 if (TREE_CODE (t) != UNION_TYPE)
6545 {
6546 tree field, type;
6547
6548 for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
6549 if (TREE_CODE (field) == FIELD_DECL)
6550 {
6551 type = TREE_TYPE (field);
6552 if (CLASS_TYPE_P (type))
6553 {
6554 if (TYPE_NEEDS_CONSTRUCTING (type))
6555 cp_error_at ("member %#D' with constructor not allowed in anonymous aggregate",
6556 field);
6557 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
6558 cp_error_at ("member %#D' with destructor not allowed in anonymous aggregate",
6559 field);
6560 if (TYPE_HAS_COMPLEX_ASSIGN_REF (type))
6561 cp_error_at ("member %#D' with copy assignment operator not allowed in anonymous aggregate",
6562 field);
6563 }
6564 }
6565 }
61a127b3
MM
6566}
6567
72a93143 6568/* Make sure that a declaration with no declarator is well-formed, i.e.
a723baf1 6569 just declares a tagged type or anonymous union.
8d08fdba 6570
a723baf1 6571 Returns the type declared; or NULL_TREE if none. */
8d08fdba 6572
72a93143 6573tree
11f6b451 6574check_tag_decl (tree declspecs)
8d08fdba 6575{
72a93143 6576 int found_type = 0;
2bdb0643 6577 int saw_friend = 0;
e8186ecf 6578 int saw_typedef = 0;
2986ae00 6579 tree ob_modifier = NULL_TREE;
8d08fdba 6580 register tree link;
a723baf1
MM
6581 /* If a class, struct, or enum type is declared by the DECLSPECS
6582 (i.e, if a class-specifier, enum-specifier, or non-typename
6583 elaborated-type-specifier appears in the DECLSPECS),
6584 DECLARED_TYPE is set to the corresponding type. */
6585 tree declared_type = NULL_TREE;
6586 bool error_p = false;
8d08fdba
MS
6587
6588 for (link = declspecs; link; link = TREE_CHAIN (link))
6589 {
a723baf1 6590 tree value = TREE_VALUE (link);
8d08fdba 6591
c003e212 6592 if (TYPE_P (value) || TREE_CODE (value) == TYPE_DECL
2bdb0643 6593 || (TREE_CODE (value) == IDENTIFIER_NODE
c003e212 6594 && is_typename_at_global_scope (value)))
8d08fdba 6595 {
72a93143 6596 ++found_type;
5566b478 6597
bd0d5d4a
JM
6598 if (found_type == 2 && TREE_CODE (value) == IDENTIFIER_NODE)
6599 {
6600 if (! in_system_header)
33bd39a2 6601 pedwarn ("redeclaration of C++ built-in type `%T'", value);
bd0d5d4a
JM
6602 return NULL_TREE;
6603 }
6604
6605 if (TYPE_P (value)
6606 && ((TREE_CODE (value) != TYPENAME_TYPE && IS_AGGR_TYPE (value))
6607 || TREE_CODE (value) == ENUMERAL_TYPE))
72a93143
JM
6608 {
6609 my_friendly_assert (TYPE_MAIN_DECL (value) != NULL_TREE, 261);
a723baf1 6610 declared_type = value;
72a93143 6611 }
8d08fdba 6612 }
e8186ecf
GDR
6613 else if (value == ridpointers[(int) RID_TYPEDEF])
6614 saw_typedef = 1;
83f660b7
JM
6615 else if (value == ridpointers[(int) RID_FRIEND])
6616 {
83f660b7
JM
6617 if (current_class_type == NULL_TREE
6618 || current_scope () != current_class_type)
6619 ob_modifier = value;
2bdb0643
JM
6620 else
6621 saw_friend = 1;
83f660b7 6622 }
8d08fdba 6623 else if (value == ridpointers[(int) RID_STATIC]
2986ae00
MS
6624 || value == ridpointers[(int) RID_EXTERN]
6625 || value == ridpointers[(int) RID_AUTO]
28cbf42c
MS
6626 || value == ridpointers[(int) RID_REGISTER]
6627 || value == ridpointers[(int) RID_INLINE]
6628 || value == ridpointers[(int) RID_VIRTUAL]
72a93143
JM
6629 || value == ridpointers[(int) RID_CONST]
6630 || value == ridpointers[(int) RID_VOLATILE]
7a1f3f5f
RH
6631 || value == ridpointers[(int) RID_EXPLICIT]
6632 || value == ridpointers[(int) RID_THREAD])
2986ae00 6633 ob_modifier = value;
a723baf1
MM
6634 else if (value == error_mark_node)
6635 error_p = true;
8d08fdba
MS
6636 }
6637
72a93143
JM
6638 if (found_type > 1)
6639 error ("multiple types in one declaration");
7e2067ca 6640
a723baf1 6641 if (declared_type == NULL_TREE && ! saw_friend && !error_p)
7e2067ca 6642 pedwarn ("declaration does not declare anything");
1951a1b6 6643 /* Check for an anonymous union. */
a723baf1
MM
6644 else if (declared_type && IS_AGGR_TYPE_CODE (TREE_CODE (declared_type))
6645 && TYPE_ANONYMOUS_P (declared_type))
0dd3962d 6646 {
e8186ecf
GDR
6647 /* 7/3 In a simple-declaration, the optional init-declarator-list
6648 can be omitted only when declaring a class (clause 9) or
6649 enumeration (7.2), that is, when the decl-specifier-seq contains
6650 either a class-specifier, an elaborated-type-specifier with
6651 a class-key (9.1), or an enum-specifier. In these cases and
6652 whenever a class-specifier or enum-specifier is present in the
6653 decl-specifier-seq, the identifiers in these specifiers are among
6654 the names being declared by the declaration (as class-name,
6655 enum-names, or enumerators, depending on the syntax). In such
6656 cases, and except for the declaration of an unnamed bit-field (9.6),
6657 the decl-specifier-seq shall introduce one or more names into the
6658 program, or shall redeclare a name introduced by a previous
6659 declaration. [Example:
6660 enum { }; // ill-formed
6661 typedef class { }; // ill-formed
6662 --end example] */
6663 if (saw_typedef)
6664 {
357351e5 6665 error ("missing type-name in typedef-declaration");
e8186ecf
GDR
6666 return NULL_TREE;
6667 }
0dd3962d 6668 /* Anonymous unions are objects, so they can have specifiers. */;
a723baf1 6669 SET_ANON_AGGR_TYPE_P (declared_type);
6bdb8141 6670
a723baf1
MM
6671 if (TREE_CODE (declared_type) != UNION_TYPE && pedantic
6672 && !in_system_header)
6bdb8141 6673 pedwarn ("ISO C++ prohibits anonymous structs");
0dd3962d
JM
6674 }
6675
83f660b7 6676 else if (ob_modifier)
8d08fdba 6677 {
83f660b7
JM
6678 if (ob_modifier == ridpointers[(int) RID_INLINE]
6679 || ob_modifier == ridpointers[(int) RID_VIRTUAL])
33bd39a2 6680 error ("`%D' can only be specified for functions", ob_modifier);
83f660b7 6681 else if (ob_modifier == ridpointers[(int) RID_FRIEND])
33bd39a2 6682 error ("`%D' can only be specified inside a class", ob_modifier);
83f660b7 6683 else if (ob_modifier == ridpointers[(int) RID_EXPLICIT])
33bd39a2 6684 error ("`%D' can only be specified for constructors",
83f660b7
JM
6685 ob_modifier);
6686 else
33bd39a2 6687 error ("`%D' can only be specified for objects and functions",
83f660b7 6688 ob_modifier);
72a93143 6689 }
8d08fdba 6690
a723baf1 6691 return declared_type;
72a93143
JM
6692}
6693
6694/* Called when a declaration is seen that contains no names to declare.
6695 If its type is a reference to a structure, union or enum inherited
6696 from a containing scope, shadow that tag name for the current scope
6697 with a forward reference.
6698 If its type defines a new named structure or union
6699 or defines an enum, it is valid but we need not do anything here.
6700 Otherwise, it is an error.
6701
6702 C++: may have to grok the declspecs to learn about static,
a723baf1 6703 complain for anonymous unions.
72a93143 6704
a723baf1
MM
6705 Returns the TYPE declared -- or NULL_TREE if none. */
6706
6707tree
11f6b451 6708shadow_tag (tree declspecs)
72a93143
JM
6709{
6710 tree t = check_tag_decl (declspecs);
6711
a723baf1
MM
6712 if (!t)
6713 return NULL_TREE;
6714
6715 maybe_process_partial_specialization (t);
72a93143
JM
6716
6717 /* This is where the variables in an anonymous union are
6718 declared. An anonymous union declaration looks like:
6719 union { ... } ;
6720 because there is no declarator after the union, the parser
6721 sends that declaration here. */
a723baf1 6722 if (ANON_AGGR_TYPE_P (t))
72a93143 6723 {
6bdb8141 6724 fixup_anonymous_aggr (t);
72a93143
JM
6725
6726 if (TYPE_FIELDS (t))
6727 {
6728 tree decl = grokdeclarator (NULL_TREE, declspecs, NORMAL, 0,
91d231cb 6729 NULL);
72a93143
JM
6730 finish_anon_union (decl);
6731 }
8d08fdba 6732 }
a723baf1
MM
6733
6734 return t;
8d08fdba
MS
6735}
6736\f
6737/* Decode a "typename", such as "int **", returning a ..._TYPE node. */
6738
6739tree
11f6b451 6740groktypename (tree typename)
8d08fdba 6741{
98884b26
JM
6742 tree specs, attrs;
6743 tree type;
8d08fdba
MS
6744 if (TREE_CODE (typename) != TREE_LIST)
6745 return typename;
98884b26
JM
6746 split_specs_attrs (TREE_PURPOSE (typename), &specs, &attrs);
6747 type = grokdeclarator (TREE_VALUE (typename), specs,
6748 TYPENAME, 0, &attrs);
6749 if (attrs)
6750 cplus_decl_attributes (&type, attrs, 0);
6751 return type;
8d08fdba
MS
6752}
6753
6754/* Decode a declarator in an ordinary declaration or data definition.
6755 This is called as soon as the type information and variable name
6756 have been parsed, before parsing the initializer if any.
6757 Here we create the ..._DECL node, fill in its type,
6758 and put it on the list of decls for the current context.
6759 The ..._DECL node is returned as the value.
6760
6761 Exception: for arrays where the length is not specified,
82580166 6762 the type is left null, to be filled in by `cp_finish_decl'.
8d08fdba
MS
6763
6764 Function definitions do not come here; they go to start_function
6765 instead. However, external and forward declarations of functions
6766 do go through here. Structure field declarations are done by
6767 grokfield and not through here. */
6768
8d08fdba 6769tree
11f6b451
NN
6770start_decl (tree declarator,
6771 tree declspecs,
6772 int initialized,
6773 tree attributes,
6774 tree prefix_attributes)
8d08fdba 6775{
59387d2e 6776 tree decl;
8d08fdba
MS
6777 register tree type, tem;
6778 tree context;
8d08fdba 6779
e92cc029 6780 /* This should only be done once on the top most decl. */
594bb0e7 6781 if (have_extern_spec)
8d08fdba 6782 {
1f8f4a0b
MM
6783 declspecs = tree_cons (NULL_TREE, get_identifier ("extern"),
6784 declspecs);
594bb0e7 6785 have_extern_spec = false;
8d08fdba
MS
6786 }
6787
e23bd218
IR
6788 /* An object declared as __attribute__((deprecated)) suppresses
6789 warnings of uses of other deprecated items. */
6790 if (lookup_attribute ("deprecated", attributes))
6791 deprecated_state = DEPRECATED_SUPPRESS;
6792
91d231cb 6793 attributes = chainon (attributes, prefix_attributes);
b17e2870 6794
c11b6f21 6795 decl = grokdeclarator (declarator, declspecs, NORMAL, initialized,
91d231cb 6796 &attributes);
68642fb6 6797
e23bd218
IR
6798 deprecated_state = DEPRECATED_NORMAL;
6799
a1774733 6800 if (decl == NULL_TREE || TREE_CODE (decl) == VOID_TYPE)
8d08fdba
MS
6801 return NULL_TREE;
6802
6803 type = TREE_TYPE (decl);
6804
44689c12
ML
6805 if (type == error_mark_node)
6806 return NULL_TREE;
6807
4f1c5b7d 6808 context = DECL_CONTEXT (decl);
8d08fdba 6809
9a68c51f
JM
6810 if (initialized && context && TREE_CODE (context) == NAMESPACE_DECL
6811 && context != current_namespace && TREE_CODE (decl) == VAR_DECL)
6812 {
6813 /* When parsing the initializer, lookup should use the object's
a1c65f9f 6814 namespace. */
9a68c51f
JM
6815 push_decl_namespace (context);
6816 }
6817
a1c65f9f 6818 /* We are only interested in class contexts, later. */
2c73f9f5
ML
6819 if (context && TREE_CODE (context) == NAMESPACE_DECL)
6820 context = NULL_TREE;
6821
8d08fdba
MS
6822 if (initialized)
6823 /* Is it valid for this decl to have an initializer at all?
6824 If not, set INITIALIZED to zero, which will indirectly
82580166 6825 tell `cp_finish_decl' to ignore the initializer once it is parsed. */
8d08fdba
MS
6826 switch (TREE_CODE (decl))
6827 {
6828 case TYPE_DECL:
4a7510cb 6829 error ("typedef `%D' is initialized (use __typeof__ instead)", decl);
95f79357 6830 initialized = 0;
8d08fdba
MS
6831 break;
6832
6833 case FUNCTION_DECL:
33bd39a2 6834 error ("function `%#D' is initialized like a variable", decl);
8d08fdba
MS
6835 initialized = 0;
6836 break;
6837
6838 default:
3e41d13b 6839 break;
8d08fdba
MS
6840 }
6841
8d08fdba
MS
6842 if (initialized)
6843 {
a9aedbc2 6844 if (! toplevel_bindings_p ()
8d08fdba 6845 && DECL_EXTERNAL (decl))
33bd39a2 6846 warning ("declaration of `%#D' has `extern' and is initialized",
8d08fdba
MS
6847 decl);
6848 DECL_EXTERNAL (decl) = 0;
5566b478 6849 if (toplevel_bindings_p ())
8d08fdba
MS
6850 TREE_STATIC (decl) = 1;
6851
6852 /* Tell `pushdecl' this is an initialized decl
6853 even though we don't yet have the initializer expression.
82580166 6854 Also tell `cp_finish_decl' it may store the real initializer. */
8d08fdba
MS
6855 DECL_INITIAL (decl) = error_mark_node;
6856 }
6857
fa20888b 6858 /* Set attributes here so if duplicate decl, will have proper attributes. */
91d231cb 6859 cplus_decl_attributes (&decl, attributes, 0);
fa20888b 6860
ecb0eece 6861 /* If #pragma weak was used, mark the decl weak now. */
5f52c0e0 6862 if (global_scope_p (current_binding_level))
ecb0eece
RH
6863 maybe_apply_pragma_weak (decl);
6864
97055d5c
AO
6865 if (TREE_CODE (decl) == FUNCTION_DECL
6866 && DECL_DECLARED_INLINE_P (decl)
6867 && DECL_UNINLINABLE (decl)
6868 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
ddd2d57e 6869 warning ("%Jinline function '%D' given attribute noinline", decl, decl);
97055d5c 6870
d0f062fb 6871 if (context && COMPLETE_TYPE_P (complete_type (context)))
5b605f68 6872 {
14d22dd6 6873 push_nested_class (context);
e97e5263 6874
5b605f68
MS
6875 if (TREE_CODE (decl) == VAR_DECL)
6876 {
86ac0575 6877 tree field = lookup_field (context, DECL_NAME (decl), 0, false);
5b605f68 6878 if (field == NULL_TREE || TREE_CODE (field) != VAR_DECL)
33bd39a2 6879 error ("`%#D' is not a static member of `%#T'", decl, context);
e349ee73
MS
6880 else
6881 {
6882 if (DECL_CONTEXT (field) != context)
f2d773a2 6883 {
a723baf1
MM
6884 if (!same_type_p (DECL_CONTEXT (field), context))
6885 pedwarn ("ISO C++ does not permit `%T::%D' to be defined as `%T::%D'",
6886 DECL_CONTEXT (field), DECL_NAME (decl),
6887 context, DECL_NAME (decl));
f2d773a2
JM
6888 DECL_CONTEXT (decl) = DECL_CONTEXT (field);
6889 }
75650646
MM
6890 /* Static data member are tricky; an in-class initialization
6891 still doesn't provide a definition, so the in-class
6892 declaration will have DECL_EXTERNAL set, but will have an
6893 initialization. Thus, duplicate_decls won't warn
6894 about this situation, and so we check here. */
6895 if (DECL_INITIAL (decl) && DECL_INITIAL (field))
33bd39a2 6896 error ("duplicate initialization of %D", decl);
e349ee73
MS
6897 if (duplicate_decls (decl, field))
6898 decl = field;
6899 }
5b605f68 6900 }
f30432d7
MS
6901 else
6902 {
5566b478 6903 tree field = check_classfn (context, decl);
f30432d7
MS
6904 if (field && duplicate_decls (decl, field))
6905 decl = field;
6906 }
6907
6908 /* cp_finish_decl sets DECL_EXTERNAL if DECL_IN_AGGR_P is set. */
dff3e828 6909 DECL_IN_AGGR_P (decl) = 0;
68642fb6 6910 if ((DECL_LANG_SPECIFIC (decl) && DECL_USE_TEMPLATE (decl))
1f6e1acc 6911 || CLASSTYPE_TEMPLATE_INSTANTIATION (context))
84e6233f
JM
6912 {
6913 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
6914 /* [temp.expl.spec] An explicit specialization of a static data
6915 member of a template is a definition if the declaration
6916 includes an initializer; otherwise, it is a declaration.
6917
6918 We check for processing_specialization so this only applies
6919 to the new specialization syntax. */
6920 if (DECL_INITIAL (decl) == NULL_TREE && processing_specialization)
6921 DECL_EXTERNAL (decl) = 1;
6922 }
f30432d7 6923
b7698cf0 6924 if (DECL_EXTERNAL (decl) && ! DECL_TEMPLATE_SPECIALIZATION (decl))
33bd39a2 6925 pedwarn ("declaration of `%#D' outside of class is not definition",
f30432d7 6926 decl);
5b605f68
MS
6927 }
6928
9188c363
MM
6929 /* Enter this declaration into the symbol table. */
6930 tem = maybe_push_decl (decl);
2ee887f2 6931
5156628f 6932 if (processing_template_decl)
cd9f6678 6933 tem = push_template_decl (tem);
5566b478 6934
2ee887f2 6935#if ! defined (ASM_OUTPUT_BSS) && ! defined (ASM_OUTPUT_ALIGNED_BSS)
a3203465 6936 /* Tell the back-end to use or not use .common as appropriate. If we say
a50f0918
MS
6937 -fconserve-space, we want this to save .data space, at the expense of
6938 wrong semantics. If we say -fno-conserve-space, we want this to
6939 produce errors about redefs; to do this we force variables into the
6940 data segment. */
ca5dd8b7
JJ
6941 DECL_COMMON (tem) = ((TREE_CODE (tem) != VAR_DECL
6942 || !DECL_THREAD_LOCAL (tem))
c456a45a 6943 && (flag_conserve_space || ! TREE_PUBLIC (tem)));
2ee887f2 6944#endif
68642fb6 6945
5156628f 6946 if (! processing_template_decl)
5566b478 6947 start_decl_1 (tem);
8d08fdba 6948
8d08fdba
MS
6949 return tem;
6950}
6951
5566b478 6952void
11f6b451 6953start_decl_1 (tree decl)
8d08fdba 6954{
5566b478
MS
6955 tree type = TREE_TYPE (decl);
6956 int initialized = (DECL_INITIAL (decl) != NULL_TREE);
8d08fdba 6957
44689c12
ML
6958 if (type == error_mark_node)
6959 return;
6960
bd0d5d4a 6961 maybe_push_cleanup_level (type);
5566b478
MS
6962
6963 if (initialized)
6964 /* Is it valid for this decl to have an initializer at all?
6965 If not, set INITIALIZED to zero, which will indirectly
6966 tell `cp_finish_decl' to ignore the initializer once it is parsed. */
8d08fdba 6967 {
5566b478
MS
6968 /* Don't allow initializations for incomplete types except for
6969 arrays which might be completed by the initialization. */
d0f062fb 6970 if (COMPLETE_TYPE_P (complete_type (type)))
5566b478
MS
6971 ; /* A complete type is ok. */
6972 else if (TREE_CODE (type) != ARRAY_TYPE)
8d08fdba 6973 {
33bd39a2 6974 error ("variable `%#D' has initializer but incomplete type",
5566b478
MS
6975 decl);
6976 initialized = 0;
25eb19ff 6977 type = TREE_TYPE (decl) = error_mark_node;
5566b478 6978 }
d0f062fb 6979 else if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (type))))
5566b478
MS
6980 {
6981 if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
33bd39a2 6982 error ("elements of array `%#D' have incomplete type", decl);
5566b478
MS
6983 /* else we already gave an error in start_decl. */
6984 initialized = 0;
8d08fdba 6985 }
8d08fdba
MS
6986 }
6987
5566b478
MS
6988 if (!initialized
6989 && TREE_CODE (decl) != TYPE_DECL
6990 && TREE_CODE (decl) != TEMPLATE_DECL
07c88314 6991 && type != error_mark_node
5362b086 6992 && IS_AGGR_TYPE (type)
07c88314 6993 && ! DECL_EXTERNAL (decl))
8d08fdba 6994 {
5156628f 6995 if ((! processing_template_decl || ! uses_template_parms (type))
d0f062fb 6996 && !COMPLETE_TYPE_P (complete_type (type)))
5566b478 6997 {
0154eaa8 6998 error ("aggregate `%#D' has incomplete type and cannot be defined",
5566b478
MS
6999 decl);
7000 /* Change the type so that assemble_variable will give
7001 DECL an rtl we can live with: (mem (const_int 0)). */
25eb19ff 7002 type = TREE_TYPE (decl) = error_mark_node;
5566b478
MS
7003 }
7004 else
7005 {
7006 /* If any base type in the hierarchy of TYPE needs a constructor,
7007 then we set initialized to 1. This way any nodes which are
7008 created for the purposes of initializing this aggregate
7009 will live as long as it does. This is necessary for global
7010 aggregates which do not have their initializers processed until
7011 the end of the file. */
7012 initialized = TYPE_NEEDS_CONSTRUCTING (type);
7013 }
7014 }
7015
5566b478
MS
7016 if (! initialized)
7017 DECL_INITIAL (decl) = NULL_TREE;
7018}
7019
7e99327d
MM
7020/* Handle initialization of references. DECL, TYPE, and INIT have the
7021 same meaning as in cp_finish_decl. *CLEANUP must be NULL on entry,
7022 but will be set to a new CLEANUP_STMT if a temporary is created
7023 that must be destroeyd subsequently.
7024
7025 Returns an initializer expression to use to initialize DECL, or
7026 NULL if the initialization can be performed statically.
e92cc029
MS
7027
7028 Quotes on semantics can be found in ARM 8.4.3. */
7029
8e4ce833 7030static tree
7e99327d 7031grok_reference_init (tree decl, tree type, tree init, tree *cleanup)
5566b478
MS
7032{
7033 tree tmp;
7034
7035 if (init == NULL_TREE)
7036 {
7037 if ((DECL_LANG_SPECIFIC (decl) == 0
7038 || DECL_IN_AGGR_P (decl) == 0)
7039 && ! DECL_THIS_EXTERN (decl))
33bd39a2 7040 error ("`%D' declared as reference but not initialized", decl);
8e4ce833 7041 return NULL_TREE;
5566b478
MS
7042 }
7043
ed5511d9 7044 if (TREE_CODE (init) == CONSTRUCTOR)
5566b478 7045 {
33bd39a2 7046 error ("ISO C++ forbids use of initializer list to initialize reference `%D'", decl);
8e4ce833 7047 return NULL_TREE;
8d08fdba
MS
7048 }
7049
7050 if (TREE_CODE (init) == TREE_LIST)
c7b62f14 7051 init = build_x_compound_expr_from_list (init, "initializer");
8d08fdba 7052
8ccc31eb
MS
7053 if (TREE_CODE (TREE_TYPE (init)) == REFERENCE_TYPE)
7054 init = convert_from_reference (init);
7055
8d08fdba
MS
7056 if (TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE
7057 && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE)
0a72704b
MM
7058 /* Note: default conversion is only called in very special cases. */
7059 init = decay_conversion (init);
68642fb6 7060
24bef158
MM
7061 /* Convert INIT to the reference type TYPE. This may involve the
7062 creation of a temporary, whose lifetime must be the same as that
7063 of the reference. If so, a DECL_STMT for the temporary will be
7064 added just after the DECL_STMT for DECL. That's why we don't set
7065 DECL_INITIAL for local references (instead assigning to them
7066 explicitly); we need to allow the temporary to be initialized
7067 first. */
7e99327d 7068 tmp = initialize_reference (type, init, decl, cleanup);
8d08fdba 7069
a3203465 7070 if (tmp == error_mark_node)
8e4ce833
JJ
7071 return NULL_TREE;
7072 else if (tmp == NULL_TREE)
8d08fdba 7073 {
33bd39a2 7074 error ("cannot initialize `%T' from `%T'", type, TREE_TYPE (init));
8e4ce833 7075 return NULL_TREE;
8d08fdba 7076 }
8d08fdba 7077
8e4ce833
JJ
7078 if (TREE_STATIC (decl) && !TREE_CONSTANT (tmp))
7079 return tmp;
7080
08ac397c 7081 DECL_INITIAL (decl) = tmp;
8e4ce833
JJ
7082
7083 return NULL_TREE;
8d08fdba
MS
7084}
7085
27778b73
MM
7086/* When parsing `int a[] = {1, 2};' we don't know the size of the
7087 array until we finish parsing the initializer. If that's the
7088 situation we're in, update DECL accordingly. */
7089
7090static void
11f6b451 7091maybe_deduce_size_from_array_init (tree decl, tree init)
27778b73
MM
7092{
7093 tree type = TREE_TYPE (decl);
7094
7095 if (TREE_CODE (type) == ARRAY_TYPE
7096 && TYPE_DOMAIN (type) == NULL_TREE
7097 && TREE_CODE (decl) != TYPE_DECL)
7098 {
f2ae0c45
JM
7099 /* do_default is really a C-ism to deal with tentative definitions.
7100 But let's leave it here to ease the eventual merge. */
7101 int do_default = !DECL_EXTERNAL (decl);
27778b73
MM
7102 tree initializer = init ? init : DECL_INITIAL (decl);
7103 int failure = complete_array_type (type, initializer, do_default);
7104
7105 if (failure == 1)
33bd39a2 7106 error ("initializer fails to determine size of `%D'", decl);
27778b73
MM
7107
7108 if (failure == 2)
7109 {
7110 if (do_default)
33bd39a2 7111 error ("array size missing in `%D'", decl);
27778b73
MM
7112 /* If a `static' var's size isn't known, make it extern as
7113 well as static, so it does not get allocated. If it's not
7114 `static', then don't mark it extern; finish_incomplete_decl
7115 will give it a default size and it will get allocated. */
7116 else if (!pedantic && TREE_STATIC (decl) && !TREE_PUBLIC (decl))
7117 DECL_EXTERNAL (decl) = 1;
7118 }
7119
7120 if (pedantic && TYPE_DOMAIN (type) != NULL_TREE
7121 && tree_int_cst_lt (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
7122 integer_zero_node))
33bd39a2 7123 error ("zero-size array `%D'", decl);
27778b73
MM
7124
7125 layout_decl (decl, 0);
7126 }
7127}
7128
7129/* Set DECL_SIZE, DECL_ALIGN, etc. for DECL (a VAR_DECL), and issue
57b52417 7130 any appropriate error messages regarding the layout. */
27778b73 7131
57b52417 7132static void
11f6b451 7133layout_var_decl (tree decl)
27778b73 7134{
57b52417 7135 tree type = TREE_TYPE (decl);
c95cd22e 7136#if 0
57b52417 7137 tree ttype = target_type (type);
c95cd22e 7138#endif
57b52417
MM
7139
7140 /* If we haven't already layed out this declaration, do so now.
7141 Note that we must not call complete type for an external object
7142 because it's type might involve templates that we are not
34cd5ae7 7143 supposed to instantiate yet. (And it's perfectly valid to say
57b52417
MM
7144 `extern X x' for some incomplete type `X'.) */
7145 if (!DECL_EXTERNAL (decl))
7146 complete_type (type);
7de85f7e 7147 if (!DECL_SIZE (decl)
328de7c2 7148 && TREE_TYPE (decl) != error_mark_node
7de85f7e
MM
7149 && (COMPLETE_TYPE_P (type)
7150 || (TREE_CODE (type) == ARRAY_TYPE
7151 && !TYPE_DOMAIN (type)
7152 && COMPLETE_TYPE_P (TREE_TYPE (type)))))
27778b73
MM
7153 layout_decl (decl, 0);
7154
c82dbd95 7155 if (!DECL_EXTERNAL (decl) && DECL_SIZE (decl) == NULL_TREE)
27778b73
MM
7156 {
7157 /* An automatic variable with an incomplete type: that is an error.
7158 Don't talk about array types here, since we took care of that
7159 message in grokdeclarator. */
33bd39a2 7160 error ("storage size of `%D' isn't known", decl);
27778b73
MM
7161 TREE_TYPE (decl) = error_mark_node;
7162 }
ae673f14
JM
7163#if 0
7164 /* Keep this code around in case we later want to control debug info
7165 based on whether a type is "used". (jason 1999-11-11) */
7166
27778b73
MM
7167 else if (!DECL_EXTERNAL (decl) && IS_AGGR_TYPE (ttype))
7168 /* Let debugger know it should output info for this type. */
7169 note_debug_info_needed (ttype);
7170
7171 if (TREE_STATIC (decl) && DECL_CLASS_SCOPE_P (decl))
7172 note_debug_info_needed (DECL_CONTEXT (decl));
ae673f14 7173#endif
27778b73
MM
7174
7175 if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl))
7176 && DECL_SIZE (decl) != NULL_TREE
7177 && ! TREE_CONSTANT (DECL_SIZE (decl)))
7178 {
7179 if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
7180 constant_expression_warning (DECL_SIZE (decl));
7181 else
33bd39a2 7182 error ("storage size of `%D' isn't constant", decl);
27778b73 7183 }
8e4ce833
JJ
7184
7185 if (TREE_STATIC (decl)
7186 && !DECL_ARTIFICIAL (decl)
7187 && current_function_decl
7188 && DECL_CONTEXT (decl) == current_function_decl)
7189 push_local_name (decl);
27778b73
MM
7190}
7191
27778b73
MM
7192/* If a local static variable is declared in an inline function, or if
7193 we have a weak definition, we must endeavor to create only one
7194 instance of the variable at link-time. */
7195
7196static void
11f6b451 7197maybe_commonize_var (tree decl)
27778b73
MM
7198{
7199 /* Static data in a function with comdat linkage also has comdat
7200 linkage. */
7201 if (TREE_STATIC (decl)
7202 /* Don't mess with __FUNCTION__. */
cf74fb86 7203 && ! DECL_ARTIFICIAL (decl)
0b50d7f1
MM
7204 && DECL_FUNCTION_SCOPE_P (decl)
7205 /* Unfortunately, import_export_decl has not always been called
7206 before the function is processed, so we cannot simply check
7207 DECL_COMDAT. */
9e6aaf5e 7208 && (DECL_COMDAT (DECL_CONTEXT (decl))
0b50d7f1
MM
7209 || ((DECL_DECLARED_INLINE_P (DECL_CONTEXT (decl))
7210 || DECL_TEMPLATE_INSTANTIATION (DECL_CONTEXT (decl)))
7211 && TREE_PUBLIC (DECL_CONTEXT (decl)))))
27778b73 7212 {
0dbc5cd3 7213 if (flag_weak)
27778b73 7214 {
0dbc5cd3
MM
7215 /* With weak symbols, we simply make the variable COMDAT;
7216 that will cause copies in multiple translations units to
7217 be merged. */
7218 comdat_linkage (decl);
7219 }
7220 else
7221 {
7222 if (DECL_INITIAL (decl) == NULL_TREE
7223 || DECL_INITIAL (decl) == error_mark_node)
27778b73 7224 {
0dbc5cd3
MM
7225 /* Without weak symbols, we can use COMMON to merge
7226 uninitialized variables. */
27778b73
MM
7227 TREE_PUBLIC (decl) = 1;
7228 DECL_COMMON (decl) = 1;
7229 }
0dbc5cd3 7230 else
27778b73 7231 {
0dbc5cd3
MM
7232 /* While for initialized variables, we must use internal
7233 linkage -- which means that multiple copies will not
7234 be merged. */
7235 TREE_PUBLIC (decl) = 0;
7236 DECL_COMMON (decl) = 0;
27778b73 7237 cp_warning_at ("sorry: semantics of inline function static data `%#D' are wrong (you'll wind up with multiple copies)", decl);
ddd2d57e
RH
7238 warning ("%J you can work around this by removing the initializer",
7239 decl);
27778b73
MM
7240 }
7241 }
7242 }
7243 else if (DECL_LANG_SPECIFIC (decl) && DECL_COMDAT (decl))
7244 /* Set it up again; we might have set DECL_INITIAL since the last
7245 time. */
7246 comdat_linkage (decl);
7247}
7248
91063b51
MM
7249/* Issue an error message if DECL is an uninitialized const variable. */
7250
7251static void
11f6b451 7252check_for_uninitialized_const_var (tree decl)
91063b51
MM
7253{
7254 tree type = TREE_TYPE (decl);
7255
7256 /* ``Unless explicitly declared extern, a const object does not have
7257 external linkage and must be initialized. ($8.4; $12.1)'' ARM
7258 7.1.6 */
7259 if (TREE_CODE (decl) == VAR_DECL
7260 && TREE_CODE (type) != REFERENCE_TYPE
7261 && CP_TYPE_CONST_P (type)
7262 && !TYPE_NEEDS_CONSTRUCTING (type)
7263 && !DECL_INITIAL (decl))
33bd39a2 7264 error ("uninitialized const `%D'", decl);
91063b51
MM
7265}
7266
8e3df2de
MM
7267/* FIELD is a FIELD_DECL or NULL. In the former case, the value
7268 returned is the next FIELD_DECL (possibly FIELD itself) that can be
7269 initialized. If there are no more such fields, the return value
7270 will be NULL. */
7271
7272static tree
7273next_initializable_field (tree field)
7274{
7275 while (field
7276 && (TREE_CODE (field) != FIELD_DECL
7277 || (DECL_C_BIT_FIELD (field) && !DECL_NAME (field))
7278 || DECL_ARTIFICIAL (field)))
7279 field = TREE_CHAIN (field);
7280
7281 return field;
7282}
7283
7284/* Undo the brace-elision allowed by [dcl.init.aggr] in a
7285 brace-enclosed aggregate initializer.
7286
7287 *INITP is one of a list of initializers describing a brace-enclosed
7288 initializer for an entity of the indicated aggregate TYPE. It may
7289 not presently match the shape of the TYPE; for example:
7290
7291 struct S { int a; int b; };
7292 struct S a[] = { 1, 2, 3, 4 };
7293
7294 Here *INITP will point to TREE_LIST of four elements, rather than a
7295 list of two elements, each itself a list of two elements. This
7296 routine transforms INIT from the former form into the latter. The
7297 revised initializer is returned. */
7298
7299static tree
7300reshape_init (tree type, tree *initp)
7301{
7302 tree inits;
7303 tree old_init;
7304 tree old_init_value;
7305 tree new_init;
7306 bool brace_enclosed_p;
7307
7308 old_init = *initp;
7309 old_init_value = (TREE_CODE (*initp) == TREE_LIST
7310 ? TREE_VALUE (*initp) : old_init);
7311
ee20f4ee 7312 my_friendly_assert (old_init_value, 20030723);
ee7ecb29 7313
8e3df2de
MM
7314 /* If the initializer is brace-enclosed, pull initializers from the
7315 enclosed elements. Advance past the brace-enclosed initializer
7316 now. */
dbe85b80
JM
7317 if (TREE_CODE (old_init_value) == CONSTRUCTOR
7318 && TREE_TYPE (old_init_value) == NULL_TREE
8e3df2de
MM
7319 && TREE_HAS_CONSTRUCTOR (old_init_value))
7320 {
7321 *initp = TREE_CHAIN (old_init);
7322 TREE_CHAIN (old_init) = NULL_TREE;
7323 inits = CONSTRUCTOR_ELTS (old_init_value);
7324 initp = &inits;
7325 brace_enclosed_p = true;
7326 }
7327 else
7328 {
7329 inits = NULL_TREE;
7330 brace_enclosed_p = false;
7331 }
7332
7333 /* A non-aggregate type is always initialized with a single
7334 initializer. */
7335 if (!CP_AGGREGATE_TYPE_P (type))
7336 {
7337 *initp = TREE_CHAIN (old_init);
7338 TREE_CHAIN (old_init) = NULL_TREE;
7339 /* It is invalid to initialize a non-aggregate type with a
7340 brace-enclosed initializer. */
7341 if (brace_enclosed_p)
7342 {
7343 error ("brace-enclosed initializer used to initialize `%T'",
7344 type);
7345 if (TREE_CODE (old_init) == TREE_LIST)
7346 TREE_VALUE (old_init) = error_mark_node;
7347 else
7348 old_init = error_mark_node;
7349 }
7350
7351 return old_init;
7352 }
7353
7354 /* [dcl.init.aggr]
7355
7356 All implicit type conversions (clause _conv_) are considered when
7357 initializing the aggregate member with an initializer from an
7358 initializer-list. If the initializer can initialize a member,
7359 the member is initialized. Otherwise, if the member is itself a
7360 non-empty subaggregate, brace elision is assumed and the
7361 initializer is considered for the initialization of the first
7362 member of the subaggregate. */
dbe85b80 7363 if (!brace_enclosed_p
8e3df2de
MM
7364 && can_convert_arg (type, TREE_TYPE (old_init_value), old_init_value))
7365 {
7366 *initp = TREE_CHAIN (old_init);
7367 TREE_CHAIN (old_init) = NULL_TREE;
7368 return old_init;
7369 }
7370
3fa3c4bd 7371 if (TREE_CODE (old_init_value) == STRING_CST
8e3df2de 7372 && TREE_CODE (type) == ARRAY_TYPE
42328048 7373 && char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type))))
8e3df2de
MM
7374 {
7375 /* [dcl.init.string]
7376
7377 A char array (whether plain char, signed char, or unsigned char)
7378 can be initialized by a string-literal (optionally enclosed in
7379 braces); a wchar_t array can be initialized by a wide
7380 string-literal (optionally enclosed in braces). */
7381 new_init = old_init;
7382 /* Move past the initializer. */
7383 *initp = TREE_CHAIN (old_init);
7384 TREE_CHAIN (old_init) = NULL_TREE;
7385 }
7386 else
7387 {
7388 /* Build a CONSTRUCTOR to hold the contents of the aggregate. */
dcf92453 7389 new_init = build_constructor (type, NULL_TREE);
8e3df2de
MM
7390 TREE_HAS_CONSTRUCTOR (new_init) = 1;
7391
7392 if (CLASS_TYPE_P (type))
7393 {
7394 tree field;
7395
7396 field = next_initializable_field (TYPE_FIELDS (type));
7397
7398 if (!field)
7399 {
7400 /* [dcl.init.aggr]
7401
7402 An initializer for an aggregate member that is an
7403 empty class shall have the form of an empty
7404 initializer-list {}. */
7405 if (!brace_enclosed_p)
7406 error ("initializer for `%T' must be brace-enclosed",
7407 type);
7408 }
7409 else
7410 {
7411 /* Loop through the initializable fields, gathering
7412 initializers. */
446d5e47 7413 while (*initp)
8e3df2de
MM
7414 {
7415 tree field_init;
7416
446d5e47
MM
7417 /* Handle designated initializers, as an extension. */
7418 if (TREE_PURPOSE (*initp))
7419 {
7420 if (pedantic)
7421 pedwarn ("ISO C++ does not allow designated initializers");
7422 field = lookup_field_1 (type, TREE_PURPOSE (*initp),
7423 /*want_type=*/false);
7424 if (!field || TREE_CODE (field) != FIELD_DECL)
7425 error ("`%T' has no non-static data member named `%D'",
7426 type, TREE_PURPOSE (*initp));
7427 }
7428 if (!field)
7429 break;
7430
8e3df2de
MM
7431 field_init = reshape_init (TREE_TYPE (field), initp);
7432 TREE_CHAIN (field_init) = CONSTRUCTOR_ELTS (new_init);
7433 CONSTRUCTOR_ELTS (new_init) = field_init;
7434 /* [dcl.init.aggr]
7435
7436 When a union is initialized with a brace-enclosed
7437 initializer, the braces shall only contain an
7438 initializer for the first member of the union. */
7439 if (TREE_CODE (type) == UNION_TYPE)
7440 break;
8e3df2de
MM
7441 field = next_initializable_field (TREE_CHAIN (field));
7442 }
7443 }
7444 }
7445 else if (TREE_CODE (type) == ARRAY_TYPE)
7446 {
7447 tree index;
7448 tree max_index;
7449
7450 /* If the bound of the array is known, take no more initializers
7451 than are allowed. */
7452 max_index = (TYPE_DOMAIN (type)
7453 ? array_type_nelts (type) : NULL_TREE);
7454 /* Loop through the array elements, gathering initializers. */
7455 for (index = size_zero_node;
7456 *initp && (!max_index || !tree_int_cst_lt (max_index, index));
7457 index = size_binop (PLUS_EXPR, index, size_one_node))
7458 {
7459 tree element_init;
7460
7461 element_init = reshape_init (TREE_TYPE (type), initp);
7462 TREE_CHAIN (element_init) = CONSTRUCTOR_ELTS (new_init);
7463 CONSTRUCTOR_ELTS (new_init) = element_init;
7464 if (TREE_PURPOSE (element_init))
7465 index = TREE_PURPOSE (element_init);
7466 }
7467 }
7468 else
7469 abort ();
7470
7471 /* The initializers were placed in reverse order in the
7472 CONSTRUCTOR. */
7473 CONSTRUCTOR_ELTS (new_init) = nreverse (CONSTRUCTOR_ELTS (new_init));
7474
7475 if (TREE_CODE (old_init) == TREE_LIST)
7476 new_init = build_tree_list (TREE_PURPOSE (old_init), new_init);
7477 }
7478
7479 /* If this was a brace-enclosed initializer and all of the
7480 initializers were not used up, there is a problem. */
7481 if (brace_enclosed_p && *initp)
7482 error ("too many initializers for `%T'", type);
7483
7484 return new_init;
7485}
7486
c82dbd95 7487/* Verify INIT (the initializer for DECL), and record the
7e99327d
MM
7488 initialization in DECL_INITIAL, if appropriate. CLEANUP is as for
7489 grok_reference_init.
8e3df2de
MM
7490
7491 If the return value is non-NULL, it is an expression that must be
7492 evaluated dynamically to initialize DECL. */
27778b73 7493
c82dbd95 7494static tree
7e99327d 7495check_initializer (tree decl, tree init, int flags, tree *cleanup)
27778b73 7496{
17bbb839 7497 tree type = TREE_TYPE (decl);
fc0e7bf5 7498
27778b73
MM
7499 /* If `start_decl' didn't like having an initialization, ignore it now. */
7500 if (init != NULL_TREE && DECL_INITIAL (decl) == NULL_TREE)
7501 init = NULL_TREE;
27778b73 7502
17bbb839
MM
7503 /* If an initializer is present, DECL_INITIAL has been
7504 error_mark_node, to indicate that an as-of-yet unevaluated
7505 initialization will occur. From now on, DECL_INITIAL reflects
7506 the static initialization -- if any -- of DECL. */
7507 DECL_INITIAL (decl) = NULL_TREE;
7508
328de7c2
MM
7509 /* Things that are going to be initialized need to have complete
7510 type. */
7511 TREE_TYPE (decl) = type = complete_type (TREE_TYPE (decl));
c82dbd95 7512
328de7c2
MM
7513 if (type == error_mark_node)
7514 /* We will have already complained. */
7515 init = NULL_TREE;
7516 else if (init && COMPLETE_TYPE_P (type)
7517 && !TREE_CONSTANT (TYPE_SIZE (type)))
7518 {
7519 error ("variable-sized object `%D' may not be initialized", decl);
7520 init = NULL_TREE;
7521 }
7522 else if (TREE_CODE (type) == ARRAY_TYPE
7523 && !COMPLETE_TYPE_P (complete_type (TREE_TYPE (type))))
7524 {
7525 error ("elements of array `%#D' have incomplete type", decl);
7526 init = NULL_TREE;
7527 }
7528 else if (TREE_CODE (type) != ARRAY_TYPE && !COMPLETE_TYPE_P (type))
7529 {
7530 error ("`%D' has incomplete type", decl);
7531 TREE_TYPE (decl) = error_mark_node;
7532 init = NULL_TREE;
27778b73
MM
7533 }
7534
7535 if (TREE_CODE (decl) == CONST_DECL)
7536 {
7537 my_friendly_assert (TREE_CODE (decl) != REFERENCE_TYPE, 148);
7538
7539 DECL_INITIAL (decl) = init;
7540
27778b73
MM
7541 my_friendly_assert (init != NULL_TREE, 149);
7542 init = NULL_TREE;
7543 }
c82dbd95 7544 else if (!DECL_EXTERNAL (decl) && TREE_CODE (type) == REFERENCE_TYPE)
7e99327d 7545 init = grok_reference_init (decl, type, init, cleanup);
27778b73
MM
7546 else if (init)
7547 {
8e3df2de 7548 if (TREE_CODE (init) == CONSTRUCTOR && TREE_HAS_CONSTRUCTOR (init))
a16f2357
GK
7549 {
7550 /* [dcl.init] paragraph 13,
7551 If T is a scalar type, then a declaration of the form
7552 T x = { a };
7553 is equivalent to
7554 T x = a;
7555
7556 reshape_init will complain about the extra braces,
7557 and doesn't do anything useful in the case where TYPE is
7558 scalar, so just don't call it. */
7559 if (CP_AGGREGATE_TYPE_P (type))
7560 init = reshape_init (type, &init);
6e1b3a7c
AH
7561
7562 if ((*targetm.vector_opaque_p) (type))
7563 {
7564 error ("opaque vector types cannot be initialized");
7565 init = error_mark_node;
7566 }
a16f2357 7567 }
8e3df2de
MM
7568
7569 /* If DECL has an array type without a specific bound, deduce the
7570 array size from the initializer. */
7571 maybe_deduce_size_from_array_init (decl, init);
7572 type = TREE_TYPE (decl);
7573 if (TREE_CODE (init) == CONSTRUCTOR && TREE_HAS_CONSTRUCTOR (init))
7574 TREE_TYPE (init) = type;
7575
27778b73
MM
7576 if (TYPE_HAS_CONSTRUCTOR (type) || TYPE_NEEDS_CONSTRUCTING (type))
7577 {
7578 if (TREE_CODE (type) == ARRAY_TYPE)
8e3df2de 7579 goto initialize_aggr;
27778b73
MM
7580 else if (TREE_CODE (init) == CONSTRUCTOR
7581 && TREE_HAS_CONSTRUCTOR (init))
7582 {
7583 if (TYPE_NON_AGGREGATE_CLASS (type))
7584 {
33bd39a2 7585 error ("`%D' must be initialized by constructor, not by `{...}'",
8e3df2de 7586 decl);
27778b73
MM
7587 init = error_mark_node;
7588 }
7589 else
7590 goto dont_use_constructor;
7591 }
8e3df2de
MM
7592 else
7593 {
7594 int saved_stmts_are_full_exprs_p;
7595
7596 initialize_aggr:
7597 saved_stmts_are_full_exprs_p = 0;
7598 if (building_stmt_tree ())
7599 {
7600 saved_stmts_are_full_exprs_p = stmts_are_full_exprs_p ();
7601 current_stmt_tree ()->stmts_are_full_exprs_p = 1;
7602 }
7603 init = build_aggr_init (decl, init, flags);
7604 if (building_stmt_tree ())
7605 current_stmt_tree ()->stmts_are_full_exprs_p =
7606 saved_stmts_are_full_exprs_p;
7607 return init;
7608 }
27778b73
MM
7609 }
7610 else
7611 {
7612 dont_use_constructor:
7613 if (TREE_CODE (init) != TREE_VEC)
7614 init = store_init_value (decl, init);
7615 }
27778b73
MM
7616 }
7617 else if (DECL_EXTERNAL (decl))
7618 ;
8e3df2de
MM
7619 else if (TYPE_P (type) && TYPE_NEEDS_CONSTRUCTING (type))
7620 goto initialize_aggr;
7621 else if (IS_AGGR_TYPE (type))
27778b73
MM
7622 {
7623 tree core_type = strip_array_types (type);
7624
8e3df2de
MM
7625 if (CLASSTYPE_READONLY_FIELDS_NEED_INIT (core_type))
7626 error ("structure `%D' with uninitialized const members", decl);
7627 if (CLASSTYPE_REF_FIELDS_NEED_INIT (core_type))
7628 error ("structure `%D' with uninitialized reference members",
7629 decl);
27778b73
MM
7630
7631 check_for_uninitialized_const_var (decl);
27778b73
MM
7632 }
7633 else
7634 check_for_uninitialized_const_var (decl);
68642fb6 7635
8e3df2de
MM
7636 if (init && init != error_mark_node)
7637 init = build (INIT_EXPR, type, decl, init);
7638
c82dbd95 7639 return init;
27778b73
MM
7640}
7641
7642/* If DECL is not a local variable, give it RTL. */
7643
7644static void
11f6b451 7645make_rtl_for_nonlocal_decl (tree decl, tree init, const char* asmspec)
27778b73 7646{
95ee998c
MM
7647 int toplev = toplevel_bindings_p ();
7648 int defer_p;
27778b73 7649
f39ee884
MM
7650 /* Handle non-variables up front. */
7651 if (TREE_CODE (decl) != VAR_DECL)
7652 {
7653 rest_of_decl_compilation (decl, asmspec, toplev, at_eof);
7654 return;
7655 }
7656
95ee998c
MM
7657 /* If we see a class member here, it should be a static data
7658 member. */
7659 if (DECL_LANG_SPECIFIC (decl) && DECL_IN_AGGR_P (decl))
7660 {
7661 my_friendly_assert (TREE_STATIC (decl), 19990828);
7662 /* An in-class declaration of a static data member should be
7663 external; it is only a declaration, and not a definition. */
7664 if (init == NULL_TREE)
7665 my_friendly_assert (DECL_EXTERNAL (decl), 20000723);
7666 }
7667
f39ee884
MM
7668 /* Set the DECL_ASSEMBLER_NAME for the variable. */
7669 if (asmspec)
7ed47c04
MM
7670 {
7671 SET_DECL_ASSEMBLER_NAME (decl, get_identifier (asmspec));
7672 /* The `register' keyword, when used together with an
7673 asm-specification, indicates that the variable should be
7674 placed in a particular register. */
7675 if (DECL_REGISTER (decl))
7676 DECL_C_HARD_REGISTER (decl) = 1;
7677 }
f39ee884 7678
95ee998c
MM
7679 /* We don't create any RTL for local variables. */
7680 if (DECL_FUNCTION_SCOPE_P (decl) && !TREE_STATIC (decl))
7681 return;
27778b73 7682
95ee998c
MM
7683 /* We defer emission of local statics until the corresponding
7684 DECL_STMT is expanded. */
7685 defer_p = DECL_FUNCTION_SCOPE_P (decl) || DECL_VIRTUAL_P (decl);
7686
b602511f
JM
7687 /* We try to defer namespace-scope static constants so that they are
7688 not emitted into the object file unnecessarily. */
7689 if (!DECL_VIRTUAL_P (decl)
7690 && TREE_READONLY (decl)
7691 && DECL_INITIAL (decl) != NULL_TREE
7692 && DECL_INITIAL (decl) != error_mark_node
7693 && ! EMPTY_CONSTRUCTOR_P (DECL_INITIAL (decl))
7694 && toplev
7695 && !TREE_PUBLIC (decl))
5cc90635
JM
7696 {
7697 /* Fool with the linkage of static consts according to #pragma
7698 interface. */
7699 if (!interface_unknown && !TREE_PUBLIC (decl))
27778b73 7700 {
95ee998c
MM
7701 TREE_PUBLIC (decl) = 1;
7702 DECL_EXTERNAL (decl) = interface_only;
27778b73 7703 }
27778b73 7704
95ee998c 7705 defer_p = 1;
27778b73 7706 }
b602511f
JM
7707 /* Likewise for template instantiations. */
7708 else if (DECL_COMDAT (decl))
7709 defer_p = 1;
95ee998c 7710
92643fea
MM
7711 /* If we're deferring the variable, we only need to make RTL if
7712 there's an ASMSPEC. Otherwise, we'll lazily create it later when
7713 we need it. (There's no way to lazily create RTL for things that
7714 have assembly specs because the information about the specifier
7715 isn't stored in the tree, yet) */
7716 if (defer_p && asmspec)
6c418184 7717 make_decl_rtl (decl, asmspec);
95ee998c 7718 /* If we're not deferring, go ahead and assemble the variable. */
92643fea 7719 else if (!defer_p)
27778b73 7720 rest_of_decl_compilation (decl, asmspec, toplev, at_eof);
27778b73
MM
7721}
7722
7723/* The old ARM scoping rules injected variables declared in the
7724 initialization statement of a for-statement into the surrounding
7725 scope. We support this usage, in order to be backward-compatible.
7726 DECL is a just-declared VAR_DECL; if necessary inject its
7727 declaration into the surrounding scope. */
7728
b7b8bcd2 7729void
11f6b451 7730maybe_inject_for_scope_var (tree decl)
27778b73 7731{
22ffcc6f 7732 timevar_push (TV_NAME_LOOKUP);
c3783399 7733 if (!DECL_NAME (decl))
de94b46c
KG
7734 {
7735 timevar_pop (TV_NAME_LOOKUP);
7736 return;
7737 }
6b607ffd
MM
7738
7739 /* Declarations of __FUNCTION__ and its ilk appear magically when
7740 the variable is first used. If that happens to be inside a
7741 for-loop, we don't want to do anything special. */
7742 if (DECL_PRETTY_FUNCTION_P (decl))
de94b46c
KG
7743 {
7744 timevar_pop (TV_NAME_LOOKUP);
7745 return;
7746 }
5362b086 7747
a7e8c268 7748 if (current_binding_level->kind == sk_for)
27778b73 7749 {
e2500fed 7750 struct cp_binding_level *outer
27778b73
MM
7751 = current_binding_level->level_chain;
7752
7753 /* Check to see if the same name is already bound at the outer
7754 level, either because it was directly declared, or because a
7755 dead for-decl got preserved. In either case, the code would
7756 not have been valid under the ARM scope rules, so clear
7757 is_for_scope for the current_binding_level.
7758
7759 Otherwise, we need to preserve the temp slot for decl to last
7760 into the outer binding level. */
7761
af6fd53f
GDR
7762 cxx_binding *outer_binding
7763 = IDENTIFIER_BINDING (DECL_NAME (decl))->previous;
68642fb6 7764
147135cc
GDR
7765 if (outer_binding && outer_binding->scope == outer
7766 && (TREE_CODE (outer_binding->value) == VAR_DECL)
7767 && DECL_DEAD_FOR_LOCAL (outer_binding->value))
27778b73 7768 {
147135cc 7769 outer_binding->value = DECL_SHADOWED_FOR_VAR (outer_binding->value);
a7e8c268 7770 current_binding_level->kind = sk_block;
27778b73 7771 }
27778b73 7772 }
22ffcc6f 7773 timevar_pop (TV_NAME_LOOKUP);
27778b73
MM
7774}
7775
ed5511d9 7776/* Generate code to initialize DECL (a local variable). */
27778b73 7777
8e3df2de 7778static void
11f6b451 7779initialize_local_var (tree decl, tree init)
27778b73 7780{
9ed9e79a 7781 tree type = TREE_TYPE (decl);
170b020f 7782 tree cleanup;
27778b73 7783
8e3df2de
MM
7784 my_friendly_assert (TREE_CODE (decl) == VAR_DECL
7785 || TREE_CODE (decl) == RESULT_DECL,
7786 20021010);
7787 my_friendly_assert (!TREE_STATIC (decl), 20021010);
b7b8bcd2 7788
8e3df2de 7789 if (DECL_SIZE (decl) == NULL_TREE)
b7b8bcd2
MM
7790 {
7791 /* If we used it already as memory, it must stay in memory. */
7792 DECL_INITIAL (decl) = NULL_TREE;
7793 TREE_ADDRESSABLE (decl) = TREE_USED (decl);
7794 }
7795
27778b73
MM
7796 if (DECL_SIZE (decl) && type != error_mark_node)
7797 {
7798 int already_used;
68642fb6 7799
27778b73 7800 /* Compute and store the initial value. */
27778b73
MM
7801 already_used = TREE_USED (decl) || TREE_USED (type);
7802
8e3df2de
MM
7803 /* Perform the initialization. */
7804 if (init)
27778b73 7805 {
24bef158
MM
7806 int saved_stmts_are_full_exprs_p;
7807
3a0d3e1e 7808 my_friendly_assert (building_stmt_tree (), 20000906);
f2c5f623 7809 saved_stmts_are_full_exprs_p = stmts_are_full_exprs_p ();
ae499cce 7810 current_stmt_tree ()->stmts_are_full_exprs_p = 1;
8e3df2de 7811 finish_expr_stmt (init);
5362b086 7812 current_stmt_tree ()->stmts_are_full_exprs_p =
ae499cce 7813 saved_stmts_are_full_exprs_p;
27778b73
MM
7814 }
7815
7816 /* Set this to 0 so we can tell whether an aggregate which was
7817 initialized was ever used. Don't do this if it has a
7818 destructor, so we don't complain about the 'resource
b7b8bcd2
MM
7819 allocation is initialization' idiom. Now set
7820 attribute((unused)) on types so decls of that type will be
7821 marked used. (see TREE_USED, above.) */
27778b73
MM
7822 if (TYPE_NEEDS_CONSTRUCTING (type)
7823 && ! already_used
834c6dff 7824 && TYPE_HAS_TRIVIAL_DESTRUCTOR (type)
27778b73
MM
7825 && DECL_NAME (decl))
7826 TREE_USED (decl) = 0;
b7b8bcd2 7827 else if (already_used)
27778b73
MM
7828 TREE_USED (decl) = 1;
7829 }
24bef158 7830
8e3df2de 7831 /* Generate a cleanup, if necessary. */
170b020f
MM
7832 cleanup = cxx_maybe_build_cleanup (decl);
7833 if (DECL_SIZE (decl) && cleanup)
7834 finish_decl_cleanup (decl, cleanup);
24bef158
MM
7835}
7836
8d08fdba
MS
7837/* Finish processing of a declaration;
7838 install its line number and initial value.
7839 If the length of an array type is not known before,
7840 it must be determined now, from the initial value, or it is an error.
7841
8b27e9ef 7842 INIT holds the value of an initializer that should be allowed to escape
8d08fdba
MS
7843 the normal rules.
7844
920f9474 7845 FLAGS is LOOKUP_ONLYCONVERTING if the = init syntax was used, else 0
f78c7bc6 7846 if the (init) syntax was used. */
8d08fdba
MS
7847
7848void
11f6b451 7849cp_finish_decl (tree decl, tree init, tree asmspec_tree, int flags)
8d08fdba 7850{
7e99327d 7851 tree type;
27778b73 7852 tree ttype = NULL_TREE;
7e99327d 7853 tree cleanup;
9c0758dd 7854 const char *asmspec = NULL;
8d08fdba
MS
7855 int was_readonly = 0;
7856
8d08fdba
MS
7857 if (! decl)
7858 {
7859 if (init)
8251199e 7860 error ("assignment (not initialization) in declaration");
8d08fdba
MS
7861 return;
7862 }
7863
170b020f
MM
7864 my_friendly_assert (TREE_CODE (decl) != RESULT_DECL, 20030619);
7865
7e99327d
MM
7866 /* Assume no cleanup is required. */
7867 cleanup = NULL_TREE;
7868
a4443a08 7869 /* If a name was specified, get the string. */
5f52c0e0 7870 if (global_scope_p (current_binding_level))
41c64394 7871 asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
8d08fdba 7872 if (asmspec_tree)
5cc90635 7873 asmspec = TREE_STRING_POINTER (asmspec_tree);
8d08fdba 7874
2c73f9f5
ML
7875 if (init && TREE_CODE (init) == NAMESPACE_DECL)
7876 {
33bd39a2 7877 error ("cannot initialize `%D' to namespace `%D'",
2c73f9f5
ML
7878 decl, init);
7879 init = NULL_TREE;
7880 }
7881
6ba89f8e 7882 if (current_class_type
4f1c5b7d 7883 && CP_DECL_CONTEXT (decl) == current_class_type
6ba89f8e
MM
7884 && TYPE_BEING_DEFINED (current_class_type)
7885 && (DECL_INITIAL (decl) || init))
3febd123 7886 DECL_INITIALIZED_IN_CLASS_P (decl) = 1;
6ba89f8e 7887
68642fb6 7888 if (TREE_CODE (decl) == VAR_DECL
9a68c51f
JM
7889 && DECL_CONTEXT (decl)
7890 && TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL
7891 && DECL_CONTEXT (decl) != current_namespace
7892 && init)
7893 {
a1c65f9f 7894 /* Leave the namespace of the object. */
9a68c51f
JM
7895 pop_decl_namespace ();
7896 }
7897
c82dbd95 7898 type = TREE_TYPE (decl);
8d08fdba 7899
f376e137 7900 if (type == error_mark_node)
df7f7b9c 7901 goto finish_end0;
5362b086 7902
a7a7710d
NS
7903 if (TYPE_HAS_MUTABLE_P (type))
7904 TREE_READONLY (decl) = 0;
24bef158 7905
5156628f 7906 if (processing_template_decl)
5566b478 7907 {
08ac397c 7908 /* Add this declaration to the statement-tree. */
170b020f 7909 if (at_function_scope_p ())
08ac397c
JM
7910 add_decl_stmt (decl);
7911
5566b478 7912 if (init && DECL_INITIAL (decl))
2a1e9fdd 7913 DECL_INITIAL (decl) = init;
5566b478
MS
7914 goto finish_end0;
7915 }
3e41d13b 7916
27778b73
MM
7917 /* Parameters are handled by store_parm_decls, not cp_finish_decl. */
7918 my_friendly_assert (TREE_CODE (decl) != PARM_DECL, 19990828);
7919
8d08fdba
MS
7920 /* Take care of TYPE_DECLs up front. */
7921 if (TREE_CODE (decl) == TYPE_DECL)
7922 {
a0a33927
MS
7923 if (type != error_mark_node
7924 && IS_AGGR_TYPE (type) && DECL_NAME (decl))
8d08fdba
MS
7925 {
7926 if (TREE_TYPE (DECL_NAME (decl)) && TREE_TYPE (decl) != type)
33bd39a2 7927 warning ("shadowing previous type declaration of `%#D'", decl);
4b0d3cbe 7928 set_identifier_type_value (DECL_NAME (decl), decl);
8d08fdba 7929 }
cffa8729
MS
7930
7931 /* If we have installed this as the canonical typedef for this
7932 type, and that type has not been defined yet, delay emitting
956d6950 7933 the debug information for it, as we will emit it later. */
d2e5ee5c 7934 if (TYPE_MAIN_DECL (TREE_TYPE (decl)) == decl
d0f062fb 7935 && !COMPLETE_TYPE_P (TREE_TYPE (decl)))
cffa8729
MS
7936 TYPE_DECL_SUPPRESS_DEBUG (decl) = 1;
7937
3e411c3f 7938 rest_of_decl_compilation (decl, NULL,
5566b478 7939 DECL_CONTEXT (decl) == NULL_TREE, at_eof);
8d08fdba
MS
7940 goto finish_end;
7941 }
3e41d13b 7942
8d08fdba 7943 if (TREE_CODE (decl) != FUNCTION_DECL)
3e41d13b 7944 ttype = target_type (type);
8d08fdba
MS
7945
7946 if (! DECL_EXTERNAL (decl) && TREE_READONLY (decl)
e455bc27
MM
7947 && (TYPE_NEEDS_CONSTRUCTING (type)
7948 || TREE_CODE (type) == REFERENCE_TYPE))
8d08fdba 7949 {
8d08fdba
MS
7950 /* Currently, GNU C++ puts constants in text space, making them
7951 impossible to initialize. In the future, one would hope for
7952 an operating system which understood the difference between
7953 initialization and the running of a program. */
7954 was_readonly = 1;
7955 TREE_READONLY (decl) = 0;
7956 }
7957
27778b73 7958 if (TREE_CODE (decl) == FIELD_DECL && asmspec)
8d08fdba 7959 {
27778b73
MM
7960 /* This must override the asm specifier which was placed by
7961 grokclassfn. Lay this out fresh. */
19e7881c 7962 SET_DECL_RTL (TREE_TYPE (decl), NULL_RTX);
92643fea 7963 SET_DECL_ASSEMBLER_NAME (decl, get_identifier (asmspec));
6c418184 7964 make_decl_rtl (decl, asmspec);
8d08fdba 7965 }
17bbb839
MM
7966 else if (TREE_CODE (decl) == VAR_DECL)
7967 {
7968 /* Only PODs can have thread-local storage. Other types may require
7969 various kinds of non-trivial initialization. */
7970 if (DECL_THREAD_LOCAL (decl) && !pod_type_p (TREE_TYPE (decl)))
7971 error ("`%D' cannot be thread-local because it has non-POD type `%T'",
7972 decl, TREE_TYPE (decl));
7973 /* Convert the initializer to the type of DECL, if we have not
7974 already initialized DECL. */
7975 if (!DECL_INITIALIZED_P (decl)
7976 /* If !DECL_EXTERNAL then DECL is being defined. In the
8e3df2de 7977 case of a static data member initialized inside the
17bbb839
MM
7978 class-specifier, there can be an initializer even if DECL
7979 is *not* defined. */
7980 && (!DECL_EXTERNAL (decl) || init))
7981 {
7e99327d 7982 init = check_initializer (decl, init, flags, &cleanup);
b4c20e52
MM
7983 /* Thread-local storage cannot be dynamically initialized. */
7984 if (DECL_THREAD_LOCAL (decl) && init)
7985 {
7986 error ("`%D' is thread-local and so cannot be dynamically "
7987 "initialized", decl);
7988 init = NULL_TREE;
7989 }
17bbb839
MM
7990 /* Handle:
7991
7992 [dcl.init]
7993
7994 The memory occupied by any object of static storage
7995 duration is zero-initialized at program startup before
7996 any other initialization takes place.
7997
7998 We cannot create an appropriate initializer until after
7999 the type of DECL is finalized. If DECL_INITIAL is set,
8000 then the DECL is statically initialized, and any
8001 necessary zero-initialization has already been performed. */
8002 if (TREE_STATIC (decl) && !DECL_INITIAL (decl))
8003 DECL_INITIAL (decl) = build_zero_init (TREE_TYPE (decl),
1cb8292f 8004 /*nelts=*/NULL_TREE,
17bbb839
MM
8005 /*static_storage_p=*/true);
8006 /* Remember that the initialization for this variable has
8007 taken place. */
8008 DECL_INITIALIZED_P (decl) = 1;
8009 }
d881ff25
MM
8010 /* If the variable has an array type, lay out the type, even if
8011 there is no initializer. It is valid to index through the
8012 array, and we must get TYPE_ALIGN set correctly on the array
8013 type. */
8014 else if (TREE_CODE (type) == ARRAY_TYPE)
8015 layout_type (type);
17bbb839 8016 }
3e41d13b 8017
57cf57fb
JM
8018 /* Add this declaration to the statement-tree. This needs to happen
8019 after the call to check_initializer so that the DECL_STMT for a
8020 reference temp is added before the DECL_STMT for the reference itself. */
170b020f 8021 if (at_function_scope_p ())
08ac397c
JM
8022 add_decl_stmt (decl);
8023
8d08fdba 8024 if (TREE_CODE (decl) == VAR_DECL)
57b52417 8025 layout_var_decl (decl);
8d08fdba
MS
8026
8027 /* Output the assembler code and/or RTL code for variables and functions,
8028 unless the type is an undefined structure or union.
8029 If not, it will get done when the type is completed. */
170b020f 8030 if (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == FUNCTION_DECL)
8d08fdba 8031 {
27778b73
MM
8032 if (TREE_CODE (decl) == VAR_DECL)
8033 maybe_commonize_var (decl);
8d08fdba 8034
27778b73 8035 make_rtl_for_nonlocal_decl (decl, init, asmspec);
8d08fdba 8036
68642fb6 8037 if (TREE_CODE (type) == FUNCTION_TYPE
27778b73 8038 || TREE_CODE (type) == METHOD_TYPE)
68642fb6 8039 abstract_virtuals_error (decl,
27778b73 8040 strip_array_types (TREE_TYPE (type)));
68642fb6 8041 else
27778b73 8042 abstract_virtuals_error (decl, strip_array_types (type));
8d08fdba 8043
8e3df2de
MM
8044 if (TREE_CODE (decl) == FUNCTION_DECL
8045 || TREE_TYPE (decl) == error_mark_node)
8046 /* No initialization required. */
faae18ab 8047 ;
67d743fe
MS
8048 else if (DECL_EXTERNAL (decl)
8049 && ! (DECL_LANG_SPECIFIC (decl)
8050 && DECL_NOT_REALLY_EXTERN (decl)))
5566b478
MS
8051 {
8052 if (init)
8053 DECL_INITIAL (decl) = init;
8054 }
8e3df2de 8055 else
8d08fdba 8056 {
8e3df2de
MM
8057 /* A variable definition. */
8058 if (DECL_FUNCTION_SCOPE_P (decl))
24bef158 8059 {
8e3df2de 8060 /* This is a local declaration. */
a7e8c268 8061 maybe_inject_for_scope_var (decl);
8e3df2de
MM
8062 /* Initialize the local variable. */
8063 if (processing_template_decl)
8064 {
8065 if (init || DECL_INITIAL (decl) == error_mark_node)
8066 DECL_INITIAL (decl) = init;
8067 }
8068 else if (!TREE_STATIC (decl))
8069 initialize_local_var (decl, init);
24bef158 8070 }
8e3df2de
MM
8071
8072 if (TREE_STATIC (decl))
9ed9e79a
MM
8073 expand_static_init (decl, init);
8074 }
8d08fdba
MS
8075 finish_end0:
8076
8077 /* Undo call to `pushclass' that was done in `start_decl'
8078 due to initialization of qualified member variable.
8079 I.e., Foo::x = 10; */
8080 {
4f1c5b7d 8081 tree context = CP_DECL_CONTEXT (decl);
8d08fdba 8082 if (context
2f939d94 8083 && TYPE_P (context)
8d08fdba
MS
8084 && (TREE_CODE (decl) == VAR_DECL
8085 /* We also have a pushclass done that we need to undo here
8086 if we're at top level and declare a method. */
5566b478
MS
8087 || TREE_CODE (decl) == FUNCTION_DECL)
8088 /* If size hasn't been set, we're still defining it,
8089 and therefore inside the class body; don't pop
8090 the binding level.. */
d0f062fb 8091 && COMPLETE_TYPE_P (context)
5566b478 8092 && context == current_class_type)
6b400b21 8093 pop_nested_class ();
8d08fdba
MS
8094 }
8095 }
8096
7e99327d
MM
8097 /* If a CLEANUP_STMT was created to destroy a temporary bound to a
8098 reference, insert it in the statement-tree now. */
8099 if (cleanup)
8100 add_stmt (cleanup);
8101
8d08fdba
MS
8102 finish_end:
8103
8d08fdba
MS
8104 if (was_readonly)
8105 TREE_READONLY (decl) = 1;
d1bd0ded
GK
8106
8107 /* If this was marked 'used', be sure it will be output. */
8108 if (lookup_attribute ("used", DECL_ATTRIBUTES (decl)))
8109 mark_referenced (DECL_ASSEMBLER_NAME (decl));
8d08fdba
MS
8110}
8111
82580166 8112/* This is here for a midend callback from c-common.c */
e92cc029 8113
82580166 8114void
11f6b451 8115finish_decl (tree decl, tree init, tree asmspec_tree)
82580166 8116{
cd9f6678 8117 cp_finish_decl (decl, init, asmspec_tree, 0);
82580166
MS
8118}
8119
db4283a0
MM
8120/* Returns a declaration for a VAR_DECL as if:
8121
8122 extern "C" TYPE NAME;
8123
8124 had been seen. Used to create compiler-generated global
8125 variables. */
8126
8127tree
11f6b451 8128declare_global_var (tree name, tree type)
db4283a0
MM
8129{
8130 tree decl;
8131
8132 push_to_top_level ();
8133 decl = build_decl (VAR_DECL, name, type);
8134 TREE_PUBLIC (decl) = 1;
8135 DECL_EXTERNAL (decl) = 1;
8136 DECL_ARTIFICIAL (decl) = 1;
8137 pushdecl (decl);
8138 cp_finish_decl (decl, NULL_TREE, NULL_TREE, 0);
8139 pop_from_top_level ();
8140
8141 return decl;
8142}
8143
8144/* Returns a pointer to the `atexit' function. Note that if
838dfd8a 8145 FLAG_USE_CXA_ATEXIT is nonzero, then this will actually be the new
db4283a0
MM
8146 `__cxa_atexit' function specified in the IA64 C++ ABI. */
8147
8148static tree
11f6b451 8149get_atexit_node (void)
db4283a0
MM
8150{
8151 tree atexit_fndecl;
8152 tree arg_types;
8153 tree fn_type;
8154 tree fn_ptr_type;
8155 const char *name;
8156
8157 if (atexit_node)
8158 return atexit_node;
8159
8160 if (flag_use_cxa_atexit)
8161 {
8162 /* The declaration for `__cxa_atexit' is:
8163
8164 int __cxa_atexit (void (*)(void *), void *, void *)
8165
8166 We build up the argument types and then then function type
8167 itself. */
68642fb6 8168
db4283a0
MM
8169 /* First, build the pointer-to-function type for the first
8170 argument. */
8171 arg_types = tree_cons (NULL_TREE, ptr_type_node, void_list_node);
8172 fn_type = build_function_type (void_type_node, arg_types);
8173 fn_ptr_type = build_pointer_type (fn_type);
8174 /* Then, build the rest of the argument types. */
8175 arg_types = tree_cons (NULL_TREE, ptr_type_node, void_list_node);
8176 arg_types = tree_cons (NULL_TREE, ptr_type_node, arg_types);
8177 arg_types = tree_cons (NULL_TREE, fn_ptr_type, arg_types);
8178 /* And the final __cxa_atexit type. */
8179 fn_type = build_function_type (integer_type_node, arg_types);
8180 fn_ptr_type = build_pointer_type (fn_type);
8181 name = "__cxa_atexit";
8182 }
8183 else
8184 {
8185 /* The declaration for `atexit' is:
68642fb6 8186
db4283a0
MM
8187 int atexit (void (*)());
8188
8189 We build up the argument types and then then function type
8190 itself. */
8191 fn_type = build_function_type (void_type_node, void_list_node);
8192 fn_ptr_type = build_pointer_type (fn_type);
8193 arg_types = tree_cons (NULL_TREE, fn_ptr_type, void_list_node);
8194 /* Build the final atexit type. */
8195 fn_type = build_function_type (integer_type_node, arg_types);
8196 name = "atexit";
8197 }
8198
8199 /* Now, build the function declaration. */
8200 push_lang_context (lang_name_c);
0c11ada6 8201 atexit_fndecl = build_library_fn_ptr (name, fn_type);
db4283a0
MM
8202 mark_used (atexit_fndecl);
8203 pop_lang_context ();
0a72704b 8204 atexit_node = decay_conversion (atexit_fndecl);
db4283a0
MM
8205
8206 return atexit_node;
8207}
8208
8209/* Returns the __dso_handle VAR_DECL. */
8210
8211static tree
11f6b451 8212get_dso_handle_node (void)
db4283a0
MM
8213{
8214 if (dso_handle_node)
8215 return dso_handle_node;
8216
8217 /* Declare the variable. */
8218 dso_handle_node = declare_global_var (get_identifier ("__dso_handle"),
8219 ptr_type_node);
8220
8221 return dso_handle_node;
8222}
8223
8224/* Begin a new function with internal linkage whose job will be simply
8225 to destroy some particular variable. */
8226
d192828a
GK
8227static GTY(()) int start_cleanup_cnt;
8228
db4283a0 8229static tree
11f6b451 8230start_cleanup_fn (void)
db4283a0 8231{
23ccd1f3 8232 int old_interface_only = interface_only;
db4283a0
MM
8233 int old_interface_unknown = interface_unknown;
8234 char name[32];
8235 tree parmtypes;
8236 tree fntype;
8237 tree fndecl;
8238
8239 push_to_top_level ();
8240
8241 /* No need to mangle this. */
8242 push_lang_context (lang_name_c);
8243
23ccd1f3 8244 interface_only = 0;
db4283a0
MM
8245 interface_unknown = 1;
8246
8247 /* Build the parameter-types. */
8248 parmtypes = void_list_node;
8249 /* Functions passed to __cxa_atexit take an additional parameter.
8250 We'll just ignore it. After we implement the new calling
8251 convention for destructors, we can eliminate the use of
8252 additional cleanup functions entirely in the -fnew-abi case. */
8253 if (flag_use_cxa_atexit)
8254 parmtypes = tree_cons (NULL_TREE, ptr_type_node, parmtypes);
8255 /* Build the function type itself. */
8256 fntype = build_function_type (void_type_node, parmtypes);
8257 /* Build the name of the function. */
d192828a 8258 sprintf (name, "__tcf_%d", start_cleanup_cnt++);
db4283a0
MM
8259 /* Build the function declaration. */
8260 fndecl = build_lang_decl (FUNCTION_DECL, get_identifier (name), fntype);
8261 /* It's a function with internal linkage, generated by the
8262 compiler. */
8263 TREE_PUBLIC (fndecl) = 0;
8264 DECL_ARTIFICIAL (fndecl) = 1;
5a728aca
MM
8265 /* Make the function `inline' so that it is only emitted if it is
8266 actually needed. It is unlikely that it will be inlined, since
aba649ba 8267 it is only called via a function pointer, but we avoid unnecessary
5a728aca
MM
8268 emissions this way. */
8269 DECL_INLINE (fndecl) = 1;
db4283a0
MM
8270 /* Build the parameter. */
8271 if (flag_use_cxa_atexit)
8272 {
8273 tree parmdecl;
8274
8e51619a 8275 parmdecl = cp_build_parm_decl (NULL_TREE, ptr_type_node);
db4283a0 8276 DECL_CONTEXT (parmdecl) = fndecl;
db4283a0
MM
8277 TREE_USED (parmdecl) = 1;
8278 DECL_ARGUMENTS (fndecl) = parmdecl;
8279 }
8280
09ed39ad 8281 pushdecl (fndecl);
db4283a0 8282 start_function (/*specs=*/NULL_TREE, fndecl, NULL_TREE, SF_PRE_PARSED);
db4283a0
MM
8283
8284 interface_unknown = old_interface_unknown;
23ccd1f3 8285 interface_only = old_interface_only;
db4283a0
MM
8286
8287 pop_lang_context ();
8288
8289 return current_function_decl;
8290}
8291
8292/* Finish the cleanup function begun by start_cleanup_fn. */
8293
8294static void
11f6b451 8295end_cleanup_fn (void)
db4283a0 8296{
8cd2462c 8297 expand_or_defer_fn (finish_function (0));
db4283a0
MM
8298
8299 pop_from_top_level ();
8300}
8301
bf419747
MM
8302/* Generate code to handle the destruction of DECL, an object with
8303 static storage duration. */
f0105ed3 8304
bf419747 8305void
11f6b451 8306register_dtor_fn (tree decl)
f0105ed3 8307{
db4283a0 8308 tree cleanup;
f0105ed3 8309 tree compound_stmt;
db4283a0
MM
8310 tree args;
8311 tree fcall;
f0105ed3 8312
834c6dff 8313 if (TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
bf419747
MM
8314 return;
8315
f0105ed3
MM
8316 /* Call build_cleanup before we enter the anonymous function so that
8317 any access checks will be done relative to the current scope,
8318 rather than the scope of the anonymous function. */
8319 build_cleanup (decl);
8320
8321 /* Now start the function. */
db4283a0 8322 cleanup = start_cleanup_fn ();
f0105ed3
MM
8323
8324 /* Now, recompute the cleanup. It may contain SAVE_EXPRs that refer
8325 to the original function, rather than the anonymous one. That
8326 will make the back-end think that nested functions are in use,
8327 which causes confusion. */
78757caa
KL
8328
8329 push_deferring_access_checks (dk_no_check);
f0105ed3 8330 fcall = build_cleanup (decl);
78757caa 8331 pop_deferring_access_checks ();
f0105ed3
MM
8332
8333 /* Create the body of the anonymous function. */
7a3397c7 8334 compound_stmt = begin_compound_stmt (/*has_no_scope=*/false);
f0105ed3 8335 finish_expr_stmt (fcall);
7a3397c7 8336 finish_compound_stmt (compound_stmt);
db4283a0 8337 end_cleanup_fn ();
f0105ed3
MM
8338
8339 /* Call atexit with the cleanup function. */
dffd7eb6 8340 cxx_mark_addressable (cleanup);
bb4f6e6b 8341 mark_used (cleanup);
f0105ed3 8342 cleanup = build_unary_op (ADDR_EXPR, cleanup, 0);
db4283a0
MM
8343 if (flag_use_cxa_atexit)
8344 {
0f268005
MM
8345 args = tree_cons (NULL_TREE,
8346 build_unary_op (ADDR_EXPR, get_dso_handle_node (), 0),
8347 NULL_TREE);
db4283a0
MM
8348 args = tree_cons (NULL_TREE, null_pointer_node, args);
8349 args = tree_cons (NULL_TREE, cleanup, args);
8350 }
8351 else
8352 args = tree_cons (NULL_TREE, cleanup, NULL_TREE);
8353 finish_expr_stmt (build_function_call (get_atexit_node (), args));
f0105ed3
MM
8354}
8355
8e3df2de
MM
8356/* DECL is a VAR_DECL with static storage duration. INIT, if present,
8357 is its initializer. Generate code to handle the construction
8358 and destruction of DECL. */
8359
8360static void
11f6b451 8361expand_static_init (tree decl, tree init)
8d08fdba 8362{
8e3df2de
MM
8363 my_friendly_assert (TREE_CODE (decl) == VAR_DECL, 20021010);
8364 my_friendly_assert (TREE_STATIC (decl), 20021010);
8365
8366 /* Some variables require no initialization. */
8367 if (!init
8368 && !TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl))
8369 && TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
8370 return;
8371
4b0d3cbe 8372 if (! toplevel_bindings_p ())
8d08fdba
MS
8373 {
8374 /* Emit code to perform this initialization but once. */
b7b8bcd2 8375 tree if_stmt;
f0105ed3 8376 tree then_clause;
f1dedc31 8377 tree assignment;
c395453c
MM
8378 tree guard;
8379 tree guard_init;
8d08fdba 8380
2036a15c
MM
8381 /* Emit code to perform this initialization but once. This code
8382 looks like:
8383
c395453c
MM
8384 static int guard = 0;
8385 if (!guard) {
2036a15c 8386 // Do initialization.
c395453c 8387 guard = 1;
2036a15c
MM
8388 // Register variable for destruction at end of program.
8389 }
8390
8391 Note that the `temp' variable is only set to 1 *after* the
8392 initialization is complete. This ensures that an exception,
8393 thrown during the construction, will cause the variable to
8394 reinitialized when we pass through this code again, as per:
68642fb6 8395
2036a15c
MM
8396 [stmt.dcl]
8397
8398 If the initialization exits by throwing an exception, the
8399 initialization is not complete, so it will be tried again
8400 the next time control enters the declaration.
8401
8402 In theory, this process should be thread-safe, too; multiple
8403 threads should not be able to initialize the variable more
8404 than once. We don't yet attempt to ensure thread-safety. */
c395453c
MM
8405
8406 /* Create the guard variable. */
8407 guard = get_guard (decl);
2036a15c
MM
8408
8409 /* Begin the conditional initialization. */
b7b8bcd2 8410 if_stmt = begin_if_stmt ();
c395453c 8411 finish_if_stmt_cond (get_guard_cond (guard), if_stmt);
7a3397c7 8412 then_clause = begin_compound_stmt (/*has_no_scope=*/false);
72b7eeff 8413
2036a15c 8414 /* Do the initialization itself. */
8e3df2de 8415 assignment = init ? init : NULL_TREE;
f1dedc31
MM
8416
8417 /* Once the assignment is complete, set TEMP to 1. Since the
8418 construction of the static object is complete at this point,
8419 we want to make sure TEMP is set to 1 even if a temporary
8420 constructed during the initialization throws an exception
8421 when it is destroyed. So, we combine the initialization and
8422 the assignment to TEMP into a single expression, ensuring
8423 that when we call finish_expr_stmt the cleanups will not be
8424 run until after TEMP is set to 1. */
c395453c 8425 guard_init = set_guard (guard);
f1dedc31 8426 if (assignment)
c7b62f14 8427 assignment = build_compound_expr (assignment, guard_init);
f1dedc31 8428 else
c395453c 8429 assignment = guard_init;
f1dedc31 8430 finish_expr_stmt (assignment);
72b7eeff 8431
2036a15c
MM
8432 /* Use atexit to register a function for destroying this static
8433 variable. */
bf419747 8434 register_dtor_fn (decl);
72b7eeff 8435
7a3397c7 8436 finish_compound_stmt (then_clause);
b7b8bcd2
MM
8437 finish_then_clause (if_stmt);
8438 finish_if_stmt ();
8d08fdba
MS
8439 }
8440 else
bbd15aac 8441 static_aggregates = tree_cons (init, decl, static_aggregates);
8d08fdba 8442}
3c5c0849
MM
8443
8444/* Finish the declaration of a catch-parameter. */
8445
b35d4555 8446tree
11f6b451 8447start_handler_parms (tree declspecs, tree declarator)
3c5c0849
MM
8448{
8449 tree decl;
8450 if (declspecs)
8451 {
8452 decl = grokdeclarator (declarator, declspecs, CATCHPARM,
91d231cb 8453 1, NULL);
3c5c0849
MM
8454 if (decl == NULL_TREE)
8455 error ("invalid catch parameter");
8456 }
8457 else
8458 decl = NULL_TREE;
b35d4555
MM
8459
8460 return decl;
3c5c0849
MM
8461}
8462
8d08fdba
MS
8463\f
8464/* Make TYPE a complete type based on INITIAL_VALUE.
8465 Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
6ab5c740 8466 2 if there was no information (in which case assume 0 if DO_DEFAULT). */
8d08fdba
MS
8467
8468int
11f6b451 8469complete_array_type (tree type, tree initial_value, int do_default)
8d08fdba
MS
8470{
8471 register tree maxindex = NULL_TREE;
8472 int value = 0;
68642fb6 8473
8d08fdba
MS
8474 if (initial_value)
8475 {
7b019c19
MM
8476 /* An array of character type can be initialized from a
8477 brace-enclosed string constant. */
8478 if (char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type)))
8479 && TREE_CODE (initial_value) == CONSTRUCTOR
8480 && CONSTRUCTOR_ELTS (initial_value)
8481 && (TREE_CODE (TREE_VALUE (CONSTRUCTOR_ELTS (initial_value)))
8482 == STRING_CST)
8483 && TREE_CHAIN (CONSTRUCTOR_ELTS (initial_value)) == NULL_TREE)
8484 initial_value = TREE_VALUE (CONSTRUCTOR_ELTS (initial_value));
8485
8486 /* Note MAXINDEX is really the maximum index, one less than the
8487 size. */
8d08fdba 8488 if (TREE_CODE (initial_value) == STRING_CST)
e1cd6e56
MS
8489 {
8490 int eltsize
8491 = int_size_in_bytes (TREE_TYPE (TREE_TYPE (initial_value)));
8492 maxindex = build_int_2 ((TREE_STRING_LENGTH (initial_value)
8493 / eltsize) - 1, 0);
8494 }
8d08fdba
MS
8495 else if (TREE_CODE (initial_value) == CONSTRUCTOR)
8496 {
e1cd6e56 8497 tree elts = CONSTRUCTOR_ELTS (initial_value);
fed3cef0
RK
8498
8499 maxindex = ssize_int (-1);
e1cd6e56
MS
8500 for (; elts; elts = TREE_CHAIN (elts))
8501 {
8502 if (TREE_PURPOSE (elts))
8503 maxindex = TREE_PURPOSE (elts);
8504 else
fed3cef0 8505 maxindex = size_binop (PLUS_EXPR, maxindex, ssize_int (1));
e1cd6e56
MS
8506 }
8507 maxindex = copy_node (maxindex);
8d08fdba
MS
8508 }
8509 else
8510 {
8511 /* Make an error message unless that happened already. */
8512 if (initial_value != error_mark_node)
8513 value = 1;
0db982be
ML
8514 else
8515 initial_value = NULL_TREE;
8d08fdba
MS
8516
8517 /* Prevent further error messages. */
8518 maxindex = build_int_2 (0, 0);
8519 }
8520 }
8521
8522 if (!maxindex)
8523 {
8524 if (do_default)
8525 maxindex = build_int_2 (0, 0);
8526 value = 2;
8527 }
8528
8529 if (maxindex)
8530 {
51c184be 8531 tree itype;
6ab5c740
NS
8532 tree domain;
8533
8534 domain = build_index_type (maxindex);
8535 TYPE_DOMAIN (type) = domain;
51c184be 8536
dff6b454 8537 if (! TREE_TYPE (maxindex))
6ab5c740 8538 TREE_TYPE (maxindex) = domain;
51c184be
MS
8539 if (initial_value)
8540 itype = TREE_TYPE (initial_value);
8541 else
8542 itype = NULL;
8543 if (itype && !TYPE_DOMAIN (itype))
6ab5c740 8544 TYPE_DOMAIN (itype) = domain;
dff6b454
RK
8545 /* The type of the main variant should never be used for arrays
8546 of different sizes. It should only ever be completed with the
8547 size of the array. */
8548 if (! TYPE_DOMAIN (TYPE_MAIN_VARIANT (type)))
6ab5c740 8549 TYPE_DOMAIN (TYPE_MAIN_VARIANT (type)) = domain;
8d08fdba
MS
8550 }
8551
8552 /* Lay out the type now that we can get the real answer. */
8553
8554 layout_type (type);
8555
8556 return value;
8557}
8558\f
8559/* Return zero if something is declared to be a member of type
8560 CTYPE when in the context of CUR_TYPE. STRING is the error
8561 message to print in that case. Otherwise, quietly return 1. */
e92cc029 8562
8d08fdba 8563static int
11f6b451 8564member_function_or_else (tree ctype, tree cur_type, enum overload_flags flags)
8d08fdba
MS
8565{
8566 if (ctype && ctype != cur_type)
8567 {
4dacf5bd 8568 if (flags == DTOR_FLAG)
33bd39a2 8569 error ("destructor for alien class `%T' cannot be a member",
2ae7bada 8570 ctype);
4dacf5bd 8571 else
33bd39a2 8572 error ("constructor for alien class `%T' cannot be a member",
2ae7bada 8573 ctype);
8d08fdba
MS
8574 return 0;
8575 }
8576 return 1;
8577}
8578\f
8579/* Subroutine of `grokdeclarator'. */
8580
8581/* Generate errors possibly applicable for a given set of specifiers.
8582 This is for ARM $7.1.2. */
e92cc029 8583
8d08fdba 8584static void
11f6b451
NN
8585bad_specifiers (tree object,
8586 const char* type,
8587 int virtualp,
8588 int quals,
45225759 8589 int inlinep,
11f6b451 8590 int friendp,
45225759 8591 int raises)
8d08fdba
MS
8592{
8593 if (virtualp)
33bd39a2 8594 error ("`%D' declared as a `virtual' %s", object, type);
8d08fdba 8595 if (inlinep)
33bd39a2 8596 error ("`%D' declared as an `inline' %s", object, type);
8d08fdba 8597 if (quals)
33bd39a2 8598 error ("`const' and `volatile' function specifiers on `%D' invalid in %s declaration",
a28e3c7f 8599 object, type);
8d08fdba 8600 if (friendp)
f8e55f34 8601 cp_error_at ("`%D' declared as a friend", object);
e1be26f4
RS
8602 if (raises
8603 && (TREE_CODE (object) == TYPE_DECL
8604 || (!TYPE_PTRFN_P (TREE_TYPE (object))
742a37d5 8605 && !TYPE_REFFN_P (TREE_TYPE (object))
e1be26f4 8606 && !TYPE_PTRMEMFUNC_P (TREE_TYPE (object)))))
f8e55f34 8607 cp_error_at ("`%D' declared with an exception specification", object);
8d08fdba
MS
8608}
8609
8610/* CTYPE is class type, or null if non-class.
8611 TYPE is type this FUNCTION_DECL should have, either FUNCTION_TYPE
8612 or METHOD_TYPE.
8613 DECLARATOR is the function's name.
8614 VIRTUALP is truthvalue of whether the function is virtual or not.
8615 FLAGS are to be passed through to `grokclassfn'.
8616 QUALS are qualifiers indicating whether the function is `const'
8617 or `volatile'.
8618 RAISES is a list of exceptions that this function can raise.
8619 CHECK is 1 if we must find this method in CTYPE, 0 if we should
68642fb6 8620 not look, and -1 if we should not call `grokclassfn' at all.
3ddfb0e6 8621
20496fa2 8622 Returns `NULL_TREE' if something goes wrong, after issuing
3ddfb0e6 8623 applicable error messages. */
e92cc029 8624
8d08fdba 8625static tree
11f6b451
NN
8626grokfndecl (tree ctype,
8627 tree type,
8628 tree declarator,
8629 tree orig_declarator,
8630 int virtualp,
8631 enum overload_flags flags,
8632 tree quals,
8633 tree raises,
8634 int check,
8635 int friendp,
8636 int publicp,
8637 int inlinep,
8638 int funcdef_flag,
8639 int template_count,
8640 tree in_namespace)
8d08fdba 8641{
1951a1b6 8642 tree decl;
8d08fdba 8643 int staticp = ctype && TREE_CODE (type) == FUNCTION_TYPE;
856216bb 8644 int has_default_arg = 0;
42976354 8645 tree t;
8d08fdba 8646
8d08fdba 8647 if (raises)
271e6f02 8648 type = build_exception_variant (type, raises);
c11b6f21 8649
8d08fdba 8650 decl = build_lang_decl (FUNCTION_DECL, declarator, type);
a1c65f9f 8651 /* Propagate volatile out from type to decl. */
8d08fdba 8652 if (TYPE_VOLATILE (type))
893de33c 8653 TREE_THIS_VOLATILE (decl) = 1;
8d08fdba 8654
79c4d4b7 8655 /* If this decl has namespace scope, set that up. */
2c73f9f5 8656 if (in_namespace)
b262d64c 8657 set_decl_namespace (decl, in_namespace, friendp);
adae082f 8658 else if (!ctype)
79c4d4b7 8659 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
2c73f9f5 8660
0f8766b8
JM
8661 /* `main' and builtins have implicit 'C' linkage. */
8662 if ((MAIN_NAME_P (declarator)
8663 || (IDENTIFIER_LENGTH (declarator) > 10
8664 && IDENTIFIER_POINTER (declarator)[0] == '_'
8665 && IDENTIFIER_POINTER (declarator)[1] == '_'
8666 && strncmp (IDENTIFIER_POINTER (declarator)+2, "builtin_", 8) == 0))
8667 && current_lang_name == lang_name_cplusplus
94706a5c 8668 && ctype == NULL_TREE
79c4d4b7
JM
8669 /* NULL_TREE means global namespace. */
8670 && DECL_CONTEXT (decl) == NULL_TREE)
5d2ed28c 8671 SET_DECL_LANGUAGE (decl, lang_c);
0f8766b8 8672
8d08fdba
MS
8673 /* Should probably propagate const out from type to decl I bet (mrs). */
8674 if (staticp)
8675 {
8676 DECL_STATIC_FUNCTION_P (decl) = 1;
8677 DECL_CONTEXT (decl) = ctype;
8d08fdba
MS
8678 }
8679
e76a2646 8680 if (ctype)
4f1c5b7d 8681 DECL_CONTEXT (decl) = ctype;
e76a2646 8682
0f8766b8 8683 if (ctype == NULL_TREE && DECL_MAIN_P (decl))
faae18ab 8684 {
848b92e1 8685 if (processing_template_decl)
cb9a3ff8 8686 error ("cannot declare `::main' to be a template");
faae18ab 8687 if (inlinep)
cb9a3ff8 8688 error ("cannot declare `::main' to be inline");
f22967f3 8689 if (!publicp)
cb9a3ff8 8690 error ("cannot declare `::main' to be static");
f22967f3
MM
8691 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
8692 integer_type_node))
8693 error ("`main' must return `int'");
faae18ab
MS
8694 inlinep = 0;
8695 publicp = 1;
8696 }
50a6dbd7 8697
59e76fc6
JM
8698 /* Members of anonymous types and local classes have no linkage; make
8699 them internal. */
1951a1b6
JM
8700 /* FIXME what if it gets a name from typedef? */
8701 if (ctype && (TYPE_ANONYMOUS_P (ctype)
4f1c5b7d 8702 || decl_function_context (TYPE_MAIN_DECL (ctype))))
50a6dbd7
JM
8703 publicp = 0;
8704
8705 if (publicp)
8706 {
8707 /* [basic.link]: A name with no linkage (notably, the name of a class
8708 or enumeration declared in a local scope) shall not be used to
8709 declare an entity with linkage.
8710
50b97e0f 8711 Only check this for public decls for now. See core 319, 389. */
50a6dbd7
JM
8712 t = no_linkage_check (TREE_TYPE (decl));
8713 if (t)
8714 {
1951a1b6 8715 if (TYPE_ANONYMOUS_P (t))
7f7c930e 8716 {
eb68cb58 8717 if (DECL_EXTERN_C_P (decl))
7f7c930e
JM
8718 /* Allow this; it's pretty common in C. */;
8719 else
1951a1b6 8720 {
33bd39a2 8721 pedwarn ("non-local function `%#D' uses anonymous type",
1951a1b6
JM
8722 decl);
8723 if (DECL_ORIGINAL_TYPE (TYPE_NAME (t)))
8724 cp_pedwarn_at ("\
8725`%#D' does not refer to the unqualified type, so it is not used for linkage",
8726 TYPE_NAME (t));
8727 }
7f7c930e 8728 }
50a6dbd7 8729 else
33bd39a2 8730 pedwarn ("non-local function `%#D' uses local type `%T'",
cce2be43 8731 decl, t);
50a6dbd7
JM
8732 }
8733 }
8734
893de33c 8735 TREE_PUBLIC (decl) = publicp;
faae18ab 8736 if (! publicp)
893de33c
JM
8737 {
8738 DECL_INTERFACE_KNOWN (decl) = 1;
8739 DECL_NOT_REALLY_EXTERN (decl) = 1;
8740 }
faae18ab 8741
acc72c37 8742 /* If the declaration was declared inline, mark it as such. */
faae18ab 8743 if (inlinep)
acc72c37
MM
8744 DECL_DECLARED_INLINE_P (decl) = 1;
8745 /* We inline functions that are explicitly declared inline, or, when
8746 the user explicitly asks us to, all functions. */
b3c3af2f
SB
8747 if (DECL_DECLARED_INLINE_P (decl)
8748 || (flag_inline_trees == 2 && !DECL_INLINE (decl) && funcdef_flag))
acc72c37 8749 DECL_INLINE (decl) = 1;
8d08fdba
MS
8750
8751 DECL_EXTERNAL (decl) = 1;
8752 if (quals != NULL_TREE && TREE_CODE (type) == FUNCTION_TYPE)
8753 {
33bd39a2 8754 error ("%smember function `%D' cannot have `%T' method qualifier",
8d08fdba
MS
8755 (ctype ? "static " : "non-"), decl, TREE_VALUE (quals));
8756 quals = NULL_TREE;
8757 }
8758
596ea4e5 8759 if (IDENTIFIER_OPNAME_P (DECL_NAME (decl)))
4b0d3cbe 8760 grok_op_properties (decl, friendp, /*complain=*/true);
8d08fdba 8761
4f1c5b7d 8762 if (ctype && decl_function_context (decl))
893de33c 8763 DECL_NO_STATIC_CHAIN (decl) = 1;
e76a2646 8764
42976354
BK
8765 for (t = TYPE_ARG_TYPES (TREE_TYPE (decl)); t; t = TREE_CHAIN (t))
8766 if (TREE_PURPOSE (t)
8767 && TREE_CODE (TREE_PURPOSE (t)) == DEFAULT_ARG)
8768 {
856216bb 8769 has_default_arg = 1;
42976354
BK
8770 break;
8771 }
8772
f9d94ea4
JM
8773 if (friendp
8774 && TREE_CODE (orig_declarator) == TEMPLATE_ID_EXPR)
8775 {
8776 if (funcdef_flag)
33bd39a2 8777 error
8251199e 8778 ("defining explicit specialization `%D' in friend declaration",
f9d94ea4
JM
8779 orig_declarator);
8780 else
8781 {
76e57b45
NS
8782 tree fns = TREE_OPERAND (orig_declarator, 0);
8783 tree args = TREE_OPERAND (orig_declarator, 1);
d363e7bf 8784
7e2421f7
MM
8785 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
8786 {
8787 /* Something like `template <class T> friend void f<T>()'. */
33bd39a2 8788 error ("invalid use of template-id `%D' in declaration of primary template",
7e2421f7 8789 orig_declarator);
20496fa2 8790 return NULL_TREE;
7e2421f7
MM
8791 }
8792
856216bb 8793
f9d94ea4
JM
8794 /* A friend declaration of the form friend void f<>(). Record
8795 the information in the TEMPLATE_ID_EXPR. */
8796 SET_DECL_IMPLICIT_INSTANTIATION (decl);
76e57b45
NS
8797
8798 if (TREE_CODE (fns) == COMPONENT_REF)
8799 {
8800 /* Due to bison parser ickiness, we will have already looked
8801 up an operator_name or PFUNCNAME within the current class
8802 (see template_id in parse.y). If the current class contains
a1c65f9f 8803 such a name, we'll get a COMPONENT_REF here. Undo that. */
d363e7bf 8804
76e57b45
NS
8805 my_friendly_assert (TREE_TYPE (TREE_OPERAND (fns, 0))
8806 == current_class_type, 20001120);
8807 fns = TREE_OPERAND (fns, 1);
8808 }
8809 my_friendly_assert (TREE_CODE (fns) == IDENTIFIER_NODE
76e57b45
NS
8810 || TREE_CODE (fns) == OVERLOAD, 20001120);
8811 DECL_TEMPLATE_INFO (decl) = tree_cons (fns, args, NULL_TREE);
856216bb
MM
8812
8813 if (has_default_arg)
8814 {
33bd39a2 8815 error ("default arguments are not allowed in declaration of friend template specialization `%D'",
856216bb
MM
8816 decl);
8817 return NULL_TREE;
8818 }
8819
8820 if (inlinep)
8821 {
33bd39a2 8822 error ("`inline' is not allowed in declaration of friend template specialization `%D'",
856216bb
MM
8823 decl);
8824 return NULL_TREE;
8825 }
f9d94ea4 8826 }
f84b4be9 8827 }
386b8a85 8828
1eb0072d
JM
8829 if (funcdef_flag)
8830 /* Make the init_value nonzero so pushdecl knows this is not
8831 tentative. error_mark_node is replaced later with the BLOCK. */
8832 DECL_INITIAL (decl) = error_mark_node;
8833
93ca4ba7 8834 if (TYPE_NOTHROW_P (type) || nothrow_libfn_p (decl))
1660cb3a
JM
8835 TREE_NOTHROW (decl) = 1;
8836
75650646 8837 /* Caller will do the rest of this. */
8d08fdba
MS
8838 if (check < 0)
8839 return decl;
8840
a723baf1 8841 if (flags == NO_SPECIAL && ctype && constructor_name_p (declarator, ctype))
74b846e0
MM
8842 DECL_CONSTRUCTOR_P (decl) = 1;
8843
8844 /* Function gets the ugly name, field gets the nice one. This call
8845 may change the type of the function (because of default
8846 parameters)! */
8847 if (ctype != NULL_TREE)
8848 grokclassfn (ctype, decl, flags, quals);
8849
8850 decl = check_explicit_specialization (orig_declarator, decl,
8851 template_count,
8852 2 * (funcdef_flag != 0) +
8853 4 * (friendp != 0));
8854 if (decl == error_mark_node)
8855 return NULL_TREE;
98c1c668 8856
74b846e0
MM
8857 if (ctype != NULL_TREE
8858 && (! TYPE_FOR_JAVA (ctype) || check_java_method (decl))
8859 && check)
8d08fdba 8860 {
74b846e0 8861 tree old_decl;
8d08fdba 8862
74b846e0 8863 old_decl = check_classfn (ctype, decl);
8d08fdba 8864
74b846e0
MM
8865 if (old_decl && TREE_CODE (old_decl) == TEMPLATE_DECL)
8866 /* Because grokfndecl is always supposed to return a
8867 FUNCTION_DECL, we pull out the DECL_TEMPLATE_RESULT
8868 here. We depend on our callers to figure out that its
8869 really a template that's being returned. */
8870 old_decl = DECL_TEMPLATE_RESULT (old_decl);
6c30752f 8871
74b846e0
MM
8872 if (old_decl && DECL_STATIC_FUNCTION_P (old_decl)
8873 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE)
5566b478 8874 {
74b846e0
MM
8875 /* Remove the `this' parm added by grokclassfn.
8876 XXX Isn't this done in start_function, too? */
3afb32a4 8877 revert_static_member_fn (decl);
74b846e0 8878 last_function_parms = TREE_CHAIN (last_function_parms);
5566b478 8879 }
74b846e0 8880 if (old_decl && DECL_ARTIFICIAL (old_decl))
33bd39a2 8881 error ("definition of implicitly-declared `%D'", old_decl);
8d08fdba 8882
74b846e0 8883 if (old_decl)
8d08fdba 8884 {
316a2456
MM
8885 bool ok;
8886
74b846e0
MM
8887 /* Since we've smashed OLD_DECL to its
8888 DECL_TEMPLATE_RESULT, we must do the same to DECL. */
8889 if (TREE_CODE (decl) == TEMPLATE_DECL)
8890 decl = DECL_TEMPLATE_RESULT (decl);
8891
8892 /* Attempt to merge the declarations. This can fail, in
0e339752 8893 the case of some invalid specialization declarations. */
14d22dd6 8894 push_scope (ctype);
316a2456 8895 ok = duplicate_decls (decl, old_decl);
14d22dd6 8896 pop_scope (ctype);
316a2456
MM
8897 if (!ok)
8898 {
8899 error ("no `%#D' member function declared in class `%T'",
8900 decl, ctype);
8901 return NULL_TREE;
8902 }
74b846e0 8903 return old_decl;
8d08fdba
MS
8904 }
8905 }
74b846e0
MM
8906
8907 if (DECL_CONSTRUCTOR_P (decl) && !grok_ctor_properties (ctype, decl))
8908 return NULL_TREE;
8909
8910 if (ctype == NULL_TREE || check)
8911 return decl;
8912
8913 if (virtualp)
cbb40945 8914 DECL_VIRTUAL_P (decl) = 1;
74b846e0 8915
8d08fdba
MS
8916 return decl;
8917}
8918
9e259dd1
MM
8919/* Create a VAR_DECL named NAME with the indicated TYPE.
8920
8921 If SCOPE is non-NULL, it is the class type or namespace containing
8922 the variable. If SCOPE is NULL, the variable should is created in
8923 the innermost enclosings scope. */
8924
8d08fdba 8925static tree
11f6b451
NN
8926grokvardecl (tree type,
8927 tree name,
8928 RID_BIT_TYPE * specbits_in,
8929 int initialized,
8930 int constp,
8931 tree scope)
8d08fdba
MS
8932{
8933 tree decl;
f7da6097
MS
8934 RID_BIT_TYPE specbits;
8935
9e259dd1
MM
8936 my_friendly_assert (!name || TREE_CODE (name) == IDENTIFIER_NODE,
8937 20020808);
8938
f7da6097 8939 specbits = *specbits_in;
8d08fdba 8940
9e259dd1
MM
8941 /* Compute the scope in which to place the variable. */
8942 if (!scope)
8d08fdba 8943 {
9e259dd1
MM
8944 /* An explicit "extern" specifier indicates a namespace-scope
8945 variable. */
8946 if (RIDBIT_SETP (RID_EXTERN, specbits))
8947 scope = current_namespace;
8948 else if (!at_function_scope_p ())
8949 {
8950 scope = current_scope ();
8951 if (!scope)
8952 scope = current_namespace;
8953 }
8954 }
8955
8956 if (scope
8957 && (/* If the variable is a namespace-scope variable declared in a
8958 template, we need DECL_LANG_SPECIFIC. */
8959 (TREE_CODE (scope) == NAMESPACE_DECL && processing_template_decl)
8960 /* Similarly for namespace-scope variables with language linkage
8961 other than C++. */
8962 || (TREE_CODE (scope) == NAMESPACE_DECL
8963 && current_lang_name != lang_name_cplusplus)
8964 /* Similarly for static data members. */
8965 || TYPE_P (scope)))
8966 decl = build_lang_decl (VAR_DECL, name, type);
8d08fdba 8967 else
9e259dd1 8968 decl = build_decl (VAR_DECL, name, type);
79c4d4b7 8969
9e259dd1
MM
8970 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
8971 set_decl_namespace (decl, scope, 0);
8972 else
8973 DECL_CONTEXT (decl) = scope;
6060a796 8974
9e259dd1
MM
8975 if (name && scope && current_lang_name != lang_name_c)
8976 /* We can't mangle lazily here because we don't have any
8977 way to recover whether or not a variable was `extern
8978 "C"' later. */
8979 mangle_decl (decl);
2c73f9f5 8980
8d08fdba
MS
8981 if (RIDBIT_SETP (RID_EXTERN, specbits))
8982 {
8983 DECL_THIS_EXTERN (decl) = 1;
8984 DECL_EXTERNAL (decl) = !initialized;
8985 }
8986
8987 /* In class context, static means one per class,
8988 public access, and static storage. */
2b9dc906 8989 if (DECL_CLASS_SCOPE_P (decl))
8d08fdba
MS
8990 {
8991 TREE_PUBLIC (decl) = 1;
8992 TREE_STATIC (decl) = 1;
5b605f68 8993 DECL_EXTERNAL (decl) = 0;
8d08fdba
MS
8994 }
8995 /* At top level, either `static' or no s.c. makes a definition
8996 (perhaps tentative), and absence of `static' makes it public. */
a9aedbc2 8997 else if (toplevel_bindings_p ())
8d08fdba 8998 {
a9aedbc2 8999 TREE_PUBLIC (decl) = (RIDBIT_NOTSETP (RID_STATIC, specbits)
9fffd093 9000 && (DECL_THIS_EXTERN (decl) || ! constp));
8d08fdba
MS
9001 TREE_STATIC (decl) = ! DECL_EXTERNAL (decl);
9002 }
9003 /* Not at top level, only `static' makes a static definition. */
9004 else
9005 {
9006 TREE_STATIC (decl) = !! RIDBIT_SETP (RID_STATIC, specbits);
9007 TREE_PUBLIC (decl) = DECL_EXTERNAL (decl);
9008 }
50a6dbd7 9009
7a1f3f5f
RH
9010 if (RIDBIT_SETP (RID_THREAD, specbits))
9011 {
9012 if (targetm.have_tls)
9013 DECL_THREAD_LOCAL (decl) = 1;
9014 else
9015 /* A mere warning is sure to result in improper semantics
9016 at runtime. Don't bother to allow this to compile. */
9017 error ("thread-local storage not supported for this target");
9018 }
9019
50a6dbd7
JM
9020 if (TREE_PUBLIC (decl))
9021 {
9022 /* [basic.link]: A name with no linkage (notably, the name of a class
9023 or enumeration declared in a local scope) shall not be used to
9024 declare an entity with linkage.
9025
9026 Only check this for public decls for now. */
9027 tree t = no_linkage_check (TREE_TYPE (decl));
9028 if (t)
9029 {
1951a1b6 9030 if (TYPE_ANONYMOUS_P (t))
50a6dbd7
JM
9031 /* Ignore for now; `enum { foo } e' is pretty common. */;
9032 else
33bd39a2 9033 pedwarn ("non-local variable `%#D' uses local type `%T'",
50a6dbd7
JM
9034 decl, t);
9035 }
9036 }
9037
8d08fdba
MS
9038 return decl;
9039}
9040
d8f8dca1
MM
9041/* Create and return a canonical pointer to member function type, for
9042 TYPE, which is a POINTER_TYPE to a METHOD_TYPE. */
8d08fdba
MS
9043
9044tree
11f6b451 9045build_ptrmemfunc_type (tree type)
8d08fdba 9046{
4977bab6 9047 tree field, fields;
8d08fdba 9048 tree t;
46cbda4a 9049 tree unqualified_variant = NULL_TREE;
8d08fdba 9050
d48ebde1
NS
9051 if (type == error_mark_node)
9052 return type;
d363e7bf 9053
8d08fdba
MS
9054 /* If a canonical type already exists for this type, use it. We use
9055 this method instead of type_hash_canon, because it only does a
9056 simple equality check on the list of field members. */
9057
9058 if ((t = TYPE_GET_PTRMEMFUNC_TYPE (type)))
9059 return t;
9060
46cbda4a
MM
9061 /* Make sure that we always have the unqualified pointer-to-member
9062 type first. */
89d684bb 9063 if (cp_type_quals (type) != TYPE_UNQUALIFIED)
68642fb6 9064 unqualified_variant
46cbda4a
MM
9065 = build_ptrmemfunc_type (TYPE_MAIN_VARIANT (type));
9066
33848bb0 9067 t = make_aggr_type (RECORD_TYPE);
2c73f9f5 9068 /* Let the front-end know this is a pointer to member function... */
db5ae43f 9069 TYPE_PTRMEMFUNC_FLAG (t) = 1;
2c73f9f5 9070 /* ... and not really an aggregate. */
7ddedda4 9071 SET_IS_AGGR_TYPE (t, 0);
8d08fdba 9072
4977bab6
ZW
9073 field = build_decl (FIELD_DECL, pfn_identifier, type);
9074 fields = field;
9075
9076 field = build_decl (FIELD_DECL, delta_identifier, delta_type_node);
9077 TREE_CHAIN (field) = fields;
9078 fields = field;
9079
9080 finish_builtin_struct (t, "__ptrmemfunc_type", fields, ptr_type_node);
8d08fdba 9081
8d08fdba
MS
9082 /* Zap out the name so that the back-end will give us the debugging
9083 information for this anonymous RECORD_TYPE. */
9084 TYPE_NAME (t) = NULL_TREE;
9085
46cbda4a
MM
9086 /* If this is not the unqualified form of this pointer-to-member
9087 type, set the TYPE_MAIN_VARIANT for this type to be the
9088 unqualified type. Since they are actually RECORD_TYPEs that are
9089 not variants of each other, we must do this manually. */
89d684bb 9090 if (cp_type_quals (type) != TYPE_UNQUALIFIED)
46cbda4a 9091 {
89d684bb 9092 t = build_qualified_type (t, cp_type_quals (type));
46cbda4a
MM
9093 TYPE_MAIN_VARIANT (t) = unqualified_variant;
9094 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (unqualified_variant);
9095 TYPE_NEXT_VARIANT (unqualified_variant) = t;
9096 }
9097
9098 /* Cache this pointer-to-member type so that we can find it again
9099 later. */
8d08fdba
MS
9100 TYPE_SET_PTRMEMFUNC_TYPE (type, t);
9101
8d08fdba
MS
9102 return t;
9103}
9104
9e259dd1
MM
9105/* Create and return a pointer to data member type. */
9106
9107tree
9108build_ptrmem_type (tree class_type, tree member_type)
9109{
a5ac359a
MM
9110 if (TREE_CODE (member_type) == METHOD_TYPE)
9111 {
9112 tree arg_types;
9113
9114 arg_types = TYPE_ARG_TYPES (member_type);
9115 class_type = (cp_build_qualified_type
9116 (class_type,
9117 cp_type_quals (TREE_TYPE (TREE_VALUE (arg_types)))));
9118 member_type
43dc123f
MM
9119 = build_method_type_directly (class_type,
9120 TREE_TYPE (member_type),
9121 TREE_CHAIN (arg_types));
a5ac359a
MM
9122 return build_ptrmemfunc_type (build_pointer_type (member_type));
9123 }
9124 else
9125 {
9126 my_friendly_assert (TREE_CODE (member_type) != FUNCTION_TYPE,
9127 20030716);
9128 return build_offset_type (class_type, member_type);
9129 }
9e259dd1
MM
9130}
9131
b17e2870
JM
9132/* DECL is a VAR_DECL defined in-class, whose TYPE is also given.
9133 Check to see that the definition is valid. Issue appropriate error
9134 messages. Return 1 if the definition is particularly bad, or 0
9135 otherwise. */
9136
9137int
11f6b451 9138check_static_variable_definition (tree decl, tree type)
b17e2870
JM
9139{
9140 /* Motion 10 at San Diego: If a static const integral data member is
9141 initialized with an integral constant expression, the initializer
9142 may appear either in the declaration (within the class), or in
9143 the definition, but not both. If it appears in the class, the
9144 member is a member constant. The file-scope definition is always
9145 required. */
dcba9b0f 9146 if (!ARITHMETIC_TYPE_P (type) && TREE_CODE (type) != ENUMERAL_TYPE)
b17e2870 9147 {
33bd39a2 9148 error ("invalid in-class initialization of static data member of non-integral type `%T'",
dcba9b0f 9149 type);
b17e2870
JM
9150 /* If we just return the declaration, crashes will sometimes
9151 occur. We therefore return void_type_node, as if this was a
9152 friend declaration, to cause callers to completely ignore
9153 this declaration. */
9154 return 1;
9155 }
9156 else if (!CP_TYPE_CONST_P (type))
33bd39a2 9157 error ("ISO C++ forbids in-class initialization of non-const static member `%D'",
b17e2870
JM
9158 decl);
9159 else if (pedantic && !INTEGRAL_TYPE_P (type))
33bd39a2 9160 pedwarn ("ISO C++ forbids initialization of member constant `%D' of non-integral type `%T'", decl, type);
b17e2870
JM
9161
9162 return 0;
9163}
9164
2848ad0f
MM
9165/* Given the SIZE (i.e., number of elements) in an array, compute an
9166 appropriate index type for the array. If non-NULL, NAME is the
9167 name of the thing being declared. */
9168
c95cd22e 9169tree
11f6b451 9170compute_array_index_type (tree name, tree size)
2848ad0f
MM
9171{
9172 tree itype;
9173
2848ad0f
MM
9174 /* If this involves a template parameter, it will be a constant at
9175 instantiation time, but we don't know what the value is yet.
9176 Even if no template parameters are involved, we may an expression
9177 that is not a constant; we don't even simplify `1 + 2' when
9178 processing a template. */
9179 if (processing_template_decl)
9180 {
9181 /* Resolve a qualified reference to an enumerator or static
9182 const data member of ours. */
9183 if (TREE_CODE (size) == SCOPE_REF
9184 && TREE_OPERAND (size, 0) == current_class_type)
9185 {
9186 tree t = lookup_field (current_class_type,
86ac0575 9187 TREE_OPERAND (size, 1), 0, false);
2848ad0f
MM
9188 if (t)
9189 size = t;
9190 }
9191
9192 return build_index_type (build_min (MINUS_EXPR, sizetype,
9193 size, integer_one_node));
9194 }
9195
a1c65f9f 9196 /* The size might be the result of a cast. */
80f5bb34
MM
9197 STRIP_TYPE_NOPS (size);
9198
9199 /* It might be a const variable or enumeration constant. */
9200 size = decl_constant_value (size);
9201
2848ad0f
MM
9202 /* The array bound must be an integer type. */
9203 if (TREE_CODE (TREE_TYPE (size)) != INTEGER_TYPE
9204 && TREE_CODE (TREE_TYPE (size)) != ENUMERAL_TYPE
9205 && TREE_CODE (TREE_TYPE (size)) != BOOLEAN_TYPE)
9206 {
d67cdbc3 9207 if (name)
33bd39a2 9208 error ("size of array `%D' has non-integer type", name);
d67cdbc3 9209 else
33bd39a2 9210 error ("size of array has non-integer type");
2848ad0f
MM
9211 size = integer_one_node;
9212 }
9213
9214 /* Normally, the array-bound will be a constant. */
2bb5d995 9215 if (TREE_CODE (size) == INTEGER_CST)
2848ad0f
MM
9216 {
9217 /* Check to see if the array bound overflowed. Make that an
9218 error, no matter how generous we're being. */
9219 int old_flag_pedantic_errors = flag_pedantic_errors;
9220 int old_pedantic = pedantic;
9221 pedantic = flag_pedantic_errors = 1;
9222 constant_expression_warning (size);
9223 pedantic = old_pedantic;
9224 flag_pedantic_errors = old_flag_pedantic_errors;
9225
9226 /* An array must have a positive number of elements. */
9227 if (INT_CST_LT (size, integer_zero_node))
9228 {
d67cdbc3 9229 if (name)
33bd39a2 9230 error ("size of array `%D' is negative", name);
d67cdbc3 9231 else
33bd39a2 9232 error ("size of array is negative");
2848ad0f
MM
9233 size = integer_one_node;
9234 }
9235 /* Except that an extension we allow zero-sized arrays. We
68642fb6 9236 always allow them in system headers because glibc uses
2848ad0f
MM
9237 them. */
9238 else if (integer_zerop (size) && pedantic && !in_system_header)
d67cdbc3
JM
9239 {
9240 if (name)
33bd39a2 9241 pedwarn ("ISO C++ forbids zero-size array `%D'", name);
d67cdbc3 9242 else
33bd39a2 9243 pedwarn ("ISO C++ forbids zero-size array");
d67cdbc3 9244 }
2848ad0f 9245 }
2bb5d995
JM
9246 else if (TREE_CONSTANT (size))
9247 {
9248 /* `(int) &fn' is not a valid array bound. */
9249 if (name)
33bd39a2 9250 error ("size of array `%D' is not an integral constant-expression",
2bb5d995
JM
9251 name);
9252 else
33bd39a2 9253 error ("size of array is not an integral constant-expression");
2bb5d995 9254 }
2848ad0f
MM
9255
9256 /* Compute the index of the largest element in the array. It is
9257 one less than the number of elements in the array. */
9258 itype
ab76ca54
MM
9259 = fold (cp_build_binary_op (MINUS_EXPR,
9260 cp_convert (ssizetype, size),
9261 cp_convert (ssizetype,
9262 integer_one_node)));
68642fb6 9263
2848ad0f
MM
9264 /* Check for variable-sized arrays. We allow such things as an
9265 extension, even though they are not allowed in ANSI/ISO C++. */
9266 if (!TREE_CONSTANT (itype))
9267 {
9268 if (pedantic)
9269 {
9270 if (name)
33bd39a2 9271 pedwarn ("ISO C++ forbids variable-size array `%D'",
2848ad0f
MM
9272 name);
9273 else
33bd39a2 9274 pedwarn ("ISO C++ forbids variable-size array");
2848ad0f
MM
9275 }
9276
9277 /* Create a variable-sized array index type. */
9278 itype = variable_size (itype);
9279 }
9280 /* Make sure that there was no overflow when creating to a signed
9281 index type. (For example, on a 32-bit machine, an array with
9282 size 2^32 - 1 is too big.) */
9283 else if (TREE_OVERFLOW (itype))
9284 {
9285 error ("overflow in array dimension");
9286 TREE_OVERFLOW (itype) = 0;
9287 }
68642fb6 9288
2848ad0f
MM
9289 /* Create and return the appropriate index type. */
9290 return build_index_type (itype);
9291}
9292
a723baf1
MM
9293/* Returns the scope (if any) in which the entity declared by
9294 DECLARATOR will be located. If the entity was declared with an
9295 unqualified name, NULL_TREE is returned. */
9296
9297tree
11f6b451 9298get_scope_of_declarator (tree declarator)
a723baf1
MM
9299{
9300 if (!declarator)
9301 return NULL_TREE;
9302
9303 switch (TREE_CODE (declarator))
9304 {
9305 case CALL_EXPR:
9306 case ARRAY_REF:
9307 case INDIRECT_REF:
9308 case ADDR_EXPR:
9309 /* For any of these, the main declarator is the first operand. */
9310 return get_scope_of_declarator (TREE_OPERAND
9311 (declarator, 0));
9312
9313 case SCOPE_REF:
9314 /* For a pointer-to-member, continue descending. */
9315 if (TREE_CODE (TREE_OPERAND (declarator, 1))
9316 == INDIRECT_REF)
9317 return get_scope_of_declarator (TREE_OPERAND
9318 (declarator, 1));
9319 /* Otherwise, if the declarator-id is a SCOPE_REF, the scope in
9320 which the declaration occurs is the first operand. */
9321 return TREE_OPERAND (declarator, 0);
9322
9323 case TREE_LIST:
9324 /* Attributes to be applied. The declarator is TREE_VALUE. */
9325 return get_scope_of_declarator (TREE_VALUE (declarator));
9326
9327 default:
9328 /* Otherwise, we have a declarator-id which is not a qualified
9329 name; the entity will be declared in the current scope. */
9330 return NULL_TREE;
9331 }
9332}
9333
2848ad0f
MM
9334/* Returns an ARRAY_TYPE for an array with SIZE elements of the
9335 indicated TYPE. If non-NULL, NAME is the NAME of the declaration
9336 with this type. */
9337
9338static tree
11f6b451 9339create_array_type_for_decl (tree name, tree type, tree size)
2848ad0f
MM
9340{
9341 tree itype = NULL_TREE;
9342 const char* error_msg;
9343
9344 /* If things have already gone awry, bail now. */
9345 if (type == error_mark_node || size == error_mark_node)
9346 return error_mark_node;
9347
9348 /* Assume that everything will go OK. */
9349 error_msg = NULL;
9350
9351 /* There are some types which cannot be array elements. */
9352 switch (TREE_CODE (type))
9353 {
9354 case VOID_TYPE:
9355 error_msg = "array of void";
9356 break;
9357
9358 case FUNCTION_TYPE:
9359 error_msg = "array of functions";
9360 break;
9361
9362 case REFERENCE_TYPE:
9363 error_msg = "array of references";
9364 break;
9365
2848ad0f
MM
9366 case METHOD_TYPE:
9367 error_msg = "array of function members";
9368 break;
9369
9370 default:
9371 break;
9372 }
9373
9374 /* If something went wrong, issue an error-message and return. */
9375 if (error_msg)
9376 {
9377 if (name)
33bd39a2 9378 error ("declaration of `%D' as %s", name, error_msg);
2848ad0f 9379 else
33bd39a2 9380 error ("creating %s", error_msg);
2848ad0f
MM
9381
9382 return error_mark_node;
9383 }
9384
9385 /* [dcl.array]
68642fb6 9386
2848ad0f
MM
9387 The constant expressions that specify the bounds of the arrays
9388 can be omitted only for the first member of the sequence. */
9389 if (TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type))
9390 {
b3faacfd 9391 if (name)
33bd39a2 9392 error ("declaration of `%D' as multidimensional array must have bounds for all dimensions except the first",
b3faacfd
KL
9393 name);
9394 else
33bd39a2 9395 error ("multidimensional array must have bounds for all dimensions except the first");
2848ad0f
MM
9396
9397 return error_mark_node;
9398 }
9399
9400 /* Figure out the index type for the array. */
9401 if (size)
9402 itype = compute_array_index_type (name, size);
9403
9404 return build_cplus_array_type (type, itype);
9405}
9406
3dbc07b6
MM
9407/* Check that it's OK to declare a function with the indicated TYPE.
9408 SFK indicates the kind of special function (if any) that this
1f84ec23 9409 function is. OPTYPE is the type given in a conversion operator
3dbc07b6
MM
9410 declaration. Returns the actual return type of the function; that
9411 may be different than TYPE if an error occurs, or for certain
9412 special functions. */
9413
9414static tree
11f6b451
NN
9415check_special_function_return_type (special_function_kind sfk,
9416 tree type,
9417 tree optype)
3dbc07b6
MM
9418{
9419 switch (sfk)
9420 {
9421 case sfk_constructor:
9422 if (type)
33bd39a2 9423 error ("return type specification for constructor invalid");
5362b086 9424
1f84ec23 9425 type = void_type_node;
3dbc07b6
MM
9426 break;
9427
9428 case sfk_destructor:
9429 if (type)
33bd39a2 9430 error ("return type specification for destructor invalid");
3dbc07b6
MM
9431 type = void_type_node;
9432 break;
9433
9434 case sfk_conversion:
9435 if (type && !same_type_p (type, optype))
33bd39a2 9436 error ("operator `%T' declared to return `%T'", optype, type);
3dbc07b6 9437 else if (type)
33bd39a2 9438 pedwarn ("return type specified for `operator %T'", optype);
3dbc07b6
MM
9439 type = optype;
9440 break;
9441
9442 default:
a98facb0 9443 abort ();
3dbc07b6
MM
9444 break;
9445 }
9446
9447 return type;
9448}
9449
a723baf1
MM
9450/* Given declspecs and a declarator (abstract or otherwise), determine
9451 the name and type of the object declared and construct a DECL node
9452 for it.
8d08fdba
MS
9453
9454 DECLSPECS is a chain of tree_list nodes whose value fields
9455 are the storage classes and type specifiers.
9456
9457 DECL_CONTEXT says which syntactic context this declaration is in:
9458 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
9459 FUNCDEF for a function definition. Like NORMAL but a few different
9460 error messages in each case. Return value may be zero meaning
9461 this definition is too screwy to try to parse.
9462 MEMFUNCDEF for a function definition. Like FUNCDEF but prepares to
9463 handle member functions (which have FIELD context).
9464 Return value may be zero meaning this definition is too screwy to
9465 try to parse.
9466 PARM for a parameter declaration (either within a function prototype
9467 or before a function body). Make a PARM_DECL, or return void_type_node.
db5ae43f 9468 CATCHPARM for a parameter declaration before a catch clause.
8d08fdba
MS
9469 TYPENAME if for a typename (in a cast or sizeof).
9470 Don't make a DECL node; just return the ..._TYPE node.
9471 FIELD for a struct or union field; make a FIELD_DECL.
9472 BITFIELD for a field with specified width.
9473 INITIALIZED is 1 if the decl has an initializer.
9474
91d231cb
JM
9475 ATTRLIST is a pointer to the list of attributes, which may be NULL
9476 if there are none; *ATTRLIST may be modified if attributes from inside
9477 the declarator should be applied to the declaration.
b17e2870 9478
a723baf1
MM
9479 When this function is called, scoping variables (such as
9480 CURRENT_CLASS_TYPE) should reflect the scope in which the
9481 declaration occurs, not the scope in which the new declaration will
9482 be placed. For example, on:
8d08fdba 9483
a723baf1 9484 void S::f() { ... }
8d08fdba 9485
a723baf1
MM
9486 when grokdeclarator is called for `S::f', the CURRENT_CLASS_TYPE
9487 should not be `S'. */
8d08fdba 9488
8d08fdba 9489tree
11f6b451
NN
9490grokdeclarator (tree declarator,
9491 tree declspecs,
9492 enum decl_context decl_context,
9493 int initialized,
9494 tree* attrlist)
8d08fdba
MS
9495{
9496 RID_BIT_TYPE specbits;
9497 int nclasses = 0;
9498 tree spec;
9499 tree type = NULL_TREE;
9500 int longlong = 0;
91063b51 9501 int type_quals;
db5ae43f 9502 int virtualp, explicitp, friendp, inlinep, staticp;
8d08fdba
MS
9503 int explicit_int = 0;
9504 int explicit_char = 0;
37c46b43 9505 int defaulted_int = 0;
270d8c65 9506 int extern_langp = 0;
d5614afb 9507 tree dependant_name = NULL_TREE;
270d8c65 9508
8d08fdba 9509 tree typedef_decl = NULL_TREE;
9c0758dd 9510 const char *name;
8d08fdba
MS
9511 tree typedef_type = NULL_TREE;
9512 int funcdef_flag = 0;
9513 enum tree_code innermost_code = ERROR_MARK;
9514 int bitfield = 0;
6125f3be
DE
9515#if 0
9516 /* See the code below that used this. */
91d231cb 9517 tree decl_attr = NULL_TREE;
6125f3be 9518#endif
8d08fdba
MS
9519
9520 /* Keep track of what sort of function is being processed
9521 so that we can warn about default return values, or explicit
9522 return values which do not match prescribed defaults. */
3dbc07b6 9523 special_function_kind sfk = sfk_none;
8d08fdba
MS
9524
9525 tree dname = NULL_TREE;
9526 tree ctype = current_class_type;
9527 tree ctor_return_type = NULL_TREE;
9528 enum overload_flags flags = NO_SPECIAL;
8d08fdba 9529 tree quals = NULL_TREE;
c11b6f21 9530 tree raises = NULL_TREE;
386b8a85 9531 int template_count = 0;
2c73f9f5 9532 tree in_namespace = NULL_TREE;
91d231cb 9533 tree returned_attrs = NULL_TREE;
a723baf1 9534 tree scope = NULL_TREE;
8d08fdba
MS
9535
9536 RIDBIT_RESET_ALL (specbits);
9537 if (decl_context == FUNCDEF)
9538 funcdef_flag = 1, decl_context = NORMAL;
9539 else if (decl_context == MEMFUNCDEF)
9540 funcdef_flag = -1, decl_context = FIELD;
9541 else if (decl_context == BITFIELD)
9542 bitfield = 1, decl_context = FIELD;
9543
8d08fdba
MS
9544 /* Look inside a declarator for the name being declared
9545 and get it as a string, for an error message. */
9546 {
be99da77
MS
9547 tree *next = &declarator;
9548 register tree decl;
8d08fdba
MS
9549 name = NULL;
9550
be99da77
MS
9551 while (next && *next)
9552 {
9553 decl = *next;
9554 switch (TREE_CODE (decl))
8d08fdba 9555 {
52fbc847
JM
9556 case TREE_LIST:
9557 /* For attributes. */
9558 next = &TREE_VALUE (decl);
9559 break;
9560
be99da77
MS
9561 case COND_EXPR:
9562 ctype = NULL_TREE;
9563 next = &TREE_OPERAND (decl, 0);
9564 break;
8d08fdba 9565
2c73f9f5 9566 case BIT_NOT_EXPR: /* For C++ destructors! */
8d08fdba 9567 {
be99da77
MS
9568 tree name = TREE_OPERAND (decl, 0);
9569 tree rename = NULL_TREE;
9570
9571 my_friendly_assert (flags == NO_SPECIAL, 152);
9572 flags = DTOR_FLAG;
3dbc07b6 9573 sfk = sfk_destructor;
a723baf1 9574 if (TYPE_P (name))
5566b478 9575 TREE_OPERAND (decl, 0) = name = constructor_name (name);
be99da77
MS
9576 my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, 153);
9577 if (ctype == NULL_TREE)
9578 {
9579 if (current_class_type == NULL_TREE)
9580 {
8251199e 9581 error ("destructors must be member functions");
be99da77
MS
9582 flags = NO_SPECIAL;
9583 }
9584 else
9585 {
a723baf1 9586 tree t = constructor_name (current_class_type);
be99da77
MS
9587 if (t != name)
9588 rename = t;
9589 }
9590 }
8d08fdba 9591 else
be99da77
MS
9592 {
9593 tree t = constructor_name (ctype);
9594 if (t != name)
9595 rename = t;
9596 }
51c184be 9597
be99da77 9598 if (rename)
39211cd5 9599 {
33bd39a2 9600 error ("destructor `%T' must match class name `%T'",
5566b478 9601 name, rename);
be99da77 9602 TREE_OPERAND (decl, 0) = rename;
39211cd5 9603 }
be99da77 9604 next = &name;
51c184be 9605 }
be99da77 9606 break;
8d08fdba 9607
be99da77 9608 case ADDR_EXPR: /* C++ reference declaration */
a1c65f9f 9609 /* Fall through. */
be99da77
MS
9610 case ARRAY_REF:
9611 case INDIRECT_REF:
9612 ctype = NULL_TREE;
9613 innermost_code = TREE_CODE (decl);
9614 next = &TREE_OPERAND (decl, 0);
9615 break;
8d08fdba 9616
be99da77 9617 case CALL_EXPR:
be99da77
MS
9618 innermost_code = TREE_CODE (decl);
9619 if (decl_context == FIELD && ctype == NULL_TREE)
9620 ctype = current_class_type;
45537677 9621 if (ctype
c11b6f21 9622 && TREE_OPERAND (decl, 0)
45537677 9623 && (TREE_CODE (TREE_OPERAND (decl, 0)) == TYPE_DECL
8ba658ee
MM
9624 && constructor_name_p (DECL_NAME (TREE_OPERAND (decl, 0)),
9625 ctype)))
be99da77
MS
9626 TREE_OPERAND (decl, 0) = constructor_name (ctype);
9627 next = &TREE_OPERAND (decl, 0);
9628 decl = *next;
9629 if (ctype != NULL_TREE
9630 && decl != NULL_TREE && flags != DTOR_FLAG
a723baf1 9631 && constructor_name_p (decl, ctype))
8d08fdba 9632 {
3dbc07b6 9633 sfk = sfk_constructor;
be99da77 9634 ctor_return_type = ctype;
8d08fdba 9635 }
be99da77
MS
9636 ctype = NULL_TREE;
9637 break;
68642fb6 9638
386b8a85
JM
9639 case TEMPLATE_ID_EXPR:
9640 {
9641 tree fns = TREE_OPERAND (decl, 0);
9642
8f032717
MM
9643 dname = fns;
9644 if (TREE_CODE (dname) == COMPONENT_REF)
9645 dname = TREE_OPERAND (dname, 1);
9646 if (TREE_CODE (dname) != IDENTIFIER_NODE)
9647 {
9648 my_friendly_assert (is_overloaded_fn (dname),
9649 19990331);
9650 dname = DECL_NAME (get_first_fn (dname));
9651 }
386b8a85 9652 }
a1c65f9f 9653 /* Fall through. */
be99da77
MS
9654
9655 case IDENTIFIER_NODE:
386b8a85
JM
9656 if (TREE_CODE (decl) == IDENTIFIER_NODE)
9657 dname = decl;
9658
be99da77
MS
9659 next = 0;
9660
0e5921e8 9661 if (C_IS_RESERVED_WORD (dname))
8d08fdba 9662 {
33bd39a2 9663 error ("declarator-id missing; using reserved word `%D'",
be99da77
MS
9664 dname);
9665 name = IDENTIFIER_POINTER (dname);
8d08fdba 9666 }
596ea4e5 9667 else if (!IDENTIFIER_TYPENAME_P (dname))
be99da77 9668 name = IDENTIFIER_POINTER (dname);
8d08fdba
MS
9669 else
9670 {
596ea4e5
AS
9671 my_friendly_assert (flags == NO_SPECIAL, 154);
9672 flags = TYPENAME_FLAG;
9673 ctor_return_type = TREE_TYPE (dname);
9674 sfk = sfk_conversion;
c003e212 9675 if (is_typename_at_global_scope (dname))
596ea4e5
AS
9676 name = IDENTIFIER_POINTER (dname);
9677 else
9678 name = "<invalid operator>";
8d08fdba 9679 }
be99da77 9680 break;
8d08fdba 9681
be99da77
MS
9682 /* C++ extension */
9683 case SCOPE_REF:
9684 {
9685 /* Perform error checking, and decide on a ctype. */
9686 tree cname = TREE_OPERAND (decl, 0);
9687 if (cname == NULL_TREE)
9688 ctype = NULL_TREE;
2c73f9f5
ML
9689 else if (TREE_CODE (cname) == NAMESPACE_DECL)
9690 {
9691 ctype = NULL_TREE;
9692 in_namespace = TREE_OPERAND (decl, 0);
2c73f9f5 9693 }
be99da77 9694 else if (! is_aggr_type (cname, 1))
a723baf1 9695 ctype = NULL_TREE;
be99da77
MS
9696 /* Must test TREE_OPERAND (decl, 1), in case user gives
9697 us `typedef (class::memfunc)(int); memfunc *memfuncptr;' */
9698 else if (TREE_OPERAND (decl, 1)
9699 && TREE_CODE (TREE_OPERAND (decl, 1)) == INDIRECT_REF)
9700 ctype = cname;
73b0fce8 9701 else if (TREE_CODE (cname) == TEMPLATE_TYPE_PARM
a1281f45 9702 || TREE_CODE (cname) == BOUND_TEMPLATE_TEMPLATE_PARM)
5566b478 9703 {
d5614afb
NS
9704 /* This might be declaring a member of a template
9705 parm to be a friend. */
9706 ctype = cname;
9707 dependant_name = TREE_OPERAND (decl, 1);
5566b478 9708 }
be99da77
MS
9709 else if (ctype == NULL_TREE)
9710 ctype = cname;
9711 else if (TREE_COMPLEXITY (decl) == current_class_depth)
a723baf1 9712 ;
be99da77
MS
9713 else
9714 {
9715 if (! UNIQUELY_DERIVED_FROM_P (cname, ctype))
9716 {
33bd39a2 9717 error ("type `%T' is not derived from type `%T'",
be99da77 9718 cname, ctype);
a723baf1 9719 ctype = NULL_TREE;
be99da77
MS
9720 }
9721 else
9722 ctype = cname;
9723 }
9724
a723baf1
MM
9725 /* It is valid to write:
9726
9727 class C { void f(); };
9728 typedef C D;
9729 void D::f();
9730
9731 The standard is not clear about whether `typedef const C D' is
9732 legal; as of 2002-09-15 the committee is considering
9733 that question. EDG 3.0 allows that syntax.
9734 Therefore, we do as well. */
9735 if (ctype)
9736 ctype = TYPE_MAIN_VARIANT (ctype);
9737 /* Update the declarator so that when we process it
9738 again the correct type is present. */
9739 TREE_OPERAND (decl, 0) = ctype;
9740
c91a56d2 9741 if (ctype && TREE_CODE (TREE_OPERAND (decl, 1)) == TYPE_DECL
8ba658ee
MM
9742 && constructor_name_p (DECL_NAME (TREE_OPERAND (decl, 1)),
9743 ctype))
be99da77
MS
9744 TREE_OPERAND (decl, 1) = constructor_name (ctype);
9745 next = &TREE_OPERAND (decl, 1);
9746 decl = *next;
9747 if (ctype)
9748 {
633221db
NS
9749 tree name = decl;
9750
9751 if (TREE_CODE (name) == BIT_NOT_EXPR)
9752 name = TREE_OPERAND (name, 0);
9753
9754 if (!constructor_name_p (decl, ctype))
9755 ;
9756 else if (decl == name)
be99da77 9757 {
3dbc07b6 9758 sfk = sfk_constructor;
be99da77
MS
9759 ctor_return_type = ctype;
9760 }
633221db 9761 else
be99da77 9762 {
3dbc07b6 9763 sfk = sfk_destructor;
be99da77
MS
9764 ctor_return_type = ctype;
9765 flags = DTOR_FLAG;
9766 TREE_OPERAND (decl, 0) = constructor_name (ctype);
9767 next = &TREE_OPERAND (decl, 0);
9768 }
9769 }
9770 }
9771 break;
9772
9773 case ERROR_MARK:
9774 next = 0;
9775 break;
9776
45537677
MS
9777 case TYPE_DECL:
9778 /* Parse error puts this typespec where
9779 a declarator should go. */
33bd39a2 9780 error ("`%T' specified as declarator-id", DECL_NAME (decl));
45537677 9781 if (TREE_TYPE (decl) == current_class_type)
33bd39a2 9782 error (" perhaps you want `%T' for a constructor",
45537677
MS
9783 current_class_name);
9784 dname = DECL_NAME (decl);
9785 name = IDENTIFIER_POINTER (dname);
9786
e92cc029 9787 /* Avoid giving two errors for this. */
45537677
MS
9788 IDENTIFIER_CLASS_VALUE (dname) = NULL_TREE;
9789
58010b57 9790 declspecs = tree_cons (NULL_TREE, integer_type_node, declspecs);
45537677
MS
9791 *next = dname;
9792 next = 0;
9793 break;
9794
50ad9642
MM
9795 case BASELINK:
9796 next = &BASELINK_FUNCTIONS (decl);
9797 break;
2ee366b5
GDR
9798
9799 case TEMPLATE_DECL:
9800 /* Sometimes, we see a template-name used as part of a
9801 decl-specifier like in
9802 std::allocator alloc;
9803 Handle that gracefully. */
9804 error ("invalid use of template-name '%E' in a declarator", decl);
9805 return error_mark_node;
9806 break;
50ad9642 9807
be99da77 9808 default:
dd29188b 9809 my_friendly_assert (0, 20020917);
8d08fdba 9810 }
be99da77 9811 }
8d08fdba
MS
9812 }
9813
9814 /* A function definition's declarator must have the form of
9815 a function declarator. */
9816
9817 if (funcdef_flag && innermost_code != CALL_EXPR)
9818 return 0;
9819
e1cd6e56
MS
9820 if (((dname && IDENTIFIER_OPNAME_P (dname)) || flags == TYPENAME_FLAG)
9821 && innermost_code != CALL_EXPR
9822 && ! (ctype && declspecs == NULL_TREE))
9823 {
33bd39a2 9824 error ("declaration of `%D' as non-function", dname);
e1cd6e56
MS
9825 return void_type_node;
9826 }
9827
8d08fdba
MS
9828 /* Anything declared one level down from the top level
9829 must be one of the parameters of a function
9830 (because the body is at least two levels down). */
9831
9832 /* This heuristic cannot be applied to C++ nodes! Fixed, however,
9833 by not allowing C++ class definitions to specify their parameters
9834 with xdecls (must be spec.d in the parmlist).
9835
9836 Since we now wait to push a class scope until we are sure that
9837 we are in a legitimate method context, we must set oldcname
a9aedbc2
MS
9838 explicitly (since current_class_name is not yet alive).
9839
9840 We also want to avoid calling this a PARM if it is in a namespace. */
8d08fdba 9841
9188c363 9842 if (decl_context == NORMAL && !toplevel_bindings_p ())
a9aedbc2 9843 {
e2500fed 9844 struct cp_binding_level *b = current_binding_level;
ff955512 9845 current_binding_level = b->level_chain;
a9aedbc2
MS
9846 if (current_binding_level != 0 && toplevel_bindings_p ())
9847 decl_context = PARM;
ff955512 9848 current_binding_level = b;
a9aedbc2 9849 }
8d08fdba 9850
34fc957d
NS
9851 if (name == NULL)
9852 name = decl_context == PARM ? "parameter" : "type name";
68642fb6 9853
8d08fdba
MS
9854 /* Look through the decl specs and record which ones appear.
9855 Some typespecs are defined as built-in typenames.
9856 Others, the ones that are modifiers of other types,
9857 are represented by bits in SPECBITS: set the bits for
9858 the modifiers that appear. Storage class keywords are also in SPECBITS.
9859
9860 If there is a typedef name or a type, store the type in TYPE.
9861 This includes builtin typedefs such as `int'.
9862
9863 Set EXPLICIT_INT if the type is `int' or `char' and did not
9864 come from a user typedef.
9865
9866 Set LONGLONG if `long' is mentioned twice.
9867
9868 For C++, constructors and destructors have their own fast treatment. */
9869
9870 for (spec = declspecs; spec; spec = TREE_CHAIN (spec))
9871 {
9872 register int i;
9873 register tree id;
9874
9875 /* Certain parse errors slip through. For example,
9876 `int class;' is not caught by the parser. Try
9877 weakly to recover here. */
9878 if (TREE_CODE (spec) != TREE_LIST)
9879 return 0;
9880
9881 id = TREE_VALUE (spec);
9882
e23bd218
IR
9883 /* If the entire declaration is itself tagged as deprecated then
9884 suppress reports of deprecated items. */
9885 if (!adding_implicit_members && id && TREE_DEPRECATED (id))
9886 {
9887 if (deprecated_state != DEPRECATED_SUPPRESS)
9888 warn_deprecated_use (id);
9889 }
9890
8d08fdba
MS
9891 if (TREE_CODE (id) == IDENTIFIER_NODE)
9892 {
a3203465
MS
9893 if (id == ridpointers[(int) RID_INT]
9894 || id == ridpointers[(int) RID_CHAR]
9895 || id == ridpointers[(int) RID_BOOL]
9896 || id == ridpointers[(int) RID_WCHAR])
8d08fdba
MS
9897 {
9898 if (type)
8ccc31eb
MS
9899 {
9900 if (id == ridpointers[(int) RID_BOOL])
8251199e 9901 error ("`bool' is now a keyword");
8ccc31eb 9902 else
33bd39a2 9903 error ("extraneous `%T' ignored", id);
8ccc31eb 9904 }
8d08fdba
MS
9905 else
9906 {
a3203465
MS
9907 if (id == ridpointers[(int) RID_INT])
9908 explicit_int = 1;
9909 else if (id == ridpointers[(int) RID_CHAR])
9910 explicit_char = 1;
8d08fdba
MS
9911 type = TREE_TYPE (IDENTIFIER_GLOBAL_VALUE (id));
9912 }
9913 goto found;
9914 }
e92cc029 9915 /* C++ aggregate types. */
8d08fdba
MS
9916 if (IDENTIFIER_HAS_TYPE_VALUE (id))
9917 {
9918 if (type)
33bd39a2 9919 error ("multiple declarations `%T' and `%T'", type, id);
8d08fdba
MS
9920 else
9921 type = IDENTIFIER_TYPE_VALUE (id);
9922 goto found;
9923 }
9924
f376e137 9925 for (i = (int) RID_FIRST_MODIFIER; i <= (int) RID_LAST_MODIFIER; i++)
8d08fdba
MS
9926 {
9927 if (ridpointers[i] == id)
9928 {
9929 if (i == (int) RID_LONG && RIDBIT_SETP (i, specbits))
9930 {
795add94 9931 if (pedantic && ! in_system_header && warn_long_long)
cb9a3ff8 9932 pedwarn ("ISO C++ does not support `long long'");
9a3b49ac 9933 if (longlong)
8251199e 9934 error ("`long long long' is too long for GCC");
8d08fdba
MS
9935 else
9936 longlong = 1;
9937 }
9938 else if (RIDBIT_SETP (i, specbits))
8251199e 9939 pedwarn ("duplicate `%s'", IDENTIFIER_POINTER (id));
7a1f3f5f 9940
946d5e45
MM
9941 /* Diagnose "__thread extern" or "__thread static". */
9942 if (RIDBIT_SETP (RID_THREAD, specbits))
7a1f3f5f 9943 {
946d5e45 9944 if (i == (int)RID_EXTERN)
7a1f3f5f 9945 error ("`__thread' before `extern'");
946d5e45 9946 else if (i == (int)RID_STATIC)
7a1f3f5f
RH
9947 error ("`__thread' before `static'");
9948 }
9949
270d8c65
NS
9950 if (i == (int)RID_EXTERN
9951 && TREE_PURPOSE (spec) == error_mark_node)
9952 /* This extern was part of a language linkage. */
9953 extern_langp = 1;
7a1f3f5f 9954
8d08fdba
MS
9955 RIDBIT_SET (i, specbits);
9956 goto found;
9957 }
9958 }
9959 }
1899c3a4 9960 else if (TREE_CODE (id) == TYPE_DECL)
45537677
MS
9961 {
9962 if (type)
33bd39a2 9963 error ("multiple declarations `%T' and `%T'", type,
45537677
MS
9964 TREE_TYPE (id));
9965 else
5566b478
MS
9966 {
9967 type = TREE_TYPE (id);
9968 TREE_VALUE (spec) = type;
8d6e459d 9969 typedef_decl = id;
5566b478 9970 }
45537677
MS
9971 goto found;
9972 }
8d08fdba 9973 if (type)
8251199e 9974 error ("two or more data types in declaration of `%s'", name);
8d08fdba
MS
9975 else if (TREE_CODE (id) == IDENTIFIER_NODE)
9976 {
9977 register tree t = lookup_name (id, 1);
9978 if (!t || TREE_CODE (t) != TYPE_DECL)
8251199e 9979 error ("`%s' fails to be a typedef or built in type",
8d08fdba
MS
9980 IDENTIFIER_POINTER (id));
9981 else
9982 {
9983 type = TREE_TYPE (t);
9984 typedef_decl = t;
9985 }
9986 }
bd6dd845 9987 else if (id != error_mark_node)
8d08fdba
MS
9988 /* Can't change CLASS nodes into RECORD nodes here! */
9989 type = id;
9990
9991 found: ;
9992 }
9993
8d6e459d
NS
9994#if 0
9995 /* See the code below that used this. */
9996 if (typedef_decl)
9997 decl_attr = DECL_ATTRIBUTES (typedef_decl);
9998#endif
8d08fdba
MS
9999 typedef_type = type;
10000
37c46b43 10001 /* No type at all: default to `int', and set DEFAULTED_INT
6eabb241 10002 because it was not a user-defined typedef. */
8d08fdba 10003
a3203465
MS
10004 if (type == NULL_TREE
10005 && (RIDBIT_SETP (RID_SIGNED, specbits)
10006 || RIDBIT_SETP (RID_UNSIGNED, specbits)
10007 || RIDBIT_SETP (RID_LONG, specbits)
10008 || RIDBIT_SETP (RID_SHORT, specbits)))
10009 {
10010 /* These imply 'int'. */
10011 type = integer_type_node;
37c46b43 10012 defaulted_int = 1;
a3203465
MS
10013 }
10014
3dbc07b6
MM
10015 if (sfk != sfk_none)
10016 type = check_special_function_return_type (sfk, type,
3dbc07b6
MM
10017 ctor_return_type);
10018 else if (type == NULL_TREE)
8d08fdba 10019 {
3dbc07b6
MM
10020 int is_main;
10021
8d08fdba 10022 explicit_int = -1;
3dbc07b6 10023
0fd0b7ee
JM
10024 /* We handle `main' specially here, because 'main () { }' is so
10025 common. With no options, it is allowed. With -Wreturn-type,
10026 it is a warning. It is only an error with -pedantic-errors. */
3dbc07b6 10027 is_main = (funcdef_flag
7437519c 10028 && dname && MAIN_NAME_P (dname)
3dbc07b6
MM
10029 && ctype == NULL_TREE
10030 && in_namespace == NULL_TREE
10031 && current_namespace == global_namespace);
10032
10033 if (in_system_header || flag_ms_extensions)
10034 /* Allow it, sigh. */;
10035 else if (pedantic || ! is_main)
33bd39a2 10036 pedwarn ("ISO C++ forbids declaration of `%s' with no type",
3dbc07b6
MM
10037 name);
10038 else if (warn_return_type)
33bd39a2 10039 warning ("ISO C++ forbids declaration of `%s' with no type",
3dbc07b6 10040 name);
41eff652 10041
3dbc07b6 10042 type = integer_type_node;
51c184be 10043 }
ad1a6d45 10044
8d08fdba
MS
10045 ctype = NULL_TREE;
10046
10047 /* Now process the modifiers that were specified
10048 and check for invalid combinations. */
10049
10050 /* Long double is a special combination. */
10051
10052 if (RIDBIT_SETP (RID_LONG, specbits)
10053 && TYPE_MAIN_VARIANT (type) == double_type_node)
10054 {
10055 RIDBIT_RESET (RID_LONG, specbits);
68642fb6 10056 type = build_qualified_type (long_double_type_node,
89d684bb 10057 cp_type_quals (type));
8d08fdba
MS
10058 }
10059
10060 /* Check all other uses of type modifiers. */
10061
10062 if (RIDBIT_SETP (RID_UNSIGNED, specbits)
10063 || RIDBIT_SETP (RID_SIGNED, specbits)
10064 || RIDBIT_SETP (RID_LONG, specbits)
10065 || RIDBIT_SETP (RID_SHORT, specbits))
10066 {
10067 int ok = 0;
10068
10069 if (TREE_CODE (type) == REAL_TYPE)
8251199e 10070 error ("short, signed or unsigned invalid for `%s'", name);
b7484fbe 10071 else if (TREE_CODE (type) != INTEGER_TYPE)
8251199e 10072 error ("long, short, signed or unsigned invalid for `%s'", name);
8d08fdba
MS
10073 else if (RIDBIT_SETP (RID_LONG, specbits)
10074 && RIDBIT_SETP (RID_SHORT, specbits))
8251199e 10075 error ("long and short specified together for `%s'", name);
8d08fdba
MS
10076 else if ((RIDBIT_SETP (RID_LONG, specbits)
10077 || RIDBIT_SETP (RID_SHORT, specbits))
10078 && explicit_char)
8251199e 10079 error ("long or short specified with char for `%s'", name);
8d08fdba
MS
10080 else if ((RIDBIT_SETP (RID_LONG, specbits)
10081 || RIDBIT_SETP (RID_SHORT, specbits))
10082 && TREE_CODE (type) == REAL_TYPE)
8251199e 10083 error ("long or short specified with floating type for `%s'", name);
8d08fdba
MS
10084 else if (RIDBIT_SETP (RID_SIGNED, specbits)
10085 && RIDBIT_SETP (RID_UNSIGNED, specbits))
8251199e 10086 error ("signed and unsigned given together for `%s'", name);
8d08fdba
MS
10087 else
10088 {
10089 ok = 1;
37c46b43 10090 if (!explicit_int && !defaulted_int && !explicit_char && pedantic)
8d08fdba 10091 {
8251199e 10092 pedwarn ("long, short, signed or unsigned used invalidly for `%s'",
8d08fdba
MS
10093 name);
10094 if (flag_pedantic_errors)
10095 ok = 0;
10096 }
10097 }
10098
10099 /* Discard the type modifiers if they are invalid. */
10100 if (! ok)
10101 {
10102 RIDBIT_RESET (RID_UNSIGNED, specbits);
10103 RIDBIT_RESET (RID_SIGNED, specbits);
10104 RIDBIT_RESET (RID_LONG, specbits);
10105 RIDBIT_RESET (RID_SHORT, specbits);
10106 longlong = 0;
10107 }
10108 }
10109
37c46b43
MS
10110 if (RIDBIT_SETP (RID_COMPLEX, specbits)
10111 && TREE_CODE (type) != INTEGER_TYPE && TREE_CODE (type) != REAL_TYPE)
10112 {
8251199e 10113 error ("complex invalid for `%s'", name);
37c46b43
MS
10114 RIDBIT_RESET (RID_COMPLEX, specbits);
10115 }
10116
8d08fdba
MS
10117 /* Decide whether an integer type is signed or not.
10118 Optionally treat bitfields as signed by default. */
10119 if (RIDBIT_SETP (RID_UNSIGNED, specbits)
b89c5a7b
MM
10120 /* [class.bit]
10121
10122 It is implementation-defined whether a plain (neither
10123 explicitly signed or unsigned) char, short, int, or long
10124 bit-field is signed or unsigned.
68642fb6 10125
b89c5a7b
MM
10126 Naturally, we extend this to long long as well. Note that
10127 this does not include wchar_t. */
10128 || (bitfield && !flag_signed_bitfields
10129 && RIDBIT_NOTSETP (RID_SIGNED, specbits)
10130 /* A typedef for plain `int' without `signed' can be
10131 controlled just like plain `int', but a typedef for
10132 `signed int' cannot be so controlled. */
68642fb6 10133 && !(typedef_decl
29bbeb1c 10134 && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl))
b89c5a7b
MM
10135 && (TREE_CODE (type) == INTEGER_TYPE
10136 || TREE_CODE (type) == CHAR_TYPE)
29bbeb1c 10137 && !same_type_p (TYPE_MAIN_VARIANT (type), wchar_type_node)))
8d08fdba
MS
10138 {
10139 if (longlong)
10140 type = long_long_unsigned_type_node;
10141 else if (RIDBIT_SETP (RID_LONG, specbits))
10142 type = long_unsigned_type_node;
10143 else if (RIDBIT_SETP (RID_SHORT, specbits))
10144 type = short_unsigned_type_node;
10145 else if (type == char_type_node)
10146 type = unsigned_char_type_node;
10147 else if (typedef_decl)
ceef8ce4 10148 type = c_common_unsigned_type (type);
8d08fdba
MS
10149 else
10150 type = unsigned_type_node;
10151 }
10152 else if (RIDBIT_SETP (RID_SIGNED, specbits)
10153 && type == char_type_node)
10154 type = signed_char_type_node;
10155 else if (longlong)
10156 type = long_long_integer_type_node;
10157 else if (RIDBIT_SETP (RID_LONG, specbits))
10158 type = long_integer_type_node;
10159 else if (RIDBIT_SETP (RID_SHORT, specbits))
10160 type = short_integer_type_node;
10161
37c46b43
MS
10162 if (RIDBIT_SETP (RID_COMPLEX, specbits))
10163 {
10164 /* If we just have "complex", it is equivalent to
10165 "complex double", but if any modifiers at all are specified it is
10166 the complex form of TYPE. E.g, "complex short" is
10167 "complex short int". */
10168
10169 if (defaulted_int && ! longlong
10170 && ! (RIDBIT_SETP (RID_LONG, specbits)
10171 || RIDBIT_SETP (RID_SHORT, specbits)
10172 || RIDBIT_SETP (RID_SIGNED, specbits)
10173 || RIDBIT_SETP (RID_UNSIGNED, specbits)))
10174 type = complex_double_type_node;
10175 else if (type == integer_type_node)
10176 type = complex_integer_type_node;
10177 else if (type == float_type_node)
10178 type = complex_float_type_node;
10179 else if (type == double_type_node)
10180 type = complex_double_type_node;
10181 else if (type == long_double_type_node)
10182 type = complex_long_double_type_node;
10183 else
10184 type = build_complex_type (type);
10185 }
10186
4f2b0fb2
NS
10187 type_quals = TYPE_UNQUALIFIED;
10188 if (RIDBIT_SETP (RID_CONST, specbits))
10189 type_quals |= TYPE_QUAL_CONST;
10190 if (RIDBIT_SETP (RID_VOLATILE, specbits))
10191 type_quals |= TYPE_QUAL_VOLATILE;
10192 if (RIDBIT_SETP (RID_RESTRICT, specbits))
10193 type_quals |= TYPE_QUAL_RESTRICT;
10194 if (sfk == sfk_conversion && type_quals != TYPE_UNQUALIFIED)
33bd39a2 10195 error ("qualifiers are not allowed on declaration of `operator %T'",
3ac3d9ea
MM
10196 ctor_return_type);
10197
4f2b0fb2
NS
10198 type_quals |= cp_type_quals (type);
10199 type = cp_build_qualified_type_real
10200 (type, type_quals, ((typedef_decl && !DECL_ARTIFICIAL (typedef_decl)
10201 ? tf_ignore_bad_quals : 0) | tf_error | tf_warning));
10202 /* We might have ignored or rejected some of the qualifiers. */
10203 type_quals = cp_type_quals (type);
10204
8d08fdba
MS
10205 staticp = 0;
10206 inlinep = !! RIDBIT_SETP (RID_INLINE, specbits);
8d08fdba 10207 virtualp = RIDBIT_SETP (RID_VIRTUAL, specbits);
db5ae43f
MS
10208 RIDBIT_RESET (RID_VIRTUAL, specbits);
10209 explicitp = RIDBIT_SETP (RID_EXPLICIT, specbits) != 0;
10210 RIDBIT_RESET (RID_EXPLICIT, specbits);
8d08fdba 10211
8d08fdba
MS
10212 if (RIDBIT_SETP (RID_STATIC, specbits))
10213 staticp = 1 + (decl_context == FIELD);
10214
10215 if (virtualp && staticp == 2)
10216 {
33bd39a2 10217 error ("member `%D' cannot be declared both virtual and static",
8d08fdba
MS
10218 dname);
10219 staticp = 0;
10220 }
10221 friendp = RIDBIT_SETP (RID_FRIEND, specbits);
8d08fdba
MS
10222 RIDBIT_RESET (RID_FRIEND, specbits);
10223
d5614afb
NS
10224 if (dependant_name && !friendp)
10225 {
10226 error ("`%T::%D' is not a valid declarator", ctype, dependant_name);
10227 return void_type_node;
10228 }
10229
8d08fdba
MS
10230 /* Warn if two storage classes are given. Default to `auto'. */
10231
10232 if (RIDBIT_ANY_SET (specbits))
10233 {
10234 if (RIDBIT_SETP (RID_STATIC, specbits)) nclasses++;
270d8c65 10235 if (RIDBIT_SETP (RID_EXTERN, specbits) && !extern_langp) nclasses++;
7a1f3f5f 10236 if (RIDBIT_SETP (RID_THREAD, specbits)) nclasses++;
8d08fdba 10237 if (decl_context == PARM && nclasses > 0)
8251199e 10238 error ("storage class specifiers invalid in parameter declarations");
8d08fdba
MS
10239 if (RIDBIT_SETP (RID_TYPEDEF, specbits))
10240 {
10241 if (decl_context == PARM)
8251199e 10242 error ("typedef declaration invalid in parameter declaration");
8d08fdba
MS
10243 nclasses++;
10244 }
10245 if (RIDBIT_SETP (RID_AUTO, specbits)) nclasses++;
10246 if (RIDBIT_SETP (RID_REGISTER, specbits)) nclasses++;
270d8c65
NS
10247 if (!nclasses && !friendp && extern_langp)
10248 nclasses++;
8d08fdba
MS
10249 }
10250
10251 /* Give error if `virtual' is used outside of class declaration. */
b7484fbe
MS
10252 if (virtualp
10253 && (current_class_name == NULL_TREE || decl_context != FIELD))
8d08fdba 10254 {
8251199e 10255 error ("virtual outside class declaration");
8d08fdba
MS
10256 virtualp = 0;
10257 }
8d08fdba
MS
10258
10259 /* Static anonymous unions are dealt with here. */
10260 if (staticp && decl_context == TYPENAME
10261 && TREE_CODE (declspecs) == TREE_LIST
6bdb8141 10262 && ANON_AGGR_TYPE_P (TREE_VALUE (declspecs)))
8d08fdba
MS
10263 decl_context = FIELD;
10264
8d08fdba
MS
10265 /* Warn about storage classes that are invalid for certain
10266 kinds of declarations (parameters, typenames, etc.). */
10267
7a1f3f5f
RH
10268 /* "static __thread" and "extern __thread" are allowed. */
10269 if (nclasses == 2
10270 && RIDBIT_SETP (RID_THREAD, specbits)
10271 && (RIDBIT_SETP (RID_EXTERN, specbits)
10272 || RIDBIT_SETP (RID_STATIC, specbits)))
10273 nclasses = 1;
10274
8d08fdba 10275 if (nclasses > 1)
8251199e 10276 error ("multiple storage classes in declaration of `%s'", name);
8d08fdba
MS
10277 else if (decl_context != NORMAL && nclasses > 0)
10278 {
db5ae43f 10279 if ((decl_context == PARM || decl_context == CATCHPARM)
8d08fdba
MS
10280 && (RIDBIT_SETP (RID_REGISTER, specbits)
10281 || RIDBIT_SETP (RID_AUTO, specbits)))
10282 ;
fc378698
MS
10283 else if (RIDBIT_SETP (RID_TYPEDEF, specbits))
10284 ;
8d08fdba 10285 else if (decl_context == FIELD
d363e7bf
AJ
10286 /* C++ allows static class elements */
10287 && RIDBIT_SETP (RID_STATIC, specbits))
10288 /* C++ also allows inlines and signed and unsigned elements,
10289 but in those cases we don't come in here. */
8d08fdba
MS
10290 ;
10291 else
10292 {
10293 if (decl_context == FIELD)
10294 {
b7484fbe
MS
10295 tree tmp = NULL_TREE;
10296 register int op = 0;
10297
10298 if (declarator)
10299 {
68642fb6 10300 /* Avoid trying to get an operand off an identifier node. */
9e9ff709
MS
10301 if (TREE_CODE (declarator) == IDENTIFIER_NODE)
10302 tmp = declarator;
10303 else
10304 tmp = TREE_OPERAND (declarator, 0);
b7484fbe 10305 op = IDENTIFIER_OPNAME_P (tmp);
596ea4e5
AS
10306 if (IDENTIFIER_TYPENAME_P (tmp))
10307 {
c003e212 10308 if (is_typename_at_global_scope (tmp))
596ea4e5
AS
10309 name = IDENTIFIER_POINTER (tmp);
10310 else
10311 name = "<invalid operator>";
10312 }
b7484fbe 10313 }
8251199e 10314 error ("storage class specified for %s `%s'",
6eabb241 10315 op ? "member operator" : "field",
596ea4e5 10316 name);
8d08fdba
MS
10317 }
10318 else
b9d12519
KG
10319 {
10320 if (decl_context == PARM || decl_context == CATCHPARM)
10321 error ("storage class specified for parameter `%s'", name);
10322 else
10323 error ("storage class specified for typename");
10324 }
8d08fdba
MS
10325 RIDBIT_RESET (RID_REGISTER, specbits);
10326 RIDBIT_RESET (RID_AUTO, specbits);
10327 RIDBIT_RESET (RID_EXTERN, specbits);
7a1f3f5f 10328 RIDBIT_RESET (RID_THREAD, specbits);
8d08fdba
MS
10329 }
10330 }
10331 else if (RIDBIT_SETP (RID_EXTERN, specbits) && initialized && !funcdef_flag)
10332 {
a9aedbc2 10333 if (toplevel_bindings_p ())
8d08fdba 10334 {
59be0cdd 10335 /* It's common practice (and completely valid) to have a const
8d08fdba 10336 be initialized and declared extern. */
950ad3c3 10337 if (!(type_quals & TYPE_QUAL_CONST))
8251199e 10338 warning ("`%s' initialized and declared `extern'", name);
8d08fdba
MS
10339 }
10340 else
8251199e 10341 error ("`%s' has both `extern' and initializer", name);
8d08fdba
MS
10342 }
10343 else if (RIDBIT_SETP (RID_EXTERN, specbits) && funcdef_flag
a9aedbc2 10344 && ! toplevel_bindings_p ())
8251199e 10345 error ("nested function `%s' declared `extern'", name);
a9aedbc2 10346 else if (toplevel_bindings_p ())
8d08fdba
MS
10347 {
10348 if (RIDBIT_SETP (RID_AUTO, specbits))
8251199e 10349 error ("top-level declaration of `%s' specifies `auto'", name);
8d08fdba 10350 }
7a1f3f5f
RH
10351 else if (RIDBIT_SETP (RID_THREAD, specbits)
10352 && !RIDBIT_SETP (RID_EXTERN, specbits)
10353 && !RIDBIT_SETP (RID_STATIC, specbits))
10354 {
10355 error ("function-scope `%s' implicitly auto and declared `__thread'",
10356 name);
10357 RIDBIT_RESET (RID_THREAD, specbits);
10358 }
8d08fdba 10359
909e536a 10360 if (nclasses > 0 && friendp)
8251199e 10361 error ("storage class specifiers invalid in friend function declarations");
909e536a 10362
a723baf1
MM
10363 scope = get_scope_of_declarator (declarator);
10364
8d08fdba
MS
10365 /* Now figure out the structure of the declarator proper.
10366 Descend through it, creating more complex types, until we reach
9e259dd1 10367 the declared identifier (or NULL_TREE, in an abstract declarator). */
8d08fdba 10368
386b8a85
JM
10369 while (declarator && TREE_CODE (declarator) != IDENTIFIER_NODE
10370 && TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
8d08fdba
MS
10371 {
10372 /* Each level of DECLARATOR is either an ARRAY_REF (for ...[..]),
10373 an INDIRECT_REF (for *...),
10374 a CALL_EXPR (for ...(...)),
10375 an identifier (for the name being declared)
10376 or a null pointer (for the place in an absolute declarator
10377 where the name was omitted).
10378 For the last two cases, we have just exited the loop.
10379
10380 For C++ it could also be
10381 a SCOPE_REF (for class :: ...). In this case, we have converted
10382 sensible names to types, and those are the values we use to
10383 qualify the member name.
10384 an ADDR_EXPR (for &...),
10385 a BIT_NOT_EXPR (for destructors)
8d08fdba
MS
10386
10387 At this point, TYPE is the type of elements of an array,
10388 or for a function to return, or for a pointer to point to.
10389 After this sequence of ifs, TYPE is the type of the
10390 array or function or pointer, and DECLARATOR has had its
10391 outermost layer removed. */
10392
bd6dd845 10393 if (type == error_mark_node)
8d08fdba 10394 {
88d5a16e
R
10395 if (declarator == error_mark_node)
10396 return error_mark_node;
10397 else if (TREE_CODE (declarator) == SCOPE_REF)
8d08fdba
MS
10398 declarator = TREE_OPERAND (declarator, 1);
10399 else
10400 declarator = TREE_OPERAND (declarator, 0);
10401 continue;
10402 }
10403 if (quals != NULL_TREE
10404 && (declarator == NULL_TREE
10405 || TREE_CODE (declarator) != SCOPE_REF))
10406 {
10407 if (ctype == NULL_TREE && TREE_CODE (type) == METHOD_TYPE)
10408 ctype = TYPE_METHOD_BASETYPE (type);
10409 if (ctype != NULL_TREE)
10410 {
8d08fdba 10411 tree dummy = build_decl (TYPE_DECL, NULL_TREE, type);
535233a8 10412 grok_method_quals (ctype, dummy, quals);
8d08fdba
MS
10413 type = TREE_TYPE (dummy);
10414 quals = NULL_TREE;
10415 }
10416 }
1eab9b56 10417
8d08fdba
MS
10418 switch (TREE_CODE (declarator))
10419 {
52fbc847
JM
10420 case TREE_LIST:
10421 {
10422 /* We encode a declarator with embedded attributes using
91d231cb
JM
10423 a TREE_LIST. */
10424 tree attrs = TREE_PURPOSE (declarator);
10425 tree inner_decl;
c9574c9f
R
10426 int attr_flags;
10427
52fbc847 10428 declarator = TREE_VALUE (declarator);
91d231cb
JM
10429 inner_decl = declarator;
10430 while (inner_decl != NULL_TREE
10431 && TREE_CODE (inner_decl) == TREE_LIST)
10432 inner_decl = TREE_VALUE (inner_decl);
c9574c9f 10433 attr_flags = 0;
91d231cb
JM
10434 if (inner_decl == NULL_TREE
10435 || TREE_CODE (inner_decl) == IDENTIFIER_NODE)
10436 attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
10437 if (TREE_CODE (inner_decl) == CALL_EXPR)
10438 attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
10439 if (TREE_CODE (inner_decl) == ARRAY_REF)
10440 attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
10441 returned_attrs = decl_attributes (&type,
10442 chainon (returned_attrs, attrs),
10443 attr_flags);
52fbc847
JM
10444 }
10445 break;
10446
8d08fdba
MS
10447 case ARRAY_REF:
10448 {
01bf0f3e 10449 tree size = TREE_OPERAND (declarator, 1);
817aed6f 10450 declarator = TREE_OPERAND (declarator, 0);
8d08fdba 10451
2848ad0f 10452 type = create_array_type_for_decl (dname, type, size);
c1b177ec 10453
8d08fdba
MS
10454 ctype = NULL_TREE;
10455 }
10456 break;
10457
10458 case CALL_EXPR:
10459 {
10460 tree arg_types;
f376e137 10461 int funcdecl_p;
43f887f9 10462 tree inner_parms = CALL_DECLARATOR_PARMS (declarator);
f376e137 10463 tree inner_decl = TREE_OPERAND (declarator, 0);
8d08fdba
MS
10464
10465 /* Declaring a function type.
10466 Make sure we have a valid type for the function to return. */
8d08fdba 10467
91063b51 10468 /* We now know that the TYPE_QUALS don't apply to the
14ae7e7d 10469 decl, but to its return type. */
91063b51 10470 type_quals = TYPE_UNQUALIFIED;
8d08fdba
MS
10471
10472 /* Warn about some types functions can't return. */
10473
10474 if (TREE_CODE (type) == FUNCTION_TYPE)
10475 {
8251199e 10476 error ("`%s' declared as function returning a function", name);
8d08fdba
MS
10477 type = integer_type_node;
10478 }
10479 if (TREE_CODE (type) == ARRAY_TYPE)
10480 {
8251199e 10481 error ("`%s' declared as function returning an array", name);
8d08fdba
MS
10482 type = integer_type_node;
10483 }
10484
f376e137
MS
10485 if (inner_decl && TREE_CODE (inner_decl) == SCOPE_REF)
10486 inner_decl = TREE_OPERAND (inner_decl, 1);
10487
68642fb6 10488 if (inner_decl && TREE_CODE (inner_decl) == TEMPLATE_ID_EXPR)
386b8a85
JM
10489 inner_decl = dname;
10490
b7484fbe 10491 /* Pick up type qualifiers which should be applied to `this'. */
43f887f9 10492 quals = CALL_DECLARATOR_QUALS (declarator);
b7484fbe 10493
c11b6f21 10494 /* Pick up the exception specifications. */
43f887f9 10495 raises = CALL_DECLARATOR_EXCEPTION_SPEC (declarator);
c11b6f21 10496
f376e137
MS
10497 /* Say it's a definition only for the CALL_EXPR
10498 closest to the identifier. */
beb53fb8 10499 funcdecl_p
68642fb6 10500 = inner_decl
386b8a85 10501 && (TREE_CODE (inner_decl) == IDENTIFIER_NODE
68642fb6 10502 || TREE_CODE (inner_decl) == TEMPLATE_ID_EXPR
386b8a85 10503 || TREE_CODE (inner_decl) == BIT_NOT_EXPR);
68642fb6 10504
8d08fdba
MS
10505 if (ctype == NULL_TREE
10506 && decl_context == FIELD
f376e137 10507 && funcdecl_p
8d08fdba
MS
10508 && (friendp == 0 || dname == current_class_name))
10509 ctype = current_class_type;
10510
3dbc07b6 10511 if (ctype && sfk == sfk_conversion)
8d08fdba 10512 TYPE_HAS_CONVERSION (ctype) = 1;
a723baf1 10513 if (ctype && constructor_name_p (dname, ctype))
8d08fdba
MS
10514 {
10515 /* We are within a class's scope. If our declarator name
10516 is the same as the class name, and we are defining
10517 a function, then it is a constructor/destructor, and
10518 therefore returns a void type. */
10519
10520 if (flags == DTOR_FLAG)
10521 {
cab1f180
ML
10522 /* ISO C++ 12.4/2. A destructor may not be
10523 declared const or volatile. A destructor may
10524 not be static. */
8d08fdba 10525 if (staticp == 2)
8251199e 10526 error ("destructor cannot be static member function");
b7484fbe 10527 if (quals)
8d08fdba 10528 {
33bd39a2 10529 error ("destructors may not be `%s'",
4f70a846
MM
10530 IDENTIFIER_POINTER (TREE_VALUE (quals)));
10531 quals = NULL_TREE;
8d08fdba
MS
10532 }
10533 if (decl_context == FIELD)
10534 {
4dacf5bd
KG
10535 if (! member_function_or_else (ctype,
10536 current_class_type,
10537 flags))
8d08fdba
MS
10538 return void_type_node;
10539 }
10540 }
2c73f9f5 10541 else /* It's a constructor. */
8d08fdba 10542 {
db5ae43f
MS
10543 if (explicitp == 1)
10544 explicitp = 2;
cab1f180
ML
10545 /* ISO C++ 12.1. A constructor may not be
10546 declared const or volatile. A constructor may
10547 not be virtual. A constructor may not be
10548 static. */
8d08fdba 10549 if (staticp == 2)
8251199e 10550 error ("constructor cannot be static member function");
8d08fdba
MS
10551 if (virtualp)
10552 {
8251199e 10553 pedwarn ("constructors cannot be declared virtual");
8d08fdba
MS
10554 virtualp = 0;
10555 }
b7484fbe 10556 if (quals)
8d08fdba 10557 {
33bd39a2 10558 error ("constructors may not be `%s'",
4f70a846
MM
10559 IDENTIFIER_POINTER (TREE_VALUE (quals)));
10560 quals = NULL_TREE;
d363e7bf 10561 }
8d08fdba 10562 {
51c184be 10563 RID_BIT_TYPE tmp_bits;
4e135bdd 10564 memcpy (&tmp_bits, &specbits, sizeof (RID_BIT_TYPE));
51c184be
MS
10565 RIDBIT_RESET (RID_INLINE, tmp_bits);
10566 RIDBIT_RESET (RID_STATIC, tmp_bits);
10567 if (RIDBIT_ANY_SET (tmp_bits))
8251199e 10568 error ("return value type specifier for constructor ignored");
8d08fdba 10569 }
6eabb241 10570 if (decl_context == FIELD)
8d08fdba 10571 {
4dacf5bd
KG
10572 if (! member_function_or_else (ctype,
10573 current_class_type,
10574 flags))
8d08fdba
MS
10575 return void_type_node;
10576 TYPE_HAS_CONSTRUCTOR (ctype) = 1;
3dbc07b6 10577 if (sfk != sfk_constructor)
8d08fdba
MS
10578 return NULL_TREE;
10579 }
10580 }
10581 if (decl_context == FIELD)
10582 staticp = 0;
10583 }
b7484fbe 10584 else if (friendp)
8d08fdba 10585 {
b7484fbe 10586 if (initialized)
8251199e 10587 error ("can't initialize friend function `%s'", name);
b7484fbe
MS
10588 if (virtualp)
10589 {
10590 /* Cannot be both friend and virtual. */
8251199e 10591 error ("virtual functions cannot be friends");
b7484fbe
MS
10592 RIDBIT_RESET (RID_FRIEND, specbits);
10593 friendp = 0;
10594 }
28cbf42c 10595 if (decl_context == NORMAL)
8251199e 10596 error ("friend declaration not in class definition");
28cbf42c 10597 if (current_function_decl && funcdef_flag)
33bd39a2 10598 error ("can't define friend function `%s' in a local class definition",
28cbf42c 10599 name);
8d08fdba
MS
10600 }
10601
8d08fdba
MS
10602 /* Construct the function type and go to the next
10603 inner layer of declarator. */
10604
f376e137 10605 declarator = TREE_OPERAND (declarator, 0);
8d08fdba 10606
5cce22b6 10607 arg_types = grokparms (inner_parms);
8d08fdba 10608
4f70a846 10609 if (declarator && flags == DTOR_FLAG)
8d08fdba 10610 {
4f70a846
MM
10611 /* A destructor declared in the body of a class will
10612 be represented as a BIT_NOT_EXPR. But, we just
10613 want the underlying IDENTIFIER. */
8d08fdba 10614 if (TREE_CODE (declarator) == BIT_NOT_EXPR)
4f70a846 10615 declarator = TREE_OPERAND (declarator, 0);
68642fb6 10616
8b27e9ef 10617 if (arg_types != void_list_node)
8d08fdba 10618 {
33bd39a2 10619 error ("destructors may not have parameters");
4f70a846
MM
10620 arg_types = void_list_node;
10621 last_function_parms = NULL_TREE;
8d08fdba
MS
10622 }
10623 }
10624
d22c8596 10625 /* ANSI says that `const int foo ();'
8d08fdba 10626 does not make the function foo const. */
d22c8596 10627 type = build_function_type (type, arg_types);
8d08fdba
MS
10628 }
10629 break;
10630
10631 case ADDR_EXPR:
10632 case INDIRECT_REF:
10633 /* Filter out pointers-to-references and references-to-references.
10634 We can get these if a TYPE_DECL is used. */
10635
10636 if (TREE_CODE (type) == REFERENCE_TYPE)
10637 {
dd29188b
NS
10638 error (TREE_CODE (declarator) == ADDR_EXPR
10639 ? "cannot declare reference to `%#T'"
10640 : "cannot declare pointer to `%#T'", type);
a5894242
MS
10641 type = TREE_TYPE (type);
10642 }
dd29188b
NS
10643 else if (VOID_TYPE_P (type)
10644 && (ctype || TREE_CODE (declarator) == ADDR_EXPR))
10645 error (ctype ? "cannot declare pointer to `%#T' member"
10646 : "cannot declare reference to `%#T'", type);
a5894242 10647
61a127b3
MM
10648 /* Merge any constancy or volatility into the target type
10649 for the pointer. */
10650
91063b51
MM
10651 /* We now know that the TYPE_QUALS don't apply to the decl,
10652 but to the target of the pointer. */
10653 type_quals = TYPE_UNQUALIFIED;
8d08fdba 10654
6eabb241 10655 if (TREE_CODE (declarator) == ADDR_EXPR)
8d08fdba 10656 {
dd29188b 10657 if (!VOID_TYPE_P (type))
69851283 10658 type = build_reference_type (type);
8d08fdba
MS
10659 }
10660 else if (TREE_CODE (type) == METHOD_TYPE)
d8f8dca1 10661 type = build_ptrmemfunc_type (build_pointer_type (type));
9e259dd1
MM
10662 else if (ctype)
10663 type = build_ptrmem_type (ctype, type);
8d08fdba
MS
10664 else
10665 type = build_pointer_type (type);
10666
10667 /* Process a list of type modifier keywords (such as
10668 const or volatile) that were given inside the `*' or `&'. */
10669
10670 if (TREE_TYPE (declarator))
10671 {
10672 register tree typemodlist;
10673 int erred = 0;
4f2b0fb2
NS
10674 int constp = 0;
10675 int volatilep = 0;
10676 int restrictp = 0;
10677
8d08fdba
MS
10678 for (typemodlist = TREE_TYPE (declarator); typemodlist;
10679 typemodlist = TREE_CHAIN (typemodlist))
10680 {
91063b51
MM
10681 tree qualifier = TREE_VALUE (typemodlist);
10682
10683 if (qualifier == ridpointers[(int) RID_CONST])
4f2b0fb2
NS
10684 {
10685 constp++;
10686 type_quals |= TYPE_QUAL_CONST;
10687 }
91063b51 10688 else if (qualifier == ridpointers[(int) RID_VOLATILE])
4f2b0fb2
NS
10689 {
10690 volatilep++;
10691 type_quals |= TYPE_QUAL_VOLATILE;
10692 }
91063b51 10693 else if (qualifier == ridpointers[(int) RID_RESTRICT])
4f2b0fb2
NS
10694 {
10695 restrictp++;
10696 type_quals |= TYPE_QUAL_RESTRICT;
10697 }
8d08fdba
MS
10698 else if (!erred)
10699 {
10700 erred = 1;
91063b51 10701 error ("invalid type modifier within pointer declarator");
8d08fdba
MS
10702 }
10703 }
10704 if (constp > 1)
8251199e 10705 pedwarn ("duplicate `const'");
8d08fdba 10706 if (volatilep > 1)
8251199e 10707 pedwarn ("duplicate `volatile'");
91063b51
MM
10708 if (restrictp > 1)
10709 pedwarn ("duplicate `restrict'");
91063b51 10710 type = cp_build_qualified_type (type, type_quals);
4f2b0fb2 10711 type_quals = cp_type_quals (type);
8d08fdba
MS
10712 }
10713 declarator = TREE_OPERAND (declarator, 0);
10714 ctype = NULL_TREE;
10715 break;
10716
10717 case SCOPE_REF:
10718 {
10719 /* We have converted type names to NULL_TREE if the
10720 name was bogus, or to a _TYPE node, if not.
10721
10722 The variable CTYPE holds the type we will ultimately
10723 resolve to. The code here just needs to build
10724 up appropriate member types. */
10725 tree sname = TREE_OPERAND (declarator, 1);
386b8a85
JM
10726 tree t;
10727
8d08fdba
MS
10728 /* Destructors can have their visibilities changed as well. */
10729 if (TREE_CODE (sname) == BIT_NOT_EXPR)
10730 sname = TREE_OPERAND (sname, 0);
10731
8d08fdba
MS
10732 if (TREE_OPERAND (declarator, 0) == NULL_TREE)
10733 {
10734 /* We had a reference to a global decl, or
10735 perhaps we were given a non-aggregate typedef,
10736 in which case we cleared this out, and should just
10737 keep going as though it wasn't there. */
10738 declarator = sname;
10739 continue;
10740 }
10741 ctype = TREE_OPERAND (declarator, 0);
10742
386b8a85 10743 t = ctype;
68642fb6 10744 while (t != NULL_TREE && CLASS_TYPE_P (t))
386b8a85 10745 {
5362b086 10746 /* You're supposed to have one `template <...>'
da8a7456
MM
10747 for every template class, but you don't need one
10748 for a full specialization. For example:
10749
10750 template <class T> struct S{};
10751 template <> struct S<int> { void f(); };
10752 void S<int>::f () {}
10753
10754 is correct; there shouldn't be a `template <>' for
10755 the definition of `S<int>::f'. */
10756 if (CLASSTYPE_TEMPLATE_INFO (t)
10757 && (CLASSTYPE_TEMPLATE_INSTANTIATION (t)
92ecdfb7
NS
10758 || uses_template_parms (CLASSTYPE_TI_ARGS (t)))
10759 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t)))
386b8a85 10760 template_count += 1;
da8a7456 10761
386b8a85 10762 t = TYPE_MAIN_DECL (t);
92ecdfb7 10763 t = DECL_CONTEXT (t);
386b8a85
JM
10764 }
10765
8d08fdba
MS
10766 if (sname == NULL_TREE)
10767 goto done_scoping;
10768
10769 if (TREE_CODE (sname) == IDENTIFIER_NODE)
10770 {
10771 /* This is the `standard' use of the scoping operator:
10772 basetype :: member . */
10773
db5ae43f 10774 if (ctype == current_class_type)
28cbf42c
MS
10775 {
10776 /* class A {
10777 void A::f ();
10778 };
10779
10780 Is this ill-formed? */
10781
10782 if (pedantic)
33bd39a2 10783 pedwarn ("extra qualification `%T::' on member `%s' ignored",
28cbf42c
MS
10784 ctype, name);
10785 }
db5ae43f 10786 else if (TREE_CODE (type) == FUNCTION_TYPE)
8d08fdba 10787 {
eeb75383 10788 if (current_class_type == NULL_TREE || friendp)
43dc123f
MM
10789 type
10790 = build_method_type_directly (ctype,
10791 TREE_TYPE (type),
14ae7e7d 10792 TYPE_ARG_TYPES (type));
8d08fdba
MS
10793 else
10794 {
33bd39a2 10795 error ("cannot declare member function `%T::%s' within `%T'",
a3203465 10796 ctype, name, current_class_type);
a723baf1 10797 return error_mark_node;
8d08fdba
MS
10798 }
10799 }
5566b478 10800 else if (RIDBIT_SETP (RID_TYPEDEF, specbits)
d0f062fb 10801 || COMPLETE_TYPE_P (complete_type (ctype)))
8d08fdba 10802 {
2c73f9f5 10803 /* Have to move this code elsewhere in this function.
db5ae43f
MS
10804 this code is used for i.e., typedef int A::M; M *pm;
10805
10806 It is? How? jason 10/2/94 */
8d08fdba 10807
8d08fdba
MS
10808 if (current_class_type)
10809 {
33bd39a2 10810 error ("cannot declare member `%T::%s' within `%T'",
db5ae43f
MS
10811 ctype, name, current_class_type);
10812 return void_type_node;
8d08fdba 10813 }
8d08fdba 10814 }
8d08fdba 10815 else
eeb75383 10816 {
7a228918 10817 cxx_incomplete_type_error (NULL_TREE, ctype);
eeb75383 10818 return error_mark_node;
d363e7bf 10819 }
8d08fdba
MS
10820
10821 declarator = sname;
10822 }
8d08fdba 10823 else if (TREE_CODE (sname) == SCOPE_REF)
a98facb0 10824 abort ();
8d08fdba
MS
10825 else
10826 {
10827 done_scoping:
10828 declarator = TREE_OPERAND (declarator, 1);
10829 if (declarator && TREE_CODE (declarator) == CALL_EXPR)
10830 /* In this case, we will deal with it later. */
10831 ;
9e259dd1 10832 else if (TREE_CODE (type) == FUNCTION_TYPE)
43dc123f
MM
10833 type = build_method_type_directly (ctype,
10834 TREE_TYPE (type),
10835 TYPE_ARG_TYPES (type));
8d08fdba
MS
10836 }
10837 }
10838 break;
10839
10840 case BIT_NOT_EXPR:
10841 declarator = TREE_OPERAND (declarator, 0);
10842 break;
10843
50ad9642
MM
10844 case BASELINK:
10845 declarator = BASELINK_FUNCTIONS (declarator);
10846 break;
10847
8d08fdba
MS
10848 case RECORD_TYPE:
10849 case UNION_TYPE:
10850 case ENUMERAL_TYPE:
10851 declarator = NULL_TREE;
10852 break;
10853
10854 case ERROR_MARK:
10855 declarator = NULL_TREE;
10856 break;
10857
10858 default:
a98facb0 10859 abort ();
8d08fdba
MS
10860 }
10861 }
10862
91d231cb 10863 if (returned_attrs)
1eab9b56 10864 {
91d231cb
JM
10865 if (attrlist)
10866 *attrlist = chainon (returned_attrs, *attrlist);
1eab9b56 10867 else
91d231cb 10868 attrlist = &returned_attrs;
1eab9b56
JM
10869 }
10870
419c6212
JM
10871 /* Now TYPE has the actual type. */
10872
55b3d665
JM
10873 /* Did array size calculations overflow? */
10874
10875 if (TREE_CODE (type) == ARRAY_TYPE
10876 && COMPLETE_TYPE_P (type)
10877 && TREE_OVERFLOW (TYPE_SIZE (type)))
10878 {
10879 error ("size of array `%s' is too large", name);
aba649ba 10880 /* If we proceed with the array type as it is, we'll eventually
55b3d665
JM
10881 crash in tree_low_cst(). */
10882 type = error_mark_node;
10883 }
10884
2fff6d71 10885 if ((decl_context == FIELD || decl_context == PARM)
dac45b5c
MM
10886 && !processing_template_decl
10887 && variably_modified_type_p (type))
10888 {
2fff6d71
MM
10889 if (decl_context == FIELD)
10890 error ("data member may not have variably modified type `%T'", type);
10891 else
10892 error ("parameter may not have variably modified type `%T'", type);
dac45b5c
MM
10893 type = error_mark_node;
10894 }
10895
34fc957d 10896 if (explicitp == 1 || (explicitp && friendp))
db5ae43f 10897 {
34fc957d
NS
10898 /* [dcl.fct.spec] The explicit specifier shall only be used in
10899 declarations of constructors within a class definition. */
10900 error ("only declarations of constructors can be `explicit'");
db5ae43f
MS
10901 explicitp = 0;
10902 }
10903
f30432d7
MS
10904 if (RIDBIT_SETP (RID_MUTABLE, specbits))
10905 {
4223f82f 10906 if (decl_context != FIELD || friendp)
34fc957d
NS
10907 {
10908 error ("non-member `%s' cannot be declared `mutable'", name);
10909 RIDBIT_RESET (RID_MUTABLE, specbits);
10910 }
10911 else if (decl_context == TYPENAME || RIDBIT_SETP (RID_TYPEDEF, specbits))
f30432d7 10912 {
34fc957d 10913 error ("non-object member `%s' cannot be declared `mutable'", name);
f30432d7
MS
10914 RIDBIT_RESET (RID_MUTABLE, specbits);
10915 }
34fc957d
NS
10916 else if (TREE_CODE (type) == FUNCTION_TYPE
10917 || TREE_CODE (type) == METHOD_TYPE)
10918 {
10919 error ("function `%s' cannot be declared `mutable'", name);
10920 RIDBIT_RESET (RID_MUTABLE, specbits);
10921 }
f30432d7
MS
10922 else if (staticp)
10923 {
8251199e 10924 error ("static `%s' cannot be declared `mutable'", name);
f30432d7
MS
10925 RIDBIT_RESET (RID_MUTABLE, specbits);
10926 }
34fc957d
NS
10927 else if (type_quals & TYPE_QUAL_CONST)
10928 {
10929 error ("const `%s' cannot be declared `mutable'", name);
4f2b0fb2 10930 RIDBIT_RESET (RID_MUTABLE, specbits);
34fc957d 10931 }
f30432d7
MS
10932 }
10933
efe38fab
JM
10934 if (declarator == NULL_TREE
10935 || TREE_CODE (declarator) == IDENTIFIER_NODE
10936 || (TREE_CODE (declarator) == TEMPLATE_ID_EXPR
10937 && (TREE_CODE (type) == FUNCTION_TYPE
10938 || TREE_CODE (type) == METHOD_TYPE)))
10939 /* OK */;
10940 else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
10941 {
33bd39a2 10942 error ("template-id `%D' used as a declarator", declarator);
efe38fab
JM
10943 declarator = dname;
10944 }
10945 else
419c6212 10946 /* Unexpected declarator format. */
a98facb0 10947 abort ();
efe38fab 10948
419c6212
JM
10949 /* If this is declaring a typedef name, return a TYPE_DECL. */
10950
fc378698 10951 if (RIDBIT_SETP (RID_TYPEDEF, specbits) && decl_context != TYPENAME)
8d08fdba
MS
10952 {
10953 tree decl;
10954
10955 /* Note that the grammar rejects storage classes
10956 in typenames, fields or parameters. */
eff71ab0
PB
10957 if (current_lang_name == lang_name_java)
10958 TYPE_FOR_JAVA (type) = 1;
8d08fdba 10959
d2e5ee5c
MS
10960 if (decl_context == FIELD)
10961 {
a723baf1 10962 if (constructor_name_p (declarator, current_class_type))
33bd39a2 10963 pedwarn ("ISO C++ forbids nested type `%D' with same name as enclosing class",
d2e5ee5c
MS
10964 declarator);
10965 decl = build_lang_decl (TYPE_DECL, declarator, type);
d2e5ee5c
MS
10966 }
10967 else
6bda7a5e
NS
10968 {
10969 decl = build_decl (TYPE_DECL, declarator, type);
a723baf1 10970 if (in_namespace || ctype)
ddd2d57e 10971 error ("%Jtypedef name may not be a nested-name-specifier", decl);
6bda7a5e
NS
10972 if (!current_function_decl)
10973 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
10974 }
10975
9188c363
MM
10976 /* If the user declares "typedef struct {...} foo" then the
10977 struct will have an anonymous name. Fill that name in now.
10978 Nothing can refer to it, so nothing needs know about the name
10979 change. */
8d08fdba 10980 if (type != error_mark_node
2bdb0643 10981 && declarator
8d08fdba
MS
10982 && TYPE_NAME (type)
10983 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
1951a1b6 10984 && TYPE_ANONYMOUS_P (type)
ac905924
JM
10985 /* Don't do this if there are attributes. */
10986 && (!attrlist || !*attrlist)
89d684bb 10987 && cp_type_quals (type) == TYPE_UNQUALIFIED)
8d08fdba 10988 {
dcd08efc
JM
10989 tree oldname = TYPE_NAME (type);
10990 tree t;
10991
2c73f9f5 10992 /* Replace the anonymous name with the real name everywhere. */
8d08fdba 10993 lookup_tag_reverse (type, declarator);
dcd08efc
JM
10994 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
10995 if (TYPE_NAME (t) == oldname)
10996 TYPE_NAME (t) = decl;
8d08fdba
MS
10997
10998 if (TYPE_LANG_SPECIFIC (type))
10999 TYPE_WAS_ANONYMOUS (type) = 1;
11000
33964bf4
MM
11001 /* If this is a typedef within a template class, the nested
11002 type is a (non-primary) template. The name for the
11003 template needs updating as well. */
11004 if (TYPE_LANG_SPECIFIC (type) && CLASSTYPE_TEMPLATE_INFO (type))
68642fb6 11005 DECL_NAME (CLASSTYPE_TI_TEMPLATE (type))
33964bf4
MM
11006 = TYPE_IDENTIFIER (type);
11007
50a6dbd7
JM
11008 /* FIXME remangle member functions; member functions of a
11009 type with external linkage have external linkage. */
fc378698 11010 }
fc378698 11011
a723baf1 11012 if (quals)
8d08fdba
MS
11013 {
11014 if (ctype == NULL_TREE)
11015 {
11016 if (TREE_CODE (type) != METHOD_TYPE)
ddd2d57e
RH
11017 error ("%Jinvalid type qualifier for non-member function type",
11018 decl);
8d08fdba
MS
11019 else
11020 ctype = TYPE_METHOD_BASETYPE (type);
11021 }
11022 if (ctype != NULL_TREE)
11023 grok_method_quals (ctype, decl, quals);
11024 }
11025
11026 if (RIDBIT_SETP (RID_SIGNED, specbits)
11027 || (typedef_decl && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl)))
11028 C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
11029
c91a56d2
MS
11030 bad_specifiers (decl, "type", virtualp, quals != NULL_TREE,
11031 inlinep, friendp, raises != NULL_TREE);
11032
8d08fdba
MS
11033 return decl;
11034 }
11035
11036 /* Detect the case of an array type of unspecified size
11037 which came, as such, direct from a typedef name.
8d6e459d
NS
11038 We must copy the type, so that the array's domain can be
11039 individually set by the object's initializer. */
8d08fdba 11040
8d6e459d
NS
11041 if (type && typedef_type
11042 && TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type)
55b3d665 11043 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (typedef_type))
8d6e459d 11044 type = build_cplus_array_type (TREE_TYPE (type), NULL_TREE);
8d08fdba 11045
969fd501
NS
11046 /* Detect where we're using a typedef of function type to declare a
11047 function. last_function_parms will not be set, so we must create
11048 it now. */
11049
11050 if (type == typedef_type && TREE_CODE (type) == FUNCTION_TYPE)
11051 {
11052 tree decls = NULL_TREE;
11053 tree args;
11054
11055 for (args = TYPE_ARG_TYPES (type); args; args = TREE_CHAIN (args))
11056 {
8e51619a 11057 tree decl = cp_build_parm_decl (NULL_TREE, TREE_VALUE (args));
969fd501
NS
11058
11059 TREE_CHAIN (decl) = decls;
11060 decls = decl;
11061 }
11062
11063 last_function_parms = nreverse (decls);
11064 }
11065
8d08fdba
MS
11066 /* If this is a type name (such as, in a cast or sizeof),
11067 compute the type and return it now. */
11068
11069 if (decl_context == TYPENAME)
11070 {
11071 /* Note that the grammar rejects storage classes
11072 in typenames, fields or parameters. */
91063b51 11073 if (type_quals != TYPE_UNQUALIFIED)
6eabb241 11074 type_quals = TYPE_UNQUALIFIED;
8d08fdba
MS
11075
11076 /* Special case: "friend class foo" looks like a TYPENAME context. */
11077 if (friendp)
11078 {
91063b51 11079 if (type_quals != TYPE_UNQUALIFIED)
b7484fbe 11080 {
33bd39a2 11081 error ("type qualifiers specified for friend class declaration");
91063b51 11082 type_quals = TYPE_UNQUALIFIED;
b7484fbe
MS
11083 }
11084 if (inlinep)
11085 {
33bd39a2 11086 error ("`inline' specified for friend class declaration");
b7484fbe
MS
11087 inlinep = 0;
11088 }
f2ae0c45 11089
218e0eb6 11090 if (!current_aggr)
4b054b80 11091 {
218e0eb6 11092 /* Don't allow friend declaration without a class-key. */
4b054b80 11093 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
218e0eb6 11094 pedwarn ("template parameters cannot be friends");
f2ae0c45 11095 else if (TREE_CODE (type) == TYPENAME_TYPE)
218e0eb6
KL
11096 pedwarn ("friend declaration requires class-key, "
11097 "i.e. `friend class %T::%D'",
11098 TYPE_CONTEXT (type), TYPENAME_TYPE_FULLNAME (type));
4b054b80 11099 else
218e0eb6
KL
11100 pedwarn ("friend declaration requires class-key, "
11101 "i.e. `friend %#T'",
11102 type);
4b054b80 11103 }
b7484fbe
MS
11104
11105 /* Only try to do this stuff if we didn't already give up. */
11106 if (type != integer_type_node)
11107 {
11108 /* A friendly class? */
11109 if (current_class_type)
19db77ce
KL
11110 make_friend_class (current_class_type, TYPE_MAIN_VARIANT (type),
11111 /*complain=*/true);
b7484fbe 11112 else
33bd39a2 11113 error ("trying to make class `%T' a friend of global scope",
2ae7bada 11114 type);
d363e7bf 11115
b7484fbe
MS
11116 type = void_type_node;
11117 }
8d08fdba
MS
11118 }
11119 else if (quals)
11120 {
8d08fdba
MS
11121 if (ctype == NULL_TREE)
11122 {
41cbc04c 11123 if (TREE_CODE (type) != METHOD_TYPE)
33bd39a2 11124 error ("invalid qualifiers on non-member function type");
41cbc04c
NS
11125 else
11126 ctype = TYPE_METHOD_BASETYPE (type);
11127 }
11128 if (ctype)
11129 {
11130 tree dummy = build_decl (TYPE_DECL, declarator, type);
11131 grok_method_quals (ctype, dummy, quals);
11132 type = TREE_TYPE (dummy);
8d08fdba 11133 }
8d08fdba
MS
11134 }
11135
11136 return type;
11137 }
11138 else if (declarator == NULL_TREE && decl_context != PARM
db5ae43f 11139 && decl_context != CATCHPARM
8d08fdba
MS
11140 && TREE_CODE (type) != UNION_TYPE
11141 && ! bitfield)
11142 {
33bd39a2 11143 error ("abstract declarator `%T' used as declaration", type);
8d08fdba
MS
11144 declarator = make_anon_name ();
11145 }
11146
11147 /* `void' at top level (not within pointer)
11148 is allowed only in typedefs or type names.
11149 We don't complain about parms either, but that is because
11150 a better error message can be made later. */
11151
a1774733 11152 if (TREE_CODE (type) == VOID_TYPE && decl_context != PARM)
8d08fdba 11153 {
b7484fbe 11154 if (! declarator)
8251199e 11155 error ("unnamed variable or field declared void");
b7484fbe 11156 else if (TREE_CODE (declarator) == IDENTIFIER_NODE)
8d08fdba
MS
11157 {
11158 if (IDENTIFIER_OPNAME_P (declarator))
a98facb0 11159 abort ();
8d08fdba 11160 else
8251199e 11161 error ("variable or field `%s' declared void", name);
8d08fdba
MS
11162 }
11163 else
8251199e 11164 error ("variable or field declared void");
8d08fdba
MS
11165 type = integer_type_node;
11166 }
11167
11168 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
11169 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
11170
14ae7e7d
JM
11171 if (decl_context == PARM || decl_context == CATCHPARM)
11172 {
11173 if (ctype || in_namespace)
11174 error ("cannot use `::' in parameter declaration");
11175
11176 /* A parameter declared as an array of T is really a pointer to T.
11177 One declared as a function is really a pointer to a function.
11178 One declared as a member is really a pointer to member. */
11179
11180 if (TREE_CODE (type) == ARRAY_TYPE)
11181 {
11182 /* Transfer const-ness of array into that of type pointed to. */
11183 type = build_pointer_type (TREE_TYPE (type));
91063b51 11184 type_quals = TYPE_UNQUALIFIED;
14ae7e7d
JM
11185 }
11186 else if (TREE_CODE (type) == FUNCTION_TYPE)
11187 type = build_pointer_type (type);
14ae7e7d 11188 }
68642fb6 11189
8d08fdba
MS
11190 {
11191 register tree decl;
11192
11193 if (decl_context == PARM)
11194 {
8e51619a 11195 decl = cp_build_parm_decl (declarator, type);
8d08fdba
MS
11196
11197 bad_specifiers (decl, "parameter", virtualp, quals != NULL_TREE,
11198 inlinep, friendp, raises != NULL_TREE);
8d08fdba
MS
11199 }
11200 else if (decl_context == FIELD)
11201 {
01bf0f3e
JM
11202 /* The C99 flexible array extension. */
11203 if (!staticp && TREE_CODE (type) == ARRAY_TYPE
11204 && TYPE_DOMAIN (type) == NULL_TREE)
11205 {
11206 tree itype = compute_array_index_type (dname, integer_zero_node);
11207 type = build_cplus_array_type (TREE_TYPE (type), itype);
11208 }
11209
8d08fdba
MS
11210 if (type == error_mark_node)
11211 {
11212 /* Happens when declaring arrays of sizes which
11213 are error_mark_node, for example. */
11214 decl = NULL_TREE;
11215 }
997a088c 11216 else if (in_namespace && !friendp)
05008fb9
MM
11217 {
11218 /* Something like struct S { int N::j; }; */
33bd39a2 11219 error ("invalid use of `::'");
05008fb9
MM
11220 decl = NULL_TREE;
11221 }
8d08fdba
MS
11222 else if (TREE_CODE (type) == FUNCTION_TYPE)
11223 {
11224 int publicp = 0;
e1467ff2 11225 tree function_context;
8d08fdba 11226
72b7eeff
MS
11227 /* We catch the others as conflicts with the builtin
11228 typedefs. */
11229 if (friendp && declarator == ridpointers[(int) RID_SIGNED])
11230 {
33bd39a2 11231 error ("function `%D' cannot be declared friend",
72b7eeff
MS
11232 declarator);
11233 friendp = 0;
11234 }
11235
8d08fdba
MS
11236 if (friendp == 0)
11237 {
11238 if (ctype == NULL_TREE)
11239 ctype = current_class_type;
11240
11241 if (ctype == NULL_TREE)
11242 {
33bd39a2 11243 error ("can't make `%D' into a method -- not in a class",
8d08fdba
MS
11244 declarator);
11245 return void_type_node;
11246 }
11247
11248 /* ``A union may [ ... ] not [ have ] virtual functions.''
11249 ARM 9.5 */
11250 if (virtualp && TREE_CODE (ctype) == UNION_TYPE)
11251 {
33bd39a2 11252 error ("function `%D' declared virtual inside a union",
8d08fdba
MS
11253 declarator);
11254 return void_type_node;
11255 }
11256
596ea4e5
AS
11257 if (declarator == ansi_opname (NEW_EXPR)
11258 || declarator == ansi_opname (VEC_NEW_EXPR)
11259 || declarator == ansi_opname (DELETE_EXPR)
11260 || declarator == ansi_opname (VEC_DELETE_EXPR))
8d08fdba
MS
11261 {
11262 if (virtualp)
11263 {
33bd39a2 11264 error ("`%D' cannot be declared virtual, since it is always static",
8d08fdba
MS
11265 declarator);
11266 virtualp = 0;
11267 }
11268 }
11269 else if (staticp < 2)
43dc123f
MM
11270 type = build_method_type_directly (ctype,
11271 TREE_TYPE (type),
11272 TYPE_ARG_TYPES (type));
8d08fdba
MS
11273 }
11274
11275 /* Tell grokfndecl if it needs to set TREE_PUBLIC on the node. */
68642fb6 11276 function_context = (ctype != NULL_TREE) ?
4f1c5b7d 11277 decl_function_context (TYPE_MAIN_DECL (ctype)) : NULL_TREE;
e1467ff2
MM
11278 publicp = (! friendp || ! staticp)
11279 && function_context == NULL_TREE;
68642fb6 11280 decl = grokfndecl (ctype, type,
386b8a85
JM
11281 TREE_CODE (declarator) != TEMPLATE_ID_EXPR
11282 ? declarator : dname,
11283 declarator,
7a8f9fa9 11284 virtualp, flags, quals, raises,
386b8a85 11285 friendp ? -1 : 0, friendp, publicp, inlinep,
2c73f9f5 11286 funcdef_flag, template_count, in_namespace);
20496fa2 11287 if (decl == NULL_TREE)
3ddfb0e6 11288 return decl;
6125f3be
DE
11289#if 0
11290 /* This clobbers the attrs stored in `decl' from `attrlist'. */
91d231cb
JM
11291 /* The decl and setting of decl_attr is also turned off. */
11292 decl = build_decl_attribute_variant (decl, decl_attr);
6125f3be 11293#endif
f0e01782 11294
cc804e51
MM
11295 /* [class.conv.ctor]
11296
11297 A constructor declared without the function-specifier
11298 explicit that can be called with a single parameter
11299 specifies a conversion from the type of its first
11300 parameter to the type of its class. Such a constructor
11301 is called a converting constructor. */
db5ae43f
MS
11302 if (explicitp == 2)
11303 DECL_NONCONVERTING_P (decl) = 1;
cc804e51
MM
11304 else if (DECL_CONSTRUCTOR_P (decl))
11305 {
11306 /* The constructor can be called with exactly one
11307 parameter if there is at least one parameter, and
11308 any subsequent parameters have default arguments.
e0fff4b3
JM
11309 Ignore any compiler-added parms. */
11310 tree arg_types = FUNCTION_FIRST_USER_PARMTYPE (decl);
cc804e51
MM
11311
11312 if (arg_types == void_list_node
68642fb6
UD
11313 || (arg_types
11314 && TREE_CHAIN (arg_types)
cc804e51
MM
11315 && TREE_CHAIN (arg_types) != void_list_node
11316 && !TREE_PURPOSE (TREE_CHAIN (arg_types))))
11317 DECL_NONCONVERTING_P (decl) = 1;
11318 }
8d08fdba
MS
11319 }
11320 else if (TREE_CODE (type) == METHOD_TYPE)
11321 {
faae18ab
MS
11322 /* We only get here for friend declarations of
11323 members of other classes. */
8d08fdba
MS
11324 /* All method decls are public, so tell grokfndecl to set
11325 TREE_PUBLIC, also. */
866eb556
KL
11326 decl = grokfndecl (ctype, type,
11327 TREE_CODE (declarator) != TEMPLATE_ID_EXPR
11328 ? declarator : dname,
11329 declarator,
7a8f9fa9 11330 virtualp, flags, quals, raises,
386b8a85 11331 friendp ? -1 : 0, friendp, 1, 0, funcdef_flag,
2c73f9f5 11332 template_count, in_namespace);
f0e01782
MS
11333 if (decl == NULL_TREE)
11334 return NULL_TREE;
8d08fdba 11335 }
5566b478 11336 else if (!staticp && ! processing_template_decl
d0f062fb 11337 && !COMPLETE_TYPE_P (complete_type (type))
8d08fdba
MS
11338 && (TREE_CODE (type) != ARRAY_TYPE || initialized == 0))
11339 {
b7484fbe 11340 if (declarator)
33bd39a2 11341 error ("field `%D' has incomplete type", declarator);
b7484fbe 11342 else
33bd39a2 11343 error ("name `%T' has incomplete type", type);
8d08fdba
MS
11344
11345 /* If we're instantiating a template, tell them which
11346 instantiation made the field's type be incomplete. */
11347 if (current_class_type
11348 && TYPE_NAME (current_class_type)
d2e5ee5c 11349 && IDENTIFIER_TEMPLATE (TYPE_IDENTIFIER (current_class_type))
8d08fdba
MS
11350 && declspecs && TREE_VALUE (declspecs)
11351 && TREE_TYPE (TREE_VALUE (declspecs)) == type)
33bd39a2 11352 error (" in instantiation of template `%T'",
db5ae43f
MS
11353 current_class_type);
11354
8d08fdba
MS
11355 type = error_mark_node;
11356 decl = NULL_TREE;
11357 }
11358 else
11359 {
11360 if (friendp)
11361 {
cb9a3ff8 11362 error ("`%s' is neither function nor member function; cannot be declared friend",
8d08fdba
MS
11363 IDENTIFIER_POINTER (declarator));
11364 friendp = 0;
11365 }
11366 decl = NULL_TREE;
11367 }
11368
11369 if (friendp)
11370 {
11371 /* Friends are treated specially. */
11372 if (ctype == current_class_type)
8251199e 11373 warning ("member functions are implicitly friends of their class");
8db1028e
NS
11374 else if (decl && DECL_NAME (decl))
11375 {
11376 if (template_class_depth (current_class_type) == 0)
11377 {
11378 decl = check_explicit_specialization
11379 (declarator, decl, template_count,
11380 2 * (funcdef_flag != 0) + 4);
11381 if (decl == error_mark_node)
11382 return error_mark_node;
11383 }
11384
11385 decl = do_friend (ctype, declarator, decl,
11386 last_function_parms, *attrlist,
11387 flags, quals, funcdef_flag);
11388 return decl;
11389 }
11390 else
11391 return void_type_node;
8d08fdba
MS
11392 }
11393
11394 /* Structure field. It may not be a function, except for C++ */
11395
11396 if (decl == NULL_TREE)
11397 {
8d08fdba
MS
11398 if (initialized)
11399 {
3ac3d9ea
MM
11400 if (!staticp)
11401 {
11402 /* An attempt is being made to initialize a non-static
11403 member. But, from [class.mem]:
68642fb6 11404
3ac3d9ea
MM
11405 4 A member-declarator can contain a
11406 constant-initializer only if it declares a static
11407 member (_class.static_) of integral or enumeration
68642fb6 11408 type, see _class.static.data_.
3ac3d9ea
MM
11409
11410 This used to be relatively common practice, but
11411 the rest of the compiler does not correctly
11412 handle the initialization unless the member is
11413 static so we make it static below. */
33bd39a2 11414 pedwarn ("ISO C++ forbids initialization of member `%D'",
3ac3d9ea 11415 declarator);
33bd39a2 11416 pedwarn ("making `%D' static", declarator);
3ac3d9ea
MM
11417 staticp = 1;
11418 }
11419
6ba89f8e
MM
11420 if (uses_template_parms (type))
11421 /* We'll check at instantiation time. */
11422 ;
11423 else if (check_static_variable_definition (declarator,
11424 type))
11425 /* If we just return the declaration, crashes
11426 will sometimes occur. We therefore return
72a93143
JM
11427 void_type_node, as if this was a friend
11428 declaration, to cause callers to completely
11429 ignore this declaration. */
6ba89f8e 11430 return void_type_node;
8d08fdba
MS
11431 }
11432
3ac3d9ea 11433 if (staticp)
8d08fdba 11434 {
c717c5af
MM
11435 /* [class.mem] forbids static data members with the
11436 same name as the enclosing class. Non-static data
11437 members are checked in check_field_decls. */
11438 if (constructor_name_p (declarator, current_class_type))
11439 pedwarn ("ISO C++ forbids static data member `%D' with same name as enclosing class",
11440 declarator);
11441
f18a14bc
MM
11442 /* C++ allows static class members. All other work
11443 for this is done by grokfield. */
4ce3d537 11444 decl = build_lang_decl (VAR_DECL, declarator, type);
5b605f68
MS
11445 TREE_STATIC (decl) = 1;
11446 /* In class context, 'static' means public access. */
3ac3d9ea 11447 TREE_PUBLIC (decl) = DECL_EXTERNAL (decl) = 1;
8d08fdba
MS
11448 }
11449 else
11450 {
721c3b42 11451 decl = build_decl (FIELD_DECL, declarator, type);
2bf105ab 11452 DECL_NONADDRESSABLE_P (decl) = bitfield;
8d08fdba
MS
11453 if (RIDBIT_SETP (RID_MUTABLE, specbits))
11454 {
11455 DECL_MUTABLE_P (decl) = 1;
11456 RIDBIT_RESET (RID_MUTABLE, specbits);
11457 }
11458 }
11459
11460 bad_specifiers (decl, "field", virtualp, quals != NULL_TREE,
11461 inlinep, friendp, raises != NULL_TREE);
11462 }
11463 }
fd9aef9d
NS
11464 else if (TREE_CODE (type) == FUNCTION_TYPE
11465 || TREE_CODE (type) == METHOD_TYPE)
8d08fdba 11466 {
386b8a85 11467 tree original_name;
8d08fdba
MS
11468 int publicp = 0;
11469
11470 if (! declarator)
11471 return NULL_TREE;
11472
386b8a85
JM
11473 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
11474 original_name = dname;
11475 else
11476 original_name = declarator;
11477
8926095f 11478 if (RIDBIT_SETP (RID_AUTO, specbits))
8251199e 11479 error ("storage class `auto' invalid for function `%s'", name);
8926095f 11480 else if (RIDBIT_SETP (RID_REGISTER, specbits))
8251199e 11481 error ("storage class `register' invalid for function `%s'", name);
7a1f3f5f
RH
11482 else if (RIDBIT_SETP (RID_THREAD, specbits))
11483 error ("storage class `__thread' invalid for function `%s'", name);
8d08fdba
MS
11484
11485 /* Function declaration not at top level.
11486 Storage classes other than `extern' are not allowed
11487 and `extern' makes no difference. */
a9aedbc2 11488 if (! toplevel_bindings_p ()
8926095f
MS
11489 && (RIDBIT_SETP (RID_STATIC, specbits)
11490 || RIDBIT_SETP (RID_INLINE, specbits))
8d08fdba 11491 && pedantic)
8926095f
MS
11492 {
11493 if (RIDBIT_SETP (RID_STATIC, specbits))
8251199e 11494 pedwarn ("storage class `static' invalid for function `%s' declared out of global scope", name);
8926095f 11495 else
8251199e 11496 pedwarn ("storage class `inline' invalid for function `%s' declared out of global scope", name);
8926095f 11497 }
68642fb6 11498
8d08fdba
MS
11499 if (ctype == NULL_TREE)
11500 {
11501 if (virtualp)
11502 {
8251199e 11503 error ("virtual non-class function `%s'", name);
8d08fdba
MS
11504 virtualp = 0;
11505 }
8d08fdba
MS
11506 }
11507 else if (TREE_CODE (type) == FUNCTION_TYPE && staticp < 2)
43dc123f
MM
11508 type = build_method_type_directly (ctype,
11509 TREE_TYPE (type),
11510 TYPE_ARG_TYPES (type));
8d08fdba 11511
eb66be0e 11512 /* Record presence of `static'. */
faae18ab 11513 publicp = (ctype != NULL_TREE
a9aedbc2 11514 || RIDBIT_SETP (RID_EXTERN, specbits)
eb66be0e 11515 || !RIDBIT_SETP (RID_STATIC, specbits));
8d08fdba 11516
386b8a85 11517 decl = grokfndecl (ctype, type, original_name, declarator,
7a8f9fa9 11518 virtualp, flags, quals, raises,
75650646 11519 1, friendp,
68642fb6 11520 publicp, inlinep, funcdef_flag,
2c73f9f5 11521 template_count, in_namespace);
f0e01782
MS
11522 if (decl == NULL_TREE)
11523 return NULL_TREE;
8d08fdba 11524
8d08fdba
MS
11525 if (staticp == 1)
11526 {
0e339752 11527 int invalid_static = 0;
8d08fdba
MS
11528
11529 /* Don't allow a static member function in a class, and forbid
11530 declaring main to be static. */
11531 if (TREE_CODE (type) == METHOD_TYPE)
11532 {
33bd39a2 11533 pedwarn ("cannot declare member function `%D' to have static linkage", decl);
0e339752 11534 invalid_static = 1;
8d08fdba 11535 }
8d08fdba
MS
11536 else if (current_function_decl)
11537 {
11538 /* FIXME need arm citation */
8251199e 11539 error ("cannot declare static function inside another function");
0e339752 11540 invalid_static = 1;
8d08fdba
MS
11541 }
11542
0e339752 11543 if (invalid_static)
8d08fdba
MS
11544 {
11545 staticp = 0;
11546 RIDBIT_RESET (RID_STATIC, specbits);
11547 }
11548 }
8d08fdba
MS
11549 }
11550 else
11551 {
11552 /* It's a variable. */
11553
11554 /* An uninitialized decl with `extern' is a reference. */
68642fb6
UD
11555 decl = grokvardecl (type, declarator, &specbits,
11556 initialized,
11557 (type_quals & TYPE_QUAL_CONST) != 0,
9e259dd1 11558 ctype ? ctype : in_namespace);
8d08fdba
MS
11559 bad_specifiers (decl, "variable", virtualp, quals != NULL_TREE,
11560 inlinep, friendp, raises != NULL_TREE);
11561
11562 if (ctype)
11563 {
f0e01782 11564 DECL_CONTEXT (decl) = ctype;
8d08fdba
MS
11565 if (staticp == 1)
11566 {
33bd39a2 11567 pedwarn ("`static' may not be used when defining (as opposed to declaring) a static data member");
8d08fdba
MS
11568 staticp = 0;
11569 RIDBIT_RESET (RID_STATIC, specbits);
11570 }
b7484fbe
MS
11571 if (RIDBIT_SETP (RID_REGISTER, specbits) && TREE_STATIC (decl))
11572 {
33bd39a2 11573 error ("static member `%D' declared `register'", decl);
b7484fbe
MS
11574 RIDBIT_RESET (RID_REGISTER, specbits);
11575 }
f30432d7 11576 if (RIDBIT_SETP (RID_EXTERN, specbits) && pedantic)
8d08fdba 11577 {
33bd39a2 11578 pedwarn ("cannot explicitly declare member `%#D' to have extern linkage",
f30432d7 11579 decl);
8d08fdba
MS
11580 RIDBIT_RESET (RID_EXTERN, specbits);
11581 }
11582 }
11583 }
11584
34fc957d 11585 my_friendly_assert (!RIDBIT_SETP (RID_MUTABLE, specbits), 19990927);
8d08fdba
MS
11586
11587 /* Record `register' declaration for warnings on &
11588 and in case doing stupid register allocation. */
11589
11590 if (RIDBIT_SETP (RID_REGISTER, specbits))
11591 DECL_REGISTER (decl) = 1;
11592
8926095f
MS
11593 if (RIDBIT_SETP (RID_EXTERN, specbits))
11594 DECL_THIS_EXTERN (decl) = 1;
11595
faae18ab
MS
11596 if (RIDBIT_SETP (RID_STATIC, specbits))
11597 DECL_THIS_STATIC (decl) = 1;
11598
adecb3f4
MM
11599 /* Record constancy and volatility. There's no need to do this
11600 when processing a template; we'll do this for the instantiated
11601 declaration based on the type of DECL. */
11602 if (!processing_template_decl)
11603 c_apply_type_quals_to_decl (type_quals, decl);
8d08fdba
MS
11604
11605 return decl;
11606 }
11607}
11608\f
f181d4ae
MM
11609/* Subroutine of start_function. Ensure that each of the parameter
11610 types (as listed in PARMS) is complete, as is required for a
11611 function definition. */
e92cc029 11612
8d08fdba 11613static void
11f6b451 11614require_complete_types_for_parms (tree parms)
8d08fdba 11615{
07c88314 11616 for (; parms; parms = TREE_CHAIN (parms))
8d08fdba 11617 {
5cce22b6
NS
11618 if (VOID_TYPE_P (TREE_TYPE (parms)))
11619 /* grokparms will have already issued an error */
11620 TREE_TYPE (parms) = error_mark_node;
11621 else if (complete_type_or_else (TREE_TYPE (parms), parms))
753225c1
JM
11622 {
11623 layout_decl (parms, 0);
11624 DECL_ARG_TYPE (parms) = type_passed_as (TREE_TYPE (parms));
11625 }
5cce22b6
NS
11626 else
11627 TREE_TYPE (parms) = error_mark_node;
8d08fdba
MS
11628 }
11629}
11630
838dfd8a 11631/* Returns nonzero if T is a local variable. */
297e73d8 11632
46e8c075 11633int
11f6b451 11634local_variable_p (tree t)
297e73d8 11635{
68642fb6 11636 if ((TREE_CODE (t) == VAR_DECL
297e73d8
MM
11637 /* A VAR_DECL with a context that is a _TYPE is a static data
11638 member. */
11639 && !TYPE_P (CP_DECL_CONTEXT (t))
11640 /* Any other non-local variable must be at namespace scope. */
46e8c075 11641 && !DECL_NAMESPACE_SCOPE_P (t))
297e73d8 11642 || (TREE_CODE (t) == PARM_DECL))
46e8c075 11643 return 1;
297e73d8 11644
46e8c075
MM
11645 return 0;
11646}
11647
838dfd8a 11648/* Returns nonzero if T is an automatic local variable or a label.
46e8c075
MM
11649 (These are the declarations that need to be remapped when the code
11650 containing them is duplicated.) */
11651
11652int
11f6b451 11653nonstatic_local_decl_p (tree t)
46e8c075
MM
11654{
11655 return ((local_variable_p (t) && !TREE_STATIC (t))
11656 || TREE_CODE (t) == LABEL_DECL
11657 || TREE_CODE (t) == RESULT_DECL);
11658}
11659
11660/* Like local_variable_p, but suitable for use as a tree-walking
11661 function. */
11662
11663static tree
11f6b451
NN
11664local_variable_p_walkfn (tree* tp,
11665 int* walk_subtrees ATTRIBUTE_UNUSED ,
11666 void* data ATTRIBUTE_UNUSED )
46e8c075 11667{
68642fb6 11668 return ((local_variable_p (*tp) && !DECL_ARTIFICIAL (*tp))
46e8c075 11669 ? *tp : NULL_TREE);
297e73d8
MM
11670}
11671
11672/* Check that ARG, which is a default-argument expression for a
0e339752 11673 parameter DECL, is valid. Returns ARG, or ERROR_MARK_NODE, if
297e73d8
MM
11674 something goes wrong. DECL may also be a _TYPE node, rather than a
11675 DECL, if there is no DECL available. */
11676
11677tree
11f6b451 11678check_default_argument (tree decl, tree arg)
297e73d8
MM
11679{
11680 tree var;
11681 tree decl_type;
11682
11683 if (TREE_CODE (arg) == DEFAULT_ARG)
11684 /* We get a DEFAULT_ARG when looking at an in-class declaration
11685 with a default argument. Ignore the argument for now; we'll
11686 deal with it after the class is complete. */
11687 return arg;
11688
11689 if (processing_template_decl || uses_template_parms (arg))
11690 /* We don't do anything checking until instantiation-time. Note
11691 that there may be uninstantiated arguments even for an
11692 instantiated function, since default arguments are not
11693 instantiated until they are needed. */
11694 return arg;
11695
11696 if (TYPE_P (decl))
11697 {
11698 decl_type = decl;
11699 decl = NULL_TREE;
11700 }
11701 else
11702 decl_type = TREE_TYPE (decl);
11703
68642fb6 11704 if (arg == error_mark_node
297e73d8
MM
11705 || decl == error_mark_node
11706 || TREE_TYPE (arg) == error_mark_node
11707 || decl_type == error_mark_node)
11708 /* Something already went wrong. There's no need to check
11709 further. */
11710 return error_mark_node;
11711
11712 /* [dcl.fct.default]
68642fb6 11713
297e73d8
MM
11714 A default argument expression is implicitly converted to the
11715 parameter type. */
11716 if (!TREE_TYPE (arg)
11717 || !can_convert_arg (decl_type, TREE_TYPE (arg), arg))
11718 {
11719 if (decl)
33bd39a2 11720 error ("default argument for `%#D' has type `%T'",
297e73d8
MM
11721 decl, TREE_TYPE (arg));
11722 else
33bd39a2 11723 error ("default argument for parameter of type `%T' has type `%T'",
297e73d8
MM
11724 decl_type, TREE_TYPE (arg));
11725
11726 return error_mark_node;
11727 }
11728
11729 /* [dcl.fct.default]
11730
11731 Local variables shall not be used in default argument
68642fb6 11732 expressions.
297e73d8
MM
11733
11734 The keyword `this' shall not be used in a default argument of a
11735 member function. */
5362b086 11736 var = walk_tree_without_duplicates (&arg, local_variable_p_walkfn,
ee94fce6 11737 NULL);
297e73d8
MM
11738 if (var)
11739 {
33bd39a2 11740 error ("default argument `%E' uses local variable `%D'",
297e73d8
MM
11741 arg, var);
11742 return error_mark_node;
11743 }
11744
11745 /* All is well. */
11746 return arg;
11747}
11748
8d08fdba
MS
11749/* Decode the list of parameter types for a function type.
11750 Given the list of things declared inside the parens,
11751 return a list of types.
11752
5cce22b6
NS
11753 We determine whether ellipsis parms are used by PARMLIST_ELLIPSIS_P
11754 flag. If unset, we append void_list_node. A parmlist declared
11755 as `(void)' is accepted as the empty parmlist.
11756
11757 Also set last_function_parms to the chain of PARM_DECLs. */
8d08fdba
MS
11758
11759static tree
11f6b451 11760grokparms (tree first_parm)
8d08fdba
MS
11761{
11762 tree result = NULL_TREE;
11763 tree decls = NULL_TREE;
5cce22b6
NS
11764 int ellipsis = !first_parm || PARMLIST_ELLIPSIS_P (first_parm);
11765 tree parm, chain;
11766 int any_error = 0;
8d08fdba 11767
5cce22b6
NS
11768 my_friendly_assert (!first_parm || TREE_PARMLIST (first_parm), 20001115);
11769
11770 for (parm = first_parm; parm != NULL_TREE; parm = chain)
8d08fdba 11771 {
0657c69c 11772 tree type = NULL_TREE;
98884b26 11773 tree decl = TREE_VALUE (parm);
5cce22b6 11774 tree init = TREE_PURPOSE (parm);
98884b26 11775 tree specs, attrs;
8d08fdba 11776
5cce22b6
NS
11777 chain = TREE_CHAIN (parm);
11778 /* @@ weak defense against parse errors. */
11779 if (TREE_CODE (decl) != VOID_TYPE
11780 && TREE_CODE (decl) != TREE_LIST)
8d08fdba 11781 {
5cce22b6
NS
11782 /* Give various messages as the need arises. */
11783 if (TREE_CODE (decl) == STRING_CST)
33bd39a2 11784 error ("invalid string constant `%E'", decl);
5cce22b6
NS
11785 else if (TREE_CODE (decl) == INTEGER_CST)
11786 error ("invalid integer constant in parameter list, did you forget to give parameter name?");
11787 continue;
11788 }
8d08fdba 11789
5cce22b6
NS
11790 if (parm == void_list_node)
11791 break;
8d08fdba 11792
98884b26
JM
11793 split_specs_attrs (TREE_PURPOSE (decl), &specs, &attrs);
11794 decl = grokdeclarator (TREE_VALUE (decl), specs,
11795 PARM, init != NULL_TREE, &attrs);
5cce22b6
NS
11796 if (! decl || TREE_TYPE (decl) == error_mark_node)
11797 continue;
d363e7bf 11798
98884b26
JM
11799 if (attrs)
11800 cplus_decl_attributes (&decl, attrs, 0);
11801
5cce22b6
NS
11802 type = TREE_TYPE (decl);
11803 if (VOID_TYPE_P (type))
11804 {
11805 if (same_type_p (type, void_type_node)
11806 && !DECL_NAME (decl) && !result && !chain && !ellipsis)
11807 /* this is a parmlist of `(void)', which is ok. */
11808 break;
7a228918 11809 cxx_incomplete_type_error (decl, type);
0657c69c
MM
11810 /* It's not a good idea to actually create parameters of
11811 type `void'; other parts of the compiler assume that a
11812 void type terminates the parameter list. */
04f3dc2b 11813 type = error_mark_node;
0657c69c 11814 TREE_TYPE (decl) = error_mark_node;
5cce22b6 11815 }
8d08fdba 11816
d363e7bf 11817 if (type != error_mark_node)
04f3dc2b
MM
11818 {
11819 /* Top-level qualifiers on the parameters are
11820 ignored for function types. */
79a1a736 11821 type = cp_build_qualified_type (type, 0);
04f3dc2b
MM
11822 if (TREE_CODE (type) == METHOD_TYPE)
11823 {
33bd39a2 11824 error ("parameter `%D' invalidly declared method type", decl);
04f3dc2b
MM
11825 type = build_pointer_type (type);
11826 TREE_TYPE (decl) = type;
11827 }
04f3dc2b 11828 else if (abstract_virtuals_error (decl, type))
a1c65f9f 11829 any_error = 1; /* Seems like a good idea. */
04f3dc2b
MM
11830 else if (POINTER_TYPE_P (type))
11831 {
11832 /* [dcl.fct]/6, parameter types cannot contain pointers
11833 (references) to arrays of unknown bound. */
98979fe0
NS
11834 tree t = TREE_TYPE (type);
11835 int ptr = TYPE_PTR_P (type);
11836
11837 while (1)
11838 {
11839 if (TYPE_PTR_P (t))
11840 ptr = 1;
11841 else if (TREE_CODE (t) != ARRAY_TYPE)
11842 break;
11843 else if (!TYPE_DOMAIN (t))
11844 break;
11845 t = TREE_TYPE (t);
11846 }
04f3dc2b 11847 if (TREE_CODE (t) == ARRAY_TYPE)
33bd39a2 11848 error ("parameter `%D' includes %s to array of unknown bound `%T'",
98979fe0 11849 decl, ptr ? "pointer" : "reference", t);
04f3dc2b
MM
11850 }
11851
04f3dc2b
MM
11852 if (!any_error && init)
11853 init = check_default_argument (decl, init);
11854 else
11855 init = NULL_TREE;
11856 }
8d08fdba 11857
5cce22b6
NS
11858 TREE_CHAIN (decl) = decls;
11859 decls = decl;
0657c69c 11860 result = tree_cons (init, type, result);
8d08fdba 11861 }
5cce22b6
NS
11862 decls = nreverse (decls);
11863 result = nreverse (result);
11864 if (!ellipsis)
11865 result = chainon (result, void_list_node);
8d08fdba
MS
11866 last_function_parms = decls;
11867
8d08fdba
MS
11868 return result;
11869}
42976354 11870
8d08fdba 11871\f
271e6f02
NS
11872/* D is a constructor or overloaded `operator='.
11873
11874 Let T be the class in which D is declared. Then, this function
11875 returns:
11876
11877 -1 if D's is an ill-formed constructor or copy assignment operator
11878 whose first parameter is of type `T'.
11879 0 if D is not a copy constructor or copy assignment
11880 operator.
11881 1 if D is a copy constructor or copy assignment operator whose
11882 first parameter is a reference to const qualified T.
11883 2 if D is a copy constructor or copy assignment operator whose
11884 first parameter is a reference to non-const qualified T.
11885
11886 This function can be used as a predicate. Positive values indicate
838dfd8a 11887 a copy constructor and nonzero values indicate a copy assignment
4f1c5b7d
MM
11888 operator. */
11889
c11b6f21 11890int
11f6b451 11891copy_fn_p (tree d)
c11b6f21 11892{
271e6f02
NS
11893 tree args;
11894 tree arg_type;
11895 int result = 1;
11896
11897 my_friendly_assert (DECL_FUNCTION_MEMBER_P (d), 20011208);
4f1c5b7d 11898
271e6f02
NS
11899 if (DECL_TEMPLATE_INFO (d) && is_member_template (DECL_TI_TEMPLATE (d)))
11900 /* Instantiations of template member functions are never copy
11901 functions. Note that member functions of templated classes are
11902 represented as template functions internally, and we must
11903 accept those as copy functions. */
11904 return 0;
11905
11906 args = FUNCTION_FIRST_USER_PARMTYPE (d);
11907 if (!args)
4f1c5b7d
MM
11908 return 0;
11909
271e6f02
NS
11910 arg_type = TREE_VALUE (args);
11911
11912 if (TYPE_MAIN_VARIANT (arg_type) == DECL_CONTEXT (d))
11913 {
11914 /* Pass by value copy assignment operator. */
11915 result = -1;
11916 }
11917 else if (TREE_CODE (arg_type) == REFERENCE_TYPE
11918 && TYPE_MAIN_VARIANT (TREE_TYPE (arg_type)) == DECL_CONTEXT (d))
11919 {
11920 if (CP_TYPE_CONST_P (TREE_TYPE (arg_type)))
11921 result = 2;
11922 }
11923 else
11924 return 0;
11925
11926 args = TREE_CHAIN (args);
11927
11928 if (args && args != void_list_node && !TREE_PURPOSE (args))
11929 /* There are more non-optional args. */
11930 return 0;
11931
11932 return result;
11933}
11934
11935/* Remember any special properties of member function DECL. */
11936
11f6b451 11937void grok_special_member_properties (tree decl)
271e6f02
NS
11938{
11939 if (!DECL_NONSTATIC_MEMBER_FUNCTION_P(decl))
11940 ; /* Not special. */
11941 else if (DECL_CONSTRUCTOR_P (decl))
11942 {
11943 int ctor = copy_fn_p (decl);
11944
11945 if (ctor > 0)
11946 {
11947 /* [class.copy]
11948
11949 A non-template constructor for class X is a copy
11950 constructor if its first parameter is of type X&, const
11951 X&, volatile X& or const volatile X&, and either there
11952 are no other parameters or else all other parameters have
11953 default arguments. */
11954 TYPE_HAS_INIT_REF (DECL_CONTEXT (decl)) = 1;
11955 if (ctor > 1)
11956 TYPE_HAS_CONST_INIT_REF (DECL_CONTEXT (decl)) = 1;
11957 }
11958 else if (sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (decl)))
11959 TYPE_HAS_DEFAULT_CONSTRUCTOR (DECL_CONTEXT (decl)) = 1;
11960 }
11961 else if (DECL_OVERLOADED_OPERATOR_P (decl) == NOP_EXPR)
11962 {
11963 /* [class.copy]
11964
11965 A non-template assignment operator for class X is a copy
11966 assignment operator if its parameter is of type X, X&, const
11967 X&, volatile X& or const volatile X&. */
11968
11969 int assop = copy_fn_p (decl);
11970
11971 if (assop)
11972 {
11973 TYPE_HAS_ASSIGN_REF (DECL_CONTEXT (decl)) = 1;
11974 if (assop != 1)
11975 TYPE_HAS_CONST_ASSIGN_REF (DECL_CONTEXT (decl)) = 1;
11976 if (DECL_PURE_VIRTUAL_P (decl))
11977 TYPE_HAS_ABSTRACT_ASSIGN_REF (DECL_CONTEXT (decl)) = 1;
11978 }
11979 }
c11b6f21
MS
11980}
11981
271e6f02
NS
11982/* Check a constructor DECL has the correct form. Complains
11983 if the class has a constructor of the form X(X). */
e92cc029 11984
a0a33927 11985int
11f6b451 11986grok_ctor_properties (tree ctype, tree decl)
8d08fdba 11987{
271e6f02
NS
11988 int ctor_parm = copy_fn_p (decl);
11989
11990 if (ctor_parm < 0)
11991 {
11992 /* [class.copy]
11993
11994 A declaration of a constructor for a class X is ill-formed if
11995 its first parameter is of type (optionally cv-qualified) X
11996 and either there are no other parameters or else all other
11997 parameters have default arguments.
11998
11999 We *don't* complain about member template instantiations that
12000 have this form, though; they can occur as we try to decide
12001 what constructor to use during overload resolution. Since
12002 overload resolution will never prefer such a constructor to
12003 the non-template copy constructor (which is either explicitly
12004 or implicitly defined), there's no need to worry about their
12005 existence. Theoretically, they should never even be
12006 instantiated, but that's hard to forestall. */
33bd39a2 12007 error ("invalid constructor; you probably meant `%T (const %T&)'",
0b41abe6
JM
12008 ctype, ctype);
12009 SET_IDENTIFIER_ERROR_LOCUS (DECL_NAME (decl), ctype);
12010 return 0;
8d08fdba 12011 }
271e6f02 12012
a0a33927 12013 return 1;
8d08fdba
MS
12014}
12015
596ea4e5 12016/* An operator with this code is unary, but can also be binary. */
e92cc029 12017
a28e3c7f 12018static int
11f6b451 12019ambi_op_p (enum tree_code code)
8d08fdba 12020{
596ea4e5
AS
12021 return (code == INDIRECT_REF
12022 || code == ADDR_EXPR
12023 || code == CONVERT_EXPR
12024 || code == NEGATE_EXPR
12025 || code == PREINCREMENT_EXPR
12026 || code == PREDECREMENT_EXPR);
8d08fdba
MS
12027}
12028
12029/* An operator with this name can only be unary. */
e92cc029 12030
a28e3c7f 12031static int
11f6b451 12032unary_op_p (enum tree_code code)
8d08fdba 12033{
596ea4e5
AS
12034 return (code == TRUTH_NOT_EXPR
12035 || code == BIT_NOT_EXPR
12036 || code == COMPONENT_REF
12037 || code == TYPE_EXPR);
8d08fdba
MS
12038}
12039
4b0d3cbe
MM
12040/* DECL is a declaration for an overloaded operator. Returns true if
12041 the declaration is valid; false otherwise. If COMPLAIN is true,
12042 errors are issued for invalid declarations. */
e92cc029 12043
4b0d3cbe
MM
12044bool
12045grok_op_properties (tree decl, int friendp, bool complain)
8d08fdba
MS
12046{
12047 tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
596ea4e5 12048 tree argtype;
8d08fdba
MS
12049 int methodp = (TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE);
12050 tree name = DECL_NAME (decl);
596ea4e5
AS
12051 enum tree_code operator_code;
12052 int arity;
4b0d3cbe
MM
12053 bool ok;
12054
12055 /* Assume that the declaration is valid. */
12056 ok = true;
596ea4e5
AS
12057
12058 /* Count the number of arguments. */
12059 for (argtype = argtypes, arity = 0;
12060 argtype && argtype != void_list_node;
12061 argtype = TREE_CHAIN (argtype))
12062 ++arity;
8d08fdba 12063
a28e3c7f
MS
12064 if (current_class_type == NULL_TREE)
12065 friendp = 1;
8d08fdba 12066
596ea4e5
AS
12067 if (DECL_CONV_FN_P (decl))
12068 operator_code = TYPE_EXPR;
12069 else
12070 do
12071 {
0c918ce5
MM
12072#define DEF_OPERATOR(NAME, CODE, MANGLING, ARITY, ASSN_P) \
12073 if (ansi_opname (CODE) == name) \
12074 { \
75ac8dec 12075 operator_code = (CODE); \
0c918ce5
MM
12076 break; \
12077 } \
12078 else if (ansi_assopname (CODE) == name) \
12079 { \
75ac8dec 12080 operator_code = (CODE); \
0c918ce5
MM
12081 DECL_ASSIGNMENT_OPERATOR_P (decl) = 1; \
12082 break; \
596ea4e5
AS
12083 }
12084
12085#include "operators.def"
12086#undef DEF_OPERATOR
12087
a98facb0 12088 abort ();
596ea4e5
AS
12089 }
12090 while (0);
12091 my_friendly_assert (operator_code != LAST_CPLUS_TREE_CODE, 20000526);
12092 SET_OVERLOADED_OPERATOR_CODE (decl, operator_code);
12093
a28e3c7f
MS
12094 if (! friendp)
12095 {
596ea4e5
AS
12096 switch (operator_code)
12097 {
596ea4e5
AS
12098 case NEW_EXPR:
12099 TYPE_HAS_NEW_OPERATOR (current_class_type) = 1;
12100 break;
5362b086 12101
596ea4e5
AS
12102 case DELETE_EXPR:
12103 TYPE_GETS_DELETE (current_class_type) |= 1;
12104 break;
5362b086 12105
596ea4e5
AS
12106 case VEC_NEW_EXPR:
12107 TYPE_HAS_ARRAY_NEW_OPERATOR (current_class_type) = 1;
12108 break;
5362b086 12109
596ea4e5
AS
12110 case VEC_DELETE_EXPR:
12111 TYPE_GETS_DELETE (current_class_type) |= 2;
12112 break;
12113
12114 default:
12115 break;
12116 }
12117 }
12118
12119 if (operator_code == NEW_EXPR || operator_code == VEC_NEW_EXPR)
8d08fdba 12120 {
8d08fdba
MS
12121 /* When the compiler encounters the definition of A::operator new, it
12122 doesn't look at the class declaration to find out if it's static. */
a28e3c7f 12123 if (methodp)
3afb32a4 12124 revert_static_member_fn (decl);
68642fb6 12125
36791f1e 12126 TREE_TYPE (decl) = coerce_new_type (TREE_TYPE (decl));
8d08fdba 12127 }
596ea4e5 12128 else if (operator_code == DELETE_EXPR || operator_code == VEC_DELETE_EXPR)
8d08fdba 12129 {
a28e3c7f 12130 if (methodp)
3afb32a4 12131 revert_static_member_fn (decl);
68642fb6 12132
36791f1e 12133 TREE_TYPE (decl) = coerce_delete_type (TREE_TYPE (decl));
8d08fdba 12134 }
8d08fdba
MS
12135 else
12136 {
12137 /* An operator function must either be a non-static member function
12138 or have at least one parameter of a class, a reference to a class,
12139 an enumeration, or a reference to an enumeration. 13.4.0.6 */
700f8a87 12140 if (! methodp || DECL_STATIC_FUNCTION_P (decl))
8d08fdba 12141 {
596ea4e5
AS
12142 if (operator_code == TYPE_EXPR
12143 || operator_code == CALL_EXPR
12144 || operator_code == COMPONENT_REF
12145 || operator_code == ARRAY_REF
12146 || operator_code == NOP_EXPR)
33bd39a2 12147 error ("`%D' must be a nonstatic member function", decl);
8d08fdba
MS
12148 else
12149 {
4b0d3cbe 12150 tree p;
8d08fdba 12151
700f8a87 12152 if (DECL_STATIC_FUNCTION_P (decl))
33bd39a2 12153 error ("`%D' must be either a non-static member function or a non-member function", decl);
700f8a87 12154
4b0d3cbe
MM
12155 for (p = argtypes; p && p != void_list_node; p = TREE_CHAIN (p))
12156 {
12157 tree arg = non_reference (TREE_VALUE (p));
12158 /* IS_AGGR_TYPE, rather than CLASS_TYPE_P, is used
12159 because these checks are performed even on
12160 template functions. */
12161 if (IS_AGGR_TYPE (arg) || TREE_CODE (arg) == ENUMERAL_TYPE)
12162 break;
12163 }
12164
12165 if (!p || p == void_list_node)
12166 {
12167 if (!complain)
12168 return false;
12169
12170 error ("`%D' must have an argument of class or "
12171 "enumerated type",
12172 decl);
12173 ok = false;
12174 }
8d08fdba
MS
12175 }
12176 }
68642fb6 12177
4b0d3cbe
MM
12178 /* There are no restrictions on the arguments to an overloaded
12179 "operator ()". */
596ea4e5 12180 if (operator_code == CALL_EXPR)
4b0d3cbe 12181 return ok;
8d08fdba 12182
9a3b49ac 12183 if (IDENTIFIER_TYPENAME_P (name) && ! DECL_TEMPLATE_INFO (decl))
a0a33927
MS
12184 {
12185 tree t = TREE_TYPE (name);
17708e90 12186 if (! friendp)
a0a33927
MS
12187 {
12188 int ref = (TREE_CODE (t) == REFERENCE_TYPE);
d8e178a0 12189 const char *what = 0;
5362b086 12190
a0a33927
MS
12191 if (ref)
12192 t = TYPE_MAIN_VARIANT (TREE_TYPE (t));
12193
17708e90
NS
12194 if (TREE_CODE (t) == VOID_TYPE)
12195 what = "void";
12196 else if (t == current_class_type)
a0a33927 12197 what = "the same type";
9a3b49ac 12198 /* Don't force t to be complete here. */
a0a33927 12199 else if (IS_AGGR_TYPE (t)
d0f062fb 12200 && COMPLETE_TYPE_P (t)
a0a33927
MS
12201 && DERIVED_FROM_P (t, current_class_type))
12202 what = "a base class";
12203
dfbeb061 12204 if (what && warn_conversion)
8251199e 12205 warning ("conversion to %s%s will never use a type conversion operator",
a0a33927
MS
12206 ref ? "a reference to " : "", what);
12207 }
12208 }
271e6f02 12209 if (operator_code == COND_EXPR)
8d08fdba
MS
12210 {
12211 /* 13.4.0.3 */
33bd39a2 12212 error ("ISO C++ prohibits overloading operator ?:");
68642fb6 12213 }
596ea4e5 12214 else if (ambi_op_p (operator_code))
8d08fdba 12215 {
596ea4e5
AS
12216 if (arity == 1)
12217 /* We pick the one-argument operator codes by default, so
12218 we don't have to change anything. */
12219 ;
12220 else if (arity == 2)
8d08fdba 12221 {
596ea4e5
AS
12222 /* If we thought this was a unary operator, we now know
12223 it to be a binary operator. */
12224 switch (operator_code)
12225 {
12226 case INDIRECT_REF:
12227 operator_code = MULT_EXPR;
12228 break;
12229
12230 case ADDR_EXPR:
12231 operator_code = BIT_AND_EXPR;
12232 break;
12233
12234 case CONVERT_EXPR:
12235 operator_code = PLUS_EXPR;
12236 break;
12237
12238 case NEGATE_EXPR:
12239 operator_code = MINUS_EXPR;
12240 break;
12241
12242 case PREINCREMENT_EXPR:
12243 operator_code = POSTINCREMENT_EXPR;
12244 break;
12245
12246 case PREDECREMENT_EXPR:
655dc6ee 12247 operator_code = POSTDECREMENT_EXPR;
596ea4e5
AS
12248 break;
12249
12250 default:
a98facb0 12251 abort ();
596ea4e5
AS
12252 }
12253
12254 SET_OVERLOADED_OPERATOR_CODE (decl, operator_code);
12255
12256 if ((operator_code == POSTINCREMENT_EXPR
12257 || operator_code == POSTDECREMENT_EXPR)
5156628f 12258 && ! processing_template_decl
007e5fea 12259 && ! same_type_p (TREE_VALUE (TREE_CHAIN (argtypes)), integer_type_node))
8d08fdba
MS
12260 {
12261 if (methodp)
33bd39a2 12262 error ("postfix `%D' must take `int' as its argument",
8d08fdba
MS
12263 decl);
12264 else
33bd39a2 12265 error
8251199e
JM
12266 ("postfix `%D' must take `int' as its second argument",
12267 decl);
8d08fdba
MS
12268 }
12269 }
12270 else
12271 {
12272 if (methodp)
33bd39a2 12273 error ("`%D' must take either zero or one argument", decl);
8d08fdba 12274 else
33bd39a2 12275 error ("`%D' must take either one or two arguments", decl);
8d08fdba 12276 }
824b9a4c
MS
12277
12278 /* More Effective C++ rule 6. */
eb448459 12279 if (warn_ecpp
596ea4e5
AS
12280 && (operator_code == POSTINCREMENT_EXPR
12281 || operator_code == POSTDECREMENT_EXPR
12282 || operator_code == PREINCREMENT_EXPR
12283 || operator_code == PREDECREMENT_EXPR))
824b9a4c
MS
12284 {
12285 tree arg = TREE_VALUE (argtypes);
12286 tree ret = TREE_TYPE (TREE_TYPE (decl));
12287 if (methodp || TREE_CODE (arg) == REFERENCE_TYPE)
12288 arg = TREE_TYPE (arg);
12289 arg = TYPE_MAIN_VARIANT (arg);
596ea4e5
AS
12290 if (operator_code == PREINCREMENT_EXPR
12291 || operator_code == PREDECREMENT_EXPR)
824b9a4c
MS
12292 {
12293 if (TREE_CODE (ret) != REFERENCE_TYPE
3bfdc719
MM
12294 || !same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (ret)),
12295 arg))
33bd39a2 12296 warning ("prefix `%D' should return `%T'", decl,
824b9a4c
MS
12297 build_reference_type (arg));
12298 }
12299 else
12300 {
3bfdc719 12301 if (!same_type_p (TYPE_MAIN_VARIANT (ret), arg))
33bd39a2 12302 warning ("postfix `%D' should return `%T'", decl, arg);
824b9a4c
MS
12303 }
12304 }
8d08fdba 12305 }
596ea4e5 12306 else if (unary_op_p (operator_code))
8d08fdba 12307 {
596ea4e5 12308 if (arity != 1)
8d08fdba
MS
12309 {
12310 if (methodp)
33bd39a2 12311 error ("`%D' must take `void'", decl);
8d08fdba 12312 else
33bd39a2 12313 error ("`%D' must take exactly one argument", decl);
8d08fdba
MS
12314 }
12315 }
596ea4e5 12316 else /* if (binary_op_p (operator_code)) */
8d08fdba 12317 {
596ea4e5 12318 if (arity != 2)
8d08fdba
MS
12319 {
12320 if (methodp)
33bd39a2 12321 error ("`%D' must take exactly one argument", decl);
8d08fdba 12322 else
33bd39a2 12323 error ("`%D' must take exactly two arguments", decl);
8d08fdba 12324 }
824b9a4c
MS
12325
12326 /* More Effective C++ rule 7. */
eb448459 12327 if (warn_ecpp
596ea4e5
AS
12328 && (operator_code == TRUTH_ANDIF_EXPR
12329 || operator_code == TRUTH_ORIF_EXPR
12330 || operator_code == COMPOUND_EXPR))
33bd39a2 12331 warning ("user-defined `%D' always evaluates both arguments",
824b9a4c
MS
12332 decl);
12333 }
12334
12335 /* Effective C++ rule 23. */
eb448459 12336 if (warn_ecpp
596ea4e5 12337 && arity == 2
4bd7c270 12338 && !DECL_ASSIGNMENT_OPERATOR_P (decl)
596ea4e5
AS
12339 && (operator_code == PLUS_EXPR
12340 || operator_code == MINUS_EXPR
12341 || operator_code == TRUNC_DIV_EXPR
4bd7c270
NS
12342 || operator_code == MULT_EXPR
12343 || operator_code == TRUNC_MOD_EXPR)
824b9a4c 12344 && TREE_CODE (TREE_TYPE (TREE_TYPE (decl))) == REFERENCE_TYPE)
33bd39a2 12345 warning ("`%D' should return by value", decl);
8d08fdba 12346
271e6f02 12347 /* [over.oper]/8 */
34332678
CT
12348 for (; argtypes && argtypes != void_list_node;
12349 argtypes = TREE_CHAIN (argtypes))
12350 if (TREE_PURPOSE (argtypes))
12351 {
12352 TREE_PURPOSE (argtypes) = NULL_TREE;
596ea4e5
AS
12353 if (operator_code == POSTINCREMENT_EXPR
12354 || operator_code == POSTDECREMENT_EXPR)
34332678
CT
12355 {
12356 if (pedantic)
33bd39a2 12357 pedwarn ("`%D' cannot have default arguments", decl);
34332678
CT
12358 }
12359 else
33bd39a2 12360 error ("`%D' cannot have default arguments", decl);
34332678
CT
12361 }
12362
8d08fdba 12363 }
4b0d3cbe
MM
12364
12365 return ok;
8d08fdba
MS
12366}
12367\f
d8e178a0 12368static const char *
11f6b451 12369tag_name (enum tag_types code)
094fe153
JM
12370{
12371 switch (code)
12372 {
12373 case record_type:
12374 return "struct";
12375 case class_type:
12376 return "class";
12377 case union_type:
12378 return "union ";
12379 case enum_type:
12380 return "enum";
094fe153 12381 default:
a98facb0 12382 abort ();
094fe153
JM
12383 }
12384}
12385
befcd99b 12386/* Name lookup in an elaborated-type-specifier (after the keyword
4b0d3cbe 12387 indicated by TAG_CODE) has found the TYPE_DECL DECL. If the
befcd99b 12388 elaborated-type-specifier is invalid, issue a diagnostic and return
4b0d3cbe 12389 error_mark_node; otherwise, return the *_TYPE to which it referred.
cbd63935 12390 If ALLOW_TEMPLATE_P is true, TYPE may be a class template. */
befcd99b 12391
560ad596 12392tree
befcd99b 12393check_elaborated_type_specifier (enum tag_types tag_code,
4b0d3cbe 12394 tree decl,
cbd63935 12395 bool allow_template_p)
befcd99b 12396{
4b0d3cbe 12397 tree type;
befcd99b 12398
4b0d3cbe
MM
12399 /* In the case of:
12400
12401 struct S { struct S *p; };
12402
12403 name lookup will find the TYPE_DECL for the implicit "S::S"
12404 typedef. Adjust for that here. */
12405 if (DECL_SELF_REFERENCE_P (decl))
12406 decl = TYPE_NAME (TREE_TYPE (decl));
12407
12408 type = TREE_TYPE (decl);
12409
12410 /* [dcl.type.elab]
12411
12412 If the identifier resolves to a typedef-name or a template
12413 type-parameter, the elaborated-type-specifier is ill-formed.
12414
12415 In other words, the only legitimate declaration to use in the
12416 elaborated type specifier is the implicit typedef created when
12417 the type is declared. */
12418 if (!DECL_IMPLICIT_TYPEDEF_P (decl))
befcd99b 12419 {
4b0d3cbe
MM
12420 error ("using typedef-name `%D' after `%s'", decl, tag_name (tag_code));
12421 return IS_AGGR_TYPE (type) ? type : error_mark_node;
befcd99b 12422 }
4b0d3cbe
MM
12423
12424 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
befcd99b
MM
12425 {
12426 error ("using template type parameter `%T' after `%s'",
12427 type, tag_name (tag_code));
4b0d3cbe 12428 return error_mark_node;
befcd99b 12429 }
cbd63935
KL
12430 else if (TREE_CODE (type) != RECORD_TYPE
12431 && TREE_CODE (type) != UNION_TYPE
12432 && tag_code != enum_type)
12433 {
12434 error ("`%T' referred to as `%s'", type, tag_name (tag_code));
4b0d3cbe 12435 return error_mark_node;
cbd63935
KL
12436 }
12437 else if (TREE_CODE (type) != ENUMERAL_TYPE
12438 && tag_code == enum_type)
12439 {
12440 error ("`%T' referred to as enum", type);
4b0d3cbe 12441 return error_mark_node;
cbd63935
KL
12442 }
12443 else if (!allow_template_p
12444 && TREE_CODE (type) == RECORD_TYPE
12445 && CLASSTYPE_IS_TEMPLATE (type))
12446 {
12447 /* If a class template appears as elaborated type specifier
12448 without a template header such as:
12449
12450 template <class T> class C {};
12451 void f(class C); // No template header here
12452
12453 then the required template argument is missing. */
12454
12455 error ("template argument required for `%s %T'",
12456 tag_name (tag_code),
12457 DECL_NAME (CLASSTYPE_TI_TEMPLATE (type)));
4b0d3cbe 12458 return error_mark_node;
cbd63935 12459 }
befcd99b 12460
4b0d3cbe 12461 return type;
befcd99b
MM
12462}
12463
cbd63935 12464/* Get the struct, enum or union (TAG_CODE says which) with tag NAME.
8d08fdba
MS
12465 Define the tag as a forward-reference if it is not defined.
12466
cbd63935
KL
12467 If a declaration is given, process it here, and report an error if
12468 multiple declarations are not identical. ATTRIBUTE is the attribute
12469 appeared in this declaration.
8d08fdba 12470
cbd63935 12471 GLOBALIZE is false when this is also a definition. Only look in
8d08fdba 12472 the current frame for the name (since C++ allows new names in any
cbd63935
KL
12473 scope.)
12474
12475 TEMPLATE_HEADER_P is true when this declaration is preceded by
12476 a set of template parameters. */
8d08fdba 12477
8d08fdba 12478tree
88e5899c 12479xref_tag (enum tag_types tag_code, tree name, tree attributes,
cbd63935 12480 bool globalize, bool template_header_p)
8d08fdba 12481{
8d08fdba 12482 enum tree_code code;
cbd63935 12483 register tree t;
e2500fed 12484 struct cp_binding_level *b = current_binding_level;
25aab5d0 12485 tree context = NULL_TREE;
dc8263bc 12486
22ffcc6f 12487 timevar_push (TV_NAME_LOOKUP);
cbd63935
KL
12488
12489 my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, 0);
12490
8d08fdba
MS
12491 switch (tag_code)
12492 {
12493 case record_type:
12494 case class_type:
8d08fdba 12495 code = RECORD_TYPE;
8d08fdba
MS
12496 break;
12497 case union_type:
12498 code = UNION_TYPE;
8d08fdba
MS
12499 break;
12500 case enum_type:
12501 code = ENUMERAL_TYPE;
12502 break;
12503 default:
a98facb0 12504 abort ();
8d08fdba
MS
12505 }
12506
8ccc31eb 12507 if (! globalize)
8d08fdba 12508 {
f3400fe2
JM
12509 /* If we know we are defining this tag, only look it up in
12510 this scope and don't try to find it as a type. */
cbd63935 12511 t = lookup_tag (code, name, b, 1);
8d08fdba
MS
12512 }
12513 else
12514 {
cbd63935
KL
12515 tree decl = lookup_name (name, 1);
12516
12517 if (decl && DECL_CLASS_TEMPLATE_P (decl))
12518 decl = DECL_TEMPLATE_RESULT (decl);
68642fb6 12519
cbd63935 12520 if (decl && TREE_CODE (decl) == TYPE_DECL)
25aab5d0 12521 {
cbd63935
KL
12522 /* Two cases we need to consider when deciding if a class
12523 template is allowed as an elaborated type specifier:
12524 1. It is a self reference to its own class.
12525 2. It comes with a template header.
25aab5d0 12526
cbd63935 12527 For example:
6757edfe 12528
cbd63935
KL
12529 template <class T> class C {
12530 class C *c1; // DECL_SELF_REFERENCE_P is true
12531 class D;
12532 };
12533 template <class U> class C; // template_header_p is true
12534 template <class T> class C<T>::D {
12535 class C *c2; // DECL_SELF_REFERENCE_P is true
12536 }; */
12537
4b0d3cbe
MM
12538 t = check_elaborated_type_specifier (tag_code,
12539 decl,
cbd63935
KL
12540 template_header_p
12541 | DECL_SELF_REFERENCE_P (decl));
12542 if (t == error_mark_node)
12543 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
25aab5d0 12544 }
cbd63935
KL
12545 else
12546 t = NULL_TREE;
25aab5d0 12547
cbd63935 12548 if (t && current_class_type
68642fb6 12549 && template_class_depth (current_class_type)
cbd63935 12550 && template_header_p)
25aab5d0 12551 {
838dfd8a 12552 /* Since GLOBALIZE is nonzero, we are not looking at a
25aab5d0
MM
12553 definition of this tag. Since, in addition, we are currently
12554 processing a (member) template declaration of a template
12555 class, we must be very careful; consider:
12556
12557 template <class X>
12558 struct S1
12559
12560 template <class U>
12561 struct S2
12562 { template <class V>
12563 friend struct S1; };
12564
12565 Here, the S2::S1 declaration should not be confused with the
12566 outer declaration. In particular, the inner version should
12567 have a template parameter of level 2, not level 1. This
12568 would be particularly important if the member declaration
12569 were instead:
12570
12571 template <class V = U> friend struct S1;
12572
12573 say, when we should tsubst into `U' when instantiating
12574 S2. On the other hand, when presented with:
12575
12576 template <class T>
12577 struct S1 {
12578 template <class U>
12579 struct S2 {};
12580 template <class U>
12581 friend struct S2;
12582 };
12583
12584 we must find the inner binding eventually. We
12585 accomplish this by making sure that the new type we
12586 create to represent this declaration has the right
12587 TYPE_CONTEXT. */
cbd63935
KL
12588 context = TYPE_CONTEXT (t);
12589 t = NULL_TREE;
8d08fdba
MS
12590 }
12591 }
12592
cbd63935 12593 if (! t)
8d08fdba
MS
12594 {
12595 /* If no such tag is yet defined, create a forward-reference node
12596 and record it as the "definition".
12597 When a real declaration of this type is found,
12598 the forward-reference will be altered into a real type. */
8d08fdba
MS
12599 if (code == ENUMERAL_TYPE)
12600 {
33bd39a2 12601 error ("use of enum `%#D' without previous declaration", name);
fc378698 12602
cbd63935 12603 t = make_node (ENUMERAL_TYPE);
8d08fdba
MS
12604
12605 /* Give the type a default layout like unsigned int
12606 to avoid crashing if it does not get defined. */
cbd63935
KL
12607 TYPE_MODE (t) = TYPE_MODE (unsigned_type_node);
12608 TYPE_ALIGN (t) = TYPE_ALIGN (unsigned_type_node);
12609 TYPE_USER_ALIGN (t) = 0;
12610 TREE_UNSIGNED (t) = 1;
12611 TYPE_PRECISION (t) = TYPE_PRECISION (unsigned_type_node);
12612 TYPE_MIN_VALUE (t) = TYPE_MIN_VALUE (unsigned_type_node);
12613 TYPE_MAX_VALUE (t) = TYPE_MAX_VALUE (unsigned_type_node);
8d08fdba
MS
12614
12615 /* Enable us to recognize when a type is created in class context.
12616 To do nested classes correctly, this should probably be cleared
12617 out when we leave this classes scope. Currently this in only
12618 done in `start_enum'. */
12619
cbd63935 12620 pushtag (name, t, globalize);
8d08fdba 12621 }
8d08fdba
MS
12622 else
12623 {
cbd63935
KL
12624 t = make_aggr_type (code);
12625 TYPE_CONTEXT (t) = context;
12626 pushtag (name, t, globalize);
8d08fdba
MS
12627 }
12628 }
12629 else
12630 {
cbd63935
KL
12631 if (!globalize && processing_template_decl && IS_AGGR_TYPE (t))
12632 redeclare_class_template (t, current_template_parms);
8d08fdba
MS
12633 }
12634
cbd63935 12635 TYPE_ATTRIBUTES (t) = attributes;
dc8263bc 12636
cbd63935 12637 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
8d08fdba 12638}
8ccc31eb 12639
fc378698 12640tree
11f6b451 12641xref_tag_from_type (tree old, tree id, int globalize)
fc378698 12642{
88e5899c 12643 enum tag_types tag_kind;
fc378698
MS
12644
12645 if (TREE_CODE (old) == RECORD_TYPE)
88e5899c 12646 tag_kind = (CLASSTYPE_DECLARED_CLASS (old) ? class_type : record_type);
fc378698 12647 else
88e5899c 12648 tag_kind = union_type;
fc378698
MS
12649
12650 if (id == NULL_TREE)
12651 id = TYPE_IDENTIFIER (old);
12652
cbd63935 12653 return xref_tag (tag_kind, id, /*attributes=*/NULL_TREE, globalize, false);
fc378698
MS
12654}
12655
3fd71a52 12656/* REF is a type (named NAME), for which we have just seen some
dbbf88d1 12657 baseclasses. BASE_LIST is a list of those baseclasses; the
3fd71a52 12658 TREE_PURPOSE is an access_* node, and the TREE_VALUE is the type of
dbbf88d1
NS
12659 the base-class. TREE_VIA_VIRTUAL indicates virtual
12660 inheritance. CODE_TYPE_NODE indicates whether REF is a class,
3fd71a52
MM
12661 struct, or union. */
12662
8ccc31eb 12663void
dbbf88d1 12664xref_basetypes (tree ref, tree base_list)
8ccc31eb
MS
12665{
12666 /* In the declaration `A : X, Y, ... Z' we mark all the types
12667 (A, X, Y, ..., Z) so we can check for duplicates. */
8fbc5ae7 12668 tree *basep;
d6479fe7 12669
8fbc5ae7 12670 int i;
591382c4 12671 enum tag_types tag_code;
a5d7c4a3 12672
bef89e9e
KL
12673 if (ref == error_mark_node)
12674 return;
12675
da15dae6 12676 if (TREE_CODE (ref) == UNION_TYPE)
8ccc31eb 12677 {
33bd39a2 12678 error ("derived union `%T' invalid", ref);
8ccc31eb
MS
12679 return;
12680 }
12681
da15dae6
MM
12682 tag_code = (CLASSTYPE_DECLARED_CLASS (ref) ? class_type : record_type);
12683
d6479fe7
MM
12684 /* First, make sure that any templates in base-classes are
12685 instantiated. This ensures that if we call ourselves recursively
12686 we do not get confused about which classes are marked and which
12687 are not. */
dbbf88d1 12688 basep = &base_list;
8fbc5ae7
MM
12689 while (*basep)
12690 {
12691 tree basetype = TREE_VALUE (*basep);
12692 if (!(processing_template_decl && uses_template_parms (basetype))
12693 && !complete_type_or_else (basetype, NULL))
dbbf88d1 12694 /* An incomplete type. Remove it from the list. */
8fbc5ae7
MM
12695 *basep = TREE_CHAIN (*basep);
12696 else
12697 basep = &TREE_CHAIN (*basep);
12698 }
d6479fe7 12699
8ccc31eb 12700 SET_CLASSTYPE_MARKED (ref);
dbbf88d1
NS
12701 i = list_length (base_list);
12702 if (i)
12703 {
12704 tree binfo = TYPE_BINFO (ref);
12705 tree binfos = make_tree_vec (i);
12706 tree accesses = make_tree_vec (i);
12707
12708 BINFO_BASETYPES (binfo) = binfos;
12709 BINFO_BASEACCESSES (binfo) = accesses;
12710
12711 for (i = 0; base_list; base_list = TREE_CHAIN (base_list))
8ccc31eb 12712 {
dbbf88d1
NS
12713 tree access = TREE_PURPOSE (base_list);
12714 int via_virtual = TREE_VIA_VIRTUAL (base_list);
12715 tree basetype = TREE_VALUE (base_list);
12716 tree base_binfo;
12717
12718 if (access == access_default_node)
12719 /* The base of a derived struct is public by default. */
12720 access = (tag_code == class_type
12721 ? access_private_node : access_public_node);
12722
12723 if (basetype && TREE_CODE (basetype) == TYPE_DECL)
12724 basetype = TREE_TYPE (basetype);
12725 if (!basetype
12726 || (TREE_CODE (basetype) != RECORD_TYPE
12727 && TREE_CODE (basetype) != TYPENAME_TYPE
12728 && TREE_CODE (basetype) != TEMPLATE_TYPE_PARM
12729 && TREE_CODE (basetype) != BOUND_TEMPLATE_TEMPLATE_PARM))
12730 {
12731 error ("base type `%T' fails to be a struct or class type",
12732 basetype);
12733 continue;
12734 }
12735
12736 if (CLASSTYPE_MARKED (basetype))
12737 {
12738 if (basetype == ref)
12739 error ("recursive type `%T' undefined", basetype);
12740 else
12741 error ("duplicate base type `%T' invalid", basetype);
12742 continue;
12743 }
12744
12745 if (TYPE_FOR_JAVA (basetype)
12746 && (current_lang_depth () == 0))
12747 TYPE_FOR_JAVA (ref) = 1;
12748
12749 if (CLASS_TYPE_P (basetype))
12750 {
12751 base_binfo = TYPE_BINFO (basetype);
12752 /* This flag will be in the binfo of the base type, we must
12753 clear it after copying the base binfos. */
12754 BINFO_DEPENDENT_BASE_P (base_binfo)
12755 = dependent_type_p (basetype);
12756 }
8fbc5ae7 12757 else
dbbf88d1
NS
12758 base_binfo = make_binfo (size_zero_node, basetype,
12759 NULL_TREE, NULL_TREE);
12760
12761 TREE_VEC_ELT (binfos, i) = base_binfo;
12762 TREE_VEC_ELT (accesses, i) = access;
12763 /* This flag will be in the binfo of the base type, we must
12764 clear it after copying the base binfos. */
12765 TREE_VIA_VIRTUAL (base_binfo) = via_virtual;
12766
12767 SET_CLASSTYPE_MARKED (basetype);
12768
12769 /* We are free to modify these bits because they are meaningless
12770 at top level, and BASETYPE is a top-level type. */
12771 if (via_virtual || TYPE_USES_VIRTUAL_BASECLASSES (basetype))
12772 {
12773 TYPE_USES_VIRTUAL_BASECLASSES (ref) = 1;
12774 /* Converting to a virtual base class requires looking
12775 up the offset of the virtual base. */
12776 TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (ref) = 1;
12777 }
12778
12779 if (CLASS_TYPE_P (basetype))
12780 {
12781 TYPE_HAS_NEW_OPERATOR (ref)
12782 |= TYPE_HAS_NEW_OPERATOR (basetype);
12783 TYPE_HAS_ARRAY_NEW_OPERATOR (ref)
12784 |= TYPE_HAS_ARRAY_NEW_OPERATOR (basetype);
12785 TYPE_GETS_DELETE (ref) |= TYPE_GETS_DELETE (basetype);
12786 /* If the base-class uses multiple inheritance, so do we. */
12787 TYPE_USES_MULTIPLE_INHERITANCE (ref)
12788 |= TYPE_USES_MULTIPLE_INHERITANCE (basetype);
12789 /* Likewise, if converting to a base of the base may require
12790 code, then we may need to generate code to convert to a
12791 base as well. */
12792 TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (ref)
12793 |= TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (basetype);
12794 }
12795 i++;
8ccc31eb 12796 }
dbbf88d1
NS
12797 if (i)
12798 TREE_VEC_LENGTH (accesses) = TREE_VEC_LENGTH (binfos) = i;
12799 else
12800 BINFO_BASEACCESSES (binfo) = BINFO_BASETYPES (binfo) = NULL_TREE;
12801
12802 if (i > 1)
12803 {
12804 TYPE_USES_MULTIPLE_INHERITANCE (ref) = 1;
12805 /* If there is more than one non-empty they cannot be at the same
12806 address. */
8fbc5ae7
MM
12807 TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (ref) = 1;
12808 }
dbbf88d1
NS
12809 }
12810
12811 /* Copy the base binfos, collect the virtual bases and set the
12812 inheritance order chain. */
12813 copy_base_binfos (TYPE_BINFO (ref), ref, NULL_TREE);
12814 CLASSTYPE_VBASECLASSES (ref) = nreverse (CLASSTYPE_VBASECLASSES (ref));
12815
12816 /* Unmark all the types. */
12817 while (i--)
12818 {
12819 tree basetype = BINFO_TYPE (BINFO_BASETYPE (TYPE_BINFO (ref), i));
12820
12821 CLEAR_CLASSTYPE_MARKED (basetype);
8fbc5ae7
MM
12822 if (CLASS_TYPE_P (basetype))
12823 {
dbbf88d1
NS
12824 TREE_VIA_VIRTUAL (TYPE_BINFO (basetype)) = 0;
12825 BINFO_DEPENDENT_BASE_P (TYPE_BINFO (basetype)) = 0;
8fbc5ae7 12826 }
7ddedda4 12827 }
8ccc31eb 12828 CLEAR_CLASSTYPE_MARKED (ref);
8ccc31eb 12829}
68642fb6 12830
8d08fdba 12831\f
8d08fdba
MS
12832/* Begin compiling the definition of an enumeration type.
12833 NAME is its name (or null if anonymous).
12834 Returns the type object, as yet incomplete.
12835 Also records info about it so that build_enumerator
12836 may be used to declare the individual values as they are read. */
12837
12838tree
11f6b451 12839start_enum (tree name)
8d08fdba
MS
12840{
12841 register tree enumtype = NULL_TREE;
e2500fed 12842 struct cp_binding_level *b = current_binding_level;
8d08fdba
MS
12843
12844 /* If this is the real definition for a previous forward reference,
12845 fill in the contents in the same object that used to be the
12846 forward reference. */
12847
12848 if (name != NULL_TREE)
12849 enumtype = lookup_tag (ENUMERAL_TYPE, name, b, 1);
12850
12851 if (enumtype != NULL_TREE && TREE_CODE (enumtype) == ENUMERAL_TYPE)
30ff8252 12852 {
33bd39a2 12853 error ("multiple definition of `%#T'", enumtype);
ddd2d57e 12854 error ("%Jprevious definition here", TYPE_MAIN_DECL (enumtype));
58595203
MM
12855 /* Clear out TYPE_VALUES, and start again. */
12856 TYPE_VALUES (enumtype) = NULL_TREE;
30ff8252 12857 }
8d08fdba
MS
12858 else
12859 {
12860 enumtype = make_node (ENUMERAL_TYPE);
12861 pushtag (name, enumtype, 0);
12862 }
12863
8d08fdba
MS
12864 return enumtype;
12865}
12866
12867/* After processing and defining all the values of an enumeration type,
12868 install their decls in the enumeration type and finish it off.
968b956a 12869 ENUMTYPE is the type object and VALUES a list of name-value pairs. */
8d08fdba 12870
968b956a 12871void
11f6b451 12872finish_enum (tree enumtype)
8d08fdba 12873{
7b6d72fc
MM
12874 tree values;
12875 tree decl;
12876 tree value;
968b956a
MM
12877 tree minnode;
12878 tree maxnode;
12879 tree t;
12880 bool unsignedp;
12881 int lowprec;
12882 int highprec;
12883 int precision;
7b6d72fc 12884 integer_type_kind itk;
ad96995b 12885 tree underlying_type = NULL_TREE;
968b956a
MM
12886
12887 /* We built up the VALUES in reverse order. */
12888 TYPE_VALUES (enumtype) = nreverse (TYPE_VALUES (enumtype));
12889
f5d70cc0
JM
12890 /* For an enum defined in a template, just set the type of the values;
12891 all further processing is postponed until the template is
12892 instantiated. We need to set the type so that tsubst of a CONST_DECL
12893 works. */
968b956a
MM
12894 if (processing_template_decl)
12895 {
7b6d72fc
MM
12896 for (values = TYPE_VALUES (enumtype);
12897 values;
12898 values = TREE_CHAIN (values))
12899 TREE_TYPE (TREE_VALUE (values)) = enumtype;
5f261ba9 12900 if (at_function_scope_p ())
968b956a 12901 add_stmt (build_min (TAG_DEFN, enumtype));
968b956a
MM
12902 return;
12903 }
12904
7b6d72fc 12905 /* Determine the minimum and maximum values of the enumerators. */
968b956a 12906 if (TYPE_VALUES (enumtype))
8d08fdba 12907 {
968b956a 12908 minnode = maxnode = NULL_TREE;
5566b478 12909
7b6d72fc
MM
12910 for (values = TYPE_VALUES (enumtype);
12911 values;
12912 values = TREE_CHAIN (values))
8d08fdba 12913 {
7b6d72fc 12914 decl = TREE_VALUE (values);
f5d70cc0
JM
12915
12916 /* [dcl.enum]: Following the closing brace of an enum-specifier,
12917 each enumerator has the type of its enumeration. Prior to the
12918 closing brace, the type of each enumerator is the type of its
12919 initializing value. */
12920 TREE_TYPE (decl) = enumtype;
12921
7b6d72fc
MM
12922 /* Update the minimum and maximum values, if appropriate. */
12923 value = DECL_INITIAL (decl);
f5d70cc0
JM
12924 /* Figure out what the minimum and maximum values of the
12925 enumerators are. */
12926 if (!minnode)
12927 minnode = maxnode = value;
12928 else if (tree_int_cst_lt (maxnode, value))
12929 maxnode = value;
12930 else if (tree_int_cst_lt (value, minnode))
12931 minnode = value;
12932
12933 /* Set the TREE_TYPE for the values as well. That's so that when
12934 we call decl_constant_value we get an entity of the right type
12935 (but with the constant value). But first make a copy so we
12936 don't clobber shared INTEGER_CSTs. */
12937 if (TREE_TYPE (value) != enumtype)
fdedded1 12938 {
f5d70cc0
JM
12939 value = DECL_INITIAL (decl) = copy_node (value);
12940 TREE_TYPE (value) = enumtype;
fdedded1 12941 }
8d08fdba
MS
12942 }
12943 }
f376e137 12944 else
7b6d72fc
MM
12945 /* [dcl.enum]
12946
12947 If the enumerator-list is empty, the underlying type is as if
12948 the enumeration had a single enumerator with value 0. */
968b956a
MM
12949 minnode = maxnode = integer_zero_node;
12950
12951 /* Compute the number of bits require to represent all values of the
12952 enumeration. We must do this before the type of MINNODE and
12953 MAXNODE are transformed, since min_precision relies on the
12954 TREE_TYPE of the value it is passed. */
12955 unsignedp = tree_int_cst_sgn (minnode) >= 0;
12956 lowprec = min_precision (minnode, unsignedp);
12957 highprec = min_precision (maxnode, unsignedp);
12958 precision = MAX (lowprec, highprec);
12959
7b6d72fc
MM
12960 /* Determine the underlying type of the enumeration.
12961
12962 [dcl.enum]
12963
12964 The underlying type of an enumeration is an integral type that
12965 can represent all the enumerator values defined in the
12966 enumeration. It is implementation-defined which integral type is
12967 used as the underlying type for an enumeration except that the
12968 underlying type shall not be larger than int unless the value of
12969 an enumerator cannot fit in an int or unsigned int.
12970
12971 We use "int" or an "unsigned int" as the underlying type, even if
12972 a smaller integral type would work, unless the user has
12973 explicitly requested that we use the smallest possible type. */
12974 for (itk = (flag_short_enums ? itk_char : itk_int);
12975 itk != itk_none;
12976 itk++)
e455bc27 12977 {
7b6d72fc
MM
12978 underlying_type = integer_types[itk];
12979 if (TYPE_PRECISION (underlying_type) >= precision
12980 && TREE_UNSIGNED (underlying_type) == unsignedp)
12981 break;
12982 }
12983 if (itk == itk_none)
12984 {
12985 /* DR 377
12986
12987 IF no integral type can represent all the enumerator values, the
12988 enumeration is ill-formed. */
e455bc27
MM
12989 error ("no integral type can represent all of the enumerator values "
12990 "for `%T'", enumtype);
12991 precision = TYPE_PRECISION (long_long_integer_type_node);
7b6d72fc 12992 underlying_type = integer_types[itk_unsigned_long_long];
e455bc27
MM
12993 }
12994
7b6d72fc 12995 /* Compute the minium and maximum values for the type.
8d08fdba 12996
7b6d72fc
MM
12997 [dcl.enum]
12998
12999 For an enumeration where emin is the smallest enumerator and emax
13000 is the largest, the values of the enumeration are the values of the
13001 underlying type in the range bmin to bmax, where bmin and bmax are,
13002 respectively, the smallest and largest values of the smallest bit-
13003 field that can store emin and emax. */
13004 TYPE_PRECISION (enumtype) = precision;
13005 set_min_and_max_values_for_integral_type (enumtype, precision, unsignedp);
13006
13007 /* [dcl.enum]
13008
13009 The value of sizeof() applied to an enumeration type, an object
13010 of an enumeration type, or an enumerator, is the value of sizeof()
13011 applied to the underlying type. */
13012 TYPE_SIZE (enumtype) = TYPE_SIZE (underlying_type);
13013 TYPE_SIZE_UNIT (enumtype) = TYPE_SIZE_UNIT (underlying_type);
13014 TYPE_MODE (enumtype) = TYPE_MODE (underlying_type);
13015 TYPE_ALIGN (enumtype) = TYPE_ALIGN (underlying_type);
13016 TYPE_USER_ALIGN (enumtype) = TYPE_USER_ALIGN (underlying_type);
13017 TREE_UNSIGNED (enumtype) = TREE_UNSIGNED (underlying_type);
13018
13019 /* Convert each of the enumerators to the type of the underlying
13020 type of the enumeration. */
13021 for (values = TYPE_VALUES (enumtype); values; values = TREE_CHAIN (values))
13022 {
13023 decl = TREE_VALUE (values);
13024 value = perform_implicit_conversion (underlying_type,
13025 DECL_INITIAL (decl));
13026 TREE_TYPE (value) = enumtype;
13027 DECL_INITIAL (decl) = value;
13028 TREE_VALUE (values) = value;
e455bc27 13029 }
8d08fdba 13030
968b956a
MM
13031 /* Fix up all variant types of this enum type. */
13032 for (t = TYPE_MAIN_VARIANT (enumtype); t; t = TYPE_NEXT_VARIANT (t))
13033 {
13034 TYPE_VALUES (t) = TYPE_VALUES (enumtype);
13035 TYPE_MIN_VALUE (t) = TYPE_MIN_VALUE (enumtype);
13036 TYPE_MAX_VALUE (t) = TYPE_MAX_VALUE (enumtype);
13037 TYPE_SIZE (t) = TYPE_SIZE (enumtype);
13038 TYPE_SIZE_UNIT (t) = TYPE_SIZE_UNIT (enumtype);
13039 TYPE_MODE (t) = TYPE_MODE (enumtype);
13040 TYPE_PRECISION (t) = TYPE_PRECISION (enumtype);
13041 TYPE_ALIGN (t) = TYPE_ALIGN (enumtype);
13042 TYPE_USER_ALIGN (t) = TYPE_USER_ALIGN (enumtype);
13043 TREE_UNSIGNED (t) = TREE_UNSIGNED (enumtype);
cbf882af
MM
13044 }
13045
968b956a
MM
13046 /* Finish debugging output for this type. */
13047 rest_of_type_compilation (enumtype, namespace_bindings_p ());
8d08fdba
MS
13048}
13049
079e1098 13050/* Build and install a CONST_DECL for an enumeration constant of the
58595203 13051 enumeration type ENUMTYPE whose NAME and VALUE (if any) are provided.
8d08fdba
MS
13052 Assignment of sequential values by default is handled here. */
13053
58595203 13054void
11f6b451 13055build_enumerator (tree name, tree value, tree enumtype)
8d08fdba 13056{
58595203 13057 tree decl;
e8bd800e 13058 tree context;
58595203 13059 tree type;
8d08fdba
MS
13060
13061 /* Remove no-op casts from the value. */
13062 if (value)
13063 STRIP_TYPE_NOPS (value);
13064
58595203
MM
13065 if (! processing_template_decl)
13066 {
13067 /* Validate and default VALUE. */
13068 if (value != NULL_TREE)
13069 {
fc611ce0 13070 value = decl_constant_value (value);
58595203
MM
13071
13072 if (TREE_CODE (value) == INTEGER_CST)
13073 {
0a72704b 13074 value = perform_integral_promotions (value);
58595203
MM
13075 constant_expression_warning (value);
13076 }
13077 else
13078 {
33bd39a2 13079 error ("enumerator value for `%D' not integer constant", name);
58595203
MM
13080 value = NULL_TREE;
13081 }
13082 }
13083
13084 /* Default based on previous value. */
6a540f3c 13085 if (value == NULL_TREE)
58595203
MM
13086 {
13087 tree prev_value;
13088
13089 if (TYPE_VALUES (enumtype))
13090 {
a1c65f9f 13091 /* The next value is the previous value ... */
58595203
MM
13092 prev_value = DECL_INITIAL (TREE_VALUE (TYPE_VALUES (enumtype)));
13093 /* ... plus one. */
ab76ca54
MM
13094 value = cp_build_binary_op (PLUS_EXPR,
13095 prev_value,
13096 integer_one_node);
68642fb6 13097
58595203 13098 if (tree_int_cst_lt (value, prev_value))
33bd39a2 13099 error ("overflow in enumeration values at `%D'", name);
58595203
MM
13100 }
13101 else
13102 value = integer_zero_node;
13103 }
13104
13105 /* Remove no-op casts from the value. */
f5d70cc0 13106 STRIP_TYPE_NOPS (value);
58595203 13107 }
8d08fdba 13108
8d08fdba 13109 /* C++ associates enums with global, function, or class declarations. */
58595203 13110 context = current_scope ();
7b6d72fc
MM
13111 if (!context)
13112 context = current_namespace;
58595203
MM
13113
13114 /* Build the actual enumeration constant. Note that the enumeration
13115 constants have the type of their initializers until the
13116 enumeration is complete:
13117
13118 [ dcl.enum ]
13119
13120 Following the closing brace of an enum-specifier, each enumer-
13121 ator has the type of its enumeration. Prior to the closing
13122 brace, the type of each enumerator is the type of its
13123 initializing value.
13124
13125 In finish_enum we will reset the type. Of course, if we're
a1c65f9f 13126 processing a template, there may be no value. */
58595203
MM
13127 type = value ? TREE_TYPE (value) : NULL_TREE;
13128
13129 if (context && context == current_class_type)
13130 /* This enum declaration is local to the class. We need the full
8f17b5c5 13131 lang_decl so that we can record DECL_CLASS_CONTEXT, for example. */
58595203
MM
13132 decl = build_lang_decl (CONST_DECL, name, type);
13133 else
13134 /* It's a global enum, or it's local to a function. (Note local to
e8bd800e 13135 a function could mean local to a class method. */
58595203 13136 decl = build_decl (CONST_DECL, name, type);
e8bd800e 13137
58595203 13138 DECL_CONTEXT (decl) = FROB_CONTEXT (context);
3e4a3562 13139 TREE_CONSTANT (decl) = TREE_READONLY (decl) = 1;
58595203 13140 DECL_INITIAL (decl) = value;
e8bd800e 13141
58595203
MM
13142 if (context && context == current_class_type)
13143 /* In something like `struct S { enum E { i = 7 }; };' we put `i'
7b6d72fc
MM
13144 on the TYPE_FIELDS list for `S'. (That's so that you can say
13145 things like `S::i' later.) */
58595203
MM
13146 finish_member_declaration (decl);
13147 else
9780c24f 13148 pushdecl (decl);
58595203
MM
13149
13150 /* Add this enumeration constant to the list for this type. */
13151 TYPE_VALUES (enumtype) = tree_cons (name, decl, TYPE_VALUES (enumtype));
8d08fdba
MS
13152}
13153
8d08fdba 13154\f
a8f73d4b
MM
13155/* We're defining DECL. Make sure that it's type is OK. */
13156
13157static void
11f6b451 13158check_function_type (tree decl, tree current_function_parms)
a8f73d4b
MM
13159{
13160 tree fntype = TREE_TYPE (decl);
d0f062fb 13161 tree return_type = complete_type (TREE_TYPE (fntype));
a8f73d4b
MM
13162
13163 /* In a function definition, arg types must be complete. */
13164 require_complete_types_for_parms (current_function_parms);
13165
d0f062fb 13166 if (!COMPLETE_OR_VOID_TYPE_P (return_type))
a8f73d4b 13167 {
33bd39a2 13168 error ("return type `%#T' is incomplete", TREE_TYPE (fntype));
a8f73d4b
MM
13169
13170 /* Make it return void instead, but don't change the
13171 type of the DECL_RESULT, in case we have a named return value. */
13172 if (TREE_CODE (fntype) == METHOD_TYPE)
13173 {
13174 tree ctype = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (fntype)));
13175 TREE_TYPE (decl)
43dc123f
MM
13176 = build_method_type_directly (ctype,
13177 void_type_node,
13178 FUNCTION_ARG_CHAIN (decl));
a8f73d4b
MM
13179 }
13180 else
13181 TREE_TYPE (decl)
13182 = build_function_type (void_type_node,
13183 TYPE_ARG_TYPES (TREE_TYPE (decl)));
68642fb6 13184 TREE_TYPE (decl)
a8f73d4b
MM
13185 = build_exception_variant (fntype,
13186 TYPE_RAISES_EXCEPTIONS (fntype));
13187 }
13188 else
13189 abstract_virtuals_error (decl, TREE_TYPE (fntype));
13190}
13191
8d08fdba
MS
13192/* Create the FUNCTION_DECL for a function definition.
13193 DECLSPECS and DECLARATOR are the parts of the declaration;
13194 they describe the function's name and the type it returns,
13195 but twisted together in a fashion that parallels the syntax of C.
13196
a8f73d4b
MM
13197 FLAGS is a bitwise or of SF_PRE_PARSED (indicating that the
13198 DECLARATOR is really the DECL for the function we are about to
13199 process and that DECLSPECS should be ignored), SF_INCLASS_INLINE
f444e36b 13200 indicating that the function is an inline defined in-class.
68642fb6 13201
8d08fdba
MS
13202 This function creates a binding context for the function body
13203 as well as setting up the FUNCTION_DECL in current_function_decl.
13204
13205 Returns 1 on success. If the DECLARATOR is not suitable for a function
13206 (it defines a datum instead), we return 0, which tells
13207 yyparse to report a parse error.
13208
13209 For C++, we must first check whether that datum makes any sense.
13210 For example, "class A local_a(1,2);" means that variable local_a
13211 is an aggregate of type A, which should have a constructor
87e3dbc9 13212 applied to it with the argument list [1, 2]. */
8d08fdba
MS
13213
13214int
11f6b451 13215start_function (tree declspecs, tree declarator, tree attrs, int flags)
8d08fdba 13216{
5566b478 13217 tree decl1;
8d08fdba
MS
13218 tree ctype = NULL_TREE;
13219 tree fntype;
13220 tree restype;
8d08fdba 13221 int doing_friend = 0;
e2500fed 13222 struct cp_binding_level *bl;
f444e36b 13223 tree current_function_parms;
8d08fdba 13224
8d08fdba 13225 /* Sanity check. */
a1774733 13226 my_friendly_assert (TREE_CODE (TREE_VALUE (void_list_node)) == VOID_TYPE, 160);
8d08fdba
MS
13227 my_friendly_assert (TREE_CHAIN (void_list_node) == NULL_TREE, 161);
13228
e92cc029 13229 /* This should only be done once on the top most decl. */
594bb0e7 13230 if (have_extern_spec)
8d08fdba 13231 {
1f8f4a0b 13232 declspecs = tree_cons (NULL_TREE, get_identifier ("extern"), declspecs);
594bb0e7 13233 have_extern_spec = false;
8d08fdba
MS
13234 }
13235
a8f73d4b 13236 if (flags & SF_PRE_PARSED)
8d08fdba
MS
13237 {
13238 decl1 = declarator;
13239
8d08fdba
MS
13240 fntype = TREE_TYPE (decl1);
13241 if (TREE_CODE (fntype) == METHOD_TYPE)
13242 ctype = TYPE_METHOD_BASETYPE (fntype);
13243
cab1f180
ML
13244 /* ISO C++ 11.4/5. A friend function defined in a class is in
13245 the (lexical) scope of the class in which it is defined. */
8d08fdba
MS
13246 if (!ctype && DECL_FRIEND_P (decl1))
13247 {
4f1c5b7d 13248 ctype = DECL_FRIEND_CONTEXT (decl1);
8d08fdba
MS
13249
13250 /* CTYPE could be null here if we're dealing with a template;
13251 for example, `inline friend float foo()' inside a template
13252 will have no CTYPE set. */
13253 if (ctype && TREE_CODE (ctype) != RECORD_TYPE)
13254 ctype = NULL_TREE;
13255 else
13256 doing_friend = 1;
13257 }
13258
5566b478 13259 last_function_parms = DECL_ARGUMENTS (decl1);
8d08fdba
MS
13260 }
13261 else
13262 {
91d231cb 13263 decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, 1, NULL);
8d08fdba
MS
13264 /* If the declarator is not suitable for a function definition,
13265 cause a syntax error. */
e89a6075
JM
13266 if (decl1 == NULL_TREE || TREE_CODE (decl1) != FUNCTION_DECL)
13267 return 0;
13268
13269 cplus_decl_attributes (&decl1, attrs, 0);
8d08fdba 13270
ecb0eece 13271 /* If #pragma weak was used, mark the decl weak now. */
5f52c0e0 13272 if (global_scope_p (current_binding_level))
ecb0eece
RH
13273 maybe_apply_pragma_weak (decl1);
13274
8d08fdba
MS
13275 fntype = TREE_TYPE (decl1);
13276
13277 restype = TREE_TYPE (fntype);
8d08fdba
MS
13278
13279 if (TREE_CODE (fntype) == METHOD_TYPE)
13280 ctype = TYPE_METHOD_BASETYPE (fntype);
35680744 13281 else if (DECL_MAIN_P (decl1))
8d08fdba 13282 {
4b41d9eb
KL
13283 /* If this doesn't return integer_type, or a typedef to
13284 integer_type, complain. */
13285 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl1)), integer_type_node))
8d08fdba 13286 {
a28e3c7f 13287 if (pedantic || warn_return_type)
8251199e 13288 pedwarn ("return type for `main' changed to `int'");
8d08fdba
MS
13289 TREE_TYPE (decl1) = fntype = default_function_type;
13290 }
8d08fdba
MS
13291 }
13292 }
68642fb6 13293
97055d5c
AO
13294 if (DECL_DECLARED_INLINE_P (decl1)
13295 && lookup_attribute ("noinline", attrs))
ddd2d57e 13296 warning ("%Jinline function '%D' given attribute noinline", decl1, decl1);
97055d5c 13297
5f6eeeb3
NS
13298 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (decl1))
13299 /* This is a constructor, we must ensure that any default args
13300 introduced by this definition are propagated to the clones
13301 now. The clones are used directly in overload resolution. */
13302 adjust_clone_args (decl1);
13303
b35d4555
MM
13304 /* Sometimes we don't notice that a function is a static member, and
13305 build a METHOD_TYPE for it. Fix that up now. */
13306 if (ctype != NULL_TREE && DECL_STATIC_FUNCTION_P (decl1)
13307 && TREE_CODE (TREE_TYPE (decl1)) == METHOD_TYPE)
13308 {
3afb32a4 13309 revert_static_member_fn (decl1);
b35d4555
MM
13310 last_function_parms = TREE_CHAIN (last_function_parms);
13311 ctype = NULL_TREE;
13312 }
8d08fdba
MS
13313
13314 /* Warn if function was previously implicitly declared
13315 (but not if we warned then). */
13316 if (! warn_implicit
13317 && IDENTIFIER_IMPLICIT_DECL (DECL_NAME (decl1)) != NULL_TREE)
8251199e 13318 cp_warning_at ("`%D' implicitly declared before its definition", IDENTIFIER_IMPLICIT_DECL (DECL_NAME (decl1)));
8d08fdba 13319
f181d4ae
MM
13320 /* Set up current_class_type, and enter the scope of the class, if
13321 appropriate. */
13322 if (ctype)
14d22dd6 13323 push_nested_class (ctype);
f181d4ae 13324 else if (DECL_STATIC_FUNCTION_P (decl1))
14d22dd6 13325 push_nested_class (DECL_CONTEXT (decl1));
f181d4ae
MM
13326
13327 /* Now that we have entered the scope of the class, we must restore
13328 the bindings for any template parameters surrounding DECL1, if it
13329 is an inline member template. (Order is important; consider the
13330 case where a template parameter has the same name as a field of
13331 the class.) It is not until after this point that
13332 PROCESSING_TEMPLATE_DECL is guaranteed to be set up correctly. */
a8f73d4b 13333 if (flags & SF_INCLASS_INLINE)
f181d4ae
MM
13334 maybe_begin_member_template_processing (decl1);
13335
56cb9733 13336 /* Effective C++ rule 15. */
9188c363 13337 if (warn_ecpp
596ea4e5 13338 && DECL_OVERLOADED_OPERATOR_P (decl1) == NOP_EXPR
9188c363 13339 && TREE_CODE (TREE_TYPE (fntype)) == VOID_TYPE)
33bd39a2 13340 warning ("`operator=' should return a reference to `*this'");
9188c363
MM
13341
13342 /* Make the init_value nonzero so pushdecl knows this is not tentative.
13343 error_mark_node is replaced below (in poplevel) with the BLOCK. */
b35d4555
MM
13344 if (!DECL_INITIAL (decl1))
13345 DECL_INITIAL (decl1) = error_mark_node;
9188c363 13346
9188c363
MM
13347 /* This function exists in static storage.
13348 (This does not mean `static' in the C sense!) */
13349 TREE_STATIC (decl1) = 1;
13350
13351 /* We must call push_template_decl after current_class_type is set
13352 up. (If we are processing inline definitions after exiting a
13353 class scope, current_class_type will be NULL_TREE until set above
13354 by push_nested_class.) */
13355 if (processing_template_decl)
13356 decl1 = push_template_decl (decl1);
13357
f181d4ae 13358 /* We are now in the scope of the function being defined. */
8d08fdba 13359 current_function_decl = decl1;
f181d4ae 13360
5566b478
MS
13361 /* Save the parm names or decls from this function's declarator
13362 where store_parm_decls will find them. */
13363 current_function_parms = last_function_parms;
8d08fdba 13364
a8f73d4b
MM
13365 /* Make sure the parameter and return types are reasonable. When
13366 you declare a function, these types can be incomplete, but they
13367 must be complete when you define the function. */
5156628f 13368 if (! processing_template_decl)
f444e36b 13369 check_function_type (decl1, current_function_parms);
f181d4ae 13370
a8f73d4b
MM
13371 /* Build the return declaration for the function. */
13372 restype = TREE_TYPE (fntype);
e89a6075
JM
13373 /* Promote the value to int before returning it. */
13374 if (c_promoting_integer_type_p (restype))
13375 restype = type_promotes_to (restype);
13376 if (DECL_RESULT (decl1) == NULL_TREE)
a8f73d4b 13377 {
e89a6075
JM
13378 DECL_RESULT (decl1)
13379 = build_decl (RESULT_DECL, 0, TYPE_MAIN_VARIANT (restype));
13380 c_apply_type_quals_to_decl (cp_type_quals (restype),
13381 DECL_RESULT (decl1));
5566b478 13382 }
a8f73d4b
MM
13383
13384 /* Initialize RTL machinery. We cannot do this until
13385 CURRENT_FUNCTION_DECL and DECL_RESULT are set up. We do this
13386 even when processing a template; this is how we get
c00996a3
JM
13387 CFUN set up, and our per-function variables initialized.
13388 FIXME factor out the non-RTL stuff. */
a8f73d4b 13389 bl = current_binding_level;
4985cde3 13390 allocate_struct_function (decl1);
ff955512 13391 current_binding_level = bl;
a8f73d4b
MM
13392
13393 /* Even though we're inside a function body, we still don't want to
13394 call expand_expr to calculate the size of a variable-sized array.
13395 We haven't necessarily assigned RTL to all variables yet, so it's
13396 not safe to try to expand expressions involving them. */
13397 immediate_size_expand = 0;
01d939e8 13398 cfun->x_dont_save_pending_sizes_p = 1;
a8f73d4b 13399
f444e36b
MM
13400 /* Start the statement-tree, start the tree now. */
13401 begin_stmt_tree (&DECL_SAVED_TREE (decl1));
6f80451c 13402
d1a74aa7 13403 /* Don't double-count statements in templates. */
084c1779 13404 DECL_ESTIMATED_INSNS (decl1) = 0;
d1a74aa7 13405
a8f73d4b 13406 /* Let the user know we're compiling this function. */
ea11ca7e 13407 announce_function (decl1);
b7484fbe 13408
878cd289
MS
13409 /* Record the decl so that the function name is defined.
13410 If we already have a decl for this name, and it is a FUNCTION_DECL,
13411 use the old decl. */
a8f73d4b 13412 if (!processing_template_decl && !(flags & SF_PRE_PARSED))
878cd289 13413 {
75650646 13414 /* A specialization is not used to guide overload resolution. */
2228d450 13415 if (!DECL_TEMPLATE_SPECIALIZATION (decl1)
959d8796 13416 && ! DECL_FUNCTION_MEMBER_P (decl1))
75650646 13417 decl1 = pushdecl (decl1);
2c73f9f5 13418 else
b7698cf0 13419 {
a1c65f9f 13420 /* We need to set the DECL_CONTEXT. */
b7698cf0
JM
13421 if (!DECL_CONTEXT (decl1) && DECL_TEMPLATE_INFO (decl1))
13422 DECL_CONTEXT (decl1) = DECL_CONTEXT (DECL_TI_TEMPLATE (decl1));
13423 /* And make sure we have enough default args. */
13424 check_default_args (decl1);
13425 }
878cd289
MS
13426 fntype = TREE_TYPE (decl1);
13427 }
5566b478 13428
a8f73d4b 13429 /* Reset these in case the call to pushdecl changed them. */
5566b478 13430 current_function_decl = decl1;
01d939e8 13431 cfun->decl = decl1;
878cd289 13432
78c120b5
MM
13433 /* If we are (erroneously) defining a function that we have already
13434 defined before, wipe out what we knew before. */
e2500fed
GK
13435 if (!DECL_PENDING_INLINE_P (decl1))
13436 DECL_SAVED_FUNCTION_DATA (decl1) = NULL;
b35d4555 13437
f444e36b 13438 if (ctype && !doing_friend && !DECL_STATIC_FUNCTION_P (decl1))
b35d4555
MM
13439 {
13440 /* We know that this was set up by `grokclassfn'. We do not
13441 wait until `store_parm_decls', since evil parse errors may
13442 never get us to that point. Here we keep the consistency
13443 between `current_class_type' and `current_class_ptr'. */
13444 tree t = DECL_ARGUMENTS (decl1);
68642fb6
UD
13445
13446 my_friendly_assert (t != NULL_TREE && TREE_CODE (t) == PARM_DECL,
b35d4555
MM
13447 162);
13448 my_friendly_assert (TREE_CODE (TREE_TYPE (t)) == POINTER_TYPE,
13449 19990811);
68642fb6
UD
13450
13451 cp_function_chain->x_current_class_ref
3e411c3f 13452 = build_indirect_ref (t, NULL);
b35d4555
MM
13453 cp_function_chain->x_current_class_ptr = t;
13454
018fc244
MM
13455 /* Constructors and destructors need to know whether they're "in
13456 charge" of initializing virtual base classes. */
e0fff4b3 13457 t = TREE_CHAIN (t);
454fa7a7 13458 if (DECL_HAS_IN_CHARGE_PARM_P (decl1))
e0fff4b3
JM
13459 {
13460 current_in_charge_parm = t;
13461 t = TREE_CHAIN (t);
13462 }
13463 if (DECL_HAS_VTT_PARM_P (decl1))
13464 {
13465 if (DECL_NAME (t) != vtt_parm_identifier)
13466 abort ();
13467 current_vtt_parm = t;
13468 }
b35d4555
MM
13469 }
13470
db5ae43f 13471 if (DECL_INTERFACE_KNOWN (decl1))
faae18ab 13472 {
4f1c5b7d 13473 tree ctx = decl_function_context (decl1);
86052cc3 13474
faae18ab
MS
13475 if (DECL_NOT_REALLY_EXTERN (decl1))
13476 DECL_EXTERNAL (decl1) = 0;
86052cc3 13477
79065db2 13478 if (ctx != NULL_TREE && DECL_DECLARED_INLINE_P (ctx)
86052cc3
JM
13479 && TREE_PUBLIC (ctx))
13480 /* This is a function in a local class in an extern inline
13481 function. */
13482 comdat_linkage (decl1);
faae18ab 13483 }
8d08fdba
MS
13484 /* If this function belongs to an interface, it is public.
13485 If it belongs to someone else's interface, it is also external.
1f901793 13486 This only affects inlines and template instantiations. */
5566b478
MS
13487 else if (interface_unknown == 0
13488 && (! DECL_TEMPLATE_INSTANTIATION (decl1)
d11ad92e 13489 || flag_alt_external_templates))
8d08fdba 13490 {
79065db2
MM
13491 if (DECL_DECLARED_INLINE_P (decl1)
13492 || DECL_TEMPLATE_INSTANTIATION (decl1)
5156628f 13493 || processing_template_decl)
1f901793
JM
13494 {
13495 DECL_EXTERNAL (decl1)
13496 = (interface_only
79065db2
MM
13497 || (DECL_DECLARED_INLINE_P (decl1)
13498 && ! flag_implement_inlines
9c73ec84 13499 && !DECL_VINDEX (decl1)));
1f901793
JM
13500
13501 /* For WIN32 we also want to put these in linkonce sections. */
13502 maybe_make_one_only (decl1);
13503 }
db5ae43f 13504 else
893de33c 13505 DECL_EXTERNAL (decl1) = 0;
e8abc66f 13506 DECL_NOT_REALLY_EXTERN (decl1) = 0;
db5ae43f 13507 DECL_INTERFACE_KNOWN (decl1) = 1;
8d08fdba 13508 }
c16c47fb
JM
13509 else if (interface_unknown && interface_only
13510 && (! DECL_TEMPLATE_INSTANTIATION (decl1)
13511 || flag_alt_external_templates))
13512 {
13513 /* If MULTIPLE_SYMBOL_SPACES is defined and we saw a #pragma
13514 interface, we will have interface_only set but not
13515 interface_known. In that case, we don't want to use the normal
13516 heuristics because someone will supply a #pragma implementation
13517 elsewhere, and deducing it here would produce a conflict. */
13518 comdat_linkage (decl1);
13519 DECL_EXTERNAL (decl1) = 0;
13520 DECL_INTERFACE_KNOWN (decl1) = 1;
13521 DECL_DEFER_OUTPUT (decl1) = 1;
13522 }
8d08fdba 13523 else
a0a33927
MS
13524 {
13525 /* This is a definition, not a reference.
b7484fbe
MS
13526 So clear DECL_EXTERNAL. */
13527 DECL_EXTERNAL (decl1) = 0;
faae18ab 13528
79065db2
MM
13529 if ((DECL_DECLARED_INLINE_P (decl1)
13530 || DECL_TEMPLATE_INSTANTIATION (decl1))
5566b478
MS
13531 && ! DECL_INTERFACE_KNOWN (decl1)
13532 /* Don't try to defer nested functions for now. */
4f1c5b7d 13533 && ! decl_function_context (decl1))
878cd289
MS
13534 DECL_DEFER_OUTPUT (decl1) = 1;
13535 else
893de33c 13536 DECL_INTERFACE_KNOWN (decl1) = 1;
db5ae43f 13537 }
a9aedbc2 13538
ac20c67a 13539 begin_scope (sk_function_parms, decl1);
8d08fdba 13540
5566b478
MS
13541 ++function_depth;
13542
0d9eb3ba 13543 if (DECL_DESTRUCTOR_P (decl1))
46e8c075
MM
13544 {
13545 dtor_label = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE);
13546 DECL_CONTEXT (dtor_label) = current_function_decl;
13547 }
8d08fdba 13548
0ba8a114
NS
13549 start_fname_decls ();
13550
f444e36b
MM
13551 store_parm_decls (current_function_parms);
13552
8d08fdba
MS
13553 return 1;
13554}
13555\f
13556/* Store the parameter declarations into the current function declaration.
13557 This is called after parsing the parameter declarations, before
13558 digesting the body of the function.
13559
13560 Also install to binding contour return value identifier, if any. */
13561
f444e36b 13562static void
11f6b451 13563store_parm_decls (tree current_function_parms)
8d08fdba
MS
13564{
13565 register tree fndecl = current_function_decl;
13566 register tree parm;
8d08fdba 13567
8d08fdba
MS
13568 /* This is a chain of any other decls that came in among the parm
13569 declarations. If a parm is declared with enum {foo, bar} x;
13570 then CONST_DECLs for foo and bar are put here. */
13571 tree nonparms = NULL_TREE;
13572
b35d4555 13573 if (current_function_parms)
8d08fdba
MS
13574 {
13575 /* This case is when the function was defined with an ANSI prototype.
13576 The parms already have decls, so we need not do anything here
13577 except record them as in effect
13578 and complain if any redundant old-style parm decls were written. */
13579
b35d4555
MM
13580 tree specparms = current_function_parms;
13581 tree next;
13582
f444e36b 13583 /* Must clear this because it might contain TYPE_DECLs declared
b35d4555 13584 at class level. */
f444e36b 13585 storedecls (NULL_TREE);
8d08fdba 13586
f444e36b 13587 /* If we're doing semantic analysis, then we'll call pushdecl
b35d4555
MM
13588 for each of these. We must do them in reverse order so that
13589 they end in the correct forward order. */
f444e36b 13590 specparms = nreverse (specparms);
5566b478 13591
b35d4555 13592 for (parm = specparms; parm; parm = next)
8d08fdba
MS
13593 {
13594 next = TREE_CHAIN (parm);
13595 if (TREE_CODE (parm) == PARM_DECL)
13596 {
f444e36b
MM
13597 if (DECL_NAME (parm) == NULL_TREE
13598 || TREE_CODE (parm) != VOID_TYPE)
13599 pushdecl (parm);
13600 else
33bd39a2 13601 error ("parameter `%D' declared void", parm);
8d08fdba
MS
13602 }
13603 else
13604 {
13605 /* If we find an enum constant or a type tag,
13606 put it aside for the moment. */
13607 TREE_CHAIN (parm) = NULL_TREE;
13608 nonparms = chainon (nonparms, parm);
13609 }
13610 }
13611
f444e36b
MM
13612 /* Get the decls in their original chain order and record in the
13613 function. This is all and only the PARM_DECLs that were
13614 pushed into scope by the loop above. */
13615 DECL_ARGUMENTS (fndecl) = getdecls ();
8d08fdba
MS
13616 }
13617 else
13618 DECL_ARGUMENTS (fndecl) = NULL_TREE;
13619
13620 /* Now store the final chain of decls for the arguments
13621 as the decl-chain of the current lexical scope.
13622 Put the enumerators in as well, at the front so that
13623 DECL_ARGUMENTS is not modified. */
f444e36b 13624 storedecls (chainon (nonparms, DECL_ARGUMENTS (fndecl)));
eb448459 13625
b35d4555 13626 /* Do the starting of the exception specifications, if we have any. */
68642fb6 13627 if (flag_exceptions && !processing_template_decl
1660cb3a 13628 && flag_enforce_eh_specs
b35d4555 13629 && TYPE_RAISES_EXCEPTIONS (TREE_TYPE (current_function_decl)))
52a11cbf 13630 current_eh_spec_block = begin_eh_spec_block ();
8d08fdba
MS
13631}
13632
8d08fdba 13633\f
59026e79
MM
13634/* We have finished doing semantic analysis on DECL, but have not yet
13635 generated RTL for its body. Save away our current state, so that
13636 when we want to generate RTL later we know what to do. */
13637
13638static void
11f6b451 13639save_function_data (tree decl)
59026e79 13640{
e2500fed 13641 struct language_function *f;
59026e79
MM
13642
13643 /* Save the language-specific per-function data so that we can
13644 get it back when we really expand this function. */
13645 my_friendly_assert (!DECL_PENDING_INLINE_P (decl),
13646 19990908);
68642fb6 13647
59026e79 13648 /* Make a copy. */
c68b0a84 13649 f = ggc_alloc (sizeof (struct language_function));
e2500fed 13650 memcpy (f, cp_function_chain, sizeof (struct language_function));
59026e79
MM
13651 DECL_SAVED_FUNCTION_DATA (decl) = f;
13652
13653 /* Clear out the bits we don't need. */
ae499cce
MM
13654 f->base.x_stmt_tree.x_last_stmt = NULL_TREE;
13655 f->base.x_stmt_tree.x_last_expr_type = NULL_TREE;
59026e79
MM
13656 f->x_named_label_uses = NULL;
13657 f->bindings = NULL;
8e4ce833 13658 f->x_local_names = NULL;
59026e79 13659
914653a2
MM
13660 /* If we've already decided that we cannot inline this function, we
13661 must remember that fact when we actually go to expand the
13662 function. */
acc72c37
MM
13663 if (current_function_cannot_inline)
13664 {
13665 f->cannot_inline = current_function_cannot_inline;
13666 DECL_INLINE (decl) = 0;
13667 }
59026e79
MM
13668}
13669
cdd2559c
JM
13670/* Add a note to mark the beginning of the main body of the constructor.
13671 This is used to set up the data structures for the cleanup regions for
13672 fully-constructed bases and members. */
13673
13674static void
11f6b451 13675begin_constructor_body (void)
cdd2559c 13676{
cdd2559c
JM
13677}
13678
ade3dc07
JM
13679/* Add a note to mark the end of the main body of the constructor. This is
13680 used to end the cleanup regions for fully-constructed bases and
13681 members. */
efee38a9
MM
13682
13683static void
11f6b451 13684finish_constructor_body (void)
efee38a9 13685{
efee38a9
MM
13686}
13687
cdd2559c
JM
13688/* Do all the processing for the beginning of a destructor; set up the
13689 vtable pointers and cleanups for bases and members. */
13690
13691static void
11f6b451 13692begin_destructor_body (void)
cdd2559c
JM
13693{
13694 tree if_stmt;
13695 tree compound_stmt;
13696
13697 /* If the dtor is empty, and we know there is not any possible
13698 way we could use any vtable entries, before they are possibly
13699 set by a base class dtor, we don't have to setup the vtables,
13700 as we know that any base class dtor will set up any vtables
13701 it needs. We avoid MI, because one base class dtor can do a
13702 virtual dispatch to an overridden function that would need to
13703 have a non-related vtable set up, we cannot avoid setting up
13704 vtables in that case. We could change this to see if there
13705 is just one vtable.
13706
13707 ??? In the destructor for a class, the vtables are set
13708 appropriately for that class. There will be no non-related
13709 vtables. jason 2001-12-11. */
13710 if_stmt = begin_if_stmt ();
13711
13712 /* If it is not safe to avoid setting up the vtables, then
13713 someone will change the condition to be boolean_true_node.
13714 (Actually, for now, we do not have code to set the condition
13715 appropriately, so we just assume that we always need to
13716 initialize the vtables.) */
13717 finish_if_stmt_cond (boolean_true_node, if_stmt);
cdd2559c 13718
7a3397c7 13719 compound_stmt = begin_compound_stmt (/*has_no_scope=*/false);
cdd2559c
JM
13720
13721 /* Make all virtual function table pointers in non-virtual base
13722 classes point to CURRENT_CLASS_TYPE's virtual function
13723 tables. */
13724 initialize_vtbl_ptrs (current_class_ptr);
13725
7a3397c7 13726 finish_compound_stmt (compound_stmt);
cdd2559c
JM
13727 finish_then_clause (if_stmt);
13728 finish_if_stmt ();
13729
13730 /* And insert cleanups for our bases and members so that they
13731 will be properly destroyed if we throw. */
13732 push_base_cleanups ();
13733}
13734
ade3dc07
JM
13735/* At the end of every destructor we generate code to delete the object if
13736 necessary. Do that now. */
9bfadf57
MM
13737
13738static void
11f6b451 13739finish_destructor_body (void)
9bfadf57 13740{
9bfadf57
MM
13741 tree exprstmt;
13742
5633b37c
MM
13743 /* Any return from a destructor will end up here; that way all base
13744 and member cleanups will be run when the function returns. */
13745 add_stmt (build_stmt (LABEL_STMT, dtor_label));
13746
52682a1b
MM
13747 /* In a virtual destructor, we must call delete. */
13748 if (DECL_VIRTUAL_P (current_function_decl))
13749 {
13750 tree if_stmt;
fa72b064 13751 tree virtual_size = cxx_sizeof (current_class_type);
68642fb6 13752
52682a1b 13753 /* [class.dtor]
68642fb6 13754
ade3dc07
JM
13755 At the point of definition of a virtual destructor (including
13756 an implicit definition), non-placement operator delete shall
13757 be looked up in the scope of the destructor's class and if
13758 found shall be accessible and unambiguous. */
52682a1b
MM
13759 exprstmt = build_op_delete_call
13760 (DELETE_EXPR, current_class_ptr, virtual_size,
13761 LOOKUP_NORMAL | LOOKUP_SPECULATIVELY, NULL_TREE);
298d6f60 13762
52682a1b
MM
13763 if_stmt = begin_if_stmt ();
13764 finish_if_stmt_cond (build (BIT_AND_EXPR, integer_type_node,
13765 current_in_charge_parm,
13766 integer_one_node),
13767 if_stmt);
13768 finish_expr_stmt (exprstmt);
13769 finish_then_clause (if_stmt);
13770 finish_if_stmt ();
13771 }
ade3dc07 13772}
9bfadf57 13773
ade3dc07
JM
13774/* Do the necessary processing for the beginning of a function body, which
13775 in this case includes member-initializers, but not the catch clauses of
13776 a function-try-block. Currently, this means opening a binding level
13777 for the member-initializers (in a ctor) and member cleanups (in a dtor).
13778 In other functions, this isn't necessary, but it doesn't hurt. */
13779
13780tree
11f6b451 13781begin_function_body (void)
ade3dc07 13782{
cdd2559c
JM
13783 tree stmt;
13784
b5856475
JM
13785 if (processing_template_decl)
13786 /* Do nothing now. */;
13787 else
13788 /* Always keep the BLOCK node associated with the outermost pair of
13789 curly braces of a function. These are needed for correct
13790 operation of dwarfout.c. */
ac20c67a 13791 keep_next_level (true);
b5856475 13792
7a3397c7 13793 stmt = begin_compound_stmt (/*has_no_scope=*/false);
ade3dc07 13794 COMPOUND_STMT_BODY_BLOCK (stmt) = 1;
cdd2559c
JM
13795
13796 if (processing_template_decl)
13797 /* Do nothing now. */;
13798 else if (DECL_CONSTRUCTOR_P (current_function_decl))
13799 begin_constructor_body ();
13800 else if (DECL_DESTRUCTOR_P (current_function_decl))
13801 begin_destructor_body ();
13802
ade3dc07 13803 return stmt;
9bfadf57
MM
13804}
13805
ade3dc07
JM
13806/* Do the processing for the end of a function body. Currently, this means
13807 closing out the cleanups for fully-constructed bases and members, and in
13808 the case of the destructor, deleting the object if desired. Again, this
13809 is only meaningful for [cd]tors, since they are the only functions where
13810 there is a significant distinction between the main body and any
13811 function catch clauses. Handling, say, main() return semantics here
13812 would be wrong, as flowing off the end of a function catch clause for
13813 main() would also need to return 0. */
13814
13815void
11f6b451 13816finish_function_body (tree compstmt)
ade3dc07 13817{
5633b37c 13818 /* Close the block. */
7a3397c7 13819 finish_compound_stmt (compstmt);
ade3dc07
JM
13820
13821 if (processing_template_decl)
13822 /* Do nothing now. */;
13823 else if (DECL_CONSTRUCTOR_P (current_function_decl))
13824 finish_constructor_body ();
13825 else if (DECL_DESTRUCTOR_P (current_function_decl))
13826 finish_destructor_body ();
13827}
13828
8d08fdba
MS
13829/* Finish up a function declaration and compile that function
13830 all the way to assembler language output. The free the storage
13831 for the function definition.
13832
68642fb6 13833 FLAGS is a bitwise or of the following values:
f181d4ae
MM
13834 2 - INCLASS_INLINE
13835 We just finished processing the body of an in-class inline
13836 function definition. (This processing will have taken place
87e3dbc9 13837 after the class definition is complete.) */
8d08fdba 13838
4d6abc1c 13839tree
11f6b451 13840finish_function (int flags)
8d08fdba
MS
13841{
13842 register tree fndecl = current_function_decl;
13843 tree fntype, ctype = NULL_TREE;
f181d4ae 13844 int inclass_inline = (flags & 2) != 0;
87e3dbc9 13845 int nested;
8d08fdba
MS
13846
13847 /* When we get some parse errors, we can end up without a
13848 current_function_decl, so cope. */
13849 if (fndecl == NULL_TREE)
4d6abc1c 13850 return error_mark_node;
8d08fdba 13851
9aad8f83
MA
13852 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fndecl)
13853 && DECL_VIRTUAL_P (fndecl)
13854 && !processing_template_decl)
13855 {
13856 tree fnclass = DECL_CONTEXT (fndecl);
13857 if (fndecl == CLASSTYPE_KEY_METHOD (fnclass))
13858 keyed_classes = tree_cons (NULL_TREE, fnclass, keyed_classes);
13859 }
13860
87e3dbc9 13861 nested = function_depth > 1;
8d08fdba
MS
13862 fntype = TREE_TYPE (fndecl);
13863
9bfadf57
MM
13864 /* TREE_READONLY (fndecl) = 1;
13865 This caused &foo to be of type ptr-to-const-function
13866 which then got a warning when stored in a ptr-to-function variable. */
8d08fdba 13867
f444e36b 13868 my_friendly_assert (building_stmt_tree (), 20000911);
8d08fdba 13869
0ba8a114
NS
13870 finish_fname_decls ();
13871
db9b2174
MM
13872 /* For a cloned function, we've already got all the code we need;
13873 there's no need to add any extra bits. */
f444e36b 13874 if (!DECL_CLONED_FUNCTION_P (fndecl))
8d08fdba 13875 {
ade3dc07 13876 if (DECL_MAIN_P (current_function_decl))
efee38a9
MM
13877 {
13878 /* Make it so that `main' always returns 0 by default. */
08c7ae5a 13879#if VMS_TARGET
efee38a9
MM
13880 finish_return_stmt (integer_one_node);
13881#else
13882 finish_return_stmt (integer_zero_node);
13883#endif
13884 }
87e3dbc9 13885
b35d4555
MM
13886 /* Finish dealing with exception specifiers. */
13887 if (flag_exceptions && !processing_template_decl
1660cb3a 13888 && flag_enforce_eh_specs
b35d4555 13889 && TYPE_RAISES_EXCEPTIONS (TREE_TYPE (current_function_decl)))
52a11cbf
RH
13890 finish_eh_spec_block (TYPE_RAISES_EXCEPTIONS
13891 (TREE_TYPE (current_function_decl)),
13892 current_eh_spec_block);
5566b478 13893 }
68642fb6 13894
558475f0 13895 /* If we're saving up tree structure, tie off the function now. */
f444e36b 13896 finish_stmt_tree (&DECL_SAVED_TREE (fndecl));
8d2733ca 13897
b2dd096b
MM
13898 /* If this function can't throw any exceptions, remember that. */
13899 if (!processing_template_decl
13900 && !cp_function_chain->can_throw
13901 && !flag_non_call_exceptions)
13902 TREE_NOTHROW (fndecl) = 1;
13903
8d08fdba
MS
13904 /* This must come after expand_function_end because cleanups might
13905 have declarations (from inline functions) that need to go into
13906 this function's blocks. */
7437519c
ZW
13907
13908 /* If the current binding level isn't the outermost binding level
13909 for this function, either there is a bug, or we have experienced
13910 syntax errors and the statement tree is malformed. */
a7e8c268 13911 if (current_binding_level->kind != sk_function_parms)
7437519c
ZW
13912 {
13913 /* Make sure we have already experienced errors. */
13914 if (errorcount == 0)
13915 abort ();
13916
13917 /* Throw away the broken statement tree and extra binding
13918 levels. */
13919 DECL_SAVED_TREE (fndecl) = build_stmt (COMPOUND_STMT, NULL_TREE);
13920
a7e8c268 13921 while (current_binding_level->kind != sk_function_parms)
7437519c 13922 {
a7e8c268 13923 if (current_binding_level->kind == sk_class)
7437519c
ZW
13924 pop_nested_class ();
13925 else
13926 poplevel (0, 0, 0);
13927 }
13928 }
f444e36b 13929 poplevel (1, 0, 1);
8d08fdba 13930
4985cde3
RH
13931 /* Statements should always be full-expressions at the outermost set
13932 of curly braces for a function. */
13933 my_friendly_assert (stmts_are_full_exprs_p (), 19990831);
13934
07b2f2fd
JM
13935 /* Set up the named return value optimization, if we can. Here, we
13936 eliminate the copy from the nrv into the RESULT_DECL and any cleanup
13937 for the nrv. genrtl_start_function and declare_return_variable
13938 handle making the nrv and RESULT_DECL share space. */
13939 if (current_function_return_value)
13940 {
13941 tree r = current_function_return_value;
35e939ae
JM
13942 tree outer;
13943
07b2f2fd 13944 if (r != error_mark_node
35e939ae
JM
13945 /* This is only worth doing for fns that return in memory--and
13946 simpler, since we don't have to worry about promoted modes. */
61f71b34 13947 && aggregate_value_p (TREE_TYPE (TREE_TYPE (fndecl)), fndecl)
35e939ae
JM
13948 /* Only allow this for variables declared in the outer scope of
13949 the function so we know that their lifetime always ends with a
13950 return; see g++.dg/opt/nrv6.C. We could be more flexible if
13951 we were to do this optimization in tree-ssa. */
13952 /* Skip the artificial function body block. */
13953 && (outer = BLOCK_SUBBLOCKS (BLOCK_SUBBLOCKS (DECL_INITIAL (fndecl))),
13954 chain_member (r, BLOCK_VARS (outer))))
07b2f2fd 13955 {
35e939ae 13956
07b2f2fd
JM
13957 DECL_ALIGN (r) = DECL_ALIGN (DECL_RESULT (fndecl));
13958 walk_tree_without_duplicates (&DECL_SAVED_TREE (fndecl),
13959 nullify_returns_r, r);
13960 }
13961 else
13962 /* Clear it so genrtl_start_function and declare_return_variable
13963 know we're not optimizing. */
13964 current_function_return_value = NULL_TREE;
13965 }
13966
a8f73d4b 13967 /* Remember that we were in class scope. */
db5ae43f 13968 if (current_class_name)
a8f73d4b 13969 ctype = current_class_type;
db5ae43f 13970
1caa11d3
MM
13971 /* Must mark the RESULT_DECL as being in this function. */
13972 DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
13973
13974 /* Set the BLOCK_SUPERCONTEXT of the outermost function scope to point
13975 to the FUNCTION_DECL node itself. */
13976 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
13977
59026e79 13978 /* Save away current state, if appropriate. */
f444e36b 13979 if (!processing_template_decl)
59026e79
MM
13980 save_function_data (fndecl);
13981
95fabfd3
MM
13982 /* If this function calls `setjmp' it cannot be inlined. When
13983 `longjmp' is called it is not guaranteed to restore the value of
13984 local variables that have been modified since the call to
13985 `setjmp'. So, if were to inline this function into some caller
13986 `c', then when we `longjmp', we might not restore all variables
13987 in `c'. (It might seem, at first blush, that there's no way for
13988 this function to modify local variables in `c', but their
13989 addresses may have been stored somewhere accessible to this
13990 function.) */
f444e36b 13991 if (!processing_template_decl && calls_setjmp_p (fndecl))
95fabfd3
MM
13992 DECL_UNINLINABLE (fndecl) = 1;
13993
efe49da0 13994 /* Complain if there's just no return statement. */
46cfb101 13995 if (warn_return_type
efe49da0 13996 && TREE_CODE (TREE_TYPE (fntype)) != VOID_TYPE
57d5032b 13997 && !current_function_returns_value && !current_function_returns_null
efe49da0
JM
13998 /* Don't complain if we abort or throw. */
13999 && !current_function_returns_abnormally
46cfb101
JM
14000 && !DECL_NAME (DECL_RESULT (fndecl))
14001 /* Normally, with -Wreturn-type, flow will complain. Unless we're an
efe49da0 14002 inline function, as we might never be compiled separately. */
efc7052d 14003 && (DECL_INLINE (fndecl) || processing_template_decl))
efe49da0 14004 warning ("no return statement in function returning non-void");
4985cde3
RH
14005
14006 /* We're leaving the context of this function, so zap cfun. It's still in
14007 DECL_SAVED_INSNS, and we'll restore it in tree_rest_of_compilation. */
f444e36b 14008 cfun = NULL;
c1f927e8 14009 current_function_decl = NULL;
a8f73d4b 14010
27631dae 14011 /* If this is an in-class inline definition, we may have to pop the
a8f73d4b
MM
14012 bindings for the template parameters that we added in
14013 maybe_begin_member_template_processing when start_function was
14014 called. */
14015 if (inclass_inline)
14016 maybe_end_member_template_processing ();
14017
14018 /* Leave the scope of the class. */
14019 if (ctype)
14020 pop_nested_class ();
5566b478
MS
14021
14022 --function_depth;
8d08fdba 14023
4d6abc1c 14024 /* Clean up. */
28cbf42c 14025 if (! nested)
1f8f4a0b
MM
14026 /* Let the error reporting routines know that we're outside a
14027 function. For a nested function, this value is used in
b03e38e1 14028 cxx_pop_function_context and then reset via pop_function_context. */
1f8f4a0b 14029 current_function_decl = NULL_TREE;
4d6abc1c
MM
14030
14031 return fndecl;
8d08fdba
MS
14032}
14033\f
14034/* Create the FUNCTION_DECL for a function definition.
8d08fdba
MS
14035 DECLSPECS and DECLARATOR are the parts of the declaration;
14036 they describe the return type and the name of the function,
14037 but twisted together in a fashion that parallels the syntax of C.
14038
14039 This function creates a binding context for the function body
14040 as well as setting up the FUNCTION_DECL in current_function_decl.
14041
14042 Returns a FUNCTION_DECL on success.
14043
14044 If the DECLARATOR is not suitable for a function (it defines a datum
14045 instead), we return 0, which tells yyparse to report a parse error.
14046
14047 May return void_type_node indicating that this method is actually
14048 a friend. See grokfield for more details.
14049
14050 Came here with a `.pushlevel' .
14051
14052 DO NOT MAKE ANY CHANGES TO THIS CODE WITHOUT MAKING CORRESPONDING
14053 CHANGES TO CODE IN `grokfield'. */
e92cc029 14054
8d08fdba 14055tree
11f6b451 14056start_method (tree declspecs, tree declarator, tree attrlist)
8d08fdba 14057{
c11b6f21 14058 tree fndecl = grokdeclarator (declarator, declspecs, MEMFUNCDEF, 0,
91d231cb 14059 &attrlist);
8d08fdba 14060
a723baf1
MM
14061 if (fndecl == error_mark_node)
14062 return error_mark_node;
14063
14064 if (fndecl == NULL || TREE_CODE (fndecl) != FUNCTION_DECL)
14065 {
14066 error ("invalid member function declaration");
14067 return error_mark_node;
14068 }
8d08fdba 14069
195a5def
IR
14070 if (attrlist)
14071 cplus_decl_attributes (&fndecl, attrlist, 0);
14072
8d08fdba 14073 /* Pass friends other than inline friend functions back. */
a1774733 14074 if (fndecl == void_type_node)
8d08fdba
MS
14075 return fndecl;
14076
8d08fdba
MS
14077 if (DECL_IN_AGGR_P (fndecl))
14078 {
14079 if (IDENTIFIER_ERROR_LOCUS (DECL_ASSEMBLER_NAME (fndecl)) != current_class_type)
14080 {
68642fb6 14081 if (DECL_CONTEXT (fndecl)
2c73f9f5 14082 && TREE_CODE( DECL_CONTEXT (fndecl)) != NAMESPACE_DECL)
33bd39a2 14083 error ("`%D' is already defined in class `%T'", fndecl,
2ae7bada 14084 DECL_CONTEXT (fndecl));
8d08fdba
MS
14085 }
14086 return void_type_node;
14087 }
14088
f3400fe2
JM
14089 check_template_shadow (fndecl);
14090
79065db2 14091 DECL_DECLARED_INLINE_P (fndecl) = 1;
8926095f 14092 if (flag_default_inline)
8d08fdba
MS
14093 DECL_INLINE (fndecl) = 1;
14094
36a117a5
MM
14095 /* We process method specializations in finish_struct_1. */
14096 if (processing_template_decl && !DECL_TEMPLATE_SPECIALIZATION (fndecl))
4b0d3cbe
MM
14097 {
14098 fndecl = push_template_decl (fndecl);
14099 if (fndecl == error_mark_node)
14100 return fndecl;
14101 }
a0a33927 14102
8d08fdba
MS
14103 if (! DECL_FRIEND_P (fndecl))
14104 {
8d08fdba
MS
14105 if (TREE_CHAIN (fndecl))
14106 {
14107 fndecl = copy_node (fndecl);
14108 TREE_CHAIN (fndecl) = NULL_TREE;
14109 }
271e6f02 14110 grok_special_member_properties (fndecl);
8d08fdba
MS
14111 }
14112
cd9f6678 14113 cp_finish_decl (fndecl, NULL_TREE, NULL_TREE, 0);
8d08fdba
MS
14114
14115 /* Make a place for the parms */
ac20c67a 14116 begin_scope (sk_function_parms, fndecl);
68642fb6 14117
8d08fdba
MS
14118 DECL_IN_AGGR_P (fndecl) = 1;
14119 return fndecl;
14120}
14121
14122/* Go through the motions of finishing a function definition.
14123 We don't compile this method until after the whole class has
14124 been processed.
14125
14126 FINISH_METHOD must return something that looks as though it
14127 came from GROKFIELD (since we are defining a method, after all).
14128
14129 This is called after parsing the body of the function definition.
14130 STMTS is the chain of statements that makes up the function body.
14131
14132 DECL is the ..._DECL that `start_method' provided. */
14133
14134tree
11f6b451 14135finish_method (tree decl)
8d08fdba
MS
14136{
14137 register tree fndecl = decl;
14138 tree old_initial;
8d08fdba
MS
14139
14140 register tree link;
14141
a1774733 14142 if (decl == void_type_node)
8d08fdba
MS
14143 return decl;
14144
14145 old_initial = DECL_INITIAL (fndecl);
14146
14147 /* Undo the level for the parms (from start_method).
14148 This is like poplevel, but it causes nothing to be
14149 saved. Saving information here confuses symbol-table
14150 output routines. Besides, this information will
14151 be correctly output when this method is actually
14152 compiled. */
14153
14154 /* Clear out the meanings of the local variables of this level;
14155 also record in each decl which block it belongs to. */
14156
14157 for (link = current_binding_level->names; link; link = TREE_CHAIN (link))
14158 {
14159 if (DECL_NAME (link) != NULL_TREE)
d8f8dca1 14160 pop_binding (DECL_NAME (link), link);
8d08fdba
MS
14161 my_friendly_assert (TREE_CODE (link) != FUNCTION_DECL, 163);
14162 DECL_CONTEXT (link) = NULL_TREE;
14163 }
14164
8d08fdba
MS
14165 poplevel (0, 0, 0);
14166
14167 DECL_INITIAL (fndecl) = old_initial;
14168
14169 /* We used to check if the context of FNDECL was different from
14170 current_class_type as another way to get inside here. This didn't work
14171 for String.cc in libg++. */
14172 if (DECL_FRIEND_P (fndecl))
14173 {
14174 CLASSTYPE_INLINE_FRIENDS (current_class_type)
14175 = tree_cons (NULL_TREE, fndecl, CLASSTYPE_INLINE_FRIENDS (current_class_type));
14176 decl = void_type_node;
14177 }
14178
14179 return decl;
14180}
14181\f
0154eaa8
MM
14182
14183/* VAR is a VAR_DECL. If its type is incomplete, remember VAR so that
14184 we can lay it out later, when and if its type becomes complete. */
8d08fdba
MS
14185
14186void
11f6b451 14187maybe_register_incomplete_var (tree var)
8d08fdba 14188{
0154eaa8 14189 my_friendly_assert (TREE_CODE (var) == VAR_DECL, 20020406);
8d08fdba 14190
0154eaa8
MM
14191 /* Keep track of variables with incomplete types. */
14192 if (!processing_template_decl && TREE_TYPE (var) != error_mark_node
14193 && DECL_EXTERNAL (var))
70adf8a9 14194 {
0154eaa8
MM
14195 tree inner_type = TREE_TYPE (var);
14196
14197 while (TREE_CODE (inner_type) == ARRAY_TYPE)
14198 inner_type = TREE_TYPE (inner_type);
14199 inner_type = TYPE_MAIN_VARIANT (inner_type);
14200
14201 if ((!COMPLETE_TYPE_P (inner_type) && CLASS_TYPE_P (inner_type))
14202 /* RTTI TD entries are created while defining the type_info. */
14203 || (TYPE_LANG_SPECIFIC (inner_type)
14204 && TYPE_BEING_DEFINED (inner_type)))
14205 incomplete_vars = tree_cons (inner_type, var, incomplete_vars);
70adf8a9 14206 }
0154eaa8 14207}
70adf8a9 14208
0154eaa8
MM
14209/* Called when a class type (given by TYPE) is defined. If there are
14210 any existing VAR_DECLs whose type hsa been completed by this
14211 declaration, update them now. */
70adf8a9 14212
0154eaa8 14213void
11f6b451 14214complete_vars (tree type)
0154eaa8
MM
14215{
14216 tree *list = &incomplete_vars;
14217
14218 my_friendly_assert (CLASS_TYPE_P (type), 20020406);
14219 while (*list)
14220 {
14221 if (same_type_p (type, TREE_PURPOSE (*list)))
70adf8a9 14222 {
0154eaa8 14223 tree var = TREE_VALUE (*list);
c740732f
MM
14224 /* Complete the type of the variable. The VAR_DECL itself
14225 will be laid out in expand_expr. */
14226 complete_type (TREE_TYPE (var));
0154eaa8
MM
14227 /* Remove this entry from the list. */
14228 *list = TREE_CHAIN (*list);
f30432d7
MS
14229 }
14230 else
0154eaa8 14231 list = &TREE_CHAIN (*list);
f30432d7 14232 }
8d08fdba
MS
14233}
14234
86f45d2c
MM
14235/* If DECL is of a type which needs a cleanup, build that cleanup
14236 here. */
e92cc029 14237
86f45d2c 14238tree
11f6b451 14239cxx_maybe_build_cleanup (tree decl)
8d08fdba
MS
14240{
14241 tree type = TREE_TYPE (decl);
86f45d2c 14242
834c6dff 14243 if (type != error_mark_node && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
8d08fdba 14244 {
80048418 14245 int flags = LOOKUP_NORMAL|LOOKUP_DESTRUCTOR;
8d08fdba 14246 tree rval;
8d08fdba 14247
8d08fdba
MS
14248 if (TREE_CODE (type) == ARRAY_TYPE)
14249 rval = decl;
14250 else
14251 {
dffd7eb6 14252 cxx_mark_addressable (decl);
8d08fdba
MS
14253 rval = build_unary_op (ADDR_EXPR, decl, 0);
14254 }
14255
14256 /* Optimize for space over speed here. */
14257 if (! TYPE_USES_VIRTUAL_BASECLASSES (type)
14258 || flag_expensive_optimizations)
14259 flags |= LOOKUP_NONVIRTUAL;
14260
86f45d2c
MM
14261 rval = build_delete (TREE_TYPE (rval), rval,
14262 sfk_complete_destructor, flags, 0);
8d08fdba
MS
14263
14264 if (TYPE_USES_VIRTUAL_BASECLASSES (type)
14265 && ! TYPE_HAS_DESTRUCTOR (type))
c7b62f14 14266 rval = build_compound_expr (rval, build_vbase_delete (type, decl));
8d08fdba 14267
8d08fdba
MS
14268 return rval;
14269 }
6e4ae815 14270 return NULL_TREE;
8d08fdba
MS
14271}
14272\f
558475f0 14273/* When a stmt has been parsed, this function is called. */
8d08fdba
MS
14274
14275void
11f6b451 14276finish_stmt (void)
8d08fdba 14277{
558475f0
MM
14278 /* Always assume this statement was not an expression statement. If
14279 it actually was an expression statement, its our callers
14280 responsibility to fix this up. */
14281 last_expr_type = NULL_TREE;
8d08fdba
MS
14282}
14283
3afb32a4
MM
14284/* DECL was originally constructed as a non-static member function,
14285 but turned out to be static. Update it accordingly. */
700f8a87 14286
8857f91e 14287void
11f6b451 14288revert_static_member_fn (tree decl)
8d08fdba 14289{
700f8a87 14290 tree tmp;
3afb32a4
MM
14291 tree function = TREE_TYPE (decl);
14292 tree args = TYPE_ARG_TYPES (function);
8d08fdba 14293
89d684bb 14294 if (cp_type_quals (TREE_TYPE (TREE_VALUE (args)))
91063b51 14295 != TYPE_UNQUALIFIED)
33bd39a2 14296 error ("static member function `%#D' declared with type qualifiers",
11306230 14297 decl);
f30432d7 14298
700f8a87
MS
14299 args = TREE_CHAIN (args);
14300 tmp = build_function_type (TREE_TYPE (function), args);
89d684bb 14301 tmp = build_qualified_type (tmp, cp_type_quals (function));
f30432d7 14302 tmp = build_exception_variant (tmp,
8d08fdba 14303 TYPE_RAISES_EXCEPTIONS (function));
3afb32a4
MM
14304 TREE_TYPE (decl) = tmp;
14305 if (DECL_ARGUMENTS (decl))
14306 DECL_ARGUMENTS (decl) = TREE_CHAIN (DECL_ARGUMENTS (decl));
14307 DECL_STATIC_FUNCTION_P (decl) = 1;
8d08fdba 14308}
a4443a08 14309
68642fb6
UD
14310/* Initialize the variables used during compilation of a C++
14311 function. */
db5ae43f 14312
b03e38e1 14313void
11f6b451 14314cxx_push_function_context (struct function * f)
99dccabc 14315{
e2500fed 14316 struct language_function *p
c68b0a84 14317 = ggc_alloc_cleared (sizeof (struct language_function));
e2500fed 14318 f->language = p;
db5ae43f 14319
f1dedc31
MM
14320 /* Whenever we start a new function, we destroy temporaries in the
14321 usual way. */
ae499cce 14322 current_stmt_tree ()->stmts_are_full_exprs_p = 1;
4985cde3
RH
14323
14324 if (f->decl)
14325 {
14326 tree fn = f->decl;
14327
14328 current_function_is_thunk = DECL_THUNK_P (fn);
14329
14330 if (DECL_SAVED_FUNCTION_DATA (fn))
14331 {
14332 /* If we already parsed this function, and we're just expanding it
14333 now, restore saved state. */
14334 *cp_function_chain = *DECL_SAVED_FUNCTION_DATA (fn);
14335
14336 /* If we decided that we didn't want to inline this function,
14337 make sure the back-end knows that. */
14338 if (!current_function_cannot_inline)
14339 current_function_cannot_inline = cp_function_chain->cannot_inline;
14340
14341 /* We don't need the saved data anymore. Unless this is an inline
14342 function; we need the named return value info for
14343 cp_copy_res_decl_for_inlining. */
14344 if (! DECL_INLINE (fn))
14345 DECL_SAVED_FUNCTION_DATA (fn) = NULL;
14346 }
14347 }
db5ae43f
MS
14348}
14349
a8f73d4b
MM
14350/* Free the language-specific parts of F, now that we've finished
14351 compiling the function. */
db5ae43f 14352
b03e38e1 14353void
11f6b451 14354cxx_pop_function_context (struct function * f)
db5ae43f 14355{
99dccabc 14356 f->language = 0;
db5ae43f 14357}
ebfc180f 14358
e2500fed
GK
14359/* Return which tree structure is used by T, or TS_CP_GENERIC if T is
14360 one of the language-independent trees. */
4519c0a8 14361
e2500fed 14362enum cp_tree_node_structure_enum
11f6b451 14363cp_tree_node_structure (union lang_tree_node * t)
4519c0a8 14364{
e2500fed 14365 switch (TREE_CODE (&t->generic))
4519c0a8 14366 {
a723baf1 14367 case DEFAULT_ARG: return TS_CP_DEFAULT_ARG;
e2500fed 14368 case IDENTIFIER_NODE: return TS_CP_IDENTIFIER;
e2500fed
GK
14369 case OVERLOAD: return TS_CP_OVERLOAD;
14370 case TEMPLATE_PARM_INDEX: return TS_CP_TPI;
14371 case PTRMEM_CST: return TS_CP_PTRMEM;
5dae1114 14372 case BASELINK: return TS_CP_BASELINK;
e2500fed 14373 case WRAPPER: return TS_CP_WRAPPER;
e2500fed 14374 default: return TS_CP_GENERIC;
4519c0a8
MM
14375 }
14376}
5fd8e536
JM
14377
14378/* Return the IDENTIFIER_GLOBAL_VALUE of T, for use in common code, since
14379 the definition of IDENTIFIER_GLOBAL_VALUE is different for C and C++. */
14380
14381tree
11f6b451 14382identifier_global_value (tree t)
5fd8e536
JM
14383{
14384 return IDENTIFIER_GLOBAL_VALUE (t);
14385}
d363e7bf 14386
eaa7c03f
JM
14387/* Build the void_list_node (void_type_node having been created). */
14388tree
11f6b451 14389build_void_list_node (void)
eaa7c03f
JM
14390{
14391 tree t = build_tree_list (NULL_TREE, void_type_node);
14392 TREE_PARMLIST (t) = 1;
14393 return t;
14394}
14395
d363e7bf 14396static int
11f6b451 14397cp_missing_noreturn_ok_p (tree decl)
d363e7bf
AJ
14398{
14399 /* A missing noreturn is ok for the `main' function. */
92643fea 14400 return DECL_MAIN_P (decl);
d363e7bf 14401}
e2500fed
GK
14402
14403#include "gt-cp-decl.h"
14404#include "gtype-cp.h"
This page took 5.794277 seconds and 5 git commands to generate.