]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/cp/decl.c
Include function.h in most files.
[thirdparty/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"
49ad7cfa 34#include "function.h"
8d08fdba
MS
35#include "flags.h"
36#include "cp-tree.h"
37#include "decl.h"
38#include "lex.h"
8d08fdba
MS
39#include <signal.h>
40#include "obstack.h"
72b7eeff 41#include "defaults.h"
49c249e1
JM
42#include "output.h"
43#include "except.h"
54f92bfb 44#include "toplev.h"
7ddedda4 45#include "../hash.h"
091f9839 46#include "defaults.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
42976354
BK
58extern int static_labelno;
59
30394414
JM
60extern tree current_namespace;
61extern tree global_namespace;
62
62c154ed 63extern void (*print_error_function) PROTO((char *));
e5dc5fb2 64extern int (*valid_lang_attribute) PROTO ((tree, tree, tree, tree));
62c154ed 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
72#ifndef CHAR_TYPE_SIZE
73#define CHAR_TYPE_SIZE BITS_PER_UNIT
74#endif
75
76#ifndef SHORT_TYPE_SIZE
77#define SHORT_TYPE_SIZE (BITS_PER_UNIT * MIN ((UNITS_PER_WORD + 1) / 2, 2))
78#endif
79
80#ifndef INT_TYPE_SIZE
81#define INT_TYPE_SIZE BITS_PER_WORD
82#endif
83
84#ifndef LONG_TYPE_SIZE
85#define LONG_TYPE_SIZE BITS_PER_WORD
86#endif
87
88#ifndef LONG_LONG_TYPE_SIZE
89#define LONG_LONG_TYPE_SIZE (BITS_PER_WORD * 2)
90#endif
91
92#ifndef WCHAR_UNSIGNED
93#define WCHAR_UNSIGNED 0
94#endif
95
96#ifndef FLOAT_TYPE_SIZE
97#define FLOAT_TYPE_SIZE BITS_PER_WORD
98#endif
99
100#ifndef DOUBLE_TYPE_SIZE
101#define DOUBLE_TYPE_SIZE (BITS_PER_WORD * 2)
102#endif
103
104#ifndef LONG_DOUBLE_TYPE_SIZE
105#define LONG_DOUBLE_TYPE_SIZE (BITS_PER_WORD * 2)
106#endif
107
255512c1 108#ifndef BOOL_TYPE_SIZE
e1cd6e56 109#ifdef SLOW_BYTE_ACCESS
d2e5ee5c 110#define BOOL_TYPE_SIZE ((SLOW_BYTE_ACCESS) ? (POINTER_SIZE) : (CHAR_TYPE_SIZE))
e1cd6e56 111#else
d2e5ee5c 112#define BOOL_TYPE_SIZE CHAR_TYPE_SIZE
e1cd6e56 113#endif
255512c1
JM
114#endif
115
8d08fdba
MS
116/* We let tm.h override the types used here, to handle trivial differences
117 such as the choice of unsigned int or long unsigned int for size_t.
118 When machines start needing nontrivial differences in the size type,
119 it would be best to do something here to figure out automatically
120 from other information what type to use. */
121
122#ifndef SIZE_TYPE
123#define SIZE_TYPE "long unsigned int"
124#endif
125
126#ifndef PTRDIFF_TYPE
127#define PTRDIFF_TYPE "long int"
128#endif
129
130#ifndef WCHAR_TYPE
131#define WCHAR_TYPE "int"
132#endif
133
8d08fdba 134static tree grokparms PROTO((tree, int));
d8e178a0 135static const char *redeclaration_error_message PROTO((tree, tree));
8d08fdba 136
49c249e1
JM
137static struct stack_level *push_decl_level PROTO((struct stack_level *,
138 struct obstack *));
139static void push_binding_level PROTO((struct binding_level *, int,
140 int));
141static void pop_binding_level PROTO((void));
142static void suspend_binding_level PROTO((void));
143static void resume_binding_level PROTO((struct binding_level *));
144static struct binding_level *make_binding_level PROTO((void));
49c249e1 145static void declare_namespace_level PROTO((void));
1ddb2906 146static void signal_catch PROTO((int)) ATTRIBUTE_NORETURN;
49c249e1 147static void storedecls PROTO((tree));
49c249e1
JM
148static void require_complete_types_for_parms PROTO((tree));
149static void push_overloaded_decl_1 PROTO((tree));
150static int ambi_op_p PROTO((tree));
151static int unary_op_p PROTO((tree));
152static tree store_bindings PROTO((tree, tree));
153static tree lookup_tag_reverse PROTO((tree, tree));
154static tree obscure_complex_init PROTO((tree, tree));
155static tree maybe_build_cleanup_1 PROTO((tree, tree));
3e3f722c 156static tree lookup_name_real PROTO((tree, int, int, int));
49c249e1 157static void warn_extern_redeclared_static PROTO((tree, tree));
a703fb38 158static void grok_reference_init PROTO((tree, tree, tree));
386b8a85 159static tree grokfndecl PROTO((tree, tree, tree, tree, int,
2c73f9f5 160 enum overload_flags, tree,
7a8f9fa9 161 tree, int, int, int, int, int, int, tree));
2c73f9f5 162static tree grokvardecl PROTO((tree, tree, RID_BIT_TYPE *, int, int, tree));
49c249e1
JM
163static tree lookup_tag PROTO((enum tree_code, tree,
164 struct binding_level *, int));
165static void set_identifier_type_value_with_scope
166 PROTO((tree, tree, struct binding_level *));
d8e178a0
KG
167static void record_builtin_type PROTO((enum rid, const char *, tree));
168static void record_unknown_type PROTO((tree, const char *));
169static int member_function_or_else PROTO((tree, tree, const char *));
170static void bad_specifiers PROTO((tree, const char *, int, int, int, int,
49c249e1 171 int));
62c154ed 172static void lang_print_error_function PROTO((char *));
39c01e4c 173static tree maybe_process_template_type_declaration PROTO((tree, int, struct binding_level*));
91063b51 174static void check_for_uninitialized_const_var PROTO((tree));
7ddedda4
MM
175static unsigned long typename_hash PROTO((hash_table_key));
176static boolean typename_compare PROTO((hash_table_key, hash_table_key));
f181d4ae 177static void push_binding PROTO((tree, tree, struct binding_level*));
8f032717 178static int add_binding PROTO((tree, tree));
d8f8dca1 179static void pop_binding PROTO((tree, tree));
297e73d8 180static tree local_variable_p PROTO((tree));
d8e178a0
KG
181static tree find_binding PROTO((tree, tree));
182static tree select_decl PROTO((tree, int));
183static tree unqualified_namespace_lookup PROTO((tree, int));
184static int lookup_flags PROTO((int, int));
185static tree qualify_lookup PROTO((tree, int));
186static tree record_builtin_java_type PROTO((const char *, int));
187static const char *tag_name PROTO((enum tag_types code));
8f032717
MM
188static void find_class_binding_level PROTO((void));
189static struct binding_level *innermost_nonclass_level PROTO((void));
b74a0560 190static tree poplevel_class PROTO((void));
235f734d 191static void warn_about_implicit_typename_lookup PROTO((tree, tree));
0aafb128
MM
192static int walk_namespaces_r PROTO((tree, walk_namespaces_fn, void *));
193static int walk_globals_r PROTO((tree, void *));
8d08fdba 194
69ac77ce
JL
195#if defined (DEBUG_CP_BINDING_LEVELS)
196static void indent PROTO((void));
197#endif
198
2c73f9f5 199/* A node which has tree code ERROR_MARK, and whose type is itself.
8d08fdba
MS
200 All erroneous expressions are replaced with this node. All functions
201 that accept nodes as arguments should avoid generating error messages
202 if this node is one of the arguments, since it is undesirable to get
203 multiple error messages from one error in the input. */
204
205tree error_mark_node;
206
207/* Erroneous argument lists can use this *IFF* they do not modify it. */
208tree error_mark_list;
209
7f4edbcb
BS
210/* The following symbols are subsumed in the cp_global_trees array, and
211 listed here individually for documentation purposes.
8d08fdba 212
7f4edbcb
BS
213 C++ extensions
214 tree wchar_decl_node;
215 tree void_zero_node;
37c46b43 216
7f4edbcb
BS
217 tree vtable_entry_type;
218 tree delta_type_node;
219#if 0
220 Old rtti stuff.
221 tree __baselist_desc_type_node;
222 tree __i_desc_type_node, __m_desc_type_node;
223 tree __t_desc_array_type, __i_desc_array_type, __m_desc_array_type;
946dc1c8 224#endif
7f4edbcb
BS
225 tree __t_desc_type_node;
226#if 0
227 tree __tp_desc_type_node;
228#endif
229 tree __access_mode_type_node;
230 tree __bltn_desc_type_node, __user_desc_type_node, __class_desc_type_node;
231 tree __ptr_desc_type_node, __attr_desc_type_node, __func_desc_type_node;
232 tree __ptmf_desc_type_node, __ptmd_desc_type_node;
233#if 0
234 Not needed yet? May be needed one day?
235 tree __bltn_desc_array_type, __user_desc_array_type, __class_desc_array_type;
236 tree __ptr_desc_array_type, __attr_dec_array_type, __func_desc_array_type;
237 tree __ptmf_desc_array_type, __ptmd_desc_array_type;
946dc1c8 238#endif
8d08fdba 239
7f4edbcb
BS
240 tree class_star_type_node;
241 tree class_type_node, record_type_node, union_type_node, enum_type_node;
242 tree unknown_type_node;
243 tree opaque_type_node, signature_type_node;
244 tree sigtable_entry_type;
8d08fdba 245
7f4edbcb 246 Array type `vtable_entry_type[]'
8d08fdba 247
7f4edbcb
BS
248 tree vtbl_type_node;
249 tree vtbl_ptr_type_node;
8d08fdba 250
7f4edbcb 251 Nnamespace std
8d08fdba 252
7f4edbcb 253 tree std_node;
8d08fdba 254
7f4edbcb
BS
255 A FUNCTION_DECL which can call `abort'. Not necessarily the
256 one that the user will declare, but sufficient to be called
257 by routines that want to abort the program.
8d08fdba 258
7f4edbcb 259 tree abort_fndecl;
8d08fdba 260
7f4edbcb 261 The FUNCTION_DECL for the default `::operator delete'.
2986ae00 262
7f4edbcb 263 tree global_delete_fndecl;
8d08fdba 264
7f4edbcb
BS
265 Used by RTTI
266 tree type_info_type_node, tinfo_fn_id, tinfo_fn_type;
8d08fdba 267
7f4edbcb 268*/
8d08fdba 269
7f4edbcb 270tree cp_global_trees[CPTI_MAX];
8d08fdba 271
7f4edbcb
BS
272/* These can't be part of the above array, since they are declared
273 individually in tree.h, and used by the debug output routines. */
8d08fdba 274
7f4edbcb
BS
275tree void_type_node;
276tree char_type_node;
277tree integer_type_node;
278tree unsigned_type_node;
db5ae43f 279
4a4b0aa3
RH
280/* These can't be part of the above array, since they are declared
281 individially in tree.h and used by the target routines. */
282
283tree ptr_type_node;
284tree va_list_type_node;
285
2c73f9f5 286/* Indicates that there is a type value in some namespace, although
7f4edbcb 287 that is not necessarily in scope at the moment. */
2c73f9f5
ML
288
289static tree global_type_node;
290
7f4edbcb 291/* Namespace std. */
0dde4175 292int in_std;
6633d636 293
3e3f722c
ML
294/* Expect only namespace names now. */
295static int only_namespace_names;
296
8d08fdba
MS
297/* In a destructor, the point at which all derived class destroying
298 has been done, just before any base class destroying will be done. */
299
300tree dtor_label;
301
72b7eeff
MS
302/* In a destructor, the last insn emitted after the start of the
303 function and the parms. */
304
bd6dd845 305static rtx last_dtor_insn;
72b7eeff 306
b87692e5 307/* In a constructor, the last insn emitted after the start of the
9664b89e
JM
308 function and the parms, the exception specification and any
309 function-try-block. The constructor initializers are emitted after
310 this insn. */
b87692e5
MS
311
312static rtx last_parm_cleanup_insn;
313
8d08fdba
MS
314/* In a constructor, the point at which we are ready to return
315 the pointer to the initialized object. */
316
317tree ctor_label;
318
8d08fdba
MS
319/* If original DECL_RESULT of current function was a register,
320 but due to being an addressable named return value, would up
321 on the stack, this variable holds the named return value's
322 original location. */
bd6dd845 323static rtx original_result_rtx;
8d08fdba
MS
324
325/* Sequence of insns which represents base initialization. */
a9aedbc2 326tree base_init_expr;
8d08fdba
MS
327
328/* C++: Keep these around to reduce calls to `get_identifier'.
329 Identifiers for `this' in member functions and the auto-delete
330 parameter for destructors. */
331tree this_identifier, in_charge_identifier;
fc378698 332tree ctor_identifier, dtor_identifier;
e92cc029 333/* Used in pointer to member functions, in vtables, and in sigtables. */
8d08fdba 334tree pfn_identifier, index_identifier, delta_identifier, delta2_identifier;
9dd70aa4 335tree pfn_or_delta2_identifier, tag_identifier;
5566b478 336tree vt_off_identifier;
8d08fdba 337
4cc1d462
NS
338/* Exception specifier used for throw(). */
339tree empty_except_spec;
340
0dde4175
JM
341/* Nonzero if we're in a handler for a function-try-block. */
342int in_function_try_handler;
343
e349ee73
MS
344struct named_label_list
345{
346 struct binding_level *binding_level;
347 tree names_in_scope;
348 tree label_decl;
9c0758dd 349 const char *filename_o_goto;
e349ee73
MS
350 int lineno_o_goto;
351 struct named_label_list *next;
352};
353
8d08fdba
MS
354/* A list (chain of TREE_LIST nodes) of named label uses.
355 The TREE_PURPOSE field is the list of variables defined
38e01259 356 in the label's scope defined at the point of use.
8d08fdba
MS
357 The TREE_VALUE field is the LABEL_DECL used.
358 The TREE_TYPE field holds `current_binding_level' at the
359 point of the label's use.
360
e349ee73
MS
361 BWAHAHAAHAHahhahahahaah. No, no, no, said the little chicken.
362
363 Look at the pretty struct named_label_list. See the pretty struct
364 with the pretty named fields that describe what they do. See the
365 pretty lack of gratuitous casts. Notice the code got a lot cleaner.
366
8d08fdba
MS
367 Used only for jumps to as-yet undefined labels, since
368 jumps to defined labels can have their validity checked
369 by stmt.c. */
370
e349ee73 371static struct named_label_list *named_label_uses = NULL;
8d08fdba
MS
372
373/* A list of objects which have constructors or destructors
374 which reside in the global scope. The decl is stored in
375 the TREE_VALUE slot and the initializer is stored
376 in the TREE_PURPOSE slot. */
377tree static_aggregates;
378
8d08fdba
MS
379/* -- end of C++ */
380
381/* Two expressions that are constants with value zero.
382 The first is of type `int', the second of type `void *'. */
383
384tree integer_zero_node;
385tree null_pointer_node;
386
03d0f4af
MM
387/* The value for __null (NULL), namely, a zero of an integer type with
388 the same number of bits as a pointer. */
d11ad92e
MS
389tree null_node;
390
8d08fdba
MS
391/* A node for the integer constants 1, 2, and 3. */
392
393tree integer_one_node, integer_two_node, integer_three_node;
394
8d08fdba
MS
395/* While defining an enum type, this is 1 plus the last enumerator
396 constant value. */
397
398static tree enum_next_value;
399
7177d104
MS
400/* Nonzero means that there was overflow computing enum_next_value. */
401
402static int enum_overflow;
403
8d08fdba
MS
404/* Parsing a function declarator leaves a list of parameter names
405 or a chain or parameter decls here. */
406
407tree last_function_parms;
408
409/* Parsing a function declarator leaves here a chain of structure
410 and enum types declared in the parmlist. */
411
412static tree last_function_parm_tags;
413
414/* After parsing the declarator that starts a function definition,
415 `start_function' puts here the list of parameter names or chain of decls.
416 `store_parm_decls' finds it here. */
417
418static tree current_function_parms;
419
420/* Similar, for last_function_parm_tags. */
421static tree current_function_parm_tags;
422
423/* A list (chain of TREE_LIST nodes) of all LABEL_DECLs in the function
424 that have names. Here so we can clear out their names' definitions
425 at the end of the function. */
426
427static tree named_labels;
428
429/* A list of LABEL_DECLs from outer contexts that are currently shadowed. */
430
431static tree shadowed_labels;
432
8d08fdba
MS
433/* The FUNCTION_DECL for the function currently being compiled,
434 or 0 if between functions. */
435tree current_function_decl;
436
437/* Set to 0 at beginning of a function definition, set to 1 if
438 a return statement that specifies a return value is seen. */
439
440int current_function_returns_value;
441
442/* Set to 0 at beginning of a function definition, set to 1 if
443 a return statement with no argument is seen. */
444
445int current_function_returns_null;
446
447/* Set to 0 at beginning of a function definition, and whenever
448 a label (case or named) is defined. Set to value of expression
449 returned from function when that value can be transformed into
450 a named return value. */
451
452tree current_function_return_value;
453
8d08fdba
MS
454/* Nonzero means give `double' the same size as `float'. */
455
456extern int flag_short_double;
457
458/* Nonzero means don't recognize any builtin functions. */
459
460extern int flag_no_builtin;
461
00595019
MS
462/* Nonzero means don't recognize the non-ANSI builtin functions.
463 -ansi sets this. */
464
465extern int flag_no_nonansi_builtin;
466
e1cd6e56
MS
467/* Nonzero means enable obscure ANSI features and disable GNU extensions
468 that might cause ANSI-compliant code to be miscompiled. */
8d08fdba
MS
469
470extern int flag_ansi;
471
472/* Nonzero if we want to support huge (> 2^(sizeof(short)*8-1) bytes)
e92cc029 473 objects. */
8d08fdba
MS
474extern int flag_huge_objects;
475
476/* Nonzero if we want to conserve space in the .o files. We do this
477 by putting uninitialized data and runtime initialized data into
ddd5a7c1 478 .common instead of .data at the expense of not flagging multiple
8d08fdba
MS
479 definitions. */
480extern int flag_conserve_space;
481
482/* Pointers to the base and current top of the language name stack. */
483
484extern tree *current_lang_base, *current_lang_stack;
485\f
51c184be 486/* C and C++ flags are in decl2.c. */
8d08fdba
MS
487
488/* Set to 0 at beginning of a constructor, set to 1
489 if that function does an allocation before referencing its
490 instance variable. */
5566b478 491static int current_function_assigns_this;
8d08fdba
MS
492int current_function_just_assigned_this;
493
494/* Set to 0 at beginning of a function. Set non-zero when
495 store_parm_decls is called. Don't call store_parm_decls
496 if this flag is non-zero! */
497int current_function_parms_stored;
498
51c184be 499/* Flag used when debugging spew.c */
8d08fdba
MS
500
501extern int spew_debug;
502
503/* This is a copy of the class_shadowed list of the previous class binding
504 contour when at global scope. It's used to reset IDENTIFIER_CLASS_VALUEs
505 when entering another class scope (i.e. a cache miss). */
506extern tree previous_class_values;
507
e92cc029
MS
508/* A expression of value 0 with the same precision as a sizetype
509 node, but signed. */
510tree signed_size_zero_node;
511
0c8feefe
MM
512/* The name of the anonymous namespace, throughout this translation
513 unit. */
514tree anonymous_namespace_name;
515
8d08fdba
MS
516\f
517/* Allocate a level of searching. */
e92cc029 518
824b9a4c 519static
8d08fdba
MS
520struct stack_level *
521push_decl_level (stack, obstack)
522 struct stack_level *stack;
523 struct obstack *obstack;
524{
525 struct stack_level tem;
526 tem.prev = stack;
527
528 return push_stack_level (obstack, (char *)&tem, sizeof (tem));
529}
530\f
531/* For each binding contour we allocate a binding_level structure
e92cc029
MS
532 which records the names defined in that contour.
533 Contours include:
534 0) the global one
535 1) one for each function definition,
536 where internal declarations of the parameters appear.
537 2) one for each compound statement,
538 to record its declarations.
539
540 The current meaning of a name can be found by searching the levels
541 from the current one out to the global one.
542
543 Off to the side, may be the class_binding_level. This exists only
544 to catch class-local declarations. It is otherwise nonexistent.
545
546 Also there may be binding levels that catch cleanups that must be
d8f8dca1
MM
547 run when exceptions occur. Thus, to see whether a name is bound in
548 the current scope, it is not enough to look in the
549 CURRENT_BINDING_LEVEL. You should use lookup_name_current_level
550 instead. */
8d08fdba
MS
551
552/* Note that the information in the `names' component of the global contour
553 is duplicated in the IDENTIFIER_GLOBAL_VALUEs of all identifiers. */
554
555struct binding_level
556 {
557 /* A chain of _DECL nodes for all variables, constants, functions,
e92cc029 558 and typedef types. These are in the reverse of the order
f181d4ae
MM
559 supplied. There may be OVERLOADs on this list, too, but they
560 are wrapped in TREE_LISTs; the TREE_VALUE is the OVERLOAD. */
8d08fdba
MS
561 tree names;
562
e92cc029
MS
563 /* A list of structure, union and enum definitions, for looking up
564 tag names.
565 It is a chain of TREE_LIST nodes, each of whose TREE_PURPOSE is a name,
566 or NULL_TREE; and whose TREE_VALUE is a RECORD_TYPE, UNION_TYPE,
567 or ENUMERAL_TYPE node.
568
569 C++: the TREE_VALUE nodes can be simple types for
570 component_bindings. */
8d08fdba
MS
571 tree tags;
572
2c73f9f5
ML
573 /* A list of USING_DECL nodes. */
574 tree usings;
575
ea9635c7
ML
576 /* A list of used namespaces. PURPOSE is the namespace,
577 VALUE the common ancestor with this binding_level's namespace. */
578 tree using_directives;
579
f181d4ae
MM
580 /* If this binding level is the binding level for a class, then
581 class_shadowed is a TREE_LIST. The TREE_PURPOSE of each node
582 is the name of an entity bound in the class; the TREE_VALUE is
583 the IDENTIFIER_CLASS_VALUE before we entered the class. Thus,
584 when leaving class scope, we can restore the
d8f8dca1
MM
585 IDENTIFIER_CLASS_VALUE by walking this list. The TREE_TYPE is
586 the DECL bound by this name in the class. */
8d08fdba
MS
587 tree class_shadowed;
588
f181d4ae
MM
589 /* Similar to class_shadowed, but for IDENTIFIER_TYPE_VALUE, and
590 is used for all binding levels. */
8d08fdba
MS
591 tree type_shadowed;
592
593 /* For each level (except not the global one),
594 a chain of BLOCK nodes for all the levels
595 that were entered and exited one level down. */
596 tree blocks;
597
598 /* The BLOCK node for this level, if one has been preallocated.
599 If 0, the BLOCK is allocated (if needed) when the level is popped. */
600 tree this_block;
601
602 /* The binding level which this one is contained in (inherits from). */
603 struct binding_level *level_chain;
604
f30432d7 605 /* List of decls in `names' that have incomplete
8d08fdba 606 structure or union types. */
f30432d7 607 tree incomplete;
8d08fdba 608
8d6e462b 609 /* List of VAR_DECLS saved from a previous for statement.
abc95ed3 610 These would be dead in ANSI-conforming code, but might
f181d4ae
MM
611 be referenced in ARM-era code. These are stored in a
612 TREE_LIST; the TREE_VALUE is the actual declaration. */
8d6e462b
PB
613 tree dead_vars_from_for;
614
8d08fdba
MS
615 /* 1 for the level that holds the parameters of a function.
616 2 for the level that holds a class declaration.
617 3 for levels that hold parameter declarations. */
618 unsigned parm_flag : 4;
619
620 /* 1 means make a BLOCK for this level regardless of all else.
621 2 for temporary binding contours created by the compiler. */
622 unsigned keep : 3;
623
624 /* Nonzero if this level "doesn't exist" for tags. */
625 unsigned tag_transparent : 1;
626
627 /* Nonzero if this level can safely have additional
628 cleanup-needing variables added to it. */
629 unsigned more_cleanups_ok : 1;
630 unsigned have_cleanups : 1;
631
5566b478
MS
632 /* Nonzero if this level is for storing the decls for template
633 parameters and generic decls; these decls will be discarded and
634 replaced with a TEMPLATE_DECL. */
8d08fdba
MS
635 unsigned pseudo_global : 1;
636
a9aedbc2
MS
637 /* This is set for a namespace binding level. */
638 unsigned namespace_p : 1;
639
2ee887f2 640 /* True if this level is that of a for-statement where we need to
d22c8596 641 worry about ambiguous (ARM or ANSI) scope rules. */
8d6e462b
PB
642 unsigned is_for_scope : 1;
643
227cf171
JM
644 /* True if this level corresponds to an EH region, as for a try block. */
645 unsigned eh_region : 1;
646
647 /* One bit left for this word. */
8d08fdba
MS
648
649#if defined(DEBUG_CP_BINDING_LEVELS)
650 /* Binding depth at which this level began. */
651 unsigned binding_depth;
652#endif /* defined(DEBUG_CP_BINDING_LEVELS) */
653 };
654
655#define NULL_BINDING_LEVEL ((struct binding_level *) NULL)
656
8f032717 657/* The binding level currently in effect. */
8d08fdba
MS
658
659static struct binding_level *current_binding_level;
660
661/* The binding level of the current class, if any. */
662
663static struct binding_level *class_binding_level;
664
8d08fdba
MS
665/* A chain of binding_level structures awaiting reuse. */
666
667static struct binding_level *free_binding_level;
668
669/* The outermost binding level, for names of file scope.
670 This is created when the compiler is started and exists
671 through the entire run. */
672
673static struct binding_level *global_binding_level;
674
675/* Binding level structures are initialized by copying this one. */
676
677static struct binding_level clear_binding_level;
678
679/* Nonzero means unconditionally make a BLOCK for the next level pushed. */
680
681static int keep_next_level_flag;
682
683#if defined(DEBUG_CP_BINDING_LEVELS)
684static int binding_depth = 0;
685static int is_class_level = 0;
686
687static void
688indent ()
689{
690 register unsigned i;
691
692 for (i = 0; i < binding_depth*2; i++)
693 putc (' ', stderr);
694}
695#endif /* defined(DEBUG_CP_BINDING_LEVELS) */
696
697static tree pushdecl_with_scope PROTO((tree, struct binding_level *));
698
699static void
700push_binding_level (newlevel, tag_transparent, keep)
701 struct binding_level *newlevel;
702 int tag_transparent, keep;
703{
704 /* Add this level to the front of the chain (stack) of levels that
705 are active. */
706 *newlevel = clear_binding_level;
8f032717 707 newlevel->level_chain = current_binding_level;
8d08fdba
MS
708 current_binding_level = newlevel;
709 newlevel->tag_transparent = tag_transparent;
710 newlevel->more_cleanups_ok = 1;
227cf171
JM
711
712 /* We are called before expand_start_bindings, but after
713 expand_eh_region_start for a try block; so we check this now,
714 before the EH block is covered up. */
715 newlevel->eh_region = is_eh_region ();
716
8d08fdba
MS
717 newlevel->keep = keep;
718#if defined(DEBUG_CP_BINDING_LEVELS)
719 newlevel->binding_depth = binding_depth;
720 indent ();
721 fprintf (stderr, "push %s level 0x%08x line %d\n",
722 (is_class_level) ? "class" : "block", newlevel, lineno);
723 is_class_level = 0;
724 binding_depth++;
725#endif /* defined(DEBUG_CP_BINDING_LEVELS) */
726}
727
8f032717
MM
728/* Find the innermost enclosing class scope, and reset
729 CLASS_BINDING_LEVEL appropriately. */
730
731static void
732find_class_binding_level ()
733{
734 struct binding_level *level = current_binding_level;
735
736 while (level && level->parm_flag != 2)
737 level = level->level_chain;
738 if (level && level->parm_flag == 2)
739 class_binding_level = level;
740 else
741 class_binding_level = 0;
742}
743
8d08fdba
MS
744static void
745pop_binding_level ()
746{
8d08fdba
MS
747 if (global_binding_level)
748 {
2c73f9f5 749 /* Cannot pop a level, if there are none left to pop. */
8d08fdba
MS
750 if (current_binding_level == global_binding_level)
751 my_friendly_abort (123);
752 }
753 /* Pop the current level, and free the structure for reuse. */
754#if defined(DEBUG_CP_BINDING_LEVELS)
755 binding_depth--;
756 indent ();
757 fprintf (stderr, "pop %s level 0x%08x line %d\n",
758 (is_class_level) ? "class" : "block",
759 current_binding_level, lineno);
760 if (is_class_level != (current_binding_level == class_binding_level))
9e9ff709
MS
761 {
762 indent ();
763 fprintf (stderr, "XXX is_class_level != (current_binding_level == class_binding_level)\n");
764 }
8d08fdba
MS
765 is_class_level = 0;
766#endif /* defined(DEBUG_CP_BINDING_LEVELS) */
767 {
768 register struct binding_level *level = current_binding_level;
769 current_binding_level = current_binding_level->level_chain;
770 level->level_chain = free_binding_level;
771#if 0 /* defined(DEBUG_CP_BINDING_LEVELS) */
772 if (level->binding_depth != binding_depth)
773 abort ();
774#endif /* defined(DEBUG_CP_BINDING_LEVELS) */
8f032717
MM
775 free_binding_level = level;
776 find_class_binding_level ();
8d08fdba
MS
777 }
778}
a9aedbc2
MS
779
780static void
781suspend_binding_level ()
782{
783 if (class_binding_level)
784 current_binding_level = class_binding_level;
785
786 if (global_binding_level)
787 {
2c73f9f5 788 /* Cannot suspend a level, if there are none left to suspend. */
a9aedbc2
MS
789 if (current_binding_level == global_binding_level)
790 my_friendly_abort (123);
791 }
792 /* Suspend the current level. */
793#if defined(DEBUG_CP_BINDING_LEVELS)
794 binding_depth--;
795 indent ();
796 fprintf (stderr, "suspend %s level 0x%08x line %d\n",
797 (is_class_level) ? "class" : "block",
798 current_binding_level, lineno);
799 if (is_class_level != (current_binding_level == class_binding_level))
9e9ff709
MS
800 {
801 indent ();
802 fprintf (stderr, "XXX is_class_level != (current_binding_level == class_binding_level)\n");
803 }
a9aedbc2
MS
804 is_class_level = 0;
805#endif /* defined(DEBUG_CP_BINDING_LEVELS) */
8f032717
MM
806 current_binding_level = current_binding_level->level_chain;
807 find_class_binding_level ();
a9aedbc2
MS
808}
809
824b9a4c 810static void
a9aedbc2
MS
811resume_binding_level (b)
812 struct binding_level *b;
813{
2c73f9f5
ML
814 /* Resuming binding levels is meant only for namespaces,
815 and those cannot nest into classes. */
816 my_friendly_assert(!class_binding_level, 386);
817 /* Also, resuming a non-directly nested namespace is a no-no. */
818 my_friendly_assert(b->level_chain == current_binding_level, 386);
a9aedbc2
MS
819 current_binding_level = b;
820#if defined(DEBUG_CP_BINDING_LEVELS)
821 b->binding_depth = binding_depth;
822 indent ();
823 fprintf (stderr, "resume %s level 0x%08x line %d\n",
824 (is_class_level) ? "class" : "block", b, lineno);
825 is_class_level = 0;
826 binding_depth++;
827#endif /* defined(DEBUG_CP_BINDING_LEVELS) */
828}
8d08fdba 829\f
cffa8729
MS
830/* Create a new `struct binding_level'. */
831
832static
833struct binding_level *
834make_binding_level ()
835{
836 /* NOSTRICT */
837 return (struct binding_level *) xmalloc (sizeof (struct binding_level));
838}
839
8d08fdba
MS
840/* Nonzero if we are currently in the global binding level. */
841
842int
843global_bindings_p ()
844{
845 return current_binding_level == global_binding_level;
846}
847
8f032717
MM
848/* Return the innermost binding level that is not for a class scope. */
849
850static struct binding_level *
851innermost_nonclass_level ()
852{
853 struct binding_level *b;
854
855 b = current_binding_level;
856 while (b->parm_flag == 2)
857 b = b->level_chain;
858
859 return b;
860}
861
a9aedbc2
MS
862/* Nonzero if we are currently in a toplevel binding level. This
863 means either the global binding level or a namespace in a toplevel
8f032717
MM
864 binding level. Since there are no non-toplevel namespace levels,
865 this really means any namespace or pseudo-global level. We also
866 include a class whose context is toplevel. */
a9aedbc2
MS
867
868int
869toplevel_bindings_p ()
870{
8f032717
MM
871 struct binding_level *b = innermost_nonclass_level ();
872
873 return b->namespace_p || b->pseudo_global;
a9aedbc2
MS
874}
875
8f032717
MM
876/* Nonzero if this is a namespace scope, or if we are defining a class
877 which is itself at namespace scope, or whose enclosing class is
878 such a class, etc. */
a9aedbc2 879
7bdbfa05 880int
a9aedbc2
MS
881namespace_bindings_p ()
882{
8f032717
MM
883 struct binding_level *b = innermost_nonclass_level ();
884
885 return b->namespace_p;
a9aedbc2
MS
886}
887
8d08fdba
MS
888void
889keep_next_level ()
890{
891 keep_next_level_flag = 1;
892}
893
894/* Nonzero if the current level needs to have a BLOCK made. */
895
896int
897kept_level_p ()
898{
899 return (current_binding_level->blocks != NULL_TREE
900 || current_binding_level->keep
901 || current_binding_level->names != NULL_TREE
902 || (current_binding_level->tags != NULL_TREE
903 && !current_binding_level->tag_transparent));
904}
905
906/* Identify this binding level as a level of parameters. */
907
908void
909declare_parm_level ()
910{
911 current_binding_level->parm_flag = 1;
912}
913
8d08fdba
MS
914void
915declare_pseudo_global_level ()
916{
917 current_binding_level->pseudo_global = 1;
918}
919
824b9a4c 920static void
a9aedbc2
MS
921declare_namespace_level ()
922{
923 current_binding_level->namespace_p = 1;
924}
925
8d08fdba
MS
926int
927pseudo_global_level_p ()
928{
8f032717
MM
929 struct binding_level *b = innermost_nonclass_level ();
930
931 return b->pseudo_global;
8d08fdba
MS
932}
933
934void
935set_class_shadows (shadows)
936 tree shadows;
937{
938 class_binding_level->class_shadowed = shadows;
939}
940
941/* Enter a new binding level.
942 If TAG_TRANSPARENT is nonzero, do so only for the name space of variables,
943 not for that of tags. */
944
945void
946pushlevel (tag_transparent)
947 int tag_transparent;
948{
949 register struct binding_level *newlevel = NULL_BINDING_LEVEL;
950
951 /* If this is the top level of a function,
952 just make sure that NAMED_LABELS is 0.
953 They should have been set to 0 at the end of the previous function. */
954
955 if (current_binding_level == global_binding_level)
956 my_friendly_assert (named_labels == NULL_TREE, 134);
957
958 /* Reuse or create a struct for this binding level. */
959
960#if defined(DEBUG_CP_BINDING_LEVELS)
961 if (0)
962#else /* !defined(DEBUG_CP_BINDING_LEVELS) */
963 if (free_binding_level)
964#endif /* !defined(DEBUG_CP_BINDING_LEVELS) */
965 {
966 newlevel = free_binding_level;
967 free_binding_level = free_binding_level->level_chain;
968 }
969 else
970 {
cffa8729 971 newlevel = make_binding_level ();
8d08fdba 972 }
cffa8729 973
8d08fdba
MS
974 push_binding_level (newlevel, tag_transparent, keep_next_level_flag);
975 GNU_xref_start_scope ((HOST_WIDE_INT) newlevel);
976 keep_next_level_flag = 0;
977}
978
5566b478 979void
8d6e462b
PB
980note_level_for_for ()
981{
982 current_binding_level->is_for_scope = 1;
983}
984
8d08fdba
MS
985void
986pushlevel_temporary (tag_transparent)
987 int tag_transparent;
988{
989 pushlevel (tag_transparent);
990 current_binding_level->keep = 2;
991 clear_last_expr ();
992
993 /* Note we don't call push_momentary() here. Otherwise, it would cause
994 cleanups to be allocated on the momentary obstack, and they will be
995 overwritten by the next statement. */
996
997 expand_start_bindings (0);
998}
999
f181d4ae
MM
1000/* For a binding between a name and an entity at a block scope,
1001 this is the `struct binding_level' for the block. */
1002#define BINDING_LEVEL(NODE) \
c7a932b1 1003 (((struct tree_binding*)NODE)->scope.level)
f181d4ae
MM
1004
1005/* These are currently unused, but permanent, CPLUS_BINDING nodes.
1006 They are kept here because they are allocated from the permanent
1007 obstack and cannot be easily freed. */
1008static tree free_binding_nodes;
1009
1010/* Make DECL the innermost binding for ID. The LEVEL is the binding
1011 level at which this declaration is being bound. */
1012
1013static void
1014push_binding (id, decl, level)
1015 tree id;
1016 tree decl;
1017 struct binding_level* level;
1018{
1019 tree binding;
1020
1021 if (!free_binding_nodes)
1022 {
1023 /* There are no free nodes, so we must build one here. */
1024 push_obstacks_nochange ();
1025 end_temporary_allocation ();
1026 binding = make_node (CPLUS_BINDING);
1027 pop_obstacks ();
1028 }
1029 else
1030 {
1031 /* There are nodes on the free list. Grab the first one. */
1032 binding = free_binding_nodes;
1033
1034 /* And update the free list. */
1035 free_binding_nodes = TREE_CHAIN (free_binding_nodes);
1036 }
1037
1038 /* Now, fill in the binding information. */
1039 BINDING_VALUE (binding) = decl;
d8f8dca1 1040 BINDING_TYPE (binding) = NULL_TREE;
f181d4ae 1041 BINDING_LEVEL (binding) = level;
8f032717 1042 INHERITED_VALUE_BINDING_P (binding) = 0;
f181d4ae
MM
1043 LOCAL_BINDING_P (binding) = (level != class_binding_level);
1044
c45df9c1 1045 /* And put it on the front of the list of bindings for ID. */
f181d4ae
MM
1046 TREE_CHAIN (binding) = IDENTIFIER_BINDING (id);
1047 IDENTIFIER_BINDING (id) = binding;
1048}
1049
d8f8dca1
MM
1050/* ID is already bound in the current scope. But, DECL is an
1051 additional binding for ID in the same scope. This is the `struct
1052 stat' hack whereby a non-typedef class-name or enum-name can be
1053 bound at the same level as some other kind of entity. It's the
1054 responsibility of the caller to check that inserting this name is
8f032717
MM
1055 legal here. Returns nonzero if the new binding was successful. */
1056static int
d8f8dca1
MM
1057add_binding (id, decl)
1058 tree id;
1059 tree decl;
1060{
1061 tree binding = IDENTIFIER_BINDING (id);
8f032717 1062 int ok = 1;
d8f8dca1
MM
1063
1064 if (TREE_CODE (decl) == TYPE_DECL && DECL_ARTIFICIAL (decl))
1065 /* The new name is the type name. */
1066 BINDING_TYPE (binding) = decl;
8f032717
MM
1067 else if (!BINDING_VALUE (binding))
1068 /* This situation arises when push_class_level_binding moves an
1069 inherited type-binding out of the way to make room for a new
1070 value binding. */
1071 BINDING_VALUE (binding) = decl;
1072 else if (TREE_CODE (BINDING_VALUE (binding)) == TYPE_DECL
1073 && DECL_ARTIFICIAL (BINDING_VALUE (binding)))
1074 {
1075 /* The old binding was a type name. It was placed in
1076 BINDING_VALUE because it was thought, at the point it was
1077 declared, to be the only entity with such a name. Move the
1078 type name into the type slot; it is now hidden by the new
1079 binding. */
d8f8dca1
MM
1080 BINDING_TYPE (binding) = BINDING_VALUE (binding);
1081 BINDING_VALUE (binding) = decl;
8f032717 1082 INHERITED_VALUE_BINDING_P (binding) = 0;
d8f8dca1 1083 }
263505af
MM
1084 else if (TREE_CODE (BINDING_VALUE (binding)) == TYPE_DECL
1085 && TREE_CODE (decl) == TYPE_DECL
1086 && DECL_NAME (decl) == DECL_NAME (BINDING_VALUE (binding))
1087 && same_type_p (TREE_TYPE (decl),
1088 TREE_TYPE (BINDING_VALUE (binding))))
1089 /* We have two typedef-names, both naming the same type to have
1090 the same name. This is OK because of:
1091
1092 [dcl.typedef]
1093
1094 In a given scope, a typedef specifier can be used to redefine
1095 the name of any type declared in that scope to refer to the
1096 type to which it already refers. */
1097 ok = 0;
8f032717
MM
1098 else
1099 {
1100 cp_error ("declaration of `%#D'", decl);
1101 cp_error_at ("conflicts with previous declaration `%#D'",
1102 BINDING_VALUE (binding));
1103 ok = 0;
1104 }
1105
1106 return ok;
d8f8dca1
MM
1107}
1108
0034cf72
JM
1109/* Bind DECL to ID in the current_binding_level.
1110 If PUSH_USING is set in FLAGS, we know that DECL doesn't really belong
1111 to this binding level, that it got here through a using-declaration. */
f181d4ae
MM
1112
1113void
0034cf72 1114push_local_binding (id, decl, flags)
f181d4ae
MM
1115 tree id;
1116 tree decl;
0034cf72 1117 int flags;
f181d4ae 1118{
8f032717
MM
1119 struct binding_level *b;
1120
1121 /* Skip over any local classes. This makes sense if we call
1122 push_local_binding with a friend decl of a local class. */
1123 b = current_binding_level;
1124 while (b->parm_flag == 2)
1125 b = b->level_chain;
f181d4ae 1126
d8f8dca1 1127 if (lookup_name_current_level (id))
8f032717
MM
1128 {
1129 /* Supplement the existing binding. */
1130 if (!add_binding (id, decl))
1131 /* It didn't work. Something else must be bound at this
1132 level. Do not add DECL to the list of things to pop
1133 later. */
1134 return;
1135 }
d8f8dca1
MM
1136 else
1137 /* Create a new binding. */
8f032717 1138 push_binding (id, decl, b);
f181d4ae 1139
0034cf72 1140 if (TREE_CODE (decl) == OVERLOAD || (flags & PUSH_USING))
a06d48ef
JM
1141 /* We must put the OVERLOAD into a TREE_LIST since the
1142 TREE_CHAIN of an OVERLOAD is already used. Similarly for
1143 decls that got here through a using-declaration. */
1144 decl = build_tree_list (NULL_TREE, decl);
1145
f181d4ae
MM
1146 /* And put DECL on the list of things declared by the current
1147 binding level. */
8f032717
MM
1148 TREE_CHAIN (decl) = b->names;
1149 b->names = decl;
f181d4ae
MM
1150}
1151
8f032717
MM
1152/* Bind DECL to ID in the class_binding_level. Returns nonzero if the
1153 binding was successful. */
f181d4ae 1154
8f032717 1155int
f181d4ae
MM
1156push_class_binding (id, decl)
1157 tree id;
1158 tree decl;
1159{
8f032717
MM
1160 int result = 1;
1161 tree binding = IDENTIFIER_BINDING (id);
1162 tree context;
1163
1164 /* Note that we declared this value so that we can issue an error if
1165 this an illegal redeclaration of a name already used for some
1166 other purpose. */
1167 note_name_declared_in_class (id, decl);
1168
1169 if (binding && BINDING_LEVEL (binding) == class_binding_level)
d8f8dca1 1170 /* Supplement the existing binding. */
8f032717 1171 result = add_binding (id, decl);
d8f8dca1
MM
1172 else
1173 /* Create a new binding. */
1174 push_binding (id, decl, class_binding_level);
1175
1176 /* Update the IDENTIFIER_CLASS_VALUE for this ID to be the
1177 class-level declaration. Note that we do not use DECL here
1178 because of the possibility of the `struct stat' hack; if DECL is
1179 a class-name or enum-name we might prefer a field-name, or some
1180 such. */
1181 IDENTIFIER_CLASS_VALUE (id) = BINDING_VALUE (IDENTIFIER_BINDING (id));
8f032717
MM
1182
1183 /* If this is a binding from a base class, mark it as such. */
1184 binding = IDENTIFIER_BINDING (id);
1185 if (BINDING_VALUE (binding) == decl && TREE_CODE (decl) != TREE_LIST)
1186 {
280f9385
MM
1187 /* Any implicit typename must be from a base-class. The
1188 context for an implicit typename declaration is always
1189 the derived class in which the lookup was done, so the checks
1190 based on the context of DECL below will not trigger. */
83233dca 1191 if (IMPLICIT_TYPENAME_TYPE_DECL_P (decl))
280f9385 1192 INHERITED_VALUE_BINDING_P (binding) = 1;
8f032717
MM
1193 else
1194 {
280f9385
MM
1195 if (TREE_CODE (decl) == OVERLOAD)
1196 context = DECL_REAL_CONTEXT (OVL_CURRENT (decl));
1197 else
1198 {
1199 my_friendly_assert (TREE_CODE_CLASS (TREE_CODE (decl)) == 'd',
1200 0);
1201 context = DECL_REAL_CONTEXT (decl);
1202 }
8f032717 1203
280f9385
MM
1204 if (is_properly_derived_from (current_class_type, context))
1205 INHERITED_VALUE_BINDING_P (binding) = 1;
1206 else
1207 INHERITED_VALUE_BINDING_P (binding) = 0;
1208 }
8f032717
MM
1209 }
1210 else if (BINDING_VALUE (binding) == decl)
1211 /* We only encounter a TREE_LIST when push_class_decls detects an
1212 ambiguity. Such an ambiguity can be overridden by a definition
1213 in this class. */
1214 INHERITED_VALUE_BINDING_P (binding) = 1;
1215
1216 return result;
f181d4ae
MM
1217}
1218
d8f8dca1
MM
1219/* Remove the binding for DECL which should be the innermost binding
1220 for ID. */
f181d4ae
MM
1221
1222static void
d8f8dca1 1223pop_binding (id, decl)
f181d4ae 1224 tree id;
d8f8dca1 1225 tree decl;
f181d4ae
MM
1226{
1227 tree binding;
d8f8dca1 1228
f181d4ae
MM
1229 if (id == NULL_TREE)
1230 /* It's easiest to write the loops that call this function without
1231 checking whether or not the entities involved have names. We
1232 get here for such an entity. */
1233 return;
1234
d8f8dca1 1235 /* Get the innermost binding for ID. */
f181d4ae 1236 binding = IDENTIFIER_BINDING (id);
f181d4ae 1237
d8f8dca1
MM
1238 /* The name should be bound. */
1239 my_friendly_assert (binding != NULL_TREE, 0);
1240
1241 /* The DECL will be either the ordinary binding or the type
1242 binding for this identifier. Remove that binding. */
1243 if (BINDING_VALUE (binding) == decl)
1244 BINDING_VALUE (binding) = NULL_TREE;
1245 else if (BINDING_TYPE (binding) == decl)
1246 BINDING_TYPE (binding) = NULL_TREE;
1247 else
1248 my_friendly_abort (0);
1249
1250 if (!BINDING_VALUE (binding) && !BINDING_TYPE (binding))
1251 {
1252 /* We're completely done with the innermost binding for this
1253 identifier. Unhook it from the list of bindings. */
1254 IDENTIFIER_BINDING (id) = TREE_CHAIN (binding);
1255
1256 /* And place it on the free list. */
1257 TREE_CHAIN (binding) = free_binding_nodes;
1258 free_binding_nodes = binding;
1259 }
f181d4ae
MM
1260}
1261
8d08fdba
MS
1262/* Exit a binding level.
1263 Pop the level off, and restore the state of the identifier-decl mappings
1264 that were in effect when this level was entered.
1265
1266 If KEEP == 1, this level had explicit declarations, so
1267 and create a "block" (a BLOCK node) for the level
1268 to record its declarations and subblocks for symbol table output.
1269
8d08fdba
MS
1270 If FUNCTIONBODY is nonzero, this level is the body of a function,
1271 so create a block as if KEEP were set and also clear out all
1272 label names.
1273
1274 If REVERSE is nonzero, reverse the order of decls before putting
1275 them into the BLOCK. */
1276
1277tree
1278poplevel (keep, reverse, functionbody)
1279 int keep;
1280 int reverse;
1281 int functionbody;
1282{
1283 register tree link;
1284 /* The chain of decls was accumulated in reverse order.
1285 Put it into forward order, just for cleanliness. */
1286 tree decls;
1287 int tmp = functionbody;
8d08fdba
MS
1288 int real_functionbody = current_binding_level->keep == 2
1289 ? ((functionbody = 0), tmp) : functionbody;
1290 tree tags = functionbody >= 0 ? current_binding_level->tags : 0;
1291 tree subblocks = functionbody >= 0 ? current_binding_level->blocks : 0;
1292 tree block = NULL_TREE;
1293 tree decl;
1294 int block_previously_created;
f181d4ae
MM
1295 int leaving_for_scope;
1296
b74a0560
MM
1297 if (current_binding_level->parm_flag == 2)
1298 return poplevel_class ();
1299
1300 my_friendly_assert (!current_binding_level->class_shadowed,
1301 19990414);
8d08fdba 1302
536333d4
MM
1303 /* We used to use KEEP == 2 to indicate that the new block should go
1304 at the beginning of the list of blocks at this binding level,
1305 rather than the end. This hack is no longer used. */
1306 my_friendly_assert (keep == 0 || keep == 1, 0);
1307
8d08fdba
MS
1308 GNU_xref_end_scope ((HOST_WIDE_INT) current_binding_level,
1309 (HOST_WIDE_INT) current_binding_level->level_chain,
1310 current_binding_level->parm_flag,
5566b478 1311 current_binding_level->keep);
8d08fdba
MS
1312
1313 if (current_binding_level->keep == 1)
1314 keep = 1;
1315
8d08fdba
MS
1316 /* Get the decls in the order they were written.
1317 Usually current_binding_level->names is in reverse order.
1318 But parameter decls were previously put in forward order. */
1319
1320 if (reverse)
1321 current_binding_level->names
1322 = decls = nreverse (current_binding_level->names);
1323 else
1324 decls = current_binding_level->names;
1325
1326 /* Output any nested inline functions within this block
1327 if they weren't already output. */
1328
1329 for (decl = decls; decl; decl = TREE_CHAIN (decl))
1330 if (TREE_CODE (decl) == FUNCTION_DECL
1331 && ! TREE_ASM_WRITTEN (decl)
1332 && DECL_INITIAL (decl) != NULL_TREE
6060a796
MS
1333 && TREE_ADDRESSABLE (decl)
1334 && decl_function_context (decl) == current_function_decl)
8d08fdba
MS
1335 {
1336 /* If this decl was copied from a file-scope decl
1337 on account of a block-scope extern decl,
1338 propagate TREE_ADDRESSABLE to the file-scope decl. */
1339 if (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE)
1340 TREE_ADDRESSABLE (DECL_ABSTRACT_ORIGIN (decl)) = 1;
1341 else
1342 {
1343 push_function_context ();
1344 output_inline_function (decl);
1345 pop_function_context ();
1346 }
1347 }
1348
1349 /* If there were any declarations or structure tags in that level,
1350 or if this level is a function body,
1351 create a BLOCK to record them for the life of this function. */
1352
1353 block = NULL_TREE;
1354 block_previously_created = (current_binding_level->this_block != NULL_TREE);
1355 if (block_previously_created)
1356 block = current_binding_level->this_block;
1357 else if (keep == 1 || functionbody)
1358 block = make_node (BLOCK);
1359 if (block != NULL_TREE)
1360 {
72b7eeff
MS
1361 if (block_previously_created)
1362 {
1363 if (decls || tags || subblocks)
1364 {
be99da77 1365 if (BLOCK_VARS (block) || BLOCK_TYPE_TAGS (block))
f181d4ae
MM
1366 warning ("internal compiler error: debugging info corrupted");
1367
72b7eeff
MS
1368 BLOCK_VARS (block) = decls;
1369 BLOCK_TYPE_TAGS (block) = tags;
be99da77
MS
1370
1371 /* We can have previous subblocks and new subblocks when
1372 doing fixup_gotos with complex cleanups. We chain the new
1373 subblocks onto the end of any pre-existing subblocks. */
1374 BLOCK_SUBBLOCKS (block) = chainon (BLOCK_SUBBLOCKS (block),
1375 subblocks);
72b7eeff 1376 }
be99da77
MS
1377 /* If we created the block earlier on, and we are just
1378 diddling it now, then it already should have a proper
1379 BLOCK_END_NOTE value associated with it. */
72b7eeff
MS
1380 }
1381 else
1382 {
1383 BLOCK_VARS (block) = decls;
1384 BLOCK_TYPE_TAGS (block) = tags;
1385 BLOCK_SUBBLOCKS (block) = subblocks;
f181d4ae
MM
1386 /* Otherwise, for a new block, install a new BLOCK_END_NOTE
1387 value. */
72b7eeff
MS
1388 remember_end_note (block);
1389 }
8d08fdba
MS
1390 }
1391
1392 /* In each subblock, record that this is its superior. */
1393
1394 if (keep >= 0)
1395 for (link = subblocks; link; link = TREE_CHAIN (link))
1396 BLOCK_SUPERCONTEXT (link) = block;
1397
f181d4ae
MM
1398 /* We still support the old for-scope rules, whereby the variables
1399 in a for-init statement were in scope after the for-statement
1400 ended. We only use the new rules in flag_new_for_scope is
1401 nonzero. */
1402 leaving_for_scope
1403 = current_binding_level->is_for_scope && flag_new_for_scope == 1;
1404
1405 /* Remove declarations for all the DECLs in this level. */
1406 for (link = decls; link; link = TREE_CHAIN (link))
1407 {
1408 if (leaving_for_scope && TREE_CODE (link) == VAR_DECL)
1409 {
1410 tree outer_binding
1411 = TREE_CHAIN (IDENTIFIER_BINDING (DECL_NAME (link)));
1412 tree ns_binding;
1413
1414 if (!outer_binding)
1415 ns_binding = IDENTIFIER_NAMESPACE_VALUE (DECL_NAME (link));
c7dda1e3
MM
1416 else
1417 ns_binding = NULL_TREE;
1418
f181d4ae
MM
1419 if (outer_binding
1420 && (BINDING_LEVEL (outer_binding)
1421 == current_binding_level->level_chain))
1422 /* We have something like:
1423
1424 int i;
1425 for (int i; ;);
1426
1427 and we are leaving the `for' scope. There's no reason to
1428 keep the binding of the inner `i' in this case. */
d8f8dca1 1429 pop_binding (DECL_NAME (link), link);
f181d4ae
MM
1430 else if ((outer_binding
1431 && (TREE_CODE (BINDING_VALUE (outer_binding))
1432 == TYPE_DECL))
1433 || (ns_binding
1434 && TREE_CODE (ns_binding) == TYPE_DECL))
1435 /* Here, we have something like:
1436
1437 typedef int I;
1438
1439 void f () {
1440 for (int I; ;);
1441 }
1442
1443 We must pop the for-scope binding so we know what's a
1444 type and what isn't. */
d8f8dca1 1445 pop_binding (DECL_NAME (link), link);
e76a2646 1446 else
e76a2646 1447 {
f181d4ae
MM
1448 /* Mark this VAR_DECL as dead so that we can tell we left it
1449 there only for backward compatibility. */
1450 DECL_DEAD_FOR_LOCAL (link) = 1;
1451
1452 /* Keep track of what should of have happenned when we
1453 popped the binding. */
1454 if (outer_binding && BINDING_VALUE (outer_binding))
1455 DECL_SHADOWED_FOR_VAR (link)
1456 = BINDING_VALUE (outer_binding);
1457
1458 /* Add it to the list of dead variables in the next
1459 outermost binding to that we can remove these when we
1460 leave that binding. */
1461 current_binding_level->level_chain->dead_vars_from_for
1462 = tree_cons (NULL_TREE, link,
1463 current_binding_level->level_chain->
1464 dead_vars_from_for);
1465
1466 /* Although we don't pop the CPLUS_BINDING, we do clear
1467 its BINDING_LEVEL since the level is going away now. */
1468 BINDING_LEVEL (IDENTIFIER_BINDING (DECL_NAME (link)))
1469 = 0;
e76a2646 1470 }
2ee887f2 1471 }
f181d4ae 1472 else
8d6e462b 1473 {
f181d4ae 1474 /* Remove the binding. */
0034cf72
JM
1475 decl = link;
1476 if (TREE_CODE (decl) == TREE_LIST)
1477 decl = TREE_VALUE (decl);
1478 if (TREE_CODE_CLASS (TREE_CODE (decl)) == 'd')
1479 pop_binding (DECL_NAME (decl), decl);
1480 else if (TREE_CODE (decl) == OVERLOAD)
1481 pop_binding (DECL_NAME (OVL_FUNCTION (decl)), decl);
d8f8dca1 1482 else
f181d4ae 1483 my_friendly_abort (0);
8d08fdba 1484 }
f181d4ae 1485 }
8d08fdba 1486
f181d4ae
MM
1487 /* Remove declarations for any `for' variables from inner scopes
1488 that we kept around. */
1489 for (link = current_binding_level->dead_vars_from_for;
1490 link; link = TREE_CHAIN (link))
d8f8dca1 1491 pop_binding (DECL_NAME (TREE_VALUE (link)), TREE_VALUE (link));
2ee887f2 1492
f181d4ae
MM
1493 /* Restore the IDENTIFIER_TYPE_VALUEs. */
1494 for (link = current_binding_level->type_shadowed;
1495 link; link = TREE_CHAIN (link))
1496 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (link), TREE_VALUE (link));
1497
1498 /* There may be OVERLOADs (wrapped in TREE_LISTs) on the BLOCK_VARs
1499 list if a `using' declaration put them there. The debugging
1500 back-ends won't understand OVERLOAD, so we remove them here.
1501 Because the BLOCK_VARS are (temporarily) shared with
1502 CURRENT_BINDING_LEVEL->NAMES we must do this fixup after we have
1503 popped all the bindings. */
1504 if (block)
1505 {
1506 tree* d;
8d6e462b 1507
a06d48ef
JM
1508 for (d = &BLOCK_VARS (block); *d; )
1509 {
1510 if (TREE_CODE (*d) == TREE_LIST)
1511 *d = TREE_CHAIN (*d);
1512 else
1513 d = &TREE_CHAIN (*d);
1514 }
8d6e462b 1515 }
8d08fdba
MS
1516
1517 /* If the level being exited is the top level of a function,
1518 check over all the labels. */
1519
1520 if (functionbody)
1521 {
1522 /* If this is the top level block of a function,
1523 the vars are the function's parameters.
1524 Don't leave them in the BLOCK because they are
1525 found in the FUNCTION_DECL instead. */
1526
1527 BLOCK_VARS (block) = 0;
1528
1529 /* Clear out the definitions of all label names,
1530 since their scopes end here. */
1531
1532 for (link = named_labels; link; link = TREE_CHAIN (link))
1533 {
1534 register tree label = TREE_VALUE (link);
1535
1536 if (DECL_INITIAL (label) == NULL_TREE)
1537 {
8251199e 1538 cp_error_at ("label `%D' used but not defined", label);
8d08fdba
MS
1539 /* Avoid crashing later. */
1540 define_label (input_filename, 1, DECL_NAME (label));
1541 }
1542 else if (warn_unused && !TREE_USED (label))
8251199e 1543 cp_warning_at ("label `%D' defined but not used", label);
8d08fdba
MS
1544 SET_IDENTIFIER_LABEL_VALUE (DECL_NAME (label), NULL_TREE);
1545
1546 /* Put the labels into the "variables" of the
1547 top-level block, so debugger can see them. */
1548 TREE_CHAIN (label) = BLOCK_VARS (block);
1549 BLOCK_VARS (block) = label;
1550 }
1551
1552 named_labels = NULL_TREE;
1553 }
1554
1555 /* Any uses of undefined labels now operate under constraints
1556 of next binding contour. */
1557 {
1558 struct binding_level *level_chain;
1559 level_chain = current_binding_level->level_chain;
1560 if (level_chain)
1561 {
e349ee73
MS
1562 struct named_label_list *labels;
1563 for (labels = named_label_uses; labels; labels = labels->next)
1564 if (labels->binding_level == current_binding_level)
8d08fdba 1565 {
e349ee73
MS
1566 labels->binding_level = level_chain;
1567 labels->names_in_scope = level_chain->names;
8d08fdba
MS
1568 }
1569 }
1570 }
1571
1572 tmp = current_binding_level->keep;
1573
1574 pop_binding_level ();
1575 if (functionbody)
1576 DECL_INITIAL (current_function_decl) = block;
1577 else if (block)
1578 {
1579 if (!block_previously_created)
1580 current_binding_level->blocks
1581 = chainon (current_binding_level->blocks, block);
1582 }
1583 /* If we did not make a block for the level just exited,
1584 any blocks made for inner levels
1585 (since they cannot be recorded as subblocks in that level)
1586 must be carried forward so they will later become subblocks
1587 of something else. */
1588 else if (subblocks)
536333d4
MM
1589 current_binding_level->blocks
1590 = chainon (current_binding_level->blocks, subblocks);
8d08fdba
MS
1591
1592 /* Take care of compiler's internal binding structures. */
a4443a08 1593 if (tmp == 2)
8d08fdba 1594 {
8d08fdba
MS
1595 expand_end_bindings (getdecls (), keep, 1);
1596 /* Each and every BLOCK node created here in `poplevel' is important
1597 (e.g. for proper debugging information) so if we created one
1598 earlier, mark it as "used". */
1599 if (block)
1600 TREE_USED (block) = 1;
1601 block = poplevel (keep, reverse, real_functionbody);
1602 }
1603
1604 /* Each and every BLOCK node created here in `poplevel' is important
1605 (e.g. for proper debugging information) so if we created one
1606 earlier, mark it as "used". */
1607 if (block)
1608 TREE_USED (block) = 1;
1609 return block;
1610}
1611
1612/* Delete the node BLOCK from the current binding level.
1613 This is used for the block inside a stmt expr ({...})
1614 so that the block can be reinserted where appropriate. */
1615
1616void
1617delete_block (block)
1618 tree block;
1619{
1620 tree t;
1621 if (current_binding_level->blocks == block)
1622 current_binding_level->blocks = TREE_CHAIN (block);
1623 for (t = current_binding_level->blocks; t;)
1624 {
1625 if (TREE_CHAIN (t) == block)
1626 TREE_CHAIN (t) = TREE_CHAIN (block);
1627 else
1628 t = TREE_CHAIN (t);
1629 }
1630 TREE_CHAIN (block) = NULL_TREE;
1631 /* Clear TREE_USED which is always set by poplevel.
1632 The flag is set again if insert_block is called. */
1633 TREE_USED (block) = 0;
1634}
1635
1636/* Insert BLOCK at the end of the list of subblocks of the
1637 current binding level. This is used when a BIND_EXPR is expanded,
1638 to handle the BLOCK node inside the BIND_EXPR. */
1639
1640void
1641insert_block (block)
1642 tree block;
1643{
1644 TREE_USED (block) = 1;
1645 current_binding_level->blocks
1646 = chainon (current_binding_level->blocks, block);
1647}
1648
8d08fdba
MS
1649/* Set the BLOCK node for the innermost scope
1650 (the one we are currently in). */
1651
1652void
1653set_block (block)
1654 register tree block;
1655{
1656 current_binding_level->this_block = block;
1657}
1658
1659/* Do a pushlevel for class declarations. */
e92cc029 1660
8d08fdba
MS
1661void
1662pushlevel_class ()
1663{
1664 register struct binding_level *newlevel;
1665
1666 /* Reuse or create a struct for this binding level. */
1667#if defined(DEBUG_CP_BINDING_LEVELS)
1668 if (0)
1669#else /* !defined(DEBUG_CP_BINDING_LEVELS) */
1670 if (free_binding_level)
1671#endif /* !defined(DEBUG_CP_BINDING_LEVELS) */
1672 {
1673 newlevel = free_binding_level;
1674 free_binding_level = free_binding_level->level_chain;
1675 }
1676 else
f181d4ae 1677 newlevel = make_binding_level ();
8d08fdba
MS
1678
1679#if defined(DEBUG_CP_BINDING_LEVELS)
1680 is_class_level = 1;
1681#endif /* defined(DEBUG_CP_BINDING_LEVELS) */
1682
1683 push_binding_level (newlevel, 0, 0);
1684
1685 decl_stack = push_decl_level (decl_stack, &decl_obstack);
1686 class_binding_level = current_binding_level;
1687 class_binding_level->parm_flag = 2;
8d08fdba
MS
1688}
1689
b74a0560 1690/* ...and a poplevel for class declarations. */
e92cc029 1691
b74a0560
MM
1692static tree
1693poplevel_class ()
8d08fdba
MS
1694{
1695 register struct binding_level *level = class_binding_level;
8d08fdba
MS
1696 tree shadowed;
1697
1698 my_friendly_assert (level != 0, 354);
1699
1700 decl_stack = pop_stack_level (decl_stack);
8d08fdba 1701 /* If we're leaving a toplevel class, don't bother to do the setting
ddd5a7c1 1702 of IDENTIFIER_CLASS_VALUE to NULL_TREE, since first of all this slot
8d08fdba 1703 shouldn't even be used when current_class_type isn't set, and second,
ddd5a7c1 1704 if we don't touch it here, we're able to use the cache effect if the
8d08fdba 1705 next time we're entering a class scope, it is the same class. */
b74a0560 1706 if (current_class_depth != 1)
8f032717
MM
1707 {
1708 struct binding_level* b;
1709
1710 /* Clear out our IDENTIFIER_CLASS_VALUEs. */
1711 for (shadowed = level->class_shadowed;
1712 shadowed;
1713 shadowed = TREE_CHAIN (shadowed))
1714 IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (shadowed)) = NULL_TREE;
1715
1716 /* Find the next enclosing class, and recreate
1717 IDENTIFIER_CLASS_VALUEs appropriate for that class. */
1718 b = level->level_chain;
1719 while (b && b->parm_flag != 2)
1720 b = b->level_chain;
1721
1722 if (b)
1723 for (shadowed = b->class_shadowed;
1724 shadowed;
1725 shadowed = TREE_CHAIN (shadowed))
1726 {
1727 tree t;
1728
1729 t = IDENTIFIER_BINDING (TREE_PURPOSE (shadowed));
1730 while (t && BINDING_LEVEL (t) != b)
1731 t = TREE_CHAIN (t);
1732
1733 if (t)
1734 IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (shadowed))
1735 = BINDING_VALUE (t);
1736 }
1737 }
8d08fdba
MS
1738 else
1739 /* Remember to save what IDENTIFIER's were bound in this scope so we
1740 can recover from cache misses. */
e76a2646
MS
1741 {
1742 previous_class_type = current_class_type;
1743 previous_class_values = class_binding_level->class_shadowed;
1744 }
8d08fdba
MS
1745 for (shadowed = level->type_shadowed;
1746 shadowed;
1747 shadowed = TREE_CHAIN (shadowed))
2c73f9f5 1748 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (shadowed), TREE_VALUE (shadowed));
8d08fdba 1749
f181d4ae
MM
1750 /* Remove the bindings for all of the class-level declarations. */
1751 for (shadowed = level->class_shadowed;
1752 shadowed;
1753 shadowed = TREE_CHAIN (shadowed))
d8f8dca1 1754 pop_binding (TREE_PURPOSE (shadowed), TREE_TYPE (shadowed));
f181d4ae 1755
8d08fdba
MS
1756 GNU_xref_end_scope ((HOST_WIDE_INT) class_binding_level,
1757 (HOST_WIDE_INT) class_binding_level->level_chain,
1758 class_binding_level->parm_flag,
5566b478 1759 class_binding_level->keep);
8d08fdba 1760
38e01259 1761 /* Now, pop out of the binding level which we created up in the
8d08fdba
MS
1762 `pushlevel_class' routine. */
1763#if defined(DEBUG_CP_BINDING_LEVELS)
1764 is_class_level = 1;
1765#endif /* defined(DEBUG_CP_BINDING_LEVELS) */
1766
1767 pop_binding_level ();
1768
b74a0560 1769 return NULL_TREE;
8d08fdba 1770}
8f032717
MM
1771
1772/* We are entering the scope of a class. Clear IDENTIFIER_CLASS_VALUE
1773 for any names in enclosing classes. */
1774
1775void
1776clear_identifier_class_values ()
1777{
1778 tree t;
1779
1780 if (!class_binding_level)
1781 return;
1782
1783 for (t = class_binding_level->class_shadowed;
1784 t;
1785 t = TREE_CHAIN (t))
1786 IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (t)) = NULL_TREE;
1787}
1788
0aafb128
MM
1789/* Returns non-zero if T is a virtual function table. */
1790
1791int
1792vtable_decl_p (t, data)
1793 tree t;
1794 void *data ATTRIBUTE_UNUSED;
1795{
1796 return (TREE_CODE (t) == VAR_DECL && DECL_VIRTUAL_P (t));
1797}
1798
1799/* Returns non-zero if T is a TYPE_DECL for a type with virtual
1800 functions. */
1801
1802int
1803vtype_decl_p (t, data)
1804 tree t;
1805 void *data ATTRIBUTE_UNUSED;
1806{
1807 return (TREE_CODE (t) == TYPE_DECL
1808 && TREE_TYPE (t) != error_mark_node
1809 && TYPE_LANG_SPECIFIC (TREE_TYPE (t))
1810 && CLASSTYPE_VSIZE (TREE_TYPE (t)));
1811}
1812
1813/* Returns non-zero if T is a signature table. */
1814
1815int
1816sigtable_decl_p (t, data)
1817 tree t;
1818 void *data ATTRIBUTE_UNUSED;
1819{
1820 return (TREE_CODE (t) == VAR_DECL
1821 && TREE_TYPE (t) != error_mark_node
1822 && IS_SIGNATURE (TREE_TYPE (t)));
1823}
1824
f71f87f9
MM
1825/* Return the declarations that are members of the namespace NS. */
1826
1827tree
1828cp_namespace_decls (ns)
1829 tree ns;
1830{
1831 return NAMESPACE_LEVEL (ns)->names;
1832}
1833
0aafb128
MM
1834/* Walk all the namespaces contained NAMESPACE, including NAMESPACE
1835 itself, calling F for each. The DATA is passed to F as well. */
1836
1837static int
1838walk_namespaces_r (namespace, f, data)
1839 tree namespace;
1840 walk_namespaces_fn f;
1841 void *data;
1842{
1843 tree current;
1844 int result = 0;
1845
1846 result |= (*f) (namespace, data);
1847
f71f87f9 1848 for (current = cp_namespace_decls (namespace);
0aafb128
MM
1849 current;
1850 current = TREE_CHAIN (current))
1851 {
1852 if (TREE_CODE (current) != NAMESPACE_DECL
1853 || DECL_NAMESPACE_ALIAS (current))
1854 continue;
1855 if (!DECL_LANG_SPECIFIC (current))
1856 {
1857 /* Hmm. std. */
1858 my_friendly_assert (current == std_node, 393);
1859 continue;
1860 }
1861
1862 /* We found a namespace. */
1863 result |= walk_namespaces_r (current, f, data);
1864 }
1865
1866 return result;
1867}
1868
1869/* Walk all the namespaces, calling F for each. The DATA is passed to
1870 F as well. */
1871
1872int
1873walk_namespaces (f, data)
1874 walk_namespaces_fn f;
1875 void *data;
1876{
1877 return walk_namespaces_r (global_namespace, f, data);
1878}
1879
1880struct walk_globals_data {
1881 walk_globals_pred p;
1882 walk_globals_fn f;
1883 void *data;
1884};
1885
1886/* Walk the global declarations in NAMESPACE. Whenever one is found
1887 for which P returns non-zero, call F with its address. If any call
1888 to F returns a non-zero value, return a non-zero value. */
1889
1890static int
1891walk_globals_r (namespace, data)
1892 tree namespace;
1893 void *data;
1894{
1895 struct walk_globals_data* wgd = (struct walk_globals_data *) data;
1896 walk_globals_pred p = wgd->p;
1897 walk_globals_fn f = wgd->f;
1898 void *d = wgd->data;
1899 tree *t;
1900 int result = 0;
1901
1902 t = &NAMESPACE_LEVEL (namespace)->names;
1903
1904 while (*t)
1905 {
1906 tree glbl = *t;
1907
1908 if ((*p) (glbl, d))
1909 result |= (*f) (t, d);
1910
1911 /* If F changed *T, then *T still points at the next item to
1912 examine. */
1913 if (*t == glbl)
1914 t = &TREE_CHAIN (*t);
1915 }
1916
1917 return result;
1918}
1919
1920/* Walk the global declarations. Whenever one is found for which P
1921 returns non-zero, call F with its address. If any call to F
1922 returns a non-zero value, return a non-zero value. */
1923
1924int
1925walk_globals (p, f, data)
1926 walk_globals_pred p;
1927 walk_globals_fn f;
1928 void *data;
1929{
1930 struct walk_globals_data wgd;
1931 wgd.p = p;
1932 wgd.f = f;
1933 wgd.data = data;
1934
1935 return walk_namespaces (walk_globals_r, &wgd);
1936}
1937
1938/* Call wrapup_globals_declarations for the globals in NAMESPACE. If
1939 DATA is non-NULL, this is the last time we will call
1940 wrapup_global_declarations for this NAMESPACE. */
1941
1942int
1943wrapup_globals_for_namespace (namespace, data)
1944 tree namespace;
1945 void *data;
1946{
f71f87f9 1947 tree globals = cp_namespace_decls (namespace);
0aafb128
MM
1948 int len = list_length (globals);
1949 tree *vec = (tree *) alloca (sizeof (tree) * len);
1950 int i;
0352cfc8 1951 int result;
0aafb128
MM
1952 tree decl;
1953 int last_time = (data != 0);
1954
1955 if (last_time && namespace == global_namespace)
1956 /* Let compile_file handle the global namespace. */
1957 return 0;
1958
1959 /* Process the decls in reverse order--earliest first.
1960 Put them into VEC from back to front, then take out from front. */
1961
1962 for (i = 0, decl = globals; i < len; i++, decl = TREE_CHAIN (decl))
1963 vec[len - i - 1] = decl;
1964
0352cfc8
MM
1965 if (last_time)
1966 {
1967 check_global_declarations (vec, len);
1968 return 0;
1969 }
1970
1971 /* Temporarily mark vtables as external. That prevents
1972 wrapup_global_declarations from writing them out; we must process
1973 them ourselves in finish_vtable_vardecl. */
1974 for (i = 0; i < len; ++i)
91f2aa22 1975 if (vtable_decl_p (vec[i], /*data=*/0) && !DECL_EXTERNAL (vec[i]))
0352cfc8
MM
1976 {
1977 DECL_NOT_REALLY_EXTERN (vec[i]) = 1;
1978 DECL_EXTERNAL (vec[i]) = 1;
1979 }
0aafb128 1980
0352cfc8
MM
1981 /* Write out any globals that need to be output. */
1982 result = wrapup_global_declarations (vec, len);
1983
1984 /* Undo the hack to DECL_EXTERNAL above. */
1985 for (i = 0; i < len; ++i)
1986 if (vtable_decl_p (vec[i], /*data=*/0)
1987 && DECL_NOT_REALLY_EXTERN (vec[i]))
1988 {
1989 DECL_NOT_REALLY_EXTERN (vec[i]) = 0;
1990 DECL_EXTERNAL (vec[i]) = 0;
1991 }
1992
1993 return result;
0aafb128
MM
1994}
1995
8d08fdba
MS
1996\f
1997/* For debugging. */
5566b478
MS
1998static int no_print_functions = 0;
1999static int no_print_builtins = 0;
8d08fdba
MS
2000
2001void
2002print_binding_level (lvl)
2003 struct binding_level *lvl;
2004{
2005 tree t;
2006 int i = 0, len;
2007 fprintf (stderr, " blocks=");
2008 fprintf (stderr, HOST_PTR_PRINTF, lvl->blocks);
2009 fprintf (stderr, " n_incomplete=%d parm_flag=%d keep=%d",
f30432d7 2010 list_length (lvl->incomplete), lvl->parm_flag, lvl->keep);
8d08fdba
MS
2011 if (lvl->tag_transparent)
2012 fprintf (stderr, " tag-transparent");
2013 if (lvl->more_cleanups_ok)
2014 fprintf (stderr, " more-cleanups-ok");
2015 if (lvl->have_cleanups)
2016 fprintf (stderr, " have-cleanups");
8d08fdba
MS
2017 fprintf (stderr, "\n");
2018 if (lvl->names)
2019 {
2020 fprintf (stderr, " names:\t");
2021 /* We can probably fit 3 names to a line? */
2022 for (t = lvl->names; t; t = TREE_CHAIN (t))
2023 {
fc378698 2024 if (no_print_functions && (TREE_CODE (t) == FUNCTION_DECL))
8d08fdba
MS
2025 continue;
2026 if (no_print_builtins
fc378698
MS
2027 && (TREE_CODE (t) == TYPE_DECL)
2028 && (!strcmp (DECL_SOURCE_FILE (t),"<built-in>")))
8d08fdba
MS
2029 continue;
2030
2031 /* Function decls tend to have longer names. */
2032 if (TREE_CODE (t) == FUNCTION_DECL)
2033 len = 3;
2034 else
2035 len = 2;
2036 i += len;
2037 if (i > 6)
2038 {
2039 fprintf (stderr, "\n\t");
2040 i = len;
2041 }
2042 print_node_brief (stderr, "", t, 0);
bd6dd845 2043 if (t == error_mark_node)
8d08fdba
MS
2044 break;
2045 }
2046 if (i)
2047 fprintf (stderr, "\n");
2048 }
2049 if (lvl->tags)
2050 {
2051 fprintf (stderr, " tags:\t");
2052 i = 0;
2053 for (t = lvl->tags; t; t = TREE_CHAIN (t))
2054 {
2055 if (TREE_PURPOSE (t) == NULL_TREE)
2056 len = 3;
2057 else if (TREE_PURPOSE (t) == TYPE_IDENTIFIER (TREE_VALUE (t)))
2058 len = 2;
2059 else
2060 len = 4;
2061 i += len;
2062 if (i > 5)
2063 {
2064 fprintf (stderr, "\n\t");
2065 i = len;
2066 }
2067 if (TREE_PURPOSE (t) == NULL_TREE)
2068 {
2069 print_node_brief (stderr, "<unnamed-typedef", TREE_VALUE (t), 0);
2070 fprintf (stderr, ">");
2071 }
2072 else if (TREE_PURPOSE (t) == TYPE_IDENTIFIER (TREE_VALUE (t)))
2073 print_node_brief (stderr, "", TREE_VALUE (t), 0);
2074 else
2075 {
2076 print_node_brief (stderr, "<typedef", TREE_PURPOSE (t), 0);
2077 print_node_brief (stderr, "", TREE_VALUE (t), 0);
2078 fprintf (stderr, ">");
2079 }
2080 }
2081 if (i)
2082 fprintf (stderr, "\n");
2083 }
8d08fdba
MS
2084 if (lvl->class_shadowed)
2085 {
2086 fprintf (stderr, " class-shadowed:");
2087 for (t = lvl->class_shadowed; t; t = TREE_CHAIN (t))
2088 {
2089 fprintf (stderr, " %s ", IDENTIFIER_POINTER (TREE_PURPOSE (t)));
2090 }
2091 fprintf (stderr, "\n");
2092 }
2093 if (lvl->type_shadowed)
2094 {
2095 fprintf (stderr, " type-shadowed:");
2096 for (t = lvl->type_shadowed; t; t = TREE_CHAIN (t))
2097 {
8d08fdba 2098 fprintf (stderr, " %s ", IDENTIFIER_POINTER (TREE_PURPOSE (t)));
8d08fdba
MS
2099 }
2100 fprintf (stderr, "\n");
2101 }
2102}
2103
2104void
2105print_other_binding_stack (stack)
2106 struct binding_level *stack;
2107{
2108 struct binding_level *level;
2109 for (level = stack; level != global_binding_level; level = level->level_chain)
2110 {
2111 fprintf (stderr, "binding level ");
2112 fprintf (stderr, HOST_PTR_PRINTF, level);
2113 fprintf (stderr, "\n");
2114 print_binding_level (level);
2115 }
2116}
2117
2118void
2119print_binding_stack ()
2120{
2121 struct binding_level *b;
2122 fprintf (stderr, "current_binding_level=");
2123 fprintf (stderr, HOST_PTR_PRINTF, current_binding_level);
2124 fprintf (stderr, "\nclass_binding_level=");
2125 fprintf (stderr, HOST_PTR_PRINTF, class_binding_level);
2126 fprintf (stderr, "\nglobal_binding_level=");
2127 fprintf (stderr, HOST_PTR_PRINTF, global_binding_level);
2128 fprintf (stderr, "\n");
2129 if (class_binding_level)
2130 {
2131 for (b = class_binding_level; b; b = b->level_chain)
2132 if (b == current_binding_level)
2133 break;
2134 if (b)
2135 b = class_binding_level;
2136 else
2137 b = current_binding_level;
2138 }
2139 else
2140 b = current_binding_level;
2141 print_other_binding_stack (b);
2142 fprintf (stderr, "global:\n");
2143 print_binding_level (global_binding_level);
2144}
a9aedbc2 2145
2c73f9f5
ML
2146/* Namespace binding access routines: The namespace_bindings field of
2147 the identifier is polymorphic, with three possible values:
2148 NULL_TREE, a list of CPLUS_BINDINGS, or any other tree_node
2149 indicating the BINDING_VALUE of global_namespace. */
30394414 2150
2c73f9f5
ML
2151/* Check whether the a binding for the name to scope is known.
2152 Assumes that the bindings of the name are already a list
2153 of bindings. Returns the binding found, or NULL_TREE. */
2154
2155static tree
2156find_binding (name, scope)
30394414
JM
2157 tree name;
2158 tree scope;
2159{
2c73f9f5 2160 tree iter, prev = NULL_TREE;
3e3f722c
ML
2161
2162 scope = ORIGINAL_NAMESPACE (scope);
2163
30394414
JM
2164 for (iter = IDENTIFIER_NAMESPACE_BINDINGS (name); iter;
2165 iter = TREE_CHAIN (iter))
2166 {
2167 my_friendly_assert (TREE_CODE (iter) == CPLUS_BINDING, 374);
2168 if (BINDING_SCOPE (iter) == scope)
2c73f9f5
ML
2169 {
2170 /* Move binding found to the fron of the list, so
2171 subsequent lookups will find it faster. */
2172 if (prev)
2173 {
2174 TREE_CHAIN (prev) = TREE_CHAIN (iter);
2175 TREE_CHAIN (iter) = IDENTIFIER_NAMESPACE_BINDINGS (name);
2176 IDENTIFIER_NAMESPACE_BINDINGS (name) = iter;
2177 }
2178 return iter;
2179 }
2180 prev = iter;
30394414 2181 }
2c73f9f5
ML
2182 return NULL_TREE;
2183}
2184
2185/* Always returns a binding for name in scope. If the
2186 namespace_bindings is not a list, convert it to one first.
2187 If no binding is found, make a new one. */
2188
2189tree
2190binding_for_name (name, scope)
2191 tree name;
2192 tree scope;
2193{
2194 tree b = IDENTIFIER_NAMESPACE_BINDINGS (name);
2195 tree result;
3e3f722c
ML
2196
2197 scope = ORIGINAL_NAMESPACE (scope);
2198
2c73f9f5
ML
2199 if (b && TREE_CODE (b) != CPLUS_BINDING)
2200 {
2201 /* Get rid of optimization for global scope. */
2202 IDENTIFIER_NAMESPACE_BINDINGS (name) = NULL_TREE;
2203 BINDING_VALUE (binding_for_name (name, global_namespace)) = b;
2204 b = IDENTIFIER_NAMESPACE_BINDINGS (name);
2205 }
2206 if (b && (result = find_binding (name, scope)))
2207 return result;
2208 /* Not found, make a new permanent one. */
30394414 2209 push_obstacks (&permanent_obstack, &permanent_obstack);
2c73f9f5
ML
2210 result = make_node (CPLUS_BINDING);
2211 TREE_CHAIN (result) = b;
2212 IDENTIFIER_NAMESPACE_BINDINGS (name) = result;
2213 BINDING_SCOPE (result) = scope;
2214 BINDING_TYPE (result) = NULL_TREE;
2215 BINDING_VALUE (result) = NULL_TREE;
30394414 2216 pop_obstacks ();
2c73f9f5
ML
2217 return result;
2218}
2219
2220/* Return the binding value for name in scope, considering that
2221 namespace_binding may or may not be a list of CPLUS_BINDINGS. */
2222
2223tree
2224namespace_binding (name, scope)
2225 tree name;
2226 tree scope;
2227{
2228 tree b = IDENTIFIER_NAMESPACE_BINDINGS (name);
2229 if (b == NULL_TREE)
2230 return NULL_TREE;
cb0dbb9a
JM
2231 if (scope == NULL_TREE)
2232 scope = global_namespace;
2c73f9f5
ML
2233 if (TREE_CODE (b) != CPLUS_BINDING)
2234 return (scope == global_namespace) ? b : NULL_TREE;
2235 name = find_binding (name,scope);
2236 if (name == NULL_TREE)
2237 return name;
2238 return BINDING_VALUE (name);
2239}
2240
2241/* Set the binding value for name in scope. If modifying the binding
2242 of global_namespace is attempted, try to optimize it. */
2243
2244void
2245set_namespace_binding (name, scope, val)
2246 tree name;
2247 tree scope;
2248 tree val;
2249{
2250 tree b;
cb0dbb9a
JM
2251
2252 if (scope == NULL_TREE)
2253 scope = global_namespace;
2254
2c73f9f5
ML
2255 if (scope == global_namespace)
2256 {
2257 b = IDENTIFIER_NAMESPACE_BINDINGS (name);
2258 if (b == NULL_TREE || TREE_CODE (b) != CPLUS_BINDING)
2259 {
2260 IDENTIFIER_NAMESPACE_BINDINGS (name) = val;
2261 return;
2262 }
2263 }
2264 b = binding_for_name (name, scope);
2265 BINDING_VALUE (b) = val;
30394414
JM
2266}
2267
dff6b454
RK
2268/* Push into the scope of the NAME namespace. If NAME is NULL_TREE, then we
2269 select a name that is unique to this compilation unit. */
e92cc029 2270
a9aedbc2
MS
2271void
2272push_namespace (name)
2273 tree name;
2274{
b370501f 2275 tree d = NULL_TREE;
30394414
JM
2276 int need_new = 1;
2277 int implicit_use = 0;
2c73f9f5 2278 int global = 0;
30394414
JM
2279 if (!global_namespace)
2280 {
2c73f9f5 2281 /* This must be ::. */
30394414 2282 my_friendly_assert (name == get_identifier ("::"), 377);
2c73f9f5 2283 global = 1;
30394414
JM
2284 }
2285 else if (!name)
2286 {
3ab52652
ML
2287 /* The name of anonymous namespace is unique for the translation
2288 unit. */
0c8feefe
MM
2289 if (!anonymous_namespace_name)
2290 anonymous_namespace_name = get_file_function_name ('N');
2291 name = anonymous_namespace_name;
3ab52652
ML
2292 d = IDENTIFIER_NAMESPACE_VALUE (name);
2293 if (d)
2294 /* Reopening anonymous namespace. */
2295 need_new = 0;
30394414 2296 implicit_use = 1;
2c73f9f5
ML
2297 }
2298 else if (current_namespace == global_namespace
2299 && name == DECL_NAME (std_node))
2300 {
2301 in_std++;
2302 return;
2303 }
30394414
JM
2304 else
2305 {
2c73f9f5 2306 /* Check whether this is an extended namespace definition. */
30394414
JM
2307 d = IDENTIFIER_NAMESPACE_VALUE (name);
2308 if (d != NULL_TREE && TREE_CODE (d) == NAMESPACE_DECL)
3e3f722c
ML
2309 {
2310 need_new = 0;
2311 if (DECL_NAMESPACE_ALIAS (d))
2312 {
8251199e 2313 cp_error ("namespace alias `%D' not allowed here, assuming `%D'",
3e3f722c
ML
2314 d, DECL_NAMESPACE_ALIAS (d));
2315 d = DECL_NAMESPACE_ALIAS (d);
2316 }
2317 }
30394414 2318 }
6633d636 2319
30394414
JM
2320 if (need_new)
2321 {
2c73f9f5 2322 /* Make a new namespace, binding the name to it. */
6b57ac29 2323 d = build_lang_decl (NAMESPACE_DECL, name, void_type_node);
2c73f9f5
ML
2324 /* The global namespace is not pushed, and the global binding
2325 level is set elsewhere. */
2326 if (!global)
2327 {
2328 d = pushdecl (d);
2329 pushlevel (0);
2330 declare_namespace_level ();
2331 NAMESPACE_LEVEL (d) = current_binding_level;
2332 }
30394414 2333 }
2c73f9f5
ML
2334 else
2335 resume_binding_level (NAMESPACE_LEVEL (d));
2336
30394414
JM
2337 if (implicit_use)
2338 do_using_directive (d);
2c73f9f5 2339 /* Enter the name space. */
30394414 2340 current_namespace = d;
a9aedbc2
MS
2341}
2342
2343/* Pop from the scope of the current namespace. */
e92cc029 2344
a9aedbc2
MS
2345void
2346pop_namespace ()
2347{
2c73f9f5
ML
2348 if (current_namespace == global_namespace)
2349 {
2350 my_friendly_assert (in_std>0, 980421);
2351 in_std--;
2352 return;
2353 }
cb0dbb9a 2354 current_namespace = CP_DECL_CONTEXT (current_namespace);
2c73f9f5
ML
2355 /* The binding level is not popped, as it might be re-opened later. */
2356 suspend_binding_level ();
2357}
a9aedbc2 2358
61fc8c9e
MM
2359/* Push into the scope of the namespace NS, even if it is deeply
2360 nested within another namespace. */
2361
2362void
2363push_nested_namespace (ns)
2364 tree ns;
2365{
2366 if (ns == global_namespace)
2367 push_to_top_level ();
2368 else
2369 {
2370 push_nested_namespace (CP_DECL_CONTEXT (ns));
2371 push_namespace (DECL_NAME (ns));
2372 }
2373}
2374
2375/* Pop back from the scope of the namespace NS, which was previously
2376 entered with push_nested_namespace. */
2377
2378void
2379pop_nested_namespace (ns)
2380 tree ns;
2381{
2382 while (ns != global_namespace)
2383 {
2384 pop_namespace ();
2385 ns = CP_DECL_CONTEXT (ns);
2386 }
2387
2388 pop_from_top_level ();
2389}
2390
8d08fdba
MS
2391\f
2392/* Subroutines for reverting temporarily to top-level for instantiation
2393 of templates and such. We actually need to clear out the class- and
2394 local-value slots of all identifiers, so that only the global values
2395 are at all visible. Simply setting current_binding_level to the global
2396 scope isn't enough, because more binding levels may be pushed. */
2397struct saved_scope {
2398 struct binding_level *old_binding_level;
2399 tree old_bindings;
2c73f9f5 2400 tree old_namespace;
8d08fdba 2401 struct saved_scope *prev;
61a127b3
MM
2402 tree class_name, class_type;
2403 tree access_specifier;
2404 tree function_decl;
8d08fdba 2405 struct binding_level *class_bindings;
51c184be
MS
2406 tree *lang_base, *lang_stack, lang_name;
2407 int lang_stacksize;
5566b478
MS
2408 int minimal_parse_mode;
2409 tree last_function_parms;
e76a2646 2410 tree template_parms;
5156628f 2411 HOST_WIDE_INT processing_template_decl;
a50f0918 2412 tree previous_class_type, previous_class_values;
e1467ff2
MM
2413 int processing_specialization;
2414 int processing_explicit_instantiation;
3f1892e1 2415 char *class_cache_firstobj;
8d08fdba
MS
2416};
2417static struct saved_scope *current_saved_scope;
8d08fdba 2418
78957a2a
JM
2419/* A chain of the binding vecs created by store_bindings. We create a
2420 whole bunch of these during compilation, on permanent_obstack, so we
2421 can't just throw them away. */
2422static tree free_binding_vecs;
2423
e92cc029 2424static tree
45537677
MS
2425store_bindings (names, old_bindings)
2426 tree names, old_bindings;
2427{
2428 tree t;
2429 for (t = names; t; t = TREE_CHAIN (t))
2430 {
2431 tree binding, t1, id;
2432
2433 if (TREE_CODE (t) == TREE_LIST)
2434 id = TREE_PURPOSE (t);
2435 else
2436 id = DECL_NAME (t);
2437
f181d4ae
MM
2438 if (!id
2439 /* Note that we may have an IDENTIFIER_CLASS_VALUE even when
2440 we have no IDENTIFIER_BINDING if we have left the class
2441 scope, but cached the class-level declarations. */
2442 || !(IDENTIFIER_BINDING (id) || IDENTIFIER_CLASS_VALUE (id)))
45537677
MS
2443 continue;
2444
2445 for (t1 = old_bindings; t1; t1 = TREE_CHAIN (t1))
2446 if (TREE_VEC_ELT (t1, 0) == id)
2447 goto skip_it;
78957a2a
JM
2448
2449 if (free_binding_vecs)
2450 {
2451 binding = free_binding_vecs;
2452 free_binding_vecs = TREE_CHAIN (free_binding_vecs);
2453 }
2454 else
2455 binding = make_tree_vec (4);
2456
45537677
MS
2457 if (id)
2458 {
2459 my_friendly_assert (TREE_CODE (id) == IDENTIFIER_NODE, 135);
2460 TREE_VEC_ELT (binding, 0) = id;
2c73f9f5 2461 TREE_VEC_ELT (binding, 1) = REAL_IDENTIFIER_TYPE_VALUE (id);
f181d4ae 2462 TREE_VEC_ELT (binding, 2) = IDENTIFIER_BINDING (id);
45537677 2463 TREE_VEC_ELT (binding, 3) = IDENTIFIER_CLASS_VALUE (id);
f181d4ae 2464 IDENTIFIER_BINDING (id) = NULL_TREE;
45537677
MS
2465 IDENTIFIER_CLASS_VALUE (id) = NULL_TREE;
2466 }
2467 TREE_CHAIN (binding) = old_bindings;
2468 old_bindings = binding;
2469 skip_it:
2470 ;
2471 }
2472 return old_bindings;
2473}
2474
8d08fdba 2475void
5566b478
MS
2476maybe_push_to_top_level (pseudo)
2477 int pseudo;
8d08fdba 2478{
51c184be 2479 extern int current_lang_stacksize;
beb53fb8
JM
2480 struct saved_scope *s
2481 = (struct saved_scope *) xmalloc (sizeof (struct saved_scope));
8f032717 2482 struct binding_level *b = current_binding_level;
8d08fdba
MS
2483 tree old_bindings = NULL_TREE;
2484
642b32a5 2485 push_cp_function_context (NULL_TREE);
e349ee73 2486
a50f0918
MS
2487 if (previous_class_type)
2488 old_bindings = store_bindings (previous_class_values, old_bindings);
2489
8d08fdba
MS
2490 /* Have to include global_binding_level, because class-level decls
2491 aren't listed anywhere useful. */
2492 for (; b; b = b->level_chain)
2493 {
2494 tree t;
2495
2c73f9f5
ML
2496 /* Template IDs are inserted into the global level. If they were
2497 inserted into namespace level, finish_file wouldn't find them
2498 when doing pending instantiations. Therefore, don't stop at
2499 namespace level, but continue until :: . */
5566b478
MS
2500 if (b == global_binding_level || (pseudo && b->pseudo_global))
2501 break;
8d08fdba 2502
45537677 2503 old_bindings = store_bindings (b->names, old_bindings);
cffa8729 2504 /* We also need to check class_shadowed to save class-level type
45537677
MS
2505 bindings, since pushclass doesn't fill in b->names. */
2506 if (b->parm_flag == 2)
cffa8729 2507 old_bindings = store_bindings (b->class_shadowed, old_bindings);
8d08fdba 2508
8d08fdba
MS
2509 /* Unwind type-value slots back to top level. */
2510 for (t = b->type_shadowed; t; t = TREE_CHAIN (t))
2511 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (t), TREE_VALUE (t));
2512 }
8d08fdba
MS
2513
2514 s->old_binding_level = current_binding_level;
5566b478 2515 current_binding_level = b;
8d08fdba 2516
2c73f9f5 2517 s->old_namespace = current_namespace;
8d08fdba
MS
2518 s->class_name = current_class_name;
2519 s->class_type = current_class_type;
61a127b3 2520 s->access_specifier = current_access_specifier;
8d08fdba
MS
2521 s->function_decl = current_function_decl;
2522 s->class_bindings = class_binding_level;
51c184be
MS
2523 s->lang_stack = current_lang_stack;
2524 s->lang_base = current_lang_base;
2525 s->lang_stacksize = current_lang_stacksize;
2526 s->lang_name = current_lang_name;
5566b478
MS
2527 s->minimal_parse_mode = minimal_parse_mode;
2528 s->last_function_parms = last_function_parms;
e76a2646 2529 s->template_parms = current_template_parms;
5156628f 2530 s->processing_template_decl = processing_template_decl;
a50f0918
MS
2531 s->previous_class_type = previous_class_type;
2532 s->previous_class_values = previous_class_values;
3f1892e1 2533 s->class_cache_firstobj = class_cache_firstobj;
e1467ff2
MM
2534 s->processing_specialization = processing_specialization;
2535 s->processing_explicit_instantiation = processing_explicit_instantiation;
e349ee73 2536
5f34005f 2537 current_class_name = current_class_type = NULL_TREE;
8d08fdba
MS
2538 current_function_decl = NULL_TREE;
2539 class_binding_level = (struct binding_level *)0;
51c184be
MS
2540 current_lang_stacksize = 10;
2541 current_lang_stack = current_lang_base
2542 = (tree *) xmalloc (current_lang_stacksize * sizeof (tree));
2543 current_lang_name = lang_name_cplusplus;
2544 strict_prototype = strict_prototypes_lang_cplusplus;
2545 named_labels = NULL_TREE;
120722ac 2546 shadowed_labels = NULL_TREE;
5566b478 2547 minimal_parse_mode = 0;
a50f0918 2548 previous_class_type = previous_class_values = NULL_TREE;
3f1892e1 2549 class_cache_firstobj = 0;
e1467ff2
MM
2550 processing_specialization = 0;
2551 processing_explicit_instantiation = 0;
eae97bd9
MM
2552 current_template_parms = NULL_TREE;
2553 processing_template_decl = 0;
2c73f9f5 2554 current_namespace = global_namespace;
8d08fdba
MS
2555
2556 s->prev = current_saved_scope;
2557 s->old_bindings = old_bindings;
2558 current_saved_scope = s;
5566b478
MS
2559
2560 push_obstacks (&permanent_obstack, &permanent_obstack);
2561}
2562
2563void
2564push_to_top_level ()
2565{
2566 maybe_push_to_top_level (0);
8d08fdba
MS
2567}
2568
2569void
2570pop_from_top_level ()
2571{
51c184be 2572 extern int current_lang_stacksize;
8d08fdba
MS
2573 struct saved_scope *s = current_saved_scope;
2574 tree t;
2575
e76a2646 2576 /* Clear out class-level bindings cache. */
8d08fdba 2577 if (previous_class_type)
8f032717 2578 invalidate_class_lookup_cache ();
8d08fdba 2579
5566b478
MS
2580 pop_obstacks ();
2581
8d08fdba
MS
2582 current_binding_level = s->old_binding_level;
2583 current_saved_scope = s->prev;
78957a2a 2584 for (t = s->old_bindings; t; )
8d08fdba 2585 {
78957a2a 2586 tree save = t;
8d08fdba
MS
2587 tree id = TREE_VEC_ELT (t, 0);
2588 if (id)
2589 {
2c73f9f5 2590 SET_IDENTIFIER_TYPE_VALUE (id, TREE_VEC_ELT (t, 1));
f181d4ae 2591 IDENTIFIER_BINDING (id) = TREE_VEC_ELT (t, 2);
8d08fdba
MS
2592 IDENTIFIER_CLASS_VALUE (id) = TREE_VEC_ELT (t, 3);
2593 }
78957a2a
JM
2594 t = TREE_CHAIN (t);
2595 TREE_CHAIN (save) = free_binding_vecs;
2596 free_binding_vecs = save;
8d08fdba 2597 }
2c73f9f5 2598 current_namespace = s->old_namespace;
8d08fdba
MS
2599 current_class_name = s->class_name;
2600 current_class_type = s->class_type;
61a127b3 2601 current_access_specifier = s->access_specifier;
8d08fdba
MS
2602 current_function_decl = s->function_decl;
2603 class_binding_level = s->class_bindings;
51c184be
MS
2604 free (current_lang_base);
2605 current_lang_base = s->lang_base;
2606 current_lang_stack = s->lang_stack;
2607 current_lang_name = s->lang_name;
2608 current_lang_stacksize = s->lang_stacksize;
2609 if (current_lang_name == lang_name_cplusplus)
2610 strict_prototype = strict_prototypes_lang_cplusplus;
2611 else if (current_lang_name == lang_name_c)
2612 strict_prototype = strict_prototypes_lang_c;
5566b478
MS
2613 minimal_parse_mode = s->minimal_parse_mode;
2614 last_function_parms = s->last_function_parms;
e76a2646 2615 current_template_parms = s->template_parms;
5156628f 2616 processing_template_decl = s->processing_template_decl;
a50f0918
MS
2617 previous_class_type = s->previous_class_type;
2618 previous_class_values = s->previous_class_values;
e1467ff2
MM
2619 processing_specialization = s->processing_specialization;
2620 processing_explicit_instantiation = s->processing_explicit_instantiation;
3f1892e1 2621 class_cache_firstobj = s->class_cache_firstobj;
51c184be 2622
8d08fdba 2623 free (s);
e349ee73 2624
642b32a5 2625 pop_cp_function_context (NULL_TREE);
8d08fdba
MS
2626}
2627\f
2628/* Push a definition of struct, union or enum tag "name".
2629 into binding_level "b". "type" should be the type node,
2630 We assume that the tag "name" is not already defined.
2631
2632 Note that the definition may really be just a forward reference.
2633 In that case, the TYPE_SIZE will be a NULL_TREE.
2634
e92cc029 2635 C++ gratuitously puts all these tags in the name space. */
8d08fdba
MS
2636
2637/* When setting the IDENTIFIER_TYPE_VALUE field of an identifier ID,
2638 record the shadowed value for this binding contour. TYPE is
2639 the type that ID maps to. */
2640
2641static void
2642set_identifier_type_value_with_scope (id, type, b)
2643 tree id;
2644 tree type;
2645 struct binding_level *b;
2646{
2c73f9f5 2647 if (!b->namespace_p)
8d08fdba 2648 {
2c73f9f5
ML
2649 /* Shadow the marker, not the real thing, so that the marker
2650 gets restored later. */
2651 tree old_type_value = REAL_IDENTIFIER_TYPE_VALUE (id);
8d08fdba
MS
2652 b->type_shadowed
2653 = tree_cons (id, old_type_value, b->type_shadowed);
2654 }
2c73f9f5
ML
2655 else
2656 {
2657 tree binding = binding_for_name (id, current_namespace);
2658 BINDING_TYPE (binding) = type;
2659 /* Store marker instead of real type. */
2660 type = global_type_node;
2661 }
8d08fdba
MS
2662 SET_IDENTIFIER_TYPE_VALUE (id, type);
2663}
2664
8f032717 2665/* As set_identifier_type_value_with_scope, but using current_binding_level. */
8d08fdba
MS
2666
2667void
2668set_identifier_type_value (id, type)
2669 tree id;
2670 tree type;
2671{
8f032717 2672 set_identifier_type_value_with_scope (id, type, current_binding_level);
8d08fdba
MS
2673}
2674
2c73f9f5
ML
2675/* Return the type associated with id. */
2676
2677tree
2678identifier_type_value (id)
2679 tree id;
2680{
2681 /* There is no type with that name, anywhere. */
2682 if (REAL_IDENTIFIER_TYPE_VALUE (id) == NULL_TREE)
2683 return NULL_TREE;
2684 /* This is not the type marker, but the real thing. */
2685 if (REAL_IDENTIFIER_TYPE_VALUE (id) != global_type_node)
2686 return REAL_IDENTIFIER_TYPE_VALUE (id);
2687 /* Have to search for it. It must be on the global level, now.
2688 Ask lookup_name not to return non-types. */
3e3f722c 2689 id = lookup_name_real (id, 2, 1, 0);
2c73f9f5
ML
2690 if (id)
2691 return TREE_TYPE (id);
2692 return NULL_TREE;
2693}
2694
a9aedbc2
MS
2695/* Pop off extraneous binding levels left over due to syntax errors.
2696
2697 We don't pop past namespaces, as they might be valid. */
e92cc029 2698
8926095f
MS
2699void
2700pop_everything ()
2701{
2702#ifdef DEBUG_CP_BINDING_LEVELS
2703 fprintf (stderr, "XXX entering pop_everything ()\n");
2704#endif
8f032717 2705 while (!toplevel_bindings_p ())
8926095f 2706 {
8f032717 2707 if (current_binding_level->parm_flag == 2)
b74a0560 2708 pop_nested_class ();
8926095f
MS
2709 else
2710 poplevel (0, 0, 0);
2711 }
2712#ifdef DEBUG_CP_BINDING_LEVELS
2713 fprintf (stderr, "XXX leaving pop_everything ()\n");
2714#endif
2715}
2716
39c01e4c
MM
2717/* The type TYPE is being declared. If it is a class template, or a
2718 specialization of a class template, do any processing required and
2719 perform error-checking. If IS_FRIEND is non-zero, this TYPE is
2720 being declared a friend. B is the binding level at which this TYPE
2721 should be bound.
2722
2723 Returns the TYPE_DECL for TYPE, which may have been altered by this
2724 processing. */
2725
2726static tree
2727maybe_process_template_type_declaration (type, globalize, b)
2728 tree type;
2729 int globalize;
2730 struct binding_level* b;
2731{
2732 tree decl = TYPE_NAME (type);
2733
2734 if (processing_template_parmlist)
2735 /* You can't declare a new template type in a template parameter
2736 list. But, you can declare a non-template type:
2737
2738 template <class A*> struct S;
2739
2740 is a forward-declaration of `A'. */
2741 ;
2742 else
2743 {
2744 maybe_check_template_type (type);
2745
ed44da02
MM
2746 my_friendly_assert (IS_AGGR_TYPE (type)
2747 || TREE_CODE (type) == ENUMERAL_TYPE, 0);
2748
2749
3ebc5c52 2750 if (processing_template_decl)
39c01e4c
MM
2751 {
2752 /* This may change after the call to
2753 push_template_decl_real, but we want the original value. */
2754 tree name = DECL_NAME (decl);
2755
2756 decl = push_template_decl_real (decl, globalize);
2757 /* If the current binding level is the binding level for the
2758 template parameters (see the comment in
2759 begin_template_parm_list) and the enclosing level is a class
2760 scope, and we're not looking at a friend, push the
2761 declaration of the member class into the class scope. In the
2762 friend case, push_template_decl will already have put the
2763 friend into global scope, if appropriate. */
ed44da02
MM
2764 if (TREE_CODE (type) != ENUMERAL_TYPE
2765 && !globalize && b->pseudo_global
39c01e4c
MM
2766 && b->level_chain->parm_flag == 2)
2767 {
61a127b3 2768 finish_member_declaration (CLASSTYPE_TI_TEMPLATE (type));
39c01e4c
MM
2769 /* Put this tag on the list of tags for the class, since
2770 that won't happen below because B is not the class
2771 binding level, but is instead the pseudo-global level. */
2772 b->level_chain->tags =
2773 saveable_tree_cons (name, type, b->level_chain->tags);
39c01e4c
MM
2774 if (TYPE_SIZE (current_class_type) == NULL_TREE)
2775 CLASSTYPE_TAGS (current_class_type) = b->level_chain->tags;
2776 }
2777 }
2778 }
2779
2780 return decl;
2781}
2782
8d08fdba 2783/* Push a tag name NAME for struct/class/union/enum type TYPE.
6757edfe 2784 Normally put it into the inner-most non-tag-transparent scope,
8d08fdba 2785 but if GLOBALIZE is true, put it in the inner-most non-class scope.
e92cc029 2786 The latter is needed for implicit declarations. */
8d08fdba
MS
2787
2788void
2789pushtag (name, type, globalize)
2790 tree name, type;
2791 int globalize;
2792{
2793 register struct binding_level *b;
8d08fdba 2794
8f032717 2795 b = current_binding_level;
8d08fdba
MS
2796 while (b->tag_transparent
2797 || (globalize && b->parm_flag == 2))
2798 b = b->level_chain;
2799
a9aedbc2 2800 if (toplevel_bindings_p ())
8d08fdba
MS
2801 b->tags = perm_tree_cons (name, type, b->tags);
2802 else
2803 b->tags = saveable_tree_cons (name, type, b->tags);
2804
2805 if (name)
2806 {
8d08fdba 2807 /* Do C++ gratuitous typedefing. */
db5ae43f 2808 if (IDENTIFIER_TYPE_VALUE (name) != type)
8d08fdba 2809 {
93cdc044 2810 register tree d = NULL_TREE;
848cf1e0 2811 int in_class = 0;
280f9385 2812 tree context;
280f9385
MM
2813
2814 context = type ? TYPE_CONTEXT (type) : NULL_TREE;
2815 if (! context)
2816 {
2817 tree cs = current_scope ();
2818
2819 if (! globalize)
2820 context = cs;
2821 else if (cs != NULL_TREE
2822 && TREE_CODE_CLASS (TREE_CODE (cs)) == 't')
2823 /* When declaring a friend class of a local class, we want
2824 to inject the newly named class into the scope
2825 containing the local class, not the namespace scope. */
2826 context = hack_decl_function_context (get_type_decl (cs));
2827 }
280f9385
MM
2828 if (!context)
2829 context = current_namespace;
8d08fdba 2830
93cdc044
JM
2831 if ((b->pseudo_global && b->level_chain->parm_flag == 2)
2832 || b->parm_flag == 2)
2833 in_class = 1;
93cdc044 2834
848cf1e0
MM
2835 d = build_decl (TYPE_DECL, name, type);
2836 if (current_lang_name == lang_name_java)
2837 TYPE_FOR_JAVA (type) = 1;
2838 SET_DECL_ARTIFICIAL (d);
2839 if (! in_class)
2840 set_identifier_type_value_with_scope (name, type, b);
93cdc044
JM
2841
2842 TYPE_NAME (type) = d;
cb0dbb9a 2843 DECL_CONTEXT (d) = FROB_CONTEXT (context);
e1cd6e56 2844
39c01e4c
MM
2845 d = maybe_process_template_type_declaration (type,
2846 globalize, b);
93cdc044
JM
2847
2848 if (b->parm_flag == 2)
61a127b3 2849 {
848cf1e0 2850 if (!PROCESSING_REAL_TEMPLATE_DECL_P ())
61a127b3
MM
2851 /* Put this TYPE_DECL on the TYPE_FIELDS list for the
2852 class. But if it's a member template class, we
2853 want the TEMPLATE_DECL, not the TYPE_DECL, so this
2854 is done later. */
2855 finish_member_declaration (d);
8f032717
MM
2856 else
2857 pushdecl_class_level (d);
61a127b3 2858 }
93cdc044
JM
2859 else
2860 d = pushdecl_with_scope (d, b);
2861
848cf1e0
MM
2862 if (ANON_AGGRNAME_P (name))
2863 DECL_IGNORED_P (d) = 1;
2864
2865 TYPE_CONTEXT (type) = DECL_CONTEXT (d);
2866 DECL_ASSEMBLER_NAME (d) = DECL_NAME (d);
2867 if (!uses_template_parms (type))
2868 DECL_ASSEMBLER_NAME (d)
2869 = get_identifier (build_overload_name (type, 1, 1));
8d08fdba
MS
2870 }
2871 if (b->parm_flag == 2)
2872 {
8d08fdba
MS
2873 if (TYPE_SIZE (current_class_type) == NULL_TREE)
2874 CLASSTYPE_TAGS (current_class_type) = b->tags;
2875 }
2876 }
2877
2878 if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL)
2879 /* Use the canonical TYPE_DECL for this node. */
2880 TYPE_STUB_DECL (type) = TYPE_NAME (type);
2881 else
2882 {
2883 /* Create a fake NULL-named TYPE_DECL node whose TREE_TYPE
2884 will be the tagged type we just added to the current
2885 binding level. This fake NULL-named TYPE_DECL node helps
2886 dwarfout.c to know when it needs to output a
2887 representation of a tagged type, and it also gives us a
2888 convenient place to record the "scope start" address for
2889 the tagged type. */
2890
8d08fdba 2891 tree d = build_decl (TYPE_DECL, NULL_TREE, type);
8d08fdba
MS
2892 TYPE_STUB_DECL (type) = pushdecl_with_scope (d, b);
2893 }
2894}
2895
2896/* Counter used to create anonymous type names. */
e92cc029 2897
8d08fdba
MS
2898static int anon_cnt = 0;
2899
2900/* Return an IDENTIFIER which can be used as a name for
2901 anonymous structs and unions. */
e92cc029 2902
8d08fdba
MS
2903tree
2904make_anon_name ()
2905{
2906 char buf[32];
2907
2908 sprintf (buf, ANON_AGGRNAME_FORMAT, anon_cnt++);
2909 return get_identifier (buf);
2910}
2911
2912/* Clear the TREE_PURPOSE slot of tags which have anonymous typenames.
2913 This keeps dbxout from getting confused. */
e92cc029 2914
8d08fdba
MS
2915void
2916clear_anon_tags ()
2917{
2918 register struct binding_level *b;
2919 register tree tags;
2920 static int last_cnt = 0;
2921
2922 /* Fast out if no new anon names were declared. */
2923 if (last_cnt == anon_cnt)
2924 return;
2925
2926 b = current_binding_level;
2927 while (b->tag_transparent)
2928 b = b->level_chain;
2929 tags = b->tags;
2930 while (tags)
2931 {
2932 /* A NULL purpose means we have already processed all tags
2933 from here to the end of the list. */
2934 if (TREE_PURPOSE (tags) == NULL_TREE)
2935 break;
2936 if (ANON_AGGRNAME_P (TREE_PURPOSE (tags)))
2937 TREE_PURPOSE (tags) = NULL_TREE;
2938 tags = TREE_CHAIN (tags);
2939 }
2940 last_cnt = anon_cnt;
2941}
2942\f
2943/* Subroutine of duplicate_decls: return truthvalue of whether
2944 or not types of these decls match.
2945
2946 For C++, we must compare the parameter list so that `int' can match
2947 `int&' in a parameter position, but `int&' is not confused with
2948 `const int&'. */
e92cc029 2949
6060a796 2950int
8d08fdba
MS
2951decls_match (newdecl, olddecl)
2952 tree newdecl, olddecl;
2953{
2954 int types_match;
2955
347d73d7
ML
2956 if (newdecl == olddecl)
2957 return 1;
2958
6b4b3deb
MM
2959 if (TREE_CODE (newdecl) != TREE_CODE (olddecl))
2960 /* If the two DECLs are not even the same kind of thing, we're not
2961 interested in their types. */
2962 return 0;
2963
2964 if (TREE_CODE (newdecl) == FUNCTION_DECL)
8d08fdba
MS
2965 {
2966 tree f1 = TREE_TYPE (newdecl);
2967 tree f2 = TREE_TYPE (olddecl);
2968 tree p1 = TYPE_ARG_TYPES (f1);
2969 tree p2 = TYPE_ARG_TYPES (f2);
2970
c5a6fc45
JM
2971 if (DECL_REAL_CONTEXT (newdecl) != DECL_REAL_CONTEXT (olddecl)
2972 && ! (DECL_LANGUAGE (newdecl) == lang_c
2973 && DECL_LANGUAGE (olddecl) == lang_c))
2974 return 0;
2975
8d08fdba
MS
2976 /* When we parse a static member function definition,
2977 we put together a FUNCTION_DECL which thinks its type
2978 is METHOD_TYPE. Change that to FUNCTION_TYPE, and
2979 proceed. */
2980 if (TREE_CODE (f1) == METHOD_TYPE && DECL_STATIC_FUNCTION_P (olddecl))
700f8a87 2981 revert_static_member_fn (&newdecl, &f1, &p1);
8d08fdba
MS
2982 else if (TREE_CODE (f2) == METHOD_TYPE
2983 && DECL_STATIC_FUNCTION_P (newdecl))
700f8a87 2984 revert_static_member_fn (&olddecl, &f2, &p2);
8d08fdba
MS
2985
2986 /* Here we must take care of the case where new default
2987 parameters are specified. Also, warn if an old
2988 declaration becomes ambiguous because default
2989 parameters may cause the two to be ambiguous. */
2990 if (TREE_CODE (f1) != TREE_CODE (f2))
2991 {
2992 if (TREE_CODE (f1) == OFFSET_TYPE)
8251199e 2993 cp_compiler_error ("`%D' redeclared as member function", newdecl);
8d08fdba 2994 else
8251199e 2995 cp_compiler_error ("`%D' redeclared as non-member function", newdecl);
8d08fdba
MS
2996 return 0;
2997 }
2998
3bfdc719 2999 if (same_type_p (TREE_TYPE (f1), TREE_TYPE (f2)))
8926095f 3000 {
a28e3c7f 3001 if (! strict_prototypes_lang_c && DECL_LANGUAGE (olddecl) == lang_c
8926095f 3002 && p2 == NULL_TREE)
a28e3c7f
MS
3003 {
3004 types_match = self_promoting_args_p (p1);
3005 if (p1 == void_list_node)
3006 TREE_TYPE (newdecl) = TREE_TYPE (olddecl);
3007 }
3008 else if (!strict_prototypes_lang_c && DECL_LANGUAGE (olddecl)==lang_c
3009 && DECL_LANGUAGE (newdecl) == lang_c && p1 == NULL_TREE)
3010 {
3011 types_match = self_promoting_args_p (p2);
3012 TREE_TYPE (newdecl) = TREE_TYPE (olddecl);
3013 }
8926095f 3014 else
91063b51 3015 types_match = compparms (p1, p2);
8926095f 3016 }
8d08fdba
MS
3017 else
3018 types_match = 0;
3019 }
6b4b3deb 3020 else if (TREE_CODE (newdecl) == TEMPLATE_DECL)
51c184be 3021 {
f84b4be9
JM
3022 if (!comp_template_parms (DECL_TEMPLATE_PARMS (newdecl),
3023 DECL_TEMPLATE_PARMS (olddecl)))
3024 return 0;
3025
3026 if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL)
3027 types_match = 1;
3028 else
3029 types_match = decls_match (DECL_TEMPLATE_RESULT (olddecl),
3030 DECL_TEMPLATE_RESULT (newdecl));
51c184be 3031 }
8d08fdba
MS
3032 else
3033 {
3034 if (TREE_TYPE (newdecl) == error_mark_node)
3035 types_match = TREE_TYPE (olddecl) == error_mark_node;
3036 else if (TREE_TYPE (olddecl) == NULL_TREE)
3037 types_match = TREE_TYPE (newdecl) == NULL_TREE;
a0a33927
MS
3038 else if (TREE_TYPE (newdecl) == NULL_TREE)
3039 types_match = 0;
8d08fdba 3040 else
01240200 3041 types_match = comptypes (TREE_TYPE (newdecl),
3bfdc719
MM
3042 TREE_TYPE (olddecl),
3043 COMPARE_REDECLARATION);
8d08fdba
MS
3044 }
3045
3046 return types_match;
3047}
3048
3049/* If NEWDECL is `static' and an `extern' was seen previously,
3050 warn about it. (OLDDECL may be NULL_TREE; NAME contains
3051 information about previous usage as an `extern'.)
3052
3053 Note that this does not apply to the C++ case of declaring
3054 a variable `extern const' and then later `const'.
3055
8d08fdba
MS
3056 Don't complain about built-in functions, since they are beyond
3057 the user's control. */
3058
3059static void
3060warn_extern_redeclared_static (newdecl, olddecl)
3061 tree newdecl, olddecl;
3062{
3063 tree name;
3064
d8e178a0 3065 static const char *explicit_extern_static_warning
8251199e 3066 = "`%D' was declared `extern' and later `static'";
d8e178a0 3067 static const char *implicit_extern_static_warning
8251199e
JM
3068 = "`%D' was declared implicitly `extern' and later `static'";
3069
d22c8596 3070 if (TREE_CODE (newdecl) == TYPE_DECL)
8d08fdba
MS
3071 return;
3072
3073 name = DECL_ASSEMBLER_NAME (newdecl);
faae18ab 3074 if (TREE_PUBLIC (name) && DECL_THIS_STATIC (newdecl))
8d08fdba
MS
3075 {
3076 /* It's okay to redeclare an ANSI built-in function as static,
3077 or to declare a non-ANSI built-in function as anything. */
3078 if (! (TREE_CODE (newdecl) == FUNCTION_DECL
3079 && olddecl != NULL_TREE
3080 && TREE_CODE (olddecl) == FUNCTION_DECL
9f33663b 3081 && DECL_ARTIFICIAL (olddecl)))
8d08fdba 3082 {
a9aedbc2 3083 cp_pedwarn (IDENTIFIER_IMPLICIT_DECL (name)
8251199e
JM
3084 ? implicit_extern_static_warning
3085 : explicit_extern_static_warning, newdecl);
8d08fdba 3086 if (olddecl != NULL_TREE)
8251199e 3087 cp_pedwarn_at ("previous declaration of `%D'", olddecl);
8d08fdba
MS
3088 }
3089 }
3090}
3091
3092/* Handle when a new declaration NEWDECL has the same name as an old
3093 one OLDDECL in the same binding contour. Prints an error message
3094 if appropriate.
3095
3096 If safely possible, alter OLDDECL to look like NEWDECL, and return 1.
3097 Otherwise, return 0. */
3098
51c184be 3099int
8d08fdba 3100duplicate_decls (newdecl, olddecl)
824b9a4c 3101 tree newdecl, olddecl;
8d08fdba
MS
3102{
3103 extern struct obstack permanent_obstack;
3104 unsigned olddecl_uid = DECL_UID (olddecl);
3105 int olddecl_friend = 0, types_match = 0;
0b60dfe3 3106 int new_defines_function = 0;
5566b478
MS
3107
3108 if (newdecl == olddecl)
3109 return 1;
8d08fdba 3110
8926095f 3111 types_match = decls_match (newdecl, olddecl);
8d08fdba 3112
8d08fdba
MS
3113 /* If either the type of the new decl or the type of the old decl is an
3114 error_mark_node, then that implies that we have already issued an
3115 error (earlier) for some bogus type specification, and in that case,
3116 it is rather pointless to harass the user with yet more error message
0b60dfe3 3117 about the same declaration, so just pretend the types match here. */
bd6dd845
MS
3118 if (TREE_TYPE (newdecl) == error_mark_node
3119 || TREE_TYPE (olddecl) == error_mark_node)
8d08fdba 3120 types_match = 1;
0b60dfe3
BK
3121
3122 /* Check for redeclaration and other discrepancies. */
d22c8596 3123 if (TREE_CODE (olddecl) == FUNCTION_DECL
9f33663b
JM
3124 && DECL_ARTIFICIAL (olddecl))
3125 {
3126 if (TREE_CODE (newdecl) != FUNCTION_DECL)
3127 {
3128 /* If you declare a built-in or predefined function name as static,
3129 the old definition is overridden, but optionally warn this was a
3130 bad choice of name. */
3131 if (! TREE_PUBLIC (newdecl))
3132 {
3133 if (warn_shadow)
3134 cp_warning ("shadowing %s function `%#D'",
3135 DECL_BUILT_IN (olddecl) ? "built-in" : "library",
3136 olddecl);
3137 /* Discard the old built-in function. */
3138 return 0;
3139 }
3140 /* If the built-in is not ansi, then programs can override
3141 it even globally without an error. */
3142 else if (! DECL_BUILT_IN (olddecl))
3143 cp_warning ("library function `%#D' redeclared as non-function `%#D'",
3144 olddecl, newdecl);
3145 else
3146 {
3147 cp_error ("declaration of `%#D'", newdecl);
3148 cp_error ("conflicts with built-in declaration `%#D'",
a4443a08 3149 olddecl);
9f33663b 3150 }
a4443a08
MS
3151 return 0;
3152 }
9f33663b 3153 else if (!types_match)
8d08fdba 3154 {
9f33663b
JM
3155 if ((DECL_LANGUAGE (newdecl) == lang_c
3156 && DECL_LANGUAGE (olddecl) == lang_c)
3157 || compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
3158 TYPE_ARG_TYPES (TREE_TYPE (olddecl))))
a4443a08 3159 {
9f33663b
JM
3160 /* A near match; override the builtin. */
3161
3162 if (TREE_PUBLIC (newdecl))
a4443a08 3163 {
9f33663b
JM
3164 cp_warning ("new declaration `%#D'", newdecl);
3165 cp_warning ("ambiguates built-in declaration `%#D'",
3166 olddecl);
a4443a08 3167 }
9f33663b
JM
3168 else if (warn_shadow)
3169 cp_warning ("shadowing %s function `%#D'",
3170 DECL_BUILT_IN (olddecl) ? "built-in" : "library",
3171 olddecl);
a4443a08 3172 }
9f33663b
JM
3173 else
3174 /* Discard the old built-in function. */
3175 return 0;
8d08fdba 3176 }
39211cd5
MS
3177 }
3178 else if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
3179 {
9ed182dc
JM
3180 if ((TREE_CODE (olddecl) == TYPE_DECL && DECL_ARTIFICIAL (olddecl)
3181 && TREE_CODE (newdecl) != TYPE_DECL
3182 && ! (TREE_CODE (newdecl) == TEMPLATE_DECL
3183 && TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL))
3184 || (TREE_CODE (newdecl) == TYPE_DECL && DECL_ARTIFICIAL (newdecl)
3185 && TREE_CODE (olddecl) != TYPE_DECL
3186 && ! (TREE_CODE (olddecl) == TEMPLATE_DECL
3187 && (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl))
3188 == TYPE_DECL))))
3189 {
3190 /* We do nothing special here, because C++ does such nasty
3191 things with TYPE_DECLs. Instead, just let the TYPE_DECL
3192 get shadowed, and know that if we need to find a TYPE_DECL
3193 for a given name, we can look in the IDENTIFIER_TYPE_VALUE
3194 slot of the identifier. */
3195 return 0;
3196 }
3197
39211cd5 3198 if ((TREE_CODE (newdecl) == FUNCTION_DECL
5566b478 3199 && DECL_FUNCTION_TEMPLATE_P (olddecl))
39211cd5 3200 || (TREE_CODE (olddecl) == FUNCTION_DECL
5566b478 3201 && DECL_FUNCTION_TEMPLATE_P (newdecl)))
39211cd5 3202 return 0;
9ed182dc 3203
8251199e 3204 cp_error ("`%#D' redeclared as different kind of symbol", newdecl);
39211cd5
MS
3205 if (TREE_CODE (olddecl) == TREE_LIST)
3206 olddecl = TREE_VALUE (olddecl);
8251199e 3207 cp_error_at ("previous declaration of `%#D'", olddecl);
39211cd5
MS
3208
3209 /* New decl is completely inconsistent with the old one =>
3210 tell caller to replace the old one. */
3211
3212 return 0;
8d08fdba 3213 }
8d08fdba
MS
3214 else if (!types_match)
3215 {
7bdbfa05
MM
3216 if (DECL_REAL_CONTEXT (newdecl) != DECL_REAL_CONTEXT (olddecl))
3217 /* These are certainly not duplicate declarations; they're
3218 from different scopes. */
3219 return 0;
3220
8926095f 3221 if (TREE_CODE (newdecl) == TEMPLATE_DECL)
f0e01782
MS
3222 {
3223 /* The name of a class template may not be declared to refer to
3224 any other template, class, function, object, namespace, value,
e92cc029 3225 or type in the same scope. */
5566b478
MS
3226 if (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) == TYPE_DECL
3227 || TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL)
f0e01782 3228 {
8251199e
JM
3229 cp_error ("declaration of template `%#D'", newdecl);
3230 cp_error_at ("conflicts with previous declaration `%#D'",
f0e01782
MS
3231 olddecl);
3232 }
ec255269
MS
3233 else if (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) == FUNCTION_DECL
3234 && TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == FUNCTION_DECL
3235 && compparms (TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (olddecl))),
91063b51 3236 TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (newdecl))))
75650646
MM
3237 && comp_template_parms (DECL_TEMPLATE_PARMS (newdecl),
3238 DECL_TEMPLATE_PARMS (olddecl)))
ec255269 3239 {
8251199e
JM
3240 cp_error ("new declaration `%#D'", newdecl);
3241 cp_error_at ("ambiguates old declaration `%#D'", olddecl);
ec255269 3242 }
f0e01782
MS
3243 return 0;
3244 }
8926095f
MS
3245 if (TREE_CODE (newdecl) == FUNCTION_DECL)
3246 {
3247 if (DECL_LANGUAGE (newdecl) == lang_c
3248 && DECL_LANGUAGE (olddecl) == lang_c)
3249 {
8251199e 3250 cp_error ("declaration of C function `%#D' conflicts with",
8926095f 3251 newdecl);
8251199e 3252 cp_error_at ("previous declaration `%#D' here", olddecl);
8926095f 3253 }
00595019 3254 else if (compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
91063b51 3255 TYPE_ARG_TYPES (TREE_TYPE (olddecl))))
700f8a87 3256 {
8251199e
JM
3257 cp_error ("new declaration `%#D'", newdecl);
3258 cp_error_at ("ambiguates old declaration `%#D'", olddecl);
700f8a87
MS
3259 }
3260 else
3261 return 0;
8926095f 3262 }
8d08fdba
MS
3263
3264 /* Already complained about this, so don't do so again. */
a4443a08 3265 else if (current_class_type == NULL_TREE
8d08fdba
MS
3266 || IDENTIFIER_ERROR_LOCUS (DECL_ASSEMBLER_NAME (newdecl)) != current_class_type)
3267 {
8251199e
JM
3268 cp_error ("conflicting types for `%#D'", newdecl);
3269 cp_error_at ("previous declaration as `%#D'", olddecl);
8d08fdba
MS
3270 }
3271 }
75650646
MM
3272 else if (TREE_CODE (newdecl) == FUNCTION_DECL
3273 && ((DECL_TEMPLATE_SPECIALIZATION (olddecl)
3274 && (!DECL_TEMPLATE_INFO (newdecl)
3275 || (DECL_TI_TEMPLATE (newdecl)
3276 != DECL_TI_TEMPLATE (olddecl))))
3277 || (DECL_TEMPLATE_SPECIALIZATION (newdecl)
3278 && (!DECL_TEMPLATE_INFO (olddecl)
3279 || (DECL_TI_TEMPLATE (olddecl)
3280 != DECL_TI_TEMPLATE (newdecl))))))
386b8a85
JM
3281 /* It's OK to have a template specialization and a non-template
3282 with the same type, or to have specializations of two
75650646
MM
3283 different templates with the same type. Note that if one is a
3284 specialization, and the other is an instantiation of the same
3285 template, that we do not exit at this point. That situation
3286 can occur if we instantiate a template class, and then
3287 specialize one of its methods. This situation is legal, but
3288 the declarations must be merged in the usual way. */
3289 return 0;
3290 else if (TREE_CODE (newdecl) == FUNCTION_DECL
3291 && ((DECL_TEMPLATE_INSTANTIATION (olddecl)
3292 && !DECL_USE_TEMPLATE (newdecl))
3293 || (DECL_TEMPLATE_INSTANTIATION (newdecl)
3294 && !DECL_USE_TEMPLATE (olddecl))))
3295 /* One of the declarations is a template instantiation, and the
3296 other is not a template at all. That's OK. */
386b8a85 3297 return 0;
85c6cbaf
ML
3298 else if (TREE_CODE (newdecl) == NAMESPACE_DECL
3299 && DECL_NAMESPACE_ALIAS (newdecl)
3300 && DECL_NAMESPACE_ALIAS (newdecl) == DECL_NAMESPACE_ALIAS (olddecl))
3301 /* Redeclaration of namespace alias, ignore it. */
3302 return 1;
8d08fdba
MS
3303 else
3304 {
d8e178a0 3305 const char *errmsg = redeclaration_error_message (newdecl, olddecl);
8251199e 3306 if (errmsg)
8d08fdba 3307 {
8251199e 3308 cp_error (errmsg, newdecl);
8d08fdba
MS
3309 if (DECL_NAME (olddecl) != NULL_TREE)
3310 cp_error_at ((DECL_INITIAL (olddecl)
2c73f9f5 3311 && namespace_bindings_p ())
8251199e
JM
3312 ? "`%#D' previously defined here"
3313 : "`%#D' previously declared here", olddecl);
8d08fdba
MS
3314 }
3315 else if (TREE_CODE (olddecl) == FUNCTION_DECL
3316 && DECL_INITIAL (olddecl) != NULL_TREE
3317 && TYPE_ARG_TYPES (TREE_TYPE (olddecl)) == NULL_TREE
3318 && TYPE_ARG_TYPES (TREE_TYPE (newdecl)) != NULL_TREE)
3319 {
3320 /* Prototype decl follows defn w/o prototype. */
8251199e
JM
3321 cp_warning_at ("prototype for `%#D'", newdecl);
3322 cp_warning_at ("follows non-prototype definition here", olddecl);
8d08fdba
MS
3323 }
3324 else if (TREE_CODE (olddecl) == FUNCTION_DECL
3325 && DECL_LANGUAGE (newdecl) != DECL_LANGUAGE (olddecl))
8926095f
MS
3326 {
3327 /* extern "C" int foo ();
3328 int foo () { bar (); }
3329 is OK. */
3330 if (current_lang_stack == current_lang_base)
a28e3c7f 3331 DECL_LANGUAGE (newdecl) = DECL_LANGUAGE (olddecl);
8926095f
MS
3332 else
3333 {
8251199e 3334 cp_error_at ("previous declaration of `%#D' with %L linkage",
8926095f 3335 olddecl, DECL_LANGUAGE (olddecl));
8251199e 3336 cp_error ("conflicts with new declaration with %L linkage",
8926095f
MS
3337 DECL_LANGUAGE (newdecl));
3338 }
3339 }
e1cd6e56 3340
a6f02587 3341 if (DECL_LANG_SPECIFIC (olddecl) && DECL_USE_TEMPLATE (olddecl))
c32381b1
JM
3342 ;
3343 else if (TREE_CODE (olddecl) == FUNCTION_DECL)
e1cd6e56
MS
3344 {
3345 tree t1 = TYPE_ARG_TYPES (TREE_TYPE (olddecl));
3346 tree t2 = TYPE_ARG_TYPES (TREE_TYPE (newdecl));
3347 int i = 1;
3348
3349 if (TREE_CODE (TREE_TYPE (newdecl)) == METHOD_TYPE)
3350 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2);
3351
3352 for (; t1 && t1 != void_list_node;
3353 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2), i++)
3354 if (TREE_PURPOSE (t1) && TREE_PURPOSE (t2))
3355 {
2507f3b5
RK
3356 if (1 == simple_cst_equal (TREE_PURPOSE (t1),
3357 TREE_PURPOSE (t2)))
e1cd6e56
MS
3358 {
3359 if (pedantic)
3360 {
8251199e 3361 cp_pedwarn ("default argument given for parameter %d of `%#D'",
e1cd6e56 3362 i, newdecl);
8251199e 3363 cp_pedwarn_at ("after previous specification in `%#D'",
e1cd6e56
MS
3364 olddecl);
3365 }
3366 }
3367 else
3368 {
8251199e 3369 cp_error ("default argument given for parameter %d of `%#D'",
e1cd6e56 3370 i, newdecl);
8251199e 3371 cp_error_at ("after previous specification in `%#D'",
e1cd6e56
MS
3372 olddecl);
3373 }
3374 }
a5894242 3375
7fcdf4c2
MS
3376 if (DECL_THIS_INLINE (newdecl) && ! DECL_THIS_INLINE (olddecl)
3377 && TREE_ADDRESSABLE (olddecl) && warn_inline)
dff6b454 3378 {
8251199e 3379 cp_warning ("`%#D' was used before it was declared inline",
7fcdf4c2 3380 newdecl);
8251199e 3381 cp_warning_at ("previous non-inline declaration here",
7fcdf4c2 3382 olddecl);
dff6b454 3383 }
e1cd6e56 3384 }
8d08fdba
MS
3385 }
3386
3387 /* If new decl is `static' and an `extern' was seen previously,
3388 warn about it. */
3389 warn_extern_redeclared_static (newdecl, olddecl);
3390
e92cc029 3391 /* We have committed to returning 1 at this point. */
8d08fdba
MS
3392 if (TREE_CODE (newdecl) == FUNCTION_DECL)
3393 {
3394 /* Now that functions must hold information normally held
3395 by field decls, there is extra work to do so that
3396 declaration information does not get destroyed during
3397 definition. */
3398 if (DECL_VINDEX (olddecl))
3399 DECL_VINDEX (newdecl) = DECL_VINDEX (olddecl);
3400 if (DECL_CONTEXT (olddecl))
3401 DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
3402 if (DECL_CLASS_CONTEXT (olddecl))
3403 DECL_CLASS_CONTEXT (newdecl) = DECL_CLASS_CONTEXT (olddecl);
8d08fdba
MS
3404 if (DECL_PENDING_INLINE_INFO (newdecl) == (struct pending_inline *)0)
3405 DECL_PENDING_INLINE_INFO (newdecl) = DECL_PENDING_INLINE_INFO (olddecl);
e1cd6e56
MS
3406 DECL_STATIC_CONSTRUCTOR (newdecl) |= DECL_STATIC_CONSTRUCTOR (olddecl);
3407 DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
7215f9a0 3408 DECL_ABSTRACT_VIRTUAL_P (newdecl) |= DECL_ABSTRACT_VIRTUAL_P (olddecl);
864b83b9 3409 DECL_VIRTUAL_P (newdecl) |= DECL_VIRTUAL_P (olddecl);
4a67c9e9 3410 DECL_NEEDS_FINAL_OVERRIDER_P (newdecl) |= DECL_NEEDS_FINAL_OVERRIDER_P (olddecl);
0b60dfe3
BK
3411 new_defines_function = DECL_INITIAL (newdecl) != NULL_TREE;
3412
3413 /* Optionally warn about more than one declaration for the same
3414 name, but don't warn about a function declaration followed by a
3415 definition. */
3416 if (warn_redundant_decls && ! DECL_ARTIFICIAL (olddecl)
3417 && !(new_defines_function && DECL_INITIAL (olddecl) == NULL_TREE)
3418 /* Don't warn about extern decl followed by definition. */
3419 && !(DECL_EXTERNAL (olddecl) && ! DECL_EXTERNAL (newdecl))
3420 /* Don't warn about friends, let add_friend take care of it. */
3421 && ! DECL_FRIEND_P (newdecl))
3422 {
8251199e
JM
3423 cp_warning ("redundant redeclaration of `%D' in same scope", newdecl);
3424 cp_warning_at ("previous declaration of `%D'", olddecl);
0b60dfe3 3425 }
8d08fdba
MS
3426 }
3427
3428 /* Deal with C++: must preserve virtual function table size. */
3429 if (TREE_CODE (olddecl) == TYPE_DECL)
3430 {
3431 register tree newtype = TREE_TYPE (newdecl);
3432 register tree oldtype = TREE_TYPE (olddecl);
3433
3434 if (newtype != error_mark_node && oldtype != error_mark_node
3435 && TYPE_LANG_SPECIFIC (newtype) && TYPE_LANG_SPECIFIC (oldtype))
3436 {
3437 CLASSTYPE_VSIZE (newtype) = CLASSTYPE_VSIZE (oldtype);
3438 CLASSTYPE_FRIEND_CLASSES (newtype)
3439 = CLASSTYPE_FRIEND_CLASSES (oldtype);
3440 }
8d08fdba
MS
3441 }
3442
8d08fdba
MS
3443 /* Copy all the DECL_... slots specified in the new decl
3444 except for any that we copy here from the old type. */
0b60dfe3
BK
3445 DECL_MACHINE_ATTRIBUTES (newdecl)
3446 = merge_machine_decl_attributes (olddecl, newdecl);
8d08fdba 3447
5566b478
MS
3448 if (TREE_CODE (newdecl) == TEMPLATE_DECL)
3449 {
4d85e00e
MM
3450 if (! duplicate_decls (DECL_TEMPLATE_RESULT (newdecl),
3451 DECL_TEMPLATE_RESULT (olddecl)))
8251199e 3452 cp_error ("invalid redeclaration of %D", newdecl);
4d85e00e 3453 TREE_TYPE (olddecl) = TREE_TYPE (DECL_TEMPLATE_RESULT (olddecl));
4d85e00e
MM
3454 DECL_TEMPLATE_SPECIALIZATIONS (olddecl)
3455 = chainon (DECL_TEMPLATE_SPECIALIZATIONS (olddecl),
3456 DECL_TEMPLATE_SPECIALIZATIONS (newdecl));
0b60dfe3 3457
5566b478
MS
3458 return 1;
3459 }
0b60dfe3 3460
8d08fdba
MS
3461 if (types_match)
3462 {
3463 /* Automatically handles default parameters. */
3464 tree oldtype = TREE_TYPE (olddecl);
e1cd6e56 3465 tree newtype;
8d08fdba 3466
e1cd6e56
MS
3467 /* Make sure we put the new type in the same obstack as the old one. */
3468 if (oldtype)
39211cd5
MS
3469 push_obstacks (TYPE_OBSTACK (oldtype), TYPE_OBSTACK (oldtype));
3470 else
3471 {
3472 push_obstacks_nochange ();
3473 end_temporary_allocation ();
3474 }
3475
e1cd6e56
MS
3476 /* Merge the data types specified in the two decls. */
3477 newtype = common_type (TREE_TYPE (newdecl), TREE_TYPE (olddecl));
3478
8d08fdba
MS
3479 if (TREE_CODE (newdecl) == VAR_DECL)
3480 DECL_THIS_EXTERN (newdecl) |= DECL_THIS_EXTERN (olddecl);
3481 /* Do this after calling `common_type' so that default
3482 parameters don't confuse us. */
3483 else if (TREE_CODE (newdecl) == FUNCTION_DECL
3484 && (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (newdecl))
3485 != TYPE_RAISES_EXCEPTIONS (TREE_TYPE (olddecl))))
3486 {
f30432d7 3487 TREE_TYPE (newdecl) = build_exception_variant (newtype,
8d08fdba 3488 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (newdecl)));
f30432d7 3489 TREE_TYPE (olddecl) = build_exception_variant (newtype,
8d08fdba
MS
3490 TYPE_RAISES_EXCEPTIONS (oldtype));
3491
9a224b4a
JM
3492 if ((pedantic || ! DECL_IN_SYSTEM_HEADER (olddecl))
3493 && DECL_SOURCE_LINE (olddecl) != 0
da20811c 3494 && flag_exceptions
4cc1d462
NS
3495 && !comp_except_specs (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (newdecl)),
3496 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (olddecl)), 1))
8d08fdba 3497 {
4cc1d462 3498 cp_error ("declaration of `%F' throws different exceptions",
a28e3c7f 3499 newdecl);
4cc1d462 3500 cp_error_at ("to previous declaration `%F'", olddecl);
8d08fdba
MS
3501 }
3502 }
3503 TREE_TYPE (newdecl) = TREE_TYPE (olddecl) = newtype;
3504
3505 /* Lay the type out, unless already done. */
407f03b8 3506 if (! same_type_p (newtype, oldtype)
5566b478 3507 && TREE_TYPE (newdecl) != error_mark_node
5156628f 3508 && !(processing_template_decl && uses_template_parms (newdecl)))
b7484fbe
MS
3509 layout_type (TREE_TYPE (newdecl));
3510
5566b478
MS
3511 if ((TREE_CODE (newdecl) == VAR_DECL
3512 || TREE_CODE (newdecl) == PARM_DECL
3513 || TREE_CODE (newdecl) == RESULT_DECL
3514 || TREE_CODE (newdecl) == FIELD_DECL
3515 || TREE_CODE (newdecl) == TYPE_DECL)
5156628f 3516 && !(processing_template_decl && uses_template_parms (newdecl)))
b7484fbe 3517 layout_decl (newdecl, 0);
8d08fdba
MS
3518
3519 /* Merge the type qualifiers. */
3520 if (TREE_READONLY (newdecl))
3521 TREE_READONLY (olddecl) = 1;
3522 if (TREE_THIS_VOLATILE (newdecl))
3523 TREE_THIS_VOLATILE (olddecl) = 1;
3524
3525 /* Merge the initialization information. */
8926095f
MS
3526 if (DECL_INITIAL (newdecl) == NULL_TREE
3527 && DECL_INITIAL (olddecl) != NULL_TREE)
3528 {
3529 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
3530 DECL_SOURCE_FILE (newdecl) = DECL_SOURCE_FILE (olddecl);
3531 DECL_SOURCE_LINE (newdecl) = DECL_SOURCE_LINE (olddecl);
4d85e00e
MM
3532 if (DECL_LANG_SPECIFIC (newdecl)
3533 && DECL_LANG_SPECIFIC (olddecl))
3534 DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
8926095f 3535 }
39211cd5
MS
3536
3537 /* Merge the section attribute.
3538 We want to issue an error if the sections conflict but that must be
3539 done later in decl_attributes since we are called before attributes
3540 are assigned. */
3541 if (DECL_SECTION_NAME (newdecl) == NULL_TREE)
3542 DECL_SECTION_NAME (newdecl) = DECL_SECTION_NAME (olddecl);
3543
8d08fdba
MS
3544 /* Keep the old rtl since we can safely use it, unless it's the
3545 call to abort() used for abstract virtuals. */
3546 if ((DECL_LANG_SPECIFIC (olddecl)
3547 && !DECL_ABSTRACT_VIRTUAL_P (olddecl))
3548 || DECL_RTL (olddecl) != DECL_RTL (abort_fndecl))
3549 DECL_RTL (newdecl) = DECL_RTL (olddecl);
39211cd5
MS
3550
3551 pop_obstacks ();
8d08fdba
MS
3552 }
3553 /* If cannot merge, then use the new type and qualifiers,
3554 and don't preserve the old rtl. */
3555 else
3556 {
3557 /* Clean out any memory we had of the old declaration. */
3558 tree oldstatic = value_member (olddecl, static_aggregates);
3559 if (oldstatic)
3560 TREE_VALUE (oldstatic) = error_mark_node;
3561
3562 TREE_TYPE (olddecl) = TREE_TYPE (newdecl);
3563 TREE_READONLY (olddecl) = TREE_READONLY (newdecl);
3564 TREE_THIS_VOLATILE (olddecl) = TREE_THIS_VOLATILE (newdecl);
3565 TREE_SIDE_EFFECTS (olddecl) = TREE_SIDE_EFFECTS (newdecl);
3566 }
3567
3568 /* Merge the storage class information. */
a9aedbc2 3569 DECL_WEAK (newdecl) |= DECL_WEAK (olddecl);
e92cc029 3570 DECL_ONE_ONLY (newdecl) |= DECL_ONE_ONLY (olddecl);
a9aedbc2
MS
3571 TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
3572 TREE_STATIC (olddecl) = TREE_STATIC (newdecl) |= TREE_STATIC (olddecl);
3573 if (! DECL_EXTERNAL (olddecl))
3574 DECL_EXTERNAL (newdecl) = 0;
0b60dfe3
BK
3575
3576 if (DECL_LANG_SPECIFIC (newdecl) && DECL_LANG_SPECIFIC (olddecl))
8d08fdba 3577 {
a9aedbc2
MS
3578 DECL_INTERFACE_KNOWN (newdecl) |= DECL_INTERFACE_KNOWN (olddecl);
3579 DECL_NOT_REALLY_EXTERN (newdecl) |= DECL_NOT_REALLY_EXTERN (olddecl);
77be6f82 3580 DECL_COMDAT (newdecl) |= DECL_COMDAT (olddecl);
2b0a63a3
MM
3581 DECL_TEMPLATE_INSTANTIATED (newdecl)
3582 |= DECL_TEMPLATE_INSTANTIATED (olddecl);
0b60dfe3
BK
3583 /* Don't really know how much of the language-specific
3584 values we should copy from old to new. */
3585 DECL_IN_AGGR_P (newdecl) = DECL_IN_AGGR_P (olddecl);
3586 DECL_ACCESS (newdecl) = DECL_ACCESS (olddecl);
3587 DECL_NONCONVERTING_P (newdecl) = DECL_NONCONVERTING_P (olddecl);
fbf1c34b 3588 DECL_TEMPLATE_INFO (newdecl) = DECL_TEMPLATE_INFO (olddecl);
0b60dfe3 3589 olddecl_friend = DECL_FRIEND_P (olddecl);
6a629cac
MM
3590
3591 /* Only functions have DECL_BEFRIENDING_CLASSES. */
3592 if (TREE_CODE (newdecl) == FUNCTION_DECL
3593 || DECL_FUNCTION_TEMPLATE_P (newdecl))
3594 DECL_BEFRIENDING_CLASSES (newdecl)
3595 = chainon (DECL_BEFRIENDING_CLASSES (newdecl),
3596 DECL_BEFRIENDING_CLASSES (olddecl));
8d08fdba
MS
3597 }
3598
8d08fdba
MS
3599 if (TREE_CODE (newdecl) == FUNCTION_DECL)
3600 {
75650646
MM
3601 if (DECL_TEMPLATE_INSTANTIATION (olddecl)
3602 && !DECL_TEMPLATE_INSTANTIATION (newdecl))
3603 {
3604 /* If newdecl is not a specialization, then it is not a
3605 template-related function at all. And that means that we
3606 shoud have exited above, returning 0. */
3607 my_friendly_assert (DECL_TEMPLATE_SPECIALIZATION (newdecl),
3608 0);
3609
3610 if (TREE_USED (olddecl))
3611 /* From [temp.expl.spec]:
3612
3613 If a template, a member template or the member of a class
3614 template is explicitly specialized then that
3615 specialization shall be declared before the first use of
3616 that specialization that would cause an implicit
3617 instantiation to take place, in every translation unit in
3618 which such a use occurs. */
8251199e 3619 cp_error ("explicit specialization of %D after first use",
75650646
MM
3620 olddecl);
3621
3622 SET_DECL_TEMPLATE_SPECIALIZATION (olddecl);
3623 }
faae18ab
MS
3624 DECL_THIS_INLINE (newdecl) |= DECL_THIS_INLINE (olddecl);
3625
3626 /* If either decl says `inline', this fn is inline, unless its
3627 definition was passed already. */
3628 if (DECL_INLINE (newdecl) && DECL_INITIAL (olddecl) == NULL_TREE)
3629 DECL_INLINE (olddecl) = 1;
3630 DECL_INLINE (newdecl) = DECL_INLINE (olddecl);
3631
700f8a87
MS
3632 if (! types_match)
3633 {
3634 DECL_LANGUAGE (olddecl) = DECL_LANGUAGE (newdecl);
3635 DECL_ASSEMBLER_NAME (olddecl) = DECL_ASSEMBLER_NAME (newdecl);
5566b478
MS
3636 DECL_RTL (olddecl) = DECL_RTL (newdecl);
3637 }
3638 if (! types_match || new_defines_function)
3639 {
6f1b4c42
JM
3640 /* These need to be copied so that the names are available.
3641 Note that if the types do match, we'll preserve inline
3642 info and other bits, but if not, we won't. */
700f8a87
MS
3643 DECL_ARGUMENTS (olddecl) = DECL_ARGUMENTS (newdecl);
3644 DECL_RESULT (olddecl) = DECL_RESULT (newdecl);
700f8a87 3645 }
8d08fdba
MS
3646 if (new_defines_function)
3647 /* If defining a function declared with other language
3648 linkage, use the previously declared language linkage. */
3649 DECL_LANGUAGE (newdecl) = DECL_LANGUAGE (olddecl);
6f1b4c42 3650 else if (types_match)
8d08fdba
MS
3651 {
3652 /* If redeclaring a builtin function, and not a definition,
3653 it stays built in. */
3654 if (DECL_BUILT_IN (olddecl))
3655 {
3656 DECL_BUILT_IN (newdecl) = 1;
39211cd5 3657 DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl);
8d08fdba
MS
3658 /* If we're keeping the built-in definition, keep the rtl,
3659 regardless of declaration matches. */
3660 DECL_RTL (newdecl) = DECL_RTL (olddecl);
3661 }
3662 else
3663 DECL_FRAME_SIZE (newdecl) = DECL_FRAME_SIZE (olddecl);
3664
3665 DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
8926095f 3666 if ((DECL_SAVED_INSNS (newdecl) = DECL_SAVED_INSNS (olddecl)))
8d08fdba
MS
3667 /* Previously saved insns go together with
3668 the function's previous definition. */
3669 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
3670 /* Don't clear out the arguments if we're redefining a function. */
3671 if (DECL_ARGUMENTS (olddecl))
3672 DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl);
3673 }
6060a796
MS
3674 if (DECL_LANG_SPECIFIC (olddecl))
3675 DECL_MAIN_VARIANT (newdecl) = DECL_MAIN_VARIANT (olddecl);
8d08fdba
MS
3676 }
3677
a9aedbc2
MS
3678 if (TREE_CODE (newdecl) == NAMESPACE_DECL)
3679 {
3680 NAMESPACE_LEVEL (newdecl) = NAMESPACE_LEVEL (olddecl);
3681 }
3682
8d08fdba
MS
3683 /* Now preserve various other info from the definition. */
3684 TREE_ADDRESSABLE (newdecl) = TREE_ADDRESSABLE (olddecl);
3685 TREE_ASM_WRITTEN (newdecl) = TREE_ASM_WRITTEN (olddecl);
3686 DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
f376e137 3687 DECL_ASSEMBLER_NAME (newdecl) = DECL_ASSEMBLER_NAME (olddecl);
8d08fdba 3688
8d08fdba
MS
3689 if (TREE_CODE (newdecl) == FUNCTION_DECL)
3690 {
3691 int function_size;
3692 struct lang_decl *ol = DECL_LANG_SPECIFIC (olddecl);
3693 struct lang_decl *nl = DECL_LANG_SPECIFIC (newdecl);
3694
3695 function_size = sizeof (struct tree_decl);
3696
3697 bcopy ((char *) newdecl + sizeof (struct tree_common),
3698 (char *) olddecl + sizeof (struct tree_common),
3699 function_size - sizeof (struct tree_common));
3700
3701 /* Can we safely free the storage used by newdecl? */
3702
3703#define ROUND(x) ((x + obstack_alignment_mask (&permanent_obstack)) \
3704 & ~ obstack_alignment_mask (&permanent_obstack))
3705
75650646
MM
3706 if (DECL_TEMPLATE_INSTANTIATION (newdecl))
3707 {
3708 /* If newdecl is a template instantiation, it is possible that
3709 the following sequence of events has occurred:
3710
3711 o A friend function was declared in a class template. The
3712 class template was instantiated.
3713
3714 o The instantiation of the friend declaration was
3715 recorded on the instantiation list, and is newdecl.
3716
3717 o Later, however, instantiate_class_template called pushdecl
3718 on the newdecl to perform name injection. But, pushdecl in
3719 turn called duplicate_decls when it discovered that another
3720 declaration of a global function with the same name already
3721 existed.
3722
3723 o Here, in duplicate_decls, we decided to clobber newdecl.
3724
3725 If we're going to do that, we'd better make sure that
3726 olddecl, and not newdecl, is on the list of
3727 instantiations so that if we try to do the instantiation
3728 again we won't get the clobbered declaration. */
3729
3730 tree tmpl = DECL_TI_TEMPLATE (newdecl);
3731 tree decls = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
3732
3733 for (; decls; decls = TREE_CHAIN (decls))
3734 if (TREE_VALUE (decls) == newdecl)
3735 TREE_VALUE (decls) = olddecl;
3736 }
3737
3a56f0ab
JM
3738 if (((char *)newdecl + ROUND (function_size) == (char *)nl
3739 && ((char *)newdecl + ROUND (function_size)
3740 + ROUND (sizeof (struct lang_decl))
3741 == obstack_next_free (&permanent_obstack)))
3742 || ((char *)newdecl + ROUND (function_size)
3743 == obstack_next_free (&permanent_obstack)))
8d08fdba
MS
3744 {
3745 DECL_MAIN_VARIANT (newdecl) = olddecl;
3746 DECL_LANG_SPECIFIC (olddecl) = ol;
3747 bcopy ((char *)nl, (char *)ol, sizeof (struct lang_decl));
3748
3749 obstack_free (&permanent_obstack, newdecl);
3750 }
d22c8596 3751 else if (LANG_DECL_PERMANENT (ol) && ol != nl)
8d08fdba
MS
3752 {
3753 if (DECL_MAIN_VARIANT (olddecl) == olddecl)
3754 {
3755 /* Save these lang_decls that would otherwise be lost. */
3756 extern tree free_lang_decl_chain;
3757 tree free_lang_decl = (tree) ol;
d22c8596
MS
3758
3759 if (DECL_LANG_SPECIFIC (olddecl) == ol)
3760 abort ();
3761
8d08fdba
MS
3762 TREE_CHAIN (free_lang_decl) = free_lang_decl_chain;
3763 free_lang_decl_chain = free_lang_decl;
3764 }
3765 else
3766 {
bd6dd845 3767 /* Storage leak. */;
8d08fdba
MS
3768 }
3769 }
3770 }
3771 else
3772 {
3773 bcopy ((char *) newdecl + sizeof (struct tree_common),
3774 (char *) olddecl + sizeof (struct tree_common),
3775 sizeof (struct tree_decl) - sizeof (struct tree_common)
3776 + tree_code_length [(int)TREE_CODE (newdecl)] * sizeof (char *));
3777 }
3778
3779 DECL_UID (olddecl) = olddecl_uid;
3780 if (olddecl_friend)
3781 DECL_FRIEND_P (olddecl) = 1;
3782
d9525bec
BK
3783 /* NEWDECL contains the merged attribute lists.
3784 Update OLDDECL to be the same. */
3785 DECL_MACHINE_ATTRIBUTES (olddecl) = DECL_MACHINE_ATTRIBUTES (newdecl);
3786
8d08fdba
MS
3787 return 1;
3788}
3789
3790/* Record a decl-node X as belonging to the current lexical scope.
3791 Check for errors (such as an incompatible declaration for the same
3792 name already seen in the same scope).
3793
3794 Returns either X or an old decl for the same name.
3795 If an old decl is returned, it may have been smashed
3796 to agree with what X says. */
3797
3798tree
3799pushdecl (x)
3800 tree x;
3801{
3802 register tree t;
8d08fdba 3803 register tree name = DECL_ASSEMBLER_NAME (x);
f181d4ae 3804 int need_new_binding = 1;
8d08fdba 3805
50714e79
MM
3806 if (DECL_TEMPLATE_PARM_P (x))
3807 /* Template parameters have no context; they are not X::T even
3808 when declared within a class or namespace. */
3809 ;
3810 else
3811 {
3812 if (current_function_decl && x != current_function_decl
3813 /* A local declaration for a function doesn't constitute
3814 nesting. */
3815 && (TREE_CODE (x) != FUNCTION_DECL || DECL_INITIAL (x))
3816 /* Don't change DECL_CONTEXT of virtual methods. */
3817 && (TREE_CODE (x) != FUNCTION_DECL || !DECL_VIRTUAL_P (x))
3818 && !DECL_CONTEXT (x))
3819 DECL_CONTEXT (x) = current_function_decl;
3820 if (!DECL_CONTEXT (x))
3821 DECL_CONTEXT (x) = FROB_CONTEXT (current_namespace);
3822 }
8d08fdba 3823
8d08fdba 3824 /* Type are looked up using the DECL_NAME, as that is what the rest of the
e92cc029 3825 compiler wants to use. */
a9aedbc2 3826 if (TREE_CODE (x) == TYPE_DECL || TREE_CODE (x) == VAR_DECL
0c20d3d6 3827 || TREE_CODE (x) == NAMESPACE_DECL)
8d08fdba 3828 name = DECL_NAME (x);
8d08fdba
MS
3829
3830 if (name)
3831 {
5566b478
MS
3832#if 0
3833 /* Not needed...see below. */
8d08fdba
MS
3834 char *file;
3835 int line;
5566b478 3836#endif
386b8a85
JM
3837 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
3838 name = TREE_OPERAND (name, 0);
3839
2c73f9f5 3840 /* Namespace-scoped variables are not found in the current level. */
6eb3bb27 3841 if (TREE_CODE (x) == VAR_DECL && DECL_NAMESPACE_SCOPE_P (x))
2c73f9f5
ML
3842 t = namespace_binding (name, DECL_CONTEXT (x));
3843 else
3844 t = lookup_name_current_level (name);
8d08fdba
MS
3845 if (t == error_mark_node)
3846 {
3847 /* error_mark_node is 0 for a while during initialization! */
3848 t = NULL_TREE;
8251199e 3849 cp_error_at ("`%#D' used prior to declaration", x);
8d08fdba
MS
3850 }
3851
51c184be 3852 else if (t != NULL_TREE)
8d08fdba 3853 {
5566b478
MS
3854#if 0
3855 /* This is turned off until I have time to do it right (bpk). */
e92cc029 3856 /* With the code below that uses it... */
8d6e462b
PB
3857 file = DECL_SOURCE_FILE (t);
3858 line = DECL_SOURCE_LINE (t);
5566b478 3859#endif
2ee887f2 3860 if (TREE_CODE (t) == PARM_DECL)
8d08fdba
MS
3861 {
3862 if (DECL_CONTEXT (t) == NULL_TREE)
3863 fatal ("parse errors have confused me too much");
be99da77 3864
e92cc029 3865 /* Check for duplicate params. */
be99da77
MS
3866 if (duplicate_decls (x, t))
3867 return t;
8d08fdba 3868 }
8d6e462b 3869 else if (((TREE_CODE (x) == FUNCTION_DECL && DECL_LANGUAGE (x) == lang_c)
5566b478
MS
3870 || DECL_FUNCTION_TEMPLATE_P (x))
3871 && is_overloaded_fn (t))
2c73f9f5 3872 /* Don't do anything just yet. */;
e1cd6e56
MS
3873 else if (t == wchar_decl_node)
3874 {
3875 if (pedantic && ! DECL_IN_SYSTEM_HEADER (x))
8251199e 3876 cp_pedwarn ("redeclaration of wchar_t as `%T'", TREE_TYPE (x));
e1cd6e56
MS
3877
3878 /* Throw away the redeclaration. */
3879 return t;
3880 }
8926095f 3881 else if (TREE_CODE (t) != TREE_CODE (x))
8d08fdba 3882 {
9ed182dc 3883 if (duplicate_decls (x, t))
51c184be 3884 return t;
8d08fdba
MS
3885 }
3886 else if (duplicate_decls (x, t))
51c184be 3887 {
8d08fdba 3888#if 0
8926095f 3889 /* This is turned off until I have time to do it right (bpk). */
8d08fdba 3890
8926095f
MS
3891 /* Also warn if they did a prototype with `static' on it, but
3892 then later left the `static' off. */
3893 if (! TREE_PUBLIC (name) && TREE_PUBLIC (x))
8d08fdba 3894 {
8926095f
MS
3895 if (DECL_LANG_SPECIFIC (t) && DECL_FRIEND_P (t))
3896 return t;
8d08fdba 3897
8926095f
MS
3898 if (extra_warnings)
3899 {
8251199e 3900 cp_warning ("`static' missing from declaration of `%D'",
a28e3c7f 3901 t);
8926095f
MS
3902 warning_with_file_and_line (file, line,
3903 "previous declaration of `%s'",
3904 decl_as_string (t, 0));
3905 }
8d08fdba 3906
8926095f
MS
3907 /* Now fix things so it'll do what they expect. */
3908 if (current_function_decl)
3909 TREE_PUBLIC (current_function_decl) = 0;
3910 }
51c184be
MS
3911 /* Due to interference in memory reclamation (X may be
3912 obstack-deallocated at this point), we must guard against
8926095f
MS
3913 one really special case. [jason: This should be handled
3914 by start_function] */
51c184be
MS
3915 if (current_function_decl == x)
3916 current_function_decl = t;
8926095f 3917#endif
7177d104
MS
3918 if (TREE_CODE (t) == TYPE_DECL)
3919 SET_IDENTIFIER_TYPE_VALUE (name, TREE_TYPE (t));
f30432d7
MS
3920 else if (TREE_CODE (t) == FUNCTION_DECL)
3921 check_default_args (t);
7177d104 3922
51c184be
MS
3923 return t;
3924 }
35680744
MM
3925 else if (DECL_MAIN_P (x))
3926 {
3927 /* A redeclaration of main, but not a duplicate of the
3928 previous one.
3929
3930 [basic.start.main]
3931
3932 This function shall not be overloaded. */
8251199e
JM
3933 cp_error_at ("invalid redeclaration of `%D'", t);
3934 cp_error ("as `%D'", x);
35680744
MM
3935 /* We don't try to push this declaration since that
3936 causes a crash. */
3937 return x;
3938 }
8d08fdba 3939 }
8926095f 3940
f3400fe2
JM
3941 check_template_shadow (x);
3942
fcfcdfc8
JM
3943 /* If this is a function conjured up by the backend, massage it
3944 so it looks friendly. */
3945 if (TREE_CODE (x) == FUNCTION_DECL
3946 && ! DECL_LANG_SPECIFIC (x))
3947 {
3948 retrofit_lang_decl (x);
3949 DECL_LANGUAGE (x) = lang_c;
3950 }
3951
8926095f
MS
3952 if (TREE_CODE (x) == FUNCTION_DECL && ! DECL_FUNCTION_MEMBER_P (x))
3953 {
7bdbfa05 3954 t = push_overloaded_decl (x, PUSH_LOCAL);
8926095f
MS
3955 if (t != x || DECL_LANGUAGE (x) == lang_c)
3956 return t;
f181d4ae
MM
3957 if (!namespace_bindings_p ())
3958 /* We do not need to create a binding for this name;
3959 push_overloaded_decl will have already done so if
3960 necessary. */
3961 need_new_binding = 0;
8926095f 3962 }
6eb3bb27 3963 else if (DECL_FUNCTION_TEMPLATE_P (x) && DECL_NAMESPACE_SCOPE_P (x))
7bdbfa05 3964 return push_overloaded_decl (x, PUSH_GLOBAL);
8d08fdba 3965
a1774733
BK
3966 /* If declaring a type as a typedef, copy the type (unless we're
3967 at line 0), and install this TYPE_DECL as the new type's typedef
3968 name. See the extensive comment in ../c-decl.c (pushdecl). */
8d08fdba
MS
3969 if (TREE_CODE (x) == TYPE_DECL)
3970 {
3971 tree type = TREE_TYPE (x);
a1774733
BK
3972 if (DECL_SOURCE_LINE (x) == 0)
3973 {
3974 if (TYPE_NAME (type) == 0)
3975 TYPE_NAME (type) = x;
3976 }
1c80fb65
MM
3977 else if (type != error_mark_node && TYPE_NAME (type) != x
3978 /* We don't want to copy the type when all we're
3979 doing is making a TYPE_DECL for the purposes of
3980 inlining. */
3981 && (!TYPE_NAME (type)
3982 || TYPE_NAME (type) != DECL_ABSTRACT_ORIGIN (x)))
a1774733 3983 {
ae0a6181
MM
3984 push_obstacks (TYPE_OBSTACK (type), TYPE_OBSTACK (type));
3985
a1774733
BK
3986 DECL_ORIGINAL_TYPE (x) = type;
3987 type = build_type_copy (type);
3988 TYPE_STUB_DECL (type) = TYPE_STUB_DECL (DECL_ORIGINAL_TYPE (x));
3989 TYPE_NAME (type) = x;
3990 TREE_TYPE (x) = type;
ae0a6181
MM
3991
3992 pop_obstacks ();
a1774733 3993 }
8d08fdba 3994
8d08fdba
MS
3995 if (type != error_mark_node
3996 && TYPE_NAME (type)
3997 && TYPE_IDENTIFIER (type))
f181d4ae
MM
3998 set_identifier_type_value_with_scope (DECL_NAME (x), type,
3999 current_binding_level);
4000
8d08fdba
MS
4001 }
4002
4003 /* Multiple external decls of the same identifier ought to match.
4004
4005 We get warnings about inline functions where they are defined.
39211cd5
MS
4006 We get warnings about other functions from push_overloaded_decl.
4007
8d08fdba 4008 Avoid duplicate warnings where they are used. */
39211cd5 4009 if (TREE_PUBLIC (x) && TREE_CODE (x) != FUNCTION_DECL)
8d08fdba
MS
4010 {
4011 tree decl;
4012
31928556
JM
4013 if (IDENTIFIER_NAMESPACE_VALUE (name) != NULL_TREE
4014 && (DECL_EXTERNAL (IDENTIFIER_NAMESPACE_VALUE (name))
4015 || TREE_PUBLIC (IDENTIFIER_NAMESPACE_VALUE (name))))
4016 decl = IDENTIFIER_NAMESPACE_VALUE (name);
8d08fdba
MS
4017 else
4018 decl = NULL_TREE;
4019
39211cd5 4020 if (decl
8d08fdba
MS
4021 /* If different sort of thing, we already gave an error. */
4022 && TREE_CODE (decl) == TREE_CODE (x)
3bfdc719 4023 && !same_type_p (TREE_TYPE (x), TREE_TYPE (decl)))
8d08fdba 4024 {
8251199e
JM
4025 cp_pedwarn ("type mismatch with previous external decl", x);
4026 cp_pedwarn_at ("previous external decl of `%#D'", decl);
8d08fdba
MS
4027 }
4028 }
4029
8d08fdba
MS
4030 /* This name is new in its binding level.
4031 Install the new declaration and return it. */
2c73f9f5 4032 if (namespace_bindings_p ())
8d08fdba
MS
4033 {
4034 /* Install a global value. */
4035
8d08fdba
MS
4036 /* If the first global decl has external linkage,
4037 warn if we later see static one. */
31928556 4038 if (IDENTIFIER_GLOBAL_VALUE (name) == NULL_TREE && TREE_PUBLIC (x))
8d08fdba
MS
4039 TREE_PUBLIC (name) = 1;
4040
d8f8dca1
MM
4041 if (!(TREE_CODE (x) == TYPE_DECL && DECL_ARTIFICIAL (x)
4042 && t != NULL_TREE))
30394414
JM
4043 {
4044 if (TREE_CODE (x) == FUNCTION_DECL)
31928556
JM
4045 my_friendly_assert
4046 ((IDENTIFIER_GLOBAL_VALUE (name) == NULL_TREE)
4047 || (IDENTIFIER_GLOBAL_VALUE (name) == x), 378);
4048 SET_IDENTIFIER_NAMESPACE_VALUE (name, x);
30394414 4049 }
8d08fdba
MS
4050
4051 /* Don't forget if the function was used via an implicit decl. */
4052 if (IDENTIFIER_IMPLICIT_DECL (name)
4053 && TREE_USED (IDENTIFIER_IMPLICIT_DECL (name)))
4054 TREE_USED (x) = 1;
4055
4056 /* Don't forget if its address was taken in that way. */
4057 if (IDENTIFIER_IMPLICIT_DECL (name)
4058 && TREE_ADDRESSABLE (IDENTIFIER_IMPLICIT_DECL (name)))
4059 TREE_ADDRESSABLE (x) = 1;
4060
4061 /* Warn about mismatches against previous implicit decl. */
4062 if (IDENTIFIER_IMPLICIT_DECL (name) != NULL_TREE
4063 /* If this real decl matches the implicit, don't complain. */
4064 && ! (TREE_CODE (x) == FUNCTION_DECL
4065 && TREE_TYPE (TREE_TYPE (x)) == integer_type_node))
8251199e
JM
4066 cp_warning
4067 ("`%D' was previously implicitly declared to return `int'", x);
8d08fdba
MS
4068
4069 /* If new decl is `static' and an `extern' was seen previously,
4070 warn about it. */
a0a33927
MS
4071 if (x != NULL_TREE && t != NULL_TREE && decls_match (x, t))
4072 warn_extern_redeclared_static (x, t);
8d08fdba
MS
4073 }
4074 else
4075 {
4076 /* Here to install a non-global value. */
f181d4ae 4077 tree oldlocal = IDENTIFIER_VALUE (name);
31928556 4078 tree oldglobal = IDENTIFIER_NAMESPACE_VALUE (name);
8d08fdba 4079
f181d4ae
MM
4080 if (need_new_binding)
4081 {
0034cf72 4082 push_local_binding (name, x, 0);
f181d4ae
MM
4083 /* Because push_local_binding will hook X on to the
4084 current_binding_level's name list, we don't want to
4085 do that again below. */
4086 need_new_binding = 0;
4087 }
8d08fdba
MS
4088
4089 /* If this is a TYPE_DECL, push it into the type value slot. */
4090 if (TREE_CODE (x) == TYPE_DECL)
f181d4ae
MM
4091 set_identifier_type_value_with_scope (name, TREE_TYPE (x),
4092 current_binding_level);
8d08fdba 4093
a9aedbc2
MS
4094 /* Clear out any TYPE_DECL shadowed by a namespace so that
4095 we won't think this is a type. The C struct hack doesn't
4096 go through namespaces. */
4097 if (TREE_CODE (x) == NAMESPACE_DECL)
f181d4ae
MM
4098 set_identifier_type_value_with_scope (name, NULL_TREE,
4099 current_binding_level);
a9aedbc2 4100
8d08fdba
MS
4101 /* If this is an extern function declaration, see if we
4102 have a global definition or declaration for the function. */
4103 if (oldlocal == NULL_TREE
faae18ab 4104 && DECL_EXTERNAL (x)
31928556 4105 && oldglobal != NULL_TREE
8d08fdba 4106 && TREE_CODE (x) == FUNCTION_DECL
31928556 4107 && TREE_CODE (oldglobal) == FUNCTION_DECL)
8d08fdba
MS
4108 {
4109 /* We have one. Their types must agree. */
31928556 4110 if (decls_match (x, oldglobal))
6060a796
MS
4111 /* OK */;
4112 else
8d08fdba 4113 {
8251199e
JM
4114 cp_warning ("extern declaration of `%#D' doesn't match", x);
4115 cp_warning_at ("global declaration `%#D'", oldglobal);
8d08fdba 4116 }
8d08fdba
MS
4117 }
4118 /* If we have a local external declaration,
4119 and no file-scope declaration has yet been seen,
4120 then if we later have a file-scope decl it must not be static. */
4121 if (oldlocal == NULL_TREE
31928556 4122 && oldglobal == NULL_TREE
8d08fdba
MS
4123 && DECL_EXTERNAL (x)
4124 && TREE_PUBLIC (x))
f181d4ae 4125 TREE_PUBLIC (name) = 1;
8d08fdba
MS
4126
4127 if (DECL_FROM_INLINE (x))
4128 /* Inline decls shadow nothing. */;
4129
4130 /* Warn if shadowing an argument at the top level of the body. */
4131 else if (oldlocal != NULL_TREE && !DECL_EXTERNAL (x)
9d29e7af
JM
4132 && TREE_CODE (oldlocal) == PARM_DECL
4133 /* Don't complain if it's from an enclosing function. */
4134 && DECL_CONTEXT (oldlocal) == current_function_decl
4135 && TREE_CODE (x) != PARM_DECL)
8d08fdba
MS
4136 {
4137 /* Go to where the parms should be and see if we
4138 find them there. */
4139 struct binding_level *b = current_binding_level->level_chain;
4140
4141 if (cleanup_label)
4142 b = b->level_chain;
4143
4144 /* ARM $8.3 */
4145 if (b->parm_flag == 1)
8251199e 4146 cp_error ("declaration of `%#D' shadows a parameter", name);
8d08fdba 4147 }
f181d4ae
MM
4148 else if (warn_shadow && oldlocal != NULL_TREE
4149 && current_binding_level->is_for_scope
8d6e462b
PB
4150 && !DECL_DEAD_FOR_LOCAL (oldlocal))
4151 {
8251199e 4152 warning ("variable `%s' shadows local",
8d6e462b 4153 IDENTIFIER_POINTER (name));
8251199e 4154 cp_warning_at (" this is the shadowed declaration", oldlocal);
8d6e462b 4155 }
8d08fdba
MS
4156 /* Maybe warn if shadowing something else. */
4157 else if (warn_shadow && !DECL_EXTERNAL (x)
4158 /* No shadow warnings for internally generated vars. */
700f8a87 4159 && ! DECL_ARTIFICIAL (x)
8d08fdba
MS
4160 /* No shadow warnings for vars made for inlining. */
4161 && ! DECL_FROM_INLINE (x))
4162 {
d8e178a0 4163 const char *warnstring = NULL;
8d08fdba
MS
4164
4165 if (oldlocal != NULL_TREE && TREE_CODE (oldlocal) == PARM_DECL)
4166 warnstring = "declaration of `%s' shadows a parameter";
4167 else if (IDENTIFIER_CLASS_VALUE (name) != NULL_TREE
4ac14744 4168 && current_class_ptr
8d08fdba
MS
4169 && !TREE_STATIC (name))
4170 warnstring = "declaration of `%s' shadows a member of `this'";
4171 else if (oldlocal != NULL_TREE)
4172 warnstring = "declaration of `%s' shadows previous local";
31928556 4173 else if (oldglobal != NULL_TREE)
30394414 4174 /* XXX shadow warnings in outer-more namespaces */
8d08fdba
MS
4175 warnstring = "declaration of `%s' shadows global declaration";
4176
4177 if (warnstring)
4178 warning (warnstring, IDENTIFIER_POINTER (name));
4179 }
e1cd6e56 4180 }
8d08fdba 4181
e1cd6e56 4182 if (TREE_CODE (x) == FUNCTION_DECL)
f30432d7 4183 check_default_args (x);
8145f082
MS
4184
4185 /* Keep count of variables in this level with incomplete type. */
8145f082 4186 if (TREE_CODE (x) == VAR_DECL
28cbf42c 4187 && TREE_TYPE (x) != error_mark_node
f30432d7
MS
4188 && ((TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
4189 && PROMOTES_TO_AGGR_TYPE (TREE_TYPE (x), ARRAY_TYPE))
4190 /* RTTI TD entries are created while defining the type_info. */
4191 || (TYPE_LANG_SPECIFIC (TREE_TYPE (x))
4192 && TYPE_BEING_DEFINED (TREE_TYPE (x)))))
f181d4ae
MM
4193 current_binding_level->incomplete
4194 = tree_cons (NULL_TREE, x, current_binding_level->incomplete);
8d08fdba
MS
4195 }
4196
f181d4ae
MM
4197 if (need_new_binding)
4198 {
4199 /* Put decls on list in reverse order.
4200 We will reverse them later if necessary. */
4201 TREE_CHAIN (x) = current_binding_level->names;
4202 current_binding_level->names = x;
8f032717
MM
4203 if (current_binding_level == global_binding_level
4204 && !TREE_PERMANENT (x))
f181d4ae
MM
4205 my_friendly_abort (124);
4206 }
8d08fdba
MS
4207
4208 return x;
4209}
4210
5566b478
MS
4211/* Same as pushdecl, but define X in binding-level LEVEL. We rely on the
4212 caller to set DECL_CONTEXT properly. */
8d08fdba
MS
4213
4214static tree
4215pushdecl_with_scope (x, level)
4216 tree x;
4217 struct binding_level *level;
4218{
8d019cef 4219 register struct binding_level *b;
5566b478 4220 tree function_decl = current_function_decl;
8d08fdba 4221
5566b478 4222 current_function_decl = NULL_TREE;
8d019cef
JM
4223 if (level->parm_flag == 2)
4224 {
4225 b = class_binding_level;
4226 class_binding_level = level;
4227 pushdecl_class_level (x);
4228 class_binding_level = b;
4229 }
4230 else
4231 {
4232 b = current_binding_level;
4233 current_binding_level = level;
4234 x = pushdecl (x);
4235 current_binding_level = b;
4236 }
5566b478 4237 current_function_decl = function_decl;
8d08fdba
MS
4238 return x;
4239}
4240
2c73f9f5 4241/* Like pushdecl, only it places X in the current namespace,
8d08fdba 4242 if appropriate. */
e92cc029 4243
8d08fdba 4244tree
2c73f9f5 4245pushdecl_namespace_level (x)
8d08fdba
MS
4246 tree x;
4247{
8f032717 4248 register struct binding_level *b = current_binding_level;
2c73f9f5
ML
4249 register tree t;
4250
4251 t = pushdecl_with_scope (x, NAMESPACE_LEVEL (current_namespace));
8d08fdba
MS
4252
4253 /* Now, the type_shadowed stack may screw us. Munge it so it does
4254 what we want. */
4255 if (TREE_CODE (x) == TYPE_DECL)
4256 {
4257 tree name = DECL_NAME (x);
4258 tree newval;
4259 tree *ptr = (tree *)0;
4260 for (; b != global_binding_level; b = b->level_chain)
4261 {
4262 tree shadowed = b->type_shadowed;
4263 for (; shadowed; shadowed = TREE_CHAIN (shadowed))
4264 if (TREE_PURPOSE (shadowed) == name)
4265 {
4266 ptr = &TREE_VALUE (shadowed);
4267 /* Can't break out of the loop here because sometimes
4268 a binding level will have duplicate bindings for
4269 PT names. It's gross, but I haven't time to fix it. */
4270 }
4271 }
4272 newval = TREE_TYPE (x);
4273 if (ptr == (tree *)0)
4274 {
4275 /* @@ This shouldn't be needed. My test case "zstring.cc" trips
4276 up here if this is changed to an assertion. --KR */
4277 SET_IDENTIFIER_TYPE_VALUE (name, newval);
4278 }
4279 else
4280 {
8d08fdba
MS
4281 *ptr = newval;
4282 }
4283 }
4284 return t;
4285}
4286
2c73f9f5
ML
4287/* Like pushdecl, only it places X in GLOBAL_BINDING_LEVEL,
4288 if appropriate. */
4289
4290tree
4291pushdecl_top_level (x)
4292 tree x;
4293{
4294 tree cur_namespace = current_namespace;
4295 current_namespace = global_namespace;
4296 x = pushdecl_namespace_level (x);
4297 current_namespace = cur_namespace;
4298 return x;
4299}
4300
8d08fdba 4301/* Make the declaration of X appear in CLASS scope. */
e92cc029 4302
61a127b3 4303void
8d08fdba
MS
4304pushdecl_class_level (x)
4305 tree x;
4306{
4307 /* Don't use DECL_ASSEMBLER_NAME here! Everything that looks in class
4308 scope looks for the pre-mangled name. */
8f032717
MM
4309 register tree name;
4310
4311 if (TREE_CODE (x) == OVERLOAD)
4312 x = OVL_CURRENT (x);
4313 name = DECL_NAME (x);
8d08fdba
MS
4314
4315 if (name)
4316 {
4317 push_class_level_binding (name, x);
4318 if (TREE_CODE (x) == TYPE_DECL)
f181d4ae 4319 set_identifier_type_value (name, TREE_TYPE (x));
8d08fdba 4320 }
6bdb8141 4321 else if (ANON_AGGR_TYPE_P (TREE_TYPE (x)))
8f032717
MM
4322 {
4323 tree f;
4324
4325 for (f = TYPE_FIELDS (TREE_TYPE (x));
4326 f;
4327 f = TREE_CHAIN (f))
4328 pushdecl_class_level (f);
4329 }
8d08fdba
MS
4330}
4331
bd6dd845 4332#if 0
7177d104
MS
4333/* This function is used to push the mangled decls for nested types into
4334 the appropriate scope. Previously pushdecl_top_level was used, but that
4335 is incorrect for members of local classes. */
e92cc029 4336
5566b478 4337void
7177d104
MS
4338pushdecl_nonclass_level (x)
4339 tree x;
4340{
4341 struct binding_level *b = current_binding_level;
4342
7177d104 4343 my_friendly_assert (b->parm_flag != 2, 180);
7177d104 4344
5566b478 4345#if 0
7177d104
MS
4346 /* Get out of template binding levels */
4347 while (b->pseudo_global)
4348 b = b->level_chain;
5566b478 4349#endif
7177d104
MS
4350
4351 pushdecl_with_scope (x, b);
4352}
bd6dd845 4353#endif
7177d104 4354
8d08fdba
MS
4355/* Make the declaration(s) of X appear in CLASS scope
4356 under the name NAME. */
e92cc029 4357
8d08fdba
MS
4358void
4359push_class_level_binding (name, x)
4360 tree name;
4361 tree x;
4362{
8f032717 4363 tree binding;
98c1c668
JM
4364 /* The class_binding_level will be NULL if x is a template
4365 parameter name in a member template. */
4366 if (!class_binding_level)
4367 return;
4368
908c4e83
MM
4369 /* Make sure that this new member does not have the same name
4370 as a template parameter. */
4371 if (TYPE_BEING_DEFINED (current_class_type))
4372 check_template_shadow (x);
4373
f181d4ae
MM
4374 /* If this declaration shadows a declaration from an enclosing
4375 class, then we will need to restore IDENTIFIER_CLASS_VALUE when
4376 we leave this class. Record the shadowed declaration here. */
8f032717
MM
4377 binding = IDENTIFIER_BINDING (name);
4378 if (binding
4379 && ((TREE_CODE (x) == OVERLOAD
4380 && BINDING_VALUE (binding)
4381 && is_overloaded_fn (BINDING_VALUE (binding)))
4382 || INHERITED_VALUE_BINDING_P (binding)))
4383 {
4384 tree shadow;
4385 tree old_decl;
4386
4387 /* If the old binding was from a base class, and was for a tag
4388 name, slide it over to make room for the new binding. The
4389 old binding is still visible if explicitly qualified with a
4390 class-key. */
4391 if (INHERITED_VALUE_BINDING_P (binding)
4392 && BINDING_VALUE (binding)
4393 && TREE_CODE (BINDING_VALUE (binding)) == TYPE_DECL
4394 && DECL_ARTIFICIAL (BINDING_VALUE (binding))
4395 && !(TREE_CODE (x) == TYPE_DECL && DECL_ARTIFICIAL (x)))
4396 {
4397 old_decl = BINDING_TYPE (binding);
4398 BINDING_TYPE (binding) = BINDING_VALUE (binding);
4399 BINDING_VALUE (binding) = NULL_TREE;
4400 INHERITED_VALUE_BINDING_P (binding) = 0;
4401 }
4402 else
4403 old_decl = BINDING_VALUE (binding);
4404
4405 /* There was already a binding for X containing fewer
4406 functions than are named in X. Find the previous
4407 declaration of X on the class-shadowed list, and update it. */
4408 for (shadow = class_binding_level->class_shadowed;
4409 shadow;
4410 shadow = TREE_CHAIN (shadow))
4411 if (TREE_PURPOSE (shadow) == name
4412 && TREE_TYPE (shadow) == old_decl)
4413 {
4414 BINDING_VALUE (binding) = x;
4415 INHERITED_VALUE_BINDING_P (binding) = 0;
4416 TREE_TYPE (shadow) = x;
4417 return;
4418 }
4419 }
f181d4ae 4420
8f032717
MM
4421 /* If we didn't replace an existing binding, put the binding on the
4422 stack of bindings for the identifier, and update
4423 IDENTIFIER_CLASS_VALUE. */
4424 if (push_class_binding (name, x))
4425 {
3f1892e1 4426 push_cache_obstack ();
8f032717
MM
4427 class_binding_level->class_shadowed
4428 = tree_cons (name, IDENTIFIER_CLASS_VALUE (name),
4429 class_binding_level->class_shadowed);
4430 pop_obstacks ();
4431 /* Record the value we are binding NAME to so that we can know
4432 what to pop later. */
4433 TREE_TYPE (class_binding_level->class_shadowed) = x;
4434 }
8d08fdba
MS
4435}
4436
2c73f9f5
ML
4437/* Insert another USING_DECL into the current binding level,
4438 returning this declaration. If this is a redeclaration,
4439 do nothing and return NULL_TREE. */
e92cc029 4440
2c73f9f5
ML
4441tree
4442push_using_decl (scope, name)
4443 tree scope;
4444 tree name;
8d08fdba 4445{
2c73f9f5
ML
4446 tree decl;
4447
2c73f9f5
ML
4448 my_friendly_assert (TREE_CODE (scope) == NAMESPACE_DECL, 383);
4449 my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, 384);
4450 for (decl = current_binding_level->usings; decl; decl = TREE_CHAIN (decl))
4451 if (DECL_INITIAL (decl) == scope && DECL_NAME (decl) == name)
4452 break;
4453 if (decl)
4454 return NULL_TREE;
4455 decl = build_lang_decl (USING_DECL, name, void_type_node);
4456 DECL_INITIAL (decl) = scope;
4457 TREE_CHAIN (decl) = current_binding_level->usings;
4458 current_binding_level->usings = decl;
4459 return decl;
8d08fdba
MS
4460}
4461
ea9635c7
ML
4462/* Add namespace to using_directives. Return NULL_TREE if nothing was
4463 changed (i.e. there was already a directive), or the fresh
4464 TREE_LIST otherwise. */
4465
4466tree
9ed182dc 4467push_using_directive (used)
ea9635c7 4468 tree used;
ea9635c7
ML
4469{
4470 tree ud = current_binding_level->using_directives;
9ed182dc 4471 tree iter, ancestor;
ea9635c7
ML
4472
4473 /* Check if we already have this. */
4474 if (purpose_member (used, ud) != NULL_TREE)
4475 return NULL_TREE;
00dc6358
JM
4476
4477 /* Recursively add all namespaces used. */
4478 for (iter = DECL_NAMESPACE_USING (used); iter; iter = TREE_CHAIN (iter))
9ed182dc 4479 push_using_directive (TREE_PURPOSE (iter));
00dc6358 4480
9ed182dc 4481 ancestor = namespace_ancestor (current_decl_namespace (), used);
00dc6358 4482 ud = current_binding_level->using_directives;
ea9635c7
ML
4483 ud = perm_tree_cons (used, ancestor, ud);
4484 current_binding_level->using_directives = ud;
4485 return ud;
4486}
4487
f181d4ae
MM
4488/* DECL is a FUNCTION_DECL for a non-member function, which may have
4489 other definitions already in place. We get around this by making
4490 the value of the identifier point to a list of all the things that
4491 want to be referenced by that name. It is then up to the users of
4492 that name to decide what to do with that list.
8d08fdba
MS
4493
4494 DECL may also be a TEMPLATE_DECL, with a FUNCTION_DECL in its DECL_RESULT
4495 slot. It is dealt with the same way.
4496
7bdbfa05
MM
4497 FLAGS is a bitwise-or of the following values:
4498 PUSH_LOCAL: Bind DECL in the current scope, rather than at
4499 namespace scope.
4500 PUSH_USING: DECL is being pushed as the result of a using
4501 declaration.
4502
8d08fdba
MS
4503 The value returned may be a previous declaration if we guessed wrong
4504 about what language DECL should belong to (C or C++). Otherwise,
4505 it's always DECL (and never something that's not a _DECL). */
e92cc029 4506
7bdbfa05
MM
4507tree
4508push_overloaded_decl (decl, flags)
8d08fdba 4509 tree decl;
7bdbfa05 4510 int flags;
8d08fdba 4511{
f181d4ae 4512 tree name = DECL_NAME (decl);
700f8a87 4513 tree old;
f181d4ae 4514 tree new_binding;
7bdbfa05 4515 int doing_global = (namespace_bindings_p () || !(flags & PUSH_LOCAL));
700f8a87
MS
4516
4517 if (doing_global)
9f33663b 4518 old = namespace_binding (name, DECL_CONTEXT (decl));
700f8a87 4519 else
f181d4ae 4520 old = lookup_name_current_level (name);
8d08fdba 4521
700f8a87 4522 if (old)
8d08fdba 4523 {
e1cd6e56 4524 if (TREE_CODE (old) == TYPE_DECL && DECL_ARTIFICIAL (old))
8926095f 4525 {
700f8a87 4526 tree t = TREE_TYPE (old);
cdf5b885
MS
4527 if (IS_AGGR_TYPE (t) && warn_shadow
4528 && (! DECL_IN_SYSTEM_HEADER (decl)
4529 || ! DECL_IN_SYSTEM_HEADER (old)))
8251199e 4530 cp_warning ("`%#D' hides constructor for `%#T'", decl, t);
5b605f68 4531 old = NULL_TREE;
8926095f 4532 }
700f8a87 4533 else if (is_overloaded_fn (old))
8d08fdba 4534 {
8d08fdba
MS
4535 tree tmp;
4536
2c73f9f5 4537 for (tmp = old; tmp; tmp = OVL_NEXT (tmp))
7bdbfa05
MM
4538 {
4539 tree fn = OVL_CURRENT (tmp);
4540
4541 if (TREE_CODE (tmp) == OVERLOAD && OVL_USED (tmp)
4542 && !(flags & PUSH_USING)
4543 && compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
4544 TYPE_ARG_TYPES (TREE_TYPE (decl))))
4545 cp_error ("`%#D' conflicts with previous using declaration `%#D'",
4546 decl, fn);
4547
4548 if (duplicate_decls (decl, fn))
4549 return fn;
4550 }
8d08fdba 4551 }
e1cd6e56
MS
4552 else
4553 {
8251199e
JM
4554 cp_error_at ("previous non-function declaration `%#D'", old);
4555 cp_error ("conflicts with function declaration `%#D'", decl);
a9aedbc2 4556 return decl;
e1cd6e56 4557 }
8d08fdba 4558 }
7177d104 4559
700f8a87 4560 if (old || TREE_CODE (decl) == TEMPLATE_DECL)
8d08fdba 4561 {
2c73f9f5 4562 if (old && TREE_CODE (old) != OVERLOAD)
f181d4ae
MM
4563 new_binding = ovl_cons (decl, ovl_cons (old, NULL_TREE));
4564 else
4565 new_binding = ovl_cons (decl, old);
347d73d7
ML
4566 if (flags & PUSH_USING)
4567 OVL_USED (new_binding) = 1;
8d08fdba
MS
4568 }
4569 else
f181d4ae
MM
4570 /* NAME is not ambiguous. */
4571 new_binding = decl;
700f8a87
MS
4572
4573 if (doing_global)
f181d4ae 4574 set_namespace_binding (name, current_namespace, new_binding);
700f8a87 4575 else
f181d4ae
MM
4576 {
4577 /* We only create an OVERLOAD if there was a previous binding at
0580c9aa
ML
4578 this level, or if decl is a template. In the former case, we
4579 need to remove the old binding and replace it with the new
4580 binding. We must also run through the NAMES on the binding
4581 level where the name was bound to update the chain. */
4582
4583 if (TREE_CODE (new_binding) == OVERLOAD && old)
f181d4ae
MM
4584 {
4585 tree *d;
4586
4587 for (d = &BINDING_LEVEL (IDENTIFIER_BINDING (name))->names;
4588 *d;
4589 d = &TREE_CHAIN (*d))
4590 if (*d == old
4591 || (TREE_CODE (*d) == TREE_LIST
4592 && TREE_VALUE (*d) == old))
4593 {
d8f8dca1
MM
4594 if (TREE_CODE (*d) == TREE_LIST)
4595 /* Just replace the old binding with the new. */
4596 TREE_VALUE (*d) = new_binding;
4597 else
4598 /* Build a TREE_LIST to wrap the OVERLOAD. */
4599 *d = build_tree_list (NULL_TREE, new_binding);
4600
4601 /* And update the CPLUS_BINDING node. */
4602 BINDING_VALUE (IDENTIFIER_BINDING (name))
4603 = new_binding;
4604 return decl;
f181d4ae
MM
4605 }
4606
d8f8dca1
MM
4607 /* We should always find a previous binding in this case. */
4608 my_friendly_abort (0);
f181d4ae
MM
4609 }
4610
4611 /* Install the new binding. */
0034cf72 4612 push_local_binding (name, new_binding, flags);
f181d4ae 4613 }
700f8a87 4614
8d08fdba
MS
4615 return decl;
4616}
4617\f
4618/* Generate an implicit declaration for identifier FUNCTIONID
4619 as a function of type int (). Print a warning if appropriate. */
4620
4621tree
4622implicitly_declare (functionid)
4623 tree functionid;
4624{
4625 register tree decl;
4626 int temp = allocation_temporary_p ();
4627
4628 push_obstacks_nochange ();
4629
4630 /* Save the decl permanently so we can warn if definition follows.
4631 In ANSI C, warn_implicit is usually false, so the saves little space.
4632 But in C++, it's usually true, hence the extra code. */
d22c8596 4633 if (temp && (! warn_implicit || toplevel_bindings_p ()))
8d08fdba
MS
4634 end_temporary_allocation ();
4635
4636 /* We used to reuse an old implicit decl here,
4637 but this loses with inline functions because it can clobber
4638 the saved decl chains. */
4639 decl = build_lang_decl (FUNCTION_DECL, functionid, default_function_type);
4640
4641 DECL_EXTERNAL (decl) = 1;
4642 TREE_PUBLIC (decl) = 1;
4643
4644 /* ANSI standard says implicit declarations are in the innermost block.
d22c8596 4645 So we record the decl in the standard fashion. */
8d08fdba
MS
4646 pushdecl (decl);
4647 rest_of_decl_compilation (decl, NULL_PTR, 0, 0);
4648
4649 if (warn_implicit
4650 /* Only one warning per identifier. */
4651 && IDENTIFIER_IMPLICIT_DECL (functionid) == NULL_TREE)
4652 {
8251199e 4653 cp_pedwarn ("implicit declaration of function `%#D'", decl);
8d08fdba
MS
4654 }
4655
4656 SET_IDENTIFIER_IMPLICIT_DECL (functionid, decl);
4657
4658 pop_obstacks ();
4659
4660 return decl;
4661}
4662
8251199e 4663/* Return zero if the declaration NEWDECL is valid
8d08fdba
MS
4664 when the declaration OLDDECL (assumed to be for the same name)
4665 has already been seen.
4666 Otherwise return an error message format string with a %s
4667 where the identifier should go. */
4668
d8e178a0 4669static const char *
8d08fdba
MS
4670redeclaration_error_message (newdecl, olddecl)
4671 tree newdecl, olddecl;
4672{
4673 if (TREE_CODE (newdecl) == TYPE_DECL)
4674 {
4675 /* Because C++ can put things into name space for free,
4676 constructs like "typedef struct foo { ... } foo"
4677 would look like an erroneous redeclaration. */
3bfdc719 4678 if (same_type_p (TREE_TYPE (newdecl), TREE_TYPE (olddecl)))
8251199e 4679 return 0;
8d08fdba 4680 else
8251199e 4681 return "redefinition of `%#D'";
8d08fdba
MS
4682 }
4683 else if (TREE_CODE (newdecl) == FUNCTION_DECL)
4684 {
4685 /* If this is a pure function, its olddecl will actually be
4686 the original initialization to `0' (which we force to call
4687 abort()). Don't complain about redefinition in this case. */
4688 if (DECL_LANG_SPECIFIC (olddecl) && DECL_ABSTRACT_VIRTUAL_P (olddecl))
8251199e 4689 return 0;
8d08fdba 4690
2c73f9f5
ML
4691 /* If both functions come from different namespaces, this is not
4692 a redeclaration - this is a conflict with a used function. */
6eb3bb27 4693 if (DECL_NAMESPACE_SCOPE_P (olddecl)
2c73f9f5 4694 && DECL_CONTEXT (olddecl) != DECL_CONTEXT (newdecl))
8251199e 4695 return "`%D' conflicts with used function";
2c73f9f5 4696
db5ae43f
MS
4697 /* We'll complain about linkage mismatches in
4698 warn_extern_redeclared_static. */
4699
2c73f9f5 4700 /* Defining the same name twice is no good. */
8d08fdba 4701 if (DECL_INITIAL (olddecl) != NULL_TREE
faae18ab 4702 && DECL_INITIAL (newdecl) != NULL_TREE)
8d08fdba
MS
4703 {
4704 if (DECL_NAME (olddecl) == NULL_TREE)
8251199e 4705 return "`%#D' not declared in class";
8d08fdba 4706 else
8251199e 4707 return "redefinition of `%#D'";
8d08fdba 4708 }
8251199e 4709 return 0;
8d08fdba 4710 }
51c184be
MS
4711 else if (TREE_CODE (newdecl) == TEMPLATE_DECL)
4712 {
ec255269
MS
4713 if ((TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == FUNCTION_DECL
4714 && DECL_INITIAL (DECL_TEMPLATE_RESULT (newdecl))
4715 && DECL_INITIAL (DECL_TEMPLATE_RESULT (olddecl)))
4716 || (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL
4717 && TYPE_SIZE (TREE_TYPE (newdecl))
4718 && TYPE_SIZE (TREE_TYPE (olddecl))))
8251199e
JM
4719 return "redefinition of `%#D'";
4720 return 0;
51c184be 4721 }
5566b478 4722 else if (toplevel_bindings_p ())
8d08fdba
MS
4723 {
4724 /* Objects declared at top level: */
4725 /* If at least one is a reference, it's ok. */
4726 if (DECL_EXTERNAL (newdecl) || DECL_EXTERNAL (olddecl))
8251199e 4727 return 0;
8926095f 4728 /* Reject two definitions. */
8251199e 4729 return "redefinition of `%#D'";
8d08fdba
MS
4730 }
4731 else
4732 {
4733 /* Objects declared with block scope: */
4734 /* Reject two definitions, and reject a definition
4735 together with an external reference. */
4736 if (!(DECL_EXTERNAL (newdecl) && DECL_EXTERNAL (olddecl)))
8251199e
JM
4737 return "redeclaration of `%#D'";
4738 return 0;
8d08fdba
MS
4739 }
4740}
4741\f
4742/* Get the LABEL_DECL corresponding to identifier ID as a label.
4743 Create one if none exists so far for the current function.
4744 This function is called for both label definitions and label references. */
4745
4746tree
4747lookup_label (id)
4748 tree id;
4749{
4750 register tree decl = IDENTIFIER_LABEL_VALUE (id);
4751
4752 if (current_function_decl == NULL_TREE)
4753 {
8251199e 4754 error ("label `%s' referenced outside of any function",
8d08fdba
MS
4755 IDENTIFIER_POINTER (id));
4756 return NULL_TREE;
4757 }
4758
4759 if ((decl == NULL_TREE
4760 || DECL_SOURCE_LINE (decl) == 0)
e349ee73
MS
4761 && (named_label_uses == NULL
4762 || named_label_uses->names_in_scope != current_binding_level->names
4763 || named_label_uses->label_decl != decl))
4764 {
4765 struct named_label_list *new_ent;
4766 new_ent
4767 = (struct named_label_list*)oballoc (sizeof (struct named_label_list));
4768 new_ent->label_decl = decl;
4769 new_ent->names_in_scope = current_binding_level->names;
4770 new_ent->binding_level = current_binding_level;
4771 new_ent->lineno_o_goto = lineno;
4772 new_ent->filename_o_goto = input_filename;
4773 new_ent->next = named_label_uses;
4774 named_label_uses = new_ent;
8d08fdba
MS
4775 }
4776
4777 /* Use a label already defined or ref'd with this name. */
4778 if (decl != NULL_TREE)
4779 {
4780 /* But not if it is inherited and wasn't declared to be inheritable. */
4781 if (DECL_CONTEXT (decl) != current_function_decl
4782 && ! C_DECLARED_LABEL_FLAG (decl))
4783 return shadow_label (id);
4784 return decl;
4785 }
4786
4787 decl = build_decl (LABEL_DECL, id, void_type_node);
4788
cffa8729
MS
4789 /* Make sure every label has an rtx. */
4790 label_rtx (decl);
4791
8d08fdba
MS
4792 /* A label not explicitly declared must be local to where it's ref'd. */
4793 DECL_CONTEXT (decl) = current_function_decl;
4794
4795 DECL_MODE (decl) = VOIDmode;
4796
4797 /* Say where one reference is to the label,
4798 for the sake of the error if it is not defined. */
4799 DECL_SOURCE_LINE (decl) = lineno;
4800 DECL_SOURCE_FILE (decl) = input_filename;
4801
4802 SET_IDENTIFIER_LABEL_VALUE (id, decl);
4803
4804 named_labels = tree_cons (NULL_TREE, decl, named_labels);
e349ee73 4805 named_label_uses->label_decl = decl;
8d08fdba
MS
4806
4807 return decl;
4808}
4809
4810/* Make a label named NAME in the current function,
4811 shadowing silently any that may be inherited from containing functions
4812 or containing scopes.
4813
4814 Note that valid use, if the label being shadowed
4815 comes from another scope in the same function,
4816 requires calling declare_nonlocal_label right away. */
4817
4818tree
4819shadow_label (name)
4820 tree name;
4821{
4822 register tree decl = IDENTIFIER_LABEL_VALUE (name);
4823
4824 if (decl != NULL_TREE)
4825 {
4826 shadowed_labels = tree_cons (NULL_TREE, decl, shadowed_labels);
4827 SET_IDENTIFIER_LABEL_VALUE (name, NULL_TREE);
8d08fdba
MS
4828 }
4829
4830 return lookup_label (name);
4831}
4832
4833/* Define a label, specifying the location in the source file.
4834 Return the LABEL_DECL node for the label, if the definition is valid.
4835 Otherwise return 0. */
4836
4837tree
4838define_label (filename, line, name)
4839 char *filename;
4840 int line;
4841 tree name;
4842{
5566b478
MS
4843 tree decl;
4844
4845 if (minimal_parse_mode)
4846 {
4847 push_obstacks (&permanent_obstack, &permanent_obstack);
4848 decl = build_decl (LABEL_DECL, name, void_type_node);
4849 pop_obstacks ();
4850 DECL_SOURCE_LINE (decl) = line;
4851 DECL_SOURCE_FILE (decl) = filename;
4852 add_tree (decl);
4853 return decl;
4854 }
4855
4856 decl = lookup_label (name);
8d08fdba
MS
4857
4858 /* After labels, make any new cleanups go into their
4859 own new (temporary) binding contour. */
4860 current_binding_level->more_cleanups_ok = 0;
4861
4862 /* If label with this name is known from an outer context, shadow it. */
4863 if (decl != NULL_TREE && DECL_CONTEXT (decl) != current_function_decl)
4864 {
4865 shadowed_labels = tree_cons (NULL_TREE, decl, shadowed_labels);
4866 SET_IDENTIFIER_LABEL_VALUE (name, NULL_TREE);
4867 decl = lookup_label (name);
4868 }
4869
e1cd6e56 4870 if (name == get_identifier ("wchar_t"))
8251199e 4871 cp_pedwarn ("label named wchar_t");
e1cd6e56 4872
8d08fdba
MS
4873 if (DECL_INITIAL (decl) != NULL_TREE)
4874 {
8251199e 4875 cp_error ("duplicate label `%D'", decl);
8d08fdba
MS
4876 return 0;
4877 }
4878 else
4879 {
e349ee73 4880 struct named_label_list *uses, *prev;
28cbf42c 4881 int identified = 0;
227cf171 4882 int saw_eh = 0;
8d08fdba
MS
4883
4884 /* Mark label as having been defined. */
4885 DECL_INITIAL (decl) = error_mark_node;
4886 /* Say where in the source. */
4887 DECL_SOURCE_FILE (decl) = filename;
4888 DECL_SOURCE_LINE (decl) = line;
4889
e349ee73
MS
4890 prev = NULL;
4891 uses = named_label_uses;
4892 while (uses != NULL)
4893 if (uses->label_decl == decl)
8d08fdba
MS
4894 {
4895 struct binding_level *b = current_binding_level;
4896 while (b)
4897 {
4898 tree new_decls = b->names;
e349ee73
MS
4899 tree old_decls = (b == uses->binding_level)
4900 ? uses->names_in_scope : NULL_TREE;
8d08fdba
MS
4901 while (new_decls != old_decls)
4902 {
4903 if (TREE_CODE (new_decls) == VAR_DECL
4904 /* Don't complain about crossing initialization
4905 of internal entities. They can't be accessed,
4906 and they should be cleaned up
4907 by the time we get to the label. */
700f8a87 4908 && ! DECL_ARTIFICIAL (new_decls)
5524676d
JM
4909 && !(DECL_INITIAL (new_decls) == NULL_TREE
4910 && pod_type_p (TREE_TYPE (new_decls))))
8d08fdba 4911 {
b607c87f
JM
4912 /* This is really only important if we're crossing
4913 an initialization. The POD stuff is just
4914 pedantry; why should it matter if the class
4915 contains a field of pointer to member type? */
4916 int problem = (DECL_INITIAL (new_decls)
4917 || (TYPE_NEEDS_CONSTRUCTING
4918 (TREE_TYPE (new_decls))));
4919
4920 if (! identified)
e349ee73 4921 {
b607c87f
JM
4922 if (problem)
4923 {
4924 cp_error ("jump to label `%D'", decl);
4925 error_with_file_and_line
4926 (uses->filename_o_goto,
4927 uses->lineno_o_goto, " from here");
4928 }
4929 else
4930 {
4931 cp_pedwarn ("jump to label `%D'", decl);
4932 pedwarn_with_file_and_line
4933 (uses->filename_o_goto,
4934 uses->lineno_o_goto, " from here");
4935 }
e349ee73 4936 identified = 1;
227cf171 4937 }
b607c87f
JM
4938
4939 if (problem)
5524676d
JM
4940 cp_error_at (" crosses initialization of `%#D'",
4941 new_decls);
4942 else
b607c87f 4943 cp_pedwarn_at (" enters scope of non-POD `%#D'",
5524676d 4944 new_decls);
8d08fdba
MS
4945 }
4946 new_decls = TREE_CHAIN (new_decls);
4947 }
e349ee73 4948 if (b == uses->binding_level)
8d08fdba 4949 break;
227cf171
JM
4950 if (b->eh_region && ! saw_eh)
4951 {
4952 if (! identified)
4953 {
4954 cp_error ("jump to label `%D'", decl);
4955 error_with_file_and_line
4956 (uses->filename_o_goto,
4957 uses->lineno_o_goto, " from here");
4958 identified = 1;
4959 }
4960 error (" enters exception handling block");
4961 saw_eh = 1;
4962 }
8d08fdba
MS
4963 b = b->level_chain;
4964 }
4965
e349ee73
MS
4966 if (prev != NULL)
4967 prev->next = uses->next;
8d08fdba 4968 else
e349ee73
MS
4969 named_label_uses = uses->next;
4970
4971 uses = uses->next;
4972 }
4973 else
4974 {
4975 prev = uses;
4976 uses = uses->next;
8d08fdba
MS
4977 }
4978 current_function_return_value = NULL_TREE;
4979 return decl;
4980 }
4981}
4982
a5894242
MS
4983struct cp_switch
4984{
4985 struct binding_level *level;
4986 struct cp_switch *next;
4987};
4988
4989static struct cp_switch *switch_stack;
4990
4991void
4992push_switch ()
4993{
4994 struct cp_switch *p
4995 = (struct cp_switch *) oballoc (sizeof (struct cp_switch));
4996 p->level = current_binding_level;
4997 p->next = switch_stack;
4998 switch_stack = p;
4999}
5000
5001void
5002pop_switch ()
5003{
5004 switch_stack = switch_stack->next;
5005}
5006
8d08fdba
MS
5007/* Same, but for CASE labels. If DECL is NULL_TREE, it's the default. */
5008/* XXX Note decl is never actually used. (bpk) */
e92cc029 5009
8d08fdba 5010void
b370501f 5011define_case_label ()
8d08fdba
MS
5012{
5013 tree cleanup = last_cleanup_this_contour ();
a5894242
MS
5014 struct binding_level *b = current_binding_level;
5015 int identified = 0;
5016
8d08fdba
MS
5017 if (cleanup)
5018 {
5019 static int explained = 0;
8251199e
JM
5020 cp_warning_at ("destructor needed for `%#D'", TREE_PURPOSE (cleanup));
5021 warning ("where case label appears here");
8d08fdba
MS
5022 if (!explained)
5023 {
8251199e
JM
5024 warning ("(enclose actions of previous case statements requiring");
5025 warning ("destructors in their own binding contours.)");
8d08fdba
MS
5026 explained = 1;
5027 }
5028 }
5029
a5894242
MS
5030 for (; b && b != switch_stack->level; b = b->level_chain)
5031 {
5032 tree new_decls = b->names;
5033 for (; new_decls; new_decls = TREE_CHAIN (new_decls))
5034 {
5035 if (TREE_CODE (new_decls) == VAR_DECL
5036 /* Don't complain about crossing initialization
5037 of internal entities. They can't be accessed,
5038 and they should be cleaned up
5039 by the time we get to the label. */
5040 && ! DECL_ARTIFICIAL (new_decls)
5041 && ((DECL_INITIAL (new_decls) != NULL_TREE
5042 && DECL_INITIAL (new_decls) != error_mark_node)
5043 || TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (new_decls))))
5044 {
5045 if (! identified)
8251199e 5046 error ("jump to case label");
a5894242 5047 identified = 1;
8251199e 5048 cp_error_at (" crosses initialization of `%#D'",
a5894242
MS
5049 new_decls);
5050 }
5051 }
5052 }
5053
8d08fdba
MS
5054 /* After labels, make any new cleanups go into their
5055 own new (temporary) binding contour. */
5056
5057 current_binding_level->more_cleanups_ok = 0;
5058 current_function_return_value = NULL_TREE;
5059}
5060\f
5061/* Return the list of declarations of the current level.
5062 Note that this list is in reverse order unless/until
5063 you nreverse it; and when you do nreverse it, you must
5064 store the result back using `storedecls' or you will lose. */
5065
5066tree
5067getdecls ()
5068{
5069 return current_binding_level->names;
5070}
5071
5072/* Return the list of type-tags (for structs, etc) of the current level. */
5073
5074tree
5075gettags ()
5076{
5077 return current_binding_level->tags;
5078}
5079
5080/* Store the list of declarations of the current level.
5081 This is done for the parameter declarations of a function being defined,
5082 after they are modified in the light of any missing parameters. */
5083
5084static void
5085storedecls (decls)
5086 tree decls;
5087{
5088 current_binding_level->names = decls;
5089}
5090
5091/* Similarly, store the list of tags of the current level. */
5092
280f9385 5093void
8d08fdba
MS
5094storetags (tags)
5095 tree tags;
5096{
5097 current_binding_level->tags = tags;
5098}
5099\f
5100/* Given NAME, an IDENTIFIER_NODE,
5101 return the structure (or union or enum) definition for that name.
5102 Searches binding levels from BINDING_LEVEL up to the global level.
5103 If THISLEVEL_ONLY is nonzero, searches only the specified context
5104 (but skips any tag-transparent contexts to find one that is
5105 meaningful for tags).
5106 FORM says which kind of type the caller wants;
5107 it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
5108 If the wrong kind of type is found, and it's not a template, an error is
5109 reported. */
5110
5111static tree
5112lookup_tag (form, name, binding_level, thislevel_only)
5113 enum tree_code form;
8d08fdba 5114 tree name;
cffa8729 5115 struct binding_level *binding_level;
8d08fdba
MS
5116 int thislevel_only;
5117{
5118 register struct binding_level *level;
36a117a5
MM
5119 /* Non-zero if, we should look past a pseudo-global level, even if
5120 THISLEVEL_ONLY. */
5121 int allow_pseudo_global = 1;
8d08fdba
MS
5122
5123 for (level = binding_level; level; level = level->level_chain)
5124 {
5125 register tree tail;
5126 if (ANON_AGGRNAME_P (name))
5127 for (tail = level->tags; tail; tail = TREE_CHAIN (tail))
5128 {
5129 /* There's no need for error checking here, because
5130 anon names are unique throughout the compilation. */
5131 if (TYPE_IDENTIFIER (TREE_VALUE (tail)) == name)
5132 return TREE_VALUE (tail);
5133 }
2c73f9f5
ML
5134 else if (level->namespace_p)
5135 /* Do namespace lookup. */
6c011b01 5136 for (tail = current_namespace; 1; tail = CP_DECL_CONTEXT (tail))
2c73f9f5 5137 {
36a117a5
MM
5138 tree old = binding_for_name (name, tail);
5139
5140 /* If we just skipped past a pseudo global level, even
5141 though THISLEVEL_ONLY, and we find a template class
5142 declaration, then we use the _TYPE node for the
5143 template. See the example below. */
5144 if (thislevel_only && !allow_pseudo_global
5145 && old && BINDING_VALUE (old)
5146 && DECL_CLASS_TEMPLATE_P (BINDING_VALUE (old)))
5147 old = TREE_TYPE (BINDING_VALUE (old));
5148 else
5149 old = BINDING_TYPE (old);
5150
2c73f9f5
ML
5151 /* If it has an original type, it is a typedef, and we
5152 should not return it. */
5153 if (old && DECL_ORIGINAL_TYPE (TYPE_NAME (old)))
5154 old = NULL_TREE;
5155 if (old && TREE_CODE (old) != form
5156 && !(form != ENUMERAL_TYPE && TREE_CODE (old) == TEMPLATE_DECL))
5157 {
8251199e 5158 cp_error ("`%#D' redeclared as %C", old, form);
2c73f9f5
ML
5159 return NULL_TREE;
5160 }
5161 if (old)
5162 return old;
5163 if (thislevel_only || tail == global_namespace)
5164 return NULL_TREE;
5165 }
8d08fdba
MS
5166 else
5167 for (tail = level->tags; tail; tail = TREE_CHAIN (tail))
5168 {
a80e4195 5169 if (TREE_PURPOSE (tail) == name)
8d08fdba
MS
5170 {
5171 enum tree_code code = TREE_CODE (TREE_VALUE (tail));
5172 /* Should tighten this up; it'll probably permit
5173 UNION_TYPE and a struct template, for example. */
5174 if (code != form
5566b478 5175 && !(form != ENUMERAL_TYPE && code == TEMPLATE_DECL))
8d08fdba
MS
5176 {
5177 /* Definition isn't the kind we were looking for. */
8251199e 5178 cp_error ("`%#D' redeclared as %C", TREE_VALUE (tail),
8d08fdba 5179 form);
72b7eeff 5180 return NULL_TREE;
8d08fdba
MS
5181 }
5182 return TREE_VALUE (tail);
5183 }
5184 }
5185 if (thislevel_only && ! level->tag_transparent)
5566b478 5186 {
36a117a5 5187 if (level->pseudo_global && allow_pseudo_global)
5566b478 5188 {
36a117a5
MM
5189 /* We must deal with cases like this:
5190
5191 template <class T> struct S;
5192 template <class T> struct S {};
5193
5194 When looking up `S', for the second declaration, we
5195 would like to find the first declaration. But, we
5196 are in the pseudo-global level created for the
5197 template parameters, rather than the (surrounding)
5198 namespace level. Thus, we keep going one more level,
5199 even though THISLEVEL_ONLY is non-zero. */
5200 allow_pseudo_global = 0;
5201 continue;
5566b478 5202 }
36a117a5
MM
5203 else
5204 return NULL_TREE;
5566b478 5205 }
8d08fdba
MS
5206 }
5207 return NULL_TREE;
5208}
5209
bd6dd845 5210#if 0
8d08fdba
MS
5211void
5212set_current_level_tags_transparency (tags_transparent)
5213 int tags_transparent;
5214{
5215 current_binding_level->tag_transparent = tags_transparent;
5216}
bd6dd845 5217#endif
8d08fdba
MS
5218
5219/* Given a type, find the tag that was defined for it and return the tag name.
5220 Otherwise return 0. However, the value can never be 0
5221 in the cases in which this is used.
5222
5223 C++: If NAME is non-zero, this is the new name to install. This is
5224 done when replacing anonymous tags with real tag names. */
5225
5226static tree
5227lookup_tag_reverse (type, name)
5228 tree type;
5229 tree name;
5230{
5231 register struct binding_level *level;
5232
5233 for (level = current_binding_level; level; level = level->level_chain)
5234 {
5235 register tree tail;
5236 for (tail = level->tags; tail; tail = TREE_CHAIN (tail))
5237 {
5238 if (TREE_VALUE (tail) == type)
5239 {
5240 if (name)
5241 TREE_PURPOSE (tail) = name;
5242 return TREE_PURPOSE (tail);
5243 }
5244 }
5245 }
5246 return NULL_TREE;
5247}
8d08fdba 5248\f
a9aedbc2 5249/* Look up NAME in the NAMESPACE. */
e92cc029 5250
a9aedbc2
MS
5251tree
5252lookup_namespace_name (namespace, name)
5253 tree namespace, name;
5254{
2c73f9f5 5255 struct tree_binding _b;
30394414 5256 tree val;
f30c84c9 5257 tree template_id = NULL_TREE;
2c73f9f5 5258
30394414 5259 my_friendly_assert (TREE_CODE (namespace) == NAMESPACE_DECL, 370);
1231fb96 5260
1231fb96 5261 if (TREE_CODE (name) == NAMESPACE_DECL)
d8f8dca1 5262 /* This happens for A::B<int> when B is a namespace. */
1231fb96 5263 return name;
d8f8dca1
MM
5264 else if (TREE_CODE (name) == TEMPLATE_DECL)
5265 {
5266 /* This happens for A::B where B is a template, and there are no
5267 template arguments. */
5268 cp_error ("invalid use of `%D'", name);
5269 return error_mark_node;
5270 }
1231fb96 5271
b262d64c
JM
5272 namespace = ORIGINAL_NAMESPACE (namespace);
5273
f30c84c9
MM
5274 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
5275 {
5276 template_id = name;
5277 name = TREE_OPERAND (name, 0);
5278 if (TREE_CODE (name) == OVERLOAD)
5279 name = DECL_NAME (OVL_CURRENT (name));
5280 else if (TREE_CODE_CLASS (TREE_CODE (name)) == 'd')
5281 name = DECL_NAME (name);
5282 }
5283
30394414
JM
5284 my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, 373);
5285
2c73f9f5 5286 val = binding_init (&_b);
52c11ef6 5287 if (!qualified_lookup_using_namespace (name, namespace, val, 0))
2c73f9f5
ML
5288 return error_mark_node;
5289
5290 if (BINDING_VALUE (val))
1c35f5b6
JM
5291 {
5292 val = BINDING_VALUE (val);
5293
f30c84c9
MM
5294 if (template_id)
5295 {
5296 if (DECL_CLASS_TEMPLATE_P (val))
5297 val = lookup_template_class (val,
5298 TREE_OPERAND (template_id, 1),
5299 /*in_decl=*/NULL_TREE,
5300 /*context=*/NULL_TREE,
5301 /*entering_scope=*/0);
5302 else if (DECL_FUNCTION_TEMPLATE_P (val)
5303 || TREE_CODE (val) == OVERLOAD)
5304 val = lookup_template_function (val,
5305 TREE_OPERAND (template_id, 1));
5306 else
5307 {
5308 cp_error ("`%D::%D' is not a template",
5309 namespace, name);
5310 return error_mark_node;
5311 }
5312 }
5313
1c35f5b6
JM
5314 /* If we have a single function from a using decl, pull it out. */
5315 if (TREE_CODE (val) == OVERLOAD && ! really_overloaded_fn (val))
5316 val = OVL_FUNCTION (val);
5317 return val;
5318 }
5319
8251199e 5320 cp_error ("`%D' undeclared in namespace `%D'", name, namespace);
30394414 5321 return error_mark_node;
a9aedbc2
MS
5322}
5323
7ddedda4
MM
5324/* Hash a TYPENAME_TYPE. K is really of type `tree'. */
5325
5326static unsigned long
5327typename_hash (k)
5328 hash_table_key k;
5329{
5330 unsigned long hash;
5331 tree t;
5332
5333 t = (tree) k;
5334 hash = (((unsigned long) TYPE_CONTEXT (t))
5335 ^ ((unsigned long) DECL_NAME (TYPE_NAME (t))));
5336
5337 return hash;
5338}
5339
5340/* Compare two TYPENAME_TYPEs. K1 and K2 are really of type `tree'. */
5341
5342static boolean
5343typename_compare (k1, k2)
5344 hash_table_key k1;
5345 hash_table_key k2;
5346{
5347 tree t1;
5348 tree t2;
5349 tree d1;
5350 tree d2;
5351
5352 t1 = (tree) k1;
5353 t2 = (tree) k2;
5354 d1 = TYPE_NAME (t1);
5355 d2 = TYPE_NAME (t2);
5356
5357 return (DECL_NAME (d1) == DECL_NAME (d2)
5358 && same_type_p (TYPE_CONTEXT (t1), TYPE_CONTEXT (t2))
5359 && ((TREE_TYPE (t1) != NULL_TREE)
5360 == (TREE_TYPE (t2) != NULL_TREE))
5361 && same_type_p (TREE_TYPE (t1), TREE_TYPE (t2))
5362 && TYPENAME_TYPE_FULLNAME (t1) == TYPENAME_TYPE_FULLNAME (t2));
5363}
5364
45869a6c
MM
5365/* Build a TYPENAME_TYPE. If the type is `typename T::t', CONTEXT is
5366 the type of `T', NAME is the IDENTIFIER_NODE for `t'. If BASE_TYPE
5367 is non-NULL, this type is being created by the implicit typename
5368 extension, and BASE_TYPE is a type named `t' in some base class of
5369 `T' which depends on template parameters.
5370
5371 Returns the new TYPENAME_TYPE. */
5372
5373tree
5374build_typename_type (context, name, fullname, base_type)
5375 tree context;
5376 tree name;
5377 tree fullname;
5378 tree base_type;
5379{
5380 tree t;
5381 tree d;
7ddedda4 5382 struct hash_entry* e;
45869a6c 5383
7ddedda4
MM
5384 static struct hash_table ht;
5385
5386 push_obstacks (&permanent_obstack, &permanent_obstack);
5387
5388 if (!ht.table
5389 && !hash_table_init (&ht, &hash_newfunc, &typename_hash,
5390 &typename_compare))
5391 fatal ("virtual memory exhausted");
45869a6c 5392
78638e24
MM
5393 /* The FULLNAME needs to exist for the life of the hash table, i.e.,
5394 for the entire compilation. */
5395 if (!TREE_PERMANENT (fullname))
5396 fullname = copy_to_permanent (fullname);
5397
45869a6c
MM
5398 /* Build the TYPENAME_TYPE. */
5399 t = make_lang_type (TYPENAME_TYPE);
5400 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
5401 TYPENAME_TYPE_FULLNAME (t) = fullname;
5402 TREE_TYPE (t) = base_type;
45869a6c
MM
5403
5404 /* Build the corresponding TYPE_DECL. */
5405 d = build_decl (TYPE_DECL, name, t);
5406 TYPE_NAME (TREE_TYPE (d)) = d;
5407 TYPE_STUB_DECL (TREE_TYPE (d)) = d;
5408 DECL_CONTEXT (d) = FROB_CONTEXT (context);
2642b9bf 5409 DECL_ARTIFICIAL (d) = 1;
45869a6c 5410
7ddedda4
MM
5411 /* See if we already have this type. */
5412 e = hash_lookup (&ht, t, /*create=*/false, /*copy=*/0);
5413 if (e)
5414 {
5415 /* This will free not only TREE_TYPE, but the lang-specific data
5416 and the TYPE_DECL as well. */
5417 obstack_free (&permanent_obstack, t);
5418 t = (tree) e->key;
5419 }
5420 else
5421 /* Insert the type into the table. */
5422 hash_lookup (&ht, t, /*create=*/true, /*copy=*/0);
5423
5424 pop_obstacks ();
45869a6c
MM
5425
5426 return t;
5427}
5428
5566b478
MS
5429tree
5430make_typename_type (context, name)
5431 tree context, name;
5432{
b2b7d40a 5433 tree fullname;
a80e4195 5434
653cc74a 5435 if (TREE_CODE_CLASS (TREE_CODE (name)) == 't')
78638e24
MM
5436 {
5437 if (!(TYPE_LANG_SPECIFIC (name)
5438 && (CLASSTYPE_IS_TEMPLATE (name)
5439 || CLASSTYPE_USE_TEMPLATE (name))))
5440 name = TYPE_IDENTIFIER (name);
5441 else
5442 /* Create a TEMPLATE_ID_EXPR for the type. */
5443 name = build_nt (TEMPLATE_ID_EXPR,
5444 CLASSTYPE_TI_TEMPLATE (name),
5445 CLASSTYPE_TI_ARGS (name));
5446 }
653cc74a 5447 else if (TREE_CODE (name) == TYPE_DECL)
a80e4195 5448 name = DECL_NAME (name);
b2b7d40a
JM
5449
5450 fullname = name;
5451
5452 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
11686454
JM
5453 {
5454 name = TREE_OPERAND (name, 0);
5455 if (TREE_CODE (name) == TEMPLATE_DECL)
5456 name = TREE_OPERAND (fullname, 0) = DECL_NAME (name);
5457 }
b2b7d40a 5458 if (TREE_CODE (name) != IDENTIFIER_NODE)
a80e4195 5459 my_friendly_abort (2000);
5566b478 5460
04ddee1b
BK
5461 if (TREE_CODE (context) == NAMESPACE_DECL)
5462 {
5463 /* We can get here from typename_sub0 in the explicit_template_type
5464 expansion. Just fail. */
5465 cp_error ("no class template named `%#T' in `%#T'",
5466 name, context);
5467 return error_mark_node;
5468 }
5469
85b71cf2 5470 if (! uses_template_parms (context)
b77ead33 5471 || currently_open_class (context))
5566b478 5472 {
b2b7d40a
JM
5473 if (TREE_CODE (fullname) == TEMPLATE_ID_EXPR)
5474 {
ad810b22 5475 tree tmpl = NULL_TREE;
b2b7d40a 5476 if (IS_AGGR_TYPE (context))
ad810b22
MM
5477 tmpl = lookup_field (context, name, 0, 0);
5478 if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl))
b2b7d40a 5479 {
8251199e 5480 cp_error ("no class template named `%#T' in `%#T'",
b2b7d40a
JM
5481 name, context);
5482 return error_mark_node;
5483 }
ffb690bd 5484
ad810b22
MM
5485 return lookup_template_class (tmpl,
5486 TREE_OPERAND (fullname, 1),
5487 NULL_TREE, context,
5488 /*entering_scope=*/0);
b2b7d40a
JM
5489 }
5490 else
5566b478 5491 {
b4f70b3d
NS
5492 tree t;
5493
5494 if (!IS_AGGR_TYPE (context))
b2b7d40a 5495 {
8251199e 5496 cp_error ("no type named `%#T' in `%#T'", name, context);
b2b7d40a
JM
5497 return error_mark_node;
5498 }
1107c4b3 5499
b4f70b3d 5500 t = lookup_field (context, name, 0, 1);
7d4bdeed
MM
5501 if (t)
5502 return TREE_TYPE (t);
5566b478 5503 }
5566b478 5504 }
11249cf0
MM
5505
5506 /* If the CONTEXT is not a template type, then either the field is
5507 there now or its never going to be. */
b4f70b3d 5508 if (!uses_template_parms (context))
11249cf0
MM
5509 {
5510 cp_error ("no type named `%#T' in `%#T'", name, context);
5511 return error_mark_node;
5512 }
5513
45869a6c
MM
5514
5515 return build_typename_type (context, name, fullname, NULL_TREE);
5566b478
MS
5516}
5517
2c73f9f5
ML
5518/* Select the right _DECL from multiple choices. */
5519
5520static tree
52c11ef6 5521select_decl (binding, flags)
2c73f9f5 5522 tree binding;
52c11ef6 5523 int flags;
2c73f9f5
ML
5524{
5525 tree val;
5526 val = BINDING_VALUE (binding);
52c11ef6 5527 if (LOOKUP_NAMESPACES_ONLY (flags))
3e3f722c
ML
5528 {
5529 /* We are not interested in types. */
5530 if (val && TREE_CODE (val) == NAMESPACE_DECL)
5531 return val;
5532 return NULL_TREE;
5533 }
5534
2c73f9f5
ML
5535 /* If we could have a type and
5536 we have nothing or we need a type and have none. */
5537 if (BINDING_TYPE (binding)
52c11ef6
JM
5538 && (!val || ((flags & LOOKUP_PREFER_TYPES)
5539 && TREE_CODE (val) != TYPE_DECL)))
2c73f9f5
ML
5540 val = TYPE_STUB_DECL (BINDING_TYPE (binding));
5541 /* Don't return non-types if we really prefer types. */
52c11ef6 5542 else if (val && LOOKUP_TYPES_ONLY (flags) && TREE_CODE (val) != TYPE_DECL
c592d5d2
ML
5543 && (TREE_CODE (val) != TEMPLATE_DECL
5544 || !DECL_CLASS_TEMPLATE_P (val)))
2c73f9f5 5545 val = NULL_TREE;
1c35f5b6 5546
2c73f9f5
ML
5547 return val;
5548}
5549
ea9635c7
ML
5550/* Unscoped lookup of a global, iterate over namespaces, considering
5551 using namespace statements. */
5552
5553static tree
52c11ef6 5554unqualified_namespace_lookup (name, flags)
ea9635c7 5555 tree name;
52c11ef6 5556 int flags;
ea9635c7
ML
5557{
5558 struct tree_binding _binding;
5559 tree b = binding_init (&_binding);
5560 tree initial = current_decl_namespace();
5561 tree scope = initial;
5562 tree siter;
5563 struct binding_level *level;
5564 tree val = NULL_TREE;
5565
5566 while (!val)
5567 {
5568 val = binding_for_name (name, scope);
5569
5570 /* Initialize binding for this context. */
5571 BINDING_VALUE (b) = BINDING_VALUE (val);
5572 BINDING_TYPE (b) = BINDING_TYPE (val);
5573
5574 /* Add all _DECLs seen through local using-directives. */
5575 for (level = current_binding_level;
5576 !level->namespace_p;
5577 level = level->level_chain)
52c11ef6
JM
5578 if (!lookup_using_namespace (name, b, level->using_directives,
5579 scope, flags))
ea9635c7 5580 /* Give up because of error. */
5b163de4 5581 return error_mark_node;
ea9635c7
ML
5582
5583 /* Add all _DECLs seen through global using-directives. */
5584 /* XXX local and global using lists should work equally. */
5585 siter = initial;
5586 while (1)
5587 {
5588 if (!lookup_using_namespace (name, b, DECL_NAMESPACE_USING (siter),
52c11ef6 5589 scope, flags))
ea9635c7 5590 /* Give up because of error. */
5b163de4 5591 return error_mark_node;
ea9635c7
ML
5592 if (siter == scope) break;
5593 siter = CP_DECL_CONTEXT (siter);
5594 }
5595
52c11ef6 5596 val = select_decl (b, flags);
ea9635c7
ML
5597 if (scope == global_namespace)
5598 break;
0c64a9ca 5599 scope = CP_DECL_CONTEXT (scope);
ea9635c7
ML
5600 }
5601 return val;
5602}
5603
52c11ef6
JM
5604/* Combine prefer_type and namespaces_only into flags. */
5605
5606static int
5607lookup_flags (prefer_type, namespaces_only)
5608 int prefer_type, namespaces_only;
5609{
5610 if (namespaces_only)
5611 return LOOKUP_PREFER_NAMESPACES;
5612 if (prefer_type > 1)
5613 return LOOKUP_PREFER_TYPES;
5614 if (prefer_type > 0)
5615 return LOOKUP_PREFER_BOTH;
5616 return 0;
5617}
5618
5619/* Given a lookup that returned VAL, use FLAGS to decide if we want to
5620 ignore it or not. Subroutine of lookup_name_real. */
5621
5622static tree
5623qualify_lookup (val, flags)
5624 tree val;
5625 int flags;
5626{
5627 if (val == NULL_TREE)
5628 return val;
6b945830
JM
5629 if ((flags & LOOKUP_PREFER_NAMESPACES) && TREE_CODE (val) == NAMESPACE_DECL)
5630 return val;
5631 if ((flags & LOOKUP_PREFER_TYPES)
5632 && (TREE_CODE (val) == TYPE_DECL
5633 || ((flags & LOOKUP_TEMPLATES_EXPECTED)
5634 && DECL_CLASS_TEMPLATE_P (val))))
5635 return val;
5636 if (flags & (LOOKUP_PREFER_NAMESPACES | LOOKUP_PREFER_TYPES))
52c11ef6
JM
5637 return NULL_TREE;
5638 return val;
5639}
5640
235f734d
MM
5641/* Any other BINDING overrides an implicit TYPENAME. Warn about
5642 that. */
5643
5644static void
5645warn_about_implicit_typename_lookup (typename, binding)
5646 tree typename;
5647 tree binding;
5648{
5649 tree subtype = TREE_TYPE (TREE_TYPE (typename));
5650 tree name = DECL_NAME (typename);
5651
5652 if (! (TREE_CODE (binding) == TEMPLATE_DECL
5653 && CLASSTYPE_TEMPLATE_INFO (subtype)
5654 && CLASSTYPE_TI_TEMPLATE (subtype) == binding)
5655 && ! (TREE_CODE (binding) == TYPE_DECL
5656 && same_type_p (TREE_TYPE (binding), subtype)))
5657 {
5658 cp_warning ("lookup of `%D' finds `%#D'",
5659 name, binding);
5660 cp_warning (" instead of `%D' from dependent base class",
5661 typename);
5662 cp_warning (" (use `typename %T::%D' if that's what you meant)",
5663 constructor_name (current_class_type), name);
5664 }
5665}
5666
8d08fdba
MS
5667/* Look up NAME in the current binding level and its superiors in the
5668 namespace of variables, functions and typedefs. Return a ..._DECL
5669 node of some kind representing its definition if there is only one
5670 such declaration, or return a TREE_LIST with all the overloaded
5671 definitions if there are many, or return 0 if it is undefined.
5672
2c73f9f5
ML
5673 If PREFER_TYPE is > 0, we prefer TYPE_DECLs or namespaces.
5674 If PREFER_TYPE is > 1, we reject non-type decls (e.g. namespaces).
a28e3c7f 5675 If PREFER_TYPE is -2, we're being called from yylex(). (UGLY)
f84b4be9
JM
5676 Otherwise we prefer non-TYPE_DECLs.
5677
5678 If NONCLASS is non-zero, we don't look for the NAME in class scope,
5679 using IDENTIFIER_CLASS_VALUE. */
8d08fdba 5680
824b9a4c 5681static tree
3e3f722c 5682lookup_name_real (name, prefer_type, nonclass, namespaces_only)
8d08fdba 5683 tree name;
3e3f722c 5684 int prefer_type, nonclass, namespaces_only;
8d08fdba 5685{
235f734d
MM
5686 tree t;
5687 tree val = NULL_TREE;
a28e3c7f 5688 int yylex = 0;
e1cd6e56 5689 tree from_obj = NULL_TREE;
52c11ef6 5690 int flags;
235f734d 5691 int val_is_implicit_typename = 0;
8d08fdba 5692
3e3f722c
ML
5693 /* Hack: copy flag set by parser, if set. */
5694 if (only_namespace_names)
5695 namespaces_only = 1;
52c11ef6 5696
a28e3c7f
MS
5697 if (prefer_type == -2)
5698 {
5699 extern int looking_for_typename;
fc378698 5700 tree type = NULL_TREE;
a28e3c7f
MS
5701
5702 yylex = 1;
5703 prefer_type = looking_for_typename;
e1cd6e56 5704
52c11ef6 5705 flags = lookup_flags (prefer_type, namespaces_only);
52c11ef6
JM
5706 /* If the next thing is '<', class templates are types. */
5707 if (looking_for_template)
5708 flags |= LOOKUP_TEMPLATES_EXPECTED;
5709
653cc74a
JM
5710 /* std:: becomes :: for now. */
5711 if (got_scope == std_node)
5712 got_scope = void_type_node;
5713
e1cd6e56
MS
5714 if (got_scope)
5715 type = got_scope;
dff6b454 5716 else if (got_object != error_mark_node)
e1cd6e56 5717 type = got_object;
a28e3c7f 5718
e1cd6e56 5719 if (type)
a28e3c7f 5720 {
e1cd6e56 5721 if (type == error_mark_node)
f376e137 5722 return error_mark_node;
a80e4195
MS
5723 if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type))
5724 type = TREE_TYPE (type);
5566b478 5725
2b9dc906
JM
5726 if (TYPE_P (type))
5727 type = complete_type (type);
5566b478 5728
a1774733 5729 if (TREE_CODE (type) == VOID_TYPE)
2c73f9f5
ML
5730 type = global_namespace;
5731 if (TREE_CODE (type) == NAMESPACE_DECL)
a9aedbc2 5732 {
2c73f9f5
ML
5733 struct tree_binding b;
5734 val = binding_init (&b);
6ad07332 5735 flags |= LOOKUP_COMPLAIN;
52c11ef6 5736 if (!qualified_lookup_using_namespace (name, type, val, flags))
2c73f9f5 5737 return NULL_TREE;
52c11ef6 5738 val = select_decl (val, flags);
a9aedbc2 5739 }
5566b478 5740 else if (! IS_AGGR_TYPE (type)
5156628f 5741 || TREE_CODE (type) == TEMPLATE_TYPE_PARM
73b0fce8 5742 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM
5156628f 5743 || TREE_CODE (type) == TYPENAME_TYPE)
e92cc029 5744 /* Someone else will give an error about this if needed. */
a28e3c7f 5745 val = NULL_TREE;
e1cd6e56 5746 else if (type == current_class_type)
a28e3c7f 5747 val = IDENTIFIER_CLASS_VALUE (name);
a28e3c7f 5748 else
d23a1bb1 5749 val = lookup_member (type, name, 0, prefer_type);
a28e3c7f 5750 }
e1cd6e56
MS
5751 else
5752 val = NULL_TREE;
5753
594740f3 5754 if (got_scope)
e1cd6e56 5755 goto done;
594740f3 5756 else if (got_object && val)
e1cd6e56 5757 from_obj = val;
a28e3c7f 5758 }
52c11ef6 5759 else
5b163de4
JM
5760 {
5761 flags = lookup_flags (prefer_type, namespaces_only);
5762 /* If we're not parsing, we need to complain. */
5763 flags |= LOOKUP_COMPLAIN;
5764 }
e76a2646 5765
d8f8dca1 5766 /* First, look in non-namespace scopes. */
6f1b4c42
JM
5767
5768 if (current_class_type == NULL_TREE)
5769 nonclass = 1;
5770
235f734d 5771 for (t = IDENTIFIER_BINDING (name); t; t = TREE_CHAIN (t))
d8f8dca1 5772 {
235f734d
MM
5773 tree binding;
5774
5775 if (!LOCAL_BINDING_P (t) && nonclass)
d8f8dca1
MM
5776 /* We're not looking for class-scoped bindings, so keep going. */
5777 continue;
5778
5779 /* If this is the kind of thing we're looking for, we're done. */
235f734d
MM
5780 if (qualify_lookup (BINDING_VALUE (t), flags))
5781 binding = BINDING_VALUE (t);
d8f8dca1 5782 else if ((flags & LOOKUP_PREFER_TYPES)
235f734d
MM
5783 && qualify_lookup (BINDING_TYPE (t), flags))
5784 binding = BINDING_TYPE (t);
5785 else
5786 binding = NULL_TREE;
5787
5788 if (binding
83233dca 5789 && (!val || !IMPLICIT_TYPENAME_TYPE_DECL_P (binding)))
d8f8dca1 5790 {
235f734d
MM
5791 if (val_is_implicit_typename && !yylex)
5792 warn_about_implicit_typename_lookup (val, binding);
5793 val = binding;
5794 val_is_implicit_typename
83233dca 5795 = IMPLICIT_TYPENAME_TYPE_DECL_P (val);
235f734d
MM
5796 if (!val_is_implicit_typename)
5797 break;
d8f8dca1
MM
5798 }
5799 }
f181d4ae 5800
235f734d
MM
5801 /* Now lookup in namespace scopes. */
5802 if (!val || val_is_implicit_typename)
e76a2646 5803 {
235f734d
MM
5804 t = unqualified_namespace_lookup (name, flags);
5805 if (t)
c1def683 5806 {
235f734d
MM
5807 if (val_is_implicit_typename && !yylex)
5808 warn_about_implicit_typename_lookup (val, t);
5809 val = t;
c1def683
JM
5810 }
5811 }
5812
a28e3c7f 5813 done:
8d08fdba
MS
5814 if (val)
5815 {
c91a56d2 5816 /* This should only warn about types used in qualified-ids. */
e1cd6e56 5817 if (from_obj && from_obj != val)
5566b478 5818 {
c91a56d2
MS
5819 if (looking_for_typename && TREE_CODE (from_obj) == TYPE_DECL
5820 && TREE_CODE (val) == TYPE_DECL
5821 && TREE_TYPE (from_obj) != TREE_TYPE (val))
5822 {
8251199e 5823 cp_pedwarn ("lookup of `%D' in the scope of `%#T' (`%#T')",
c91a56d2 5824 name, got_object, TREE_TYPE (from_obj));
8251199e 5825 cp_pedwarn (" does not match lookup in the current scope (`%#T')",
c91a56d2
MS
5826 TREE_TYPE (val));
5827 }
594740f3 5828
b8b1a3c1
JM
5829 /* We don't change val to from_obj if got_object depends on
5830 template parms because that breaks implicit typename for
5831 destructor calls. */
5832 if (! uses_template_parms (got_object))
53fdbb3b 5833 val = from_obj;
5566b478 5834 }
e1cd6e56 5835
0c64a9ca
JM
5836 /* If we have a single function from a using decl, pull it out. */
5837 if (TREE_CODE (val) == OVERLOAD && ! really_overloaded_fn (val))
5838 val = OVL_FUNCTION (val);
8d08fdba 5839 }
e1cd6e56
MS
5840 else if (from_obj)
5841 val = from_obj;
8d08fdba
MS
5842
5843 return val;
5844}
5845
700f8a87
MS
5846tree
5847lookup_name_nonclass (name)
5848 tree name;
5849{
3e3f722c 5850 return lookup_name_real (name, 0, 1, 0);
700f8a87
MS
5851}
5852
2c73f9f5
ML
5853tree
5854lookup_function_nonclass (name, args)
5855 tree name;
5856 tree args;
5857{
5858 return lookup_arg_dependent (name, lookup_name_nonclass (name), args);
5859}
5860
3e3f722c
ML
5861tree
5862lookup_name_namespace_only (name)
5863 tree name;
5864{
5865 /* type-or-namespace, nonclass, namespace_only */
5866 return lookup_name_real (name, 1, 1, 1);
5867}
5868
700f8a87
MS
5869tree
5870lookup_name (name, prefer_type)
5871 tree name;
5872 int prefer_type;
5873{
3e3f722c 5874 return lookup_name_real (name, prefer_type, 0, 0);
700f8a87
MS
5875}
5876
a7d2d407
MM
5877/* Similar to `lookup_name' but look only in the innermost non-class
5878 binding level. */
8d08fdba
MS
5879
5880tree
5881lookup_name_current_level (name)
5882 tree name;
5883{
a7d2d407
MM
5884 struct binding_level *b;
5885 tree t = NULL_TREE;
8d08fdba 5886
a7d2d407
MM
5887 b = current_binding_level;
5888 while (b->parm_flag == 2)
5889 b = b->level_chain;
5890
5891 if (b->namespace_p)
8d08fdba 5892 {
30394414 5893 t = IDENTIFIER_NAMESPACE_VALUE (name);
8d08fdba
MS
5894
5895 /* extern "C" function() */
5896 if (t != NULL_TREE && TREE_CODE (t) == TREE_LIST)
5897 t = TREE_VALUE (t);
5898 }
f181d4ae
MM
5899 else if (IDENTIFIER_BINDING (name)
5900 && LOCAL_BINDING_P (IDENTIFIER_BINDING (name)))
8d08fdba 5901 {
a4443a08
MS
5902 while (1)
5903 {
f181d4ae
MM
5904 if (BINDING_LEVEL (IDENTIFIER_BINDING (name)) == b)
5905 return IDENTIFIER_VALUE (name);
5906
9ed182dc
JM
5907 if (b->keep == 2)
5908 b = b->level_chain;
5909 else
5910 break;
5911 }
5912 }
5913
5914 return t;
5915}
5916
5917/* Like lookup_name_current_level, but for types. */
5918
5919tree
5920lookup_type_current_level (name)
5921 tree name;
5922{
5923 register tree t = NULL_TREE;
5924
5925 my_friendly_assert (! current_binding_level->namespace_p, 980716);
5926
5927 if (REAL_IDENTIFIER_TYPE_VALUE (name) != NULL_TREE
5928 && REAL_IDENTIFIER_TYPE_VALUE (name) != global_type_node)
5929 {
5930 struct binding_level *b = current_binding_level;
5931 while (1)
5932 {
5933 if (purpose_member (name, b->type_shadowed))
5934 return REAL_IDENTIFIER_TYPE_VALUE (name);
a4443a08
MS
5935 if (b->keep == 2)
5936 b = b->level_chain;
5937 else
5938 break;
5939 }
8d08fdba
MS
5940 }
5941
5942 return t;
5943}
3e3f722c
ML
5944
5945void
5946begin_only_namespace_names ()
5947{
5948 only_namespace_names = 1;
5949}
5950
5951void
5952end_only_namespace_names ()
5953{
5954 only_namespace_names = 0;
5955}
8d08fdba
MS
5956\f
5957/* Arrange for the user to get a source line number, even when the
5958 compiler is going down in flames, so that she at least has a
5959 chance of working around problems in the compiler. We used to
5960 call error(), but that let the segmentation fault continue
5961 through; now, it's much more passive by asking them to send the
5962 maintainers mail about the problem. */
5963
5964static void
5965signal_catch (sig)
7dee3f36 5966 int sig ATTRIBUTE_UNUSED;
8d08fdba
MS
5967{
5968 signal (SIGSEGV, SIG_DFL);
5969#ifdef SIGIOT
5970 signal (SIGIOT, SIG_DFL);
5971#endif
5972#ifdef SIGILL
5973 signal (SIGILL, SIG_DFL);
5974#endif
5975#ifdef SIGABRT
5976 signal (SIGABRT, SIG_DFL);
5977#endif
5978#ifdef SIGBUS
5979 signal (SIGBUS, SIG_DFL);
5980#endif
5981 my_friendly_abort (0);
5982}
5983
de22184b
MS
5984#if 0
5985/* Unused -- brendan 970107 */
8d08fdba
MS
5986/* Array for holding types considered "built-in". These types
5987 are output in the module in which `main' is defined. */
5988static tree *builtin_type_tdescs_arr;
5989static int builtin_type_tdescs_len, builtin_type_tdescs_max;
de22184b 5990#endif
8d08fdba
MS
5991
5992/* Push the declarations of builtin types into the namespace.
5993 RID_INDEX, if < RID_MAX is the index of the builtin type
5994 in the array RID_POINTERS. NAME is the name used when looking
5995 up the builtin type. TYPE is the _TYPE node for the builtin type. */
5996
5997static void
5998record_builtin_type (rid_index, name, type)
5999 enum rid rid_index;
d8e178a0 6000 const char *name;
8d08fdba
MS
6001 tree type;
6002{
6003 tree rname = NULL_TREE, tname = NULL_TREE;
a703fb38 6004 tree tdecl = NULL_TREE;
8d08fdba
MS
6005
6006 if ((int) rid_index < (int) RID_MAX)
6007 rname = ridpointers[(int) rid_index];
6008 if (name)
6009 tname = get_identifier (name);
6010
6011 TYPE_BUILT_IN (type) = 1;
6012
6013 if (tname)
6014 {
8d08fdba 6015 tdecl = pushdecl (build_decl (TYPE_DECL, tname, type));
8d08fdba
MS
6016 set_identifier_type_value (tname, NULL_TREE);
6017 if ((int) rid_index < (int) RID_MAX)
2c73f9f5
ML
6018 /* Built-in types live in the global namespace. */
6019 SET_IDENTIFIER_GLOBAL_VALUE (tname, tdecl);
8d08fdba
MS
6020 }
6021 if (rname != NULL_TREE)
6022 {
6023 if (tname != NULL_TREE)
6024 {
6025 set_identifier_type_value (rname, NULL_TREE);
2c73f9f5 6026 SET_IDENTIFIER_GLOBAL_VALUE (rname, tdecl);
8d08fdba
MS
6027 }
6028 else
6029 {
8d08fdba 6030 tdecl = pushdecl (build_decl (TYPE_DECL, rname, type));
8d08fdba
MS
6031 set_identifier_type_value (rname, NULL_TREE);
6032 }
6033 }
8d08fdba
MS
6034}
6035
eff71ab0
PB
6036/* Record one of the standard Java types.
6037 * Declare it as having the given NAME.
6038 * If SIZE > 0, it is the size of one of the integral types;
6039 * otherwise it is the negative of the size of one of the other types. */
6040
6041static tree
6042record_builtin_java_type (name, size)
d8e178a0 6043 const char *name;
eff71ab0
PB
6044 int size;
6045{
6046 tree type, decl;
6047 if (size > 0)
6048 type = make_signed_type (size);
6049 else if (size > -32)
6050 { /* "__java_char" or ""__java_boolean". */
6051 type = make_unsigned_type (-size);
6052 /*if (size == -1) TREE_SET_CODE (type, BOOLEAN_TYPE);*/
6053 }
6054 else
6055 { /* "__java_float" or ""__java_double". */
6056 type = make_node (REAL_TYPE);
6057 TYPE_PRECISION (type) = - size;
6058 layout_type (type);
6059 }
6060 record_builtin_type (RID_MAX, name, type);
6061 decl = TYPE_NAME (type);
e229f2cd
PB
6062
6063 /* Suppress generate debug symbol entries for these types,
6064 since for normal C++ they are just clutter.
6065 However, push_lang_context undoes this if extern "Java" is seen. */
eff71ab0 6066 DECL_IGNORED_P (decl) = 1;
e229f2cd 6067
eff71ab0
PB
6068 TYPE_FOR_JAVA (type) = 1;
6069 return type;
6070}
6071
036407f7
ML
6072/* Push a type into the namespace so that the back-ends ignore it. */
6073
6074static void
6075record_unknown_type (type, name)
6076 tree type;
d8e178a0 6077 const char *name;
036407f7
ML
6078{
6079 tree decl = pushdecl (build_decl (TYPE_DECL, get_identifier (name), type));
6080 /* Make sure the "unknown type" typedecl gets ignored for debug info. */
6081 DECL_IGNORED_P (decl) = 1;
6082 TYPE_DECL_SUPPRESS_DEBUG (decl) = 1;
6083 TYPE_SIZE (type) = TYPE_SIZE (void_type_node);
6084 TYPE_ALIGN (type) = 1;
6085 TYPE_MODE (type) = TYPE_MODE (void_type_node);
6086}
6087
8d08fdba
MS
6088/* Push overloaded decl, in global scope, with one argument so it
6089 can be used as a callback from define_function. */
e92cc029 6090
8d08fdba
MS
6091static void
6092push_overloaded_decl_1 (x)
6093 tree x;
6094{
7bdbfa05 6095 push_overloaded_decl (x, PUSH_GLOBAL);
8d08fdba
MS
6096}
6097
8ccc31eb
MS
6098#ifdef __GNUC__
6099__inline
6100#endif
6b5fbb55
MS
6101tree
6102auto_function (name, type, code)
8ccc31eb
MS
6103 tree name, type;
6104 enum built_in_function code;
6105{
6106 return define_function
49c249e1 6107 (IDENTIFIER_POINTER (name), type, code, push_overloaded_decl_1,
8ccc31eb
MS
6108 IDENTIFIER_POINTER (build_decl_overload (name, TYPE_ARG_TYPES (type),
6109 0)));
6110}
6111
8d08fdba
MS
6112/* Create the predefined scalar types of C,
6113 and some nodes representing standard constants (0, 1, (void *)0).
6114 Initialize the global binding level.
6115 Make definitions for built-in primitive functions. */
6116
6117void
6118init_decl_processing ()
6119{
8d08fdba 6120 tree fields[20];
8d08fdba 6121 int wchar_type_size;
8d08fdba 6122 tree array_domain_type;
a703fb38 6123 tree vb_off_identifier = NULL_TREE;
8d08fdba
MS
6124
6125 /* Have to make these distinct before we try using them. */
6126 lang_name_cplusplus = get_identifier ("C++");
6127 lang_name_c = get_identifier ("C");
a1774733 6128 lang_name_java = get_identifier ("Java");
8d08fdba 6129
2c73f9f5 6130 /* Enter the global namespace. */
30394414
JM
6131 my_friendly_assert (global_namespace == NULL_TREE, 375);
6132 my_friendly_assert (current_lang_name == NULL_TREE, 375);
6133 current_lang_name = lang_name_cplusplus;
6134 push_namespace (get_identifier ("::"));
6135 global_namespace = current_namespace;
6136 current_lang_name = NULL_TREE;
6137
e1cd6e56 6138 if (flag_strict_prototype == 2)
830fcda8 6139 flag_strict_prototype = pedantic;
2642b9bf
JM
6140 if (! flag_permissive && ! pedantic)
6141 flag_pedantic_errors = 1;
830fcda8
JM
6142
6143 strict_prototypes_lang_c = flag_strict_prototype;
8926095f 6144
8d08fdba
MS
6145 /* Initially, C. */
6146 current_lang_name = lang_name_c;
6147
6148 current_function_decl = NULL_TREE;
6149 named_labels = NULL_TREE;
e349ee73 6150 named_label_uses = NULL;
8d08fdba
MS
6151 current_binding_level = NULL_BINDING_LEVEL;
6152 free_binding_level = NULL_BINDING_LEVEL;
6153
6154 /* Because most segmentation signals can be traced back into user
6155 code, catch them and at least give the user a chance of working
e92cc029 6156 around compiler bugs. */
8d08fdba
MS
6157 signal (SIGSEGV, signal_catch);
6158
6159 /* We will also catch aborts in the back-end through signal_catch and
6160 give the user a chance to see where the error might be, and to defeat
6161 aborts in the back-end when there have been errors previously in their
e92cc029 6162 code. */
8d08fdba
MS
6163#ifdef SIGIOT
6164 signal (SIGIOT, signal_catch);
6165#endif
6166#ifdef SIGILL
6167 signal (SIGILL, signal_catch);
6168#endif
6169#ifdef SIGABRT
6170 signal (SIGABRT, signal_catch);
6171#endif
6172#ifdef SIGBUS
6173 signal (SIGBUS, signal_catch);
6174#endif
6175
6176 gcc_obstack_init (&decl_obstack);
8d08fdba
MS
6177
6178 /* Must lay these out before anything else gets laid out. */
6179 error_mark_node = make_node (ERROR_MARK);
6180 TREE_PERMANENT (error_mark_node) = 1;
6181 TREE_TYPE (error_mark_node) = error_mark_node;
6182 error_mark_list = build_tree_list (error_mark_node, error_mark_node);
6183 TREE_TYPE (error_mark_list) = error_mark_node;
6184
a28e3c7f
MS
6185 /* Make the binding_level structure for global names. */
6186 pushlevel (0);
8d08fdba 6187 global_binding_level = current_binding_level;
2c73f9f5
ML
6188 /* The global level is the namespace level of ::. */
6189 NAMESPACE_LEVEL (global_namespace) = global_binding_level;
6190 declare_namespace_level ();
8d08fdba
MS
6191
6192 this_identifier = get_identifier (THIS_NAME);
6193 in_charge_identifier = get_identifier (IN_CHARGE_NAME);
fc378698
MS
6194 ctor_identifier = get_identifier (CTOR_NAME);
6195 dtor_identifier = get_identifier (DTOR_NAME);
8d08fdba
MS
6196 pfn_identifier = get_identifier (VTABLE_PFN_NAME);
6197 index_identifier = get_identifier (VTABLE_INDEX_NAME);
6198 delta_identifier = get_identifier (VTABLE_DELTA_NAME);
6199 delta2_identifier = get_identifier (VTABLE_DELTA2_NAME);
6200 pfn_or_delta2_identifier = get_identifier ("__pfn_or_delta2");
dff6b454
RK
6201 if (flag_handle_signatures)
6202 {
6203 tag_identifier = get_identifier (SIGTABLE_TAG_NAME);
9dd70aa4
GB
6204 vb_off_identifier = get_identifier (SIGTABLE_VB_OFF_NAME);
6205 vt_off_identifier = get_identifier (SIGTABLE_VT_OFF_NAME);
dff6b454 6206 }
8d08fdba
MS
6207
6208 /* Define `int' and `char' first so that dbx will output them first. */
6209
6210 integer_type_node = make_signed_type (INT_TYPE_SIZE);
6211 record_builtin_type (RID_INT, NULL_PTR, integer_type_node);
6212
6213 /* Define `char', which is like either `signed char' or `unsigned char'
6214 but not the same as either. */
6215
beb53fb8
JM
6216 char_type_node
6217 = (flag_signed_char
6218 ? make_signed_type (CHAR_TYPE_SIZE)
6219 : make_unsigned_type (CHAR_TYPE_SIZE));
8d08fdba
MS
6220 record_builtin_type (RID_CHAR, "char", char_type_node);
6221
45075bf3
NS
6222 /* `signed' is the same as `int' */
6223 record_builtin_type (RID_SIGNED, NULL_PTR, integer_type_node);
6224
8d08fdba
MS
6225 long_integer_type_node = make_signed_type (LONG_TYPE_SIZE);
6226 record_builtin_type (RID_LONG, "long int", long_integer_type_node);
6227
6228 unsigned_type_node = make_unsigned_type (INT_TYPE_SIZE);
6229 record_builtin_type (RID_UNSIGNED, "unsigned int", unsigned_type_node);
6230
6231 long_unsigned_type_node = make_unsigned_type (LONG_TYPE_SIZE);
6232 record_builtin_type (RID_MAX, "long unsigned int", long_unsigned_type_node);
6233 record_builtin_type (RID_MAX, "unsigned long", long_unsigned_type_node);
6234
6235 long_long_integer_type_node = make_signed_type (LONG_LONG_TYPE_SIZE);
6236 record_builtin_type (RID_MAX, "long long int", long_long_integer_type_node);
6237
6238 long_long_unsigned_type_node = make_unsigned_type (LONG_LONG_TYPE_SIZE);
6239 record_builtin_type (RID_MAX, "long long unsigned int",
6240 long_long_unsigned_type_node);
6241 record_builtin_type (RID_MAX, "long long unsigned",
6242 long_long_unsigned_type_node);
6243
5156628f
MS
6244 short_integer_type_node = make_signed_type (SHORT_TYPE_SIZE);
6245 record_builtin_type (RID_SHORT, "short int", short_integer_type_node);
6246 short_unsigned_type_node = make_unsigned_type (SHORT_TYPE_SIZE);
6247 record_builtin_type (RID_MAX, "short unsigned int", short_unsigned_type_node);
6248 record_builtin_type (RID_MAX, "unsigned short", short_unsigned_type_node);
6249
8d08fdba 6250 /* `unsigned long' is the standard type for sizeof.
8d08fdba 6251 Note that stddef.h uses `unsigned long',
f5426d1e
R
6252 and this must agree, even if long and int are the same size. */
6253 set_sizetype
6254 (TREE_TYPE (IDENTIFIER_GLOBAL_VALUE (get_identifier (SIZE_TYPE))));
8d08fdba
MS
6255
6256 ptrdiff_type_node
6257 = TREE_TYPE (IDENTIFIER_GLOBAL_VALUE (get_identifier (PTRDIFF_TYPE)));
6258
8d08fdba
MS
6259 /* Define both `signed char' and `unsigned char'. */
6260 signed_char_type_node = make_signed_type (CHAR_TYPE_SIZE);
6261 record_builtin_type (RID_MAX, "signed char", signed_char_type_node);
6262 unsigned_char_type_node = make_unsigned_type (CHAR_TYPE_SIZE);
6263 record_builtin_type (RID_MAX, "unsigned char", unsigned_char_type_node);
6264
835f9b4d
GRK
6265 /* Create the widest literal types. */
6266 widest_integer_literal_type_node = make_signed_type (HOST_BITS_PER_WIDE_INT * 2);
6267 pushdecl (build_decl (TYPE_DECL, NULL_TREE,
6268 widest_integer_literal_type_node));
6269
6270 widest_unsigned_literal_type_node = make_unsigned_type (HOST_BITS_PER_WIDE_INT * 2);
6271 pushdecl (build_decl (TYPE_DECL, NULL_TREE,
6272 widest_unsigned_literal_type_node));
6273
8d08fdba
MS
6274 /* These are types that type_for_size and type_for_mode use. */
6275 intQI_type_node = make_signed_type (GET_MODE_BITSIZE (QImode));
6276 pushdecl (build_decl (TYPE_DECL, NULL_TREE, intQI_type_node));
6277 intHI_type_node = make_signed_type (GET_MODE_BITSIZE (HImode));
6278 pushdecl (build_decl (TYPE_DECL, NULL_TREE, intHI_type_node));
6279 intSI_type_node = make_signed_type (GET_MODE_BITSIZE (SImode));
6280 pushdecl (build_decl (TYPE_DECL, NULL_TREE, intSI_type_node));
6281 intDI_type_node = make_signed_type (GET_MODE_BITSIZE (DImode));
6282 pushdecl (build_decl (TYPE_DECL, NULL_TREE, intDI_type_node));
946dc1c8 6283#if HOST_BITS_PER_WIDE_INT >= 64
f7554e8c 6284 intTI_type_node = make_signed_type (GET_MODE_BITSIZE (TImode));
5ebcdddb 6285 pushdecl (build_decl (TYPE_DECL, get_identifier ("__int128_t"), intTI_type_node));
946dc1c8 6286#endif
8d08fdba
MS
6287 unsigned_intQI_type_node = make_unsigned_type (GET_MODE_BITSIZE (QImode));
6288 pushdecl (build_decl (TYPE_DECL, NULL_TREE, unsigned_intQI_type_node));
6289 unsigned_intHI_type_node = make_unsigned_type (GET_MODE_BITSIZE (HImode));
6290 pushdecl (build_decl (TYPE_DECL, NULL_TREE, unsigned_intHI_type_node));
6291 unsigned_intSI_type_node = make_unsigned_type (GET_MODE_BITSIZE (SImode));
6292 pushdecl (build_decl (TYPE_DECL, NULL_TREE, unsigned_intSI_type_node));
6293 unsigned_intDI_type_node = make_unsigned_type (GET_MODE_BITSIZE (DImode));
6294 pushdecl (build_decl (TYPE_DECL, NULL_TREE, unsigned_intDI_type_node));
946dc1c8 6295#if HOST_BITS_PER_WIDE_INT >= 64
f7554e8c 6296 unsigned_intTI_type_node = make_unsigned_type (GET_MODE_BITSIZE (TImode));
5ebcdddb 6297 pushdecl (build_decl (TYPE_DECL, get_identifier ("__uint128_t"), unsigned_intTI_type_node));
946dc1c8 6298#endif
8d08fdba
MS
6299
6300 float_type_node = make_node (REAL_TYPE);
6301 TYPE_PRECISION (float_type_node) = FLOAT_TYPE_SIZE;
6302 record_builtin_type (RID_FLOAT, NULL_PTR, float_type_node);
6303 layout_type (float_type_node);
6304
6305 double_type_node = make_node (REAL_TYPE);
6306 if (flag_short_double)
6307 TYPE_PRECISION (double_type_node) = FLOAT_TYPE_SIZE;
6308 else
6309 TYPE_PRECISION (double_type_node) = DOUBLE_TYPE_SIZE;
6310 record_builtin_type (RID_DOUBLE, NULL_PTR, double_type_node);
6311 layout_type (double_type_node);
6312
6313 long_double_type_node = make_node (REAL_TYPE);
6314 TYPE_PRECISION (long_double_type_node) = LONG_DOUBLE_TYPE_SIZE;
6315 record_builtin_type (RID_MAX, "long double", long_double_type_node);
6316 layout_type (long_double_type_node);
6317
37c46b43
MS
6318 complex_integer_type_node = make_node (COMPLEX_TYPE);
6319 pushdecl (build_decl (TYPE_DECL, get_identifier ("complex int"),
6320 complex_integer_type_node));
6321 TREE_TYPE (complex_integer_type_node) = integer_type_node;
6322 layout_type (complex_integer_type_node);
6323
6324 complex_float_type_node = make_node (COMPLEX_TYPE);
6325 pushdecl (build_decl (TYPE_DECL, get_identifier ("complex float"),
6326 complex_float_type_node));
6327 TREE_TYPE (complex_float_type_node) = float_type_node;
6328 layout_type (complex_float_type_node);
6329
6330 complex_double_type_node = make_node (COMPLEX_TYPE);
6331 pushdecl (build_decl (TYPE_DECL, get_identifier ("complex double"),
6332 complex_double_type_node));
6333 TREE_TYPE (complex_double_type_node) = double_type_node;
6334 layout_type (complex_double_type_node);
6335
6336 complex_long_double_type_node = make_node (COMPLEX_TYPE);
6337 pushdecl (build_decl (TYPE_DECL, get_identifier ("complex long double"),
6338 complex_long_double_type_node));
6339 TREE_TYPE (complex_long_double_type_node) = long_double_type_node;
6340 layout_type (complex_long_double_type_node);
6341
eff71ab0
PB
6342 java_byte_type_node = record_builtin_java_type ("__java_byte", 8);
6343 java_short_type_node = record_builtin_java_type ("__java_short", 16);
6344 java_int_type_node = record_builtin_java_type ("__java_int", 32);
6345 java_long_type_node = record_builtin_java_type ("__java_long", 64);
6346 java_float_type_node = record_builtin_java_type ("__java_float", -32);
6347 java_double_type_node = record_builtin_java_type ("__java_double", -64);
6348 java_char_type_node = record_builtin_java_type ("__java_char", -16);
6349 java_boolean_type_node = record_builtin_java_type ("__java_boolean", -1);
6350
8d08fdba
MS
6351 integer_zero_node = build_int_2 (0, 0);
6352 TREE_TYPE (integer_zero_node) = integer_type_node;
6353 integer_one_node = build_int_2 (1, 0);
6354 TREE_TYPE (integer_one_node) = integer_type_node;
6355 integer_two_node = build_int_2 (2, 0);
6356 TREE_TYPE (integer_two_node) = integer_type_node;
6357 integer_three_node = build_int_2 (3, 0);
6358 TREE_TYPE (integer_three_node) = integer_type_node;
8d08fdba 6359
255512c1
JM
6360 boolean_type_node = make_unsigned_type (BOOL_TYPE_SIZE);
6361 TREE_SET_CODE (boolean_type_node, BOOLEAN_TYPE);
c1ea6a0b
BK
6362 TYPE_MAX_VALUE (boolean_type_node) = build_int_2 (1, 0);
6363 TREE_TYPE (TYPE_MAX_VALUE (boolean_type_node)) = boolean_type_node;
6364 TYPE_PRECISION (boolean_type_node) = 1;
255512c1
JM
6365 record_builtin_type (RID_BOOL, "bool", boolean_type_node);
6366 boolean_false_node = build_int_2 (0, 0);
6367 TREE_TYPE (boolean_false_node) = boolean_type_node;
6368 boolean_true_node = build_int_2 (1, 0);
6369 TREE_TYPE (boolean_true_node) = boolean_type_node;
2986ae00 6370
8d08fdba
MS
6371 /* These are needed by stor-layout.c. */
6372 size_zero_node = size_int (0);
6373 size_one_node = size_int (1);
6374
e92cc029
MS
6375 signed_size_zero_node = build_int_2 (0, 0);
6376 TREE_TYPE (signed_size_zero_node) = make_signed_type (TYPE_PRECISION (sizetype));
6377
8d08fdba
MS
6378 void_type_node = make_node (VOID_TYPE);
6379 record_builtin_type (RID_VOID, NULL_PTR, void_type_node);
6380 layout_type (void_type_node); /* Uses integer_zero_node. */
6381 void_list_node = build_tree_list (NULL_TREE, void_type_node);
6382 TREE_PARMLIST (void_list_node) = 1;
6383
d11ad92e 6384 null_pointer_node = build_int_2 (0, 0);
8d08fdba 6385 TREE_TYPE (null_pointer_node) = build_pointer_type (void_type_node);
d11ad92e
MS
6386 layout_type (TREE_TYPE (null_pointer_node));
6387
8d08fdba
MS
6388 /* Used for expressions that do nothing, but are not errors. */
6389 void_zero_node = build_int_2 (0, 0);
6390 TREE_TYPE (void_zero_node) = void_type_node;
6391
6392 string_type_node = build_pointer_type (char_type_node);
beb53fb8 6393 const_string_type_node
91063b51
MM
6394 = build_pointer_type (build_qualified_type (char_type_node,
6395 TYPE_QUAL_CONST));
4cc1d462 6396 empty_except_spec = build_tree_list (NULL_TREE, NULL_TREE);
6b5fbb55 6397#if 0
8d08fdba 6398 record_builtin_type (RID_MAX, NULL_PTR, string_type_node);
6b5fbb55 6399#endif
8d08fdba
MS
6400
6401 /* Make a type to be the domain of a few array types
6402 whose domains don't really matter.
6403 200 is small enough that it always fits in size_t
6404 and large enough that it can hold most function names for the
6405 initializations of __FUNCTION__ and __PRETTY_FUNCTION__. */
6406 array_domain_type = build_index_type (build_int_2 (200, 0));
6407
2c73f9f5 6408 /* Make a type for arrays of characters.
8d08fdba
MS
6409 With luck nothing will ever really depend on the length of this
6410 array type. */
6411 char_array_type_node
6412 = build_array_type (char_type_node, array_domain_type);
6413 /* Likewise for arrays of ints. */
6414 int_array_type_node
6415 = build_array_type (integer_type_node, array_domain_type);
6416
6417 /* This is just some anonymous class type. Nobody should ever
6418 need to look inside this envelope. */
6419 class_star_type_node = build_pointer_type (make_lang_type (RECORD_TYPE));
6420
7f4edbcb
BS
6421 if (flag_huge_objects)
6422 delta_type_node = long_integer_type_node;
6423 else
6424 delta_type_node = short_integer_type_node;
6425
8d08fdba
MS
6426 default_function_type
6427 = build_function_type (integer_type_node, NULL_TREE);
8d08fdba
MS
6428
6429 ptr_type_node = build_pointer_type (void_type_node);
beb53fb8 6430 const_ptr_type_node
91063b51 6431 = build_pointer_type (build_qualified_type (void_type_node,
7f4edbcb
BS
6432 TYPE_QUAL_CONST));
6433 c_common_nodes_and_builtins (1, flag_no_builtin, flag_no_nonansi_builtin);
6434
824b9a4c 6435 void_ftype_ptr
4cc1d462 6436 = build_exception_variant (void_ftype_ptr, empty_except_spec);
8d08fdba 6437
8d08fdba
MS
6438 /* C++ extensions */
6439
6440 unknown_type_node = make_node (UNKNOWN_TYPE);
036407f7
ML
6441 record_unknown_type (unknown_type_node, "unknown type");
6442
8d08fdba
MS
6443 /* Indirecting an UNKNOWN_TYPE node yields an UNKNOWN_TYPE node. */
6444 TREE_TYPE (unknown_type_node) = unknown_type_node;
a6967cc0 6445
03d0f4af 6446 TREE_TYPE (null_node) = type_for_size (POINTER_SIZE, 0);
a6967cc0
JM
6447
6448 /* Looking up TYPE_POINTER_TO and TYPE_REFERENCE_TO yield the same
6449 result. */
8d08fdba
MS
6450 TYPE_POINTER_TO (unknown_type_node) = unknown_type_node;
6451 TYPE_REFERENCE_TO (unknown_type_node) = unknown_type_node;
6452
6453 /* This is for handling opaque types in signatures. */
6454 opaque_type_node = copy_node (ptr_type_node);
6455 TYPE_MAIN_VARIANT (opaque_type_node) = opaque_type_node;
6456 record_builtin_type (RID_MAX, 0, opaque_type_node);
6457
e92cc029 6458 /* This is special for C++ so functions can be overloaded. */
8d08fdba
MS
6459 wchar_type_node
6460 = TREE_TYPE (IDENTIFIER_GLOBAL_VALUE (get_identifier (WCHAR_TYPE)));
6461 wchar_type_size = TYPE_PRECISION (wchar_type_node);
6462 signed_wchar_type_node = make_signed_type (wchar_type_size);
6463 unsigned_wchar_type_node = make_unsigned_type (wchar_type_size);
6464 wchar_type_node
6465 = TREE_UNSIGNED (wchar_type_node)
6466 ? unsigned_wchar_type_node
6467 : signed_wchar_type_node;
6468 record_builtin_type (RID_WCHAR, "__wchar_t", wchar_type_node);
6469
f376e137
MS
6470 /* Artificial declaration of wchar_t -- can be bashed */
6471 wchar_decl_node = build_decl (TYPE_DECL, get_identifier ("wchar_t"),
6472 wchar_type_node);
6473 pushdecl (wchar_decl_node);
6474
8d08fdba
MS
6475 /* This is for wide string constants. */
6476 wchar_array_type_node
6477 = build_array_type (wchar_type_node, array_domain_type);
6478
8926095f 6479 if (flag_vtable_thunks)
700f8a87
MS
6480 {
6481 /* Make sure we get a unique function type, so we can give
6482 its pointer type a name. (This wins for gdb.) */
6483 tree vfunc_type = make_node (FUNCTION_TYPE);
6484 TREE_TYPE (vfunc_type) = integer_type_node;
6485 TYPE_ARG_TYPES (vfunc_type) = NULL_TREE;
6486 layout_type (vfunc_type);
6487
6488 vtable_entry_type = build_pointer_type (vfunc_type);
6489 }
8926095f 6490 else
700f8a87
MS
6491 {
6492 vtable_entry_type = make_lang_type (RECORD_TYPE);
6493 fields[0] = build_lang_field_decl (FIELD_DECL, delta_identifier,
6494 delta_type_node);
6495 fields[1] = build_lang_field_decl (FIELD_DECL, index_identifier,
6496 delta_type_node);
6497 fields[2] = build_lang_field_decl (FIELD_DECL, pfn_identifier,
6498 ptr_type_node);
6499 finish_builtin_type (vtable_entry_type, VTBL_PTR_TYPE, fields, 2,
6500 double_type_node);
6501
6502 /* Make this part of an invisible union. */
6503 fields[3] = copy_node (fields[2]);
6504 TREE_TYPE (fields[3]) = delta_type_node;
6505 DECL_NAME (fields[3]) = delta2_identifier;
6506 DECL_MODE (fields[3]) = TYPE_MODE (delta_type_node);
6507 DECL_SIZE (fields[3]) = TYPE_SIZE (delta_type_node);
6508 TREE_UNSIGNED (fields[3]) = 0;
6509 TREE_CHAIN (fields[2]) = fields[3];
91063b51
MM
6510 vtable_entry_type = build_qualified_type (vtable_entry_type,
6511 TYPE_QUAL_CONST);
700f8a87
MS
6512 }
6513 record_builtin_type (RID_MAX, VTBL_PTR_TYPE, vtable_entry_type);
8d08fdba 6514
8d08fdba 6515 vtbl_type_node
52bf7d5d 6516 = build_cplus_array_type (vtable_entry_type, NULL_TREE);
8d08fdba 6517 layout_type (vtbl_type_node);
91063b51 6518 vtbl_type_node = build_qualified_type (vtbl_type_node, TYPE_QUAL_CONST);
8d08fdba 6519 record_builtin_type (RID_MAX, NULL_PTR, vtbl_type_node);
849da744
MM
6520 vtbl_ptr_type_node = build_pointer_type (vtable_entry_type);
6521 layout_type (vtbl_ptr_type_node);
6522 record_builtin_type (RID_MAX, NULL_PTR, vtbl_ptr_type_node);
8d08fdba
MS
6523
6524 /* Simplify life by making a "sigtable_entry_type". Give its
6525 fields names so that the debugger can use them. */
6526
6527 if (flag_handle_signatures)
6528 {
6529 sigtable_entry_type = make_lang_type (RECORD_TYPE);
dff6b454
RK
6530 fields[0] = build_lang_field_decl (FIELD_DECL, tag_identifier,
6531 delta_type_node);
9dd70aa4 6532 fields[1] = build_lang_field_decl (FIELD_DECL, vb_off_identifier,
dff6b454 6533 delta_type_node);
9dd70aa4 6534 fields[2] = build_lang_field_decl (FIELD_DECL, delta_identifier,
dff6b454
RK
6535 delta_type_node);
6536 fields[3] = build_lang_field_decl (FIELD_DECL, index_identifier,
6537 delta_type_node);
9dd70aa4
GB
6538 fields[4] = build_lang_field_decl (FIELD_DECL, pfn_identifier,
6539 ptr_type_node);
6540
6541 /* Set the alignment to the max of the alignment of ptr_type_node and
6542 delta_type_node. Double alignment wastes a word on the Sparc. */
6543 finish_builtin_type (sigtable_entry_type, SIGTABLE_PTR_TYPE, fields, 4,
6544 (TYPE_ALIGN (ptr_type_node) > TYPE_ALIGN (delta_type_node))
6545 ? ptr_type_node
6546 : delta_type_node);
dff6b454
RK
6547
6548 /* Make this part of an invisible union. */
9dd70aa4
GB
6549 fields[5] = copy_node (fields[4]);
6550 TREE_TYPE (fields[5]) = delta_type_node;
6551 DECL_NAME (fields[5]) = vt_off_identifier;
6552 DECL_MODE (fields[5]) = TYPE_MODE (delta_type_node);
6553 DECL_SIZE (fields[5]) = TYPE_SIZE (delta_type_node);
6554 TREE_UNSIGNED (fields[5]) = 0;
6555 TREE_CHAIN (fields[4]) = fields[5];
dff6b454 6556
91063b51
MM
6557 sigtable_entry_type = build_qualified_type (sigtable_entry_type,
6558 TYPE_QUAL_CONST);
8d08fdba
MS
6559 record_builtin_type (RID_MAX, SIGTABLE_PTR_TYPE, sigtable_entry_type);
6560 }
6561
2c73f9f5
ML
6562 std_node = build_decl (NAMESPACE_DECL,
6563 get_identifier (flag_honor_std ? "fake std":"std"),
6633d636
MS
6564 void_type_node);
6565 pushdecl (std_node);
6566
2c73f9f5 6567 global_type_node = make_node (LANG_TYPE);
036407f7 6568 record_unknown_type (global_type_node, "global type");
2c73f9f5 6569
db5ae43f
MS
6570 /* Now, C++. */
6571 current_lang_name = lang_name_cplusplus;
8d08fdba 6572
ced78d8b 6573 {
2c73f9f5
ML
6574 tree bad_alloc_type_node, newtype, deltype;
6575 if (flag_honor_std)
6576 push_namespace (get_identifier ("std"));
6577 bad_alloc_type_node = xref_tag
ca107ded 6578 (class_type_node, get_identifier ("bad_alloc"), 1);
2c73f9f5
ML
6579 if (flag_honor_std)
6580 pop_namespace ();
6581 newtype = build_exception_variant
4cc1d462
NS
6582 (ptr_ftype_sizetype, add_exception_specifier (NULL_TREE, bad_alloc_type_node, -1));
6583 deltype = build_exception_variant (void_ftype_ptr, empty_except_spec);
ced78d8b
JM
6584 auto_function (ansi_opname[(int) NEW_EXPR], newtype, NOT_BUILT_IN);
6585 auto_function (ansi_opname[(int) VEC_NEW_EXPR], newtype, NOT_BUILT_IN);
94e098d1
MM
6586 global_delete_fndecl
6587 = auto_function (ansi_opname[(int) DELETE_EXPR], deltype, NOT_BUILT_IN);
ced78d8b
JM
6588 auto_function (ansi_opname[(int) VEC_DELETE_EXPR], deltype, NOT_BUILT_IN);
6589 }
8d08fdba
MS
6590
6591 abort_fndecl
de22184b 6592 = define_function ("__pure_virtual", void_ftype,
8d08fdba
MS
6593 NOT_BUILT_IN, 0, 0);
6594
8d08fdba
MS
6595 /* Perform other language dependent initializations. */
6596 init_class_processing ();
6597 init_init_processing ();
6598 init_search_processing ();
1737fe20
BK
6599 if (flag_rtti)
6600 init_rtti_processing ();
8d08fdba 6601
6467930b 6602 if (flag_exceptions)
8d2733ca 6603 init_exception_processing ();
8d08fdba
MS
6604 if (flag_no_inline)
6605 {
6606 flag_inline_functions = 0;
8d08fdba 6607 }
9e9ff709 6608
7fcdf4c2 6609 if (! supports_one_only ())
72b7eeff 6610 flag_weak = 0;
8d08fdba
MS
6611
6612 /* Create the global bindings for __FUNCTION__ and __PRETTY_FUNCTION__. */
6613 declare_function_name ();
6614
6615 /* Prepare to check format strings against argument lists. */
6616 init_function_format_info ();
e9a25f70
JL
6617
6618 /* Show we use EH for cleanups. */
6619 using_eh_for_cleanups ();
62c154ed
JM
6620
6621 print_error_function = lang_print_error_function;
501ba25a 6622 lang_get_alias_set = &c_get_alias_set;
e5dc5fb2 6623 valid_lang_attribute = cp_valid_lang_attribute;
d9cf7c82
JM
6624
6625 /* Maintain consistency. Perhaps we should just complain if they
6626 say -fwritable-strings? */
6627 if (flag_writable_strings)
6628 flag_const_strings = 0;
62c154ed
JM
6629}
6630
6631/* Function to print any language-specific context for an error message. */
6632
6633static void
6634lang_print_error_function (file)
6635 char *file;
6636{
6637 default_print_error_function (file);
6638 maybe_print_template_context ();
8d08fdba
MS
6639}
6640
6641/* Make a definition for a builtin function named NAME and whose data type
6642 is TYPE. TYPE should be a function type with argument types.
6643 FUNCTION_CODE tells later passes how to compile calls to this function.
6644 See tree.h for its possible values.
6645
6646 If LIBRARY_NAME is nonzero, use that for DECL_ASSEMBLER_NAME,
6647 the name to be called if we can't opencode the function. */
6648
6649tree
6650define_function (name, type, function_code, pfn, library_name)
d8e178a0 6651 const char *name;
8d08fdba
MS
6652 tree type;
6653 enum built_in_function function_code;
49c249e1 6654 void (*pfn) PROTO((tree));
d8e178a0 6655 const char *library_name;
8d08fdba
MS
6656{
6657 tree decl = build_lang_decl (FUNCTION_DECL, get_identifier (name), type);
6658 DECL_EXTERNAL (decl) = 1;
6659 TREE_PUBLIC (decl) = 1;
863adfc0 6660 DECL_ARTIFICIAL (decl) = 1;
8d08fdba 6661
2c73f9f5 6662 my_friendly_assert (DECL_CONTEXT (decl) == NULL_TREE, 392);
cb0dbb9a 6663 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
2c73f9f5 6664
8d08fdba
MS
6665 /* Since `pushdecl' relies on DECL_ASSEMBLER_NAME instead of DECL_NAME,
6666 we cannot change DECL_ASSEMBLER_NAME until we have installed this
6667 function in the namespace. */
6668 if (pfn) (*pfn) (decl);
6669 if (library_name)
6670 DECL_ASSEMBLER_NAME (decl) = get_identifier (library_name);
6671 make_function_rtl (decl);
6672 if (function_code != NOT_BUILT_IN)
6673 {
6674 DECL_BUILT_IN (decl) = 1;
39211cd5 6675 DECL_FUNCTION_CODE (decl) = function_code;
8d08fdba
MS
6676 }
6677 return decl;
6678}
7f4edbcb
BS
6679
6680tree
6681builtin_function (name, type, code, libname)
6682 const char *name;
6683 tree type;
6684 enum built_in_function code;
6685 const char *libname;
6686{
6687 return define_function (name, type, code, (void (*) PROTO((tree)))pushdecl, libname);
6688}
8d08fdba 6689\f
61a127b3
MM
6690/* When we call finish_struct for an anonymous union, we create
6691 default copy constructors and such. But, an anonymous union
6692 shouldn't have such things; this function undoes the damage to the
6693 anonymous union type T.
6694
6695 (The reason that we create the synthesized methods is that we don't
6696 distinguish `union { int i; }' from `typedef union { int i; } U'.
6697 The first is an anonymous union; the second is just an ordinary
6698 union type.) */
6699
6700void
6bdb8141 6701fixup_anonymous_aggr (t)
61a127b3
MM
6702 tree t;
6703{
6704 tree *q;
6705
6706 /* Wipe out memory of synthesized methods */
6707 TYPE_HAS_CONSTRUCTOR (t) = 0;
6708 TYPE_HAS_DEFAULT_CONSTRUCTOR (t) = 0;
6709 TYPE_HAS_INIT_REF (t) = 0;
6710 TYPE_HAS_CONST_INIT_REF (t) = 0;
6711 TYPE_HAS_ASSIGN_REF (t) = 0;
61a127b3
MM
6712 TYPE_HAS_CONST_ASSIGN_REF (t) = 0;
6713
6714 /* Splice the implicitly generated functions out of the TYPE_METHODS
6715 list. */
6716 q = &TYPE_METHODS (t);
6717 while (*q)
6718 {
6719 if (DECL_ARTIFICIAL (*q))
6720 *q = TREE_CHAIN (*q);
6721 else
6722 q = &TREE_CHAIN (*q);
6723 }
6724
6725 /* ANSI C++ June 5 1992 WP 9.5.3. Anonymous unions may not have
6726 function members. */
6727 if (TYPE_METHODS (t))
6728 error ("an anonymous union cannot have function members");
6729}
6730
72a93143
JM
6731/* Make sure that a declaration with no declarator is well-formed, i.e.
6732 just defines a tagged type or anonymous union.
8d08fdba 6733
72a93143 6734 Returns the type defined, if any. */
8d08fdba 6735
72a93143
JM
6736tree
6737check_tag_decl (declspecs)
8d08fdba
MS
6738 tree declspecs;
6739{
72a93143 6740 int found_type = 0;
2986ae00 6741 tree ob_modifier = NULL_TREE;
8d08fdba 6742 register tree link;
8d08fdba
MS
6743 register tree t = NULL_TREE;
6744
6745 for (link = declspecs; link; link = TREE_CHAIN (link))
6746 {
6747 register tree value = TREE_VALUE (link);
6748
72a93143 6749 if (TYPE_P (value))
8d08fdba 6750 {
72a93143 6751 ++found_type;
5566b478 6752
72a93143
JM
6753 if (IS_AGGR_TYPE (value) || TREE_CODE (value) == ENUMERAL_TYPE)
6754 {
6755 my_friendly_assert (TYPE_MAIN_DECL (value) != NULL_TREE, 261);
6756 t = value;
6757 }
8d08fdba 6758 }
83f660b7
JM
6759 else if (value == ridpointers[(int) RID_FRIEND])
6760 {
83f660b7
JM
6761 if (current_class_type == NULL_TREE
6762 || current_scope () != current_class_type)
6763 ob_modifier = value;
6764 }
8d08fdba 6765 else if (value == ridpointers[(int) RID_STATIC]
2986ae00
MS
6766 || value == ridpointers[(int) RID_EXTERN]
6767 || value == ridpointers[(int) RID_AUTO]
28cbf42c
MS
6768 || value == ridpointers[(int) RID_REGISTER]
6769 || value == ridpointers[(int) RID_INLINE]
6770 || value == ridpointers[(int) RID_VIRTUAL]
72a93143
JM
6771 || value == ridpointers[(int) RID_CONST]
6772 || value == ridpointers[(int) RID_VOLATILE]
28cbf42c 6773 || value == ridpointers[(int) RID_EXPLICIT])
2986ae00 6774 ob_modifier = value;
8d08fdba
MS
6775 }
6776
72a93143
JM
6777 if (found_type > 1)
6778 error ("multiple types in one declaration");
7e2067ca
JM
6779
6780 /* Inside a class, we might be in a friend or access declaration.
6781 Until we have a good way of detecting the latter, don't warn. */
6782 if (t == NULL_TREE && ! current_class_type)
6783 pedwarn ("declaration does not declare anything");
0dd3962d
JM
6784
6785 /* Check for an anonymous union. We're careful
6786 accessing TYPE_IDENTIFIER because some built-in types, like
6787 pointer-to-member types, do not have TYPE_NAME. */
6bdb8141 6788 else if (t && IS_AGGR_TYPE_CODE (TREE_CODE (t))
0dd3962d
JM
6789 && TYPE_NAME (t)
6790 && ANON_AGGRNAME_P (TYPE_IDENTIFIER (t)))
6791 {
6792 /* Anonymous unions are objects, so they can have specifiers. */;
6bdb8141
JM
6793 SET_ANON_AGGR_TYPE_P (t);
6794
6795 if (TREE_CODE (t) != UNION_TYPE && pedantic && ! in_system_header)
6796 pedwarn ("ISO C++ prohibits anonymous structs");
0dd3962d
JM
6797 }
6798
83f660b7 6799 else if (ob_modifier)
8d08fdba 6800 {
83f660b7
JM
6801 if (ob_modifier == ridpointers[(int) RID_INLINE]
6802 || ob_modifier == ridpointers[(int) RID_VIRTUAL])
6803 cp_error ("`%D' can only be specified for functions", ob_modifier);
6804 else if (ob_modifier == ridpointers[(int) RID_FRIEND])
6805 cp_error ("`%D' can only be specified inside a class", ob_modifier);
6806 else if (ob_modifier == ridpointers[(int) RID_EXPLICIT])
6807 cp_error ("`%D' can only be specified for constructors",
6808 ob_modifier);
6809 else
6810 cp_error ("`%D' can only be specified for objects and functions",
6811 ob_modifier);
72a93143 6812 }
8d08fdba 6813
72a93143
JM
6814 return t;
6815}
6816
6817/* Called when a declaration is seen that contains no names to declare.
6818 If its type is a reference to a structure, union or enum inherited
6819 from a containing scope, shadow that tag name for the current scope
6820 with a forward reference.
6821 If its type defines a new named structure or union
6822 or defines an enum, it is valid but we need not do anything here.
6823 Otherwise, it is an error.
6824
6825 C++: may have to grok the declspecs to learn about static,
6826 complain for anonymous unions. */
6827
6828void
6829shadow_tag (declspecs)
6830 tree declspecs;
6831{
6832 tree t = check_tag_decl (declspecs);
6833
6834 if (t)
6835 maybe_process_partial_specialization (t);
6836
6837 /* This is where the variables in an anonymous union are
6838 declared. An anonymous union declaration looks like:
6839 union { ... } ;
6840 because there is no declarator after the union, the parser
6841 sends that declaration here. */
6bdb8141 6842 if (t && ANON_AGGR_TYPE_P (t))
72a93143 6843 {
6bdb8141 6844 fixup_anonymous_aggr (t);
72a93143
JM
6845
6846 if (TYPE_FIELDS (t))
6847 {
6848 tree decl = grokdeclarator (NULL_TREE, declspecs, NORMAL, 0,
6849 NULL_TREE);
6850 finish_anon_union (decl);
6851 }
8d08fdba
MS
6852 }
6853}
6854\f
6855/* Decode a "typename", such as "int **", returning a ..._TYPE node. */
6856
6857tree
6858groktypename (typename)
6859 tree typename;
6860{
6861 if (TREE_CODE (typename) != TREE_LIST)
6862 return typename;
6863 return grokdeclarator (TREE_VALUE (typename),
6864 TREE_PURPOSE (typename),
c11b6f21 6865 TYPENAME, 0, NULL_TREE);
8d08fdba
MS
6866}
6867
6868/* Decode a declarator in an ordinary declaration or data definition.
6869 This is called as soon as the type information and variable name
6870 have been parsed, before parsing the initializer if any.
6871 Here we create the ..._DECL node, fill in its type,
6872 and put it on the list of decls for the current context.
6873 The ..._DECL node is returned as the value.
6874
6875 Exception: for arrays where the length is not specified,
82580166 6876 the type is left null, to be filled in by `cp_finish_decl'.
8d08fdba
MS
6877
6878 Function definitions do not come here; they go to start_function
6879 instead. However, external and forward declarations of functions
6880 do go through here. Structure field declarations are done by
6881 grokfield and not through here. */
6882
6883/* Set this to zero to debug not using the temporary obstack
6884 to parse initializers. */
6885int debug_temp_inits = 1;
6886
6887tree
a1774733 6888start_decl (declarator, declspecs, initialized, attributes, prefix_attributes)
8d08fdba
MS
6889 tree declarator, declspecs;
6890 int initialized;
a1774733 6891 tree attributes, prefix_attributes;
8d08fdba
MS
6892{
6893 register tree decl;
6894 register tree type, tem;
6895 tree context;
6896 extern int have_extern_spec;
6897 extern int used_extern_spec;
b17e2870 6898 tree attrlist;
8d08fdba 6899
5566b478
MS
6900#if 0
6901 /* See code below that used this. */
8d08fdba 6902 int init_written = initialized;
5566b478 6903#endif
8d08fdba 6904
e92cc029 6905 /* This should only be done once on the top most decl. */
8d08fdba
MS
6906 if (have_extern_spec && !used_extern_spec)
6907 {
a28e3c7f
MS
6908 declspecs = decl_tree_cons (NULL_TREE, get_identifier ("extern"),
6909 declspecs);
8d08fdba
MS
6910 used_extern_spec = 1;
6911 }
6912
b17e2870
JM
6913 if (attributes || prefix_attributes)
6914 attrlist = build_scratch_list (attributes, prefix_attributes);
6915 else
6916 attrlist = NULL_TREE;
6917
c11b6f21 6918 decl = grokdeclarator (declarator, declspecs, NORMAL, initialized,
b17e2870
JM
6919 attrlist);
6920
a1774733 6921 if (decl == NULL_TREE || TREE_CODE (decl) == VOID_TYPE)
8d08fdba
MS
6922 return NULL_TREE;
6923
6924 type = TREE_TYPE (decl);
6925
44689c12
ML
6926 if (type == error_mark_node)
6927 return NULL_TREE;
6928
8d08fdba 6929 /* Don't lose if destructors must be executed at file-level. */
5156628f 6930 if (! processing_template_decl && TREE_STATIC (decl)
ec255269 6931 && TYPE_NEEDS_DESTRUCTOR (complete_type (type))
8d08fdba
MS
6932 && !TREE_PERMANENT (decl))
6933 {
6934 push_obstacks (&permanent_obstack, &permanent_obstack);
6935 decl = copy_node (decl);
6936 if (TREE_CODE (type) == ARRAY_TYPE)
6937 {
6938 tree itype = TYPE_DOMAIN (type);
6939 if (itype && ! TREE_PERMANENT (itype))
6940 {
6941 itype = build_index_type (copy_to_permanent (TYPE_MAX_VALUE (itype)));
6942 type = build_cplus_array_type (TREE_TYPE (type), itype);
6943 TREE_TYPE (decl) = type;
6944 }
6945 }
6946 pop_obstacks ();
6947 }
6948
8d08fdba
MS
6949 context
6950 = (TREE_CODE (decl) == FUNCTION_DECL && DECL_VIRTUAL_P (decl))
6951 ? DECL_CLASS_CONTEXT (decl)
6952 : DECL_CONTEXT (decl);
6953
9a68c51f
JM
6954 if (initialized && context && TREE_CODE (context) == NAMESPACE_DECL
6955 && context != current_namespace && TREE_CODE (decl) == VAR_DECL)
6956 {
6957 /* When parsing the initializer, lookup should use the object's
6958 namespace. */
6959 push_decl_namespace (context);
6960 }
6961
2c73f9f5
ML
6962 /* We are only interested in class contexts, later. */
6963 if (context && TREE_CODE (context) == NAMESPACE_DECL)
6964 context = NULL_TREE;
6965
8d08fdba
MS
6966 if (initialized)
6967 /* Is it valid for this decl to have an initializer at all?
6968 If not, set INITIALIZED to zero, which will indirectly
82580166 6969 tell `cp_finish_decl' to ignore the initializer once it is parsed. */
8d08fdba
MS
6970 switch (TREE_CODE (decl))
6971 {
6972 case TYPE_DECL:
6973 /* typedef foo = bar means give foo the same type as bar.
82580166 6974 We haven't parsed bar yet, so `cp_finish_decl' will fix that up.
8d08fdba
MS
6975 Any other case of an initialization in a TYPE_DECL is an error. */
6976 if (pedantic || list_length (declspecs) > 1)
6977 {
8251199e 6978 cp_error ("typedef `%D' is initialized", decl);
8d08fdba
MS
6979 initialized = 0;
6980 }
6981 break;
6982
6983 case FUNCTION_DECL:
8251199e 6984 cp_error ("function `%#D' is initialized like a variable", decl);
8d08fdba
MS
6985 initialized = 0;
6986 break;
6987
6988 default:
3e41d13b 6989 break;
8d08fdba
MS
6990 }
6991
8d08fdba
MS
6992 if (initialized)
6993 {
a9aedbc2 6994 if (! toplevel_bindings_p ()
8d08fdba 6995 && DECL_EXTERNAL (decl))
8251199e 6996 cp_warning ("declaration of `%#D' has `extern' and is initialized",
8d08fdba
MS
6997 decl);
6998 DECL_EXTERNAL (decl) = 0;
5566b478 6999 if (toplevel_bindings_p ())
8d08fdba
MS
7000 TREE_STATIC (decl) = 1;
7001
7002 /* Tell `pushdecl' this is an initialized decl
7003 even though we don't yet have the initializer expression.
82580166 7004 Also tell `cp_finish_decl' it may store the real initializer. */
8d08fdba
MS
7005 DECL_INITIAL (decl) = error_mark_node;
7006 }
7007
fa20888b
MK
7008#ifdef SET_DEFAULT_DECL_ATTRIBUTES
7009 SET_DEFAULT_DECL_ATTRIBUTES (decl, attributes);
7010#endif
7011
7012 /* Set attributes here so if duplicate decl, will have proper attributes. */
7013 cplus_decl_attributes (decl, attributes, prefix_attributes);
7014
5566b478 7015 if (context && TYPE_SIZE (complete_type (context)) != NULL_TREE)
5b605f68 7016 {
6b400b21 7017 push_nested_class (context, 2);
e97e5263 7018
5b605f68
MS
7019 if (TREE_CODE (decl) == VAR_DECL)
7020 {
7021 tree field = lookup_field (context, DECL_NAME (decl), 0, 0);
7022 if (field == NULL_TREE || TREE_CODE (field) != VAR_DECL)
8251199e 7023 cp_error ("`%#D' is not a static member of `%#T'", decl, context);
e349ee73
MS
7024 else
7025 {
7026 if (DECL_CONTEXT (field) != context)
f2d773a2 7027 {
8251199e 7028 cp_pedwarn ("ANSI C++ does not permit `%T::%D' to be defined as `%T::%D'",
f2d773a2
JM
7029 DECL_CONTEXT (field), DECL_NAME (decl),
7030 context, DECL_NAME (decl));
7031 DECL_CONTEXT (decl) = DECL_CONTEXT (field);
7032 }
75650646
MM
7033 /* Static data member are tricky; an in-class initialization
7034 still doesn't provide a definition, so the in-class
7035 declaration will have DECL_EXTERNAL set, but will have an
7036 initialization. Thus, duplicate_decls won't warn
7037 about this situation, and so we check here. */
7038 if (DECL_INITIAL (decl) && DECL_INITIAL (field))
8251199e 7039 cp_error ("duplicate initialization of %D", decl);
e349ee73
MS
7040 if (duplicate_decls (decl, field))
7041 decl = field;
7042 }
5b605f68 7043 }
f30432d7
MS
7044 else
7045 {
5566b478 7046 tree field = check_classfn (context, decl);
f30432d7
MS
7047 if (field && duplicate_decls (decl, field))
7048 decl = field;
7049 }
7050
7051 /* cp_finish_decl sets DECL_EXTERNAL if DECL_IN_AGGR_P is set. */
dff3e828
BK
7052 DECL_IN_AGGR_P (decl) = 0;
7053 if ((DECL_LANG_SPECIFIC (decl) && DECL_USE_TEMPLATE (decl))
7054 || CLASSTYPE_USE_TEMPLATE (context))
84e6233f
JM
7055 {
7056 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
7057 /* [temp.expl.spec] An explicit specialization of a static data
7058 member of a template is a definition if the declaration
7059 includes an initializer; otherwise, it is a declaration.
7060
7061 We check for processing_specialization so this only applies
7062 to the new specialization syntax. */
7063 if (DECL_INITIAL (decl) == NULL_TREE && processing_specialization)
7064 DECL_EXTERNAL (decl) = 1;
7065 }
f30432d7 7066
b7698cf0 7067 if (DECL_EXTERNAL (decl) && ! DECL_TEMPLATE_SPECIALIZATION (decl))
8251199e 7068 cp_pedwarn ("declaration of `%#D' outside of class is not definition",
f30432d7 7069 decl);
5b605f68
MS
7070 }
7071
8d08fdba
MS
7072 /* Add this decl to the current binding level, but not if it
7073 comes from another scope, e.g. a static member variable.
7074 TEM may equal DECL or it may be a previous decl of the same name. */
5b605f68 7075
51bcf661
JM
7076 if ((TREE_CODE (decl) != PARM_DECL && DECL_CONTEXT (decl) != NULL_TREE
7077 /* Definitions of namespace members outside their namespace are
7078 possible. */
7079 && TREE_CODE (DECL_CONTEXT (decl)) != NAMESPACE_DECL)
2c73f9f5 7080 || (TREE_CODE (decl) == TEMPLATE_DECL && !namespace_bindings_p ())
75650646
MM
7081 || TREE_CODE (type) == LANG_TYPE
7082 /* The declaration of template specializations does not affect
7083 the functions available for overload resolution, so we do not
7084 call pushdecl. */
e1467ff2 7085 || (TREE_CODE (decl) == FUNCTION_DECL
75650646 7086 && DECL_TEMPLATE_SPECIALIZATION (decl)))
8d08fdba
MS
7087 tem = decl;
7088 else
8926095f 7089 tem = pushdecl (decl);
2ee887f2 7090
5156628f 7091 if (processing_template_decl)
5566b478
MS
7092 {
7093 if (! current_function_decl)
3ac3d9ea 7094 tem = push_template_decl (tem);
5566b478 7095 else if (minimal_parse_mode)
b5effa19 7096 DECL_VINDEX (tem)
5566b478
MS
7097 = build_min_nt (DECL_STMT, copy_to_permanent (declarator),
7098 copy_to_permanent (declspecs),
c11b6f21 7099 NULL_TREE);
5566b478
MS
7100 }
7101
7102
2ee887f2 7103#if ! defined (ASM_OUTPUT_BSS) && ! defined (ASM_OUTPUT_ALIGNED_BSS)
a3203465 7104 /* Tell the back-end to use or not use .common as appropriate. If we say
a50f0918
MS
7105 -fconserve-space, we want this to save .data space, at the expense of
7106 wrong semantics. If we say -fno-conserve-space, we want this to
7107 produce errors about redefs; to do this we force variables into the
7108 data segment. */
a3203465 7109 DECL_COMMON (tem) = flag_conserve_space || ! TREE_PUBLIC (tem);
2ee887f2 7110#endif
3a846e6e 7111
5156628f 7112 if (! processing_template_decl)
5566b478 7113 start_decl_1 (tem);
8d08fdba 7114
5566b478
MS
7115 /* Corresponding pop_obstacks is done in `cp_finish_decl'. */
7116 push_obstacks_nochange ();
8d08fdba 7117
5566b478
MS
7118#if 0
7119 /* We have no way of knowing whether the initializer will need to be
7120 evaluated at run-time or not until we've parsed it, so let's just put
7121 it in the permanent obstack. (jason) */
8d08fdba
MS
7122 if (init_written
7123 && ! (TREE_CODE (tem) == PARM_DECL
7124 || (TREE_READONLY (tem)
7125 && (TREE_CODE (tem) == VAR_DECL
7126 || TREE_CODE (tem) == FIELD_DECL))))
7127 {
7128 /* When parsing and digesting the initializer,
7129 use temporary storage. Do this even if we will ignore the value. */
a9aedbc2 7130 if (toplevel_bindings_p () && debug_temp_inits)
8d08fdba 7131 {
5156628f 7132 if (processing_template_decl
5566b478 7133 || TYPE_NEEDS_CONSTRUCTING (type)
a28e3c7f 7134 || TREE_CODE (type) == REFERENCE_TYPE)
8d08fdba
MS
7135 /* In this case, the initializer must lay down in permanent
7136 storage, since it will be saved until `finish_file' is run. */
7137 ;
7138 else
7139 temporary_allocation ();
7140 }
7141 }
5566b478 7142#endif
8d08fdba 7143
8d08fdba
MS
7144 return tem;
7145}
7146
5566b478
MS
7147void
7148start_decl_1 (decl)
7149 tree decl;
8d08fdba 7150{
5566b478
MS
7151 tree type = TREE_TYPE (decl);
7152 int initialized = (DECL_INITIAL (decl) != NULL_TREE);
8d08fdba 7153
44689c12
ML
7154 if (type == error_mark_node)
7155 return;
7156
5566b478
MS
7157 /* If this type of object needs a cleanup, and control may
7158 jump past it, make a new binding level so that it is cleaned
7159 up only when it is initialized first. */
7160 if (TYPE_NEEDS_DESTRUCTOR (type)
7161 && current_binding_level->more_cleanups_ok == 0)
7162 pushlevel_temporary (1);
7163
7164 if (initialized)
7165 /* Is it valid for this decl to have an initializer at all?
7166 If not, set INITIALIZED to zero, which will indirectly
7167 tell `cp_finish_decl' to ignore the initializer once it is parsed. */
8d08fdba 7168 {
5566b478
MS
7169 /* Don't allow initializations for incomplete types except for
7170 arrays which might be completed by the initialization. */
44689c12 7171 if (TYPE_SIZE (complete_type (type)) != NULL_TREE)
5566b478
MS
7172 ; /* A complete type is ok. */
7173 else if (TREE_CODE (type) != ARRAY_TYPE)
8d08fdba 7174 {
8251199e 7175 cp_error ("variable `%#D' has initializer but incomplete type",
5566b478
MS
7176 decl);
7177 initialized = 0;
25eb19ff 7178 type = TREE_TYPE (decl) = error_mark_node;
5566b478
MS
7179 }
7180 else if (TYPE_SIZE (complete_type (TREE_TYPE (type))) == NULL_TREE)
7181 {
7182 if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
8251199e 7183 cp_error ("elements of array `%#D' have incomplete type", decl);
5566b478
MS
7184 /* else we already gave an error in start_decl. */
7185 initialized = 0;
8d08fdba 7186 }
8d08fdba
MS
7187 }
7188
5566b478
MS
7189 if (!initialized
7190 && TREE_CODE (decl) != TYPE_DECL
7191 && TREE_CODE (decl) != TEMPLATE_DECL
7192 && IS_AGGR_TYPE (type) && ! DECL_EXTERNAL (decl))
8d08fdba 7193 {
5156628f 7194 if ((! processing_template_decl || ! uses_template_parms (type))
7fcdf4c2 7195 && TYPE_SIZE (complete_type (type)) == NULL_TREE)
5566b478 7196 {
8251199e 7197 cp_error ("aggregate `%#D' has incomplete type and cannot be initialized",
5566b478
MS
7198 decl);
7199 /* Change the type so that assemble_variable will give
7200 DECL an rtl we can live with: (mem (const_int 0)). */
25eb19ff 7201 type = TREE_TYPE (decl) = error_mark_node;
5566b478
MS
7202 }
7203 else
7204 {
7205 /* If any base type in the hierarchy of TYPE needs a constructor,
7206 then we set initialized to 1. This way any nodes which are
7207 created for the purposes of initializing this aggregate
7208 will live as long as it does. This is necessary for global
7209 aggregates which do not have their initializers processed until
7210 the end of the file. */
7211 initialized = TYPE_NEEDS_CONSTRUCTING (type);
7212 }
7213 }
7214
7215#if 0
7216 /* We don't do this yet for GNU C++. */
7217 /* For a local variable, define the RTL now. */
7218 if (! toplevel_bindings_p ()
7219 /* But not if this is a duplicate decl
7220 and we preserved the rtl from the previous one
7221 (which may or may not happen). */
7222 && DECL_RTL (tem) == NULL_RTX)
7223 {
7224 if (TYPE_SIZE (TREE_TYPE (tem)) != NULL_TREE)
7225 expand_decl (tem);
7226 else if (TREE_CODE (TREE_TYPE (tem)) == ARRAY_TYPE
7227 && DECL_INITIAL (tem) != NULL_TREE)
7228 expand_decl (tem);
7229 }
7230#endif
7231
7232 if (! initialized)
7233 DECL_INITIAL (decl) = NULL_TREE;
7234}
7235
7236/* Handle initialization of references.
38e01259 7237 These three arguments are from `cp_finish_decl', and have the
e92cc029
MS
7238 same meaning here that they do there.
7239
7240 Quotes on semantics can be found in ARM 8.4.3. */
7241
5566b478 7242static void
a703fb38 7243grok_reference_init (decl, type, init)
5566b478 7244 tree decl, type, init;
5566b478
MS
7245{
7246 tree tmp;
7247
7248 if (init == NULL_TREE)
7249 {
7250 if ((DECL_LANG_SPECIFIC (decl) == 0
7251 || DECL_IN_AGGR_P (decl) == 0)
7252 && ! DECL_THIS_EXTERN (decl))
7253 {
8251199e 7254 cp_error ("`%D' declared as reference but not initialized", decl);
5566b478
MS
7255 if (TREE_CODE (decl) == VAR_DECL)
7256 SET_DECL_REFERENCE_SLOT (decl, error_mark_node);
7257 }
7258 return;
7259 }
7260
7261 if (init == error_mark_node)
7262 return;
7263
7264 if (TREE_CODE (type) == REFERENCE_TYPE
7265 && TREE_CODE (init) == CONSTRUCTOR)
7266 {
8251199e 7267 cp_error ("ANSI C++ forbids use of initializer list to initialize reference `%D'", decl);
8d08fdba
MS
7268 return;
7269 }
7270
7271 if (TREE_CODE (init) == TREE_LIST)
7272 init = build_compound_expr (init);
8d08fdba 7273
8ccc31eb
MS
7274 if (TREE_CODE (TREE_TYPE (init)) == REFERENCE_TYPE)
7275 init = convert_from_reference (init);
7276
8d08fdba
MS
7277 if (TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE
7278 && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE)
7279 {
a3203465 7280 /* Note: default conversion is only called in very special cases. */
8d08fdba
MS
7281 init = default_conversion (init);
7282 }
7283
a3203465 7284 tmp = convert_to_reference
9a3b49ac
MS
7285 (type, init, CONV_IMPLICIT,
7286 LOOKUP_SPECULATIVELY|LOOKUP_NORMAL|DIRECT_BIND, decl);
8d08fdba 7287
a3203465
MS
7288 if (tmp == error_mark_node)
7289 goto fail;
7290 else if (tmp != NULL_TREE)
8d08fdba 7291 {
a3203465 7292 init = tmp;
4c7bdca6 7293 DECL_INITIAL (decl) = save_expr (init);
8d08fdba 7294 }
a3203465 7295 else
8d08fdba 7296 {
8251199e 7297 cp_error ("cannot initialize `%T' from `%T'", type, TREE_TYPE (init));
a3203465 7298 goto fail;
8d08fdba 7299 }
8d08fdba 7300
8d08fdba
MS
7301 /* ?? Can this be optimized in some cases to
7302 hand back the DECL_INITIAL slot?? */
7303 if (TYPE_SIZE (TREE_TYPE (type)))
7304 {
7305 init = convert_from_reference (decl);
7306 if (TREE_PERMANENT (decl))
7307 init = copy_to_permanent (init);
7308 SET_DECL_REFERENCE_SLOT (decl, init);
7309 }
7310
7311 if (TREE_STATIC (decl) && ! TREE_CONSTANT (DECL_INITIAL (decl)))
7312 {
7313 expand_static_init (decl, DECL_INITIAL (decl));
7314 DECL_INITIAL (decl) = NULL_TREE;
7315 }
7316 return;
7317
7318 fail:
7319 if (TREE_CODE (decl) == VAR_DECL)
7320 SET_DECL_REFERENCE_SLOT (decl, error_mark_node);
7321 return;
7322}
7323
6060a796
MS
7324/* Fill in DECL_INITIAL with some magical value to prevent expand_decl from
7325 mucking with forces it does not comprehend (i.e. initialization with a
7326 constructor). If we are at global scope and won't go into COMMON, fill
7327 it in with a dummy CONSTRUCTOR to force the variable into .data;
7328 otherwise we can use error_mark_node. */
7329
28cbf42c
MS
7330static tree
7331obscure_complex_init (decl, init)
7332 tree decl, init;
6060a796 7333{
28cbf42c
MS
7334 if (! flag_no_inline && TREE_STATIC (decl))
7335 {
7336 if (extract_init (decl, init))
7337 return NULL_TREE;
7338 }
7339
2ee887f2 7340#if ! defined (ASM_OUTPUT_BSS) && ! defined (ASM_OUTPUT_ALIGNED_BSS)
a9aedbc2 7341 if (toplevel_bindings_p () && ! DECL_COMMON (decl))
6060a796
MS
7342 DECL_INITIAL (decl) = build (CONSTRUCTOR, TREE_TYPE (decl), NULL_TREE,
7343 NULL_TREE);
7344 else
2ee887f2 7345#endif
6060a796 7346 DECL_INITIAL (decl) = error_mark_node;
28cbf42c
MS
7347
7348 return init;
6060a796
MS
7349}
7350
91063b51
MM
7351/* Issue an error message if DECL is an uninitialized const variable. */
7352
7353static void
7354check_for_uninitialized_const_var (decl)
7355 tree decl;
7356{
7357 tree type = TREE_TYPE (decl);
7358
7359 /* ``Unless explicitly declared extern, a const object does not have
7360 external linkage and must be initialized. ($8.4; $12.1)'' ARM
7361 7.1.6 */
7362 if (TREE_CODE (decl) == VAR_DECL
7363 && TREE_CODE (type) != REFERENCE_TYPE
7364 && CP_TYPE_CONST_P (type)
7365 && !TYPE_NEEDS_CONSTRUCTING (type)
7366 && !DECL_INITIAL (decl))
7367 cp_error ("uninitialized const `%D'", decl);
7368}
7369
8d08fdba
MS
7370/* Finish processing of a declaration;
7371 install its line number and initial value.
7372 If the length of an array type is not known before,
7373 it must be determined now, from the initial value, or it is an error.
7374
7375 Call `pop_obstacks' iff NEED_POP is nonzero.
7376
82580166 7377 For C++, `cp_finish_decl' must be fairly evasive: it must keep initializers
8d08fdba
MS
7378 for aggregates that have constructors alive on the permanent obstack,
7379 so that the global initializing functions can be written at the end.
7380
7381 INIT0 holds the value of an initializer that should be allowed to escape
7382 the normal rules.
7383
6060a796
MS
7384 FLAGS is LOOKUP_ONLYCONVERTING is the = init syntax was used, else 0
7385 if the (init) syntax was used.
7386
8d08fdba 7387 For functions that take default parameters, DECL points to its
82580166 7388 "maximal" instantiation. `cp_finish_decl' must then also declared its
8d08fdba
MS
7389 subsequently lower and lower forms of instantiation, checking for
7390 ambiguity as it goes. This can be sped up later. */
7391
7392void
82580166 7393cp_finish_decl (decl, init, asmspec_tree, need_pop, flags)
8d08fdba
MS
7394 tree decl, init;
7395 tree asmspec_tree;
7396 int need_pop;
6060a796 7397 int flags;
8d08fdba
MS
7398{
7399 register tree type;
a703fb38 7400 tree cleanup = NULL_TREE, ttype = NULL_TREE;
8d08fdba
MS
7401 int was_incomplete;
7402 int temporary = allocation_temporary_p ();
9c0758dd 7403 const char *asmspec = NULL;
8d08fdba 7404 int was_readonly = 0;
0c4b14c4 7405 int already_used = 0;
1c609c4c 7406 tree core_type;
8d08fdba
MS
7407
7408 /* If this is 0, then we did not change obstacks. */
7409 if (! decl)
7410 {
7411 if (init)
8251199e 7412 error ("assignment (not initialization) in declaration");
8d08fdba
MS
7413 return;
7414 }
7415
a4443a08 7416 /* If a name was specified, get the string. */
8d08fdba 7417 if (asmspec_tree)
8d08fdba 7418 asmspec = TREE_STRING_POINTER (asmspec_tree);
8d08fdba 7419
2c73f9f5
ML
7420 if (init && TREE_CODE (init) == NAMESPACE_DECL)
7421 {
8251199e 7422 cp_error ("Cannot initialize `%D' to namespace `%D'",
2c73f9f5
ML
7423 decl, init);
7424 init = NULL_TREE;
7425 }
7426
6ba89f8e
MM
7427 if (current_class_type
7428 && DECL_REAL_CONTEXT (decl) == current_class_type
7429 && TYPE_BEING_DEFINED (current_class_type)
7430 && (DECL_INITIAL (decl) || init))
7431 DECL_DEFINED_IN_CLASS_P (decl) = 1;
7432
9a68c51f
JM
7433 if (TREE_CODE (decl) == VAR_DECL
7434 && DECL_CONTEXT (decl)
7435 && TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL
7436 && DECL_CONTEXT (decl) != current_namespace
7437 && init)
7438 {
7439 /* Leave the namespace of the object. */
7440 pop_decl_namespace ();
7441 }
7442
8d08fdba
MS
7443 /* If the type of the thing we are declaring either has
7444 a constructor, or has a virtual function table pointer,
7445 AND its initialization was accepted by `start_decl',
7446 then we stayed on the permanent obstack through the
7447 declaration, otherwise, changed obstacks as GCC would. */
7448
7449 type = TREE_TYPE (decl);
7450
f376e137 7451 if (type == error_mark_node)
eac293a1 7452 {
a9aedbc2 7453 if (toplevel_bindings_p () && temporary)
eac293a1
MS
7454 end_temporary_allocation ();
7455
7456 return;
7457 }
f376e137 7458
a7a7710d
NS
7459 if (TYPE_HAS_MUTABLE_P (type))
7460 TREE_READONLY (decl) = 0;
7461
5156628f 7462 if (processing_template_decl)
5566b478
MS
7463 {
7464 if (init && DECL_INITIAL (decl))
7465 DECL_INITIAL (decl) = init;
faf5394a 7466 if (minimal_parse_mode && ! DECL_ARTIFICIAL (decl))
5566b478
MS
7467 {
7468 tree stmt = DECL_VINDEX (decl);
e1467ff2 7469 /* If the decl is declaring a member of a local class (in a
8857f91e
MM
7470 template function), the DECL_VINDEX will either be NULL,
7471 or it will be an actual virtual function index, not a
7472 DECL_STMT. */
7473 if (stmt != NULL_TREE && TREE_CODE (stmt) == DECL_STMT)
75650646
MM
7474 {
7475 DECL_VINDEX (decl) = NULL_TREE;
7476 TREE_OPERAND (stmt, 2) = copy_to_permanent (init);
7477 add_tree (stmt);
7478 }
5566b478 7479 }
8d08fdba 7480
5566b478
MS
7481 goto finish_end0;
7482 }
3e41d13b 7483
8d08fdba
MS
7484 /* Take care of TYPE_DECLs up front. */
7485 if (TREE_CODE (decl) == TYPE_DECL)
7486 {
7487 if (init && DECL_INITIAL (decl))
7488 {
7489 /* typedef foo = bar; store the type of bar as the type of foo. */
7490 TREE_TYPE (decl) = type = TREE_TYPE (init);
7491 DECL_INITIAL (decl) = init = NULL_TREE;
7492 }
a0a33927
MS
7493 if (type != error_mark_node
7494 && IS_AGGR_TYPE (type) && DECL_NAME (decl))
8d08fdba
MS
7495 {
7496 if (TREE_TYPE (DECL_NAME (decl)) && TREE_TYPE (decl) != type)
8251199e 7497 cp_warning ("shadowing previous type declaration of `%#D'", decl);
8d08fdba
MS
7498 set_identifier_type_value (DECL_NAME (decl), type);
7499 CLASSTYPE_GOT_SEMICOLON (type) = 1;
7500 }
7501 GNU_xref_decl (current_function_decl, decl);
cffa8729
MS
7502
7503 /* If we have installed this as the canonical typedef for this
7504 type, and that type has not been defined yet, delay emitting
956d6950 7505 the debug information for it, as we will emit it later. */
d2e5ee5c 7506 if (TYPE_MAIN_DECL (TREE_TYPE (decl)) == decl
cffa8729
MS
7507 && TYPE_SIZE (TREE_TYPE (decl)) == NULL_TREE)
7508 TYPE_DECL_SUPPRESS_DEBUG (decl) = 1;
7509
8d08fdba 7510 rest_of_decl_compilation (decl, NULL_PTR,
5566b478 7511 DECL_CONTEXT (decl) == NULL_TREE, at_eof);
8d08fdba
MS
7512 goto finish_end;
7513 }
3e41d13b 7514
8d08fdba 7515 if (TREE_CODE (decl) != FUNCTION_DECL)
3e41d13b 7516 ttype = target_type (type);
8d08fdba
MS
7517
7518 if (! DECL_EXTERNAL (decl) && TREE_READONLY (decl)
7519 && TYPE_NEEDS_CONSTRUCTING (type))
7520 {
8d08fdba
MS
7521 /* Currently, GNU C++ puts constants in text space, making them
7522 impossible to initialize. In the future, one would hope for
7523 an operating system which understood the difference between
7524 initialization and the running of a program. */
7525 was_readonly = 1;
7526 TREE_READONLY (decl) = 0;
7527 }
7528
7529 if (TREE_CODE (decl) == FIELD_DECL)
7530 {
7531 if (init && init != error_mark_node)
7532 my_friendly_assert (TREE_PERMANENT (init), 147);
7533
7534 if (asmspec)
7535 {
7536 /* This must override the asm specifier which was placed
6060a796 7537 by grokclassfn. Lay this out fresh. */
8d08fdba
MS
7538 DECL_RTL (TREE_TYPE (decl)) = NULL_RTX;
7539 DECL_ASSEMBLER_NAME (decl) = get_identifier (asmspec);
7540 make_decl_rtl (decl, asmspec, 0);
7541 }
7542 }
7543 /* If `start_decl' didn't like having an initialization, ignore it now. */
7544 else if (init != NULL_TREE && DECL_INITIAL (decl) == NULL_TREE)
7545 init = NULL_TREE;
7546 else if (DECL_EXTERNAL (decl))
7547 ;
7548 else if (TREE_CODE (type) == REFERENCE_TYPE
7549 || (TYPE_LANG_SPECIFIC (type) && IS_SIGNATURE_REFERENCE (type)))
7550 {
f376e137
MS
7551 if (TREE_STATIC (decl))
7552 make_decl_rtl (decl, NULL_PTR,
a9aedbc2 7553 toplevel_bindings_p ()
f376e137 7554 || pseudo_global_level_p ());
a703fb38 7555 grok_reference_init (decl, type, init);
8d08fdba
MS
7556 init = NULL_TREE;
7557 }
7558
3e41d13b
MM
7559 /* Check for certain invalid initializations. */
7560 if (init)
7561 {
7562 if (TYPE_SIZE (type) && !TREE_CONSTANT (TYPE_SIZE (type)))
7563 {
7564 cp_error ("variable-sized object `%D' may not be initialized", decl);
7565 init = NULL_TREE;
7566 }
7567 if (TREE_CODE (type) == ARRAY_TYPE
7568 && !TYPE_SIZE (complete_type (TREE_TYPE (type))))
7569 {
7570 cp_error ("elements of array `%#D' have incomplete type", decl);
7571 init = NULL_TREE;
7572 }
7573 }
7574
8d08fdba
MS
7575 GNU_xref_decl (current_function_decl, decl);
7576
1c609c4c
NS
7577 core_type = type;
7578 while (TREE_CODE (core_type) == ARRAY_TYPE)
7579 core_type = TREE_TYPE (core_type);
7580
a0a33927 7581 if (TREE_CODE (decl) == FIELD_DECL)
8d08fdba
MS
7582 ;
7583 else if (TREE_CODE (decl) == CONST_DECL)
7584 {
7585 my_friendly_assert (TREE_CODE (decl) != REFERENCE_TYPE, 148);
7586
7587 DECL_INITIAL (decl) = init;
7588
7589 /* This will keep us from needing to worry about our obstacks. */
7590 my_friendly_assert (init != NULL_TREE, 149);
7591 init = NULL_TREE;
7592 }
7593 else if (init)
7594 {
7595 if (TYPE_HAS_CONSTRUCTOR (type) || TYPE_NEEDS_CONSTRUCTING (type))
7596 {
7597 if (TREE_CODE (type) == ARRAY_TYPE)
7598 init = digest_init (type, init, (tree *) 0);
848b92e1
JM
7599 else if (TREE_CODE (init) == CONSTRUCTOR
7600 && TREE_HAS_CONSTRUCTOR (init))
8d08fdba 7601 {
f30432d7 7602 if (TYPE_NON_AGGREGATE_CLASS (type))
8d08fdba 7603 {
8251199e 7604 cp_error ("`%D' must be initialized by constructor, not by `{...}'",
a28e3c7f 7605 decl);
8d08fdba
MS
7606 init = error_mark_node;
7607 }
7608 else
7609 goto dont_use_constructor;
7610 }
8d08fdba
MS
7611 }
7612 else
7613 {
7614 dont_use_constructor:
7615 if (TREE_CODE (init) != TREE_VEC)
7616 init = store_init_value (decl, init);
8d08fdba 7617 }
28cbf42c
MS
7618
7619 if (init)
7620 /* We must hide the initializer so that expand_decl
7621 won't try to do something it does not understand. */
7622 init = obscure_complex_init (decl, init);
8d08fdba 7623 }
a0a33927
MS
7624 else if (DECL_EXTERNAL (decl))
7625 ;
8d08fdba
MS
7626 else if (TREE_CODE_CLASS (TREE_CODE (type)) == 't'
7627 && (IS_AGGR_TYPE (type) || TYPE_NEEDS_CONSTRUCTING (type)))
7628 {
1c609c4c 7629 if (! TYPE_NEEDS_CONSTRUCTING (core_type))
8d08fdba 7630 {
1c609c4c 7631 if (CLASSTYPE_READONLY_FIELDS_NEED_INIT (core_type))
8251199e 7632 cp_error ("structure `%D' with uninitialized const members", decl);
1c609c4c 7633 if (CLASSTYPE_REF_FIELDS_NEED_INIT (core_type))
8251199e 7634 cp_error ("structure `%D' with uninitialized reference members",
a28e3c7f 7635 decl);
8d08fdba
MS
7636 }
7637
91063b51 7638 check_for_uninitialized_const_var (decl);
8d08fdba 7639
6060a796
MS
7640 if (TYPE_SIZE (type) != NULL_TREE
7641 && TYPE_NEEDS_CONSTRUCTING (type))
28cbf42c 7642 init = obscure_complex_init (decl, NULL_TREE);
8d08fdba 7643
91063b51
MM
7644 }
7645 else
7646 check_for_uninitialized_const_var (decl);
7647
8d08fdba
MS
7648 /* For top-level declaration, the initial value was read in
7649 the temporary obstack. MAXINDEX, rtl, etc. to be made below
7650 must go in the permanent obstack; but don't discard the
7651 temporary data yet. */
7652
a9aedbc2 7653 if (toplevel_bindings_p () && temporary)
8d08fdba
MS
7654 end_temporary_allocation ();
7655
7656 /* Deduce size of array from initialization, if not already known. */
7657
7658 if (TREE_CODE (type) == ARRAY_TYPE
7659 && TYPE_DOMAIN (type) == NULL_TREE
7660 && TREE_CODE (decl) != TYPE_DECL)
7661 {
7662 int do_default
7663 = (TREE_STATIC (decl)
7664 /* Even if pedantic, an external linkage array
7665 may have incomplete type at first. */
7666 ? pedantic && ! DECL_EXTERNAL (decl)
7667 : !DECL_EXTERNAL (decl));
7668 tree initializer = init ? init : DECL_INITIAL (decl);
7669 int failure = complete_array_type (type, initializer, do_default);
7670
7671 if (failure == 1)
8251199e 7672 cp_error ("initializer fails to determine size of `%D'", decl);
8d08fdba
MS
7673
7674 if (failure == 2)
7675 {
7676 if (do_default)
8251199e 7677 cp_error ("array size missing in `%D'", decl);
8d08fdba
MS
7678 /* If a `static' var's size isn't known, make it extern as
7679 well as static, so it does not get allocated. If it's not
7680 `static', then don't mark it extern; finish_incomplete_decl
7681 will give it a default size and it will get allocated. */
7682 else if (!pedantic && TREE_STATIC (decl) && !TREE_PUBLIC (decl))
7683 DECL_EXTERNAL (decl) = 1;
7684 }
7685
7686 if (pedantic && TYPE_DOMAIN (type) != NULL_TREE
7687 && tree_int_cst_lt (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
7688 integer_zero_node))
8251199e 7689 cp_error ("zero-size array `%D'", decl);
8d08fdba
MS
7690
7691 layout_decl (decl, 0);
7692 }
7693
7694 if (TREE_CODE (decl) == VAR_DECL)
7695 {
7696 if (DECL_SIZE (decl) == NULL_TREE
ec255269 7697 && TYPE_SIZE (complete_type (TREE_TYPE (decl))) != NULL_TREE)
8d08fdba
MS
7698 layout_decl (decl, 0);
7699
7700 if (TREE_STATIC (decl) && DECL_SIZE (decl) == NULL_TREE)
7701 {
7702 /* A static variable with an incomplete type:
7703 that is an error if it is initialized.
7704 Otherwise, let it through, but if it is not `extern'
7705 then it may cause an error message later. */
7706 if (DECL_INITIAL (decl) != NULL_TREE)
8251199e 7707 cp_error ("storage size of `%D' isn't known", decl);
8d08fdba
MS
7708 init = NULL_TREE;
7709 }
7710 else if (!DECL_EXTERNAL (decl) && DECL_SIZE (decl) == NULL_TREE)
7711 {
7712 /* An automatic variable with an incomplete type: that is an error.
7713 Don't talk about array types here, since we took care of that
7714 message in grokdeclarator. */
8251199e 7715 cp_error ("storage size of `%D' isn't known", decl);
8d08fdba
MS
7716 TREE_TYPE (decl) = error_mark_node;
7717 }
7718 else if (!DECL_EXTERNAL (decl) && IS_AGGR_TYPE (ttype))
7719 /* Let debugger know it should output info for this type. */
7720 note_debug_info_needed (ttype);
7721
6eb3bb27 7722 if (TREE_STATIC (decl) && DECL_CLASS_SCOPE_P (decl))
d2e5ee5c
MS
7723 note_debug_info_needed (DECL_CONTEXT (decl));
7724
8d08fdba
MS
7725 if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl))
7726 && DECL_SIZE (decl) != NULL_TREE
7727 && ! TREE_CONSTANT (DECL_SIZE (decl)))
7728 {
7729 if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
7730 constant_expression_warning (DECL_SIZE (decl));
7731 else
8251199e 7732 cp_error ("storage size of `%D' isn't constant", decl);
8d08fdba
MS
7733 }
7734
c91a56d2
MS
7735 if (! DECL_EXTERNAL (decl) && TYPE_NEEDS_DESTRUCTOR (type)
7736 /* Cleanups for static variables are handled by `finish_file'. */
7737 && ! TREE_STATIC (decl))
8d08fdba
MS
7738 {
7739 int yes = suspend_momentary ();
2ee887f2 7740 cleanup = maybe_build_cleanup (decl);
8d08fdba
MS
7741 resume_momentary (yes);
7742 }
7743 }
7744 /* PARM_DECLs get cleanups, too. */
7745 else if (TREE_CODE (decl) == PARM_DECL && TYPE_NEEDS_DESTRUCTOR (type))
7746 {
7747 if (temporary)
7748 end_temporary_allocation ();
7749 cleanup = maybe_build_cleanup (decl);
7750 if (temporary)
7751 resume_temporary_allocation ();
7752 }
7753
7754 /* Output the assembler code and/or RTL code for variables and functions,
7755 unless the type is an undefined structure or union.
7756 If not, it will get done when the type is completed. */
7757
5566b478
MS
7758 was_incomplete = (DECL_SIZE (decl) == NULL_TREE);
7759
8d08fdba
MS
7760 if (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == FUNCTION_DECL
7761 || TREE_CODE (decl) == RESULT_DECL)
7762 {
7763 /* ??? FIXME: What about nested classes? */
e1467ff2 7764 int toplev = toplevel_bindings_p () || pseudo_global_level_p ();
8d08fdba 7765 int was_temp
d22c8596 7766 = (TREE_STATIC (decl) && TYPE_NEEDS_DESTRUCTOR (type)
8d08fdba
MS
7767 && allocation_temporary_p ());
7768
7769 if (was_temp)
7770 end_temporary_allocation ();
7771
5f2c99c4
JM
7772 /* Static data in a function with comdat linkage also has comdat
7773 linkage. */
42976354
BK
7774 if (TREE_CODE (decl) == VAR_DECL
7775 && TREE_STATIC (decl)
5f2c99c4 7776 /* Don't mess with __FUNCTION__. */
aeb302bb 7777 && ! TREE_ASM_WRITTEN (decl)
42976354
BK
7778 && current_function_decl
7779 && DECL_CONTEXT (decl) == current_function_decl
5f2c99c4
JM
7780 && (DECL_THIS_INLINE (current_function_decl)
7781 || DECL_TEMPLATE_INSTANTIATION (current_function_decl))
893de33c 7782 && TREE_PUBLIC (current_function_decl))
42976354 7783 {
5f2c99c4
JM
7784 /* Rather than try to get this right with inlining, we suppress
7785 inlining of such functions. */
aeb302bb
JM
7786 current_function_cannot_inline
7787 = "function with static variable cannot be inline";
5f2c99c4
JM
7788
7789 /* If flag_weak, we don't need to mess with this, as we can just
7790 make the function weak, and let it refer to its unique local
7791 copy. This works because we don't allow the function to be
7792 inlined. */
7793 if (! flag_weak)
7794 {
7795 if (DECL_INTERFACE_KNOWN (current_function_decl))
7796 {
7797 TREE_PUBLIC (decl) = 1;
7798 DECL_EXTERNAL (decl) = DECL_EXTERNAL (current_function_decl);
7799 }
7800 else if (DECL_INITIAL (decl) == NULL_TREE
7801 || DECL_INITIAL (decl) == error_mark_node)
7802 {
7803 TREE_PUBLIC (decl) = 1;
7804 DECL_COMMON (decl) = 1;
7805 }
7806 /* else we lose. We can only do this if we can use common,
7807 which we can't if it has been initialized. */
7808
7809 if (TREE_PUBLIC (decl))
7810 DECL_ASSEMBLER_NAME (decl)
7811 = build_static_name (current_function_decl, DECL_NAME (decl));
7812 else if (! DECL_ARTIFICIAL (decl))
7813 {
7814 cp_warning_at ("sorry: semantics of inline function static data `%#D' are wrong (you'll wind up with multiple copies)", decl);
dee6497d 7815 cp_warning_at (" you can work around this by removing the initializer", decl);
5f2c99c4
JM
7816 }
7817 }
42976354
BK
7818 }
7819
77be6f82
JM
7820 else if (TREE_CODE (decl) == VAR_DECL
7821 && DECL_LANG_SPECIFIC (decl)
7822 && DECL_COMDAT (decl))
ea735e02
JM
7823 /* Set it up again; we might have set DECL_INITIAL since the
7824 last time. */
7825 comdat_linkage (decl);
77be6f82 7826
8d08fdba
MS
7827 if (TREE_CODE (decl) == VAR_DECL && DECL_VIRTUAL_P (decl))
7828 make_decl_rtl (decl, NULL_PTR, toplev);
7829 else if (TREE_CODE (decl) == VAR_DECL
7830 && TREE_READONLY (decl)
7831 && DECL_INITIAL (decl) != NULL_TREE
7832 && DECL_INITIAL (decl) != error_mark_node
a3203465 7833 && ! EMPTY_CONSTRUCTOR_P (DECL_INITIAL (decl)))
8d08fdba
MS
7834 {
7835 DECL_INITIAL (decl) = save_expr (DECL_INITIAL (decl));
7836
7837 if (asmspec)
7838 DECL_ASSEMBLER_NAME (decl) = get_identifier (asmspec);
7839
7840 if (! toplev
7841 && TREE_STATIC (decl)
7842 && ! TREE_SIDE_EFFECTS (decl)
7843 && ! TREE_PUBLIC (decl)
7844 && ! DECL_EXTERNAL (decl)
7845 && ! TYPE_NEEDS_DESTRUCTOR (type)
7846 && DECL_MODE (decl) != BLKmode)
7847 {
7848 /* If this variable is really a constant, then fill its DECL_RTL
7849 slot with something which won't take up storage.
7850 If something later should take its address, we can always give
7851 it legitimate RTL at that time. */
7852 DECL_RTL (decl) = gen_reg_rtx (DECL_MODE (decl));
7853 store_expr (DECL_INITIAL (decl), DECL_RTL (decl), 0);
7854 TREE_ASM_WRITTEN (decl) = 1;
7855 }
a0a33927 7856 else if (toplev && ! TREE_PUBLIC (decl))
8d08fdba 7857 {
8d08fdba 7858 /* If this is a static const, change its apparent linkage
db5ae43f 7859 if it belongs to a #pragma interface. */
a0a33927 7860 if (!interface_unknown)
8d08fdba
MS
7861 {
7862 TREE_PUBLIC (decl) = 1;
7863 DECL_EXTERNAL (decl) = interface_only;
7864 }
7865 make_decl_rtl (decl, asmspec, toplev);
7866 }
7867 else
5566b478 7868 rest_of_decl_compilation (decl, asmspec, toplev, at_eof);
8d08fdba
MS
7869 }
7870 else if (TREE_CODE (decl) == VAR_DECL
7871 && DECL_LANG_SPECIFIC (decl)
7872 && DECL_IN_AGGR_P (decl))
7873 {
7874 if (TREE_STATIC (decl))
7875 {
7876 if (init == NULL_TREE
7877#ifdef DEFAULT_STATIC_DEFS
7878 /* If this code is dead, then users must
7879 explicitly declare static member variables
7880 outside the class def'n as well. */
7881 && TYPE_NEEDS_CONSTRUCTING (type)
7882#endif
7883 )
7884 {
7885 DECL_EXTERNAL (decl) = 1;
7886 make_decl_rtl (decl, asmspec, 1);
7887 }
7888 else
5566b478 7889 rest_of_decl_compilation (decl, asmspec, toplev, at_eof);
8d08fdba
MS
7890 }
7891 else
7892 /* Just a constant field. Should not need any rtl. */
7893 goto finish_end0;
7894 }
7895 else
5566b478 7896 rest_of_decl_compilation (decl, asmspec, toplev, at_eof);
8d08fdba
MS
7897
7898 if (was_temp)
7899 resume_temporary_allocation ();
7900
a7a64a77
MM
7901 if (!abstract_virtuals_error (decl, core_type)
7902 && (TREE_CODE (type) == FUNCTION_TYPE
7903 || TREE_CODE (type) == METHOD_TYPE))
8d08fdba
MS
7904 abstract_virtuals_error (decl, TREE_TYPE (type));
7905
1c609c4c
NS
7906 if (TYPE_LANG_SPECIFIC (core_type) && IS_SIGNATURE (core_type))
7907 signature_error (decl, core_type);
8d08fdba
MS
7908 else if ((TREE_CODE (type) == FUNCTION_TYPE
7909 || TREE_CODE (type) == METHOD_TYPE)
7910 && TYPE_LANG_SPECIFIC (TREE_TYPE (type))
7911 && IS_SIGNATURE (TREE_TYPE (type)))
7912 signature_error (decl, TREE_TYPE (type));
7913
7914 if (TREE_CODE (decl) == FUNCTION_DECL)
faae18ab 7915 ;
67d743fe
MS
7916 else if (DECL_EXTERNAL (decl)
7917 && ! (DECL_LANG_SPECIFIC (decl)
7918 && DECL_NOT_REALLY_EXTERN (decl)))
5566b478
MS
7919 {
7920 if (init)
7921 DECL_INITIAL (decl) = init;
7922 }
8d08fdba
MS
7923 else if (TREE_STATIC (decl) && type != error_mark_node)
7924 {
7925 /* Cleanups for static variables are handled by `finish_file'. */
f30432d7
MS
7926 if (TYPE_NEEDS_CONSTRUCTING (type) || init != NULL_TREE
7927 || TYPE_NEEDS_DESTRUCTOR (type))
8d08fdba 7928 expand_static_init (decl, init);
8d08fdba
MS
7929 }
7930 else if (! toplev)
7931 {
7932 /* This is a declared decl which must live until the
7933 end of the binding contour. It may need a cleanup. */
7934
7935 /* Recompute the RTL of a local array now
7936 if it used to be an incomplete type. */
7937 if (was_incomplete && ! TREE_STATIC (decl))
7938 {
7939 /* If we used it already as memory, it must stay in memory. */
7940 TREE_ADDRESSABLE (decl) = TREE_USED (decl);
7941 /* If it's still incomplete now, no init will save it. */
7942 if (DECL_SIZE (decl) == NULL_TREE)
7943 DECL_INITIAL (decl) = NULL_TREE;
7944 expand_decl (decl);
7945 }
7946 else if (! TREE_ASM_WRITTEN (decl)
7947 && (TYPE_SIZE (type) != NULL_TREE
7948 || TREE_CODE (type) == ARRAY_TYPE))
7949 {
7950 /* Do this here, because we did not expand this decl's
7951 rtl in start_decl. */
7952 if (DECL_RTL (decl) == NULL_RTX)
7953 expand_decl (decl);
7954 else if (cleanup)
7955 {
8d2733ca
MS
7956 /* XXX: Why don't we use decl here? */
7957 /* Ans: Because it was already expanded? */
e349ee73 7958 if (! expand_decl_cleanup (NULL_TREE, cleanup))
8251199e 7959 cp_error ("parser lost in parsing declaration of `%D'",
8d2733ca 7960 decl);
8d08fdba
MS
7961 /* Cleanup used up here. */
7962 cleanup = NULL_TREE;
7963 }
7964 }
7965
2ee887f2
MS
7966 if (current_binding_level->is_for_scope)
7967 {
f181d4ae
MM
7968 struct binding_level *outer
7969 = current_binding_level->level_chain;
2ee887f2
MS
7970
7971 /* Check to see if the same name is already bound at
7972 the outer level, either because it was directly declared,
7973 or because a dead for-decl got preserved. In either case,
d22c8596 7974 the code would not have been valid under the ARM
2ee887f2
MS
7975 scope rules, so clear is_for_scope for the
7976 current_binding_level.
7977
7978 Otherwise, we need to preserve the temp slot for decl
e92cc029 7979 to last into the outer binding level. */
2ee887f2 7980
f181d4ae
MM
7981 tree outer_binding
7982 = TREE_CHAIN (IDENTIFIER_BINDING (DECL_NAME (decl)));
7983
7984 if (outer_binding && BINDING_LEVEL (outer_binding) == outer
7985 && (TREE_CODE (BINDING_VALUE (outer_binding))
7986 == VAR_DECL)
7987 && DECL_DEAD_FOR_LOCAL (BINDING_VALUE (outer_binding)))
2ee887f2 7988 {
f181d4ae
MM
7989 BINDING_VALUE (outer_binding)
7990 = DECL_SHADOWED_FOR_VAR (BINDING_VALUE (outer_binding));
7991 current_binding_level->is_for_scope = 0;
2ee887f2 7992 }
f181d4ae
MM
7993 else if (DECL_IN_MEMORY_P (decl))
7994 preserve_temp_slots (DECL_RTL (decl));
2ee887f2
MS
7995 }
7996
eb66be0e 7997 expand_start_target_temps ();
72b7eeff 7998
8d08fdba
MS
7999 if (DECL_SIZE (decl) && type != error_mark_node)
8000 {
8001 /* Compute and store the initial value. */
8002 expand_decl_init (decl);
0c4b14c4 8003 already_used = TREE_USED (decl) || TREE_USED (type);
8d08fdba
MS
8004
8005 if (init || TYPE_NEEDS_CONSTRUCTING (type))
8006 {
a28e3c7f
MS
8007 emit_line_note (DECL_SOURCE_FILE (decl),
8008 DECL_SOURCE_LINE (decl));
b370501f 8009 expand_aggr_init (decl, init, flags);
8d08fdba
MS
8010 }
8011
00595019
MS
8012 /* Set this to 0 so we can tell whether an aggregate which
8013 was initialized was ever used. Don't do this if it has a
8014 destructor, so we don't complain about the 'resource
8015 allocation is initialization' idiom. */
249555b0 8016 /* Now set attribute((unused)) on types so decls of
38e01259 8017 that type will be marked used. (see TREE_USED, above.)
249555b0
BK
8018 This avoids the warning problems this particular code
8019 tried to work around. */
0c4b14c4 8020
be99da77 8021 if (TYPE_NEEDS_CONSTRUCTING (type)
0c4b14c4 8022 && ! already_used
be99da77
MS
8023 && cleanup == NULL_TREE
8024 && DECL_NAME (decl))
8d08fdba 8025 TREE_USED (decl) = 0;
0c4b14c4
JM
8026
8027 if (already_used)
8028 TREE_USED (decl) = 1;
934c6b13 8029 }
eb66be0e 8030
934c6b13 8031 /* Cleanup any temporaries needed for the initial value. */
eb66be0e 8032 expand_end_target_temps ();
8d08fdba 8033
934c6b13
MS
8034 if (DECL_SIZE (decl) && type != error_mark_node)
8035 {
8d08fdba
MS
8036 /* Store the cleanup, if there was one. */
8037 if (cleanup)
8038 {
e349ee73 8039 if (! expand_decl_cleanup (decl, cleanup))
8251199e 8040 cp_error ("parser lost in parsing declaration of `%D'",
a28e3c7f 8041 decl);
8d08fdba
MS
8042 }
8043 }
8044 }
8045 finish_end0:
8046
8047 /* Undo call to `pushclass' that was done in `start_decl'
8048 due to initialization of qualified member variable.
8049 I.e., Foo::x = 10; */
8050 {
f30432d7 8051 tree context = DECL_REAL_CONTEXT (decl);
8d08fdba
MS
8052 if (context
8053 && TREE_CODE_CLASS (TREE_CODE (context)) == 't'
8054 && (TREE_CODE (decl) == VAR_DECL
8055 /* We also have a pushclass done that we need to undo here
8056 if we're at top level and declare a method. */
5566b478
MS
8057 || TREE_CODE (decl) == FUNCTION_DECL)
8058 /* If size hasn't been set, we're still defining it,
8059 and therefore inside the class body; don't pop
8060 the binding level.. */
8061 && TYPE_SIZE (context) != NULL_TREE
8062 && context == current_class_type)
6b400b21 8063 pop_nested_class ();
8d08fdba
MS
8064 }
8065 }
8066
8067 finish_end:
8068
39211cd5
MS
8069 /* If requested, warn about definitions of large data objects. */
8070
8071 if (warn_larger_than
5156628f 8072 && ! processing_template_decl
39211cd5
MS
8073 && (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL)
8074 && !DECL_EXTERNAL (decl))
8075 {
8076 register tree decl_size = DECL_SIZE (decl);
8077
8078 if (decl_size && TREE_CODE (decl_size) == INTEGER_CST)
8079 {
8080 unsigned units = TREE_INT_CST_LOW (decl_size) / BITS_PER_UNIT;
8081
8082 if (units > larger_than_size)
8083 warning_with_decl (decl, "size of `%s' is %u bytes", units);
8084 }
8085 }
8086
8d08fdba
MS
8087 if (need_pop)
8088 {
8089 /* Resume permanent allocation, if not within a function. */
8090 /* The corresponding push_obstacks_nochange is in start_decl,
8091 start_method, groktypename, and in grokfield. */
8092 pop_obstacks ();
8093 }
8094
8095 if (was_readonly)
8096 TREE_READONLY (decl) = 1;
8d08fdba
MS
8097}
8098
82580166 8099/* This is here for a midend callback from c-common.c */
e92cc029 8100
82580166
MS
8101void
8102finish_decl (decl, init, asmspec_tree)
8103 tree decl, init;
8104 tree asmspec_tree;
8105{
8106 cp_finish_decl (decl, init, asmspec_tree, 1, 0);
8107}
8108
8d08fdba
MS
8109void
8110expand_static_init (decl, init)
8111 tree decl;
8112 tree init;
8113{
8114 tree oldstatic = value_member (decl, static_aggregates);
a4443a08 8115
8d08fdba
MS
8116 if (oldstatic)
8117 {
8118 if (TREE_PURPOSE (oldstatic) && init != NULL_TREE)
8251199e 8119 cp_error ("multiple initializations given for `%D'", decl);
8d08fdba 8120 }
0aafb128 8121 else if (! toplevel_bindings_p ())
8d08fdba
MS
8122 {
8123 /* Emit code to perform this initialization but once. */
8124 tree temp;
8125
e92cc029 8126 /* Remember this information until end of file. */
8d08fdba
MS
8127 push_obstacks (&permanent_obstack, &permanent_obstack);
8128
2036a15c
MM
8129 /* Emit code to perform this initialization but once. This code
8130 looks like:
8131
8132 static int temp = 0;
8133 if (!temp) {
8134 // Do initialization.
8135 temp = 1;
8136 // Register variable for destruction at end of program.
8137 }
8138
8139 Note that the `temp' variable is only set to 1 *after* the
8140 initialization is complete. This ensures that an exception,
8141 thrown during the construction, will cause the variable to
8142 reinitialized when we pass through this code again, as per:
8143
8144 [stmt.dcl]
8145
8146 If the initialization exits by throwing an exception, the
8147 initialization is not complete, so it will be tried again
8148 the next time control enters the declaration.
8149
8150 In theory, this process should be thread-safe, too; multiple
8151 threads should not be able to initialize the variable more
8152 than once. We don't yet attempt to ensure thread-safety. */
8d08fdba
MS
8153 temp = get_temp_name (integer_type_node, 1);
8154 rest_of_decl_compilation (temp, NULL_PTR, 0, 0);
2036a15c
MM
8155
8156 /* Begin the conditional initialization. */
8d08fdba 8157 expand_start_cond (build_binary_op (EQ_EXPR, temp,
337c90cc 8158 integer_zero_node), 0);
eb66be0e 8159 expand_start_target_temps ();
72b7eeff 8160
2036a15c 8161 /* Do the initialization itself. */
28cbf42c 8162 if (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl))
f30432d7 8163 || (init && TREE_CODE (init) == TREE_LIST))
8d08fdba 8164 {
b370501f 8165 expand_aggr_init (decl, init, 0);
8d08fdba
MS
8166 do_pending_stack_adjust ();
8167 }
f30432d7 8168 else if (init)
8d08fdba 8169 expand_assignment (decl, init, 0, 0);
f30432d7 8170
2036a15c
MM
8171 /* Set TEMP to 1. */
8172 expand_assignment (temp, integer_one_node, 0, 0);
8173
8174 /* Cleanup any temporaries needed for the initial value. If
8175 destroying one of the temporaries causes an exception to be
8176 thrown, then the object itself has still been fully
8177 constructed. */
eb66be0e 8178 expand_end_target_temps ();
72b7eeff 8179
2036a15c
MM
8180 /* Use atexit to register a function for destroying this static
8181 variable. */
72b7eeff
MS
8182 if (TYPE_NEEDS_DESTRUCTOR (TREE_TYPE (decl)))
8183 {
8184 tree cleanup, fcall;
8185 static tree Atexit = 0;
0045e0bc
MM
8186 int saved_flag_access_control;
8187
72b7eeff
MS
8188 if (Atexit == 0)
8189 {
8190 tree atexit_fndecl, PFV, pfvlist;
e92cc029 8191 /* Remember this information until end of file. */
72b7eeff
MS
8192 push_obstacks (&permanent_obstack, &permanent_obstack);
8193 PFV = build_pointer_type (build_function_type
8194 (void_type_node, void_list_node));
8195
8196 pfvlist = tree_cons (NULL_TREE, PFV, void_list_node);
8197
8198 push_lang_context (lang_name_c);
f30c84c9
MM
8199 /* Note that we do not call pushdecl for this function;
8200 there's no reason that this declaration should be
8201 accessible to anyone. */
beb53fb8 8202 atexit_fndecl
f30c84c9
MM
8203 = define_function ("atexit",
8204 build_function_type (void_type_node,
8205 pfvlist),
8206 NOT_BUILT_IN,
8207 /*pfn=*/0,
8208 NULL_PTR);
a6ecf8b6 8209 mark_used (atexit_fndecl);
72b7eeff
MS
8210 Atexit = default_conversion (atexit_fndecl);
8211 pop_lang_context ();
8212 pop_obstacks ();
8213 }
8214
7cfac595
MM
8215 /* Call build_cleanup before we enter the anonymous function
8216 so that any access checks will be done relative to the
8217 current scope, rather than the scope of the anonymous
8218 function. */
0045e0bc
MM
8219 build_cleanup (decl);
8220
8221 /* Now start the function. */
72b7eeff 8222 cleanup = start_anon_func ();
0045e0bc
MM
8223
8224 /* Now, recompute the cleanup. It may contain SAVE_EXPRs
8225 that refer to the original function, rather than the
8226 anonymous one. That will make the back-end think that
8227 nested functions are in use, which causes confusion. */
8228 saved_flag_access_control = flag_access_control;
8229 flag_access_control = 0;
8230 fcall = build_cleanup (decl);
8231 flag_access_control = saved_flag_access_control;
8232
8233 /* Finish off the function. */
7cfac595 8234 expand_expr_stmt (fcall);
72b7eeff 8235 end_anon_func ();
0045e0bc
MM
8236
8237 /* Call atexit with the cleanup function. */
72b7eeff
MS
8238 mark_addressable (cleanup);
8239 cleanup = build_unary_op (ADDR_EXPR, cleanup, 0);
0045e0bc
MM
8240 fcall = build_function_call (Atexit,
8241 expr_tree_cons (NULL_TREE,
8242 cleanup,
8243 NULL_TREE));
72b7eeff
MS
8244 expand_expr_stmt (fcall);
8245 }
8246
8d08fdba 8247 expand_end_cond ();
e92cc029 8248 /* Resume old (possibly temporary) allocation. */
8d08fdba
MS
8249 pop_obstacks ();
8250 }
8251 else
8252 {
0aafb128
MM
8253 /* This code takes into account memory allocation policy of
8254 `start_decl'. Namely, if TYPE_NEEDS_CONSTRUCTING does not
8255 hold for this object, then we must make permanent the storage
8256 currently in the temporary obstack. */
8257 if (!TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl)))
8d08fdba
MS
8258 preserve_initializer ();
8259 static_aggregates = perm_tree_cons (init, decl, static_aggregates);
8260 }
8261}
8262\f
8263/* Make TYPE a complete type based on INITIAL_VALUE.
8264 Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
6ab5c740 8265 2 if there was no information (in which case assume 0 if DO_DEFAULT). */
8d08fdba
MS
8266
8267int
8268complete_array_type (type, initial_value, do_default)
8269 tree type, initial_value;
8270 int do_default;
8271{
8272 register tree maxindex = NULL_TREE;
8273 int value = 0;
6ab5c740
NS
8274
8275 /* Allocate on the same obstack as TYPE. */
8276 push_obstacks (TYPE_OBSTACK (type), TYPE_OBSTACK (type));
8277
8d08fdba
MS
8278 if (initial_value)
8279 {
8280 /* Note MAXINDEX is really the maximum index,
8281 one less than the size. */
8282 if (TREE_CODE (initial_value) == STRING_CST)
e1cd6e56
MS
8283 {
8284 int eltsize
8285 = int_size_in_bytes (TREE_TYPE (TREE_TYPE (initial_value)));
8286 maxindex = build_int_2 ((TREE_STRING_LENGTH (initial_value)
8287 / eltsize) - 1, 0);
8288 }
8d08fdba
MS
8289 else if (TREE_CODE (initial_value) == CONSTRUCTOR)
8290 {
e1cd6e56
MS
8291 tree elts = CONSTRUCTOR_ELTS (initial_value);
8292 maxindex = size_binop (MINUS_EXPR, integer_zero_node, size_one_node);
8293 for (; elts; elts = TREE_CHAIN (elts))
8294 {
8295 if (TREE_PURPOSE (elts))
8296 maxindex = TREE_PURPOSE (elts);
8297 else
8298 maxindex = size_binop (PLUS_EXPR, maxindex, size_one_node);
8299 }
8300 maxindex = copy_node (maxindex);
8d08fdba
MS
8301 }
8302 else
8303 {
8304 /* Make an error message unless that happened already. */
8305 if (initial_value != error_mark_node)
8306 value = 1;
0db982be
ML
8307 else
8308 initial_value = NULL_TREE;
8d08fdba
MS
8309
8310 /* Prevent further error messages. */
8311 maxindex = build_int_2 (0, 0);
8312 }
8313 }
8314
8315 if (!maxindex)
8316 {
8317 if (do_default)
8318 maxindex = build_int_2 (0, 0);
8319 value = 2;
8320 }
8321
8322 if (maxindex)
8323 {
51c184be 8324 tree itype;
6ab5c740
NS
8325 tree domain;
8326
8327 domain = build_index_type (maxindex);
8328 TYPE_DOMAIN (type) = domain;
51c184be 8329
dff6b454 8330 if (! TREE_TYPE (maxindex))
6ab5c740 8331 TREE_TYPE (maxindex) = domain;
51c184be
MS
8332 if (initial_value)
8333 itype = TREE_TYPE (initial_value);
8334 else
8335 itype = NULL;
8336 if (itype && !TYPE_DOMAIN (itype))
6ab5c740 8337 TYPE_DOMAIN (itype) = domain;
dff6b454
RK
8338 /* The type of the main variant should never be used for arrays
8339 of different sizes. It should only ever be completed with the
8340 size of the array. */
8341 if (! TYPE_DOMAIN (TYPE_MAIN_VARIANT (type)))
6ab5c740 8342 TYPE_DOMAIN (TYPE_MAIN_VARIANT (type)) = domain;
8d08fdba
MS
8343 }
8344
6ab5c740
NS
8345 pop_obstacks();
8346
8d08fdba
MS
8347 /* Lay out the type now that we can get the real answer. */
8348
8349 layout_type (type);
8350
8351 return value;
8352}
8353\f
8354/* Return zero if something is declared to be a member of type
8355 CTYPE when in the context of CUR_TYPE. STRING is the error
8356 message to print in that case. Otherwise, quietly return 1. */
e92cc029 8357
8d08fdba
MS
8358static int
8359member_function_or_else (ctype, cur_type, string)
8360 tree ctype, cur_type;
d8e178a0 8361 const char *string;
8d08fdba
MS
8362{
8363 if (ctype && ctype != cur_type)
8364 {
8365 error (string, TYPE_NAME_STRING (ctype));
8366 return 0;
8367 }
8368 return 1;
8369}
8370\f
8371/* Subroutine of `grokdeclarator'. */
8372
8373/* Generate errors possibly applicable for a given set of specifiers.
8374 This is for ARM $7.1.2. */
e92cc029 8375
8d08fdba
MS
8376static void
8377bad_specifiers (object, type, virtualp, quals, inlinep, friendp, raises)
8378 tree object;
d8e178a0 8379 const char *type;
8d08fdba
MS
8380 int virtualp, quals, friendp, raises, inlinep;
8381{
8382 if (virtualp)
8251199e 8383 cp_error ("`%D' declared as a `virtual' %s", object, type);
8d08fdba 8384 if (inlinep)
8251199e 8385 cp_error ("`%D' declared as an `inline' %s", object, type);
8d08fdba 8386 if (quals)
8251199e 8387 cp_error ("`const' and `volatile' function specifiers on `%D' invalid in %s declaration",
a28e3c7f 8388 object, type);
8d08fdba 8389 if (friendp)
f8e55f34 8390 cp_error_at ("`%D' declared as a friend", object);
f3e4d63c 8391 if (raises)
f8e55f34 8392 cp_error_at ("`%D' declared with an exception specification", object);
8d08fdba
MS
8393}
8394
8395/* CTYPE is class type, or null if non-class.
8396 TYPE is type this FUNCTION_DECL should have, either FUNCTION_TYPE
8397 or METHOD_TYPE.
8398 DECLARATOR is the function's name.
8399 VIRTUALP is truthvalue of whether the function is virtual or not.
8400 FLAGS are to be passed through to `grokclassfn'.
8401 QUALS are qualifiers indicating whether the function is `const'
8402 or `volatile'.
8403 RAISES is a list of exceptions that this function can raise.
8404 CHECK is 1 if we must find this method in CTYPE, 0 if we should
3ddfb0e6
MM
8405 not look, and -1 if we should not call `grokclassfn' at all.
8406
20496fa2 8407 Returns `NULL_TREE' if something goes wrong, after issuing
3ddfb0e6 8408 applicable error messages. */
e92cc029 8409
8d08fdba 8410static tree
386b8a85 8411grokfndecl (ctype, type, declarator, orig_declarator, virtualp, flags, quals,
7a8f9fa9 8412 raises, check, friendp, publicp, inlinep, funcdef_flag,
2c73f9f5 8413 template_count, in_namespace)
8d08fdba
MS
8414 tree ctype, type;
8415 tree declarator;
386b8a85 8416 tree orig_declarator;
8d08fdba
MS
8417 int virtualp;
8418 enum overload_flags flags;
7a8f9fa9 8419 tree quals, raises;
386b8a85 8420 int check, friendp, publicp, inlinep, funcdef_flag, template_count;
2c73f9f5 8421 tree in_namespace;
8d08fdba
MS
8422{
8423 tree cname, decl;
8424 int staticp = ctype && TREE_CODE (type) == FUNCTION_TYPE;
856216bb 8425 int has_default_arg = 0;
42976354 8426 tree t;
8d08fdba
MS
8427
8428 if (ctype)
8429 cname = TREE_CODE (TYPE_NAME (ctype)) == TYPE_DECL
8430 ? TYPE_IDENTIFIER (ctype) : TYPE_NAME (ctype);
8431 else
8432 cname = NULL_TREE;
8433
8434 if (raises)
8435 {
f30432d7 8436 type = build_exception_variant (type, raises);
8d08fdba 8437 }
c11b6f21 8438
8d08fdba 8439 decl = build_lang_decl (FUNCTION_DECL, declarator, type);
2c73f9f5 8440 /* Propagate volatile out from type to decl. */
8d08fdba 8441 if (TYPE_VOLATILE (type))
893de33c 8442 TREE_THIS_VOLATILE (decl) = 1;
8d08fdba 8443
79c4d4b7 8444 /* If this decl has namespace scope, set that up. */
2c73f9f5 8445 if (in_namespace)
b262d64c 8446 set_decl_namespace (decl, in_namespace, friendp);
79c4d4b7
JM
8447 else if (publicp && ! ctype)
8448 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
2c73f9f5 8449
0f8766b8
JM
8450 /* `main' and builtins have implicit 'C' linkage. */
8451 if ((MAIN_NAME_P (declarator)
8452 || (IDENTIFIER_LENGTH (declarator) > 10
8453 && IDENTIFIER_POINTER (declarator)[0] == '_'
8454 && IDENTIFIER_POINTER (declarator)[1] == '_'
8455 && strncmp (IDENTIFIER_POINTER (declarator)+2, "builtin_", 8) == 0))
8456 && current_lang_name == lang_name_cplusplus
94706a5c 8457 && ctype == NULL_TREE
79c4d4b7
JM
8458 /* NULL_TREE means global namespace. */
8459 && DECL_CONTEXT (decl) == NULL_TREE)
0f8766b8
JM
8460 DECL_LANGUAGE (decl) = lang_c;
8461
8d08fdba
MS
8462 /* Should probably propagate const out from type to decl I bet (mrs). */
8463 if (staticp)
8464 {
8465 DECL_STATIC_FUNCTION_P (decl) = 1;
8466 DECL_CONTEXT (decl) = ctype;
8d08fdba
MS
8467 }
8468
e76a2646
MS
8469 if (ctype)
8470 DECL_CLASS_CONTEXT (decl) = ctype;
8471
0f8766b8 8472 if (ctype == NULL_TREE && DECL_MAIN_P (decl))
faae18ab 8473 {
848b92e1
JM
8474 if (processing_template_decl)
8475 error ("cannot declare `main' to be a template");
faae18ab 8476 if (inlinep)
8251199e 8477 error ("cannot declare `main' to be inline");
faae18ab 8478 else if (! publicp)
8251199e 8479 error ("cannot declare `main' to be static");
faae18ab
MS
8480 inlinep = 0;
8481 publicp = 1;
8482 }
50a6dbd7 8483
59e76fc6
JM
8484 /* Members of anonymous types and local classes have no linkage; make
8485 them internal. */
8486 if (ctype && (ANON_AGGRNAME_P (TYPE_IDENTIFIER (ctype))
8487 || hack_decl_function_context (TYPE_MAIN_DECL (ctype))))
50a6dbd7
JM
8488 publicp = 0;
8489
8490 if (publicp)
8491 {
8492 /* [basic.link]: A name with no linkage (notably, the name of a class
8493 or enumeration declared in a local scope) shall not be used to
8494 declare an entity with linkage.
8495
8496 Only check this for public decls for now. */
8497 t = no_linkage_check (TREE_TYPE (decl));
8498 if (t)
8499 {
7f7c930e
JM
8500 if (ANON_AGGRNAME_P (TYPE_IDENTIFIER (t)))
8501 {
8502 if (DECL_LANGUAGE (decl) == lang_c)
8503 /* Allow this; it's pretty common in C. */;
8504 else
8505 cp_pedwarn ("non-local function `%#D' uses anonymous type",
8506 decl);
8507 }
50a6dbd7 8508 else
8251199e 8509 cp_pedwarn ("non-local function `%#D' uses local type `%T'",
cce2be43 8510 decl, t);
50a6dbd7
JM
8511 }
8512 }
8513
893de33c 8514 TREE_PUBLIC (decl) = publicp;
faae18ab 8515 if (! publicp)
893de33c
JM
8516 {
8517 DECL_INTERFACE_KNOWN (decl) = 1;
8518 DECL_NOT_REALLY_EXTERN (decl) = 1;
8519 }
faae18ab
MS
8520
8521 if (inlinep)
8522 DECL_THIS_INLINE (decl) = DECL_INLINE (decl) = 1;
8d08fdba
MS
8523
8524 DECL_EXTERNAL (decl) = 1;
8525 if (quals != NULL_TREE && TREE_CODE (type) == FUNCTION_TYPE)
8526 {
8251199e 8527 cp_error ("%smember function `%D' cannot have `%T' method qualifier",
8d08fdba
MS
8528 (ctype ? "static " : "non-"), decl, TREE_VALUE (quals));
8529 quals = NULL_TREE;
8530 }
8531
8532 if (IDENTIFIER_OPNAME_P (DECL_NAME (decl)))
8533 grok_op_properties (decl, virtualp, check < 0);
8534
e76a2646 8535 if (ctype && hack_decl_function_context (decl))
893de33c 8536 DECL_NO_STATIC_CHAIN (decl) = 1;
e76a2646 8537
42976354
BK
8538 for (t = TYPE_ARG_TYPES (TREE_TYPE (decl)); t; t = TREE_CHAIN (t))
8539 if (TREE_PURPOSE (t)
8540 && TREE_CODE (TREE_PURPOSE (t)) == DEFAULT_ARG)
8541 {
856216bb 8542 has_default_arg = 1;
42976354
BK
8543 break;
8544 }
8545
f9d94ea4
JM
8546 if (friendp
8547 && TREE_CODE (orig_declarator) == TEMPLATE_ID_EXPR)
8548 {
8549 if (funcdef_flag)
8251199e
JM
8550 cp_error
8551 ("defining explicit specialization `%D' in friend declaration",
f9d94ea4
JM
8552 orig_declarator);
8553 else
8554 {
7e2421f7
MM
8555 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
8556 {
8557 /* Something like `template <class T> friend void f<T>()'. */
8251199e 8558 cp_error ("template-id `%D' in declaration of primary template",
7e2421f7 8559 orig_declarator);
20496fa2 8560 return NULL_TREE;
7e2421f7
MM
8561 }
8562
856216bb 8563
f9d94ea4
JM
8564 /* A friend declaration of the form friend void f<>(). Record
8565 the information in the TEMPLATE_ID_EXPR. */
8566 SET_DECL_IMPLICIT_INSTANTIATION (decl);
8567 DECL_TEMPLATE_INFO (decl)
8568 = perm_tree_cons (TREE_OPERAND (orig_declarator, 0),
8569 TREE_OPERAND (orig_declarator, 1),
8570 NULL_TREE);
856216bb
MM
8571
8572 if (has_default_arg)
8573 {
8574 cp_error ("default arguments are not allowed in declaration of friend template specialization `%D'",
8575 decl);
8576 return NULL_TREE;
8577 }
8578
8579 if (inlinep)
8580 {
8581 cp_error ("`inline' is not allowed in declaration of friend template specialization `%D'",
8582 decl);
8583 return NULL_TREE;
8584 }
f9d94ea4 8585 }
f84b4be9 8586 }
386b8a85 8587
856216bb
MM
8588 if (has_default_arg)
8589 add_defarg_fn (decl);
8590
0f8766b8
JM
8591 /* Plain overloading: will not be grok'd by grokclassfn. */
8592 if (! ctype && ! processing_template_decl
8593 && DECL_LANGUAGE (decl) != lang_c
8594 && (! DECL_USE_TEMPLATE (decl) || name_mangling_version < 1))
8595 set_mangled_name_for_decl (decl);
8596
1eb0072d
JM
8597 if (funcdef_flag)
8598 /* Make the init_value nonzero so pushdecl knows this is not
8599 tentative. error_mark_node is replaced later with the BLOCK. */
8600 DECL_INITIAL (decl) = error_mark_node;
8601
75650646 8602 /* Caller will do the rest of this. */
8d08fdba
MS
8603 if (check < 0)
8604 return decl;
8605
5566b478 8606 if (check && funcdef_flag)
d2e5ee5c 8607 DECL_INITIAL (decl) = error_mark_node;
5566b478 8608
8d08fdba
MS
8609 if (flags == NO_SPECIAL && ctype && constructor_name (cname) == declarator)
8610 {
8611 tree tmp;
8612 /* Just handle constructors here. We could do this
8613 inside the following if stmt, but I think
8614 that the code is more legible by breaking this
8615 case out. See comments below for what each of
8616 the following calls is supposed to do. */
8617 DECL_CONSTRUCTOR_P (decl) = 1;
8618
b370501f 8619 grokclassfn (ctype, decl, flags, quals);
386b8a85 8620
e1467ff2
MM
8621 decl = check_explicit_specialization (orig_declarator, decl,
8622 template_count,
f84b4be9
JM
8623 2 * (funcdef_flag != 0) +
8624 4 * (friendp != 0));
6c30752f 8625 if (decl == error_mark_node)
20496fa2 8626 return NULL_TREE;
75650646 8627
b370501f 8628 if ((! TYPE_FOR_JAVA (ctype) || check_java_method (decl))
eff71ab0 8629 && check)
5566b478
MS
8630 {
8631 tmp = check_classfn (ctype, decl);
98c1c668
JM
8632
8633 if (tmp && TREE_CODE (tmp) == TEMPLATE_DECL)
8634 tmp = DECL_TEMPLATE_RESULT(tmp);
8635
e349ee73 8636 if (tmp && DECL_ARTIFICIAL (tmp))
8251199e 8637 cp_error ("definition of implicitly-declared `%D'", tmp);
5566b478
MS
8638 if (tmp && duplicate_decls (decl, tmp))
8639 return tmp;
8640 }
a0a33927 8641 if (! grok_ctor_properties (ctype, decl))
20496fa2 8642 return NULL_TREE;
8d08fdba
MS
8643 }
8644 else
8645 {
8646 tree tmp;
8647
8648 /* Function gets the ugly name, field gets the nice one.
8649 This call may change the type of the function (because
8650 of default parameters)! */
8651 if (ctype != NULL_TREE)
b370501f 8652 grokclassfn (ctype, decl, flags, quals);
8d08fdba 8653
e1467ff2
MM
8654 decl = check_explicit_specialization (orig_declarator, decl,
8655 template_count,
f84b4be9
JM
8656 2 * (funcdef_flag != 0) +
8657 4 * (friendp != 0));
6c30752f 8658 if (decl == error_mark_node)
20496fa2 8659 return NULL_TREE;
6c30752f 8660
eff71ab0 8661 if (ctype != NULL_TREE
b370501f 8662 && (! TYPE_FOR_JAVA (ctype) || check_java_method (decl))
eff71ab0 8663 && check)
5566b478
MS
8664 {
8665 tmp = check_classfn (ctype, decl);
98c1c668
JM
8666
8667 if (tmp && TREE_CODE (tmp) == TEMPLATE_DECL)
75650646 8668 tmp = DECL_TEMPLATE_RESULT (tmp);
98c1c668 8669
5566b478
MS
8670 if (tmp && DECL_STATIC_FUNCTION_P (tmp)
8671 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE)
8672 {
8673 /* Remove the `this' parm added by grokclassfn.
8674 XXX Isn't this done in start_function, too? */
8675 revert_static_member_fn (&decl, NULL, NULL);
8676 last_function_parms = TREE_CHAIN (last_function_parms);
8677 }
e349ee73 8678 if (tmp && DECL_ARTIFICIAL (tmp))
8251199e 8679 cp_error ("definition of implicitly-declared `%D'", tmp);
7834ab39
MS
8680 if (tmp)
8681 {
03017874
MM
8682 /* Attempt to merge the declarations. This can fail, in
8683 the case of some illegal specialization declarations. */
7834ab39 8684 if (!duplicate_decls (decl, tmp))
03017874
MM
8685 cp_error ("no `%#D' member function declared in class `%T'",
8686 decl, ctype);
7834ab39
MS
8687 return tmp;
8688 }
5566b478 8689 }
8d08fdba
MS
8690
8691 if (ctype == NULL_TREE || check)
8692 return decl;
8693
8d08fdba
MS
8694 if (virtualp)
8695 {
2ee887f2 8696 DECL_VIRTUAL_P (decl) = 1;
8d08fdba
MS
8697 if (DECL_VINDEX (decl) == NULL_TREE)
8698 DECL_VINDEX (decl) = error_mark_node;
8699 IDENTIFIER_VIRTUAL_P (DECL_NAME (decl)) = 1;
8d08fdba
MS
8700 }
8701 }
8702 return decl;
8703}
8704
8705static tree
2c73f9f5 8706grokvardecl (type, declarator, specbits_in, initialized, constp, in_namespace)
8d08fdba
MS
8707 tree type;
8708 tree declarator;
d2e5ee5c 8709 RID_BIT_TYPE *specbits_in;
8d08fdba 8710 int initialized;
a9aedbc2 8711 int constp;
2c73f9f5 8712 tree in_namespace;
8d08fdba
MS
8713{
8714 tree decl;
f7da6097
MS
8715 RID_BIT_TYPE specbits;
8716
8717 specbits = *specbits_in;
8d08fdba
MS
8718
8719 if (TREE_CODE (type) == OFFSET_TYPE)
8720 {
8721 /* If you declare a static member so that it
8722 can be initialized, the code will reach here. */
5b605f68
MS
8723 tree basetype = TYPE_OFFSET_BASETYPE (type);
8724 type = TREE_TYPE (type);
8725 decl = build_lang_field_decl (VAR_DECL, declarator, type);
8726 DECL_CONTEXT (decl) = basetype;
8727 DECL_CLASS_CONTEXT (decl) = basetype;
f376e137 8728 DECL_ASSEMBLER_NAME (decl) = build_static_name (basetype, declarator);
8d08fdba
MS
8729 }
8730 else
30394414 8731 {
79c4d4b7
JM
8732 tree context;
8733
8734 if (in_namespace)
8735 context = in_namespace;
8736 else if (namespace_bindings_p () || RIDBIT_SETP (RID_EXTERN, specbits))
8737 context = current_namespace;
820fcad8
JM
8738 else
8739 context = NULL_TREE;
79c4d4b7 8740
30394414 8741 decl = build_decl (VAR_DECL, declarator, complete_type (type));
79c4d4b7
JM
8742
8743 if (context)
b262d64c 8744 set_decl_namespace (decl, context, 0);
79c4d4b7
JM
8745
8746 context = DECL_CONTEXT (decl);
8747 if (declarator && context && current_lang_name != lang_name_c)
8748 DECL_ASSEMBLER_NAME (decl) = build_static_name (context, declarator);
30394414 8749 }
6060a796 8750
2c73f9f5 8751 if (in_namespace)
b262d64c 8752 set_decl_namespace (decl, in_namespace, 0);
2c73f9f5 8753
8d08fdba
MS
8754 if (RIDBIT_SETP (RID_EXTERN, specbits))
8755 {
8756 DECL_THIS_EXTERN (decl) = 1;
8757 DECL_EXTERNAL (decl) = !initialized;
8758 }
8759
8760 /* In class context, static means one per class,
8761 public access, and static storage. */
2b9dc906 8762 if (DECL_CLASS_SCOPE_P (decl))
8d08fdba
MS
8763 {
8764 TREE_PUBLIC (decl) = 1;
8765 TREE_STATIC (decl) = 1;
5b605f68 8766 DECL_EXTERNAL (decl) = 0;
8d08fdba
MS
8767 }
8768 /* At top level, either `static' or no s.c. makes a definition
8769 (perhaps tentative), and absence of `static' makes it public. */
a9aedbc2 8770 else if (toplevel_bindings_p ())
8d08fdba 8771 {
a9aedbc2 8772 TREE_PUBLIC (decl) = (RIDBIT_NOTSETP (RID_STATIC, specbits)
9fffd093 8773 && (DECL_THIS_EXTERN (decl) || ! constp));
8d08fdba
MS
8774 TREE_STATIC (decl) = ! DECL_EXTERNAL (decl);
8775 }
8776 /* Not at top level, only `static' makes a static definition. */
8777 else
8778 {
8779 TREE_STATIC (decl) = !! RIDBIT_SETP (RID_STATIC, specbits);
8780 TREE_PUBLIC (decl) = DECL_EXTERNAL (decl);
8781 }
50a6dbd7
JM
8782
8783 if (TREE_PUBLIC (decl))
8784 {
8785 /* [basic.link]: A name with no linkage (notably, the name of a class
8786 or enumeration declared in a local scope) shall not be used to
8787 declare an entity with linkage.
8788
8789 Only check this for public decls for now. */
8790 tree t = no_linkage_check (TREE_TYPE (decl));
8791 if (t)
8792 {
8793 if (ANON_AGGRNAME_P (TYPE_IDENTIFIER (t)))
8794 /* Ignore for now; `enum { foo } e' is pretty common. */;
8795 else
8251199e 8796 cp_pedwarn ("non-local variable `%#D' uses local type `%T'",
50a6dbd7
JM
8797 decl, t);
8798 }
8799 }
8800
8d08fdba
MS
8801 return decl;
8802}
8803
d8f8dca1
MM
8804/* Create and return a canonical pointer to member function type, for
8805 TYPE, which is a POINTER_TYPE to a METHOD_TYPE. */
8d08fdba
MS
8806
8807tree
8808build_ptrmemfunc_type (type)
8809 tree type;
8810{
8811 tree fields[4];
8812 tree t;
8813 tree u;
8814
8815 /* If a canonical type already exists for this type, use it. We use
8816 this method instead of type_hash_canon, because it only does a
8817 simple equality check on the list of field members. */
8818
8819 if ((t = TYPE_GET_PTRMEMFUNC_TYPE (type)))
8820 return t;
8821
8822 push_obstacks (TYPE_OBSTACK (type), TYPE_OBSTACK (type));
8823
8824 u = make_lang_type (UNION_TYPE);
7ddedda4 8825 SET_IS_AGGR_TYPE (u, 0);
8d08fdba 8826 fields[0] = build_lang_field_decl (FIELD_DECL, pfn_identifier, type);
a28e3c7f
MS
8827 fields[1] = build_lang_field_decl (FIELD_DECL, delta2_identifier,
8828 delta_type_node);
8d08fdba
MS
8829 finish_builtin_type (u, "__ptrmemfunc_type", fields, 1, ptr_type_node);
8830 TYPE_NAME (u) = NULL_TREE;
8831
8832 t = make_lang_type (RECORD_TYPE);
8833
2c73f9f5 8834 /* Let the front-end know this is a pointer to member function... */
db5ae43f 8835 TYPE_PTRMEMFUNC_FLAG (t) = 1;
2c73f9f5 8836 /* ... and not really an aggregate. */
7ddedda4 8837 SET_IS_AGGR_TYPE (t, 0);
8d08fdba 8838
a28e3c7f
MS
8839 fields[0] = build_lang_field_decl (FIELD_DECL, delta_identifier,
8840 delta_type_node);
8841 fields[1] = build_lang_field_decl (FIELD_DECL, index_identifier,
8842 delta_type_node);
8d08fdba
MS
8843 fields[2] = build_lang_field_decl (FIELD_DECL, pfn_or_delta2_identifier, u);
8844 finish_builtin_type (t, "__ptrmemfunc_type", fields, 2, ptr_type_node);
8845
8846 pop_obstacks ();
8847
8848 /* Zap out the name so that the back-end will give us the debugging
8849 information for this anonymous RECORD_TYPE. */
8850 TYPE_NAME (t) = NULL_TREE;
8851
8852 TYPE_SET_PTRMEMFUNC_TYPE (type, t);
8853
e92cc029 8854 /* Seems to be wanted. */
8d08fdba
MS
8855 CLASSTYPE_GOT_SEMICOLON (t) = 1;
8856 return t;
8857}
8858
b17e2870
JM
8859/* DECL is a VAR_DECL defined in-class, whose TYPE is also given.
8860 Check to see that the definition is valid. Issue appropriate error
8861 messages. Return 1 if the definition is particularly bad, or 0
8862 otherwise. */
8863
8864int
8865check_static_variable_definition (decl, type)
8866 tree decl;
8867 tree type;
8868{
8869 /* Motion 10 at San Diego: If a static const integral data member is
8870 initialized with an integral constant expression, the initializer
8871 may appear either in the declaration (within the class), or in
8872 the definition, but not both. If it appears in the class, the
8873 member is a member constant. The file-scope definition is always
8874 required. */
8875 if (CLASS_TYPE_P (type) || TREE_CODE (type) == REFERENCE_TYPE)
8876 {
8877 cp_error ("in-class initialization of static data member of non-integral type `%T'",
8878 type);
8879 /* If we just return the declaration, crashes will sometimes
8880 occur. We therefore return void_type_node, as if this was a
8881 friend declaration, to cause callers to completely ignore
8882 this declaration. */
8883 return 1;
8884 }
8885 else if (!CP_TYPE_CONST_P (type))
8886 cp_error ("ANSI C++ forbids in-class initialization of non-const static member `%D'",
8887 decl);
8888 else if (pedantic && !INTEGRAL_TYPE_P (type))
8889 cp_pedwarn ("ANSI C++ forbids initialization of member constant `%D' of non-integral type `%T'", decl, type);
8890
8891 return 0;
8892}
8893
8d08fdba
MS
8894/* Given declspecs and a declarator,
8895 determine the name and type of the object declared
8896 and construct a ..._DECL node for it.
8897 (In one case we can return a ..._TYPE node instead.
8898 For invalid input we sometimes return 0.)
8899
8900 DECLSPECS is a chain of tree_list nodes whose value fields
8901 are the storage classes and type specifiers.
8902
8903 DECL_CONTEXT says which syntactic context this declaration is in:
8904 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
8905 FUNCDEF for a function definition. Like NORMAL but a few different
8906 error messages in each case. Return value may be zero meaning
8907 this definition is too screwy to try to parse.
8908 MEMFUNCDEF for a function definition. Like FUNCDEF but prepares to
8909 handle member functions (which have FIELD context).
8910 Return value may be zero meaning this definition is too screwy to
8911 try to parse.
8912 PARM for a parameter declaration (either within a function prototype
8913 or before a function body). Make a PARM_DECL, or return void_type_node.
db5ae43f 8914 CATCHPARM for a parameter declaration before a catch clause.
8d08fdba
MS
8915 TYPENAME if for a typename (in a cast or sizeof).
8916 Don't make a DECL node; just return the ..._TYPE node.
8917 FIELD for a struct or union field; make a FIELD_DECL.
8918 BITFIELD for a field with specified width.
8919 INITIALIZED is 1 if the decl has an initializer.
8920
b17e2870
JM
8921 ATTRLIST is a TREE_LIST node with prefix attributes in TREE_VALUE and
8922 normal attributes in TREE_PURPOSE, or NULL_TREE.
8923
8d08fdba
MS
8924 In the TYPENAME case, DECLARATOR is really an absolute declarator.
8925 It may also be so in the PARM case, for a prototype where the
8926 argument type is specified but not the name.
8927
8928 This function is where the complicated C meanings of `static'
8929 and `extern' are interpreted.
8930
8931 For C++, if there is any monkey business to do, the function which
8932 calls this one must do it, i.e., prepending instance variables,
8933 renaming overloaded function names, etc.
8934
8935 Note that for this C++, it is an error to define a method within a class
8936 which does not belong to that class.
8937
8938 Except in the case where SCOPE_REFs are implicitly known (such as
8939 methods within a class being redundantly qualified),
8940 declarations which involve SCOPE_REFs are returned as SCOPE_REFs
8941 (class_name::decl_name). The caller must also deal with this.
8942
8943 If a constructor or destructor is seen, and the context is FIELD,
8944 then the type gains the attribute TREE_HAS_x. If such a declaration
8945 is erroneous, NULL_TREE is returned.
8946
8947 QUALS is used only for FUNCDEF and MEMFUNCDEF cases. For a member
8948 function, these are the qualifiers to give to the `this' pointer.
8949
8950 May return void_type_node if the declarator turned out to be a friend.
8951 See grokfield for details. */
8952
8953enum return_types { return_normal, return_ctor, return_dtor, return_conversion };
8954
8955tree
c11b6f21 8956grokdeclarator (declarator, declspecs, decl_context, initialized, attrlist)
8d08fdba
MS
8957 tree declspecs;
8958 tree declarator;
8959 enum decl_context decl_context;
8960 int initialized;
c11b6f21 8961 tree attrlist;
8d08fdba
MS
8962{
8963 RID_BIT_TYPE specbits;
8964 int nclasses = 0;
8965 tree spec;
8966 tree type = NULL_TREE;
8967 int longlong = 0;
8968 int constp;
91063b51 8969 int restrictp;
8d08fdba 8970 int volatilep;
91063b51 8971 int type_quals;
db5ae43f 8972 int virtualp, explicitp, friendp, inlinep, staticp;
8d08fdba
MS
8973 int explicit_int = 0;
8974 int explicit_char = 0;
37c46b43 8975 int defaulted_int = 0;
8d08fdba
MS
8976 int opaque_typedef = 0;
8977 tree typedef_decl = NULL_TREE;
9c0758dd 8978 const char *name;
8d08fdba
MS
8979 tree typedef_type = NULL_TREE;
8980 int funcdef_flag = 0;
8981 enum tree_code innermost_code = ERROR_MARK;
8982 int bitfield = 0;
6125f3be
DE
8983#if 0
8984 /* See the code below that used this. */
f6abb50a 8985 tree decl_machine_attr = NULL_TREE;
6125f3be 8986#endif
8d08fdba
MS
8987 /* Set this to error_mark_node for FIELD_DECLs we could not handle properly.
8988 All FIELD_DECLs we build here have `init' put into their DECL_INITIAL. */
8989 tree init = NULL_TREE;
8990
8991 /* Keep track of what sort of function is being processed
8992 so that we can warn about default return values, or explicit
8993 return values which do not match prescribed defaults. */
8994 enum return_types return_type = return_normal;
8995
8996 tree dname = NULL_TREE;
8997 tree ctype = current_class_type;
8998 tree ctor_return_type = NULL_TREE;
8999 enum overload_flags flags = NO_SPECIAL;
8d08fdba 9000 tree quals = NULL_TREE;
c11b6f21 9001 tree raises = NULL_TREE;
386b8a85 9002 int template_count = 0;
2c73f9f5 9003 tree in_namespace = NULL_TREE;
1eab9b56
JM
9004 tree inner_attrs;
9005 int ignore_attrs;
8d08fdba
MS
9006
9007 RIDBIT_RESET_ALL (specbits);
9008 if (decl_context == FUNCDEF)
9009 funcdef_flag = 1, decl_context = NORMAL;
9010 else if (decl_context == MEMFUNCDEF)
9011 funcdef_flag = -1, decl_context = FIELD;
9012 else if (decl_context == BITFIELD)
9013 bitfield = 1, decl_context = FIELD;
9014
8d08fdba
MS
9015 /* Look inside a declarator for the name being declared
9016 and get it as a string, for an error message. */
9017 {
be99da77
MS
9018 tree *next = &declarator;
9019 register tree decl;
8d08fdba
MS
9020 name = NULL;
9021
be99da77
MS
9022 while (next && *next)
9023 {
9024 decl = *next;
9025 switch (TREE_CODE (decl))
8d08fdba 9026 {
52fbc847
JM
9027 case TREE_LIST:
9028 /* For attributes. */
9029 next = &TREE_VALUE (decl);
9030 break;
9031
be99da77
MS
9032 case COND_EXPR:
9033 ctype = NULL_TREE;
9034 next = &TREE_OPERAND (decl, 0);
9035 break;
8d08fdba 9036
2c73f9f5 9037 case BIT_NOT_EXPR: /* For C++ destructors! */
8d08fdba 9038 {
be99da77
MS
9039 tree name = TREE_OPERAND (decl, 0);
9040 tree rename = NULL_TREE;
9041
9042 my_friendly_assert (flags == NO_SPECIAL, 152);
9043 flags = DTOR_FLAG;
9044 return_type = return_dtor;
5566b478
MS
9045 if (TREE_CODE (name) == TYPE_DECL)
9046 TREE_OPERAND (decl, 0) = name = constructor_name (name);
be99da77
MS
9047 my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, 153);
9048 if (ctype == NULL_TREE)
9049 {
9050 if (current_class_type == NULL_TREE)
9051 {
8251199e 9052 error ("destructors must be member functions");
be99da77
MS
9053 flags = NO_SPECIAL;
9054 }
9055 else
9056 {
9057 tree t = constructor_name (current_class_name);
9058 if (t != name)
9059 rename = t;
9060 }
9061 }
8d08fdba 9062 else
be99da77
MS
9063 {
9064 tree t = constructor_name (ctype);
9065 if (t != name)
9066 rename = t;
9067 }
51c184be 9068
be99da77 9069 if (rename)
39211cd5 9070 {
8251199e 9071 cp_error ("destructor `%T' must match class name `%T'",
5566b478 9072 name, rename);
be99da77 9073 TREE_OPERAND (decl, 0) = rename;
39211cd5 9074 }
be99da77 9075 next = &name;
51c184be 9076 }
be99da77 9077 break;
8d08fdba 9078
be99da77 9079 case ADDR_EXPR: /* C++ reference declaration */
2c73f9f5 9080 /* Fall through. */
be99da77
MS
9081 case ARRAY_REF:
9082 case INDIRECT_REF:
9083 ctype = NULL_TREE;
9084 innermost_code = TREE_CODE (decl);
9085 next = &TREE_OPERAND (decl, 0);
9086 break;
8d08fdba 9087
be99da77
MS
9088 case CALL_EXPR:
9089 if (parmlist_is_exprlist (TREE_OPERAND (decl, 1)))
8d08fdba 9090 {
b17e2870
JM
9091 /* This is actually a variable declaration using
9092 constructor syntax. We need to call start_decl and
9093 cp_finish_decl so we can get the variable
9094 initialized... */
9095
9096 tree attributes, prefix_attributes;
be99da77
MS
9097
9098 *next = TREE_OPERAND (decl, 0);
9099 init = TREE_OPERAND (decl, 1);
9100
b17e2870
JM
9101 if (attrlist)
9102 {
9103 attributes = TREE_PURPOSE (attrlist);
9104 prefix_attributes = TREE_VALUE (attrlist);
9105 }
9106 else
9107 {
9108 attributes = NULL_TREE;
9109 prefix_attributes = NULL_TREE;
9110 }
9111
9112 decl = start_decl (declarator, declspecs, 1,
9113 attributes, prefix_attributes);
1eb0072d
JM
9114 if (decl)
9115 {
9116 /* Look for __unused__ attribute */
9117 if (TREE_USED (TREE_TYPE (decl)))
9118 TREE_USED (decl) = 1;
9119 finish_decl (decl, init, NULL_TREE);
9120 }
9121 else
9122 cp_error ("invalid declarator");
be99da77 9123 return 0;
8d08fdba 9124 }
be99da77
MS
9125 innermost_code = TREE_CODE (decl);
9126 if (decl_context == FIELD && ctype == NULL_TREE)
9127 ctype = current_class_type;
45537677 9128 if (ctype
c11b6f21 9129 && TREE_OPERAND (decl, 0)
45537677
MS
9130 && (TREE_CODE (TREE_OPERAND (decl, 0)) == TYPE_DECL
9131 && ((DECL_NAME (TREE_OPERAND (decl, 0))
9132 == constructor_name_full (ctype))
9133 || (DECL_NAME (TREE_OPERAND (decl, 0))
9134 == constructor_name (ctype)))))
be99da77
MS
9135 TREE_OPERAND (decl, 0) = constructor_name (ctype);
9136 next = &TREE_OPERAND (decl, 0);
9137 decl = *next;
9138 if (ctype != NULL_TREE
9139 && decl != NULL_TREE && flags != DTOR_FLAG
9140 && decl == constructor_name (ctype))
8d08fdba 9141 {
be99da77
MS
9142 return_type = return_ctor;
9143 ctor_return_type = ctype;
8d08fdba 9144 }
be99da77
MS
9145 ctype = NULL_TREE;
9146 break;
386b8a85
JM
9147
9148 case TEMPLATE_ID_EXPR:
9149 {
9150 tree fns = TREE_OPERAND (decl, 0);
9151
9152 if (TREE_CODE (fns) == LOOKUP_EXPR)
9153 fns = TREE_OPERAND (fns, 0);
9154
8f032717
MM
9155 dname = fns;
9156 if (TREE_CODE (dname) == COMPONENT_REF)
9157 dname = TREE_OPERAND (dname, 1);
9158 if (TREE_CODE (dname) != IDENTIFIER_NODE)
9159 {
9160 my_friendly_assert (is_overloaded_fn (dname),
9161 19990331);
9162 dname = DECL_NAME (get_first_fn (dname));
9163 }
386b8a85 9164 }
2c73f9f5 9165 /* Fall through. */
be99da77
MS
9166
9167 case IDENTIFIER_NODE:
386b8a85
JM
9168 if (TREE_CODE (decl) == IDENTIFIER_NODE)
9169 dname = decl;
9170
be99da77
MS
9171 next = 0;
9172
9173 if (is_rid (dname))
8d08fdba 9174 {
8251199e 9175 cp_error ("declarator-id missing; using reserved word `%D'",
be99da77
MS
9176 dname);
9177 name = IDENTIFIER_POINTER (dname);
8d08fdba 9178 }
be99da77 9179 if (! IDENTIFIER_OPNAME_P (dname)
956d6950 9180 /* GNU/Linux headers use '__op'. Arrgh. */
a703fb38 9181 || (IDENTIFIER_TYPENAME_P (dname) && ! TREE_TYPE (dname)))
be99da77 9182 name = IDENTIFIER_POINTER (dname);
8d08fdba
MS
9183 else
9184 {
be99da77 9185 if (IDENTIFIER_TYPENAME_P (dname))
8d08fdba 9186 {
be99da77
MS
9187 my_friendly_assert (flags == NO_SPECIAL, 154);
9188 flags = TYPENAME_FLAG;
9189 ctor_return_type = TREE_TYPE (dname);
9190 return_type = return_conversion;
8d08fdba 9191 }
be99da77 9192 name = operator_name_string (dname);
8d08fdba 9193 }
be99da77 9194 break;
8d08fdba 9195
be99da77
MS
9196 /* C++ extension */
9197 case SCOPE_REF:
9198 {
9199 /* Perform error checking, and decide on a ctype. */
9200 tree cname = TREE_OPERAND (decl, 0);
9201 if (cname == NULL_TREE)
9202 ctype = NULL_TREE;
2c73f9f5
ML
9203 else if (TREE_CODE (cname) == NAMESPACE_DECL)
9204 {
9205 ctype = NULL_TREE;
9206 in_namespace = TREE_OPERAND (decl, 0);
9207 TREE_OPERAND (decl, 0) = NULL_TREE;
9208 }
be99da77
MS
9209 else if (! is_aggr_type (cname, 1))
9210 TREE_OPERAND (decl, 0) = NULL_TREE;
9211 /* Must test TREE_OPERAND (decl, 1), in case user gives
9212 us `typedef (class::memfunc)(int); memfunc *memfuncptr;' */
9213 else if (TREE_OPERAND (decl, 1)
9214 && TREE_CODE (TREE_OPERAND (decl, 1)) == INDIRECT_REF)
9215 ctype = cname;
73b0fce8
KL
9216 else if (TREE_CODE (cname) == TEMPLATE_TYPE_PARM
9217 || TREE_CODE (cname) == TEMPLATE_TEMPLATE_PARM)
5566b478 9218 {
8251199e 9219 cp_error ("`%T::%D' is not a valid declarator", cname,
5566b478 9220 TREE_OPERAND (decl, 1));
8251199e 9221 cp_error (" perhaps you want `typename %T::%D' to make it a type",
5566b478
MS
9222 cname, TREE_OPERAND (decl, 1));
9223 return void_type_node;
9224 }
be99da77
MS
9225 else if (ctype == NULL_TREE)
9226 ctype = cname;
9227 else if (TREE_COMPLEXITY (decl) == current_class_depth)
9228 TREE_OPERAND (decl, 0) = ctype;
9229 else
9230 {
9231 if (! UNIQUELY_DERIVED_FROM_P (cname, ctype))
9232 {
8251199e 9233 cp_error ("type `%T' is not derived from type `%T'",
be99da77
MS
9234 cname, ctype);
9235 TREE_OPERAND (decl, 0) = NULL_TREE;
9236 }
9237 else
9238 ctype = cname;
9239 }
9240
c91a56d2
MS
9241 if (ctype && TREE_CODE (TREE_OPERAND (decl, 1)) == TYPE_DECL
9242 && ((DECL_NAME (TREE_OPERAND (decl, 1))
9243 == constructor_name_full (ctype))
9244 || (DECL_NAME (TREE_OPERAND (decl, 1))
9245 == constructor_name (ctype))))
be99da77
MS
9246 TREE_OPERAND (decl, 1) = constructor_name (ctype);
9247 next = &TREE_OPERAND (decl, 1);
9248 decl = *next;
9249 if (ctype)
9250 {
9251 if (TREE_CODE (decl) == IDENTIFIER_NODE
9252 && constructor_name (ctype) == decl)
9253 {
9254 return_type = return_ctor;
9255 ctor_return_type = ctype;
9256 }
9257 else if (TREE_CODE (decl) == BIT_NOT_EXPR
9258 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
9259 && (constructor_name (ctype) == TREE_OPERAND (decl, 0)
9260 || constructor_name_full (ctype) == TREE_OPERAND (decl, 0)))
9261 {
9262 return_type = return_dtor;
9263 ctor_return_type = ctype;
9264 flags = DTOR_FLAG;
9265 TREE_OPERAND (decl, 0) = constructor_name (ctype);
9266 next = &TREE_OPERAND (decl, 0);
9267 }
9268 }
9269 }
9270 break;
9271
9272 case ERROR_MARK:
9273 next = 0;
9274 break;
9275
45537677
MS
9276 case TYPE_DECL:
9277 /* Parse error puts this typespec where
9278 a declarator should go. */
8251199e 9279 cp_error ("`%T' specified as declarator-id", DECL_NAME (decl));
45537677 9280 if (TREE_TYPE (decl) == current_class_type)
8251199e 9281 cp_error (" perhaps you want `%T' for a constructor",
45537677
MS
9282 current_class_name);
9283 dname = DECL_NAME (decl);
9284 name = IDENTIFIER_POINTER (dname);
9285
e92cc029 9286 /* Avoid giving two errors for this. */
45537677
MS
9287 IDENTIFIER_CLASS_VALUE (dname) = NULL_TREE;
9288
9289 declspecs = temp_tree_cons (NULL_TREE, integer_type_node,
9290 declspecs);
9291 *next = dname;
9292 next = 0;
9293 break;
9294
be99da77 9295 default:
8251199e 9296 cp_compiler_error ("`%D' as declarator", decl);
be99da77 9297 return 0; /* We used to do a 155 abort here. */
8d08fdba 9298 }
be99da77 9299 }
8d08fdba
MS
9300 if (name == NULL)
9301 name = "type name";
9302 }
9303
9304 /* A function definition's declarator must have the form of
9305 a function declarator. */
9306
9307 if (funcdef_flag && innermost_code != CALL_EXPR)
9308 return 0;
9309
e1cd6e56
MS
9310 if (((dname && IDENTIFIER_OPNAME_P (dname)) || flags == TYPENAME_FLAG)
9311 && innermost_code != CALL_EXPR
9312 && ! (ctype && declspecs == NULL_TREE))
9313 {
8251199e 9314 cp_error ("declaration of `%D' as non-function", dname);
e1cd6e56
MS
9315 return void_type_node;
9316 }
9317
8d08fdba
MS
9318 /* Anything declared one level down from the top level
9319 must be one of the parameters of a function
9320 (because the body is at least two levels down). */
9321
9322 /* This heuristic cannot be applied to C++ nodes! Fixed, however,
9323 by not allowing C++ class definitions to specify their parameters
9324 with xdecls (must be spec.d in the parmlist).
9325
9326 Since we now wait to push a class scope until we are sure that
9327 we are in a legitimate method context, we must set oldcname
a9aedbc2
MS
9328 explicitly (since current_class_name is not yet alive).
9329
9330 We also want to avoid calling this a PARM if it is in a namespace. */
8d08fdba 9331
5566b478
MS
9332 if (decl_context == NORMAL && ! namespace_bindings_p ()
9333 && ! pseudo_global_level_p ())
a9aedbc2
MS
9334 {
9335 struct binding_level *b = current_binding_level;
9336 current_binding_level = b->level_chain;
9337 if (current_binding_level != 0 && toplevel_bindings_p ())
9338 decl_context = PARM;
9339 current_binding_level = b;
9340 }
8d08fdba
MS
9341
9342 /* Look through the decl specs and record which ones appear.
9343 Some typespecs are defined as built-in typenames.
9344 Others, the ones that are modifiers of other types,
9345 are represented by bits in SPECBITS: set the bits for
9346 the modifiers that appear. Storage class keywords are also in SPECBITS.
9347
9348 If there is a typedef name or a type, store the type in TYPE.
9349 This includes builtin typedefs such as `int'.
9350
9351 Set EXPLICIT_INT if the type is `int' or `char' and did not
9352 come from a user typedef.
9353
9354 Set LONGLONG if `long' is mentioned twice.
9355
9356 For C++, constructors and destructors have their own fast treatment. */
9357
9358 for (spec = declspecs; spec; spec = TREE_CHAIN (spec))
9359 {
9360 register int i;
9361 register tree id;
9362
9363 /* Certain parse errors slip through. For example,
9364 `int class;' is not caught by the parser. Try
9365 weakly to recover here. */
9366 if (TREE_CODE (spec) != TREE_LIST)
9367 return 0;
9368
9369 id = TREE_VALUE (spec);
9370
9371 if (TREE_CODE (id) == IDENTIFIER_NODE)
9372 {
a3203465
MS
9373 if (id == ridpointers[(int) RID_INT]
9374 || id == ridpointers[(int) RID_CHAR]
9375 || id == ridpointers[(int) RID_BOOL]
9376 || id == ridpointers[(int) RID_WCHAR])
8d08fdba
MS
9377 {
9378 if (type)
8ccc31eb
MS
9379 {
9380 if (id == ridpointers[(int) RID_BOOL])
8251199e 9381 error ("`bool' is now a keyword");
8ccc31eb 9382 else
8251199e 9383 cp_error ("extraneous `%T' ignored", id);
8ccc31eb 9384 }
8d08fdba
MS
9385 else
9386 {
a3203465
MS
9387 if (id == ridpointers[(int) RID_INT])
9388 explicit_int = 1;
9389 else if (id == ridpointers[(int) RID_CHAR])
9390 explicit_char = 1;
8d08fdba
MS
9391 type = TREE_TYPE (IDENTIFIER_GLOBAL_VALUE (id));
9392 }
9393 goto found;
9394 }
e92cc029 9395 /* C++ aggregate types. */
8d08fdba
MS
9396 if (IDENTIFIER_HAS_TYPE_VALUE (id))
9397 {
9398 if (type)
8251199e 9399 cp_error ("multiple declarations `%T' and `%T'", type, id);
8d08fdba
MS
9400 else
9401 type = IDENTIFIER_TYPE_VALUE (id);
9402 goto found;
9403 }
9404
f376e137 9405 for (i = (int) RID_FIRST_MODIFIER; i <= (int) RID_LAST_MODIFIER; i++)
8d08fdba
MS
9406 {
9407 if (ridpointers[i] == id)
9408 {
9409 if (i == (int) RID_LONG && RIDBIT_SETP (i, specbits))
9410 {
795add94 9411 if (pedantic && ! in_system_header && warn_long_long)
8251199e 9412 pedwarn ("ANSI C++ does not support `long long'");
9a3b49ac 9413 if (longlong)
8251199e 9414 error ("`long long long' is too long for GCC");
8d08fdba
MS
9415 else
9416 longlong = 1;
9417 }
9418 else if (RIDBIT_SETP (i, specbits))
8251199e 9419 pedwarn ("duplicate `%s'", IDENTIFIER_POINTER (id));
8d08fdba
MS
9420 RIDBIT_SET (i, specbits);
9421 goto found;
9422 }
9423 }
9424 }
e92cc029 9425 /* C++ aggregate types. */
73b0fce8 9426 else if (TREE_CODE (id) == TYPE_DECL || TREE_CODE (id) == TEMPLATE_DECL)
45537677
MS
9427 {
9428 if (type)
8251199e 9429 cp_error ("multiple declarations `%T' and `%T'", type,
45537677
MS
9430 TREE_TYPE (id));
9431 else
5566b478
MS
9432 {
9433 type = TREE_TYPE (id);
9434 TREE_VALUE (spec) = type;
9435 }
45537677
MS
9436 goto found;
9437 }
8d08fdba 9438 if (type)
8251199e 9439 error ("two or more data types in declaration of `%s'", name);
8d08fdba
MS
9440 else if (TREE_CODE (id) == IDENTIFIER_NODE)
9441 {
9442 register tree t = lookup_name (id, 1);
9443 if (!t || TREE_CODE (t) != TYPE_DECL)
8251199e 9444 error ("`%s' fails to be a typedef or built in type",
8d08fdba
MS
9445 IDENTIFIER_POINTER (id));
9446 else
9447 {
9448 type = TREE_TYPE (t);
6125f3be
DE
9449#if 0
9450 /* See the code below that used this. */
f6abb50a 9451 decl_machine_attr = DECL_MACHINE_ATTRIBUTES (id);
6125f3be 9452#endif
8d08fdba
MS
9453 typedef_decl = t;
9454 }
9455 }
bd6dd845 9456 else if (id != error_mark_node)
8d08fdba
MS
9457 /* Can't change CLASS nodes into RECORD nodes here! */
9458 type = id;
9459
9460 found: ;
9461 }
9462
9463 typedef_type = type;
9464
37c46b43 9465 /* No type at all: default to `int', and set DEFAULTED_INT
8d08fdba
MS
9466 because it was not a user-defined typedef.
9467 Except when we have a `typedef' inside a signature, in
9468 which case the type defaults to `unknown type' and is
9469 instantiated when assigning to a signature pointer or ref. */
9470
a3203465
MS
9471 if (type == NULL_TREE
9472 && (RIDBIT_SETP (RID_SIGNED, specbits)
9473 || RIDBIT_SETP (RID_UNSIGNED, specbits)
9474 || RIDBIT_SETP (RID_LONG, specbits)
9475 || RIDBIT_SETP (RID_SHORT, specbits)))
9476 {
9477 /* These imply 'int'. */
9478 type = integer_type_node;
37c46b43 9479 defaulted_int = 1;
a3203465
MS
9480 }
9481
8d08fdba
MS
9482 if (type == NULL_TREE)
9483 {
9484 explicit_int = -1;
9485 if (return_type == return_dtor)
9486 type = void_type_node;
9487 else if (return_type == return_ctor)
f30432d7 9488 type = build_pointer_type (ctor_return_type);
51c184be
MS
9489 else if (return_type == return_conversion)
9490 type = ctor_return_type;
8d08fdba
MS
9491 else if (current_class_type
9492 && IS_SIGNATURE (current_class_type)
fc378698 9493 && RIDBIT_SETP (RID_TYPEDEF, specbits)
8d08fdba
MS
9494 && (decl_context == FIELD || decl_context == NORMAL))
9495 {
9496 explicit_int = 0;
9497 opaque_typedef = 1;
9498 type = copy_node (opaque_type_node);
9499 }
9500 else
9501 {
41eff652
JM
9502 /* We handle `main' specially here, because 'main () { }' is so
9503 common. With no options, it is allowed. With -Wreturn-type,
9504 it is a warning. It is only an error with -pedantic-errors. */
9505 int is_main = (funcdef_flag
9506 && MAIN_NAME_P (dname)
9507 && ctype == NULL_TREE
9508 && in_namespace == NULL_TREE
9509 && current_namespace == global_namespace);
9510
1f901793
JM
9511 if (in_system_header)
9512 /* Allow it, sigh. */;
9513 else if (pedantic || ! is_main)
ff0cf464
JM
9514 cp_pedwarn ("ANSI C++ forbids declaration `%D' with no type",
9515 dname);
41eff652
JM
9516 else if (warn_return_type)
9517 cp_warning ("ANSI C++ forbids declaration `%D' with no type",
9518 dname);
9519
8d08fdba
MS
9520 type = integer_type_node;
9521 }
9522 }
9523 else if (return_type == return_dtor)
9524 {
8251199e 9525 error ("return type specification for destructor invalid");
8d08fdba
MS
9526 type = void_type_node;
9527 }
9528 else if (return_type == return_ctor)
9529 {
8251199e 9530 error ("return type specification for constructor invalid");
f30432d7 9531 type = build_pointer_type (ctor_return_type);
8d08fdba 9532 }
51c184be
MS
9533 else if (return_type == return_conversion)
9534 {
3bfdc719 9535 if (!same_type_p (type, ctor_return_type))
8251199e 9536 cp_error ("operator `%T' declared to return `%T'",
51c184be
MS
9537 ctor_return_type, type);
9538 else
8251199e 9539 cp_pedwarn ("return type specified for `operator %T'",
51c184be
MS
9540 ctor_return_type);
9541
9542 type = ctor_return_type;
9543 }
8d08fdba
MS
9544
9545 ctype = NULL_TREE;
9546
9547 /* Now process the modifiers that were specified
9548 and check for invalid combinations. */
9549
9550 /* Long double is a special combination. */
9551
9552 if (RIDBIT_SETP (RID_LONG, specbits)
9553 && TYPE_MAIN_VARIANT (type) == double_type_node)
9554 {
9555 RIDBIT_RESET (RID_LONG, specbits);
91063b51
MM
9556 type = build_qualified_type (long_double_type_node,
9557 CP_TYPE_QUALS (type));
8d08fdba
MS
9558 }
9559
9560 /* Check all other uses of type modifiers. */
9561
9562 if (RIDBIT_SETP (RID_UNSIGNED, specbits)
9563 || RIDBIT_SETP (RID_SIGNED, specbits)
9564 || RIDBIT_SETP (RID_LONG, specbits)
9565 || RIDBIT_SETP (RID_SHORT, specbits))
9566 {
9567 int ok = 0;
9568
9569 if (TREE_CODE (type) == REAL_TYPE)
8251199e 9570 error ("short, signed or unsigned invalid for `%s'", name);
b7484fbe 9571 else if (TREE_CODE (type) != INTEGER_TYPE)
8251199e 9572 error ("long, short, signed or unsigned invalid for `%s'", name);
8d08fdba
MS
9573 else if (RIDBIT_SETP (RID_LONG, specbits)
9574 && RIDBIT_SETP (RID_SHORT, specbits))
8251199e 9575 error ("long and short specified together for `%s'", name);
8d08fdba
MS
9576 else if ((RIDBIT_SETP (RID_LONG, specbits)
9577 || RIDBIT_SETP (RID_SHORT, specbits))
9578 && explicit_char)
8251199e 9579 error ("long or short specified with char for `%s'", name);
8d08fdba
MS
9580 else if ((RIDBIT_SETP (RID_LONG, specbits)
9581 || RIDBIT_SETP (RID_SHORT, specbits))
9582 && TREE_CODE (type) == REAL_TYPE)
8251199e 9583 error ("long or short specified with floating type for `%s'", name);
8d08fdba
MS
9584 else if (RIDBIT_SETP (RID_SIGNED, specbits)
9585 && RIDBIT_SETP (RID_UNSIGNED, specbits))
8251199e 9586 error ("signed and unsigned given together for `%s'", name);
8d08fdba
MS
9587 else
9588 {
9589 ok = 1;
37c46b43 9590 if (!explicit_int && !defaulted_int && !explicit_char && pedantic)
8d08fdba 9591 {
8251199e 9592 pedwarn ("long, short, signed or unsigned used invalidly for `%s'",
8d08fdba
MS
9593 name);
9594 if (flag_pedantic_errors)
9595 ok = 0;
9596 }
9597 }
9598
9599 /* Discard the type modifiers if they are invalid. */
9600 if (! ok)
9601 {
9602 RIDBIT_RESET (RID_UNSIGNED, specbits);
9603 RIDBIT_RESET (RID_SIGNED, specbits);
9604 RIDBIT_RESET (RID_LONG, specbits);
9605 RIDBIT_RESET (RID_SHORT, specbits);
9606 longlong = 0;
9607 }
9608 }
9609
37c46b43
MS
9610 if (RIDBIT_SETP (RID_COMPLEX, specbits)
9611 && TREE_CODE (type) != INTEGER_TYPE && TREE_CODE (type) != REAL_TYPE)
9612 {
8251199e 9613 error ("complex invalid for `%s'", name);
37c46b43
MS
9614 RIDBIT_RESET (RID_COMPLEX, specbits);
9615 }
9616
8d08fdba
MS
9617 /* Decide whether an integer type is signed or not.
9618 Optionally treat bitfields as signed by default. */
9619 if (RIDBIT_SETP (RID_UNSIGNED, specbits)
b89c5a7b
MM
9620 /* [class.bit]
9621
9622 It is implementation-defined whether a plain (neither
9623 explicitly signed or unsigned) char, short, int, or long
9624 bit-field is signed or unsigned.
9625
9626 Naturally, we extend this to long long as well. Note that
9627 this does not include wchar_t. */
9628 || (bitfield && !flag_signed_bitfields
9629 && RIDBIT_NOTSETP (RID_SIGNED, specbits)
9630 /* A typedef for plain `int' without `signed' can be
9631 controlled just like plain `int', but a typedef for
9632 `signed int' cannot be so controlled. */
9633 && !(typedef_decl
29bbeb1c 9634 && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl))
b89c5a7b
MM
9635 && (TREE_CODE (type) == INTEGER_TYPE
9636 || TREE_CODE (type) == CHAR_TYPE)
29bbeb1c 9637 && !same_type_p (TYPE_MAIN_VARIANT (type), wchar_type_node)))
8d08fdba
MS
9638 {
9639 if (longlong)
9640 type = long_long_unsigned_type_node;
9641 else if (RIDBIT_SETP (RID_LONG, specbits))
9642 type = long_unsigned_type_node;
9643 else if (RIDBIT_SETP (RID_SHORT, specbits))
9644 type = short_unsigned_type_node;
9645 else if (type == char_type_node)
9646 type = unsigned_char_type_node;
9647 else if (typedef_decl)
9648 type = unsigned_type (type);
9649 else
9650 type = unsigned_type_node;
9651 }
9652 else if (RIDBIT_SETP (RID_SIGNED, specbits)
9653 && type == char_type_node)
9654 type = signed_char_type_node;
9655 else if (longlong)
9656 type = long_long_integer_type_node;
9657 else if (RIDBIT_SETP (RID_LONG, specbits))
9658 type = long_integer_type_node;
9659 else if (RIDBIT_SETP (RID_SHORT, specbits))
9660 type = short_integer_type_node;
9661
37c46b43
MS
9662 if (RIDBIT_SETP (RID_COMPLEX, specbits))
9663 {
9664 /* If we just have "complex", it is equivalent to
9665 "complex double", but if any modifiers at all are specified it is
9666 the complex form of TYPE. E.g, "complex short" is
9667 "complex short int". */
9668
9669 if (defaulted_int && ! longlong
9670 && ! (RIDBIT_SETP (RID_LONG, specbits)
9671 || RIDBIT_SETP (RID_SHORT, specbits)
9672 || RIDBIT_SETP (RID_SIGNED, specbits)
9673 || RIDBIT_SETP (RID_UNSIGNED, specbits)))
9674 type = complex_double_type_node;
9675 else if (type == integer_type_node)
9676 type = complex_integer_type_node;
9677 else if (type == float_type_node)
9678 type = complex_float_type_node;
9679 else if (type == double_type_node)
9680 type = complex_double_type_node;
9681 else if (type == long_double_type_node)
9682 type = complex_long_double_type_node;
9683 else
9684 type = build_complex_type (type);
9685 }
9686
3ac3d9ea
MM
9687 if (return_type == return_conversion
9688 && (RIDBIT_SETP (RID_CONST, specbits)
91063b51
MM
9689 || RIDBIT_SETP (RID_VOLATILE, specbits)
9690 || RIDBIT_SETP (RID_RESTRICT, specbits)))
9691 cp_error ("qualifiers are not allowed on declaration of `operator %T'",
3ac3d9ea
MM
9692 ctor_return_type);
9693
8d08fdba
MS
9694 /* Set CONSTP if this declaration is `const', whether by
9695 explicit specification or via a typedef.
9696 Likewise for VOLATILEP. */
9697
91063b51
MM
9698 constp = !! RIDBIT_SETP (RID_CONST, specbits) + CP_TYPE_CONST_P (type);
9699 restrictp =
9700 !! RIDBIT_SETP (RID_RESTRICT, specbits) + CP_TYPE_RESTRICT_P (type);
9701 volatilep =
9702 !! RIDBIT_SETP (RID_VOLATILE, specbits) + CP_TYPE_VOLATILE_P (type);
9703 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
9704 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
9705 | (volatilep ? TYPE_QUAL_VOLATILE : 0));
9706 type = cp_build_qualified_type (type, type_quals);
8d08fdba
MS
9707 staticp = 0;
9708 inlinep = !! RIDBIT_SETP (RID_INLINE, specbits);
8d08fdba 9709 virtualp = RIDBIT_SETP (RID_VIRTUAL, specbits);
db5ae43f
MS
9710 RIDBIT_RESET (RID_VIRTUAL, specbits);
9711 explicitp = RIDBIT_SETP (RID_EXPLICIT, specbits) != 0;
9712 RIDBIT_RESET (RID_EXPLICIT, specbits);
8d08fdba 9713
8d08fdba
MS
9714 if (RIDBIT_SETP (RID_STATIC, specbits))
9715 staticp = 1 + (decl_context == FIELD);
9716
9717 if (virtualp && staticp == 2)
9718 {
8251199e 9719 cp_error ("member `%D' cannot be declared both virtual and static",
8d08fdba
MS
9720 dname);
9721 staticp = 0;
9722 }
9723 friendp = RIDBIT_SETP (RID_FRIEND, specbits);
8d08fdba
MS
9724 RIDBIT_RESET (RID_FRIEND, specbits);
9725
7e83af84
BK
9726 /* $7.1.2, Function specifiers */
9727 if (friendp && explicitp)
9728 error ("only declarations of constructors can be `explicit'");
9729
8d08fdba
MS
9730 if (RIDBIT_SETP (RID_MUTABLE, specbits))
9731 {
9732 if (decl_context == PARM)
9733 {
8251199e 9734 error ("non-member `%s' cannot be declared `mutable'", name);
8d08fdba
MS
9735 RIDBIT_RESET (RID_MUTABLE, specbits);
9736 }
9737 else if (friendp || decl_context == TYPENAME)
9738 {
8251199e 9739 error ("non-object member `%s' cannot be declared `mutable'", name);
8d08fdba
MS
9740 RIDBIT_RESET (RID_MUTABLE, specbits);
9741 }
8d08fdba
MS
9742 }
9743
9744 /* Warn if two storage classes are given. Default to `auto'. */
9745
9746 if (RIDBIT_ANY_SET (specbits))
9747 {
9748 if (RIDBIT_SETP (RID_STATIC, specbits)) nclasses++;
9749 if (RIDBIT_SETP (RID_EXTERN, specbits)) nclasses++;
9750 if (decl_context == PARM && nclasses > 0)
8251199e 9751 error ("storage class specifiers invalid in parameter declarations");
8d08fdba
MS
9752 if (RIDBIT_SETP (RID_TYPEDEF, specbits))
9753 {
9754 if (decl_context == PARM)
8251199e 9755 error ("typedef declaration invalid in parameter declaration");
8d08fdba
MS
9756 nclasses++;
9757 }
9758 if (RIDBIT_SETP (RID_AUTO, specbits)) nclasses++;
9759 if (RIDBIT_SETP (RID_REGISTER, specbits)) nclasses++;
9760 }
9761
9762 /* Give error if `virtual' is used outside of class declaration. */
b7484fbe
MS
9763 if (virtualp
9764 && (current_class_name == NULL_TREE || decl_context != FIELD))
8d08fdba 9765 {
8251199e 9766 error ("virtual outside class declaration");
8d08fdba
MS
9767 virtualp = 0;
9768 }
9769 if (current_class_name == NULL_TREE && RIDBIT_SETP (RID_MUTABLE, specbits))
9770 {
8251199e 9771 error ("only members can be declared mutable");
8d08fdba
MS
9772 RIDBIT_RESET (RID_MUTABLE, specbits);
9773 }
9774
9775 /* Static anonymous unions are dealt with here. */
9776 if (staticp && decl_context == TYPENAME
9777 && TREE_CODE (declspecs) == TREE_LIST
6bdb8141 9778 && ANON_AGGR_TYPE_P (TREE_VALUE (declspecs)))
8d08fdba
MS
9779 decl_context = FIELD;
9780
9781 /* Give error if `const,' `volatile,' `inline,' `friend,' or `virtual'
9782 is used in a signature member function declaration. */
9783 if (decl_context == FIELD
9784 && IS_SIGNATURE (current_class_type)
fc378698 9785 && RIDBIT_NOTSETP (RID_TYPEDEF, specbits))
8d08fdba 9786 {
91063b51 9787 if (type_quals != TYPE_UNQUALIFIED)
8d08fdba 9788 {
91063b51
MM
9789 error ("type qualifiers specified for signature member function `%s'", name);
9790 type_quals = TYPE_UNQUALIFIED;
8d08fdba
MS
9791 }
9792 if (inlinep)
9793 {
8251199e 9794 error ("`inline' specified for signature member function `%s'", name);
8d08fdba
MS
9795 /* Later, we'll make signature member functions inline. */
9796 inlinep = 0;
9797 }
9798 if (friendp)
9799 {
8251199e 9800 error ("`friend' declaration in signature definition");
8d08fdba
MS
9801 friendp = 0;
9802 }
9803 if (virtualp)
9804 {
8251199e 9805 error ("`virtual' specified for signature member function `%s'",
a28e3c7f 9806 name);
8d08fdba
MS
9807 /* Later, we'll make signature member functions virtual. */
9808 virtualp = 0;
9809 }
9810 }
9811
9812 /* Warn about storage classes that are invalid for certain
9813 kinds of declarations (parameters, typenames, etc.). */
9814
9815 if (nclasses > 1)
8251199e 9816 error ("multiple storage classes in declaration of `%s'", name);
8d08fdba
MS
9817 else if (decl_context != NORMAL && nclasses > 0)
9818 {
db5ae43f 9819 if ((decl_context == PARM || decl_context == CATCHPARM)
8d08fdba
MS
9820 && (RIDBIT_SETP (RID_REGISTER, specbits)
9821 || RIDBIT_SETP (RID_AUTO, specbits)))
9822 ;
fc378698
MS
9823 else if (RIDBIT_SETP (RID_TYPEDEF, specbits))
9824 ;
8d08fdba 9825 else if (decl_context == FIELD
fc378698 9826 && ! IS_SIGNATURE (current_class_type)
8d08fdba
MS
9827 /* C++ allows static class elements */
9828 && RIDBIT_SETP (RID_STATIC, specbits))
9829 /* C++ also allows inlines and signed and unsigned elements,
9830 but in those cases we don't come in here. */
9831 ;
9832 else
9833 {
9834 if (decl_context == FIELD)
9835 {
b7484fbe
MS
9836 tree tmp = NULL_TREE;
9837 register int op = 0;
9838
9839 if (declarator)
9840 {
9e9ff709
MS
9841 /* Avoid trying to get an operand off an identifier node. */
9842 if (TREE_CODE (declarator) == IDENTIFIER_NODE)
9843 tmp = declarator;
9844 else
9845 tmp = TREE_OPERAND (declarator, 0);
b7484fbe
MS
9846 op = IDENTIFIER_OPNAME_P (tmp);
9847 }
8251199e 9848 error ("storage class specified for %s `%s'",
8d08fdba
MS
9849 IS_SIGNATURE (current_class_type)
9850 ? (op
9851 ? "signature member operator"
9852 : "signature member function")
b7484fbe 9853 : (op ? "member operator" : "field"),
8d08fdba
MS
9854 op ? operator_name_string (tmp) : name);
9855 }
9856 else
db5ae43f 9857 error (((decl_context == PARM || decl_context == CATCHPARM)
8d08fdba
MS
9858 ? "storage class specified for parameter `%s'"
9859 : "storage class specified for typename"), name);
9860 RIDBIT_RESET (RID_REGISTER, specbits);
9861 RIDBIT_RESET (RID_AUTO, specbits);
9862 RIDBIT_RESET (RID_EXTERN, specbits);
9863
9864 if (decl_context == FIELD && IS_SIGNATURE (current_class_type))
9865 {
9866 RIDBIT_RESET (RID_STATIC, specbits);
9867 staticp = 0;
9868 }
9869 }
9870 }
9871 else if (RIDBIT_SETP (RID_EXTERN, specbits) && initialized && !funcdef_flag)
9872 {
a9aedbc2 9873 if (toplevel_bindings_p ())
8d08fdba 9874 {
59be0cdd 9875 /* It's common practice (and completely valid) to have a const
8d08fdba 9876 be initialized and declared extern. */
950ad3c3 9877 if (!(type_quals & TYPE_QUAL_CONST))
8251199e 9878 warning ("`%s' initialized and declared `extern'", name);
8d08fdba
MS
9879 }
9880 else
8251199e 9881 error ("`%s' has both `extern' and initializer", name);
8d08fdba
MS
9882 }
9883 else if (RIDBIT_SETP (RID_EXTERN, specbits) && funcdef_flag
a9aedbc2 9884 && ! toplevel_bindings_p ())
8251199e 9885 error ("nested function `%s' declared `extern'", name);
a9aedbc2 9886 else if (toplevel_bindings_p ())
8d08fdba
MS
9887 {
9888 if (RIDBIT_SETP (RID_AUTO, specbits))
8251199e 9889 error ("top-level declaration of `%s' specifies `auto'", name);
8d08fdba
MS
9890 }
9891
909e536a 9892 if (nclasses > 0 && friendp)
8251199e 9893 error ("storage class specifiers invalid in friend function declarations");
909e536a 9894
8d08fdba
MS
9895 /* Now figure out the structure of the declarator proper.
9896 Descend through it, creating more complex types, until we reach
9897 the declared identifier (or NULL_TREE, in an absolute declarator). */
9898
1eab9b56
JM
9899 inner_attrs = NULL_TREE;
9900 ignore_attrs = 0;
9901
386b8a85
JM
9902 while (declarator && TREE_CODE (declarator) != IDENTIFIER_NODE
9903 && TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
8d08fdba
MS
9904 {
9905 /* Each level of DECLARATOR is either an ARRAY_REF (for ...[..]),
9906 an INDIRECT_REF (for *...),
9907 a CALL_EXPR (for ...(...)),
9908 an identifier (for the name being declared)
9909 or a null pointer (for the place in an absolute declarator
9910 where the name was omitted).
9911 For the last two cases, we have just exited the loop.
9912
9913 For C++ it could also be
9914 a SCOPE_REF (for class :: ...). In this case, we have converted
9915 sensible names to types, and those are the values we use to
9916 qualify the member name.
9917 an ADDR_EXPR (for &...),
9918 a BIT_NOT_EXPR (for destructors)
8d08fdba
MS
9919
9920 At this point, TYPE is the type of elements of an array,
9921 or for a function to return, or for a pointer to point to.
9922 After this sequence of ifs, TYPE is the type of the
9923 array or function or pointer, and DECLARATOR has had its
9924 outermost layer removed. */
9925
bd6dd845 9926 if (type == error_mark_node)
8d08fdba
MS
9927 {
9928 if (TREE_CODE (declarator) == SCOPE_REF)
9929 declarator = TREE_OPERAND (declarator, 1);
9930 else
9931 declarator = TREE_OPERAND (declarator, 0);
9932 continue;
9933 }
9934 if (quals != NULL_TREE
9935 && (declarator == NULL_TREE
9936 || TREE_CODE (declarator) != SCOPE_REF))
9937 {
9938 if (ctype == NULL_TREE && TREE_CODE (type) == METHOD_TYPE)
9939 ctype = TYPE_METHOD_BASETYPE (type);
9940 if (ctype != NULL_TREE)
9941 {
8d08fdba 9942 tree dummy = build_decl (TYPE_DECL, NULL_TREE, type);
8d08fdba
MS
9943 ctype = grok_method_quals (ctype, dummy, quals);
9944 type = TREE_TYPE (dummy);
9945 quals = NULL_TREE;
9946 }
9947 }
1eab9b56
JM
9948
9949 /* See the comment for the TREE_LIST case, below. */
9950 if (ignore_attrs)
9951 ignore_attrs = 0;
9952 else if (inner_attrs)
9953 {
9954 decl_attributes (type, inner_attrs, NULL_TREE);
9955 inner_attrs = NULL_TREE;
9956 }
9957
8d08fdba
MS
9958 switch (TREE_CODE (declarator))
9959 {
52fbc847
JM
9960 case TREE_LIST:
9961 {
9962 /* We encode a declarator with embedded attributes using
1eab9b56
JM
9963 a TREE_LIST. The attributes apply to the declarator
9964 directly inside them, so we have to skip an iteration
9965 before applying them to the type. If the declarator just
9966 inside is the declarator-id, we apply the attrs to the
9967 decl itself. */
9968 inner_attrs = TREE_PURPOSE (declarator);
9969 ignore_attrs = 1;
52fbc847 9970 declarator = TREE_VALUE (declarator);
52fbc847
JM
9971 }
9972 break;
9973
8d08fdba
MS
9974 case ARRAY_REF:
9975 {
9976 register tree itype = NULL_TREE;
9977 register tree size = TREE_OPERAND (declarator, 1);
b7484fbe
MS
9978 /* The index is a signed object `sizetype' bits wide. */
9979 tree index_type = signed_type (sizetype);
8d08fdba
MS
9980
9981 declarator = TREE_OPERAND (declarator, 0);
9982
9983 /* Check for some types that there cannot be arrays of. */
9984
a1774733 9985 if (TREE_CODE (type) == VOID_TYPE)
8d08fdba 9986 {
8251199e 9987 cp_error ("declaration of `%D' as array of voids", dname);
8d08fdba
MS
9988 type = error_mark_node;
9989 }
9990
9991 if (TREE_CODE (type) == FUNCTION_TYPE)
9992 {
8251199e 9993 cp_error ("declaration of `%D' as array of functions", dname);
8d08fdba
MS
9994 type = error_mark_node;
9995 }
9996
9997 /* ARM $8.4.3: Since you can't have a pointer to a reference,
9998 you can't have arrays of references. If we allowed them,
59be0cdd 9999 then we'd be saying x[i] is valid for an array x, but
8d08fdba
MS
10000 then you'd have to ask: what does `*(x + i)' mean? */
10001 if (TREE_CODE (type) == REFERENCE_TYPE)
10002 {
10003 if (decl_context == TYPENAME)
8251199e 10004 cp_error ("cannot make arrays of references");
8d08fdba 10005 else
8251199e 10006 cp_error ("declaration of `%D' as array of references",
8d08fdba
MS
10007 dname);
10008 type = error_mark_node;
10009 }
10010
10011 if (TREE_CODE (type) == OFFSET_TYPE)
10012 {
8251199e 10013 cp_error ("declaration of `%D' as array of data members",
8d08fdba
MS
10014 dname);
10015 type = error_mark_node;
10016 }
10017
10018 if (TREE_CODE (type) == METHOD_TYPE)
10019 {
8251199e 10020 cp_error ("declaration of `%D' as array of function members",
8d08fdba
MS
10021 dname);
10022 type = error_mark_node;
10023 }
10024
10025 if (size == error_mark_node)
10026 type = error_mark_node;
3bfdc719
MM
10027 else if (TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type))
10028 {
10029 /* [dcl.array]
10030
10031 the constant expressions that specify the bounds of
10032 the arrays can be omitted only for the first member
10033 of the sequence. */
10034 cp_error ("declaration of `%D' as multidimensional array",
10035 dname);
10036 cp_error ("must have bounds for all dimensions except the first");
10037 type = error_mark_node;
10038 }
8d08fdba
MS
10039
10040 if (type == error_mark_node)
10041 continue;
10042
f2e2cbd4 10043 /* VC++ spells a zero-sized array with []. */
e8ea7036
MM
10044 if (size == NULL_TREE && decl_context == FIELD && ! staticp
10045 && ! RIDBIT_SETP (RID_TYPEDEF, specbits))
f2e2cbd4
JM
10046 size = integer_zero_node;
10047
8d08fdba
MS
10048 if (size)
10049 {
10050 /* Must suspend_momentary here because the index
10051 type may need to live until the end of the function.
10052 For example, it is used in the declaration of a
10053 variable which requires destructing at the end of
10054 the function; then build_vec_delete will need this
10055 value. */
10056 int yes = suspend_momentary ();
2c73f9f5 10057 /* Might be a cast. */
8d08fdba
MS
10058 if (TREE_CODE (size) == NOP_EXPR
10059 && TREE_TYPE (size) == TREE_TYPE (TREE_OPERAND (size, 0)))
10060 size = TREE_OPERAND (size, 0);
9f33663b
JM
10061 if (TREE_READONLY_DECL_P (size))
10062 size = decl_constant_value (size);
8d08fdba 10063
5db698f6
MM
10064 /* If this involves a template parameter, it will be a
10065 constant at instantiation time, but we don't know
10066 what the value is yet. Even if no template
10067 parameters are involved, we may an expression that
10068 is not a constant; we don't even simplify `1 + 2'
10069 when processing a template. */
10070 if (processing_template_decl)
5566b478 10071 {
e97e5263
JM
10072 /* Resolve a qualified reference to an enumerator or
10073 static const data member of ours. */
10074 if (TREE_CODE (size) == SCOPE_REF
10075 && TREE_OPERAND (size, 0) == current_class_type)
10076 {
10077 tree t = lookup_field (current_class_type,
10078 TREE_OPERAND (size, 1), 0, 0);
10079 if (t)
10080 size = t;
10081 }
10082
f05f680a
AO
10083 itype = build_index_type (build_min
10084 (MINUS_EXPR, sizetype, size, integer_one_node));
5566b478
MS
10085 goto dont_grok_size;
10086 }
8d08fdba
MS
10087
10088 if (TREE_CODE (TREE_TYPE (size)) != INTEGER_TYPE
161c12b0
JM
10089 && TREE_CODE (TREE_TYPE (size)) != ENUMERAL_TYPE
10090 && TREE_CODE (TREE_TYPE (size)) != BOOLEAN_TYPE)
8d08fdba 10091 {
8251199e 10092 cp_error ("size of array `%D' has non-integer type",
8d08fdba
MS
10093 dname);
10094 size = integer_one_node;
10095 }
e1cd6e56 10096 if (pedantic && integer_zerop (size))
8251199e 10097 cp_pedwarn ("ANSI C++ forbids zero-size array `%D'", dname);
8d08fdba
MS
10098 if (TREE_CONSTANT (size))
10099 {
dff6b454
RK
10100 int old_flag_pedantic_errors = flag_pedantic_errors;
10101 int old_pedantic = pedantic;
10102 pedantic = flag_pedantic_errors = 1;
10103 /* Always give overflow errors on array subscripts. */
8d08fdba 10104 constant_expression_warning (size);
dff6b454
RK
10105 pedantic = old_pedantic;
10106 flag_pedantic_errors = old_flag_pedantic_errors;
8d08fdba
MS
10107 if (INT_CST_LT (size, integer_zero_node))
10108 {
8251199e 10109 cp_error ("size of array `%D' is negative", dname);
8d08fdba
MS
10110 size = integer_one_node;
10111 }
8d08fdba
MS
10112 }
10113 else
10114 {
e1cd6e56 10115 if (pedantic)
a0a33927
MS
10116 {
10117 if (dname)
8251199e 10118 cp_pedwarn ("ANSI C++ forbids variable-size array `%D'",
a0a33927
MS
10119 dname);
10120 else
8251199e 10121 cp_pedwarn ("ANSI C++ forbids variable-size array");
a0a33927 10122 }
8d08fdba 10123 }
b7484fbe 10124
beb53fb8
JM
10125 itype
10126 = fold (build_binary_op (MINUS_EXPR,
37c46b43
MS
10127 cp_convert (index_type, size),
10128 cp_convert (index_type,
337c90cc 10129 integer_one_node)));
b7484fbe
MS
10130 if (! TREE_CONSTANT (itype))
10131 itype = variable_size (itype);
594740f3
MS
10132 else if (TREE_OVERFLOW (itype))
10133 {
8251199e 10134 error ("overflow in array dimension");
594740f3
MS
10135 TREE_OVERFLOW (itype) = 0;
10136 }
fc378698
MS
10137
10138 /* If we're a parm, we need to have a permanent type so
10139 mangling checks for re-use will work right. If both the
10140 element and index types are permanent, the array type
10141 will be, too. */
10142 if (decl_context == PARM
10143 && allocation_temporary_p () && TREE_PERMANENT (type))
10144 {
10145 push_obstacks (&permanent_obstack, &permanent_obstack);
10146 itype = build_index_type (itype);
10147 pop_obstacks ();
10148 }
10149 else
10150 itype = build_index_type (itype);
10151
5566b478 10152 dont_grok_size:
8d08fdba
MS
10153 resume_momentary (yes);
10154 }
10155
8d08fdba 10156 type = build_cplus_array_type (type, itype);
8d08fdba
MS
10157 ctype = NULL_TREE;
10158 }
10159 break;
10160
10161 case CALL_EXPR:
10162 {
10163 tree arg_types;
f376e137
MS
10164 int funcdecl_p;
10165 tree inner_parms = TREE_OPERAND (declarator, 1);
10166 tree inner_decl = TREE_OPERAND (declarator, 0);
8d08fdba
MS
10167
10168 /* Declaring a function type.
10169 Make sure we have a valid type for the function to return. */
8d08fdba 10170
91063b51 10171 /* We now know that the TYPE_QUALS don't apply to the
14ae7e7d 10172 decl, but to its return type. */
91063b51 10173 type_quals = TYPE_UNQUALIFIED;
8d08fdba
MS
10174
10175 /* Warn about some types functions can't return. */
10176
10177 if (TREE_CODE (type) == FUNCTION_TYPE)
10178 {
8251199e 10179 error ("`%s' declared as function returning a function", name);
8d08fdba
MS
10180 type = integer_type_node;
10181 }
10182 if (TREE_CODE (type) == ARRAY_TYPE)
10183 {
8251199e 10184 error ("`%s' declared as function returning an array", name);
8d08fdba
MS
10185 type = integer_type_node;
10186 }
10187
f376e137
MS
10188 if (inner_decl && TREE_CODE (inner_decl) == SCOPE_REF)
10189 inner_decl = TREE_OPERAND (inner_decl, 1);
10190
386b8a85
JM
10191 if (inner_decl && TREE_CODE (inner_decl) == TEMPLATE_ID_EXPR)
10192 inner_decl = dname;
10193
b7484fbe
MS
10194 /* Pick up type qualifiers which should be applied to `this'. */
10195 quals = TREE_OPERAND (declarator, 2);
10196
c11b6f21
MS
10197 /* Pick up the exception specifications. */
10198 raises = TREE_TYPE (declarator);
10199
f376e137
MS
10200 /* Say it's a definition only for the CALL_EXPR
10201 closest to the identifier. */
beb53fb8 10202 funcdecl_p
386b8a85
JM
10203 = inner_decl
10204 && (TREE_CODE (inner_decl) == IDENTIFIER_NODE
10205 || TREE_CODE (inner_decl) == TEMPLATE_ID_EXPR
10206 || TREE_CODE (inner_decl) == BIT_NOT_EXPR);
10207
8d08fdba
MS
10208 if (ctype == NULL_TREE
10209 && decl_context == FIELD
f376e137 10210 && funcdecl_p
8d08fdba
MS
10211 && (friendp == 0 || dname == current_class_name))
10212 ctype = current_class_type;
10213
51c184be 10214 if (ctype && return_type == return_conversion)
8d08fdba
MS
10215 TYPE_HAS_CONVERSION (ctype) = 1;
10216 if (ctype && constructor_name (ctype) == dname)
10217 {
10218 /* We are within a class's scope. If our declarator name
10219 is the same as the class name, and we are defining
10220 a function, then it is a constructor/destructor, and
10221 therefore returns a void type. */
10222
10223 if (flags == DTOR_FLAG)
10224 {
10225 /* ANSI C++ June 5 1992 WP 12.4.1. A destructor may
10226 not be declared const or volatile. A destructor
10227 may not be static. */
10228 if (staticp == 2)
8251199e 10229 error ("destructor cannot be static member function");
b7484fbe 10230 if (quals)
8d08fdba 10231 {
4f70a846
MM
10232 cp_error ("destructors may not be `%s'",
10233 IDENTIFIER_POINTER (TREE_VALUE (quals)));
10234 quals = NULL_TREE;
8d08fdba
MS
10235 }
10236 if (decl_context == FIELD)
10237 {
10238 if (! member_function_or_else (ctype, current_class_type,
10239 "destructor for alien class `%s' cannot be a member"))
10240 return void_type_node;
10241 }
10242 }
2c73f9f5 10243 else /* It's a constructor. */
8d08fdba 10244 {
db5ae43f
MS
10245 if (explicitp == 1)
10246 explicitp = 2;
8d08fdba
MS
10247 /* ANSI C++ June 5 1992 WP 12.1.2. A constructor may
10248 not be declared const or volatile. A constructor may
10249 not be virtual. A constructor may not be static. */
10250 if (staticp == 2)
8251199e 10251 error ("constructor cannot be static member function");
8d08fdba
MS
10252 if (virtualp)
10253 {
8251199e 10254 pedwarn ("constructors cannot be declared virtual");
8d08fdba
MS
10255 virtualp = 0;
10256 }
b7484fbe 10257 if (quals)
8d08fdba 10258 {
4f70a846
MM
10259 cp_error ("constructors may not be `%s'",
10260 IDENTIFIER_POINTER (TREE_VALUE (quals)));
10261 quals = NULL_TREE;
8d08fdba 10262 }
8d08fdba 10263 {
51c184be 10264 RID_BIT_TYPE tmp_bits;
fc378698 10265 bcopy ((void*)&specbits, (void*)&tmp_bits, sizeof (RID_BIT_TYPE));
51c184be
MS
10266 RIDBIT_RESET (RID_INLINE, tmp_bits);
10267 RIDBIT_RESET (RID_STATIC, tmp_bits);
10268 if (RIDBIT_ANY_SET (tmp_bits))
8251199e 10269 error ("return value type specifier for constructor ignored");
8d08fdba 10270 }
f30432d7 10271 type = build_pointer_type (ctype);
beb53fb8
JM
10272 if (decl_context == FIELD
10273 && IS_SIGNATURE (current_class_type))
8d08fdba 10274 {
8251199e 10275 error ("constructor not allowed in signature");
8d08fdba
MS
10276 return void_type_node;
10277 }
10278 else if (decl_context == FIELD)
10279 {
10280 if (! member_function_or_else (ctype, current_class_type,
10281 "constructor for alien class `%s' cannot be member"))
10282 return void_type_node;
10283 TYPE_HAS_CONSTRUCTOR (ctype) = 1;
10284 if (return_type != return_ctor)
10285 return NULL_TREE;
10286 }
10287 }
10288 if (decl_context == FIELD)
10289 staticp = 0;
10290 }
b7484fbe 10291 else if (friendp)
8d08fdba 10292 {
b7484fbe 10293 if (initialized)
8251199e 10294 error ("can't initialize friend function `%s'", name);
b7484fbe
MS
10295 if (virtualp)
10296 {
10297 /* Cannot be both friend and virtual. */
8251199e 10298 error ("virtual functions cannot be friends");
b7484fbe
MS
10299 RIDBIT_RESET (RID_FRIEND, specbits);
10300 friendp = 0;
10301 }
28cbf42c 10302 if (decl_context == NORMAL)
8251199e 10303 error ("friend declaration not in class definition");
28cbf42c 10304 if (current_function_decl && funcdef_flag)
8251199e 10305 cp_error ("can't define friend function `%s' in a local class definition",
28cbf42c 10306 name);
8d08fdba
MS
10307 }
10308
8d08fdba
MS
10309 /* Construct the function type and go to the next
10310 inner layer of declarator. */
10311
f376e137 10312 declarator = TREE_OPERAND (declarator, 0);
8d08fdba 10313
f376e137
MS
10314 /* FIXME: This is where default args should be fully
10315 processed. */
8d08fdba 10316
f376e137 10317 arg_types = grokparms (inner_parms, funcdecl_p ? funcdef_flag : 0);
8d08fdba 10318
4f70a846 10319 if (declarator && flags == DTOR_FLAG)
8d08fdba 10320 {
4f70a846
MM
10321 /* A destructor declared in the body of a class will
10322 be represented as a BIT_NOT_EXPR. But, we just
10323 want the underlying IDENTIFIER. */
8d08fdba 10324 if (TREE_CODE (declarator) == BIT_NOT_EXPR)
4f70a846
MM
10325 declarator = TREE_OPERAND (declarator, 0);
10326
10327 if (strict_prototype == 0 && arg_types == NULL_TREE)
10328 arg_types = void_list_node;
10329 else if (arg_types == NULL_TREE
10330 || arg_types != void_list_node)
8d08fdba 10331 {
4f70a846
MM
10332 cp_error ("destructors may not have parameters");
10333 arg_types = void_list_node;
10334 last_function_parms = NULL_TREE;
8d08fdba
MS
10335 }
10336 }
10337
d22c8596 10338 /* ANSI says that `const int foo ();'
8d08fdba 10339 does not make the function foo const. */
d22c8596 10340 type = build_function_type (type, arg_types);
42976354
BK
10341
10342 {
10343 tree t;
10344 for (t = arg_types; t; t = TREE_CHAIN (t))
10345 if (TREE_PURPOSE (t)
10346 && TREE_CODE (TREE_PURPOSE (t)) == DEFAULT_ARG)
10347 {
10348 add_defarg_fn (type);
10349 break;
10350 }
10351 }
8d08fdba
MS
10352 }
10353 break;
10354
10355 case ADDR_EXPR:
10356 case INDIRECT_REF:
10357 /* Filter out pointers-to-references and references-to-references.
10358 We can get these if a TYPE_DECL is used. */
10359
10360 if (TREE_CODE (type) == REFERENCE_TYPE)
10361 {
8251199e 10362 error ("cannot declare %s to references",
8d08fdba
MS
10363 TREE_CODE (declarator) == ADDR_EXPR
10364 ? "references" : "pointers");
10365 declarator = TREE_OPERAND (declarator, 0);
10366 continue;
10367 }
10368
a5894242
MS
10369 if (TREE_CODE (type) == OFFSET_TYPE
10370 && (TREE_CODE (TREE_TYPE (type)) == VOID_TYPE
10371 || TREE_CODE (TREE_TYPE (type)) == REFERENCE_TYPE))
10372 {
8251199e 10373 cp_error ("cannot declare pointer to `%#T' member",
a5894242
MS
10374 TREE_TYPE (type));
10375 type = TREE_TYPE (type);
10376 }
10377
61a127b3
MM
10378 /* Merge any constancy or volatility into the target type
10379 for the pointer. */
10380
91063b51
MM
10381 /* We now know that the TYPE_QUALS don't apply to the decl,
10382 but to the target of the pointer. */
10383 type_quals = TYPE_UNQUALIFIED;
8d08fdba
MS
10384
10385 if (IS_SIGNATURE (type))
10386 {
10387 if (TREE_CODE (declarator) == ADDR_EXPR)
10388 {
63718c49
GB
10389 if (CLASSTYPE_METHOD_VEC (type) == NULL_TREE
10390 && TYPE_SIZE (type))
8251199e 10391 cp_warning ("empty signature `%T' used in signature reference declaration",
63718c49 10392 type);
8d08fdba 10393#if 0
14ae7e7d 10394 type = build_signature_reference_type (type);
8d08fdba
MS
10395#else
10396 sorry ("signature reference");
10397 return NULL_TREE;
10398#endif
10399 }
10400 else
10401 {
63718c49
GB
10402 if (CLASSTYPE_METHOD_VEC (type) == NULL_TREE
10403 && TYPE_SIZE (type))
8251199e 10404 cp_warning ("empty signature `%T' used in signature pointer declaration",
63718c49 10405 type);
14ae7e7d 10406 type = build_signature_pointer_type (type);
8d08fdba 10407 }
8d08fdba
MS
10408 }
10409 else if (TREE_CODE (declarator) == ADDR_EXPR)
10410 {
69851283
MM
10411 if (TREE_CODE (type) == VOID_TYPE)
10412 error ("invalid type: `void &'");
8d08fdba 10413 else
69851283 10414 type = build_reference_type (type);
8d08fdba
MS
10415 }
10416 else if (TREE_CODE (type) == METHOD_TYPE)
d8f8dca1 10417 type = build_ptrmemfunc_type (build_pointer_type (type));
8d08fdba
MS
10418 else
10419 type = build_pointer_type (type);
10420
10421 /* Process a list of type modifier keywords (such as
10422 const or volatile) that were given inside the `*' or `&'. */
10423
10424 if (TREE_TYPE (declarator))
10425 {
10426 register tree typemodlist;
10427 int erred = 0;
91063b51
MM
10428
10429 constp = 0;
10430 volatilep = 0;
10431 restrictp = 0;
8d08fdba
MS
10432 for (typemodlist = TREE_TYPE (declarator); typemodlist;
10433 typemodlist = TREE_CHAIN (typemodlist))
10434 {
91063b51
MM
10435 tree qualifier = TREE_VALUE (typemodlist);
10436
10437 if (qualifier == ridpointers[(int) RID_CONST])
8d08fdba 10438 constp++;
91063b51 10439 else if (qualifier == ridpointers[(int) RID_VOLATILE])
8d08fdba 10440 volatilep++;
91063b51
MM
10441 else if (qualifier == ridpointers[(int) RID_RESTRICT])
10442 restrictp++;
8d08fdba
MS
10443 else if (!erred)
10444 {
10445 erred = 1;
91063b51 10446 error ("invalid type modifier within pointer declarator");
8d08fdba
MS
10447 }
10448 }
10449 if (constp > 1)
8251199e 10450 pedwarn ("duplicate `const'");
8d08fdba 10451 if (volatilep > 1)
8251199e 10452 pedwarn ("duplicate `volatile'");
91063b51
MM
10453 if (restrictp > 1)
10454 pedwarn ("duplicate `restrict'");
10455
10456 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
10457 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
10458 | (volatilep ? TYPE_QUAL_VOLATILE : 0));
a0a33927
MS
10459 if (TREE_CODE (declarator) == ADDR_EXPR
10460 && (constp || volatilep))
10461 {
10462 if (constp)
8251199e 10463 pedwarn ("discarding `const' applied to a reference");
a0a33927 10464 if (volatilep)
8251199e 10465 pedwarn ("discarding `volatile' applied to a reference");
91063b51 10466 type_quals &= ~(TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE);
a0a33927 10467 }
91063b51 10468 type = cp_build_qualified_type (type, type_quals);
8d08fdba
MS
10469 }
10470 declarator = TREE_OPERAND (declarator, 0);
10471 ctype = NULL_TREE;
10472 break;
10473
10474 case SCOPE_REF:
10475 {
10476 /* We have converted type names to NULL_TREE if the
10477 name was bogus, or to a _TYPE node, if not.
10478
10479 The variable CTYPE holds the type we will ultimately
10480 resolve to. The code here just needs to build
10481 up appropriate member types. */
10482 tree sname = TREE_OPERAND (declarator, 1);
386b8a85
JM
10483 tree t;
10484
8d08fdba
MS
10485 /* Destructors can have their visibilities changed as well. */
10486 if (TREE_CODE (sname) == BIT_NOT_EXPR)
10487 sname = TREE_OPERAND (sname, 0);
10488
10489 if (TREE_COMPLEXITY (declarator) == 0)
10490 /* This needs to be here, in case we are called
10491 multiple times. */ ;
9a68c51f
JM
10492 else if (TREE_COMPLEXITY (declarator) == -1)
10493 /* Namespace member. */
10494 pop_decl_namespace ();
8d08fdba 10495 else if (friendp && (TREE_COMPLEXITY (declarator) < 2))
2c73f9f5 10496 /* Don't fall out into global scope. Hides real bug? --eichin */ ;
5566b478
MS
10497 else if (! IS_AGGR_TYPE_CODE
10498 (TREE_CODE (TREE_OPERAND (declarator, 0))))
10499 ;
8d08fdba
MS
10500 else if (TREE_COMPLEXITY (declarator) == current_class_depth)
10501 {
9a3b49ac
MS
10502 /* Resolve any TYPENAME_TYPEs from the decl-specifier-seq
10503 that refer to ctype. They couldn't be resolved earlier
10504 because we hadn't pushed into the class yet.
10505 Example: resolve 'B<T>::type' in
10506 'B<typename B<T>::type> B<T>::f () { }'. */
10507 if (current_template_parms
10508 && uses_template_parms (type)
10509 && uses_template_parms (current_class_type))
10510 {
10511 tree args = current_template_args ();
4393e105 10512 type = tsubst (type, args, /*complain=*/1, NULL_TREE);
9a3b49ac
MS
10513 }
10514
a28e3c7f
MS
10515 /* This pop_nested_class corresponds to the
10516 push_nested_class used to push into class scope for
10517 parsing the argument list of a function decl, in
10518 qualified_id. */
b74a0560 10519 pop_nested_class ();
8d08fdba
MS
10520 TREE_COMPLEXITY (declarator) = current_class_depth;
10521 }
10522 else
10523 my_friendly_abort (16);
10524
10525 if (TREE_OPERAND (declarator, 0) == NULL_TREE)
10526 {
10527 /* We had a reference to a global decl, or
10528 perhaps we were given a non-aggregate typedef,
10529 in which case we cleared this out, and should just
10530 keep going as though it wasn't there. */
10531 declarator = sname;
10532 continue;
10533 }
10534 ctype = TREE_OPERAND (declarator, 0);
10535
386b8a85 10536 t = ctype;
7ddedda4 10537 while (t != NULL_TREE && CLASS_TYPE_P (t))
386b8a85 10538 {
75650646
MM
10539 if (CLASSTYPE_TEMPLATE_INFO (t) &&
10540 !CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
386b8a85
JM
10541 template_count += 1;
10542 t = TYPE_MAIN_DECL (t);
10543 if (DECL_LANG_SPECIFIC (t))
10544 t = DECL_CLASS_CONTEXT (t);
10545 else
10546 t = NULL_TREE;
10547 }
10548
8d08fdba
MS
10549 if (sname == NULL_TREE)
10550 goto done_scoping;
10551
10552 if (TREE_CODE (sname) == IDENTIFIER_NODE)
10553 {
10554 /* This is the `standard' use of the scoping operator:
10555 basetype :: member . */
10556
db5ae43f 10557 if (ctype == current_class_type)
28cbf42c
MS
10558 {
10559 /* class A {
10560 void A::f ();
10561 };
10562
10563 Is this ill-formed? */
10564
10565 if (pedantic)
8251199e 10566 cp_pedwarn ("extra qualification `%T::' on member `%s' ignored",
28cbf42c
MS
10567 ctype, name);
10568 }
db5ae43f 10569 else if (TREE_CODE (type) == FUNCTION_TYPE)
8d08fdba
MS
10570 {
10571 if (current_class_type == NULL_TREE
8d08fdba 10572 || friendp)
14ae7e7d
JM
10573 type = build_cplus_method_type (ctype, TREE_TYPE (type),
10574 TYPE_ARG_TYPES (type));
8d08fdba
MS
10575 else
10576 {
8251199e 10577 cp_error ("cannot declare member function `%T::%s' within `%T'",
a3203465 10578 ctype, name, current_class_type);
8d08fdba
MS
10579 return void_type_node;
10580 }
10581 }
5566b478
MS
10582 else if (RIDBIT_SETP (RID_TYPEDEF, specbits)
10583 || TYPE_SIZE (complete_type (ctype)) != NULL_TREE)
8d08fdba 10584 {
2c73f9f5 10585 /* Have to move this code elsewhere in this function.
db5ae43f
MS
10586 this code is used for i.e., typedef int A::M; M *pm;
10587
10588 It is? How? jason 10/2/94 */
8d08fdba 10589
8d08fdba
MS
10590 if (current_class_type)
10591 {
8251199e 10592 cp_error ("cannot declare member `%T::%s' within `%T'",
db5ae43f
MS
10593 ctype, name, current_class_type);
10594 return void_type_node;
8d08fdba
MS
10595 }
10596 type = build_offset_type (ctype, type);
10597 }
10598 else if (uses_template_parms (ctype))
10599 {
8d08fdba 10600 if (TREE_CODE (type) == FUNCTION_TYPE)
beb53fb8 10601 type
14ae7e7d 10602 = build_cplus_method_type (ctype, TREE_TYPE (type),
beb53fb8 10603 TYPE_ARG_TYPES (type));
8d08fdba
MS
10604 }
10605 else
10606 {
8251199e 10607 cp_error ("structure `%T' not yet defined", ctype);
8d08fdba
MS
10608 return error_mark_node;
10609 }
10610
10611 declarator = sname;
10612 }
8d08fdba
MS
10613 else if (TREE_CODE (sname) == SCOPE_REF)
10614 my_friendly_abort (17);
10615 else
10616 {
10617 done_scoping:
10618 declarator = TREE_OPERAND (declarator, 1);
10619 if (declarator && TREE_CODE (declarator) == CALL_EXPR)
10620 /* In this case, we will deal with it later. */
10621 ;
10622 else
10623 {
10624 if (TREE_CODE (type) == FUNCTION_TYPE)
14ae7e7d
JM
10625 type = build_cplus_method_type (ctype, TREE_TYPE (type),
10626 TYPE_ARG_TYPES (type));
8d08fdba
MS
10627 else
10628 type = build_offset_type (ctype, type);
10629 }
10630 }
10631 }
10632 break;
10633
10634 case BIT_NOT_EXPR:
10635 declarator = TREE_OPERAND (declarator, 0);
10636 break;
10637
8d08fdba
MS
10638 case RECORD_TYPE:
10639 case UNION_TYPE:
10640 case ENUMERAL_TYPE:
10641 declarator = NULL_TREE;
10642 break;
10643
10644 case ERROR_MARK:
10645 declarator = NULL_TREE;
10646 break;
10647
10648 default:
10649 my_friendly_abort (158);
10650 }
10651 }
10652
1eab9b56
JM
10653 /* See the comment for the TREE_LIST case, above. */
10654 if (inner_attrs)
10655 {
10656 if (! ignore_attrs)
10657 decl_attributes (type, inner_attrs, NULL_TREE);
10658 else if (attrlist)
10659 TREE_VALUE (attrlist) = chainon (inner_attrs, TREE_VALUE (attrlist));
10660 else
10661 attrlist = build_decl_list (NULL_TREE, inner_attrs);
10662 }
10663
419c6212
JM
10664 /* Now TYPE has the actual type. */
10665
db5ae43f
MS
10666 if (explicitp == 1)
10667 {
8251199e 10668 error ("only constructors can be declared `explicit'");
db5ae43f
MS
10669 explicitp = 0;
10670 }
10671
f30432d7
MS
10672 if (RIDBIT_SETP (RID_MUTABLE, specbits))
10673 {
91063b51 10674 if (type_quals & TYPE_QUAL_CONST)
f30432d7 10675 {
8251199e 10676 error ("const `%s' cannot be declared `mutable'", name);
f30432d7
MS
10677 RIDBIT_RESET (RID_MUTABLE, specbits);
10678 }
10679 else if (staticp)
10680 {
8251199e 10681 error ("static `%s' cannot be declared `mutable'", name);
f30432d7
MS
10682 RIDBIT_RESET (RID_MUTABLE, specbits);
10683 }
10684 }
10685
efe38fab
JM
10686 if (declarator == NULL_TREE
10687 || TREE_CODE (declarator) == IDENTIFIER_NODE
10688 || (TREE_CODE (declarator) == TEMPLATE_ID_EXPR
10689 && (TREE_CODE (type) == FUNCTION_TYPE
10690 || TREE_CODE (type) == METHOD_TYPE)))
10691 /* OK */;
10692 else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
10693 {
10694 cp_error ("template-id `%D' used as a declarator", declarator);
10695 declarator = dname;
10696 }
10697 else
419c6212 10698 /* Unexpected declarator format. */
efe38fab
JM
10699 my_friendly_abort (990210);
10700
419c6212
JM
10701 /* If this is declaring a typedef name, return a TYPE_DECL. */
10702
fc378698 10703 if (RIDBIT_SETP (RID_TYPEDEF, specbits) && decl_context != TYPENAME)
8d08fdba
MS
10704 {
10705 tree decl;
10706
10707 /* Note that the grammar rejects storage classes
10708 in typenames, fields or parameters. */
eff71ab0
PB
10709 if (current_lang_name == lang_name_java)
10710 TYPE_FOR_JAVA (type) = 1;
8d08fdba 10711
d2e5ee5c
MS
10712 if (decl_context == FIELD)
10713 {
094fe153 10714 if (declarator == constructor_name (current_class_type))
8251199e 10715 cp_pedwarn ("ANSI C++ forbids nested type `%D' with same name as enclosing class",
d2e5ee5c
MS
10716 declarator);
10717 decl = build_lang_decl (TYPE_DECL, declarator, type);
10718 if (IS_SIGNATURE (current_class_type) && opaque_typedef)
10719 SIGNATURE_HAS_OPAQUE_TYPEDECLS (current_class_type) = 1;
10720 }
10721 else
7fb4a8f7
JM
10722 {
10723 /* Make sure this typedef lives as long as its type,
10724 since it might be used as a template parameter. */
1aed5355
MM
10725 if (type != error_mark_node)
10726 push_obstacks (TYPE_OBSTACK (type), TYPE_OBSTACK (type));
7fb4a8f7 10727 decl = build_decl (TYPE_DECL, declarator, type);
1aed5355
MM
10728 if (type != error_mark_node)
10729 pop_obstacks ();
7fb4a8f7 10730 }
d2e5ee5c 10731
8d08fdba
MS
10732 /* If the user declares "struct {...} foo" then `foo' will have
10733 an anonymous name. Fill that name in now. Nothing can
10734 refer to it, so nothing needs know about the name change.
10735 The TYPE_NAME field was filled in by build_struct_xref. */
10736 if (type != error_mark_node
10737 && TYPE_NAME (type)
10738 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
b537d120
MM
10739 && ANON_AGGRNAME_P (TYPE_IDENTIFIER (type))
10740 && CP_TYPE_QUALS (type) == TYPE_UNQUALIFIED)
8d08fdba 10741 {
dcd08efc
JM
10742 tree oldname = TYPE_NAME (type);
10743 tree t;
10744
2c73f9f5 10745 /* Replace the anonymous name with the real name everywhere. */
8d08fdba 10746 lookup_tag_reverse (type, declarator);
dcd08efc
JM
10747 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
10748 if (TYPE_NAME (t) == oldname)
10749 TYPE_NAME (t) = decl;
8d08fdba
MS
10750
10751 if (TYPE_LANG_SPECIFIC (type))
10752 TYPE_WAS_ANONYMOUS (type) = 1;
10753
33964bf4
MM
10754 /* If this is a typedef within a template class, the nested
10755 type is a (non-primary) template. The name for the
10756 template needs updating as well. */
10757 if (TYPE_LANG_SPECIFIC (type) && CLASSTYPE_TEMPLATE_INFO (type))
10758 DECL_NAME (CLASSTYPE_TI_TEMPLATE (type))
10759 = TYPE_IDENTIFIER (type);
10760
2c73f9f5
ML
10761 /* XXX Temporarily set the scope.
10762 When returning, start_decl expects it as NULL_TREE,
10763 and will then then set it using pushdecl. */
10764 my_friendly_assert (DECL_CONTEXT (decl) == NULL_TREE, 980404);
10765 if (current_class_type)
10766 DECL_CONTEXT (decl) = current_class_type;
10767 else
cb0dbb9a 10768 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
2c73f9f5 10769
d2e5ee5c
MS
10770 DECL_ASSEMBLER_NAME (decl) = DECL_NAME (decl);
10771 DECL_ASSEMBLER_NAME (decl)
10772 = get_identifier (build_overload_name (type, 1, 1));
2c73f9f5 10773 DECL_CONTEXT (decl) = NULL_TREE;
50a6dbd7
JM
10774
10775 /* FIXME remangle member functions; member functions of a
10776 type with external linkage have external linkage. */
fc378698 10777 }
fc378698 10778
8d08fdba
MS
10779 if (TREE_CODE (type) == OFFSET_TYPE || TREE_CODE (type) == METHOD_TYPE)
10780 {
8251199e 10781 cp_error_at ("typedef name may not be class-qualified", decl);
a3203465 10782 return NULL_TREE;
8d08fdba
MS
10783 }
10784 else if (quals)
10785 {
10786 if (ctype == NULL_TREE)
10787 {
10788 if (TREE_CODE (type) != METHOD_TYPE)
8251199e 10789 cp_error_at ("invalid type qualifier for non-method type", decl);
8d08fdba
MS
10790 else
10791 ctype = TYPE_METHOD_BASETYPE (type);
10792 }
10793 if (ctype != NULL_TREE)
10794 grok_method_quals (ctype, decl, quals);
10795 }
10796
10797 if (RIDBIT_SETP (RID_SIGNED, specbits)
10798 || (typedef_decl && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl)))
10799 C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
10800
10801 if (RIDBIT_SETP (RID_MUTABLE, specbits))
61a127b3 10802 error ("non-object member `%s' cannot be declared mutable", name);
8d08fdba 10803
c91a56d2
MS
10804 bad_specifiers (decl, "type", virtualp, quals != NULL_TREE,
10805 inlinep, friendp, raises != NULL_TREE);
10806
10807 if (initialized)
8251199e 10808 error ("typedef declaration includes an initializer");
c91a56d2 10809
8d08fdba
MS
10810 return decl;
10811 }
10812
10813 /* Detect the case of an array type of unspecified size
10814 which came, as such, direct from a typedef name.
10815 We must copy the type, so that each identifier gets
10816 a distinct type, so that each identifier's size can be
10817 controlled separately by its own initializer. */
10818
10819 if (type == typedef_type && TREE_CODE (type) == ARRAY_TYPE
10820 && TYPE_DOMAIN (type) == NULL_TREE)
10821 {
10822 type = build_cplus_array_type (TREE_TYPE (type), TYPE_DOMAIN (type));
10823 }
10824
10825 /* If this is a type name (such as, in a cast or sizeof),
10826 compute the type and return it now. */
10827
10828 if (decl_context == TYPENAME)
10829 {
10830 /* Note that the grammar rejects storage classes
10831 in typenames, fields or parameters. */
91063b51 10832 if (type_quals != TYPE_UNQUALIFIED)
a703fb38
KG
10833 {
10834 if (IS_SIGNATURE (type))
91063b51
MM
10835 error ("type qualifiers specified for signature type");
10836 type_quals = TYPE_UNQUALIFIED;
a703fb38 10837 }
8d08fdba
MS
10838
10839 /* Special case: "friend class foo" looks like a TYPENAME context. */
10840 if (friendp)
10841 {
91063b51 10842 if (type_quals != TYPE_UNQUALIFIED)
b7484fbe 10843 {
91063b51
MM
10844 cp_error ("type qualifiers specified for friend class declaration");
10845 type_quals = TYPE_UNQUALIFIED;
b7484fbe
MS
10846 }
10847 if (inlinep)
10848 {
8251199e 10849 cp_error ("`inline' specified for friend class declaration");
b7484fbe
MS
10850 inlinep = 0;
10851 }
10852
10853 /* Only try to do this stuff if we didn't already give up. */
10854 if (type != integer_type_node)
10855 {
10856 /* A friendly class? */
10857 if (current_class_type)
10858 make_friend_class (current_class_type, TYPE_MAIN_VARIANT (type));
10859 else
8251199e 10860 error ("trying to make class `%s' a friend of global scope",
b7484fbe
MS
10861 TYPE_NAME_STRING (type));
10862 type = void_type_node;
10863 }
8d08fdba
MS
10864 }
10865 else if (quals)
10866 {
8d08fdba 10867 tree dummy = build_decl (TYPE_DECL, declarator, type);
8d08fdba
MS
10868 if (ctype == NULL_TREE)
10869 {
10870 my_friendly_assert (TREE_CODE (type) == METHOD_TYPE, 159);
10871 ctype = TYPE_METHOD_BASETYPE (type);
10872 }
10873 grok_method_quals (ctype, dummy, quals);
10874 type = TREE_TYPE (dummy);
10875 }
10876
10877 return type;
10878 }
10879 else if (declarator == NULL_TREE && decl_context != PARM
db5ae43f 10880 && decl_context != CATCHPARM
8d08fdba
MS
10881 && TREE_CODE (type) != UNION_TYPE
10882 && ! bitfield)
10883 {
8251199e 10884 cp_error ("abstract declarator `%T' used as declaration", type);
8d08fdba
MS
10885 declarator = make_anon_name ();
10886 }
10887
10888 /* `void' at top level (not within pointer)
10889 is allowed only in typedefs or type names.
10890 We don't complain about parms either, but that is because
10891 a better error message can be made later. */
10892
a1774733 10893 if (TREE_CODE (type) == VOID_TYPE && decl_context != PARM)
8d08fdba 10894 {
b7484fbe 10895 if (! declarator)
8251199e 10896 error ("unnamed variable or field declared void");
b7484fbe 10897 else if (TREE_CODE (declarator) == IDENTIFIER_NODE)
8d08fdba
MS
10898 {
10899 if (IDENTIFIER_OPNAME_P (declarator))
8d08fdba 10900 my_friendly_abort (356);
8d08fdba 10901 else
8251199e 10902 error ("variable or field `%s' declared void", name);
8d08fdba
MS
10903 }
10904 else
8251199e 10905 error ("variable or field declared void");
8d08fdba
MS
10906 type = integer_type_node;
10907 }
10908
10909 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
10910 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
10911
14ae7e7d
JM
10912 if (decl_context == PARM || decl_context == CATCHPARM)
10913 {
10914 if (ctype || in_namespace)
10915 error ("cannot use `::' in parameter declaration");
10916
10917 /* A parameter declared as an array of T is really a pointer to T.
10918 One declared as a function is really a pointer to a function.
10919 One declared as a member is really a pointer to member. */
10920
10921 if (TREE_CODE (type) == ARRAY_TYPE)
10922 {
10923 /* Transfer const-ness of array into that of type pointed to. */
10924 type = build_pointer_type (TREE_TYPE (type));
91063b51 10925 type_quals = TYPE_UNQUALIFIED;
14ae7e7d
JM
10926 }
10927 else if (TREE_CODE (type) == FUNCTION_TYPE)
10928 type = build_pointer_type (type);
10929 else if (TREE_CODE (type) == OFFSET_TYPE)
10930 type = build_pointer_type (type);
10931 else if (TREE_CODE (type) == VOID_TYPE && declarator)
10932 {
10933 error ("declaration of `%s' as void", name);
10934 return NULL_TREE;
10935 }
10936 }
10937
8d08fdba
MS
10938 {
10939 register tree decl;
10940
10941 if (decl_context == PARM)
10942 {
ff350acd 10943 decl = build_decl (PARM_DECL, declarator, type);
8d08fdba
MS
10944
10945 bad_specifiers (decl, "parameter", virtualp, quals != NULL_TREE,
10946 inlinep, friendp, raises != NULL_TREE);
10947 if (current_class_type
10948 && IS_SIGNATURE (current_class_type))
10949 {
10950 if (inlinep)
8251199e 10951 error ("parameter of signature member function declared `inline'");
8d08fdba 10952 if (RIDBIT_SETP (RID_AUTO, specbits))
8251199e 10953 error ("parameter of signature member function declared `auto'");
8d08fdba 10954 if (RIDBIT_SETP (RID_REGISTER, specbits))
8251199e 10955 error ("parameter of signature member function declared `register'");
8d08fdba
MS
10956 }
10957
10958 /* Compute the type actually passed in the parmlist,
10959 for the case where there is no prototype.
10960 (For example, shorts and chars are passed as ints.)
10961 When there is a prototype, this is overridden later. */
10962
39211cd5 10963 DECL_ARG_TYPE (decl) = type_promotes_to (type);
8d08fdba
MS
10964 }
10965 else if (decl_context == FIELD)
10966 {
10967 if (type == error_mark_node)
10968 {
10969 /* Happens when declaring arrays of sizes which
10970 are error_mark_node, for example. */
10971 decl = NULL_TREE;
10972 }
997a088c 10973 else if (in_namespace && !friendp)
05008fb9
MM
10974 {
10975 /* Something like struct S { int N::j; }; */
8251199e 10976 cp_error ("invalid use of `::'");
05008fb9
MM
10977 decl = NULL_TREE;
10978 }
8d08fdba
MS
10979 else if (TREE_CODE (type) == FUNCTION_TYPE)
10980 {
10981 int publicp = 0;
e1467ff2 10982 tree function_context;
8d08fdba 10983
72b7eeff
MS
10984 /* We catch the others as conflicts with the builtin
10985 typedefs. */
10986 if (friendp && declarator == ridpointers[(int) RID_SIGNED])
10987 {
8251199e 10988 cp_error ("function `%D' cannot be declared friend",
72b7eeff
MS
10989 declarator);
10990 friendp = 0;
10991 }
10992
8d08fdba
MS
10993 if (friendp == 0)
10994 {
10995 if (ctype == NULL_TREE)
10996 ctype = current_class_type;
10997
10998 if (ctype == NULL_TREE)
10999 {
8251199e 11000 cp_error ("can't make `%D' into a method -- not in a class",
8d08fdba
MS
11001 declarator);
11002 return void_type_node;
11003 }
11004
11005 /* ``A union may [ ... ] not [ have ] virtual functions.''
11006 ARM 9.5 */
11007 if (virtualp && TREE_CODE (ctype) == UNION_TYPE)
11008 {
8251199e 11009 cp_error ("function `%D' declared virtual inside a union",
8d08fdba
MS
11010 declarator);
11011 return void_type_node;
11012 }
11013
11014 if (declarator == ansi_opname[(int) NEW_EXPR]
a28e3c7f
MS
11015 || declarator == ansi_opname[(int) VEC_NEW_EXPR]
11016 || declarator == ansi_opname[(int) DELETE_EXPR]
11017 || declarator == ansi_opname[(int) VEC_DELETE_EXPR])
8d08fdba
MS
11018 {
11019 if (virtualp)
11020 {
8251199e 11021 cp_error ("`%D' cannot be declared virtual, since it is always static",
8d08fdba
MS
11022 declarator);
11023 virtualp = 0;
11024 }
11025 }
11026 else if (staticp < 2)
14ae7e7d
JM
11027 type = build_cplus_method_type (ctype, TREE_TYPE (type),
11028 TYPE_ARG_TYPES (type));
8d08fdba
MS
11029 }
11030
11031 /* Tell grokfndecl if it needs to set TREE_PUBLIC on the node. */
e1467ff2
MM
11032 function_context = (ctype != NULL_TREE) ?
11033 hack_decl_function_context (TYPE_MAIN_DECL (ctype)) : NULL_TREE;
11034 publicp = (! friendp || ! staticp)
11035 && function_context == NULL_TREE;
386b8a85
JM
11036 decl = grokfndecl (ctype, type,
11037 TREE_CODE (declarator) != TEMPLATE_ID_EXPR
11038 ? declarator : dname,
11039 declarator,
7a8f9fa9 11040 virtualp, flags, quals, raises,
386b8a85 11041 friendp ? -1 : 0, friendp, publicp, inlinep,
2c73f9f5 11042 funcdef_flag, template_count, in_namespace);
20496fa2 11043 if (decl == NULL_TREE)
3ddfb0e6 11044 return decl;
6125f3be
DE
11045#if 0
11046 /* This clobbers the attrs stored in `decl' from `attrlist'. */
11047 /* The decl and setting of decl_machine_attr is also turned off. */
71851aaa 11048 decl = build_decl_attribute_variant (decl, decl_machine_attr);
6125f3be 11049#endif
f0e01782 11050
cc804e51
MM
11051 /* [class.conv.ctor]
11052
11053 A constructor declared without the function-specifier
11054 explicit that can be called with a single parameter
11055 specifies a conversion from the type of its first
11056 parameter to the type of its class. Such a constructor
11057 is called a converting constructor. */
db5ae43f
MS
11058 if (explicitp == 2)
11059 DECL_NONCONVERTING_P (decl) = 1;
cc804e51
MM
11060 else if (DECL_CONSTRUCTOR_P (decl))
11061 {
11062 /* The constructor can be called with exactly one
11063 parameter if there is at least one parameter, and
11064 any subsequent parameters have default arguments.
11065 We don't look at the first parameter, which is
11066 really just the `this' parameter for the new
11067 object. */
11068 tree arg_types =
11069 TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)));
11070
11071 /* Skip the `in_chrg' argument too, if present. */
11072 if (TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (decl)))
11073 arg_types = TREE_CHAIN (arg_types);
11074
11075 if (arg_types == void_list_node
11076 || (arg_types
11077 && TREE_CHAIN (arg_types)
11078 && TREE_CHAIN (arg_types) != void_list_node
11079 && !TREE_PURPOSE (TREE_CHAIN (arg_types))))
11080 DECL_NONCONVERTING_P (decl) = 1;
11081 }
8d08fdba
MS
11082 }
11083 else if (TREE_CODE (type) == METHOD_TYPE)
11084 {
faae18ab
MS
11085 /* We only get here for friend declarations of
11086 members of other classes. */
8d08fdba
MS
11087 /* All method decls are public, so tell grokfndecl to set
11088 TREE_PUBLIC, also. */
386b8a85 11089 decl = grokfndecl (ctype, type, declarator, declarator,
7a8f9fa9 11090 virtualp, flags, quals, raises,
386b8a85 11091 friendp ? -1 : 0, friendp, 1, 0, funcdef_flag,
2c73f9f5 11092 template_count, in_namespace);
f0e01782
MS
11093 if (decl == NULL_TREE)
11094 return NULL_TREE;
8d08fdba 11095 }
5566b478
MS
11096 else if (!staticp && ! processing_template_decl
11097 && TYPE_SIZE (complete_type (type)) == NULL_TREE
8d08fdba
MS
11098 && (TREE_CODE (type) != ARRAY_TYPE || initialized == 0))
11099 {
b7484fbe 11100 if (declarator)
8251199e 11101 cp_error ("field `%D' has incomplete type", declarator);
b7484fbe 11102 else
8251199e 11103 cp_error ("name `%T' has incomplete type", type);
8d08fdba
MS
11104
11105 /* If we're instantiating a template, tell them which
11106 instantiation made the field's type be incomplete. */
11107 if (current_class_type
11108 && TYPE_NAME (current_class_type)
d2e5ee5c 11109 && IDENTIFIER_TEMPLATE (TYPE_IDENTIFIER (current_class_type))
8d08fdba
MS
11110 && declspecs && TREE_VALUE (declspecs)
11111 && TREE_TYPE (TREE_VALUE (declspecs)) == type)
8251199e 11112 cp_error (" in instantiation of template `%T'",
db5ae43f
MS
11113 current_class_type);
11114
8d08fdba
MS
11115 type = error_mark_node;
11116 decl = NULL_TREE;
11117 }
11118 else
11119 {
11120 if (friendp)
11121 {
8251199e 11122 error ("`%s' is neither function nor method; cannot be declared friend",
8d08fdba
MS
11123 IDENTIFIER_POINTER (declarator));
11124 friendp = 0;
11125 }
11126 decl = NULL_TREE;
11127 }
11128
11129 if (friendp)
11130 {
11131 /* Friends are treated specially. */
11132 if (ctype == current_class_type)
8251199e 11133 warning ("member functions are implicitly friends of their class");
8d08fdba
MS
11134 else
11135 {
11136 tree t = NULL_TREE;
11137 if (decl && DECL_NAME (decl))
36a117a5
MM
11138 {
11139 if (template_class_depth (current_class_type) == 0)
6c30752f
MM
11140 {
11141 decl
11142 = check_explicit_specialization
11143 (declarator, decl,
11144 template_count, 2 * (funcdef_flag != 0) + 4);
11145 if (decl == error_mark_node)
11146 return error_mark_node;
11147 }
11148
36a117a5 11149 t = do_friend (ctype, declarator, decl,
7a8f9fa9 11150 last_function_parms, attrlist, flags, quals,
36a117a5
MM
11151 funcdef_flag);
11152 }
8d08fdba
MS
11153 if (t && funcdef_flag)
11154 return t;
11155
11156 return void_type_node;
11157 }
11158 }
11159
11160 /* Structure field. It may not be a function, except for C++ */
11161
11162 if (decl == NULL_TREE)
11163 {
8d08fdba
MS
11164 if (initialized)
11165 {
3ac3d9ea
MM
11166 if (!staticp)
11167 {
11168 /* An attempt is being made to initialize a non-static
11169 member. But, from [class.mem]:
11170
11171 4 A member-declarator can contain a
11172 constant-initializer only if it declares a static
11173 member (_class.static_) of integral or enumeration
11174 type, see _class.static.data_.
11175
11176 This used to be relatively common practice, but
11177 the rest of the compiler does not correctly
11178 handle the initialization unless the member is
11179 static so we make it static below. */
950ad3c3 11180 cp_pedwarn ("ANSI C++ forbids initialization of member `%D'",
3ac3d9ea 11181 declarator);
8251199e 11182 cp_pedwarn ("making `%D' static", declarator);
3ac3d9ea
MM
11183 staticp = 1;
11184 }
11185
6ba89f8e
MM
11186 if (uses_template_parms (type))
11187 /* We'll check at instantiation time. */
11188 ;
11189 else if (check_static_variable_definition (declarator,
11190 type))
11191 /* If we just return the declaration, crashes
11192 will sometimes occur. We therefore return
72a93143
JM
11193 void_type_node, as if this was a friend
11194 declaration, to cause callers to completely
11195 ignore this declaration. */
6ba89f8e 11196 return void_type_node;
8d08fdba
MS
11197 }
11198
8ebeee52 11199 /* 9.2p13 [class.mem] */
4d7614e9
JM
11200 if (declarator == constructor_name (current_class_type)
11201 /* Divergence from the standard: In extern "C", we
11202 allow non-static data members here, because C does
11203 and /usr/include/netinet/in.h uses that. */
1f901793 11204 && (staticp || ! in_system_header))
8ebeee52
JM
11205 cp_pedwarn ("ANSI C++ forbids data member `%D' with same name as enclosing class",
11206 declarator);
11207
3ac3d9ea 11208 if (staticp)
8d08fdba
MS
11209 {
11210 /* C++ allows static class members.
11211 All other work for this is done by grokfield.
d7ceab7b 11212 This VAR_DCL is built by build_lang_field_decl.
8d08fdba
MS
11213 All other VAR_DECLs are built by build_decl. */
11214 decl = build_lang_field_decl (VAR_DECL, declarator, type);
5b605f68
MS
11215 TREE_STATIC (decl) = 1;
11216 /* In class context, 'static' means public access. */
3ac3d9ea 11217 TREE_PUBLIC (decl) = DECL_EXTERNAL (decl) = 1;
8d08fdba
MS
11218 }
11219 else
11220 {
11221 decl = build_lang_field_decl (FIELD_DECL, declarator, type);
11222 if (RIDBIT_SETP (RID_MUTABLE, specbits))
11223 {
11224 DECL_MUTABLE_P (decl) = 1;
11225 RIDBIT_RESET (RID_MUTABLE, specbits);
11226 }
11227 }
11228
11229 bad_specifiers (decl, "field", virtualp, quals != NULL_TREE,
11230 inlinep, friendp, raises != NULL_TREE);
11231 }
11232 }
11233 else if (TREE_CODE (type) == FUNCTION_TYPE || TREE_CODE (type) == METHOD_TYPE)
11234 {
386b8a85 11235 tree original_name;
8d08fdba
MS
11236 int publicp = 0;
11237
11238 if (! declarator)
11239 return NULL_TREE;
11240
386b8a85
JM
11241 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
11242 original_name = dname;
11243 else
11244 original_name = declarator;
11245
8926095f 11246 if (RIDBIT_SETP (RID_AUTO, specbits))
8251199e 11247 error ("storage class `auto' invalid for function `%s'", name);
8926095f 11248 else if (RIDBIT_SETP (RID_REGISTER, specbits))
8251199e 11249 error ("storage class `register' invalid for function `%s'", name);
8d08fdba
MS
11250
11251 /* Function declaration not at top level.
11252 Storage classes other than `extern' are not allowed
11253 and `extern' makes no difference. */
a9aedbc2 11254 if (! toplevel_bindings_p ()
8926095f
MS
11255 && (RIDBIT_SETP (RID_STATIC, specbits)
11256 || RIDBIT_SETP (RID_INLINE, specbits))
8d08fdba 11257 && pedantic)
8926095f
MS
11258 {
11259 if (RIDBIT_SETP (RID_STATIC, specbits))
8251199e 11260 pedwarn ("storage class `static' invalid for function `%s' declared out of global scope", name);
8926095f 11261 else
8251199e 11262 pedwarn ("storage class `inline' invalid for function `%s' declared out of global scope", name);
8926095f
MS
11263 }
11264
8d08fdba
MS
11265 if (ctype == NULL_TREE)
11266 {
11267 if (virtualp)
11268 {
8251199e 11269 error ("virtual non-class function `%s'", name);
8d08fdba
MS
11270 virtualp = 0;
11271 }
8d08fdba
MS
11272 }
11273 else if (TREE_CODE (type) == FUNCTION_TYPE && staticp < 2)
14ae7e7d
JM
11274 type = build_cplus_method_type (ctype, TREE_TYPE (type),
11275 TYPE_ARG_TYPES (type));
8d08fdba 11276
eb66be0e 11277 /* Record presence of `static'. */
faae18ab 11278 publicp = (ctype != NULL_TREE
a9aedbc2 11279 || RIDBIT_SETP (RID_EXTERN, specbits)
eb66be0e 11280 || !RIDBIT_SETP (RID_STATIC, specbits));
8d08fdba 11281
386b8a85 11282 decl = grokfndecl (ctype, type, original_name, declarator,
7a8f9fa9 11283 virtualp, flags, quals, raises,
75650646 11284 1, friendp,
386b8a85 11285 publicp, inlinep, funcdef_flag,
2c73f9f5 11286 template_count, in_namespace);
f0e01782
MS
11287 if (decl == NULL_TREE)
11288 return NULL_TREE;
8d08fdba 11289
8d08fdba
MS
11290 if (staticp == 1)
11291 {
11292 int illegal_static = 0;
11293
11294 /* Don't allow a static member function in a class, and forbid
11295 declaring main to be static. */
11296 if (TREE_CODE (type) == METHOD_TYPE)
11297 {
8251199e 11298 cp_pedwarn ("cannot declare member function `%D' to have static linkage", decl);
8d08fdba
MS
11299 illegal_static = 1;
11300 }
8d08fdba
MS
11301 else if (current_function_decl)
11302 {
11303 /* FIXME need arm citation */
8251199e 11304 error ("cannot declare static function inside another function");
8d08fdba
MS
11305 illegal_static = 1;
11306 }
11307
11308 if (illegal_static)
11309 {
11310 staticp = 0;
11311 RIDBIT_RESET (RID_STATIC, specbits);
11312 }
11313 }
8d08fdba
MS
11314 }
11315 else
11316 {
11317 /* It's a variable. */
11318
11319 /* An uninitialized decl with `extern' is a reference. */
2c73f9f5 11320 decl = grokvardecl (type, declarator, &specbits,
950ad3c3
MM
11321 initialized,
11322 (type_quals & TYPE_QUAL_CONST) != 0,
11323 in_namespace);
8d08fdba
MS
11324 bad_specifiers (decl, "variable", virtualp, quals != NULL_TREE,
11325 inlinep, friendp, raises != NULL_TREE);
11326
11327 if (ctype)
11328 {
f0e01782 11329 DECL_CONTEXT (decl) = ctype;
8d08fdba
MS
11330 if (staticp == 1)
11331 {
8251199e 11332 cp_pedwarn ("static member `%D' re-declared as static", decl);
8d08fdba
MS
11333 staticp = 0;
11334 RIDBIT_RESET (RID_STATIC, specbits);
11335 }
b7484fbe
MS
11336 if (RIDBIT_SETP (RID_REGISTER, specbits) && TREE_STATIC (decl))
11337 {
8251199e 11338 cp_error ("static member `%D' declared `register'", decl);
b7484fbe
MS
11339 RIDBIT_RESET (RID_REGISTER, specbits);
11340 }
f30432d7 11341 if (RIDBIT_SETP (RID_EXTERN, specbits) && pedantic)
8d08fdba 11342 {
8251199e 11343 cp_pedwarn ("cannot explicitly declare member `%#D' to have extern linkage",
f30432d7 11344 decl);
8d08fdba
MS
11345 RIDBIT_RESET (RID_EXTERN, specbits);
11346 }
11347 }
11348 }
11349
11350 if (RIDBIT_SETP (RID_MUTABLE, specbits))
11351 {
8251199e 11352 error ("`%s' cannot be declared mutable", name);
8d08fdba
MS
11353 }
11354
11355 /* Record `register' declaration for warnings on &
11356 and in case doing stupid register allocation. */
11357
11358 if (RIDBIT_SETP (RID_REGISTER, specbits))
11359 DECL_REGISTER (decl) = 1;
11360
8926095f
MS
11361 if (RIDBIT_SETP (RID_EXTERN, specbits))
11362 DECL_THIS_EXTERN (decl) = 1;
11363
faae18ab
MS
11364 if (RIDBIT_SETP (RID_STATIC, specbits))
11365 DECL_THIS_STATIC (decl) = 1;
11366
adecb3f4
MM
11367 /* Record constancy and volatility. There's no need to do this
11368 when processing a template; we'll do this for the instantiated
11369 declaration based on the type of DECL. */
11370 if (!processing_template_decl)
11371 c_apply_type_quals_to_decl (type_quals, decl);
8d08fdba
MS
11372
11373 return decl;
11374 }
11375}
11376\f
11377/* Tell if a parmlist/exprlist looks like an exprlist or a parmlist.
11378 An empty exprlist is a parmlist. An exprlist which
11379 contains only identifiers at the global level
11380 is a parmlist. Otherwise, it is an exprlist. */
e92cc029 11381
8d08fdba
MS
11382int
11383parmlist_is_exprlist (exprs)
11384 tree exprs;
11385{
11386 if (exprs == NULL_TREE || TREE_PARMLIST (exprs))
11387 return 0;
11388
a9aedbc2 11389 if (toplevel_bindings_p ())
8d08fdba
MS
11390 {
11391 /* At the global level, if these are all identifiers,
11392 then it is a parmlist. */
11393 while (exprs)
11394 {
11395 if (TREE_CODE (TREE_VALUE (exprs)) != IDENTIFIER_NODE)
11396 return 1;
11397 exprs = TREE_CHAIN (exprs);
11398 }
11399 return 0;
11400 }
11401 return 1;
11402}
11403
f181d4ae
MM
11404/* Subroutine of start_function. Ensure that each of the parameter
11405 types (as listed in PARMS) is complete, as is required for a
11406 function definition. */
e92cc029 11407
8d08fdba
MS
11408static void
11409require_complete_types_for_parms (parms)
11410 tree parms;
11411{
11412 while (parms)
11413 {
11414 tree type = TREE_TYPE (parms);
5566b478 11415 if (TYPE_SIZE (complete_type (type)) == NULL_TREE)
8d08fdba
MS
11416 {
11417 if (DECL_NAME (parms))
8251199e 11418 error ("parameter `%s' has incomplete type",
8d08fdba
MS
11419 IDENTIFIER_POINTER (DECL_NAME (parms)));
11420 else
8251199e 11421 error ("parameter has incomplete type");
8d08fdba
MS
11422 TREE_TYPE (parms) = error_mark_node;
11423 }
42f989ff
JM
11424 else
11425 layout_decl (parms, 0);
f181d4ae 11426
8d08fdba
MS
11427 parms = TREE_CHAIN (parms);
11428 }
11429}
11430
297e73d8
MM
11431/* Returns DECL if DECL is a local variable (or parameter). Returns
11432 NULL_TREE otherwise. */
11433
11434static tree
11435local_variable_p (t)
11436 tree t;
11437{
11438 if ((TREE_CODE (t) == VAR_DECL
11439 /* A VAR_DECL with a context that is a _TYPE is a static data
11440 member. */
11441 && !TYPE_P (CP_DECL_CONTEXT (t))
11442 /* Any other non-local variable must be at namespace scope. */
11443 && TREE_CODE (CP_DECL_CONTEXT (t)) != NAMESPACE_DECL)
11444 || (TREE_CODE (t) == PARM_DECL))
11445 return t;
11446
11447 return NULL_TREE;
11448}
11449
11450/* Check that ARG, which is a default-argument expression for a
11451 parameter DECL, is legal. Returns ARG, or ERROR_MARK_NODE, if
11452 something goes wrong. DECL may also be a _TYPE node, rather than a
11453 DECL, if there is no DECL available. */
11454
11455tree
11456check_default_argument (decl, arg)
11457 tree decl;
11458 tree arg;
11459{
11460 tree var;
11461 tree decl_type;
11462
11463 if (TREE_CODE (arg) == DEFAULT_ARG)
11464 /* We get a DEFAULT_ARG when looking at an in-class declaration
11465 with a default argument. Ignore the argument for now; we'll
11466 deal with it after the class is complete. */
11467 return arg;
11468
11469 if (processing_template_decl || uses_template_parms (arg))
11470 /* We don't do anything checking until instantiation-time. Note
11471 that there may be uninstantiated arguments even for an
11472 instantiated function, since default arguments are not
11473 instantiated until they are needed. */
11474 return arg;
11475
11476 if (TYPE_P (decl))
11477 {
11478 decl_type = decl;
11479 decl = NULL_TREE;
11480 }
11481 else
11482 decl_type = TREE_TYPE (decl);
11483
11484 if (arg == error_mark_node
11485 || decl == error_mark_node
11486 || TREE_TYPE (arg) == error_mark_node
11487 || decl_type == error_mark_node)
11488 /* Something already went wrong. There's no need to check
11489 further. */
11490 return error_mark_node;
11491
11492 /* [dcl.fct.default]
11493
11494 A default argument expression is implicitly converted to the
11495 parameter type. */
11496 if (!TREE_TYPE (arg)
11497 || !can_convert_arg (decl_type, TREE_TYPE (arg), arg))
11498 {
11499 if (decl)
11500 cp_error ("default argument for `%#D' has type `%T'",
11501 decl, TREE_TYPE (arg));
11502 else
11503 cp_error ("default argument for paramter of type `%T' has type `%T'",
11504 decl_type, TREE_TYPE (arg));
11505
11506 return error_mark_node;
11507 }
11508
11509 /* [dcl.fct.default]
11510
11511 Local variables shall not be used in default argument
11512 expressions.
11513
11514 The keyword `this' shall not be used in a default argument of a
11515 member function. */
11516 var = search_tree (arg, local_variable_p);
11517 if (var)
11518 {
11519 cp_error ("default argument `%E' uses local variable `%D'",
11520 arg, var);
11521 return error_mark_node;
11522 }
11523
11524 /* All is well. */
11525 return arg;
11526}
11527
8d08fdba
MS
11528/* Decode the list of parameter types for a function type.
11529 Given the list of things declared inside the parens,
11530 return a list of types.
11531
11532 The list we receive can have three kinds of elements:
11533 an IDENTIFIER_NODE for names given without types,
11534 a TREE_LIST node for arguments given as typespecs or names with typespecs,
11535 or void_type_node, to mark the end of an argument list
11536 when additional arguments are not permitted (... was not used).
11537
11538 FUNCDEF_FLAG is nonzero for a function definition, 0 for
11539 a mere declaration. A nonempty identifier-list gets an error message
11540 when FUNCDEF_FLAG is zero.
11541 If FUNCDEF_FLAG is 1, then parameter types must be complete.
11542 If FUNCDEF_FLAG is -1, then parameter types may be incomplete.
11543
11544 If all elements of the input list contain types,
11545 we return a list of the types.
11546 If all elements contain no type (except perhaps a void_type_node
11547 at the end), we return a null list.
11548 If some have types and some do not, it is an error, and we
11549 return a null list.
11550
11551 Also set last_function_parms to either
11552 a list of names (IDENTIFIER_NODEs) or a chain of PARM_DECLs.
11553 A list of names is converted to a chain of PARM_DECLs
11554 by store_parm_decls so that ultimately it is always a chain of decls.
11555
11556 Note that in C++, parameters can take default values. These default
11557 values are in the TREE_PURPOSE field of the TREE_LIST. It is
11558 an error to specify default values which are followed by parameters
11559 that have no default values, or an ELLIPSES. For simplicities sake,
11560 only parameters which are specified with their types can take on
11561 default values. */
11562
11563static tree
11564grokparms (first_parm, funcdef_flag)
11565 tree first_parm;
11566 int funcdef_flag;
11567{
11568 tree result = NULL_TREE;
11569 tree decls = NULL_TREE;
11570
11571 if (first_parm != NULL_TREE
11572 && TREE_CODE (TREE_VALUE (first_parm)) == IDENTIFIER_NODE)
11573 {
11574 if (! funcdef_flag)
8251199e 11575 pedwarn ("parameter names (without types) in function declaration");
8d08fdba
MS
11576 last_function_parms = first_parm;
11577 return NULL_TREE;
11578 }
11579 else if (first_parm != NULL_TREE
11580 && TREE_CODE (TREE_VALUE (first_parm)) != TREE_LIST
a1774733 11581 && TREE_CODE (TREE_VALUE (first_parm)) != VOID_TYPE)
8d08fdba
MS
11582 my_friendly_abort (145);
11583 else
11584 {
11585 /* Types were specified. This is a list of declarators
11586 each represented as a TREE_LIST node. */
11587 register tree parm, chain;
5566b478 11588 int any_init = 0, any_error = 0;
8d08fdba
MS
11589
11590 if (first_parm != NULL_TREE)
11591 {
11592 tree last_result = NULL_TREE;
11593 tree last_decl = NULL_TREE;
11594
11595 for (parm = first_parm; parm != NULL_TREE; parm = chain)
11596 {
a703fb38 11597 tree type = NULL_TREE, list_node = parm;
8d08fdba
MS
11598 register tree decl = TREE_VALUE (parm);
11599 tree init = TREE_PURPOSE (parm);
11600
11601 chain = TREE_CHAIN (parm);
11602 /* @@ weak defense against parse errors. */
a1774733
BK
11603 if (TREE_CODE (decl) != VOID_TYPE
11604 && TREE_CODE (decl) != TREE_LIST)
8d08fdba
MS
11605 {
11606 /* Give various messages as the need arises. */
11607 if (TREE_CODE (decl) == STRING_CST)
8251199e 11608 cp_error ("invalid string constant `%E'", decl);
8d08fdba 11609 else if (TREE_CODE (decl) == INTEGER_CST)
8251199e 11610 error ("invalid integer constant in parameter list, did you forget to give parameter name?");
8d08fdba
MS
11611 continue;
11612 }
11613
a1774733 11614 if (TREE_CODE (decl) != VOID_TYPE)
8d08fdba 11615 {
8d08fdba
MS
11616 decl = grokdeclarator (TREE_VALUE (decl),
11617 TREE_PURPOSE (decl),
f30432d7 11618 PARM, init != NULL_TREE,
c11b6f21 11619 NULL_TREE);
3bfdc719 11620 if (! decl || TREE_TYPE (decl) == error_mark_node)
8d08fdba 11621 continue;
01240200
MM
11622
11623 /* Top-level qualifiers on the parameters are
11624 ignored for function types. */
11625 type = TYPE_MAIN_VARIANT (TREE_TYPE (decl));
11626
a1774733 11627 if (TREE_CODE (type) == VOID_TYPE)
8d08fdba
MS
11628 decl = void_type_node;
11629 else if (TREE_CODE (type) == METHOD_TYPE)
11630 {
11631 if (DECL_NAME (decl))
fc378698 11632 /* Cannot use the decl here because
8d08fdba 11633 we don't have DECL_CONTEXT set up yet. */
8251199e 11634 cp_error ("parameter `%D' invalidly declared method type",
fc378698 11635 DECL_NAME (decl));
8d08fdba 11636 else
8251199e 11637 error ("parameter invalidly declared method type");
8d08fdba
MS
11638 type = build_pointer_type (type);
11639 TREE_TYPE (decl) = type;
11640 }
11641 else if (TREE_CODE (type) == OFFSET_TYPE)
11642 {
11643 if (DECL_NAME (decl))
8251199e 11644 cp_error ("parameter `%D' invalidly declared offset type",
fc378698 11645 DECL_NAME (decl));
8d08fdba 11646 else
8251199e 11647 error ("parameter invalidly declared offset type");
8d08fdba
MS
11648 type = build_pointer_type (type);
11649 TREE_TYPE (decl) = type;
11650 }
a7a64a77
MM
11651 else if (abstract_virtuals_error (decl, type))
11652 any_error = 1; /* Seems like a good idea. */
8d08fdba
MS
11653 else if (TREE_CODE (type) == RECORD_TYPE
11654 && TYPE_LANG_SPECIFIC (type)
11655 && IS_SIGNATURE (type))
11656 {
11657 signature_error (decl, type);
2c73f9f5 11658 any_error = 1; /* Seems like a good idea. */
8d08fdba 11659 }
482b883f
JM
11660 else if (POINTER_TYPE_P (type))
11661 {
11662 tree t = type;
11663 while (POINTER_TYPE_P (t)
11664 || (TREE_CODE (t) == ARRAY_TYPE
11665 && TYPE_DOMAIN (t) != NULL_TREE))
11666 t = TREE_TYPE (t);
11667 if (TREE_CODE (t) == ARRAY_TYPE)
57af8358
MM
11668 cp_error ("parameter type `%T' includes %s to array of unknown bound",
11669 type,
11670 TYPE_PTR_P (type) ? "pointer" : "reference");
482b883f 11671 }
8d08fdba
MS
11672 }
11673
a1774733 11674 if (TREE_CODE (decl) == VOID_TYPE)
8d08fdba
MS
11675 {
11676 if (result == NULL_TREE)
11677 {
11678 result = void_list_node;
11679 last_result = result;
11680 }
11681 else
11682 {
11683 TREE_CHAIN (last_result) = void_list_node;
11684 last_result = void_list_node;
11685 }
8d08fdba
MS
11686 if (chain
11687 && (chain != void_list_node || TREE_CHAIN (chain)))
8251199e 11688 error ("`void' in parameter list must be entire list");
8d08fdba
MS
11689 break;
11690 }
11691
11692 /* Since there is a prototype, args are passed in their own types. */
11693 DECL_ARG_TYPE (decl) = TREE_TYPE (decl);
fa56377d
JJ
11694 if (PROMOTE_PROTOTYPES
11695 && (TREE_CODE (type) == INTEGER_TYPE
11696 || TREE_CODE (type) == ENUMERAL_TYPE)
8d08fdba
MS
11697 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
11698 DECL_ARG_TYPE (decl) = integer_type_node;
297e73d8 11699 if (!any_error && init)
8d08fdba 11700 {
297e73d8
MM
11701 any_init++;
11702 init = check_default_argument (decl, init);
8d08fdba
MS
11703 }
11704 else
11705 init = NULL_TREE;
11706
11707 if (decls == NULL_TREE)
11708 {
11709 decls = decl;
11710 last_decl = decls;
11711 }
11712 else
11713 {
11714 TREE_CHAIN (last_decl) = decl;
11715 last_decl = decl;
11716 }
66f2e88d 11717 if (! current_function_decl && TREE_PERMANENT (list_node))
8d08fdba
MS
11718 {
11719 TREE_PURPOSE (list_node) = init;
11720 TREE_VALUE (list_node) = type;
11721 TREE_CHAIN (list_node) = NULL_TREE;
11722 }
11723 else
11724 list_node = saveable_tree_cons (init, type, NULL_TREE);
11725 if (result == NULL_TREE)
11726 {
11727 result = list_node;
11728 last_result = result;
11729 }
11730 else
11731 {
11732 TREE_CHAIN (last_result) = list_node;
11733 last_result = list_node;
11734 }
11735 }
11736 if (last_result)
11737 TREE_CHAIN (last_result) = NULL_TREE;
11738 /* If there are no parameters, and the function does not end
11739 with `...', then last_decl will be NULL_TREE. */
11740 if (last_decl != NULL_TREE)
11741 TREE_CHAIN (last_decl) = NULL_TREE;
11742 }
11743 }
11744
11745 last_function_parms = decls;
11746
8d08fdba
MS
11747 return result;
11748}
42976354
BK
11749
11750/* Called from the parser to update an element of TYPE_ARG_TYPES for some
11751 FUNCTION_TYPE with the newly parsed version of its default argument, which
11752 was previously digested as text. See snarf_defarg et al in lex.c. */
11753
11754void
11755replace_defarg (arg, init)
11756 tree arg, init;
11757{
aa5f3bad 11758 if (! processing_template_decl
42976354 11759 && ! can_convert_arg (TREE_VALUE (arg), TREE_TYPE (init), init))
8251199e 11760 cp_pedwarn ("invalid type `%T' for default argument to `%T'",
42976354
BK
11761 TREE_TYPE (init), TREE_VALUE (arg));
11762 TREE_PURPOSE (arg) = init;
11763}
8d08fdba 11764\f
c11b6f21
MS
11765int
11766copy_args_p (d)
11767 tree d;
11768{
11769 tree t = FUNCTION_ARG_CHAIN (d);
67437d5b
JM
11770 if (DECL_CONSTRUCTOR_P (d)
11771 && TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (d)))
11772 t = TREE_CHAIN (t);
c11b6f21
MS
11773 if (t && TREE_CODE (TREE_VALUE (t)) == REFERENCE_TYPE
11774 && (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_VALUE (t)))
11775 == DECL_CLASS_CONTEXT (d))
11776 && (TREE_CHAIN (t) == NULL_TREE
11777 || TREE_CHAIN (t) == void_list_node
11778 || TREE_PURPOSE (TREE_CHAIN (t))))
11779 return 1;
11780 return 0;
11781}
11782
8d08fdba
MS
11783/* These memoizing functions keep track of special properties which
11784 a class may have. `grok_ctor_properties' notices whether a class
11785 has a constructor of the form X(X&), and also complains
11786 if the class has a constructor of the form X(X).
11787 `grok_op_properties' takes notice of the various forms of
11788 operator= which are defined, as well as what sorts of type conversion
11789 may apply. Both functions take a FUNCTION_DECL as an argument. */
e92cc029 11790
a0a33927 11791int
8d08fdba
MS
11792grok_ctor_properties (ctype, decl)
11793 tree ctype, decl;
11794{
11795 tree parmtypes = FUNCTION_ARG_CHAIN (decl);
11796 tree parmtype = parmtypes ? TREE_VALUE (parmtypes) : void_type_node;
11797
11798 /* When a type has virtual baseclasses, a magical first int argument is
11799 added to any ctor so we can tell if the class has been initialized
11800 yet. This could screw things up in this function, so we deliberately
11801 ignore the leading int if we're in that situation. */
711734a9 11802 if (TYPE_USES_VIRTUAL_BASECLASSES (ctype))
8d08fdba 11803 {
711734a9
JM
11804 my_friendly_assert (parmtypes
11805 && TREE_VALUE (parmtypes) == integer_type_node,
11806 980529);
8d08fdba
MS
11807 parmtypes = TREE_CHAIN (parmtypes);
11808 parmtype = TREE_VALUE (parmtypes);
11809 }
11810
f181d4ae
MM
11811 /* [class.copy]
11812
11813 A non-template constructor for class X is a copy constructor if
11814 its first parameter is of type X&, const X&, volatile X& or const
11815 volatile X&, and either there are no other parameters or else all
11816 other parameters have default arguments. */
8d08fdba 11817 if (TREE_CODE (parmtype) == REFERENCE_TYPE
0b41abe6
JM
11818 && TYPE_MAIN_VARIANT (TREE_TYPE (parmtype)) == ctype
11819 && (TREE_CHAIN (parmtypes) == NULL_TREE
8d08fdba 11820 || TREE_CHAIN (parmtypes) == void_list_node
f181d4ae
MM
11821 || TREE_PURPOSE (TREE_CHAIN (parmtypes)))
11822 && !(DECL_TEMPLATE_INSTANTIATION (decl)
11823 && is_member_template (DECL_TI_TEMPLATE (decl))))
0b41abe6
JM
11824 {
11825 TYPE_HAS_INIT_REF (ctype) = 1;
91063b51 11826 if (CP_TYPE_CONST_P (TREE_TYPE (parmtype)))
0b41abe6 11827 TYPE_HAS_CONST_INIT_REF (ctype) = 1;
8d08fdba 11828 }
f181d4ae
MM
11829 /* [class.copy]
11830
11831 A declaration of a constructor for a class X is ill-formed if its
11832 first parameter is of type (optionally cv-qualified) X and either
11833 there are no other parameters or else all other parameters have
11834 default arguments.
11835
11836 We *don't* complain about member template instantiations that
11837 have this form, though; they can occur as we try to decide what
11838 constructor to use during overload resolution. Since overload
11839 resolution will never prefer such a constructor to the
11840 non-template copy constructor (which is either explicitly or
11841 implicitly defined), there's no need to worry about their
11842 existence. Theoretically, they should never even be
11843 instantiated, but that's hard to forestall. */
0b41abe6 11844 else if (TYPE_MAIN_VARIANT (parmtype) == ctype
59e76fc6
JM
11845 && (TREE_CHAIN (parmtypes) == NULL_TREE
11846 || TREE_CHAIN (parmtypes) == void_list_node
f181d4ae
MM
11847 || TREE_PURPOSE (TREE_CHAIN (parmtypes)))
11848 && !(DECL_TEMPLATE_INSTANTIATION (decl)
11849 && is_member_template (DECL_TI_TEMPLATE (decl))))
8d08fdba 11850 {
8251199e 11851 cp_error ("invalid constructor; you probably meant `%T (const %T&)'",
0b41abe6
JM
11852 ctype, ctype);
11853 SET_IDENTIFIER_ERROR_LOCUS (DECL_NAME (decl), ctype);
11854 return 0;
8d08fdba
MS
11855 }
11856 else if (TREE_CODE (parmtype) == VOID_TYPE
11857 || TREE_PURPOSE (parmtypes) != NULL_TREE)
11858 TYPE_HAS_DEFAULT_CONSTRUCTOR (ctype) = 1;
a0a33927
MS
11859
11860 return 1;
8d08fdba
MS
11861}
11862
11863/* An operator with this name can be either unary or binary. */
e92cc029 11864
a28e3c7f
MS
11865static int
11866ambi_op_p (name)
8d08fdba
MS
11867 tree name;
11868{
11869 return (name == ansi_opname [(int) INDIRECT_REF]
11870 || name == ansi_opname [(int) ADDR_EXPR]
11871 || name == ansi_opname [(int) NEGATE_EXPR]
11872 || name == ansi_opname[(int) POSTINCREMENT_EXPR]
11873 || name == ansi_opname[(int) POSTDECREMENT_EXPR]
11874 || name == ansi_opname [(int) CONVERT_EXPR]);
11875}
11876
11877/* An operator with this name can only be unary. */
e92cc029 11878
a28e3c7f
MS
11879static int
11880unary_op_p (name)
8d08fdba
MS
11881 tree name;
11882{
11883 return (name == ansi_opname [(int) TRUTH_NOT_EXPR]
11884 || name == ansi_opname [(int) BIT_NOT_EXPR]
11885 || name == ansi_opname [(int) COMPONENT_REF]
4c571114 11886 || IDENTIFIER_TYPENAME_P (name));
8d08fdba
MS
11887}
11888
11889/* Do a little sanity-checking on how they declared their operator. */
e92cc029 11890
5566b478 11891void
8d08fdba
MS
11892grok_op_properties (decl, virtualp, friendp)
11893 tree decl;
11894 int virtualp, friendp;
11895{
11896 tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
11897 int methodp = (TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE);
11898 tree name = DECL_NAME (decl);
8d08fdba 11899
a28e3c7f
MS
11900 if (current_class_type == NULL_TREE)
11901 friendp = 1;
8d08fdba 11902
a28e3c7f
MS
11903 if (! friendp)
11904 {
f181d4ae
MM
11905 /* [class.copy]
11906
11907 A user-declared copy assignment operator X::operator= is a
11908 non-static non-template member function of class X with
11909 exactly one parameter of type X, X&, const X&, volatile X& or
11910 const volatile X&. */
11911 if (name == ansi_opname[(int) MODIFY_EXPR]
11912 && !(DECL_TEMPLATE_INSTANTIATION (decl)
11913 && is_member_template (DECL_TI_TEMPLATE (decl))))
6a629cac 11914 ;
a28e3c7f
MS
11915 else if (name == ansi_opname[(int) CALL_EXPR])
11916 TYPE_OVERLOADS_CALL_EXPR (current_class_type) = 1;
11917 else if (name == ansi_opname[(int) ARRAY_REF])
11918 TYPE_OVERLOADS_ARRAY_REF (current_class_type) = 1;
11919 else if (name == ansi_opname[(int) COMPONENT_REF]
11920 || name == ansi_opname[(int) MEMBER_REF])
11921 TYPE_OVERLOADS_ARROW (current_class_type) = 1;
11922 else if (name == ansi_opname[(int) NEW_EXPR])
11923 TYPE_GETS_NEW (current_class_type) |= 1;
11924 else if (name == ansi_opname[(int) DELETE_EXPR])
11925 TYPE_GETS_DELETE (current_class_type) |= 1;
11926 else if (name == ansi_opname[(int) VEC_NEW_EXPR])
11927 TYPE_GETS_NEW (current_class_type) |= 2;
11928 else if (name == ansi_opname[(int) VEC_DELETE_EXPR])
11929 TYPE_GETS_DELETE (current_class_type) |= 2;
11930 }
11931
11932 if (name == ansi_opname[(int) NEW_EXPR]
11933 || name == ansi_opname[(int) VEC_NEW_EXPR])
8d08fdba 11934 {
8d08fdba
MS
11935 /* When the compiler encounters the definition of A::operator new, it
11936 doesn't look at the class declaration to find out if it's static. */
a28e3c7f 11937 if (methodp)
700f8a87 11938 revert_static_member_fn (&decl, NULL, NULL);
8d08fdba
MS
11939
11940 /* Take care of function decl if we had syntax errors. */
11941 if (argtypes == NULL_TREE)
beb53fb8
JM
11942 TREE_TYPE (decl)
11943 = build_function_type (ptr_type_node,
11944 hash_tree_chain (integer_type_node,
11945 void_list_node));
8d08fdba 11946 else
a28e3c7f 11947 TREE_TYPE (decl) = coerce_new_type (TREE_TYPE (decl));
8d08fdba 11948 }
a28e3c7f
MS
11949 else if (name == ansi_opname[(int) DELETE_EXPR]
11950 || name == ansi_opname[(int) VEC_DELETE_EXPR])
8d08fdba 11951 {
a28e3c7f 11952 if (methodp)
700f8a87 11953 revert_static_member_fn (&decl, NULL, NULL);
8d08fdba
MS
11954
11955 if (argtypes == NULL_TREE)
beb53fb8
JM
11956 TREE_TYPE (decl)
11957 = build_function_type (void_type_node,
11958 hash_tree_chain (ptr_type_node,
11959 void_list_node));
8d08fdba 11960 else
a28e3c7f
MS
11961 {
11962 TREE_TYPE (decl) = coerce_delete_type (TREE_TYPE (decl));
11963
11964 if (! friendp && name == ansi_opname[(int) VEC_DELETE_EXPR]
11965 && (TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
11966 != void_list_node))
11967 TYPE_VEC_DELETE_TAKES_SIZE (current_class_type) = 1;
11968 }
8d08fdba 11969 }
8d08fdba
MS
11970 else
11971 {
11972 /* An operator function must either be a non-static member function
11973 or have at least one parameter of a class, a reference to a class,
11974 an enumeration, or a reference to an enumeration. 13.4.0.6 */
700f8a87 11975 if (! methodp || DECL_STATIC_FUNCTION_P (decl))
8d08fdba 11976 {
4c571114 11977 if (IDENTIFIER_TYPENAME_P (name)
8d08fdba
MS
11978 || name == ansi_opname[(int) CALL_EXPR]
11979 || name == ansi_opname[(int) MODIFY_EXPR]
11980 || name == ansi_opname[(int) COMPONENT_REF]
11981 || name == ansi_opname[(int) ARRAY_REF])
8251199e 11982 cp_error ("`%D' must be a nonstatic member function", decl);
8d08fdba
MS
11983 else
11984 {
11985 tree p = argtypes;
11986
700f8a87 11987 if (DECL_STATIC_FUNCTION_P (decl))
8251199e 11988 cp_error ("`%D' must be either a non-static member function or a non-member function", decl);
700f8a87 11989
8d08fdba 11990 if (p)
a1774733 11991 for (; TREE_CODE (TREE_VALUE (p)) != VOID_TYPE ; p = TREE_CHAIN (p))
8d08fdba
MS
11992 {
11993 tree arg = TREE_VALUE (p);
11994 if (TREE_CODE (arg) == REFERENCE_TYPE)
11995 arg = TREE_TYPE (arg);
11996
11997 /* This lets bad template code slip through. */
11998 if (IS_AGGR_TYPE (arg)
11999 || TREE_CODE (arg) == ENUMERAL_TYPE
73b0fce8
KL
12000 || TREE_CODE (arg) == TEMPLATE_TYPE_PARM
12001 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
8d08fdba
MS
12002 goto foundaggr;
12003 }
8251199e
JM
12004 cp_error
12005 ("`%D' must have an argument of class or enumerated type",
8d08fdba
MS
12006 decl);
12007 foundaggr:
12008 ;
12009 }
12010 }
12011
277294d7 12012 if (name == ansi_opname[(int) CALL_EXPR])
2c73f9f5 12013 return; /* No restrictions on args. */
8d08fdba 12014
9a3b49ac 12015 if (IDENTIFIER_TYPENAME_P (name) && ! DECL_TEMPLATE_INFO (decl))
a0a33927
MS
12016 {
12017 tree t = TREE_TYPE (name);
12018 if (TREE_CODE (t) == VOID_TYPE)
8251199e 12019 pedwarn ("void is not a valid type conversion operator");
a0a33927
MS
12020 else if (! friendp)
12021 {
12022 int ref = (TREE_CODE (t) == REFERENCE_TYPE);
d8e178a0 12023 const char *what = 0;
a0a33927
MS
12024 if (ref)
12025 t = TYPE_MAIN_VARIANT (TREE_TYPE (t));
12026
12027 if (t == current_class_type)
12028 what = "the same type";
9a3b49ac 12029 /* Don't force t to be complete here. */
a0a33927 12030 else if (IS_AGGR_TYPE (t)
9a3b49ac 12031 && TYPE_SIZE (t)
a0a33927
MS
12032 && DERIVED_FROM_P (t, current_class_type))
12033 what = "a base class";
12034
12035 if (what)
8251199e 12036 warning ("conversion to %s%s will never use a type conversion operator",
a0a33927
MS
12037 ref ? "a reference to " : "", what);
12038 }
12039 }
12040
8d08fdba
MS
12041 if (name == ansi_opname[(int) MODIFY_EXPR])
12042 {
12043 tree parmtype;
12044
12045 if (list_length (argtypes) != 3 && methodp)
12046 {
8251199e 12047 cp_error ("`%D' must take exactly one argument", decl);
8d08fdba
MS
12048 return;
12049 }
12050 parmtype = TREE_VALUE (TREE_CHAIN (argtypes));
12051
f0e01782 12052 if (copy_assignment_arg_p (parmtype, virtualp)
a28e3c7f 12053 && ! friendp)
8d08fdba
MS
12054 {
12055 TYPE_HAS_ASSIGN_REF (current_class_type) = 1;
f0e01782 12056 if (TREE_CODE (parmtype) != REFERENCE_TYPE
91063b51 12057 || CP_TYPE_CONST_P (TREE_TYPE (parmtype)))
8d08fdba
MS
12058 TYPE_HAS_CONST_ASSIGN_REF (current_class_type) = 1;
12059 }
12060 }
12061 else if (name == ansi_opname[(int) COND_EXPR])
12062 {
12063 /* 13.4.0.3 */
a7a64a77 12064 cp_error ("ANSI C++ prohibits overloading operator ?:");
8d08fdba
MS
12065 }
12066 else if (ambi_op_p (name))
12067 {
12068 if (list_length (argtypes) == 2)
12069 /* prefix */;
12070 else if (list_length (argtypes) == 3)
12071 {
12072 if ((name == ansi_opname[(int) POSTINCREMENT_EXPR]
12073 || name == ansi_opname[(int) POSTDECREMENT_EXPR])
5156628f 12074 && ! processing_template_decl
007e5fea 12075 && ! same_type_p (TREE_VALUE (TREE_CHAIN (argtypes)), integer_type_node))
8d08fdba
MS
12076 {
12077 if (methodp)
8251199e 12078 cp_error ("postfix `%D' must take `int' as its argument",
8d08fdba
MS
12079 decl);
12080 else
8251199e
JM
12081 cp_error
12082 ("postfix `%D' must take `int' as its second argument",
12083 decl);
8d08fdba
MS
12084 }
12085 }
12086 else
12087 {
12088 if (methodp)
8251199e 12089 cp_error ("`%D' must take either zero or one argument", decl);
8d08fdba 12090 else
8251199e 12091 cp_error ("`%D' must take either one or two arguments", decl);
8d08fdba 12092 }
824b9a4c
MS
12093
12094 /* More Effective C++ rule 6. */
eb448459 12095 if (warn_ecpp
824b9a4c
MS
12096 && (name == ansi_opname[(int) POSTINCREMENT_EXPR]
12097 || name == ansi_opname[(int) POSTDECREMENT_EXPR]))
12098 {
12099 tree arg = TREE_VALUE (argtypes);
12100 tree ret = TREE_TYPE (TREE_TYPE (decl));
12101 if (methodp || TREE_CODE (arg) == REFERENCE_TYPE)
12102 arg = TREE_TYPE (arg);
12103 arg = TYPE_MAIN_VARIANT (arg);
12104 if (list_length (argtypes) == 2)
12105 {
12106 if (TREE_CODE (ret) != REFERENCE_TYPE
3bfdc719
MM
12107 || !same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (ret)),
12108 arg))
8251199e 12109 cp_warning ("prefix `%D' should return `%T'", decl,
824b9a4c
MS
12110 build_reference_type (arg));
12111 }
12112 else
12113 {
3bfdc719 12114 if (!same_type_p (TYPE_MAIN_VARIANT (ret), arg))
8251199e 12115 cp_warning ("postfix `%D' should return `%T'", decl, arg);
824b9a4c
MS
12116 }
12117 }
8d08fdba
MS
12118 }
12119 else if (unary_op_p (name))
12120 {
12121 if (list_length (argtypes) != 2)
12122 {
12123 if (methodp)
8251199e 12124 cp_error ("`%D' must take `void'", decl);
8d08fdba 12125 else
8251199e 12126 cp_error ("`%D' must take exactly one argument", decl);
8d08fdba
MS
12127 }
12128 }
12129 else /* if (binary_op_p (name)) */
12130 {
12131 if (list_length (argtypes) != 3)
12132 {
12133 if (methodp)
8251199e 12134 cp_error ("`%D' must take exactly one argument", decl);
8d08fdba 12135 else
8251199e 12136 cp_error ("`%D' must take exactly two arguments", decl);
8d08fdba 12137 }
824b9a4c
MS
12138
12139 /* More Effective C++ rule 7. */
eb448459 12140 if (warn_ecpp
824b9a4c
MS
12141 && (name == ansi_opname [TRUTH_ANDIF_EXPR]
12142 || name == ansi_opname [TRUTH_ORIF_EXPR]
12143 || name == ansi_opname [COMPOUND_EXPR]))
8251199e 12144 cp_warning ("user-defined `%D' always evaluates both arguments",
824b9a4c
MS
12145 decl);
12146 }
12147
12148 /* Effective C++ rule 23. */
eb448459 12149 if (warn_ecpp
824b9a4c
MS
12150 && list_length (argtypes) == 3
12151 && (name == ansi_opname [PLUS_EXPR]
12152 || name == ansi_opname [MINUS_EXPR]
12153 || name == ansi_opname [TRUNC_DIV_EXPR]
12154 || name == ansi_opname [MULT_EXPR])
12155 && TREE_CODE (TREE_TYPE (TREE_TYPE (decl))) == REFERENCE_TYPE)
8251199e 12156 cp_warning ("`%D' should return by value", decl);
8d08fdba
MS
12157
12158 /* 13.4.0.8 */
12159 if (argtypes)
12160 for (; argtypes != void_list_node ; argtypes = TREE_CHAIN (argtypes))
12161 if (TREE_PURPOSE (argtypes))
12162 {
12163 TREE_PURPOSE (argtypes) = NULL_TREE;
12164 if (name == ansi_opname[(int) POSTINCREMENT_EXPR]
12165 || name == ansi_opname[(int) POSTDECREMENT_EXPR])
12166 {
12167 if (pedantic)
8251199e 12168 cp_pedwarn ("`%D' cannot have default arguments", decl);
8d08fdba
MS
12169 }
12170 else
8251199e 12171 cp_error ("`%D' cannot have default arguments", decl);
8d08fdba
MS
12172 }
12173 }
12174}
12175\f
d8e178a0 12176static const char *
094fe153
JM
12177tag_name (code)
12178 enum tag_types code;
12179{
12180 switch (code)
12181 {
12182 case record_type:
12183 return "struct";
12184 case class_type:
12185 return "class";
12186 case union_type:
12187 return "union ";
12188 case enum_type:
12189 return "enum";
12190 case signature_type:
12191 return "signature";
12192 default:
12193 my_friendly_abort (981122);
12194 }
12195}
12196
8d08fdba
MS
12197/* Get the struct, enum or union (CODE says which) with tag NAME.
12198 Define the tag as a forward-reference if it is not defined.
12199
12200 C++: If a class derivation is given, process it here, and report
12201 an error if multiple derivation declarations are not identical.
12202
12203 If this is a definition, come in through xref_tag and only look in
12204 the current frame for the name (since C++ allows new names in any
12205 scope.) */
12206
8d08fdba 12207tree
ca107ded 12208xref_tag (code_type_node, name, globalize)
8d08fdba 12209 tree code_type_node;
ca107ded 12210 tree name;
8d08fdba
MS
12211 int globalize;
12212{
12213 enum tag_types tag_code;
12214 enum tree_code code;
12215 int temp = 0;
8d08fdba 12216 register tree ref, t;
8f032717 12217 struct binding_level *b = current_binding_level;
a80e4195 12218 int got_type = 0;
dc8263bc 12219 tree attributes = NULL_TREE;
25aab5d0 12220 tree context = NULL_TREE;
dc8263bc
JM
12221
12222 /* If we are called from the parser, code_type_node will sometimes be a
12223 TREE_LIST. This indicates that the user wrote
12224 "class __attribute__ ((foo)) bar". Extract the attributes so we can
12225 use them later. */
12226 if (TREE_CODE (code_type_node) == TREE_LIST)
12227 {
12228 attributes = TREE_PURPOSE (code_type_node);
12229 code_type_node = TREE_VALUE (code_type_node);
12230 }
8d08fdba
MS
12231
12232 tag_code = (enum tag_types) TREE_INT_CST_LOW (code_type_node);
12233 switch (tag_code)
12234 {
12235 case record_type:
12236 case class_type:
8d08fdba
MS
12237 case signature_type:
12238 code = RECORD_TYPE;
8d08fdba
MS
12239 break;
12240 case union_type:
12241 code = UNION_TYPE;
8d08fdba
MS
12242 break;
12243 case enum_type:
12244 code = ENUMERAL_TYPE;
12245 break;
12246 default:
12247 my_friendly_abort (18);
12248 }
12249
12250 /* If a cross reference is requested, look up the type
12251 already defined for this tag and return it. */
be99da77
MS
12252 if (TREE_CODE_CLASS (TREE_CODE (name)) == 't')
12253 {
12254 t = name;
a80e4195
MS
12255 name = TYPE_IDENTIFIER (t);
12256 got_type = 1;
be99da77
MS
12257 }
12258 else
12259 t = IDENTIFIER_TYPE_VALUE (name);
61a127b3 12260
73b0fce8
KL
12261 if (t && TREE_CODE (t) != code && TREE_CODE (t) != TEMPLATE_TYPE_PARM
12262 && TREE_CODE (t) != TEMPLATE_TEMPLATE_PARM)
a0a33927
MS
12263 t = NULL_TREE;
12264
8ccc31eb 12265 if (! globalize)
8d08fdba 12266 {
f3400fe2
JM
12267 /* If we know we are defining this tag, only look it up in
12268 this scope and don't try to find it as a type. */
12269 ref = lookup_tag (code, name, b, 1);
8d08fdba
MS
12270 }
12271 else
12272 {
25aab5d0 12273 if (t)
36a117a5 12274 {
25aab5d0
MM
12275 /* [dcl.type.elab] If the identifier resolves to a
12276 typedef-name or a template type-parameter, the
12277 elaborated-type-specifier is ill-formed. */
12278 if (t != TYPE_MAIN_VARIANT (t)
12279 || (CLASS_TYPE_P (t) && TYPE_WAS_ANONYMOUS (t)))
12280 cp_pedwarn ("using typedef-name `%D' after `%s'",
12281 TYPE_NAME (t), tag_name (tag_code));
12282 else if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
12283 cp_error ("using template type parameter `%T' after `%s'",
12284 t, tag_name (tag_code));
12285
12286 ref = t;
12287 }
12288 else
12289 ref = lookup_tag (code, name, b, 0);
36a117a5 12290
25aab5d0
MM
12291 if (! ref)
12292 {
12293 /* Try finding it as a type declaration. If that wins,
12294 use it. */
12295 ref = lookup_name (name, 1);
12296
12297 if (ref != NULL_TREE
12298 && processing_template_decl
12299 && DECL_CLASS_TEMPLATE_P (ref)
12300 && template_class_depth (current_class_type) == 0)
12301 /* Since GLOBALIZE is true, we're declaring a global
6757edfe 12302 template, so we want this type. */
25aab5d0 12303 ref = DECL_RESULT (ref);
6757edfe 12304
25aab5d0
MM
12305 if (ref && TREE_CODE (ref) == TYPE_DECL
12306 && TREE_CODE (TREE_TYPE (ref)) == code)
12307 ref = TREE_TYPE (ref);
12308 else
12309 ref = NULL_TREE;
12310 }
12311
12312 if (ref && current_class_type
12313 && template_class_depth (current_class_type)
12314 && PROCESSING_REAL_TEMPLATE_DECL_P ())
12315 {
12316 /* Since GLOBALIZE is non-zero, we are not looking at a
12317 definition of this tag. Since, in addition, we are currently
12318 processing a (member) template declaration of a template
12319 class, we must be very careful; consider:
12320
12321 template <class X>
12322 struct S1
12323
12324 template <class U>
12325 struct S2
12326 { template <class V>
12327 friend struct S1; };
12328
12329 Here, the S2::S1 declaration should not be confused with the
12330 outer declaration. In particular, the inner version should
12331 have a template parameter of level 2, not level 1. This
12332 would be particularly important if the member declaration
12333 were instead:
12334
12335 template <class V = U> friend struct S1;
12336
12337 say, when we should tsubst into `U' when instantiating
12338 S2. On the other hand, when presented with:
12339
12340 template <class T>
12341 struct S1 {
12342 template <class U>
12343 struct S2 {};
12344 template <class U>
12345 friend struct S2;
12346 };
12347
12348 we must find the inner binding eventually. We
12349 accomplish this by making sure that the new type we
12350 create to represent this declaration has the right
12351 TYPE_CONTEXT. */
12352 context = TYPE_CONTEXT (ref);
12353 ref = NULL_TREE;
8d08fdba
MS
12354 }
12355 }
12356
12357 push_obstacks_nochange ();
12358
12359 if (! ref)
12360 {
12361 /* If no such tag is yet defined, create a forward-reference node
12362 and record it as the "definition".
12363 When a real declaration of this type is found,
12364 the forward-reference will be altered into a real type. */
12365
12366 /* In C++, since these migrate into the global scope, we must
12367 build them on the permanent obstack. */
12368
12369 temp = allocation_temporary_p ();
12370 if (temp)
12371 end_temporary_allocation ();
12372
12373 if (code == ENUMERAL_TYPE)
12374 {
8251199e 12375 cp_error ("use of enum `%#D' without previous declaration", name);
fc378698 12376
8d08fdba
MS
12377 ref = make_node (ENUMERAL_TYPE);
12378
12379 /* Give the type a default layout like unsigned int
12380 to avoid crashing if it does not get defined. */
12381 TYPE_MODE (ref) = TYPE_MODE (unsigned_type_node);
12382 TYPE_ALIGN (ref) = TYPE_ALIGN (unsigned_type_node);
12383 TREE_UNSIGNED (ref) = 1;
12384 TYPE_PRECISION (ref) = TYPE_PRECISION (unsigned_type_node);
12385 TYPE_MIN_VALUE (ref) = TYPE_MIN_VALUE (unsigned_type_node);
12386 TYPE_MAX_VALUE (ref) = TYPE_MAX_VALUE (unsigned_type_node);
12387
12388 /* Enable us to recognize when a type is created in class context.
12389 To do nested classes correctly, this should probably be cleared
12390 out when we leave this classes scope. Currently this in only
12391 done in `start_enum'. */
12392
12393 pushtag (name, ref, globalize);
8d08fdba 12394 }
8d08fdba
MS
12395 else
12396 {
8d08fdba 12397 struct binding_level *old_b = class_binding_level;
8d08fdba
MS
12398
12399 ref = make_lang_type (code);
25aab5d0 12400 TYPE_CONTEXT (ref) = context;
8d08fdba 12401
8d08fdba
MS
12402 if (tag_code == signature_type)
12403 {
12404 SET_SIGNATURE (ref);
f376e137
MS
12405 /* Since a signature type will be turned into the type
12406 of signature tables, it's not only an interface. */
8d08fdba 12407 CLASSTYPE_INTERFACE_ONLY (ref) = 0;
f376e137
MS
12408 SET_CLASSTYPE_INTERFACE_KNOWN (ref);
12409 /* A signature doesn't have a vtable. */
00595019 12410 CLASSTYPE_VTABLE_NEEDS_WRITING (ref) = 0;
8d08fdba
MS
12411 }
12412
8d08fdba
MS
12413#ifdef NONNESTED_CLASSES
12414 /* Class types don't nest the way enums do. */
12415 class_binding_level = (struct binding_level *)0;
12416#endif
12417 pushtag (name, ref, globalize);
12418 class_binding_level = old_b;
8d08fdba
MS
12419 }
12420 }
12421 else
12422 {
8d08fdba 12423 /* If it no longer looks like a nested type, make sure it's
30394414
JM
12424 in global scope.
12425 If it is not an IDENTIFIER, this is not a declaration */
2c73f9f5 12426 if (b->namespace_p && !class_binding_level
30394414
JM
12427 && TREE_CODE (name) == IDENTIFIER_NODE)
12428 {
2c73f9f5
ML
12429 if (IDENTIFIER_NAMESPACE_VALUE (name) == NULL_TREE)
12430 SET_IDENTIFIER_NAMESPACE_VALUE (name, TYPE_NAME (ref));
30394414 12431 }
7fe6899f
MM
12432
12433 if (!globalize && processing_template_decl && IS_AGGR_TYPE (ref))
6757edfe 12434 redeclare_class_template (ref, current_template_parms);
8d08fdba
MS
12435 }
12436
8d08fdba
MS
12437 /* Until the type is defined, tentatively accept whatever
12438 structure tag the user hands us. */
12439 if (TYPE_SIZE (ref) == NULL_TREE
12440 && ref != current_class_type
12441 /* Have to check this, in case we have contradictory tag info. */
12442 && IS_AGGR_TYPE_CODE (TREE_CODE (ref)))
12443 {
12444 if (tag_code == class_type)
12445 CLASSTYPE_DECLARED_CLASS (ref) = 1;
12446 else if (tag_code == record_type || tag_code == signature_type)
12447 CLASSTYPE_DECLARED_CLASS (ref) = 0;
12448 }
12449
12450 pop_obstacks ();
12451
dc8263bc
JM
12452 TREE_TYPE (ref) = attributes;
12453
8d08fdba
MS
12454 return ref;
12455}
8ccc31eb 12456
fc378698
MS
12457tree
12458xref_tag_from_type (old, id, globalize)
12459 tree old, id;
12460 int globalize;
12461{
12462 tree code_type_node;
12463
12464 if (TREE_CODE (old) == RECORD_TYPE)
12465 code_type_node = (CLASSTYPE_DECLARED_CLASS (old)
12466 ? class_type_node : record_type_node);
12467 else
12468 code_type_node = union_type_node;
12469
12470 if (id == NULL_TREE)
12471 id = TYPE_IDENTIFIER (old);
12472
ca107ded 12473 return xref_tag (code_type_node, id, globalize);
fc378698
MS
12474}
12475
3fd71a52
MM
12476/* REF is a type (named NAME), for which we have just seen some
12477 baseclasses. BINFO is a list of those baseclasses; the
12478 TREE_PURPOSE is an access_* node, and the TREE_VALUE is the type of
12479 the base-class. CODE_TYPE_NODE indicates whether REF is a class,
12480 struct, or union. */
12481
8ccc31eb
MS
12482void
12483xref_basetypes (code_type_node, name, ref, binfo)
12484 tree code_type_node;
12485 tree name, ref;
12486 tree binfo;
12487{
12488 /* In the declaration `A : X, Y, ... Z' we mark all the types
12489 (A, X, Y, ..., Z) so we can check for duplicates. */
12490 tree binfos;
d6479fe7
MM
12491 tree base;
12492
8ccc31eb
MS
12493 int i, len;
12494 enum tag_types tag_code = (enum tag_types) TREE_INT_CST_LOW (code_type_node);
12495
12496 if (tag_code == union_type)
12497 {
8251199e 12498 cp_error ("derived union `%T' invalid", ref);
8ccc31eb
MS
12499 return;
12500 }
12501
12502 len = list_length (binfo);
12503 push_obstacks (TYPE_OBSTACK (ref), TYPE_OBSTACK (ref));
12504
d6479fe7
MM
12505 /* First, make sure that any templates in base-classes are
12506 instantiated. This ensures that if we call ourselves recursively
12507 we do not get confused about which classes are marked and which
12508 are not. */
12509 for (base = binfo; base; base = TREE_CHAIN (base))
12510 complete_type (TREE_VALUE (base));
12511
8ccc31eb
MS
12512 SET_CLASSTYPE_MARKED (ref);
12513 BINFO_BASETYPES (TYPE_BINFO (ref)) = binfos = make_tree_vec (len);
12514
12515 for (i = 0; binfo; binfo = TREE_CHAIN (binfo))
12516 {
12517 /* The base of a derived struct is public by default. */
12518 int via_public
be99da77
MS
12519 = (TREE_PURPOSE (binfo) == access_public_node
12520 || TREE_PURPOSE (binfo) == access_public_virtual_node
8ccc31eb 12521 || (tag_code != class_type
be99da77
MS
12522 && (TREE_PURPOSE (binfo) == access_default_node
12523 || TREE_PURPOSE (binfo) == access_default_virtual_node)));
d8b55a76
JM
12524 int via_protected
12525 = (TREE_PURPOSE (binfo) == access_protected_node
12526 || TREE_PURPOSE (binfo) == access_protected_virtual_node);
8ccc31eb 12527 int via_virtual
be99da77 12528 = (TREE_PURPOSE (binfo) == access_private_virtual_node
d8b55a76 12529 || TREE_PURPOSE (binfo) == access_protected_virtual_node
be99da77
MS
12530 || TREE_PURPOSE (binfo) == access_public_virtual_node
12531 || TREE_PURPOSE (binfo) == access_default_virtual_node);
12532 tree basetype = TREE_VALUE (binfo);
8ccc31eb
MS
12533 tree base_binfo;
12534
8ccc31eb
MS
12535 if (basetype && TREE_CODE (basetype) == TYPE_DECL)
12536 basetype = TREE_TYPE (basetype);
5566b478
MS
12537 if (!basetype
12538 || (TREE_CODE (basetype) != RECORD_TYPE
a80e4195 12539 && TREE_CODE (basetype) != TYPENAME_TYPE
73b0fce8
KL
12540 && TREE_CODE (basetype) != TEMPLATE_TYPE_PARM
12541 && TREE_CODE (basetype) != TEMPLATE_TEMPLATE_PARM))
8ccc31eb 12542 {
8251199e 12543 cp_error ("base type `%T' fails to be a struct or class type",
8ccc31eb
MS
12544 TREE_VALUE (binfo));
12545 continue;
12546 }
2b9dc906 12547
11b5139c 12548 GNU_xref_hier (name, basetype, via_public, via_virtual, 0);
2b9dc906 12549
8adf5b5e
JM
12550 /* This code replaces similar code in layout_basetypes.
12551 We put the complete_type first for implicit `typename'. */
d6479fe7 12552 if (TYPE_SIZE (basetype) == NULL_TREE
2b9dc906 12553 && ! (current_template_parms && uses_template_parms (basetype)))
8ccc31eb 12554 {
8251199e 12555 cp_error ("base class `%T' has incomplete type", basetype);
8ccc31eb
MS
12556 continue;
12557 }
8ccc31eb
MS
12558 else
12559 {
12560 if (CLASSTYPE_MARKED (basetype))
12561 {
12562 if (basetype == ref)
8251199e 12563 cp_error ("recursive type `%T' undefined", basetype);
8ccc31eb 12564 else
8251199e 12565 cp_error ("duplicate base type `%T' invalid", basetype);
8ccc31eb
MS
12566 continue;
12567 }
12568
eff71ab0
PB
12569 if (TYPE_FOR_JAVA (basetype)
12570 && current_lang_stack == current_lang_base)
12571 TYPE_FOR_JAVA (ref) = 1;
12572
8ccc31eb
MS
12573 /* Note that the BINFO records which describe individual
12574 inheritances are *not* shared in the lattice! They
12575 cannot be shared because a given baseclass may be
12576 inherited with different `accessibility' by different
12577 derived classes. (Each BINFO record describing an
12578 individual inheritance contains flags which say what
12579 the `accessibility' of that particular inheritance is.) */
12580
7ddedda4
MM
12581 base_binfo
12582 = make_binfo (integer_zero_node, basetype,
12583 CLASS_TYPE_P (basetype)
12584 ? TYPE_BINFO_VTABLE (basetype) : NULL_TREE,
12585 CLASS_TYPE_P (basetype)
12586 ? TYPE_BINFO_VIRTUALS (basetype) : NULL_TREE);
8ccc31eb
MS
12587
12588 TREE_VEC_ELT (binfos, i) = base_binfo;
12589 TREE_VIA_PUBLIC (base_binfo) = via_public;
12590 TREE_VIA_PROTECTED (base_binfo) = via_protected;
12591 TREE_VIA_VIRTUAL (base_binfo) = via_virtual;
12592 BINFO_INHERITANCE_CHAIN (base_binfo) = TYPE_BINFO (ref);
12593
dfbcd65a
JM
12594 /* We need to unshare the binfos now so that lookups during class
12595 definition work. */
12596 unshare_base_binfos (base_binfo);
12597
8ccc31eb 12598 SET_CLASSTYPE_MARKED (basetype);
9e9ff709 12599
8ccc31eb
MS
12600 /* We are free to modify these bits because they are meaningless
12601 at top level, and BASETYPE is a top-level type. */
12602 if (via_virtual || TYPE_USES_VIRTUAL_BASECLASSES (basetype))
12603 {
12604 TYPE_USES_VIRTUAL_BASECLASSES (ref) = 1;
12605 TYPE_USES_COMPLEX_INHERITANCE (ref) = 1;
12606 }
12607
7ddedda4
MM
12608 if (CLASS_TYPE_P (basetype))
12609 {
12610 TYPE_GETS_NEW (ref) |= TYPE_GETS_NEW (basetype);
12611 TYPE_GETS_DELETE (ref) |= TYPE_GETS_DELETE (basetype);
12612 }
12613
8ccc31eb
MS
12614 i += 1;
12615 }
12616 }
12617 if (i)
12618 TREE_VEC_LENGTH (binfos) = i;
12619 else
12620 BINFO_BASETYPES (TYPE_BINFO (ref)) = NULL_TREE;
12621
12622 if (i > 1)
12623 TYPE_USES_MULTIPLE_INHERITANCE (ref) = 1;
12624 else if (i == 1)
7ddedda4
MM
12625 {
12626 tree basetype = BINFO_TYPE (TREE_VEC_ELT (binfos, 0));
12627
12628 if (CLASS_TYPE_P (basetype))
12629 TYPE_USES_MULTIPLE_INHERITANCE (ref)
12630 = TYPE_USES_MULTIPLE_INHERITANCE (basetype);
12631 }
12632
8ccc31eb
MS
12633 if (TYPE_USES_MULTIPLE_INHERITANCE (ref))
12634 TYPE_USES_COMPLEX_INHERITANCE (ref) = 1;
12635
12636 /* Unmark all the types. */
12637 while (--i >= 0)
12638 CLEAR_CLASSTYPE_MARKED (BINFO_TYPE (TREE_VEC_ELT (binfos, i)));
12639 CLEAR_CLASSTYPE_MARKED (ref);
12640
70c532b5
MM
12641 /* Now that we know all the base-classes, set up the list of virtual
12642 bases. */
12643 CLASSTYPE_VBASECLASSES (ref) = get_vbase_types (ref);
12644
8ccc31eb
MS
12645 pop_obstacks ();
12646}
12647
8d08fdba 12648\f
8d08fdba
MS
12649/* Begin compiling the definition of an enumeration type.
12650 NAME is its name (or null if anonymous).
12651 Returns the type object, as yet incomplete.
12652 Also records info about it so that build_enumerator
12653 may be used to declare the individual values as they are read. */
12654
12655tree
12656start_enum (name)
12657 tree name;
12658{
12659 register tree enumtype = NULL_TREE;
8f032717 12660 struct binding_level *b = current_binding_level;
8d08fdba 12661
8c1e8edc 12662 /* We are wasting space here and putting these on the permanent_obstack so
6e687e5e 12663 that typeid(local enum) will work correctly. */
cbf882af 12664 push_obstacks (&permanent_obstack, &permanent_obstack);
b87692e5 12665
8d08fdba
MS
12666 /* If this is the real definition for a previous forward reference,
12667 fill in the contents in the same object that used to be the
12668 forward reference. */
12669
12670 if (name != NULL_TREE)
12671 enumtype = lookup_tag (ENUMERAL_TYPE, name, b, 1);
12672
12673 if (enumtype != NULL_TREE && TREE_CODE (enumtype) == ENUMERAL_TYPE)
30ff8252
NS
12674 {
12675 cp_error ("multiple definition of `%#T'", enumtype);
12676 cp_error_at ("previous definition here", enumtype);
12677 }
8d08fdba
MS
12678 else
12679 {
12680 enumtype = make_node (ENUMERAL_TYPE);
12681 pushtag (name, enumtype, 0);
12682 }
12683
12684 if (current_class_type)
12685 TREE_ADDRESSABLE (b->tags) = 1;
c91a56d2 12686
e92cc029
MS
12687 /* We don't copy this value because build_enumerator needs to do it. */
12688 enum_next_value = integer_zero_node;
7177d104 12689 enum_overflow = 0;
8d08fdba
MS
12690
12691 GNU_xref_decl (current_function_decl, enumtype);
12692 return enumtype;
12693}
12694
12695/* After processing and defining all the values of an enumeration type,
12696 install their decls in the enumeration type and finish it off.
12697 ENUMTYPE is the type object and VALUES a list of name-value pairs.
12698 Returns ENUMTYPE. */
12699
12700tree
dbfe2124
MM
12701finish_enum (enumtype)
12702 tree enumtype;
8d08fdba 12703{
a703fb38 12704 register tree minnode = NULL_TREE, maxnode = NULL_TREE;
8d08fdba
MS
12705 /* Calculate the maximum value of any enumerator in this type. */
12706
dbfe2124 12707 tree values = TYPE_VALUES (enumtype);
8d08fdba
MS
12708 if (values)
12709 {
ed44da02 12710 tree pair;
5566b478 12711
ed44da02 12712 for (pair = values; pair; pair = TREE_CHAIN (pair))
8d08fdba 12713 {
ed44da02
MM
12714 tree decl;
12715 tree value;
12716
12717 /* The TREE_VALUE is a CONST_DECL for this enumeration
12718 constant. */
12719 decl = TREE_VALUE (pair);
12720
ed44da02
MM
12721 /* The DECL_INITIAL will be NULL if we are processing a
12722 template declaration and this enumeration constant had no
12723 explicit initializer. */
12724 value = DECL_INITIAL (decl);
079e1098 12725 if (value && !processing_template_decl)
5566b478 12726 {
079e1098
MM
12727 /* Set the TREE_TYPE for the VALUE as well. That's so
12728 that when we call decl_constant_value we get an
12729 entity of the right type (but with the constant
12730 value). Since we shouldn't ever call
12731 decl_constant_value on a template type, there's no
12732 reason to do that when processing_template_decl.
12733 And, if the expression is something like a
12734 TEMPLATE_PARM_INDEX or a CAST_EXPR doing so will
12735 wreak havoc on the intended type of the expression.
12736
12737 Of course, there's also no point in trying to compute
12738 minimum or maximum values if we're in a template. */
5566b478 12739 TREE_TYPE (value) = enumtype;
ed44da02 12740
079e1098
MM
12741 if (!minnode)
12742 minnode = maxnode = value;
12743 else if (tree_int_cst_lt (maxnode, value))
12744 maxnode = value;
12745 else if (tree_int_cst_lt (value, minnode))
12746 minnode = value;
5566b478 12747 }
ed44da02 12748
72f2bd78
MM
12749 if (processing_template_decl)
12750 /* If this is just a template, leave the CONST_DECL
12751 alone. That way tsubst_copy will find CONST_DECLs for
12752 CONST_DECLs, and not INTEGER_CSTs. */
12753 ;
12754 else
12755 /* In the list we're building up, we want the enumeration
12756 values, not the CONST_DECLs. */
12757 TREE_VALUE (pair) = value;
8d08fdba
MS
12758 }
12759 }
f376e137
MS
12760 else
12761 maxnode = minnode = integer_zero_node;
8d08fdba 12762
de22184b 12763 TYPE_VALUES (enumtype) = nreverse (values);
2986ae00 12764
5156628f 12765 if (processing_template_decl)
b87692e5 12766 {
9360ac70
MM
12767 tree scope = current_scope ();
12768 if (scope && TREE_CODE (scope) == FUNCTION_DECL)
cbf882af 12769 add_tree (build_min (TAG_DEFN, enumtype));
b87692e5 12770 }
cbf882af
MM
12771 else
12772 {
12773 int unsignedp = tree_int_cst_sgn (minnode) >= 0;
12774 int lowprec = min_precision (minnode, unsignedp);
12775 int highprec = min_precision (maxnode, unsignedp);
12776 int precision = MAX (lowprec, highprec);
12777 tree tem;
5566b478 12778
cbf882af 12779 TYPE_SIZE (enumtype) = NULL_TREE;
8d08fdba 12780
cbf882af 12781 /* Set TYPE_MIN_VALUE and TYPE_MAX_VALUE according to `precision'. */
8ccc31eb 12782
cbf882af
MM
12783 TYPE_PRECISION (enumtype) = precision;
12784 if (unsignedp)
12785 fixup_unsigned_type (enumtype);
12786 else
12787 fixup_signed_type (enumtype);
8ccc31eb 12788
cbf882af
MM
12789 if (flag_short_enums || (precision > TYPE_PRECISION (integer_type_node)))
12790 /* Use the width of the narrowest normal C type which is wide
12791 enough. */
12792 TYPE_PRECISION (enumtype) = TYPE_PRECISION (type_for_size
12793 (precision, 1));
12794 else
12795 TYPE_PRECISION (enumtype) = TYPE_PRECISION (integer_type_node);
8d08fdba 12796
cbf882af
MM
12797 TYPE_SIZE (enumtype) = 0;
12798 layout_type (enumtype);
f376e137 12799
cbf882af
MM
12800 /* Fix up all variant types of this enum type. */
12801 for (tem = TYPE_MAIN_VARIANT (enumtype); tem;
12802 tem = TYPE_NEXT_VARIANT (tem))
12803 {
12804 TYPE_VALUES (tem) = TYPE_VALUES (enumtype);
12805 TYPE_MIN_VALUE (tem) = TYPE_MIN_VALUE (enumtype);
12806 TYPE_MAX_VALUE (tem) = TYPE_MAX_VALUE (enumtype);
12807 TYPE_SIZE (tem) = TYPE_SIZE (enumtype);
12808 TYPE_SIZE_UNIT (tem) = TYPE_SIZE_UNIT (enumtype);
12809 TYPE_MODE (tem) = TYPE_MODE (enumtype);
12810 TYPE_PRECISION (tem) = TYPE_PRECISION (enumtype);
12811 TYPE_ALIGN (tem) = TYPE_ALIGN (enumtype);
12812 TREE_UNSIGNED (tem) = TREE_UNSIGNED (enumtype);
12813 }
8d08fdba 12814
cbf882af
MM
12815 /* Finish debugging output for this type. */
12816 rest_of_type_compilation (enumtype, namespace_bindings_p ());
12817 }
12818
12819 /* In start_enum we pushed obstacks. Here, we must pop them. */
12820 pop_obstacks ();
8d08fdba
MS
12821
12822 return enumtype;
12823}
12824
079e1098
MM
12825/* Build and install a CONST_DECL for an enumeration constant of the
12826 enumeration type TYPE whose NAME and VALUE (if any) are provided.
8d08fdba
MS
12827 Assignment of sequential values by default is handled here. */
12828
12829tree
079e1098
MM
12830build_enumerator (name, value, type)
12831 tree name;
12832 tree value;
12833 tree type;
8d08fdba
MS
12834{
12835 tree decl, result;
e8bd800e 12836 tree context;
8d08fdba
MS
12837
12838 /* Remove no-op casts from the value. */
12839 if (value)
12840 STRIP_TYPE_NOPS (value);
12841
5156628f 12842 if (! processing_template_decl)
5566b478
MS
12843 {
12844 /* Validate and default VALUE. */
12845 if (value != NULL_TREE)
12846 {
12847 if (TREE_READONLY_DECL_P (value))
e92cc029 12848 value = decl_constant_value (value);
5566b478
MS
12849
12850 if (TREE_CODE (value) == INTEGER_CST)
12851 {
12852 value = default_conversion (value);
12853 constant_expression_warning (value);
12854 }
12855 else
12856 {
8251199e 12857 cp_error ("enumerator value for `%D' not integer constant", name);
5566b478
MS
12858 value = NULL_TREE;
12859 }
12860 }
12861
5566b478 12862 /* Default based on previous value. */
5156628f 12863 if (value == NULL_TREE && ! processing_template_decl)
5566b478
MS
12864 {
12865 value = enum_next_value;
12866 if (enum_overflow)
8251199e 12867 cp_error ("overflow in enumeration values at `%D'", name);
5566b478
MS
12868 }
12869
12870 /* Remove no-op casts from the value. */
12871 if (value)
12872 STRIP_TYPE_NOPS (value);
013bc8af
MS
12873#if 0
12874 /* To fix MAX_VAL enum consts. (bkoz) */
e92cc029 12875 TREE_TYPE (value) = integer_type_node;
013bc8af 12876#endif
5566b478 12877 }
8d08fdba 12878
079e1098
MM
12879 /* We always have to copy here; not all INTEGER_CSTs are unshared.
12880 Even in other cases, we will later (in finish_enum) be setting the
12881 type of VALUE. */
ed44da02
MM
12882 if (value != NULL_TREE)
12883 value = copy_node (value);
12884
8d08fdba 12885 /* C++ associates enums with global, function, or class declarations. */
e8bd800e
MM
12886
12887 context = current_scope ();
12888 if (context && context == current_class_type)
12889 /* This enum declaration is local to the class. */
079e1098 12890 decl = build_lang_field_decl (CONST_DECL, name, type);
e8bd800e
MM
12891 else
12892 /* It's a global enum, or it's local to a function. (Note local to
12893 a function could mean local to a class method. */
079e1098 12894 decl = build_decl (CONST_DECL, name, type);
e8bd800e
MM
12895
12896 DECL_CONTEXT (decl) = FROB_CONTEXT (context);
12897 DECL_INITIAL (decl) = value;
12898 TREE_READONLY (decl) = 1;
12899
12900 if (context && context == current_class_type)
8f032717
MM
12901 /* In something like `struct S { enum E { i = 7 }; };' we put `i'
12902 on the TYPE_FIELDS list for `S'. (That's so that you can say
12903 things like `S::i' later.) */
12904 finish_member_declaration (decl);
e8bd800e
MM
12905 else
12906 {
12907 pushdecl (decl);
12908 GNU_xref_decl (current_function_decl, decl);
12909 }
8d08fdba 12910
5156628f 12911 if (! processing_template_decl)
5566b478
MS
12912 {
12913 /* Set basis for default for next value. */
12914 enum_next_value = build_binary_op_nodefault (PLUS_EXPR, value,
12915 integer_one_node, PLUS_EXPR);
12916 enum_overflow = tree_int_cst_lt (enum_next_value, value);
5566b478 12917 }
8d08fdba 12918
8d2733ca 12919 result = saveable_tree_cons (name, decl, NULL_TREE);
8d08fdba
MS
12920 return result;
12921}
12922
8d08fdba 12923\f
5566b478
MS
12924static int function_depth;
12925
8d08fdba
MS
12926/* Create the FUNCTION_DECL for a function definition.
12927 DECLSPECS and DECLARATOR are the parts of the declaration;
12928 they describe the function's name and the type it returns,
12929 but twisted together in a fashion that parallels the syntax of C.
12930
f181d4ae
MM
12931 If PRE_PARSED_P is non-zero then DECLARATOR is really the DECL for
12932 the function we are about to process; DECLSPECS are ignored. For
12933 example, we set PRE_PARSED_P when processing the definition of
12934 inline function that was defined in-class; the definition is
12935 actually processed when the class is complete. In this case,
12936 PRE_PARSED_P is 2. We also set PRE_PARSED_P when instanting the
12937 body of a template function, and when constructing thunk functions
12938 and such; in these cases PRE_PARSED_P is 1.
12939
8d08fdba
MS
12940 This function creates a binding context for the function body
12941 as well as setting up the FUNCTION_DECL in current_function_decl.
12942
12943 Returns 1 on success. If the DECLARATOR is not suitable for a function
12944 (it defines a datum instead), we return 0, which tells
12945 yyparse to report a parse error.
12946
12947 For C++, we must first check whether that datum makes any sense.
12948 For example, "class A local_a(1,2);" means that variable local_a
12949 is an aggregate of type A, which should have a constructor
12950 applied to it with the argument list [1, 2].
12951
12952 @@ There is currently no way to retrieve the storage
12953 @@ allocated to FUNCTION (or all of its parms) if we return
12954 @@ something we had previously. */
12955
12956int
c11b6f21
MS
12957start_function (declspecs, declarator, attrs, pre_parsed_p)
12958 tree declspecs, declarator, attrs;
8d08fdba
MS
12959 int pre_parsed_p;
12960{
5566b478 12961 tree decl1;
8d08fdba
MS
12962 tree ctype = NULL_TREE;
12963 tree fntype;
12964 tree restype;
12965 extern int have_extern_spec;
12966 extern int used_extern_spec;
12967 int doing_friend = 0;
12968
8d08fdba 12969 /* Sanity check. */
a1774733 12970 my_friendly_assert (TREE_CODE (TREE_VALUE (void_list_node)) == VOID_TYPE, 160);
8d08fdba
MS
12971 my_friendly_assert (TREE_CHAIN (void_list_node) == NULL_TREE, 161);
12972
e92cc029 12973 /* Assume, until we see it does. */
8d08fdba
MS
12974 current_function_returns_value = 0;
12975 current_function_returns_null = 0;
fd378c9d
JM
12976 named_labels = 0;
12977 shadowed_labels = 0;
8d08fdba
MS
12978 current_function_assigns_this = 0;
12979 current_function_just_assigned_this = 0;
12980 current_function_parms_stored = 0;
12981 original_result_rtx = NULL_RTX;
a9aedbc2 12982 base_init_expr = NULL_TREE;
79ff2c6c
MS
12983 current_base_init_list = NULL_TREE;
12984 current_member_init_list = NULL_TREE;
f30432d7 12985 ctor_label = dtor_label = NULL_TREE;
42976354 12986 static_labelno = 0;
0dde4175 12987 in_function_try_handler = 0;
8d08fdba
MS
12988
12989 clear_temp_name ();
12990
e92cc029 12991 /* This should only be done once on the top most decl. */
8d08fdba
MS
12992 if (have_extern_spec && !used_extern_spec)
12993 {
12994 declspecs = decl_tree_cons (NULL_TREE, get_identifier ("extern"), declspecs);
12995 used_extern_spec = 1;
12996 }
12997
12998 if (pre_parsed_p)
12999 {
13000 decl1 = declarator;
13001
67f7c391
JM
13002#if 0
13003 /* What was this testing for, exactly? */
8d08fdba
MS
13004 if (! DECL_ARGUMENTS (decl1)
13005 && !DECL_STATIC_FUNCTION_P (decl1)
67f7c391 13006 && !DECL_ARTIFICIAL (decl1)
6eb3bb27 13007 && DECL_CLASS_SCOPE_P (decl1)
d2e5ee5c
MS
13008 && TYPE_IDENTIFIER (DECL_CONTEXT (decl1))
13009 && IDENTIFIER_TEMPLATE (TYPE_IDENTIFIER (DECL_CONTEXT (decl1))))
8d08fdba 13010 {
30394414
JM
13011 tree binding = binding_for_name (DECL_NAME (decl1),
13012 current_namespace);
8251199e 13013 cp_error ("redeclaration of `%#D'", decl1);
8d08fdba 13014 if (IDENTIFIER_CLASS_VALUE (DECL_NAME (decl1)))
8251199e 13015 cp_error_at ("previous declaration here", IDENTIFIER_CLASS_VALUE (DECL_NAME (decl1)));
30394414 13016 else if (BINDING_VALUE (binding))
8251199e 13017 cp_error_at ("previous declaration here", BINDING_VALUE (binding));
8d08fdba 13018 }
67f7c391 13019#endif
8d08fdba 13020
8d08fdba
MS
13021 fntype = TREE_TYPE (decl1);
13022 if (TREE_CODE (fntype) == METHOD_TYPE)
13023 ctype = TYPE_METHOD_BASETYPE (fntype);
13024
13025 /* ANSI C++ June 5 1992 WP 11.4.5. A friend function defined in a
13026 class is in the (lexical) scope of the class in which it is
13027 defined. */
13028 if (!ctype && DECL_FRIEND_P (decl1))
13029 {
13030 ctype = DECL_CLASS_CONTEXT (decl1);
13031
13032 /* CTYPE could be null here if we're dealing with a template;
13033 for example, `inline friend float foo()' inside a template
13034 will have no CTYPE set. */
13035 if (ctype && TREE_CODE (ctype) != RECORD_TYPE)
13036 ctype = NULL_TREE;
13037 else
13038 doing_friend = 1;
13039 }
13040
5566b478
MS
13041 last_function_parms = DECL_ARGUMENTS (decl1);
13042 last_function_parm_tags = NULL_TREE;
8d08fdba
MS
13043 }
13044 else
13045 {
c11b6f21 13046 decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, 1, NULL_TREE);
8d08fdba
MS
13047 /* If the declarator is not suitable for a function definition,
13048 cause a syntax error. */
13049 if (decl1 == NULL_TREE || TREE_CODE (decl1) != FUNCTION_DECL) return 0;
13050
13051 fntype = TREE_TYPE (decl1);
13052
13053 restype = TREE_TYPE (fntype);
7ddedda4 13054 if (CLASS_TYPE_P (restype) && !CLASSTYPE_GOT_SEMICOLON (restype))
8d08fdba 13055 {
8251199e 13056 cp_error ("semicolon missing after declaration of `%#T'", restype);
e66d884e 13057 shadow_tag (build_expr_list (NULL_TREE, restype));
8d08fdba
MS
13058 CLASSTYPE_GOT_SEMICOLON (restype) = 1;
13059 if (TREE_CODE (fntype) == FUNCTION_TYPE)
13060 fntype = build_function_type (integer_type_node,
13061 TYPE_ARG_TYPES (fntype));
13062 else
13063 fntype = build_cplus_method_type (build_type_variant (TYPE_METHOD_BASETYPE (fntype), TREE_READONLY (decl1), TREE_SIDE_EFFECTS (decl1)),
13064 integer_type_node,
13065 TYPE_ARG_TYPES (fntype));
13066 TREE_TYPE (decl1) = fntype;
13067 }
13068
13069 if (TREE_CODE (fntype) == METHOD_TYPE)
13070 ctype = TYPE_METHOD_BASETYPE (fntype);
35680744 13071 else if (DECL_MAIN_P (decl1))
8d08fdba
MS
13072 {
13073 /* If this doesn't return integer_type, complain. */
13074 if (TREE_TYPE (TREE_TYPE (decl1)) != integer_type_node)
13075 {
a28e3c7f 13076 if (pedantic || warn_return_type)
8251199e 13077 pedwarn ("return type for `main' changed to `int'");
8d08fdba
MS
13078 TREE_TYPE (decl1) = fntype = default_function_type;
13079 }
8d08fdba
MS
13080 }
13081 }
13082
13083 /* Warn if function was previously implicitly declared
13084 (but not if we warned then). */
13085 if (! warn_implicit
13086 && IDENTIFIER_IMPLICIT_DECL (DECL_NAME (decl1)) != NULL_TREE)
8251199e 13087 cp_warning_at ("`%D' implicitly declared before its definition", IDENTIFIER_IMPLICIT_DECL (DECL_NAME (decl1)));
8d08fdba 13088
f181d4ae
MM
13089 announce_function (decl1);
13090
13091 /* Set up current_class_type, and enter the scope of the class, if
13092 appropriate. */
13093 if (ctype)
13094 push_nested_class (ctype, 1);
13095 else if (DECL_STATIC_FUNCTION_P (decl1))
13096 push_nested_class (DECL_CONTEXT (decl1), 2);
13097
13098 /* Now that we have entered the scope of the class, we must restore
13099 the bindings for any template parameters surrounding DECL1, if it
13100 is an inline member template. (Order is important; consider the
13101 case where a template parameter has the same name as a field of
13102 the class.) It is not until after this point that
13103 PROCESSING_TEMPLATE_DECL is guaranteed to be set up correctly. */
13104 if (pre_parsed_p == 2)
13105 maybe_begin_member_template_processing (decl1);
13106
13107 /* We are now in the scope of the function being defined. */
8d08fdba 13108 current_function_decl = decl1;
f181d4ae 13109
5566b478
MS
13110 /* Save the parm names or decls from this function's declarator
13111 where store_parm_decls will find them. */
13112 current_function_parms = last_function_parms;
13113 current_function_parm_tags = last_function_parm_tags;
8d08fdba 13114
5156628f 13115 if (! processing_template_decl)
8d08fdba 13116 {
d490621d
MM
13117 /* In a function definition, arg types must be complete. */
13118 require_complete_types_for_parms (current_function_parms);
f181d4ae 13119
5566b478
MS
13120 if (TYPE_SIZE (complete_type (TREE_TYPE (fntype))) == NULL_TREE)
13121 {
8251199e 13122 cp_error ("return-type `%#T' is an incomplete type",
5566b478 13123 TREE_TYPE (fntype));
8d08fdba 13124
5566b478
MS
13125 /* Make it return void instead, but don't change the
13126 type of the DECL_RESULT, in case we have a named return value. */
13127 if (ctype)
13128 TREE_TYPE (decl1)
13129 = build_cplus_method_type (build_type_variant (ctype,
13130 TREE_READONLY (decl1),
13131 TREE_SIDE_EFFECTS (decl1)),
13132 void_type_node,
13133 FUNCTION_ARG_CHAIN (decl1));
13134 else
13135 TREE_TYPE (decl1)
13136 = build_function_type (void_type_node,
13137 TYPE_ARG_TYPES (TREE_TYPE (decl1)));
13138 DECL_RESULT (decl1)
13139 = build_decl (RESULT_DECL, 0, TYPE_MAIN_VARIANT (TREE_TYPE (fntype)));
91063b51
MM
13140 TREE_READONLY (DECL_RESULT (decl1))
13141 = CP_TYPE_CONST_P (TREE_TYPE (fntype));
13142 TREE_THIS_VOLATILE (DECL_RESULT (decl1))
13143 = CP_TYPE_VOLATILE_P (TREE_TYPE (fntype));
5566b478
MS
13144 }
13145
a7a64a77 13146 abstract_virtuals_error (decl1, TREE_TYPE (fntype));
5566b478 13147 }
b7484fbe 13148
824b9a4c 13149 /* Effective C++ rule 15. See also c_expand_return. */
eb448459 13150 if (warn_ecpp
824b9a4c 13151 && DECL_NAME (decl1) == ansi_opname[(int) MODIFY_EXPR]
a1774733 13152 && TREE_CODE (TREE_TYPE (fntype)) == VOID_TYPE)
8251199e 13153 cp_warning ("`operator=' should return a reference to `*this'");
824b9a4c 13154
2a2480e1
JM
13155 /* Make the init_value nonzero so pushdecl knows this is not tentative.
13156 error_mark_node is replaced below (in poplevel) with the BLOCK. */
13157 DECL_INITIAL (decl1) = error_mark_node;
13158
3a846e6e 13159#ifdef SET_DEFAULT_DECL_ATTRIBUTES
76fb7947 13160 SET_DEFAULT_DECL_ATTRIBUTES (decl1, attrs);
3a846e6e
NC
13161#endif
13162
8d08fdba
MS
13163 /* This function exists in static storage.
13164 (This does not mean `static' in the C sense!) */
13165 TREE_STATIC (decl1) = 1;
13166
36a117a5
MM
13167 /* We must call push_template_decl after current_class_type is set
13168 up. (If we are processing inline definitions after exiting a
13169 class scope, current_class_type will be NULL_TREE until set above
13170 by push_nested_class.) */
13171 if (processing_template_decl)
13172 decl1 = push_template_decl (decl1);
13173
878cd289
MS
13174 /* Record the decl so that the function name is defined.
13175 If we already have a decl for this name, and it is a FUNCTION_DECL,
13176 use the old decl. */
36a117a5 13177 if (!processing_template_decl && pre_parsed_p == 0)
878cd289 13178 {
75650646 13179 /* A specialization is not used to guide overload resolution. */
959d8796
JM
13180 if ((flag_guiding_decls
13181 || !DECL_TEMPLATE_SPECIALIZATION (decl1))
13182 && ! DECL_FUNCTION_MEMBER_P (decl1))
75650646 13183 decl1 = pushdecl (decl1);
2c73f9f5 13184 else
b7698cf0
JM
13185 {
13186 /* We need to set the DECL_CONTEXT. */
13187 if (!DECL_CONTEXT (decl1) && DECL_TEMPLATE_INFO (decl1))
13188 DECL_CONTEXT (decl1) = DECL_CONTEXT (DECL_TI_TEMPLATE (decl1));
13189 /* And make sure we have enough default args. */
13190 check_default_args (decl1);
13191 }
878cd289
MS
13192 DECL_MAIN_VARIANT (decl1) = decl1;
13193 fntype = TREE_TYPE (decl1);
13194 }
5566b478
MS
13195
13196 current_function_decl = decl1;
878cd289 13197
db5ae43f 13198 if (DECL_INTERFACE_KNOWN (decl1))
faae18ab 13199 {
86052cc3
JM
13200 tree ctx = hack_decl_function_context (decl1);
13201
faae18ab
MS
13202 if (DECL_NOT_REALLY_EXTERN (decl1))
13203 DECL_EXTERNAL (decl1) = 0;
86052cc3
JM
13204
13205 if (ctx != NULL_TREE && DECL_THIS_INLINE (ctx)
13206 && TREE_PUBLIC (ctx))
13207 /* This is a function in a local class in an extern inline
13208 function. */
13209 comdat_linkage (decl1);
faae18ab 13210 }
8d08fdba
MS
13211 /* If this function belongs to an interface, it is public.
13212 If it belongs to someone else's interface, it is also external.
1f901793 13213 This only affects inlines and template instantiations. */
5566b478
MS
13214 else if (interface_unknown == 0
13215 && (! DECL_TEMPLATE_INSTANTIATION (decl1)
d11ad92e 13216 || flag_alt_external_templates))
8d08fdba 13217 {
5566b478 13218 if (DECL_THIS_INLINE (decl1) || DECL_TEMPLATE_INSTANTIATION (decl1)
5156628f 13219 || processing_template_decl)
1f901793
JM
13220 {
13221 DECL_EXTERNAL (decl1)
13222 = (interface_only
9c73ec84
MS
13223 || (DECL_THIS_INLINE (decl1) && ! flag_implement_inlines
13224 && !DECL_VINDEX (decl1)));
1f901793
JM
13225
13226 /* For WIN32 we also want to put these in linkonce sections. */
13227 maybe_make_one_only (decl1);
13228 }
db5ae43f 13229 else
893de33c 13230 DECL_EXTERNAL (decl1) = 0;
e8abc66f 13231 DECL_NOT_REALLY_EXTERN (decl1) = 0;
db5ae43f 13232 DECL_INTERFACE_KNOWN (decl1) = 1;
8d08fdba 13233 }
c16c47fb
JM
13234 else if (interface_unknown && interface_only
13235 && (! DECL_TEMPLATE_INSTANTIATION (decl1)
13236 || flag_alt_external_templates))
13237 {
13238 /* If MULTIPLE_SYMBOL_SPACES is defined and we saw a #pragma
13239 interface, we will have interface_only set but not
13240 interface_known. In that case, we don't want to use the normal
13241 heuristics because someone will supply a #pragma implementation
13242 elsewhere, and deducing it here would produce a conflict. */
13243 comdat_linkage (decl1);
13244 DECL_EXTERNAL (decl1) = 0;
13245 DECL_INTERFACE_KNOWN (decl1) = 1;
13246 DECL_DEFER_OUTPUT (decl1) = 1;
13247 }
8d08fdba 13248 else
a0a33927
MS
13249 {
13250 /* This is a definition, not a reference.
b7484fbe
MS
13251 So clear DECL_EXTERNAL. */
13252 DECL_EXTERNAL (decl1) = 0;
faae18ab 13253
5566b478
MS
13254 if ((DECL_THIS_INLINE (decl1) || DECL_TEMPLATE_INSTANTIATION (decl1))
13255 && ! DECL_INTERFACE_KNOWN (decl1)
13256 /* Don't try to defer nested functions for now. */
e76a2646 13257 && ! hack_decl_function_context (decl1))
878cd289
MS
13258 DECL_DEFER_OUTPUT (decl1) = 1;
13259 else
893de33c 13260 DECL_INTERFACE_KNOWN (decl1) = 1;
db5ae43f 13261 }
a9aedbc2 13262
8d08fdba
MS
13263 if (ctype != NULL_TREE && DECL_STATIC_FUNCTION_P (decl1))
13264 {
13265 if (TREE_CODE (fntype) == METHOD_TYPE)
13266 TREE_TYPE (decl1) = fntype
13267 = build_function_type (TREE_TYPE (fntype),
13268 TREE_CHAIN (TYPE_ARG_TYPES (fntype)));
5566b478
MS
13269 current_function_parms = TREE_CHAIN (current_function_parms);
13270 DECL_ARGUMENTS (decl1) = current_function_parms;
8d08fdba
MS
13271 ctype = NULL_TREE;
13272 }
13273 restype = TREE_TYPE (fntype);
13274
8d08fdba
MS
13275 if (ctype)
13276 {
8d08fdba 13277 /* If we're compiling a friend function, neither of the variables
4ac14744 13278 current_class_ptr nor current_class_type will have values. */
8d08fdba
MS
13279 if (! doing_friend)
13280 {
13281 /* We know that this was set up by `grokclassfn'.
13282 We do not wait until `store_parm_decls', since evil
13283 parse errors may never get us to that point. Here
13284 we keep the consistency between `current_class_type'
4ac14744 13285 and `current_class_ptr'. */
5566b478 13286 tree t = current_function_parms;
5f34005f
RK
13287
13288 my_friendly_assert (t != NULL_TREE
13289 && TREE_CODE (t) == PARM_DECL, 162);
13290
dff6b454
RK
13291 if (TREE_CODE (TREE_TYPE (t)) == POINTER_TYPE)
13292 {
7ddedda4 13293 int i;
dff6b454 13294
7ddedda4
MM
13295 if (! hack_decl_function_context (decl1))
13296 temporary_allocation ();
13297 i = suspend_momentary ();
13298
13299 /* Normally, build_indirect_ref returns
13300 current_class_ref whenever current_class_ptr is
13301 dereferenced. This time, however, we want it to
13302 *create* current_class_ref, so we temporarily clear
13303 current_class_ptr to fool it. */
4ac14744
MS
13304 current_class_ptr = NULL_TREE;
13305 current_class_ref = build_indirect_ref (t, NULL_PTR);
13306 current_class_ptr = t;
7ddedda4 13307
dff6b454 13308 resume_momentary (i);
7ddedda4
MM
13309 if (! hack_decl_function_context (decl1))
13310 end_temporary_allocation ();
dff6b454
RK
13311 }
13312 else
13313 /* We're having a signature pointer here. */
4ac14744 13314 current_class_ref = current_class_ptr = t;
dff6b454 13315
8d08fdba
MS
13316 }
13317 }
13318 else
7ddedda4 13319 current_class_ptr = current_class_ref = NULL_TREE;
8d08fdba 13320
db5ae43f
MS
13321 pushlevel (0);
13322 current_binding_level->parm_flag = 1;
13323
db5ae43f
MS
13324 GNU_xref_function (decl1, current_function_parms);
13325
f30432d7
MS
13326 if (attrs)
13327 cplus_decl_attributes (decl1, NULL_TREE, attrs);
3a846e6e 13328
db5ae43f
MS
13329 make_function_rtl (decl1);
13330
8d08fdba
MS
13331 /* Promote the value to int before returning it. */
13332 if (C_PROMOTING_INTEGER_TYPE_P (restype))
5566b478
MS
13333 restype = type_promotes_to (restype);
13334
13335 /* If this fcn was already referenced via a block-scope `extern' decl
13336 (or an implicit decl), propagate certain information about the usage. */
13337 if (TREE_ADDRESSABLE (DECL_ASSEMBLER_NAME (decl1)))
13338 TREE_ADDRESSABLE (decl1) = 1;
13339
8d08fdba 13340 if (DECL_RESULT (decl1) == NULL_TREE)
f30432d7
MS
13341 {
13342 DECL_RESULT (decl1)
13343 = build_decl (RESULT_DECL, 0, TYPE_MAIN_VARIANT (restype));
91063b51
MM
13344 TREE_READONLY (DECL_RESULT (decl1)) = CP_TYPE_CONST_P (restype);
13345 TREE_THIS_VOLATILE (DECL_RESULT (decl1)) = CP_TYPE_VOLATILE_P (restype);
f30432d7 13346 }
8d08fdba 13347
948edfa7
JW
13348 /* Allocate further tree nodes temporarily during compilation
13349 of this function only. Tiemann moved up here from bottom of fn. */
13350 /* If this is a nested function, then we must continue to allocate RTL
13351 on the permanent obstack in case we need to inline it later. */
13352 if (! hack_decl_function_context (decl1))
5566b478
MS
13353 temporary_allocation ();
13354
5156628f 13355 if (processing_template_decl)
5566b478 13356 {
5566b478
MS
13357 ++minimal_parse_mode;
13358 last_tree = DECL_SAVED_TREE (decl1)
13359 = build_nt (EXPR_STMT, void_zero_node);
13360 }
13361
13362 ++function_depth;
13363
f30432d7
MS
13364 if (DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (decl1))
13365 && DECL_LANGUAGE (decl1) == lang_cplusplus)
8d08fdba
MS
13366 {
13367 dtor_label = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE);
13368 ctor_label = NULL_TREE;
13369 }
13370 else
13371 {
13372 dtor_label = NULL_TREE;
13373 if (DECL_CONSTRUCTOR_P (decl1))
13374 ctor_label = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE);
13375 }
13376
8d08fdba
MS
13377 return 1;
13378}
13379\f
9664b89e
JM
13380/* Called after store_parm_decls for a function-try-block. We need to update
13381 last_parm_cleanup_insn so that the base initializers for a constructor
13382 are run within this block, not before it. */
6467930b
MS
13383
13384void
13385expand_start_early_try_stmts ()
13386{
6467930b 13387 expand_start_try_stmts ();
9664b89e 13388 last_parm_cleanup_insn = get_last_insn ();
f30432d7
MS
13389}
13390
8d08fdba
MS
13391/* Store the parameter declarations into the current function declaration.
13392 This is called after parsing the parameter declarations, before
13393 digesting the body of the function.
13394
13395 Also install to binding contour return value identifier, if any. */
13396
13397void
13398store_parm_decls ()
13399{
13400 register tree fndecl = current_function_decl;
13401 register tree parm;
13402 int parms_have_cleanups = 0;
eb66be0e 13403 tree cleanups = NULL_TREE;
8d08fdba
MS
13404
13405 /* This is either a chain of PARM_DECLs (when a prototype is used). */
13406 tree specparms = current_function_parms;
13407
13408 /* This is a list of types declared among parms in a prototype. */
13409 tree parmtags = current_function_parm_tags;
13410
13411 /* This is a chain of any other decls that came in among the parm
13412 declarations. If a parm is declared with enum {foo, bar} x;
13413 then CONST_DECLs for foo and bar are put here. */
13414 tree nonparms = NULL_TREE;
13415
a9aedbc2 13416 if (toplevel_bindings_p ())
8d08fdba
MS
13417 fatal ("parse errors have confused me too much");
13418
13419 /* Initialize RTL machinery. */
13420 init_function_start (fndecl, input_filename, lineno);
13421
8d08fdba
MS
13422 /* Create a binding level for the parms. */
13423 expand_start_bindings (0);
13424
8d08fdba
MS
13425 if (specparms != NULL_TREE)
13426 {
13427 /* This case is when the function was defined with an ANSI prototype.
13428 The parms already have decls, so we need not do anything here
13429 except record them as in effect
13430 and complain if any redundant old-style parm decls were written. */
13431
13432 register tree next;
13433
13434 /* Must clear this because it might contain TYPE_DECLs declared
13435 at class level. */
13436 storedecls (NULL_TREE);
5566b478 13437
8d08fdba
MS
13438 for (parm = nreverse (specparms); parm; parm = next)
13439 {
13440 next = TREE_CHAIN (parm);
13441 if (TREE_CODE (parm) == PARM_DECL)
13442 {
5566b478 13443 tree cleanup;
8d08fdba
MS
13444 if (DECL_NAME (parm) == NULL_TREE)
13445 {
8d08fdba 13446 pushdecl (parm);
8d08fdba 13447 }
a1774733 13448 else if (TREE_CODE (TREE_TYPE (parm)) == VOID_TYPE)
8251199e 13449 cp_error ("parameter `%D' declared void", parm);
8d08fdba
MS
13450 else
13451 {
13452 /* Now fill in DECL_REFERENCE_SLOT for any of the parm decls.
13453 A parameter is assumed not to have any side effects.
13454 If this should change for any reason, then this
13455 will have to wrap the bashed reference type in a save_expr.
13456
13457 Also, if the parameter type is declared to be an X
13458 and there is an X(X&) constructor, we cannot lay it
13459 into the stack (any more), so we make this parameter
13460 look like it is really of reference type. Functions
13461 which pass parameters to this function will know to
13462 create a temporary in their frame, and pass a reference
13463 to that. */
13464
13465 if (TREE_CODE (TREE_TYPE (parm)) == REFERENCE_TYPE
13466 && TYPE_SIZE (TREE_TYPE (TREE_TYPE (parm))))
13467 SET_DECL_REFERENCE_SLOT (parm, convert_from_reference (parm));
13468
13469 pushdecl (parm);
13470 }
5156628f 13471 if (! processing_template_decl
5566b478 13472 && (cleanup = maybe_build_cleanup (parm), cleanup))
8d08fdba
MS
13473 {
13474 expand_decl (parm);
8d08fdba 13475 parms_have_cleanups = 1;
eb66be0e
MS
13476
13477 /* Keep track of the cleanups. */
13478 cleanups = tree_cons (parm, cleanup, cleanups);
8d08fdba
MS
13479 }
13480 }
13481 else
13482 {
13483 /* If we find an enum constant or a type tag,
13484 put it aside for the moment. */
13485 TREE_CHAIN (parm) = NULL_TREE;
13486 nonparms = chainon (nonparms, parm);
13487 }
13488 }
13489
13490 /* Get the decls in their original chain order
13491 and record in the function. This is all and only the
13492 PARM_DECLs that were pushed into scope by the loop above. */
13493 DECL_ARGUMENTS (fndecl) = getdecls ();
13494
13495 storetags (chainon (parmtags, gettags ()));
13496 }
13497 else
13498 DECL_ARGUMENTS (fndecl) = NULL_TREE;
13499
13500 /* Now store the final chain of decls for the arguments
13501 as the decl-chain of the current lexical scope.
13502 Put the enumerators in as well, at the front so that
13503 DECL_ARGUMENTS is not modified. */
13504
13505 storedecls (chainon (nonparms, DECL_ARGUMENTS (fndecl)));
13506
f181d4ae
MM
13507 /* Declare __FUNCTION__ and __PRETTY_FUNCTION__ for this function. */
13508 declare_function_name ();
13509
8d08fdba 13510 /* Initialize the RTL code for the function. */
49ad7cfa 13511 DECL_SAVED_INSNS (fndecl) = 0;
5156628f 13512 if (! processing_template_decl)
5566b478 13513 expand_function_start (fndecl, parms_have_cleanups);
8d08fdba 13514
eb448459
MS
13515 current_function_parms_stored = 1;
13516
13517 /* If this function is `main', emit a call to `__main'
13518 to run global initializers, etc. */
35680744
MM
13519 if (DECL_MAIN_P (fndecl))
13520 expand_main_function ();
eb448459 13521
eb66be0e
MS
13522 /* Now that we have initialized the parms, we can start their
13523 cleanups. We cannot do this before, since expand_decl_cleanup
13524 should not be called before the parm can be used. */
eb448459 13525 if (cleanups
eb66be0e
MS
13526 && ! processing_template_decl)
13527 {
13528 for (cleanups = nreverse (cleanups); cleanups; cleanups = TREE_CHAIN (cleanups))
13529 {
13530 if (! expand_decl_cleanup (TREE_PURPOSE (cleanups), TREE_VALUE (cleanups)))
8251199e 13531 cp_error ("parser lost in parsing declaration of `%D'",
eb66be0e
MS
13532 TREE_PURPOSE (cleanups));
13533 }
13534 }
13535
8d08fdba
MS
13536 /* Create a binding contour which can be used to catch
13537 cleanup-generated temporaries. Also, if the return value needs or
13538 has initialization, deal with that now. */
13539 if (parms_have_cleanups)
13540 {
13541 pushlevel (0);
13542 expand_start_bindings (0);
13543 }
13544
5156628f 13545 if (! processing_template_decl && flag_exceptions)
f30432d7 13546 {
f30432d7
MS
13547 /* Do the starting of the exception specifications, if we have any. */
13548 if (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (current_function_decl)))
13549 expand_start_eh_spec ();
f30432d7 13550 }
eb448459 13551
5c825fc2 13552 last_parm_cleanup_insn = get_last_insn ();
72b7eeff 13553 last_dtor_insn = get_last_insn ();
8d08fdba
MS
13554}
13555
13556/* Bind a name and initialization to the return value of
13557 the current function. */
e92cc029 13558
8d08fdba
MS
13559void
13560store_return_init (return_id, init)
13561 tree return_id, init;
13562{
13563 tree decl = DECL_RESULT (current_function_decl);
13564
e1cd6e56 13565 if (pedantic)
8d08fdba
MS
13566 /* Give this error as many times as there are occurrences,
13567 so that users can use Emacs compilation buffers to find
13568 and fix all such places. */
8251199e 13569 pedwarn ("ANSI C++ does not permit named return values");
8d08fdba
MS
13570
13571 if (return_id != NULL_TREE)
13572 {
13573 if (DECL_NAME (decl) == NULL_TREE)
13574 {
13575 DECL_NAME (decl) = return_id;
13576 DECL_ASSEMBLER_NAME (decl) = return_id;
13577 }
13578 else
8251199e 13579 cp_error ("return identifier `%D' already in place", decl);
8d08fdba
MS
13580 }
13581
13582 /* Can't let this happen for constructors. */
13583 if (DECL_CONSTRUCTOR_P (current_function_decl))
13584 {
8251199e 13585 error ("can't redefine default return value for constructors");
8d08fdba
MS
13586 return;
13587 }
13588
13589 /* If we have a named return value, put that in our scope as well. */
13590 if (DECL_NAME (decl) != NULL_TREE)
13591 {
13592 /* If this named return value comes in a register,
13593 put it in a pseudo-register. */
13594 if (DECL_REGISTER (decl))
13595 {
13596 original_result_rtx = DECL_RTL (decl);
13597 DECL_RTL (decl) = gen_reg_rtx (DECL_MODE (decl));
13598 }
13599
82580166 13600 /* Let `cp_finish_decl' know that this initializer is ok. */
8d08fdba
MS
13601 DECL_INITIAL (decl) = init;
13602 pushdecl (decl);
e76a2646
MS
13603
13604 if (minimal_parse_mode)
13605 add_tree (build_min_nt (RETURN_INIT, return_id,
13606 copy_to_permanent (init)));
13607 else
dae1d6f6 13608 cp_finish_decl (decl, init, NULL_TREE, 0, 0);
8d08fdba
MS
13609 }
13610}
13611
8d08fdba
MS
13612\f
13613/* Finish up a function declaration and compile that function
13614 all the way to assembler language output. The free the storage
13615 for the function definition.
13616
13617 This is called after parsing the body of the function definition.
13618 LINENO is the current line number.
13619
f181d4ae
MM
13620 FLAGS is a bitwise or of the following values:
13621 1 - CALL_POPLEVEL
13622 An extra call to poplevel (and expand_end_bindings) must be
13623 made to take care of the binding contour for the base
13624 initializers. This is only relevant for constructors.
13625 2 - INCLASS_INLINE
13626 We just finished processing the body of an in-class inline
13627 function definition. (This processing will have taken place
13628 after the class definition is complete.)
e92cc029
MS
13629
13630 NESTED is nonzero if we were in the middle of compiling another function
13631 when we started on this one. */
8d08fdba
MS
13632
13633void
f181d4ae 13634finish_function (lineno, flags, nested)
8d08fdba 13635 int lineno;
f181d4ae 13636 int flags;
db5ae43f 13637 int nested;
8d08fdba
MS
13638{
13639 register tree fndecl = current_function_decl;
13640 tree fntype, ctype = NULL_TREE;
49ad7cfa 13641 rtx fn_last_parm_insn, insns;
8d08fdba
MS
13642 /* Label to use if this function is supposed to return a value. */
13643 tree no_return_label = NULL_TREE;
13644 tree decls = NULL_TREE;
f181d4ae
MM
13645 int call_poplevel = (flags & 1) != 0;
13646 int inclass_inline = (flags & 2) != 0;
13647 int in_template;
8d08fdba
MS
13648
13649 /* When we get some parse errors, we can end up without a
13650 current_function_decl, so cope. */
13651 if (fndecl == NULL_TREE)
13652 return;
13653
0aafb128 13654 if (function_depth > 1)
e92cc029
MS
13655 nested = 1;
13656
8d08fdba
MS
13657 fntype = TREE_TYPE (fndecl);
13658
13659/* TREE_READONLY (fndecl) = 1;
13660 This caused &foo to be of type ptr-to-const-function
13661 which then got a warning when stored in a ptr-to-function variable. */
13662
13663 /* This happens on strange parse errors. */
13664 if (! current_function_parms_stored)
13665 {
13666 call_poplevel = 0;
13667 store_parm_decls ();
13668 }
13669
5156628f 13670 if (processing_template_decl)
8d08fdba 13671 {
5566b478
MS
13672 if (DECL_CONSTRUCTOR_P (fndecl) && call_poplevel)
13673 {
13674 decls = getdecls ();
13675 expand_end_bindings (decls, decls != NULL_TREE, 0);
13676 poplevel (decls != NULL_TREE, 0, 0);
13677 }
13678 }
13679 else
13680 {
13681 if (write_symbols != NO_DEBUG /*&& TREE_CODE (fntype) != METHOD_TYPE*/)
8d08fdba 13682 {
5566b478
MS
13683 tree ttype = target_type (fntype);
13684 tree parmdecl;
13685
8d08fdba
MS
13686 if (IS_AGGR_TYPE (ttype))
13687 /* Let debugger know it should output info for this type. */
13688 note_debug_info_needed (ttype);
5566b478
MS
13689
13690 for (parmdecl = DECL_ARGUMENTS (fndecl); parmdecl; parmdecl = TREE_CHAIN (parmdecl))
13691 {
13692 ttype = target_type (TREE_TYPE (parmdecl));
13693 if (IS_AGGR_TYPE (ttype))
13694 /* Let debugger know it should output info for this type. */
13695 note_debug_info_needed (ttype);
13696 }
8d08fdba 13697 }
8d08fdba 13698
5566b478
MS
13699 /* Clean house because we will need to reorder insns here. */
13700 do_pending_stack_adjust ();
8d08fdba 13701
5566b478 13702 if (dtor_label)
8d08fdba 13703 {
5566b478
MS
13704 tree binfo = TYPE_BINFO (current_class_type);
13705 tree cond = integer_one_node;
13706 tree exprstmt;
13707 tree in_charge_node = lookup_name (in_charge_identifier, 0);
13708 tree virtual_size;
13709 int ok_to_optimize_dtor = 0;
13710 int empty_dtor = get_last_insn () == last_dtor_insn;
8d08fdba 13711
5566b478
MS
13712 if (current_function_assigns_this)
13713 cond = build (NE_EXPR, boolean_type_node,
4ac14744 13714 current_class_ptr, integer_zero_node);
5566b478
MS
13715 else
13716 {
13717 int n_baseclasses = CLASSTYPE_N_BASECLASSES (current_class_type);
13718
13719 /* If this destructor is empty, then we don't need to check
13720 whether `this' is NULL in some cases. */
13721 if ((flag_this_is_variable & 1) == 0)
13722 ok_to_optimize_dtor = 1;
13723 else if (empty_dtor)
13724 ok_to_optimize_dtor
13725 = (n_baseclasses == 0
13726 || (n_baseclasses == 1
13727 && TYPE_HAS_DESTRUCTOR (TYPE_BINFO_BASETYPE (current_class_type, 0))));
13728 }
8d08fdba 13729
5566b478
MS
13730 /* These initializations might go inline. Protect
13731 the binding level of the parms. */
13732 pushlevel (0);
13733 expand_start_bindings (0);
8d08fdba 13734
5566b478
MS
13735 if (current_function_assigns_this)
13736 {
13737 current_function_assigns_this = 0;
13738 current_function_just_assigned_this = 0;
13739 }
8d08fdba 13740
5566b478
MS
13741 /* Generate the code to call destructor on base class.
13742 If this destructor belongs to a class with virtual
13743 functions, then set the virtual function table
13744 pointer to represent the type of our base class. */
8d08fdba 13745
5566b478 13746 /* This side-effect makes call to `build_delete' generate the
2a2480e1
JM
13747 code we have to have at the end of this destructor.
13748 `build_delete' will set the flag again. */
5566b478 13749 TYPE_HAS_DESTRUCTOR (current_class_type) = 0;
8d08fdba 13750
5566b478
MS
13751 /* These are two cases where we cannot delegate deletion. */
13752 if (TYPE_USES_VIRTUAL_BASECLASSES (current_class_type)
13753 || TYPE_GETS_REG_DELETE (current_class_type))
4ac14744 13754 exprstmt = build_delete (current_class_type, current_class_ref, integer_zero_node,
e92cc029 13755 LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR|LOOKUP_NORMAL, 0);
5566b478 13756 else
4ac14744 13757 exprstmt = build_delete (current_class_type, current_class_ref, in_charge_node,
e92cc029 13758 LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR|LOOKUP_NORMAL, 0);
5566b478
MS
13759
13760 /* If we did not assign to this, then `this' is non-zero at
13761 the end of a destructor. As a special optimization, don't
13762 emit test if this is an empty destructor. If it does nothing,
13763 it does nothing. If it calls a base destructor, the base
13764 destructor will perform the test. */
13765
13766 if (exprstmt != error_mark_node
13767 && (TREE_CODE (exprstmt) != NOP_EXPR
13768 || TREE_OPERAND (exprstmt, 0) != integer_zero_node
13769 || TYPE_USES_VIRTUAL_BASECLASSES (current_class_type)))
8d08fdba 13770 {
5566b478
MS
13771 expand_label (dtor_label);
13772 if (cond != integer_one_node)
13773 expand_start_cond (cond, 0);
13774 if (exprstmt != void_zero_node)
13775 /* Don't call `expand_expr_stmt' if we're not going to do
13776 anything, since -Wall will give a diagnostic. */
13777 expand_expr_stmt (exprstmt);
13778
13779 /* Run destructor on all virtual baseclasses. */
13780 if (TYPE_USES_VIRTUAL_BASECLASSES (current_class_type))
8d08fdba 13781 {
5566b478
MS
13782 tree vbases = nreverse (copy_list (CLASSTYPE_VBASECLASSES (current_class_type)));
13783 expand_start_cond (build (BIT_AND_EXPR, integer_type_node,
13784 in_charge_node, integer_two_node), 0);
13785 while (vbases)
8d08fdba 13786 {
5566b478
MS
13787 if (TYPE_NEEDS_DESTRUCTOR (BINFO_TYPE (vbases)))
13788 {
bd6dd845
MS
13789 tree vb = get_vbase
13790 (BINFO_TYPE (vbases),
8ea2460f 13791 TYPE_BINFO (current_class_type));
e92cc029 13792 expand_expr_stmt
bd6dd845
MS
13793 (build_scoped_method_call
13794 (current_class_ref, vb, dtor_identifier,
e66d884e 13795 build_expr_list (NULL_TREE, integer_zero_node)));
5566b478
MS
13796 }
13797 vbases = TREE_CHAIN (vbases);
8d08fdba 13798 }
5566b478 13799 expand_end_cond ();
8d08fdba 13800 }
5566b478
MS
13801
13802 do_pending_stack_adjust ();
13803 if (cond != integer_one_node)
13804 expand_end_cond ();
8d08fdba
MS
13805 }
13806
5566b478
MS
13807 virtual_size = c_sizeof (current_class_type);
13808
13809 /* At the end, call delete if that's what's requested. */
519ebd1e
JM
13810
13811 /* FDIS sez: At the point of definition of a virtual destructor
13812 (including an implicit definition), non-placement operator
13813 delete shall be looked up in the scope of the destructor's
13814 class and if found shall be accessible and unambiguous.
13815
13816 This is somewhat unclear, but I take it to mean that if the
13817 class only defines placement deletes we don't do anything here.
13818 So we pass LOOKUP_SPECULATIVELY; delete_sanity will complain
13819 for us if they ever try to delete one of these. */
13820
13821 if (TYPE_GETS_REG_DELETE (current_class_type)
13822 || TYPE_USES_VIRTUAL_BASECLASSES (current_class_type))
13823 exprstmt = build_op_delete_call
13824 (DELETE_EXPR, current_class_ptr, virtual_size,
13825 LOOKUP_NORMAL | LOOKUP_SPECULATIVELY, NULL_TREE);
5566b478
MS
13826 else
13827 exprstmt = NULL_TREE;
8d08fdba 13828
5566b478
MS
13829 if (exprstmt)
13830 {
13831 cond = build (BIT_AND_EXPR, integer_type_node,
13832 in_charge_node, integer_one_node);
13833 expand_start_cond (cond, 0);
13834 expand_expr_stmt (exprstmt);
13835 expand_end_cond ();
13836 }
8d08fdba 13837
5566b478 13838 /* End of destructor. */
fc378698 13839 expand_end_bindings (NULL_TREE, getdecls () != NULL_TREE, 0);
536333d4 13840 poplevel (getdecls () != NULL_TREE, 0, 0);
5566b478
MS
13841
13842 /* Back to the top of destructor. */
956d6950 13843 /* Don't execute destructor code if `this' is NULL. */
5566b478
MS
13844
13845 start_sequence ();
13846
13847 /* If the dtor is empty, and we know there is not possible way we
13848 could use any vtable entries, before they are possibly set by
13849 a base class dtor, we don't have to setup the vtables, as we
13850 know that any base class dtoring will set up any vtables it
13851 needs. We avoid MI, because one base class dtor can do a
13852 virtual dispatch to an overridden function that would need to
13853 have a non-related vtable set up, we cannot avoid setting up
13854 vtables in that case. We could change this to see if there is
13855 just one vtable. */
13856 if (! empty_dtor || TYPE_USES_COMPLEX_INHERITANCE (current_class_type))
13857 {
13858 /* Make all virtual function table pointers in non-virtual base
13859 classes point to CURRENT_CLASS_TYPE's virtual function
13860 tables. */
4ac14744 13861 expand_direct_vtbls_init (binfo, binfo, 1, 0, current_class_ptr);
8d08fdba 13862
5566b478 13863 if (TYPE_USES_VIRTUAL_BASECLASSES (current_class_type))
4ac14744 13864 expand_indirect_vtbls_init (binfo, current_class_ref, current_class_ptr);
5566b478
MS
13865 }
13866
13867 if (! ok_to_optimize_dtor)
13868 {
13869 cond = build_binary_op (NE_EXPR,
337c90cc 13870 current_class_ptr, integer_zero_node);
5566b478
MS
13871 expand_start_cond (cond, 0);
13872 }
8d08fdba 13873
5566b478
MS
13874 insns = get_insns ();
13875 end_sequence ();
f30432d7 13876
49ad7cfa
BS
13877 fn_last_parm_insn = get_first_nonparm_insn ();
13878 if (fn_last_parm_insn == NULL_RTX)
13879 fn_last_parm_insn = get_last_insn ();
5566b478 13880 else
49ad7cfa 13881 fn_last_parm_insn = previous_insn (fn_last_parm_insn);
8d08fdba 13882
49ad7cfa 13883 emit_insns_after (insns, fn_last_parm_insn);
72b7eeff 13884
5566b478
MS
13885 if (! ok_to_optimize_dtor)
13886 expand_end_cond ();
72b7eeff 13887 }
5566b478 13888 else if (current_function_assigns_this)
8d08fdba 13889 {
5566b478
MS
13890 /* Does not need to call emit_base_init, because
13891 that is done (if needed) just after assignment to this
13892 is seen. */
13893
13894 if (DECL_CONSTRUCTOR_P (current_function_decl))
13895 {
13896 end_protect_partials ();
13897 expand_label (ctor_label);
13898 ctor_label = NULL_TREE;
13899
13900 if (call_poplevel)
13901 {
13902 decls = getdecls ();
13903 expand_end_bindings (decls, decls != NULL_TREE, 0);
13904 poplevel (decls != NULL_TREE, 0, 0);
13905 }
1c2c08a5
JM
13906 /* c_expand_return knows to return 'this' from a constructor. */
13907 c_expand_return (NULL_TREE);
5566b478 13908 }
a1774733 13909 else if (TREE_CODE (TREE_TYPE (DECL_RESULT (current_function_decl))) != VOID_TYPE
5566b478
MS
13910 && return_label != NULL_RTX)
13911 no_return_label = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE);
13912
13913 current_function_assigns_this = 0;
13914 current_function_just_assigned_this = 0;
13915 base_init_expr = NULL_TREE;
8d08fdba 13916 }
5566b478
MS
13917 else if (DECL_CONSTRUCTOR_P (fndecl))
13918 {
a703fb38 13919 tree cond = NULL_TREE, thenclause = NULL_TREE;
5566b478
MS
13920 /* Allow constructor for a type to get a new instance of the object
13921 using `build_new'. */
13922 tree abstract_virtuals = CLASSTYPE_ABSTRACT_VIRTUALS (current_class_type);
13923 CLASSTYPE_ABSTRACT_VIRTUALS (current_class_type) = NULL_TREE;
f30432d7 13924
5566b478
MS
13925 if (flag_this_is_variable > 0)
13926 {
13927 cond = build_binary_op (EQ_EXPR,
337c90cc 13928 current_class_ptr, integer_zero_node);
4ac14744 13929 thenclause = build_modify_expr (current_class_ptr, NOP_EXPR,
5566b478
MS
13930 build_new (NULL_TREE, current_class_type, void_type_node, 0));
13931 }
f30432d7 13932
5566b478 13933 CLASSTYPE_ABSTRACT_VIRTUALS (current_class_type) = abstract_virtuals;
f30432d7 13934
5566b478
MS
13935 start_sequence ();
13936
13937 if (flag_this_is_variable > 0)
13938 {
13939 expand_start_cond (cond, 0);
13940 expand_expr_stmt (thenclause);
13941 expand_end_cond ();
13942 }
13943
13944 /* Emit insns from `emit_base_init' which sets up virtual
13945 function table pointer(s). */
13946 if (base_init_expr)
13947 {
13948 expand_expr_stmt (base_init_expr);
13949 base_init_expr = NULL_TREE;
13950 }
13951
13952 insns = get_insns ();
13953 end_sequence ();
13954
b87692e5 13955 /* This is where the body of the constructor begins. */
5566b478 13956
b87692e5 13957 emit_insns_after (insns, last_parm_cleanup_insn);
8d08fdba 13958
b7484fbe 13959 end_protect_partials ();
5566b478
MS
13960
13961 /* This is where the body of the constructor ends. */
8d08fdba
MS
13962 expand_label (ctor_label);
13963 ctor_label = NULL_TREE;
13964
13965 if (call_poplevel)
13966 {
13967 decls = getdecls ();
8d08fdba 13968 expand_end_bindings (decls, decls != NULL_TREE, 0);
5566b478 13969 poplevel (decls != NULL_TREE, 1, 0);
8d08fdba 13970 }
8d08fdba 13971
1c2c08a5
JM
13972 /* c_expand_return knows to return 'this' from a constructor. */
13973 c_expand_return (NULL_TREE);
8d08fdba 13974
5566b478
MS
13975 current_function_assigns_this = 0;
13976 current_function_just_assigned_this = 0;
13977 }
35680744 13978 else if (DECL_MAIN_P (fndecl))
8d08fdba 13979 {
5566b478
MS
13980 /* Make it so that `main' always returns 0 by default. */
13981#ifdef VMS
13982 c_expand_return (integer_one_node);
13983#else
13984 c_expand_return (integer_zero_node);
13985#endif
8d08fdba 13986 }
5566b478
MS
13987 else if (return_label != NULL_RTX
13988 && current_function_return_value == NULL_TREE
13989 && ! DECL_NAME (DECL_RESULT (current_function_decl)))
13990 no_return_label = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE);
8d08fdba 13991
eb448459
MS
13992 if (flag_exceptions)
13993 expand_exception_blocks ();
13994
5566b478
MS
13995 /* If this function is supposed to return a value, ensure that
13996 we do not fall into the cleanups by mistake. The end of our
13997 function will look like this:
13998
13999 user code (may have return stmt somewhere)
14000 goto no_return_label
14001 cleanup_label:
14002 cleanups
14003 goto return_label
14004 no_return_label:
14005 NOTE_INSN_FUNCTION_END
14006 return_label:
14007 things for return
14008
14009 If the user omits a return stmt in the USER CODE section, we
14010 will have a control path which reaches NOTE_INSN_FUNCTION_END.
14011 Otherwise, we won't. */
14012 if (no_return_label)
8d08fdba 14013 {
5566b478
MS
14014 DECL_CONTEXT (no_return_label) = fndecl;
14015 DECL_INITIAL (no_return_label) = error_mark_node;
14016 DECL_SOURCE_FILE (no_return_label) = input_filename;
14017 DECL_SOURCE_LINE (no_return_label) = lineno;
14018 expand_goto (no_return_label);
8d08fdba
MS
14019 }
14020
5566b478 14021 if (cleanup_label)
a9aedbc2 14022 {
2c73f9f5 14023 /* Remove the binding contour which is used
5566b478
MS
14024 to catch cleanup-generated temporaries. */
14025 expand_end_bindings (0, 0, 0);
14026 poplevel (0, 0, 0);
8d08fdba 14027
eb448459
MS
14028 /* Emit label at beginning of cleanup code for parameters. */
14029 emit_label (cleanup_label);
14030 }
b7484fbe 14031
5566b478
MS
14032 /* Get return value into register if that's where it's supposed to be. */
14033 if (original_result_rtx)
14034 fixup_result_decl (DECL_RESULT (fndecl), original_result_rtx);
8d08fdba 14035
5566b478
MS
14036 /* Finish building code that will trigger warnings if users forget
14037 to make their functions return values. */
14038 if (no_return_label || cleanup_label)
14039 emit_jump (return_label);
14040 if (no_return_label)
8d08fdba 14041 {
5566b478
MS
14042 /* We don't need to call `expand_*_return' here because we
14043 don't need any cleanups here--this path of code is only
14044 for error checking purposes. */
14045 expand_label (no_return_label);
8d08fdba
MS
14046 }
14047
5566b478
MS
14048 /* Generate rtl for function exit. */
14049 expand_function_end (input_filename, lineno, 1);
8d08fdba 14050 }
f181d4ae 14051
f181d4ae
MM
14052 /* If we're processing a template, squirrel away the definition
14053 until we do an instantiation. */
14054 if (processing_template_decl)
14055 {
14056 --minimal_parse_mode;
14057 DECL_SAVED_TREE (fndecl) = TREE_CHAIN (DECL_SAVED_TREE (fndecl));
14058 /* We have to save this value here in case
14059 maybe_end_member_template_processing decides to pop all the
14060 template parameters. */
14061 in_template = 1;
14062 }
14063 else
14064 in_template = 0;
8d2733ca 14065
8d08fdba
MS
14066 /* This must come after expand_function_end because cleanups might
14067 have declarations (from inline functions) that need to go into
14068 this function's blocks. */
14069 if (current_binding_level->parm_flag != 1)
14070 my_friendly_abort (122);
14071 poplevel (1, 0, 1);
14072
f181d4ae
MM
14073 /* If this is a in-class inline definition, we may have to pop the
14074 bindings for the template parameters that we added in
14075 maybe_begin_member_template_processing when start_function was
14076 called. */
14077 if (inclass_inline)
14078 maybe_end_member_template_processing ();
14079
2c73f9f5 14080 /* Reset scope for C++: if we were in the scope of a class,
db5ae43f
MS
14081 then when we finish this function, we are not longer so.
14082 This cannot be done until we know for sure that no more
14083 class members will ever be referenced in this function
14084 (i.e., calls to destructors). */
14085 if (current_class_name)
14086 {
14087 ctype = current_class_type;
b74a0560 14088 pop_nested_class ();
db5ae43f 14089 }
db5ae43f 14090
1caa11d3
MM
14091 /* Must mark the RESULT_DECL as being in this function. */
14092 DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
14093
14094 /* Set the BLOCK_SUPERCONTEXT of the outermost function scope to point
14095 to the FUNCTION_DECL node itself. */
14096 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
14097
f181d4ae 14098 if (!in_template)
5566b478 14099 {
75650646
MM
14100 int saved_flag_keep_inline_functions =
14101 flag_keep_inline_functions;
14102
5566b478
MS
14103 /* So we can tell if jump_optimize sets it to 1. */
14104 can_reach_end = 0;
8d08fdba 14105
75650646 14106 if (DECL_CONTEXT (fndecl) != NULL_TREE
e1467ff2 14107 && hack_decl_function_context (fndecl))
75650646
MM
14108 /* Trick rest_of_compilation into not deferring output of this
14109 function, even if it is inline, since the rtl_obstack for
14110 this function is the function_obstack of the enclosing
14111 function and will be deallocated when the enclosing
14112 function is gone. See save_tree_status. */
14113 flag_keep_inline_functions = 1;
14114
6633d636
MS
14115 /* Run the optimizers and output the assembler code for this
14116 function. */
14117
14118 if (DECL_ARTIFICIAL (fndecl))
14119 {
14120 /* Do we really *want* to inline this synthesized method? */
14121
14122 int save_fif = flag_inline_functions;
14123 flag_inline_functions = 1;
14124
14125 /* Turn off DECL_INLINE for the moment so function_cannot_inline_p
14126 will check our size. */
14127 DECL_INLINE (fndecl) = 0;
14128
14129 rest_of_compilation (fndecl);
14130 flag_inline_functions = save_fif;
14131 }
14132 else
14133 rest_of_compilation (fndecl);
8d08fdba 14134
75650646
MM
14135 flag_keep_inline_functions = saved_flag_keep_inline_functions;
14136
5566b478
MS
14137 if (DECL_SAVED_INSNS (fndecl) && ! TREE_ASM_WRITTEN (fndecl))
14138 {
14139 /* Set DECL_EXTERNAL so that assemble_external will be called as
14140 necessary. We'll clear it again in finish_file. */
14141 if (! DECL_EXTERNAL (fndecl))
14142 DECL_NOT_REALLY_EXTERN (fndecl) = 1;
14143 DECL_EXTERNAL (fndecl) = 1;
14144 mark_inline_for_output (fndecl);
14145 }
8926095f 14146
d2e5ee5c
MS
14147 if (ctype && TREE_ASM_WRITTEN (fndecl))
14148 note_debug_info_needed (ctype);
14149
5566b478 14150 current_function_returns_null |= can_reach_end;
8d08fdba 14151
5566b478
MS
14152 /* Since we don't normally go through c_expand_return for constructors,
14153 this normally gets the wrong value.
14154 Also, named return values have their return codes emitted after
14155 NOTE_INSN_FUNCTION_END, confusing jump.c. */
14156 if (DECL_CONSTRUCTOR_P (fndecl)
14157 || DECL_NAME (DECL_RESULT (fndecl)) != NULL_TREE)
14158 current_function_returns_null = 0;
8d08fdba 14159
5566b478 14160 if (TREE_THIS_VOLATILE (fndecl) && current_function_returns_null)
8251199e 14161 cp_warning ("`noreturn' function `%D' does return", fndecl);
5566b478
MS
14162 else if ((warn_return_type || pedantic)
14163 && current_function_returns_null
a1774733 14164 && TREE_CODE (TREE_TYPE (fntype)) != VOID_TYPE)
5566b478
MS
14165 {
14166 /* If this function returns non-void and control can drop through,
14167 complain. */
8251199e 14168 cp_warning ("control reaches end of non-void function `%D'", fndecl);
5566b478
MS
14169 }
14170 /* With just -W, complain only if function returns both with
14171 and without a value. */
14172 else if (extra_warnings
14173 && current_function_returns_value && current_function_returns_null)
8251199e 14174 warning ("this function may return with or without a value");
8d08fdba 14175 }
5566b478
MS
14176
14177 --function_depth;
8d08fdba
MS
14178
14179 /* Free all the tree nodes making up this function. */
14180 /* Switch back to allocating nodes permanently
14181 until we start another function. */
db5ae43f
MS
14182 if (! nested)
14183 permanent_allocation (1);
8d08fdba 14184
49ad7cfa 14185 if (DECL_SAVED_INSNS (fndecl) == 0)
8d08fdba 14186 {
d2e5ee5c
MS
14187 tree t;
14188
8d08fdba
MS
14189 /* Stop pointing to the local nodes about to be freed. */
14190 /* But DECL_INITIAL must remain nonzero so we know this
14191 was an actual function definition. */
14192 DECL_INITIAL (fndecl) = error_mark_node;
d2e5ee5c 14193 for (t = DECL_ARGUMENTS (fndecl); t; t = TREE_CHAIN (t))
de22184b 14194 DECL_RTL (t) = DECL_INCOMING_RTL (t) = NULL_RTX;
8d08fdba
MS
14195 }
14196
e1cd6e56
MS
14197 if (DECL_STATIC_CONSTRUCTOR (fndecl))
14198 static_ctors = perm_tree_cons (NULL_TREE, fndecl, static_ctors);
14199 if (DECL_STATIC_DESTRUCTOR (fndecl))
14200 static_dtors = perm_tree_cons (NULL_TREE, fndecl, static_dtors);
14201
28cbf42c
MS
14202 if (! nested)
14203 {
14204 /* Let the error reporting routines know that we're outside a
14205 function. For a nested function, this value is used in
14206 pop_cp_function_context and then reset via pop_function_context. */
14207 current_function_decl = NULL_TREE;
14208 }
14209
e349ee73 14210 named_label_uses = NULL;
4ac14744
MS
14211 current_class_ptr = NULL_TREE;
14212 current_class_ref = NULL_TREE;
8d08fdba
MS
14213}
14214\f
14215/* Create the FUNCTION_DECL for a function definition.
8d08fdba
MS
14216 DECLSPECS and DECLARATOR are the parts of the declaration;
14217 they describe the return type and the name of the function,
14218 but twisted together in a fashion that parallels the syntax of C.
14219
14220 This function creates a binding context for the function body
14221 as well as setting up the FUNCTION_DECL in current_function_decl.
14222
14223 Returns a FUNCTION_DECL on success.
14224
14225 If the DECLARATOR is not suitable for a function (it defines a datum
14226 instead), we return 0, which tells yyparse to report a parse error.
14227
14228 May return void_type_node indicating that this method is actually
14229 a friend. See grokfield for more details.
14230
14231 Came here with a `.pushlevel' .
14232
14233 DO NOT MAKE ANY CHANGES TO THIS CODE WITHOUT MAKING CORRESPONDING
14234 CHANGES TO CODE IN `grokfield'. */
e92cc029 14235
8d08fdba 14236tree
acf82af2
JM
14237start_method (declspecs, declarator, attrlist)
14238 tree declarator, declspecs, attrlist;
8d08fdba 14239{
c11b6f21 14240 tree fndecl = grokdeclarator (declarator, declspecs, MEMFUNCDEF, 0,
acf82af2 14241 attrlist);
8d08fdba
MS
14242
14243 /* Something too ugly to handle. */
14244 if (fndecl == NULL_TREE)
14245 return NULL_TREE;
14246
14247 /* Pass friends other than inline friend functions back. */
a1774733 14248 if (fndecl == void_type_node)
8d08fdba
MS
14249 return fndecl;
14250
14251 if (TREE_CODE (fndecl) != FUNCTION_DECL)
14252 /* Not a function, tell parser to report parse error. */
14253 return NULL_TREE;
14254
14255 if (IS_SIGNATURE (current_class_type))
824b9a4c 14256 IS_DEFAULT_IMPLEMENTATION (fndecl) = 1;
8d08fdba
MS
14257
14258 if (DECL_IN_AGGR_P (fndecl))
14259 {
14260 if (IDENTIFIER_ERROR_LOCUS (DECL_ASSEMBLER_NAME (fndecl)) != current_class_type)
14261 {
2c73f9f5
ML
14262 if (DECL_CONTEXT (fndecl)
14263 && TREE_CODE( DECL_CONTEXT (fndecl)) != NAMESPACE_DECL)
8251199e
JM
14264 cp_error ("`%D' is already defined in class %s", fndecl,
14265 TYPE_NAME_STRING (DECL_CONTEXT (fndecl)));
8d08fdba
MS
14266 }
14267 return void_type_node;
14268 }
14269
f3400fe2
JM
14270 check_template_shadow (fndecl);
14271
faae18ab
MS
14272 DECL_THIS_INLINE (fndecl) = 1;
14273
8926095f 14274 if (flag_default_inline)
8d08fdba
MS
14275 DECL_INLINE (fndecl) = 1;
14276
36a117a5
MM
14277 /* We process method specializations in finish_struct_1. */
14278 if (processing_template_decl && !DECL_TEMPLATE_SPECIALIZATION (fndecl))
3ac3d9ea 14279 fndecl = push_template_decl (fndecl);
a0a33927 14280
8d08fdba
MS
14281 /* We read in the parameters on the maybepermanent_obstack,
14282 but we won't be getting back to them until after we
14283 may have clobbered them. So the call to preserve_data
14284 will keep them safe. */
14285 preserve_data ();
14286
14287 if (! DECL_FRIEND_P (fndecl))
14288 {
8d08fdba
MS
14289 if (TREE_CHAIN (fndecl))
14290 {
14291 fndecl = copy_node (fndecl);
14292 TREE_CHAIN (fndecl) = NULL_TREE;
14293 }
14294
14295 if (DECL_CONSTRUCTOR_P (fndecl))
a0a33927
MS
14296 {
14297 if (! grok_ctor_properties (current_class_type, fndecl))
14298 return void_type_node;
14299 }
8d08fdba
MS
14300 else if (IDENTIFIER_OPNAME_P (DECL_NAME (fndecl)))
14301 grok_op_properties (fndecl, DECL_VIRTUAL_P (fndecl), 0);
14302 }
14303
82580166 14304 cp_finish_decl (fndecl, NULL_TREE, NULL_TREE, 0, 0);
8d08fdba
MS
14305
14306 /* Make a place for the parms */
14307 pushlevel (0);
14308 current_binding_level->parm_flag = 1;
14309
14310 DECL_IN_AGGR_P (fndecl) = 1;
14311 return fndecl;
14312}
14313
14314/* Go through the motions of finishing a function definition.
14315 We don't compile this method until after the whole class has
14316 been processed.
14317
14318 FINISH_METHOD must return something that looks as though it
14319 came from GROKFIELD (since we are defining a method, after all).
14320
14321 This is called after parsing the body of the function definition.
14322 STMTS is the chain of statements that makes up the function body.
14323
14324 DECL is the ..._DECL that `start_method' provided. */
14325
14326tree
14327finish_method (decl)
14328 tree decl;
14329{
14330 register tree fndecl = decl;
14331 tree old_initial;
8d08fdba
MS
14332
14333 register tree link;
14334
a1774733 14335 if (decl == void_type_node)
8d08fdba
MS
14336 return decl;
14337
14338 old_initial = DECL_INITIAL (fndecl);
14339
14340 /* Undo the level for the parms (from start_method).
14341 This is like poplevel, but it causes nothing to be
14342 saved. Saving information here confuses symbol-table
14343 output routines. Besides, this information will
14344 be correctly output when this method is actually
14345 compiled. */
14346
14347 /* Clear out the meanings of the local variables of this level;
14348 also record in each decl which block it belongs to. */
14349
14350 for (link = current_binding_level->names; link; link = TREE_CHAIN (link))
14351 {
14352 if (DECL_NAME (link) != NULL_TREE)
d8f8dca1 14353 pop_binding (DECL_NAME (link), link);
8d08fdba
MS
14354 my_friendly_assert (TREE_CODE (link) != FUNCTION_DECL, 163);
14355 DECL_CONTEXT (link) = NULL_TREE;
14356 }
14357
8d08fdba
MS
14358 GNU_xref_end_scope ((HOST_WIDE_INT) current_binding_level,
14359 (HOST_WIDE_INT) current_binding_level->level_chain,
14360 current_binding_level->parm_flag,
5566b478 14361 current_binding_level->keep);
8d08fdba
MS
14362
14363 poplevel (0, 0, 0);
14364
14365 DECL_INITIAL (fndecl) = old_initial;
14366
14367 /* We used to check if the context of FNDECL was different from
14368 current_class_type as another way to get inside here. This didn't work
14369 for String.cc in libg++. */
14370 if (DECL_FRIEND_P (fndecl))
14371 {
14372 CLASSTYPE_INLINE_FRIENDS (current_class_type)
14373 = tree_cons (NULL_TREE, fndecl, CLASSTYPE_INLINE_FRIENDS (current_class_type));
14374 decl = void_type_node;
14375 }
14376
14377 return decl;
14378}
14379\f
14380/* Called when a new struct TYPE is defined.
14381 If this structure or union completes the type of any previous
14382 variable declaration, lay it out and output its rtl. */
14383
14384void
14385hack_incomplete_structures (type)
14386 tree type;
14387{
f30432d7 14388 tree *list;
8d08fdba 14389
f30432d7 14390 if (current_binding_level->incomplete == NULL_TREE)
8d08fdba
MS
14391 return;
14392
14393 if (!type) /* Don't do this for class templates. */
14394 return;
14395
f30432d7
MS
14396 for (list = &current_binding_level->incomplete; *list; )
14397 {
14398 tree decl = TREE_VALUE (*list);
a703fb38 14399 if ((decl && TREE_TYPE (decl) == type)
f30432d7
MS
14400 || (TREE_TYPE (decl)
14401 && TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
14402 && TREE_TYPE (TREE_TYPE (decl)) == type))
14403 {
14404 int toplevel = toplevel_bindings_p ();
14405 if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
14406 && TREE_TYPE (TREE_TYPE (decl)) == type)
14407 layout_type (TREE_TYPE (decl));
14408 layout_decl (decl, 0);
14409 rest_of_decl_compilation (decl, NULL_PTR, toplevel, 0);
14410 if (! toplevel)
14411 {
14412 tree cleanup;
14413 expand_decl (decl);
14414 cleanup = maybe_build_cleanup (decl);
14415 expand_decl_init (decl);
e349ee73 14416 if (! expand_decl_cleanup (decl, cleanup))
8251199e 14417 cp_error ("parser lost in parsing declaration of `%D'",
f30432d7
MS
14418 decl);
14419 }
14420 *list = TREE_CHAIN (*list);
14421 }
14422 else
14423 list = &TREE_CHAIN (*list);
14424 }
8d08fdba
MS
14425}
14426
8d08fdba 14427/* If DECL is of a type which needs a cleanup, build that cleanup here.
c73964b2 14428 See build_delete for information about AUTO_DELETE.
8d08fdba
MS
14429
14430 Don't build these on the momentary obstack; they must live
14431 the life of the binding contour. */
e92cc029 14432
c73964b2
MS
14433static tree
14434maybe_build_cleanup_1 (decl, auto_delete)
14435 tree decl, auto_delete;
8d08fdba
MS
14436{
14437 tree type = TREE_TYPE (decl);
44689c12 14438 if (type != error_mark_node && TYPE_NEEDS_DESTRUCTOR (type))
8d08fdba
MS
14439 {
14440 int temp = 0, flags = LOOKUP_NORMAL|LOOKUP_DESTRUCTOR;
14441 tree rval;
8d08fdba
MS
14442
14443 if (TREE_CODE (decl) != PARM_DECL)
14444 temp = suspend_momentary ();
14445
14446 if (TREE_CODE (type) == ARRAY_TYPE)
14447 rval = decl;
14448 else
14449 {
14450 mark_addressable (decl);
14451 rval = build_unary_op (ADDR_EXPR, decl, 0);
14452 }
14453
14454 /* Optimize for space over speed here. */
14455 if (! TYPE_USES_VIRTUAL_BASECLASSES (type)
14456 || flag_expensive_optimizations)
14457 flags |= LOOKUP_NONVIRTUAL;
14458
c73964b2 14459 rval = build_delete (TREE_TYPE (rval), rval, auto_delete, flags, 0);
8d08fdba
MS
14460
14461 if (TYPE_USES_VIRTUAL_BASECLASSES (type)
14462 && ! TYPE_HAS_DESTRUCTOR (type))
e66d884e
JM
14463 rval = build_compound_expr (expr_tree_cons (NULL_TREE, rval,
14464 build_expr_list (NULL_TREE, build_vbase_delete (type, decl))));
8d08fdba 14465
8d08fdba
MS
14466 if (TREE_CODE (decl) != PARM_DECL)
14467 resume_momentary (temp);
14468
8d08fdba
MS
14469 return rval;
14470 }
14471 return 0;
14472}
c73964b2
MS
14473
14474/* If DECL is of a type which needs a cleanup, build that cleanup
14475 here. The cleanup does free the storage with a call to delete. */
14476
14477tree
14478maybe_build_cleanup_and_delete (decl)
14479 tree decl;
14480{
14481 return maybe_build_cleanup_1 (decl, integer_three_node);
14482}
14483
14484/* If DECL is of a type which needs a cleanup, build that cleanup
14485 here. The cleanup does not free the storage with a call a delete. */
14486
14487tree
14488maybe_build_cleanup (decl)
14489 tree decl;
14490{
14491 return maybe_build_cleanup_1 (decl, integer_two_node);
14492}
8d08fdba
MS
14493\f
14494/* Expand a C++ expression at the statement level.
14495 This is needed to ferret out nodes which have UNKNOWN_TYPE.
14496 The C++ type checker should get all of these out when
14497 expressions are combined with other, type-providing, expressions,
14498 leaving only orphan expressions, such as:
14499
e92cc029 14500 &class::bar; / / takes its address, but does nothing with it. */
8d08fdba 14501
8d08fdba
MS
14502void
14503cplus_expand_expr_stmt (exp)
14504 tree exp;
14505{
5156628f 14506 if (processing_template_decl)
5566b478
MS
14507 {
14508 add_tree (build_min_nt (EXPR_STMT, exp));
14509 return;
14510 }
14511
eb66be0e
MS
14512 /* Arrange for all temps to disappear. */
14513 expand_start_target_temps ();
14514
66543169
NS
14515 exp = require_complete_type_in_void (exp);
14516
14517 if (TREE_CODE (exp) == FUNCTION_DECL)
8d08fdba 14518 {
66543169
NS
14519 cp_warning ("reference, not call, to function `%D'", exp);
14520 warning ("at this point in file");
8d08fdba 14521 }
8d08fdba 14522
63c68bb7 14523#if 0
66543169
NS
14524 /* We should do this eventually, but right now this causes regex.o from
14525 libg++ to miscompile, and tString to core dump. */
14526 exp = build1 (CLEANUP_POINT_EXPR, TREE_TYPE (exp), exp);
63c68bb7 14527#endif
b3e401c2 14528
66543169
NS
14529 /* Strip unused implicit INDIRECT_REFs of references. */
14530 if (TREE_CODE (exp) == INDIRECT_REF
14531 && TREE_CODE (TREE_TYPE (TREE_OPERAND (exp, 0))) == REFERENCE_TYPE)
14532 exp = TREE_OPERAND (exp, 0);
b3e401c2 14533
66543169
NS
14534 /* If we don't do this, we end up down inside expand_expr
14535 trying to do TYPE_MODE on the ERROR_MARK, and really
14536 go outside the bounds of the type. */
14537 if (exp != error_mark_node)
14538 expand_expr_stmt (break_out_cleanups (exp));
8d08fdba
MS
14539
14540 /* Clean up any pending cleanups. This happens when a function call
14541 returns a cleanup-needing value that nobody uses. */
eb66be0e 14542 expand_end_target_temps ();
8d08fdba
MS
14543}
14544
14545/* When a stmt has been parsed, this function is called.
14546
14547 Currently, this function only does something within a
14548 constructor's scope: if a stmt has just assigned to this,
14549 and we are in a derived class, we call `emit_base_init'. */
14550
14551void
14552finish_stmt ()
14553{
8d08fdba
MS
14554 if (current_function_assigns_this
14555 || ! current_function_just_assigned_this)
14556 return;
14557 if (DECL_CONSTRUCTOR_P (current_function_decl))
14558 {
14559 /* Constructors must wait until we are out of control
14560 zones before calling base constructors. */
3f1d071b 14561 if (in_control_zone_p ())
8d08fdba 14562 return;
a9aedbc2 14563 expand_expr_stmt (base_init_expr);
8d08fdba
MS
14564 check_base_init (current_class_type);
14565 }
14566 current_function_assigns_this = 1;
8d08fdba
MS
14567}
14568
8d08fdba 14569/* Change a static member function definition into a FUNCTION_TYPE, instead
700f8a87
MS
14570 of the METHOD_TYPE that we create when it's originally parsed.
14571
14572 WARNING: DO NOT pass &TREE_TYPE (decl) to FN or &TYPE_ARG_TYPES
14573 (TREE_TYPE (decl)) to ARGTYPES, as doing so will corrupt the types of
14574 other decls. Either pass the addresses of local variables or NULL. */
14575
8857f91e 14576void
700f8a87
MS
14577revert_static_member_fn (decl, fn, argtypes)
14578 tree *decl, *fn, *argtypes;
8d08fdba 14579{
700f8a87
MS
14580 tree tmp;
14581 tree function = fn ? *fn : TREE_TYPE (*decl);
14582 tree args = argtypes ? *argtypes : TYPE_ARG_TYPES (function);
8d08fdba 14583
91063b51
MM
14584 if (CP_TYPE_QUALS (TREE_TYPE (TREE_VALUE (args)))
14585 != TYPE_UNQUALIFIED)
14586 cp_error ("static member function `%#D' declared with type qualifiers",
14587 *decl);
f30432d7 14588
700f8a87
MS
14589 args = TREE_CHAIN (args);
14590 tmp = build_function_type (TREE_TYPE (function), args);
91063b51 14591 tmp = build_qualified_type (tmp, CP_TYPE_QUALS (function));
f30432d7 14592 tmp = build_exception_variant (tmp,
8d08fdba
MS
14593 TYPE_RAISES_EXCEPTIONS (function));
14594 TREE_TYPE (*decl) = tmp;
f30432d7
MS
14595 if (DECL_ARGUMENTS (*decl))
14596 DECL_ARGUMENTS (*decl) = TREE_CHAIN (DECL_ARGUMENTS (*decl));
8d08fdba 14597 DECL_STATIC_FUNCTION_P (*decl) = 1;
700f8a87
MS
14598 if (fn)
14599 *fn = tmp;
14600 if (argtypes)
14601 *argtypes = args;
8d08fdba 14602}
a4443a08 14603
db5ae43f
MS
14604struct cp_function
14605{
14606 int returns_value;
14607 int returns_null;
db5ae43f
MS
14608 int assigns_this;
14609 int just_assigned_this;
14610 int parms_stored;
7215f9a0 14611 int temp_name_counter;
db5ae43f 14612 tree named_labels;
5524676d 14613 struct named_label_list *named_label_uses;
db5ae43f
MS
14614 tree shadowed_labels;
14615 tree ctor_label;
14616 tree dtor_label;
72b7eeff 14617 rtx last_dtor_insn;
b87692e5 14618 rtx last_parm_cleanup_insn;
79ff2c6c
MS
14619 tree base_init_list;
14620 tree member_init_list;
a9aedbc2 14621 tree base_init_expr;
4ac14744
MS
14622 tree current_class_ptr;
14623 tree current_class_ref;
db5ae43f
MS
14624 rtx result_rtx;
14625 struct cp_function *next;
14626 struct binding_level *binding_level;
42976354 14627 int static_labelno;
0dde4175 14628 int in_function_try_handler;
db5ae43f
MS
14629};
14630
bd6dd845 14631static struct cp_function *cp_function_chain;
db5ae43f 14632
7215f9a0
MS
14633extern int temp_name_counter;
14634
db5ae43f
MS
14635/* Save and reinitialize the variables
14636 used during compilation of a C++ function. */
14637
14638void
28cbf42c
MS
14639push_cp_function_context (context)
14640 tree context;
db5ae43f
MS
14641{
14642 struct cp_function *p
14643 = (struct cp_function *) xmalloc (sizeof (struct cp_function));
14644
28cbf42c 14645 push_function_context_to (context);
db5ae43f
MS
14646
14647 p->next = cp_function_chain;
14648 cp_function_chain = p;
14649
14650 p->named_labels = named_labels;
5524676d 14651 p->named_label_uses = named_label_uses;
db5ae43f
MS
14652 p->shadowed_labels = shadowed_labels;
14653 p->returns_value = current_function_returns_value;
14654 p->returns_null = current_function_returns_null;
db5ae43f
MS
14655 p->binding_level = current_binding_level;
14656 p->ctor_label = ctor_label;
14657 p->dtor_label = dtor_label;
72b7eeff 14658 p->last_dtor_insn = last_dtor_insn;
b87692e5 14659 p->last_parm_cleanup_insn = last_parm_cleanup_insn;
db5ae43f
MS
14660 p->assigns_this = current_function_assigns_this;
14661 p->just_assigned_this = current_function_just_assigned_this;
14662 p->parms_stored = current_function_parms_stored;
14663 p->result_rtx = original_result_rtx;
a9aedbc2 14664 p->base_init_expr = base_init_expr;
7215f9a0 14665 p->temp_name_counter = temp_name_counter;
79ff2c6c
MS
14666 p->base_init_list = current_base_init_list;
14667 p->member_init_list = current_member_init_list;
4ac14744
MS
14668 p->current_class_ptr = current_class_ptr;
14669 p->current_class_ref = current_class_ref;
42976354 14670 p->static_labelno = static_labelno;
0dde4175 14671 p->in_function_try_handler = in_function_try_handler;
db5ae43f
MS
14672}
14673
14674/* Restore the variables used during compilation of a C++ function. */
14675
14676void
28cbf42c
MS
14677pop_cp_function_context (context)
14678 tree context;
db5ae43f
MS
14679{
14680 struct cp_function *p = cp_function_chain;
14681 tree link;
14682
14683 /* Bring back all the labels that were shadowed. */
14684 for (link = shadowed_labels; link; link = TREE_CHAIN (link))
14685 if (DECL_NAME (TREE_VALUE (link)) != 0)
14686 SET_IDENTIFIER_LABEL_VALUE (DECL_NAME (TREE_VALUE (link)),
14687 TREE_VALUE (link));
14688
28cbf42c 14689 pop_function_context_from (context);
db5ae43f
MS
14690
14691 cp_function_chain = p->next;
14692
14693 named_labels = p->named_labels;
5524676d 14694 named_label_uses = p->named_label_uses;
db5ae43f
MS
14695 shadowed_labels = p->shadowed_labels;
14696 current_function_returns_value = p->returns_value;
14697 current_function_returns_null = p->returns_null;
db5ae43f
MS
14698 current_binding_level = p->binding_level;
14699 ctor_label = p->ctor_label;
14700 dtor_label = p->dtor_label;
72b7eeff 14701 last_dtor_insn = p->last_dtor_insn;
b87692e5 14702 last_parm_cleanup_insn = p->last_parm_cleanup_insn;
db5ae43f
MS
14703 current_function_assigns_this = p->assigns_this;
14704 current_function_just_assigned_this = p->just_assigned_this;
14705 current_function_parms_stored = p->parms_stored;
14706 original_result_rtx = p->result_rtx;
a9aedbc2 14707 base_init_expr = p->base_init_expr;
7215f9a0 14708 temp_name_counter = p->temp_name_counter;
79ff2c6c
MS
14709 current_base_init_list = p->base_init_list;
14710 current_member_init_list = p->member_init_list;
4ac14744
MS
14711 current_class_ptr = p->current_class_ptr;
14712 current_class_ref = p->current_class_ref;
42976354 14713 static_labelno = p->static_labelno;
0dde4175 14714 in_function_try_handler = p->in_function_try_handler;
db5ae43f
MS
14715
14716 free (p);
14717}
ebfc180f 14718
5566b478
MS
14719int
14720in_function_p ()
14721{
14722 return function_depth != 0;
14723}