]> gcc.gnu.org Git - gcc.git/blame - gcc/cp/decl.c
Daily bump.
[gcc.git] / gcc / cp / decl.c
CommitLineData
e5e809f4 1/* Process declarations and variables for C compiler.
c12f5242 2 Copyright (C) 1988, 92-98, 1999 Free Software Foundation, Inc.
e5e809f4 3 Contributed by Michael Tiemann (tiemann@cygnus.com)
8d08fdba
MS
4
5This file is part of GNU CC.
6
7GNU CC is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation; either version 2, or (at your option)
10any later version.
11
12GNU CC is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with GNU CC; see the file COPYING. If not, write to
e9fa0c7c
RK
19the Free Software Foundation, 59 Temple Place - Suite 330,
20Boston, MA 02111-1307, USA. */
8d08fdba
MS
21
22
23/* Process declarations and symbol lookup for C front end.
24 Also constructs types; the standard scalar types at initialization,
25 and structure, union, array and enum types when they are declared. */
26
27/* ??? not all decl nodes are given the most useful possible
28 line numbers. For example, the CONST_DECLs for enum values. */
29
8d08fdba 30#include "config.h"
8d052bc7 31#include "system.h"
8d08fdba
MS
32#include "tree.h"
33#include "rtl.h"
34#include "flags.h"
35#include "cp-tree.h"
36#include "decl.h"
37#include "lex.h"
8d08fdba
MS
38#include <signal.h>
39#include "obstack.h"
72b7eeff 40#include "defaults.h"
49c249e1
JM
41#include "output.h"
42#include "except.h"
54f92bfb 43#include "toplev.h"
7ddedda4 44#include "../hash.h"
091f9839 45#include "defaults.h"
4519c0a8 46#include "ggc.h"
8d08fdba
MS
47
48#define obstack_chunk_alloc xmalloc
49#define obstack_chunk_free free
50
51extern struct obstack permanent_obstack;
ae0a6181 52extern struct obstack* saveable_obstack;
8d08fdba
MS
53
54extern int current_class_depth;
55
e1cd6e56
MS
56extern tree static_ctors, static_dtors;
57
30394414
JM
58extern tree global_namespace;
59
e5dc5fb2 60extern int (*valid_lang_attribute) PROTO ((tree, tree, tree, tree));
62c154ed 61
47e19316 62/* Use garbage collection. */
fc6af6e3 63
47e19316 64int ggc_p = 1;
fc6af6e3 65
8d08fdba
MS
66/* Obstack used for remembering local class declarations (like
67 enums and static (const) members. */
68#include "stack.h"
5566b478 69struct obstack decl_obstack;
8d08fdba
MS
70static struct stack_level *decl_stack;
71
8d08fdba
MS
72#ifndef WCHAR_UNSIGNED
73#define WCHAR_UNSIGNED 0
74#endif
75
81b3411c
BS
76#ifndef CHAR_TYPE_SIZE
77#define CHAR_TYPE_SIZE BITS_PER_UNIT
8d08fdba
MS
78#endif
79
255512c1 80#ifndef BOOL_TYPE_SIZE
e1cd6e56 81#ifdef SLOW_BYTE_ACCESS
d2e5ee5c 82#define BOOL_TYPE_SIZE ((SLOW_BYTE_ACCESS) ? (POINTER_SIZE) : (CHAR_TYPE_SIZE))
e1cd6e56 83#else
d2e5ee5c 84#define BOOL_TYPE_SIZE CHAR_TYPE_SIZE
e1cd6e56 85#endif
255512c1
JM
86#endif
87
8d08fdba
MS
88/* We let tm.h override the types used here, to handle trivial differences
89 such as the choice of unsigned int or long unsigned int for size_t.
90 When machines start needing nontrivial differences in the size type,
91 it would be best to do something here to figure out automatically
92 from other information what type to use. */
93
94#ifndef SIZE_TYPE
95#define SIZE_TYPE "long unsigned int"
96#endif
97
98#ifndef PTRDIFF_TYPE
99#define PTRDIFF_TYPE "long int"
100#endif
101
102#ifndef WCHAR_TYPE
103#define WCHAR_TYPE "int"
104#endif
105
8d08fdba 106static tree grokparms PROTO((tree, int));
d8e178a0 107static const char *redeclaration_error_message PROTO((tree, tree));
8d08fdba 108
49c249e1
JM
109static struct stack_level *push_decl_level PROTO((struct stack_level *,
110 struct obstack *));
111static void push_binding_level PROTO((struct binding_level *, int,
112 int));
113static void pop_binding_level PROTO((void));
114static void suspend_binding_level PROTO((void));
115static void resume_binding_level PROTO((struct binding_level *));
116static struct binding_level *make_binding_level PROTO((void));
49c249e1 117static void declare_namespace_level PROTO((void));
1ddb2906 118static void signal_catch PROTO((int)) ATTRIBUTE_NORETURN;
49c249e1 119static void storedecls PROTO((tree));
49c249e1
JM
120static void require_complete_types_for_parms PROTO((tree));
121static void push_overloaded_decl_1 PROTO((tree));
122static int ambi_op_p PROTO((tree));
123static int unary_op_p PROTO((tree));
124static tree store_bindings PROTO((tree, tree));
125static tree lookup_tag_reverse PROTO((tree, tree));
126static tree obscure_complex_init PROTO((tree, tree));
127static tree maybe_build_cleanup_1 PROTO((tree, tree));
3e3f722c 128static tree lookup_name_real PROTO((tree, int, int, int));
49c249e1 129static void warn_extern_redeclared_static PROTO((tree, tree));
a703fb38 130static void grok_reference_init PROTO((tree, tree, tree));
386b8a85 131static tree grokfndecl PROTO((tree, tree, tree, tree, int,
2c73f9f5 132 enum overload_flags, tree,
7a8f9fa9 133 tree, int, int, int, int, int, int, tree));
2c73f9f5 134static tree grokvardecl PROTO((tree, tree, RID_BIT_TYPE *, int, int, tree));
49c249e1
JM
135static tree lookup_tag PROTO((enum tree_code, tree,
136 struct binding_level *, int));
137static void set_identifier_type_value_with_scope
138 PROTO((tree, tree, struct binding_level *));
d8e178a0
KG
139static void record_builtin_type PROTO((enum rid, const char *, tree));
140static void record_unknown_type PROTO((tree, const char *));
141static int member_function_or_else PROTO((tree, tree, const char *));
142static void bad_specifiers PROTO((tree, const char *, int, int, int, int,
49c249e1 143 int));
8df4696d 144static void lang_print_error_function PROTO((const char *));
39c01e4c 145static tree maybe_process_template_type_declaration PROTO((tree, int, struct binding_level*));
91063b51 146static void check_for_uninitialized_const_var PROTO((tree));
7ddedda4
MM
147static unsigned long typename_hash PROTO((hash_table_key));
148static boolean typename_compare PROTO((hash_table_key, hash_table_key));
f181d4ae 149static void push_binding PROTO((tree, tree, struct binding_level*));
8f032717 150static int add_binding PROTO((tree, tree));
d8f8dca1 151static void pop_binding PROTO((tree, tree));
297e73d8 152static tree local_variable_p PROTO((tree));
d8e178a0
KG
153static tree find_binding PROTO((tree, tree));
154static tree select_decl PROTO((tree, int));
d8e178a0
KG
155static int lookup_flags PROTO((int, int));
156static tree qualify_lookup PROTO((tree, int));
157static tree record_builtin_java_type PROTO((const char *, int));
158static const char *tag_name PROTO((enum tag_types code));
8f032717
MM
159static void find_class_binding_level PROTO((void));
160static struct binding_level *innermost_nonclass_level PROTO((void));
235f734d 161static void warn_about_implicit_typename_lookup PROTO((tree, tree));
0aafb128
MM
162static int walk_namespaces_r PROTO((tree, walk_namespaces_fn, void *));
163static int walk_globals_r PROTO((tree, void *));
cfe507be 164static void add_decl_to_level PROTO((tree, struct binding_level *));
acef433b
MM
165static tree make_label_decl PROTO((tree, int));
166static void pop_label PROTO((tree));
167static void pop_labels PROTO((tree));
27778b73 168static void maybe_deduce_size_from_array_init PROTO((tree, tree));
57b52417 169static void layout_var_decl PROTO((tree));
27778b73 170static void maybe_commonize_var PROTO((tree));
c82dbd95 171static tree check_initializer PROTO((tree, tree));
27778b73 172static void make_rtl_for_nonlocal_decl PROTO((tree, tree, const char *));
99dccabc
MM
173static void push_cp_function_context PROTO((struct function *));
174static void pop_cp_function_context PROTO((struct function *));
4519c0a8
MM
175static void mark_binding_level PROTO((void *));
176static void mark_cp_function_context PROTO((struct function *));
47e19316 177static void mark_saved_scope PROTO((void *));
59026e79
MM
178static void mark_lang_function PROTO((struct language_function *));
179static void save_function_data PROTO((tree));
a8f73d4b 180static void check_function_type PROTO((tree));
f0105ed3 181static void destroy_local_static PROTO((tree));
24bef158 182static void destroy_local_var PROTO((tree));
9bfadf57 183static void finish_destructor_body PROTO((void));
8d08fdba 184
69ac77ce
JL
185#if defined (DEBUG_CP_BINDING_LEVELS)
186static void indent PROTO((void));
187#endif
188
8d08fdba
MS
189/* Erroneous argument lists can use this *IFF* they do not modify it. */
190tree error_mark_list;
191
7f4edbcb
BS
192/* The following symbols are subsumed in the cp_global_trees array, and
193 listed here individually for documentation purposes.
8d08fdba 194
7f4edbcb
BS
195 C++ extensions
196 tree wchar_decl_node;
197 tree void_zero_node;
37c46b43 198
7f4edbcb
BS
199 tree vtable_entry_type;
200 tree delta_type_node;
201#if 0
202 Old rtti stuff.
203 tree __baselist_desc_type_node;
204 tree __i_desc_type_node, __m_desc_type_node;
205 tree __t_desc_array_type, __i_desc_array_type, __m_desc_array_type;
946dc1c8 206#endif
7f4edbcb
BS
207 tree __t_desc_type_node;
208#if 0
209 tree __tp_desc_type_node;
210#endif
211 tree __access_mode_type_node;
212 tree __bltn_desc_type_node, __user_desc_type_node, __class_desc_type_node;
213 tree __ptr_desc_type_node, __attr_desc_type_node, __func_desc_type_node;
214 tree __ptmf_desc_type_node, __ptmd_desc_type_node;
215#if 0
216 Not needed yet? May be needed one day?
217 tree __bltn_desc_array_type, __user_desc_array_type, __class_desc_array_type;
218 tree __ptr_desc_array_type, __attr_dec_array_type, __func_desc_array_type;
219 tree __ptmf_desc_array_type, __ptmd_desc_array_type;
946dc1c8 220#endif
8d08fdba 221
7f4edbcb
BS
222 tree class_star_type_node;
223 tree class_type_node, record_type_node, union_type_node, enum_type_node;
224 tree unknown_type_node;
8d08fdba 225
7f4edbcb 226 Array type `vtable_entry_type[]'
8d08fdba 227
7f4edbcb
BS
228 tree vtbl_type_node;
229 tree vtbl_ptr_type_node;
8d08fdba 230
7f4edbcb 231 Nnamespace std
8d08fdba 232
7f4edbcb 233 tree std_node;
8d08fdba 234
7f4edbcb
BS
235 A FUNCTION_DECL which can call `abort'. Not necessarily the
236 one that the user will declare, but sufficient to be called
237 by routines that want to abort the program.
8d08fdba 238
7f4edbcb 239 tree abort_fndecl;
8d08fdba 240
7f4edbcb 241 The FUNCTION_DECL for the default `::operator delete'.
2986ae00 242
7f4edbcb 243 tree global_delete_fndecl;
8d08fdba 244
7f4edbcb
BS
245 Used by RTTI
246 tree type_info_type_node, tinfo_fn_id, tinfo_fn_type;
8d08fdba 247
7f4edbcb 248*/
8d08fdba 249
7f4edbcb 250tree cp_global_trees[CPTI_MAX];
8d08fdba 251
2c73f9f5 252/* Indicates that there is a type value in some namespace, although
7f4edbcb 253 that is not necessarily in scope at the moment. */
2c73f9f5
ML
254
255static tree global_type_node;
256
7f4edbcb 257/* Namespace std. */
0dde4175 258int in_std;
6633d636 259
3e3f722c
ML
260/* Expect only namespace names now. */
261static int only_namespace_names;
262
72b7eeff
MS
263/* In a destructor, the last insn emitted after the start of the
264 function and the parms. */
265
4519c0a8 266#define last_dtor_insn cp_function_chain->x_last_dtor_insn
72b7eeff 267
b87692e5 268/* In a constructor, the last insn emitted after the start of the
9664b89e
JM
269 function and the parms, the exception specification and any
270 function-try-block. The constructor initializers are emitted after
271 this insn. */
b87692e5 272
4519c0a8 273#define last_parm_cleanup_insn cp_function_chain->x_last_parm_cleanup_insn
8d08fdba 274
8d08fdba
MS
275/* If original DECL_RESULT of current function was a register,
276 but due to being an addressable named return value, would up
277 on the stack, this variable holds the named return value's
278 original location. */
8d08fdba 279
4519c0a8 280#define original_result_rtx cp_function_chain->x_result_rtx
8d08fdba 281
e349ee73
MS
282struct named_label_list
283{
284 struct binding_level *binding_level;
285 tree names_in_scope;
286 tree label_decl;
9c0758dd 287 const char *filename_o_goto;
e349ee73
MS
288 int lineno_o_goto;
289 struct named_label_list *next;
290};
291
ed5511d9
MM
292/* Used only for jumps to as-yet undefined labels, since jumps to
293 defined labels can have their validity checked by stmt.c. */
8d08fdba 294
ed5511d9 295#define named_label_uses cp_function_chain->x_named_label_uses
8d08fdba
MS
296
297/* A list of objects which have constructors or destructors
298 which reside in the global scope. The decl is stored in
299 the TREE_VALUE slot and the initializer is stored
300 in the TREE_PURPOSE slot. */
301tree static_aggregates;
302
8d08fdba
MS
303/* -- end of C++ */
304
81b3411c 305/* A node for the integer constants 2, and 3. */
d11ad92e 306
81b3411c 307tree integer_two_node, integer_three_node;
8d08fdba 308
8d08fdba
MS
309/* While defining an enum type, this is 1 plus the last enumerator
310 constant value. */
311
312static tree enum_next_value;
313
7177d104
MS
314/* Nonzero means that there was overflow computing enum_next_value. */
315
316static int enum_overflow;
317
8d08fdba
MS
318/* Parsing a function declarator leaves here a chain of structure
319 and enum types declared in the parmlist. */
320
321static tree last_function_parm_tags;
322
8d08fdba 323/* Similar, for last_function_parm_tags. */
9cd64686 324tree last_function_parms;
8d08fdba
MS
325static tree current_function_parm_tags;
326
327/* A list (chain of TREE_LIST nodes) of all LABEL_DECLs in the function
328 that have names. Here so we can clear out their names' definitions
acef433b
MM
329 at the end of the function. The TREE_VALUE is a LABEL_DECL; the
330 TREE_PURPOSE is the previous binding of the label. */
8d08fdba 331
4519c0a8 332#define named_labels cp_function_chain->x_named_labels
8d08fdba 333
8d08fdba
MS
334/* The FUNCTION_DECL for the function currently being compiled,
335 or 0 if between functions. */
336tree current_function_decl;
337
8d08fdba
MS
338/* Set to 0 at beginning of a function definition, and whenever
339 a label (case or named) is defined. Set to value of expression
340 returned from function when that value can be transformed into
341 a named return value. */
342
343tree current_function_return_value;
344
8d08fdba
MS
345/* Nonzero means give `double' the same size as `float'. */
346
347extern int flag_short_double;
348
349/* Nonzero means don't recognize any builtin functions. */
350
351extern int flag_no_builtin;
352
00595019
MS
353/* Nonzero means don't recognize the non-ANSI builtin functions.
354 -ansi sets this. */
355
356extern int flag_no_nonansi_builtin;
357
e1cd6e56
MS
358/* Nonzero means enable obscure ANSI features and disable GNU extensions
359 that might cause ANSI-compliant code to be miscompiled. */
8d08fdba
MS
360
361extern int flag_ansi;
362
363/* Nonzero if we want to support huge (> 2^(sizeof(short)*8-1) bytes)
e92cc029 364 objects. */
8d08fdba
MS
365extern int flag_huge_objects;
366
367/* Nonzero if we want to conserve space in the .o files. We do this
368 by putting uninitialized data and runtime initialized data into
ddd5a7c1 369 .common instead of .data at the expense of not flagging multiple
8d08fdba
MS
370 definitions. */
371extern int flag_conserve_space;
8d08fdba 372\f
51c184be 373/* C and C++ flags are in decl2.c. */
8d08fdba 374
51c184be 375/* Flag used when debugging spew.c */
8d08fdba
MS
376
377extern int spew_debug;
378
e92cc029
MS
379/* A expression of value 0 with the same precision as a sizetype
380 node, but signed. */
381tree signed_size_zero_node;
382
0c8feefe
MM
383/* The name of the anonymous namespace, throughout this translation
384 unit. */
385tree anonymous_namespace_name;
386
8d08fdba
MS
387\f
388/* Allocate a level of searching. */
e92cc029 389
824b9a4c 390static
8d08fdba
MS
391struct stack_level *
392push_decl_level (stack, obstack)
393 struct stack_level *stack;
394 struct obstack *obstack;
395{
396 struct stack_level tem;
397 tem.prev = stack;
398
399 return push_stack_level (obstack, (char *)&tem, sizeof (tem));
400}
401\f
402/* For each binding contour we allocate a binding_level structure
e92cc029
MS
403 which records the names defined in that contour.
404 Contours include:
405 0) the global one
406 1) one for each function definition,
407 where internal declarations of the parameters appear.
408 2) one for each compound statement,
409 to record its declarations.
410
411 The current meaning of a name can be found by searching the levels
412 from the current one out to the global one.
413
414 Off to the side, may be the class_binding_level. This exists only
415 to catch class-local declarations. It is otherwise nonexistent.
416
417 Also there may be binding levels that catch cleanups that must be
d8f8dca1
MM
418 run when exceptions occur. Thus, to see whether a name is bound in
419 the current scope, it is not enough to look in the
420 CURRENT_BINDING_LEVEL. You should use lookup_name_current_level
421 instead. */
8d08fdba
MS
422
423/* Note that the information in the `names' component of the global contour
424 is duplicated in the IDENTIFIER_GLOBAL_VALUEs of all identifiers. */
425
426struct binding_level
427 {
428 /* A chain of _DECL nodes for all variables, constants, functions,
e92cc029 429 and typedef types. These are in the reverse of the order
f181d4ae
MM
430 supplied. There may be OVERLOADs on this list, too, but they
431 are wrapped in TREE_LISTs; the TREE_VALUE is the OVERLOAD. */
8d08fdba
MS
432 tree names;
433
e92cc029
MS
434 /* A list of structure, union and enum definitions, for looking up
435 tag names.
436 It is a chain of TREE_LIST nodes, each of whose TREE_PURPOSE is a name,
437 or NULL_TREE; and whose TREE_VALUE is a RECORD_TYPE, UNION_TYPE,
438 or ENUMERAL_TYPE node.
439
440 C++: the TREE_VALUE nodes can be simple types for
441 component_bindings. */
8d08fdba
MS
442 tree tags;
443
2c73f9f5
ML
444 /* A list of USING_DECL nodes. */
445 tree usings;
446
ea9635c7
ML
447 /* A list of used namespaces. PURPOSE is the namespace,
448 VALUE the common ancestor with this binding_level's namespace. */
449 tree using_directives;
450
f181d4ae
MM
451 /* If this binding level is the binding level for a class, then
452 class_shadowed is a TREE_LIST. The TREE_PURPOSE of each node
453 is the name of an entity bound in the class; the TREE_VALUE is
454 the IDENTIFIER_CLASS_VALUE before we entered the class. Thus,
455 when leaving class scope, we can restore the
d8f8dca1
MM
456 IDENTIFIER_CLASS_VALUE by walking this list. The TREE_TYPE is
457 the DECL bound by this name in the class. */
8d08fdba
MS
458 tree class_shadowed;
459
f181d4ae
MM
460 /* Similar to class_shadowed, but for IDENTIFIER_TYPE_VALUE, and
461 is used for all binding levels. */
8d08fdba
MS
462 tree type_shadowed;
463
acef433b
MM
464 /* A TREE_LIST. Each TREE_VALUE is the LABEL_DECL for a local
465 label in this scope. The TREE_PURPOSE is the previous value of
466 the IDENTIFIER_LABEL VALUE. */
467 tree shadowed_labels;
468
8d08fdba
MS
469 /* For each level (except not the global one),
470 a chain of BLOCK nodes for all the levels
471 that were entered and exited one level down. */
472 tree blocks;
473
474 /* The BLOCK node for this level, if one has been preallocated.
475 If 0, the BLOCK is allocated (if needed) when the level is popped. */
476 tree this_block;
477
478 /* The binding level which this one is contained in (inherits from). */
479 struct binding_level *level_chain;
480
f30432d7 481 /* List of decls in `names' that have incomplete
8d08fdba 482 structure or union types. */
f30432d7 483 tree incomplete;
8d08fdba 484
8d6e462b 485 /* List of VAR_DECLS saved from a previous for statement.
abc95ed3 486 These would be dead in ANSI-conforming code, but might
f181d4ae
MM
487 be referenced in ARM-era code. These are stored in a
488 TREE_LIST; the TREE_VALUE is the actual declaration. */
8d6e462b
PB
489 tree dead_vars_from_for;
490
8d08fdba
MS
491 /* 1 for the level that holds the parameters of a function.
492 2 for the level that holds a class declaration.
493 3 for levels that hold parameter declarations. */
494 unsigned parm_flag : 4;
495
496 /* 1 means make a BLOCK for this level regardless of all else.
497 2 for temporary binding contours created by the compiler. */
498 unsigned keep : 3;
499
500 /* Nonzero if this level "doesn't exist" for tags. */
501 unsigned tag_transparent : 1;
502
503 /* Nonzero if this level can safely have additional
504 cleanup-needing variables added to it. */
505 unsigned more_cleanups_ok : 1;
506 unsigned have_cleanups : 1;
507
5566b478
MS
508 /* Nonzero if this level is for storing the decls for template
509 parameters and generic decls; these decls will be discarded and
510 replaced with a TEMPLATE_DECL. */
8d08fdba
MS
511 unsigned pseudo_global : 1;
512
a9aedbc2
MS
513 /* This is set for a namespace binding level. */
514 unsigned namespace_p : 1;
515
2ee887f2 516 /* True if this level is that of a for-statement where we need to
d22c8596 517 worry about ambiguous (ARM or ANSI) scope rules. */
8d6e462b
PB
518 unsigned is_for_scope : 1;
519
227cf171
JM
520 /* True if this level corresponds to an EH region, as for a try block. */
521 unsigned eh_region : 1;
522
523 /* One bit left for this word. */
8d08fdba
MS
524
525#if defined(DEBUG_CP_BINDING_LEVELS)
526 /* Binding depth at which this level began. */
527 unsigned binding_depth;
528#endif /* defined(DEBUG_CP_BINDING_LEVELS) */
529 };
530
531#define NULL_BINDING_LEVEL ((struct binding_level *) NULL)
532
8f032717 533/* The binding level currently in effect. */
8d08fdba 534
a8f73d4b
MM
535#define current_binding_level \
536 (current_function \
537 ? cp_function_chain->bindings \
538 : scope_chain->bindings)
8d08fdba
MS
539
540/* The binding level of the current class, if any. */
541
9cd64686 542#define class_binding_level scope_chain->class_bindings
8d08fdba 543
8d08fdba
MS
544/* A chain of binding_level structures awaiting reuse. */
545
546static struct binding_level *free_binding_level;
547
548/* The outermost binding level, for names of file scope.
549 This is created when the compiler is started and exists
550 through the entire run. */
551
552static struct binding_level *global_binding_level;
553
8d08fdba
MS
554/* Nonzero means unconditionally make a BLOCK for the next level pushed. */
555
556static int keep_next_level_flag;
557
558#if defined(DEBUG_CP_BINDING_LEVELS)
559static int binding_depth = 0;
560static int is_class_level = 0;
561
562static void
563indent ()
564{
565 register unsigned i;
566
567 for (i = 0; i < binding_depth*2; i++)
568 putc (' ', stderr);
569}
570#endif /* defined(DEBUG_CP_BINDING_LEVELS) */
571
572static tree pushdecl_with_scope PROTO((tree, struct binding_level *));
573
574static void
575push_binding_level (newlevel, tag_transparent, keep)
576 struct binding_level *newlevel;
577 int tag_transparent, keep;
578{
579 /* Add this level to the front of the chain (stack) of levels that
580 are active. */
acef433b 581 bzero ((char*) newlevel, sizeof (struct binding_level));
8f032717 582 newlevel->level_chain = current_binding_level;
8d08fdba
MS
583 current_binding_level = newlevel;
584 newlevel->tag_transparent = tag_transparent;
585 newlevel->more_cleanups_ok = 1;
227cf171
JM
586
587 /* We are called before expand_start_bindings, but after
588 expand_eh_region_start for a try block; so we check this now,
589 before the EH block is covered up. */
590 newlevel->eh_region = is_eh_region ();
591
8d08fdba
MS
592 newlevel->keep = keep;
593#if defined(DEBUG_CP_BINDING_LEVELS)
594 newlevel->binding_depth = binding_depth;
595 indent ();
596 fprintf (stderr, "push %s level 0x%08x line %d\n",
597 (is_class_level) ? "class" : "block", newlevel, lineno);
598 is_class_level = 0;
599 binding_depth++;
600#endif /* defined(DEBUG_CP_BINDING_LEVELS) */
601}
602
8f032717
MM
603/* Find the innermost enclosing class scope, and reset
604 CLASS_BINDING_LEVEL appropriately. */
605
606static void
607find_class_binding_level ()
608{
609 struct binding_level *level = current_binding_level;
610
611 while (level && level->parm_flag != 2)
612 level = level->level_chain;
613 if (level && level->parm_flag == 2)
614 class_binding_level = level;
615 else
616 class_binding_level = 0;
617}
618
8d08fdba
MS
619static void
620pop_binding_level ()
621{
8d08fdba
MS
622 if (global_binding_level)
623 {
2c73f9f5 624 /* Cannot pop a level, if there are none left to pop. */
8d08fdba
MS
625 if (current_binding_level == global_binding_level)
626 my_friendly_abort (123);
627 }
628 /* Pop the current level, and free the structure for reuse. */
629#if defined(DEBUG_CP_BINDING_LEVELS)
630 binding_depth--;
631 indent ();
632 fprintf (stderr, "pop %s level 0x%08x line %d\n",
633 (is_class_level) ? "class" : "block",
634 current_binding_level, lineno);
635 if (is_class_level != (current_binding_level == class_binding_level))
9e9ff709
MS
636 {
637 indent ();
638 fprintf (stderr, "XXX is_class_level != (current_binding_level == class_binding_level)\n");
639 }
8d08fdba
MS
640 is_class_level = 0;
641#endif /* defined(DEBUG_CP_BINDING_LEVELS) */
642 {
643 register struct binding_level *level = current_binding_level;
644 current_binding_level = current_binding_level->level_chain;
645 level->level_chain = free_binding_level;
646#if 0 /* defined(DEBUG_CP_BINDING_LEVELS) */
647 if (level->binding_depth != binding_depth)
648 abort ();
649#endif /* defined(DEBUG_CP_BINDING_LEVELS) */
8f032717
MM
650 free_binding_level = level;
651 find_class_binding_level ();
8d08fdba
MS
652 }
653}
a9aedbc2
MS
654
655static void
656suspend_binding_level ()
657{
658 if (class_binding_level)
659 current_binding_level = class_binding_level;
660
661 if (global_binding_level)
662 {
2c73f9f5 663 /* Cannot suspend a level, if there are none left to suspend. */
a9aedbc2
MS
664 if (current_binding_level == global_binding_level)
665 my_friendly_abort (123);
666 }
667 /* Suspend the current level. */
668#if defined(DEBUG_CP_BINDING_LEVELS)
669 binding_depth--;
670 indent ();
671 fprintf (stderr, "suspend %s level 0x%08x line %d\n",
672 (is_class_level) ? "class" : "block",
673 current_binding_level, lineno);
674 if (is_class_level != (current_binding_level == class_binding_level))
9e9ff709
MS
675 {
676 indent ();
677 fprintf (stderr, "XXX is_class_level != (current_binding_level == class_binding_level)\n");
678 }
a9aedbc2
MS
679 is_class_level = 0;
680#endif /* defined(DEBUG_CP_BINDING_LEVELS) */
8f032717
MM
681 current_binding_level = current_binding_level->level_chain;
682 find_class_binding_level ();
a9aedbc2
MS
683}
684
824b9a4c 685static void
a9aedbc2
MS
686resume_binding_level (b)
687 struct binding_level *b;
688{
2c73f9f5
ML
689 /* Resuming binding levels is meant only for namespaces,
690 and those cannot nest into classes. */
691 my_friendly_assert(!class_binding_level, 386);
692 /* Also, resuming a non-directly nested namespace is a no-no. */
693 my_friendly_assert(b->level_chain == current_binding_level, 386);
a9aedbc2
MS
694 current_binding_level = b;
695#if defined(DEBUG_CP_BINDING_LEVELS)
696 b->binding_depth = binding_depth;
697 indent ();
698 fprintf (stderr, "resume %s level 0x%08x line %d\n",
699 (is_class_level) ? "class" : "block", b, lineno);
700 is_class_level = 0;
701 binding_depth++;
702#endif /* defined(DEBUG_CP_BINDING_LEVELS) */
703}
8d08fdba 704\f
cffa8729
MS
705/* Create a new `struct binding_level'. */
706
707static
708struct binding_level *
709make_binding_level ()
710{
711 /* NOSTRICT */
712 return (struct binding_level *) xmalloc (sizeof (struct binding_level));
713}
714
8d08fdba
MS
715/* Nonzero if we are currently in the global binding level. */
716
717int
718global_bindings_p ()
719{
720 return current_binding_level == global_binding_level;
721}
722
8f032717
MM
723/* Return the innermost binding level that is not for a class scope. */
724
725static struct binding_level *
726innermost_nonclass_level ()
727{
728 struct binding_level *b;
729
730 b = current_binding_level;
731 while (b->parm_flag == 2)
732 b = b->level_chain;
733
734 return b;
735}
736
a9aedbc2
MS
737/* Nonzero if we are currently in a toplevel binding level. This
738 means either the global binding level or a namespace in a toplevel
8f032717
MM
739 binding level. Since there are no non-toplevel namespace levels,
740 this really means any namespace or pseudo-global level. We also
741 include a class whose context is toplevel. */
a9aedbc2
MS
742
743int
744toplevel_bindings_p ()
745{
8f032717
MM
746 struct binding_level *b = innermost_nonclass_level ();
747
748 return b->namespace_p || b->pseudo_global;
a9aedbc2
MS
749}
750
8f032717
MM
751/* Nonzero if this is a namespace scope, or if we are defining a class
752 which is itself at namespace scope, or whose enclosing class is
753 such a class, etc. */
a9aedbc2 754
7bdbfa05 755int
a9aedbc2
MS
756namespace_bindings_p ()
757{
8f032717
MM
758 struct binding_level *b = innermost_nonclass_level ();
759
760 return b->namespace_p;
a9aedbc2
MS
761}
762
f1dedc31
MM
763/* If KEEP is non-zero, make a BLOCK node for the next binding level,
764 unconditionally. Otherwise, use the normal logic to decide whether
765 or not to create a BLOCK. */
766
8d08fdba 767void
f1dedc31
MM
768keep_next_level (keep)
769 int keep;
8d08fdba 770{
f1dedc31 771 keep_next_level_flag = keep;
8d08fdba
MS
772}
773
774/* Nonzero if the current level needs to have a BLOCK made. */
775
776int
777kept_level_p ()
778{
779 return (current_binding_level->blocks != NULL_TREE
780 || current_binding_level->keep
781 || current_binding_level->names != NULL_TREE
782 || (current_binding_level->tags != NULL_TREE
783 && !current_binding_level->tag_transparent));
784}
785
8d08fdba
MS
786void
787declare_pseudo_global_level ()
788{
789 current_binding_level->pseudo_global = 1;
790}
791
824b9a4c 792static void
a9aedbc2
MS
793declare_namespace_level ()
794{
795 current_binding_level->namespace_p = 1;
796}
797
8d08fdba
MS
798int
799pseudo_global_level_p ()
800{
9188c363 801 return current_binding_level->pseudo_global;
8d08fdba
MS
802}
803
804void
805set_class_shadows (shadows)
806 tree shadows;
807{
808 class_binding_level->class_shadowed = shadows;
809}
810
811/* Enter a new binding level.
812 If TAG_TRANSPARENT is nonzero, do so only for the name space of variables,
813 not for that of tags. */
814
815void
816pushlevel (tag_transparent)
817 int tag_transparent;
818{
b35d4555 819 struct binding_level *newlevel;
8d08fdba 820
b35d4555
MM
821 if (current_function && !doing_semantic_analysis_p ())
822 return;
8d08fdba 823
b35d4555 824 /* Reuse or create a struct for this binding level. */
8d08fdba
MS
825#if defined(DEBUG_CP_BINDING_LEVELS)
826 if (0)
827#else /* !defined(DEBUG_CP_BINDING_LEVELS) */
828 if (free_binding_level)
829#endif /* !defined(DEBUG_CP_BINDING_LEVELS) */
830 {
831 newlevel = free_binding_level;
832 free_binding_level = free_binding_level->level_chain;
833 }
834 else
acef433b 835 newlevel = make_binding_level ();
cffa8729 836
8d08fdba
MS
837 push_binding_level (newlevel, tag_transparent, keep_next_level_flag);
838 GNU_xref_start_scope ((HOST_WIDE_INT) newlevel);
839 keep_next_level_flag = 0;
840}
841
5566b478 842void
8d6e462b
PB
843note_level_for_for ()
844{
845 current_binding_level->is_for_scope = 1;
846}
847
8d08fdba
MS
848void
849pushlevel_temporary (tag_transparent)
850 int tag_transparent;
851{
852 pushlevel (tag_transparent);
853 current_binding_level->keep = 2;
854 clear_last_expr ();
855
856 /* Note we don't call push_momentary() here. Otherwise, it would cause
857 cleanups to be allocated on the momentary obstack, and they will be
858 overwritten by the next statement. */
859
860 expand_start_bindings (0);
861}
862
f181d4ae
MM
863/* For a binding between a name and an entity at a block scope,
864 this is the `struct binding_level' for the block. */
865#define BINDING_LEVEL(NODE) \
c7a932b1 866 (((struct tree_binding*)NODE)->scope.level)
f181d4ae 867
f181d4ae
MM
868/* Make DECL the innermost binding for ID. The LEVEL is the binding
869 level at which this declaration is being bound. */
870
871static void
872push_binding (id, decl, level)
873 tree id;
874 tree decl;
875 struct binding_level* level;
876{
877 tree binding;
878
87e3dbc9 879 binding = make_node (CPLUS_BINDING);
f181d4ae
MM
880
881 /* Now, fill in the binding information. */
882 BINDING_VALUE (binding) = decl;
d8f8dca1 883 BINDING_TYPE (binding) = NULL_TREE;
f181d4ae 884 BINDING_LEVEL (binding) = level;
8f032717 885 INHERITED_VALUE_BINDING_P (binding) = 0;
f181d4ae 886 LOCAL_BINDING_P (binding) = (level != class_binding_level);
87e3dbc9 887 BINDING_HAS_LEVEL_P (binding) = 1;
f181d4ae 888
c45df9c1 889 /* And put it on the front of the list of bindings for ID. */
f181d4ae
MM
890 TREE_CHAIN (binding) = IDENTIFIER_BINDING (id);
891 IDENTIFIER_BINDING (id) = binding;
892}
893
d8f8dca1
MM
894/* ID is already bound in the current scope. But, DECL is an
895 additional binding for ID in the same scope. This is the `struct
896 stat' hack whereby a non-typedef class-name or enum-name can be
897 bound at the same level as some other kind of entity. It's the
898 responsibility of the caller to check that inserting this name is
8f032717
MM
899 legal here. Returns nonzero if the new binding was successful. */
900static int
d8f8dca1
MM
901add_binding (id, decl)
902 tree id;
903 tree decl;
904{
905 tree binding = IDENTIFIER_BINDING (id);
8f032717 906 int ok = 1;
d8f8dca1
MM
907
908 if (TREE_CODE (decl) == TYPE_DECL && DECL_ARTIFICIAL (decl))
909 /* The new name is the type name. */
910 BINDING_TYPE (binding) = decl;
8f032717
MM
911 else if (!BINDING_VALUE (binding))
912 /* This situation arises when push_class_level_binding moves an
913 inherited type-binding out of the way to make room for a new
914 value binding. */
915 BINDING_VALUE (binding) = decl;
916 else if (TREE_CODE (BINDING_VALUE (binding)) == TYPE_DECL
917 && DECL_ARTIFICIAL (BINDING_VALUE (binding)))
918 {
919 /* The old binding was a type name. It was placed in
920 BINDING_VALUE because it was thought, at the point it was
921 declared, to be the only entity with such a name. Move the
922 type name into the type slot; it is now hidden by the new
923 binding. */
d8f8dca1
MM
924 BINDING_TYPE (binding) = BINDING_VALUE (binding);
925 BINDING_VALUE (binding) = decl;
8f032717 926 INHERITED_VALUE_BINDING_P (binding) = 0;
d8f8dca1 927 }
263505af
MM
928 else if (TREE_CODE (BINDING_VALUE (binding)) == TYPE_DECL
929 && TREE_CODE (decl) == TYPE_DECL
930 && DECL_NAME (decl) == DECL_NAME (BINDING_VALUE (binding))
931 && same_type_p (TREE_TYPE (decl),
932 TREE_TYPE (BINDING_VALUE (binding))))
933 /* We have two typedef-names, both naming the same type to have
934 the same name. This is OK because of:
935
936 [dcl.typedef]
937
938 In a given scope, a typedef specifier can be used to redefine
939 the name of any type declared in that scope to refer to the
940 type to which it already refers. */
941 ok = 0;
8f032717
MM
942 else
943 {
944 cp_error ("declaration of `%#D'", decl);
945 cp_error_at ("conflicts with previous declaration `%#D'",
946 BINDING_VALUE (binding));
947 ok = 0;
948 }
949
950 return ok;
d8f8dca1
MM
951}
952
cfe507be
MM
953/* Add DECL to the list of things declared in B. */
954
955static void
956add_decl_to_level (decl, b)
957 tree decl;
958 struct binding_level *b;
959{
cfe507be
MM
960 /* We build up the list in reverse order, and reverse it later if
961 necessary. */
962 TREE_CHAIN (decl) = b->names;
963 b->names = decl;
964}
965
966/* Bind DECL to ID in the current_binding_level, assumed to be a local
967 binding level. If PUSH_USING is set in FLAGS, we know that DECL
968 doesn't really belong to this binding level, that it got here
969 through a using-declaration. */
f181d4ae
MM
970
971void
0034cf72 972push_local_binding (id, decl, flags)
f181d4ae
MM
973 tree id;
974 tree decl;
0034cf72 975 int flags;
f181d4ae 976{
8f032717
MM
977 struct binding_level *b;
978
979 /* Skip over any local classes. This makes sense if we call
980 push_local_binding with a friend decl of a local class. */
981 b = current_binding_level;
982 while (b->parm_flag == 2)
983 b = b->level_chain;
f181d4ae 984
d8f8dca1 985 if (lookup_name_current_level (id))
8f032717
MM
986 {
987 /* Supplement the existing binding. */
988 if (!add_binding (id, decl))
989 /* It didn't work. Something else must be bound at this
990 level. Do not add DECL to the list of things to pop
991 later. */
992 return;
993 }
d8f8dca1
MM
994 else
995 /* Create a new binding. */
8f032717 996 push_binding (id, decl, b);
f181d4ae 997
0034cf72 998 if (TREE_CODE (decl) == OVERLOAD || (flags & PUSH_USING))
a06d48ef
JM
999 /* We must put the OVERLOAD into a TREE_LIST since the
1000 TREE_CHAIN of an OVERLOAD is already used. Similarly for
1001 decls that got here through a using-declaration. */
1002 decl = build_tree_list (NULL_TREE, decl);
1003
f181d4ae
MM
1004 /* And put DECL on the list of things declared by the current
1005 binding level. */
cfe507be 1006 add_decl_to_level (decl, b);
f181d4ae
MM
1007}
1008
8f032717
MM
1009/* Bind DECL to ID in the class_binding_level. Returns nonzero if the
1010 binding was successful. */
f181d4ae 1011
8f032717 1012int
f181d4ae
MM
1013push_class_binding (id, decl)
1014 tree id;
1015 tree decl;
1016{
8f032717
MM
1017 int result = 1;
1018 tree binding = IDENTIFIER_BINDING (id);
1019 tree context;
1020
1021 /* Note that we declared this value so that we can issue an error if
1022 this an illegal redeclaration of a name already used for some
1023 other purpose. */
1024 note_name_declared_in_class (id, decl);
1025
1026 if (binding && BINDING_LEVEL (binding) == class_binding_level)
d8f8dca1 1027 /* Supplement the existing binding. */
8f032717 1028 result = add_binding (id, decl);
d8f8dca1
MM
1029 else
1030 /* Create a new binding. */
1031 push_binding (id, decl, class_binding_level);
1032
1033 /* Update the IDENTIFIER_CLASS_VALUE for this ID to be the
1034 class-level declaration. Note that we do not use DECL here
1035 because of the possibility of the `struct stat' hack; if DECL is
1036 a class-name or enum-name we might prefer a field-name, or some
1037 such. */
1038 IDENTIFIER_CLASS_VALUE (id) = BINDING_VALUE (IDENTIFIER_BINDING (id));
8f032717
MM
1039
1040 /* If this is a binding from a base class, mark it as such. */
1041 binding = IDENTIFIER_BINDING (id);
1042 if (BINDING_VALUE (binding) == decl && TREE_CODE (decl) != TREE_LIST)
1043 {
280f9385
MM
1044 /* Any implicit typename must be from a base-class. The
1045 context for an implicit typename declaration is always
1046 the derived class in which the lookup was done, so the checks
1047 based on the context of DECL below will not trigger. */
83233dca 1048 if (IMPLICIT_TYPENAME_TYPE_DECL_P (decl))
280f9385 1049 INHERITED_VALUE_BINDING_P (binding) = 1;
8f032717
MM
1050 else
1051 {
280f9385
MM
1052 if (TREE_CODE (decl) == OVERLOAD)
1053 context = DECL_REAL_CONTEXT (OVL_CURRENT (decl));
1054 else
1055 {
1056 my_friendly_assert (TREE_CODE_CLASS (TREE_CODE (decl)) == 'd',
1057 0);
1058 context = DECL_REAL_CONTEXT (decl);
1059 }
8f032717 1060
280f9385
MM
1061 if (is_properly_derived_from (current_class_type, context))
1062 INHERITED_VALUE_BINDING_P (binding) = 1;
1063 else
1064 INHERITED_VALUE_BINDING_P (binding) = 0;
1065 }
8f032717
MM
1066 }
1067 else if (BINDING_VALUE (binding) == decl)
1068 /* We only encounter a TREE_LIST when push_class_decls detects an
1069 ambiguity. Such an ambiguity can be overridden by a definition
1070 in this class. */
1071 INHERITED_VALUE_BINDING_P (binding) = 1;
1072
1073 return result;
f181d4ae
MM
1074}
1075
d8f8dca1
MM
1076/* Remove the binding for DECL which should be the innermost binding
1077 for ID. */
f181d4ae
MM
1078
1079static void
d8f8dca1 1080pop_binding (id, decl)
f181d4ae 1081 tree id;
d8f8dca1 1082 tree decl;
f181d4ae
MM
1083{
1084 tree binding;
d8f8dca1 1085
f181d4ae
MM
1086 if (id == NULL_TREE)
1087 /* It's easiest to write the loops that call this function without
1088 checking whether or not the entities involved have names. We
1089 get here for such an entity. */
1090 return;
1091
d8f8dca1 1092 /* Get the innermost binding for ID. */
f181d4ae 1093 binding = IDENTIFIER_BINDING (id);
f181d4ae 1094
d8f8dca1
MM
1095 /* The name should be bound. */
1096 my_friendly_assert (binding != NULL_TREE, 0);
1097
1098 /* The DECL will be either the ordinary binding or the type
1099 binding for this identifier. Remove that binding. */
1100 if (BINDING_VALUE (binding) == decl)
1101 BINDING_VALUE (binding) = NULL_TREE;
1102 else if (BINDING_TYPE (binding) == decl)
1103 BINDING_TYPE (binding) = NULL_TREE;
1104 else
1105 my_friendly_abort (0);
1106
1107 if (!BINDING_VALUE (binding) && !BINDING_TYPE (binding))
87e3dbc9
MM
1108 /* We're completely done with the innermost binding for this
1109 identifier. Unhook it from the list of bindings. */
1110 IDENTIFIER_BINDING (id) = TREE_CHAIN (binding);
f181d4ae
MM
1111}
1112
acef433b
MM
1113/* When a label goes out of scope, check to see if that label was used
1114 in a valid manner, and issue any appropriate warnings or errors. */
1115
1116static void
1117pop_label (link)
1118 tree link;
1119{
1120 tree label = TREE_VALUE (link);
1121
88848bde 1122 if (!processing_template_decl && doing_semantic_analysis_p ())
acef433b 1123 {
88848bde
MM
1124 if (DECL_INITIAL (label) == NULL_TREE)
1125 {
1126 cp_error_at ("label `%D' used but not defined", label);
1127 /* Avoid crashing later. */
1128 define_label (input_filename, 1, DECL_NAME (label));
1129 }
1130 else if (warn_unused && !TREE_USED (label))
1131 cp_warning_at ("label `%D' defined but not used", label);
acef433b 1132 }
acef433b
MM
1133
1134 SET_IDENTIFIER_LABEL_VALUE (DECL_NAME (label), TREE_PURPOSE (link));
1135}
1136
1137/* At the end of a function, all labels declared within the fucntion
1138 go out of scope. BLOCK is the top-level block for the
1139 function. */
1140
1141static void
1142pop_labels (block)
1143 tree block;
1144{
1145 tree link;
1146
1147 /* Clear out the definitions of all label names, since their scopes
1148 end here. */
1149 for (link = named_labels; link; link = TREE_CHAIN (link))
1150 {
1151 pop_label (link);
1152 /* Put the labels into the "variables" of the top-level block,
1153 so debugger can see them. */
1154 TREE_CHAIN (TREE_VALUE (link)) = BLOCK_VARS (block);
1155 BLOCK_VARS (block) = TREE_VALUE (link);
1156 }
1157
1158 named_labels = NULL_TREE;
1159}
1160
8d08fdba
MS
1161/* Exit a binding level.
1162 Pop the level off, and restore the state of the identifier-decl mappings
1163 that were in effect when this level was entered.
1164
1165 If KEEP == 1, this level had explicit declarations, so
1166 and create a "block" (a BLOCK node) for the level
1167 to record its declarations and subblocks for symbol table output.
1168
8d08fdba
MS
1169 If FUNCTIONBODY is nonzero, this level is the body of a function,
1170 so create a block as if KEEP were set and also clear out all
1171 label names.
1172
1173 If REVERSE is nonzero, reverse the order of decls before putting
1174 them into the BLOCK. */
1175
1176tree
1177poplevel (keep, reverse, functionbody)
1178 int keep;
1179 int reverse;
1180 int functionbody;
1181{
1182 register tree link;
1183 /* The chain of decls was accumulated in reverse order.
1184 Put it into forward order, just for cleanliness. */
1185 tree decls;
1186 int tmp = functionbody;
b35d4555
MM
1187 int real_functionbody;
1188 tree tags;
1189 tree subblocks;
8d08fdba
MS
1190 tree block = NULL_TREE;
1191 tree decl;
1192 int block_previously_created;
f181d4ae
MM
1193 int leaving_for_scope;
1194
b35d4555
MM
1195 if (current_function && !doing_semantic_analysis_p ())
1196 return NULL_TREE;
1197
273a708f
MM
1198 my_friendly_assert (current_binding_level->parm_flag != 2,
1199 19990916);
b74a0560 1200
b35d4555
MM
1201 real_functionbody = (current_binding_level->keep == 2
1202 ? ((functionbody = 0), tmp) : functionbody);
1203 tags = functionbody >= 0 ? current_binding_level->tags : 0;
1204 subblocks = functionbody >= 0 ? current_binding_level->blocks : 0;
1205
b74a0560
MM
1206 my_friendly_assert (!current_binding_level->class_shadowed,
1207 19990414);
8d08fdba 1208
536333d4
MM
1209 /* We used to use KEEP == 2 to indicate that the new block should go
1210 at the beginning of the list of blocks at this binding level,
1211 rather than the end. This hack is no longer used. */
1212 my_friendly_assert (keep == 0 || keep == 1, 0);
1213
8d08fdba
MS
1214 GNU_xref_end_scope ((HOST_WIDE_INT) current_binding_level,
1215 (HOST_WIDE_INT) current_binding_level->level_chain,
1216 current_binding_level->parm_flag,
5566b478 1217 current_binding_level->keep);
8d08fdba
MS
1218
1219 if (current_binding_level->keep == 1)
1220 keep = 1;
1221
8d08fdba
MS
1222 /* Get the decls in the order they were written.
1223 Usually current_binding_level->names is in reverse order.
1224 But parameter decls were previously put in forward order. */
1225
1226 if (reverse)
1227 current_binding_level->names
1228 = decls = nreverse (current_binding_level->names);
1229 else
1230 decls = current_binding_level->names;
1231
1232 /* Output any nested inline functions within this block
1233 if they weren't already output. */
1234
1235 for (decl = decls; decl; decl = TREE_CHAIN (decl))
1236 if (TREE_CODE (decl) == FUNCTION_DECL
1237 && ! TREE_ASM_WRITTEN (decl)
1238 && DECL_INITIAL (decl) != NULL_TREE
6060a796
MS
1239 && TREE_ADDRESSABLE (decl)
1240 && decl_function_context (decl) == current_function_decl)
8d08fdba
MS
1241 {
1242 /* If this decl was copied from a file-scope decl
1243 on account of a block-scope extern decl,
1244 propagate TREE_ADDRESSABLE to the file-scope decl. */
1245 if (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE)
1246 TREE_ADDRESSABLE (DECL_ABSTRACT_ORIGIN (decl)) = 1;
1247 else
1248 {
1249 push_function_context ();
1250 output_inline_function (decl);
1251 pop_function_context ();
1252 }
1253 }
1254
1255 /* If there were any declarations or structure tags in that level,
1256 or if this level is a function body,
1257 create a BLOCK to record them for the life of this function. */
1258
1259 block = NULL_TREE;
1260 block_previously_created = (current_binding_level->this_block != NULL_TREE);
1261 if (block_previously_created)
1262 block = current_binding_level->this_block;
1263 else if (keep == 1 || functionbody)
1264 block = make_node (BLOCK);
1265 if (block != NULL_TREE)
1266 {
72b7eeff
MS
1267 if (block_previously_created)
1268 {
1269 if (decls || tags || subblocks)
1270 {
be99da77 1271 if (BLOCK_VARS (block) || BLOCK_TYPE_TAGS (block))
f181d4ae
MM
1272 warning ("internal compiler error: debugging info corrupted");
1273
72b7eeff
MS
1274 BLOCK_VARS (block) = decls;
1275 BLOCK_TYPE_TAGS (block) = tags;
be99da77
MS
1276
1277 /* We can have previous subblocks and new subblocks when
1278 doing fixup_gotos with complex cleanups. We chain the new
1279 subblocks onto the end of any pre-existing subblocks. */
1280 BLOCK_SUBBLOCKS (block) = chainon (BLOCK_SUBBLOCKS (block),
1281 subblocks);
72b7eeff 1282 }
be99da77
MS
1283 /* If we created the block earlier on, and we are just
1284 diddling it now, then it already should have a proper
1285 BLOCK_END_NOTE value associated with it. */
72b7eeff
MS
1286 }
1287 else
1288 {
1289 BLOCK_VARS (block) = decls;
1290 BLOCK_TYPE_TAGS (block) = tags;
1291 BLOCK_SUBBLOCKS (block) = subblocks;
f181d4ae
MM
1292 /* Otherwise, for a new block, install a new BLOCK_END_NOTE
1293 value. */
72b7eeff
MS
1294 remember_end_note (block);
1295 }
8d08fdba
MS
1296 }
1297
1298 /* In each subblock, record that this is its superior. */
1299
1300 if (keep >= 0)
1301 for (link = subblocks; link; link = TREE_CHAIN (link))
1302 BLOCK_SUPERCONTEXT (link) = block;
1303
f181d4ae
MM
1304 /* We still support the old for-scope rules, whereby the variables
1305 in a for-init statement were in scope after the for-statement
1306 ended. We only use the new rules in flag_new_for_scope is
1307 nonzero. */
1308 leaving_for_scope
1309 = current_binding_level->is_for_scope && flag_new_for_scope == 1;
1310
1311 /* Remove declarations for all the DECLs in this level. */
1312 for (link = decls; link; link = TREE_CHAIN (link))
1313 {
1314 if (leaving_for_scope && TREE_CODE (link) == VAR_DECL)
1315 {
1316 tree outer_binding
1317 = TREE_CHAIN (IDENTIFIER_BINDING (DECL_NAME (link)));
1318 tree ns_binding;
1319
1320 if (!outer_binding)
1321 ns_binding = IDENTIFIER_NAMESPACE_VALUE (DECL_NAME (link));
c7dda1e3
MM
1322 else
1323 ns_binding = NULL_TREE;
1324
f181d4ae
MM
1325 if (outer_binding
1326 && (BINDING_LEVEL (outer_binding)
1327 == current_binding_level->level_chain))
1328 /* We have something like:
1329
1330 int i;
1331 for (int i; ;);
1332
1333 and we are leaving the `for' scope. There's no reason to
1334 keep the binding of the inner `i' in this case. */
d8f8dca1 1335 pop_binding (DECL_NAME (link), link);
f181d4ae
MM
1336 else if ((outer_binding
1337 && (TREE_CODE (BINDING_VALUE (outer_binding))
1338 == TYPE_DECL))
1339 || (ns_binding
1340 && TREE_CODE (ns_binding) == TYPE_DECL))
1341 /* Here, we have something like:
1342
1343 typedef int I;
1344
1345 void f () {
1346 for (int I; ;);
1347 }
1348
1349 We must pop the for-scope binding so we know what's a
1350 type and what isn't. */
d8f8dca1 1351 pop_binding (DECL_NAME (link), link);
e76a2646 1352 else
e76a2646 1353 {
f181d4ae
MM
1354 /* Mark this VAR_DECL as dead so that we can tell we left it
1355 there only for backward compatibility. */
1356 DECL_DEAD_FOR_LOCAL (link) = 1;
1357
1358 /* Keep track of what should of have happenned when we
1359 popped the binding. */
1360 if (outer_binding && BINDING_VALUE (outer_binding))
1361 DECL_SHADOWED_FOR_VAR (link)
1362 = BINDING_VALUE (outer_binding);
1363
1364 /* Add it to the list of dead variables in the next
1365 outermost binding to that we can remove these when we
1366 leave that binding. */
1367 current_binding_level->level_chain->dead_vars_from_for
1368 = tree_cons (NULL_TREE, link,
1369 current_binding_level->level_chain->
1370 dead_vars_from_for);
1371
1372 /* Although we don't pop the CPLUS_BINDING, we do clear
1373 its BINDING_LEVEL since the level is going away now. */
1374 BINDING_LEVEL (IDENTIFIER_BINDING (DECL_NAME (link)))
1375 = 0;
e76a2646 1376 }
2ee887f2 1377 }
f181d4ae 1378 else
8d6e462b 1379 {
f181d4ae 1380 /* Remove the binding. */
0034cf72
JM
1381 decl = link;
1382 if (TREE_CODE (decl) == TREE_LIST)
1383 decl = TREE_VALUE (decl);
1384 if (TREE_CODE_CLASS (TREE_CODE (decl)) == 'd')
1385 pop_binding (DECL_NAME (decl), decl);
1386 else if (TREE_CODE (decl) == OVERLOAD)
1387 pop_binding (DECL_NAME (OVL_FUNCTION (decl)), decl);
d8f8dca1 1388 else
f181d4ae 1389 my_friendly_abort (0);
8d08fdba 1390 }
f181d4ae 1391 }
8d08fdba 1392
f181d4ae
MM
1393 /* Remove declarations for any `for' variables from inner scopes
1394 that we kept around. */
1395 for (link = current_binding_level->dead_vars_from_for;
1396 link; link = TREE_CHAIN (link))
d8f8dca1 1397 pop_binding (DECL_NAME (TREE_VALUE (link)), TREE_VALUE (link));
2ee887f2 1398
f181d4ae
MM
1399 /* Restore the IDENTIFIER_TYPE_VALUEs. */
1400 for (link = current_binding_level->type_shadowed;
1401 link; link = TREE_CHAIN (link))
1402 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (link), TREE_VALUE (link));
acef433b
MM
1403
1404 /* Restore the IDENTIFIER_LABEL_VALUEs for local labels. */
1405 for (link = current_binding_level->shadowed_labels;
1406 link;
1407 link = TREE_CHAIN (link))
1408 pop_label (link);
1409
f181d4ae
MM
1410 /* There may be OVERLOADs (wrapped in TREE_LISTs) on the BLOCK_VARs
1411 list if a `using' declaration put them there. The debugging
1412 back-ends won't understand OVERLOAD, so we remove them here.
1413 Because the BLOCK_VARS are (temporarily) shared with
1414 CURRENT_BINDING_LEVEL->NAMES we must do this fixup after we have
1415 popped all the bindings. */
1416 if (block)
1417 {
1418 tree* d;
8d6e462b 1419
a06d48ef
JM
1420 for (d = &BLOCK_VARS (block); *d; )
1421 {
1422 if (TREE_CODE (*d) == TREE_LIST)
1423 *d = TREE_CHAIN (*d);
1424 else
1425 d = &TREE_CHAIN (*d);
1426 }
8d6e462b 1427 }
8d08fdba
MS
1428
1429 /* If the level being exited is the top level of a function,
1430 check over all the labels. */
8d08fdba
MS
1431 if (functionbody)
1432 {
acef433b
MM
1433 /* Since this is the top level block of a function, the vars are
1434 the function's parameters. Don't leave them in the BLOCK
1435 because they are found in the FUNCTION_DECL instead. */
8d08fdba 1436 BLOCK_VARS (block) = 0;
acef433b 1437 pop_labels (block);
8d08fdba
MS
1438 }
1439
1440 /* Any uses of undefined labels now operate under constraints
1441 of next binding contour. */
a8f73d4b
MM
1442 if (current_function)
1443 {
1444 struct binding_level *level_chain;
1445 level_chain = current_binding_level->level_chain;
1446 if (level_chain)
1447 {
1448 struct named_label_list *labels;
1449 for (labels = named_label_uses; labels; labels = labels->next)
1450 if (labels->binding_level == current_binding_level)
1451 {
1452 labels->binding_level = level_chain;
1453 labels->names_in_scope = level_chain->names;
1454 }
1455 }
1456 }
8d08fdba
MS
1457
1458 tmp = current_binding_level->keep;
1459
1460 pop_binding_level ();
1461 if (functionbody)
1462 DECL_INITIAL (current_function_decl) = block;
1463 else if (block)
1464 {
1465 if (!block_previously_created)
1466 current_binding_level->blocks
1467 = chainon (current_binding_level->blocks, block);
1468 }
1469 /* If we did not make a block for the level just exited,
1470 any blocks made for inner levels
1471 (since they cannot be recorded as subblocks in that level)
1472 must be carried forward so they will later become subblocks
1473 of something else. */
1474 else if (subblocks)
536333d4
MM
1475 current_binding_level->blocks
1476 = chainon (current_binding_level->blocks, subblocks);
8d08fdba
MS
1477
1478 /* Take care of compiler's internal binding structures. */
a4443a08 1479 if (tmp == 2)
8d08fdba 1480 {
8d08fdba
MS
1481 expand_end_bindings (getdecls (), keep, 1);
1482 /* Each and every BLOCK node created here in `poplevel' is important
1483 (e.g. for proper debugging information) so if we created one
1484 earlier, mark it as "used". */
1485 if (block)
1486 TREE_USED (block) = 1;
1487 block = poplevel (keep, reverse, real_functionbody);
1488 }
1489
1490 /* Each and every BLOCK node created here in `poplevel' is important
1491 (e.g. for proper debugging information) so if we created one
1492 earlier, mark it as "used". */
1493 if (block)
1494 TREE_USED (block) = 1;
1495 return block;
1496}
1497
1498/* Delete the node BLOCK from the current binding level.
1499 This is used for the block inside a stmt expr ({...})
1500 so that the block can be reinserted where appropriate. */
1501
1502void
1503delete_block (block)
1504 tree block;
1505{
1506 tree t;
1507 if (current_binding_level->blocks == block)
1508 current_binding_level->blocks = TREE_CHAIN (block);
1509 for (t = current_binding_level->blocks; t;)
1510 {
1511 if (TREE_CHAIN (t) == block)
1512 TREE_CHAIN (t) = TREE_CHAIN (block);
1513 else
1514 t = TREE_CHAIN (t);
1515 }
1516 TREE_CHAIN (block) = NULL_TREE;
1517 /* Clear TREE_USED which is always set by poplevel.
1518 The flag is set again if insert_block is called. */
1519 TREE_USED (block) = 0;
1520}
1521
1522/* Insert BLOCK at the end of the list of subblocks of the
1523 current binding level. This is used when a BIND_EXPR is expanded,
1524 to handle the BLOCK node inside the BIND_EXPR. */
1525
1526void
1527insert_block (block)
1528 tree block;
1529{
1530 TREE_USED (block) = 1;
1531 current_binding_level->blocks
1532 = chainon (current_binding_level->blocks, block);
1533}
1534
8d08fdba
MS
1535/* Set the BLOCK node for the innermost scope
1536 (the one we are currently in). */
1537
1538void
1539set_block (block)
1540 register tree block;
1541{
1542 current_binding_level->this_block = block;
1543}
1544
1545/* Do a pushlevel for class declarations. */
e92cc029 1546
8d08fdba
MS
1547void
1548pushlevel_class ()
1549{
1550 register struct binding_level *newlevel;
1551
1552 /* Reuse or create a struct for this binding level. */
1553#if defined(DEBUG_CP_BINDING_LEVELS)
1554 if (0)
1555#else /* !defined(DEBUG_CP_BINDING_LEVELS) */
1556 if (free_binding_level)
1557#endif /* !defined(DEBUG_CP_BINDING_LEVELS) */
1558 {
1559 newlevel = free_binding_level;
1560 free_binding_level = free_binding_level->level_chain;
1561 }
1562 else
f181d4ae 1563 newlevel = make_binding_level ();
8d08fdba
MS
1564
1565#if defined(DEBUG_CP_BINDING_LEVELS)
1566 is_class_level = 1;
1567#endif /* defined(DEBUG_CP_BINDING_LEVELS) */
1568
1569 push_binding_level (newlevel, 0, 0);
1570
1571 decl_stack = push_decl_level (decl_stack, &decl_obstack);
1572 class_binding_level = current_binding_level;
1573 class_binding_level->parm_flag = 2;
8d08fdba
MS
1574}
1575
b74a0560 1576/* ...and a poplevel for class declarations. */
e92cc029 1577
273a708f 1578void
b74a0560 1579poplevel_class ()
8d08fdba
MS
1580{
1581 register struct binding_level *level = class_binding_level;
8d08fdba
MS
1582 tree shadowed;
1583
1584 my_friendly_assert (level != 0, 354);
1585
1586 decl_stack = pop_stack_level (decl_stack);
8d08fdba 1587 /* If we're leaving a toplevel class, don't bother to do the setting
ddd5a7c1 1588 of IDENTIFIER_CLASS_VALUE to NULL_TREE, since first of all this slot
8d08fdba 1589 shouldn't even be used when current_class_type isn't set, and second,
ddd5a7c1 1590 if we don't touch it here, we're able to use the cache effect if the
8d08fdba 1591 next time we're entering a class scope, it is the same class. */
b74a0560 1592 if (current_class_depth != 1)
8f032717
MM
1593 {
1594 struct binding_level* b;
1595
1596 /* Clear out our IDENTIFIER_CLASS_VALUEs. */
1597 for (shadowed = level->class_shadowed;
1598 shadowed;
1599 shadowed = TREE_CHAIN (shadowed))
1600 IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (shadowed)) = NULL_TREE;
1601
1602 /* Find the next enclosing class, and recreate
1603 IDENTIFIER_CLASS_VALUEs appropriate for that class. */
1604 b = level->level_chain;
1605 while (b && b->parm_flag != 2)
1606 b = b->level_chain;
1607
1608 if (b)
1609 for (shadowed = b->class_shadowed;
1610 shadowed;
1611 shadowed = TREE_CHAIN (shadowed))
1612 {
1613 tree t;
1614
1615 t = IDENTIFIER_BINDING (TREE_PURPOSE (shadowed));
1616 while (t && BINDING_LEVEL (t) != b)
1617 t = TREE_CHAIN (t);
1618
1619 if (t)
1620 IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (shadowed))
1621 = BINDING_VALUE (t);
1622 }
1623 }
8d08fdba
MS
1624 else
1625 /* Remember to save what IDENTIFIER's were bound in this scope so we
1626 can recover from cache misses. */
e76a2646
MS
1627 {
1628 previous_class_type = current_class_type;
1629 previous_class_values = class_binding_level->class_shadowed;
1630 }
8d08fdba
MS
1631 for (shadowed = level->type_shadowed;
1632 shadowed;
1633 shadowed = TREE_CHAIN (shadowed))
2c73f9f5 1634 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (shadowed), TREE_VALUE (shadowed));
8d08fdba 1635
f181d4ae
MM
1636 /* Remove the bindings for all of the class-level declarations. */
1637 for (shadowed = level->class_shadowed;
1638 shadowed;
1639 shadowed = TREE_CHAIN (shadowed))
d8f8dca1 1640 pop_binding (TREE_PURPOSE (shadowed), TREE_TYPE (shadowed));
f181d4ae 1641
8d08fdba
MS
1642 GNU_xref_end_scope ((HOST_WIDE_INT) class_binding_level,
1643 (HOST_WIDE_INT) class_binding_level->level_chain,
1644 class_binding_level->parm_flag,
5566b478 1645 class_binding_level->keep);
8d08fdba 1646
38e01259 1647 /* Now, pop out of the binding level which we created up in the
8d08fdba
MS
1648 `pushlevel_class' routine. */
1649#if defined(DEBUG_CP_BINDING_LEVELS)
1650 is_class_level = 1;
1651#endif /* defined(DEBUG_CP_BINDING_LEVELS) */
1652
1653 pop_binding_level ();
8d08fdba 1654}
8f032717
MM
1655
1656/* We are entering the scope of a class. Clear IDENTIFIER_CLASS_VALUE
1657 for any names in enclosing classes. */
1658
1659void
1660clear_identifier_class_values ()
1661{
1662 tree t;
1663
1664 if (!class_binding_level)
1665 return;
1666
1667 for (t = class_binding_level->class_shadowed;
1668 t;
1669 t = TREE_CHAIN (t))
1670 IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (t)) = NULL_TREE;
1671}
1672
0aafb128
MM
1673/* Returns non-zero if T is a virtual function table. */
1674
1675int
1676vtable_decl_p (t, data)
1677 tree t;
1678 void *data ATTRIBUTE_UNUSED;
1679{
1680 return (TREE_CODE (t) == VAR_DECL && DECL_VIRTUAL_P (t));
1681}
1682
1683/* Returns non-zero if T is a TYPE_DECL for a type with virtual
1684 functions. */
1685
1686int
1687vtype_decl_p (t, data)
1688 tree t;
1689 void *data ATTRIBUTE_UNUSED;
1690{
1691 return (TREE_CODE (t) == TYPE_DECL
1692 && TREE_TYPE (t) != error_mark_node
1693 && TYPE_LANG_SPECIFIC (TREE_TYPE (t))
1694 && CLASSTYPE_VSIZE (TREE_TYPE (t)));
1695}
1696
f71f87f9
MM
1697/* Return the declarations that are members of the namespace NS. */
1698
1699tree
1700cp_namespace_decls (ns)
1701 tree ns;
1702{
1703 return NAMESPACE_LEVEL (ns)->names;
1704}
1705
0aafb128
MM
1706/* Walk all the namespaces contained NAMESPACE, including NAMESPACE
1707 itself, calling F for each. The DATA is passed to F as well. */
1708
1709static int
1710walk_namespaces_r (namespace, f, data)
1711 tree namespace;
1712 walk_namespaces_fn f;
1713 void *data;
1714{
1715 tree current;
1716 int result = 0;
1717
1718 result |= (*f) (namespace, data);
1719
f71f87f9 1720 for (current = cp_namespace_decls (namespace);
0aafb128
MM
1721 current;
1722 current = TREE_CHAIN (current))
1723 {
1724 if (TREE_CODE (current) != NAMESPACE_DECL
1725 || DECL_NAMESPACE_ALIAS (current))
1726 continue;
1727 if (!DECL_LANG_SPECIFIC (current))
1728 {
1729 /* Hmm. std. */
1730 my_friendly_assert (current == std_node, 393);
1731 continue;
1732 }
1733
1734 /* We found a namespace. */
1735 result |= walk_namespaces_r (current, f, data);
1736 }
1737
1738 return result;
1739}
1740
1741/* Walk all the namespaces, calling F for each. The DATA is passed to
1742 F as well. */
1743
1744int
1745walk_namespaces (f, data)
1746 walk_namespaces_fn f;
1747 void *data;
1748{
1749 return walk_namespaces_r (global_namespace, f, data);
1750}
1751
1752struct walk_globals_data {
1753 walk_globals_pred p;
1754 walk_globals_fn f;
1755 void *data;
1756};
1757
1758/* Walk the global declarations in NAMESPACE. Whenever one is found
1759 for which P returns non-zero, call F with its address. If any call
1760 to F returns a non-zero value, return a non-zero value. */
1761
1762static int
1763walk_globals_r (namespace, data)
1764 tree namespace;
1765 void *data;
1766{
1767 struct walk_globals_data* wgd = (struct walk_globals_data *) data;
1768 walk_globals_pred p = wgd->p;
1769 walk_globals_fn f = wgd->f;
1770 void *d = wgd->data;
1771 tree *t;
1772 int result = 0;
1773
1774 t = &NAMESPACE_LEVEL (namespace)->names;
1775
1776 while (*t)
1777 {
1778 tree glbl = *t;
1779
1780 if ((*p) (glbl, d))
1781 result |= (*f) (t, d);
1782
1783 /* If F changed *T, then *T still points at the next item to
1784 examine. */
1785 if (*t == glbl)
1786 t = &TREE_CHAIN (*t);
1787 }
1788
1789 return result;
1790}
1791
1792/* Walk the global declarations. Whenever one is found for which P
1793 returns non-zero, call F with its address. If any call to F
1794 returns a non-zero value, return a non-zero value. */
1795
1796int
1797walk_globals (p, f, data)
1798 walk_globals_pred p;
1799 walk_globals_fn f;
1800 void *data;
1801{
1802 struct walk_globals_data wgd;
1803 wgd.p = p;
1804 wgd.f = f;
1805 wgd.data = data;
1806
1807 return walk_namespaces (walk_globals_r, &wgd);
1808}
1809
1810/* Call wrapup_globals_declarations for the globals in NAMESPACE. If
1811 DATA is non-NULL, this is the last time we will call
1812 wrapup_global_declarations for this NAMESPACE. */
1813
1814int
1815wrapup_globals_for_namespace (namespace, data)
1816 tree namespace;
1817 void *data;
1818{
f71f87f9 1819 tree globals = cp_namespace_decls (namespace);
0aafb128
MM
1820 int len = list_length (globals);
1821 tree *vec = (tree *) alloca (sizeof (tree) * len);
1822 int i;
0352cfc8 1823 int result;
0aafb128
MM
1824 tree decl;
1825 int last_time = (data != 0);
1826
1827 if (last_time && namespace == global_namespace)
1828 /* Let compile_file handle the global namespace. */
1829 return 0;
1830
1831 /* Process the decls in reverse order--earliest first.
1832 Put them into VEC from back to front, then take out from front. */
1833
1834 for (i = 0, decl = globals; i < len; i++, decl = TREE_CHAIN (decl))
1835 vec[len - i - 1] = decl;
1836
0352cfc8
MM
1837 if (last_time)
1838 {
1839 check_global_declarations (vec, len);
1840 return 0;
1841 }
1842
1843 /* Temporarily mark vtables as external. That prevents
1844 wrapup_global_declarations from writing them out; we must process
1845 them ourselves in finish_vtable_vardecl. */
1846 for (i = 0; i < len; ++i)
91f2aa22 1847 if (vtable_decl_p (vec[i], /*data=*/0) && !DECL_EXTERNAL (vec[i]))
0352cfc8
MM
1848 {
1849 DECL_NOT_REALLY_EXTERN (vec[i]) = 1;
1850 DECL_EXTERNAL (vec[i]) = 1;
1851 }
0aafb128 1852
0352cfc8
MM
1853 /* Write out any globals that need to be output. */
1854 result = wrapup_global_declarations (vec, len);
1855
1856 /* Undo the hack to DECL_EXTERNAL above. */
1857 for (i = 0; i < len; ++i)
1858 if (vtable_decl_p (vec[i], /*data=*/0)
1859 && DECL_NOT_REALLY_EXTERN (vec[i]))
1860 {
1861 DECL_NOT_REALLY_EXTERN (vec[i]) = 0;
1862 DECL_EXTERNAL (vec[i]) = 0;
1863 }
1864
1865 return result;
0aafb128
MM
1866}
1867
4519c0a8 1868\f
fc6af6e3
RH
1869/* Mark ARG (which is really a struct binding_level **) for GC. */
1870
4519c0a8
MM
1871static void
1872mark_binding_level (arg)
1873 void *arg;
1874{
1875 struct binding_level *lvl = *(struct binding_level **)arg;
1876
1877 while (lvl)
1878 {
1879 ggc_mark_tree (lvl->names);
1880 ggc_mark_tree (lvl->tags);
1881 ggc_mark_tree (lvl->usings);
1882 ggc_mark_tree (lvl->using_directives);
1883 ggc_mark_tree (lvl->class_shadowed);
1884 ggc_mark_tree (lvl->type_shadowed);
1885 ggc_mark_tree (lvl->shadowed_labels);
1886 ggc_mark_tree (lvl->blocks);
1887 ggc_mark_tree (lvl->this_block);
1888 ggc_mark_tree (lvl->incomplete);
1889 ggc_mark_tree (lvl->dead_vars_from_for);
1890
1891 lvl = lvl->level_chain;
1892 }
1893}
8d08fdba
MS
1894\f
1895/* For debugging. */
5566b478
MS
1896static int no_print_functions = 0;
1897static int no_print_builtins = 0;
8d08fdba
MS
1898
1899void
1900print_binding_level (lvl)
1901 struct binding_level *lvl;
1902{
1903 tree t;
1904 int i = 0, len;
1905 fprintf (stderr, " blocks=");
1906 fprintf (stderr, HOST_PTR_PRINTF, lvl->blocks);
1907 fprintf (stderr, " n_incomplete=%d parm_flag=%d keep=%d",
f30432d7 1908 list_length (lvl->incomplete), lvl->parm_flag, lvl->keep);
8d08fdba
MS
1909 if (lvl->tag_transparent)
1910 fprintf (stderr, " tag-transparent");
1911 if (lvl->more_cleanups_ok)
1912 fprintf (stderr, " more-cleanups-ok");
1913 if (lvl->have_cleanups)
1914 fprintf (stderr, " have-cleanups");
8d08fdba
MS
1915 fprintf (stderr, "\n");
1916 if (lvl->names)
1917 {
1918 fprintf (stderr, " names:\t");
1919 /* We can probably fit 3 names to a line? */
1920 for (t = lvl->names; t; t = TREE_CHAIN (t))
1921 {
fc378698 1922 if (no_print_functions && (TREE_CODE (t) == FUNCTION_DECL))
8d08fdba
MS
1923 continue;
1924 if (no_print_builtins
fc378698
MS
1925 && (TREE_CODE (t) == TYPE_DECL)
1926 && (!strcmp (DECL_SOURCE_FILE (t),"<built-in>")))
8d08fdba
MS
1927 continue;
1928
1929 /* Function decls tend to have longer names. */
1930 if (TREE_CODE (t) == FUNCTION_DECL)
1931 len = 3;
1932 else
1933 len = 2;
1934 i += len;
1935 if (i > 6)
1936 {
1937 fprintf (stderr, "\n\t");
1938 i = len;
1939 }
1940 print_node_brief (stderr, "", t, 0);
bd6dd845 1941 if (t == error_mark_node)
8d08fdba
MS
1942 break;
1943 }
1944 if (i)
1945 fprintf (stderr, "\n");
1946 }
1947 if (lvl->tags)
1948 {
1949 fprintf (stderr, " tags:\t");
1950 i = 0;
1951 for (t = lvl->tags; t; t = TREE_CHAIN (t))
1952 {
1953 if (TREE_PURPOSE (t) == NULL_TREE)
1954 len = 3;
1955 else if (TREE_PURPOSE (t) == TYPE_IDENTIFIER (TREE_VALUE (t)))
1956 len = 2;
1957 else
1958 len = 4;
1959 i += len;
1960 if (i > 5)
1961 {
1962 fprintf (stderr, "\n\t");
1963 i = len;
1964 }
1965 if (TREE_PURPOSE (t) == NULL_TREE)
1966 {
1967 print_node_brief (stderr, "<unnamed-typedef", TREE_VALUE (t), 0);
1968 fprintf (stderr, ">");
1969 }
1970 else if (TREE_PURPOSE (t) == TYPE_IDENTIFIER (TREE_VALUE (t)))
1971 print_node_brief (stderr, "", TREE_VALUE (t), 0);
1972 else
1973 {
1974 print_node_brief (stderr, "<typedef", TREE_PURPOSE (t), 0);
1975 print_node_brief (stderr, "", TREE_VALUE (t), 0);
1976 fprintf (stderr, ">");
1977 }
1978 }
1979 if (i)
1980 fprintf (stderr, "\n");
1981 }
8d08fdba
MS
1982 if (lvl->class_shadowed)
1983 {
1984 fprintf (stderr, " class-shadowed:");
1985 for (t = lvl->class_shadowed; t; t = TREE_CHAIN (t))
1986 {
1987 fprintf (stderr, " %s ", IDENTIFIER_POINTER (TREE_PURPOSE (t)));
1988 }
1989 fprintf (stderr, "\n");
1990 }
1991 if (lvl->type_shadowed)
1992 {
1993 fprintf (stderr, " type-shadowed:");
1994 for (t = lvl->type_shadowed; t; t = TREE_CHAIN (t))
1995 {
8d08fdba 1996 fprintf (stderr, " %s ", IDENTIFIER_POINTER (TREE_PURPOSE (t)));
8d08fdba
MS
1997 }
1998 fprintf (stderr, "\n");
1999 }
2000}
2001
2002void
2003print_other_binding_stack (stack)
2004 struct binding_level *stack;
2005{
2006 struct binding_level *level;
2007 for (level = stack; level != global_binding_level; level = level->level_chain)
2008 {
2009 fprintf (stderr, "binding level ");
2010 fprintf (stderr, HOST_PTR_PRINTF, level);
2011 fprintf (stderr, "\n");
2012 print_binding_level (level);
2013 }
2014}
2015
2016void
2017print_binding_stack ()
2018{
2019 struct binding_level *b;
2020 fprintf (stderr, "current_binding_level=");
2021 fprintf (stderr, HOST_PTR_PRINTF, current_binding_level);
2022 fprintf (stderr, "\nclass_binding_level=");
2023 fprintf (stderr, HOST_PTR_PRINTF, class_binding_level);
2024 fprintf (stderr, "\nglobal_binding_level=");
2025 fprintf (stderr, HOST_PTR_PRINTF, global_binding_level);
2026 fprintf (stderr, "\n");
2027 if (class_binding_level)
2028 {
2029 for (b = class_binding_level; b; b = b->level_chain)
2030 if (b == current_binding_level)
2031 break;
2032 if (b)
2033 b = class_binding_level;
2034 else
2035 b = current_binding_level;
2036 }
2037 else
2038 b = current_binding_level;
2039 print_other_binding_stack (b);
2040 fprintf (stderr, "global:\n");
2041 print_binding_level (global_binding_level);
2042}
a9aedbc2 2043
2c73f9f5
ML
2044/* Namespace binding access routines: The namespace_bindings field of
2045 the identifier is polymorphic, with three possible values:
2046 NULL_TREE, a list of CPLUS_BINDINGS, or any other tree_node
2047 indicating the BINDING_VALUE of global_namespace. */
30394414 2048
2c73f9f5
ML
2049/* Check whether the a binding for the name to scope is known.
2050 Assumes that the bindings of the name are already a list
2051 of bindings. Returns the binding found, or NULL_TREE. */
2052
2053static tree
2054find_binding (name, scope)
30394414
JM
2055 tree name;
2056 tree scope;
2057{
2c73f9f5 2058 tree iter, prev = NULL_TREE;
3e3f722c
ML
2059
2060 scope = ORIGINAL_NAMESPACE (scope);
2061
30394414
JM
2062 for (iter = IDENTIFIER_NAMESPACE_BINDINGS (name); iter;
2063 iter = TREE_CHAIN (iter))
2064 {
2065 my_friendly_assert (TREE_CODE (iter) == CPLUS_BINDING, 374);
2066 if (BINDING_SCOPE (iter) == scope)
2c73f9f5 2067 {
cfe507be 2068 /* Move binding found to the front of the list, so
2c73f9f5
ML
2069 subsequent lookups will find it faster. */
2070 if (prev)
2071 {
2072 TREE_CHAIN (prev) = TREE_CHAIN (iter);
2073 TREE_CHAIN (iter) = IDENTIFIER_NAMESPACE_BINDINGS (name);
2074 IDENTIFIER_NAMESPACE_BINDINGS (name) = iter;
2075 }
2076 return iter;
2077 }
2078 prev = iter;
30394414 2079 }
2c73f9f5
ML
2080 return NULL_TREE;
2081}
2082
2083/* Always returns a binding for name in scope. If the
2084 namespace_bindings is not a list, convert it to one first.
2085 If no binding is found, make a new one. */
2086
2087tree
2088binding_for_name (name, scope)
2089 tree name;
2090 tree scope;
2091{
2092 tree b = IDENTIFIER_NAMESPACE_BINDINGS (name);
2093 tree result;
3e3f722c
ML
2094
2095 scope = ORIGINAL_NAMESPACE (scope);
2096
2c73f9f5
ML
2097 if (b && TREE_CODE (b) != CPLUS_BINDING)
2098 {
2099 /* Get rid of optimization for global scope. */
2100 IDENTIFIER_NAMESPACE_BINDINGS (name) = NULL_TREE;
2101 BINDING_VALUE (binding_for_name (name, global_namespace)) = b;
2102 b = IDENTIFIER_NAMESPACE_BINDINGS (name);
2103 }
2104 if (b && (result = find_binding (name, scope)))
2105 return result;
2106 /* Not found, make a new permanent one. */
30394414 2107 push_obstacks (&permanent_obstack, &permanent_obstack);
2c73f9f5
ML
2108 result = make_node (CPLUS_BINDING);
2109 TREE_CHAIN (result) = b;
2110 IDENTIFIER_NAMESPACE_BINDINGS (name) = result;
2111 BINDING_SCOPE (result) = scope;
2112 BINDING_TYPE (result) = NULL_TREE;
2113 BINDING_VALUE (result) = NULL_TREE;
30394414 2114 pop_obstacks ();
2c73f9f5
ML
2115 return result;
2116}
2117
2118/* Return the binding value for name in scope, considering that
2119 namespace_binding may or may not be a list of CPLUS_BINDINGS. */
2120
2121tree
2122namespace_binding (name, scope)
2123 tree name;
2124 tree scope;
2125{
2126 tree b = IDENTIFIER_NAMESPACE_BINDINGS (name);
2127 if (b == NULL_TREE)
2128 return NULL_TREE;
cb0dbb9a
JM
2129 if (scope == NULL_TREE)
2130 scope = global_namespace;
2c73f9f5
ML
2131 if (TREE_CODE (b) != CPLUS_BINDING)
2132 return (scope == global_namespace) ? b : NULL_TREE;
2133 name = find_binding (name,scope);
2134 if (name == NULL_TREE)
2135 return name;
2136 return BINDING_VALUE (name);
2137}
2138
2139/* Set the binding value for name in scope. If modifying the binding
2140 of global_namespace is attempted, try to optimize it. */
2141
2142void
2143set_namespace_binding (name, scope, val)
2144 tree name;
2145 tree scope;
2146 tree val;
2147{
2148 tree b;
cb0dbb9a
JM
2149
2150 if (scope == NULL_TREE)
2151 scope = global_namespace;
2152
2c73f9f5
ML
2153 if (scope == global_namespace)
2154 {
2155 b = IDENTIFIER_NAMESPACE_BINDINGS (name);
2156 if (b == NULL_TREE || TREE_CODE (b) != CPLUS_BINDING)
2157 {
2158 IDENTIFIER_NAMESPACE_BINDINGS (name) = val;
2159 return;
2160 }
2161 }
2162 b = binding_for_name (name, scope);
2163 BINDING_VALUE (b) = val;
30394414
JM
2164}
2165
dff6b454
RK
2166/* Push into the scope of the NAME namespace. If NAME is NULL_TREE, then we
2167 select a name that is unique to this compilation unit. */
e92cc029 2168
a9aedbc2
MS
2169void
2170push_namespace (name)
2171 tree name;
2172{
b370501f 2173 tree d = NULL_TREE;
30394414
JM
2174 int need_new = 1;
2175 int implicit_use = 0;
2c73f9f5 2176 int global = 0;
30394414
JM
2177 if (!global_namespace)
2178 {
2c73f9f5 2179 /* This must be ::. */
30394414 2180 my_friendly_assert (name == get_identifier ("::"), 377);
2c73f9f5 2181 global = 1;
30394414
JM
2182 }
2183 else if (!name)
2184 {
3ab52652
ML
2185 /* The name of anonymous namespace is unique for the translation
2186 unit. */
0c8feefe
MM
2187 if (!anonymous_namespace_name)
2188 anonymous_namespace_name = get_file_function_name ('N');
2189 name = anonymous_namespace_name;
3ab52652
ML
2190 d = IDENTIFIER_NAMESPACE_VALUE (name);
2191 if (d)
2192 /* Reopening anonymous namespace. */
2193 need_new = 0;
30394414 2194 implicit_use = 1;
2c73f9f5
ML
2195 }
2196 else if (current_namespace == global_namespace
2197 && name == DECL_NAME (std_node))
2198 {
2199 in_std++;
2200 return;
2201 }
30394414
JM
2202 else
2203 {
2c73f9f5 2204 /* Check whether this is an extended namespace definition. */
30394414
JM
2205 d = IDENTIFIER_NAMESPACE_VALUE (name);
2206 if (d != NULL_TREE && TREE_CODE (d) == NAMESPACE_DECL)
3e3f722c
ML
2207 {
2208 need_new = 0;
2209 if (DECL_NAMESPACE_ALIAS (d))
2210 {
8251199e 2211 cp_error ("namespace alias `%D' not allowed here, assuming `%D'",
3e3f722c
ML
2212 d, DECL_NAMESPACE_ALIAS (d));
2213 d = DECL_NAMESPACE_ALIAS (d);
2214 }
2215 }
30394414 2216 }
6633d636 2217
30394414
JM
2218 if (need_new)
2219 {
2c73f9f5 2220 /* Make a new namespace, binding the name to it. */
6b57ac29 2221 d = build_lang_decl (NAMESPACE_DECL, name, void_type_node);
2c73f9f5
ML
2222 /* The global namespace is not pushed, and the global binding
2223 level is set elsewhere. */
2224 if (!global)
2225 {
2226 d = pushdecl (d);
2227 pushlevel (0);
2228 declare_namespace_level ();
2229 NAMESPACE_LEVEL (d) = current_binding_level;
2230 }
30394414 2231 }
2c73f9f5
ML
2232 else
2233 resume_binding_level (NAMESPACE_LEVEL (d));
2234
30394414
JM
2235 if (implicit_use)
2236 do_using_directive (d);
2c73f9f5 2237 /* Enter the name space. */
30394414 2238 current_namespace = d;
a9aedbc2
MS
2239}
2240
2241/* Pop from the scope of the current namespace. */
e92cc029 2242
a9aedbc2
MS
2243void
2244pop_namespace ()
2245{
2c73f9f5
ML
2246 if (current_namespace == global_namespace)
2247 {
2248 my_friendly_assert (in_std>0, 980421);
2249 in_std--;
2250 return;
2251 }
cb0dbb9a 2252 current_namespace = CP_DECL_CONTEXT (current_namespace);
2c73f9f5
ML
2253 /* The binding level is not popped, as it might be re-opened later. */
2254 suspend_binding_level ();
2255}
a9aedbc2 2256
61fc8c9e
MM
2257/* Push into the scope of the namespace NS, even if it is deeply
2258 nested within another namespace. */
2259
2260void
2261push_nested_namespace (ns)
2262 tree ns;
2263{
2264 if (ns == global_namespace)
2265 push_to_top_level ();
2266 else
2267 {
2268 push_nested_namespace (CP_DECL_CONTEXT (ns));
2269 push_namespace (DECL_NAME (ns));
2270 }
2271}
2272
2273/* Pop back from the scope of the namespace NS, which was previously
2274 entered with push_nested_namespace. */
2275
2276void
2277pop_nested_namespace (ns)
2278 tree ns;
2279{
2280 while (ns != global_namespace)
2281 {
2282 pop_namespace ();
2283 ns = CP_DECL_CONTEXT (ns);
2284 }
2285
2286 pop_from_top_level ();
2287}
2288
8d08fdba
MS
2289\f
2290/* Subroutines for reverting temporarily to top-level for instantiation
2291 of templates and such. We actually need to clear out the class- and
2292 local-value slots of all identifiers, so that only the global values
2293 are at all visible. Simply setting current_binding_level to the global
2294 scope isn't enough, because more binding levels may be pushed. */
9cd64686 2295struct saved_scope *scope_chain;
8d08fdba 2296
9cd64686 2297/* Mark ARG (which is really a struct saved_scope **) for GC. */
fc6af6e3
RH
2298
2299static void
2300mark_saved_scope (arg)
2301 void *arg;
2302{
2303 struct saved_scope *t = *(struct saved_scope **)arg;
2304 while (t)
2305 {
fc6af6e3
RH
2306 mark_binding_level (&t->class_bindings);
2307 ggc_mark_tree (t->old_bindings);
2308 ggc_mark_tree (t->old_namespace);
2309 ggc_mark_tree (t->class_name);
2310 ggc_mark_tree (t->class_type);
2311 ggc_mark_tree (t->access_specifier);
a8f73d4b 2312 ggc_mark_tree (t->function_decl);
fc6af6e3 2313 if (t->lang_base)
9cd64686 2314 ggc_mark_tree_varray (t->lang_base);
fc6af6e3 2315 ggc_mark_tree (t->lang_name);
9cd64686 2316 ggc_mark_tree (t->x_function_parms);
fc6af6e3 2317 ggc_mark_tree (t->template_parms);
9cd64686
MM
2318 ggc_mark_tree (t->x_previous_class_type);
2319 ggc_mark_tree (t->x_previous_class_values);
a8f73d4b 2320 mark_binding_level (&t->bindings);
fc6af6e3
RH
2321 t = t->prev;
2322 }
2323}
2324
e92cc029 2325static tree
45537677
MS
2326store_bindings (names, old_bindings)
2327 tree names, old_bindings;
2328{
2329 tree t;
2330 for (t = names; t; t = TREE_CHAIN (t))
2331 {
2332 tree binding, t1, id;
2333
2334 if (TREE_CODE (t) == TREE_LIST)
2335 id = TREE_PURPOSE (t);
2336 else
2337 id = DECL_NAME (t);
2338
f181d4ae
MM
2339 if (!id
2340 /* Note that we may have an IDENTIFIER_CLASS_VALUE even when
2341 we have no IDENTIFIER_BINDING if we have left the class
2342 scope, but cached the class-level declarations. */
2343 || !(IDENTIFIER_BINDING (id) || IDENTIFIER_CLASS_VALUE (id)))
45537677
MS
2344 continue;
2345
2346 for (t1 = old_bindings; t1; t1 = TREE_CHAIN (t1))
2347 if (TREE_VEC_ELT (t1, 0) == id)
2348 goto skip_it;
78957a2a 2349
87e3dbc9 2350 binding = make_tree_vec (4);
78957a2a 2351
45537677
MS
2352 if (id)
2353 {
2354 my_friendly_assert (TREE_CODE (id) == IDENTIFIER_NODE, 135);
2355 TREE_VEC_ELT (binding, 0) = id;
2c73f9f5 2356 TREE_VEC_ELT (binding, 1) = REAL_IDENTIFIER_TYPE_VALUE (id);
f181d4ae 2357 TREE_VEC_ELT (binding, 2) = IDENTIFIER_BINDING (id);
45537677 2358 TREE_VEC_ELT (binding, 3) = IDENTIFIER_CLASS_VALUE (id);
f181d4ae 2359 IDENTIFIER_BINDING (id) = NULL_TREE;
45537677
MS
2360 IDENTIFIER_CLASS_VALUE (id) = NULL_TREE;
2361 }
2362 TREE_CHAIN (binding) = old_bindings;
2363 old_bindings = binding;
2364 skip_it:
2365 ;
2366 }
2367 return old_bindings;
2368}
2369
8d08fdba 2370void
5566b478
MS
2371maybe_push_to_top_level (pseudo)
2372 int pseudo;
8d08fdba 2373{
a8f73d4b 2374 struct saved_scope *s;
9cd64686 2375 struct binding_level *b;
a8f73d4b
MM
2376 tree old_bindings;
2377 int need_pop;
2378
2379 s = (struct saved_scope *) xcalloc (1, sizeof (struct saved_scope));
8d08fdba 2380
9cd64686
MM
2381 b = scope_chain ? current_binding_level : 0;
2382
a8f73d4b
MM
2383 /* If we're in the middle of some function, save our state. */
2384 if (current_function)
2385 {
2386 need_pop = 1;
2387 push_function_context_to (NULL_TREE);
2388 }
2389 else
2390 need_pop = 0;
e349ee73 2391
a8f73d4b 2392 old_bindings = NULL_TREE;
9cd64686 2393 if (scope_chain && previous_class_type)
a50f0918
MS
2394 old_bindings = store_bindings (previous_class_values, old_bindings);
2395
8d08fdba
MS
2396 /* Have to include global_binding_level, because class-level decls
2397 aren't listed anywhere useful. */
2398 for (; b; b = b->level_chain)
2399 {
2400 tree t;
2401
2c73f9f5
ML
2402 /* Template IDs are inserted into the global level. If they were
2403 inserted into namespace level, finish_file wouldn't find them
2404 when doing pending instantiations. Therefore, don't stop at
2405 namespace level, but continue until :: . */
5566b478
MS
2406 if (b == global_binding_level || (pseudo && b->pseudo_global))
2407 break;
8d08fdba 2408
45537677 2409 old_bindings = store_bindings (b->names, old_bindings);
cffa8729 2410 /* We also need to check class_shadowed to save class-level type
45537677
MS
2411 bindings, since pushclass doesn't fill in b->names. */
2412 if (b->parm_flag == 2)
cffa8729 2413 old_bindings = store_bindings (b->class_shadowed, old_bindings);
8d08fdba 2414
8d08fdba
MS
2415 /* Unwind type-value slots back to top level. */
2416 for (t = b->type_shadowed; t; t = TREE_CHAIN (t))
2417 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (t), TREE_VALUE (t));
2418 }
fc0e7bf5
MM
2419 s->prev = scope_chain;
2420 s->old_bindings = old_bindings;
a8f73d4b
MM
2421 s->bindings = b;
2422 s->need_pop_function_context = need_pop;
2423 s->function_decl = current_function_decl;
fc0e7bf5
MM
2424
2425 scope_chain = s;
8d08fdba 2426 current_function_decl = NULL_TREE;
9cd64686
MM
2427 VARRAY_TREE_INIT (current_lang_base, 10, "current_lang_base");
2428 current_lang_stack = &VARRAY_TREE (current_lang_base, 0);
51c184be
MS
2429 current_lang_name = lang_name_cplusplus;
2430 strict_prototype = strict_prototypes_lang_cplusplus;
2c73f9f5 2431 current_namespace = global_namespace;
8d08fdba 2432
5566b478
MS
2433 push_obstacks (&permanent_obstack, &permanent_obstack);
2434}
2435
2436void
2437push_to_top_level ()
2438{
2439 maybe_push_to_top_level (0);
8d08fdba
MS
2440}
2441
2442void
2443pop_from_top_level ()
2444{
9cd64686 2445 struct saved_scope *s = scope_chain;
8d08fdba
MS
2446 tree t;
2447
e76a2646 2448 /* Clear out class-level bindings cache. */
8d08fdba 2449 if (previous_class_type)
8f032717 2450 invalidate_class_lookup_cache ();
8d08fdba 2451
5566b478
MS
2452 pop_obstacks ();
2453
9cd64686
MM
2454 VARRAY_FREE (current_lang_base);
2455
2456 scope_chain = s->prev;
87e3dbc9 2457 for (t = s->old_bindings; t; t = TREE_CHAIN (t))
8d08fdba
MS
2458 {
2459 tree id = TREE_VEC_ELT (t, 0);
2460 if (id)
2461 {
2c73f9f5 2462 SET_IDENTIFIER_TYPE_VALUE (id, TREE_VEC_ELT (t, 1));
f181d4ae 2463 IDENTIFIER_BINDING (id) = TREE_VEC_ELT (t, 2);
8d08fdba 2464 IDENTIFIER_CLASS_VALUE (id) = TREE_VEC_ELT (t, 3);
9cd64686 2465 }
8d08fdba 2466 }
9cd64686 2467
51c184be
MS
2468 if (current_lang_name == lang_name_cplusplus)
2469 strict_prototype = strict_prototypes_lang_cplusplus;
2470 else if (current_lang_name == lang_name_c)
2471 strict_prototype = strict_prototypes_lang_c;
51c184be 2472
a8f73d4b
MM
2473 /* If we were in the middle of compiling a function, restore our
2474 state. */
2475 if (s->need_pop_function_context)
2476 pop_function_context_from (NULL_TREE);
2477 current_function_decl = s->function_decl;
e349ee73 2478
a8f73d4b 2479 free (s);
8d08fdba
MS
2480}
2481\f
2482/* Push a definition of struct, union or enum tag "name".
2483 into binding_level "b". "type" should be the type node,
2484 We assume that the tag "name" is not already defined.
2485
2486 Note that the definition may really be just a forward reference.
2487 In that case, the TYPE_SIZE will be a NULL_TREE.
2488
e92cc029 2489 C++ gratuitously puts all these tags in the name space. */
8d08fdba
MS
2490
2491/* When setting the IDENTIFIER_TYPE_VALUE field of an identifier ID,
2492 record the shadowed value for this binding contour. TYPE is
2493 the type that ID maps to. */
2494
2495static void
2496set_identifier_type_value_with_scope (id, type, b)
2497 tree id;
2498 tree type;
2499 struct binding_level *b;
2500{
2c73f9f5 2501 if (!b->namespace_p)
8d08fdba 2502 {
2c73f9f5
ML
2503 /* Shadow the marker, not the real thing, so that the marker
2504 gets restored later. */
2505 tree old_type_value = REAL_IDENTIFIER_TYPE_VALUE (id);
8d08fdba
MS
2506 b->type_shadowed
2507 = tree_cons (id, old_type_value, b->type_shadowed);
2508 }
2c73f9f5
ML
2509 else
2510 {
2511 tree binding = binding_for_name (id, current_namespace);
2512 BINDING_TYPE (binding) = type;
2513 /* Store marker instead of real type. */
2514 type = global_type_node;
2515 }
8d08fdba
MS
2516 SET_IDENTIFIER_TYPE_VALUE (id, type);
2517}
2518
8f032717 2519/* As set_identifier_type_value_with_scope, but using current_binding_level. */
8d08fdba
MS
2520
2521void
2522set_identifier_type_value (id, type)
2523 tree id;
2524 tree type;
2525{
8f032717 2526 set_identifier_type_value_with_scope (id, type, current_binding_level);
8d08fdba
MS
2527}
2528
2c73f9f5
ML
2529/* Return the type associated with id. */
2530
2531tree
2532identifier_type_value (id)
2533 tree id;
2534{
2535 /* There is no type with that name, anywhere. */
2536 if (REAL_IDENTIFIER_TYPE_VALUE (id) == NULL_TREE)
2537 return NULL_TREE;
2538 /* This is not the type marker, but the real thing. */
2539 if (REAL_IDENTIFIER_TYPE_VALUE (id) != global_type_node)
2540 return REAL_IDENTIFIER_TYPE_VALUE (id);
2541 /* Have to search for it. It must be on the global level, now.
2542 Ask lookup_name not to return non-types. */
3e3f722c 2543 id = lookup_name_real (id, 2, 1, 0);
2c73f9f5
ML
2544 if (id)
2545 return TREE_TYPE (id);
2546 return NULL_TREE;
2547}
2548
a9aedbc2
MS
2549/* Pop off extraneous binding levels left over due to syntax errors.
2550
2551 We don't pop past namespaces, as they might be valid. */
e92cc029 2552
8926095f
MS
2553void
2554pop_everything ()
2555{
2556#ifdef DEBUG_CP_BINDING_LEVELS
2557 fprintf (stderr, "XXX entering pop_everything ()\n");
2558#endif
8f032717 2559 while (!toplevel_bindings_p ())
8926095f 2560 {
8f032717 2561 if (current_binding_level->parm_flag == 2)
b74a0560 2562 pop_nested_class ();
8926095f
MS
2563 else
2564 poplevel (0, 0, 0);
2565 }
2566#ifdef DEBUG_CP_BINDING_LEVELS
2567 fprintf (stderr, "XXX leaving pop_everything ()\n");
2568#endif
2569}
2570
39c01e4c
MM
2571/* The type TYPE is being declared. If it is a class template, or a
2572 specialization of a class template, do any processing required and
2573 perform error-checking. If IS_FRIEND is non-zero, this TYPE is
2574 being declared a friend. B is the binding level at which this TYPE
2575 should be bound.
2576
2577 Returns the TYPE_DECL for TYPE, which may have been altered by this
2578 processing. */
2579
2580static tree
2581maybe_process_template_type_declaration (type, globalize, b)
2582 tree type;
2583 int globalize;
2584 struct binding_level* b;
2585{
2586 tree decl = TYPE_NAME (type);
2587
2588 if (processing_template_parmlist)
2589 /* You can't declare a new template type in a template parameter
2590 list. But, you can declare a non-template type:
2591
2592 template <class A*> struct S;
2593
2594 is a forward-declaration of `A'. */
2595 ;
2596 else
2597 {
2598 maybe_check_template_type (type);
2599
ed44da02
MM
2600 my_friendly_assert (IS_AGGR_TYPE (type)
2601 || TREE_CODE (type) == ENUMERAL_TYPE, 0);
2602
2603
3ebc5c52 2604 if (processing_template_decl)
39c01e4c
MM
2605 {
2606 /* This may change after the call to
2607 push_template_decl_real, but we want the original value. */
2608 tree name = DECL_NAME (decl);
2609
2610 decl = push_template_decl_real (decl, globalize);
2611 /* If the current binding level is the binding level for the
2612 template parameters (see the comment in
2613 begin_template_parm_list) and the enclosing level is a class
2614 scope, and we're not looking at a friend, push the
2615 declaration of the member class into the class scope. In the
2616 friend case, push_template_decl will already have put the
2617 friend into global scope, if appropriate. */
ed44da02
MM
2618 if (TREE_CODE (type) != ENUMERAL_TYPE
2619 && !globalize && b->pseudo_global
39c01e4c
MM
2620 && b->level_chain->parm_flag == 2)
2621 {
61a127b3 2622 finish_member_declaration (CLASSTYPE_TI_TEMPLATE (type));
39c01e4c
MM
2623 /* Put this tag on the list of tags for the class, since
2624 that won't happen below because B is not the class
2625 binding level, but is instead the pseudo-global level. */
2626 b->level_chain->tags =
e1b3e07d 2627 tree_cons (name, type, b->level_chain->tags);
39c01e4c
MM
2628 if (TYPE_SIZE (current_class_type) == NULL_TREE)
2629 CLASSTYPE_TAGS (current_class_type) = b->level_chain->tags;
2630 }
2631 }
2632 }
2633
2634 return decl;
2635}
2636
9188c363
MM
2637/* In C++, you don't have to write `struct S' to refer to `S'; you
2638 can just use `S'. We accomplish this by creating a TYPE_DECL as
2639 if the user had written `typedef struct S S'. Create and return
2640 the TYPE_DECL for TYPE. */
2641
2642tree
2643create_implicit_typedef (name, type)
2644 tree name;
2645 tree type;
2646{
2647 tree decl;
2648
2649 decl = build_decl (TYPE_DECL, name, type);
2650 SET_DECL_ARTIFICIAL (decl);
2651 /* There are other implicit type declarations, like the one *within*
2652 a class that allows you to write `S::S'. We must distinguish
2653 amongst these. */
2654 SET_DECL_IMPLICIT_TYPEDEF_P (decl);
2655 TYPE_NAME (type) = decl;
2656
2657 return decl;
2658}
2659
8d08fdba 2660/* Push a tag name NAME for struct/class/union/enum type TYPE.
6757edfe 2661 Normally put it into the inner-most non-tag-transparent scope,
8d08fdba 2662 but if GLOBALIZE is true, put it in the inner-most non-class scope.
e92cc029 2663 The latter is needed for implicit declarations. */
8d08fdba
MS
2664
2665void
2666pushtag (name, type, globalize)
2667 tree name, type;
2668 int globalize;
2669{
2670 register struct binding_level *b;
8d08fdba 2671
8f032717 2672 b = current_binding_level;
8d08fdba
MS
2673 while (b->tag_transparent
2674 || (globalize && b->parm_flag == 2))
2675 b = b->level_chain;
2676
e1b3e07d 2677 b->tags = tree_cons (name, type, b->tags);
8d08fdba
MS
2678
2679 if (name)
2680 {
8d08fdba 2681 /* Do C++ gratuitous typedefing. */
db5ae43f 2682 if (IDENTIFIER_TYPE_VALUE (name) != type)
8d08fdba 2683 {
93cdc044 2684 register tree d = NULL_TREE;
848cf1e0 2685 int in_class = 0;
9188c363 2686 tree context = TYPE_CONTEXT (type);
280f9385 2687
280f9385
MM
2688 if (! context)
2689 {
2690 tree cs = current_scope ();
2691
2692 if (! globalize)
2693 context = cs;
2694 else if (cs != NULL_TREE
2695 && TREE_CODE_CLASS (TREE_CODE (cs)) == 't')
2696 /* When declaring a friend class of a local class, we want
2697 to inject the newly named class into the scope
2698 containing the local class, not the namespace scope. */
2699 context = hack_decl_function_context (get_type_decl (cs));
2700 }
280f9385
MM
2701 if (!context)
2702 context = current_namespace;
8d08fdba 2703
93cdc044
JM
2704 if ((b->pseudo_global && b->level_chain->parm_flag == 2)
2705 || b->parm_flag == 2)
2706 in_class = 1;
93cdc044 2707
848cf1e0
MM
2708 if (current_lang_name == lang_name_java)
2709 TYPE_FOR_JAVA (type) = 1;
93cdc044 2710
9188c363 2711 d = create_implicit_typedef (name, type);
cb0dbb9a 2712 DECL_CONTEXT (d) = FROB_CONTEXT (context);
9188c363
MM
2713 if (! in_class)
2714 set_identifier_type_value_with_scope (name, type, b);
e1cd6e56 2715
39c01e4c
MM
2716 d = maybe_process_template_type_declaration (type,
2717 globalize, b);
93cdc044
JM
2718
2719 if (b->parm_flag == 2)
61a127b3 2720 {
848cf1e0 2721 if (!PROCESSING_REAL_TEMPLATE_DECL_P ())
61a127b3
MM
2722 /* Put this TYPE_DECL on the TYPE_FIELDS list for the
2723 class. But if it's a member template class, we
2724 want the TEMPLATE_DECL, not the TYPE_DECL, so this
2725 is done later. */
2726 finish_member_declaration (d);
8f032717
MM
2727 else
2728 pushdecl_class_level (d);
61a127b3 2729 }
93cdc044
JM
2730 else
2731 d = pushdecl_with_scope (d, b);
2732
848cf1e0
MM
2733 if (ANON_AGGRNAME_P (name))
2734 DECL_IGNORED_P (d) = 1;
2735
2736 TYPE_CONTEXT (type) = DECL_CONTEXT (d);
2737 DECL_ASSEMBLER_NAME (d) = DECL_NAME (d);
2738 if (!uses_template_parms (type))
2739 DECL_ASSEMBLER_NAME (d)
2740 = get_identifier (build_overload_name (type, 1, 1));
8d08fdba
MS
2741 }
2742 if (b->parm_flag == 2)
2743 {
8d08fdba
MS
2744 if (TYPE_SIZE (current_class_type) == NULL_TREE)
2745 CLASSTYPE_TAGS (current_class_type) = b->tags;
2746 }
2747 }
2748
2749 if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL)
2750 /* Use the canonical TYPE_DECL for this node. */
2751 TYPE_STUB_DECL (type) = TYPE_NAME (type);
2752 else
2753 {
2754 /* Create a fake NULL-named TYPE_DECL node whose TREE_TYPE
2755 will be the tagged type we just added to the current
2756 binding level. This fake NULL-named TYPE_DECL node helps
2757 dwarfout.c to know when it needs to output a
2758 representation of a tagged type, and it also gives us a
2759 convenient place to record the "scope start" address for
2760 the tagged type. */
2761
8d08fdba 2762 tree d = build_decl (TYPE_DECL, NULL_TREE, type);
8d08fdba
MS
2763 TYPE_STUB_DECL (type) = pushdecl_with_scope (d, b);
2764 }
2765}
2766
2767/* Counter used to create anonymous type names. */
e92cc029 2768
8d08fdba
MS
2769static int anon_cnt = 0;
2770
2771/* Return an IDENTIFIER which can be used as a name for
2772 anonymous structs and unions. */
e92cc029 2773
8d08fdba
MS
2774tree
2775make_anon_name ()
2776{
2777 char buf[32];
2778
2779 sprintf (buf, ANON_AGGRNAME_FORMAT, anon_cnt++);
2780 return get_identifier (buf);
2781}
2782
2783/* Clear the TREE_PURPOSE slot of tags which have anonymous typenames.
2784 This keeps dbxout from getting confused. */
e92cc029 2785
8d08fdba
MS
2786void
2787clear_anon_tags ()
2788{
2789 register struct binding_level *b;
2790 register tree tags;
2791 static int last_cnt = 0;
2792
2793 /* Fast out if no new anon names were declared. */
2794 if (last_cnt == anon_cnt)
2795 return;
2796
2797 b = current_binding_level;
2798 while (b->tag_transparent)
2799 b = b->level_chain;
2800 tags = b->tags;
2801 while (tags)
2802 {
2803 /* A NULL purpose means we have already processed all tags
2804 from here to the end of the list. */
2805 if (TREE_PURPOSE (tags) == NULL_TREE)
2806 break;
2807 if (ANON_AGGRNAME_P (TREE_PURPOSE (tags)))
2808 TREE_PURPOSE (tags) = NULL_TREE;
2809 tags = TREE_CHAIN (tags);
2810 }
2811 last_cnt = anon_cnt;
2812}
2813\f
2814/* Subroutine of duplicate_decls: return truthvalue of whether
2815 or not types of these decls match.
2816
2817 For C++, we must compare the parameter list so that `int' can match
2818 `int&' in a parameter position, but `int&' is not confused with
2819 `const int&'. */
e92cc029 2820
6060a796 2821int
8d08fdba
MS
2822decls_match (newdecl, olddecl)
2823 tree newdecl, olddecl;
2824{
2825 int types_match;
2826
347d73d7
ML
2827 if (newdecl == olddecl)
2828 return 1;
2829
6b4b3deb
MM
2830 if (TREE_CODE (newdecl) != TREE_CODE (olddecl))
2831 /* If the two DECLs are not even the same kind of thing, we're not
2832 interested in their types. */
2833 return 0;
2834
2835 if (TREE_CODE (newdecl) == FUNCTION_DECL)
8d08fdba
MS
2836 {
2837 tree f1 = TREE_TYPE (newdecl);
2838 tree f2 = TREE_TYPE (olddecl);
2839 tree p1 = TYPE_ARG_TYPES (f1);
2840 tree p2 = TYPE_ARG_TYPES (f2);
2841
c5a6fc45
JM
2842 if (DECL_REAL_CONTEXT (newdecl) != DECL_REAL_CONTEXT (olddecl)
2843 && ! (DECL_LANGUAGE (newdecl) == lang_c
2844 && DECL_LANGUAGE (olddecl) == lang_c))
2845 return 0;
2846
8d08fdba
MS
2847 /* When we parse a static member function definition,
2848 we put together a FUNCTION_DECL which thinks its type
2849 is METHOD_TYPE. Change that to FUNCTION_TYPE, and
2850 proceed. */
2851 if (TREE_CODE (f1) == METHOD_TYPE && DECL_STATIC_FUNCTION_P (olddecl))
700f8a87 2852 revert_static_member_fn (&newdecl, &f1, &p1);
8d08fdba
MS
2853 else if (TREE_CODE (f2) == METHOD_TYPE
2854 && DECL_STATIC_FUNCTION_P (newdecl))
700f8a87 2855 revert_static_member_fn (&olddecl, &f2, &p2);
8d08fdba
MS
2856
2857 /* Here we must take care of the case where new default
2858 parameters are specified. Also, warn if an old
2859 declaration becomes ambiguous because default
2860 parameters may cause the two to be ambiguous. */
2861 if (TREE_CODE (f1) != TREE_CODE (f2))
2862 {
2863 if (TREE_CODE (f1) == OFFSET_TYPE)
8251199e 2864 cp_compiler_error ("`%D' redeclared as member function", newdecl);
8d08fdba 2865 else
8251199e 2866 cp_compiler_error ("`%D' redeclared as non-member function", newdecl);
8d08fdba
MS
2867 return 0;
2868 }
2869
3bfdc719 2870 if (same_type_p (TREE_TYPE (f1), TREE_TYPE (f2)))
8926095f 2871 {
a28e3c7f 2872 if (! strict_prototypes_lang_c && DECL_LANGUAGE (olddecl) == lang_c
8926095f 2873 && p2 == NULL_TREE)
a28e3c7f
MS
2874 {
2875 types_match = self_promoting_args_p (p1);
2876 if (p1 == void_list_node)
2877 TREE_TYPE (newdecl) = TREE_TYPE (olddecl);
2878 }
2879 else if (!strict_prototypes_lang_c && DECL_LANGUAGE (olddecl)==lang_c
2880 && DECL_LANGUAGE (newdecl) == lang_c && p1 == NULL_TREE)
2881 {
2882 types_match = self_promoting_args_p (p2);
2883 TREE_TYPE (newdecl) = TREE_TYPE (olddecl);
2884 }
8926095f 2885 else
91063b51 2886 types_match = compparms (p1, p2);
8926095f 2887 }
8d08fdba
MS
2888 else
2889 types_match = 0;
2890 }
6b4b3deb 2891 else if (TREE_CODE (newdecl) == TEMPLATE_DECL)
51c184be 2892 {
f84b4be9
JM
2893 if (!comp_template_parms (DECL_TEMPLATE_PARMS (newdecl),
2894 DECL_TEMPLATE_PARMS (olddecl)))
2895 return 0;
2896
2897 if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL)
2898 types_match = 1;
2899 else
2900 types_match = decls_match (DECL_TEMPLATE_RESULT (olddecl),
2901 DECL_TEMPLATE_RESULT (newdecl));
51c184be 2902 }
8d08fdba
MS
2903 else
2904 {
2905 if (TREE_TYPE (newdecl) == error_mark_node)
2906 types_match = TREE_TYPE (olddecl) == error_mark_node;
2907 else if (TREE_TYPE (olddecl) == NULL_TREE)
2908 types_match = TREE_TYPE (newdecl) == NULL_TREE;
a0a33927
MS
2909 else if (TREE_TYPE (newdecl) == NULL_TREE)
2910 types_match = 0;
8d08fdba 2911 else
01240200 2912 types_match = comptypes (TREE_TYPE (newdecl),
3bfdc719
MM
2913 TREE_TYPE (olddecl),
2914 COMPARE_REDECLARATION);
8d08fdba
MS
2915 }
2916
2917 return types_match;
2918}
2919
2920/* If NEWDECL is `static' and an `extern' was seen previously,
24f30ed4 2921 warn about it. OLDDECL is the previous declaration.
8d08fdba
MS
2922
2923 Note that this does not apply to the C++ case of declaring
2924 a variable `extern const' and then later `const'.
2925
8d08fdba
MS
2926 Don't complain about built-in functions, since they are beyond
2927 the user's control. */
2928
2929static void
2930warn_extern_redeclared_static (newdecl, olddecl)
2931 tree newdecl, olddecl;
2932{
d8e178a0 2933 static const char *explicit_extern_static_warning
8251199e 2934 = "`%D' was declared `extern' and later `static'";
d8e178a0 2935 static const char *implicit_extern_static_warning
8251199e
JM
2936 = "`%D' was declared implicitly `extern' and later `static'";
2937
24f30ed4
MM
2938 tree name;
2939
d22c8596 2940 if (TREE_CODE (newdecl) == TYPE_DECL)
8d08fdba 2941 return;
963d5758
MM
2942
2943 /* Don't get confused by static member functions; that's a different
2944 use of `static'. */
2945 if (TREE_CODE (newdecl) == FUNCTION_DECL
2946 && DECL_STATIC_FUNCTION_P (newdecl))
2947 return;
8d08fdba 2948
24f30ed4
MM
2949 /* If the old declaration was `static', or the new one isn't, then
2950 then everything is OK. */
2951 if (DECL_THIS_STATIC (olddecl) || !DECL_THIS_STATIC (newdecl))
2952 return;
2953
2954 /* It's OK to declare a builtin function as `static'. */
2955 if (TREE_CODE (olddecl) == FUNCTION_DECL
2956 && DECL_ARTIFICIAL (olddecl))
2957 return;
2958
8d08fdba 2959 name = DECL_ASSEMBLER_NAME (newdecl);
24f30ed4
MM
2960 cp_pedwarn (IDENTIFIER_IMPLICIT_DECL (name)
2961 ? implicit_extern_static_warning
2962 : explicit_extern_static_warning, newdecl);
2963 cp_pedwarn_at ("previous declaration of `%D'", olddecl);
8d08fdba
MS
2964}
2965
2966/* Handle when a new declaration NEWDECL has the same name as an old
2967 one OLDDECL in the same binding contour. Prints an error message
2968 if appropriate.
2969
2970 If safely possible, alter OLDDECL to look like NEWDECL, and return 1.
2971 Otherwise, return 0. */
2972
51c184be 2973int
8d08fdba 2974duplicate_decls (newdecl, olddecl)
824b9a4c 2975 tree newdecl, olddecl;
8d08fdba 2976{
8d08fdba
MS
2977 unsigned olddecl_uid = DECL_UID (olddecl);
2978 int olddecl_friend = 0, types_match = 0;
0b60dfe3 2979 int new_defines_function = 0;
5566b478
MS
2980
2981 if (newdecl == olddecl)
2982 return 1;
8d08fdba 2983
8926095f 2984 types_match = decls_match (newdecl, olddecl);
8d08fdba 2985
8d08fdba
MS
2986 /* If either the type of the new decl or the type of the old decl is an
2987 error_mark_node, then that implies that we have already issued an
2988 error (earlier) for some bogus type specification, and in that case,
2989 it is rather pointless to harass the user with yet more error message
0b60dfe3 2990 about the same declaration, so just pretend the types match here. */
bd6dd845
MS
2991 if (TREE_TYPE (newdecl) == error_mark_node
2992 || TREE_TYPE (olddecl) == error_mark_node)
8d08fdba 2993 types_match = 1;
0b60dfe3
BK
2994
2995 /* Check for redeclaration and other discrepancies. */
d22c8596 2996 if (TREE_CODE (olddecl) == FUNCTION_DECL
9f33663b
JM
2997 && DECL_ARTIFICIAL (olddecl))
2998 {
2999 if (TREE_CODE (newdecl) != FUNCTION_DECL)
3000 {
3001 /* If you declare a built-in or predefined function name as static,
3002 the old definition is overridden, but optionally warn this was a
3003 bad choice of name. */
3004 if (! TREE_PUBLIC (newdecl))
3005 {
3006 if (warn_shadow)
3007 cp_warning ("shadowing %s function `%#D'",
3008 DECL_BUILT_IN (olddecl) ? "built-in" : "library",
3009 olddecl);
3010 /* Discard the old built-in function. */
3011 return 0;
3012 }
3013 /* If the built-in is not ansi, then programs can override
3014 it even globally without an error. */
3015 else if (! DECL_BUILT_IN (olddecl))
3016 cp_warning ("library function `%#D' redeclared as non-function `%#D'",
3017 olddecl, newdecl);
3018 else
3019 {
3020 cp_error ("declaration of `%#D'", newdecl);
3021 cp_error ("conflicts with built-in declaration `%#D'",
a4443a08 3022 olddecl);
9f33663b 3023 }
a4443a08
MS
3024 return 0;
3025 }
9f33663b 3026 else if (!types_match)
8d08fdba 3027 {
9f33663b
JM
3028 if ((DECL_LANGUAGE (newdecl) == lang_c
3029 && DECL_LANGUAGE (olddecl) == lang_c)
3030 || compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
3031 TYPE_ARG_TYPES (TREE_TYPE (olddecl))))
a4443a08 3032 {
9f33663b
JM
3033 /* A near match; override the builtin. */
3034
3035 if (TREE_PUBLIC (newdecl))
a4443a08 3036 {
9f33663b
JM
3037 cp_warning ("new declaration `%#D'", newdecl);
3038 cp_warning ("ambiguates built-in declaration `%#D'",
3039 olddecl);
a4443a08 3040 }
9f33663b
JM
3041 else if (warn_shadow)
3042 cp_warning ("shadowing %s function `%#D'",
3043 DECL_BUILT_IN (olddecl) ? "built-in" : "library",
3044 olddecl);
a4443a08 3045 }
9f33663b
JM
3046 else
3047 /* Discard the old built-in function. */
3048 return 0;
8d08fdba 3049 }
24f30ed4
MM
3050
3051 if (DECL_THIS_STATIC (newdecl) && !DECL_THIS_STATIC (olddecl))
3052 {
3053 /* If a builtin function is redeclared as `static', merge
3054 the declarations, but make the original one static. */
3055 DECL_THIS_STATIC (olddecl) = 1;
3056 TREE_PUBLIC (olddecl) = 0;
9bfadf57
MM
3057
3058 /* Make the olddeclaration consistent with the new one so that
3059 all remnants of the builtin-ness of this function will be
3060 banished. */
3061 DECL_LANGUAGE (olddecl) = DECL_LANGUAGE (newdecl);
3062 DECL_RTL (olddecl) = DECL_RTL (newdecl);
3063 DECL_ASSEMBLER_NAME (olddecl) = DECL_ASSEMBLER_NAME (newdecl);
3064 SET_IDENTIFIER_GLOBAL_VALUE (DECL_ASSEMBLER_NAME (newdecl),
3065 newdecl);
24f30ed4 3066 }
39211cd5
MS
3067 }
3068 else if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
3069 {
9ed182dc
JM
3070 if ((TREE_CODE (olddecl) == TYPE_DECL && DECL_ARTIFICIAL (olddecl)
3071 && TREE_CODE (newdecl) != TYPE_DECL
3072 && ! (TREE_CODE (newdecl) == TEMPLATE_DECL
3073 && TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL))
3074 || (TREE_CODE (newdecl) == TYPE_DECL && DECL_ARTIFICIAL (newdecl)
3075 && TREE_CODE (olddecl) != TYPE_DECL
3076 && ! (TREE_CODE (olddecl) == TEMPLATE_DECL
3077 && (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl))
3078 == TYPE_DECL))))
3079 {
3080 /* We do nothing special here, because C++ does such nasty
3081 things with TYPE_DECLs. Instead, just let the TYPE_DECL
3082 get shadowed, and know that if we need to find a TYPE_DECL
3083 for a given name, we can look in the IDENTIFIER_TYPE_VALUE
3084 slot of the identifier. */
3085 return 0;
3086 }
3087
39211cd5 3088 if ((TREE_CODE (newdecl) == FUNCTION_DECL
5566b478 3089 && DECL_FUNCTION_TEMPLATE_P (olddecl))
39211cd5 3090 || (TREE_CODE (olddecl) == FUNCTION_DECL
5566b478 3091 && DECL_FUNCTION_TEMPLATE_P (newdecl)))
39211cd5 3092 return 0;
9ed182dc 3093
8251199e 3094 cp_error ("`%#D' redeclared as different kind of symbol", newdecl);
39211cd5
MS
3095 if (TREE_CODE (olddecl) == TREE_LIST)
3096 olddecl = TREE_VALUE (olddecl);
8251199e 3097 cp_error_at ("previous declaration of `%#D'", olddecl);
39211cd5
MS
3098
3099 /* New decl is completely inconsistent with the old one =>
3100 tell caller to replace the old one. */
3101
3102 return 0;
8d08fdba 3103 }
8d08fdba
MS
3104 else if (!types_match)
3105 {
7bdbfa05
MM
3106 if (DECL_REAL_CONTEXT (newdecl) != DECL_REAL_CONTEXT (olddecl))
3107 /* These are certainly not duplicate declarations; they're
3108 from different scopes. */
3109 return 0;
3110
8926095f 3111 if (TREE_CODE (newdecl) == TEMPLATE_DECL)
f0e01782
MS
3112 {
3113 /* The name of a class template may not be declared to refer to
3114 any other template, class, function, object, namespace, value,
e92cc029 3115 or type in the same scope. */
5566b478
MS
3116 if (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) == TYPE_DECL
3117 || TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL)
f0e01782 3118 {
8251199e
JM
3119 cp_error ("declaration of template `%#D'", newdecl);
3120 cp_error_at ("conflicts with previous declaration `%#D'",
f0e01782
MS
3121 olddecl);
3122 }
ec255269
MS
3123 else if (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) == FUNCTION_DECL
3124 && TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == FUNCTION_DECL
3125 && compparms (TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (olddecl))),
91063b51 3126 TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (newdecl))))
75650646
MM
3127 && comp_template_parms (DECL_TEMPLATE_PARMS (newdecl),
3128 DECL_TEMPLATE_PARMS (olddecl)))
ec255269 3129 {
8251199e
JM
3130 cp_error ("new declaration `%#D'", newdecl);
3131 cp_error_at ("ambiguates old declaration `%#D'", olddecl);
ec255269 3132 }
f0e01782
MS
3133 return 0;
3134 }
8926095f
MS
3135 if (TREE_CODE (newdecl) == FUNCTION_DECL)
3136 {
3137 if (DECL_LANGUAGE (newdecl) == lang_c
3138 && DECL_LANGUAGE (olddecl) == lang_c)
3139 {
8251199e 3140 cp_error ("declaration of C function `%#D' conflicts with",
8926095f 3141 newdecl);
8251199e 3142 cp_error_at ("previous declaration `%#D' here", olddecl);
8926095f 3143 }
00595019 3144 else if (compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
91063b51 3145 TYPE_ARG_TYPES (TREE_TYPE (olddecl))))
700f8a87 3146 {
8251199e
JM
3147 cp_error ("new declaration `%#D'", newdecl);
3148 cp_error_at ("ambiguates old declaration `%#D'", olddecl);
700f8a87
MS
3149 }
3150 else
3151 return 0;
8926095f 3152 }
8d08fdba
MS
3153
3154 /* Already complained about this, so don't do so again. */
a4443a08 3155 else if (current_class_type == NULL_TREE
8d08fdba
MS
3156 || IDENTIFIER_ERROR_LOCUS (DECL_ASSEMBLER_NAME (newdecl)) != current_class_type)
3157 {
8251199e
JM
3158 cp_error ("conflicting types for `%#D'", newdecl);
3159 cp_error_at ("previous declaration as `%#D'", olddecl);
8d08fdba
MS
3160 }
3161 }
75650646
MM
3162 else if (TREE_CODE (newdecl) == FUNCTION_DECL
3163 && ((DECL_TEMPLATE_SPECIALIZATION (olddecl)
3164 && (!DECL_TEMPLATE_INFO (newdecl)
3165 || (DECL_TI_TEMPLATE (newdecl)
3166 != DECL_TI_TEMPLATE (olddecl))))
3167 || (DECL_TEMPLATE_SPECIALIZATION (newdecl)
3168 && (!DECL_TEMPLATE_INFO (olddecl)
3169 || (DECL_TI_TEMPLATE (olddecl)
3170 != DECL_TI_TEMPLATE (newdecl))))))
386b8a85
JM
3171 /* It's OK to have a template specialization and a non-template
3172 with the same type, or to have specializations of two
75650646
MM
3173 different templates with the same type. Note that if one is a
3174 specialization, and the other is an instantiation of the same
3175 template, that we do not exit at this point. That situation
3176 can occur if we instantiate a template class, and then
3177 specialize one of its methods. This situation is legal, but
3178 the declarations must be merged in the usual way. */
3179 return 0;
3180 else if (TREE_CODE (newdecl) == FUNCTION_DECL
3181 && ((DECL_TEMPLATE_INSTANTIATION (olddecl)
3182 && !DECL_USE_TEMPLATE (newdecl))
3183 || (DECL_TEMPLATE_INSTANTIATION (newdecl)
3184 && !DECL_USE_TEMPLATE (olddecl))))
3185 /* One of the declarations is a template instantiation, and the
3186 other is not a template at all. That's OK. */
386b8a85 3187 return 0;
85c6cbaf
ML
3188 else if (TREE_CODE (newdecl) == NAMESPACE_DECL
3189 && DECL_NAMESPACE_ALIAS (newdecl)
3190 && DECL_NAMESPACE_ALIAS (newdecl) == DECL_NAMESPACE_ALIAS (olddecl))
3191 /* Redeclaration of namespace alias, ignore it. */
3192 return 1;
8d08fdba
MS
3193 else
3194 {
d8e178a0 3195 const char *errmsg = redeclaration_error_message (newdecl, olddecl);
8251199e 3196 if (errmsg)
8d08fdba 3197 {
8251199e 3198 cp_error (errmsg, newdecl);
8d08fdba
MS
3199 if (DECL_NAME (olddecl) != NULL_TREE)
3200 cp_error_at ((DECL_INITIAL (olddecl)
2c73f9f5 3201 && namespace_bindings_p ())
8251199e
JM
3202 ? "`%#D' previously defined here"
3203 : "`%#D' previously declared here", olddecl);
8d08fdba
MS
3204 }
3205 else if (TREE_CODE (olddecl) == FUNCTION_DECL
3206 && DECL_INITIAL (olddecl) != NULL_TREE
3207 && TYPE_ARG_TYPES (TREE_TYPE (olddecl)) == NULL_TREE
3208 && TYPE_ARG_TYPES (TREE_TYPE (newdecl)) != NULL_TREE)
3209 {
3210 /* Prototype decl follows defn w/o prototype. */
8251199e
JM
3211 cp_warning_at ("prototype for `%#D'", newdecl);
3212 cp_warning_at ("follows non-prototype definition here", olddecl);
8d08fdba
MS
3213 }
3214 else if (TREE_CODE (olddecl) == FUNCTION_DECL
3215 && DECL_LANGUAGE (newdecl) != DECL_LANGUAGE (olddecl))
8926095f
MS
3216 {
3217 /* extern "C" int foo ();
3218 int foo () { bar (); }
3219 is OK. */
9cd64686
MM
3220 if (current_lang_stack
3221 == &VARRAY_TREE (current_lang_base, 0))
a28e3c7f 3222 DECL_LANGUAGE (newdecl) = DECL_LANGUAGE (olddecl);
8926095f
MS
3223 else
3224 {
8251199e 3225 cp_error_at ("previous declaration of `%#D' with %L linkage",
8926095f 3226 olddecl, DECL_LANGUAGE (olddecl));
8251199e 3227 cp_error ("conflicts with new declaration with %L linkage",
8926095f
MS
3228 DECL_LANGUAGE (newdecl));
3229 }
3230 }
e1cd6e56 3231
a6f02587 3232 if (DECL_LANG_SPECIFIC (olddecl) && DECL_USE_TEMPLATE (olddecl))
c32381b1
JM
3233 ;
3234 else if (TREE_CODE (olddecl) == FUNCTION_DECL)
e1cd6e56
MS
3235 {
3236 tree t1 = TYPE_ARG_TYPES (TREE_TYPE (olddecl));
3237 tree t2 = TYPE_ARG_TYPES (TREE_TYPE (newdecl));
3238 int i = 1;
3239
3240 if (TREE_CODE (TREE_TYPE (newdecl)) == METHOD_TYPE)
3241 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2);
3242
3243 for (; t1 && t1 != void_list_node;
3244 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2), i++)
3245 if (TREE_PURPOSE (t1) && TREE_PURPOSE (t2))
3246 {
2507f3b5
RK
3247 if (1 == simple_cst_equal (TREE_PURPOSE (t1),
3248 TREE_PURPOSE (t2)))
e1cd6e56
MS
3249 {
3250 if (pedantic)
3251 {
8251199e 3252 cp_pedwarn ("default argument given for parameter %d of `%#D'",
e1cd6e56 3253 i, newdecl);
8251199e 3254 cp_pedwarn_at ("after previous specification in `%#D'",
e1cd6e56
MS
3255 olddecl);
3256 }
3257 }
3258 else
3259 {
8251199e 3260 cp_error ("default argument given for parameter %d of `%#D'",
e1cd6e56 3261 i, newdecl);
8251199e 3262 cp_error_at ("after previous specification in `%#D'",
e1cd6e56
MS
3263 olddecl);
3264 }
3265 }
a5894242 3266
7fcdf4c2
MS
3267 if (DECL_THIS_INLINE (newdecl) && ! DECL_THIS_INLINE (olddecl)
3268 && TREE_ADDRESSABLE (olddecl) && warn_inline)
dff6b454 3269 {
8251199e 3270 cp_warning ("`%#D' was used before it was declared inline",
7fcdf4c2 3271 newdecl);
8251199e 3272 cp_warning_at ("previous non-inline declaration here",
7fcdf4c2 3273 olddecl);
dff6b454 3274 }
e1cd6e56 3275 }
8d08fdba
MS
3276 }
3277
3278 /* If new decl is `static' and an `extern' was seen previously,
3279 warn about it. */
3280 warn_extern_redeclared_static (newdecl, olddecl);
3281
e92cc029 3282 /* We have committed to returning 1 at this point. */
8d08fdba
MS
3283 if (TREE_CODE (newdecl) == FUNCTION_DECL)
3284 {
3285 /* Now that functions must hold information normally held
3286 by field decls, there is extra work to do so that
3287 declaration information does not get destroyed during
3288 definition. */
3289 if (DECL_VINDEX (olddecl))
3290 DECL_VINDEX (newdecl) = DECL_VINDEX (olddecl);
3291 if (DECL_CONTEXT (olddecl))
3292 DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
3293 if (DECL_CLASS_CONTEXT (olddecl))
3294 DECL_CLASS_CONTEXT (newdecl) = DECL_CLASS_CONTEXT (olddecl);
8d08fdba
MS
3295 if (DECL_PENDING_INLINE_INFO (newdecl) == (struct pending_inline *)0)
3296 DECL_PENDING_INLINE_INFO (newdecl) = DECL_PENDING_INLINE_INFO (olddecl);
e1cd6e56
MS
3297 DECL_STATIC_CONSTRUCTOR (newdecl) |= DECL_STATIC_CONSTRUCTOR (olddecl);
3298 DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
7215f9a0 3299 DECL_ABSTRACT_VIRTUAL_P (newdecl) |= DECL_ABSTRACT_VIRTUAL_P (olddecl);
864b83b9 3300 DECL_VIRTUAL_P (newdecl) |= DECL_VIRTUAL_P (olddecl);
4a67c9e9 3301 DECL_NEEDS_FINAL_OVERRIDER_P (newdecl) |= DECL_NEEDS_FINAL_OVERRIDER_P (olddecl);
963d5758 3302 DECL_THIS_STATIC (newdecl) |= DECL_THIS_STATIC (olddecl);
0b60dfe3
BK
3303 new_defines_function = DECL_INITIAL (newdecl) != NULL_TREE;
3304
3305 /* Optionally warn about more than one declaration for the same
3306 name, but don't warn about a function declaration followed by a
3307 definition. */
3308 if (warn_redundant_decls && ! DECL_ARTIFICIAL (olddecl)
3309 && !(new_defines_function && DECL_INITIAL (olddecl) == NULL_TREE)
3310 /* Don't warn about extern decl followed by definition. */
3311 && !(DECL_EXTERNAL (olddecl) && ! DECL_EXTERNAL (newdecl))
3312 /* Don't warn about friends, let add_friend take care of it. */
3313 && ! DECL_FRIEND_P (newdecl))
3314 {
8251199e
JM
3315 cp_warning ("redundant redeclaration of `%D' in same scope", newdecl);
3316 cp_warning_at ("previous declaration of `%D'", olddecl);
0b60dfe3 3317 }
8d08fdba
MS
3318 }
3319
3320 /* Deal with C++: must preserve virtual function table size. */
3321 if (TREE_CODE (olddecl) == TYPE_DECL)
3322 {
3323 register tree newtype = TREE_TYPE (newdecl);
3324 register tree oldtype = TREE_TYPE (olddecl);
3325
3326 if (newtype != error_mark_node && oldtype != error_mark_node
3327 && TYPE_LANG_SPECIFIC (newtype) && TYPE_LANG_SPECIFIC (oldtype))
3328 {
3329 CLASSTYPE_VSIZE (newtype) = CLASSTYPE_VSIZE (oldtype);
3330 CLASSTYPE_FRIEND_CLASSES (newtype)
3331 = CLASSTYPE_FRIEND_CLASSES (oldtype);
3332 }
8d08fdba
MS
3333 }
3334
8d08fdba
MS
3335 /* Copy all the DECL_... slots specified in the new decl
3336 except for any that we copy here from the old type. */
0b60dfe3
BK
3337 DECL_MACHINE_ATTRIBUTES (newdecl)
3338 = merge_machine_decl_attributes (olddecl, newdecl);
8d08fdba 3339
5566b478
MS
3340 if (TREE_CODE (newdecl) == TEMPLATE_DECL)
3341 {
4d85e00e
MM
3342 if (! duplicate_decls (DECL_TEMPLATE_RESULT (newdecl),
3343 DECL_TEMPLATE_RESULT (olddecl)))
8251199e 3344 cp_error ("invalid redeclaration of %D", newdecl);
4d85e00e 3345 TREE_TYPE (olddecl) = TREE_TYPE (DECL_TEMPLATE_RESULT (olddecl));
4d85e00e
MM
3346 DECL_TEMPLATE_SPECIALIZATIONS (olddecl)
3347 = chainon (DECL_TEMPLATE_SPECIALIZATIONS (olddecl),
3348 DECL_TEMPLATE_SPECIALIZATIONS (newdecl));
0b60dfe3 3349
5566b478
MS
3350 return 1;
3351 }
0b60dfe3 3352
8d08fdba
MS
3353 if (types_match)
3354 {
3355 /* Automatically handles default parameters. */
3356 tree oldtype = TREE_TYPE (olddecl);
e1cd6e56 3357 tree newtype;
8d08fdba 3358
e1cd6e56
MS
3359 /* Make sure we put the new type in the same obstack as the old one. */
3360 if (oldtype)
39211cd5
MS
3361 push_obstacks (TYPE_OBSTACK (oldtype), TYPE_OBSTACK (oldtype));
3362 else
9188c363 3363 push_permanent_obstack ();
39211cd5 3364
e1cd6e56
MS
3365 /* Merge the data types specified in the two decls. */
3366 newtype = common_type (TREE_TYPE (newdecl), TREE_TYPE (olddecl));
3367
8d08fdba
MS
3368 if (TREE_CODE (newdecl) == VAR_DECL)
3369 DECL_THIS_EXTERN (newdecl) |= DECL_THIS_EXTERN (olddecl);
3370 /* Do this after calling `common_type' so that default
3371 parameters don't confuse us. */
3372 else if (TREE_CODE (newdecl) == FUNCTION_DECL
3373 && (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (newdecl))
3374 != TYPE_RAISES_EXCEPTIONS (TREE_TYPE (olddecl))))
3375 {
f30432d7 3376 TREE_TYPE (newdecl) = build_exception_variant (newtype,
8d08fdba 3377 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (newdecl)));
f30432d7 3378 TREE_TYPE (olddecl) = build_exception_variant (newtype,
8d08fdba
MS
3379 TYPE_RAISES_EXCEPTIONS (oldtype));
3380
9a224b4a
JM
3381 if ((pedantic || ! DECL_IN_SYSTEM_HEADER (olddecl))
3382 && DECL_SOURCE_LINE (olddecl) != 0
da20811c 3383 && flag_exceptions
4cc1d462
NS
3384 && !comp_except_specs (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (newdecl)),
3385 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (olddecl)), 1))
8d08fdba 3386 {
4cc1d462 3387 cp_error ("declaration of `%F' throws different exceptions",
a28e3c7f 3388 newdecl);
4cc1d462 3389 cp_error_at ("to previous declaration `%F'", olddecl);
8d08fdba
MS
3390 }
3391 }
3392 TREE_TYPE (newdecl) = TREE_TYPE (olddecl) = newtype;
3393
3394 /* Lay the type out, unless already done. */
407f03b8 3395 if (! same_type_p (newtype, oldtype)
5566b478 3396 && TREE_TYPE (newdecl) != error_mark_node
5156628f 3397 && !(processing_template_decl && uses_template_parms (newdecl)))
b7484fbe
MS
3398 layout_type (TREE_TYPE (newdecl));
3399
5566b478
MS
3400 if ((TREE_CODE (newdecl) == VAR_DECL
3401 || TREE_CODE (newdecl) == PARM_DECL
3402 || TREE_CODE (newdecl) == RESULT_DECL
3403 || TREE_CODE (newdecl) == FIELD_DECL
3404 || TREE_CODE (newdecl) == TYPE_DECL)
5156628f 3405 && !(processing_template_decl && uses_template_parms (newdecl)))
b7484fbe 3406 layout_decl (newdecl, 0);
8d08fdba
MS
3407
3408 /* Merge the type qualifiers. */
3409 if (TREE_READONLY (newdecl))
3410 TREE_READONLY (olddecl) = 1;
3411 if (TREE_THIS_VOLATILE (newdecl))
3412 TREE_THIS_VOLATILE (olddecl) = 1;
3413
3414 /* Merge the initialization information. */
8926095f
MS
3415 if (DECL_INITIAL (newdecl) == NULL_TREE
3416 && DECL_INITIAL (olddecl) != NULL_TREE)
3417 {
3418 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
3419 DECL_SOURCE_FILE (newdecl) = DECL_SOURCE_FILE (olddecl);
3420 DECL_SOURCE_LINE (newdecl) = DECL_SOURCE_LINE (olddecl);
b0d06515
MM
3421 if (CAN_HAVE_FULL_LANG_DECL_P (newdecl)
3422 && DECL_LANG_SPECIFIC (newdecl)
4d85e00e
MM
3423 && DECL_LANG_SPECIFIC (olddecl))
3424 DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
8926095f 3425 }
39211cd5
MS
3426
3427 /* Merge the section attribute.
3428 We want to issue an error if the sections conflict but that must be
3429 done later in decl_attributes since we are called before attributes
3430 are assigned. */
3431 if (DECL_SECTION_NAME (newdecl) == NULL_TREE)
3432 DECL_SECTION_NAME (newdecl) = DECL_SECTION_NAME (olddecl);
3433
5d73aa63
MM
3434 /* Keep the old rtl since we can safely use it. */
3435 DECL_RTL (newdecl) = DECL_RTL (olddecl);
39211cd5
MS
3436
3437 pop_obstacks ();
8d08fdba
MS
3438 }
3439 /* If cannot merge, then use the new type and qualifiers,
3440 and don't preserve the old rtl. */
3441 else
3442 {
3443 /* Clean out any memory we had of the old declaration. */
3444 tree oldstatic = value_member (olddecl, static_aggregates);
3445 if (oldstatic)
3446 TREE_VALUE (oldstatic) = error_mark_node;
3447
3448 TREE_TYPE (olddecl) = TREE_TYPE (newdecl);
3449 TREE_READONLY (olddecl) = TREE_READONLY (newdecl);
3450 TREE_THIS_VOLATILE (olddecl) = TREE_THIS_VOLATILE (newdecl);
3451 TREE_SIDE_EFFECTS (olddecl) = TREE_SIDE_EFFECTS (newdecl);
3452 }
3453
3454 /* Merge the storage class information. */
a9aedbc2 3455 DECL_WEAK (newdecl) |= DECL_WEAK (olddecl);
e92cc029 3456 DECL_ONE_ONLY (newdecl) |= DECL_ONE_ONLY (olddecl);
a9aedbc2
MS
3457 TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
3458 TREE_STATIC (olddecl) = TREE_STATIC (newdecl) |= TREE_STATIC (olddecl);
3459 if (! DECL_EXTERNAL (olddecl))
3460 DECL_EXTERNAL (newdecl) = 0;
0b60dfe3
BK
3461
3462 if (DECL_LANG_SPECIFIC (newdecl) && DECL_LANG_SPECIFIC (olddecl))
8d08fdba 3463 {
a9aedbc2
MS
3464 DECL_INTERFACE_KNOWN (newdecl) |= DECL_INTERFACE_KNOWN (olddecl);
3465 DECL_NOT_REALLY_EXTERN (newdecl) |= DECL_NOT_REALLY_EXTERN (olddecl);
77be6f82 3466 DECL_COMDAT (newdecl) |= DECL_COMDAT (olddecl);
2b0a63a3
MM
3467 DECL_TEMPLATE_INSTANTIATED (newdecl)
3468 |= DECL_TEMPLATE_INSTANTIATED (olddecl);
0b60dfe3
BK
3469 /* Don't really know how much of the language-specific
3470 values we should copy from old to new. */
3471 DECL_IN_AGGR_P (newdecl) = DECL_IN_AGGR_P (olddecl);
3472 DECL_ACCESS (newdecl) = DECL_ACCESS (olddecl);
3473 DECL_NONCONVERTING_P (newdecl) = DECL_NONCONVERTING_P (olddecl);
fbf1c34b 3474 DECL_TEMPLATE_INFO (newdecl) = DECL_TEMPLATE_INFO (olddecl);
0b60dfe3 3475 olddecl_friend = DECL_FRIEND_P (olddecl);
6a629cac
MM
3476
3477 /* Only functions have DECL_BEFRIENDING_CLASSES. */
3478 if (TREE_CODE (newdecl) == FUNCTION_DECL
3479 || DECL_FUNCTION_TEMPLATE_P (newdecl))
3480 DECL_BEFRIENDING_CLASSES (newdecl)
3481 = chainon (DECL_BEFRIENDING_CLASSES (newdecl),
3482 DECL_BEFRIENDING_CLASSES (olddecl));
8d08fdba
MS
3483 }
3484
8d08fdba
MS
3485 if (TREE_CODE (newdecl) == FUNCTION_DECL)
3486 {
75650646
MM
3487 if (DECL_TEMPLATE_INSTANTIATION (olddecl)
3488 && !DECL_TEMPLATE_INSTANTIATION (newdecl))
3489 {
3490 /* If newdecl is not a specialization, then it is not a
3491 template-related function at all. And that means that we
3492 shoud have exited above, returning 0. */
3493 my_friendly_assert (DECL_TEMPLATE_SPECIALIZATION (newdecl),
3494 0);
3495
3496 if (TREE_USED (olddecl))
3497 /* From [temp.expl.spec]:
3498
3499 If a template, a member template or the member of a class
3500 template is explicitly specialized then that
3501 specialization shall be declared before the first use of
3502 that specialization that would cause an implicit
3503 instantiation to take place, in every translation unit in
3504 which such a use occurs. */
8251199e 3505 cp_error ("explicit specialization of %D after first use",
75650646
MM
3506 olddecl);
3507
3508 SET_DECL_TEMPLATE_SPECIALIZATION (olddecl);
3509 }
faae18ab
MS
3510 DECL_THIS_INLINE (newdecl) |= DECL_THIS_INLINE (olddecl);
3511
3512 /* If either decl says `inline', this fn is inline, unless its
3513 definition was passed already. */
3514 if (DECL_INLINE (newdecl) && DECL_INITIAL (olddecl) == NULL_TREE)
3515 DECL_INLINE (olddecl) = 1;
3516 DECL_INLINE (newdecl) = DECL_INLINE (olddecl);
3517
700f8a87
MS
3518 if (! types_match)
3519 {
3520 DECL_LANGUAGE (olddecl) = DECL_LANGUAGE (newdecl);
3521 DECL_ASSEMBLER_NAME (olddecl) = DECL_ASSEMBLER_NAME (newdecl);
5566b478
MS
3522 DECL_RTL (olddecl) = DECL_RTL (newdecl);
3523 }
3524 if (! types_match || new_defines_function)
3525 {
6f1b4c42
JM
3526 /* These need to be copied so that the names are available.
3527 Note that if the types do match, we'll preserve inline
3528 info and other bits, but if not, we won't. */
700f8a87
MS
3529 DECL_ARGUMENTS (olddecl) = DECL_ARGUMENTS (newdecl);
3530 DECL_RESULT (olddecl) = DECL_RESULT (newdecl);
700f8a87 3531 }
8d08fdba
MS
3532 if (new_defines_function)
3533 /* If defining a function declared with other language
3534 linkage, use the previously declared language linkage. */
3535 DECL_LANGUAGE (newdecl) = DECL_LANGUAGE (olddecl);
6f1b4c42 3536 else if (types_match)
8d08fdba
MS
3537 {
3538 /* If redeclaring a builtin function, and not a definition,
3539 it stays built in. */
3540 if (DECL_BUILT_IN (olddecl))
3541 {
3542 DECL_BUILT_IN (newdecl) = 1;
39211cd5 3543 DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl);
8d08fdba
MS
3544 /* If we're keeping the built-in definition, keep the rtl,
3545 regardless of declaration matches. */
3546 DECL_RTL (newdecl) = DECL_RTL (olddecl);
3547 }
3548 else
3549 DECL_FRAME_SIZE (newdecl) = DECL_FRAME_SIZE (olddecl);
3550
3551 DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
8926095f 3552 if ((DECL_SAVED_INSNS (newdecl) = DECL_SAVED_INSNS (olddecl)))
8d08fdba
MS
3553 /* Previously saved insns go together with
3554 the function's previous definition. */
3555 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
3556 /* Don't clear out the arguments if we're redefining a function. */
3557 if (DECL_ARGUMENTS (olddecl))
3558 DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl);
3559 }
6060a796
MS
3560 if (DECL_LANG_SPECIFIC (olddecl))
3561 DECL_MAIN_VARIANT (newdecl) = DECL_MAIN_VARIANT (olddecl);
8d08fdba
MS
3562 }
3563
a9aedbc2
MS
3564 if (TREE_CODE (newdecl) == NAMESPACE_DECL)
3565 {
3566 NAMESPACE_LEVEL (newdecl) = NAMESPACE_LEVEL (olddecl);
3567 }
3568
8d08fdba
MS
3569 /* Now preserve various other info from the definition. */
3570 TREE_ADDRESSABLE (newdecl) = TREE_ADDRESSABLE (olddecl);
3571 TREE_ASM_WRITTEN (newdecl) = TREE_ASM_WRITTEN (olddecl);
3572 DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
f376e137 3573 DECL_ASSEMBLER_NAME (newdecl) = DECL_ASSEMBLER_NAME (olddecl);
8d08fdba 3574
8d08fdba
MS
3575 if (TREE_CODE (newdecl) == FUNCTION_DECL)
3576 {
3577 int function_size;
8d08fdba
MS
3578
3579 function_size = sizeof (struct tree_decl);
3580
3581 bcopy ((char *) newdecl + sizeof (struct tree_common),
3582 (char *) olddecl + sizeof (struct tree_common),
3583 function_size - sizeof (struct tree_common));
3584
75650646
MM
3585 if (DECL_TEMPLATE_INSTANTIATION (newdecl))
3586 {
3587 /* If newdecl is a template instantiation, it is possible that
3588 the following sequence of events has occurred:
3589
3590 o A friend function was declared in a class template. The
3591 class template was instantiated.
3592
3593 o The instantiation of the friend declaration was
3594 recorded on the instantiation list, and is newdecl.
3595
3596 o Later, however, instantiate_class_template called pushdecl
3597 on the newdecl to perform name injection. But, pushdecl in
3598 turn called duplicate_decls when it discovered that another
3599 declaration of a global function with the same name already
3600 existed.
3601
3602 o Here, in duplicate_decls, we decided to clobber newdecl.
3603
3604 If we're going to do that, we'd better make sure that
3605 olddecl, and not newdecl, is on the list of
3606 instantiations so that if we try to do the instantiation
3607 again we won't get the clobbered declaration. */
3608
3609 tree tmpl = DECL_TI_TEMPLATE (newdecl);
3610 tree decls = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
3611
3612 for (; decls; decls = TREE_CHAIN (decls))
3613 if (TREE_VALUE (decls) == newdecl)
3614 TREE_VALUE (decls) = olddecl;
3615 }
8d08fdba
MS
3616 }
3617 else
3618 {
3619 bcopy ((char *) newdecl + sizeof (struct tree_common),
3620 (char *) olddecl + sizeof (struct tree_common),
3621 sizeof (struct tree_decl) - sizeof (struct tree_common)
3622 + tree_code_length [(int)TREE_CODE (newdecl)] * sizeof (char *));
3623 }
3624
3625 DECL_UID (olddecl) = olddecl_uid;
3626 if (olddecl_friend)
3627 DECL_FRIEND_P (olddecl) = 1;
3628
d9525bec
BK
3629 /* NEWDECL contains the merged attribute lists.
3630 Update OLDDECL to be the same. */
3631 DECL_MACHINE_ATTRIBUTES (olddecl) = DECL_MACHINE_ATTRIBUTES (newdecl);
3632
8d08fdba
MS
3633 return 1;
3634}
3635
3636/* Record a decl-node X as belonging to the current lexical scope.
3637 Check for errors (such as an incompatible declaration for the same
3638 name already seen in the same scope).
3639
3640 Returns either X or an old decl for the same name.
3641 If an old decl is returned, it may have been smashed
3642 to agree with what X says. */
3643
3644tree
3645pushdecl (x)
3646 tree x;
3647{
3648 register tree t;
b35d4555
MM
3649 register tree name;
3650 int need_new_binding;
3651
3652 /* We shouldn't be calling pushdecl when we're generating RTL for a
3653 function that we already did semantic analysis on previously. */
3654 my_friendly_assert (!current_function || doing_semantic_analysis_p (),
3655 19990913);
3656
3657 name = DECL_ASSEMBLER_NAME (x);
3658 need_new_binding = 1;
8d08fdba 3659
50714e79
MM
3660 if (DECL_TEMPLATE_PARM_P (x))
3661 /* Template parameters have no context; they are not X::T even
3662 when declared within a class or namespace. */
3663 ;
3664 else
3665 {
3666 if (current_function_decl && x != current_function_decl
3667 /* A local declaration for a function doesn't constitute
3668 nesting. */
3669 && (TREE_CODE (x) != FUNCTION_DECL || DECL_INITIAL (x))
3670 /* Don't change DECL_CONTEXT of virtual methods. */
3671 && (TREE_CODE (x) != FUNCTION_DECL || !DECL_VIRTUAL_P (x))
3672 && !DECL_CONTEXT (x))
3673 DECL_CONTEXT (x) = current_function_decl;
3674 if (!DECL_CONTEXT (x))
3675 DECL_CONTEXT (x) = FROB_CONTEXT (current_namespace);
3676 }
8d08fdba 3677
8d08fdba 3678 /* Type are looked up using the DECL_NAME, as that is what the rest of the
e92cc029 3679 compiler wants to use. */
a9aedbc2 3680 if (TREE_CODE (x) == TYPE_DECL || TREE_CODE (x) == VAR_DECL
0c20d3d6 3681 || TREE_CODE (x) == NAMESPACE_DECL)
8d08fdba 3682 name = DECL_NAME (x);
8d08fdba
MS
3683
3684 if (name)
3685 {
5566b478
MS
3686#if 0
3687 /* Not needed...see below. */
8d08fdba
MS
3688 char *file;
3689 int line;
5566b478 3690#endif
386b8a85
JM
3691 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
3692 name = TREE_OPERAND (name, 0);
3693
2c73f9f5 3694 /* Namespace-scoped variables are not found in the current level. */
6eb3bb27 3695 if (TREE_CODE (x) == VAR_DECL && DECL_NAMESPACE_SCOPE_P (x))
2c73f9f5
ML
3696 t = namespace_binding (name, DECL_CONTEXT (x));
3697 else
3698 t = lookup_name_current_level (name);
8d08fdba
MS
3699 if (t == error_mark_node)
3700 {
3701 /* error_mark_node is 0 for a while during initialization! */
3702 t = NULL_TREE;
8251199e 3703 cp_error_at ("`%#D' used prior to declaration", x);
8d08fdba
MS
3704 }
3705
51c184be 3706 else if (t != NULL_TREE)
8d08fdba 3707 {
5566b478
MS
3708#if 0
3709 /* This is turned off until I have time to do it right (bpk). */
e92cc029 3710 /* With the code below that uses it... */
8d6e462b
PB
3711 file = DECL_SOURCE_FILE (t);
3712 line = DECL_SOURCE_LINE (t);
5566b478 3713#endif
2ee887f2 3714 if (TREE_CODE (t) == PARM_DECL)
8d08fdba
MS
3715 {
3716 if (DECL_CONTEXT (t) == NULL_TREE)
3717 fatal ("parse errors have confused me too much");
be99da77 3718
e92cc029 3719 /* Check for duplicate params. */
be99da77
MS
3720 if (duplicate_decls (x, t))
3721 return t;
8d08fdba 3722 }
8d6e462b 3723 else if (((TREE_CODE (x) == FUNCTION_DECL && DECL_LANGUAGE (x) == lang_c)
5566b478
MS
3724 || DECL_FUNCTION_TEMPLATE_P (x))
3725 && is_overloaded_fn (t))
2c73f9f5 3726 /* Don't do anything just yet. */;
e1cd6e56
MS
3727 else if (t == wchar_decl_node)
3728 {
3729 if (pedantic && ! DECL_IN_SYSTEM_HEADER (x))
8251199e 3730 cp_pedwarn ("redeclaration of wchar_t as `%T'", TREE_TYPE (x));
e1cd6e56
MS
3731
3732 /* Throw away the redeclaration. */
3733 return t;
3734 }
8926095f 3735 else if (TREE_CODE (t) != TREE_CODE (x))
8d08fdba 3736 {
9ed182dc 3737 if (duplicate_decls (x, t))
51c184be 3738 return t;
8d08fdba
MS
3739 }
3740 else if (duplicate_decls (x, t))
51c184be 3741 {
8d08fdba 3742#if 0
8926095f 3743 /* This is turned off until I have time to do it right (bpk). */
8d08fdba 3744
8926095f
MS
3745 /* Also warn if they did a prototype with `static' on it, but
3746 then later left the `static' off. */
3747 if (! TREE_PUBLIC (name) && TREE_PUBLIC (x))
8d08fdba 3748 {
8926095f
MS
3749 if (DECL_LANG_SPECIFIC (t) && DECL_FRIEND_P (t))
3750 return t;
8d08fdba 3751
8926095f
MS
3752 if (extra_warnings)
3753 {
8251199e 3754 cp_warning ("`static' missing from declaration of `%D'",
a28e3c7f 3755 t);
8926095f
MS
3756 warning_with_file_and_line (file, line,
3757 "previous declaration of `%s'",
3758 decl_as_string (t, 0));
3759 }
8d08fdba 3760
8926095f
MS
3761 /* Now fix things so it'll do what they expect. */
3762 if (current_function_decl)
3763 TREE_PUBLIC (current_function_decl) = 0;
3764 }
51c184be
MS
3765 /* Due to interference in memory reclamation (X may be
3766 obstack-deallocated at this point), we must guard against
8926095f
MS
3767 one really special case. [jason: This should be handled
3768 by start_function] */
51c184be
MS
3769 if (current_function_decl == x)
3770 current_function_decl = t;
8926095f 3771#endif
7177d104
MS
3772 if (TREE_CODE (t) == TYPE_DECL)
3773 SET_IDENTIFIER_TYPE_VALUE (name, TREE_TYPE (t));
f30432d7
MS
3774 else if (TREE_CODE (t) == FUNCTION_DECL)
3775 check_default_args (t);
7177d104 3776
51c184be
MS
3777 return t;
3778 }
35680744
MM
3779 else if (DECL_MAIN_P (x))
3780 {
3781 /* A redeclaration of main, but not a duplicate of the
3782 previous one.
3783
3784 [basic.start.main]
3785
3786 This function shall not be overloaded. */
8251199e
JM
3787 cp_error_at ("invalid redeclaration of `%D'", t);
3788 cp_error ("as `%D'", x);
35680744
MM
3789 /* We don't try to push this declaration since that
3790 causes a crash. */
3791 return x;
3792 }
8d08fdba 3793 }
8926095f 3794
f3400fe2
JM
3795 check_template_shadow (x);
3796
fcfcdfc8
JM
3797 /* If this is a function conjured up by the backend, massage it
3798 so it looks friendly. */
3799 if (TREE_CODE (x) == FUNCTION_DECL
3800 && ! DECL_LANG_SPECIFIC (x))
3801 {
3802 retrofit_lang_decl (x);
3803 DECL_LANGUAGE (x) = lang_c;
3804 }
3805
8926095f
MS
3806 if (TREE_CODE (x) == FUNCTION_DECL && ! DECL_FUNCTION_MEMBER_P (x))
3807 {
7bdbfa05 3808 t = push_overloaded_decl (x, PUSH_LOCAL);
5fdaba89 3809 if (t != x)
8926095f 3810 return t;
f181d4ae
MM
3811 if (!namespace_bindings_p ())
3812 /* We do not need to create a binding for this name;
3813 push_overloaded_decl will have already done so if
3814 necessary. */
3815 need_new_binding = 0;
8926095f 3816 }
6eb3bb27 3817 else if (DECL_FUNCTION_TEMPLATE_P (x) && DECL_NAMESPACE_SCOPE_P (x))
cfe507be
MM
3818 {
3819 t = push_overloaded_decl (x, PUSH_GLOBAL);
3820 if (t == x)
3821 add_decl_to_level (x, NAMESPACE_LEVEL (CP_DECL_CONTEXT (t)));
3822 return t;
3823 }
8d08fdba 3824
a1774733
BK
3825 /* If declaring a type as a typedef, copy the type (unless we're
3826 at line 0), and install this TYPE_DECL as the new type's typedef
3827 name. See the extensive comment in ../c-decl.c (pushdecl). */
8d08fdba
MS
3828 if (TREE_CODE (x) == TYPE_DECL)
3829 {
3830 tree type = TREE_TYPE (x);
a1774733
BK
3831 if (DECL_SOURCE_LINE (x) == 0)
3832 {
3833 if (TYPE_NAME (type) == 0)
3834 TYPE_NAME (type) = x;
3835 }
1c80fb65
MM
3836 else if (type != error_mark_node && TYPE_NAME (type) != x
3837 /* We don't want to copy the type when all we're
3838 doing is making a TYPE_DECL for the purposes of
3839 inlining. */
3840 && (!TYPE_NAME (type)
3841 || TYPE_NAME (type) != DECL_ABSTRACT_ORIGIN (x)))
a1774733 3842 {
ae0a6181
MM
3843 push_obstacks (TYPE_OBSTACK (type), TYPE_OBSTACK (type));
3844
a1774733
BK
3845 DECL_ORIGINAL_TYPE (x) = type;
3846 type = build_type_copy (type);
3847 TYPE_STUB_DECL (type) = TYPE_STUB_DECL (DECL_ORIGINAL_TYPE (x));
3848 TYPE_NAME (type) = x;
3849 TREE_TYPE (x) = type;
ae0a6181
MM
3850
3851 pop_obstacks ();
a1774733 3852 }
8d08fdba 3853
8d08fdba
MS
3854 if (type != error_mark_node
3855 && TYPE_NAME (type)
3856 && TYPE_IDENTIFIER (type))
f181d4ae
MM
3857 set_identifier_type_value_with_scope (DECL_NAME (x), type,
3858 current_binding_level);
3859
8d08fdba
MS
3860 }
3861
3862 /* Multiple external decls of the same identifier ought to match.
3863
3864 We get warnings about inline functions where they are defined.
39211cd5
MS
3865 We get warnings about other functions from push_overloaded_decl.
3866
8d08fdba 3867 Avoid duplicate warnings where they are used. */
39211cd5 3868 if (TREE_PUBLIC (x) && TREE_CODE (x) != FUNCTION_DECL)
8d08fdba
MS
3869 {
3870 tree decl;
3871
31928556
JM
3872 if (IDENTIFIER_NAMESPACE_VALUE (name) != NULL_TREE
3873 && (DECL_EXTERNAL (IDENTIFIER_NAMESPACE_VALUE (name))
3874 || TREE_PUBLIC (IDENTIFIER_NAMESPACE_VALUE (name))))
3875 decl = IDENTIFIER_NAMESPACE_VALUE (name);
8d08fdba
MS
3876 else
3877 decl = NULL_TREE;
3878
39211cd5 3879 if (decl
8d08fdba
MS
3880 /* If different sort of thing, we already gave an error. */
3881 && TREE_CODE (decl) == TREE_CODE (x)
3bfdc719 3882 && !same_type_p (TREE_TYPE (x), TREE_TYPE (decl)))
8d08fdba 3883 {
8251199e
JM
3884 cp_pedwarn ("type mismatch with previous external decl", x);
3885 cp_pedwarn_at ("previous external decl of `%#D'", decl);
8d08fdba
MS
3886 }
3887 }
3888
8d08fdba
MS
3889 /* This name is new in its binding level.
3890 Install the new declaration and return it. */
2c73f9f5 3891 if (namespace_bindings_p ())
8d08fdba
MS
3892 {
3893 /* Install a global value. */
3894
8d08fdba
MS
3895 /* If the first global decl has external linkage,
3896 warn if we later see static one. */
31928556 3897 if (IDENTIFIER_GLOBAL_VALUE (name) == NULL_TREE && TREE_PUBLIC (x))
8d08fdba
MS
3898 TREE_PUBLIC (name) = 1;
3899
d8f8dca1 3900 if (!(TREE_CODE (x) == TYPE_DECL && DECL_ARTIFICIAL (x)
5fdaba89
MM
3901 && t != NULL_TREE)
3902 /* For an ordinary function, we create a binding from
3903 the mangled name (i.e., NAME) to the DECL. But, for
3904 an `extern "C"' function, the mangled name and the
3905 ordinary name are the same so we need not do this. */
3906 && !(TREE_CODE (x) == FUNCTION_DECL &&
3907 DECL_LANGUAGE (x) == lang_c))
30394414
JM
3908 {
3909 if (TREE_CODE (x) == FUNCTION_DECL)
31928556
JM
3910 my_friendly_assert
3911 ((IDENTIFIER_GLOBAL_VALUE (name) == NULL_TREE)
3912 || (IDENTIFIER_GLOBAL_VALUE (name) == x), 378);
3913 SET_IDENTIFIER_NAMESPACE_VALUE (name, x);
30394414 3914 }
8d08fdba
MS
3915
3916 /* Don't forget if the function was used via an implicit decl. */
3917 if (IDENTIFIER_IMPLICIT_DECL (name)
3918 && TREE_USED (IDENTIFIER_IMPLICIT_DECL (name)))
3919 TREE_USED (x) = 1;
3920
3921 /* Don't forget if its address was taken in that way. */
3922 if (IDENTIFIER_IMPLICIT_DECL (name)
3923 && TREE_ADDRESSABLE (IDENTIFIER_IMPLICIT_DECL (name)))
3924 TREE_ADDRESSABLE (x) = 1;
3925
3926 /* Warn about mismatches against previous implicit decl. */
3927 if (IDENTIFIER_IMPLICIT_DECL (name) != NULL_TREE
3928 /* If this real decl matches the implicit, don't complain. */
3929 && ! (TREE_CODE (x) == FUNCTION_DECL
3930 && TREE_TYPE (TREE_TYPE (x)) == integer_type_node))
8251199e
JM
3931 cp_warning
3932 ("`%D' was previously implicitly declared to return `int'", x);
8d08fdba
MS
3933
3934 /* If new decl is `static' and an `extern' was seen previously,
3935 warn about it. */
a0a33927
MS
3936 if (x != NULL_TREE && t != NULL_TREE && decls_match (x, t))
3937 warn_extern_redeclared_static (x, t);
8d08fdba
MS
3938 }
3939 else
3940 {
3941 /* Here to install a non-global value. */
f181d4ae 3942 tree oldlocal = IDENTIFIER_VALUE (name);
31928556 3943 tree oldglobal = IDENTIFIER_NAMESPACE_VALUE (name);
8d08fdba 3944
f181d4ae
MM
3945 if (need_new_binding)
3946 {
0034cf72 3947 push_local_binding (name, x, 0);
f181d4ae
MM
3948 /* Because push_local_binding will hook X on to the
3949 current_binding_level's name list, we don't want to
3950 do that again below. */
3951 need_new_binding = 0;
3952 }
8d08fdba
MS
3953
3954 /* If this is a TYPE_DECL, push it into the type value slot. */
3955 if (TREE_CODE (x) == TYPE_DECL)
f181d4ae
MM
3956 set_identifier_type_value_with_scope (name, TREE_TYPE (x),
3957 current_binding_level);
8d08fdba 3958
a9aedbc2
MS
3959 /* Clear out any TYPE_DECL shadowed by a namespace so that
3960 we won't think this is a type. The C struct hack doesn't
3961 go through namespaces. */
3962 if (TREE_CODE (x) == NAMESPACE_DECL)
f181d4ae
MM
3963 set_identifier_type_value_with_scope (name, NULL_TREE,
3964 current_binding_level);
a9aedbc2 3965
8d08fdba
MS
3966 /* If this is an extern function declaration, see if we
3967 have a global definition or declaration for the function. */
3968 if (oldlocal == NULL_TREE
faae18ab 3969 && DECL_EXTERNAL (x)
31928556 3970 && oldglobal != NULL_TREE
8d08fdba 3971 && TREE_CODE (x) == FUNCTION_DECL
31928556 3972 && TREE_CODE (oldglobal) == FUNCTION_DECL)
8d08fdba
MS
3973 {
3974 /* We have one. Their types must agree. */
31928556 3975 if (decls_match (x, oldglobal))
6060a796
MS
3976 /* OK */;
3977 else
8d08fdba 3978 {
8251199e
JM
3979 cp_warning ("extern declaration of `%#D' doesn't match", x);
3980 cp_warning_at ("global declaration `%#D'", oldglobal);
8d08fdba 3981 }
8d08fdba
MS
3982 }
3983 /* If we have a local external declaration,
3984 and no file-scope declaration has yet been seen,
3985 then if we later have a file-scope decl it must not be static. */
3986 if (oldlocal == NULL_TREE
31928556 3987 && oldglobal == NULL_TREE
8d08fdba
MS
3988 && DECL_EXTERNAL (x)
3989 && TREE_PUBLIC (x))
f181d4ae 3990 TREE_PUBLIC (name) = 1;
8d08fdba
MS
3991
3992 if (DECL_FROM_INLINE (x))
3993 /* Inline decls shadow nothing. */;
3994
3995 /* Warn if shadowing an argument at the top level of the body. */
3996 else if (oldlocal != NULL_TREE && !DECL_EXTERNAL (x)
9d29e7af
JM
3997 && TREE_CODE (oldlocal) == PARM_DECL
3998 /* Don't complain if it's from an enclosing function. */
3999 && DECL_CONTEXT (oldlocal) == current_function_decl
4000 && TREE_CODE (x) != PARM_DECL)
8d08fdba
MS
4001 {
4002 /* Go to where the parms should be and see if we
4003 find them there. */
4004 struct binding_level *b = current_binding_level->level_chain;
4005
4006 if (cleanup_label)
4007 b = b->level_chain;
4008
4009 /* ARM $8.3 */
4010 if (b->parm_flag == 1)
8251199e 4011 cp_error ("declaration of `%#D' shadows a parameter", name);
8d08fdba 4012 }
f181d4ae
MM
4013 else if (warn_shadow && oldlocal != NULL_TREE
4014 && current_binding_level->is_for_scope
8d6e462b
PB
4015 && !DECL_DEAD_FOR_LOCAL (oldlocal))
4016 {
8251199e 4017 warning ("variable `%s' shadows local",
8d6e462b 4018 IDENTIFIER_POINTER (name));
8251199e 4019 cp_warning_at (" this is the shadowed declaration", oldlocal);
8d6e462b 4020 }
8d08fdba
MS
4021 /* Maybe warn if shadowing something else. */
4022 else if (warn_shadow && !DECL_EXTERNAL (x)
4023 /* No shadow warnings for internally generated vars. */
700f8a87 4024 && ! DECL_ARTIFICIAL (x)
8d08fdba
MS
4025 /* No shadow warnings for vars made for inlining. */
4026 && ! DECL_FROM_INLINE (x))
4027 {
d8e178a0 4028 const char *warnstring = NULL;
8d08fdba
MS
4029
4030 if (oldlocal != NULL_TREE && TREE_CODE (oldlocal) == PARM_DECL)
4031 warnstring = "declaration of `%s' shadows a parameter";
4032 else if (IDENTIFIER_CLASS_VALUE (name) != NULL_TREE
4ac14744 4033 && current_class_ptr
8d08fdba
MS
4034 && !TREE_STATIC (name))
4035 warnstring = "declaration of `%s' shadows a member of `this'";
4036 else if (oldlocal != NULL_TREE)
4037 warnstring = "declaration of `%s' shadows previous local";
31928556 4038 else if (oldglobal != NULL_TREE)
30394414 4039 /* XXX shadow warnings in outer-more namespaces */
8d08fdba
MS
4040 warnstring = "declaration of `%s' shadows global declaration";
4041
4042 if (warnstring)
4043 warning (warnstring, IDENTIFIER_POINTER (name));
4044 }
e1cd6e56 4045 }
8d08fdba 4046
e1cd6e56 4047 if (TREE_CODE (x) == FUNCTION_DECL)
f30432d7 4048 check_default_args (x);
8145f082
MS
4049
4050 /* Keep count of variables in this level with incomplete type. */
8145f082 4051 if (TREE_CODE (x) == VAR_DECL
28cbf42c 4052 && TREE_TYPE (x) != error_mark_node
f30432d7
MS
4053 && ((TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
4054 && PROMOTES_TO_AGGR_TYPE (TREE_TYPE (x), ARRAY_TYPE))
4055 /* RTTI TD entries are created while defining the type_info. */
4056 || (TYPE_LANG_SPECIFIC (TREE_TYPE (x))
4057 && TYPE_BEING_DEFINED (TREE_TYPE (x)))))
f181d4ae
MM
4058 current_binding_level->incomplete
4059 = tree_cons (NULL_TREE, x, current_binding_level->incomplete);
8d08fdba
MS
4060 }
4061
f181d4ae 4062 if (need_new_binding)
cfe507be 4063 add_decl_to_level (x, current_binding_level);
8d08fdba
MS
4064
4065 return x;
4066}
4067
5566b478
MS
4068/* Same as pushdecl, but define X in binding-level LEVEL. We rely on the
4069 caller to set DECL_CONTEXT properly. */
8d08fdba
MS
4070
4071static tree
4072pushdecl_with_scope (x, level)
4073 tree x;
4074 struct binding_level *level;
4075{
8d019cef 4076 register struct binding_level *b;
5566b478 4077 tree function_decl = current_function_decl;
8d08fdba 4078
5566b478 4079 current_function_decl = NULL_TREE;
8d019cef
JM
4080 if (level->parm_flag == 2)
4081 {
4082 b = class_binding_level;
4083 class_binding_level = level;
4084 pushdecl_class_level (x);
4085 class_binding_level = b;
4086 }
4087 else
4088 {
4089 b = current_binding_level;
4090 current_binding_level = level;
4091 x = pushdecl (x);
4092 current_binding_level = b;
4093 }
5566b478 4094 current_function_decl = function_decl;
8d08fdba
MS
4095 return x;
4096}
4097
2c73f9f5 4098/* Like pushdecl, only it places X in the current namespace,
8d08fdba 4099 if appropriate. */
e92cc029 4100
8d08fdba 4101tree
2c73f9f5 4102pushdecl_namespace_level (x)
8d08fdba
MS
4103 tree x;
4104{
8f032717 4105 register struct binding_level *b = current_binding_level;
2c73f9f5
ML
4106 register tree t;
4107
4108 t = pushdecl_with_scope (x, NAMESPACE_LEVEL (current_namespace));
8d08fdba
MS
4109
4110 /* Now, the type_shadowed stack may screw us. Munge it so it does
4111 what we want. */
4112 if (TREE_CODE (x) == TYPE_DECL)
4113 {
4114 tree name = DECL_NAME (x);
4115 tree newval;
4116 tree *ptr = (tree *)0;
4117 for (; b != global_binding_level; b = b->level_chain)
4118 {
4119 tree shadowed = b->type_shadowed;
4120 for (; shadowed; shadowed = TREE_CHAIN (shadowed))
4121 if (TREE_PURPOSE (shadowed) == name)
4122 {
4123 ptr = &TREE_VALUE (shadowed);
4124 /* Can't break out of the loop here because sometimes
4125 a binding level will have duplicate bindings for
4126 PT names. It's gross, but I haven't time to fix it. */
4127 }
4128 }
4129 newval = TREE_TYPE (x);
4130 if (ptr == (tree *)0)
4131 {
4132 /* @@ This shouldn't be needed. My test case "zstring.cc" trips
4133 up here if this is changed to an assertion. --KR */
4134 SET_IDENTIFIER_TYPE_VALUE (name, newval);
4135 }
4136 else
4137 {
8d08fdba
MS
4138 *ptr = newval;
4139 }
4140 }
4141 return t;
4142}
4143
2c73f9f5
ML
4144/* Like pushdecl, only it places X in GLOBAL_BINDING_LEVEL,
4145 if appropriate. */
4146
4147tree
4148pushdecl_top_level (x)
4149 tree x;
4150{
b35d4555 4151 push_to_top_level ();
2c73f9f5 4152 x = pushdecl_namespace_level (x);
b35d4555 4153 pop_from_top_level ();
2c73f9f5
ML
4154 return x;
4155}
4156
8d08fdba 4157/* Make the declaration of X appear in CLASS scope. */
e92cc029 4158
61a127b3 4159void
8d08fdba
MS
4160pushdecl_class_level (x)
4161 tree x;
4162{
4163 /* Don't use DECL_ASSEMBLER_NAME here! Everything that looks in class
4164 scope looks for the pre-mangled name. */
8f032717
MM
4165 register tree name;
4166
4167 if (TREE_CODE (x) == OVERLOAD)
4168 x = OVL_CURRENT (x);
4169 name = DECL_NAME (x);
8d08fdba
MS
4170
4171 if (name)
4172 {
4173 push_class_level_binding (name, x);
4174 if (TREE_CODE (x) == TYPE_DECL)
f181d4ae 4175 set_identifier_type_value (name, TREE_TYPE (x));
8d08fdba 4176 }
6bdb8141 4177 else if (ANON_AGGR_TYPE_P (TREE_TYPE (x)))
8f032717
MM
4178 {
4179 tree f;
4180
4181 for (f = TYPE_FIELDS (TREE_TYPE (x));
4182 f;
4183 f = TREE_CHAIN (f))
4184 pushdecl_class_level (f);
4185 }
8d08fdba
MS
4186}
4187
9188c363
MM
4188/* Enter DECL into the symbol table, if that's appropriate. Returns
4189 DECL, or a modified version thereof. */
4190
4191tree
4192maybe_push_decl (decl)
4193 tree decl;
4194{
4195 tree type = TREE_TYPE (decl);
4196
4197 /* Add this decl to the current binding level, but not if it comes
4198 from another scope, e.g. a static member variable. TEM may equal
4199 DECL or it may be a previous decl of the same name. */
4200 if ((TREE_CODE (decl) != PARM_DECL
4201 && DECL_CONTEXT (decl) != NULL_TREE
4202 /* Definitions of namespace members outside their namespace are
4203 possible. */
4204 && TREE_CODE (DECL_CONTEXT (decl)) != NAMESPACE_DECL)
4205 || (TREE_CODE (decl) == TEMPLATE_DECL && !namespace_bindings_p ())
4206 || TREE_CODE (type) == UNKNOWN_TYPE
558475f0 4207 /* The declaration of a template specialization does not affect
9188c363
MM
4208 the functions available for overload resolution, so we do not
4209 call pushdecl. */
4210 || (TREE_CODE (decl) == FUNCTION_DECL
4211 && DECL_TEMPLATE_SPECIALIZATION (decl)))
4212 return decl;
4213 else
4214 return pushdecl (decl);
4215}
4216
bd6dd845 4217#if 0
7177d104
MS
4218/* This function is used to push the mangled decls for nested types into
4219 the appropriate scope. Previously pushdecl_top_level was used, but that
4220 is incorrect for members of local classes. */
e92cc029 4221
5566b478 4222void
7177d104
MS
4223pushdecl_nonclass_level (x)
4224 tree x;
4225{
4226 struct binding_level *b = current_binding_level;
4227
7177d104 4228 my_friendly_assert (b->parm_flag != 2, 180);
7177d104 4229
5566b478 4230#if 0
7177d104
MS
4231 /* Get out of template binding levels */
4232 while (b->pseudo_global)
4233 b = b->level_chain;
5566b478 4234#endif
7177d104
MS
4235
4236 pushdecl_with_scope (x, b);
4237}
bd6dd845 4238#endif
7177d104 4239
8d08fdba
MS
4240/* Make the declaration(s) of X appear in CLASS scope
4241 under the name NAME. */
e92cc029 4242
8d08fdba
MS
4243void
4244push_class_level_binding (name, x)
4245 tree name;
4246 tree x;
4247{
8f032717 4248 tree binding;
98c1c668
JM
4249 /* The class_binding_level will be NULL if x is a template
4250 parameter name in a member template. */
4251 if (!class_binding_level)
4252 return;
4253
908c4e83
MM
4254 /* Make sure that this new member does not have the same name
4255 as a template parameter. */
4256 if (TYPE_BEING_DEFINED (current_class_type))
4257 check_template_shadow (x);
4258
f181d4ae
MM
4259 /* If this declaration shadows a declaration from an enclosing
4260 class, then we will need to restore IDENTIFIER_CLASS_VALUE when
4261 we leave this class. Record the shadowed declaration here. */
8f032717
MM
4262 binding = IDENTIFIER_BINDING (name);
4263 if (binding
4264 && ((TREE_CODE (x) == OVERLOAD
4265 && BINDING_VALUE (binding)
4266 && is_overloaded_fn (BINDING_VALUE (binding)))
4267 || INHERITED_VALUE_BINDING_P (binding)))
4268 {
4269 tree shadow;
4270 tree old_decl;
4271
4272 /* If the old binding was from a base class, and was for a tag
4273 name, slide it over to make room for the new binding. The
4274 old binding is still visible if explicitly qualified with a
4275 class-key. */
4276 if (INHERITED_VALUE_BINDING_P (binding)
4277 && BINDING_VALUE (binding)
4278 && TREE_CODE (BINDING_VALUE (binding)) == TYPE_DECL
4279 && DECL_ARTIFICIAL (BINDING_VALUE (binding))
4280 && !(TREE_CODE (x) == TYPE_DECL && DECL_ARTIFICIAL (x)))
4281 {
4282 old_decl = BINDING_TYPE (binding);
4283 BINDING_TYPE (binding) = BINDING_VALUE (binding);
4284 BINDING_VALUE (binding) = NULL_TREE;
4285 INHERITED_VALUE_BINDING_P (binding) = 0;
4286 }
4287 else
4288 old_decl = BINDING_VALUE (binding);
4289
4290 /* There was already a binding for X containing fewer
4291 functions than are named in X. Find the previous
4292 declaration of X on the class-shadowed list, and update it. */
4293 for (shadow = class_binding_level->class_shadowed;
4294 shadow;
4295 shadow = TREE_CHAIN (shadow))
4296 if (TREE_PURPOSE (shadow) == name
4297 && TREE_TYPE (shadow) == old_decl)
4298 {
4299 BINDING_VALUE (binding) = x;
4300 INHERITED_VALUE_BINDING_P (binding) = 0;
4301 TREE_TYPE (shadow) = x;
4302 return;
4303 }
4304 }
f181d4ae 4305
8f032717
MM
4306 /* If we didn't replace an existing binding, put the binding on the
4307 stack of bindings for the identifier, and update
4308 IDENTIFIER_CLASS_VALUE. */
4309 if (push_class_binding (name, x))
4310 {
3f1892e1 4311 push_cache_obstack ();
8f032717
MM
4312 class_binding_level->class_shadowed
4313 = tree_cons (name, IDENTIFIER_CLASS_VALUE (name),
4314 class_binding_level->class_shadowed);
4315 pop_obstacks ();
4316 /* Record the value we are binding NAME to so that we can know
4317 what to pop later. */
4318 TREE_TYPE (class_binding_level->class_shadowed) = x;
4319 }
8d08fdba
MS
4320}
4321
2c73f9f5
ML
4322/* Insert another USING_DECL into the current binding level,
4323 returning this declaration. If this is a redeclaration,
4324 do nothing and return NULL_TREE. */
e92cc029 4325
2c73f9f5
ML
4326tree
4327push_using_decl (scope, name)
4328 tree scope;
4329 tree name;
8d08fdba 4330{
2c73f9f5
ML
4331 tree decl;
4332
2c73f9f5
ML
4333 my_friendly_assert (TREE_CODE (scope) == NAMESPACE_DECL, 383);
4334 my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, 384);
4335 for (decl = current_binding_level->usings; decl; decl = TREE_CHAIN (decl))
4336 if (DECL_INITIAL (decl) == scope && DECL_NAME (decl) == name)
4337 break;
4338 if (decl)
4339 return NULL_TREE;
4ce3d537 4340 decl = build_lang_decl (USING_DECL, name, void_type_node);
2c73f9f5
ML
4341 DECL_INITIAL (decl) = scope;
4342 TREE_CHAIN (decl) = current_binding_level->usings;
4343 current_binding_level->usings = decl;
4344 return decl;
8d08fdba
MS
4345}
4346
ea9635c7
ML
4347/* Add namespace to using_directives. Return NULL_TREE if nothing was
4348 changed (i.e. there was already a directive), or the fresh
4349 TREE_LIST otherwise. */
4350
4351tree
9ed182dc 4352push_using_directive (used)
ea9635c7 4353 tree used;
ea9635c7
ML
4354{
4355 tree ud = current_binding_level->using_directives;
9ed182dc 4356 tree iter, ancestor;
ea9635c7
ML
4357
4358 /* Check if we already have this. */
4359 if (purpose_member (used, ud) != NULL_TREE)
4360 return NULL_TREE;
00dc6358
JM
4361
4362 /* Recursively add all namespaces used. */
4363 for (iter = DECL_NAMESPACE_USING (used); iter; iter = TREE_CHAIN (iter))
9ed182dc 4364 push_using_directive (TREE_PURPOSE (iter));
00dc6358 4365
9ed182dc 4366 ancestor = namespace_ancestor (current_decl_namespace (), used);
00dc6358 4367 ud = current_binding_level->using_directives;
e1b3e07d 4368 ud = tree_cons (used, ancestor, ud);
ea9635c7
ML
4369 current_binding_level->using_directives = ud;
4370 return ud;
4371}
4372
f181d4ae
MM
4373/* DECL is a FUNCTION_DECL for a non-member function, which may have
4374 other definitions already in place. We get around this by making
4375 the value of the identifier point to a list of all the things that
4376 want to be referenced by that name. It is then up to the users of
4377 that name to decide what to do with that list.
8d08fdba
MS
4378
4379 DECL may also be a TEMPLATE_DECL, with a FUNCTION_DECL in its DECL_RESULT
4380 slot. It is dealt with the same way.
4381
7bdbfa05
MM
4382 FLAGS is a bitwise-or of the following values:
4383 PUSH_LOCAL: Bind DECL in the current scope, rather than at
4384 namespace scope.
4385 PUSH_USING: DECL is being pushed as the result of a using
4386 declaration.
4387
8d08fdba
MS
4388 The value returned may be a previous declaration if we guessed wrong
4389 about what language DECL should belong to (C or C++). Otherwise,
4390 it's always DECL (and never something that's not a _DECL). */
e92cc029 4391
7bdbfa05
MM
4392tree
4393push_overloaded_decl (decl, flags)
8d08fdba 4394 tree decl;
7bdbfa05 4395 int flags;
8d08fdba 4396{
f181d4ae 4397 tree name = DECL_NAME (decl);
700f8a87 4398 tree old;
f181d4ae 4399 tree new_binding;
7bdbfa05 4400 int doing_global = (namespace_bindings_p () || !(flags & PUSH_LOCAL));
700f8a87
MS
4401
4402 if (doing_global)
9f33663b 4403 old = namespace_binding (name, DECL_CONTEXT (decl));
700f8a87 4404 else
f181d4ae 4405 old = lookup_name_current_level (name);
8d08fdba 4406
700f8a87 4407 if (old)
8d08fdba 4408 {
e1cd6e56 4409 if (TREE_CODE (old) == TYPE_DECL && DECL_ARTIFICIAL (old))
8926095f 4410 {
700f8a87 4411 tree t = TREE_TYPE (old);
cdf5b885
MS
4412 if (IS_AGGR_TYPE (t) && warn_shadow
4413 && (! DECL_IN_SYSTEM_HEADER (decl)
4414 || ! DECL_IN_SYSTEM_HEADER (old)))
8251199e 4415 cp_warning ("`%#D' hides constructor for `%#T'", decl, t);
5b605f68 4416 old = NULL_TREE;
8926095f 4417 }
700f8a87 4418 else if (is_overloaded_fn (old))
8d08fdba 4419 {
8d08fdba
MS
4420 tree tmp;
4421
2c73f9f5 4422 for (tmp = old; tmp; tmp = OVL_NEXT (tmp))
7bdbfa05
MM
4423 {
4424 tree fn = OVL_CURRENT (tmp);
4425
4426 if (TREE_CODE (tmp) == OVERLOAD && OVL_USED (tmp)
4427 && !(flags & PUSH_USING)
4428 && compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
4429 TYPE_ARG_TYPES (TREE_TYPE (decl))))
4430 cp_error ("`%#D' conflicts with previous using declaration `%#D'",
4431 decl, fn);
4432
4433 if (duplicate_decls (decl, fn))
4434 return fn;
4435 }
8d08fdba 4436 }
e1cd6e56
MS
4437 else
4438 {
8251199e
JM
4439 cp_error_at ("previous non-function declaration `%#D'", old);
4440 cp_error ("conflicts with function declaration `%#D'", decl);
a9aedbc2 4441 return decl;
e1cd6e56 4442 }
8d08fdba 4443 }
7177d104 4444
700f8a87 4445 if (old || TREE_CODE (decl) == TEMPLATE_DECL)
8d08fdba 4446 {
2c73f9f5 4447 if (old && TREE_CODE (old) != OVERLOAD)
f181d4ae
MM
4448 new_binding = ovl_cons (decl, ovl_cons (old, NULL_TREE));
4449 else
4450 new_binding = ovl_cons (decl, old);
347d73d7
ML
4451 if (flags & PUSH_USING)
4452 OVL_USED (new_binding) = 1;
8d08fdba
MS
4453 }
4454 else
f181d4ae
MM
4455 /* NAME is not ambiguous. */
4456 new_binding = decl;
700f8a87
MS
4457
4458 if (doing_global)
f181d4ae 4459 set_namespace_binding (name, current_namespace, new_binding);
700f8a87 4460 else
f181d4ae
MM
4461 {
4462 /* We only create an OVERLOAD if there was a previous binding at
0580c9aa
ML
4463 this level, or if decl is a template. In the former case, we
4464 need to remove the old binding and replace it with the new
4465 binding. We must also run through the NAMES on the binding
4466 level where the name was bound to update the chain. */
4467
4468 if (TREE_CODE (new_binding) == OVERLOAD && old)
f181d4ae
MM
4469 {
4470 tree *d;
4471
4472 for (d = &BINDING_LEVEL (IDENTIFIER_BINDING (name))->names;
4473 *d;
4474 d = &TREE_CHAIN (*d))
4475 if (*d == old
4476 || (TREE_CODE (*d) == TREE_LIST
4477 && TREE_VALUE (*d) == old))
4478 {
d8f8dca1
MM
4479 if (TREE_CODE (*d) == TREE_LIST)
4480 /* Just replace the old binding with the new. */
4481 TREE_VALUE (*d) = new_binding;
4482 else
4483 /* Build a TREE_LIST to wrap the OVERLOAD. */
ed5511d9
MM
4484 *d = tree_cons (NULL_TREE, new_binding,
4485 TREE_CHAIN (*d));
d8f8dca1
MM
4486
4487 /* And update the CPLUS_BINDING node. */
4488 BINDING_VALUE (IDENTIFIER_BINDING (name))
4489 = new_binding;
4490 return decl;
f181d4ae
MM
4491 }
4492
d8f8dca1
MM
4493 /* We should always find a previous binding in this case. */
4494 my_friendly_abort (0);
f181d4ae
MM
4495 }
4496
4497 /* Install the new binding. */
0034cf72 4498 push_local_binding (name, new_binding, flags);
f181d4ae 4499 }
700f8a87 4500
8d08fdba
MS
4501 return decl;
4502}
4503\f
4504/* Generate an implicit declaration for identifier FUNCTIONID
4505 as a function of type int (). Print a warning if appropriate. */
4506
4507tree
4508implicitly_declare (functionid)
4509 tree functionid;
4510{
4511 register tree decl;
4512 int temp = allocation_temporary_p ();
4513
4514 push_obstacks_nochange ();
4515
4516 /* Save the decl permanently so we can warn if definition follows.
4517 In ANSI C, warn_implicit is usually false, so the saves little space.
4518 But in C++, it's usually true, hence the extra code. */
d22c8596 4519 if (temp && (! warn_implicit || toplevel_bindings_p ()))
8d08fdba
MS
4520 end_temporary_allocation ();
4521
4522 /* We used to reuse an old implicit decl here,
4523 but this loses with inline functions because it can clobber
4524 the saved decl chains. */
4525 decl = build_lang_decl (FUNCTION_DECL, functionid, default_function_type);
4526
4527 DECL_EXTERNAL (decl) = 1;
4528 TREE_PUBLIC (decl) = 1;
4529
4530 /* ANSI standard says implicit declarations are in the innermost block.
d22c8596 4531 So we record the decl in the standard fashion. */
8d08fdba
MS
4532 pushdecl (decl);
4533 rest_of_decl_compilation (decl, NULL_PTR, 0, 0);
4534
4535 if (warn_implicit
4536 /* Only one warning per identifier. */
4537 && IDENTIFIER_IMPLICIT_DECL (functionid) == NULL_TREE)
4538 {
8251199e 4539 cp_pedwarn ("implicit declaration of function `%#D'", decl);
8d08fdba
MS
4540 }
4541
4542 SET_IDENTIFIER_IMPLICIT_DECL (functionid, decl);
4543
4544 pop_obstacks ();
4545
4546 return decl;
4547}
4548
8251199e 4549/* Return zero if the declaration NEWDECL is valid
8d08fdba
MS
4550 when the declaration OLDDECL (assumed to be for the same name)
4551 has already been seen.
4552 Otherwise return an error message format string with a %s
4553 where the identifier should go. */
4554
d8e178a0 4555static const char *
8d08fdba
MS
4556redeclaration_error_message (newdecl, olddecl)
4557 tree newdecl, olddecl;
4558{
4559 if (TREE_CODE (newdecl) == TYPE_DECL)
4560 {
4561 /* Because C++ can put things into name space for free,
4562 constructs like "typedef struct foo { ... } foo"
4563 would look like an erroneous redeclaration. */
3bfdc719 4564 if (same_type_p (TREE_TYPE (newdecl), TREE_TYPE (olddecl)))
8251199e 4565 return 0;
8d08fdba 4566 else
8251199e 4567 return "redefinition of `%#D'";
8d08fdba
MS
4568 }
4569 else if (TREE_CODE (newdecl) == FUNCTION_DECL)
4570 {
4571 /* If this is a pure function, its olddecl will actually be
4572 the original initialization to `0' (which we force to call
4573 abort()). Don't complain about redefinition in this case. */
4574 if (DECL_LANG_SPECIFIC (olddecl) && DECL_ABSTRACT_VIRTUAL_P (olddecl))
8251199e 4575 return 0;
8d08fdba 4576
2c73f9f5
ML
4577 /* If both functions come from different namespaces, this is not
4578 a redeclaration - this is a conflict with a used function. */
6eb3bb27 4579 if (DECL_NAMESPACE_SCOPE_P (olddecl)
2c73f9f5 4580 && DECL_CONTEXT (olddecl) != DECL_CONTEXT (newdecl))
8251199e 4581 return "`%D' conflicts with used function";
2c73f9f5 4582
db5ae43f
MS
4583 /* We'll complain about linkage mismatches in
4584 warn_extern_redeclared_static. */
4585
2c73f9f5 4586 /* Defining the same name twice is no good. */
8d08fdba 4587 if (DECL_INITIAL (olddecl) != NULL_TREE
faae18ab 4588 && DECL_INITIAL (newdecl) != NULL_TREE)
8d08fdba
MS
4589 {
4590 if (DECL_NAME (olddecl) == NULL_TREE)
8251199e 4591 return "`%#D' not declared in class";
8d08fdba 4592 else
8251199e 4593 return "redefinition of `%#D'";
8d08fdba 4594 }
8251199e 4595 return 0;
8d08fdba 4596 }
51c184be
MS
4597 else if (TREE_CODE (newdecl) == TEMPLATE_DECL)
4598 {
ec255269
MS
4599 if ((TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == FUNCTION_DECL
4600 && DECL_INITIAL (DECL_TEMPLATE_RESULT (newdecl))
4601 && DECL_INITIAL (DECL_TEMPLATE_RESULT (olddecl)))
4602 || (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL
4603 && TYPE_SIZE (TREE_TYPE (newdecl))
4604 && TYPE_SIZE (TREE_TYPE (olddecl))))
8251199e
JM
4605 return "redefinition of `%#D'";
4606 return 0;
51c184be 4607 }
5566b478 4608 else if (toplevel_bindings_p ())
8d08fdba
MS
4609 {
4610 /* Objects declared at top level: */
4611 /* If at least one is a reference, it's ok. */
4612 if (DECL_EXTERNAL (newdecl) || DECL_EXTERNAL (olddecl))
8251199e 4613 return 0;
8926095f 4614 /* Reject two definitions. */
8251199e 4615 return "redefinition of `%#D'";
8d08fdba
MS
4616 }
4617 else
4618 {
4619 /* Objects declared with block scope: */
4620 /* Reject two definitions, and reject a definition
4621 together with an external reference. */
4622 if (!(DECL_EXTERNAL (newdecl) && DECL_EXTERNAL (olddecl)))
8251199e
JM
4623 return "redeclaration of `%#D'";
4624 return 0;
8d08fdba
MS
4625 }
4626}
4627\f
acef433b 4628/* Create a new label, named ID. */
8d08fdba 4629
acef433b
MM
4630static tree
4631make_label_decl (id, local_p)
8d08fdba 4632 tree id;
acef433b 4633 int local_p;
8d08fdba 4634{
acef433b 4635 tree decl;
8d08fdba 4636
acef433b 4637 decl = build_decl (LABEL_DECL, id, void_type_node);
b35d4555 4638 if (expanding_p)
acef433b
MM
4639 /* Make sure every label has an rtx. */
4640 label_rtx (decl);
4641
4642 DECL_CONTEXT (decl) = current_function_decl;
4643 DECL_MODE (decl) = VOIDmode;
4644 C_DECLARED_LABEL_FLAG (decl) = local_p;
8d08fdba 4645
acef433b
MM
4646 /* Say where one reference is to the label, for the sake of the
4647 error if it is not defined. */
4648 DECL_SOURCE_LINE (decl) = lineno;
4649 DECL_SOURCE_FILE (decl) = input_filename;
4650
4651 /* Record the fact that this identifier is bound to this label. */
4652 SET_IDENTIFIER_LABEL_VALUE (id, decl);
4653
4654 /* Record this label on the list of used labels so that we can check
4655 at the end of the function to see whether or not the label was
4656 actually defined. */
4657 if ((named_label_uses == NULL || named_label_uses->label_decl != decl)
e349ee73
MS
4658 && (named_label_uses == NULL
4659 || named_label_uses->names_in_scope != current_binding_level->names
4660 || named_label_uses->label_decl != decl))
4661 {
4662 struct named_label_list *new_ent;
4663 new_ent
4664 = (struct named_label_list*)oballoc (sizeof (struct named_label_list));
4665 new_ent->label_decl = decl;
4666 new_ent->names_in_scope = current_binding_level->names;
4667 new_ent->binding_level = current_binding_level;
4668 new_ent->lineno_o_goto = lineno;
4669 new_ent->filename_o_goto = input_filename;
4670 new_ent->next = named_label_uses;
4671 named_label_uses = new_ent;
8d08fdba
MS
4672 }
4673
acef433b
MM
4674 return decl;
4675}
8d08fdba 4676
acef433b
MM
4677/* Look for a label named ID in the current function. If one cannot
4678 be found, create one. (We keep track of used, but undefined,
4679 labels, and complain about them at the end of a function.) */
8d08fdba 4680
acef433b
MM
4681tree
4682lookup_label (id)
4683 tree id;
4684{
4685 tree decl;
8d08fdba 4686
acef433b
MM
4687 /* You can't use labels at global scope. */
4688 if (current_function_decl == NULL_TREE)
4689 {
4690 error ("label `%s' referenced outside of any function",
4691 IDENTIFIER_POINTER (id));
4692 return NULL_TREE;
4693 }
4694
4695 /* See if we've already got this label. */
4696 decl = IDENTIFIER_LABEL_VALUE (id);
4697 if (decl != NULL_TREE && DECL_CONTEXT (decl) == current_function_decl)
4698 return decl;
8d08fdba 4699
acef433b
MM
4700 /* Record this label on the list of labels used in this function.
4701 We do this before calling make_label_decl so that we get the
4702 IDENTIFIER_LABEL_VALUE before the new label is declared. */
4703 named_labels = tree_cons (IDENTIFIER_LABEL_VALUE (id), NULL_TREE,
4704 named_labels);
4705 /* We need a new label. */
4706 decl = make_label_decl (id, /*local_p=*/0);
4707 /* Now fill in the information we didn't have before. */
4708 TREE_VALUE (named_labels) = decl;
8d08fdba
MS
4709
4710 return decl;
4711}
4712
acef433b 4713/* Declare a local label named ID. */
8d08fdba
MS
4714
4715tree
acef433b
MM
4716declare_local_label (id)
4717 tree id;
8d08fdba 4718{
acef433b 4719 tree decl;
8d08fdba 4720
acef433b
MM
4721 /* Add a new entry to the SHADOWED_LABELS list so that when we leave
4722 this scope we can restore the old value of
4723 IDENTIFIER_TYPE_VALUE. */
4724 current_binding_level->shadowed_labels
4725 = tree_cons (IDENTIFIER_LABEL_VALUE (id), NULL_TREE,
4726 current_binding_level->shadowed_labels);
4727 /* Look for the label. */
4728 decl = make_label_decl (id, /*local_p=*/1);
4729 /* Now fill in the information we didn't have before. */
4730 TREE_VALUE (current_binding_level->shadowed_labels) = decl;
4731
4732 return decl;
8d08fdba
MS
4733}
4734
4735/* Define a label, specifying the location in the source file.
4736 Return the LABEL_DECL node for the label, if the definition is valid.
4737 Otherwise return 0. */
4738
4739tree
4740define_label (filename, line, name)
4741 char *filename;
4742 int line;
4743 tree name;
4744{
f01b0acb 4745 tree decl = lookup_label (name);
8d08fdba
MS
4746
4747 /* After labels, make any new cleanups go into their
4748 own new (temporary) binding contour. */
4749 current_binding_level->more_cleanups_ok = 0;
4750
e1cd6e56 4751 if (name == get_identifier ("wchar_t"))
8251199e 4752 cp_pedwarn ("label named wchar_t");
e1cd6e56 4753
8d08fdba
MS
4754 if (DECL_INITIAL (decl) != NULL_TREE)
4755 {
8251199e 4756 cp_error ("duplicate label `%D'", decl);
8d08fdba
MS
4757 return 0;
4758 }
4759 else
4760 {
e349ee73 4761 struct named_label_list *uses, *prev;
28cbf42c 4762 int identified = 0;
227cf171 4763 int saw_eh = 0;
8d08fdba
MS
4764
4765 /* Mark label as having been defined. */
4766 DECL_INITIAL (decl) = error_mark_node;
4767 /* Say where in the source. */
4768 DECL_SOURCE_FILE (decl) = filename;
4769 DECL_SOURCE_LINE (decl) = line;
4770
e349ee73
MS
4771 prev = NULL;
4772 uses = named_label_uses;
4773 while (uses != NULL)
4774 if (uses->label_decl == decl)
8d08fdba
MS
4775 {
4776 struct binding_level *b = current_binding_level;
4777 while (b)
4778 {
4779 tree new_decls = b->names;
e349ee73
MS
4780 tree old_decls = (b == uses->binding_level)
4781 ? uses->names_in_scope : NULL_TREE;
8d08fdba
MS
4782 while (new_decls != old_decls)
4783 {
4784 if (TREE_CODE (new_decls) == VAR_DECL
4785 /* Don't complain about crossing initialization
4786 of internal entities. They can't be accessed,
4787 and they should be cleaned up
4788 by the time we get to the label. */
700f8a87 4789 && ! DECL_ARTIFICIAL (new_decls)
5524676d
JM
4790 && !(DECL_INITIAL (new_decls) == NULL_TREE
4791 && pod_type_p (TREE_TYPE (new_decls))))
8d08fdba 4792 {
b607c87f
JM
4793 /* This is really only important if we're crossing
4794 an initialization. The POD stuff is just
4795 pedantry; why should it matter if the class
4796 contains a field of pointer to member type? */
4797 int problem = (DECL_INITIAL (new_decls)
4798 || (TYPE_NEEDS_CONSTRUCTING
4799 (TREE_TYPE (new_decls))));
4800
4801 if (! identified)
e349ee73 4802 {
b607c87f
JM
4803 if (problem)
4804 {
4805 cp_error ("jump to label `%D'", decl);
4806 error_with_file_and_line
4807 (uses->filename_o_goto,
4808 uses->lineno_o_goto, " from here");
4809 }
4810 else
4811 {
4812 cp_pedwarn ("jump to label `%D'", decl);
4813 pedwarn_with_file_and_line
4814 (uses->filename_o_goto,
4815 uses->lineno_o_goto, " from here");
4816 }
e349ee73 4817 identified = 1;
227cf171 4818 }
b607c87f
JM
4819
4820 if (problem)
5524676d
JM
4821 cp_error_at (" crosses initialization of `%#D'",
4822 new_decls);
4823 else
b607c87f 4824 cp_pedwarn_at (" enters scope of non-POD `%#D'",
5524676d 4825 new_decls);
8d08fdba
MS
4826 }
4827 new_decls = TREE_CHAIN (new_decls);
4828 }
e349ee73 4829 if (b == uses->binding_level)
8d08fdba 4830 break;
227cf171
JM
4831 if (b->eh_region && ! saw_eh)
4832 {
4833 if (! identified)
4834 {
4835 cp_error ("jump to label `%D'", decl);
4836 error_with_file_and_line
4837 (uses->filename_o_goto,
4838 uses->lineno_o_goto, " from here");
4839 identified = 1;
4840 }
4841 error (" enters exception handling block");
4842 saw_eh = 1;
4843 }
8d08fdba
MS
4844 b = b->level_chain;
4845 }
4846
e349ee73
MS
4847 if (prev != NULL)
4848 prev->next = uses->next;
8d08fdba 4849 else
e349ee73
MS
4850 named_label_uses = uses->next;
4851
4852 uses = uses->next;
4853 }
4854 else
4855 {
4856 prev = uses;
4857 uses = uses->next;
8d08fdba
MS
4858 }
4859 current_function_return_value = NULL_TREE;
4860 return decl;
4861 }
4862}
4863
a5894242
MS
4864struct cp_switch
4865{
4866 struct binding_level *level;
4867 struct cp_switch *next;
4868};
4869
4870static struct cp_switch *switch_stack;
4871
4872void
4873push_switch ()
4874{
4875 struct cp_switch *p
4876 = (struct cp_switch *) oballoc (sizeof (struct cp_switch));
4877 p->level = current_binding_level;
4878 p->next = switch_stack;
4879 switch_stack = p;
4880}
4881
4882void
4883pop_switch ()
4884{
4885 switch_stack = switch_stack->next;
4886}
4887
b0a1da19
JM
4888/* Note that we've seen a definition of a case label, and complain if this
4889 is a bad place for one. */
e92cc029 4890
8d08fdba 4891void
b370501f 4892define_case_label ()
8d08fdba
MS
4893{
4894 tree cleanup = last_cleanup_this_contour ();
a5894242
MS
4895 struct binding_level *b = current_binding_level;
4896 int identified = 0;
4897
b0a1da19
JM
4898 if (! switch_stack)
4899 /* Don't crash; we'll complain in do_case. */
4900 return;
4901
8d08fdba
MS
4902 if (cleanup)
4903 {
4904 static int explained = 0;
8251199e
JM
4905 cp_warning_at ("destructor needed for `%#D'", TREE_PURPOSE (cleanup));
4906 warning ("where case label appears here");
8d08fdba
MS
4907 if (!explained)
4908 {
8251199e
JM
4909 warning ("(enclose actions of previous case statements requiring");
4910 warning ("destructors in their own binding contours.)");
8d08fdba
MS
4911 explained = 1;
4912 }
4913 }
4914
a5894242
MS
4915 for (; b && b != switch_stack->level; b = b->level_chain)
4916 {
4917 tree new_decls = b->names;
4918 for (; new_decls; new_decls = TREE_CHAIN (new_decls))
4919 {
4920 if (TREE_CODE (new_decls) == VAR_DECL
4921 /* Don't complain about crossing initialization
4922 of internal entities. They can't be accessed,
4923 and they should be cleaned up
4924 by the time we get to the label. */
4925 && ! DECL_ARTIFICIAL (new_decls)
4926 && ((DECL_INITIAL (new_decls) != NULL_TREE
4927 && DECL_INITIAL (new_decls) != error_mark_node)
4928 || TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (new_decls))))
4929 {
4930 if (! identified)
8251199e 4931 error ("jump to case label");
a5894242 4932 identified = 1;
8251199e 4933 cp_error_at (" crosses initialization of `%#D'",
a5894242
MS
4934 new_decls);
4935 }
4936 }
4937 }
4938
8d08fdba
MS
4939 /* After labels, make any new cleanups go into their
4940 own new (temporary) binding contour. */
4941
4942 current_binding_level->more_cleanups_ok = 0;
4943 current_function_return_value = NULL_TREE;
4944}
4945\f
4946/* Return the list of declarations of the current level.
4947 Note that this list is in reverse order unless/until
4948 you nreverse it; and when you do nreverse it, you must
4949 store the result back using `storedecls' or you will lose. */
4950
4951tree
4952getdecls ()
4953{
4954 return current_binding_level->names;
4955}
4956
4957/* Return the list of type-tags (for structs, etc) of the current level. */
4958
4959tree
4960gettags ()
4961{
4962 return current_binding_level->tags;
4963}
4964
4965/* Store the list of declarations of the current level.
4966 This is done for the parameter declarations of a function being defined,
4967 after they are modified in the light of any missing parameters. */
4968
4969static void
4970storedecls (decls)
4971 tree decls;
4972{
4973 current_binding_level->names = decls;
4974}
4975
4976/* Similarly, store the list of tags of the current level. */
4977
280f9385 4978void
8d08fdba
MS
4979storetags (tags)
4980 tree tags;
4981{
4982 current_binding_level->tags = tags;
4983}
4984\f
4985/* Given NAME, an IDENTIFIER_NODE,
4986 return the structure (or union or enum) definition for that name.
4987 Searches binding levels from BINDING_LEVEL up to the global level.
4988 If THISLEVEL_ONLY is nonzero, searches only the specified context
4989 (but skips any tag-transparent contexts to find one that is
4990 meaningful for tags).
4991 FORM says which kind of type the caller wants;
4992 it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
4993 If the wrong kind of type is found, and it's not a template, an error is
4994 reported. */
4995
4996static tree
4997lookup_tag (form, name, binding_level, thislevel_only)
4998 enum tree_code form;
8d08fdba 4999 tree name;
cffa8729 5000 struct binding_level *binding_level;
8d08fdba
MS
5001 int thislevel_only;
5002{
5003 register struct binding_level *level;
36a117a5
MM
5004 /* Non-zero if, we should look past a pseudo-global level, even if
5005 THISLEVEL_ONLY. */
5006 int allow_pseudo_global = 1;
8d08fdba
MS
5007
5008 for (level = binding_level; level; level = level->level_chain)
5009 {
5010 register tree tail;
5011 if (ANON_AGGRNAME_P (name))
5012 for (tail = level->tags; tail; tail = TREE_CHAIN (tail))
5013 {
5014 /* There's no need for error checking here, because
5015 anon names are unique throughout the compilation. */
5016 if (TYPE_IDENTIFIER (TREE_VALUE (tail)) == name)
5017 return TREE_VALUE (tail);
5018 }
2c73f9f5
ML
5019 else if (level->namespace_p)
5020 /* Do namespace lookup. */
6c011b01 5021 for (tail = current_namespace; 1; tail = CP_DECL_CONTEXT (tail))
2c73f9f5 5022 {
36a117a5
MM
5023 tree old = binding_for_name (name, tail);
5024
5025 /* If we just skipped past a pseudo global level, even
5026 though THISLEVEL_ONLY, and we find a template class
5027 declaration, then we use the _TYPE node for the
5028 template. See the example below. */
5029 if (thislevel_only && !allow_pseudo_global
5030 && old && BINDING_VALUE (old)
5031 && DECL_CLASS_TEMPLATE_P (BINDING_VALUE (old)))
5032 old = TREE_TYPE (BINDING_VALUE (old));
5033 else
5034 old = BINDING_TYPE (old);
5035
2c73f9f5
ML
5036 /* If it has an original type, it is a typedef, and we
5037 should not return it. */
5038 if (old && DECL_ORIGINAL_TYPE (TYPE_NAME (old)))
5039 old = NULL_TREE;
5040 if (old && TREE_CODE (old) != form
5041 && !(form != ENUMERAL_TYPE && TREE_CODE (old) == TEMPLATE_DECL))
5042 {
8251199e 5043 cp_error ("`%#D' redeclared as %C", old, form);
2c73f9f5
ML
5044 return NULL_TREE;
5045 }
5046 if (old)
5047 return old;
5048 if (thislevel_only || tail == global_namespace)
5049 return NULL_TREE;
5050 }
8d08fdba
MS
5051 else
5052 for (tail = level->tags; tail; tail = TREE_CHAIN (tail))
5053 {
a80e4195 5054 if (TREE_PURPOSE (tail) == name)
8d08fdba
MS
5055 {
5056 enum tree_code code = TREE_CODE (TREE_VALUE (tail));
5057 /* Should tighten this up; it'll probably permit
5058 UNION_TYPE and a struct template, for example. */
5059 if (code != form
5566b478 5060 && !(form != ENUMERAL_TYPE && code == TEMPLATE_DECL))
8d08fdba
MS
5061 {
5062 /* Definition isn't the kind we were looking for. */
8251199e 5063 cp_error ("`%#D' redeclared as %C", TREE_VALUE (tail),
8d08fdba 5064 form);
72b7eeff 5065 return NULL_TREE;
8d08fdba
MS
5066 }
5067 return TREE_VALUE (tail);
5068 }
5069 }
5070 if (thislevel_only && ! level->tag_transparent)
5566b478 5071 {
36a117a5 5072 if (level->pseudo_global && allow_pseudo_global)
5566b478 5073 {
36a117a5
MM
5074 /* We must deal with cases like this:
5075
5076 template <class T> struct S;
5077 template <class T> struct S {};
5078
5079 When looking up `S', for the second declaration, we
5080 would like to find the first declaration. But, we
5081 are in the pseudo-global level created for the
5082 template parameters, rather than the (surrounding)
5083 namespace level. Thus, we keep going one more level,
5084 even though THISLEVEL_ONLY is non-zero. */
5085 allow_pseudo_global = 0;
5086 continue;
5566b478 5087 }
36a117a5
MM
5088 else
5089 return NULL_TREE;
5566b478 5090 }
8d08fdba
MS
5091 }
5092 return NULL_TREE;
5093}
5094
bd6dd845 5095#if 0
8d08fdba
MS
5096void
5097set_current_level_tags_transparency (tags_transparent)
5098 int tags_transparent;
5099{
5100 current_binding_level->tag_transparent = tags_transparent;
5101}
bd6dd845 5102#endif
8d08fdba
MS
5103
5104/* Given a type, find the tag that was defined for it and return the tag name.
5105 Otherwise return 0. However, the value can never be 0
5106 in the cases in which this is used.
5107
5108 C++: If NAME is non-zero, this is the new name to install. This is
5109 done when replacing anonymous tags with real tag names. */
5110
5111static tree
5112lookup_tag_reverse (type, name)
5113 tree type;
5114 tree name;
5115{
5116 register struct binding_level *level;
5117
5118 for (level = current_binding_level; level; level = level->level_chain)
5119 {
5120 register tree tail;
5121 for (tail = level->tags; tail; tail = TREE_CHAIN (tail))
5122 {
5123 if (TREE_VALUE (tail) == type)
5124 {
5125 if (name)
5126 TREE_PURPOSE (tail) = name;
5127 return TREE_PURPOSE (tail);
5128 }
5129 }
5130 }
5131 return NULL_TREE;
5132}
8d08fdba 5133\f
a9aedbc2 5134/* Look up NAME in the NAMESPACE. */
e92cc029 5135
a9aedbc2
MS
5136tree
5137lookup_namespace_name (namespace, name)
5138 tree namespace, name;
5139{
30394414 5140 tree val;
f30c84c9 5141 tree template_id = NULL_TREE;
2c73f9f5 5142
30394414 5143 my_friendly_assert (TREE_CODE (namespace) == NAMESPACE_DECL, 370);
1231fb96 5144
1231fb96 5145 if (TREE_CODE (name) == NAMESPACE_DECL)
d8f8dca1 5146 /* This happens for A::B<int> when B is a namespace. */
1231fb96 5147 return name;
d8f8dca1
MM
5148 else if (TREE_CODE (name) == TEMPLATE_DECL)
5149 {
5150 /* This happens for A::B where B is a template, and there are no
5151 template arguments. */
5152 cp_error ("invalid use of `%D'", name);
5153 return error_mark_node;
5154 }
1231fb96 5155
b262d64c
JM
5156 namespace = ORIGINAL_NAMESPACE (namespace);
5157
f30c84c9
MM
5158 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
5159 {
5160 template_id = name;
5161 name = TREE_OPERAND (name, 0);
5162 if (TREE_CODE (name) == OVERLOAD)
5163 name = DECL_NAME (OVL_CURRENT (name));
5164 else if (TREE_CODE_CLASS (TREE_CODE (name)) == 'd')
5165 name = DECL_NAME (name);
5166 }
5167
30394414
JM
5168 my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, 373);
5169
87e3dbc9 5170 val = make_node (CPLUS_BINDING);
52c11ef6 5171 if (!qualified_lookup_using_namespace (name, namespace, val, 0))
2c73f9f5
ML
5172 return error_mark_node;
5173
5174 if (BINDING_VALUE (val))
1c35f5b6
JM
5175 {
5176 val = BINDING_VALUE (val);
5177
f30c84c9
MM
5178 if (template_id)
5179 {
5180 if (DECL_CLASS_TEMPLATE_P (val))
5181 val = lookup_template_class (val,
5182 TREE_OPERAND (template_id, 1),
5183 /*in_decl=*/NULL_TREE,
5184 /*context=*/NULL_TREE,
5185 /*entering_scope=*/0);
5186 else if (DECL_FUNCTION_TEMPLATE_P (val)
5187 || TREE_CODE (val) == OVERLOAD)
5188 val = lookup_template_function (val,
5189 TREE_OPERAND (template_id, 1));
5190 else
5191 {
5192 cp_error ("`%D::%D' is not a template",
5193 namespace, name);
5194 return error_mark_node;
5195 }
5196 }
5197
1c35f5b6
JM
5198 /* If we have a single function from a using decl, pull it out. */
5199 if (TREE_CODE (val) == OVERLOAD && ! really_overloaded_fn (val))
5200 val = OVL_FUNCTION (val);
5201 return val;
5202 }
5203
8251199e 5204 cp_error ("`%D' undeclared in namespace `%D'", name, namespace);
30394414 5205 return error_mark_node;
a9aedbc2
MS
5206}
5207
7ddedda4
MM
5208/* Hash a TYPENAME_TYPE. K is really of type `tree'. */
5209
5210static unsigned long
5211typename_hash (k)
5212 hash_table_key k;
5213{
5214 unsigned long hash;
5215 tree t;
5216
5217 t = (tree) k;
5218 hash = (((unsigned long) TYPE_CONTEXT (t))
5219 ^ ((unsigned long) DECL_NAME (TYPE_NAME (t))));
5220
5221 return hash;
5222}
5223
5224/* Compare two TYPENAME_TYPEs. K1 and K2 are really of type `tree'. */
5225
5226static boolean
5227typename_compare (k1, k2)
5228 hash_table_key k1;
5229 hash_table_key k2;
5230{
5231 tree t1;
5232 tree t2;
5233 tree d1;
5234 tree d2;
5235
5236 t1 = (tree) k1;
5237 t2 = (tree) k2;
5238 d1 = TYPE_NAME (t1);
5239 d2 = TYPE_NAME (t2);
5240
5241 return (DECL_NAME (d1) == DECL_NAME (d2)
5242 && same_type_p (TYPE_CONTEXT (t1), TYPE_CONTEXT (t2))
5243 && ((TREE_TYPE (t1) != NULL_TREE)
5244 == (TREE_TYPE (t2) != NULL_TREE))
5245 && same_type_p (TREE_TYPE (t1), TREE_TYPE (t2))
5246 && TYPENAME_TYPE_FULLNAME (t1) == TYPENAME_TYPE_FULLNAME (t2));
5247}
5248
45869a6c
MM
5249/* Build a TYPENAME_TYPE. If the type is `typename T::t', CONTEXT is
5250 the type of `T', NAME is the IDENTIFIER_NODE for `t'. If BASE_TYPE
5251 is non-NULL, this type is being created by the implicit typename
5252 extension, and BASE_TYPE is a type named `t' in some base class of
5253 `T' which depends on template parameters.
5254
5255 Returns the new TYPENAME_TYPE. */
5256
5257tree
5258build_typename_type (context, name, fullname, base_type)
5259 tree context;
5260 tree name;
5261 tree fullname;
5262 tree base_type;
5263{
5264 tree t;
5265 tree d;
7ddedda4 5266 struct hash_entry* e;
45869a6c 5267
7ddedda4
MM
5268 static struct hash_table ht;
5269
5270 push_obstacks (&permanent_obstack, &permanent_obstack);
5271
9cd64686
MM
5272 if (!ht.table)
5273 {
5274 static struct hash_table *h = &ht;
5275 if (!hash_table_init (&ht, &hash_newfunc, &typename_hash,
5276 &typename_compare))
5277 fatal ("virtual memory exhausted");
5278 ggc_add_tree_hash_table_root (&h, 1);
5279 }
45869a6c
MM
5280
5281 /* Build the TYPENAME_TYPE. */
5282 t = make_lang_type (TYPENAME_TYPE);
5283 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
5284 TYPENAME_TYPE_FULLNAME (t) = fullname;
5285 TREE_TYPE (t) = base_type;
45869a6c
MM
5286
5287 /* Build the corresponding TYPE_DECL. */
5288 d = build_decl (TYPE_DECL, name, t);
5289 TYPE_NAME (TREE_TYPE (d)) = d;
5290 TYPE_STUB_DECL (TREE_TYPE (d)) = d;
5291 DECL_CONTEXT (d) = FROB_CONTEXT (context);
2642b9bf 5292 DECL_ARTIFICIAL (d) = 1;
45869a6c 5293
7ddedda4
MM
5294 /* See if we already have this type. */
5295 e = hash_lookup (&ht, t, /*create=*/false, /*copy=*/0);
5296 if (e)
87e3dbc9 5297 t = (tree) e->key;
7ddedda4
MM
5298 else
5299 /* Insert the type into the table. */
5300 hash_lookup (&ht, t, /*create=*/true, /*copy=*/0);
5301
5302 pop_obstacks ();
45869a6c
MM
5303
5304 return t;
5305}
5306
5566b478
MS
5307tree
5308make_typename_type (context, name)
5309 tree context, name;
5310{
b2b7d40a 5311 tree fullname;
a80e4195 5312
653cc74a 5313 if (TREE_CODE_CLASS (TREE_CODE (name)) == 't')
78638e24
MM
5314 {
5315 if (!(TYPE_LANG_SPECIFIC (name)
5316 && (CLASSTYPE_IS_TEMPLATE (name)
5317 || CLASSTYPE_USE_TEMPLATE (name))))
5318 name = TYPE_IDENTIFIER (name);
5319 else
5320 /* Create a TEMPLATE_ID_EXPR for the type. */
5321 name = build_nt (TEMPLATE_ID_EXPR,
5322 CLASSTYPE_TI_TEMPLATE (name),
5323 CLASSTYPE_TI_ARGS (name));
5324 }
653cc74a 5325 else if (TREE_CODE (name) == TYPE_DECL)
a80e4195 5326 name = DECL_NAME (name);
b2b7d40a
JM
5327
5328 fullname = name;
5329
5330 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
11686454
JM
5331 {
5332 name = TREE_OPERAND (name, 0);
5333 if (TREE_CODE (name) == TEMPLATE_DECL)
5334 name = TREE_OPERAND (fullname, 0) = DECL_NAME (name);
5335 }
b2b7d40a 5336 if (TREE_CODE (name) != IDENTIFIER_NODE)
a80e4195 5337 my_friendly_abort (2000);
5566b478 5338
04ddee1b
BK
5339 if (TREE_CODE (context) == NAMESPACE_DECL)
5340 {
5341 /* We can get here from typename_sub0 in the explicit_template_type
5342 expansion. Just fail. */
5343 cp_error ("no class template named `%#T' in `%#T'",
5344 name, context);
5345 return error_mark_node;
5346 }
5347
85b71cf2 5348 if (! uses_template_parms (context)
b77ead33 5349 || currently_open_class (context))
5566b478 5350 {
b2b7d40a
JM
5351 if (TREE_CODE (fullname) == TEMPLATE_ID_EXPR)
5352 {
ad810b22 5353 tree tmpl = NULL_TREE;
b2b7d40a 5354 if (IS_AGGR_TYPE (context))
ad810b22
MM
5355 tmpl = lookup_field (context, name, 0, 0);
5356 if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl))
b2b7d40a 5357 {
8251199e 5358 cp_error ("no class template named `%#T' in `%#T'",
b2b7d40a
JM
5359 name, context);
5360 return error_mark_node;
5361 }
ffb690bd 5362
ad810b22
MM
5363 return lookup_template_class (tmpl,
5364 TREE_OPERAND (fullname, 1),
5365 NULL_TREE, context,
5366 /*entering_scope=*/0);
b2b7d40a
JM
5367 }
5368 else
5566b478 5369 {
b4f70b3d
NS
5370 tree t;
5371
5372 if (!IS_AGGR_TYPE (context))
b2b7d40a 5373 {
8251199e 5374 cp_error ("no type named `%#T' in `%#T'", name, context);
b2b7d40a
JM
5375 return error_mark_node;
5376 }
1107c4b3 5377
b4f70b3d 5378 t = lookup_field (context, name, 0, 1);
7d4bdeed
MM
5379 if (t)
5380 return TREE_TYPE (t);
5566b478 5381 }
5566b478 5382 }
11249cf0
MM
5383
5384 /* If the CONTEXT is not a template type, then either the field is
5385 there now or its never going to be. */
b4f70b3d 5386 if (!uses_template_parms (context))
11249cf0
MM
5387 {
5388 cp_error ("no type named `%#T' in `%#T'", name, context);
5389 return error_mark_node;
5390 }
5391
45869a6c
MM
5392
5393 return build_typename_type (context, name, fullname, NULL_TREE);
5566b478
MS
5394}
5395
2c73f9f5
ML
5396/* Select the right _DECL from multiple choices. */
5397
5398static tree
52c11ef6 5399select_decl (binding, flags)
2c73f9f5 5400 tree binding;
52c11ef6 5401 int flags;
2c73f9f5
ML
5402{
5403 tree val;
5404 val = BINDING_VALUE (binding);
52c11ef6 5405 if (LOOKUP_NAMESPACES_ONLY (flags))
3e3f722c
ML
5406 {
5407 /* We are not interested in types. */
5408 if (val && TREE_CODE (val) == NAMESPACE_DECL)
5409 return val;
5410 return NULL_TREE;
5411 }
5412
2c73f9f5
ML
5413 /* If we could have a type and
5414 we have nothing or we need a type and have none. */
5415 if (BINDING_TYPE (binding)
52c11ef6
JM
5416 && (!val || ((flags & LOOKUP_PREFER_TYPES)
5417 && TREE_CODE (val) != TYPE_DECL)))
2c73f9f5
ML
5418 val = TYPE_STUB_DECL (BINDING_TYPE (binding));
5419 /* Don't return non-types if we really prefer types. */
52c11ef6 5420 else if (val && LOOKUP_TYPES_ONLY (flags) && TREE_CODE (val) != TYPE_DECL
c592d5d2
ML
5421 && (TREE_CODE (val) != TEMPLATE_DECL
5422 || !DECL_CLASS_TEMPLATE_P (val)))
2c73f9f5 5423 val = NULL_TREE;
1c35f5b6 5424
2c73f9f5
ML
5425 return val;
5426}
5427
2c169bab
JM
5428/* Unscoped lookup of a global: iterate over current namespaces,
5429 considering using-directives. If SPACESP is non-NULL, store a list
5430 of the namespaces we've considered in it. */
ea9635c7 5431
2c169bab
JM
5432tree
5433unqualified_namespace_lookup (name, flags, spacesp)
ea9635c7 5434 tree name;
52c11ef6 5435 int flags;
2c169bab 5436 tree *spacesp;
ea9635c7 5437{
87e3dbc9 5438 tree b = make_node (CPLUS_BINDING);
ea9635c7
ML
5439 tree initial = current_decl_namespace();
5440 tree scope = initial;
5441 tree siter;
5442 struct binding_level *level;
5443 tree val = NULL_TREE;
5444
2c169bab
JM
5445 if (spacesp)
5446 *spacesp = NULL_TREE;
5447
5448 for (; !val; scope = CP_DECL_CONTEXT (scope))
ea9635c7 5449 {
2c169bab 5450 if (spacesp)
e1b3e07d 5451 *spacesp = tree_cons (scope, NULL_TREE, *spacesp);
ea9635c7
ML
5452 val = binding_for_name (name, scope);
5453
5454 /* Initialize binding for this context. */
5455 BINDING_VALUE (b) = BINDING_VALUE (val);
5456 BINDING_TYPE (b) = BINDING_TYPE (val);
5457
5458 /* Add all _DECLs seen through local using-directives. */
5459 for (level = current_binding_level;
5460 !level->namespace_p;
5461 level = level->level_chain)
52c11ef6 5462 if (!lookup_using_namespace (name, b, level->using_directives,
2c169bab 5463 scope, flags, spacesp))
ea9635c7 5464 /* Give up because of error. */
5b163de4 5465 return error_mark_node;
ea9635c7
ML
5466
5467 /* Add all _DECLs seen through global using-directives. */
5468 /* XXX local and global using lists should work equally. */
5469 siter = initial;
5470 while (1)
5471 {
5472 if (!lookup_using_namespace (name, b, DECL_NAMESPACE_USING (siter),
2c169bab 5473 scope, flags, spacesp))
ea9635c7 5474 /* Give up because of error. */
5b163de4 5475 return error_mark_node;
ea9635c7
ML
5476 if (siter == scope) break;
5477 siter = CP_DECL_CONTEXT (siter);
5478 }
5479
52c11ef6 5480 val = select_decl (b, flags);
ea9635c7
ML
5481 if (scope == global_namespace)
5482 break;
ea9635c7
ML
5483 }
5484 return val;
5485}
5486
52c11ef6
JM
5487/* Combine prefer_type and namespaces_only into flags. */
5488
5489static int
5490lookup_flags (prefer_type, namespaces_only)
5491 int prefer_type, namespaces_only;
5492{
5493 if (namespaces_only)
5494 return LOOKUP_PREFER_NAMESPACES;
5495 if (prefer_type > 1)
5496 return LOOKUP_PREFER_TYPES;
5497 if (prefer_type > 0)
5498 return LOOKUP_PREFER_BOTH;
5499 return 0;
5500}
5501
5502/* Given a lookup that returned VAL, use FLAGS to decide if we want to
5503 ignore it or not. Subroutine of lookup_name_real. */
5504
5505static tree
5506qualify_lookup (val, flags)
5507 tree val;
5508 int flags;
5509{
5510 if (val == NULL_TREE)
5511 return val;
6b945830
JM
5512 if ((flags & LOOKUP_PREFER_NAMESPACES) && TREE_CODE (val) == NAMESPACE_DECL)
5513 return val;
5514 if ((flags & LOOKUP_PREFER_TYPES)
5515 && (TREE_CODE (val) == TYPE_DECL
5516 || ((flags & LOOKUP_TEMPLATES_EXPECTED)
5517 && DECL_CLASS_TEMPLATE_P (val))))
5518 return val;
5519 if (flags & (LOOKUP_PREFER_NAMESPACES | LOOKUP_PREFER_TYPES))
52c11ef6
JM
5520 return NULL_TREE;
5521 return val;
5522}
5523
235f734d
MM
5524/* Any other BINDING overrides an implicit TYPENAME. Warn about
5525 that. */
5526
5527static void
5528warn_about_implicit_typename_lookup (typename, binding)
5529 tree typename;
5530 tree binding;
5531{
5532 tree subtype = TREE_TYPE (TREE_TYPE (typename));
5533 tree name = DECL_NAME (typename);
5534
5535 if (! (TREE_CODE (binding) == TEMPLATE_DECL
5536 && CLASSTYPE_TEMPLATE_INFO (subtype)
5537 && CLASSTYPE_TI_TEMPLATE (subtype) == binding)
5538 && ! (TREE_CODE (binding) == TYPE_DECL
5539 && same_type_p (TREE_TYPE (binding), subtype)))
5540 {
5541 cp_warning ("lookup of `%D' finds `%#D'",
5542 name, binding);
5543 cp_warning (" instead of `%D' from dependent base class",
5544 typename);
5545 cp_warning (" (use `typename %T::%D' if that's what you meant)",
5546 constructor_name (current_class_type), name);
5547 }
5548}
5549
8d08fdba
MS
5550/* Look up NAME in the current binding level and its superiors in the
5551 namespace of variables, functions and typedefs. Return a ..._DECL
5552 node of some kind representing its definition if there is only one
5553 such declaration, or return a TREE_LIST with all the overloaded
5554 definitions if there are many, or return 0 if it is undefined.
5555
2c73f9f5
ML
5556 If PREFER_TYPE is > 0, we prefer TYPE_DECLs or namespaces.
5557 If PREFER_TYPE is > 1, we reject non-type decls (e.g. namespaces).
a28e3c7f 5558 If PREFER_TYPE is -2, we're being called from yylex(). (UGLY)
f84b4be9
JM
5559 Otherwise we prefer non-TYPE_DECLs.
5560
5561 If NONCLASS is non-zero, we don't look for the NAME in class scope,
5562 using IDENTIFIER_CLASS_VALUE. */
8d08fdba 5563
824b9a4c 5564static tree
3e3f722c 5565lookup_name_real (name, prefer_type, nonclass, namespaces_only)
8d08fdba 5566 tree name;
3e3f722c 5567 int prefer_type, nonclass, namespaces_only;
8d08fdba 5568{
235f734d
MM
5569 tree t;
5570 tree val = NULL_TREE;
a28e3c7f 5571 int yylex = 0;
e1cd6e56 5572 tree from_obj = NULL_TREE;
52c11ef6 5573 int flags;
235f734d 5574 int val_is_implicit_typename = 0;
8d08fdba 5575
3e3f722c
ML
5576 /* Hack: copy flag set by parser, if set. */
5577 if (only_namespace_names)
5578 namespaces_only = 1;
52c11ef6 5579
a28e3c7f
MS
5580 if (prefer_type == -2)
5581 {
5582 extern int looking_for_typename;
fc378698 5583 tree type = NULL_TREE;
a28e3c7f
MS
5584
5585 yylex = 1;
5586 prefer_type = looking_for_typename;
e1cd6e56 5587
52c11ef6 5588 flags = lookup_flags (prefer_type, namespaces_only);
52c11ef6
JM
5589 /* If the next thing is '<', class templates are types. */
5590 if (looking_for_template)
5591 flags |= LOOKUP_TEMPLATES_EXPECTED;
5592
653cc74a
JM
5593 /* std:: becomes :: for now. */
5594 if (got_scope == std_node)
5595 got_scope = void_type_node;
5596
e1cd6e56
MS
5597 if (got_scope)
5598 type = got_scope;
dff6b454 5599 else if (got_object != error_mark_node)
e1cd6e56 5600 type = got_object;
a28e3c7f 5601
e1cd6e56 5602 if (type)
a28e3c7f 5603 {
e1cd6e56 5604 if (type == error_mark_node)
f376e137 5605 return error_mark_node;
a80e4195
MS
5606 if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type))
5607 type = TREE_TYPE (type);
5566b478 5608
2b9dc906
JM
5609 if (TYPE_P (type))
5610 type = complete_type (type);
5566b478 5611
a1774733 5612 if (TREE_CODE (type) == VOID_TYPE)
2c73f9f5
ML
5613 type = global_namespace;
5614 if (TREE_CODE (type) == NAMESPACE_DECL)
a9aedbc2 5615 {
87e3dbc9 5616 val = make_node (CPLUS_BINDING);
6ad07332 5617 flags |= LOOKUP_COMPLAIN;
52c11ef6 5618 if (!qualified_lookup_using_namespace (name, type, val, flags))
2c73f9f5 5619 return NULL_TREE;
52c11ef6 5620 val = select_decl (val, flags);
a9aedbc2 5621 }
5566b478 5622 else if (! IS_AGGR_TYPE (type)
5156628f 5623 || TREE_CODE (type) == TEMPLATE_TYPE_PARM
73b0fce8 5624 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM
5156628f 5625 || TREE_CODE (type) == TYPENAME_TYPE)
e92cc029 5626 /* Someone else will give an error about this if needed. */
a28e3c7f 5627 val = NULL_TREE;
e1cd6e56 5628 else if (type == current_class_type)
a28e3c7f 5629 val = IDENTIFIER_CLASS_VALUE (name);
a28e3c7f 5630 else
d23a1bb1 5631 val = lookup_member (type, name, 0, prefer_type);
a28e3c7f 5632 }
e1cd6e56
MS
5633 else
5634 val = NULL_TREE;
5635
594740f3 5636 if (got_scope)
e1cd6e56 5637 goto done;
594740f3 5638 else if (got_object && val)
e1cd6e56 5639 from_obj = val;
a28e3c7f 5640 }
52c11ef6 5641 else
5b163de4
JM
5642 {
5643 flags = lookup_flags (prefer_type, namespaces_only);
5644 /* If we're not parsing, we need to complain. */
5645 flags |= LOOKUP_COMPLAIN;
5646 }
e76a2646 5647
d8f8dca1 5648 /* First, look in non-namespace scopes. */
6f1b4c42
JM
5649
5650 if (current_class_type == NULL_TREE)
5651 nonclass = 1;
5652
235f734d 5653 for (t = IDENTIFIER_BINDING (name); t; t = TREE_CHAIN (t))
d8f8dca1 5654 {
235f734d
MM
5655 tree binding;
5656
5657 if (!LOCAL_BINDING_P (t) && nonclass)
d8f8dca1
MM
5658 /* We're not looking for class-scoped bindings, so keep going. */
5659 continue;
5660
5661 /* If this is the kind of thing we're looking for, we're done. */
235f734d
MM
5662 if (qualify_lookup (BINDING_VALUE (t), flags))
5663 binding = BINDING_VALUE (t);
d8f8dca1 5664 else if ((flags & LOOKUP_PREFER_TYPES)
235f734d
MM
5665 && qualify_lookup (BINDING_TYPE (t), flags))
5666 binding = BINDING_TYPE (t);
5667 else
5668 binding = NULL_TREE;
5669
5670 if (binding
83233dca 5671 && (!val || !IMPLICIT_TYPENAME_TYPE_DECL_P (binding)))
d8f8dca1 5672 {
235f734d
MM
5673 if (val_is_implicit_typename && !yylex)
5674 warn_about_implicit_typename_lookup (val, binding);
5675 val = binding;
5676 val_is_implicit_typename
83233dca 5677 = IMPLICIT_TYPENAME_TYPE_DECL_P (val);
235f734d
MM
5678 if (!val_is_implicit_typename)
5679 break;
d8f8dca1
MM
5680 }
5681 }
f181d4ae 5682
235f734d
MM
5683 /* Now lookup in namespace scopes. */
5684 if (!val || val_is_implicit_typename)
e76a2646 5685 {
2c169bab 5686 t = unqualified_namespace_lookup (name, flags, 0);
235f734d 5687 if (t)
c1def683 5688 {
235f734d
MM
5689 if (val_is_implicit_typename && !yylex)
5690 warn_about_implicit_typename_lookup (val, t);
5691 val = t;
c1def683
JM
5692 }
5693 }
5694
a28e3c7f 5695 done:
8d08fdba
MS
5696 if (val)
5697 {
c91a56d2 5698 /* This should only warn about types used in qualified-ids. */
e1cd6e56 5699 if (from_obj && from_obj != val)
5566b478 5700 {
c91a56d2
MS
5701 if (looking_for_typename && TREE_CODE (from_obj) == TYPE_DECL
5702 && TREE_CODE (val) == TYPE_DECL
5703 && TREE_TYPE (from_obj) != TREE_TYPE (val))
5704 {
8251199e 5705 cp_pedwarn ("lookup of `%D' in the scope of `%#T' (`%#T')",
c91a56d2 5706 name, got_object, TREE_TYPE (from_obj));
8251199e 5707 cp_pedwarn (" does not match lookup in the current scope (`%#T')",
c91a56d2
MS
5708 TREE_TYPE (val));
5709 }
594740f3 5710
b8b1a3c1
JM
5711 /* We don't change val to from_obj if got_object depends on
5712 template parms because that breaks implicit typename for
5713 destructor calls. */
5714 if (! uses_template_parms (got_object))
53fdbb3b 5715 val = from_obj;
5566b478 5716 }
e1cd6e56 5717
0c64a9ca
JM
5718 /* If we have a single function from a using decl, pull it out. */
5719 if (TREE_CODE (val) == OVERLOAD && ! really_overloaded_fn (val))
5720 val = OVL_FUNCTION (val);
8d08fdba 5721 }
e1cd6e56
MS
5722 else if (from_obj)
5723 val = from_obj;
8d08fdba
MS
5724
5725 return val;
5726}
5727
700f8a87
MS
5728tree
5729lookup_name_nonclass (name)
5730 tree name;
5731{
3e3f722c 5732 return lookup_name_real (name, 0, 1, 0);
700f8a87
MS
5733}
5734
2c73f9f5
ML
5735tree
5736lookup_function_nonclass (name, args)
5737 tree name;
5738 tree args;
5739{
5740 return lookup_arg_dependent (name, lookup_name_nonclass (name), args);
5741}
5742
3e3f722c
ML
5743tree
5744lookup_name_namespace_only (name)
5745 tree name;
5746{
5747 /* type-or-namespace, nonclass, namespace_only */
5748 return lookup_name_real (name, 1, 1, 1);
5749}
5750
700f8a87
MS
5751tree
5752lookup_name (name, prefer_type)
5753 tree name;
5754 int prefer_type;
5755{
3e3f722c 5756 return lookup_name_real (name, prefer_type, 0, 0);
700f8a87
MS
5757}
5758
a7d2d407
MM
5759/* Similar to `lookup_name' but look only in the innermost non-class
5760 binding level. */
8d08fdba
MS
5761
5762tree
5763lookup_name_current_level (name)
5764 tree name;
5765{
a7d2d407
MM
5766 struct binding_level *b;
5767 tree t = NULL_TREE;
8d08fdba 5768
a7d2d407
MM
5769 b = current_binding_level;
5770 while (b->parm_flag == 2)
5771 b = b->level_chain;
5772
5773 if (b->namespace_p)
8d08fdba 5774 {
5fdaba89 5775 t = IDENTIFIER_NAMESPACE_VALUE (name);
8d08fdba
MS
5776
5777 /* extern "C" function() */
5778 if (t != NULL_TREE && TREE_CODE (t) == TREE_LIST)
5779 t = TREE_VALUE (t);
5780 }
f181d4ae
MM
5781 else if (IDENTIFIER_BINDING (name)
5782 && LOCAL_BINDING_P (IDENTIFIER_BINDING (name)))
8d08fdba 5783 {
a4443a08
MS
5784 while (1)
5785 {
f181d4ae
MM
5786 if (BINDING_LEVEL (IDENTIFIER_BINDING (name)) == b)
5787 return IDENTIFIER_VALUE (name);
5788
9ed182dc
JM
5789 if (b->keep == 2)
5790 b = b->level_chain;
5791 else
5792 break;
5793 }
5794 }
5795
5796 return t;
5797}
5798
5799/* Like lookup_name_current_level, but for types. */
5800
5801tree
5802lookup_type_current_level (name)
5803 tree name;
5804{
5805 register tree t = NULL_TREE;
5806
5807 my_friendly_assert (! current_binding_level->namespace_p, 980716);
5808
5809 if (REAL_IDENTIFIER_TYPE_VALUE (name) != NULL_TREE
5810 && REAL_IDENTIFIER_TYPE_VALUE (name) != global_type_node)
5811 {
5812 struct binding_level *b = current_binding_level;
5813 while (1)
5814 {
5815 if (purpose_member (name, b->type_shadowed))
5816 return REAL_IDENTIFIER_TYPE_VALUE (name);
a4443a08
MS
5817 if (b->keep == 2)
5818 b = b->level_chain;
5819 else
5820 break;
5821 }
8d08fdba
MS
5822 }
5823
5824 return t;
5825}
3e3f722c
ML
5826
5827void
5828begin_only_namespace_names ()
5829{
5830 only_namespace_names = 1;
5831}
5832
5833void
5834end_only_namespace_names ()
5835{
5836 only_namespace_names = 0;
5837}
8d08fdba
MS
5838\f
5839/* Arrange for the user to get a source line number, even when the
5840 compiler is going down in flames, so that she at least has a
5841 chance of working around problems in the compiler. We used to
5842 call error(), but that let the segmentation fault continue
5843 through; now, it's much more passive by asking them to send the
5844 maintainers mail about the problem. */
5845
5846static void
5847signal_catch (sig)
7dee3f36 5848 int sig ATTRIBUTE_UNUSED;
8d08fdba
MS
5849{
5850 signal (SIGSEGV, SIG_DFL);
5851#ifdef SIGIOT
5852 signal (SIGIOT, SIG_DFL);
5853#endif
5854#ifdef SIGILL
5855 signal (SIGILL, SIG_DFL);
5856#endif
5857#ifdef SIGABRT
5858 signal (SIGABRT, SIG_DFL);
5859#endif
5860#ifdef SIGBUS
5861 signal (SIGBUS, SIG_DFL);
5862#endif
5863 my_friendly_abort (0);
5864}
5865
8d08fdba
MS
5866/* Push the declarations of builtin types into the namespace.
5867 RID_INDEX, if < RID_MAX is the index of the builtin type
5868 in the array RID_POINTERS. NAME is the name used when looking
5869 up the builtin type. TYPE is the _TYPE node for the builtin type. */
5870
5871static void
5872record_builtin_type (rid_index, name, type)
5873 enum rid rid_index;
d8e178a0 5874 const char *name;
8d08fdba
MS
5875 tree type;
5876{
5877 tree rname = NULL_TREE, tname = NULL_TREE;
a703fb38 5878 tree tdecl = NULL_TREE;
8d08fdba
MS
5879
5880 if ((int) rid_index < (int) RID_MAX)
5881 rname = ridpointers[(int) rid_index];
5882 if (name)
5883 tname = get_identifier (name);
5884
5885 TYPE_BUILT_IN (type) = 1;
5886
5887 if (tname)
5888 {
8d08fdba 5889 tdecl = pushdecl (build_decl (TYPE_DECL, tname, type));
8d08fdba
MS
5890 set_identifier_type_value (tname, NULL_TREE);
5891 if ((int) rid_index < (int) RID_MAX)
2c73f9f5
ML
5892 /* Built-in types live in the global namespace. */
5893 SET_IDENTIFIER_GLOBAL_VALUE (tname, tdecl);
8d08fdba
MS
5894 }
5895 if (rname != NULL_TREE)
5896 {
5897 if (tname != NULL_TREE)
5898 {
5899 set_identifier_type_value (rname, NULL_TREE);
2c73f9f5 5900 SET_IDENTIFIER_GLOBAL_VALUE (rname, tdecl);
8d08fdba
MS
5901 }
5902 else
5903 {
8d08fdba 5904 tdecl = pushdecl (build_decl (TYPE_DECL, rname, type));
8d08fdba
MS
5905 set_identifier_type_value (rname, NULL_TREE);
5906 }
5907 }
8d08fdba
MS
5908}
5909
eff71ab0
PB
5910/* Record one of the standard Java types.
5911 * Declare it as having the given NAME.
5912 * If SIZE > 0, it is the size of one of the integral types;
5913 * otherwise it is the negative of the size of one of the other types. */
5914
5915static tree
5916record_builtin_java_type (name, size)
d8e178a0 5917 const char *name;
eff71ab0
PB
5918 int size;
5919{
5920 tree type, decl;
5921 if (size > 0)
5922 type = make_signed_type (size);
5923 else if (size > -32)
5924 { /* "__java_char" or ""__java_boolean". */
5925 type = make_unsigned_type (-size);
5926 /*if (size == -1) TREE_SET_CODE (type, BOOLEAN_TYPE);*/
5927 }
5928 else
5929 { /* "__java_float" or ""__java_double". */
5930 type = make_node (REAL_TYPE);
5931 TYPE_PRECISION (type) = - size;
5932 layout_type (type);
5933 }
5934 record_builtin_type (RID_MAX, name, type);
5935 decl = TYPE_NAME (type);
e229f2cd
PB
5936
5937 /* Suppress generate debug symbol entries for these types,
5938 since for normal C++ they are just clutter.
5939 However, push_lang_context undoes this if extern "Java" is seen. */
eff71ab0 5940 DECL_IGNORED_P (decl) = 1;
e229f2cd 5941
eff71ab0
PB
5942 TYPE_FOR_JAVA (type) = 1;
5943 return type;
5944}
5945
036407f7
ML
5946/* Push a type into the namespace so that the back-ends ignore it. */
5947
5948static void
5949record_unknown_type (type, name)
5950 tree type;
d8e178a0 5951 const char *name;
036407f7
ML
5952{
5953 tree decl = pushdecl (build_decl (TYPE_DECL, get_identifier (name), type));
5954 /* Make sure the "unknown type" typedecl gets ignored for debug info. */
5955 DECL_IGNORED_P (decl) = 1;
5956 TYPE_DECL_SUPPRESS_DEBUG (decl) = 1;
5957 TYPE_SIZE (type) = TYPE_SIZE (void_type_node);
5958 TYPE_ALIGN (type) = 1;
5959 TYPE_MODE (type) = TYPE_MODE (void_type_node);
5960}
5961
8d08fdba
MS
5962/* Push overloaded decl, in global scope, with one argument so it
5963 can be used as a callback from define_function. */
e92cc029 5964
8d08fdba
MS
5965static void
5966push_overloaded_decl_1 (x)
5967 tree x;
5968{
7bdbfa05 5969 push_overloaded_decl (x, PUSH_GLOBAL);
8d08fdba
MS
5970}
5971
8ccc31eb
MS
5972#ifdef __GNUC__
5973__inline
5974#endif
6b5fbb55 5975tree
c2a37c55 5976auto_function (name, type)
8ccc31eb 5977 tree name, type;
8ccc31eb
MS
5978{
5979 return define_function
c2a37c55 5980 (IDENTIFIER_POINTER (name), type, push_overloaded_decl_1,
8ccc31eb
MS
5981 IDENTIFIER_POINTER (build_decl_overload (name, TYPE_ARG_TYPES (type),
5982 0)));
5983}
5984
8d08fdba
MS
5985/* Create the predefined scalar types of C,
5986 and some nodes representing standard constants (0, 1, (void *)0).
5987 Initialize the global binding level.
5988 Make definitions for built-in primitive functions. */
5989
5990void
5991init_decl_processing ()
5992{
8d08fdba 5993 tree fields[20];
8d08fdba 5994 int wchar_type_size;
8d08fdba
MS
5995 tree array_domain_type;
5996
5997 /* Have to make these distinct before we try using them. */
5998 lang_name_cplusplus = get_identifier ("C++");
5999 lang_name_c = get_identifier ("C");
a1774733 6000 lang_name_java = get_identifier ("Java");
8d08fdba 6001
99dccabc
MM
6002 /* Let the back-end now how to save and restore language-specific
6003 per-function globals. */
a8f73d4b
MM
6004 init_lang_status = &push_cp_function_context;
6005 free_lang_status = &pop_cp_function_context;
b4b8bee7 6006 mark_lang_status = &mark_cp_function_context;
99dccabc 6007
fc6af6e3
RH
6008 cp_parse_init ();
6009 init_decl2 ();
9cd64686 6010 init_pt ();
fc6af6e3 6011
9cd64686
MM
6012 /* Create the global variables. */
6013 push_to_top_level ();
8012c983 6014
2c73f9f5 6015 /* Enter the global namespace. */
30394414 6016 my_friendly_assert (global_namespace == NULL_TREE, 375);
30394414
JM
6017 push_namespace (get_identifier ("::"));
6018 global_namespace = current_namespace;
6019 current_lang_name = NULL_TREE;
6020
e1cd6e56 6021 if (flag_strict_prototype == 2)
830fcda8 6022 flag_strict_prototype = pedantic;
2642b9bf
JM
6023 if (! flag_permissive && ! pedantic)
6024 flag_pedantic_errors = 1;
830fcda8
JM
6025
6026 strict_prototypes_lang_c = flag_strict_prototype;
8926095f 6027
8d08fdba
MS
6028 /* Initially, C. */
6029 current_lang_name = lang_name_c;
6030
6031 current_function_decl = NULL_TREE;
8d08fdba
MS
6032 current_binding_level = NULL_BINDING_LEVEL;
6033 free_binding_level = NULL_BINDING_LEVEL;
6034
6035 /* Because most segmentation signals can be traced back into user
6036 code, catch them and at least give the user a chance of working
e92cc029 6037 around compiler bugs. */
8d08fdba
MS
6038 signal (SIGSEGV, signal_catch);
6039
6040 /* We will also catch aborts in the back-end through signal_catch and
6041 give the user a chance to see where the error might be, and to defeat
6042 aborts in the back-end when there have been errors previously in their
e92cc029 6043 code. */
8d08fdba
MS
6044#ifdef SIGIOT
6045 signal (SIGIOT, signal_catch);
6046#endif
6047#ifdef SIGILL
6048 signal (SIGILL, signal_catch);
6049#endif
6050#ifdef SIGABRT
6051 signal (SIGABRT, signal_catch);
6052#endif
6053#ifdef SIGBUS
6054 signal (SIGBUS, signal_catch);
6055#endif
6056
6057 gcc_obstack_init (&decl_obstack);
8d08fdba 6058
81b3411c
BS
6059 build_common_tree_nodes (flag_signed_char);
6060
8d08fdba
MS
6061 error_mark_list = build_tree_list (error_mark_node, error_mark_node);
6062 TREE_TYPE (error_mark_list) = error_mark_node;
6063
a28e3c7f
MS
6064 /* Make the binding_level structure for global names. */
6065 pushlevel (0);
8d08fdba 6066 global_binding_level = current_binding_level;
2c73f9f5
ML
6067 /* The global level is the namespace level of ::. */
6068 NAMESPACE_LEVEL (global_namespace) = global_binding_level;
6069 declare_namespace_level ();
8d08fdba
MS
6070
6071 this_identifier = get_identifier (THIS_NAME);
6072 in_charge_identifier = get_identifier (IN_CHARGE_NAME);
fc378698
MS
6073 ctor_identifier = get_identifier (CTOR_NAME);
6074 dtor_identifier = get_identifier (DTOR_NAME);
8d08fdba
MS
6075 pfn_identifier = get_identifier (VTABLE_PFN_NAME);
6076 index_identifier = get_identifier (VTABLE_INDEX_NAME);
6077 delta_identifier = get_identifier (VTABLE_DELTA_NAME);
6078 delta2_identifier = get_identifier (VTABLE_DELTA2_NAME);
6079 pfn_or_delta2_identifier = get_identifier ("__pfn_or_delta2");
6080
6081 /* Define `int' and `char' first so that dbx will output them first. */
8d08fdba 6082 record_builtin_type (RID_INT, NULL_PTR, integer_type_node);
8d08fdba
MS
6083 record_builtin_type (RID_CHAR, "char", char_type_node);
6084
45075bf3
NS
6085 /* `signed' is the same as `int' */
6086 record_builtin_type (RID_SIGNED, NULL_PTR, integer_type_node);
8d08fdba 6087 record_builtin_type (RID_LONG, "long int", long_integer_type_node);
8d08fdba 6088 record_builtin_type (RID_UNSIGNED, "unsigned int", unsigned_type_node);
8d08fdba
MS
6089 record_builtin_type (RID_MAX, "long unsigned int", long_unsigned_type_node);
6090 record_builtin_type (RID_MAX, "unsigned long", long_unsigned_type_node);
8d08fdba 6091 record_builtin_type (RID_MAX, "long long int", long_long_integer_type_node);
8d08fdba
MS
6092 record_builtin_type (RID_MAX, "long long unsigned int",
6093 long_long_unsigned_type_node);
6094 record_builtin_type (RID_MAX, "long long unsigned",
6095 long_long_unsigned_type_node);
5156628f 6096 record_builtin_type (RID_SHORT, "short int", short_integer_type_node);
5156628f
MS
6097 record_builtin_type (RID_MAX, "short unsigned int", short_unsigned_type_node);
6098 record_builtin_type (RID_MAX, "unsigned short", short_unsigned_type_node);
6099
8d08fdba
MS
6100 ptrdiff_type_node
6101 = TREE_TYPE (IDENTIFIER_GLOBAL_VALUE (get_identifier (PTRDIFF_TYPE)));
6102
8d08fdba 6103 /* Define both `signed char' and `unsigned char'. */
8d08fdba 6104 record_builtin_type (RID_MAX, "signed char", signed_char_type_node);
8d08fdba
MS
6105 record_builtin_type (RID_MAX, "unsigned char", unsigned_char_type_node);
6106
81b3411c
BS
6107 /* `unsigned long' is the standard type for sizeof.
6108 Note that stddef.h uses `unsigned long',
6109 and this must agree, even if long and int are the same size. */
6110 set_sizetype
6111 (TREE_TYPE (IDENTIFIER_GLOBAL_VALUE (get_identifier (SIZE_TYPE))));
6112
835f9b4d
GRK
6113 /* Create the widest literal types. */
6114 widest_integer_literal_type_node = make_signed_type (HOST_BITS_PER_WIDE_INT * 2);
6115 pushdecl (build_decl (TYPE_DECL, NULL_TREE,
6116 widest_integer_literal_type_node));
6117
6118 widest_unsigned_literal_type_node = make_unsigned_type (HOST_BITS_PER_WIDE_INT * 2);
6119 pushdecl (build_decl (TYPE_DECL, NULL_TREE,
6120 widest_unsigned_literal_type_node));
6121
8d08fdba 6122 /* These are types that type_for_size and type_for_mode use. */
8d08fdba 6123 pushdecl (build_decl (TYPE_DECL, NULL_TREE, intQI_type_node));
8d08fdba 6124 pushdecl (build_decl (TYPE_DECL, NULL_TREE, intHI_type_node));
8d08fdba 6125 pushdecl (build_decl (TYPE_DECL, NULL_TREE, intSI_type_node));
8d08fdba 6126 pushdecl (build_decl (TYPE_DECL, NULL_TREE, intDI_type_node));
946dc1c8 6127#if HOST_BITS_PER_WIDE_INT >= 64
5ebcdddb 6128 pushdecl (build_decl (TYPE_DECL, get_identifier ("__int128_t"), intTI_type_node));
946dc1c8 6129#endif
8d08fdba 6130 pushdecl (build_decl (TYPE_DECL, NULL_TREE, unsigned_intQI_type_node));
8d08fdba 6131 pushdecl (build_decl (TYPE_DECL, NULL_TREE, unsigned_intHI_type_node));
8d08fdba 6132 pushdecl (build_decl (TYPE_DECL, NULL_TREE, unsigned_intSI_type_node));
8d08fdba 6133 pushdecl (build_decl (TYPE_DECL, NULL_TREE, unsigned_intDI_type_node));
946dc1c8 6134#if HOST_BITS_PER_WIDE_INT >= 64
5ebcdddb 6135 pushdecl (build_decl (TYPE_DECL, get_identifier ("__uint128_t"), unsigned_intTI_type_node));
946dc1c8 6136#endif
8d08fdba 6137
81b3411c 6138 build_common_tree_nodes_2 (flag_short_double);
37c46b43 6139
eff71ab0
PB
6140 java_byte_type_node = record_builtin_java_type ("__java_byte", 8);
6141 java_short_type_node = record_builtin_java_type ("__java_short", 16);
6142 java_int_type_node = record_builtin_java_type ("__java_int", 32);
6143 java_long_type_node = record_builtin_java_type ("__java_long", 64);
6144 java_float_type_node = record_builtin_java_type ("__java_float", -32);
6145 java_double_type_node = record_builtin_java_type ("__java_double", -64);
6146 java_char_type_node = record_builtin_java_type ("__java_char", -16);
6147 java_boolean_type_node = record_builtin_java_type ("__java_boolean", -1);
6148
8d08fdba
MS
6149 integer_two_node = build_int_2 (2, 0);
6150 TREE_TYPE (integer_two_node) = integer_type_node;
6151 integer_three_node = build_int_2 (3, 0);
6152 TREE_TYPE (integer_three_node) = integer_type_node;
8d08fdba 6153
255512c1
JM
6154 boolean_type_node = make_unsigned_type (BOOL_TYPE_SIZE);
6155 TREE_SET_CODE (boolean_type_node, BOOLEAN_TYPE);
c1ea6a0b
BK
6156 TYPE_MAX_VALUE (boolean_type_node) = build_int_2 (1, 0);
6157 TREE_TYPE (TYPE_MAX_VALUE (boolean_type_node)) = boolean_type_node;
6158 TYPE_PRECISION (boolean_type_node) = 1;
255512c1
JM
6159 record_builtin_type (RID_BOOL, "bool", boolean_type_node);
6160 boolean_false_node = build_int_2 (0, 0);
6161 TREE_TYPE (boolean_false_node) = boolean_type_node;
6162 boolean_true_node = build_int_2 (1, 0);
6163 TREE_TYPE (boolean_true_node) = boolean_type_node;
2986ae00 6164
81b3411c
BS
6165 signed_size_zero_node = build_int_2 (0, 0); record_builtin_type (RID_FLOAT, NULL_PTR, float_type_node);
6166 record_builtin_type (RID_DOUBLE, NULL_PTR, double_type_node);
6167 record_builtin_type (RID_MAX, "long double", long_double_type_node);
6168
6169 pushdecl (build_decl (TYPE_DECL, get_identifier ("complex int"),
6170 complex_integer_type_node));
6171 pushdecl (build_decl (TYPE_DECL, get_identifier ("complex float"),
6172 complex_float_type_node));
6173 pushdecl (build_decl (TYPE_DECL, get_identifier ("complex double"),
6174 complex_double_type_node));
6175 pushdecl (build_decl (TYPE_DECL, get_identifier ("complex long double"),
6176 complex_long_double_type_node));
8d08fdba 6177
e92cc029
MS
6178 TREE_TYPE (signed_size_zero_node) = make_signed_type (TYPE_PRECISION (sizetype));
6179
8d08fdba 6180 record_builtin_type (RID_VOID, NULL_PTR, void_type_node);
8d08fdba
MS
6181 void_list_node = build_tree_list (NULL_TREE, void_type_node);
6182 TREE_PARMLIST (void_list_node) = 1;
6183
8d08fdba
MS
6184 /* Used for expressions that do nothing, but are not errors. */
6185 void_zero_node = build_int_2 (0, 0);
6186 TREE_TYPE (void_zero_node) = void_type_node;
6187
6188 string_type_node = build_pointer_type (char_type_node);
beb53fb8 6189 const_string_type_node
91063b51
MM
6190 = build_pointer_type (build_qualified_type (char_type_node,
6191 TYPE_QUAL_CONST));
4cc1d462 6192 empty_except_spec = build_tree_list (NULL_TREE, NULL_TREE);
6b5fbb55 6193#if 0
8d08fdba 6194 record_builtin_type (RID_MAX, NULL_PTR, string_type_node);
6b5fbb55 6195#endif
8d08fdba
MS
6196
6197 /* Make a type to be the domain of a few array types
6198 whose domains don't really matter.
6199 200 is small enough that it always fits in size_t
6200 and large enough that it can hold most function names for the
6201 initializations of __FUNCTION__ and __PRETTY_FUNCTION__. */
6202 array_domain_type = build_index_type (build_int_2 (200, 0));
6203
2c73f9f5 6204 /* Make a type for arrays of characters.
8d08fdba
MS
6205 With luck nothing will ever really depend on the length of this
6206 array type. */
6207 char_array_type_node
6208 = build_array_type (char_type_node, array_domain_type);
6209 /* Likewise for arrays of ints. */
6210 int_array_type_node
6211 = build_array_type (integer_type_node, array_domain_type);
6212
6213 /* This is just some anonymous class type. Nobody should ever
6214 need to look inside this envelope. */
6215 class_star_type_node = build_pointer_type (make_lang_type (RECORD_TYPE));
6216
7f4edbcb
BS
6217 if (flag_huge_objects)
6218 delta_type_node = long_integer_type_node;
6219 else
6220 delta_type_node = short_integer_type_node;
6221
8d08fdba
MS
6222 default_function_type
6223 = build_function_type (integer_type_node, NULL_TREE);
8d08fdba
MS
6224
6225 ptr_type_node = build_pointer_type (void_type_node);
beb53fb8 6226 const_ptr_type_node
91063b51 6227 = build_pointer_type (build_qualified_type (void_type_node,
7f4edbcb
BS
6228 TYPE_QUAL_CONST));
6229 c_common_nodes_and_builtins (1, flag_no_builtin, flag_no_nonansi_builtin);
6230
824b9a4c 6231 void_ftype_ptr
4cc1d462 6232 = build_exception_variant (void_ftype_ptr, empty_except_spec);
8d08fdba 6233
8d08fdba
MS
6234 /* C++ extensions */
6235
6236 unknown_type_node = make_node (UNKNOWN_TYPE);
036407f7
ML
6237 record_unknown_type (unknown_type_node, "unknown type");
6238
8d08fdba
MS
6239 /* Indirecting an UNKNOWN_TYPE node yields an UNKNOWN_TYPE node. */
6240 TREE_TYPE (unknown_type_node) = unknown_type_node;
a6967cc0 6241
03d0f4af 6242 TREE_TYPE (null_node) = type_for_size (POINTER_SIZE, 0);
a6967cc0
JM
6243
6244 /* Looking up TYPE_POINTER_TO and TYPE_REFERENCE_TO yield the same
6245 result. */
8d08fdba
MS
6246 TYPE_POINTER_TO (unknown_type_node) = unknown_type_node;
6247 TYPE_REFERENCE_TO (unknown_type_node) = unknown_type_node;
6248
e92cc029 6249 /* This is special for C++ so functions can be overloaded. */
5096c664
JM
6250 wchar_type_node = get_identifier (flag_short_wchar
6251 ? "short unsigned int"
6252 : WCHAR_TYPE);
6253 wchar_type_node = TREE_TYPE (IDENTIFIER_GLOBAL_VALUE (wchar_type_node));
8d08fdba
MS
6254 wchar_type_size = TYPE_PRECISION (wchar_type_node);
6255 signed_wchar_type_node = make_signed_type (wchar_type_size);
6256 unsigned_wchar_type_node = make_unsigned_type (wchar_type_size);
6257 wchar_type_node
6258 = TREE_UNSIGNED (wchar_type_node)
6259 ? unsigned_wchar_type_node
6260 : signed_wchar_type_node;
6261 record_builtin_type (RID_WCHAR, "__wchar_t", wchar_type_node);
6262
f376e137
MS
6263 /* Artificial declaration of wchar_t -- can be bashed */
6264 wchar_decl_node = build_decl (TYPE_DECL, get_identifier ("wchar_t"),
6265 wchar_type_node);
6266 pushdecl (wchar_decl_node);
6267
8d08fdba
MS
6268 /* This is for wide string constants. */
6269 wchar_array_type_node
6270 = build_array_type (wchar_type_node, array_domain_type);
6271
8926095f 6272 if (flag_vtable_thunks)
700f8a87
MS
6273 {
6274 /* Make sure we get a unique function type, so we can give
6275 its pointer type a name. (This wins for gdb.) */
6276 tree vfunc_type = make_node (FUNCTION_TYPE);
6277 TREE_TYPE (vfunc_type) = integer_type_node;
6278 TYPE_ARG_TYPES (vfunc_type) = NULL_TREE;
6279 layout_type (vfunc_type);
6280
6281 vtable_entry_type = build_pointer_type (vfunc_type);
6282 }
8926095f 6283 else
700f8a87
MS
6284 {
6285 vtable_entry_type = make_lang_type (RECORD_TYPE);
4ce3d537
MM
6286 fields[0] = build_lang_decl (FIELD_DECL, delta_identifier,
6287 delta_type_node);
6288 fields[1] = build_lang_decl (FIELD_DECL, index_identifier,
6289 delta_type_node);
6290 fields[2] = build_lang_decl (FIELD_DECL, pfn_identifier,
6291 ptr_type_node);
700f8a87
MS
6292 finish_builtin_type (vtable_entry_type, VTBL_PTR_TYPE, fields, 2,
6293 double_type_node);
6294
6295 /* Make this part of an invisible union. */
6296 fields[3] = copy_node (fields[2]);
6297 TREE_TYPE (fields[3]) = delta_type_node;
6298 DECL_NAME (fields[3]) = delta2_identifier;
6299 DECL_MODE (fields[3]) = TYPE_MODE (delta_type_node);
6300 DECL_SIZE (fields[3]) = TYPE_SIZE (delta_type_node);
6301 TREE_UNSIGNED (fields[3]) = 0;
6302 TREE_CHAIN (fields[2]) = fields[3];
91063b51
MM
6303 vtable_entry_type = build_qualified_type (vtable_entry_type,
6304 TYPE_QUAL_CONST);
700f8a87
MS
6305 }
6306 record_builtin_type (RID_MAX, VTBL_PTR_TYPE, vtable_entry_type);
8d08fdba 6307
8d08fdba 6308 vtbl_type_node
52bf7d5d 6309 = build_cplus_array_type (vtable_entry_type, NULL_TREE);
8d08fdba 6310 layout_type (vtbl_type_node);
91063b51 6311 vtbl_type_node = build_qualified_type (vtbl_type_node, TYPE_QUAL_CONST);
8d08fdba 6312 record_builtin_type (RID_MAX, NULL_PTR, vtbl_type_node);
849da744
MM
6313 vtbl_ptr_type_node = build_pointer_type (vtable_entry_type);
6314 layout_type (vtbl_ptr_type_node);
6315 record_builtin_type (RID_MAX, NULL_PTR, vtbl_ptr_type_node);
8d08fdba 6316
2c73f9f5
ML
6317 std_node = build_decl (NAMESPACE_DECL,
6318 get_identifier (flag_honor_std ? "fake std":"std"),
6633d636
MS
6319 void_type_node);
6320 pushdecl (std_node);
6321
2c73f9f5 6322 global_type_node = make_node (LANG_TYPE);
036407f7 6323 record_unknown_type (global_type_node, "global type");
2c73f9f5 6324
db5ae43f
MS
6325 /* Now, C++. */
6326 current_lang_name = lang_name_cplusplus;
8d08fdba 6327
ced78d8b 6328 {
2c73f9f5
ML
6329 tree bad_alloc_type_node, newtype, deltype;
6330 if (flag_honor_std)
6331 push_namespace (get_identifier ("std"));
6332 bad_alloc_type_node = xref_tag
ca107ded 6333 (class_type_node, get_identifier ("bad_alloc"), 1);
2c73f9f5
ML
6334 if (flag_honor_std)
6335 pop_namespace ();
6336 newtype = build_exception_variant
4cc1d462
NS
6337 (ptr_ftype_sizetype, add_exception_specifier (NULL_TREE, bad_alloc_type_node, -1));
6338 deltype = build_exception_variant (void_ftype_ptr, empty_except_spec);
c2a37c55
BS
6339 auto_function (ansi_opname[(int) NEW_EXPR], newtype);
6340 auto_function (ansi_opname[(int) VEC_NEW_EXPR], newtype);
6341 global_delete_fndecl = auto_function (ansi_opname[(int) DELETE_EXPR],
6342 deltype);
6343 auto_function (ansi_opname[(int) VEC_DELETE_EXPR], deltype);
ced78d8b 6344 }
8d08fdba
MS
6345
6346 abort_fndecl
c2a37c55 6347 = define_function ("__pure_virtual", void_ftype, 0, 0);
8d08fdba 6348
8d08fdba
MS
6349 /* Perform other language dependent initializations. */
6350 init_class_processing ();
6351 init_init_processing ();
6352 init_search_processing ();
1737fe20
BK
6353 if (flag_rtti)
6354 init_rtti_processing ();
8d08fdba 6355
6467930b 6356 if (flag_exceptions)
8d2733ca 6357 init_exception_processing ();
8d08fdba
MS
6358 if (flag_no_inline)
6359 {
6360 flag_inline_functions = 0;
8d08fdba 6361 }
9e9ff709 6362
7fcdf4c2 6363 if (! supports_one_only ())
72b7eeff 6364 flag_weak = 0;
8d08fdba
MS
6365
6366 /* Create the global bindings for __FUNCTION__ and __PRETTY_FUNCTION__. */
6367 declare_function_name ();
6368
6369 /* Prepare to check format strings against argument lists. */
6370 init_function_format_info ();
e9a25f70
JL
6371
6372 /* Show we use EH for cleanups. */
6373 using_eh_for_cleanups ();
62c154ed
JM
6374
6375 print_error_function = lang_print_error_function;
501ba25a 6376 lang_get_alias_set = &c_get_alias_set;
e5dc5fb2 6377 valid_lang_attribute = cp_valid_lang_attribute;
d9cf7c82
JM
6378
6379 /* Maintain consistency. Perhaps we should just complain if they
6380 say -fwritable-strings? */
6381 if (flag_writable_strings)
6382 flag_const_strings = 0;
fc6af6e3
RH
6383
6384 /* Add GC roots for all of our global variables. */
6385 ggc_add_tree_root (c_global_trees, sizeof c_global_trees / sizeof(tree));
6386 ggc_add_tree_root (cp_global_trees, sizeof cp_global_trees / sizeof(tree));
fc6af6e3
RH
6387 ggc_add_tree_root (&integer_three_node, 1);
6388 ggc_add_tree_root (&integer_two_node, 1);
fc6af6e3 6389 ggc_add_tree_root (&signed_size_zero_node, 1);
fc6af6e3
RH
6390 ggc_add_tree_root (&size_one_node, 1);
6391 ggc_add_tree_root (&size_zero_node, 1);
fc6af6e3
RH
6392 ggc_add_root (&global_binding_level, 1, sizeof global_binding_level,
6393 mark_binding_level);
9cd64686 6394 ggc_add_root (&scope_chain, 1, sizeof scope_chain, &mark_saved_scope);
fc6af6e3
RH
6395 ggc_add_tree_root (&static_ctors, 1);
6396 ggc_add_tree_root (&static_dtors, 1);
9cd64686 6397 ggc_add_tree_root (&lastiddecl, 1);
fc6af6e3
RH
6398
6399 ggc_add_tree_root (&enum_next_value, 1);
fc6af6e3
RH
6400 ggc_add_tree_root (&last_function_parm_tags, 1);
6401 ggc_add_tree_root (&current_function_return_value, 1);
6402 ggc_add_tree_root (&current_function_parms, 1);
6403 ggc_add_tree_root (&current_function_parm_tags, 1);
9cd64686 6404 ggc_add_tree_root (&last_function_parms, 1);
fc6af6e3 6405 ggc_add_tree_root (&error_mark_list, 1);
9cd64686 6406
fc6af6e3 6407 ggc_add_tree_root (&global_namespace, 1);
fc6af6e3
RH
6408 ggc_add_tree_root (&global_type_node, 1);
6409 ggc_add_tree_root (&anonymous_namespace_name, 1);
9cd64686
MM
6410
6411 ggc_add_tree_root (&got_object, 1);
6412 ggc_add_tree_root (&got_scope, 1);
6413
6414 ggc_add_tree_root (&current_lang_name, 1);
6415 ggc_add_tree_root (&static_aggregates, 1);
62c154ed
JM
6416}
6417
6418/* Function to print any language-specific context for an error message. */
6419
6420static void
6421lang_print_error_function (file)
8df4696d 6422 const char *file;
62c154ed
JM
6423{
6424 default_print_error_function (file);
6425 maybe_print_template_context ();
8d08fdba
MS
6426}
6427
6428/* Make a definition for a builtin function named NAME and whose data type
6429 is TYPE. TYPE should be a function type with argument types.
8d08fdba
MS
6430
6431 If LIBRARY_NAME is nonzero, use that for DECL_ASSEMBLER_NAME,
6432 the name to be called if we can't opencode the function. */
6433
6434tree
c2a37c55 6435define_function (name, type, pfn, library_name)
d8e178a0 6436 const char *name;
8d08fdba 6437 tree type;
49c249e1 6438 void (*pfn) PROTO((tree));
d8e178a0 6439 const char *library_name;
8d08fdba
MS
6440{
6441 tree decl = build_lang_decl (FUNCTION_DECL, get_identifier (name), type);
6442 DECL_EXTERNAL (decl) = 1;
6443 TREE_PUBLIC (decl) = 1;
863adfc0 6444 DECL_ARTIFICIAL (decl) = 1;
8d08fdba 6445
2c73f9f5 6446 my_friendly_assert (DECL_CONTEXT (decl) == NULL_TREE, 392);
cb0dbb9a 6447 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
2c73f9f5 6448
8d08fdba
MS
6449 /* Since `pushdecl' relies on DECL_ASSEMBLER_NAME instead of DECL_NAME,
6450 we cannot change DECL_ASSEMBLER_NAME until we have installed this
6451 function in the namespace. */
6452 if (pfn) (*pfn) (decl);
6453 if (library_name)
6454 DECL_ASSEMBLER_NAME (decl) = get_identifier (library_name);
6455 make_function_rtl (decl);
8d08fdba
MS
6456 return decl;
6457}
7f4edbcb 6458
c2a37c55
BS
6459
6460/* Wrapper around define_function, for the benefit of
6461 c_common_nodes_and_builtins.
6462 FUNCTION_CODE tells later passes how to compile calls to this function.
6463 See tree.h for its possible values. */
6464
7f4edbcb
BS
6465tree
6466builtin_function (name, type, code, libname)
6467 const char *name;
6468 tree type;
6469 enum built_in_function code;
6470 const char *libname;
6471{
c2a37c55
BS
6472 tree decl = define_function (name, type, (void (*) PROTO((tree)))pushdecl,
6473 libname);
6474 if (code != NOT_BUILT_IN)
6475 {
6476 DECL_BUILT_IN (decl) = 1;
6477 DECL_FUNCTION_CODE (decl) = code;
6478 }
6479 return decl;
7f4edbcb 6480}
8d08fdba 6481\f
61a127b3
MM
6482/* When we call finish_struct for an anonymous union, we create
6483 default copy constructors and such. But, an anonymous union
6484 shouldn't have such things; this function undoes the damage to the
6485 anonymous union type T.
6486
6487 (The reason that we create the synthesized methods is that we don't
6488 distinguish `union { int i; }' from `typedef union { int i; } U'.
6489 The first is an anonymous union; the second is just an ordinary
6490 union type.) */
6491
6492void
6bdb8141 6493fixup_anonymous_aggr (t)
61a127b3
MM
6494 tree t;
6495{
6496 tree *q;
6497
6498 /* Wipe out memory of synthesized methods */
6499 TYPE_HAS_CONSTRUCTOR (t) = 0;
6500 TYPE_HAS_DEFAULT_CONSTRUCTOR (t) = 0;
6501 TYPE_HAS_INIT_REF (t) = 0;
6502 TYPE_HAS_CONST_INIT_REF (t) = 0;
6503 TYPE_HAS_ASSIGN_REF (t) = 0;
61a127b3
MM
6504 TYPE_HAS_CONST_ASSIGN_REF (t) = 0;
6505
6506 /* Splice the implicitly generated functions out of the TYPE_METHODS
6507 list. */
6508 q = &TYPE_METHODS (t);
6509 while (*q)
6510 {
6511 if (DECL_ARTIFICIAL (*q))
6512 *q = TREE_CHAIN (*q);
6513 else
6514 q = &TREE_CHAIN (*q);
6515 }
6516
6517 /* ANSI C++ June 5 1992 WP 9.5.3. Anonymous unions may not have
6518 function members. */
6519 if (TYPE_METHODS (t))
6520 error ("an anonymous union cannot have function members");
6521}
6522
72a93143
JM
6523/* Make sure that a declaration with no declarator is well-formed, i.e.
6524 just defines a tagged type or anonymous union.
8d08fdba 6525
72a93143 6526 Returns the type defined, if any. */
8d08fdba 6527
72a93143
JM
6528tree
6529check_tag_decl (declspecs)
8d08fdba
MS
6530 tree declspecs;
6531{
72a93143 6532 int found_type = 0;
2986ae00 6533 tree ob_modifier = NULL_TREE;
8d08fdba 6534 register tree link;
8d08fdba
MS
6535 register tree t = NULL_TREE;
6536
6537 for (link = declspecs; link; link = TREE_CHAIN (link))
6538 {
6539 register tree value = TREE_VALUE (link);
6540
72a93143 6541 if (TYPE_P (value))
8d08fdba 6542 {
72a93143 6543 ++found_type;
5566b478 6544
72a93143
JM
6545 if (IS_AGGR_TYPE (value) || TREE_CODE (value) == ENUMERAL_TYPE)
6546 {
6547 my_friendly_assert (TYPE_MAIN_DECL (value) != NULL_TREE, 261);
6548 t = value;
6549 }
8d08fdba 6550 }
83f660b7
JM
6551 else if (value == ridpointers[(int) RID_FRIEND])
6552 {
83f660b7
JM
6553 if (current_class_type == NULL_TREE
6554 || current_scope () != current_class_type)
6555 ob_modifier = value;
6556 }
8d08fdba 6557 else if (value == ridpointers[(int) RID_STATIC]
2986ae00
MS
6558 || value == ridpointers[(int) RID_EXTERN]
6559 || value == ridpointers[(int) RID_AUTO]
28cbf42c
MS
6560 || value == ridpointers[(int) RID_REGISTER]
6561 || value == ridpointers[(int) RID_INLINE]
6562 || value == ridpointers[(int) RID_VIRTUAL]
72a93143
JM
6563 || value == ridpointers[(int) RID_CONST]
6564 || value == ridpointers[(int) RID_VOLATILE]
28cbf42c 6565 || value == ridpointers[(int) RID_EXPLICIT])
2986ae00 6566 ob_modifier = value;
8d08fdba
MS
6567 }
6568
72a93143
JM
6569 if (found_type > 1)
6570 error ("multiple types in one declaration");
7e2067ca
JM
6571
6572 /* Inside a class, we might be in a friend or access declaration.
6573 Until we have a good way of detecting the latter, don't warn. */
6574 if (t == NULL_TREE && ! current_class_type)
6575 pedwarn ("declaration does not declare anything");
0dd3962d
JM
6576
6577 /* Check for an anonymous union. We're careful
6578 accessing TYPE_IDENTIFIER because some built-in types, like
6579 pointer-to-member types, do not have TYPE_NAME. */
6bdb8141 6580 else if (t && IS_AGGR_TYPE_CODE (TREE_CODE (t))
0dd3962d
JM
6581 && TYPE_NAME (t)
6582 && ANON_AGGRNAME_P (TYPE_IDENTIFIER (t)))
6583 {
6584 /* Anonymous unions are objects, so they can have specifiers. */;
6bdb8141
JM
6585 SET_ANON_AGGR_TYPE_P (t);
6586
6587 if (TREE_CODE (t) != UNION_TYPE && pedantic && ! in_system_header)
6588 pedwarn ("ISO C++ prohibits anonymous structs");
0dd3962d
JM
6589 }
6590
83f660b7 6591 else if (ob_modifier)
8d08fdba 6592 {
83f660b7
JM
6593 if (ob_modifier == ridpointers[(int) RID_INLINE]
6594 || ob_modifier == ridpointers[(int) RID_VIRTUAL])
6595 cp_error ("`%D' can only be specified for functions", ob_modifier);
6596 else if (ob_modifier == ridpointers[(int) RID_FRIEND])
6597 cp_error ("`%D' can only be specified inside a class", ob_modifier);
6598 else if (ob_modifier == ridpointers[(int) RID_EXPLICIT])
6599 cp_error ("`%D' can only be specified for constructors",
6600 ob_modifier);
6601 else
6602 cp_error ("`%D' can only be specified for objects and functions",
6603 ob_modifier);
72a93143 6604 }
8d08fdba 6605
72a93143
JM
6606 return t;
6607}
6608
6609/* Called when a declaration is seen that contains no names to declare.
6610 If its type is a reference to a structure, union or enum inherited
6611 from a containing scope, shadow that tag name for the current scope
6612 with a forward reference.
6613 If its type defines a new named structure or union
6614 or defines an enum, it is valid but we need not do anything here.
6615 Otherwise, it is an error.
6616
6617 C++: may have to grok the declspecs to learn about static,
6618 complain for anonymous unions. */
6619
6620void
6621shadow_tag (declspecs)
6622 tree declspecs;
6623{
6624 tree t = check_tag_decl (declspecs);
6625
6626 if (t)
6627 maybe_process_partial_specialization (t);
6628
6629 /* This is where the variables in an anonymous union are
6630 declared. An anonymous union declaration looks like:
6631 union { ... } ;
6632 because there is no declarator after the union, the parser
6633 sends that declaration here. */
6bdb8141 6634 if (t && ANON_AGGR_TYPE_P (t))
72a93143 6635 {
6bdb8141 6636 fixup_anonymous_aggr (t);
72a93143
JM
6637
6638 if (TYPE_FIELDS (t))
6639 {
6640 tree decl = grokdeclarator (NULL_TREE, declspecs, NORMAL, 0,
6641 NULL_TREE);
6642 finish_anon_union (decl);
6643 }
8d08fdba
MS
6644 }
6645}
6646\f
6647/* Decode a "typename", such as "int **", returning a ..._TYPE node. */
6648
6649tree
6650groktypename (typename)
6651 tree typename;
6652{
6653 if (TREE_CODE (typename) != TREE_LIST)
6654 return typename;
6655 return grokdeclarator (TREE_VALUE (typename),
6656 TREE_PURPOSE (typename),
c11b6f21 6657 TYPENAME, 0, NULL_TREE);
8d08fdba
MS
6658}
6659
6660/* Decode a declarator in an ordinary declaration or data definition.
6661 This is called as soon as the type information and variable name
6662 have been parsed, before parsing the initializer if any.
6663 Here we create the ..._DECL node, fill in its type,
6664 and put it on the list of decls for the current context.
6665 The ..._DECL node is returned as the value.
6666
6667 Exception: for arrays where the length is not specified,
82580166 6668 the type is left null, to be filled in by `cp_finish_decl'.
8d08fdba
MS
6669
6670 Function definitions do not come here; they go to start_function
6671 instead. However, external and forward declarations of functions
6672 do go through here. Structure field declarations are done by
6673 grokfield and not through here. */
6674
6675/* Set this to zero to debug not using the temporary obstack
6676 to parse initializers. */
6677int debug_temp_inits = 1;
6678
6679tree
a1774733 6680start_decl (declarator, declspecs, initialized, attributes, prefix_attributes)
8d08fdba
MS
6681 tree declarator, declspecs;
6682 int initialized;
a1774733 6683 tree attributes, prefix_attributes;
8d08fdba
MS
6684{
6685 register tree decl;
6686 register tree type, tem;
6687 tree context;
6688 extern int have_extern_spec;
6689 extern int used_extern_spec;
b17e2870 6690 tree attrlist;
8d08fdba 6691
5566b478
MS
6692#if 0
6693 /* See code below that used this. */
8d08fdba 6694 int init_written = initialized;
5566b478 6695#endif
8d08fdba 6696
e92cc029 6697 /* This should only be done once on the top most decl. */
8d08fdba
MS
6698 if (have_extern_spec && !used_extern_spec)
6699 {
a28e3c7f
MS
6700 declspecs = decl_tree_cons (NULL_TREE, get_identifier ("extern"),
6701 declspecs);
8d08fdba
MS
6702 used_extern_spec = 1;
6703 }
6704
b17e2870
JM
6705 if (attributes || prefix_attributes)
6706 attrlist = build_scratch_list (attributes, prefix_attributes);
6707 else
6708 attrlist = NULL_TREE;
6709
c11b6f21 6710 decl = grokdeclarator (declarator, declspecs, NORMAL, initialized,
b17e2870
JM
6711 attrlist);
6712
a1774733 6713 if (decl == NULL_TREE || TREE_CODE (decl) == VOID_TYPE)
8d08fdba
MS
6714 return NULL_TREE;
6715
6716 type = TREE_TYPE (decl);
6717
44689c12
ML
6718 if (type == error_mark_node)
6719 return NULL_TREE;
6720
8d08fdba
MS
6721 context
6722 = (TREE_CODE (decl) == FUNCTION_DECL && DECL_VIRTUAL_P (decl))
6723 ? DECL_CLASS_CONTEXT (decl)
6724 : DECL_CONTEXT (decl);
6725
9a68c51f
JM
6726 if (initialized && context && TREE_CODE (context) == NAMESPACE_DECL
6727 && context != current_namespace && TREE_CODE (decl) == VAR_DECL)
6728 {
6729 /* When parsing the initializer, lookup should use the object's
6730 namespace. */
6731 push_decl_namespace (context);
6732 }
6733
2c73f9f5
ML
6734 /* We are only interested in class contexts, later. */
6735 if (context && TREE_CODE (context) == NAMESPACE_DECL)
6736 context = NULL_TREE;
6737
8d08fdba
MS
6738 if (initialized)
6739 /* Is it valid for this decl to have an initializer at all?
6740 If not, set INITIALIZED to zero, which will indirectly
82580166 6741 tell `cp_finish_decl' to ignore the initializer once it is parsed. */
8d08fdba
MS
6742 switch (TREE_CODE (decl))
6743 {
6744 case TYPE_DECL:
6745 /* typedef foo = bar means give foo the same type as bar.
82580166 6746 We haven't parsed bar yet, so `cp_finish_decl' will fix that up.
8d08fdba
MS
6747 Any other case of an initialization in a TYPE_DECL is an error. */
6748 if (pedantic || list_length (declspecs) > 1)
6749 {
8251199e 6750 cp_error ("typedef `%D' is initialized", decl);
8d08fdba
MS
6751 initialized = 0;
6752 }
6753 break;
6754
6755 case FUNCTION_DECL:
8251199e 6756 cp_error ("function `%#D' is initialized like a variable", decl);
8d08fdba
MS
6757 initialized = 0;
6758 break;
6759
6760 default:
3e41d13b 6761 break;
8d08fdba
MS
6762 }
6763
8d08fdba
MS
6764 if (initialized)
6765 {
a9aedbc2 6766 if (! toplevel_bindings_p ()
8d08fdba 6767 && DECL_EXTERNAL (decl))
8251199e 6768 cp_warning ("declaration of `%#D' has `extern' and is initialized",
8d08fdba
MS
6769 decl);
6770 DECL_EXTERNAL (decl) = 0;
5566b478 6771 if (toplevel_bindings_p ())
8d08fdba
MS
6772 TREE_STATIC (decl) = 1;
6773
6774 /* Tell `pushdecl' this is an initialized decl
6775 even though we don't yet have the initializer expression.
82580166 6776 Also tell `cp_finish_decl' it may store the real initializer. */
8d08fdba
MS
6777 DECL_INITIAL (decl) = error_mark_node;
6778 }
6779
fa20888b
MK
6780#ifdef SET_DEFAULT_DECL_ATTRIBUTES
6781 SET_DEFAULT_DECL_ATTRIBUTES (decl, attributes);
6782#endif
6783
6784 /* Set attributes here so if duplicate decl, will have proper attributes. */
6785 cplus_decl_attributes (decl, attributes, prefix_attributes);
6786
5566b478 6787 if (context && TYPE_SIZE (complete_type (context)) != NULL_TREE)
5b605f68 6788 {
6b400b21 6789 push_nested_class (context, 2);
e97e5263 6790
5b605f68
MS
6791 if (TREE_CODE (decl) == VAR_DECL)
6792 {
6793 tree field = lookup_field (context, DECL_NAME (decl), 0, 0);
6794 if (field == NULL_TREE || TREE_CODE (field) != VAR_DECL)
8251199e 6795 cp_error ("`%#D' is not a static member of `%#T'", decl, context);
e349ee73
MS
6796 else
6797 {
6798 if (DECL_CONTEXT (field) != context)
f2d773a2 6799 {
8251199e 6800 cp_pedwarn ("ANSI C++ does not permit `%T::%D' to be defined as `%T::%D'",
f2d773a2
JM
6801 DECL_CONTEXT (field), DECL_NAME (decl),
6802 context, DECL_NAME (decl));
6803 DECL_CONTEXT (decl) = DECL_CONTEXT (field);
6804 }
75650646
MM
6805 /* Static data member are tricky; an in-class initialization
6806 still doesn't provide a definition, so the in-class
6807 declaration will have DECL_EXTERNAL set, but will have an
6808 initialization. Thus, duplicate_decls won't warn
6809 about this situation, and so we check here. */
6810 if (DECL_INITIAL (decl) && DECL_INITIAL (field))
8251199e 6811 cp_error ("duplicate initialization of %D", decl);
e349ee73
MS
6812 if (duplicate_decls (decl, field))
6813 decl = field;
6814 }
5b605f68 6815 }
f30432d7
MS
6816 else
6817 {
5566b478 6818 tree field = check_classfn (context, decl);
f30432d7
MS
6819 if (field && duplicate_decls (decl, field))
6820 decl = field;
6821 }
6822
6823 /* cp_finish_decl sets DECL_EXTERNAL if DECL_IN_AGGR_P is set. */
dff3e828
BK
6824 DECL_IN_AGGR_P (decl) = 0;
6825 if ((DECL_LANG_SPECIFIC (decl) && DECL_USE_TEMPLATE (decl))
6826 || CLASSTYPE_USE_TEMPLATE (context))
84e6233f
JM
6827 {
6828 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
6829 /* [temp.expl.spec] An explicit specialization of a static data
6830 member of a template is a definition if the declaration
6831 includes an initializer; otherwise, it is a declaration.
6832
6833 We check for processing_specialization so this only applies
6834 to the new specialization syntax. */
6835 if (DECL_INITIAL (decl) == NULL_TREE && processing_specialization)
6836 DECL_EXTERNAL (decl) = 1;
6837 }
f30432d7 6838
b7698cf0 6839 if (DECL_EXTERNAL (decl) && ! DECL_TEMPLATE_SPECIALIZATION (decl))
8251199e 6840 cp_pedwarn ("declaration of `%#D' outside of class is not definition",
f30432d7 6841 decl);
5b605f68
MS
6842 }
6843
9188c363
MM
6844 /* Enter this declaration into the symbol table. */
6845 tem = maybe_push_decl (decl);
2ee887f2 6846
5156628f 6847 if (processing_template_decl)
5566b478 6848 {
9188c363
MM
6849 if (at_function_scope_p ())
6850 push_permanent_obstack ();
9188c363 6851 tem = push_template_decl (tem);
9188c363 6852 if (at_function_scope_p ())
24bef158 6853 pop_obstacks ();
5566b478
MS
6854 }
6855
6856
2ee887f2 6857#if ! defined (ASM_OUTPUT_BSS) && ! defined (ASM_OUTPUT_ALIGNED_BSS)
a3203465 6858 /* Tell the back-end to use or not use .common as appropriate. If we say
a50f0918
MS
6859 -fconserve-space, we want this to save .data space, at the expense of
6860 wrong semantics. If we say -fno-conserve-space, we want this to
6861 produce errors about redefs; to do this we force variables into the
6862 data segment. */
a3203465 6863 DECL_COMMON (tem) = flag_conserve_space || ! TREE_PUBLIC (tem);
2ee887f2 6864#endif
3a846e6e 6865
5156628f 6866 if (! processing_template_decl)
5566b478 6867 start_decl_1 (tem);
8d08fdba 6868
5566b478
MS
6869 /* Corresponding pop_obstacks is done in `cp_finish_decl'. */
6870 push_obstacks_nochange ();
8d08fdba 6871
8d08fdba
MS
6872 return tem;
6873}
6874
5566b478
MS
6875void
6876start_decl_1 (decl)
6877 tree decl;
8d08fdba 6878{
5566b478
MS
6879 tree type = TREE_TYPE (decl);
6880 int initialized = (DECL_INITIAL (decl) != NULL_TREE);
8d08fdba 6881
44689c12
ML
6882 if (type == error_mark_node)
6883 return;
6884
5566b478
MS
6885 /* If this type of object needs a cleanup, and control may
6886 jump past it, make a new binding level so that it is cleaned
6887 up only when it is initialized first. */
6888 if (TYPE_NEEDS_DESTRUCTOR (type)
6889 && current_binding_level->more_cleanups_ok == 0)
6890 pushlevel_temporary (1);
6891
6892 if (initialized)
6893 /* Is it valid for this decl to have an initializer at all?
6894 If not, set INITIALIZED to zero, which will indirectly
6895 tell `cp_finish_decl' to ignore the initializer once it is parsed. */
8d08fdba 6896 {
5566b478
MS
6897 /* Don't allow initializations for incomplete types except for
6898 arrays which might be completed by the initialization. */
44689c12 6899 if (TYPE_SIZE (complete_type (type)) != NULL_TREE)
5566b478
MS
6900 ; /* A complete type is ok. */
6901 else if (TREE_CODE (type) != ARRAY_TYPE)
8d08fdba 6902 {
8251199e 6903 cp_error ("variable `%#D' has initializer but incomplete type",
5566b478
MS
6904 decl);
6905 initialized = 0;
25eb19ff 6906 type = TREE_TYPE (decl) = error_mark_node;
5566b478
MS
6907 }
6908 else if (TYPE_SIZE (complete_type (TREE_TYPE (type))) == NULL_TREE)
6909 {
6910 if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
8251199e 6911 cp_error ("elements of array `%#D' have incomplete type", decl);
5566b478
MS
6912 /* else we already gave an error in start_decl. */
6913 initialized = 0;
8d08fdba 6914 }
8d08fdba
MS
6915 }
6916
5566b478
MS
6917 if (!initialized
6918 && TREE_CODE (decl) != TYPE_DECL
6919 && TREE_CODE (decl) != TEMPLATE_DECL
6920 && IS_AGGR_TYPE (type) && ! DECL_EXTERNAL (decl))
8d08fdba 6921 {
5156628f 6922 if ((! processing_template_decl || ! uses_template_parms (type))
7fcdf4c2 6923 && TYPE_SIZE (complete_type (type)) == NULL_TREE)
5566b478 6924 {
8251199e 6925 cp_error ("aggregate `%#D' has incomplete type and cannot be initialized",
5566b478
MS
6926 decl);
6927 /* Change the type so that assemble_variable will give
6928 DECL an rtl we can live with: (mem (const_int 0)). */
25eb19ff 6929 type = TREE_TYPE (decl) = error_mark_node;
5566b478
MS
6930 }
6931 else
6932 {
6933 /* If any base type in the hierarchy of TYPE needs a constructor,
6934 then we set initialized to 1. This way any nodes which are
6935 created for the purposes of initializing this aggregate
6936 will live as long as it does. This is necessary for global
6937 aggregates which do not have their initializers processed until
6938 the end of the file. */
6939 initialized = TYPE_NEEDS_CONSTRUCTING (type);
6940 }
6941 }
6942
5566b478
MS
6943 if (! initialized)
6944 DECL_INITIAL (decl) = NULL_TREE;
6945}
6946
6947/* Handle initialization of references.
38e01259 6948 These three arguments are from `cp_finish_decl', and have the
e92cc029
MS
6949 same meaning here that they do there.
6950
6951 Quotes on semantics can be found in ARM 8.4.3. */
6952
5566b478 6953static void
a703fb38 6954grok_reference_init (decl, type, init)
5566b478 6955 tree decl, type, init;
5566b478
MS
6956{
6957 tree tmp;
6958
6959 if (init == NULL_TREE)
6960 {
6961 if ((DECL_LANG_SPECIFIC (decl) == 0
6962 || DECL_IN_AGGR_P (decl) == 0)
6963 && ! DECL_THIS_EXTERN (decl))
ed5511d9 6964 cp_error ("`%D' declared as reference but not initialized", decl);
5566b478
MS
6965 return;
6966 }
6967
6968 if (init == error_mark_node)
6969 return;
6970
ed5511d9 6971 if (TREE_CODE (init) == CONSTRUCTOR)
5566b478 6972 {
8251199e 6973 cp_error ("ANSI C++ forbids use of initializer list to initialize reference `%D'", decl);
8d08fdba
MS
6974 return;
6975 }
6976
6977 if (TREE_CODE (init) == TREE_LIST)
6978 init = build_compound_expr (init);
8d08fdba 6979
8ccc31eb
MS
6980 if (TREE_CODE (TREE_TYPE (init)) == REFERENCE_TYPE)
6981 init = convert_from_reference (init);
6982
8d08fdba
MS
6983 if (TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE
6984 && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE)
6985 {
a3203465 6986 /* Note: default conversion is only called in very special cases. */
8d08fdba
MS
6987 init = default_conversion (init);
6988 }
24bef158
MM
6989
6990 /* Convert INIT to the reference type TYPE. This may involve the
6991 creation of a temporary, whose lifetime must be the same as that
6992 of the reference. If so, a DECL_STMT for the temporary will be
6993 added just after the DECL_STMT for DECL. That's why we don't set
6994 DECL_INITIAL for local references (instead assigning to them
6995 explicitly); we need to allow the temporary to be initialized
6996 first. */
a3203465 6997 tmp = convert_to_reference
9a3b49ac
MS
6998 (type, init, CONV_IMPLICIT,
6999 LOOKUP_SPECULATIVELY|LOOKUP_NORMAL|DIRECT_BIND, decl);
8d08fdba 7000
a3203465 7001 if (tmp == error_mark_node)
ed5511d9 7002 return;
a3203465 7003 else if (tmp != NULL_TREE)
8d08fdba 7004 {
a3203465 7005 init = tmp;
24bef158
MM
7006 tmp = save_expr (tmp);
7007 if (building_stmt_tree ())
7008 {
7009 /* Initialize the declaration. */
7010 tmp = build (INIT_EXPR, TREE_TYPE (decl), decl, tmp);
7011 /* Setting TREE_SIDE_EFFECTS prevents expand_expr from
7012 omitting this expression entirely. */
7013 TREE_SIDE_EFFECTS (tmp) = 1;
7014 finish_expr_stmt (tmp);
7015 }
7016 else
7017 DECL_INITIAL (decl) = tmp;
8d08fdba 7018 }
a3203465 7019 else
8d08fdba 7020 {
8251199e 7021 cp_error ("cannot initialize `%T' from `%T'", type, TREE_TYPE (init));
ed5511d9 7022 return;
8d08fdba 7023 }
8d08fdba 7024
8d08fdba
MS
7025 if (TREE_STATIC (decl) && ! TREE_CONSTANT (DECL_INITIAL (decl)))
7026 {
7027 expand_static_init (decl, DECL_INITIAL (decl));
7028 DECL_INITIAL (decl) = NULL_TREE;
7029 }
7030 return;
8d08fdba
MS
7031}
7032
6060a796
MS
7033/* Fill in DECL_INITIAL with some magical value to prevent expand_decl from
7034 mucking with forces it does not comprehend (i.e. initialization with a
7035 constructor). If we are at global scope and won't go into COMMON, fill
7036 it in with a dummy CONSTRUCTOR to force the variable into .data;
7037 otherwise we can use error_mark_node. */
7038
28cbf42c
MS
7039static tree
7040obscure_complex_init (decl, init)
7041 tree decl, init;
6060a796 7042{
28cbf42c
MS
7043 if (! flag_no_inline && TREE_STATIC (decl))
7044 {
7045 if (extract_init (decl, init))
7046 return NULL_TREE;
7047 }
7048
2ee887f2 7049#if ! defined (ASM_OUTPUT_BSS) && ! defined (ASM_OUTPUT_ALIGNED_BSS)
a9aedbc2 7050 if (toplevel_bindings_p () && ! DECL_COMMON (decl))
6060a796
MS
7051 DECL_INITIAL (decl) = build (CONSTRUCTOR, TREE_TYPE (decl), NULL_TREE,
7052 NULL_TREE);
7053 else
2ee887f2 7054#endif
6060a796 7055 DECL_INITIAL (decl) = error_mark_node;
28cbf42c
MS
7056
7057 return init;
6060a796
MS
7058}
7059
27778b73
MM
7060/* When parsing `int a[] = {1, 2};' we don't know the size of the
7061 array until we finish parsing the initializer. If that's the
7062 situation we're in, update DECL accordingly. */
7063
7064static void
7065maybe_deduce_size_from_array_init (decl, init)
7066 tree decl;
7067 tree init;
7068{
7069 tree type = TREE_TYPE (decl);
7070
7071 if (TREE_CODE (type) == ARRAY_TYPE
7072 && TYPE_DOMAIN (type) == NULL_TREE
7073 && TREE_CODE (decl) != TYPE_DECL)
7074 {
7075 int do_default
7076 = (TREE_STATIC (decl)
7077 /* Even if pedantic, an external linkage array
7078 may have incomplete type at first. */
7079 ? pedantic && ! DECL_EXTERNAL (decl)
7080 : !DECL_EXTERNAL (decl));
7081 tree initializer = init ? init : DECL_INITIAL (decl);
7082 int failure = complete_array_type (type, initializer, do_default);
7083
7084 if (failure == 1)
7085 cp_error ("initializer fails to determine size of `%D'", decl);
7086
7087 if (failure == 2)
7088 {
7089 if (do_default)
7090 cp_error ("array size missing in `%D'", decl);
7091 /* If a `static' var's size isn't known, make it extern as
7092 well as static, so it does not get allocated. If it's not
7093 `static', then don't mark it extern; finish_incomplete_decl
7094 will give it a default size and it will get allocated. */
7095 else if (!pedantic && TREE_STATIC (decl) && !TREE_PUBLIC (decl))
7096 DECL_EXTERNAL (decl) = 1;
7097 }
7098
7099 if (pedantic && TYPE_DOMAIN (type) != NULL_TREE
7100 && tree_int_cst_lt (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
7101 integer_zero_node))
7102 cp_error ("zero-size array `%D'", decl);
7103
7104 layout_decl (decl, 0);
7105 }
7106}
7107
7108/* Set DECL_SIZE, DECL_ALIGN, etc. for DECL (a VAR_DECL), and issue
57b52417 7109 any appropriate error messages regarding the layout. */
27778b73 7110
57b52417
MM
7111static void
7112layout_var_decl (decl)
27778b73 7113 tree decl;
27778b73 7114{
57b52417
MM
7115 tree type = TREE_TYPE (decl);
7116 tree ttype = target_type (type);
7117
7118 /* If we haven't already layed out this declaration, do so now.
7119 Note that we must not call complete type for an external object
7120 because it's type might involve templates that we are not
7121 supposed to isntantiate yet. (And it's perfectly legal to say
7122 `extern X x' for some incomplete type `X'.) */
7123 if (!DECL_EXTERNAL (decl))
7124 complete_type (type);
7125 if (!DECL_SIZE (decl)&& TYPE_SIZE (type))
27778b73
MM
7126 layout_decl (decl, 0);
7127
c82dbd95 7128 if (!DECL_EXTERNAL (decl) && DECL_SIZE (decl) == NULL_TREE)
27778b73
MM
7129 {
7130 /* An automatic variable with an incomplete type: that is an error.
7131 Don't talk about array types here, since we took care of that
7132 message in grokdeclarator. */
7133 cp_error ("storage size of `%D' isn't known", decl);
7134 TREE_TYPE (decl) = error_mark_node;
7135 }
7136 else if (!DECL_EXTERNAL (decl) && IS_AGGR_TYPE (ttype))
7137 /* Let debugger know it should output info for this type. */
7138 note_debug_info_needed (ttype);
7139
7140 if (TREE_STATIC (decl) && DECL_CLASS_SCOPE_P (decl))
7141 note_debug_info_needed (DECL_CONTEXT (decl));
7142
7143 if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl))
7144 && DECL_SIZE (decl) != NULL_TREE
7145 && ! TREE_CONSTANT (DECL_SIZE (decl)))
7146 {
7147 if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
7148 constant_expression_warning (DECL_SIZE (decl));
7149 else
7150 cp_error ("storage size of `%D' isn't constant", decl);
7151 }
7152}
7153
27778b73
MM
7154/* If a local static variable is declared in an inline function, or if
7155 we have a weak definition, we must endeavor to create only one
7156 instance of the variable at link-time. */
7157
7158static void
7159maybe_commonize_var (decl)
7160 tree decl;
7161{
7162 /* Static data in a function with comdat linkage also has comdat
7163 linkage. */
7164 if (TREE_STATIC (decl)
7165 /* Don't mess with __FUNCTION__. */
7166 && ! TREE_ASM_WRITTEN (decl)
7167 && current_function_decl
7168 && DECL_CONTEXT (decl) == current_function_decl
7169 && (DECL_THIS_INLINE (current_function_decl)
7170 || DECL_TEMPLATE_INSTANTIATION (current_function_decl))
7171 && TREE_PUBLIC (current_function_decl))
7172 {
7173 /* Rather than try to get this right with inlining, we suppress
7174 inlining of such functions. */
7175 current_function_cannot_inline
7176 = "function with static variable cannot be inline";
7177
7178 /* If flag_weak, we don't need to mess with this, as we can just
7179 make the function weak, and let it refer to its unique local
7180 copy. This works because we don't allow the function to be
7181 inlined. */
7182 if (! flag_weak)
7183 {
7184 if (DECL_INTERFACE_KNOWN (current_function_decl))
7185 {
7186 TREE_PUBLIC (decl) = 1;
7187 DECL_EXTERNAL (decl) = DECL_EXTERNAL (current_function_decl);
7188 }
7189 else if (DECL_INITIAL (decl) == NULL_TREE
7190 || DECL_INITIAL (decl) == error_mark_node)
7191 {
7192 TREE_PUBLIC (decl) = 1;
7193 DECL_COMMON (decl) = 1;
7194 }
7195 /* else we lose. We can only do this if we can use common,
7196 which we can't if it has been initialized. */
7197
7198 if (TREE_PUBLIC (decl))
7199 DECL_ASSEMBLER_NAME (decl)
7200 = build_static_name (current_function_decl, DECL_NAME (decl));
7201 else if (! DECL_ARTIFICIAL (decl))
7202 {
7203 cp_warning_at ("sorry: semantics of inline function static data `%#D' are wrong (you'll wind up with multiple copies)", decl);
7204 cp_warning_at (" you can work around this by removing the initializer", decl);
7205 }
7206 }
7207 }
7208 else if (DECL_LANG_SPECIFIC (decl) && DECL_COMDAT (decl))
7209 /* Set it up again; we might have set DECL_INITIAL since the last
7210 time. */
7211 comdat_linkage (decl);
7212}
7213
91063b51
MM
7214/* Issue an error message if DECL is an uninitialized const variable. */
7215
7216static void
7217check_for_uninitialized_const_var (decl)
7218 tree decl;
7219{
7220 tree type = TREE_TYPE (decl);
7221
7222 /* ``Unless explicitly declared extern, a const object does not have
7223 external linkage and must be initialized. ($8.4; $12.1)'' ARM
7224 7.1.6 */
7225 if (TREE_CODE (decl) == VAR_DECL
7226 && TREE_CODE (type) != REFERENCE_TYPE
7227 && CP_TYPE_CONST_P (type)
7228 && !TYPE_NEEDS_CONSTRUCTING (type)
7229 && !DECL_INITIAL (decl))
7230 cp_error ("uninitialized const `%D'", decl);
7231}
7232
c82dbd95
MM
7233/* Verify INIT (the initializer for DECL), and record the
7234 initialization in DECL_INITIAL, if appropriate. Returns a new
7235 value for INIT. */
27778b73 7236
c82dbd95
MM
7237static tree
7238check_initializer (decl, init)
27778b73 7239 tree decl;
c82dbd95 7240 tree init;
27778b73 7241{
27778b73
MM
7242 tree type;
7243
7244 if (TREE_CODE (decl) == FIELD_DECL)
c82dbd95 7245 return init;
27778b73 7246
fc0e7bf5
MM
7247 type = TREE_TYPE (decl);
7248
27778b73
MM
7249 /* If `start_decl' didn't like having an initialization, ignore it now. */
7250 if (init != NULL_TREE && DECL_INITIAL (decl) == NULL_TREE)
7251 init = NULL_TREE;
27778b73 7252
c82dbd95 7253 /* Check the initializer. */
27778b73
MM
7254 if (init)
7255 {
c82dbd95
MM
7256 /* Things that are going to be initialized need to have complete
7257 type. */
7258 TREE_TYPE (decl) = type = complete_type (TREE_TYPE (decl));
7259
7260 if (type == error_mark_node)
7261 /* We will have already complained. */
7262 init = NULL_TREE;
7263 else if (TYPE_SIZE (type) && !TREE_CONSTANT (TYPE_SIZE (type)))
27778b73
MM
7264 {
7265 cp_error ("variable-sized object `%D' may not be initialized", decl);
7266 init = NULL_TREE;
7267 }
c82dbd95
MM
7268 else if (TREE_CODE (type) == ARRAY_TYPE
7269 && !TYPE_SIZE (TREE_TYPE (type)))
27778b73
MM
7270 {
7271 cp_error ("elements of array `%#D' have incomplete type", decl);
7272 init = NULL_TREE;
7273 }
c82dbd95
MM
7274 else if (!TYPE_SIZE (type))
7275 {
7276 cp_error ("`%D' has incomplete type", decl);
7277 TREE_TYPE (decl) = error_mark_node;
7278 init = NULL_TREE;
7279 }
27778b73
MM
7280 }
7281
7282 if (TREE_CODE (decl) == CONST_DECL)
7283 {
7284 my_friendly_assert (TREE_CODE (decl) != REFERENCE_TYPE, 148);
7285
7286 DECL_INITIAL (decl) = init;
7287
7288 /* This will keep us from needing to worry about our obstacks. */
7289 my_friendly_assert (init != NULL_TREE, 149);
7290 init = NULL_TREE;
7291 }
c82dbd95
MM
7292 else if (!DECL_EXTERNAL (decl) && TREE_CODE (type) == REFERENCE_TYPE)
7293 {
7294 if (TREE_STATIC (decl))
7295 make_decl_rtl (decl, NULL_PTR, toplevel_bindings_p ());
7296 grok_reference_init (decl, type, init);
7297 init = NULL_TREE;
7298 }
27778b73
MM
7299 else if (init)
7300 {
7301 if (TYPE_HAS_CONSTRUCTOR (type) || TYPE_NEEDS_CONSTRUCTING (type))
7302 {
7303 if (TREE_CODE (type) == ARRAY_TYPE)
7304 init = digest_init (type, init, (tree *) 0);
7305 else if (TREE_CODE (init) == CONSTRUCTOR
7306 && TREE_HAS_CONSTRUCTOR (init))
7307 {
7308 if (TYPE_NON_AGGREGATE_CLASS (type))
7309 {
7310 cp_error ("`%D' must be initialized by constructor, not by `{...}'",
7311 decl);
7312 init = error_mark_node;
7313 }
7314 else
7315 goto dont_use_constructor;
7316 }
7317 }
7318 else
7319 {
7320 dont_use_constructor:
7321 if (TREE_CODE (init) != TREE_VEC)
7322 init = store_init_value (decl, init);
7323 }
7324
7325 if (init)
7326 /* We must hide the initializer so that expand_decl
7327 won't try to do something it does not understand. */
7328 init = obscure_complex_init (decl, init);
7329 }
7330 else if (DECL_EXTERNAL (decl))
7331 ;
7332 else if (TREE_CODE_CLASS (TREE_CODE (type)) == 't'
7333 && (IS_AGGR_TYPE (type) || TYPE_NEEDS_CONSTRUCTING (type)))
7334 {
7335 tree core_type = strip_array_types (type);
7336
7337 if (! TYPE_NEEDS_CONSTRUCTING (core_type))
7338 {
7339 if (CLASSTYPE_READONLY_FIELDS_NEED_INIT (core_type))
7340 cp_error ("structure `%D' with uninitialized const members", decl);
7341 if (CLASSTYPE_REF_FIELDS_NEED_INIT (core_type))
7342 cp_error ("structure `%D' with uninitialized reference members",
7343 decl);
7344 }
7345
7346 check_for_uninitialized_const_var (decl);
7347
7348 if (TYPE_SIZE (type) != NULL_TREE
7349 && TYPE_NEEDS_CONSTRUCTING (type))
7350 init = obscure_complex_init (decl, NULL_TREE);
7351
7352 }
7353 else
7354 check_for_uninitialized_const_var (decl);
7355
c82dbd95 7356 return init;
27778b73
MM
7357}
7358
7359/* If DECL is not a local variable, give it RTL. */
7360
7361static void
7362make_rtl_for_nonlocal_decl (decl, init, asmspec)
7363 tree decl;
7364 tree init;
7365 const char *asmspec;
7366{
27778b73
MM
7367 int toplev;
7368 tree type;
7369
7370 type = TREE_TYPE (decl);
7371 toplev = toplevel_bindings_p ();
b7b8bcd2
MM
7372 push_obstacks_nochange ();
7373 if (TREE_STATIC (decl)
7374 && TYPE_NEEDS_DESTRUCTOR (type)
7375 && allocation_temporary_p ())
7376 end_temporary_allocation ();
27778b73
MM
7377
7378 if (TREE_CODE (decl) == VAR_DECL && DECL_VIRTUAL_P (decl))
7379 make_decl_rtl (decl, NULL_PTR, toplev);
7380 else if (TREE_CODE (decl) == VAR_DECL
7381 && TREE_READONLY (decl)
7382 && DECL_INITIAL (decl) != NULL_TREE
7383 && DECL_INITIAL (decl) != error_mark_node
7384 && ! EMPTY_CONSTRUCTOR_P (DECL_INITIAL (decl)))
7385 {
7386 DECL_INITIAL (decl) = save_expr (DECL_INITIAL (decl));
7387
7388 if (asmspec)
7389 DECL_ASSEMBLER_NAME (decl) = get_identifier (asmspec);
7390
7391 if (! toplev
7392 && TREE_STATIC (decl)
7393 && ! TREE_SIDE_EFFECTS (decl)
7394 && ! TREE_PUBLIC (decl)
7395 && ! DECL_EXTERNAL (decl)
7396 && ! TYPE_NEEDS_DESTRUCTOR (type)
7397 && DECL_MODE (decl) != BLKmode)
7398 {
7399 /* If this variable is really a constant, then fill its DECL_RTL
7400 slot with something which won't take up storage.
7401 If something later should take its address, we can always give
7402 it legitimate RTL at that time. */
7403 DECL_RTL (decl) = gen_reg_rtx (DECL_MODE (decl));
7404 store_expr (DECL_INITIAL (decl), DECL_RTL (decl), 0);
7405 TREE_ASM_WRITTEN (decl) = 1;
7406 }
7407 else if (toplev && ! TREE_PUBLIC (decl))
7408 {
7409 /* If this is a static const, change its apparent linkage
7410 if it belongs to a #pragma interface. */
7411 if (!interface_unknown)
7412 {
7413 TREE_PUBLIC (decl) = 1;
7414 DECL_EXTERNAL (decl) = interface_only;
7415 }
7416 make_decl_rtl (decl, asmspec, toplev);
7417 }
7418 else
7419 rest_of_decl_compilation (decl, asmspec, toplev, at_eof);
7420 }
7421 else if (TREE_CODE (decl) == VAR_DECL
7422 && DECL_LANG_SPECIFIC (decl)
7423 && DECL_IN_AGGR_P (decl))
7424 {
7425 my_friendly_assert (TREE_STATIC (decl), 19990828);
7426
7427 if (init == NULL_TREE
7428#ifdef DEFAULT_STATIC_DEFS
7429 /* If this code is dead, then users must
7430 explicitly declare static member variables
7431 outside the class def'n as well. */
7432 && TYPE_NEEDS_CONSTRUCTING (type)
7433#endif
7434 )
7435 {
7436 DECL_EXTERNAL (decl) = 1;
7437 make_decl_rtl (decl, asmspec, 1);
7438 }
7439 else
7440 rest_of_decl_compilation (decl, asmspec, toplev, at_eof);
7441 }
7442 else
7443 rest_of_decl_compilation (decl, asmspec, toplev, at_eof);
7444
b7b8bcd2 7445 pop_obstacks ();
27778b73
MM
7446}
7447
7448/* The old ARM scoping rules injected variables declared in the
7449 initialization statement of a for-statement into the surrounding
7450 scope. We support this usage, in order to be backward-compatible.
7451 DECL is a just-declared VAR_DECL; if necessary inject its
7452 declaration into the surrounding scope. */
7453
b7b8bcd2 7454void
27778b73
MM
7455maybe_inject_for_scope_var (decl)
7456 tree decl;
7457{
7458 if (current_binding_level->is_for_scope)
7459 {
7460 struct binding_level *outer
7461 = current_binding_level->level_chain;
7462
7463 /* Check to see if the same name is already bound at the outer
7464 level, either because it was directly declared, or because a
7465 dead for-decl got preserved. In either case, the code would
7466 not have been valid under the ARM scope rules, so clear
7467 is_for_scope for the current_binding_level.
7468
7469 Otherwise, we need to preserve the temp slot for decl to last
7470 into the outer binding level. */
7471
7472 tree outer_binding
7473 = TREE_CHAIN (IDENTIFIER_BINDING (DECL_NAME (decl)));
7474
7475 if (outer_binding && BINDING_LEVEL (outer_binding) == outer
7476 && (TREE_CODE (BINDING_VALUE (outer_binding))
7477 == VAR_DECL)
7478 && DECL_DEAD_FOR_LOCAL (BINDING_VALUE (outer_binding)))
7479 {
7480 BINDING_VALUE (outer_binding)
7481 = DECL_SHADOWED_FOR_VAR (BINDING_VALUE (outer_binding));
7482 current_binding_level->is_for_scope = 0;
7483 }
7484 else if (DECL_IN_MEMORY_P (decl))
7485 preserve_temp_slots (DECL_RTL (decl));
7486 }
7487}
7488
ed5511d9 7489/* Generate code to initialize DECL (a local variable). */
27778b73 7490
b7b8bcd2
MM
7491void
7492initialize_local_var (decl, init, flags)
27778b73
MM
7493 tree decl;
7494 tree init;
27778b73
MM
7495 int flags;
7496{
7497 tree type;
7498
c82dbd95 7499 type = complete_type (TREE_TYPE (decl));
b7b8bcd2 7500
b7b8bcd2
MM
7501 if (DECL_SIZE (decl) == NULL_TREE && !TREE_STATIC (decl))
7502 {
7503 /* If we used it already as memory, it must stay in memory. */
7504 DECL_INITIAL (decl) = NULL_TREE;
7505 TREE_ADDRESSABLE (decl) = TREE_USED (decl);
7506 }
7507
27778b73
MM
7508 if (DECL_SIZE (decl) && type != error_mark_node)
7509 {
7510 int already_used;
7511
7512 /* Compute and store the initial value. */
27778b73
MM
7513 already_used = TREE_USED (decl) || TREE_USED (type);
7514
7515 if (init || TYPE_NEEDS_CONSTRUCTING (type))
7516 {
24bef158
MM
7517 int saved_stmts_are_full_exprs_p;
7518
27778b73
MM
7519 emit_line_note (DECL_SOURCE_FILE (decl),
7520 DECL_SOURCE_LINE (decl));
24bef158
MM
7521 saved_stmts_are_full_exprs_p = stmts_are_full_exprs_p;
7522 stmts_are_full_exprs_p = 1;
27778b73 7523 finish_expr_stmt (build_aggr_init (decl, init, flags));
24bef158 7524 stmts_are_full_exprs_p = saved_stmts_are_full_exprs_p;
27778b73
MM
7525 }
7526
7527 /* Set this to 0 so we can tell whether an aggregate which was
7528 initialized was ever used. Don't do this if it has a
7529 destructor, so we don't complain about the 'resource
b7b8bcd2
MM
7530 allocation is initialization' idiom. Now set
7531 attribute((unused)) on types so decls of that type will be
7532 marked used. (see TREE_USED, above.) */
27778b73
MM
7533 if (TYPE_NEEDS_CONSTRUCTING (type)
7534 && ! already_used
24bef158 7535 && !TYPE_NEEDS_DESTRUCTOR (type)
27778b73
MM
7536 && DECL_NAME (decl))
7537 TREE_USED (decl) = 0;
b7b8bcd2 7538 else if (already_used)
27778b73
MM
7539 TREE_USED (decl) = 1;
7540 }
24bef158 7541}
27778b73 7542
24bef158
MM
7543/* Generate code to destroy DECL (a local variable). */
7544
7545void
7546destroy_local_var (decl)
7547 tree decl;
7548{
9d85d30c
MM
7549 tree type = TREE_TYPE (decl);
7550 tree cleanup;
7551
7552 /* Only variables get cleaned up. */
7553 if (TREE_CODE (decl) != VAR_DECL)
7554 return;
7555
7556 /* And only things with destructors need cleaning up. */
7557 if (!TYPE_NEEDS_DESTRUCTOR (type))
7558 return;
7559
7560 if (TREE_CODE (decl) == VAR_DECL &&
7561 (DECL_EXTERNAL (decl) || TREE_STATIC (decl)))
7562 /* We don't clean up things that aren't defined in this
7563 translation unit, or that need a static cleanup. The latter
7564 are handled by finish_file. */
7565 return;
7566
7567 /* Compute the cleanup. */
7568 cleanup = maybe_build_cleanup (decl);
27778b73 7569
b7b8bcd2 7570 /* Record the cleanup required for this declaration. */
24bef158
MM
7571 if (DECL_SIZE (decl) && TREE_TYPE (decl) != error_mark_node
7572 && cleanup)
7573 finish_decl_cleanup (decl, cleanup);
7574}
7575
7576/* Let the back-end know about DECL. */
7577
7578void
7579emit_local_var (decl)
7580 tree decl;
7581{
7582 /* Create RTL for this variable. */
7583 if (DECL_RTL (decl))
7584 /* Only a RESULT_DECL should have non-NULL RTL when
7585 arriving here. All other local variables are
7586 assigned RTL in this function. */
7587 my_friendly_assert (TREE_CODE (decl) == RESULT_DECL,
7588 19990828);
7589 else
7590 expand_decl (decl);
7591
7592 /* Actually do the initialization. */
7593 expand_start_target_temps ();
7594 expand_decl_init (decl);
7595 expand_end_target_temps ();
27778b73
MM
7596}
7597
8d08fdba
MS
7598/* Finish processing of a declaration;
7599 install its line number and initial value.
7600 If the length of an array type is not known before,
7601 it must be determined now, from the initial value, or it is an error.
7602
7603 Call `pop_obstacks' iff NEED_POP is nonzero.
7604
82580166 7605 For C++, `cp_finish_decl' must be fairly evasive: it must keep initializers
8d08fdba
MS
7606 for aggregates that have constructors alive on the permanent obstack,
7607 so that the global initializing functions can be written at the end.
7608
7609 INIT0 holds the value of an initializer that should be allowed to escape
7610 the normal rules.
7611
6060a796
MS
7612 FLAGS is LOOKUP_ONLYCONVERTING is the = init syntax was used, else 0
7613 if the (init) syntax was used.
7614
8d08fdba 7615 For functions that take default parameters, DECL points to its
82580166 7616 "maximal" instantiation. `cp_finish_decl' must then also declared its
8d08fdba
MS
7617 subsequently lower and lower forms of instantiation, checking for
7618 ambiguity as it goes. This can be sped up later. */
7619
7620void
82580166 7621cp_finish_decl (decl, init, asmspec_tree, need_pop, flags)
8d08fdba
MS
7622 tree decl, init;
7623 tree asmspec_tree;
7624 int need_pop;
6060a796 7625 int flags;
8d08fdba
MS
7626{
7627 register tree type;
27778b73 7628 tree ttype = NULL_TREE;
8d08fdba 7629 int temporary = allocation_temporary_p ();
9c0758dd 7630 const char *asmspec = NULL;
8d08fdba
MS
7631 int was_readonly = 0;
7632
7633 /* If this is 0, then we did not change obstacks. */
7634 if (! decl)
7635 {
7636 if (init)
8251199e 7637 error ("assignment (not initialization) in declaration");
8d08fdba
MS
7638 return;
7639 }
7640
a4443a08 7641 /* If a name was specified, get the string. */
8d08fdba 7642 if (asmspec_tree)
8d08fdba 7643 asmspec = TREE_STRING_POINTER (asmspec_tree);
8d08fdba 7644
2c73f9f5
ML
7645 if (init && TREE_CODE (init) == NAMESPACE_DECL)
7646 {
8251199e 7647 cp_error ("Cannot initialize `%D' to namespace `%D'",
2c73f9f5
ML
7648 decl, init);
7649 init = NULL_TREE;
7650 }
7651
6ba89f8e
MM
7652 if (current_class_type
7653 && DECL_REAL_CONTEXT (decl) == current_class_type
7654 && TYPE_BEING_DEFINED (current_class_type)
7655 && (DECL_INITIAL (decl) || init))
7656 DECL_DEFINED_IN_CLASS_P (decl) = 1;
7657
9a68c51f
JM
7658 if (TREE_CODE (decl) == VAR_DECL
7659 && DECL_CONTEXT (decl)
7660 && TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL
7661 && DECL_CONTEXT (decl) != current_namespace
7662 && init)
7663 {
7664 /* Leave the namespace of the object. */
7665 pop_decl_namespace ();
7666 }
7667
c82dbd95 7668 type = TREE_TYPE (decl);
8d08fdba 7669
f376e137 7670 if (type == error_mark_node)
eac293a1 7671 {
a9aedbc2 7672 if (toplevel_bindings_p () && temporary)
eac293a1
MS
7673 end_temporary_allocation ();
7674
7675 return;
7676 }
f376e137 7677
24bef158
MM
7678 /* Add this declaration to the statement-tree. */
7679 if (building_stmt_tree ()
7680 && TREE_CODE (current_scope ()) == FUNCTION_DECL)
7681 add_decl_stmt (decl);
7682
a7a7710d
NS
7683 if (TYPE_HAS_MUTABLE_P (type))
7684 TREE_READONLY (decl) = 0;
24bef158 7685
5156628f 7686 if (processing_template_decl)
5566b478
MS
7687 {
7688 if (init && DECL_INITIAL (decl))
2a1e9fdd 7689 DECL_INITIAL (decl) = init;
5566b478
MS
7690 goto finish_end0;
7691 }
3e41d13b 7692
27778b73
MM
7693 /* Parameters are handled by store_parm_decls, not cp_finish_decl. */
7694 my_friendly_assert (TREE_CODE (decl) != PARM_DECL, 19990828);
7695
8d08fdba
MS
7696 /* Take care of TYPE_DECLs up front. */
7697 if (TREE_CODE (decl) == TYPE_DECL)
7698 {
7699 if (init && DECL_INITIAL (decl))
7700 {
7701 /* typedef foo = bar; store the type of bar as the type of foo. */
7702 TREE_TYPE (decl) = type = TREE_TYPE (init);
7703 DECL_INITIAL (decl) = init = NULL_TREE;
7704 }
a0a33927
MS
7705 if (type != error_mark_node
7706 && IS_AGGR_TYPE (type) && DECL_NAME (decl))
8d08fdba
MS
7707 {
7708 if (TREE_TYPE (DECL_NAME (decl)) && TREE_TYPE (decl) != type)
8251199e 7709 cp_warning ("shadowing previous type declaration of `%#D'", decl);
8d08fdba
MS
7710 set_identifier_type_value (DECL_NAME (decl), type);
7711 CLASSTYPE_GOT_SEMICOLON (type) = 1;
7712 }
7713 GNU_xref_decl (current_function_decl, decl);
cffa8729
MS
7714
7715 /* If we have installed this as the canonical typedef for this
7716 type, and that type has not been defined yet, delay emitting
956d6950 7717 the debug information for it, as we will emit it later. */
d2e5ee5c 7718 if (TYPE_MAIN_DECL (TREE_TYPE (decl)) == decl
cffa8729
MS
7719 && TYPE_SIZE (TREE_TYPE (decl)) == NULL_TREE)
7720 TYPE_DECL_SUPPRESS_DEBUG (decl) = 1;
7721
8d08fdba 7722 rest_of_decl_compilation (decl, NULL_PTR,
5566b478 7723 DECL_CONTEXT (decl) == NULL_TREE, at_eof);
8d08fdba
MS
7724 goto finish_end;
7725 }
3e41d13b 7726
8d08fdba 7727 if (TREE_CODE (decl) != FUNCTION_DECL)
3e41d13b 7728 ttype = target_type (type);
8d08fdba
MS
7729
7730 if (! DECL_EXTERNAL (decl) && TREE_READONLY (decl)
7731 && TYPE_NEEDS_CONSTRUCTING (type))
7732 {
8d08fdba
MS
7733 /* Currently, GNU C++ puts constants in text space, making them
7734 impossible to initialize. In the future, one would hope for
7735 an operating system which understood the difference between
7736 initialization and the running of a program. */
7737 was_readonly = 1;
7738 TREE_READONLY (decl) = 0;
7739 }
7740
27778b73 7741 if (TREE_CODE (decl) == FIELD_DECL && asmspec)
8d08fdba 7742 {
27778b73
MM
7743 /* This must override the asm specifier which was placed by
7744 grokclassfn. Lay this out fresh. */
7745 DECL_RTL (TREE_TYPE (decl)) = NULL_RTX;
7746 DECL_ASSEMBLER_NAME (decl) = get_identifier (asmspec);
7747 make_decl_rtl (decl, asmspec, 0);
8d08fdba
MS
7748 }
7749
c82dbd95
MM
7750 /* Deduce size of array from initialization, if not already known. */
7751 maybe_deduce_size_from_array_init (decl, init);
7752 init = check_initializer (decl, init);
3e41d13b 7753
8d08fdba
MS
7754 GNU_xref_decl (current_function_decl, decl);
7755
8d08fdba
MS
7756 /* For top-level declaration, the initial value was read in
7757 the temporary obstack. MAXINDEX, rtl, etc. to be made below
7758 must go in the permanent obstack; but don't discard the
7759 temporary data yet. */
7760
a9aedbc2 7761 if (toplevel_bindings_p () && temporary)
8d08fdba
MS
7762 end_temporary_allocation ();
7763
8d08fdba 7764 if (TREE_CODE (decl) == VAR_DECL)
57b52417 7765 layout_var_decl (decl);
8d08fdba
MS
7766
7767 /* Output the assembler code and/or RTL code for variables and functions,
7768 unless the type is an undefined structure or union.
7769 If not, it will get done when the type is completed. */
8d08fdba
MS
7770 if (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == FUNCTION_DECL
7771 || TREE_CODE (decl) == RESULT_DECL)
7772 {
27778b73
MM
7773 if (TREE_CODE (decl) == VAR_DECL)
7774 maybe_commonize_var (decl);
8d08fdba 7775
27778b73 7776 make_rtl_for_nonlocal_decl (decl, init, asmspec);
8d08fdba 7777
27778b73
MM
7778 if (TREE_CODE (type) == FUNCTION_TYPE
7779 || TREE_CODE (type) == METHOD_TYPE)
7780 abstract_virtuals_error (decl,
7781 strip_array_types (TREE_TYPE (type)));
7782 else
7783 abstract_virtuals_error (decl, strip_array_types (type));
8d08fdba 7784
8d08fdba 7785 if (TREE_CODE (decl) == FUNCTION_DECL)
faae18ab 7786 ;
67d743fe
MS
7787 else if (DECL_EXTERNAL (decl)
7788 && ! (DECL_LANG_SPECIFIC (decl)
7789 && DECL_NOT_REALLY_EXTERN (decl)))
5566b478
MS
7790 {
7791 if (init)
7792 DECL_INITIAL (decl) = init;
7793 }
8d08fdba
MS
7794 else if (TREE_STATIC (decl) && type != error_mark_node)
7795 {
7796 /* Cleanups for static variables are handled by `finish_file'. */
f30432d7
MS
7797 if (TYPE_NEEDS_CONSTRUCTING (type) || init != NULL_TREE
7798 || TYPE_NEEDS_DESTRUCTOR (type))
8d08fdba 7799 expand_static_init (decl, init);
8d08fdba 7800 }
b35d4555 7801 else if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL)
8d08fdba 7802 {
b7b8bcd2 7803 /* This is a local declaration. */
b35d4555
MM
7804 if (doing_semantic_analysis_p ())
7805 maybe_inject_for_scope_var (decl);
b7b8bcd2
MM
7806 /* Initialize the local variable. But, if we're building a
7807 statement-tree, we'll do the initialization when we
7808 expand the tree. */
24bef158
MM
7809 if (processing_template_decl)
7810 {
7811 if (init || DECL_INITIAL (decl) == error_mark_node)
7812 DECL_INITIAL (decl) = init;
7813 }
7814 else
7815 {
b35d4555
MM
7816 /* If we're not building RTL, then we need to do so
7817 now. */
24bef158
MM
7818 if (!building_stmt_tree ())
7819 emit_local_var (decl);
b35d4555 7820 /* Initialize the variable. */
24bef158
MM
7821 initialize_local_var (decl, init, flags);
7822 /* Clean up the variable. */
7823 destroy_local_var (decl);
7824 }
8d08fdba
MS
7825 }
7826 finish_end0:
7827
7828 /* Undo call to `pushclass' that was done in `start_decl'
7829 due to initialization of qualified member variable.
7830 I.e., Foo::x = 10; */
7831 {
f30432d7 7832 tree context = DECL_REAL_CONTEXT (decl);
8d08fdba
MS
7833 if (context
7834 && TREE_CODE_CLASS (TREE_CODE (context)) == 't'
7835 && (TREE_CODE (decl) == VAR_DECL
7836 /* We also have a pushclass done that we need to undo here
7837 if we're at top level and declare a method. */
5566b478
MS
7838 || TREE_CODE (decl) == FUNCTION_DECL)
7839 /* If size hasn't been set, we're still defining it,
7840 and therefore inside the class body; don't pop
7841 the binding level.. */
7842 && TYPE_SIZE (context) != NULL_TREE
7843 && context == current_class_type)
6b400b21 7844 pop_nested_class ();
8d08fdba
MS
7845 }
7846 }
7847
7848 finish_end:
7849
39211cd5
MS
7850 /* If requested, warn about definitions of large data objects. */
7851
7852 if (warn_larger_than
5156628f 7853 && ! processing_template_decl
39211cd5
MS
7854 && (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL)
7855 && !DECL_EXTERNAL (decl))
7856 {
7857 register tree decl_size = DECL_SIZE (decl);
7858
7859 if (decl_size && TREE_CODE (decl_size) == INTEGER_CST)
7860 {
7861 unsigned units = TREE_INT_CST_LOW (decl_size) / BITS_PER_UNIT;
7862
7863 if (units > larger_than_size)
7864 warning_with_decl (decl, "size of `%s' is %u bytes", units);
7865 }
7866 }
7867
8d08fdba 7868 if (need_pop)
27778b73
MM
7869 /* Resume permanent allocation, if not within a function. The
7870 corresponding push_obstacks_nochange is in start_decl,
7871 start_method, groktypename, and in grokfield. */
7872 pop_obstacks ();
8d08fdba
MS
7873
7874 if (was_readonly)
7875 TREE_READONLY (decl) = 1;
8d08fdba
MS
7876}
7877
82580166 7878/* This is here for a midend callback from c-common.c */
e92cc029 7879
82580166
MS
7880void
7881finish_decl (decl, init, asmspec_tree)
7882 tree decl, init;
7883 tree asmspec_tree;
7884{
7885 cp_finish_decl (decl, init, asmspec_tree, 1, 0);
7886}
7887
f0105ed3
MM
7888/* Generate code to handle the destruction of the function-scoped
7889 static variable DECL. */
7890
7891static void
7892destroy_local_static (decl)
7893 tree decl;
7894{
7895 tree cleanup, fcall;
7896 tree compound_stmt;
7897 int saved_flag_access_control;
7898
7899 if (atexit_node == 0)
7900 {
7901 tree atexit_fndecl, PFV, pfvlist;
7902 /* Remember this information until end of file. */
7903 push_obstacks (&permanent_obstack, &permanent_obstack);
7904 PFV = build_pointer_type (build_function_type
7905 (void_type_node, void_list_node));
7906
7907 pfvlist = tree_cons (NULL_TREE, PFV, void_list_node);
7908
7909 push_lang_context (lang_name_c);
7910 /* Note that we do not call pushdecl for this function;
7911 there's no reason that this declaration should be
7912 accessible to anyone. */
7913 atexit_fndecl
7914 = define_function ("atexit",
7915 build_function_type (void_type_node,
7916 pfvlist),
c2a37c55 7917 /*pfn=*/0, NULL_PTR);
f0105ed3
MM
7918 mark_used (atexit_fndecl);
7919 atexit_node = default_conversion (atexit_fndecl);
7920 pop_lang_context ();
7921 pop_obstacks ();
7922 }
7923
7924 /* Call build_cleanup before we enter the anonymous function so that
7925 any access checks will be done relative to the current scope,
7926 rather than the scope of the anonymous function. */
7927 build_cleanup (decl);
7928
7929 /* Now start the function. */
7930 cleanup = start_anon_func ();
7931
7932 /* Now, recompute the cleanup. It may contain SAVE_EXPRs that refer
7933 to the original function, rather than the anonymous one. That
7934 will make the back-end think that nested functions are in use,
7935 which causes confusion. */
7936 saved_flag_access_control = flag_access_control;
7937 flag_access_control = 0;
7938 fcall = build_cleanup (decl);
7939 flag_access_control = saved_flag_access_control;
7940
7941 /* Create the body of the anonymous function. */
7942 compound_stmt = begin_compound_stmt (/*has_no_scope=*/0);
7943 finish_expr_stmt (fcall);
7944 finish_compound_stmt (/*has_no_scope=*/0, compound_stmt);
7945 end_anon_func ();
7946
7947 /* Call atexit with the cleanup function. */
7948 mark_addressable (cleanup);
7949 cleanup = build_unary_op (ADDR_EXPR, cleanup, 0);
7950 fcall = build_function_call (atexit_node,
7951 tree_cons (NULL_TREE,
7952 cleanup,
7953 NULL_TREE));
7954 finish_expr_stmt (fcall);
7955}
7956
8d08fdba
MS
7957void
7958expand_static_init (decl, init)
7959 tree decl;
7960 tree init;
7961{
7962 tree oldstatic = value_member (decl, static_aggregates);
a4443a08 7963
8d08fdba
MS
7964 if (oldstatic)
7965 {
7966 if (TREE_PURPOSE (oldstatic) && init != NULL_TREE)
8251199e 7967 cp_error ("multiple initializations given for `%D'", decl);
8d08fdba 7968 }
0aafb128 7969 else if (! toplevel_bindings_p ())
8d08fdba
MS
7970 {
7971 /* Emit code to perform this initialization but once. */
7972 tree temp;
b7b8bcd2 7973 tree if_stmt;
f0105ed3 7974 tree then_clause;
f1dedc31
MM
7975 tree assignment;
7976 tree temp_init;
8d08fdba 7977
e92cc029 7978 /* Remember this information until end of file. */
8d08fdba
MS
7979 push_obstacks (&permanent_obstack, &permanent_obstack);
7980
2036a15c
MM
7981 /* Emit code to perform this initialization but once. This code
7982 looks like:
7983
7984 static int temp = 0;
7985 if (!temp) {
7986 // Do initialization.
7987 temp = 1;
7988 // Register variable for destruction at end of program.
7989 }
7990
7991 Note that the `temp' variable is only set to 1 *after* the
7992 initialization is complete. This ensures that an exception,
7993 thrown during the construction, will cause the variable to
7994 reinitialized when we pass through this code again, as per:
7995
7996 [stmt.dcl]
7997
7998 If the initialization exits by throwing an exception, the
7999 initialization is not complete, so it will be tried again
8000 the next time control enters the declaration.
8001
8002 In theory, this process should be thread-safe, too; multiple
8003 threads should not be able to initialize the variable more
8004 than once. We don't yet attempt to ensure thread-safety. */
8d08fdba
MS
8005 temp = get_temp_name (integer_type_node, 1);
8006 rest_of_decl_compilation (temp, NULL_PTR, 0, 0);
2036a15c
MM
8007
8008 /* Begin the conditional initialization. */
b7b8bcd2
MM
8009 if_stmt = begin_if_stmt ();
8010 finish_if_stmt_cond (build_binary_op (EQ_EXPR, temp,
8011 integer_zero_node),
8012 if_stmt);
f0105ed3 8013 then_clause = begin_compound_stmt (/*has_no_scope=*/0);
72b7eeff 8014
2036a15c 8015 /* Do the initialization itself. */
28cbf42c 8016 if (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl))
f30432d7 8017 || (init && TREE_CODE (init) == TREE_LIST))
f1dedc31 8018 assignment = build_aggr_init (decl, init, 0);
f30432d7 8019 else if (init)
963d5758
MM
8020 {
8021 /* The initialization we're doing here is just a bitwise
8022 copy. */
8023 assignment = build (INIT_EXPR, TREE_TYPE (decl), decl, init);
8024 TREE_SIDE_EFFECTS (assignment) = 1;
8025 }
f1dedc31
MM
8026 else
8027 assignment = NULL_TREE;
8028
8029 /* Once the assignment is complete, set TEMP to 1. Since the
8030 construction of the static object is complete at this point,
8031 we want to make sure TEMP is set to 1 even if a temporary
8032 constructed during the initialization throws an exception
8033 when it is destroyed. So, we combine the initialization and
8034 the assignment to TEMP into a single expression, ensuring
8035 that when we call finish_expr_stmt the cleanups will not be
8036 run until after TEMP is set to 1. */
8037 temp_init = build_modify_expr (temp, NOP_EXPR, integer_one_node);
8038 if (assignment)
8039 {
8040 assignment = tree_cons (NULL_TREE, assignment,
8041 build_tree_list (NULL_TREE,
8042 temp_init));
8043 assignment = build_compound_expr (assignment);
8044 }
8045 else
8046 assignment = temp_init;
8047 finish_expr_stmt (assignment);
72b7eeff 8048
2036a15c
MM
8049 /* Use atexit to register a function for destroying this static
8050 variable. */
72b7eeff 8051 if (TYPE_NEEDS_DESTRUCTOR (TREE_TYPE (decl)))
f0105ed3 8052 destroy_local_static (decl);
72b7eeff 8053
f0105ed3 8054 finish_compound_stmt (/*has_no_scope=*/0, then_clause);
b7b8bcd2
MM
8055 finish_then_clause (if_stmt);
8056 finish_if_stmt ();
8057
e92cc029 8058 /* Resume old (possibly temporary) allocation. */
8d08fdba
MS
8059 pop_obstacks ();
8060 }
8061 else
8062 {
0aafb128
MM
8063 /* This code takes into account memory allocation policy of
8064 `start_decl'. Namely, if TYPE_NEEDS_CONSTRUCTING does not
8065 hold for this object, then we must make permanent the storage
8066 currently in the temporary obstack. */
8067 if (!TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl)))
8d08fdba 8068 preserve_initializer ();
e1b3e07d 8069 static_aggregates = tree_cons (init, decl, static_aggregates);
8d08fdba
MS
8070 }
8071}
3c5c0849
MM
8072
8073/* Finish the declaration of a catch-parameter. */
8074
b35d4555 8075tree
3c5c0849
MM
8076start_handler_parms (declspecs, declarator)
8077 tree declspecs;
8078 tree declarator;
8079{
8080 tree decl;
8081 if (declspecs)
8082 {
8083 decl = grokdeclarator (declarator, declspecs, CATCHPARM,
8084 1, NULL_TREE);
8085 if (decl == NULL_TREE)
8086 error ("invalid catch parameter");
8087 }
8088 else
8089 decl = NULL_TREE;
b35d4555
MM
8090
8091 return decl;
3c5c0849
MM
8092}
8093
8d08fdba
MS
8094\f
8095/* Make TYPE a complete type based on INITIAL_VALUE.
8096 Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
6ab5c740 8097 2 if there was no information (in which case assume 0 if DO_DEFAULT). */
8d08fdba
MS
8098
8099int
8100complete_array_type (type, initial_value, do_default)
8101 tree type, initial_value;
8102 int do_default;
8103{
8104 register tree maxindex = NULL_TREE;
8105 int value = 0;
6ab5c740
NS
8106
8107 /* Allocate on the same obstack as TYPE. */
8108 push_obstacks (TYPE_OBSTACK (type), TYPE_OBSTACK (type));
8109
8d08fdba
MS
8110 if (initial_value)
8111 {
8112 /* Note MAXINDEX is really the maximum index,
8113 one less than the size. */
8114 if (TREE_CODE (initial_value) == STRING_CST)
e1cd6e56
MS
8115 {
8116 int eltsize
8117 = int_size_in_bytes (TREE_TYPE (TREE_TYPE (initial_value)));
8118 maxindex = build_int_2 ((TREE_STRING_LENGTH (initial_value)
8119 / eltsize) - 1, 0);
8120 }
8d08fdba
MS
8121 else if (TREE_CODE (initial_value) == CONSTRUCTOR)
8122 {
e1cd6e56
MS
8123 tree elts = CONSTRUCTOR_ELTS (initial_value);
8124 maxindex = size_binop (MINUS_EXPR, integer_zero_node, size_one_node);
8125 for (; elts; elts = TREE_CHAIN (elts))
8126 {
8127 if (TREE_PURPOSE (elts))
8128 maxindex = TREE_PURPOSE (elts);
8129 else
8130 maxindex = size_binop (PLUS_EXPR, maxindex, size_one_node);
8131 }
8132 maxindex = copy_node (maxindex);
8d08fdba
MS
8133 }
8134 else
8135 {
8136 /* Make an error message unless that happened already. */
8137 if (initial_value != error_mark_node)
8138 value = 1;
0db982be
ML
8139 else
8140 initial_value = NULL_TREE;
8d08fdba
MS
8141
8142 /* Prevent further error messages. */
8143 maxindex = build_int_2 (0, 0);
8144 }
8145 }
8146
8147 if (!maxindex)
8148 {
8149 if (do_default)
8150 maxindex = build_int_2 (0, 0);
8151 value = 2;
8152 }
8153
8154 if (maxindex)
8155 {
51c184be 8156 tree itype;
6ab5c740
NS
8157 tree domain;
8158
8159 domain = build_index_type (maxindex);
8160 TYPE_DOMAIN (type) = domain;
51c184be 8161
dff6b454 8162 if (! TREE_TYPE (maxindex))
6ab5c740 8163 TREE_TYPE (maxindex) = domain;
51c184be
MS
8164 if (initial_value)
8165 itype = TREE_TYPE (initial_value);
8166 else
8167 itype = NULL;
8168 if (itype && !TYPE_DOMAIN (itype))
6ab5c740 8169 TYPE_DOMAIN (itype) = domain;
dff6b454
RK
8170 /* The type of the main variant should never be used for arrays
8171 of different sizes. It should only ever be completed with the
8172 size of the array. */
8173 if (! TYPE_DOMAIN (TYPE_MAIN_VARIANT (type)))
6ab5c740 8174 TYPE_DOMAIN (TYPE_MAIN_VARIANT (type)) = domain;
8d08fdba
MS
8175 }
8176
6ab5c740
NS
8177 pop_obstacks();
8178
8d08fdba
MS
8179 /* Lay out the type now that we can get the real answer. */
8180
8181 layout_type (type);
8182
8183 return value;
8184}
8185\f
8186/* Return zero if something is declared to be a member of type
8187 CTYPE when in the context of CUR_TYPE. STRING is the error
8188 message to print in that case. Otherwise, quietly return 1. */
e92cc029 8189
8d08fdba
MS
8190static int
8191member_function_or_else (ctype, cur_type, string)
8192 tree ctype, cur_type;
d8e178a0 8193 const char *string;
8d08fdba
MS
8194{
8195 if (ctype && ctype != cur_type)
8196 {
8197 error (string, TYPE_NAME_STRING (ctype));
8198 return 0;
8199 }
8200 return 1;
8201}
8202\f
8203/* Subroutine of `grokdeclarator'. */
8204
8205/* Generate errors possibly applicable for a given set of specifiers.
8206 This is for ARM $7.1.2. */
e92cc029 8207
8d08fdba
MS
8208static void
8209bad_specifiers (object, type, virtualp, quals, inlinep, friendp, raises)
8210 tree object;
d8e178a0 8211 const char *type;
8d08fdba
MS
8212 int virtualp, quals, friendp, raises, inlinep;
8213{
8214 if (virtualp)
8251199e 8215 cp_error ("`%D' declared as a `virtual' %s", object, type);
8d08fdba 8216 if (inlinep)
8251199e 8217 cp_error ("`%D' declared as an `inline' %s", object, type);
8d08fdba 8218 if (quals)
8251199e 8219 cp_error ("`const' and `volatile' function specifiers on `%D' invalid in %s declaration",
a28e3c7f 8220 object, type);
8d08fdba 8221 if (friendp)
f8e55f34 8222 cp_error_at ("`%D' declared as a friend", object);
f3e4d63c 8223 if (raises)
f8e55f34 8224 cp_error_at ("`%D' declared with an exception specification", object);
8d08fdba
MS
8225}
8226
8227/* CTYPE is class type, or null if non-class.
8228 TYPE is type this FUNCTION_DECL should have, either FUNCTION_TYPE
8229 or METHOD_TYPE.
8230 DECLARATOR is the function's name.
8231 VIRTUALP is truthvalue of whether the function is virtual or not.
8232 FLAGS are to be passed through to `grokclassfn'.
8233 QUALS are qualifiers indicating whether the function is `const'
8234 or `volatile'.
8235 RAISES is a list of exceptions that this function can raise.
8236 CHECK is 1 if we must find this method in CTYPE, 0 if we should
3ddfb0e6
MM
8237 not look, and -1 if we should not call `grokclassfn' at all.
8238
20496fa2 8239 Returns `NULL_TREE' if something goes wrong, after issuing
3ddfb0e6 8240 applicable error messages. */
e92cc029 8241
8d08fdba 8242static tree
386b8a85 8243grokfndecl (ctype, type, declarator, orig_declarator, virtualp, flags, quals,
7a8f9fa9 8244 raises, check, friendp, publicp, inlinep, funcdef_flag,
2c73f9f5 8245 template_count, in_namespace)
8d08fdba
MS
8246 tree ctype, type;
8247 tree declarator;
386b8a85 8248 tree orig_declarator;
8d08fdba
MS
8249 int virtualp;
8250 enum overload_flags flags;
7a8f9fa9 8251 tree quals, raises;
386b8a85 8252 int check, friendp, publicp, inlinep, funcdef_flag, template_count;
2c73f9f5 8253 tree in_namespace;
8d08fdba
MS
8254{
8255 tree cname, decl;
8256 int staticp = ctype && TREE_CODE (type) == FUNCTION_TYPE;
856216bb 8257 int has_default_arg = 0;
42976354 8258 tree t;
8d08fdba
MS
8259
8260 if (ctype)
8261 cname = TREE_CODE (TYPE_NAME (ctype)) == TYPE_DECL
8262 ? TYPE_IDENTIFIER (ctype) : TYPE_NAME (ctype);
8263 else
8264 cname = NULL_TREE;
8265
8266 if (raises)
8267 {
f30432d7 8268 type = build_exception_variant (type, raises);
8d08fdba 8269 }
c11b6f21 8270
8d08fdba 8271 decl = build_lang_decl (FUNCTION_DECL, declarator, type);
2c73f9f5 8272 /* Propagate volatile out from type to decl. */
8d08fdba 8273 if (TYPE_VOLATILE (type))
893de33c 8274 TREE_THIS_VOLATILE (decl) = 1;
8d08fdba 8275
79c4d4b7 8276 /* If this decl has namespace scope, set that up. */
2c73f9f5 8277 if (in_namespace)
b262d64c 8278 set_decl_namespace (decl, in_namespace, friendp);
79c4d4b7
JM
8279 else if (publicp && ! ctype)
8280 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
2c73f9f5 8281
0f8766b8
JM
8282 /* `main' and builtins have implicit 'C' linkage. */
8283 if ((MAIN_NAME_P (declarator)
8284 || (IDENTIFIER_LENGTH (declarator) > 10
8285 && IDENTIFIER_POINTER (declarator)[0] == '_'
8286 && IDENTIFIER_POINTER (declarator)[1] == '_'
8287 && strncmp (IDENTIFIER_POINTER (declarator)+2, "builtin_", 8) == 0))
8288 && current_lang_name == lang_name_cplusplus
94706a5c 8289 && ctype == NULL_TREE
79c4d4b7
JM
8290 /* NULL_TREE means global namespace. */
8291 && DECL_CONTEXT (decl) == NULL_TREE)
0f8766b8
JM
8292 DECL_LANGUAGE (decl) = lang_c;
8293
8d08fdba
MS
8294 /* Should probably propagate const out from type to decl I bet (mrs). */
8295 if (staticp)
8296 {
8297 DECL_STATIC_FUNCTION_P (decl) = 1;
8298 DECL_CONTEXT (decl) = ctype;
8d08fdba
MS
8299 }
8300
e76a2646
MS
8301 if (ctype)
8302 DECL_CLASS_CONTEXT (decl) = ctype;
8303
0f8766b8 8304 if (ctype == NULL_TREE && DECL_MAIN_P (decl))
faae18ab 8305 {
848b92e1
JM
8306 if (processing_template_decl)
8307 error ("cannot declare `main' to be a template");
faae18ab 8308 if (inlinep)
8251199e 8309 error ("cannot declare `main' to be inline");
faae18ab 8310 else if (! publicp)
8251199e 8311 error ("cannot declare `main' to be static");
faae18ab
MS
8312 inlinep = 0;
8313 publicp = 1;
8314 }
50a6dbd7 8315
59e76fc6
JM
8316 /* Members of anonymous types and local classes have no linkage; make
8317 them internal. */
8318 if (ctype && (ANON_AGGRNAME_P (TYPE_IDENTIFIER (ctype))
8319 || hack_decl_function_context (TYPE_MAIN_DECL (ctype))))
50a6dbd7
JM
8320 publicp = 0;
8321
8322 if (publicp)
8323 {
8324 /* [basic.link]: A name with no linkage (notably, the name of a class
8325 or enumeration declared in a local scope) shall not be used to
8326 declare an entity with linkage.
8327
8328 Only check this for public decls for now. */
8329 t = no_linkage_check (TREE_TYPE (decl));
8330 if (t)
8331 {
7f7c930e
JM
8332 if (ANON_AGGRNAME_P (TYPE_IDENTIFIER (t)))
8333 {
8334 if (DECL_LANGUAGE (decl) == lang_c)
8335 /* Allow this; it's pretty common in C. */;
8336 else
8337 cp_pedwarn ("non-local function `%#D' uses anonymous type",
8338 decl);
8339 }
50a6dbd7 8340 else
8251199e 8341 cp_pedwarn ("non-local function `%#D' uses local type `%T'",
cce2be43 8342 decl, t);
50a6dbd7
JM
8343 }
8344 }
8345
893de33c 8346 TREE_PUBLIC (decl) = publicp;
faae18ab 8347 if (! publicp)
893de33c
JM
8348 {
8349 DECL_INTERFACE_KNOWN (decl) = 1;
8350 DECL_NOT_REALLY_EXTERN (decl) = 1;
8351 }
faae18ab
MS
8352
8353 if (inlinep)
8354 DECL_THIS_INLINE (decl) = DECL_INLINE (decl) = 1;
8d08fdba
MS
8355
8356 DECL_EXTERNAL (decl) = 1;
8357 if (quals != NULL_TREE && TREE_CODE (type) == FUNCTION_TYPE)
8358 {
8251199e 8359 cp_error ("%smember function `%D' cannot have `%T' method qualifier",
8d08fdba
MS
8360 (ctype ? "static " : "non-"), decl, TREE_VALUE (quals));
8361 quals = NULL_TREE;
8362 }
8363
8364 if (IDENTIFIER_OPNAME_P (DECL_NAME (decl)))
8365 grok_op_properties (decl, virtualp, check < 0);
8366
e76a2646 8367 if (ctype && hack_decl_function_context (decl))
893de33c 8368 DECL_NO_STATIC_CHAIN (decl) = 1;
e76a2646 8369
42976354
BK
8370 for (t = TYPE_ARG_TYPES (TREE_TYPE (decl)); t; t = TREE_CHAIN (t))
8371 if (TREE_PURPOSE (t)
8372 && TREE_CODE (TREE_PURPOSE (t)) == DEFAULT_ARG)
8373 {
856216bb 8374 has_default_arg = 1;
42976354
BK
8375 break;
8376 }
8377
f9d94ea4
JM
8378 if (friendp
8379 && TREE_CODE (orig_declarator) == TEMPLATE_ID_EXPR)
8380 {
8381 if (funcdef_flag)
8251199e
JM
8382 cp_error
8383 ("defining explicit specialization `%D' in friend declaration",
f9d94ea4
JM
8384 orig_declarator);
8385 else
8386 {
7e2421f7
MM
8387 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
8388 {
8389 /* Something like `template <class T> friend void f<T>()'. */
8251199e 8390 cp_error ("template-id `%D' in declaration of primary template",
7e2421f7 8391 orig_declarator);
20496fa2 8392 return NULL_TREE;
7e2421f7
MM
8393 }
8394
856216bb 8395
f9d94ea4
JM
8396 /* A friend declaration of the form friend void f<>(). Record
8397 the information in the TEMPLATE_ID_EXPR. */
8398 SET_DECL_IMPLICIT_INSTANTIATION (decl);
8399 DECL_TEMPLATE_INFO (decl)
e1b3e07d
MM
8400 = tree_cons (TREE_OPERAND (orig_declarator, 0),
8401 TREE_OPERAND (orig_declarator, 1),
8402 NULL_TREE);
856216bb
MM
8403
8404 if (has_default_arg)
8405 {
8406 cp_error ("default arguments are not allowed in declaration of friend template specialization `%D'",
8407 decl);
8408 return NULL_TREE;
8409 }
8410
8411 if (inlinep)
8412 {
8413 cp_error ("`inline' is not allowed in declaration of friend template specialization `%D'",
8414 decl);
8415 return NULL_TREE;
8416 }
f9d94ea4 8417 }
f84b4be9 8418 }
386b8a85 8419
856216bb
MM
8420 if (has_default_arg)
8421 add_defarg_fn (decl);
8422
0f8766b8
JM
8423 /* Plain overloading: will not be grok'd by grokclassfn. */
8424 if (! ctype && ! processing_template_decl
8425 && DECL_LANGUAGE (decl) != lang_c
8426 && (! DECL_USE_TEMPLATE (decl) || name_mangling_version < 1))
8427 set_mangled_name_for_decl (decl);
8428
1eb0072d
JM
8429 if (funcdef_flag)
8430 /* Make the init_value nonzero so pushdecl knows this is not
8431 tentative. error_mark_node is replaced later with the BLOCK. */
8432 DECL_INITIAL (decl) = error_mark_node;
8433
75650646 8434 /* Caller will do the rest of this. */
8d08fdba
MS
8435 if (check < 0)
8436 return decl;
8437
5566b478 8438 if (check && funcdef_flag)
d2e5ee5c 8439 DECL_INITIAL (decl) = error_mark_node;
5566b478 8440
8d08fdba
MS
8441 if (flags == NO_SPECIAL && ctype && constructor_name (cname) == declarator)
8442 {
8443 tree tmp;
8444 /* Just handle constructors here. We could do this
8445 inside the following if stmt, but I think
8446 that the code is more legible by breaking this
8447 case out. See comments below for what each of
8448 the following calls is supposed to do. */
8449 DECL_CONSTRUCTOR_P (decl) = 1;
8450
b370501f 8451 grokclassfn (ctype, decl, flags, quals);
386b8a85 8452
e1467ff2
MM
8453 decl = check_explicit_specialization (orig_declarator, decl,
8454 template_count,
f84b4be9
JM
8455 2 * (funcdef_flag != 0) +
8456 4 * (friendp != 0));
6c30752f 8457 if (decl == error_mark_node)
20496fa2 8458 return NULL_TREE;
75650646 8459
b370501f 8460 if ((! TYPE_FOR_JAVA (ctype) || check_java_method (decl))
eff71ab0 8461 && check)
5566b478
MS
8462 {
8463 tmp = check_classfn (ctype, decl);
98c1c668
JM
8464
8465 if (tmp && TREE_CODE (tmp) == TEMPLATE_DECL)
8466 tmp = DECL_TEMPLATE_RESULT(tmp);
8467
e349ee73 8468 if (tmp && DECL_ARTIFICIAL (tmp))
8251199e 8469 cp_error ("definition of implicitly-declared `%D'", tmp);
5566b478
MS
8470 if (tmp && duplicate_decls (decl, tmp))
8471 return tmp;
8472 }
a0a33927 8473 if (! grok_ctor_properties (ctype, decl))
20496fa2 8474 return NULL_TREE;
8d08fdba
MS
8475 }
8476 else
8477 {
8478 tree tmp;
8479
8480 /* Function gets the ugly name, field gets the nice one.
8481 This call may change the type of the function (because
8482 of default parameters)! */
8483 if (ctype != NULL_TREE)
b370501f 8484 grokclassfn (ctype, decl, flags, quals);
8d08fdba 8485
e1467ff2
MM
8486 decl = check_explicit_specialization (orig_declarator, decl,
8487 template_count,
f84b4be9
JM
8488 2 * (funcdef_flag != 0) +
8489 4 * (friendp != 0));
6c30752f 8490 if (decl == error_mark_node)
20496fa2 8491 return NULL_TREE;
6c30752f 8492
eff71ab0 8493 if (ctype != NULL_TREE
b370501f 8494 && (! TYPE_FOR_JAVA (ctype) || check_java_method (decl))
eff71ab0 8495 && check)
5566b478
MS
8496 {
8497 tmp = check_classfn (ctype, decl);
98c1c668
JM
8498
8499 if (tmp && TREE_CODE (tmp) == TEMPLATE_DECL)
75650646 8500 tmp = DECL_TEMPLATE_RESULT (tmp);
98c1c668 8501
5566b478
MS
8502 if (tmp && DECL_STATIC_FUNCTION_P (tmp)
8503 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE)
8504 {
8505 /* Remove the `this' parm added by grokclassfn.
8506 XXX Isn't this done in start_function, too? */
8507 revert_static_member_fn (&decl, NULL, NULL);
8508 last_function_parms = TREE_CHAIN (last_function_parms);
8509 }
e349ee73 8510 if (tmp && DECL_ARTIFICIAL (tmp))
8251199e 8511 cp_error ("definition of implicitly-declared `%D'", tmp);
7834ab39
MS
8512 if (tmp)
8513 {
03017874
MM
8514 /* Attempt to merge the declarations. This can fail, in
8515 the case of some illegal specialization declarations. */
7834ab39 8516 if (!duplicate_decls (decl, tmp))
03017874
MM
8517 cp_error ("no `%#D' member function declared in class `%T'",
8518 decl, ctype);
7834ab39
MS
8519 return tmp;
8520 }
5566b478 8521 }
8d08fdba
MS
8522
8523 if (ctype == NULL_TREE || check)
8524 return decl;
8525
8d08fdba
MS
8526 if (virtualp)
8527 {
2ee887f2 8528 DECL_VIRTUAL_P (decl) = 1;
8d08fdba
MS
8529 if (DECL_VINDEX (decl) == NULL_TREE)
8530 DECL_VINDEX (decl) = error_mark_node;
8531 IDENTIFIER_VIRTUAL_P (DECL_NAME (decl)) = 1;
8d08fdba
MS
8532 }
8533 }
8534 return decl;
8535}
8536
8537static tree
2c73f9f5 8538grokvardecl (type, declarator, specbits_in, initialized, constp, in_namespace)
8d08fdba
MS
8539 tree type;
8540 tree declarator;
d2e5ee5c 8541 RID_BIT_TYPE *specbits_in;
8d08fdba 8542 int initialized;
a9aedbc2 8543 int constp;
2c73f9f5 8544 tree in_namespace;
8d08fdba
MS
8545{
8546 tree decl;
f7da6097
MS
8547 RID_BIT_TYPE specbits;
8548
8549 specbits = *specbits_in;
8d08fdba
MS
8550
8551 if (TREE_CODE (type) == OFFSET_TYPE)
8552 {
8553 /* If you declare a static member so that it
8554 can be initialized, the code will reach here. */
5b605f68
MS
8555 tree basetype = TYPE_OFFSET_BASETYPE (type);
8556 type = TREE_TYPE (type);
4ce3d537 8557 decl = build_lang_decl (VAR_DECL, declarator, type);
5b605f68
MS
8558 DECL_CONTEXT (decl) = basetype;
8559 DECL_CLASS_CONTEXT (decl) = basetype;
f376e137 8560 DECL_ASSEMBLER_NAME (decl) = build_static_name (basetype, declarator);
8d08fdba
MS
8561 }
8562 else
30394414 8563 {
79c4d4b7
JM
8564 tree context;
8565
8566 if (in_namespace)
8567 context = in_namespace;
8568 else if (namespace_bindings_p () || RIDBIT_SETP (RID_EXTERN, specbits))
8569 context = current_namespace;
820fcad8
JM
8570 else
8571 context = NULL_TREE;
79c4d4b7 8572
9188c363
MM
8573 if (processing_template_decl)
8574 {
8575 /* If we're in a template, we need DECL_LANG_SPECIFIC so that
8576 we can call push_template_decl. */
8577 push_permanent_obstack ();
c82dbd95 8578 decl = build_lang_decl (VAR_DECL, declarator, type);
9188c363
MM
8579 pop_obstacks ();
8580 }
8581 else
c82dbd95 8582 decl = build_decl (VAR_DECL, declarator, type);
79c4d4b7
JM
8583
8584 if (context)
b262d64c 8585 set_decl_namespace (decl, context, 0);
79c4d4b7
JM
8586
8587 context = DECL_CONTEXT (decl);
8588 if (declarator && context && current_lang_name != lang_name_c)
8589 DECL_ASSEMBLER_NAME (decl) = build_static_name (context, declarator);
30394414 8590 }
6060a796 8591
2c73f9f5 8592 if (in_namespace)
b262d64c 8593 set_decl_namespace (decl, in_namespace, 0);
2c73f9f5 8594
8d08fdba
MS
8595 if (RIDBIT_SETP (RID_EXTERN, specbits))
8596 {
8597 DECL_THIS_EXTERN (decl) = 1;
8598 DECL_EXTERNAL (decl) = !initialized;
8599 }
8600
8601 /* In class context, static means one per class,
8602 public access, and static storage. */
2b9dc906 8603 if (DECL_CLASS_SCOPE_P (decl))
8d08fdba
MS
8604 {
8605 TREE_PUBLIC (decl) = 1;
8606 TREE_STATIC (decl) = 1;
5b605f68 8607 DECL_EXTERNAL (decl) = 0;
8d08fdba
MS
8608 }
8609 /* At top level, either `static' or no s.c. makes a definition
8610 (perhaps tentative), and absence of `static' makes it public. */
a9aedbc2 8611 else if (toplevel_bindings_p ())
8d08fdba 8612 {
a9aedbc2 8613 TREE_PUBLIC (decl) = (RIDBIT_NOTSETP (RID_STATIC, specbits)
9fffd093 8614 && (DECL_THIS_EXTERN (decl) || ! constp));
8d08fdba
MS
8615 TREE_STATIC (decl) = ! DECL_EXTERNAL (decl);
8616 }
8617 /* Not at top level, only `static' makes a static definition. */
8618 else
8619 {
8620 TREE_STATIC (decl) = !! RIDBIT_SETP (RID_STATIC, specbits);
8621 TREE_PUBLIC (decl) = DECL_EXTERNAL (decl);
8622 }
50a6dbd7
JM
8623
8624 if (TREE_PUBLIC (decl))
8625 {
8626 /* [basic.link]: A name with no linkage (notably, the name of a class
8627 or enumeration declared in a local scope) shall not be used to
8628 declare an entity with linkage.
8629
8630 Only check this for public decls for now. */
8631 tree t = no_linkage_check (TREE_TYPE (decl));
8632 if (t)
8633 {
8634 if (ANON_AGGRNAME_P (TYPE_IDENTIFIER (t)))
8635 /* Ignore for now; `enum { foo } e' is pretty common. */;
8636 else
8251199e 8637 cp_pedwarn ("non-local variable `%#D' uses local type `%T'",
50a6dbd7
JM
8638 decl, t);
8639 }
8640 }
8641
8d08fdba
MS
8642 return decl;
8643}
8644
d8f8dca1
MM
8645/* Create and return a canonical pointer to member function type, for
8646 TYPE, which is a POINTER_TYPE to a METHOD_TYPE. */
8d08fdba
MS
8647
8648tree
8649build_ptrmemfunc_type (type)
8650 tree type;
8651{
8652 tree fields[4];
8653 tree t;
8654 tree u;
46cbda4a 8655 tree unqualified_variant = NULL_TREE;
8d08fdba
MS
8656
8657 /* If a canonical type already exists for this type, use it. We use
8658 this method instead of type_hash_canon, because it only does a
8659 simple equality check on the list of field members. */
8660
8661 if ((t = TYPE_GET_PTRMEMFUNC_TYPE (type)))
8662 return t;
8663
46cbda4a
MM
8664 /* Make sure that we always have the unqualified pointer-to-member
8665 type first. */
8666 if (CP_TYPE_QUALS (type) != TYPE_UNQUALIFIED)
8667 unqualified_variant
8668 = build_ptrmemfunc_type (TYPE_MAIN_VARIANT (type));
8669
8d08fdba
MS
8670 push_obstacks (TYPE_OBSTACK (type), TYPE_OBSTACK (type));
8671
8672 u = make_lang_type (UNION_TYPE);
7ddedda4 8673 SET_IS_AGGR_TYPE (u, 0);
4ce3d537
MM
8674 fields[0] = build_lang_decl (FIELD_DECL, pfn_identifier, type);
8675 fields[1] = build_lang_decl (FIELD_DECL, delta2_identifier,
8676 delta_type_node);
8d08fdba
MS
8677 finish_builtin_type (u, "__ptrmemfunc_type", fields, 1, ptr_type_node);
8678 TYPE_NAME (u) = NULL_TREE;
8679
8680 t = make_lang_type (RECORD_TYPE);
8681
2c73f9f5 8682 /* Let the front-end know this is a pointer to member function... */
db5ae43f 8683 TYPE_PTRMEMFUNC_FLAG (t) = 1;
2c73f9f5 8684 /* ... and not really an aggregate. */
7ddedda4 8685 SET_IS_AGGR_TYPE (t, 0);
8d08fdba 8686
4ce3d537
MM
8687 fields[0] = build_lang_decl (FIELD_DECL, delta_identifier,
8688 delta_type_node);
8689 fields[1] = build_lang_decl (FIELD_DECL, index_identifier,
8690 delta_type_node);
8691 fields[2] = build_lang_decl (FIELD_DECL, pfn_or_delta2_identifier, u);
8d08fdba
MS
8692 finish_builtin_type (t, "__ptrmemfunc_type", fields, 2, ptr_type_node);
8693
8694 pop_obstacks ();
8695
8696 /* Zap out the name so that the back-end will give us the debugging
8697 information for this anonymous RECORD_TYPE. */
8698 TYPE_NAME (t) = NULL_TREE;
8699
46cbda4a
MM
8700 /* If this is not the unqualified form of this pointer-to-member
8701 type, set the TYPE_MAIN_VARIANT for this type to be the
8702 unqualified type. Since they are actually RECORD_TYPEs that are
8703 not variants of each other, we must do this manually. */
8704 if (CP_TYPE_QUALS (type) != TYPE_UNQUALIFIED)
8705 {
8706 t = build_qualified_type (t, CP_TYPE_QUALS (type));
8707 TYPE_MAIN_VARIANT (t) = unqualified_variant;
8708 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (unqualified_variant);
8709 TYPE_NEXT_VARIANT (unqualified_variant) = t;
8710 }
8711
8712 /* Cache this pointer-to-member type so that we can find it again
8713 later. */
8d08fdba
MS
8714 TYPE_SET_PTRMEMFUNC_TYPE (type, t);
8715
e92cc029 8716 /* Seems to be wanted. */
8d08fdba 8717 CLASSTYPE_GOT_SEMICOLON (t) = 1;
46cbda4a 8718
8d08fdba
MS
8719 return t;
8720}
8721
b17e2870
JM
8722/* DECL is a VAR_DECL defined in-class, whose TYPE is also given.
8723 Check to see that the definition is valid. Issue appropriate error
8724 messages. Return 1 if the definition is particularly bad, or 0
8725 otherwise. */
8726
8727int
8728check_static_variable_definition (decl, type)
8729 tree decl;
8730 tree type;
8731{
8732 /* Motion 10 at San Diego: If a static const integral data member is
8733 initialized with an integral constant expression, the initializer
8734 may appear either in the declaration (within the class), or in
8735 the definition, but not both. If it appears in the class, the
8736 member is a member constant. The file-scope definition is always
8737 required. */
8738 if (CLASS_TYPE_P (type) || TREE_CODE (type) == REFERENCE_TYPE)
8739 {
8740 cp_error ("in-class initialization of static data member of non-integral type `%T'",
8741 type);
8742 /* If we just return the declaration, crashes will sometimes
8743 occur. We therefore return void_type_node, as if this was a
8744 friend declaration, to cause callers to completely ignore
8745 this declaration. */
8746 return 1;
8747 }
8748 else if (!CP_TYPE_CONST_P (type))
8749 cp_error ("ANSI C++ forbids in-class initialization of non-const static member `%D'",
8750 decl);
8751 else if (pedantic && !INTEGRAL_TYPE_P (type))
8752 cp_pedwarn ("ANSI C++ forbids initialization of member constant `%D' of non-integral type `%T'", decl, type);
8753
8754 return 0;
8755}
8756
8d08fdba
MS
8757/* Given declspecs and a declarator,
8758 determine the name and type of the object declared
8759 and construct a ..._DECL node for it.
8760 (In one case we can return a ..._TYPE node instead.
8761 For invalid input we sometimes return 0.)
8762
8763 DECLSPECS is a chain of tree_list nodes whose value fields
8764 are the storage classes and type specifiers.
8765
8766 DECL_CONTEXT says which syntactic context this declaration is in:
8767 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
8768 FUNCDEF for a function definition. Like NORMAL but a few different
8769 error messages in each case. Return value may be zero meaning
8770 this definition is too screwy to try to parse.
8771 MEMFUNCDEF for a function definition. Like FUNCDEF but prepares to
8772 handle member functions (which have FIELD context).
8773 Return value may be zero meaning this definition is too screwy to
8774 try to parse.
8775 PARM for a parameter declaration (either within a function prototype
8776 or before a function body). Make a PARM_DECL, or return void_type_node.
db5ae43f 8777 CATCHPARM for a parameter declaration before a catch clause.
8d08fdba
MS
8778 TYPENAME if for a typename (in a cast or sizeof).
8779 Don't make a DECL node; just return the ..._TYPE node.
8780 FIELD for a struct or union field; make a FIELD_DECL.
8781 BITFIELD for a field with specified width.
8782 INITIALIZED is 1 if the decl has an initializer.
8783
b17e2870
JM
8784 ATTRLIST is a TREE_LIST node with prefix attributes in TREE_VALUE and
8785 normal attributes in TREE_PURPOSE, or NULL_TREE.
8786
8d08fdba
MS
8787 In the TYPENAME case, DECLARATOR is really an absolute declarator.
8788 It may also be so in the PARM case, for a prototype where the
8789 argument type is specified but not the name.
8790
8791 This function is where the complicated C meanings of `static'
8792 and `extern' are interpreted.
8793
8794 For C++, if there is any monkey business to do, the function which
8795 calls this one must do it, i.e., prepending instance variables,
8796 renaming overloaded function names, etc.
8797
8798 Note that for this C++, it is an error to define a method within a class
8799 which does not belong to that class.
8800
8801 Except in the case where SCOPE_REFs are implicitly known (such as
8802 methods within a class being redundantly qualified),
8803 declarations which involve SCOPE_REFs are returned as SCOPE_REFs
8804 (class_name::decl_name). The caller must also deal with this.
8805
8806 If a constructor or destructor is seen, and the context is FIELD,
8807 then the type gains the attribute TREE_HAS_x. If such a declaration
8808 is erroneous, NULL_TREE is returned.
8809
8810 QUALS is used only for FUNCDEF and MEMFUNCDEF cases. For a member
8811 function, these are the qualifiers to give to the `this' pointer.
8812
8813 May return void_type_node if the declarator turned out to be a friend.
8814 See grokfield for details. */
8815
8816enum return_types { return_normal, return_ctor, return_dtor, return_conversion };
8817
8818tree
c11b6f21 8819grokdeclarator (declarator, declspecs, decl_context, initialized, attrlist)
8d08fdba
MS
8820 tree declspecs;
8821 tree declarator;
8822 enum decl_context decl_context;
8823 int initialized;
c11b6f21 8824 tree attrlist;
8d08fdba
MS
8825{
8826 RID_BIT_TYPE specbits;
8827 int nclasses = 0;
8828 tree spec;
8829 tree type = NULL_TREE;
8830 int longlong = 0;
8831 int constp;
91063b51 8832 int restrictp;
8d08fdba 8833 int volatilep;
91063b51 8834 int type_quals;
db5ae43f 8835 int virtualp, explicitp, friendp, inlinep, staticp;
8d08fdba
MS
8836 int explicit_int = 0;
8837 int explicit_char = 0;
37c46b43 8838 int defaulted_int = 0;
8d08fdba 8839 tree typedef_decl = NULL_TREE;
9c0758dd 8840 const char *name;
8d08fdba
MS
8841 tree typedef_type = NULL_TREE;
8842 int funcdef_flag = 0;
8843 enum tree_code innermost_code = ERROR_MARK;
8844 int bitfield = 0;
6125f3be
DE
8845#if 0
8846 /* See the code below that used this. */
f6abb50a 8847 tree decl_machine_attr = NULL_TREE;
6125f3be 8848#endif
8d08fdba
MS
8849 /* Set this to error_mark_node for FIELD_DECLs we could not handle properly.
8850 All FIELD_DECLs we build here have `init' put into their DECL_INITIAL. */
8851 tree init = NULL_TREE;
8852
8853 /* Keep track of what sort of function is being processed
8854 so that we can warn about default return values, or explicit
8855 return values which do not match prescribed defaults. */
8856 enum return_types return_type = return_normal;
8857
8858 tree dname = NULL_TREE;
8859 tree ctype = current_class_type;
8860 tree ctor_return_type = NULL_TREE;
8861 enum overload_flags flags = NO_SPECIAL;
8d08fdba 8862 tree quals = NULL_TREE;
c11b6f21 8863 tree raises = NULL_TREE;
386b8a85 8864 int template_count = 0;
2c73f9f5 8865 tree in_namespace = NULL_TREE;
1eab9b56
JM
8866 tree inner_attrs;
8867 int ignore_attrs;
8d08fdba
MS
8868
8869 RIDBIT_RESET_ALL (specbits);
8870 if (decl_context == FUNCDEF)
8871 funcdef_flag = 1, decl_context = NORMAL;
8872 else if (decl_context == MEMFUNCDEF)
8873 funcdef_flag = -1, decl_context = FIELD;
8874 else if (decl_context == BITFIELD)
8875 bitfield = 1, decl_context = FIELD;
8876
8d08fdba
MS
8877 /* Look inside a declarator for the name being declared
8878 and get it as a string, for an error message. */
8879 {
be99da77
MS
8880 tree *next = &declarator;
8881 register tree decl;
8d08fdba
MS
8882 name = NULL;
8883
be99da77
MS
8884 while (next && *next)
8885 {
8886 decl = *next;
8887 switch (TREE_CODE (decl))
8d08fdba 8888 {
52fbc847
JM
8889 case TREE_LIST:
8890 /* For attributes. */
8891 next = &TREE_VALUE (decl);
8892 break;
8893
be99da77
MS
8894 case COND_EXPR:
8895 ctype = NULL_TREE;
8896 next = &TREE_OPERAND (decl, 0);
8897 break;
8d08fdba 8898
2c73f9f5 8899 case BIT_NOT_EXPR: /* For C++ destructors! */
8d08fdba 8900 {
be99da77
MS
8901 tree name = TREE_OPERAND (decl, 0);
8902 tree rename = NULL_TREE;
8903
8904 my_friendly_assert (flags == NO_SPECIAL, 152);
8905 flags = DTOR_FLAG;
8906 return_type = return_dtor;
5566b478
MS
8907 if (TREE_CODE (name) == TYPE_DECL)
8908 TREE_OPERAND (decl, 0) = name = constructor_name (name);
be99da77
MS
8909 my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, 153);
8910 if (ctype == NULL_TREE)
8911 {
8912 if (current_class_type == NULL_TREE)
8913 {
8251199e 8914 error ("destructors must be member functions");
be99da77
MS
8915 flags = NO_SPECIAL;
8916 }
8917 else
8918 {
8919 tree t = constructor_name (current_class_name);
8920 if (t != name)
8921 rename = t;
8922 }
8923 }
8d08fdba 8924 else
be99da77
MS
8925 {
8926 tree t = constructor_name (ctype);
8927 if (t != name)
8928 rename = t;
8929 }
51c184be 8930
be99da77 8931 if (rename)
39211cd5 8932 {
8251199e 8933 cp_error ("destructor `%T' must match class name `%T'",
5566b478 8934 name, rename);
be99da77 8935 TREE_OPERAND (decl, 0) = rename;
39211cd5 8936 }
be99da77 8937 next = &name;
51c184be 8938 }
be99da77 8939 break;
8d08fdba 8940
be99da77 8941 case ADDR_EXPR: /* C++ reference declaration */
2c73f9f5 8942 /* Fall through. */
be99da77
MS
8943 case ARRAY_REF:
8944 case INDIRECT_REF:
8945 ctype = NULL_TREE;
8946 innermost_code = TREE_CODE (decl);
8947 next = &TREE_OPERAND (decl, 0);
8948 break;
8d08fdba 8949
be99da77 8950 case CALL_EXPR:
43f887f9 8951 if (parmlist_is_exprlist (CALL_DECLARATOR_PARMS (decl)))
8d08fdba 8952 {
b17e2870
JM
8953 /* This is actually a variable declaration using
8954 constructor syntax. We need to call start_decl and
8955 cp_finish_decl so we can get the variable
8956 initialized... */
8957
8958 tree attributes, prefix_attributes;
be99da77
MS
8959
8960 *next = TREE_OPERAND (decl, 0);
43f887f9 8961 init = CALL_DECLARATOR_PARMS (decl);
be99da77 8962
b17e2870
JM
8963 if (attrlist)
8964 {
8965 attributes = TREE_PURPOSE (attrlist);
8966 prefix_attributes = TREE_VALUE (attrlist);
8967 }
8968 else
8969 {
8970 attributes = NULL_TREE;
8971 prefix_attributes = NULL_TREE;
8972 }
8973
8974 decl = start_decl (declarator, declspecs, 1,
8975 attributes, prefix_attributes);
1eb0072d
JM
8976 if (decl)
8977 {
8978 /* Look for __unused__ attribute */
8979 if (TREE_USED (TREE_TYPE (decl)))
8980 TREE_USED (decl) = 1;
8981 finish_decl (decl, init, NULL_TREE);
8982 }
8983 else
8984 cp_error ("invalid declarator");
be99da77 8985 return 0;
8d08fdba 8986 }
be99da77
MS
8987 innermost_code = TREE_CODE (decl);
8988 if (decl_context == FIELD && ctype == NULL_TREE)
8989 ctype = current_class_type;
45537677 8990 if (ctype
c11b6f21 8991 && TREE_OPERAND (decl, 0)
45537677
MS
8992 && (TREE_CODE (TREE_OPERAND (decl, 0)) == TYPE_DECL
8993 && ((DECL_NAME (TREE_OPERAND (decl, 0))
8994 == constructor_name_full (ctype))
8995 || (DECL_NAME (TREE_OPERAND (decl, 0))
8996 == constructor_name (ctype)))))
be99da77
MS
8997 TREE_OPERAND (decl, 0) = constructor_name (ctype);
8998 next = &TREE_OPERAND (decl, 0);
8999 decl = *next;
9000 if (ctype != NULL_TREE
9001 && decl != NULL_TREE && flags != DTOR_FLAG
9002 && decl == constructor_name (ctype))
8d08fdba 9003 {
be99da77
MS
9004 return_type = return_ctor;
9005 ctor_return_type = ctype;
8d08fdba 9006 }
be99da77
MS
9007 ctype = NULL_TREE;
9008 break;
386b8a85
JM
9009
9010 case TEMPLATE_ID_EXPR:
9011 {
9012 tree fns = TREE_OPERAND (decl, 0);
9013
9014 if (TREE_CODE (fns) == LOOKUP_EXPR)
9015 fns = TREE_OPERAND (fns, 0);
9016
8f032717
MM
9017 dname = fns;
9018 if (TREE_CODE (dname) == COMPONENT_REF)
9019 dname = TREE_OPERAND (dname, 1);
9020 if (TREE_CODE (dname) != IDENTIFIER_NODE)
9021 {
9022 my_friendly_assert (is_overloaded_fn (dname),
9023 19990331);
9024 dname = DECL_NAME (get_first_fn (dname));
9025 }
386b8a85 9026 }
2c73f9f5 9027 /* Fall through. */
be99da77
MS
9028
9029 case IDENTIFIER_NODE:
386b8a85
JM
9030 if (TREE_CODE (decl) == IDENTIFIER_NODE)
9031 dname = decl;
9032
be99da77
MS
9033 next = 0;
9034
9035 if (is_rid (dname))
8d08fdba 9036 {
8251199e 9037 cp_error ("declarator-id missing; using reserved word `%D'",
be99da77
MS
9038 dname);
9039 name = IDENTIFIER_POINTER (dname);
8d08fdba 9040 }
be99da77 9041 if (! IDENTIFIER_OPNAME_P (dname)
956d6950 9042 /* GNU/Linux headers use '__op'. Arrgh. */
a703fb38 9043 || (IDENTIFIER_TYPENAME_P (dname) && ! TREE_TYPE (dname)))
be99da77 9044 name = IDENTIFIER_POINTER (dname);
8d08fdba
MS
9045 else
9046 {
be99da77 9047 if (IDENTIFIER_TYPENAME_P (dname))
8d08fdba 9048 {
be99da77
MS
9049 my_friendly_assert (flags == NO_SPECIAL, 154);
9050 flags = TYPENAME_FLAG;
9051 ctor_return_type = TREE_TYPE (dname);
9052 return_type = return_conversion;
8d08fdba 9053 }
be99da77 9054 name = operator_name_string (dname);
8d08fdba 9055 }
be99da77 9056 break;
8d08fdba 9057
be99da77
MS
9058 /* C++ extension */
9059 case SCOPE_REF:
9060 {
9061 /* Perform error checking, and decide on a ctype. */
9062 tree cname = TREE_OPERAND (decl, 0);
9063 if (cname == NULL_TREE)
9064 ctype = NULL_TREE;
2c73f9f5
ML
9065 else if (TREE_CODE (cname) == NAMESPACE_DECL)
9066 {
9067 ctype = NULL_TREE;
9068 in_namespace = TREE_OPERAND (decl, 0);
9069 TREE_OPERAND (decl, 0) = NULL_TREE;
9070 }
be99da77
MS
9071 else if (! is_aggr_type (cname, 1))
9072 TREE_OPERAND (decl, 0) = NULL_TREE;
9073 /* Must test TREE_OPERAND (decl, 1), in case user gives
9074 us `typedef (class::memfunc)(int); memfunc *memfuncptr;' */
9075 else if (TREE_OPERAND (decl, 1)
9076 && TREE_CODE (TREE_OPERAND (decl, 1)) == INDIRECT_REF)
9077 ctype = cname;
73b0fce8
KL
9078 else if (TREE_CODE (cname) == TEMPLATE_TYPE_PARM
9079 || TREE_CODE (cname) == TEMPLATE_TEMPLATE_PARM)
5566b478 9080 {
8251199e 9081 cp_error ("`%T::%D' is not a valid declarator", cname,
5566b478 9082 TREE_OPERAND (decl, 1));
8251199e 9083 cp_error (" perhaps you want `typename %T::%D' to make it a type",
5566b478
MS
9084 cname, TREE_OPERAND (decl, 1));
9085 return void_type_node;
9086 }
be99da77
MS
9087 else if (ctype == NULL_TREE)
9088 ctype = cname;
9089 else if (TREE_COMPLEXITY (decl) == current_class_depth)
9090 TREE_OPERAND (decl, 0) = ctype;
9091 else
9092 {
9093 if (! UNIQUELY_DERIVED_FROM_P (cname, ctype))
9094 {
8251199e 9095 cp_error ("type `%T' is not derived from type `%T'",
be99da77
MS
9096 cname, ctype);
9097 TREE_OPERAND (decl, 0) = NULL_TREE;
9098 }
9099 else
9100 ctype = cname;
9101 }
9102
c91a56d2
MS
9103 if (ctype && TREE_CODE (TREE_OPERAND (decl, 1)) == TYPE_DECL
9104 && ((DECL_NAME (TREE_OPERAND (decl, 1))
9105 == constructor_name_full (ctype))
9106 || (DECL_NAME (TREE_OPERAND (decl, 1))
9107 == constructor_name (ctype))))
be99da77
MS
9108 TREE_OPERAND (decl, 1) = constructor_name (ctype);
9109 next = &TREE_OPERAND (decl, 1);
9110 decl = *next;
9111 if (ctype)
9112 {
9113 if (TREE_CODE (decl) == IDENTIFIER_NODE
9114 && constructor_name (ctype) == decl)
9115 {
9116 return_type = return_ctor;
9117 ctor_return_type = ctype;
9118 }
9119 else if (TREE_CODE (decl) == BIT_NOT_EXPR
9120 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
9121 && (constructor_name (ctype) == TREE_OPERAND (decl, 0)
9122 || constructor_name_full (ctype) == TREE_OPERAND (decl, 0)))
9123 {
9124 return_type = return_dtor;
9125 ctor_return_type = ctype;
9126 flags = DTOR_FLAG;
9127 TREE_OPERAND (decl, 0) = constructor_name (ctype);
9128 next = &TREE_OPERAND (decl, 0);
9129 }
9130 }
9131 }
9132 break;
9133
9134 case ERROR_MARK:
9135 next = 0;
9136 break;
9137
45537677
MS
9138 case TYPE_DECL:
9139 /* Parse error puts this typespec where
9140 a declarator should go. */
8251199e 9141 cp_error ("`%T' specified as declarator-id", DECL_NAME (decl));
45537677 9142 if (TREE_TYPE (decl) == current_class_type)
8251199e 9143 cp_error (" perhaps you want `%T' for a constructor",
45537677
MS
9144 current_class_name);
9145 dname = DECL_NAME (decl);
9146 name = IDENTIFIER_POINTER (dname);
9147
e92cc029 9148 /* Avoid giving two errors for this. */
45537677
MS
9149 IDENTIFIER_CLASS_VALUE (dname) = NULL_TREE;
9150
9151 declspecs = temp_tree_cons (NULL_TREE, integer_type_node,
9152 declspecs);
9153 *next = dname;
9154 next = 0;
9155 break;
9156
be99da77 9157 default:
8251199e 9158 cp_compiler_error ("`%D' as declarator", decl);
be99da77 9159 return 0; /* We used to do a 155 abort here. */
8d08fdba 9160 }
be99da77 9161 }
8d08fdba
MS
9162 if (name == NULL)
9163 name = "type name";
9164 }
9165
9166 /* A function definition's declarator must have the form of
9167 a function declarator. */
9168
9169 if (funcdef_flag && innermost_code != CALL_EXPR)
9170 return 0;
9171
e1cd6e56
MS
9172 if (((dname && IDENTIFIER_OPNAME_P (dname)) || flags == TYPENAME_FLAG)
9173 && innermost_code != CALL_EXPR
9174 && ! (ctype && declspecs == NULL_TREE))
9175 {
8251199e 9176 cp_error ("declaration of `%D' as non-function", dname);
e1cd6e56
MS
9177 return void_type_node;
9178 }
9179
8d08fdba
MS
9180 /* Anything declared one level down from the top level
9181 must be one of the parameters of a function
9182 (because the body is at least two levels down). */
9183
9184 /* This heuristic cannot be applied to C++ nodes! Fixed, however,
9185 by not allowing C++ class definitions to specify their parameters
9186 with xdecls (must be spec.d in the parmlist).
9187
9188 Since we now wait to push a class scope until we are sure that
9189 we are in a legitimate method context, we must set oldcname
a9aedbc2
MS
9190 explicitly (since current_class_name is not yet alive).
9191
9192 We also want to avoid calling this a PARM if it is in a namespace. */
8d08fdba 9193
9188c363 9194 if (decl_context == NORMAL && !toplevel_bindings_p ())
a9aedbc2
MS
9195 {
9196 struct binding_level *b = current_binding_level;
9197 current_binding_level = b->level_chain;
9198 if (current_binding_level != 0 && toplevel_bindings_p ())
9199 decl_context = PARM;
9200 current_binding_level = b;
9201 }
8d08fdba
MS
9202
9203 /* Look through the decl specs and record which ones appear.
9204 Some typespecs are defined as built-in typenames.
9205 Others, the ones that are modifiers of other types,
9206 are represented by bits in SPECBITS: set the bits for
9207 the modifiers that appear. Storage class keywords are also in SPECBITS.
9208
9209 If there is a typedef name or a type, store the type in TYPE.
9210 This includes builtin typedefs such as `int'.
9211
9212 Set EXPLICIT_INT if the type is `int' or `char' and did not
9213 come from a user typedef.
9214
9215 Set LONGLONG if `long' is mentioned twice.
9216
9217 For C++, constructors and destructors have their own fast treatment. */
9218
9219 for (spec = declspecs; spec; spec = TREE_CHAIN (spec))
9220 {
9221 register int i;
9222 register tree id;
9223
9224 /* Certain parse errors slip through. For example,
9225 `int class;' is not caught by the parser. Try
9226 weakly to recover here. */
9227 if (TREE_CODE (spec) != TREE_LIST)
9228 return 0;
9229
9230 id = TREE_VALUE (spec);
9231
9232 if (TREE_CODE (id) == IDENTIFIER_NODE)
9233 {
a3203465
MS
9234 if (id == ridpointers[(int) RID_INT]
9235 || id == ridpointers[(int) RID_CHAR]
9236 || id == ridpointers[(int) RID_BOOL]
9237 || id == ridpointers[(int) RID_WCHAR])
8d08fdba
MS
9238 {
9239 if (type)
8ccc31eb
MS
9240 {
9241 if (id == ridpointers[(int) RID_BOOL])
8251199e 9242 error ("`bool' is now a keyword");
8ccc31eb 9243 else
8251199e 9244 cp_error ("extraneous `%T' ignored", id);
8ccc31eb 9245 }
8d08fdba
MS
9246 else
9247 {
a3203465
MS
9248 if (id == ridpointers[(int) RID_INT])
9249 explicit_int = 1;
9250 else if (id == ridpointers[(int) RID_CHAR])
9251 explicit_char = 1;
8d08fdba
MS
9252 type = TREE_TYPE (IDENTIFIER_GLOBAL_VALUE (id));
9253 }
9254 goto found;
9255 }
e92cc029 9256 /* C++ aggregate types. */
8d08fdba
MS
9257 if (IDENTIFIER_HAS_TYPE_VALUE (id))
9258 {
9259 if (type)
8251199e 9260 cp_error ("multiple declarations `%T' and `%T'", type, id);
8d08fdba
MS
9261 else
9262 type = IDENTIFIER_TYPE_VALUE (id);
9263 goto found;
9264 }
9265
f376e137 9266 for (i = (int) RID_FIRST_MODIFIER; i <= (int) RID_LAST_MODIFIER; i++)
8d08fdba
MS
9267 {
9268 if (ridpointers[i] == id)
9269 {
9270 if (i == (int) RID_LONG && RIDBIT_SETP (i, specbits))
9271 {
795add94 9272 if (pedantic && ! in_system_header && warn_long_long)
8251199e 9273 pedwarn ("ANSI C++ does not support `long long'");
9a3b49ac 9274 if (longlong)
8251199e 9275 error ("`long long long' is too long for GCC");
8d08fdba
MS
9276 else
9277 longlong = 1;
9278 }
9279 else if (RIDBIT_SETP (i, specbits))
8251199e 9280 pedwarn ("duplicate `%s'", IDENTIFIER_POINTER (id));
8d08fdba
MS
9281 RIDBIT_SET (i, specbits);
9282 goto found;
9283 }
9284 }
9285 }
e92cc029 9286 /* C++ aggregate types. */
73b0fce8 9287 else if (TREE_CODE (id) == TYPE_DECL || TREE_CODE (id) == TEMPLATE_DECL)
45537677
MS
9288 {
9289 if (type)
8251199e 9290 cp_error ("multiple declarations `%T' and `%T'", type,
45537677
MS
9291 TREE_TYPE (id));
9292 else
5566b478
MS
9293 {
9294 type = TREE_TYPE (id);
9295 TREE_VALUE (spec) = type;
9296 }
45537677
MS
9297 goto found;
9298 }
8d08fdba 9299 if (type)
8251199e 9300 error ("two or more data types in declaration of `%s'", name);
8d08fdba
MS
9301 else if (TREE_CODE (id) == IDENTIFIER_NODE)
9302 {
9303 register tree t = lookup_name (id, 1);
9304 if (!t || TREE_CODE (t) != TYPE_DECL)
8251199e 9305 error ("`%s' fails to be a typedef or built in type",
8d08fdba
MS
9306 IDENTIFIER_POINTER (id));
9307 else
9308 {
9309 type = TREE_TYPE (t);
6125f3be
DE
9310#if 0
9311 /* See the code below that used this. */
f6abb50a 9312 decl_machine_attr = DECL_MACHINE_ATTRIBUTES (id);
6125f3be 9313#endif
8d08fdba
MS
9314 typedef_decl = t;
9315 }
9316 }
bd6dd845 9317 else if (id != error_mark_node)
8d08fdba
MS
9318 /* Can't change CLASS nodes into RECORD nodes here! */
9319 type = id;
9320
9321 found: ;
9322 }
9323
9324 typedef_type = type;
9325
37c46b43 9326 /* No type at all: default to `int', and set DEFAULTED_INT
6eabb241 9327 because it was not a user-defined typedef. */
8d08fdba 9328
a3203465
MS
9329 if (type == NULL_TREE
9330 && (RIDBIT_SETP (RID_SIGNED, specbits)
9331 || RIDBIT_SETP (RID_UNSIGNED, specbits)
9332 || RIDBIT_SETP (RID_LONG, specbits)
9333 || RIDBIT_SETP (RID_SHORT, specbits)))
9334 {
9335 /* These imply 'int'. */
9336 type = integer_type_node;
37c46b43 9337 defaulted_int = 1;
a3203465
MS
9338 }
9339
8d08fdba
MS
9340 if (type == NULL_TREE)
9341 {
9342 explicit_int = -1;
9343 if (return_type == return_dtor)
9344 type = void_type_node;
9345 else if (return_type == return_ctor)
f30432d7 9346 type = build_pointer_type (ctor_return_type);
51c184be
MS
9347 else if (return_type == return_conversion)
9348 type = ctor_return_type;
8d08fdba
MS
9349 else
9350 {
41eff652
JM
9351 /* We handle `main' specially here, because 'main () { }' is so
9352 common. With no options, it is allowed. With -Wreturn-type,
9353 it is a warning. It is only an error with -pedantic-errors. */
9354 int is_main = (funcdef_flag
9355 && MAIN_NAME_P (dname)
9356 && ctype == NULL_TREE
9357 && in_namespace == NULL_TREE
9358 && current_namespace == global_namespace);
9359
d2c192ad 9360 if (in_system_header || flag_ms_extensions)
1f901793
JM
9361 /* Allow it, sigh. */;
9362 else if (pedantic || ! is_main)
ff0cf464
JM
9363 cp_pedwarn ("ANSI C++ forbids declaration `%D' with no type",
9364 dname);
41eff652
JM
9365 else if (warn_return_type)
9366 cp_warning ("ANSI C++ forbids declaration `%D' with no type",
9367 dname);
9368
8d08fdba
MS
9369 type = integer_type_node;
9370 }
9371 }
9372 else if (return_type == return_dtor)
9373 {
8251199e 9374 error ("return type specification for destructor invalid");
8d08fdba
MS
9375 type = void_type_node;
9376 }
9377 else if (return_type == return_ctor)
9378 {
8251199e 9379 error ("return type specification for constructor invalid");
f30432d7 9380 type = build_pointer_type (ctor_return_type);
8d08fdba 9381 }
51c184be
MS
9382 else if (return_type == return_conversion)
9383 {
3bfdc719 9384 if (!same_type_p (type, ctor_return_type))
8251199e 9385 cp_error ("operator `%T' declared to return `%T'",
51c184be
MS
9386 ctor_return_type, type);
9387 else
8251199e 9388 cp_pedwarn ("return type specified for `operator %T'",
51c184be
MS
9389 ctor_return_type);
9390
9391 type = ctor_return_type;
9392 }
8d08fdba
MS
9393
9394 ctype = NULL_TREE;
9395
9396 /* Now process the modifiers that were specified
9397 and check for invalid combinations. */
9398
9399 /* Long double is a special combination. */
9400
9401 if (RIDBIT_SETP (RID_LONG, specbits)
9402 && TYPE_MAIN_VARIANT (type) == double_type_node)
9403 {
9404 RIDBIT_RESET (RID_LONG, specbits);
91063b51
MM
9405 type = build_qualified_type (long_double_type_node,
9406 CP_TYPE_QUALS (type));
8d08fdba
MS
9407 }
9408
9409 /* Check all other uses of type modifiers. */
9410
9411 if (RIDBIT_SETP (RID_UNSIGNED, specbits)
9412 || RIDBIT_SETP (RID_SIGNED, specbits)
9413 || RIDBIT_SETP (RID_LONG, specbits)
9414 || RIDBIT_SETP (RID_SHORT, specbits))
9415 {
9416 int ok = 0;
9417
9418 if (TREE_CODE (type) == REAL_TYPE)
8251199e 9419 error ("short, signed or unsigned invalid for `%s'", name);
b7484fbe 9420 else if (TREE_CODE (type) != INTEGER_TYPE)
8251199e 9421 error ("long, short, signed or unsigned invalid for `%s'", name);
8d08fdba
MS
9422 else if (RIDBIT_SETP (RID_LONG, specbits)
9423 && RIDBIT_SETP (RID_SHORT, specbits))
8251199e 9424 error ("long and short specified together for `%s'", name);
8d08fdba
MS
9425 else if ((RIDBIT_SETP (RID_LONG, specbits)
9426 || RIDBIT_SETP (RID_SHORT, specbits))
9427 && explicit_char)
8251199e 9428 error ("long or short specified with char for `%s'", name);
8d08fdba
MS
9429 else if ((RIDBIT_SETP (RID_LONG, specbits)
9430 || RIDBIT_SETP (RID_SHORT, specbits))
9431 && TREE_CODE (type) == REAL_TYPE)
8251199e 9432 error ("long or short specified with floating type for `%s'", name);
8d08fdba
MS
9433 else if (RIDBIT_SETP (RID_SIGNED, specbits)
9434 && RIDBIT_SETP (RID_UNSIGNED, specbits))
8251199e 9435 error ("signed and unsigned given together for `%s'", name);
8d08fdba
MS
9436 else
9437 {
9438 ok = 1;
37c46b43 9439 if (!explicit_int && !defaulted_int && !explicit_char && pedantic)
8d08fdba 9440 {
8251199e 9441 pedwarn ("long, short, signed or unsigned used invalidly for `%s'",
8d08fdba
MS
9442 name);
9443 if (flag_pedantic_errors)
9444 ok = 0;
9445 }
9446 }
9447
9448 /* Discard the type modifiers if they are invalid. */
9449 if (! ok)
9450 {
9451 RIDBIT_RESET (RID_UNSIGNED, specbits);
9452 RIDBIT_RESET (RID_SIGNED, specbits);
9453 RIDBIT_RESET (RID_LONG, specbits);
9454 RIDBIT_RESET (RID_SHORT, specbits);
9455 longlong = 0;
9456 }
9457 }
9458
37c46b43
MS
9459 if (RIDBIT_SETP (RID_COMPLEX, specbits)
9460 && TREE_CODE (type) != INTEGER_TYPE && TREE_CODE (type) != REAL_TYPE)
9461 {
8251199e 9462 error ("complex invalid for `%s'", name);
37c46b43
MS
9463 RIDBIT_RESET (RID_COMPLEX, specbits);
9464 }
9465
8d08fdba
MS
9466 /* Decide whether an integer type is signed or not.
9467 Optionally treat bitfields as signed by default. */
9468 if (RIDBIT_SETP (RID_UNSIGNED, specbits)
b89c5a7b
MM
9469 /* [class.bit]
9470
9471 It is implementation-defined whether a plain (neither
9472 explicitly signed or unsigned) char, short, int, or long
9473 bit-field is signed or unsigned.
9474
9475 Naturally, we extend this to long long as well. Note that
9476 this does not include wchar_t. */
9477 || (bitfield && !flag_signed_bitfields
9478 && RIDBIT_NOTSETP (RID_SIGNED, specbits)
9479 /* A typedef for plain `int' without `signed' can be
9480 controlled just like plain `int', but a typedef for
9481 `signed int' cannot be so controlled. */
9482 && !(typedef_decl
29bbeb1c 9483 && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl))
b89c5a7b
MM
9484 && (TREE_CODE (type) == INTEGER_TYPE
9485 || TREE_CODE (type) == CHAR_TYPE)
29bbeb1c 9486 && !same_type_p (TYPE_MAIN_VARIANT (type), wchar_type_node)))
8d08fdba
MS
9487 {
9488 if (longlong)
9489 type = long_long_unsigned_type_node;
9490 else if (RIDBIT_SETP (RID_LONG, specbits))
9491 type = long_unsigned_type_node;
9492 else if (RIDBIT_SETP (RID_SHORT, specbits))
9493 type = short_unsigned_type_node;
9494 else if (type == char_type_node)
9495 type = unsigned_char_type_node;
9496 else if (typedef_decl)
9497 type = unsigned_type (type);
9498 else
9499 type = unsigned_type_node;
9500 }
9501 else if (RIDBIT_SETP (RID_SIGNED, specbits)
9502 && type == char_type_node)
9503 type = signed_char_type_node;
9504 else if (longlong)
9505 type = long_long_integer_type_node;
9506 else if (RIDBIT_SETP (RID_LONG, specbits))
9507 type = long_integer_type_node;
9508 else if (RIDBIT_SETP (RID_SHORT, specbits))
9509 type = short_integer_type_node;
9510
37c46b43
MS
9511 if (RIDBIT_SETP (RID_COMPLEX, specbits))
9512 {
9513 /* If we just have "complex", it is equivalent to
9514 "complex double", but if any modifiers at all are specified it is
9515 the complex form of TYPE. E.g, "complex short" is
9516 "complex short int". */
9517
9518 if (defaulted_int && ! longlong
9519 && ! (RIDBIT_SETP (RID_LONG, specbits)
9520 || RIDBIT_SETP (RID_SHORT, specbits)
9521 || RIDBIT_SETP (RID_SIGNED, specbits)
9522 || RIDBIT_SETP (RID_UNSIGNED, specbits)))
9523 type = complex_double_type_node;
9524 else if (type == integer_type_node)
9525 type = complex_integer_type_node;
9526 else if (type == float_type_node)
9527 type = complex_float_type_node;
9528 else if (type == double_type_node)
9529 type = complex_double_type_node;
9530 else if (type == long_double_type_node)
9531 type = complex_long_double_type_node;
9532 else
9533 type = build_complex_type (type);
9534 }
9535
3ac3d9ea
MM
9536 if (return_type == return_conversion
9537 && (RIDBIT_SETP (RID_CONST, specbits)
91063b51
MM
9538 || RIDBIT_SETP (RID_VOLATILE, specbits)
9539 || RIDBIT_SETP (RID_RESTRICT, specbits)))
9540 cp_error ("qualifiers are not allowed on declaration of `operator %T'",
3ac3d9ea
MM
9541 ctor_return_type);
9542
8d08fdba
MS
9543 /* Set CONSTP if this declaration is `const', whether by
9544 explicit specification or via a typedef.
9545 Likewise for VOLATILEP. */
9546
91063b51
MM
9547 constp = !! RIDBIT_SETP (RID_CONST, specbits) + CP_TYPE_CONST_P (type);
9548 restrictp =
9549 !! RIDBIT_SETP (RID_RESTRICT, specbits) + CP_TYPE_RESTRICT_P (type);
9550 volatilep =
9551 !! RIDBIT_SETP (RID_VOLATILE, specbits) + CP_TYPE_VOLATILE_P (type);
9552 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
9553 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
9554 | (volatilep ? TYPE_QUAL_VOLATILE : 0));
9555 type = cp_build_qualified_type (type, type_quals);
8d08fdba
MS
9556 staticp = 0;
9557 inlinep = !! RIDBIT_SETP (RID_INLINE, specbits);
8d08fdba 9558 virtualp = RIDBIT_SETP (RID_VIRTUAL, specbits);
db5ae43f
MS
9559 RIDBIT_RESET (RID_VIRTUAL, specbits);
9560 explicitp = RIDBIT_SETP (RID_EXPLICIT, specbits) != 0;
9561 RIDBIT_RESET (RID_EXPLICIT, specbits);
8d08fdba 9562
8d08fdba
MS
9563 if (RIDBIT_SETP (RID_STATIC, specbits))
9564 staticp = 1 + (decl_context == FIELD);
9565
9566 if (virtualp && staticp == 2)
9567 {
8251199e 9568 cp_error ("member `%D' cannot be declared both virtual and static",
8d08fdba
MS
9569 dname);
9570 staticp = 0;
9571 }
9572 friendp = RIDBIT_SETP (RID_FRIEND, specbits);
8d08fdba
MS
9573 RIDBIT_RESET (RID_FRIEND, specbits);
9574
7e83af84
BK
9575 /* $7.1.2, Function specifiers */
9576 if (friendp && explicitp)
9577 error ("only declarations of constructors can be `explicit'");
9578
8d08fdba
MS
9579 if (RIDBIT_SETP (RID_MUTABLE, specbits))
9580 {
9581 if (decl_context == PARM)
9582 {
8251199e 9583 error ("non-member `%s' cannot be declared `mutable'", name);
8d08fdba
MS
9584 RIDBIT_RESET (RID_MUTABLE, specbits);
9585 }
9586 else if (friendp || decl_context == TYPENAME)
9587 {
8251199e 9588 error ("non-object member `%s' cannot be declared `mutable'", name);
8d08fdba
MS
9589 RIDBIT_RESET (RID_MUTABLE, specbits);
9590 }
8d08fdba
MS
9591 }
9592
9593 /* Warn if two storage classes are given. Default to `auto'. */
9594
9595 if (RIDBIT_ANY_SET (specbits))
9596 {
9597 if (RIDBIT_SETP (RID_STATIC, specbits)) nclasses++;
9598 if (RIDBIT_SETP (RID_EXTERN, specbits)) nclasses++;
9599 if (decl_context == PARM && nclasses > 0)
8251199e 9600 error ("storage class specifiers invalid in parameter declarations");
8d08fdba
MS
9601 if (RIDBIT_SETP (RID_TYPEDEF, specbits))
9602 {
9603 if (decl_context == PARM)
8251199e 9604 error ("typedef declaration invalid in parameter declaration");
8d08fdba
MS
9605 nclasses++;
9606 }
9607 if (RIDBIT_SETP (RID_AUTO, specbits)) nclasses++;
9608 if (RIDBIT_SETP (RID_REGISTER, specbits)) nclasses++;
9609 }
9610
9611 /* Give error if `virtual' is used outside of class declaration. */
b7484fbe
MS
9612 if (virtualp
9613 && (current_class_name == NULL_TREE || decl_context != FIELD))
8d08fdba 9614 {
8251199e 9615 error ("virtual outside class declaration");
8d08fdba
MS
9616 virtualp = 0;
9617 }
9618 if (current_class_name == NULL_TREE && RIDBIT_SETP (RID_MUTABLE, specbits))
9619 {
8251199e 9620 error ("only members can be declared mutable");
8d08fdba
MS
9621 RIDBIT_RESET (RID_MUTABLE, specbits);
9622 }
9623
9624 /* Static anonymous unions are dealt with here. */
9625 if (staticp && decl_context == TYPENAME
9626 && TREE_CODE (declspecs) == TREE_LIST
6bdb8141 9627 && ANON_AGGR_TYPE_P (TREE_VALUE (declspecs)))
8d08fdba
MS
9628 decl_context = FIELD;
9629
8d08fdba
MS
9630 /* Warn about storage classes that are invalid for certain
9631 kinds of declarations (parameters, typenames, etc.). */
9632
9633 if (nclasses > 1)
8251199e 9634 error ("multiple storage classes in declaration of `%s'", name);
8d08fdba
MS
9635 else if (decl_context != NORMAL && nclasses > 0)
9636 {
db5ae43f 9637 if ((decl_context == PARM || decl_context == CATCHPARM)
8d08fdba
MS
9638 && (RIDBIT_SETP (RID_REGISTER, specbits)
9639 || RIDBIT_SETP (RID_AUTO, specbits)))
9640 ;
fc378698
MS
9641 else if (RIDBIT_SETP (RID_TYPEDEF, specbits))
9642 ;
8d08fdba 9643 else if (decl_context == FIELD
8d08fdba
MS
9644 /* C++ allows static class elements */
9645 && RIDBIT_SETP (RID_STATIC, specbits))
9646 /* C++ also allows inlines and signed and unsigned elements,
9647 but in those cases we don't come in here. */
9648 ;
9649 else
9650 {
9651 if (decl_context == FIELD)
9652 {
b7484fbe
MS
9653 tree tmp = NULL_TREE;
9654 register int op = 0;
9655
9656 if (declarator)
9657 {
9e9ff709
MS
9658 /* Avoid trying to get an operand off an identifier node. */
9659 if (TREE_CODE (declarator) == IDENTIFIER_NODE)
9660 tmp = declarator;
9661 else
9662 tmp = TREE_OPERAND (declarator, 0);
b7484fbe
MS
9663 op = IDENTIFIER_OPNAME_P (tmp);
9664 }
8251199e 9665 error ("storage class specified for %s `%s'",
6eabb241 9666 op ? "member operator" : "field",
8d08fdba
MS
9667 op ? operator_name_string (tmp) : name);
9668 }
9669 else
db5ae43f 9670 error (((decl_context == PARM || decl_context == CATCHPARM)
8d08fdba
MS
9671 ? "storage class specified for parameter `%s'"
9672 : "storage class specified for typename"), name);
9673 RIDBIT_RESET (RID_REGISTER, specbits);
9674 RIDBIT_RESET (RID_AUTO, specbits);
9675 RIDBIT_RESET (RID_EXTERN, specbits);
8d08fdba
MS
9676 }
9677 }
9678 else if (RIDBIT_SETP (RID_EXTERN, specbits) && initialized && !funcdef_flag)
9679 {
a9aedbc2 9680 if (toplevel_bindings_p ())
8d08fdba 9681 {
59be0cdd 9682 /* It's common practice (and completely valid) to have a const
8d08fdba 9683 be initialized and declared extern. */
950ad3c3 9684 if (!(type_quals & TYPE_QUAL_CONST))
8251199e 9685 warning ("`%s' initialized and declared `extern'", name);
8d08fdba
MS
9686 }
9687 else
8251199e 9688 error ("`%s' has both `extern' and initializer", name);
8d08fdba
MS
9689 }
9690 else if (RIDBIT_SETP (RID_EXTERN, specbits) && funcdef_flag
a9aedbc2 9691 && ! toplevel_bindings_p ())
8251199e 9692 error ("nested function `%s' declared `extern'", name);
a9aedbc2 9693 else if (toplevel_bindings_p ())
8d08fdba
MS
9694 {
9695 if (RIDBIT_SETP (RID_AUTO, specbits))
8251199e 9696 error ("top-level declaration of `%s' specifies `auto'", name);
8d08fdba
MS
9697 }
9698
909e536a 9699 if (nclasses > 0 && friendp)
8251199e 9700 error ("storage class specifiers invalid in friend function declarations");
909e536a 9701
8d08fdba
MS
9702 /* Now figure out the structure of the declarator proper.
9703 Descend through it, creating more complex types, until we reach
9704 the declared identifier (or NULL_TREE, in an absolute declarator). */
9705
1eab9b56
JM
9706 inner_attrs = NULL_TREE;
9707 ignore_attrs = 0;
9708
386b8a85
JM
9709 while (declarator && TREE_CODE (declarator) != IDENTIFIER_NODE
9710 && TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
8d08fdba
MS
9711 {
9712 /* Each level of DECLARATOR is either an ARRAY_REF (for ...[..]),
9713 an INDIRECT_REF (for *...),
9714 a CALL_EXPR (for ...(...)),
9715 an identifier (for the name being declared)
9716 or a null pointer (for the place in an absolute declarator
9717 where the name was omitted).
9718 For the last two cases, we have just exited the loop.
9719
9720 For C++ it could also be
9721 a SCOPE_REF (for class :: ...). In this case, we have converted
9722 sensible names to types, and those are the values we use to
9723 qualify the member name.
9724 an ADDR_EXPR (for &...),
9725 a BIT_NOT_EXPR (for destructors)
8d08fdba
MS
9726
9727 At this point, TYPE is the type of elements of an array,
9728 or for a function to return, or for a pointer to point to.
9729 After this sequence of ifs, TYPE is the type of the
9730 array or function or pointer, and DECLARATOR has had its
9731 outermost layer removed. */
9732
bd6dd845 9733 if (type == error_mark_node)
8d08fdba
MS
9734 {
9735 if (TREE_CODE (declarator) == SCOPE_REF)
9736 declarator = TREE_OPERAND (declarator, 1);
9737 else
9738 declarator = TREE_OPERAND (declarator, 0);
9739 continue;
9740 }
9741 if (quals != NULL_TREE
9742 && (declarator == NULL_TREE
9743 || TREE_CODE (declarator) != SCOPE_REF))
9744 {
9745 if (ctype == NULL_TREE && TREE_CODE (type) == METHOD_TYPE)
9746 ctype = TYPE_METHOD_BASETYPE (type);
9747 if (ctype != NULL_TREE)
9748 {
8d08fdba 9749 tree dummy = build_decl (TYPE_DECL, NULL_TREE, type);
8d08fdba
MS
9750 ctype = grok_method_quals (ctype, dummy, quals);
9751 type = TREE_TYPE (dummy);
9752 quals = NULL_TREE;
9753 }
9754 }
1eab9b56
JM
9755
9756 /* See the comment for the TREE_LIST case, below. */
9757 if (ignore_attrs)
9758 ignore_attrs = 0;
9759 else if (inner_attrs)
9760 {
9761 decl_attributes (type, inner_attrs, NULL_TREE);
9762 inner_attrs = NULL_TREE;
9763 }
9764
8d08fdba
MS
9765 switch (TREE_CODE (declarator))
9766 {
52fbc847
JM
9767 case TREE_LIST:
9768 {
9769 /* We encode a declarator with embedded attributes using
1eab9b56
JM
9770 a TREE_LIST. The attributes apply to the declarator
9771 directly inside them, so we have to skip an iteration
9772 before applying them to the type. If the declarator just
9773 inside is the declarator-id, we apply the attrs to the
9774 decl itself. */
9775 inner_attrs = TREE_PURPOSE (declarator);
9776 ignore_attrs = 1;
52fbc847 9777 declarator = TREE_VALUE (declarator);
52fbc847
JM
9778 }
9779 break;
9780
8d08fdba
MS
9781 case ARRAY_REF:
9782 {
9783 register tree itype = NULL_TREE;
9784 register tree size = TREE_OPERAND (declarator, 1);
b7484fbe
MS
9785 /* The index is a signed object `sizetype' bits wide. */
9786 tree index_type = signed_type (sizetype);
8d08fdba
MS
9787
9788 declarator = TREE_OPERAND (declarator, 0);
9789
9790 /* Check for some types that there cannot be arrays of. */
9791
a1774733 9792 if (TREE_CODE (type) == VOID_TYPE)
8d08fdba 9793 {
8251199e 9794 cp_error ("declaration of `%D' as array of voids", dname);
8d08fdba
MS
9795 type = error_mark_node;
9796 }
9797
9798 if (TREE_CODE (type) == FUNCTION_TYPE)
9799 {
8251199e 9800 cp_error ("declaration of `%D' as array of functions", dname);
8d08fdba
MS
9801 type = error_mark_node;
9802 }
9803
9804 /* ARM $8.4.3: Since you can't have a pointer to a reference,
9805 you can't have arrays of references. If we allowed them,
59be0cdd 9806 then we'd be saying x[i] is valid for an array x, but
8d08fdba
MS
9807 then you'd have to ask: what does `*(x + i)' mean? */
9808 if (TREE_CODE (type) == REFERENCE_TYPE)
9809 {
9810 if (decl_context == TYPENAME)
8251199e 9811 cp_error ("cannot make arrays of references");
8d08fdba 9812 else
8251199e 9813 cp_error ("declaration of `%D' as array of references",
8d08fdba
MS
9814 dname);
9815 type = error_mark_node;
9816 }
9817
9818 if (TREE_CODE (type) == OFFSET_TYPE)
9819 {
8251199e 9820 cp_error ("declaration of `%D' as array of data members",
8d08fdba
MS
9821 dname);
9822 type = error_mark_node;
9823 }
9824
9825 if (TREE_CODE (type) == METHOD_TYPE)
9826 {
8251199e 9827 cp_error ("declaration of `%D' as array of function members",
8d08fdba
MS
9828 dname);
9829 type = error_mark_node;
9830 }
9831
9832 if (size == error_mark_node)
9833 type = error_mark_node;
3bfdc719
MM
9834 else if (TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type))
9835 {
9836 /* [dcl.array]
9837
9838 the constant expressions that specify the bounds of
9839 the arrays can be omitted only for the first member
9840 of the sequence. */
9841 cp_error ("declaration of `%D' as multidimensional array",
9842 dname);
9843 cp_error ("must have bounds for all dimensions except the first");
9844 type = error_mark_node;
9845 }
8d08fdba
MS
9846
9847 if (type == error_mark_node)
9848 continue;
9849
f2e2cbd4 9850 /* VC++ spells a zero-sized array with []. */
e8ea7036
MM
9851 if (size == NULL_TREE && decl_context == FIELD && ! staticp
9852 && ! RIDBIT_SETP (RID_TYPEDEF, specbits))
f2e2cbd4
JM
9853 size = integer_zero_node;
9854
8d08fdba
MS
9855 if (size)
9856 {
9857 /* Must suspend_momentary here because the index
9858 type may need to live until the end of the function.
9859 For example, it is used in the declaration of a
9860 variable which requires destructing at the end of
9861 the function; then build_vec_delete will need this
9862 value. */
9863 int yes = suspend_momentary ();
2c73f9f5 9864 /* Might be a cast. */
8d08fdba
MS
9865 if (TREE_CODE (size) == NOP_EXPR
9866 && TREE_TYPE (size) == TREE_TYPE (TREE_OPERAND (size, 0)))
9867 size = TREE_OPERAND (size, 0);
9f33663b
JM
9868 if (TREE_READONLY_DECL_P (size))
9869 size = decl_constant_value (size);
8d08fdba 9870
5db698f6
MM
9871 /* If this involves a template parameter, it will be a
9872 constant at instantiation time, but we don't know
9873 what the value is yet. Even if no template
9874 parameters are involved, we may an expression that
9875 is not a constant; we don't even simplify `1 + 2'
9876 when processing a template. */
9877 if (processing_template_decl)
5566b478 9878 {
e97e5263
JM
9879 /* Resolve a qualified reference to an enumerator or
9880 static const data member of ours. */
9881 if (TREE_CODE (size) == SCOPE_REF
9882 && TREE_OPERAND (size, 0) == current_class_type)
9883 {
9884 tree t = lookup_field (current_class_type,
9885 TREE_OPERAND (size, 1), 0, 0);
9886 if (t)
9887 size = t;
9888 }
9889
f05f680a
AO
9890 itype = build_index_type (build_min
9891 (MINUS_EXPR, sizetype, size, integer_one_node));
5566b478
MS
9892 goto dont_grok_size;
9893 }
8d08fdba
MS
9894
9895 if (TREE_CODE (TREE_TYPE (size)) != INTEGER_TYPE
161c12b0
JM
9896 && TREE_CODE (TREE_TYPE (size)) != ENUMERAL_TYPE
9897 && TREE_CODE (TREE_TYPE (size)) != BOOLEAN_TYPE)
8d08fdba 9898 {
8251199e 9899 cp_error ("size of array `%D' has non-integer type",
8d08fdba
MS
9900 dname);
9901 size = integer_one_node;
9902 }
e1cd6e56 9903 if (pedantic && integer_zerop (size))
8251199e 9904 cp_pedwarn ("ANSI C++ forbids zero-size array `%D'", dname);
8d08fdba
MS
9905 if (TREE_CONSTANT (size))
9906 {
dff6b454
RK
9907 int old_flag_pedantic_errors = flag_pedantic_errors;
9908 int old_pedantic = pedantic;
9909 pedantic = flag_pedantic_errors = 1;
9910 /* Always give overflow errors on array subscripts. */
8d08fdba 9911 constant_expression_warning (size);
dff6b454
RK
9912 pedantic = old_pedantic;
9913 flag_pedantic_errors = old_flag_pedantic_errors;
8d08fdba
MS
9914 if (INT_CST_LT (size, integer_zero_node))
9915 {
8251199e 9916 cp_error ("size of array `%D' is negative", dname);
8d08fdba
MS
9917 size = integer_one_node;
9918 }
8d08fdba
MS
9919 }
9920 else
9921 {
e1cd6e56 9922 if (pedantic)
a0a33927
MS
9923 {
9924 if (dname)
8251199e 9925 cp_pedwarn ("ANSI C++ forbids variable-size array `%D'",
a0a33927
MS
9926 dname);
9927 else
8251199e 9928 cp_pedwarn ("ANSI C++ forbids variable-size array");
a0a33927 9929 }
8d08fdba 9930 }
b7484fbe 9931
beb53fb8
JM
9932 itype
9933 = fold (build_binary_op (MINUS_EXPR,
37c46b43
MS
9934 cp_convert (index_type, size),
9935 cp_convert (index_type,
337c90cc 9936 integer_one_node)));
b7484fbe
MS
9937 if (! TREE_CONSTANT (itype))
9938 itype = variable_size (itype);
594740f3
MS
9939 else if (TREE_OVERFLOW (itype))
9940 {
8251199e 9941 error ("overflow in array dimension");
594740f3
MS
9942 TREE_OVERFLOW (itype) = 0;
9943 }
fc378698 9944
5868eb4e 9945 itype = build_index_type (itype);
fc378698 9946
5566b478 9947 dont_grok_size:
8d08fdba
MS
9948 resume_momentary (yes);
9949 }
9950
8d08fdba 9951 type = build_cplus_array_type (type, itype);
8d08fdba
MS
9952 ctype = NULL_TREE;
9953 }
9954 break;
9955
9956 case CALL_EXPR:
9957 {
9958 tree arg_types;
f376e137 9959 int funcdecl_p;
43f887f9 9960 tree inner_parms = CALL_DECLARATOR_PARMS (declarator);
f376e137 9961 tree inner_decl = TREE_OPERAND (declarator, 0);
8d08fdba
MS
9962
9963 /* Declaring a function type.
9964 Make sure we have a valid type for the function to return. */
8d08fdba 9965
91063b51 9966 /* We now know that the TYPE_QUALS don't apply to the
14ae7e7d 9967 decl, but to its return type. */
91063b51 9968 type_quals = TYPE_UNQUALIFIED;
8d08fdba
MS
9969
9970 /* Warn about some types functions can't return. */
9971
9972 if (TREE_CODE (type) == FUNCTION_TYPE)
9973 {
8251199e 9974 error ("`%s' declared as function returning a function", name);
8d08fdba
MS
9975 type = integer_type_node;
9976 }
9977 if (TREE_CODE (type) == ARRAY_TYPE)
9978 {
8251199e 9979 error ("`%s' declared as function returning an array", name);
8d08fdba
MS
9980 type = integer_type_node;
9981 }
9982
f376e137
MS
9983 if (inner_decl && TREE_CODE (inner_decl) == SCOPE_REF)
9984 inner_decl = TREE_OPERAND (inner_decl, 1);
9985
386b8a85
JM
9986 if (inner_decl && TREE_CODE (inner_decl) == TEMPLATE_ID_EXPR)
9987 inner_decl = dname;
9988
b7484fbe 9989 /* Pick up type qualifiers which should be applied to `this'. */
43f887f9 9990 quals = CALL_DECLARATOR_QUALS (declarator);
b7484fbe 9991
c11b6f21 9992 /* Pick up the exception specifications. */
43f887f9 9993 raises = CALL_DECLARATOR_EXCEPTION_SPEC (declarator);
c11b6f21 9994
f376e137
MS
9995 /* Say it's a definition only for the CALL_EXPR
9996 closest to the identifier. */
beb53fb8 9997 funcdecl_p
386b8a85
JM
9998 = inner_decl
9999 && (TREE_CODE (inner_decl) == IDENTIFIER_NODE
10000 || TREE_CODE (inner_decl) == TEMPLATE_ID_EXPR
10001 || TREE_CODE (inner_decl) == BIT_NOT_EXPR);
10002
8d08fdba
MS
10003 if (ctype == NULL_TREE
10004 && decl_context == FIELD
f376e137 10005 && funcdecl_p
8d08fdba
MS
10006 && (friendp == 0 || dname == current_class_name))
10007 ctype = current_class_type;
10008
51c184be 10009 if (ctype && return_type == return_conversion)
8d08fdba
MS
10010 TYPE_HAS_CONVERSION (ctype) = 1;
10011 if (ctype && constructor_name (ctype) == dname)
10012 {
10013 /* We are within a class's scope. If our declarator name
10014 is the same as the class name, and we are defining
10015 a function, then it is a constructor/destructor, and
10016 therefore returns a void type. */
10017
10018 if (flags == DTOR_FLAG)
10019 {
10020 /* ANSI C++ June 5 1992 WP 12.4.1. A destructor may
10021 not be declared const or volatile. A destructor
10022 may not be static. */
10023 if (staticp == 2)
8251199e 10024 error ("destructor cannot be static member function");
b7484fbe 10025 if (quals)
8d08fdba 10026 {
4f70a846
MM
10027 cp_error ("destructors may not be `%s'",
10028 IDENTIFIER_POINTER (TREE_VALUE (quals)));
10029 quals = NULL_TREE;
8d08fdba
MS
10030 }
10031 if (decl_context == FIELD)
10032 {
10033 if (! member_function_or_else (ctype, current_class_type,
10034 "destructor for alien class `%s' cannot be a member"))
10035 return void_type_node;
10036 }
10037 }
2c73f9f5 10038 else /* It's a constructor. */
8d08fdba 10039 {
db5ae43f
MS
10040 if (explicitp == 1)
10041 explicitp = 2;
8d08fdba
MS
10042 /* ANSI C++ June 5 1992 WP 12.1.2. A constructor may
10043 not be declared const or volatile. A constructor may
10044 not be virtual. A constructor may not be static. */
10045 if (staticp == 2)
8251199e 10046 error ("constructor cannot be static member function");
8d08fdba
MS
10047 if (virtualp)
10048 {
8251199e 10049 pedwarn ("constructors cannot be declared virtual");
8d08fdba
MS
10050 virtualp = 0;
10051 }
b7484fbe 10052 if (quals)
8d08fdba 10053 {
4f70a846
MM
10054 cp_error ("constructors may not be `%s'",
10055 IDENTIFIER_POINTER (TREE_VALUE (quals)));
10056 quals = NULL_TREE;
8d08fdba 10057 }
8d08fdba 10058 {
51c184be 10059 RID_BIT_TYPE tmp_bits;
fc378698 10060 bcopy ((void*)&specbits, (void*)&tmp_bits, sizeof (RID_BIT_TYPE));
51c184be
MS
10061 RIDBIT_RESET (RID_INLINE, tmp_bits);
10062 RIDBIT_RESET (RID_STATIC, tmp_bits);
10063 if (RIDBIT_ANY_SET (tmp_bits))
8251199e 10064 error ("return value type specifier for constructor ignored");
8d08fdba 10065 }
f30432d7 10066 type = build_pointer_type (ctype);
6eabb241 10067 if (decl_context == FIELD)
8d08fdba
MS
10068 {
10069 if (! member_function_or_else (ctype, current_class_type,
10070 "constructor for alien class `%s' cannot be member"))
10071 return void_type_node;
10072 TYPE_HAS_CONSTRUCTOR (ctype) = 1;
10073 if (return_type != return_ctor)
10074 return NULL_TREE;
10075 }
10076 }
10077 if (decl_context == FIELD)
10078 staticp = 0;
10079 }
b7484fbe 10080 else if (friendp)
8d08fdba 10081 {
b7484fbe 10082 if (initialized)
8251199e 10083 error ("can't initialize friend function `%s'", name);
b7484fbe
MS
10084 if (virtualp)
10085 {
10086 /* Cannot be both friend and virtual. */
8251199e 10087 error ("virtual functions cannot be friends");
b7484fbe
MS
10088 RIDBIT_RESET (RID_FRIEND, specbits);
10089 friendp = 0;
10090 }
28cbf42c 10091 if (decl_context == NORMAL)
8251199e 10092 error ("friend declaration not in class definition");
28cbf42c 10093 if (current_function_decl && funcdef_flag)
8251199e 10094 cp_error ("can't define friend function `%s' in a local class definition",
28cbf42c 10095 name);
8d08fdba
MS
10096 }
10097
8d08fdba
MS
10098 /* Construct the function type and go to the next
10099 inner layer of declarator. */
10100
f376e137 10101 declarator = TREE_OPERAND (declarator, 0);
8d08fdba 10102
f376e137
MS
10103 /* FIXME: This is where default args should be fully
10104 processed. */
8d08fdba 10105
f376e137 10106 arg_types = grokparms (inner_parms, funcdecl_p ? funcdef_flag : 0);
8d08fdba 10107
4f70a846 10108 if (declarator && flags == DTOR_FLAG)
8d08fdba 10109 {
4f70a846
MM
10110 /* A destructor declared in the body of a class will
10111 be represented as a BIT_NOT_EXPR. But, we just
10112 want the underlying IDENTIFIER. */
8d08fdba 10113 if (TREE_CODE (declarator) == BIT_NOT_EXPR)
4f70a846
MM
10114 declarator = TREE_OPERAND (declarator, 0);
10115
10116 if (strict_prototype == 0 && arg_types == NULL_TREE)
10117 arg_types = void_list_node;
10118 else if (arg_types == NULL_TREE
10119 || arg_types != void_list_node)
8d08fdba 10120 {
4f70a846
MM
10121 cp_error ("destructors may not have parameters");
10122 arg_types = void_list_node;
10123 last_function_parms = NULL_TREE;
8d08fdba
MS
10124 }
10125 }
10126
d22c8596 10127 /* ANSI says that `const int foo ();'
8d08fdba 10128 does not make the function foo const. */
d22c8596 10129 type = build_function_type (type, arg_types);
42976354
BK
10130
10131 {
10132 tree t;
10133 for (t = arg_types; t; t = TREE_CHAIN (t))
10134 if (TREE_PURPOSE (t)
10135 && TREE_CODE (TREE_PURPOSE (t)) == DEFAULT_ARG)
10136 {
10137 add_defarg_fn (type);
10138 break;
10139 }
10140 }
8d08fdba
MS
10141 }
10142 break;
10143
10144 case ADDR_EXPR:
10145 case INDIRECT_REF:
10146 /* Filter out pointers-to-references and references-to-references.
10147 We can get these if a TYPE_DECL is used. */
10148
10149 if (TREE_CODE (type) == REFERENCE_TYPE)
10150 {
8251199e 10151 error ("cannot declare %s to references",
8d08fdba
MS
10152 TREE_CODE (declarator) == ADDR_EXPR
10153 ? "references" : "pointers");
10154 declarator = TREE_OPERAND (declarator, 0);
10155 continue;
10156 }
10157
a5894242
MS
10158 if (TREE_CODE (type) == OFFSET_TYPE
10159 && (TREE_CODE (TREE_TYPE (type)) == VOID_TYPE
10160 || TREE_CODE (TREE_TYPE (type)) == REFERENCE_TYPE))
10161 {
8251199e 10162 cp_error ("cannot declare pointer to `%#T' member",
a5894242
MS
10163 TREE_TYPE (type));
10164 type = TREE_TYPE (type);
10165 }
10166
61a127b3
MM
10167 /* Merge any constancy or volatility into the target type
10168 for the pointer. */
10169
91063b51
MM
10170 /* We now know that the TYPE_QUALS don't apply to the decl,
10171 but to the target of the pointer. */
10172 type_quals = TYPE_UNQUALIFIED;
8d08fdba 10173
6eabb241 10174 if (TREE_CODE (declarator) == ADDR_EXPR)
8d08fdba 10175 {
69851283
MM
10176 if (TREE_CODE (type) == VOID_TYPE)
10177 error ("invalid type: `void &'");
8d08fdba 10178 else
69851283 10179 type = build_reference_type (type);
8d08fdba
MS
10180 }
10181 else if (TREE_CODE (type) == METHOD_TYPE)
d8f8dca1 10182 type = build_ptrmemfunc_type (build_pointer_type (type));
8d08fdba
MS
10183 else
10184 type = build_pointer_type (type);
10185
10186 /* Process a list of type modifier keywords (such as
10187 const or volatile) that were given inside the `*' or `&'. */
10188
10189 if (TREE_TYPE (declarator))
10190 {
10191 register tree typemodlist;
10192 int erred = 0;
91063b51
MM
10193
10194 constp = 0;
10195 volatilep = 0;
10196 restrictp = 0;
8d08fdba
MS
10197 for (typemodlist = TREE_TYPE (declarator); typemodlist;
10198 typemodlist = TREE_CHAIN (typemodlist))
10199 {
91063b51
MM
10200 tree qualifier = TREE_VALUE (typemodlist);
10201
10202 if (qualifier == ridpointers[(int) RID_CONST])
8d08fdba 10203 constp++;
91063b51 10204 else if (qualifier == ridpointers[(int) RID_VOLATILE])
8d08fdba 10205 volatilep++;
91063b51
MM
10206 else if (qualifier == ridpointers[(int) RID_RESTRICT])
10207 restrictp++;
8d08fdba
MS
10208 else if (!erred)
10209 {
10210 erred = 1;
91063b51 10211 error ("invalid type modifier within pointer declarator");
8d08fdba
MS
10212 }
10213 }
10214 if (constp > 1)
8251199e 10215 pedwarn ("duplicate `const'");
8d08fdba 10216 if (volatilep > 1)
8251199e 10217 pedwarn ("duplicate `volatile'");
91063b51
MM
10218 if (restrictp > 1)
10219 pedwarn ("duplicate `restrict'");
10220
10221 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
10222 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
10223 | (volatilep ? TYPE_QUAL_VOLATILE : 0));
a0a33927
MS
10224 if (TREE_CODE (declarator) == ADDR_EXPR
10225 && (constp || volatilep))
10226 {
10227 if (constp)
8251199e 10228 pedwarn ("discarding `const' applied to a reference");
a0a33927 10229 if (volatilep)
8251199e 10230 pedwarn ("discarding `volatile' applied to a reference");
91063b51 10231 type_quals &= ~(TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE);
a0a33927 10232 }
91063b51 10233 type = cp_build_qualified_type (type, type_quals);
8d08fdba
MS
10234 }
10235 declarator = TREE_OPERAND (declarator, 0);
10236 ctype = NULL_TREE;
10237 break;
10238
10239 case SCOPE_REF:
10240 {
10241 /* We have converted type names to NULL_TREE if the
10242 name was bogus, or to a _TYPE node, if not.
10243
10244 The variable CTYPE holds the type we will ultimately
10245 resolve to. The code here just needs to build
10246 up appropriate member types. */
10247 tree sname = TREE_OPERAND (declarator, 1);
386b8a85
JM
10248 tree t;
10249
8d08fdba
MS
10250 /* Destructors can have their visibilities changed as well. */
10251 if (TREE_CODE (sname) == BIT_NOT_EXPR)
10252 sname = TREE_OPERAND (sname, 0);
10253
10254 if (TREE_COMPLEXITY (declarator) == 0)
10255 /* This needs to be here, in case we are called
10256 multiple times. */ ;
9a68c51f
JM
10257 else if (TREE_COMPLEXITY (declarator) == -1)
10258 /* Namespace member. */
10259 pop_decl_namespace ();
8d08fdba 10260 else if (friendp && (TREE_COMPLEXITY (declarator) < 2))
2c73f9f5 10261 /* Don't fall out into global scope. Hides real bug? --eichin */ ;
5566b478
MS
10262 else if (! IS_AGGR_TYPE_CODE
10263 (TREE_CODE (TREE_OPERAND (declarator, 0))))
10264 ;
8d08fdba
MS
10265 else if (TREE_COMPLEXITY (declarator) == current_class_depth)
10266 {
9a3b49ac
MS
10267 /* Resolve any TYPENAME_TYPEs from the decl-specifier-seq
10268 that refer to ctype. They couldn't be resolved earlier
10269 because we hadn't pushed into the class yet.
10270 Example: resolve 'B<T>::type' in
10271 'B<typename B<T>::type> B<T>::f () { }'. */
10272 if (current_template_parms
10273 && uses_template_parms (type)
10274 && uses_template_parms (current_class_type))
10275 {
10276 tree args = current_template_args ();
4393e105 10277 type = tsubst (type, args, /*complain=*/1, NULL_TREE);
9a3b49ac
MS
10278 }
10279
a28e3c7f
MS
10280 /* This pop_nested_class corresponds to the
10281 push_nested_class used to push into class scope for
10282 parsing the argument list of a function decl, in
10283 qualified_id. */
b74a0560 10284 pop_nested_class ();
8d08fdba
MS
10285 TREE_COMPLEXITY (declarator) = current_class_depth;
10286 }
10287 else
10288 my_friendly_abort (16);
10289
10290 if (TREE_OPERAND (declarator, 0) == NULL_TREE)
10291 {
10292 /* We had a reference to a global decl, or
10293 perhaps we were given a non-aggregate typedef,
10294 in which case we cleared this out, and should just
10295 keep going as though it wasn't there. */
10296 declarator = sname;
10297 continue;
10298 }
10299 ctype = TREE_OPERAND (declarator, 0);
10300
386b8a85 10301 t = ctype;
7ddedda4 10302 while (t != NULL_TREE && CLASS_TYPE_P (t))
386b8a85 10303 {
75650646
MM
10304 if (CLASSTYPE_TEMPLATE_INFO (t) &&
10305 !CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
386b8a85
JM
10306 template_count += 1;
10307 t = TYPE_MAIN_DECL (t);
10308 if (DECL_LANG_SPECIFIC (t))
10309 t = DECL_CLASS_CONTEXT (t);
10310 else
10311 t = NULL_TREE;
10312 }
10313
8d08fdba
MS
10314 if (sname == NULL_TREE)
10315 goto done_scoping;
10316
10317 if (TREE_CODE (sname) == IDENTIFIER_NODE)
10318 {
10319 /* This is the `standard' use of the scoping operator:
10320 basetype :: member . */
10321
db5ae43f 10322 if (ctype == current_class_type)
28cbf42c
MS
10323 {
10324 /* class A {
10325 void A::f ();
10326 };
10327
10328 Is this ill-formed? */
10329
10330 if (pedantic)
8251199e 10331 cp_pedwarn ("extra qualification `%T::' on member `%s' ignored",
28cbf42c
MS
10332 ctype, name);
10333 }
db5ae43f 10334 else if (TREE_CODE (type) == FUNCTION_TYPE)
8d08fdba
MS
10335 {
10336 if (current_class_type == NULL_TREE
8d08fdba 10337 || friendp)
14ae7e7d
JM
10338 type = build_cplus_method_type (ctype, TREE_TYPE (type),
10339 TYPE_ARG_TYPES (type));
8d08fdba
MS
10340 else
10341 {
8251199e 10342 cp_error ("cannot declare member function `%T::%s' within `%T'",
a3203465 10343 ctype, name, current_class_type);
8d08fdba
MS
10344 return void_type_node;
10345 }
10346 }
5566b478
MS
10347 else if (RIDBIT_SETP (RID_TYPEDEF, specbits)
10348 || TYPE_SIZE (complete_type (ctype)) != NULL_TREE)
8d08fdba 10349 {
2c73f9f5 10350 /* Have to move this code elsewhere in this function.
db5ae43f
MS
10351 this code is used for i.e., typedef int A::M; M *pm;
10352
10353 It is? How? jason 10/2/94 */
8d08fdba 10354
8d08fdba
MS
10355 if (current_class_type)
10356 {
8251199e 10357 cp_error ("cannot declare member `%T::%s' within `%T'",
db5ae43f
MS
10358 ctype, name, current_class_type);
10359 return void_type_node;
8d08fdba
MS
10360 }
10361 type = build_offset_type (ctype, type);
10362 }
10363 else if (uses_template_parms (ctype))
10364 {
8d08fdba 10365 if (TREE_CODE (type) == FUNCTION_TYPE)
beb53fb8 10366 type
14ae7e7d 10367 = build_cplus_method_type (ctype, TREE_TYPE (type),
beb53fb8 10368 TYPE_ARG_TYPES (type));
8d08fdba
MS
10369 }
10370 else
10371 {
8251199e 10372 cp_error ("structure `%T' not yet defined", ctype);
8d08fdba
MS
10373 return error_mark_node;
10374 }
10375
10376 declarator = sname;
10377 }
8d08fdba
MS
10378 else if (TREE_CODE (sname) == SCOPE_REF)
10379 my_friendly_abort (17);
10380 else
10381 {
10382 done_scoping:
10383 declarator = TREE_OPERAND (declarator, 1);
10384 if (declarator && TREE_CODE (declarator) == CALL_EXPR)
10385 /* In this case, we will deal with it later. */
10386 ;
10387 else
10388 {
10389 if (TREE_CODE (type) == FUNCTION_TYPE)
14ae7e7d
JM
10390 type = build_cplus_method_type (ctype, TREE_TYPE (type),
10391 TYPE_ARG_TYPES (type));
8d08fdba
MS
10392 else
10393 type = build_offset_type (ctype, type);
10394 }
10395 }
10396 }
10397 break;
10398
10399 case BIT_NOT_EXPR:
10400 declarator = TREE_OPERAND (declarator, 0);
10401 break;
10402
8d08fdba
MS
10403 case RECORD_TYPE:
10404 case UNION_TYPE:
10405 case ENUMERAL_TYPE:
10406 declarator = NULL_TREE;
10407 break;
10408
10409 case ERROR_MARK:
10410 declarator = NULL_TREE;
10411 break;
10412
10413 default:
10414 my_friendly_abort (158);
10415 }
10416 }
10417
1eab9b56
JM
10418 /* See the comment for the TREE_LIST case, above. */
10419 if (inner_attrs)
10420 {
10421 if (! ignore_attrs)
10422 decl_attributes (type, inner_attrs, NULL_TREE);
10423 else if (attrlist)
10424 TREE_VALUE (attrlist) = chainon (inner_attrs, TREE_VALUE (attrlist));
10425 else
10426 attrlist = build_decl_list (NULL_TREE, inner_attrs);
10427 }
10428
419c6212
JM
10429 /* Now TYPE has the actual type. */
10430
db5ae43f
MS
10431 if (explicitp == 1)
10432 {
8251199e 10433 error ("only constructors can be declared `explicit'");
db5ae43f
MS
10434 explicitp = 0;
10435 }
10436
f30432d7
MS
10437 if (RIDBIT_SETP (RID_MUTABLE, specbits))
10438 {
91063b51 10439 if (type_quals & TYPE_QUAL_CONST)
f30432d7 10440 {
8251199e 10441 error ("const `%s' cannot be declared `mutable'", name);
f30432d7
MS
10442 RIDBIT_RESET (RID_MUTABLE, specbits);
10443 }
10444 else if (staticp)
10445 {
8251199e 10446 error ("static `%s' cannot be declared `mutable'", name);
f30432d7
MS
10447 RIDBIT_RESET (RID_MUTABLE, specbits);
10448 }
10449 }
10450
efe38fab
JM
10451 if (declarator == NULL_TREE
10452 || TREE_CODE (declarator) == IDENTIFIER_NODE
10453 || (TREE_CODE (declarator) == TEMPLATE_ID_EXPR
10454 && (TREE_CODE (type) == FUNCTION_TYPE
10455 || TREE_CODE (type) == METHOD_TYPE)))
10456 /* OK */;
10457 else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
10458 {
10459 cp_error ("template-id `%D' used as a declarator", declarator);
10460 declarator = dname;
10461 }
10462 else
419c6212 10463 /* Unexpected declarator format. */
efe38fab
JM
10464 my_friendly_abort (990210);
10465
419c6212
JM
10466 /* If this is declaring a typedef name, return a TYPE_DECL. */
10467
fc378698 10468 if (RIDBIT_SETP (RID_TYPEDEF, specbits) && decl_context != TYPENAME)
8d08fdba
MS
10469 {
10470 tree decl;
10471
10472 /* Note that the grammar rejects storage classes
10473 in typenames, fields or parameters. */
eff71ab0
PB
10474 if (current_lang_name == lang_name_java)
10475 TYPE_FOR_JAVA (type) = 1;
8d08fdba 10476
d2e5ee5c
MS
10477 if (decl_context == FIELD)
10478 {
094fe153 10479 if (declarator == constructor_name (current_class_type))
8251199e 10480 cp_pedwarn ("ANSI C++ forbids nested type `%D' with same name as enclosing class",
d2e5ee5c
MS
10481 declarator);
10482 decl = build_lang_decl (TYPE_DECL, declarator, type);
d2e5ee5c
MS
10483 }
10484 else
7fb4a8f7
JM
10485 {
10486 /* Make sure this typedef lives as long as its type,
10487 since it might be used as a template parameter. */
1aed5355
MM
10488 if (type != error_mark_node)
10489 push_obstacks (TYPE_OBSTACK (type), TYPE_OBSTACK (type));
9188c363 10490 if (processing_template_decl)
b0d06515 10491 decl = build_lang_decl (TYPE_DECL, declarator, type);
9188c363
MM
10492 else
10493 decl = build_decl (TYPE_DECL, declarator, type);
1aed5355
MM
10494 if (type != error_mark_node)
10495 pop_obstacks ();
7fb4a8f7 10496 }
d2e5ee5c 10497
9188c363
MM
10498 /* If the user declares "typedef struct {...} foo" then the
10499 struct will have an anonymous name. Fill that name in now.
10500 Nothing can refer to it, so nothing needs know about the name
10501 change. */
8d08fdba
MS
10502 if (type != error_mark_node
10503 && TYPE_NAME (type)
10504 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
b537d120
MM
10505 && ANON_AGGRNAME_P (TYPE_IDENTIFIER (type))
10506 && CP_TYPE_QUALS (type) == TYPE_UNQUALIFIED)
8d08fdba 10507 {
dcd08efc
JM
10508 tree oldname = TYPE_NAME (type);
10509 tree t;
10510
2c73f9f5 10511 /* Replace the anonymous name with the real name everywhere. */
8d08fdba 10512 lookup_tag_reverse (type, declarator);
dcd08efc
JM
10513 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
10514 if (TYPE_NAME (t) == oldname)
10515 TYPE_NAME (t) = decl;
8d08fdba
MS
10516
10517 if (TYPE_LANG_SPECIFIC (type))
10518 TYPE_WAS_ANONYMOUS (type) = 1;
10519
33964bf4
MM
10520 /* If this is a typedef within a template class, the nested
10521 type is a (non-primary) template. The name for the
10522 template needs updating as well. */
10523 if (TYPE_LANG_SPECIFIC (type) && CLASSTYPE_TEMPLATE_INFO (type))
10524 DECL_NAME (CLASSTYPE_TI_TEMPLATE (type))
10525 = TYPE_IDENTIFIER (type);
10526
2c73f9f5
ML
10527 /* XXX Temporarily set the scope.
10528 When returning, start_decl expects it as NULL_TREE,
10529 and will then then set it using pushdecl. */
10530 my_friendly_assert (DECL_CONTEXT (decl) == NULL_TREE, 980404);
10531 if (current_class_type)
10532 DECL_CONTEXT (decl) = current_class_type;
10533 else
cb0dbb9a 10534 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
2c73f9f5 10535
d2e5ee5c
MS
10536 DECL_ASSEMBLER_NAME (decl) = DECL_NAME (decl);
10537 DECL_ASSEMBLER_NAME (decl)
10538 = get_identifier (build_overload_name (type, 1, 1));
2c73f9f5 10539 DECL_CONTEXT (decl) = NULL_TREE;
50a6dbd7
JM
10540
10541 /* FIXME remangle member functions; member functions of a
10542 type with external linkage have external linkage. */
fc378698 10543 }
fc378698 10544
8d08fdba
MS
10545 if (TREE_CODE (type) == OFFSET_TYPE || TREE_CODE (type) == METHOD_TYPE)
10546 {
8251199e 10547 cp_error_at ("typedef name may not be class-qualified", decl);
a3203465 10548 return NULL_TREE;
8d08fdba
MS
10549 }
10550 else if (quals)
10551 {
10552 if (ctype == NULL_TREE)
10553 {
10554 if (TREE_CODE (type) != METHOD_TYPE)
8251199e 10555 cp_error_at ("invalid type qualifier for non-method type", decl);
8d08fdba
MS
10556 else
10557 ctype = TYPE_METHOD_BASETYPE (type);
10558 }
10559 if (ctype != NULL_TREE)
10560 grok_method_quals (ctype, decl, quals);
10561 }
10562
10563 if (RIDBIT_SETP (RID_SIGNED, specbits)
10564 || (typedef_decl && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl)))
10565 C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
10566
10567 if (RIDBIT_SETP (RID_MUTABLE, specbits))
61a127b3 10568 error ("non-object member `%s' cannot be declared mutable", name);
8d08fdba 10569
c91a56d2
MS
10570 bad_specifiers (decl, "type", virtualp, quals != NULL_TREE,
10571 inlinep, friendp, raises != NULL_TREE);
10572
10573 if (initialized)
8251199e 10574 error ("typedef declaration includes an initializer");
c91a56d2 10575
8d08fdba
MS
10576 return decl;
10577 }
10578
10579 /* Detect the case of an array type of unspecified size
10580 which came, as such, direct from a typedef name.
10581 We must copy the type, so that each identifier gets
10582 a distinct type, so that each identifier's size can be
10583 controlled separately by its own initializer. */
10584
10585 if (type == typedef_type && TREE_CODE (type) == ARRAY_TYPE
10586 && TYPE_DOMAIN (type) == NULL_TREE)
10587 {
10588 type = build_cplus_array_type (TREE_TYPE (type), TYPE_DOMAIN (type));
10589 }
10590
10591 /* If this is a type name (such as, in a cast or sizeof),
10592 compute the type and return it now. */
10593
10594 if (decl_context == TYPENAME)
10595 {
10596 /* Note that the grammar rejects storage classes
10597 in typenames, fields or parameters. */
91063b51 10598 if (type_quals != TYPE_UNQUALIFIED)
6eabb241 10599 type_quals = TYPE_UNQUALIFIED;
8d08fdba
MS
10600
10601 /* Special case: "friend class foo" looks like a TYPENAME context. */
10602 if (friendp)
10603 {
91063b51 10604 if (type_quals != TYPE_UNQUALIFIED)
b7484fbe 10605 {
91063b51
MM
10606 cp_error ("type qualifiers specified for friend class declaration");
10607 type_quals = TYPE_UNQUALIFIED;
b7484fbe
MS
10608 }
10609 if (inlinep)
10610 {
8251199e 10611 cp_error ("`inline' specified for friend class declaration");
b7484fbe
MS
10612 inlinep = 0;
10613 }
10614
10615 /* Only try to do this stuff if we didn't already give up. */
10616 if (type != integer_type_node)
10617 {
10618 /* A friendly class? */
10619 if (current_class_type)
10620 make_friend_class (current_class_type, TYPE_MAIN_VARIANT (type));
10621 else
8251199e 10622 error ("trying to make class `%s' a friend of global scope",
b7484fbe
MS
10623 TYPE_NAME_STRING (type));
10624 type = void_type_node;
10625 }
8d08fdba
MS
10626 }
10627 else if (quals)
10628 {
8d08fdba 10629 tree dummy = build_decl (TYPE_DECL, declarator, type);
8d08fdba
MS
10630 if (ctype == NULL_TREE)
10631 {
10632 my_friendly_assert (TREE_CODE (type) == METHOD_TYPE, 159);
10633 ctype = TYPE_METHOD_BASETYPE (type);
10634 }
10635 grok_method_quals (ctype, dummy, quals);
10636 type = TREE_TYPE (dummy);
10637 }
10638
10639 return type;
10640 }
10641 else if (declarator == NULL_TREE && decl_context != PARM
db5ae43f 10642 && decl_context != CATCHPARM
8d08fdba
MS
10643 && TREE_CODE (type) != UNION_TYPE
10644 && ! bitfield)
10645 {
8251199e 10646 cp_error ("abstract declarator `%T' used as declaration", type);
8d08fdba
MS
10647 declarator = make_anon_name ();
10648 }
10649
10650 /* `void' at top level (not within pointer)
10651 is allowed only in typedefs or type names.
10652 We don't complain about parms either, but that is because
10653 a better error message can be made later. */
10654
a1774733 10655 if (TREE_CODE (type) == VOID_TYPE && decl_context != PARM)
8d08fdba 10656 {
b7484fbe 10657 if (! declarator)
8251199e 10658 error ("unnamed variable or field declared void");
b7484fbe 10659 else if (TREE_CODE (declarator) == IDENTIFIER_NODE)
8d08fdba
MS
10660 {
10661 if (IDENTIFIER_OPNAME_P (declarator))
8d08fdba 10662 my_friendly_abort (356);
8d08fdba 10663 else
8251199e 10664 error ("variable or field `%s' declared void", name);
8d08fdba
MS
10665 }
10666 else
8251199e 10667 error ("variable or field declared void");
8d08fdba
MS
10668 type = integer_type_node;
10669 }
10670
10671 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
10672 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
10673
14ae7e7d
JM
10674 if (decl_context == PARM || decl_context == CATCHPARM)
10675 {
10676 if (ctype || in_namespace)
10677 error ("cannot use `::' in parameter declaration");
10678
10679 /* A parameter declared as an array of T is really a pointer to T.
10680 One declared as a function is really a pointer to a function.
10681 One declared as a member is really a pointer to member. */
10682
10683 if (TREE_CODE (type) == ARRAY_TYPE)
10684 {
10685 /* Transfer const-ness of array into that of type pointed to. */
10686 type = build_pointer_type (TREE_TYPE (type));
91063b51 10687 type_quals = TYPE_UNQUALIFIED;
14ae7e7d
JM
10688 }
10689 else if (TREE_CODE (type) == FUNCTION_TYPE)
10690 type = build_pointer_type (type);
10691 else if (TREE_CODE (type) == OFFSET_TYPE)
10692 type = build_pointer_type (type);
10693 else if (TREE_CODE (type) == VOID_TYPE && declarator)
10694 {
10695 error ("declaration of `%s' as void", name);
10696 return NULL_TREE;
10697 }
10698 }
10699
8d08fdba
MS
10700 {
10701 register tree decl;
10702
10703 if (decl_context == PARM)
10704 {
ff350acd 10705 decl = build_decl (PARM_DECL, declarator, type);
8d08fdba
MS
10706
10707 bad_specifiers (decl, "parameter", virtualp, quals != NULL_TREE,
10708 inlinep, friendp, raises != NULL_TREE);
8d08fdba
MS
10709
10710 /* Compute the type actually passed in the parmlist,
10711 for the case where there is no prototype.
10712 (For example, shorts and chars are passed as ints.)
10713 When there is a prototype, this is overridden later. */
10714
39211cd5 10715 DECL_ARG_TYPE (decl) = type_promotes_to (type);
8d08fdba
MS
10716 }
10717 else if (decl_context == FIELD)
10718 {
10719 if (type == error_mark_node)
10720 {
10721 /* Happens when declaring arrays of sizes which
10722 are error_mark_node, for example. */
10723 decl = NULL_TREE;
10724 }
997a088c 10725 else if (in_namespace && !friendp)
05008fb9
MM
10726 {
10727 /* Something like struct S { int N::j; }; */
8251199e 10728 cp_error ("invalid use of `::'");
05008fb9
MM
10729 decl = NULL_TREE;
10730 }
8d08fdba
MS
10731 else if (TREE_CODE (type) == FUNCTION_TYPE)
10732 {
10733 int publicp = 0;
e1467ff2 10734 tree function_context;
8d08fdba 10735
72b7eeff
MS
10736 /* We catch the others as conflicts with the builtin
10737 typedefs. */
10738 if (friendp && declarator == ridpointers[(int) RID_SIGNED])
10739 {
8251199e 10740 cp_error ("function `%D' cannot be declared friend",
72b7eeff
MS
10741 declarator);
10742 friendp = 0;
10743 }
10744
8d08fdba
MS
10745 if (friendp == 0)
10746 {
10747 if (ctype == NULL_TREE)
10748 ctype = current_class_type;
10749
10750 if (ctype == NULL_TREE)
10751 {
8251199e 10752 cp_error ("can't make `%D' into a method -- not in a class",
8d08fdba
MS
10753 declarator);
10754 return void_type_node;
10755 }
10756
10757 /* ``A union may [ ... ] not [ have ] virtual functions.''
10758 ARM 9.5 */
10759 if (virtualp && TREE_CODE (ctype) == UNION_TYPE)
10760 {
8251199e 10761 cp_error ("function `%D' declared virtual inside a union",
8d08fdba
MS
10762 declarator);
10763 return void_type_node;
10764 }
10765
10766 if (declarator == ansi_opname[(int) NEW_EXPR]
a28e3c7f
MS
10767 || declarator == ansi_opname[(int) VEC_NEW_EXPR]
10768 || declarator == ansi_opname[(int) DELETE_EXPR]
10769 || declarator == ansi_opname[(int) VEC_DELETE_EXPR])
8d08fdba
MS
10770 {
10771 if (virtualp)
10772 {
8251199e 10773 cp_error ("`%D' cannot be declared virtual, since it is always static",
8d08fdba
MS
10774 declarator);
10775 virtualp = 0;
10776 }
10777 }
10778 else if (staticp < 2)
14ae7e7d
JM
10779 type = build_cplus_method_type (ctype, TREE_TYPE (type),
10780 TYPE_ARG_TYPES (type));
8d08fdba
MS
10781 }
10782
10783 /* Tell grokfndecl if it needs to set TREE_PUBLIC on the node. */
e1467ff2
MM
10784 function_context = (ctype != NULL_TREE) ?
10785 hack_decl_function_context (TYPE_MAIN_DECL (ctype)) : NULL_TREE;
10786 publicp = (! friendp || ! staticp)
10787 && function_context == NULL_TREE;
386b8a85
JM
10788 decl = grokfndecl (ctype, type,
10789 TREE_CODE (declarator) != TEMPLATE_ID_EXPR
10790 ? declarator : dname,
10791 declarator,
7a8f9fa9 10792 virtualp, flags, quals, raises,
386b8a85 10793 friendp ? -1 : 0, friendp, publicp, inlinep,
2c73f9f5 10794 funcdef_flag, template_count, in_namespace);
20496fa2 10795 if (decl == NULL_TREE)
3ddfb0e6 10796 return decl;
6125f3be
DE
10797#if 0
10798 /* This clobbers the attrs stored in `decl' from `attrlist'. */
10799 /* The decl and setting of decl_machine_attr is also turned off. */
71851aaa 10800 decl = build_decl_attribute_variant (decl, decl_machine_attr);
6125f3be 10801#endif
f0e01782 10802
cc804e51
MM
10803 /* [class.conv.ctor]
10804
10805 A constructor declared without the function-specifier
10806 explicit that can be called with a single parameter
10807 specifies a conversion from the type of its first
10808 parameter to the type of its class. Such a constructor
10809 is called a converting constructor. */
db5ae43f
MS
10810 if (explicitp == 2)
10811 DECL_NONCONVERTING_P (decl) = 1;
cc804e51
MM
10812 else if (DECL_CONSTRUCTOR_P (decl))
10813 {
10814 /* The constructor can be called with exactly one
10815 parameter if there is at least one parameter, and
10816 any subsequent parameters have default arguments.
10817 We don't look at the first parameter, which is
10818 really just the `this' parameter for the new
10819 object. */
10820 tree arg_types =
10821 TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)));
10822
10823 /* Skip the `in_chrg' argument too, if present. */
10824 if (TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (decl)))
10825 arg_types = TREE_CHAIN (arg_types);
10826
10827 if (arg_types == void_list_node
10828 || (arg_types
10829 && TREE_CHAIN (arg_types)
10830 && TREE_CHAIN (arg_types) != void_list_node
10831 && !TREE_PURPOSE (TREE_CHAIN (arg_types))))
10832 DECL_NONCONVERTING_P (decl) = 1;
10833 }
8d08fdba
MS
10834 }
10835 else if (TREE_CODE (type) == METHOD_TYPE)
10836 {
faae18ab
MS
10837 /* We only get here for friend declarations of
10838 members of other classes. */
8d08fdba
MS
10839 /* All method decls are public, so tell grokfndecl to set
10840 TREE_PUBLIC, also. */
386b8a85 10841 decl = grokfndecl (ctype, type, declarator, declarator,
7a8f9fa9 10842 virtualp, flags, quals, raises,
386b8a85 10843 friendp ? -1 : 0, friendp, 1, 0, funcdef_flag,
2c73f9f5 10844 template_count, in_namespace);
f0e01782
MS
10845 if (decl == NULL_TREE)
10846 return NULL_TREE;
8d08fdba 10847 }
5566b478
MS
10848 else if (!staticp && ! processing_template_decl
10849 && TYPE_SIZE (complete_type (type)) == NULL_TREE
8d08fdba
MS
10850 && (TREE_CODE (type) != ARRAY_TYPE || initialized == 0))
10851 {
b7484fbe 10852 if (declarator)
8251199e 10853 cp_error ("field `%D' has incomplete type", declarator);
b7484fbe 10854 else
8251199e 10855 cp_error ("name `%T' has incomplete type", type);
8d08fdba
MS
10856
10857 /* If we're instantiating a template, tell them which
10858 instantiation made the field's type be incomplete. */
10859 if (current_class_type
10860 && TYPE_NAME (current_class_type)
d2e5ee5c 10861 && IDENTIFIER_TEMPLATE (TYPE_IDENTIFIER (current_class_type))
8d08fdba
MS
10862 && declspecs && TREE_VALUE (declspecs)
10863 && TREE_TYPE (TREE_VALUE (declspecs)) == type)
8251199e 10864 cp_error (" in instantiation of template `%T'",
db5ae43f
MS
10865 current_class_type);
10866
8d08fdba
MS
10867 type = error_mark_node;
10868 decl = NULL_TREE;
10869 }
10870 else
10871 {
10872 if (friendp)
10873 {
8251199e 10874 error ("`%s' is neither function nor method; cannot be declared friend",
8d08fdba
MS
10875 IDENTIFIER_POINTER (declarator));
10876 friendp = 0;
10877 }
10878 decl = NULL_TREE;
10879 }
10880
10881 if (friendp)
10882 {
10883 /* Friends are treated specially. */
10884 if (ctype == current_class_type)
8251199e 10885 warning ("member functions are implicitly friends of their class");
8d08fdba
MS
10886 else
10887 {
10888 tree t = NULL_TREE;
10889 if (decl && DECL_NAME (decl))
36a117a5
MM
10890 {
10891 if (template_class_depth (current_class_type) == 0)
6c30752f
MM
10892 {
10893 decl
10894 = check_explicit_specialization
10895 (declarator, decl,
10896 template_count, 2 * (funcdef_flag != 0) + 4);
10897 if (decl == error_mark_node)
10898 return error_mark_node;
10899 }
10900
36a117a5 10901 t = do_friend (ctype, declarator, decl,
7a8f9fa9 10902 last_function_parms, attrlist, flags, quals,
36a117a5
MM
10903 funcdef_flag);
10904 }
8d08fdba
MS
10905 if (t && funcdef_flag)
10906 return t;
10907
10908 return void_type_node;
10909 }
10910 }
10911
10912 /* Structure field. It may not be a function, except for C++ */
10913
10914 if (decl == NULL_TREE)
10915 {
8d08fdba
MS
10916 if (initialized)
10917 {
3ac3d9ea
MM
10918 if (!staticp)
10919 {
10920 /* An attempt is being made to initialize a non-static
10921 member. But, from [class.mem]:
10922
10923 4 A member-declarator can contain a
10924 constant-initializer only if it declares a static
10925 member (_class.static_) of integral or enumeration
10926 type, see _class.static.data_.
10927
10928 This used to be relatively common practice, but
10929 the rest of the compiler does not correctly
10930 handle the initialization unless the member is
10931 static so we make it static below. */
950ad3c3 10932 cp_pedwarn ("ANSI C++ forbids initialization of member `%D'",
3ac3d9ea 10933 declarator);
8251199e 10934 cp_pedwarn ("making `%D' static", declarator);
3ac3d9ea
MM
10935 staticp = 1;
10936 }
10937
6ba89f8e
MM
10938 if (uses_template_parms (type))
10939 /* We'll check at instantiation time. */
10940 ;
10941 else if (check_static_variable_definition (declarator,
10942 type))
10943 /* If we just return the declaration, crashes
10944 will sometimes occur. We therefore return
72a93143
JM
10945 void_type_node, as if this was a friend
10946 declaration, to cause callers to completely
10947 ignore this declaration. */
6ba89f8e 10948 return void_type_node;
8d08fdba
MS
10949 }
10950
8ebeee52 10951 /* 9.2p13 [class.mem] */
4d7614e9
JM
10952 if (declarator == constructor_name (current_class_type)
10953 /* Divergence from the standard: In extern "C", we
10954 allow non-static data members here, because C does
10955 and /usr/include/netinet/in.h uses that. */
1f901793 10956 && (staticp || ! in_system_header))
8ebeee52
JM
10957 cp_pedwarn ("ANSI C++ forbids data member `%D' with same name as enclosing class",
10958 declarator);
10959
3ac3d9ea 10960 if (staticp)
8d08fdba 10961 {
f18a14bc
MM
10962 /* C++ allows static class members. All other work
10963 for this is done by grokfield. */
4ce3d537 10964 decl = build_lang_decl (VAR_DECL, declarator, type);
5b605f68
MS
10965 TREE_STATIC (decl) = 1;
10966 /* In class context, 'static' means public access. */
3ac3d9ea 10967 TREE_PUBLIC (decl) = DECL_EXTERNAL (decl) = 1;
8d08fdba
MS
10968 }
10969 else
10970 {
4ce3d537 10971 decl = build_lang_decl (FIELD_DECL, declarator, type);
8d08fdba
MS
10972 if (RIDBIT_SETP (RID_MUTABLE, specbits))
10973 {
10974 DECL_MUTABLE_P (decl) = 1;
10975 RIDBIT_RESET (RID_MUTABLE, specbits);
10976 }
10977 }
10978
10979 bad_specifiers (decl, "field", virtualp, quals != NULL_TREE,
10980 inlinep, friendp, raises != NULL_TREE);
10981 }
10982 }
10983 else if (TREE_CODE (type) == FUNCTION_TYPE || TREE_CODE (type) == METHOD_TYPE)
10984 {
386b8a85 10985 tree original_name;
8d08fdba
MS
10986 int publicp = 0;
10987
10988 if (! declarator)
10989 return NULL_TREE;
10990
386b8a85
JM
10991 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
10992 original_name = dname;
10993 else
10994 original_name = declarator;
10995
8926095f 10996 if (RIDBIT_SETP (RID_AUTO, specbits))
8251199e 10997 error ("storage class `auto' invalid for function `%s'", name);
8926095f 10998 else if (RIDBIT_SETP (RID_REGISTER, specbits))
8251199e 10999 error ("storage class `register' invalid for function `%s'", name);
8d08fdba
MS
11000
11001 /* Function declaration not at top level.
11002 Storage classes other than `extern' are not allowed
11003 and `extern' makes no difference. */
a9aedbc2 11004 if (! toplevel_bindings_p ()
8926095f
MS
11005 && (RIDBIT_SETP (RID_STATIC, specbits)
11006 || RIDBIT_SETP (RID_INLINE, specbits))
8d08fdba 11007 && pedantic)
8926095f
MS
11008 {
11009 if (RIDBIT_SETP (RID_STATIC, specbits))
8251199e 11010 pedwarn ("storage class `static' invalid for function `%s' declared out of global scope", name);
8926095f 11011 else
8251199e 11012 pedwarn ("storage class `inline' invalid for function `%s' declared out of global scope", name);
8926095f
MS
11013 }
11014
8d08fdba
MS
11015 if (ctype == NULL_TREE)
11016 {
11017 if (virtualp)
11018 {
8251199e 11019 error ("virtual non-class function `%s'", name);
8d08fdba
MS
11020 virtualp = 0;
11021 }
8d08fdba
MS
11022 }
11023 else if (TREE_CODE (type) == FUNCTION_TYPE && staticp < 2)
14ae7e7d
JM
11024 type = build_cplus_method_type (ctype, TREE_TYPE (type),
11025 TYPE_ARG_TYPES (type));
8d08fdba 11026
eb66be0e 11027 /* Record presence of `static'. */
faae18ab 11028 publicp = (ctype != NULL_TREE
a9aedbc2 11029 || RIDBIT_SETP (RID_EXTERN, specbits)
eb66be0e 11030 || !RIDBIT_SETP (RID_STATIC, specbits));
8d08fdba 11031
386b8a85 11032 decl = grokfndecl (ctype, type, original_name, declarator,
7a8f9fa9 11033 virtualp, flags, quals, raises,
75650646 11034 1, friendp,
386b8a85 11035 publicp, inlinep, funcdef_flag,
2c73f9f5 11036 template_count, in_namespace);
f0e01782
MS
11037 if (decl == NULL_TREE)
11038 return NULL_TREE;
8d08fdba 11039
8d08fdba
MS
11040 if (staticp == 1)
11041 {
11042 int illegal_static = 0;
11043
11044 /* Don't allow a static member function in a class, and forbid
11045 declaring main to be static. */
11046 if (TREE_CODE (type) == METHOD_TYPE)
11047 {
8251199e 11048 cp_pedwarn ("cannot declare member function `%D' to have static linkage", decl);
8d08fdba
MS
11049 illegal_static = 1;
11050 }
8d08fdba
MS
11051 else if (current_function_decl)
11052 {
11053 /* FIXME need arm citation */
8251199e 11054 error ("cannot declare static function inside another function");
8d08fdba
MS
11055 illegal_static = 1;
11056 }
11057
11058 if (illegal_static)
11059 {
11060 staticp = 0;
11061 RIDBIT_RESET (RID_STATIC, specbits);
11062 }
11063 }
8d08fdba
MS
11064 }
11065 else
11066 {
11067 /* It's a variable. */
11068
11069 /* An uninitialized decl with `extern' is a reference. */
2c73f9f5 11070 decl = grokvardecl (type, declarator, &specbits,
950ad3c3
MM
11071 initialized,
11072 (type_quals & TYPE_QUAL_CONST) != 0,
11073 in_namespace);
8d08fdba
MS
11074 bad_specifiers (decl, "variable", virtualp, quals != NULL_TREE,
11075 inlinep, friendp, raises != NULL_TREE);
11076
11077 if (ctype)
11078 {
f0e01782 11079 DECL_CONTEXT (decl) = ctype;
8d08fdba
MS
11080 if (staticp == 1)
11081 {
8251199e 11082 cp_pedwarn ("static member `%D' re-declared as static", decl);
8d08fdba
MS
11083 staticp = 0;
11084 RIDBIT_RESET (RID_STATIC, specbits);
11085 }
b7484fbe
MS
11086 if (RIDBIT_SETP (RID_REGISTER, specbits) && TREE_STATIC (decl))
11087 {
8251199e 11088 cp_error ("static member `%D' declared `register'", decl);
b7484fbe
MS
11089 RIDBIT_RESET (RID_REGISTER, specbits);
11090 }
f30432d7 11091 if (RIDBIT_SETP (RID_EXTERN, specbits) && pedantic)
8d08fdba 11092 {
8251199e 11093 cp_pedwarn ("cannot explicitly declare member `%#D' to have extern linkage",
f30432d7 11094 decl);
8d08fdba
MS
11095 RIDBIT_RESET (RID_EXTERN, specbits);
11096 }
11097 }
11098 }
11099
11100 if (RIDBIT_SETP (RID_MUTABLE, specbits))
11101 {
8251199e 11102 error ("`%s' cannot be declared mutable", name);
8d08fdba
MS
11103 }
11104
11105 /* Record `register' declaration for warnings on &
11106 and in case doing stupid register allocation. */
11107
11108 if (RIDBIT_SETP (RID_REGISTER, specbits))
11109 DECL_REGISTER (decl) = 1;
11110
8926095f
MS
11111 if (RIDBIT_SETP (RID_EXTERN, specbits))
11112 DECL_THIS_EXTERN (decl) = 1;
11113
faae18ab
MS
11114 if (RIDBIT_SETP (RID_STATIC, specbits))
11115 DECL_THIS_STATIC (decl) = 1;
11116
adecb3f4
MM
11117 /* Record constancy and volatility. There's no need to do this
11118 when processing a template; we'll do this for the instantiated
11119 declaration based on the type of DECL. */
11120 if (!processing_template_decl)
11121 c_apply_type_quals_to_decl (type_quals, decl);
8d08fdba
MS
11122
11123 return decl;
11124 }
11125}
11126\f
11127/* Tell if a parmlist/exprlist looks like an exprlist or a parmlist.
11128 An empty exprlist is a parmlist. An exprlist which
11129 contains only identifiers at the global level
11130 is a parmlist. Otherwise, it is an exprlist. */
e92cc029 11131
8d08fdba
MS
11132int
11133parmlist_is_exprlist (exprs)
11134 tree exprs;
11135{
11136 if (exprs == NULL_TREE || TREE_PARMLIST (exprs))
11137 return 0;
11138
a9aedbc2 11139 if (toplevel_bindings_p ())
8d08fdba
MS
11140 {
11141 /* At the global level, if these are all identifiers,
11142 then it is a parmlist. */
11143 while (exprs)
11144 {
11145 if (TREE_CODE (TREE_VALUE (exprs)) != IDENTIFIER_NODE)
11146 return 1;
11147 exprs = TREE_CHAIN (exprs);
11148 }
11149 return 0;
11150 }
11151 return 1;
11152}
11153
f181d4ae
MM
11154/* Subroutine of start_function. Ensure that each of the parameter
11155 types (as listed in PARMS) is complete, as is required for a
11156 function definition. */
e92cc029 11157
8d08fdba
MS
11158static void
11159require_complete_types_for_parms (parms)
11160 tree parms;
11161{
11162 while (parms)
11163 {
11164 tree type = TREE_TYPE (parms);
5566b478 11165 if (TYPE_SIZE (complete_type (type)) == NULL_TREE)
8d08fdba
MS
11166 {
11167 if (DECL_NAME (parms))
8251199e 11168 error ("parameter `%s' has incomplete type",
8d08fdba
MS
11169 IDENTIFIER_POINTER (DECL_NAME (parms)));
11170 else
8251199e 11171 error ("parameter has incomplete type");
8d08fdba
MS
11172 TREE_TYPE (parms) = error_mark_node;
11173 }
42f989ff
JM
11174 else
11175 layout_decl (parms, 0);
f181d4ae 11176
8d08fdba
MS
11177 parms = TREE_CHAIN (parms);
11178 }
11179}
11180
297e73d8
MM
11181/* Returns DECL if DECL is a local variable (or parameter). Returns
11182 NULL_TREE otherwise. */
11183
11184static tree
11185local_variable_p (t)
11186 tree t;
11187{
11188 if ((TREE_CODE (t) == VAR_DECL
11189 /* A VAR_DECL with a context that is a _TYPE is a static data
11190 member. */
11191 && !TYPE_P (CP_DECL_CONTEXT (t))
11192 /* Any other non-local variable must be at namespace scope. */
11193 && TREE_CODE (CP_DECL_CONTEXT (t)) != NAMESPACE_DECL)
11194 || (TREE_CODE (t) == PARM_DECL))
11195 return t;
11196
11197 return NULL_TREE;
11198}
11199
11200/* Check that ARG, which is a default-argument expression for a
11201 parameter DECL, is legal. Returns ARG, or ERROR_MARK_NODE, if
11202 something goes wrong. DECL may also be a _TYPE node, rather than a
11203 DECL, if there is no DECL available. */
11204
11205tree
11206check_default_argument (decl, arg)
11207 tree decl;
11208 tree arg;
11209{
11210 tree var;
11211 tree decl_type;
11212
11213 if (TREE_CODE (arg) == DEFAULT_ARG)
11214 /* We get a DEFAULT_ARG when looking at an in-class declaration
11215 with a default argument. Ignore the argument for now; we'll
11216 deal with it after the class is complete. */
11217 return arg;
11218
11219 if (processing_template_decl || uses_template_parms (arg))
11220 /* We don't do anything checking until instantiation-time. Note
11221 that there may be uninstantiated arguments even for an
11222 instantiated function, since default arguments are not
11223 instantiated until they are needed. */
11224 return arg;
11225
11226 if (TYPE_P (decl))
11227 {
11228 decl_type = decl;
11229 decl = NULL_TREE;
11230 }
11231 else
11232 decl_type = TREE_TYPE (decl);
11233
11234 if (arg == error_mark_node
11235 || decl == error_mark_node
11236 || TREE_TYPE (arg) == error_mark_node
11237 || decl_type == error_mark_node)
11238 /* Something already went wrong. There's no need to check
11239 further. */
11240 return error_mark_node;
11241
11242 /* [dcl.fct.default]
11243
11244 A default argument expression is implicitly converted to the
11245 parameter type. */
11246 if (!TREE_TYPE (arg)
11247 || !can_convert_arg (decl_type, TREE_TYPE (arg), arg))
11248 {
11249 if (decl)
11250 cp_error ("default argument for `%#D' has type `%T'",
11251 decl, TREE_TYPE (arg));
11252 else
8614a1d6 11253 cp_error ("default argument for parameter of type `%T' has type `%T'",
297e73d8
MM
11254 decl_type, TREE_TYPE (arg));
11255
11256 return error_mark_node;
11257 }
11258
11259 /* [dcl.fct.default]
11260
11261 Local variables shall not be used in default argument
11262 expressions.
11263
11264 The keyword `this' shall not be used in a default argument of a
11265 member function. */
11266 var = search_tree (arg, local_variable_p);
11267 if (var)
11268 {
11269 cp_error ("default argument `%E' uses local variable `%D'",
11270 arg, var);
11271 return error_mark_node;
11272 }
11273
11274 /* All is well. */
11275 return arg;
11276}
11277
8d08fdba
MS
11278/* Decode the list of parameter types for a function type.
11279 Given the list of things declared inside the parens,
11280 return a list of types.
11281
11282 The list we receive can have three kinds of elements:
11283 an IDENTIFIER_NODE for names given without types,
11284 a TREE_LIST node for arguments given as typespecs or names with typespecs,
11285 or void_type_node, to mark the end of an argument list
11286 when additional arguments are not permitted (... was not used).
11287
11288 FUNCDEF_FLAG is nonzero for a function definition, 0 for
11289 a mere declaration. A nonempty identifier-list gets an error message
11290 when FUNCDEF_FLAG is zero.
11291 If FUNCDEF_FLAG is 1, then parameter types must be complete.
11292 If FUNCDEF_FLAG is -1, then parameter types may be incomplete.
11293
11294 If all elements of the input list contain types,
11295 we return a list of the types.
11296 If all elements contain no type (except perhaps a void_type_node
11297 at the end), we return a null list.
11298 If some have types and some do not, it is an error, and we
11299 return a null list.
11300
11301 Also set last_function_parms to either
11302 a list of names (IDENTIFIER_NODEs) or a chain of PARM_DECLs.
11303 A list of names is converted to a chain of PARM_DECLs
11304 by store_parm_decls so that ultimately it is always a chain of decls.
11305
11306 Note that in C++, parameters can take default values. These default
11307 values are in the TREE_PURPOSE field of the TREE_LIST. It is
11308 an error to specify default values which are followed by parameters
11309 that have no default values, or an ELLIPSES. For simplicities sake,
11310 only parameters which are specified with their types can take on
11311 default values. */
11312
11313static tree
11314grokparms (first_parm, funcdef_flag)
11315 tree first_parm;
11316 int funcdef_flag;
11317{
11318 tree result = NULL_TREE;
11319 tree decls = NULL_TREE;
11320
11321 if (first_parm != NULL_TREE
11322 && TREE_CODE (TREE_VALUE (first_parm)) == IDENTIFIER_NODE)
11323 {
11324 if (! funcdef_flag)
8251199e 11325 pedwarn ("parameter names (without types) in function declaration");
8d08fdba
MS
11326 last_function_parms = first_parm;
11327 return NULL_TREE;
11328 }
11329 else if (first_parm != NULL_TREE
11330 && TREE_CODE (TREE_VALUE (first_parm)) != TREE_LIST
a1774733 11331 && TREE_CODE (TREE_VALUE (first_parm)) != VOID_TYPE)
8d08fdba
MS
11332 my_friendly_abort (145);
11333 else
11334 {
11335 /* Types were specified. This is a list of declarators
11336 each represented as a TREE_LIST node. */
11337 register tree parm, chain;
5566b478 11338 int any_init = 0, any_error = 0;
8d08fdba
MS
11339
11340 if (first_parm != NULL_TREE)
11341 {
11342 tree last_result = NULL_TREE;
11343 tree last_decl = NULL_TREE;
11344
11345 for (parm = first_parm; parm != NULL_TREE; parm = chain)
11346 {
a703fb38 11347 tree type = NULL_TREE, list_node = parm;
8d08fdba
MS
11348 register tree decl = TREE_VALUE (parm);
11349 tree init = TREE_PURPOSE (parm);
11350
11351 chain = TREE_CHAIN (parm);
11352 /* @@ weak defense against parse errors. */
a1774733
BK
11353 if (TREE_CODE (decl) != VOID_TYPE
11354 && TREE_CODE (decl) != TREE_LIST)
8d08fdba
MS
11355 {
11356 /* Give various messages as the need arises. */
11357 if (TREE_CODE (decl) == STRING_CST)
8251199e 11358 cp_error ("invalid string constant `%E'", decl);
8d08fdba 11359 else if (TREE_CODE (decl) == INTEGER_CST)
8251199e 11360 error ("invalid integer constant in parameter list, did you forget to give parameter name?");
8d08fdba
MS
11361 continue;
11362 }
11363
a1774733 11364 if (TREE_CODE (decl) != VOID_TYPE)
8d08fdba 11365 {
8d08fdba
MS
11366 decl = grokdeclarator (TREE_VALUE (decl),
11367 TREE_PURPOSE (decl),
f30432d7 11368 PARM, init != NULL_TREE,
c11b6f21 11369 NULL_TREE);
3bfdc719 11370 if (! decl || TREE_TYPE (decl) == error_mark_node)
8d08fdba 11371 continue;
01240200
MM
11372
11373 /* Top-level qualifiers on the parameters are
11374 ignored for function types. */
11375 type = TYPE_MAIN_VARIANT (TREE_TYPE (decl));
11376
a1774733 11377 if (TREE_CODE (type) == VOID_TYPE)
8d08fdba
MS
11378 decl = void_type_node;
11379 else if (TREE_CODE (type) == METHOD_TYPE)
11380 {
11381 if (DECL_NAME (decl))
fc378698 11382 /* Cannot use the decl here because
8d08fdba 11383 we don't have DECL_CONTEXT set up yet. */
8251199e 11384 cp_error ("parameter `%D' invalidly declared method type",
fc378698 11385 DECL_NAME (decl));
8d08fdba 11386 else
8251199e 11387 error ("parameter invalidly declared method type");
8d08fdba
MS
11388 type = build_pointer_type (type);
11389 TREE_TYPE (decl) = type;
11390 }
11391 else if (TREE_CODE (type) == OFFSET_TYPE)
11392 {
11393 if (DECL_NAME (decl))
8251199e 11394 cp_error ("parameter `%D' invalidly declared offset type",
fc378698 11395 DECL_NAME (decl));
8d08fdba 11396 else
8251199e 11397 error ("parameter invalidly declared offset type");
8d08fdba
MS
11398 type = build_pointer_type (type);
11399 TREE_TYPE (decl) = type;
11400 }
a7a64a77
MM
11401 else if (abstract_virtuals_error (decl, type))
11402 any_error = 1; /* Seems like a good idea. */
482b883f
JM
11403 else if (POINTER_TYPE_P (type))
11404 {
11405 tree t = type;
11406 while (POINTER_TYPE_P (t)
11407 || (TREE_CODE (t) == ARRAY_TYPE
11408 && TYPE_DOMAIN (t) != NULL_TREE))
11409 t = TREE_TYPE (t);
11410 if (TREE_CODE (t) == ARRAY_TYPE)
57af8358
MM
11411 cp_error ("parameter type `%T' includes %s to array of unknown bound",
11412 type,
11413 TYPE_PTR_P (type) ? "pointer" : "reference");
482b883f 11414 }
8d08fdba
MS
11415 }
11416
a1774733 11417 if (TREE_CODE (decl) == VOID_TYPE)
8d08fdba
MS
11418 {
11419 if (result == NULL_TREE)
11420 {
11421 result = void_list_node;
11422 last_result = result;
11423 }
11424 else
11425 {
11426 TREE_CHAIN (last_result) = void_list_node;
11427 last_result = void_list_node;
11428 }
8d08fdba
MS
11429 if (chain
11430 && (chain != void_list_node || TREE_CHAIN (chain)))
8251199e 11431 error ("`void' in parameter list must be entire list");
8d08fdba
MS
11432 break;
11433 }
11434
11435 /* Since there is a prototype, args are passed in their own types. */
11436 DECL_ARG_TYPE (decl) = TREE_TYPE (decl);
fa56377d
JJ
11437 if (PROMOTE_PROTOTYPES
11438 && (TREE_CODE (type) == INTEGER_TYPE
11439 || TREE_CODE (type) == ENUMERAL_TYPE)
8d08fdba
MS
11440 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
11441 DECL_ARG_TYPE (decl) = integer_type_node;
297e73d8 11442 if (!any_error && init)
8d08fdba 11443 {
297e73d8
MM
11444 any_init++;
11445 init = check_default_argument (decl, init);
8d08fdba
MS
11446 }
11447 else
11448 init = NULL_TREE;
11449
11450 if (decls == NULL_TREE)
11451 {
11452 decls = decl;
11453 last_decl = decls;
11454 }
11455 else
11456 {
11457 TREE_CHAIN (last_decl) = decl;
11458 last_decl = decl;
11459 }
5868eb4e 11460 list_node = tree_cons (init, type, NULL_TREE);
8d08fdba
MS
11461 if (result == NULL_TREE)
11462 {
11463 result = list_node;
11464 last_result = result;
11465 }
11466 else
11467 {
11468 TREE_CHAIN (last_result) = list_node;
11469 last_result = list_node;
11470 }
11471 }
11472 if (last_result)
11473 TREE_CHAIN (last_result) = NULL_TREE;
11474 /* If there are no parameters, and the function does not end
11475 with `...', then last_decl will be NULL_TREE. */
11476 if (last_decl != NULL_TREE)
11477 TREE_CHAIN (last_decl) = NULL_TREE;
11478 }
11479 }
11480
11481 last_function_parms = decls;
11482
8d08fdba
MS
11483 return result;
11484}
42976354
BK
11485
11486/* Called from the parser to update an element of TYPE_ARG_TYPES for some
11487 FUNCTION_TYPE with the newly parsed version of its default argument, which
11488 was previously digested as text. See snarf_defarg et al in lex.c. */
11489
11490void
11491replace_defarg (arg, init)
11492 tree arg, init;
11493{
aa5f3bad 11494 if (! processing_template_decl
42976354 11495 && ! can_convert_arg (TREE_VALUE (arg), TREE_TYPE (init), init))
8251199e 11496 cp_pedwarn ("invalid type `%T' for default argument to `%T'",
42976354
BK
11497 TREE_TYPE (init), TREE_VALUE (arg));
11498 TREE_PURPOSE (arg) = init;
11499}
8d08fdba 11500\f
c11b6f21
MS
11501int
11502copy_args_p (d)
11503 tree d;
11504{
11505 tree t = FUNCTION_ARG_CHAIN (d);
67437d5b
JM
11506 if (DECL_CONSTRUCTOR_P (d)
11507 && TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (d)))
11508 t = TREE_CHAIN (t);
c11b6f21
MS
11509 if (t && TREE_CODE (TREE_VALUE (t)) == REFERENCE_TYPE
11510 && (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_VALUE (t)))
11511 == DECL_CLASS_CONTEXT (d))
11512 && (TREE_CHAIN (t) == NULL_TREE
11513 || TREE_CHAIN (t) == void_list_node
11514 || TREE_PURPOSE (TREE_CHAIN (t))))
11515 return 1;
11516 return 0;
11517}
11518
8d08fdba
MS
11519/* These memoizing functions keep track of special properties which
11520 a class may have. `grok_ctor_properties' notices whether a class
11521 has a constructor of the form X(X&), and also complains
11522 if the class has a constructor of the form X(X).
11523 `grok_op_properties' takes notice of the various forms of
11524 operator= which are defined, as well as what sorts of type conversion
11525 may apply. Both functions take a FUNCTION_DECL as an argument. */
e92cc029 11526
a0a33927 11527int
8d08fdba
MS
11528grok_ctor_properties (ctype, decl)
11529 tree ctype, decl;
11530{
11531 tree parmtypes = FUNCTION_ARG_CHAIN (decl);
11532 tree parmtype = parmtypes ? TREE_VALUE (parmtypes) : void_type_node;
11533
11534 /* When a type has virtual baseclasses, a magical first int argument is
11535 added to any ctor so we can tell if the class has been initialized
11536 yet. This could screw things up in this function, so we deliberately
11537 ignore the leading int if we're in that situation. */
711734a9 11538 if (TYPE_USES_VIRTUAL_BASECLASSES (ctype))
8d08fdba 11539 {
711734a9
JM
11540 my_friendly_assert (parmtypes
11541 && TREE_VALUE (parmtypes) == integer_type_node,
11542 980529);
8d08fdba
MS
11543 parmtypes = TREE_CHAIN (parmtypes);
11544 parmtype = TREE_VALUE (parmtypes);
11545 }
11546
f181d4ae
MM
11547 /* [class.copy]
11548
11549 A non-template constructor for class X is a copy constructor if
11550 its first parameter is of type X&, const X&, volatile X& or const
11551 volatile X&, and either there are no other parameters or else all
11552 other parameters have default arguments. */
8d08fdba 11553 if (TREE_CODE (parmtype) == REFERENCE_TYPE
0b41abe6
JM
11554 && TYPE_MAIN_VARIANT (TREE_TYPE (parmtype)) == ctype
11555 && (TREE_CHAIN (parmtypes) == NULL_TREE
8d08fdba 11556 || TREE_CHAIN (parmtypes) == void_list_node
f181d4ae
MM
11557 || TREE_PURPOSE (TREE_CHAIN (parmtypes)))
11558 && !(DECL_TEMPLATE_INSTANTIATION (decl)
11559 && is_member_template (DECL_TI_TEMPLATE (decl))))
0b41abe6
JM
11560 {
11561 TYPE_HAS_INIT_REF (ctype) = 1;
91063b51 11562 if (CP_TYPE_CONST_P (TREE_TYPE (parmtype)))
0b41abe6 11563 TYPE_HAS_CONST_INIT_REF (ctype) = 1;
8d08fdba 11564 }
f181d4ae
MM
11565 /* [class.copy]
11566
11567 A declaration of a constructor for a class X is ill-formed if its
11568 first parameter is of type (optionally cv-qualified) X and either
11569 there are no other parameters or else all other parameters have
11570 default arguments.
11571
11572 We *don't* complain about member template instantiations that
11573 have this form, though; they can occur as we try to decide what
11574 constructor to use during overload resolution. Since overload
11575 resolution will never prefer such a constructor to the
11576 non-template copy constructor (which is either explicitly or
11577 implicitly defined), there's no need to worry about their
11578 existence. Theoretically, they should never even be
11579 instantiated, but that's hard to forestall. */
0b41abe6 11580 else if (TYPE_MAIN_VARIANT (parmtype) == ctype
59e76fc6
JM
11581 && (TREE_CHAIN (parmtypes) == NULL_TREE
11582 || TREE_CHAIN (parmtypes) == void_list_node
f181d4ae
MM
11583 || TREE_PURPOSE (TREE_CHAIN (parmtypes)))
11584 && !(DECL_TEMPLATE_INSTANTIATION (decl)
11585 && is_member_template (DECL_TI_TEMPLATE (decl))))
8d08fdba 11586 {
8251199e 11587 cp_error ("invalid constructor; you probably meant `%T (const %T&)'",
0b41abe6
JM
11588 ctype, ctype);
11589 SET_IDENTIFIER_ERROR_LOCUS (DECL_NAME (decl), ctype);
11590 return 0;
8d08fdba
MS
11591 }
11592 else if (TREE_CODE (parmtype) == VOID_TYPE
11593 || TREE_PURPOSE (parmtypes) != NULL_TREE)
11594 TYPE_HAS_DEFAULT_CONSTRUCTOR (ctype) = 1;
a0a33927
MS
11595
11596 return 1;
8d08fdba
MS
11597}
11598
11599/* An operator with this name can be either unary or binary. */
e92cc029 11600
a28e3c7f
MS
11601static int
11602ambi_op_p (name)
8d08fdba
MS
11603 tree name;
11604{
11605 return (name == ansi_opname [(int) INDIRECT_REF]
11606 || name == ansi_opname [(int) ADDR_EXPR]
11607 || name == ansi_opname [(int) NEGATE_EXPR]
11608 || name == ansi_opname[(int) POSTINCREMENT_EXPR]
11609 || name == ansi_opname[(int) POSTDECREMENT_EXPR]
11610 || name == ansi_opname [(int) CONVERT_EXPR]);
11611}
11612
11613/* An operator with this name can only be unary. */
e92cc029 11614
a28e3c7f
MS
11615static int
11616unary_op_p (name)
8d08fdba
MS
11617 tree name;
11618{
11619 return (name == ansi_opname [(int) TRUTH_NOT_EXPR]
11620 || name == ansi_opname [(int) BIT_NOT_EXPR]
11621 || name == ansi_opname [(int) COMPONENT_REF]
4c571114 11622 || IDENTIFIER_TYPENAME_P (name));
8d08fdba
MS
11623}
11624
11625/* Do a little sanity-checking on how they declared their operator. */
e92cc029 11626
5566b478 11627void
8d08fdba
MS
11628grok_op_properties (decl, virtualp, friendp)
11629 tree decl;
11630 int virtualp, friendp;
11631{
11632 tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
11633 int methodp = (TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE);
11634 tree name = DECL_NAME (decl);
8d08fdba 11635
a28e3c7f
MS
11636 if (current_class_type == NULL_TREE)
11637 friendp = 1;
8d08fdba 11638
a28e3c7f
MS
11639 if (! friendp)
11640 {
f181d4ae
MM
11641 /* [class.copy]
11642
11643 A user-declared copy assignment operator X::operator= is a
11644 non-static non-template member function of class X with
11645 exactly one parameter of type X, X&, const X&, volatile X& or
11646 const volatile X&. */
11647 if (name == ansi_opname[(int) MODIFY_EXPR]
11648 && !(DECL_TEMPLATE_INSTANTIATION (decl)
11649 && is_member_template (DECL_TI_TEMPLATE (decl))))
6a629cac 11650 ;
a28e3c7f
MS
11651 else if (name == ansi_opname[(int) CALL_EXPR])
11652 TYPE_OVERLOADS_CALL_EXPR (current_class_type) = 1;
11653 else if (name == ansi_opname[(int) ARRAY_REF])
11654 TYPE_OVERLOADS_ARRAY_REF (current_class_type) = 1;
11655 else if (name == ansi_opname[(int) COMPONENT_REF]
11656 || name == ansi_opname[(int) MEMBER_REF])
11657 TYPE_OVERLOADS_ARROW (current_class_type) = 1;
11658 else if (name == ansi_opname[(int) NEW_EXPR])
11659 TYPE_GETS_NEW (current_class_type) |= 1;
11660 else if (name == ansi_opname[(int) DELETE_EXPR])
11661 TYPE_GETS_DELETE (current_class_type) |= 1;
11662 else if (name == ansi_opname[(int) VEC_NEW_EXPR])
11663 TYPE_GETS_NEW (current_class_type) |= 2;
11664 else if (name == ansi_opname[(int) VEC_DELETE_EXPR])
11665 TYPE_GETS_DELETE (current_class_type) |= 2;
11666 }
11667
11668 if (name == ansi_opname[(int) NEW_EXPR]
11669 || name == ansi_opname[(int) VEC_NEW_EXPR])
8d08fdba 11670 {
8d08fdba
MS
11671 /* When the compiler encounters the definition of A::operator new, it
11672 doesn't look at the class declaration to find out if it's static. */
a28e3c7f 11673 if (methodp)
700f8a87 11674 revert_static_member_fn (&decl, NULL, NULL);
8d08fdba
MS
11675
11676 /* Take care of function decl if we had syntax errors. */
11677 if (argtypes == NULL_TREE)
beb53fb8
JM
11678 TREE_TYPE (decl)
11679 = build_function_type (ptr_type_node,
11680 hash_tree_chain (integer_type_node,
11681 void_list_node));
8d08fdba 11682 else
a28e3c7f 11683 TREE_TYPE (decl) = coerce_new_type (TREE_TYPE (decl));
8d08fdba 11684 }
a28e3c7f
MS
11685 else if (name == ansi_opname[(int) DELETE_EXPR]
11686 || name == ansi_opname[(int) VEC_DELETE_EXPR])
8d08fdba 11687 {
a28e3c7f 11688 if (methodp)
700f8a87 11689 revert_static_member_fn (&decl, NULL, NULL);
8d08fdba
MS
11690
11691 if (argtypes == NULL_TREE)
beb53fb8
JM
11692 TREE_TYPE (decl)
11693 = build_function_type (void_type_node,
11694 hash_tree_chain (ptr_type_node,
11695 void_list_node));
8d08fdba 11696 else
a28e3c7f
MS
11697 {
11698 TREE_TYPE (decl) = coerce_delete_type (TREE_TYPE (decl));
11699
11700 if (! friendp && name == ansi_opname[(int) VEC_DELETE_EXPR]
11701 && (TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
11702 != void_list_node))
11703 TYPE_VEC_DELETE_TAKES_SIZE (current_class_type) = 1;
11704 }
8d08fdba 11705 }
8d08fdba
MS
11706 else
11707 {
11708 /* An operator function must either be a non-static member function
11709 or have at least one parameter of a class, a reference to a class,
11710 an enumeration, or a reference to an enumeration. 13.4.0.6 */
700f8a87 11711 if (! methodp || DECL_STATIC_FUNCTION_P (decl))
8d08fdba 11712 {
4c571114 11713 if (IDENTIFIER_TYPENAME_P (name)
8d08fdba
MS
11714 || name == ansi_opname[(int) CALL_EXPR]
11715 || name == ansi_opname[(int) MODIFY_EXPR]
11716 || name == ansi_opname[(int) COMPONENT_REF]
11717 || name == ansi_opname[(int) ARRAY_REF])
8251199e 11718 cp_error ("`%D' must be a nonstatic member function", decl);
8d08fdba
MS
11719 else
11720 {
11721 tree p = argtypes;
11722
700f8a87 11723 if (DECL_STATIC_FUNCTION_P (decl))
8251199e 11724 cp_error ("`%D' must be either a non-static member function or a non-member function", decl);
700f8a87 11725
8d08fdba 11726 if (p)
a1774733 11727 for (; TREE_CODE (TREE_VALUE (p)) != VOID_TYPE ; p = TREE_CHAIN (p))
8d08fdba
MS
11728 {
11729 tree arg = TREE_VALUE (p);
11730 if (TREE_CODE (arg) == REFERENCE_TYPE)
11731 arg = TREE_TYPE (arg);
11732
11733 /* This lets bad template code slip through. */
11734 if (IS_AGGR_TYPE (arg)
11735 || TREE_CODE (arg) == ENUMERAL_TYPE
73b0fce8
KL
11736 || TREE_CODE (arg) == TEMPLATE_TYPE_PARM
11737 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
8d08fdba
MS
11738 goto foundaggr;
11739 }
8251199e
JM
11740 cp_error
11741 ("`%D' must have an argument of class or enumerated type",
8d08fdba
MS
11742 decl);
11743 foundaggr:
11744 ;
11745 }
11746 }
11747
277294d7 11748 if (name == ansi_opname[(int) CALL_EXPR])
2c73f9f5 11749 return; /* No restrictions on args. */
8d08fdba 11750
9a3b49ac 11751 if (IDENTIFIER_TYPENAME_P (name) && ! DECL_TEMPLATE_INFO (decl))
a0a33927
MS
11752 {
11753 tree t = TREE_TYPE (name);
11754 if (TREE_CODE (t) == VOID_TYPE)
8251199e 11755 pedwarn ("void is not a valid type conversion operator");
a0a33927
MS
11756 else if (! friendp)
11757 {
11758 int ref = (TREE_CODE (t) == REFERENCE_TYPE);
d8e178a0 11759 const char *what = 0;
a0a33927
MS
11760 if (ref)
11761 t = TYPE_MAIN_VARIANT (TREE_TYPE (t));
11762
11763 if (t == current_class_type)
11764 what = "the same type";
9a3b49ac 11765 /* Don't force t to be complete here. */
a0a33927 11766 else if (IS_AGGR_TYPE (t)
9a3b49ac 11767 && TYPE_SIZE (t)
a0a33927
MS
11768 && DERIVED_FROM_P (t, current_class_type))
11769 what = "a base class";
11770
11771 if (what)
8251199e 11772 warning ("conversion to %s%s will never use a type conversion operator",
a0a33927
MS
11773 ref ? "a reference to " : "", what);
11774 }
11775 }
11776
8d08fdba
MS
11777 if (name == ansi_opname[(int) MODIFY_EXPR])
11778 {
11779 tree parmtype;
11780
11781 if (list_length (argtypes) != 3 && methodp)
11782 {
8251199e 11783 cp_error ("`%D' must take exactly one argument", decl);
8d08fdba
MS
11784 return;
11785 }
11786 parmtype = TREE_VALUE (TREE_CHAIN (argtypes));
11787
f0e01782 11788 if (copy_assignment_arg_p (parmtype, virtualp)
a28e3c7f 11789 && ! friendp)
8d08fdba
MS
11790 {
11791 TYPE_HAS_ASSIGN_REF (current_class_type) = 1;
f0e01782 11792 if (TREE_CODE (parmtype) != REFERENCE_TYPE
91063b51 11793 || CP_TYPE_CONST_P (TREE_TYPE (parmtype)))
8d08fdba
MS
11794 TYPE_HAS_CONST_ASSIGN_REF (current_class_type) = 1;
11795 }
11796 }
11797 else if (name == ansi_opname[(int) COND_EXPR])
11798 {
11799 /* 13.4.0.3 */
a7a64a77 11800 cp_error ("ANSI C++ prohibits overloading operator ?:");
8d08fdba
MS
11801 }
11802 else if (ambi_op_p (name))
11803 {
11804 if (list_length (argtypes) == 2)
11805 /* prefix */;
11806 else if (list_length (argtypes) == 3)
11807 {
11808 if ((name == ansi_opname[(int) POSTINCREMENT_EXPR]
11809 || name == ansi_opname[(int) POSTDECREMENT_EXPR])
5156628f 11810 && ! processing_template_decl
007e5fea 11811 && ! same_type_p (TREE_VALUE (TREE_CHAIN (argtypes)), integer_type_node))
8d08fdba
MS
11812 {
11813 if (methodp)
8251199e 11814 cp_error ("postfix `%D' must take `int' as its argument",
8d08fdba
MS
11815 decl);
11816 else
8251199e
JM
11817 cp_error
11818 ("postfix `%D' must take `int' as its second argument",
11819 decl);
8d08fdba
MS
11820 }
11821 }
11822 else
11823 {
11824 if (methodp)
8251199e 11825 cp_error ("`%D' must take either zero or one argument", decl);
8d08fdba 11826 else
8251199e 11827 cp_error ("`%D' must take either one or two arguments", decl);
8d08fdba 11828 }
824b9a4c
MS
11829
11830 /* More Effective C++ rule 6. */
eb448459 11831 if (warn_ecpp
824b9a4c
MS
11832 && (name == ansi_opname[(int) POSTINCREMENT_EXPR]
11833 || name == ansi_opname[(int) POSTDECREMENT_EXPR]))
11834 {
11835 tree arg = TREE_VALUE (argtypes);
11836 tree ret = TREE_TYPE (TREE_TYPE (decl));
11837 if (methodp || TREE_CODE (arg) == REFERENCE_TYPE)
11838 arg = TREE_TYPE (arg);
11839 arg = TYPE_MAIN_VARIANT (arg);
11840 if (list_length (argtypes) == 2)
11841 {
11842 if (TREE_CODE (ret) != REFERENCE_TYPE
3bfdc719
MM
11843 || !same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (ret)),
11844 arg))
8251199e 11845 cp_warning ("prefix `%D' should return `%T'", decl,
824b9a4c
MS
11846 build_reference_type (arg));
11847 }
11848 else
11849 {
3bfdc719 11850 if (!same_type_p (TYPE_MAIN_VARIANT (ret), arg))
8251199e 11851 cp_warning ("postfix `%D' should return `%T'", decl, arg);
824b9a4c
MS
11852 }
11853 }
8d08fdba
MS
11854 }
11855 else if (unary_op_p (name))
11856 {
11857 if (list_length (argtypes) != 2)
11858 {
11859 if (methodp)
8251199e 11860 cp_error ("`%D' must take `void'", decl);
8d08fdba 11861 else
8251199e 11862 cp_error ("`%D' must take exactly one argument", decl);
8d08fdba
MS
11863 }
11864 }
11865 else /* if (binary_op_p (name)) */
11866 {
11867 if (list_length (argtypes) != 3)
11868 {
11869 if (methodp)
8251199e 11870 cp_error ("`%D' must take exactly one argument", decl);
8d08fdba 11871 else
8251199e 11872 cp_error ("`%D' must take exactly two arguments", decl);
8d08fdba 11873 }
824b9a4c
MS
11874
11875 /* More Effective C++ rule 7. */
eb448459 11876 if (warn_ecpp
824b9a4c
MS
11877 && (name == ansi_opname [TRUTH_ANDIF_EXPR]
11878 || name == ansi_opname [TRUTH_ORIF_EXPR]
11879 || name == ansi_opname [COMPOUND_EXPR]))
8251199e 11880 cp_warning ("user-defined `%D' always evaluates both arguments",
824b9a4c
MS
11881 decl);
11882 }
11883
11884 /* Effective C++ rule 23. */
eb448459 11885 if (warn_ecpp
824b9a4c
MS
11886 && list_length (argtypes) == 3
11887 && (name == ansi_opname [PLUS_EXPR]
11888 || name == ansi_opname [MINUS_EXPR]
11889 || name == ansi_opname [TRUNC_DIV_EXPR]
11890 || name == ansi_opname [MULT_EXPR])
11891 && TREE_CODE (TREE_TYPE (TREE_TYPE (decl))) == REFERENCE_TYPE)
8251199e 11892 cp_warning ("`%D' should return by value", decl);
8d08fdba
MS
11893
11894 /* 13.4.0.8 */
11895 if (argtypes)
11896 for (; argtypes != void_list_node ; argtypes = TREE_CHAIN (argtypes))
11897 if (TREE_PURPOSE (argtypes))
11898 {
11899 TREE_PURPOSE (argtypes) = NULL_TREE;
11900 if (name == ansi_opname[(int) POSTINCREMENT_EXPR]
11901 || name == ansi_opname[(int) POSTDECREMENT_EXPR])
11902 {
11903 if (pedantic)
8251199e 11904 cp_pedwarn ("`%D' cannot have default arguments", decl);
8d08fdba
MS
11905 }
11906 else
8251199e 11907 cp_error ("`%D' cannot have default arguments", decl);
8d08fdba
MS
11908 }
11909 }
11910}
11911\f
d8e178a0 11912static const char *
094fe153
JM
11913tag_name (code)
11914 enum tag_types code;
11915{
11916 switch (code)
11917 {
11918 case record_type:
11919 return "struct";
11920 case class_type:
11921 return "class";
11922 case union_type:
11923 return "union ";
11924 case enum_type:
11925 return "enum";
094fe153
JM
11926 default:
11927 my_friendly_abort (981122);
11928 }
11929}
11930
8d08fdba
MS
11931/* Get the struct, enum or union (CODE says which) with tag NAME.
11932 Define the tag as a forward-reference if it is not defined.
11933
11934 C++: If a class derivation is given, process it here, and report
11935 an error if multiple derivation declarations are not identical.
11936
11937 If this is a definition, come in through xref_tag and only look in
11938 the current frame for the name (since C++ allows new names in any
11939 scope.) */
11940
8d08fdba 11941tree
ca107ded 11942xref_tag (code_type_node, name, globalize)
8d08fdba 11943 tree code_type_node;
ca107ded 11944 tree name;
8d08fdba
MS
11945 int globalize;
11946{
11947 enum tag_types tag_code;
11948 enum tree_code code;
11949 int temp = 0;
8d08fdba 11950 register tree ref, t;
8f032717 11951 struct binding_level *b = current_binding_level;
a80e4195 11952 int got_type = 0;
dc8263bc 11953 tree attributes = NULL_TREE;
25aab5d0 11954 tree context = NULL_TREE;
dc8263bc
JM
11955
11956 /* If we are called from the parser, code_type_node will sometimes be a
11957 TREE_LIST. This indicates that the user wrote
11958 "class __attribute__ ((foo)) bar". Extract the attributes so we can
11959 use them later. */
11960 if (TREE_CODE (code_type_node) == TREE_LIST)
11961 {
11962 attributes = TREE_PURPOSE (code_type_node);
11963 code_type_node = TREE_VALUE (code_type_node);
11964 }
8d08fdba
MS
11965
11966 tag_code = (enum tag_types) TREE_INT_CST_LOW (code_type_node);
11967 switch (tag_code)
11968 {
11969 case record_type:
11970 case class_type:
8d08fdba 11971 code = RECORD_TYPE;
8d08fdba
MS
11972 break;
11973 case union_type:
11974 code = UNION_TYPE;
8d08fdba
MS
11975 break;
11976 case enum_type:
11977 code = ENUMERAL_TYPE;
11978 break;
11979 default:
11980 my_friendly_abort (18);
11981 }
11982
11983 /* If a cross reference is requested, look up the type
11984 already defined for this tag and return it. */
be99da77
MS
11985 if (TREE_CODE_CLASS (TREE_CODE (name)) == 't')
11986 {
11987 t = name;
a80e4195
MS
11988 name = TYPE_IDENTIFIER (t);
11989 got_type = 1;
be99da77
MS
11990 }
11991 else
11992 t = IDENTIFIER_TYPE_VALUE (name);
61a127b3 11993
73b0fce8
KL
11994 if (t && TREE_CODE (t) != code && TREE_CODE (t) != TEMPLATE_TYPE_PARM
11995 && TREE_CODE (t) != TEMPLATE_TEMPLATE_PARM)
a0a33927
MS
11996 t = NULL_TREE;
11997
8ccc31eb 11998 if (! globalize)
8d08fdba 11999 {
f3400fe2
JM
12000 /* If we know we are defining this tag, only look it up in
12001 this scope and don't try to find it as a type. */
12002 ref = lookup_tag (code, name, b, 1);
8d08fdba
MS
12003 }
12004 else
12005 {
25aab5d0 12006 if (t)
36a117a5 12007 {
25aab5d0
MM
12008 /* [dcl.type.elab] If the identifier resolves to a
12009 typedef-name or a template type-parameter, the
12010 elaborated-type-specifier is ill-formed. */
12011 if (t != TYPE_MAIN_VARIANT (t)
12012 || (CLASS_TYPE_P (t) && TYPE_WAS_ANONYMOUS (t)))
12013 cp_pedwarn ("using typedef-name `%D' after `%s'",
12014 TYPE_NAME (t), tag_name (tag_code));
12015 else if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
12016 cp_error ("using template type parameter `%T' after `%s'",
12017 t, tag_name (tag_code));
12018
12019 ref = t;
12020 }
12021 else
12022 ref = lookup_tag (code, name, b, 0);
36a117a5 12023
25aab5d0
MM
12024 if (! ref)
12025 {
12026 /* Try finding it as a type declaration. If that wins,
12027 use it. */
12028 ref = lookup_name (name, 1);
12029
12030 if (ref != NULL_TREE
12031 && processing_template_decl
12032 && DECL_CLASS_TEMPLATE_P (ref)
12033 && template_class_depth (current_class_type) == 0)
12034 /* Since GLOBALIZE is true, we're declaring a global
6757edfe 12035 template, so we want this type. */
25aab5d0 12036 ref = DECL_RESULT (ref);
6757edfe 12037
25aab5d0
MM
12038 if (ref && TREE_CODE (ref) == TYPE_DECL
12039 && TREE_CODE (TREE_TYPE (ref)) == code)
12040 ref = TREE_TYPE (ref);
12041 else
12042 ref = NULL_TREE;
12043 }
12044
12045 if (ref && current_class_type
12046 && template_class_depth (current_class_type)
12047 && PROCESSING_REAL_TEMPLATE_DECL_P ())
12048 {
12049 /* Since GLOBALIZE is non-zero, we are not looking at a
12050 definition of this tag. Since, in addition, we are currently
12051 processing a (member) template declaration of a template
12052 class, we must be very careful; consider:
12053
12054 template <class X>
12055 struct S1
12056
12057 template <class U>
12058 struct S2
12059 { template <class V>
12060 friend struct S1; };
12061
12062 Here, the S2::S1 declaration should not be confused with the
12063 outer declaration. In particular, the inner version should
12064 have a template parameter of level 2, not level 1. This
12065 would be particularly important if the member declaration
12066 were instead:
12067
12068 template <class V = U> friend struct S1;
12069
12070 say, when we should tsubst into `U' when instantiating
12071 S2. On the other hand, when presented with:
12072
12073 template <class T>
12074 struct S1 {
12075 template <class U>
12076 struct S2 {};
12077 template <class U>
12078 friend struct S2;
12079 };
12080
12081 we must find the inner binding eventually. We
12082 accomplish this by making sure that the new type we
12083 create to represent this declaration has the right
12084 TYPE_CONTEXT. */
12085 context = TYPE_CONTEXT (ref);
12086 ref = NULL_TREE;
8d08fdba
MS
12087 }
12088 }
12089
12090 push_obstacks_nochange ();
12091
12092 if (! ref)
12093 {
12094 /* If no such tag is yet defined, create a forward-reference node
12095 and record it as the "definition".
12096 When a real declaration of this type is found,
12097 the forward-reference will be altered into a real type. */
12098
12099 /* In C++, since these migrate into the global scope, we must
12100 build them on the permanent obstack. */
12101
12102 temp = allocation_temporary_p ();
12103 if (temp)
12104 end_temporary_allocation ();
12105
12106 if (code == ENUMERAL_TYPE)
12107 {
8251199e 12108 cp_error ("use of enum `%#D' without previous declaration", name);
fc378698 12109
8d08fdba
MS
12110 ref = make_node (ENUMERAL_TYPE);
12111
12112 /* Give the type a default layout like unsigned int
12113 to avoid crashing if it does not get defined. */
12114 TYPE_MODE (ref) = TYPE_MODE (unsigned_type_node);
12115 TYPE_ALIGN (ref) = TYPE_ALIGN (unsigned_type_node);
12116 TREE_UNSIGNED (ref) = 1;
12117 TYPE_PRECISION (ref) = TYPE_PRECISION (unsigned_type_node);
12118 TYPE_MIN_VALUE (ref) = TYPE_MIN_VALUE (unsigned_type_node);
12119 TYPE_MAX_VALUE (ref) = TYPE_MAX_VALUE (unsigned_type_node);
12120
12121 /* Enable us to recognize when a type is created in class context.
12122 To do nested classes correctly, this should probably be cleared
12123 out when we leave this classes scope. Currently this in only
12124 done in `start_enum'. */
12125
12126 pushtag (name, ref, globalize);
8d08fdba 12127 }
8d08fdba
MS
12128 else
12129 {
8d08fdba 12130 struct binding_level *old_b = class_binding_level;
8d08fdba
MS
12131
12132 ref = make_lang_type (code);
25aab5d0 12133 TYPE_CONTEXT (ref) = context;
8d08fdba 12134
8d08fdba
MS
12135#ifdef NONNESTED_CLASSES
12136 /* Class types don't nest the way enums do. */
12137 class_binding_level = (struct binding_level *)0;
12138#endif
12139 pushtag (name, ref, globalize);
12140 class_binding_level = old_b;
8d08fdba
MS
12141 }
12142 }
12143 else
12144 {
8d08fdba 12145 /* If it no longer looks like a nested type, make sure it's
30394414
JM
12146 in global scope.
12147 If it is not an IDENTIFIER, this is not a declaration */
2c73f9f5 12148 if (b->namespace_p && !class_binding_level
5fdaba89
MM
12149 && TREE_CODE (name) == IDENTIFIER_NODE
12150 && IDENTIFIER_NAMESPACE_VALUE (name) == NULL_TREE)
12151 SET_IDENTIFIER_NAMESPACE_VALUE (name, TYPE_NAME (ref));
7fe6899f
MM
12152
12153 if (!globalize && processing_template_decl && IS_AGGR_TYPE (ref))
6757edfe 12154 redeclare_class_template (ref, current_template_parms);
8d08fdba
MS
12155 }
12156
8d08fdba
MS
12157 /* Until the type is defined, tentatively accept whatever
12158 structure tag the user hands us. */
12159 if (TYPE_SIZE (ref) == NULL_TREE
12160 && ref != current_class_type
12161 /* Have to check this, in case we have contradictory tag info. */
12162 && IS_AGGR_TYPE_CODE (TREE_CODE (ref)))
12163 {
12164 if (tag_code == class_type)
12165 CLASSTYPE_DECLARED_CLASS (ref) = 1;
6eabb241 12166 else if (tag_code == record_type)
8d08fdba
MS
12167 CLASSTYPE_DECLARED_CLASS (ref) = 0;
12168 }
12169
12170 pop_obstacks ();
12171
dc8263bc
JM
12172 TREE_TYPE (ref) = attributes;
12173
8d08fdba
MS
12174 return ref;
12175}
8ccc31eb 12176
fc378698
MS
12177tree
12178xref_tag_from_type (old, id, globalize)
12179 tree old, id;
12180 int globalize;
12181{
12182 tree code_type_node;
12183
12184 if (TREE_CODE (old) == RECORD_TYPE)
12185 code_type_node = (CLASSTYPE_DECLARED_CLASS (old)
12186 ? class_type_node : record_type_node);
12187 else
12188 code_type_node = union_type_node;
12189
12190 if (id == NULL_TREE)
12191 id = TYPE_IDENTIFIER (old);
12192
ca107ded 12193 return xref_tag (code_type_node, id, globalize);
fc378698
MS
12194}
12195
3fd71a52
MM
12196/* REF is a type (named NAME), for which we have just seen some
12197 baseclasses. BINFO is a list of those baseclasses; the
12198 TREE_PURPOSE is an access_* node, and the TREE_VALUE is the type of
12199 the base-class. CODE_TYPE_NODE indicates whether REF is a class,
12200 struct, or union. */
12201
8ccc31eb
MS
12202void
12203xref_basetypes (code_type_node, name, ref, binfo)
12204 tree code_type_node;
12205 tree name, ref;
12206 tree binfo;
12207{
12208 /* In the declaration `A : X, Y, ... Z' we mark all the types
12209 (A, X, Y, ..., Z) so we can check for duplicates. */
12210 tree binfos;
d6479fe7
MM
12211 tree base;
12212
8ccc31eb
MS
12213 int i, len;
12214 enum tag_types tag_code = (enum tag_types) TREE_INT_CST_LOW (code_type_node);
12215
12216 if (tag_code == union_type)
12217 {
8251199e 12218 cp_error ("derived union `%T' invalid", ref);
8ccc31eb
MS
12219 return;
12220 }
12221
12222 len = list_length (binfo);
12223 push_obstacks (TYPE_OBSTACK (ref), TYPE_OBSTACK (ref));
12224
d6479fe7
MM
12225 /* First, make sure that any templates in base-classes are
12226 instantiated. This ensures that if we call ourselves recursively
12227 we do not get confused about which classes are marked and which
12228 are not. */
12229 for (base = binfo; base; base = TREE_CHAIN (base))
12230 complete_type (TREE_VALUE (base));
12231
8ccc31eb
MS
12232 SET_CLASSTYPE_MARKED (ref);
12233 BINFO_BASETYPES (TYPE_BINFO (ref)) = binfos = make_tree_vec (len);
12234
12235 for (i = 0; binfo; binfo = TREE_CHAIN (binfo))
12236 {
12237 /* The base of a derived struct is public by default. */
12238 int via_public
be99da77
MS
12239 = (TREE_PURPOSE (binfo) == access_public_node
12240 || TREE_PURPOSE (binfo) == access_public_virtual_node
8ccc31eb 12241 || (tag_code != class_type
be99da77
MS
12242 && (TREE_PURPOSE (binfo) == access_default_node
12243 || TREE_PURPOSE (binfo) == access_default_virtual_node)));
d8b55a76
JM
12244 int via_protected
12245 = (TREE_PURPOSE (binfo) == access_protected_node
12246 || TREE_PURPOSE (binfo) == access_protected_virtual_node);
8ccc31eb 12247 int via_virtual
be99da77 12248 = (TREE_PURPOSE (binfo) == access_private_virtual_node
d8b55a76 12249 || TREE_PURPOSE (binfo) == access_protected_virtual_node
be99da77
MS
12250 || TREE_PURPOSE (binfo) == access_public_virtual_node
12251 || TREE_PURPOSE (binfo) == access_default_virtual_node);
12252 tree basetype = TREE_VALUE (binfo);
8ccc31eb
MS
12253 tree base_binfo;
12254
8ccc31eb
MS
12255 if (basetype && TREE_CODE (basetype) == TYPE_DECL)
12256 basetype = TREE_TYPE (basetype);
5566b478
MS
12257 if (!basetype
12258 || (TREE_CODE (basetype) != RECORD_TYPE
a80e4195 12259 && TREE_CODE (basetype) != TYPENAME_TYPE
73b0fce8
KL
12260 && TREE_CODE (basetype) != TEMPLATE_TYPE_PARM
12261 && TREE_CODE (basetype) != TEMPLATE_TEMPLATE_PARM))
8ccc31eb 12262 {
8251199e 12263 cp_error ("base type `%T' fails to be a struct or class type",
8ccc31eb
MS
12264 TREE_VALUE (binfo));
12265 continue;
12266 }
2b9dc906 12267
11b5139c 12268 GNU_xref_hier (name, basetype, via_public, via_virtual, 0);
2b9dc906 12269
8adf5b5e
JM
12270 /* This code replaces similar code in layout_basetypes.
12271 We put the complete_type first for implicit `typename'. */
d6479fe7 12272 if (TYPE_SIZE (basetype) == NULL_TREE
2b9dc906 12273 && ! (current_template_parms && uses_template_parms (basetype)))
8ccc31eb 12274 {
8251199e 12275 cp_error ("base class `%T' has incomplete type", basetype);
8ccc31eb
MS
12276 continue;
12277 }
8ccc31eb
MS
12278 else
12279 {
12280 if (CLASSTYPE_MARKED (basetype))
12281 {
12282 if (basetype == ref)
8251199e 12283 cp_error ("recursive type `%T' undefined", basetype);
8ccc31eb 12284 else
8251199e 12285 cp_error ("duplicate base type `%T' invalid", basetype);
8ccc31eb
MS
12286 continue;
12287 }
12288
eff71ab0 12289 if (TYPE_FOR_JAVA (basetype)
9cd64686
MM
12290 && (current_lang_stack
12291 == &VARRAY_TREE (current_lang_base, 0)))
eff71ab0
PB
12292 TYPE_FOR_JAVA (ref) = 1;
12293
8ccc31eb
MS
12294 /* Note that the BINFO records which describe individual
12295 inheritances are *not* shared in the lattice! They
12296 cannot be shared because a given baseclass may be
12297 inherited with different `accessibility' by different
12298 derived classes. (Each BINFO record describing an
12299 individual inheritance contains flags which say what
12300 the `accessibility' of that particular inheritance is.) */
12301
7ddedda4
MM
12302 base_binfo
12303 = make_binfo (integer_zero_node, basetype,
12304 CLASS_TYPE_P (basetype)
12305 ? TYPE_BINFO_VTABLE (basetype) : NULL_TREE,
12306 CLASS_TYPE_P (basetype)
12307 ? TYPE_BINFO_VIRTUALS (basetype) : NULL_TREE);
8ccc31eb
MS
12308
12309 TREE_VEC_ELT (binfos, i) = base_binfo;
12310 TREE_VIA_PUBLIC (base_binfo) = via_public;
12311 TREE_VIA_PROTECTED (base_binfo) = via_protected;
12312 TREE_VIA_VIRTUAL (base_binfo) = via_virtual;
12313 BINFO_INHERITANCE_CHAIN (base_binfo) = TYPE_BINFO (ref);
12314
dfbcd65a
JM
12315 /* We need to unshare the binfos now so that lookups during class
12316 definition work. */
12317 unshare_base_binfos (base_binfo);
12318
8ccc31eb 12319 SET_CLASSTYPE_MARKED (basetype);
9e9ff709 12320
8ccc31eb
MS
12321 /* We are free to modify these bits because they are meaningless
12322 at top level, and BASETYPE is a top-level type. */
12323 if (via_virtual || TYPE_USES_VIRTUAL_BASECLASSES (basetype))
12324 {
12325 TYPE_USES_VIRTUAL_BASECLASSES (ref) = 1;
12326 TYPE_USES_COMPLEX_INHERITANCE (ref) = 1;
12327 }
12328
7ddedda4
MM
12329 if (CLASS_TYPE_P (basetype))
12330 {
12331 TYPE_GETS_NEW (ref) |= TYPE_GETS_NEW (basetype);
12332 TYPE_GETS_DELETE (ref) |= TYPE_GETS_DELETE (basetype);
12333 }
12334
8ccc31eb
MS
12335 i += 1;
12336 }
12337 }
12338 if (i)
12339 TREE_VEC_LENGTH (binfos) = i;
12340 else
12341 BINFO_BASETYPES (TYPE_BINFO (ref)) = NULL_TREE;
12342
12343 if (i > 1)
12344 TYPE_USES_MULTIPLE_INHERITANCE (ref) = 1;
12345 else if (i == 1)
7ddedda4
MM
12346 {
12347 tree basetype = BINFO_TYPE (TREE_VEC_ELT (binfos, 0));
12348
12349 if (CLASS_TYPE_P (basetype))
12350 TYPE_USES_MULTIPLE_INHERITANCE (ref)
12351 = TYPE_USES_MULTIPLE_INHERITANCE (basetype);
12352 }
12353
8ccc31eb
MS
12354 if (TYPE_USES_MULTIPLE_INHERITANCE (ref))
12355 TYPE_USES_COMPLEX_INHERITANCE (ref) = 1;
12356
12357 /* Unmark all the types. */
12358 while (--i >= 0)
12359 CLEAR_CLASSTYPE_MARKED (BINFO_TYPE (TREE_VEC_ELT (binfos, i)));
12360 CLEAR_CLASSTYPE_MARKED (ref);
12361
70c532b5
MM
12362 /* Now that we know all the base-classes, set up the list of virtual
12363 bases. */
12364 CLASSTYPE_VBASECLASSES (ref) = get_vbase_types (ref);
12365
8ccc31eb
MS
12366 pop_obstacks ();
12367}
12368
8d08fdba 12369\f
8d08fdba
MS
12370/* Begin compiling the definition of an enumeration type.
12371 NAME is its name (or null if anonymous).
12372 Returns the type object, as yet incomplete.
12373 Also records info about it so that build_enumerator
12374 may be used to declare the individual values as they are read. */
12375
12376tree
12377start_enum (name)
12378 tree name;
12379{
12380 register tree enumtype = NULL_TREE;
8f032717 12381 struct binding_level *b = current_binding_level;
8d08fdba 12382
8c1e8edc 12383 /* We are wasting space here and putting these on the permanent_obstack so
6e687e5e 12384 that typeid(local enum) will work correctly. */
cbf882af 12385 push_obstacks (&permanent_obstack, &permanent_obstack);
b87692e5 12386
8d08fdba
MS
12387 /* If this is the real definition for a previous forward reference,
12388 fill in the contents in the same object that used to be the
12389 forward reference. */
12390
12391 if (name != NULL_TREE)
12392 enumtype = lookup_tag (ENUMERAL_TYPE, name, b, 1);
12393
12394 if (enumtype != NULL_TREE && TREE_CODE (enumtype) == ENUMERAL_TYPE)
30ff8252
NS
12395 {
12396 cp_error ("multiple definition of `%#T'", enumtype);
12397 cp_error_at ("previous definition here", enumtype);
12398 }
8d08fdba
MS
12399 else
12400 {
12401 enumtype = make_node (ENUMERAL_TYPE);
12402 pushtag (name, enumtype, 0);
12403 }
12404
12405 if (current_class_type)
12406 TREE_ADDRESSABLE (b->tags) = 1;
c91a56d2 12407
e92cc029
MS
12408 /* We don't copy this value because build_enumerator needs to do it. */
12409 enum_next_value = integer_zero_node;
7177d104 12410 enum_overflow = 0;
8d08fdba
MS
12411
12412 GNU_xref_decl (current_function_decl, enumtype);
12413 return enumtype;
12414}
12415
12416/* After processing and defining all the values of an enumeration type,
12417 install their decls in the enumeration type and finish it off.
12418 ENUMTYPE is the type object and VALUES a list of name-value pairs.
12419 Returns ENUMTYPE. */
12420
12421tree
dbfe2124
MM
12422finish_enum (enumtype)
12423 tree enumtype;
8d08fdba 12424{
a703fb38 12425 register tree minnode = NULL_TREE, maxnode = NULL_TREE;
8d08fdba
MS
12426 /* Calculate the maximum value of any enumerator in this type. */
12427
dbfe2124 12428 tree values = TYPE_VALUES (enumtype);
8d08fdba
MS
12429 if (values)
12430 {
ed44da02 12431 tree pair;
5566b478 12432
ed44da02 12433 for (pair = values; pair; pair = TREE_CHAIN (pair))
8d08fdba 12434 {
ed44da02
MM
12435 tree decl;
12436 tree value;
12437
12438 /* The TREE_VALUE is a CONST_DECL for this enumeration
12439 constant. */
12440 decl = TREE_VALUE (pair);
12441
ed44da02
MM
12442 /* The DECL_INITIAL will be NULL if we are processing a
12443 template declaration and this enumeration constant had no
12444 explicit initializer. */
12445 value = DECL_INITIAL (decl);
079e1098 12446 if (value && !processing_template_decl)
5566b478 12447 {
079e1098
MM
12448 /* Set the TREE_TYPE for the VALUE as well. That's so
12449 that when we call decl_constant_value we get an
12450 entity of the right type (but with the constant
12451 value). Since we shouldn't ever call
12452 decl_constant_value on a template type, there's no
12453 reason to do that when processing_template_decl.
12454 And, if the expression is something like a
12455 TEMPLATE_PARM_INDEX or a CAST_EXPR doing so will
12456 wreak havoc on the intended type of the expression.
12457
12458 Of course, there's also no point in trying to compute
12459 minimum or maximum values if we're in a template. */
5566b478 12460 TREE_TYPE (value) = enumtype;
ed44da02 12461
079e1098
MM
12462 if (!minnode)
12463 minnode = maxnode = value;
12464 else if (tree_int_cst_lt (maxnode, value))
12465 maxnode = value;
12466 else if (tree_int_cst_lt (value, minnode))
12467 minnode = value;
5566b478 12468 }
ed44da02 12469
72f2bd78
MM
12470 if (processing_template_decl)
12471 /* If this is just a template, leave the CONST_DECL
12472 alone. That way tsubst_copy will find CONST_DECLs for
12473 CONST_DECLs, and not INTEGER_CSTs. */
12474 ;
12475 else
12476 /* In the list we're building up, we want the enumeration
12477 values, not the CONST_DECLs. */
12478 TREE_VALUE (pair) = value;
8d08fdba
MS
12479 }
12480 }
f376e137
MS
12481 else
12482 maxnode = minnode = integer_zero_node;
8d08fdba 12483
de22184b 12484 TYPE_VALUES (enumtype) = nreverse (values);
2986ae00 12485
5156628f 12486 if (processing_template_decl)
b87692e5 12487 {
9360ac70
MM
12488 tree scope = current_scope ();
12489 if (scope && TREE_CODE (scope) == FUNCTION_DECL)
cbf882af 12490 add_tree (build_min (TAG_DEFN, enumtype));
b87692e5 12491 }
cbf882af
MM
12492 else
12493 {
12494 int unsignedp = tree_int_cst_sgn (minnode) >= 0;
12495 int lowprec = min_precision (minnode, unsignedp);
12496 int highprec = min_precision (maxnode, unsignedp);
12497 int precision = MAX (lowprec, highprec);
12498 tree tem;
5566b478 12499
cbf882af 12500 TYPE_SIZE (enumtype) = NULL_TREE;
8d08fdba 12501
cbf882af 12502 /* Set TYPE_MIN_VALUE and TYPE_MAX_VALUE according to `precision'. */
8ccc31eb 12503
cbf882af
MM
12504 TYPE_PRECISION (enumtype) = precision;
12505 if (unsignedp)
12506 fixup_unsigned_type (enumtype);
12507 else
12508 fixup_signed_type (enumtype);
8ccc31eb 12509
cbf882af
MM
12510 if (flag_short_enums || (precision > TYPE_PRECISION (integer_type_node)))
12511 /* Use the width of the narrowest normal C type which is wide
12512 enough. */
12513 TYPE_PRECISION (enumtype) = TYPE_PRECISION (type_for_size
12514 (precision, 1));
12515 else
12516 TYPE_PRECISION (enumtype) = TYPE_PRECISION (integer_type_node);
8d08fdba 12517
cbf882af
MM
12518 TYPE_SIZE (enumtype) = 0;
12519 layout_type (enumtype);
f376e137 12520
cbf882af
MM
12521 /* Fix up all variant types of this enum type. */
12522 for (tem = TYPE_MAIN_VARIANT (enumtype); tem;
12523 tem = TYPE_NEXT_VARIANT (tem))
12524 {
12525 TYPE_VALUES (tem) = TYPE_VALUES (enumtype);
12526 TYPE_MIN_VALUE (tem) = TYPE_MIN_VALUE (enumtype);
12527 TYPE_MAX_VALUE (tem) = TYPE_MAX_VALUE (enumtype);
12528 TYPE_SIZE (tem) = TYPE_SIZE (enumtype);
12529 TYPE_SIZE_UNIT (tem) = TYPE_SIZE_UNIT (enumtype);
12530 TYPE_MODE (tem) = TYPE_MODE (enumtype);
12531 TYPE_PRECISION (tem) = TYPE_PRECISION (enumtype);
12532 TYPE_ALIGN (tem) = TYPE_ALIGN (enumtype);
12533 TREE_UNSIGNED (tem) = TREE_UNSIGNED (enumtype);
12534 }
8d08fdba 12535
cbf882af
MM
12536 /* Finish debugging output for this type. */
12537 rest_of_type_compilation (enumtype, namespace_bindings_p ());
12538 }
12539
12540 /* In start_enum we pushed obstacks. Here, we must pop them. */
12541 pop_obstacks ();
8d08fdba
MS
12542
12543 return enumtype;
12544}
12545
079e1098
MM
12546/* Build and install a CONST_DECL for an enumeration constant of the
12547 enumeration type TYPE whose NAME and VALUE (if any) are provided.
8d08fdba
MS
12548 Assignment of sequential values by default is handled here. */
12549
12550tree
079e1098
MM
12551build_enumerator (name, value, type)
12552 tree name;
12553 tree value;
12554 tree type;
8d08fdba
MS
12555{
12556 tree decl, result;
e8bd800e 12557 tree context;
8d08fdba
MS
12558
12559 /* Remove no-op casts from the value. */
12560 if (value)
12561 STRIP_TYPE_NOPS (value);
12562
5156628f 12563 if (! processing_template_decl)
5566b478
MS
12564 {
12565 /* Validate and default VALUE. */
12566 if (value != NULL_TREE)
12567 {
12568 if (TREE_READONLY_DECL_P (value))
e92cc029 12569 value = decl_constant_value (value);
5566b478
MS
12570
12571 if (TREE_CODE (value) == INTEGER_CST)
12572 {
12573 value = default_conversion (value);
12574 constant_expression_warning (value);
12575 }
12576 else
12577 {
8251199e 12578 cp_error ("enumerator value for `%D' not integer constant", name);
5566b478
MS
12579 value = NULL_TREE;
12580 }
12581 }
12582
5566b478 12583 /* Default based on previous value. */
5156628f 12584 if (value == NULL_TREE && ! processing_template_decl)
5566b478
MS
12585 {
12586 value = enum_next_value;
12587 if (enum_overflow)
8251199e 12588 cp_error ("overflow in enumeration values at `%D'", name);
5566b478
MS
12589 }
12590
12591 /* Remove no-op casts from the value. */
12592 if (value)
12593 STRIP_TYPE_NOPS (value);
013bc8af
MS
12594#if 0
12595 /* To fix MAX_VAL enum consts. (bkoz) */
e92cc029 12596 TREE_TYPE (value) = integer_type_node;
013bc8af 12597#endif
5566b478 12598 }
8d08fdba 12599
079e1098
MM
12600 /* We always have to copy here; not all INTEGER_CSTs are unshared.
12601 Even in other cases, we will later (in finish_enum) be setting the
12602 type of VALUE. */
ed44da02
MM
12603 if (value != NULL_TREE)
12604 value = copy_node (value);
12605
8d08fdba 12606 /* C++ associates enums with global, function, or class declarations. */
e8bd800e
MM
12607
12608 context = current_scope ();
12609 if (context && context == current_class_type)
12610 /* This enum declaration is local to the class. */
4ce3d537 12611 decl = build_lang_decl (CONST_DECL, name, type);
e8bd800e
MM
12612 else
12613 /* It's a global enum, or it's local to a function. (Note local to
12614 a function could mean local to a class method. */
079e1098 12615 decl = build_decl (CONST_DECL, name, type);
e8bd800e
MM
12616
12617 DECL_CONTEXT (decl) = FROB_CONTEXT (context);
12618 DECL_INITIAL (decl) = value;
12619 TREE_READONLY (decl) = 1;
12620
12621 if (context && context == current_class_type)
8f032717
MM
12622 /* In something like `struct S { enum E { i = 7 }; };' we put `i'
12623 on the TYPE_FIELDS list for `S'. (That's so that you can say
12624 things like `S::i' later.) */
12625 finish_member_declaration (decl);
e8bd800e
MM
12626 else
12627 {
12628 pushdecl (decl);
12629 GNU_xref_decl (current_function_decl, decl);
12630 }
8d08fdba 12631
5156628f 12632 if (! processing_template_decl)
5566b478
MS
12633 {
12634 /* Set basis for default for next value. */
12635 enum_next_value = build_binary_op_nodefault (PLUS_EXPR, value,
12636 integer_one_node, PLUS_EXPR);
12637 enum_overflow = tree_int_cst_lt (enum_next_value, value);
5566b478 12638 }
8d08fdba 12639
e1b3e07d 12640 result = tree_cons (name, decl, NULL_TREE);
8d08fdba
MS
12641 return result;
12642}
12643
8d08fdba 12644\f
5566b478
MS
12645static int function_depth;
12646
a8f73d4b
MM
12647/* We're defining DECL. Make sure that it's type is OK. */
12648
12649static void
12650check_function_type (decl)
12651 tree decl;
12652{
12653 tree fntype = TREE_TYPE (decl);
12654
12655 /* In a function definition, arg types must be complete. */
12656 require_complete_types_for_parms (current_function_parms);
12657
12658 if (TYPE_SIZE (complete_type (TREE_TYPE (fntype))) == NULL_TREE)
12659 {
12660 cp_error ("return type `%#T' is incomplete", TREE_TYPE (fntype));
12661
12662 /* Make it return void instead, but don't change the
12663 type of the DECL_RESULT, in case we have a named return value. */
12664 if (TREE_CODE (fntype) == METHOD_TYPE)
12665 {
12666 tree ctype = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (fntype)));
12667 TREE_TYPE (decl)
12668 = build_cplus_method_type (ctype,
12669 void_type_node,
12670 FUNCTION_ARG_CHAIN (decl));
12671 }
12672 else
12673 TREE_TYPE (decl)
12674 = build_function_type (void_type_node,
12675 TYPE_ARG_TYPES (TREE_TYPE (decl)));
12676 TREE_TYPE (decl)
12677 = build_exception_variant (fntype,
12678 TYPE_RAISES_EXCEPTIONS (fntype));
12679 }
12680 else
12681 abstract_virtuals_error (decl, TREE_TYPE (fntype));
12682}
12683
8d08fdba
MS
12684/* Create the FUNCTION_DECL for a function definition.
12685 DECLSPECS and DECLARATOR are the parts of the declaration;
12686 they describe the function's name and the type it returns,
12687 but twisted together in a fashion that parallels the syntax of C.
12688
a8f73d4b
MM
12689 FLAGS is a bitwise or of SF_PRE_PARSED (indicating that the
12690 DECLARATOR is really the DECL for the function we are about to
12691 process and that DECLSPECS should be ignored), SF_INCLASS_INLINE
12692 indicating that the function is an inline defined in-class, and
12693 SF_EXPAND indicating that we should generate RTL for this
12694 function.
f181d4ae 12695
8d08fdba
MS
12696 This function creates a binding context for the function body
12697 as well as setting up the FUNCTION_DECL in current_function_decl.
12698
12699 Returns 1 on success. If the DECLARATOR is not suitable for a function
12700 (it defines a datum instead), we return 0, which tells
12701 yyparse to report a parse error.
12702
12703 For C++, we must first check whether that datum makes any sense.
12704 For example, "class A local_a(1,2);" means that variable local_a
12705 is an aggregate of type A, which should have a constructor
87e3dbc9 12706 applied to it with the argument list [1, 2]. */
8d08fdba
MS
12707
12708int
a8f73d4b 12709start_function (declspecs, declarator, attrs, flags)
c11b6f21 12710 tree declspecs, declarator, attrs;
a8f73d4b 12711 int flags;
8d08fdba 12712{
5566b478 12713 tree decl1;
8d08fdba
MS
12714 tree ctype = NULL_TREE;
12715 tree fntype;
12716 tree restype;
12717 extern int have_extern_spec;
12718 extern int used_extern_spec;
12719 int doing_friend = 0;
a8f73d4b 12720 struct binding_level *bl;
8d08fdba 12721
8d08fdba 12722 /* Sanity check. */
a1774733 12723 my_friendly_assert (TREE_CODE (TREE_VALUE (void_list_node)) == VOID_TYPE, 160);
8d08fdba
MS
12724 my_friendly_assert (TREE_CHAIN (void_list_node) == NULL_TREE, 161);
12725
e92cc029 12726 /* This should only be done once on the top most decl. */
8d08fdba
MS
12727 if (have_extern_spec && !used_extern_spec)
12728 {
12729 declspecs = decl_tree_cons (NULL_TREE, get_identifier ("extern"), declspecs);
12730 used_extern_spec = 1;
12731 }
12732
a8f73d4b 12733 if (flags & SF_PRE_PARSED)
8d08fdba
MS
12734 {
12735 decl1 = declarator;
12736
8d08fdba
MS
12737 fntype = TREE_TYPE (decl1);
12738 if (TREE_CODE (fntype) == METHOD_TYPE)
12739 ctype = TYPE_METHOD_BASETYPE (fntype);
12740
12741 /* ANSI C++ June 5 1992 WP 11.4.5. A friend function defined in a
12742 class is in the (lexical) scope of the class in which it is
12743 defined. */
12744 if (!ctype && DECL_FRIEND_P (decl1))
12745 {
12746 ctype = DECL_CLASS_CONTEXT (decl1);
12747
12748 /* CTYPE could be null here if we're dealing with a template;
12749 for example, `inline friend float foo()' inside a template
12750 will have no CTYPE set. */
12751 if (ctype && TREE_CODE (ctype) != RECORD_TYPE)
12752 ctype = NULL_TREE;
12753 else
12754 doing_friend = 1;
12755 }
12756
5566b478
MS
12757 last_function_parms = DECL_ARGUMENTS (decl1);
12758 last_function_parm_tags = NULL_TREE;
8d08fdba
MS
12759 }
12760 else
12761 {
c11b6f21 12762 decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, 1, NULL_TREE);
8d08fdba
MS
12763 /* If the declarator is not suitable for a function definition,
12764 cause a syntax error. */
12765 if (decl1 == NULL_TREE || TREE_CODE (decl1) != FUNCTION_DECL) return 0;
12766
12767 fntype = TREE_TYPE (decl1);
12768
12769 restype = TREE_TYPE (fntype);
7ddedda4 12770 if (CLASS_TYPE_P (restype) && !CLASSTYPE_GOT_SEMICOLON (restype))
8d08fdba 12771 {
8251199e 12772 cp_error ("semicolon missing after declaration of `%#T'", restype);
e66d884e 12773 shadow_tag (build_expr_list (NULL_TREE, restype));
8d08fdba
MS
12774 CLASSTYPE_GOT_SEMICOLON (restype) = 1;
12775 if (TREE_CODE (fntype) == FUNCTION_TYPE)
12776 fntype = build_function_type (integer_type_node,
12777 TYPE_ARG_TYPES (fntype));
12778 else
12779 fntype = build_cplus_method_type (build_type_variant (TYPE_METHOD_BASETYPE (fntype), TREE_READONLY (decl1), TREE_SIDE_EFFECTS (decl1)),
12780 integer_type_node,
12781 TYPE_ARG_TYPES (fntype));
12782 TREE_TYPE (decl1) = fntype;
12783 }
12784
12785 if (TREE_CODE (fntype) == METHOD_TYPE)
12786 ctype = TYPE_METHOD_BASETYPE (fntype);
35680744 12787 else if (DECL_MAIN_P (decl1))
8d08fdba
MS
12788 {
12789 /* If this doesn't return integer_type, complain. */
12790 if (TREE_TYPE (TREE_TYPE (decl1)) != integer_type_node)
12791 {
a28e3c7f 12792 if (pedantic || warn_return_type)
8251199e 12793 pedwarn ("return type for `main' changed to `int'");
8d08fdba
MS
12794 TREE_TYPE (decl1) = fntype = default_function_type;
12795 }
8d08fdba
MS
12796 }
12797 }
b35d4555
MM
12798
12799 /* Sometimes we don't notice that a function is a static member, and
12800 build a METHOD_TYPE for it. Fix that up now. */
12801 if (ctype != NULL_TREE && DECL_STATIC_FUNCTION_P (decl1)
12802 && TREE_CODE (TREE_TYPE (decl1)) == METHOD_TYPE)
12803 {
12804 revert_static_member_fn (&decl1, NULL, NULL);
12805 last_function_parms = TREE_CHAIN (last_function_parms);
12806 ctype = NULL_TREE;
12807 }
8d08fdba
MS
12808
12809 /* Warn if function was previously implicitly declared
12810 (but not if we warned then). */
12811 if (! warn_implicit
12812 && IDENTIFIER_IMPLICIT_DECL (DECL_NAME (decl1)) != NULL_TREE)
8251199e 12813 cp_warning_at ("`%D' implicitly declared before its definition", IDENTIFIER_IMPLICIT_DECL (DECL_NAME (decl1)));
8d08fdba 12814
f181d4ae
MM
12815 /* Set up current_class_type, and enter the scope of the class, if
12816 appropriate. */
12817 if (ctype)
12818 push_nested_class (ctype, 1);
12819 else if (DECL_STATIC_FUNCTION_P (decl1))
12820 push_nested_class (DECL_CONTEXT (decl1), 2);
12821
12822 /* Now that we have entered the scope of the class, we must restore
12823 the bindings for any template parameters surrounding DECL1, if it
12824 is an inline member template. (Order is important; consider the
12825 case where a template parameter has the same name as a field of
12826 the class.) It is not until after this point that
12827 PROCESSING_TEMPLATE_DECL is guaranteed to be set up correctly. */
a8f73d4b 12828 if (flags & SF_INCLASS_INLINE)
f181d4ae
MM
12829 maybe_begin_member_template_processing (decl1);
12830
9188c363
MM
12831 /* Effective C++ rule 15. See also c_expand_return. */
12832 if (warn_ecpp
12833 && DECL_NAME (decl1) == ansi_opname[(int) MODIFY_EXPR]
12834 && TREE_CODE (TREE_TYPE (fntype)) == VOID_TYPE)
12835 cp_warning ("`operator=' should return a reference to `*this'");
12836
12837 /* Make the init_value nonzero so pushdecl knows this is not tentative.
12838 error_mark_node is replaced below (in poplevel) with the BLOCK. */
b35d4555
MM
12839 if (!DECL_INITIAL (decl1))
12840 DECL_INITIAL (decl1) = error_mark_node;
9188c363
MM
12841
12842#ifdef SET_DEFAULT_DECL_ATTRIBUTES
12843 SET_DEFAULT_DECL_ATTRIBUTES (decl1, attrs);
12844#endif
12845
12846 /* This function exists in static storage.
12847 (This does not mean `static' in the C sense!) */
12848 TREE_STATIC (decl1) = 1;
12849
12850 /* We must call push_template_decl after current_class_type is set
12851 up. (If we are processing inline definitions after exiting a
12852 class scope, current_class_type will be NULL_TREE until set above
12853 by push_nested_class.) */
12854 if (processing_template_decl)
12855 decl1 = push_template_decl (decl1);
12856
f181d4ae 12857 /* We are now in the scope of the function being defined. */
8d08fdba 12858 current_function_decl = decl1;
f181d4ae 12859
5566b478
MS
12860 /* Save the parm names or decls from this function's declarator
12861 where store_parm_decls will find them. */
12862 current_function_parms = last_function_parms;
12863 current_function_parm_tags = last_function_parm_tags;
8d08fdba 12864
a8f73d4b
MM
12865 /* Make sure the parameter and return types are reasonable. When
12866 you declare a function, these types can be incomplete, but they
12867 must be complete when you define the function. */
5156628f 12868 if (! processing_template_decl)
a8f73d4b 12869 check_function_type (decl1);
f181d4ae 12870
a8f73d4b
MM
12871 /* Build the return declaration for the function. */
12872 restype = TREE_TYPE (fntype);
12873 if (!processing_template_decl)
12874 {
12875 if (!DECL_RESULT (decl1))
5566b478 12876 {
5566b478 12877 DECL_RESULT (decl1)
a8f73d4b
MM
12878 = build_decl (RESULT_DECL, 0, TYPE_MAIN_VARIANT (restype));
12879 c_apply_type_quals_to_decl (CP_TYPE_QUALS (restype),
12880 DECL_RESULT (decl1));
5566b478 12881 }
5566b478 12882 }
a8f73d4b
MM
12883 else
12884 /* Just use `void'. Nobody will ever look at this anyhow. */
12885 DECL_RESULT (decl1) = build_decl (RESULT_DECL, 0, void_type_node);
12886
12887 /* Initialize RTL machinery. We cannot do this until
12888 CURRENT_FUNCTION_DECL and DECL_RESULT are set up. We do this
12889 even when processing a template; this is how we get
12890 CURRENT_FUNCTION set up, and our per-function variables
12891 initialized. */
12892 bl = current_binding_level;
12893 init_function_start (decl1, input_filename, lineno);
12894 current_binding_level = bl;
12895 expanding_p = (flags & SF_EXPAND) != 0;
12896
12897 /* Even though we're inside a function body, we still don't want to
12898 call expand_expr to calculate the size of a variable-sized array.
12899 We haven't necessarily assigned RTL to all variables yet, so it's
12900 not safe to try to expand expressions involving them. */
12901 immediate_size_expand = 0;
12902 get_pending_sizes ();
12903
12904 /* Let the user know we're compiling this function. */
24bef158 12905 if (processing_template_decl || !building_stmt_tree ())
a8f73d4b 12906 announce_function (decl1);
b7484fbe 12907
878cd289
MS
12908 /* Record the decl so that the function name is defined.
12909 If we already have a decl for this name, and it is a FUNCTION_DECL,
12910 use the old decl. */
a8f73d4b 12911 if (!processing_template_decl && !(flags & SF_PRE_PARSED))
878cd289 12912 {
75650646 12913 /* A specialization is not used to guide overload resolution. */
959d8796
JM
12914 if ((flag_guiding_decls
12915 || !DECL_TEMPLATE_SPECIALIZATION (decl1))
12916 && ! DECL_FUNCTION_MEMBER_P (decl1))
75650646 12917 decl1 = pushdecl (decl1);
2c73f9f5 12918 else
b7698cf0
JM
12919 {
12920 /* We need to set the DECL_CONTEXT. */
12921 if (!DECL_CONTEXT (decl1) && DECL_TEMPLATE_INFO (decl1))
12922 DECL_CONTEXT (decl1) = DECL_CONTEXT (DECL_TI_TEMPLATE (decl1));
12923 /* And make sure we have enough default args. */
12924 check_default_args (decl1);
12925 }
878cd289
MS
12926 DECL_MAIN_VARIANT (decl1) = decl1;
12927 fntype = TREE_TYPE (decl1);
12928 }
5566b478 12929
a8f73d4b 12930 /* Reset these in case the call to pushdecl changed them. */
5566b478 12931 current_function_decl = decl1;
a8f73d4b 12932 current_function->decl = decl1;
878cd289 12933
b35d4555
MM
12934 /* Initialize the per-function data. */
12935 if (!DECL_PENDING_INLINE_P (decl1) && DECL_SAVED_FUNCTION_DATA (decl1))
12936 {
12937 /* If we already parsed this function, and we're just expanding it
12938 now, restore saved state. */
12939 struct binding_level *bl = current_binding_level;
12940 *cp_function_chain = *DECL_SAVED_FUNCTION_DATA (decl1);
12941 current_binding_level = bl;
12942
b35d4555
MM
12943 /* This function is being processed in whole-function mode; we
12944 already did semantic analysis. */
12945 current_function->x_whole_function_mode_p = 1;
12946
914653a2
MM
12947 /* If we decided that we didn't want to inline this function,
12948 make sure the back-end knows that. */
12949 if (!current_function_cannot_inline)
12950 current_function_cannot_inline = cp_function_chain->cannot_inline;
12951
b35d4555
MM
12952 /* We don't need the saved data anymore. */
12953 free (DECL_SAVED_FUNCTION_DATA (decl1));
12954 DECL_SAVED_FUNCTION_DATA (decl1) = NULL;
12955 }
12956 else if (ctype && !doing_friend && !DECL_STATIC_FUNCTION_P (decl1))
12957 {
12958 /* We know that this was set up by `grokclassfn'. We do not
12959 wait until `store_parm_decls', since evil parse errors may
12960 never get us to that point. Here we keep the consistency
12961 between `current_class_type' and `current_class_ptr'. */
12962 tree t = DECL_ARGUMENTS (decl1);
12963
12964 my_friendly_assert (t != NULL_TREE && TREE_CODE (t) == PARM_DECL,
12965 162);
12966 my_friendly_assert (TREE_CODE (TREE_TYPE (t)) == POINTER_TYPE,
12967 19990811);
12968
12969 cp_function_chain->x_current_class_ref
12970 = build_indirect_ref (t, NULL_PTR);
12971 cp_function_chain->x_current_class_ptr = t;
12972
12973 if (DECL_DESTRUCTOR_P (decl1))
12974 current_in_charge_parm = TREE_CHAIN (t);
12975 }
12976
db5ae43f 12977 if (DECL_INTERFACE_KNOWN (decl1))
faae18ab 12978 {
86052cc3
JM
12979 tree ctx = hack_decl_function_context (decl1);
12980
faae18ab
MS
12981 if (DECL_NOT_REALLY_EXTERN (decl1))
12982 DECL_EXTERNAL (decl1) = 0;
86052cc3
JM
12983
12984 if (ctx != NULL_TREE && DECL_THIS_INLINE (ctx)
12985 && TREE_PUBLIC (ctx))
12986 /* This is a function in a local class in an extern inline
12987 function. */
12988 comdat_linkage (decl1);
faae18ab 12989 }
8d08fdba
MS
12990 /* If this function belongs to an interface, it is public.
12991 If it belongs to someone else's interface, it is also external.
1f901793 12992 This only affects inlines and template instantiations. */
5566b478
MS
12993 else if (interface_unknown == 0
12994 && (! DECL_TEMPLATE_INSTANTIATION (decl1)
d11ad92e 12995 || flag_alt_external_templates))
8d08fdba 12996 {
5566b478 12997 if (DECL_THIS_INLINE (decl1) || DECL_TEMPLATE_INSTANTIATION (decl1)
5156628f 12998 || processing_template_decl)
1f901793
JM
12999 {
13000 DECL_EXTERNAL (decl1)
13001 = (interface_only
9c73ec84
MS
13002 || (DECL_THIS_INLINE (decl1) && ! flag_implement_inlines
13003 && !DECL_VINDEX (decl1)));
1f901793
JM
13004
13005 /* For WIN32 we also want to put these in linkonce sections. */
13006 maybe_make_one_only (decl1);
13007 }
db5ae43f 13008 else
893de33c 13009 DECL_EXTERNAL (decl1) = 0;
e8abc66f 13010 DECL_NOT_REALLY_EXTERN (decl1) = 0;
db5ae43f 13011 DECL_INTERFACE_KNOWN (decl1) = 1;
8d08fdba 13012 }
c16c47fb
JM
13013 else if (interface_unknown && interface_only
13014 && (! DECL_TEMPLATE_INSTANTIATION (decl1)
13015 || flag_alt_external_templates))
13016 {
13017 /* If MULTIPLE_SYMBOL_SPACES is defined and we saw a #pragma
13018 interface, we will have interface_only set but not
13019 interface_known. In that case, we don't want to use the normal
13020 heuristics because someone will supply a #pragma implementation
13021 elsewhere, and deducing it here would produce a conflict. */
13022 comdat_linkage (decl1);
13023 DECL_EXTERNAL (decl1) = 0;
13024 DECL_INTERFACE_KNOWN (decl1) = 1;
13025 DECL_DEFER_OUTPUT (decl1) = 1;
13026 }
8d08fdba 13027 else
a0a33927
MS
13028 {
13029 /* This is a definition, not a reference.
b7484fbe
MS
13030 So clear DECL_EXTERNAL. */
13031 DECL_EXTERNAL (decl1) = 0;
faae18ab 13032
5566b478
MS
13033 if ((DECL_THIS_INLINE (decl1) || DECL_TEMPLATE_INSTANTIATION (decl1))
13034 && ! DECL_INTERFACE_KNOWN (decl1)
13035 /* Don't try to defer nested functions for now. */
e76a2646 13036 && ! hack_decl_function_context (decl1))
878cd289
MS
13037 DECL_DEFER_OUTPUT (decl1) = 1;
13038 else
893de33c 13039 DECL_INTERFACE_KNOWN (decl1) = 1;
db5ae43f 13040 }
a9aedbc2 13041
b35d4555 13042 if (doing_semantic_analysis_p ())
8d08fdba 13043 {
b35d4555
MM
13044 pushlevel (0);
13045 current_binding_level->parm_flag = 1;
8d08fdba 13046 }
8d08fdba 13047
f30432d7
MS
13048 if (attrs)
13049 cplus_decl_attributes (decl1, NULL_TREE, attrs);
3a846e6e 13050
558475f0
MM
13051 if (!building_stmt_tree ())
13052 {
13053 GNU_xref_function (decl1, current_function_parms);
13054 make_function_rtl (decl1);
13055 }
db5ae43f 13056
8d08fdba
MS
13057 /* Promote the value to int before returning it. */
13058 if (C_PROMOTING_INTEGER_TYPE_P (restype))
5566b478
MS
13059 restype = type_promotes_to (restype);
13060
13061 /* If this fcn was already referenced via a block-scope `extern' decl
13062 (or an implicit decl), propagate certain information about the usage. */
13063 if (TREE_ADDRESSABLE (DECL_ASSEMBLER_NAME (decl1)))
13064 TREE_ADDRESSABLE (decl1) = 1;
13065
8d08fdba 13066 if (DECL_RESULT (decl1) == NULL_TREE)
f30432d7
MS
13067 {
13068 DECL_RESULT (decl1)
13069 = build_decl (RESULT_DECL, 0, TYPE_MAIN_VARIANT (restype));
91063b51
MM
13070 TREE_READONLY (DECL_RESULT (decl1)) = CP_TYPE_CONST_P (restype);
13071 TREE_THIS_VOLATILE (DECL_RESULT (decl1)) = CP_TYPE_VOLATILE_P (restype);
f30432d7 13072 }
8d08fdba 13073
948edfa7
JW
13074 /* Allocate further tree nodes temporarily during compilation
13075 of this function only. Tiemann moved up here from bottom of fn. */
13076 /* If this is a nested function, then we must continue to allocate RTL
13077 on the permanent obstack in case we need to inline it later. */
13078 if (! hack_decl_function_context (decl1))
5566b478 13079 temporary_allocation ();
f1dedc31
MM
13080
13081 /* Make sure that we always have a momntary obstack while we're in a
13082 function body. */
13083 push_momentary ();
5566b478 13084
558475f0
MM
13085 if (building_stmt_tree ())
13086 begin_stmt_tree (decl1);
5566b478
MS
13087
13088 ++function_depth;
13089
f30432d7
MS
13090 if (DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (decl1))
13091 && DECL_LANGUAGE (decl1) == lang_cplusplus)
b35d4555
MM
13092 dtor_label = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE);
13093 else if (DECL_CONSTRUCTOR_P (decl1))
13094 ctor_label = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE);
8d08fdba 13095
8d08fdba
MS
13096 return 1;
13097}
13098\f
9664b89e
JM
13099/* Called after store_parm_decls for a function-try-block. We need to update
13100 last_parm_cleanup_insn so that the base initializers for a constructor
13101 are run within this block, not before it. */
6467930b
MS
13102
13103void
13104expand_start_early_try_stmts ()
13105{
6467930b 13106 expand_start_try_stmts ();
9664b89e 13107 last_parm_cleanup_insn = get_last_insn ();
f30432d7
MS
13108}
13109
8d08fdba
MS
13110/* Store the parameter declarations into the current function declaration.
13111 This is called after parsing the parameter declarations, before
13112 digesting the body of the function.
13113
13114 Also install to binding contour return value identifier, if any. */
13115
13116void
13117store_parm_decls ()
13118{
13119 register tree fndecl = current_function_decl;
13120 register tree parm;
13121 int parms_have_cleanups = 0;
eb66be0e 13122 tree cleanups = NULL_TREE;
8d08fdba 13123
8d08fdba
MS
13124 /* This is a list of types declared among parms in a prototype. */
13125 tree parmtags = current_function_parm_tags;
13126
13127 /* This is a chain of any other decls that came in among the parm
13128 declarations. If a parm is declared with enum {foo, bar} x;
13129 then CONST_DECLs for foo and bar are put here. */
13130 tree nonparms = NULL_TREE;
13131
8d08fdba 13132 /* Create a binding level for the parms. */
b35d4555
MM
13133 if (!building_stmt_tree ())
13134 expand_start_bindings (2);
8d08fdba 13135
b35d4555 13136 if (current_function_parms)
8d08fdba
MS
13137 {
13138 /* This case is when the function was defined with an ANSI prototype.
13139 The parms already have decls, so we need not do anything here
13140 except record them as in effect
13141 and complain if any redundant old-style parm decls were written. */
13142
b35d4555
MM
13143 tree specparms = current_function_parms;
13144 tree next;
13145
13146 if (doing_semantic_analysis_p ())
13147 {
13148 /* Must clear this because it might contain TYPE_DECLs declared
13149 at class level. */
13150 storedecls (NULL_TREE);
8d08fdba 13151
b35d4555
MM
13152 /* If we're doing semantic analysis, then we'll call pushdecl
13153 for each of these. We must do them in reverse order so that
13154 they end in the correct forward order. */
13155 specparms = nreverse (specparms);
13156 }
5566b478 13157
b35d4555 13158 for (parm = specparms; parm; parm = next)
8d08fdba
MS
13159 {
13160 next = TREE_CHAIN (parm);
13161 if (TREE_CODE (parm) == PARM_DECL)
13162 {
5566b478 13163 tree cleanup;
b35d4555
MM
13164
13165 if (doing_semantic_analysis_p ())
8d08fdba 13166 {
b35d4555
MM
13167 if (DECL_NAME (parm) == NULL_TREE
13168 || TREE_CODE (TREE_TYPE (parm)) != VOID_TYPE)
13169 pushdecl (parm);
13170 else
13171 cp_error ("parameter `%D' declared void", parm);
8d08fdba 13172 }
b35d4555 13173
558475f0 13174 if (! building_stmt_tree ()
5566b478 13175 && (cleanup = maybe_build_cleanup (parm), cleanup))
8d08fdba
MS
13176 {
13177 expand_decl (parm);
8d08fdba 13178 parms_have_cleanups = 1;
eb66be0e
MS
13179
13180 /* Keep track of the cleanups. */
13181 cleanups = tree_cons (parm, cleanup, cleanups);
8d08fdba
MS
13182 }
13183 }
13184 else
13185 {
13186 /* If we find an enum constant or a type tag,
13187 put it aside for the moment. */
13188 TREE_CHAIN (parm) = NULL_TREE;
13189 nonparms = chainon (nonparms, parm);
13190 }
13191 }
13192
b35d4555
MM
13193 if (doing_semantic_analysis_p ())
13194 {
13195 /* Get the decls in their original chain order
13196 and record in the function. This is all and only the
13197 PARM_DECLs that were pushed into scope by the loop above. */
13198 DECL_ARGUMENTS (fndecl) = getdecls ();
13199 storetags (chainon (parmtags, gettags ()));
8d08fdba 13200
b35d4555
MM
13201 /* We built up the cleanups in reversed order. */
13202 cleanups = nreverse (cleanups);
13203 }
8d08fdba
MS
13204 }
13205 else
13206 DECL_ARGUMENTS (fndecl) = NULL_TREE;
13207
13208 /* Now store the final chain of decls for the arguments
13209 as the decl-chain of the current lexical scope.
13210 Put the enumerators in as well, at the front so that
13211 DECL_ARGUMENTS is not modified. */
b35d4555
MM
13212 if (doing_semantic_analysis_p ())
13213 storedecls (chainon (nonparms, DECL_ARGUMENTS (fndecl)));
8d08fdba
MS
13214
13215 /* Initialize the RTL code for the function. */
49ad7cfa 13216 DECL_SAVED_INSNS (fndecl) = 0;
558475f0 13217 if (! building_stmt_tree ())
5566b478 13218 expand_function_start (fndecl, parms_have_cleanups);
8d08fdba 13219
eb448459
MS
13220 current_function_parms_stored = 1;
13221
13222 /* If this function is `main', emit a call to `__main'
13223 to run global initializers, etc. */
558475f0 13224 if (DECL_MAIN_P (fndecl) && !building_stmt_tree ())
35680744 13225 expand_main_function ();
eb448459 13226
eb66be0e
MS
13227 /* Now that we have initialized the parms, we can start their
13228 cleanups. We cannot do this before, since expand_decl_cleanup
13229 should not be called before the parm can be used. */
558475f0 13230 if (cleanups && !building_stmt_tree ())
b35d4555
MM
13231 while (cleanups)
13232 {
13233 if (! expand_decl_cleanup (TREE_PURPOSE (cleanups),
13234 TREE_VALUE (cleanups)))
13235 cp_error ("parser lost in parsing declaration of `%D'",
13236 TREE_PURPOSE (cleanups));
13237
13238 cleanups = TREE_CHAIN (cleanups);
13239 }
eb66be0e 13240
8d08fdba
MS
13241 /* Create a binding contour which can be used to catch
13242 cleanup-generated temporaries. Also, if the return value needs or
13243 has initialization, deal with that now. */
13244 if (parms_have_cleanups)
13245 {
13246 pushlevel (0);
558475f0 13247 if (!building_stmt_tree ())
b35d4555 13248 expand_start_bindings (2);
8d08fdba
MS
13249 }
13250
b35d4555
MM
13251 /* Do the starting of the exception specifications, if we have any. */
13252 if (flag_exceptions && !processing_template_decl
13253 && building_stmt_tree ()
13254 && TYPE_RAISES_EXCEPTIONS (TREE_TYPE (current_function_decl)))
13255 current_eh_spec_try_block = expand_start_eh_spec ();
eb448459 13256
5c825fc2 13257 last_parm_cleanup_insn = get_last_insn ();
72b7eeff 13258 last_dtor_insn = get_last_insn ();
8d08fdba
MS
13259}
13260
13261/* Bind a name and initialization to the return value of
13262 the current function. */
e92cc029 13263
8d08fdba 13264void
558475f0
MM
13265store_return_init (decl)
13266 tree decl;
8d08fdba 13267{
558475f0
MM
13268 /* If this named return value comes in a register, put it in a
13269 pseudo-register. */
13270 if (DECL_REGISTER (decl))
8d08fdba 13271 {
558475f0
MM
13272 original_result_rtx = DECL_RTL (decl);
13273 DECL_RTL (decl) = gen_reg_rtx (DECL_MODE (decl));
8d08fdba
MS
13274 }
13275}
13276
8d08fdba 13277\f
59026e79
MM
13278/* We have finished doing semantic analysis on DECL, but have not yet
13279 generated RTL for its body. Save away our current state, so that
13280 when we want to generate RTL later we know what to do. */
13281
13282static void
13283save_function_data (decl)
13284 tree decl;
13285{
13286 struct language_function *f;
13287
13288 /* Save the language-specific per-function data so that we can
13289 get it back when we really expand this function. */
13290 my_friendly_assert (!DECL_PENDING_INLINE_P (decl),
13291 19990908);
13292
13293 /* Make a copy. */
13294 f = ((struct language_function *)
13295 xmalloc (sizeof (struct language_function)));
13296 bcopy ((char *) cp_function_chain, (char *) f,
13297 sizeof (struct language_function));
13298 DECL_SAVED_FUNCTION_DATA (decl) = f;
13299
13300 /* Clear out the bits we don't need. */
13301 f->x_base_init_list = NULL_TREE;
13302 f->x_member_init_list = NULL_TREE;
13303 f->x_last_tree = NULL_TREE;
13304 f->x_last_expr_type = NULL_TREE;
13305 f->x_last_dtor_insn = NULL_RTX;
13306 f->x_last_parm_cleanup_insn = NULL_RTX;
13307 f->x_result_rtx = NULL_RTX;
13308 f->x_named_label_uses = NULL;
13309 f->bindings = NULL;
13310
13311 /* When we get back here again, we will be expanding. */
13312 f->x_expanding_p = 1;
914653a2
MM
13313
13314 /* If we've already decided that we cannot inline this function, we
13315 must remember that fact when we actually go to expand the
13316 function. */
13317 f->cannot_inline = current_function_cannot_inline;
59026e79
MM
13318}
13319
9bfadf57
MM
13320/* At the end of every destructor we generate code to restore virtual
13321 function tables to the values desired by base classes and to call
13322 to base class destructors. Do that now, for DECL. */
13323
13324static void
13325finish_destructor_body ()
13326{
13327 tree compound_stmt;
13328 tree in_charge;
13329 tree virtual_size;
13330 tree exprstmt;
13331
13332 /* Create a block to contain all the extra code. */
13333 compound_stmt = begin_compound_stmt (/*has_no_scope=*/0);
13334
13335 /* Generate the code to call destructor on base class. If this
13336 destructor belongs to a class with virtual functions, then set
13337 the virtual function table pointer to represent the type of our
13338 base class. */
13339
13340 /* This side-effect makes call to `build_delete' generate the code
13341 we have to have at the end of this destructor. `build_delete'
13342 will set the flag again. */
13343 TYPE_HAS_DESTRUCTOR (current_class_type) = 0;
13344
13345 /* These are two cases where we cannot delegate deletion. */
13346 if (TYPE_USES_VIRTUAL_BASECLASSES (current_class_type)
13347 || TYPE_GETS_REG_DELETE (current_class_type))
13348 in_charge = integer_zero_node;
13349 else
13350 in_charge = current_in_charge_parm;
13351
13352 exprstmt = build_delete (current_class_type,
13353 current_class_ref,
13354 in_charge,
13355 LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR|LOOKUP_NORMAL,
13356 0);
13357
13358 if (exprstmt != error_mark_node
13359 && (TREE_CODE (exprstmt) != NOP_EXPR
13360 || TREE_OPERAND (exprstmt, 0) != integer_zero_node
13361 || TYPE_USES_VIRTUAL_BASECLASSES (current_class_type)))
13362 {
13363 add_tree (build_min_nt (LABEL_STMT, dtor_label));
13364 if (exprstmt != void_zero_node)
13365 /* Don't call `expand_expr_stmt' if we're not going to do
13366 anything, since -Wall will give a diagnostic. */
13367 finish_expr_stmt (exprstmt);
13368
13369 /* Run destructor on all virtual baseclasses. */
13370 if (TYPE_USES_VIRTUAL_BASECLASSES (current_class_type))
13371 {
13372 tree vbases = nreverse (copy_list (CLASSTYPE_VBASECLASSES (current_class_type)));
13373 tree if_stmt = begin_if_stmt ();
13374 finish_if_stmt_cond (build (BIT_AND_EXPR, integer_type_node,
13375 current_in_charge_parm,
13376 integer_two_node),
13377 if_stmt);
13378
13379 while (vbases)
13380 {
13381 if (TYPE_NEEDS_DESTRUCTOR (BINFO_TYPE (vbases)))
13382 {
13383 tree vb = get_vbase
13384 (BINFO_TYPE (vbases),
13385 TYPE_BINFO (current_class_type));
13386 finish_expr_stmt
13387 (build_scoped_method_call
13388 (current_class_ref, vb, dtor_identifier,
13389 build_expr_list (NULL_TREE, integer_zero_node)));
13390 }
13391 vbases = TREE_CHAIN (vbases);
13392 }
13393
13394 finish_then_clause (if_stmt);
13395 finish_if_stmt ();
13396 }
13397 }
13398
13399 virtual_size = c_sizeof (current_class_type);
13400
13401 /* At the end, call delete if that's what's requested. */
13402
13403 /* FDIS sez: At the point of definition of a virtual destructor
13404 (including an implicit definition), non-placement operator delete
13405 shall be looked up in the scope of the destructor's class and if
13406 found shall be accessible and unambiguous.
13407
13408 This is somewhat unclear, but I take it to mean that if the class
13409 only defines placement deletes we don't do anything here. So we
13410 pass LOOKUP_SPECULATIVELY; delete_sanity will complain for us if
13411 they ever try to delete one of these. */
13412 if (TYPE_GETS_REG_DELETE (current_class_type)
13413 || TYPE_USES_VIRTUAL_BASECLASSES (current_class_type))
13414 {
13415 tree if_stmt;
13416
13417 exprstmt = build_op_delete_call
13418 (DELETE_EXPR, current_class_ptr, virtual_size,
13419 LOOKUP_NORMAL | LOOKUP_SPECULATIVELY, NULL_TREE);
13420
13421 if_stmt = begin_if_stmt ();
13422 finish_if_stmt_cond (build (BIT_AND_EXPR, integer_type_node,
13423 current_in_charge_parm,
13424 integer_one_node),
13425 if_stmt);
13426 finish_expr_stmt (exprstmt);
13427 finish_then_clause (if_stmt);
13428 finish_if_stmt ();
13429 }
13430
13431 /* Close the block we started above. */
13432 finish_compound_stmt (/*has_no_scope=*/0, compound_stmt);
13433}
13434
8d08fdba
MS
13435/* Finish up a function declaration and compile that function
13436 all the way to assembler language output. The free the storage
13437 for the function definition.
13438
13439 This is called after parsing the body of the function definition.
13440 LINENO is the current line number.
13441
f181d4ae
MM
13442 FLAGS is a bitwise or of the following values:
13443 1 - CALL_POPLEVEL
13444 An extra call to poplevel (and expand_end_bindings) must be
13445 made to take care of the binding contour for the base
13446 initializers. This is only relevant for constructors.
13447 2 - INCLASS_INLINE
13448 We just finished processing the body of an in-class inline
13449 function definition. (This processing will have taken place
87e3dbc9 13450 after the class definition is complete.) */
8d08fdba 13451
4d6abc1c 13452tree
87e3dbc9 13453finish_function (lineno, flags)
8d08fdba 13454 int lineno;
f181d4ae 13455 int flags;
8d08fdba
MS
13456{
13457 register tree fndecl = current_function_decl;
13458 tree fntype, ctype = NULL_TREE;
8d08fdba
MS
13459 /* Label to use if this function is supposed to return a value. */
13460 tree no_return_label = NULL_TREE;
f181d4ae
MM
13461 int call_poplevel = (flags & 1) != 0;
13462 int inclass_inline = (flags & 2) != 0;
558475f0 13463 int expand_p;
87e3dbc9 13464 int nested;
8d08fdba
MS
13465
13466 /* When we get some parse errors, we can end up without a
13467 current_function_decl, so cope. */
13468 if (fndecl == NULL_TREE)
4d6abc1c 13469 return error_mark_node;
8d08fdba 13470
87e3dbc9 13471 nested = function_depth > 1;
8d08fdba
MS
13472 fntype = TREE_TYPE (fndecl);
13473
9bfadf57
MM
13474 /* TREE_READONLY (fndecl) = 1;
13475 This caused &foo to be of type ptr-to-const-function
13476 which then got a warning when stored in a ptr-to-function variable. */
8d08fdba
MS
13477
13478 /* This happens on strange parse errors. */
13479 if (! current_function_parms_stored)
13480 {
13481 call_poplevel = 0;
13482 store_parm_decls ();
13483 }
13484
558475f0 13485 if (building_stmt_tree ())
8d08fdba 13486 {
5566b478 13487 if (DECL_CONSTRUCTOR_P (fndecl) && call_poplevel)
b35d4555 13488 do_poplevel ();
9bfadf57
MM
13489 else if (DECL_DESTRUCTOR_P (fndecl) && !processing_template_decl)
13490 finish_destructor_body ();
87e3dbc9 13491
b35d4555
MM
13492 /* Finish dealing with exception specifiers. */
13493 if (flag_exceptions && !processing_template_decl
13494 && TYPE_RAISES_EXCEPTIONS (TREE_TYPE (current_function_decl)))
13495 expand_end_eh_spec (TYPE_RAISES_EXCEPTIONS
13496 (TREE_TYPE (current_function_decl)),
13497 current_eh_spec_try_block);
5566b478
MS
13498 }
13499 else
13500 {
13501 if (write_symbols != NO_DEBUG /*&& TREE_CODE (fntype) != METHOD_TYPE*/)
8d08fdba 13502 {
5566b478
MS
13503 tree ttype = target_type (fntype);
13504 tree parmdecl;
13505
8d08fdba
MS
13506 if (IS_AGGR_TYPE (ttype))
13507 /* Let debugger know it should output info for this type. */
13508 note_debug_info_needed (ttype);
5566b478
MS
13509
13510 for (parmdecl = DECL_ARGUMENTS (fndecl); parmdecl; parmdecl = TREE_CHAIN (parmdecl))
13511 {
13512 ttype = target_type (TREE_TYPE (parmdecl));
13513 if (IS_AGGR_TYPE (ttype))
13514 /* Let debugger know it should output info for this type. */
13515 note_debug_info_needed (ttype);
13516 }
8d08fdba 13517 }
8d08fdba 13518
5566b478
MS
13519 /* Clean house because we will need to reorder insns here. */
13520 do_pending_stack_adjust ();
8d08fdba 13521
5566b478 13522 if (dtor_label)
9bfadf57 13523 ;
b35d4555 13524 else if (DECL_CONSTRUCTOR_P (fndecl))
8d08fdba 13525 {
b35d4555
MM
13526 /* This is where the body of the constructor begins. All
13527 subobjects have been fully constructed at this point. */
b7484fbe 13528 end_protect_partials ();
5566b478
MS
13529
13530 /* This is where the body of the constructor ends. */
8d08fdba
MS
13531 expand_label (ctor_label);
13532 ctor_label = NULL_TREE;
13533
13534 if (call_poplevel)
b35d4555 13535 do_poplevel ();
8d08fdba 13536
1c2c08a5
JM
13537 /* c_expand_return knows to return 'this' from a constructor. */
13538 c_expand_return (NULL_TREE);
5566b478 13539 }
35680744 13540 else if (DECL_MAIN_P (fndecl))
8d08fdba 13541 {
5566b478
MS
13542 /* Make it so that `main' always returns 0 by default. */
13543#ifdef VMS
13544 c_expand_return (integer_one_node);
13545#else
13546 c_expand_return (integer_zero_node);
13547#endif
8d08fdba 13548 }
5566b478 13549 else if (return_label != NULL_RTX
9bfadf57
MM
13550 && flag_this_is_variable <= 0
13551 && current_function_return_value == NULL_TREE
13552 && ! DECL_NAME (DECL_RESULT (current_function_decl)))
5566b478 13553 no_return_label = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE);
8d08fdba 13554
eb448459
MS
13555 if (flag_exceptions)
13556 expand_exception_blocks ();
13557
5566b478
MS
13558 /* If this function is supposed to return a value, ensure that
13559 we do not fall into the cleanups by mistake. The end of our
13560 function will look like this:
13561
13562 user code (may have return stmt somewhere)
13563 goto no_return_label
13564 cleanup_label:
13565 cleanups
13566 goto return_label
13567 no_return_label:
13568 NOTE_INSN_FUNCTION_END
13569 return_label:
13570 things for return
13571
13572 If the user omits a return stmt in the USER CODE section, we
13573 will have a control path which reaches NOTE_INSN_FUNCTION_END.
13574 Otherwise, we won't. */
13575 if (no_return_label)
8d08fdba 13576 {
5566b478
MS
13577 DECL_CONTEXT (no_return_label) = fndecl;
13578 DECL_INITIAL (no_return_label) = error_mark_node;
13579 DECL_SOURCE_FILE (no_return_label) = input_filename;
13580 DECL_SOURCE_LINE (no_return_label) = lineno;
13581 expand_goto (no_return_label);
8d08fdba
MS
13582 }
13583
5566b478 13584 if (cleanup_label)
a9aedbc2 13585 {
2c73f9f5 13586 /* Remove the binding contour which is used
5566b478
MS
13587 to catch cleanup-generated temporaries. */
13588 expand_end_bindings (0, 0, 0);
13589 poplevel (0, 0, 0);
8d08fdba 13590
eb448459
MS
13591 /* Emit label at beginning of cleanup code for parameters. */
13592 emit_label (cleanup_label);
13593 }
b7484fbe 13594
a8f73d4b
MM
13595 /* Get return value into register if that's where it's supposed
13596 to be. */
5566b478
MS
13597 if (original_result_rtx)
13598 fixup_result_decl (DECL_RESULT (fndecl), original_result_rtx);
8d08fdba 13599
5566b478
MS
13600 /* Finish building code that will trigger warnings if users forget
13601 to make their functions return values. */
13602 if (no_return_label || cleanup_label)
13603 emit_jump (return_label);
13604 if (no_return_label)
8d08fdba 13605 {
5566b478
MS
13606 /* We don't need to call `expand_*_return' here because we
13607 don't need any cleanups here--this path of code is only
13608 for error checking purposes. */
13609 expand_label (no_return_label);
8d08fdba
MS
13610 }
13611
078a76c8
MM
13612 /* We hard-wired immediate_size_expand to zero in
13613 start_function. Expand_function_end will decrement this
13614 variable. So, we set the variable to one here, so that after
13615 the decrement it will remain zero. */
4ca7ba86 13616 immediate_size_expand = 1;
078a76c8 13617
5566b478
MS
13618 /* Generate rtl for function exit. */
13619 expand_function_end (input_filename, lineno, 1);
8d08fdba 13620 }
558475f0
MM
13621
13622 /* We have to save this value here in case
13623 maybe_end_member_template_processing decides to pop all the
13624 template parameters. */
13625 expand_p = !building_stmt_tree ();
f181d4ae 13626
558475f0
MM
13627 /* If we're saving up tree structure, tie off the function now. */
13628 if (!expand_p)
13629 finish_stmt_tree (fndecl);
8d2733ca 13630
8d08fdba
MS
13631 /* This must come after expand_function_end because cleanups might
13632 have declarations (from inline functions) that need to go into
13633 this function's blocks. */
b35d4555
MM
13634 if (doing_semantic_analysis_p ())
13635 {
13636 if (current_binding_level->parm_flag != 1)
13637 my_friendly_abort (122);
13638 poplevel (1, 0, 1);
13639 }
8d08fdba 13640
a8f73d4b 13641 /* Remember that we were in class scope. */
db5ae43f 13642 if (current_class_name)
a8f73d4b 13643 ctype = current_class_type;
db5ae43f 13644
1caa11d3
MM
13645 /* Must mark the RESULT_DECL as being in this function. */
13646 DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
13647
13648 /* Set the BLOCK_SUPERCONTEXT of the outermost function scope to point
13649 to the FUNCTION_DECL node itself. */
13650 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
13651
f1dedc31
MM
13652 /* Undo the call to push_momentary in start_function. */
13653 pop_momentary ();
13654
59026e79
MM
13655 /* Save away current state, if appropriate. */
13656 if (!expanding_p && !processing_template_decl)
13657 save_function_data (fndecl);
13658
558475f0 13659 if (expand_p)
5566b478 13660 {
a8f73d4b
MM
13661 int returns_null;
13662 int returns_value;
75650646
MM
13663 int saved_flag_keep_inline_functions =
13664 flag_keep_inline_functions;
13665
5566b478
MS
13666 /* So we can tell if jump_optimize sets it to 1. */
13667 can_reach_end = 0;
8d08fdba 13668
75650646 13669 if (DECL_CONTEXT (fndecl) != NULL_TREE
e1467ff2 13670 && hack_decl_function_context (fndecl))
75650646
MM
13671 /* Trick rest_of_compilation into not deferring output of this
13672 function, even if it is inline, since the rtl_obstack for
13673 this function is the function_obstack of the enclosing
13674 function and will be deallocated when the enclosing
13675 function is gone. See save_tree_status. */
13676 flag_keep_inline_functions = 1;
13677
a8f73d4b
MM
13678 /* Before we call rest_of_compilation (which will pop the
13679 CURRENT_FUNCTION), we must save these values. */
13680 returns_null = current_function_returns_null;
13681 returns_value = current_function_returns_value;
13682
87e3dbc9
MM
13683 /* If this is a nested function (like a template instantiation
13684 that we're compiling in the midst of compiling something
13685 else), push a new GC context. That will keep local variables
13686 on the stack from being collected while we're doing the
13687 compilation of this function. */
13688 if (function_depth > 1)
13689 ggc_push_context ();
13690
6633d636
MS
13691 /* Run the optimizers and output the assembler code for this
13692 function. */
6633d636
MS
13693 if (DECL_ARTIFICIAL (fndecl))
13694 {
13695 /* Do we really *want* to inline this synthesized method? */
13696
13697 int save_fif = flag_inline_functions;
13698 flag_inline_functions = 1;
13699
13700 /* Turn off DECL_INLINE for the moment so function_cannot_inline_p
13701 will check our size. */
13702 DECL_INLINE (fndecl) = 0;
13703
13704 rest_of_compilation (fndecl);
13705 flag_inline_functions = save_fif;
13706 }
13707 else
13708 rest_of_compilation (fndecl);
8d08fdba 13709
87e3dbc9
MM
13710 /* Undo the call to ggc_push_context above. */
13711 if (function_depth > 1)
13712 ggc_pop_context ();
13713
75650646
MM
13714 flag_keep_inline_functions = saved_flag_keep_inline_functions;
13715
5566b478
MS
13716 if (DECL_SAVED_INSNS (fndecl) && ! TREE_ASM_WRITTEN (fndecl))
13717 {
13718 /* Set DECL_EXTERNAL so that assemble_external will be called as
13719 necessary. We'll clear it again in finish_file. */
13720 if (! DECL_EXTERNAL (fndecl))
13721 DECL_NOT_REALLY_EXTERN (fndecl) = 1;
13722 DECL_EXTERNAL (fndecl) = 1;
13723 mark_inline_for_output (fndecl);
13724 }
8926095f 13725
d2e5ee5c
MS
13726 if (ctype && TREE_ASM_WRITTEN (fndecl))
13727 note_debug_info_needed (ctype);
13728
a8f73d4b 13729 returns_null |= can_reach_end;
8d08fdba 13730
5566b478
MS
13731 /* Since we don't normally go through c_expand_return for constructors,
13732 this normally gets the wrong value.
13733 Also, named return values have their return codes emitted after
13734 NOTE_INSN_FUNCTION_END, confusing jump.c. */
13735 if (DECL_CONSTRUCTOR_P (fndecl)
13736 || DECL_NAME (DECL_RESULT (fndecl)) != NULL_TREE)
a8f73d4b 13737 returns_null = 0;
8d08fdba 13738
a8f73d4b 13739 if (TREE_THIS_VOLATILE (fndecl) && returns_null)
8251199e 13740 cp_warning ("`noreturn' function `%D' does return", fndecl);
5566b478 13741 else if ((warn_return_type || pedantic)
a8f73d4b 13742 && returns_null
a1774733 13743 && TREE_CODE (TREE_TYPE (fntype)) != VOID_TYPE)
5566b478
MS
13744 {
13745 /* If this function returns non-void and control can drop through,
13746 complain. */
8251199e 13747 cp_warning ("control reaches end of non-void function `%D'", fndecl);
5566b478
MS
13748 }
13749 /* With just -W, complain only if function returns both with
13750 and without a value. */
a8f73d4b 13751 else if (extra_warnings && returns_value && returns_null)
8251199e 13752 warning ("this function may return with or without a value");
8d08fdba 13753 }
a8f73d4b
MM
13754 else
13755 {
13756 /* Since we never call rest_of_compilation, we never clear
13757 CURRENT_FUNCTION. Do so explicitly. */
13758 free_after_compilation (current_function);
13759 current_function = NULL;
13760 }
13761
13762 /* If this is a in-class inline definition, we may have to pop the
13763 bindings for the template parameters that we added in
13764 maybe_begin_member_template_processing when start_function was
13765 called. */
13766 if (inclass_inline)
13767 maybe_end_member_template_processing ();
13768
13769 /* Leave the scope of the class. */
13770 if (ctype)
13771 pop_nested_class ();
5566b478
MS
13772
13773 --function_depth;
8d08fdba
MS
13774
13775 /* Free all the tree nodes making up this function. */
13776 /* Switch back to allocating nodes permanently
13777 until we start another function. */
db5ae43f
MS
13778 if (! nested)
13779 permanent_allocation (1);
8d08fdba 13780
b35d4555 13781 if (!DECL_SAVED_INSNS (fndecl) && !DECL_SAVED_FUNCTION_DATA (fndecl))
8d08fdba 13782 {
d2e5ee5c
MS
13783 tree t;
13784
8d08fdba
MS
13785 /* Stop pointing to the local nodes about to be freed. */
13786 /* But DECL_INITIAL must remain nonzero so we know this
13787 was an actual function definition. */
13788 DECL_INITIAL (fndecl) = error_mark_node;
d2e5ee5c 13789 for (t = DECL_ARGUMENTS (fndecl); t; t = TREE_CHAIN (t))
de22184b 13790 DECL_RTL (t) = DECL_INCOMING_RTL (t) = NULL_RTX;
8d08fdba
MS
13791 }
13792
e1cd6e56 13793 if (DECL_STATIC_CONSTRUCTOR (fndecl))
e1b3e07d 13794 static_ctors = tree_cons (NULL_TREE, fndecl, static_ctors);
e1cd6e56 13795 if (DECL_STATIC_DESTRUCTOR (fndecl))
e1b3e07d 13796 static_dtors = tree_cons (NULL_TREE, fndecl, static_dtors);
e1cd6e56 13797
4d6abc1c 13798 /* Clean up. */
28cbf42c
MS
13799 if (! nested)
13800 {
13801 /* Let the error reporting routines know that we're outside a
13802 function. For a nested function, this value is used in
13803 pop_cp_function_context and then reset via pop_function_context. */
13804 current_function_decl = NULL_TREE;
13805 }
4d6abc1c
MM
13806
13807 return fndecl;
8d08fdba
MS
13808}
13809\f
13810/* Create the FUNCTION_DECL for a function definition.
8d08fdba
MS
13811 DECLSPECS and DECLARATOR are the parts of the declaration;
13812 they describe the return type and the name of the function,
13813 but twisted together in a fashion that parallels the syntax of C.
13814
13815 This function creates a binding context for the function body
13816 as well as setting up the FUNCTION_DECL in current_function_decl.
13817
13818 Returns a FUNCTION_DECL on success.
13819
13820 If the DECLARATOR is not suitable for a function (it defines a datum
13821 instead), we return 0, which tells yyparse to report a parse error.
13822
13823 May return void_type_node indicating that this method is actually
13824 a friend. See grokfield for more details.
13825
13826 Came here with a `.pushlevel' .
13827
13828 DO NOT MAKE ANY CHANGES TO THIS CODE WITHOUT MAKING CORRESPONDING
13829 CHANGES TO CODE IN `grokfield'. */
e92cc029 13830
8d08fdba 13831tree
acf82af2
JM
13832start_method (declspecs, declarator, attrlist)
13833 tree declarator, declspecs, attrlist;
8d08fdba 13834{
c11b6f21 13835 tree fndecl = grokdeclarator (declarator, declspecs, MEMFUNCDEF, 0,
acf82af2 13836 attrlist);
8d08fdba
MS
13837
13838 /* Something too ugly to handle. */
13839 if (fndecl == NULL_TREE)
13840 return NULL_TREE;
13841
13842 /* Pass friends other than inline friend functions back. */
a1774733 13843 if (fndecl == void_type_node)
8d08fdba
MS
13844 return fndecl;
13845
13846 if (TREE_CODE (fndecl) != FUNCTION_DECL)
13847 /* Not a function, tell parser to report parse error. */
13848 return NULL_TREE;
13849
8d08fdba
MS
13850 if (DECL_IN_AGGR_P (fndecl))
13851 {
13852 if (IDENTIFIER_ERROR_LOCUS (DECL_ASSEMBLER_NAME (fndecl)) != current_class_type)
13853 {
2c73f9f5
ML
13854 if (DECL_CONTEXT (fndecl)
13855 && TREE_CODE( DECL_CONTEXT (fndecl)) != NAMESPACE_DECL)
8251199e
JM
13856 cp_error ("`%D' is already defined in class %s", fndecl,
13857 TYPE_NAME_STRING (DECL_CONTEXT (fndecl)));
8d08fdba
MS
13858 }
13859 return void_type_node;
13860 }
13861
f3400fe2
JM
13862 check_template_shadow (fndecl);
13863
faae18ab
MS
13864 DECL_THIS_INLINE (fndecl) = 1;
13865
8926095f 13866 if (flag_default_inline)
8d08fdba
MS
13867 DECL_INLINE (fndecl) = 1;
13868
36a117a5
MM
13869 /* We process method specializations in finish_struct_1. */
13870 if (processing_template_decl && !DECL_TEMPLATE_SPECIALIZATION (fndecl))
3ac3d9ea 13871 fndecl = push_template_decl (fndecl);
a0a33927 13872
8d08fdba
MS
13873 /* We read in the parameters on the maybepermanent_obstack,
13874 but we won't be getting back to them until after we
13875 may have clobbered them. So the call to preserve_data
13876 will keep them safe. */
13877 preserve_data ();
13878
13879 if (! DECL_FRIEND_P (fndecl))
13880 {
8d08fdba
MS
13881 if (TREE_CHAIN (fndecl))
13882 {
13883 fndecl = copy_node (fndecl);
13884 TREE_CHAIN (fndecl) = NULL_TREE;
13885 }
13886
13887 if (DECL_CONSTRUCTOR_P (fndecl))
a0a33927
MS
13888 {
13889 if (! grok_ctor_properties (current_class_type, fndecl))
13890 return void_type_node;
13891 }
8d08fdba
MS
13892 else if (IDENTIFIER_OPNAME_P (DECL_NAME (fndecl)))
13893 grok_op_properties (fndecl, DECL_VIRTUAL_P (fndecl), 0);
13894 }
13895
82580166 13896 cp_finish_decl (fndecl, NULL_TREE, NULL_TREE, 0, 0);
8d08fdba
MS
13897
13898 /* Make a place for the parms */
13899 pushlevel (0);
13900 current_binding_level->parm_flag = 1;
13901
13902 DECL_IN_AGGR_P (fndecl) = 1;
13903 return fndecl;
13904}
13905
13906/* Go through the motions of finishing a function definition.
13907 We don't compile this method until after the whole class has
13908 been processed.
13909
13910 FINISH_METHOD must return something that looks as though it
13911 came from GROKFIELD (since we are defining a method, after all).
13912
13913 This is called after parsing the body of the function definition.
13914 STMTS is the chain of statements that makes up the function body.
13915
13916 DECL is the ..._DECL that `start_method' provided. */
13917
13918tree
13919finish_method (decl)
13920 tree decl;
13921{
13922 register tree fndecl = decl;
13923 tree old_initial;
8d08fdba
MS
13924
13925 register tree link;
13926
a1774733 13927 if (decl == void_type_node)
8d08fdba
MS
13928 return decl;
13929
13930 old_initial = DECL_INITIAL (fndecl);
13931
13932 /* Undo the level for the parms (from start_method).
13933 This is like poplevel, but it causes nothing to be
13934 saved. Saving information here confuses symbol-table
13935 output routines. Besides, this information will
13936 be correctly output when this method is actually
13937 compiled. */
13938
13939 /* Clear out the meanings of the local variables of this level;
13940 also record in each decl which block it belongs to. */
13941
13942 for (link = current_binding_level->names; link; link = TREE_CHAIN (link))
13943 {
13944 if (DECL_NAME (link) != NULL_TREE)
d8f8dca1 13945 pop_binding (DECL_NAME (link), link);
8d08fdba
MS
13946 my_friendly_assert (TREE_CODE (link) != FUNCTION_DECL, 163);
13947 DECL_CONTEXT (link) = NULL_TREE;
13948 }
13949
8d08fdba
MS
13950 GNU_xref_end_scope ((HOST_WIDE_INT) current_binding_level,
13951 (HOST_WIDE_INT) current_binding_level->level_chain,
13952 current_binding_level->parm_flag,
5566b478 13953 current_binding_level->keep);
8d08fdba
MS
13954
13955 poplevel (0, 0, 0);
13956
13957 DECL_INITIAL (fndecl) = old_initial;
13958
13959 /* We used to check if the context of FNDECL was different from
13960 current_class_type as another way to get inside here. This didn't work
13961 for String.cc in libg++. */
13962 if (DECL_FRIEND_P (fndecl))
13963 {
13964 CLASSTYPE_INLINE_FRIENDS (current_class_type)
13965 = tree_cons (NULL_TREE, fndecl, CLASSTYPE_INLINE_FRIENDS (current_class_type));
13966 decl = void_type_node;
13967 }
13968
13969 return decl;
13970}
13971\f
13972/* Called when a new struct TYPE is defined.
13973 If this structure or union completes the type of any previous
13974 variable declaration, lay it out and output its rtl. */
13975
13976void
13977hack_incomplete_structures (type)
13978 tree type;
13979{
f30432d7 13980 tree *list;
8d08fdba 13981
f30432d7 13982 if (current_binding_level->incomplete == NULL_TREE)
8d08fdba
MS
13983 return;
13984
13985 if (!type) /* Don't do this for class templates. */
13986 return;
13987
f30432d7
MS
13988 for (list = &current_binding_level->incomplete; *list; )
13989 {
13990 tree decl = TREE_VALUE (*list);
a703fb38 13991 if ((decl && TREE_TYPE (decl) == type)
f30432d7
MS
13992 || (TREE_TYPE (decl)
13993 && TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
13994 && TREE_TYPE (TREE_TYPE (decl)) == type))
13995 {
13996 int toplevel = toplevel_bindings_p ();
13997 if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
13998 && TREE_TYPE (TREE_TYPE (decl)) == type)
13999 layout_type (TREE_TYPE (decl));
14000 layout_decl (decl, 0);
14001 rest_of_decl_compilation (decl, NULL_PTR, toplevel, 0);
14002 if (! toplevel)
14003 {
14004 tree cleanup;
14005 expand_decl (decl);
14006 cleanup = maybe_build_cleanup (decl);
14007 expand_decl_init (decl);
e349ee73 14008 if (! expand_decl_cleanup (decl, cleanup))
8251199e 14009 cp_error ("parser lost in parsing declaration of `%D'",
f30432d7
MS
14010 decl);
14011 }
14012 *list = TREE_CHAIN (*list);
14013 }
14014 else
14015 list = &TREE_CHAIN (*list);
14016 }
8d08fdba
MS
14017}
14018
8d08fdba 14019/* If DECL is of a type which needs a cleanup, build that cleanup here.
c73964b2 14020 See build_delete for information about AUTO_DELETE.
8d08fdba
MS
14021
14022 Don't build these on the momentary obstack; they must live
14023 the life of the binding contour. */
e92cc029 14024
c73964b2
MS
14025static tree
14026maybe_build_cleanup_1 (decl, auto_delete)
14027 tree decl, auto_delete;
8d08fdba
MS
14028{
14029 tree type = TREE_TYPE (decl);
44689c12 14030 if (type != error_mark_node && TYPE_NEEDS_DESTRUCTOR (type))
8d08fdba
MS
14031 {
14032 int temp = 0, flags = LOOKUP_NORMAL|LOOKUP_DESTRUCTOR;
14033 tree rval;
8d08fdba
MS
14034
14035 if (TREE_CODE (decl) != PARM_DECL)
14036 temp = suspend_momentary ();
14037
14038 if (TREE_CODE (type) == ARRAY_TYPE)
14039 rval = decl;
14040 else
14041 {
14042 mark_addressable (decl);
14043 rval = build_unary_op (ADDR_EXPR, decl, 0);
14044 }
14045
14046 /* Optimize for space over speed here. */
14047 if (! TYPE_USES_VIRTUAL_BASECLASSES (type)
14048 || flag_expensive_optimizations)
14049 flags |= LOOKUP_NONVIRTUAL;
14050
c73964b2 14051 rval = build_delete (TREE_TYPE (rval), rval, auto_delete, flags, 0);
8d08fdba
MS
14052
14053 if (TYPE_USES_VIRTUAL_BASECLASSES (type)
14054 && ! TYPE_HAS_DESTRUCTOR (type))
e1b3e07d 14055 rval = build_compound_expr (tree_cons (NULL_TREE, rval,
e66d884e 14056 build_expr_list (NULL_TREE, build_vbase_delete (type, decl))));
8d08fdba 14057
8d08fdba
MS
14058 if (TREE_CODE (decl) != PARM_DECL)
14059 resume_momentary (temp);
14060
8d08fdba
MS
14061 return rval;
14062 }
14063 return 0;
14064}
c73964b2 14065
9d85d30c
MM
14066/* Build a TARGET_EXPR, initializing the DECL with the VALUE. */
14067
14068tree
14069build_target_expr (decl, value)
14070 tree decl;
14071 tree value;
14072{
9263e6d6
MM
14073 tree t;
14074
14075 t = build (TARGET_EXPR, TREE_TYPE (decl), decl, value,
14076 maybe_build_cleanup (decl), NULL_TREE);
14077 /* We always set TREE_SIDE_EFFECTS so that expand_expr does not
14078 ignore the TARGET_EXPR. If there really turn out to be no
14079 side-effects, then the optimizer should be able to get rid of
14080 whatever code is generated anyhow. */
14081 TREE_SIDE_EFFECTS (t) = 1;
14082
14083 return t;
9d85d30c
MM
14084}
14085
c73964b2
MS
14086/* If DECL is of a type which needs a cleanup, build that cleanup
14087 here. The cleanup does free the storage with a call to delete. */
14088
14089tree
14090maybe_build_cleanup_and_delete (decl)
14091 tree decl;
14092{
14093 return maybe_build_cleanup_1 (decl, integer_three_node);
14094}
14095
14096/* If DECL is of a type which needs a cleanup, build that cleanup
14097 here. The cleanup does not free the storage with a call a delete. */
14098
14099tree
14100maybe_build_cleanup (decl)
14101 tree decl;
14102{
14103 return maybe_build_cleanup_1 (decl, integer_two_node);
14104}
8d08fdba
MS
14105\f
14106/* Expand a C++ expression at the statement level.
14107 This is needed to ferret out nodes which have UNKNOWN_TYPE.
14108 The C++ type checker should get all of these out when
14109 expressions are combined with other, type-providing, expressions,
14110 leaving only orphan expressions, such as:
14111
e92cc029 14112 &class::bar; / / takes its address, but does nothing with it. */
8d08fdba 14113
8d08fdba
MS
14114void
14115cplus_expand_expr_stmt (exp)
14116 tree exp;
14117{
02cac427
NS
14118 if (stmts_are_full_exprs_p)
14119 exp = convert_to_void (exp, "statement");
66543169 14120
63c68bb7 14121#if 0
66543169
NS
14122 /* We should do this eventually, but right now this causes regex.o from
14123 libg++ to miscompile, and tString to core dump. */
14124 exp = build1 (CLEANUP_POINT_EXPR, TREE_TYPE (exp), exp);
63c68bb7 14125#endif
b3e401c2 14126
66543169
NS
14127 /* If we don't do this, we end up down inside expand_expr
14128 trying to do TYPE_MODE on the ERROR_MARK, and really
14129 go outside the bounds of the type. */
14130 if (exp != error_mark_node)
14131 expand_expr_stmt (break_out_cleanups (exp));
8d08fdba
MS
14132}
14133
558475f0 14134/* When a stmt has been parsed, this function is called. */
8d08fdba
MS
14135
14136void
14137finish_stmt ()
14138{
558475f0
MM
14139 /* Always assume this statement was not an expression statement. If
14140 it actually was an expression statement, its our callers
14141 responsibility to fix this up. */
14142 last_expr_type = NULL_TREE;
8d08fdba
MS
14143}
14144
8d08fdba 14145/* Change a static member function definition into a FUNCTION_TYPE, instead
700f8a87
MS
14146 of the METHOD_TYPE that we create when it's originally parsed.
14147
14148 WARNING: DO NOT pass &TREE_TYPE (decl) to FN or &TYPE_ARG_TYPES
14149 (TREE_TYPE (decl)) to ARGTYPES, as doing so will corrupt the types of
14150 other decls. Either pass the addresses of local variables or NULL. */
14151
8857f91e 14152void
700f8a87
MS
14153revert_static_member_fn (decl, fn, argtypes)
14154 tree *decl, *fn, *argtypes;
8d08fdba 14155{
700f8a87
MS
14156 tree tmp;
14157 tree function = fn ? *fn : TREE_TYPE (*decl);
14158 tree args = argtypes ? *argtypes : TYPE_ARG_TYPES (function);
8d08fdba 14159
91063b51
MM
14160 if (CP_TYPE_QUALS (TREE_TYPE (TREE_VALUE (args)))
14161 != TYPE_UNQUALIFIED)
14162 cp_error ("static member function `%#D' declared with type qualifiers",
14163 *decl);
f30432d7 14164
700f8a87
MS
14165 args = TREE_CHAIN (args);
14166 tmp = build_function_type (TREE_TYPE (function), args);
91063b51 14167 tmp = build_qualified_type (tmp, CP_TYPE_QUALS (function));
f30432d7 14168 tmp = build_exception_variant (tmp,
8d08fdba
MS
14169 TYPE_RAISES_EXCEPTIONS (function));
14170 TREE_TYPE (*decl) = tmp;
f30432d7
MS
14171 if (DECL_ARGUMENTS (*decl))
14172 DECL_ARGUMENTS (*decl) = TREE_CHAIN (DECL_ARGUMENTS (*decl));
8d08fdba 14173 DECL_STATIC_FUNCTION_P (*decl) = 1;
700f8a87
MS
14174 if (fn)
14175 *fn = tmp;
14176 if (argtypes)
14177 *argtypes = args;
8d08fdba 14178}
a4443a08 14179
a8f73d4b
MM
14180/* Initialize the variables used during compilation of a C++
14181 function. */
db5ae43f 14182
99dccabc
MM
14183static void
14184push_cp_function_context (f)
14185 struct function *f;
14186{
14187 struct language_function *p
14188 = ((struct language_function *)
be91309c 14189 xcalloc (1, sizeof (struct language_function)));
99dccabc 14190 f->language = p;
db5ae43f 14191
b35d4555
MM
14192 /* It takes an explicit call to expand_body to generate RTL for a
14193 function. */
14194 expanding_p = 0;
f1dedc31
MM
14195
14196 /* Whenever we start a new function, we destroy temporaries in the
14197 usual way. */
14198 stmts_are_full_exprs_p = 1;
db5ae43f
MS
14199}
14200
a8f73d4b
MM
14201/* Free the language-specific parts of F, now that we've finished
14202 compiling the function. */
db5ae43f 14203
99dccabc
MM
14204static void
14205pop_cp_function_context (f)
14206 struct function *f;
db5ae43f 14207{
99dccabc
MM
14208 free (f->language);
14209 f->language = 0;
db5ae43f 14210}
ebfc180f 14211
59026e79 14212/* Mark P for GC. */
4519c0a8 14213
59026e79
MM
14214static void
14215mark_lang_function (p)
14216 struct language_function *p;
14217{
87e3dbc9
MM
14218 if (!p)
14219 return;
14220
4519c0a8
MM
14221 ggc_mark_tree (p->x_named_labels);
14222 ggc_mark_tree (p->x_ctor_label);
14223 ggc_mark_tree (p->x_dtor_label);
14224 ggc_mark_tree (p->x_base_init_list);
14225 ggc_mark_tree (p->x_member_init_list);
4519c0a8
MM
14226 ggc_mark_tree (p->x_current_class_ptr);
14227 ggc_mark_tree (p->x_current_class_ref);
14228 ggc_mark_tree (p->x_last_tree);
14229 ggc_mark_tree (p->x_last_expr_type);
b35d4555
MM
14230 ggc_mark_tree (p->x_eh_spec_try_block);
14231 ggc_mark_tree (p->x_scope_stmt_stack);
4519c0a8
MM
14232
14233 ggc_mark_rtx (p->x_last_dtor_insn);
14234 ggc_mark_rtx (p->x_last_parm_cleanup_insn);
14235 ggc_mark_rtx (p->x_result_rtx);
14236
a8f73d4b 14237 mark_binding_level (&p->bindings);
4519c0a8
MM
14238}
14239
59026e79
MM
14240/* Mark the language-specific data in F for GC. */
14241
14242void
14243mark_cp_function_context (f)
14244 struct function *f;
14245{
14246 mark_lang_function (f->language);
14247}
4519c0a8 14248
5566b478
MS
14249int
14250in_function_p ()
14251{
14252 return function_depth != 0;
14253}
4519c0a8
MM
14254
14255
14256void
b4b8bee7
MM
14257lang_mark_false_label_stack (l)
14258 struct label_node *l;
4519c0a8
MM
14259{
14260 /* C++ doesn't use false_label_stack. It better be NULL. */
87e3dbc9 14261 my_friendly_assert (l == NULL, 19990904);
4519c0a8
MM
14262}
14263
14264void
14265lang_mark_tree (t)
14266 tree t;
14267{
14268 enum tree_code code = TREE_CODE (t);
14269 if (code == IDENTIFIER_NODE)
14270 {
14271 struct lang_identifier *li = (struct lang_identifier *) t;
14272 struct lang_id2 *li2 = li->x;
14273 ggc_mark_tree (li->namespace_bindings);
a8f73d4b 14274 ggc_mark_tree (li->bindings);
4519c0a8
MM
14275 ggc_mark_tree (li->class_value);
14276 ggc_mark_tree (li->class_template_info);
14277
14278 if (li2)
14279 {
14280 ggc_mark_tree (li2->label_value);
14281 ggc_mark_tree (li2->implicit_decl);
14282 ggc_mark_tree (li2->error_locus);
14283 }
14284 }
87e3dbc9
MM
14285 else if (code == CPLUS_BINDING)
14286 {
14287 if (BINDING_HAS_LEVEL_P (t))
14288 mark_binding_level (&BINDING_LEVEL (t));
14289 else
14290 ggc_mark_tree (BINDING_SCOPE (t));
14291 ggc_mark_tree (BINDING_VALUE (t));
14292 }
14293 else if (code == OVERLOAD)
14294 ggc_mark_tree (OVL_FUNCTION (t));
14295 else if (code == TEMPLATE_PARM_INDEX)
14296 ggc_mark_tree (TEMPLATE_PARM_DECL (t));
4519c0a8
MM
14297 else if (TREE_CODE_CLASS (code) == 'd')
14298 {
14299 struct lang_decl *ld = DECL_LANG_SPECIFIC (t);
14300
14301 if (ld)
14302 {
d60f72ae 14303 ggc_mark (ld);
4519c0a8
MM
14304 ggc_mark_tree (ld->decl_flags.access);
14305 ggc_mark_tree (ld->decl_flags.context);
14306 if (TREE_CODE (t) != NAMESPACE_DECL)
14307 ggc_mark_tree (ld->decl_flags.u.template_info);
14308 if (CAN_HAVE_FULL_LANG_DECL_P (t))
14309 {
14310 ggc_mark_tree (ld->main_decl_variant);
14311 ggc_mark_tree (ld->befriending_classes);
14312 ggc_mark_tree (ld->saved_tree);
14313 if (TREE_CODE (t) == TYPE_DECL)
14314 ggc_mark_tree (ld->u.sorted_fields);
59026e79
MM
14315 else if (TREE_CODE (t) == FUNCTION_DECL
14316 && !DECL_PENDING_INLINE_P (t))
14317 mark_lang_function (DECL_SAVED_FUNCTION_DATA (t));
4519c0a8
MM
14318 }
14319 }
14320 }
14321 else if (TREE_CODE_CLASS (code) == 't')
14322 {
14323 struct lang_type *lt = TYPE_LANG_SPECIFIC (t);
14324
87e3dbc9
MM
14325 if (lt && !(TREE_CODE (t) == POINTER_TYPE
14326 && TREE_CODE (TREE_TYPE (t)) == METHOD_TYPE))
4519c0a8 14327 {
c27db0df 14328 ggc_mark (lt);
4519c0a8
MM
14329 ggc_mark_tree (lt->vfields);
14330 ggc_mark_tree (lt->vbases);
14331 ggc_mark_tree (lt->tags);
14332 ggc_mark_tree (lt->search_slot);
14333 ggc_mark_tree (lt->size);
14334 ggc_mark_tree (lt->abstract_virtuals);
14335 ggc_mark_tree (lt->friend_classes);
14336 ggc_mark_tree (lt->rtti);
14337 ggc_mark_tree (lt->methods);
14338 ggc_mark_tree (lt->template_info);
87e3dbc9 14339 ggc_mark_tree (lt->befriending_classes);
4519c0a8 14340 }
87e3dbc9
MM
14341 else if (lt)
14342 /* In the case of pointer-to-member function types, the
14343 TYPE_LANG_SPECIFIC is really just a tree. */
14344 ggc_mark_tree ((tree) lt);
4519c0a8
MM
14345 }
14346}
14347
This page took 3.147645 seconds and 5 git commands to generate.