]> gcc.gnu.org Git - gcc.git/blame - gcc/cp/decl.c
cpplib.c (detect_if_not_defined): New function.
[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
e5dc5fb2 63extern int (*valid_lang_attribute) PROTO ((tree, tree, tree, tree));
62c154ed 64
8d08fdba
MS
65/* Obstack used for remembering local class declarations (like
66 enums and static (const) members. */
67#include "stack.h"
5566b478 68struct obstack decl_obstack;
8d08fdba
MS
69static struct stack_level *decl_stack;
70
71#ifndef CHAR_TYPE_SIZE
72#define CHAR_TYPE_SIZE BITS_PER_UNIT
73#endif
74
75#ifndef SHORT_TYPE_SIZE
76#define SHORT_TYPE_SIZE (BITS_PER_UNIT * MIN ((UNITS_PER_WORD + 1) / 2, 2))
77#endif
78
79#ifndef INT_TYPE_SIZE
80#define INT_TYPE_SIZE BITS_PER_WORD
81#endif
82
83#ifndef LONG_TYPE_SIZE
84#define LONG_TYPE_SIZE BITS_PER_WORD
85#endif
86
87#ifndef LONG_LONG_TYPE_SIZE
88#define LONG_LONG_TYPE_SIZE (BITS_PER_WORD * 2)
89#endif
90
91#ifndef WCHAR_UNSIGNED
92#define WCHAR_UNSIGNED 0
93#endif
94
95#ifndef FLOAT_TYPE_SIZE
96#define FLOAT_TYPE_SIZE BITS_PER_WORD
97#endif
98
99#ifndef DOUBLE_TYPE_SIZE
100#define DOUBLE_TYPE_SIZE (BITS_PER_WORD * 2)
101#endif
102
103#ifndef LONG_DOUBLE_TYPE_SIZE
104#define LONG_DOUBLE_TYPE_SIZE (BITS_PER_WORD * 2)
105#endif
106
255512c1 107#ifndef BOOL_TYPE_SIZE
e1cd6e56 108#ifdef SLOW_BYTE_ACCESS
d2e5ee5c 109#define BOOL_TYPE_SIZE ((SLOW_BYTE_ACCESS) ? (POINTER_SIZE) : (CHAR_TYPE_SIZE))
e1cd6e56 110#else
d2e5ee5c 111#define BOOL_TYPE_SIZE CHAR_TYPE_SIZE
e1cd6e56 112#endif
255512c1
JM
113#endif
114
8d08fdba
MS
115/* We let tm.h override the types used here, to handle trivial differences
116 such as the choice of unsigned int or long unsigned int for size_t.
117 When machines start needing nontrivial differences in the size type,
118 it would be best to do something here to figure out automatically
119 from other information what type to use. */
120
121#ifndef SIZE_TYPE
122#define SIZE_TYPE "long unsigned int"
123#endif
124
125#ifndef PTRDIFF_TYPE
126#define PTRDIFF_TYPE "long int"
127#endif
128
129#ifndef WCHAR_TYPE
130#define WCHAR_TYPE "int"
131#endif
132
8d08fdba 133static tree grokparms PROTO((tree, int));
d8e178a0 134static const char *redeclaration_error_message PROTO((tree, tree));
8d08fdba 135
49c249e1
JM
136static struct stack_level *push_decl_level PROTO((struct stack_level *,
137 struct obstack *));
138static void push_binding_level PROTO((struct binding_level *, int,
139 int));
140static void pop_binding_level PROTO((void));
141static void suspend_binding_level PROTO((void));
142static void resume_binding_level PROTO((struct binding_level *));
143static struct binding_level *make_binding_level PROTO((void));
49c249e1 144static void declare_namespace_level PROTO((void));
1ddb2906 145static void signal_catch PROTO((int)) ATTRIBUTE_NORETURN;
49c249e1 146static void storedecls PROTO((tree));
49c249e1
JM
147static void require_complete_types_for_parms PROTO((tree));
148static void push_overloaded_decl_1 PROTO((tree));
149static int ambi_op_p PROTO((tree));
150static int unary_op_p PROTO((tree));
151static tree store_bindings PROTO((tree, tree));
152static tree lookup_tag_reverse PROTO((tree, tree));
153static tree obscure_complex_init PROTO((tree, tree));
154static tree maybe_build_cleanup_1 PROTO((tree, tree));
3e3f722c 155static tree lookup_name_real PROTO((tree, int, int, int));
49c249e1 156static void warn_extern_redeclared_static PROTO((tree, tree));
a703fb38 157static void grok_reference_init PROTO((tree, tree, tree));
386b8a85 158static tree grokfndecl PROTO((tree, tree, tree, tree, int,
2c73f9f5 159 enum overload_flags, tree,
7a8f9fa9 160 tree, int, int, int, int, int, int, tree));
2c73f9f5 161static tree grokvardecl PROTO((tree, tree, RID_BIT_TYPE *, int, int, tree));
49c249e1
JM
162static tree lookup_tag PROTO((enum tree_code, tree,
163 struct binding_level *, int));
164static void set_identifier_type_value_with_scope
165 PROTO((tree, tree, struct binding_level *));
d8e178a0
KG
166static void record_builtin_type PROTO((enum rid, const char *, tree));
167static void record_unknown_type PROTO((tree, const char *));
168static int member_function_or_else PROTO((tree, tree, const char *));
169static void bad_specifiers PROTO((tree, const char *, int, int, int, int,
49c249e1 170 int));
8df4696d 171static void lang_print_error_function PROTO((const char *));
39c01e4c 172static tree maybe_process_template_type_declaration PROTO((tree, int, struct binding_level*));
91063b51 173static void check_for_uninitialized_const_var PROTO((tree));
7ddedda4
MM
174static unsigned long typename_hash PROTO((hash_table_key));
175static boolean typename_compare PROTO((hash_table_key, hash_table_key));
f181d4ae 176static void push_binding PROTO((tree, tree, struct binding_level*));
8f032717 177static int add_binding PROTO((tree, tree));
d8f8dca1 178static void pop_binding PROTO((tree, tree));
297e73d8 179static tree local_variable_p PROTO((tree));
d8e178a0
KG
180static tree find_binding PROTO((tree, tree));
181static tree select_decl PROTO((tree, int));
d8e178a0
KG
182static int lookup_flags PROTO((int, int));
183static tree qualify_lookup PROTO((tree, int));
184static tree record_builtin_java_type PROTO((const char *, int));
185static const char *tag_name PROTO((enum tag_types code));
8f032717
MM
186static void find_class_binding_level PROTO((void));
187static struct binding_level *innermost_nonclass_level PROTO((void));
b74a0560 188static tree poplevel_class PROTO((void));
235f734d 189static void warn_about_implicit_typename_lookup PROTO((tree, tree));
0aafb128
MM
190static int walk_namespaces_r PROTO((tree, walk_namespaces_fn, void *));
191static int walk_globals_r PROTO((tree, void *));
cfe507be 192static void add_decl_to_level PROTO((tree, struct binding_level *));
8d08fdba 193
69ac77ce
JL
194#if defined (DEBUG_CP_BINDING_LEVELS)
195static void indent PROTO((void));
196#endif
197
2c73f9f5 198/* A node which has tree code ERROR_MARK, and whose type is itself.
8d08fdba
MS
199 All erroneous expressions are replaced with this node. All functions
200 that accept nodes as arguments should avoid generating error messages
201 if this node is one of the arguments, since it is undesirable to get
202 multiple error messages from one error in the input. */
203
204tree error_mark_node;
205
206/* Erroneous argument lists can use this *IFF* they do not modify it. */
207tree error_mark_list;
208
7f4edbcb
BS
209/* The following symbols are subsumed in the cp_global_trees array, and
210 listed here individually for documentation purposes.
8d08fdba 211
7f4edbcb
BS
212 C++ extensions
213 tree wchar_decl_node;
214 tree void_zero_node;
37c46b43 215
7f4edbcb
BS
216 tree vtable_entry_type;
217 tree delta_type_node;
218#if 0
219 Old rtti stuff.
220 tree __baselist_desc_type_node;
221 tree __i_desc_type_node, __m_desc_type_node;
222 tree __t_desc_array_type, __i_desc_array_type, __m_desc_array_type;
946dc1c8 223#endif
7f4edbcb
BS
224 tree __t_desc_type_node;
225#if 0
226 tree __tp_desc_type_node;
227#endif
228 tree __access_mode_type_node;
229 tree __bltn_desc_type_node, __user_desc_type_node, __class_desc_type_node;
230 tree __ptr_desc_type_node, __attr_desc_type_node, __func_desc_type_node;
231 tree __ptmf_desc_type_node, __ptmd_desc_type_node;
232#if 0
233 Not needed yet? May be needed one day?
234 tree __bltn_desc_array_type, __user_desc_array_type, __class_desc_array_type;
235 tree __ptr_desc_array_type, __attr_dec_array_type, __func_desc_array_type;
236 tree __ptmf_desc_array_type, __ptmd_desc_array_type;
946dc1c8 237#endif
8d08fdba 238
7f4edbcb
BS
239 tree class_star_type_node;
240 tree class_type_node, record_type_node, union_type_node, enum_type_node;
241 tree unknown_type_node;
8d08fdba 242
7f4edbcb 243 Array type `vtable_entry_type[]'
8d08fdba 244
7f4edbcb
BS
245 tree vtbl_type_node;
246 tree vtbl_ptr_type_node;
8d08fdba 247
7f4edbcb 248 Nnamespace std
8d08fdba 249
7f4edbcb 250 tree std_node;
8d08fdba 251
7f4edbcb
BS
252 A FUNCTION_DECL which can call `abort'. Not necessarily the
253 one that the user will declare, but sufficient to be called
254 by routines that want to abort the program.
8d08fdba 255
7f4edbcb 256 tree abort_fndecl;
8d08fdba 257
7f4edbcb 258 The FUNCTION_DECL for the default `::operator delete'.
2986ae00 259
7f4edbcb 260 tree global_delete_fndecl;
8d08fdba 261
7f4edbcb
BS
262 Used by RTTI
263 tree type_info_type_node, tinfo_fn_id, tinfo_fn_type;
8d08fdba 264
7f4edbcb 265*/
8d08fdba 266
7f4edbcb 267tree cp_global_trees[CPTI_MAX];
8d08fdba 268
7f4edbcb
BS
269/* These can't be part of the above array, since they are declared
270 individually in tree.h, and used by the debug output routines. */
8d08fdba 271
7f4edbcb
BS
272tree void_type_node;
273tree char_type_node;
274tree integer_type_node;
275tree unsigned_type_node;
db5ae43f 276
4a4b0aa3
RH
277/* These can't be part of the above array, since they are declared
278 individially in tree.h and used by the target routines. */
279
280tree ptr_type_node;
281tree va_list_type_node;
282
2c73f9f5 283/* Indicates that there is a type value in some namespace, although
7f4edbcb 284 that is not necessarily in scope at the moment. */
2c73f9f5
ML
285
286static tree global_type_node;
287
7f4edbcb 288/* Namespace std. */
0dde4175 289int in_std;
6633d636 290
3e3f722c
ML
291/* Expect only namespace names now. */
292static int only_namespace_names;
293
8d08fdba
MS
294/* In a destructor, the point at which all derived class destroying
295 has been done, just before any base class destroying will be done. */
296
297tree dtor_label;
298
72b7eeff
MS
299/* In a destructor, the last insn emitted after the start of the
300 function and the parms. */
301
bd6dd845 302static rtx last_dtor_insn;
72b7eeff 303
b87692e5 304/* In a constructor, the last insn emitted after the start of the
9664b89e
JM
305 function and the parms, the exception specification and any
306 function-try-block. The constructor initializers are emitted after
307 this insn. */
b87692e5
MS
308
309static rtx last_parm_cleanup_insn;
310
8d08fdba
MS
311/* In a constructor, the point at which we are ready to return
312 the pointer to the initialized object. */
313
314tree ctor_label;
315
8d08fdba
MS
316/* If original DECL_RESULT of current function was a register,
317 but due to being an addressable named return value, would up
318 on the stack, this variable holds the named return value's
319 original location. */
bd6dd845 320static rtx original_result_rtx;
8d08fdba
MS
321
322/* Sequence of insns which represents base initialization. */
a9aedbc2 323tree base_init_expr;
8d08fdba
MS
324
325/* C++: Keep these around to reduce calls to `get_identifier'.
326 Identifiers for `this' in member functions and the auto-delete
327 parameter for destructors. */
328tree this_identifier, in_charge_identifier;
fc378698 329tree ctor_identifier, dtor_identifier;
e92cc029 330/* Used in pointer to member functions, in vtables, and in sigtables. */
8d08fdba 331tree pfn_identifier, index_identifier, delta_identifier, delta2_identifier;
9dd70aa4 332tree pfn_or_delta2_identifier, tag_identifier;
5566b478 333tree vt_off_identifier;
8d08fdba 334
4cc1d462
NS
335/* Exception specifier used for throw(). */
336tree empty_except_spec;
337
0dde4175
JM
338/* Nonzero if we're in a handler for a function-try-block. */
339int in_function_try_handler;
340
e349ee73
MS
341struct named_label_list
342{
343 struct binding_level *binding_level;
344 tree names_in_scope;
345 tree label_decl;
9c0758dd 346 const char *filename_o_goto;
e349ee73
MS
347 int lineno_o_goto;
348 struct named_label_list *next;
349};
350
8d08fdba
MS
351/* A list (chain of TREE_LIST nodes) of named label uses.
352 The TREE_PURPOSE field is the list of variables defined
38e01259 353 in the label's scope defined at the point of use.
8d08fdba
MS
354 The TREE_VALUE field is the LABEL_DECL used.
355 The TREE_TYPE field holds `current_binding_level' at the
356 point of the label's use.
357
e349ee73
MS
358 BWAHAHAAHAHahhahahahaah. No, no, no, said the little chicken.
359
360 Look at the pretty struct named_label_list. See the pretty struct
361 with the pretty named fields that describe what they do. See the
362 pretty lack of gratuitous casts. Notice the code got a lot cleaner.
363
8d08fdba
MS
364 Used only for jumps to as-yet undefined labels, since
365 jumps to defined labels can have their validity checked
366 by stmt.c. */
367
e349ee73 368static struct named_label_list *named_label_uses = NULL;
8d08fdba
MS
369
370/* A list of objects which have constructors or destructors
371 which reside in the global scope. The decl is stored in
372 the TREE_VALUE slot and the initializer is stored
373 in the TREE_PURPOSE slot. */
374tree static_aggregates;
375
8d08fdba
MS
376/* -- end of C++ */
377
378/* Two expressions that are constants with value zero.
379 The first is of type `int', the second of type `void *'. */
380
381tree integer_zero_node;
382tree null_pointer_node;
383
03d0f4af
MM
384/* The value for __null (NULL), namely, a zero of an integer type with
385 the same number of bits as a pointer. */
d11ad92e
MS
386tree null_node;
387
8d08fdba
MS
388/* A node for the integer constants 1, 2, and 3. */
389
390tree integer_one_node, integer_two_node, integer_three_node;
391
8d08fdba
MS
392/* While defining an enum type, this is 1 plus the last enumerator
393 constant value. */
394
395static tree enum_next_value;
396
7177d104
MS
397/* Nonzero means that there was overflow computing enum_next_value. */
398
399static int enum_overflow;
400
8d08fdba
MS
401/* Parsing a function declarator leaves a list of parameter names
402 or a chain or parameter decls here. */
403
404tree last_function_parms;
405
406/* Parsing a function declarator leaves here a chain of structure
407 and enum types declared in the parmlist. */
408
409static tree last_function_parm_tags;
410
411/* After parsing the declarator that starts a function definition,
412 `start_function' puts here the list of parameter names or chain of decls.
413 `store_parm_decls' finds it here. */
414
415static tree current_function_parms;
416
417/* Similar, for last_function_parm_tags. */
418static tree current_function_parm_tags;
419
420/* A list (chain of TREE_LIST nodes) of all LABEL_DECLs in the function
421 that have names. Here so we can clear out their names' definitions
422 at the end of the function. */
423
424static tree named_labels;
425
426/* A list of LABEL_DECLs from outer contexts that are currently shadowed. */
427
428static tree shadowed_labels;
429
8d08fdba
MS
430/* The FUNCTION_DECL for the function currently being compiled,
431 or 0 if between functions. */
432tree current_function_decl;
433
434/* Set to 0 at beginning of a function definition, set to 1 if
435 a return statement that specifies a return value is seen. */
436
437int current_function_returns_value;
438
439/* Set to 0 at beginning of a function definition, set to 1 if
440 a return statement with no argument is seen. */
441
442int current_function_returns_null;
443
444/* Set to 0 at beginning of a function definition, and whenever
445 a label (case or named) is defined. Set to value of expression
446 returned from function when that value can be transformed into
447 a named return value. */
448
449tree current_function_return_value;
450
8d08fdba
MS
451/* Nonzero means give `double' the same size as `float'. */
452
453extern int flag_short_double;
454
455/* Nonzero means don't recognize any builtin functions. */
456
457extern int flag_no_builtin;
458
00595019
MS
459/* Nonzero means don't recognize the non-ANSI builtin functions.
460 -ansi sets this. */
461
462extern int flag_no_nonansi_builtin;
463
e1cd6e56
MS
464/* Nonzero means enable obscure ANSI features and disable GNU extensions
465 that might cause ANSI-compliant code to be miscompiled. */
8d08fdba
MS
466
467extern int flag_ansi;
468
469/* Nonzero if we want to support huge (> 2^(sizeof(short)*8-1) bytes)
e92cc029 470 objects. */
8d08fdba
MS
471extern int flag_huge_objects;
472
473/* Nonzero if we want to conserve space in the .o files. We do this
474 by putting uninitialized data and runtime initialized data into
ddd5a7c1 475 .common instead of .data at the expense of not flagging multiple
8d08fdba
MS
476 definitions. */
477extern int flag_conserve_space;
478
479/* Pointers to the base and current top of the language name stack. */
480
481extern tree *current_lang_base, *current_lang_stack;
482\f
51c184be 483/* C and C++ flags are in decl2.c. */
8d08fdba
MS
484
485/* Set to 0 at beginning of a constructor, set to 1
486 if that function does an allocation before referencing its
487 instance variable. */
5566b478 488static int current_function_assigns_this;
8d08fdba
MS
489int current_function_just_assigned_this;
490
491/* Set to 0 at beginning of a function. Set non-zero when
492 store_parm_decls is called. Don't call store_parm_decls
493 if this flag is non-zero! */
494int current_function_parms_stored;
495
51c184be 496/* Flag used when debugging spew.c */
8d08fdba
MS
497
498extern int spew_debug;
499
500/* This is a copy of the class_shadowed list of the previous class binding
501 contour when at global scope. It's used to reset IDENTIFIER_CLASS_VALUEs
502 when entering another class scope (i.e. a cache miss). */
503extern tree previous_class_values;
504
e92cc029
MS
505/* A expression of value 0 with the same precision as a sizetype
506 node, but signed. */
507tree signed_size_zero_node;
508
0c8feefe
MM
509/* The name of the anonymous namespace, throughout this translation
510 unit. */
511tree anonymous_namespace_name;
512
8d08fdba
MS
513\f
514/* Allocate a level of searching. */
e92cc029 515
824b9a4c 516static
8d08fdba
MS
517struct stack_level *
518push_decl_level (stack, obstack)
519 struct stack_level *stack;
520 struct obstack *obstack;
521{
522 struct stack_level tem;
523 tem.prev = stack;
524
525 return push_stack_level (obstack, (char *)&tem, sizeof (tem));
526}
527\f
528/* For each binding contour we allocate a binding_level structure
e92cc029
MS
529 which records the names defined in that contour.
530 Contours include:
531 0) the global one
532 1) one for each function definition,
533 where internal declarations of the parameters appear.
534 2) one for each compound statement,
535 to record its declarations.
536
537 The current meaning of a name can be found by searching the levels
538 from the current one out to the global one.
539
540 Off to the side, may be the class_binding_level. This exists only
541 to catch class-local declarations. It is otherwise nonexistent.
542
543 Also there may be binding levels that catch cleanups that must be
d8f8dca1
MM
544 run when exceptions occur. Thus, to see whether a name is bound in
545 the current scope, it is not enough to look in the
546 CURRENT_BINDING_LEVEL. You should use lookup_name_current_level
547 instead. */
8d08fdba
MS
548
549/* Note that the information in the `names' component of the global contour
550 is duplicated in the IDENTIFIER_GLOBAL_VALUEs of all identifiers. */
551
552struct binding_level
553 {
554 /* A chain of _DECL nodes for all variables, constants, functions,
e92cc029 555 and typedef types. These are in the reverse of the order
f181d4ae
MM
556 supplied. There may be OVERLOADs on this list, too, but they
557 are wrapped in TREE_LISTs; the TREE_VALUE is the OVERLOAD. */
8d08fdba
MS
558 tree names;
559
e92cc029
MS
560 /* A list of structure, union and enum definitions, for looking up
561 tag names.
562 It is a chain of TREE_LIST nodes, each of whose TREE_PURPOSE is a name,
563 or NULL_TREE; and whose TREE_VALUE is a RECORD_TYPE, UNION_TYPE,
564 or ENUMERAL_TYPE node.
565
566 C++: the TREE_VALUE nodes can be simple types for
567 component_bindings. */
8d08fdba
MS
568 tree tags;
569
2c73f9f5
ML
570 /* A list of USING_DECL nodes. */
571 tree usings;
572
ea9635c7
ML
573 /* A list of used namespaces. PURPOSE is the namespace,
574 VALUE the common ancestor with this binding_level's namespace. */
575 tree using_directives;
576
f181d4ae
MM
577 /* If this binding level is the binding level for a class, then
578 class_shadowed is a TREE_LIST. The TREE_PURPOSE of each node
579 is the name of an entity bound in the class; the TREE_VALUE is
580 the IDENTIFIER_CLASS_VALUE before we entered the class. Thus,
581 when leaving class scope, we can restore the
d8f8dca1
MM
582 IDENTIFIER_CLASS_VALUE by walking this list. The TREE_TYPE is
583 the DECL bound by this name in the class. */
8d08fdba
MS
584 tree class_shadowed;
585
f181d4ae
MM
586 /* Similar to class_shadowed, but for IDENTIFIER_TYPE_VALUE, and
587 is used for all binding levels. */
8d08fdba
MS
588 tree type_shadowed;
589
590 /* For each level (except not the global one),
591 a chain of BLOCK nodes for all the levels
592 that were entered and exited one level down. */
593 tree blocks;
594
595 /* The BLOCK node for this level, if one has been preallocated.
596 If 0, the BLOCK is allocated (if needed) when the level is popped. */
597 tree this_block;
598
599 /* The binding level which this one is contained in (inherits from). */
600 struct binding_level *level_chain;
601
f30432d7 602 /* List of decls in `names' that have incomplete
8d08fdba 603 structure or union types. */
f30432d7 604 tree incomplete;
8d08fdba 605
8d6e462b 606 /* List of VAR_DECLS saved from a previous for statement.
abc95ed3 607 These would be dead in ANSI-conforming code, but might
f181d4ae
MM
608 be referenced in ARM-era code. These are stored in a
609 TREE_LIST; the TREE_VALUE is the actual declaration. */
8d6e462b
PB
610 tree dead_vars_from_for;
611
8d08fdba
MS
612 /* 1 for the level that holds the parameters of a function.
613 2 for the level that holds a class declaration.
614 3 for levels that hold parameter declarations. */
615 unsigned parm_flag : 4;
616
617 /* 1 means make a BLOCK for this level regardless of all else.
618 2 for temporary binding contours created by the compiler. */
619 unsigned keep : 3;
620
621 /* Nonzero if this level "doesn't exist" for tags. */
622 unsigned tag_transparent : 1;
623
624 /* Nonzero if this level can safely have additional
625 cleanup-needing variables added to it. */
626 unsigned more_cleanups_ok : 1;
627 unsigned have_cleanups : 1;
628
5566b478
MS
629 /* Nonzero if this level is for storing the decls for template
630 parameters and generic decls; these decls will be discarded and
631 replaced with a TEMPLATE_DECL. */
8d08fdba
MS
632 unsigned pseudo_global : 1;
633
a9aedbc2
MS
634 /* This is set for a namespace binding level. */
635 unsigned namespace_p : 1;
636
2ee887f2 637 /* True if this level is that of a for-statement where we need to
d22c8596 638 worry about ambiguous (ARM or ANSI) scope rules. */
8d6e462b
PB
639 unsigned is_for_scope : 1;
640
227cf171
JM
641 /* True if this level corresponds to an EH region, as for a try block. */
642 unsigned eh_region : 1;
643
644 /* One bit left for this word. */
8d08fdba
MS
645
646#if defined(DEBUG_CP_BINDING_LEVELS)
647 /* Binding depth at which this level began. */
648 unsigned binding_depth;
649#endif /* defined(DEBUG_CP_BINDING_LEVELS) */
650 };
651
652#define NULL_BINDING_LEVEL ((struct binding_level *) NULL)
653
8f032717 654/* The binding level currently in effect. */
8d08fdba
MS
655
656static struct binding_level *current_binding_level;
657
658/* The binding level of the current class, if any. */
659
660static struct binding_level *class_binding_level;
661
8d08fdba
MS
662/* A chain of binding_level structures awaiting reuse. */
663
664static struct binding_level *free_binding_level;
665
666/* The outermost binding level, for names of file scope.
667 This is created when the compiler is started and exists
668 through the entire run. */
669
670static struct binding_level *global_binding_level;
671
672/* Binding level structures are initialized by copying this one. */
673
674static struct binding_level clear_binding_level;
675
676/* Nonzero means unconditionally make a BLOCK for the next level pushed. */
677
678static int keep_next_level_flag;
679
680#if defined(DEBUG_CP_BINDING_LEVELS)
681static int binding_depth = 0;
682static int is_class_level = 0;
683
684static void
685indent ()
686{
687 register unsigned i;
688
689 for (i = 0; i < binding_depth*2; i++)
690 putc (' ', stderr);
691}
692#endif /* defined(DEBUG_CP_BINDING_LEVELS) */
693
694static tree pushdecl_with_scope PROTO((tree, struct binding_level *));
695
696static void
697push_binding_level (newlevel, tag_transparent, keep)
698 struct binding_level *newlevel;
699 int tag_transparent, keep;
700{
701 /* Add this level to the front of the chain (stack) of levels that
702 are active. */
703 *newlevel = clear_binding_level;
8f032717 704 newlevel->level_chain = current_binding_level;
8d08fdba
MS
705 current_binding_level = newlevel;
706 newlevel->tag_transparent = tag_transparent;
707 newlevel->more_cleanups_ok = 1;
227cf171
JM
708
709 /* We are called before expand_start_bindings, but after
710 expand_eh_region_start for a try block; so we check this now,
711 before the EH block is covered up. */
712 newlevel->eh_region = is_eh_region ();
713
8d08fdba
MS
714 newlevel->keep = keep;
715#if defined(DEBUG_CP_BINDING_LEVELS)
716 newlevel->binding_depth = binding_depth;
717 indent ();
718 fprintf (stderr, "push %s level 0x%08x line %d\n",
719 (is_class_level) ? "class" : "block", newlevel, lineno);
720 is_class_level = 0;
721 binding_depth++;
722#endif /* defined(DEBUG_CP_BINDING_LEVELS) */
723}
724
8f032717
MM
725/* Find the innermost enclosing class scope, and reset
726 CLASS_BINDING_LEVEL appropriately. */
727
728static void
729find_class_binding_level ()
730{
731 struct binding_level *level = current_binding_level;
732
733 while (level && level->parm_flag != 2)
734 level = level->level_chain;
735 if (level && level->parm_flag == 2)
736 class_binding_level = level;
737 else
738 class_binding_level = 0;
739}
740
8d08fdba
MS
741static void
742pop_binding_level ()
743{
8d08fdba
MS
744 if (global_binding_level)
745 {
2c73f9f5 746 /* Cannot pop a level, if there are none left to pop. */
8d08fdba
MS
747 if (current_binding_level == global_binding_level)
748 my_friendly_abort (123);
749 }
750 /* Pop the current level, and free the structure for reuse. */
751#if defined(DEBUG_CP_BINDING_LEVELS)
752 binding_depth--;
753 indent ();
754 fprintf (stderr, "pop %s level 0x%08x line %d\n",
755 (is_class_level) ? "class" : "block",
756 current_binding_level, lineno);
757 if (is_class_level != (current_binding_level == class_binding_level))
9e9ff709
MS
758 {
759 indent ();
760 fprintf (stderr, "XXX is_class_level != (current_binding_level == class_binding_level)\n");
761 }
8d08fdba
MS
762 is_class_level = 0;
763#endif /* defined(DEBUG_CP_BINDING_LEVELS) */
764 {
765 register struct binding_level *level = current_binding_level;
766 current_binding_level = current_binding_level->level_chain;
767 level->level_chain = free_binding_level;
768#if 0 /* defined(DEBUG_CP_BINDING_LEVELS) */
769 if (level->binding_depth != binding_depth)
770 abort ();
771#endif /* defined(DEBUG_CP_BINDING_LEVELS) */
8f032717
MM
772 free_binding_level = level;
773 find_class_binding_level ();
8d08fdba
MS
774 }
775}
a9aedbc2
MS
776
777static void
778suspend_binding_level ()
779{
780 if (class_binding_level)
781 current_binding_level = class_binding_level;
782
783 if (global_binding_level)
784 {
2c73f9f5 785 /* Cannot suspend a level, if there are none left to suspend. */
a9aedbc2
MS
786 if (current_binding_level == global_binding_level)
787 my_friendly_abort (123);
788 }
789 /* Suspend the current level. */
790#if defined(DEBUG_CP_BINDING_LEVELS)
791 binding_depth--;
792 indent ();
793 fprintf (stderr, "suspend %s level 0x%08x line %d\n",
794 (is_class_level) ? "class" : "block",
795 current_binding_level, lineno);
796 if (is_class_level != (current_binding_level == class_binding_level))
9e9ff709
MS
797 {
798 indent ();
799 fprintf (stderr, "XXX is_class_level != (current_binding_level == class_binding_level)\n");
800 }
a9aedbc2
MS
801 is_class_level = 0;
802#endif /* defined(DEBUG_CP_BINDING_LEVELS) */
8f032717
MM
803 current_binding_level = current_binding_level->level_chain;
804 find_class_binding_level ();
a9aedbc2
MS
805}
806
824b9a4c 807static void
a9aedbc2
MS
808resume_binding_level (b)
809 struct binding_level *b;
810{
2c73f9f5
ML
811 /* Resuming binding levels is meant only for namespaces,
812 and those cannot nest into classes. */
813 my_friendly_assert(!class_binding_level, 386);
814 /* Also, resuming a non-directly nested namespace is a no-no. */
815 my_friendly_assert(b->level_chain == current_binding_level, 386);
a9aedbc2
MS
816 current_binding_level = b;
817#if defined(DEBUG_CP_BINDING_LEVELS)
818 b->binding_depth = binding_depth;
819 indent ();
820 fprintf (stderr, "resume %s level 0x%08x line %d\n",
821 (is_class_level) ? "class" : "block", b, lineno);
822 is_class_level = 0;
823 binding_depth++;
824#endif /* defined(DEBUG_CP_BINDING_LEVELS) */
825}
8d08fdba 826\f
cffa8729
MS
827/* Create a new `struct binding_level'. */
828
829static
830struct binding_level *
831make_binding_level ()
832{
833 /* NOSTRICT */
834 return (struct binding_level *) xmalloc (sizeof (struct binding_level));
835}
836
8d08fdba
MS
837/* Nonzero if we are currently in the global binding level. */
838
839int
840global_bindings_p ()
841{
842 return current_binding_level == global_binding_level;
843}
844
8f032717
MM
845/* Return the innermost binding level that is not for a class scope. */
846
847static struct binding_level *
848innermost_nonclass_level ()
849{
850 struct binding_level *b;
851
852 b = current_binding_level;
853 while (b->parm_flag == 2)
854 b = b->level_chain;
855
856 return b;
857}
858
a9aedbc2
MS
859/* Nonzero if we are currently in a toplevel binding level. This
860 means either the global binding level or a namespace in a toplevel
8f032717
MM
861 binding level. Since there are no non-toplevel namespace levels,
862 this really means any namespace or pseudo-global level. We also
863 include a class whose context is toplevel. */
a9aedbc2
MS
864
865int
866toplevel_bindings_p ()
867{
8f032717
MM
868 struct binding_level *b = innermost_nonclass_level ();
869
870 return b->namespace_p || b->pseudo_global;
a9aedbc2
MS
871}
872
8f032717
MM
873/* Nonzero if this is a namespace scope, or if we are defining a class
874 which is itself at namespace scope, or whose enclosing class is
875 such a class, etc. */
a9aedbc2 876
7bdbfa05 877int
a9aedbc2
MS
878namespace_bindings_p ()
879{
8f032717
MM
880 struct binding_level *b = innermost_nonclass_level ();
881
882 return b->namespace_p;
a9aedbc2
MS
883}
884
8d08fdba
MS
885void
886keep_next_level ()
887{
888 keep_next_level_flag = 1;
889}
890
891/* Nonzero if the current level needs to have a BLOCK made. */
892
893int
894kept_level_p ()
895{
896 return (current_binding_level->blocks != NULL_TREE
897 || current_binding_level->keep
898 || current_binding_level->names != NULL_TREE
899 || (current_binding_level->tags != NULL_TREE
900 && !current_binding_level->tag_transparent));
901}
902
903/* Identify this binding level as a level of parameters. */
904
905void
906declare_parm_level ()
907{
908 current_binding_level->parm_flag = 1;
909}
910
8d08fdba
MS
911void
912declare_pseudo_global_level ()
913{
914 current_binding_level->pseudo_global = 1;
915}
916
824b9a4c 917static void
a9aedbc2
MS
918declare_namespace_level ()
919{
920 current_binding_level->namespace_p = 1;
921}
922
8d08fdba
MS
923int
924pseudo_global_level_p ()
925{
9188c363 926 return current_binding_level->pseudo_global;
8d08fdba
MS
927}
928
929void
930set_class_shadows (shadows)
931 tree shadows;
932{
933 class_binding_level->class_shadowed = shadows;
934}
935
936/* Enter a new binding level.
937 If TAG_TRANSPARENT is nonzero, do so only for the name space of variables,
938 not for that of tags. */
939
940void
941pushlevel (tag_transparent)
942 int tag_transparent;
943{
944 register struct binding_level *newlevel = NULL_BINDING_LEVEL;
945
946 /* If this is the top level of a function,
947 just make sure that NAMED_LABELS is 0.
948 They should have been set to 0 at the end of the previous function. */
949
950 if (current_binding_level == global_binding_level)
951 my_friendly_assert (named_labels == NULL_TREE, 134);
952
953 /* Reuse or create a struct for this binding level. */
954
955#if defined(DEBUG_CP_BINDING_LEVELS)
956 if (0)
957#else /* !defined(DEBUG_CP_BINDING_LEVELS) */
958 if (free_binding_level)
959#endif /* !defined(DEBUG_CP_BINDING_LEVELS) */
960 {
961 newlevel = free_binding_level;
962 free_binding_level = free_binding_level->level_chain;
963 }
964 else
965 {
cffa8729 966 newlevel = make_binding_level ();
8d08fdba 967 }
cffa8729 968
8d08fdba
MS
969 push_binding_level (newlevel, tag_transparent, keep_next_level_flag);
970 GNU_xref_start_scope ((HOST_WIDE_INT) newlevel);
971 keep_next_level_flag = 0;
972}
973
5566b478 974void
8d6e462b
PB
975note_level_for_for ()
976{
977 current_binding_level->is_for_scope = 1;
978}
979
8d08fdba
MS
980void
981pushlevel_temporary (tag_transparent)
982 int tag_transparent;
983{
984 pushlevel (tag_transparent);
985 current_binding_level->keep = 2;
986 clear_last_expr ();
987
988 /* Note we don't call push_momentary() here. Otherwise, it would cause
989 cleanups to be allocated on the momentary obstack, and they will be
990 overwritten by the next statement. */
991
992 expand_start_bindings (0);
993}
994
f181d4ae
MM
995/* For a binding between a name and an entity at a block scope,
996 this is the `struct binding_level' for the block. */
997#define BINDING_LEVEL(NODE) \
c7a932b1 998 (((struct tree_binding*)NODE)->scope.level)
f181d4ae
MM
999
1000/* These are currently unused, but permanent, CPLUS_BINDING nodes.
1001 They are kept here because they are allocated from the permanent
1002 obstack and cannot be easily freed. */
1003static tree free_binding_nodes;
1004
1005/* Make DECL the innermost binding for ID. The LEVEL is the binding
1006 level at which this declaration is being bound. */
1007
1008static void
1009push_binding (id, decl, level)
1010 tree id;
1011 tree decl;
1012 struct binding_level* level;
1013{
1014 tree binding;
1015
1016 if (!free_binding_nodes)
1017 {
1018 /* There are no free nodes, so we must build one here. */
9188c363 1019 push_permanent_obstack ();
f181d4ae
MM
1020 binding = make_node (CPLUS_BINDING);
1021 pop_obstacks ();
1022 }
1023 else
1024 {
1025 /* There are nodes on the free list. Grab the first one. */
1026 binding = free_binding_nodes;
1027
1028 /* And update the free list. */
1029 free_binding_nodes = TREE_CHAIN (free_binding_nodes);
1030 }
1031
1032 /* Now, fill in the binding information. */
1033 BINDING_VALUE (binding) = decl;
d8f8dca1 1034 BINDING_TYPE (binding) = NULL_TREE;
f181d4ae 1035 BINDING_LEVEL (binding) = level;
8f032717 1036 INHERITED_VALUE_BINDING_P (binding) = 0;
f181d4ae
MM
1037 LOCAL_BINDING_P (binding) = (level != class_binding_level);
1038
c45df9c1 1039 /* And put it on the front of the list of bindings for ID. */
f181d4ae
MM
1040 TREE_CHAIN (binding) = IDENTIFIER_BINDING (id);
1041 IDENTIFIER_BINDING (id) = binding;
1042}
1043
d8f8dca1
MM
1044/* ID is already bound in the current scope. But, DECL is an
1045 additional binding for ID in the same scope. This is the `struct
1046 stat' hack whereby a non-typedef class-name or enum-name can be
1047 bound at the same level as some other kind of entity. It's the
1048 responsibility of the caller to check that inserting this name is
8f032717
MM
1049 legal here. Returns nonzero if the new binding was successful. */
1050static int
d8f8dca1
MM
1051add_binding (id, decl)
1052 tree id;
1053 tree decl;
1054{
1055 tree binding = IDENTIFIER_BINDING (id);
8f032717 1056 int ok = 1;
d8f8dca1
MM
1057
1058 if (TREE_CODE (decl) == TYPE_DECL && DECL_ARTIFICIAL (decl))
1059 /* The new name is the type name. */
1060 BINDING_TYPE (binding) = decl;
8f032717
MM
1061 else if (!BINDING_VALUE (binding))
1062 /* This situation arises when push_class_level_binding moves an
1063 inherited type-binding out of the way to make room for a new
1064 value binding. */
1065 BINDING_VALUE (binding) = decl;
1066 else if (TREE_CODE (BINDING_VALUE (binding)) == TYPE_DECL
1067 && DECL_ARTIFICIAL (BINDING_VALUE (binding)))
1068 {
1069 /* The old binding was a type name. It was placed in
1070 BINDING_VALUE because it was thought, at the point it was
1071 declared, to be the only entity with such a name. Move the
1072 type name into the type slot; it is now hidden by the new
1073 binding. */
d8f8dca1
MM
1074 BINDING_TYPE (binding) = BINDING_VALUE (binding);
1075 BINDING_VALUE (binding) = decl;
8f032717 1076 INHERITED_VALUE_BINDING_P (binding) = 0;
d8f8dca1 1077 }
263505af
MM
1078 else if (TREE_CODE (BINDING_VALUE (binding)) == TYPE_DECL
1079 && TREE_CODE (decl) == TYPE_DECL
1080 && DECL_NAME (decl) == DECL_NAME (BINDING_VALUE (binding))
1081 && same_type_p (TREE_TYPE (decl),
1082 TREE_TYPE (BINDING_VALUE (binding))))
1083 /* We have two typedef-names, both naming the same type to have
1084 the same name. This is OK because of:
1085
1086 [dcl.typedef]
1087
1088 In a given scope, a typedef specifier can be used to redefine
1089 the name of any type declared in that scope to refer to the
1090 type to which it already refers. */
1091 ok = 0;
8f032717
MM
1092 else
1093 {
1094 cp_error ("declaration of `%#D'", decl);
1095 cp_error_at ("conflicts with previous declaration `%#D'",
1096 BINDING_VALUE (binding));
1097 ok = 0;
1098 }
1099
1100 return ok;
d8f8dca1
MM
1101}
1102
cfe507be
MM
1103/* Add DECL to the list of things declared in B. */
1104
1105static void
1106add_decl_to_level (decl, b)
1107 tree decl;
1108 struct binding_level *b;
1109{
1110 /* Only things that will live forever should go in the global
1111 binding level. */
1112 my_friendly_assert (!(b == global_binding_level
1113 && !TREE_PERMANENT (decl)),
1114 19990817);
1115
1116 /* We build up the list in reverse order, and reverse it later if
1117 necessary. */
1118 TREE_CHAIN (decl) = b->names;
1119 b->names = decl;
1120}
1121
1122/* Bind DECL to ID in the current_binding_level, assumed to be a local
1123 binding level. If PUSH_USING is set in FLAGS, we know that DECL
1124 doesn't really belong to this binding level, that it got here
1125 through a using-declaration. */
f181d4ae
MM
1126
1127void
0034cf72 1128push_local_binding (id, decl, flags)
f181d4ae
MM
1129 tree id;
1130 tree decl;
0034cf72 1131 int flags;
f181d4ae 1132{
8f032717
MM
1133 struct binding_level *b;
1134
1135 /* Skip over any local classes. This makes sense if we call
1136 push_local_binding with a friend decl of a local class. */
1137 b = current_binding_level;
1138 while (b->parm_flag == 2)
1139 b = b->level_chain;
f181d4ae 1140
d8f8dca1 1141 if (lookup_name_current_level (id))
8f032717
MM
1142 {
1143 /* Supplement the existing binding. */
1144 if (!add_binding (id, decl))
1145 /* It didn't work. Something else must be bound at this
1146 level. Do not add DECL to the list of things to pop
1147 later. */
1148 return;
1149 }
d8f8dca1
MM
1150 else
1151 /* Create a new binding. */
8f032717 1152 push_binding (id, decl, b);
f181d4ae 1153
0034cf72 1154 if (TREE_CODE (decl) == OVERLOAD || (flags & PUSH_USING))
a06d48ef
JM
1155 /* We must put the OVERLOAD into a TREE_LIST since the
1156 TREE_CHAIN of an OVERLOAD is already used. Similarly for
1157 decls that got here through a using-declaration. */
1158 decl = build_tree_list (NULL_TREE, decl);
1159
f181d4ae
MM
1160 /* And put DECL on the list of things declared by the current
1161 binding level. */
cfe507be 1162 add_decl_to_level (decl, b);
f181d4ae
MM
1163}
1164
8f032717
MM
1165/* Bind DECL to ID in the class_binding_level. Returns nonzero if the
1166 binding was successful. */
f181d4ae 1167
8f032717 1168int
f181d4ae
MM
1169push_class_binding (id, decl)
1170 tree id;
1171 tree decl;
1172{
8f032717
MM
1173 int result = 1;
1174 tree binding = IDENTIFIER_BINDING (id);
1175 tree context;
1176
1177 /* Note that we declared this value so that we can issue an error if
1178 this an illegal redeclaration of a name already used for some
1179 other purpose. */
1180 note_name_declared_in_class (id, decl);
1181
1182 if (binding && BINDING_LEVEL (binding) == class_binding_level)
d8f8dca1 1183 /* Supplement the existing binding. */
8f032717 1184 result = add_binding (id, decl);
d8f8dca1
MM
1185 else
1186 /* Create a new binding. */
1187 push_binding (id, decl, class_binding_level);
1188
1189 /* Update the IDENTIFIER_CLASS_VALUE for this ID to be the
1190 class-level declaration. Note that we do not use DECL here
1191 because of the possibility of the `struct stat' hack; if DECL is
1192 a class-name or enum-name we might prefer a field-name, or some
1193 such. */
1194 IDENTIFIER_CLASS_VALUE (id) = BINDING_VALUE (IDENTIFIER_BINDING (id));
8f032717
MM
1195
1196 /* If this is a binding from a base class, mark it as such. */
1197 binding = IDENTIFIER_BINDING (id);
1198 if (BINDING_VALUE (binding) == decl && TREE_CODE (decl) != TREE_LIST)
1199 {
280f9385
MM
1200 /* Any implicit typename must be from a base-class. The
1201 context for an implicit typename declaration is always
1202 the derived class in which the lookup was done, so the checks
1203 based on the context of DECL below will not trigger. */
83233dca 1204 if (IMPLICIT_TYPENAME_TYPE_DECL_P (decl))
280f9385 1205 INHERITED_VALUE_BINDING_P (binding) = 1;
8f032717
MM
1206 else
1207 {
280f9385
MM
1208 if (TREE_CODE (decl) == OVERLOAD)
1209 context = DECL_REAL_CONTEXT (OVL_CURRENT (decl));
1210 else
1211 {
1212 my_friendly_assert (TREE_CODE_CLASS (TREE_CODE (decl)) == 'd',
1213 0);
1214 context = DECL_REAL_CONTEXT (decl);
1215 }
8f032717 1216
280f9385
MM
1217 if (is_properly_derived_from (current_class_type, context))
1218 INHERITED_VALUE_BINDING_P (binding) = 1;
1219 else
1220 INHERITED_VALUE_BINDING_P (binding) = 0;
1221 }
8f032717
MM
1222 }
1223 else if (BINDING_VALUE (binding) == decl)
1224 /* We only encounter a TREE_LIST when push_class_decls detects an
1225 ambiguity. Such an ambiguity can be overridden by a definition
1226 in this class. */
1227 INHERITED_VALUE_BINDING_P (binding) = 1;
1228
1229 return result;
f181d4ae
MM
1230}
1231
d8f8dca1
MM
1232/* Remove the binding for DECL which should be the innermost binding
1233 for ID. */
f181d4ae
MM
1234
1235static void
d8f8dca1 1236pop_binding (id, decl)
f181d4ae 1237 tree id;
d8f8dca1 1238 tree decl;
f181d4ae
MM
1239{
1240 tree binding;
d8f8dca1 1241
f181d4ae
MM
1242 if (id == NULL_TREE)
1243 /* It's easiest to write the loops that call this function without
1244 checking whether or not the entities involved have names. We
1245 get here for such an entity. */
1246 return;
1247
d8f8dca1 1248 /* Get the innermost binding for ID. */
f181d4ae 1249 binding = IDENTIFIER_BINDING (id);
f181d4ae 1250
d8f8dca1
MM
1251 /* The name should be bound. */
1252 my_friendly_assert (binding != NULL_TREE, 0);
1253
1254 /* The DECL will be either the ordinary binding or the type
1255 binding for this identifier. Remove that binding. */
1256 if (BINDING_VALUE (binding) == decl)
1257 BINDING_VALUE (binding) = NULL_TREE;
1258 else if (BINDING_TYPE (binding) == decl)
1259 BINDING_TYPE (binding) = NULL_TREE;
1260 else
1261 my_friendly_abort (0);
1262
1263 if (!BINDING_VALUE (binding) && !BINDING_TYPE (binding))
1264 {
1265 /* We're completely done with the innermost binding for this
1266 identifier. Unhook it from the list of bindings. */
1267 IDENTIFIER_BINDING (id) = TREE_CHAIN (binding);
1268
1269 /* And place it on the free list. */
1270 TREE_CHAIN (binding) = free_binding_nodes;
1271 free_binding_nodes = binding;
1272 }
f181d4ae
MM
1273}
1274
8d08fdba
MS
1275/* Exit a binding level.
1276 Pop the level off, and restore the state of the identifier-decl mappings
1277 that were in effect when this level was entered.
1278
1279 If KEEP == 1, this level had explicit declarations, so
1280 and create a "block" (a BLOCK node) for the level
1281 to record its declarations and subblocks for symbol table output.
1282
8d08fdba
MS
1283 If FUNCTIONBODY is nonzero, this level is the body of a function,
1284 so create a block as if KEEP were set and also clear out all
1285 label names.
1286
1287 If REVERSE is nonzero, reverse the order of decls before putting
1288 them into the BLOCK. */
1289
1290tree
1291poplevel (keep, reverse, functionbody)
1292 int keep;
1293 int reverse;
1294 int functionbody;
1295{
1296 register tree link;
1297 /* The chain of decls was accumulated in reverse order.
1298 Put it into forward order, just for cleanliness. */
1299 tree decls;
1300 int tmp = functionbody;
8d08fdba
MS
1301 int real_functionbody = current_binding_level->keep == 2
1302 ? ((functionbody = 0), tmp) : functionbody;
1303 tree tags = functionbody >= 0 ? current_binding_level->tags : 0;
1304 tree subblocks = functionbody >= 0 ? current_binding_level->blocks : 0;
1305 tree block = NULL_TREE;
1306 tree decl;
1307 int block_previously_created;
f181d4ae
MM
1308 int leaving_for_scope;
1309
b74a0560
MM
1310 if (current_binding_level->parm_flag == 2)
1311 return poplevel_class ();
1312
1313 my_friendly_assert (!current_binding_level->class_shadowed,
1314 19990414);
8d08fdba 1315
536333d4
MM
1316 /* We used to use KEEP == 2 to indicate that the new block should go
1317 at the beginning of the list of blocks at this binding level,
1318 rather than the end. This hack is no longer used. */
1319 my_friendly_assert (keep == 0 || keep == 1, 0);
1320
8d08fdba
MS
1321 GNU_xref_end_scope ((HOST_WIDE_INT) current_binding_level,
1322 (HOST_WIDE_INT) current_binding_level->level_chain,
1323 current_binding_level->parm_flag,
5566b478 1324 current_binding_level->keep);
8d08fdba
MS
1325
1326 if (current_binding_level->keep == 1)
1327 keep = 1;
1328
8d08fdba
MS
1329 /* Get the decls in the order they were written.
1330 Usually current_binding_level->names is in reverse order.
1331 But parameter decls were previously put in forward order. */
1332
1333 if (reverse)
1334 current_binding_level->names
1335 = decls = nreverse (current_binding_level->names);
1336 else
1337 decls = current_binding_level->names;
1338
1339 /* Output any nested inline functions within this block
1340 if they weren't already output. */
1341
1342 for (decl = decls; decl; decl = TREE_CHAIN (decl))
1343 if (TREE_CODE (decl) == FUNCTION_DECL
1344 && ! TREE_ASM_WRITTEN (decl)
1345 && DECL_INITIAL (decl) != NULL_TREE
6060a796
MS
1346 && TREE_ADDRESSABLE (decl)
1347 && decl_function_context (decl) == current_function_decl)
8d08fdba
MS
1348 {
1349 /* If this decl was copied from a file-scope decl
1350 on account of a block-scope extern decl,
1351 propagate TREE_ADDRESSABLE to the file-scope decl. */
1352 if (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE)
1353 TREE_ADDRESSABLE (DECL_ABSTRACT_ORIGIN (decl)) = 1;
1354 else
1355 {
1356 push_function_context ();
1357 output_inline_function (decl);
1358 pop_function_context ();
1359 }
1360 }
1361
1362 /* If there were any declarations or structure tags in that level,
1363 or if this level is a function body,
1364 create a BLOCK to record them for the life of this function. */
1365
1366 block = NULL_TREE;
1367 block_previously_created = (current_binding_level->this_block != NULL_TREE);
1368 if (block_previously_created)
1369 block = current_binding_level->this_block;
1370 else if (keep == 1 || functionbody)
1371 block = make_node (BLOCK);
1372 if (block != NULL_TREE)
1373 {
72b7eeff
MS
1374 if (block_previously_created)
1375 {
1376 if (decls || tags || subblocks)
1377 {
be99da77 1378 if (BLOCK_VARS (block) || BLOCK_TYPE_TAGS (block))
f181d4ae
MM
1379 warning ("internal compiler error: debugging info corrupted");
1380
72b7eeff
MS
1381 BLOCK_VARS (block) = decls;
1382 BLOCK_TYPE_TAGS (block) = tags;
be99da77
MS
1383
1384 /* We can have previous subblocks and new subblocks when
1385 doing fixup_gotos with complex cleanups. We chain the new
1386 subblocks onto the end of any pre-existing subblocks. */
1387 BLOCK_SUBBLOCKS (block) = chainon (BLOCK_SUBBLOCKS (block),
1388 subblocks);
72b7eeff 1389 }
be99da77
MS
1390 /* If we created the block earlier on, and we are just
1391 diddling it now, then it already should have a proper
1392 BLOCK_END_NOTE value associated with it. */
72b7eeff
MS
1393 }
1394 else
1395 {
1396 BLOCK_VARS (block) = decls;
1397 BLOCK_TYPE_TAGS (block) = tags;
1398 BLOCK_SUBBLOCKS (block) = subblocks;
f181d4ae
MM
1399 /* Otherwise, for a new block, install a new BLOCK_END_NOTE
1400 value. */
72b7eeff
MS
1401 remember_end_note (block);
1402 }
8d08fdba
MS
1403 }
1404
1405 /* In each subblock, record that this is its superior. */
1406
1407 if (keep >= 0)
1408 for (link = subblocks; link; link = TREE_CHAIN (link))
1409 BLOCK_SUPERCONTEXT (link) = block;
1410
f181d4ae
MM
1411 /* We still support the old for-scope rules, whereby the variables
1412 in a for-init statement were in scope after the for-statement
1413 ended. We only use the new rules in flag_new_for_scope is
1414 nonzero. */
1415 leaving_for_scope
1416 = current_binding_level->is_for_scope && flag_new_for_scope == 1;
1417
1418 /* Remove declarations for all the DECLs in this level. */
1419 for (link = decls; link; link = TREE_CHAIN (link))
1420 {
1421 if (leaving_for_scope && TREE_CODE (link) == VAR_DECL)
1422 {
1423 tree outer_binding
1424 = TREE_CHAIN (IDENTIFIER_BINDING (DECL_NAME (link)));
1425 tree ns_binding;
1426
1427 if (!outer_binding)
1428 ns_binding = IDENTIFIER_NAMESPACE_VALUE (DECL_NAME (link));
c7dda1e3
MM
1429 else
1430 ns_binding = NULL_TREE;
1431
f181d4ae
MM
1432 if (outer_binding
1433 && (BINDING_LEVEL (outer_binding)
1434 == current_binding_level->level_chain))
1435 /* We have something like:
1436
1437 int i;
1438 for (int i; ;);
1439
1440 and we are leaving the `for' scope. There's no reason to
1441 keep the binding of the inner `i' in this case. */
d8f8dca1 1442 pop_binding (DECL_NAME (link), link);
f181d4ae
MM
1443 else if ((outer_binding
1444 && (TREE_CODE (BINDING_VALUE (outer_binding))
1445 == TYPE_DECL))
1446 || (ns_binding
1447 && TREE_CODE (ns_binding) == TYPE_DECL))
1448 /* Here, we have something like:
1449
1450 typedef int I;
1451
1452 void f () {
1453 for (int I; ;);
1454 }
1455
1456 We must pop the for-scope binding so we know what's a
1457 type and what isn't. */
d8f8dca1 1458 pop_binding (DECL_NAME (link), link);
e76a2646 1459 else
e76a2646 1460 {
f181d4ae
MM
1461 /* Mark this VAR_DECL as dead so that we can tell we left it
1462 there only for backward compatibility. */
1463 DECL_DEAD_FOR_LOCAL (link) = 1;
1464
1465 /* Keep track of what should of have happenned when we
1466 popped the binding. */
1467 if (outer_binding && BINDING_VALUE (outer_binding))
1468 DECL_SHADOWED_FOR_VAR (link)
1469 = BINDING_VALUE (outer_binding);
1470
1471 /* Add it to the list of dead variables in the next
1472 outermost binding to that we can remove these when we
1473 leave that binding. */
1474 current_binding_level->level_chain->dead_vars_from_for
1475 = tree_cons (NULL_TREE, link,
1476 current_binding_level->level_chain->
1477 dead_vars_from_for);
1478
1479 /* Although we don't pop the CPLUS_BINDING, we do clear
1480 its BINDING_LEVEL since the level is going away now. */
1481 BINDING_LEVEL (IDENTIFIER_BINDING (DECL_NAME (link)))
1482 = 0;
e76a2646 1483 }
2ee887f2 1484 }
f181d4ae 1485 else
8d6e462b 1486 {
f181d4ae 1487 /* Remove the binding. */
0034cf72
JM
1488 decl = link;
1489 if (TREE_CODE (decl) == TREE_LIST)
1490 decl = TREE_VALUE (decl);
1491 if (TREE_CODE_CLASS (TREE_CODE (decl)) == 'd')
1492 pop_binding (DECL_NAME (decl), decl);
1493 else if (TREE_CODE (decl) == OVERLOAD)
1494 pop_binding (DECL_NAME (OVL_FUNCTION (decl)), decl);
d8f8dca1 1495 else
f181d4ae 1496 my_friendly_abort (0);
8d08fdba 1497 }
f181d4ae 1498 }
8d08fdba 1499
f181d4ae
MM
1500 /* Remove declarations for any `for' variables from inner scopes
1501 that we kept around. */
1502 for (link = current_binding_level->dead_vars_from_for;
1503 link; link = TREE_CHAIN (link))
d8f8dca1 1504 pop_binding (DECL_NAME (TREE_VALUE (link)), TREE_VALUE (link));
2ee887f2 1505
f181d4ae
MM
1506 /* Restore the IDENTIFIER_TYPE_VALUEs. */
1507 for (link = current_binding_level->type_shadowed;
1508 link; link = TREE_CHAIN (link))
1509 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (link), TREE_VALUE (link));
1510
1511 /* There may be OVERLOADs (wrapped in TREE_LISTs) on the BLOCK_VARs
1512 list if a `using' declaration put them there. The debugging
1513 back-ends won't understand OVERLOAD, so we remove them here.
1514 Because the BLOCK_VARS are (temporarily) shared with
1515 CURRENT_BINDING_LEVEL->NAMES we must do this fixup after we have
1516 popped all the bindings. */
1517 if (block)
1518 {
1519 tree* d;
8d6e462b 1520
a06d48ef
JM
1521 for (d = &BLOCK_VARS (block); *d; )
1522 {
1523 if (TREE_CODE (*d) == TREE_LIST)
1524 *d = TREE_CHAIN (*d);
1525 else
1526 d = &TREE_CHAIN (*d);
1527 }
8d6e462b 1528 }
8d08fdba
MS
1529
1530 /* If the level being exited is the top level of a function,
1531 check over all the labels. */
1532
1533 if (functionbody)
1534 {
1535 /* If this is the top level block of a function,
1536 the vars are the function's parameters.
1537 Don't leave them in the BLOCK because they are
1538 found in the FUNCTION_DECL instead. */
1539
1540 BLOCK_VARS (block) = 0;
1541
1542 /* Clear out the definitions of all label names,
1543 since their scopes end here. */
1544
1545 for (link = named_labels; link; link = TREE_CHAIN (link))
1546 {
1547 register tree label = TREE_VALUE (link);
1548
1549 if (DECL_INITIAL (label) == NULL_TREE)
1550 {
8251199e 1551 cp_error_at ("label `%D' used but not defined", label);
8d08fdba
MS
1552 /* Avoid crashing later. */
1553 define_label (input_filename, 1, DECL_NAME (label));
1554 }
1555 else if (warn_unused && !TREE_USED (label))
8251199e 1556 cp_warning_at ("label `%D' defined but not used", label);
8d08fdba
MS
1557 SET_IDENTIFIER_LABEL_VALUE (DECL_NAME (label), NULL_TREE);
1558
1559 /* Put the labels into the "variables" of the
1560 top-level block, so debugger can see them. */
1561 TREE_CHAIN (label) = BLOCK_VARS (block);
1562 BLOCK_VARS (block) = label;
1563 }
1564
1565 named_labels = NULL_TREE;
1566 }
1567
1568 /* Any uses of undefined labels now operate under constraints
1569 of next binding contour. */
1570 {
1571 struct binding_level *level_chain;
1572 level_chain = current_binding_level->level_chain;
1573 if (level_chain)
1574 {
e349ee73
MS
1575 struct named_label_list *labels;
1576 for (labels = named_label_uses; labels; labels = labels->next)
1577 if (labels->binding_level == current_binding_level)
8d08fdba 1578 {
e349ee73
MS
1579 labels->binding_level = level_chain;
1580 labels->names_in_scope = level_chain->names;
8d08fdba
MS
1581 }
1582 }
1583 }
1584
1585 tmp = current_binding_level->keep;
1586
1587 pop_binding_level ();
1588 if (functionbody)
1589 DECL_INITIAL (current_function_decl) = block;
1590 else if (block)
1591 {
1592 if (!block_previously_created)
1593 current_binding_level->blocks
1594 = chainon (current_binding_level->blocks, block);
1595 }
1596 /* If we did not make a block for the level just exited,
1597 any blocks made for inner levels
1598 (since they cannot be recorded as subblocks in that level)
1599 must be carried forward so they will later become subblocks
1600 of something else. */
1601 else if (subblocks)
536333d4
MM
1602 current_binding_level->blocks
1603 = chainon (current_binding_level->blocks, subblocks);
8d08fdba
MS
1604
1605 /* Take care of compiler's internal binding structures. */
a4443a08 1606 if (tmp == 2)
8d08fdba 1607 {
8d08fdba
MS
1608 expand_end_bindings (getdecls (), keep, 1);
1609 /* Each and every BLOCK node created here in `poplevel' is important
1610 (e.g. for proper debugging information) so if we created one
1611 earlier, mark it as "used". */
1612 if (block)
1613 TREE_USED (block) = 1;
1614 block = poplevel (keep, reverse, real_functionbody);
1615 }
1616
1617 /* Each and every BLOCK node created here in `poplevel' is important
1618 (e.g. for proper debugging information) so if we created one
1619 earlier, mark it as "used". */
1620 if (block)
1621 TREE_USED (block) = 1;
1622 return block;
1623}
1624
1625/* Delete the node BLOCK from the current binding level.
1626 This is used for the block inside a stmt expr ({...})
1627 so that the block can be reinserted where appropriate. */
1628
1629void
1630delete_block (block)
1631 tree block;
1632{
1633 tree t;
1634 if (current_binding_level->blocks == block)
1635 current_binding_level->blocks = TREE_CHAIN (block);
1636 for (t = current_binding_level->blocks; t;)
1637 {
1638 if (TREE_CHAIN (t) == block)
1639 TREE_CHAIN (t) = TREE_CHAIN (block);
1640 else
1641 t = TREE_CHAIN (t);
1642 }
1643 TREE_CHAIN (block) = NULL_TREE;
1644 /* Clear TREE_USED which is always set by poplevel.
1645 The flag is set again if insert_block is called. */
1646 TREE_USED (block) = 0;
1647}
1648
1649/* Insert BLOCK at the end of the list of subblocks of the
1650 current binding level. This is used when a BIND_EXPR is expanded,
1651 to handle the BLOCK node inside the BIND_EXPR. */
1652
1653void
1654insert_block (block)
1655 tree block;
1656{
1657 TREE_USED (block) = 1;
1658 current_binding_level->blocks
1659 = chainon (current_binding_level->blocks, block);
1660}
1661
8d08fdba
MS
1662/* Set the BLOCK node for the innermost scope
1663 (the one we are currently in). */
1664
1665void
1666set_block (block)
1667 register tree block;
1668{
1669 current_binding_level->this_block = block;
1670}
1671
1672/* Do a pushlevel for class declarations. */
e92cc029 1673
8d08fdba
MS
1674void
1675pushlevel_class ()
1676{
1677 register struct binding_level *newlevel;
1678
1679 /* Reuse or create a struct for this binding level. */
1680#if defined(DEBUG_CP_BINDING_LEVELS)
1681 if (0)
1682#else /* !defined(DEBUG_CP_BINDING_LEVELS) */
1683 if (free_binding_level)
1684#endif /* !defined(DEBUG_CP_BINDING_LEVELS) */
1685 {
1686 newlevel = free_binding_level;
1687 free_binding_level = free_binding_level->level_chain;
1688 }
1689 else
f181d4ae 1690 newlevel = make_binding_level ();
8d08fdba
MS
1691
1692#if defined(DEBUG_CP_BINDING_LEVELS)
1693 is_class_level = 1;
1694#endif /* defined(DEBUG_CP_BINDING_LEVELS) */
1695
1696 push_binding_level (newlevel, 0, 0);
1697
1698 decl_stack = push_decl_level (decl_stack, &decl_obstack);
1699 class_binding_level = current_binding_level;
1700 class_binding_level->parm_flag = 2;
8d08fdba
MS
1701}
1702
b74a0560 1703/* ...and a poplevel for class declarations. */
e92cc029 1704
b74a0560
MM
1705static tree
1706poplevel_class ()
8d08fdba
MS
1707{
1708 register struct binding_level *level = class_binding_level;
8d08fdba
MS
1709 tree shadowed;
1710
1711 my_friendly_assert (level != 0, 354);
1712
1713 decl_stack = pop_stack_level (decl_stack);
8d08fdba 1714 /* If we're leaving a toplevel class, don't bother to do the setting
ddd5a7c1 1715 of IDENTIFIER_CLASS_VALUE to NULL_TREE, since first of all this slot
8d08fdba 1716 shouldn't even be used when current_class_type isn't set, and second,
ddd5a7c1 1717 if we don't touch it here, we're able to use the cache effect if the
8d08fdba 1718 next time we're entering a class scope, it is the same class. */
b74a0560 1719 if (current_class_depth != 1)
8f032717
MM
1720 {
1721 struct binding_level* b;
1722
1723 /* Clear out our IDENTIFIER_CLASS_VALUEs. */
1724 for (shadowed = level->class_shadowed;
1725 shadowed;
1726 shadowed = TREE_CHAIN (shadowed))
1727 IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (shadowed)) = NULL_TREE;
1728
1729 /* Find the next enclosing class, and recreate
1730 IDENTIFIER_CLASS_VALUEs appropriate for that class. */
1731 b = level->level_chain;
1732 while (b && b->parm_flag != 2)
1733 b = b->level_chain;
1734
1735 if (b)
1736 for (shadowed = b->class_shadowed;
1737 shadowed;
1738 shadowed = TREE_CHAIN (shadowed))
1739 {
1740 tree t;
1741
1742 t = IDENTIFIER_BINDING (TREE_PURPOSE (shadowed));
1743 while (t && BINDING_LEVEL (t) != b)
1744 t = TREE_CHAIN (t);
1745
1746 if (t)
1747 IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (shadowed))
1748 = BINDING_VALUE (t);
1749 }
1750 }
8d08fdba
MS
1751 else
1752 /* Remember to save what IDENTIFIER's were bound in this scope so we
1753 can recover from cache misses. */
e76a2646
MS
1754 {
1755 previous_class_type = current_class_type;
1756 previous_class_values = class_binding_level->class_shadowed;
1757 }
8d08fdba
MS
1758 for (shadowed = level->type_shadowed;
1759 shadowed;
1760 shadowed = TREE_CHAIN (shadowed))
2c73f9f5 1761 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (shadowed), TREE_VALUE (shadowed));
8d08fdba 1762
f181d4ae
MM
1763 /* Remove the bindings for all of the class-level declarations. */
1764 for (shadowed = level->class_shadowed;
1765 shadowed;
1766 shadowed = TREE_CHAIN (shadowed))
d8f8dca1 1767 pop_binding (TREE_PURPOSE (shadowed), TREE_TYPE (shadowed));
f181d4ae 1768
8d08fdba
MS
1769 GNU_xref_end_scope ((HOST_WIDE_INT) class_binding_level,
1770 (HOST_WIDE_INT) class_binding_level->level_chain,
1771 class_binding_level->parm_flag,
5566b478 1772 class_binding_level->keep);
8d08fdba 1773
38e01259 1774 /* Now, pop out of the binding level which we created up in the
8d08fdba
MS
1775 `pushlevel_class' routine. */
1776#if defined(DEBUG_CP_BINDING_LEVELS)
1777 is_class_level = 1;
1778#endif /* defined(DEBUG_CP_BINDING_LEVELS) */
1779
1780 pop_binding_level ();
1781
b74a0560 1782 return NULL_TREE;
8d08fdba 1783}
8f032717
MM
1784
1785/* We are entering the scope of a class. Clear IDENTIFIER_CLASS_VALUE
1786 for any names in enclosing classes. */
1787
1788void
1789clear_identifier_class_values ()
1790{
1791 tree t;
1792
1793 if (!class_binding_level)
1794 return;
1795
1796 for (t = class_binding_level->class_shadowed;
1797 t;
1798 t = TREE_CHAIN (t))
1799 IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (t)) = NULL_TREE;
1800}
1801
0aafb128
MM
1802/* Returns non-zero if T is a virtual function table. */
1803
1804int
1805vtable_decl_p (t, data)
1806 tree t;
1807 void *data ATTRIBUTE_UNUSED;
1808{
1809 return (TREE_CODE (t) == VAR_DECL && DECL_VIRTUAL_P (t));
1810}
1811
1812/* Returns non-zero if T is a TYPE_DECL for a type with virtual
1813 functions. */
1814
1815int
1816vtype_decl_p (t, data)
1817 tree t;
1818 void *data ATTRIBUTE_UNUSED;
1819{
1820 return (TREE_CODE (t) == TYPE_DECL
1821 && TREE_TYPE (t) != error_mark_node
1822 && TYPE_LANG_SPECIFIC (TREE_TYPE (t))
1823 && CLASSTYPE_VSIZE (TREE_TYPE (t)));
1824}
1825
f71f87f9
MM
1826/* Return the declarations that are members of the namespace NS. */
1827
1828tree
1829cp_namespace_decls (ns)
1830 tree ns;
1831{
1832 return NAMESPACE_LEVEL (ns)->names;
1833}
1834
0aafb128
MM
1835/* Walk all the namespaces contained NAMESPACE, including NAMESPACE
1836 itself, calling F for each. The DATA is passed to F as well. */
1837
1838static int
1839walk_namespaces_r (namespace, f, data)
1840 tree namespace;
1841 walk_namespaces_fn f;
1842 void *data;
1843{
1844 tree current;
1845 int result = 0;
1846
1847 result |= (*f) (namespace, data);
1848
f71f87f9 1849 for (current = cp_namespace_decls (namespace);
0aafb128
MM
1850 current;
1851 current = TREE_CHAIN (current))
1852 {
1853 if (TREE_CODE (current) != NAMESPACE_DECL
1854 || DECL_NAMESPACE_ALIAS (current))
1855 continue;
1856 if (!DECL_LANG_SPECIFIC (current))
1857 {
1858 /* Hmm. std. */
1859 my_friendly_assert (current == std_node, 393);
1860 continue;
1861 }
1862
1863 /* We found a namespace. */
1864 result |= walk_namespaces_r (current, f, data);
1865 }
1866
1867 return result;
1868}
1869
1870/* Walk all the namespaces, calling F for each. The DATA is passed to
1871 F as well. */
1872
1873int
1874walk_namespaces (f, data)
1875 walk_namespaces_fn f;
1876 void *data;
1877{
1878 return walk_namespaces_r (global_namespace, f, data);
1879}
1880
1881struct walk_globals_data {
1882 walk_globals_pred p;
1883 walk_globals_fn f;
1884 void *data;
1885};
1886
1887/* Walk the global declarations in NAMESPACE. Whenever one is found
1888 for which P returns non-zero, call F with its address. If any call
1889 to F returns a non-zero value, return a non-zero value. */
1890
1891static int
1892walk_globals_r (namespace, data)
1893 tree namespace;
1894 void *data;
1895{
1896 struct walk_globals_data* wgd = (struct walk_globals_data *) data;
1897 walk_globals_pred p = wgd->p;
1898 walk_globals_fn f = wgd->f;
1899 void *d = wgd->data;
1900 tree *t;
1901 int result = 0;
1902
1903 t = &NAMESPACE_LEVEL (namespace)->names;
1904
1905 while (*t)
1906 {
1907 tree glbl = *t;
1908
1909 if ((*p) (glbl, d))
1910 result |= (*f) (t, d);
1911
1912 /* If F changed *T, then *T still points at the next item to
1913 examine. */
1914 if (*t == glbl)
1915 t = &TREE_CHAIN (*t);
1916 }
1917
1918 return result;
1919}
1920
1921/* Walk the global declarations. Whenever one is found for which P
1922 returns non-zero, call F with its address. If any call to F
1923 returns a non-zero value, return a non-zero value. */
1924
1925int
1926walk_globals (p, f, data)
1927 walk_globals_pred p;
1928 walk_globals_fn f;
1929 void *data;
1930{
1931 struct walk_globals_data wgd;
1932 wgd.p = p;
1933 wgd.f = f;
1934 wgd.data = data;
1935
1936 return walk_namespaces (walk_globals_r, &wgd);
1937}
1938
1939/* Call wrapup_globals_declarations for the globals in NAMESPACE. If
1940 DATA is non-NULL, this is the last time we will call
1941 wrapup_global_declarations for this NAMESPACE. */
1942
1943int
1944wrapup_globals_for_namespace (namespace, data)
1945 tree namespace;
1946 void *data;
1947{
f71f87f9 1948 tree globals = cp_namespace_decls (namespace);
0aafb128
MM
1949 int len = list_length (globals);
1950 tree *vec = (tree *) alloca (sizeof (tree) * len);
1951 int i;
0352cfc8 1952 int result;
0aafb128
MM
1953 tree decl;
1954 int last_time = (data != 0);
1955
1956 if (last_time && namespace == global_namespace)
1957 /* Let compile_file handle the global namespace. */
1958 return 0;
1959
1960 /* Process the decls in reverse order--earliest first.
1961 Put them into VEC from back to front, then take out from front. */
1962
1963 for (i = 0, decl = globals; i < len; i++, decl = TREE_CHAIN (decl))
1964 vec[len - i - 1] = decl;
1965
0352cfc8
MM
1966 if (last_time)
1967 {
1968 check_global_declarations (vec, len);
1969 return 0;
1970 }
1971
1972 /* Temporarily mark vtables as external. That prevents
1973 wrapup_global_declarations from writing them out; we must process
1974 them ourselves in finish_vtable_vardecl. */
1975 for (i = 0; i < len; ++i)
91f2aa22 1976 if (vtable_decl_p (vec[i], /*data=*/0) && !DECL_EXTERNAL (vec[i]))
0352cfc8
MM
1977 {
1978 DECL_NOT_REALLY_EXTERN (vec[i]) = 1;
1979 DECL_EXTERNAL (vec[i]) = 1;
1980 }
0aafb128 1981
0352cfc8
MM
1982 /* Write out any globals that need to be output. */
1983 result = wrapup_global_declarations (vec, len);
1984
1985 /* Undo the hack to DECL_EXTERNAL above. */
1986 for (i = 0; i < len; ++i)
1987 if (vtable_decl_p (vec[i], /*data=*/0)
1988 && DECL_NOT_REALLY_EXTERN (vec[i]))
1989 {
1990 DECL_NOT_REALLY_EXTERN (vec[i]) = 0;
1991 DECL_EXTERNAL (vec[i]) = 0;
1992 }
1993
1994 return result;
0aafb128
MM
1995}
1996
8d08fdba
MS
1997\f
1998/* For debugging. */
5566b478
MS
1999static int no_print_functions = 0;
2000static int no_print_builtins = 0;
8d08fdba
MS
2001
2002void
2003print_binding_level (lvl)
2004 struct binding_level *lvl;
2005{
2006 tree t;
2007 int i = 0, len;
2008 fprintf (stderr, " blocks=");
2009 fprintf (stderr, HOST_PTR_PRINTF, lvl->blocks);
2010 fprintf (stderr, " n_incomplete=%d parm_flag=%d keep=%d",
f30432d7 2011 list_length (lvl->incomplete), lvl->parm_flag, lvl->keep);
8d08fdba
MS
2012 if (lvl->tag_transparent)
2013 fprintf (stderr, " tag-transparent");
2014 if (lvl->more_cleanups_ok)
2015 fprintf (stderr, " more-cleanups-ok");
2016 if (lvl->have_cleanups)
2017 fprintf (stderr, " have-cleanups");
8d08fdba
MS
2018 fprintf (stderr, "\n");
2019 if (lvl->names)
2020 {
2021 fprintf (stderr, " names:\t");
2022 /* We can probably fit 3 names to a line? */
2023 for (t = lvl->names; t; t = TREE_CHAIN (t))
2024 {
fc378698 2025 if (no_print_functions && (TREE_CODE (t) == FUNCTION_DECL))
8d08fdba
MS
2026 continue;
2027 if (no_print_builtins
fc378698
MS
2028 && (TREE_CODE (t) == TYPE_DECL)
2029 && (!strcmp (DECL_SOURCE_FILE (t),"<built-in>")))
8d08fdba
MS
2030 continue;
2031
2032 /* Function decls tend to have longer names. */
2033 if (TREE_CODE (t) == FUNCTION_DECL)
2034 len = 3;
2035 else
2036 len = 2;
2037 i += len;
2038 if (i > 6)
2039 {
2040 fprintf (stderr, "\n\t");
2041 i = len;
2042 }
2043 print_node_brief (stderr, "", t, 0);
bd6dd845 2044 if (t == error_mark_node)
8d08fdba
MS
2045 break;
2046 }
2047 if (i)
2048 fprintf (stderr, "\n");
2049 }
2050 if (lvl->tags)
2051 {
2052 fprintf (stderr, " tags:\t");
2053 i = 0;
2054 for (t = lvl->tags; t; t = TREE_CHAIN (t))
2055 {
2056 if (TREE_PURPOSE (t) == NULL_TREE)
2057 len = 3;
2058 else if (TREE_PURPOSE (t) == TYPE_IDENTIFIER (TREE_VALUE (t)))
2059 len = 2;
2060 else
2061 len = 4;
2062 i += len;
2063 if (i > 5)
2064 {
2065 fprintf (stderr, "\n\t");
2066 i = len;
2067 }
2068 if (TREE_PURPOSE (t) == NULL_TREE)
2069 {
2070 print_node_brief (stderr, "<unnamed-typedef", TREE_VALUE (t), 0);
2071 fprintf (stderr, ">");
2072 }
2073 else if (TREE_PURPOSE (t) == TYPE_IDENTIFIER (TREE_VALUE (t)))
2074 print_node_brief (stderr, "", TREE_VALUE (t), 0);
2075 else
2076 {
2077 print_node_brief (stderr, "<typedef", TREE_PURPOSE (t), 0);
2078 print_node_brief (stderr, "", TREE_VALUE (t), 0);
2079 fprintf (stderr, ">");
2080 }
2081 }
2082 if (i)
2083 fprintf (stderr, "\n");
2084 }
8d08fdba
MS
2085 if (lvl->class_shadowed)
2086 {
2087 fprintf (stderr, " class-shadowed:");
2088 for (t = lvl->class_shadowed; t; t = TREE_CHAIN (t))
2089 {
2090 fprintf (stderr, " %s ", IDENTIFIER_POINTER (TREE_PURPOSE (t)));
2091 }
2092 fprintf (stderr, "\n");
2093 }
2094 if (lvl->type_shadowed)
2095 {
2096 fprintf (stderr, " type-shadowed:");
2097 for (t = lvl->type_shadowed; t; t = TREE_CHAIN (t))
2098 {
8d08fdba 2099 fprintf (stderr, " %s ", IDENTIFIER_POINTER (TREE_PURPOSE (t)));
8d08fdba
MS
2100 }
2101 fprintf (stderr, "\n");
2102 }
2103}
2104
2105void
2106print_other_binding_stack (stack)
2107 struct binding_level *stack;
2108{
2109 struct binding_level *level;
2110 for (level = stack; level != global_binding_level; level = level->level_chain)
2111 {
2112 fprintf (stderr, "binding level ");
2113 fprintf (stderr, HOST_PTR_PRINTF, level);
2114 fprintf (stderr, "\n");
2115 print_binding_level (level);
2116 }
2117}
2118
2119void
2120print_binding_stack ()
2121{
2122 struct binding_level *b;
2123 fprintf (stderr, "current_binding_level=");
2124 fprintf (stderr, HOST_PTR_PRINTF, current_binding_level);
2125 fprintf (stderr, "\nclass_binding_level=");
2126 fprintf (stderr, HOST_PTR_PRINTF, class_binding_level);
2127 fprintf (stderr, "\nglobal_binding_level=");
2128 fprintf (stderr, HOST_PTR_PRINTF, global_binding_level);
2129 fprintf (stderr, "\n");
2130 if (class_binding_level)
2131 {
2132 for (b = class_binding_level; b; b = b->level_chain)
2133 if (b == current_binding_level)
2134 break;
2135 if (b)
2136 b = class_binding_level;
2137 else
2138 b = current_binding_level;
2139 }
2140 else
2141 b = current_binding_level;
2142 print_other_binding_stack (b);
2143 fprintf (stderr, "global:\n");
2144 print_binding_level (global_binding_level);
2145}
a9aedbc2 2146
2c73f9f5
ML
2147/* Namespace binding access routines: The namespace_bindings field of
2148 the identifier is polymorphic, with three possible values:
2149 NULL_TREE, a list of CPLUS_BINDINGS, or any other tree_node
2150 indicating the BINDING_VALUE of global_namespace. */
30394414 2151
2c73f9f5
ML
2152/* Check whether the a binding for the name to scope is known.
2153 Assumes that the bindings of the name are already a list
2154 of bindings. Returns the binding found, or NULL_TREE. */
2155
2156static tree
2157find_binding (name, scope)
30394414
JM
2158 tree name;
2159 tree scope;
2160{
2c73f9f5 2161 tree iter, prev = NULL_TREE;
3e3f722c
ML
2162
2163 scope = ORIGINAL_NAMESPACE (scope);
2164
30394414
JM
2165 for (iter = IDENTIFIER_NAMESPACE_BINDINGS (name); iter;
2166 iter = TREE_CHAIN (iter))
2167 {
2168 my_friendly_assert (TREE_CODE (iter) == CPLUS_BINDING, 374);
2169 if (BINDING_SCOPE (iter) == scope)
2c73f9f5 2170 {
cfe507be 2171 /* Move binding found to the front of the list, so
2c73f9f5
ML
2172 subsequent lookups will find it faster. */
2173 if (prev)
2174 {
2175 TREE_CHAIN (prev) = TREE_CHAIN (iter);
2176 TREE_CHAIN (iter) = IDENTIFIER_NAMESPACE_BINDINGS (name);
2177 IDENTIFIER_NAMESPACE_BINDINGS (name) = iter;
2178 }
2179 return iter;
2180 }
2181 prev = iter;
30394414 2182 }
2c73f9f5
ML
2183 return NULL_TREE;
2184}
2185
2186/* Always returns a binding for name in scope. If the
2187 namespace_bindings is not a list, convert it to one first.
2188 If no binding is found, make a new one. */
2189
2190tree
2191binding_for_name (name, scope)
2192 tree name;
2193 tree scope;
2194{
2195 tree b = IDENTIFIER_NAMESPACE_BINDINGS (name);
2196 tree result;
3e3f722c
ML
2197
2198 scope = ORIGINAL_NAMESPACE (scope);
2199
2c73f9f5
ML
2200 if (b && TREE_CODE (b) != CPLUS_BINDING)
2201 {
2202 /* Get rid of optimization for global scope. */
2203 IDENTIFIER_NAMESPACE_BINDINGS (name) = NULL_TREE;
2204 BINDING_VALUE (binding_for_name (name, global_namespace)) = b;
2205 b = IDENTIFIER_NAMESPACE_BINDINGS (name);
2206 }
2207 if (b && (result = find_binding (name, scope)))
2208 return result;
2209 /* Not found, make a new permanent one. */
30394414 2210 push_obstacks (&permanent_obstack, &permanent_obstack);
2c73f9f5
ML
2211 result = make_node (CPLUS_BINDING);
2212 TREE_CHAIN (result) = b;
2213 IDENTIFIER_NAMESPACE_BINDINGS (name) = result;
2214 BINDING_SCOPE (result) = scope;
2215 BINDING_TYPE (result) = NULL_TREE;
2216 BINDING_VALUE (result) = NULL_TREE;
30394414 2217 pop_obstacks ();
2c73f9f5
ML
2218 return result;
2219}
2220
2221/* Return the binding value for name in scope, considering that
2222 namespace_binding may or may not be a list of CPLUS_BINDINGS. */
2223
2224tree
2225namespace_binding (name, scope)
2226 tree name;
2227 tree scope;
2228{
2229 tree b = IDENTIFIER_NAMESPACE_BINDINGS (name);
2230 if (b == NULL_TREE)
2231 return NULL_TREE;
cb0dbb9a
JM
2232 if (scope == NULL_TREE)
2233 scope = global_namespace;
2c73f9f5
ML
2234 if (TREE_CODE (b) != CPLUS_BINDING)
2235 return (scope == global_namespace) ? b : NULL_TREE;
2236 name = find_binding (name,scope);
2237 if (name == NULL_TREE)
2238 return name;
2239 return BINDING_VALUE (name);
2240}
2241
2242/* Set the binding value for name in scope. If modifying the binding
2243 of global_namespace is attempted, try to optimize it. */
2244
2245void
2246set_namespace_binding (name, scope, val)
2247 tree name;
2248 tree scope;
2249 tree val;
2250{
2251 tree b;
cb0dbb9a
JM
2252
2253 if (scope == NULL_TREE)
2254 scope = global_namespace;
2255
2c73f9f5
ML
2256 if (scope == global_namespace)
2257 {
2258 b = IDENTIFIER_NAMESPACE_BINDINGS (name);
2259 if (b == NULL_TREE || TREE_CODE (b) != CPLUS_BINDING)
2260 {
2261 IDENTIFIER_NAMESPACE_BINDINGS (name) = val;
2262 return;
2263 }
2264 }
2265 b = binding_for_name (name, scope);
2266 BINDING_VALUE (b) = val;
30394414
JM
2267}
2268
dff6b454
RK
2269/* Push into the scope of the NAME namespace. If NAME is NULL_TREE, then we
2270 select a name that is unique to this compilation unit. */
e92cc029 2271
a9aedbc2
MS
2272void
2273push_namespace (name)
2274 tree name;
2275{
b370501f 2276 tree d = NULL_TREE;
30394414
JM
2277 int need_new = 1;
2278 int implicit_use = 0;
2c73f9f5 2279 int global = 0;
30394414
JM
2280 if (!global_namespace)
2281 {
2c73f9f5 2282 /* This must be ::. */
30394414 2283 my_friendly_assert (name == get_identifier ("::"), 377);
2c73f9f5 2284 global = 1;
30394414
JM
2285 }
2286 else if (!name)
2287 {
3ab52652
ML
2288 /* The name of anonymous namespace is unique for the translation
2289 unit. */
0c8feefe
MM
2290 if (!anonymous_namespace_name)
2291 anonymous_namespace_name = get_file_function_name ('N');
2292 name = anonymous_namespace_name;
3ab52652
ML
2293 d = IDENTIFIER_NAMESPACE_VALUE (name);
2294 if (d)
2295 /* Reopening anonymous namespace. */
2296 need_new = 0;
30394414 2297 implicit_use = 1;
2c73f9f5
ML
2298 }
2299 else if (current_namespace == global_namespace
2300 && name == DECL_NAME (std_node))
2301 {
2302 in_std++;
2303 return;
2304 }
30394414
JM
2305 else
2306 {
2c73f9f5 2307 /* Check whether this is an extended namespace definition. */
30394414
JM
2308 d = IDENTIFIER_NAMESPACE_VALUE (name);
2309 if (d != NULL_TREE && TREE_CODE (d) == NAMESPACE_DECL)
3e3f722c
ML
2310 {
2311 need_new = 0;
2312 if (DECL_NAMESPACE_ALIAS (d))
2313 {
8251199e 2314 cp_error ("namespace alias `%D' not allowed here, assuming `%D'",
3e3f722c
ML
2315 d, DECL_NAMESPACE_ALIAS (d));
2316 d = DECL_NAMESPACE_ALIAS (d);
2317 }
2318 }
30394414 2319 }
6633d636 2320
30394414
JM
2321 if (need_new)
2322 {
2c73f9f5 2323 /* Make a new namespace, binding the name to it. */
6b57ac29 2324 d = build_lang_decl (NAMESPACE_DECL, name, void_type_node);
2c73f9f5
ML
2325 /* The global namespace is not pushed, and the global binding
2326 level is set elsewhere. */
2327 if (!global)
2328 {
2329 d = pushdecl (d);
2330 pushlevel (0);
2331 declare_namespace_level ();
2332 NAMESPACE_LEVEL (d) = current_binding_level;
2333 }
30394414 2334 }
2c73f9f5
ML
2335 else
2336 resume_binding_level (NAMESPACE_LEVEL (d));
2337
30394414
JM
2338 if (implicit_use)
2339 do_using_directive (d);
2c73f9f5 2340 /* Enter the name space. */
30394414 2341 current_namespace = d;
a9aedbc2
MS
2342}
2343
2344/* Pop from the scope of the current namespace. */
e92cc029 2345
a9aedbc2
MS
2346void
2347pop_namespace ()
2348{
2c73f9f5
ML
2349 if (current_namespace == global_namespace)
2350 {
2351 my_friendly_assert (in_std>0, 980421);
2352 in_std--;
2353 return;
2354 }
cb0dbb9a 2355 current_namespace = CP_DECL_CONTEXT (current_namespace);
2c73f9f5
ML
2356 /* The binding level is not popped, as it might be re-opened later. */
2357 suspend_binding_level ();
2358}
a9aedbc2 2359
61fc8c9e
MM
2360/* Push into the scope of the namespace NS, even if it is deeply
2361 nested within another namespace. */
2362
2363void
2364push_nested_namespace (ns)
2365 tree ns;
2366{
2367 if (ns == global_namespace)
2368 push_to_top_level ();
2369 else
2370 {
2371 push_nested_namespace (CP_DECL_CONTEXT (ns));
2372 push_namespace (DECL_NAME (ns));
2373 }
2374}
2375
2376/* Pop back from the scope of the namespace NS, which was previously
2377 entered with push_nested_namespace. */
2378
2379void
2380pop_nested_namespace (ns)
2381 tree ns;
2382{
2383 while (ns != global_namespace)
2384 {
2385 pop_namespace ();
2386 ns = CP_DECL_CONTEXT (ns);
2387 }
2388
2389 pop_from_top_level ();
2390}
2391
8d08fdba
MS
2392\f
2393/* Subroutines for reverting temporarily to top-level for instantiation
2394 of templates and such. We actually need to clear out the class- and
2395 local-value slots of all identifiers, so that only the global values
2396 are at all visible. Simply setting current_binding_level to the global
2397 scope isn't enough, because more binding levels may be pushed. */
2398struct saved_scope {
2399 struct binding_level *old_binding_level;
2400 tree old_bindings;
2c73f9f5 2401 tree old_namespace;
8d08fdba 2402 struct saved_scope *prev;
61a127b3
MM
2403 tree class_name, class_type;
2404 tree access_specifier;
2405 tree function_decl;
8d08fdba 2406 struct binding_level *class_bindings;
51c184be
MS
2407 tree *lang_base, *lang_stack, lang_name;
2408 int lang_stacksize;
5566b478 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 2527 s->last_function_parms = last_function_parms;
e76a2646 2528 s->template_parms = current_template_parms;
5156628f 2529 s->processing_template_decl = processing_template_decl;
a50f0918
MS
2530 s->previous_class_type = previous_class_type;
2531 s->previous_class_values = previous_class_values;
3f1892e1 2532 s->class_cache_firstobj = class_cache_firstobj;
e1467ff2
MM
2533 s->processing_specialization = processing_specialization;
2534 s->processing_explicit_instantiation = processing_explicit_instantiation;
e349ee73 2535
5f34005f 2536 current_class_name = current_class_type = NULL_TREE;
8d08fdba
MS
2537 current_function_decl = NULL_TREE;
2538 class_binding_level = (struct binding_level *)0;
51c184be
MS
2539 current_lang_stacksize = 10;
2540 current_lang_stack = current_lang_base
2541 = (tree *) xmalloc (current_lang_stacksize * sizeof (tree));
2542 current_lang_name = lang_name_cplusplus;
2543 strict_prototype = strict_prototypes_lang_cplusplus;
2544 named_labels = NULL_TREE;
120722ac 2545 shadowed_labels = NULL_TREE;
a50f0918 2546 previous_class_type = previous_class_values = NULL_TREE;
3f1892e1 2547 class_cache_firstobj = 0;
e1467ff2
MM
2548 processing_specialization = 0;
2549 processing_explicit_instantiation = 0;
eae97bd9
MM
2550 current_template_parms = NULL_TREE;
2551 processing_template_decl = 0;
2c73f9f5 2552 current_namespace = global_namespace;
8d08fdba
MS
2553
2554 s->prev = current_saved_scope;
2555 s->old_bindings = old_bindings;
2556 current_saved_scope = s;
5566b478
MS
2557
2558 push_obstacks (&permanent_obstack, &permanent_obstack);
2559}
2560
2561void
2562push_to_top_level ()
2563{
2564 maybe_push_to_top_level (0);
8d08fdba
MS
2565}
2566
2567void
2568pop_from_top_level ()
2569{
51c184be 2570 extern int current_lang_stacksize;
8d08fdba
MS
2571 struct saved_scope *s = current_saved_scope;
2572 tree t;
2573
e76a2646 2574 /* Clear out class-level bindings cache. */
8d08fdba 2575 if (previous_class_type)
8f032717 2576 invalidate_class_lookup_cache ();
8d08fdba 2577
5566b478
MS
2578 pop_obstacks ();
2579
8d08fdba
MS
2580 current_binding_level = s->old_binding_level;
2581 current_saved_scope = s->prev;
78957a2a 2582 for (t = s->old_bindings; t; )
8d08fdba 2583 {
78957a2a 2584 tree save = t;
8d08fdba
MS
2585 tree id = TREE_VEC_ELT (t, 0);
2586 if (id)
2587 {
2c73f9f5 2588 SET_IDENTIFIER_TYPE_VALUE (id, TREE_VEC_ELT (t, 1));
f181d4ae 2589 IDENTIFIER_BINDING (id) = TREE_VEC_ELT (t, 2);
8d08fdba
MS
2590 IDENTIFIER_CLASS_VALUE (id) = TREE_VEC_ELT (t, 3);
2591 }
78957a2a
JM
2592 t = TREE_CHAIN (t);
2593 TREE_CHAIN (save) = free_binding_vecs;
2594 free_binding_vecs = save;
8d08fdba 2595 }
2c73f9f5 2596 current_namespace = s->old_namespace;
8d08fdba
MS
2597 current_class_name = s->class_name;
2598 current_class_type = s->class_type;
61a127b3 2599 current_access_specifier = s->access_specifier;
8d08fdba
MS
2600 current_function_decl = s->function_decl;
2601 class_binding_level = s->class_bindings;
51c184be
MS
2602 free (current_lang_base);
2603 current_lang_base = s->lang_base;
2604 current_lang_stack = s->lang_stack;
2605 current_lang_name = s->lang_name;
2606 current_lang_stacksize = s->lang_stacksize;
2607 if (current_lang_name == lang_name_cplusplus)
2608 strict_prototype = strict_prototypes_lang_cplusplus;
2609 else if (current_lang_name == lang_name_c)
2610 strict_prototype = strict_prototypes_lang_c;
5566b478 2611 last_function_parms = s->last_function_parms;
e76a2646 2612 current_template_parms = s->template_parms;
5156628f 2613 processing_template_decl = s->processing_template_decl;
a50f0918
MS
2614 previous_class_type = s->previous_class_type;
2615 previous_class_values = s->previous_class_values;
e1467ff2
MM
2616 processing_specialization = s->processing_specialization;
2617 processing_explicit_instantiation = s->processing_explicit_instantiation;
3f1892e1 2618 class_cache_firstobj = s->class_cache_firstobj;
51c184be 2619
8d08fdba 2620 free (s);
e349ee73 2621
642b32a5 2622 pop_cp_function_context (NULL_TREE);
8d08fdba
MS
2623}
2624\f
2625/* Push a definition of struct, union or enum tag "name".
2626 into binding_level "b". "type" should be the type node,
2627 We assume that the tag "name" is not already defined.
2628
2629 Note that the definition may really be just a forward reference.
2630 In that case, the TYPE_SIZE will be a NULL_TREE.
2631
e92cc029 2632 C++ gratuitously puts all these tags in the name space. */
8d08fdba
MS
2633
2634/* When setting the IDENTIFIER_TYPE_VALUE field of an identifier ID,
2635 record the shadowed value for this binding contour. TYPE is
2636 the type that ID maps to. */
2637
2638static void
2639set_identifier_type_value_with_scope (id, type, b)
2640 tree id;
2641 tree type;
2642 struct binding_level *b;
2643{
2c73f9f5 2644 if (!b->namespace_p)
8d08fdba 2645 {
2c73f9f5
ML
2646 /* Shadow the marker, not the real thing, so that the marker
2647 gets restored later. */
2648 tree old_type_value = REAL_IDENTIFIER_TYPE_VALUE (id);
8d08fdba
MS
2649 b->type_shadowed
2650 = tree_cons (id, old_type_value, b->type_shadowed);
2651 }
2c73f9f5
ML
2652 else
2653 {
2654 tree binding = binding_for_name (id, current_namespace);
2655 BINDING_TYPE (binding) = type;
2656 /* Store marker instead of real type. */
2657 type = global_type_node;
2658 }
8d08fdba
MS
2659 SET_IDENTIFIER_TYPE_VALUE (id, type);
2660}
2661
8f032717 2662/* As set_identifier_type_value_with_scope, but using current_binding_level. */
8d08fdba
MS
2663
2664void
2665set_identifier_type_value (id, type)
2666 tree id;
2667 tree type;
2668{
8f032717 2669 set_identifier_type_value_with_scope (id, type, current_binding_level);
8d08fdba
MS
2670}
2671
2c73f9f5
ML
2672/* Return the type associated with id. */
2673
2674tree
2675identifier_type_value (id)
2676 tree id;
2677{
2678 /* There is no type with that name, anywhere. */
2679 if (REAL_IDENTIFIER_TYPE_VALUE (id) == NULL_TREE)
2680 return NULL_TREE;
2681 /* This is not the type marker, but the real thing. */
2682 if (REAL_IDENTIFIER_TYPE_VALUE (id) != global_type_node)
2683 return REAL_IDENTIFIER_TYPE_VALUE (id);
2684 /* Have to search for it. It must be on the global level, now.
2685 Ask lookup_name not to return non-types. */
3e3f722c 2686 id = lookup_name_real (id, 2, 1, 0);
2c73f9f5
ML
2687 if (id)
2688 return TREE_TYPE (id);
2689 return NULL_TREE;
2690}
2691
a9aedbc2
MS
2692/* Pop off extraneous binding levels left over due to syntax errors.
2693
2694 We don't pop past namespaces, as they might be valid. */
e92cc029 2695
8926095f
MS
2696void
2697pop_everything ()
2698{
2699#ifdef DEBUG_CP_BINDING_LEVELS
2700 fprintf (stderr, "XXX entering pop_everything ()\n");
2701#endif
8f032717 2702 while (!toplevel_bindings_p ())
8926095f 2703 {
8f032717 2704 if (current_binding_level->parm_flag == 2)
b74a0560 2705 pop_nested_class ();
8926095f
MS
2706 else
2707 poplevel (0, 0, 0);
2708 }
2709#ifdef DEBUG_CP_BINDING_LEVELS
2710 fprintf (stderr, "XXX leaving pop_everything ()\n");
2711#endif
2712}
2713
39c01e4c
MM
2714/* The type TYPE is being declared. If it is a class template, or a
2715 specialization of a class template, do any processing required and
2716 perform error-checking. If IS_FRIEND is non-zero, this TYPE is
2717 being declared a friend. B is the binding level at which this TYPE
2718 should be bound.
2719
2720 Returns the TYPE_DECL for TYPE, which may have been altered by this
2721 processing. */
2722
2723static tree
2724maybe_process_template_type_declaration (type, globalize, b)
2725 tree type;
2726 int globalize;
2727 struct binding_level* b;
2728{
2729 tree decl = TYPE_NAME (type);
2730
2731 if (processing_template_parmlist)
2732 /* You can't declare a new template type in a template parameter
2733 list. But, you can declare a non-template type:
2734
2735 template <class A*> struct S;
2736
2737 is a forward-declaration of `A'. */
2738 ;
2739 else
2740 {
2741 maybe_check_template_type (type);
2742
ed44da02
MM
2743 my_friendly_assert (IS_AGGR_TYPE (type)
2744 || TREE_CODE (type) == ENUMERAL_TYPE, 0);
2745
2746
3ebc5c52 2747 if (processing_template_decl)
39c01e4c
MM
2748 {
2749 /* This may change after the call to
2750 push_template_decl_real, but we want the original value. */
2751 tree name = DECL_NAME (decl);
2752
2753 decl = push_template_decl_real (decl, globalize);
2754 /* If the current binding level is the binding level for the
2755 template parameters (see the comment in
2756 begin_template_parm_list) and the enclosing level is a class
2757 scope, and we're not looking at a friend, push the
2758 declaration of the member class into the class scope. In the
2759 friend case, push_template_decl will already have put the
2760 friend into global scope, if appropriate. */
ed44da02
MM
2761 if (TREE_CODE (type) != ENUMERAL_TYPE
2762 && !globalize && b->pseudo_global
39c01e4c
MM
2763 && b->level_chain->parm_flag == 2)
2764 {
61a127b3 2765 finish_member_declaration (CLASSTYPE_TI_TEMPLATE (type));
39c01e4c
MM
2766 /* Put this tag on the list of tags for the class, since
2767 that won't happen below because B is not the class
2768 binding level, but is instead the pseudo-global level. */
2769 b->level_chain->tags =
2770 saveable_tree_cons (name, type, b->level_chain->tags);
39c01e4c
MM
2771 if (TYPE_SIZE (current_class_type) == NULL_TREE)
2772 CLASSTYPE_TAGS (current_class_type) = b->level_chain->tags;
2773 }
2774 }
2775 }
2776
2777 return decl;
2778}
2779
9188c363
MM
2780/* In C++, you don't have to write `struct S' to refer to `S'; you
2781 can just use `S'. We accomplish this by creating a TYPE_DECL as
2782 if the user had written `typedef struct S S'. Create and return
2783 the TYPE_DECL for TYPE. */
2784
2785tree
2786create_implicit_typedef (name, type)
2787 tree name;
2788 tree type;
2789{
2790 tree decl;
2791
2792 decl = build_decl (TYPE_DECL, name, type);
2793 SET_DECL_ARTIFICIAL (decl);
2794 /* There are other implicit type declarations, like the one *within*
2795 a class that allows you to write `S::S'. We must distinguish
2796 amongst these. */
2797 SET_DECL_IMPLICIT_TYPEDEF_P (decl);
2798 TYPE_NAME (type) = decl;
2799
2800 return decl;
2801}
2802
8d08fdba 2803/* Push a tag name NAME for struct/class/union/enum type TYPE.
6757edfe 2804 Normally put it into the inner-most non-tag-transparent scope,
8d08fdba 2805 but if GLOBALIZE is true, put it in the inner-most non-class scope.
e92cc029 2806 The latter is needed for implicit declarations. */
8d08fdba
MS
2807
2808void
2809pushtag (name, type, globalize)
2810 tree name, type;
2811 int globalize;
2812{
2813 register struct binding_level *b;
8d08fdba 2814
8f032717 2815 b = current_binding_level;
8d08fdba
MS
2816 while (b->tag_transparent
2817 || (globalize && b->parm_flag == 2))
2818 b = b->level_chain;
2819
a9aedbc2 2820 if (toplevel_bindings_p ())
8d08fdba
MS
2821 b->tags = perm_tree_cons (name, type, b->tags);
2822 else
2823 b->tags = saveable_tree_cons (name, type, b->tags);
2824
2825 if (name)
2826 {
8d08fdba 2827 /* Do C++ gratuitous typedefing. */
db5ae43f 2828 if (IDENTIFIER_TYPE_VALUE (name) != type)
8d08fdba 2829 {
93cdc044 2830 register tree d = NULL_TREE;
848cf1e0 2831 int in_class = 0;
9188c363 2832 tree context = TYPE_CONTEXT (type);
280f9385 2833
280f9385
MM
2834 if (! context)
2835 {
2836 tree cs = current_scope ();
2837
2838 if (! globalize)
2839 context = cs;
2840 else if (cs != NULL_TREE
2841 && TREE_CODE_CLASS (TREE_CODE (cs)) == 't')
2842 /* When declaring a friend class of a local class, we want
2843 to inject the newly named class into the scope
2844 containing the local class, not the namespace scope. */
2845 context = hack_decl_function_context (get_type_decl (cs));
2846 }
280f9385
MM
2847 if (!context)
2848 context = current_namespace;
8d08fdba 2849
93cdc044
JM
2850 if ((b->pseudo_global && b->level_chain->parm_flag == 2)
2851 || b->parm_flag == 2)
2852 in_class = 1;
93cdc044 2853
848cf1e0
MM
2854 if (current_lang_name == lang_name_java)
2855 TYPE_FOR_JAVA (type) = 1;
93cdc044 2856
9188c363 2857 d = create_implicit_typedef (name, type);
cb0dbb9a 2858 DECL_CONTEXT (d) = FROB_CONTEXT (context);
9188c363
MM
2859 if (! in_class)
2860 set_identifier_type_value_with_scope (name, type, b);
e1cd6e56 2861
39c01e4c
MM
2862 d = maybe_process_template_type_declaration (type,
2863 globalize, b);
93cdc044
JM
2864
2865 if (b->parm_flag == 2)
61a127b3 2866 {
848cf1e0 2867 if (!PROCESSING_REAL_TEMPLATE_DECL_P ())
61a127b3
MM
2868 /* Put this TYPE_DECL on the TYPE_FIELDS list for the
2869 class. But if it's a member template class, we
2870 want the TEMPLATE_DECL, not the TYPE_DECL, so this
2871 is done later. */
2872 finish_member_declaration (d);
8f032717
MM
2873 else
2874 pushdecl_class_level (d);
61a127b3 2875 }
93cdc044
JM
2876 else
2877 d = pushdecl_with_scope (d, b);
2878
848cf1e0
MM
2879 if (ANON_AGGRNAME_P (name))
2880 DECL_IGNORED_P (d) = 1;
2881
2882 TYPE_CONTEXT (type) = DECL_CONTEXT (d);
2883 DECL_ASSEMBLER_NAME (d) = DECL_NAME (d);
2884 if (!uses_template_parms (type))
2885 DECL_ASSEMBLER_NAME (d)
2886 = get_identifier (build_overload_name (type, 1, 1));
8d08fdba
MS
2887 }
2888 if (b->parm_flag == 2)
2889 {
8d08fdba
MS
2890 if (TYPE_SIZE (current_class_type) == NULL_TREE)
2891 CLASSTYPE_TAGS (current_class_type) = b->tags;
2892 }
2893 }
2894
2895 if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL)
2896 /* Use the canonical TYPE_DECL for this node. */
2897 TYPE_STUB_DECL (type) = TYPE_NAME (type);
2898 else
2899 {
2900 /* Create a fake NULL-named TYPE_DECL node whose TREE_TYPE
2901 will be the tagged type we just added to the current
2902 binding level. This fake NULL-named TYPE_DECL node helps
2903 dwarfout.c to know when it needs to output a
2904 representation of a tagged type, and it also gives us a
2905 convenient place to record the "scope start" address for
2906 the tagged type. */
2907
8d08fdba 2908 tree d = build_decl (TYPE_DECL, NULL_TREE, type);
8d08fdba
MS
2909 TYPE_STUB_DECL (type) = pushdecl_with_scope (d, b);
2910 }
2911}
2912
2913/* Counter used to create anonymous type names. */
e92cc029 2914
8d08fdba
MS
2915static int anon_cnt = 0;
2916
2917/* Return an IDENTIFIER which can be used as a name for
2918 anonymous structs and unions. */
e92cc029 2919
8d08fdba
MS
2920tree
2921make_anon_name ()
2922{
2923 char buf[32];
2924
2925 sprintf (buf, ANON_AGGRNAME_FORMAT, anon_cnt++);
2926 return get_identifier (buf);
2927}
2928
2929/* Clear the TREE_PURPOSE slot of tags which have anonymous typenames.
2930 This keeps dbxout from getting confused. */
e92cc029 2931
8d08fdba
MS
2932void
2933clear_anon_tags ()
2934{
2935 register struct binding_level *b;
2936 register tree tags;
2937 static int last_cnt = 0;
2938
2939 /* Fast out if no new anon names were declared. */
2940 if (last_cnt == anon_cnt)
2941 return;
2942
2943 b = current_binding_level;
2944 while (b->tag_transparent)
2945 b = b->level_chain;
2946 tags = b->tags;
2947 while (tags)
2948 {
2949 /* A NULL purpose means we have already processed all tags
2950 from here to the end of the list. */
2951 if (TREE_PURPOSE (tags) == NULL_TREE)
2952 break;
2953 if (ANON_AGGRNAME_P (TREE_PURPOSE (tags)))
2954 TREE_PURPOSE (tags) = NULL_TREE;
2955 tags = TREE_CHAIN (tags);
2956 }
2957 last_cnt = anon_cnt;
2958}
2959\f
2960/* Subroutine of duplicate_decls: return truthvalue of whether
2961 or not types of these decls match.
2962
2963 For C++, we must compare the parameter list so that `int' can match
2964 `int&' in a parameter position, but `int&' is not confused with
2965 `const int&'. */
e92cc029 2966
6060a796 2967int
8d08fdba
MS
2968decls_match (newdecl, olddecl)
2969 tree newdecl, olddecl;
2970{
2971 int types_match;
2972
347d73d7
ML
2973 if (newdecl == olddecl)
2974 return 1;
2975
6b4b3deb
MM
2976 if (TREE_CODE (newdecl) != TREE_CODE (olddecl))
2977 /* If the two DECLs are not even the same kind of thing, we're not
2978 interested in their types. */
2979 return 0;
2980
2981 if (TREE_CODE (newdecl) == FUNCTION_DECL)
8d08fdba
MS
2982 {
2983 tree f1 = TREE_TYPE (newdecl);
2984 tree f2 = TREE_TYPE (olddecl);
2985 tree p1 = TYPE_ARG_TYPES (f1);
2986 tree p2 = TYPE_ARG_TYPES (f2);
2987
c5a6fc45
JM
2988 if (DECL_REAL_CONTEXT (newdecl) != DECL_REAL_CONTEXT (olddecl)
2989 && ! (DECL_LANGUAGE (newdecl) == lang_c
2990 && DECL_LANGUAGE (olddecl) == lang_c))
2991 return 0;
2992
8d08fdba
MS
2993 /* When we parse a static member function definition,
2994 we put together a FUNCTION_DECL which thinks its type
2995 is METHOD_TYPE. Change that to FUNCTION_TYPE, and
2996 proceed. */
2997 if (TREE_CODE (f1) == METHOD_TYPE && DECL_STATIC_FUNCTION_P (olddecl))
700f8a87 2998 revert_static_member_fn (&newdecl, &f1, &p1);
8d08fdba
MS
2999 else if (TREE_CODE (f2) == METHOD_TYPE
3000 && DECL_STATIC_FUNCTION_P (newdecl))
700f8a87 3001 revert_static_member_fn (&olddecl, &f2, &p2);
8d08fdba
MS
3002
3003 /* Here we must take care of the case where new default
3004 parameters are specified. Also, warn if an old
3005 declaration becomes ambiguous because default
3006 parameters may cause the two to be ambiguous. */
3007 if (TREE_CODE (f1) != TREE_CODE (f2))
3008 {
3009 if (TREE_CODE (f1) == OFFSET_TYPE)
8251199e 3010 cp_compiler_error ("`%D' redeclared as member function", newdecl);
8d08fdba 3011 else
8251199e 3012 cp_compiler_error ("`%D' redeclared as non-member function", newdecl);
8d08fdba
MS
3013 return 0;
3014 }
3015
3bfdc719 3016 if (same_type_p (TREE_TYPE (f1), TREE_TYPE (f2)))
8926095f 3017 {
a28e3c7f 3018 if (! strict_prototypes_lang_c && DECL_LANGUAGE (olddecl) == lang_c
8926095f 3019 && p2 == NULL_TREE)
a28e3c7f
MS
3020 {
3021 types_match = self_promoting_args_p (p1);
3022 if (p1 == void_list_node)
3023 TREE_TYPE (newdecl) = TREE_TYPE (olddecl);
3024 }
3025 else if (!strict_prototypes_lang_c && DECL_LANGUAGE (olddecl)==lang_c
3026 && DECL_LANGUAGE (newdecl) == lang_c && p1 == NULL_TREE)
3027 {
3028 types_match = self_promoting_args_p (p2);
3029 TREE_TYPE (newdecl) = TREE_TYPE (olddecl);
3030 }
8926095f 3031 else
91063b51 3032 types_match = compparms (p1, p2);
8926095f 3033 }
8d08fdba
MS
3034 else
3035 types_match = 0;
3036 }
6b4b3deb 3037 else if (TREE_CODE (newdecl) == TEMPLATE_DECL)
51c184be 3038 {
f84b4be9
JM
3039 if (!comp_template_parms (DECL_TEMPLATE_PARMS (newdecl),
3040 DECL_TEMPLATE_PARMS (olddecl)))
3041 return 0;
3042
3043 if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL)
3044 types_match = 1;
3045 else
3046 types_match = decls_match (DECL_TEMPLATE_RESULT (olddecl),
3047 DECL_TEMPLATE_RESULT (newdecl));
51c184be 3048 }
8d08fdba
MS
3049 else
3050 {
3051 if (TREE_TYPE (newdecl) == error_mark_node)
3052 types_match = TREE_TYPE (olddecl) == error_mark_node;
3053 else if (TREE_TYPE (olddecl) == NULL_TREE)
3054 types_match = TREE_TYPE (newdecl) == NULL_TREE;
a0a33927
MS
3055 else if (TREE_TYPE (newdecl) == NULL_TREE)
3056 types_match = 0;
8d08fdba 3057 else
01240200 3058 types_match = comptypes (TREE_TYPE (newdecl),
3bfdc719
MM
3059 TREE_TYPE (olddecl),
3060 COMPARE_REDECLARATION);
8d08fdba
MS
3061 }
3062
3063 return types_match;
3064}
3065
3066/* If NEWDECL is `static' and an `extern' was seen previously,
3067 warn about it. (OLDDECL may be NULL_TREE; NAME contains
3068 information about previous usage as an `extern'.)
3069
3070 Note that this does not apply to the C++ case of declaring
3071 a variable `extern const' and then later `const'.
3072
8d08fdba
MS
3073 Don't complain about built-in functions, since they are beyond
3074 the user's control. */
3075
3076static void
3077warn_extern_redeclared_static (newdecl, olddecl)
3078 tree newdecl, olddecl;
3079{
3080 tree name;
3081
d8e178a0 3082 static const char *explicit_extern_static_warning
8251199e 3083 = "`%D' was declared `extern' and later `static'";
d8e178a0 3084 static const char *implicit_extern_static_warning
8251199e
JM
3085 = "`%D' was declared implicitly `extern' and later `static'";
3086
d22c8596 3087 if (TREE_CODE (newdecl) == TYPE_DECL)
8d08fdba
MS
3088 return;
3089
3090 name = DECL_ASSEMBLER_NAME (newdecl);
faae18ab 3091 if (TREE_PUBLIC (name) && DECL_THIS_STATIC (newdecl))
8d08fdba
MS
3092 {
3093 /* It's okay to redeclare an ANSI built-in function as static,
3094 or to declare a non-ANSI built-in function as anything. */
3095 if (! (TREE_CODE (newdecl) == FUNCTION_DECL
3096 && olddecl != NULL_TREE
3097 && TREE_CODE (olddecl) == FUNCTION_DECL
9f33663b 3098 && DECL_ARTIFICIAL (olddecl)))
8d08fdba 3099 {
a9aedbc2 3100 cp_pedwarn (IDENTIFIER_IMPLICIT_DECL (name)
8251199e
JM
3101 ? implicit_extern_static_warning
3102 : explicit_extern_static_warning, newdecl);
8d08fdba 3103 if (olddecl != NULL_TREE)
8251199e 3104 cp_pedwarn_at ("previous declaration of `%D'", olddecl);
8d08fdba
MS
3105 }
3106 }
3107}
3108
3109/* Handle when a new declaration NEWDECL has the same name as an old
3110 one OLDDECL in the same binding contour. Prints an error message
3111 if appropriate.
3112
3113 If safely possible, alter OLDDECL to look like NEWDECL, and return 1.
3114 Otherwise, return 0. */
3115
51c184be 3116int
8d08fdba 3117duplicate_decls (newdecl, olddecl)
824b9a4c 3118 tree newdecl, olddecl;
8d08fdba
MS
3119{
3120 extern struct obstack permanent_obstack;
3121 unsigned olddecl_uid = DECL_UID (olddecl);
3122 int olddecl_friend = 0, types_match = 0;
0b60dfe3 3123 int new_defines_function = 0;
5566b478
MS
3124
3125 if (newdecl == olddecl)
3126 return 1;
8d08fdba 3127
8926095f 3128 types_match = decls_match (newdecl, olddecl);
8d08fdba 3129
8d08fdba
MS
3130 /* If either the type of the new decl or the type of the old decl is an
3131 error_mark_node, then that implies that we have already issued an
3132 error (earlier) for some bogus type specification, and in that case,
3133 it is rather pointless to harass the user with yet more error message
0b60dfe3 3134 about the same declaration, so just pretend the types match here. */
bd6dd845
MS
3135 if (TREE_TYPE (newdecl) == error_mark_node
3136 || TREE_TYPE (olddecl) == error_mark_node)
8d08fdba 3137 types_match = 1;
0b60dfe3
BK
3138
3139 /* Check for redeclaration and other discrepancies. */
d22c8596 3140 if (TREE_CODE (olddecl) == FUNCTION_DECL
9f33663b
JM
3141 && DECL_ARTIFICIAL (olddecl))
3142 {
3143 if (TREE_CODE (newdecl) != FUNCTION_DECL)
3144 {
3145 /* If you declare a built-in or predefined function name as static,
3146 the old definition is overridden, but optionally warn this was a
3147 bad choice of name. */
3148 if (! TREE_PUBLIC (newdecl))
3149 {
3150 if (warn_shadow)
3151 cp_warning ("shadowing %s function `%#D'",
3152 DECL_BUILT_IN (olddecl) ? "built-in" : "library",
3153 olddecl);
3154 /* Discard the old built-in function. */
3155 return 0;
3156 }
3157 /* If the built-in is not ansi, then programs can override
3158 it even globally without an error. */
3159 else if (! DECL_BUILT_IN (olddecl))
3160 cp_warning ("library function `%#D' redeclared as non-function `%#D'",
3161 olddecl, newdecl);
3162 else
3163 {
3164 cp_error ("declaration of `%#D'", newdecl);
3165 cp_error ("conflicts with built-in declaration `%#D'",
a4443a08 3166 olddecl);
9f33663b 3167 }
a4443a08
MS
3168 return 0;
3169 }
9f33663b 3170 else if (!types_match)
8d08fdba 3171 {
9f33663b
JM
3172 if ((DECL_LANGUAGE (newdecl) == lang_c
3173 && DECL_LANGUAGE (olddecl) == lang_c)
3174 || compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
3175 TYPE_ARG_TYPES (TREE_TYPE (olddecl))))
a4443a08 3176 {
9f33663b
JM
3177 /* A near match; override the builtin. */
3178
3179 if (TREE_PUBLIC (newdecl))
a4443a08 3180 {
9f33663b
JM
3181 cp_warning ("new declaration `%#D'", newdecl);
3182 cp_warning ("ambiguates built-in declaration `%#D'",
3183 olddecl);
a4443a08 3184 }
9f33663b
JM
3185 else if (warn_shadow)
3186 cp_warning ("shadowing %s function `%#D'",
3187 DECL_BUILT_IN (olddecl) ? "built-in" : "library",
3188 olddecl);
a4443a08 3189 }
9f33663b
JM
3190 else
3191 /* Discard the old built-in function. */
3192 return 0;
8d08fdba 3193 }
39211cd5
MS
3194 }
3195 else if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
3196 {
9ed182dc
JM
3197 if ((TREE_CODE (olddecl) == TYPE_DECL && DECL_ARTIFICIAL (olddecl)
3198 && TREE_CODE (newdecl) != TYPE_DECL
3199 && ! (TREE_CODE (newdecl) == TEMPLATE_DECL
3200 && TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL))
3201 || (TREE_CODE (newdecl) == TYPE_DECL && DECL_ARTIFICIAL (newdecl)
3202 && TREE_CODE (olddecl) != TYPE_DECL
3203 && ! (TREE_CODE (olddecl) == TEMPLATE_DECL
3204 && (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl))
3205 == TYPE_DECL))))
3206 {
3207 /* We do nothing special here, because C++ does such nasty
3208 things with TYPE_DECLs. Instead, just let the TYPE_DECL
3209 get shadowed, and know that if we need to find a TYPE_DECL
3210 for a given name, we can look in the IDENTIFIER_TYPE_VALUE
3211 slot of the identifier. */
3212 return 0;
3213 }
3214
39211cd5 3215 if ((TREE_CODE (newdecl) == FUNCTION_DECL
5566b478 3216 && DECL_FUNCTION_TEMPLATE_P (olddecl))
39211cd5 3217 || (TREE_CODE (olddecl) == FUNCTION_DECL
5566b478 3218 && DECL_FUNCTION_TEMPLATE_P (newdecl)))
39211cd5 3219 return 0;
9ed182dc 3220
8251199e 3221 cp_error ("`%#D' redeclared as different kind of symbol", newdecl);
39211cd5
MS
3222 if (TREE_CODE (olddecl) == TREE_LIST)
3223 olddecl = TREE_VALUE (olddecl);
8251199e 3224 cp_error_at ("previous declaration of `%#D'", olddecl);
39211cd5
MS
3225
3226 /* New decl is completely inconsistent with the old one =>
3227 tell caller to replace the old one. */
3228
3229 return 0;
8d08fdba 3230 }
8d08fdba
MS
3231 else if (!types_match)
3232 {
7bdbfa05
MM
3233 if (DECL_REAL_CONTEXT (newdecl) != DECL_REAL_CONTEXT (olddecl))
3234 /* These are certainly not duplicate declarations; they're
3235 from different scopes. */
3236 return 0;
3237
8926095f 3238 if (TREE_CODE (newdecl) == TEMPLATE_DECL)
f0e01782
MS
3239 {
3240 /* The name of a class template may not be declared to refer to
3241 any other template, class, function, object, namespace, value,
e92cc029 3242 or type in the same scope. */
5566b478
MS
3243 if (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) == TYPE_DECL
3244 || TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL)
f0e01782 3245 {
8251199e
JM
3246 cp_error ("declaration of template `%#D'", newdecl);
3247 cp_error_at ("conflicts with previous declaration `%#D'",
f0e01782
MS
3248 olddecl);
3249 }
ec255269
MS
3250 else if (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) == FUNCTION_DECL
3251 && TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == FUNCTION_DECL
3252 && compparms (TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (olddecl))),
91063b51 3253 TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (newdecl))))
75650646
MM
3254 && comp_template_parms (DECL_TEMPLATE_PARMS (newdecl),
3255 DECL_TEMPLATE_PARMS (olddecl)))
ec255269 3256 {
8251199e
JM
3257 cp_error ("new declaration `%#D'", newdecl);
3258 cp_error_at ("ambiguates old declaration `%#D'", olddecl);
ec255269 3259 }
f0e01782
MS
3260 return 0;
3261 }
8926095f
MS
3262 if (TREE_CODE (newdecl) == FUNCTION_DECL)
3263 {
3264 if (DECL_LANGUAGE (newdecl) == lang_c
3265 && DECL_LANGUAGE (olddecl) == lang_c)
3266 {
8251199e 3267 cp_error ("declaration of C function `%#D' conflicts with",
8926095f 3268 newdecl);
8251199e 3269 cp_error_at ("previous declaration `%#D' here", olddecl);
8926095f 3270 }
00595019 3271 else if (compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
91063b51 3272 TYPE_ARG_TYPES (TREE_TYPE (olddecl))))
700f8a87 3273 {
8251199e
JM
3274 cp_error ("new declaration `%#D'", newdecl);
3275 cp_error_at ("ambiguates old declaration `%#D'", olddecl);
700f8a87
MS
3276 }
3277 else
3278 return 0;
8926095f 3279 }
8d08fdba
MS
3280
3281 /* Already complained about this, so don't do so again. */
a4443a08 3282 else if (current_class_type == NULL_TREE
8d08fdba
MS
3283 || IDENTIFIER_ERROR_LOCUS (DECL_ASSEMBLER_NAME (newdecl)) != current_class_type)
3284 {
8251199e
JM
3285 cp_error ("conflicting types for `%#D'", newdecl);
3286 cp_error_at ("previous declaration as `%#D'", olddecl);
8d08fdba
MS
3287 }
3288 }
75650646
MM
3289 else if (TREE_CODE (newdecl) == FUNCTION_DECL
3290 && ((DECL_TEMPLATE_SPECIALIZATION (olddecl)
3291 && (!DECL_TEMPLATE_INFO (newdecl)
3292 || (DECL_TI_TEMPLATE (newdecl)
3293 != DECL_TI_TEMPLATE (olddecl))))
3294 || (DECL_TEMPLATE_SPECIALIZATION (newdecl)
3295 && (!DECL_TEMPLATE_INFO (olddecl)
3296 || (DECL_TI_TEMPLATE (olddecl)
3297 != DECL_TI_TEMPLATE (newdecl))))))
386b8a85
JM
3298 /* It's OK to have a template specialization and a non-template
3299 with the same type, or to have specializations of two
75650646
MM
3300 different templates with the same type. Note that if one is a
3301 specialization, and the other is an instantiation of the same
3302 template, that we do not exit at this point. That situation
3303 can occur if we instantiate a template class, and then
3304 specialize one of its methods. This situation is legal, but
3305 the declarations must be merged in the usual way. */
3306 return 0;
3307 else if (TREE_CODE (newdecl) == FUNCTION_DECL
3308 && ((DECL_TEMPLATE_INSTANTIATION (olddecl)
3309 && !DECL_USE_TEMPLATE (newdecl))
3310 || (DECL_TEMPLATE_INSTANTIATION (newdecl)
3311 && !DECL_USE_TEMPLATE (olddecl))))
3312 /* One of the declarations is a template instantiation, and the
3313 other is not a template at all. That's OK. */
386b8a85 3314 return 0;
85c6cbaf
ML
3315 else if (TREE_CODE (newdecl) == NAMESPACE_DECL
3316 && DECL_NAMESPACE_ALIAS (newdecl)
3317 && DECL_NAMESPACE_ALIAS (newdecl) == DECL_NAMESPACE_ALIAS (olddecl))
3318 /* Redeclaration of namespace alias, ignore it. */
3319 return 1;
8d08fdba
MS
3320 else
3321 {
d8e178a0 3322 const char *errmsg = redeclaration_error_message (newdecl, olddecl);
8251199e 3323 if (errmsg)
8d08fdba 3324 {
8251199e 3325 cp_error (errmsg, newdecl);
8d08fdba
MS
3326 if (DECL_NAME (olddecl) != NULL_TREE)
3327 cp_error_at ((DECL_INITIAL (olddecl)
2c73f9f5 3328 && namespace_bindings_p ())
8251199e
JM
3329 ? "`%#D' previously defined here"
3330 : "`%#D' previously declared here", olddecl);
8d08fdba
MS
3331 }
3332 else if (TREE_CODE (olddecl) == FUNCTION_DECL
3333 && DECL_INITIAL (olddecl) != NULL_TREE
3334 && TYPE_ARG_TYPES (TREE_TYPE (olddecl)) == NULL_TREE
3335 && TYPE_ARG_TYPES (TREE_TYPE (newdecl)) != NULL_TREE)
3336 {
3337 /* Prototype decl follows defn w/o prototype. */
8251199e
JM
3338 cp_warning_at ("prototype for `%#D'", newdecl);
3339 cp_warning_at ("follows non-prototype definition here", olddecl);
8d08fdba
MS
3340 }
3341 else if (TREE_CODE (olddecl) == FUNCTION_DECL
3342 && DECL_LANGUAGE (newdecl) != DECL_LANGUAGE (olddecl))
8926095f
MS
3343 {
3344 /* extern "C" int foo ();
3345 int foo () { bar (); }
3346 is OK. */
3347 if (current_lang_stack == current_lang_base)
a28e3c7f 3348 DECL_LANGUAGE (newdecl) = DECL_LANGUAGE (olddecl);
8926095f
MS
3349 else
3350 {
8251199e 3351 cp_error_at ("previous declaration of `%#D' with %L linkage",
8926095f 3352 olddecl, DECL_LANGUAGE (olddecl));
8251199e 3353 cp_error ("conflicts with new declaration with %L linkage",
8926095f
MS
3354 DECL_LANGUAGE (newdecl));
3355 }
3356 }
e1cd6e56 3357
a6f02587 3358 if (DECL_LANG_SPECIFIC (olddecl) && DECL_USE_TEMPLATE (olddecl))
c32381b1
JM
3359 ;
3360 else if (TREE_CODE (olddecl) == FUNCTION_DECL)
e1cd6e56
MS
3361 {
3362 tree t1 = TYPE_ARG_TYPES (TREE_TYPE (olddecl));
3363 tree t2 = TYPE_ARG_TYPES (TREE_TYPE (newdecl));
3364 int i = 1;
3365
3366 if (TREE_CODE (TREE_TYPE (newdecl)) == METHOD_TYPE)
3367 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2);
3368
3369 for (; t1 && t1 != void_list_node;
3370 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2), i++)
3371 if (TREE_PURPOSE (t1) && TREE_PURPOSE (t2))
3372 {
2507f3b5
RK
3373 if (1 == simple_cst_equal (TREE_PURPOSE (t1),
3374 TREE_PURPOSE (t2)))
e1cd6e56
MS
3375 {
3376 if (pedantic)
3377 {
8251199e 3378 cp_pedwarn ("default argument given for parameter %d of `%#D'",
e1cd6e56 3379 i, newdecl);
8251199e 3380 cp_pedwarn_at ("after previous specification in `%#D'",
e1cd6e56
MS
3381 olddecl);
3382 }
3383 }
3384 else
3385 {
8251199e 3386 cp_error ("default argument given for parameter %d of `%#D'",
e1cd6e56 3387 i, newdecl);
8251199e 3388 cp_error_at ("after previous specification in `%#D'",
e1cd6e56
MS
3389 olddecl);
3390 }
3391 }
a5894242 3392
7fcdf4c2
MS
3393 if (DECL_THIS_INLINE (newdecl) && ! DECL_THIS_INLINE (olddecl)
3394 && TREE_ADDRESSABLE (olddecl) && warn_inline)
dff6b454 3395 {
8251199e 3396 cp_warning ("`%#D' was used before it was declared inline",
7fcdf4c2 3397 newdecl);
8251199e 3398 cp_warning_at ("previous non-inline declaration here",
7fcdf4c2 3399 olddecl);
dff6b454 3400 }
e1cd6e56 3401 }
8d08fdba
MS
3402 }
3403
3404 /* If new decl is `static' and an `extern' was seen previously,
3405 warn about it. */
3406 warn_extern_redeclared_static (newdecl, olddecl);
3407
e92cc029 3408 /* We have committed to returning 1 at this point. */
8d08fdba
MS
3409 if (TREE_CODE (newdecl) == FUNCTION_DECL)
3410 {
3411 /* Now that functions must hold information normally held
3412 by field decls, there is extra work to do so that
3413 declaration information does not get destroyed during
3414 definition. */
3415 if (DECL_VINDEX (olddecl))
3416 DECL_VINDEX (newdecl) = DECL_VINDEX (olddecl);
3417 if (DECL_CONTEXT (olddecl))
3418 DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
3419 if (DECL_CLASS_CONTEXT (olddecl))
3420 DECL_CLASS_CONTEXT (newdecl) = DECL_CLASS_CONTEXT (olddecl);
8d08fdba
MS
3421 if (DECL_PENDING_INLINE_INFO (newdecl) == (struct pending_inline *)0)
3422 DECL_PENDING_INLINE_INFO (newdecl) = DECL_PENDING_INLINE_INFO (olddecl);
e1cd6e56
MS
3423 DECL_STATIC_CONSTRUCTOR (newdecl) |= DECL_STATIC_CONSTRUCTOR (olddecl);
3424 DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
7215f9a0 3425 DECL_ABSTRACT_VIRTUAL_P (newdecl) |= DECL_ABSTRACT_VIRTUAL_P (olddecl);
864b83b9 3426 DECL_VIRTUAL_P (newdecl) |= DECL_VIRTUAL_P (olddecl);
4a67c9e9 3427 DECL_NEEDS_FINAL_OVERRIDER_P (newdecl) |= DECL_NEEDS_FINAL_OVERRIDER_P (olddecl);
0b60dfe3
BK
3428 new_defines_function = DECL_INITIAL (newdecl) != NULL_TREE;
3429
3430 /* Optionally warn about more than one declaration for the same
3431 name, but don't warn about a function declaration followed by a
3432 definition. */
3433 if (warn_redundant_decls && ! DECL_ARTIFICIAL (olddecl)
3434 && !(new_defines_function && DECL_INITIAL (olddecl) == NULL_TREE)
3435 /* Don't warn about extern decl followed by definition. */
3436 && !(DECL_EXTERNAL (olddecl) && ! DECL_EXTERNAL (newdecl))
3437 /* Don't warn about friends, let add_friend take care of it. */
3438 && ! DECL_FRIEND_P (newdecl))
3439 {
8251199e
JM
3440 cp_warning ("redundant redeclaration of `%D' in same scope", newdecl);
3441 cp_warning_at ("previous declaration of `%D'", olddecl);
0b60dfe3 3442 }
8d08fdba
MS
3443 }
3444
3445 /* Deal with C++: must preserve virtual function table size. */
3446 if (TREE_CODE (olddecl) == TYPE_DECL)
3447 {
3448 register tree newtype = TREE_TYPE (newdecl);
3449 register tree oldtype = TREE_TYPE (olddecl);
3450
3451 if (newtype != error_mark_node && oldtype != error_mark_node
3452 && TYPE_LANG_SPECIFIC (newtype) && TYPE_LANG_SPECIFIC (oldtype))
3453 {
3454 CLASSTYPE_VSIZE (newtype) = CLASSTYPE_VSIZE (oldtype);
3455 CLASSTYPE_FRIEND_CLASSES (newtype)
3456 = CLASSTYPE_FRIEND_CLASSES (oldtype);
3457 }
8d08fdba
MS
3458 }
3459
8d08fdba
MS
3460 /* Copy all the DECL_... slots specified in the new decl
3461 except for any that we copy here from the old type. */
0b60dfe3
BK
3462 DECL_MACHINE_ATTRIBUTES (newdecl)
3463 = merge_machine_decl_attributes (olddecl, newdecl);
8d08fdba 3464
5566b478
MS
3465 if (TREE_CODE (newdecl) == TEMPLATE_DECL)
3466 {
4d85e00e
MM
3467 if (! duplicate_decls (DECL_TEMPLATE_RESULT (newdecl),
3468 DECL_TEMPLATE_RESULT (olddecl)))
8251199e 3469 cp_error ("invalid redeclaration of %D", newdecl);
4d85e00e 3470 TREE_TYPE (olddecl) = TREE_TYPE (DECL_TEMPLATE_RESULT (olddecl));
4d85e00e
MM
3471 DECL_TEMPLATE_SPECIALIZATIONS (olddecl)
3472 = chainon (DECL_TEMPLATE_SPECIALIZATIONS (olddecl),
3473 DECL_TEMPLATE_SPECIALIZATIONS (newdecl));
0b60dfe3 3474
5566b478
MS
3475 return 1;
3476 }
0b60dfe3 3477
8d08fdba
MS
3478 if (types_match)
3479 {
3480 /* Automatically handles default parameters. */
3481 tree oldtype = TREE_TYPE (olddecl);
e1cd6e56 3482 tree newtype;
8d08fdba 3483
e1cd6e56
MS
3484 /* Make sure we put the new type in the same obstack as the old one. */
3485 if (oldtype)
39211cd5
MS
3486 push_obstacks (TYPE_OBSTACK (oldtype), TYPE_OBSTACK (oldtype));
3487 else
9188c363 3488 push_permanent_obstack ();
39211cd5 3489
e1cd6e56
MS
3490 /* Merge the data types specified in the two decls. */
3491 newtype = common_type (TREE_TYPE (newdecl), TREE_TYPE (olddecl));
3492
8d08fdba
MS
3493 if (TREE_CODE (newdecl) == VAR_DECL)
3494 DECL_THIS_EXTERN (newdecl) |= DECL_THIS_EXTERN (olddecl);
3495 /* Do this after calling `common_type' so that default
3496 parameters don't confuse us. */
3497 else if (TREE_CODE (newdecl) == FUNCTION_DECL
3498 && (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (newdecl))
3499 != TYPE_RAISES_EXCEPTIONS (TREE_TYPE (olddecl))))
3500 {
f30432d7 3501 TREE_TYPE (newdecl) = build_exception_variant (newtype,
8d08fdba 3502 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (newdecl)));
f30432d7 3503 TREE_TYPE (olddecl) = build_exception_variant (newtype,
8d08fdba
MS
3504 TYPE_RAISES_EXCEPTIONS (oldtype));
3505
9a224b4a
JM
3506 if ((pedantic || ! DECL_IN_SYSTEM_HEADER (olddecl))
3507 && DECL_SOURCE_LINE (olddecl) != 0
da20811c 3508 && flag_exceptions
4cc1d462
NS
3509 && !comp_except_specs (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (newdecl)),
3510 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (olddecl)), 1))
8d08fdba 3511 {
4cc1d462 3512 cp_error ("declaration of `%F' throws different exceptions",
a28e3c7f 3513 newdecl);
4cc1d462 3514 cp_error_at ("to previous declaration `%F'", olddecl);
8d08fdba
MS
3515 }
3516 }
3517 TREE_TYPE (newdecl) = TREE_TYPE (olddecl) = newtype;
3518
3519 /* Lay the type out, unless already done. */
407f03b8 3520 if (! same_type_p (newtype, oldtype)
5566b478 3521 && TREE_TYPE (newdecl) != error_mark_node
5156628f 3522 && !(processing_template_decl && uses_template_parms (newdecl)))
b7484fbe
MS
3523 layout_type (TREE_TYPE (newdecl));
3524
5566b478
MS
3525 if ((TREE_CODE (newdecl) == VAR_DECL
3526 || TREE_CODE (newdecl) == PARM_DECL
3527 || TREE_CODE (newdecl) == RESULT_DECL
3528 || TREE_CODE (newdecl) == FIELD_DECL
3529 || TREE_CODE (newdecl) == TYPE_DECL)
5156628f 3530 && !(processing_template_decl && uses_template_parms (newdecl)))
b7484fbe 3531 layout_decl (newdecl, 0);
8d08fdba
MS
3532
3533 /* Merge the type qualifiers. */
3534 if (TREE_READONLY (newdecl))
3535 TREE_READONLY (olddecl) = 1;
3536 if (TREE_THIS_VOLATILE (newdecl))
3537 TREE_THIS_VOLATILE (olddecl) = 1;
3538
3539 /* Merge the initialization information. */
8926095f
MS
3540 if (DECL_INITIAL (newdecl) == NULL_TREE
3541 && DECL_INITIAL (olddecl) != NULL_TREE)
3542 {
3543 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
3544 DECL_SOURCE_FILE (newdecl) = DECL_SOURCE_FILE (olddecl);
3545 DECL_SOURCE_LINE (newdecl) = DECL_SOURCE_LINE (olddecl);
b0d06515
MM
3546 if (CAN_HAVE_FULL_LANG_DECL_P (newdecl)
3547 && DECL_LANG_SPECIFIC (newdecl)
4d85e00e
MM
3548 && DECL_LANG_SPECIFIC (olddecl))
3549 DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
8926095f 3550 }
39211cd5
MS
3551
3552 /* Merge the section attribute.
3553 We want to issue an error if the sections conflict but that must be
3554 done later in decl_attributes since we are called before attributes
3555 are assigned. */
3556 if (DECL_SECTION_NAME (newdecl) == NULL_TREE)
3557 DECL_SECTION_NAME (newdecl) = DECL_SECTION_NAME (olddecl);
3558
8d08fdba
MS
3559 /* Keep the old rtl since we can safely use it, unless it's the
3560 call to abort() used for abstract virtuals. */
3561 if ((DECL_LANG_SPECIFIC (olddecl)
3562 && !DECL_ABSTRACT_VIRTUAL_P (olddecl))
3563 || DECL_RTL (olddecl) != DECL_RTL (abort_fndecl))
3564 DECL_RTL (newdecl) = DECL_RTL (olddecl);
39211cd5
MS
3565
3566 pop_obstacks ();
8d08fdba
MS
3567 }
3568 /* If cannot merge, then use the new type and qualifiers,
3569 and don't preserve the old rtl. */
3570 else
3571 {
3572 /* Clean out any memory we had of the old declaration. */
3573 tree oldstatic = value_member (olddecl, static_aggregates);
3574 if (oldstatic)
3575 TREE_VALUE (oldstatic) = error_mark_node;
3576
3577 TREE_TYPE (olddecl) = TREE_TYPE (newdecl);
3578 TREE_READONLY (olddecl) = TREE_READONLY (newdecl);
3579 TREE_THIS_VOLATILE (olddecl) = TREE_THIS_VOLATILE (newdecl);
3580 TREE_SIDE_EFFECTS (olddecl) = TREE_SIDE_EFFECTS (newdecl);
3581 }
3582
3583 /* Merge the storage class information. */
a9aedbc2 3584 DECL_WEAK (newdecl) |= DECL_WEAK (olddecl);
e92cc029 3585 DECL_ONE_ONLY (newdecl) |= DECL_ONE_ONLY (olddecl);
a9aedbc2
MS
3586 TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
3587 TREE_STATIC (olddecl) = TREE_STATIC (newdecl) |= TREE_STATIC (olddecl);
3588 if (! DECL_EXTERNAL (olddecl))
3589 DECL_EXTERNAL (newdecl) = 0;
0b60dfe3
BK
3590
3591 if (DECL_LANG_SPECIFIC (newdecl) && DECL_LANG_SPECIFIC (olddecl))
8d08fdba 3592 {
a9aedbc2
MS
3593 DECL_INTERFACE_KNOWN (newdecl) |= DECL_INTERFACE_KNOWN (olddecl);
3594 DECL_NOT_REALLY_EXTERN (newdecl) |= DECL_NOT_REALLY_EXTERN (olddecl);
77be6f82 3595 DECL_COMDAT (newdecl) |= DECL_COMDAT (olddecl);
2b0a63a3
MM
3596 DECL_TEMPLATE_INSTANTIATED (newdecl)
3597 |= DECL_TEMPLATE_INSTANTIATED (olddecl);
0b60dfe3
BK
3598 /* Don't really know how much of the language-specific
3599 values we should copy from old to new. */
3600 DECL_IN_AGGR_P (newdecl) = DECL_IN_AGGR_P (olddecl);
3601 DECL_ACCESS (newdecl) = DECL_ACCESS (olddecl);
3602 DECL_NONCONVERTING_P (newdecl) = DECL_NONCONVERTING_P (olddecl);
fbf1c34b 3603 DECL_TEMPLATE_INFO (newdecl) = DECL_TEMPLATE_INFO (olddecl);
0b60dfe3 3604 olddecl_friend = DECL_FRIEND_P (olddecl);
6a629cac
MM
3605
3606 /* Only functions have DECL_BEFRIENDING_CLASSES. */
3607 if (TREE_CODE (newdecl) == FUNCTION_DECL
3608 || DECL_FUNCTION_TEMPLATE_P (newdecl))
3609 DECL_BEFRIENDING_CLASSES (newdecl)
3610 = chainon (DECL_BEFRIENDING_CLASSES (newdecl),
3611 DECL_BEFRIENDING_CLASSES (olddecl));
8d08fdba
MS
3612 }
3613
8d08fdba
MS
3614 if (TREE_CODE (newdecl) == FUNCTION_DECL)
3615 {
75650646
MM
3616 if (DECL_TEMPLATE_INSTANTIATION (olddecl)
3617 && !DECL_TEMPLATE_INSTANTIATION (newdecl))
3618 {
3619 /* If newdecl is not a specialization, then it is not a
3620 template-related function at all. And that means that we
3621 shoud have exited above, returning 0. */
3622 my_friendly_assert (DECL_TEMPLATE_SPECIALIZATION (newdecl),
3623 0);
3624
3625 if (TREE_USED (olddecl))
3626 /* From [temp.expl.spec]:
3627
3628 If a template, a member template or the member of a class
3629 template is explicitly specialized then that
3630 specialization shall be declared before the first use of
3631 that specialization that would cause an implicit
3632 instantiation to take place, in every translation unit in
3633 which such a use occurs. */
8251199e 3634 cp_error ("explicit specialization of %D after first use",
75650646
MM
3635 olddecl);
3636
3637 SET_DECL_TEMPLATE_SPECIALIZATION (olddecl);
3638 }
faae18ab
MS
3639 DECL_THIS_INLINE (newdecl) |= DECL_THIS_INLINE (olddecl);
3640
3641 /* If either decl says `inline', this fn is inline, unless its
3642 definition was passed already. */
3643 if (DECL_INLINE (newdecl) && DECL_INITIAL (olddecl) == NULL_TREE)
3644 DECL_INLINE (olddecl) = 1;
3645 DECL_INLINE (newdecl) = DECL_INLINE (olddecl);
3646
700f8a87
MS
3647 if (! types_match)
3648 {
3649 DECL_LANGUAGE (olddecl) = DECL_LANGUAGE (newdecl);
3650 DECL_ASSEMBLER_NAME (olddecl) = DECL_ASSEMBLER_NAME (newdecl);
5566b478
MS
3651 DECL_RTL (olddecl) = DECL_RTL (newdecl);
3652 }
3653 if (! types_match || new_defines_function)
3654 {
6f1b4c42
JM
3655 /* These need to be copied so that the names are available.
3656 Note that if the types do match, we'll preserve inline
3657 info and other bits, but if not, we won't. */
700f8a87
MS
3658 DECL_ARGUMENTS (olddecl) = DECL_ARGUMENTS (newdecl);
3659 DECL_RESULT (olddecl) = DECL_RESULT (newdecl);
700f8a87 3660 }
8d08fdba
MS
3661 if (new_defines_function)
3662 /* If defining a function declared with other language
3663 linkage, use the previously declared language linkage. */
3664 DECL_LANGUAGE (newdecl) = DECL_LANGUAGE (olddecl);
6f1b4c42 3665 else if (types_match)
8d08fdba
MS
3666 {
3667 /* If redeclaring a builtin function, and not a definition,
3668 it stays built in. */
3669 if (DECL_BUILT_IN (olddecl))
3670 {
3671 DECL_BUILT_IN (newdecl) = 1;
39211cd5 3672 DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl);
8d08fdba
MS
3673 /* If we're keeping the built-in definition, keep the rtl,
3674 regardless of declaration matches. */
3675 DECL_RTL (newdecl) = DECL_RTL (olddecl);
3676 }
3677 else
3678 DECL_FRAME_SIZE (newdecl) = DECL_FRAME_SIZE (olddecl);
3679
3680 DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
8926095f 3681 if ((DECL_SAVED_INSNS (newdecl) = DECL_SAVED_INSNS (olddecl)))
8d08fdba
MS
3682 /* Previously saved insns go together with
3683 the function's previous definition. */
3684 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
3685 /* Don't clear out the arguments if we're redefining a function. */
3686 if (DECL_ARGUMENTS (olddecl))
3687 DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl);
3688 }
6060a796
MS
3689 if (DECL_LANG_SPECIFIC (olddecl))
3690 DECL_MAIN_VARIANT (newdecl) = DECL_MAIN_VARIANT (olddecl);
8d08fdba
MS
3691 }
3692
a9aedbc2
MS
3693 if (TREE_CODE (newdecl) == NAMESPACE_DECL)
3694 {
3695 NAMESPACE_LEVEL (newdecl) = NAMESPACE_LEVEL (olddecl);
3696 }
3697
8d08fdba
MS
3698 /* Now preserve various other info from the definition. */
3699 TREE_ADDRESSABLE (newdecl) = TREE_ADDRESSABLE (olddecl);
3700 TREE_ASM_WRITTEN (newdecl) = TREE_ASM_WRITTEN (olddecl);
3701 DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
f376e137 3702 DECL_ASSEMBLER_NAME (newdecl) = DECL_ASSEMBLER_NAME (olddecl);
8d08fdba 3703
8d08fdba
MS
3704 if (TREE_CODE (newdecl) == FUNCTION_DECL)
3705 {
3706 int function_size;
3707 struct lang_decl *ol = DECL_LANG_SPECIFIC (olddecl);
3708 struct lang_decl *nl = DECL_LANG_SPECIFIC (newdecl);
3709
3710 function_size = sizeof (struct tree_decl);
3711
3712 bcopy ((char *) newdecl + sizeof (struct tree_common),
3713 (char *) olddecl + sizeof (struct tree_common),
3714 function_size - sizeof (struct tree_common));
3715
3716 /* Can we safely free the storage used by newdecl? */
3717
3718#define ROUND(x) ((x + obstack_alignment_mask (&permanent_obstack)) \
3719 & ~ obstack_alignment_mask (&permanent_obstack))
3720
75650646
MM
3721 if (DECL_TEMPLATE_INSTANTIATION (newdecl))
3722 {
3723 /* If newdecl is a template instantiation, it is possible that
3724 the following sequence of events has occurred:
3725
3726 o A friend function was declared in a class template. The
3727 class template was instantiated.
3728
3729 o The instantiation of the friend declaration was
3730 recorded on the instantiation list, and is newdecl.
3731
3732 o Later, however, instantiate_class_template called pushdecl
3733 on the newdecl to perform name injection. But, pushdecl in
3734 turn called duplicate_decls when it discovered that another
3735 declaration of a global function with the same name already
3736 existed.
3737
3738 o Here, in duplicate_decls, we decided to clobber newdecl.
3739
3740 If we're going to do that, we'd better make sure that
3741 olddecl, and not newdecl, is on the list of
3742 instantiations so that if we try to do the instantiation
3743 again we won't get the clobbered declaration. */
3744
3745 tree tmpl = DECL_TI_TEMPLATE (newdecl);
3746 tree decls = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
3747
3748 for (; decls; decls = TREE_CHAIN (decls))
3749 if (TREE_VALUE (decls) == newdecl)
3750 TREE_VALUE (decls) = olddecl;
3751 }
3752
3a56f0ab
JM
3753 if (((char *)newdecl + ROUND (function_size) == (char *)nl
3754 && ((char *)newdecl + ROUND (function_size)
3755 + ROUND (sizeof (struct lang_decl))
3756 == obstack_next_free (&permanent_obstack)))
3757 || ((char *)newdecl + ROUND (function_size)
3758 == obstack_next_free (&permanent_obstack)))
8d08fdba
MS
3759 {
3760 DECL_MAIN_VARIANT (newdecl) = olddecl;
3761 DECL_LANG_SPECIFIC (olddecl) = ol;
3762 bcopy ((char *)nl, (char *)ol, sizeof (struct lang_decl));
3763
3764 obstack_free (&permanent_obstack, newdecl);
3765 }
d22c8596 3766 else if (LANG_DECL_PERMANENT (ol) && ol != nl)
8d08fdba
MS
3767 {
3768 if (DECL_MAIN_VARIANT (olddecl) == olddecl)
3769 {
9188c363 3770 struct lang_decl *free_lang_decl = ol;
d22c8596 3771
9188c363 3772 /* Save these lang_decls that would otherwise be lost. */
d22c8596
MS
3773 if (DECL_LANG_SPECIFIC (olddecl) == ol)
3774 abort ();
3775
9188c363 3776 free_lang_decl->u.next = free_lang_decl_chain;
8d08fdba
MS
3777 free_lang_decl_chain = free_lang_decl;
3778 }
3779 else
3780 {
bd6dd845 3781 /* Storage leak. */;
8d08fdba
MS
3782 }
3783 }
3784 }
3785 else
3786 {
3787 bcopy ((char *) newdecl + sizeof (struct tree_common),
3788 (char *) olddecl + sizeof (struct tree_common),
3789 sizeof (struct tree_decl) - sizeof (struct tree_common)
3790 + tree_code_length [(int)TREE_CODE (newdecl)] * sizeof (char *));
3791 }
3792
3793 DECL_UID (olddecl) = olddecl_uid;
3794 if (olddecl_friend)
3795 DECL_FRIEND_P (olddecl) = 1;
3796
d9525bec
BK
3797 /* NEWDECL contains the merged attribute lists.
3798 Update OLDDECL to be the same. */
3799 DECL_MACHINE_ATTRIBUTES (olddecl) = DECL_MACHINE_ATTRIBUTES (newdecl);
3800
8d08fdba
MS
3801 return 1;
3802}
3803
3804/* Record a decl-node X as belonging to the current lexical scope.
3805 Check for errors (such as an incompatible declaration for the same
3806 name already seen in the same scope).
3807
3808 Returns either X or an old decl for the same name.
3809 If an old decl is returned, it may have been smashed
3810 to agree with what X says. */
3811
3812tree
3813pushdecl (x)
3814 tree x;
3815{
3816 register tree t;
8d08fdba 3817 register tree name = DECL_ASSEMBLER_NAME (x);
f181d4ae 3818 int need_new_binding = 1;
8d08fdba 3819
50714e79
MM
3820 if (DECL_TEMPLATE_PARM_P (x))
3821 /* Template parameters have no context; they are not X::T even
3822 when declared within a class or namespace. */
3823 ;
3824 else
3825 {
3826 if (current_function_decl && x != current_function_decl
3827 /* A local declaration for a function doesn't constitute
3828 nesting. */
3829 && (TREE_CODE (x) != FUNCTION_DECL || DECL_INITIAL (x))
3830 /* Don't change DECL_CONTEXT of virtual methods. */
3831 && (TREE_CODE (x) != FUNCTION_DECL || !DECL_VIRTUAL_P (x))
3832 && !DECL_CONTEXT (x))
3833 DECL_CONTEXT (x) = current_function_decl;
3834 if (!DECL_CONTEXT (x))
3835 DECL_CONTEXT (x) = FROB_CONTEXT (current_namespace);
3836 }
8d08fdba 3837
8d08fdba 3838 /* Type are looked up using the DECL_NAME, as that is what the rest of the
e92cc029 3839 compiler wants to use. */
a9aedbc2 3840 if (TREE_CODE (x) == TYPE_DECL || TREE_CODE (x) == VAR_DECL
0c20d3d6 3841 || TREE_CODE (x) == NAMESPACE_DECL)
8d08fdba 3842 name = DECL_NAME (x);
8d08fdba
MS
3843
3844 if (name)
3845 {
5566b478
MS
3846#if 0
3847 /* Not needed...see below. */
8d08fdba
MS
3848 char *file;
3849 int line;
5566b478 3850#endif
386b8a85
JM
3851 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
3852 name = TREE_OPERAND (name, 0);
3853
2c73f9f5 3854 /* Namespace-scoped variables are not found in the current level. */
6eb3bb27 3855 if (TREE_CODE (x) == VAR_DECL && DECL_NAMESPACE_SCOPE_P (x))
2c73f9f5
ML
3856 t = namespace_binding (name, DECL_CONTEXT (x));
3857 else
3858 t = lookup_name_current_level (name);
8d08fdba
MS
3859 if (t == error_mark_node)
3860 {
3861 /* error_mark_node is 0 for a while during initialization! */
3862 t = NULL_TREE;
8251199e 3863 cp_error_at ("`%#D' used prior to declaration", x);
8d08fdba
MS
3864 }
3865
51c184be 3866 else if (t != NULL_TREE)
8d08fdba 3867 {
5566b478
MS
3868#if 0
3869 /* This is turned off until I have time to do it right (bpk). */
e92cc029 3870 /* With the code below that uses it... */
8d6e462b
PB
3871 file = DECL_SOURCE_FILE (t);
3872 line = DECL_SOURCE_LINE (t);
5566b478 3873#endif
2ee887f2 3874 if (TREE_CODE (t) == PARM_DECL)
8d08fdba
MS
3875 {
3876 if (DECL_CONTEXT (t) == NULL_TREE)
3877 fatal ("parse errors have confused me too much");
be99da77 3878
e92cc029 3879 /* Check for duplicate params. */
be99da77
MS
3880 if (duplicate_decls (x, t))
3881 return t;
8d08fdba 3882 }
8d6e462b 3883 else if (((TREE_CODE (x) == FUNCTION_DECL && DECL_LANGUAGE (x) == lang_c)
5566b478
MS
3884 || DECL_FUNCTION_TEMPLATE_P (x))
3885 && is_overloaded_fn (t))
2c73f9f5 3886 /* Don't do anything just yet. */;
e1cd6e56
MS
3887 else if (t == wchar_decl_node)
3888 {
3889 if (pedantic && ! DECL_IN_SYSTEM_HEADER (x))
8251199e 3890 cp_pedwarn ("redeclaration of wchar_t as `%T'", TREE_TYPE (x));
e1cd6e56
MS
3891
3892 /* Throw away the redeclaration. */
3893 return t;
3894 }
8926095f 3895 else if (TREE_CODE (t) != TREE_CODE (x))
8d08fdba 3896 {
9ed182dc 3897 if (duplicate_decls (x, t))
51c184be 3898 return t;
8d08fdba
MS
3899 }
3900 else if (duplicate_decls (x, t))
51c184be 3901 {
8d08fdba 3902#if 0
8926095f 3903 /* This is turned off until I have time to do it right (bpk). */
8d08fdba 3904
8926095f
MS
3905 /* Also warn if they did a prototype with `static' on it, but
3906 then later left the `static' off. */
3907 if (! TREE_PUBLIC (name) && TREE_PUBLIC (x))
8d08fdba 3908 {
8926095f
MS
3909 if (DECL_LANG_SPECIFIC (t) && DECL_FRIEND_P (t))
3910 return t;
8d08fdba 3911
8926095f
MS
3912 if (extra_warnings)
3913 {
8251199e 3914 cp_warning ("`static' missing from declaration of `%D'",
a28e3c7f 3915 t);
8926095f
MS
3916 warning_with_file_and_line (file, line,
3917 "previous declaration of `%s'",
3918 decl_as_string (t, 0));
3919 }
8d08fdba 3920
8926095f
MS
3921 /* Now fix things so it'll do what they expect. */
3922 if (current_function_decl)
3923 TREE_PUBLIC (current_function_decl) = 0;
3924 }
51c184be
MS
3925 /* Due to interference in memory reclamation (X may be
3926 obstack-deallocated at this point), we must guard against
8926095f
MS
3927 one really special case. [jason: This should be handled
3928 by start_function] */
51c184be
MS
3929 if (current_function_decl == x)
3930 current_function_decl = t;
8926095f 3931#endif
7177d104
MS
3932 if (TREE_CODE (t) == TYPE_DECL)
3933 SET_IDENTIFIER_TYPE_VALUE (name, TREE_TYPE (t));
f30432d7
MS
3934 else if (TREE_CODE (t) == FUNCTION_DECL)
3935 check_default_args (t);
7177d104 3936
51c184be
MS
3937 return t;
3938 }
35680744
MM
3939 else if (DECL_MAIN_P (x))
3940 {
3941 /* A redeclaration of main, but not a duplicate of the
3942 previous one.
3943
3944 [basic.start.main]
3945
3946 This function shall not be overloaded. */
8251199e
JM
3947 cp_error_at ("invalid redeclaration of `%D'", t);
3948 cp_error ("as `%D'", x);
35680744
MM
3949 /* We don't try to push this declaration since that
3950 causes a crash. */
3951 return x;
3952 }
8d08fdba 3953 }
8926095f 3954
f3400fe2
JM
3955 check_template_shadow (x);
3956
fcfcdfc8
JM
3957 /* If this is a function conjured up by the backend, massage it
3958 so it looks friendly. */
3959 if (TREE_CODE (x) == FUNCTION_DECL
3960 && ! DECL_LANG_SPECIFIC (x))
3961 {
3962 retrofit_lang_decl (x);
3963 DECL_LANGUAGE (x) = lang_c;
3964 }
3965
8926095f
MS
3966 if (TREE_CODE (x) == FUNCTION_DECL && ! DECL_FUNCTION_MEMBER_P (x))
3967 {
7bdbfa05 3968 t = push_overloaded_decl (x, PUSH_LOCAL);
8926095f
MS
3969 if (t != x || DECL_LANGUAGE (x) == lang_c)
3970 return t;
f181d4ae
MM
3971 if (!namespace_bindings_p ())
3972 /* We do not need to create a binding for this name;
3973 push_overloaded_decl will have already done so if
3974 necessary. */
3975 need_new_binding = 0;
8926095f 3976 }
6eb3bb27 3977 else if (DECL_FUNCTION_TEMPLATE_P (x) && DECL_NAMESPACE_SCOPE_P (x))
cfe507be
MM
3978 {
3979 t = push_overloaded_decl (x, PUSH_GLOBAL);
3980 if (t == x)
3981 add_decl_to_level (x, NAMESPACE_LEVEL (CP_DECL_CONTEXT (t)));
3982 return t;
3983 }
8d08fdba 3984
a1774733
BK
3985 /* If declaring a type as a typedef, copy the type (unless we're
3986 at line 0), and install this TYPE_DECL as the new type's typedef
3987 name. See the extensive comment in ../c-decl.c (pushdecl). */
8d08fdba
MS
3988 if (TREE_CODE (x) == TYPE_DECL)
3989 {
3990 tree type = TREE_TYPE (x);
a1774733
BK
3991 if (DECL_SOURCE_LINE (x) == 0)
3992 {
3993 if (TYPE_NAME (type) == 0)
3994 TYPE_NAME (type) = x;
3995 }
1c80fb65
MM
3996 else if (type != error_mark_node && TYPE_NAME (type) != x
3997 /* We don't want to copy the type when all we're
3998 doing is making a TYPE_DECL for the purposes of
3999 inlining. */
4000 && (!TYPE_NAME (type)
4001 || TYPE_NAME (type) != DECL_ABSTRACT_ORIGIN (x)))
a1774733 4002 {
ae0a6181
MM
4003 push_obstacks (TYPE_OBSTACK (type), TYPE_OBSTACK (type));
4004
a1774733
BK
4005 DECL_ORIGINAL_TYPE (x) = type;
4006 type = build_type_copy (type);
4007 TYPE_STUB_DECL (type) = TYPE_STUB_DECL (DECL_ORIGINAL_TYPE (x));
4008 TYPE_NAME (type) = x;
4009 TREE_TYPE (x) = type;
ae0a6181
MM
4010
4011 pop_obstacks ();
a1774733 4012 }
8d08fdba 4013
8d08fdba
MS
4014 if (type != error_mark_node
4015 && TYPE_NAME (type)
4016 && TYPE_IDENTIFIER (type))
f181d4ae
MM
4017 set_identifier_type_value_with_scope (DECL_NAME (x), type,
4018 current_binding_level);
4019
8d08fdba
MS
4020 }
4021
4022 /* Multiple external decls of the same identifier ought to match.
4023
4024 We get warnings about inline functions where they are defined.
39211cd5
MS
4025 We get warnings about other functions from push_overloaded_decl.
4026
8d08fdba 4027 Avoid duplicate warnings where they are used. */
39211cd5 4028 if (TREE_PUBLIC (x) && TREE_CODE (x) != FUNCTION_DECL)
8d08fdba
MS
4029 {
4030 tree decl;
4031
31928556
JM
4032 if (IDENTIFIER_NAMESPACE_VALUE (name) != NULL_TREE
4033 && (DECL_EXTERNAL (IDENTIFIER_NAMESPACE_VALUE (name))
4034 || TREE_PUBLIC (IDENTIFIER_NAMESPACE_VALUE (name))))
4035 decl = IDENTIFIER_NAMESPACE_VALUE (name);
8d08fdba
MS
4036 else
4037 decl = NULL_TREE;
4038
39211cd5 4039 if (decl
8d08fdba
MS
4040 /* If different sort of thing, we already gave an error. */
4041 && TREE_CODE (decl) == TREE_CODE (x)
3bfdc719 4042 && !same_type_p (TREE_TYPE (x), TREE_TYPE (decl)))
8d08fdba 4043 {
8251199e
JM
4044 cp_pedwarn ("type mismatch with previous external decl", x);
4045 cp_pedwarn_at ("previous external decl of `%#D'", decl);
8d08fdba
MS
4046 }
4047 }
4048
8d08fdba
MS
4049 /* This name is new in its binding level.
4050 Install the new declaration and return it. */
2c73f9f5 4051 if (namespace_bindings_p ())
8d08fdba
MS
4052 {
4053 /* Install a global value. */
4054
8d08fdba
MS
4055 /* If the first global decl has external linkage,
4056 warn if we later see static one. */
31928556 4057 if (IDENTIFIER_GLOBAL_VALUE (name) == NULL_TREE && TREE_PUBLIC (x))
8d08fdba
MS
4058 TREE_PUBLIC (name) = 1;
4059
d8f8dca1
MM
4060 if (!(TREE_CODE (x) == TYPE_DECL && DECL_ARTIFICIAL (x)
4061 && t != NULL_TREE))
30394414
JM
4062 {
4063 if (TREE_CODE (x) == FUNCTION_DECL)
31928556
JM
4064 my_friendly_assert
4065 ((IDENTIFIER_GLOBAL_VALUE (name) == NULL_TREE)
4066 || (IDENTIFIER_GLOBAL_VALUE (name) == x), 378);
4067 SET_IDENTIFIER_NAMESPACE_VALUE (name, x);
30394414 4068 }
8d08fdba
MS
4069
4070 /* Don't forget if the function was used via an implicit decl. */
4071 if (IDENTIFIER_IMPLICIT_DECL (name)
4072 && TREE_USED (IDENTIFIER_IMPLICIT_DECL (name)))
4073 TREE_USED (x) = 1;
4074
4075 /* Don't forget if its address was taken in that way. */
4076 if (IDENTIFIER_IMPLICIT_DECL (name)
4077 && TREE_ADDRESSABLE (IDENTIFIER_IMPLICIT_DECL (name)))
4078 TREE_ADDRESSABLE (x) = 1;
4079
4080 /* Warn about mismatches against previous implicit decl. */
4081 if (IDENTIFIER_IMPLICIT_DECL (name) != NULL_TREE
4082 /* If this real decl matches the implicit, don't complain. */
4083 && ! (TREE_CODE (x) == FUNCTION_DECL
4084 && TREE_TYPE (TREE_TYPE (x)) == integer_type_node))
8251199e
JM
4085 cp_warning
4086 ("`%D' was previously implicitly declared to return `int'", x);
8d08fdba
MS
4087
4088 /* If new decl is `static' and an `extern' was seen previously,
4089 warn about it. */
a0a33927
MS
4090 if (x != NULL_TREE && t != NULL_TREE && decls_match (x, t))
4091 warn_extern_redeclared_static (x, t);
8d08fdba
MS
4092 }
4093 else
4094 {
4095 /* Here to install a non-global value. */
f181d4ae 4096 tree oldlocal = IDENTIFIER_VALUE (name);
31928556 4097 tree oldglobal = IDENTIFIER_NAMESPACE_VALUE (name);
8d08fdba 4098
f181d4ae
MM
4099 if (need_new_binding)
4100 {
0034cf72 4101 push_local_binding (name, x, 0);
f181d4ae
MM
4102 /* Because push_local_binding will hook X on to the
4103 current_binding_level's name list, we don't want to
4104 do that again below. */
4105 need_new_binding = 0;
4106 }
8d08fdba
MS
4107
4108 /* If this is a TYPE_DECL, push it into the type value slot. */
4109 if (TREE_CODE (x) == TYPE_DECL)
f181d4ae
MM
4110 set_identifier_type_value_with_scope (name, TREE_TYPE (x),
4111 current_binding_level);
8d08fdba 4112
a9aedbc2
MS
4113 /* Clear out any TYPE_DECL shadowed by a namespace so that
4114 we won't think this is a type. The C struct hack doesn't
4115 go through namespaces. */
4116 if (TREE_CODE (x) == NAMESPACE_DECL)
f181d4ae
MM
4117 set_identifier_type_value_with_scope (name, NULL_TREE,
4118 current_binding_level);
a9aedbc2 4119
8d08fdba
MS
4120 /* If this is an extern function declaration, see if we
4121 have a global definition or declaration for the function. */
4122 if (oldlocal == NULL_TREE
faae18ab 4123 && DECL_EXTERNAL (x)
31928556 4124 && oldglobal != NULL_TREE
8d08fdba 4125 && TREE_CODE (x) == FUNCTION_DECL
31928556 4126 && TREE_CODE (oldglobal) == FUNCTION_DECL)
8d08fdba
MS
4127 {
4128 /* We have one. Their types must agree. */
31928556 4129 if (decls_match (x, oldglobal))
6060a796
MS
4130 /* OK */;
4131 else
8d08fdba 4132 {
8251199e
JM
4133 cp_warning ("extern declaration of `%#D' doesn't match", x);
4134 cp_warning_at ("global declaration `%#D'", oldglobal);
8d08fdba 4135 }
8d08fdba
MS
4136 }
4137 /* If we have a local external declaration,
4138 and no file-scope declaration has yet been seen,
4139 then if we later have a file-scope decl it must not be static. */
4140 if (oldlocal == NULL_TREE
31928556 4141 && oldglobal == NULL_TREE
8d08fdba
MS
4142 && DECL_EXTERNAL (x)
4143 && TREE_PUBLIC (x))
f181d4ae 4144 TREE_PUBLIC (name) = 1;
8d08fdba
MS
4145
4146 if (DECL_FROM_INLINE (x))
4147 /* Inline decls shadow nothing. */;
4148
4149 /* Warn if shadowing an argument at the top level of the body. */
4150 else if (oldlocal != NULL_TREE && !DECL_EXTERNAL (x)
9d29e7af
JM
4151 && TREE_CODE (oldlocal) == PARM_DECL
4152 /* Don't complain if it's from an enclosing function. */
4153 && DECL_CONTEXT (oldlocal) == current_function_decl
4154 && TREE_CODE (x) != PARM_DECL)
8d08fdba
MS
4155 {
4156 /* Go to where the parms should be and see if we
4157 find them there. */
4158 struct binding_level *b = current_binding_level->level_chain;
4159
4160 if (cleanup_label)
4161 b = b->level_chain;
4162
4163 /* ARM $8.3 */
4164 if (b->parm_flag == 1)
8251199e 4165 cp_error ("declaration of `%#D' shadows a parameter", name);
8d08fdba 4166 }
f181d4ae
MM
4167 else if (warn_shadow && oldlocal != NULL_TREE
4168 && current_binding_level->is_for_scope
8d6e462b
PB
4169 && !DECL_DEAD_FOR_LOCAL (oldlocal))
4170 {
8251199e 4171 warning ("variable `%s' shadows local",
8d6e462b 4172 IDENTIFIER_POINTER (name));
8251199e 4173 cp_warning_at (" this is the shadowed declaration", oldlocal);
8d6e462b 4174 }
8d08fdba
MS
4175 /* Maybe warn if shadowing something else. */
4176 else if (warn_shadow && !DECL_EXTERNAL (x)
4177 /* No shadow warnings for internally generated vars. */
700f8a87 4178 && ! DECL_ARTIFICIAL (x)
8d08fdba
MS
4179 /* No shadow warnings for vars made for inlining. */
4180 && ! DECL_FROM_INLINE (x))
4181 {
d8e178a0 4182 const char *warnstring = NULL;
8d08fdba
MS
4183
4184 if (oldlocal != NULL_TREE && TREE_CODE (oldlocal) == PARM_DECL)
4185 warnstring = "declaration of `%s' shadows a parameter";
4186 else if (IDENTIFIER_CLASS_VALUE (name) != NULL_TREE
4ac14744 4187 && current_class_ptr
8d08fdba
MS
4188 && !TREE_STATIC (name))
4189 warnstring = "declaration of `%s' shadows a member of `this'";
4190 else if (oldlocal != NULL_TREE)
4191 warnstring = "declaration of `%s' shadows previous local";
31928556 4192 else if (oldglobal != NULL_TREE)
30394414 4193 /* XXX shadow warnings in outer-more namespaces */
8d08fdba
MS
4194 warnstring = "declaration of `%s' shadows global declaration";
4195
4196 if (warnstring)
4197 warning (warnstring, IDENTIFIER_POINTER (name));
4198 }
e1cd6e56 4199 }
8d08fdba 4200
e1cd6e56 4201 if (TREE_CODE (x) == FUNCTION_DECL)
f30432d7 4202 check_default_args (x);
8145f082
MS
4203
4204 /* Keep count of variables in this level with incomplete type. */
8145f082 4205 if (TREE_CODE (x) == VAR_DECL
28cbf42c 4206 && TREE_TYPE (x) != error_mark_node
f30432d7
MS
4207 && ((TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
4208 && PROMOTES_TO_AGGR_TYPE (TREE_TYPE (x), ARRAY_TYPE))
4209 /* RTTI TD entries are created while defining the type_info. */
4210 || (TYPE_LANG_SPECIFIC (TREE_TYPE (x))
4211 && TYPE_BEING_DEFINED (TREE_TYPE (x)))))
f181d4ae
MM
4212 current_binding_level->incomplete
4213 = tree_cons (NULL_TREE, x, current_binding_level->incomplete);
8d08fdba
MS
4214 }
4215
f181d4ae 4216 if (need_new_binding)
cfe507be 4217 add_decl_to_level (x, current_binding_level);
8d08fdba
MS
4218
4219 return x;
4220}
4221
5566b478
MS
4222/* Same as pushdecl, but define X in binding-level LEVEL. We rely on the
4223 caller to set DECL_CONTEXT properly. */
8d08fdba
MS
4224
4225static tree
4226pushdecl_with_scope (x, level)
4227 tree x;
4228 struct binding_level *level;
4229{
8d019cef 4230 register struct binding_level *b;
5566b478 4231 tree function_decl = current_function_decl;
8d08fdba 4232
5566b478 4233 current_function_decl = NULL_TREE;
8d019cef
JM
4234 if (level->parm_flag == 2)
4235 {
4236 b = class_binding_level;
4237 class_binding_level = level;
4238 pushdecl_class_level (x);
4239 class_binding_level = b;
4240 }
4241 else
4242 {
4243 b = current_binding_level;
4244 current_binding_level = level;
4245 x = pushdecl (x);
4246 current_binding_level = b;
4247 }
5566b478 4248 current_function_decl = function_decl;
8d08fdba
MS
4249 return x;
4250}
4251
2c73f9f5 4252/* Like pushdecl, only it places X in the current namespace,
8d08fdba 4253 if appropriate. */
e92cc029 4254
8d08fdba 4255tree
2c73f9f5 4256pushdecl_namespace_level (x)
8d08fdba
MS
4257 tree x;
4258{
8f032717 4259 register struct binding_level *b = current_binding_level;
2c73f9f5
ML
4260 register tree t;
4261
4262 t = pushdecl_with_scope (x, NAMESPACE_LEVEL (current_namespace));
8d08fdba
MS
4263
4264 /* Now, the type_shadowed stack may screw us. Munge it so it does
4265 what we want. */
4266 if (TREE_CODE (x) == TYPE_DECL)
4267 {
4268 tree name = DECL_NAME (x);
4269 tree newval;
4270 tree *ptr = (tree *)0;
4271 for (; b != global_binding_level; b = b->level_chain)
4272 {
4273 tree shadowed = b->type_shadowed;
4274 for (; shadowed; shadowed = TREE_CHAIN (shadowed))
4275 if (TREE_PURPOSE (shadowed) == name)
4276 {
4277 ptr = &TREE_VALUE (shadowed);
4278 /* Can't break out of the loop here because sometimes
4279 a binding level will have duplicate bindings for
4280 PT names. It's gross, but I haven't time to fix it. */
4281 }
4282 }
4283 newval = TREE_TYPE (x);
4284 if (ptr == (tree *)0)
4285 {
4286 /* @@ This shouldn't be needed. My test case "zstring.cc" trips
4287 up here if this is changed to an assertion. --KR */
4288 SET_IDENTIFIER_TYPE_VALUE (name, newval);
4289 }
4290 else
4291 {
8d08fdba
MS
4292 *ptr = newval;
4293 }
4294 }
4295 return t;
4296}
4297
2c73f9f5
ML
4298/* Like pushdecl, only it places X in GLOBAL_BINDING_LEVEL,
4299 if appropriate. */
4300
4301tree
4302pushdecl_top_level (x)
4303 tree x;
4304{
4305 tree cur_namespace = current_namespace;
4306 current_namespace = global_namespace;
4307 x = pushdecl_namespace_level (x);
4308 current_namespace = cur_namespace;
4309 return x;
4310}
4311
8d08fdba 4312/* Make the declaration of X appear in CLASS scope. */
e92cc029 4313
61a127b3 4314void
8d08fdba
MS
4315pushdecl_class_level (x)
4316 tree x;
4317{
4318 /* Don't use DECL_ASSEMBLER_NAME here! Everything that looks in class
4319 scope looks for the pre-mangled name. */
8f032717
MM
4320 register tree name;
4321
4322 if (TREE_CODE (x) == OVERLOAD)
4323 x = OVL_CURRENT (x);
4324 name = DECL_NAME (x);
8d08fdba
MS
4325
4326 if (name)
4327 {
4328 push_class_level_binding (name, x);
4329 if (TREE_CODE (x) == TYPE_DECL)
f181d4ae 4330 set_identifier_type_value (name, TREE_TYPE (x));
8d08fdba 4331 }
6bdb8141 4332 else if (ANON_AGGR_TYPE_P (TREE_TYPE (x)))
8f032717
MM
4333 {
4334 tree f;
4335
4336 for (f = TYPE_FIELDS (TREE_TYPE (x));
4337 f;
4338 f = TREE_CHAIN (f))
4339 pushdecl_class_level (f);
4340 }
8d08fdba
MS
4341}
4342
9188c363
MM
4343/* Enter DECL into the symbol table, if that's appropriate. Returns
4344 DECL, or a modified version thereof. */
4345
4346tree
4347maybe_push_decl (decl)
4348 tree decl;
4349{
4350 tree type = TREE_TYPE (decl);
4351
4352 /* Add this decl to the current binding level, but not if it comes
4353 from another scope, e.g. a static member variable. TEM may equal
4354 DECL or it may be a previous decl of the same name. */
4355 if ((TREE_CODE (decl) != PARM_DECL
4356 && DECL_CONTEXT (decl) != NULL_TREE
4357 /* Definitions of namespace members outside their namespace are
4358 possible. */
4359 && TREE_CODE (DECL_CONTEXT (decl)) != NAMESPACE_DECL)
4360 || (TREE_CODE (decl) == TEMPLATE_DECL && !namespace_bindings_p ())
4361 || TREE_CODE (type) == UNKNOWN_TYPE
558475f0 4362 /* The declaration of a template specialization does not affect
9188c363
MM
4363 the functions available for overload resolution, so we do not
4364 call pushdecl. */
4365 || (TREE_CODE (decl) == FUNCTION_DECL
4366 && DECL_TEMPLATE_SPECIALIZATION (decl)))
4367 return decl;
4368 else
4369 return pushdecl (decl);
4370}
4371
bd6dd845 4372#if 0
7177d104
MS
4373/* This function is used to push the mangled decls for nested types into
4374 the appropriate scope. Previously pushdecl_top_level was used, but that
4375 is incorrect for members of local classes. */
e92cc029 4376
5566b478 4377void
7177d104
MS
4378pushdecl_nonclass_level (x)
4379 tree x;
4380{
4381 struct binding_level *b = current_binding_level;
4382
7177d104 4383 my_friendly_assert (b->parm_flag != 2, 180);
7177d104 4384
5566b478 4385#if 0
7177d104
MS
4386 /* Get out of template binding levels */
4387 while (b->pseudo_global)
4388 b = b->level_chain;
5566b478 4389#endif
7177d104
MS
4390
4391 pushdecl_with_scope (x, b);
4392}
bd6dd845 4393#endif
7177d104 4394
8d08fdba
MS
4395/* Make the declaration(s) of X appear in CLASS scope
4396 under the name NAME. */
e92cc029 4397
8d08fdba
MS
4398void
4399push_class_level_binding (name, x)
4400 tree name;
4401 tree x;
4402{
8f032717 4403 tree binding;
98c1c668
JM
4404 /* The class_binding_level will be NULL if x is a template
4405 parameter name in a member template. */
4406 if (!class_binding_level)
4407 return;
4408
908c4e83
MM
4409 /* Make sure that this new member does not have the same name
4410 as a template parameter. */
4411 if (TYPE_BEING_DEFINED (current_class_type))
4412 check_template_shadow (x);
4413
f181d4ae
MM
4414 /* If this declaration shadows a declaration from an enclosing
4415 class, then we will need to restore IDENTIFIER_CLASS_VALUE when
4416 we leave this class. Record the shadowed declaration here. */
8f032717
MM
4417 binding = IDENTIFIER_BINDING (name);
4418 if (binding
4419 && ((TREE_CODE (x) == OVERLOAD
4420 && BINDING_VALUE (binding)
4421 && is_overloaded_fn (BINDING_VALUE (binding)))
4422 || INHERITED_VALUE_BINDING_P (binding)))
4423 {
4424 tree shadow;
4425 tree old_decl;
4426
4427 /* If the old binding was from a base class, and was for a tag
4428 name, slide it over to make room for the new binding. The
4429 old binding is still visible if explicitly qualified with a
4430 class-key. */
4431 if (INHERITED_VALUE_BINDING_P (binding)
4432 && BINDING_VALUE (binding)
4433 && TREE_CODE (BINDING_VALUE (binding)) == TYPE_DECL
4434 && DECL_ARTIFICIAL (BINDING_VALUE (binding))
4435 && !(TREE_CODE (x) == TYPE_DECL && DECL_ARTIFICIAL (x)))
4436 {
4437 old_decl = BINDING_TYPE (binding);
4438 BINDING_TYPE (binding) = BINDING_VALUE (binding);
4439 BINDING_VALUE (binding) = NULL_TREE;
4440 INHERITED_VALUE_BINDING_P (binding) = 0;
4441 }
4442 else
4443 old_decl = BINDING_VALUE (binding);
4444
4445 /* There was already a binding for X containing fewer
4446 functions than are named in X. Find the previous
4447 declaration of X on the class-shadowed list, and update it. */
4448 for (shadow = class_binding_level->class_shadowed;
4449 shadow;
4450 shadow = TREE_CHAIN (shadow))
4451 if (TREE_PURPOSE (shadow) == name
4452 && TREE_TYPE (shadow) == old_decl)
4453 {
4454 BINDING_VALUE (binding) = x;
4455 INHERITED_VALUE_BINDING_P (binding) = 0;
4456 TREE_TYPE (shadow) = x;
4457 return;
4458 }
4459 }
f181d4ae 4460
8f032717
MM
4461 /* If we didn't replace an existing binding, put the binding on the
4462 stack of bindings for the identifier, and update
4463 IDENTIFIER_CLASS_VALUE. */
4464 if (push_class_binding (name, x))
4465 {
3f1892e1 4466 push_cache_obstack ();
8f032717
MM
4467 class_binding_level->class_shadowed
4468 = tree_cons (name, IDENTIFIER_CLASS_VALUE (name),
4469 class_binding_level->class_shadowed);
4470 pop_obstacks ();
4471 /* Record the value we are binding NAME to so that we can know
4472 what to pop later. */
4473 TREE_TYPE (class_binding_level->class_shadowed) = x;
4474 }
8d08fdba
MS
4475}
4476
2c73f9f5
ML
4477/* Insert another USING_DECL into the current binding level,
4478 returning this declaration. If this is a redeclaration,
4479 do nothing and return NULL_TREE. */
e92cc029 4480
2c73f9f5
ML
4481tree
4482push_using_decl (scope, name)
4483 tree scope;
4484 tree name;
8d08fdba 4485{
2c73f9f5
ML
4486 tree decl;
4487
2c73f9f5
ML
4488 my_friendly_assert (TREE_CODE (scope) == NAMESPACE_DECL, 383);
4489 my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, 384);
4490 for (decl = current_binding_level->usings; decl; decl = TREE_CHAIN (decl))
4491 if (DECL_INITIAL (decl) == scope && DECL_NAME (decl) == name)
4492 break;
4493 if (decl)
4494 return NULL_TREE;
4ce3d537 4495 decl = build_lang_decl (USING_DECL, name, void_type_node);
2c73f9f5
ML
4496 DECL_INITIAL (decl) = scope;
4497 TREE_CHAIN (decl) = current_binding_level->usings;
4498 current_binding_level->usings = decl;
4499 return decl;
8d08fdba
MS
4500}
4501
ea9635c7
ML
4502/* Add namespace to using_directives. Return NULL_TREE if nothing was
4503 changed (i.e. there was already a directive), or the fresh
4504 TREE_LIST otherwise. */
4505
4506tree
9ed182dc 4507push_using_directive (used)
ea9635c7 4508 tree used;
ea9635c7
ML
4509{
4510 tree ud = current_binding_level->using_directives;
9ed182dc 4511 tree iter, ancestor;
ea9635c7
ML
4512
4513 /* Check if we already have this. */
4514 if (purpose_member (used, ud) != NULL_TREE)
4515 return NULL_TREE;
00dc6358
JM
4516
4517 /* Recursively add all namespaces used. */
4518 for (iter = DECL_NAMESPACE_USING (used); iter; iter = TREE_CHAIN (iter))
9ed182dc 4519 push_using_directive (TREE_PURPOSE (iter));
00dc6358 4520
9ed182dc 4521 ancestor = namespace_ancestor (current_decl_namespace (), used);
00dc6358 4522 ud = current_binding_level->using_directives;
ea9635c7
ML
4523 ud = perm_tree_cons (used, ancestor, ud);
4524 current_binding_level->using_directives = ud;
4525 return ud;
4526}
4527
f181d4ae
MM
4528/* DECL is a FUNCTION_DECL for a non-member function, which may have
4529 other definitions already in place. We get around this by making
4530 the value of the identifier point to a list of all the things that
4531 want to be referenced by that name. It is then up to the users of
4532 that name to decide what to do with that list.
8d08fdba
MS
4533
4534 DECL may also be a TEMPLATE_DECL, with a FUNCTION_DECL in its DECL_RESULT
4535 slot. It is dealt with the same way.
4536
7bdbfa05
MM
4537 FLAGS is a bitwise-or of the following values:
4538 PUSH_LOCAL: Bind DECL in the current scope, rather than at
4539 namespace scope.
4540 PUSH_USING: DECL is being pushed as the result of a using
4541 declaration.
4542
8d08fdba
MS
4543 The value returned may be a previous declaration if we guessed wrong
4544 about what language DECL should belong to (C or C++). Otherwise,
4545 it's always DECL (and never something that's not a _DECL). */
e92cc029 4546
7bdbfa05
MM
4547tree
4548push_overloaded_decl (decl, flags)
8d08fdba 4549 tree decl;
7bdbfa05 4550 int flags;
8d08fdba 4551{
f181d4ae 4552 tree name = DECL_NAME (decl);
700f8a87 4553 tree old;
f181d4ae 4554 tree new_binding;
7bdbfa05 4555 int doing_global = (namespace_bindings_p () || !(flags & PUSH_LOCAL));
700f8a87
MS
4556
4557 if (doing_global)
9f33663b 4558 old = namespace_binding (name, DECL_CONTEXT (decl));
700f8a87 4559 else
f181d4ae 4560 old = lookup_name_current_level (name);
8d08fdba 4561
700f8a87 4562 if (old)
8d08fdba 4563 {
e1cd6e56 4564 if (TREE_CODE (old) == TYPE_DECL && DECL_ARTIFICIAL (old))
8926095f 4565 {
700f8a87 4566 tree t = TREE_TYPE (old);
cdf5b885
MS
4567 if (IS_AGGR_TYPE (t) && warn_shadow
4568 && (! DECL_IN_SYSTEM_HEADER (decl)
4569 || ! DECL_IN_SYSTEM_HEADER (old)))
8251199e 4570 cp_warning ("`%#D' hides constructor for `%#T'", decl, t);
5b605f68 4571 old = NULL_TREE;
8926095f 4572 }
700f8a87 4573 else if (is_overloaded_fn (old))
8d08fdba 4574 {
8d08fdba
MS
4575 tree tmp;
4576
2c73f9f5 4577 for (tmp = old; tmp; tmp = OVL_NEXT (tmp))
7bdbfa05
MM
4578 {
4579 tree fn = OVL_CURRENT (tmp);
4580
4581 if (TREE_CODE (tmp) == OVERLOAD && OVL_USED (tmp)
4582 && !(flags & PUSH_USING)
4583 && compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
4584 TYPE_ARG_TYPES (TREE_TYPE (decl))))
4585 cp_error ("`%#D' conflicts with previous using declaration `%#D'",
4586 decl, fn);
4587
4588 if (duplicate_decls (decl, fn))
4589 return fn;
4590 }
8d08fdba 4591 }
e1cd6e56
MS
4592 else
4593 {
8251199e
JM
4594 cp_error_at ("previous non-function declaration `%#D'", old);
4595 cp_error ("conflicts with function declaration `%#D'", decl);
a9aedbc2 4596 return decl;
e1cd6e56 4597 }
8d08fdba 4598 }
7177d104 4599
700f8a87 4600 if (old || TREE_CODE (decl) == TEMPLATE_DECL)
8d08fdba 4601 {
2c73f9f5 4602 if (old && TREE_CODE (old) != OVERLOAD)
f181d4ae
MM
4603 new_binding = ovl_cons (decl, ovl_cons (old, NULL_TREE));
4604 else
4605 new_binding = ovl_cons (decl, old);
347d73d7
ML
4606 if (flags & PUSH_USING)
4607 OVL_USED (new_binding) = 1;
8d08fdba
MS
4608 }
4609 else
f181d4ae
MM
4610 /* NAME is not ambiguous. */
4611 new_binding = decl;
700f8a87
MS
4612
4613 if (doing_global)
f181d4ae 4614 set_namespace_binding (name, current_namespace, new_binding);
700f8a87 4615 else
f181d4ae
MM
4616 {
4617 /* We only create an OVERLOAD if there was a previous binding at
0580c9aa
ML
4618 this level, or if decl is a template. In the former case, we
4619 need to remove the old binding and replace it with the new
4620 binding. We must also run through the NAMES on the binding
4621 level where the name was bound to update the chain. */
4622
4623 if (TREE_CODE (new_binding) == OVERLOAD && old)
f181d4ae
MM
4624 {
4625 tree *d;
4626
4627 for (d = &BINDING_LEVEL (IDENTIFIER_BINDING (name))->names;
4628 *d;
4629 d = &TREE_CHAIN (*d))
4630 if (*d == old
4631 || (TREE_CODE (*d) == TREE_LIST
4632 && TREE_VALUE (*d) == old))
4633 {
d8f8dca1
MM
4634 if (TREE_CODE (*d) == TREE_LIST)
4635 /* Just replace the old binding with the new. */
4636 TREE_VALUE (*d) = new_binding;
4637 else
4638 /* Build a TREE_LIST to wrap the OVERLOAD. */
4639 *d = build_tree_list (NULL_TREE, new_binding);
4640
4641 /* And update the CPLUS_BINDING node. */
4642 BINDING_VALUE (IDENTIFIER_BINDING (name))
4643 = new_binding;
4644 return decl;
f181d4ae
MM
4645 }
4646
d8f8dca1
MM
4647 /* We should always find a previous binding in this case. */
4648 my_friendly_abort (0);
f181d4ae
MM
4649 }
4650
4651 /* Install the new binding. */
0034cf72 4652 push_local_binding (name, new_binding, flags);
f181d4ae 4653 }
700f8a87 4654
8d08fdba
MS
4655 return decl;
4656}
4657\f
4658/* Generate an implicit declaration for identifier FUNCTIONID
4659 as a function of type int (). Print a warning if appropriate. */
4660
4661tree
4662implicitly_declare (functionid)
4663 tree functionid;
4664{
4665 register tree decl;
4666 int temp = allocation_temporary_p ();
4667
4668 push_obstacks_nochange ();
4669
4670 /* Save the decl permanently so we can warn if definition follows.
4671 In ANSI C, warn_implicit is usually false, so the saves little space.
4672 But in C++, it's usually true, hence the extra code. */
d22c8596 4673 if (temp && (! warn_implicit || toplevel_bindings_p ()))
8d08fdba
MS
4674 end_temporary_allocation ();
4675
4676 /* We used to reuse an old implicit decl here,
4677 but this loses with inline functions because it can clobber
4678 the saved decl chains. */
4679 decl = build_lang_decl (FUNCTION_DECL, functionid, default_function_type);
4680
4681 DECL_EXTERNAL (decl) = 1;
4682 TREE_PUBLIC (decl) = 1;
4683
4684 /* ANSI standard says implicit declarations are in the innermost block.
d22c8596 4685 So we record the decl in the standard fashion. */
8d08fdba
MS
4686 pushdecl (decl);
4687 rest_of_decl_compilation (decl, NULL_PTR, 0, 0);
4688
4689 if (warn_implicit
4690 /* Only one warning per identifier. */
4691 && IDENTIFIER_IMPLICIT_DECL (functionid) == NULL_TREE)
4692 {
8251199e 4693 cp_pedwarn ("implicit declaration of function `%#D'", decl);
8d08fdba
MS
4694 }
4695
4696 SET_IDENTIFIER_IMPLICIT_DECL (functionid, decl);
4697
4698 pop_obstacks ();
4699
4700 return decl;
4701}
4702
8251199e 4703/* Return zero if the declaration NEWDECL is valid
8d08fdba
MS
4704 when the declaration OLDDECL (assumed to be for the same name)
4705 has already been seen.
4706 Otherwise return an error message format string with a %s
4707 where the identifier should go. */
4708
d8e178a0 4709static const char *
8d08fdba
MS
4710redeclaration_error_message (newdecl, olddecl)
4711 tree newdecl, olddecl;
4712{
4713 if (TREE_CODE (newdecl) == TYPE_DECL)
4714 {
4715 /* Because C++ can put things into name space for free,
4716 constructs like "typedef struct foo { ... } foo"
4717 would look like an erroneous redeclaration. */
3bfdc719 4718 if (same_type_p (TREE_TYPE (newdecl), TREE_TYPE (olddecl)))
8251199e 4719 return 0;
8d08fdba 4720 else
8251199e 4721 return "redefinition of `%#D'";
8d08fdba
MS
4722 }
4723 else if (TREE_CODE (newdecl) == FUNCTION_DECL)
4724 {
4725 /* If this is a pure function, its olddecl will actually be
4726 the original initialization to `0' (which we force to call
4727 abort()). Don't complain about redefinition in this case. */
4728 if (DECL_LANG_SPECIFIC (olddecl) && DECL_ABSTRACT_VIRTUAL_P (olddecl))
8251199e 4729 return 0;
8d08fdba 4730
2c73f9f5
ML
4731 /* If both functions come from different namespaces, this is not
4732 a redeclaration - this is a conflict with a used function. */
6eb3bb27 4733 if (DECL_NAMESPACE_SCOPE_P (olddecl)
2c73f9f5 4734 && DECL_CONTEXT (olddecl) != DECL_CONTEXT (newdecl))
8251199e 4735 return "`%D' conflicts with used function";
2c73f9f5 4736
db5ae43f
MS
4737 /* We'll complain about linkage mismatches in
4738 warn_extern_redeclared_static. */
4739
2c73f9f5 4740 /* Defining the same name twice is no good. */
8d08fdba 4741 if (DECL_INITIAL (olddecl) != NULL_TREE
faae18ab 4742 && DECL_INITIAL (newdecl) != NULL_TREE)
8d08fdba
MS
4743 {
4744 if (DECL_NAME (olddecl) == NULL_TREE)
8251199e 4745 return "`%#D' not declared in class";
8d08fdba 4746 else
8251199e 4747 return "redefinition of `%#D'";
8d08fdba 4748 }
8251199e 4749 return 0;
8d08fdba 4750 }
51c184be
MS
4751 else if (TREE_CODE (newdecl) == TEMPLATE_DECL)
4752 {
ec255269
MS
4753 if ((TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == FUNCTION_DECL
4754 && DECL_INITIAL (DECL_TEMPLATE_RESULT (newdecl))
4755 && DECL_INITIAL (DECL_TEMPLATE_RESULT (olddecl)))
4756 || (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL
4757 && TYPE_SIZE (TREE_TYPE (newdecl))
4758 && TYPE_SIZE (TREE_TYPE (olddecl))))
8251199e
JM
4759 return "redefinition of `%#D'";
4760 return 0;
51c184be 4761 }
5566b478 4762 else if (toplevel_bindings_p ())
8d08fdba
MS
4763 {
4764 /* Objects declared at top level: */
4765 /* If at least one is a reference, it's ok. */
4766 if (DECL_EXTERNAL (newdecl) || DECL_EXTERNAL (olddecl))
8251199e 4767 return 0;
8926095f 4768 /* Reject two definitions. */
8251199e 4769 return "redefinition of `%#D'";
8d08fdba
MS
4770 }
4771 else
4772 {
4773 /* Objects declared with block scope: */
4774 /* Reject two definitions, and reject a definition
4775 together with an external reference. */
4776 if (!(DECL_EXTERNAL (newdecl) && DECL_EXTERNAL (olddecl)))
8251199e
JM
4777 return "redeclaration of `%#D'";
4778 return 0;
8d08fdba
MS
4779 }
4780}
4781\f
4782/* Get the LABEL_DECL corresponding to identifier ID as a label.
4783 Create one if none exists so far for the current function.
4784 This function is called for both label definitions and label references. */
4785
4786tree
4787lookup_label (id)
4788 tree id;
4789{
4790 register tree decl = IDENTIFIER_LABEL_VALUE (id);
4791
4792 if (current_function_decl == NULL_TREE)
4793 {
8251199e 4794 error ("label `%s' referenced outside of any function",
8d08fdba
MS
4795 IDENTIFIER_POINTER (id));
4796 return NULL_TREE;
4797 }
4798
4799 if ((decl == NULL_TREE
4800 || DECL_SOURCE_LINE (decl) == 0)
e349ee73
MS
4801 && (named_label_uses == NULL
4802 || named_label_uses->names_in_scope != current_binding_level->names
4803 || named_label_uses->label_decl != decl))
4804 {
4805 struct named_label_list *new_ent;
4806 new_ent
4807 = (struct named_label_list*)oballoc (sizeof (struct named_label_list));
4808 new_ent->label_decl = decl;
4809 new_ent->names_in_scope = current_binding_level->names;
4810 new_ent->binding_level = current_binding_level;
4811 new_ent->lineno_o_goto = lineno;
4812 new_ent->filename_o_goto = input_filename;
4813 new_ent->next = named_label_uses;
4814 named_label_uses = new_ent;
8d08fdba
MS
4815 }
4816
4817 /* Use a label already defined or ref'd with this name. */
4818 if (decl != NULL_TREE)
4819 {
4820 /* But not if it is inherited and wasn't declared to be inheritable. */
4821 if (DECL_CONTEXT (decl) != current_function_decl
4822 && ! C_DECLARED_LABEL_FLAG (decl))
4823 return shadow_label (id);
4824 return decl;
4825 }
4826
4827 decl = build_decl (LABEL_DECL, id, void_type_node);
4828
cffa8729
MS
4829 /* Make sure every label has an rtx. */
4830 label_rtx (decl);
4831
8d08fdba
MS
4832 /* A label not explicitly declared must be local to where it's ref'd. */
4833 DECL_CONTEXT (decl) = current_function_decl;
4834
4835 DECL_MODE (decl) = VOIDmode;
4836
4837 /* Say where one reference is to the label,
4838 for the sake of the error if it is not defined. */
4839 DECL_SOURCE_LINE (decl) = lineno;
4840 DECL_SOURCE_FILE (decl) = input_filename;
4841
4842 SET_IDENTIFIER_LABEL_VALUE (id, decl);
4843
4844 named_labels = tree_cons (NULL_TREE, decl, named_labels);
e349ee73 4845 named_label_uses->label_decl = decl;
8d08fdba
MS
4846
4847 return decl;
4848}
4849
4850/* Make a label named NAME in the current function,
4851 shadowing silently any that may be inherited from containing functions
4852 or containing scopes.
4853
4854 Note that valid use, if the label being shadowed
4855 comes from another scope in the same function,
4856 requires calling declare_nonlocal_label right away. */
4857
4858tree
4859shadow_label (name)
4860 tree name;
4861{
4862 register tree decl = IDENTIFIER_LABEL_VALUE (name);
4863
4864 if (decl != NULL_TREE)
4865 {
4866 shadowed_labels = tree_cons (NULL_TREE, decl, shadowed_labels);
4867 SET_IDENTIFIER_LABEL_VALUE (name, NULL_TREE);
8d08fdba
MS
4868 }
4869
4870 return lookup_label (name);
4871}
4872
4873/* Define a label, specifying the location in the source file.
4874 Return the LABEL_DECL node for the label, if the definition is valid.
4875 Otherwise return 0. */
4876
4877tree
4878define_label (filename, line, name)
4879 char *filename;
4880 int line;
4881 tree name;
4882{
f01b0acb 4883 tree decl = lookup_label (name);
8d08fdba
MS
4884
4885 /* After labels, make any new cleanups go into their
4886 own new (temporary) binding contour. */
4887 current_binding_level->more_cleanups_ok = 0;
4888
4889 /* If label with this name is known from an outer context, shadow it. */
4890 if (decl != NULL_TREE && DECL_CONTEXT (decl) != current_function_decl)
4891 {
4892 shadowed_labels = tree_cons (NULL_TREE, decl, shadowed_labels);
4893 SET_IDENTIFIER_LABEL_VALUE (name, NULL_TREE);
4894 decl = lookup_label (name);
4895 }
4896
e1cd6e56 4897 if (name == get_identifier ("wchar_t"))
8251199e 4898 cp_pedwarn ("label named wchar_t");
e1cd6e56 4899
8d08fdba
MS
4900 if (DECL_INITIAL (decl) != NULL_TREE)
4901 {
8251199e 4902 cp_error ("duplicate label `%D'", decl);
8d08fdba
MS
4903 return 0;
4904 }
4905 else
4906 {
e349ee73 4907 struct named_label_list *uses, *prev;
28cbf42c 4908 int identified = 0;
227cf171 4909 int saw_eh = 0;
8d08fdba
MS
4910
4911 /* Mark label as having been defined. */
4912 DECL_INITIAL (decl) = error_mark_node;
4913 /* Say where in the source. */
4914 DECL_SOURCE_FILE (decl) = filename;
4915 DECL_SOURCE_LINE (decl) = line;
4916
e349ee73
MS
4917 prev = NULL;
4918 uses = named_label_uses;
4919 while (uses != NULL)
4920 if (uses->label_decl == decl)
8d08fdba
MS
4921 {
4922 struct binding_level *b = current_binding_level;
4923 while (b)
4924 {
4925 tree new_decls = b->names;
e349ee73
MS
4926 tree old_decls = (b == uses->binding_level)
4927 ? uses->names_in_scope : NULL_TREE;
8d08fdba
MS
4928 while (new_decls != old_decls)
4929 {
4930 if (TREE_CODE (new_decls) == VAR_DECL
4931 /* Don't complain about crossing initialization
4932 of internal entities. They can't be accessed,
4933 and they should be cleaned up
4934 by the time we get to the label. */
700f8a87 4935 && ! DECL_ARTIFICIAL (new_decls)
5524676d
JM
4936 && !(DECL_INITIAL (new_decls) == NULL_TREE
4937 && pod_type_p (TREE_TYPE (new_decls))))
8d08fdba 4938 {
b607c87f
JM
4939 /* This is really only important if we're crossing
4940 an initialization. The POD stuff is just
4941 pedantry; why should it matter if the class
4942 contains a field of pointer to member type? */
4943 int problem = (DECL_INITIAL (new_decls)
4944 || (TYPE_NEEDS_CONSTRUCTING
4945 (TREE_TYPE (new_decls))));
4946
4947 if (! identified)
e349ee73 4948 {
b607c87f
JM
4949 if (problem)
4950 {
4951 cp_error ("jump to label `%D'", decl);
4952 error_with_file_and_line
4953 (uses->filename_o_goto,
4954 uses->lineno_o_goto, " from here");
4955 }
4956 else
4957 {
4958 cp_pedwarn ("jump to label `%D'", decl);
4959 pedwarn_with_file_and_line
4960 (uses->filename_o_goto,
4961 uses->lineno_o_goto, " from here");
4962 }
e349ee73 4963 identified = 1;
227cf171 4964 }
b607c87f
JM
4965
4966 if (problem)
5524676d
JM
4967 cp_error_at (" crosses initialization of `%#D'",
4968 new_decls);
4969 else
b607c87f 4970 cp_pedwarn_at (" enters scope of non-POD `%#D'",
5524676d 4971 new_decls);
8d08fdba
MS
4972 }
4973 new_decls = TREE_CHAIN (new_decls);
4974 }
e349ee73 4975 if (b == uses->binding_level)
8d08fdba 4976 break;
227cf171
JM
4977 if (b->eh_region && ! saw_eh)
4978 {
4979 if (! identified)
4980 {
4981 cp_error ("jump to label `%D'", decl);
4982 error_with_file_and_line
4983 (uses->filename_o_goto,
4984 uses->lineno_o_goto, " from here");
4985 identified = 1;
4986 }
4987 error (" enters exception handling block");
4988 saw_eh = 1;
4989 }
8d08fdba
MS
4990 b = b->level_chain;
4991 }
4992
e349ee73
MS
4993 if (prev != NULL)
4994 prev->next = uses->next;
8d08fdba 4995 else
e349ee73
MS
4996 named_label_uses = uses->next;
4997
4998 uses = uses->next;
4999 }
5000 else
5001 {
5002 prev = uses;
5003 uses = uses->next;
8d08fdba
MS
5004 }
5005 current_function_return_value = NULL_TREE;
5006 return decl;
5007 }
5008}
5009
a5894242
MS
5010struct cp_switch
5011{
5012 struct binding_level *level;
5013 struct cp_switch *next;
5014};
5015
5016static struct cp_switch *switch_stack;
5017
5018void
5019push_switch ()
5020{
5021 struct cp_switch *p
5022 = (struct cp_switch *) oballoc (sizeof (struct cp_switch));
5023 p->level = current_binding_level;
5024 p->next = switch_stack;
5025 switch_stack = p;
5026}
5027
5028void
5029pop_switch ()
5030{
5031 switch_stack = switch_stack->next;
5032}
5033
8d08fdba
MS
5034/* Same, but for CASE labels. If DECL is NULL_TREE, it's the default. */
5035/* XXX Note decl is never actually used. (bpk) */
e92cc029 5036
8d08fdba 5037void
b370501f 5038define_case_label ()
8d08fdba
MS
5039{
5040 tree cleanup = last_cleanup_this_contour ();
a5894242
MS
5041 struct binding_level *b = current_binding_level;
5042 int identified = 0;
5043
8d08fdba
MS
5044 if (cleanup)
5045 {
5046 static int explained = 0;
8251199e
JM
5047 cp_warning_at ("destructor needed for `%#D'", TREE_PURPOSE (cleanup));
5048 warning ("where case label appears here");
8d08fdba
MS
5049 if (!explained)
5050 {
8251199e
JM
5051 warning ("(enclose actions of previous case statements requiring");
5052 warning ("destructors in their own binding contours.)");
8d08fdba
MS
5053 explained = 1;
5054 }
5055 }
5056
a5894242
MS
5057 for (; b && b != switch_stack->level; b = b->level_chain)
5058 {
5059 tree new_decls = b->names;
5060 for (; new_decls; new_decls = TREE_CHAIN (new_decls))
5061 {
5062 if (TREE_CODE (new_decls) == VAR_DECL
5063 /* Don't complain about crossing initialization
5064 of internal entities. They can't be accessed,
5065 and they should be cleaned up
5066 by the time we get to the label. */
5067 && ! DECL_ARTIFICIAL (new_decls)
5068 && ((DECL_INITIAL (new_decls) != NULL_TREE
5069 && DECL_INITIAL (new_decls) != error_mark_node)
5070 || TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (new_decls))))
5071 {
5072 if (! identified)
8251199e 5073 error ("jump to case label");
a5894242 5074 identified = 1;
8251199e 5075 cp_error_at (" crosses initialization of `%#D'",
a5894242
MS
5076 new_decls);
5077 }
5078 }
5079 }
5080
8d08fdba
MS
5081 /* After labels, make any new cleanups go into their
5082 own new (temporary) binding contour. */
5083
5084 current_binding_level->more_cleanups_ok = 0;
5085 current_function_return_value = NULL_TREE;
5086}
5087\f
5088/* Return the list of declarations of the current level.
5089 Note that this list is in reverse order unless/until
5090 you nreverse it; and when you do nreverse it, you must
5091 store the result back using `storedecls' or you will lose. */
5092
5093tree
5094getdecls ()
5095{
5096 return current_binding_level->names;
5097}
5098
5099/* Return the list of type-tags (for structs, etc) of the current level. */
5100
5101tree
5102gettags ()
5103{
5104 return current_binding_level->tags;
5105}
5106
5107/* Store the list of declarations of the current level.
5108 This is done for the parameter declarations of a function being defined,
5109 after they are modified in the light of any missing parameters. */
5110
5111static void
5112storedecls (decls)
5113 tree decls;
5114{
5115 current_binding_level->names = decls;
5116}
5117
5118/* Similarly, store the list of tags of the current level. */
5119
280f9385 5120void
8d08fdba
MS
5121storetags (tags)
5122 tree tags;
5123{
5124 current_binding_level->tags = tags;
5125}
5126\f
5127/* Given NAME, an IDENTIFIER_NODE,
5128 return the structure (or union or enum) definition for that name.
5129 Searches binding levels from BINDING_LEVEL up to the global level.
5130 If THISLEVEL_ONLY is nonzero, searches only the specified context
5131 (but skips any tag-transparent contexts to find one that is
5132 meaningful for tags).
5133 FORM says which kind of type the caller wants;
5134 it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
5135 If the wrong kind of type is found, and it's not a template, an error is
5136 reported. */
5137
5138static tree
5139lookup_tag (form, name, binding_level, thislevel_only)
5140 enum tree_code form;
8d08fdba 5141 tree name;
cffa8729 5142 struct binding_level *binding_level;
8d08fdba
MS
5143 int thislevel_only;
5144{
5145 register struct binding_level *level;
36a117a5
MM
5146 /* Non-zero if, we should look past a pseudo-global level, even if
5147 THISLEVEL_ONLY. */
5148 int allow_pseudo_global = 1;
8d08fdba
MS
5149
5150 for (level = binding_level; level; level = level->level_chain)
5151 {
5152 register tree tail;
5153 if (ANON_AGGRNAME_P (name))
5154 for (tail = level->tags; tail; tail = TREE_CHAIN (tail))
5155 {
5156 /* There's no need for error checking here, because
5157 anon names are unique throughout the compilation. */
5158 if (TYPE_IDENTIFIER (TREE_VALUE (tail)) == name)
5159 return TREE_VALUE (tail);
5160 }
2c73f9f5
ML
5161 else if (level->namespace_p)
5162 /* Do namespace lookup. */
6c011b01 5163 for (tail = current_namespace; 1; tail = CP_DECL_CONTEXT (tail))
2c73f9f5 5164 {
36a117a5
MM
5165 tree old = binding_for_name (name, tail);
5166
5167 /* If we just skipped past a pseudo global level, even
5168 though THISLEVEL_ONLY, and we find a template class
5169 declaration, then we use the _TYPE node for the
5170 template. See the example below. */
5171 if (thislevel_only && !allow_pseudo_global
5172 && old && BINDING_VALUE (old)
5173 && DECL_CLASS_TEMPLATE_P (BINDING_VALUE (old)))
5174 old = TREE_TYPE (BINDING_VALUE (old));
5175 else
5176 old = BINDING_TYPE (old);
5177
2c73f9f5
ML
5178 /* If it has an original type, it is a typedef, and we
5179 should not return it. */
5180 if (old && DECL_ORIGINAL_TYPE (TYPE_NAME (old)))
5181 old = NULL_TREE;
5182 if (old && TREE_CODE (old) != form
5183 && !(form != ENUMERAL_TYPE && TREE_CODE (old) == TEMPLATE_DECL))
5184 {
8251199e 5185 cp_error ("`%#D' redeclared as %C", old, form);
2c73f9f5
ML
5186 return NULL_TREE;
5187 }
5188 if (old)
5189 return old;
5190 if (thislevel_only || tail == global_namespace)
5191 return NULL_TREE;
5192 }
8d08fdba
MS
5193 else
5194 for (tail = level->tags; tail; tail = TREE_CHAIN (tail))
5195 {
a80e4195 5196 if (TREE_PURPOSE (tail) == name)
8d08fdba
MS
5197 {
5198 enum tree_code code = TREE_CODE (TREE_VALUE (tail));
5199 /* Should tighten this up; it'll probably permit
5200 UNION_TYPE and a struct template, for example. */
5201 if (code != form
5566b478 5202 && !(form != ENUMERAL_TYPE && code == TEMPLATE_DECL))
8d08fdba
MS
5203 {
5204 /* Definition isn't the kind we were looking for. */
8251199e 5205 cp_error ("`%#D' redeclared as %C", TREE_VALUE (tail),
8d08fdba 5206 form);
72b7eeff 5207 return NULL_TREE;
8d08fdba
MS
5208 }
5209 return TREE_VALUE (tail);
5210 }
5211 }
5212 if (thislevel_only && ! level->tag_transparent)
5566b478 5213 {
36a117a5 5214 if (level->pseudo_global && allow_pseudo_global)
5566b478 5215 {
36a117a5
MM
5216 /* We must deal with cases like this:
5217
5218 template <class T> struct S;
5219 template <class T> struct S {};
5220
5221 When looking up `S', for the second declaration, we
5222 would like to find the first declaration. But, we
5223 are in the pseudo-global level created for the
5224 template parameters, rather than the (surrounding)
5225 namespace level. Thus, we keep going one more level,
5226 even though THISLEVEL_ONLY is non-zero. */
5227 allow_pseudo_global = 0;
5228 continue;
5566b478 5229 }
36a117a5
MM
5230 else
5231 return NULL_TREE;
5566b478 5232 }
8d08fdba
MS
5233 }
5234 return NULL_TREE;
5235}
5236
bd6dd845 5237#if 0
8d08fdba
MS
5238void
5239set_current_level_tags_transparency (tags_transparent)
5240 int tags_transparent;
5241{
5242 current_binding_level->tag_transparent = tags_transparent;
5243}
bd6dd845 5244#endif
8d08fdba
MS
5245
5246/* Given a type, find the tag that was defined for it and return the tag name.
5247 Otherwise return 0. However, the value can never be 0
5248 in the cases in which this is used.
5249
5250 C++: If NAME is non-zero, this is the new name to install. This is
5251 done when replacing anonymous tags with real tag names. */
5252
5253static tree
5254lookup_tag_reverse (type, name)
5255 tree type;
5256 tree name;
5257{
5258 register struct binding_level *level;
5259
5260 for (level = current_binding_level; level; level = level->level_chain)
5261 {
5262 register tree tail;
5263 for (tail = level->tags; tail; tail = TREE_CHAIN (tail))
5264 {
5265 if (TREE_VALUE (tail) == type)
5266 {
5267 if (name)
5268 TREE_PURPOSE (tail) = name;
5269 return TREE_PURPOSE (tail);
5270 }
5271 }
5272 }
5273 return NULL_TREE;
5274}
8d08fdba 5275\f
a9aedbc2 5276/* Look up NAME in the NAMESPACE. */
e92cc029 5277
a9aedbc2
MS
5278tree
5279lookup_namespace_name (namespace, name)
5280 tree namespace, name;
5281{
2c73f9f5 5282 struct tree_binding _b;
30394414 5283 tree val;
f30c84c9 5284 tree template_id = NULL_TREE;
2c73f9f5 5285
30394414 5286 my_friendly_assert (TREE_CODE (namespace) == NAMESPACE_DECL, 370);
1231fb96 5287
1231fb96 5288 if (TREE_CODE (name) == NAMESPACE_DECL)
d8f8dca1 5289 /* This happens for A::B<int> when B is a namespace. */
1231fb96 5290 return name;
d8f8dca1
MM
5291 else if (TREE_CODE (name) == TEMPLATE_DECL)
5292 {
5293 /* This happens for A::B where B is a template, and there are no
5294 template arguments. */
5295 cp_error ("invalid use of `%D'", name);
5296 return error_mark_node;
5297 }
1231fb96 5298
b262d64c
JM
5299 namespace = ORIGINAL_NAMESPACE (namespace);
5300
f30c84c9
MM
5301 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
5302 {
5303 template_id = name;
5304 name = TREE_OPERAND (name, 0);
5305 if (TREE_CODE (name) == OVERLOAD)
5306 name = DECL_NAME (OVL_CURRENT (name));
5307 else if (TREE_CODE_CLASS (TREE_CODE (name)) == 'd')
5308 name = DECL_NAME (name);
5309 }
5310
30394414
JM
5311 my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, 373);
5312
2c73f9f5 5313 val = binding_init (&_b);
52c11ef6 5314 if (!qualified_lookup_using_namespace (name, namespace, val, 0))
2c73f9f5
ML
5315 return error_mark_node;
5316
5317 if (BINDING_VALUE (val))
1c35f5b6
JM
5318 {
5319 val = BINDING_VALUE (val);
5320
f30c84c9
MM
5321 if (template_id)
5322 {
5323 if (DECL_CLASS_TEMPLATE_P (val))
5324 val = lookup_template_class (val,
5325 TREE_OPERAND (template_id, 1),
5326 /*in_decl=*/NULL_TREE,
5327 /*context=*/NULL_TREE,
5328 /*entering_scope=*/0);
5329 else if (DECL_FUNCTION_TEMPLATE_P (val)
5330 || TREE_CODE (val) == OVERLOAD)
5331 val = lookup_template_function (val,
5332 TREE_OPERAND (template_id, 1));
5333 else
5334 {
5335 cp_error ("`%D::%D' is not a template",
5336 namespace, name);
5337 return error_mark_node;
5338 }
5339 }
5340
1c35f5b6
JM
5341 /* If we have a single function from a using decl, pull it out. */
5342 if (TREE_CODE (val) == OVERLOAD && ! really_overloaded_fn (val))
5343 val = OVL_FUNCTION (val);
5344 return val;
5345 }
5346
8251199e 5347 cp_error ("`%D' undeclared in namespace `%D'", name, namespace);
30394414 5348 return error_mark_node;
a9aedbc2
MS
5349}
5350
7ddedda4
MM
5351/* Hash a TYPENAME_TYPE. K is really of type `tree'. */
5352
5353static unsigned long
5354typename_hash (k)
5355 hash_table_key k;
5356{
5357 unsigned long hash;
5358 tree t;
5359
5360 t = (tree) k;
5361 hash = (((unsigned long) TYPE_CONTEXT (t))
5362 ^ ((unsigned long) DECL_NAME (TYPE_NAME (t))));
5363
5364 return hash;
5365}
5366
5367/* Compare two TYPENAME_TYPEs. K1 and K2 are really of type `tree'. */
5368
5369static boolean
5370typename_compare (k1, k2)
5371 hash_table_key k1;
5372 hash_table_key k2;
5373{
5374 tree t1;
5375 tree t2;
5376 tree d1;
5377 tree d2;
5378
5379 t1 = (tree) k1;
5380 t2 = (tree) k2;
5381 d1 = TYPE_NAME (t1);
5382 d2 = TYPE_NAME (t2);
5383
5384 return (DECL_NAME (d1) == DECL_NAME (d2)
5385 && same_type_p (TYPE_CONTEXT (t1), TYPE_CONTEXT (t2))
5386 && ((TREE_TYPE (t1) != NULL_TREE)
5387 == (TREE_TYPE (t2) != NULL_TREE))
5388 && same_type_p (TREE_TYPE (t1), TREE_TYPE (t2))
5389 && TYPENAME_TYPE_FULLNAME (t1) == TYPENAME_TYPE_FULLNAME (t2));
5390}
5391
45869a6c
MM
5392/* Build a TYPENAME_TYPE. If the type is `typename T::t', CONTEXT is
5393 the type of `T', NAME is the IDENTIFIER_NODE for `t'. If BASE_TYPE
5394 is non-NULL, this type is being created by the implicit typename
5395 extension, and BASE_TYPE is a type named `t' in some base class of
5396 `T' which depends on template parameters.
5397
5398 Returns the new TYPENAME_TYPE. */
5399
5400tree
5401build_typename_type (context, name, fullname, base_type)
5402 tree context;
5403 tree name;
5404 tree fullname;
5405 tree base_type;
5406{
5407 tree t;
5408 tree d;
7ddedda4 5409 struct hash_entry* e;
45869a6c 5410
7ddedda4
MM
5411 static struct hash_table ht;
5412
5413 push_obstacks (&permanent_obstack, &permanent_obstack);
5414
5415 if (!ht.table
5416 && !hash_table_init (&ht, &hash_newfunc, &typename_hash,
5417 &typename_compare))
5418 fatal ("virtual memory exhausted");
45869a6c 5419
78638e24
MM
5420 /* The FULLNAME needs to exist for the life of the hash table, i.e.,
5421 for the entire compilation. */
5422 if (!TREE_PERMANENT (fullname))
5423 fullname = copy_to_permanent (fullname);
5424
45869a6c
MM
5425 /* Build the TYPENAME_TYPE. */
5426 t = make_lang_type (TYPENAME_TYPE);
5427 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
5428 TYPENAME_TYPE_FULLNAME (t) = fullname;
5429 TREE_TYPE (t) = base_type;
45869a6c
MM
5430
5431 /* Build the corresponding TYPE_DECL. */
5432 d = build_decl (TYPE_DECL, name, t);
5433 TYPE_NAME (TREE_TYPE (d)) = d;
5434 TYPE_STUB_DECL (TREE_TYPE (d)) = d;
5435 DECL_CONTEXT (d) = FROB_CONTEXT (context);
2642b9bf 5436 DECL_ARTIFICIAL (d) = 1;
45869a6c 5437
7ddedda4
MM
5438 /* See if we already have this type. */
5439 e = hash_lookup (&ht, t, /*create=*/false, /*copy=*/0);
5440 if (e)
5441 {
5442 /* This will free not only TREE_TYPE, but the lang-specific data
5443 and the TYPE_DECL as well. */
5444 obstack_free (&permanent_obstack, t);
5445 t = (tree) e->key;
5446 }
5447 else
5448 /* Insert the type into the table. */
5449 hash_lookup (&ht, t, /*create=*/true, /*copy=*/0);
5450
5451 pop_obstacks ();
45869a6c
MM
5452
5453 return t;
5454}
5455
5566b478
MS
5456tree
5457make_typename_type (context, name)
5458 tree context, name;
5459{
b2b7d40a 5460 tree fullname;
a80e4195 5461
653cc74a 5462 if (TREE_CODE_CLASS (TREE_CODE (name)) == 't')
78638e24
MM
5463 {
5464 if (!(TYPE_LANG_SPECIFIC (name)
5465 && (CLASSTYPE_IS_TEMPLATE (name)
5466 || CLASSTYPE_USE_TEMPLATE (name))))
5467 name = TYPE_IDENTIFIER (name);
5468 else
5469 /* Create a TEMPLATE_ID_EXPR for the type. */
5470 name = build_nt (TEMPLATE_ID_EXPR,
5471 CLASSTYPE_TI_TEMPLATE (name),
5472 CLASSTYPE_TI_ARGS (name));
5473 }
653cc74a 5474 else if (TREE_CODE (name) == TYPE_DECL)
a80e4195 5475 name = DECL_NAME (name);
b2b7d40a
JM
5476
5477 fullname = name;
5478
5479 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
11686454
JM
5480 {
5481 name = TREE_OPERAND (name, 0);
5482 if (TREE_CODE (name) == TEMPLATE_DECL)
5483 name = TREE_OPERAND (fullname, 0) = DECL_NAME (name);
5484 }
b2b7d40a 5485 if (TREE_CODE (name) != IDENTIFIER_NODE)
a80e4195 5486 my_friendly_abort (2000);
5566b478 5487
04ddee1b
BK
5488 if (TREE_CODE (context) == NAMESPACE_DECL)
5489 {
5490 /* We can get here from typename_sub0 in the explicit_template_type
5491 expansion. Just fail. */
5492 cp_error ("no class template named `%#T' in `%#T'",
5493 name, context);
5494 return error_mark_node;
5495 }
5496
85b71cf2 5497 if (! uses_template_parms (context)
b77ead33 5498 || currently_open_class (context))
5566b478 5499 {
b2b7d40a
JM
5500 if (TREE_CODE (fullname) == TEMPLATE_ID_EXPR)
5501 {
ad810b22 5502 tree tmpl = NULL_TREE;
b2b7d40a 5503 if (IS_AGGR_TYPE (context))
ad810b22
MM
5504 tmpl = lookup_field (context, name, 0, 0);
5505 if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl))
b2b7d40a 5506 {
8251199e 5507 cp_error ("no class template named `%#T' in `%#T'",
b2b7d40a
JM
5508 name, context);
5509 return error_mark_node;
5510 }
ffb690bd 5511
ad810b22
MM
5512 return lookup_template_class (tmpl,
5513 TREE_OPERAND (fullname, 1),
5514 NULL_TREE, context,
5515 /*entering_scope=*/0);
b2b7d40a
JM
5516 }
5517 else
5566b478 5518 {
b4f70b3d
NS
5519 tree t;
5520
5521 if (!IS_AGGR_TYPE (context))
b2b7d40a 5522 {
8251199e 5523 cp_error ("no type named `%#T' in `%#T'", name, context);
b2b7d40a
JM
5524 return error_mark_node;
5525 }
1107c4b3 5526
b4f70b3d 5527 t = lookup_field (context, name, 0, 1);
7d4bdeed
MM
5528 if (t)
5529 return TREE_TYPE (t);
5566b478 5530 }
5566b478 5531 }
11249cf0
MM
5532
5533 /* If the CONTEXT is not a template type, then either the field is
5534 there now or its never going to be. */
b4f70b3d 5535 if (!uses_template_parms (context))
11249cf0
MM
5536 {
5537 cp_error ("no type named `%#T' in `%#T'", name, context);
5538 return error_mark_node;
5539 }
5540
45869a6c
MM
5541
5542 return build_typename_type (context, name, fullname, NULL_TREE);
5566b478
MS
5543}
5544
2c73f9f5
ML
5545/* Select the right _DECL from multiple choices. */
5546
5547static tree
52c11ef6 5548select_decl (binding, flags)
2c73f9f5 5549 tree binding;
52c11ef6 5550 int flags;
2c73f9f5
ML
5551{
5552 tree val;
5553 val = BINDING_VALUE (binding);
52c11ef6 5554 if (LOOKUP_NAMESPACES_ONLY (flags))
3e3f722c
ML
5555 {
5556 /* We are not interested in types. */
5557 if (val && TREE_CODE (val) == NAMESPACE_DECL)
5558 return val;
5559 return NULL_TREE;
5560 }
5561
2c73f9f5
ML
5562 /* If we could have a type and
5563 we have nothing or we need a type and have none. */
5564 if (BINDING_TYPE (binding)
52c11ef6
JM
5565 && (!val || ((flags & LOOKUP_PREFER_TYPES)
5566 && TREE_CODE (val) != TYPE_DECL)))
2c73f9f5
ML
5567 val = TYPE_STUB_DECL (BINDING_TYPE (binding));
5568 /* Don't return non-types if we really prefer types. */
52c11ef6 5569 else if (val && LOOKUP_TYPES_ONLY (flags) && TREE_CODE (val) != TYPE_DECL
c592d5d2
ML
5570 && (TREE_CODE (val) != TEMPLATE_DECL
5571 || !DECL_CLASS_TEMPLATE_P (val)))
2c73f9f5 5572 val = NULL_TREE;
1c35f5b6 5573
2c73f9f5
ML
5574 return val;
5575}
5576
2c169bab
JM
5577/* Unscoped lookup of a global: iterate over current namespaces,
5578 considering using-directives. If SPACESP is non-NULL, store a list
5579 of the namespaces we've considered in it. */
ea9635c7 5580
2c169bab
JM
5581tree
5582unqualified_namespace_lookup (name, flags, spacesp)
ea9635c7 5583 tree name;
52c11ef6 5584 int flags;
2c169bab 5585 tree *spacesp;
ea9635c7
ML
5586{
5587 struct tree_binding _binding;
5588 tree b = binding_init (&_binding);
5589 tree initial = current_decl_namespace();
5590 tree scope = initial;
5591 tree siter;
5592 struct binding_level *level;
5593 tree val = NULL_TREE;
5594
2c169bab
JM
5595 if (spacesp)
5596 *spacesp = NULL_TREE;
5597
5598 for (; !val; scope = CP_DECL_CONTEXT (scope))
ea9635c7 5599 {
2c169bab
JM
5600 if (spacesp)
5601 *spacesp = scratch_tree_cons (scope, NULL_TREE, *spacesp);
ea9635c7
ML
5602 val = binding_for_name (name, scope);
5603
5604 /* Initialize binding for this context. */
5605 BINDING_VALUE (b) = BINDING_VALUE (val);
5606 BINDING_TYPE (b) = BINDING_TYPE (val);
5607
5608 /* Add all _DECLs seen through local using-directives. */
5609 for (level = current_binding_level;
5610 !level->namespace_p;
5611 level = level->level_chain)
52c11ef6 5612 if (!lookup_using_namespace (name, b, level->using_directives,
2c169bab 5613 scope, flags, spacesp))
ea9635c7 5614 /* Give up because of error. */
5b163de4 5615 return error_mark_node;
ea9635c7
ML
5616
5617 /* Add all _DECLs seen through global using-directives. */
5618 /* XXX local and global using lists should work equally. */
5619 siter = initial;
5620 while (1)
5621 {
5622 if (!lookup_using_namespace (name, b, DECL_NAMESPACE_USING (siter),
2c169bab 5623 scope, flags, spacesp))
ea9635c7 5624 /* Give up because of error. */
5b163de4 5625 return error_mark_node;
ea9635c7
ML
5626 if (siter == scope) break;
5627 siter = CP_DECL_CONTEXT (siter);
5628 }
5629
52c11ef6 5630 val = select_decl (b, flags);
ea9635c7
ML
5631 if (scope == global_namespace)
5632 break;
ea9635c7
ML
5633 }
5634 return val;
5635}
5636
52c11ef6
JM
5637/* Combine prefer_type and namespaces_only into flags. */
5638
5639static int
5640lookup_flags (prefer_type, namespaces_only)
5641 int prefer_type, namespaces_only;
5642{
5643 if (namespaces_only)
5644 return LOOKUP_PREFER_NAMESPACES;
5645 if (prefer_type > 1)
5646 return LOOKUP_PREFER_TYPES;
5647 if (prefer_type > 0)
5648 return LOOKUP_PREFER_BOTH;
5649 return 0;
5650}
5651
5652/* Given a lookup that returned VAL, use FLAGS to decide if we want to
5653 ignore it or not. Subroutine of lookup_name_real. */
5654
5655static tree
5656qualify_lookup (val, flags)
5657 tree val;
5658 int flags;
5659{
5660 if (val == NULL_TREE)
5661 return val;
6b945830
JM
5662 if ((flags & LOOKUP_PREFER_NAMESPACES) && TREE_CODE (val) == NAMESPACE_DECL)
5663 return val;
5664 if ((flags & LOOKUP_PREFER_TYPES)
5665 && (TREE_CODE (val) == TYPE_DECL
5666 || ((flags & LOOKUP_TEMPLATES_EXPECTED)
5667 && DECL_CLASS_TEMPLATE_P (val))))
5668 return val;
5669 if (flags & (LOOKUP_PREFER_NAMESPACES | LOOKUP_PREFER_TYPES))
52c11ef6
JM
5670 return NULL_TREE;
5671 return val;
5672}
5673
235f734d
MM
5674/* Any other BINDING overrides an implicit TYPENAME. Warn about
5675 that. */
5676
5677static void
5678warn_about_implicit_typename_lookup (typename, binding)
5679 tree typename;
5680 tree binding;
5681{
5682 tree subtype = TREE_TYPE (TREE_TYPE (typename));
5683 tree name = DECL_NAME (typename);
5684
5685 if (! (TREE_CODE (binding) == TEMPLATE_DECL
5686 && CLASSTYPE_TEMPLATE_INFO (subtype)
5687 && CLASSTYPE_TI_TEMPLATE (subtype) == binding)
5688 && ! (TREE_CODE (binding) == TYPE_DECL
5689 && same_type_p (TREE_TYPE (binding), subtype)))
5690 {
5691 cp_warning ("lookup of `%D' finds `%#D'",
5692 name, binding);
5693 cp_warning (" instead of `%D' from dependent base class",
5694 typename);
5695 cp_warning (" (use `typename %T::%D' if that's what you meant)",
5696 constructor_name (current_class_type), name);
5697 }
5698}
5699
8d08fdba
MS
5700/* Look up NAME in the current binding level and its superiors in the
5701 namespace of variables, functions and typedefs. Return a ..._DECL
5702 node of some kind representing its definition if there is only one
5703 such declaration, or return a TREE_LIST with all the overloaded
5704 definitions if there are many, or return 0 if it is undefined.
5705
2c73f9f5
ML
5706 If PREFER_TYPE is > 0, we prefer TYPE_DECLs or namespaces.
5707 If PREFER_TYPE is > 1, we reject non-type decls (e.g. namespaces).
a28e3c7f 5708 If PREFER_TYPE is -2, we're being called from yylex(). (UGLY)
f84b4be9
JM
5709 Otherwise we prefer non-TYPE_DECLs.
5710
5711 If NONCLASS is non-zero, we don't look for the NAME in class scope,
5712 using IDENTIFIER_CLASS_VALUE. */
8d08fdba 5713
824b9a4c 5714static tree
3e3f722c 5715lookup_name_real (name, prefer_type, nonclass, namespaces_only)
8d08fdba 5716 tree name;
3e3f722c 5717 int prefer_type, nonclass, namespaces_only;
8d08fdba 5718{
235f734d
MM
5719 tree t;
5720 tree val = NULL_TREE;
a28e3c7f 5721 int yylex = 0;
e1cd6e56 5722 tree from_obj = NULL_TREE;
52c11ef6 5723 int flags;
235f734d 5724 int val_is_implicit_typename = 0;
8d08fdba 5725
3e3f722c
ML
5726 /* Hack: copy flag set by parser, if set. */
5727 if (only_namespace_names)
5728 namespaces_only = 1;
52c11ef6 5729
a28e3c7f
MS
5730 if (prefer_type == -2)
5731 {
5732 extern int looking_for_typename;
fc378698 5733 tree type = NULL_TREE;
a28e3c7f
MS
5734
5735 yylex = 1;
5736 prefer_type = looking_for_typename;
e1cd6e56 5737
52c11ef6 5738 flags = lookup_flags (prefer_type, namespaces_only);
52c11ef6
JM
5739 /* If the next thing is '<', class templates are types. */
5740 if (looking_for_template)
5741 flags |= LOOKUP_TEMPLATES_EXPECTED;
5742
653cc74a
JM
5743 /* std:: becomes :: for now. */
5744 if (got_scope == std_node)
5745 got_scope = void_type_node;
5746
e1cd6e56
MS
5747 if (got_scope)
5748 type = got_scope;
dff6b454 5749 else if (got_object != error_mark_node)
e1cd6e56 5750 type = got_object;
a28e3c7f 5751
e1cd6e56 5752 if (type)
a28e3c7f 5753 {
e1cd6e56 5754 if (type == error_mark_node)
f376e137 5755 return error_mark_node;
a80e4195
MS
5756 if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type))
5757 type = TREE_TYPE (type);
5566b478 5758
2b9dc906
JM
5759 if (TYPE_P (type))
5760 type = complete_type (type);
5566b478 5761
a1774733 5762 if (TREE_CODE (type) == VOID_TYPE)
2c73f9f5
ML
5763 type = global_namespace;
5764 if (TREE_CODE (type) == NAMESPACE_DECL)
a9aedbc2 5765 {
2c73f9f5
ML
5766 struct tree_binding b;
5767 val = binding_init (&b);
6ad07332 5768 flags |= LOOKUP_COMPLAIN;
52c11ef6 5769 if (!qualified_lookup_using_namespace (name, type, val, flags))
2c73f9f5 5770 return NULL_TREE;
52c11ef6 5771 val = select_decl (val, flags);
a9aedbc2 5772 }
5566b478 5773 else if (! IS_AGGR_TYPE (type)
5156628f 5774 || TREE_CODE (type) == TEMPLATE_TYPE_PARM
73b0fce8 5775 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM
5156628f 5776 || TREE_CODE (type) == TYPENAME_TYPE)
e92cc029 5777 /* Someone else will give an error about this if needed. */
a28e3c7f 5778 val = NULL_TREE;
e1cd6e56 5779 else if (type == current_class_type)
a28e3c7f 5780 val = IDENTIFIER_CLASS_VALUE (name);
a28e3c7f 5781 else
d23a1bb1 5782 val = lookup_member (type, name, 0, prefer_type);
a28e3c7f 5783 }
e1cd6e56
MS
5784 else
5785 val = NULL_TREE;
5786
594740f3 5787 if (got_scope)
e1cd6e56 5788 goto done;
594740f3 5789 else if (got_object && val)
e1cd6e56 5790 from_obj = val;
a28e3c7f 5791 }
52c11ef6 5792 else
5b163de4
JM
5793 {
5794 flags = lookup_flags (prefer_type, namespaces_only);
5795 /* If we're not parsing, we need to complain. */
5796 flags |= LOOKUP_COMPLAIN;
5797 }
e76a2646 5798
d8f8dca1 5799 /* First, look in non-namespace scopes. */
6f1b4c42
JM
5800
5801 if (current_class_type == NULL_TREE)
5802 nonclass = 1;
5803
235f734d 5804 for (t = IDENTIFIER_BINDING (name); t; t = TREE_CHAIN (t))
d8f8dca1 5805 {
235f734d
MM
5806 tree binding;
5807
5808 if (!LOCAL_BINDING_P (t) && nonclass)
d8f8dca1
MM
5809 /* We're not looking for class-scoped bindings, so keep going. */
5810 continue;
5811
5812 /* If this is the kind of thing we're looking for, we're done. */
235f734d
MM
5813 if (qualify_lookup (BINDING_VALUE (t), flags))
5814 binding = BINDING_VALUE (t);
d8f8dca1 5815 else if ((flags & LOOKUP_PREFER_TYPES)
235f734d
MM
5816 && qualify_lookup (BINDING_TYPE (t), flags))
5817 binding = BINDING_TYPE (t);
5818 else
5819 binding = NULL_TREE;
5820
5821 if (binding
83233dca 5822 && (!val || !IMPLICIT_TYPENAME_TYPE_DECL_P (binding)))
d8f8dca1 5823 {
235f734d
MM
5824 if (val_is_implicit_typename && !yylex)
5825 warn_about_implicit_typename_lookup (val, binding);
5826 val = binding;
5827 val_is_implicit_typename
83233dca 5828 = IMPLICIT_TYPENAME_TYPE_DECL_P (val);
235f734d
MM
5829 if (!val_is_implicit_typename)
5830 break;
d8f8dca1
MM
5831 }
5832 }
f181d4ae 5833
235f734d
MM
5834 /* Now lookup in namespace scopes. */
5835 if (!val || val_is_implicit_typename)
e76a2646 5836 {
2c169bab 5837 t = unqualified_namespace_lookup (name, flags, 0);
235f734d 5838 if (t)
c1def683 5839 {
235f734d
MM
5840 if (val_is_implicit_typename && !yylex)
5841 warn_about_implicit_typename_lookup (val, t);
5842 val = t;
c1def683
JM
5843 }
5844 }
5845
a28e3c7f 5846 done:
8d08fdba
MS
5847 if (val)
5848 {
c91a56d2 5849 /* This should only warn about types used in qualified-ids. */
e1cd6e56 5850 if (from_obj && from_obj != val)
5566b478 5851 {
c91a56d2
MS
5852 if (looking_for_typename && TREE_CODE (from_obj) == TYPE_DECL
5853 && TREE_CODE (val) == TYPE_DECL
5854 && TREE_TYPE (from_obj) != TREE_TYPE (val))
5855 {
8251199e 5856 cp_pedwarn ("lookup of `%D' in the scope of `%#T' (`%#T')",
c91a56d2 5857 name, got_object, TREE_TYPE (from_obj));
8251199e 5858 cp_pedwarn (" does not match lookup in the current scope (`%#T')",
c91a56d2
MS
5859 TREE_TYPE (val));
5860 }
594740f3 5861
b8b1a3c1
JM
5862 /* We don't change val to from_obj if got_object depends on
5863 template parms because that breaks implicit typename for
5864 destructor calls. */
5865 if (! uses_template_parms (got_object))
53fdbb3b 5866 val = from_obj;
5566b478 5867 }
e1cd6e56 5868
0c64a9ca
JM
5869 /* If we have a single function from a using decl, pull it out. */
5870 if (TREE_CODE (val) == OVERLOAD && ! really_overloaded_fn (val))
5871 val = OVL_FUNCTION (val);
8d08fdba 5872 }
e1cd6e56
MS
5873 else if (from_obj)
5874 val = from_obj;
8d08fdba
MS
5875
5876 return val;
5877}
5878
700f8a87
MS
5879tree
5880lookup_name_nonclass (name)
5881 tree name;
5882{
3e3f722c 5883 return lookup_name_real (name, 0, 1, 0);
700f8a87
MS
5884}
5885
2c73f9f5
ML
5886tree
5887lookup_function_nonclass (name, args)
5888 tree name;
5889 tree args;
5890{
5891 return lookup_arg_dependent (name, lookup_name_nonclass (name), args);
5892}
5893
3e3f722c
ML
5894tree
5895lookup_name_namespace_only (name)
5896 tree name;
5897{
5898 /* type-or-namespace, nonclass, namespace_only */
5899 return lookup_name_real (name, 1, 1, 1);
5900}
5901
700f8a87
MS
5902tree
5903lookup_name (name, prefer_type)
5904 tree name;
5905 int prefer_type;
5906{
3e3f722c 5907 return lookup_name_real (name, prefer_type, 0, 0);
700f8a87
MS
5908}
5909
a7d2d407
MM
5910/* Similar to `lookup_name' but look only in the innermost non-class
5911 binding level. */
8d08fdba
MS
5912
5913tree
5914lookup_name_current_level (name)
5915 tree name;
5916{
a7d2d407
MM
5917 struct binding_level *b;
5918 tree t = NULL_TREE;
8d08fdba 5919
a7d2d407
MM
5920 b = current_binding_level;
5921 while (b->parm_flag == 2)
5922 b = b->level_chain;
5923
5924 if (b->namespace_p)
8d08fdba 5925 {
30394414 5926 t = IDENTIFIER_NAMESPACE_VALUE (name);
8d08fdba
MS
5927
5928 /* extern "C" function() */
5929 if (t != NULL_TREE && TREE_CODE (t) == TREE_LIST)
5930 t = TREE_VALUE (t);
5931 }
f181d4ae
MM
5932 else if (IDENTIFIER_BINDING (name)
5933 && LOCAL_BINDING_P (IDENTIFIER_BINDING (name)))
8d08fdba 5934 {
a4443a08
MS
5935 while (1)
5936 {
f181d4ae
MM
5937 if (BINDING_LEVEL (IDENTIFIER_BINDING (name)) == b)
5938 return IDENTIFIER_VALUE (name);
5939
9ed182dc
JM
5940 if (b->keep == 2)
5941 b = b->level_chain;
5942 else
5943 break;
5944 }
5945 }
5946
5947 return t;
5948}
5949
5950/* Like lookup_name_current_level, but for types. */
5951
5952tree
5953lookup_type_current_level (name)
5954 tree name;
5955{
5956 register tree t = NULL_TREE;
5957
5958 my_friendly_assert (! current_binding_level->namespace_p, 980716);
5959
5960 if (REAL_IDENTIFIER_TYPE_VALUE (name) != NULL_TREE
5961 && REAL_IDENTIFIER_TYPE_VALUE (name) != global_type_node)
5962 {
5963 struct binding_level *b = current_binding_level;
5964 while (1)
5965 {
5966 if (purpose_member (name, b->type_shadowed))
5967 return REAL_IDENTIFIER_TYPE_VALUE (name);
a4443a08
MS
5968 if (b->keep == 2)
5969 b = b->level_chain;
5970 else
5971 break;
5972 }
8d08fdba
MS
5973 }
5974
5975 return t;
5976}
3e3f722c
ML
5977
5978void
5979begin_only_namespace_names ()
5980{
5981 only_namespace_names = 1;
5982}
5983
5984void
5985end_only_namespace_names ()
5986{
5987 only_namespace_names = 0;
5988}
8d08fdba
MS
5989\f
5990/* Arrange for the user to get a source line number, even when the
5991 compiler is going down in flames, so that she at least has a
5992 chance of working around problems in the compiler. We used to
5993 call error(), but that let the segmentation fault continue
5994 through; now, it's much more passive by asking them to send the
5995 maintainers mail about the problem. */
5996
5997static void
5998signal_catch (sig)
7dee3f36 5999 int sig ATTRIBUTE_UNUSED;
8d08fdba
MS
6000{
6001 signal (SIGSEGV, SIG_DFL);
6002#ifdef SIGIOT
6003 signal (SIGIOT, SIG_DFL);
6004#endif
6005#ifdef SIGILL
6006 signal (SIGILL, SIG_DFL);
6007#endif
6008#ifdef SIGABRT
6009 signal (SIGABRT, SIG_DFL);
6010#endif
6011#ifdef SIGBUS
6012 signal (SIGBUS, SIG_DFL);
6013#endif
6014 my_friendly_abort (0);
6015}
6016
de22184b
MS
6017#if 0
6018/* Unused -- brendan 970107 */
8d08fdba
MS
6019/* Array for holding types considered "built-in". These types
6020 are output in the module in which `main' is defined. */
6021static tree *builtin_type_tdescs_arr;
6022static int builtin_type_tdescs_len, builtin_type_tdescs_max;
de22184b 6023#endif
8d08fdba
MS
6024
6025/* Push the declarations of builtin types into the namespace.
6026 RID_INDEX, if < RID_MAX is the index of the builtin type
6027 in the array RID_POINTERS. NAME is the name used when looking
6028 up the builtin type. TYPE is the _TYPE node for the builtin type. */
6029
6030static void
6031record_builtin_type (rid_index, name, type)
6032 enum rid rid_index;
d8e178a0 6033 const char *name;
8d08fdba
MS
6034 tree type;
6035{
6036 tree rname = NULL_TREE, tname = NULL_TREE;
a703fb38 6037 tree tdecl = NULL_TREE;
8d08fdba
MS
6038
6039 if ((int) rid_index < (int) RID_MAX)
6040 rname = ridpointers[(int) rid_index];
6041 if (name)
6042 tname = get_identifier (name);
6043
6044 TYPE_BUILT_IN (type) = 1;
6045
6046 if (tname)
6047 {
8d08fdba 6048 tdecl = pushdecl (build_decl (TYPE_DECL, tname, type));
8d08fdba
MS
6049 set_identifier_type_value (tname, NULL_TREE);
6050 if ((int) rid_index < (int) RID_MAX)
2c73f9f5
ML
6051 /* Built-in types live in the global namespace. */
6052 SET_IDENTIFIER_GLOBAL_VALUE (tname, tdecl);
8d08fdba
MS
6053 }
6054 if (rname != NULL_TREE)
6055 {
6056 if (tname != NULL_TREE)
6057 {
6058 set_identifier_type_value (rname, NULL_TREE);
2c73f9f5 6059 SET_IDENTIFIER_GLOBAL_VALUE (rname, tdecl);
8d08fdba
MS
6060 }
6061 else
6062 {
8d08fdba 6063 tdecl = pushdecl (build_decl (TYPE_DECL, rname, type));
8d08fdba
MS
6064 set_identifier_type_value (rname, NULL_TREE);
6065 }
6066 }
8d08fdba
MS
6067}
6068
eff71ab0
PB
6069/* Record one of the standard Java types.
6070 * Declare it as having the given NAME.
6071 * If SIZE > 0, it is the size of one of the integral types;
6072 * otherwise it is the negative of the size of one of the other types. */
6073
6074static tree
6075record_builtin_java_type (name, size)
d8e178a0 6076 const char *name;
eff71ab0
PB
6077 int size;
6078{
6079 tree type, decl;
6080 if (size > 0)
6081 type = make_signed_type (size);
6082 else if (size > -32)
6083 { /* "__java_char" or ""__java_boolean". */
6084 type = make_unsigned_type (-size);
6085 /*if (size == -1) TREE_SET_CODE (type, BOOLEAN_TYPE);*/
6086 }
6087 else
6088 { /* "__java_float" or ""__java_double". */
6089 type = make_node (REAL_TYPE);
6090 TYPE_PRECISION (type) = - size;
6091 layout_type (type);
6092 }
6093 record_builtin_type (RID_MAX, name, type);
6094 decl = TYPE_NAME (type);
e229f2cd
PB
6095
6096 /* Suppress generate debug symbol entries for these types,
6097 since for normal C++ they are just clutter.
6098 However, push_lang_context undoes this if extern "Java" is seen. */
eff71ab0 6099 DECL_IGNORED_P (decl) = 1;
e229f2cd 6100
eff71ab0
PB
6101 TYPE_FOR_JAVA (type) = 1;
6102 return type;
6103}
6104
036407f7
ML
6105/* Push a type into the namespace so that the back-ends ignore it. */
6106
6107static void
6108record_unknown_type (type, name)
6109 tree type;
d8e178a0 6110 const char *name;
036407f7
ML
6111{
6112 tree decl = pushdecl (build_decl (TYPE_DECL, get_identifier (name), type));
6113 /* Make sure the "unknown type" typedecl gets ignored for debug info. */
6114 DECL_IGNORED_P (decl) = 1;
6115 TYPE_DECL_SUPPRESS_DEBUG (decl) = 1;
6116 TYPE_SIZE (type) = TYPE_SIZE (void_type_node);
6117 TYPE_ALIGN (type) = 1;
6118 TYPE_MODE (type) = TYPE_MODE (void_type_node);
6119}
6120
8d08fdba
MS
6121/* Push overloaded decl, in global scope, with one argument so it
6122 can be used as a callback from define_function. */
e92cc029 6123
8d08fdba
MS
6124static void
6125push_overloaded_decl_1 (x)
6126 tree x;
6127{
7bdbfa05 6128 push_overloaded_decl (x, PUSH_GLOBAL);
8d08fdba
MS
6129}
6130
8ccc31eb
MS
6131#ifdef __GNUC__
6132__inline
6133#endif
6b5fbb55
MS
6134tree
6135auto_function (name, type, code)
8ccc31eb
MS
6136 tree name, type;
6137 enum built_in_function code;
6138{
6139 return define_function
49c249e1 6140 (IDENTIFIER_POINTER (name), type, code, push_overloaded_decl_1,
8ccc31eb
MS
6141 IDENTIFIER_POINTER (build_decl_overload (name, TYPE_ARG_TYPES (type),
6142 0)));
6143}
6144
8d08fdba
MS
6145/* Create the predefined scalar types of C,
6146 and some nodes representing standard constants (0, 1, (void *)0).
6147 Initialize the global binding level.
6148 Make definitions for built-in primitive functions. */
6149
6150void
6151init_decl_processing ()
6152{
8d08fdba 6153 tree fields[20];
8d08fdba 6154 int wchar_type_size;
8d08fdba
MS
6155 tree array_domain_type;
6156
6157 /* Have to make these distinct before we try using them. */
6158 lang_name_cplusplus = get_identifier ("C++");
6159 lang_name_c = get_identifier ("C");
a1774733 6160 lang_name_java = get_identifier ("Java");
8d08fdba 6161
2c73f9f5 6162 /* Enter the global namespace. */
30394414
JM
6163 my_friendly_assert (global_namespace == NULL_TREE, 375);
6164 my_friendly_assert (current_lang_name == NULL_TREE, 375);
6165 current_lang_name = lang_name_cplusplus;
6166 push_namespace (get_identifier ("::"));
6167 global_namespace = current_namespace;
6168 current_lang_name = NULL_TREE;
6169
e1cd6e56 6170 if (flag_strict_prototype == 2)
830fcda8 6171 flag_strict_prototype = pedantic;
2642b9bf
JM
6172 if (! flag_permissive && ! pedantic)
6173 flag_pedantic_errors = 1;
830fcda8
JM
6174
6175 strict_prototypes_lang_c = flag_strict_prototype;
8926095f 6176
8d08fdba
MS
6177 /* Initially, C. */
6178 current_lang_name = lang_name_c;
6179
6180 current_function_decl = NULL_TREE;
6181 named_labels = NULL_TREE;
e349ee73 6182 named_label_uses = NULL;
8d08fdba
MS
6183 current_binding_level = NULL_BINDING_LEVEL;
6184 free_binding_level = NULL_BINDING_LEVEL;
6185
6186 /* Because most segmentation signals can be traced back into user
6187 code, catch them and at least give the user a chance of working
e92cc029 6188 around compiler bugs. */
8d08fdba
MS
6189 signal (SIGSEGV, signal_catch);
6190
6191 /* We will also catch aborts in the back-end through signal_catch and
6192 give the user a chance to see where the error might be, and to defeat
6193 aborts in the back-end when there have been errors previously in their
e92cc029 6194 code. */
8d08fdba
MS
6195#ifdef SIGIOT
6196 signal (SIGIOT, signal_catch);
6197#endif
6198#ifdef SIGILL
6199 signal (SIGILL, signal_catch);
6200#endif
6201#ifdef SIGABRT
6202 signal (SIGABRT, signal_catch);
6203#endif
6204#ifdef SIGBUS
6205 signal (SIGBUS, signal_catch);
6206#endif
6207
6208 gcc_obstack_init (&decl_obstack);
8d08fdba
MS
6209
6210 /* Must lay these out before anything else gets laid out. */
6211 error_mark_node = make_node (ERROR_MARK);
6212 TREE_PERMANENT (error_mark_node) = 1;
6213 TREE_TYPE (error_mark_node) = error_mark_node;
6214 error_mark_list = build_tree_list (error_mark_node, error_mark_node);
6215 TREE_TYPE (error_mark_list) = error_mark_node;
6216
a28e3c7f
MS
6217 /* Make the binding_level structure for global names. */
6218 pushlevel (0);
8d08fdba 6219 global_binding_level = current_binding_level;
2c73f9f5
ML
6220 /* The global level is the namespace level of ::. */
6221 NAMESPACE_LEVEL (global_namespace) = global_binding_level;
6222 declare_namespace_level ();
8d08fdba
MS
6223
6224 this_identifier = get_identifier (THIS_NAME);
6225 in_charge_identifier = get_identifier (IN_CHARGE_NAME);
fc378698
MS
6226 ctor_identifier = get_identifier (CTOR_NAME);
6227 dtor_identifier = get_identifier (DTOR_NAME);
8d08fdba
MS
6228 pfn_identifier = get_identifier (VTABLE_PFN_NAME);
6229 index_identifier = get_identifier (VTABLE_INDEX_NAME);
6230 delta_identifier = get_identifier (VTABLE_DELTA_NAME);
6231 delta2_identifier = get_identifier (VTABLE_DELTA2_NAME);
6232 pfn_or_delta2_identifier = get_identifier ("__pfn_or_delta2");
6233
6234 /* Define `int' and `char' first so that dbx will output them first. */
6235
6236 integer_type_node = make_signed_type (INT_TYPE_SIZE);
6237 record_builtin_type (RID_INT, NULL_PTR, integer_type_node);
6238
6239 /* Define `char', which is like either `signed char' or `unsigned char'
6240 but not the same as either. */
6241
beb53fb8
JM
6242 char_type_node
6243 = (flag_signed_char
6244 ? make_signed_type (CHAR_TYPE_SIZE)
6245 : make_unsigned_type (CHAR_TYPE_SIZE));
8d08fdba
MS
6246 record_builtin_type (RID_CHAR, "char", char_type_node);
6247
45075bf3
NS
6248 /* `signed' is the same as `int' */
6249 record_builtin_type (RID_SIGNED, NULL_PTR, integer_type_node);
6250
8d08fdba
MS
6251 long_integer_type_node = make_signed_type (LONG_TYPE_SIZE);
6252 record_builtin_type (RID_LONG, "long int", long_integer_type_node);
6253
6254 unsigned_type_node = make_unsigned_type (INT_TYPE_SIZE);
6255 record_builtin_type (RID_UNSIGNED, "unsigned int", unsigned_type_node);
6256
6257 long_unsigned_type_node = make_unsigned_type (LONG_TYPE_SIZE);
6258 record_builtin_type (RID_MAX, "long unsigned int", long_unsigned_type_node);
6259 record_builtin_type (RID_MAX, "unsigned long", long_unsigned_type_node);
6260
6261 long_long_integer_type_node = make_signed_type (LONG_LONG_TYPE_SIZE);
6262 record_builtin_type (RID_MAX, "long long int", long_long_integer_type_node);
6263
6264 long_long_unsigned_type_node = make_unsigned_type (LONG_LONG_TYPE_SIZE);
6265 record_builtin_type (RID_MAX, "long long unsigned int",
6266 long_long_unsigned_type_node);
6267 record_builtin_type (RID_MAX, "long long unsigned",
6268 long_long_unsigned_type_node);
6269
5156628f
MS
6270 short_integer_type_node = make_signed_type (SHORT_TYPE_SIZE);
6271 record_builtin_type (RID_SHORT, "short int", short_integer_type_node);
6272 short_unsigned_type_node = make_unsigned_type (SHORT_TYPE_SIZE);
6273 record_builtin_type (RID_MAX, "short unsigned int", short_unsigned_type_node);
6274 record_builtin_type (RID_MAX, "unsigned short", short_unsigned_type_node);
6275
8d08fdba 6276 /* `unsigned long' is the standard type for sizeof.
8d08fdba 6277 Note that stddef.h uses `unsigned long',
f5426d1e
R
6278 and this must agree, even if long and int are the same size. */
6279 set_sizetype
6280 (TREE_TYPE (IDENTIFIER_GLOBAL_VALUE (get_identifier (SIZE_TYPE))));
8d08fdba
MS
6281
6282 ptrdiff_type_node
6283 = TREE_TYPE (IDENTIFIER_GLOBAL_VALUE (get_identifier (PTRDIFF_TYPE)));
6284
8d08fdba
MS
6285 /* Define both `signed char' and `unsigned char'. */
6286 signed_char_type_node = make_signed_type (CHAR_TYPE_SIZE);
6287 record_builtin_type (RID_MAX, "signed char", signed_char_type_node);
6288 unsigned_char_type_node = make_unsigned_type (CHAR_TYPE_SIZE);
6289 record_builtin_type (RID_MAX, "unsigned char", unsigned_char_type_node);
6290
835f9b4d
GRK
6291 /* Create the widest literal types. */
6292 widest_integer_literal_type_node = make_signed_type (HOST_BITS_PER_WIDE_INT * 2);
6293 pushdecl (build_decl (TYPE_DECL, NULL_TREE,
6294 widest_integer_literal_type_node));
6295
6296 widest_unsigned_literal_type_node = make_unsigned_type (HOST_BITS_PER_WIDE_INT * 2);
6297 pushdecl (build_decl (TYPE_DECL, NULL_TREE,
6298 widest_unsigned_literal_type_node));
6299
8d08fdba
MS
6300 /* These are types that type_for_size and type_for_mode use. */
6301 intQI_type_node = make_signed_type (GET_MODE_BITSIZE (QImode));
6302 pushdecl (build_decl (TYPE_DECL, NULL_TREE, intQI_type_node));
6303 intHI_type_node = make_signed_type (GET_MODE_BITSIZE (HImode));
6304 pushdecl (build_decl (TYPE_DECL, NULL_TREE, intHI_type_node));
6305 intSI_type_node = make_signed_type (GET_MODE_BITSIZE (SImode));
6306 pushdecl (build_decl (TYPE_DECL, NULL_TREE, intSI_type_node));
6307 intDI_type_node = make_signed_type (GET_MODE_BITSIZE (DImode));
6308 pushdecl (build_decl (TYPE_DECL, NULL_TREE, intDI_type_node));
946dc1c8 6309#if HOST_BITS_PER_WIDE_INT >= 64
f7554e8c 6310 intTI_type_node = make_signed_type (GET_MODE_BITSIZE (TImode));
5ebcdddb 6311 pushdecl (build_decl (TYPE_DECL, get_identifier ("__int128_t"), intTI_type_node));
946dc1c8 6312#endif
8d08fdba
MS
6313 unsigned_intQI_type_node = make_unsigned_type (GET_MODE_BITSIZE (QImode));
6314 pushdecl (build_decl (TYPE_DECL, NULL_TREE, unsigned_intQI_type_node));
6315 unsigned_intHI_type_node = make_unsigned_type (GET_MODE_BITSIZE (HImode));
6316 pushdecl (build_decl (TYPE_DECL, NULL_TREE, unsigned_intHI_type_node));
6317 unsigned_intSI_type_node = make_unsigned_type (GET_MODE_BITSIZE (SImode));
6318 pushdecl (build_decl (TYPE_DECL, NULL_TREE, unsigned_intSI_type_node));
6319 unsigned_intDI_type_node = make_unsigned_type (GET_MODE_BITSIZE (DImode));
6320 pushdecl (build_decl (TYPE_DECL, NULL_TREE, unsigned_intDI_type_node));
946dc1c8 6321#if HOST_BITS_PER_WIDE_INT >= 64
f7554e8c 6322 unsigned_intTI_type_node = make_unsigned_type (GET_MODE_BITSIZE (TImode));
5ebcdddb 6323 pushdecl (build_decl (TYPE_DECL, get_identifier ("__uint128_t"), unsigned_intTI_type_node));
946dc1c8 6324#endif
8d08fdba
MS
6325
6326 float_type_node = make_node (REAL_TYPE);
6327 TYPE_PRECISION (float_type_node) = FLOAT_TYPE_SIZE;
6328 record_builtin_type (RID_FLOAT, NULL_PTR, float_type_node);
6329 layout_type (float_type_node);
6330
6331 double_type_node = make_node (REAL_TYPE);
6332 if (flag_short_double)
6333 TYPE_PRECISION (double_type_node) = FLOAT_TYPE_SIZE;
6334 else
6335 TYPE_PRECISION (double_type_node) = DOUBLE_TYPE_SIZE;
6336 record_builtin_type (RID_DOUBLE, NULL_PTR, double_type_node);
6337 layout_type (double_type_node);
6338
6339 long_double_type_node = make_node (REAL_TYPE);
6340 TYPE_PRECISION (long_double_type_node) = LONG_DOUBLE_TYPE_SIZE;
6341 record_builtin_type (RID_MAX, "long double", long_double_type_node);
6342 layout_type (long_double_type_node);
6343
37c46b43
MS
6344 complex_integer_type_node = make_node (COMPLEX_TYPE);
6345 pushdecl (build_decl (TYPE_DECL, get_identifier ("complex int"),
6346 complex_integer_type_node));
6347 TREE_TYPE (complex_integer_type_node) = integer_type_node;
6348 layout_type (complex_integer_type_node);
6349
6350 complex_float_type_node = make_node (COMPLEX_TYPE);
6351 pushdecl (build_decl (TYPE_DECL, get_identifier ("complex float"),
6352 complex_float_type_node));
6353 TREE_TYPE (complex_float_type_node) = float_type_node;
6354 layout_type (complex_float_type_node);
6355
6356 complex_double_type_node = make_node (COMPLEX_TYPE);
6357 pushdecl (build_decl (TYPE_DECL, get_identifier ("complex double"),
6358 complex_double_type_node));
6359 TREE_TYPE (complex_double_type_node) = double_type_node;
6360 layout_type (complex_double_type_node);
6361
6362 complex_long_double_type_node = make_node (COMPLEX_TYPE);
6363 pushdecl (build_decl (TYPE_DECL, get_identifier ("complex long double"),
6364 complex_long_double_type_node));
6365 TREE_TYPE (complex_long_double_type_node) = long_double_type_node;
6366 layout_type (complex_long_double_type_node);
6367
eff71ab0
PB
6368 java_byte_type_node = record_builtin_java_type ("__java_byte", 8);
6369 java_short_type_node = record_builtin_java_type ("__java_short", 16);
6370 java_int_type_node = record_builtin_java_type ("__java_int", 32);
6371 java_long_type_node = record_builtin_java_type ("__java_long", 64);
6372 java_float_type_node = record_builtin_java_type ("__java_float", -32);
6373 java_double_type_node = record_builtin_java_type ("__java_double", -64);
6374 java_char_type_node = record_builtin_java_type ("__java_char", -16);
6375 java_boolean_type_node = record_builtin_java_type ("__java_boolean", -1);
6376
8d08fdba
MS
6377 integer_zero_node = build_int_2 (0, 0);
6378 TREE_TYPE (integer_zero_node) = integer_type_node;
6379 integer_one_node = build_int_2 (1, 0);
6380 TREE_TYPE (integer_one_node) = integer_type_node;
6381 integer_two_node = build_int_2 (2, 0);
6382 TREE_TYPE (integer_two_node) = integer_type_node;
6383 integer_three_node = build_int_2 (3, 0);
6384 TREE_TYPE (integer_three_node) = integer_type_node;
8d08fdba 6385
255512c1
JM
6386 boolean_type_node = make_unsigned_type (BOOL_TYPE_SIZE);
6387 TREE_SET_CODE (boolean_type_node, BOOLEAN_TYPE);
c1ea6a0b
BK
6388 TYPE_MAX_VALUE (boolean_type_node) = build_int_2 (1, 0);
6389 TREE_TYPE (TYPE_MAX_VALUE (boolean_type_node)) = boolean_type_node;
6390 TYPE_PRECISION (boolean_type_node) = 1;
255512c1
JM
6391 record_builtin_type (RID_BOOL, "bool", boolean_type_node);
6392 boolean_false_node = build_int_2 (0, 0);
6393 TREE_TYPE (boolean_false_node) = boolean_type_node;
6394 boolean_true_node = build_int_2 (1, 0);
6395 TREE_TYPE (boolean_true_node) = boolean_type_node;
2986ae00 6396
8d08fdba
MS
6397 /* These are needed by stor-layout.c. */
6398 size_zero_node = size_int (0);
6399 size_one_node = size_int (1);
6400
e92cc029
MS
6401 signed_size_zero_node = build_int_2 (0, 0);
6402 TREE_TYPE (signed_size_zero_node) = make_signed_type (TYPE_PRECISION (sizetype));
6403
8d08fdba
MS
6404 void_type_node = make_node (VOID_TYPE);
6405 record_builtin_type (RID_VOID, NULL_PTR, void_type_node);
6406 layout_type (void_type_node); /* Uses integer_zero_node. */
6407 void_list_node = build_tree_list (NULL_TREE, void_type_node);
6408 TREE_PARMLIST (void_list_node) = 1;
6409
d11ad92e 6410 null_pointer_node = build_int_2 (0, 0);
8d08fdba 6411 TREE_TYPE (null_pointer_node) = build_pointer_type (void_type_node);
d11ad92e
MS
6412 layout_type (TREE_TYPE (null_pointer_node));
6413
8d08fdba
MS
6414 /* Used for expressions that do nothing, but are not errors. */
6415 void_zero_node = build_int_2 (0, 0);
6416 TREE_TYPE (void_zero_node) = void_type_node;
6417
6418 string_type_node = build_pointer_type (char_type_node);
beb53fb8 6419 const_string_type_node
91063b51
MM
6420 = build_pointer_type (build_qualified_type (char_type_node,
6421 TYPE_QUAL_CONST));
4cc1d462 6422 empty_except_spec = build_tree_list (NULL_TREE, NULL_TREE);
6b5fbb55 6423#if 0
8d08fdba 6424 record_builtin_type (RID_MAX, NULL_PTR, string_type_node);
6b5fbb55 6425#endif
8d08fdba
MS
6426
6427 /* Make a type to be the domain of a few array types
6428 whose domains don't really matter.
6429 200 is small enough that it always fits in size_t
6430 and large enough that it can hold most function names for the
6431 initializations of __FUNCTION__ and __PRETTY_FUNCTION__. */
6432 array_domain_type = build_index_type (build_int_2 (200, 0));
6433
2c73f9f5 6434 /* Make a type for arrays of characters.
8d08fdba
MS
6435 With luck nothing will ever really depend on the length of this
6436 array type. */
6437 char_array_type_node
6438 = build_array_type (char_type_node, array_domain_type);
6439 /* Likewise for arrays of ints. */
6440 int_array_type_node
6441 = build_array_type (integer_type_node, array_domain_type);
6442
6443 /* This is just some anonymous class type. Nobody should ever
6444 need to look inside this envelope. */
6445 class_star_type_node = build_pointer_type (make_lang_type (RECORD_TYPE));
6446
7f4edbcb
BS
6447 if (flag_huge_objects)
6448 delta_type_node = long_integer_type_node;
6449 else
6450 delta_type_node = short_integer_type_node;
6451
8d08fdba
MS
6452 default_function_type
6453 = build_function_type (integer_type_node, NULL_TREE);
8d08fdba
MS
6454
6455 ptr_type_node = build_pointer_type (void_type_node);
beb53fb8 6456 const_ptr_type_node
91063b51 6457 = build_pointer_type (build_qualified_type (void_type_node,
7f4edbcb
BS
6458 TYPE_QUAL_CONST));
6459 c_common_nodes_and_builtins (1, flag_no_builtin, flag_no_nonansi_builtin);
6460
824b9a4c 6461 void_ftype_ptr
4cc1d462 6462 = build_exception_variant (void_ftype_ptr, empty_except_spec);
8d08fdba 6463
8d08fdba
MS
6464 /* C++ extensions */
6465
6466 unknown_type_node = make_node (UNKNOWN_TYPE);
036407f7
ML
6467 record_unknown_type (unknown_type_node, "unknown type");
6468
8d08fdba
MS
6469 /* Indirecting an UNKNOWN_TYPE node yields an UNKNOWN_TYPE node. */
6470 TREE_TYPE (unknown_type_node) = unknown_type_node;
a6967cc0 6471
03d0f4af 6472 TREE_TYPE (null_node) = type_for_size (POINTER_SIZE, 0);
a6967cc0
JM
6473
6474 /* Looking up TYPE_POINTER_TO and TYPE_REFERENCE_TO yield the same
6475 result. */
8d08fdba
MS
6476 TYPE_POINTER_TO (unknown_type_node) = unknown_type_node;
6477 TYPE_REFERENCE_TO (unknown_type_node) = unknown_type_node;
6478
e92cc029 6479 /* This is special for C++ so functions can be overloaded. */
8d08fdba
MS
6480 wchar_type_node
6481 = TREE_TYPE (IDENTIFIER_GLOBAL_VALUE (get_identifier (WCHAR_TYPE)));
6482 wchar_type_size = TYPE_PRECISION (wchar_type_node);
6483 signed_wchar_type_node = make_signed_type (wchar_type_size);
6484 unsigned_wchar_type_node = make_unsigned_type (wchar_type_size);
6485 wchar_type_node
6486 = TREE_UNSIGNED (wchar_type_node)
6487 ? unsigned_wchar_type_node
6488 : signed_wchar_type_node;
6489 record_builtin_type (RID_WCHAR, "__wchar_t", wchar_type_node);
6490
f376e137
MS
6491 /* Artificial declaration of wchar_t -- can be bashed */
6492 wchar_decl_node = build_decl (TYPE_DECL, get_identifier ("wchar_t"),
6493 wchar_type_node);
6494 pushdecl (wchar_decl_node);
6495
8d08fdba
MS
6496 /* This is for wide string constants. */
6497 wchar_array_type_node
6498 = build_array_type (wchar_type_node, array_domain_type);
6499
8926095f 6500 if (flag_vtable_thunks)
700f8a87
MS
6501 {
6502 /* Make sure we get a unique function type, so we can give
6503 its pointer type a name. (This wins for gdb.) */
6504 tree vfunc_type = make_node (FUNCTION_TYPE);
6505 TREE_TYPE (vfunc_type) = integer_type_node;
6506 TYPE_ARG_TYPES (vfunc_type) = NULL_TREE;
6507 layout_type (vfunc_type);
6508
6509 vtable_entry_type = build_pointer_type (vfunc_type);
6510 }
8926095f 6511 else
700f8a87
MS
6512 {
6513 vtable_entry_type = make_lang_type (RECORD_TYPE);
4ce3d537
MM
6514 fields[0] = build_lang_decl (FIELD_DECL, delta_identifier,
6515 delta_type_node);
6516 fields[1] = build_lang_decl (FIELD_DECL, index_identifier,
6517 delta_type_node);
6518 fields[2] = build_lang_decl (FIELD_DECL, pfn_identifier,
6519 ptr_type_node);
700f8a87
MS
6520 finish_builtin_type (vtable_entry_type, VTBL_PTR_TYPE, fields, 2,
6521 double_type_node);
6522
6523 /* Make this part of an invisible union. */
6524 fields[3] = copy_node (fields[2]);
6525 TREE_TYPE (fields[3]) = delta_type_node;
6526 DECL_NAME (fields[3]) = delta2_identifier;
6527 DECL_MODE (fields[3]) = TYPE_MODE (delta_type_node);
6528 DECL_SIZE (fields[3]) = TYPE_SIZE (delta_type_node);
6529 TREE_UNSIGNED (fields[3]) = 0;
6530 TREE_CHAIN (fields[2]) = fields[3];
91063b51
MM
6531 vtable_entry_type = build_qualified_type (vtable_entry_type,
6532 TYPE_QUAL_CONST);
700f8a87
MS
6533 }
6534 record_builtin_type (RID_MAX, VTBL_PTR_TYPE, vtable_entry_type);
8d08fdba 6535
8d08fdba 6536 vtbl_type_node
52bf7d5d 6537 = build_cplus_array_type (vtable_entry_type, NULL_TREE);
8d08fdba 6538 layout_type (vtbl_type_node);
91063b51 6539 vtbl_type_node = build_qualified_type (vtbl_type_node, TYPE_QUAL_CONST);
8d08fdba 6540 record_builtin_type (RID_MAX, NULL_PTR, vtbl_type_node);
849da744
MM
6541 vtbl_ptr_type_node = build_pointer_type (vtable_entry_type);
6542 layout_type (vtbl_ptr_type_node);
6543 record_builtin_type (RID_MAX, NULL_PTR, vtbl_ptr_type_node);
8d08fdba 6544
2c73f9f5
ML
6545 std_node = build_decl (NAMESPACE_DECL,
6546 get_identifier (flag_honor_std ? "fake std":"std"),
6633d636
MS
6547 void_type_node);
6548 pushdecl (std_node);
6549
2c73f9f5 6550 global_type_node = make_node (LANG_TYPE);
036407f7 6551 record_unknown_type (global_type_node, "global type");
2c73f9f5 6552
db5ae43f
MS
6553 /* Now, C++. */
6554 current_lang_name = lang_name_cplusplus;
8d08fdba 6555
ced78d8b 6556 {
2c73f9f5
ML
6557 tree bad_alloc_type_node, newtype, deltype;
6558 if (flag_honor_std)
6559 push_namespace (get_identifier ("std"));
6560 bad_alloc_type_node = xref_tag
ca107ded 6561 (class_type_node, get_identifier ("bad_alloc"), 1);
2c73f9f5
ML
6562 if (flag_honor_std)
6563 pop_namespace ();
6564 newtype = build_exception_variant
4cc1d462
NS
6565 (ptr_ftype_sizetype, add_exception_specifier (NULL_TREE, bad_alloc_type_node, -1));
6566 deltype = build_exception_variant (void_ftype_ptr, empty_except_spec);
ced78d8b
JM
6567 auto_function (ansi_opname[(int) NEW_EXPR], newtype, NOT_BUILT_IN);
6568 auto_function (ansi_opname[(int) VEC_NEW_EXPR], newtype, NOT_BUILT_IN);
94e098d1
MM
6569 global_delete_fndecl
6570 = auto_function (ansi_opname[(int) DELETE_EXPR], deltype, NOT_BUILT_IN);
ced78d8b
JM
6571 auto_function (ansi_opname[(int) VEC_DELETE_EXPR], deltype, NOT_BUILT_IN);
6572 }
8d08fdba
MS
6573
6574 abort_fndecl
de22184b 6575 = define_function ("__pure_virtual", void_ftype,
8d08fdba
MS
6576 NOT_BUILT_IN, 0, 0);
6577
8d08fdba
MS
6578 /* Perform other language dependent initializations. */
6579 init_class_processing ();
6580 init_init_processing ();
6581 init_search_processing ();
1737fe20
BK
6582 if (flag_rtti)
6583 init_rtti_processing ();
8d08fdba 6584
6467930b 6585 if (flag_exceptions)
8d2733ca 6586 init_exception_processing ();
8d08fdba
MS
6587 if (flag_no_inline)
6588 {
6589 flag_inline_functions = 0;
8d08fdba 6590 }
9e9ff709 6591
7fcdf4c2 6592 if (! supports_one_only ())
72b7eeff 6593 flag_weak = 0;
8d08fdba
MS
6594
6595 /* Create the global bindings for __FUNCTION__ and __PRETTY_FUNCTION__. */
6596 declare_function_name ();
6597
6598 /* Prepare to check format strings against argument lists. */
6599 init_function_format_info ();
e9a25f70
JL
6600
6601 /* Show we use EH for cleanups. */
6602 using_eh_for_cleanups ();
62c154ed
JM
6603
6604 print_error_function = lang_print_error_function;
501ba25a 6605 lang_get_alias_set = &c_get_alias_set;
e5dc5fb2 6606 valid_lang_attribute = cp_valid_lang_attribute;
d9cf7c82
JM
6607
6608 /* Maintain consistency. Perhaps we should just complain if they
6609 say -fwritable-strings? */
6610 if (flag_writable_strings)
6611 flag_const_strings = 0;
62c154ed
JM
6612}
6613
6614/* Function to print any language-specific context for an error message. */
6615
6616static void
6617lang_print_error_function (file)
8df4696d 6618 const char *file;
62c154ed
JM
6619{
6620 default_print_error_function (file);
6621 maybe_print_template_context ();
8d08fdba
MS
6622}
6623
6624/* Make a definition for a builtin function named NAME and whose data type
6625 is TYPE. TYPE should be a function type with argument types.
6626 FUNCTION_CODE tells later passes how to compile calls to this function.
6627 See tree.h for its possible values.
6628
6629 If LIBRARY_NAME is nonzero, use that for DECL_ASSEMBLER_NAME,
6630 the name to be called if we can't opencode the function. */
6631
6632tree
6633define_function (name, type, function_code, pfn, library_name)
d8e178a0 6634 const char *name;
8d08fdba
MS
6635 tree type;
6636 enum built_in_function function_code;
49c249e1 6637 void (*pfn) PROTO((tree));
d8e178a0 6638 const char *library_name;
8d08fdba
MS
6639{
6640 tree decl = build_lang_decl (FUNCTION_DECL, get_identifier (name), type);
6641 DECL_EXTERNAL (decl) = 1;
6642 TREE_PUBLIC (decl) = 1;
863adfc0 6643 DECL_ARTIFICIAL (decl) = 1;
8d08fdba 6644
2c73f9f5 6645 my_friendly_assert (DECL_CONTEXT (decl) == NULL_TREE, 392);
cb0dbb9a 6646 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
2c73f9f5 6647
8d08fdba
MS
6648 /* Since `pushdecl' relies on DECL_ASSEMBLER_NAME instead of DECL_NAME,
6649 we cannot change DECL_ASSEMBLER_NAME until we have installed this
6650 function in the namespace. */
6651 if (pfn) (*pfn) (decl);
6652 if (library_name)
6653 DECL_ASSEMBLER_NAME (decl) = get_identifier (library_name);
6654 make_function_rtl (decl);
6655 if (function_code != NOT_BUILT_IN)
6656 {
6657 DECL_BUILT_IN (decl) = 1;
39211cd5 6658 DECL_FUNCTION_CODE (decl) = function_code;
8d08fdba
MS
6659 }
6660 return decl;
6661}
7f4edbcb
BS
6662
6663tree
6664builtin_function (name, type, code, libname)
6665 const char *name;
6666 tree type;
6667 enum built_in_function code;
6668 const char *libname;
6669{
6670 return define_function (name, type, code, (void (*) PROTO((tree)))pushdecl, libname);
6671}
8d08fdba 6672\f
61a127b3
MM
6673/* When we call finish_struct for an anonymous union, we create
6674 default copy constructors and such. But, an anonymous union
6675 shouldn't have such things; this function undoes the damage to the
6676 anonymous union type T.
6677
6678 (The reason that we create the synthesized methods is that we don't
6679 distinguish `union { int i; }' from `typedef union { int i; } U'.
6680 The first is an anonymous union; the second is just an ordinary
6681 union type.) */
6682
6683void
6bdb8141 6684fixup_anonymous_aggr (t)
61a127b3
MM
6685 tree t;
6686{
6687 tree *q;
6688
6689 /* Wipe out memory of synthesized methods */
6690 TYPE_HAS_CONSTRUCTOR (t) = 0;
6691 TYPE_HAS_DEFAULT_CONSTRUCTOR (t) = 0;
6692 TYPE_HAS_INIT_REF (t) = 0;
6693 TYPE_HAS_CONST_INIT_REF (t) = 0;
6694 TYPE_HAS_ASSIGN_REF (t) = 0;
61a127b3
MM
6695 TYPE_HAS_CONST_ASSIGN_REF (t) = 0;
6696
6697 /* Splice the implicitly generated functions out of the TYPE_METHODS
6698 list. */
6699 q = &TYPE_METHODS (t);
6700 while (*q)
6701 {
6702 if (DECL_ARTIFICIAL (*q))
6703 *q = TREE_CHAIN (*q);
6704 else
6705 q = &TREE_CHAIN (*q);
6706 }
6707
6708 /* ANSI C++ June 5 1992 WP 9.5.3. Anonymous unions may not have
6709 function members. */
6710 if (TYPE_METHODS (t))
6711 error ("an anonymous union cannot have function members");
6712}
6713
72a93143
JM
6714/* Make sure that a declaration with no declarator is well-formed, i.e.
6715 just defines a tagged type or anonymous union.
8d08fdba 6716
72a93143 6717 Returns the type defined, if any. */
8d08fdba 6718
72a93143
JM
6719tree
6720check_tag_decl (declspecs)
8d08fdba
MS
6721 tree declspecs;
6722{
72a93143 6723 int found_type = 0;
2986ae00 6724 tree ob_modifier = NULL_TREE;
8d08fdba 6725 register tree link;
8d08fdba
MS
6726 register tree t = NULL_TREE;
6727
6728 for (link = declspecs; link; link = TREE_CHAIN (link))
6729 {
6730 register tree value = TREE_VALUE (link);
6731
72a93143 6732 if (TYPE_P (value))
8d08fdba 6733 {
72a93143 6734 ++found_type;
5566b478 6735
72a93143
JM
6736 if (IS_AGGR_TYPE (value) || TREE_CODE (value) == ENUMERAL_TYPE)
6737 {
6738 my_friendly_assert (TYPE_MAIN_DECL (value) != NULL_TREE, 261);
6739 t = value;
6740 }
8d08fdba 6741 }
83f660b7
JM
6742 else if (value == ridpointers[(int) RID_FRIEND])
6743 {
83f660b7
JM
6744 if (current_class_type == NULL_TREE
6745 || current_scope () != current_class_type)
6746 ob_modifier = value;
6747 }
8d08fdba 6748 else if (value == ridpointers[(int) RID_STATIC]
2986ae00
MS
6749 || value == ridpointers[(int) RID_EXTERN]
6750 || value == ridpointers[(int) RID_AUTO]
28cbf42c
MS
6751 || value == ridpointers[(int) RID_REGISTER]
6752 || value == ridpointers[(int) RID_INLINE]
6753 || value == ridpointers[(int) RID_VIRTUAL]
72a93143
JM
6754 || value == ridpointers[(int) RID_CONST]
6755 || value == ridpointers[(int) RID_VOLATILE]
28cbf42c 6756 || value == ridpointers[(int) RID_EXPLICIT])
2986ae00 6757 ob_modifier = value;
8d08fdba
MS
6758 }
6759
72a93143
JM
6760 if (found_type > 1)
6761 error ("multiple types in one declaration");
7e2067ca
JM
6762
6763 /* Inside a class, we might be in a friend or access declaration.
6764 Until we have a good way of detecting the latter, don't warn. */
6765 if (t == NULL_TREE && ! current_class_type)
6766 pedwarn ("declaration does not declare anything");
0dd3962d
JM
6767
6768 /* Check for an anonymous union. We're careful
6769 accessing TYPE_IDENTIFIER because some built-in types, like
6770 pointer-to-member types, do not have TYPE_NAME. */
6bdb8141 6771 else if (t && IS_AGGR_TYPE_CODE (TREE_CODE (t))
0dd3962d
JM
6772 && TYPE_NAME (t)
6773 && ANON_AGGRNAME_P (TYPE_IDENTIFIER (t)))
6774 {
6775 /* Anonymous unions are objects, so they can have specifiers. */;
6bdb8141
JM
6776 SET_ANON_AGGR_TYPE_P (t);
6777
6778 if (TREE_CODE (t) != UNION_TYPE && pedantic && ! in_system_header)
6779 pedwarn ("ISO C++ prohibits anonymous structs");
0dd3962d
JM
6780 }
6781
83f660b7 6782 else if (ob_modifier)
8d08fdba 6783 {
83f660b7
JM
6784 if (ob_modifier == ridpointers[(int) RID_INLINE]
6785 || ob_modifier == ridpointers[(int) RID_VIRTUAL])
6786 cp_error ("`%D' can only be specified for functions", ob_modifier);
6787 else if (ob_modifier == ridpointers[(int) RID_FRIEND])
6788 cp_error ("`%D' can only be specified inside a class", ob_modifier);
6789 else if (ob_modifier == ridpointers[(int) RID_EXPLICIT])
6790 cp_error ("`%D' can only be specified for constructors",
6791 ob_modifier);
6792 else
6793 cp_error ("`%D' can only be specified for objects and functions",
6794 ob_modifier);
72a93143 6795 }
8d08fdba 6796
72a93143
JM
6797 return t;
6798}
6799
6800/* Called when a declaration is seen that contains no names to declare.
6801 If its type is a reference to a structure, union or enum inherited
6802 from a containing scope, shadow that tag name for the current scope
6803 with a forward reference.
6804 If its type defines a new named structure or union
6805 or defines an enum, it is valid but we need not do anything here.
6806 Otherwise, it is an error.
6807
6808 C++: may have to grok the declspecs to learn about static,
6809 complain for anonymous unions. */
6810
6811void
6812shadow_tag (declspecs)
6813 tree declspecs;
6814{
6815 tree t = check_tag_decl (declspecs);
6816
6817 if (t)
6818 maybe_process_partial_specialization (t);
6819
6820 /* This is where the variables in an anonymous union are
6821 declared. An anonymous union declaration looks like:
6822 union { ... } ;
6823 because there is no declarator after the union, the parser
6824 sends that declaration here. */
6bdb8141 6825 if (t && ANON_AGGR_TYPE_P (t))
72a93143 6826 {
6bdb8141 6827 fixup_anonymous_aggr (t);
72a93143
JM
6828
6829 if (TYPE_FIELDS (t))
6830 {
6831 tree decl = grokdeclarator (NULL_TREE, declspecs, NORMAL, 0,
6832 NULL_TREE);
6833 finish_anon_union (decl);
6834 }
8d08fdba
MS
6835 }
6836}
6837\f
6838/* Decode a "typename", such as "int **", returning a ..._TYPE node. */
6839
6840tree
6841groktypename (typename)
6842 tree typename;
6843{
6844 if (TREE_CODE (typename) != TREE_LIST)
6845 return typename;
6846 return grokdeclarator (TREE_VALUE (typename),
6847 TREE_PURPOSE (typename),
c11b6f21 6848 TYPENAME, 0, NULL_TREE);
8d08fdba
MS
6849}
6850
6851/* Decode a declarator in an ordinary declaration or data definition.
6852 This is called as soon as the type information and variable name
6853 have been parsed, before parsing the initializer if any.
6854 Here we create the ..._DECL node, fill in its type,
6855 and put it on the list of decls for the current context.
6856 The ..._DECL node is returned as the value.
6857
6858 Exception: for arrays where the length is not specified,
82580166 6859 the type is left null, to be filled in by `cp_finish_decl'.
8d08fdba
MS
6860
6861 Function definitions do not come here; they go to start_function
6862 instead. However, external and forward declarations of functions
6863 do go through here. Structure field declarations are done by
6864 grokfield and not through here. */
6865
6866/* Set this to zero to debug not using the temporary obstack
6867 to parse initializers. */
6868int debug_temp_inits = 1;
6869
6870tree
a1774733 6871start_decl (declarator, declspecs, initialized, attributes, prefix_attributes)
8d08fdba
MS
6872 tree declarator, declspecs;
6873 int initialized;
a1774733 6874 tree attributes, prefix_attributes;
8d08fdba
MS
6875{
6876 register tree decl;
6877 register tree type, tem;
6878 tree context;
6879 extern int have_extern_spec;
6880 extern int used_extern_spec;
b17e2870 6881 tree attrlist;
8d08fdba 6882
5566b478
MS
6883#if 0
6884 /* See code below that used this. */
8d08fdba 6885 int init_written = initialized;
5566b478 6886#endif
8d08fdba 6887
e92cc029 6888 /* This should only be done once on the top most decl. */
8d08fdba
MS
6889 if (have_extern_spec && !used_extern_spec)
6890 {
a28e3c7f
MS
6891 declspecs = decl_tree_cons (NULL_TREE, get_identifier ("extern"),
6892 declspecs);
8d08fdba
MS
6893 used_extern_spec = 1;
6894 }
6895
b17e2870
JM
6896 if (attributes || prefix_attributes)
6897 attrlist = build_scratch_list (attributes, prefix_attributes);
6898 else
6899 attrlist = NULL_TREE;
6900
c11b6f21 6901 decl = grokdeclarator (declarator, declspecs, NORMAL, initialized,
b17e2870
JM
6902 attrlist);
6903
a1774733 6904 if (decl == NULL_TREE || TREE_CODE (decl) == VOID_TYPE)
8d08fdba
MS
6905 return NULL_TREE;
6906
6907 type = TREE_TYPE (decl);
6908
44689c12
ML
6909 if (type == error_mark_node)
6910 return NULL_TREE;
6911
8d08fdba 6912 /* Don't lose if destructors must be executed at file-level. */
5156628f 6913 if (! processing_template_decl && TREE_STATIC (decl)
ec255269 6914 && TYPE_NEEDS_DESTRUCTOR (complete_type (type))
8d08fdba
MS
6915 && !TREE_PERMANENT (decl))
6916 {
6917 push_obstacks (&permanent_obstack, &permanent_obstack);
6918 decl = copy_node (decl);
6919 if (TREE_CODE (type) == ARRAY_TYPE)
6920 {
6921 tree itype = TYPE_DOMAIN (type);
6922 if (itype && ! TREE_PERMANENT (itype))
6923 {
6924 itype = build_index_type (copy_to_permanent (TYPE_MAX_VALUE (itype)));
6925 type = build_cplus_array_type (TREE_TYPE (type), itype);
6926 TREE_TYPE (decl) = type;
6927 }
6928 }
6929 pop_obstacks ();
6930 }
6931
8d08fdba
MS
6932 context
6933 = (TREE_CODE (decl) == FUNCTION_DECL && DECL_VIRTUAL_P (decl))
6934 ? DECL_CLASS_CONTEXT (decl)
6935 : DECL_CONTEXT (decl);
6936
9a68c51f
JM
6937 if (initialized && context && TREE_CODE (context) == NAMESPACE_DECL
6938 && context != current_namespace && TREE_CODE (decl) == VAR_DECL)
6939 {
6940 /* When parsing the initializer, lookup should use the object's
6941 namespace. */
6942 push_decl_namespace (context);
6943 }
6944
2c73f9f5
ML
6945 /* We are only interested in class contexts, later. */
6946 if (context && TREE_CODE (context) == NAMESPACE_DECL)
6947 context = NULL_TREE;
6948
8d08fdba
MS
6949 if (initialized)
6950 /* Is it valid for this decl to have an initializer at all?
6951 If not, set INITIALIZED to zero, which will indirectly
82580166 6952 tell `cp_finish_decl' to ignore the initializer once it is parsed. */
8d08fdba
MS
6953 switch (TREE_CODE (decl))
6954 {
6955 case TYPE_DECL:
6956 /* typedef foo = bar means give foo the same type as bar.
82580166 6957 We haven't parsed bar yet, so `cp_finish_decl' will fix that up.
8d08fdba
MS
6958 Any other case of an initialization in a TYPE_DECL is an error. */
6959 if (pedantic || list_length (declspecs) > 1)
6960 {
8251199e 6961 cp_error ("typedef `%D' is initialized", decl);
8d08fdba
MS
6962 initialized = 0;
6963 }
6964 break;
6965
6966 case FUNCTION_DECL:
8251199e 6967 cp_error ("function `%#D' is initialized like a variable", decl);
8d08fdba
MS
6968 initialized = 0;
6969 break;
6970
6971 default:
3e41d13b 6972 break;
8d08fdba
MS
6973 }
6974
8d08fdba
MS
6975 if (initialized)
6976 {
a9aedbc2 6977 if (! toplevel_bindings_p ()
8d08fdba 6978 && DECL_EXTERNAL (decl))
8251199e 6979 cp_warning ("declaration of `%#D' has `extern' and is initialized",
8d08fdba
MS
6980 decl);
6981 DECL_EXTERNAL (decl) = 0;
5566b478 6982 if (toplevel_bindings_p ())
8d08fdba
MS
6983 TREE_STATIC (decl) = 1;
6984
6985 /* Tell `pushdecl' this is an initialized decl
6986 even though we don't yet have the initializer expression.
82580166 6987 Also tell `cp_finish_decl' it may store the real initializer. */
8d08fdba
MS
6988 DECL_INITIAL (decl) = error_mark_node;
6989 }
6990
fa20888b
MK
6991#ifdef SET_DEFAULT_DECL_ATTRIBUTES
6992 SET_DEFAULT_DECL_ATTRIBUTES (decl, attributes);
6993#endif
6994
6995 /* Set attributes here so if duplicate decl, will have proper attributes. */
6996 cplus_decl_attributes (decl, attributes, prefix_attributes);
6997
5566b478 6998 if (context && TYPE_SIZE (complete_type (context)) != NULL_TREE)
5b605f68 6999 {
6b400b21 7000 push_nested_class (context, 2);
e97e5263 7001
5b605f68
MS
7002 if (TREE_CODE (decl) == VAR_DECL)
7003 {
7004 tree field = lookup_field (context, DECL_NAME (decl), 0, 0);
7005 if (field == NULL_TREE || TREE_CODE (field) != VAR_DECL)
8251199e 7006 cp_error ("`%#D' is not a static member of `%#T'", decl, context);
e349ee73
MS
7007 else
7008 {
7009 if (DECL_CONTEXT (field) != context)
f2d773a2 7010 {
8251199e 7011 cp_pedwarn ("ANSI C++ does not permit `%T::%D' to be defined as `%T::%D'",
f2d773a2
JM
7012 DECL_CONTEXT (field), DECL_NAME (decl),
7013 context, DECL_NAME (decl));
7014 DECL_CONTEXT (decl) = DECL_CONTEXT (field);
7015 }
75650646
MM
7016 /* Static data member are tricky; an in-class initialization
7017 still doesn't provide a definition, so the in-class
7018 declaration will have DECL_EXTERNAL set, but will have an
7019 initialization. Thus, duplicate_decls won't warn
7020 about this situation, and so we check here. */
7021 if (DECL_INITIAL (decl) && DECL_INITIAL (field))
8251199e 7022 cp_error ("duplicate initialization of %D", decl);
e349ee73
MS
7023 if (duplicate_decls (decl, field))
7024 decl = field;
7025 }
5b605f68 7026 }
f30432d7
MS
7027 else
7028 {
5566b478 7029 tree field = check_classfn (context, decl);
f30432d7
MS
7030 if (field && duplicate_decls (decl, field))
7031 decl = field;
7032 }
7033
7034 /* cp_finish_decl sets DECL_EXTERNAL if DECL_IN_AGGR_P is set. */
dff3e828
BK
7035 DECL_IN_AGGR_P (decl) = 0;
7036 if ((DECL_LANG_SPECIFIC (decl) && DECL_USE_TEMPLATE (decl))
7037 || CLASSTYPE_USE_TEMPLATE (context))
84e6233f
JM
7038 {
7039 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
7040 /* [temp.expl.spec] An explicit specialization of a static data
7041 member of a template is a definition if the declaration
7042 includes an initializer; otherwise, it is a declaration.
7043
7044 We check for processing_specialization so this only applies
7045 to the new specialization syntax. */
7046 if (DECL_INITIAL (decl) == NULL_TREE && processing_specialization)
7047 DECL_EXTERNAL (decl) = 1;
7048 }
f30432d7 7049
b7698cf0 7050 if (DECL_EXTERNAL (decl) && ! DECL_TEMPLATE_SPECIALIZATION (decl))
8251199e 7051 cp_pedwarn ("declaration of `%#D' outside of class is not definition",
f30432d7 7052 decl);
5b605f68
MS
7053 }
7054
9188c363
MM
7055 /* Enter this declaration into the symbol table. */
7056 tem = maybe_push_decl (decl);
2ee887f2 7057
5156628f 7058 if (processing_template_decl)
5566b478 7059 {
9188c363
MM
7060 if (at_function_scope_p ())
7061 push_permanent_obstack ();
7062
7063 tem = push_template_decl (tem);
7064 /* In a a local scope, add a representation of this declaration
7065 to the statement tree. */
7066 if (at_function_scope_p ())
7067 {
7068 add_decl_stmt (decl);
7069 pop_obstacks ();
7070 }
5566b478
MS
7071 }
7072
7073
2ee887f2 7074#if ! defined (ASM_OUTPUT_BSS) && ! defined (ASM_OUTPUT_ALIGNED_BSS)
a3203465 7075 /* Tell the back-end to use or not use .common as appropriate. If we say
a50f0918
MS
7076 -fconserve-space, we want this to save .data space, at the expense of
7077 wrong semantics. If we say -fno-conserve-space, we want this to
7078 produce errors about redefs; to do this we force variables into the
7079 data segment. */
a3203465 7080 DECL_COMMON (tem) = flag_conserve_space || ! TREE_PUBLIC (tem);
2ee887f2 7081#endif
3a846e6e 7082
5156628f 7083 if (! processing_template_decl)
5566b478 7084 start_decl_1 (tem);
8d08fdba 7085
5566b478
MS
7086 /* Corresponding pop_obstacks is done in `cp_finish_decl'. */
7087 push_obstacks_nochange ();
8d08fdba 7088
8d08fdba
MS
7089 return tem;
7090}
7091
5566b478
MS
7092void
7093start_decl_1 (decl)
7094 tree decl;
8d08fdba 7095{
5566b478
MS
7096 tree type = TREE_TYPE (decl);
7097 int initialized = (DECL_INITIAL (decl) != NULL_TREE);
8d08fdba 7098
44689c12
ML
7099 if (type == error_mark_node)
7100 return;
7101
5566b478
MS
7102 /* If this type of object needs a cleanup, and control may
7103 jump past it, make a new binding level so that it is cleaned
7104 up only when it is initialized first. */
7105 if (TYPE_NEEDS_DESTRUCTOR (type)
7106 && current_binding_level->more_cleanups_ok == 0)
7107 pushlevel_temporary (1);
7108
7109 if (initialized)
7110 /* Is it valid for this decl to have an initializer at all?
7111 If not, set INITIALIZED to zero, which will indirectly
7112 tell `cp_finish_decl' to ignore the initializer once it is parsed. */
8d08fdba 7113 {
5566b478
MS
7114 /* Don't allow initializations for incomplete types except for
7115 arrays which might be completed by the initialization. */
44689c12 7116 if (TYPE_SIZE (complete_type (type)) != NULL_TREE)
5566b478
MS
7117 ; /* A complete type is ok. */
7118 else if (TREE_CODE (type) != ARRAY_TYPE)
8d08fdba 7119 {
8251199e 7120 cp_error ("variable `%#D' has initializer but incomplete type",
5566b478
MS
7121 decl);
7122 initialized = 0;
25eb19ff 7123 type = TREE_TYPE (decl) = error_mark_node;
5566b478
MS
7124 }
7125 else if (TYPE_SIZE (complete_type (TREE_TYPE (type))) == NULL_TREE)
7126 {
7127 if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
8251199e 7128 cp_error ("elements of array `%#D' have incomplete type", decl);
5566b478
MS
7129 /* else we already gave an error in start_decl. */
7130 initialized = 0;
8d08fdba 7131 }
8d08fdba
MS
7132 }
7133
5566b478
MS
7134 if (!initialized
7135 && TREE_CODE (decl) != TYPE_DECL
7136 && TREE_CODE (decl) != TEMPLATE_DECL
7137 && IS_AGGR_TYPE (type) && ! DECL_EXTERNAL (decl))
8d08fdba 7138 {
5156628f 7139 if ((! processing_template_decl || ! uses_template_parms (type))
7fcdf4c2 7140 && TYPE_SIZE (complete_type (type)) == NULL_TREE)
5566b478 7141 {
8251199e 7142 cp_error ("aggregate `%#D' has incomplete type and cannot be initialized",
5566b478
MS
7143 decl);
7144 /* Change the type so that assemble_variable will give
7145 DECL an rtl we can live with: (mem (const_int 0)). */
25eb19ff 7146 type = TREE_TYPE (decl) = error_mark_node;
5566b478
MS
7147 }
7148 else
7149 {
7150 /* If any base type in the hierarchy of TYPE needs a constructor,
7151 then we set initialized to 1. This way any nodes which are
7152 created for the purposes of initializing this aggregate
7153 will live as long as it does. This is necessary for global
7154 aggregates which do not have their initializers processed until
7155 the end of the file. */
7156 initialized = TYPE_NEEDS_CONSTRUCTING (type);
7157 }
7158 }
7159
5566b478
MS
7160 if (! initialized)
7161 DECL_INITIAL (decl) = NULL_TREE;
7162}
7163
7164/* Handle initialization of references.
38e01259 7165 These three arguments are from `cp_finish_decl', and have the
e92cc029
MS
7166 same meaning here that they do there.
7167
7168 Quotes on semantics can be found in ARM 8.4.3. */
7169
5566b478 7170static void
a703fb38 7171grok_reference_init (decl, type, init)
5566b478 7172 tree decl, type, init;
5566b478
MS
7173{
7174 tree tmp;
7175
7176 if (init == NULL_TREE)
7177 {
7178 if ((DECL_LANG_SPECIFIC (decl) == 0
7179 || DECL_IN_AGGR_P (decl) == 0)
7180 && ! DECL_THIS_EXTERN (decl))
7181 {
8251199e 7182 cp_error ("`%D' declared as reference but not initialized", decl);
5566b478
MS
7183 if (TREE_CODE (decl) == VAR_DECL)
7184 SET_DECL_REFERENCE_SLOT (decl, error_mark_node);
7185 }
7186 return;
7187 }
7188
7189 if (init == error_mark_node)
7190 return;
7191
7192 if (TREE_CODE (type) == REFERENCE_TYPE
7193 && TREE_CODE (init) == CONSTRUCTOR)
7194 {
8251199e 7195 cp_error ("ANSI C++ forbids use of initializer list to initialize reference `%D'", decl);
8d08fdba
MS
7196 return;
7197 }
7198
7199 if (TREE_CODE (init) == TREE_LIST)
7200 init = build_compound_expr (init);
8d08fdba 7201
8ccc31eb
MS
7202 if (TREE_CODE (TREE_TYPE (init)) == REFERENCE_TYPE)
7203 init = convert_from_reference (init);
7204
8d08fdba
MS
7205 if (TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE
7206 && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE)
7207 {
a3203465 7208 /* Note: default conversion is only called in very special cases. */
8d08fdba
MS
7209 init = default_conversion (init);
7210 }
7211
a3203465 7212 tmp = convert_to_reference
9a3b49ac
MS
7213 (type, init, CONV_IMPLICIT,
7214 LOOKUP_SPECULATIVELY|LOOKUP_NORMAL|DIRECT_BIND, decl);
8d08fdba 7215
a3203465
MS
7216 if (tmp == error_mark_node)
7217 goto fail;
7218 else if (tmp != NULL_TREE)
8d08fdba 7219 {
a3203465 7220 init = tmp;
4c7bdca6 7221 DECL_INITIAL (decl) = save_expr (init);
8d08fdba 7222 }
a3203465 7223 else
8d08fdba 7224 {
8251199e 7225 cp_error ("cannot initialize `%T' from `%T'", type, TREE_TYPE (init));
a3203465 7226 goto fail;
8d08fdba 7227 }
8d08fdba 7228
8d08fdba
MS
7229 /* ?? Can this be optimized in some cases to
7230 hand back the DECL_INITIAL slot?? */
7231 if (TYPE_SIZE (TREE_TYPE (type)))
7232 {
7233 init = convert_from_reference (decl);
7234 if (TREE_PERMANENT (decl))
7235 init = copy_to_permanent (init);
7236 SET_DECL_REFERENCE_SLOT (decl, init);
7237 }
7238
7239 if (TREE_STATIC (decl) && ! TREE_CONSTANT (DECL_INITIAL (decl)))
7240 {
7241 expand_static_init (decl, DECL_INITIAL (decl));
7242 DECL_INITIAL (decl) = NULL_TREE;
7243 }
7244 return;
7245
7246 fail:
7247 if (TREE_CODE (decl) == VAR_DECL)
7248 SET_DECL_REFERENCE_SLOT (decl, error_mark_node);
7249 return;
7250}
7251
6060a796
MS
7252/* Fill in DECL_INITIAL with some magical value to prevent expand_decl from
7253 mucking with forces it does not comprehend (i.e. initialization with a
7254 constructor). If we are at global scope and won't go into COMMON, fill
7255 it in with a dummy CONSTRUCTOR to force the variable into .data;
7256 otherwise we can use error_mark_node. */
7257
28cbf42c
MS
7258static tree
7259obscure_complex_init (decl, init)
7260 tree decl, init;
6060a796 7261{
28cbf42c
MS
7262 if (! flag_no_inline && TREE_STATIC (decl))
7263 {
7264 if (extract_init (decl, init))
7265 return NULL_TREE;
7266 }
7267
2ee887f2 7268#if ! defined (ASM_OUTPUT_BSS) && ! defined (ASM_OUTPUT_ALIGNED_BSS)
a9aedbc2 7269 if (toplevel_bindings_p () && ! DECL_COMMON (decl))
6060a796
MS
7270 DECL_INITIAL (decl) = build (CONSTRUCTOR, TREE_TYPE (decl), NULL_TREE,
7271 NULL_TREE);
7272 else
2ee887f2 7273#endif
6060a796 7274 DECL_INITIAL (decl) = error_mark_node;
28cbf42c
MS
7275
7276 return init;
6060a796
MS
7277}
7278
91063b51
MM
7279/* Issue an error message if DECL is an uninitialized const variable. */
7280
7281static void
7282check_for_uninitialized_const_var (decl)
7283 tree decl;
7284{
7285 tree type = TREE_TYPE (decl);
7286
7287 /* ``Unless explicitly declared extern, a const object does not have
7288 external linkage and must be initialized. ($8.4; $12.1)'' ARM
7289 7.1.6 */
7290 if (TREE_CODE (decl) == VAR_DECL
7291 && TREE_CODE (type) != REFERENCE_TYPE
7292 && CP_TYPE_CONST_P (type)
7293 && !TYPE_NEEDS_CONSTRUCTING (type)
7294 && !DECL_INITIAL (decl))
7295 cp_error ("uninitialized const `%D'", decl);
7296}
7297
8d08fdba
MS
7298/* Finish processing of a declaration;
7299 install its line number and initial value.
7300 If the length of an array type is not known before,
7301 it must be determined now, from the initial value, or it is an error.
7302
7303 Call `pop_obstacks' iff NEED_POP is nonzero.
7304
82580166 7305 For C++, `cp_finish_decl' must be fairly evasive: it must keep initializers
8d08fdba
MS
7306 for aggregates that have constructors alive on the permanent obstack,
7307 so that the global initializing functions can be written at the end.
7308
7309 INIT0 holds the value of an initializer that should be allowed to escape
7310 the normal rules.
7311
6060a796
MS
7312 FLAGS is LOOKUP_ONLYCONVERTING is the = init syntax was used, else 0
7313 if the (init) syntax was used.
7314
8d08fdba 7315 For functions that take default parameters, DECL points to its
82580166 7316 "maximal" instantiation. `cp_finish_decl' must then also declared its
8d08fdba
MS
7317 subsequently lower and lower forms of instantiation, checking for
7318 ambiguity as it goes. This can be sped up later. */
7319
7320void
82580166 7321cp_finish_decl (decl, init, asmspec_tree, need_pop, flags)
8d08fdba
MS
7322 tree decl, init;
7323 tree asmspec_tree;
7324 int need_pop;
6060a796 7325 int flags;
8d08fdba
MS
7326{
7327 register tree type;
a703fb38 7328 tree cleanup = NULL_TREE, ttype = NULL_TREE;
8d08fdba
MS
7329 int was_incomplete;
7330 int temporary = allocation_temporary_p ();
9c0758dd 7331 const char *asmspec = NULL;
8d08fdba 7332 int was_readonly = 0;
0c4b14c4 7333 int already_used = 0;
1c609c4c 7334 tree core_type;
8d08fdba
MS
7335
7336 /* If this is 0, then we did not change obstacks. */
7337 if (! decl)
7338 {
7339 if (init)
8251199e 7340 error ("assignment (not initialization) in declaration");
8d08fdba
MS
7341 return;
7342 }
7343
a4443a08 7344 /* If a name was specified, get the string. */
8d08fdba 7345 if (asmspec_tree)
8d08fdba 7346 asmspec = TREE_STRING_POINTER (asmspec_tree);
8d08fdba 7347
2c73f9f5
ML
7348 if (init && TREE_CODE (init) == NAMESPACE_DECL)
7349 {
8251199e 7350 cp_error ("Cannot initialize `%D' to namespace `%D'",
2c73f9f5
ML
7351 decl, init);
7352 init = NULL_TREE;
7353 }
7354
6ba89f8e
MM
7355 if (current_class_type
7356 && DECL_REAL_CONTEXT (decl) == current_class_type
7357 && TYPE_BEING_DEFINED (current_class_type)
7358 && (DECL_INITIAL (decl) || init))
7359 DECL_DEFINED_IN_CLASS_P (decl) = 1;
7360
9a68c51f
JM
7361 if (TREE_CODE (decl) == VAR_DECL
7362 && DECL_CONTEXT (decl)
7363 && TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL
7364 && DECL_CONTEXT (decl) != current_namespace
7365 && init)
7366 {
7367 /* Leave the namespace of the object. */
7368 pop_decl_namespace ();
7369 }
7370
8d08fdba
MS
7371 /* If the type of the thing we are declaring either has
7372 a constructor, or has a virtual function table pointer,
7373 AND its initialization was accepted by `start_decl',
7374 then we stayed on the permanent obstack through the
7375 declaration, otherwise, changed obstacks as GCC would. */
7376
7377 type = TREE_TYPE (decl);
7378
f376e137 7379 if (type == error_mark_node)
eac293a1 7380 {
a9aedbc2 7381 if (toplevel_bindings_p () && temporary)
eac293a1
MS
7382 end_temporary_allocation ();
7383
7384 return;
7385 }
f376e137 7386
a7a7710d
NS
7387 if (TYPE_HAS_MUTABLE_P (type))
7388 TREE_READONLY (decl) = 0;
7389
5156628f 7390 if (processing_template_decl)
5566b478
MS
7391 {
7392 if (init && DECL_INITIAL (decl))
9188c363 7393 DECL_INITIAL (decl) = copy_to_permanent (init);
5566b478
MS
7394 goto finish_end0;
7395 }
3e41d13b 7396
8d08fdba
MS
7397 /* Take care of TYPE_DECLs up front. */
7398 if (TREE_CODE (decl) == TYPE_DECL)
7399 {
7400 if (init && DECL_INITIAL (decl))
7401 {
7402 /* typedef foo = bar; store the type of bar as the type of foo. */
7403 TREE_TYPE (decl) = type = TREE_TYPE (init);
7404 DECL_INITIAL (decl) = init = NULL_TREE;
7405 }
a0a33927
MS
7406 if (type != error_mark_node
7407 && IS_AGGR_TYPE (type) && DECL_NAME (decl))
8d08fdba
MS
7408 {
7409 if (TREE_TYPE (DECL_NAME (decl)) && TREE_TYPE (decl) != type)
8251199e 7410 cp_warning ("shadowing previous type declaration of `%#D'", decl);
8d08fdba
MS
7411 set_identifier_type_value (DECL_NAME (decl), type);
7412 CLASSTYPE_GOT_SEMICOLON (type) = 1;
7413 }
7414 GNU_xref_decl (current_function_decl, decl);
cffa8729
MS
7415
7416 /* If we have installed this as the canonical typedef for this
7417 type, and that type has not been defined yet, delay emitting
956d6950 7418 the debug information for it, as we will emit it later. */
d2e5ee5c 7419 if (TYPE_MAIN_DECL (TREE_TYPE (decl)) == decl
cffa8729
MS
7420 && TYPE_SIZE (TREE_TYPE (decl)) == NULL_TREE)
7421 TYPE_DECL_SUPPRESS_DEBUG (decl) = 1;
7422
8d08fdba 7423 rest_of_decl_compilation (decl, NULL_PTR,
5566b478 7424 DECL_CONTEXT (decl) == NULL_TREE, at_eof);
8d08fdba
MS
7425 goto finish_end;
7426 }
3e41d13b 7427
8d08fdba 7428 if (TREE_CODE (decl) != FUNCTION_DECL)
3e41d13b 7429 ttype = target_type (type);
8d08fdba
MS
7430
7431 if (! DECL_EXTERNAL (decl) && TREE_READONLY (decl)
7432 && TYPE_NEEDS_CONSTRUCTING (type))
7433 {
8d08fdba
MS
7434 /* Currently, GNU C++ puts constants in text space, making them
7435 impossible to initialize. In the future, one would hope for
7436 an operating system which understood the difference between
7437 initialization and the running of a program. */
7438 was_readonly = 1;
7439 TREE_READONLY (decl) = 0;
7440 }
7441
7442 if (TREE_CODE (decl) == FIELD_DECL)
7443 {
7444 if (init && init != error_mark_node)
7445 my_friendly_assert (TREE_PERMANENT (init), 147);
7446
7447 if (asmspec)
7448 {
7449 /* This must override the asm specifier which was placed
6060a796 7450 by grokclassfn. Lay this out fresh. */
8d08fdba
MS
7451 DECL_RTL (TREE_TYPE (decl)) = NULL_RTX;
7452 DECL_ASSEMBLER_NAME (decl) = get_identifier (asmspec);
7453 make_decl_rtl (decl, asmspec, 0);
7454 }
7455 }
7456 /* If `start_decl' didn't like having an initialization, ignore it now. */
7457 else if (init != NULL_TREE && DECL_INITIAL (decl) == NULL_TREE)
7458 init = NULL_TREE;
7459 else if (DECL_EXTERNAL (decl))
7460 ;
6eabb241 7461 else if (TREE_CODE (type) == REFERENCE_TYPE)
8d08fdba 7462 {
f376e137 7463 if (TREE_STATIC (decl))
9188c363 7464 make_decl_rtl (decl, NULL_PTR, toplevel_bindings_p ());
a703fb38 7465 grok_reference_init (decl, type, init);
8d08fdba
MS
7466 init = NULL_TREE;
7467 }
7468
3e41d13b
MM
7469 /* Check for certain invalid initializations. */
7470 if (init)
7471 {
7472 if (TYPE_SIZE (type) && !TREE_CONSTANT (TYPE_SIZE (type)))
7473 {
7474 cp_error ("variable-sized object `%D' may not be initialized", decl);
7475 init = NULL_TREE;
7476 }
7477 if (TREE_CODE (type) == ARRAY_TYPE
7478 && !TYPE_SIZE (complete_type (TREE_TYPE (type))))
7479 {
7480 cp_error ("elements of array `%#D' have incomplete type", decl);
7481 init = NULL_TREE;
7482 }
7483 }
7484
8d08fdba
MS
7485 GNU_xref_decl (current_function_decl, decl);
7486
1c609c4c
NS
7487 core_type = type;
7488 while (TREE_CODE (core_type) == ARRAY_TYPE)
7489 core_type = TREE_TYPE (core_type);
7490
a0a33927 7491 if (TREE_CODE (decl) == FIELD_DECL)
8d08fdba
MS
7492 ;
7493 else if (TREE_CODE (decl) == CONST_DECL)
7494 {
7495 my_friendly_assert (TREE_CODE (decl) != REFERENCE_TYPE, 148);
7496
7497 DECL_INITIAL (decl) = init;
7498
7499 /* This will keep us from needing to worry about our obstacks. */
7500 my_friendly_assert (init != NULL_TREE, 149);
7501 init = NULL_TREE;
7502 }
7503 else if (init)
7504 {
7505 if (TYPE_HAS_CONSTRUCTOR (type) || TYPE_NEEDS_CONSTRUCTING (type))
7506 {
7507 if (TREE_CODE (type) == ARRAY_TYPE)
7508 init = digest_init (type, init, (tree *) 0);
848b92e1
JM
7509 else if (TREE_CODE (init) == CONSTRUCTOR
7510 && TREE_HAS_CONSTRUCTOR (init))
8d08fdba 7511 {
f30432d7 7512 if (TYPE_NON_AGGREGATE_CLASS (type))
8d08fdba 7513 {
8251199e 7514 cp_error ("`%D' must be initialized by constructor, not by `{...}'",
a28e3c7f 7515 decl);
8d08fdba
MS
7516 init = error_mark_node;
7517 }
7518 else
7519 goto dont_use_constructor;
7520 }
8d08fdba
MS
7521 }
7522 else
7523 {
7524 dont_use_constructor:
7525 if (TREE_CODE (init) != TREE_VEC)
7526 init = store_init_value (decl, init);
8d08fdba 7527 }
28cbf42c
MS
7528
7529 if (init)
7530 /* We must hide the initializer so that expand_decl
7531 won't try to do something it does not understand. */
7532 init = obscure_complex_init (decl, init);
8d08fdba 7533 }
a0a33927
MS
7534 else if (DECL_EXTERNAL (decl))
7535 ;
8d08fdba
MS
7536 else if (TREE_CODE_CLASS (TREE_CODE (type)) == 't'
7537 && (IS_AGGR_TYPE (type) || TYPE_NEEDS_CONSTRUCTING (type)))
7538 {
1c609c4c 7539 if (! TYPE_NEEDS_CONSTRUCTING (core_type))
8d08fdba 7540 {
1c609c4c 7541 if (CLASSTYPE_READONLY_FIELDS_NEED_INIT (core_type))
8251199e 7542 cp_error ("structure `%D' with uninitialized const members", decl);
1c609c4c 7543 if (CLASSTYPE_REF_FIELDS_NEED_INIT (core_type))
8251199e 7544 cp_error ("structure `%D' with uninitialized reference members",
a28e3c7f 7545 decl);
8d08fdba
MS
7546 }
7547
91063b51 7548 check_for_uninitialized_const_var (decl);
8d08fdba 7549
6060a796
MS
7550 if (TYPE_SIZE (type) != NULL_TREE
7551 && TYPE_NEEDS_CONSTRUCTING (type))
28cbf42c 7552 init = obscure_complex_init (decl, NULL_TREE);
8d08fdba 7553
91063b51
MM
7554 }
7555 else
7556 check_for_uninitialized_const_var (decl);
7557
8d08fdba
MS
7558 /* For top-level declaration, the initial value was read in
7559 the temporary obstack. MAXINDEX, rtl, etc. to be made below
7560 must go in the permanent obstack; but don't discard the
7561 temporary data yet. */
7562
a9aedbc2 7563 if (toplevel_bindings_p () && temporary)
8d08fdba
MS
7564 end_temporary_allocation ();
7565
7566 /* Deduce size of array from initialization, if not already known. */
7567
7568 if (TREE_CODE (type) == ARRAY_TYPE
7569 && TYPE_DOMAIN (type) == NULL_TREE
7570 && TREE_CODE (decl) != TYPE_DECL)
7571 {
7572 int do_default
7573 = (TREE_STATIC (decl)
7574 /* Even if pedantic, an external linkage array
7575 may have incomplete type at first. */
7576 ? pedantic && ! DECL_EXTERNAL (decl)
7577 : !DECL_EXTERNAL (decl));
7578 tree initializer = init ? init : DECL_INITIAL (decl);
7579 int failure = complete_array_type (type, initializer, do_default);
7580
7581 if (failure == 1)
8251199e 7582 cp_error ("initializer fails to determine size of `%D'", decl);
8d08fdba
MS
7583
7584 if (failure == 2)
7585 {
7586 if (do_default)
8251199e 7587 cp_error ("array size missing in `%D'", decl);
8d08fdba
MS
7588 /* If a `static' var's size isn't known, make it extern as
7589 well as static, so it does not get allocated. If it's not
7590 `static', then don't mark it extern; finish_incomplete_decl
7591 will give it a default size and it will get allocated. */
7592 else if (!pedantic && TREE_STATIC (decl) && !TREE_PUBLIC (decl))
7593 DECL_EXTERNAL (decl) = 1;
7594 }
7595
7596 if (pedantic && TYPE_DOMAIN (type) != NULL_TREE
7597 && tree_int_cst_lt (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
7598 integer_zero_node))
8251199e 7599 cp_error ("zero-size array `%D'", decl);
8d08fdba
MS
7600
7601 layout_decl (decl, 0);
7602 }
7603
7604 if (TREE_CODE (decl) == VAR_DECL)
7605 {
7606 if (DECL_SIZE (decl) == NULL_TREE
ec255269 7607 && TYPE_SIZE (complete_type (TREE_TYPE (decl))) != NULL_TREE)
8d08fdba
MS
7608 layout_decl (decl, 0);
7609
7610 if (TREE_STATIC (decl) && DECL_SIZE (decl) == NULL_TREE)
7611 {
7612 /* A static variable with an incomplete type:
7613 that is an error if it is initialized.
7614 Otherwise, let it through, but if it is not `extern'
7615 then it may cause an error message later. */
7616 if (DECL_INITIAL (decl) != NULL_TREE)
8251199e 7617 cp_error ("storage size of `%D' isn't known", decl);
8d08fdba
MS
7618 init = NULL_TREE;
7619 }
7620 else if (!DECL_EXTERNAL (decl) && DECL_SIZE (decl) == NULL_TREE)
7621 {
7622 /* An automatic variable with an incomplete type: that is an error.
7623 Don't talk about array types here, since we took care of that
7624 message in grokdeclarator. */
8251199e 7625 cp_error ("storage size of `%D' isn't known", decl);
8d08fdba
MS
7626 TREE_TYPE (decl) = error_mark_node;
7627 }
7628 else if (!DECL_EXTERNAL (decl) && IS_AGGR_TYPE (ttype))
7629 /* Let debugger know it should output info for this type. */
7630 note_debug_info_needed (ttype);
7631
6eb3bb27 7632 if (TREE_STATIC (decl) && DECL_CLASS_SCOPE_P (decl))
d2e5ee5c
MS
7633 note_debug_info_needed (DECL_CONTEXT (decl));
7634
8d08fdba
MS
7635 if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl))
7636 && DECL_SIZE (decl) != NULL_TREE
7637 && ! TREE_CONSTANT (DECL_SIZE (decl)))
7638 {
7639 if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
7640 constant_expression_warning (DECL_SIZE (decl));
7641 else
8251199e 7642 cp_error ("storage size of `%D' isn't constant", decl);
8d08fdba
MS
7643 }
7644
c91a56d2
MS
7645 if (! DECL_EXTERNAL (decl) && TYPE_NEEDS_DESTRUCTOR (type)
7646 /* Cleanups for static variables are handled by `finish_file'. */
7647 && ! TREE_STATIC (decl))
8d08fdba
MS
7648 {
7649 int yes = suspend_momentary ();
2ee887f2 7650 cleanup = maybe_build_cleanup (decl);
8d08fdba
MS
7651 resume_momentary (yes);
7652 }
7653 }
7654 /* PARM_DECLs get cleanups, too. */
7655 else if (TREE_CODE (decl) == PARM_DECL && TYPE_NEEDS_DESTRUCTOR (type))
7656 {
7657 if (temporary)
7658 end_temporary_allocation ();
7659 cleanup = maybe_build_cleanup (decl);
7660 if (temporary)
7661 resume_temporary_allocation ();
7662 }
7663
7664 /* Output the assembler code and/or RTL code for variables and functions,
7665 unless the type is an undefined structure or union.
7666 If not, it will get done when the type is completed. */
7667
5566b478
MS
7668 was_incomplete = (DECL_SIZE (decl) == NULL_TREE);
7669
8d08fdba
MS
7670 if (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == FUNCTION_DECL
7671 || TREE_CODE (decl) == RESULT_DECL)
7672 {
7673 /* ??? FIXME: What about nested classes? */
9188c363 7674 int toplev = toplevel_bindings_p ();
8d08fdba 7675 int was_temp
d22c8596 7676 = (TREE_STATIC (decl) && TYPE_NEEDS_DESTRUCTOR (type)
8d08fdba
MS
7677 && allocation_temporary_p ());
7678
7679 if (was_temp)
7680 end_temporary_allocation ();
7681
5f2c99c4
JM
7682 /* Static data in a function with comdat linkage also has comdat
7683 linkage. */
42976354
BK
7684 if (TREE_CODE (decl) == VAR_DECL
7685 && TREE_STATIC (decl)
5f2c99c4 7686 /* Don't mess with __FUNCTION__. */
aeb302bb 7687 && ! TREE_ASM_WRITTEN (decl)
42976354
BK
7688 && current_function_decl
7689 && DECL_CONTEXT (decl) == current_function_decl
5f2c99c4
JM
7690 && (DECL_THIS_INLINE (current_function_decl)
7691 || DECL_TEMPLATE_INSTANTIATION (current_function_decl))
893de33c 7692 && TREE_PUBLIC (current_function_decl))
42976354 7693 {
5f2c99c4
JM
7694 /* Rather than try to get this right with inlining, we suppress
7695 inlining of such functions. */
aeb302bb
JM
7696 current_function_cannot_inline
7697 = "function with static variable cannot be inline";
5f2c99c4
JM
7698
7699 /* If flag_weak, we don't need to mess with this, as we can just
7700 make the function weak, and let it refer to its unique local
7701 copy. This works because we don't allow the function to be
7702 inlined. */
7703 if (! flag_weak)
7704 {
7705 if (DECL_INTERFACE_KNOWN (current_function_decl))
7706 {
7707 TREE_PUBLIC (decl) = 1;
7708 DECL_EXTERNAL (decl) = DECL_EXTERNAL (current_function_decl);
7709 }
7710 else if (DECL_INITIAL (decl) == NULL_TREE
7711 || DECL_INITIAL (decl) == error_mark_node)
7712 {
7713 TREE_PUBLIC (decl) = 1;
7714 DECL_COMMON (decl) = 1;
7715 }
7716 /* else we lose. We can only do this if we can use common,
7717 which we can't if it has been initialized. */
7718
7719 if (TREE_PUBLIC (decl))
7720 DECL_ASSEMBLER_NAME (decl)
7721 = build_static_name (current_function_decl, DECL_NAME (decl));
7722 else if (! DECL_ARTIFICIAL (decl))
7723 {
7724 cp_warning_at ("sorry: semantics of inline function static data `%#D' are wrong (you'll wind up with multiple copies)", decl);
dee6497d 7725 cp_warning_at (" you can work around this by removing the initializer", decl);
5f2c99c4
JM
7726 }
7727 }
42976354
BK
7728 }
7729
77be6f82
JM
7730 else if (TREE_CODE (decl) == VAR_DECL
7731 && DECL_LANG_SPECIFIC (decl)
7732 && DECL_COMDAT (decl))
ea735e02
JM
7733 /* Set it up again; we might have set DECL_INITIAL since the
7734 last time. */
7735 comdat_linkage (decl);
77be6f82 7736
8d08fdba
MS
7737 if (TREE_CODE (decl) == VAR_DECL && DECL_VIRTUAL_P (decl))
7738 make_decl_rtl (decl, NULL_PTR, toplev);
7739 else if (TREE_CODE (decl) == VAR_DECL
7740 && TREE_READONLY (decl)
7741 && DECL_INITIAL (decl) != NULL_TREE
7742 && DECL_INITIAL (decl) != error_mark_node
a3203465 7743 && ! EMPTY_CONSTRUCTOR_P (DECL_INITIAL (decl)))
8d08fdba
MS
7744 {
7745 DECL_INITIAL (decl) = save_expr (DECL_INITIAL (decl));
7746
7747 if (asmspec)
7748 DECL_ASSEMBLER_NAME (decl) = get_identifier (asmspec);
7749
7750 if (! toplev
7751 && TREE_STATIC (decl)
7752 && ! TREE_SIDE_EFFECTS (decl)
7753 && ! TREE_PUBLIC (decl)
7754 && ! DECL_EXTERNAL (decl)
7755 && ! TYPE_NEEDS_DESTRUCTOR (type)
7756 && DECL_MODE (decl) != BLKmode)
7757 {
7758 /* If this variable is really a constant, then fill its DECL_RTL
7759 slot with something which won't take up storage.
7760 If something later should take its address, we can always give
7761 it legitimate RTL at that time. */
7762 DECL_RTL (decl) = gen_reg_rtx (DECL_MODE (decl));
7763 store_expr (DECL_INITIAL (decl), DECL_RTL (decl), 0);
7764 TREE_ASM_WRITTEN (decl) = 1;
7765 }
a0a33927 7766 else if (toplev && ! TREE_PUBLIC (decl))
8d08fdba 7767 {
8d08fdba 7768 /* If this is a static const, change its apparent linkage
db5ae43f 7769 if it belongs to a #pragma interface. */
a0a33927 7770 if (!interface_unknown)
8d08fdba
MS
7771 {
7772 TREE_PUBLIC (decl) = 1;
7773 DECL_EXTERNAL (decl) = interface_only;
7774 }
7775 make_decl_rtl (decl, asmspec, toplev);
7776 }
7777 else
5566b478 7778 rest_of_decl_compilation (decl, asmspec, toplev, at_eof);
8d08fdba
MS
7779 }
7780 else if (TREE_CODE (decl) == VAR_DECL
7781 && DECL_LANG_SPECIFIC (decl)
7782 && DECL_IN_AGGR_P (decl))
7783 {
7784 if (TREE_STATIC (decl))
7785 {
7786 if (init == NULL_TREE
7787#ifdef DEFAULT_STATIC_DEFS
7788 /* If this code is dead, then users must
7789 explicitly declare static member variables
7790 outside the class def'n as well. */
7791 && TYPE_NEEDS_CONSTRUCTING (type)
7792#endif
7793 )
7794 {
7795 DECL_EXTERNAL (decl) = 1;
7796 make_decl_rtl (decl, asmspec, 1);
7797 }
7798 else
5566b478 7799 rest_of_decl_compilation (decl, asmspec, toplev, at_eof);
8d08fdba
MS
7800 }
7801 else
7802 /* Just a constant field. Should not need any rtl. */
7803 goto finish_end0;
7804 }
7805 else
5566b478 7806 rest_of_decl_compilation (decl, asmspec, toplev, at_eof);
8d08fdba
MS
7807
7808 if (was_temp)
7809 resume_temporary_allocation ();
7810
a7a64a77
MM
7811 if (!abstract_virtuals_error (decl, core_type)
7812 && (TREE_CODE (type) == FUNCTION_TYPE
7813 || TREE_CODE (type) == METHOD_TYPE))
8d08fdba
MS
7814 abstract_virtuals_error (decl, TREE_TYPE (type));
7815
8d08fdba 7816 if (TREE_CODE (decl) == FUNCTION_DECL)
faae18ab 7817 ;
67d743fe
MS
7818 else if (DECL_EXTERNAL (decl)
7819 && ! (DECL_LANG_SPECIFIC (decl)
7820 && DECL_NOT_REALLY_EXTERN (decl)))
5566b478
MS
7821 {
7822 if (init)
7823 DECL_INITIAL (decl) = init;
7824 }
8d08fdba
MS
7825 else if (TREE_STATIC (decl) && type != error_mark_node)
7826 {
7827 /* Cleanups for static variables are handled by `finish_file'. */
f30432d7
MS
7828 if (TYPE_NEEDS_CONSTRUCTING (type) || init != NULL_TREE
7829 || TYPE_NEEDS_DESTRUCTOR (type))
8d08fdba 7830 expand_static_init (decl, init);
8d08fdba
MS
7831 }
7832 else if (! toplev)
7833 {
7834 /* This is a declared decl which must live until the
7835 end of the binding contour. It may need a cleanup. */
7836
7837 /* Recompute the RTL of a local array now
7838 if it used to be an incomplete type. */
7839 if (was_incomplete && ! TREE_STATIC (decl))
7840 {
7841 /* If we used it already as memory, it must stay in memory. */
7842 TREE_ADDRESSABLE (decl) = TREE_USED (decl);
7843 /* If it's still incomplete now, no init will save it. */
7844 if (DECL_SIZE (decl) == NULL_TREE)
7845 DECL_INITIAL (decl) = NULL_TREE;
7846 expand_decl (decl);
7847 }
7848 else if (! TREE_ASM_WRITTEN (decl)
7849 && (TYPE_SIZE (type) != NULL_TREE
7850 || TREE_CODE (type) == ARRAY_TYPE))
7851 {
7852 /* Do this here, because we did not expand this decl's
7853 rtl in start_decl. */
7854 if (DECL_RTL (decl) == NULL_RTX)
7855 expand_decl (decl);
7856 else if (cleanup)
7857 {
8d2733ca
MS
7858 /* XXX: Why don't we use decl here? */
7859 /* Ans: Because it was already expanded? */
e349ee73 7860 if (! expand_decl_cleanup (NULL_TREE, cleanup))
8251199e 7861 cp_error ("parser lost in parsing declaration of `%D'",
8d2733ca 7862 decl);
8d08fdba
MS
7863 /* Cleanup used up here. */
7864 cleanup = NULL_TREE;
7865 }
7866 }
7867
2ee887f2
MS
7868 if (current_binding_level->is_for_scope)
7869 {
f181d4ae
MM
7870 struct binding_level *outer
7871 = current_binding_level->level_chain;
2ee887f2
MS
7872
7873 /* Check to see if the same name is already bound at
7874 the outer level, either because it was directly declared,
7875 or because a dead for-decl got preserved. In either case,
d22c8596 7876 the code would not have been valid under the ARM
2ee887f2
MS
7877 scope rules, so clear is_for_scope for the
7878 current_binding_level.
7879
7880 Otherwise, we need to preserve the temp slot for decl
e92cc029 7881 to last into the outer binding level. */
2ee887f2 7882
f181d4ae
MM
7883 tree outer_binding
7884 = TREE_CHAIN (IDENTIFIER_BINDING (DECL_NAME (decl)));
7885
7886 if (outer_binding && BINDING_LEVEL (outer_binding) == outer
7887 && (TREE_CODE (BINDING_VALUE (outer_binding))
7888 == VAR_DECL)
7889 && DECL_DEAD_FOR_LOCAL (BINDING_VALUE (outer_binding)))
2ee887f2 7890 {
f181d4ae
MM
7891 BINDING_VALUE (outer_binding)
7892 = DECL_SHADOWED_FOR_VAR (BINDING_VALUE (outer_binding));
7893 current_binding_level->is_for_scope = 0;
2ee887f2 7894 }
f181d4ae
MM
7895 else if (DECL_IN_MEMORY_P (decl))
7896 preserve_temp_slots (DECL_RTL (decl));
2ee887f2
MS
7897 }
7898
eb66be0e 7899 expand_start_target_temps ();
72b7eeff 7900
8d08fdba
MS
7901 if (DECL_SIZE (decl) && type != error_mark_node)
7902 {
7903 /* Compute and store the initial value. */
7904 expand_decl_init (decl);
0c4b14c4 7905 already_used = TREE_USED (decl) || TREE_USED (type);
8d08fdba
MS
7906
7907 if (init || TYPE_NEEDS_CONSTRUCTING (type))
7908 {
a28e3c7f
MS
7909 emit_line_note (DECL_SOURCE_FILE (decl),
7910 DECL_SOURCE_LINE (decl));
b370501f 7911 expand_aggr_init (decl, init, flags);
8d08fdba
MS
7912 }
7913
00595019
MS
7914 /* Set this to 0 so we can tell whether an aggregate which
7915 was initialized was ever used. Don't do this if it has a
7916 destructor, so we don't complain about the 'resource
7917 allocation is initialization' idiom. */
249555b0 7918 /* Now set attribute((unused)) on types so decls of
38e01259 7919 that type will be marked used. (see TREE_USED, above.)
249555b0
BK
7920 This avoids the warning problems this particular code
7921 tried to work around. */
0c4b14c4 7922
be99da77 7923 if (TYPE_NEEDS_CONSTRUCTING (type)
0c4b14c4 7924 && ! already_used
be99da77
MS
7925 && cleanup == NULL_TREE
7926 && DECL_NAME (decl))
8d08fdba 7927 TREE_USED (decl) = 0;
0c4b14c4
JM
7928
7929 if (already_used)
7930 TREE_USED (decl) = 1;
934c6b13 7931 }
eb66be0e 7932
934c6b13 7933 /* Cleanup any temporaries needed for the initial value. */
eb66be0e 7934 expand_end_target_temps ();
8d08fdba 7935
934c6b13
MS
7936 if (DECL_SIZE (decl) && type != error_mark_node)
7937 {
8d08fdba
MS
7938 /* Store the cleanup, if there was one. */
7939 if (cleanup)
7940 {
e349ee73 7941 if (! expand_decl_cleanup (decl, cleanup))
8251199e 7942 cp_error ("parser lost in parsing declaration of `%D'",
a28e3c7f 7943 decl);
8d08fdba
MS
7944 }
7945 }
7946 }
7947 finish_end0:
7948
7949 /* Undo call to `pushclass' that was done in `start_decl'
7950 due to initialization of qualified member variable.
7951 I.e., Foo::x = 10; */
7952 {
f30432d7 7953 tree context = DECL_REAL_CONTEXT (decl);
8d08fdba
MS
7954 if (context
7955 && TREE_CODE_CLASS (TREE_CODE (context)) == 't'
7956 && (TREE_CODE (decl) == VAR_DECL
7957 /* We also have a pushclass done that we need to undo here
7958 if we're at top level and declare a method. */
5566b478
MS
7959 || TREE_CODE (decl) == FUNCTION_DECL)
7960 /* If size hasn't been set, we're still defining it,
7961 and therefore inside the class body; don't pop
7962 the binding level.. */
7963 && TYPE_SIZE (context) != NULL_TREE
7964 && context == current_class_type)
6b400b21 7965 pop_nested_class ();
8d08fdba
MS
7966 }
7967 }
7968
7969 finish_end:
7970
39211cd5
MS
7971 /* If requested, warn about definitions of large data objects. */
7972
7973 if (warn_larger_than
5156628f 7974 && ! processing_template_decl
39211cd5
MS
7975 && (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL)
7976 && !DECL_EXTERNAL (decl))
7977 {
7978 register tree decl_size = DECL_SIZE (decl);
7979
7980 if (decl_size && TREE_CODE (decl_size) == INTEGER_CST)
7981 {
7982 unsigned units = TREE_INT_CST_LOW (decl_size) / BITS_PER_UNIT;
7983
7984 if (units > larger_than_size)
7985 warning_with_decl (decl, "size of `%s' is %u bytes", units);
7986 }
7987 }
7988
8d08fdba
MS
7989 if (need_pop)
7990 {
7991 /* Resume permanent allocation, if not within a function. */
7992 /* The corresponding push_obstacks_nochange is in start_decl,
7993 start_method, groktypename, and in grokfield. */
7994 pop_obstacks ();
7995 }
7996
7997 if (was_readonly)
7998 TREE_READONLY (decl) = 1;
8d08fdba
MS
7999}
8000
82580166 8001/* This is here for a midend callback from c-common.c */
e92cc029 8002
82580166
MS
8003void
8004finish_decl (decl, init, asmspec_tree)
8005 tree decl, init;
8006 tree asmspec_tree;
8007{
8008 cp_finish_decl (decl, init, asmspec_tree, 1, 0);
8009}
8010
8d08fdba
MS
8011void
8012expand_static_init (decl, init)
8013 tree decl;
8014 tree init;
8015{
8016 tree oldstatic = value_member (decl, static_aggregates);
a4443a08 8017
8d08fdba
MS
8018 if (oldstatic)
8019 {
8020 if (TREE_PURPOSE (oldstatic) && init != NULL_TREE)
8251199e 8021 cp_error ("multiple initializations given for `%D'", decl);
8d08fdba 8022 }
0aafb128 8023 else if (! toplevel_bindings_p ())
8d08fdba
MS
8024 {
8025 /* Emit code to perform this initialization but once. */
8026 tree temp;
8027
e92cc029 8028 /* Remember this information until end of file. */
8d08fdba
MS
8029 push_obstacks (&permanent_obstack, &permanent_obstack);
8030
2036a15c
MM
8031 /* Emit code to perform this initialization but once. This code
8032 looks like:
8033
8034 static int temp = 0;
8035 if (!temp) {
8036 // Do initialization.
8037 temp = 1;
8038 // Register variable for destruction at end of program.
8039 }
8040
8041 Note that the `temp' variable is only set to 1 *after* the
8042 initialization is complete. This ensures that an exception,
8043 thrown during the construction, will cause the variable to
8044 reinitialized when we pass through this code again, as per:
8045
8046 [stmt.dcl]
8047
8048 If the initialization exits by throwing an exception, the
8049 initialization is not complete, so it will be tried again
8050 the next time control enters the declaration.
8051
8052 In theory, this process should be thread-safe, too; multiple
8053 threads should not be able to initialize the variable more
8054 than once. We don't yet attempt to ensure thread-safety. */
8d08fdba
MS
8055 temp = get_temp_name (integer_type_node, 1);
8056 rest_of_decl_compilation (temp, NULL_PTR, 0, 0);
2036a15c
MM
8057
8058 /* Begin the conditional initialization. */
8d08fdba 8059 expand_start_cond (build_binary_op (EQ_EXPR, temp,
337c90cc 8060 integer_zero_node), 0);
eb66be0e 8061 expand_start_target_temps ();
72b7eeff 8062
2036a15c 8063 /* Do the initialization itself. */
28cbf42c 8064 if (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl))
f30432d7 8065 || (init && TREE_CODE (init) == TREE_LIST))
8d08fdba 8066 {
b370501f 8067 expand_aggr_init (decl, init, 0);
8d08fdba
MS
8068 do_pending_stack_adjust ();
8069 }
f30432d7 8070 else if (init)
8d08fdba 8071 expand_assignment (decl, init, 0, 0);
f30432d7 8072
2036a15c
MM
8073 /* Set TEMP to 1. */
8074 expand_assignment (temp, integer_one_node, 0, 0);
8075
8076 /* Cleanup any temporaries needed for the initial value. If
8077 destroying one of the temporaries causes an exception to be
8078 thrown, then the object itself has still been fully
8079 constructed. */
eb66be0e 8080 expand_end_target_temps ();
72b7eeff 8081
2036a15c
MM
8082 /* Use atexit to register a function for destroying this static
8083 variable. */
72b7eeff
MS
8084 if (TYPE_NEEDS_DESTRUCTOR (TREE_TYPE (decl)))
8085 {
8086 tree cleanup, fcall;
8087 static tree Atexit = 0;
0045e0bc
MM
8088 int saved_flag_access_control;
8089
72b7eeff
MS
8090 if (Atexit == 0)
8091 {
8092 tree atexit_fndecl, PFV, pfvlist;
e92cc029 8093 /* Remember this information until end of file. */
72b7eeff
MS
8094 push_obstacks (&permanent_obstack, &permanent_obstack);
8095 PFV = build_pointer_type (build_function_type
8096 (void_type_node, void_list_node));
8097
8098 pfvlist = tree_cons (NULL_TREE, PFV, void_list_node);
8099
8100 push_lang_context (lang_name_c);
f30c84c9
MM
8101 /* Note that we do not call pushdecl for this function;
8102 there's no reason that this declaration should be
8103 accessible to anyone. */
beb53fb8 8104 atexit_fndecl
f30c84c9
MM
8105 = define_function ("atexit",
8106 build_function_type (void_type_node,
8107 pfvlist),
8108 NOT_BUILT_IN,
8109 /*pfn=*/0,
8110 NULL_PTR);
a6ecf8b6 8111 mark_used (atexit_fndecl);
72b7eeff
MS
8112 Atexit = default_conversion (atexit_fndecl);
8113 pop_lang_context ();
8114 pop_obstacks ();
8115 }
8116
7cfac595
MM
8117 /* Call build_cleanup before we enter the anonymous function
8118 so that any access checks will be done relative to the
8119 current scope, rather than the scope of the anonymous
8120 function. */
0045e0bc
MM
8121 build_cleanup (decl);
8122
8123 /* Now start the function. */
72b7eeff 8124 cleanup = start_anon_func ();
0045e0bc
MM
8125
8126 /* Now, recompute the cleanup. It may contain SAVE_EXPRs
8127 that refer to the original function, rather than the
8128 anonymous one. That will make the back-end think that
8129 nested functions are in use, which causes confusion. */
8130 saved_flag_access_control = flag_access_control;
8131 flag_access_control = 0;
8132 fcall = build_cleanup (decl);
8133 flag_access_control = saved_flag_access_control;
8134
8135 /* Finish off the function. */
7cfac595 8136 expand_expr_stmt (fcall);
72b7eeff 8137 end_anon_func ();
0045e0bc
MM
8138
8139 /* Call atexit with the cleanup function. */
72b7eeff
MS
8140 mark_addressable (cleanup);
8141 cleanup = build_unary_op (ADDR_EXPR, cleanup, 0);
0045e0bc
MM
8142 fcall = build_function_call (Atexit,
8143 expr_tree_cons (NULL_TREE,
8144 cleanup,
8145 NULL_TREE));
72b7eeff
MS
8146 expand_expr_stmt (fcall);
8147 }
8148
8d08fdba 8149 expand_end_cond ();
e92cc029 8150 /* Resume old (possibly temporary) allocation. */
8d08fdba
MS
8151 pop_obstacks ();
8152 }
8153 else
8154 {
0aafb128
MM
8155 /* This code takes into account memory allocation policy of
8156 `start_decl'. Namely, if TYPE_NEEDS_CONSTRUCTING does not
8157 hold for this object, then we must make permanent the storage
8158 currently in the temporary obstack. */
8159 if (!TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl)))
8d08fdba
MS
8160 preserve_initializer ();
8161 static_aggregates = perm_tree_cons (init, decl, static_aggregates);
8162 }
8163}
8164\f
8165/* Make TYPE a complete type based on INITIAL_VALUE.
8166 Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
6ab5c740 8167 2 if there was no information (in which case assume 0 if DO_DEFAULT). */
8d08fdba
MS
8168
8169int
8170complete_array_type (type, initial_value, do_default)
8171 tree type, initial_value;
8172 int do_default;
8173{
8174 register tree maxindex = NULL_TREE;
8175 int value = 0;
6ab5c740
NS
8176
8177 /* Allocate on the same obstack as TYPE. */
8178 push_obstacks (TYPE_OBSTACK (type), TYPE_OBSTACK (type));
8179
8d08fdba
MS
8180 if (initial_value)
8181 {
8182 /* Note MAXINDEX is really the maximum index,
8183 one less than the size. */
8184 if (TREE_CODE (initial_value) == STRING_CST)
e1cd6e56
MS
8185 {
8186 int eltsize
8187 = int_size_in_bytes (TREE_TYPE (TREE_TYPE (initial_value)));
8188 maxindex = build_int_2 ((TREE_STRING_LENGTH (initial_value)
8189 / eltsize) - 1, 0);
8190 }
8d08fdba
MS
8191 else if (TREE_CODE (initial_value) == CONSTRUCTOR)
8192 {
e1cd6e56
MS
8193 tree elts = CONSTRUCTOR_ELTS (initial_value);
8194 maxindex = size_binop (MINUS_EXPR, integer_zero_node, size_one_node);
8195 for (; elts; elts = TREE_CHAIN (elts))
8196 {
8197 if (TREE_PURPOSE (elts))
8198 maxindex = TREE_PURPOSE (elts);
8199 else
8200 maxindex = size_binop (PLUS_EXPR, maxindex, size_one_node);
8201 }
8202 maxindex = copy_node (maxindex);
8d08fdba
MS
8203 }
8204 else
8205 {
8206 /* Make an error message unless that happened already. */
8207 if (initial_value != error_mark_node)
8208 value = 1;
0db982be
ML
8209 else
8210 initial_value = NULL_TREE;
8d08fdba
MS
8211
8212 /* Prevent further error messages. */
8213 maxindex = build_int_2 (0, 0);
8214 }
8215 }
8216
8217 if (!maxindex)
8218 {
8219 if (do_default)
8220 maxindex = build_int_2 (0, 0);
8221 value = 2;
8222 }
8223
8224 if (maxindex)
8225 {
51c184be 8226 tree itype;
6ab5c740
NS
8227 tree domain;
8228
8229 domain = build_index_type (maxindex);
8230 TYPE_DOMAIN (type) = domain;
51c184be 8231
dff6b454 8232 if (! TREE_TYPE (maxindex))
6ab5c740 8233 TREE_TYPE (maxindex) = domain;
51c184be
MS
8234 if (initial_value)
8235 itype = TREE_TYPE (initial_value);
8236 else
8237 itype = NULL;
8238 if (itype && !TYPE_DOMAIN (itype))
6ab5c740 8239 TYPE_DOMAIN (itype) = domain;
dff6b454
RK
8240 /* The type of the main variant should never be used for arrays
8241 of different sizes. It should only ever be completed with the
8242 size of the array. */
8243 if (! TYPE_DOMAIN (TYPE_MAIN_VARIANT (type)))
6ab5c740 8244 TYPE_DOMAIN (TYPE_MAIN_VARIANT (type)) = domain;
8d08fdba
MS
8245 }
8246
6ab5c740
NS
8247 pop_obstacks();
8248
8d08fdba
MS
8249 /* Lay out the type now that we can get the real answer. */
8250
8251 layout_type (type);
8252
8253 return value;
8254}
8255\f
8256/* Return zero if something is declared to be a member of type
8257 CTYPE when in the context of CUR_TYPE. STRING is the error
8258 message to print in that case. Otherwise, quietly return 1. */
e92cc029 8259
8d08fdba
MS
8260static int
8261member_function_or_else (ctype, cur_type, string)
8262 tree ctype, cur_type;
d8e178a0 8263 const char *string;
8d08fdba
MS
8264{
8265 if (ctype && ctype != cur_type)
8266 {
8267 error (string, TYPE_NAME_STRING (ctype));
8268 return 0;
8269 }
8270 return 1;
8271}
8272\f
8273/* Subroutine of `grokdeclarator'. */
8274
8275/* Generate errors possibly applicable for a given set of specifiers.
8276 This is for ARM $7.1.2. */
e92cc029 8277
8d08fdba
MS
8278static void
8279bad_specifiers (object, type, virtualp, quals, inlinep, friendp, raises)
8280 tree object;
d8e178a0 8281 const char *type;
8d08fdba
MS
8282 int virtualp, quals, friendp, raises, inlinep;
8283{
8284 if (virtualp)
8251199e 8285 cp_error ("`%D' declared as a `virtual' %s", object, type);
8d08fdba 8286 if (inlinep)
8251199e 8287 cp_error ("`%D' declared as an `inline' %s", object, type);
8d08fdba 8288 if (quals)
8251199e 8289 cp_error ("`const' and `volatile' function specifiers on `%D' invalid in %s declaration",
a28e3c7f 8290 object, type);
8d08fdba 8291 if (friendp)
f8e55f34 8292 cp_error_at ("`%D' declared as a friend", object);
f3e4d63c 8293 if (raises)
f8e55f34 8294 cp_error_at ("`%D' declared with an exception specification", object);
8d08fdba
MS
8295}
8296
8297/* CTYPE is class type, or null if non-class.
8298 TYPE is type this FUNCTION_DECL should have, either FUNCTION_TYPE
8299 or METHOD_TYPE.
8300 DECLARATOR is the function's name.
8301 VIRTUALP is truthvalue of whether the function is virtual or not.
8302 FLAGS are to be passed through to `grokclassfn'.
8303 QUALS are qualifiers indicating whether the function is `const'
8304 or `volatile'.
8305 RAISES is a list of exceptions that this function can raise.
8306 CHECK is 1 if we must find this method in CTYPE, 0 if we should
3ddfb0e6
MM
8307 not look, and -1 if we should not call `grokclassfn' at all.
8308
20496fa2 8309 Returns `NULL_TREE' if something goes wrong, after issuing
3ddfb0e6 8310 applicable error messages. */
e92cc029 8311
8d08fdba 8312static tree
386b8a85 8313grokfndecl (ctype, type, declarator, orig_declarator, virtualp, flags, quals,
7a8f9fa9 8314 raises, check, friendp, publicp, inlinep, funcdef_flag,
2c73f9f5 8315 template_count, in_namespace)
8d08fdba
MS
8316 tree ctype, type;
8317 tree declarator;
386b8a85 8318 tree orig_declarator;
8d08fdba
MS
8319 int virtualp;
8320 enum overload_flags flags;
7a8f9fa9 8321 tree quals, raises;
386b8a85 8322 int check, friendp, publicp, inlinep, funcdef_flag, template_count;
2c73f9f5 8323 tree in_namespace;
8d08fdba
MS
8324{
8325 tree cname, decl;
8326 int staticp = ctype && TREE_CODE (type) == FUNCTION_TYPE;
856216bb 8327 int has_default_arg = 0;
42976354 8328 tree t;
8d08fdba
MS
8329
8330 if (ctype)
8331 cname = TREE_CODE (TYPE_NAME (ctype)) == TYPE_DECL
8332 ? TYPE_IDENTIFIER (ctype) : TYPE_NAME (ctype);
8333 else
8334 cname = NULL_TREE;
8335
8336 if (raises)
8337 {
f30432d7 8338 type = build_exception_variant (type, raises);
8d08fdba 8339 }
c11b6f21 8340
8d08fdba 8341 decl = build_lang_decl (FUNCTION_DECL, declarator, type);
2c73f9f5 8342 /* Propagate volatile out from type to decl. */
8d08fdba 8343 if (TYPE_VOLATILE (type))
893de33c 8344 TREE_THIS_VOLATILE (decl) = 1;
8d08fdba 8345
79c4d4b7 8346 /* If this decl has namespace scope, set that up. */
2c73f9f5 8347 if (in_namespace)
b262d64c 8348 set_decl_namespace (decl, in_namespace, friendp);
79c4d4b7
JM
8349 else if (publicp && ! ctype)
8350 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
2c73f9f5 8351
0f8766b8
JM
8352 /* `main' and builtins have implicit 'C' linkage. */
8353 if ((MAIN_NAME_P (declarator)
8354 || (IDENTIFIER_LENGTH (declarator) > 10
8355 && IDENTIFIER_POINTER (declarator)[0] == '_'
8356 && IDENTIFIER_POINTER (declarator)[1] == '_'
8357 && strncmp (IDENTIFIER_POINTER (declarator)+2, "builtin_", 8) == 0))
8358 && current_lang_name == lang_name_cplusplus
94706a5c 8359 && ctype == NULL_TREE
79c4d4b7
JM
8360 /* NULL_TREE means global namespace. */
8361 && DECL_CONTEXT (decl) == NULL_TREE)
0f8766b8
JM
8362 DECL_LANGUAGE (decl) = lang_c;
8363
8d08fdba
MS
8364 /* Should probably propagate const out from type to decl I bet (mrs). */
8365 if (staticp)
8366 {
8367 DECL_STATIC_FUNCTION_P (decl) = 1;
8368 DECL_CONTEXT (decl) = ctype;
8d08fdba
MS
8369 }
8370
e76a2646
MS
8371 if (ctype)
8372 DECL_CLASS_CONTEXT (decl) = ctype;
8373
0f8766b8 8374 if (ctype == NULL_TREE && DECL_MAIN_P (decl))
faae18ab 8375 {
848b92e1
JM
8376 if (processing_template_decl)
8377 error ("cannot declare `main' to be a template");
faae18ab 8378 if (inlinep)
8251199e 8379 error ("cannot declare `main' to be inline");
faae18ab 8380 else if (! publicp)
8251199e 8381 error ("cannot declare `main' to be static");
faae18ab
MS
8382 inlinep = 0;
8383 publicp = 1;
8384 }
50a6dbd7 8385
59e76fc6
JM
8386 /* Members of anonymous types and local classes have no linkage; make
8387 them internal. */
8388 if (ctype && (ANON_AGGRNAME_P (TYPE_IDENTIFIER (ctype))
8389 || hack_decl_function_context (TYPE_MAIN_DECL (ctype))))
50a6dbd7
JM
8390 publicp = 0;
8391
8392 if (publicp)
8393 {
8394 /* [basic.link]: A name with no linkage (notably, the name of a class
8395 or enumeration declared in a local scope) shall not be used to
8396 declare an entity with linkage.
8397
8398 Only check this for public decls for now. */
8399 t = no_linkage_check (TREE_TYPE (decl));
8400 if (t)
8401 {
7f7c930e
JM
8402 if (ANON_AGGRNAME_P (TYPE_IDENTIFIER (t)))
8403 {
8404 if (DECL_LANGUAGE (decl) == lang_c)
8405 /* Allow this; it's pretty common in C. */;
8406 else
8407 cp_pedwarn ("non-local function `%#D' uses anonymous type",
8408 decl);
8409 }
50a6dbd7 8410 else
8251199e 8411 cp_pedwarn ("non-local function `%#D' uses local type `%T'",
cce2be43 8412 decl, t);
50a6dbd7
JM
8413 }
8414 }
8415
893de33c 8416 TREE_PUBLIC (decl) = publicp;
faae18ab 8417 if (! publicp)
893de33c
JM
8418 {
8419 DECL_INTERFACE_KNOWN (decl) = 1;
8420 DECL_NOT_REALLY_EXTERN (decl) = 1;
8421 }
faae18ab
MS
8422
8423 if (inlinep)
8424 DECL_THIS_INLINE (decl) = DECL_INLINE (decl) = 1;
8d08fdba
MS
8425
8426 DECL_EXTERNAL (decl) = 1;
8427 if (quals != NULL_TREE && TREE_CODE (type) == FUNCTION_TYPE)
8428 {
8251199e 8429 cp_error ("%smember function `%D' cannot have `%T' method qualifier",
8d08fdba
MS
8430 (ctype ? "static " : "non-"), decl, TREE_VALUE (quals));
8431 quals = NULL_TREE;
8432 }
8433
8434 if (IDENTIFIER_OPNAME_P (DECL_NAME (decl)))
8435 grok_op_properties (decl, virtualp, check < 0);
8436
e76a2646 8437 if (ctype && hack_decl_function_context (decl))
893de33c 8438 DECL_NO_STATIC_CHAIN (decl) = 1;
e76a2646 8439
42976354
BK
8440 for (t = TYPE_ARG_TYPES (TREE_TYPE (decl)); t; t = TREE_CHAIN (t))
8441 if (TREE_PURPOSE (t)
8442 && TREE_CODE (TREE_PURPOSE (t)) == DEFAULT_ARG)
8443 {
856216bb 8444 has_default_arg = 1;
42976354
BK
8445 break;
8446 }
8447
f9d94ea4
JM
8448 if (friendp
8449 && TREE_CODE (orig_declarator) == TEMPLATE_ID_EXPR)
8450 {
8451 if (funcdef_flag)
8251199e
JM
8452 cp_error
8453 ("defining explicit specialization `%D' in friend declaration",
f9d94ea4
JM
8454 orig_declarator);
8455 else
8456 {
7e2421f7
MM
8457 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
8458 {
8459 /* Something like `template <class T> friend void f<T>()'. */
8251199e 8460 cp_error ("template-id `%D' in declaration of primary template",
7e2421f7 8461 orig_declarator);
20496fa2 8462 return NULL_TREE;
7e2421f7
MM
8463 }
8464
856216bb 8465
f9d94ea4
JM
8466 /* A friend declaration of the form friend void f<>(). Record
8467 the information in the TEMPLATE_ID_EXPR. */
8468 SET_DECL_IMPLICIT_INSTANTIATION (decl);
8469 DECL_TEMPLATE_INFO (decl)
8470 = perm_tree_cons (TREE_OPERAND (orig_declarator, 0),
8471 TREE_OPERAND (orig_declarator, 1),
8472 NULL_TREE);
856216bb
MM
8473
8474 if (has_default_arg)
8475 {
8476 cp_error ("default arguments are not allowed in declaration of friend template specialization `%D'",
8477 decl);
8478 return NULL_TREE;
8479 }
8480
8481 if (inlinep)
8482 {
8483 cp_error ("`inline' is not allowed in declaration of friend template specialization `%D'",
8484 decl);
8485 return NULL_TREE;
8486 }
f9d94ea4 8487 }
f84b4be9 8488 }
386b8a85 8489
856216bb
MM
8490 if (has_default_arg)
8491 add_defarg_fn (decl);
8492
0f8766b8
JM
8493 /* Plain overloading: will not be grok'd by grokclassfn. */
8494 if (! ctype && ! processing_template_decl
8495 && DECL_LANGUAGE (decl) != lang_c
8496 && (! DECL_USE_TEMPLATE (decl) || name_mangling_version < 1))
8497 set_mangled_name_for_decl (decl);
8498
1eb0072d
JM
8499 if (funcdef_flag)
8500 /* Make the init_value nonzero so pushdecl knows this is not
8501 tentative. error_mark_node is replaced later with the BLOCK. */
8502 DECL_INITIAL (decl) = error_mark_node;
8503
75650646 8504 /* Caller will do the rest of this. */
8d08fdba
MS
8505 if (check < 0)
8506 return decl;
8507
5566b478 8508 if (check && funcdef_flag)
d2e5ee5c 8509 DECL_INITIAL (decl) = error_mark_node;
5566b478 8510
8d08fdba
MS
8511 if (flags == NO_SPECIAL && ctype && constructor_name (cname) == declarator)
8512 {
8513 tree tmp;
8514 /* Just handle constructors here. We could do this
8515 inside the following if stmt, but I think
8516 that the code is more legible by breaking this
8517 case out. See comments below for what each of
8518 the following calls is supposed to do. */
8519 DECL_CONSTRUCTOR_P (decl) = 1;
8520
b370501f 8521 grokclassfn (ctype, decl, flags, quals);
386b8a85 8522
e1467ff2
MM
8523 decl = check_explicit_specialization (orig_declarator, decl,
8524 template_count,
f84b4be9
JM
8525 2 * (funcdef_flag != 0) +
8526 4 * (friendp != 0));
6c30752f 8527 if (decl == error_mark_node)
20496fa2 8528 return NULL_TREE;
75650646 8529
b370501f 8530 if ((! TYPE_FOR_JAVA (ctype) || check_java_method (decl))
eff71ab0 8531 && check)
5566b478
MS
8532 {
8533 tmp = check_classfn (ctype, decl);
98c1c668
JM
8534
8535 if (tmp && TREE_CODE (tmp) == TEMPLATE_DECL)
8536 tmp = DECL_TEMPLATE_RESULT(tmp);
8537
e349ee73 8538 if (tmp && DECL_ARTIFICIAL (tmp))
8251199e 8539 cp_error ("definition of implicitly-declared `%D'", tmp);
5566b478
MS
8540 if (tmp && duplicate_decls (decl, tmp))
8541 return tmp;
8542 }
a0a33927 8543 if (! grok_ctor_properties (ctype, decl))
20496fa2 8544 return NULL_TREE;
8d08fdba
MS
8545 }
8546 else
8547 {
8548 tree tmp;
8549
8550 /* Function gets the ugly name, field gets the nice one.
8551 This call may change the type of the function (because
8552 of default parameters)! */
8553 if (ctype != NULL_TREE)
b370501f 8554 grokclassfn (ctype, decl, flags, quals);
8d08fdba 8555
e1467ff2
MM
8556 decl = check_explicit_specialization (orig_declarator, decl,
8557 template_count,
f84b4be9
JM
8558 2 * (funcdef_flag != 0) +
8559 4 * (friendp != 0));
6c30752f 8560 if (decl == error_mark_node)
20496fa2 8561 return NULL_TREE;
6c30752f 8562
eff71ab0 8563 if (ctype != NULL_TREE
b370501f 8564 && (! TYPE_FOR_JAVA (ctype) || check_java_method (decl))
eff71ab0 8565 && check)
5566b478
MS
8566 {
8567 tmp = check_classfn (ctype, decl);
98c1c668
JM
8568
8569 if (tmp && TREE_CODE (tmp) == TEMPLATE_DECL)
75650646 8570 tmp = DECL_TEMPLATE_RESULT (tmp);
98c1c668 8571
5566b478
MS
8572 if (tmp && DECL_STATIC_FUNCTION_P (tmp)
8573 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE)
8574 {
8575 /* Remove the `this' parm added by grokclassfn.
8576 XXX Isn't this done in start_function, too? */
8577 revert_static_member_fn (&decl, NULL, NULL);
8578 last_function_parms = TREE_CHAIN (last_function_parms);
8579 }
e349ee73 8580 if (tmp && DECL_ARTIFICIAL (tmp))
8251199e 8581 cp_error ("definition of implicitly-declared `%D'", tmp);
7834ab39
MS
8582 if (tmp)
8583 {
03017874
MM
8584 /* Attempt to merge the declarations. This can fail, in
8585 the case of some illegal specialization declarations. */
7834ab39 8586 if (!duplicate_decls (decl, tmp))
03017874
MM
8587 cp_error ("no `%#D' member function declared in class `%T'",
8588 decl, ctype);
7834ab39
MS
8589 return tmp;
8590 }
5566b478 8591 }
8d08fdba
MS
8592
8593 if (ctype == NULL_TREE || check)
8594 return decl;
8595
8d08fdba
MS
8596 if (virtualp)
8597 {
2ee887f2 8598 DECL_VIRTUAL_P (decl) = 1;
8d08fdba
MS
8599 if (DECL_VINDEX (decl) == NULL_TREE)
8600 DECL_VINDEX (decl) = error_mark_node;
8601 IDENTIFIER_VIRTUAL_P (DECL_NAME (decl)) = 1;
8d08fdba
MS
8602 }
8603 }
8604 return decl;
8605}
8606
8607static tree
2c73f9f5 8608grokvardecl (type, declarator, specbits_in, initialized, constp, in_namespace)
8d08fdba
MS
8609 tree type;
8610 tree declarator;
d2e5ee5c 8611 RID_BIT_TYPE *specbits_in;
8d08fdba 8612 int initialized;
a9aedbc2 8613 int constp;
2c73f9f5 8614 tree in_namespace;
8d08fdba
MS
8615{
8616 tree decl;
f7da6097
MS
8617 RID_BIT_TYPE specbits;
8618
8619 specbits = *specbits_in;
8d08fdba
MS
8620
8621 if (TREE_CODE (type) == OFFSET_TYPE)
8622 {
8623 /* If you declare a static member so that it
8624 can be initialized, the code will reach here. */
5b605f68
MS
8625 tree basetype = TYPE_OFFSET_BASETYPE (type);
8626 type = TREE_TYPE (type);
4ce3d537 8627 decl = build_lang_decl (VAR_DECL, declarator, type);
5b605f68
MS
8628 DECL_CONTEXT (decl) = basetype;
8629 DECL_CLASS_CONTEXT (decl) = basetype;
f376e137 8630 DECL_ASSEMBLER_NAME (decl) = build_static_name (basetype, declarator);
8d08fdba
MS
8631 }
8632 else
30394414 8633 {
79c4d4b7
JM
8634 tree context;
8635
8636 if (in_namespace)
8637 context = in_namespace;
8638 else if (namespace_bindings_p () || RIDBIT_SETP (RID_EXTERN, specbits))
8639 context = current_namespace;
820fcad8
JM
8640 else
8641 context = NULL_TREE;
79c4d4b7 8642
9188c363
MM
8643 if (processing_template_decl)
8644 {
8645 /* If we're in a template, we need DECL_LANG_SPECIFIC so that
8646 we can call push_template_decl. */
8647 push_permanent_obstack ();
4ce3d537 8648 decl = build_lang_decl (VAR_DECL, declarator,
9188c363
MM
8649 complete_type (type));
8650 pop_obstacks ();
8651 }
8652 else
8653 decl = build_decl (VAR_DECL, declarator, complete_type (type));
79c4d4b7
JM
8654
8655 if (context)
b262d64c 8656 set_decl_namespace (decl, context, 0);
79c4d4b7
JM
8657
8658 context = DECL_CONTEXT (decl);
8659 if (declarator && context && current_lang_name != lang_name_c)
8660 DECL_ASSEMBLER_NAME (decl) = build_static_name (context, declarator);
30394414 8661 }
6060a796 8662
2c73f9f5 8663 if (in_namespace)
b262d64c 8664 set_decl_namespace (decl, in_namespace, 0);
2c73f9f5 8665
8d08fdba
MS
8666 if (RIDBIT_SETP (RID_EXTERN, specbits))
8667 {
8668 DECL_THIS_EXTERN (decl) = 1;
8669 DECL_EXTERNAL (decl) = !initialized;
8670 }
8671
8672 /* In class context, static means one per class,
8673 public access, and static storage. */
2b9dc906 8674 if (DECL_CLASS_SCOPE_P (decl))
8d08fdba
MS
8675 {
8676 TREE_PUBLIC (decl) = 1;
8677 TREE_STATIC (decl) = 1;
5b605f68 8678 DECL_EXTERNAL (decl) = 0;
8d08fdba
MS
8679 }
8680 /* At top level, either `static' or no s.c. makes a definition
8681 (perhaps tentative), and absence of `static' makes it public. */
a9aedbc2 8682 else if (toplevel_bindings_p ())
8d08fdba 8683 {
a9aedbc2 8684 TREE_PUBLIC (decl) = (RIDBIT_NOTSETP (RID_STATIC, specbits)
9fffd093 8685 && (DECL_THIS_EXTERN (decl) || ! constp));
8d08fdba
MS
8686 TREE_STATIC (decl) = ! DECL_EXTERNAL (decl);
8687 }
8688 /* Not at top level, only `static' makes a static definition. */
8689 else
8690 {
8691 TREE_STATIC (decl) = !! RIDBIT_SETP (RID_STATIC, specbits);
8692 TREE_PUBLIC (decl) = DECL_EXTERNAL (decl);
8693 }
50a6dbd7
JM
8694
8695 if (TREE_PUBLIC (decl))
8696 {
8697 /* [basic.link]: A name with no linkage (notably, the name of a class
8698 or enumeration declared in a local scope) shall not be used to
8699 declare an entity with linkage.
8700
8701 Only check this for public decls for now. */
8702 tree t = no_linkage_check (TREE_TYPE (decl));
8703 if (t)
8704 {
8705 if (ANON_AGGRNAME_P (TYPE_IDENTIFIER (t)))
8706 /* Ignore for now; `enum { foo } e' is pretty common. */;
8707 else
8251199e 8708 cp_pedwarn ("non-local variable `%#D' uses local type `%T'",
50a6dbd7
JM
8709 decl, t);
8710 }
8711 }
8712
8d08fdba
MS
8713 return decl;
8714}
8715
d8f8dca1
MM
8716/* Create and return a canonical pointer to member function type, for
8717 TYPE, which is a POINTER_TYPE to a METHOD_TYPE. */
8d08fdba
MS
8718
8719tree
8720build_ptrmemfunc_type (type)
8721 tree type;
8722{
8723 tree fields[4];
8724 tree t;
8725 tree u;
46cbda4a 8726 tree unqualified_variant = NULL_TREE;
8d08fdba
MS
8727
8728 /* If a canonical type already exists for this type, use it. We use
8729 this method instead of type_hash_canon, because it only does a
8730 simple equality check on the list of field members. */
8731
8732 if ((t = TYPE_GET_PTRMEMFUNC_TYPE (type)))
8733 return t;
8734
46cbda4a
MM
8735 /* Make sure that we always have the unqualified pointer-to-member
8736 type first. */
8737 if (CP_TYPE_QUALS (type) != TYPE_UNQUALIFIED)
8738 unqualified_variant
8739 = build_ptrmemfunc_type (TYPE_MAIN_VARIANT (type));
8740
8d08fdba
MS
8741 push_obstacks (TYPE_OBSTACK (type), TYPE_OBSTACK (type));
8742
8743 u = make_lang_type (UNION_TYPE);
7ddedda4 8744 SET_IS_AGGR_TYPE (u, 0);
4ce3d537
MM
8745 fields[0] = build_lang_decl (FIELD_DECL, pfn_identifier, type);
8746 fields[1] = build_lang_decl (FIELD_DECL, delta2_identifier,
8747 delta_type_node);
8d08fdba
MS
8748 finish_builtin_type (u, "__ptrmemfunc_type", fields, 1, ptr_type_node);
8749 TYPE_NAME (u) = NULL_TREE;
8750
8751 t = make_lang_type (RECORD_TYPE);
8752
2c73f9f5 8753 /* Let the front-end know this is a pointer to member function... */
db5ae43f 8754 TYPE_PTRMEMFUNC_FLAG (t) = 1;
2c73f9f5 8755 /* ... and not really an aggregate. */
7ddedda4 8756 SET_IS_AGGR_TYPE (t, 0);
8d08fdba 8757
4ce3d537
MM
8758 fields[0] = build_lang_decl (FIELD_DECL, delta_identifier,
8759 delta_type_node);
8760 fields[1] = build_lang_decl (FIELD_DECL, index_identifier,
8761 delta_type_node);
8762 fields[2] = build_lang_decl (FIELD_DECL, pfn_or_delta2_identifier, u);
8d08fdba
MS
8763 finish_builtin_type (t, "__ptrmemfunc_type", fields, 2, ptr_type_node);
8764
8765 pop_obstacks ();
8766
8767 /* Zap out the name so that the back-end will give us the debugging
8768 information for this anonymous RECORD_TYPE. */
8769 TYPE_NAME (t) = NULL_TREE;
8770
46cbda4a
MM
8771 /* If this is not the unqualified form of this pointer-to-member
8772 type, set the TYPE_MAIN_VARIANT for this type to be the
8773 unqualified type. Since they are actually RECORD_TYPEs that are
8774 not variants of each other, we must do this manually. */
8775 if (CP_TYPE_QUALS (type) != TYPE_UNQUALIFIED)
8776 {
8777 t = build_qualified_type (t, CP_TYPE_QUALS (type));
8778 TYPE_MAIN_VARIANT (t) = unqualified_variant;
8779 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (unqualified_variant);
8780 TYPE_NEXT_VARIANT (unqualified_variant) = t;
8781 }
8782
8783 /* Cache this pointer-to-member type so that we can find it again
8784 later. */
8d08fdba
MS
8785 TYPE_SET_PTRMEMFUNC_TYPE (type, t);
8786
e92cc029 8787 /* Seems to be wanted. */
8d08fdba 8788 CLASSTYPE_GOT_SEMICOLON (t) = 1;
46cbda4a 8789
8d08fdba
MS
8790 return t;
8791}
8792
b17e2870
JM
8793/* DECL is a VAR_DECL defined in-class, whose TYPE is also given.
8794 Check to see that the definition is valid. Issue appropriate error
8795 messages. Return 1 if the definition is particularly bad, or 0
8796 otherwise. */
8797
8798int
8799check_static_variable_definition (decl, type)
8800 tree decl;
8801 tree type;
8802{
8803 /* Motion 10 at San Diego: If a static const integral data member is
8804 initialized with an integral constant expression, the initializer
8805 may appear either in the declaration (within the class), or in
8806 the definition, but not both. If it appears in the class, the
8807 member is a member constant. The file-scope definition is always
8808 required. */
8809 if (CLASS_TYPE_P (type) || TREE_CODE (type) == REFERENCE_TYPE)
8810 {
8811 cp_error ("in-class initialization of static data member of non-integral type `%T'",
8812 type);
8813 /* If we just return the declaration, crashes will sometimes
8814 occur. We therefore return void_type_node, as if this was a
8815 friend declaration, to cause callers to completely ignore
8816 this declaration. */
8817 return 1;
8818 }
8819 else if (!CP_TYPE_CONST_P (type))
8820 cp_error ("ANSI C++ forbids in-class initialization of non-const static member `%D'",
8821 decl);
8822 else if (pedantic && !INTEGRAL_TYPE_P (type))
8823 cp_pedwarn ("ANSI C++ forbids initialization of member constant `%D' of non-integral type `%T'", decl, type);
8824
8825 return 0;
8826}
8827
8d08fdba
MS
8828/* Given declspecs and a declarator,
8829 determine the name and type of the object declared
8830 and construct a ..._DECL node for it.
8831 (In one case we can return a ..._TYPE node instead.
8832 For invalid input we sometimes return 0.)
8833
8834 DECLSPECS is a chain of tree_list nodes whose value fields
8835 are the storage classes and type specifiers.
8836
8837 DECL_CONTEXT says which syntactic context this declaration is in:
8838 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
8839 FUNCDEF for a function definition. Like NORMAL but a few different
8840 error messages in each case. Return value may be zero meaning
8841 this definition is too screwy to try to parse.
8842 MEMFUNCDEF for a function definition. Like FUNCDEF but prepares to
8843 handle member functions (which have FIELD context).
8844 Return value may be zero meaning this definition is too screwy to
8845 try to parse.
8846 PARM for a parameter declaration (either within a function prototype
8847 or before a function body). Make a PARM_DECL, or return void_type_node.
db5ae43f 8848 CATCHPARM for a parameter declaration before a catch clause.
8d08fdba
MS
8849 TYPENAME if for a typename (in a cast or sizeof).
8850 Don't make a DECL node; just return the ..._TYPE node.
8851 FIELD for a struct or union field; make a FIELD_DECL.
8852 BITFIELD for a field with specified width.
8853 INITIALIZED is 1 if the decl has an initializer.
8854
b17e2870
JM
8855 ATTRLIST is a TREE_LIST node with prefix attributes in TREE_VALUE and
8856 normal attributes in TREE_PURPOSE, or NULL_TREE.
8857
8d08fdba
MS
8858 In the TYPENAME case, DECLARATOR is really an absolute declarator.
8859 It may also be so in the PARM case, for a prototype where the
8860 argument type is specified but not the name.
8861
8862 This function is where the complicated C meanings of `static'
8863 and `extern' are interpreted.
8864
8865 For C++, if there is any monkey business to do, the function which
8866 calls this one must do it, i.e., prepending instance variables,
8867 renaming overloaded function names, etc.
8868
8869 Note that for this C++, it is an error to define a method within a class
8870 which does not belong to that class.
8871
8872 Except in the case where SCOPE_REFs are implicitly known (such as
8873 methods within a class being redundantly qualified),
8874 declarations which involve SCOPE_REFs are returned as SCOPE_REFs
8875 (class_name::decl_name). The caller must also deal with this.
8876
8877 If a constructor or destructor is seen, and the context is FIELD,
8878 then the type gains the attribute TREE_HAS_x. If such a declaration
8879 is erroneous, NULL_TREE is returned.
8880
8881 QUALS is used only for FUNCDEF and MEMFUNCDEF cases. For a member
8882 function, these are the qualifiers to give to the `this' pointer.
8883
8884 May return void_type_node if the declarator turned out to be a friend.
8885 See grokfield for details. */
8886
8887enum return_types { return_normal, return_ctor, return_dtor, return_conversion };
8888
8889tree
c11b6f21 8890grokdeclarator (declarator, declspecs, decl_context, initialized, attrlist)
8d08fdba
MS
8891 tree declspecs;
8892 tree declarator;
8893 enum decl_context decl_context;
8894 int initialized;
c11b6f21 8895 tree attrlist;
8d08fdba
MS
8896{
8897 RID_BIT_TYPE specbits;
8898 int nclasses = 0;
8899 tree spec;
8900 tree type = NULL_TREE;
8901 int longlong = 0;
8902 int constp;
91063b51 8903 int restrictp;
8d08fdba 8904 int volatilep;
91063b51 8905 int type_quals;
db5ae43f 8906 int virtualp, explicitp, friendp, inlinep, staticp;
8d08fdba
MS
8907 int explicit_int = 0;
8908 int explicit_char = 0;
37c46b43 8909 int defaulted_int = 0;
8d08fdba 8910 tree typedef_decl = NULL_TREE;
9c0758dd 8911 const char *name;
8d08fdba
MS
8912 tree typedef_type = NULL_TREE;
8913 int funcdef_flag = 0;
8914 enum tree_code innermost_code = ERROR_MARK;
8915 int bitfield = 0;
6125f3be
DE
8916#if 0
8917 /* See the code below that used this. */
f6abb50a 8918 tree decl_machine_attr = NULL_TREE;
6125f3be 8919#endif
8d08fdba
MS
8920 /* Set this to error_mark_node for FIELD_DECLs we could not handle properly.
8921 All FIELD_DECLs we build here have `init' put into their DECL_INITIAL. */
8922 tree init = NULL_TREE;
8923
8924 /* Keep track of what sort of function is being processed
8925 so that we can warn about default return values, or explicit
8926 return values which do not match prescribed defaults. */
8927 enum return_types return_type = return_normal;
8928
8929 tree dname = NULL_TREE;
8930 tree ctype = current_class_type;
8931 tree ctor_return_type = NULL_TREE;
8932 enum overload_flags flags = NO_SPECIAL;
8d08fdba 8933 tree quals = NULL_TREE;
c11b6f21 8934 tree raises = NULL_TREE;
386b8a85 8935 int template_count = 0;
2c73f9f5 8936 tree in_namespace = NULL_TREE;
1eab9b56
JM
8937 tree inner_attrs;
8938 int ignore_attrs;
8d08fdba
MS
8939
8940 RIDBIT_RESET_ALL (specbits);
8941 if (decl_context == FUNCDEF)
8942 funcdef_flag = 1, decl_context = NORMAL;
8943 else if (decl_context == MEMFUNCDEF)
8944 funcdef_flag = -1, decl_context = FIELD;
8945 else if (decl_context == BITFIELD)
8946 bitfield = 1, decl_context = FIELD;
8947
8d08fdba
MS
8948 /* Look inside a declarator for the name being declared
8949 and get it as a string, for an error message. */
8950 {
be99da77
MS
8951 tree *next = &declarator;
8952 register tree decl;
8d08fdba
MS
8953 name = NULL;
8954
be99da77
MS
8955 while (next && *next)
8956 {
8957 decl = *next;
8958 switch (TREE_CODE (decl))
8d08fdba 8959 {
52fbc847
JM
8960 case TREE_LIST:
8961 /* For attributes. */
8962 next = &TREE_VALUE (decl);
8963 break;
8964
be99da77
MS
8965 case COND_EXPR:
8966 ctype = NULL_TREE;
8967 next = &TREE_OPERAND (decl, 0);
8968 break;
8d08fdba 8969
2c73f9f5 8970 case BIT_NOT_EXPR: /* For C++ destructors! */
8d08fdba 8971 {
be99da77
MS
8972 tree name = TREE_OPERAND (decl, 0);
8973 tree rename = NULL_TREE;
8974
8975 my_friendly_assert (flags == NO_SPECIAL, 152);
8976 flags = DTOR_FLAG;
8977 return_type = return_dtor;
5566b478
MS
8978 if (TREE_CODE (name) == TYPE_DECL)
8979 TREE_OPERAND (decl, 0) = name = constructor_name (name);
be99da77
MS
8980 my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, 153);
8981 if (ctype == NULL_TREE)
8982 {
8983 if (current_class_type == NULL_TREE)
8984 {
8251199e 8985 error ("destructors must be member functions");
be99da77
MS
8986 flags = NO_SPECIAL;
8987 }
8988 else
8989 {
8990 tree t = constructor_name (current_class_name);
8991 if (t != name)
8992 rename = t;
8993 }
8994 }
8d08fdba 8995 else
be99da77
MS
8996 {
8997 tree t = constructor_name (ctype);
8998 if (t != name)
8999 rename = t;
9000 }
51c184be 9001
be99da77 9002 if (rename)
39211cd5 9003 {
8251199e 9004 cp_error ("destructor `%T' must match class name `%T'",
5566b478 9005 name, rename);
be99da77 9006 TREE_OPERAND (decl, 0) = rename;
39211cd5 9007 }
be99da77 9008 next = &name;
51c184be 9009 }
be99da77 9010 break;
8d08fdba 9011
be99da77 9012 case ADDR_EXPR: /* C++ reference declaration */
2c73f9f5 9013 /* Fall through. */
be99da77
MS
9014 case ARRAY_REF:
9015 case INDIRECT_REF:
9016 ctype = NULL_TREE;
9017 innermost_code = TREE_CODE (decl);
9018 next = &TREE_OPERAND (decl, 0);
9019 break;
8d08fdba 9020
be99da77 9021 case CALL_EXPR:
43f887f9 9022 if (parmlist_is_exprlist (CALL_DECLARATOR_PARMS (decl)))
8d08fdba 9023 {
b17e2870
JM
9024 /* This is actually a variable declaration using
9025 constructor syntax. We need to call start_decl and
9026 cp_finish_decl so we can get the variable
9027 initialized... */
9028
9029 tree attributes, prefix_attributes;
be99da77
MS
9030
9031 *next = TREE_OPERAND (decl, 0);
43f887f9 9032 init = CALL_DECLARATOR_PARMS (decl);
be99da77 9033
b17e2870
JM
9034 if (attrlist)
9035 {
9036 attributes = TREE_PURPOSE (attrlist);
9037 prefix_attributes = TREE_VALUE (attrlist);
9038 }
9039 else
9040 {
9041 attributes = NULL_TREE;
9042 prefix_attributes = NULL_TREE;
9043 }
9044
9045 decl = start_decl (declarator, declspecs, 1,
9046 attributes, prefix_attributes);
1eb0072d
JM
9047 if (decl)
9048 {
9049 /* Look for __unused__ attribute */
9050 if (TREE_USED (TREE_TYPE (decl)))
9051 TREE_USED (decl) = 1;
9052 finish_decl (decl, init, NULL_TREE);
9053 }
9054 else
9055 cp_error ("invalid declarator");
be99da77 9056 return 0;
8d08fdba 9057 }
be99da77
MS
9058 innermost_code = TREE_CODE (decl);
9059 if (decl_context == FIELD && ctype == NULL_TREE)
9060 ctype = current_class_type;
45537677 9061 if (ctype
c11b6f21 9062 && TREE_OPERAND (decl, 0)
45537677
MS
9063 && (TREE_CODE (TREE_OPERAND (decl, 0)) == TYPE_DECL
9064 && ((DECL_NAME (TREE_OPERAND (decl, 0))
9065 == constructor_name_full (ctype))
9066 || (DECL_NAME (TREE_OPERAND (decl, 0))
9067 == constructor_name (ctype)))))
be99da77
MS
9068 TREE_OPERAND (decl, 0) = constructor_name (ctype);
9069 next = &TREE_OPERAND (decl, 0);
9070 decl = *next;
9071 if (ctype != NULL_TREE
9072 && decl != NULL_TREE && flags != DTOR_FLAG
9073 && decl == constructor_name (ctype))
8d08fdba 9074 {
be99da77
MS
9075 return_type = return_ctor;
9076 ctor_return_type = ctype;
8d08fdba 9077 }
be99da77
MS
9078 ctype = NULL_TREE;
9079 break;
386b8a85
JM
9080
9081 case TEMPLATE_ID_EXPR:
9082 {
9083 tree fns = TREE_OPERAND (decl, 0);
9084
9085 if (TREE_CODE (fns) == LOOKUP_EXPR)
9086 fns = TREE_OPERAND (fns, 0);
9087
8f032717
MM
9088 dname = fns;
9089 if (TREE_CODE (dname) == COMPONENT_REF)
9090 dname = TREE_OPERAND (dname, 1);
9091 if (TREE_CODE (dname) != IDENTIFIER_NODE)
9092 {
9093 my_friendly_assert (is_overloaded_fn (dname),
9094 19990331);
9095 dname = DECL_NAME (get_first_fn (dname));
9096 }
386b8a85 9097 }
2c73f9f5 9098 /* Fall through. */
be99da77
MS
9099
9100 case IDENTIFIER_NODE:
386b8a85
JM
9101 if (TREE_CODE (decl) == IDENTIFIER_NODE)
9102 dname = decl;
9103
be99da77
MS
9104 next = 0;
9105
9106 if (is_rid (dname))
8d08fdba 9107 {
8251199e 9108 cp_error ("declarator-id missing; using reserved word `%D'",
be99da77
MS
9109 dname);
9110 name = IDENTIFIER_POINTER (dname);
8d08fdba 9111 }
be99da77 9112 if (! IDENTIFIER_OPNAME_P (dname)
956d6950 9113 /* GNU/Linux headers use '__op'. Arrgh. */
a703fb38 9114 || (IDENTIFIER_TYPENAME_P (dname) && ! TREE_TYPE (dname)))
be99da77 9115 name = IDENTIFIER_POINTER (dname);
8d08fdba
MS
9116 else
9117 {
be99da77 9118 if (IDENTIFIER_TYPENAME_P (dname))
8d08fdba 9119 {
be99da77
MS
9120 my_friendly_assert (flags == NO_SPECIAL, 154);
9121 flags = TYPENAME_FLAG;
9122 ctor_return_type = TREE_TYPE (dname);
9123 return_type = return_conversion;
8d08fdba 9124 }
be99da77 9125 name = operator_name_string (dname);
8d08fdba 9126 }
be99da77 9127 break;
8d08fdba 9128
be99da77
MS
9129 /* C++ extension */
9130 case SCOPE_REF:
9131 {
9132 /* Perform error checking, and decide on a ctype. */
9133 tree cname = TREE_OPERAND (decl, 0);
9134 if (cname == NULL_TREE)
9135 ctype = NULL_TREE;
2c73f9f5
ML
9136 else if (TREE_CODE (cname) == NAMESPACE_DECL)
9137 {
9138 ctype = NULL_TREE;
9139 in_namespace = TREE_OPERAND (decl, 0);
9140 TREE_OPERAND (decl, 0) = NULL_TREE;
9141 }
be99da77
MS
9142 else if (! is_aggr_type (cname, 1))
9143 TREE_OPERAND (decl, 0) = NULL_TREE;
9144 /* Must test TREE_OPERAND (decl, 1), in case user gives
9145 us `typedef (class::memfunc)(int); memfunc *memfuncptr;' */
9146 else if (TREE_OPERAND (decl, 1)
9147 && TREE_CODE (TREE_OPERAND (decl, 1)) == INDIRECT_REF)
9148 ctype = cname;
73b0fce8
KL
9149 else if (TREE_CODE (cname) == TEMPLATE_TYPE_PARM
9150 || TREE_CODE (cname) == TEMPLATE_TEMPLATE_PARM)
5566b478 9151 {
8251199e 9152 cp_error ("`%T::%D' is not a valid declarator", cname,
5566b478 9153 TREE_OPERAND (decl, 1));
8251199e 9154 cp_error (" perhaps you want `typename %T::%D' to make it a type",
5566b478
MS
9155 cname, TREE_OPERAND (decl, 1));
9156 return void_type_node;
9157 }
be99da77
MS
9158 else if (ctype == NULL_TREE)
9159 ctype = cname;
9160 else if (TREE_COMPLEXITY (decl) == current_class_depth)
9161 TREE_OPERAND (decl, 0) = ctype;
9162 else
9163 {
9164 if (! UNIQUELY_DERIVED_FROM_P (cname, ctype))
9165 {
8251199e 9166 cp_error ("type `%T' is not derived from type `%T'",
be99da77
MS
9167 cname, ctype);
9168 TREE_OPERAND (decl, 0) = NULL_TREE;
9169 }
9170 else
9171 ctype = cname;
9172 }
9173
c91a56d2
MS
9174 if (ctype && TREE_CODE (TREE_OPERAND (decl, 1)) == TYPE_DECL
9175 && ((DECL_NAME (TREE_OPERAND (decl, 1))
9176 == constructor_name_full (ctype))
9177 || (DECL_NAME (TREE_OPERAND (decl, 1))
9178 == constructor_name (ctype))))
be99da77
MS
9179 TREE_OPERAND (decl, 1) = constructor_name (ctype);
9180 next = &TREE_OPERAND (decl, 1);
9181 decl = *next;
9182 if (ctype)
9183 {
9184 if (TREE_CODE (decl) == IDENTIFIER_NODE
9185 && constructor_name (ctype) == decl)
9186 {
9187 return_type = return_ctor;
9188 ctor_return_type = ctype;
9189 }
9190 else if (TREE_CODE (decl) == BIT_NOT_EXPR
9191 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
9192 && (constructor_name (ctype) == TREE_OPERAND (decl, 0)
9193 || constructor_name_full (ctype) == TREE_OPERAND (decl, 0)))
9194 {
9195 return_type = return_dtor;
9196 ctor_return_type = ctype;
9197 flags = DTOR_FLAG;
9198 TREE_OPERAND (decl, 0) = constructor_name (ctype);
9199 next = &TREE_OPERAND (decl, 0);
9200 }
9201 }
9202 }
9203 break;
9204
9205 case ERROR_MARK:
9206 next = 0;
9207 break;
9208
45537677
MS
9209 case TYPE_DECL:
9210 /* Parse error puts this typespec where
9211 a declarator should go. */
8251199e 9212 cp_error ("`%T' specified as declarator-id", DECL_NAME (decl));
45537677 9213 if (TREE_TYPE (decl) == current_class_type)
8251199e 9214 cp_error (" perhaps you want `%T' for a constructor",
45537677
MS
9215 current_class_name);
9216 dname = DECL_NAME (decl);
9217 name = IDENTIFIER_POINTER (dname);
9218
e92cc029 9219 /* Avoid giving two errors for this. */
45537677
MS
9220 IDENTIFIER_CLASS_VALUE (dname) = NULL_TREE;
9221
9222 declspecs = temp_tree_cons (NULL_TREE, integer_type_node,
9223 declspecs);
9224 *next = dname;
9225 next = 0;
9226 break;
9227
be99da77 9228 default:
8251199e 9229 cp_compiler_error ("`%D' as declarator", decl);
be99da77 9230 return 0; /* We used to do a 155 abort here. */
8d08fdba 9231 }
be99da77 9232 }
8d08fdba
MS
9233 if (name == NULL)
9234 name = "type name";
9235 }
9236
9237 /* A function definition's declarator must have the form of
9238 a function declarator. */
9239
9240 if (funcdef_flag && innermost_code != CALL_EXPR)
9241 return 0;
9242
e1cd6e56
MS
9243 if (((dname && IDENTIFIER_OPNAME_P (dname)) || flags == TYPENAME_FLAG)
9244 && innermost_code != CALL_EXPR
9245 && ! (ctype && declspecs == NULL_TREE))
9246 {
8251199e 9247 cp_error ("declaration of `%D' as non-function", dname);
e1cd6e56
MS
9248 return void_type_node;
9249 }
9250
8d08fdba
MS
9251 /* Anything declared one level down from the top level
9252 must be one of the parameters of a function
9253 (because the body is at least two levels down). */
9254
9255 /* This heuristic cannot be applied to C++ nodes! Fixed, however,
9256 by not allowing C++ class definitions to specify their parameters
9257 with xdecls (must be spec.d in the parmlist).
9258
9259 Since we now wait to push a class scope until we are sure that
9260 we are in a legitimate method context, we must set oldcname
a9aedbc2
MS
9261 explicitly (since current_class_name is not yet alive).
9262
9263 We also want to avoid calling this a PARM if it is in a namespace. */
8d08fdba 9264
9188c363 9265 if (decl_context == NORMAL && !toplevel_bindings_p ())
a9aedbc2
MS
9266 {
9267 struct binding_level *b = current_binding_level;
9268 current_binding_level = b->level_chain;
9269 if (current_binding_level != 0 && toplevel_bindings_p ())
9270 decl_context = PARM;
9271 current_binding_level = b;
9272 }
8d08fdba
MS
9273
9274 /* Look through the decl specs and record which ones appear.
9275 Some typespecs are defined as built-in typenames.
9276 Others, the ones that are modifiers of other types,
9277 are represented by bits in SPECBITS: set the bits for
9278 the modifiers that appear. Storage class keywords are also in SPECBITS.
9279
9280 If there is a typedef name or a type, store the type in TYPE.
9281 This includes builtin typedefs such as `int'.
9282
9283 Set EXPLICIT_INT if the type is `int' or `char' and did not
9284 come from a user typedef.
9285
9286 Set LONGLONG if `long' is mentioned twice.
9287
9288 For C++, constructors and destructors have their own fast treatment. */
9289
9290 for (spec = declspecs; spec; spec = TREE_CHAIN (spec))
9291 {
9292 register int i;
9293 register tree id;
9294
9295 /* Certain parse errors slip through. For example,
9296 `int class;' is not caught by the parser. Try
9297 weakly to recover here. */
9298 if (TREE_CODE (spec) != TREE_LIST)
9299 return 0;
9300
9301 id = TREE_VALUE (spec);
9302
9303 if (TREE_CODE (id) == IDENTIFIER_NODE)
9304 {
a3203465
MS
9305 if (id == ridpointers[(int) RID_INT]
9306 || id == ridpointers[(int) RID_CHAR]
9307 || id == ridpointers[(int) RID_BOOL]
9308 || id == ridpointers[(int) RID_WCHAR])
8d08fdba
MS
9309 {
9310 if (type)
8ccc31eb
MS
9311 {
9312 if (id == ridpointers[(int) RID_BOOL])
8251199e 9313 error ("`bool' is now a keyword");
8ccc31eb 9314 else
8251199e 9315 cp_error ("extraneous `%T' ignored", id);
8ccc31eb 9316 }
8d08fdba
MS
9317 else
9318 {
a3203465
MS
9319 if (id == ridpointers[(int) RID_INT])
9320 explicit_int = 1;
9321 else if (id == ridpointers[(int) RID_CHAR])
9322 explicit_char = 1;
8d08fdba
MS
9323 type = TREE_TYPE (IDENTIFIER_GLOBAL_VALUE (id));
9324 }
9325 goto found;
9326 }
e92cc029 9327 /* C++ aggregate types. */
8d08fdba
MS
9328 if (IDENTIFIER_HAS_TYPE_VALUE (id))
9329 {
9330 if (type)
8251199e 9331 cp_error ("multiple declarations `%T' and `%T'", type, id);
8d08fdba
MS
9332 else
9333 type = IDENTIFIER_TYPE_VALUE (id);
9334 goto found;
9335 }
9336
f376e137 9337 for (i = (int) RID_FIRST_MODIFIER; i <= (int) RID_LAST_MODIFIER; i++)
8d08fdba
MS
9338 {
9339 if (ridpointers[i] == id)
9340 {
9341 if (i == (int) RID_LONG && RIDBIT_SETP (i, specbits))
9342 {
795add94 9343 if (pedantic && ! in_system_header && warn_long_long)
8251199e 9344 pedwarn ("ANSI C++ does not support `long long'");
9a3b49ac 9345 if (longlong)
8251199e 9346 error ("`long long long' is too long for GCC");
8d08fdba
MS
9347 else
9348 longlong = 1;
9349 }
9350 else if (RIDBIT_SETP (i, specbits))
8251199e 9351 pedwarn ("duplicate `%s'", IDENTIFIER_POINTER (id));
8d08fdba
MS
9352 RIDBIT_SET (i, specbits);
9353 goto found;
9354 }
9355 }
9356 }
e92cc029 9357 /* C++ aggregate types. */
73b0fce8 9358 else if (TREE_CODE (id) == TYPE_DECL || TREE_CODE (id) == TEMPLATE_DECL)
45537677
MS
9359 {
9360 if (type)
8251199e 9361 cp_error ("multiple declarations `%T' and `%T'", type,
45537677
MS
9362 TREE_TYPE (id));
9363 else
5566b478
MS
9364 {
9365 type = TREE_TYPE (id);
9366 TREE_VALUE (spec) = type;
9367 }
45537677
MS
9368 goto found;
9369 }
8d08fdba 9370 if (type)
8251199e 9371 error ("two or more data types in declaration of `%s'", name);
8d08fdba
MS
9372 else if (TREE_CODE (id) == IDENTIFIER_NODE)
9373 {
9374 register tree t = lookup_name (id, 1);
9375 if (!t || TREE_CODE (t) != TYPE_DECL)
8251199e 9376 error ("`%s' fails to be a typedef or built in type",
8d08fdba
MS
9377 IDENTIFIER_POINTER (id));
9378 else
9379 {
9380 type = TREE_TYPE (t);
6125f3be
DE
9381#if 0
9382 /* See the code below that used this. */
f6abb50a 9383 decl_machine_attr = DECL_MACHINE_ATTRIBUTES (id);
6125f3be 9384#endif
8d08fdba
MS
9385 typedef_decl = t;
9386 }
9387 }
bd6dd845 9388 else if (id != error_mark_node)
8d08fdba
MS
9389 /* Can't change CLASS nodes into RECORD nodes here! */
9390 type = id;
9391
9392 found: ;
9393 }
9394
9395 typedef_type = type;
9396
37c46b43 9397 /* No type at all: default to `int', and set DEFAULTED_INT
6eabb241 9398 because it was not a user-defined typedef. */
8d08fdba 9399
a3203465
MS
9400 if (type == NULL_TREE
9401 && (RIDBIT_SETP (RID_SIGNED, specbits)
9402 || RIDBIT_SETP (RID_UNSIGNED, specbits)
9403 || RIDBIT_SETP (RID_LONG, specbits)
9404 || RIDBIT_SETP (RID_SHORT, specbits)))
9405 {
9406 /* These imply 'int'. */
9407 type = integer_type_node;
37c46b43 9408 defaulted_int = 1;
a3203465
MS
9409 }
9410
8d08fdba
MS
9411 if (type == NULL_TREE)
9412 {
9413 explicit_int = -1;
9414 if (return_type == return_dtor)
9415 type = void_type_node;
9416 else if (return_type == return_ctor)
f30432d7 9417 type = build_pointer_type (ctor_return_type);
51c184be
MS
9418 else if (return_type == return_conversion)
9419 type = ctor_return_type;
8d08fdba
MS
9420 else
9421 {
41eff652
JM
9422 /* We handle `main' specially here, because 'main () { }' is so
9423 common. With no options, it is allowed. With -Wreturn-type,
9424 it is a warning. It is only an error with -pedantic-errors. */
9425 int is_main = (funcdef_flag
9426 && MAIN_NAME_P (dname)
9427 && ctype == NULL_TREE
9428 && in_namespace == NULL_TREE
9429 && current_namespace == global_namespace);
9430
d2c192ad 9431 if (in_system_header || flag_ms_extensions)
1f901793
JM
9432 /* Allow it, sigh. */;
9433 else if (pedantic || ! is_main)
ff0cf464
JM
9434 cp_pedwarn ("ANSI C++ forbids declaration `%D' with no type",
9435 dname);
41eff652
JM
9436 else if (warn_return_type)
9437 cp_warning ("ANSI C++ forbids declaration `%D' with no type",
9438 dname);
9439
8d08fdba
MS
9440 type = integer_type_node;
9441 }
9442 }
9443 else if (return_type == return_dtor)
9444 {
8251199e 9445 error ("return type specification for destructor invalid");
8d08fdba
MS
9446 type = void_type_node;
9447 }
9448 else if (return_type == return_ctor)
9449 {
8251199e 9450 error ("return type specification for constructor invalid");
f30432d7 9451 type = build_pointer_type (ctor_return_type);
8d08fdba 9452 }
51c184be
MS
9453 else if (return_type == return_conversion)
9454 {
3bfdc719 9455 if (!same_type_p (type, ctor_return_type))
8251199e 9456 cp_error ("operator `%T' declared to return `%T'",
51c184be
MS
9457 ctor_return_type, type);
9458 else
8251199e 9459 cp_pedwarn ("return type specified for `operator %T'",
51c184be
MS
9460 ctor_return_type);
9461
9462 type = ctor_return_type;
9463 }
8d08fdba
MS
9464
9465 ctype = NULL_TREE;
9466
9467 /* Now process the modifiers that were specified
9468 and check for invalid combinations. */
9469
9470 /* Long double is a special combination. */
9471
9472 if (RIDBIT_SETP (RID_LONG, specbits)
9473 && TYPE_MAIN_VARIANT (type) == double_type_node)
9474 {
9475 RIDBIT_RESET (RID_LONG, specbits);
91063b51
MM
9476 type = build_qualified_type (long_double_type_node,
9477 CP_TYPE_QUALS (type));
8d08fdba
MS
9478 }
9479
9480 /* Check all other uses of type modifiers. */
9481
9482 if (RIDBIT_SETP (RID_UNSIGNED, specbits)
9483 || RIDBIT_SETP (RID_SIGNED, specbits)
9484 || RIDBIT_SETP (RID_LONG, specbits)
9485 || RIDBIT_SETP (RID_SHORT, specbits))
9486 {
9487 int ok = 0;
9488
9489 if (TREE_CODE (type) == REAL_TYPE)
8251199e 9490 error ("short, signed or unsigned invalid for `%s'", name);
b7484fbe 9491 else if (TREE_CODE (type) != INTEGER_TYPE)
8251199e 9492 error ("long, short, signed or unsigned invalid for `%s'", name);
8d08fdba
MS
9493 else if (RIDBIT_SETP (RID_LONG, specbits)
9494 && RIDBIT_SETP (RID_SHORT, specbits))
8251199e 9495 error ("long and short specified together for `%s'", name);
8d08fdba
MS
9496 else if ((RIDBIT_SETP (RID_LONG, specbits)
9497 || RIDBIT_SETP (RID_SHORT, specbits))
9498 && explicit_char)
8251199e 9499 error ("long or short specified with char for `%s'", name);
8d08fdba
MS
9500 else if ((RIDBIT_SETP (RID_LONG, specbits)
9501 || RIDBIT_SETP (RID_SHORT, specbits))
9502 && TREE_CODE (type) == REAL_TYPE)
8251199e 9503 error ("long or short specified with floating type for `%s'", name);
8d08fdba
MS
9504 else if (RIDBIT_SETP (RID_SIGNED, specbits)
9505 && RIDBIT_SETP (RID_UNSIGNED, specbits))
8251199e 9506 error ("signed and unsigned given together for `%s'", name);
8d08fdba
MS
9507 else
9508 {
9509 ok = 1;
37c46b43 9510 if (!explicit_int && !defaulted_int && !explicit_char && pedantic)
8d08fdba 9511 {
8251199e 9512 pedwarn ("long, short, signed or unsigned used invalidly for `%s'",
8d08fdba
MS
9513 name);
9514 if (flag_pedantic_errors)
9515 ok = 0;
9516 }
9517 }
9518
9519 /* Discard the type modifiers if they are invalid. */
9520 if (! ok)
9521 {
9522 RIDBIT_RESET (RID_UNSIGNED, specbits);
9523 RIDBIT_RESET (RID_SIGNED, specbits);
9524 RIDBIT_RESET (RID_LONG, specbits);
9525 RIDBIT_RESET (RID_SHORT, specbits);
9526 longlong = 0;
9527 }
9528 }
9529
37c46b43
MS
9530 if (RIDBIT_SETP (RID_COMPLEX, specbits)
9531 && TREE_CODE (type) != INTEGER_TYPE && TREE_CODE (type) != REAL_TYPE)
9532 {
8251199e 9533 error ("complex invalid for `%s'", name);
37c46b43
MS
9534 RIDBIT_RESET (RID_COMPLEX, specbits);
9535 }
9536
8d08fdba
MS
9537 /* Decide whether an integer type is signed or not.
9538 Optionally treat bitfields as signed by default. */
9539 if (RIDBIT_SETP (RID_UNSIGNED, specbits)
b89c5a7b
MM
9540 /* [class.bit]
9541
9542 It is implementation-defined whether a plain (neither
9543 explicitly signed or unsigned) char, short, int, or long
9544 bit-field is signed or unsigned.
9545
9546 Naturally, we extend this to long long as well. Note that
9547 this does not include wchar_t. */
9548 || (bitfield && !flag_signed_bitfields
9549 && RIDBIT_NOTSETP (RID_SIGNED, specbits)
9550 /* A typedef for plain `int' without `signed' can be
9551 controlled just like plain `int', but a typedef for
9552 `signed int' cannot be so controlled. */
9553 && !(typedef_decl
29bbeb1c 9554 && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl))
b89c5a7b
MM
9555 && (TREE_CODE (type) == INTEGER_TYPE
9556 || TREE_CODE (type) == CHAR_TYPE)
29bbeb1c 9557 && !same_type_p (TYPE_MAIN_VARIANT (type), wchar_type_node)))
8d08fdba
MS
9558 {
9559 if (longlong)
9560 type = long_long_unsigned_type_node;
9561 else if (RIDBIT_SETP (RID_LONG, specbits))
9562 type = long_unsigned_type_node;
9563 else if (RIDBIT_SETP (RID_SHORT, specbits))
9564 type = short_unsigned_type_node;
9565 else if (type == char_type_node)
9566 type = unsigned_char_type_node;
9567 else if (typedef_decl)
9568 type = unsigned_type (type);
9569 else
9570 type = unsigned_type_node;
9571 }
9572 else if (RIDBIT_SETP (RID_SIGNED, specbits)
9573 && type == char_type_node)
9574 type = signed_char_type_node;
9575 else if (longlong)
9576 type = long_long_integer_type_node;
9577 else if (RIDBIT_SETP (RID_LONG, specbits))
9578 type = long_integer_type_node;
9579 else if (RIDBIT_SETP (RID_SHORT, specbits))
9580 type = short_integer_type_node;
9581
37c46b43
MS
9582 if (RIDBIT_SETP (RID_COMPLEX, specbits))
9583 {
9584 /* If we just have "complex", it is equivalent to
9585 "complex double", but if any modifiers at all are specified it is
9586 the complex form of TYPE. E.g, "complex short" is
9587 "complex short int". */
9588
9589 if (defaulted_int && ! longlong
9590 && ! (RIDBIT_SETP (RID_LONG, specbits)
9591 || RIDBIT_SETP (RID_SHORT, specbits)
9592 || RIDBIT_SETP (RID_SIGNED, specbits)
9593 || RIDBIT_SETP (RID_UNSIGNED, specbits)))
9594 type = complex_double_type_node;
9595 else if (type == integer_type_node)
9596 type = complex_integer_type_node;
9597 else if (type == float_type_node)
9598 type = complex_float_type_node;
9599 else if (type == double_type_node)
9600 type = complex_double_type_node;
9601 else if (type == long_double_type_node)
9602 type = complex_long_double_type_node;
9603 else
9604 type = build_complex_type (type);
9605 }
9606
3ac3d9ea
MM
9607 if (return_type == return_conversion
9608 && (RIDBIT_SETP (RID_CONST, specbits)
91063b51
MM
9609 || RIDBIT_SETP (RID_VOLATILE, specbits)
9610 || RIDBIT_SETP (RID_RESTRICT, specbits)))
9611 cp_error ("qualifiers are not allowed on declaration of `operator %T'",
3ac3d9ea
MM
9612 ctor_return_type);
9613
8d08fdba
MS
9614 /* Set CONSTP if this declaration is `const', whether by
9615 explicit specification or via a typedef.
9616 Likewise for VOLATILEP. */
9617
91063b51
MM
9618 constp = !! RIDBIT_SETP (RID_CONST, specbits) + CP_TYPE_CONST_P (type);
9619 restrictp =
9620 !! RIDBIT_SETP (RID_RESTRICT, specbits) + CP_TYPE_RESTRICT_P (type);
9621 volatilep =
9622 !! RIDBIT_SETP (RID_VOLATILE, specbits) + CP_TYPE_VOLATILE_P (type);
9623 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
9624 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
9625 | (volatilep ? TYPE_QUAL_VOLATILE : 0));
9626 type = cp_build_qualified_type (type, type_quals);
8d08fdba
MS
9627 staticp = 0;
9628 inlinep = !! RIDBIT_SETP (RID_INLINE, specbits);
8d08fdba 9629 virtualp = RIDBIT_SETP (RID_VIRTUAL, specbits);
db5ae43f
MS
9630 RIDBIT_RESET (RID_VIRTUAL, specbits);
9631 explicitp = RIDBIT_SETP (RID_EXPLICIT, specbits) != 0;
9632 RIDBIT_RESET (RID_EXPLICIT, specbits);
8d08fdba 9633
8d08fdba
MS
9634 if (RIDBIT_SETP (RID_STATIC, specbits))
9635 staticp = 1 + (decl_context == FIELD);
9636
9637 if (virtualp && staticp == 2)
9638 {
8251199e 9639 cp_error ("member `%D' cannot be declared both virtual and static",
8d08fdba
MS
9640 dname);
9641 staticp = 0;
9642 }
9643 friendp = RIDBIT_SETP (RID_FRIEND, specbits);
8d08fdba
MS
9644 RIDBIT_RESET (RID_FRIEND, specbits);
9645
7e83af84
BK
9646 /* $7.1.2, Function specifiers */
9647 if (friendp && explicitp)
9648 error ("only declarations of constructors can be `explicit'");
9649
8d08fdba
MS
9650 if (RIDBIT_SETP (RID_MUTABLE, specbits))
9651 {
9652 if (decl_context == PARM)
9653 {
8251199e 9654 error ("non-member `%s' cannot be declared `mutable'", name);
8d08fdba
MS
9655 RIDBIT_RESET (RID_MUTABLE, specbits);
9656 }
9657 else if (friendp || decl_context == TYPENAME)
9658 {
8251199e 9659 error ("non-object member `%s' cannot be declared `mutable'", name);
8d08fdba
MS
9660 RIDBIT_RESET (RID_MUTABLE, specbits);
9661 }
8d08fdba
MS
9662 }
9663
9664 /* Warn if two storage classes are given. Default to `auto'. */
9665
9666 if (RIDBIT_ANY_SET (specbits))
9667 {
9668 if (RIDBIT_SETP (RID_STATIC, specbits)) nclasses++;
9669 if (RIDBIT_SETP (RID_EXTERN, specbits)) nclasses++;
9670 if (decl_context == PARM && nclasses > 0)
8251199e 9671 error ("storage class specifiers invalid in parameter declarations");
8d08fdba
MS
9672 if (RIDBIT_SETP (RID_TYPEDEF, specbits))
9673 {
9674 if (decl_context == PARM)
8251199e 9675 error ("typedef declaration invalid in parameter declaration");
8d08fdba
MS
9676 nclasses++;
9677 }
9678 if (RIDBIT_SETP (RID_AUTO, specbits)) nclasses++;
9679 if (RIDBIT_SETP (RID_REGISTER, specbits)) nclasses++;
9680 }
9681
9682 /* Give error if `virtual' is used outside of class declaration. */
b7484fbe
MS
9683 if (virtualp
9684 && (current_class_name == NULL_TREE || decl_context != FIELD))
8d08fdba 9685 {
8251199e 9686 error ("virtual outside class declaration");
8d08fdba
MS
9687 virtualp = 0;
9688 }
9689 if (current_class_name == NULL_TREE && RIDBIT_SETP (RID_MUTABLE, specbits))
9690 {
8251199e 9691 error ("only members can be declared mutable");
8d08fdba
MS
9692 RIDBIT_RESET (RID_MUTABLE, specbits);
9693 }
9694
9695 /* Static anonymous unions are dealt with here. */
9696 if (staticp && decl_context == TYPENAME
9697 && TREE_CODE (declspecs) == TREE_LIST
6bdb8141 9698 && ANON_AGGR_TYPE_P (TREE_VALUE (declspecs)))
8d08fdba
MS
9699 decl_context = FIELD;
9700
8d08fdba
MS
9701 /* Warn about storage classes that are invalid for certain
9702 kinds of declarations (parameters, typenames, etc.). */
9703
9704 if (nclasses > 1)
8251199e 9705 error ("multiple storage classes in declaration of `%s'", name);
8d08fdba
MS
9706 else if (decl_context != NORMAL && nclasses > 0)
9707 {
db5ae43f 9708 if ((decl_context == PARM || decl_context == CATCHPARM)
8d08fdba
MS
9709 && (RIDBIT_SETP (RID_REGISTER, specbits)
9710 || RIDBIT_SETP (RID_AUTO, specbits)))
9711 ;
fc378698
MS
9712 else if (RIDBIT_SETP (RID_TYPEDEF, specbits))
9713 ;
8d08fdba 9714 else if (decl_context == FIELD
8d08fdba
MS
9715 /* C++ allows static class elements */
9716 && RIDBIT_SETP (RID_STATIC, specbits))
9717 /* C++ also allows inlines and signed and unsigned elements,
9718 but in those cases we don't come in here. */
9719 ;
9720 else
9721 {
9722 if (decl_context == FIELD)
9723 {
b7484fbe
MS
9724 tree tmp = NULL_TREE;
9725 register int op = 0;
9726
9727 if (declarator)
9728 {
9e9ff709
MS
9729 /* Avoid trying to get an operand off an identifier node. */
9730 if (TREE_CODE (declarator) == IDENTIFIER_NODE)
9731 tmp = declarator;
9732 else
9733 tmp = TREE_OPERAND (declarator, 0);
b7484fbe
MS
9734 op = IDENTIFIER_OPNAME_P (tmp);
9735 }
8251199e 9736 error ("storage class specified for %s `%s'",
6eabb241 9737 op ? "member operator" : "field",
8d08fdba
MS
9738 op ? operator_name_string (tmp) : name);
9739 }
9740 else
db5ae43f 9741 error (((decl_context == PARM || decl_context == CATCHPARM)
8d08fdba
MS
9742 ? "storage class specified for parameter `%s'"
9743 : "storage class specified for typename"), name);
9744 RIDBIT_RESET (RID_REGISTER, specbits);
9745 RIDBIT_RESET (RID_AUTO, specbits);
9746 RIDBIT_RESET (RID_EXTERN, specbits);
8d08fdba
MS
9747 }
9748 }
9749 else if (RIDBIT_SETP (RID_EXTERN, specbits) && initialized && !funcdef_flag)
9750 {
a9aedbc2 9751 if (toplevel_bindings_p ())
8d08fdba 9752 {
59be0cdd 9753 /* It's common practice (and completely valid) to have a const
8d08fdba 9754 be initialized and declared extern. */
950ad3c3 9755 if (!(type_quals & TYPE_QUAL_CONST))
8251199e 9756 warning ("`%s' initialized and declared `extern'", name);
8d08fdba
MS
9757 }
9758 else
8251199e 9759 error ("`%s' has both `extern' and initializer", name);
8d08fdba
MS
9760 }
9761 else if (RIDBIT_SETP (RID_EXTERN, specbits) && funcdef_flag
a9aedbc2 9762 && ! toplevel_bindings_p ())
8251199e 9763 error ("nested function `%s' declared `extern'", name);
a9aedbc2 9764 else if (toplevel_bindings_p ())
8d08fdba
MS
9765 {
9766 if (RIDBIT_SETP (RID_AUTO, specbits))
8251199e 9767 error ("top-level declaration of `%s' specifies `auto'", name);
8d08fdba
MS
9768 }
9769
909e536a 9770 if (nclasses > 0 && friendp)
8251199e 9771 error ("storage class specifiers invalid in friend function declarations");
909e536a 9772
8d08fdba
MS
9773 /* Now figure out the structure of the declarator proper.
9774 Descend through it, creating more complex types, until we reach
9775 the declared identifier (or NULL_TREE, in an absolute declarator). */
9776
1eab9b56
JM
9777 inner_attrs = NULL_TREE;
9778 ignore_attrs = 0;
9779
386b8a85
JM
9780 while (declarator && TREE_CODE (declarator) != IDENTIFIER_NODE
9781 && TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
8d08fdba
MS
9782 {
9783 /* Each level of DECLARATOR is either an ARRAY_REF (for ...[..]),
9784 an INDIRECT_REF (for *...),
9785 a CALL_EXPR (for ...(...)),
9786 an identifier (for the name being declared)
9787 or a null pointer (for the place in an absolute declarator
9788 where the name was omitted).
9789 For the last two cases, we have just exited the loop.
9790
9791 For C++ it could also be
9792 a SCOPE_REF (for class :: ...). In this case, we have converted
9793 sensible names to types, and those are the values we use to
9794 qualify the member name.
9795 an ADDR_EXPR (for &...),
9796 a BIT_NOT_EXPR (for destructors)
8d08fdba
MS
9797
9798 At this point, TYPE is the type of elements of an array,
9799 or for a function to return, or for a pointer to point to.
9800 After this sequence of ifs, TYPE is the type of the
9801 array or function or pointer, and DECLARATOR has had its
9802 outermost layer removed. */
9803
bd6dd845 9804 if (type == error_mark_node)
8d08fdba
MS
9805 {
9806 if (TREE_CODE (declarator) == SCOPE_REF)
9807 declarator = TREE_OPERAND (declarator, 1);
9808 else
9809 declarator = TREE_OPERAND (declarator, 0);
9810 continue;
9811 }
9812 if (quals != NULL_TREE
9813 && (declarator == NULL_TREE
9814 || TREE_CODE (declarator) != SCOPE_REF))
9815 {
9816 if (ctype == NULL_TREE && TREE_CODE (type) == METHOD_TYPE)
9817 ctype = TYPE_METHOD_BASETYPE (type);
9818 if (ctype != NULL_TREE)
9819 {
8d08fdba 9820 tree dummy = build_decl (TYPE_DECL, NULL_TREE, type);
8d08fdba
MS
9821 ctype = grok_method_quals (ctype, dummy, quals);
9822 type = TREE_TYPE (dummy);
9823 quals = NULL_TREE;
9824 }
9825 }
1eab9b56
JM
9826
9827 /* See the comment for the TREE_LIST case, below. */
9828 if (ignore_attrs)
9829 ignore_attrs = 0;
9830 else if (inner_attrs)
9831 {
9832 decl_attributes (type, inner_attrs, NULL_TREE);
9833 inner_attrs = NULL_TREE;
9834 }
9835
8d08fdba
MS
9836 switch (TREE_CODE (declarator))
9837 {
52fbc847
JM
9838 case TREE_LIST:
9839 {
9840 /* We encode a declarator with embedded attributes using
1eab9b56
JM
9841 a TREE_LIST. The attributes apply to the declarator
9842 directly inside them, so we have to skip an iteration
9843 before applying them to the type. If the declarator just
9844 inside is the declarator-id, we apply the attrs to the
9845 decl itself. */
9846 inner_attrs = TREE_PURPOSE (declarator);
9847 ignore_attrs = 1;
52fbc847 9848 declarator = TREE_VALUE (declarator);
52fbc847
JM
9849 }
9850 break;
9851
8d08fdba
MS
9852 case ARRAY_REF:
9853 {
9854 register tree itype = NULL_TREE;
9855 register tree size = TREE_OPERAND (declarator, 1);
b7484fbe
MS
9856 /* The index is a signed object `sizetype' bits wide. */
9857 tree index_type = signed_type (sizetype);
8d08fdba
MS
9858
9859 declarator = TREE_OPERAND (declarator, 0);
9860
9861 /* Check for some types that there cannot be arrays of. */
9862
a1774733 9863 if (TREE_CODE (type) == VOID_TYPE)
8d08fdba 9864 {
8251199e 9865 cp_error ("declaration of `%D' as array of voids", dname);
8d08fdba
MS
9866 type = error_mark_node;
9867 }
9868
9869 if (TREE_CODE (type) == FUNCTION_TYPE)
9870 {
8251199e 9871 cp_error ("declaration of `%D' as array of functions", dname);
8d08fdba
MS
9872 type = error_mark_node;
9873 }
9874
9875 /* ARM $8.4.3: Since you can't have a pointer to a reference,
9876 you can't have arrays of references. If we allowed them,
59be0cdd 9877 then we'd be saying x[i] is valid for an array x, but
8d08fdba
MS
9878 then you'd have to ask: what does `*(x + i)' mean? */
9879 if (TREE_CODE (type) == REFERENCE_TYPE)
9880 {
9881 if (decl_context == TYPENAME)
8251199e 9882 cp_error ("cannot make arrays of references");
8d08fdba 9883 else
8251199e 9884 cp_error ("declaration of `%D' as array of references",
8d08fdba
MS
9885 dname);
9886 type = error_mark_node;
9887 }
9888
9889 if (TREE_CODE (type) == OFFSET_TYPE)
9890 {
8251199e 9891 cp_error ("declaration of `%D' as array of data members",
8d08fdba
MS
9892 dname);
9893 type = error_mark_node;
9894 }
9895
9896 if (TREE_CODE (type) == METHOD_TYPE)
9897 {
8251199e 9898 cp_error ("declaration of `%D' as array of function members",
8d08fdba
MS
9899 dname);
9900 type = error_mark_node;
9901 }
9902
9903 if (size == error_mark_node)
9904 type = error_mark_node;
3bfdc719
MM
9905 else if (TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type))
9906 {
9907 /* [dcl.array]
9908
9909 the constant expressions that specify the bounds of
9910 the arrays can be omitted only for the first member
9911 of the sequence. */
9912 cp_error ("declaration of `%D' as multidimensional array",
9913 dname);
9914 cp_error ("must have bounds for all dimensions except the first");
9915 type = error_mark_node;
9916 }
8d08fdba
MS
9917
9918 if (type == error_mark_node)
9919 continue;
9920
f2e2cbd4 9921 /* VC++ spells a zero-sized array with []. */
e8ea7036
MM
9922 if (size == NULL_TREE && decl_context == FIELD && ! staticp
9923 && ! RIDBIT_SETP (RID_TYPEDEF, specbits))
f2e2cbd4
JM
9924 size = integer_zero_node;
9925
8d08fdba
MS
9926 if (size)
9927 {
9928 /* Must suspend_momentary here because the index
9929 type may need to live until the end of the function.
9930 For example, it is used in the declaration of a
9931 variable which requires destructing at the end of
9932 the function; then build_vec_delete will need this
9933 value. */
9934 int yes = suspend_momentary ();
2c73f9f5 9935 /* Might be a cast. */
8d08fdba
MS
9936 if (TREE_CODE (size) == NOP_EXPR
9937 && TREE_TYPE (size) == TREE_TYPE (TREE_OPERAND (size, 0)))
9938 size = TREE_OPERAND (size, 0);
9f33663b
JM
9939 if (TREE_READONLY_DECL_P (size))
9940 size = decl_constant_value (size);
8d08fdba 9941
5db698f6
MM
9942 /* If this involves a template parameter, it will be a
9943 constant at instantiation time, but we don't know
9944 what the value is yet. Even if no template
9945 parameters are involved, we may an expression that
9946 is not a constant; we don't even simplify `1 + 2'
9947 when processing a template. */
9948 if (processing_template_decl)
5566b478 9949 {
e97e5263
JM
9950 /* Resolve a qualified reference to an enumerator or
9951 static const data member of ours. */
9952 if (TREE_CODE (size) == SCOPE_REF
9953 && TREE_OPERAND (size, 0) == current_class_type)
9954 {
9955 tree t = lookup_field (current_class_type,
9956 TREE_OPERAND (size, 1), 0, 0);
9957 if (t)
9958 size = t;
9959 }
9960
f05f680a
AO
9961 itype = build_index_type (build_min
9962 (MINUS_EXPR, sizetype, size, integer_one_node));
5566b478
MS
9963 goto dont_grok_size;
9964 }
8d08fdba
MS
9965
9966 if (TREE_CODE (TREE_TYPE (size)) != INTEGER_TYPE
161c12b0
JM
9967 && TREE_CODE (TREE_TYPE (size)) != ENUMERAL_TYPE
9968 && TREE_CODE (TREE_TYPE (size)) != BOOLEAN_TYPE)
8d08fdba 9969 {
8251199e 9970 cp_error ("size of array `%D' has non-integer type",
8d08fdba
MS
9971 dname);
9972 size = integer_one_node;
9973 }
e1cd6e56 9974 if (pedantic && integer_zerop (size))
8251199e 9975 cp_pedwarn ("ANSI C++ forbids zero-size array `%D'", dname);
8d08fdba
MS
9976 if (TREE_CONSTANT (size))
9977 {
dff6b454
RK
9978 int old_flag_pedantic_errors = flag_pedantic_errors;
9979 int old_pedantic = pedantic;
9980 pedantic = flag_pedantic_errors = 1;
9981 /* Always give overflow errors on array subscripts. */
8d08fdba 9982 constant_expression_warning (size);
dff6b454
RK
9983 pedantic = old_pedantic;
9984 flag_pedantic_errors = old_flag_pedantic_errors;
8d08fdba
MS
9985 if (INT_CST_LT (size, integer_zero_node))
9986 {
8251199e 9987 cp_error ("size of array `%D' is negative", dname);
8d08fdba
MS
9988 size = integer_one_node;
9989 }
8d08fdba
MS
9990 }
9991 else
9992 {
e1cd6e56 9993 if (pedantic)
a0a33927
MS
9994 {
9995 if (dname)
8251199e 9996 cp_pedwarn ("ANSI C++ forbids variable-size array `%D'",
a0a33927
MS
9997 dname);
9998 else
8251199e 9999 cp_pedwarn ("ANSI C++ forbids variable-size array");
a0a33927 10000 }
8d08fdba 10001 }
b7484fbe 10002
beb53fb8
JM
10003 itype
10004 = fold (build_binary_op (MINUS_EXPR,
37c46b43
MS
10005 cp_convert (index_type, size),
10006 cp_convert (index_type,
337c90cc 10007 integer_one_node)));
b7484fbe
MS
10008 if (! TREE_CONSTANT (itype))
10009 itype = variable_size (itype);
594740f3
MS
10010 else if (TREE_OVERFLOW (itype))
10011 {
8251199e 10012 error ("overflow in array dimension");
594740f3
MS
10013 TREE_OVERFLOW (itype) = 0;
10014 }
fc378698
MS
10015
10016 /* If we're a parm, we need to have a permanent type so
10017 mangling checks for re-use will work right. If both the
10018 element and index types are permanent, the array type
10019 will be, too. */
10020 if (decl_context == PARM
10021 && allocation_temporary_p () && TREE_PERMANENT (type))
10022 {
10023 push_obstacks (&permanent_obstack, &permanent_obstack);
10024 itype = build_index_type (itype);
10025 pop_obstacks ();
10026 }
10027 else
10028 itype = build_index_type (itype);
10029
5566b478 10030 dont_grok_size:
8d08fdba
MS
10031 resume_momentary (yes);
10032 }
10033
8d08fdba 10034 type = build_cplus_array_type (type, itype);
8d08fdba
MS
10035 ctype = NULL_TREE;
10036 }
10037 break;
10038
10039 case CALL_EXPR:
10040 {
10041 tree arg_types;
f376e137 10042 int funcdecl_p;
43f887f9 10043 tree inner_parms = CALL_DECLARATOR_PARMS (declarator);
f376e137 10044 tree inner_decl = TREE_OPERAND (declarator, 0);
8d08fdba
MS
10045
10046 /* Declaring a function type.
10047 Make sure we have a valid type for the function to return. */
8d08fdba 10048
91063b51 10049 /* We now know that the TYPE_QUALS don't apply to the
14ae7e7d 10050 decl, but to its return type. */
91063b51 10051 type_quals = TYPE_UNQUALIFIED;
8d08fdba
MS
10052
10053 /* Warn about some types functions can't return. */
10054
10055 if (TREE_CODE (type) == FUNCTION_TYPE)
10056 {
8251199e 10057 error ("`%s' declared as function returning a function", name);
8d08fdba
MS
10058 type = integer_type_node;
10059 }
10060 if (TREE_CODE (type) == ARRAY_TYPE)
10061 {
8251199e 10062 error ("`%s' declared as function returning an array", name);
8d08fdba
MS
10063 type = integer_type_node;
10064 }
10065
f376e137
MS
10066 if (inner_decl && TREE_CODE (inner_decl) == SCOPE_REF)
10067 inner_decl = TREE_OPERAND (inner_decl, 1);
10068
386b8a85
JM
10069 if (inner_decl && TREE_CODE (inner_decl) == TEMPLATE_ID_EXPR)
10070 inner_decl = dname;
10071
b7484fbe 10072 /* Pick up type qualifiers which should be applied to `this'. */
43f887f9 10073 quals = CALL_DECLARATOR_QUALS (declarator);
b7484fbe 10074
c11b6f21 10075 /* Pick up the exception specifications. */
43f887f9 10076 raises = CALL_DECLARATOR_EXCEPTION_SPEC (declarator);
c11b6f21 10077
f376e137
MS
10078 /* Say it's a definition only for the CALL_EXPR
10079 closest to the identifier. */
beb53fb8 10080 funcdecl_p
386b8a85
JM
10081 = inner_decl
10082 && (TREE_CODE (inner_decl) == IDENTIFIER_NODE
10083 || TREE_CODE (inner_decl) == TEMPLATE_ID_EXPR
10084 || TREE_CODE (inner_decl) == BIT_NOT_EXPR);
10085
8d08fdba
MS
10086 if (ctype == NULL_TREE
10087 && decl_context == FIELD
f376e137 10088 && funcdecl_p
8d08fdba
MS
10089 && (friendp == 0 || dname == current_class_name))
10090 ctype = current_class_type;
10091
51c184be 10092 if (ctype && return_type == return_conversion)
8d08fdba
MS
10093 TYPE_HAS_CONVERSION (ctype) = 1;
10094 if (ctype && constructor_name (ctype) == dname)
10095 {
10096 /* We are within a class's scope. If our declarator name
10097 is the same as the class name, and we are defining
10098 a function, then it is a constructor/destructor, and
10099 therefore returns a void type. */
10100
10101 if (flags == DTOR_FLAG)
10102 {
10103 /* ANSI C++ June 5 1992 WP 12.4.1. A destructor may
10104 not be declared const or volatile. A destructor
10105 may not be static. */
10106 if (staticp == 2)
8251199e 10107 error ("destructor cannot be static member function");
b7484fbe 10108 if (quals)
8d08fdba 10109 {
4f70a846
MM
10110 cp_error ("destructors may not be `%s'",
10111 IDENTIFIER_POINTER (TREE_VALUE (quals)));
10112 quals = NULL_TREE;
8d08fdba
MS
10113 }
10114 if (decl_context == FIELD)
10115 {
10116 if (! member_function_or_else (ctype, current_class_type,
10117 "destructor for alien class `%s' cannot be a member"))
10118 return void_type_node;
10119 }
10120 }
2c73f9f5 10121 else /* It's a constructor. */
8d08fdba 10122 {
db5ae43f
MS
10123 if (explicitp == 1)
10124 explicitp = 2;
8d08fdba
MS
10125 /* ANSI C++ June 5 1992 WP 12.1.2. A constructor may
10126 not be declared const or volatile. A constructor may
10127 not be virtual. A constructor may not be static. */
10128 if (staticp == 2)
8251199e 10129 error ("constructor cannot be static member function");
8d08fdba
MS
10130 if (virtualp)
10131 {
8251199e 10132 pedwarn ("constructors cannot be declared virtual");
8d08fdba
MS
10133 virtualp = 0;
10134 }
b7484fbe 10135 if (quals)
8d08fdba 10136 {
4f70a846
MM
10137 cp_error ("constructors may not be `%s'",
10138 IDENTIFIER_POINTER (TREE_VALUE (quals)));
10139 quals = NULL_TREE;
8d08fdba 10140 }
8d08fdba 10141 {
51c184be 10142 RID_BIT_TYPE tmp_bits;
fc378698 10143 bcopy ((void*)&specbits, (void*)&tmp_bits, sizeof (RID_BIT_TYPE));
51c184be
MS
10144 RIDBIT_RESET (RID_INLINE, tmp_bits);
10145 RIDBIT_RESET (RID_STATIC, tmp_bits);
10146 if (RIDBIT_ANY_SET (tmp_bits))
8251199e 10147 error ("return value type specifier for constructor ignored");
8d08fdba 10148 }
f30432d7 10149 type = build_pointer_type (ctype);
6eabb241 10150 if (decl_context == FIELD)
8d08fdba
MS
10151 {
10152 if (! member_function_or_else (ctype, current_class_type,
10153 "constructor for alien class `%s' cannot be member"))
10154 return void_type_node;
10155 TYPE_HAS_CONSTRUCTOR (ctype) = 1;
10156 if (return_type != return_ctor)
10157 return NULL_TREE;
10158 }
10159 }
10160 if (decl_context == FIELD)
10161 staticp = 0;
10162 }
b7484fbe 10163 else if (friendp)
8d08fdba 10164 {
b7484fbe 10165 if (initialized)
8251199e 10166 error ("can't initialize friend function `%s'", name);
b7484fbe
MS
10167 if (virtualp)
10168 {
10169 /* Cannot be both friend and virtual. */
8251199e 10170 error ("virtual functions cannot be friends");
b7484fbe
MS
10171 RIDBIT_RESET (RID_FRIEND, specbits);
10172 friendp = 0;
10173 }
28cbf42c 10174 if (decl_context == NORMAL)
8251199e 10175 error ("friend declaration not in class definition");
28cbf42c 10176 if (current_function_decl && funcdef_flag)
8251199e 10177 cp_error ("can't define friend function `%s' in a local class definition",
28cbf42c 10178 name);
8d08fdba
MS
10179 }
10180
8d08fdba
MS
10181 /* Construct the function type and go to the next
10182 inner layer of declarator. */
10183
f376e137 10184 declarator = TREE_OPERAND (declarator, 0);
8d08fdba 10185
f376e137
MS
10186 /* FIXME: This is where default args should be fully
10187 processed. */
8d08fdba 10188
f376e137 10189 arg_types = grokparms (inner_parms, funcdecl_p ? funcdef_flag : 0);
8d08fdba 10190
4f70a846 10191 if (declarator && flags == DTOR_FLAG)
8d08fdba 10192 {
4f70a846
MM
10193 /* A destructor declared in the body of a class will
10194 be represented as a BIT_NOT_EXPR. But, we just
10195 want the underlying IDENTIFIER. */
8d08fdba 10196 if (TREE_CODE (declarator) == BIT_NOT_EXPR)
4f70a846
MM
10197 declarator = TREE_OPERAND (declarator, 0);
10198
10199 if (strict_prototype == 0 && arg_types == NULL_TREE)
10200 arg_types = void_list_node;
10201 else if (arg_types == NULL_TREE
10202 || arg_types != void_list_node)
8d08fdba 10203 {
4f70a846
MM
10204 cp_error ("destructors may not have parameters");
10205 arg_types = void_list_node;
10206 last_function_parms = NULL_TREE;
8d08fdba
MS
10207 }
10208 }
10209
d22c8596 10210 /* ANSI says that `const int foo ();'
8d08fdba 10211 does not make the function foo const. */
d22c8596 10212 type = build_function_type (type, arg_types);
42976354
BK
10213
10214 {
10215 tree t;
10216 for (t = arg_types; t; t = TREE_CHAIN (t))
10217 if (TREE_PURPOSE (t)
10218 && TREE_CODE (TREE_PURPOSE (t)) == DEFAULT_ARG)
10219 {
10220 add_defarg_fn (type);
10221 break;
10222 }
10223 }
8d08fdba
MS
10224 }
10225 break;
10226
10227 case ADDR_EXPR:
10228 case INDIRECT_REF:
10229 /* Filter out pointers-to-references and references-to-references.
10230 We can get these if a TYPE_DECL is used. */
10231
10232 if (TREE_CODE (type) == REFERENCE_TYPE)
10233 {
8251199e 10234 error ("cannot declare %s to references",
8d08fdba
MS
10235 TREE_CODE (declarator) == ADDR_EXPR
10236 ? "references" : "pointers");
10237 declarator = TREE_OPERAND (declarator, 0);
10238 continue;
10239 }
10240
a5894242
MS
10241 if (TREE_CODE (type) == OFFSET_TYPE
10242 && (TREE_CODE (TREE_TYPE (type)) == VOID_TYPE
10243 || TREE_CODE (TREE_TYPE (type)) == REFERENCE_TYPE))
10244 {
8251199e 10245 cp_error ("cannot declare pointer to `%#T' member",
a5894242
MS
10246 TREE_TYPE (type));
10247 type = TREE_TYPE (type);
10248 }
10249
61a127b3
MM
10250 /* Merge any constancy or volatility into the target type
10251 for the pointer. */
10252
91063b51
MM
10253 /* We now know that the TYPE_QUALS don't apply to the decl,
10254 but to the target of the pointer. */
10255 type_quals = TYPE_UNQUALIFIED;
8d08fdba 10256
6eabb241 10257 if (TREE_CODE (declarator) == ADDR_EXPR)
8d08fdba 10258 {
69851283
MM
10259 if (TREE_CODE (type) == VOID_TYPE)
10260 error ("invalid type: `void &'");
8d08fdba 10261 else
69851283 10262 type = build_reference_type (type);
8d08fdba
MS
10263 }
10264 else if (TREE_CODE (type) == METHOD_TYPE)
d8f8dca1 10265 type = build_ptrmemfunc_type (build_pointer_type (type));
8d08fdba
MS
10266 else
10267 type = build_pointer_type (type);
10268
10269 /* Process a list of type modifier keywords (such as
10270 const or volatile) that were given inside the `*' or `&'. */
10271
10272 if (TREE_TYPE (declarator))
10273 {
10274 register tree typemodlist;
10275 int erred = 0;
91063b51
MM
10276
10277 constp = 0;
10278 volatilep = 0;
10279 restrictp = 0;
8d08fdba
MS
10280 for (typemodlist = TREE_TYPE (declarator); typemodlist;
10281 typemodlist = TREE_CHAIN (typemodlist))
10282 {
91063b51
MM
10283 tree qualifier = TREE_VALUE (typemodlist);
10284
10285 if (qualifier == ridpointers[(int) RID_CONST])
8d08fdba 10286 constp++;
91063b51 10287 else if (qualifier == ridpointers[(int) RID_VOLATILE])
8d08fdba 10288 volatilep++;
91063b51
MM
10289 else if (qualifier == ridpointers[(int) RID_RESTRICT])
10290 restrictp++;
8d08fdba
MS
10291 else if (!erred)
10292 {
10293 erred = 1;
91063b51 10294 error ("invalid type modifier within pointer declarator");
8d08fdba
MS
10295 }
10296 }
10297 if (constp > 1)
8251199e 10298 pedwarn ("duplicate `const'");
8d08fdba 10299 if (volatilep > 1)
8251199e 10300 pedwarn ("duplicate `volatile'");
91063b51
MM
10301 if (restrictp > 1)
10302 pedwarn ("duplicate `restrict'");
10303
10304 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
10305 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
10306 | (volatilep ? TYPE_QUAL_VOLATILE : 0));
a0a33927
MS
10307 if (TREE_CODE (declarator) == ADDR_EXPR
10308 && (constp || volatilep))
10309 {
10310 if (constp)
8251199e 10311 pedwarn ("discarding `const' applied to a reference");
a0a33927 10312 if (volatilep)
8251199e 10313 pedwarn ("discarding `volatile' applied to a reference");
91063b51 10314 type_quals &= ~(TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE);
a0a33927 10315 }
91063b51 10316 type = cp_build_qualified_type (type, type_quals);
8d08fdba
MS
10317 }
10318 declarator = TREE_OPERAND (declarator, 0);
10319 ctype = NULL_TREE;
10320 break;
10321
10322 case SCOPE_REF:
10323 {
10324 /* We have converted type names to NULL_TREE if the
10325 name was bogus, or to a _TYPE node, if not.
10326
10327 The variable CTYPE holds the type we will ultimately
10328 resolve to. The code here just needs to build
10329 up appropriate member types. */
10330 tree sname = TREE_OPERAND (declarator, 1);
386b8a85
JM
10331 tree t;
10332
8d08fdba
MS
10333 /* Destructors can have their visibilities changed as well. */
10334 if (TREE_CODE (sname) == BIT_NOT_EXPR)
10335 sname = TREE_OPERAND (sname, 0);
10336
10337 if (TREE_COMPLEXITY (declarator) == 0)
10338 /* This needs to be here, in case we are called
10339 multiple times. */ ;
9a68c51f
JM
10340 else if (TREE_COMPLEXITY (declarator) == -1)
10341 /* Namespace member. */
10342 pop_decl_namespace ();
8d08fdba 10343 else if (friendp && (TREE_COMPLEXITY (declarator) < 2))
2c73f9f5 10344 /* Don't fall out into global scope. Hides real bug? --eichin */ ;
5566b478
MS
10345 else if (! IS_AGGR_TYPE_CODE
10346 (TREE_CODE (TREE_OPERAND (declarator, 0))))
10347 ;
8d08fdba
MS
10348 else if (TREE_COMPLEXITY (declarator) == current_class_depth)
10349 {
9a3b49ac
MS
10350 /* Resolve any TYPENAME_TYPEs from the decl-specifier-seq
10351 that refer to ctype. They couldn't be resolved earlier
10352 because we hadn't pushed into the class yet.
10353 Example: resolve 'B<T>::type' in
10354 'B<typename B<T>::type> B<T>::f () { }'. */
10355 if (current_template_parms
10356 && uses_template_parms (type)
10357 && uses_template_parms (current_class_type))
10358 {
10359 tree args = current_template_args ();
4393e105 10360 type = tsubst (type, args, /*complain=*/1, NULL_TREE);
9a3b49ac
MS
10361 }
10362
a28e3c7f
MS
10363 /* This pop_nested_class corresponds to the
10364 push_nested_class used to push into class scope for
10365 parsing the argument list of a function decl, in
10366 qualified_id. */
b74a0560 10367 pop_nested_class ();
8d08fdba
MS
10368 TREE_COMPLEXITY (declarator) = current_class_depth;
10369 }
10370 else
10371 my_friendly_abort (16);
10372
10373 if (TREE_OPERAND (declarator, 0) == NULL_TREE)
10374 {
10375 /* We had a reference to a global decl, or
10376 perhaps we were given a non-aggregate typedef,
10377 in which case we cleared this out, and should just
10378 keep going as though it wasn't there. */
10379 declarator = sname;
10380 continue;
10381 }
10382 ctype = TREE_OPERAND (declarator, 0);
10383
386b8a85 10384 t = ctype;
7ddedda4 10385 while (t != NULL_TREE && CLASS_TYPE_P (t))
386b8a85 10386 {
75650646
MM
10387 if (CLASSTYPE_TEMPLATE_INFO (t) &&
10388 !CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
386b8a85
JM
10389 template_count += 1;
10390 t = TYPE_MAIN_DECL (t);
10391 if (DECL_LANG_SPECIFIC (t))
10392 t = DECL_CLASS_CONTEXT (t);
10393 else
10394 t = NULL_TREE;
10395 }
10396
8d08fdba
MS
10397 if (sname == NULL_TREE)
10398 goto done_scoping;
10399
10400 if (TREE_CODE (sname) == IDENTIFIER_NODE)
10401 {
10402 /* This is the `standard' use of the scoping operator:
10403 basetype :: member . */
10404
db5ae43f 10405 if (ctype == current_class_type)
28cbf42c
MS
10406 {
10407 /* class A {
10408 void A::f ();
10409 };
10410
10411 Is this ill-formed? */
10412
10413 if (pedantic)
8251199e 10414 cp_pedwarn ("extra qualification `%T::' on member `%s' ignored",
28cbf42c
MS
10415 ctype, name);
10416 }
db5ae43f 10417 else if (TREE_CODE (type) == FUNCTION_TYPE)
8d08fdba
MS
10418 {
10419 if (current_class_type == NULL_TREE
8d08fdba 10420 || friendp)
14ae7e7d
JM
10421 type = build_cplus_method_type (ctype, TREE_TYPE (type),
10422 TYPE_ARG_TYPES (type));
8d08fdba
MS
10423 else
10424 {
8251199e 10425 cp_error ("cannot declare member function `%T::%s' within `%T'",
a3203465 10426 ctype, name, current_class_type);
8d08fdba
MS
10427 return void_type_node;
10428 }
10429 }
5566b478
MS
10430 else if (RIDBIT_SETP (RID_TYPEDEF, specbits)
10431 || TYPE_SIZE (complete_type (ctype)) != NULL_TREE)
8d08fdba 10432 {
2c73f9f5 10433 /* Have to move this code elsewhere in this function.
db5ae43f
MS
10434 this code is used for i.e., typedef int A::M; M *pm;
10435
10436 It is? How? jason 10/2/94 */
8d08fdba 10437
8d08fdba
MS
10438 if (current_class_type)
10439 {
8251199e 10440 cp_error ("cannot declare member `%T::%s' within `%T'",
db5ae43f
MS
10441 ctype, name, current_class_type);
10442 return void_type_node;
8d08fdba
MS
10443 }
10444 type = build_offset_type (ctype, type);
10445 }
10446 else if (uses_template_parms (ctype))
10447 {
8d08fdba 10448 if (TREE_CODE (type) == FUNCTION_TYPE)
beb53fb8 10449 type
14ae7e7d 10450 = build_cplus_method_type (ctype, TREE_TYPE (type),
beb53fb8 10451 TYPE_ARG_TYPES (type));
8d08fdba
MS
10452 }
10453 else
10454 {
8251199e 10455 cp_error ("structure `%T' not yet defined", ctype);
8d08fdba
MS
10456 return error_mark_node;
10457 }
10458
10459 declarator = sname;
10460 }
8d08fdba
MS
10461 else if (TREE_CODE (sname) == SCOPE_REF)
10462 my_friendly_abort (17);
10463 else
10464 {
10465 done_scoping:
10466 declarator = TREE_OPERAND (declarator, 1);
10467 if (declarator && TREE_CODE (declarator) == CALL_EXPR)
10468 /* In this case, we will deal with it later. */
10469 ;
10470 else
10471 {
10472 if (TREE_CODE (type) == FUNCTION_TYPE)
14ae7e7d
JM
10473 type = build_cplus_method_type (ctype, TREE_TYPE (type),
10474 TYPE_ARG_TYPES (type));
8d08fdba
MS
10475 else
10476 type = build_offset_type (ctype, type);
10477 }
10478 }
10479 }
10480 break;
10481
10482 case BIT_NOT_EXPR:
10483 declarator = TREE_OPERAND (declarator, 0);
10484 break;
10485
8d08fdba
MS
10486 case RECORD_TYPE:
10487 case UNION_TYPE:
10488 case ENUMERAL_TYPE:
10489 declarator = NULL_TREE;
10490 break;
10491
10492 case ERROR_MARK:
10493 declarator = NULL_TREE;
10494 break;
10495
10496 default:
10497 my_friendly_abort (158);
10498 }
10499 }
10500
1eab9b56
JM
10501 /* See the comment for the TREE_LIST case, above. */
10502 if (inner_attrs)
10503 {
10504 if (! ignore_attrs)
10505 decl_attributes (type, inner_attrs, NULL_TREE);
10506 else if (attrlist)
10507 TREE_VALUE (attrlist) = chainon (inner_attrs, TREE_VALUE (attrlist));
10508 else
10509 attrlist = build_decl_list (NULL_TREE, inner_attrs);
10510 }
10511
419c6212
JM
10512 /* Now TYPE has the actual type. */
10513
db5ae43f
MS
10514 if (explicitp == 1)
10515 {
8251199e 10516 error ("only constructors can be declared `explicit'");
db5ae43f
MS
10517 explicitp = 0;
10518 }
10519
f30432d7
MS
10520 if (RIDBIT_SETP (RID_MUTABLE, specbits))
10521 {
91063b51 10522 if (type_quals & TYPE_QUAL_CONST)
f30432d7 10523 {
8251199e 10524 error ("const `%s' cannot be declared `mutable'", name);
f30432d7
MS
10525 RIDBIT_RESET (RID_MUTABLE, specbits);
10526 }
10527 else if (staticp)
10528 {
8251199e 10529 error ("static `%s' cannot be declared `mutable'", name);
f30432d7
MS
10530 RIDBIT_RESET (RID_MUTABLE, specbits);
10531 }
10532 }
10533
efe38fab
JM
10534 if (declarator == NULL_TREE
10535 || TREE_CODE (declarator) == IDENTIFIER_NODE
10536 || (TREE_CODE (declarator) == TEMPLATE_ID_EXPR
10537 && (TREE_CODE (type) == FUNCTION_TYPE
10538 || TREE_CODE (type) == METHOD_TYPE)))
10539 /* OK */;
10540 else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
10541 {
10542 cp_error ("template-id `%D' used as a declarator", declarator);
10543 declarator = dname;
10544 }
10545 else
419c6212 10546 /* Unexpected declarator format. */
efe38fab
JM
10547 my_friendly_abort (990210);
10548
419c6212
JM
10549 /* If this is declaring a typedef name, return a TYPE_DECL. */
10550
fc378698 10551 if (RIDBIT_SETP (RID_TYPEDEF, specbits) && decl_context != TYPENAME)
8d08fdba
MS
10552 {
10553 tree decl;
10554
10555 /* Note that the grammar rejects storage classes
10556 in typenames, fields or parameters. */
eff71ab0
PB
10557 if (current_lang_name == lang_name_java)
10558 TYPE_FOR_JAVA (type) = 1;
8d08fdba 10559
d2e5ee5c
MS
10560 if (decl_context == FIELD)
10561 {
094fe153 10562 if (declarator == constructor_name (current_class_type))
8251199e 10563 cp_pedwarn ("ANSI C++ forbids nested type `%D' with same name as enclosing class",
d2e5ee5c
MS
10564 declarator);
10565 decl = build_lang_decl (TYPE_DECL, declarator, type);
d2e5ee5c
MS
10566 }
10567 else
7fb4a8f7
JM
10568 {
10569 /* Make sure this typedef lives as long as its type,
10570 since it might be used as a template parameter. */
1aed5355
MM
10571 if (type != error_mark_node)
10572 push_obstacks (TYPE_OBSTACK (type), TYPE_OBSTACK (type));
9188c363 10573 if (processing_template_decl)
b0d06515 10574 decl = build_lang_decl (TYPE_DECL, declarator, type);
9188c363
MM
10575 else
10576 decl = build_decl (TYPE_DECL, declarator, type);
1aed5355
MM
10577 if (type != error_mark_node)
10578 pop_obstacks ();
7fb4a8f7 10579 }
d2e5ee5c 10580
9188c363
MM
10581 /* If the user declares "typedef struct {...} foo" then the
10582 struct will have an anonymous name. Fill that name in now.
10583 Nothing can refer to it, so nothing needs know about the name
10584 change. */
8d08fdba
MS
10585 if (type != error_mark_node
10586 && TYPE_NAME (type)
10587 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
b537d120
MM
10588 && ANON_AGGRNAME_P (TYPE_IDENTIFIER (type))
10589 && CP_TYPE_QUALS (type) == TYPE_UNQUALIFIED)
8d08fdba 10590 {
dcd08efc
JM
10591 tree oldname = TYPE_NAME (type);
10592 tree t;
10593
2c73f9f5 10594 /* Replace the anonymous name with the real name everywhere. */
8d08fdba 10595 lookup_tag_reverse (type, declarator);
dcd08efc
JM
10596 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
10597 if (TYPE_NAME (t) == oldname)
10598 TYPE_NAME (t) = decl;
8d08fdba
MS
10599
10600 if (TYPE_LANG_SPECIFIC (type))
10601 TYPE_WAS_ANONYMOUS (type) = 1;
10602
33964bf4
MM
10603 /* If this is a typedef within a template class, the nested
10604 type is a (non-primary) template. The name for the
10605 template needs updating as well. */
10606 if (TYPE_LANG_SPECIFIC (type) && CLASSTYPE_TEMPLATE_INFO (type))
10607 DECL_NAME (CLASSTYPE_TI_TEMPLATE (type))
10608 = TYPE_IDENTIFIER (type);
10609
2c73f9f5
ML
10610 /* XXX Temporarily set the scope.
10611 When returning, start_decl expects it as NULL_TREE,
10612 and will then then set it using pushdecl. */
10613 my_friendly_assert (DECL_CONTEXT (decl) == NULL_TREE, 980404);
10614 if (current_class_type)
10615 DECL_CONTEXT (decl) = current_class_type;
10616 else
cb0dbb9a 10617 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
2c73f9f5 10618
d2e5ee5c
MS
10619 DECL_ASSEMBLER_NAME (decl) = DECL_NAME (decl);
10620 DECL_ASSEMBLER_NAME (decl)
10621 = get_identifier (build_overload_name (type, 1, 1));
2c73f9f5 10622 DECL_CONTEXT (decl) = NULL_TREE;
50a6dbd7
JM
10623
10624 /* FIXME remangle member functions; member functions of a
10625 type with external linkage have external linkage. */
fc378698 10626 }
fc378698 10627
8d08fdba
MS
10628 if (TREE_CODE (type) == OFFSET_TYPE || TREE_CODE (type) == METHOD_TYPE)
10629 {
8251199e 10630 cp_error_at ("typedef name may not be class-qualified", decl);
a3203465 10631 return NULL_TREE;
8d08fdba
MS
10632 }
10633 else if (quals)
10634 {
10635 if (ctype == NULL_TREE)
10636 {
10637 if (TREE_CODE (type) != METHOD_TYPE)
8251199e 10638 cp_error_at ("invalid type qualifier for non-method type", decl);
8d08fdba
MS
10639 else
10640 ctype = TYPE_METHOD_BASETYPE (type);
10641 }
10642 if (ctype != NULL_TREE)
10643 grok_method_quals (ctype, decl, quals);
10644 }
10645
10646 if (RIDBIT_SETP (RID_SIGNED, specbits)
10647 || (typedef_decl && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl)))
10648 C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
10649
10650 if (RIDBIT_SETP (RID_MUTABLE, specbits))
61a127b3 10651 error ("non-object member `%s' cannot be declared mutable", name);
8d08fdba 10652
c91a56d2
MS
10653 bad_specifiers (decl, "type", virtualp, quals != NULL_TREE,
10654 inlinep, friendp, raises != NULL_TREE);
10655
10656 if (initialized)
8251199e 10657 error ("typedef declaration includes an initializer");
c91a56d2 10658
8d08fdba
MS
10659 return decl;
10660 }
10661
10662 /* Detect the case of an array type of unspecified size
10663 which came, as such, direct from a typedef name.
10664 We must copy the type, so that each identifier gets
10665 a distinct type, so that each identifier's size can be
10666 controlled separately by its own initializer. */
10667
10668 if (type == typedef_type && TREE_CODE (type) == ARRAY_TYPE
10669 && TYPE_DOMAIN (type) == NULL_TREE)
10670 {
10671 type = build_cplus_array_type (TREE_TYPE (type), TYPE_DOMAIN (type));
10672 }
10673
10674 /* If this is a type name (such as, in a cast or sizeof),
10675 compute the type and return it now. */
10676
10677 if (decl_context == TYPENAME)
10678 {
10679 /* Note that the grammar rejects storage classes
10680 in typenames, fields or parameters. */
91063b51 10681 if (type_quals != TYPE_UNQUALIFIED)
6eabb241 10682 type_quals = TYPE_UNQUALIFIED;
8d08fdba
MS
10683
10684 /* Special case: "friend class foo" looks like a TYPENAME context. */
10685 if (friendp)
10686 {
91063b51 10687 if (type_quals != TYPE_UNQUALIFIED)
b7484fbe 10688 {
91063b51
MM
10689 cp_error ("type qualifiers specified for friend class declaration");
10690 type_quals = TYPE_UNQUALIFIED;
b7484fbe
MS
10691 }
10692 if (inlinep)
10693 {
8251199e 10694 cp_error ("`inline' specified for friend class declaration");
b7484fbe
MS
10695 inlinep = 0;
10696 }
10697
10698 /* Only try to do this stuff if we didn't already give up. */
10699 if (type != integer_type_node)
10700 {
10701 /* A friendly class? */
10702 if (current_class_type)
10703 make_friend_class (current_class_type, TYPE_MAIN_VARIANT (type));
10704 else
8251199e 10705 error ("trying to make class `%s' a friend of global scope",
b7484fbe
MS
10706 TYPE_NAME_STRING (type));
10707 type = void_type_node;
10708 }
8d08fdba
MS
10709 }
10710 else if (quals)
10711 {
8d08fdba 10712 tree dummy = build_decl (TYPE_DECL, declarator, type);
8d08fdba
MS
10713 if (ctype == NULL_TREE)
10714 {
10715 my_friendly_assert (TREE_CODE (type) == METHOD_TYPE, 159);
10716 ctype = TYPE_METHOD_BASETYPE (type);
10717 }
10718 grok_method_quals (ctype, dummy, quals);
10719 type = TREE_TYPE (dummy);
10720 }
10721
10722 return type;
10723 }
10724 else if (declarator == NULL_TREE && decl_context != PARM
db5ae43f 10725 && decl_context != CATCHPARM
8d08fdba
MS
10726 && TREE_CODE (type) != UNION_TYPE
10727 && ! bitfield)
10728 {
8251199e 10729 cp_error ("abstract declarator `%T' used as declaration", type);
8d08fdba
MS
10730 declarator = make_anon_name ();
10731 }
10732
10733 /* `void' at top level (not within pointer)
10734 is allowed only in typedefs or type names.
10735 We don't complain about parms either, but that is because
10736 a better error message can be made later. */
10737
a1774733 10738 if (TREE_CODE (type) == VOID_TYPE && decl_context != PARM)
8d08fdba 10739 {
b7484fbe 10740 if (! declarator)
8251199e 10741 error ("unnamed variable or field declared void");
b7484fbe 10742 else if (TREE_CODE (declarator) == IDENTIFIER_NODE)
8d08fdba
MS
10743 {
10744 if (IDENTIFIER_OPNAME_P (declarator))
8d08fdba 10745 my_friendly_abort (356);
8d08fdba 10746 else
8251199e 10747 error ("variable or field `%s' declared void", name);
8d08fdba
MS
10748 }
10749 else
8251199e 10750 error ("variable or field declared void");
8d08fdba
MS
10751 type = integer_type_node;
10752 }
10753
10754 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
10755 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
10756
14ae7e7d
JM
10757 if (decl_context == PARM || decl_context == CATCHPARM)
10758 {
10759 if (ctype || in_namespace)
10760 error ("cannot use `::' in parameter declaration");
10761
10762 /* A parameter declared as an array of T is really a pointer to T.
10763 One declared as a function is really a pointer to a function.
10764 One declared as a member is really a pointer to member. */
10765
10766 if (TREE_CODE (type) == ARRAY_TYPE)
10767 {
10768 /* Transfer const-ness of array into that of type pointed to. */
10769 type = build_pointer_type (TREE_TYPE (type));
91063b51 10770 type_quals = TYPE_UNQUALIFIED;
14ae7e7d
JM
10771 }
10772 else if (TREE_CODE (type) == FUNCTION_TYPE)
10773 type = build_pointer_type (type);
10774 else if (TREE_CODE (type) == OFFSET_TYPE)
10775 type = build_pointer_type (type);
10776 else if (TREE_CODE (type) == VOID_TYPE && declarator)
10777 {
10778 error ("declaration of `%s' as void", name);
10779 return NULL_TREE;
10780 }
10781 }
10782
8d08fdba
MS
10783 {
10784 register tree decl;
10785
10786 if (decl_context == PARM)
10787 {
ff350acd 10788 decl = build_decl (PARM_DECL, declarator, type);
8d08fdba
MS
10789
10790 bad_specifiers (decl, "parameter", virtualp, quals != NULL_TREE,
10791 inlinep, friendp, raises != NULL_TREE);
8d08fdba
MS
10792
10793 /* Compute the type actually passed in the parmlist,
10794 for the case where there is no prototype.
10795 (For example, shorts and chars are passed as ints.)
10796 When there is a prototype, this is overridden later. */
10797
39211cd5 10798 DECL_ARG_TYPE (decl) = type_promotes_to (type);
8d08fdba
MS
10799 }
10800 else if (decl_context == FIELD)
10801 {
10802 if (type == error_mark_node)
10803 {
10804 /* Happens when declaring arrays of sizes which
10805 are error_mark_node, for example. */
10806 decl = NULL_TREE;
10807 }
997a088c 10808 else if (in_namespace && !friendp)
05008fb9
MM
10809 {
10810 /* Something like struct S { int N::j; }; */
8251199e 10811 cp_error ("invalid use of `::'");
05008fb9
MM
10812 decl = NULL_TREE;
10813 }
8d08fdba
MS
10814 else if (TREE_CODE (type) == FUNCTION_TYPE)
10815 {
10816 int publicp = 0;
e1467ff2 10817 tree function_context;
8d08fdba 10818
72b7eeff
MS
10819 /* We catch the others as conflicts with the builtin
10820 typedefs. */
10821 if (friendp && declarator == ridpointers[(int) RID_SIGNED])
10822 {
8251199e 10823 cp_error ("function `%D' cannot be declared friend",
72b7eeff
MS
10824 declarator);
10825 friendp = 0;
10826 }
10827
8d08fdba
MS
10828 if (friendp == 0)
10829 {
10830 if (ctype == NULL_TREE)
10831 ctype = current_class_type;
10832
10833 if (ctype == NULL_TREE)
10834 {
8251199e 10835 cp_error ("can't make `%D' into a method -- not in a class",
8d08fdba
MS
10836 declarator);
10837 return void_type_node;
10838 }
10839
10840 /* ``A union may [ ... ] not [ have ] virtual functions.''
10841 ARM 9.5 */
10842 if (virtualp && TREE_CODE (ctype) == UNION_TYPE)
10843 {
8251199e 10844 cp_error ("function `%D' declared virtual inside a union",
8d08fdba
MS
10845 declarator);
10846 return void_type_node;
10847 }
10848
10849 if (declarator == ansi_opname[(int) NEW_EXPR]
a28e3c7f
MS
10850 || declarator == ansi_opname[(int) VEC_NEW_EXPR]
10851 || declarator == ansi_opname[(int) DELETE_EXPR]
10852 || declarator == ansi_opname[(int) VEC_DELETE_EXPR])
8d08fdba
MS
10853 {
10854 if (virtualp)
10855 {
8251199e 10856 cp_error ("`%D' cannot be declared virtual, since it is always static",
8d08fdba
MS
10857 declarator);
10858 virtualp = 0;
10859 }
10860 }
10861 else if (staticp < 2)
14ae7e7d
JM
10862 type = build_cplus_method_type (ctype, TREE_TYPE (type),
10863 TYPE_ARG_TYPES (type));
8d08fdba
MS
10864 }
10865
10866 /* Tell grokfndecl if it needs to set TREE_PUBLIC on the node. */
e1467ff2
MM
10867 function_context = (ctype != NULL_TREE) ?
10868 hack_decl_function_context (TYPE_MAIN_DECL (ctype)) : NULL_TREE;
10869 publicp = (! friendp || ! staticp)
10870 && function_context == NULL_TREE;
386b8a85
JM
10871 decl = grokfndecl (ctype, type,
10872 TREE_CODE (declarator) != TEMPLATE_ID_EXPR
10873 ? declarator : dname,
10874 declarator,
7a8f9fa9 10875 virtualp, flags, quals, raises,
386b8a85 10876 friendp ? -1 : 0, friendp, publicp, inlinep,
2c73f9f5 10877 funcdef_flag, template_count, in_namespace);
20496fa2 10878 if (decl == NULL_TREE)
3ddfb0e6 10879 return decl;
6125f3be
DE
10880#if 0
10881 /* This clobbers the attrs stored in `decl' from `attrlist'. */
10882 /* The decl and setting of decl_machine_attr is also turned off. */
71851aaa 10883 decl = build_decl_attribute_variant (decl, decl_machine_attr);
6125f3be 10884#endif
f0e01782 10885
cc804e51
MM
10886 /* [class.conv.ctor]
10887
10888 A constructor declared without the function-specifier
10889 explicit that can be called with a single parameter
10890 specifies a conversion from the type of its first
10891 parameter to the type of its class. Such a constructor
10892 is called a converting constructor. */
db5ae43f
MS
10893 if (explicitp == 2)
10894 DECL_NONCONVERTING_P (decl) = 1;
cc804e51
MM
10895 else if (DECL_CONSTRUCTOR_P (decl))
10896 {
10897 /* The constructor can be called with exactly one
10898 parameter if there is at least one parameter, and
10899 any subsequent parameters have default arguments.
10900 We don't look at the first parameter, which is
10901 really just the `this' parameter for the new
10902 object. */
10903 tree arg_types =
10904 TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)));
10905
10906 /* Skip the `in_chrg' argument too, if present. */
10907 if (TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (decl)))
10908 arg_types = TREE_CHAIN (arg_types);
10909
10910 if (arg_types == void_list_node
10911 || (arg_types
10912 && TREE_CHAIN (arg_types)
10913 && TREE_CHAIN (arg_types) != void_list_node
10914 && !TREE_PURPOSE (TREE_CHAIN (arg_types))))
10915 DECL_NONCONVERTING_P (decl) = 1;
10916 }
8d08fdba
MS
10917 }
10918 else if (TREE_CODE (type) == METHOD_TYPE)
10919 {
faae18ab
MS
10920 /* We only get here for friend declarations of
10921 members of other classes. */
8d08fdba
MS
10922 /* All method decls are public, so tell grokfndecl to set
10923 TREE_PUBLIC, also. */
386b8a85 10924 decl = grokfndecl (ctype, type, declarator, declarator,
7a8f9fa9 10925 virtualp, flags, quals, raises,
386b8a85 10926 friendp ? -1 : 0, friendp, 1, 0, funcdef_flag,
2c73f9f5 10927 template_count, in_namespace);
f0e01782
MS
10928 if (decl == NULL_TREE)
10929 return NULL_TREE;
8d08fdba 10930 }
5566b478
MS
10931 else if (!staticp && ! processing_template_decl
10932 && TYPE_SIZE (complete_type (type)) == NULL_TREE
8d08fdba
MS
10933 && (TREE_CODE (type) != ARRAY_TYPE || initialized == 0))
10934 {
b7484fbe 10935 if (declarator)
8251199e 10936 cp_error ("field `%D' has incomplete type", declarator);
b7484fbe 10937 else
8251199e 10938 cp_error ("name `%T' has incomplete type", type);
8d08fdba
MS
10939
10940 /* If we're instantiating a template, tell them which
10941 instantiation made the field's type be incomplete. */
10942 if (current_class_type
10943 && TYPE_NAME (current_class_type)
d2e5ee5c 10944 && IDENTIFIER_TEMPLATE (TYPE_IDENTIFIER (current_class_type))
8d08fdba
MS
10945 && declspecs && TREE_VALUE (declspecs)
10946 && TREE_TYPE (TREE_VALUE (declspecs)) == type)
8251199e 10947 cp_error (" in instantiation of template `%T'",
db5ae43f
MS
10948 current_class_type);
10949
8d08fdba
MS
10950 type = error_mark_node;
10951 decl = NULL_TREE;
10952 }
10953 else
10954 {
10955 if (friendp)
10956 {
8251199e 10957 error ("`%s' is neither function nor method; cannot be declared friend",
8d08fdba
MS
10958 IDENTIFIER_POINTER (declarator));
10959 friendp = 0;
10960 }
10961 decl = NULL_TREE;
10962 }
10963
10964 if (friendp)
10965 {
10966 /* Friends are treated specially. */
10967 if (ctype == current_class_type)
8251199e 10968 warning ("member functions are implicitly friends of their class");
8d08fdba
MS
10969 else
10970 {
10971 tree t = NULL_TREE;
10972 if (decl && DECL_NAME (decl))
36a117a5
MM
10973 {
10974 if (template_class_depth (current_class_type) == 0)
6c30752f
MM
10975 {
10976 decl
10977 = check_explicit_specialization
10978 (declarator, decl,
10979 template_count, 2 * (funcdef_flag != 0) + 4);
10980 if (decl == error_mark_node)
10981 return error_mark_node;
10982 }
10983
36a117a5 10984 t = do_friend (ctype, declarator, decl,
7a8f9fa9 10985 last_function_parms, attrlist, flags, quals,
36a117a5
MM
10986 funcdef_flag);
10987 }
8d08fdba
MS
10988 if (t && funcdef_flag)
10989 return t;
10990
10991 return void_type_node;
10992 }
10993 }
10994
10995 /* Structure field. It may not be a function, except for C++ */
10996
10997 if (decl == NULL_TREE)
10998 {
8d08fdba
MS
10999 if (initialized)
11000 {
3ac3d9ea
MM
11001 if (!staticp)
11002 {
11003 /* An attempt is being made to initialize a non-static
11004 member. But, from [class.mem]:
11005
11006 4 A member-declarator can contain a
11007 constant-initializer only if it declares a static
11008 member (_class.static_) of integral or enumeration
11009 type, see _class.static.data_.
11010
11011 This used to be relatively common practice, but
11012 the rest of the compiler does not correctly
11013 handle the initialization unless the member is
11014 static so we make it static below. */
950ad3c3 11015 cp_pedwarn ("ANSI C++ forbids initialization of member `%D'",
3ac3d9ea 11016 declarator);
8251199e 11017 cp_pedwarn ("making `%D' static", declarator);
3ac3d9ea
MM
11018 staticp = 1;
11019 }
11020
6ba89f8e
MM
11021 if (uses_template_parms (type))
11022 /* We'll check at instantiation time. */
11023 ;
11024 else if (check_static_variable_definition (declarator,
11025 type))
11026 /* If we just return the declaration, crashes
11027 will sometimes occur. We therefore return
72a93143
JM
11028 void_type_node, as if this was a friend
11029 declaration, to cause callers to completely
11030 ignore this declaration. */
6ba89f8e 11031 return void_type_node;
8d08fdba
MS
11032 }
11033
8ebeee52 11034 /* 9.2p13 [class.mem] */
4d7614e9
JM
11035 if (declarator == constructor_name (current_class_type)
11036 /* Divergence from the standard: In extern "C", we
11037 allow non-static data members here, because C does
11038 and /usr/include/netinet/in.h uses that. */
1f901793 11039 && (staticp || ! in_system_header))
8ebeee52
JM
11040 cp_pedwarn ("ANSI C++ forbids data member `%D' with same name as enclosing class",
11041 declarator);
11042
3ac3d9ea 11043 if (staticp)
8d08fdba
MS
11044 {
11045 /* C++ allows static class members.
11046 All other work for this is done by grokfield.
4ce3d537 11047 This VAR_DECL is built by build_lang_decl.
8d08fdba 11048 All other VAR_DECLs are built by build_decl. */
4ce3d537 11049 decl = build_lang_decl (VAR_DECL, declarator, type);
5b605f68
MS
11050 TREE_STATIC (decl) = 1;
11051 /* In class context, 'static' means public access. */
3ac3d9ea 11052 TREE_PUBLIC (decl) = DECL_EXTERNAL (decl) = 1;
8d08fdba
MS
11053 }
11054 else
11055 {
4ce3d537 11056 decl = build_lang_decl (FIELD_DECL, declarator, type);
8d08fdba
MS
11057 if (RIDBIT_SETP (RID_MUTABLE, specbits))
11058 {
11059 DECL_MUTABLE_P (decl) = 1;
11060 RIDBIT_RESET (RID_MUTABLE, specbits);
11061 }
11062 }
11063
11064 bad_specifiers (decl, "field", virtualp, quals != NULL_TREE,
11065 inlinep, friendp, raises != NULL_TREE);
11066 }
11067 }
11068 else if (TREE_CODE (type) == FUNCTION_TYPE || TREE_CODE (type) == METHOD_TYPE)
11069 {
386b8a85 11070 tree original_name;
8d08fdba
MS
11071 int publicp = 0;
11072
11073 if (! declarator)
11074 return NULL_TREE;
11075
386b8a85
JM
11076 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
11077 original_name = dname;
11078 else
11079 original_name = declarator;
11080
8926095f 11081 if (RIDBIT_SETP (RID_AUTO, specbits))
8251199e 11082 error ("storage class `auto' invalid for function `%s'", name);
8926095f 11083 else if (RIDBIT_SETP (RID_REGISTER, specbits))
8251199e 11084 error ("storage class `register' invalid for function `%s'", name);
8d08fdba
MS
11085
11086 /* Function declaration not at top level.
11087 Storage classes other than `extern' are not allowed
11088 and `extern' makes no difference. */
a9aedbc2 11089 if (! toplevel_bindings_p ()
8926095f
MS
11090 && (RIDBIT_SETP (RID_STATIC, specbits)
11091 || RIDBIT_SETP (RID_INLINE, specbits))
8d08fdba 11092 && pedantic)
8926095f
MS
11093 {
11094 if (RIDBIT_SETP (RID_STATIC, specbits))
8251199e 11095 pedwarn ("storage class `static' invalid for function `%s' declared out of global scope", name);
8926095f 11096 else
8251199e 11097 pedwarn ("storage class `inline' invalid for function `%s' declared out of global scope", name);
8926095f
MS
11098 }
11099
8d08fdba
MS
11100 if (ctype == NULL_TREE)
11101 {
11102 if (virtualp)
11103 {
8251199e 11104 error ("virtual non-class function `%s'", name);
8d08fdba
MS
11105 virtualp = 0;
11106 }
8d08fdba
MS
11107 }
11108 else if (TREE_CODE (type) == FUNCTION_TYPE && staticp < 2)
14ae7e7d
JM
11109 type = build_cplus_method_type (ctype, TREE_TYPE (type),
11110 TYPE_ARG_TYPES (type));
8d08fdba 11111
eb66be0e 11112 /* Record presence of `static'. */
faae18ab 11113 publicp = (ctype != NULL_TREE
a9aedbc2 11114 || RIDBIT_SETP (RID_EXTERN, specbits)
eb66be0e 11115 || !RIDBIT_SETP (RID_STATIC, specbits));
8d08fdba 11116
386b8a85 11117 decl = grokfndecl (ctype, type, original_name, declarator,
7a8f9fa9 11118 virtualp, flags, quals, raises,
75650646 11119 1, friendp,
386b8a85 11120 publicp, inlinep, funcdef_flag,
2c73f9f5 11121 template_count, in_namespace);
f0e01782
MS
11122 if (decl == NULL_TREE)
11123 return NULL_TREE;
8d08fdba 11124
8d08fdba
MS
11125 if (staticp == 1)
11126 {
11127 int illegal_static = 0;
11128
11129 /* Don't allow a static member function in a class, and forbid
11130 declaring main to be static. */
11131 if (TREE_CODE (type) == METHOD_TYPE)
11132 {
8251199e 11133 cp_pedwarn ("cannot declare member function `%D' to have static linkage", decl);
8d08fdba
MS
11134 illegal_static = 1;
11135 }
8d08fdba
MS
11136 else if (current_function_decl)
11137 {
11138 /* FIXME need arm citation */
8251199e 11139 error ("cannot declare static function inside another function");
8d08fdba
MS
11140 illegal_static = 1;
11141 }
11142
11143 if (illegal_static)
11144 {
11145 staticp = 0;
11146 RIDBIT_RESET (RID_STATIC, specbits);
11147 }
11148 }
8d08fdba
MS
11149 }
11150 else
11151 {
11152 /* It's a variable. */
11153
11154 /* An uninitialized decl with `extern' is a reference. */
2c73f9f5 11155 decl = grokvardecl (type, declarator, &specbits,
950ad3c3
MM
11156 initialized,
11157 (type_quals & TYPE_QUAL_CONST) != 0,
11158 in_namespace);
8d08fdba
MS
11159 bad_specifiers (decl, "variable", virtualp, quals != NULL_TREE,
11160 inlinep, friendp, raises != NULL_TREE);
11161
11162 if (ctype)
11163 {
f0e01782 11164 DECL_CONTEXT (decl) = ctype;
8d08fdba
MS
11165 if (staticp == 1)
11166 {
8251199e 11167 cp_pedwarn ("static member `%D' re-declared as static", decl);
8d08fdba
MS
11168 staticp = 0;
11169 RIDBIT_RESET (RID_STATIC, specbits);
11170 }
b7484fbe
MS
11171 if (RIDBIT_SETP (RID_REGISTER, specbits) && TREE_STATIC (decl))
11172 {
8251199e 11173 cp_error ("static member `%D' declared `register'", decl);
b7484fbe
MS
11174 RIDBIT_RESET (RID_REGISTER, specbits);
11175 }
f30432d7 11176 if (RIDBIT_SETP (RID_EXTERN, specbits) && pedantic)
8d08fdba 11177 {
8251199e 11178 cp_pedwarn ("cannot explicitly declare member `%#D' to have extern linkage",
f30432d7 11179 decl);
8d08fdba
MS
11180 RIDBIT_RESET (RID_EXTERN, specbits);
11181 }
11182 }
11183 }
11184
11185 if (RIDBIT_SETP (RID_MUTABLE, specbits))
11186 {
8251199e 11187 error ("`%s' cannot be declared mutable", name);
8d08fdba
MS
11188 }
11189
11190 /* Record `register' declaration for warnings on &
11191 and in case doing stupid register allocation. */
11192
11193 if (RIDBIT_SETP (RID_REGISTER, specbits))
11194 DECL_REGISTER (decl) = 1;
11195
8926095f
MS
11196 if (RIDBIT_SETP (RID_EXTERN, specbits))
11197 DECL_THIS_EXTERN (decl) = 1;
11198
faae18ab
MS
11199 if (RIDBIT_SETP (RID_STATIC, specbits))
11200 DECL_THIS_STATIC (decl) = 1;
11201
adecb3f4
MM
11202 /* Record constancy and volatility. There's no need to do this
11203 when processing a template; we'll do this for the instantiated
11204 declaration based on the type of DECL. */
11205 if (!processing_template_decl)
11206 c_apply_type_quals_to_decl (type_quals, decl);
8d08fdba
MS
11207
11208 return decl;
11209 }
11210}
11211\f
11212/* Tell if a parmlist/exprlist looks like an exprlist or a parmlist.
11213 An empty exprlist is a parmlist. An exprlist which
11214 contains only identifiers at the global level
11215 is a parmlist. Otherwise, it is an exprlist. */
e92cc029 11216
8d08fdba
MS
11217int
11218parmlist_is_exprlist (exprs)
11219 tree exprs;
11220{
11221 if (exprs == NULL_TREE || TREE_PARMLIST (exprs))
11222 return 0;
11223
a9aedbc2 11224 if (toplevel_bindings_p ())
8d08fdba
MS
11225 {
11226 /* At the global level, if these are all identifiers,
11227 then it is a parmlist. */
11228 while (exprs)
11229 {
11230 if (TREE_CODE (TREE_VALUE (exprs)) != IDENTIFIER_NODE)
11231 return 1;
11232 exprs = TREE_CHAIN (exprs);
11233 }
11234 return 0;
11235 }
11236 return 1;
11237}
11238
f181d4ae
MM
11239/* Subroutine of start_function. Ensure that each of the parameter
11240 types (as listed in PARMS) is complete, as is required for a
11241 function definition. */
e92cc029 11242
8d08fdba
MS
11243static void
11244require_complete_types_for_parms (parms)
11245 tree parms;
11246{
11247 while (parms)
11248 {
11249 tree type = TREE_TYPE (parms);
5566b478 11250 if (TYPE_SIZE (complete_type (type)) == NULL_TREE)
8d08fdba
MS
11251 {
11252 if (DECL_NAME (parms))
8251199e 11253 error ("parameter `%s' has incomplete type",
8d08fdba
MS
11254 IDENTIFIER_POINTER (DECL_NAME (parms)));
11255 else
8251199e 11256 error ("parameter has incomplete type");
8d08fdba
MS
11257 TREE_TYPE (parms) = error_mark_node;
11258 }
42f989ff
JM
11259 else
11260 layout_decl (parms, 0);
f181d4ae 11261
8d08fdba
MS
11262 parms = TREE_CHAIN (parms);
11263 }
11264}
11265
297e73d8
MM
11266/* Returns DECL if DECL is a local variable (or parameter). Returns
11267 NULL_TREE otherwise. */
11268
11269static tree
11270local_variable_p (t)
11271 tree t;
11272{
11273 if ((TREE_CODE (t) == VAR_DECL
11274 /* A VAR_DECL with a context that is a _TYPE is a static data
11275 member. */
11276 && !TYPE_P (CP_DECL_CONTEXT (t))
11277 /* Any other non-local variable must be at namespace scope. */
11278 && TREE_CODE (CP_DECL_CONTEXT (t)) != NAMESPACE_DECL)
11279 || (TREE_CODE (t) == PARM_DECL))
11280 return t;
11281
11282 return NULL_TREE;
11283}
11284
11285/* Check that ARG, which is a default-argument expression for a
11286 parameter DECL, is legal. Returns ARG, or ERROR_MARK_NODE, if
11287 something goes wrong. DECL may also be a _TYPE node, rather than a
11288 DECL, if there is no DECL available. */
11289
11290tree
11291check_default_argument (decl, arg)
11292 tree decl;
11293 tree arg;
11294{
11295 tree var;
11296 tree decl_type;
11297
11298 if (TREE_CODE (arg) == DEFAULT_ARG)
11299 /* We get a DEFAULT_ARG when looking at an in-class declaration
11300 with a default argument. Ignore the argument for now; we'll
11301 deal with it after the class is complete. */
11302 return arg;
11303
11304 if (processing_template_decl || uses_template_parms (arg))
11305 /* We don't do anything checking until instantiation-time. Note
11306 that there may be uninstantiated arguments even for an
11307 instantiated function, since default arguments are not
11308 instantiated until they are needed. */
11309 return arg;
11310
11311 if (TYPE_P (decl))
11312 {
11313 decl_type = decl;
11314 decl = NULL_TREE;
11315 }
11316 else
11317 decl_type = TREE_TYPE (decl);
11318
11319 if (arg == error_mark_node
11320 || decl == error_mark_node
11321 || TREE_TYPE (arg) == error_mark_node
11322 || decl_type == error_mark_node)
11323 /* Something already went wrong. There's no need to check
11324 further. */
11325 return error_mark_node;
11326
11327 /* [dcl.fct.default]
11328
11329 A default argument expression is implicitly converted to the
11330 parameter type. */
11331 if (!TREE_TYPE (arg)
11332 || !can_convert_arg (decl_type, TREE_TYPE (arg), arg))
11333 {
11334 if (decl)
11335 cp_error ("default argument for `%#D' has type `%T'",
11336 decl, TREE_TYPE (arg));
11337 else
8614a1d6 11338 cp_error ("default argument for parameter of type `%T' has type `%T'",
297e73d8
MM
11339 decl_type, TREE_TYPE (arg));
11340
11341 return error_mark_node;
11342 }
11343
11344 /* [dcl.fct.default]
11345
11346 Local variables shall not be used in default argument
11347 expressions.
11348
11349 The keyword `this' shall not be used in a default argument of a
11350 member function. */
11351 var = search_tree (arg, local_variable_p);
11352 if (var)
11353 {
11354 cp_error ("default argument `%E' uses local variable `%D'",
11355 arg, var);
11356 return error_mark_node;
11357 }
11358
11359 /* All is well. */
11360 return arg;
11361}
11362
8d08fdba
MS
11363/* Decode the list of parameter types for a function type.
11364 Given the list of things declared inside the parens,
11365 return a list of types.
11366
11367 The list we receive can have three kinds of elements:
11368 an IDENTIFIER_NODE for names given without types,
11369 a TREE_LIST node for arguments given as typespecs or names with typespecs,
11370 or void_type_node, to mark the end of an argument list
11371 when additional arguments are not permitted (... was not used).
11372
11373 FUNCDEF_FLAG is nonzero for a function definition, 0 for
11374 a mere declaration. A nonempty identifier-list gets an error message
11375 when FUNCDEF_FLAG is zero.
11376 If FUNCDEF_FLAG is 1, then parameter types must be complete.
11377 If FUNCDEF_FLAG is -1, then parameter types may be incomplete.
11378
11379 If all elements of the input list contain types,
11380 we return a list of the types.
11381 If all elements contain no type (except perhaps a void_type_node
11382 at the end), we return a null list.
11383 If some have types and some do not, it is an error, and we
11384 return a null list.
11385
11386 Also set last_function_parms to either
11387 a list of names (IDENTIFIER_NODEs) or a chain of PARM_DECLs.
11388 A list of names is converted to a chain of PARM_DECLs
11389 by store_parm_decls so that ultimately it is always a chain of decls.
11390
11391 Note that in C++, parameters can take default values. These default
11392 values are in the TREE_PURPOSE field of the TREE_LIST. It is
11393 an error to specify default values which are followed by parameters
11394 that have no default values, or an ELLIPSES. For simplicities sake,
11395 only parameters which are specified with their types can take on
11396 default values. */
11397
11398static tree
11399grokparms (first_parm, funcdef_flag)
11400 tree first_parm;
11401 int funcdef_flag;
11402{
11403 tree result = NULL_TREE;
11404 tree decls = NULL_TREE;
11405
11406 if (first_parm != NULL_TREE
11407 && TREE_CODE (TREE_VALUE (first_parm)) == IDENTIFIER_NODE)
11408 {
11409 if (! funcdef_flag)
8251199e 11410 pedwarn ("parameter names (without types) in function declaration");
8d08fdba
MS
11411 last_function_parms = first_parm;
11412 return NULL_TREE;
11413 }
11414 else if (first_parm != NULL_TREE
11415 && TREE_CODE (TREE_VALUE (first_parm)) != TREE_LIST
a1774733 11416 && TREE_CODE (TREE_VALUE (first_parm)) != VOID_TYPE)
8d08fdba
MS
11417 my_friendly_abort (145);
11418 else
11419 {
11420 /* Types were specified. This is a list of declarators
11421 each represented as a TREE_LIST node. */
11422 register tree parm, chain;
5566b478 11423 int any_init = 0, any_error = 0;
8d08fdba
MS
11424
11425 if (first_parm != NULL_TREE)
11426 {
11427 tree last_result = NULL_TREE;
11428 tree last_decl = NULL_TREE;
11429
11430 for (parm = first_parm; parm != NULL_TREE; parm = chain)
11431 {
a703fb38 11432 tree type = NULL_TREE, list_node = parm;
8d08fdba
MS
11433 register tree decl = TREE_VALUE (parm);
11434 tree init = TREE_PURPOSE (parm);
11435
11436 chain = TREE_CHAIN (parm);
11437 /* @@ weak defense against parse errors. */
a1774733
BK
11438 if (TREE_CODE (decl) != VOID_TYPE
11439 && TREE_CODE (decl) != TREE_LIST)
8d08fdba
MS
11440 {
11441 /* Give various messages as the need arises. */
11442 if (TREE_CODE (decl) == STRING_CST)
8251199e 11443 cp_error ("invalid string constant `%E'", decl);
8d08fdba 11444 else if (TREE_CODE (decl) == INTEGER_CST)
8251199e 11445 error ("invalid integer constant in parameter list, did you forget to give parameter name?");
8d08fdba
MS
11446 continue;
11447 }
11448
a1774733 11449 if (TREE_CODE (decl) != VOID_TYPE)
8d08fdba 11450 {
8d08fdba
MS
11451 decl = grokdeclarator (TREE_VALUE (decl),
11452 TREE_PURPOSE (decl),
f30432d7 11453 PARM, init != NULL_TREE,
c11b6f21 11454 NULL_TREE);
3bfdc719 11455 if (! decl || TREE_TYPE (decl) == error_mark_node)
8d08fdba 11456 continue;
01240200
MM
11457
11458 /* Top-level qualifiers on the parameters are
11459 ignored for function types. */
11460 type = TYPE_MAIN_VARIANT (TREE_TYPE (decl));
11461
a1774733 11462 if (TREE_CODE (type) == VOID_TYPE)
8d08fdba
MS
11463 decl = void_type_node;
11464 else if (TREE_CODE (type) == METHOD_TYPE)
11465 {
11466 if (DECL_NAME (decl))
fc378698 11467 /* Cannot use the decl here because
8d08fdba 11468 we don't have DECL_CONTEXT set up yet. */
8251199e 11469 cp_error ("parameter `%D' invalidly declared method type",
fc378698 11470 DECL_NAME (decl));
8d08fdba 11471 else
8251199e 11472 error ("parameter invalidly declared method type");
8d08fdba
MS
11473 type = build_pointer_type (type);
11474 TREE_TYPE (decl) = type;
11475 }
11476 else if (TREE_CODE (type) == OFFSET_TYPE)
11477 {
11478 if (DECL_NAME (decl))
8251199e 11479 cp_error ("parameter `%D' invalidly declared offset type",
fc378698 11480 DECL_NAME (decl));
8d08fdba 11481 else
8251199e 11482 error ("parameter invalidly declared offset type");
8d08fdba
MS
11483 type = build_pointer_type (type);
11484 TREE_TYPE (decl) = type;
11485 }
a7a64a77
MM
11486 else if (abstract_virtuals_error (decl, type))
11487 any_error = 1; /* Seems like a good idea. */
482b883f
JM
11488 else if (POINTER_TYPE_P (type))
11489 {
11490 tree t = type;
11491 while (POINTER_TYPE_P (t)
11492 || (TREE_CODE (t) == ARRAY_TYPE
11493 && TYPE_DOMAIN (t) != NULL_TREE))
11494 t = TREE_TYPE (t);
11495 if (TREE_CODE (t) == ARRAY_TYPE)
57af8358
MM
11496 cp_error ("parameter type `%T' includes %s to array of unknown bound",
11497 type,
11498 TYPE_PTR_P (type) ? "pointer" : "reference");
482b883f 11499 }
8d08fdba
MS
11500 }
11501
a1774733 11502 if (TREE_CODE (decl) == VOID_TYPE)
8d08fdba
MS
11503 {
11504 if (result == NULL_TREE)
11505 {
11506 result = void_list_node;
11507 last_result = result;
11508 }
11509 else
11510 {
11511 TREE_CHAIN (last_result) = void_list_node;
11512 last_result = void_list_node;
11513 }
8d08fdba
MS
11514 if (chain
11515 && (chain != void_list_node || TREE_CHAIN (chain)))
8251199e 11516 error ("`void' in parameter list must be entire list");
8d08fdba
MS
11517 break;
11518 }
11519
11520 /* Since there is a prototype, args are passed in their own types. */
11521 DECL_ARG_TYPE (decl) = TREE_TYPE (decl);
fa56377d
JJ
11522 if (PROMOTE_PROTOTYPES
11523 && (TREE_CODE (type) == INTEGER_TYPE
11524 || TREE_CODE (type) == ENUMERAL_TYPE)
8d08fdba
MS
11525 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
11526 DECL_ARG_TYPE (decl) = integer_type_node;
297e73d8 11527 if (!any_error && init)
8d08fdba 11528 {
297e73d8
MM
11529 any_init++;
11530 init = check_default_argument (decl, init);
8d08fdba
MS
11531 }
11532 else
11533 init = NULL_TREE;
11534
11535 if (decls == NULL_TREE)
11536 {
11537 decls = decl;
11538 last_decl = decls;
11539 }
11540 else
11541 {
11542 TREE_CHAIN (last_decl) = decl;
11543 last_decl = decl;
11544 }
66f2e88d 11545 if (! current_function_decl && TREE_PERMANENT (list_node))
8d08fdba
MS
11546 {
11547 TREE_PURPOSE (list_node) = init;
11548 TREE_VALUE (list_node) = type;
11549 TREE_CHAIN (list_node) = NULL_TREE;
11550 }
11551 else
11552 list_node = saveable_tree_cons (init, type, NULL_TREE);
11553 if (result == NULL_TREE)
11554 {
11555 result = list_node;
11556 last_result = result;
11557 }
11558 else
11559 {
11560 TREE_CHAIN (last_result) = list_node;
11561 last_result = list_node;
11562 }
11563 }
11564 if (last_result)
11565 TREE_CHAIN (last_result) = NULL_TREE;
11566 /* If there are no parameters, and the function does not end
11567 with `...', then last_decl will be NULL_TREE. */
11568 if (last_decl != NULL_TREE)
11569 TREE_CHAIN (last_decl) = NULL_TREE;
11570 }
11571 }
11572
11573 last_function_parms = decls;
11574
8d08fdba
MS
11575 return result;
11576}
42976354
BK
11577
11578/* Called from the parser to update an element of TYPE_ARG_TYPES for some
11579 FUNCTION_TYPE with the newly parsed version of its default argument, which
11580 was previously digested as text. See snarf_defarg et al in lex.c. */
11581
11582void
11583replace_defarg (arg, init)
11584 tree arg, init;
11585{
aa5f3bad 11586 if (! processing_template_decl
42976354 11587 && ! can_convert_arg (TREE_VALUE (arg), TREE_TYPE (init), init))
8251199e 11588 cp_pedwarn ("invalid type `%T' for default argument to `%T'",
42976354
BK
11589 TREE_TYPE (init), TREE_VALUE (arg));
11590 TREE_PURPOSE (arg) = init;
11591}
8d08fdba 11592\f
c11b6f21
MS
11593int
11594copy_args_p (d)
11595 tree d;
11596{
11597 tree t = FUNCTION_ARG_CHAIN (d);
67437d5b
JM
11598 if (DECL_CONSTRUCTOR_P (d)
11599 && TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (d)))
11600 t = TREE_CHAIN (t);
c11b6f21
MS
11601 if (t && TREE_CODE (TREE_VALUE (t)) == REFERENCE_TYPE
11602 && (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_VALUE (t)))
11603 == DECL_CLASS_CONTEXT (d))
11604 && (TREE_CHAIN (t) == NULL_TREE
11605 || TREE_CHAIN (t) == void_list_node
11606 || TREE_PURPOSE (TREE_CHAIN (t))))
11607 return 1;
11608 return 0;
11609}
11610
8d08fdba
MS
11611/* These memoizing functions keep track of special properties which
11612 a class may have. `grok_ctor_properties' notices whether a class
11613 has a constructor of the form X(X&), and also complains
11614 if the class has a constructor of the form X(X).
11615 `grok_op_properties' takes notice of the various forms of
11616 operator= which are defined, as well as what sorts of type conversion
11617 may apply. Both functions take a FUNCTION_DECL as an argument. */
e92cc029 11618
a0a33927 11619int
8d08fdba
MS
11620grok_ctor_properties (ctype, decl)
11621 tree ctype, decl;
11622{
11623 tree parmtypes = FUNCTION_ARG_CHAIN (decl);
11624 tree parmtype = parmtypes ? TREE_VALUE (parmtypes) : void_type_node;
11625
11626 /* When a type has virtual baseclasses, a magical first int argument is
11627 added to any ctor so we can tell if the class has been initialized
11628 yet. This could screw things up in this function, so we deliberately
11629 ignore the leading int if we're in that situation. */
711734a9 11630 if (TYPE_USES_VIRTUAL_BASECLASSES (ctype))
8d08fdba 11631 {
711734a9
JM
11632 my_friendly_assert (parmtypes
11633 && TREE_VALUE (parmtypes) == integer_type_node,
11634 980529);
8d08fdba
MS
11635 parmtypes = TREE_CHAIN (parmtypes);
11636 parmtype = TREE_VALUE (parmtypes);
11637 }
11638
f181d4ae
MM
11639 /* [class.copy]
11640
11641 A non-template constructor for class X is a copy constructor if
11642 its first parameter is of type X&, const X&, volatile X& or const
11643 volatile X&, and either there are no other parameters or else all
11644 other parameters have default arguments. */
8d08fdba 11645 if (TREE_CODE (parmtype) == REFERENCE_TYPE
0b41abe6
JM
11646 && TYPE_MAIN_VARIANT (TREE_TYPE (parmtype)) == ctype
11647 && (TREE_CHAIN (parmtypes) == NULL_TREE
8d08fdba 11648 || TREE_CHAIN (parmtypes) == void_list_node
f181d4ae
MM
11649 || TREE_PURPOSE (TREE_CHAIN (parmtypes)))
11650 && !(DECL_TEMPLATE_INSTANTIATION (decl)
11651 && is_member_template (DECL_TI_TEMPLATE (decl))))
0b41abe6
JM
11652 {
11653 TYPE_HAS_INIT_REF (ctype) = 1;
91063b51 11654 if (CP_TYPE_CONST_P (TREE_TYPE (parmtype)))
0b41abe6 11655 TYPE_HAS_CONST_INIT_REF (ctype) = 1;
8d08fdba 11656 }
f181d4ae
MM
11657 /* [class.copy]
11658
11659 A declaration of a constructor for a class X is ill-formed if its
11660 first parameter is of type (optionally cv-qualified) X and either
11661 there are no other parameters or else all other parameters have
11662 default arguments.
11663
11664 We *don't* complain about member template instantiations that
11665 have this form, though; they can occur as we try to decide what
11666 constructor to use during overload resolution. Since overload
11667 resolution will never prefer such a constructor to the
11668 non-template copy constructor (which is either explicitly or
11669 implicitly defined), there's no need to worry about their
11670 existence. Theoretically, they should never even be
11671 instantiated, but that's hard to forestall. */
0b41abe6 11672 else if (TYPE_MAIN_VARIANT (parmtype) == ctype
59e76fc6
JM
11673 && (TREE_CHAIN (parmtypes) == NULL_TREE
11674 || TREE_CHAIN (parmtypes) == void_list_node
f181d4ae
MM
11675 || TREE_PURPOSE (TREE_CHAIN (parmtypes)))
11676 && !(DECL_TEMPLATE_INSTANTIATION (decl)
11677 && is_member_template (DECL_TI_TEMPLATE (decl))))
8d08fdba 11678 {
8251199e 11679 cp_error ("invalid constructor; you probably meant `%T (const %T&)'",
0b41abe6
JM
11680 ctype, ctype);
11681 SET_IDENTIFIER_ERROR_LOCUS (DECL_NAME (decl), ctype);
11682 return 0;
8d08fdba
MS
11683 }
11684 else if (TREE_CODE (parmtype) == VOID_TYPE
11685 || TREE_PURPOSE (parmtypes) != NULL_TREE)
11686 TYPE_HAS_DEFAULT_CONSTRUCTOR (ctype) = 1;
a0a33927
MS
11687
11688 return 1;
8d08fdba
MS
11689}
11690
11691/* An operator with this name can be either unary or binary. */
e92cc029 11692
a28e3c7f
MS
11693static int
11694ambi_op_p (name)
8d08fdba
MS
11695 tree name;
11696{
11697 return (name == ansi_opname [(int) INDIRECT_REF]
11698 || name == ansi_opname [(int) ADDR_EXPR]
11699 || name == ansi_opname [(int) NEGATE_EXPR]
11700 || name == ansi_opname[(int) POSTINCREMENT_EXPR]
11701 || name == ansi_opname[(int) POSTDECREMENT_EXPR]
11702 || name == ansi_opname [(int) CONVERT_EXPR]);
11703}
11704
11705/* An operator with this name can only be unary. */
e92cc029 11706
a28e3c7f
MS
11707static int
11708unary_op_p (name)
8d08fdba
MS
11709 tree name;
11710{
11711 return (name == ansi_opname [(int) TRUTH_NOT_EXPR]
11712 || name == ansi_opname [(int) BIT_NOT_EXPR]
11713 || name == ansi_opname [(int) COMPONENT_REF]
4c571114 11714 || IDENTIFIER_TYPENAME_P (name));
8d08fdba
MS
11715}
11716
11717/* Do a little sanity-checking on how they declared their operator. */
e92cc029 11718
5566b478 11719void
8d08fdba
MS
11720grok_op_properties (decl, virtualp, friendp)
11721 tree decl;
11722 int virtualp, friendp;
11723{
11724 tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
11725 int methodp = (TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE);
11726 tree name = DECL_NAME (decl);
8d08fdba 11727
a28e3c7f
MS
11728 if (current_class_type == NULL_TREE)
11729 friendp = 1;
8d08fdba 11730
a28e3c7f
MS
11731 if (! friendp)
11732 {
f181d4ae
MM
11733 /* [class.copy]
11734
11735 A user-declared copy assignment operator X::operator= is a
11736 non-static non-template member function of class X with
11737 exactly one parameter of type X, X&, const X&, volatile X& or
11738 const volatile X&. */
11739 if (name == ansi_opname[(int) MODIFY_EXPR]
11740 && !(DECL_TEMPLATE_INSTANTIATION (decl)
11741 && is_member_template (DECL_TI_TEMPLATE (decl))))
6a629cac 11742 ;
a28e3c7f
MS
11743 else if (name == ansi_opname[(int) CALL_EXPR])
11744 TYPE_OVERLOADS_CALL_EXPR (current_class_type) = 1;
11745 else if (name == ansi_opname[(int) ARRAY_REF])
11746 TYPE_OVERLOADS_ARRAY_REF (current_class_type) = 1;
11747 else if (name == ansi_opname[(int) COMPONENT_REF]
11748 || name == ansi_opname[(int) MEMBER_REF])
11749 TYPE_OVERLOADS_ARROW (current_class_type) = 1;
11750 else if (name == ansi_opname[(int) NEW_EXPR])
11751 TYPE_GETS_NEW (current_class_type) |= 1;
11752 else if (name == ansi_opname[(int) DELETE_EXPR])
11753 TYPE_GETS_DELETE (current_class_type) |= 1;
11754 else if (name == ansi_opname[(int) VEC_NEW_EXPR])
11755 TYPE_GETS_NEW (current_class_type) |= 2;
11756 else if (name == ansi_opname[(int) VEC_DELETE_EXPR])
11757 TYPE_GETS_DELETE (current_class_type) |= 2;
11758 }
11759
11760 if (name == ansi_opname[(int) NEW_EXPR]
11761 || name == ansi_opname[(int) VEC_NEW_EXPR])
8d08fdba 11762 {
8d08fdba
MS
11763 /* When the compiler encounters the definition of A::operator new, it
11764 doesn't look at the class declaration to find out if it's static. */
a28e3c7f 11765 if (methodp)
700f8a87 11766 revert_static_member_fn (&decl, NULL, NULL);
8d08fdba
MS
11767
11768 /* Take care of function decl if we had syntax errors. */
11769 if (argtypes == NULL_TREE)
beb53fb8
JM
11770 TREE_TYPE (decl)
11771 = build_function_type (ptr_type_node,
11772 hash_tree_chain (integer_type_node,
11773 void_list_node));
8d08fdba 11774 else
a28e3c7f 11775 TREE_TYPE (decl) = coerce_new_type (TREE_TYPE (decl));
8d08fdba 11776 }
a28e3c7f
MS
11777 else if (name == ansi_opname[(int) DELETE_EXPR]
11778 || name == ansi_opname[(int) VEC_DELETE_EXPR])
8d08fdba 11779 {
a28e3c7f 11780 if (methodp)
700f8a87 11781 revert_static_member_fn (&decl, NULL, NULL);
8d08fdba
MS
11782
11783 if (argtypes == NULL_TREE)
beb53fb8
JM
11784 TREE_TYPE (decl)
11785 = build_function_type (void_type_node,
11786 hash_tree_chain (ptr_type_node,
11787 void_list_node));
8d08fdba 11788 else
a28e3c7f
MS
11789 {
11790 TREE_TYPE (decl) = coerce_delete_type (TREE_TYPE (decl));
11791
11792 if (! friendp && name == ansi_opname[(int) VEC_DELETE_EXPR]
11793 && (TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
11794 != void_list_node))
11795 TYPE_VEC_DELETE_TAKES_SIZE (current_class_type) = 1;
11796 }
8d08fdba 11797 }
8d08fdba
MS
11798 else
11799 {
11800 /* An operator function must either be a non-static member function
11801 or have at least one parameter of a class, a reference to a class,
11802 an enumeration, or a reference to an enumeration. 13.4.0.6 */
700f8a87 11803 if (! methodp || DECL_STATIC_FUNCTION_P (decl))
8d08fdba 11804 {
4c571114 11805 if (IDENTIFIER_TYPENAME_P (name)
8d08fdba
MS
11806 || name == ansi_opname[(int) CALL_EXPR]
11807 || name == ansi_opname[(int) MODIFY_EXPR]
11808 || name == ansi_opname[(int) COMPONENT_REF]
11809 || name == ansi_opname[(int) ARRAY_REF])
8251199e 11810 cp_error ("`%D' must be a nonstatic member function", decl);
8d08fdba
MS
11811 else
11812 {
11813 tree p = argtypes;
11814
700f8a87 11815 if (DECL_STATIC_FUNCTION_P (decl))
8251199e 11816 cp_error ("`%D' must be either a non-static member function or a non-member function", decl);
700f8a87 11817
8d08fdba 11818 if (p)
a1774733 11819 for (; TREE_CODE (TREE_VALUE (p)) != VOID_TYPE ; p = TREE_CHAIN (p))
8d08fdba
MS
11820 {
11821 tree arg = TREE_VALUE (p);
11822 if (TREE_CODE (arg) == REFERENCE_TYPE)
11823 arg = TREE_TYPE (arg);
11824
11825 /* This lets bad template code slip through. */
11826 if (IS_AGGR_TYPE (arg)
11827 || TREE_CODE (arg) == ENUMERAL_TYPE
73b0fce8
KL
11828 || TREE_CODE (arg) == TEMPLATE_TYPE_PARM
11829 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
8d08fdba
MS
11830 goto foundaggr;
11831 }
8251199e
JM
11832 cp_error
11833 ("`%D' must have an argument of class or enumerated type",
8d08fdba
MS
11834 decl);
11835 foundaggr:
11836 ;
11837 }
11838 }
11839
277294d7 11840 if (name == ansi_opname[(int) CALL_EXPR])
2c73f9f5 11841 return; /* No restrictions on args. */
8d08fdba 11842
9a3b49ac 11843 if (IDENTIFIER_TYPENAME_P (name) && ! DECL_TEMPLATE_INFO (decl))
a0a33927
MS
11844 {
11845 tree t = TREE_TYPE (name);
11846 if (TREE_CODE (t) == VOID_TYPE)
8251199e 11847 pedwarn ("void is not a valid type conversion operator");
a0a33927
MS
11848 else if (! friendp)
11849 {
11850 int ref = (TREE_CODE (t) == REFERENCE_TYPE);
d8e178a0 11851 const char *what = 0;
a0a33927
MS
11852 if (ref)
11853 t = TYPE_MAIN_VARIANT (TREE_TYPE (t));
11854
11855 if (t == current_class_type)
11856 what = "the same type";
9a3b49ac 11857 /* Don't force t to be complete here. */
a0a33927 11858 else if (IS_AGGR_TYPE (t)
9a3b49ac 11859 && TYPE_SIZE (t)
a0a33927
MS
11860 && DERIVED_FROM_P (t, current_class_type))
11861 what = "a base class";
11862
11863 if (what)
8251199e 11864 warning ("conversion to %s%s will never use a type conversion operator",
a0a33927
MS
11865 ref ? "a reference to " : "", what);
11866 }
11867 }
11868
8d08fdba
MS
11869 if (name == ansi_opname[(int) MODIFY_EXPR])
11870 {
11871 tree parmtype;
11872
11873 if (list_length (argtypes) != 3 && methodp)
11874 {
8251199e 11875 cp_error ("`%D' must take exactly one argument", decl);
8d08fdba
MS
11876 return;
11877 }
11878 parmtype = TREE_VALUE (TREE_CHAIN (argtypes));
11879
f0e01782 11880 if (copy_assignment_arg_p (parmtype, virtualp)
a28e3c7f 11881 && ! friendp)
8d08fdba
MS
11882 {
11883 TYPE_HAS_ASSIGN_REF (current_class_type) = 1;
f0e01782 11884 if (TREE_CODE (parmtype) != REFERENCE_TYPE
91063b51 11885 || CP_TYPE_CONST_P (TREE_TYPE (parmtype)))
8d08fdba
MS
11886 TYPE_HAS_CONST_ASSIGN_REF (current_class_type) = 1;
11887 }
11888 }
11889 else if (name == ansi_opname[(int) COND_EXPR])
11890 {
11891 /* 13.4.0.3 */
a7a64a77 11892 cp_error ("ANSI C++ prohibits overloading operator ?:");
8d08fdba
MS
11893 }
11894 else if (ambi_op_p (name))
11895 {
11896 if (list_length (argtypes) == 2)
11897 /* prefix */;
11898 else if (list_length (argtypes) == 3)
11899 {
11900 if ((name == ansi_opname[(int) POSTINCREMENT_EXPR]
11901 || name == ansi_opname[(int) POSTDECREMENT_EXPR])
5156628f 11902 && ! processing_template_decl
007e5fea 11903 && ! same_type_p (TREE_VALUE (TREE_CHAIN (argtypes)), integer_type_node))
8d08fdba
MS
11904 {
11905 if (methodp)
8251199e 11906 cp_error ("postfix `%D' must take `int' as its argument",
8d08fdba
MS
11907 decl);
11908 else
8251199e
JM
11909 cp_error
11910 ("postfix `%D' must take `int' as its second argument",
11911 decl);
8d08fdba
MS
11912 }
11913 }
11914 else
11915 {
11916 if (methodp)
8251199e 11917 cp_error ("`%D' must take either zero or one argument", decl);
8d08fdba 11918 else
8251199e 11919 cp_error ("`%D' must take either one or two arguments", decl);
8d08fdba 11920 }
824b9a4c
MS
11921
11922 /* More Effective C++ rule 6. */
eb448459 11923 if (warn_ecpp
824b9a4c
MS
11924 && (name == ansi_opname[(int) POSTINCREMENT_EXPR]
11925 || name == ansi_opname[(int) POSTDECREMENT_EXPR]))
11926 {
11927 tree arg = TREE_VALUE (argtypes);
11928 tree ret = TREE_TYPE (TREE_TYPE (decl));
11929 if (methodp || TREE_CODE (arg) == REFERENCE_TYPE)
11930 arg = TREE_TYPE (arg);
11931 arg = TYPE_MAIN_VARIANT (arg);
11932 if (list_length (argtypes) == 2)
11933 {
11934 if (TREE_CODE (ret) != REFERENCE_TYPE
3bfdc719
MM
11935 || !same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (ret)),
11936 arg))
8251199e 11937 cp_warning ("prefix `%D' should return `%T'", decl,
824b9a4c
MS
11938 build_reference_type (arg));
11939 }
11940 else
11941 {
3bfdc719 11942 if (!same_type_p (TYPE_MAIN_VARIANT (ret), arg))
8251199e 11943 cp_warning ("postfix `%D' should return `%T'", decl, arg);
824b9a4c
MS
11944 }
11945 }
8d08fdba
MS
11946 }
11947 else if (unary_op_p (name))
11948 {
11949 if (list_length (argtypes) != 2)
11950 {
11951 if (methodp)
8251199e 11952 cp_error ("`%D' must take `void'", decl);
8d08fdba 11953 else
8251199e 11954 cp_error ("`%D' must take exactly one argument", decl);
8d08fdba
MS
11955 }
11956 }
11957 else /* if (binary_op_p (name)) */
11958 {
11959 if (list_length (argtypes) != 3)
11960 {
11961 if (methodp)
8251199e 11962 cp_error ("`%D' must take exactly one argument", decl);
8d08fdba 11963 else
8251199e 11964 cp_error ("`%D' must take exactly two arguments", decl);
8d08fdba 11965 }
824b9a4c
MS
11966
11967 /* More Effective C++ rule 7. */
eb448459 11968 if (warn_ecpp
824b9a4c
MS
11969 && (name == ansi_opname [TRUTH_ANDIF_EXPR]
11970 || name == ansi_opname [TRUTH_ORIF_EXPR]
11971 || name == ansi_opname [COMPOUND_EXPR]))
8251199e 11972 cp_warning ("user-defined `%D' always evaluates both arguments",
824b9a4c
MS
11973 decl);
11974 }
11975
11976 /* Effective C++ rule 23. */
eb448459 11977 if (warn_ecpp
824b9a4c
MS
11978 && list_length (argtypes) == 3
11979 && (name == ansi_opname [PLUS_EXPR]
11980 || name == ansi_opname [MINUS_EXPR]
11981 || name == ansi_opname [TRUNC_DIV_EXPR]
11982 || name == ansi_opname [MULT_EXPR])
11983 && TREE_CODE (TREE_TYPE (TREE_TYPE (decl))) == REFERENCE_TYPE)
8251199e 11984 cp_warning ("`%D' should return by value", decl);
8d08fdba
MS
11985
11986 /* 13.4.0.8 */
11987 if (argtypes)
11988 for (; argtypes != void_list_node ; argtypes = TREE_CHAIN (argtypes))
11989 if (TREE_PURPOSE (argtypes))
11990 {
11991 TREE_PURPOSE (argtypes) = NULL_TREE;
11992 if (name == ansi_opname[(int) POSTINCREMENT_EXPR]
11993 || name == ansi_opname[(int) POSTDECREMENT_EXPR])
11994 {
11995 if (pedantic)
8251199e 11996 cp_pedwarn ("`%D' cannot have default arguments", decl);
8d08fdba
MS
11997 }
11998 else
8251199e 11999 cp_error ("`%D' cannot have default arguments", decl);
8d08fdba
MS
12000 }
12001 }
12002}
12003\f
d8e178a0 12004static const char *
094fe153
JM
12005tag_name (code)
12006 enum tag_types code;
12007{
12008 switch (code)
12009 {
12010 case record_type:
12011 return "struct";
12012 case class_type:
12013 return "class";
12014 case union_type:
12015 return "union ";
12016 case enum_type:
12017 return "enum";
094fe153
JM
12018 default:
12019 my_friendly_abort (981122);
12020 }
12021}
12022
8d08fdba
MS
12023/* Get the struct, enum or union (CODE says which) with tag NAME.
12024 Define the tag as a forward-reference if it is not defined.
12025
12026 C++: If a class derivation is given, process it here, and report
12027 an error if multiple derivation declarations are not identical.
12028
12029 If this is a definition, come in through xref_tag and only look in
12030 the current frame for the name (since C++ allows new names in any
12031 scope.) */
12032
8d08fdba 12033tree
ca107ded 12034xref_tag (code_type_node, name, globalize)
8d08fdba 12035 tree code_type_node;
ca107ded 12036 tree name;
8d08fdba
MS
12037 int globalize;
12038{
12039 enum tag_types tag_code;
12040 enum tree_code code;
12041 int temp = 0;
8d08fdba 12042 register tree ref, t;
8f032717 12043 struct binding_level *b = current_binding_level;
a80e4195 12044 int got_type = 0;
dc8263bc 12045 tree attributes = NULL_TREE;
25aab5d0 12046 tree context = NULL_TREE;
dc8263bc
JM
12047
12048 /* If we are called from the parser, code_type_node will sometimes be a
12049 TREE_LIST. This indicates that the user wrote
12050 "class __attribute__ ((foo)) bar". Extract the attributes so we can
12051 use them later. */
12052 if (TREE_CODE (code_type_node) == TREE_LIST)
12053 {
12054 attributes = TREE_PURPOSE (code_type_node);
12055 code_type_node = TREE_VALUE (code_type_node);
12056 }
8d08fdba
MS
12057
12058 tag_code = (enum tag_types) TREE_INT_CST_LOW (code_type_node);
12059 switch (tag_code)
12060 {
12061 case record_type:
12062 case class_type:
8d08fdba 12063 code = RECORD_TYPE;
8d08fdba
MS
12064 break;
12065 case union_type:
12066 code = UNION_TYPE;
8d08fdba
MS
12067 break;
12068 case enum_type:
12069 code = ENUMERAL_TYPE;
12070 break;
12071 default:
12072 my_friendly_abort (18);
12073 }
12074
12075 /* If a cross reference is requested, look up the type
12076 already defined for this tag and return it. */
be99da77
MS
12077 if (TREE_CODE_CLASS (TREE_CODE (name)) == 't')
12078 {
12079 t = name;
a80e4195
MS
12080 name = TYPE_IDENTIFIER (t);
12081 got_type = 1;
be99da77
MS
12082 }
12083 else
12084 t = IDENTIFIER_TYPE_VALUE (name);
61a127b3 12085
73b0fce8
KL
12086 if (t && TREE_CODE (t) != code && TREE_CODE (t) != TEMPLATE_TYPE_PARM
12087 && TREE_CODE (t) != TEMPLATE_TEMPLATE_PARM)
a0a33927
MS
12088 t = NULL_TREE;
12089
8ccc31eb 12090 if (! globalize)
8d08fdba 12091 {
f3400fe2
JM
12092 /* If we know we are defining this tag, only look it up in
12093 this scope and don't try to find it as a type. */
12094 ref = lookup_tag (code, name, b, 1);
8d08fdba
MS
12095 }
12096 else
12097 {
25aab5d0 12098 if (t)
36a117a5 12099 {
25aab5d0
MM
12100 /* [dcl.type.elab] If the identifier resolves to a
12101 typedef-name or a template type-parameter, the
12102 elaborated-type-specifier is ill-formed. */
12103 if (t != TYPE_MAIN_VARIANT (t)
12104 || (CLASS_TYPE_P (t) && TYPE_WAS_ANONYMOUS (t)))
12105 cp_pedwarn ("using typedef-name `%D' after `%s'",
12106 TYPE_NAME (t), tag_name (tag_code));
12107 else if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
12108 cp_error ("using template type parameter `%T' after `%s'",
12109 t, tag_name (tag_code));
12110
12111 ref = t;
12112 }
12113 else
12114 ref = lookup_tag (code, name, b, 0);
36a117a5 12115
25aab5d0
MM
12116 if (! ref)
12117 {
12118 /* Try finding it as a type declaration. If that wins,
12119 use it. */
12120 ref = lookup_name (name, 1);
12121
12122 if (ref != NULL_TREE
12123 && processing_template_decl
12124 && DECL_CLASS_TEMPLATE_P (ref)
12125 && template_class_depth (current_class_type) == 0)
12126 /* Since GLOBALIZE is true, we're declaring a global
6757edfe 12127 template, so we want this type. */
25aab5d0 12128 ref = DECL_RESULT (ref);
6757edfe 12129
25aab5d0
MM
12130 if (ref && TREE_CODE (ref) == TYPE_DECL
12131 && TREE_CODE (TREE_TYPE (ref)) == code)
12132 ref = TREE_TYPE (ref);
12133 else
12134 ref = NULL_TREE;
12135 }
12136
12137 if (ref && current_class_type
12138 && template_class_depth (current_class_type)
12139 && PROCESSING_REAL_TEMPLATE_DECL_P ())
12140 {
12141 /* Since GLOBALIZE is non-zero, we are not looking at a
12142 definition of this tag. Since, in addition, we are currently
12143 processing a (member) template declaration of a template
12144 class, we must be very careful; consider:
12145
12146 template <class X>
12147 struct S1
12148
12149 template <class U>
12150 struct S2
12151 { template <class V>
12152 friend struct S1; };
12153
12154 Here, the S2::S1 declaration should not be confused with the
12155 outer declaration. In particular, the inner version should
12156 have a template parameter of level 2, not level 1. This
12157 would be particularly important if the member declaration
12158 were instead:
12159
12160 template <class V = U> friend struct S1;
12161
12162 say, when we should tsubst into `U' when instantiating
12163 S2. On the other hand, when presented with:
12164
12165 template <class T>
12166 struct S1 {
12167 template <class U>
12168 struct S2 {};
12169 template <class U>
12170 friend struct S2;
12171 };
12172
12173 we must find the inner binding eventually. We
12174 accomplish this by making sure that the new type we
12175 create to represent this declaration has the right
12176 TYPE_CONTEXT. */
12177 context = TYPE_CONTEXT (ref);
12178 ref = NULL_TREE;
8d08fdba
MS
12179 }
12180 }
12181
12182 push_obstacks_nochange ();
12183
12184 if (! ref)
12185 {
12186 /* If no such tag is yet defined, create a forward-reference node
12187 and record it as the "definition".
12188 When a real declaration of this type is found,
12189 the forward-reference will be altered into a real type. */
12190
12191 /* In C++, since these migrate into the global scope, we must
12192 build them on the permanent obstack. */
12193
12194 temp = allocation_temporary_p ();
12195 if (temp)
12196 end_temporary_allocation ();
12197
12198 if (code == ENUMERAL_TYPE)
12199 {
8251199e 12200 cp_error ("use of enum `%#D' without previous declaration", name);
fc378698 12201
8d08fdba
MS
12202 ref = make_node (ENUMERAL_TYPE);
12203
12204 /* Give the type a default layout like unsigned int
12205 to avoid crashing if it does not get defined. */
12206 TYPE_MODE (ref) = TYPE_MODE (unsigned_type_node);
12207 TYPE_ALIGN (ref) = TYPE_ALIGN (unsigned_type_node);
12208 TREE_UNSIGNED (ref) = 1;
12209 TYPE_PRECISION (ref) = TYPE_PRECISION (unsigned_type_node);
12210 TYPE_MIN_VALUE (ref) = TYPE_MIN_VALUE (unsigned_type_node);
12211 TYPE_MAX_VALUE (ref) = TYPE_MAX_VALUE (unsigned_type_node);
12212
12213 /* Enable us to recognize when a type is created in class context.
12214 To do nested classes correctly, this should probably be cleared
12215 out when we leave this classes scope. Currently this in only
12216 done in `start_enum'. */
12217
12218 pushtag (name, ref, globalize);
8d08fdba 12219 }
8d08fdba
MS
12220 else
12221 {
8d08fdba 12222 struct binding_level *old_b = class_binding_level;
8d08fdba
MS
12223
12224 ref = make_lang_type (code);
25aab5d0 12225 TYPE_CONTEXT (ref) = context;
8d08fdba 12226
8d08fdba
MS
12227#ifdef NONNESTED_CLASSES
12228 /* Class types don't nest the way enums do. */
12229 class_binding_level = (struct binding_level *)0;
12230#endif
12231 pushtag (name, ref, globalize);
12232 class_binding_level = old_b;
8d08fdba
MS
12233 }
12234 }
12235 else
12236 {
8d08fdba 12237 /* If it no longer looks like a nested type, make sure it's
30394414
JM
12238 in global scope.
12239 If it is not an IDENTIFIER, this is not a declaration */
2c73f9f5 12240 if (b->namespace_p && !class_binding_level
30394414
JM
12241 && TREE_CODE (name) == IDENTIFIER_NODE)
12242 {
2c73f9f5
ML
12243 if (IDENTIFIER_NAMESPACE_VALUE (name) == NULL_TREE)
12244 SET_IDENTIFIER_NAMESPACE_VALUE (name, TYPE_NAME (ref));
30394414 12245 }
7fe6899f
MM
12246
12247 if (!globalize && processing_template_decl && IS_AGGR_TYPE (ref))
6757edfe 12248 redeclare_class_template (ref, current_template_parms);
8d08fdba
MS
12249 }
12250
8d08fdba
MS
12251 /* Until the type is defined, tentatively accept whatever
12252 structure tag the user hands us. */
12253 if (TYPE_SIZE (ref) == NULL_TREE
12254 && ref != current_class_type
12255 /* Have to check this, in case we have contradictory tag info. */
12256 && IS_AGGR_TYPE_CODE (TREE_CODE (ref)))
12257 {
12258 if (tag_code == class_type)
12259 CLASSTYPE_DECLARED_CLASS (ref) = 1;
6eabb241 12260 else if (tag_code == record_type)
8d08fdba
MS
12261 CLASSTYPE_DECLARED_CLASS (ref) = 0;
12262 }
12263
12264 pop_obstacks ();
12265
dc8263bc
JM
12266 TREE_TYPE (ref) = attributes;
12267
8d08fdba
MS
12268 return ref;
12269}
8ccc31eb 12270
fc378698
MS
12271tree
12272xref_tag_from_type (old, id, globalize)
12273 tree old, id;
12274 int globalize;
12275{
12276 tree code_type_node;
12277
12278 if (TREE_CODE (old) == RECORD_TYPE)
12279 code_type_node = (CLASSTYPE_DECLARED_CLASS (old)
12280 ? class_type_node : record_type_node);
12281 else
12282 code_type_node = union_type_node;
12283
12284 if (id == NULL_TREE)
12285 id = TYPE_IDENTIFIER (old);
12286
ca107ded 12287 return xref_tag (code_type_node, id, globalize);
fc378698
MS
12288}
12289
3fd71a52
MM
12290/* REF is a type (named NAME), for which we have just seen some
12291 baseclasses. BINFO is a list of those baseclasses; the
12292 TREE_PURPOSE is an access_* node, and the TREE_VALUE is the type of
12293 the base-class. CODE_TYPE_NODE indicates whether REF is a class,
12294 struct, or union. */
12295
8ccc31eb
MS
12296void
12297xref_basetypes (code_type_node, name, ref, binfo)
12298 tree code_type_node;
12299 tree name, ref;
12300 tree binfo;
12301{
12302 /* In the declaration `A : X, Y, ... Z' we mark all the types
12303 (A, X, Y, ..., Z) so we can check for duplicates. */
12304 tree binfos;
d6479fe7
MM
12305 tree base;
12306
8ccc31eb
MS
12307 int i, len;
12308 enum tag_types tag_code = (enum tag_types) TREE_INT_CST_LOW (code_type_node);
12309
12310 if (tag_code == union_type)
12311 {
8251199e 12312 cp_error ("derived union `%T' invalid", ref);
8ccc31eb
MS
12313 return;
12314 }
12315
12316 len = list_length (binfo);
12317 push_obstacks (TYPE_OBSTACK (ref), TYPE_OBSTACK (ref));
12318
d6479fe7
MM
12319 /* First, make sure that any templates in base-classes are
12320 instantiated. This ensures that if we call ourselves recursively
12321 we do not get confused about which classes are marked and which
12322 are not. */
12323 for (base = binfo; base; base = TREE_CHAIN (base))
12324 complete_type (TREE_VALUE (base));
12325
8ccc31eb
MS
12326 SET_CLASSTYPE_MARKED (ref);
12327 BINFO_BASETYPES (TYPE_BINFO (ref)) = binfos = make_tree_vec (len);
12328
12329 for (i = 0; binfo; binfo = TREE_CHAIN (binfo))
12330 {
12331 /* The base of a derived struct is public by default. */
12332 int via_public
be99da77
MS
12333 = (TREE_PURPOSE (binfo) == access_public_node
12334 || TREE_PURPOSE (binfo) == access_public_virtual_node
8ccc31eb 12335 || (tag_code != class_type
be99da77
MS
12336 && (TREE_PURPOSE (binfo) == access_default_node
12337 || TREE_PURPOSE (binfo) == access_default_virtual_node)));
d8b55a76
JM
12338 int via_protected
12339 = (TREE_PURPOSE (binfo) == access_protected_node
12340 || TREE_PURPOSE (binfo) == access_protected_virtual_node);
8ccc31eb 12341 int via_virtual
be99da77 12342 = (TREE_PURPOSE (binfo) == access_private_virtual_node
d8b55a76 12343 || TREE_PURPOSE (binfo) == access_protected_virtual_node
be99da77
MS
12344 || TREE_PURPOSE (binfo) == access_public_virtual_node
12345 || TREE_PURPOSE (binfo) == access_default_virtual_node);
12346 tree basetype = TREE_VALUE (binfo);
8ccc31eb
MS
12347 tree base_binfo;
12348
8ccc31eb
MS
12349 if (basetype && TREE_CODE (basetype) == TYPE_DECL)
12350 basetype = TREE_TYPE (basetype);
5566b478
MS
12351 if (!basetype
12352 || (TREE_CODE (basetype) != RECORD_TYPE
a80e4195 12353 && TREE_CODE (basetype) != TYPENAME_TYPE
73b0fce8
KL
12354 && TREE_CODE (basetype) != TEMPLATE_TYPE_PARM
12355 && TREE_CODE (basetype) != TEMPLATE_TEMPLATE_PARM))
8ccc31eb 12356 {
8251199e 12357 cp_error ("base type `%T' fails to be a struct or class type",
8ccc31eb
MS
12358 TREE_VALUE (binfo));
12359 continue;
12360 }
2b9dc906 12361
11b5139c 12362 GNU_xref_hier (name, basetype, via_public, via_virtual, 0);
2b9dc906 12363
8adf5b5e
JM
12364 /* This code replaces similar code in layout_basetypes.
12365 We put the complete_type first for implicit `typename'. */
d6479fe7 12366 if (TYPE_SIZE (basetype) == NULL_TREE
2b9dc906 12367 && ! (current_template_parms && uses_template_parms (basetype)))
8ccc31eb 12368 {
8251199e 12369 cp_error ("base class `%T' has incomplete type", basetype);
8ccc31eb
MS
12370 continue;
12371 }
8ccc31eb
MS
12372 else
12373 {
12374 if (CLASSTYPE_MARKED (basetype))
12375 {
12376 if (basetype == ref)
8251199e 12377 cp_error ("recursive type `%T' undefined", basetype);
8ccc31eb 12378 else
8251199e 12379 cp_error ("duplicate base type `%T' invalid", basetype);
8ccc31eb
MS
12380 continue;
12381 }
12382
eff71ab0
PB
12383 if (TYPE_FOR_JAVA (basetype)
12384 && current_lang_stack == current_lang_base)
12385 TYPE_FOR_JAVA (ref) = 1;
12386
8ccc31eb
MS
12387 /* Note that the BINFO records which describe individual
12388 inheritances are *not* shared in the lattice! They
12389 cannot be shared because a given baseclass may be
12390 inherited with different `accessibility' by different
12391 derived classes. (Each BINFO record describing an
12392 individual inheritance contains flags which say what
12393 the `accessibility' of that particular inheritance is.) */
12394
7ddedda4
MM
12395 base_binfo
12396 = make_binfo (integer_zero_node, basetype,
12397 CLASS_TYPE_P (basetype)
12398 ? TYPE_BINFO_VTABLE (basetype) : NULL_TREE,
12399 CLASS_TYPE_P (basetype)
12400 ? TYPE_BINFO_VIRTUALS (basetype) : NULL_TREE);
8ccc31eb
MS
12401
12402 TREE_VEC_ELT (binfos, i) = base_binfo;
12403 TREE_VIA_PUBLIC (base_binfo) = via_public;
12404 TREE_VIA_PROTECTED (base_binfo) = via_protected;
12405 TREE_VIA_VIRTUAL (base_binfo) = via_virtual;
12406 BINFO_INHERITANCE_CHAIN (base_binfo) = TYPE_BINFO (ref);
12407
dfbcd65a
JM
12408 /* We need to unshare the binfos now so that lookups during class
12409 definition work. */
12410 unshare_base_binfos (base_binfo);
12411
8ccc31eb 12412 SET_CLASSTYPE_MARKED (basetype);
9e9ff709 12413
8ccc31eb
MS
12414 /* We are free to modify these bits because they are meaningless
12415 at top level, and BASETYPE is a top-level type. */
12416 if (via_virtual || TYPE_USES_VIRTUAL_BASECLASSES (basetype))
12417 {
12418 TYPE_USES_VIRTUAL_BASECLASSES (ref) = 1;
12419 TYPE_USES_COMPLEX_INHERITANCE (ref) = 1;
12420 }
12421
7ddedda4
MM
12422 if (CLASS_TYPE_P (basetype))
12423 {
12424 TYPE_GETS_NEW (ref) |= TYPE_GETS_NEW (basetype);
12425 TYPE_GETS_DELETE (ref) |= TYPE_GETS_DELETE (basetype);
12426 }
12427
8ccc31eb
MS
12428 i += 1;
12429 }
12430 }
12431 if (i)
12432 TREE_VEC_LENGTH (binfos) = i;
12433 else
12434 BINFO_BASETYPES (TYPE_BINFO (ref)) = NULL_TREE;
12435
12436 if (i > 1)
12437 TYPE_USES_MULTIPLE_INHERITANCE (ref) = 1;
12438 else if (i == 1)
7ddedda4
MM
12439 {
12440 tree basetype = BINFO_TYPE (TREE_VEC_ELT (binfos, 0));
12441
12442 if (CLASS_TYPE_P (basetype))
12443 TYPE_USES_MULTIPLE_INHERITANCE (ref)
12444 = TYPE_USES_MULTIPLE_INHERITANCE (basetype);
12445 }
12446
8ccc31eb
MS
12447 if (TYPE_USES_MULTIPLE_INHERITANCE (ref))
12448 TYPE_USES_COMPLEX_INHERITANCE (ref) = 1;
12449
12450 /* Unmark all the types. */
12451 while (--i >= 0)
12452 CLEAR_CLASSTYPE_MARKED (BINFO_TYPE (TREE_VEC_ELT (binfos, i)));
12453 CLEAR_CLASSTYPE_MARKED (ref);
12454
70c532b5
MM
12455 /* Now that we know all the base-classes, set up the list of virtual
12456 bases. */
12457 CLASSTYPE_VBASECLASSES (ref) = get_vbase_types (ref);
12458
8ccc31eb
MS
12459 pop_obstacks ();
12460}
12461
8d08fdba 12462\f
8d08fdba
MS
12463/* Begin compiling the definition of an enumeration type.
12464 NAME is its name (or null if anonymous).
12465 Returns the type object, as yet incomplete.
12466 Also records info about it so that build_enumerator
12467 may be used to declare the individual values as they are read. */
12468
12469tree
12470start_enum (name)
12471 tree name;
12472{
12473 register tree enumtype = NULL_TREE;
8f032717 12474 struct binding_level *b = current_binding_level;
8d08fdba 12475
8c1e8edc 12476 /* We are wasting space here and putting these on the permanent_obstack so
6e687e5e 12477 that typeid(local enum) will work correctly. */
cbf882af 12478 push_obstacks (&permanent_obstack, &permanent_obstack);
b87692e5 12479
8d08fdba
MS
12480 /* If this is the real definition for a previous forward reference,
12481 fill in the contents in the same object that used to be the
12482 forward reference. */
12483
12484 if (name != NULL_TREE)
12485 enumtype = lookup_tag (ENUMERAL_TYPE, name, b, 1);
12486
12487 if (enumtype != NULL_TREE && TREE_CODE (enumtype) == ENUMERAL_TYPE)
30ff8252
NS
12488 {
12489 cp_error ("multiple definition of `%#T'", enumtype);
12490 cp_error_at ("previous definition here", enumtype);
12491 }
8d08fdba
MS
12492 else
12493 {
12494 enumtype = make_node (ENUMERAL_TYPE);
12495 pushtag (name, enumtype, 0);
12496 }
12497
12498 if (current_class_type)
12499 TREE_ADDRESSABLE (b->tags) = 1;
c91a56d2 12500
e92cc029
MS
12501 /* We don't copy this value because build_enumerator needs to do it. */
12502 enum_next_value = integer_zero_node;
7177d104 12503 enum_overflow = 0;
8d08fdba
MS
12504
12505 GNU_xref_decl (current_function_decl, enumtype);
12506 return enumtype;
12507}
12508
12509/* After processing and defining all the values of an enumeration type,
12510 install their decls in the enumeration type and finish it off.
12511 ENUMTYPE is the type object and VALUES a list of name-value pairs.
12512 Returns ENUMTYPE. */
12513
12514tree
dbfe2124
MM
12515finish_enum (enumtype)
12516 tree enumtype;
8d08fdba 12517{
a703fb38 12518 register tree minnode = NULL_TREE, maxnode = NULL_TREE;
8d08fdba
MS
12519 /* Calculate the maximum value of any enumerator in this type. */
12520
dbfe2124 12521 tree values = TYPE_VALUES (enumtype);
8d08fdba
MS
12522 if (values)
12523 {
ed44da02 12524 tree pair;
5566b478 12525
ed44da02 12526 for (pair = values; pair; pair = TREE_CHAIN (pair))
8d08fdba 12527 {
ed44da02
MM
12528 tree decl;
12529 tree value;
12530
12531 /* The TREE_VALUE is a CONST_DECL for this enumeration
12532 constant. */
12533 decl = TREE_VALUE (pair);
12534
ed44da02
MM
12535 /* The DECL_INITIAL will be NULL if we are processing a
12536 template declaration and this enumeration constant had no
12537 explicit initializer. */
12538 value = DECL_INITIAL (decl);
079e1098 12539 if (value && !processing_template_decl)
5566b478 12540 {
079e1098
MM
12541 /* Set the TREE_TYPE for the VALUE as well. That's so
12542 that when we call decl_constant_value we get an
12543 entity of the right type (but with the constant
12544 value). Since we shouldn't ever call
12545 decl_constant_value on a template type, there's no
12546 reason to do that when processing_template_decl.
12547 And, if the expression is something like a
12548 TEMPLATE_PARM_INDEX or a CAST_EXPR doing so will
12549 wreak havoc on the intended type of the expression.
12550
12551 Of course, there's also no point in trying to compute
12552 minimum or maximum values if we're in a template. */
5566b478 12553 TREE_TYPE (value) = enumtype;
ed44da02 12554
079e1098
MM
12555 if (!minnode)
12556 minnode = maxnode = value;
12557 else if (tree_int_cst_lt (maxnode, value))
12558 maxnode = value;
12559 else if (tree_int_cst_lt (value, minnode))
12560 minnode = value;
5566b478 12561 }
ed44da02 12562
72f2bd78
MM
12563 if (processing_template_decl)
12564 /* If this is just a template, leave the CONST_DECL
12565 alone. That way tsubst_copy will find CONST_DECLs for
12566 CONST_DECLs, and not INTEGER_CSTs. */
12567 ;
12568 else
12569 /* In the list we're building up, we want the enumeration
12570 values, not the CONST_DECLs. */
12571 TREE_VALUE (pair) = value;
8d08fdba
MS
12572 }
12573 }
f376e137
MS
12574 else
12575 maxnode = minnode = integer_zero_node;
8d08fdba 12576
de22184b 12577 TYPE_VALUES (enumtype) = nreverse (values);
2986ae00 12578
5156628f 12579 if (processing_template_decl)
b87692e5 12580 {
9360ac70
MM
12581 tree scope = current_scope ();
12582 if (scope && TREE_CODE (scope) == FUNCTION_DECL)
cbf882af 12583 add_tree (build_min (TAG_DEFN, enumtype));
b87692e5 12584 }
cbf882af
MM
12585 else
12586 {
12587 int unsignedp = tree_int_cst_sgn (minnode) >= 0;
12588 int lowprec = min_precision (minnode, unsignedp);
12589 int highprec = min_precision (maxnode, unsignedp);
12590 int precision = MAX (lowprec, highprec);
12591 tree tem;
5566b478 12592
cbf882af 12593 TYPE_SIZE (enumtype) = NULL_TREE;
8d08fdba 12594
cbf882af 12595 /* Set TYPE_MIN_VALUE and TYPE_MAX_VALUE according to `precision'. */
8ccc31eb 12596
cbf882af
MM
12597 TYPE_PRECISION (enumtype) = precision;
12598 if (unsignedp)
12599 fixup_unsigned_type (enumtype);
12600 else
12601 fixup_signed_type (enumtype);
8ccc31eb 12602
cbf882af
MM
12603 if (flag_short_enums || (precision > TYPE_PRECISION (integer_type_node)))
12604 /* Use the width of the narrowest normal C type which is wide
12605 enough. */
12606 TYPE_PRECISION (enumtype) = TYPE_PRECISION (type_for_size
12607 (precision, 1));
12608 else
12609 TYPE_PRECISION (enumtype) = TYPE_PRECISION (integer_type_node);
8d08fdba 12610
cbf882af
MM
12611 TYPE_SIZE (enumtype) = 0;
12612 layout_type (enumtype);
f376e137 12613
cbf882af
MM
12614 /* Fix up all variant types of this enum type. */
12615 for (tem = TYPE_MAIN_VARIANT (enumtype); tem;
12616 tem = TYPE_NEXT_VARIANT (tem))
12617 {
12618 TYPE_VALUES (tem) = TYPE_VALUES (enumtype);
12619 TYPE_MIN_VALUE (tem) = TYPE_MIN_VALUE (enumtype);
12620 TYPE_MAX_VALUE (tem) = TYPE_MAX_VALUE (enumtype);
12621 TYPE_SIZE (tem) = TYPE_SIZE (enumtype);
12622 TYPE_SIZE_UNIT (tem) = TYPE_SIZE_UNIT (enumtype);
12623 TYPE_MODE (tem) = TYPE_MODE (enumtype);
12624 TYPE_PRECISION (tem) = TYPE_PRECISION (enumtype);
12625 TYPE_ALIGN (tem) = TYPE_ALIGN (enumtype);
12626 TREE_UNSIGNED (tem) = TREE_UNSIGNED (enumtype);
12627 }
8d08fdba 12628
cbf882af
MM
12629 /* Finish debugging output for this type. */
12630 rest_of_type_compilation (enumtype, namespace_bindings_p ());
12631 }
12632
12633 /* In start_enum we pushed obstacks. Here, we must pop them. */
12634 pop_obstacks ();
8d08fdba
MS
12635
12636 return enumtype;
12637}
12638
079e1098
MM
12639/* Build and install a CONST_DECL for an enumeration constant of the
12640 enumeration type TYPE whose NAME and VALUE (if any) are provided.
8d08fdba
MS
12641 Assignment of sequential values by default is handled here. */
12642
12643tree
079e1098
MM
12644build_enumerator (name, value, type)
12645 tree name;
12646 tree value;
12647 tree type;
8d08fdba
MS
12648{
12649 tree decl, result;
e8bd800e 12650 tree context;
8d08fdba
MS
12651
12652 /* Remove no-op casts from the value. */
12653 if (value)
12654 STRIP_TYPE_NOPS (value);
12655
5156628f 12656 if (! processing_template_decl)
5566b478
MS
12657 {
12658 /* Validate and default VALUE. */
12659 if (value != NULL_TREE)
12660 {
12661 if (TREE_READONLY_DECL_P (value))
e92cc029 12662 value = decl_constant_value (value);
5566b478
MS
12663
12664 if (TREE_CODE (value) == INTEGER_CST)
12665 {
12666 value = default_conversion (value);
12667 constant_expression_warning (value);
12668 }
12669 else
12670 {
8251199e 12671 cp_error ("enumerator value for `%D' not integer constant", name);
5566b478
MS
12672 value = NULL_TREE;
12673 }
12674 }
12675
5566b478 12676 /* Default based on previous value. */
5156628f 12677 if (value == NULL_TREE && ! processing_template_decl)
5566b478
MS
12678 {
12679 value = enum_next_value;
12680 if (enum_overflow)
8251199e 12681 cp_error ("overflow in enumeration values at `%D'", name);
5566b478
MS
12682 }
12683
12684 /* Remove no-op casts from the value. */
12685 if (value)
12686 STRIP_TYPE_NOPS (value);
013bc8af
MS
12687#if 0
12688 /* To fix MAX_VAL enum consts. (bkoz) */
e92cc029 12689 TREE_TYPE (value) = integer_type_node;
013bc8af 12690#endif
5566b478 12691 }
8d08fdba 12692
079e1098
MM
12693 /* We always have to copy here; not all INTEGER_CSTs are unshared.
12694 Even in other cases, we will later (in finish_enum) be setting the
12695 type of VALUE. */
ed44da02
MM
12696 if (value != NULL_TREE)
12697 value = copy_node (value);
12698
8d08fdba 12699 /* C++ associates enums with global, function, or class declarations. */
e8bd800e
MM
12700
12701 context = current_scope ();
12702 if (context && context == current_class_type)
12703 /* This enum declaration is local to the class. */
4ce3d537 12704 decl = build_lang_decl (CONST_DECL, name, type);
e8bd800e
MM
12705 else
12706 /* It's a global enum, or it's local to a function. (Note local to
12707 a function could mean local to a class method. */
079e1098 12708 decl = build_decl (CONST_DECL, name, type);
e8bd800e
MM
12709
12710 DECL_CONTEXT (decl) = FROB_CONTEXT (context);
12711 DECL_INITIAL (decl) = value;
12712 TREE_READONLY (decl) = 1;
12713
12714 if (context && context == current_class_type)
8f032717
MM
12715 /* In something like `struct S { enum E { i = 7 }; };' we put `i'
12716 on the TYPE_FIELDS list for `S'. (That's so that you can say
12717 things like `S::i' later.) */
12718 finish_member_declaration (decl);
e8bd800e
MM
12719 else
12720 {
12721 pushdecl (decl);
12722 GNU_xref_decl (current_function_decl, decl);
12723 }
8d08fdba 12724
5156628f 12725 if (! processing_template_decl)
5566b478
MS
12726 {
12727 /* Set basis for default for next value. */
12728 enum_next_value = build_binary_op_nodefault (PLUS_EXPR, value,
12729 integer_one_node, PLUS_EXPR);
12730 enum_overflow = tree_int_cst_lt (enum_next_value, value);
5566b478 12731 }
8d08fdba 12732
8d2733ca 12733 result = saveable_tree_cons (name, decl, NULL_TREE);
8d08fdba
MS
12734 return result;
12735}
12736
8d08fdba 12737\f
5566b478
MS
12738static int function_depth;
12739
8d08fdba
MS
12740/* Create the FUNCTION_DECL for a function definition.
12741 DECLSPECS and DECLARATOR are the parts of the declaration;
12742 they describe the function's name and the type it returns,
12743 but twisted together in a fashion that parallels the syntax of C.
12744
f181d4ae
MM
12745 If PRE_PARSED_P is non-zero then DECLARATOR is really the DECL for
12746 the function we are about to process; DECLSPECS are ignored. For
12747 example, we set PRE_PARSED_P when processing the definition of
12748 inline function that was defined in-class; the definition is
12749 actually processed when the class is complete. In this case,
12750 PRE_PARSED_P is 2. We also set PRE_PARSED_P when instanting the
12751 body of a template function, and when constructing thunk functions
12752 and such; in these cases PRE_PARSED_P is 1.
12753
8d08fdba
MS
12754 This function creates a binding context for the function body
12755 as well as setting up the FUNCTION_DECL in current_function_decl.
12756
12757 Returns 1 on success. If the DECLARATOR is not suitable for a function
12758 (it defines a datum instead), we return 0, which tells
12759 yyparse to report a parse error.
12760
12761 For C++, we must first check whether that datum makes any sense.
12762 For example, "class A local_a(1,2);" means that variable local_a
12763 is an aggregate of type A, which should have a constructor
12764 applied to it with the argument list [1, 2].
12765
12766 @@ There is currently no way to retrieve the storage
12767 @@ allocated to FUNCTION (or all of its parms) if we return
12768 @@ something we had previously. */
12769
12770int
c11b6f21
MS
12771start_function (declspecs, declarator, attrs, pre_parsed_p)
12772 tree declspecs, declarator, attrs;
8d08fdba
MS
12773 int pre_parsed_p;
12774{
5566b478 12775 tree decl1;
8d08fdba
MS
12776 tree ctype = NULL_TREE;
12777 tree fntype;
12778 tree restype;
12779 extern int have_extern_spec;
12780 extern int used_extern_spec;
12781 int doing_friend = 0;
12782
8d08fdba 12783 /* Sanity check. */
a1774733 12784 my_friendly_assert (TREE_CODE (TREE_VALUE (void_list_node)) == VOID_TYPE, 160);
8d08fdba
MS
12785 my_friendly_assert (TREE_CHAIN (void_list_node) == NULL_TREE, 161);
12786
e92cc029 12787 /* Assume, until we see it does. */
8d08fdba
MS
12788 current_function_returns_value = 0;
12789 current_function_returns_null = 0;
fd378c9d
JM
12790 named_labels = 0;
12791 shadowed_labels = 0;
8d08fdba
MS
12792 current_function_assigns_this = 0;
12793 current_function_just_assigned_this = 0;
12794 current_function_parms_stored = 0;
12795 original_result_rtx = NULL_RTX;
a9aedbc2 12796 base_init_expr = NULL_TREE;
79ff2c6c
MS
12797 current_base_init_list = NULL_TREE;
12798 current_member_init_list = NULL_TREE;
f30432d7 12799 ctor_label = dtor_label = NULL_TREE;
42976354 12800 static_labelno = 0;
0dde4175 12801 in_function_try_handler = 0;
8d08fdba
MS
12802
12803 clear_temp_name ();
12804
e92cc029 12805 /* This should only be done once on the top most decl. */
8d08fdba
MS
12806 if (have_extern_spec && !used_extern_spec)
12807 {
12808 declspecs = decl_tree_cons (NULL_TREE, get_identifier ("extern"), declspecs);
12809 used_extern_spec = 1;
12810 }
12811
12812 if (pre_parsed_p)
12813 {
12814 decl1 = declarator;
12815
67f7c391
JM
12816#if 0
12817 /* What was this testing for, exactly? */
8d08fdba
MS
12818 if (! DECL_ARGUMENTS (decl1)
12819 && !DECL_STATIC_FUNCTION_P (decl1)
67f7c391 12820 && !DECL_ARTIFICIAL (decl1)
6eb3bb27 12821 && DECL_CLASS_SCOPE_P (decl1)
d2e5ee5c
MS
12822 && TYPE_IDENTIFIER (DECL_CONTEXT (decl1))
12823 && IDENTIFIER_TEMPLATE (TYPE_IDENTIFIER (DECL_CONTEXT (decl1))))
8d08fdba 12824 {
30394414
JM
12825 tree binding = binding_for_name (DECL_NAME (decl1),
12826 current_namespace);
8251199e 12827 cp_error ("redeclaration of `%#D'", decl1);
8d08fdba 12828 if (IDENTIFIER_CLASS_VALUE (DECL_NAME (decl1)))
8251199e 12829 cp_error_at ("previous declaration here", IDENTIFIER_CLASS_VALUE (DECL_NAME (decl1)));
30394414 12830 else if (BINDING_VALUE (binding))
8251199e 12831 cp_error_at ("previous declaration here", BINDING_VALUE (binding));
8d08fdba 12832 }
67f7c391 12833#endif
8d08fdba 12834
8d08fdba
MS
12835 fntype = TREE_TYPE (decl1);
12836 if (TREE_CODE (fntype) == METHOD_TYPE)
12837 ctype = TYPE_METHOD_BASETYPE (fntype);
12838
12839 /* ANSI C++ June 5 1992 WP 11.4.5. A friend function defined in a
12840 class is in the (lexical) scope of the class in which it is
12841 defined. */
12842 if (!ctype && DECL_FRIEND_P (decl1))
12843 {
12844 ctype = DECL_CLASS_CONTEXT (decl1);
12845
12846 /* CTYPE could be null here if we're dealing with a template;
12847 for example, `inline friend float foo()' inside a template
12848 will have no CTYPE set. */
12849 if (ctype && TREE_CODE (ctype) != RECORD_TYPE)
12850 ctype = NULL_TREE;
12851 else
12852 doing_friend = 1;
12853 }
12854
5566b478
MS
12855 last_function_parms = DECL_ARGUMENTS (decl1);
12856 last_function_parm_tags = NULL_TREE;
8d08fdba
MS
12857 }
12858 else
12859 {
c11b6f21 12860 decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, 1, NULL_TREE);
8d08fdba
MS
12861 /* If the declarator is not suitable for a function definition,
12862 cause a syntax error. */
12863 if (decl1 == NULL_TREE || TREE_CODE (decl1) != FUNCTION_DECL) return 0;
12864
12865 fntype = TREE_TYPE (decl1);
12866
12867 restype = TREE_TYPE (fntype);
7ddedda4 12868 if (CLASS_TYPE_P (restype) && !CLASSTYPE_GOT_SEMICOLON (restype))
8d08fdba 12869 {
8251199e 12870 cp_error ("semicolon missing after declaration of `%#T'", restype);
e66d884e 12871 shadow_tag (build_expr_list (NULL_TREE, restype));
8d08fdba
MS
12872 CLASSTYPE_GOT_SEMICOLON (restype) = 1;
12873 if (TREE_CODE (fntype) == FUNCTION_TYPE)
12874 fntype = build_function_type (integer_type_node,
12875 TYPE_ARG_TYPES (fntype));
12876 else
12877 fntype = build_cplus_method_type (build_type_variant (TYPE_METHOD_BASETYPE (fntype), TREE_READONLY (decl1), TREE_SIDE_EFFECTS (decl1)),
12878 integer_type_node,
12879 TYPE_ARG_TYPES (fntype));
12880 TREE_TYPE (decl1) = fntype;
12881 }
12882
12883 if (TREE_CODE (fntype) == METHOD_TYPE)
12884 ctype = TYPE_METHOD_BASETYPE (fntype);
35680744 12885 else if (DECL_MAIN_P (decl1))
8d08fdba
MS
12886 {
12887 /* If this doesn't return integer_type, complain. */
12888 if (TREE_TYPE (TREE_TYPE (decl1)) != integer_type_node)
12889 {
a28e3c7f 12890 if (pedantic || warn_return_type)
8251199e 12891 pedwarn ("return type for `main' changed to `int'");
8d08fdba
MS
12892 TREE_TYPE (decl1) = fntype = default_function_type;
12893 }
8d08fdba
MS
12894 }
12895 }
12896
12897 /* Warn if function was previously implicitly declared
12898 (but not if we warned then). */
12899 if (! warn_implicit
12900 && IDENTIFIER_IMPLICIT_DECL (DECL_NAME (decl1)) != NULL_TREE)
8251199e 12901 cp_warning_at ("`%D' implicitly declared before its definition", IDENTIFIER_IMPLICIT_DECL (DECL_NAME (decl1)));
8d08fdba 12902
558475f0
MM
12903 if (!building_stmt_tree ())
12904 announce_function (decl1);
f181d4ae
MM
12905
12906 /* Set up current_class_type, and enter the scope of the class, if
12907 appropriate. */
12908 if (ctype)
12909 push_nested_class (ctype, 1);
12910 else if (DECL_STATIC_FUNCTION_P (decl1))
12911 push_nested_class (DECL_CONTEXT (decl1), 2);
12912
12913 /* Now that we have entered the scope of the class, we must restore
12914 the bindings for any template parameters surrounding DECL1, if it
12915 is an inline member template. (Order is important; consider the
12916 case where a template parameter has the same name as a field of
12917 the class.) It is not until after this point that
12918 PROCESSING_TEMPLATE_DECL is guaranteed to be set up correctly. */
12919 if (pre_parsed_p == 2)
12920 maybe_begin_member_template_processing (decl1);
12921
9188c363
MM
12922 /* Effective C++ rule 15. See also c_expand_return. */
12923 if (warn_ecpp
12924 && DECL_NAME (decl1) == ansi_opname[(int) MODIFY_EXPR]
12925 && TREE_CODE (TREE_TYPE (fntype)) == VOID_TYPE)
12926 cp_warning ("`operator=' should return a reference to `*this'");
12927
12928 /* Make the init_value nonzero so pushdecl knows this is not tentative.
12929 error_mark_node is replaced below (in poplevel) with the BLOCK. */
12930 DECL_INITIAL (decl1) = error_mark_node;
12931
12932#ifdef SET_DEFAULT_DECL_ATTRIBUTES
12933 SET_DEFAULT_DECL_ATTRIBUTES (decl1, attrs);
12934#endif
12935
12936 /* This function exists in static storage.
12937 (This does not mean `static' in the C sense!) */
12938 TREE_STATIC (decl1) = 1;
12939
12940 /* We must call push_template_decl after current_class_type is set
12941 up. (If we are processing inline definitions after exiting a
12942 class scope, current_class_type will be NULL_TREE until set above
12943 by push_nested_class.) */
12944 if (processing_template_decl)
12945 decl1 = push_template_decl (decl1);
12946
f181d4ae 12947 /* We are now in the scope of the function being defined. */
8d08fdba 12948 current_function_decl = decl1;
f181d4ae 12949
5566b478
MS
12950 /* Save the parm names or decls from this function's declarator
12951 where store_parm_decls will find them. */
12952 current_function_parms = last_function_parms;
12953 current_function_parm_tags = last_function_parm_tags;
8d08fdba 12954
5156628f 12955 if (! processing_template_decl)
8d08fdba 12956 {
d490621d
MM
12957 /* In a function definition, arg types must be complete. */
12958 require_complete_types_for_parms (current_function_parms);
f181d4ae 12959
5566b478
MS
12960 if (TYPE_SIZE (complete_type (TREE_TYPE (fntype))) == NULL_TREE)
12961 {
8251199e 12962 cp_error ("return-type `%#T' is an incomplete type",
5566b478 12963 TREE_TYPE (fntype));
8d08fdba 12964
5566b478
MS
12965 /* Make it return void instead, but don't change the
12966 type of the DECL_RESULT, in case we have a named return value. */
12967 if (ctype)
12968 TREE_TYPE (decl1)
12969 = build_cplus_method_type (build_type_variant (ctype,
12970 TREE_READONLY (decl1),
12971 TREE_SIDE_EFFECTS (decl1)),
12972 void_type_node,
12973 FUNCTION_ARG_CHAIN (decl1));
12974 else
12975 TREE_TYPE (decl1)
12976 = build_function_type (void_type_node,
12977 TYPE_ARG_TYPES (TREE_TYPE (decl1)));
12978 DECL_RESULT (decl1)
12979 = build_decl (RESULT_DECL, 0, TYPE_MAIN_VARIANT (TREE_TYPE (fntype)));
91063b51
MM
12980 TREE_READONLY (DECL_RESULT (decl1))
12981 = CP_TYPE_CONST_P (TREE_TYPE (fntype));
12982 TREE_THIS_VOLATILE (DECL_RESULT (decl1))
12983 = CP_TYPE_VOLATILE_P (TREE_TYPE (fntype));
5566b478
MS
12984 }
12985
a7a64a77 12986 abstract_virtuals_error (decl1, TREE_TYPE (fntype));
5566b478 12987 }
b7484fbe 12988
878cd289
MS
12989 /* Record the decl so that the function name is defined.
12990 If we already have a decl for this name, and it is a FUNCTION_DECL,
12991 use the old decl. */
36a117a5 12992 if (!processing_template_decl && pre_parsed_p == 0)
878cd289 12993 {
75650646 12994 /* A specialization is not used to guide overload resolution. */
959d8796
JM
12995 if ((flag_guiding_decls
12996 || !DECL_TEMPLATE_SPECIALIZATION (decl1))
12997 && ! DECL_FUNCTION_MEMBER_P (decl1))
75650646 12998 decl1 = pushdecl (decl1);
2c73f9f5 12999 else
b7698cf0
JM
13000 {
13001 /* We need to set the DECL_CONTEXT. */
13002 if (!DECL_CONTEXT (decl1) && DECL_TEMPLATE_INFO (decl1))
13003 DECL_CONTEXT (decl1) = DECL_CONTEXT (DECL_TI_TEMPLATE (decl1));
13004 /* And make sure we have enough default args. */
13005 check_default_args (decl1);
13006 }
878cd289
MS
13007 DECL_MAIN_VARIANT (decl1) = decl1;
13008 fntype = TREE_TYPE (decl1);
13009 }
5566b478
MS
13010
13011 current_function_decl = decl1;
878cd289 13012
db5ae43f 13013 if (DECL_INTERFACE_KNOWN (decl1))
faae18ab 13014 {
86052cc3
JM
13015 tree ctx = hack_decl_function_context (decl1);
13016
faae18ab
MS
13017 if (DECL_NOT_REALLY_EXTERN (decl1))
13018 DECL_EXTERNAL (decl1) = 0;
86052cc3
JM
13019
13020 if (ctx != NULL_TREE && DECL_THIS_INLINE (ctx)
13021 && TREE_PUBLIC (ctx))
13022 /* This is a function in a local class in an extern inline
13023 function. */
13024 comdat_linkage (decl1);
faae18ab 13025 }
8d08fdba
MS
13026 /* If this function belongs to an interface, it is public.
13027 If it belongs to someone else's interface, it is also external.
1f901793 13028 This only affects inlines and template instantiations. */
5566b478
MS
13029 else if (interface_unknown == 0
13030 && (! DECL_TEMPLATE_INSTANTIATION (decl1)
d11ad92e 13031 || flag_alt_external_templates))
8d08fdba 13032 {
5566b478 13033 if (DECL_THIS_INLINE (decl1) || DECL_TEMPLATE_INSTANTIATION (decl1)
5156628f 13034 || processing_template_decl)
1f901793
JM
13035 {
13036 DECL_EXTERNAL (decl1)
13037 = (interface_only
9c73ec84
MS
13038 || (DECL_THIS_INLINE (decl1) && ! flag_implement_inlines
13039 && !DECL_VINDEX (decl1)));
1f901793
JM
13040
13041 /* For WIN32 we also want to put these in linkonce sections. */
13042 maybe_make_one_only (decl1);
13043 }
db5ae43f 13044 else
893de33c 13045 DECL_EXTERNAL (decl1) = 0;
e8abc66f 13046 DECL_NOT_REALLY_EXTERN (decl1) = 0;
db5ae43f 13047 DECL_INTERFACE_KNOWN (decl1) = 1;
8d08fdba 13048 }
c16c47fb
JM
13049 else if (interface_unknown && interface_only
13050 && (! DECL_TEMPLATE_INSTANTIATION (decl1)
13051 || flag_alt_external_templates))
13052 {
13053 /* If MULTIPLE_SYMBOL_SPACES is defined and we saw a #pragma
13054 interface, we will have interface_only set but not
13055 interface_known. In that case, we don't want to use the normal
13056 heuristics because someone will supply a #pragma implementation
13057 elsewhere, and deducing it here would produce a conflict. */
13058 comdat_linkage (decl1);
13059 DECL_EXTERNAL (decl1) = 0;
13060 DECL_INTERFACE_KNOWN (decl1) = 1;
13061 DECL_DEFER_OUTPUT (decl1) = 1;
13062 }
8d08fdba 13063 else
a0a33927
MS
13064 {
13065 /* This is a definition, not a reference.
b7484fbe
MS
13066 So clear DECL_EXTERNAL. */
13067 DECL_EXTERNAL (decl1) = 0;
faae18ab 13068
5566b478
MS
13069 if ((DECL_THIS_INLINE (decl1) || DECL_TEMPLATE_INSTANTIATION (decl1))
13070 && ! DECL_INTERFACE_KNOWN (decl1)
13071 /* Don't try to defer nested functions for now. */
e76a2646 13072 && ! hack_decl_function_context (decl1))
878cd289
MS
13073 DECL_DEFER_OUTPUT (decl1) = 1;
13074 else
893de33c 13075 DECL_INTERFACE_KNOWN (decl1) = 1;
db5ae43f 13076 }
a9aedbc2 13077
8d08fdba
MS
13078 if (ctype != NULL_TREE && DECL_STATIC_FUNCTION_P (decl1))
13079 {
13080 if (TREE_CODE (fntype) == METHOD_TYPE)
13081 TREE_TYPE (decl1) = fntype
13082 = build_function_type (TREE_TYPE (fntype),
13083 TREE_CHAIN (TYPE_ARG_TYPES (fntype)));
5566b478
MS
13084 current_function_parms = TREE_CHAIN (current_function_parms);
13085 DECL_ARGUMENTS (decl1) = current_function_parms;
8d08fdba
MS
13086 ctype = NULL_TREE;
13087 }
13088 restype = TREE_TYPE (fntype);
13089
8d08fdba
MS
13090 if (ctype)
13091 {
8d08fdba 13092 /* If we're compiling a friend function, neither of the variables
4ac14744 13093 current_class_ptr nor current_class_type will have values. */
8d08fdba
MS
13094 if (! doing_friend)
13095 {
13096 /* We know that this was set up by `grokclassfn'.
13097 We do not wait until `store_parm_decls', since evil
13098 parse errors may never get us to that point. Here
13099 we keep the consistency between `current_class_type'
4ac14744 13100 and `current_class_ptr'. */
5566b478 13101 tree t = current_function_parms;
6eabb241
MM
13102 int i;
13103
5f34005f
RK
13104 my_friendly_assert (t != NULL_TREE
13105 && TREE_CODE (t) == PARM_DECL, 162);
6eabb241
MM
13106 my_friendly_assert (TREE_CODE (TREE_TYPE (t)) == POINTER_TYPE,
13107 19990811);
13108
13109 if (! hack_decl_function_context (decl1))
13110 temporary_allocation ();
13111 i = suspend_momentary ();
13112
13113 /* Normally, build_indirect_ref returns current_class_ref
13114 whenever current_class_ptr is dereferenced. This time,
13115 however, we want it to *create* current_class_ref, so we
13116 temporarily clear current_class_ptr to fool it. */
13117 current_class_ptr = NULL_TREE;
13118 current_class_ref = build_indirect_ref (t, NULL_PTR);
13119 current_class_ptr = t;
13120
13121 resume_momentary (i);
13122 if (! hack_decl_function_context (decl1))
13123 end_temporary_allocation ();
8d08fdba
MS
13124 }
13125 }
13126 else
7ddedda4 13127 current_class_ptr = current_class_ref = NULL_TREE;
8d08fdba 13128
db5ae43f
MS
13129 pushlevel (0);
13130 current_binding_level->parm_flag = 1;
13131
f30432d7
MS
13132 if (attrs)
13133 cplus_decl_attributes (decl1, NULL_TREE, attrs);
3a846e6e 13134
558475f0
MM
13135 if (!building_stmt_tree ())
13136 {
13137 GNU_xref_function (decl1, current_function_parms);
13138 make_function_rtl (decl1);
13139 }
db5ae43f 13140
8d08fdba
MS
13141 /* Promote the value to int before returning it. */
13142 if (C_PROMOTING_INTEGER_TYPE_P (restype))
5566b478
MS
13143 restype = type_promotes_to (restype);
13144
13145 /* If this fcn was already referenced via a block-scope `extern' decl
13146 (or an implicit decl), propagate certain information about the usage. */
13147 if (TREE_ADDRESSABLE (DECL_ASSEMBLER_NAME (decl1)))
13148 TREE_ADDRESSABLE (decl1) = 1;
13149
8d08fdba 13150 if (DECL_RESULT (decl1) == NULL_TREE)
f30432d7
MS
13151 {
13152 DECL_RESULT (decl1)
13153 = build_decl (RESULT_DECL, 0, TYPE_MAIN_VARIANT (restype));
91063b51
MM
13154 TREE_READONLY (DECL_RESULT (decl1)) = CP_TYPE_CONST_P (restype);
13155 TREE_THIS_VOLATILE (DECL_RESULT (decl1)) = CP_TYPE_VOLATILE_P (restype);
f30432d7 13156 }
8d08fdba 13157
948edfa7
JW
13158 /* Allocate further tree nodes temporarily during compilation
13159 of this function only. Tiemann moved up here from bottom of fn. */
13160 /* If this is a nested function, then we must continue to allocate RTL
13161 on the permanent obstack in case we need to inline it later. */
13162 if (! hack_decl_function_context (decl1))
5566b478
MS
13163 temporary_allocation ();
13164
558475f0
MM
13165 if (building_stmt_tree ())
13166 begin_stmt_tree (decl1);
5566b478
MS
13167
13168 ++function_depth;
13169
f30432d7
MS
13170 if (DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (decl1))
13171 && DECL_LANGUAGE (decl1) == lang_cplusplus)
8d08fdba
MS
13172 {
13173 dtor_label = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE);
13174 ctor_label = NULL_TREE;
13175 }
13176 else
13177 {
13178 dtor_label = NULL_TREE;
13179 if (DECL_CONSTRUCTOR_P (decl1))
13180 ctor_label = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE);
13181 }
13182
8d08fdba
MS
13183 return 1;
13184}
13185\f
9664b89e
JM
13186/* Called after store_parm_decls for a function-try-block. We need to update
13187 last_parm_cleanup_insn so that the base initializers for a constructor
13188 are run within this block, not before it. */
6467930b
MS
13189
13190void
13191expand_start_early_try_stmts ()
13192{
6467930b 13193 expand_start_try_stmts ();
9664b89e 13194 last_parm_cleanup_insn = get_last_insn ();
f30432d7
MS
13195}
13196
8d08fdba
MS
13197/* Store the parameter declarations into the current function declaration.
13198 This is called after parsing the parameter declarations, before
13199 digesting the body of the function.
13200
13201 Also install to binding contour return value identifier, if any. */
13202
13203void
13204store_parm_decls ()
13205{
13206 register tree fndecl = current_function_decl;
13207 register tree parm;
13208 int parms_have_cleanups = 0;
eb66be0e 13209 tree cleanups = NULL_TREE;
8d08fdba
MS
13210
13211 /* This is either a chain of PARM_DECLs (when a prototype is used). */
13212 tree specparms = current_function_parms;
13213
13214 /* This is a list of types declared among parms in a prototype. */
13215 tree parmtags = current_function_parm_tags;
13216
13217 /* This is a chain of any other decls that came in among the parm
13218 declarations. If a parm is declared with enum {foo, bar} x;
13219 then CONST_DECLs for foo and bar are put here. */
13220 tree nonparms = NULL_TREE;
13221
a9aedbc2 13222 if (toplevel_bindings_p ())
8d08fdba
MS
13223 fatal ("parse errors have confused me too much");
13224
13225 /* Initialize RTL machinery. */
13226 init_function_start (fndecl, input_filename, lineno);
13227
8d08fdba
MS
13228 /* Create a binding level for the parms. */
13229 expand_start_bindings (0);
13230
8d08fdba
MS
13231 if (specparms != NULL_TREE)
13232 {
13233 /* This case is when the function was defined with an ANSI prototype.
13234 The parms already have decls, so we need not do anything here
13235 except record them as in effect
13236 and complain if any redundant old-style parm decls were written. */
13237
13238 register tree next;
13239
13240 /* Must clear this because it might contain TYPE_DECLs declared
13241 at class level. */
13242 storedecls (NULL_TREE);
5566b478 13243
8d08fdba
MS
13244 for (parm = nreverse (specparms); parm; parm = next)
13245 {
13246 next = TREE_CHAIN (parm);
13247 if (TREE_CODE (parm) == PARM_DECL)
13248 {
5566b478 13249 tree cleanup;
8d08fdba
MS
13250 if (DECL_NAME (parm) == NULL_TREE)
13251 {
8d08fdba 13252 pushdecl (parm);
8d08fdba 13253 }
a1774733 13254 else if (TREE_CODE (TREE_TYPE (parm)) == VOID_TYPE)
8251199e 13255 cp_error ("parameter `%D' declared void", parm);
8d08fdba
MS
13256 else
13257 {
13258 /* Now fill in DECL_REFERENCE_SLOT for any of the parm decls.
13259 A parameter is assumed not to have any side effects.
13260 If this should change for any reason, then this
13261 will have to wrap the bashed reference type in a save_expr.
13262
13263 Also, if the parameter type is declared to be an X
13264 and there is an X(X&) constructor, we cannot lay it
13265 into the stack (any more), so we make this parameter
13266 look like it is really of reference type. Functions
13267 which pass parameters to this function will know to
13268 create a temporary in their frame, and pass a reference
13269 to that. */
13270
13271 if (TREE_CODE (TREE_TYPE (parm)) == REFERENCE_TYPE
13272 && TYPE_SIZE (TREE_TYPE (TREE_TYPE (parm))))
13273 SET_DECL_REFERENCE_SLOT (parm, convert_from_reference (parm));
13274
13275 pushdecl (parm);
13276 }
558475f0 13277 if (! building_stmt_tree ()
5566b478 13278 && (cleanup = maybe_build_cleanup (parm), cleanup))
8d08fdba
MS
13279 {
13280 expand_decl (parm);
8d08fdba 13281 parms_have_cleanups = 1;
eb66be0e
MS
13282
13283 /* Keep track of the cleanups. */
13284 cleanups = tree_cons (parm, cleanup, cleanups);
8d08fdba
MS
13285 }
13286 }
13287 else
13288 {
13289 /* If we find an enum constant or a type tag,
13290 put it aside for the moment. */
13291 TREE_CHAIN (parm) = NULL_TREE;
13292 nonparms = chainon (nonparms, parm);
13293 }
13294 }
13295
13296 /* Get the decls in their original chain order
13297 and record in the function. This is all and only the
13298 PARM_DECLs that were pushed into scope by the loop above. */
13299 DECL_ARGUMENTS (fndecl) = getdecls ();
13300
13301 storetags (chainon (parmtags, gettags ()));
13302 }
13303 else
13304 DECL_ARGUMENTS (fndecl) = NULL_TREE;
13305
13306 /* Now store the final chain of decls for the arguments
13307 as the decl-chain of the current lexical scope.
13308 Put the enumerators in as well, at the front so that
13309 DECL_ARGUMENTS is not modified. */
13310
13311 storedecls (chainon (nonparms, DECL_ARGUMENTS (fndecl)));
13312
f181d4ae
MM
13313 /* Declare __FUNCTION__ and __PRETTY_FUNCTION__ for this function. */
13314 declare_function_name ();
13315
8d08fdba 13316 /* Initialize the RTL code for the function. */
49ad7cfa 13317 DECL_SAVED_INSNS (fndecl) = 0;
558475f0 13318 if (! building_stmt_tree ())
5566b478 13319 expand_function_start (fndecl, parms_have_cleanups);
8d08fdba 13320
eb448459
MS
13321 current_function_parms_stored = 1;
13322
13323 /* If this function is `main', emit a call to `__main'
13324 to run global initializers, etc. */
558475f0 13325 if (DECL_MAIN_P (fndecl) && !building_stmt_tree ())
35680744 13326 expand_main_function ();
eb448459 13327
eb66be0e
MS
13328 /* Now that we have initialized the parms, we can start their
13329 cleanups. We cannot do this before, since expand_decl_cleanup
13330 should not be called before the parm can be used. */
558475f0 13331 if (cleanups && !building_stmt_tree ())
eb66be0e
MS
13332 {
13333 for (cleanups = nreverse (cleanups); cleanups; cleanups = TREE_CHAIN (cleanups))
13334 {
13335 if (! expand_decl_cleanup (TREE_PURPOSE (cleanups), TREE_VALUE (cleanups)))
8251199e 13336 cp_error ("parser lost in parsing declaration of `%D'",
eb66be0e
MS
13337 TREE_PURPOSE (cleanups));
13338 }
13339 }
13340
8d08fdba
MS
13341 /* Create a binding contour which can be used to catch
13342 cleanup-generated temporaries. Also, if the return value needs or
13343 has initialization, deal with that now. */
13344 if (parms_have_cleanups)
13345 {
13346 pushlevel (0);
558475f0
MM
13347 if (!building_stmt_tree ())
13348 expand_start_bindings (0);
8d08fdba
MS
13349 }
13350
558475f0 13351 if (! building_stmt_tree () && flag_exceptions)
f30432d7 13352 {
f30432d7
MS
13353 /* Do the starting of the exception specifications, if we have any. */
13354 if (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (current_function_decl)))
13355 expand_start_eh_spec ();
f30432d7 13356 }
eb448459 13357
5c825fc2 13358 last_parm_cleanup_insn = get_last_insn ();
72b7eeff 13359 last_dtor_insn = get_last_insn ();
8d08fdba
MS
13360}
13361
13362/* Bind a name and initialization to the return value of
13363 the current function. */
e92cc029 13364
8d08fdba 13365void
558475f0
MM
13366store_return_init (decl)
13367 tree decl;
8d08fdba 13368{
558475f0
MM
13369 /* If this named return value comes in a register, put it in a
13370 pseudo-register. */
13371 if (DECL_REGISTER (decl))
8d08fdba 13372 {
558475f0
MM
13373 original_result_rtx = DECL_RTL (decl);
13374 DECL_RTL (decl) = gen_reg_rtx (DECL_MODE (decl));
8d08fdba
MS
13375 }
13376}
13377
8d08fdba
MS
13378\f
13379/* Finish up a function declaration and compile that function
13380 all the way to assembler language output. The free the storage
13381 for the function definition.
13382
13383 This is called after parsing the body of the function definition.
13384 LINENO is the current line number.
13385
f181d4ae
MM
13386 FLAGS is a bitwise or of the following values:
13387 1 - CALL_POPLEVEL
13388 An extra call to poplevel (and expand_end_bindings) must be
13389 made to take care of the binding contour for the base
13390 initializers. This is only relevant for constructors.
13391 2 - INCLASS_INLINE
13392 We just finished processing the body of an in-class inline
13393 function definition. (This processing will have taken place
13394 after the class definition is complete.)
e92cc029
MS
13395
13396 NESTED is nonzero if we were in the middle of compiling another function
13397 when we started on this one. */
8d08fdba
MS
13398
13399void
f181d4ae 13400finish_function (lineno, flags, nested)
8d08fdba 13401 int lineno;
f181d4ae 13402 int flags;
db5ae43f 13403 int nested;
8d08fdba
MS
13404{
13405 register tree fndecl = current_function_decl;
13406 tree fntype, ctype = NULL_TREE;
49ad7cfa 13407 rtx fn_last_parm_insn, insns;
8d08fdba
MS
13408 /* Label to use if this function is supposed to return a value. */
13409 tree no_return_label = NULL_TREE;
13410 tree decls = NULL_TREE;
f181d4ae
MM
13411 int call_poplevel = (flags & 1) != 0;
13412 int inclass_inline = (flags & 2) != 0;
558475f0 13413 int expand_p;
8d08fdba
MS
13414
13415 /* When we get some parse errors, we can end up without a
13416 current_function_decl, so cope. */
13417 if (fndecl == NULL_TREE)
13418 return;
13419
0aafb128 13420 if (function_depth > 1)
e92cc029
MS
13421 nested = 1;
13422
8d08fdba
MS
13423 fntype = TREE_TYPE (fndecl);
13424
13425/* TREE_READONLY (fndecl) = 1;
13426 This caused &foo to be of type ptr-to-const-function
13427 which then got a warning when stored in a ptr-to-function variable. */
13428
13429 /* This happens on strange parse errors. */
13430 if (! current_function_parms_stored)
13431 {
13432 call_poplevel = 0;
13433 store_parm_decls ();
13434 }
13435
558475f0 13436 if (building_stmt_tree ())
8d08fdba 13437 {
5566b478
MS
13438 if (DECL_CONSTRUCTOR_P (fndecl) && call_poplevel)
13439 {
13440 decls = getdecls ();
13441 expand_end_bindings (decls, decls != NULL_TREE, 0);
13442 poplevel (decls != NULL_TREE, 0, 0);
13443 }
13444 }
13445 else
13446 {
13447 if (write_symbols != NO_DEBUG /*&& TREE_CODE (fntype) != METHOD_TYPE*/)
8d08fdba 13448 {
5566b478
MS
13449 tree ttype = target_type (fntype);
13450 tree parmdecl;
13451
8d08fdba
MS
13452 if (IS_AGGR_TYPE (ttype))
13453 /* Let debugger know it should output info for this type. */
13454 note_debug_info_needed (ttype);
5566b478
MS
13455
13456 for (parmdecl = DECL_ARGUMENTS (fndecl); parmdecl; parmdecl = TREE_CHAIN (parmdecl))
13457 {
13458 ttype = target_type (TREE_TYPE (parmdecl));
13459 if (IS_AGGR_TYPE (ttype))
13460 /* Let debugger know it should output info for this type. */
13461 note_debug_info_needed (ttype);
13462 }
8d08fdba 13463 }
8d08fdba 13464
5566b478
MS
13465 /* Clean house because we will need to reorder insns here. */
13466 do_pending_stack_adjust ();
8d08fdba 13467
5566b478 13468 if (dtor_label)
8d08fdba 13469 {
5566b478
MS
13470 tree binfo = TYPE_BINFO (current_class_type);
13471 tree cond = integer_one_node;
13472 tree exprstmt;
13473 tree in_charge_node = lookup_name (in_charge_identifier, 0);
13474 tree virtual_size;
13475 int ok_to_optimize_dtor = 0;
13476 int empty_dtor = get_last_insn () == last_dtor_insn;
8d08fdba 13477
5566b478
MS
13478 if (current_function_assigns_this)
13479 cond = build (NE_EXPR, boolean_type_node,
4ac14744 13480 current_class_ptr, integer_zero_node);
5566b478
MS
13481 else
13482 {
13483 int n_baseclasses = CLASSTYPE_N_BASECLASSES (current_class_type);
13484
13485 /* If this destructor is empty, then we don't need to check
13486 whether `this' is NULL in some cases. */
13487 if ((flag_this_is_variable & 1) == 0)
13488 ok_to_optimize_dtor = 1;
13489 else if (empty_dtor)
13490 ok_to_optimize_dtor
13491 = (n_baseclasses == 0
13492 || (n_baseclasses == 1
13493 && TYPE_HAS_DESTRUCTOR (TYPE_BINFO_BASETYPE (current_class_type, 0))));
13494 }
8d08fdba 13495
5566b478
MS
13496 /* These initializations might go inline. Protect
13497 the binding level of the parms. */
13498 pushlevel (0);
13499 expand_start_bindings (0);
8d08fdba 13500
5566b478
MS
13501 if (current_function_assigns_this)
13502 {
13503 current_function_assigns_this = 0;
13504 current_function_just_assigned_this = 0;
13505 }
8d08fdba 13506
5566b478
MS
13507 /* Generate the code to call destructor on base class.
13508 If this destructor belongs to a class with virtual
13509 functions, then set the virtual function table
13510 pointer to represent the type of our base class. */
8d08fdba 13511
5566b478 13512 /* This side-effect makes call to `build_delete' generate the
2a2480e1
JM
13513 code we have to have at the end of this destructor.
13514 `build_delete' will set the flag again. */
5566b478 13515 TYPE_HAS_DESTRUCTOR (current_class_type) = 0;
8d08fdba 13516
5566b478
MS
13517 /* These are two cases where we cannot delegate deletion. */
13518 if (TYPE_USES_VIRTUAL_BASECLASSES (current_class_type)
13519 || TYPE_GETS_REG_DELETE (current_class_type))
4ac14744 13520 exprstmt = build_delete (current_class_type, current_class_ref, integer_zero_node,
e92cc029 13521 LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR|LOOKUP_NORMAL, 0);
5566b478 13522 else
4ac14744 13523 exprstmt = build_delete (current_class_type, current_class_ref, in_charge_node,
e92cc029 13524 LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR|LOOKUP_NORMAL, 0);
5566b478
MS
13525
13526 /* If we did not assign to this, then `this' is non-zero at
13527 the end of a destructor. As a special optimization, don't
13528 emit test if this is an empty destructor. If it does nothing,
13529 it does nothing. If it calls a base destructor, the base
13530 destructor will perform the test. */
13531
13532 if (exprstmt != error_mark_node
13533 && (TREE_CODE (exprstmt) != NOP_EXPR
13534 || TREE_OPERAND (exprstmt, 0) != integer_zero_node
13535 || TYPE_USES_VIRTUAL_BASECLASSES (current_class_type)))
8d08fdba 13536 {
5566b478
MS
13537 expand_label (dtor_label);
13538 if (cond != integer_one_node)
13539 expand_start_cond (cond, 0);
13540 if (exprstmt != void_zero_node)
13541 /* Don't call `expand_expr_stmt' if we're not going to do
13542 anything, since -Wall will give a diagnostic. */
13543 expand_expr_stmt (exprstmt);
13544
13545 /* Run destructor on all virtual baseclasses. */
13546 if (TYPE_USES_VIRTUAL_BASECLASSES (current_class_type))
8d08fdba 13547 {
5566b478
MS
13548 tree vbases = nreverse (copy_list (CLASSTYPE_VBASECLASSES (current_class_type)));
13549 expand_start_cond (build (BIT_AND_EXPR, integer_type_node,
13550 in_charge_node, integer_two_node), 0);
13551 while (vbases)
8d08fdba 13552 {
5566b478
MS
13553 if (TYPE_NEEDS_DESTRUCTOR (BINFO_TYPE (vbases)))
13554 {
bd6dd845
MS
13555 tree vb = get_vbase
13556 (BINFO_TYPE (vbases),
8ea2460f 13557 TYPE_BINFO (current_class_type));
e92cc029 13558 expand_expr_stmt
bd6dd845
MS
13559 (build_scoped_method_call
13560 (current_class_ref, vb, dtor_identifier,
e66d884e 13561 build_expr_list (NULL_TREE, integer_zero_node)));
5566b478
MS
13562 }
13563 vbases = TREE_CHAIN (vbases);
8d08fdba 13564 }
5566b478 13565 expand_end_cond ();
8d08fdba 13566 }
5566b478
MS
13567
13568 do_pending_stack_adjust ();
13569 if (cond != integer_one_node)
13570 expand_end_cond ();
8d08fdba
MS
13571 }
13572
5566b478
MS
13573 virtual_size = c_sizeof (current_class_type);
13574
13575 /* At the end, call delete if that's what's requested. */
519ebd1e
JM
13576
13577 /* FDIS sez: At the point of definition of a virtual destructor
13578 (including an implicit definition), non-placement operator
13579 delete shall be looked up in the scope of the destructor's
13580 class and if found shall be accessible and unambiguous.
13581
13582 This is somewhat unclear, but I take it to mean that if the
13583 class only defines placement deletes we don't do anything here.
13584 So we pass LOOKUP_SPECULATIVELY; delete_sanity will complain
13585 for us if they ever try to delete one of these. */
13586
13587 if (TYPE_GETS_REG_DELETE (current_class_type)
13588 || TYPE_USES_VIRTUAL_BASECLASSES (current_class_type))
13589 exprstmt = build_op_delete_call
13590 (DELETE_EXPR, current_class_ptr, virtual_size,
13591 LOOKUP_NORMAL | LOOKUP_SPECULATIVELY, NULL_TREE);
5566b478
MS
13592 else
13593 exprstmt = NULL_TREE;
8d08fdba 13594
5566b478
MS
13595 if (exprstmt)
13596 {
13597 cond = build (BIT_AND_EXPR, integer_type_node,
13598 in_charge_node, integer_one_node);
13599 expand_start_cond (cond, 0);
13600 expand_expr_stmt (exprstmt);
13601 expand_end_cond ();
13602 }
8d08fdba 13603
5566b478 13604 /* End of destructor. */
fc378698 13605 expand_end_bindings (NULL_TREE, getdecls () != NULL_TREE, 0);
536333d4 13606 poplevel (getdecls () != NULL_TREE, 0, 0);
5566b478
MS
13607
13608 /* Back to the top of destructor. */
956d6950 13609 /* Don't execute destructor code if `this' is NULL. */
5566b478
MS
13610
13611 start_sequence ();
13612
13613 /* If the dtor is empty, and we know there is not possible way we
13614 could use any vtable entries, before they are possibly set by
13615 a base class dtor, we don't have to setup the vtables, as we
13616 know that any base class dtoring will set up any vtables it
13617 needs. We avoid MI, because one base class dtor can do a
13618 virtual dispatch to an overridden function that would need to
13619 have a non-related vtable set up, we cannot avoid setting up
13620 vtables in that case. We could change this to see if there is
13621 just one vtable. */
13622 if (! empty_dtor || TYPE_USES_COMPLEX_INHERITANCE (current_class_type))
13623 {
13624 /* Make all virtual function table pointers in non-virtual base
13625 classes point to CURRENT_CLASS_TYPE's virtual function
13626 tables. */
4ac14744 13627 expand_direct_vtbls_init (binfo, binfo, 1, 0, current_class_ptr);
8d08fdba 13628
5566b478 13629 if (TYPE_USES_VIRTUAL_BASECLASSES (current_class_type))
4ac14744 13630 expand_indirect_vtbls_init (binfo, current_class_ref, current_class_ptr);
5566b478
MS
13631 }
13632
13633 if (! ok_to_optimize_dtor)
13634 {
13635 cond = build_binary_op (NE_EXPR,
337c90cc 13636 current_class_ptr, integer_zero_node);
5566b478
MS
13637 expand_start_cond (cond, 0);
13638 }
8d08fdba 13639
5566b478
MS
13640 insns = get_insns ();
13641 end_sequence ();
f30432d7 13642
49ad7cfa
BS
13643 fn_last_parm_insn = get_first_nonparm_insn ();
13644 if (fn_last_parm_insn == NULL_RTX)
13645 fn_last_parm_insn = get_last_insn ();
5566b478 13646 else
49ad7cfa 13647 fn_last_parm_insn = previous_insn (fn_last_parm_insn);
8d08fdba 13648
49ad7cfa 13649 emit_insns_after (insns, fn_last_parm_insn);
72b7eeff 13650
5566b478
MS
13651 if (! ok_to_optimize_dtor)
13652 expand_end_cond ();
72b7eeff 13653 }
5566b478 13654 else if (current_function_assigns_this)
8d08fdba 13655 {
5566b478
MS
13656 /* Does not need to call emit_base_init, because
13657 that is done (if needed) just after assignment to this
13658 is seen. */
13659
13660 if (DECL_CONSTRUCTOR_P (current_function_decl))
13661 {
13662 end_protect_partials ();
13663 expand_label (ctor_label);
13664 ctor_label = NULL_TREE;
13665
13666 if (call_poplevel)
13667 {
13668 decls = getdecls ();
13669 expand_end_bindings (decls, decls != NULL_TREE, 0);
13670 poplevel (decls != NULL_TREE, 0, 0);
13671 }
1c2c08a5
JM
13672 /* c_expand_return knows to return 'this' from a constructor. */
13673 c_expand_return (NULL_TREE);
5566b478 13674 }
a1774733 13675 else if (TREE_CODE (TREE_TYPE (DECL_RESULT (current_function_decl))) != VOID_TYPE
5566b478
MS
13676 && return_label != NULL_RTX)
13677 no_return_label = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE);
13678
13679 current_function_assigns_this = 0;
13680 current_function_just_assigned_this = 0;
13681 base_init_expr = NULL_TREE;
8d08fdba 13682 }
5566b478
MS
13683 else if (DECL_CONSTRUCTOR_P (fndecl))
13684 {
a703fb38 13685 tree cond = NULL_TREE, thenclause = NULL_TREE;
5566b478
MS
13686 /* Allow constructor for a type to get a new instance of the object
13687 using `build_new'. */
13688 tree abstract_virtuals = CLASSTYPE_ABSTRACT_VIRTUALS (current_class_type);
13689 CLASSTYPE_ABSTRACT_VIRTUALS (current_class_type) = NULL_TREE;
f30432d7 13690
5566b478
MS
13691 if (flag_this_is_variable > 0)
13692 {
13693 cond = build_binary_op (EQ_EXPR,
337c90cc 13694 current_class_ptr, integer_zero_node);
4ac14744 13695 thenclause = build_modify_expr (current_class_ptr, NOP_EXPR,
5566b478
MS
13696 build_new (NULL_TREE, current_class_type, void_type_node, 0));
13697 }
f30432d7 13698
5566b478 13699 CLASSTYPE_ABSTRACT_VIRTUALS (current_class_type) = abstract_virtuals;
f30432d7 13700
5566b478
MS
13701 start_sequence ();
13702
13703 if (flag_this_is_variable > 0)
13704 {
13705 expand_start_cond (cond, 0);
13706 expand_expr_stmt (thenclause);
13707 expand_end_cond ();
13708 }
13709
13710 /* Emit insns from `emit_base_init' which sets up virtual
13711 function table pointer(s). */
13712 if (base_init_expr)
13713 {
13714 expand_expr_stmt (base_init_expr);
13715 base_init_expr = NULL_TREE;
13716 }
13717
13718 insns = get_insns ();
13719 end_sequence ();
13720
b87692e5 13721 /* This is where the body of the constructor begins. */
5566b478 13722
b87692e5 13723 emit_insns_after (insns, last_parm_cleanup_insn);
8d08fdba 13724
b7484fbe 13725 end_protect_partials ();
5566b478
MS
13726
13727 /* This is where the body of the constructor ends. */
8d08fdba
MS
13728 expand_label (ctor_label);
13729 ctor_label = NULL_TREE;
13730
13731 if (call_poplevel)
13732 {
13733 decls = getdecls ();
8d08fdba 13734 expand_end_bindings (decls, decls != NULL_TREE, 0);
5566b478 13735 poplevel (decls != NULL_TREE, 1, 0);
8d08fdba 13736 }
8d08fdba 13737
1c2c08a5
JM
13738 /* c_expand_return knows to return 'this' from a constructor. */
13739 c_expand_return (NULL_TREE);
8d08fdba 13740
5566b478
MS
13741 current_function_assigns_this = 0;
13742 current_function_just_assigned_this = 0;
13743 }
35680744 13744 else if (DECL_MAIN_P (fndecl))
8d08fdba 13745 {
5566b478
MS
13746 /* Make it so that `main' always returns 0 by default. */
13747#ifdef VMS
13748 c_expand_return (integer_one_node);
13749#else
13750 c_expand_return (integer_zero_node);
13751#endif
8d08fdba 13752 }
5566b478
MS
13753 else if (return_label != NULL_RTX
13754 && current_function_return_value == NULL_TREE
13755 && ! DECL_NAME (DECL_RESULT (current_function_decl)))
13756 no_return_label = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE);
8d08fdba 13757
eb448459
MS
13758 if (flag_exceptions)
13759 expand_exception_blocks ();
13760
5566b478
MS
13761 /* If this function is supposed to return a value, ensure that
13762 we do not fall into the cleanups by mistake. The end of our
13763 function will look like this:
13764
13765 user code (may have return stmt somewhere)
13766 goto no_return_label
13767 cleanup_label:
13768 cleanups
13769 goto return_label
13770 no_return_label:
13771 NOTE_INSN_FUNCTION_END
13772 return_label:
13773 things for return
13774
13775 If the user omits a return stmt in the USER CODE section, we
13776 will have a control path which reaches NOTE_INSN_FUNCTION_END.
13777 Otherwise, we won't. */
13778 if (no_return_label)
8d08fdba 13779 {
5566b478
MS
13780 DECL_CONTEXT (no_return_label) = fndecl;
13781 DECL_INITIAL (no_return_label) = error_mark_node;
13782 DECL_SOURCE_FILE (no_return_label) = input_filename;
13783 DECL_SOURCE_LINE (no_return_label) = lineno;
13784 expand_goto (no_return_label);
8d08fdba
MS
13785 }
13786
5566b478 13787 if (cleanup_label)
a9aedbc2 13788 {
2c73f9f5 13789 /* Remove the binding contour which is used
5566b478
MS
13790 to catch cleanup-generated temporaries. */
13791 expand_end_bindings (0, 0, 0);
13792 poplevel (0, 0, 0);
8d08fdba 13793
eb448459
MS
13794 /* Emit label at beginning of cleanup code for parameters. */
13795 emit_label (cleanup_label);
13796 }
b7484fbe 13797
5566b478
MS
13798 /* Get return value into register if that's where it's supposed to be. */
13799 if (original_result_rtx)
13800 fixup_result_decl (DECL_RESULT (fndecl), original_result_rtx);
8d08fdba 13801
5566b478
MS
13802 /* Finish building code that will trigger warnings if users forget
13803 to make their functions return values. */
13804 if (no_return_label || cleanup_label)
13805 emit_jump (return_label);
13806 if (no_return_label)
8d08fdba 13807 {
5566b478
MS
13808 /* We don't need to call `expand_*_return' here because we
13809 don't need any cleanups here--this path of code is only
13810 for error checking purposes. */
13811 expand_label (no_return_label);
8d08fdba
MS
13812 }
13813
5566b478
MS
13814 /* Generate rtl for function exit. */
13815 expand_function_end (input_filename, lineno, 1);
8d08fdba 13816 }
558475f0
MM
13817
13818 /* We have to save this value here in case
13819 maybe_end_member_template_processing decides to pop all the
13820 template parameters. */
13821 expand_p = !building_stmt_tree ();
f181d4ae 13822
558475f0
MM
13823 /* If we're saving up tree structure, tie off the function now. */
13824 if (!expand_p)
13825 finish_stmt_tree (fndecl);
8d2733ca 13826
8d08fdba
MS
13827 /* This must come after expand_function_end because cleanups might
13828 have declarations (from inline functions) that need to go into
13829 this function's blocks. */
13830 if (current_binding_level->parm_flag != 1)
13831 my_friendly_abort (122);
13832 poplevel (1, 0, 1);
13833
f181d4ae
MM
13834 /* If this is a in-class inline definition, we may have to pop the
13835 bindings for the template parameters that we added in
13836 maybe_begin_member_template_processing when start_function was
13837 called. */
13838 if (inclass_inline)
13839 maybe_end_member_template_processing ();
13840
2c73f9f5 13841 /* Reset scope for C++: if we were in the scope of a class,
db5ae43f
MS
13842 then when we finish this function, we are not longer so.
13843 This cannot be done until we know for sure that no more
13844 class members will ever be referenced in this function
13845 (i.e., calls to destructors). */
13846 if (current_class_name)
13847 {
13848 ctype = current_class_type;
b74a0560 13849 pop_nested_class ();
db5ae43f 13850 }
db5ae43f 13851
1caa11d3
MM
13852 /* Must mark the RESULT_DECL as being in this function. */
13853 DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
13854
13855 /* Set the BLOCK_SUPERCONTEXT of the outermost function scope to point
13856 to the FUNCTION_DECL node itself. */
13857 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
13858
558475f0 13859 if (expand_p)
5566b478 13860 {
75650646
MM
13861 int saved_flag_keep_inline_functions =
13862 flag_keep_inline_functions;
13863
5566b478
MS
13864 /* So we can tell if jump_optimize sets it to 1. */
13865 can_reach_end = 0;
8d08fdba 13866
75650646 13867 if (DECL_CONTEXT (fndecl) != NULL_TREE
e1467ff2 13868 && hack_decl_function_context (fndecl))
75650646
MM
13869 /* Trick rest_of_compilation into not deferring output of this
13870 function, even if it is inline, since the rtl_obstack for
13871 this function is the function_obstack of the enclosing
13872 function and will be deallocated when the enclosing
13873 function is gone. See save_tree_status. */
13874 flag_keep_inline_functions = 1;
13875
6633d636
MS
13876 /* Run the optimizers and output the assembler code for this
13877 function. */
13878
13879 if (DECL_ARTIFICIAL (fndecl))
13880 {
13881 /* Do we really *want* to inline this synthesized method? */
13882
13883 int save_fif = flag_inline_functions;
13884 flag_inline_functions = 1;
13885
13886 /* Turn off DECL_INLINE for the moment so function_cannot_inline_p
13887 will check our size. */
13888 DECL_INLINE (fndecl) = 0;
13889
13890 rest_of_compilation (fndecl);
13891 flag_inline_functions = save_fif;
13892 }
13893 else
13894 rest_of_compilation (fndecl);
8d08fdba 13895
75650646
MM
13896 flag_keep_inline_functions = saved_flag_keep_inline_functions;
13897
5566b478
MS
13898 if (DECL_SAVED_INSNS (fndecl) && ! TREE_ASM_WRITTEN (fndecl))
13899 {
13900 /* Set DECL_EXTERNAL so that assemble_external will be called as
13901 necessary. We'll clear it again in finish_file. */
13902 if (! DECL_EXTERNAL (fndecl))
13903 DECL_NOT_REALLY_EXTERN (fndecl) = 1;
13904 DECL_EXTERNAL (fndecl) = 1;
13905 mark_inline_for_output (fndecl);
13906 }
8926095f 13907
d2e5ee5c
MS
13908 if (ctype && TREE_ASM_WRITTEN (fndecl))
13909 note_debug_info_needed (ctype);
13910
5566b478 13911 current_function_returns_null |= can_reach_end;
8d08fdba 13912
5566b478
MS
13913 /* Since we don't normally go through c_expand_return for constructors,
13914 this normally gets the wrong value.
13915 Also, named return values have their return codes emitted after
13916 NOTE_INSN_FUNCTION_END, confusing jump.c. */
13917 if (DECL_CONSTRUCTOR_P (fndecl)
13918 || DECL_NAME (DECL_RESULT (fndecl)) != NULL_TREE)
13919 current_function_returns_null = 0;
8d08fdba 13920
5566b478 13921 if (TREE_THIS_VOLATILE (fndecl) && current_function_returns_null)
8251199e 13922 cp_warning ("`noreturn' function `%D' does return", fndecl);
5566b478
MS
13923 else if ((warn_return_type || pedantic)
13924 && current_function_returns_null
a1774733 13925 && TREE_CODE (TREE_TYPE (fntype)) != VOID_TYPE)
5566b478
MS
13926 {
13927 /* If this function returns non-void and control can drop through,
13928 complain. */
8251199e 13929 cp_warning ("control reaches end of non-void function `%D'", fndecl);
5566b478
MS
13930 }
13931 /* With just -W, complain only if function returns both with
13932 and without a value. */
13933 else if (extra_warnings
13934 && current_function_returns_value && current_function_returns_null)
8251199e 13935 warning ("this function may return with or without a value");
8d08fdba 13936 }
5566b478
MS
13937
13938 --function_depth;
8d08fdba
MS
13939
13940 /* Free all the tree nodes making up this function. */
13941 /* Switch back to allocating nodes permanently
13942 until we start another function. */
db5ae43f
MS
13943 if (! nested)
13944 permanent_allocation (1);
8d08fdba 13945
49ad7cfa 13946 if (DECL_SAVED_INSNS (fndecl) == 0)
8d08fdba 13947 {
d2e5ee5c
MS
13948 tree t;
13949
8d08fdba
MS
13950 /* Stop pointing to the local nodes about to be freed. */
13951 /* But DECL_INITIAL must remain nonzero so we know this
13952 was an actual function definition. */
13953 DECL_INITIAL (fndecl) = error_mark_node;
d2e5ee5c 13954 for (t = DECL_ARGUMENTS (fndecl); t; t = TREE_CHAIN (t))
de22184b 13955 DECL_RTL (t) = DECL_INCOMING_RTL (t) = NULL_RTX;
8d08fdba
MS
13956 }
13957
e1cd6e56
MS
13958 if (DECL_STATIC_CONSTRUCTOR (fndecl))
13959 static_ctors = perm_tree_cons (NULL_TREE, fndecl, static_ctors);
13960 if (DECL_STATIC_DESTRUCTOR (fndecl))
13961 static_dtors = perm_tree_cons (NULL_TREE, fndecl, static_dtors);
13962
28cbf42c
MS
13963 if (! nested)
13964 {
13965 /* Let the error reporting routines know that we're outside a
13966 function. For a nested function, this value is used in
13967 pop_cp_function_context and then reset via pop_function_context. */
13968 current_function_decl = NULL_TREE;
13969 }
13970
e349ee73 13971 named_label_uses = NULL;
4ac14744
MS
13972 current_class_ptr = NULL_TREE;
13973 current_class_ref = NULL_TREE;
8d08fdba
MS
13974}
13975\f
13976/* Create the FUNCTION_DECL for a function definition.
8d08fdba
MS
13977 DECLSPECS and DECLARATOR are the parts of the declaration;
13978 they describe the return type and the name of the function,
13979 but twisted together in a fashion that parallels the syntax of C.
13980
13981 This function creates a binding context for the function body
13982 as well as setting up the FUNCTION_DECL in current_function_decl.
13983
13984 Returns a FUNCTION_DECL on success.
13985
13986 If the DECLARATOR is not suitable for a function (it defines a datum
13987 instead), we return 0, which tells yyparse to report a parse error.
13988
13989 May return void_type_node indicating that this method is actually
13990 a friend. See grokfield for more details.
13991
13992 Came here with a `.pushlevel' .
13993
13994 DO NOT MAKE ANY CHANGES TO THIS CODE WITHOUT MAKING CORRESPONDING
13995 CHANGES TO CODE IN `grokfield'. */
e92cc029 13996
8d08fdba 13997tree
acf82af2
JM
13998start_method (declspecs, declarator, attrlist)
13999 tree declarator, declspecs, attrlist;
8d08fdba 14000{
c11b6f21 14001 tree fndecl = grokdeclarator (declarator, declspecs, MEMFUNCDEF, 0,
acf82af2 14002 attrlist);
8d08fdba
MS
14003
14004 /* Something too ugly to handle. */
14005 if (fndecl == NULL_TREE)
14006 return NULL_TREE;
14007
14008 /* Pass friends other than inline friend functions back. */
a1774733 14009 if (fndecl == void_type_node)
8d08fdba
MS
14010 return fndecl;
14011
14012 if (TREE_CODE (fndecl) != FUNCTION_DECL)
14013 /* Not a function, tell parser to report parse error. */
14014 return NULL_TREE;
14015
8d08fdba
MS
14016 if (DECL_IN_AGGR_P (fndecl))
14017 {
14018 if (IDENTIFIER_ERROR_LOCUS (DECL_ASSEMBLER_NAME (fndecl)) != current_class_type)
14019 {
2c73f9f5
ML
14020 if (DECL_CONTEXT (fndecl)
14021 && TREE_CODE( DECL_CONTEXT (fndecl)) != NAMESPACE_DECL)
8251199e
JM
14022 cp_error ("`%D' is already defined in class %s", fndecl,
14023 TYPE_NAME_STRING (DECL_CONTEXT (fndecl)));
8d08fdba
MS
14024 }
14025 return void_type_node;
14026 }
14027
f3400fe2
JM
14028 check_template_shadow (fndecl);
14029
faae18ab
MS
14030 DECL_THIS_INLINE (fndecl) = 1;
14031
8926095f 14032 if (flag_default_inline)
8d08fdba
MS
14033 DECL_INLINE (fndecl) = 1;
14034
36a117a5
MM
14035 /* We process method specializations in finish_struct_1. */
14036 if (processing_template_decl && !DECL_TEMPLATE_SPECIALIZATION (fndecl))
3ac3d9ea 14037 fndecl = push_template_decl (fndecl);
a0a33927 14038
8d08fdba
MS
14039 /* We read in the parameters on the maybepermanent_obstack,
14040 but we won't be getting back to them until after we
14041 may have clobbered them. So the call to preserve_data
14042 will keep them safe. */
14043 preserve_data ();
14044
14045 if (! DECL_FRIEND_P (fndecl))
14046 {
8d08fdba
MS
14047 if (TREE_CHAIN (fndecl))
14048 {
14049 fndecl = copy_node (fndecl);
14050 TREE_CHAIN (fndecl) = NULL_TREE;
14051 }
14052
14053 if (DECL_CONSTRUCTOR_P (fndecl))
a0a33927
MS
14054 {
14055 if (! grok_ctor_properties (current_class_type, fndecl))
14056 return void_type_node;
14057 }
8d08fdba
MS
14058 else if (IDENTIFIER_OPNAME_P (DECL_NAME (fndecl)))
14059 grok_op_properties (fndecl, DECL_VIRTUAL_P (fndecl), 0);
14060 }
14061
82580166 14062 cp_finish_decl (fndecl, NULL_TREE, NULL_TREE, 0, 0);
8d08fdba
MS
14063
14064 /* Make a place for the parms */
14065 pushlevel (0);
14066 current_binding_level->parm_flag = 1;
14067
14068 DECL_IN_AGGR_P (fndecl) = 1;
14069 return fndecl;
14070}
14071
14072/* Go through the motions of finishing a function definition.
14073 We don't compile this method until after the whole class has
14074 been processed.
14075
14076 FINISH_METHOD must return something that looks as though it
14077 came from GROKFIELD (since we are defining a method, after all).
14078
14079 This is called after parsing the body of the function definition.
14080 STMTS is the chain of statements that makes up the function body.
14081
14082 DECL is the ..._DECL that `start_method' provided. */
14083
14084tree
14085finish_method (decl)
14086 tree decl;
14087{
14088 register tree fndecl = decl;
14089 tree old_initial;
8d08fdba
MS
14090
14091 register tree link;
14092
a1774733 14093 if (decl == void_type_node)
8d08fdba
MS
14094 return decl;
14095
14096 old_initial = DECL_INITIAL (fndecl);
14097
14098 /* Undo the level for the parms (from start_method).
14099 This is like poplevel, but it causes nothing to be
14100 saved. Saving information here confuses symbol-table
14101 output routines. Besides, this information will
14102 be correctly output when this method is actually
14103 compiled. */
14104
14105 /* Clear out the meanings of the local variables of this level;
14106 also record in each decl which block it belongs to. */
14107
14108 for (link = current_binding_level->names; link; link = TREE_CHAIN (link))
14109 {
14110 if (DECL_NAME (link) != NULL_TREE)
d8f8dca1 14111 pop_binding (DECL_NAME (link), link);
8d08fdba
MS
14112 my_friendly_assert (TREE_CODE (link) != FUNCTION_DECL, 163);
14113 DECL_CONTEXT (link) = NULL_TREE;
14114 }
14115
8d08fdba
MS
14116 GNU_xref_end_scope ((HOST_WIDE_INT) current_binding_level,
14117 (HOST_WIDE_INT) current_binding_level->level_chain,
14118 current_binding_level->parm_flag,
5566b478 14119 current_binding_level->keep);
8d08fdba
MS
14120
14121 poplevel (0, 0, 0);
14122
14123 DECL_INITIAL (fndecl) = old_initial;
14124
14125 /* We used to check if the context of FNDECL was different from
14126 current_class_type as another way to get inside here. This didn't work
14127 for String.cc in libg++. */
14128 if (DECL_FRIEND_P (fndecl))
14129 {
14130 CLASSTYPE_INLINE_FRIENDS (current_class_type)
14131 = tree_cons (NULL_TREE, fndecl, CLASSTYPE_INLINE_FRIENDS (current_class_type));
14132 decl = void_type_node;
14133 }
14134
14135 return decl;
14136}
14137\f
14138/* Called when a new struct TYPE is defined.
14139 If this structure or union completes the type of any previous
14140 variable declaration, lay it out and output its rtl. */
14141
14142void
14143hack_incomplete_structures (type)
14144 tree type;
14145{
f30432d7 14146 tree *list;
8d08fdba 14147
f30432d7 14148 if (current_binding_level->incomplete == NULL_TREE)
8d08fdba
MS
14149 return;
14150
14151 if (!type) /* Don't do this for class templates. */
14152 return;
14153
f30432d7
MS
14154 for (list = &current_binding_level->incomplete; *list; )
14155 {
14156 tree decl = TREE_VALUE (*list);
a703fb38 14157 if ((decl && TREE_TYPE (decl) == type)
f30432d7
MS
14158 || (TREE_TYPE (decl)
14159 && TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
14160 && TREE_TYPE (TREE_TYPE (decl)) == type))
14161 {
14162 int toplevel = toplevel_bindings_p ();
14163 if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
14164 && TREE_TYPE (TREE_TYPE (decl)) == type)
14165 layout_type (TREE_TYPE (decl));
14166 layout_decl (decl, 0);
14167 rest_of_decl_compilation (decl, NULL_PTR, toplevel, 0);
14168 if (! toplevel)
14169 {
14170 tree cleanup;
14171 expand_decl (decl);
14172 cleanup = maybe_build_cleanup (decl);
14173 expand_decl_init (decl);
e349ee73 14174 if (! expand_decl_cleanup (decl, cleanup))
8251199e 14175 cp_error ("parser lost in parsing declaration of `%D'",
f30432d7
MS
14176 decl);
14177 }
14178 *list = TREE_CHAIN (*list);
14179 }
14180 else
14181 list = &TREE_CHAIN (*list);
14182 }
8d08fdba
MS
14183}
14184
8d08fdba 14185/* If DECL is of a type which needs a cleanup, build that cleanup here.
c73964b2 14186 See build_delete for information about AUTO_DELETE.
8d08fdba
MS
14187
14188 Don't build these on the momentary obstack; they must live
14189 the life of the binding contour. */
e92cc029 14190
c73964b2
MS
14191static tree
14192maybe_build_cleanup_1 (decl, auto_delete)
14193 tree decl, auto_delete;
8d08fdba
MS
14194{
14195 tree type = TREE_TYPE (decl);
44689c12 14196 if (type != error_mark_node && TYPE_NEEDS_DESTRUCTOR (type))
8d08fdba
MS
14197 {
14198 int temp = 0, flags = LOOKUP_NORMAL|LOOKUP_DESTRUCTOR;
14199 tree rval;
8d08fdba
MS
14200
14201 if (TREE_CODE (decl) != PARM_DECL)
14202 temp = suspend_momentary ();
14203
14204 if (TREE_CODE (type) == ARRAY_TYPE)
14205 rval = decl;
14206 else
14207 {
14208 mark_addressable (decl);
14209 rval = build_unary_op (ADDR_EXPR, decl, 0);
14210 }
14211
14212 /* Optimize for space over speed here. */
14213 if (! TYPE_USES_VIRTUAL_BASECLASSES (type)
14214 || flag_expensive_optimizations)
14215 flags |= LOOKUP_NONVIRTUAL;
14216
c73964b2 14217 rval = build_delete (TREE_TYPE (rval), rval, auto_delete, flags, 0);
8d08fdba
MS
14218
14219 if (TYPE_USES_VIRTUAL_BASECLASSES (type)
14220 && ! TYPE_HAS_DESTRUCTOR (type))
e66d884e
JM
14221 rval = build_compound_expr (expr_tree_cons (NULL_TREE, rval,
14222 build_expr_list (NULL_TREE, build_vbase_delete (type, decl))));
8d08fdba 14223
8d08fdba
MS
14224 if (TREE_CODE (decl) != PARM_DECL)
14225 resume_momentary (temp);
14226
8d08fdba
MS
14227 return rval;
14228 }
14229 return 0;
14230}
c73964b2
MS
14231
14232/* If DECL is of a type which needs a cleanup, build that cleanup
14233 here. The cleanup does free the storage with a call to delete. */
14234
14235tree
14236maybe_build_cleanup_and_delete (decl)
14237 tree decl;
14238{
14239 return maybe_build_cleanup_1 (decl, integer_three_node);
14240}
14241
14242/* If DECL is of a type which needs a cleanup, build that cleanup
14243 here. The cleanup does not free the storage with a call a delete. */
14244
14245tree
14246maybe_build_cleanup (decl)
14247 tree decl;
14248{
14249 return maybe_build_cleanup_1 (decl, integer_two_node);
14250}
8d08fdba
MS
14251\f
14252/* Expand a C++ expression at the statement level.
14253 This is needed to ferret out nodes which have UNKNOWN_TYPE.
14254 The C++ type checker should get all of these out when
14255 expressions are combined with other, type-providing, expressions,
14256 leaving only orphan expressions, such as:
14257
e92cc029 14258 &class::bar; / / takes its address, but does nothing with it. */
8d08fdba 14259
8d08fdba
MS
14260void
14261cplus_expand_expr_stmt (exp)
14262 tree exp;
14263{
eb66be0e
MS
14264 /* Arrange for all temps to disappear. */
14265 expand_start_target_temps ();
14266
66543169
NS
14267 exp = require_complete_type_in_void (exp);
14268
14269 if (TREE_CODE (exp) == FUNCTION_DECL)
8d08fdba 14270 {
66543169
NS
14271 cp_warning ("reference, not call, to function `%D'", exp);
14272 warning ("at this point in file");
8d08fdba 14273 }
8d08fdba 14274
63c68bb7 14275#if 0
66543169
NS
14276 /* We should do this eventually, but right now this causes regex.o from
14277 libg++ to miscompile, and tString to core dump. */
14278 exp = build1 (CLEANUP_POINT_EXPR, TREE_TYPE (exp), exp);
63c68bb7 14279#endif
b3e401c2 14280
66543169
NS
14281 /* Strip unused implicit INDIRECT_REFs of references. */
14282 if (TREE_CODE (exp) == INDIRECT_REF
14283 && TREE_CODE (TREE_TYPE (TREE_OPERAND (exp, 0))) == REFERENCE_TYPE)
14284 exp = TREE_OPERAND (exp, 0);
b3e401c2 14285
66543169
NS
14286 /* If we don't do this, we end up down inside expand_expr
14287 trying to do TYPE_MODE on the ERROR_MARK, and really
14288 go outside the bounds of the type. */
14289 if (exp != error_mark_node)
14290 expand_expr_stmt (break_out_cleanups (exp));
8d08fdba
MS
14291
14292 /* Clean up any pending cleanups. This happens when a function call
14293 returns a cleanup-needing value that nobody uses. */
eb66be0e 14294 expand_end_target_temps ();
8d08fdba
MS
14295}
14296
558475f0 14297/* When a stmt has been parsed, this function is called. */
8d08fdba
MS
14298
14299void
14300finish_stmt ()
14301{
558475f0
MM
14302 if (!current_function_assigns_this
14303 && current_function_just_assigned_this)
8d08fdba 14304 {
558475f0
MM
14305 if (DECL_CONSTRUCTOR_P (current_function_decl))
14306 {
14307 /* Constructors must wait until we are out of control
14308 zones before calling base constructors. */
14309 if (in_control_zone_p ())
14310 return;
14311 expand_expr_stmt (base_init_expr);
14312 check_base_init (current_class_type);
14313 }
14314 current_function_assigns_this = 1;
8d08fdba 14315 }
558475f0
MM
14316
14317 /* Always assume this statement was not an expression statement. If
14318 it actually was an expression statement, its our callers
14319 responsibility to fix this up. */
14320 last_expr_type = NULL_TREE;
8d08fdba
MS
14321}
14322
8d08fdba 14323/* Change a static member function definition into a FUNCTION_TYPE, instead
700f8a87
MS
14324 of the METHOD_TYPE that we create when it's originally parsed.
14325
14326 WARNING: DO NOT pass &TREE_TYPE (decl) to FN or &TYPE_ARG_TYPES
14327 (TREE_TYPE (decl)) to ARGTYPES, as doing so will corrupt the types of
14328 other decls. Either pass the addresses of local variables or NULL. */
14329
8857f91e 14330void
700f8a87
MS
14331revert_static_member_fn (decl, fn, argtypes)
14332 tree *decl, *fn, *argtypes;
8d08fdba 14333{
700f8a87
MS
14334 tree tmp;
14335 tree function = fn ? *fn : TREE_TYPE (*decl);
14336 tree args = argtypes ? *argtypes : TYPE_ARG_TYPES (function);
8d08fdba 14337
91063b51
MM
14338 if (CP_TYPE_QUALS (TREE_TYPE (TREE_VALUE (args)))
14339 != TYPE_UNQUALIFIED)
14340 cp_error ("static member function `%#D' declared with type qualifiers",
14341 *decl);
f30432d7 14342
700f8a87
MS
14343 args = TREE_CHAIN (args);
14344 tmp = build_function_type (TREE_TYPE (function), args);
91063b51 14345 tmp = build_qualified_type (tmp, CP_TYPE_QUALS (function));
f30432d7 14346 tmp = build_exception_variant (tmp,
8d08fdba
MS
14347 TYPE_RAISES_EXCEPTIONS (function));
14348 TREE_TYPE (*decl) = tmp;
f30432d7
MS
14349 if (DECL_ARGUMENTS (*decl))
14350 DECL_ARGUMENTS (*decl) = TREE_CHAIN (DECL_ARGUMENTS (*decl));
8d08fdba 14351 DECL_STATIC_FUNCTION_P (*decl) = 1;
700f8a87
MS
14352 if (fn)
14353 *fn = tmp;
14354 if (argtypes)
14355 *argtypes = args;
8d08fdba 14356}
a4443a08 14357
db5ae43f
MS
14358struct cp_function
14359{
14360 int returns_value;
14361 int returns_null;
db5ae43f
MS
14362 int assigns_this;
14363 int just_assigned_this;
14364 int parms_stored;
7215f9a0 14365 int temp_name_counter;
db5ae43f 14366 tree named_labels;
5524676d 14367 struct named_label_list *named_label_uses;
db5ae43f
MS
14368 tree shadowed_labels;
14369 tree ctor_label;
14370 tree dtor_label;
72b7eeff 14371 rtx last_dtor_insn;
b87692e5 14372 rtx last_parm_cleanup_insn;
79ff2c6c
MS
14373 tree base_init_list;
14374 tree member_init_list;
a9aedbc2 14375 tree base_init_expr;
4ac14744
MS
14376 tree current_class_ptr;
14377 tree current_class_ref;
db5ae43f
MS
14378 rtx result_rtx;
14379 struct cp_function *next;
14380 struct binding_level *binding_level;
42976354 14381 int static_labelno;
0dde4175 14382 int in_function_try_handler;
558475f0
MM
14383 int expanding_p;
14384 tree last_tree;
14385 tree last_expr_type;
db5ae43f
MS
14386};
14387
bd6dd845 14388static struct cp_function *cp_function_chain;
db5ae43f 14389
7215f9a0
MS
14390extern int temp_name_counter;
14391
db5ae43f
MS
14392/* Save and reinitialize the variables
14393 used during compilation of a C++ function. */
14394
14395void
28cbf42c
MS
14396push_cp_function_context (context)
14397 tree context;
db5ae43f
MS
14398{
14399 struct cp_function *p
14400 = (struct cp_function *) xmalloc (sizeof (struct cp_function));
14401
28cbf42c 14402 push_function_context_to (context);
db5ae43f
MS
14403
14404 p->next = cp_function_chain;
14405 cp_function_chain = p;
14406
14407 p->named_labels = named_labels;
5524676d 14408 p->named_label_uses = named_label_uses;
db5ae43f
MS
14409 p->shadowed_labels = shadowed_labels;
14410 p->returns_value = current_function_returns_value;
14411 p->returns_null = current_function_returns_null;
db5ae43f
MS
14412 p->binding_level = current_binding_level;
14413 p->ctor_label = ctor_label;
14414 p->dtor_label = dtor_label;
72b7eeff 14415 p->last_dtor_insn = last_dtor_insn;
b87692e5 14416 p->last_parm_cleanup_insn = last_parm_cleanup_insn;
db5ae43f
MS
14417 p->assigns_this = current_function_assigns_this;
14418 p->just_assigned_this = current_function_just_assigned_this;
14419 p->parms_stored = current_function_parms_stored;
14420 p->result_rtx = original_result_rtx;
a9aedbc2 14421 p->base_init_expr = base_init_expr;
7215f9a0 14422 p->temp_name_counter = temp_name_counter;
79ff2c6c
MS
14423 p->base_init_list = current_base_init_list;
14424 p->member_init_list = current_member_init_list;
4ac14744
MS
14425 p->current_class_ptr = current_class_ptr;
14426 p->current_class_ref = current_class_ref;
42976354 14427 p->static_labelno = static_labelno;
0dde4175 14428 p->in_function_try_handler = in_function_try_handler;
558475f0
MM
14429 p->last_tree = last_tree;
14430 p->last_expr_type = last_expr_type;
14431 p->expanding_p = expanding_p;
14432
14433 /* For now, we always assume we're expanding all the way to RTL
14434 unless we're explicitly doing otherwise. */
14435 expanding_p = 1;
db5ae43f
MS
14436}
14437
14438/* Restore the variables used during compilation of a C++ function. */
14439
14440void
28cbf42c
MS
14441pop_cp_function_context (context)
14442 tree context;
db5ae43f
MS
14443{
14444 struct cp_function *p = cp_function_chain;
14445 tree link;
14446
14447 /* Bring back all the labels that were shadowed. */
14448 for (link = shadowed_labels; link; link = TREE_CHAIN (link))
14449 if (DECL_NAME (TREE_VALUE (link)) != 0)
14450 SET_IDENTIFIER_LABEL_VALUE (DECL_NAME (TREE_VALUE (link)),
14451 TREE_VALUE (link));
14452
28cbf42c 14453 pop_function_context_from (context);
db5ae43f
MS
14454
14455 cp_function_chain = p->next;
14456
14457 named_labels = p->named_labels;
5524676d 14458 named_label_uses = p->named_label_uses;
db5ae43f
MS
14459 shadowed_labels = p->shadowed_labels;
14460 current_function_returns_value = p->returns_value;
14461 current_function_returns_null = p->returns_null;
db5ae43f
MS
14462 current_binding_level = p->binding_level;
14463 ctor_label = p->ctor_label;
14464 dtor_label = p->dtor_label;
72b7eeff 14465 last_dtor_insn = p->last_dtor_insn;
b87692e5 14466 last_parm_cleanup_insn = p->last_parm_cleanup_insn;
db5ae43f
MS
14467 current_function_assigns_this = p->assigns_this;
14468 current_function_just_assigned_this = p->just_assigned_this;
14469 current_function_parms_stored = p->parms_stored;
14470 original_result_rtx = p->result_rtx;
a9aedbc2 14471 base_init_expr = p->base_init_expr;
7215f9a0 14472 temp_name_counter = p->temp_name_counter;
79ff2c6c
MS
14473 current_base_init_list = p->base_init_list;
14474 current_member_init_list = p->member_init_list;
4ac14744
MS
14475 current_class_ptr = p->current_class_ptr;
14476 current_class_ref = p->current_class_ref;
42976354 14477 static_labelno = p->static_labelno;
0dde4175 14478 in_function_try_handler = p->in_function_try_handler;
558475f0
MM
14479 last_tree = p->last_tree;
14480 last_expr_type = p->last_expr_type;
14481 expanding_p = p->expanding_p;
db5ae43f
MS
14482
14483 free (p);
14484}
ebfc180f 14485
5566b478
MS
14486int
14487in_function_p ()
14488{
14489 return function_depth != 0;
14490}
This page took 3.151839 seconds and 5 git commands to generate.