]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/cp/decl.c
IA-64 ABI Exception Handling.
[thirdparty/gcc.git] / gcc / cp / decl.c
CommitLineData
e5e809f4 1/* Process declarations and variables for C compiler.
d363e7bf
AJ
2 Copyright (C) 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
3 2001 Free Software Foundation, Inc.
e5e809f4 4 Contributed by Michael Tiemann (tiemann@cygnus.com)
8d08fdba
MS
5
6This file is part of GNU CC.
7
8GNU CC is free software; you can redistribute it and/or modify
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
13GNU CC is distributed in the hope that it will be useful,
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
19along with GNU CC; 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
24/* Process declarations and symbol lookup for C front end.
25 Also constructs types; the standard scalar types at initialization,
26 and structure, union, array and enum types when they are declared. */
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"
8d08fdba
MS
33#include "tree.h"
34#include "rtl.h"
3bdf5ad1 35#include "expr.h"
8d08fdba
MS
36#include "flags.h"
37#include "cp-tree.h"
38#include "decl.h"
39#include "lex.h"
49c249e1
JM
40#include "output.h"
41#include "except.h"
54f92bfb 42#include "toplev.h"
7ddedda4 43#include "../hash.h"
4519c0a8 44#include "ggc.h"
0e9295cf 45#include "tm_p.h"
8d08fdba 46
8d08fdba
MS
47extern int current_class_depth;
48
30394414
JM
49extern tree global_namespace;
50
158991b7 51extern int (*valid_lang_attribute) PARAMS ((tree, tree, tree, tree));
62c154ed 52
255512c1 53#ifndef BOOL_TYPE_SIZE
2ef16140
MM
54/* In the new ABI, `bool' has size and alignment `1', on all
55 platforms. */
d2e5ee5c 56#define BOOL_TYPE_SIZE CHAR_TYPE_SIZE
e1cd6e56 57#endif
255512c1 58
5cce22b6 59static tree grokparms PARAMS ((tree));
158991b7 60static const char *redeclaration_error_message PARAMS ((tree, tree));
8d08fdba 61
158991b7 62static void push_binding_level PARAMS ((struct binding_level *, int,
49c249e1 63 int));
158991b7
KG
64static void pop_binding_level PARAMS ((void));
65static void suspend_binding_level PARAMS ((void));
66static void resume_binding_level PARAMS ((struct binding_level *));
67static struct binding_level *make_binding_level PARAMS ((void));
68static void declare_namespace_level PARAMS ((void));
6625cdb5 69static int decl_jump_unsafe PARAMS ((tree));
158991b7
KG
70static void storedecls PARAMS ((tree));
71static void require_complete_types_for_parms PARAMS ((tree));
596ea4e5
AS
72static int ambi_op_p PARAMS ((enum tree_code));
73static int unary_op_p PARAMS ((enum tree_code));
158991b7
KG
74static tree store_bindings PARAMS ((tree, tree));
75static tree lookup_tag_reverse PARAMS ((tree, tree));
76static tree obscure_complex_init PARAMS ((tree, tree));
158991b7 77static tree lookup_name_real PARAMS ((tree, int, int, int));
8e4ce833 78static void push_local_name PARAMS ((tree));
158991b7 79static void warn_extern_redeclared_static PARAMS ((tree, tree));
8e4ce833 80static tree grok_reference_init PARAMS ((tree, tree, tree));
158991b7 81static tree grokfndecl PARAMS ((tree, tree, tree, tree, int,
2c73f9f5 82 enum overload_flags, tree,
7a8f9fa9 83 tree, int, int, int, int, int, int, tree));
158991b7
KG
84static tree grokvardecl PARAMS ((tree, tree, RID_BIT_TYPE *, int, int, tree));
85static tree lookup_tag PARAMS ((enum tree_code, tree,
49c249e1
JM
86 struct binding_level *, int));
87static void set_identifier_type_value_with_scope
158991b7 88 PARAMS ((tree, tree, struct binding_level *));
158991b7 89static void record_unknown_type PARAMS ((tree, const char *));
596ea4e5 90static tree build_library_fn_1 PARAMS ((tree, enum tree_code, tree));
158991b7
KG
91static int member_function_or_else PARAMS ((tree, tree, enum overload_flags));
92static void bad_specifiers PARAMS ((tree, const char *, int, int, int, int,
49c249e1 93 int));
158991b7
KG
94static tree maybe_process_template_type_declaration PARAMS ((tree, int, struct binding_level*));
95static void check_for_uninitialized_const_var PARAMS ((tree));
96static unsigned long typename_hash PARAMS ((hash_table_key));
d6edb99e 97static bool typename_compare PARAMS ((hash_table_key, hash_table_key));
158991b7
KG
98static void push_binding PARAMS ((tree, tree, struct binding_level*));
99static int add_binding PARAMS ((tree, tree));
100static void pop_binding PARAMS ((tree, tree));
101static tree local_variable_p_walkfn PARAMS ((tree *, int *, void *));
102static tree find_binding PARAMS ((tree, tree));
103static tree select_decl PARAMS ((tree, int));
104static int lookup_flags PARAMS ((int, int));
105static tree qualify_lookup PARAMS ((tree, int));
106static tree record_builtin_java_type PARAMS ((const char *, int));
107static const char *tag_name PARAMS ((enum tag_types code));
108static void find_class_binding_level PARAMS ((void));
109static struct binding_level *innermost_nonclass_level PARAMS ((void));
110static void warn_about_implicit_typename_lookup PARAMS ((tree, tree));
111static int walk_namespaces_r PARAMS ((tree, walk_namespaces_fn, void *));
112static int walk_globals_r PARAMS ((tree, void *));
113static void add_decl_to_level PARAMS ((tree, struct binding_level *));
114static tree make_label_decl PARAMS ((tree, int));
6625cdb5
JM
115static void use_label PARAMS ((tree));
116static void check_previous_goto_1 PARAMS ((tree, struct binding_level *, tree,
117 const char *, int));
118static void check_previous_goto PARAMS ((struct named_label_use_list *));
119static void check_switch_goto PARAMS ((struct binding_level *));
120static void check_previous_gotos PARAMS ((tree));
121static void pop_label PARAMS ((tree, tree));
158991b7
KG
122static void pop_labels PARAMS ((tree));
123static void maybe_deduce_size_from_array_init PARAMS ((tree, tree));
124static void layout_var_decl PARAMS ((tree));
125static void maybe_commonize_var PARAMS ((tree));
126static tree check_initializer PARAMS ((tree, tree));
127static void make_rtl_for_nonlocal_decl PARAMS ((tree, tree, const char *));
128static void push_cp_function_context PARAMS ((struct function *));
129static void pop_cp_function_context PARAMS ((struct function *));
130static void mark_binding_level PARAMS ((void *));
2f8edb39 131static void mark_named_label_lists PARAMS ((void *, void *));
158991b7
KG
132static void mark_cp_function_context PARAMS ((struct function *));
133static void mark_saved_scope PARAMS ((void *));
ae499cce 134static void mark_lang_function PARAMS ((struct cp_language_function *));
158991b7 135static void save_function_data PARAMS ((tree));
f444e36b 136static void check_function_type PARAMS ((tree, tree));
158991b7
KG
137static void destroy_local_var PARAMS ((tree));
138static void finish_constructor_body PARAMS ((void));
139static void finish_destructor_body PARAMS ((void));
140static tree create_array_type_for_decl PARAMS ((tree, tree, tree));
141static tree get_atexit_node PARAMS ((void));
142static tree get_dso_handle_node PARAMS ((void));
143static tree start_cleanup_fn PARAMS ((void));
144static void end_cleanup_fn PARAMS ((void));
2ce07e2d 145static tree cp_make_fname_decl PARAMS ((tree, const char *, int));
d43829f9 146static void initialize_predefined_identifiers PARAMS ((void));
5362b086 147static tree check_special_function_return_type
1f84ec23 148 PARAMS ((special_function_kind, tree, tree));
596ea4e5
AS
149static tree push_cp_library_fn PARAMS ((enum tree_code, tree));
150static tree build_cp_library_fn PARAMS ((tree, enum tree_code, tree));
f444e36b 151static void store_parm_decls PARAMS ((tree));
d363e7bf 152static int cp_missing_noreturn_ok_p PARAMS ((tree));
8d08fdba 153
69ac77ce 154#if defined (DEBUG_CP_BINDING_LEVELS)
158991b7 155static void indent PARAMS ((void));
69ac77ce
JL
156#endif
157
8d08fdba
MS
158/* Erroneous argument lists can use this *IFF* they do not modify it. */
159tree error_mark_list;
160
7f4edbcb 161/* The following symbols are subsumed in the cp_global_trees array, and
68642fb6 162 listed here individually for documentation purposes.
8d08fdba 163
7f4edbcb
BS
164 C++ extensions
165 tree wchar_decl_node;
37c46b43 166
7f4edbcb
BS
167 tree vtable_entry_type;
168 tree delta_type_node;
169#if 0
170 Old rtti stuff.
171 tree __baselist_desc_type_node;
172 tree __i_desc_type_node, __m_desc_type_node;
173 tree __t_desc_array_type, __i_desc_array_type, __m_desc_array_type;
946dc1c8 174#endif
7f4edbcb
BS
175 tree __t_desc_type_node;
176#if 0
177 tree __tp_desc_type_node;
178#endif
db1147b2 179 tree ti_desc_type_node;
1f4cb92b 180 tree bltn_desc_type_node, ptr_desc_type_node;
db1147b2
NS
181 tree ary_desc_type_node, func_desc_type_node, enum_desc_type_node;
182 tree class_desc_type_node, si_class_desc_type_node, vmi_class_desc_type_node;
387769ed 183 tree ptm_desc_type_node;
db1147b2 184 tree base_desc_type_node;
7f4edbcb
BS
185#if 0
186 Not needed yet? May be needed one day?
187 tree __bltn_desc_array_type, __user_desc_array_type, __class_desc_array_type;
188 tree __ptr_desc_array_type, __attr_dec_array_type, __func_desc_array_type;
189 tree __ptmf_desc_array_type, __ptmd_desc_array_type;
946dc1c8 190#endif
8d08fdba 191
7f4edbcb
BS
192 tree class_type_node, record_type_node, union_type_node, enum_type_node;
193 tree unknown_type_node;
8d08fdba 194
7f4edbcb 195 Array type `vtable_entry_type[]'
8d08fdba 196
7f4edbcb
BS
197 tree vtbl_type_node;
198 tree vtbl_ptr_type_node;
8d08fdba 199
2854d3c6 200 Namespaces,
8d08fdba 201
7f4edbcb 202 tree std_node;
2854d3c6 203 tree abi_node;
8d08fdba 204
7f4edbcb
BS
205 A FUNCTION_DECL which can call `abort'. Not necessarily the
206 one that the user will declare, but sufficient to be called
207 by routines that want to abort the program.
8d08fdba 208
7f4edbcb 209 tree abort_fndecl;
8d08fdba 210
7f4edbcb 211 The FUNCTION_DECL for the default `::operator delete'.
2986ae00 212
7f4edbcb 213 tree global_delete_fndecl;
8d08fdba 214
7f4edbcb 215 Used by RTTI
669ec2b4
JM
216 tree type_info_type_node, tinfo_decl_id, tinfo_decl_type;
217 tree tinfo_var_id;
218
7f4edbcb 219*/
8d08fdba 220
7f4edbcb 221tree cp_global_trees[CPTI_MAX];
8d08fdba 222
2c73f9f5 223/* Indicates that there is a type value in some namespace, although
7f4edbcb 224 that is not necessarily in scope at the moment. */
2c73f9f5
ML
225
226static tree global_type_node;
227
6bcedb4e
MM
228/* If non-zero, this is the number of times we have entered the `std'
229 namespace when we are treating that namespace as an alias for the
230 global namespace. */
231static int in_fake_std;
6633d636 232
3e3f722c
ML
233/* Expect only namespace names now. */
234static int only_namespace_names;
235
6625cdb5
JM
236/* Used only for jumps to as-yet undefined labels, since jumps to
237 defined labels can have their validity checked immediately. */
238
239struct named_label_use_list
e349ee73
MS
240{
241 struct binding_level *binding_level;
242 tree names_in_scope;
243 tree label_decl;
9c0758dd 244 const char *filename_o_goto;
e349ee73 245 int lineno_o_goto;
6625cdb5 246 struct named_label_use_list *next;
e349ee73
MS
247};
248
ed5511d9 249#define named_label_uses cp_function_chain->x_named_label_uses
8d08fdba 250
8e4ce833
JJ
251#define local_names cp_function_chain->x_local_names
252
8d08fdba
MS
253/* A list of objects which have constructors or destructors
254 which reside in the global scope. The decl is stored in
255 the TREE_VALUE slot and the initializer is stored
256 in the TREE_PURPOSE slot. */
257tree static_aggregates;
258
8d08fdba
MS
259/* -- end of C++ */
260
81b3411c 261/* A node for the integer constants 2, and 3. */
d11ad92e 262
81b3411c 263tree integer_two_node, integer_three_node;
8d08fdba 264
8d08fdba
MS
265/* Parsing a function declarator leaves here a chain of structure
266 and enum types declared in the parmlist. */
267
268static tree last_function_parm_tags;
269
8d08fdba 270/* Similar, for last_function_parm_tags. */
9cd64686 271tree last_function_parms;
8d08fdba
MS
272static tree current_function_parm_tags;
273
6625cdb5
JM
274/* A list of all LABEL_DECLs in the function that have names. Here so
275 we can clear out their names' definitions at the end of the
276 function, and so we can check the validity of jumps to these labels. */
277
278struct named_label_list
279{
280 struct binding_level *binding_level;
281 tree names_in_scope;
282 tree old_value;
283 tree label_decl;
284 tree bad_decls;
6625cdb5 285 struct named_label_list *next;
826840d9
RH
286 unsigned int in_try_scope : 1;
287 unsigned int in_catch_scope : 1;
6625cdb5 288};
8d08fdba 289
4519c0a8 290#define named_labels cp_function_chain->x_named_labels
8d08fdba 291
8d08fdba
MS
292/* Set to 0 at beginning of a function definition, and whenever
293 a label (case or named) is defined. Set to value of expression
294 returned from function when that value can be transformed into
295 a named return value. */
296
297tree current_function_return_value;
298
b8458e3e
JM
299/* Nonzero means use the ISO C94 dialect of C. */
300
301int flag_isoc94;
302
83309c26 303/* Nonzero means use the ISO C99 dialect of C. */
68642fb6 304
83309c26 305int flag_isoc99;
68642fb6 306
93e2382f
JM
307/* Nonzero means we are a hosted implementation for code shared with C. */
308
309int flag_hosted = 1;
310
311/* Nonzero means add default format_arg attributes for functions not
312 in ISO C. */
313
314int flag_noniso_default_format_attributes = 1;
315
8d08fdba
MS
316/* Nonzero if we want to conserve space in the .o files. We do this
317 by putting uninitialized data and runtime initialized data into
ddd5a7c1 318 .common instead of .data at the expense of not flagging multiple
8d08fdba
MS
319 definitions. */
320extern int flag_conserve_space;
8d08fdba 321\f
51c184be 322/* C and C++ flags are in decl2.c. */
8d08fdba 323
51c184be 324/* Flag used when debugging spew.c */
8d08fdba
MS
325
326extern int spew_debug;
327
e92cc029
MS
328/* A expression of value 0 with the same precision as a sizetype
329 node, but signed. */
330tree signed_size_zero_node;
331
0c8feefe
MM
332/* The name of the anonymous namespace, throughout this translation
333 unit. */
334tree anonymous_namespace_name;
335
b2244c65
MM
336/* The number of function bodies which we are currently processing.
337 (Zero if we are at namespace scope, one inside the body of a
338 function, two inside the body of a function in a local class, etc.) */
339int function_depth;
8d08fdba
MS
340\f
341/* For each binding contour we allocate a binding_level structure
e92cc029
MS
342 which records the names defined in that contour.
343 Contours include:
344 0) the global one
345 1) one for each function definition,
346 where internal declarations of the parameters appear.
347 2) one for each compound statement,
348 to record its declarations.
349
350 The current meaning of a name can be found by searching the levels
351 from the current one out to the global one.
352
353 Off to the side, may be the class_binding_level. This exists only
354 to catch class-local declarations. It is otherwise nonexistent.
355
356 Also there may be binding levels that catch cleanups that must be
d8f8dca1
MM
357 run when exceptions occur. Thus, to see whether a name is bound in
358 the current scope, it is not enough to look in the
359 CURRENT_BINDING_LEVEL. You should use lookup_name_current_level
360 instead. */
8d08fdba
MS
361
362/* Note that the information in the `names' component of the global contour
363 is duplicated in the IDENTIFIER_GLOBAL_VALUEs of all identifiers. */
364
365struct binding_level
366 {
367 /* A chain of _DECL nodes for all variables, constants, functions,
e92cc029 368 and typedef types. These are in the reverse of the order
f181d4ae
MM
369 supplied. There may be OVERLOADs on this list, too, but they
370 are wrapped in TREE_LISTs; the TREE_VALUE is the OVERLOAD. */
8d08fdba
MS
371 tree names;
372
e92cc029
MS
373 /* A list of structure, union and enum definitions, for looking up
374 tag names.
375 It is a chain of TREE_LIST nodes, each of whose TREE_PURPOSE is a name,
376 or NULL_TREE; and whose TREE_VALUE is a RECORD_TYPE, UNION_TYPE,
377 or ENUMERAL_TYPE node.
378
379 C++: the TREE_VALUE nodes can be simple types for
380 component_bindings. */
8d08fdba
MS
381 tree tags;
382
2c73f9f5
ML
383 /* A list of USING_DECL nodes. */
384 tree usings;
385
ea9635c7
ML
386 /* A list of used namespaces. PURPOSE is the namespace,
387 VALUE the common ancestor with this binding_level's namespace. */
388 tree using_directives;
389
f181d4ae
MM
390 /* If this binding level is the binding level for a class, then
391 class_shadowed is a TREE_LIST. The TREE_PURPOSE of each node
76191fdd 392 is the name of an entity bound in the class. The TREE_TYPE is
d8f8dca1 393 the DECL bound by this name in the class. */
8d08fdba
MS
394 tree class_shadowed;
395
f181d4ae 396 /* Similar to class_shadowed, but for IDENTIFIER_TYPE_VALUE, and
76191fdd
NS
397 is used for all binding levels. In addition the TREE_VALUE is the
398 IDENTIFIER_TYPE_VALUE before we entered the class. */
8d08fdba
MS
399 tree type_shadowed;
400
acef433b
MM
401 /* A TREE_LIST. Each TREE_VALUE is the LABEL_DECL for a local
402 label in this scope. The TREE_PURPOSE is the previous value of
403 the IDENTIFIER_LABEL VALUE. */
404 tree shadowed_labels;
405
8d08fdba
MS
406 /* For each level (except not the global one),
407 a chain of BLOCK nodes for all the levels
408 that were entered and exited one level down. */
409 tree blocks;
410
70adf8a9
JM
411 /* The _TYPE node for this level, if parm_flag == 2. */
412 tree this_class;
413
8d08fdba
MS
414 /* The binding level which this one is contained in (inherits from). */
415 struct binding_level *level_chain;
416
f30432d7 417 /* List of decls in `names' that have incomplete
8d08fdba 418 structure or union types. */
f30432d7 419 tree incomplete;
8d08fdba 420
8d6e462b 421 /* List of VAR_DECLS saved from a previous for statement.
cab1f180 422 These would be dead in ISO-conforming code, but might
f181d4ae
MM
423 be referenced in ARM-era code. These are stored in a
424 TREE_LIST; the TREE_VALUE is the actual declaration. */
8d6e462b
PB
425 tree dead_vars_from_for;
426
8d08fdba 427 /* 1 for the level that holds the parameters of a function.
74b846e0
MM
428 2 for the level that holds a class declaration. */
429 unsigned parm_flag : 2;
8d08fdba
MS
430
431 /* 1 means make a BLOCK for this level regardless of all else.
432 2 for temporary binding contours created by the compiler. */
74b846e0 433 unsigned keep : 2;
8d08fdba
MS
434
435 /* Nonzero if this level "doesn't exist" for tags. */
436 unsigned tag_transparent : 1;
437
438 /* Nonzero if this level can safely have additional
439 cleanup-needing variables added to it. */
440 unsigned more_cleanups_ok : 1;
441 unsigned have_cleanups : 1;
442
74b846e0 443 /* Nonzero if this scope is for storing the decls for template
5566b478
MS
444 parameters and generic decls; these decls will be discarded and
445 replaced with a TEMPLATE_DECL. */
74b846e0
MM
446 unsigned template_parms_p : 1;
447
5362b086 448 /* Nonzero if this scope corresponds to the `<>' in a
74b846e0
MM
449 `template <>' clause. Whenever this flag is set,
450 TEMPLATE_PARMS_P will be set as well. */
451 unsigned template_spec_p : 1;
8d08fdba 452
a9aedbc2
MS
453 /* This is set for a namespace binding level. */
454 unsigned namespace_p : 1;
455
2ee887f2 456 /* True if this level is that of a for-statement where we need to
cab1f180 457 worry about ambiguous (ARM or ISO) scope rules. */
8d6e462b
PB
458 unsigned is_for_scope : 1;
459
826840d9
RH
460 /* True if this level corresponds to a TRY block. Currently this
461 information is only available while building the tree structure. */
462 unsigned is_try_scope : 1;
227cf171 463
826840d9
RH
464 /* True if this level corresponds to a CATCH block. Currently this
465 information is only available while building the tree structure. */
466 unsigned is_catch_scope : 1;
467
468 /* Three bits left for this word. */
8d08fdba
MS
469
470#if defined(DEBUG_CP_BINDING_LEVELS)
471 /* Binding depth at which this level began. */
472 unsigned binding_depth;
473#endif /* defined(DEBUG_CP_BINDING_LEVELS) */
474 };
475
476#define NULL_BINDING_LEVEL ((struct binding_level *) NULL)
68642fb6 477
8f032717 478/* The binding level currently in effect. */
8d08fdba 479
a8f73d4b 480#define current_binding_level \
52a11cbf 481 (cfun && cp_function_chain->bindings \
d363e7bf 482 ? cp_function_chain->bindings \
a8f73d4b 483 : scope_chain->bindings)
8d08fdba
MS
484
485/* The binding level of the current class, if any. */
486
9cd64686 487#define class_binding_level scope_chain->class_bindings
8d08fdba 488
8d08fdba
MS
489/* A chain of binding_level structures awaiting reuse. */
490
491static struct binding_level *free_binding_level;
492
493/* The outermost binding level, for names of file scope.
494 This is created when the compiler is started and exists
495 through the entire run. */
496
497static struct binding_level *global_binding_level;
498
8d08fdba
MS
499/* Nonzero means unconditionally make a BLOCK for the next level pushed. */
500
501static int keep_next_level_flag;
502
503#if defined(DEBUG_CP_BINDING_LEVELS)
504static int binding_depth = 0;
505static int is_class_level = 0;
506
507static void
508indent ()
509{
510 register unsigned i;
511
512 for (i = 0; i < binding_depth*2; i++)
513 putc (' ', stderr);
514}
515#endif /* defined(DEBUG_CP_BINDING_LEVELS) */
516
158991b7 517static tree pushdecl_with_scope PARAMS ((tree, struct binding_level *));
8d08fdba
MS
518
519static void
520push_binding_level (newlevel, tag_transparent, keep)
521 struct binding_level *newlevel;
522 int tag_transparent, keep;
523{
524 /* Add this level to the front of the chain (stack) of levels that
525 are active. */
961192e1 526 memset ((char*) newlevel, 0, sizeof (struct binding_level));
8f032717 527 newlevel->level_chain = current_binding_level;
8d08fdba
MS
528 current_binding_level = newlevel;
529 newlevel->tag_transparent = tag_transparent;
530 newlevel->more_cleanups_ok = 1;
227cf171 531
8d08fdba
MS
532 newlevel->keep = keep;
533#if defined(DEBUG_CP_BINDING_LEVELS)
534 newlevel->binding_depth = binding_depth;
535 indent ();
536 fprintf (stderr, "push %s level 0x%08x line %d\n",
537 (is_class_level) ? "class" : "block", newlevel, lineno);
538 is_class_level = 0;
539 binding_depth++;
540#endif /* defined(DEBUG_CP_BINDING_LEVELS) */
541}
542
8f032717
MM
543/* Find the innermost enclosing class scope, and reset
544 CLASS_BINDING_LEVEL appropriately. */
545
546static void
547find_class_binding_level ()
548{
549 struct binding_level *level = current_binding_level;
550
551 while (level && level->parm_flag != 2)
552 level = level->level_chain;
553 if (level && level->parm_flag == 2)
554 class_binding_level = level;
555 else
556 class_binding_level = 0;
557}
558
8d08fdba
MS
559static void
560pop_binding_level ()
561{
8d08fdba
MS
562 if (global_binding_level)
563 {
2c73f9f5 564 /* Cannot pop a level, if there are none left to pop. */
8d08fdba
MS
565 if (current_binding_level == global_binding_level)
566 my_friendly_abort (123);
567 }
568 /* Pop the current level, and free the structure for reuse. */
569#if defined(DEBUG_CP_BINDING_LEVELS)
570 binding_depth--;
571 indent ();
572 fprintf (stderr, "pop %s level 0x%08x line %d\n",
573 (is_class_level) ? "class" : "block",
574 current_binding_level, lineno);
575 if (is_class_level != (current_binding_level == class_binding_level))
9e9ff709
MS
576 {
577 indent ();
578 fprintf (stderr, "XXX is_class_level != (current_binding_level == class_binding_level)\n");
579 }
8d08fdba
MS
580 is_class_level = 0;
581#endif /* defined(DEBUG_CP_BINDING_LEVELS) */
582 {
583 register struct binding_level *level = current_binding_level;
584 current_binding_level = current_binding_level->level_chain;
585 level->level_chain = free_binding_level;
586#if 0 /* defined(DEBUG_CP_BINDING_LEVELS) */
587 if (level->binding_depth != binding_depth)
588 abort ();
589#endif /* defined(DEBUG_CP_BINDING_LEVELS) */
8f032717
MM
590 free_binding_level = level;
591 find_class_binding_level ();
8d08fdba
MS
592 }
593}
a9aedbc2
MS
594
595static void
596suspend_binding_level ()
597{
598 if (class_binding_level)
599 current_binding_level = class_binding_level;
600
601 if (global_binding_level)
602 {
2c73f9f5 603 /* Cannot suspend a level, if there are none left to suspend. */
a9aedbc2
MS
604 if (current_binding_level == global_binding_level)
605 my_friendly_abort (123);
606 }
607 /* Suspend the current level. */
608#if defined(DEBUG_CP_BINDING_LEVELS)
609 binding_depth--;
610 indent ();
611 fprintf (stderr, "suspend %s level 0x%08x line %d\n",
612 (is_class_level) ? "class" : "block",
613 current_binding_level, lineno);
614 if (is_class_level != (current_binding_level == class_binding_level))
9e9ff709
MS
615 {
616 indent ();
617 fprintf (stderr, "XXX is_class_level != (current_binding_level == class_binding_level)\n");
618 }
a9aedbc2
MS
619 is_class_level = 0;
620#endif /* defined(DEBUG_CP_BINDING_LEVELS) */
8f032717
MM
621 current_binding_level = current_binding_level->level_chain;
622 find_class_binding_level ();
a9aedbc2
MS
623}
624
824b9a4c 625static void
a9aedbc2
MS
626resume_binding_level (b)
627 struct binding_level *b;
628{
2c73f9f5
ML
629 /* Resuming binding levels is meant only for namespaces,
630 and those cannot nest into classes. */
631 my_friendly_assert(!class_binding_level, 386);
632 /* Also, resuming a non-directly nested namespace is a no-no. */
633 my_friendly_assert(b->level_chain == current_binding_level, 386);
a9aedbc2
MS
634 current_binding_level = b;
635#if defined(DEBUG_CP_BINDING_LEVELS)
636 b->binding_depth = binding_depth;
637 indent ();
638 fprintf (stderr, "resume %s level 0x%08x line %d\n",
639 (is_class_level) ? "class" : "block", b, lineno);
640 is_class_level = 0;
641 binding_depth++;
642#endif /* defined(DEBUG_CP_BINDING_LEVELS) */
643}
8d08fdba 644\f
cffa8729
MS
645/* Create a new `struct binding_level'. */
646
647static
648struct binding_level *
649make_binding_level ()
650{
651 /* NOSTRICT */
652 return (struct binding_level *) xmalloc (sizeof (struct binding_level));
653}
654
8d08fdba
MS
655/* Nonzero if we are currently in the global binding level. */
656
657int
658global_bindings_p ()
659{
660 return current_binding_level == global_binding_level;
661}
662
8f032717
MM
663/* Return the innermost binding level that is not for a class scope. */
664
665static struct binding_level *
666innermost_nonclass_level ()
667{
668 struct binding_level *b;
669
670 b = current_binding_level;
671 while (b->parm_flag == 2)
672 b = b->level_chain;
673
674 return b;
675}
676
a9aedbc2
MS
677/* Nonzero if we are currently in a toplevel binding level. This
678 means either the global binding level or a namespace in a toplevel
8f032717 679 binding level. Since there are no non-toplevel namespace levels,
74b846e0
MM
680 this really means any namespace or template parameter level. We
681 also include a class whose context is toplevel. */
a9aedbc2
MS
682
683int
684toplevel_bindings_p ()
685{
8f032717
MM
686 struct binding_level *b = innermost_nonclass_level ();
687
74b846e0 688 return b->namespace_p || b->template_parms_p;
a9aedbc2
MS
689}
690
8f032717
MM
691/* Nonzero if this is a namespace scope, or if we are defining a class
692 which is itself at namespace scope, or whose enclosing class is
693 such a class, etc. */
a9aedbc2 694
7bdbfa05 695int
a9aedbc2
MS
696namespace_bindings_p ()
697{
8f032717
MM
698 struct binding_level *b = innermost_nonclass_level ();
699
700 return b->namespace_p;
a9aedbc2
MS
701}
702
f1dedc31
MM
703/* If KEEP is non-zero, make a BLOCK node for the next binding level,
704 unconditionally. Otherwise, use the normal logic to decide whether
705 or not to create a BLOCK. */
706
8d08fdba 707void
f1dedc31
MM
708keep_next_level (keep)
709 int keep;
8d08fdba 710{
f1dedc31 711 keep_next_level_flag = keep;
8d08fdba
MS
712}
713
714/* Nonzero if the current level needs to have a BLOCK made. */
715
716int
717kept_level_p ()
718{
719 return (current_binding_level->blocks != NULL_TREE
720 || current_binding_level->keep
721 || current_binding_level->names != NULL_TREE
722 || (current_binding_level->tags != NULL_TREE
723 && !current_binding_level->tag_transparent));
724}
725
824b9a4c 726static void
a9aedbc2
MS
727declare_namespace_level ()
728{
729 current_binding_level->namespace_p = 1;
730}
731
74b846e0
MM
732/* Returns non-zero if this scope was created to store template
733 parameters. */
734
8d08fdba 735int
74b846e0 736template_parm_scope_p ()
8d08fdba 737{
74b846e0
MM
738 return current_binding_level->template_parms_p;
739}
740
741/* Returns the kind of template specialization we are currently
742 processing, given that it's declaration contained N_CLASS_SCOPES
743 explicit scope qualifications. */
744
745tmpl_spec_kind
746current_tmpl_spec_kind (n_class_scopes)
747 int n_class_scopes;
748{
749 int n_template_parm_scopes = 0;
750 int seen_specialization_p = 0;
751 int innermost_specialization_p = 0;
752 struct binding_level *b;
753
754 /* Scan through the template parameter scopes. */
755 for (b = current_binding_level; b->template_parms_p; b = b->level_chain)
756 {
757 /* If we see a specialization scope inside a parameter scope,
758 then something is wrong. That corresponds to a declaration
759 like:
760
761 template <class T> template <> ...
762
763 which is always illegal since [temp.expl.spec] forbids the
764 specialization of a class member template if the enclosing
765 class templates are not explicitly specialized as well. */
766 if (b->template_spec_p)
767 {
768 if (n_template_parm_scopes == 0)
769 innermost_specialization_p = 1;
770 else
771 seen_specialization_p = 1;
772 }
773 else if (seen_specialization_p == 1)
774 return tsk_invalid_member_spec;
775
776 ++n_template_parm_scopes;
777 }
778
779 /* Handle explicit instantiations. */
780 if (processing_explicit_instantiation)
781 {
782 if (n_template_parm_scopes != 0)
783 /* We've seen a template parameter list during an explicit
784 instantiation. For example:
785
786 template <class T> template void f(int);
787
788 This is erroneous. */
789 return tsk_invalid_expl_inst;
790 else
791 return tsk_expl_inst;
792 }
793
794 if (n_template_parm_scopes < n_class_scopes)
795 /* We've not seen enough template headers to match all the
796 specialized classes present. For example:
797
798 template <class T> void R<T>::S<T>::f(int);
799
800 This is illegal; there needs to be one set of template
801 parameters for each class. */
802 return tsk_insufficient_parms;
803 else if (n_template_parm_scopes == n_class_scopes)
804 /* We're processing a non-template declaration (even though it may
805 be a member of a template class.) For example:
806
807 template <class T> void S<T>::f(int);
808
809 The `class T' maches the `S<T>', leaving no template headers
810 corresponding to the `f'. */
811 return tsk_none;
812 else if (n_template_parm_scopes > n_class_scopes + 1)
813 /* We've got too many template headers. For example:
814
815 template <> template <class T> void f (T);
816
817 There need to be more enclosing classes. */
818 return tsk_excessive_parms;
819 else
820 /* This must be a template. It's of the form:
821
822 template <class T> template <class U> void S<T>::f(U);
823
824 This is a specialization if the innermost level was a
825 specialization; otherwise it's just a definition of the
826 template. */
827 return innermost_specialization_p ? tsk_expl_spec : tsk_template;
8d08fdba
MS
828}
829
830void
831set_class_shadows (shadows)
832 tree shadows;
833{
834 class_binding_level->class_shadowed = shadows;
835}
836
837/* Enter a new binding level.
838 If TAG_TRANSPARENT is nonzero, do so only for the name space of variables,
839 not for that of tags. */
840
841void
842pushlevel (tag_transparent)
843 int tag_transparent;
844{
b35d4555 845 struct binding_level *newlevel;
8d08fdba 846
01d939e8 847 if (cfun && !doing_semantic_analysis_p ())
b35d4555 848 return;
8d08fdba 849
b35d4555 850 /* Reuse or create a struct for this binding level. */
8d08fdba
MS
851#if defined(DEBUG_CP_BINDING_LEVELS)
852 if (0)
853#else /* !defined(DEBUG_CP_BINDING_LEVELS) */
854 if (free_binding_level)
855#endif /* !defined(DEBUG_CP_BINDING_LEVELS) */
856 {
857 newlevel = free_binding_level;
858 free_binding_level = free_binding_level->level_chain;
859 }
860 else
acef433b 861 newlevel = make_binding_level ();
cffa8729 862
8d08fdba
MS
863 push_binding_level (newlevel, tag_transparent, keep_next_level_flag);
864 GNU_xref_start_scope ((HOST_WIDE_INT) newlevel);
865 keep_next_level_flag = 0;
866}
867
bd0d5d4a
JM
868/* We're defining an object of type TYPE. If it needs a cleanup, but
869 we're not allowed to add any more objects with cleanups to the current
870 scope, create a new binding level. */
871
872void
873maybe_push_cleanup_level (type)
874 tree type;
875{
876 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
877 && current_binding_level->more_cleanups_ok == 0)
878 {
879 keep_next_level (2);
880 pushlevel (1);
881 clear_last_expr ();
882 add_scope_stmt (/*begin_p=*/1, /*partial_p=*/1);
883 }
884}
885
74b846e0
MM
886/* Enter a new scope. The KIND indicates what kind of scope is being
887 created. */
888
889void
890begin_scope (sk)
891 scope_kind sk;
892{
893 pushlevel (0);
894
895 switch (sk)
896 {
897 case sk_template_spec:
898 current_binding_level->template_spec_p = 1;
899 /* Fall through. */
900
901 case sk_template_parms:
902 current_binding_level->template_parms_p = 1;
903 break;
904
905 default:
906 my_friendly_abort (20000309);
907 }
908}
909
910/* Exit the current scope. */
911
912void
913finish_scope ()
914{
915 poplevel (0, 0, 0);
916}
917
5566b478 918void
8d6e462b
PB
919note_level_for_for ()
920{
921 current_binding_level->is_for_scope = 1;
922}
923
6625cdb5
JM
924/* Record that the current binding level represents a try block. */
925
926void
826840d9 927note_level_for_try ()
6625cdb5 928{
826840d9
RH
929 current_binding_level->is_try_scope = 1;
930}
931
932/* Record that the current binding level represents a catch block. */
933
934void
935note_level_for_catch ()
936{
937 current_binding_level->is_catch_scope = 1;
6625cdb5
JM
938}
939
f181d4ae
MM
940/* For a binding between a name and an entity at a block scope,
941 this is the `struct binding_level' for the block. */
942#define BINDING_LEVEL(NODE) \
c7a932b1 943 (((struct tree_binding*)NODE)->scope.level)
f181d4ae 944
4890c2f4
MM
945/* A free list of CPLUS_BINDING nodes, connected by their
946 TREE_CHAINs. */
947
948static tree free_bindings;
949
f181d4ae
MM
950/* Make DECL the innermost binding for ID. The LEVEL is the binding
951 level at which this declaration is being bound. */
952
953static void
954push_binding (id, decl, level)
955 tree id;
956 tree decl;
957 struct binding_level* level;
958{
959 tree binding;
960
4890c2f4
MM
961 if (free_bindings)
962 {
963 binding = free_bindings;
964 free_bindings = TREE_CHAIN (binding);
965 }
966 else
967 binding = make_node (CPLUS_BINDING);
f181d4ae
MM
968
969 /* Now, fill in the binding information. */
970 BINDING_VALUE (binding) = decl;
d8f8dca1 971 BINDING_TYPE (binding) = NULL_TREE;
f181d4ae 972 BINDING_LEVEL (binding) = level;
8f032717 973 INHERITED_VALUE_BINDING_P (binding) = 0;
f181d4ae 974 LOCAL_BINDING_P (binding) = (level != class_binding_level);
87e3dbc9 975 BINDING_HAS_LEVEL_P (binding) = 1;
f181d4ae 976
c45df9c1 977 /* And put it on the front of the list of bindings for ID. */
f181d4ae
MM
978 TREE_CHAIN (binding) = IDENTIFIER_BINDING (id);
979 IDENTIFIER_BINDING (id) = binding;
980}
981
d8f8dca1
MM
982/* ID is already bound in the current scope. But, DECL is an
983 additional binding for ID in the same scope. This is the `struct
984 stat' hack whereby a non-typedef class-name or enum-name can be
985 bound at the same level as some other kind of entity. It's the
986 responsibility of the caller to check that inserting this name is
8f032717
MM
987 legal here. Returns nonzero if the new binding was successful. */
988static int
d8f8dca1
MM
989add_binding (id, decl)
990 tree id;
991 tree decl;
992{
993 tree binding = IDENTIFIER_BINDING (id);
8f032717 994 int ok = 1;
d8f8dca1
MM
995
996 if (TREE_CODE (decl) == TYPE_DECL && DECL_ARTIFICIAL (decl))
997 /* The new name is the type name. */
998 BINDING_TYPE (binding) = decl;
8f032717
MM
999 else if (!BINDING_VALUE (binding))
1000 /* This situation arises when push_class_level_binding moves an
1001 inherited type-binding out of the way to make room for a new
1002 value binding. */
1003 BINDING_VALUE (binding) = decl;
1004 else if (TREE_CODE (BINDING_VALUE (binding)) == TYPE_DECL
1005 && DECL_ARTIFICIAL (BINDING_VALUE (binding)))
1006 {
1007 /* The old binding was a type name. It was placed in
1008 BINDING_VALUE because it was thought, at the point it was
1009 declared, to be the only entity with such a name. Move the
1010 type name into the type slot; it is now hidden by the new
1011 binding. */
d8f8dca1
MM
1012 BINDING_TYPE (binding) = BINDING_VALUE (binding);
1013 BINDING_VALUE (binding) = decl;
8f032717 1014 INHERITED_VALUE_BINDING_P (binding) = 0;
d8f8dca1 1015 }
263505af
MM
1016 else if (TREE_CODE (BINDING_VALUE (binding)) == TYPE_DECL
1017 && TREE_CODE (decl) == TYPE_DECL
1018 && DECL_NAME (decl) == DECL_NAME (BINDING_VALUE (binding))
1019 && same_type_p (TREE_TYPE (decl),
1020 TREE_TYPE (BINDING_VALUE (binding))))
1021 /* We have two typedef-names, both naming the same type to have
1022 the same name. This is OK because of:
1023
1024 [dcl.typedef]
1025
1026 In a given scope, a typedef specifier can be used to redefine
1027 the name of any type declared in that scope to refer to the
1028 type to which it already refers. */
1029 ok = 0;
7b176381
MM
1030 /* There can be two block-scope declarations of the same variable,
1031 so long as they are `extern' declarations. */
1032 else if (TREE_CODE (decl) == VAR_DECL
1033 && TREE_CODE (BINDING_VALUE (binding)) == VAR_DECL
1034 && DECL_EXTERNAL (decl)
1035 && DECL_EXTERNAL (BINDING_VALUE (binding)))
1036 {
1037 duplicate_decls (decl, BINDING_VALUE (binding));
1038 ok = 0;
1039 }
8f032717
MM
1040 else
1041 {
1042 cp_error ("declaration of `%#D'", decl);
1043 cp_error_at ("conflicts with previous declaration `%#D'",
1044 BINDING_VALUE (binding));
1045 ok = 0;
1046 }
1047
1048 return ok;
d8f8dca1
MM
1049}
1050
cfe507be
MM
1051/* Add DECL to the list of things declared in B. */
1052
1053static void
1054add_decl_to_level (decl, b)
1055 tree decl;
1056 struct binding_level *b;
1057{
cfe507be
MM
1058 /* We build up the list in reverse order, and reverse it later if
1059 necessary. */
1060 TREE_CHAIN (decl) = b->names;
1061 b->names = decl;
1062}
1063
1064/* Bind DECL to ID in the current_binding_level, assumed to be a local
1065 binding level. If PUSH_USING is set in FLAGS, we know that DECL
1066 doesn't really belong to this binding level, that it got here
1067 through a using-declaration. */
f181d4ae
MM
1068
1069void
0034cf72 1070push_local_binding (id, decl, flags)
f181d4ae
MM
1071 tree id;
1072 tree decl;
0034cf72 1073 int flags;
f181d4ae 1074{
8f032717
MM
1075 struct binding_level *b;
1076
1077 /* Skip over any local classes. This makes sense if we call
1078 push_local_binding with a friend decl of a local class. */
1079 b = current_binding_level;
1080 while (b->parm_flag == 2)
1081 b = b->level_chain;
f181d4ae 1082
d8f8dca1 1083 if (lookup_name_current_level (id))
8f032717
MM
1084 {
1085 /* Supplement the existing binding. */
1086 if (!add_binding (id, decl))
1087 /* It didn't work. Something else must be bound at this
1088 level. Do not add DECL to the list of things to pop
1089 later. */
1090 return;
1091 }
d8f8dca1
MM
1092 else
1093 /* Create a new binding. */
8f032717 1094 push_binding (id, decl, b);
f181d4ae 1095
0034cf72 1096 if (TREE_CODE (decl) == OVERLOAD || (flags & PUSH_USING))
a06d48ef
JM
1097 /* We must put the OVERLOAD into a TREE_LIST since the
1098 TREE_CHAIN of an OVERLOAD is already used. Similarly for
1099 decls that got here through a using-declaration. */
1100 decl = build_tree_list (NULL_TREE, decl);
1101
f181d4ae
MM
1102 /* And put DECL on the list of things declared by the current
1103 binding level. */
cfe507be 1104 add_decl_to_level (decl, b);
f181d4ae
MM
1105}
1106
8f032717
MM
1107/* Bind DECL to ID in the class_binding_level. Returns nonzero if the
1108 binding was successful. */
f181d4ae 1109
8f032717 1110int
f181d4ae
MM
1111push_class_binding (id, decl)
1112 tree id;
1113 tree decl;
1114{
8f032717
MM
1115 int result = 1;
1116 tree binding = IDENTIFIER_BINDING (id);
1117 tree context;
1118
1119 /* Note that we declared this value so that we can issue an error if
1120 this an illegal redeclaration of a name already used for some
1121 other purpose. */
1122 note_name_declared_in_class (id, decl);
1123
1124 if (binding && BINDING_LEVEL (binding) == class_binding_level)
d8f8dca1 1125 /* Supplement the existing binding. */
8f032717 1126 result = add_binding (id, decl);
d8f8dca1
MM
1127 else
1128 /* Create a new binding. */
1129 push_binding (id, decl, class_binding_level);
1130
1131 /* Update the IDENTIFIER_CLASS_VALUE for this ID to be the
1132 class-level declaration. Note that we do not use DECL here
1133 because of the possibility of the `struct stat' hack; if DECL is
1134 a class-name or enum-name we might prefer a field-name, or some
1135 such. */
1136 IDENTIFIER_CLASS_VALUE (id) = BINDING_VALUE (IDENTIFIER_BINDING (id));
8f032717
MM
1137
1138 /* If this is a binding from a base class, mark it as such. */
1139 binding = IDENTIFIER_BINDING (id);
1140 if (BINDING_VALUE (binding) == decl && TREE_CODE (decl) != TREE_LIST)
1141 {
280f9385
MM
1142 /* Any implicit typename must be from a base-class. The
1143 context for an implicit typename declaration is always
1144 the derived class in which the lookup was done, so the checks
1145 based on the context of DECL below will not trigger. */
83233dca 1146 if (IMPLICIT_TYPENAME_TYPE_DECL_P (decl))
280f9385 1147 INHERITED_VALUE_BINDING_P (binding) = 1;
8f032717
MM
1148 else
1149 {
280f9385 1150 if (TREE_CODE (decl) == OVERLOAD)
4f1c5b7d 1151 context = CP_DECL_CONTEXT (OVL_CURRENT (decl));
280f9385
MM
1152 else
1153 {
2f939d94 1154 my_friendly_assert (DECL_P (decl), 0);
55de1b66 1155 context = context_for_name_lookup (decl);
280f9385 1156 }
8f032717 1157
280f9385
MM
1158 if (is_properly_derived_from (current_class_type, context))
1159 INHERITED_VALUE_BINDING_P (binding) = 1;
1160 else
1161 INHERITED_VALUE_BINDING_P (binding) = 0;
1162 }
8f032717
MM
1163 }
1164 else if (BINDING_VALUE (binding) == decl)
1165 /* We only encounter a TREE_LIST when push_class_decls detects an
1166 ambiguity. Such an ambiguity can be overridden by a definition
1167 in this class. */
1168 INHERITED_VALUE_BINDING_P (binding) = 1;
1169
1170 return result;
f181d4ae
MM
1171}
1172
d8f8dca1
MM
1173/* Remove the binding for DECL which should be the innermost binding
1174 for ID. */
f181d4ae 1175
68642fb6
UD
1176static void
1177pop_binding (id, decl)
f181d4ae 1178 tree id;
d8f8dca1 1179 tree decl;
f181d4ae
MM
1180{
1181 tree binding;
68642fb6 1182
f181d4ae
MM
1183 if (id == NULL_TREE)
1184 /* It's easiest to write the loops that call this function without
1185 checking whether or not the entities involved have names. We
1186 get here for such an entity. */
1187 return;
1188
d8f8dca1 1189 /* Get the innermost binding for ID. */
f181d4ae 1190 binding = IDENTIFIER_BINDING (id);
f181d4ae 1191
d8f8dca1
MM
1192 /* The name should be bound. */
1193 my_friendly_assert (binding != NULL_TREE, 0);
1194
1195 /* The DECL will be either the ordinary binding or the type
1196 binding for this identifier. Remove that binding. */
1197 if (BINDING_VALUE (binding) == decl)
1198 BINDING_VALUE (binding) = NULL_TREE;
1199 else if (BINDING_TYPE (binding) == decl)
1200 BINDING_TYPE (binding) = NULL_TREE;
1201 else
1202 my_friendly_abort (0);
1203
1204 if (!BINDING_VALUE (binding) && !BINDING_TYPE (binding))
4890c2f4
MM
1205 {
1206 /* We're completely done with the innermost binding for this
1207 identifier. Unhook it from the list of bindings. */
1208 IDENTIFIER_BINDING (id) = TREE_CHAIN (binding);
1209
1210 /* Add it to the free list. */
1211 TREE_CHAIN (binding) = free_bindings;
1212 free_bindings = binding;
1213
1214 /* Clear the BINDING_LEVEL so the garbage collector doesn't walk
1215 it. */
1216 BINDING_LEVEL (binding) = NULL;
1217 }
f181d4ae
MM
1218}
1219
acef433b
MM
1220/* When a label goes out of scope, check to see if that label was used
1221 in a valid manner, and issue any appropriate warnings or errors. */
1222
1223static void
6625cdb5
JM
1224pop_label (label, old_value)
1225 tree label;
1226 tree old_value;
acef433b 1227{
88848bde 1228 if (!processing_template_decl && doing_semantic_analysis_p ())
acef433b 1229 {
88848bde
MM
1230 if (DECL_INITIAL (label) == NULL_TREE)
1231 {
1232 cp_error_at ("label `%D' used but not defined", label);
1233 /* Avoid crashing later. */
1234 define_label (input_filename, 1, DECL_NAME (label));
1235 }
078721e1 1236 else if (warn_unused_label && !TREE_USED (label))
88848bde 1237 cp_warning_at ("label `%D' defined but not used", label);
acef433b 1238 }
acef433b 1239
6625cdb5 1240 SET_IDENTIFIER_LABEL_VALUE (DECL_NAME (label), old_value);
acef433b
MM
1241}
1242
0811ea8f 1243/* At the end of a function, all labels declared within the function
68642fb6 1244 go out of scope. BLOCK is the top-level block for the
acef433b
MM
1245 function. */
1246
1247static void
1248pop_labels (block)
1249 tree block;
1250{
6625cdb5 1251 struct named_label_list *link;
acef433b
MM
1252
1253 /* Clear out the definitions of all label names, since their scopes
1254 end here. */
6625cdb5 1255 for (link = named_labels; link; link = link->next)
acef433b 1256 {
6625cdb5 1257 pop_label (link->label_decl, link->old_value);
acef433b
MM
1258 /* Put the labels into the "variables" of the top-level block,
1259 so debugger can see them. */
6625cdb5
JM
1260 TREE_CHAIN (link->label_decl) = BLOCK_VARS (block);
1261 BLOCK_VARS (block) = link->label_decl;
acef433b
MM
1262 }
1263
6625cdb5 1264 named_labels = NULL;
acef433b
MM
1265}
1266
8d08fdba
MS
1267/* Exit a binding level.
1268 Pop the level off, and restore the state of the identifier-decl mappings
1269 that were in effect when this level was entered.
1270
1271 If KEEP == 1, this level had explicit declarations, so
1272 and create a "block" (a BLOCK node) for the level
1273 to record its declarations and subblocks for symbol table output.
1274
8d08fdba
MS
1275 If FUNCTIONBODY is nonzero, this level is the body of a function,
1276 so create a block as if KEEP were set and also clear out all
1277 label names.
1278
1279 If REVERSE is nonzero, reverse the order of decls before putting
1280 them into the BLOCK. */
1281
1282tree
1283poplevel (keep, reverse, functionbody)
1284 int keep;
1285 int reverse;
1286 int functionbody;
1287{
1288 register tree link;
1289 /* The chain of decls was accumulated in reverse order.
1290 Put it into forward order, just for cleanliness. */
1291 tree decls;
1292 int tmp = functionbody;
b35d4555
MM
1293 int real_functionbody;
1294 tree tags;
1295 tree subblocks;
8d08fdba
MS
1296 tree block = NULL_TREE;
1297 tree decl;
f181d4ae
MM
1298 int leaving_for_scope;
1299
01d939e8 1300 if (cfun && !doing_semantic_analysis_p ())
b35d4555
MM
1301 return NULL_TREE;
1302
273a708f
MM
1303 my_friendly_assert (current_binding_level->parm_flag != 2,
1304 19990916);
b74a0560 1305
b35d4555
MM
1306 real_functionbody = (current_binding_level->keep == 2
1307 ? ((functionbody = 0), tmp) : functionbody);
1308 tags = functionbody >= 0 ? current_binding_level->tags : 0;
1309 subblocks = functionbody >= 0 ? current_binding_level->blocks : 0;
1310
b74a0560
MM
1311 my_friendly_assert (!current_binding_level->class_shadowed,
1312 19990414);
8d08fdba 1313
536333d4
MM
1314 /* We used to use KEEP == 2 to indicate that the new block should go
1315 at the beginning of the list of blocks at this binding level,
1316 rather than the end. This hack is no longer used. */
1317 my_friendly_assert (keep == 0 || keep == 1, 0);
1318
8d08fdba
MS
1319 GNU_xref_end_scope ((HOST_WIDE_INT) current_binding_level,
1320 (HOST_WIDE_INT) current_binding_level->level_chain,
1321 current_binding_level->parm_flag,
5566b478 1322 current_binding_level->keep);
8d08fdba
MS
1323
1324 if (current_binding_level->keep == 1)
1325 keep = 1;
1326
6625cdb5
JM
1327 /* Any uses of undefined labels, and any defined labels, now operate
1328 under constraints of next binding contour. */
1329 if (cfun && !functionbody)
1330 {
1331 struct binding_level *level_chain;
1332 level_chain = current_binding_level->level_chain;
1333 if (level_chain)
1334 {
1335 struct named_label_use_list *uses;
1336 struct named_label_list *labels;
1337 for (labels = named_labels; labels; labels = labels->next)
1338 if (labels->binding_level == current_binding_level)
1339 {
1340 tree decl;
826840d9
RH
1341 if (current_binding_level->is_try_scope)
1342 labels->in_try_scope = 1;
1343 if (current_binding_level->is_catch_scope)
1344 labels->in_catch_scope = 1;
6625cdb5
JM
1345 for (decl = labels->names_in_scope; decl;
1346 decl = TREE_CHAIN (decl))
1347 if (decl_jump_unsafe (decl))
1348 labels->bad_decls = tree_cons (NULL_TREE, decl,
1349 labels->bad_decls);
1350 labels->binding_level = level_chain;
1351 labels->names_in_scope = level_chain->names;
1352 }
1353
1354 for (uses = named_label_uses; uses; uses = uses->next)
1355 if (uses->binding_level == current_binding_level)
1356 {
1357 uses->binding_level = level_chain;
1358 uses->names_in_scope = level_chain->names;
1359 }
1360 }
1361 }
1362
8d08fdba
MS
1363 /* Get the decls in the order they were written.
1364 Usually current_binding_level->names is in reverse order.
1365 But parameter decls were previously put in forward order. */
1366
1367 if (reverse)
1368 current_binding_level->names
1369 = decls = nreverse (current_binding_level->names);
1370 else
1371 decls = current_binding_level->names;
1372
1373 /* Output any nested inline functions within this block
1374 if they weren't already output. */
8d08fdba
MS
1375 for (decl = decls; decl; decl = TREE_CHAIN (decl))
1376 if (TREE_CODE (decl) == FUNCTION_DECL
1377 && ! TREE_ASM_WRITTEN (decl)
1378 && DECL_INITIAL (decl) != NULL_TREE
6060a796
MS
1379 && TREE_ADDRESSABLE (decl)
1380 && decl_function_context (decl) == current_function_decl)
8d08fdba
MS
1381 {
1382 /* If this decl was copied from a file-scope decl
1383 on account of a block-scope extern decl,
1384 propagate TREE_ADDRESSABLE to the file-scope decl. */
1385 if (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE)
1386 TREE_ADDRESSABLE (DECL_ABSTRACT_ORIGIN (decl)) = 1;
1387 else
1388 {
1389 push_function_context ();
1390 output_inline_function (decl);
1391 pop_function_context ();
1392 }
1393 }
1394
d9b2d9da
MM
1395 /* When not in function-at-a-time mode, expand_end_bindings will
1396 warn about unused variables. But, in function-at-a-time mode
1397 expand_end_bindings is not passed the list of variables in the
1398 current scope, and therefore no warning is emitted. So, we
1399 explicitly warn here. */
1400 if (!processing_template_decl)
1401 warn_about_unused_variables (getdecls ());
1402
8d08fdba
MS
1403 /* If there were any declarations or structure tags in that level,
1404 or if this level is a function body,
1405 create a BLOCK to record them for the life of this function. */
8d08fdba 1406 block = NULL_TREE;
f444e36b 1407 if (keep == 1 || functionbody)
8d08fdba
MS
1408 block = make_node (BLOCK);
1409 if (block != NULL_TREE)
1410 {
f444e36b
MM
1411 BLOCK_VARS (block) = decls;
1412 BLOCK_SUBBLOCKS (block) = subblocks;
8d08fdba
MS
1413 }
1414
1415 /* In each subblock, record that this is its superior. */
8d08fdba
MS
1416 if (keep >= 0)
1417 for (link = subblocks; link; link = TREE_CHAIN (link))
1418 BLOCK_SUPERCONTEXT (link) = block;
1419
f181d4ae
MM
1420 /* We still support the old for-scope rules, whereby the variables
1421 in a for-init statement were in scope after the for-statement
1422 ended. We only use the new rules in flag_new_for_scope is
1423 nonzero. */
68642fb6 1424 leaving_for_scope
f181d4ae
MM
1425 = current_binding_level->is_for_scope && flag_new_for_scope == 1;
1426
1427 /* Remove declarations for all the DECLs in this level. */
1428 for (link = decls; link; link = TREE_CHAIN (link))
1429 {
c3783399
NS
1430 if (leaving_for_scope && TREE_CODE (link) == VAR_DECL
1431 && DECL_NAME (link))
f181d4ae 1432 {
68642fb6 1433 tree outer_binding
f181d4ae
MM
1434 = TREE_CHAIN (IDENTIFIER_BINDING (DECL_NAME (link)));
1435 tree ns_binding;
1436
1437 if (!outer_binding)
1438 ns_binding = IDENTIFIER_NAMESPACE_VALUE (DECL_NAME (link));
c7dda1e3
MM
1439 else
1440 ns_binding = NULL_TREE;
1441
68642fb6
UD
1442 if (outer_binding
1443 && (BINDING_LEVEL (outer_binding)
f181d4ae
MM
1444 == current_binding_level->level_chain))
1445 /* We have something like:
68642fb6 1446
f181d4ae
MM
1447 int i;
1448 for (int i; ;);
68642fb6 1449
f181d4ae
MM
1450 and we are leaving the `for' scope. There's no reason to
1451 keep the binding of the inner `i' in this case. */
d8f8dca1 1452 pop_binding (DECL_NAME (link), link);
68642fb6
UD
1453 else if ((outer_binding
1454 && (TREE_CODE (BINDING_VALUE (outer_binding))
f181d4ae 1455 == TYPE_DECL))
68642fb6 1456 || (ns_binding
f181d4ae
MM
1457 && TREE_CODE (ns_binding) == TYPE_DECL))
1458 /* Here, we have something like:
1459
1460 typedef int I;
1461
1462 void f () {
1463 for (int I; ;);
1464 }
1465
1466 We must pop the for-scope binding so we know what's a
1467 type and what isn't. */
d8f8dca1 1468 pop_binding (DECL_NAME (link), link);
e76a2646 1469 else
e76a2646 1470 {
f181d4ae
MM
1471 /* Mark this VAR_DECL as dead so that we can tell we left it
1472 there only for backward compatibility. */
1473 DECL_DEAD_FOR_LOCAL (link) = 1;
68642fb6 1474
f181d4ae
MM
1475 /* Keep track of what should of have happenned when we
1476 popped the binding. */
1477 if (outer_binding && BINDING_VALUE (outer_binding))
68642fb6 1478 DECL_SHADOWED_FOR_VAR (link)
f181d4ae
MM
1479 = BINDING_VALUE (outer_binding);
1480
1481 /* Add it to the list of dead variables in the next
1482 outermost binding to that we can remove these when we
1483 leave that binding. */
1484 current_binding_level->level_chain->dead_vars_from_for
1485 = tree_cons (NULL_TREE, link,
1486 current_binding_level->level_chain->
1487 dead_vars_from_for);
1488
1489 /* Although we don't pop the CPLUS_BINDING, we do clear
1490 its BINDING_LEVEL since the level is going away now. */
1491 BINDING_LEVEL (IDENTIFIER_BINDING (DECL_NAME (link)))
1492 = 0;
e76a2646 1493 }
2ee887f2 1494 }
68642fb6 1495 else
8d6e462b 1496 {
f181d4ae 1497 /* Remove the binding. */
0034cf72
JM
1498 decl = link;
1499 if (TREE_CODE (decl) == TREE_LIST)
1500 decl = TREE_VALUE (decl);
2f939d94 1501 if (DECL_P (decl))
0034cf72
JM
1502 pop_binding (DECL_NAME (decl), decl);
1503 else if (TREE_CODE (decl) == OVERLOAD)
1504 pop_binding (DECL_NAME (OVL_FUNCTION (decl)), decl);
68642fb6 1505 else
f181d4ae 1506 my_friendly_abort (0);
8d08fdba 1507 }
f181d4ae 1508 }
8d08fdba 1509
f181d4ae
MM
1510 /* Remove declarations for any `for' variables from inner scopes
1511 that we kept around. */
1512 for (link = current_binding_level->dead_vars_from_for;
1513 link; link = TREE_CHAIN (link))
d8f8dca1 1514 pop_binding (DECL_NAME (TREE_VALUE (link)), TREE_VALUE (link));
2ee887f2 1515
f181d4ae
MM
1516 /* Restore the IDENTIFIER_TYPE_VALUEs. */
1517 for (link = current_binding_level->type_shadowed;
1518 link; link = TREE_CHAIN (link))
1519 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (link), TREE_VALUE (link));
acef433b
MM
1520
1521 /* Restore the IDENTIFIER_LABEL_VALUEs for local labels. */
1522 for (link = current_binding_level->shadowed_labels;
68642fb6 1523 link;
acef433b 1524 link = TREE_CHAIN (link))
6625cdb5 1525 pop_label (TREE_VALUE (link), TREE_PURPOSE (link));
acef433b 1526
f181d4ae
MM
1527 /* There may be OVERLOADs (wrapped in TREE_LISTs) on the BLOCK_VARs
1528 list if a `using' declaration put them there. The debugging
1529 back-ends won't understand OVERLOAD, so we remove them here.
1530 Because the BLOCK_VARS are (temporarily) shared with
1531 CURRENT_BINDING_LEVEL->NAMES we must do this fixup after we have
1532 popped all the bindings. */
1533 if (block)
1534 {
1535 tree* d;
8d6e462b 1536
a06d48ef
JM
1537 for (d = &BLOCK_VARS (block); *d; )
1538 {
1539 if (TREE_CODE (*d) == TREE_LIST)
1540 *d = TREE_CHAIN (*d);
1541 else
1542 d = &TREE_CHAIN (*d);
1543 }
8d6e462b 1544 }
8d08fdba
MS
1545
1546 /* If the level being exited is the top level of a function,
1547 check over all the labels. */
8d08fdba
MS
1548 if (functionbody)
1549 {
acef433b
MM
1550 /* Since this is the top level block of a function, the vars are
1551 the function's parameters. Don't leave them in the BLOCK
1552 because they are found in the FUNCTION_DECL instead. */
8d08fdba 1553 BLOCK_VARS (block) = 0;
acef433b 1554 pop_labels (block);
8d08fdba
MS
1555 }
1556
8d08fdba
MS
1557 tmp = current_binding_level->keep;
1558
1559 pop_binding_level ();
1560 if (functionbody)
1561 DECL_INITIAL (current_function_decl) = block;
1562 else if (block)
f444e36b
MM
1563 current_binding_level->blocks
1564 = chainon (current_binding_level->blocks, block);
1565
8d08fdba
MS
1566 /* If we did not make a block for the level just exited,
1567 any blocks made for inner levels
1568 (since they cannot be recorded as subblocks in that level)
1569 must be carried forward so they will later become subblocks
1570 of something else. */
1571 else if (subblocks)
536333d4
MM
1572 current_binding_level->blocks
1573 = chainon (current_binding_level->blocks, subblocks);
8d08fdba 1574
d9b2d9da
MM
1575 /* Each and every BLOCK node created here in `poplevel' is important
1576 (e.g. for proper debugging information) so if we created one
1577 earlier, mark it as "used". */
1578 if (block)
1579 TREE_USED (block) = 1;
1580
8d08fdba 1581 /* Take care of compiler's internal binding structures. */
a4443a08 1582 if (tmp == 2)
8d08fdba 1583 {
d9b2d9da
MM
1584 tree scope_stmts;
1585
68642fb6 1586 scope_stmts
d9b2d9da 1587 = add_scope_stmt (/*begin_p=*/0, /*partial_p=*/1);
8d08fdba 1588 if (block)
d9b2d9da
MM
1589 {
1590 SCOPE_STMT_BLOCK (TREE_PURPOSE (scope_stmts)) = block;
1591 SCOPE_STMT_BLOCK (TREE_VALUE (scope_stmts)) = block;
1592 }
1593
8f471b0d 1594 block = poplevel (keep, reverse, functionbody);
8d08fdba
MS
1595 }
1596
8d08fdba
MS
1597 return block;
1598}
1599
1600/* Delete the node BLOCK from the current binding level.
1601 This is used for the block inside a stmt expr ({...})
1602 so that the block can be reinserted where appropriate. */
1603
1604void
1605delete_block (block)
1606 tree block;
1607{
1608 tree t;
1609 if (current_binding_level->blocks == block)
1610 current_binding_level->blocks = TREE_CHAIN (block);
1611 for (t = current_binding_level->blocks; t;)
1612 {
1613 if (TREE_CHAIN (t) == block)
1614 TREE_CHAIN (t) = TREE_CHAIN (block);
1615 else
1616 t = TREE_CHAIN (t);
1617 }
1618 TREE_CHAIN (block) = NULL_TREE;
1619 /* Clear TREE_USED which is always set by poplevel.
1620 The flag is set again if insert_block is called. */
1621 TREE_USED (block) = 0;
1622}
1623
1624/* Insert BLOCK at the end of the list of subblocks of the
1625 current binding level. This is used when a BIND_EXPR is expanded,
1626 to handle the BLOCK node inside the BIND_EXPR. */
1627
1628void
1629insert_block (block)
1630 tree block;
1631{
1632 TREE_USED (block) = 1;
1633 current_binding_level->blocks
1634 = chainon (current_binding_level->blocks, block);
1635}
1636
8d08fdba
MS
1637/* Set the BLOCK node for the innermost scope
1638 (the one we are currently in). */
1639
1640void
1641set_block (block)
f444e36b 1642 tree block ATTRIBUTE_UNUSED;
8d08fdba 1643{
f444e36b
MM
1644 /* The RTL expansion machinery requires us to provide this callback,
1645 but it is not applicable in function-at-a-time mode. */
1646 my_friendly_assert (cfun && !doing_semantic_analysis_p (), 20000911);
8d08fdba
MS
1647}
1648
1649/* Do a pushlevel for class declarations. */
e92cc029 1650
8d08fdba
MS
1651void
1652pushlevel_class ()
1653{
1654 register struct binding_level *newlevel;
1655
1656 /* Reuse or create a struct for this binding level. */
1657#if defined(DEBUG_CP_BINDING_LEVELS)
1658 if (0)
1659#else /* !defined(DEBUG_CP_BINDING_LEVELS) */
1660 if (free_binding_level)
1661#endif /* !defined(DEBUG_CP_BINDING_LEVELS) */
1662 {
1663 newlevel = free_binding_level;
1664 free_binding_level = free_binding_level->level_chain;
1665 }
1666 else
f181d4ae 1667 newlevel = make_binding_level ();
8d08fdba
MS
1668
1669#if defined(DEBUG_CP_BINDING_LEVELS)
1670 is_class_level = 1;
1671#endif /* defined(DEBUG_CP_BINDING_LEVELS) */
1672
1673 push_binding_level (newlevel, 0, 0);
1674
8d08fdba
MS
1675 class_binding_level = current_binding_level;
1676 class_binding_level->parm_flag = 2;
70adf8a9 1677 class_binding_level->this_class = current_class_type;
8d08fdba
MS
1678}
1679
b74a0560 1680/* ...and a poplevel for class declarations. */
e92cc029 1681
273a708f 1682void
b74a0560 1683poplevel_class ()
8d08fdba
MS
1684{
1685 register struct binding_level *level = class_binding_level;
8d08fdba
MS
1686 tree shadowed;
1687
1688 my_friendly_assert (level != 0, 354);
68642fb6 1689
8d08fdba 1690 /* If we're leaving a toplevel class, don't bother to do the setting
ddd5a7c1 1691 of IDENTIFIER_CLASS_VALUE to NULL_TREE, since first of all this slot
8d08fdba 1692 shouldn't even be used when current_class_type isn't set, and second,
ddd5a7c1 1693 if we don't touch it here, we're able to use the cache effect if the
8d08fdba 1694 next time we're entering a class scope, it is the same class. */
b74a0560 1695 if (current_class_depth != 1)
8f032717
MM
1696 {
1697 struct binding_level* b;
1698
1699 /* Clear out our IDENTIFIER_CLASS_VALUEs. */
1700 for (shadowed = level->class_shadowed;
1701 shadowed;
1702 shadowed = TREE_CHAIN (shadowed))
1703 IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (shadowed)) = NULL_TREE;
68642fb6 1704
8f032717
MM
1705 /* Find the next enclosing class, and recreate
1706 IDENTIFIER_CLASS_VALUEs appropriate for that class. */
1707 b = level->level_chain;
1708 while (b && b->parm_flag != 2)
1709 b = b->level_chain;
1710
1711 if (b)
68642fb6
UD
1712 for (shadowed = b->class_shadowed;
1713 shadowed;
8f032717
MM
1714 shadowed = TREE_CHAIN (shadowed))
1715 {
1716 tree t;
1717
1718 t = IDENTIFIER_BINDING (TREE_PURPOSE (shadowed));
1719 while (t && BINDING_LEVEL (t) != b)
1720 t = TREE_CHAIN (t);
68642fb6 1721
8f032717 1722 if (t)
68642fb6 1723 IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (shadowed))
8f032717
MM
1724 = BINDING_VALUE (t);
1725 }
1726 }
8d08fdba
MS
1727 else
1728 /* Remember to save what IDENTIFIER's were bound in this scope so we
1729 can recover from cache misses. */
e76a2646
MS
1730 {
1731 previous_class_type = current_class_type;
1732 previous_class_values = class_binding_level->class_shadowed;
1733 }
8d08fdba
MS
1734 for (shadowed = level->type_shadowed;
1735 shadowed;
1736 shadowed = TREE_CHAIN (shadowed))
2c73f9f5 1737 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (shadowed), TREE_VALUE (shadowed));
8d08fdba 1738
f181d4ae 1739 /* Remove the bindings for all of the class-level declarations. */
68642fb6
UD
1740 for (shadowed = level->class_shadowed;
1741 shadowed;
f181d4ae 1742 shadowed = TREE_CHAIN (shadowed))
d8f8dca1 1743 pop_binding (TREE_PURPOSE (shadowed), TREE_TYPE (shadowed));
f181d4ae 1744
8d08fdba
MS
1745 GNU_xref_end_scope ((HOST_WIDE_INT) class_binding_level,
1746 (HOST_WIDE_INT) class_binding_level->level_chain,
1747 class_binding_level->parm_flag,
5566b478 1748 class_binding_level->keep);
8d08fdba 1749
38e01259 1750 /* Now, pop out of the binding level which we created up in the
8d08fdba
MS
1751 `pushlevel_class' routine. */
1752#if defined(DEBUG_CP_BINDING_LEVELS)
1753 is_class_level = 1;
1754#endif /* defined(DEBUG_CP_BINDING_LEVELS) */
1755
1756 pop_binding_level ();
8d08fdba 1757}
8f032717
MM
1758
1759/* We are entering the scope of a class. Clear IDENTIFIER_CLASS_VALUE
1760 for any names in enclosing classes. */
1761
1762void
1763clear_identifier_class_values ()
1764{
1765 tree t;
1766
1767 if (!class_binding_level)
1768 return;
1769
1770 for (t = class_binding_level->class_shadowed;
1771 t;
1772 t = TREE_CHAIN (t))
1773 IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (t)) = NULL_TREE;
1774}
1775
0aafb128
MM
1776/* Returns non-zero if T is a virtual function table. */
1777
1778int
1779vtable_decl_p (t, data)
1780 tree t;
1781 void *data ATTRIBUTE_UNUSED;
1782{
1783 return (TREE_CODE (t) == VAR_DECL && DECL_VIRTUAL_P (t));
1784}
1785
1786/* Returns non-zero if T is a TYPE_DECL for a type with virtual
1787 functions. */
1788
1789int
1790vtype_decl_p (t, data)
1791 tree t;
1792 void *data ATTRIBUTE_UNUSED;
1793{
1794 return (TREE_CODE (t) == TYPE_DECL
34249844
NS
1795 && TREE_CODE (TREE_TYPE (t)) == RECORD_TYPE
1796 && TYPE_POLYMORPHIC_P (TREE_TYPE (t)));
0aafb128
MM
1797}
1798
f71f87f9
MM
1799/* Return the declarations that are members of the namespace NS. */
1800
1801tree
1802cp_namespace_decls (ns)
1803 tree ns;
1804{
1805 return NAMESPACE_LEVEL (ns)->names;
1806}
1807
0aafb128
MM
1808/* Walk all the namespaces contained NAMESPACE, including NAMESPACE
1809 itself, calling F for each. The DATA is passed to F as well. */
1810
1811static int
1812walk_namespaces_r (namespace, f, data)
1813 tree namespace;
1814 walk_namespaces_fn f;
1815 void *data;
1816{
1817 tree current;
1818 int result = 0;
1819
1820 result |= (*f) (namespace, data);
1821
f71f87f9 1822 for (current = cp_namespace_decls (namespace);
0aafb128
MM
1823 current;
1824 current = TREE_CHAIN (current))
1825 {
1826 if (TREE_CODE (current) != NAMESPACE_DECL
1827 || DECL_NAMESPACE_ALIAS (current))
1828 continue;
1829 if (!DECL_LANG_SPECIFIC (current))
1830 {
1831 /* Hmm. std. */
6bcedb4e 1832 my_friendly_assert (current == fake_std_node, 393);
0aafb128
MM
1833 continue;
1834 }
1835
1836 /* We found a namespace. */
1837 result |= walk_namespaces_r (current, f, data);
1838 }
1839
1840 return result;
1841}
1842
1843/* Walk all the namespaces, calling F for each. The DATA is passed to
1844 F as well. */
1845
1846int
1847walk_namespaces (f, data)
1848 walk_namespaces_fn f;
1849 void *data;
1850{
1851 return walk_namespaces_r (global_namespace, f, data);
1852}
1853
1854struct walk_globals_data {
1855 walk_globals_pred p;
1856 walk_globals_fn f;
1857 void *data;
1858};
1859
1860/* Walk the global declarations in NAMESPACE. Whenever one is found
1861 for which P returns non-zero, call F with its address. If any call
1862 to F returns a non-zero value, return a non-zero value. */
1863
68642fb6 1864static int
0aafb128
MM
1865walk_globals_r (namespace, data)
1866 tree namespace;
1867 void *data;
1868{
1869 struct walk_globals_data* wgd = (struct walk_globals_data *) data;
1870 walk_globals_pred p = wgd->p;
1871 walk_globals_fn f = wgd->f;
1872 void *d = wgd->data;
1873 tree *t;
1874 int result = 0;
1875
1876 t = &NAMESPACE_LEVEL (namespace)->names;
1877
1878 while (*t)
1879 {
1880 tree glbl = *t;
1881
1882 if ((*p) (glbl, d))
1883 result |= (*f) (t, d);
1884
1885 /* If F changed *T, then *T still points at the next item to
1886 examine. */
1887 if (*t == glbl)
1888 t = &TREE_CHAIN (*t);
1889 }
1890
1891 return result;
1892}
1893
1894/* Walk the global declarations. Whenever one is found for which P
1895 returns non-zero, call F with its address. If any call to F
1896 returns a non-zero value, return a non-zero value. */
1897
1898int
1899walk_globals (p, f, data)
1900 walk_globals_pred p;
1901 walk_globals_fn f;
1902 void *data;
1903{
1904 struct walk_globals_data wgd;
1905 wgd.p = p;
1906 wgd.f = f;
1907 wgd.data = data;
1908
1909 return walk_namespaces (walk_globals_r, &wgd);
1910}
1911
1912/* Call wrapup_globals_declarations for the globals in NAMESPACE. If
1913 DATA is non-NULL, this is the last time we will call
1914 wrapup_global_declarations for this NAMESPACE. */
1915
1916int
1917wrapup_globals_for_namespace (namespace, data)
1918 tree namespace;
1919 void *data;
1920{
f71f87f9 1921 tree globals = cp_namespace_decls (namespace);
0aafb128
MM
1922 int len = list_length (globals);
1923 tree *vec = (tree *) alloca (sizeof (tree) * len);
1924 int i;
0352cfc8 1925 int result;
0aafb128
MM
1926 tree decl;
1927 int last_time = (data != 0);
1928
1929 if (last_time && namespace == global_namespace)
1930 /* Let compile_file handle the global namespace. */
1931 return 0;
1932
1933 /* Process the decls in reverse order--earliest first.
1934 Put them into VEC from back to front, then take out from front. */
0aafb128 1935 for (i = 0, decl = globals; i < len; i++, decl = TREE_CHAIN (decl))
62136084 1936 vec[len - i - 1] = decl;
68642fb6 1937
0352cfc8
MM
1938 if (last_time)
1939 {
1940 check_global_declarations (vec, len);
1941 return 0;
1942 }
1943
1944 /* Temporarily mark vtables as external. That prevents
1945 wrapup_global_declarations from writing them out; we must process
1946 them ourselves in finish_vtable_vardecl. */
1947 for (i = 0; i < len; ++i)
91f2aa22 1948 if (vtable_decl_p (vec[i], /*data=*/0) && !DECL_EXTERNAL (vec[i]))
0352cfc8
MM
1949 {
1950 DECL_NOT_REALLY_EXTERN (vec[i]) = 1;
1951 DECL_EXTERNAL (vec[i]) = 1;
1952 }
0aafb128 1953
0352cfc8
MM
1954 /* Write out any globals that need to be output. */
1955 result = wrapup_global_declarations (vec, len);
1956
1957 /* Undo the hack to DECL_EXTERNAL above. */
1958 for (i = 0; i < len; ++i)
1959 if (vtable_decl_p (vec[i], /*data=*/0)
1960 && DECL_NOT_REALLY_EXTERN (vec[i]))
1961 {
1962 DECL_NOT_REALLY_EXTERN (vec[i]) = 0;
1963 DECL_EXTERNAL (vec[i]) = 0;
1964 }
1965
1966 return result;
0aafb128
MM
1967}
1968
4519c0a8 1969\f
fc6af6e3
RH
1970/* Mark ARG (which is really a struct binding_level **) for GC. */
1971
4519c0a8
MM
1972static void
1973mark_binding_level (arg)
1974 void *arg;
1975{
1976 struct binding_level *lvl = *(struct binding_level **)arg;
1977
6625cdb5 1978 for (; lvl; lvl = lvl->level_chain)
4519c0a8
MM
1979 {
1980 ggc_mark_tree (lvl->names);
1981 ggc_mark_tree (lvl->tags);
1982 ggc_mark_tree (lvl->usings);
1983 ggc_mark_tree (lvl->using_directives);
1984 ggc_mark_tree (lvl->class_shadowed);
1985 ggc_mark_tree (lvl->type_shadowed);
1986 ggc_mark_tree (lvl->shadowed_labels);
1987 ggc_mark_tree (lvl->blocks);
70adf8a9 1988 ggc_mark_tree (lvl->this_class);
4519c0a8
MM
1989 ggc_mark_tree (lvl->incomplete);
1990 ggc_mark_tree (lvl->dead_vars_from_for);
6625cdb5
JM
1991 }
1992}
1993
1994static void
1995mark_named_label_lists (labs, uses)
1996 void *labs;
1997 void *uses;
1998{
1999 struct named_label_list *l = *(struct named_label_list **)labs;
2000 struct named_label_use_list *u = *(struct named_label_use_list **)uses;
4519c0a8 2001
6625cdb5
JM
2002 for (; l; l = l->next)
2003 {
2004 ggc_mark (l);
2005 mark_binding_level (l->binding_level);
2006 ggc_mark_tree (l->old_value);
2007 ggc_mark_tree (l->label_decl);
2008 ggc_mark_tree (l->bad_decls);
4519c0a8 2009 }
6625cdb5
JM
2010
2011 for (; u; u = u->next)
2012 ggc_mark (u);
4519c0a8 2013}
8d08fdba
MS
2014\f
2015/* For debugging. */
5566b478
MS
2016static int no_print_functions = 0;
2017static int no_print_builtins = 0;
8d08fdba
MS
2018
2019void
2020print_binding_level (lvl)
2021 struct binding_level *lvl;
2022{
2023 tree t;
2024 int i = 0, len;
2025 fprintf (stderr, " blocks=");
2026 fprintf (stderr, HOST_PTR_PRINTF, lvl->blocks);
2027 fprintf (stderr, " n_incomplete=%d parm_flag=%d keep=%d",
f30432d7 2028 list_length (lvl->incomplete), lvl->parm_flag, lvl->keep);
8d08fdba
MS
2029 if (lvl->tag_transparent)
2030 fprintf (stderr, " tag-transparent");
2031 if (lvl->more_cleanups_ok)
2032 fprintf (stderr, " more-cleanups-ok");
2033 if (lvl->have_cleanups)
2034 fprintf (stderr, " have-cleanups");
8d08fdba
MS
2035 fprintf (stderr, "\n");
2036 if (lvl->names)
2037 {
2038 fprintf (stderr, " names:\t");
2039 /* We can probably fit 3 names to a line? */
2040 for (t = lvl->names; t; t = TREE_CHAIN (t))
2041 {
68642fb6 2042 if (no_print_functions && (TREE_CODE (t) == FUNCTION_DECL))
8d08fdba
MS
2043 continue;
2044 if (no_print_builtins
fc378698
MS
2045 && (TREE_CODE (t) == TYPE_DECL)
2046 && (!strcmp (DECL_SOURCE_FILE (t),"<built-in>")))
8d08fdba
MS
2047 continue;
2048
2049 /* Function decls tend to have longer names. */
2050 if (TREE_CODE (t) == FUNCTION_DECL)
2051 len = 3;
2052 else
2053 len = 2;
2054 i += len;
2055 if (i > 6)
2056 {
2057 fprintf (stderr, "\n\t");
2058 i = len;
2059 }
2060 print_node_brief (stderr, "", t, 0);
bd6dd845 2061 if (t == error_mark_node)
8d08fdba
MS
2062 break;
2063 }
2064 if (i)
2065 fprintf (stderr, "\n");
2066 }
2067 if (lvl->tags)
2068 {
2069 fprintf (stderr, " tags:\t");
2070 i = 0;
2071 for (t = lvl->tags; t; t = TREE_CHAIN (t))
2072 {
2073 if (TREE_PURPOSE (t) == NULL_TREE)
2074 len = 3;
2075 else if (TREE_PURPOSE (t) == TYPE_IDENTIFIER (TREE_VALUE (t)))
2076 len = 2;
2077 else
2078 len = 4;
2079 i += len;
2080 if (i > 5)
2081 {
2082 fprintf (stderr, "\n\t");
2083 i = len;
2084 }
2085 if (TREE_PURPOSE (t) == NULL_TREE)
2086 {
2087 print_node_brief (stderr, "<unnamed-typedef", TREE_VALUE (t), 0);
2088 fprintf (stderr, ">");
2089 }
2090 else if (TREE_PURPOSE (t) == TYPE_IDENTIFIER (TREE_VALUE (t)))
2091 print_node_brief (stderr, "", TREE_VALUE (t), 0);
2092 else
2093 {
2094 print_node_brief (stderr, "<typedef", TREE_PURPOSE (t), 0);
2095 print_node_brief (stderr, "", TREE_VALUE (t), 0);
2096 fprintf (stderr, ">");
2097 }
2098 }
2099 if (i)
2100 fprintf (stderr, "\n");
2101 }
8d08fdba
MS
2102 if (lvl->class_shadowed)
2103 {
2104 fprintf (stderr, " class-shadowed:");
2105 for (t = lvl->class_shadowed; t; t = TREE_CHAIN (t))
2106 {
2107 fprintf (stderr, " %s ", IDENTIFIER_POINTER (TREE_PURPOSE (t)));
2108 }
2109 fprintf (stderr, "\n");
2110 }
2111 if (lvl->type_shadowed)
2112 {
2113 fprintf (stderr, " type-shadowed:");
2114 for (t = lvl->type_shadowed; t; t = TREE_CHAIN (t))
2115 {
8d08fdba 2116 fprintf (stderr, " %s ", IDENTIFIER_POINTER (TREE_PURPOSE (t)));
8d08fdba
MS
2117 }
2118 fprintf (stderr, "\n");
2119 }
2120}
2121
2122void
2123print_other_binding_stack (stack)
2124 struct binding_level *stack;
2125{
2126 struct binding_level *level;
2127 for (level = stack; level != global_binding_level; level = level->level_chain)
2128 {
2129 fprintf (stderr, "binding level ");
2130 fprintf (stderr, HOST_PTR_PRINTF, level);
2131 fprintf (stderr, "\n");
2132 print_binding_level (level);
2133 }
2134}
2135
2136void
2137print_binding_stack ()
2138{
2139 struct binding_level *b;
2140 fprintf (stderr, "current_binding_level=");
2141 fprintf (stderr, HOST_PTR_PRINTF, current_binding_level);
2142 fprintf (stderr, "\nclass_binding_level=");
2143 fprintf (stderr, HOST_PTR_PRINTF, class_binding_level);
2144 fprintf (stderr, "\nglobal_binding_level=");
2145 fprintf (stderr, HOST_PTR_PRINTF, global_binding_level);
2146 fprintf (stderr, "\n");
2147 if (class_binding_level)
2148 {
2149 for (b = class_binding_level; b; b = b->level_chain)
2150 if (b == current_binding_level)
2151 break;
2152 if (b)
2153 b = class_binding_level;
2154 else
2155 b = current_binding_level;
2156 }
2157 else
2158 b = current_binding_level;
2159 print_other_binding_stack (b);
2160 fprintf (stderr, "global:\n");
2161 print_binding_level (global_binding_level);
2162}
a9aedbc2 2163
2c73f9f5
ML
2164/* Namespace binding access routines: The namespace_bindings field of
2165 the identifier is polymorphic, with three possible values:
2166 NULL_TREE, a list of CPLUS_BINDINGS, or any other tree_node
2167 indicating the BINDING_VALUE of global_namespace. */
30394414 2168
2c73f9f5
ML
2169/* Check whether the a binding for the name to scope is known.
2170 Assumes that the bindings of the name are already a list
2171 of bindings. Returns the binding found, or NULL_TREE. */
2172
2173static tree
2174find_binding (name, scope)
30394414
JM
2175 tree name;
2176 tree scope;
2177{
2c73f9f5 2178 tree iter, prev = NULL_TREE;
3e3f722c
ML
2179
2180 scope = ORIGINAL_NAMESPACE (scope);
68642fb6 2181
30394414
JM
2182 for (iter = IDENTIFIER_NAMESPACE_BINDINGS (name); iter;
2183 iter = TREE_CHAIN (iter))
2184 {
2185 my_friendly_assert (TREE_CODE (iter) == CPLUS_BINDING, 374);
2186 if (BINDING_SCOPE (iter) == scope)
2c73f9f5 2187 {
cfe507be 2188 /* Move binding found to the front of the list, so
2c73f9f5
ML
2189 subsequent lookups will find it faster. */
2190 if (prev)
2191 {
2192 TREE_CHAIN (prev) = TREE_CHAIN (iter);
2193 TREE_CHAIN (iter) = IDENTIFIER_NAMESPACE_BINDINGS (name);
2194 IDENTIFIER_NAMESPACE_BINDINGS (name) = iter;
2195 }
2196 return iter;
2197 }
2198 prev = iter;
30394414 2199 }
2c73f9f5
ML
2200 return NULL_TREE;
2201}
2202
2203/* Always returns a binding for name in scope. If the
2204 namespace_bindings is not a list, convert it to one first.
2205 If no binding is found, make a new one. */
2206
2207tree
2208binding_for_name (name, scope)
2209 tree name;
2210 tree scope;
2211{
2212 tree b = IDENTIFIER_NAMESPACE_BINDINGS (name);
2213 tree result;
3e3f722c
ML
2214
2215 scope = ORIGINAL_NAMESPACE (scope);
68642fb6 2216
2c73f9f5
ML
2217 if (b && TREE_CODE (b) != CPLUS_BINDING)
2218 {
2219 /* Get rid of optimization for global scope. */
2220 IDENTIFIER_NAMESPACE_BINDINGS (name) = NULL_TREE;
2221 BINDING_VALUE (binding_for_name (name, global_namespace)) = b;
2222 b = IDENTIFIER_NAMESPACE_BINDINGS (name);
2223 }
2224 if (b && (result = find_binding (name, scope)))
2225 return result;
cd9f6678 2226 /* Not found, make a new one. */
2c73f9f5
ML
2227 result = make_node (CPLUS_BINDING);
2228 TREE_CHAIN (result) = b;
2229 IDENTIFIER_NAMESPACE_BINDINGS (name) = result;
2230 BINDING_SCOPE (result) = scope;
2231 BINDING_TYPE (result) = NULL_TREE;
2232 BINDING_VALUE (result) = NULL_TREE;
2c73f9f5
ML
2233 return result;
2234}
2235
2236/* Return the binding value for name in scope, considering that
2237 namespace_binding may or may not be a list of CPLUS_BINDINGS. */
2238
2239tree
2240namespace_binding (name, scope)
2241 tree name;
2242 tree scope;
2243{
2244 tree b = IDENTIFIER_NAMESPACE_BINDINGS (name);
2245 if (b == NULL_TREE)
2246 return NULL_TREE;
cb0dbb9a
JM
2247 if (scope == NULL_TREE)
2248 scope = global_namespace;
2c73f9f5
ML
2249 if (TREE_CODE (b) != CPLUS_BINDING)
2250 return (scope == global_namespace) ? b : NULL_TREE;
2251 name = find_binding (name,scope);
2252 if (name == NULL_TREE)
2253 return name;
2254 return BINDING_VALUE (name);
2255}
2256
2257/* Set the binding value for name in scope. If modifying the binding
2258 of global_namespace is attempted, try to optimize it. */
2259
2260void
2261set_namespace_binding (name, scope, val)
2262 tree name;
2263 tree scope;
2264 tree val;
2265{
2266 tree b;
cb0dbb9a
JM
2267
2268 if (scope == NULL_TREE)
2269 scope = global_namespace;
68642fb6 2270
2c73f9f5
ML
2271 if (scope == global_namespace)
2272 {
2273 b = IDENTIFIER_NAMESPACE_BINDINGS (name);
2274 if (b == NULL_TREE || TREE_CODE (b) != CPLUS_BINDING)
2275 {
2276 IDENTIFIER_NAMESPACE_BINDINGS (name) = val;
2277 return;
2278 }
2279 }
2280 b = binding_for_name (name, scope);
2281 BINDING_VALUE (b) = val;
30394414
JM
2282}
2283
dff6b454
RK
2284/* Push into the scope of the NAME namespace. If NAME is NULL_TREE, then we
2285 select a name that is unique to this compilation unit. */
e92cc029 2286
a9aedbc2
MS
2287void
2288push_namespace (name)
2289 tree name;
2290{
b370501f 2291 tree d = NULL_TREE;
30394414
JM
2292 int need_new = 1;
2293 int implicit_use = 0;
2c73f9f5 2294 int global = 0;
30394414
JM
2295 if (!global_namespace)
2296 {
2c73f9f5 2297 /* This must be ::. */
30394414 2298 my_friendly_assert (name == get_identifier ("::"), 377);
2c73f9f5 2299 global = 1;
30394414
JM
2300 }
2301 else if (!name)
2302 {
3ab52652
ML
2303 /* The name of anonymous namespace is unique for the translation
2304 unit. */
0c8feefe
MM
2305 if (!anonymous_namespace_name)
2306 anonymous_namespace_name = get_file_function_name ('N');
2307 name = anonymous_namespace_name;
3ab52652
ML
2308 d = IDENTIFIER_NAMESPACE_VALUE (name);
2309 if (d)
2310 /* Reopening anonymous namespace. */
2311 need_new = 0;
30394414 2312 implicit_use = 1;
2c73f9f5
ML
2313 }
2314 else if (current_namespace == global_namespace
6bcedb4e
MM
2315 && !flag_honor_std
2316 && name == std_identifier)
2c73f9f5 2317 {
6bcedb4e 2318 in_fake_std++;
2c73f9f5
ML
2319 return;
2320 }
30394414
JM
2321 else
2322 {
2c73f9f5 2323 /* Check whether this is an extended namespace definition. */
30394414
JM
2324 d = IDENTIFIER_NAMESPACE_VALUE (name);
2325 if (d != NULL_TREE && TREE_CODE (d) == NAMESPACE_DECL)
3e3f722c
ML
2326 {
2327 need_new = 0;
2328 if (DECL_NAMESPACE_ALIAS (d))
2329 {
8251199e 2330 cp_error ("namespace alias `%D' not allowed here, assuming `%D'",
3e3f722c
ML
2331 d, DECL_NAMESPACE_ALIAS (d));
2332 d = DECL_NAMESPACE_ALIAS (d);
2333 }
2334 }
30394414 2335 }
68642fb6 2336
30394414
JM
2337 if (need_new)
2338 {
2c73f9f5 2339 /* Make a new namespace, binding the name to it. */
6b57ac29 2340 d = build_lang_decl (NAMESPACE_DECL, name, void_type_node);
2c73f9f5
ML
2341 /* The global namespace is not pushed, and the global binding
2342 level is set elsewhere. */
2343 if (!global)
2344 {
40f1e342 2345 DECL_CONTEXT (d) = FROB_CONTEXT (current_namespace);
2c73f9f5
ML
2346 d = pushdecl (d);
2347 pushlevel (0);
2348 declare_namespace_level ();
2349 NAMESPACE_LEVEL (d) = current_binding_level;
2350 }
30394414 2351 }
2c73f9f5
ML
2352 else
2353 resume_binding_level (NAMESPACE_LEVEL (d));
2354
30394414
JM
2355 if (implicit_use)
2356 do_using_directive (d);
2c73f9f5 2357 /* Enter the name space. */
30394414 2358 current_namespace = d;
a9aedbc2
MS
2359}
2360
2361/* Pop from the scope of the current namespace. */
e92cc029 2362
a9aedbc2
MS
2363void
2364pop_namespace ()
2365{
2c73f9f5
ML
2366 if (current_namespace == global_namespace)
2367 {
6bcedb4e
MM
2368 my_friendly_assert (in_fake_std > 0, 980421);
2369 in_fake_std--;
2c73f9f5
ML
2370 return;
2371 }
cb0dbb9a 2372 current_namespace = CP_DECL_CONTEXT (current_namespace);
2c73f9f5
ML
2373 /* The binding level is not popped, as it might be re-opened later. */
2374 suspend_binding_level ();
2375}
a9aedbc2 2376
61fc8c9e
MM
2377/* Push into the scope of the namespace NS, even if it is deeply
2378 nested within another namespace. */
2379
2380void
2381push_nested_namespace (ns)
2382 tree ns;
2383{
2384 if (ns == global_namespace)
2385 push_to_top_level ();
2386 else
2387 {
2388 push_nested_namespace (CP_DECL_CONTEXT (ns));
2389 push_namespace (DECL_NAME (ns));
2390 }
2391}
2392
2393/* Pop back from the scope of the namespace NS, which was previously
2394 entered with push_nested_namespace. */
68642fb6 2395
61fc8c9e
MM
2396void
2397pop_nested_namespace (ns)
2398 tree ns;
2399{
2400 while (ns != global_namespace)
2401 {
2402 pop_namespace ();
2403 ns = CP_DECL_CONTEXT (ns);
2404 }
2405
2406 pop_from_top_level ();
2407}
2408
8d08fdba
MS
2409\f
2410/* Subroutines for reverting temporarily to top-level for instantiation
2411 of templates and such. We actually need to clear out the class- and
2412 local-value slots of all identifiers, so that only the global values
2413 are at all visible. Simply setting current_binding_level to the global
2414 scope isn't enough, because more binding levels may be pushed. */
9cd64686 2415struct saved_scope *scope_chain;
8d08fdba 2416
9cd64686 2417/* Mark ARG (which is really a struct saved_scope **) for GC. */
fc6af6e3
RH
2418
2419static void
2420mark_saved_scope (arg)
2421 void *arg;
2422{
2423 struct saved_scope *t = *(struct saved_scope **)arg;
2424 while (t)
2425 {
fc6af6e3
RH
2426 mark_binding_level (&t->class_bindings);
2427 ggc_mark_tree (t->old_bindings);
2428 ggc_mark_tree (t->old_namespace);
2429 ggc_mark_tree (t->class_name);
2430 ggc_mark_tree (t->class_type);
2431 ggc_mark_tree (t->access_specifier);
a8f73d4b 2432 ggc_mark_tree (t->function_decl);
fc6af6e3 2433 if (t->lang_base)
9cd64686 2434 ggc_mark_tree_varray (t->lang_base);
fc6af6e3 2435 ggc_mark_tree (t->lang_name);
fc6af6e3 2436 ggc_mark_tree (t->template_parms);
9cd64686
MM
2437 ggc_mark_tree (t->x_previous_class_type);
2438 ggc_mark_tree (t->x_previous_class_values);
6f80451c 2439 ggc_mark_tree (t->x_saved_tree);
70adf8a9
JM
2440 ggc_mark_tree (t->incomplete);
2441 ggc_mark_tree (t->lookups);
6f80451c
MM
2442
2443 mark_stmt_tree (&t->x_stmt_tree);
a8f73d4b 2444 mark_binding_level (&t->bindings);
fc6af6e3
RH
2445 t = t->prev;
2446 }
2447}
2448
e92cc029 2449static tree
45537677
MS
2450store_bindings (names, old_bindings)
2451 tree names, old_bindings;
2452{
2453 tree t;
c00996a3
JM
2454 tree search_bindings = old_bindings;
2455
45537677
MS
2456 for (t = names; t; t = TREE_CHAIN (t))
2457 {
2458 tree binding, t1, id;
2459
2460 if (TREE_CODE (t) == TREE_LIST)
2461 id = TREE_PURPOSE (t);
2462 else
2463 id = DECL_NAME (t);
2464
68642fb6 2465 if (!id
f181d4ae
MM
2466 /* Note that we may have an IDENTIFIER_CLASS_VALUE even when
2467 we have no IDENTIFIER_BINDING if we have left the class
2468 scope, but cached the class-level declarations. */
2469 || !(IDENTIFIER_BINDING (id) || IDENTIFIER_CLASS_VALUE (id)))
45537677
MS
2470 continue;
2471
c00996a3 2472 for (t1 = search_bindings; t1; t1 = TREE_CHAIN (t1))
45537677
MS
2473 if (TREE_VEC_ELT (t1, 0) == id)
2474 goto skip_it;
78957a2a 2475
31a714f6 2476 my_friendly_assert (TREE_CODE (id) == IDENTIFIER_NODE, 135);
87e3dbc9 2477 binding = make_tree_vec (4);
31a714f6
MM
2478 TREE_VEC_ELT (binding, 0) = id;
2479 TREE_VEC_ELT (binding, 1) = REAL_IDENTIFIER_TYPE_VALUE (id);
2480 TREE_VEC_ELT (binding, 2) = IDENTIFIER_BINDING (id);
2481 TREE_VEC_ELT (binding, 3) = IDENTIFIER_CLASS_VALUE (id);
2482 IDENTIFIER_BINDING (id) = NULL_TREE;
2483 IDENTIFIER_CLASS_VALUE (id) = NULL_TREE;
45537677
MS
2484 TREE_CHAIN (binding) = old_bindings;
2485 old_bindings = binding;
2486 skip_it:
2487 ;
2488 }
2489 return old_bindings;
2490}
2491
8d08fdba 2492void
5566b478
MS
2493maybe_push_to_top_level (pseudo)
2494 int pseudo;
8d08fdba 2495{
a8f73d4b 2496 struct saved_scope *s;
9cd64686 2497 struct binding_level *b;
a8f73d4b
MM
2498 tree old_bindings;
2499 int need_pop;
2500
2501 s = (struct saved_scope *) xcalloc (1, sizeof (struct saved_scope));
8d08fdba 2502
9cd64686
MM
2503 b = scope_chain ? current_binding_level : 0;
2504
a8f73d4b 2505 /* If we're in the middle of some function, save our state. */
01d939e8 2506 if (cfun)
a8f73d4b
MM
2507 {
2508 need_pop = 1;
2509 push_function_context_to (NULL_TREE);
2510 }
2511 else
2512 need_pop = 0;
e349ee73 2513
a8f73d4b 2514 old_bindings = NULL_TREE;
9cd64686 2515 if (scope_chain && previous_class_type)
a50f0918
MS
2516 old_bindings = store_bindings (previous_class_values, old_bindings);
2517
8d08fdba
MS
2518 /* Have to include global_binding_level, because class-level decls
2519 aren't listed anywhere useful. */
2520 for (; b; b = b->level_chain)
2521 {
2522 tree t;
2523
2c73f9f5
ML
2524 /* Template IDs are inserted into the global level. If they were
2525 inserted into namespace level, finish_file wouldn't find them
2526 when doing pending instantiations. Therefore, don't stop at
2527 namespace level, but continue until :: . */
74b846e0 2528 if (b == global_binding_level || (pseudo && b->template_parms_p))
5566b478 2529 break;
8d08fdba 2530
45537677 2531 old_bindings = store_bindings (b->names, old_bindings);
cffa8729 2532 /* We also need to check class_shadowed to save class-level type
45537677
MS
2533 bindings, since pushclass doesn't fill in b->names. */
2534 if (b->parm_flag == 2)
cffa8729 2535 old_bindings = store_bindings (b->class_shadowed, old_bindings);
8d08fdba 2536
8d08fdba
MS
2537 /* Unwind type-value slots back to top level. */
2538 for (t = b->type_shadowed; t; t = TREE_CHAIN (t))
2539 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (t), TREE_VALUE (t));
2540 }
fc0e7bf5
MM
2541 s->prev = scope_chain;
2542 s->old_bindings = old_bindings;
a8f73d4b
MM
2543 s->bindings = b;
2544 s->need_pop_function_context = need_pop;
2545 s->function_decl = current_function_decl;
fc0e7bf5
MM
2546
2547 scope_chain = s;
8d08fdba 2548 current_function_decl = NULL_TREE;
9cd64686 2549 VARRAY_TREE_INIT (current_lang_base, 10, "current_lang_base");
51c184be 2550 current_lang_name = lang_name_cplusplus;
2c73f9f5 2551 current_namespace = global_namespace;
5566b478
MS
2552}
2553
2554void
2555push_to_top_level ()
2556{
2557 maybe_push_to_top_level (0);
8d08fdba
MS
2558}
2559
2560void
2561pop_from_top_level ()
2562{
9cd64686 2563 struct saved_scope *s = scope_chain;
8d08fdba
MS
2564 tree t;
2565
e76a2646 2566 /* Clear out class-level bindings cache. */
8d08fdba 2567 if (previous_class_type)
8f032717 2568 invalidate_class_lookup_cache ();
8d08fdba 2569
9cd64686
MM
2570 VARRAY_FREE (current_lang_base);
2571
2572 scope_chain = s->prev;
87e3dbc9 2573 for (t = s->old_bindings; t; t = TREE_CHAIN (t))
8d08fdba
MS
2574 {
2575 tree id = TREE_VEC_ELT (t, 0);
31a714f6
MM
2576
2577 SET_IDENTIFIER_TYPE_VALUE (id, TREE_VEC_ELT (t, 1));
2578 IDENTIFIER_BINDING (id) = TREE_VEC_ELT (t, 2);
2579 IDENTIFIER_CLASS_VALUE (id) = TREE_VEC_ELT (t, 3);
8d08fdba 2580 }
9cd64686 2581
a8f73d4b
MM
2582 /* If we were in the middle of compiling a function, restore our
2583 state. */
2584 if (s->need_pop_function_context)
2585 pop_function_context_from (NULL_TREE);
2586 current_function_decl = s->function_decl;
e349ee73 2587
a8f73d4b 2588 free (s);
8d08fdba
MS
2589}
2590\f
2591/* Push a definition of struct, union or enum tag "name".
68642fb6 2592 into binding_level "b". "type" should be the type node,
8d08fdba
MS
2593 We assume that the tag "name" is not already defined.
2594
2595 Note that the definition may really be just a forward reference.
2596 In that case, the TYPE_SIZE will be a NULL_TREE.
2597
e92cc029 2598 C++ gratuitously puts all these tags in the name space. */
8d08fdba
MS
2599
2600/* When setting the IDENTIFIER_TYPE_VALUE field of an identifier ID,
2601 record the shadowed value for this binding contour. TYPE is
2602 the type that ID maps to. */
2603
2604static void
2605set_identifier_type_value_with_scope (id, type, b)
2606 tree id;
2607 tree type;
2608 struct binding_level *b;
2609{
2c73f9f5 2610 if (!b->namespace_p)
8d08fdba 2611 {
2c73f9f5
ML
2612 /* Shadow the marker, not the real thing, so that the marker
2613 gets restored later. */
2614 tree old_type_value = REAL_IDENTIFIER_TYPE_VALUE (id);
8d08fdba
MS
2615 b->type_shadowed
2616 = tree_cons (id, old_type_value, b->type_shadowed);
2617 }
2c73f9f5
ML
2618 else
2619 {
2620 tree binding = binding_for_name (id, current_namespace);
2621 BINDING_TYPE (binding) = type;
2622 /* Store marker instead of real type. */
2623 type = global_type_node;
2624 }
8d08fdba
MS
2625 SET_IDENTIFIER_TYPE_VALUE (id, type);
2626}
2627
8f032717 2628/* As set_identifier_type_value_with_scope, but using current_binding_level. */
8d08fdba
MS
2629
2630void
2631set_identifier_type_value (id, type)
2632 tree id;
2633 tree type;
2634{
8f032717 2635 set_identifier_type_value_with_scope (id, type, current_binding_level);
8d08fdba
MS
2636}
2637
2c73f9f5
ML
2638/* Return the type associated with id. */
2639
2640tree
2641identifier_type_value (id)
2642 tree id;
2643{
2644 /* There is no type with that name, anywhere. */
2645 if (REAL_IDENTIFIER_TYPE_VALUE (id) == NULL_TREE)
2646 return NULL_TREE;
2647 /* This is not the type marker, but the real thing. */
2648 if (REAL_IDENTIFIER_TYPE_VALUE (id) != global_type_node)
2649 return REAL_IDENTIFIER_TYPE_VALUE (id);
2650 /* Have to search for it. It must be on the global level, now.
2651 Ask lookup_name not to return non-types. */
3e3f722c 2652 id = lookup_name_real (id, 2, 1, 0);
2c73f9f5
ML
2653 if (id)
2654 return TREE_TYPE (id);
2655 return NULL_TREE;
2656}
2657
a9aedbc2
MS
2658/* Pop off extraneous binding levels left over due to syntax errors.
2659
2660 We don't pop past namespaces, as they might be valid. */
e92cc029 2661
8926095f
MS
2662void
2663pop_everything ()
2664{
2665#ifdef DEBUG_CP_BINDING_LEVELS
2666 fprintf (stderr, "XXX entering pop_everything ()\n");
2667#endif
8f032717 2668 while (!toplevel_bindings_p ())
8926095f 2669 {
8f032717 2670 if (current_binding_level->parm_flag == 2)
b74a0560 2671 pop_nested_class ();
8926095f
MS
2672 else
2673 poplevel (0, 0, 0);
2674 }
2675#ifdef DEBUG_CP_BINDING_LEVELS
2676 fprintf (stderr, "XXX leaving pop_everything ()\n");
2677#endif
2678}
2679
39c01e4c
MM
2680/* The type TYPE is being declared. If it is a class template, or a
2681 specialization of a class template, do any processing required and
2682 perform error-checking. If IS_FRIEND is non-zero, this TYPE is
2683 being declared a friend. B is the binding level at which this TYPE
2684 should be bound.
2685
2686 Returns the TYPE_DECL for TYPE, which may have been altered by this
2687 processing. */
2688
68642fb6 2689static tree
39c01e4c
MM
2690maybe_process_template_type_declaration (type, globalize, b)
2691 tree type;
2692 int globalize;
2693 struct binding_level* b;
2694{
2695 tree decl = TYPE_NAME (type);
68642fb6 2696
39c01e4c
MM
2697 if (processing_template_parmlist)
2698 /* You can't declare a new template type in a template parameter
2699 list. But, you can declare a non-template type:
68642fb6 2700
39c01e4c 2701 template <class A*> struct S;
68642fb6 2702
39c01e4c
MM
2703 is a forward-declaration of `A'. */
2704 ;
68642fb6 2705 else
39c01e4c
MM
2706 {
2707 maybe_check_template_type (type);
2708
68642fb6 2709 my_friendly_assert (IS_AGGR_TYPE (type)
ed44da02 2710 || TREE_CODE (type) == ENUMERAL_TYPE, 0);
68642fb6
UD
2711
2712
3ebc5c52 2713 if (processing_template_decl)
39c01e4c
MM
2714 {
2715 /* This may change after the call to
2716 push_template_decl_real, but we want the original value. */
2717 tree name = DECL_NAME (decl);
2718
2719 decl = push_template_decl_real (decl, globalize);
2720 /* If the current binding level is the binding level for the
2721 template parameters (see the comment in
2722 begin_template_parm_list) and the enclosing level is a class
2723 scope, and we're not looking at a friend, push the
2724 declaration of the member class into the class scope. In the
2725 friend case, push_template_decl will already have put the
2726 friend into global scope, if appropriate. */
ed44da02 2727 if (TREE_CODE (type) != ENUMERAL_TYPE
74b846e0 2728 && !globalize && b->template_parms_p
39c01e4c
MM
2729 && b->level_chain->parm_flag == 2)
2730 {
61a127b3 2731 finish_member_declaration (CLASSTYPE_TI_TEMPLATE (type));
39c01e4c
MM
2732 /* Put this tag on the list of tags for the class, since
2733 that won't happen below because B is not the class
2734 binding level, but is instead the pseudo-global level. */
68642fb6 2735 b->level_chain->tags =
e1b3e07d 2736 tree_cons (name, type, b->level_chain->tags);
d0f062fb 2737 if (!COMPLETE_TYPE_P (current_class_type))
39c01e4c
MM
2738 CLASSTYPE_TAGS (current_class_type) = b->level_chain->tags;
2739 }
2740 }
2741 }
2742
2743 return decl;
2744}
2745
9188c363
MM
2746/* In C++, you don't have to write `struct S' to refer to `S'; you
2747 can just use `S'. We accomplish this by creating a TYPE_DECL as
2748 if the user had written `typedef struct S S'. Create and return
2749 the TYPE_DECL for TYPE. */
2750
2751tree
2752create_implicit_typedef (name, type)
2753 tree name;
2754 tree type;
2755{
2756 tree decl;
2757
2758 decl = build_decl (TYPE_DECL, name, type);
c727aa5e 2759 DECL_ARTIFICIAL (decl) = 1;
9188c363
MM
2760 /* There are other implicit type declarations, like the one *within*
2761 a class that allows you to write `S::S'. We must distinguish
2762 amongst these. */
2763 SET_DECL_IMPLICIT_TYPEDEF_P (decl);
2764 TYPE_NAME (type) = decl;
2765
2766 return decl;
2767}
2768
8e4ce833
JJ
2769/* Remember a local name for name-mangling purposes. */
2770
2771static void
2772push_local_name (decl)
2773 tree decl;
2774{
2775 size_t i, nelts;
2776 tree t, name;
2777
2778 if (!local_names)
2779 VARRAY_TREE_INIT (local_names, 8, "local_names");
2780
2781 name = DECL_NAME (decl);
2782
2783 nelts = VARRAY_ACTIVE_SIZE (local_names);
2784 for (i = 0; i < nelts; i++)
2785 {
2786 t = VARRAY_TREE (local_names, i);
2787 if (DECL_NAME (t) == name)
2788 {
2789 if (!DECL_LANG_SPECIFIC (decl))
2790 retrofit_lang_decl (decl);
2791 if (DECL_LANG_SPECIFIC (t))
2792 DECL_DISCRIMINATOR (decl) = DECL_DISCRIMINATOR (t) + 1;
2793 else
2794 DECL_DISCRIMINATOR (decl) = 1;
2795
2796 VARRAY_TREE (local_names, i) = decl;
2797 return;
2798 }
2799 }
2800
2801 VARRAY_PUSH_TREE (local_names, decl);
2802}
2803
8d08fdba 2804/* Push a tag name NAME for struct/class/union/enum type TYPE.
6757edfe 2805 Normally put it into the inner-most non-tag-transparent scope,
8d08fdba 2806 but if GLOBALIZE is true, put it in the inner-most non-class scope.
e92cc029 2807 The latter is needed for implicit declarations. */
8d08fdba
MS
2808
2809void
2810pushtag (name, type, globalize)
2811 tree name, type;
2812 int globalize;
2813{
2814 register struct binding_level *b;
8d08fdba 2815
8f032717 2816 b = current_binding_level;
8d08fdba
MS
2817 while (b->tag_transparent
2818 || (globalize && b->parm_flag == 2))
2819 b = b->level_chain;
2820
e1b3e07d 2821 b->tags = tree_cons (name, type, b->tags);
8d08fdba
MS
2822
2823 if (name)
2824 {
8d08fdba 2825 /* Do C++ gratuitous typedefing. */
db5ae43f 2826 if (IDENTIFIER_TYPE_VALUE (name) != type)
8d08fdba 2827 {
93cdc044 2828 register tree d = NULL_TREE;
848cf1e0 2829 int in_class = 0;
9188c363 2830 tree context = TYPE_CONTEXT (type);
280f9385 2831
280f9385
MM
2832 if (! context)
2833 {
2834 tree cs = current_scope ();
2835
2836 if (! globalize)
2837 context = cs;
2f939d94 2838 else if (cs != NULL_TREE && TYPE_P (cs))
280f9385
MM
2839 /* When declaring a friend class of a local class, we want
2840 to inject the newly named class into the scope
2841 containing the local class, not the namespace scope. */
4f1c5b7d 2842 context = decl_function_context (get_type_decl (cs));
280f9385 2843 }
280f9385
MM
2844 if (!context)
2845 context = current_namespace;
8d08fdba 2846
74b846e0 2847 if ((b->template_parms_p && b->level_chain->parm_flag == 2)
93cdc044
JM
2848 || b->parm_flag == 2)
2849 in_class = 1;
93cdc044 2850
848cf1e0
MM
2851 if (current_lang_name == lang_name_java)
2852 TYPE_FOR_JAVA (type) = 1;
93cdc044 2853
9188c363 2854 d = create_implicit_typedef (name, type);
cb0dbb9a 2855 DECL_CONTEXT (d) = FROB_CONTEXT (context);
9188c363
MM
2856 if (! in_class)
2857 set_identifier_type_value_with_scope (name, type, b);
e1cd6e56 2858
39c01e4c
MM
2859 d = maybe_process_template_type_declaration (type,
2860 globalize, b);
93cdc044
JM
2861
2862 if (b->parm_flag == 2)
61a127b3 2863 {
848cf1e0 2864 if (!PROCESSING_REAL_TEMPLATE_DECL_P ())
61a127b3
MM
2865 /* Put this TYPE_DECL on the TYPE_FIELDS list for the
2866 class. But if it's a member template class, we
2867 want the TEMPLATE_DECL, not the TYPE_DECL, so this
2868 is done later. */
2869 finish_member_declaration (d);
8f032717
MM
2870 else
2871 pushdecl_class_level (d);
61a127b3 2872 }
93cdc044
JM
2873 else
2874 d = pushdecl_with_scope (d, b);
2875
848cf1e0
MM
2876 if (ANON_AGGRNAME_P (name))
2877 DECL_IGNORED_P (d) = 1;
2878
2879 TYPE_CONTEXT (type) = DECL_CONTEXT (d);
1f6e1acc
AS
2880
2881 /* If this is a local class, keep track of it. We need this
2882 information for name-mangling, and so that it is possible to find
2883 all function definitions in a translation unit in a convenient
2884 way. (It's otherwise tricky to find a member function definition
2885 it's only pointed to from within a local class.) */
5362b086 2886 if (TYPE_CONTEXT (type)
089acd57
MM
2887 && TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL
2888 && !processing_template_decl)
1f6e1acc 2889 VARRAY_PUSH_TREE (local_classes, type);
8d08fdba
MS
2890 }
2891 if (b->parm_flag == 2)
2892 {
d0f062fb 2893 if (!COMPLETE_TYPE_P (current_class_type))
8d08fdba
MS
2894 CLASSTYPE_TAGS (current_class_type) = b->tags;
2895 }
2896 }
2897
2898 if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL)
2899 /* Use the canonical TYPE_DECL for this node. */
2900 TYPE_STUB_DECL (type) = TYPE_NAME (type);
2901 else
2902 {
2903 /* Create a fake NULL-named TYPE_DECL node whose TREE_TYPE
2904 will be the tagged type we just added to the current
2905 binding level. This fake NULL-named TYPE_DECL node helps
2906 dwarfout.c to know when it needs to output a
2907 representation of a tagged type, and it also gives us a
2908 convenient place to record the "scope start" address for
2909 the tagged type. */
2910
8d08fdba 2911 tree d = build_decl (TYPE_DECL, NULL_TREE, type);
8d08fdba
MS
2912 TYPE_STUB_DECL (type) = pushdecl_with_scope (d, b);
2913 }
2914}
2915
2916/* Counter used to create anonymous type names. */
e92cc029 2917
8d08fdba
MS
2918static int anon_cnt = 0;
2919
2920/* Return an IDENTIFIER which can be used as a name for
2921 anonymous structs and unions. */
e92cc029 2922
8d08fdba
MS
2923tree
2924make_anon_name ()
2925{
2926 char buf[32];
2927
2928 sprintf (buf, ANON_AGGRNAME_FORMAT, anon_cnt++);
2929 return get_identifier (buf);
2930}
2931
2932/* Clear the TREE_PURPOSE slot of tags which have anonymous typenames.
2933 This keeps dbxout from getting confused. */
e92cc029 2934
8d08fdba
MS
2935void
2936clear_anon_tags ()
2937{
2938 register struct binding_level *b;
2939 register tree tags;
2940 static int last_cnt = 0;
2941
2942 /* Fast out if no new anon names were declared. */
2943 if (last_cnt == anon_cnt)
2944 return;
2945
2946 b = current_binding_level;
2947 while (b->tag_transparent)
2948 b = b->level_chain;
2949 tags = b->tags;
2950 while (tags)
2951 {
2952 /* A NULL purpose means we have already processed all tags
2953 from here to the end of the list. */
2954 if (TREE_PURPOSE (tags) == NULL_TREE)
2955 break;
2956 if (ANON_AGGRNAME_P (TREE_PURPOSE (tags)))
2957 TREE_PURPOSE (tags) = NULL_TREE;
2958 tags = TREE_CHAIN (tags);
2959 }
2960 last_cnt = anon_cnt;
2961}
2962\f
2963/* Subroutine of duplicate_decls: return truthvalue of whether
2964 or not types of these decls match.
2965
2966 For C++, we must compare the parameter list so that `int' can match
2967 `int&' in a parameter position, but `int&' is not confused with
2968 `const int&'. */
e92cc029 2969
6060a796 2970int
8d08fdba
MS
2971decls_match (newdecl, olddecl)
2972 tree newdecl, olddecl;
2973{
2974 int types_match;
2975
347d73d7
ML
2976 if (newdecl == olddecl)
2977 return 1;
2978
6b4b3deb
MM
2979 if (TREE_CODE (newdecl) != TREE_CODE (olddecl))
2980 /* If the two DECLs are not even the same kind of thing, we're not
2981 interested in their types. */
2982 return 0;
2983
2984 if (TREE_CODE (newdecl) == FUNCTION_DECL)
8d08fdba
MS
2985 {
2986 tree f1 = TREE_TYPE (newdecl);
2987 tree f2 = TREE_TYPE (olddecl);
2988 tree p1 = TYPE_ARG_TYPES (f1);
2989 tree p2 = TYPE_ARG_TYPES (f2);
2990
4f1c5b7d 2991 if (CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl)
eb68cb58
MM
2992 && ! (DECL_EXTERN_C_P (newdecl)
2993 && DECL_EXTERN_C_P (olddecl)))
c5a6fc45
JM
2994 return 0;
2995
8d08fdba 2996 if (TREE_CODE (f1) != TREE_CODE (f2))
e867a179 2997 return 0;
8d08fdba 2998
3bfdc719 2999 if (same_type_p (TREE_TYPE (f1), TREE_TYPE (f2)))
8926095f 3000 {
8b27e9ef
NS
3001 if (p2 == NULL_TREE && DECL_EXTERN_C_P (olddecl)
3002 && (DECL_BUILT_IN (olddecl)
3003#ifndef NO_IMPLICIT_EXTERN_C
3004 || (DECL_IN_SYSTEM_HEADER (newdecl) && !DECL_CLASS_SCOPE_P (newdecl))
3005 || (DECL_IN_SYSTEM_HEADER (olddecl) && !DECL_CLASS_SCOPE_P (olddecl))
3006#endif
3007 ))
a28e3c7f
MS
3008 {
3009 types_match = self_promoting_args_p (p1);
3010 if (p1 == void_list_node)
3011 TREE_TYPE (newdecl) = TREE_TYPE (olddecl);
3012 }
8b27e9ef
NS
3013#ifndef NO_IMPLICIT_EXTERN_C
3014 else if (p1 == NULL_TREE
3015 && (DECL_EXTERN_C_P (olddecl)
3016 && DECL_IN_SYSTEM_HEADER (olddecl)
3017 && !DECL_CLASS_SCOPE_P (olddecl))
3018 && (DECL_EXTERN_C_P (newdecl)
3019 && DECL_IN_SYSTEM_HEADER (newdecl)
3020 && !DECL_CLASS_SCOPE_P (newdecl)))
a28e3c7f
MS
3021 {
3022 types_match = self_promoting_args_p (p2);
3023 TREE_TYPE (newdecl) = TREE_TYPE (olddecl);
3024 }
8b27e9ef 3025#endif
8926095f 3026 else
91063b51 3027 types_match = compparms (p1, p2);
8926095f 3028 }
8d08fdba
MS
3029 else
3030 types_match = 0;
3031 }
6b4b3deb 3032 else if (TREE_CODE (newdecl) == TEMPLATE_DECL)
51c184be 3033 {
f84b4be9
JM
3034 if (!comp_template_parms (DECL_TEMPLATE_PARMS (newdecl),
3035 DECL_TEMPLATE_PARMS (olddecl)))
3036 return 0;
68642fb6 3037
2bb5d995
JM
3038 if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl))
3039 != TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)))
3040 return 0;
3041
f84b4be9
JM
3042 if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL)
3043 types_match = 1;
3044 else
3045 types_match = decls_match (DECL_TEMPLATE_RESULT (olddecl),
3046 DECL_TEMPLATE_RESULT (newdecl));
51c184be 3047 }
8d08fdba
MS
3048 else
3049 {
3050 if (TREE_TYPE (newdecl) == error_mark_node)
3051 types_match = TREE_TYPE (olddecl) == error_mark_node;
3052 else if (TREE_TYPE (olddecl) == NULL_TREE)
3053 types_match = TREE_TYPE (newdecl) == NULL_TREE;
a0a33927
MS
3054 else if (TREE_TYPE (newdecl) == NULL_TREE)
3055 types_match = 0;
8d08fdba 3056 else
01240200 3057 types_match = comptypes (TREE_TYPE (newdecl),
3bfdc719
MM
3058 TREE_TYPE (olddecl),
3059 COMPARE_REDECLARATION);
8d08fdba
MS
3060 }
3061
3062 return types_match;
3063}
3064
3065/* If NEWDECL is `static' and an `extern' was seen previously,
24f30ed4 3066 warn about it. OLDDECL is the previous declaration.
8d08fdba
MS
3067
3068 Note that this does not apply to the C++ case of declaring
3069 a variable `extern const' and then later `const'.
3070
8d08fdba
MS
3071 Don't complain about built-in functions, since they are beyond
3072 the user's control. */
3073
3074static void
3075warn_extern_redeclared_static (newdecl, olddecl)
3076 tree newdecl, olddecl;
3077{
d8e178a0 3078 static const char *explicit_extern_static_warning
8251199e 3079 = "`%D' was declared `extern' and later `static'";
d8e178a0 3080 static const char *implicit_extern_static_warning
8251199e
JM
3081 = "`%D' was declared implicitly `extern' and later `static'";
3082
24f30ed4
MM
3083 tree name;
3084
68642fb6 3085 if (TREE_CODE (newdecl) == TYPE_DECL
655dc6ee
JM
3086 || TREE_CODE (newdecl) == TEMPLATE_DECL
3087 || TREE_CODE (newdecl) == CONST_DECL)
8d08fdba 3088 return;
68642fb6 3089
963d5758
MM
3090 /* Don't get confused by static member functions; that's a different
3091 use of `static'. */
3092 if (TREE_CODE (newdecl) == FUNCTION_DECL
3093 && DECL_STATIC_FUNCTION_P (newdecl))
3094 return;
8d08fdba 3095
24f30ed4
MM
3096 /* If the old declaration was `static', or the new one isn't, then
3097 then everything is OK. */
3098 if (DECL_THIS_STATIC (olddecl) || !DECL_THIS_STATIC (newdecl))
3099 return;
3100
3101 /* It's OK to declare a builtin function as `static'. */
3102 if (TREE_CODE (olddecl) == FUNCTION_DECL
3103 && DECL_ARTIFICIAL (olddecl))
3104 return;
3105
8d08fdba 3106 name = DECL_ASSEMBLER_NAME (newdecl);
24f30ed4
MM
3107 cp_pedwarn (IDENTIFIER_IMPLICIT_DECL (name)
3108 ? implicit_extern_static_warning
3109 : explicit_extern_static_warning, newdecl);
3110 cp_pedwarn_at ("previous declaration of `%D'", olddecl);
8d08fdba
MS
3111}
3112
3113/* Handle when a new declaration NEWDECL has the same name as an old
3114 one OLDDECL in the same binding contour. Prints an error message
3115 if appropriate.
3116
3117 If safely possible, alter OLDDECL to look like NEWDECL, and return 1.
3118 Otherwise, return 0. */
3119
51c184be 3120int
8d08fdba 3121duplicate_decls (newdecl, olddecl)
824b9a4c 3122 tree newdecl, olddecl;
8d08fdba 3123{
8d08fdba
MS
3124 unsigned olddecl_uid = DECL_UID (olddecl);
3125 int olddecl_friend = 0, types_match = 0;
0b60dfe3 3126 int new_defines_function = 0;
5566b478
MS
3127
3128 if (newdecl == olddecl)
3129 return 1;
8d08fdba 3130
8926095f 3131 types_match = decls_match (newdecl, olddecl);
8d08fdba 3132
8d08fdba
MS
3133 /* If either the type of the new decl or the type of the old decl is an
3134 error_mark_node, then that implies that we have already issued an
3135 error (earlier) for some bogus type specification, and in that case,
3136 it is rather pointless to harass the user with yet more error message
0b60dfe3 3137 about the same declaration, so just pretend the types match here. */
bd6dd845
MS
3138 if (TREE_TYPE (newdecl) == error_mark_node
3139 || TREE_TYPE (olddecl) == error_mark_node)
8d08fdba 3140 types_match = 1;
68642fb6 3141
0b60dfe3 3142 /* Check for redeclaration and other discrepancies. */
d22c8596 3143 if (TREE_CODE (olddecl) == FUNCTION_DECL
9f33663b
JM
3144 && DECL_ARTIFICIAL (olddecl))
3145 {
3146 if (TREE_CODE (newdecl) != FUNCTION_DECL)
3147 {
3148 /* If you declare a built-in or predefined function name as static,
3149 the old definition is overridden, but optionally warn this was a
3150 bad choice of name. */
3151 if (! TREE_PUBLIC (newdecl))
3152 {
3153 if (warn_shadow)
3154 cp_warning ("shadowing %s function `%#D'",
3155 DECL_BUILT_IN (olddecl) ? "built-in" : "library",
3156 olddecl);
3157 /* Discard the old built-in function. */
3158 return 0;
3159 }
3160 /* If the built-in is not ansi, then programs can override
3161 it even globally without an error. */
3162 else if (! DECL_BUILT_IN (olddecl))
3163 cp_warning ("library function `%#D' redeclared as non-function `%#D'",
3164 olddecl, newdecl);
3165 else
3166 {
3167 cp_error ("declaration of `%#D'", newdecl);
3168 cp_error ("conflicts with built-in declaration `%#D'",
a4443a08 3169 olddecl);
9f33663b 3170 }
a4443a08
MS
3171 return 0;
3172 }
9f33663b 3173 else if (!types_match)
8d08fdba 3174 {
eb68cb58
MM
3175 if ((DECL_EXTERN_C_P (newdecl)
3176 && DECL_EXTERN_C_P (olddecl))
9f33663b
JM
3177 || compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
3178 TYPE_ARG_TYPES (TREE_TYPE (olddecl))))
a4443a08 3179 {
9f33663b
JM
3180 /* A near match; override the builtin. */
3181
3182 if (TREE_PUBLIC (newdecl))
a4443a08 3183 {
9f33663b
JM
3184 cp_warning ("new declaration `%#D'", newdecl);
3185 cp_warning ("ambiguates built-in declaration `%#D'",
3186 olddecl);
a4443a08 3187 }
9f33663b
JM
3188 else if (warn_shadow)
3189 cp_warning ("shadowing %s function `%#D'",
3190 DECL_BUILT_IN (olddecl) ? "built-in" : "library",
3191 olddecl);
a4443a08 3192 }
9f33663b
JM
3193 else
3194 /* Discard the old built-in function. */
3195 return 0;
8d08fdba 3196 }
68642fb6 3197
24f30ed4
MM
3198 if (DECL_THIS_STATIC (newdecl) && !DECL_THIS_STATIC (olddecl))
3199 {
3200 /* If a builtin function is redeclared as `static', merge
3201 the declarations, but make the original one static. */
3202 DECL_THIS_STATIC (olddecl) = 1;
3203 TREE_PUBLIC (olddecl) = 0;
9bfadf57 3204
421844e7
MM
3205 /* Make the old declaration consistent with the new one so
3206 that all remnants of the builtin-ness of this function
3207 will be banished. */
9bfadf57 3208 DECL_LANGUAGE (olddecl) = DECL_LANGUAGE (newdecl);
19e7881c 3209 SET_DECL_RTL (olddecl, DECL_RTL (newdecl));
92643fea 3210 COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
9bfadf57
MM
3211 SET_IDENTIFIER_GLOBAL_VALUE (DECL_ASSEMBLER_NAME (newdecl),
3212 newdecl);
24f30ed4 3213 }
39211cd5
MS
3214 }
3215 else if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
3216 {
9ed182dc
JM
3217 if ((TREE_CODE (olddecl) == TYPE_DECL && DECL_ARTIFICIAL (olddecl)
3218 && TREE_CODE (newdecl) != TYPE_DECL
3219 && ! (TREE_CODE (newdecl) == TEMPLATE_DECL
3220 && TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL))
3221 || (TREE_CODE (newdecl) == TYPE_DECL && DECL_ARTIFICIAL (newdecl)
3222 && TREE_CODE (olddecl) != TYPE_DECL
3223 && ! (TREE_CODE (olddecl) == TEMPLATE_DECL
3224 && (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl))
3225 == TYPE_DECL))))
3226 {
3227 /* We do nothing special here, because C++ does such nasty
3228 things with TYPE_DECLs. Instead, just let the TYPE_DECL
3229 get shadowed, and know that if we need to find a TYPE_DECL
3230 for a given name, we can look in the IDENTIFIER_TYPE_VALUE
3231 slot of the identifier. */
3232 return 0;
3233 }
3234
39211cd5 3235 if ((TREE_CODE (newdecl) == FUNCTION_DECL
5566b478 3236 && DECL_FUNCTION_TEMPLATE_P (olddecl))
39211cd5 3237 || (TREE_CODE (olddecl) == FUNCTION_DECL
5566b478 3238 && DECL_FUNCTION_TEMPLATE_P (newdecl)))
39211cd5 3239 return 0;
9ed182dc 3240
8251199e 3241 cp_error ("`%#D' redeclared as different kind of symbol", newdecl);
39211cd5
MS
3242 if (TREE_CODE (olddecl) == TREE_LIST)
3243 olddecl = TREE_VALUE (olddecl);
8251199e 3244 cp_error_at ("previous declaration of `%#D'", olddecl);
39211cd5
MS
3245
3246 /* New decl is completely inconsistent with the old one =>
3247 tell caller to replace the old one. */
3248
3249 return 0;
8d08fdba 3250 }
8d08fdba
MS
3251 else if (!types_match)
3252 {
4f1c5b7d 3253 if (CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl))
7bdbfa05
MM
3254 /* These are certainly not duplicate declarations; they're
3255 from different scopes. */
3256 return 0;
3257
8926095f 3258 if (TREE_CODE (newdecl) == TEMPLATE_DECL)
f0e01782
MS
3259 {
3260 /* The name of a class template may not be declared to refer to
3261 any other template, class, function, object, namespace, value,
e92cc029 3262 or type in the same scope. */
5566b478
MS
3263 if (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) == TYPE_DECL
3264 || TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL)
f0e01782 3265 {
8251199e
JM
3266 cp_error ("declaration of template `%#D'", newdecl);
3267 cp_error_at ("conflicts with previous declaration `%#D'",
f0e01782
MS
3268 olddecl);
3269 }
ec255269
MS
3270 else if (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) == FUNCTION_DECL
3271 && TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == FUNCTION_DECL
3272 && compparms (TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (olddecl))),
91063b51 3273 TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (newdecl))))
75650646
MM
3274 && comp_template_parms (DECL_TEMPLATE_PARMS (newdecl),
3275 DECL_TEMPLATE_PARMS (olddecl)))
ec255269 3276 {
8251199e
JM
3277 cp_error ("new declaration `%#D'", newdecl);
3278 cp_error_at ("ambiguates old declaration `%#D'", olddecl);
ec255269 3279 }
f0e01782
MS
3280 return 0;
3281 }
8926095f
MS
3282 if (TREE_CODE (newdecl) == FUNCTION_DECL)
3283 {
eb68cb58 3284 if (DECL_EXTERN_C_P (newdecl) && DECL_EXTERN_C_P (olddecl))
8926095f 3285 {
8251199e 3286 cp_error ("declaration of C function `%#D' conflicts with",
8926095f 3287 newdecl);
8251199e 3288 cp_error_at ("previous declaration `%#D' here", olddecl);
8926095f 3289 }
00595019 3290 else if (compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
91063b51 3291 TYPE_ARG_TYPES (TREE_TYPE (olddecl))))
700f8a87 3292 {
8251199e
JM
3293 cp_error ("new declaration `%#D'", newdecl);
3294 cp_error_at ("ambiguates old declaration `%#D'", olddecl);
700f8a87
MS
3295 }
3296 else
3297 return 0;
8926095f 3298 }
8d08fdba
MS
3299
3300 /* Already complained about this, so don't do so again. */
a4443a08 3301 else if (current_class_type == NULL_TREE
8d08fdba
MS
3302 || IDENTIFIER_ERROR_LOCUS (DECL_ASSEMBLER_NAME (newdecl)) != current_class_type)
3303 {
8251199e
JM
3304 cp_error ("conflicting types for `%#D'", newdecl);
3305 cp_error_at ("previous declaration as `%#D'", olddecl);
8d08fdba
MS
3306 }
3307 }
68642fb6 3308 else if (TREE_CODE (newdecl) == FUNCTION_DECL
75650646
MM
3309 && ((DECL_TEMPLATE_SPECIALIZATION (olddecl)
3310 && (!DECL_TEMPLATE_INFO (newdecl)
68642fb6 3311 || (DECL_TI_TEMPLATE (newdecl)
75650646
MM
3312 != DECL_TI_TEMPLATE (olddecl))))
3313 || (DECL_TEMPLATE_SPECIALIZATION (newdecl)
3314 && (!DECL_TEMPLATE_INFO (olddecl)
68642fb6 3315 || (DECL_TI_TEMPLATE (olddecl)
75650646 3316 != DECL_TI_TEMPLATE (newdecl))))))
386b8a85
JM
3317 /* It's OK to have a template specialization and a non-template
3318 with the same type, or to have specializations of two
75650646
MM
3319 different templates with the same type. Note that if one is a
3320 specialization, and the other is an instantiation of the same
3321 template, that we do not exit at this point. That situation
3322 can occur if we instantiate a template class, and then
3323 specialize one of its methods. This situation is legal, but
3324 the declarations must be merged in the usual way. */
3325 return 0;
68642fb6
UD
3326 else if (TREE_CODE (newdecl) == FUNCTION_DECL
3327 && ((DECL_TEMPLATE_INSTANTIATION (olddecl)
75650646
MM
3328 && !DECL_USE_TEMPLATE (newdecl))
3329 || (DECL_TEMPLATE_INSTANTIATION (newdecl)
3330 && !DECL_USE_TEMPLATE (olddecl))))
3331 /* One of the declarations is a template instantiation, and the
3332 other is not a template at all. That's OK. */
386b8a85 3333 return 0;
85c6cbaf
ML
3334 else if (TREE_CODE (newdecl) == NAMESPACE_DECL
3335 && DECL_NAMESPACE_ALIAS (newdecl)
3336 && DECL_NAMESPACE_ALIAS (newdecl) == DECL_NAMESPACE_ALIAS (olddecl))
3337 /* Redeclaration of namespace alias, ignore it. */
3338 return 1;
8d08fdba
MS
3339 else
3340 {
d8e178a0 3341 const char *errmsg = redeclaration_error_message (newdecl, olddecl);
8251199e 3342 if (errmsg)
8d08fdba 3343 {
8251199e 3344 cp_error (errmsg, newdecl);
8d08fdba
MS
3345 if (DECL_NAME (olddecl) != NULL_TREE)
3346 cp_error_at ((DECL_INITIAL (olddecl)
2c73f9f5 3347 && namespace_bindings_p ())
8251199e
JM
3348 ? "`%#D' previously defined here"
3349 : "`%#D' previously declared here", olddecl);
8d08fdba
MS
3350 }
3351 else if (TREE_CODE (olddecl) == FUNCTION_DECL
3352 && DECL_INITIAL (olddecl) != NULL_TREE
3353 && TYPE_ARG_TYPES (TREE_TYPE (olddecl)) == NULL_TREE
3354 && TYPE_ARG_TYPES (TREE_TYPE (newdecl)) != NULL_TREE)
3355 {
3356 /* Prototype decl follows defn w/o prototype. */
8251199e
JM
3357 cp_warning_at ("prototype for `%#D'", newdecl);
3358 cp_warning_at ("follows non-prototype definition here", olddecl);
8d08fdba
MS
3359 }
3360 else if (TREE_CODE (olddecl) == FUNCTION_DECL
3361 && DECL_LANGUAGE (newdecl) != DECL_LANGUAGE (olddecl))
8926095f
MS
3362 {
3363 /* extern "C" int foo ();
3364 int foo () { bar (); }
3365 is OK. */
46ccf50a 3366 if (current_lang_depth () == 0)
a28e3c7f 3367 DECL_LANGUAGE (newdecl) = DECL_LANGUAGE (olddecl);
8926095f
MS
3368 else
3369 {
8251199e 3370 cp_error_at ("previous declaration of `%#D' with %L linkage",
8926095f 3371 olddecl, DECL_LANGUAGE (olddecl));
8251199e 3372 cp_error ("conflicts with new declaration with %L linkage",
8926095f
MS
3373 DECL_LANGUAGE (newdecl));
3374 }
3375 }
e1cd6e56 3376
a6f02587 3377 if (DECL_LANG_SPECIFIC (olddecl) && DECL_USE_TEMPLATE (olddecl))
c32381b1
JM
3378 ;
3379 else if (TREE_CODE (olddecl) == FUNCTION_DECL)
e1cd6e56
MS
3380 {
3381 tree t1 = TYPE_ARG_TYPES (TREE_TYPE (olddecl));
3382 tree t2 = TYPE_ARG_TYPES (TREE_TYPE (newdecl));
3383 int i = 1;
3384
3385 if (TREE_CODE (TREE_TYPE (newdecl)) == METHOD_TYPE)
3386 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2);
68642fb6 3387
e1cd6e56
MS
3388 for (; t1 && t1 != void_list_node;
3389 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2), i++)
3390 if (TREE_PURPOSE (t1) && TREE_PURPOSE (t2))
3391 {
2507f3b5
RK
3392 if (1 == simple_cst_equal (TREE_PURPOSE (t1),
3393 TREE_PURPOSE (t2)))
e1cd6e56
MS
3394 {
3395 if (pedantic)
3396 {
8251199e 3397 cp_pedwarn ("default argument given for parameter %d of `%#D'",
e1cd6e56 3398 i, newdecl);
8251199e 3399 cp_pedwarn_at ("after previous specification in `%#D'",
e1cd6e56
MS
3400 olddecl);
3401 }
3402 }
3403 else
3404 {
8251199e 3405 cp_error ("default argument given for parameter %d of `%#D'",
e1cd6e56 3406 i, newdecl);
8251199e 3407 cp_error_at ("after previous specification in `%#D'",
e1cd6e56
MS
3408 olddecl);
3409 }
3410 }
a5894242 3411
7fcdf4c2
MS
3412 if (DECL_THIS_INLINE (newdecl) && ! DECL_THIS_INLINE (olddecl)
3413 && TREE_ADDRESSABLE (olddecl) && warn_inline)
dff6b454 3414 {
8251199e 3415 cp_warning ("`%#D' was used before it was declared inline",
7fcdf4c2 3416 newdecl);
8251199e 3417 cp_warning_at ("previous non-inline declaration here",
7fcdf4c2 3418 olddecl);
dff6b454 3419 }
e1cd6e56 3420 }
8d08fdba
MS
3421 }
3422
3423 /* If new decl is `static' and an `extern' was seen previously,
3424 warn about it. */
3425 warn_extern_redeclared_static (newdecl, olddecl);
3426
e92cc029 3427 /* We have committed to returning 1 at this point. */
8d08fdba
MS
3428 if (TREE_CODE (newdecl) == FUNCTION_DECL)
3429 {
3430 /* Now that functions must hold information normally held
3431 by field decls, there is extra work to do so that
3432 declaration information does not get destroyed during
3433 definition. */
3434 if (DECL_VINDEX (olddecl))
3435 DECL_VINDEX (newdecl) = DECL_VINDEX (olddecl);
4f1c5b7d
MM
3436 if (DECL_VIRTUAL_CONTEXT (olddecl))
3437 DECL_VIRTUAL_CONTEXT (newdecl) = DECL_VIRTUAL_CONTEXT (olddecl);
8d08fdba
MS
3438 if (DECL_CONTEXT (olddecl))
3439 DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
0e5921e8 3440 if (DECL_PENDING_INLINE_INFO (newdecl) == 0)
8d08fdba 3441 DECL_PENDING_INLINE_INFO (newdecl) = DECL_PENDING_INLINE_INFO (olddecl);
e1cd6e56
MS
3442 DECL_STATIC_CONSTRUCTOR (newdecl) |= DECL_STATIC_CONSTRUCTOR (olddecl);
3443 DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
fee7654e 3444 DECL_PURE_VIRTUAL_P (newdecl) |= DECL_PURE_VIRTUAL_P (olddecl);
864b83b9 3445 DECL_VIRTUAL_P (newdecl) |= DECL_VIRTUAL_P (olddecl);
4a67c9e9 3446 DECL_NEEDS_FINAL_OVERRIDER_P (newdecl) |= DECL_NEEDS_FINAL_OVERRIDER_P (olddecl);
963d5758 3447 DECL_THIS_STATIC (newdecl) |= DECL_THIS_STATIC (olddecl);
e0fff4b3
JM
3448 if (DECL_OVERLOADED_OPERATOR_P (olddecl) != ERROR_MARK)
3449 SET_OVERLOADED_OPERATOR_CODE
3450 (newdecl, DECL_OVERLOADED_OPERATOR_P (olddecl));
0b60dfe3 3451 new_defines_function = DECL_INITIAL (newdecl) != NULL_TREE;
68642fb6 3452
0b60dfe3
BK
3453 /* Optionally warn about more than one declaration for the same
3454 name, but don't warn about a function declaration followed by a
3455 definition. */
3456 if (warn_redundant_decls && ! DECL_ARTIFICIAL (olddecl)
3457 && !(new_defines_function && DECL_INITIAL (olddecl) == NULL_TREE)
3458 /* Don't warn about extern decl followed by definition. */
3459 && !(DECL_EXTERNAL (olddecl) && ! DECL_EXTERNAL (newdecl))
3460 /* Don't warn about friends, let add_friend take care of it. */
3461 && ! DECL_FRIEND_P (newdecl))
3462 {
8251199e
JM
3463 cp_warning ("redundant redeclaration of `%D' in same scope", newdecl);
3464 cp_warning_at ("previous declaration of `%D'", olddecl);
0b60dfe3 3465 }
8d08fdba
MS
3466 }
3467
3468 /* Deal with C++: must preserve virtual function table size. */
3469 if (TREE_CODE (olddecl) == TYPE_DECL)
3470 {
3471 register tree newtype = TREE_TYPE (newdecl);
3472 register tree oldtype = TREE_TYPE (olddecl);
3473
3474 if (newtype != error_mark_node && oldtype != error_mark_node
3475 && TYPE_LANG_SPECIFIC (newtype) && TYPE_LANG_SPECIFIC (oldtype))
3476 {
3477 CLASSTYPE_VSIZE (newtype) = CLASSTYPE_VSIZE (oldtype);
3478 CLASSTYPE_FRIEND_CLASSES (newtype)
3479 = CLASSTYPE_FRIEND_CLASSES (oldtype);
3480 }
970d6386
MM
3481
3482 DECL_ORIGINAL_TYPE (newdecl) = DECL_ORIGINAL_TYPE (olddecl);
8d08fdba
MS
3483 }
3484
8d08fdba
MS
3485 /* Copy all the DECL_... slots specified in the new decl
3486 except for any that we copy here from the old type. */
68642fb6 3487 DECL_MACHINE_ATTRIBUTES (newdecl)
0b60dfe3 3488 = merge_machine_decl_attributes (olddecl, newdecl);
8d08fdba 3489
5566b478
MS
3490 if (TREE_CODE (newdecl) == TEMPLATE_DECL)
3491 {
4d85e00e 3492 TREE_TYPE (olddecl) = TREE_TYPE (DECL_TEMPLATE_RESULT (olddecl));
68642fb6 3493 DECL_TEMPLATE_SPECIALIZATIONS (olddecl)
4d85e00e
MM
3494 = chainon (DECL_TEMPLATE_SPECIALIZATIONS (olddecl),
3495 DECL_TEMPLATE_SPECIALIZATIONS (newdecl));
68642fb6 3496
5566b478
MS
3497 return 1;
3498 }
68642fb6 3499
8d08fdba
MS
3500 if (types_match)
3501 {
3502 /* Automatically handles default parameters. */
3503 tree oldtype = TREE_TYPE (olddecl);
e1cd6e56 3504 tree newtype;
8d08fdba 3505
e1cd6e56
MS
3506 /* Merge the data types specified in the two decls. */
3507 newtype = common_type (TREE_TYPE (newdecl), TREE_TYPE (olddecl));
3508
9076e292
JM
3509 /* If common_type produces a non-typedef type, just use the old type. */
3510 if (TREE_CODE (newdecl) == TYPE_DECL
3511 && newtype == DECL_ORIGINAL_TYPE (newdecl))
3512 newtype = oldtype;
3513
8d08fdba
MS
3514 if (TREE_CODE (newdecl) == VAR_DECL)
3515 DECL_THIS_EXTERN (newdecl) |= DECL_THIS_EXTERN (olddecl);
3516 /* Do this after calling `common_type' so that default
3517 parameters don't confuse us. */
3518 else if (TREE_CODE (newdecl) == FUNCTION_DECL
3519 && (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (newdecl))
3520 != TYPE_RAISES_EXCEPTIONS (TREE_TYPE (olddecl))))
3521 {
f30432d7 3522 TREE_TYPE (newdecl) = build_exception_variant (newtype,
8d08fdba 3523 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (newdecl)));
f30432d7 3524 TREE_TYPE (olddecl) = build_exception_variant (newtype,
8d08fdba
MS
3525 TYPE_RAISES_EXCEPTIONS (oldtype));
3526
9a224b4a
JM
3527 if ((pedantic || ! DECL_IN_SYSTEM_HEADER (olddecl))
3528 && DECL_SOURCE_LINE (olddecl) != 0
da20811c 3529 && flag_exceptions
4cc1d462
NS
3530 && !comp_except_specs (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (newdecl)),
3531 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (olddecl)), 1))
8d08fdba 3532 {
4cc1d462 3533 cp_error ("declaration of `%F' throws different exceptions",
a28e3c7f 3534 newdecl);
a09ba2e0 3535 cp_error_at ("than previous declaration `%F'", olddecl);
8d08fdba
MS
3536 }
3537 }
3538 TREE_TYPE (newdecl) = TREE_TYPE (olddecl) = newtype;
3539
3540 /* Lay the type out, unless already done. */
407f03b8 3541 if (! same_type_p (newtype, oldtype)
5566b478 3542 && TREE_TYPE (newdecl) != error_mark_node
5156628f 3543 && !(processing_template_decl && uses_template_parms (newdecl)))
b7484fbe
MS
3544 layout_type (TREE_TYPE (newdecl));
3545
5566b478
MS
3546 if ((TREE_CODE (newdecl) == VAR_DECL
3547 || TREE_CODE (newdecl) == PARM_DECL
3548 || TREE_CODE (newdecl) == RESULT_DECL
3549 || TREE_CODE (newdecl) == FIELD_DECL
3550 || TREE_CODE (newdecl) == TYPE_DECL)
5156628f 3551 && !(processing_template_decl && uses_template_parms (newdecl)))
b7484fbe 3552 layout_decl (newdecl, 0);
8d08fdba
MS
3553
3554 /* Merge the type qualifiers. */
3555 if (TREE_READONLY (newdecl))
3556 TREE_READONLY (olddecl) = 1;
3557 if (TREE_THIS_VOLATILE (newdecl))
3558 TREE_THIS_VOLATILE (olddecl) = 1;
3559
3560 /* Merge the initialization information. */
8926095f
MS
3561 if (DECL_INITIAL (newdecl) == NULL_TREE
3562 && DECL_INITIAL (olddecl) != NULL_TREE)
3563 {
3564 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
3565 DECL_SOURCE_FILE (newdecl) = DECL_SOURCE_FILE (olddecl);
3566 DECL_SOURCE_LINE (newdecl) = DECL_SOURCE_LINE (olddecl);
b0d06515
MM
3567 if (CAN_HAVE_FULL_LANG_DECL_P (newdecl)
3568 && DECL_LANG_SPECIFIC (newdecl)
4d85e00e
MM
3569 && DECL_LANG_SPECIFIC (olddecl))
3570 DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
8926095f 3571 }
39211cd5
MS
3572
3573 /* Merge the section attribute.
3574 We want to issue an error if the sections conflict but that must be
3575 done later in decl_attributes since we are called before attributes
3576 are assigned. */
3577 if (DECL_SECTION_NAME (newdecl) == NULL_TREE)
3578 DECL_SECTION_NAME (newdecl) = DECL_SECTION_NAME (olddecl);
3579
5d73aa63 3580 /* Keep the old rtl since we can safely use it. */
19e7881c 3581 COPY_DECL_RTL (olddecl, newdecl);
a157febd
GK
3582
3583 if (TREE_CODE (newdecl) == FUNCTION_DECL)
3584 {
3585 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
3586 |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
3587 DECL_NO_CHECK_MEMORY_USAGE (newdecl)
3588 |= DECL_NO_CHECK_MEMORY_USAGE (olddecl);
3589 DECL_NO_LIMIT_STACK (newdecl)
3590 |= DECL_NO_LIMIT_STACK (olddecl);
3591 }
8d08fdba
MS
3592 }
3593 /* If cannot merge, then use the new type and qualifiers,
3594 and don't preserve the old rtl. */
3595 else
3596 {
3597 /* Clean out any memory we had of the old declaration. */
3598 tree oldstatic = value_member (olddecl, static_aggregates);
3599 if (oldstatic)
3600 TREE_VALUE (oldstatic) = error_mark_node;
3601
3602 TREE_TYPE (olddecl) = TREE_TYPE (newdecl);
3603 TREE_READONLY (olddecl) = TREE_READONLY (newdecl);
3604 TREE_THIS_VOLATILE (olddecl) = TREE_THIS_VOLATILE (newdecl);
3605 TREE_SIDE_EFFECTS (olddecl) = TREE_SIDE_EFFECTS (newdecl);
3606 }
3607
3608 /* Merge the storage class information. */
a9aedbc2 3609 DECL_WEAK (newdecl) |= DECL_WEAK (olddecl);
e92cc029 3610 DECL_ONE_ONLY (newdecl) |= DECL_ONE_ONLY (olddecl);
794d4a61 3611 DECL_DEFER_OUTPUT (newdecl) |= DECL_DEFER_OUTPUT (olddecl);
a9aedbc2
MS
3612 TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
3613 TREE_STATIC (olddecl) = TREE_STATIC (newdecl) |= TREE_STATIC (olddecl);
3614 if (! DECL_EXTERNAL (olddecl))
3615 DECL_EXTERNAL (newdecl) = 0;
68642fb6 3616
0b60dfe3 3617 if (DECL_LANG_SPECIFIC (newdecl) && DECL_LANG_SPECIFIC (olddecl))
8d08fdba 3618 {
a9aedbc2
MS
3619 DECL_INTERFACE_KNOWN (newdecl) |= DECL_INTERFACE_KNOWN (olddecl);
3620 DECL_NOT_REALLY_EXTERN (newdecl) |= DECL_NOT_REALLY_EXTERN (olddecl);
77be6f82 3621 DECL_COMDAT (newdecl) |= DECL_COMDAT (olddecl);
68642fb6 3622 DECL_TEMPLATE_INSTANTIATED (newdecl)
2b0a63a3 3623 |= DECL_TEMPLATE_INSTANTIATED (olddecl);
0b60dfe3
BK
3624 /* Don't really know how much of the language-specific
3625 values we should copy from old to new. */
3626 DECL_IN_AGGR_P (newdecl) = DECL_IN_AGGR_P (olddecl);
3627 DECL_ACCESS (newdecl) = DECL_ACCESS (olddecl);
3628 DECL_NONCONVERTING_P (newdecl) = DECL_NONCONVERTING_P (olddecl);
fbf1c34b 3629 DECL_TEMPLATE_INFO (newdecl) = DECL_TEMPLATE_INFO (olddecl);
3febd123
NS
3630 DECL_INITIALIZED_IN_CLASS_P (newdecl)
3631 |= DECL_INITIALIZED_IN_CLASS_P (olddecl);
0b60dfe3 3632 olddecl_friend = DECL_FRIEND_P (olddecl);
6a629cac
MM
3633
3634 /* Only functions have DECL_BEFRIENDING_CLASSES. */
3635 if (TREE_CODE (newdecl) == FUNCTION_DECL
3636 || DECL_FUNCTION_TEMPLATE_P (newdecl))
3637 DECL_BEFRIENDING_CLASSES (newdecl)
3638 = chainon (DECL_BEFRIENDING_CLASSES (newdecl),
3639 DECL_BEFRIENDING_CLASSES (olddecl));
8d08fdba
MS
3640 }
3641
8d08fdba
MS
3642 if (TREE_CODE (newdecl) == FUNCTION_DECL)
3643 {
68642fb6
UD
3644 if (DECL_TEMPLATE_INSTANTIATION (olddecl)
3645 && !DECL_TEMPLATE_INSTANTIATION (newdecl))
75650646
MM
3646 {
3647 /* If newdecl is not a specialization, then it is not a
3648 template-related function at all. And that means that we
3649 shoud have exited above, returning 0. */
3650 my_friendly_assert (DECL_TEMPLATE_SPECIALIZATION (newdecl),
3651 0);
3652
68642fb6 3653 if (TREE_USED (olddecl))
75650646 3654 /* From [temp.expl.spec]:
68642fb6 3655
75650646
MM
3656 If a template, a member template or the member of a class
3657 template is explicitly specialized then that
3658 specialization shall be declared before the first use of
3659 that specialization that would cause an implicit
3660 instantiation to take place, in every translation unit in
3661 which such a use occurs. */
68642fb6 3662 cp_error ("explicit specialization of %D after first use",
75650646
MM
3663 olddecl);
3664
3665 SET_DECL_TEMPLATE_SPECIALIZATION (olddecl);
3666 }
faae18ab
MS
3667 DECL_THIS_INLINE (newdecl) |= DECL_THIS_INLINE (olddecl);
3668
3669 /* If either decl says `inline', this fn is inline, unless its
3670 definition was passed already. */
3671 if (DECL_INLINE (newdecl) && DECL_INITIAL (olddecl) == NULL_TREE)
3672 DECL_INLINE (olddecl) = 1;
3673 DECL_INLINE (newdecl) = DECL_INLINE (olddecl);
3674
5daf7c0a
JM
3675 /* Preserve abstractness on cloned [cd]tors. */
3676 DECL_ABSTRACT (newdecl) = DECL_ABSTRACT (olddecl);
3677
700f8a87
MS
3678 if (! types_match)
3679 {
3680 DECL_LANGUAGE (olddecl) = DECL_LANGUAGE (newdecl);
92643fea 3681 COPY_DECL_ASSEMBLER_NAME (newdecl, olddecl);
19e7881c 3682 SET_DECL_RTL (olddecl, DECL_RTL (newdecl));
5566b478
MS
3683 }
3684 if (! types_match || new_defines_function)
3685 {
6f1b4c42
JM
3686 /* These need to be copied so that the names are available.
3687 Note that if the types do match, we'll preserve inline
3688 info and other bits, but if not, we won't. */
700f8a87
MS
3689 DECL_ARGUMENTS (olddecl) = DECL_ARGUMENTS (newdecl);
3690 DECL_RESULT (olddecl) = DECL_RESULT (newdecl);
700f8a87 3691 }
8d08fdba
MS
3692 if (new_defines_function)
3693 /* If defining a function declared with other language
3694 linkage, use the previously declared language linkage. */
3695 DECL_LANGUAGE (newdecl) = DECL_LANGUAGE (olddecl);
6f1b4c42 3696 else if (types_match)
8d08fdba
MS
3697 {
3698 /* If redeclaring a builtin function, and not a definition,
3699 it stays built in. */
3700 if (DECL_BUILT_IN (olddecl))
3701 {
26db82d8 3702 DECL_BUILT_IN_CLASS (newdecl) = DECL_BUILT_IN_CLASS (olddecl);
39211cd5 3703 DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl);
8d08fdba
MS
3704 /* If we're keeping the built-in definition, keep the rtl,
3705 regardless of declaration matches. */
19e7881c 3706 SET_DECL_RTL (newdecl, DECL_RTL (olddecl));
8d08fdba
MS
3707 }
3708 else
b850de4f 3709 DECL_NUM_STMTS (newdecl) = DECL_NUM_STMTS (olddecl);
8d08fdba
MS
3710
3711 DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
8926095f 3712 if ((DECL_SAVED_INSNS (newdecl) = DECL_SAVED_INSNS (olddecl)))
8d08fdba
MS
3713 /* Previously saved insns go together with
3714 the function's previous definition. */
3715 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
3716 /* Don't clear out the arguments if we're redefining a function. */
3717 if (DECL_ARGUMENTS (olddecl))
3718 DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl);
3719 }
3720 }
3721
a9aedbc2
MS
3722 if (TREE_CODE (newdecl) == NAMESPACE_DECL)
3723 {
3724 NAMESPACE_LEVEL (newdecl) = NAMESPACE_LEVEL (olddecl);
3725 }
3726
8d08fdba
MS
3727 /* Now preserve various other info from the definition. */
3728 TREE_ADDRESSABLE (newdecl) = TREE_ADDRESSABLE (olddecl);
3729 TREE_ASM_WRITTEN (newdecl) = TREE_ASM_WRITTEN (olddecl);
3730 DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
92643fea 3731 COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
8d08fdba 3732
8d08fdba
MS
3733 if (TREE_CODE (newdecl) == FUNCTION_DECL)
3734 {
3735 int function_size;
8d08fdba
MS
3736
3737 function_size = sizeof (struct tree_decl);
3738
730e1556
KG
3739 memcpy ((char *) olddecl + sizeof (struct tree_common),
3740 (char *) newdecl + sizeof (struct tree_common),
3741 function_size - sizeof (struct tree_common));
8d08fdba 3742
75650646
MM
3743 if (DECL_TEMPLATE_INSTANTIATION (newdecl))
3744 {
3745 /* If newdecl is a template instantiation, it is possible that
3746 the following sequence of events has occurred:
3747
3748 o A friend function was declared in a class template. The
68642fb6 3749 class template was instantiated.
75650646 3750
68642fb6
UD
3751 o The instantiation of the friend declaration was
3752 recorded on the instantiation list, and is newdecl.
75650646
MM
3753
3754 o Later, however, instantiate_class_template called pushdecl
3755 on the newdecl to perform name injection. But, pushdecl in
3756 turn called duplicate_decls when it discovered that another
3757 declaration of a global function with the same name already
68642fb6 3758 existed.
75650646
MM
3759
3760 o Here, in duplicate_decls, we decided to clobber newdecl.
3761
3762 If we're going to do that, we'd better make sure that
3763 olddecl, and not newdecl, is on the list of
3764 instantiations so that if we try to do the instantiation
3765 again we won't get the clobbered declaration. */
3766
68642fb6
UD
3767 tree tmpl = DECL_TI_TEMPLATE (newdecl);
3768 tree decls = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
75650646
MM
3769
3770 for (; decls; decls = TREE_CHAIN (decls))
3771 if (TREE_VALUE (decls) == newdecl)
3772 TREE_VALUE (decls) = olddecl;
3773 }
8d08fdba
MS
3774 }
3775 else
3776 {
730e1556
KG
3777 memcpy ((char *) olddecl + sizeof (struct tree_common),
3778 (char *) newdecl + sizeof (struct tree_common),
3779 sizeof (struct tree_decl) - sizeof (struct tree_common)
3780 + tree_code_length [(int)TREE_CODE (newdecl)] * sizeof (char *));
8d08fdba
MS
3781 }
3782
3783 DECL_UID (olddecl) = olddecl_uid;
3784 if (olddecl_friend)
3785 DECL_FRIEND_P (olddecl) = 1;
3786
d9525bec
BK
3787 /* NEWDECL contains the merged attribute lists.
3788 Update OLDDECL to be the same. */
3789 DECL_MACHINE_ATTRIBUTES (olddecl) = DECL_MACHINE_ATTRIBUTES (newdecl);
3790
8d08fdba
MS
3791 return 1;
3792}
3793
3794/* Record a decl-node X as belonging to the current lexical scope.
3795 Check for errors (such as an incompatible declaration for the same
3796 name already seen in the same scope).
3797
3798 Returns either X or an old decl for the same name.
3799 If an old decl is returned, it may have been smashed
3800 to agree with what X says. */
3801
3802tree
3803pushdecl (x)
3804 tree x;
3805{
3806 register tree t;
b35d4555
MM
3807 register tree name;
3808 int need_new_binding;
3809
3810 /* We shouldn't be calling pushdecl when we're generating RTL for a
3811 function that we already did semantic analysis on previously. */
01d939e8 3812 my_friendly_assert (!cfun || doing_semantic_analysis_p (),
b35d4555
MM
3813 19990913);
3814
b35d4555 3815 need_new_binding = 1;
8d08fdba 3816
50714e79
MM
3817 if (DECL_TEMPLATE_PARM_P (x))
3818 /* Template parameters have no context; they are not X::T even
3819 when declared within a class or namespace. */
3820 ;
3821 else
3822 {
3823 if (current_function_decl && x != current_function_decl
3824 /* A local declaration for a function doesn't constitute
3825 nesting. */
f39ee884
MM
3826 && !(TREE_CODE (x) == FUNCTION_DECL && !DECL_INITIAL (x))
3827 /* A local declaration for an `extern' variable is in the
94dfccd1 3828 scope of the current namespace, not the current
f39ee884
MM
3829 function. */
3830 && !(TREE_CODE (x) == VAR_DECL && DECL_EXTERNAL (x))
50714e79
MM
3831 && !DECL_CONTEXT (x))
3832 DECL_CONTEXT (x) = current_function_decl;
cd9f6678
MM
3833
3834 /* If this is the declaration for a namespace-scope function,
3835 but the declaration itself is in a local scope, mark the
3836 declaration. */
68642fb6 3837 if (TREE_CODE (x) == FUNCTION_DECL
cd9f6678
MM
3838 && DECL_NAMESPACE_SCOPE_P (x)
3839 && current_function_decl
3840 && x != current_function_decl)
3841 DECL_LOCAL_FUNCTION_P (x) = 1;
50714e79 3842 }
8d08fdba 3843
fe8fadc1 3844 name = DECL_NAME (x);
8d08fdba
MS
3845 if (name)
3846 {
94dfccd1
JM
3847 int different_binding_level = 0;
3848
386b8a85
JM
3849 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
3850 name = TREE_OPERAND (name, 0);
68642fb6 3851
94dfccd1
JM
3852 /* In case this decl was explicitly namespace-qualified, look it
3853 up in its namespace context. */
3854 if (TREE_CODE (x) == VAR_DECL && DECL_NAMESPACE_SCOPE_P (x)
3855 && namespace_bindings_p ())
2c73f9f5
ML
3856 t = namespace_binding (name, DECL_CONTEXT (x));
3857 else
3858 t = lookup_name_current_level (name);
fe8fadc1 3859
94dfccd1
JM
3860 /* [basic.link] If there is a visible declaration of an entity
3861 with linkage having the same name and type, ignoring entities
3862 declared outside the innermost enclosing namespace scope, the
3863 block scope declaration declares that same entity and
3864 receives the linkage of the previous declaration. */
3865 if (! t && current_function_decl && x != current_function_decl
3866 && (TREE_CODE (x) == FUNCTION_DECL || TREE_CODE (x) == VAR_DECL)
3867 && DECL_EXTERNAL (x))
3868 {
3869 /* Look in block scope. */
3870 t = IDENTIFIER_VALUE (name);
3871 /* Or in the innermost namespace. */
3872 if (! t)
3873 t = namespace_binding (name, DECL_CONTEXT (x));
fab09a24
JM
3874 /* Does it have linkage? Note that if this isn't a DECL, it's an
3875 OVERLOAD, which is OK. */
3876 if (t && DECL_P (t) && ! (TREE_STATIC (t) || DECL_EXTERNAL (t)))
94dfccd1
JM
3877 t = NULL_TREE;
3878 if (t)
3879 different_binding_level = 1;
3880 }
3881
fe8fadc1
MM
3882 /* If we are declaring a function, and the result of name-lookup
3883 was an OVERLOAD, look for an overloaded instance that is
3884 actually the same as the function we are declaring. (If
3885 there is one, we have to merge our declaration with the
3886 previous declaration.) */
655dc6ee 3887 if (t && TREE_CODE (t) == OVERLOAD)
fe8fadc1
MM
3888 {
3889 tree match;
3890
655dc6ee
JM
3891 if (TREE_CODE (x) == FUNCTION_DECL)
3892 for (match = t; match; match = OVL_NEXT (match))
3893 {
92643fea 3894 if (decls_match (OVL_CURRENT (match), x))
655dc6ee
JM
3895 break;
3896 }
3897 else
3898 /* Just choose one. */
3899 match = t;
fe8fadc1
MM
3900
3901 if (match)
3902 t = OVL_CURRENT (match);
3903 else
3904 t = NULL_TREE;
3905 }
3906
8d08fdba
MS
3907 if (t == error_mark_node)
3908 {
3909 /* error_mark_node is 0 for a while during initialization! */
3910 t = NULL_TREE;
8251199e 3911 cp_error_at ("`%#D' used prior to declaration", x);
8d08fdba 3912 }
51c184be 3913 else if (t != NULL_TREE)
8d08fdba 3914 {
94dfccd1
JM
3915 if (different_binding_level)
3916 {
3917 if (decls_match (x, t))
3918 /* The standard only says that the local extern
3919 inherits linkage from the previous decl; in
3920 particular, default args are not shared. It would
3921 be nice to propagate inlining info, though. FIXME. */
3922 TREE_PUBLIC (x) = TREE_PUBLIC (t);
3923 }
3924 else if (TREE_CODE (t) == PARM_DECL)
8d08fdba
MS
3925 {
3926 if (DECL_CONTEXT (t) == NULL_TREE)
400500c4
RK
3927 /* This is probaby caused by too many errors, but calling
3928 abort will say that if errors have occurred. */
3929 abort ();
be99da77 3930
e92cc029 3931 /* Check for duplicate params. */
be99da77
MS
3932 if (duplicate_decls (x, t))
3933 return t;
8d08fdba 3934 }
eb68cb58 3935 else if ((DECL_EXTERN_C_FUNCTION_P (x)
5566b478
MS
3936 || DECL_FUNCTION_TEMPLATE_P (x))
3937 && is_overloaded_fn (t))
2c73f9f5 3938 /* Don't do anything just yet. */;
e1cd6e56
MS
3939 else if (t == wchar_decl_node)
3940 {
3941 if (pedantic && ! DECL_IN_SYSTEM_HEADER (x))
8251199e 3942 cp_pedwarn ("redeclaration of wchar_t as `%T'", TREE_TYPE (x));
e1cd6e56
MS
3943
3944 /* Throw away the redeclaration. */
3945 return t;
3946 }
8926095f 3947 else if (TREE_CODE (t) != TREE_CODE (x))
8d08fdba 3948 {
9ed182dc 3949 if (duplicate_decls (x, t))
51c184be 3950 return t;
8d08fdba
MS
3951 }
3952 else if (duplicate_decls (x, t))
51c184be 3953 {
7177d104
MS
3954 if (TREE_CODE (t) == TYPE_DECL)
3955 SET_IDENTIFIER_TYPE_VALUE (name, TREE_TYPE (t));
f30432d7
MS
3956 else if (TREE_CODE (t) == FUNCTION_DECL)
3957 check_default_args (t);
7177d104 3958
51c184be
MS
3959 return t;
3960 }
35680744
MM
3961 else if (DECL_MAIN_P (x))
3962 {
3963 /* A redeclaration of main, but not a duplicate of the
68642fb6 3964 previous one.
35680744
MM
3965
3966 [basic.start.main]
3967
3968 This function shall not be overloaded. */
8251199e
JM
3969 cp_error_at ("invalid redeclaration of `%D'", t);
3970 cp_error ("as `%D'", x);
35680744
MM
3971 /* We don't try to push this declaration since that
3972 causes a crash. */
3973 return x;
3974 }
8d08fdba 3975 }
8926095f 3976
f3400fe2
JM
3977 check_template_shadow (x);
3978
fcfcdfc8
JM
3979 /* If this is a function conjured up by the backend, massage it
3980 so it looks friendly. */
eb68cb58 3981 if (DECL_NON_THUNK_FUNCTION_P (x) && ! DECL_LANG_SPECIFIC (x))
fcfcdfc8
JM
3982 {
3983 retrofit_lang_decl (x);
3984 DECL_LANGUAGE (x) = lang_c;
3985 }
3986
eb68cb58 3987 if (DECL_NON_THUNK_FUNCTION_P (x) && ! DECL_FUNCTION_MEMBER_P (x))
8926095f 3988 {
7bdbfa05 3989 t = push_overloaded_decl (x, PUSH_LOCAL);
5fdaba89 3990 if (t != x)
8926095f 3991 return t;
f181d4ae
MM
3992 if (!namespace_bindings_p ())
3993 /* We do not need to create a binding for this name;
3994 push_overloaded_decl will have already done so if
3995 necessary. */
3996 need_new_binding = 0;
8926095f 3997 }
6eb3bb27 3998 else if (DECL_FUNCTION_TEMPLATE_P (x) && DECL_NAMESPACE_SCOPE_P (x))
cfe507be
MM
3999 {
4000 t = push_overloaded_decl (x, PUSH_GLOBAL);
4001 if (t == x)
4002 add_decl_to_level (x, NAMESPACE_LEVEL (CP_DECL_CONTEXT (t)));
4003 return t;
4004 }
8d08fdba 4005
a1774733
BK
4006 /* If declaring a type as a typedef, copy the type (unless we're
4007 at line 0), and install this TYPE_DECL as the new type's typedef
4008 name. See the extensive comment in ../c-decl.c (pushdecl). */
8d08fdba
MS
4009 if (TREE_CODE (x) == TYPE_DECL)
4010 {
4011 tree type = TREE_TYPE (x);
655dc6ee 4012 if (DECL_SOURCE_LINE (x) == 0)
a1774733
BK
4013 {
4014 if (TYPE_NAME (type) == 0)
4015 TYPE_NAME (type) = x;
4016 }
1c80fb65
MM
4017 else if (type != error_mark_node && TYPE_NAME (type) != x
4018 /* We don't want to copy the type when all we're
4019 doing is making a TYPE_DECL for the purposes of
4020 inlining. */
68642fb6 4021 && (!TYPE_NAME (type)
1c80fb65 4022 || TYPE_NAME (type) != DECL_ABSTRACT_ORIGIN (x)))
a1774733
BK
4023 {
4024 DECL_ORIGINAL_TYPE (x) = type;
4025 type = build_type_copy (type);
4026 TYPE_STUB_DECL (type) = TYPE_STUB_DECL (DECL_ORIGINAL_TYPE (x));
4027 TYPE_NAME (type) = x;
4028 TREE_TYPE (x) = type;
4029 }
8d08fdba 4030
8d08fdba
MS
4031 if (type != error_mark_node
4032 && TYPE_NAME (type)
4033 && TYPE_IDENTIFIER (type))
68642fb6 4034 set_identifier_type_value_with_scope (DECL_NAME (x), type,
f181d4ae
MM
4035 current_binding_level);
4036
8d08fdba
MS
4037 }
4038
4039 /* Multiple external decls of the same identifier ought to match.
4040
4041 We get warnings about inline functions where they are defined.
39211cd5 4042 We get warnings about other functions from push_overloaded_decl.
68642fb6 4043
8d08fdba 4044 Avoid duplicate warnings where they are used. */
39211cd5 4045 if (TREE_PUBLIC (x) && TREE_CODE (x) != FUNCTION_DECL)
8d08fdba
MS
4046 {
4047 tree decl;
4048
f49fad00
JM
4049 decl = IDENTIFIER_NAMESPACE_VALUE (name);
4050 if (decl && TREE_CODE (decl) == OVERLOAD)
4051 decl = OVL_FUNCTION (decl);
8d08fdba 4052
f49fad00
JM
4053 if (decl && decl != error_mark_node
4054 && (DECL_EXTERNAL (decl) || TREE_PUBLIC (decl))
8d08fdba
MS
4055 /* If different sort of thing, we already gave an error. */
4056 && TREE_CODE (decl) == TREE_CODE (x)
3bfdc719 4057 && !same_type_p (TREE_TYPE (x), TREE_TYPE (decl)))
8d08fdba 4058 {
8251199e
JM
4059 cp_pedwarn ("type mismatch with previous external decl", x);
4060 cp_pedwarn_at ("previous external decl of `%#D'", decl);
8d08fdba
MS
4061 }
4062 }
4063
8d08fdba
MS
4064 /* This name is new in its binding level.
4065 Install the new declaration and return it. */
2c73f9f5 4066 if (namespace_bindings_p ())
8d08fdba
MS
4067 {
4068 /* Install a global value. */
4069
8d08fdba
MS
4070 /* If the first global decl has external linkage,
4071 warn if we later see static one. */
31928556 4072 if (IDENTIFIER_GLOBAL_VALUE (name) == NULL_TREE && TREE_PUBLIC (x))
8d08fdba 4073 TREE_PUBLIC (name) = 1;
5362b086 4074
92643fea
MM
4075 /* Bind the name for the entity. */
4076 if (!(TREE_CODE (x) == TYPE_DECL && DECL_ARTIFICIAL (x)
4077 && t != NULL_TREE)
4078 && (TREE_CODE (x) == TYPE_DECL
4079 || TREE_CODE (x) == VAR_DECL
4080 || TREE_CODE (x) == NAMESPACE_DECL
4081 || TREE_CODE (x) == CONST_DECL
4082 || TREE_CODE (x) == TEMPLATE_DECL))
4083 SET_IDENTIFIER_NAMESPACE_VALUE (name, x);
8d08fdba
MS
4084
4085 /* Don't forget if the function was used via an implicit decl. */
4086 if (IDENTIFIER_IMPLICIT_DECL (name)
4087 && TREE_USED (IDENTIFIER_IMPLICIT_DECL (name)))
4088 TREE_USED (x) = 1;
4089
4090 /* Don't forget if its address was taken in that way. */
4091 if (IDENTIFIER_IMPLICIT_DECL (name)
4092 && TREE_ADDRESSABLE (IDENTIFIER_IMPLICIT_DECL (name)))
4093 TREE_ADDRESSABLE (x) = 1;
4094
4095 /* Warn about mismatches against previous implicit decl. */
4096 if (IDENTIFIER_IMPLICIT_DECL (name) != NULL_TREE
4097 /* If this real decl matches the implicit, don't complain. */
4098 && ! (TREE_CODE (x) == FUNCTION_DECL
4099 && TREE_TYPE (TREE_TYPE (x)) == integer_type_node))
8251199e
JM
4100 cp_warning
4101 ("`%D' was previously implicitly declared to return `int'", x);
8d08fdba
MS
4102
4103 /* If new decl is `static' and an `extern' was seen previously,
4104 warn about it. */
a0a33927
MS
4105 if (x != NULL_TREE && t != NULL_TREE && decls_match (x, t))
4106 warn_extern_redeclared_static (x, t);
8d08fdba
MS
4107 }
4108 else
4109 {
4110 /* Here to install a non-global value. */
f181d4ae 4111 tree oldlocal = IDENTIFIER_VALUE (name);
31928556 4112 tree oldglobal = IDENTIFIER_NAMESPACE_VALUE (name);
8d08fdba 4113
f181d4ae
MM
4114 if (need_new_binding)
4115 {
0034cf72 4116 push_local_binding (name, x, 0);
f181d4ae
MM
4117 /* Because push_local_binding will hook X on to the
4118 current_binding_level's name list, we don't want to
4119 do that again below. */
4120 need_new_binding = 0;
4121 }
8d08fdba
MS
4122
4123 /* If this is a TYPE_DECL, push it into the type value slot. */
4124 if (TREE_CODE (x) == TYPE_DECL)
68642fb6 4125 set_identifier_type_value_with_scope (name, TREE_TYPE (x),
f181d4ae 4126 current_binding_level);
8d08fdba 4127
a9aedbc2
MS
4128 /* Clear out any TYPE_DECL shadowed by a namespace so that
4129 we won't think this is a type. The C struct hack doesn't
4130 go through namespaces. */
4131 if (TREE_CODE (x) == NAMESPACE_DECL)
68642fb6 4132 set_identifier_type_value_with_scope (name, NULL_TREE,
f181d4ae 4133 current_binding_level);
a9aedbc2 4134
e905ac8a
MS
4135 if (oldlocal)
4136 {
4137 tree d = oldlocal;
17aec3eb 4138
74dc0d8c
MS
4139 while (oldlocal
4140 && TREE_CODE (oldlocal) == VAR_DECL
4141 && DECL_DEAD_FOR_LOCAL (oldlocal))
17aec3eb
RK
4142 oldlocal = DECL_SHADOWED_FOR_VAR (oldlocal);
4143
e905ac8a
MS
4144 if (oldlocal == NULL_TREE)
4145 oldlocal = IDENTIFIER_NAMESPACE_VALUE (DECL_NAME (d));
4146 }
4147
8d08fdba
MS
4148 /* If this is an extern function declaration, see if we
4149 have a global definition or declaration for the function. */
4150 if (oldlocal == NULL_TREE
faae18ab 4151 && DECL_EXTERNAL (x)
31928556 4152 && oldglobal != NULL_TREE
8d08fdba 4153 && TREE_CODE (x) == FUNCTION_DECL
31928556 4154 && TREE_CODE (oldglobal) == FUNCTION_DECL)
8d08fdba
MS
4155 {
4156 /* We have one. Their types must agree. */
31928556 4157 if (decls_match (x, oldglobal))
6060a796
MS
4158 /* OK */;
4159 else
8d08fdba 4160 {
8251199e
JM
4161 cp_warning ("extern declaration of `%#D' doesn't match", x);
4162 cp_warning_at ("global declaration `%#D'", oldglobal);
8d08fdba 4163 }
8d08fdba
MS
4164 }
4165 /* If we have a local external declaration,
4166 and no file-scope declaration has yet been seen,
4167 then if we later have a file-scope decl it must not be static. */
4168 if (oldlocal == NULL_TREE
31928556 4169 && oldglobal == NULL_TREE
8d08fdba
MS
4170 && DECL_EXTERNAL (x)
4171 && TREE_PUBLIC (x))
f181d4ae 4172 TREE_PUBLIC (name) = 1;
8d08fdba 4173
8d08fdba 4174 /* Warn if shadowing an argument at the top level of the body. */
e905ac8a
MS
4175 if (oldlocal != NULL_TREE && !DECL_EXTERNAL (x)
4176 /* Inline decls shadow nothing. */
4177 && !DECL_FROM_INLINE (x)
4178 && TREE_CODE (oldlocal) == PARM_DECL
4179 /* Don't complain if it's from an enclosing function. */
4180 && DECL_CONTEXT (oldlocal) == current_function_decl
4181 && TREE_CODE (x) != PARM_DECL)
8d08fdba
MS
4182 {
4183 /* Go to where the parms should be and see if we
4184 find them there. */
4185 struct binding_level *b = current_binding_level->level_chain;
4186
4187 if (cleanup_label)
4188 b = b->level_chain;
4189
4190 /* ARM $8.3 */
4191 if (b->parm_flag == 1)
8251199e 4192 cp_error ("declaration of `%#D' shadows a parameter", name);
8d08fdba 4193 }
e905ac8a 4194
8d08fdba 4195 /* Maybe warn if shadowing something else. */
e905ac8a
MS
4196 if (warn_shadow && !DECL_EXTERNAL (x)
4197 /* Inline decls shadow nothing. */
4198 && !DECL_FROM_INLINE (x)
4199 /* No shadow warnings for internally generated vars. */
4200 && ! DECL_ARTIFICIAL (x)
4201 /* No shadow warnings for vars made for inlining. */
4202 && ! DECL_FROM_INLINE (x))
8d08fdba 4203 {
8d08fdba 4204 if (oldlocal != NULL_TREE && TREE_CODE (oldlocal) == PARM_DECL)
b9d12519
KG
4205 warning ("declaration of `%s' shadows a parameter",
4206 IDENTIFIER_POINTER (name));
8d08fdba 4207 else if (IDENTIFIER_CLASS_VALUE (name) != NULL_TREE
4ac14744 4208 && current_class_ptr
8d08fdba 4209 && !TREE_STATIC (name))
b9d12519
KG
4210 warning ("declaration of `%s' shadows a member of `this'",
4211 IDENTIFIER_POINTER (name));
8d08fdba 4212 else if (oldlocal != NULL_TREE)
b9d12519
KG
4213 warning ("declaration of `%s' shadows previous local",
4214 IDENTIFIER_POINTER (name));
31928556 4215 else if (oldglobal != NULL_TREE)
30394414 4216 /* XXX shadow warnings in outer-more namespaces */
b9d12519
KG
4217 warning ("declaration of `%s' shadows global declaration",
4218 IDENTIFIER_POINTER (name));
8d08fdba 4219 }
e1cd6e56 4220 }
8d08fdba 4221
e1cd6e56 4222 if (TREE_CODE (x) == FUNCTION_DECL)
f30432d7 4223 check_default_args (x);
8145f082
MS
4224
4225 /* Keep count of variables in this level with incomplete type. */
8145f082 4226 if (TREE_CODE (x) == VAR_DECL
28cbf42c 4227 && TREE_TYPE (x) != error_mark_node
d0f062fb 4228 && ((!COMPLETE_TYPE_P (TREE_TYPE (x))
f30432d7
MS
4229 && PROMOTES_TO_AGGR_TYPE (TREE_TYPE (x), ARRAY_TYPE))
4230 /* RTTI TD entries are created while defining the type_info. */
4231 || (TYPE_LANG_SPECIFIC (TREE_TYPE (x))
4232 && TYPE_BEING_DEFINED (TREE_TYPE (x)))))
70adf8a9
JM
4233 {
4234 if (namespace_bindings_p ())
4235 namespace_scope_incomplete
4236 = tree_cons (NULL_TREE, x, namespace_scope_incomplete);
4237 else
4238 current_binding_level->incomplete
4239 = tree_cons (NULL_TREE, x, current_binding_level->incomplete);
4240 }
8d08fdba
MS
4241 }
4242
f181d4ae 4243 if (need_new_binding)
68642fb6 4244 add_decl_to_level (x,
efee38a9
MM
4245 DECL_NAMESPACE_SCOPE_P (x)
4246 ? NAMESPACE_LEVEL (CP_DECL_CONTEXT (x))
4247 : current_binding_level);
8d08fdba
MS
4248
4249 return x;
4250}
4251
5566b478
MS
4252/* Same as pushdecl, but define X in binding-level LEVEL. We rely on the
4253 caller to set DECL_CONTEXT properly. */
8d08fdba
MS
4254
4255static tree
4256pushdecl_with_scope (x, level)
4257 tree x;
4258 struct binding_level *level;
4259{
8d019cef 4260 register struct binding_level *b;
5566b478 4261 tree function_decl = current_function_decl;
8d08fdba 4262
5566b478 4263 current_function_decl = NULL_TREE;
8d019cef
JM
4264 if (level->parm_flag == 2)
4265 {
4266 b = class_binding_level;
4267 class_binding_level = level;
4268 pushdecl_class_level (x);
4269 class_binding_level = b;
4270 }
4271 else
4272 {
4273 b = current_binding_level;
4274 current_binding_level = level;
4275 x = pushdecl (x);
4276 current_binding_level = b;
4277 }
5566b478 4278 current_function_decl = function_decl;
8d08fdba
MS
4279 return x;
4280}
4281
2c73f9f5 4282/* Like pushdecl, only it places X in the current namespace,
8d08fdba 4283 if appropriate. */
e92cc029 4284
8d08fdba 4285tree
2c73f9f5 4286pushdecl_namespace_level (x)
8d08fdba
MS
4287 tree x;
4288{
8f032717 4289 register struct binding_level *b = current_binding_level;
2c73f9f5
ML
4290 register tree t;
4291
4292 t = pushdecl_with_scope (x, NAMESPACE_LEVEL (current_namespace));
8d08fdba
MS
4293
4294 /* Now, the type_shadowed stack may screw us. Munge it so it does
4295 what we want. */
4296 if (TREE_CODE (x) == TYPE_DECL)
4297 {
4298 tree name = DECL_NAME (x);
4299 tree newval;
4300 tree *ptr = (tree *)0;
4301 for (; b != global_binding_level; b = b->level_chain)
4302 {
4303 tree shadowed = b->type_shadowed;
4304 for (; shadowed; shadowed = TREE_CHAIN (shadowed))
4305 if (TREE_PURPOSE (shadowed) == name)
4306 {
4307 ptr = &TREE_VALUE (shadowed);
4308 /* Can't break out of the loop here because sometimes
4309 a binding level will have duplicate bindings for
4310 PT names. It's gross, but I haven't time to fix it. */
4311 }
4312 }
4313 newval = TREE_TYPE (x);
4314 if (ptr == (tree *)0)
4315 {
4316 /* @@ This shouldn't be needed. My test case "zstring.cc" trips
4317 up here if this is changed to an assertion. --KR */
4318 SET_IDENTIFIER_TYPE_VALUE (name, newval);
4319 }
4320 else
4321 {
8d08fdba
MS
4322 *ptr = newval;
4323 }
4324 }
4325 return t;
4326}
4327
2c73f9f5
ML
4328/* Like pushdecl, only it places X in GLOBAL_BINDING_LEVEL,
4329 if appropriate. */
4330
4331tree
4332pushdecl_top_level (x)
4333 tree x;
4334{
b35d4555 4335 push_to_top_level ();
2c73f9f5 4336 x = pushdecl_namespace_level (x);
b35d4555 4337 pop_from_top_level ();
2c73f9f5
ML
4338 return x;
4339}
4340
8d08fdba 4341/* Make the declaration of X appear in CLASS scope. */
e92cc029 4342
61a127b3 4343void
8d08fdba
MS
4344pushdecl_class_level (x)
4345 tree x;
4346{
4347 /* Don't use DECL_ASSEMBLER_NAME here! Everything that looks in class
4348 scope looks for the pre-mangled name. */
8f032717
MM
4349 register tree name;
4350
4351 if (TREE_CODE (x) == OVERLOAD)
4352 x = OVL_CURRENT (x);
4353 name = DECL_NAME (x);
8d08fdba
MS
4354
4355 if (name)
4356 {
4357 push_class_level_binding (name, x);
4358 if (TREE_CODE (x) == TYPE_DECL)
f181d4ae 4359 set_identifier_type_value (name, TREE_TYPE (x));
8d08fdba 4360 }
6bdb8141 4361 else if (ANON_AGGR_TYPE_P (TREE_TYPE (x)))
8f032717
MM
4362 {
4363 tree f;
4364
4365 for (f = TYPE_FIELDS (TREE_TYPE (x));
4366 f;
4367 f = TREE_CHAIN (f))
4368 pushdecl_class_level (f);
4369 }
8d08fdba
MS
4370}
4371
9188c363
MM
4372/* Enter DECL into the symbol table, if that's appropriate. Returns
4373 DECL, or a modified version thereof. */
4374
4375tree
4376maybe_push_decl (decl)
4377 tree decl;
4378{
4379 tree type = TREE_TYPE (decl);
4380
4381 /* Add this decl to the current binding level, but not if it comes
4382 from another scope, e.g. a static member variable. TEM may equal
4383 DECL or it may be a previous decl of the same name. */
07c88314
MM
4384 if (decl == error_mark_node
4385 || (TREE_CODE (decl) != PARM_DECL
4386 && DECL_CONTEXT (decl) != NULL_TREE
4387 /* Definitions of namespace members outside their namespace are
4388 possible. */
4389 && TREE_CODE (DECL_CONTEXT (decl)) != NAMESPACE_DECL)
9188c363
MM
4390 || (TREE_CODE (decl) == TEMPLATE_DECL && !namespace_bindings_p ())
4391 || TREE_CODE (type) == UNKNOWN_TYPE
558475f0 4392 /* The declaration of a template specialization does not affect
9188c363
MM
4393 the functions available for overload resolution, so we do not
4394 call pushdecl. */
4395 || (TREE_CODE (decl) == FUNCTION_DECL
4396 && DECL_TEMPLATE_SPECIALIZATION (decl)))
4397 return decl;
4398 else
4399 return pushdecl (decl);
4400}
4401
8d08fdba
MS
4402/* Make the declaration(s) of X appear in CLASS scope
4403 under the name NAME. */
e92cc029 4404
8d08fdba
MS
4405void
4406push_class_level_binding (name, x)
4407 tree name;
4408 tree x;
4409{
8f032717 4410 tree binding;
68642fb6 4411 /* The class_binding_level will be NULL if x is a template
98c1c668
JM
4412 parameter name in a member template. */
4413 if (!class_binding_level)
4414 return;
4415
908c4e83
MM
4416 /* Make sure that this new member does not have the same name
4417 as a template parameter. */
4418 if (TYPE_BEING_DEFINED (current_class_type))
4419 check_template_shadow (x);
4420
f181d4ae
MM
4421 /* If this declaration shadows a declaration from an enclosing
4422 class, then we will need to restore IDENTIFIER_CLASS_VALUE when
4423 we leave this class. Record the shadowed declaration here. */
8f032717 4424 binding = IDENTIFIER_BINDING (name);
68642fb6 4425 if (binding
8f032717
MM
4426 && ((TREE_CODE (x) == OVERLOAD
4427 && BINDING_VALUE (binding)
4428 && is_overloaded_fn (BINDING_VALUE (binding)))
4429 || INHERITED_VALUE_BINDING_P (binding)))
4430 {
4431 tree shadow;
4432 tree old_decl;
4433
4434 /* If the old binding was from a base class, and was for a tag
4435 name, slide it over to make room for the new binding. The
4436 old binding is still visible if explicitly qualified with a
4437 class-key. */
4438 if (INHERITED_VALUE_BINDING_P (binding)
4439 && BINDING_VALUE (binding)
4440 && TREE_CODE (BINDING_VALUE (binding)) == TYPE_DECL
4441 && DECL_ARTIFICIAL (BINDING_VALUE (binding))
4442 && !(TREE_CODE (x) == TYPE_DECL && DECL_ARTIFICIAL (x)))
4443 {
4444 old_decl = BINDING_TYPE (binding);
4445 BINDING_TYPE (binding) = BINDING_VALUE (binding);
4446 BINDING_VALUE (binding) = NULL_TREE;
4447 INHERITED_VALUE_BINDING_P (binding) = 0;
4448 }
4449 else
4450 old_decl = BINDING_VALUE (binding);
4451
76191fdd
NS
4452 /* Find the previous binding of name on the class-shadowed
4453 list, and update it. */
8f032717
MM
4454 for (shadow = class_binding_level->class_shadowed;
4455 shadow;
4456 shadow = TREE_CHAIN (shadow))
4457 if (TREE_PURPOSE (shadow) == name
4458 && TREE_TYPE (shadow) == old_decl)
4459 {
4460 BINDING_VALUE (binding) = x;
4461 INHERITED_VALUE_BINDING_P (binding) = 0;
4462 TREE_TYPE (shadow) = x;
76191fdd 4463 IDENTIFIER_CLASS_VALUE (name) = x;
8f032717
MM
4464 return;
4465 }
4466 }
f181d4ae 4467
8f032717 4468 /* If we didn't replace an existing binding, put the binding on the
76191fdd 4469 stack of bindings for the identifier, and update the shadowed list. */
8f032717
MM
4470 if (push_class_binding (name, x))
4471 {
8f032717 4472 class_binding_level->class_shadowed
76191fdd 4473 = tree_cons (name, NULL,
8f032717 4474 class_binding_level->class_shadowed);
8f032717
MM
4475 /* Record the value we are binding NAME to so that we can know
4476 what to pop later. */
4477 TREE_TYPE (class_binding_level->class_shadowed) = x;
4478 }
8d08fdba
MS
4479}
4480
dd4fae80
ML
4481/* Insert another USING_DECL into the current binding level, returning
4482 this declaration. If this is a redeclaration, do nothing, and
4483 return NULL_TREE if this not in namespace scope (in namespace
4484 scope, a using decl might extend any previous bindings). */
e92cc029 4485
2c73f9f5
ML
4486tree
4487push_using_decl (scope, name)
4488 tree scope;
4489 tree name;
8d08fdba 4490{
2c73f9f5 4491 tree decl;
68642fb6 4492
2c73f9f5
ML
4493 my_friendly_assert (TREE_CODE (scope) == NAMESPACE_DECL, 383);
4494 my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, 384);
4495 for (decl = current_binding_level->usings; decl; decl = TREE_CHAIN (decl))
4496 if (DECL_INITIAL (decl) == scope && DECL_NAME (decl) == name)
4497 break;
4498 if (decl)
dd4fae80 4499 return namespace_bindings_p () ? decl : NULL_TREE;
4ce3d537 4500 decl = build_lang_decl (USING_DECL, name, void_type_node);
2c73f9f5
ML
4501 DECL_INITIAL (decl) = scope;
4502 TREE_CHAIN (decl) = current_binding_level->usings;
4503 current_binding_level->usings = decl;
4504 return decl;
8d08fdba
MS
4505}
4506
ea9635c7
ML
4507/* Add namespace to using_directives. Return NULL_TREE if nothing was
4508 changed (i.e. there was already a directive), or the fresh
4509 TREE_LIST otherwise. */
4510
4511tree
9ed182dc 4512push_using_directive (used)
ea9635c7 4513 tree used;
ea9635c7
ML
4514{
4515 tree ud = current_binding_level->using_directives;
9ed182dc 4516 tree iter, ancestor;
68642fb6 4517
ea9635c7
ML
4518 /* Check if we already have this. */
4519 if (purpose_member (used, ud) != NULL_TREE)
4520 return NULL_TREE;
00dc6358
JM
4521
4522 /* Recursively add all namespaces used. */
4523 for (iter = DECL_NAMESPACE_USING (used); iter; iter = TREE_CHAIN (iter))
9ed182dc 4524 push_using_directive (TREE_PURPOSE (iter));
00dc6358 4525
9ed182dc 4526 ancestor = namespace_ancestor (current_decl_namespace (), used);
00dc6358 4527 ud = current_binding_level->using_directives;
e1b3e07d 4528 ud = tree_cons (used, ancestor, ud);
ea9635c7
ML
4529 current_binding_level->using_directives = ud;
4530 return ud;
4531}
4532
f181d4ae
MM
4533/* DECL is a FUNCTION_DECL for a non-member function, which may have
4534 other definitions already in place. We get around this by making
4535 the value of the identifier point to a list of all the things that
4536 want to be referenced by that name. It is then up to the users of
4537 that name to decide what to do with that list.
8d08fdba 4538
17aec3eb
RK
4539 DECL may also be a TEMPLATE_DECL, with a FUNCTION_DECL in its
4540 DECL_TEMPLATE_RESULT. It is dealt with the same way.
8d08fdba 4541
7bdbfa05
MM
4542 FLAGS is a bitwise-or of the following values:
4543 PUSH_LOCAL: Bind DECL in the current scope, rather than at
4544 namespace scope.
4545 PUSH_USING: DECL is being pushed as the result of a using
68642fb6 4546 declaration.
7bdbfa05 4547
8d08fdba
MS
4548 The value returned may be a previous declaration if we guessed wrong
4549 about what language DECL should belong to (C or C++). Otherwise,
4550 it's always DECL (and never something that's not a _DECL). */
e92cc029 4551
7bdbfa05
MM
4552tree
4553push_overloaded_decl (decl, flags)
8d08fdba 4554 tree decl;
7bdbfa05 4555 int flags;
8d08fdba 4556{
f181d4ae 4557 tree name = DECL_NAME (decl);
700f8a87 4558 tree old;
f181d4ae 4559 tree new_binding;
7bdbfa05 4560 int doing_global = (namespace_bindings_p () || !(flags & PUSH_LOCAL));
700f8a87
MS
4561
4562 if (doing_global)
9f33663b 4563 old = namespace_binding (name, DECL_CONTEXT (decl));
700f8a87 4564 else
f181d4ae 4565 old = lookup_name_current_level (name);
8d08fdba 4566
700f8a87 4567 if (old)
8d08fdba 4568 {
e1cd6e56 4569 if (TREE_CODE (old) == TYPE_DECL && DECL_ARTIFICIAL (old))
8926095f 4570 {
700f8a87 4571 tree t = TREE_TYPE (old);
cdf5b885
MS
4572 if (IS_AGGR_TYPE (t) && warn_shadow
4573 && (! DECL_IN_SYSTEM_HEADER (decl)
4574 || ! DECL_IN_SYSTEM_HEADER (old)))
8251199e 4575 cp_warning ("`%#D' hides constructor for `%#T'", decl, t);
5b605f68 4576 old = NULL_TREE;
8926095f 4577 }
700f8a87 4578 else if (is_overloaded_fn (old))
8d08fdba 4579 {
8d08fdba 4580 tree tmp;
68642fb6 4581
2c73f9f5 4582 for (tmp = old; tmp; tmp = OVL_NEXT (tmp))
7bdbfa05
MM
4583 {
4584 tree fn = OVL_CURRENT (tmp);
4585
4586 if (TREE_CODE (tmp) == OVERLOAD && OVL_USED (tmp)
4587 && !(flags & PUSH_USING)
4588 && compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
4589 TYPE_ARG_TYPES (TREE_TYPE (decl))))
4590 cp_error ("`%#D' conflicts with previous using declaration `%#D'",
4591 decl, fn);
68642fb6 4592
7bdbfa05
MM
4593 if (duplicate_decls (decl, fn))
4594 return fn;
4595 }
8d08fdba 4596 }
655dc6ee
JM
4597 else if (old == error_mark_node)
4598 /* Ignore the undefined symbol marker. */
4599 old = NULL_TREE;
e1cd6e56
MS
4600 else
4601 {
8251199e
JM
4602 cp_error_at ("previous non-function declaration `%#D'", old);
4603 cp_error ("conflicts with function declaration `%#D'", decl);
a9aedbc2 4604 return decl;
e1cd6e56 4605 }
8d08fdba 4606 }
7177d104 4607
700f8a87 4608 if (old || TREE_CODE (decl) == TEMPLATE_DECL)
8d08fdba 4609 {
2c73f9f5 4610 if (old && TREE_CODE (old) != OVERLOAD)
f181d4ae
MM
4611 new_binding = ovl_cons (decl, ovl_cons (old, NULL_TREE));
4612 else
4613 new_binding = ovl_cons (decl, old);
347d73d7
ML
4614 if (flags & PUSH_USING)
4615 OVL_USED (new_binding) = 1;
8d08fdba
MS
4616 }
4617 else
f181d4ae
MM
4618 /* NAME is not ambiguous. */
4619 new_binding = decl;
700f8a87
MS
4620
4621 if (doing_global)
f181d4ae 4622 set_namespace_binding (name, current_namespace, new_binding);
700f8a87 4623 else
f181d4ae
MM
4624 {
4625 /* We only create an OVERLOAD if there was a previous binding at
0580c9aa
ML
4626 this level, or if decl is a template. In the former case, we
4627 need to remove the old binding and replace it with the new
4628 binding. We must also run through the NAMES on the binding
4629 level where the name was bound to update the chain. */
4630
4631 if (TREE_CODE (new_binding) == OVERLOAD && old)
f181d4ae
MM
4632 {
4633 tree *d;
68642fb6 4634
f181d4ae
MM
4635 for (d = &BINDING_LEVEL (IDENTIFIER_BINDING (name))->names;
4636 *d;
4637 d = &TREE_CHAIN (*d))
4638 if (*d == old
4639 || (TREE_CODE (*d) == TREE_LIST
4640 && TREE_VALUE (*d) == old))
4641 {
d8f8dca1
MM
4642 if (TREE_CODE (*d) == TREE_LIST)
4643 /* Just replace the old binding with the new. */
4644 TREE_VALUE (*d) = new_binding;
4645 else
4646 /* Build a TREE_LIST to wrap the OVERLOAD. */
68642fb6 4647 *d = tree_cons (NULL_TREE, new_binding,
ed5511d9 4648 TREE_CHAIN (*d));
d8f8dca1
MM
4649
4650 /* And update the CPLUS_BINDING node. */
4651 BINDING_VALUE (IDENTIFIER_BINDING (name))
4652 = new_binding;
4653 return decl;
f181d4ae
MM
4654 }
4655
d8f8dca1
MM
4656 /* We should always find a previous binding in this case. */
4657 my_friendly_abort (0);
f181d4ae
MM
4658 }
4659
4660 /* Install the new binding. */
0034cf72 4661 push_local_binding (name, new_binding, flags);
f181d4ae 4662 }
700f8a87 4663
8d08fdba
MS
4664 return decl;
4665}
4666\f
4667/* Generate an implicit declaration for identifier FUNCTIONID
4668 as a function of type int (). Print a warning if appropriate. */
4669
4670tree
4671implicitly_declare (functionid)
4672 tree functionid;
4673{
4674 register tree decl;
8d08fdba
MS
4675
4676 /* We used to reuse an old implicit decl here,
4677 but this loses with inline functions because it can clobber
4678 the saved decl chains. */
4679 decl = build_lang_decl (FUNCTION_DECL, functionid, default_function_type);
4680
4681 DECL_EXTERNAL (decl) = 1;
4682 TREE_PUBLIC (decl) = 1;
4683
cab1f180 4684 /* ISO standard says implicit declarations are in the innermost block.
d22c8596 4685 So we record the decl in the standard fashion. */
8d08fdba
MS
4686 pushdecl (decl);
4687 rest_of_decl_compilation (decl, NULL_PTR, 0, 0);
4688
4689 if (warn_implicit
4690 /* Only one warning per identifier. */
4691 && IDENTIFIER_IMPLICIT_DECL (functionid) == NULL_TREE)
4692 {
8251199e 4693 cp_pedwarn ("implicit declaration of function `%#D'", decl);
8d08fdba
MS
4694 }
4695
4696 SET_IDENTIFIER_IMPLICIT_DECL (functionid, decl);
4697
8d08fdba
MS
4698 return decl;
4699}
4700
8251199e 4701/* Return zero if the declaration NEWDECL is valid
8d08fdba
MS
4702 when the declaration OLDDECL (assumed to be for the same name)
4703 has already been seen.
4704 Otherwise return an error message format string with a %s
4705 where the identifier should go. */
4706
d8e178a0 4707static const char *
8d08fdba
MS
4708redeclaration_error_message (newdecl, olddecl)
4709 tree newdecl, olddecl;
4710{
4711 if (TREE_CODE (newdecl) == TYPE_DECL)
4712 {
4713 /* Because C++ can put things into name space for free,
4714 constructs like "typedef struct foo { ... } foo"
4715 would look like an erroneous redeclaration. */
3bfdc719 4716 if (same_type_p (TREE_TYPE (newdecl), TREE_TYPE (olddecl)))
8251199e 4717 return 0;
8d08fdba 4718 else
8251199e 4719 return "redefinition of `%#D'";
8d08fdba
MS
4720 }
4721 else if (TREE_CODE (newdecl) == FUNCTION_DECL)
4722 {
4723 /* If this is a pure function, its olddecl will actually be
4724 the original initialization to `0' (which we force to call
4725 abort()). Don't complain about redefinition in this case. */
fee7654e 4726 if (DECL_LANG_SPECIFIC (olddecl) && DECL_PURE_VIRTUAL_P (olddecl))
8251199e 4727 return 0;
8d08fdba 4728
2c73f9f5
ML
4729 /* If both functions come from different namespaces, this is not
4730 a redeclaration - this is a conflict with a used function. */
6eb3bb27 4731 if (DECL_NAMESPACE_SCOPE_P (olddecl)
2c73f9f5 4732 && DECL_CONTEXT (olddecl) != DECL_CONTEXT (newdecl))
8251199e 4733 return "`%D' conflicts with used function";
2c73f9f5 4734
db5ae43f
MS
4735 /* We'll complain about linkage mismatches in
4736 warn_extern_redeclared_static. */
4737
2c73f9f5 4738 /* Defining the same name twice is no good. */
8d08fdba 4739 if (DECL_INITIAL (olddecl) != NULL_TREE
faae18ab 4740 && DECL_INITIAL (newdecl) != NULL_TREE)
8d08fdba
MS
4741 {
4742 if (DECL_NAME (olddecl) == NULL_TREE)
8251199e 4743 return "`%#D' not declared in class";
8d08fdba 4744 else
8251199e 4745 return "redefinition of `%#D'";
8d08fdba 4746 }
8251199e 4747 return 0;
8d08fdba 4748 }
51c184be
MS
4749 else if (TREE_CODE (newdecl) == TEMPLATE_DECL)
4750 {
ec255269 4751 if ((TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == FUNCTION_DECL
5362b086 4752 && (DECL_TEMPLATE_RESULT (newdecl)
f4381d62 4753 != DECL_TEMPLATE_RESULT (olddecl))
ec255269
MS
4754 && DECL_INITIAL (DECL_TEMPLATE_RESULT (newdecl))
4755 && DECL_INITIAL (DECL_TEMPLATE_RESULT (olddecl)))
4756 || (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL
d0f062fb
NS
4757 && COMPLETE_TYPE_P (TREE_TYPE (newdecl))
4758 && COMPLETE_TYPE_P (TREE_TYPE (olddecl))))
8251199e
JM
4759 return "redefinition of `%#D'";
4760 return 0;
51c184be 4761 }
1f51a992 4762 else if (toplevel_bindings_p () || DECL_NAMESPACE_SCOPE_P (newdecl))
8d08fdba
MS
4763 {
4764 /* Objects declared at top level: */
4765 /* If at least one is a reference, it's ok. */
4766 if (DECL_EXTERNAL (newdecl) || DECL_EXTERNAL (olddecl))
8251199e 4767 return 0;
8926095f 4768 /* Reject two definitions. */
8251199e 4769 return "redefinition of `%#D'";
8d08fdba
MS
4770 }
4771 else
4772 {
4773 /* Objects declared with block scope: */
4774 /* Reject two definitions, and reject a definition
4775 together with an external reference. */
4776 if (!(DECL_EXTERNAL (newdecl) && DECL_EXTERNAL (olddecl)))
8251199e
JM
4777 return "redeclaration of `%#D'";
4778 return 0;
8d08fdba
MS
4779 }
4780}
4781\f
acef433b 4782/* Create a new label, named ID. */
8d08fdba 4783
acef433b
MM
4784static tree
4785make_label_decl (id, local_p)
8d08fdba 4786 tree id;
acef433b 4787 int local_p;
8d08fdba 4788{
acef433b 4789 tree decl;
8d08fdba 4790
acef433b 4791 decl = build_decl (LABEL_DECL, id, void_type_node);
b35d4555 4792 if (expanding_p)
acef433b
MM
4793 /* Make sure every label has an rtx. */
4794 label_rtx (decl);
4795
4796 DECL_CONTEXT (decl) = current_function_decl;
4797 DECL_MODE (decl) = VOIDmode;
4798 C_DECLARED_LABEL_FLAG (decl) = local_p;
8d08fdba 4799
acef433b
MM
4800 /* Say where one reference is to the label, for the sake of the
4801 error if it is not defined. */
4802 DECL_SOURCE_LINE (decl) = lineno;
4803 DECL_SOURCE_FILE (decl) = input_filename;
4804
4805 /* Record the fact that this identifier is bound to this label. */
4806 SET_IDENTIFIER_LABEL_VALUE (id, decl);
4807
6625cdb5
JM
4808 return decl;
4809}
4810
4811/* Record this label on the list of used labels so that we can check
4812 at the end of the function to see whether or not the label was
4813 actually defined, and so we can check when the label is defined whether
4814 this use is valid. */
4815
4816static void
4817use_label (decl)
4818 tree decl;
4819{
4820 if (named_label_uses == NULL
4821 || named_label_uses->names_in_scope != current_binding_level->names
4822 || named_label_uses->label_decl != decl)
4823 {
4824 struct named_label_use_list *new_ent;
4825 new_ent = ((struct named_label_use_list *)
4826 ggc_alloc (sizeof (struct named_label_use_list)));
e349ee73
MS
4827 new_ent->label_decl = decl;
4828 new_ent->names_in_scope = current_binding_level->names;
4829 new_ent->binding_level = current_binding_level;
4830 new_ent->lineno_o_goto = lineno;
4831 new_ent->filename_o_goto = input_filename;
4832 new_ent->next = named_label_uses;
4833 named_label_uses = new_ent;
8d08fdba 4834 }
acef433b 4835}
8d08fdba 4836
acef433b
MM
4837/* Look for a label named ID in the current function. If one cannot
4838 be found, create one. (We keep track of used, but undefined,
4839 labels, and complain about them at the end of a function.) */
8d08fdba 4840
68642fb6 4841tree
acef433b
MM
4842lookup_label (id)
4843 tree id;
4844{
4845 tree decl;
6625cdb5 4846 struct named_label_list *ent;
8d08fdba 4847
acef433b
MM
4848 /* You can't use labels at global scope. */
4849 if (current_function_decl == NULL_TREE)
4850 {
4851 error ("label `%s' referenced outside of any function",
4852 IDENTIFIER_POINTER (id));
4853 return NULL_TREE;
4854 }
68642fb6 4855
acef433b
MM
4856 /* See if we've already got this label. */
4857 decl = IDENTIFIER_LABEL_VALUE (id);
4858 if (decl != NULL_TREE && DECL_CONTEXT (decl) == current_function_decl)
4859 return decl;
8d08fdba 4860
acef433b
MM
4861 /* Record this label on the list of labels used in this function.
4862 We do this before calling make_label_decl so that we get the
4863 IDENTIFIER_LABEL_VALUE before the new label is declared. */
6625cdb5 4864 ent = ((struct named_label_list *)
f8a83ee3 4865 ggc_alloc_cleared (sizeof (struct named_label_list)));
6625cdb5
JM
4866 ent->old_value = IDENTIFIER_LABEL_VALUE (id);
4867 ent->next = named_labels;
4868 named_labels = ent;
4869
acef433b
MM
4870 /* We need a new label. */
4871 decl = make_label_decl (id, /*local_p=*/0);
6625cdb5 4872
acef433b 4873 /* Now fill in the information we didn't have before. */
6625cdb5 4874 ent->label_decl = decl;
8d08fdba
MS
4875
4876 return decl;
4877}
4878
acef433b 4879/* Declare a local label named ID. */
8d08fdba
MS
4880
4881tree
acef433b
MM
4882declare_local_label (id)
4883 tree id;
8d08fdba 4884{
acef433b 4885 tree decl;
8d08fdba 4886
acef433b
MM
4887 /* Add a new entry to the SHADOWED_LABELS list so that when we leave
4888 this scope we can restore the old value of
4889 IDENTIFIER_TYPE_VALUE. */
68642fb6 4890 current_binding_level->shadowed_labels
acef433b
MM
4891 = tree_cons (IDENTIFIER_LABEL_VALUE (id), NULL_TREE,
4892 current_binding_level->shadowed_labels);
4893 /* Look for the label. */
4894 decl = make_label_decl (id, /*local_p=*/1);
4895 /* Now fill in the information we didn't have before. */
4896 TREE_VALUE (current_binding_level->shadowed_labels) = decl;
68642fb6 4897
acef433b 4898 return decl;
8d08fdba
MS
4899}
4900
6625cdb5
JM
4901/* Returns nonzero if it is ill-formed to jump past the declaration of
4902 DECL. Returns 2 if it's also a real problem. */
4903
4904static int
4905decl_jump_unsafe (decl)
4906 tree decl;
4907{
4908 if (TREE_CODE (decl) != VAR_DECL || TREE_STATIC (decl))
4909 return 0;
4910
4911 if (DECL_INITIAL (decl) == NULL_TREE
4912 && pod_type_p (TREE_TYPE (decl)))
4913 return 0;
4914
4915 /* This is really only important if we're crossing an initialization.
4916 The POD stuff is just pedantry; why should it matter if the class
4917 contains a field of pointer to member type? */
4918 if (DECL_INITIAL (decl)
4919 || (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl))))
4920 return 2;
4921 return 1;
4922}
4923
4924/* Check that a single previously seen jump to a newly defined label
4925 is OK. DECL is the LABEL_DECL or 0; LEVEL is the binding_level for
4926 the jump context; NAMES are the names in scope in LEVEL at the jump
4927 context; FILE and LINE are the source position of the jump or 0. */
4928
4929static void
4930check_previous_goto_1 (decl, level, names, file, line)
4931 tree decl;
4932 struct binding_level *level;
4933 tree names;
4934 const char *file;
4935 int line;
4936{
4937 int identified = 0;
4938 int saw_eh = 0;
4939 struct binding_level *b = current_binding_level;
4940 for (; b; b = b->level_chain)
4941 {
4942 tree new_decls = b->names;
4943 tree old_decls = (b == level ? names : NULL_TREE);
4944 for (; new_decls != old_decls;
4945 new_decls = TREE_CHAIN (new_decls))
4946 {
4947 int problem = decl_jump_unsafe (new_decls);
4948 if (! problem)
4949 continue;
4950
4951 if (! identified)
4952 {
4953 if (decl)
4954 cp_pedwarn ("jump to label `%D'", decl);
4955 else
4956 pedwarn ("jump to case label");
4957
4958 if (file)
4959 pedwarn_with_file_and_line (file, line, " from here");
4960 identified = 1;
4961 }
4962
826840d9 4963 if (problem > 1)
6625cdb5
JM
4964 cp_error_at (" crosses initialization of `%#D'",
4965 new_decls);
4966 else
4967 cp_pedwarn_at (" enters scope of non-POD `%#D'",
4968 new_decls);
4969 }
4970
4971 if (b == level)
4972 break;
826840d9 4973 if ((b->is_try_scope || b->is_catch_scope) && ! saw_eh)
6625cdb5
JM
4974 {
4975 if (! identified)
4976 {
4977 if (decl)
4978 cp_pedwarn ("jump to label `%D'", decl);
4979 else
4980 pedwarn ("jump to case label");
4981
4982 if (file)
4983 pedwarn_with_file_and_line (file, line, " from here");
4984 identified = 1;
4985 }
826840d9
RH
4986 if (b->is_try_scope)
4987 error (" enters try block");
4988 else
4989 error (" enters catch block");
6625cdb5
JM
4990 saw_eh = 1;
4991 }
4992 }
4993}
4994
4995static void
4996check_previous_goto (use)
4997 struct named_label_use_list *use;
4998{
4999 check_previous_goto_1 (use->label_decl, use->binding_level,
5000 use->names_in_scope, use->filename_o_goto,
5001 use->lineno_o_goto);
5002}
5003
5004static void
5005check_switch_goto (level)
5006 struct binding_level *level;
5007{
5008 check_previous_goto_1 (NULL_TREE, level, level->names, NULL, 0);
5009}
5010
5011/* Check that any previously seen jumps to a newly defined label DECL
5012 are OK. Called by define_label. */
5013
5014static void
5015check_previous_gotos (decl)
5016 tree decl;
5017{
5018 struct named_label_use_list **usep;
5019
5020 if (! TREE_USED (decl))
5021 return;
5022
5023 for (usep = &named_label_uses; *usep; )
5024 {
5025 struct named_label_use_list *use = *usep;
5026 if (use->label_decl == decl)
5027 {
5028 check_previous_goto (use);
5029 *usep = use->next;
5030 }
5031 else
5032 usep = &(use->next);
5033 }
5034}
5035
5036/* Check that a new jump to a label DECL is OK. Called by
5037 finish_goto_stmt. */
5038
5039void
5040check_goto (decl)
5041 tree decl;
5042{
5043 int identified = 0;
5044 tree bad;
5045 struct named_label_list *lab;
5046
e3cd9945
APB
5047 /* We can't know where a computed goto is jumping. So we assume
5048 that it's OK. */
5049 if (! DECL_P (decl))
5050 return;
5051
6625cdb5
JM
5052 /* If the label hasn't been defined yet, defer checking. */
5053 if (! DECL_INITIAL (decl))
5054 {
5055 use_label (decl);
5056 return;
5057 }
5058
5059 for (lab = named_labels; lab; lab = lab->next)
5060 if (decl == lab->label_decl)
5061 break;
5062
5063 /* If the label is not on named_labels it's a gcc local label, so
5064 it must be in an outer scope, so jumping to it is always OK. */
5065 if (lab == 0)
5066 return;
5067
826840d9
RH
5068 if ((lab->in_try_scope || lab->in_catch_scope || lab->bad_decls)
5069 && !identified)
6625cdb5
JM
5070 {
5071 cp_pedwarn_at ("jump to label `%D'", decl);
5072 pedwarn (" from here");
5073 identified = 1;
5074 }
5075
5076 for (bad = lab->bad_decls; bad; bad = TREE_CHAIN (bad))
5077 {
5078 tree b = TREE_VALUE (bad);
5079 int u = decl_jump_unsafe (b);
5080
5081 if (u > 1 && DECL_ARTIFICIAL (b))
5082 /* Can't skip init of __exception_info. */
5083 cp_error_at (" enters catch block", b);
5084 else if (u > 1)
5085 cp_error_at (" skips initialization of `%#D'", b);
5086 else
5087 cp_pedwarn_at (" enters scope of non-POD `%#D'", b);
5088 }
5089
826840d9 5090 if (lab->in_try_scope)
6625cdb5 5091 error (" enters try block");
826840d9
RH
5092 else if (lab->in_catch_scope)
5093 error (" enters catch block");
6625cdb5
JM
5094}
5095
8d08fdba
MS
5096/* Define a label, specifying the location in the source file.
5097 Return the LABEL_DECL node for the label, if the definition is valid.
5098 Otherwise return 0. */
5099
5100tree
5101define_label (filename, line, name)
3b304f5b 5102 const char *filename;
8d08fdba
MS
5103 int line;
5104 tree name;
5105{
f01b0acb 5106 tree decl = lookup_label (name);
6625cdb5 5107 struct named_label_list *ent;
3dc9aec6 5108 register struct binding_level *p;
6625cdb5
JM
5109
5110 for (ent = named_labels; ent; ent = ent->next)
5111 if (ent->label_decl == decl)
5112 break;
8d08fdba 5113
3dc9aec6 5114 /* After labels, make any new cleanups in the function go into their
8d08fdba 5115 own new (temporary) binding contour. */
3dc9aec6
WC
5116 for (p = current_binding_level; !(p->parm_flag); p = p->level_chain)
5117 p->more_cleanups_ok = 0;
8d08fdba 5118
e1cd6e56 5119 if (name == get_identifier ("wchar_t"))
8251199e 5120 cp_pedwarn ("label named wchar_t");
e1cd6e56 5121
8d08fdba
MS
5122 if (DECL_INITIAL (decl) != NULL_TREE)
5123 {
8251199e 5124 cp_error ("duplicate label `%D'", decl);
8d08fdba
MS
5125 return 0;
5126 }
5127 else
5128 {
8d08fdba
MS
5129 /* Mark label as having been defined. */
5130 DECL_INITIAL (decl) = error_mark_node;
5131 /* Say where in the source. */
5132 DECL_SOURCE_FILE (decl) = filename;
5133 DECL_SOURCE_LINE (decl) = line;
6625cdb5
JM
5134 if (ent)
5135 {
5136 ent->names_in_scope = current_binding_level->names;
5137 ent->binding_level = current_binding_level;
5138 }
5139 check_previous_gotos (decl);
8d08fdba
MS
5140 current_function_return_value = NULL_TREE;
5141 return decl;
5142 }
5143}
5144
a5894242
MS
5145struct cp_switch
5146{
5147 struct binding_level *level;
5148 struct cp_switch *next;
56cb9733
MM
5149 /* The SWITCH_STMT being built. */
5150 tree switch_stmt;
5151 /* A splay-tree mapping the low element of a case range to the high
5152 element, or NULL_TREE if there is no high element. Used to
5153 determine whether or not a new case label duplicates an old case
5154 label. We need a tree, rather than simply a hash table, because
5155 of the GNU case range extension. */
5156 splay_tree cases;
a5894242
MS
5157};
5158
56cb9733
MM
5159/* A stack of the currently active switch statements. The innermost
5160 switch statement is on the top of the stack. There is no need to
5161 mark the stack for garbage collection because it is only active
5162 during the processing of the body of a function, and we never
5163 collect at that point. */
5362b086 5164
a5894242
MS
5165static struct cp_switch *switch_stack;
5166
56cb9733
MM
5167/* Called right after a switch-statement condition is parsed.
5168 SWITCH_STMT is the switch statement being parsed. */
5169
a5894242 5170void
56cb9733
MM
5171push_switch (switch_stmt)
5172 tree switch_stmt;
a5894242
MS
5173{
5174 struct cp_switch *p
bedda2da 5175 = (struct cp_switch *) xmalloc (sizeof (struct cp_switch));
a5894242
MS
5176 p->level = current_binding_level;
5177 p->next = switch_stack;
56cb9733
MM
5178 p->switch_stmt = switch_stmt;
5179 p->cases = splay_tree_new (case_compare, NULL, NULL);
a5894242
MS
5180 switch_stack = p;
5181}
5182
5183void
5184pop_switch ()
5185{
bedda2da 5186 struct cp_switch *cs;
5362b086 5187
bedda2da 5188 cs = switch_stack;
56cb9733 5189 splay_tree_delete (cs->cases);
a5894242 5190 switch_stack = switch_stack->next;
bedda2da 5191 free (cs);
a5894242
MS
5192}
5193
b0a1da19
JM
5194/* Note that we've seen a definition of a case label, and complain if this
5195 is a bad place for one. */
e92cc029 5196
3e4d04a1 5197tree
56cb9733
MM
5198finish_case_label (low_value, high_value)
5199 tree low_value;
5200 tree high_value;
8d08fdba 5201{
3e4d04a1 5202 tree cond, r;
3dc9aec6 5203 register struct binding_level *p;
a5894242 5204
b0a1da19 5205 if (! switch_stack)
56cb9733
MM
5206 {
5207 if (high_value)
5208 error ("case label not within a switch statement");
5209 else if (low_value)
5362b086 5210 cp_error ("case label `%E' not within a switch statement",
56cb9733
MM
5211 low_value);
5212 else
5213 error ("`default' label not within a switch statement");
3e4d04a1 5214 return NULL_TREE;
56cb9733
MM
5215 }
5216
56cb9733
MM
5217 if (processing_template_decl)
5218 {
8f17b5c5
MM
5219 tree label;
5220
56cb9733
MM
5221 /* For templates, just add the case label; we'll do semantic
5222 analysis at instantiation-time. */
8f17b5c5 5223 label = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE);
3e4d04a1 5224 return add_stmt (build_case_label (low_value, high_value, label));
56cb9733
MM
5225 }
5226
5227 /* Find the condition on which this switch statement depends. */
5228 cond = SWITCH_COND (switch_stack->switch_stmt);
5229 if (cond && TREE_CODE (cond) == TREE_LIST)
5230 cond = TREE_VALUE (cond);
56cb9733 5231
3e4d04a1
RH
5232 r = c_add_case_label (switch_stack->cases, cond, low_value, high_value);
5233 if (r == error_mark_node)
5234 r = NULL_TREE;
8d08fdba 5235
6625cdb5 5236 check_switch_goto (switch_stack->level);
a5894242 5237
3dc9aec6 5238 /* After labels, make any new cleanups in the function go into their
8d08fdba 5239 own new (temporary) binding contour. */
3dc9aec6
WC
5240 for (p = current_binding_level; !(p->parm_flag); p = p->level_chain)
5241 p->more_cleanups_ok = 0;
8d08fdba 5242 current_function_return_value = NULL_TREE;
3e4d04a1
RH
5243
5244 return r;
8d08fdba
MS
5245}
5246\f
5247/* Return the list of declarations of the current level.
5248 Note that this list is in reverse order unless/until
5249 you nreverse it; and when you do nreverse it, you must
5250 store the result back using `storedecls' or you will lose. */
5251
5252tree
5253getdecls ()
5254{
5255 return current_binding_level->names;
5256}
5257
5258/* Return the list of type-tags (for structs, etc) of the current level. */
5259
5260tree
5261gettags ()
5262{
5263 return current_binding_level->tags;
5264}
5265
5266/* Store the list of declarations of the current level.
5267 This is done for the parameter declarations of a function being defined,
5268 after they are modified in the light of any missing parameters. */
5269
5270static void
5271storedecls (decls)
5272 tree decls;
5273{
5274 current_binding_level->names = decls;
5275}
5276
5277/* Similarly, store the list of tags of the current level. */
5278
280f9385 5279void
8d08fdba
MS
5280storetags (tags)
5281 tree tags;
5282{
5283 current_binding_level->tags = tags;
5284}
5285\f
5286/* Given NAME, an IDENTIFIER_NODE,
5287 return the structure (or union or enum) definition for that name.
5288 Searches binding levels from BINDING_LEVEL up to the global level.
5289 If THISLEVEL_ONLY is nonzero, searches only the specified context
5290 (but skips any tag-transparent contexts to find one that is
5291 meaningful for tags).
5292 FORM says which kind of type the caller wants;
5293 it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
5294 If the wrong kind of type is found, and it's not a template, an error is
5295 reported. */
5296
5297static tree
5298lookup_tag (form, name, binding_level, thislevel_only)
5299 enum tree_code form;
8d08fdba 5300 tree name;
cffa8729 5301 struct binding_level *binding_level;
8d08fdba
MS
5302 int thislevel_only;
5303{
5304 register struct binding_level *level;
74b846e0
MM
5305 /* Non-zero if, we should look past a template parameter level, even
5306 if THISLEVEL_ONLY. */
5307 int allow_template_parms_p = 1;
8d08fdba
MS
5308
5309 for (level = binding_level; level; level = level->level_chain)
5310 {
5311 register tree tail;
5312 if (ANON_AGGRNAME_P (name))
5313 for (tail = level->tags; tail; tail = TREE_CHAIN (tail))
5314 {
5315 /* There's no need for error checking here, because
5316 anon names are unique throughout the compilation. */
5317 if (TYPE_IDENTIFIER (TREE_VALUE (tail)) == name)
5318 return TREE_VALUE (tail);
5319 }
2c73f9f5
ML
5320 else if (level->namespace_p)
5321 /* Do namespace lookup. */
6c011b01 5322 for (tail = current_namespace; 1; tail = CP_DECL_CONTEXT (tail))
2c73f9f5 5323 {
36a117a5
MM
5324 tree old = binding_for_name (name, tail);
5325
74b846e0
MM
5326 /* If we just skipped past a template parameter level,
5327 even though THISLEVEL_ONLY, and we find a template
5328 class declaration, then we use the _TYPE node for the
36a117a5 5329 template. See the example below. */
74b846e0 5330 if (thislevel_only && !allow_template_parms_p
68642fb6 5331 && old && BINDING_VALUE (old)
36a117a5
MM
5332 && DECL_CLASS_TEMPLATE_P (BINDING_VALUE (old)))
5333 old = TREE_TYPE (BINDING_VALUE (old));
68642fb6 5334 else
36a117a5
MM
5335 old = BINDING_TYPE (old);
5336
2c73f9f5
ML
5337 /* If it has an original type, it is a typedef, and we
5338 should not return it. */
5339 if (old && DECL_ORIGINAL_TYPE (TYPE_NAME (old)))
5340 old = NULL_TREE;
5341 if (old && TREE_CODE (old) != form
5342 && !(form != ENUMERAL_TYPE && TREE_CODE (old) == TEMPLATE_DECL))
5343 {
8251199e 5344 cp_error ("`%#D' redeclared as %C", old, form);
2c73f9f5
ML
5345 return NULL_TREE;
5346 }
5347 if (old)
5348 return old;
5349 if (thislevel_only || tail == global_namespace)
5350 return NULL_TREE;
5351 }
8d08fdba
MS
5352 else
5353 for (tail = level->tags; tail; tail = TREE_CHAIN (tail))
5354 {
a80e4195 5355 if (TREE_PURPOSE (tail) == name)
8d08fdba
MS
5356 {
5357 enum tree_code code = TREE_CODE (TREE_VALUE (tail));
5358 /* Should tighten this up; it'll probably permit
5359 UNION_TYPE and a struct template, for example. */
5360 if (code != form
5566b478 5361 && !(form != ENUMERAL_TYPE && code == TEMPLATE_DECL))
8d08fdba
MS
5362 {
5363 /* Definition isn't the kind we were looking for. */
8251199e 5364 cp_error ("`%#D' redeclared as %C", TREE_VALUE (tail),
8d08fdba 5365 form);
72b7eeff 5366 return NULL_TREE;
8d08fdba
MS
5367 }
5368 return TREE_VALUE (tail);
5369 }
5370 }
5371 if (thislevel_only && ! level->tag_transparent)
5566b478 5372 {
74b846e0 5373 if (level->template_parms_p && allow_template_parms_p)
5566b478 5374 {
36a117a5 5375 /* We must deal with cases like this:
68642fb6 5376
36a117a5
MM
5377 template <class T> struct S;
5378 template <class T> struct S {};
68642fb6 5379
36a117a5
MM
5380 When looking up `S', for the second declaration, we
5381 would like to find the first declaration. But, we
5382 are in the pseudo-global level created for the
5383 template parameters, rather than the (surrounding)
5384 namespace level. Thus, we keep going one more level,
5385 even though THISLEVEL_ONLY is non-zero. */
74b846e0 5386 allow_template_parms_p = 0;
36a117a5 5387 continue;
5566b478 5388 }
36a117a5
MM
5389 else
5390 return NULL_TREE;
5566b478 5391 }
8d08fdba
MS
5392 }
5393 return NULL_TREE;
5394}
5395
bd6dd845 5396#if 0
8d08fdba
MS
5397void
5398set_current_level_tags_transparency (tags_transparent)
5399 int tags_transparent;
5400{
5401 current_binding_level->tag_transparent = tags_transparent;
5402}
bd6dd845 5403#endif
8d08fdba
MS
5404
5405/* Given a type, find the tag that was defined for it and return the tag name.
5406 Otherwise return 0. However, the value can never be 0
5407 in the cases in which this is used.
5408
5409 C++: If NAME is non-zero, this is the new name to install. This is
5410 done when replacing anonymous tags with real tag names. */
5411
5412static tree
5413lookup_tag_reverse (type, name)
5414 tree type;
5415 tree name;
5416{
5417 register struct binding_level *level;
5418
5419 for (level = current_binding_level; level; level = level->level_chain)
5420 {
5421 register tree tail;
5422 for (tail = level->tags; tail; tail = TREE_CHAIN (tail))
5423 {
5424 if (TREE_VALUE (tail) == type)
5425 {
5426 if (name)
5427 TREE_PURPOSE (tail) = name;
5428 return TREE_PURPOSE (tail);
5429 }
5430 }
5431 }
5432 return NULL_TREE;
5433}
8d08fdba 5434\f
a9aedbc2 5435/* Look up NAME in the NAMESPACE. */
e92cc029 5436
a9aedbc2
MS
5437tree
5438lookup_namespace_name (namespace, name)
5439 tree namespace, name;
5440{
30394414 5441 tree val;
f30c84c9 5442 tree template_id = NULL_TREE;
2c73f9f5 5443
30394414 5444 my_friendly_assert (TREE_CODE (namespace) == NAMESPACE_DECL, 370);
1231fb96 5445
1231fb96 5446 if (TREE_CODE (name) == NAMESPACE_DECL)
d8f8dca1 5447 /* This happens for A::B<int> when B is a namespace. */
1231fb96 5448 return name;
d8f8dca1
MM
5449 else if (TREE_CODE (name) == TEMPLATE_DECL)
5450 {
5451 /* This happens for A::B where B is a template, and there are no
5452 template arguments. */
5453 cp_error ("invalid use of `%D'", name);
5454 return error_mark_node;
5455 }
1231fb96 5456
b262d64c
JM
5457 namespace = ORIGINAL_NAMESPACE (namespace);
5458
f30c84c9
MM
5459 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
5460 {
5461 template_id = name;
5462 name = TREE_OPERAND (name, 0);
5463 if (TREE_CODE (name) == OVERLOAD)
5464 name = DECL_NAME (OVL_CURRENT (name));
2f939d94 5465 else if (DECL_P (name))
f30c84c9
MM
5466 name = DECL_NAME (name);
5467 }
5468
30394414 5469 my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, 373);
68642fb6 5470
87e3dbc9 5471 val = make_node (CPLUS_BINDING);
52c11ef6 5472 if (!qualified_lookup_using_namespace (name, namespace, val, 0))
2c73f9f5
ML
5473 return error_mark_node;
5474
5475 if (BINDING_VALUE (val))
1c35f5b6
JM
5476 {
5477 val = BINDING_VALUE (val);
5478
f30c84c9
MM
5479 if (template_id)
5480 {
5481 if (DECL_CLASS_TEMPLATE_P (val))
68642fb6 5482 val = lookup_template_class (val,
f30c84c9
MM
5483 TREE_OPERAND (template_id, 1),
5484 /*in_decl=*/NULL_TREE,
5485 /*context=*/NULL_TREE,
f9c244b8
NS
5486 /*entering_scope=*/0,
5487 /*complain=*/1);
f30c84c9
MM
5488 else if (DECL_FUNCTION_TEMPLATE_P (val)
5489 || TREE_CODE (val) == OVERLOAD)
68642fb6 5490 val = lookup_template_function (val,
f30c84c9
MM
5491 TREE_OPERAND (template_id, 1));
5492 else
5493 {
5494 cp_error ("`%D::%D' is not a template",
5495 namespace, name);
5496 return error_mark_node;
5497 }
5498 }
5499
1c35f5b6
JM
5500 /* If we have a single function from a using decl, pull it out. */
5501 if (TREE_CODE (val) == OVERLOAD && ! really_overloaded_fn (val))
5502 val = OVL_FUNCTION (val);
5503 return val;
5504 }
5505
8251199e 5506 cp_error ("`%D' undeclared in namespace `%D'", name, namespace);
30394414 5507 return error_mark_node;
a9aedbc2
MS
5508}
5509
7ddedda4
MM
5510/* Hash a TYPENAME_TYPE. K is really of type `tree'. */
5511
5512static unsigned long
5513typename_hash (k)
5514 hash_table_key k;
5515{
5516 unsigned long hash;
5517 tree t;
5518
5519 t = (tree) k;
5520 hash = (((unsigned long) TYPE_CONTEXT (t))
5521 ^ ((unsigned long) DECL_NAME (TYPE_NAME (t))));
5522
5523 return hash;
5524}
5525
5526/* Compare two TYPENAME_TYPEs. K1 and K2 are really of type `tree'. */
5527
d6edb99e 5528static bool
7ddedda4
MM
5529typename_compare (k1, k2)
5530 hash_table_key k1;
5531 hash_table_key k2;
5532{
5533 tree t1;
5534 tree t2;
5535 tree d1;
5536 tree d2;
5537
5538 t1 = (tree) k1;
5539 t2 = (tree) k2;
5540 d1 = TYPE_NAME (t1);
5541 d2 = TYPE_NAME (t2);
68642fb6 5542
7ddedda4
MM
5543 return (DECL_NAME (d1) == DECL_NAME (d2)
5544 && same_type_p (TYPE_CONTEXT (t1), TYPE_CONTEXT (t2))
68642fb6 5545 && ((TREE_TYPE (t1) != NULL_TREE)
7ddedda4
MM
5546 == (TREE_TYPE (t2) != NULL_TREE))
5547 && same_type_p (TREE_TYPE (t1), TREE_TYPE (t2))
5548 && TYPENAME_TYPE_FULLNAME (t1) == TYPENAME_TYPE_FULLNAME (t2));
5549}
5550
45869a6c
MM
5551/* Build a TYPENAME_TYPE. If the type is `typename T::t', CONTEXT is
5552 the type of `T', NAME is the IDENTIFIER_NODE for `t'. If BASE_TYPE
5553 is non-NULL, this type is being created by the implicit typename
5554 extension, and BASE_TYPE is a type named `t' in some base class of
68642fb6 5555 `T' which depends on template parameters.
45869a6c
MM
5556
5557 Returns the new TYPENAME_TYPE. */
5558
5559tree
5560build_typename_type (context, name, fullname, base_type)
5561 tree context;
5562 tree name;
5563 tree fullname;
5564 tree base_type;
5565{
5566 tree t;
5567 tree d;
400500c4 5568 struct hash_entry *e;
45869a6c 5569
7ddedda4
MM
5570 static struct hash_table ht;
5571
9cd64686
MM
5572 if (!ht.table)
5573 {
5574 static struct hash_table *h = &ht;
400500c4
RK
5575
5576 hash_table_init (&ht, &hash_newfunc, &typename_hash, &typename_compare);
9cd64686
MM
5577 ggc_add_tree_hash_table_root (&h, 1);
5578 }
45869a6c
MM
5579
5580 /* Build the TYPENAME_TYPE. */
33848bb0 5581 t = make_aggr_type (TYPENAME_TYPE);
45869a6c
MM
5582 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
5583 TYPENAME_TYPE_FULLNAME (t) = fullname;
5584 TREE_TYPE (t) = base_type;
45869a6c
MM
5585
5586 /* Build the corresponding TYPE_DECL. */
5587 d = build_decl (TYPE_DECL, name, t);
5588 TYPE_NAME (TREE_TYPE (d)) = d;
5589 TYPE_STUB_DECL (TREE_TYPE (d)) = d;
5590 DECL_CONTEXT (d) = FROB_CONTEXT (context);
2642b9bf 5591 DECL_ARTIFICIAL (d) = 1;
45869a6c 5592
7ddedda4
MM
5593 /* See if we already have this type. */
5594 e = hash_lookup (&ht, t, /*create=*/false, /*copy=*/0);
5595 if (e)
87e3dbc9 5596 t = (tree) e->key;
7ddedda4
MM
5597 else
5598 /* Insert the type into the table. */
5599 hash_lookup (&ht, t, /*create=*/true, /*copy=*/0);
5600
45869a6c
MM
5601 return t;
5602}
5603
3baa501d
MM
5604/* Resolve `typename CONTEXT::NAME'. Returns an appropriate type,
5605 unless an error occurs, in which case error_mark_node is returned.
5606 If COMPLAIN zero, don't complain about any errors that occur. */
5607
5566b478 5608tree
3baa501d 5609make_typename_type (context, name, complain)
5566b478 5610 tree context, name;
3baa501d 5611 int complain;
5566b478 5612{
b2b7d40a 5613 tree fullname;
a80e4195 5614
2f939d94 5615 if (TYPE_P (name))
78638e24 5616 {
68642fb6
UD
5617 if (!(TYPE_LANG_SPECIFIC (name)
5618 && (CLASSTYPE_IS_TEMPLATE (name)
78638e24
MM
5619 || CLASSTYPE_USE_TEMPLATE (name))))
5620 name = TYPE_IDENTIFIER (name);
5621 else
5622 /* Create a TEMPLATE_ID_EXPR for the type. */
5623 name = build_nt (TEMPLATE_ID_EXPR,
5624 CLASSTYPE_TI_TEMPLATE (name),
5625 CLASSTYPE_TI_ARGS (name));
5626 }
653cc74a 5627 else if (TREE_CODE (name) == TYPE_DECL)
a80e4195 5628 name = DECL_NAME (name);
b2b7d40a
JM
5629
5630 fullname = name;
5631
5632 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
11686454
JM
5633 {
5634 name = TREE_OPERAND (name, 0);
5635 if (TREE_CODE (name) == TEMPLATE_DECL)
5636 name = TREE_OPERAND (fullname, 0) = DECL_NAME (name);
5637 }
b2b7d40a 5638 if (TREE_CODE (name) != IDENTIFIER_NODE)
a80e4195 5639 my_friendly_abort (2000);
5566b478 5640
04ddee1b
BK
5641 if (TREE_CODE (context) == NAMESPACE_DECL)
5642 {
5643 /* We can get here from typename_sub0 in the explicit_template_type
5644 expansion. Just fail. */
3baa501d
MM
5645 if (complain)
5646 cp_error ("no class template named `%#T' in `%#T'",
5647 name, context);
04ddee1b
BK
5648 return error_mark_node;
5649 }
5650
85b71cf2 5651 if (! uses_template_parms (context)
b77ead33 5652 || currently_open_class (context))
5566b478 5653 {
b2b7d40a
JM
5654 if (TREE_CODE (fullname) == TEMPLATE_ID_EXPR)
5655 {
ad810b22 5656 tree tmpl = NULL_TREE;
b2b7d40a 5657 if (IS_AGGR_TYPE (context))
ad810b22
MM
5658 tmpl = lookup_field (context, name, 0, 0);
5659 if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl))
b2b7d40a 5660 {
3baa501d
MM
5661 if (complain)
5662 cp_error ("no class template named `%#T' in `%#T'",
5663 name, context);
b2b7d40a
JM
5664 return error_mark_node;
5665 }
ffb690bd 5666
68642fb6 5667 return lookup_template_class (tmpl,
ad810b22 5668 TREE_OPERAND (fullname, 1),
68642fb6 5669 NULL_TREE, context,
f9c244b8
NS
5670 /*entering_scope=*/0,
5671 /*complain=*/1);
b2b7d40a
JM
5672 }
5673 else
5566b478 5674 {
b4f70b3d 5675 tree t;
68642fb6 5676
b4f70b3d 5677 if (!IS_AGGR_TYPE (context))
b2b7d40a 5678 {
3baa501d
MM
5679 if (complain)
5680 cp_error ("no type named `%#T' in `%#T'", name, context);
b2b7d40a
JM
5681 return error_mark_node;
5682 }
1107c4b3 5683
b4f70b3d 5684 t = lookup_field (context, name, 0, 1);
7d4bdeed
MM
5685 if (t)
5686 return TREE_TYPE (t);
5566b478 5687 }
5566b478 5688 }
11249cf0
MM
5689
5690 /* If the CONTEXT is not a template type, then either the field is
5691 there now or its never going to be. */
b4f70b3d 5692 if (!uses_template_parms (context))
11249cf0 5693 {
3baa501d
MM
5694 if (complain)
5695 cp_error ("no type named `%#T' in `%#T'", name, context);
11249cf0
MM
5696 return error_mark_node;
5697 }
68642fb6
UD
5698
5699
45869a6c 5700 return build_typename_type (context, name, fullname, NULL_TREE);
5566b478
MS
5701}
5702
2c73f9f5
ML
5703/* Select the right _DECL from multiple choices. */
5704
5705static tree
52c11ef6 5706select_decl (binding, flags)
2c73f9f5 5707 tree binding;
52c11ef6 5708 int flags;
2c73f9f5
ML
5709{
5710 tree val;
5711 val = BINDING_VALUE (binding);
97ba1e3c
MM
5712
5713 /* When we implicitly declare some builtin entity, we mark it
5714 DECL_ANTICIPATED, so that we know to ignore it until it is
5715 really declared. */
5362b086
EC
5716 if (val && DECL_P (val)
5717 && DECL_LANG_SPECIFIC (val)
97ba1e3c
MM
5718 && DECL_ANTICIPATED (val))
5719 return NULL_TREE;
5720
52c11ef6 5721 if (LOOKUP_NAMESPACES_ONLY (flags))
3e3f722c
ML
5722 {
5723 /* We are not interested in types. */
5724 if (val && TREE_CODE (val) == NAMESPACE_DECL)
5725 return val;
5726 return NULL_TREE;
5727 }
68642fb6 5728
2c73f9f5
ML
5729 /* If we could have a type and
5730 we have nothing or we need a type and have none. */
5731 if (BINDING_TYPE (binding)
52c11ef6
JM
5732 && (!val || ((flags & LOOKUP_PREFER_TYPES)
5733 && TREE_CODE (val) != TYPE_DECL)))
2c73f9f5
ML
5734 val = TYPE_STUB_DECL (BINDING_TYPE (binding));
5735 /* Don't return non-types if we really prefer types. */
52c11ef6 5736 else if (val && LOOKUP_TYPES_ONLY (flags) && TREE_CODE (val) != TYPE_DECL
c592d5d2
ML
5737 && (TREE_CODE (val) != TEMPLATE_DECL
5738 || !DECL_CLASS_TEMPLATE_P (val)))
2c73f9f5 5739 val = NULL_TREE;
1c35f5b6 5740
2c73f9f5
ML
5741 return val;
5742}
5743
2c169bab
JM
5744/* Unscoped lookup of a global: iterate over current namespaces,
5745 considering using-directives. If SPACESP is non-NULL, store a list
5746 of the namespaces we've considered in it. */
ea9635c7 5747
2c169bab
JM
5748tree
5749unqualified_namespace_lookup (name, flags, spacesp)
ea9635c7 5750 tree name;
52c11ef6 5751 int flags;
2c169bab 5752 tree *spacesp;
ea9635c7 5753{
87e3dbc9 5754 tree b = make_node (CPLUS_BINDING);
ea9635c7
ML
5755 tree initial = current_decl_namespace();
5756 tree scope = initial;
5757 tree siter;
5758 struct binding_level *level;
5759 tree val = NULL_TREE;
5760
2c169bab
JM
5761 if (spacesp)
5762 *spacesp = NULL_TREE;
5763
5764 for (; !val; scope = CP_DECL_CONTEXT (scope))
ea9635c7 5765 {
2c169bab 5766 if (spacesp)
e1b3e07d 5767 *spacesp = tree_cons (scope, NULL_TREE, *spacesp);
ea9635c7
ML
5768 val = binding_for_name (name, scope);
5769
5770 /* Initialize binding for this context. */
5771 BINDING_VALUE (b) = BINDING_VALUE (val);
5772 BINDING_TYPE (b) = BINDING_TYPE (val);
5773
5774 /* Add all _DECLs seen through local using-directives. */
68642fb6 5775 for (level = current_binding_level;
ea9635c7
ML
5776 !level->namespace_p;
5777 level = level->level_chain)
52c11ef6 5778 if (!lookup_using_namespace (name, b, level->using_directives,
2c169bab 5779 scope, flags, spacesp))
ea9635c7 5780 /* Give up because of error. */
5b163de4 5781 return error_mark_node;
ea9635c7
ML
5782
5783 /* Add all _DECLs seen through global using-directives. */
5784 /* XXX local and global using lists should work equally. */
5785 siter = initial;
5786 while (1)
5787 {
68642fb6 5788 if (!lookup_using_namespace (name, b, DECL_NAMESPACE_USING (siter),
2c169bab 5789 scope, flags, spacesp))
ea9635c7 5790 /* Give up because of error. */
5b163de4 5791 return error_mark_node;
ea9635c7
ML
5792 if (siter == scope) break;
5793 siter = CP_DECL_CONTEXT (siter);
5794 }
5795
52c11ef6 5796 val = select_decl (b, flags);
ea9635c7
ML
5797 if (scope == global_namespace)
5798 break;
ea9635c7
ML
5799 }
5800 return val;
5801}
5802
52c11ef6
JM
5803/* Combine prefer_type and namespaces_only into flags. */
5804
5805static int
5806lookup_flags (prefer_type, namespaces_only)
5807 int prefer_type, namespaces_only;
5808{
5809 if (namespaces_only)
5810 return LOOKUP_PREFER_NAMESPACES;
5811 if (prefer_type > 1)
5812 return LOOKUP_PREFER_TYPES;
5813 if (prefer_type > 0)
5814 return LOOKUP_PREFER_BOTH;
5815 return 0;
5816}
5817
5818/* Given a lookup that returned VAL, use FLAGS to decide if we want to
5819 ignore it or not. Subroutine of lookup_name_real. */
5820
5821static tree
5822qualify_lookup (val, flags)
5823 tree val;
5824 int flags;
5825{
5826 if (val == NULL_TREE)
5827 return val;
6b945830
JM
5828 if ((flags & LOOKUP_PREFER_NAMESPACES) && TREE_CODE (val) == NAMESPACE_DECL)
5829 return val;
5830 if ((flags & LOOKUP_PREFER_TYPES)
5831 && (TREE_CODE (val) == TYPE_DECL
5832 || ((flags & LOOKUP_TEMPLATES_EXPECTED)
5833 && DECL_CLASS_TEMPLATE_P (val))))
5834 return val;
5835 if (flags & (LOOKUP_PREFER_NAMESPACES | LOOKUP_PREFER_TYPES))
52c11ef6
JM
5836 return NULL_TREE;
5837 return val;
5838}
5839
235f734d
MM
5840/* Any other BINDING overrides an implicit TYPENAME. Warn about
5841 that. */
5842
5843static void
5844warn_about_implicit_typename_lookup (typename, binding)
5845 tree typename;
5846 tree binding;
5847{
5848 tree subtype = TREE_TYPE (TREE_TYPE (typename));
5849 tree name = DECL_NAME (typename);
5850
5851 if (! (TREE_CODE (binding) == TEMPLATE_DECL
5852 && CLASSTYPE_TEMPLATE_INFO (subtype)
5853 && CLASSTYPE_TI_TEMPLATE (subtype) == binding)
5854 && ! (TREE_CODE (binding) == TYPE_DECL
5855 && same_type_p (TREE_TYPE (binding), subtype)))
5856 {
68642fb6 5857 cp_warning ("lookup of `%D' finds `%#D'",
235f734d
MM
5858 name, binding);
5859 cp_warning (" instead of `%D' from dependent base class",
5860 typename);
5861 cp_warning (" (use `typename %T::%D' if that's what you meant)",
5862 constructor_name (current_class_type), name);
5863 }
5864}
5865
8d08fdba
MS
5866/* Look up NAME in the current binding level and its superiors in the
5867 namespace of variables, functions and typedefs. Return a ..._DECL
5868 node of some kind representing its definition if there is only one
5869 such declaration, or return a TREE_LIST with all the overloaded
5870 definitions if there are many, or return 0 if it is undefined.
5871
2c73f9f5
ML
5872 If PREFER_TYPE is > 0, we prefer TYPE_DECLs or namespaces.
5873 If PREFER_TYPE is > 1, we reject non-type decls (e.g. namespaces).
a28e3c7f 5874 If PREFER_TYPE is -2, we're being called from yylex(). (UGLY)
68642fb6 5875 Otherwise we prefer non-TYPE_DECLs.
f84b4be9
JM
5876
5877 If NONCLASS is non-zero, we don't look for the NAME in class scope,
5878 using IDENTIFIER_CLASS_VALUE. */
8d08fdba 5879
824b9a4c 5880static tree
3e3f722c 5881lookup_name_real (name, prefer_type, nonclass, namespaces_only)
8d08fdba 5882 tree name;
3e3f722c 5883 int prefer_type, nonclass, namespaces_only;
8d08fdba 5884{
235f734d
MM
5885 tree t;
5886 tree val = NULL_TREE;
a28e3c7f 5887 int yylex = 0;
e1cd6e56 5888 tree from_obj = NULL_TREE;
52c11ef6 5889 int flags;
235f734d 5890 int val_is_implicit_typename = 0;
8d08fdba 5891
3e3f722c
ML
5892 /* Hack: copy flag set by parser, if set. */
5893 if (only_namespace_names)
5894 namespaces_only = 1;
52c11ef6 5895
a28e3c7f
MS
5896 if (prefer_type == -2)
5897 {
5898 extern int looking_for_typename;
fc378698 5899 tree type = NULL_TREE;
a28e3c7f
MS
5900
5901 yylex = 1;
5902 prefer_type = looking_for_typename;
e1cd6e56 5903
52c11ef6 5904 flags = lookup_flags (prefer_type, namespaces_only);
52c11ef6
JM
5905 /* If the next thing is '<', class templates are types. */
5906 if (looking_for_template)
5907 flags |= LOOKUP_TEMPLATES_EXPECTED;
5908
653cc74a 5909 /* std:: becomes :: for now. */
6bcedb4e 5910 if (got_scope && got_scope == fake_std_node)
653cc74a
JM
5911 got_scope = void_type_node;
5912
e1cd6e56
MS
5913 if (got_scope)
5914 type = got_scope;
dff6b454 5915 else if (got_object != error_mark_node)
e1cd6e56 5916 type = got_object;
68642fb6 5917
e1cd6e56 5918 if (type)
a28e3c7f 5919 {
e1cd6e56 5920 if (type == error_mark_node)
f376e137 5921 return error_mark_node;
a80e4195
MS
5922 if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type))
5923 type = TREE_TYPE (type);
5566b478 5924
2b9dc906
JM
5925 if (TYPE_P (type))
5926 type = complete_type (type);
5566b478 5927
a1774733 5928 if (TREE_CODE (type) == VOID_TYPE)
2c73f9f5
ML
5929 type = global_namespace;
5930 if (TREE_CODE (type) == NAMESPACE_DECL)
a9aedbc2 5931 {
87e3dbc9 5932 val = make_node (CPLUS_BINDING);
6ad07332 5933 flags |= LOOKUP_COMPLAIN;
52c11ef6 5934 if (!qualified_lookup_using_namespace (name, type, val, flags))
2c73f9f5 5935 return NULL_TREE;
52c11ef6 5936 val = select_decl (val, flags);
a9aedbc2 5937 }
5566b478 5938 else if (! IS_AGGR_TYPE (type)
5156628f 5939 || TREE_CODE (type) == TEMPLATE_TYPE_PARM
a1281f45 5940 || TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM
5156628f 5941 || TREE_CODE (type) == TYPENAME_TYPE)
e92cc029 5942 /* Someone else will give an error about this if needed. */
a28e3c7f 5943 val = NULL_TREE;
e1cd6e56 5944 else if (type == current_class_type)
a28e3c7f 5945 val = IDENTIFIER_CLASS_VALUE (name);
a28e3c7f 5946 else
70adf8a9
JM
5947 {
5948 val = lookup_member (type, name, 0, prefer_type);
5949 type_access_control (type, val);
e69c8072
JM
5950
5951 /* Restore the containing TYPENAME_TYPE if we looked
5952 through it before. */
5953 if (got_scope && got_scope != type
5954 && val && TREE_CODE (val) == TYPE_DECL
5955 && TREE_CODE (TREE_TYPE (val)) == TYPENAME_TYPE)
5956 TYPE_CONTEXT (TREE_TYPE (val)) = got_scope;
70adf8a9 5957 }
a28e3c7f 5958 }
e1cd6e56
MS
5959 else
5960 val = NULL_TREE;
5961
594740f3 5962 if (got_scope)
e1cd6e56 5963 goto done;
594740f3 5964 else if (got_object && val)
aca77bd1
JM
5965 {
5966 from_obj = val;
5967 val = NULL_TREE;
5968 }
a28e3c7f 5969 }
52c11ef6 5970 else
5b163de4
JM
5971 {
5972 flags = lookup_flags (prefer_type, namespaces_only);
5973 /* If we're not parsing, we need to complain. */
5974 flags |= LOOKUP_COMPLAIN;
5975 }
e76a2646 5976
d8f8dca1 5977 /* First, look in non-namespace scopes. */
6f1b4c42
JM
5978
5979 if (current_class_type == NULL_TREE)
5980 nonclass = 1;
5981
235f734d 5982 for (t = IDENTIFIER_BINDING (name); t; t = TREE_CHAIN (t))
d8f8dca1 5983 {
235f734d
MM
5984 tree binding;
5985
5986 if (!LOCAL_BINDING_P (t) && nonclass)
d8f8dca1
MM
5987 /* We're not looking for class-scoped bindings, so keep going. */
5988 continue;
68642fb6 5989
d8f8dca1 5990 /* If this is the kind of thing we're looking for, we're done. */
235f734d
MM
5991 if (qualify_lookup (BINDING_VALUE (t), flags))
5992 binding = BINDING_VALUE (t);
68642fb6 5993 else if ((flags & LOOKUP_PREFER_TYPES)
235f734d
MM
5994 && qualify_lookup (BINDING_TYPE (t), flags))
5995 binding = BINDING_TYPE (t);
5996 else
5997 binding = NULL_TREE;
5998
70adf8a9
JM
5999 /* Handle access control on types from enclosing or base classes. */
6000 if (binding && ! yylex
6001 && BINDING_LEVEL (t) && BINDING_LEVEL (t)->parm_flag == 2)
6002 type_access_control (BINDING_LEVEL (t)->this_class, binding);
6003
235f734d 6004 if (binding
83233dca 6005 && (!val || !IMPLICIT_TYPENAME_TYPE_DECL_P (binding)))
d8f8dca1 6006 {
235f734d
MM
6007 if (val_is_implicit_typename && !yylex)
6008 warn_about_implicit_typename_lookup (val, binding);
6009 val = binding;
68642fb6 6010 val_is_implicit_typename
83233dca 6011 = IMPLICIT_TYPENAME_TYPE_DECL_P (val);
235f734d
MM
6012 if (!val_is_implicit_typename)
6013 break;
d8f8dca1
MM
6014 }
6015 }
f181d4ae 6016
235f734d
MM
6017 /* Now lookup in namespace scopes. */
6018 if (!val || val_is_implicit_typename)
e76a2646 6019 {
2c169bab 6020 t = unqualified_namespace_lookup (name, flags, 0);
235f734d 6021 if (t)
c1def683 6022 {
235f734d
MM
6023 if (val_is_implicit_typename && !yylex)
6024 warn_about_implicit_typename_lookup (val, t);
6025 val = t;
c1def683
JM
6026 }
6027 }
6028
a28e3c7f 6029 done:
8d08fdba
MS
6030 if (val)
6031 {
c91a56d2 6032 /* This should only warn about types used in qualified-ids. */
e1cd6e56 6033 if (from_obj && from_obj != val)
5566b478 6034 {
c91a56d2
MS
6035 if (looking_for_typename && TREE_CODE (from_obj) == TYPE_DECL
6036 && TREE_CODE (val) == TYPE_DECL
aca77bd1
JM
6037 && ! same_type_p (TREE_TYPE (from_obj), TREE_TYPE (val)))
6038 cp_pedwarn ("\
bd0d5d4a
JM
6039lookup of `%D' in the scope of `%#T' (`%#D') \
6040does not match lookup in the current scope (`%#D')",
6041 name, got_object, from_obj, val);
594740f3 6042
b8b1a3c1
JM
6043 /* We don't change val to from_obj if got_object depends on
6044 template parms because that breaks implicit typename for
6045 destructor calls. */
6046 if (! uses_template_parms (got_object))
53fdbb3b 6047 val = from_obj;
5566b478 6048 }
e1cd6e56 6049
0c64a9ca
JM
6050 /* If we have a single function from a using decl, pull it out. */
6051 if (TREE_CODE (val) == OVERLOAD && ! really_overloaded_fn (val))
6052 val = OVL_FUNCTION (val);
8d08fdba 6053 }
e1cd6e56
MS
6054 else if (from_obj)
6055 val = from_obj;
8d08fdba
MS
6056
6057 return val;
6058}
6059
700f8a87
MS
6060tree
6061lookup_name_nonclass (name)
6062 tree name;
6063{
3e3f722c 6064 return lookup_name_real (name, 0, 1, 0);
700f8a87
MS
6065}
6066
2c73f9f5
ML
6067tree
6068lookup_function_nonclass (name, args)
6069 tree name;
6070 tree args;
6071{
6072 return lookup_arg_dependent (name, lookup_name_nonclass (name), args);
6073}
6074
3e3f722c
ML
6075tree
6076lookup_name_namespace_only (name)
6077 tree name;
6078{
6079 /* type-or-namespace, nonclass, namespace_only */
6080 return lookup_name_real (name, 1, 1, 1);
6081}
6082
700f8a87
MS
6083tree
6084lookup_name (name, prefer_type)
6085 tree name;
6086 int prefer_type;
6087{
3e3f722c 6088 return lookup_name_real (name, prefer_type, 0, 0);
700f8a87
MS
6089}
6090
a7d2d407
MM
6091/* Similar to `lookup_name' but look only in the innermost non-class
6092 binding level. */
8d08fdba
MS
6093
6094tree
6095lookup_name_current_level (name)
6096 tree name;
6097{
a7d2d407
MM
6098 struct binding_level *b;
6099 tree t = NULL_TREE;
8d08fdba 6100
a7d2d407
MM
6101 b = current_binding_level;
6102 while (b->parm_flag == 2)
6103 b = b->level_chain;
6104
6105 if (b->namespace_p)
8d08fdba 6106 {
5fdaba89 6107 t = IDENTIFIER_NAMESPACE_VALUE (name);
8d08fdba
MS
6108
6109 /* extern "C" function() */
6110 if (t != NULL_TREE && TREE_CODE (t) == TREE_LIST)
6111 t = TREE_VALUE (t);
6112 }
68642fb6 6113 else if (IDENTIFIER_BINDING (name)
f181d4ae 6114 && LOCAL_BINDING_P (IDENTIFIER_BINDING (name)))
8d08fdba 6115 {
a4443a08
MS
6116 while (1)
6117 {
f181d4ae
MM
6118 if (BINDING_LEVEL (IDENTIFIER_BINDING (name)) == b)
6119 return IDENTIFIER_VALUE (name);
68642fb6 6120
9ed182dc
JM
6121 if (b->keep == 2)
6122 b = b->level_chain;
6123 else
6124 break;
6125 }
6126 }
6127
6128 return t;
6129}
6130
6131/* Like lookup_name_current_level, but for types. */
6132
6133tree
6134lookup_type_current_level (name)
6135 tree name;
6136{
6137 register tree t = NULL_TREE;
6138
6139 my_friendly_assert (! current_binding_level->namespace_p, 980716);
6140
6141 if (REAL_IDENTIFIER_TYPE_VALUE (name) != NULL_TREE
6142 && REAL_IDENTIFIER_TYPE_VALUE (name) != global_type_node)
6143 {
6144 struct binding_level *b = current_binding_level;
6145 while (1)
6146 {
6147 if (purpose_member (name, b->type_shadowed))
6148 return REAL_IDENTIFIER_TYPE_VALUE (name);
a4443a08
MS
6149 if (b->keep == 2)
6150 b = b->level_chain;
6151 else
6152 break;
6153 }
8d08fdba
MS
6154 }
6155
6156 return t;
6157}
3e3f722c
ML
6158
6159void
6160begin_only_namespace_names ()
6161{
6162 only_namespace_names = 1;
6163}
6164
6165void
6166end_only_namespace_names ()
6167{
6168 only_namespace_names = 0;
6169}
8d08fdba 6170\f
8d08fdba 6171/* Push the declarations of builtin types into the namespace.
0e5921e8 6172 RID_INDEX is the index of the builtin type
8d08fdba
MS
6173 in the array RID_POINTERS. NAME is the name used when looking
6174 up the builtin type. TYPE is the _TYPE node for the builtin type. */
6175
eaa7c03f 6176void
8d08fdba
MS
6177record_builtin_type (rid_index, name, type)
6178 enum rid rid_index;
d8e178a0 6179 const char *name;
8d08fdba
MS
6180 tree type;
6181{
6182 tree rname = NULL_TREE, tname = NULL_TREE;
a703fb38 6183 tree tdecl = NULL_TREE;
8d08fdba 6184
0e5921e8 6185 if ((int) rid_index < (int) RID_MAX)
8d08fdba
MS
6186 rname = ridpointers[(int) rid_index];
6187 if (name)
6188 tname = get_identifier (name);
6189
6190 TYPE_BUILT_IN (type) = 1;
68642fb6 6191
8d08fdba
MS
6192 if (tname)
6193 {
8d08fdba 6194 tdecl = pushdecl (build_decl (TYPE_DECL, tname, type));
8d08fdba 6195 set_identifier_type_value (tname, NULL_TREE);
0e5921e8 6196 if ((int) rid_index < (int) RID_MAX)
2c73f9f5
ML
6197 /* Built-in types live in the global namespace. */
6198 SET_IDENTIFIER_GLOBAL_VALUE (tname, tdecl);
8d08fdba
MS
6199 }
6200 if (rname != NULL_TREE)
6201 {
6202 if (tname != NULL_TREE)
6203 {
6204 set_identifier_type_value (rname, NULL_TREE);
2c73f9f5 6205 SET_IDENTIFIER_GLOBAL_VALUE (rname, tdecl);
8d08fdba
MS
6206 }
6207 else
6208 {
8d08fdba 6209 tdecl = pushdecl (build_decl (TYPE_DECL, rname, type));
8d08fdba
MS
6210 set_identifier_type_value (rname, NULL_TREE);
6211 }
6212 }
8d08fdba
MS
6213}
6214
eff71ab0 6215/* Record one of the standard Java types.
4d8a1dd6
MM
6216 * Declare it as having the given NAME.
6217 * If SIZE > 0, it is the size of one of the integral types;
6218 * otherwise it is the negative of the size of one of the other types. */
eff71ab0
PB
6219
6220static tree
6221record_builtin_java_type (name, size)
d8e178a0 6222 const char *name;
eff71ab0
PB
6223 int size;
6224{
6225 tree type, decl;
6226 if (size > 0)
6227 type = make_signed_type (size);
6228 else if (size > -32)
6229 { /* "__java_char" or ""__java_boolean". */
6230 type = make_unsigned_type (-size);
6231 /*if (size == -1) TREE_SET_CODE (type, BOOLEAN_TYPE);*/
6232 }
6233 else
6234 { /* "__java_float" or ""__java_double". */
6235 type = make_node (REAL_TYPE);
6236 TYPE_PRECISION (type) = - size;
6237 layout_type (type);
6238 }
0e5921e8 6239 record_builtin_type (RID_MAX, name, type);
eff71ab0 6240 decl = TYPE_NAME (type);
e229f2cd
PB
6241
6242 /* Suppress generate debug symbol entries for these types,
6243 since for normal C++ they are just clutter.
6244 However, push_lang_context undoes this if extern "Java" is seen. */
eff71ab0 6245 DECL_IGNORED_P (decl) = 1;
e229f2cd 6246
eff71ab0
PB
6247 TYPE_FOR_JAVA (type) = 1;
6248 return type;
6249}
6250
036407f7
ML
6251/* Push a type into the namespace so that the back-ends ignore it. */
6252
6253static void
6254record_unknown_type (type, name)
6255 tree type;
d8e178a0 6256 const char *name;
036407f7
ML
6257{
6258 tree decl = pushdecl (build_decl (TYPE_DECL, get_identifier (name), type));
6259 /* Make sure the "unknown type" typedecl gets ignored for debug info. */
6260 DECL_IGNORED_P (decl) = 1;
6261 TYPE_DECL_SUPPRESS_DEBUG (decl) = 1;
6262 TYPE_SIZE (type) = TYPE_SIZE (void_type_node);
6263 TYPE_ALIGN (type) = 1;
11cf4d18 6264 TYPE_USER_ALIGN (type) = 0;
036407f7 6265 TYPE_MODE (type) = TYPE_MODE (void_type_node);
68642fb6 6266}
036407f7 6267
d43829f9
MM
6268/* An string for which we should create an IDENTIFIER_NODE at
6269 startup. */
6270
6271typedef struct predefined_identifier
6272{
6273 /* The name of the identifier. */
6274 const char *name;
6275 /* The place where the IDENTIFIER_NODE should be stored. */
6276 tree *node;
298d6f60
MM
6277 /* Non-zero if this is the name of a constructor or destructor. */
6278 int ctor_or_dtor_p;
d43829f9
MM
6279} predefined_identifier;
6280
6281/* Create all the predefined identifiers. */
6282
6283static void
5362b086 6284initialize_predefined_identifiers ()
d43829f9
MM
6285{
6286 struct predefined_identifier *pid;
6287
6288 /* A table of identifiers to create at startup. */
6289 static predefined_identifier predefined_identifiers[] = {
298d6f60
MM
6290 { "C++", &lang_name_cplusplus, 0 },
6291 { "C", &lang_name_c, 0 },
6292 { "Java", &lang_name_java, 0 },
6293 { CTOR_NAME, &ctor_identifier, 1 },
6294 { "__base_ctor", &base_ctor_identifier, 1 },
6295 { "__comp_ctor", &complete_ctor_identifier, 1 },
6296 { DTOR_NAME, &dtor_identifier, 1 },
6297 { "__comp_dtor", &complete_dtor_identifier, 1 },
6298 { "__base_dtor", &base_dtor_identifier, 1 },
6299 { "__deleting_dtor", &deleting_dtor_identifier, 1 },
6300 { VTABLE_DELTA2_NAME, &delta2_identifier, 0 },
6301 { VTABLE_DELTA_NAME, &delta_identifier, 0 },
6302 { IN_CHARGE_NAME, &in_charge_identifier, 0 },
6303 { VTABLE_INDEX_NAME, &index_identifier, 0 },
6304 { "nelts", &nelts_identifier, 0 },
6305 { THIS_NAME, &this_identifier, 0 },
6306 { VTABLE_PFN_NAME, &pfn_identifier, 0 },
6307 { "__pfn_or_delta2", &pfn_or_delta2_identifier, 0 },
6308 { "_vptr", &vptr_identifier, 0 },
3ec6bad3 6309 { "__vtt_parm", &vtt_parm_identifier, 0 },
1f6e1acc 6310 { "std", &std_identifier, 0 },
298d6f60 6311 { NULL, NULL, 0 }
d43829f9
MM
6312 };
6313
6314 for (pid = predefined_identifiers; pid->name; ++pid)
298d6f60
MM
6315 {
6316 *pid->node = get_identifier (pid->name);
6317 if (pid->ctor_or_dtor_p)
6318 IDENTIFIER_CTOR_OR_DTOR_P (*pid->node) = 1;
6319 }
d43829f9
MM
6320}
6321
8d08fdba
MS
6322/* Create the predefined scalar types of C,
6323 and some nodes representing standard constants (0, 1, (void *)0).
6324 Initialize the global binding level.
6325 Make definitions for built-in primitive functions. */
6326
6327void
6328init_decl_processing ()
6329{
8d08fdba 6330 tree fields[20];
8d08fdba 6331
5bb2a292
MM
6332 /* Check to see that the user did not specify an invalid combination
6333 of command-line options. */
1f84ec23
MM
6334 if (!flag_vtable_thunks)
6335 error ("the ABI requires vtable thunks");
5bb2a292 6336
d43829f9
MM
6337 /* Create all the identifiers we need. */
6338 initialize_predefined_identifiers ();
8d08fdba 6339
8f17b5c5 6340 /* Fill in back-end hooks. */
a8f73d4b
MM
6341 init_lang_status = &push_cp_function_context;
6342 free_lang_status = &pop_cp_function_context;
b4b8bee7 6343 mark_lang_status = &mark_cp_function_context;
8f17b5c5 6344 lang_safe_from_p = &c_safe_from_p;
0a7394bc 6345 lang_dump_tree = &cp_dump_tree;
d363e7bf 6346 lang_missing_noreturn_ok_p = &cp_missing_noreturn_ok_p;
99dccabc 6347
fc6af6e3
RH
6348 cp_parse_init ();
6349 init_decl2 ();
9cd64686 6350 init_pt ();
fc6af6e3 6351
9cd64686
MM
6352 /* Create the global variables. */
6353 push_to_top_level ();
8012c983 6354
2c73f9f5 6355 /* Enter the global namespace. */
30394414 6356 my_friendly_assert (global_namespace == NULL_TREE, 375);
30394414
JM
6357 push_namespace (get_identifier ("::"));
6358 global_namespace = current_namespace;
6359 current_lang_name = NULL_TREE;
6360
bccd95ae 6361 /* Adjust various flags based on command-line settings. */
2642b9bf
JM
6362 if (! flag_permissive && ! pedantic)
6363 flag_pedantic_errors = 1;
bccd95ae 6364 if (!flag_no_inline)
b850de4f
MM
6365 {
6366 flag_inline_trees = 1;
6367 flag_no_inline = 1;
6368 }
830fcda8 6369
8d08fdba
MS
6370 /* Initially, C. */
6371 current_lang_name = lang_name_c;
6372
6373 current_function_decl = NULL_TREE;
8d08fdba
MS
6374 current_binding_level = NULL_BINDING_LEVEL;
6375 free_binding_level = NULL_BINDING_LEVEL;
6376
81b3411c
BS
6377 build_common_tree_nodes (flag_signed_char);
6378
8d08fdba
MS
6379 error_mark_list = build_tree_list (error_mark_node, error_mark_node);
6380 TREE_TYPE (error_mark_list) = error_mark_node;
6381
a28e3c7f
MS
6382 /* Make the binding_level structure for global names. */
6383 pushlevel (0);
8d08fdba 6384 global_binding_level = current_binding_level;
2c73f9f5
ML
6385 /* The global level is the namespace level of ::. */
6386 NAMESPACE_LEVEL (global_namespace) = global_binding_level;
6387 declare_namespace_level ();
8d08fdba 6388
6bcedb4e
MM
6389 /* Create the `std' namespace. */
6390 if (flag_honor_std)
6391 {
6392 push_namespace (std_identifier);
6393 std_node = current_namespace;
6394 pop_namespace ();
6395 fake_std_node = error_mark_node;
6396 }
6397 else
6398 {
6399 fake_std_node = build_decl (NAMESPACE_DECL,
6400 std_identifier,
6401 void_type_node);
6402 pushdecl (fake_std_node);
6403 }
5362b086 6404
eaa7c03f 6405 c_common_nodes_and_builtins ();
37c46b43 6406
4d8a1dd6
MM
6407 java_byte_type_node = record_builtin_java_type ("__java_byte", 8);
6408 java_short_type_node = record_builtin_java_type ("__java_short", 16);
6409 java_int_type_node = record_builtin_java_type ("__java_int", 32);
6410 java_long_type_node = record_builtin_java_type ("__java_long", 64);
6411 java_float_type_node = record_builtin_java_type ("__java_float", -32);
6412 java_double_type_node = record_builtin_java_type ("__java_double", -64);
6413 java_char_type_node = record_builtin_java_type ("__java_char", -16);
6414 java_boolean_type_node = record_builtin_java_type ("__java_boolean", -1);
eff71ab0 6415
8d08fdba
MS
6416 integer_two_node = build_int_2 (2, 0);
6417 TREE_TYPE (integer_two_node) = integer_type_node;
6418 integer_three_node = build_int_2 (3, 0);
6419 TREE_TYPE (integer_three_node) = integer_type_node;
8d08fdba 6420
255512c1
JM
6421 boolean_type_node = make_unsigned_type (BOOL_TYPE_SIZE);
6422 TREE_SET_CODE (boolean_type_node, BOOLEAN_TYPE);
c1ea6a0b
BK
6423 TYPE_MAX_VALUE (boolean_type_node) = build_int_2 (1, 0);
6424 TREE_TYPE (TYPE_MAX_VALUE (boolean_type_node)) = boolean_type_node;
6425 TYPE_PRECISION (boolean_type_node) = 1;
255512c1
JM
6426 record_builtin_type (RID_BOOL, "bool", boolean_type_node);
6427 boolean_false_node = build_int_2 (0, 0);
6428 TREE_TYPE (boolean_false_node) = boolean_type_node;
6429 boolean_true_node = build_int_2 (1, 0);
6430 TREE_TYPE (boolean_true_node) = boolean_type_node;
2986ae00 6431
ff0bee63 6432 signed_size_zero_node = build_int_2 (0, 0);
e92cc029
MS
6433 TREE_TYPE (signed_size_zero_node) = make_signed_type (TYPE_PRECISION (sizetype));
6434
4cc1d462 6435 empty_except_spec = build_tree_list (NULL_TREE, NULL_TREE);
5fd8e536
JM
6436
6437#if 0
6438 record_builtin_type (RID_MAX, NULL_PTR, string_type_node);
6439#endif
6440
1f84ec23
MM
6441 delta_type_node = ptrdiff_type_node;
6442 vtable_index_type = ptrdiff_type_node;
c7e266a6 6443
3ec6bad3 6444 vtt_parm_type = build_pointer_type (const_ptr_type_node);
356955cf 6445 lang_type_promotes_to = convert_type_from_ellipsis;
7f4edbcb 6446
824b9a4c 6447 void_ftype_ptr
4cc1d462 6448 = build_exception_variant (void_ftype_ptr, empty_except_spec);
8d08fdba 6449
8d08fdba
MS
6450 /* C++ extensions */
6451
6452 unknown_type_node = make_node (UNKNOWN_TYPE);
036407f7
ML
6453 record_unknown_type (unknown_type_node, "unknown type");
6454
8d08fdba
MS
6455 /* Indirecting an UNKNOWN_TYPE node yields an UNKNOWN_TYPE node. */
6456 TREE_TYPE (unknown_type_node) = unknown_type_node;
a6967cc0 6457
03d0f4af 6458 TREE_TYPE (null_node) = type_for_size (POINTER_SIZE, 0);
a6967cc0
JM
6459
6460 /* Looking up TYPE_POINTER_TO and TYPE_REFERENCE_TO yield the same
6461 result. */
8d08fdba
MS
6462 TYPE_POINTER_TO (unknown_type_node) = unknown_type_node;
6463 TYPE_REFERENCE_TO (unknown_type_node) = unknown_type_node;
6464
8926095f 6465 if (flag_vtable_thunks)
700f8a87
MS
6466 {
6467 /* Make sure we get a unique function type, so we can give
6468 its pointer type a name. (This wins for gdb.) */
6469 tree vfunc_type = make_node (FUNCTION_TYPE);
6470 TREE_TYPE (vfunc_type) = integer_type_node;
6471 TYPE_ARG_TYPES (vfunc_type) = NULL_TREE;
6472 layout_type (vfunc_type);
6473
6474 vtable_entry_type = build_pointer_type (vfunc_type);
6475 }
8926095f 6476 else
700f8a87 6477 {
33848bb0 6478 vtable_entry_type = make_aggr_type (RECORD_TYPE);
721c3b42
MM
6479 fields[0] = build_decl (FIELD_DECL, delta_identifier,
6480 delta_type_node);
6481 fields[1] = build_decl (FIELD_DECL, index_identifier,
6482 delta_type_node);
6483 fields[2] = build_decl (FIELD_DECL, pfn_identifier,
6484 ptr_type_node);
700f8a87
MS
6485 finish_builtin_type (vtable_entry_type, VTBL_PTR_TYPE, fields, 2,
6486 double_type_node);
6487
6488 /* Make this part of an invisible union. */
6489 fields[3] = copy_node (fields[2]);
6490 TREE_TYPE (fields[3]) = delta_type_node;
6491 DECL_NAME (fields[3]) = delta2_identifier;
6492 DECL_MODE (fields[3]) = TYPE_MODE (delta_type_node);
6493 DECL_SIZE (fields[3]) = TYPE_SIZE (delta_type_node);
06ceef4e 6494 DECL_SIZE_UNIT (fields[3]) = TYPE_SIZE_UNIT (delta_type_node);
700f8a87
MS
6495 TREE_UNSIGNED (fields[3]) = 0;
6496 TREE_CHAIN (fields[2]) = fields[3];
91063b51
MM
6497 vtable_entry_type = build_qualified_type (vtable_entry_type,
6498 TYPE_QUAL_CONST);
700f8a87 6499 }
0e5921e8 6500 record_builtin_type (RID_MAX, VTBL_PTR_TYPE, vtable_entry_type);
8d08fdba 6501
8d08fdba 6502 vtbl_type_node
52bf7d5d 6503 = build_cplus_array_type (vtable_entry_type, NULL_TREE);
8d08fdba 6504 layout_type (vtbl_type_node);
91063b51 6505 vtbl_type_node = build_qualified_type (vtbl_type_node, TYPE_QUAL_CONST);
0e5921e8 6506 record_builtin_type (RID_MAX, NULL_PTR, vtbl_type_node);
849da744
MM
6507 vtbl_ptr_type_node = build_pointer_type (vtable_entry_type);
6508 layout_type (vtbl_ptr_type_node);
0e5921e8 6509 record_builtin_type (RID_MAX, NULL_PTR, vtbl_ptr_type_node);
8d08fdba 6510
1f84ec23
MM
6511 push_namespace (get_identifier ("__cxxabiv1"));
6512 abi_node = current_namespace;
6513 pop_namespace ();
6633d636 6514
2c73f9f5 6515 global_type_node = make_node (LANG_TYPE);
036407f7 6516 record_unknown_type (global_type_node, "global type");
2c73f9f5 6517
db5ae43f
MS
6518 /* Now, C++. */
6519 current_lang_name = lang_name_cplusplus;
8d08fdba 6520
ced78d8b 6521 {
2c73f9f5
ML
6522 tree bad_alloc_type_node, newtype, deltype;
6523 if (flag_honor_std)
6bcedb4e 6524 push_namespace (std_identifier);
2c73f9f5 6525 bad_alloc_type_node = xref_tag
ca107ded 6526 (class_type_node, get_identifier ("bad_alloc"), 1);
2c73f9f5
ML
6527 if (flag_honor_std)
6528 pop_namespace ();
6529 newtype = build_exception_variant
4cc1d462
NS
6530 (ptr_ftype_sizetype, add_exception_specifier (NULL_TREE, bad_alloc_type_node, -1));
6531 deltype = build_exception_variant (void_ftype_ptr, empty_except_spec);
596ea4e5
AS
6532 push_cp_library_fn (NEW_EXPR, newtype);
6533 push_cp_library_fn (VEC_NEW_EXPR, newtype);
6534 global_delete_fndecl = push_cp_library_fn (DELETE_EXPR, deltype);
6535 push_cp_library_fn (VEC_DELETE_EXPR, deltype);
ced78d8b 6536 }
8d08fdba
MS
6537
6538 abort_fndecl
1f84ec23 6539 = build_library_fn_ptr ("__cxa_pure_virtual", void_ftype);
8d08fdba 6540
8d08fdba
MS
6541 /* Perform other language dependent initializations. */
6542 init_class_processing ();
6543 init_init_processing ();
6544 init_search_processing ();
db48b831 6545 init_rtti_processing ();
8d08fdba 6546
6467930b 6547 if (flag_exceptions)
8d2733ca 6548 init_exception_processing ();
8d08fdba
MS
6549 if (flag_no_inline)
6550 {
6551 flag_inline_functions = 0;
8d08fdba 6552 }
9e9ff709 6553
7fcdf4c2 6554 if (! supports_one_only ())
72b7eeff 6555 flag_weak = 0;
8d08fdba
MS
6556
6557 /* Create the global bindings for __FUNCTION__ and __PRETTY_FUNCTION__. */
63ad61ed
ZW
6558 function_id_node = get_identifier ("__FUNCTION__");
6559 pretty_function_id_node = get_identifier ("__PRETTY_FUNCTION__");
6560 func_id_node = get_identifier ("__func__");
6561
2ce07e2d 6562 make_fname_decl = cp_make_fname_decl;
8d08fdba
MS
6563 declare_function_name ();
6564
6565 /* Prepare to check format strings against argument lists. */
6566 init_function_format_info ();
e9a25f70
JL
6567
6568 /* Show we use EH for cleanups. */
6569 using_eh_for_cleanups ();
62c154ed 6570
e5dc5fb2 6571 valid_lang_attribute = cp_valid_lang_attribute;
d9cf7c82
JM
6572
6573 /* Maintain consistency. Perhaps we should just complain if they
6574 say -fwritable-strings? */
6575 if (flag_writable_strings)
6576 flag_const_strings = 0;
fc6af6e3
RH
6577
6578 /* Add GC roots for all of our global variables. */
6579 ggc_add_tree_root (c_global_trees, sizeof c_global_trees / sizeof(tree));
6580 ggc_add_tree_root (cp_global_trees, sizeof cp_global_trees / sizeof(tree));
fc6af6e3
RH
6581 ggc_add_tree_root (&integer_three_node, 1);
6582 ggc_add_tree_root (&integer_two_node, 1);
fc6af6e3 6583 ggc_add_tree_root (&signed_size_zero_node, 1);
fc6af6e3
RH
6584 ggc_add_tree_root (&size_one_node, 1);
6585 ggc_add_tree_root (&size_zero_node, 1);
fc6af6e3
RH
6586 ggc_add_root (&global_binding_level, 1, sizeof global_binding_level,
6587 mark_binding_level);
9cd64686 6588 ggc_add_root (&scope_chain, 1, sizeof scope_chain, &mark_saved_scope);
fc6af6e3
RH
6589 ggc_add_tree_root (&static_ctors, 1);
6590 ggc_add_tree_root (&static_dtors, 1);
9cd64686 6591 ggc_add_tree_root (&lastiddecl, 1);
fc6af6e3 6592
fc6af6e3
RH
6593 ggc_add_tree_root (&last_function_parm_tags, 1);
6594 ggc_add_tree_root (&current_function_return_value, 1);
fc6af6e3 6595 ggc_add_tree_root (&current_function_parm_tags, 1);
9cd64686 6596 ggc_add_tree_root (&last_function_parms, 1);
fc6af6e3 6597 ggc_add_tree_root (&error_mark_list, 1);
9cd64686 6598
fc6af6e3 6599 ggc_add_tree_root (&global_namespace, 1);
fc6af6e3
RH
6600 ggc_add_tree_root (&global_type_node, 1);
6601 ggc_add_tree_root (&anonymous_namespace_name, 1);
9cd64686
MM
6602
6603 ggc_add_tree_root (&got_object, 1);
6604 ggc_add_tree_root (&got_scope, 1);
6605
6606 ggc_add_tree_root (&current_lang_name, 1);
6607 ggc_add_tree_root (&static_aggregates, 1);
4890c2f4 6608 ggc_add_tree_root (&free_bindings, 1);
62c154ed
JM
6609}
6610
2ce07e2d
NS
6611/* Create the VAR_DECL for __FUNCTION__ etc. ID is the name to give the
6612 decl, NAME is the initialization string and TYPE_DEP indicates whether
6613 NAME depended on the type of the function. We make use of that to detect
6614 __PRETTY_FUNCTION__ inside a template fn. Because we build a tree for
6615 the function before emitting any of it, we don't need to treat the
6616 VAR_DECL specially. We can decide whether to emit it later, if it was
6617 used. */
6618
6619static tree
6620cp_make_fname_decl (id, name, type_dep)
6621 tree id;
6622 const char *name;
6623 int type_dep;
6624{
6625 tree decl, type, init;
6626 size_t length = strlen (name);
6627 tree domain = NULL_TREE;
5362b086 6628
2ce07e2d
NS
6629 if (!processing_template_decl)
6630 type_dep = 0;
6631 if (!type_dep)
4890c2f4 6632 domain = build_index_type (size_int (length));
2ce07e2d
NS
6633
6634 type = build_cplus_array_type
6635 (build_qualified_type (char_type_node, TYPE_QUAL_CONST),
6636 domain);
6637
6dfbb909 6638 decl = build_decl (VAR_DECL, id, type);
2ce07e2d
NS
6639 TREE_STATIC (decl) = 1;
6640 TREE_READONLY (decl) = 1;
6641 DECL_SOURCE_LINE (decl) = 0;
6642 DECL_ARTIFICIAL (decl) = 1;
6643 DECL_IN_SYSTEM_HEADER (decl) = 1;
a3eab619 6644 DECL_IGNORED_P (decl) = 1;
2ce07e2d
NS
6645 pushdecl (decl);
6646 if (processing_template_decl)
6647 decl = push_template_decl (decl);
6648 if (type_dep)
6649 {
6650 init = build (FUNCTION_NAME, type);
6651 DECL_PRETTY_FUNCTION_P (decl) = 1;
6652 }
6653 else
6654 {
6655 init = build_string (length + 1, name);
6656 TREE_TYPE (init) = type;
6657 }
6658 DECL_INITIAL (decl) = init;
6659 cp_finish_decl (decl, init, NULL_TREE, LOOKUP_ONLYCONVERTING);
5362b086 6660
2ce07e2d
NS
6661 /* We will have to make sure we only emit this, if it is actually used. */
6662 return decl;
6663}
6664
0c11ada6
JM
6665/* Entry point for the benefit of c_common_nodes_and_builtins.
6666
6667 Make a definition for a builtin function named NAME and whose data type
8d08fdba 6668 is TYPE. TYPE should be a function type with argument types.
8d08fdba 6669
0c11ada6
JM
6670 CLASS and CODE tell later passes how to compile calls to this function.
6671 See tree.h for possible values.
6672
6673 If LIBNAME is nonzero, use that for DECL_ASSEMBLER_NAME,
8d08fdba
MS
6674 the name to be called if we can't opencode the function. */
6675
6676tree
0c11ada6 6677builtin_function (name, type, code, class, libname)
d8e178a0 6678 const char *name;
8d08fdba 6679 tree type;
0c11ada6
JM
6680 int code;
6681 enum built_in_class class;
6682 const char *libname;
8d08fdba 6683{
596ea4e5 6684 tree decl = build_library_fn_1 (get_identifier (name), ERROR_MARK, type);
0c11ada6
JM
6685 DECL_BUILT_IN_CLASS (decl) = class;
6686 DECL_FUNCTION_CODE (decl) = code;
12a22e76 6687
2c73f9f5 6688 my_friendly_assert (DECL_CONTEXT (decl) == NULL_TREE, 392);
2c73f9f5 6689
6bcedb4e
MM
6690 /* All builtins that don't begin with an `_' should go in the `std'
6691 namespace. */
5362b086 6692 if (flag_honor_std && name[0] != '_')
6bcedb4e
MM
6693 {
6694 push_namespace (std_identifier);
6695 DECL_CONTEXT (decl) = std_node;
6696 }
6697 pushdecl (decl);
6698 if (flag_honor_std && name[0] != '_')
6699 pop_namespace ();
6700
8d08fdba
MS
6701 /* Since `pushdecl' relies on DECL_ASSEMBLER_NAME instead of DECL_NAME,
6702 we cannot change DECL_ASSEMBLER_NAME until we have installed this
6703 function in the namespace. */
0c11ada6 6704 if (libname)
92643fea
MM
6705 SET_DECL_ASSEMBLER_NAME (decl, get_identifier (libname));
6706 make_decl_rtl (decl, NULL);
935d1834
ZW
6707
6708 /* Warn if a function in the namespace for users
6709 is used without an occasion to consider it declared. */
6710 if (name[0] != '_' || name[1] != '_')
6711 DECL_ANTICIPATED (decl) = 1;
6712
8d08fdba
MS
6713 return decl;
6714}
7f4edbcb 6715
0c11ada6
JM
6716/* Generate a FUNCTION_DECL with the typical flags for a runtime library
6717 function. Not called directly. */
6718
6719static tree
596ea4e5 6720build_library_fn_1 (name, operator_code, type)
0c11ada6 6721 tree name;
596ea4e5 6722 enum tree_code operator_code;
0c11ada6
JM
6723 tree type;
6724{
6725 tree fn = build_lang_decl (FUNCTION_DECL, name, type);
6726 DECL_EXTERNAL (fn) = 1;
6727 TREE_PUBLIC (fn) = 1;
6728 DECL_ARTIFICIAL (fn) = 1;
6729 TREE_NOTHROW (fn) = 1;
596ea4e5 6730 SET_OVERLOADED_OPERATOR_CODE (fn, operator_code);
92643fea 6731 DECL_LANGUAGE (fn) = lang_c;
0c11ada6
JM
6732 return fn;
6733}
c2a37c55 6734
0c11ada6
JM
6735/* Returns the _DECL for a library function with C linkage.
6736 We assume that such functions never throw; if this is incorrect,
6737 callers should unset TREE_NOTHROW. */
c2a37c55 6738
7f4edbcb 6739tree
0c11ada6
JM
6740build_library_fn (name, type)
6741 tree name;
6742 tree type;
6743{
19e7881c 6744 return build_library_fn_1 (name, ERROR_MARK, type);
0c11ada6
JM
6745}
6746
6747/* Returns the _DECL for a library function with C++ linkage. */
6748
596ea4e5
AS
6749static tree
6750build_cp_library_fn (name, operator_code, type)
0c11ada6 6751 tree name;
596ea4e5 6752 enum tree_code operator_code;
0c11ada6
JM
6753 tree type;
6754{
596ea4e5 6755 tree fn = build_library_fn_1 (name, operator_code, type);
0c11ada6 6756 TREE_NOTHROW (fn) = TYPE_NOTHROW_P (type);
6bbf1598 6757 DECL_CONTEXT (fn) = FROB_CONTEXT (current_namespace);
92643fea 6758 DECL_LANGUAGE (fn) = lang_cplusplus;
0c11ada6 6759 set_mangled_name_for_decl (fn);
0c11ada6
JM
6760 return fn;
6761}
6762
6763/* Like build_library_fn, but takes a C string instead of an
6764 IDENTIFIER_NODE. */
6765
6766tree
6767build_library_fn_ptr (name, type)
7f4edbcb
BS
6768 const char *name;
6769 tree type;
7f4edbcb 6770{
0c11ada6
JM
6771 return build_library_fn (get_identifier (name), type);
6772}
6773
6774/* Like build_cp_library_fn, but takes a C string instead of an
6775 IDENTIFIER_NODE. */
6776
6777tree
6778build_cp_library_fn_ptr (name, type)
6779 const char *name;
6780 tree type;
6781{
596ea4e5 6782 return build_cp_library_fn (get_identifier (name), ERROR_MARK, type);
0c11ada6
JM
6783}
6784
6785/* Like build_library_fn, but also pushes the function so that we will
6786 be able to find it via IDENTIFIER_GLOBAL_VALUE. */
6787
6788tree
6789push_library_fn (name, type)
6790 tree name, type;
6791{
6792 tree fn = build_library_fn (name, type);
6793 pushdecl_top_level (fn);
6794 return fn;
6795}
6796
6797/* Like build_cp_library_fn, but also pushes the function so that it
6798 will be found by normal lookup. */
6799
596ea4e5
AS
6800static tree
6801push_cp_library_fn (operator_code, type)
6802 enum tree_code operator_code;
0c11ada6
JM
6803 tree type;
6804{
5362b086 6805 tree fn = build_cp_library_fn (ansi_opname (operator_code),
596ea4e5
AS
6806 operator_code,
6807 type);
0c11ada6
JM
6808 pushdecl (fn);
6809 return fn;
6810}
6811
6812/* Like push_library_fn, but takes a TREE_LIST of parm types rather than
6813 a FUNCTION_TYPE. */
6814
6815tree
6816push_void_library_fn (name, parmtypes)
6817 tree name, parmtypes;
6818{
6819 tree type = build_function_type (void_type_node, parmtypes);
6820 return push_library_fn (name, type);
6821}
6822
cf74fb86 6823/* Like push_library_fn, but also note that this function throws
0c11ada6
JM
6824 and does not return. Used for __throw_foo and the like. */
6825
6826tree
cf74fb86
JM
6827push_throw_library_fn (name, type)
6828 tree name, type;
0c11ada6 6829{
cf74fb86 6830 tree fn = push_library_fn (name, type);
0c11ada6
JM
6831 TREE_THIS_VOLATILE (fn) = 1;
6832 TREE_NOTHROW (fn) = 0;
6833 return fn;
7f4edbcb 6834}
8d08fdba 6835\f
61a127b3
MM
6836/* When we call finish_struct for an anonymous union, we create
6837 default copy constructors and such. But, an anonymous union
6838 shouldn't have such things; this function undoes the damage to the
6839 anonymous union type T.
6840
6841 (The reason that we create the synthesized methods is that we don't
6842 distinguish `union { int i; }' from `typedef union { int i; } U'.
6843 The first is an anonymous union; the second is just an ordinary
6844 union type.) */
6845
6846void
6bdb8141 6847fixup_anonymous_aggr (t)
61a127b3
MM
6848 tree t;
6849{
6850 tree *q;
6851
6852 /* Wipe out memory of synthesized methods */
6853 TYPE_HAS_CONSTRUCTOR (t) = 0;
6854 TYPE_HAS_DEFAULT_CONSTRUCTOR (t) = 0;
6855 TYPE_HAS_INIT_REF (t) = 0;
6856 TYPE_HAS_CONST_INIT_REF (t) = 0;
6857 TYPE_HAS_ASSIGN_REF (t) = 0;
61a127b3
MM
6858 TYPE_HAS_CONST_ASSIGN_REF (t) = 0;
6859
6860 /* Splice the implicitly generated functions out of the TYPE_METHODS
6861 list. */
6862 q = &TYPE_METHODS (t);
6863 while (*q)
6864 {
6865 if (DECL_ARTIFICIAL (*q))
6866 *q = TREE_CHAIN (*q);
6867 else
6868 q = &TREE_CHAIN (*q);
6869 }
6870
cab1f180 6871 /* ISO C++ 9.5.3. Anonymous unions may not have function members. */
61a127b3 6872 if (TYPE_METHODS (t))
1f0d71c5 6873 cp_error_at ("an anonymous union cannot have function members", t);
a1c2b86d
JJ
6874
6875 /* Anonymous aggregates cannot have fields with ctors, dtors or complex
6876 assignment operators (because they cannot have these methods themselves).
6877 For anonymous unions this is already checked because they are not allowed
6878 in any union, otherwise we have to check it. */
6879 if (TREE_CODE (t) != UNION_TYPE)
6880 {
6881 tree field, type;
6882
6883 for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
6884 if (TREE_CODE (field) == FIELD_DECL)
6885 {
6886 type = TREE_TYPE (field);
6887 if (CLASS_TYPE_P (type))
6888 {
6889 if (TYPE_NEEDS_CONSTRUCTING (type))
6890 cp_error_at ("member %#D' with constructor not allowed in anonymous aggregate",
6891 field);
6892 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
6893 cp_error_at ("member %#D' with destructor not allowed in anonymous aggregate",
6894 field);
6895 if (TYPE_HAS_COMPLEX_ASSIGN_REF (type))
6896 cp_error_at ("member %#D' with copy assignment operator not allowed in anonymous aggregate",
6897 field);
6898 }
6899 }
6900 }
61a127b3
MM
6901}
6902
72a93143
JM
6903/* Make sure that a declaration with no declarator is well-formed, i.e.
6904 just defines a tagged type or anonymous union.
8d08fdba 6905
72a93143 6906 Returns the type defined, if any. */
8d08fdba 6907
72a93143
JM
6908tree
6909check_tag_decl (declspecs)
8d08fdba
MS
6910 tree declspecs;
6911{
72a93143 6912 int found_type = 0;
2bdb0643 6913 int saw_friend = 0;
e8186ecf 6914 int saw_typedef = 0;
2986ae00 6915 tree ob_modifier = NULL_TREE;
8d08fdba 6916 register tree link;
8d08fdba
MS
6917 register tree t = NULL_TREE;
6918
6919 for (link = declspecs; link; link = TREE_CHAIN (link))
6920 {
6921 register tree value = TREE_VALUE (link);
6922
2bdb0643 6923 if (TYPE_P (value)
bd0d5d4a 6924 || TREE_CODE (value) == TYPE_DECL
2bdb0643
JM
6925 || (TREE_CODE (value) == IDENTIFIER_NODE
6926 && IDENTIFIER_GLOBAL_VALUE (value)
bd0d5d4a 6927 && TREE_CODE (IDENTIFIER_GLOBAL_VALUE (value)) == TYPE_DECL))
8d08fdba 6928 {
72a93143 6929 ++found_type;
5566b478 6930
bd0d5d4a
JM
6931 if (found_type == 2 && TREE_CODE (value) == IDENTIFIER_NODE)
6932 {
6933 if (! in_system_header)
6934 cp_pedwarn ("redeclaration of C++ built-in type `%T'", value);
6935 return NULL_TREE;
6936 }
6937
6938 if (TYPE_P (value)
6939 && ((TREE_CODE (value) != TYPENAME_TYPE && IS_AGGR_TYPE (value))
6940 || TREE_CODE (value) == ENUMERAL_TYPE))
72a93143
JM
6941 {
6942 my_friendly_assert (TYPE_MAIN_DECL (value) != NULL_TREE, 261);
6943 t = value;
6944 }
8d08fdba 6945 }
e8186ecf
GDR
6946 else if (value == ridpointers[(int) RID_TYPEDEF])
6947 saw_typedef = 1;
83f660b7
JM
6948 else if (value == ridpointers[(int) RID_FRIEND])
6949 {
83f660b7
JM
6950 if (current_class_type == NULL_TREE
6951 || current_scope () != current_class_type)
6952 ob_modifier = value;
2bdb0643
JM
6953 else
6954 saw_friend = 1;
83f660b7 6955 }
8d08fdba 6956 else if (value == ridpointers[(int) RID_STATIC]
2986ae00
MS
6957 || value == ridpointers[(int) RID_EXTERN]
6958 || value == ridpointers[(int) RID_AUTO]
28cbf42c
MS
6959 || value == ridpointers[(int) RID_REGISTER]
6960 || value == ridpointers[(int) RID_INLINE]
6961 || value == ridpointers[(int) RID_VIRTUAL]
72a93143
JM
6962 || value == ridpointers[(int) RID_CONST]
6963 || value == ridpointers[(int) RID_VOLATILE]
28cbf42c 6964 || value == ridpointers[(int) RID_EXPLICIT])
2986ae00 6965 ob_modifier = value;
8d08fdba
MS
6966 }
6967
72a93143
JM
6968 if (found_type > 1)
6969 error ("multiple types in one declaration");
7e2067ca 6970
2bdb0643 6971 if (t == NULL_TREE && ! saw_friend)
7e2067ca 6972 pedwarn ("declaration does not declare anything");
0dd3962d
JM
6973
6974 /* Check for an anonymous union. We're careful
6975 accessing TYPE_IDENTIFIER because some built-in types, like
6976 pointer-to-member types, do not have TYPE_NAME. */
6bdb8141 6977 else if (t && IS_AGGR_TYPE_CODE (TREE_CODE (t))
0dd3962d
JM
6978 && TYPE_NAME (t)
6979 && ANON_AGGRNAME_P (TYPE_IDENTIFIER (t)))
6980 {
e8186ecf
GDR
6981 /* 7/3 In a simple-declaration, the optional init-declarator-list
6982 can be omitted only when declaring a class (clause 9) or
6983 enumeration (7.2), that is, when the decl-specifier-seq contains
6984 either a class-specifier, an elaborated-type-specifier with
6985 a class-key (9.1), or an enum-specifier. In these cases and
6986 whenever a class-specifier or enum-specifier is present in the
6987 decl-specifier-seq, the identifiers in these specifiers are among
6988 the names being declared by the declaration (as class-name,
6989 enum-names, or enumerators, depending on the syntax). In such
6990 cases, and except for the declaration of an unnamed bit-field (9.6),
6991 the decl-specifier-seq shall introduce one or more names into the
6992 program, or shall redeclare a name introduced by a previous
6993 declaration. [Example:
6994 enum { }; // ill-formed
6995 typedef class { }; // ill-formed
6996 --end example] */
6997 if (saw_typedef)
6998 {
6999 error ("Missing type-name in typedef-declaration.");
7000 return NULL_TREE;
7001 }
0dd3962d 7002 /* Anonymous unions are objects, so they can have specifiers. */;
6bdb8141
JM
7003 SET_ANON_AGGR_TYPE_P (t);
7004
7005 if (TREE_CODE (t) != UNION_TYPE && pedantic && ! in_system_header)
7006 pedwarn ("ISO C++ prohibits anonymous structs");
0dd3962d
JM
7007 }
7008
83f660b7 7009 else if (ob_modifier)
8d08fdba 7010 {
83f660b7
JM
7011 if (ob_modifier == ridpointers[(int) RID_INLINE]
7012 || ob_modifier == ridpointers[(int) RID_VIRTUAL])
7013 cp_error ("`%D' can only be specified for functions", ob_modifier);
7014 else if (ob_modifier == ridpointers[(int) RID_FRIEND])
7015 cp_error ("`%D' can only be specified inside a class", ob_modifier);
7016 else if (ob_modifier == ridpointers[(int) RID_EXPLICIT])
7017 cp_error ("`%D' can only be specified for constructors",
7018 ob_modifier);
7019 else
7020 cp_error ("`%D' can only be specified for objects and functions",
7021 ob_modifier);
72a93143 7022 }
8d08fdba 7023
72a93143
JM
7024 return t;
7025}
7026
7027/* Called when a declaration is seen that contains no names to declare.
7028 If its type is a reference to a structure, union or enum inherited
7029 from a containing scope, shadow that tag name for the current scope
7030 with a forward reference.
7031 If its type defines a new named structure or union
7032 or defines an enum, it is valid but we need not do anything here.
7033 Otherwise, it is an error.
7034
7035 C++: may have to grok the declspecs to learn about static,
7036 complain for anonymous unions. */
7037
7038void
7039shadow_tag (declspecs)
7040 tree declspecs;
7041{
7042 tree t = check_tag_decl (declspecs);
7043
7044 if (t)
7045 maybe_process_partial_specialization (t);
7046
7047 /* This is where the variables in an anonymous union are
7048 declared. An anonymous union declaration looks like:
7049 union { ... } ;
7050 because there is no declarator after the union, the parser
7051 sends that declaration here. */
6bdb8141 7052 if (t && ANON_AGGR_TYPE_P (t))
72a93143 7053 {
6bdb8141 7054 fixup_anonymous_aggr (t);
72a93143
JM
7055
7056 if (TYPE_FIELDS (t))
7057 {
7058 tree decl = grokdeclarator (NULL_TREE, declspecs, NORMAL, 0,
7059 NULL_TREE);
7060 finish_anon_union (decl);
7061 }
8d08fdba
MS
7062 }
7063}
7064\f
7065/* Decode a "typename", such as "int **", returning a ..._TYPE node. */
7066
7067tree
7068groktypename (typename)
7069 tree typename;
7070{
7071 if (TREE_CODE (typename) != TREE_LIST)
7072 return typename;
7073 return grokdeclarator (TREE_VALUE (typename),
7074 TREE_PURPOSE (typename),
c11b6f21 7075 TYPENAME, 0, NULL_TREE);
8d08fdba
MS
7076}
7077
7078/* Decode a declarator in an ordinary declaration or data definition.
7079 This is called as soon as the type information and variable name
7080 have been parsed, before parsing the initializer if any.
7081 Here we create the ..._DECL node, fill in its type,
7082 and put it on the list of decls for the current context.
7083 The ..._DECL node is returned as the value.
7084
7085 Exception: for arrays where the length is not specified,
82580166 7086 the type is left null, to be filled in by `cp_finish_decl'.
8d08fdba
MS
7087
7088 Function definitions do not come here; they go to start_function
7089 instead. However, external and forward declarations of functions
7090 do go through here. Structure field declarations are done by
7091 grokfield and not through here. */
7092
8d08fdba 7093tree
a1774733 7094start_decl (declarator, declspecs, initialized, attributes, prefix_attributes)
8d08fdba
MS
7095 tree declarator, declspecs;
7096 int initialized;
a1774733 7097 tree attributes, prefix_attributes;
8d08fdba
MS
7098{
7099 register tree decl;
7100 register tree type, tem;
7101 tree context;
7102 extern int have_extern_spec;
7103 extern int used_extern_spec;
b17e2870 7104 tree attrlist;
8d08fdba 7105
5566b478
MS
7106#if 0
7107 /* See code below that used this. */
8d08fdba 7108 int init_written = initialized;
5566b478 7109#endif
8d08fdba 7110
e92cc029 7111 /* This should only be done once on the top most decl. */
8d08fdba
MS
7112 if (have_extern_spec && !used_extern_spec)
7113 {
1f8f4a0b
MM
7114 declspecs = tree_cons (NULL_TREE, get_identifier ("extern"),
7115 declspecs);
8d08fdba
MS
7116 used_extern_spec = 1;
7117 }
7118
b17e2870 7119 if (attributes || prefix_attributes)
051e6fd7 7120 attrlist = build_tree_list (attributes, prefix_attributes);
b17e2870
JM
7121 else
7122 attrlist = NULL_TREE;
7123
c11b6f21 7124 decl = grokdeclarator (declarator, declspecs, NORMAL, initialized,
b17e2870 7125 attrlist);
68642fb6 7126
a1774733 7127 if (decl == NULL_TREE || TREE_CODE (decl) == VOID_TYPE)
8d08fdba
MS
7128 return NULL_TREE;
7129
7130 type = TREE_TYPE (decl);
7131
44689c12
ML
7132 if (type == error_mark_node)
7133 return NULL_TREE;
7134
4f1c5b7d 7135 context = DECL_CONTEXT (decl);
8d08fdba 7136
9a68c51f
JM
7137 if (initialized && context && TREE_CODE (context) == NAMESPACE_DECL
7138 && context != current_namespace && TREE_CODE (decl) == VAR_DECL)
7139 {
7140 /* When parsing the initializer, lookup should use the object's
7141 namespace. */
7142 push_decl_namespace (context);
7143 }
7144
2c73f9f5
ML
7145 /* We are only interested in class contexts, later. */
7146 if (context && TREE_CODE (context) == NAMESPACE_DECL)
7147 context = NULL_TREE;
7148
8d08fdba
MS
7149 if (initialized)
7150 /* Is it valid for this decl to have an initializer at all?
7151 If not, set INITIALIZED to zero, which will indirectly
82580166 7152 tell `cp_finish_decl' to ignore the initializer once it is parsed. */
8d08fdba
MS
7153 switch (TREE_CODE (decl))
7154 {
7155 case TYPE_DECL:
7156 /* typedef foo = bar means give foo the same type as bar.
82580166 7157 We haven't parsed bar yet, so `cp_finish_decl' will fix that up.
8d08fdba
MS
7158 Any other case of an initialization in a TYPE_DECL is an error. */
7159 if (pedantic || list_length (declspecs) > 1)
7160 {
8251199e 7161 cp_error ("typedef `%D' is initialized", decl);
8d08fdba
MS
7162 initialized = 0;
7163 }
7164 break;
7165
7166 case FUNCTION_DECL:
8251199e 7167 cp_error ("function `%#D' is initialized like a variable", decl);
8d08fdba
MS
7168 initialized = 0;
7169 break;
7170
7171 default:
3e41d13b 7172 break;
8d08fdba
MS
7173 }
7174
8d08fdba
MS
7175 if (initialized)
7176 {
a9aedbc2 7177 if (! toplevel_bindings_p ()
8d08fdba 7178 && DECL_EXTERNAL (decl))
8251199e 7179 cp_warning ("declaration of `%#D' has `extern' and is initialized",
8d08fdba
MS
7180 decl);
7181 DECL_EXTERNAL (decl) = 0;
5566b478 7182 if (toplevel_bindings_p ())
8d08fdba
MS
7183 TREE_STATIC (decl) = 1;
7184
7185 /* Tell `pushdecl' this is an initialized decl
7186 even though we don't yet have the initializer expression.
82580166 7187 Also tell `cp_finish_decl' it may store the real initializer. */
8d08fdba
MS
7188 DECL_INITIAL (decl) = error_mark_node;
7189 }
7190
fa20888b
MK
7191#ifdef SET_DEFAULT_DECL_ATTRIBUTES
7192 SET_DEFAULT_DECL_ATTRIBUTES (decl, attributes);
7193#endif
68642fb6 7194
fa20888b
MK
7195 /* Set attributes here so if duplicate decl, will have proper attributes. */
7196 cplus_decl_attributes (decl, attributes, prefix_attributes);
7197
d0f062fb 7198 if (context && COMPLETE_TYPE_P (complete_type (context)))
5b605f68 7199 {
6b400b21 7200 push_nested_class (context, 2);
e97e5263 7201
5b605f68
MS
7202 if (TREE_CODE (decl) == VAR_DECL)
7203 {
7204 tree field = lookup_field (context, DECL_NAME (decl), 0, 0);
7205 if (field == NULL_TREE || TREE_CODE (field) != VAR_DECL)
8251199e 7206 cp_error ("`%#D' is not a static member of `%#T'", decl, context);
e349ee73
MS
7207 else
7208 {
7209 if (DECL_CONTEXT (field) != context)
f2d773a2 7210 {
cb9a3ff8 7211 cp_pedwarn ("ISO C++ does not permit `%T::%D' to be defined as `%T::%D'",
f2d773a2
JM
7212 DECL_CONTEXT (field), DECL_NAME (decl),
7213 context, DECL_NAME (decl));
7214 DECL_CONTEXT (decl) = DECL_CONTEXT (field);
7215 }
75650646
MM
7216 /* Static data member are tricky; an in-class initialization
7217 still doesn't provide a definition, so the in-class
7218 declaration will have DECL_EXTERNAL set, but will have an
7219 initialization. Thus, duplicate_decls won't warn
7220 about this situation, and so we check here. */
7221 if (DECL_INITIAL (decl) && DECL_INITIAL (field))
8251199e 7222 cp_error ("duplicate initialization of %D", decl);
e349ee73
MS
7223 if (duplicate_decls (decl, field))
7224 decl = field;
7225 }
5b605f68 7226 }
f30432d7
MS
7227 else
7228 {
5566b478 7229 tree field = check_classfn (context, decl);
f30432d7
MS
7230 if (field && duplicate_decls (decl, field))
7231 decl = field;
7232 }
7233
7234 /* cp_finish_decl sets DECL_EXTERNAL if DECL_IN_AGGR_P is set. */
dff3e828 7235 DECL_IN_AGGR_P (decl) = 0;
68642fb6 7236 if ((DECL_LANG_SPECIFIC (decl) && DECL_USE_TEMPLATE (decl))
1f6e1acc 7237 || CLASSTYPE_TEMPLATE_INSTANTIATION (context))
84e6233f
JM
7238 {
7239 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
7240 /* [temp.expl.spec] An explicit specialization of a static data
7241 member of a template is a definition if the declaration
7242 includes an initializer; otherwise, it is a declaration.
7243
7244 We check for processing_specialization so this only applies
7245 to the new specialization syntax. */
7246 if (DECL_INITIAL (decl) == NULL_TREE && processing_specialization)
7247 DECL_EXTERNAL (decl) = 1;
7248 }
f30432d7 7249
b7698cf0 7250 if (DECL_EXTERNAL (decl) && ! DECL_TEMPLATE_SPECIALIZATION (decl))
8251199e 7251 cp_pedwarn ("declaration of `%#D' outside of class is not definition",
f30432d7 7252 decl);
5b605f68
MS
7253 }
7254
9188c363
MM
7255 /* Enter this declaration into the symbol table. */
7256 tem = maybe_push_decl (decl);
2ee887f2 7257
5156628f 7258 if (processing_template_decl)
cd9f6678 7259 tem = push_template_decl (tem);
5566b478 7260
2ee887f2 7261#if ! defined (ASM_OUTPUT_BSS) && ! defined (ASM_OUTPUT_ALIGNED_BSS)
a3203465 7262 /* Tell the back-end to use or not use .common as appropriate. If we say
a50f0918
MS
7263 -fconserve-space, we want this to save .data space, at the expense of
7264 wrong semantics. If we say -fno-conserve-space, we want this to
7265 produce errors about redefs; to do this we force variables into the
7266 data segment. */
a3203465 7267 DECL_COMMON (tem) = flag_conserve_space || ! TREE_PUBLIC (tem);
2ee887f2 7268#endif
68642fb6 7269
5156628f 7270 if (! processing_template_decl)
5566b478 7271 start_decl_1 (tem);
8d08fdba 7272
8d08fdba
MS
7273 return tem;
7274}
7275
5566b478
MS
7276void
7277start_decl_1 (decl)
7278 tree decl;
8d08fdba 7279{
5566b478
MS
7280 tree type = TREE_TYPE (decl);
7281 int initialized = (DECL_INITIAL (decl) != NULL_TREE);
8d08fdba 7282
44689c12
ML
7283 if (type == error_mark_node)
7284 return;
7285
bd0d5d4a 7286 maybe_push_cleanup_level (type);
5566b478
MS
7287
7288 if (initialized)
7289 /* Is it valid for this decl to have an initializer at all?
7290 If not, set INITIALIZED to zero, which will indirectly
7291 tell `cp_finish_decl' to ignore the initializer once it is parsed. */
8d08fdba 7292 {
5566b478
MS
7293 /* Don't allow initializations for incomplete types except for
7294 arrays which might be completed by the initialization. */
d0f062fb 7295 if (COMPLETE_TYPE_P (complete_type (type)))
5566b478
MS
7296 ; /* A complete type is ok. */
7297 else if (TREE_CODE (type) != ARRAY_TYPE)
8d08fdba 7298 {
8251199e 7299 cp_error ("variable `%#D' has initializer but incomplete type",
5566b478
MS
7300 decl);
7301 initialized = 0;
25eb19ff 7302 type = TREE_TYPE (decl) = error_mark_node;
5566b478 7303 }
d0f062fb 7304 else if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (type))))
5566b478
MS
7305 {
7306 if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
8251199e 7307 cp_error ("elements of array `%#D' have incomplete type", decl);
5566b478
MS
7308 /* else we already gave an error in start_decl. */
7309 initialized = 0;
8d08fdba 7310 }
8d08fdba
MS
7311 }
7312
5566b478
MS
7313 if (!initialized
7314 && TREE_CODE (decl) != TYPE_DECL
7315 && TREE_CODE (decl) != TEMPLATE_DECL
07c88314 7316 && type != error_mark_node
5362b086 7317 && IS_AGGR_TYPE (type)
07c88314 7318 && ! DECL_EXTERNAL (decl))
8d08fdba 7319 {
5156628f 7320 if ((! processing_template_decl || ! uses_template_parms (type))
d0f062fb 7321 && !COMPLETE_TYPE_P (complete_type (type)))
5566b478 7322 {
8251199e 7323 cp_error ("aggregate `%#D' has incomplete type and cannot be initialized",
5566b478
MS
7324 decl);
7325 /* Change the type so that assemble_variable will give
7326 DECL an rtl we can live with: (mem (const_int 0)). */
25eb19ff 7327 type = TREE_TYPE (decl) = error_mark_node;
5566b478
MS
7328 }
7329 else
7330 {
7331 /* If any base type in the hierarchy of TYPE needs a constructor,
7332 then we set initialized to 1. This way any nodes which are
7333 created for the purposes of initializing this aggregate
7334 will live as long as it does. This is necessary for global
7335 aggregates which do not have their initializers processed until
7336 the end of the file. */
7337 initialized = TYPE_NEEDS_CONSTRUCTING (type);
7338 }
7339 }
7340
5566b478
MS
7341 if (! initialized)
7342 DECL_INITIAL (decl) = NULL_TREE;
7343}
7344
7345/* Handle initialization of references.
38e01259 7346 These three arguments are from `cp_finish_decl', and have the
e92cc029
MS
7347 same meaning here that they do there.
7348
7349 Quotes on semantics can be found in ARM 8.4.3. */
7350
8e4ce833 7351static tree
a703fb38 7352grok_reference_init (decl, type, init)
5566b478 7353 tree decl, type, init;
5566b478
MS
7354{
7355 tree tmp;
7356
7357 if (init == NULL_TREE)
7358 {
7359 if ((DECL_LANG_SPECIFIC (decl) == 0
7360 || DECL_IN_AGGR_P (decl) == 0)
7361 && ! DECL_THIS_EXTERN (decl))
ed5511d9 7362 cp_error ("`%D' declared as reference but not initialized", decl);
8e4ce833 7363 return NULL_TREE;
5566b478
MS
7364 }
7365
7366 if (init == error_mark_node)
8e4ce833 7367 return NULL_TREE;
5566b478 7368
ed5511d9 7369 if (TREE_CODE (init) == CONSTRUCTOR)
5566b478 7370 {
cb9a3ff8 7371 cp_error ("ISO C++ forbids use of initializer list to initialize reference `%D'", decl);
8e4ce833 7372 return NULL_TREE;
8d08fdba
MS
7373 }
7374
7375 if (TREE_CODE (init) == TREE_LIST)
7376 init = build_compound_expr (init);
8d08fdba 7377
8ccc31eb
MS
7378 if (TREE_CODE (TREE_TYPE (init)) == REFERENCE_TYPE)
7379 init = convert_from_reference (init);
7380
8d08fdba
MS
7381 if (TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE
7382 && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE)
7383 {
a3203465 7384 /* Note: default conversion is only called in very special cases. */
8d08fdba
MS
7385 init = default_conversion (init);
7386 }
68642fb6 7387
24bef158
MM
7388 /* Convert INIT to the reference type TYPE. This may involve the
7389 creation of a temporary, whose lifetime must be the same as that
7390 of the reference. If so, a DECL_STMT for the temporary will be
7391 added just after the DECL_STMT for DECL. That's why we don't set
7392 DECL_INITIAL for local references (instead assigning to them
7393 explicitly); we need to allow the temporary to be initialized
7394 first. */
a3203465 7395 tmp = convert_to_reference
9a3b49ac 7396 (type, init, CONV_IMPLICIT,
ce7715bd
JM
7397 LOOKUP_ONLYCONVERTING|LOOKUP_SPECULATIVELY|LOOKUP_NORMAL|DIRECT_BIND,
7398 decl);
8d08fdba 7399
a3203465 7400 if (tmp == error_mark_node)
8e4ce833
JJ
7401 return NULL_TREE;
7402 else if (tmp == NULL_TREE)
8d08fdba 7403 {
8251199e 7404 cp_error ("cannot initialize `%T' from `%T'", type, TREE_TYPE (init));
8e4ce833 7405 return NULL_TREE;
8d08fdba 7406 }
8d08fdba 7407
8e4ce833
JJ
7408 if (TREE_STATIC (decl) && !TREE_CONSTANT (tmp))
7409 return tmp;
7410
7411 if (building_stmt_tree ())
8d08fdba 7412 {
8e4ce833
JJ
7413 /* Initialize the declaration. */
7414 tmp = build (INIT_EXPR, TREE_TYPE (decl), decl, tmp);
7415 finish_expr_stmt (tmp);
8d08fdba 7416 }
8e4ce833
JJ
7417 else
7418 DECL_INITIAL (decl) = tmp;
7419
7420 return NULL_TREE;
8d08fdba
MS
7421}
7422
6060a796
MS
7423/* Fill in DECL_INITIAL with some magical value to prevent expand_decl from
7424 mucking with forces it does not comprehend (i.e. initialization with a
7425 constructor). If we are at global scope and won't go into COMMON, fill
7426 it in with a dummy CONSTRUCTOR to force the variable into .data;
7427 otherwise we can use error_mark_node. */
7428
28cbf42c
MS
7429static tree
7430obscure_complex_init (decl, init)
7431 tree decl, init;
6060a796 7432{
28cbf42c
MS
7433 if (! flag_no_inline && TREE_STATIC (decl))
7434 {
7435 if (extract_init (decl, init))
7436 return NULL_TREE;
7437 }
7438
2ee887f2 7439#if ! defined (ASM_OUTPUT_BSS) && ! defined (ASM_OUTPUT_ALIGNED_BSS)
a9aedbc2 7440 if (toplevel_bindings_p () && ! DECL_COMMON (decl))
6060a796
MS
7441 DECL_INITIAL (decl) = build (CONSTRUCTOR, TREE_TYPE (decl), NULL_TREE,
7442 NULL_TREE);
7443 else
2ee887f2 7444#endif
6060a796 7445 DECL_INITIAL (decl) = error_mark_node;
28cbf42c
MS
7446
7447 return init;
6060a796
MS
7448}
7449
27778b73
MM
7450/* When parsing `int a[] = {1, 2};' we don't know the size of the
7451 array until we finish parsing the initializer. If that's the
7452 situation we're in, update DECL accordingly. */
7453
7454static void
7455maybe_deduce_size_from_array_init (decl, init)
7456 tree decl;
7457 tree init;
7458{
7459 tree type = TREE_TYPE (decl);
7460
7461 if (TREE_CODE (type) == ARRAY_TYPE
7462 && TYPE_DOMAIN (type) == NULL_TREE
7463 && TREE_CODE (decl) != TYPE_DECL)
7464 {
f2ae0c45
JM
7465 /* do_default is really a C-ism to deal with tentative definitions.
7466 But let's leave it here to ease the eventual merge. */
7467 int do_default = !DECL_EXTERNAL (decl);
27778b73
MM
7468 tree initializer = init ? init : DECL_INITIAL (decl);
7469 int failure = complete_array_type (type, initializer, do_default);
7470
7471 if (failure == 1)
7472 cp_error ("initializer fails to determine size of `%D'", decl);
7473
7474 if (failure == 2)
7475 {
7476 if (do_default)
7477 cp_error ("array size missing in `%D'", decl);
7478 /* If a `static' var's size isn't known, make it extern as
7479 well as static, so it does not get allocated. If it's not
7480 `static', then don't mark it extern; finish_incomplete_decl
7481 will give it a default size and it will get allocated. */
7482 else if (!pedantic && TREE_STATIC (decl) && !TREE_PUBLIC (decl))
7483 DECL_EXTERNAL (decl) = 1;
7484 }
7485
7486 if (pedantic && TYPE_DOMAIN (type) != NULL_TREE
7487 && tree_int_cst_lt (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
7488 integer_zero_node))
7489 cp_error ("zero-size array `%D'", decl);
7490
7491 layout_decl (decl, 0);
7492 }
7493}
7494
7495/* Set DECL_SIZE, DECL_ALIGN, etc. for DECL (a VAR_DECL), and issue
57b52417 7496 any appropriate error messages regarding the layout. */
27778b73 7497
57b52417
MM
7498static void
7499layout_var_decl (decl)
27778b73 7500 tree decl;
27778b73 7501{
57b52417 7502 tree type = TREE_TYPE (decl);
c95cd22e 7503#if 0
57b52417 7504 tree ttype = target_type (type);
c95cd22e 7505#endif
57b52417
MM
7506
7507 /* If we haven't already layed out this declaration, do so now.
7508 Note that we must not call complete type for an external object
7509 because it's type might involve templates that we are not
68642fb6 7510 supposed to isntantiate yet. (And it's perfectly legal to say
57b52417
MM
7511 `extern X x' for some incomplete type `X'.) */
7512 if (!DECL_EXTERNAL (decl))
7513 complete_type (type);
d0f062fb 7514 if (!DECL_SIZE (decl) && COMPLETE_TYPE_P (type))
27778b73
MM
7515 layout_decl (decl, 0);
7516
c82dbd95 7517 if (!DECL_EXTERNAL (decl) && DECL_SIZE (decl) == NULL_TREE)
27778b73
MM
7518 {
7519 /* An automatic variable with an incomplete type: that is an error.
7520 Don't talk about array types here, since we took care of that
7521 message in grokdeclarator. */
7522 cp_error ("storage size of `%D' isn't known", decl);
7523 TREE_TYPE (decl) = error_mark_node;
7524 }
ae673f14
JM
7525#if 0
7526 /* Keep this code around in case we later want to control debug info
7527 based on whether a type is "used". (jason 1999-11-11) */
7528
27778b73
MM
7529 else if (!DECL_EXTERNAL (decl) && IS_AGGR_TYPE (ttype))
7530 /* Let debugger know it should output info for this type. */
7531 note_debug_info_needed (ttype);
7532
7533 if (TREE_STATIC (decl) && DECL_CLASS_SCOPE_P (decl))
7534 note_debug_info_needed (DECL_CONTEXT (decl));
ae673f14 7535#endif
27778b73
MM
7536
7537 if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl))
7538 && DECL_SIZE (decl) != NULL_TREE
7539 && ! TREE_CONSTANT (DECL_SIZE (decl)))
7540 {
7541 if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
7542 constant_expression_warning (DECL_SIZE (decl));
7543 else
7544 cp_error ("storage size of `%D' isn't constant", decl);
7545 }
8e4ce833
JJ
7546
7547 if (TREE_STATIC (decl)
7548 && !DECL_ARTIFICIAL (decl)
7549 && current_function_decl
7550 && DECL_CONTEXT (decl) == current_function_decl)
7551 push_local_name (decl);
27778b73
MM
7552}
7553
27778b73
MM
7554/* If a local static variable is declared in an inline function, or if
7555 we have a weak definition, we must endeavor to create only one
7556 instance of the variable at link-time. */
7557
7558static void
7559maybe_commonize_var (decl)
7560 tree decl;
7561{
7562 /* Static data in a function with comdat linkage also has comdat
7563 linkage. */
7564 if (TREE_STATIC (decl)
7565 /* Don't mess with __FUNCTION__. */
cf74fb86 7566 && ! DECL_ARTIFICIAL (decl)
27778b73
MM
7567 && current_function_decl
7568 && DECL_CONTEXT (decl) == current_function_decl
7569 && (DECL_THIS_INLINE (current_function_decl)
7570 || DECL_TEMPLATE_INSTANTIATION (current_function_decl))
7571 && TREE_PUBLIC (current_function_decl))
7572 {
27778b73
MM
7573 /* If flag_weak, we don't need to mess with this, as we can just
7574 make the function weak, and let it refer to its unique local
7575 copy. This works because we don't allow the function to be
7576 inlined. */
7577 if (! flag_weak)
7578 {
7579 if (DECL_INTERFACE_KNOWN (current_function_decl))
7580 {
7581 TREE_PUBLIC (decl) = 1;
7582 DECL_EXTERNAL (decl) = DECL_EXTERNAL (current_function_decl);
7583 }
7584 else if (DECL_INITIAL (decl) == NULL_TREE
7585 || DECL_INITIAL (decl) == error_mark_node)
7586 {
7587 TREE_PUBLIC (decl) = 1;
7588 DECL_COMMON (decl) = 1;
7589 }
7590 /* else we lose. We can only do this if we can use common,
7591 which we can't if it has been initialized. */
7592
92643fea 7593 if (!TREE_PUBLIC (decl))
27778b73
MM
7594 {
7595 cp_warning_at ("sorry: semantics of inline function static data `%#D' are wrong (you'll wind up with multiple copies)", decl);
7596 cp_warning_at (" you can work around this by removing the initializer", decl);
7597 }
7598 }
8e4ce833
JJ
7599 else
7600 comdat_linkage (decl);
27778b73
MM
7601 }
7602 else if (DECL_LANG_SPECIFIC (decl) && DECL_COMDAT (decl))
7603 /* Set it up again; we might have set DECL_INITIAL since the last
7604 time. */
7605 comdat_linkage (decl);
7606}
7607
91063b51
MM
7608/* Issue an error message if DECL is an uninitialized const variable. */
7609
7610static void
7611check_for_uninitialized_const_var (decl)
7612 tree decl;
7613{
7614 tree type = TREE_TYPE (decl);
7615
7616 /* ``Unless explicitly declared extern, a const object does not have
7617 external linkage and must be initialized. ($8.4; $12.1)'' ARM
7618 7.1.6 */
7619 if (TREE_CODE (decl) == VAR_DECL
7620 && TREE_CODE (type) != REFERENCE_TYPE
7621 && CP_TYPE_CONST_P (type)
7622 && !TYPE_NEEDS_CONSTRUCTING (type)
7623 && !DECL_INITIAL (decl))
7624 cp_error ("uninitialized const `%D'", decl);
7625}
7626
c82dbd95
MM
7627/* Verify INIT (the initializer for DECL), and record the
7628 initialization in DECL_INITIAL, if appropriate. Returns a new
7629 value for INIT. */
27778b73 7630
c82dbd95
MM
7631static tree
7632check_initializer (decl, init)
27778b73 7633 tree decl;
c82dbd95 7634 tree init;
27778b73 7635{
27778b73
MM
7636 tree type;
7637
7638 if (TREE_CODE (decl) == FIELD_DECL)
c82dbd95 7639 return init;
27778b73 7640
fc0e7bf5
MM
7641 type = TREE_TYPE (decl);
7642
27778b73
MM
7643 /* If `start_decl' didn't like having an initialization, ignore it now. */
7644 if (init != NULL_TREE && DECL_INITIAL (decl) == NULL_TREE)
7645 init = NULL_TREE;
27778b73 7646
c82dbd95 7647 /* Check the initializer. */
27778b73
MM
7648 if (init)
7649 {
c82dbd95
MM
7650 /* Things that are going to be initialized need to have complete
7651 type. */
7652 TREE_TYPE (decl) = type = complete_type (TREE_TYPE (decl));
7653
7654 if (type == error_mark_node)
7655 /* We will have already complained. */
7656 init = NULL_TREE;
d0f062fb 7657 else if (COMPLETE_TYPE_P (type) && !TREE_CONSTANT (TYPE_SIZE (type)))
27778b73
MM
7658 {
7659 cp_error ("variable-sized object `%D' may not be initialized", decl);
7660 init = NULL_TREE;
7661 }
c82dbd95 7662 else if (TREE_CODE (type) == ARRAY_TYPE
d0f062fb 7663 && !COMPLETE_TYPE_P (TREE_TYPE (type)))
27778b73
MM
7664 {
7665 cp_error ("elements of array `%#D' have incomplete type", decl);
7666 init = NULL_TREE;
7667 }
d0f062fb 7668 else if (!COMPLETE_TYPE_P (type))
c82dbd95
MM
7669 {
7670 cp_error ("`%D' has incomplete type", decl);
7671 TREE_TYPE (decl) = error_mark_node;
7672 init = NULL_TREE;
7673 }
27778b73
MM
7674 }
7675
7676 if (TREE_CODE (decl) == CONST_DECL)
7677 {
7678 my_friendly_assert (TREE_CODE (decl) != REFERENCE_TYPE, 148);
7679
7680 DECL_INITIAL (decl) = init;
7681
27778b73
MM
7682 my_friendly_assert (init != NULL_TREE, 149);
7683 init = NULL_TREE;
7684 }
c82dbd95
MM
7685 else if (!DECL_EXTERNAL (decl) && TREE_CODE (type) == REFERENCE_TYPE)
7686 {
8e4ce833
JJ
7687 init = grok_reference_init (decl, type, init);
7688 if (init)
7689 init = obscure_complex_init (decl, init);
c82dbd95 7690 }
27778b73
MM
7691 else if (init)
7692 {
7693 if (TYPE_HAS_CONSTRUCTOR (type) || TYPE_NEEDS_CONSTRUCTING (type))
7694 {
7695 if (TREE_CODE (type) == ARRAY_TYPE)
7696 init = digest_init (type, init, (tree *) 0);
7697 else if (TREE_CODE (init) == CONSTRUCTOR
7698 && TREE_HAS_CONSTRUCTOR (init))
7699 {
7700 if (TYPE_NON_AGGREGATE_CLASS (type))
7701 {
7702 cp_error ("`%D' must be initialized by constructor, not by `{...}'",
7703 decl);
7704 init = error_mark_node;
7705 }
7706 else
7707 goto dont_use_constructor;
7708 }
7709 }
7710 else
7711 {
7712 dont_use_constructor:
7713 if (TREE_CODE (init) != TREE_VEC)
7714 init = store_init_value (decl, init);
7715 }
7716
7717 if (init)
7718 /* We must hide the initializer so that expand_decl
7719 won't try to do something it does not understand. */
7720 init = obscure_complex_init (decl, init);
7721 }
7722 else if (DECL_EXTERNAL (decl))
7723 ;
2f939d94 7724 else if (TYPE_P (type)
27778b73
MM
7725 && (IS_AGGR_TYPE (type) || TYPE_NEEDS_CONSTRUCTING (type)))
7726 {
7727 tree core_type = strip_array_types (type);
7728
7729 if (! TYPE_NEEDS_CONSTRUCTING (core_type))
7730 {
7731 if (CLASSTYPE_READONLY_FIELDS_NEED_INIT (core_type))
7732 cp_error ("structure `%D' with uninitialized const members", decl);
7733 if (CLASSTYPE_REF_FIELDS_NEED_INIT (core_type))
7734 cp_error ("structure `%D' with uninitialized reference members",
7735 decl);
7736 }
7737
7738 check_for_uninitialized_const_var (decl);
7739
d0f062fb 7740 if (COMPLETE_TYPE_P (type) && TYPE_NEEDS_CONSTRUCTING (type))
27778b73
MM
7741 init = obscure_complex_init (decl, NULL_TREE);
7742
7743 }
7744 else
7745 check_for_uninitialized_const_var (decl);
68642fb6 7746
c82dbd95 7747 return init;
27778b73
MM
7748}
7749
7750/* If DECL is not a local variable, give it RTL. */
7751
7752static void
7753make_rtl_for_nonlocal_decl (decl, init, asmspec)
7754 tree decl;
7755 tree init;
7756 const char *asmspec;
7757{
95ee998c
MM
7758 int toplev = toplevel_bindings_p ();
7759 int defer_p;
27778b73 7760
f39ee884
MM
7761 /* Handle non-variables up front. */
7762 if (TREE_CODE (decl) != VAR_DECL)
7763 {
7764 rest_of_decl_compilation (decl, asmspec, toplev, at_eof);
7765 return;
7766 }
7767
95ee998c
MM
7768 /* If we see a class member here, it should be a static data
7769 member. */
7770 if (DECL_LANG_SPECIFIC (decl) && DECL_IN_AGGR_P (decl))
7771 {
7772 my_friendly_assert (TREE_STATIC (decl), 19990828);
7773 /* An in-class declaration of a static data member should be
7774 external; it is only a declaration, and not a definition. */
7775 if (init == NULL_TREE)
7776 my_friendly_assert (DECL_EXTERNAL (decl), 20000723);
7777 }
7778
f39ee884
MM
7779 /* Set the DECL_ASSEMBLER_NAME for the variable. */
7780 if (asmspec)
92643fea 7781 SET_DECL_ASSEMBLER_NAME (decl, get_identifier (asmspec));
f39ee884 7782
95ee998c
MM
7783 /* We don't create any RTL for local variables. */
7784 if (DECL_FUNCTION_SCOPE_P (decl) && !TREE_STATIC (decl))
7785 return;
27778b73 7786
95ee998c
MM
7787 /* We defer emission of local statics until the corresponding
7788 DECL_STMT is expanded. */
7789 defer_p = DECL_FUNCTION_SCOPE_P (decl) || DECL_VIRTUAL_P (decl);
7790
7791 /* We try to defer namespace-scope static constants so that they are
7792 not emitted into the object file unncessarily. */
7793 if (!DECL_VIRTUAL_P (decl)
7794 && TREE_READONLY (decl)
7795 && DECL_INITIAL (decl) != NULL_TREE
7796 && DECL_INITIAL (decl) != error_mark_node
7797 && ! EMPTY_CONSTRUCTOR_P (DECL_INITIAL (decl))
7798 && toplev
7799 && !TREE_PUBLIC (decl))
7800 {
7801 /* Fool with the linkage according to #pragma interface. */
7802 if (!interface_unknown)
27778b73 7803 {
95ee998c
MM
7804 TREE_PUBLIC (decl) = 1;
7805 DECL_EXTERNAL (decl) = interface_only;
27778b73 7806 }
27778b73 7807
95ee998c 7808 defer_p = 1;
27778b73 7809 }
95ee998c 7810
92643fea
MM
7811 /* If we're deferring the variable, we only need to make RTL if
7812 there's an ASMSPEC. Otherwise, we'll lazily create it later when
7813 we need it. (There's no way to lazily create RTL for things that
7814 have assembly specs because the information about the specifier
7815 isn't stored in the tree, yet) */
7816 if (defer_p && asmspec)
6c418184 7817 make_decl_rtl (decl, asmspec);
95ee998c 7818 /* If we're not deferring, go ahead and assemble the variable. */
92643fea 7819 else if (!defer_p)
27778b73 7820 rest_of_decl_compilation (decl, asmspec, toplev, at_eof);
27778b73
MM
7821}
7822
7823/* The old ARM scoping rules injected variables declared in the
7824 initialization statement of a for-statement into the surrounding
7825 scope. We support this usage, in order to be backward-compatible.
7826 DECL is a just-declared VAR_DECL; if necessary inject its
7827 declaration into the surrounding scope. */
7828
b7b8bcd2 7829void
27778b73
MM
7830maybe_inject_for_scope_var (decl)
7831 tree decl;
7832{
c3783399
NS
7833 if (!DECL_NAME (decl))
7834 return;
5362b086 7835
27778b73
MM
7836 if (current_binding_level->is_for_scope)
7837 {
68642fb6 7838 struct binding_level *outer
27778b73
MM
7839 = current_binding_level->level_chain;
7840
7841 /* Check to see if the same name is already bound at the outer
7842 level, either because it was directly declared, or because a
7843 dead for-decl got preserved. In either case, the code would
7844 not have been valid under the ARM scope rules, so clear
7845 is_for_scope for the current_binding_level.
7846
7847 Otherwise, we need to preserve the temp slot for decl to last
7848 into the outer binding level. */
7849
68642fb6 7850 tree outer_binding
27778b73 7851 = TREE_CHAIN (IDENTIFIER_BINDING (DECL_NAME (decl)));
68642fb6 7852
27778b73 7853 if (outer_binding && BINDING_LEVEL (outer_binding) == outer
68642fb6 7854 && (TREE_CODE (BINDING_VALUE (outer_binding))
27778b73
MM
7855 == VAR_DECL)
7856 && DECL_DEAD_FOR_LOCAL (BINDING_VALUE (outer_binding)))
7857 {
7858 BINDING_VALUE (outer_binding)
7859 = DECL_SHADOWED_FOR_VAR (BINDING_VALUE (outer_binding));
7860 current_binding_level->is_for_scope = 0;
7861 }
7862 else if (DECL_IN_MEMORY_P (decl))
7863 preserve_temp_slots (DECL_RTL (decl));
7864 }
7865}
7866
ed5511d9 7867/* Generate code to initialize DECL (a local variable). */
27778b73 7868
b7b8bcd2
MM
7869void
7870initialize_local_var (decl, init, flags)
27778b73
MM
7871 tree decl;
7872 tree init;
27778b73
MM
7873 int flags;
7874{
9ed9e79a 7875 tree type = TREE_TYPE (decl);
27778b73 7876
9ed9e79a
MM
7877 /* If the type is bogus, don't bother initializing the variable. */
7878 if (type == error_mark_node)
7879 return;
b7b8bcd2 7880
b7b8bcd2
MM
7881 if (DECL_SIZE (decl) == NULL_TREE && !TREE_STATIC (decl))
7882 {
7883 /* If we used it already as memory, it must stay in memory. */
7884 DECL_INITIAL (decl) = NULL_TREE;
7885 TREE_ADDRESSABLE (decl) = TREE_USED (decl);
7886 }
7887
9ed9e79a
MM
7888 /* Local statics are handled differently from ordinary automatic
7889 variables. */
7890 if (TREE_STATIC (decl))
7891 {
7892 if (TYPE_NEEDS_CONSTRUCTING (type) || init != NULL_TREE
834c6dff 7893 || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
9ed9e79a
MM
7894 expand_static_init (decl, init);
7895 return;
7896 }
7897
27778b73
MM
7898 if (DECL_SIZE (decl) && type != error_mark_node)
7899 {
7900 int already_used;
68642fb6 7901
27778b73 7902 /* Compute and store the initial value. */
27778b73
MM
7903 already_used = TREE_USED (decl) || TREE_USED (type);
7904
7905 if (init || TYPE_NEEDS_CONSTRUCTING (type))
7906 {
24bef158
MM
7907 int saved_stmts_are_full_exprs_p;
7908
3a0d3e1e 7909 my_friendly_assert (building_stmt_tree (), 20000906);
f2c5f623 7910 saved_stmts_are_full_exprs_p = stmts_are_full_exprs_p ();
ae499cce 7911 current_stmt_tree ()->stmts_are_full_exprs_p = 1;
3a0d3e1e 7912 finish_expr_stmt (build_aggr_init (decl, init, flags));
5362b086 7913 current_stmt_tree ()->stmts_are_full_exprs_p =
ae499cce 7914 saved_stmts_are_full_exprs_p;
27778b73
MM
7915 }
7916
7917 /* Set this to 0 so we can tell whether an aggregate which was
7918 initialized was ever used. Don't do this if it has a
7919 destructor, so we don't complain about the 'resource
b7b8bcd2
MM
7920 allocation is initialization' idiom. Now set
7921 attribute((unused)) on types so decls of that type will be
7922 marked used. (see TREE_USED, above.) */
27778b73
MM
7923 if (TYPE_NEEDS_CONSTRUCTING (type)
7924 && ! already_used
834c6dff 7925 && TYPE_HAS_TRIVIAL_DESTRUCTOR (type)
27778b73
MM
7926 && DECL_NAME (decl))
7927 TREE_USED (decl) = 0;
b7b8bcd2 7928 else if (already_used)
27778b73
MM
7929 TREE_USED (decl) = 1;
7930 }
24bef158 7931}
27778b73 7932
24bef158
MM
7933/* Generate code to destroy DECL (a local variable). */
7934
68642fb6 7935static void
24bef158
MM
7936destroy_local_var (decl)
7937 tree decl;
7938{
9d85d30c
MM
7939 tree type = TREE_TYPE (decl);
7940 tree cleanup;
7941
7942 /* Only variables get cleaned up. */
7943 if (TREE_CODE (decl) != VAR_DECL)
7944 return;
68642fb6 7945
9d85d30c 7946 /* And only things with destructors need cleaning up. */
655dc6ee
JM
7947 if (type == error_mark_node
7948 || TYPE_HAS_TRIVIAL_DESTRUCTOR (type))
9d85d30c
MM
7949 return;
7950
7951 if (TREE_CODE (decl) == VAR_DECL &&
7952 (DECL_EXTERNAL (decl) || TREE_STATIC (decl)))
7953 /* We don't clean up things that aren't defined in this
7954 translation unit, or that need a static cleanup. The latter
7955 are handled by finish_file. */
7956 return;
68642fb6 7957
9d85d30c
MM
7958 /* Compute the cleanup. */
7959 cleanup = maybe_build_cleanup (decl);
27778b73 7960
b7b8bcd2 7961 /* Record the cleanup required for this declaration. */
24bef158
MM
7962 if (DECL_SIZE (decl) && TREE_TYPE (decl) != error_mark_node
7963 && cleanup)
7964 finish_decl_cleanup (decl, cleanup);
7965}
7966
8d08fdba
MS
7967/* Finish processing of a declaration;
7968 install its line number and initial value.
7969 If the length of an array type is not known before,
7970 it must be determined now, from the initial value, or it is an error.
7971
8b27e9ef 7972 INIT holds the value of an initializer that should be allowed to escape
8d08fdba
MS
7973 the normal rules.
7974
920f9474 7975 FLAGS is LOOKUP_ONLYCONVERTING if the = init syntax was used, else 0
f78c7bc6 7976 if the (init) syntax was used. */
8d08fdba
MS
7977
7978void
cd9f6678 7979cp_finish_decl (decl, init, asmspec_tree, flags)
8d08fdba
MS
7980 tree decl, init;
7981 tree asmspec_tree;
6060a796 7982 int flags;
8d08fdba
MS
7983{
7984 register tree type;
27778b73 7985 tree ttype = NULL_TREE;
9c0758dd 7986 const char *asmspec = NULL;
8d08fdba
MS
7987 int was_readonly = 0;
7988
8d08fdba
MS
7989 if (! decl)
7990 {
7991 if (init)
8251199e 7992 error ("assignment (not initialization) in declaration");
8d08fdba
MS
7993 return;
7994 }
7995
a4443a08 7996 /* If a name was specified, get the string. */
8d08fdba 7997 if (asmspec_tree)
8d08fdba 7998 asmspec = TREE_STRING_POINTER (asmspec_tree);
8d08fdba 7999
2c73f9f5
ML
8000 if (init && TREE_CODE (init) == NAMESPACE_DECL)
8001 {
920f9474 8002 cp_error ("cannot initialize `%D' to namespace `%D'",
2c73f9f5
ML
8003 decl, init);
8004 init = NULL_TREE;
8005 }
8006
6ba89f8e 8007 if (current_class_type
4f1c5b7d 8008 && CP_DECL_CONTEXT (decl) == current_class_type
6ba89f8e
MM
8009 && TYPE_BEING_DEFINED (current_class_type)
8010 && (DECL_INITIAL (decl) || init))
3febd123 8011 DECL_INITIALIZED_IN_CLASS_P (decl) = 1;
6ba89f8e 8012
68642fb6 8013 if (TREE_CODE (decl) == VAR_DECL
9a68c51f
JM
8014 && DECL_CONTEXT (decl)
8015 && TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL
8016 && DECL_CONTEXT (decl) != current_namespace
8017 && init)
8018 {
8019 /* Leave the namespace of the object. */
8020 pop_decl_namespace ();
8021 }
8022
c82dbd95 8023 type = TREE_TYPE (decl);
8d08fdba 8024
f376e137 8025 if (type == error_mark_node)
cd9f6678 8026 return;
5362b086 8027
24bef158 8028 /* Add this declaration to the statement-tree. */
5362b086 8029 if (building_stmt_tree ()
44835fdd
MM
8030 && at_function_scope_p ()
8031 && TREE_CODE (decl) != RESULT_DECL)
24bef158
MM
8032 add_decl_stmt (decl);
8033
a7a7710d
NS
8034 if (TYPE_HAS_MUTABLE_P (type))
8035 TREE_READONLY (decl) = 0;
24bef158 8036
5156628f 8037 if (processing_template_decl)
5566b478
MS
8038 {
8039 if (init && DECL_INITIAL (decl))
2a1e9fdd 8040 DECL_INITIAL (decl) = init;
5566b478
MS
8041 goto finish_end0;
8042 }
3e41d13b 8043
27778b73
MM
8044 /* Parameters are handled by store_parm_decls, not cp_finish_decl. */
8045 my_friendly_assert (TREE_CODE (decl) != PARM_DECL, 19990828);
8046
8d08fdba
MS
8047 /* Take care of TYPE_DECLs up front. */
8048 if (TREE_CODE (decl) == TYPE_DECL)
8049 {
8050 if (init && DECL_INITIAL (decl))
8051 {
8052 /* typedef foo = bar; store the type of bar as the type of foo. */
8053 TREE_TYPE (decl) = type = TREE_TYPE (init);
8054 DECL_INITIAL (decl) = init = NULL_TREE;
8055 }
a0a33927
MS
8056 if (type != error_mark_node
8057 && IS_AGGR_TYPE (type) && DECL_NAME (decl))
8d08fdba
MS
8058 {
8059 if (TREE_TYPE (DECL_NAME (decl)) && TREE_TYPE (decl) != type)
8251199e 8060 cp_warning ("shadowing previous type declaration of `%#D'", decl);
8d08fdba
MS
8061 set_identifier_type_value (DECL_NAME (decl), type);
8062 CLASSTYPE_GOT_SEMICOLON (type) = 1;
8063 }
8064 GNU_xref_decl (current_function_decl, decl);
cffa8729
MS
8065
8066 /* If we have installed this as the canonical typedef for this
8067 type, and that type has not been defined yet, delay emitting
956d6950 8068 the debug information for it, as we will emit it later. */
d2e5ee5c 8069 if (TYPE_MAIN_DECL (TREE_TYPE (decl)) == decl
d0f062fb 8070 && !COMPLETE_TYPE_P (TREE_TYPE (decl)))
cffa8729
MS
8071 TYPE_DECL_SUPPRESS_DEBUG (decl) = 1;
8072
8d08fdba 8073 rest_of_decl_compilation (decl, NULL_PTR,
5566b478 8074 DECL_CONTEXT (decl) == NULL_TREE, at_eof);
8d08fdba
MS
8075 goto finish_end;
8076 }
3e41d13b 8077
8d08fdba 8078 if (TREE_CODE (decl) != FUNCTION_DECL)
3e41d13b 8079 ttype = target_type (type);
8d08fdba
MS
8080
8081 if (! DECL_EXTERNAL (decl) && TREE_READONLY (decl)
8082 && TYPE_NEEDS_CONSTRUCTING (type))
8083 {
8d08fdba
MS
8084 /* Currently, GNU C++ puts constants in text space, making them
8085 impossible to initialize. In the future, one would hope for
8086 an operating system which understood the difference between
8087 initialization and the running of a program. */
8088 was_readonly = 1;
8089 TREE_READONLY (decl) = 0;
8090 }
8091
27778b73 8092 if (TREE_CODE (decl) == FIELD_DECL && asmspec)
8d08fdba 8093 {
27778b73
MM
8094 /* This must override the asm specifier which was placed by
8095 grokclassfn. Lay this out fresh. */
19e7881c 8096 SET_DECL_RTL (TREE_TYPE (decl), NULL_RTX);
92643fea 8097 SET_DECL_ASSEMBLER_NAME (decl, get_identifier (asmspec));
6c418184 8098 make_decl_rtl (decl, asmspec);
8d08fdba
MS
8099 }
8100
c82dbd95
MM
8101 /* Deduce size of array from initialization, if not already known. */
8102 maybe_deduce_size_from_array_init (decl, init);
8103 init = check_initializer (decl, init);
3e41d13b 8104
8d08fdba
MS
8105 GNU_xref_decl (current_function_decl, decl);
8106
8d08fdba 8107 if (TREE_CODE (decl) == VAR_DECL)
57b52417 8108 layout_var_decl (decl);
8d08fdba
MS
8109
8110 /* Output the assembler code and/or RTL code for variables and functions,
8111 unless the type is an undefined structure or union.
8112 If not, it will get done when the type is completed. */
8d08fdba
MS
8113 if (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == FUNCTION_DECL
8114 || TREE_CODE (decl) == RESULT_DECL)
8115 {
27778b73
MM
8116 if (TREE_CODE (decl) == VAR_DECL)
8117 maybe_commonize_var (decl);
8d08fdba 8118
27778b73 8119 make_rtl_for_nonlocal_decl (decl, init, asmspec);
8d08fdba 8120
68642fb6 8121 if (TREE_CODE (type) == FUNCTION_TYPE
27778b73 8122 || TREE_CODE (type) == METHOD_TYPE)
68642fb6 8123 abstract_virtuals_error (decl,
27778b73 8124 strip_array_types (TREE_TYPE (type)));
68642fb6 8125 else
27778b73 8126 abstract_virtuals_error (decl, strip_array_types (type));
8d08fdba 8127
8d08fdba 8128 if (TREE_CODE (decl) == FUNCTION_DECL)
faae18ab 8129 ;
67d743fe
MS
8130 else if (DECL_EXTERNAL (decl)
8131 && ! (DECL_LANG_SPECIFIC (decl)
8132 && DECL_NOT_REALLY_EXTERN (decl)))
5566b478
MS
8133 {
8134 if (init)
8135 DECL_INITIAL (decl) = init;
8136 }
b35d4555 8137 else if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL)
8d08fdba 8138 {
b7b8bcd2 8139 /* This is a local declaration. */
b35d4555
MM
8140 if (doing_semantic_analysis_p ())
8141 maybe_inject_for_scope_var (decl);
b7b8bcd2
MM
8142 /* Initialize the local variable. But, if we're building a
8143 statement-tree, we'll do the initialization when we
8144 expand the tree. */
24bef158
MM
8145 if (processing_template_decl)
8146 {
8147 if (init || DECL_INITIAL (decl) == error_mark_node)
8148 DECL_INITIAL (decl) = init;
8149 }
8150 else
8151 {
b35d4555
MM
8152 /* If we're not building RTL, then we need to do so
8153 now. */
44835fdd 8154 my_friendly_assert (building_stmt_tree (), 20000906);
b35d4555 8155 /* Initialize the variable. */
24bef158
MM
8156 initialize_local_var (decl, init, flags);
8157 /* Clean up the variable. */
8158 destroy_local_var (decl);
8159 }
8d08fdba 8160 }
9ed9e79a
MM
8161 else if (TREE_STATIC (decl) && type != error_mark_node)
8162 {
8163 /* Cleanups for static variables are handled by `finish_file'. */
8164 if (TYPE_NEEDS_CONSTRUCTING (type) || init != NULL_TREE
834c6dff 8165 || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
9ed9e79a
MM
8166 expand_static_init (decl, init);
8167 }
8d08fdba
MS
8168 finish_end0:
8169
8170 /* Undo call to `pushclass' that was done in `start_decl'
8171 due to initialization of qualified member variable.
8172 I.e., Foo::x = 10; */
8173 {
4f1c5b7d 8174 tree context = CP_DECL_CONTEXT (decl);
8d08fdba 8175 if (context
2f939d94 8176 && TYPE_P (context)
8d08fdba
MS
8177 && (TREE_CODE (decl) == VAR_DECL
8178 /* We also have a pushclass done that we need to undo here
8179 if we're at top level and declare a method. */
5566b478
MS
8180 || TREE_CODE (decl) == FUNCTION_DECL)
8181 /* If size hasn't been set, we're still defining it,
8182 and therefore inside the class body; don't pop
8183 the binding level.. */
d0f062fb 8184 && COMPLETE_TYPE_P (context)
5566b478 8185 && context == current_class_type)
6b400b21 8186 pop_nested_class ();
8d08fdba
MS
8187 }
8188 }
8189
8190 finish_end:
8191
8d08fdba
MS
8192 if (was_readonly)
8193 TREE_READONLY (decl) = 1;
8d08fdba
MS
8194}
8195
82580166 8196/* This is here for a midend callback from c-common.c */
e92cc029 8197
82580166
MS
8198void
8199finish_decl (decl, init, asmspec_tree)
8200 tree decl, init;
8201 tree asmspec_tree;
8202{
cd9f6678 8203 cp_finish_decl (decl, init, asmspec_tree, 0);
82580166
MS
8204}
8205
db4283a0
MM
8206/* Returns a declaration for a VAR_DECL as if:
8207
8208 extern "C" TYPE NAME;
8209
8210 had been seen. Used to create compiler-generated global
8211 variables. */
8212
8213tree
8214declare_global_var (name, type)
8215 tree name;
8216 tree type;
8217{
8218 tree decl;
8219
8220 push_to_top_level ();
8221 decl = build_decl (VAR_DECL, name, type);
8222 TREE_PUBLIC (decl) = 1;
8223 DECL_EXTERNAL (decl) = 1;
8224 DECL_ARTIFICIAL (decl) = 1;
8225 pushdecl (decl);
8226 cp_finish_decl (decl, NULL_TREE, NULL_TREE, 0);
8227 pop_from_top_level ();
8228
8229 return decl;
8230}
8231
8232/* Returns a pointer to the `atexit' function. Note that if
8233 FLAG_USE_CXA_ATEXIT is non-zero, then this will actually be the new
8234 `__cxa_atexit' function specified in the IA64 C++ ABI. */
8235
8236static tree
8237get_atexit_node ()
8238{
8239 tree atexit_fndecl;
8240 tree arg_types;
8241 tree fn_type;
8242 tree fn_ptr_type;
8243 const char *name;
8244
8245 if (atexit_node)
8246 return atexit_node;
8247
8248 if (flag_use_cxa_atexit)
8249 {
8250 /* The declaration for `__cxa_atexit' is:
8251
8252 int __cxa_atexit (void (*)(void *), void *, void *)
8253
8254 We build up the argument types and then then function type
8255 itself. */
68642fb6 8256
db4283a0
MM
8257 /* First, build the pointer-to-function type for the first
8258 argument. */
8259 arg_types = tree_cons (NULL_TREE, ptr_type_node, void_list_node);
8260 fn_type = build_function_type (void_type_node, arg_types);
8261 fn_ptr_type = build_pointer_type (fn_type);
8262 /* Then, build the rest of the argument types. */
8263 arg_types = tree_cons (NULL_TREE, ptr_type_node, void_list_node);
8264 arg_types = tree_cons (NULL_TREE, ptr_type_node, arg_types);
8265 arg_types = tree_cons (NULL_TREE, fn_ptr_type, arg_types);
8266 /* And the final __cxa_atexit type. */
8267 fn_type = build_function_type (integer_type_node, arg_types);
8268 fn_ptr_type = build_pointer_type (fn_type);
8269 name = "__cxa_atexit";
8270 }
8271 else
8272 {
8273 /* The declaration for `atexit' is:
68642fb6 8274
db4283a0
MM
8275 int atexit (void (*)());
8276
8277 We build up the argument types and then then function type
8278 itself. */
8279 fn_type = build_function_type (void_type_node, void_list_node);
8280 fn_ptr_type = build_pointer_type (fn_type);
8281 arg_types = tree_cons (NULL_TREE, fn_ptr_type, void_list_node);
8282 /* Build the final atexit type. */
8283 fn_type = build_function_type (integer_type_node, arg_types);
8284 name = "atexit";
8285 }
8286
8287 /* Now, build the function declaration. */
8288 push_lang_context (lang_name_c);
0c11ada6 8289 atexit_fndecl = build_library_fn_ptr (name, fn_type);
db4283a0
MM
8290 mark_used (atexit_fndecl);
8291 pop_lang_context ();
8292 atexit_node = default_conversion (atexit_fndecl);
8293
8294 return atexit_node;
8295}
8296
8297/* Returns the __dso_handle VAR_DECL. */
8298
8299static tree
8300get_dso_handle_node ()
8301{
8302 if (dso_handle_node)
8303 return dso_handle_node;
8304
8305 /* Declare the variable. */
8306 dso_handle_node = declare_global_var (get_identifier ("__dso_handle"),
8307 ptr_type_node);
8308
8309 return dso_handle_node;
8310}
8311
8312/* Begin a new function with internal linkage whose job will be simply
8313 to destroy some particular variable. */
8314
8315static tree
8316start_cleanup_fn ()
8317{
8318 static int counter = 0;
8319 int old_interface_unknown = interface_unknown;
8320 char name[32];
8321 tree parmtypes;
8322 tree fntype;
8323 tree fndecl;
8324
8325 push_to_top_level ();
8326
8327 /* No need to mangle this. */
8328 push_lang_context (lang_name_c);
8329
8330 interface_unknown = 1;
8331
8332 /* Build the parameter-types. */
8333 parmtypes = void_list_node;
8334 /* Functions passed to __cxa_atexit take an additional parameter.
8335 We'll just ignore it. After we implement the new calling
8336 convention for destructors, we can eliminate the use of
8337 additional cleanup functions entirely in the -fnew-abi case. */
8338 if (flag_use_cxa_atexit)
8339 parmtypes = tree_cons (NULL_TREE, ptr_type_node, parmtypes);
8340 /* Build the function type itself. */
8341 fntype = build_function_type (void_type_node, parmtypes);
8342 /* Build the name of the function. */
8343 sprintf (name, "__tcf_%d", counter++);
8344 /* Build the function declaration. */
8345 fndecl = build_lang_decl (FUNCTION_DECL, get_identifier (name), fntype);
8346 /* It's a function with internal linkage, generated by the
8347 compiler. */
8348 TREE_PUBLIC (fndecl) = 0;
8349 DECL_ARTIFICIAL (fndecl) = 1;
5a728aca
MM
8350 /* Make the function `inline' so that it is only emitted if it is
8351 actually needed. It is unlikely that it will be inlined, since
8352 it is only called via a function pointer, but we avoid unncessary
8353 emissions this way. */
8354 DECL_INLINE (fndecl) = 1;
db4283a0
MM
8355 /* Build the parameter. */
8356 if (flag_use_cxa_atexit)
8357 {
8358 tree parmdecl;
8359
8360 parmdecl = build_decl (PARM_DECL, NULL_TREE, ptr_type_node);
8361 DECL_CONTEXT (parmdecl) = fndecl;
8362 DECL_ARG_TYPE (parmdecl) = ptr_type_node;
8363 TREE_USED (parmdecl) = 1;
8364 DECL_ARGUMENTS (fndecl) = parmdecl;
8365 }
8366
09ed39ad 8367 pushdecl (fndecl);
db4283a0
MM
8368 start_function (/*specs=*/NULL_TREE, fndecl, NULL_TREE, SF_PRE_PARSED);
8369 do_pushlevel ();
8370
8371 interface_unknown = old_interface_unknown;
8372
8373 pop_lang_context ();
8374
8375 return current_function_decl;
8376}
8377
8378/* Finish the cleanup function begun by start_cleanup_fn. */
8379
8380static void
8381end_cleanup_fn ()
8382{
8383 do_poplevel ();
8384
0acf7199 8385 expand_body (finish_function (0));
db4283a0
MM
8386
8387 pop_from_top_level ();
8388}
8389
bf419747
MM
8390/* Generate code to handle the destruction of DECL, an object with
8391 static storage duration. */
f0105ed3 8392
bf419747
MM
8393void
8394register_dtor_fn (decl)
f0105ed3
MM
8395 tree decl;
8396{
db4283a0 8397 tree cleanup;
f0105ed3 8398 tree compound_stmt;
db4283a0
MM
8399 tree args;
8400 tree fcall;
f0105ed3 8401
db4283a0 8402 int saved_flag_access_control;
f0105ed3 8403
834c6dff 8404 if (TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
bf419747
MM
8405 return;
8406
f0105ed3
MM
8407 /* Call build_cleanup before we enter the anonymous function so that
8408 any access checks will be done relative to the current scope,
8409 rather than the scope of the anonymous function. */
8410 build_cleanup (decl);
8411
8412 /* Now start the function. */
db4283a0 8413 cleanup = start_cleanup_fn ();
f0105ed3
MM
8414
8415 /* Now, recompute the cleanup. It may contain SAVE_EXPRs that refer
8416 to the original function, rather than the anonymous one. That
8417 will make the back-end think that nested functions are in use,
8418 which causes confusion. */
8419 saved_flag_access_control = flag_access_control;
8420 flag_access_control = 0;
8421 fcall = build_cleanup (decl);
8422 flag_access_control = saved_flag_access_control;
8423
8424 /* Create the body of the anonymous function. */
8425 compound_stmt = begin_compound_stmt (/*has_no_scope=*/0);
8426 finish_expr_stmt (fcall);
8427 finish_compound_stmt (/*has_no_scope=*/0, compound_stmt);
db4283a0 8428 end_cleanup_fn ();
f0105ed3
MM
8429
8430 /* Call atexit with the cleanup function. */
8431 mark_addressable (cleanup);
8432 cleanup = build_unary_op (ADDR_EXPR, cleanup, 0);
db4283a0
MM
8433 if (flag_use_cxa_atexit)
8434 {
8435 args = tree_cons (NULL_TREE, get_dso_handle_node (), NULL_TREE);
8436 args = tree_cons (NULL_TREE, null_pointer_node, args);
8437 args = tree_cons (NULL_TREE, cleanup, args);
8438 }
8439 else
8440 args = tree_cons (NULL_TREE, cleanup, NULL_TREE);
8441 finish_expr_stmt (build_function_call (get_atexit_node (), args));
f0105ed3
MM
8442}
8443
8d08fdba
MS
8444void
8445expand_static_init (decl, init)
8446 tree decl;
8447 tree init;
8448{
8449 tree oldstatic = value_member (decl, static_aggregates);
a4443a08 8450
8d08fdba
MS
8451 if (oldstatic)
8452 {
8453 if (TREE_PURPOSE (oldstatic) && init != NULL_TREE)
8251199e 8454 cp_error ("multiple initializations given for `%D'", decl);
8d08fdba 8455 }
0aafb128 8456 else if (! toplevel_bindings_p ())
8d08fdba
MS
8457 {
8458 /* Emit code to perform this initialization but once. */
b7b8bcd2 8459 tree if_stmt;
f0105ed3 8460 tree then_clause;
f1dedc31 8461 tree assignment;
c395453c
MM
8462 tree guard;
8463 tree guard_init;
8d08fdba 8464
2036a15c
MM
8465 /* Emit code to perform this initialization but once. This code
8466 looks like:
8467
c395453c
MM
8468 static int guard = 0;
8469 if (!guard) {
2036a15c 8470 // Do initialization.
c395453c 8471 guard = 1;
2036a15c
MM
8472 // Register variable for destruction at end of program.
8473 }
8474
8475 Note that the `temp' variable is only set to 1 *after* the
8476 initialization is complete. This ensures that an exception,
8477 thrown during the construction, will cause the variable to
8478 reinitialized when we pass through this code again, as per:
68642fb6 8479
2036a15c
MM
8480 [stmt.dcl]
8481
8482 If the initialization exits by throwing an exception, the
8483 initialization is not complete, so it will be tried again
8484 the next time control enters the declaration.
8485
8486 In theory, this process should be thread-safe, too; multiple
8487 threads should not be able to initialize the variable more
8488 than once. We don't yet attempt to ensure thread-safety. */
c395453c
MM
8489
8490 /* Create the guard variable. */
8491 guard = get_guard (decl);
2036a15c
MM
8492
8493 /* Begin the conditional initialization. */
b7b8bcd2 8494 if_stmt = begin_if_stmt ();
c395453c 8495 finish_if_stmt_cond (get_guard_cond (guard), if_stmt);
f0105ed3 8496 then_clause = begin_compound_stmt (/*has_no_scope=*/0);
72b7eeff 8497
2036a15c 8498 /* Do the initialization itself. */
28cbf42c 8499 if (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl))
f30432d7 8500 || (init && TREE_CODE (init) == TREE_LIST))
f1dedc31 8501 assignment = build_aggr_init (decl, init, 0);
f30432d7 8502 else if (init)
c557501d
MM
8503 /* The initialization we're doing here is just a bitwise
8504 copy. */
8505 assignment = build (INIT_EXPR, TREE_TYPE (decl), decl, init);
f1dedc31
MM
8506 else
8507 assignment = NULL_TREE;
8508
8509 /* Once the assignment is complete, set TEMP to 1. Since the
8510 construction of the static object is complete at this point,
8511 we want to make sure TEMP is set to 1 even if a temporary
8512 constructed during the initialization throws an exception
8513 when it is destroyed. So, we combine the initialization and
8514 the assignment to TEMP into a single expression, ensuring
8515 that when we call finish_expr_stmt the cleanups will not be
8516 run until after TEMP is set to 1. */
c395453c 8517 guard_init = set_guard (guard);
f1dedc31
MM
8518 if (assignment)
8519 {
8520 assignment = tree_cons (NULL_TREE, assignment,
68642fb6 8521 build_tree_list (NULL_TREE,
c395453c 8522 guard_init));
f1dedc31
MM
8523 assignment = build_compound_expr (assignment);
8524 }
8525 else
c395453c 8526 assignment = guard_init;
f1dedc31 8527 finish_expr_stmt (assignment);
72b7eeff 8528
2036a15c
MM
8529 /* Use atexit to register a function for destroying this static
8530 variable. */
bf419747 8531 register_dtor_fn (decl);
72b7eeff 8532
f0105ed3 8533 finish_compound_stmt (/*has_no_scope=*/0, then_clause);
b7b8bcd2
MM
8534 finish_then_clause (if_stmt);
8535 finish_if_stmt ();
8d08fdba
MS
8536 }
8537 else
bbd15aac 8538 static_aggregates = tree_cons (init, decl, static_aggregates);
8d08fdba 8539}
3c5c0849
MM
8540
8541/* Finish the declaration of a catch-parameter. */
8542
b35d4555 8543tree
3c5c0849
MM
8544start_handler_parms (declspecs, declarator)
8545 tree declspecs;
8546 tree declarator;
8547{
8548 tree decl;
8549 if (declspecs)
8550 {
8551 decl = grokdeclarator (declarator, declspecs, CATCHPARM,
8552 1, NULL_TREE);
8553 if (decl == NULL_TREE)
8554 error ("invalid catch parameter");
8555 }
8556 else
8557 decl = NULL_TREE;
b35d4555
MM
8558
8559 return decl;
3c5c0849
MM
8560}
8561
8d08fdba
MS
8562\f
8563/* Make TYPE a complete type based on INITIAL_VALUE.
8564 Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
6ab5c740 8565 2 if there was no information (in which case assume 0 if DO_DEFAULT). */
8d08fdba
MS
8566
8567int
8568complete_array_type (type, initial_value, do_default)
8569 tree type, initial_value;
8570 int do_default;
8571{
8572 register tree maxindex = NULL_TREE;
8573 int value = 0;
68642fb6 8574
8d08fdba
MS
8575 if (initial_value)
8576 {
7b019c19
MM
8577 /* An array of character type can be initialized from a
8578 brace-enclosed string constant. */
8579 if (char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type)))
8580 && TREE_CODE (initial_value) == CONSTRUCTOR
8581 && CONSTRUCTOR_ELTS (initial_value)
8582 && (TREE_CODE (TREE_VALUE (CONSTRUCTOR_ELTS (initial_value)))
8583 == STRING_CST)
8584 && TREE_CHAIN (CONSTRUCTOR_ELTS (initial_value)) == NULL_TREE)
8585 initial_value = TREE_VALUE (CONSTRUCTOR_ELTS (initial_value));
8586
8587 /* Note MAXINDEX is really the maximum index, one less than the
8588 size. */
8d08fdba 8589 if (TREE_CODE (initial_value) == STRING_CST)
e1cd6e56
MS
8590 {
8591 int eltsize
8592 = int_size_in_bytes (TREE_TYPE (TREE_TYPE (initial_value)));
8593 maxindex = build_int_2 ((TREE_STRING_LENGTH (initial_value)
8594 / eltsize) - 1, 0);
8595 }
8d08fdba
MS
8596 else if (TREE_CODE (initial_value) == CONSTRUCTOR)
8597 {
e1cd6e56 8598 tree elts = CONSTRUCTOR_ELTS (initial_value);
fed3cef0
RK
8599
8600 maxindex = ssize_int (-1);
e1cd6e56
MS
8601 for (; elts; elts = TREE_CHAIN (elts))
8602 {
8603 if (TREE_PURPOSE (elts))
8604 maxindex = TREE_PURPOSE (elts);
8605 else
fed3cef0 8606 maxindex = size_binop (PLUS_EXPR, maxindex, ssize_int (1));
e1cd6e56
MS
8607 }
8608 maxindex = copy_node (maxindex);
8d08fdba
MS
8609 }
8610 else
8611 {
8612 /* Make an error message unless that happened already. */
8613 if (initial_value != error_mark_node)
8614 value = 1;
0db982be
ML
8615 else
8616 initial_value = NULL_TREE;
8d08fdba
MS
8617
8618 /* Prevent further error messages. */
8619 maxindex = build_int_2 (0, 0);
8620 }
8621 }
8622
8623 if (!maxindex)
8624 {
8625 if (do_default)
8626 maxindex = build_int_2 (0, 0);
8627 value = 2;
8628 }
8629
8630 if (maxindex)
8631 {
51c184be 8632 tree itype;
6ab5c740
NS
8633 tree domain;
8634
8635 domain = build_index_type (maxindex);
8636 TYPE_DOMAIN (type) = domain;
51c184be 8637
dff6b454 8638 if (! TREE_TYPE (maxindex))
6ab5c740 8639 TREE_TYPE (maxindex) = domain;
51c184be
MS
8640 if (initial_value)
8641 itype = TREE_TYPE (initial_value);
8642 else
8643 itype = NULL;
8644 if (itype && !TYPE_DOMAIN (itype))
6ab5c740 8645 TYPE_DOMAIN (itype) = domain;
dff6b454
RK
8646 /* The type of the main variant should never be used for arrays
8647 of different sizes. It should only ever be completed with the
8648 size of the array. */
8649 if (! TYPE_DOMAIN (TYPE_MAIN_VARIANT (type)))
6ab5c740 8650 TYPE_DOMAIN (TYPE_MAIN_VARIANT (type)) = domain;
8d08fdba
MS
8651 }
8652
8653 /* Lay out the type now that we can get the real answer. */
8654
8655 layout_type (type);
8656
8657 return value;
8658}
8659\f
8660/* Return zero if something is declared to be a member of type
8661 CTYPE when in the context of CUR_TYPE. STRING is the error
8662 message to print in that case. Otherwise, quietly return 1. */
e92cc029 8663
8d08fdba 8664static int
4dacf5bd 8665member_function_or_else (ctype, cur_type, flags)
8d08fdba 8666 tree ctype, cur_type;
4dacf5bd 8667 enum overload_flags flags;
8d08fdba
MS
8668{
8669 if (ctype && ctype != cur_type)
8670 {
4dacf5bd 8671 if (flags == DTOR_FLAG)
2ae7bada
NS
8672 cp_error ("destructor for alien class `%T' cannot be a member",
8673 ctype);
4dacf5bd 8674 else
2ae7bada
NS
8675 cp_error ("constructor for alien class `%T' cannot be a member",
8676 ctype);
8d08fdba
MS
8677 return 0;
8678 }
8679 return 1;
8680}
8681\f
8682/* Subroutine of `grokdeclarator'. */
8683
8684/* Generate errors possibly applicable for a given set of specifiers.
8685 This is for ARM $7.1.2. */
e92cc029 8686
8d08fdba
MS
8687static void
8688bad_specifiers (object, type, virtualp, quals, inlinep, friendp, raises)
8689 tree object;
d8e178a0 8690 const char *type;
8d08fdba
MS
8691 int virtualp, quals, friendp, raises, inlinep;
8692{
8693 if (virtualp)
8251199e 8694 cp_error ("`%D' declared as a `virtual' %s", object, type);
8d08fdba 8695 if (inlinep)
8251199e 8696 cp_error ("`%D' declared as an `inline' %s", object, type);
8d08fdba 8697 if (quals)
8251199e 8698 cp_error ("`const' and `volatile' function specifiers on `%D' invalid in %s declaration",
a28e3c7f 8699 object, type);
8d08fdba 8700 if (friendp)
f8e55f34 8701 cp_error_at ("`%D' declared as a friend", object);
a714e5c5
JM
8702 if (raises && !TYPE_PTRFN_P (TREE_TYPE (object))
8703 && !TYPE_PTRMEMFUNC_P (TREE_TYPE (object)))
f8e55f34 8704 cp_error_at ("`%D' declared with an exception specification", object);
8d08fdba
MS
8705}
8706
8707/* CTYPE is class type, or null if non-class.
8708 TYPE is type this FUNCTION_DECL should have, either FUNCTION_TYPE
8709 or METHOD_TYPE.
8710 DECLARATOR is the function's name.
8711 VIRTUALP is truthvalue of whether the function is virtual or not.
8712 FLAGS are to be passed through to `grokclassfn'.
8713 QUALS are qualifiers indicating whether the function is `const'
8714 or `volatile'.
8715 RAISES is a list of exceptions that this function can raise.
8716 CHECK is 1 if we must find this method in CTYPE, 0 if we should
68642fb6 8717 not look, and -1 if we should not call `grokclassfn' at all.
3ddfb0e6 8718
20496fa2 8719 Returns `NULL_TREE' if something goes wrong, after issuing
3ddfb0e6 8720 applicable error messages. */
e92cc029 8721
8d08fdba 8722static tree
386b8a85 8723grokfndecl (ctype, type, declarator, orig_declarator, virtualp, flags, quals,
7a8f9fa9 8724 raises, check, friendp, publicp, inlinep, funcdef_flag,
2c73f9f5 8725 template_count, in_namespace)
8d08fdba
MS
8726 tree ctype, type;
8727 tree declarator;
386b8a85 8728 tree orig_declarator;
8d08fdba
MS
8729 int virtualp;
8730 enum overload_flags flags;
7a8f9fa9 8731 tree quals, raises;
386b8a85 8732 int check, friendp, publicp, inlinep, funcdef_flag, template_count;
2c73f9f5 8733 tree in_namespace;
8d08fdba
MS
8734{
8735 tree cname, decl;
8736 int staticp = ctype && TREE_CODE (type) == FUNCTION_TYPE;
856216bb 8737 int has_default_arg = 0;
42976354 8738 tree t;
8d08fdba
MS
8739
8740 if (ctype)
8741 cname = TREE_CODE (TYPE_NAME (ctype)) == TYPE_DECL
8742 ? TYPE_IDENTIFIER (ctype) : TYPE_NAME (ctype);
8743 else
8744 cname = NULL_TREE;
8745
8746 if (raises)
8747 {
f30432d7 8748 type = build_exception_variant (type, raises);
8d08fdba 8749 }
c11b6f21 8750
8d08fdba 8751 decl = build_lang_decl (FUNCTION_DECL, declarator, type);
2c73f9f5 8752 /* Propagate volatile out from type to decl. */
8d08fdba 8753 if (TYPE_VOLATILE (type))
893de33c 8754 TREE_THIS_VOLATILE (decl) = 1;
8d08fdba 8755
79c4d4b7 8756 /* If this decl has namespace scope, set that up. */
2c73f9f5 8757 if (in_namespace)
b262d64c 8758 set_decl_namespace (decl, in_namespace, friendp);
adae082f 8759 else if (!ctype)
79c4d4b7 8760 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
2c73f9f5 8761
0f8766b8
JM
8762 /* `main' and builtins have implicit 'C' linkage. */
8763 if ((MAIN_NAME_P (declarator)
8764 || (IDENTIFIER_LENGTH (declarator) > 10
8765 && IDENTIFIER_POINTER (declarator)[0] == '_'
8766 && IDENTIFIER_POINTER (declarator)[1] == '_'
8767 && strncmp (IDENTIFIER_POINTER (declarator)+2, "builtin_", 8) == 0))
8768 && current_lang_name == lang_name_cplusplus
94706a5c 8769 && ctype == NULL_TREE
79c4d4b7
JM
8770 /* NULL_TREE means global namespace. */
8771 && DECL_CONTEXT (decl) == NULL_TREE)
0f8766b8
JM
8772 DECL_LANGUAGE (decl) = lang_c;
8773
8d08fdba
MS
8774 /* Should probably propagate const out from type to decl I bet (mrs). */
8775 if (staticp)
8776 {
8777 DECL_STATIC_FUNCTION_P (decl) = 1;
8778 DECL_CONTEXT (decl) = ctype;
8d08fdba
MS
8779 }
8780
e76a2646 8781 if (ctype)
4f1c5b7d 8782 DECL_CONTEXT (decl) = ctype;
e76a2646 8783
0f8766b8 8784 if (ctype == NULL_TREE && DECL_MAIN_P (decl))
faae18ab 8785 {
848b92e1 8786 if (processing_template_decl)
cb9a3ff8 8787 error ("cannot declare `::main' to be a template");
faae18ab 8788 if (inlinep)
cb9a3ff8 8789 error ("cannot declare `::main' to be inline");
f22967f3 8790 if (!publicp)
cb9a3ff8 8791 error ("cannot declare `::main' to be static");
f22967f3
MM
8792 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
8793 integer_type_node))
8794 error ("`main' must return `int'");
faae18ab
MS
8795 inlinep = 0;
8796 publicp = 1;
8797 }
50a6dbd7 8798
59e76fc6
JM
8799 /* Members of anonymous types and local classes have no linkage; make
8800 them internal. */
8801 if (ctype && (ANON_AGGRNAME_P (TYPE_IDENTIFIER (ctype))
4f1c5b7d 8802 || decl_function_context (TYPE_MAIN_DECL (ctype))))
50a6dbd7
JM
8803 publicp = 0;
8804
8805 if (publicp)
8806 {
8807 /* [basic.link]: A name with no linkage (notably, the name of a class
8808 or enumeration declared in a local scope) shall not be used to
8809 declare an entity with linkage.
8810
8811 Only check this for public decls for now. */
8812 t = no_linkage_check (TREE_TYPE (decl));
8813 if (t)
8814 {
7f7c930e
JM
8815 if (ANON_AGGRNAME_P (TYPE_IDENTIFIER (t)))
8816 {
eb68cb58 8817 if (DECL_EXTERN_C_P (decl))
7f7c930e
JM
8818 /* Allow this; it's pretty common in C. */;
8819 else
8820 cp_pedwarn ("non-local function `%#D' uses anonymous type",
8821 decl);
8822 }
50a6dbd7 8823 else
8251199e 8824 cp_pedwarn ("non-local function `%#D' uses local type `%T'",
cce2be43 8825 decl, t);
50a6dbd7
JM
8826 }
8827 }
8828
893de33c 8829 TREE_PUBLIC (decl) = publicp;
faae18ab 8830 if (! publicp)
893de33c
JM
8831 {
8832 DECL_INTERFACE_KNOWN (decl) = 1;
8833 DECL_NOT_REALLY_EXTERN (decl) = 1;
8834 }
faae18ab
MS
8835
8836 if (inlinep)
8837 DECL_THIS_INLINE (decl) = DECL_INLINE (decl) = 1;
8d08fdba
MS
8838
8839 DECL_EXTERNAL (decl) = 1;
8840 if (quals != NULL_TREE && TREE_CODE (type) == FUNCTION_TYPE)
8841 {
8251199e 8842 cp_error ("%smember function `%D' cannot have `%T' method qualifier",
8d08fdba
MS
8843 (ctype ? "static " : "non-"), decl, TREE_VALUE (quals));
8844 quals = NULL_TREE;
8845 }
8846
596ea4e5 8847 if (IDENTIFIER_OPNAME_P (DECL_NAME (decl)))
8d08fdba
MS
8848 grok_op_properties (decl, virtualp, check < 0);
8849
4f1c5b7d 8850 if (ctype && decl_function_context (decl))
893de33c 8851 DECL_NO_STATIC_CHAIN (decl) = 1;
e76a2646 8852
42976354
BK
8853 for (t = TYPE_ARG_TYPES (TREE_TYPE (decl)); t; t = TREE_CHAIN (t))
8854 if (TREE_PURPOSE (t)
8855 && TREE_CODE (TREE_PURPOSE (t)) == DEFAULT_ARG)
8856 {
856216bb 8857 has_default_arg = 1;
42976354
BK
8858 break;
8859 }
8860
f9d94ea4
JM
8861 if (friendp
8862 && TREE_CODE (orig_declarator) == TEMPLATE_ID_EXPR)
8863 {
8864 if (funcdef_flag)
8251199e
JM
8865 cp_error
8866 ("defining explicit specialization `%D' in friend declaration",
f9d94ea4
JM
8867 orig_declarator);
8868 else
8869 {
76e57b45
NS
8870 tree fns = TREE_OPERAND (orig_declarator, 0);
8871 tree args = TREE_OPERAND (orig_declarator, 1);
d363e7bf 8872
7e2421f7
MM
8873 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
8874 {
8875 /* Something like `template <class T> friend void f<T>()'. */
cb9a3ff8 8876 cp_error ("invalid use of template-id `%D' in declaration of primary template",
7e2421f7 8877 orig_declarator);
20496fa2 8878 return NULL_TREE;
7e2421f7
MM
8879 }
8880
856216bb 8881
f9d94ea4
JM
8882 /* A friend declaration of the form friend void f<>(). Record
8883 the information in the TEMPLATE_ID_EXPR. */
8884 SET_DECL_IMPLICIT_INSTANTIATION (decl);
76e57b45
NS
8885
8886 if (TREE_CODE (fns) == COMPONENT_REF)
8887 {
8888 /* Due to bison parser ickiness, we will have already looked
8889 up an operator_name or PFUNCNAME within the current class
8890 (see template_id in parse.y). If the current class contains
8891 such a name, we'll get a COMPONENT_REF here. Undo that. */
d363e7bf 8892
76e57b45
NS
8893 my_friendly_assert (TREE_TYPE (TREE_OPERAND (fns, 0))
8894 == current_class_type, 20001120);
8895 fns = TREE_OPERAND (fns, 1);
8896 }
8897 my_friendly_assert (TREE_CODE (fns) == IDENTIFIER_NODE
8898 || TREE_CODE (fns) == LOOKUP_EXPR
8899 || TREE_CODE (fns) == OVERLOAD, 20001120);
8900 DECL_TEMPLATE_INFO (decl) = tree_cons (fns, args, NULL_TREE);
856216bb
MM
8901
8902 if (has_default_arg)
8903 {
8904 cp_error ("default arguments are not allowed in declaration of friend template specialization `%D'",
8905 decl);
8906 return NULL_TREE;
8907 }
8908
8909 if (inlinep)
8910 {
68642fb6 8911 cp_error ("`inline' is not allowed in declaration of friend template specialization `%D'",
856216bb
MM
8912 decl);
8913 return NULL_TREE;
8914 }
f9d94ea4 8915 }
f84b4be9 8916 }
386b8a85 8917
856216bb
MM
8918 if (has_default_arg)
8919 add_defarg_fn (decl);
8920
1eb0072d
JM
8921 if (funcdef_flag)
8922 /* Make the init_value nonzero so pushdecl knows this is not
8923 tentative. error_mark_node is replaced later with the BLOCK. */
8924 DECL_INITIAL (decl) = error_mark_node;
8925
93ca4ba7 8926 if (TYPE_NOTHROW_P (type) || nothrow_libfn_p (decl))
1660cb3a
JM
8927 TREE_NOTHROW (decl) = 1;
8928
75650646 8929 /* Caller will do the rest of this. */
8d08fdba
MS
8930 if (check < 0)
8931 return decl;
8932
8933 if (flags == NO_SPECIAL && ctype && constructor_name (cname) == declarator)
74b846e0
MM
8934 DECL_CONSTRUCTOR_P (decl) = 1;
8935
8936 /* Function gets the ugly name, field gets the nice one. This call
8937 may change the type of the function (because of default
8938 parameters)! */
8939 if (ctype != NULL_TREE)
8940 grokclassfn (ctype, decl, flags, quals);
8941
8942 decl = check_explicit_specialization (orig_declarator, decl,
8943 template_count,
8944 2 * (funcdef_flag != 0) +
8945 4 * (friendp != 0));
8946 if (decl == error_mark_node)
8947 return NULL_TREE;
98c1c668 8948
74b846e0
MM
8949 if (ctype != NULL_TREE
8950 && (! TYPE_FOR_JAVA (ctype) || check_java_method (decl))
8951 && check)
8d08fdba 8952 {
74b846e0 8953 tree old_decl;
8d08fdba 8954
74b846e0 8955 old_decl = check_classfn (ctype, decl);
8d08fdba 8956
74b846e0
MM
8957 if (old_decl && TREE_CODE (old_decl) == TEMPLATE_DECL)
8958 /* Because grokfndecl is always supposed to return a
8959 FUNCTION_DECL, we pull out the DECL_TEMPLATE_RESULT
8960 here. We depend on our callers to figure out that its
8961 really a template that's being returned. */
8962 old_decl = DECL_TEMPLATE_RESULT (old_decl);
6c30752f 8963
74b846e0
MM
8964 if (old_decl && DECL_STATIC_FUNCTION_P (old_decl)
8965 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE)
5566b478 8966 {
74b846e0
MM
8967 /* Remove the `this' parm added by grokclassfn.
8968 XXX Isn't this done in start_function, too? */
3afb32a4 8969 revert_static_member_fn (decl);
74b846e0 8970 last_function_parms = TREE_CHAIN (last_function_parms);
5566b478 8971 }
74b846e0
MM
8972 if (old_decl && DECL_ARTIFICIAL (old_decl))
8973 cp_error ("definition of implicitly-declared `%D'", old_decl);
8d08fdba 8974
74b846e0 8975 if (old_decl)
8d08fdba 8976 {
74b846e0
MM
8977 /* Since we've smashed OLD_DECL to its
8978 DECL_TEMPLATE_RESULT, we must do the same to DECL. */
8979 if (TREE_CODE (decl) == TEMPLATE_DECL)
8980 decl = DECL_TEMPLATE_RESULT (decl);
8981
8982 /* Attempt to merge the declarations. This can fail, in
8983 the case of some illegal specialization declarations. */
8984 if (!duplicate_decls (decl, old_decl))
8985 cp_error ("no `%#D' member function declared in class `%T'",
8986 decl, ctype);
8987 return old_decl;
8d08fdba
MS
8988 }
8989 }
74b846e0
MM
8990
8991 if (DECL_CONSTRUCTOR_P (decl) && !grok_ctor_properties (ctype, decl))
8992 return NULL_TREE;
8993
8994 if (ctype == NULL_TREE || check)
8995 return decl;
8996
8997 if (virtualp)
cbb40945 8998 DECL_VIRTUAL_P (decl) = 1;
74b846e0 8999
8d08fdba
MS
9000 return decl;
9001}
9002
9003static tree
2c73f9f5 9004grokvardecl (type, declarator, specbits_in, initialized, constp, in_namespace)
8d08fdba
MS
9005 tree type;
9006 tree declarator;
d2e5ee5c 9007 RID_BIT_TYPE *specbits_in;
8d08fdba 9008 int initialized;
a9aedbc2 9009 int constp;
2c73f9f5 9010 tree in_namespace;
8d08fdba
MS
9011{
9012 tree decl;
f7da6097
MS
9013 RID_BIT_TYPE specbits;
9014
9015 specbits = *specbits_in;
8d08fdba
MS
9016
9017 if (TREE_CODE (type) == OFFSET_TYPE)
9018 {
9019 /* If you declare a static member so that it
9020 can be initialized, the code will reach here. */
5b605f68
MS
9021 tree basetype = TYPE_OFFSET_BASETYPE (type);
9022 type = TREE_TYPE (type);
4ce3d537 9023 decl = build_lang_decl (VAR_DECL, declarator, type);
5b605f68 9024 DECL_CONTEXT (decl) = basetype;
8d08fdba
MS
9025 }
9026 else
30394414 9027 {
79c4d4b7
JM
9028 tree context;
9029
9030 if (in_namespace)
9031 context = in_namespace;
9032 else if (namespace_bindings_p () || RIDBIT_SETP (RID_EXTERN, specbits))
9033 context = current_namespace;
820fcad8
JM
9034 else
9035 context = NULL_TREE;
79c4d4b7 9036
6dfbb909
MM
9037 if (processing_template_decl && context)
9038 /* For global variables, declared in a template, we need the
9039 full lang_decl. */
cd9f6678 9040 decl = build_lang_decl (VAR_DECL, declarator, type);
9188c363 9041 else
c82dbd95 9042 decl = build_decl (VAR_DECL, declarator, type);
79c4d4b7
JM
9043
9044 if (context)
b262d64c 9045 set_decl_namespace (decl, context, 0);
79c4d4b7
JM
9046
9047 context = DECL_CONTEXT (decl);
5362b086 9048 if (declarator && context && current_lang_name != lang_name_c)
92643fea
MM
9049 /* We can't mangle lazily here because we don't have any
9050 way to recover whether or not a variable was `extern
9051 "C"' later. */
9052 mangle_decl (decl);
30394414 9053 }
6060a796 9054
2c73f9f5 9055 if (in_namespace)
b262d64c 9056 set_decl_namespace (decl, in_namespace, 0);
2c73f9f5 9057
8d08fdba
MS
9058 if (RIDBIT_SETP (RID_EXTERN, specbits))
9059 {
9060 DECL_THIS_EXTERN (decl) = 1;
9061 DECL_EXTERNAL (decl) = !initialized;
9062 }
9063
9064 /* In class context, static means one per class,
9065 public access, and static storage. */
2b9dc906 9066 if (DECL_CLASS_SCOPE_P (decl))
8d08fdba
MS
9067 {
9068 TREE_PUBLIC (decl) = 1;
9069 TREE_STATIC (decl) = 1;
5b605f68 9070 DECL_EXTERNAL (decl) = 0;
8d08fdba
MS
9071 }
9072 /* At top level, either `static' or no s.c. makes a definition
9073 (perhaps tentative), and absence of `static' makes it public. */
a9aedbc2 9074 else if (toplevel_bindings_p ())
8d08fdba 9075 {
a9aedbc2 9076 TREE_PUBLIC (decl) = (RIDBIT_NOTSETP (RID_STATIC, specbits)
9fffd093 9077 && (DECL_THIS_EXTERN (decl) || ! constp));
8d08fdba
MS
9078 TREE_STATIC (decl) = ! DECL_EXTERNAL (decl);
9079 }
9080 /* Not at top level, only `static' makes a static definition. */
9081 else
9082 {
9083 TREE_STATIC (decl) = !! RIDBIT_SETP (RID_STATIC, specbits);
9084 TREE_PUBLIC (decl) = DECL_EXTERNAL (decl);
9085 }
50a6dbd7
JM
9086
9087 if (TREE_PUBLIC (decl))
9088 {
9089 /* [basic.link]: A name with no linkage (notably, the name of a class
9090 or enumeration declared in a local scope) shall not be used to
9091 declare an entity with linkage.
9092
9093 Only check this for public decls for now. */
9094 tree t = no_linkage_check (TREE_TYPE (decl));
9095 if (t)
9096 {
9097 if (ANON_AGGRNAME_P (TYPE_IDENTIFIER (t)))
9098 /* Ignore for now; `enum { foo } e' is pretty common. */;
9099 else
8251199e 9100 cp_pedwarn ("non-local variable `%#D' uses local type `%T'",
50a6dbd7
JM
9101 decl, t);
9102 }
9103 }
9104
8d08fdba
MS
9105 return decl;
9106}
9107
d8f8dca1
MM
9108/* Create and return a canonical pointer to member function type, for
9109 TYPE, which is a POINTER_TYPE to a METHOD_TYPE. */
8d08fdba
MS
9110
9111tree
9112build_ptrmemfunc_type (type)
9113 tree type;
9114{
9115 tree fields[4];
9116 tree t;
46cbda4a 9117 tree unqualified_variant = NULL_TREE;
8d08fdba 9118
d48ebde1
NS
9119 if (type == error_mark_node)
9120 return type;
d363e7bf 9121
8d08fdba
MS
9122 /* If a canonical type already exists for this type, use it. We use
9123 this method instead of type_hash_canon, because it only does a
9124 simple equality check on the list of field members. */
9125
9126 if ((t = TYPE_GET_PTRMEMFUNC_TYPE (type)))
9127 return t;
9128
46cbda4a
MM
9129 /* Make sure that we always have the unqualified pointer-to-member
9130 type first. */
9131 if (CP_TYPE_QUALS (type) != TYPE_UNQUALIFIED)
68642fb6 9132 unqualified_variant
46cbda4a
MM
9133 = build_ptrmemfunc_type (TYPE_MAIN_VARIANT (type));
9134
33848bb0 9135 t = make_aggr_type (RECORD_TYPE);
2c73f9f5 9136 /* Let the front-end know this is a pointer to member function... */
db5ae43f 9137 TYPE_PTRMEMFUNC_FLAG (t) = 1;
2c73f9f5 9138 /* ... and not really an aggregate. */
7ddedda4 9139 SET_IS_AGGR_TYPE (t, 0);
8d08fdba 9140
1f84ec23
MM
9141 fields[0] = build_decl (FIELD_DECL, pfn_identifier, type);
9142 fields[1] = build_decl (FIELD_DECL, delta_identifier,
9143 delta_type_node);
9144 finish_builtin_type (t, "__ptrmemfunc_type", fields, 1, ptr_type_node);
8d08fdba 9145
8d08fdba
MS
9146 /* Zap out the name so that the back-end will give us the debugging
9147 information for this anonymous RECORD_TYPE. */
9148 TYPE_NAME (t) = NULL_TREE;
9149
46cbda4a
MM
9150 /* If this is not the unqualified form of this pointer-to-member
9151 type, set the TYPE_MAIN_VARIANT for this type to be the
9152 unqualified type. Since they are actually RECORD_TYPEs that are
9153 not variants of each other, we must do this manually. */
9154 if (CP_TYPE_QUALS (type) != TYPE_UNQUALIFIED)
9155 {
9156 t = build_qualified_type (t, CP_TYPE_QUALS (type));
9157 TYPE_MAIN_VARIANT (t) = unqualified_variant;
9158 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (unqualified_variant);
9159 TYPE_NEXT_VARIANT (unqualified_variant) = t;
9160 }
9161
9162 /* Cache this pointer-to-member type so that we can find it again
9163 later. */
8d08fdba
MS
9164 TYPE_SET_PTRMEMFUNC_TYPE (type, t);
9165
e92cc029 9166 /* Seems to be wanted. */
8d08fdba 9167 CLASSTYPE_GOT_SEMICOLON (t) = 1;
46cbda4a 9168
8d08fdba
MS
9169 return t;
9170}
9171
b17e2870
JM
9172/* DECL is a VAR_DECL defined in-class, whose TYPE is also given.
9173 Check to see that the definition is valid. Issue appropriate error
9174 messages. Return 1 if the definition is particularly bad, or 0
9175 otherwise. */
9176
9177int
9178check_static_variable_definition (decl, type)
9179 tree decl;
9180 tree type;
9181{
9182 /* Motion 10 at San Diego: If a static const integral data member is
9183 initialized with an integral constant expression, the initializer
9184 may appear either in the declaration (within the class), or in
9185 the definition, but not both. If it appears in the class, the
9186 member is a member constant. The file-scope definition is always
9187 required. */
9188 if (CLASS_TYPE_P (type) || TREE_CODE (type) == REFERENCE_TYPE)
9189 {
cb9a3ff8 9190 cp_error ("invalid in-class initialization of static data member of non-integral type `%T'",
b17e2870
JM
9191 type);
9192 /* If we just return the declaration, crashes will sometimes
9193 occur. We therefore return void_type_node, as if this was a
9194 friend declaration, to cause callers to completely ignore
9195 this declaration. */
9196 return 1;
9197 }
9198 else if (!CP_TYPE_CONST_P (type))
cb9a3ff8 9199 cp_error ("ISO C++ forbids in-class initialization of non-const static member `%D'",
b17e2870
JM
9200 decl);
9201 else if (pedantic && !INTEGRAL_TYPE_P (type))
cb9a3ff8 9202 cp_pedwarn ("ISO C++ forbids initialization of member constant `%D' of non-integral type `%T'", decl, type);
b17e2870
JM
9203
9204 return 0;
9205}
9206
2848ad0f
MM
9207/* Given the SIZE (i.e., number of elements) in an array, compute an
9208 appropriate index type for the array. If non-NULL, NAME is the
9209 name of the thing being declared. */
9210
c95cd22e 9211tree
2848ad0f
MM
9212compute_array_index_type (name, size)
9213 tree name;
9214 tree size;
9215{
9216 tree itype;
9217
9218 /* The size might be the result of a cast. */
9219 STRIP_TYPE_NOPS (size);
9220
9221 /* It might be a const variable or enumeration constant. */
fc611ce0 9222 size = decl_constant_value (size);
2848ad0f
MM
9223
9224 /* If this involves a template parameter, it will be a constant at
9225 instantiation time, but we don't know what the value is yet.
9226 Even if no template parameters are involved, we may an expression
9227 that is not a constant; we don't even simplify `1 + 2' when
9228 processing a template. */
9229 if (processing_template_decl)
9230 {
9231 /* Resolve a qualified reference to an enumerator or static
9232 const data member of ours. */
9233 if (TREE_CODE (size) == SCOPE_REF
9234 && TREE_OPERAND (size, 0) == current_class_type)
9235 {
9236 tree t = lookup_field (current_class_type,
9237 TREE_OPERAND (size, 1), 0, 0);
9238 if (t)
9239 size = t;
9240 }
9241
9242 return build_index_type (build_min (MINUS_EXPR, sizetype,
9243 size, integer_one_node));
9244 }
9245
9246 /* The array bound must be an integer type. */
9247 if (TREE_CODE (TREE_TYPE (size)) != INTEGER_TYPE
9248 && TREE_CODE (TREE_TYPE (size)) != ENUMERAL_TYPE
9249 && TREE_CODE (TREE_TYPE (size)) != BOOLEAN_TYPE)
9250 {
d67cdbc3
JM
9251 if (name)
9252 cp_error ("size of array `%D' has non-integer type", name);
9253 else
9254 cp_error ("size of array has non-integer type");
2848ad0f
MM
9255 size = integer_one_node;
9256 }
9257
9258 /* Normally, the array-bound will be a constant. */
2bb5d995 9259 if (TREE_CODE (size) == INTEGER_CST)
2848ad0f
MM
9260 {
9261 /* Check to see if the array bound overflowed. Make that an
9262 error, no matter how generous we're being. */
9263 int old_flag_pedantic_errors = flag_pedantic_errors;
9264 int old_pedantic = pedantic;
9265 pedantic = flag_pedantic_errors = 1;
9266 constant_expression_warning (size);
9267 pedantic = old_pedantic;
9268 flag_pedantic_errors = old_flag_pedantic_errors;
9269
9270 /* An array must have a positive number of elements. */
9271 if (INT_CST_LT (size, integer_zero_node))
9272 {
d67cdbc3
JM
9273 if (name)
9274 cp_error ("size of array `%D' is negative", name);
9275 else
9276 cp_error ("size of array is negative");
2848ad0f
MM
9277 size = integer_one_node;
9278 }
9279 /* Except that an extension we allow zero-sized arrays. We
68642fb6 9280 always allow them in system headers because glibc uses
2848ad0f
MM
9281 them. */
9282 else if (integer_zerop (size) && pedantic && !in_system_header)
d67cdbc3
JM
9283 {
9284 if (name)
cb9a3ff8 9285 cp_pedwarn ("ISO C++ forbids zero-size array `%D'", name);
d67cdbc3 9286 else
cb9a3ff8 9287 cp_pedwarn ("ISO C++ forbids zero-size array");
d67cdbc3 9288 }
2848ad0f 9289 }
2bb5d995
JM
9290 else if (TREE_CONSTANT (size))
9291 {
9292 /* `(int) &fn' is not a valid array bound. */
9293 if (name)
9294 cp_error ("size of array `%D' is not an integral constant-expression",
9295 name);
9296 else
9297 cp_error ("size of array is not an integral constant-expression");
9298 }
2848ad0f
MM
9299
9300 /* Compute the index of the largest element in the array. It is
9301 one less than the number of elements in the array. */
9302 itype
ab76ca54
MM
9303 = fold (cp_build_binary_op (MINUS_EXPR,
9304 cp_convert (ssizetype, size),
9305 cp_convert (ssizetype,
9306 integer_one_node)));
68642fb6 9307
2848ad0f
MM
9308 /* Check for variable-sized arrays. We allow such things as an
9309 extension, even though they are not allowed in ANSI/ISO C++. */
9310 if (!TREE_CONSTANT (itype))
9311 {
9312 if (pedantic)
9313 {
9314 if (name)
cb9a3ff8 9315 cp_pedwarn ("ISO C++ forbids variable-size array `%D'",
2848ad0f
MM
9316 name);
9317 else
cb9a3ff8 9318 cp_pedwarn ("ISO C++ forbids variable-size array");
2848ad0f
MM
9319 }
9320
9321 /* Create a variable-sized array index type. */
9322 itype = variable_size (itype);
9323 }
9324 /* Make sure that there was no overflow when creating to a signed
9325 index type. (For example, on a 32-bit machine, an array with
9326 size 2^32 - 1 is too big.) */
9327 else if (TREE_OVERFLOW (itype))
9328 {
9329 error ("overflow in array dimension");
9330 TREE_OVERFLOW (itype) = 0;
9331 }
68642fb6 9332
2848ad0f
MM
9333 /* Create and return the appropriate index type. */
9334 return build_index_type (itype);
9335}
9336
9337/* Returns an ARRAY_TYPE for an array with SIZE elements of the
9338 indicated TYPE. If non-NULL, NAME is the NAME of the declaration
9339 with this type. */
9340
9341static tree
9342create_array_type_for_decl (name, type, size)
9343 tree name;
9344 tree type;
9345 tree size;
9346{
9347 tree itype = NULL_TREE;
9348 const char* error_msg;
9349
9350 /* If things have already gone awry, bail now. */
9351 if (type == error_mark_node || size == error_mark_node)
9352 return error_mark_node;
9353
9354 /* Assume that everything will go OK. */
9355 error_msg = NULL;
9356
9357 /* There are some types which cannot be array elements. */
9358 switch (TREE_CODE (type))
9359 {
9360 case VOID_TYPE:
9361 error_msg = "array of void";
9362 break;
9363
9364 case FUNCTION_TYPE:
9365 error_msg = "array of functions";
9366 break;
9367
9368 case REFERENCE_TYPE:
9369 error_msg = "array of references";
9370 break;
9371
9372 case OFFSET_TYPE:
9373 error_msg = "array of data members";
9374 break;
9375
9376 case METHOD_TYPE:
9377 error_msg = "array of function members";
9378 break;
9379
9380 default:
9381 break;
9382 }
9383
9384 /* If something went wrong, issue an error-message and return. */
9385 if (error_msg)
9386 {
9387 if (name)
9388 cp_error ("declaration of `%D' as %s", name, error_msg);
9389 else
9390 cp_error ("creating %s", error_msg);
9391
9392 return error_mark_node;
9393 }
9394
9395 /* [dcl.array]
68642fb6 9396
2848ad0f
MM
9397 The constant expressions that specify the bounds of the arrays
9398 can be omitted only for the first member of the sequence. */
9399 if (TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type))
9400 {
cb9a3ff8 9401 cp_error ("declaration of `%D' as multidimensional array must have bounds for all dimensions except the first",
2848ad0f 9402 name);
2848ad0f
MM
9403
9404 return error_mark_node;
9405 }
9406
9407 /* Figure out the index type for the array. */
9408 if (size)
9409 itype = compute_array_index_type (name, size);
9410
9411 return build_cplus_array_type (type, itype);
9412}
9413
3dbc07b6
MM
9414/* Check that it's OK to declare a function with the indicated TYPE.
9415 SFK indicates the kind of special function (if any) that this
1f84ec23 9416 function is. OPTYPE is the type given in a conversion operator
3dbc07b6
MM
9417 declaration. Returns the actual return type of the function; that
9418 may be different than TYPE if an error occurs, or for certain
9419 special functions. */
9420
9421static tree
1f84ec23 9422check_special_function_return_type (sfk, type, optype)
3dbc07b6
MM
9423 special_function_kind sfk;
9424 tree type;
3dbc07b6
MM
9425 tree optype;
9426{
9427 switch (sfk)
9428 {
9429 case sfk_constructor:
9430 if (type)
9431 cp_error ("return type specification for constructor invalid");
5362b086 9432
1f84ec23
MM
9433 /* In the new ABI constructors do not return a value. */
9434 type = void_type_node;
3dbc07b6
MM
9435 break;
9436
9437 case sfk_destructor:
9438 if (type)
9439 cp_error ("return type specification for destructor invalid");
9440 type = void_type_node;
9441 break;
9442
9443 case sfk_conversion:
9444 if (type && !same_type_p (type, optype))
9445 cp_error ("operator `%T' declared to return `%T'", optype, type);
9446 else if (type)
9447 cp_pedwarn ("return type specified for `operator %T'", optype);
9448 type = optype;
9449 break;
9450
9451 default:
9452 my_friendly_abort (20000408);
9453 break;
9454 }
9455
9456 return type;
9457}
9458
8d08fdba
MS
9459/* Given declspecs and a declarator,
9460 determine the name and type of the object declared
9461 and construct a ..._DECL node for it.
9462 (In one case we can return a ..._TYPE node instead.
9463 For invalid input we sometimes return 0.)
9464
9465 DECLSPECS is a chain of tree_list nodes whose value fields
9466 are the storage classes and type specifiers.
9467
9468 DECL_CONTEXT says which syntactic context this declaration is in:
9469 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
9470 FUNCDEF for a function definition. Like NORMAL but a few different
9471 error messages in each case. Return value may be zero meaning
9472 this definition is too screwy to try to parse.
9473 MEMFUNCDEF for a function definition. Like FUNCDEF but prepares to
9474 handle member functions (which have FIELD context).
9475 Return value may be zero meaning this definition is too screwy to
9476 try to parse.
9477 PARM for a parameter declaration (either within a function prototype
9478 or before a function body). Make a PARM_DECL, or return void_type_node.
db5ae43f 9479 CATCHPARM for a parameter declaration before a catch clause.
8d08fdba
MS
9480 TYPENAME if for a typename (in a cast or sizeof).
9481 Don't make a DECL node; just return the ..._TYPE node.
9482 FIELD for a struct or union field; make a FIELD_DECL.
9483 BITFIELD for a field with specified width.
9484 INITIALIZED is 1 if the decl has an initializer.
9485
b17e2870
JM
9486 ATTRLIST is a TREE_LIST node with prefix attributes in TREE_VALUE and
9487 normal attributes in TREE_PURPOSE, or NULL_TREE.
9488
70adf8a9 9489 In the TYPENAME case, DECLARATOR is really an abstract declarator.
8d08fdba
MS
9490 It may also be so in the PARM case, for a prototype where the
9491 argument type is specified but not the name.
9492
9493 This function is where the complicated C meanings of `static'
9494 and `extern' are interpreted.
9495
9496 For C++, if there is any monkey business to do, the function which
9497 calls this one must do it, i.e., prepending instance variables,
9498 renaming overloaded function names, etc.
9499
9500 Note that for this C++, it is an error to define a method within a class
9501 which does not belong to that class.
9502
9503 Except in the case where SCOPE_REFs are implicitly known (such as
9504 methods within a class being redundantly qualified),
9505 declarations which involve SCOPE_REFs are returned as SCOPE_REFs
9506 (class_name::decl_name). The caller must also deal with this.
9507
9508 If a constructor or destructor is seen, and the context is FIELD,
9509 then the type gains the attribute TREE_HAS_x. If such a declaration
9510 is erroneous, NULL_TREE is returned.
9511
9512 QUALS is used only for FUNCDEF and MEMFUNCDEF cases. For a member
535233a8
NS
9513 function, these are the qualifiers to give to the `this' pointer. We
9514 apply TYPE_QUAL_RESTRICT to the this ptr, not the object.
8d08fdba
MS
9515
9516 May return void_type_node if the declarator turned out to be a friend.
9517 See grokfield for details. */
9518
8d08fdba 9519tree
c11b6f21 9520grokdeclarator (declarator, declspecs, decl_context, initialized, attrlist)
8d08fdba
MS
9521 tree declspecs;
9522 tree declarator;
9523 enum decl_context decl_context;
9524 int initialized;
c11b6f21 9525 tree attrlist;
8d08fdba
MS
9526{
9527 RID_BIT_TYPE specbits;
9528 int nclasses = 0;
9529 tree spec;
9530 tree type = NULL_TREE;
9531 int longlong = 0;
9532 int constp;
91063b51 9533 int restrictp;
8d08fdba 9534 int volatilep;
91063b51 9535 int type_quals;
db5ae43f 9536 int virtualp, explicitp, friendp, inlinep, staticp;
8d08fdba
MS
9537 int explicit_int = 0;
9538 int explicit_char = 0;
37c46b43 9539 int defaulted_int = 0;
8d08fdba 9540 tree typedef_decl = NULL_TREE;
9c0758dd 9541 const char *name;
8d08fdba
MS
9542 tree typedef_type = NULL_TREE;
9543 int funcdef_flag = 0;
9544 enum tree_code innermost_code = ERROR_MARK;
9545 int bitfield = 0;
6125f3be
DE
9546#if 0
9547 /* See the code below that used this. */
f6abb50a 9548 tree decl_machine_attr = NULL_TREE;
6125f3be 9549#endif
8d08fdba
MS
9550 /* Set this to error_mark_node for FIELD_DECLs we could not handle properly.
9551 All FIELD_DECLs we build here have `init' put into their DECL_INITIAL. */
9552 tree init = NULL_TREE;
9553
9554 /* Keep track of what sort of function is being processed
9555 so that we can warn about default return values, or explicit
9556 return values which do not match prescribed defaults. */
3dbc07b6 9557 special_function_kind sfk = sfk_none;
8d08fdba
MS
9558
9559 tree dname = NULL_TREE;
9560 tree ctype = current_class_type;
9561 tree ctor_return_type = NULL_TREE;
9562 enum overload_flags flags = NO_SPECIAL;
8d08fdba 9563 tree quals = NULL_TREE;
c11b6f21 9564 tree raises = NULL_TREE;
386b8a85 9565 int template_count = 0;
2c73f9f5 9566 tree in_namespace = NULL_TREE;
1eab9b56
JM
9567 tree inner_attrs;
9568 int ignore_attrs;
8d08fdba
MS
9569
9570 RIDBIT_RESET_ALL (specbits);
9571 if (decl_context == FUNCDEF)
9572 funcdef_flag = 1, decl_context = NORMAL;
9573 else if (decl_context == MEMFUNCDEF)
9574 funcdef_flag = -1, decl_context = FIELD;
9575 else if (decl_context == BITFIELD)
9576 bitfield = 1, decl_context = FIELD;
9577
8d08fdba
MS
9578 /* Look inside a declarator for the name being declared
9579 and get it as a string, for an error message. */
9580 {
be99da77
MS
9581 tree *next = &declarator;
9582 register tree decl;
8d08fdba
MS
9583 name = NULL;
9584
be99da77
MS
9585 while (next && *next)
9586 {
9587 decl = *next;
9588 switch (TREE_CODE (decl))
8d08fdba 9589 {
52fbc847
JM
9590 case TREE_LIST:
9591 /* For attributes. */
9592 next = &TREE_VALUE (decl);
9593 break;
9594
be99da77
MS
9595 case COND_EXPR:
9596 ctype = NULL_TREE;
9597 next = &TREE_OPERAND (decl, 0);
9598 break;
8d08fdba 9599
2c73f9f5 9600 case BIT_NOT_EXPR: /* For C++ destructors! */
8d08fdba 9601 {
be99da77
MS
9602 tree name = TREE_OPERAND (decl, 0);
9603 tree rename = NULL_TREE;
9604
9605 my_friendly_assert (flags == NO_SPECIAL, 152);
9606 flags = DTOR_FLAG;
3dbc07b6 9607 sfk = sfk_destructor;
5566b478
MS
9608 if (TREE_CODE (name) == TYPE_DECL)
9609 TREE_OPERAND (decl, 0) = name = constructor_name (name);
be99da77
MS
9610 my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, 153);
9611 if (ctype == NULL_TREE)
9612 {
9613 if (current_class_type == NULL_TREE)
9614 {
8251199e 9615 error ("destructors must be member functions");
be99da77
MS
9616 flags = NO_SPECIAL;
9617 }
9618 else
9619 {
9620 tree t = constructor_name (current_class_name);
9621 if (t != name)
9622 rename = t;
9623 }
9624 }
8d08fdba 9625 else
be99da77
MS
9626 {
9627 tree t = constructor_name (ctype);
9628 if (t != name)
9629 rename = t;
9630 }
51c184be 9631
be99da77 9632 if (rename)
39211cd5 9633 {
8251199e 9634 cp_error ("destructor `%T' must match class name `%T'",
5566b478 9635 name, rename);
be99da77 9636 TREE_OPERAND (decl, 0) = rename;
39211cd5 9637 }
be99da77 9638 next = &name;
51c184be 9639 }
be99da77 9640 break;
8d08fdba 9641
be99da77 9642 case ADDR_EXPR: /* C++ reference declaration */
2c73f9f5 9643 /* Fall through. */
be99da77
MS
9644 case ARRAY_REF:
9645 case INDIRECT_REF:
9646 ctype = NULL_TREE;
9647 innermost_code = TREE_CODE (decl);
9648 next = &TREE_OPERAND (decl, 0);
9649 break;
8d08fdba 9650
be99da77 9651 case CALL_EXPR:
43f887f9 9652 if (parmlist_is_exprlist (CALL_DECLARATOR_PARMS (decl)))
8d08fdba 9653 {
b17e2870
JM
9654 /* This is actually a variable declaration using
9655 constructor syntax. We need to call start_decl and
9656 cp_finish_decl so we can get the variable
9657 initialized... */
9658
9659 tree attributes, prefix_attributes;
be99da77
MS
9660
9661 *next = TREE_OPERAND (decl, 0);
43f887f9 9662 init = CALL_DECLARATOR_PARMS (decl);
be99da77 9663
b17e2870
JM
9664 if (attrlist)
9665 {
9666 attributes = TREE_PURPOSE (attrlist);
9667 prefix_attributes = TREE_VALUE (attrlist);
9668 }
9669 else
9670 {
9671 attributes = NULL_TREE;
9672 prefix_attributes = NULL_TREE;
9673 }
9674
9675 decl = start_decl (declarator, declspecs, 1,
9676 attributes, prefix_attributes);
9fc336c7 9677 decl_type_access_control (decl);
1eb0072d
JM
9678 if (decl)
9679 {
9680 /* Look for __unused__ attribute */
9681 if (TREE_USED (TREE_TYPE (decl)))
9682 TREE_USED (decl) = 1;
9683 finish_decl (decl, init, NULL_TREE);
9684 }
9685 else
9686 cp_error ("invalid declarator");
be99da77 9687 return 0;
8d08fdba 9688 }
be99da77
MS
9689 innermost_code = TREE_CODE (decl);
9690 if (decl_context == FIELD && ctype == NULL_TREE)
9691 ctype = current_class_type;
45537677 9692 if (ctype
c11b6f21 9693 && TREE_OPERAND (decl, 0)
45537677
MS
9694 && (TREE_CODE (TREE_OPERAND (decl, 0)) == TYPE_DECL
9695 && ((DECL_NAME (TREE_OPERAND (decl, 0))
9696 == constructor_name_full (ctype))
9697 || (DECL_NAME (TREE_OPERAND (decl, 0))
9698 == constructor_name (ctype)))))
be99da77
MS
9699 TREE_OPERAND (decl, 0) = constructor_name (ctype);
9700 next = &TREE_OPERAND (decl, 0);
9701 decl = *next;
9702 if (ctype != NULL_TREE
9703 && decl != NULL_TREE && flags != DTOR_FLAG
9704 && decl == constructor_name (ctype))
8d08fdba 9705 {
3dbc07b6 9706 sfk = sfk_constructor;
be99da77 9707 ctor_return_type = ctype;
8d08fdba 9708 }
be99da77
MS
9709 ctype = NULL_TREE;
9710 break;
68642fb6 9711
386b8a85
JM
9712 case TEMPLATE_ID_EXPR:
9713 {
9714 tree fns = TREE_OPERAND (decl, 0);
9715
9716 if (TREE_CODE (fns) == LOOKUP_EXPR)
9717 fns = TREE_OPERAND (fns, 0);
9718
8f032717
MM
9719 dname = fns;
9720 if (TREE_CODE (dname) == COMPONENT_REF)
9721 dname = TREE_OPERAND (dname, 1);
9722 if (TREE_CODE (dname) != IDENTIFIER_NODE)
9723 {
9724 my_friendly_assert (is_overloaded_fn (dname),
9725 19990331);
9726 dname = DECL_NAME (get_first_fn (dname));
9727 }
386b8a85 9728 }
2c73f9f5 9729 /* Fall through. */
be99da77
MS
9730
9731 case IDENTIFIER_NODE:
386b8a85
JM
9732 if (TREE_CODE (decl) == IDENTIFIER_NODE)
9733 dname = decl;
9734
be99da77
MS
9735 next = 0;
9736
0e5921e8 9737 if (C_IS_RESERVED_WORD (dname))
8d08fdba 9738 {
8251199e 9739 cp_error ("declarator-id missing; using reserved word `%D'",
be99da77
MS
9740 dname);
9741 name = IDENTIFIER_POINTER (dname);
8d08fdba 9742 }
596ea4e5 9743 else if (!IDENTIFIER_TYPENAME_P (dname))
be99da77 9744 name = IDENTIFIER_POINTER (dname);
8d08fdba
MS
9745 else
9746 {
596ea4e5
AS
9747 my_friendly_assert (flags == NO_SPECIAL, 154);
9748 flags = TYPENAME_FLAG;
9749 ctor_return_type = TREE_TYPE (dname);
9750 sfk = sfk_conversion;
9751 if (IDENTIFIER_GLOBAL_VALUE (dname)
5362b086 9752 && (TREE_CODE (IDENTIFIER_GLOBAL_VALUE (dname))
596ea4e5
AS
9753 == TYPE_DECL))
9754 name = IDENTIFIER_POINTER (dname);
9755 else
9756 name = "<invalid operator>";
8d08fdba 9757 }
be99da77 9758 break;
8d08fdba 9759
be99da77
MS
9760 /* C++ extension */
9761 case SCOPE_REF:
9762 {
9763 /* Perform error checking, and decide on a ctype. */
9764 tree cname = TREE_OPERAND (decl, 0);
9765 if (cname == NULL_TREE)
9766 ctype = NULL_TREE;
2c73f9f5
ML
9767 else if (TREE_CODE (cname) == NAMESPACE_DECL)
9768 {
9769 ctype = NULL_TREE;
9770 in_namespace = TREE_OPERAND (decl, 0);
9771 TREE_OPERAND (decl, 0) = NULL_TREE;
9772 }
be99da77
MS
9773 else if (! is_aggr_type (cname, 1))
9774 TREE_OPERAND (decl, 0) = NULL_TREE;
9775 /* Must test TREE_OPERAND (decl, 1), in case user gives
9776 us `typedef (class::memfunc)(int); memfunc *memfuncptr;' */
9777 else if (TREE_OPERAND (decl, 1)
9778 && TREE_CODE (TREE_OPERAND (decl, 1)) == INDIRECT_REF)
9779 ctype = cname;
73b0fce8 9780 else if (TREE_CODE (cname) == TEMPLATE_TYPE_PARM
a1281f45 9781 || TREE_CODE (cname) == BOUND_TEMPLATE_TEMPLATE_PARM)
5566b478 9782 {
8251199e 9783 cp_error ("`%T::%D' is not a valid declarator", cname,
5566b478 9784 TREE_OPERAND (decl, 1));
8251199e 9785 cp_error (" perhaps you want `typename %T::%D' to make it a type",
5566b478
MS
9786 cname, TREE_OPERAND (decl, 1));
9787 return void_type_node;
9788 }
be99da77
MS
9789 else if (ctype == NULL_TREE)
9790 ctype = cname;
9791 else if (TREE_COMPLEXITY (decl) == current_class_depth)
9792 TREE_OPERAND (decl, 0) = ctype;
9793 else
9794 {
9795 if (! UNIQUELY_DERIVED_FROM_P (cname, ctype))
9796 {
8251199e 9797 cp_error ("type `%T' is not derived from type `%T'",
be99da77
MS
9798 cname, ctype);
9799 TREE_OPERAND (decl, 0) = NULL_TREE;
9800 }
9801 else
9802 ctype = cname;
9803 }
9804
c91a56d2
MS
9805 if (ctype && TREE_CODE (TREE_OPERAND (decl, 1)) == TYPE_DECL
9806 && ((DECL_NAME (TREE_OPERAND (decl, 1))
9807 == constructor_name_full (ctype))
9808 || (DECL_NAME (TREE_OPERAND (decl, 1))
9809 == constructor_name (ctype))))
be99da77
MS
9810 TREE_OPERAND (decl, 1) = constructor_name (ctype);
9811 next = &TREE_OPERAND (decl, 1);
9812 decl = *next;
9813 if (ctype)
9814 {
9815 if (TREE_CODE (decl) == IDENTIFIER_NODE
9816 && constructor_name (ctype) == decl)
9817 {
3dbc07b6 9818 sfk = sfk_constructor;
be99da77
MS
9819 ctor_return_type = ctype;
9820 }
9821 else if (TREE_CODE (decl) == BIT_NOT_EXPR
9822 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
9823 && (constructor_name (ctype) == TREE_OPERAND (decl, 0)
9824 || constructor_name_full (ctype) == TREE_OPERAND (decl, 0)))
9825 {
3dbc07b6 9826 sfk = sfk_destructor;
be99da77
MS
9827 ctor_return_type = ctype;
9828 flags = DTOR_FLAG;
9829 TREE_OPERAND (decl, 0) = constructor_name (ctype);
9830 next = &TREE_OPERAND (decl, 0);
9831 }
9832 }
9833 }
9834 break;
9835
9836 case ERROR_MARK:
9837 next = 0;
9838 break;
9839
45537677
MS
9840 case TYPE_DECL:
9841 /* Parse error puts this typespec where
9842 a declarator should go. */
8251199e 9843 cp_error ("`%T' specified as declarator-id", DECL_NAME (decl));
45537677 9844 if (TREE_TYPE (decl) == current_class_type)
8251199e 9845 cp_error (" perhaps you want `%T' for a constructor",
45537677
MS
9846 current_class_name);
9847 dname = DECL_NAME (decl);
9848 name = IDENTIFIER_POINTER (dname);
9849
e92cc029 9850 /* Avoid giving two errors for this. */
45537677
MS
9851 IDENTIFIER_CLASS_VALUE (dname) = NULL_TREE;
9852
58010b57 9853 declspecs = tree_cons (NULL_TREE, integer_type_node, declspecs);
45537677
MS
9854 *next = dname;
9855 next = 0;
9856 break;
9857
be99da77 9858 default:
8251199e 9859 cp_compiler_error ("`%D' as declarator", decl);
be99da77 9860 return 0; /* We used to do a 155 abort here. */
8d08fdba 9861 }
be99da77 9862 }
8d08fdba
MS
9863 }
9864
9865 /* A function definition's declarator must have the form of
9866 a function declarator. */
9867
9868 if (funcdef_flag && innermost_code != CALL_EXPR)
9869 return 0;
9870
e1cd6e56
MS
9871 if (((dname && IDENTIFIER_OPNAME_P (dname)) || flags == TYPENAME_FLAG)
9872 && innermost_code != CALL_EXPR
9873 && ! (ctype && declspecs == NULL_TREE))
9874 {
8251199e 9875 cp_error ("declaration of `%D' as non-function", dname);
e1cd6e56
MS
9876 return void_type_node;
9877 }
9878
8d08fdba
MS
9879 /* Anything declared one level down from the top level
9880 must be one of the parameters of a function
9881 (because the body is at least two levels down). */
9882
9883 /* This heuristic cannot be applied to C++ nodes! Fixed, however,
9884 by not allowing C++ class definitions to specify their parameters
9885 with xdecls (must be spec.d in the parmlist).
9886
9887 Since we now wait to push a class scope until we are sure that
9888 we are in a legitimate method context, we must set oldcname
a9aedbc2
MS
9889 explicitly (since current_class_name is not yet alive).
9890
9891 We also want to avoid calling this a PARM if it is in a namespace. */
8d08fdba 9892
9188c363 9893 if (decl_context == NORMAL && !toplevel_bindings_p ())
a9aedbc2
MS
9894 {
9895 struct binding_level *b = current_binding_level;
9896 current_binding_level = b->level_chain;
9897 if (current_binding_level != 0 && toplevel_bindings_p ())
9898 decl_context = PARM;
9899 current_binding_level = b;
9900 }
8d08fdba 9901
34fc957d
NS
9902 if (name == NULL)
9903 name = decl_context == PARM ? "parameter" : "type name";
68642fb6 9904
8d08fdba
MS
9905 /* Look through the decl specs and record which ones appear.
9906 Some typespecs are defined as built-in typenames.
9907 Others, the ones that are modifiers of other types,
9908 are represented by bits in SPECBITS: set the bits for
9909 the modifiers that appear. Storage class keywords are also in SPECBITS.
9910
9911 If there is a typedef name or a type, store the type in TYPE.
9912 This includes builtin typedefs such as `int'.
9913
9914 Set EXPLICIT_INT if the type is `int' or `char' and did not
9915 come from a user typedef.
9916
9917 Set LONGLONG if `long' is mentioned twice.
9918
9919 For C++, constructors and destructors have their own fast treatment. */
9920
9921 for (spec = declspecs; spec; spec = TREE_CHAIN (spec))
9922 {
9923 register int i;
9924 register tree id;
9925
9926 /* Certain parse errors slip through. For example,
9927 `int class;' is not caught by the parser. Try
9928 weakly to recover here. */
9929 if (TREE_CODE (spec) != TREE_LIST)
9930 return 0;
9931
9932 id = TREE_VALUE (spec);
9933
9934 if (TREE_CODE (id) == IDENTIFIER_NODE)
9935 {
a3203465
MS
9936 if (id == ridpointers[(int) RID_INT]
9937 || id == ridpointers[(int) RID_CHAR]
9938 || id == ridpointers[(int) RID_BOOL]
9939 || id == ridpointers[(int) RID_WCHAR])
8d08fdba
MS
9940 {
9941 if (type)
8ccc31eb
MS
9942 {
9943 if (id == ridpointers[(int) RID_BOOL])
8251199e 9944 error ("`bool' is now a keyword");
8ccc31eb 9945 else
8251199e 9946 cp_error ("extraneous `%T' ignored", id);
8ccc31eb 9947 }
8d08fdba
MS
9948 else
9949 {
a3203465
MS
9950 if (id == ridpointers[(int) RID_INT])
9951 explicit_int = 1;
9952 else if (id == ridpointers[(int) RID_CHAR])
9953 explicit_char = 1;
8d08fdba
MS
9954 type = TREE_TYPE (IDENTIFIER_GLOBAL_VALUE (id));
9955 }
9956 goto found;
9957 }
e92cc029 9958 /* C++ aggregate types. */
8d08fdba
MS
9959 if (IDENTIFIER_HAS_TYPE_VALUE (id))
9960 {
9961 if (type)
8251199e 9962 cp_error ("multiple declarations `%T' and `%T'", type, id);
8d08fdba
MS
9963 else
9964 type = IDENTIFIER_TYPE_VALUE (id);
9965 goto found;
9966 }
9967
f376e137 9968 for (i = (int) RID_FIRST_MODIFIER; i <= (int) RID_LAST_MODIFIER; i++)
8d08fdba
MS
9969 {
9970 if (ridpointers[i] == id)
9971 {
9972 if (i == (int) RID_LONG && RIDBIT_SETP (i, specbits))
9973 {
795add94 9974 if (pedantic && ! in_system_header && warn_long_long)
cb9a3ff8 9975 pedwarn ("ISO C++ does not support `long long'");
9a3b49ac 9976 if (longlong)
8251199e 9977 error ("`long long long' is too long for GCC");
8d08fdba
MS
9978 else
9979 longlong = 1;
9980 }
9981 else if (RIDBIT_SETP (i, specbits))
8251199e 9982 pedwarn ("duplicate `%s'", IDENTIFIER_POINTER (id));
8d08fdba
MS
9983 RIDBIT_SET (i, specbits);
9984 goto found;
9985 }
9986 }
9987 }
e92cc029 9988 /* C++ aggregate types. */
1899c3a4 9989 else if (TREE_CODE (id) == TYPE_DECL)
45537677
MS
9990 {
9991 if (type)
8251199e 9992 cp_error ("multiple declarations `%T' and `%T'", type,
45537677
MS
9993 TREE_TYPE (id));
9994 else
5566b478
MS
9995 {
9996 type = TREE_TYPE (id);
9997 TREE_VALUE (spec) = type;
9998 }
45537677
MS
9999 goto found;
10000 }
8d08fdba 10001 if (type)
8251199e 10002 error ("two or more data types in declaration of `%s'", name);
8d08fdba
MS
10003 else if (TREE_CODE (id) == IDENTIFIER_NODE)
10004 {
10005 register tree t = lookup_name (id, 1);
10006 if (!t || TREE_CODE (t) != TYPE_DECL)
8251199e 10007 error ("`%s' fails to be a typedef or built in type",
8d08fdba
MS
10008 IDENTIFIER_POINTER (id));
10009 else
10010 {
10011 type = TREE_TYPE (t);
6125f3be
DE
10012#if 0
10013 /* See the code below that used this. */
f6abb50a 10014 decl_machine_attr = DECL_MACHINE_ATTRIBUTES (id);
6125f3be 10015#endif
8d08fdba
MS
10016 typedef_decl = t;
10017 }
10018 }
bd6dd845 10019 else if (id != error_mark_node)
8d08fdba
MS
10020 /* Can't change CLASS nodes into RECORD nodes here! */
10021 type = id;
10022
10023 found: ;
10024 }
10025
10026 typedef_type = type;
10027
37c46b43 10028 /* No type at all: default to `int', and set DEFAULTED_INT
6eabb241 10029 because it was not a user-defined typedef. */
8d08fdba 10030
a3203465
MS
10031 if (type == NULL_TREE
10032 && (RIDBIT_SETP (RID_SIGNED, specbits)
10033 || RIDBIT_SETP (RID_UNSIGNED, specbits)
10034 || RIDBIT_SETP (RID_LONG, specbits)
10035 || RIDBIT_SETP (RID_SHORT, specbits)))
10036 {
10037 /* These imply 'int'. */
10038 type = integer_type_node;
37c46b43 10039 defaulted_int = 1;
a3203465
MS
10040 }
10041
3dbc07b6
MM
10042 if (sfk != sfk_none)
10043 type = check_special_function_return_type (sfk, type,
3dbc07b6
MM
10044 ctor_return_type);
10045 else if (type == NULL_TREE)
8d08fdba 10046 {
3dbc07b6
MM
10047 int is_main;
10048
8d08fdba 10049 explicit_int = -1;
3dbc07b6 10050
41eff652
JM
10051 /* We handle `main' specially here, because 'main () { }' is so
10052 common. With no options, it is allowed. With -Wreturn-type,
10053 it is a warning. It is only an error with -pedantic-errors. */
3dbc07b6
MM
10054 is_main = (funcdef_flag
10055 && MAIN_NAME_P (dname)
10056 && ctype == NULL_TREE
10057 && in_namespace == NULL_TREE
10058 && current_namespace == global_namespace);
10059
10060 if (in_system_header || flag_ms_extensions)
10061 /* Allow it, sigh. */;
10062 else if (pedantic || ! is_main)
10063 cp_pedwarn ("ISO C++ forbids declaration of `%s' with no type",
10064 name);
10065 else if (warn_return_type)
10066 cp_warning ("ISO C++ forbids declaration of `%s' with no type",
10067 name);
41eff652 10068
3dbc07b6 10069 type = integer_type_node;
51c184be 10070 }
8d08fdba
MS
10071
10072 ctype = NULL_TREE;
10073
10074 /* Now process the modifiers that were specified
10075 and check for invalid combinations. */
10076
10077 /* Long double is a special combination. */
10078
10079 if (RIDBIT_SETP (RID_LONG, specbits)
10080 && TYPE_MAIN_VARIANT (type) == double_type_node)
10081 {
10082 RIDBIT_RESET (RID_LONG, specbits);
68642fb6 10083 type = build_qualified_type (long_double_type_node,
91063b51 10084 CP_TYPE_QUALS (type));
8d08fdba
MS
10085 }
10086
10087 /* Check all other uses of type modifiers. */
10088
10089 if (RIDBIT_SETP (RID_UNSIGNED, specbits)
10090 || RIDBIT_SETP (RID_SIGNED, specbits)
10091 || RIDBIT_SETP (RID_LONG, specbits)
10092 || RIDBIT_SETP (RID_SHORT, specbits))
10093 {
10094 int ok = 0;
10095
10096 if (TREE_CODE (type) == REAL_TYPE)
8251199e 10097 error ("short, signed or unsigned invalid for `%s'", name);
b7484fbe 10098 else if (TREE_CODE (type) != INTEGER_TYPE)
8251199e 10099 error ("long, short, signed or unsigned invalid for `%s'", name);
8d08fdba
MS
10100 else if (RIDBIT_SETP (RID_LONG, specbits)
10101 && RIDBIT_SETP (RID_SHORT, specbits))
8251199e 10102 error ("long and short specified together for `%s'", name);
8d08fdba
MS
10103 else if ((RIDBIT_SETP (RID_LONG, specbits)
10104 || RIDBIT_SETP (RID_SHORT, specbits))
10105 && explicit_char)
8251199e 10106 error ("long or short specified with char for `%s'", name);
8d08fdba
MS
10107 else if ((RIDBIT_SETP (RID_LONG, specbits)
10108 || RIDBIT_SETP (RID_SHORT, specbits))
10109 && TREE_CODE (type) == REAL_TYPE)
8251199e 10110 error ("long or short specified with floating type for `%s'", name);
8d08fdba
MS
10111 else if (RIDBIT_SETP (RID_SIGNED, specbits)
10112 && RIDBIT_SETP (RID_UNSIGNED, specbits))
8251199e 10113 error ("signed and unsigned given together for `%s'", name);
8d08fdba
MS
10114 else
10115 {
10116 ok = 1;
37c46b43 10117 if (!explicit_int && !defaulted_int && !explicit_char && pedantic)
8d08fdba 10118 {
8251199e 10119 pedwarn ("long, short, signed or unsigned used invalidly for `%s'",
8d08fdba
MS
10120 name);
10121 if (flag_pedantic_errors)
10122 ok = 0;
10123 }
10124 }
10125
10126 /* Discard the type modifiers if they are invalid. */
10127 if (! ok)
10128 {
10129 RIDBIT_RESET (RID_UNSIGNED, specbits);
10130 RIDBIT_RESET (RID_SIGNED, specbits);
10131 RIDBIT_RESET (RID_LONG, specbits);
10132 RIDBIT_RESET (RID_SHORT, specbits);
10133 longlong = 0;
10134 }
10135 }
10136
37c46b43
MS
10137 if (RIDBIT_SETP (RID_COMPLEX, specbits)
10138 && TREE_CODE (type) != INTEGER_TYPE && TREE_CODE (type) != REAL_TYPE)
10139 {
8251199e 10140 error ("complex invalid for `%s'", name);
37c46b43
MS
10141 RIDBIT_RESET (RID_COMPLEX, specbits);
10142 }
10143
8d08fdba
MS
10144 /* Decide whether an integer type is signed or not.
10145 Optionally treat bitfields as signed by default. */
10146 if (RIDBIT_SETP (RID_UNSIGNED, specbits)
b89c5a7b
MM
10147 /* [class.bit]
10148
10149 It is implementation-defined whether a plain (neither
10150 explicitly signed or unsigned) char, short, int, or long
10151 bit-field is signed or unsigned.
68642fb6 10152
b89c5a7b
MM
10153 Naturally, we extend this to long long as well. Note that
10154 this does not include wchar_t. */
10155 || (bitfield && !flag_signed_bitfields
10156 && RIDBIT_NOTSETP (RID_SIGNED, specbits)
10157 /* A typedef for plain `int' without `signed' can be
10158 controlled just like plain `int', but a typedef for
10159 `signed int' cannot be so controlled. */
68642fb6 10160 && !(typedef_decl
29bbeb1c 10161 && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl))
b89c5a7b
MM
10162 && (TREE_CODE (type) == INTEGER_TYPE
10163 || TREE_CODE (type) == CHAR_TYPE)
29bbeb1c 10164 && !same_type_p (TYPE_MAIN_VARIANT (type), wchar_type_node)))
8d08fdba
MS
10165 {
10166 if (longlong)
10167 type = long_long_unsigned_type_node;
10168 else if (RIDBIT_SETP (RID_LONG, specbits))
10169 type = long_unsigned_type_node;
10170 else if (RIDBIT_SETP (RID_SHORT, specbits))
10171 type = short_unsigned_type_node;
10172 else if (type == char_type_node)
10173 type = unsigned_char_type_node;
10174 else if (typedef_decl)
10175 type = unsigned_type (type);
10176 else
10177 type = unsigned_type_node;
10178 }
10179 else if (RIDBIT_SETP (RID_SIGNED, specbits)
10180 && type == char_type_node)
10181 type = signed_char_type_node;
10182 else if (longlong)
10183 type = long_long_integer_type_node;
10184 else if (RIDBIT_SETP (RID_LONG, specbits))
10185 type = long_integer_type_node;
10186 else if (RIDBIT_SETP (RID_SHORT, specbits))
10187 type = short_integer_type_node;
10188
37c46b43
MS
10189 if (RIDBIT_SETP (RID_COMPLEX, specbits))
10190 {
10191 /* If we just have "complex", it is equivalent to
10192 "complex double", but if any modifiers at all are specified it is
10193 the complex form of TYPE. E.g, "complex short" is
10194 "complex short int". */
10195
10196 if (defaulted_int && ! longlong
10197 && ! (RIDBIT_SETP (RID_LONG, specbits)
10198 || RIDBIT_SETP (RID_SHORT, specbits)
10199 || RIDBIT_SETP (RID_SIGNED, specbits)
10200 || RIDBIT_SETP (RID_UNSIGNED, specbits)))
10201 type = complex_double_type_node;
10202 else if (type == integer_type_node)
10203 type = complex_integer_type_node;
10204 else if (type == float_type_node)
10205 type = complex_float_type_node;
10206 else if (type == double_type_node)
10207 type = complex_double_type_node;
10208 else if (type == long_double_type_node)
10209 type = complex_long_double_type_node;
10210 else
10211 type = build_complex_type (type);
10212 }
10213
3dbc07b6 10214 if (sfk == sfk_conversion
3ac3d9ea 10215 && (RIDBIT_SETP (RID_CONST, specbits)
91063b51
MM
10216 || RIDBIT_SETP (RID_VOLATILE, specbits)
10217 || RIDBIT_SETP (RID_RESTRICT, specbits)))
10218 cp_error ("qualifiers are not allowed on declaration of `operator %T'",
3ac3d9ea
MM
10219 ctor_return_type);
10220
8d08fdba
MS
10221 /* Set CONSTP if this declaration is `const', whether by
10222 explicit specification or via a typedef.
10223 Likewise for VOLATILEP. */
10224
91063b51 10225 constp = !! RIDBIT_SETP (RID_CONST, specbits) + CP_TYPE_CONST_P (type);
68642fb6 10226 restrictp =
91063b51 10227 !! RIDBIT_SETP (RID_RESTRICT, specbits) + CP_TYPE_RESTRICT_P (type);
68642fb6 10228 volatilep =
91063b51
MM
10229 !! RIDBIT_SETP (RID_VOLATILE, specbits) + CP_TYPE_VOLATILE_P (type);
10230 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
10231 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
10232 | (volatilep ? TYPE_QUAL_VOLATILE : 0));
10233 type = cp_build_qualified_type (type, type_quals);
8d08fdba
MS
10234 staticp = 0;
10235 inlinep = !! RIDBIT_SETP (RID_INLINE, specbits);
8d08fdba 10236 virtualp = RIDBIT_SETP (RID_VIRTUAL, specbits);
db5ae43f
MS
10237 RIDBIT_RESET (RID_VIRTUAL, specbits);
10238 explicitp = RIDBIT_SETP (RID_EXPLICIT, specbits) != 0;
10239 RIDBIT_RESET (RID_EXPLICIT, specbits);
8d08fdba 10240
8d08fdba
MS
10241 if (RIDBIT_SETP (RID_STATIC, specbits))
10242 staticp = 1 + (decl_context == FIELD);
10243
10244 if (virtualp && staticp == 2)
10245 {
8251199e 10246 cp_error ("member `%D' cannot be declared both virtual and static",
8d08fdba
MS
10247 dname);
10248 staticp = 0;
10249 }
10250 friendp = RIDBIT_SETP (RID_FRIEND, specbits);
8d08fdba
MS
10251 RIDBIT_RESET (RID_FRIEND, specbits);
10252
8d08fdba
MS
10253 /* Warn if two storage classes are given. Default to `auto'. */
10254
10255 if (RIDBIT_ANY_SET (specbits))
10256 {
10257 if (RIDBIT_SETP (RID_STATIC, specbits)) nclasses++;
10258 if (RIDBIT_SETP (RID_EXTERN, specbits)) nclasses++;
10259 if (decl_context == PARM && nclasses > 0)
8251199e 10260 error ("storage class specifiers invalid in parameter declarations");
8d08fdba
MS
10261 if (RIDBIT_SETP (RID_TYPEDEF, specbits))
10262 {
10263 if (decl_context == PARM)
8251199e 10264 error ("typedef declaration invalid in parameter declaration");
8d08fdba
MS
10265 nclasses++;
10266 }
10267 if (RIDBIT_SETP (RID_AUTO, specbits)) nclasses++;
10268 if (RIDBIT_SETP (RID_REGISTER, specbits)) nclasses++;
10269 }
10270
10271 /* Give error if `virtual' is used outside of class declaration. */
b7484fbe
MS
10272 if (virtualp
10273 && (current_class_name == NULL_TREE || decl_context != FIELD))
8d08fdba 10274 {
8251199e 10275 error ("virtual outside class declaration");
8d08fdba
MS
10276 virtualp = 0;
10277 }
8d08fdba
MS
10278
10279 /* Static anonymous unions are dealt with here. */
10280 if (staticp && decl_context == TYPENAME
10281 && TREE_CODE (declspecs) == TREE_LIST
6bdb8141 10282 && ANON_AGGR_TYPE_P (TREE_VALUE (declspecs)))
8d08fdba
MS
10283 decl_context = FIELD;
10284
8d08fdba
MS
10285 /* Warn about storage classes that are invalid for certain
10286 kinds of declarations (parameters, typenames, etc.). */
10287
10288 if (nclasses > 1)
8251199e 10289 error ("multiple storage classes in declaration of `%s'", name);
8d08fdba
MS
10290 else if (decl_context != NORMAL && nclasses > 0)
10291 {
db5ae43f 10292 if ((decl_context == PARM || decl_context == CATCHPARM)
8d08fdba
MS
10293 && (RIDBIT_SETP (RID_REGISTER, specbits)
10294 || RIDBIT_SETP (RID_AUTO, specbits)))
10295 ;
fc378698
MS
10296 else if (RIDBIT_SETP (RID_TYPEDEF, specbits))
10297 ;
8d08fdba 10298 else if (decl_context == FIELD
d363e7bf
AJ
10299 /* C++ allows static class elements */
10300 && RIDBIT_SETP (RID_STATIC, specbits))
10301 /* C++ also allows inlines and signed and unsigned elements,
10302 but in those cases we don't come in here. */
8d08fdba
MS
10303 ;
10304 else
10305 {
10306 if (decl_context == FIELD)
10307 {
b7484fbe
MS
10308 tree tmp = NULL_TREE;
10309 register int op = 0;
10310
10311 if (declarator)
10312 {
68642fb6 10313 /* Avoid trying to get an operand off an identifier node. */
9e9ff709
MS
10314 if (TREE_CODE (declarator) == IDENTIFIER_NODE)
10315 tmp = declarator;
10316 else
10317 tmp = TREE_OPERAND (declarator, 0);
b7484fbe 10318 op = IDENTIFIER_OPNAME_P (tmp);
596ea4e5
AS
10319 if (IDENTIFIER_TYPENAME_P (tmp))
10320 {
10321 if (IDENTIFIER_GLOBAL_VALUE (tmp)
5362b086 10322 && (TREE_CODE (IDENTIFIER_GLOBAL_VALUE (tmp))
596ea4e5
AS
10323 == TYPE_DECL))
10324 name = IDENTIFIER_POINTER (tmp);
10325 else
10326 name = "<invalid operator>";
10327 }
b7484fbe 10328 }
8251199e 10329 error ("storage class specified for %s `%s'",
6eabb241 10330 op ? "member operator" : "field",
596ea4e5 10331 name);
8d08fdba
MS
10332 }
10333 else
b9d12519
KG
10334 {
10335 if (decl_context == PARM || decl_context == CATCHPARM)
10336 error ("storage class specified for parameter `%s'", name);
10337 else
10338 error ("storage class specified for typename");
10339 }
8d08fdba
MS
10340 RIDBIT_RESET (RID_REGISTER, specbits);
10341 RIDBIT_RESET (RID_AUTO, specbits);
10342 RIDBIT_RESET (RID_EXTERN, specbits);
8d08fdba
MS
10343 }
10344 }
10345 else if (RIDBIT_SETP (RID_EXTERN, specbits) && initialized && !funcdef_flag)
10346 {
a9aedbc2 10347 if (toplevel_bindings_p ())
8d08fdba 10348 {
59be0cdd 10349 /* It's common practice (and completely valid) to have a const
8d08fdba 10350 be initialized and declared extern. */
950ad3c3 10351 if (!(type_quals & TYPE_QUAL_CONST))
8251199e 10352 warning ("`%s' initialized and declared `extern'", name);
8d08fdba
MS
10353 }
10354 else
8251199e 10355 error ("`%s' has both `extern' and initializer", name);
8d08fdba
MS
10356 }
10357 else if (RIDBIT_SETP (RID_EXTERN, specbits) && funcdef_flag
a9aedbc2 10358 && ! toplevel_bindings_p ())
8251199e 10359 error ("nested function `%s' declared `extern'", name);
a9aedbc2 10360 else if (toplevel_bindings_p ())
8d08fdba
MS
10361 {
10362 if (RIDBIT_SETP (RID_AUTO, specbits))
8251199e 10363 error ("top-level declaration of `%s' specifies `auto'", name);
8d08fdba
MS
10364 }
10365
909e536a 10366 if (nclasses > 0 && friendp)
8251199e 10367 error ("storage class specifiers invalid in friend function declarations");
909e536a 10368
8d08fdba
MS
10369 /* Now figure out the structure of the declarator proper.
10370 Descend through it, creating more complex types, until we reach
10371 the declared identifier (or NULL_TREE, in an absolute declarator). */
10372
1eab9b56 10373 inner_attrs = NULL_TREE;
68642fb6 10374 ignore_attrs = 0;
1eab9b56 10375
386b8a85
JM
10376 while (declarator && TREE_CODE (declarator) != IDENTIFIER_NODE
10377 && TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
8d08fdba
MS
10378 {
10379 /* Each level of DECLARATOR is either an ARRAY_REF (for ...[..]),
10380 an INDIRECT_REF (for *...),
10381 a CALL_EXPR (for ...(...)),
10382 an identifier (for the name being declared)
10383 or a null pointer (for the place in an absolute declarator
10384 where the name was omitted).
10385 For the last two cases, we have just exited the loop.
10386
10387 For C++ it could also be
10388 a SCOPE_REF (for class :: ...). In this case, we have converted
10389 sensible names to types, and those are the values we use to
10390 qualify the member name.
10391 an ADDR_EXPR (for &...),
10392 a BIT_NOT_EXPR (for destructors)
8d08fdba
MS
10393
10394 At this point, TYPE is the type of elements of an array,
10395 or for a function to return, or for a pointer to point to.
10396 After this sequence of ifs, TYPE is the type of the
10397 array or function or pointer, and DECLARATOR has had its
10398 outermost layer removed. */
10399
bd6dd845 10400 if (type == error_mark_node)
8d08fdba
MS
10401 {
10402 if (TREE_CODE (declarator) == SCOPE_REF)
10403 declarator = TREE_OPERAND (declarator, 1);
10404 else
10405 declarator = TREE_OPERAND (declarator, 0);
10406 continue;
10407 }
10408 if (quals != NULL_TREE
10409 && (declarator == NULL_TREE
10410 || TREE_CODE (declarator) != SCOPE_REF))
10411 {
10412 if (ctype == NULL_TREE && TREE_CODE (type) == METHOD_TYPE)
10413 ctype = TYPE_METHOD_BASETYPE (type);
10414 if (ctype != NULL_TREE)
10415 {
8d08fdba 10416 tree dummy = build_decl (TYPE_DECL, NULL_TREE, type);
535233a8 10417 grok_method_quals (ctype, dummy, quals);
8d08fdba 10418 type = TREE_TYPE (dummy);
535233a8 10419 ctype = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (type)));
8d08fdba
MS
10420 quals = NULL_TREE;
10421 }
10422 }
1eab9b56
JM
10423
10424 /* See the comment for the TREE_LIST case, below. */
10425 if (ignore_attrs)
10426 ignore_attrs = 0;
10427 else if (inner_attrs)
10428 {
10429 decl_attributes (type, inner_attrs, NULL_TREE);
10430 inner_attrs = NULL_TREE;
10431 }
10432
8d08fdba
MS
10433 switch (TREE_CODE (declarator))
10434 {
52fbc847
JM
10435 case TREE_LIST:
10436 {
10437 /* We encode a declarator with embedded attributes using
1eab9b56
JM
10438 a TREE_LIST. The attributes apply to the declarator
10439 directly inside them, so we have to skip an iteration
10440 before applying them to the type. If the declarator just
10441 inside is the declarator-id, we apply the attrs to the
10442 decl itself. */
10443 inner_attrs = TREE_PURPOSE (declarator);
10444 ignore_attrs = 1;
52fbc847 10445 declarator = TREE_VALUE (declarator);
52fbc847
JM
10446 }
10447 break;
10448
8d08fdba
MS
10449 case ARRAY_REF:
10450 {
2848ad0f 10451 register tree size;
8d08fdba 10452
2848ad0f 10453 size = TREE_OPERAND (declarator, 1);
8d08fdba 10454
f2e2cbd4 10455 /* VC++ spells a zero-sized array with []. */
e8ea7036
MM
10456 if (size == NULL_TREE && decl_context == FIELD && ! staticp
10457 && ! RIDBIT_SETP (RID_TYPEDEF, specbits))
f2e2cbd4
JM
10458 size = integer_zero_node;
10459
2848ad0f 10460 declarator = TREE_OPERAND (declarator, 0);
8d08fdba 10461
2848ad0f 10462 type = create_array_type_for_decl (dname, type, size);
c1b177ec
ML
10463
10464 /* VLAs never work as fields. */
5362b086 10465 if (decl_context == FIELD && !processing_template_decl
c1b177ec
ML
10466 && TREE_CODE (type) == ARRAY_TYPE
10467 && TYPE_DOMAIN (type) != NULL_TREE
10468 && !TREE_CONSTANT (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
10469 {
10470 cp_error ("size of member `%D' is not constant", dname);
10471 /* Proceed with arbitrary constant size, so that offset
10472 computations don't get confused. */
10473 type = create_array_type_for_decl (dname, TREE_TYPE (type),
10474 integer_one_node);
10475 }
10476
8d08fdba
MS
10477 ctype = NULL_TREE;
10478 }
10479 break;
10480
10481 case CALL_EXPR:
10482 {
10483 tree arg_types;
f376e137 10484 int funcdecl_p;
43f887f9 10485 tree inner_parms = CALL_DECLARATOR_PARMS (declarator);
f376e137 10486 tree inner_decl = TREE_OPERAND (declarator, 0);
8d08fdba
MS
10487
10488 /* Declaring a function type.
10489 Make sure we have a valid type for the function to return. */
8d08fdba 10490
91063b51 10491 /* We now know that the TYPE_QUALS don't apply to the
14ae7e7d 10492 decl, but to its return type. */
91063b51 10493 type_quals = TYPE_UNQUALIFIED;
8d08fdba
MS
10494
10495 /* Warn about some types functions can't return. */
10496
10497 if (TREE_CODE (type) == FUNCTION_TYPE)
10498 {
8251199e 10499 error ("`%s' declared as function returning a function", name);
8d08fdba
MS
10500 type = integer_type_node;
10501 }
10502 if (TREE_CODE (type) == ARRAY_TYPE)
10503 {
8251199e 10504 error ("`%s' declared as function returning an array", name);
8d08fdba
MS
10505 type = integer_type_node;
10506 }
10507
f376e137
MS
10508 if (inner_decl && TREE_CODE (inner_decl) == SCOPE_REF)
10509 inner_decl = TREE_OPERAND (inner_decl, 1);
10510
68642fb6 10511 if (inner_decl && TREE_CODE (inner_decl) == TEMPLATE_ID_EXPR)
386b8a85
JM
10512 inner_decl = dname;
10513
b7484fbe 10514 /* Pick up type qualifiers which should be applied to `this'. */
43f887f9 10515 quals = CALL_DECLARATOR_QUALS (declarator);
b7484fbe 10516
c11b6f21 10517 /* Pick up the exception specifications. */
43f887f9 10518 raises = CALL_DECLARATOR_EXCEPTION_SPEC (declarator);
c11b6f21 10519
f376e137
MS
10520 /* Say it's a definition only for the CALL_EXPR
10521 closest to the identifier. */
beb53fb8 10522 funcdecl_p
68642fb6 10523 = inner_decl
386b8a85 10524 && (TREE_CODE (inner_decl) == IDENTIFIER_NODE
68642fb6 10525 || TREE_CODE (inner_decl) == TEMPLATE_ID_EXPR
386b8a85 10526 || TREE_CODE (inner_decl) == BIT_NOT_EXPR);
68642fb6 10527
8d08fdba
MS
10528 if (ctype == NULL_TREE
10529 && decl_context == FIELD
f376e137 10530 && funcdecl_p
8d08fdba
MS
10531 && (friendp == 0 || dname == current_class_name))
10532 ctype = current_class_type;
10533
3dbc07b6 10534 if (ctype && sfk == sfk_conversion)
8d08fdba
MS
10535 TYPE_HAS_CONVERSION (ctype) = 1;
10536 if (ctype && constructor_name (ctype) == dname)
10537 {
10538 /* We are within a class's scope. If our declarator name
10539 is the same as the class name, and we are defining
10540 a function, then it is a constructor/destructor, and
10541 therefore returns a void type. */
10542
10543 if (flags == DTOR_FLAG)
10544 {
cab1f180
ML
10545 /* ISO C++ 12.4/2. A destructor may not be
10546 declared const or volatile. A destructor may
10547 not be static. */
8d08fdba 10548 if (staticp == 2)
8251199e 10549 error ("destructor cannot be static member function");
b7484fbe 10550 if (quals)
8d08fdba 10551 {
4f70a846
MM
10552 cp_error ("destructors may not be `%s'",
10553 IDENTIFIER_POINTER (TREE_VALUE (quals)));
10554 quals = NULL_TREE;
8d08fdba
MS
10555 }
10556 if (decl_context == FIELD)
10557 {
4dacf5bd
KG
10558 if (! member_function_or_else (ctype,
10559 current_class_type,
10560 flags))
8d08fdba
MS
10561 return void_type_node;
10562 }
10563 }
2c73f9f5 10564 else /* It's a constructor. */
8d08fdba 10565 {
db5ae43f
MS
10566 if (explicitp == 1)
10567 explicitp = 2;
cab1f180
ML
10568 /* ISO C++ 12.1. A constructor may not be
10569 declared const or volatile. A constructor may
10570 not be virtual. A constructor may not be
10571 static. */
8d08fdba 10572 if (staticp == 2)
8251199e 10573 error ("constructor cannot be static member function");
8d08fdba
MS
10574 if (virtualp)
10575 {
8251199e 10576 pedwarn ("constructors cannot be declared virtual");
8d08fdba
MS
10577 virtualp = 0;
10578 }
b7484fbe 10579 if (quals)
8d08fdba 10580 {
4f70a846
MM
10581 cp_error ("constructors may not be `%s'",
10582 IDENTIFIER_POINTER (TREE_VALUE (quals)));
10583 quals = NULL_TREE;
d363e7bf 10584 }
8d08fdba 10585 {
51c184be 10586 RID_BIT_TYPE tmp_bits;
4e135bdd 10587 memcpy (&tmp_bits, &specbits, sizeof (RID_BIT_TYPE));
51c184be
MS
10588 RIDBIT_RESET (RID_INLINE, tmp_bits);
10589 RIDBIT_RESET (RID_STATIC, tmp_bits);
10590 if (RIDBIT_ANY_SET (tmp_bits))
8251199e 10591 error ("return value type specifier for constructor ignored");
8d08fdba 10592 }
6eabb241 10593 if (decl_context == FIELD)
8d08fdba 10594 {
4dacf5bd
KG
10595 if (! member_function_or_else (ctype,
10596 current_class_type,
10597 flags))
8d08fdba
MS
10598 return void_type_node;
10599 TYPE_HAS_CONSTRUCTOR (ctype) = 1;
3dbc07b6 10600 if (sfk != sfk_constructor)
8d08fdba
MS
10601 return NULL_TREE;
10602 }
10603 }
10604 if (decl_context == FIELD)
10605 staticp = 0;
10606 }
b7484fbe 10607 else if (friendp)
8d08fdba 10608 {
b7484fbe 10609 if (initialized)
8251199e 10610 error ("can't initialize friend function `%s'", name);
b7484fbe
MS
10611 if (virtualp)
10612 {
10613 /* Cannot be both friend and virtual. */
8251199e 10614 error ("virtual functions cannot be friends");
b7484fbe
MS
10615 RIDBIT_RESET (RID_FRIEND, specbits);
10616 friendp = 0;
10617 }
28cbf42c 10618 if (decl_context == NORMAL)
8251199e 10619 error ("friend declaration not in class definition");
28cbf42c 10620 if (current_function_decl && funcdef_flag)
8251199e 10621 cp_error ("can't define friend function `%s' in a local class definition",
28cbf42c 10622 name);
8d08fdba
MS
10623 }
10624
8d08fdba
MS
10625 /* Construct the function type and go to the next
10626 inner layer of declarator. */
10627
f376e137 10628 declarator = TREE_OPERAND (declarator, 0);
8d08fdba 10629
f376e137
MS
10630 /* FIXME: This is where default args should be fully
10631 processed. */
8d08fdba 10632
5cce22b6 10633 arg_types = grokparms (inner_parms);
8d08fdba 10634
4f70a846 10635 if (declarator && flags == DTOR_FLAG)
8d08fdba 10636 {
4f70a846
MM
10637 /* A destructor declared in the body of a class will
10638 be represented as a BIT_NOT_EXPR. But, we just
10639 want the underlying IDENTIFIER. */
8d08fdba 10640 if (TREE_CODE (declarator) == BIT_NOT_EXPR)
4f70a846 10641 declarator = TREE_OPERAND (declarator, 0);
68642fb6 10642
8b27e9ef 10643 if (arg_types != void_list_node)
8d08fdba 10644 {
4f70a846
MM
10645 cp_error ("destructors may not have parameters");
10646 arg_types = void_list_node;
10647 last_function_parms = NULL_TREE;
8d08fdba
MS
10648 }
10649 }
10650
d22c8596 10651 /* ANSI says that `const int foo ();'
8d08fdba 10652 does not make the function foo const. */
d22c8596 10653 type = build_function_type (type, arg_types);
42976354
BK
10654
10655 {
10656 tree t;
10657 for (t = arg_types; t; t = TREE_CHAIN (t))
10658 if (TREE_PURPOSE (t)
10659 && TREE_CODE (TREE_PURPOSE (t)) == DEFAULT_ARG)
10660 {
10661 add_defarg_fn (type);
10662 break;
10663 }
10664 }
8d08fdba
MS
10665 }
10666 break;
10667
10668 case ADDR_EXPR:
10669 case INDIRECT_REF:
10670 /* Filter out pointers-to-references and references-to-references.
10671 We can get these if a TYPE_DECL is used. */
10672
10673 if (TREE_CODE (type) == REFERENCE_TYPE)
10674 {
8251199e 10675 error ("cannot declare %s to references",
8d08fdba
MS
10676 TREE_CODE (declarator) == ADDR_EXPR
10677 ? "references" : "pointers");
10678 declarator = TREE_OPERAND (declarator, 0);
10679 continue;
10680 }
10681
a5894242
MS
10682 if (TREE_CODE (type) == OFFSET_TYPE
10683 && (TREE_CODE (TREE_TYPE (type)) == VOID_TYPE
10684 || TREE_CODE (TREE_TYPE (type)) == REFERENCE_TYPE))
10685 {
8251199e 10686 cp_error ("cannot declare pointer to `%#T' member",
a5894242
MS
10687 TREE_TYPE (type));
10688 type = TREE_TYPE (type);
10689 }
10690
61a127b3
MM
10691 /* Merge any constancy or volatility into the target type
10692 for the pointer. */
10693
91063b51
MM
10694 /* We now know that the TYPE_QUALS don't apply to the decl,
10695 but to the target of the pointer. */
10696 type_quals = TYPE_UNQUALIFIED;
8d08fdba 10697
6eabb241 10698 if (TREE_CODE (declarator) == ADDR_EXPR)
8d08fdba 10699 {
69851283
MM
10700 if (TREE_CODE (type) == VOID_TYPE)
10701 error ("invalid type: `void &'");
8d08fdba 10702 else
69851283 10703 type = build_reference_type (type);
8d08fdba
MS
10704 }
10705 else if (TREE_CODE (type) == METHOD_TYPE)
d8f8dca1 10706 type = build_ptrmemfunc_type (build_pointer_type (type));
8d08fdba
MS
10707 else
10708 type = build_pointer_type (type);
10709
10710 /* Process a list of type modifier keywords (such as
10711 const or volatile) that were given inside the `*' or `&'. */
10712
10713 if (TREE_TYPE (declarator))
10714 {
10715 register tree typemodlist;
10716 int erred = 0;
91063b51
MM
10717
10718 constp = 0;
10719 volatilep = 0;
10720 restrictp = 0;
8d08fdba
MS
10721 for (typemodlist = TREE_TYPE (declarator); typemodlist;
10722 typemodlist = TREE_CHAIN (typemodlist))
10723 {
91063b51
MM
10724 tree qualifier = TREE_VALUE (typemodlist);
10725
10726 if (qualifier == ridpointers[(int) RID_CONST])
8d08fdba 10727 constp++;
91063b51 10728 else if (qualifier == ridpointers[(int) RID_VOLATILE])
8d08fdba 10729 volatilep++;
91063b51
MM
10730 else if (qualifier == ridpointers[(int) RID_RESTRICT])
10731 restrictp++;
8d08fdba
MS
10732 else if (!erred)
10733 {
10734 erred = 1;
91063b51 10735 error ("invalid type modifier within pointer declarator");
8d08fdba
MS
10736 }
10737 }
10738 if (constp > 1)
8251199e 10739 pedwarn ("duplicate `const'");
8d08fdba 10740 if (volatilep > 1)
8251199e 10741 pedwarn ("duplicate `volatile'");
91063b51
MM
10742 if (restrictp > 1)
10743 pedwarn ("duplicate `restrict'");
10744
10745 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
10746 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
10747 | (volatilep ? TYPE_QUAL_VOLATILE : 0));
a0a33927
MS
10748 if (TREE_CODE (declarator) == ADDR_EXPR
10749 && (constp || volatilep))
10750 {
10751 if (constp)
8251199e 10752 pedwarn ("discarding `const' applied to a reference");
a0a33927 10753 if (volatilep)
8251199e 10754 pedwarn ("discarding `volatile' applied to a reference");
91063b51 10755 type_quals &= ~(TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE);
a0a33927 10756 }
91063b51 10757 type = cp_build_qualified_type (type, type_quals);
8d08fdba
MS
10758 }
10759 declarator = TREE_OPERAND (declarator, 0);
10760 ctype = NULL_TREE;
10761 break;
10762
10763 case SCOPE_REF:
10764 {
10765 /* We have converted type names to NULL_TREE if the
10766 name was bogus, or to a _TYPE node, if not.
10767
10768 The variable CTYPE holds the type we will ultimately
10769 resolve to. The code here just needs to build
10770 up appropriate member types. */
10771 tree sname = TREE_OPERAND (declarator, 1);
386b8a85
JM
10772 tree t;
10773
8d08fdba
MS
10774 /* Destructors can have their visibilities changed as well. */
10775 if (TREE_CODE (sname) == BIT_NOT_EXPR)
10776 sname = TREE_OPERAND (sname, 0);
10777
10778 if (TREE_COMPLEXITY (declarator) == 0)
10779 /* This needs to be here, in case we are called
10780 multiple times. */ ;
9a68c51f
JM
10781 else if (TREE_COMPLEXITY (declarator) == -1)
10782 /* Namespace member. */
10783 pop_decl_namespace ();
8d08fdba 10784 else if (friendp && (TREE_COMPLEXITY (declarator) < 2))
2c73f9f5 10785 /* Don't fall out into global scope. Hides real bug? --eichin */ ;
5566b478
MS
10786 else if (! IS_AGGR_TYPE_CODE
10787 (TREE_CODE (TREE_OPERAND (declarator, 0))))
10788 ;
8d08fdba
MS
10789 else if (TREE_COMPLEXITY (declarator) == current_class_depth)
10790 {
9a3b49ac
MS
10791 /* Resolve any TYPENAME_TYPEs from the decl-specifier-seq
10792 that refer to ctype. They couldn't be resolved earlier
10793 because we hadn't pushed into the class yet.
10794 Example: resolve 'B<T>::type' in
10795 'B<typename B<T>::type> B<T>::f () { }'. */
10796 if (current_template_parms
10797 && uses_template_parms (type)
10798 && uses_template_parms (current_class_type))
10799 {
10800 tree args = current_template_args ();
4393e105 10801 type = tsubst (type, args, /*complain=*/1, NULL_TREE);
9a3b49ac
MS
10802 }
10803
a28e3c7f
MS
10804 /* This pop_nested_class corresponds to the
10805 push_nested_class used to push into class scope for
10806 parsing the argument list of a function decl, in
10807 qualified_id. */
b74a0560 10808 pop_nested_class ();
8d08fdba
MS
10809 TREE_COMPLEXITY (declarator) = current_class_depth;
10810 }
10811 else
10812 my_friendly_abort (16);
10813
10814 if (TREE_OPERAND (declarator, 0) == NULL_TREE)
10815 {
10816 /* We had a reference to a global decl, or
10817 perhaps we were given a non-aggregate typedef,
10818 in which case we cleared this out, and should just
10819 keep going as though it wasn't there. */
10820 declarator = sname;
10821 continue;
10822 }
10823 ctype = TREE_OPERAND (declarator, 0);
10824
386b8a85 10825 t = ctype;
68642fb6 10826 while (t != NULL_TREE && CLASS_TYPE_P (t))
386b8a85 10827 {
5362b086 10828 /* You're supposed to have one `template <...>'
da8a7456
MM
10829 for every template class, but you don't need one
10830 for a full specialization. For example:
10831
10832 template <class T> struct S{};
10833 template <> struct S<int> { void f(); };
10834 void S<int>::f () {}
10835
10836 is correct; there shouldn't be a `template <>' for
10837 the definition of `S<int>::f'. */
10838 if (CLASSTYPE_TEMPLATE_INFO (t)
10839 && (CLASSTYPE_TEMPLATE_INSTANTIATION (t)
92ecdfb7
NS
10840 || uses_template_parms (CLASSTYPE_TI_ARGS (t)))
10841 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t)))
386b8a85 10842 template_count += 1;
da8a7456 10843
386b8a85 10844 t = TYPE_MAIN_DECL (t);
92ecdfb7 10845 t = DECL_CONTEXT (t);
386b8a85
JM
10846 }
10847
8d08fdba
MS
10848 if (sname == NULL_TREE)
10849 goto done_scoping;
10850
10851 if (TREE_CODE (sname) == IDENTIFIER_NODE)
10852 {
10853 /* This is the `standard' use of the scoping operator:
10854 basetype :: member . */
10855
db5ae43f 10856 if (ctype == current_class_type)
28cbf42c
MS
10857 {
10858 /* class A {
10859 void A::f ();
10860 };
10861
10862 Is this ill-formed? */
10863
10864 if (pedantic)
8251199e 10865 cp_pedwarn ("extra qualification `%T::' on member `%s' ignored",
28cbf42c
MS
10866 ctype, name);
10867 }
db5ae43f 10868 else if (TREE_CODE (type) == FUNCTION_TYPE)
8d08fdba 10869 {
eeb75383 10870 if (current_class_type == NULL_TREE || friendp)
14ae7e7d
JM
10871 type = build_cplus_method_type (ctype, TREE_TYPE (type),
10872 TYPE_ARG_TYPES (type));
8d08fdba
MS
10873 else
10874 {
8251199e 10875 cp_error ("cannot declare member function `%T::%s' within `%T'",
a3203465 10876 ctype, name, current_class_type);
8d08fdba
MS
10877 return void_type_node;
10878 }
10879 }
5566b478 10880 else if (RIDBIT_SETP (RID_TYPEDEF, specbits)
d0f062fb 10881 || COMPLETE_TYPE_P (complete_type (ctype)))
8d08fdba 10882 {
2c73f9f5 10883 /* Have to move this code elsewhere in this function.
db5ae43f
MS
10884 this code is used for i.e., typedef int A::M; M *pm;
10885
10886 It is? How? jason 10/2/94 */
8d08fdba 10887
8d08fdba
MS
10888 if (current_class_type)
10889 {
8251199e 10890 cp_error ("cannot declare member `%T::%s' within `%T'",
db5ae43f
MS
10891 ctype, name, current_class_type);
10892 return void_type_node;
8d08fdba
MS
10893 }
10894 type = build_offset_type (ctype, type);
10895 }
8d08fdba 10896 else
eeb75383
NS
10897 {
10898 incomplete_type_error (NULL_TREE, ctype);
10899 return error_mark_node;
d363e7bf 10900 }
8d08fdba
MS
10901
10902 declarator = sname;
10903 }
8d08fdba
MS
10904 else if (TREE_CODE (sname) == SCOPE_REF)
10905 my_friendly_abort (17);
10906 else
10907 {
10908 done_scoping:
10909 declarator = TREE_OPERAND (declarator, 1);
10910 if (declarator && TREE_CODE (declarator) == CALL_EXPR)
10911 /* In this case, we will deal with it later. */
10912 ;
10913 else
10914 {
10915 if (TREE_CODE (type) == FUNCTION_TYPE)
14ae7e7d
JM
10916 type = build_cplus_method_type (ctype, TREE_TYPE (type),
10917 TYPE_ARG_TYPES (type));
8d08fdba
MS
10918 else
10919 type = build_offset_type (ctype, type);
10920 }
10921 }
10922 }
10923 break;
10924
10925 case BIT_NOT_EXPR:
10926 declarator = TREE_OPERAND (declarator, 0);
10927 break;
10928
8d08fdba
MS
10929 case RECORD_TYPE:
10930 case UNION_TYPE:
10931 case ENUMERAL_TYPE:
10932 declarator = NULL_TREE;
10933 break;
10934
10935 case ERROR_MARK:
10936 declarator = NULL_TREE;
10937 break;
10938
10939 default:
10940 my_friendly_abort (158);
10941 }
10942 }
10943
1eab9b56
JM
10944 /* See the comment for the TREE_LIST case, above. */
10945 if (inner_attrs)
10946 {
10947 if (! ignore_attrs)
10948 decl_attributes (type, inner_attrs, NULL_TREE);
10949 else if (attrlist)
10950 TREE_VALUE (attrlist) = chainon (inner_attrs, TREE_VALUE (attrlist));
10951 else
1f8f4a0b 10952 attrlist = build_tree_list (NULL_TREE, inner_attrs);
1eab9b56
JM
10953 }
10954
419c6212
JM
10955 /* Now TYPE has the actual type. */
10956
34fc957d 10957 if (explicitp == 1 || (explicitp && friendp))
db5ae43f 10958 {
34fc957d
NS
10959 /* [dcl.fct.spec] The explicit specifier shall only be used in
10960 declarations of constructors within a class definition. */
10961 error ("only declarations of constructors can be `explicit'");
db5ae43f
MS
10962 explicitp = 0;
10963 }
10964
f30432d7
MS
10965 if (RIDBIT_SETP (RID_MUTABLE, specbits))
10966 {
34fc957d
NS
10967 if (current_class_name == NULL_TREE || decl_context == PARM || friendp)
10968 {
10969 error ("non-member `%s' cannot be declared `mutable'", name);
10970 RIDBIT_RESET (RID_MUTABLE, specbits);
10971 }
10972 else if (decl_context == TYPENAME || RIDBIT_SETP (RID_TYPEDEF, specbits))
f30432d7 10973 {
34fc957d 10974 error ("non-object member `%s' cannot be declared `mutable'", name);
f30432d7
MS
10975 RIDBIT_RESET (RID_MUTABLE, specbits);
10976 }
34fc957d
NS
10977 else if (TREE_CODE (type) == FUNCTION_TYPE
10978 || TREE_CODE (type) == METHOD_TYPE)
10979 {
10980 error ("function `%s' cannot be declared `mutable'", name);
10981 RIDBIT_RESET (RID_MUTABLE, specbits);
10982 }
f30432d7
MS
10983 else if (staticp)
10984 {
8251199e 10985 error ("static `%s' cannot be declared `mutable'", name);
f30432d7
MS
10986 RIDBIT_RESET (RID_MUTABLE, specbits);
10987 }
34fc957d
NS
10988 else if (type_quals & TYPE_QUAL_CONST)
10989 {
10990 error ("const `%s' cannot be declared `mutable'", name);
10991 RIDBIT_RESET (RID_MUTABLE, specbits);
10992 }
f30432d7
MS
10993 }
10994
efe38fab
JM
10995 if (declarator == NULL_TREE
10996 || TREE_CODE (declarator) == IDENTIFIER_NODE
10997 || (TREE_CODE (declarator) == TEMPLATE_ID_EXPR
10998 && (TREE_CODE (type) == FUNCTION_TYPE
10999 || TREE_CODE (type) == METHOD_TYPE)))
11000 /* OK */;
11001 else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
11002 {
11003 cp_error ("template-id `%D' used as a declarator", declarator);
11004 declarator = dname;
11005 }
11006 else
419c6212 11007 /* Unexpected declarator format. */
efe38fab
JM
11008 my_friendly_abort (990210);
11009
419c6212
JM
11010 /* If this is declaring a typedef name, return a TYPE_DECL. */
11011
fc378698 11012 if (RIDBIT_SETP (RID_TYPEDEF, specbits) && decl_context != TYPENAME)
8d08fdba
MS
11013 {
11014 tree decl;
11015
11016 /* Note that the grammar rejects storage classes
11017 in typenames, fields or parameters. */
eff71ab0
PB
11018 if (current_lang_name == lang_name_java)
11019 TYPE_FOR_JAVA (type) = 1;
8d08fdba 11020
d2e5ee5c
MS
11021 if (decl_context == FIELD)
11022 {
094fe153 11023 if (declarator == constructor_name (current_class_type))
cb9a3ff8 11024 cp_pedwarn ("ISO C++ forbids nested type `%D' with same name as enclosing class",
d2e5ee5c
MS
11025 declarator);
11026 decl = build_lang_decl (TYPE_DECL, declarator, type);
d2e5ee5c
MS
11027 }
11028 else
6dfbb909 11029 decl = build_decl (TYPE_DECL, declarator, type);
d2e5ee5c 11030
9188c363
MM
11031 /* If the user declares "typedef struct {...} foo" then the
11032 struct will have an anonymous name. Fill that name in now.
11033 Nothing can refer to it, so nothing needs know about the name
11034 change. */
8d08fdba 11035 if (type != error_mark_node
2bdb0643 11036 && declarator
8d08fdba
MS
11037 && TYPE_NAME (type)
11038 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
b537d120
MM
11039 && ANON_AGGRNAME_P (TYPE_IDENTIFIER (type))
11040 && CP_TYPE_QUALS (type) == TYPE_UNQUALIFIED)
8d08fdba 11041 {
dcd08efc
JM
11042 tree oldname = TYPE_NAME (type);
11043 tree t;
11044
2c73f9f5 11045 /* Replace the anonymous name with the real name everywhere. */
8d08fdba 11046 lookup_tag_reverse (type, declarator);
dcd08efc
JM
11047 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
11048 if (TYPE_NAME (t) == oldname)
11049 TYPE_NAME (t) = decl;
8d08fdba
MS
11050
11051 if (TYPE_LANG_SPECIFIC (type))
11052 TYPE_WAS_ANONYMOUS (type) = 1;
11053
33964bf4
MM
11054 /* If this is a typedef within a template class, the nested
11055 type is a (non-primary) template. The name for the
11056 template needs updating as well. */
11057 if (TYPE_LANG_SPECIFIC (type) && CLASSTYPE_TEMPLATE_INFO (type))
68642fb6 11058 DECL_NAME (CLASSTYPE_TI_TEMPLATE (type))
33964bf4
MM
11059 = TYPE_IDENTIFIER (type);
11060
50a6dbd7
JM
11061 /* FIXME remangle member functions; member functions of a
11062 type with external linkage have external linkage. */
fc378698 11063 }
fc378698 11064
8d08fdba
MS
11065 if (TREE_CODE (type) == OFFSET_TYPE || TREE_CODE (type) == METHOD_TYPE)
11066 {
8251199e 11067 cp_error_at ("typedef name may not be class-qualified", decl);
a3203465 11068 return NULL_TREE;
8d08fdba
MS
11069 }
11070 else if (quals)
11071 {
11072 if (ctype == NULL_TREE)
11073 {
11074 if (TREE_CODE (type) != METHOD_TYPE)
cb9a3ff8 11075 cp_error_at ("invalid type qualifier for non-member function type", decl);
8d08fdba
MS
11076 else
11077 ctype = TYPE_METHOD_BASETYPE (type);
11078 }
11079 if (ctype != NULL_TREE)
11080 grok_method_quals (ctype, decl, quals);
11081 }
11082
11083 if (RIDBIT_SETP (RID_SIGNED, specbits)
11084 || (typedef_decl && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl)))
11085 C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
11086
c91a56d2
MS
11087 bad_specifiers (decl, "type", virtualp, quals != NULL_TREE,
11088 inlinep, friendp, raises != NULL_TREE);
11089
11090 if (initialized)
8251199e 11091 error ("typedef declaration includes an initializer");
c91a56d2 11092
8d08fdba
MS
11093 return decl;
11094 }
11095
11096 /* Detect the case of an array type of unspecified size
11097 which came, as such, direct from a typedef name.
11098 We must copy the type, so that each identifier gets
11099 a distinct type, so that each identifier's size can be
11100 controlled separately by its own initializer. */
11101
11102 if (type == typedef_type && TREE_CODE (type) == ARRAY_TYPE
11103 && TYPE_DOMAIN (type) == NULL_TREE)
11104 {
11105 type = build_cplus_array_type (TREE_TYPE (type), TYPE_DOMAIN (type));
11106 }
11107
11108 /* If this is a type name (such as, in a cast or sizeof),
11109 compute the type and return it now. */
11110
11111 if (decl_context == TYPENAME)
11112 {
11113 /* Note that the grammar rejects storage classes
11114 in typenames, fields or parameters. */
91063b51 11115 if (type_quals != TYPE_UNQUALIFIED)
6eabb241 11116 type_quals = TYPE_UNQUALIFIED;
8d08fdba
MS
11117
11118 /* Special case: "friend class foo" looks like a TYPENAME context. */
11119 if (friendp)
11120 {
91063b51 11121 if (type_quals != TYPE_UNQUALIFIED)
b7484fbe 11122 {
91063b51
MM
11123 cp_error ("type qualifiers specified for friend class declaration");
11124 type_quals = TYPE_UNQUALIFIED;
b7484fbe
MS
11125 }
11126 if (inlinep)
11127 {
8251199e 11128 cp_error ("`inline' specified for friend class declaration");
b7484fbe
MS
11129 inlinep = 0;
11130 }
f2ae0c45
JM
11131
11132 /* Until core issue 180 is resolved, allow 'friend typename A::B'.
c00996a3 11133 But don't allow implicit typenames except with a class-key. */
f2ae0c45
JM
11134 if (!current_aggr && (TREE_CODE (type) != TYPENAME_TYPE
11135 || IMPLICIT_TYPENAME_P (type)))
4b054b80
NS
11136 {
11137 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
f2ae0c45
JM
11138 cp_pedwarn ("template parameters cannot be friends");
11139 else if (TREE_CODE (type) == TYPENAME_TYPE)
11140 cp_pedwarn ("\
11141friend declaration requires class-key, i.e. `friend class %T::%T'",
11142 constructor_name (current_class_type),
11143 TYPE_IDENTIFIER (type));
4b054b80 11144 else
f2ae0c45
JM
11145 cp_pedwarn ("\
11146friend declaration requires class-key, i.e. `friend %#T'",
11147 type);
4b054b80 11148 }
b7484fbe
MS
11149
11150 /* Only try to do this stuff if we didn't already give up. */
11151 if (type != integer_type_node)
11152 {
788bf0e3
NS
11153 /* DR 209. The friendly class does not need to be accessible
11154 in the scope of the class granting friendship. */
11155 skip_type_access_control ();
d363e7bf 11156
b7484fbe
MS
11157 /* A friendly class? */
11158 if (current_class_type)
11159 make_friend_class (current_class_type, TYPE_MAIN_VARIANT (type));
11160 else
2ae7bada
NS
11161 cp_error ("trying to make class `%T' a friend of global scope",
11162 type);
d363e7bf 11163
b7484fbe
MS
11164 type = void_type_node;
11165 }
8d08fdba
MS
11166 }
11167 else if (quals)
11168 {
8d08fdba
MS
11169 if (ctype == NULL_TREE)
11170 {
41cbc04c
NS
11171 if (TREE_CODE (type) != METHOD_TYPE)
11172 cp_error ("invalid qualifiers on non-member function type");
11173 else
11174 ctype = TYPE_METHOD_BASETYPE (type);
11175 }
11176 if (ctype)
11177 {
11178 tree dummy = build_decl (TYPE_DECL, declarator, type);
11179 grok_method_quals (ctype, dummy, quals);
11180 type = TREE_TYPE (dummy);
8d08fdba 11181 }
8d08fdba
MS
11182 }
11183
11184 return type;
11185 }
11186 else if (declarator == NULL_TREE && decl_context != PARM
db5ae43f 11187 && decl_context != CATCHPARM
8d08fdba
MS
11188 && TREE_CODE (type) != UNION_TYPE
11189 && ! bitfield)
11190 {
8251199e 11191 cp_error ("abstract declarator `%T' used as declaration", type);
8d08fdba
MS
11192 declarator = make_anon_name ();
11193 }
11194
11195 /* `void' at top level (not within pointer)
11196 is allowed only in typedefs or type names.
11197 We don't complain about parms either, but that is because
11198 a better error message can be made later. */
11199
a1774733 11200 if (TREE_CODE (type) == VOID_TYPE && decl_context != PARM)
8d08fdba 11201 {
b7484fbe 11202 if (! declarator)
8251199e 11203 error ("unnamed variable or field declared void");
b7484fbe 11204 else if (TREE_CODE (declarator) == IDENTIFIER_NODE)
8d08fdba
MS
11205 {
11206 if (IDENTIFIER_OPNAME_P (declarator))
8d08fdba 11207 my_friendly_abort (356);
8d08fdba 11208 else
8251199e 11209 error ("variable or field `%s' declared void", name);
8d08fdba
MS
11210 }
11211 else
8251199e 11212 error ("variable or field declared void");
8d08fdba
MS
11213 type = integer_type_node;
11214 }
11215
11216 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
11217 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
11218
14ae7e7d
JM
11219 if (decl_context == PARM || decl_context == CATCHPARM)
11220 {
11221 if (ctype || in_namespace)
11222 error ("cannot use `::' in parameter declaration");
11223
11224 /* A parameter declared as an array of T is really a pointer to T.
11225 One declared as a function is really a pointer to a function.
11226 One declared as a member is really a pointer to member. */
11227
11228 if (TREE_CODE (type) == ARRAY_TYPE)
11229 {
11230 /* Transfer const-ness of array into that of type pointed to. */
11231 type = build_pointer_type (TREE_TYPE (type));
91063b51 11232 type_quals = TYPE_UNQUALIFIED;
14ae7e7d
JM
11233 }
11234 else if (TREE_CODE (type) == FUNCTION_TYPE)
11235 type = build_pointer_type (type);
11236 else if (TREE_CODE (type) == OFFSET_TYPE)
11237 type = build_pointer_type (type);
14ae7e7d 11238 }
68642fb6 11239
8d08fdba
MS
11240 {
11241 register tree decl;
11242
11243 if (decl_context == PARM)
11244 {
ff350acd 11245 decl = build_decl (PARM_DECL, declarator, type);
8d08fdba
MS
11246
11247 bad_specifiers (decl, "parameter", virtualp, quals != NULL_TREE,
11248 inlinep, friendp, raises != NULL_TREE);
8d08fdba
MS
11249
11250 /* Compute the type actually passed in the parmlist,
11251 for the case where there is no prototype.
11252 (For example, shorts and chars are passed as ints.)
11253 When there is a prototype, this is overridden later. */
11254
39211cd5 11255 DECL_ARG_TYPE (decl) = type_promotes_to (type);
8d08fdba
MS
11256 }
11257 else if (decl_context == FIELD)
11258 {
11259 if (type == error_mark_node)
11260 {
11261 /* Happens when declaring arrays of sizes which
11262 are error_mark_node, for example. */
11263 decl = NULL_TREE;
11264 }
997a088c 11265 else if (in_namespace && !friendp)
05008fb9
MM
11266 {
11267 /* Something like struct S { int N::j; }; */
8251199e 11268 cp_error ("invalid use of `::'");
05008fb9
MM
11269 decl = NULL_TREE;
11270 }
8d08fdba
MS
11271 else if (TREE_CODE (type) == FUNCTION_TYPE)
11272 {
11273 int publicp = 0;
e1467ff2 11274 tree function_context;
8d08fdba 11275
72b7eeff
MS
11276 /* We catch the others as conflicts with the builtin
11277 typedefs. */
11278 if (friendp && declarator == ridpointers[(int) RID_SIGNED])
11279 {
8251199e 11280 cp_error ("function `%D' cannot be declared friend",
72b7eeff
MS
11281 declarator);
11282 friendp = 0;
11283 }
11284
8d08fdba
MS
11285 if (friendp == 0)
11286 {
11287 if (ctype == NULL_TREE)
11288 ctype = current_class_type;
11289
11290 if (ctype == NULL_TREE)
11291 {
8251199e 11292 cp_error ("can't make `%D' into a method -- not in a class",
8d08fdba
MS
11293 declarator);
11294 return void_type_node;
11295 }
11296
11297 /* ``A union may [ ... ] not [ have ] virtual functions.''
11298 ARM 9.5 */
11299 if (virtualp && TREE_CODE (ctype) == UNION_TYPE)
11300 {
8251199e 11301 cp_error ("function `%D' declared virtual inside a union",
8d08fdba
MS
11302 declarator);
11303 return void_type_node;
11304 }
11305
596ea4e5
AS
11306 if (declarator == ansi_opname (NEW_EXPR)
11307 || declarator == ansi_opname (VEC_NEW_EXPR)
11308 || declarator == ansi_opname (DELETE_EXPR)
11309 || declarator == ansi_opname (VEC_DELETE_EXPR))
8d08fdba
MS
11310 {
11311 if (virtualp)
11312 {
8251199e 11313 cp_error ("`%D' cannot be declared virtual, since it is always static",
8d08fdba
MS
11314 declarator);
11315 virtualp = 0;
11316 }
11317 }
11318 else if (staticp < 2)
14ae7e7d
JM
11319 type = build_cplus_method_type (ctype, TREE_TYPE (type),
11320 TYPE_ARG_TYPES (type));
8d08fdba
MS
11321 }
11322
11323 /* Tell grokfndecl if it needs to set TREE_PUBLIC on the node. */
68642fb6 11324 function_context = (ctype != NULL_TREE) ?
4f1c5b7d 11325 decl_function_context (TYPE_MAIN_DECL (ctype)) : NULL_TREE;
e1467ff2
MM
11326 publicp = (! friendp || ! staticp)
11327 && function_context == NULL_TREE;
68642fb6 11328 decl = grokfndecl (ctype, type,
386b8a85
JM
11329 TREE_CODE (declarator) != TEMPLATE_ID_EXPR
11330 ? declarator : dname,
11331 declarator,
7a8f9fa9 11332 virtualp, flags, quals, raises,
386b8a85 11333 friendp ? -1 : 0, friendp, publicp, inlinep,
2c73f9f5 11334 funcdef_flag, template_count, in_namespace);
20496fa2 11335 if (decl == NULL_TREE)
3ddfb0e6 11336 return decl;
6125f3be
DE
11337#if 0
11338 /* This clobbers the attrs stored in `decl' from `attrlist'. */
11339 /* The decl and setting of decl_machine_attr is also turned off. */
71851aaa 11340 decl = build_decl_attribute_variant (decl, decl_machine_attr);
6125f3be 11341#endif
f0e01782 11342
cc804e51
MM
11343 /* [class.conv.ctor]
11344
11345 A constructor declared without the function-specifier
11346 explicit that can be called with a single parameter
11347 specifies a conversion from the type of its first
11348 parameter to the type of its class. Such a constructor
11349 is called a converting constructor. */
db5ae43f
MS
11350 if (explicitp == 2)
11351 DECL_NONCONVERTING_P (decl) = 1;
cc804e51
MM
11352 else if (DECL_CONSTRUCTOR_P (decl))
11353 {
11354 /* The constructor can be called with exactly one
11355 parameter if there is at least one parameter, and
11356 any subsequent parameters have default arguments.
e0fff4b3
JM
11357 Ignore any compiler-added parms. */
11358 tree arg_types = FUNCTION_FIRST_USER_PARMTYPE (decl);
cc804e51
MM
11359
11360 if (arg_types == void_list_node
68642fb6
UD
11361 || (arg_types
11362 && TREE_CHAIN (arg_types)
cc804e51
MM
11363 && TREE_CHAIN (arg_types) != void_list_node
11364 && !TREE_PURPOSE (TREE_CHAIN (arg_types))))
11365 DECL_NONCONVERTING_P (decl) = 1;
11366 }
8d08fdba
MS
11367 }
11368 else if (TREE_CODE (type) == METHOD_TYPE)
11369 {
faae18ab
MS
11370 /* We only get here for friend declarations of
11371 members of other classes. */
8d08fdba
MS
11372 /* All method decls are public, so tell grokfndecl to set
11373 TREE_PUBLIC, also. */
386b8a85 11374 decl = grokfndecl (ctype, type, declarator, declarator,
7a8f9fa9 11375 virtualp, flags, quals, raises,
386b8a85 11376 friendp ? -1 : 0, friendp, 1, 0, funcdef_flag,
2c73f9f5 11377 template_count, in_namespace);
f0e01782
MS
11378 if (decl == NULL_TREE)
11379 return NULL_TREE;
8d08fdba 11380 }
5566b478 11381 else if (!staticp && ! processing_template_decl
d0f062fb 11382 && !COMPLETE_TYPE_P (complete_type (type))
8d08fdba
MS
11383 && (TREE_CODE (type) != ARRAY_TYPE || initialized == 0))
11384 {
b7484fbe 11385 if (declarator)
8251199e 11386 cp_error ("field `%D' has incomplete type", declarator);
b7484fbe 11387 else
8251199e 11388 cp_error ("name `%T' has incomplete type", type);
8d08fdba
MS
11389
11390 /* If we're instantiating a template, tell them which
11391 instantiation made the field's type be incomplete. */
11392 if (current_class_type
11393 && TYPE_NAME (current_class_type)
d2e5ee5c 11394 && IDENTIFIER_TEMPLATE (TYPE_IDENTIFIER (current_class_type))
8d08fdba
MS
11395 && declspecs && TREE_VALUE (declspecs)
11396 && TREE_TYPE (TREE_VALUE (declspecs)) == type)
8251199e 11397 cp_error (" in instantiation of template `%T'",
db5ae43f
MS
11398 current_class_type);
11399
8d08fdba
MS
11400 type = error_mark_node;
11401 decl = NULL_TREE;
11402 }
11403 else
11404 {
11405 if (friendp)
11406 {
cb9a3ff8 11407 error ("`%s' is neither function nor member function; cannot be declared friend",
8d08fdba
MS
11408 IDENTIFIER_POINTER (declarator));
11409 friendp = 0;
11410 }
11411 decl = NULL_TREE;
11412 }
11413
11414 if (friendp)
11415 {
11416 /* Friends are treated specially. */
788bf0e3
NS
11417 tree t = NULL_TREE;
11418
11419 /* DR 209. The friend does not need to be accessible at this
11420 point. */
11421 skip_type_access_control ();
11422
8d08fdba 11423 if (ctype == current_class_type)
8251199e 11424 warning ("member functions are implicitly friends of their class");
6c30752f 11425
788bf0e3
NS
11426 if (decl && DECL_NAME (decl))
11427 {
11428 if (template_class_depth (current_class_type) == 0)
11429 {
11430 decl = check_explicit_specialization
11431 (declarator, decl,
11432 template_count, 2 * (funcdef_flag != 0) + 4);
11433 if (decl == error_mark_node)
11434 return error_mark_node;
11435 }
11436
11437 t = do_friend (ctype, declarator, decl,
11438 last_function_parms, attrlist, flags, quals,
11439 funcdef_flag);
11440 }
11441 if (t && funcdef_flag)
11442 return t;
11443 return void_type_node;
8d08fdba
MS
11444 }
11445
11446 /* Structure field. It may not be a function, except for C++ */
11447
11448 if (decl == NULL_TREE)
11449 {
8d08fdba
MS
11450 if (initialized)
11451 {
3ac3d9ea
MM
11452 if (!staticp)
11453 {
11454 /* An attempt is being made to initialize a non-static
11455 member. But, from [class.mem]:
68642fb6 11456
3ac3d9ea
MM
11457 4 A member-declarator can contain a
11458 constant-initializer only if it declares a static
11459 member (_class.static_) of integral or enumeration
68642fb6 11460 type, see _class.static.data_.
3ac3d9ea
MM
11461
11462 This used to be relatively common practice, but
11463 the rest of the compiler does not correctly
11464 handle the initialization unless the member is
11465 static so we make it static below. */
cab1f180 11466 cp_pedwarn ("ISO C++ forbids initialization of member `%D'",
3ac3d9ea 11467 declarator);
8251199e 11468 cp_pedwarn ("making `%D' static", declarator);
3ac3d9ea
MM
11469 staticp = 1;
11470 }
11471
6ba89f8e
MM
11472 if (uses_template_parms (type))
11473 /* We'll check at instantiation time. */
11474 ;
11475 else if (check_static_variable_definition (declarator,
11476 type))
11477 /* If we just return the declaration, crashes
11478 will sometimes occur. We therefore return
72a93143
JM
11479 void_type_node, as if this was a friend
11480 declaration, to cause callers to completely
11481 ignore this declaration. */
6ba89f8e 11482 return void_type_node;
8d08fdba
MS
11483 }
11484
8ebeee52 11485 /* 9.2p13 [class.mem] */
4d7614e9 11486 if (declarator == constructor_name (current_class_type)
e54930f9
JM
11487 /* The standard does not allow non-static data members
11488 here either, but we agreed at the 10/99 meeting
11489 to change that in TC 1 so that they are allowed in
11490 classes with no user-defined constructors. */
11491 && staticp)
11492 cp_pedwarn ("ISO C++ forbids static data member `%D' with same name as enclosing class",
8ebeee52
JM
11493 declarator);
11494
3ac3d9ea 11495 if (staticp)
8d08fdba 11496 {
f18a14bc
MM
11497 /* C++ allows static class members. All other work
11498 for this is done by grokfield. */
4ce3d537 11499 decl = build_lang_decl (VAR_DECL, declarator, type);
5b605f68
MS
11500 TREE_STATIC (decl) = 1;
11501 /* In class context, 'static' means public access. */
3ac3d9ea 11502 TREE_PUBLIC (decl) = DECL_EXTERNAL (decl) = 1;
8d08fdba
MS
11503 }
11504 else
11505 {
721c3b42 11506 decl = build_decl (FIELD_DECL, declarator, type);
2bf105ab 11507 DECL_NONADDRESSABLE_P (decl) = bitfield;
8d08fdba
MS
11508 if (RIDBIT_SETP (RID_MUTABLE, specbits))
11509 {
11510 DECL_MUTABLE_P (decl) = 1;
11511 RIDBIT_RESET (RID_MUTABLE, specbits);
11512 }
11513 }
11514
11515 bad_specifiers (decl, "field", virtualp, quals != NULL_TREE,
11516 inlinep, friendp, raises != NULL_TREE);
11517 }
11518 }
11519 else if (TREE_CODE (type) == FUNCTION_TYPE || TREE_CODE (type) == METHOD_TYPE)
11520 {
386b8a85 11521 tree original_name;
8d08fdba
MS
11522 int publicp = 0;
11523
11524 if (! declarator)
11525 return NULL_TREE;
11526
386b8a85
JM
11527 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
11528 original_name = dname;
11529 else
11530 original_name = declarator;
11531
8926095f 11532 if (RIDBIT_SETP (RID_AUTO, specbits))
8251199e 11533 error ("storage class `auto' invalid for function `%s'", name);
8926095f 11534 else if (RIDBIT_SETP (RID_REGISTER, specbits))
8251199e 11535 error ("storage class `register' invalid for function `%s'", name);
8d08fdba
MS
11536
11537 /* Function declaration not at top level.
11538 Storage classes other than `extern' are not allowed
11539 and `extern' makes no difference. */
a9aedbc2 11540 if (! toplevel_bindings_p ()
8926095f
MS
11541 && (RIDBIT_SETP (RID_STATIC, specbits)
11542 || RIDBIT_SETP (RID_INLINE, specbits))
8d08fdba 11543 && pedantic)
8926095f
MS
11544 {
11545 if (RIDBIT_SETP (RID_STATIC, specbits))
8251199e 11546 pedwarn ("storage class `static' invalid for function `%s' declared out of global scope", name);
8926095f 11547 else
8251199e 11548 pedwarn ("storage class `inline' invalid for function `%s' declared out of global scope", name);
8926095f 11549 }
68642fb6 11550
8d08fdba
MS
11551 if (ctype == NULL_TREE)
11552 {
11553 if (virtualp)
11554 {
8251199e 11555 error ("virtual non-class function `%s'", name);
8d08fdba
MS
11556 virtualp = 0;
11557 }
8d08fdba
MS
11558 }
11559 else if (TREE_CODE (type) == FUNCTION_TYPE && staticp < 2)
14ae7e7d
JM
11560 type = build_cplus_method_type (ctype, TREE_TYPE (type),
11561 TYPE_ARG_TYPES (type));
8d08fdba 11562
eb66be0e 11563 /* Record presence of `static'. */
faae18ab 11564 publicp = (ctype != NULL_TREE
a9aedbc2 11565 || RIDBIT_SETP (RID_EXTERN, specbits)
eb66be0e 11566 || !RIDBIT_SETP (RID_STATIC, specbits));
8d08fdba 11567
386b8a85 11568 decl = grokfndecl (ctype, type, original_name, declarator,
7a8f9fa9 11569 virtualp, flags, quals, raises,
75650646 11570 1, friendp,
68642fb6 11571 publicp, inlinep, funcdef_flag,
2c73f9f5 11572 template_count, in_namespace);
f0e01782
MS
11573 if (decl == NULL_TREE)
11574 return NULL_TREE;
8d08fdba 11575
8d08fdba
MS
11576 if (staticp == 1)
11577 {
11578 int illegal_static = 0;
11579
11580 /* Don't allow a static member function in a class, and forbid
11581 declaring main to be static. */
11582 if (TREE_CODE (type) == METHOD_TYPE)
11583 {
8251199e 11584 cp_pedwarn ("cannot declare member function `%D' to have static linkage", decl);
8d08fdba
MS
11585 illegal_static = 1;
11586 }
8d08fdba
MS
11587 else if (current_function_decl)
11588 {
11589 /* FIXME need arm citation */
8251199e 11590 error ("cannot declare static function inside another function");
8d08fdba
MS
11591 illegal_static = 1;
11592 }
11593
11594 if (illegal_static)
11595 {
11596 staticp = 0;
11597 RIDBIT_RESET (RID_STATIC, specbits);
11598 }
11599 }
8d08fdba
MS
11600 }
11601 else
11602 {
11603 /* It's a variable. */
11604
11605 /* An uninitialized decl with `extern' is a reference. */
68642fb6
UD
11606 decl = grokvardecl (type, declarator, &specbits,
11607 initialized,
11608 (type_quals & TYPE_QUAL_CONST) != 0,
950ad3c3 11609 in_namespace);
8d08fdba
MS
11610 bad_specifiers (decl, "variable", virtualp, quals != NULL_TREE,
11611 inlinep, friendp, raises != NULL_TREE);
11612
11613 if (ctype)
11614 {
f0e01782 11615 DECL_CONTEXT (decl) = ctype;
8d08fdba
MS
11616 if (staticp == 1)
11617 {
8251199e 11618 cp_pedwarn ("static member `%D' re-declared as static", decl);
8d08fdba
MS
11619 staticp = 0;
11620 RIDBIT_RESET (RID_STATIC, specbits);
11621 }
b7484fbe
MS
11622 if (RIDBIT_SETP (RID_REGISTER, specbits) && TREE_STATIC (decl))
11623 {
8251199e 11624 cp_error ("static member `%D' declared `register'", decl);
b7484fbe
MS
11625 RIDBIT_RESET (RID_REGISTER, specbits);
11626 }
f30432d7 11627 if (RIDBIT_SETP (RID_EXTERN, specbits) && pedantic)
8d08fdba 11628 {
8251199e 11629 cp_pedwarn ("cannot explicitly declare member `%#D' to have extern linkage",
f30432d7 11630 decl);
8d08fdba
MS
11631 RIDBIT_RESET (RID_EXTERN, specbits);
11632 }
11633 }
11634 }
11635
34fc957d 11636 my_friendly_assert (!RIDBIT_SETP (RID_MUTABLE, specbits), 19990927);
8d08fdba
MS
11637
11638 /* Record `register' declaration for warnings on &
11639 and in case doing stupid register allocation. */
11640
11641 if (RIDBIT_SETP (RID_REGISTER, specbits))
11642 DECL_REGISTER (decl) = 1;
11643
8926095f
MS
11644 if (RIDBIT_SETP (RID_EXTERN, specbits))
11645 DECL_THIS_EXTERN (decl) = 1;
11646
faae18ab
MS
11647 if (RIDBIT_SETP (RID_STATIC, specbits))
11648 DECL_THIS_STATIC (decl) = 1;
11649
adecb3f4
MM
11650 /* Record constancy and volatility. There's no need to do this
11651 when processing a template; we'll do this for the instantiated
11652 declaration based on the type of DECL. */
11653 if (!processing_template_decl)
11654 c_apply_type_quals_to_decl (type_quals, decl);
8d08fdba
MS
11655
11656 return decl;
11657 }
11658}
11659\f
11660/* Tell if a parmlist/exprlist looks like an exprlist or a parmlist.
11661 An empty exprlist is a parmlist. An exprlist which
11662 contains only identifiers at the global level
11663 is a parmlist. Otherwise, it is an exprlist. */
e92cc029 11664
8d08fdba
MS
11665int
11666parmlist_is_exprlist (exprs)
11667 tree exprs;
11668{
11669 if (exprs == NULL_TREE || TREE_PARMLIST (exprs))
11670 return 0;
11671
a9aedbc2 11672 if (toplevel_bindings_p ())
8d08fdba
MS
11673 {
11674 /* At the global level, if these are all identifiers,
11675 then it is a parmlist. */
11676 while (exprs)
11677 {
11678 if (TREE_CODE (TREE_VALUE (exprs)) != IDENTIFIER_NODE)
11679 return 1;
11680 exprs = TREE_CHAIN (exprs);
11681 }
11682 return 0;
11683 }
11684 return 1;
11685}
11686
f181d4ae
MM
11687/* Subroutine of start_function. Ensure that each of the parameter
11688 types (as listed in PARMS) is complete, as is required for a
11689 function definition. */
e92cc029 11690
8d08fdba
MS
11691static void
11692require_complete_types_for_parms (parms)
11693 tree parms;
11694{
07c88314 11695 for (; parms; parms = TREE_CHAIN (parms))
8d08fdba 11696 {
5cce22b6
NS
11697 if (VOID_TYPE_P (TREE_TYPE (parms)))
11698 /* grokparms will have already issued an error */
11699 TREE_TYPE (parms) = error_mark_node;
11700 else if (complete_type_or_else (TREE_TYPE (parms), parms))
42f989ff 11701 layout_decl (parms, 0);
5cce22b6
NS
11702 else
11703 TREE_TYPE (parms) = error_mark_node;
8d08fdba
MS
11704 }
11705}
11706
46e8c075 11707/* Returns non-zero if T is a local variable. */
297e73d8 11708
46e8c075
MM
11709int
11710local_variable_p (t)
11711 tree t;
297e73d8 11712{
68642fb6 11713 if ((TREE_CODE (t) == VAR_DECL
297e73d8
MM
11714 /* A VAR_DECL with a context that is a _TYPE is a static data
11715 member. */
11716 && !TYPE_P (CP_DECL_CONTEXT (t))
11717 /* Any other non-local variable must be at namespace scope. */
46e8c075 11718 && !DECL_NAMESPACE_SCOPE_P (t))
297e73d8 11719 || (TREE_CODE (t) == PARM_DECL))
46e8c075 11720 return 1;
297e73d8 11721
46e8c075
MM
11722 return 0;
11723}
11724
11725/* Returns non-zero if T is an automatic local variable or a label.
11726 (These are the declarations that need to be remapped when the code
11727 containing them is duplicated.) */
11728
11729int
11730nonstatic_local_decl_p (t)
11731 tree t;
11732{
11733 return ((local_variable_p (t) && !TREE_STATIC (t))
11734 || TREE_CODE (t) == LABEL_DECL
11735 || TREE_CODE (t) == RESULT_DECL);
11736}
11737
11738/* Like local_variable_p, but suitable for use as a tree-walking
11739 function. */
11740
11741static tree
11742local_variable_p_walkfn (tp, walk_subtrees, data)
11743 tree *tp;
11744 int *walk_subtrees ATTRIBUTE_UNUSED;
11745 void *data ATTRIBUTE_UNUSED;
11746{
68642fb6 11747 return ((local_variable_p (*tp) && !DECL_ARTIFICIAL (*tp))
46e8c075 11748 ? *tp : NULL_TREE);
297e73d8
MM
11749}
11750
11751/* Check that ARG, which is a default-argument expression for a
11752 parameter DECL, is legal. Returns ARG, or ERROR_MARK_NODE, if
11753 something goes wrong. DECL may also be a _TYPE node, rather than a
11754 DECL, if there is no DECL available. */
11755
11756tree
11757check_default_argument (decl, arg)
11758 tree decl;
11759 tree arg;
11760{
11761 tree var;
11762 tree decl_type;
11763
11764 if (TREE_CODE (arg) == DEFAULT_ARG)
11765 /* We get a DEFAULT_ARG when looking at an in-class declaration
11766 with a default argument. Ignore the argument for now; we'll
11767 deal with it after the class is complete. */
11768 return arg;
11769
11770 if (processing_template_decl || uses_template_parms (arg))
11771 /* We don't do anything checking until instantiation-time. Note
11772 that there may be uninstantiated arguments even for an
11773 instantiated function, since default arguments are not
11774 instantiated until they are needed. */
11775 return arg;
11776
11777 if (TYPE_P (decl))
11778 {
11779 decl_type = decl;
11780 decl = NULL_TREE;
11781 }
11782 else
11783 decl_type = TREE_TYPE (decl);
11784
68642fb6 11785 if (arg == error_mark_node
297e73d8
MM
11786 || decl == error_mark_node
11787 || TREE_TYPE (arg) == error_mark_node
11788 || decl_type == error_mark_node)
11789 /* Something already went wrong. There's no need to check
11790 further. */
11791 return error_mark_node;
11792
11793 /* [dcl.fct.default]
68642fb6 11794
297e73d8
MM
11795 A default argument expression is implicitly converted to the
11796 parameter type. */
11797 if (!TREE_TYPE (arg)
11798 || !can_convert_arg (decl_type, TREE_TYPE (arg), arg))
11799 {
11800 if (decl)
68642fb6 11801 cp_error ("default argument for `%#D' has type `%T'",
297e73d8
MM
11802 decl, TREE_TYPE (arg));
11803 else
8614a1d6 11804 cp_error ("default argument for parameter of type `%T' has type `%T'",
297e73d8
MM
11805 decl_type, TREE_TYPE (arg));
11806
11807 return error_mark_node;
11808 }
11809
11810 /* [dcl.fct.default]
11811
11812 Local variables shall not be used in default argument
68642fb6 11813 expressions.
297e73d8
MM
11814
11815 The keyword `this' shall not be used in a default argument of a
11816 member function. */
5362b086 11817 var = walk_tree_without_duplicates (&arg, local_variable_p_walkfn,
ee94fce6 11818 NULL);
297e73d8
MM
11819 if (var)
11820 {
11821 cp_error ("default argument `%E' uses local variable `%D'",
11822 arg, var);
11823 return error_mark_node;
11824 }
11825
11826 /* All is well. */
11827 return arg;
11828}
11829
8d08fdba
MS
11830/* Decode the list of parameter types for a function type.
11831 Given the list of things declared inside the parens,
11832 return a list of types.
11833
5cce22b6
NS
11834 We determine whether ellipsis parms are used by PARMLIST_ELLIPSIS_P
11835 flag. If unset, we append void_list_node. A parmlist declared
11836 as `(void)' is accepted as the empty parmlist.
11837
11838 Also set last_function_parms to the chain of PARM_DECLs. */
8d08fdba
MS
11839
11840static tree
5cce22b6 11841grokparms (first_parm)
8d08fdba 11842 tree first_parm;
8d08fdba
MS
11843{
11844 tree result = NULL_TREE;
11845 tree decls = NULL_TREE;
5cce22b6
NS
11846 int ellipsis = !first_parm || PARMLIST_ELLIPSIS_P (first_parm);
11847 tree parm, chain;
11848 int any_error = 0;
8d08fdba 11849
5cce22b6
NS
11850 my_friendly_assert (!first_parm || TREE_PARMLIST (first_parm), 20001115);
11851
11852 for (parm = first_parm; parm != NULL_TREE; parm = chain)
8d08fdba 11853 {
0657c69c 11854 tree type = NULL_TREE;
5cce22b6
NS
11855 register tree decl = TREE_VALUE (parm);
11856 tree init = TREE_PURPOSE (parm);
8d08fdba 11857
5cce22b6
NS
11858 chain = TREE_CHAIN (parm);
11859 /* @@ weak defense against parse errors. */
11860 if (TREE_CODE (decl) != VOID_TYPE
11861 && TREE_CODE (decl) != TREE_LIST)
8d08fdba 11862 {
5cce22b6
NS
11863 /* Give various messages as the need arises. */
11864 if (TREE_CODE (decl) == STRING_CST)
11865 cp_error ("invalid string constant `%E'", decl);
11866 else if (TREE_CODE (decl) == INTEGER_CST)
11867 error ("invalid integer constant in parameter list, did you forget to give parameter name?");
11868 continue;
11869 }
8d08fdba 11870
5cce22b6
NS
11871 if (parm == void_list_node)
11872 break;
8d08fdba 11873
5cce22b6 11874 decl = grokdeclarator (TREE_VALUE (decl), TREE_PURPOSE (decl),
d363e7bf 11875 PARM, init != NULL_TREE, NULL_TREE);
5cce22b6
NS
11876 if (! decl || TREE_TYPE (decl) == error_mark_node)
11877 continue;
d363e7bf 11878
5cce22b6
NS
11879 type = TREE_TYPE (decl);
11880 if (VOID_TYPE_P (type))
11881 {
11882 if (same_type_p (type, void_type_node)
11883 && !DECL_NAME (decl) && !result && !chain && !ellipsis)
11884 /* this is a parmlist of `(void)', which is ok. */
11885 break;
11886 incomplete_type_error (decl, type);
0657c69c
MM
11887 /* It's not a good idea to actually create parameters of
11888 type `void'; other parts of the compiler assume that a
11889 void type terminates the parameter list. */
04f3dc2b 11890 type = error_mark_node;
0657c69c 11891 TREE_TYPE (decl) = error_mark_node;
5cce22b6 11892 }
8d08fdba 11893
d363e7bf 11894 if (type != error_mark_node)
04f3dc2b
MM
11895 {
11896 /* Top-level qualifiers on the parameters are
11897 ignored for function types. */
11898 type = TYPE_MAIN_VARIANT (type);
11899 if (TREE_CODE (type) == METHOD_TYPE)
11900 {
11901 cp_error ("parameter `%D' invalidly declared method type", decl);
11902 type = build_pointer_type (type);
11903 TREE_TYPE (decl) = type;
11904 }
11905 else if (TREE_CODE (type) == OFFSET_TYPE)
11906 {
11907 cp_error ("parameter `%D' invalidly declared offset type", decl);
11908 type = build_pointer_type (type);
11909 TREE_TYPE (decl) = type;
11910 }
11911 else if (abstract_virtuals_error (decl, type))
11912 any_error = 1; /* Seems like a good idea. */
11913 else if (POINTER_TYPE_P (type))
11914 {
11915 /* [dcl.fct]/6, parameter types cannot contain pointers
11916 (references) to arrays of unknown bound. */
98979fe0
NS
11917 tree t = TREE_TYPE (type);
11918 int ptr = TYPE_PTR_P (type);
11919
11920 while (1)
11921 {
11922 if (TYPE_PTR_P (t))
11923 ptr = 1;
11924 else if (TREE_CODE (t) != ARRAY_TYPE)
11925 break;
11926 else if (!TYPE_DOMAIN (t))
11927 break;
11928 t = TREE_TYPE (t);
11929 }
04f3dc2b
MM
11930 if (TREE_CODE (t) == ARRAY_TYPE)
11931 cp_error ("parameter `%D' includes %s to array of unknown bound `%T'",
98979fe0 11932 decl, ptr ? "pointer" : "reference", t);
04f3dc2b
MM
11933 }
11934
11935 DECL_ARG_TYPE (decl) = TREE_TYPE (decl);
11936 if (PROMOTE_PROTOTYPES
11937 && (TREE_CODE (type) == INTEGER_TYPE
11938 || TREE_CODE (type) == ENUMERAL_TYPE)
11939 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
11940 DECL_ARG_TYPE (decl) = integer_type_node;
11941 if (!any_error && init)
11942 init = check_default_argument (decl, init);
11943 else
11944 init = NULL_TREE;
11945 }
8d08fdba 11946
5cce22b6
NS
11947 TREE_CHAIN (decl) = decls;
11948 decls = decl;
0657c69c 11949 result = tree_cons (init, type, result);
8d08fdba 11950 }
5cce22b6
NS
11951 decls = nreverse (decls);
11952 result = nreverse (result);
11953 if (!ellipsis)
11954 result = chainon (result, void_list_node);
8d08fdba
MS
11955 last_function_parms = decls;
11956
8d08fdba
MS
11957 return result;
11958}
42976354 11959
8d08fdba 11960\f
4f1c5b7d
MM
11961/* D is a constructor or overloaded `operator='. Returns non-zero if
11962 D's arguments allow it to be a copy constructor, or copy assignment
11963 operator. */
11964
c11b6f21
MS
11965int
11966copy_args_p (d)
11967 tree d;
11968{
4f1c5b7d
MM
11969 tree t;
11970
11971 if (!DECL_FUNCTION_MEMBER_P (d))
11972 return 0;
11973
e0fff4b3 11974 t = FUNCTION_FIRST_USER_PARMTYPE (d);
c11b6f21
MS
11975 if (t && TREE_CODE (TREE_VALUE (t)) == REFERENCE_TYPE
11976 && (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_VALUE (t)))
4f1c5b7d 11977 == DECL_CONTEXT (d))
c11b6f21
MS
11978 && (TREE_CHAIN (t) == NULL_TREE
11979 || TREE_CHAIN (t) == void_list_node
11980 || TREE_PURPOSE (TREE_CHAIN (t))))
11981 return 1;
11982 return 0;
11983}
11984
8d08fdba
MS
11985/* These memoizing functions keep track of special properties which
11986 a class may have. `grok_ctor_properties' notices whether a class
11987 has a constructor of the form X(X&), and also complains
11988 if the class has a constructor of the form X(X).
11989 `grok_op_properties' takes notice of the various forms of
11990 operator= which are defined, as well as what sorts of type conversion
11991 may apply. Both functions take a FUNCTION_DECL as an argument. */
e92cc029 11992
a0a33927 11993int
8d08fdba
MS
11994grok_ctor_properties (ctype, decl)
11995 tree ctype, decl;
11996{
e0fff4b3 11997 tree parmtypes = FUNCTION_FIRST_USER_PARMTYPE (decl);
8d08fdba
MS
11998 tree parmtype = parmtypes ? TREE_VALUE (parmtypes) : void_type_node;
11999
f181d4ae
MM
12000 /* [class.copy]
12001
12002 A non-template constructor for class X is a copy constructor if
12003 its first parameter is of type X&, const X&, volatile X& or const
12004 volatile X&, and either there are no other parameters or else all
12005 other parameters have default arguments. */
8d08fdba 12006 if (TREE_CODE (parmtype) == REFERENCE_TYPE
0b41abe6 12007 && TYPE_MAIN_VARIANT (TREE_TYPE (parmtype)) == ctype
a11d04b5 12008 && sufficient_parms_p (TREE_CHAIN (parmtypes))
f181d4ae
MM
12009 && !(DECL_TEMPLATE_INSTANTIATION (decl)
12010 && is_member_template (DECL_TI_TEMPLATE (decl))))
0b41abe6
JM
12011 {
12012 TYPE_HAS_INIT_REF (ctype) = 1;
91063b51 12013 if (CP_TYPE_CONST_P (TREE_TYPE (parmtype)))
0b41abe6 12014 TYPE_HAS_CONST_INIT_REF (ctype) = 1;
8d08fdba 12015 }
f181d4ae
MM
12016 /* [class.copy]
12017
12018 A declaration of a constructor for a class X is ill-formed if its
12019 first parameter is of type (optionally cv-qualified) X and either
12020 there are no other parameters or else all other parameters have
68642fb6 12021 default arguments.
f181d4ae
MM
12022
12023 We *don't* complain about member template instantiations that
12024 have this form, though; they can occur as we try to decide what
12025 constructor to use during overload resolution. Since overload
12026 resolution will never prefer such a constructor to the
12027 non-template copy constructor (which is either explicitly or
12028 implicitly defined), there's no need to worry about their
12029 existence. Theoretically, they should never even be
12030 instantiated, but that's hard to forestall. */
0b41abe6 12031 else if (TYPE_MAIN_VARIANT (parmtype) == ctype
a11d04b5 12032 && sufficient_parms_p (TREE_CHAIN (parmtypes))
f181d4ae
MM
12033 && !(DECL_TEMPLATE_INSTANTIATION (decl)
12034 && is_member_template (DECL_TI_TEMPLATE (decl))))
8d08fdba 12035 {
8251199e 12036 cp_error ("invalid constructor; you probably meant `%T (const %T&)'",
0b41abe6
JM
12037 ctype, ctype);
12038 SET_IDENTIFIER_ERROR_LOCUS (DECL_NAME (decl), ctype);
12039 return 0;
8d08fdba
MS
12040 }
12041 else if (TREE_CODE (parmtype) == VOID_TYPE
12042 || TREE_PURPOSE (parmtypes) != NULL_TREE)
12043 TYPE_HAS_DEFAULT_CONSTRUCTOR (ctype) = 1;
a0a33927
MS
12044
12045 return 1;
8d08fdba
MS
12046}
12047
596ea4e5 12048/* An operator with this code is unary, but can also be binary. */
e92cc029 12049
a28e3c7f 12050static int
596ea4e5
AS
12051ambi_op_p (code)
12052 enum tree_code code;
8d08fdba 12053{
596ea4e5
AS
12054 return (code == INDIRECT_REF
12055 || code == ADDR_EXPR
12056 || code == CONVERT_EXPR
12057 || code == NEGATE_EXPR
12058 || code == PREINCREMENT_EXPR
12059 || code == PREDECREMENT_EXPR);
8d08fdba
MS
12060}
12061
12062/* An operator with this name can only be unary. */
e92cc029 12063
a28e3c7f 12064static int
596ea4e5
AS
12065unary_op_p (code)
12066 enum tree_code code;
8d08fdba 12067{
596ea4e5
AS
12068 return (code == TRUTH_NOT_EXPR
12069 || code == BIT_NOT_EXPR
12070 || code == COMPONENT_REF
12071 || code == TYPE_EXPR);
8d08fdba
MS
12072}
12073
12074/* Do a little sanity-checking on how they declared their operator. */
e92cc029 12075
5566b478 12076void
8d08fdba
MS
12077grok_op_properties (decl, virtualp, friendp)
12078 tree decl;
12079 int virtualp, friendp;
12080{
12081 tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
596ea4e5 12082 tree argtype;
8d08fdba
MS
12083 int methodp = (TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE);
12084 tree name = DECL_NAME (decl);
596ea4e5
AS
12085 enum tree_code operator_code;
12086 int arity;
12087
12088 /* Count the number of arguments. */
12089 for (argtype = argtypes, arity = 0;
12090 argtype && argtype != void_list_node;
12091 argtype = TREE_CHAIN (argtype))
12092 ++arity;
8d08fdba 12093
a28e3c7f
MS
12094 if (current_class_type == NULL_TREE)
12095 friendp = 1;
8d08fdba 12096
596ea4e5
AS
12097 if (DECL_CONV_FN_P (decl))
12098 operator_code = TYPE_EXPR;
12099 else
12100 do
12101 {
669ec2b4
JM
12102#define DEF_OPERATOR(NAME, CODE, NEW_MANGLING, OLD_MANGING, ARITY, ASSN_P) \
12103 if (ansi_opname (CODE) == name) \
12104 { \
12105 operator_code = CODE; \
12106 break; \
12107 } \
12108 else if (ansi_assopname (CODE) == name) \
12109 { \
12110 operator_code = CODE; \
12111 DECL_ASSIGNMENT_OPERATOR_P (decl) = 1; \
12112 break; \
596ea4e5
AS
12113 }
12114
12115#include "operators.def"
12116#undef DEF_OPERATOR
12117
12118 my_friendly_abort (20000527);
12119 }
12120 while (0);
12121 my_friendly_assert (operator_code != LAST_CPLUS_TREE_CODE, 20000526);
12122 SET_OVERLOADED_OPERATOR_CODE (decl, operator_code);
12123
a28e3c7f
MS
12124 if (! friendp)
12125 {
596ea4e5
AS
12126 switch (operator_code)
12127 {
12128 case CALL_EXPR:
12129 TYPE_OVERLOADS_CALL_EXPR (current_class_type) = 1;
12130 break;
5362b086 12131
596ea4e5
AS
12132 case ARRAY_REF:
12133 TYPE_OVERLOADS_ARRAY_REF (current_class_type) = 1;
12134 break;
f181d4ae 12135
596ea4e5
AS
12136 case COMPONENT_REF:
12137 case MEMBER_REF:
12138 TYPE_OVERLOADS_ARROW (current_class_type) = 1;
12139 break;
5362b086 12140
596ea4e5
AS
12141 case NEW_EXPR:
12142 TYPE_HAS_NEW_OPERATOR (current_class_type) = 1;
12143 break;
5362b086 12144
596ea4e5
AS
12145 case DELETE_EXPR:
12146 TYPE_GETS_DELETE (current_class_type) |= 1;
12147 break;
5362b086 12148
596ea4e5
AS
12149 case VEC_NEW_EXPR:
12150 TYPE_HAS_ARRAY_NEW_OPERATOR (current_class_type) = 1;
12151 break;
5362b086 12152
596ea4e5
AS
12153 case VEC_DELETE_EXPR:
12154 TYPE_GETS_DELETE (current_class_type) |= 2;
12155 break;
12156
12157 default:
12158 break;
12159 }
12160 }
12161
12162 if (operator_code == NEW_EXPR || operator_code == VEC_NEW_EXPR)
8d08fdba 12163 {
8d08fdba
MS
12164 /* When the compiler encounters the definition of A::operator new, it
12165 doesn't look at the class declaration to find out if it's static. */
a28e3c7f 12166 if (methodp)
3afb32a4 12167 revert_static_member_fn (decl);
68642fb6 12168
36791f1e 12169 TREE_TYPE (decl) = coerce_new_type (TREE_TYPE (decl));
8d08fdba 12170 }
596ea4e5 12171 else if (operator_code == DELETE_EXPR || operator_code == VEC_DELETE_EXPR)
8d08fdba 12172 {
a28e3c7f 12173 if (methodp)
3afb32a4 12174 revert_static_member_fn (decl);
68642fb6 12175
36791f1e 12176 TREE_TYPE (decl) = coerce_delete_type (TREE_TYPE (decl));
8d08fdba 12177 }
8d08fdba
MS
12178 else
12179 {
12180 /* An operator function must either be a non-static member function
12181 or have at least one parameter of a class, a reference to a class,
12182 an enumeration, or a reference to an enumeration. 13.4.0.6 */
700f8a87 12183 if (! methodp || DECL_STATIC_FUNCTION_P (decl))
8d08fdba 12184 {
596ea4e5
AS
12185 if (operator_code == TYPE_EXPR
12186 || operator_code == CALL_EXPR
12187 || operator_code == COMPONENT_REF
12188 || operator_code == ARRAY_REF
12189 || operator_code == NOP_EXPR)
8251199e 12190 cp_error ("`%D' must be a nonstatic member function", decl);
8d08fdba
MS
12191 else
12192 {
12193 tree p = argtypes;
12194
700f8a87 12195 if (DECL_STATIC_FUNCTION_P (decl))
8251199e 12196 cp_error ("`%D' must be either a non-static member function or a non-member function", decl);
700f8a87 12197
8d08fdba 12198 if (p)
a1774733 12199 for (; TREE_CODE (TREE_VALUE (p)) != VOID_TYPE ; p = TREE_CHAIN (p))
8d08fdba
MS
12200 {
12201 tree arg = TREE_VALUE (p);
12202 if (TREE_CODE (arg) == REFERENCE_TYPE)
12203 arg = TREE_TYPE (arg);
12204
12205 /* This lets bad template code slip through. */
12206 if (IS_AGGR_TYPE (arg)
12207 || TREE_CODE (arg) == ENUMERAL_TYPE
73b0fce8 12208 || TREE_CODE (arg) == TEMPLATE_TYPE_PARM
a1281f45 12209 || TREE_CODE (arg) == BOUND_TEMPLATE_TEMPLATE_PARM)
8d08fdba
MS
12210 goto foundaggr;
12211 }
8251199e
JM
12212 cp_error
12213 ("`%D' must have an argument of class or enumerated type",
8d08fdba
MS
12214 decl);
12215 foundaggr:
12216 ;
12217 }
12218 }
68642fb6 12219
596ea4e5 12220 if (operator_code == CALL_EXPR)
2c73f9f5 12221 return; /* No restrictions on args. */
8d08fdba 12222
9a3b49ac 12223 if (IDENTIFIER_TYPENAME_P (name) && ! DECL_TEMPLATE_INFO (decl))
a0a33927
MS
12224 {
12225 tree t = TREE_TYPE (name);
17708e90 12226 if (! friendp)
a0a33927
MS
12227 {
12228 int ref = (TREE_CODE (t) == REFERENCE_TYPE);
d8e178a0 12229 const char *what = 0;
5362b086 12230
a0a33927
MS
12231 if (ref)
12232 t = TYPE_MAIN_VARIANT (TREE_TYPE (t));
12233
17708e90
NS
12234 if (TREE_CODE (t) == VOID_TYPE)
12235 what = "void";
12236 else if (t == current_class_type)
a0a33927 12237 what = "the same type";
9a3b49ac 12238 /* Don't force t to be complete here. */
a0a33927 12239 else if (IS_AGGR_TYPE (t)
d0f062fb 12240 && COMPLETE_TYPE_P (t)
a0a33927
MS
12241 && DERIVED_FROM_P (t, current_class_type))
12242 what = "a base class";
12243
12244 if (what)
8251199e 12245 warning ("conversion to %s%s will never use a type conversion operator",
a0a33927
MS
12246 ref ? "a reference to " : "", what);
12247 }
12248 }
12249
5362b086 12250 if (DECL_ASSIGNMENT_OPERATOR_P (decl)
596ea4e5 12251 && operator_code == NOP_EXPR)
8d08fdba
MS
12252 {
12253 tree parmtype;
12254
596ea4e5 12255 if (arity != 2 && methodp)
8d08fdba 12256 {
8251199e 12257 cp_error ("`%D' must take exactly one argument", decl);
8d08fdba
MS
12258 return;
12259 }
12260 parmtype = TREE_VALUE (TREE_CHAIN (argtypes));
12261
596ea4e5
AS
12262 /* [class.copy]
12263
12264 A user-declared copy assignment operator X::operator= is
12265 a non-static non-template member function of class X with
12266 exactly one parameter of type X, X&, const X&, volatile
12267 X& or const volatile X&. */
f0e01782 12268 if (copy_assignment_arg_p (parmtype, virtualp)
596ea4e5
AS
12269 && !(DECL_TEMPLATE_INSTANTIATION (decl)
12270 && is_member_template (DECL_TI_TEMPLATE (decl)))
a28e3c7f 12271 && ! friendp)
8d08fdba
MS
12272 {
12273 TYPE_HAS_ASSIGN_REF (current_class_type) = 1;
f0e01782 12274 if (TREE_CODE (parmtype) != REFERENCE_TYPE
91063b51 12275 || CP_TYPE_CONST_P (TREE_TYPE (parmtype)))
8d08fdba
MS
12276 TYPE_HAS_CONST_ASSIGN_REF (current_class_type) = 1;
12277 }
12278 }
596ea4e5 12279 else if (operator_code == COND_EXPR)
8d08fdba
MS
12280 {
12281 /* 13.4.0.3 */
cb9a3ff8 12282 cp_error ("ISO C++ prohibits overloading operator ?:");
68642fb6 12283 }
596ea4e5 12284 else if (ambi_op_p (operator_code))
8d08fdba 12285 {
596ea4e5
AS
12286 if (arity == 1)
12287 /* We pick the one-argument operator codes by default, so
12288 we don't have to change anything. */
12289 ;
12290 else if (arity == 2)
8d08fdba 12291 {
596ea4e5
AS
12292 /* If we thought this was a unary operator, we now know
12293 it to be a binary operator. */
12294 switch (operator_code)
12295 {
12296 case INDIRECT_REF:
12297 operator_code = MULT_EXPR;
12298 break;
12299
12300 case ADDR_EXPR:
12301 operator_code = BIT_AND_EXPR;
12302 break;
12303
12304 case CONVERT_EXPR:
12305 operator_code = PLUS_EXPR;
12306 break;
12307
12308 case NEGATE_EXPR:
12309 operator_code = MINUS_EXPR;
12310 break;
12311
12312 case PREINCREMENT_EXPR:
12313 operator_code = POSTINCREMENT_EXPR;
12314 break;
12315
12316 case PREDECREMENT_EXPR:
655dc6ee 12317 operator_code = POSTDECREMENT_EXPR;
596ea4e5
AS
12318 break;
12319
12320 default:
12321 my_friendly_abort (20000527);
12322 }
12323
12324 SET_OVERLOADED_OPERATOR_CODE (decl, operator_code);
12325
12326 if ((operator_code == POSTINCREMENT_EXPR
12327 || operator_code == POSTDECREMENT_EXPR)
5156628f 12328 && ! processing_template_decl
007e5fea 12329 && ! same_type_p (TREE_VALUE (TREE_CHAIN (argtypes)), integer_type_node))
8d08fdba
MS
12330 {
12331 if (methodp)
8251199e 12332 cp_error ("postfix `%D' must take `int' as its argument",
8d08fdba
MS
12333 decl);
12334 else
8251199e
JM
12335 cp_error
12336 ("postfix `%D' must take `int' as its second argument",
12337 decl);
8d08fdba
MS
12338 }
12339 }
12340 else
12341 {
12342 if (methodp)
8251199e 12343 cp_error ("`%D' must take either zero or one argument", decl);
8d08fdba 12344 else
8251199e 12345 cp_error ("`%D' must take either one or two arguments", decl);
8d08fdba 12346 }
824b9a4c
MS
12347
12348 /* More Effective C++ rule 6. */
eb448459 12349 if (warn_ecpp
596ea4e5
AS
12350 && (operator_code == POSTINCREMENT_EXPR
12351 || operator_code == POSTDECREMENT_EXPR
12352 || operator_code == PREINCREMENT_EXPR
12353 || operator_code == PREDECREMENT_EXPR))
824b9a4c
MS
12354 {
12355 tree arg = TREE_VALUE (argtypes);
12356 tree ret = TREE_TYPE (TREE_TYPE (decl));
12357 if (methodp || TREE_CODE (arg) == REFERENCE_TYPE)
12358 arg = TREE_TYPE (arg);
12359 arg = TYPE_MAIN_VARIANT (arg);
596ea4e5
AS
12360 if (operator_code == PREINCREMENT_EXPR
12361 || operator_code == PREDECREMENT_EXPR)
824b9a4c
MS
12362 {
12363 if (TREE_CODE (ret) != REFERENCE_TYPE
3bfdc719
MM
12364 || !same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (ret)),
12365 arg))
8251199e 12366 cp_warning ("prefix `%D' should return `%T'", decl,
824b9a4c
MS
12367 build_reference_type (arg));
12368 }
12369 else
12370 {
3bfdc719 12371 if (!same_type_p (TYPE_MAIN_VARIANT (ret), arg))
8251199e 12372 cp_warning ("postfix `%D' should return `%T'", decl, arg);
824b9a4c
MS
12373 }
12374 }
8d08fdba 12375 }
596ea4e5 12376 else if (unary_op_p (operator_code))
8d08fdba 12377 {
596ea4e5 12378 if (arity != 1)
8d08fdba
MS
12379 {
12380 if (methodp)
8251199e 12381 cp_error ("`%D' must take `void'", decl);
8d08fdba 12382 else
8251199e 12383 cp_error ("`%D' must take exactly one argument", decl);
8d08fdba
MS
12384 }
12385 }
596ea4e5 12386 else /* if (binary_op_p (operator_code)) */
8d08fdba 12387 {
596ea4e5 12388 if (arity != 2)
8d08fdba
MS
12389 {
12390 if (methodp)
8251199e 12391 cp_error ("`%D' must take exactly one argument", decl);
8d08fdba 12392 else
8251199e 12393 cp_error ("`%D' must take exactly two arguments", decl);
8d08fdba 12394 }
824b9a4c
MS
12395
12396 /* More Effective C++ rule 7. */
eb448459 12397 if (warn_ecpp
596ea4e5
AS
12398 && (operator_code == TRUTH_ANDIF_EXPR
12399 || operator_code == TRUTH_ORIF_EXPR
12400 || operator_code == COMPOUND_EXPR))
8251199e 12401 cp_warning ("user-defined `%D' always evaluates both arguments",
824b9a4c
MS
12402 decl);
12403 }
12404
12405 /* Effective C++ rule 23. */
eb448459 12406 if (warn_ecpp
596ea4e5
AS
12407 && arity == 2
12408 && (operator_code == PLUS_EXPR
12409 || operator_code == MINUS_EXPR
12410 || operator_code == TRUNC_DIV_EXPR
12411 || operator_code == MULT_EXPR)
824b9a4c 12412 && TREE_CODE (TREE_TYPE (TREE_TYPE (decl))) == REFERENCE_TYPE)
8251199e 12413 cp_warning ("`%D' should return by value", decl);
8d08fdba
MS
12414
12415 /* 13.4.0.8 */
34332678
CT
12416 for (; argtypes && argtypes != void_list_node;
12417 argtypes = TREE_CHAIN (argtypes))
12418 if (TREE_PURPOSE (argtypes))
12419 {
12420 TREE_PURPOSE (argtypes) = NULL_TREE;
596ea4e5
AS
12421 if (operator_code == POSTINCREMENT_EXPR
12422 || operator_code == POSTDECREMENT_EXPR)
34332678
CT
12423 {
12424 if (pedantic)
12425 cp_pedwarn ("`%D' cannot have default arguments", decl);
12426 }
12427 else
12428 cp_error ("`%D' cannot have default arguments", decl);
12429 }
12430
8d08fdba
MS
12431 }
12432}
12433\f
d8e178a0 12434static const char *
094fe153
JM
12435tag_name (code)
12436 enum tag_types code;
12437{
12438 switch (code)
12439 {
12440 case record_type:
12441 return "struct";
12442 case class_type:
12443 return "class";
12444 case union_type:
12445 return "union ";
12446 case enum_type:
12447 return "enum";
094fe153
JM
12448 default:
12449 my_friendly_abort (981122);
12450 }
12451}
12452
8d08fdba
MS
12453/* Get the struct, enum or union (CODE says which) with tag NAME.
12454 Define the tag as a forward-reference if it is not defined.
12455
12456 C++: If a class derivation is given, process it here, and report
12457 an error if multiple derivation declarations are not identical.
12458
12459 If this is a definition, come in through xref_tag and only look in
12460 the current frame for the name (since C++ allows new names in any
12461 scope.) */
12462
8d08fdba 12463tree
ca107ded 12464xref_tag (code_type_node, name, globalize)
8d08fdba 12465 tree code_type_node;
ca107ded 12466 tree name;
8d08fdba
MS
12467 int globalize;
12468{
12469 enum tag_types tag_code;
12470 enum tree_code code;
8d08fdba 12471 register tree ref, t;
8f032717 12472 struct binding_level *b = current_binding_level;
a80e4195 12473 int got_type = 0;
dc8263bc 12474 tree attributes = NULL_TREE;
25aab5d0 12475 tree context = NULL_TREE;
dc8263bc
JM
12476
12477 /* If we are called from the parser, code_type_node will sometimes be a
12478 TREE_LIST. This indicates that the user wrote
12479 "class __attribute__ ((foo)) bar". Extract the attributes so we can
12480 use them later. */
12481 if (TREE_CODE (code_type_node) == TREE_LIST)
12482 {
12483 attributes = TREE_PURPOSE (code_type_node);
12484 code_type_node = TREE_VALUE (code_type_node);
12485 }
8d08fdba 12486
665f2503 12487 tag_code = (enum tag_types) tree_low_cst (code_type_node, 1);
8d08fdba
MS
12488 switch (tag_code)
12489 {
12490 case record_type:
12491 case class_type:
8d08fdba 12492 code = RECORD_TYPE;
8d08fdba
MS
12493 break;
12494 case union_type:
12495 code = UNION_TYPE;
8d08fdba
MS
12496 break;
12497 case enum_type:
12498 code = ENUMERAL_TYPE;
12499 break;
12500 default:
12501 my_friendly_abort (18);
12502 }
12503
12504 /* If a cross reference is requested, look up the type
12505 already defined for this tag and return it. */
2f939d94 12506 if (TYPE_P (name))
be99da77
MS
12507 {
12508 t = name;
a80e4195
MS
12509 name = TYPE_IDENTIFIER (t);
12510 got_type = 1;
be99da77
MS
12511 }
12512 else
12513 t = IDENTIFIER_TYPE_VALUE (name);
61a127b3 12514
f2ae0c45
JM
12515 /* Warn about 'friend struct Inherited;' doing the wrong thing. */
12516 if (t && globalize && TREE_CODE (t) == TYPENAME_TYPE)
12517 {
12518 static int explained;
12519
12520 cp_warning ("`%s %T' declares a new type at namespace scope;\n\
12521to refer to the inherited type, say `%s %T::%T'%s",
12522 tag_name (tag_code), name, tag_name (tag_code),
12523 constructor_name (current_class_type), TYPE_IDENTIFIER (t),
12524 (!explained ? "\n\
12525(names from dependent base classes are not visible to unqualified name lookup)"
12526 : ""));
12527
12528 explained = 1;
12529 }
12530
73b0fce8 12531 if (t && TREE_CODE (t) != code && TREE_CODE (t) != TEMPLATE_TYPE_PARM
a1281f45 12532 && TREE_CODE (t) != BOUND_TEMPLATE_TEMPLATE_PARM)
a0a33927
MS
12533 t = NULL_TREE;
12534
8ccc31eb 12535 if (! globalize)
8d08fdba 12536 {
f3400fe2
JM
12537 /* If we know we are defining this tag, only look it up in
12538 this scope and don't try to find it as a type. */
12539 ref = lookup_tag (code, name, b, 1);
8d08fdba
MS
12540 }
12541 else
12542 {
25aab5d0 12543 if (t)
36a117a5 12544 {
25aab5d0
MM
12545 /* [dcl.type.elab] If the identifier resolves to a
12546 typedef-name or a template type-parameter, the
12547 elaborated-type-specifier is ill-formed. */
12548 if (t != TYPE_MAIN_VARIANT (t)
12549 || (CLASS_TYPE_P (t) && TYPE_WAS_ANONYMOUS (t)))
12550 cp_pedwarn ("using typedef-name `%D' after `%s'",
12551 TYPE_NAME (t), tag_name (tag_code));
12552 else if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
12553 cp_error ("using template type parameter `%T' after `%s'",
12554 t, tag_name (tag_code));
12555
12556 ref = t;
12557 }
12558 else
12559 ref = lookup_tag (code, name, b, 0);
68642fb6 12560
25aab5d0
MM
12561 if (! ref)
12562 {
12563 /* Try finding it as a type declaration. If that wins,
68642fb6 12564 use it. */
25aab5d0
MM
12565 ref = lookup_name (name, 1);
12566
12567 if (ref != NULL_TREE
12568 && processing_template_decl
12569 && DECL_CLASS_TEMPLATE_P (ref)
12570 && template_class_depth (current_class_type) == 0)
12571 /* Since GLOBALIZE is true, we're declaring a global
6757edfe 12572 template, so we want this type. */
17aec3eb 12573 ref = DECL_TEMPLATE_RESULT (ref);
6757edfe 12574
25aab5d0
MM
12575 if (ref && TREE_CODE (ref) == TYPE_DECL
12576 && TREE_CODE (TREE_TYPE (ref)) == code)
12577 ref = TREE_TYPE (ref);
12578 else
12579 ref = NULL_TREE;
12580 }
12581
68642fb6
UD
12582 if (ref && current_class_type
12583 && template_class_depth (current_class_type)
12584 && PROCESSING_REAL_TEMPLATE_DECL_P ())
25aab5d0
MM
12585 {
12586 /* Since GLOBALIZE is non-zero, we are not looking at a
12587 definition of this tag. Since, in addition, we are currently
12588 processing a (member) template declaration of a template
12589 class, we must be very careful; consider:
12590
12591 template <class X>
12592 struct S1
12593
12594 template <class U>
12595 struct S2
12596 { template <class V>
12597 friend struct S1; };
12598
12599 Here, the S2::S1 declaration should not be confused with the
12600 outer declaration. In particular, the inner version should
12601 have a template parameter of level 2, not level 1. This
12602 would be particularly important if the member declaration
12603 were instead:
12604
12605 template <class V = U> friend struct S1;
12606
12607 say, when we should tsubst into `U' when instantiating
12608 S2. On the other hand, when presented with:
12609
12610 template <class T>
12611 struct S1 {
12612 template <class U>
12613 struct S2 {};
12614 template <class U>
12615 friend struct S2;
12616 };
12617
12618 we must find the inner binding eventually. We
12619 accomplish this by making sure that the new type we
12620 create to represent this declaration has the right
12621 TYPE_CONTEXT. */
12622 context = TYPE_CONTEXT (ref);
12623 ref = NULL_TREE;
8d08fdba
MS
12624 }
12625 }
12626
8d08fdba
MS
12627 if (! ref)
12628 {
12629 /* If no such tag is yet defined, create a forward-reference node
12630 and record it as the "definition".
12631 When a real declaration of this type is found,
12632 the forward-reference will be altered into a real type. */
8d08fdba
MS
12633 if (code == ENUMERAL_TYPE)
12634 {
8251199e 12635 cp_error ("use of enum `%#D' without previous declaration", name);
fc378698 12636
8d08fdba
MS
12637 ref = make_node (ENUMERAL_TYPE);
12638
12639 /* Give the type a default layout like unsigned int
12640 to avoid crashing if it does not get defined. */
12641 TYPE_MODE (ref) = TYPE_MODE (unsigned_type_node);
12642 TYPE_ALIGN (ref) = TYPE_ALIGN (unsigned_type_node);
11cf4d18 12643 TYPE_USER_ALIGN (ref) = 0;
8d08fdba
MS
12644 TREE_UNSIGNED (ref) = 1;
12645 TYPE_PRECISION (ref) = TYPE_PRECISION (unsigned_type_node);
12646 TYPE_MIN_VALUE (ref) = TYPE_MIN_VALUE (unsigned_type_node);
12647 TYPE_MAX_VALUE (ref) = TYPE_MAX_VALUE (unsigned_type_node);
12648
12649 /* Enable us to recognize when a type is created in class context.
12650 To do nested classes correctly, this should probably be cleared
12651 out when we leave this classes scope. Currently this in only
12652 done in `start_enum'. */
12653
12654 pushtag (name, ref, globalize);
8d08fdba 12655 }
8d08fdba
MS
12656 else
12657 {
8d08fdba 12658 struct binding_level *old_b = class_binding_level;
8d08fdba 12659
33848bb0 12660 ref = make_aggr_type (code);
25aab5d0 12661 TYPE_CONTEXT (ref) = context;
8d08fdba 12662
8d08fdba
MS
12663#ifdef NONNESTED_CLASSES
12664 /* Class types don't nest the way enums do. */
12665 class_binding_level = (struct binding_level *)0;
12666#endif
12667 pushtag (name, ref, globalize);
12668 class_binding_level = old_b;
8d08fdba
MS
12669 }
12670 }
12671 else
12672 {
7fe6899f 12673 if (!globalize && processing_template_decl && IS_AGGR_TYPE (ref))
6757edfe 12674 redeclare_class_template (ref, current_template_parms);
8d08fdba
MS
12675 }
12676
8d08fdba
MS
12677 /* Until the type is defined, tentatively accept whatever
12678 structure tag the user hands us. */
d0f062fb 12679 if (!COMPLETE_TYPE_P (ref)
8d08fdba
MS
12680 && ref != current_class_type
12681 /* Have to check this, in case we have contradictory tag info. */
12682 && IS_AGGR_TYPE_CODE (TREE_CODE (ref)))
12683 {
12684 if (tag_code == class_type)
12685 CLASSTYPE_DECLARED_CLASS (ref) = 1;
6eabb241 12686 else if (tag_code == record_type)
8d08fdba
MS
12687 CLASSTYPE_DECLARED_CLASS (ref) = 0;
12688 }
12689
dc8263bc
JM
12690 TREE_TYPE (ref) = attributes;
12691
8d08fdba
MS
12692 return ref;
12693}
8ccc31eb 12694
fc378698
MS
12695tree
12696xref_tag_from_type (old, id, globalize)
12697 tree old, id;
12698 int globalize;
12699{
12700 tree code_type_node;
12701
12702 if (TREE_CODE (old) == RECORD_TYPE)
12703 code_type_node = (CLASSTYPE_DECLARED_CLASS (old)
12704 ? class_type_node : record_type_node);
12705 else
12706 code_type_node = union_type_node;
12707
12708 if (id == NULL_TREE)
12709 id = TYPE_IDENTIFIER (old);
12710
ca107ded 12711 return xref_tag (code_type_node, id, globalize);
fc378698
MS
12712}
12713
3fd71a52
MM
12714/* REF is a type (named NAME), for which we have just seen some
12715 baseclasses. BINFO is a list of those baseclasses; the
12716 TREE_PURPOSE is an access_* node, and the TREE_VALUE is the type of
12717 the base-class. CODE_TYPE_NODE indicates whether REF is a class,
12718 struct, or union. */
12719
8ccc31eb
MS
12720void
12721xref_basetypes (code_type_node, name, ref, binfo)
12722 tree code_type_node;
12723 tree name, ref;
12724 tree binfo;
12725{
12726 /* In the declaration `A : X, Y, ... Z' we mark all the types
12727 (A, X, Y, ..., Z) so we can check for duplicates. */
12728 tree binfos;
d6479fe7
MM
12729 tree base;
12730
8ccc31eb 12731 int i, len;
665f2503 12732 enum tag_types tag_code = (enum tag_types) tree_low_cst (code_type_node, 1);
8ccc31eb
MS
12733
12734 if (tag_code == union_type)
12735 {
8251199e 12736 cp_error ("derived union `%T' invalid", ref);
8ccc31eb
MS
12737 return;
12738 }
12739
12740 len = list_length (binfo);
8ccc31eb 12741
d6479fe7
MM
12742 /* First, make sure that any templates in base-classes are
12743 instantiated. This ensures that if we call ourselves recursively
12744 we do not get confused about which classes are marked and which
12745 are not. */
12746 for (base = binfo; base; base = TREE_CHAIN (base))
12747 complete_type (TREE_VALUE (base));
12748
8ccc31eb
MS
12749 SET_CLASSTYPE_MARKED (ref);
12750 BINFO_BASETYPES (TYPE_BINFO (ref)) = binfos = make_tree_vec (len);
12751
12752 for (i = 0; binfo; binfo = TREE_CHAIN (binfo))
12753 {
12754 /* The base of a derived struct is public by default. */
12755 int via_public
be99da77
MS
12756 = (TREE_PURPOSE (binfo) == access_public_node
12757 || TREE_PURPOSE (binfo) == access_public_virtual_node
8ccc31eb 12758 || (tag_code != class_type
be99da77
MS
12759 && (TREE_PURPOSE (binfo) == access_default_node
12760 || TREE_PURPOSE (binfo) == access_default_virtual_node)));
d8b55a76
JM
12761 int via_protected
12762 = (TREE_PURPOSE (binfo) == access_protected_node
12763 || TREE_PURPOSE (binfo) == access_protected_virtual_node);
8ccc31eb 12764 int via_virtual
be99da77 12765 = (TREE_PURPOSE (binfo) == access_private_virtual_node
d8b55a76 12766 || TREE_PURPOSE (binfo) == access_protected_virtual_node
be99da77
MS
12767 || TREE_PURPOSE (binfo) == access_public_virtual_node
12768 || TREE_PURPOSE (binfo) == access_default_virtual_node);
12769 tree basetype = TREE_VALUE (binfo);
8ccc31eb
MS
12770 tree base_binfo;
12771
8ccc31eb
MS
12772 if (basetype && TREE_CODE (basetype) == TYPE_DECL)
12773 basetype = TREE_TYPE (basetype);
5566b478
MS
12774 if (!basetype
12775 || (TREE_CODE (basetype) != RECORD_TYPE
a80e4195 12776 && TREE_CODE (basetype) != TYPENAME_TYPE
73b0fce8 12777 && TREE_CODE (basetype) != TEMPLATE_TYPE_PARM
a1281f45 12778 && TREE_CODE (basetype) != BOUND_TEMPLATE_TEMPLATE_PARM))
8ccc31eb 12779 {
8251199e 12780 cp_error ("base type `%T' fails to be a struct or class type",
8ccc31eb
MS
12781 TREE_VALUE (binfo));
12782 continue;
12783 }
2b9dc906 12784
11b5139c 12785 GNU_xref_hier (name, basetype, via_public, via_virtual, 0);
2b9dc906 12786
8adf5b5e
JM
12787 /* This code replaces similar code in layout_basetypes.
12788 We put the complete_type first for implicit `typename'. */
d0f062fb 12789 if (!COMPLETE_TYPE_P (basetype)
2b9dc906 12790 && ! (current_template_parms && uses_template_parms (basetype)))
8ccc31eb 12791 {
8251199e 12792 cp_error ("base class `%T' has incomplete type", basetype);
8ccc31eb
MS
12793 continue;
12794 }
8ccc31eb
MS
12795 else
12796 {
12797 if (CLASSTYPE_MARKED (basetype))
12798 {
12799 if (basetype == ref)
8251199e 12800 cp_error ("recursive type `%T' undefined", basetype);
8ccc31eb 12801 else
8251199e 12802 cp_error ("duplicate base type `%T' invalid", basetype);
8ccc31eb
MS
12803 continue;
12804 }
12805
eff71ab0 12806 if (TYPE_FOR_JAVA (basetype)
46ccf50a 12807 && (current_lang_depth () == 0))
eff71ab0
PB
12808 TYPE_FOR_JAVA (ref) = 1;
12809
8ccc31eb
MS
12810 /* Note that the BINFO records which describe individual
12811 inheritances are *not* shared in the lattice! They
12812 cannot be shared because a given baseclass may be
12813 inherited with different `accessibility' by different
12814 derived classes. (Each BINFO record describing an
12815 individual inheritance contains flags which say what
12816 the `accessibility' of that particular inheritance is.) */
68642fb6
UD
12817
12818 base_binfo
fed3cef0 12819 = make_binfo (size_zero_node, basetype,
7ddedda4
MM
12820 CLASS_TYPE_P (basetype)
12821 ? TYPE_BINFO_VTABLE (basetype) : NULL_TREE,
12822 CLASS_TYPE_P (basetype)
12823 ? TYPE_BINFO_VIRTUALS (basetype) : NULL_TREE);
68642fb6 12824
8ccc31eb
MS
12825 TREE_VEC_ELT (binfos, i) = base_binfo;
12826 TREE_VIA_PUBLIC (base_binfo) = via_public;
12827 TREE_VIA_PROTECTED (base_binfo) = via_protected;
12828 TREE_VIA_VIRTUAL (base_binfo) = via_virtual;
12829 BINFO_INHERITANCE_CHAIN (base_binfo) = TYPE_BINFO (ref);
12830
dfbcd65a
JM
12831 /* We need to unshare the binfos now so that lookups during class
12832 definition work. */
12833 unshare_base_binfos (base_binfo);
12834
8ccc31eb 12835 SET_CLASSTYPE_MARKED (basetype);
9e9ff709 12836
8ccc31eb
MS
12837 /* We are free to modify these bits because they are meaningless
12838 at top level, and BASETYPE is a top-level type. */
12839 if (via_virtual || TYPE_USES_VIRTUAL_BASECLASSES (basetype))
12840 {
12841 TYPE_USES_VIRTUAL_BASECLASSES (ref) = 1;
4c6b7393
MM
12842 /* Converting to a virtual base class requires looking
12843 up the offset of the virtual base. */
12844 TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (ref) = 1;
8ccc31eb
MS
12845 }
12846
7ddedda4
MM
12847 if (CLASS_TYPE_P (basetype))
12848 {
5362b086 12849 TYPE_HAS_NEW_OPERATOR (ref)
834c6dff 12850 |= TYPE_HAS_NEW_OPERATOR (basetype);
5362b086 12851 TYPE_HAS_ARRAY_NEW_OPERATOR (ref)
834c6dff 12852 |= TYPE_HAS_ARRAY_NEW_OPERATOR (basetype);
7ddedda4 12853 TYPE_GETS_DELETE (ref) |= TYPE_GETS_DELETE (basetype);
4c6b7393 12854 /* If the base-class uses multiple inheritance, so do we. */
68642fb6 12855 TYPE_USES_MULTIPLE_INHERITANCE (ref)
4c6b7393
MM
12856 |= TYPE_USES_MULTIPLE_INHERITANCE (basetype);
12857 /* Likewise, if converting to a base of the base may require
12858 code, then we may need to generate code to convert to a
12859 base as well. */
68642fb6 12860 TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (ref)
4c6b7393 12861 |= TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (basetype);
7ddedda4
MM
12862 }
12863
8ccc31eb
MS
12864 i += 1;
12865 }
12866 }
12867 if (i)
12868 TREE_VEC_LENGTH (binfos) = i;
12869 else
12870 BINFO_BASETYPES (TYPE_BINFO (ref)) = NULL_TREE;
12871
12872 if (i > 1)
7ddedda4 12873 {
4c6b7393
MM
12874 TYPE_USES_MULTIPLE_INHERITANCE (ref) = 1;
12875 /* If there is more than one non-empty they cannot be at the same
12876 address. */
12877 TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (ref) = 1;
7ddedda4
MM
12878 }
12879
8ccc31eb
MS
12880 /* Unmark all the types. */
12881 while (--i >= 0)
12882 CLEAR_CLASSTYPE_MARKED (BINFO_TYPE (TREE_VEC_ELT (binfos, i)));
12883 CLEAR_CLASSTYPE_MARKED (ref);
12884
70c532b5
MM
12885 /* Now that we know all the base-classes, set up the list of virtual
12886 bases. */
23381155 12887 get_vbase_types (ref);
8ccc31eb 12888}
68642fb6 12889
8d08fdba 12890\f
8d08fdba
MS
12891/* Begin compiling the definition of an enumeration type.
12892 NAME is its name (or null if anonymous).
12893 Returns the type object, as yet incomplete.
12894 Also records info about it so that build_enumerator
12895 may be used to declare the individual values as they are read. */
12896
12897tree
12898start_enum (name)
12899 tree name;
12900{
12901 register tree enumtype = NULL_TREE;
8f032717 12902 struct binding_level *b = current_binding_level;
8d08fdba
MS
12903
12904 /* If this is the real definition for a previous forward reference,
12905 fill in the contents in the same object that used to be the
12906 forward reference. */
12907
12908 if (name != NULL_TREE)
12909 enumtype = lookup_tag (ENUMERAL_TYPE, name, b, 1);
12910
12911 if (enumtype != NULL_TREE && TREE_CODE (enumtype) == ENUMERAL_TYPE)
30ff8252
NS
12912 {
12913 cp_error ("multiple definition of `%#T'", enumtype);
12914 cp_error_at ("previous definition here", enumtype);
58595203
MM
12915 /* Clear out TYPE_VALUES, and start again. */
12916 TYPE_VALUES (enumtype) = NULL_TREE;
30ff8252 12917 }
8d08fdba
MS
12918 else
12919 {
12920 enumtype = make_node (ENUMERAL_TYPE);
12921 pushtag (name, enumtype, 0);
12922 }
12923
12924 if (current_class_type)
12925 TREE_ADDRESSABLE (b->tags) = 1;
c91a56d2 12926
8d08fdba
MS
12927 GNU_xref_decl (current_function_decl, enumtype);
12928 return enumtype;
12929}
12930
12931/* After processing and defining all the values of an enumeration type,
12932 install their decls in the enumeration type and finish it off.
12933 ENUMTYPE is the type object and VALUES a list of name-value pairs.
12934 Returns ENUMTYPE. */
12935
12936tree
dbfe2124
MM
12937finish_enum (enumtype)
12938 tree enumtype;
8d08fdba 12939{
a703fb38 12940 register tree minnode = NULL_TREE, maxnode = NULL_TREE;
8d08fdba
MS
12941 /* Calculate the maximum value of any enumerator in this type. */
12942
dbfe2124 12943 tree values = TYPE_VALUES (enumtype);
8d08fdba
MS
12944 if (values)
12945 {
ed44da02 12946 tree pair;
5566b478 12947
ed44da02 12948 for (pair = values; pair; pair = TREE_CHAIN (pair))
8d08fdba 12949 {
ed44da02
MM
12950 tree decl;
12951 tree value;
12952
12953 /* The TREE_VALUE is a CONST_DECL for this enumeration
12954 constant. */
12955 decl = TREE_VALUE (pair);
12956
58595203
MM
12957 /* [dcl.enum]
12958
12959 Following the closing brace of an enum-specifier, each
12960 enumerator has the type of its enumeration. Prior to the
12961 closing brace, the type of each enumerator is the type of
12962 its initializing value. */
12963 TREE_TYPE (decl) = enumtype;
12964
ed44da02
MM
12965 /* The DECL_INITIAL will be NULL if we are processing a
12966 template declaration and this enumeration constant had no
12967 explicit initializer. */
12968 value = DECL_INITIAL (decl);
079e1098 12969 if (value && !processing_template_decl)
5566b478 12970 {
079e1098
MM
12971 /* Set the TREE_TYPE for the VALUE as well. That's so
12972 that when we call decl_constant_value we get an
12973 entity of the right type (but with the constant
12974 value). Since we shouldn't ever call
12975 decl_constant_value on a template type, there's no
12976 reason to do that when processing_template_decl.
12977 And, if the expression is something like a
12978 TEMPLATE_PARM_INDEX or a CAST_EXPR doing so will
68642fb6 12979 wreak havoc on the intended type of the expression.
079e1098
MM
12980
12981 Of course, there's also no point in trying to compute
12982 minimum or maximum values if we're in a template. */
5566b478 12983 TREE_TYPE (value) = enumtype;
ed44da02 12984
079e1098
MM
12985 if (!minnode)
12986 minnode = maxnode = value;
12987 else if (tree_int_cst_lt (maxnode, value))
12988 maxnode = value;
12989 else if (tree_int_cst_lt (value, minnode))
12990 minnode = value;
5566b478 12991 }
ed44da02 12992
68642fb6 12993 if (processing_template_decl)
72f2bd78
MM
12994 /* If this is just a template, leave the CONST_DECL
12995 alone. That way tsubst_copy will find CONST_DECLs for
12996 CONST_DECLs, and not INTEGER_CSTs. */
12997 ;
12998 else
12999 /* In the list we're building up, we want the enumeration
13000 values, not the CONST_DECLs. */
13001 TREE_VALUE (pair) = value;
8d08fdba
MS
13002 }
13003 }
f376e137
MS
13004 else
13005 maxnode = minnode = integer_zero_node;
8d08fdba 13006
de22184b 13007 TYPE_VALUES (enumtype) = nreverse (values);
2986ae00 13008
5156628f 13009 if (processing_template_decl)
b87692e5 13010 {
9360ac70
MM
13011 tree scope = current_scope ();
13012 if (scope && TREE_CODE (scope) == FUNCTION_DECL)
ae499cce 13013 add_stmt (build_min (TAG_DEFN, enumtype));
b87692e5 13014 }
cbf882af
MM
13015 else
13016 {
13017 int unsignedp = tree_int_cst_sgn (minnode) >= 0;
13018 int lowprec = min_precision (minnode, unsignedp);
13019 int highprec = min_precision (maxnode, unsignedp);
13020 int precision = MAX (lowprec, highprec);
13021 tree tem;
5566b478 13022
cbf882af 13023 TYPE_SIZE (enumtype) = NULL_TREE;
8d08fdba 13024
cbf882af 13025 /* Set TYPE_MIN_VALUE and TYPE_MAX_VALUE according to `precision'. */
8ccc31eb 13026
cbf882af
MM
13027 TYPE_PRECISION (enumtype) = precision;
13028 if (unsignedp)
13029 fixup_unsigned_type (enumtype);
13030 else
13031 fixup_signed_type (enumtype);
8ccc31eb 13032
cbf882af
MM
13033 if (flag_short_enums || (precision > TYPE_PRECISION (integer_type_node)))
13034 /* Use the width of the narrowest normal C type which is wide
68642fb6 13035 enough. */
cbf882af
MM
13036 TYPE_PRECISION (enumtype) = TYPE_PRECISION (type_for_size
13037 (precision, 1));
13038 else
13039 TYPE_PRECISION (enumtype) = TYPE_PRECISION (integer_type_node);
8d08fdba 13040
cbf882af
MM
13041 TYPE_SIZE (enumtype) = 0;
13042 layout_type (enumtype);
68642fb6 13043
cbf882af
MM
13044 /* Fix up all variant types of this enum type. */
13045 for (tem = TYPE_MAIN_VARIANT (enumtype); tem;
13046 tem = TYPE_NEXT_VARIANT (tem))
13047 {
13048 TYPE_VALUES (tem) = TYPE_VALUES (enumtype);
13049 TYPE_MIN_VALUE (tem) = TYPE_MIN_VALUE (enumtype);
13050 TYPE_MAX_VALUE (tem) = TYPE_MAX_VALUE (enumtype);
13051 TYPE_SIZE (tem) = TYPE_SIZE (enumtype);
13052 TYPE_SIZE_UNIT (tem) = TYPE_SIZE_UNIT (enumtype);
13053 TYPE_MODE (tem) = TYPE_MODE (enumtype);
13054 TYPE_PRECISION (tem) = TYPE_PRECISION (enumtype);
13055 TYPE_ALIGN (tem) = TYPE_ALIGN (enumtype);
11cf4d18 13056 TYPE_USER_ALIGN (tem) = TYPE_USER_ALIGN (enumtype);
cbf882af
MM
13057 TREE_UNSIGNED (tem) = TREE_UNSIGNED (enumtype);
13058 }
8d08fdba 13059
cbf882af
MM
13060 /* Finish debugging output for this type. */
13061 rest_of_type_compilation (enumtype, namespace_bindings_p ());
13062 }
13063
8d08fdba
MS
13064 return enumtype;
13065}
13066
079e1098 13067/* Build and install a CONST_DECL for an enumeration constant of the
58595203 13068 enumeration type ENUMTYPE whose NAME and VALUE (if any) are provided.
8d08fdba
MS
13069 Assignment of sequential values by default is handled here. */
13070
58595203
MM
13071void
13072build_enumerator (name, value, enumtype)
079e1098
MM
13073 tree name;
13074 tree value;
58595203 13075 tree enumtype;
8d08fdba 13076{
58595203 13077 tree decl;
e8bd800e 13078 tree context;
58595203
MM
13079 tree type;
13080 tree values;
8d08fdba
MS
13081
13082 /* Remove no-op casts from the value. */
13083 if (value)
13084 STRIP_TYPE_NOPS (value);
13085
58595203
MM
13086 if (! processing_template_decl)
13087 {
13088 /* Validate and default VALUE. */
13089 if (value != NULL_TREE)
13090 {
fc611ce0 13091 value = decl_constant_value (value);
58595203
MM
13092
13093 if (TREE_CODE (value) == INTEGER_CST)
13094 {
13095 value = default_conversion (value);
13096 constant_expression_warning (value);
13097 }
13098 else
13099 {
13100 cp_error ("enumerator value for `%D' not integer constant", name);
13101 value = NULL_TREE;
13102 }
13103 }
13104
13105 /* Default based on previous value. */
13106 if (value == NULL_TREE && ! processing_template_decl)
13107 {
13108 tree prev_value;
13109
13110 if (TYPE_VALUES (enumtype))
13111 {
13112 /* The next value is the previous value ... */
13113 prev_value = DECL_INITIAL (TREE_VALUE (TYPE_VALUES (enumtype)));
13114 /* ... plus one. */
ab76ca54
MM
13115 value = cp_build_binary_op (PLUS_EXPR,
13116 prev_value,
13117 integer_one_node);
68642fb6 13118
58595203
MM
13119 if (tree_int_cst_lt (value, prev_value))
13120 cp_error ("overflow in enumeration values at `%D'", name);
13121 }
13122 else
13123 value = integer_zero_node;
13124 }
13125
13126 /* Remove no-op casts from the value. */
13127 if (value)
13128 STRIP_TYPE_NOPS (value);
013bc8af 13129#if 0
58595203
MM
13130 /* To fix MAX_VAL enum consts. (bkoz) */
13131 TREE_TYPE (value) = integer_type_node;
013bc8af 13132#endif
58595203 13133 }
8d08fdba 13134
58595203
MM
13135 /* We always have to copy here; not all INTEGER_CSTs are unshared.
13136 Even in other cases, we will later (in finish_enum) be setting
13137 the type of VALUE. But, we don't need to make a copy if this
13138 VALUE is one of the enumeration constants for this same
13139 enumeration type. */
13140 for (values = TYPE_VALUES (enumtype); values; values = TREE_CHAIN (values))
13141 if (TREE_VALUE (values) == value)
13142 break;
13143 /* If we didn't break out of the loop, then we do need a copy. */
13144 if (!values && value)
13145 value = copy_node (value);
ed44da02 13146
8d08fdba 13147 /* C++ associates enums with global, function, or class declarations. */
58595203
MM
13148 context = current_scope ();
13149
13150 /* Build the actual enumeration constant. Note that the enumeration
13151 constants have the type of their initializers until the
13152 enumeration is complete:
13153
13154 [ dcl.enum ]
13155
13156 Following the closing brace of an enum-specifier, each enumer-
13157 ator has the type of its enumeration. Prior to the closing
13158 brace, the type of each enumerator is the type of its
13159 initializing value.
13160
13161 In finish_enum we will reset the type. Of course, if we're
13162 processing a template, there may be no value. */
13163 type = value ? TREE_TYPE (value) : NULL_TREE;
13164
13165 if (context && context == current_class_type)
13166 /* This enum declaration is local to the class. We need the full
8f17b5c5 13167 lang_decl so that we can record DECL_CLASS_CONTEXT, for example. */
58595203
MM
13168 decl = build_lang_decl (CONST_DECL, name, type);
13169 else
13170 /* It's a global enum, or it's local to a function. (Note local to
e8bd800e 13171 a function could mean local to a class method. */
58595203 13172 decl = build_decl (CONST_DECL, name, type);
e8bd800e 13173
58595203
MM
13174 DECL_CONTEXT (decl) = FROB_CONTEXT (context);
13175 DECL_INITIAL (decl) = value;
13176 TREE_READONLY (decl) = 1;
e8bd800e 13177
58595203
MM
13178 if (context && context == current_class_type)
13179 /* In something like `struct S { enum E { i = 7 }; };' we put `i'
8f032717
MM
13180 on the TYPE_FIELDS list for `S'. (That's so that you can say
13181 things like `S::i' later.) */
58595203
MM
13182 finish_member_declaration (decl);
13183 else
13184 {
13185 pushdecl (decl);
13186 GNU_xref_decl (current_function_decl, decl);
13187 }
13188
13189 /* Add this enumeration constant to the list for this type. */
13190 TYPE_VALUES (enumtype) = tree_cons (name, decl, TYPE_VALUES (enumtype));
8d08fdba
MS
13191}
13192
8d08fdba 13193\f
a8f73d4b
MM
13194/* We're defining DECL. Make sure that it's type is OK. */
13195
13196static void
f444e36b 13197check_function_type (decl, current_function_parms)
a8f73d4b 13198 tree decl;
f444e36b 13199 tree current_function_parms;
a8f73d4b
MM
13200{
13201 tree fntype = TREE_TYPE (decl);
d0f062fb 13202 tree return_type = complete_type (TREE_TYPE (fntype));
a8f73d4b
MM
13203
13204 /* In a function definition, arg types must be complete. */
13205 require_complete_types_for_parms (current_function_parms);
13206
d0f062fb 13207 if (!COMPLETE_OR_VOID_TYPE_P (return_type))
a8f73d4b
MM
13208 {
13209 cp_error ("return type `%#T' is incomplete", TREE_TYPE (fntype));
13210
13211 /* Make it return void instead, but don't change the
13212 type of the DECL_RESULT, in case we have a named return value. */
13213 if (TREE_CODE (fntype) == METHOD_TYPE)
13214 {
13215 tree ctype = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (fntype)));
13216 TREE_TYPE (decl)
13217 = build_cplus_method_type (ctype,
13218 void_type_node,
13219 FUNCTION_ARG_CHAIN (decl));
13220 }
13221 else
13222 TREE_TYPE (decl)
13223 = build_function_type (void_type_node,
13224 TYPE_ARG_TYPES (TREE_TYPE (decl)));
68642fb6 13225 TREE_TYPE (decl)
a8f73d4b
MM
13226 = build_exception_variant (fntype,
13227 TYPE_RAISES_EXCEPTIONS (fntype));
13228 }
13229 else
13230 abstract_virtuals_error (decl, TREE_TYPE (fntype));
13231}
13232
8d08fdba
MS
13233/* Create the FUNCTION_DECL for a function definition.
13234 DECLSPECS and DECLARATOR are the parts of the declaration;
13235 they describe the function's name and the type it returns,
13236 but twisted together in a fashion that parallels the syntax of C.
13237
a8f73d4b
MM
13238 FLAGS is a bitwise or of SF_PRE_PARSED (indicating that the
13239 DECLARATOR is really the DECL for the function we are about to
13240 process and that DECLSPECS should be ignored), SF_INCLASS_INLINE
f444e36b 13241 indicating that the function is an inline defined in-class.
68642fb6 13242
8d08fdba
MS
13243 This function creates a binding context for the function body
13244 as well as setting up the FUNCTION_DECL in current_function_decl.
13245
13246 Returns 1 on success. If the DECLARATOR is not suitable for a function
13247 (it defines a datum instead), we return 0, which tells
13248 yyparse to report a parse error.
13249
13250 For C++, we must first check whether that datum makes any sense.
13251 For example, "class A local_a(1,2);" means that variable local_a
13252 is an aggregate of type A, which should have a constructor
87e3dbc9 13253 applied to it with the argument list [1, 2]. */
8d08fdba
MS
13254
13255int
a8f73d4b 13256start_function (declspecs, declarator, attrs, flags)
c11b6f21 13257 tree declspecs, declarator, attrs;
a8f73d4b 13258 int flags;
8d08fdba 13259{
5566b478 13260 tree decl1;
8d08fdba
MS
13261 tree ctype = NULL_TREE;
13262 tree fntype;
13263 tree restype;
13264 extern int have_extern_spec;
13265 extern int used_extern_spec;
13266 int doing_friend = 0;
a8f73d4b 13267 struct binding_level *bl;
f444e36b 13268 tree current_function_parms;
8d08fdba 13269
8d08fdba 13270 /* Sanity check. */
a1774733 13271 my_friendly_assert (TREE_CODE (TREE_VALUE (void_list_node)) == VOID_TYPE, 160);
8d08fdba
MS
13272 my_friendly_assert (TREE_CHAIN (void_list_node) == NULL_TREE, 161);
13273
e92cc029 13274 /* This should only be done once on the top most decl. */
8d08fdba
MS
13275 if (have_extern_spec && !used_extern_spec)
13276 {
1f8f4a0b 13277 declspecs = tree_cons (NULL_TREE, get_identifier ("extern"), declspecs);
8d08fdba
MS
13278 used_extern_spec = 1;
13279 }
13280
a8f73d4b 13281 if (flags & SF_PRE_PARSED)
8d08fdba
MS
13282 {
13283 decl1 = declarator;
13284
8d08fdba
MS
13285 fntype = TREE_TYPE (decl1);
13286 if (TREE_CODE (fntype) == METHOD_TYPE)
13287 ctype = TYPE_METHOD_BASETYPE (fntype);
13288
cab1f180
ML
13289 /* ISO C++ 11.4/5. A friend function defined in a class is in
13290 the (lexical) scope of the class in which it is defined. */
8d08fdba
MS
13291 if (!ctype && DECL_FRIEND_P (decl1))
13292 {
4f1c5b7d 13293 ctype = DECL_FRIEND_CONTEXT (decl1);
8d08fdba
MS
13294
13295 /* CTYPE could be null here if we're dealing with a template;
13296 for example, `inline friend float foo()' inside a template
13297 will have no CTYPE set. */
13298 if (ctype && TREE_CODE (ctype) != RECORD_TYPE)
13299 ctype = NULL_TREE;
13300 else
13301 doing_friend = 1;
13302 }
13303
5566b478
MS
13304 last_function_parms = DECL_ARGUMENTS (decl1);
13305 last_function_parm_tags = NULL_TREE;
8d08fdba
MS
13306 }
13307 else
13308 {
c11b6f21 13309 decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, 1, NULL_TREE);
8d08fdba
MS
13310 /* If the declarator is not suitable for a function definition,
13311 cause a syntax error. */
13312 if (decl1 == NULL_TREE || TREE_CODE (decl1) != FUNCTION_DECL) return 0;
13313
13314 fntype = TREE_TYPE (decl1);
13315
13316 restype = TREE_TYPE (fntype);
7ddedda4 13317 if (CLASS_TYPE_P (restype) && !CLASSTYPE_GOT_SEMICOLON (restype))
8d08fdba 13318 {
8251199e 13319 cp_error ("semicolon missing after declaration of `%#T'", restype);
051e6fd7 13320 shadow_tag (build_tree_list (NULL_TREE, restype));
8d08fdba
MS
13321 CLASSTYPE_GOT_SEMICOLON (restype) = 1;
13322 if (TREE_CODE (fntype) == FUNCTION_TYPE)
13323 fntype = build_function_type (integer_type_node,
13324 TYPE_ARG_TYPES (fntype));
13325 else
13326 fntype = build_cplus_method_type (build_type_variant (TYPE_METHOD_BASETYPE (fntype), TREE_READONLY (decl1), TREE_SIDE_EFFECTS (decl1)),
13327 integer_type_node,
13328 TYPE_ARG_TYPES (fntype));
13329 TREE_TYPE (decl1) = fntype;
13330 }
13331
13332 if (TREE_CODE (fntype) == METHOD_TYPE)
13333 ctype = TYPE_METHOD_BASETYPE (fntype);
35680744 13334 else if (DECL_MAIN_P (decl1))
8d08fdba
MS
13335 {
13336 /* If this doesn't return integer_type, complain. */
13337 if (TREE_TYPE (TREE_TYPE (decl1)) != integer_type_node)
13338 {
a28e3c7f 13339 if (pedantic || warn_return_type)
8251199e 13340 pedwarn ("return type for `main' changed to `int'");
8d08fdba
MS
13341 TREE_TYPE (decl1) = fntype = default_function_type;
13342 }
8d08fdba
MS
13343 }
13344 }
68642fb6 13345
b35d4555
MM
13346 /* Sometimes we don't notice that a function is a static member, and
13347 build a METHOD_TYPE for it. Fix that up now. */
13348 if (ctype != NULL_TREE && DECL_STATIC_FUNCTION_P (decl1)
13349 && TREE_CODE (TREE_TYPE (decl1)) == METHOD_TYPE)
13350 {
3afb32a4 13351 revert_static_member_fn (decl1);
b35d4555
MM
13352 last_function_parms = TREE_CHAIN (last_function_parms);
13353 ctype = NULL_TREE;
13354 }
8d08fdba
MS
13355
13356 /* Warn if function was previously implicitly declared
13357 (but not if we warned then). */
13358 if (! warn_implicit
13359 && IDENTIFIER_IMPLICIT_DECL (DECL_NAME (decl1)) != NULL_TREE)
8251199e 13360 cp_warning_at ("`%D' implicitly declared before its definition", IDENTIFIER_IMPLICIT_DECL (DECL_NAME (decl1)));
8d08fdba 13361
f181d4ae
MM
13362 /* Set up current_class_type, and enter the scope of the class, if
13363 appropriate. */
13364 if (ctype)
13365 push_nested_class (ctype, 1);
13366 else if (DECL_STATIC_FUNCTION_P (decl1))
13367 push_nested_class (DECL_CONTEXT (decl1), 2);
13368
13369 /* Now that we have entered the scope of the class, we must restore
13370 the bindings for any template parameters surrounding DECL1, if it
13371 is an inline member template. (Order is important; consider the
13372 case where a template parameter has the same name as a field of
13373 the class.) It is not until after this point that
13374 PROCESSING_TEMPLATE_DECL is guaranteed to be set up correctly. */
a8f73d4b 13375 if (flags & SF_INCLASS_INLINE)
f181d4ae
MM
13376 maybe_begin_member_template_processing (decl1);
13377
56cb9733 13378 /* Effective C++ rule 15. */
9188c363 13379 if (warn_ecpp
596ea4e5 13380 && DECL_OVERLOADED_OPERATOR_P (decl1) == NOP_EXPR
9188c363
MM
13381 && TREE_CODE (TREE_TYPE (fntype)) == VOID_TYPE)
13382 cp_warning ("`operator=' should return a reference to `*this'");
13383
13384 /* Make the init_value nonzero so pushdecl knows this is not tentative.
13385 error_mark_node is replaced below (in poplevel) with the BLOCK. */
b35d4555
MM
13386 if (!DECL_INITIAL (decl1))
13387 DECL_INITIAL (decl1) = error_mark_node;
9188c363
MM
13388
13389#ifdef SET_DEFAULT_DECL_ATTRIBUTES
13390 SET_DEFAULT_DECL_ATTRIBUTES (decl1, attrs);
13391#endif
68642fb6 13392
9188c363
MM
13393 /* This function exists in static storage.
13394 (This does not mean `static' in the C sense!) */
13395 TREE_STATIC (decl1) = 1;
13396
13397 /* We must call push_template_decl after current_class_type is set
13398 up. (If we are processing inline definitions after exiting a
13399 class scope, current_class_type will be NULL_TREE until set above
13400 by push_nested_class.) */
13401 if (processing_template_decl)
13402 decl1 = push_template_decl (decl1);
13403
f181d4ae 13404 /* We are now in the scope of the function being defined. */
8d08fdba 13405 current_function_decl = decl1;
f181d4ae 13406
5566b478
MS
13407 /* Save the parm names or decls from this function's declarator
13408 where store_parm_decls will find them. */
13409 current_function_parms = last_function_parms;
13410 current_function_parm_tags = last_function_parm_tags;
8d08fdba 13411
a8f73d4b
MM
13412 /* Make sure the parameter and return types are reasonable. When
13413 you declare a function, these types can be incomplete, but they
13414 must be complete when you define the function. */
5156628f 13415 if (! processing_template_decl)
f444e36b 13416 check_function_type (decl1, current_function_parms);
f181d4ae 13417
a8f73d4b
MM
13418 /* Build the return declaration for the function. */
13419 restype = TREE_TYPE (fntype);
13420 if (!processing_template_decl)
13421 {
13422 if (!DECL_RESULT (decl1))
5566b478 13423 {
5566b478 13424 DECL_RESULT (decl1)
a8f73d4b 13425 = build_decl (RESULT_DECL, 0, TYPE_MAIN_VARIANT (restype));
68642fb6
UD
13426 c_apply_type_quals_to_decl (CP_TYPE_QUALS (restype),
13427 DECL_RESULT (decl1));
5566b478 13428 }
5566b478 13429 }
a8f73d4b
MM
13430 else
13431 /* Just use `void'. Nobody will ever look at this anyhow. */
13432 DECL_RESULT (decl1) = build_decl (RESULT_DECL, 0, void_type_node);
13433
13434 /* Initialize RTL machinery. We cannot do this until
13435 CURRENT_FUNCTION_DECL and DECL_RESULT are set up. We do this
13436 even when processing a template; this is how we get
c00996a3
JM
13437 CFUN set up, and our per-function variables initialized.
13438 FIXME factor out the non-RTL stuff. */
a8f73d4b
MM
13439 bl = current_binding_level;
13440 init_function_start (decl1, input_filename, lineno);
13441 current_binding_level = bl;
a8f73d4b
MM
13442
13443 /* Even though we're inside a function body, we still don't want to
13444 call expand_expr to calculate the size of a variable-sized array.
13445 We haven't necessarily assigned RTL to all variables yet, so it's
13446 not safe to try to expand expressions involving them. */
13447 immediate_size_expand = 0;
01d939e8 13448 cfun->x_dont_save_pending_sizes_p = 1;
a8f73d4b 13449
f444e36b
MM
13450 /* Start the statement-tree, start the tree now. */
13451 begin_stmt_tree (&DECL_SAVED_TREE (decl1));
6f80451c 13452
a8f73d4b 13453 /* Let the user know we're compiling this function. */
ea11ca7e 13454 announce_function (decl1);
b7484fbe 13455
878cd289
MS
13456 /* Record the decl so that the function name is defined.
13457 If we already have a decl for this name, and it is a FUNCTION_DECL,
13458 use the old decl. */
a8f73d4b 13459 if (!processing_template_decl && !(flags & SF_PRE_PARSED))
878cd289 13460 {
75650646 13461 /* A specialization is not used to guide overload resolution. */
2228d450 13462 if (!DECL_TEMPLATE_SPECIALIZATION (decl1)
959d8796 13463 && ! DECL_FUNCTION_MEMBER_P (decl1))
75650646 13464 decl1 = pushdecl (decl1);
2c73f9f5 13465 else
b7698cf0
JM
13466 {
13467 /* We need to set the DECL_CONTEXT. */
13468 if (!DECL_CONTEXT (decl1) && DECL_TEMPLATE_INFO (decl1))
13469 DECL_CONTEXT (decl1) = DECL_CONTEXT (DECL_TI_TEMPLATE (decl1));
13470 /* And make sure we have enough default args. */
13471 check_default_args (decl1);
13472 }
878cd289
MS
13473 fntype = TREE_TYPE (decl1);
13474 }
5566b478 13475
a8f73d4b 13476 /* Reset these in case the call to pushdecl changed them. */
5566b478 13477 current_function_decl = decl1;
01d939e8 13478 cfun->decl = decl1;
878cd289 13479
78c120b5
MM
13480 /* If we are (erroneously) defining a function that we have already
13481 defined before, wipe out what we knew before. */
5362b086 13482 if (!DECL_PENDING_INLINE_P (decl1)
78c120b5
MM
13483 && DECL_SAVED_FUNCTION_DATA (decl1))
13484 {
13485 free (DECL_SAVED_FUNCTION_DATA (decl1));
13486 DECL_SAVED_FUNCTION_DATA (decl1) = NULL;
13487 }
b35d4555 13488
f444e36b 13489 if (ctype && !doing_friend && !DECL_STATIC_FUNCTION_P (decl1))
b35d4555
MM
13490 {
13491 /* We know that this was set up by `grokclassfn'. We do not
13492 wait until `store_parm_decls', since evil parse errors may
13493 never get us to that point. Here we keep the consistency
13494 between `current_class_type' and `current_class_ptr'. */
13495 tree t = DECL_ARGUMENTS (decl1);
68642fb6
UD
13496
13497 my_friendly_assert (t != NULL_TREE && TREE_CODE (t) == PARM_DECL,
b35d4555
MM
13498 162);
13499 my_friendly_assert (TREE_CODE (TREE_TYPE (t)) == POINTER_TYPE,
13500 19990811);
68642fb6
UD
13501
13502 cp_function_chain->x_current_class_ref
b35d4555
MM
13503 = build_indirect_ref (t, NULL_PTR);
13504 cp_function_chain->x_current_class_ptr = t;
13505
018fc244
MM
13506 /* Constructors and destructors need to know whether they're "in
13507 charge" of initializing virtual base classes. */
e0fff4b3 13508 t = TREE_CHAIN (t);
454fa7a7 13509 if (DECL_HAS_IN_CHARGE_PARM_P (decl1))
e0fff4b3
JM
13510 {
13511 current_in_charge_parm = t;
13512 t = TREE_CHAIN (t);
13513 }
13514 if (DECL_HAS_VTT_PARM_P (decl1))
13515 {
13516 if (DECL_NAME (t) != vtt_parm_identifier)
13517 abort ();
13518 current_vtt_parm = t;
13519 }
b35d4555
MM
13520 }
13521
db5ae43f 13522 if (DECL_INTERFACE_KNOWN (decl1))
faae18ab 13523 {
4f1c5b7d 13524 tree ctx = decl_function_context (decl1);
86052cc3 13525
faae18ab
MS
13526 if (DECL_NOT_REALLY_EXTERN (decl1))
13527 DECL_EXTERNAL (decl1) = 0;
86052cc3 13528
68642fb6 13529 if (ctx != NULL_TREE && DECL_THIS_INLINE (ctx)
86052cc3
JM
13530 && TREE_PUBLIC (ctx))
13531 /* This is a function in a local class in an extern inline
13532 function. */
13533 comdat_linkage (decl1);
faae18ab 13534 }
8d08fdba
MS
13535 /* If this function belongs to an interface, it is public.
13536 If it belongs to someone else's interface, it is also external.
1f901793 13537 This only affects inlines and template instantiations. */
5566b478
MS
13538 else if (interface_unknown == 0
13539 && (! DECL_TEMPLATE_INSTANTIATION (decl1)
d11ad92e 13540 || flag_alt_external_templates))
8d08fdba 13541 {
5566b478 13542 if (DECL_THIS_INLINE (decl1) || DECL_TEMPLATE_INSTANTIATION (decl1)
5156628f 13543 || processing_template_decl)
1f901793
JM
13544 {
13545 DECL_EXTERNAL (decl1)
13546 = (interface_only
9c73ec84
MS
13547 || (DECL_THIS_INLINE (decl1) && ! flag_implement_inlines
13548 && !DECL_VINDEX (decl1)));
1f901793
JM
13549
13550 /* For WIN32 we also want to put these in linkonce sections. */
13551 maybe_make_one_only (decl1);
13552 }
db5ae43f 13553 else
893de33c 13554 DECL_EXTERNAL (decl1) = 0;
e8abc66f 13555 DECL_NOT_REALLY_EXTERN (decl1) = 0;
db5ae43f 13556 DECL_INTERFACE_KNOWN (decl1) = 1;
8d08fdba 13557 }
c16c47fb
JM
13558 else if (interface_unknown && interface_only
13559 && (! DECL_TEMPLATE_INSTANTIATION (decl1)
13560 || flag_alt_external_templates))
13561 {
13562 /* If MULTIPLE_SYMBOL_SPACES is defined and we saw a #pragma
13563 interface, we will have interface_only set but not
13564 interface_known. In that case, we don't want to use the normal
13565 heuristics because someone will supply a #pragma implementation
13566 elsewhere, and deducing it here would produce a conflict. */
13567 comdat_linkage (decl1);
13568 DECL_EXTERNAL (decl1) = 0;
13569 DECL_INTERFACE_KNOWN (decl1) = 1;
13570 DECL_DEFER_OUTPUT (decl1) = 1;
13571 }
8d08fdba 13572 else
a0a33927
MS
13573 {
13574 /* This is a definition, not a reference.
b7484fbe
MS
13575 So clear DECL_EXTERNAL. */
13576 DECL_EXTERNAL (decl1) = 0;
faae18ab 13577
5566b478
MS
13578 if ((DECL_THIS_INLINE (decl1) || DECL_TEMPLATE_INSTANTIATION (decl1))
13579 && ! DECL_INTERFACE_KNOWN (decl1)
13580 /* Don't try to defer nested functions for now. */
4f1c5b7d 13581 && ! decl_function_context (decl1))
878cd289
MS
13582 DECL_DEFER_OUTPUT (decl1) = 1;
13583 else
893de33c 13584 DECL_INTERFACE_KNOWN (decl1) = 1;
db5ae43f 13585 }
a9aedbc2 13586
f444e36b
MM
13587 pushlevel (0);
13588 current_binding_level->parm_flag = 1;
8d08fdba 13589
f30432d7
MS
13590 if (attrs)
13591 cplus_decl_attributes (decl1, NULL_TREE, attrs);
68642fb6 13592
8d08fdba
MS
13593 /* Promote the value to int before returning it. */
13594 if (C_PROMOTING_INTEGER_TYPE_P (restype))
5566b478
MS
13595 restype = type_promotes_to (restype);
13596
8d08fdba 13597 if (DECL_RESULT (decl1) == NULL_TREE)
f30432d7
MS
13598 {
13599 DECL_RESULT (decl1)
13600 = build_decl (RESULT_DECL, 0, TYPE_MAIN_VARIANT (restype));
91063b51
MM
13601 TREE_READONLY (DECL_RESULT (decl1)) = CP_TYPE_CONST_P (restype);
13602 TREE_THIS_VOLATILE (DECL_RESULT (decl1)) = CP_TYPE_VOLATILE_P (restype);
f30432d7 13603 }
8d08fdba 13604
5566b478
MS
13605 ++function_depth;
13606
0d9eb3ba 13607 if (DECL_DESTRUCTOR_P (decl1))
46e8c075
MM
13608 {
13609 dtor_label = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE);
13610 DECL_CONTEXT (dtor_label) = current_function_decl;
13611 }
8d08fdba 13612
f444e36b
MM
13613 store_parm_decls (current_function_parms);
13614
8d08fdba
MS
13615 return 1;
13616}
13617\f
13618/* Store the parameter declarations into the current function declaration.
13619 This is called after parsing the parameter declarations, before
13620 digesting the body of the function.
13621
13622 Also install to binding contour return value identifier, if any. */
13623
f444e36b
MM
13624static void
13625store_parm_decls (current_function_parms)
13626 tree current_function_parms;
8d08fdba
MS
13627{
13628 register tree fndecl = current_function_decl;
13629 register tree parm;
13630 int parms_have_cleanups = 0;
eb66be0e 13631 tree cleanups = NULL_TREE;
8d08fdba 13632
8d08fdba
MS
13633 /* This is a list of types declared among parms in a prototype. */
13634 tree parmtags = current_function_parm_tags;
13635
13636 /* This is a chain of any other decls that came in among the parm
13637 declarations. If a parm is declared with enum {foo, bar} x;
13638 then CONST_DECLs for foo and bar are put here. */
13639 tree nonparms = NULL_TREE;
13640
b35d4555 13641 if (current_function_parms)
8d08fdba
MS
13642 {
13643 /* This case is when the function was defined with an ANSI prototype.
13644 The parms already have decls, so we need not do anything here
13645 except record them as in effect
13646 and complain if any redundant old-style parm decls were written. */
13647
b35d4555
MM
13648 tree specparms = current_function_parms;
13649 tree next;
13650
f444e36b 13651 /* Must clear this because it might contain TYPE_DECLs declared
b35d4555 13652 at class level. */
f444e36b 13653 storedecls (NULL_TREE);
8d08fdba 13654
f444e36b 13655 /* If we're doing semantic analysis, then we'll call pushdecl
b35d4555
MM
13656 for each of these. We must do them in reverse order so that
13657 they end in the correct forward order. */
f444e36b 13658 specparms = nreverse (specparms);
5566b478 13659
b35d4555 13660 for (parm = specparms; parm; parm = next)
8d08fdba
MS
13661 {
13662 next = TREE_CHAIN (parm);
13663 if (TREE_CODE (parm) == PARM_DECL)
13664 {
f444e36b 13665 tree cleanup;
fe1b3b96 13666
f444e36b
MM
13667 if (DECL_NAME (parm) == NULL_TREE
13668 || TREE_CODE (parm) != VOID_TYPE)
13669 pushdecl (parm);
13670 else
13671 cp_error ("parameter `%D' declared void", parm);
eb66be0e 13672
5362b086 13673 cleanup = (processing_template_decl
f444e36b
MM
13674 ? NULL_TREE
13675 : maybe_build_cleanup (parm));
68642fb6 13676
f444e36b
MM
13677 if (cleanup)
13678 cleanups = tree_cons (parm, cleanup, cleanups);
8d08fdba
MS
13679 }
13680 else
13681 {
13682 /* If we find an enum constant or a type tag,
13683 put it aside for the moment. */
13684 TREE_CHAIN (parm) = NULL_TREE;
13685 nonparms = chainon (nonparms, parm);
13686 }
13687 }
13688
f444e36b
MM
13689 /* Get the decls in their original chain order and record in the
13690 function. This is all and only the PARM_DECLs that were
13691 pushed into scope by the loop above. */
13692 DECL_ARGUMENTS (fndecl) = getdecls ();
13693 storetags (chainon (parmtags, gettags ()));
8d08fdba
MS
13694 }
13695 else
13696 DECL_ARGUMENTS (fndecl) = NULL_TREE;
13697
13698 /* Now store the final chain of decls for the arguments
13699 as the decl-chain of the current lexical scope.
13700 Put the enumerators in as well, at the front so that
13701 DECL_ARGUMENTS is not modified. */
f444e36b 13702 storedecls (chainon (nonparms, DECL_ARGUMENTS (fndecl)));
eb448459 13703
eb66be0e
MS
13704 /* Now that we have initialized the parms, we can start their
13705 cleanups. We cannot do this before, since expand_decl_cleanup
13706 should not be called before the parm can be used. */
fe1b3b96
MM
13707 while (cleanups)
13708 {
68642fb6 13709 finish_decl_cleanup (TREE_PURPOSE (cleanups),
fe1b3b96
MM
13710 TREE_VALUE (cleanups));
13711 cleanups = TREE_CHAIN (cleanups);
13712 }
eb66be0e 13713
8d08fdba 13714 /* Create a binding contour which can be used to catch
f444e36b 13715 cleanup-generated temporaries. */
8d08fdba 13716 if (parms_have_cleanups)
f444e36b 13717 pushlevel (0);
8d08fdba 13718
b35d4555 13719 /* Do the starting of the exception specifications, if we have any. */
68642fb6 13720 if (flag_exceptions && !processing_template_decl
1660cb3a 13721 && flag_enforce_eh_specs
b35d4555 13722 && TYPE_RAISES_EXCEPTIONS (TREE_TYPE (current_function_decl)))
52a11cbf 13723 current_eh_spec_block = begin_eh_spec_block ();
8d08fdba
MS
13724}
13725
8d08fdba 13726\f
59026e79
MM
13727/* We have finished doing semantic analysis on DECL, but have not yet
13728 generated RTL for its body. Save away our current state, so that
13729 when we want to generate RTL later we know what to do. */
13730
13731static void
13732save_function_data (decl)
13733 tree decl;
13734{
ae499cce 13735 struct cp_language_function *f;
59026e79
MM
13736
13737 /* Save the language-specific per-function data so that we can
13738 get it back when we really expand this function. */
13739 my_friendly_assert (!DECL_PENDING_INLINE_P (decl),
13740 19990908);
68642fb6 13741
59026e79 13742 /* Make a copy. */
ae499cce
MM
13743 f = ((struct cp_language_function *)
13744 xmalloc (sizeof (struct cp_language_function)));
4e135bdd 13745 memcpy (f, cp_function_chain, sizeof (struct cp_language_function));
59026e79
MM
13746 DECL_SAVED_FUNCTION_DATA (decl) = f;
13747
13748 /* Clear out the bits we don't need. */
ae499cce
MM
13749 f->base.x_stmt_tree.x_last_stmt = NULL_TREE;
13750 f->base.x_stmt_tree.x_last_expr_type = NULL_TREE;
59026e79
MM
13751 f->x_named_label_uses = NULL;
13752 f->bindings = NULL;
8e4ce833 13753 f->x_local_names = NULL;
59026e79
MM
13754
13755 /* When we get back here again, we will be expanding. */
13756 f->x_expanding_p = 1;
914653a2
MM
13757
13758 /* If we've already decided that we cannot inline this function, we
13759 must remember that fact when we actually go to expand the
13760 function. */
13761 f->cannot_inline = current_function_cannot_inline;
59026e79
MM
13762}
13763
efee38a9
MM
13764/* At the end of every constructor we generate to code to return
13765 `this'. Do that now. */
13766
13767static void
13768finish_constructor_body ()
13769{
13770 /* Any return from a constructor will end up here. */
3dbc07b6 13771 if (ctor_label)
ae499cce 13772 add_stmt (build_stmt (LABEL_STMT, ctor_label));
efee38a9
MM
13773
13774 /* Clear CTOR_LABEL so that finish_return_stmt knows to really
13775 generate the return, rather than a goto to CTOR_LABEL. */
13776 ctor_label = NULL_TREE;
13777 /* In check_return_expr we translate an empty return from a
13778 constructor to a return of `this'. */
13779 finish_return_stmt (NULL_TREE);
46e8c075 13780 /* Mark the end of the constructor. */
ae499cce 13781 add_stmt (build_stmt (CTOR_STMT));
efee38a9
MM
13782}
13783
9bfadf57
MM
13784/* At the end of every destructor we generate code to restore virtual
13785 function tables to the values desired by base classes and to call
efee38a9 13786 to base class destructors. Do that now. */
9bfadf57
MM
13787
13788static void
13789finish_destructor_body ()
13790{
13791 tree compound_stmt;
9bfadf57
MM
13792 tree exprstmt;
13793
13794 /* Create a block to contain all the extra code. */
13795 compound_stmt = begin_compound_stmt (/*has_no_scope=*/0);
13796
efee38a9 13797 /* Any return from a destructor will end up here. */
ae499cce 13798 add_stmt (build_stmt (LABEL_STMT, dtor_label));
efee38a9 13799
9bfadf57
MM
13800 /* Generate the code to call destructor on base class. If this
13801 destructor belongs to a class with virtual functions, then set
13802 the virtual function table pointer to represent the type of our
13803 base class. */
13804
13805 /* This side-effect makes call to `build_delete' generate the code
13806 we have to have at the end of this destructor. `build_delete'
13807 will set the flag again. */
13808 TYPE_HAS_DESTRUCTOR (current_class_type) = 0;
13809
9bfadf57 13810 exprstmt = build_delete (current_class_type,
68642fb6 13811 current_class_ref,
86f45d2c 13812 sfk_base_destructor,
68642fb6 13813 LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR|LOOKUP_NORMAL,
9bfadf57
MM
13814 0);
13815
13816 if (exprstmt != error_mark_node
13817 && (TREE_CODE (exprstmt) != NOP_EXPR
13818 || TREE_OPERAND (exprstmt, 0) != integer_zero_node
13819 || TYPE_USES_VIRTUAL_BASECLASSES (current_class_type)))
13820 {
9bfadf57
MM
13821 if (exprstmt != void_zero_node)
13822 /* Don't call `expand_expr_stmt' if we're not going to do
13823 anything, since -Wall will give a diagnostic. */
13824 finish_expr_stmt (exprstmt);
13825
efee38a9 13826 /* Run destructors for all virtual baseclasses. */
9bfadf57
MM
13827 if (TYPE_USES_VIRTUAL_BASECLASSES (current_class_type))
13828 {
a55583e9
MM
13829 tree vbases;
13830 tree if_stmt;
13831
13832 if_stmt = begin_if_stmt ();
9bfadf57 13833 finish_if_stmt_cond (build (BIT_AND_EXPR, integer_type_node,
68642fb6 13834 current_in_charge_parm,
9bfadf57
MM
13835 integer_two_node),
13836 if_stmt);
13837
a55583e9
MM
13838 vbases = CLASSTYPE_VBASECLASSES (current_class_type);
13839 /* The CLASSTYPE_VBASECLASSES list is in initialization
13840 order, so we have to march through it in reverse order. */
13841 for (vbases = nreverse (copy_list (vbases));
13842 vbases;
13843 vbases = TREE_CHAIN (vbases))
9bfadf57 13844 {
a55583e9 13845 tree vbase = TREE_VALUE (vbases);
9f724b6a 13846 tree base_type = BINFO_TYPE (vbase);
a55583e9 13847
9f724b6a 13848 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (base_type))
9bfadf57 13849 {
9f724b6a
NS
13850 tree base_ptr_type = build_pointer_type (base_type);
13851 tree expr = current_class_ptr;
13852
13853 /* Convert to the basetype here, as we know the layout is
13854 fixed. What is more, if we let build_method_call do it,
13855 it will use the vtable, which may have been clobbered
13856 by the deletion of our primary base. */
13857
13858 expr = build1 (NOP_EXPR, base_ptr_type, expr);
13859 expr = build (PLUS_EXPR, base_ptr_type, expr,
13860 BINFO_OFFSET (vbase));
13861 expr = build_indirect_ref (expr, NULL);
13862 expr = build_method_call (expr, base_dtor_identifier,
13863 NULL_TREE, vbase,
13864 LOOKUP_NORMAL);
13865 finish_expr_stmt (expr);
9bfadf57 13866 }
9bfadf57
MM
13867 }
13868
13869 finish_then_clause (if_stmt);
13870 finish_if_stmt ();
13871 }
13872 }
68642fb6 13873
52682a1b
MM
13874 /* In a virtual destructor, we must call delete. */
13875 if (DECL_VIRTUAL_P (current_function_decl))
13876 {
13877 tree if_stmt;
13878 tree virtual_size = c_sizeof (current_class_type);
68642fb6 13879
52682a1b 13880 /* [class.dtor]
68642fb6 13881
52682a1b
MM
13882 At the point of definition of a virtual destructor (including
13883 an implicit definition), non-placement operator delete shall
13884 be looked up in the scope of the destructor's class and if
13885 found shall be accessible and unambiguous. */
13886 exprstmt = build_op_delete_call
13887 (DELETE_EXPR, current_class_ptr, virtual_size,
13888 LOOKUP_NORMAL | LOOKUP_SPECULATIVELY, NULL_TREE);
298d6f60 13889
52682a1b
MM
13890 if_stmt = begin_if_stmt ();
13891 finish_if_stmt_cond (build (BIT_AND_EXPR, integer_type_node,
13892 current_in_charge_parm,
13893 integer_one_node),
13894 if_stmt);
13895 finish_expr_stmt (exprstmt);
13896 finish_then_clause (if_stmt);
13897 finish_if_stmt ();
13898 }
9bfadf57
MM
13899
13900 /* Close the block we started above. */
13901 finish_compound_stmt (/*has_no_scope=*/0, compound_stmt);
13902}
13903
8d08fdba
MS
13904/* Finish up a function declaration and compile that function
13905 all the way to assembler language output. The free the storage
13906 for the function definition.
13907
68642fb6
UD
13908 FLAGS is a bitwise or of the following values:
13909 1 - CALL_POPLEVEL
f181d4ae
MM
13910 An extra call to poplevel (and expand_end_bindings) must be
13911 made to take care of the binding contour for the base
13912 initializers. This is only relevant for constructors.
13913 2 - INCLASS_INLINE
13914 We just finished processing the body of an in-class inline
13915 function definition. (This processing will have taken place
87e3dbc9 13916 after the class definition is complete.) */
8d08fdba 13917
4d6abc1c 13918tree
0acf7199 13919finish_function (flags)
f181d4ae 13920 int flags;
8d08fdba
MS
13921{
13922 register tree fndecl = current_function_decl;
13923 tree fntype, ctype = NULL_TREE;
f181d4ae
MM
13924 int call_poplevel = (flags & 1) != 0;
13925 int inclass_inline = (flags & 2) != 0;
87e3dbc9 13926 int nested;
8d08fdba
MS
13927
13928 /* When we get some parse errors, we can end up without a
13929 current_function_decl, so cope. */
13930 if (fndecl == NULL_TREE)
4d6abc1c 13931 return error_mark_node;
8d08fdba 13932
87e3dbc9 13933 nested = function_depth > 1;
8d08fdba
MS
13934 fntype = TREE_TYPE (fndecl);
13935
9bfadf57
MM
13936 /* TREE_READONLY (fndecl) = 1;
13937 This caused &foo to be of type ptr-to-const-function
13938 which then got a warning when stored in a ptr-to-function variable. */
8d08fdba 13939
f444e36b 13940 my_friendly_assert (building_stmt_tree (), 20000911);
8d08fdba 13941
db9b2174
MM
13942 /* For a cloned function, we've already got all the code we need;
13943 there's no need to add any extra bits. */
f444e36b 13944 if (!DECL_CLONED_FUNCTION_P (fndecl))
8d08fdba 13945 {
efee38a9
MM
13946 if (DECL_CONSTRUCTOR_P (fndecl))
13947 {
13948 finish_constructor_body ();
13949 if (call_poplevel)
13950 do_poplevel ();
13951 }
9bfadf57
MM
13952 else if (DECL_DESTRUCTOR_P (fndecl) && !processing_template_decl)
13953 finish_destructor_body ();
efee38a9
MM
13954 else if (DECL_MAIN_P (fndecl))
13955 {
13956 /* Make it so that `main' always returns 0 by default. */
13957#ifdef VMS
13958 finish_return_stmt (integer_one_node);
13959#else
13960 finish_return_stmt (integer_zero_node);
13961#endif
13962 }
87e3dbc9 13963
b35d4555
MM
13964 /* Finish dealing with exception specifiers. */
13965 if (flag_exceptions && !processing_template_decl
1660cb3a 13966 && flag_enforce_eh_specs
b35d4555 13967 && TYPE_RAISES_EXCEPTIONS (TREE_TYPE (current_function_decl)))
52a11cbf
RH
13968 finish_eh_spec_block (TYPE_RAISES_EXCEPTIONS
13969 (TREE_TYPE (current_function_decl)),
13970 current_eh_spec_block);
5566b478 13971 }
68642fb6 13972
558475f0 13973 /* If we're saving up tree structure, tie off the function now. */
f444e36b 13974 finish_stmt_tree (&DECL_SAVED_TREE (fndecl));
8d2733ca 13975
8d08fdba
MS
13976 /* This must come after expand_function_end because cleanups might
13977 have declarations (from inline functions) that need to go into
13978 this function's blocks. */
f444e36b
MM
13979 if (current_binding_level->parm_flag != 1)
13980 my_friendly_abort (122);
13981 poplevel (1, 0, 1);
8d08fdba 13982
a8f73d4b 13983 /* Remember that we were in class scope. */
db5ae43f 13984 if (current_class_name)
a8f73d4b 13985 ctype = current_class_type;
db5ae43f 13986
1caa11d3
MM
13987 /* Must mark the RESULT_DECL as being in this function. */
13988 DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
13989
13990 /* Set the BLOCK_SUPERCONTEXT of the outermost function scope to point
13991 to the FUNCTION_DECL node itself. */
13992 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
13993
59026e79 13994 /* Save away current state, if appropriate. */
f444e36b 13995 if (!processing_template_decl)
59026e79
MM
13996 save_function_data (fndecl);
13997
95fabfd3
MM
13998 /* If this function calls `setjmp' it cannot be inlined. When
13999 `longjmp' is called it is not guaranteed to restore the value of
14000 local variables that have been modified since the call to
14001 `setjmp'. So, if were to inline this function into some caller
14002 `c', then when we `longjmp', we might not restore all variables
14003 in `c'. (It might seem, at first blush, that there's no way for
14004 this function to modify local variables in `c', but their
14005 addresses may have been stored somewhere accessible to this
14006 function.) */
f444e36b 14007 if (!processing_template_decl && calls_setjmp_p (fndecl))
95fabfd3
MM
14008 DECL_UNINLINABLE (fndecl) = 1;
14009
f444e36b
MM
14010 /* Clear out memory we no longer need. */
14011 free_after_parsing (cfun);
14012 /* Since we never call rest_of_compilation, we never clear
14013 CFUN. Do so explicitly. */
14014 free_after_compilation (cfun);
14015 cfun = NULL;
a8f73d4b
MM
14016
14017 /* If this is a in-class inline definition, we may have to pop the
14018 bindings for the template parameters that we added in
14019 maybe_begin_member_template_processing when start_function was
14020 called. */
14021 if (inclass_inline)
14022 maybe_end_member_template_processing ();
14023
14024 /* Leave the scope of the class. */
14025 if (ctype)
14026 pop_nested_class ();
5566b478
MS
14027
14028 --function_depth;
8d08fdba 14029
4d6abc1c 14030 /* Clean up. */
28cbf42c 14031 if (! nested)
1f8f4a0b
MM
14032 /* Let the error reporting routines know that we're outside a
14033 function. For a nested function, this value is used in
14034 pop_cp_function_context and then reset via pop_function_context. */
14035 current_function_decl = NULL_TREE;
4d6abc1c
MM
14036
14037 return fndecl;
8d08fdba
MS
14038}
14039\f
14040/* Create the FUNCTION_DECL for a function definition.
8d08fdba
MS
14041 DECLSPECS and DECLARATOR are the parts of the declaration;
14042 they describe the return type and the name of the function,
14043 but twisted together in a fashion that parallels the syntax of C.
14044
14045 This function creates a binding context for the function body
14046 as well as setting up the FUNCTION_DECL in current_function_decl.
14047
14048 Returns a FUNCTION_DECL on success.
14049
14050 If the DECLARATOR is not suitable for a function (it defines a datum
14051 instead), we return 0, which tells yyparse to report a parse error.
14052
14053 May return void_type_node indicating that this method is actually
14054 a friend. See grokfield for more details.
14055
14056 Came here with a `.pushlevel' .
14057
14058 DO NOT MAKE ANY CHANGES TO THIS CODE WITHOUT MAKING CORRESPONDING
14059 CHANGES TO CODE IN `grokfield'. */
e92cc029 14060
8d08fdba 14061tree
acf82af2
JM
14062start_method (declspecs, declarator, attrlist)
14063 tree declarator, declspecs, attrlist;
8d08fdba 14064{
c11b6f21 14065 tree fndecl = grokdeclarator (declarator, declspecs, MEMFUNCDEF, 0,
acf82af2 14066 attrlist);
8d08fdba
MS
14067
14068 /* Something too ugly to handle. */
14069 if (fndecl == NULL_TREE)
14070 return NULL_TREE;
14071
14072 /* Pass friends other than inline friend functions back. */
a1774733 14073 if (fndecl == void_type_node)
8d08fdba
MS
14074 return fndecl;
14075
14076 if (TREE_CODE (fndecl) != FUNCTION_DECL)
14077 /* Not a function, tell parser to report parse error. */
14078 return NULL_TREE;
14079
8d08fdba
MS
14080 if (DECL_IN_AGGR_P (fndecl))
14081 {
14082 if (IDENTIFIER_ERROR_LOCUS (DECL_ASSEMBLER_NAME (fndecl)) != current_class_type)
14083 {
68642fb6 14084 if (DECL_CONTEXT (fndecl)
2c73f9f5 14085 && TREE_CODE( DECL_CONTEXT (fndecl)) != NAMESPACE_DECL)
2ae7bada
NS
14086 cp_error ("`%D' is already defined in class `%T'", fndecl,
14087 DECL_CONTEXT (fndecl));
8d08fdba
MS
14088 }
14089 return void_type_node;
14090 }
14091
f3400fe2
JM
14092 check_template_shadow (fndecl);
14093
faae18ab
MS
14094 DECL_THIS_INLINE (fndecl) = 1;
14095
8926095f 14096 if (flag_default_inline)
8d08fdba
MS
14097 DECL_INLINE (fndecl) = 1;
14098
36a117a5
MM
14099 /* We process method specializations in finish_struct_1. */
14100 if (processing_template_decl && !DECL_TEMPLATE_SPECIALIZATION (fndecl))
3ac3d9ea 14101 fndecl = push_template_decl (fndecl);
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 }
14110
14111 if (DECL_CONSTRUCTOR_P (fndecl))
a0a33927
MS
14112 {
14113 if (! grok_ctor_properties (current_class_type, fndecl))
14114 return void_type_node;
14115 }
8d08fdba
MS
14116 else if (IDENTIFIER_OPNAME_P (DECL_NAME (fndecl)))
14117 grok_op_properties (fndecl, DECL_VIRTUAL_P (fndecl), 0);
14118 }
14119
cd9f6678 14120 cp_finish_decl (fndecl, NULL_TREE, NULL_TREE, 0);
8d08fdba
MS
14121
14122 /* Make a place for the parms */
14123 pushlevel (0);
14124 current_binding_level->parm_flag = 1;
68642fb6 14125
8d08fdba
MS
14126 DECL_IN_AGGR_P (fndecl) = 1;
14127 return fndecl;
14128}
14129
14130/* Go through the motions of finishing a function definition.
14131 We don't compile this method until after the whole class has
14132 been processed.
14133
14134 FINISH_METHOD must return something that looks as though it
14135 came from GROKFIELD (since we are defining a method, after all).
14136
14137 This is called after parsing the body of the function definition.
14138 STMTS is the chain of statements that makes up the function body.
14139
14140 DECL is the ..._DECL that `start_method' provided. */
14141
14142tree
14143finish_method (decl)
14144 tree decl;
14145{
14146 register tree fndecl = decl;
14147 tree old_initial;
8d08fdba
MS
14148
14149 register tree link;
14150
a1774733 14151 if (decl == void_type_node)
8d08fdba
MS
14152 return decl;
14153
14154 old_initial = DECL_INITIAL (fndecl);
14155
14156 /* Undo the level for the parms (from start_method).
14157 This is like poplevel, but it causes nothing to be
14158 saved. Saving information here confuses symbol-table
14159 output routines. Besides, this information will
14160 be correctly output when this method is actually
14161 compiled. */
14162
14163 /* Clear out the meanings of the local variables of this level;
14164 also record in each decl which block it belongs to. */
14165
14166 for (link = current_binding_level->names; link; link = TREE_CHAIN (link))
14167 {
14168 if (DECL_NAME (link) != NULL_TREE)
d8f8dca1 14169 pop_binding (DECL_NAME (link), link);
8d08fdba
MS
14170 my_friendly_assert (TREE_CODE (link) != FUNCTION_DECL, 163);
14171 DECL_CONTEXT (link) = NULL_TREE;
14172 }
14173
8d08fdba
MS
14174 GNU_xref_end_scope ((HOST_WIDE_INT) current_binding_level,
14175 (HOST_WIDE_INT) current_binding_level->level_chain,
14176 current_binding_level->parm_flag,
5566b478 14177 current_binding_level->keep);
8d08fdba
MS
14178
14179 poplevel (0, 0, 0);
14180
14181 DECL_INITIAL (fndecl) = old_initial;
14182
14183 /* We used to check if the context of FNDECL was different from
14184 current_class_type as another way to get inside here. This didn't work
14185 for String.cc in libg++. */
14186 if (DECL_FRIEND_P (fndecl))
14187 {
14188 CLASSTYPE_INLINE_FRIENDS (current_class_type)
14189 = tree_cons (NULL_TREE, fndecl, CLASSTYPE_INLINE_FRIENDS (current_class_type));
14190 decl = void_type_node;
14191 }
14192
14193 return decl;
14194}
14195\f
14196/* Called when a new struct TYPE is defined.
14197 If this structure or union completes the type of any previous
14198 variable declaration, lay it out and output its rtl. */
14199
14200void
14201hack_incomplete_structures (type)
14202 tree type;
14203{
f30432d7 14204 tree *list;
70adf8a9 14205 struct binding_level *level;
8d08fdba
MS
14206
14207 if (!type) /* Don't do this for class templates. */
14208 return;
14209
70adf8a9
JM
14210 if (namespace_bindings_p ())
14211 {
14212 level = 0;
14213 list = &namespace_scope_incomplete;
14214 }
14215 else
14216 {
14217 level = innermost_nonclass_level ();
14218 list = &level->incomplete;
14219 }
14220
14221 while (1)
14222 {
14223 while (*list)
14224 {
14225 tree decl = TREE_VALUE (*list);
14226 if ((decl && TREE_TYPE (decl) == type)
14227 || (TREE_TYPE (decl)
14228 && TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
14229 && TREE_TYPE (TREE_TYPE (decl)) == type))
f30432d7 14230 {
70adf8a9
JM
14231 int toplevel = toplevel_bindings_p ();
14232 if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
14233 && TREE_TYPE (TREE_TYPE (decl)) == type)
14234 layout_type (TREE_TYPE (decl));
14235 layout_decl (decl, 0);
14236 rest_of_decl_compilation (decl, NULL_PTR, toplevel, 0);
14237 if (! toplevel)
14238 {
14239 tree cleanup;
14240 expand_decl (decl);
14241 cleanup = maybe_build_cleanup (decl);
14242 expand_decl_init (decl);
14243 if (! expand_decl_cleanup (decl, cleanup))
14244 cp_error ("parser lost in parsing declaration of `%D'",
14245 decl);
14246 }
14247 *list = TREE_CHAIN (*list);
f30432d7 14248 }
70adf8a9
JM
14249 else
14250 list = &TREE_CHAIN (*list);
14251 }
14252
14253 /* Keep looking through artificial binding levels generated
14254 for local variables. */
14255 if (level && level->keep == 2)
14256 {
14257 level = level->level_chain;
14258 list = &level->incomplete;
f30432d7
MS
14259 }
14260 else
70adf8a9 14261 break;
f30432d7 14262 }
8d08fdba
MS
14263}
14264
86f45d2c
MM
14265/* If DECL is of a type which needs a cleanup, build that cleanup
14266 here. */
e92cc029 14267
86f45d2c
MM
14268tree
14269maybe_build_cleanup (decl)
14270 tree decl;
8d08fdba
MS
14271{
14272 tree type = TREE_TYPE (decl);
86f45d2c 14273
834c6dff 14274 if (type != error_mark_node && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
8d08fdba 14275 {
80048418 14276 int flags = LOOKUP_NORMAL|LOOKUP_DESTRUCTOR;
8d08fdba 14277 tree rval;
8d08fdba 14278
8d08fdba
MS
14279 if (TREE_CODE (type) == ARRAY_TYPE)
14280 rval = decl;
14281 else
14282 {
14283 mark_addressable (decl);
14284 rval = build_unary_op (ADDR_EXPR, decl, 0);
14285 }
14286
14287 /* Optimize for space over speed here. */
14288 if (! TYPE_USES_VIRTUAL_BASECLASSES (type)
14289 || flag_expensive_optimizations)
14290 flags |= LOOKUP_NONVIRTUAL;
14291
86f45d2c
MM
14292 rval = build_delete (TREE_TYPE (rval), rval,
14293 sfk_complete_destructor, flags, 0);
8d08fdba
MS
14294
14295 if (TYPE_USES_VIRTUAL_BASECLASSES (type)
14296 && ! TYPE_HAS_DESTRUCTOR (type))
e1b3e07d 14297 rval = build_compound_expr (tree_cons (NULL_TREE, rval,
051e6fd7 14298 build_tree_list (NULL_TREE, build_vbase_delete (type, decl))));
8d08fdba 14299
8d08fdba
MS
14300 return rval;
14301 }
14302 return 0;
14303}
14304\f
558475f0 14305/* When a stmt has been parsed, this function is called. */
8d08fdba
MS
14306
14307void
14308finish_stmt ()
14309{
558475f0
MM
14310 /* Always assume this statement was not an expression statement. If
14311 it actually was an expression statement, its our callers
14312 responsibility to fix this up. */
14313 last_expr_type = NULL_TREE;
8d08fdba
MS
14314}
14315
3afb32a4
MM
14316/* DECL was originally constructed as a non-static member function,
14317 but turned out to be static. Update it accordingly. */
700f8a87 14318
8857f91e 14319void
3afb32a4
MM
14320revert_static_member_fn (decl)
14321 tree decl;
8d08fdba 14322{
700f8a87 14323 tree tmp;
3afb32a4
MM
14324 tree function = TREE_TYPE (decl);
14325 tree args = TYPE_ARG_TYPES (function);
8d08fdba 14326
68642fb6 14327 if (CP_TYPE_QUALS (TREE_TYPE (TREE_VALUE (args)))
91063b51 14328 != TYPE_UNQUALIFIED)
68642fb6 14329 cp_error ("static member function `%#D' declared with type qualifiers",
11306230 14330 decl);
f30432d7 14331
700f8a87
MS
14332 args = TREE_CHAIN (args);
14333 tmp = build_function_type (TREE_TYPE (function), args);
91063b51 14334 tmp = build_qualified_type (tmp, CP_TYPE_QUALS (function));
f30432d7 14335 tmp = build_exception_variant (tmp,
8d08fdba 14336 TYPE_RAISES_EXCEPTIONS (function));
3afb32a4
MM
14337 TREE_TYPE (decl) = tmp;
14338 if (DECL_ARGUMENTS (decl))
14339 DECL_ARGUMENTS (decl) = TREE_CHAIN (DECL_ARGUMENTS (decl));
14340 DECL_STATIC_FUNCTION_P (decl) = 1;
8d08fdba 14341}
a4443a08 14342
68642fb6
UD
14343/* Initialize the variables used during compilation of a C++
14344 function. */
db5ae43f 14345
99dccabc
MM
14346static void
14347push_cp_function_context (f)
14348 struct function *f;
14349{
ae499cce
MM
14350 struct cp_language_function *p
14351 = ((struct cp_language_function *)
14352 xcalloc (1, sizeof (struct cp_language_function)));
14353 f->language = (struct language_function *) p;
db5ae43f 14354
b35d4555
MM
14355 /* It takes an explicit call to expand_body to generate RTL for a
14356 function. */
14357 expanding_p = 0;
f1dedc31
MM
14358
14359 /* Whenever we start a new function, we destroy temporaries in the
14360 usual way. */
ae499cce 14361 current_stmt_tree ()->stmts_are_full_exprs_p = 1;
db5ae43f
MS
14362}
14363
a8f73d4b
MM
14364/* Free the language-specific parts of F, now that we've finished
14365 compiling the function. */
db5ae43f 14366
99dccabc
MM
14367static void
14368pop_cp_function_context (f)
14369 struct function *f;
db5ae43f 14370{
d658cd4c 14371 if (f->language)
8e4ce833
JJ
14372 {
14373 struct cp_language_function *cp =
14374 (struct cp_language_function *) f->language;
14375 if (cp->x_local_names)
14376 VARRAY_FREE (cp->x_local_names);
14377 free (f->language);
14378 }
99dccabc 14379 f->language = 0;
db5ae43f 14380}
ebfc180f 14381
59026e79 14382/* Mark P for GC. */
4519c0a8 14383
59026e79
MM
14384static void
14385mark_lang_function (p)
ae499cce 14386 struct cp_language_function *p;
59026e79 14387{
87e3dbc9
MM
14388 if (!p)
14389 return;
14390
8f17b5c5
MM
14391 mark_c_language_function (&p->base);
14392
4519c0a8
MM
14393 ggc_mark_tree (p->x_ctor_label);
14394 ggc_mark_tree (p->x_dtor_label);
4519c0a8
MM
14395 ggc_mark_tree (p->x_current_class_ptr);
14396 ggc_mark_tree (p->x_current_class_ref);
52a11cbf 14397 ggc_mark_tree (p->x_eh_spec_block);
8e4ce833 14398 ggc_mark_tree_varray (p->x_local_names);
4519c0a8 14399
6625cdb5 14400 mark_named_label_lists (&p->x_named_labels, &p->x_named_label_uses);
a8f73d4b 14401 mark_binding_level (&p->bindings);
4519c0a8
MM
14402}
14403
59026e79
MM
14404/* Mark the language-specific data in F for GC. */
14405
a09ba2e0 14406static void
59026e79
MM
14407mark_cp_function_context (f)
14408 struct function *f;
14409{
ae499cce 14410 mark_lang_function ((struct cp_language_function *) f->language);
59026e79 14411}
4519c0a8 14412
4519c0a8
MM
14413void
14414lang_mark_tree (t)
14415 tree t;
14416{
14417 enum tree_code code = TREE_CODE (t);
14418 if (code == IDENTIFIER_NODE)
14419 {
14420 struct lang_identifier *li = (struct lang_identifier *) t;
14421 struct lang_id2 *li2 = li->x;
14422 ggc_mark_tree (li->namespace_bindings);
a8f73d4b 14423 ggc_mark_tree (li->bindings);
4519c0a8
MM
14424 ggc_mark_tree (li->class_value);
14425 ggc_mark_tree (li->class_template_info);
14426
14427 if (li2)
14428 {
14429 ggc_mark_tree (li2->label_value);
14430 ggc_mark_tree (li2->implicit_decl);
14431 ggc_mark_tree (li2->error_locus);
14432 }
14433 }
87e3dbc9
MM
14434 else if (code == CPLUS_BINDING)
14435 {
14436 if (BINDING_HAS_LEVEL_P (t))
14437 mark_binding_level (&BINDING_LEVEL (t));
14438 else
14439 ggc_mark_tree (BINDING_SCOPE (t));
14440 ggc_mark_tree (BINDING_VALUE (t));
14441 }
14442 else if (code == OVERLOAD)
14443 ggc_mark_tree (OVL_FUNCTION (t));
14444 else if (code == TEMPLATE_PARM_INDEX)
14445 ggc_mark_tree (TEMPLATE_PARM_DECL (t));
4519c0a8
MM
14446 else if (TREE_CODE_CLASS (code) == 'd')
14447 {
14448 struct lang_decl *ld = DECL_LANG_SPECIFIC (t);
14449
14450 if (ld)
14451 {
d60f72ae 14452 ggc_mark (ld);
8f17b5c5 14453 c_mark_lang_decl (&ld->decl_flags.base);
5362b086 14454 if (!DECL_GLOBAL_CTOR_P (t)
6462c441 14455 && !DECL_GLOBAL_DTOR_P (t)
8e4ce833
JJ
14456 && !DECL_THUNK_P (t)
14457 && !DECL_DISCRIMINATOR_P (t))
af3b4e59 14458 ggc_mark_tree (ld->decl_flags.u2.access);
31f8e4f3
MM
14459 else if (DECL_THUNK_P (t))
14460 ggc_mark_tree (ld->decl_flags.u2.vcall_offset);
4519c0a8
MM
14461 if (TREE_CODE (t) != NAMESPACE_DECL)
14462 ggc_mark_tree (ld->decl_flags.u.template_info);
16bb3387
JM
14463 else
14464 mark_binding_level (&NAMESPACE_LEVEL (t));
4519c0a8
MM
14465 if (CAN_HAVE_FULL_LANG_DECL_P (t))
14466 {
4519c0a8 14467 ggc_mark_tree (ld->befriending_classes);
8f17b5c5 14468 ggc_mark_tree (ld->context);
db9b2174 14469 ggc_mark_tree (ld->cloned_function);
8e4ce833 14470 ggc_mark_tree (ld->inlined_fns);
4519c0a8
MM
14471 if (TREE_CODE (t) == TYPE_DECL)
14472 ggc_mark_tree (ld->u.sorted_fields);
59026e79
MM
14473 else if (TREE_CODE (t) == FUNCTION_DECL
14474 && !DECL_PENDING_INLINE_P (t))
14475 mark_lang_function (DECL_SAVED_FUNCTION_DATA (t));
4519c0a8
MM
14476 }
14477 }
14478 }
14479 else if (TREE_CODE_CLASS (code) == 't')
14480 {
14481 struct lang_type *lt = TYPE_LANG_SPECIFIC (t);
14482
68642fb6 14483 if (lt && !(TREE_CODE (t) == POINTER_TYPE
87e3dbc9 14484 && TREE_CODE (TREE_TYPE (t)) == METHOD_TYPE))
4519c0a8 14485 {
c27db0df 14486 ggc_mark (lt);
911a71a7 14487 ggc_mark_tree (lt->primary_base);
4519c0a8
MM
14488 ggc_mark_tree (lt->vfields);
14489 ggc_mark_tree (lt->vbases);
14490 ggc_mark_tree (lt->tags);
4519c0a8 14491 ggc_mark_tree (lt->size);
fee7654e 14492 ggc_mark_tree (lt->pure_virtuals);
4519c0a8
MM
14493 ggc_mark_tree (lt->friend_classes);
14494 ggc_mark_tree (lt->rtti);
14495 ggc_mark_tree (lt->methods);
14496 ggc_mark_tree (lt->template_info);
87e3dbc9 14497 ggc_mark_tree (lt->befriending_classes);
4519c0a8 14498 }
87e3dbc9
MM
14499 else if (lt)
14500 /* In the case of pointer-to-member function types, the
14501 TYPE_LANG_SPECIFIC is really just a tree. */
14502 ggc_mark_tree ((tree) lt);
4519c0a8
MM
14503 }
14504}
5fd8e536
JM
14505
14506/* Return the IDENTIFIER_GLOBAL_VALUE of T, for use in common code, since
14507 the definition of IDENTIFIER_GLOBAL_VALUE is different for C and C++. */
14508
14509tree
14510identifier_global_value (t)
14511 tree t;
14512{
14513 return IDENTIFIER_GLOBAL_VALUE (t);
14514}
d363e7bf 14515
eaa7c03f
JM
14516/* Build the void_list_node (void_type_node having been created). */
14517tree
14518build_void_list_node ()
14519{
14520 tree t = build_tree_list (NULL_TREE, void_type_node);
14521 TREE_PARMLIST (t) = 1;
14522 return t;
14523}
14524
d363e7bf
AJ
14525static int
14526cp_missing_noreturn_ok_p (decl)
14527 tree decl;
14528{
14529 /* A missing noreturn is ok for the `main' function. */
92643fea 14530 return DECL_MAIN_P (decl);
d363e7bf 14531}