]> gcc.gnu.org Git - gcc.git/blame - gcc/cp/decl.c
* Make-lang.in (f77.mostlyclean): Add missing exeext.
[gcc.git] / gcc / cp / decl.c
CommitLineData
e5e809f4
JL
1/* Process declarations and variables for C compiler.
2 Copyright (C) 1988, 92-97, 1998 Free Software Foundation, Inc.
3 Contributed by Michael Tiemann (tiemann@cygnus.com)
8d08fdba
MS
4
5This file is part of GNU CC.
6
7GNU CC is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation; either version 2, or (at your option)
10any later version.
11
12GNU CC is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with GNU CC; see the file COPYING. If not, write to
e9fa0c7c
RK
19the Free Software Foundation, 59 Temple Place - Suite 330,
20Boston, MA 02111-1307, USA. */
8d08fdba
MS
21
22
23/* Process declarations and symbol lookup for C front end.
24 Also constructs types; the standard scalar types at initialization,
25 and structure, union, array and enum types when they are declared. */
26
27/* ??? not all decl nodes are given the most useful possible
28 line numbers. For example, the CONST_DECLs for enum values. */
29
8d08fdba 30#include "config.h"
8d052bc7 31#include "system.h"
8d08fdba
MS
32#include "tree.h"
33#include "rtl.h"
34#include "flags.h"
35#include "cp-tree.h"
36#include "decl.h"
37#include "lex.h"
8d08fdba
MS
38#include <signal.h>
39#include "obstack.h"
72b7eeff 40#include "defaults.h"
49c249e1
JM
41#include "output.h"
42#include "except.h"
54f92bfb 43#include "toplev.h"
8d08fdba
MS
44
45#define obstack_chunk_alloc xmalloc
46#define obstack_chunk_free free
47
8d2733ca
MS
48extern tree builtin_return_address_fndecl;
49
8d08fdba
MS
50extern struct obstack permanent_obstack;
51
52extern int current_class_depth;
53
e1cd6e56
MS
54extern tree static_ctors, static_dtors;
55
42976354
BK
56extern int static_labelno;
57
30394414
JM
58extern tree current_namespace;
59extern tree global_namespace;
60
8d08fdba
MS
61/* Stack of places to restore the search obstack back to. */
62
63/* Obstack used for remembering local class declarations (like
64 enums and static (const) members. */
65#include "stack.h"
5566b478 66struct obstack decl_obstack;
8d08fdba
MS
67static struct stack_level *decl_stack;
68
69#ifndef CHAR_TYPE_SIZE
70#define CHAR_TYPE_SIZE BITS_PER_UNIT
71#endif
72
73#ifndef SHORT_TYPE_SIZE
74#define SHORT_TYPE_SIZE (BITS_PER_UNIT * MIN ((UNITS_PER_WORD + 1) / 2, 2))
75#endif
76
77#ifndef INT_TYPE_SIZE
78#define INT_TYPE_SIZE BITS_PER_WORD
79#endif
80
81#ifndef LONG_TYPE_SIZE
82#define LONG_TYPE_SIZE BITS_PER_WORD
83#endif
84
85#ifndef LONG_LONG_TYPE_SIZE
86#define LONG_LONG_TYPE_SIZE (BITS_PER_WORD * 2)
87#endif
88
89#ifndef WCHAR_UNSIGNED
90#define WCHAR_UNSIGNED 0
91#endif
92
93#ifndef FLOAT_TYPE_SIZE
94#define FLOAT_TYPE_SIZE BITS_PER_WORD
95#endif
96
97#ifndef DOUBLE_TYPE_SIZE
98#define DOUBLE_TYPE_SIZE (BITS_PER_WORD * 2)
99#endif
100
101#ifndef LONG_DOUBLE_TYPE_SIZE
102#define LONG_DOUBLE_TYPE_SIZE (BITS_PER_WORD * 2)
103#endif
104
255512c1 105#ifndef BOOL_TYPE_SIZE
e1cd6e56 106#ifdef SLOW_BYTE_ACCESS
d2e5ee5c 107#define BOOL_TYPE_SIZE ((SLOW_BYTE_ACCESS) ? (POINTER_SIZE) : (CHAR_TYPE_SIZE))
e1cd6e56 108#else
d2e5ee5c 109#define BOOL_TYPE_SIZE CHAR_TYPE_SIZE
e1cd6e56 110#endif
255512c1
JM
111#endif
112
8d08fdba
MS
113/* We let tm.h override the types used here, to handle trivial differences
114 such as the choice of unsigned int or long unsigned int for size_t.
115 When machines start needing nontrivial differences in the size type,
116 it would be best to do something here to figure out automatically
117 from other information what type to use. */
118
119#ifndef SIZE_TYPE
120#define SIZE_TYPE "long unsigned int"
121#endif
122
123#ifndef PTRDIFF_TYPE
124#define PTRDIFF_TYPE "long int"
125#endif
126
127#ifndef WCHAR_TYPE
128#define WCHAR_TYPE "int"
129#endif
130
8d08fdba
MS
131static tree grokparms PROTO((tree, int));
132static tree lookup_nested_type PROTO((tree, tree));
133static char *redeclaration_error_message PROTO((tree, tree));
bd6dd845
MS
134static tree push_overloaded_decl PROTO((tree, int));
135static void push_overloaded_decl_top_level PROTO((tree, int));
8d08fdba 136
49c249e1
JM
137static struct stack_level *push_decl_level PROTO((struct stack_level *,
138 struct obstack *));
139static void push_binding_level PROTO((struct binding_level *, int,
140 int));
141static void pop_binding_level PROTO((void));
142static void suspend_binding_level PROTO((void));
143static void resume_binding_level PROTO((struct binding_level *));
144static struct binding_level *make_binding_level PROTO((void));
145static int namespace_bindings_p PROTO((void));
146static void declare_namespace_level PROTO((void));
147static tree get_unique_name PROTO((void));
148static void signal_catch PROTO((int));
149static void storedecls PROTO((tree));
150static void storetags PROTO((tree));
151static void require_complete_types_for_parms PROTO((tree));
152static void push_overloaded_decl_1 PROTO((tree));
153static int ambi_op_p PROTO((tree));
154static int unary_op_p PROTO((tree));
155static tree store_bindings PROTO((tree, tree));
156static tree lookup_tag_reverse PROTO((tree, tree));
157static tree obscure_complex_init PROTO((tree, tree));
158static tree maybe_build_cleanup_1 PROTO((tree, tree));
159static tree lookup_name_real PROTO((tree, int, int));
160static void warn_extern_redeclared_static PROTO((tree, tree));
a703fb38 161static void grok_reference_init PROTO((tree, tree, tree));
386b8a85 162static tree grokfndecl PROTO((tree, tree, tree, tree, int,
2c73f9f5
ML
163 enum overload_flags, tree,
164 tree, tree, int, int, int, int, int, int, tree));
165static tree grokvardecl PROTO((tree, tree, RID_BIT_TYPE *, int, int, tree));
49c249e1
JM
166static tree lookup_tag PROTO((enum tree_code, tree,
167 struct binding_level *, int));
168static void set_identifier_type_value_with_scope
169 PROTO((tree, tree, struct binding_level *));
170static void record_builtin_type PROTO((enum rid, char *, tree));
171static int member_function_or_else PROTO((tree, tree, char *));
172static void bad_specifiers PROTO((tree, char *, int, int, int, int,
173 int));
8d08fdba 174
69ac77ce
JL
175#if defined (DEBUG_CP_BINDING_LEVELS)
176static void indent PROTO((void));
177#endif
178
2c73f9f5 179/* A node which has tree code ERROR_MARK, and whose type is itself.
8d08fdba
MS
180 All erroneous expressions are replaced with this node. All functions
181 that accept nodes as arguments should avoid generating error messages
182 if this node is one of the arguments, since it is undesirable to get
183 multiple error messages from one error in the input. */
184
185tree error_mark_node;
186
187/* Erroneous argument lists can use this *IFF* they do not modify it. */
188tree error_mark_list;
189
190/* INTEGER_TYPE and REAL_TYPE nodes for the standard data types */
191
192tree short_integer_type_node;
193tree integer_type_node;
194tree long_integer_type_node;
195tree long_long_integer_type_node;
196
197tree short_unsigned_type_node;
198tree unsigned_type_node;
199tree long_unsigned_type_node;
200tree long_long_unsigned_type_node;
201
202tree ptrdiff_type_node;
203
204tree unsigned_char_type_node;
205tree signed_char_type_node;
206tree char_type_node;
207tree wchar_type_node;
208tree signed_wchar_type_node;
209tree unsigned_wchar_type_node;
210
f376e137
MS
211tree wchar_decl_node;
212
8d08fdba
MS
213tree float_type_node;
214tree double_type_node;
215tree long_double_type_node;
216
37c46b43
MS
217tree complex_integer_type_node;
218tree complex_float_type_node;
219tree complex_double_type_node;
220tree complex_long_double_type_node;
221
8d08fdba
MS
222tree intQI_type_node;
223tree intHI_type_node;
224tree intSI_type_node;
225tree intDI_type_node;
f7554e8c 226tree intTI_type_node;
8d08fdba
MS
227
228tree unsigned_intQI_type_node;
229tree unsigned_intHI_type_node;
230tree unsigned_intSI_type_node;
231tree unsigned_intDI_type_node;
f7554e8c 232tree unsigned_intTI_type_node;
8d08fdba 233
2c73f9f5 234/* A VOID_TYPE node, and the same, packaged in a TREE_LIST. */
8d08fdba
MS
235
236tree void_type_node, void_list_node;
237tree void_zero_node;
238
239/* Nodes for types `void *' and `const void *'. */
240
bd6dd845 241tree ptr_type_node;
6c20b7e9 242tree const_ptr_type_node;
8d08fdba
MS
243
244/* Nodes for types `char *' and `const char *'. */
245
246tree string_type_node, const_string_type_node;
247
248/* Type `char[256]' or something like it.
249 Used when an array of char is needed and the size is irrelevant. */
250
251tree char_array_type_node;
252
253/* Type `int[256]' or something like it.
254 Used when an array of int needed and the size is irrelevant. */
255
256tree int_array_type_node;
257
258/* Type `wchar_t[256]' or something like it.
259 Used when a wide string literal is created. */
260
261tree wchar_array_type_node;
262
2986ae00 263/* The bool data type, and constants */
255512c1 264tree boolean_type_node, boolean_true_node, boolean_false_node;
2986ae00 265
2c73f9f5 266/* Type `int ()' -- used for implicit declaration of functions. */
8d08fdba
MS
267
268tree default_function_type;
269
2c73f9f5 270/* Function types `double (double)' and `double (double, double)', etc. */
8d08fdba 271
bd6dd845
MS
272static tree double_ftype_double, double_ftype_double_double;
273static tree int_ftype_int, long_ftype_long;
274static tree float_ftype_float;
275static tree ldouble_ftype_ldouble;
8d08fdba 276
8d08fdba 277/* Function type `int (const void *, const void *, size_t)' */
5566b478 278static tree int_ftype_cptr_cptr_sizet;
8d08fdba
MS
279
280/* C++ extensions */
700f8a87 281tree vtable_entry_type;
8d08fdba 282tree delta_type_node;
db5ae43f 283#if 0
e92cc029 284/* Old rtti stuff. */
db5ae43f
MS
285tree __baselist_desc_type_node;
286tree __i_desc_type_node, __m_desc_type_node;
8d08fdba 287tree __t_desc_array_type, __i_desc_array_type, __m_desc_array_type;
db5ae43f 288#endif
5566b478
MS
289tree __t_desc_type_node;
290#if 0
291tree __tp_desc_type_node;
292#endif
db5ae43f
MS
293tree __access_mode_type_node;
294tree __bltn_desc_type_node, __user_desc_type_node, __class_desc_type_node;
295tree __ptr_desc_type_node, __attr_desc_type_node, __func_desc_type_node;
296tree __ptmf_desc_type_node, __ptmd_desc_type_node;
297#if 0
298/* Not needed yet? May be needed one day? */
299tree __bltn_desc_array_type, __user_desc_array_type, __class_desc_array_type;
300tree __ptr_desc_array_type, __attr_dec_array_type, __func_desc_array_type;
301tree __ptmf_desc_array_type, __ptmd_desc_array_type;
302#endif
303
2c73f9f5
ML
304/* Indicates that there is a type value in some namespace, although
305 that is not necessarily in scope at the moment. */
306
307static tree global_type_node;
308
8d08fdba
MS
309tree class_star_type_node;
310tree class_type_node, record_type_node, union_type_node, enum_type_node;
db5ae43f 311tree unknown_type_node;
8d08fdba
MS
312tree opaque_type_node, signature_type_node;
313tree sigtable_entry_type;
8d08fdba 314
8d08fdba
MS
315/* Array type `vtable_entry_type[]' */
316tree vtbl_type_node;
317
6633d636
MS
318/* namespace std */
319tree std_node;
2c73f9f5 320int in_std = 0;
6633d636 321
8d08fdba
MS
322/* In a destructor, the point at which all derived class destroying
323 has been done, just before any base class destroying will be done. */
324
325tree dtor_label;
326
72b7eeff
MS
327/* In a destructor, the last insn emitted after the start of the
328 function and the parms. */
329
bd6dd845 330static rtx last_dtor_insn;
72b7eeff 331
b87692e5 332/* In a constructor, the last insn emitted after the start of the
9664b89e
JM
333 function and the parms, the exception specification and any
334 function-try-block. The constructor initializers are emitted after
335 this insn. */
b87692e5
MS
336
337static rtx last_parm_cleanup_insn;
338
8d08fdba
MS
339/* In a constructor, the point at which we are ready to return
340 the pointer to the initialized object. */
341
342tree ctor_label;
343
8d08fdba
MS
344/* A FUNCTION_DECL which can call `abort'. Not necessarily the
345 one that the user will declare, but sufficient to be called
346 by routines that want to abort the program. */
347
348tree abort_fndecl;
349
350extern rtx cleanup_label, return_label;
351
352/* If original DECL_RESULT of current function was a register,
353 but due to being an addressable named return value, would up
354 on the stack, this variable holds the named return value's
355 original location. */
bd6dd845 356static rtx original_result_rtx;
8d08fdba
MS
357
358/* Sequence of insns which represents base initialization. */
a9aedbc2 359tree base_init_expr;
8d08fdba
MS
360
361/* C++: Keep these around to reduce calls to `get_identifier'.
362 Identifiers for `this' in member functions and the auto-delete
363 parameter for destructors. */
364tree this_identifier, in_charge_identifier;
fc378698 365tree ctor_identifier, dtor_identifier;
e92cc029 366/* Used in pointer to member functions, in vtables, and in sigtables. */
8d08fdba 367tree pfn_identifier, index_identifier, delta_identifier, delta2_identifier;
9dd70aa4 368tree pfn_or_delta2_identifier, tag_identifier;
5566b478 369tree vt_off_identifier;
8d08fdba 370
e349ee73
MS
371struct named_label_list
372{
373 struct binding_level *binding_level;
374 tree names_in_scope;
375 tree label_decl;
376 char *filename_o_goto;
377 int lineno_o_goto;
378 struct named_label_list *next;
379};
380
8d08fdba
MS
381/* A list (chain of TREE_LIST nodes) of named label uses.
382 The TREE_PURPOSE field is the list of variables defined
38e01259 383 in the label's scope defined at the point of use.
8d08fdba
MS
384 The TREE_VALUE field is the LABEL_DECL used.
385 The TREE_TYPE field holds `current_binding_level' at the
386 point of the label's use.
387
e349ee73
MS
388 BWAHAHAAHAHahhahahahaah. No, no, no, said the little chicken.
389
390 Look at the pretty struct named_label_list. See the pretty struct
391 with the pretty named fields that describe what they do. See the
392 pretty lack of gratuitous casts. Notice the code got a lot cleaner.
393
8d08fdba
MS
394 Used only for jumps to as-yet undefined labels, since
395 jumps to defined labels can have their validity checked
396 by stmt.c. */
397
e349ee73 398static struct named_label_list *named_label_uses = NULL;
8d08fdba
MS
399
400/* A list of objects which have constructors or destructors
401 which reside in the global scope. The decl is stored in
402 the TREE_VALUE slot and the initializer is stored
403 in the TREE_PURPOSE slot. */
404tree static_aggregates;
405
8d08fdba
MS
406/* -- end of C++ */
407
408/* Two expressions that are constants with value zero.
409 The first is of type `int', the second of type `void *'. */
410
411tree integer_zero_node;
412tree null_pointer_node;
413
d11ad92e
MS
414/* The value for __null (NULL), either of type `void *' or, with -ansi,
415 an integer type of the same size. */
416
417tree null_node;
418
8d08fdba
MS
419/* A node for the integer constants 1, 2, and 3. */
420
421tree integer_one_node, integer_two_node, integer_three_node;
422
8d08fdba
MS
423/* While defining an enum type, this is 1 plus the last enumerator
424 constant value. */
425
426static tree enum_next_value;
427
7177d104
MS
428/* Nonzero means that there was overflow computing enum_next_value. */
429
430static int enum_overflow;
431
8d08fdba
MS
432/* Parsing a function declarator leaves a list of parameter names
433 or a chain or parameter decls here. */
434
435tree last_function_parms;
436
437/* Parsing a function declarator leaves here a chain of structure
438 and enum types declared in the parmlist. */
439
440static tree last_function_parm_tags;
441
442/* After parsing the declarator that starts a function definition,
443 `start_function' puts here the list of parameter names or chain of decls.
444 `store_parm_decls' finds it here. */
445
446static tree current_function_parms;
447
448/* Similar, for last_function_parm_tags. */
449static tree current_function_parm_tags;
450
451/* A list (chain of TREE_LIST nodes) of all LABEL_DECLs in the function
452 that have names. Here so we can clear out their names' definitions
453 at the end of the function. */
454
455static tree named_labels;
456
457/* A list of LABEL_DECLs from outer contexts that are currently shadowed. */
458
459static tree shadowed_labels;
460
8d08fdba
MS
461/* The FUNCTION_DECL for the function currently being compiled,
462 or 0 if between functions. */
463tree current_function_decl;
464
465/* Set to 0 at beginning of a function definition, set to 1 if
466 a return statement that specifies a return value is seen. */
467
468int current_function_returns_value;
469
470/* Set to 0 at beginning of a function definition, set to 1 if
471 a return statement with no argument is seen. */
472
473int current_function_returns_null;
474
475/* Set to 0 at beginning of a function definition, and whenever
476 a label (case or named) is defined. Set to value of expression
477 returned from function when that value can be transformed into
478 a named return value. */
479
480tree current_function_return_value;
481
482/* Set to nonzero by `grokdeclarator' for a function
483 whose return type is defaulted, if warnings for this are desired. */
484
485static int warn_about_return_type;
486
8d08fdba
MS
487/* Nonzero means give `double' the same size as `float'. */
488
489extern int flag_short_double;
490
491/* Nonzero means don't recognize any builtin functions. */
492
493extern int flag_no_builtin;
494
00595019
MS
495/* Nonzero means don't recognize the non-ANSI builtin functions.
496 -ansi sets this. */
497
498extern int flag_no_nonansi_builtin;
499
e1cd6e56
MS
500/* Nonzero means enable obscure ANSI features and disable GNU extensions
501 that might cause ANSI-compliant code to be miscompiled. */
8d08fdba
MS
502
503extern int flag_ansi;
504
505/* Nonzero if we want to support huge (> 2^(sizeof(short)*8-1) bytes)
e92cc029 506 objects. */
8d08fdba
MS
507extern int flag_huge_objects;
508
509/* Nonzero if we want to conserve space in the .o files. We do this
510 by putting uninitialized data and runtime initialized data into
ddd5a7c1 511 .common instead of .data at the expense of not flagging multiple
8d08fdba
MS
512 definitions. */
513extern int flag_conserve_space;
514
515/* Pointers to the base and current top of the language name stack. */
516
517extern tree *current_lang_base, *current_lang_stack;
518\f
51c184be 519/* C and C++ flags are in decl2.c. */
8d08fdba
MS
520
521/* Set to 0 at beginning of a constructor, set to 1
522 if that function does an allocation before referencing its
523 instance variable. */
5566b478 524static int current_function_assigns_this;
8d08fdba
MS
525int current_function_just_assigned_this;
526
527/* Set to 0 at beginning of a function. Set non-zero when
528 store_parm_decls is called. Don't call store_parm_decls
529 if this flag is non-zero! */
530int current_function_parms_stored;
531
51c184be 532/* Flag used when debugging spew.c */
8d08fdba
MS
533
534extern int spew_debug;
535
536/* This is a copy of the class_shadowed list of the previous class binding
537 contour when at global scope. It's used to reset IDENTIFIER_CLASS_VALUEs
538 when entering another class scope (i.e. a cache miss). */
539extern tree previous_class_values;
540
e92cc029
MS
541/* A expression of value 0 with the same precision as a sizetype
542 node, but signed. */
543tree signed_size_zero_node;
544
8d08fdba
MS
545\f
546/* Allocate a level of searching. */
e92cc029 547
824b9a4c 548static
8d08fdba
MS
549struct stack_level *
550push_decl_level (stack, obstack)
551 struct stack_level *stack;
552 struct obstack *obstack;
553{
554 struct stack_level tem;
555 tem.prev = stack;
556
557 return push_stack_level (obstack, (char *)&tem, sizeof (tem));
558}
559\f
560/* For each binding contour we allocate a binding_level structure
e92cc029
MS
561 which records the names defined in that contour.
562 Contours include:
563 0) the global one
564 1) one for each function definition,
565 where internal declarations of the parameters appear.
566 2) one for each compound statement,
567 to record its declarations.
568
569 The current meaning of a name can be found by searching the levels
570 from the current one out to the global one.
571
572 Off to the side, may be the class_binding_level. This exists only
573 to catch class-local declarations. It is otherwise nonexistent.
574
575 Also there may be binding levels that catch cleanups that must be
576 run when exceptions occur. */
8d08fdba
MS
577
578/* Note that the information in the `names' component of the global contour
579 is duplicated in the IDENTIFIER_GLOBAL_VALUEs of all identifiers. */
580
581struct binding_level
582 {
583 /* A chain of _DECL nodes for all variables, constants, functions,
e92cc029
MS
584 and typedef types. These are in the reverse of the order
585 supplied. */
8d08fdba
MS
586 tree names;
587
e92cc029
MS
588 /* A list of structure, union and enum definitions, for looking up
589 tag names.
590 It is a chain of TREE_LIST nodes, each of whose TREE_PURPOSE is a name,
591 or NULL_TREE; and whose TREE_VALUE is a RECORD_TYPE, UNION_TYPE,
592 or ENUMERAL_TYPE node.
593
594 C++: the TREE_VALUE nodes can be simple types for
595 component_bindings. */
8d08fdba
MS
596 tree tags;
597
2c73f9f5
ML
598 /* A list of USING_DECL nodes. */
599 tree usings;
600
8d08fdba
MS
601 /* For each level, a list of shadowed outer-level local definitions
602 to be restored when this level is popped.
603 Each link is a TREE_LIST whose TREE_PURPOSE is an identifier and
604 whose TREE_VALUE is its old definition (a kind of ..._DECL node). */
605 tree shadowed;
606
607 /* Same, for IDENTIFIER_CLASS_VALUE. */
608 tree class_shadowed;
609
610 /* Same, for IDENTIFIER_TYPE_VALUE. */
611 tree type_shadowed;
612
613 /* For each level (except not the global one),
614 a chain of BLOCK nodes for all the levels
615 that were entered and exited one level down. */
616 tree blocks;
617
618 /* The BLOCK node for this level, if one has been preallocated.
619 If 0, the BLOCK is allocated (if needed) when the level is popped. */
620 tree this_block;
621
622 /* The binding level which this one is contained in (inherits from). */
623 struct binding_level *level_chain;
624
f30432d7 625 /* List of decls in `names' that have incomplete
8d08fdba 626 structure or union types. */
f30432d7 627 tree incomplete;
8d08fdba 628
8d6e462b 629 /* List of VAR_DECLS saved from a previous for statement.
abc95ed3 630 These would be dead in ANSI-conforming code, but might
d22c8596 631 be referenced in ARM-era code. */
8d6e462b
PB
632 tree dead_vars_from_for;
633
8d08fdba
MS
634 /* 1 for the level that holds the parameters of a function.
635 2 for the level that holds a class declaration.
636 3 for levels that hold parameter declarations. */
637 unsigned parm_flag : 4;
638
639 /* 1 means make a BLOCK for this level regardless of all else.
640 2 for temporary binding contours created by the compiler. */
641 unsigned keep : 3;
642
643 /* Nonzero if this level "doesn't exist" for tags. */
644 unsigned tag_transparent : 1;
645
646 /* Nonzero if this level can safely have additional
647 cleanup-needing variables added to it. */
648 unsigned more_cleanups_ok : 1;
649 unsigned have_cleanups : 1;
650
5566b478
MS
651 /* Nonzero if this level is for storing the decls for template
652 parameters and generic decls; these decls will be discarded and
653 replaced with a TEMPLATE_DECL. */
8d08fdba
MS
654 unsigned pseudo_global : 1;
655
a9aedbc2
MS
656 /* This is set for a namespace binding level. */
657 unsigned namespace_p : 1;
658
2ee887f2 659 /* True if this level is that of a for-statement where we need to
d22c8596 660 worry about ambiguous (ARM or ANSI) scope rules. */
8d6e462b
PB
661 unsigned is_for_scope : 1;
662
5566b478 663 /* Two bits left for this word. */
8d08fdba
MS
664
665#if defined(DEBUG_CP_BINDING_LEVELS)
666 /* Binding depth at which this level began. */
667 unsigned binding_depth;
668#endif /* defined(DEBUG_CP_BINDING_LEVELS) */
669 };
670
671#define NULL_BINDING_LEVEL ((struct binding_level *) NULL)
672
673/* The (non-class) binding level currently in effect. */
674
675static struct binding_level *current_binding_level;
676
677/* The binding level of the current class, if any. */
678
679static struct binding_level *class_binding_level;
680
681/* The current (class or non-class) binding level currently in effect. */
682
683#define inner_binding_level \
684 (class_binding_level ? class_binding_level : current_binding_level)
685
686/* A chain of binding_level structures awaiting reuse. */
687
688static struct binding_level *free_binding_level;
689
690/* The outermost binding level, for names of file scope.
691 This is created when the compiler is started and exists
692 through the entire run. */
693
694static struct binding_level *global_binding_level;
695
696/* Binding level structures are initialized by copying this one. */
697
698static struct binding_level clear_binding_level;
699
700/* Nonzero means unconditionally make a BLOCK for the next level pushed. */
701
702static int keep_next_level_flag;
703
704#if defined(DEBUG_CP_BINDING_LEVELS)
705static int binding_depth = 0;
706static int is_class_level = 0;
707
708static void
709indent ()
710{
711 register unsigned i;
712
713 for (i = 0; i < binding_depth*2; i++)
714 putc (' ', stderr);
715}
716#endif /* defined(DEBUG_CP_BINDING_LEVELS) */
717
718static tree pushdecl_with_scope PROTO((tree, struct binding_level *));
719
720static void
721push_binding_level (newlevel, tag_transparent, keep)
722 struct binding_level *newlevel;
723 int tag_transparent, keep;
724{
725 /* Add this level to the front of the chain (stack) of levels that
726 are active. */
727 *newlevel = clear_binding_level;
728 if (class_binding_level)
729 {
730 newlevel->level_chain = class_binding_level;
731 class_binding_level = (struct binding_level *)0;
732 }
733 else
734 {
735 newlevel->level_chain = current_binding_level;
736 }
737 current_binding_level = newlevel;
738 newlevel->tag_transparent = tag_transparent;
739 newlevel->more_cleanups_ok = 1;
8d08fdba
MS
740 newlevel->keep = keep;
741#if defined(DEBUG_CP_BINDING_LEVELS)
742 newlevel->binding_depth = binding_depth;
743 indent ();
744 fprintf (stderr, "push %s level 0x%08x line %d\n",
745 (is_class_level) ? "class" : "block", newlevel, lineno);
746 is_class_level = 0;
747 binding_depth++;
748#endif /* defined(DEBUG_CP_BINDING_LEVELS) */
749}
750
751static void
752pop_binding_level ()
753{
754 if (class_binding_level)
755 current_binding_level = class_binding_level;
756
757 if (global_binding_level)
758 {
2c73f9f5 759 /* Cannot pop a level, if there are none left to pop. */
8d08fdba
MS
760 if (current_binding_level == global_binding_level)
761 my_friendly_abort (123);
762 }
763 /* Pop the current level, and free the structure for reuse. */
764#if defined(DEBUG_CP_BINDING_LEVELS)
765 binding_depth--;
766 indent ();
767 fprintf (stderr, "pop %s level 0x%08x line %d\n",
768 (is_class_level) ? "class" : "block",
769 current_binding_level, lineno);
770 if (is_class_level != (current_binding_level == class_binding_level))
9e9ff709
MS
771 {
772 indent ();
773 fprintf (stderr, "XXX is_class_level != (current_binding_level == class_binding_level)\n");
774 }
8d08fdba
MS
775 is_class_level = 0;
776#endif /* defined(DEBUG_CP_BINDING_LEVELS) */
777 {
778 register struct binding_level *level = current_binding_level;
779 current_binding_level = current_binding_level->level_chain;
780 level->level_chain = free_binding_level;
781#if 0 /* defined(DEBUG_CP_BINDING_LEVELS) */
782 if (level->binding_depth != binding_depth)
783 abort ();
784#endif /* defined(DEBUG_CP_BINDING_LEVELS) */
785 free_binding_level = level;
786
787 class_binding_level = current_binding_level;
788 if (class_binding_level->parm_flag != 2)
789 class_binding_level = 0;
790 while (current_binding_level->parm_flag == 2)
791 current_binding_level = current_binding_level->level_chain;
792 }
793}
a9aedbc2
MS
794
795static void
796suspend_binding_level ()
797{
798 if (class_binding_level)
799 current_binding_level = class_binding_level;
800
801 if (global_binding_level)
802 {
2c73f9f5 803 /* Cannot suspend a level, if there are none left to suspend. */
a9aedbc2
MS
804 if (current_binding_level == global_binding_level)
805 my_friendly_abort (123);
806 }
807 /* Suspend the current level. */
808#if defined(DEBUG_CP_BINDING_LEVELS)
809 binding_depth--;
810 indent ();
811 fprintf (stderr, "suspend %s level 0x%08x line %d\n",
812 (is_class_level) ? "class" : "block",
813 current_binding_level, lineno);
814 if (is_class_level != (current_binding_level == class_binding_level))
9e9ff709
MS
815 {
816 indent ();
817 fprintf (stderr, "XXX is_class_level != (current_binding_level == class_binding_level)\n");
818 }
a9aedbc2
MS
819 is_class_level = 0;
820#endif /* defined(DEBUG_CP_BINDING_LEVELS) */
821 {
a9aedbc2 822 current_binding_level = current_binding_level->level_chain;
a9aedbc2
MS
823 class_binding_level = current_binding_level;
824 if (class_binding_level->parm_flag != 2)
825 class_binding_level = 0;
826 while (current_binding_level->parm_flag == 2)
827 current_binding_level = current_binding_level->level_chain;
828 }
829}
830
824b9a4c 831static void
a9aedbc2
MS
832resume_binding_level (b)
833 struct binding_level *b;
834{
2c73f9f5
ML
835 /* Resuming binding levels is meant only for namespaces,
836 and those cannot nest into classes. */
837 my_friendly_assert(!class_binding_level, 386);
838 /* Also, resuming a non-directly nested namespace is a no-no. */
839 my_friendly_assert(b->level_chain == current_binding_level, 386);
a9aedbc2
MS
840 current_binding_level = b;
841#if defined(DEBUG_CP_BINDING_LEVELS)
842 b->binding_depth = binding_depth;
843 indent ();
844 fprintf (stderr, "resume %s level 0x%08x line %d\n",
845 (is_class_level) ? "class" : "block", b, lineno);
846 is_class_level = 0;
847 binding_depth++;
848#endif /* defined(DEBUG_CP_BINDING_LEVELS) */
849}
8d08fdba 850\f
cffa8729
MS
851/* Create a new `struct binding_level'. */
852
853static
854struct binding_level *
855make_binding_level ()
856{
857 /* NOSTRICT */
858 return (struct binding_level *) xmalloc (sizeof (struct binding_level));
859}
860
8d08fdba
MS
861/* Nonzero if we are currently in the global binding level. */
862
863int
864global_bindings_p ()
865{
866 return current_binding_level == global_binding_level;
867}
868
a9aedbc2
MS
869/* Nonzero if we are currently in a toplevel binding level. This
870 means either the global binding level or a namespace in a toplevel
2c73f9f5
ML
871 binding level.
872 Since there are no non-toplevel namespace levels, this really
873 means any namespace or pseudo-global level. */
a9aedbc2
MS
874
875int
876toplevel_bindings_p ()
877{
2c73f9f5
ML
878 return current_binding_level->namespace_p
879 || current_binding_level->pseudo_global;
a9aedbc2
MS
880}
881
882/* Nonzero if this is a namespace scope. */
883
824b9a4c 884static int
a9aedbc2
MS
885namespace_bindings_p ()
886{
887 return current_binding_level->namespace_p;
888}
889
8d08fdba
MS
890void
891keep_next_level ()
892{
893 keep_next_level_flag = 1;
894}
895
896/* Nonzero if the current level needs to have a BLOCK made. */
897
898int
899kept_level_p ()
900{
901 return (current_binding_level->blocks != NULL_TREE
902 || current_binding_level->keep
903 || current_binding_level->names != NULL_TREE
904 || (current_binding_level->tags != NULL_TREE
905 && !current_binding_level->tag_transparent));
906}
907
908/* Identify this binding level as a level of parameters. */
909
910void
911declare_parm_level ()
912{
913 current_binding_level->parm_flag = 1;
914}
915
8d08fdba
MS
916void
917declare_pseudo_global_level ()
918{
919 current_binding_level->pseudo_global = 1;
920}
921
824b9a4c 922static void
a9aedbc2
MS
923declare_namespace_level ()
924{
925 current_binding_level->namespace_p = 1;
926}
927
8d08fdba
MS
928int
929pseudo_global_level_p ()
930{
931 return current_binding_level->pseudo_global;
932}
933
934void
935set_class_shadows (shadows)
936 tree shadows;
937{
938 class_binding_level->class_shadowed = shadows;
939}
940
941/* Enter a new binding level.
942 If TAG_TRANSPARENT is nonzero, do so only for the name space of variables,
943 not for that of tags. */
944
945void
946pushlevel (tag_transparent)
947 int tag_transparent;
948{
949 register struct binding_level *newlevel = NULL_BINDING_LEVEL;
950
951 /* If this is the top level of a function,
952 just make sure that NAMED_LABELS is 0.
953 They should have been set to 0 at the end of the previous function. */
954
955 if (current_binding_level == global_binding_level)
956 my_friendly_assert (named_labels == NULL_TREE, 134);
957
958 /* Reuse or create a struct for this binding level. */
959
960#if defined(DEBUG_CP_BINDING_LEVELS)
961 if (0)
962#else /* !defined(DEBUG_CP_BINDING_LEVELS) */
963 if (free_binding_level)
964#endif /* !defined(DEBUG_CP_BINDING_LEVELS) */
965 {
966 newlevel = free_binding_level;
967 free_binding_level = free_binding_level->level_chain;
968 }
969 else
970 {
cffa8729 971 newlevel = make_binding_level ();
8d08fdba 972 }
cffa8729 973
8d08fdba
MS
974 push_binding_level (newlevel, tag_transparent, keep_next_level_flag);
975 GNU_xref_start_scope ((HOST_WIDE_INT) newlevel);
976 keep_next_level_flag = 0;
977}
978
5566b478 979void
8d6e462b
PB
980note_level_for_for ()
981{
982 current_binding_level->is_for_scope = 1;
983}
984
8d08fdba
MS
985void
986pushlevel_temporary (tag_transparent)
987 int tag_transparent;
988{
989 pushlevel (tag_transparent);
990 current_binding_level->keep = 2;
991 clear_last_expr ();
992
993 /* Note we don't call push_momentary() here. Otherwise, it would cause
994 cleanups to be allocated on the momentary obstack, and they will be
995 overwritten by the next statement. */
996
997 expand_start_bindings (0);
998}
999
1000/* Exit a binding level.
1001 Pop the level off, and restore the state of the identifier-decl mappings
1002 that were in effect when this level was entered.
1003
1004 If KEEP == 1, this level had explicit declarations, so
1005 and create a "block" (a BLOCK node) for the level
1006 to record its declarations and subblocks for symbol table output.
1007
1008 If KEEP == 2, this level's subblocks go to the front,
1009 not the back of the current binding level. This happens,
1010 for instance, when code for constructors and destructors
1011 need to generate code at the end of a function which must
1012 be moved up to the front of the function.
1013
1014 If FUNCTIONBODY is nonzero, this level is the body of a function,
1015 so create a block as if KEEP were set and also clear out all
1016 label names.
1017
1018 If REVERSE is nonzero, reverse the order of decls before putting
1019 them into the BLOCK. */
1020
1021tree
1022poplevel (keep, reverse, functionbody)
1023 int keep;
1024 int reverse;
1025 int functionbody;
1026{
1027 register tree link;
1028 /* The chain of decls was accumulated in reverse order.
1029 Put it into forward order, just for cleanliness. */
1030 tree decls;
1031 int tmp = functionbody;
8d08fdba
MS
1032 int real_functionbody = current_binding_level->keep == 2
1033 ? ((functionbody = 0), tmp) : functionbody;
1034 tree tags = functionbody >= 0 ? current_binding_level->tags : 0;
1035 tree subblocks = functionbody >= 0 ? current_binding_level->blocks : 0;
1036 tree block = NULL_TREE;
1037 tree decl;
1038 int block_previously_created;
1039
1040 GNU_xref_end_scope ((HOST_WIDE_INT) current_binding_level,
1041 (HOST_WIDE_INT) current_binding_level->level_chain,
1042 current_binding_level->parm_flag,
5566b478 1043 current_binding_level->keep);
8d08fdba
MS
1044
1045 if (current_binding_level->keep == 1)
1046 keep = 1;
1047
8d08fdba
MS
1048 /* Get the decls in the order they were written.
1049 Usually current_binding_level->names is in reverse order.
1050 But parameter decls were previously put in forward order. */
1051
1052 if (reverse)
1053 current_binding_level->names
1054 = decls = nreverse (current_binding_level->names);
1055 else
1056 decls = current_binding_level->names;
1057
1058 /* Output any nested inline functions within this block
1059 if they weren't already output. */
1060
1061 for (decl = decls; decl; decl = TREE_CHAIN (decl))
1062 if (TREE_CODE (decl) == FUNCTION_DECL
1063 && ! TREE_ASM_WRITTEN (decl)
1064 && DECL_INITIAL (decl) != NULL_TREE
6060a796
MS
1065 && TREE_ADDRESSABLE (decl)
1066 && decl_function_context (decl) == current_function_decl)
8d08fdba
MS
1067 {
1068 /* If this decl was copied from a file-scope decl
1069 on account of a block-scope extern decl,
1070 propagate TREE_ADDRESSABLE to the file-scope decl. */
1071 if (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE)
1072 TREE_ADDRESSABLE (DECL_ABSTRACT_ORIGIN (decl)) = 1;
1073 else
1074 {
1075 push_function_context ();
1076 output_inline_function (decl);
1077 pop_function_context ();
1078 }
1079 }
1080
1081 /* If there were any declarations or structure tags in that level,
1082 or if this level is a function body,
1083 create a BLOCK to record them for the life of this function. */
1084
1085 block = NULL_TREE;
1086 block_previously_created = (current_binding_level->this_block != NULL_TREE);
1087 if (block_previously_created)
1088 block = current_binding_level->this_block;
1089 else if (keep == 1 || functionbody)
1090 block = make_node (BLOCK);
1091 if (block != NULL_TREE)
1092 {
72b7eeff
MS
1093 if (block_previously_created)
1094 {
1095 if (decls || tags || subblocks)
1096 {
be99da77 1097 if (BLOCK_VARS (block) || BLOCK_TYPE_TAGS (block))
72b7eeff
MS
1098 {
1099 warning ("internal compiler error: debugging info corrupted");
1100 }
1101 BLOCK_VARS (block) = decls;
1102 BLOCK_TYPE_TAGS (block) = tags;
be99da77
MS
1103
1104 /* We can have previous subblocks and new subblocks when
1105 doing fixup_gotos with complex cleanups. We chain the new
1106 subblocks onto the end of any pre-existing subblocks. */
1107 BLOCK_SUBBLOCKS (block) = chainon (BLOCK_SUBBLOCKS (block),
1108 subblocks);
72b7eeff 1109 }
be99da77
MS
1110 /* If we created the block earlier on, and we are just
1111 diddling it now, then it already should have a proper
1112 BLOCK_END_NOTE value associated with it. */
72b7eeff
MS
1113 }
1114 else
1115 {
1116 BLOCK_VARS (block) = decls;
1117 BLOCK_TYPE_TAGS (block) = tags;
1118 BLOCK_SUBBLOCKS (block) = subblocks;
1119 /* Otherwise, for a new block, install a new BLOCK_END_NOTE value. */
1120 remember_end_note (block);
1121 }
8d08fdba
MS
1122 }
1123
1124 /* In each subblock, record that this is its superior. */
1125
1126 if (keep >= 0)
1127 for (link = subblocks; link; link = TREE_CHAIN (link))
1128 BLOCK_SUPERCONTEXT (link) = block;
1129
1130 /* Clear out the meanings of the local variables of this level. */
1131
8d6e462b
PB
1132 if (current_binding_level->is_for_scope && flag_new_for_scope == 1)
1133 {
2ee887f2 1134 struct binding_level *outer = current_binding_level->level_chain;
8d6e462b
PB
1135 for (link = decls; link; link = TREE_CHAIN (link))
1136 {
1137 if (TREE_CODE (link) == VAR_DECL)
1138 DECL_DEAD_FOR_LOCAL (link) = 1;
e76a2646
MS
1139 else
1140 IDENTIFIER_LOCAL_VALUE (DECL_NAME (link)) = NULL_TREE;
8d6e462b 1141 }
2ee887f2
MS
1142
1143 /* Save declarations made in a 'for' statement so we can support pre-ANSI
e92cc029 1144 'for' scoping semantics. */
2ee887f2
MS
1145
1146 for (link = current_binding_level->shadowed; link; link = TREE_CHAIN (link))
1147 {
1148 tree id = TREE_PURPOSE (link);
1149 tree decl = IDENTIFIER_LOCAL_VALUE (id);
1150
e76a2646
MS
1151 if (decl && DECL_DEAD_FOR_LOCAL (decl))
1152 {
1153 /* In this case keep the dead for-decl visible,
e92cc029 1154 but remember what (if anything) it shadowed. */
e76a2646
MS
1155 DECL_SHADOWED_FOR_VAR (decl) = TREE_VALUE (link);
1156 TREE_CHAIN (decl) = outer->dead_vars_from_for;
1157 outer->dead_vars_from_for = decl;
1158 }
1159 else
1160 IDENTIFIER_LOCAL_VALUE (id) = TREE_VALUE (link);
2ee887f2 1161 }
8d6e462b 1162 }
e92cc029 1163 else /* Not special for scope. */
8d6e462b
PB
1164 {
1165 for (link = decls; link; link = TREE_CHAIN (link))
1166 {
1167 if (DECL_NAME (link) != NULL_TREE)
8d08fdba 1168 {
8d6e462b
PB
1169 /* If the ident. was used or addressed via a local extern decl,
1170 don't forget that fact. */
1171 if (DECL_EXTERNAL (link))
1172 {
1173 if (TREE_USED (link))
1174 TREE_USED (DECL_ASSEMBLER_NAME (link)) = 1;
1175 if (TREE_ADDRESSABLE (link))
1176 TREE_ADDRESSABLE (DECL_ASSEMBLER_NAME (link)) = 1;
1177 }
1178 IDENTIFIER_LOCAL_VALUE (DECL_NAME (link)) = NULL_TREE;
8d08fdba 1179 }
8d08fdba 1180 }
8d08fdba 1181
2ee887f2
MS
1182 /* Restore all name-meanings of the outer levels
1183 that were shadowed by this level. */
8d08fdba 1184
2ee887f2
MS
1185 for (link = current_binding_level->shadowed;
1186 link; link = TREE_CHAIN (link))
1187 IDENTIFIER_LOCAL_VALUE (TREE_PURPOSE (link)) = TREE_VALUE (link);
1188
1189 /* We first restore the regular decls and *then* the dead_vars_from_for
1190 to handle this case:
1191
1192 int i; // i#1
1193 {
1194 for (int i; ; ) { ...} // i#2
1195 int i; // i#3
1196 } // we are here
1197
1198 In this case, we want remove the binding for i#3, restoring
1199 that of i#2. Then we want to remove the binding for i#2,
e92cc029 1200 and restore that of i#1. */
2ee887f2
MS
1201
1202 link = current_binding_level->dead_vars_from_for;
1203 for (; link != NULL_TREE; link = TREE_CHAIN (link))
8d6e462b 1204 {
2ee887f2
MS
1205 tree id = DECL_NAME (link);
1206 if (IDENTIFIER_LOCAL_VALUE (id) == link)
1207 IDENTIFIER_LOCAL_VALUE (id) = DECL_SHADOWED_FOR_VAR (link);
8d6e462b
PB
1208 }
1209
2ee887f2
MS
1210 for (link = current_binding_level->class_shadowed;
1211 link; link = TREE_CHAIN (link))
1212 IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (link)) = TREE_VALUE (link);
1213 for (link = current_binding_level->type_shadowed;
1214 link; link = TREE_CHAIN (link))
2c73f9f5 1215 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (link), TREE_VALUE (link));
8d6e462b 1216 }
8d08fdba
MS
1217
1218 /* If the level being exited is the top level of a function,
1219 check over all the labels. */
1220
1221 if (functionbody)
1222 {
1223 /* If this is the top level block of a function,
1224 the vars are the function's parameters.
1225 Don't leave them in the BLOCK because they are
1226 found in the FUNCTION_DECL instead. */
1227
1228 BLOCK_VARS (block) = 0;
1229
1230 /* Clear out the definitions of all label names,
1231 since their scopes end here. */
1232
1233 for (link = named_labels; link; link = TREE_CHAIN (link))
1234 {
1235 register tree label = TREE_VALUE (link);
1236
1237 if (DECL_INITIAL (label) == NULL_TREE)
1238 {
1239 cp_error_at ("label `%D' used but not defined", label);
1240 /* Avoid crashing later. */
1241 define_label (input_filename, 1, DECL_NAME (label));
1242 }
1243 else if (warn_unused && !TREE_USED (label))
1244 cp_warning_at ("label `%D' defined but not used", label);
1245 SET_IDENTIFIER_LABEL_VALUE (DECL_NAME (label), NULL_TREE);
1246
1247 /* Put the labels into the "variables" of the
1248 top-level block, so debugger can see them. */
1249 TREE_CHAIN (label) = BLOCK_VARS (block);
1250 BLOCK_VARS (block) = label;
1251 }
1252
1253 named_labels = NULL_TREE;
1254 }
1255
1256 /* Any uses of undefined labels now operate under constraints
1257 of next binding contour. */
1258 {
1259 struct binding_level *level_chain;
1260 level_chain = current_binding_level->level_chain;
1261 if (level_chain)
1262 {
e349ee73
MS
1263 struct named_label_list *labels;
1264 for (labels = named_label_uses; labels; labels = labels->next)
1265 if (labels->binding_level == current_binding_level)
8d08fdba 1266 {
e349ee73
MS
1267 labels->binding_level = level_chain;
1268 labels->names_in_scope = level_chain->names;
8d08fdba
MS
1269 }
1270 }
1271 }
1272
1273 tmp = current_binding_level->keep;
1274
1275 pop_binding_level ();
1276 if (functionbody)
1277 DECL_INITIAL (current_function_decl) = block;
1278 else if (block)
1279 {
1280 if (!block_previously_created)
1281 current_binding_level->blocks
1282 = chainon (current_binding_level->blocks, block);
1283 }
1284 /* If we did not make a block for the level just exited,
1285 any blocks made for inner levels
1286 (since they cannot be recorded as subblocks in that level)
1287 must be carried forward so they will later become subblocks
1288 of something else. */
1289 else if (subblocks)
1290 {
1291 if (keep == 2)
1292 current_binding_level->blocks
1293 = chainon (subblocks, current_binding_level->blocks);
1294 else
1295 current_binding_level->blocks
1296 = chainon (current_binding_level->blocks, subblocks);
1297 }
1298
1299 /* Take care of compiler's internal binding structures. */
a4443a08 1300 if (tmp == 2)
8d08fdba 1301 {
8d08fdba
MS
1302 expand_end_bindings (getdecls (), keep, 1);
1303 /* Each and every BLOCK node created here in `poplevel' is important
1304 (e.g. for proper debugging information) so if we created one
1305 earlier, mark it as "used". */
1306 if (block)
1307 TREE_USED (block) = 1;
1308 block = poplevel (keep, reverse, real_functionbody);
1309 }
1310
1311 /* Each and every BLOCK node created here in `poplevel' is important
1312 (e.g. for proper debugging information) so if we created one
1313 earlier, mark it as "used". */
1314 if (block)
1315 TREE_USED (block) = 1;
1316 return block;
1317}
1318
1319/* Delete the node BLOCK from the current binding level.
1320 This is used for the block inside a stmt expr ({...})
1321 so that the block can be reinserted where appropriate. */
1322
1323void
1324delete_block (block)
1325 tree block;
1326{
1327 tree t;
1328 if (current_binding_level->blocks == block)
1329 current_binding_level->blocks = TREE_CHAIN (block);
1330 for (t = current_binding_level->blocks; t;)
1331 {
1332 if (TREE_CHAIN (t) == block)
1333 TREE_CHAIN (t) = TREE_CHAIN (block);
1334 else
1335 t = TREE_CHAIN (t);
1336 }
1337 TREE_CHAIN (block) = NULL_TREE;
1338 /* Clear TREE_USED which is always set by poplevel.
1339 The flag is set again if insert_block is called. */
1340 TREE_USED (block) = 0;
1341}
1342
1343/* Insert BLOCK at the end of the list of subblocks of the
1344 current binding level. This is used when a BIND_EXPR is expanded,
1345 to handle the BLOCK node inside the BIND_EXPR. */
1346
1347void
1348insert_block (block)
1349 tree block;
1350{
1351 TREE_USED (block) = 1;
1352 current_binding_level->blocks
1353 = chainon (current_binding_level->blocks, block);
1354}
1355
8d08fdba
MS
1356/* Set the BLOCK node for the innermost scope
1357 (the one we are currently in). */
1358
1359void
1360set_block (block)
1361 register tree block;
1362{
1363 current_binding_level->this_block = block;
1364}
1365
1366/* Do a pushlevel for class declarations. */
e92cc029 1367
8d08fdba
MS
1368void
1369pushlevel_class ()
1370{
1371 register struct binding_level *newlevel;
1372
1373 /* Reuse or create a struct for this binding level. */
1374#if defined(DEBUG_CP_BINDING_LEVELS)
1375 if (0)
1376#else /* !defined(DEBUG_CP_BINDING_LEVELS) */
1377 if (free_binding_level)
1378#endif /* !defined(DEBUG_CP_BINDING_LEVELS) */
1379 {
1380 newlevel = free_binding_level;
1381 free_binding_level = free_binding_level->level_chain;
1382 }
1383 else
1384 {
cffa8729 1385 newlevel = make_binding_level ();
8d08fdba
MS
1386 }
1387
1388#if defined(DEBUG_CP_BINDING_LEVELS)
1389 is_class_level = 1;
1390#endif /* defined(DEBUG_CP_BINDING_LEVELS) */
1391
1392 push_binding_level (newlevel, 0, 0);
1393
1394 decl_stack = push_decl_level (decl_stack, &decl_obstack);
1395 class_binding_level = current_binding_level;
1396 class_binding_level->parm_flag = 2;
1397 /* We have just pushed into a new binding level. Now, fake out the rest
1398 of the compiler. Set the `current_binding_level' back to point to
1399 the most closely containing non-class binding level. */
1400 do
1401 {
1402 current_binding_level = current_binding_level->level_chain;
1403 }
1404 while (current_binding_level->parm_flag == 2);
1405}
1406
700f8a87
MS
1407/* ...and a poplevel for class declarations. FORCE is used to force
1408 clearing out of CLASS_VALUEs after a class definition. */
e92cc029 1409
8d08fdba 1410tree
700f8a87
MS
1411poplevel_class (force)
1412 int force;
8d08fdba
MS
1413{
1414 register struct binding_level *level = class_binding_level;
1415 tree block = NULL_TREE;
1416 tree shadowed;
1417
1418 my_friendly_assert (level != 0, 354);
1419
1420 decl_stack = pop_stack_level (decl_stack);
1421 for (shadowed = level->shadowed; shadowed; shadowed = TREE_CHAIN (shadowed))
1422 IDENTIFIER_LOCAL_VALUE (TREE_PURPOSE (shadowed)) = TREE_VALUE (shadowed);
1423 /* If we're leaving a toplevel class, don't bother to do the setting
ddd5a7c1 1424 of IDENTIFIER_CLASS_VALUE to NULL_TREE, since first of all this slot
8d08fdba 1425 shouldn't even be used when current_class_type isn't set, and second,
ddd5a7c1 1426 if we don't touch it here, we're able to use the cache effect if the
8d08fdba 1427 next time we're entering a class scope, it is the same class. */
700f8a87 1428 if (current_class_depth != 1 || force)
8d08fdba
MS
1429 for (shadowed = level->class_shadowed;
1430 shadowed;
1431 shadowed = TREE_CHAIN (shadowed))
1432 IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (shadowed)) = TREE_VALUE (shadowed);
1433 else
1434 /* Remember to save what IDENTIFIER's were bound in this scope so we
1435 can recover from cache misses. */
e76a2646
MS
1436 {
1437 previous_class_type = current_class_type;
1438 previous_class_values = class_binding_level->class_shadowed;
1439 }
8d08fdba
MS
1440 for (shadowed = level->type_shadowed;
1441 shadowed;
1442 shadowed = TREE_CHAIN (shadowed))
2c73f9f5 1443 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (shadowed), TREE_VALUE (shadowed));
8d08fdba
MS
1444
1445 GNU_xref_end_scope ((HOST_WIDE_INT) class_binding_level,
1446 (HOST_WIDE_INT) class_binding_level->level_chain,
1447 class_binding_level->parm_flag,
5566b478 1448 class_binding_level->keep);
8d08fdba
MS
1449
1450 if (class_binding_level->parm_flag != 2)
1451 class_binding_level = (struct binding_level *)0;
1452
38e01259 1453 /* Now, pop out of the binding level which we created up in the
8d08fdba
MS
1454 `pushlevel_class' routine. */
1455#if defined(DEBUG_CP_BINDING_LEVELS)
1456 is_class_level = 1;
1457#endif /* defined(DEBUG_CP_BINDING_LEVELS) */
1458
1459 pop_binding_level ();
1460
1461 return block;
1462}
1463\f
1464/* For debugging. */
5566b478
MS
1465static int no_print_functions = 0;
1466static int no_print_builtins = 0;
8d08fdba
MS
1467
1468void
1469print_binding_level (lvl)
1470 struct binding_level *lvl;
1471{
1472 tree t;
1473 int i = 0, len;
1474 fprintf (stderr, " blocks=");
1475 fprintf (stderr, HOST_PTR_PRINTF, lvl->blocks);
1476 fprintf (stderr, " n_incomplete=%d parm_flag=%d keep=%d",
f30432d7 1477 list_length (lvl->incomplete), lvl->parm_flag, lvl->keep);
8d08fdba
MS
1478 if (lvl->tag_transparent)
1479 fprintf (stderr, " tag-transparent");
1480 if (lvl->more_cleanups_ok)
1481 fprintf (stderr, " more-cleanups-ok");
1482 if (lvl->have_cleanups)
1483 fprintf (stderr, " have-cleanups");
8d08fdba
MS
1484 fprintf (stderr, "\n");
1485 if (lvl->names)
1486 {
1487 fprintf (stderr, " names:\t");
1488 /* We can probably fit 3 names to a line? */
1489 for (t = lvl->names; t; t = TREE_CHAIN (t))
1490 {
fc378698 1491 if (no_print_functions && (TREE_CODE (t) == FUNCTION_DECL))
8d08fdba
MS
1492 continue;
1493 if (no_print_builtins
fc378698
MS
1494 && (TREE_CODE (t) == TYPE_DECL)
1495 && (!strcmp (DECL_SOURCE_FILE (t),"<built-in>")))
8d08fdba
MS
1496 continue;
1497
1498 /* Function decls tend to have longer names. */
1499 if (TREE_CODE (t) == FUNCTION_DECL)
1500 len = 3;
1501 else
1502 len = 2;
1503 i += len;
1504 if (i > 6)
1505 {
1506 fprintf (stderr, "\n\t");
1507 i = len;
1508 }
1509 print_node_brief (stderr, "", t, 0);
bd6dd845 1510 if (t == error_mark_node)
8d08fdba
MS
1511 break;
1512 }
1513 if (i)
1514 fprintf (stderr, "\n");
1515 }
1516 if (lvl->tags)
1517 {
1518 fprintf (stderr, " tags:\t");
1519 i = 0;
1520 for (t = lvl->tags; t; t = TREE_CHAIN (t))
1521 {
1522 if (TREE_PURPOSE (t) == NULL_TREE)
1523 len = 3;
1524 else if (TREE_PURPOSE (t) == TYPE_IDENTIFIER (TREE_VALUE (t)))
1525 len = 2;
1526 else
1527 len = 4;
1528 i += len;
1529 if (i > 5)
1530 {
1531 fprintf (stderr, "\n\t");
1532 i = len;
1533 }
1534 if (TREE_PURPOSE (t) == NULL_TREE)
1535 {
1536 print_node_brief (stderr, "<unnamed-typedef", TREE_VALUE (t), 0);
1537 fprintf (stderr, ">");
1538 }
1539 else if (TREE_PURPOSE (t) == TYPE_IDENTIFIER (TREE_VALUE (t)))
1540 print_node_brief (stderr, "", TREE_VALUE (t), 0);
1541 else
1542 {
1543 print_node_brief (stderr, "<typedef", TREE_PURPOSE (t), 0);
1544 print_node_brief (stderr, "", TREE_VALUE (t), 0);
1545 fprintf (stderr, ">");
1546 }
1547 }
1548 if (i)
1549 fprintf (stderr, "\n");
1550 }
1551 if (lvl->shadowed)
1552 {
1553 fprintf (stderr, " shadowed:");
1554 for (t = lvl->shadowed; t; t = TREE_CHAIN (t))
1555 {
1556 fprintf (stderr, " %s ", IDENTIFIER_POINTER (TREE_PURPOSE (t)));
1557 }
1558 fprintf (stderr, "\n");
1559 }
1560 if (lvl->class_shadowed)
1561 {
1562 fprintf (stderr, " class-shadowed:");
1563 for (t = lvl->class_shadowed; t; t = TREE_CHAIN (t))
1564 {
1565 fprintf (stderr, " %s ", IDENTIFIER_POINTER (TREE_PURPOSE (t)));
1566 }
1567 fprintf (stderr, "\n");
1568 }
1569 if (lvl->type_shadowed)
1570 {
1571 fprintf (stderr, " type-shadowed:");
1572 for (t = lvl->type_shadowed; t; t = TREE_CHAIN (t))
1573 {
8d08fdba 1574 fprintf (stderr, " %s ", IDENTIFIER_POINTER (TREE_PURPOSE (t)));
8d08fdba
MS
1575 }
1576 fprintf (stderr, "\n");
1577 }
1578}
1579
1580void
1581print_other_binding_stack (stack)
1582 struct binding_level *stack;
1583{
1584 struct binding_level *level;
1585 for (level = stack; level != global_binding_level; level = level->level_chain)
1586 {
1587 fprintf (stderr, "binding level ");
1588 fprintf (stderr, HOST_PTR_PRINTF, level);
1589 fprintf (stderr, "\n");
1590 print_binding_level (level);
1591 }
1592}
1593
1594void
1595print_binding_stack ()
1596{
1597 struct binding_level *b;
1598 fprintf (stderr, "current_binding_level=");
1599 fprintf (stderr, HOST_PTR_PRINTF, current_binding_level);
1600 fprintf (stderr, "\nclass_binding_level=");
1601 fprintf (stderr, HOST_PTR_PRINTF, class_binding_level);
1602 fprintf (stderr, "\nglobal_binding_level=");
1603 fprintf (stderr, HOST_PTR_PRINTF, global_binding_level);
1604 fprintf (stderr, "\n");
1605 if (class_binding_level)
1606 {
1607 for (b = class_binding_level; b; b = b->level_chain)
1608 if (b == current_binding_level)
1609 break;
1610 if (b)
1611 b = class_binding_level;
1612 else
1613 b = current_binding_level;
1614 }
1615 else
1616 b = current_binding_level;
1617 print_other_binding_stack (b);
1618 fprintf (stderr, "global:\n");
1619 print_binding_level (global_binding_level);
1620}
a9aedbc2 1621
2c73f9f5
ML
1622/* Namespace binding access routines: The namespace_bindings field of
1623 the identifier is polymorphic, with three possible values:
1624 NULL_TREE, a list of CPLUS_BINDINGS, or any other tree_node
1625 indicating the BINDING_VALUE of global_namespace. */
30394414 1626
2c73f9f5
ML
1627/* Check whether the a binding for the name to scope is known.
1628 Assumes that the bindings of the name are already a list
1629 of bindings. Returns the binding found, or NULL_TREE. */
1630
1631static tree
1632find_binding (name, scope)
30394414
JM
1633 tree name;
1634 tree scope;
1635{
2c73f9f5 1636 tree iter, prev = NULL_TREE;
30394414
JM
1637 for (iter = IDENTIFIER_NAMESPACE_BINDINGS (name); iter;
1638 iter = TREE_CHAIN (iter))
1639 {
1640 my_friendly_assert (TREE_CODE (iter) == CPLUS_BINDING, 374);
1641 if (BINDING_SCOPE (iter) == scope)
2c73f9f5
ML
1642 {
1643 /* Move binding found to the fron of the list, so
1644 subsequent lookups will find it faster. */
1645 if (prev)
1646 {
1647 TREE_CHAIN (prev) = TREE_CHAIN (iter);
1648 TREE_CHAIN (iter) = IDENTIFIER_NAMESPACE_BINDINGS (name);
1649 IDENTIFIER_NAMESPACE_BINDINGS (name) = iter;
1650 }
1651 return iter;
1652 }
1653 prev = iter;
30394414 1654 }
2c73f9f5
ML
1655 return NULL_TREE;
1656}
1657
1658/* Always returns a binding for name in scope. If the
1659 namespace_bindings is not a list, convert it to one first.
1660 If no binding is found, make a new one. */
1661
1662tree
1663binding_for_name (name, scope)
1664 tree name;
1665 tree scope;
1666{
1667 tree b = IDENTIFIER_NAMESPACE_BINDINGS (name);
1668 tree result;
1669 if (b && TREE_CODE (b) != CPLUS_BINDING)
1670 {
1671 /* Get rid of optimization for global scope. */
1672 IDENTIFIER_NAMESPACE_BINDINGS (name) = NULL_TREE;
1673 BINDING_VALUE (binding_for_name (name, global_namespace)) = b;
1674 b = IDENTIFIER_NAMESPACE_BINDINGS (name);
1675 }
1676 if (b && (result = find_binding (name, scope)))
1677 return result;
1678 /* Not found, make a new permanent one. */
30394414 1679 push_obstacks (&permanent_obstack, &permanent_obstack);
2c73f9f5
ML
1680 result = make_node (CPLUS_BINDING);
1681 TREE_CHAIN (result) = b;
1682 IDENTIFIER_NAMESPACE_BINDINGS (name) = result;
1683 BINDING_SCOPE (result) = scope;
1684 BINDING_TYPE (result) = NULL_TREE;
1685 BINDING_VALUE (result) = NULL_TREE;
30394414 1686 pop_obstacks ();
2c73f9f5
ML
1687 return result;
1688}
1689
1690/* Return the binding value for name in scope, considering that
1691 namespace_binding may or may not be a list of CPLUS_BINDINGS. */
1692
1693tree
1694namespace_binding (name, scope)
1695 tree name;
1696 tree scope;
1697{
1698 tree b = IDENTIFIER_NAMESPACE_BINDINGS (name);
1699 if (b == NULL_TREE)
1700 return NULL_TREE;
1701 if (TREE_CODE (b) != CPLUS_BINDING)
1702 return (scope == global_namespace) ? b : NULL_TREE;
1703 name = find_binding (name,scope);
1704 if (name == NULL_TREE)
1705 return name;
1706 return BINDING_VALUE (name);
1707}
1708
1709/* Set the binding value for name in scope. If modifying the binding
1710 of global_namespace is attempted, try to optimize it. */
1711
1712void
1713set_namespace_binding (name, scope, val)
1714 tree name;
1715 tree scope;
1716 tree val;
1717{
1718 tree b;
1719 if (scope == global_namespace)
1720 {
1721 b = IDENTIFIER_NAMESPACE_BINDINGS (name);
1722 if (b == NULL_TREE || TREE_CODE (b) != CPLUS_BINDING)
1723 {
1724 IDENTIFIER_NAMESPACE_BINDINGS (name) = val;
1725 return;
1726 }
1727 }
1728 b = binding_for_name (name, scope);
1729 BINDING_VALUE (b) = val;
30394414
JM
1730}
1731
dff6b454
RK
1732extern char * first_global_object_name;
1733
1734/* Get a unique name for each call to this routine for unnamed namespaces.
1735 Mostly copied from get_file_function_name. */
e92cc029 1736
dff6b454
RK
1737static tree
1738get_unique_name ()
1739{
1740 static int temp_name_counter = 0;
1741 char *buf;
1742 register char *p;
1743
1744 if (first_global_object_name)
1745 p = first_global_object_name;
1746 else if (main_input_filename)
1747 p = main_input_filename;
1748 else
1749 p = input_filename;
1750
1751#define UNNAMED_NAMESPACE_FORMAT "__%s_%d"
1752
1753 buf = (char *) alloca (sizeof (UNNAMED_NAMESPACE_FORMAT) + strlen (p));
1754
1755 sprintf (buf, UNNAMED_NAMESPACE_FORMAT, p, temp_name_counter++);
1756
1757 /* Don't need to pull weird characters out of global names. */
1758 if (p != first_global_object_name)
1759 {
30394414 1760 for (p = buf+2; *p; p++)
dff6b454 1761 if (! ((*p >= '0' && *p <= '9')
dff6b454
RK
1762#ifndef NO_DOLLAR_IN_LABEL /* this for `$'; unlikely, but... -- kr */
1763 || *p == '$'
1764#endif
e92cc029 1765#ifndef NO_DOT_IN_LABEL /* this for `.'; unlikely, but... */
dff6b454
RK
1766 || *p == '.'
1767#endif
1768 || (*p >= 'A' && *p <= 'Z')
1769 || (*p >= 'a' && *p <= 'z')))
1770 *p = '_';
1771 }
1772
1773 return get_identifier (buf);
1774}
1775
1776/* Push into the scope of the NAME namespace. If NAME is NULL_TREE, then we
1777 select a name that is unique to this compilation unit. */
e92cc029 1778
a9aedbc2
MS
1779void
1780push_namespace (name)
1781 tree name;
1782{
30394414
JM
1783 tree d;
1784 int need_new = 1;
1785 int implicit_use = 0;
2c73f9f5 1786 int global = 0;
30394414
JM
1787 if (!global_namespace)
1788 {
2c73f9f5 1789 /* This must be ::. */
30394414 1790 my_friendly_assert (name == get_identifier ("::"), 377);
2c73f9f5 1791 global = 1;
30394414
JM
1792 }
1793 else if (!name)
1794 {
1795 name = get_unique_name ();
1796 implicit_use = 1;
2c73f9f5
ML
1797 }
1798 else if (current_namespace == global_namespace
1799 && name == DECL_NAME (std_node))
1800 {
1801 in_std++;
1802 return;
1803 }
30394414
JM
1804 else
1805 {
2c73f9f5 1806 /* Check whether this is an extended namespace definition. */
30394414
JM
1807 d = IDENTIFIER_NAMESPACE_VALUE (name);
1808 if (d != NULL_TREE && TREE_CODE (d) == NAMESPACE_DECL)
1809 need_new = 0;
1810 }
6633d636 1811
30394414
JM
1812 if (need_new)
1813 {
2c73f9f5
ML
1814 /* Make a new namespace, binding the name to it. */
1815 d = build_lang_decl (NAMESPACE_DECL, name, NULL_TREE);
1816 /* The global namespace is not pushed, and the global binding
1817 level is set elsewhere. */
1818 if (!global)
1819 {
1820 d = pushdecl (d);
1821 pushlevel (0);
1822 declare_namespace_level ();
1823 NAMESPACE_LEVEL (d) = current_binding_level;
1824 }
30394414 1825 }
2c73f9f5
ML
1826 else
1827 resume_binding_level (NAMESPACE_LEVEL (d));
1828
30394414
JM
1829 if (implicit_use)
1830 do_using_directive (d);
2c73f9f5 1831 /* Enter the name space. */
30394414 1832 current_namespace = d;
a9aedbc2
MS
1833}
1834
1835/* Pop from the scope of the current namespace. */
e92cc029 1836
a9aedbc2
MS
1837void
1838pop_namespace ()
1839{
2c73f9f5
ML
1840 if (current_namespace == global_namespace)
1841 {
1842 my_friendly_assert (in_std>0, 980421);
1843 in_std--;
1844 return;
1845 }
1846 current_namespace = DECL_CONTEXT (current_namespace);
1847 /* The binding level is not popped, as it might be re-opened later. */
1848 suspend_binding_level ();
1849}
a9aedbc2 1850
2c73f9f5 1851/* Concatenate the binding levels of all namespaces. */
a9aedbc2 1852
2c73f9f5
ML
1853void
1854cat_namespace_levels()
1855{
1856 tree current;
1857 tree last;
1858 struct binding_level *b;
a9aedbc2 1859
2c73f9f5
ML
1860 last = NAMESPACE_LEVEL (global_namespace) -> names;
1861 /* The nested namespaces appear in the names list of their ancestors. */
1862 for (current = last; current; current = TREE_CHAIN (current))
a9aedbc2 1863 {
2c73f9f5
ML
1864 if (TREE_CODE (current) != NAMESPACE_DECL)
1865 continue;
1866 if (!DECL_LANG_SPECIFIC (current))
a9aedbc2 1867 {
2c73f9f5
ML
1868 /* Hmm. std. */
1869 my_friendly_assert (current == std_node, 393);
1870 continue;
a9aedbc2 1871 }
2c73f9f5
ML
1872 b = NAMESPACE_LEVEL (current);
1873 while (TREE_CHAIN (last))
1874 last = TREE_CHAIN (last);
1875 TREE_CHAIN (last) = NAMESPACE_LEVEL (current) -> names;
a9aedbc2 1876 }
a9aedbc2 1877}
8d08fdba
MS
1878\f
1879/* Subroutines for reverting temporarily to top-level for instantiation
1880 of templates and such. We actually need to clear out the class- and
1881 local-value slots of all identifiers, so that only the global values
1882 are at all visible. Simply setting current_binding_level to the global
1883 scope isn't enough, because more binding levels may be pushed. */
1884struct saved_scope {
1885 struct binding_level *old_binding_level;
1886 tree old_bindings;
2c73f9f5 1887 tree old_namespace;
8d08fdba 1888 struct saved_scope *prev;
5f34005f 1889 tree class_name, class_type, function_decl;
8d08fdba 1890 struct binding_level *class_bindings;
51c184be
MS
1891 tree *lang_base, *lang_stack, lang_name;
1892 int lang_stacksize;
5566b478
MS
1893 int minimal_parse_mode;
1894 tree last_function_parms;
e76a2646 1895 tree template_parms;
5156628f 1896 HOST_WIDE_INT processing_template_decl;
a50f0918 1897 tree previous_class_type, previous_class_values;
e1467ff2
MM
1898 int processing_specialization;
1899 int processing_explicit_instantiation;
8d08fdba
MS
1900};
1901static struct saved_scope *current_saved_scope;
8d08fdba 1902
78957a2a
JM
1903/* A chain of the binding vecs created by store_bindings. We create a
1904 whole bunch of these during compilation, on permanent_obstack, so we
1905 can't just throw them away. */
1906static tree free_binding_vecs;
1907
e92cc029 1908static tree
45537677
MS
1909store_bindings (names, old_bindings)
1910 tree names, old_bindings;
1911{
1912 tree t;
1913 for (t = names; t; t = TREE_CHAIN (t))
1914 {
1915 tree binding, t1, id;
1916
1917 if (TREE_CODE (t) == TREE_LIST)
1918 id = TREE_PURPOSE (t);
1919 else
1920 id = DECL_NAME (t);
1921
1922 if (!id
1923 || (!IDENTIFIER_LOCAL_VALUE (id)
1924 && !IDENTIFIER_CLASS_VALUE (id)))
1925 continue;
1926
1927 for (t1 = old_bindings; t1; t1 = TREE_CHAIN (t1))
1928 if (TREE_VEC_ELT (t1, 0) == id)
1929 goto skip_it;
78957a2a
JM
1930
1931 if (free_binding_vecs)
1932 {
1933 binding = free_binding_vecs;
1934 free_binding_vecs = TREE_CHAIN (free_binding_vecs);
1935 }
1936 else
1937 binding = make_tree_vec (4);
1938
45537677
MS
1939 if (id)
1940 {
1941 my_friendly_assert (TREE_CODE (id) == IDENTIFIER_NODE, 135);
1942 TREE_VEC_ELT (binding, 0) = id;
2c73f9f5 1943 TREE_VEC_ELT (binding, 1) = REAL_IDENTIFIER_TYPE_VALUE (id);
45537677
MS
1944 TREE_VEC_ELT (binding, 2) = IDENTIFIER_LOCAL_VALUE (id);
1945 TREE_VEC_ELT (binding, 3) = IDENTIFIER_CLASS_VALUE (id);
1946 IDENTIFIER_LOCAL_VALUE (id) = NULL_TREE;
1947 IDENTIFIER_CLASS_VALUE (id) = NULL_TREE;
1948 }
1949 TREE_CHAIN (binding) = old_bindings;
1950 old_bindings = binding;
1951 skip_it:
1952 ;
1953 }
1954 return old_bindings;
1955}
1956
8d08fdba 1957void
5566b478
MS
1958maybe_push_to_top_level (pseudo)
1959 int pseudo;
8d08fdba 1960{
51c184be 1961 extern int current_lang_stacksize;
beb53fb8
JM
1962 struct saved_scope *s
1963 = (struct saved_scope *) xmalloc (sizeof (struct saved_scope));
5566b478 1964 struct binding_level *b = inner_binding_level;
8d08fdba
MS
1965 tree old_bindings = NULL_TREE;
1966
e349ee73
MS
1967 if (current_function_decl)
1968 push_cp_function_context (NULL_TREE);
1969
a50f0918
MS
1970 if (previous_class_type)
1971 old_bindings = store_bindings (previous_class_values, old_bindings);
1972
8d08fdba
MS
1973 /* Have to include global_binding_level, because class-level decls
1974 aren't listed anywhere useful. */
1975 for (; b; b = b->level_chain)
1976 {
1977 tree t;
1978
2c73f9f5
ML
1979 /* Template IDs are inserted into the global level. If they were
1980 inserted into namespace level, finish_file wouldn't find them
1981 when doing pending instantiations. Therefore, don't stop at
1982 namespace level, but continue until :: . */
5566b478
MS
1983 if (b == global_binding_level || (pseudo && b->pseudo_global))
1984 break;
8d08fdba 1985
45537677 1986 old_bindings = store_bindings (b->names, old_bindings);
cffa8729 1987 /* We also need to check class_shadowed to save class-level type
45537677
MS
1988 bindings, since pushclass doesn't fill in b->names. */
1989 if (b->parm_flag == 2)
cffa8729 1990 old_bindings = store_bindings (b->class_shadowed, old_bindings);
8d08fdba 1991
8d08fdba
MS
1992 /* Unwind type-value slots back to top level. */
1993 for (t = b->type_shadowed; t; t = TREE_CHAIN (t))
1994 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (t), TREE_VALUE (t));
1995 }
8d08fdba
MS
1996
1997 s->old_binding_level = current_binding_level;
5566b478 1998 current_binding_level = b;
8d08fdba 1999
2c73f9f5 2000 s->old_namespace = current_namespace;
8d08fdba
MS
2001 s->class_name = current_class_name;
2002 s->class_type = current_class_type;
8d08fdba
MS
2003 s->function_decl = current_function_decl;
2004 s->class_bindings = class_binding_level;
51c184be
MS
2005 s->lang_stack = current_lang_stack;
2006 s->lang_base = current_lang_base;
2007 s->lang_stacksize = current_lang_stacksize;
2008 s->lang_name = current_lang_name;
5566b478
MS
2009 s->minimal_parse_mode = minimal_parse_mode;
2010 s->last_function_parms = last_function_parms;
e76a2646 2011 s->template_parms = current_template_parms;
5156628f 2012 s->processing_template_decl = processing_template_decl;
a50f0918
MS
2013 s->previous_class_type = previous_class_type;
2014 s->previous_class_values = previous_class_values;
e1467ff2
MM
2015 s->processing_specialization = processing_specialization;
2016 s->processing_explicit_instantiation = processing_explicit_instantiation;
e349ee73 2017
5f34005f 2018 current_class_name = current_class_type = NULL_TREE;
8d08fdba
MS
2019 current_function_decl = NULL_TREE;
2020 class_binding_level = (struct binding_level *)0;
51c184be
MS
2021 current_lang_stacksize = 10;
2022 current_lang_stack = current_lang_base
2023 = (tree *) xmalloc (current_lang_stacksize * sizeof (tree));
2024 current_lang_name = lang_name_cplusplus;
2025 strict_prototype = strict_prototypes_lang_cplusplus;
2026 named_labels = NULL_TREE;
5566b478 2027 minimal_parse_mode = 0;
a50f0918 2028 previous_class_type = previous_class_values = NULL_TREE;
e1467ff2
MM
2029 processing_specialization = 0;
2030 processing_explicit_instantiation = 0;
eae97bd9
MM
2031 current_template_parms = NULL_TREE;
2032 processing_template_decl = 0;
2c73f9f5 2033 current_namespace = global_namespace;
8d08fdba
MS
2034
2035 s->prev = current_saved_scope;
2036 s->old_bindings = old_bindings;
2037 current_saved_scope = s;
5566b478
MS
2038
2039 push_obstacks (&permanent_obstack, &permanent_obstack);
2040}
2041
2042void
2043push_to_top_level ()
2044{
2045 maybe_push_to_top_level (0);
8d08fdba
MS
2046}
2047
2048void
2049pop_from_top_level ()
2050{
51c184be 2051 extern int current_lang_stacksize;
8d08fdba
MS
2052 struct saved_scope *s = current_saved_scope;
2053 tree t;
2054
e76a2646 2055 /* Clear out class-level bindings cache. */
8d08fdba 2056 if (previous_class_type)
e76a2646
MS
2057 {
2058 popclass (-1);
2059 previous_class_type = NULL_TREE;
2060 }
8d08fdba 2061
5566b478
MS
2062 pop_obstacks ();
2063
8d08fdba
MS
2064 current_binding_level = s->old_binding_level;
2065 current_saved_scope = s->prev;
78957a2a 2066 for (t = s->old_bindings; t; )
8d08fdba 2067 {
78957a2a 2068 tree save = t;
8d08fdba
MS
2069 tree id = TREE_VEC_ELT (t, 0);
2070 if (id)
2071 {
2c73f9f5 2072 SET_IDENTIFIER_TYPE_VALUE (id, TREE_VEC_ELT (t, 1));
8d08fdba
MS
2073 IDENTIFIER_LOCAL_VALUE (id) = TREE_VEC_ELT (t, 2);
2074 IDENTIFIER_CLASS_VALUE (id) = TREE_VEC_ELT (t, 3);
2075 }
78957a2a
JM
2076 t = TREE_CHAIN (t);
2077 TREE_CHAIN (save) = free_binding_vecs;
2078 free_binding_vecs = save;
8d08fdba 2079 }
2c73f9f5 2080 current_namespace = s->old_namespace;
8d08fdba
MS
2081 current_class_name = s->class_name;
2082 current_class_type = s->class_type;
8d08fdba
MS
2083 current_function_decl = s->function_decl;
2084 class_binding_level = s->class_bindings;
51c184be
MS
2085 free (current_lang_base);
2086 current_lang_base = s->lang_base;
2087 current_lang_stack = s->lang_stack;
2088 current_lang_name = s->lang_name;
2089 current_lang_stacksize = s->lang_stacksize;
2090 if (current_lang_name == lang_name_cplusplus)
2091 strict_prototype = strict_prototypes_lang_cplusplus;
2092 else if (current_lang_name == lang_name_c)
2093 strict_prototype = strict_prototypes_lang_c;
5566b478
MS
2094 minimal_parse_mode = s->minimal_parse_mode;
2095 last_function_parms = s->last_function_parms;
e76a2646 2096 current_template_parms = s->template_parms;
5156628f 2097 processing_template_decl = s->processing_template_decl;
a50f0918
MS
2098 previous_class_type = s->previous_class_type;
2099 previous_class_values = s->previous_class_values;
e1467ff2
MM
2100 processing_specialization = s->processing_specialization;
2101 processing_explicit_instantiation = s->processing_explicit_instantiation;
51c184be 2102
8d08fdba 2103 free (s);
e349ee73
MS
2104
2105 if (current_function_decl)
2106 pop_cp_function_context (NULL_TREE);
8d08fdba
MS
2107}
2108\f
2109/* Push a definition of struct, union or enum tag "name".
2110 into binding_level "b". "type" should be the type node,
2111 We assume that the tag "name" is not already defined.
2112
2113 Note that the definition may really be just a forward reference.
2114 In that case, the TYPE_SIZE will be a NULL_TREE.
2115
e92cc029 2116 C++ gratuitously puts all these tags in the name space. */
8d08fdba
MS
2117
2118/* When setting the IDENTIFIER_TYPE_VALUE field of an identifier ID,
2119 record the shadowed value for this binding contour. TYPE is
2120 the type that ID maps to. */
2121
2122static void
2123set_identifier_type_value_with_scope (id, type, b)
2124 tree id;
2125 tree type;
2126 struct binding_level *b;
2127{
2c73f9f5 2128 if (!b->namespace_p)
8d08fdba 2129 {
2c73f9f5
ML
2130 /* Shadow the marker, not the real thing, so that the marker
2131 gets restored later. */
2132 tree old_type_value = REAL_IDENTIFIER_TYPE_VALUE (id);
8d08fdba
MS
2133 b->type_shadowed
2134 = tree_cons (id, old_type_value, b->type_shadowed);
2135 }
2c73f9f5
ML
2136 else
2137 {
2138 tree binding = binding_for_name (id, current_namespace);
2139 BINDING_TYPE (binding) = type;
2140 /* Store marker instead of real type. */
2141 type = global_type_node;
2142 }
8d08fdba
MS
2143 SET_IDENTIFIER_TYPE_VALUE (id, type);
2144}
2145
e92cc029 2146/* As set_identifier_type_value_with_scope, but using inner_binding_level. */
8d08fdba
MS
2147
2148void
2149set_identifier_type_value (id, type)
2150 tree id;
2151 tree type;
2152{
2153 set_identifier_type_value_with_scope (id, type, inner_binding_level);
2154}
2155
2c73f9f5
ML
2156/* Return the type associated with id. */
2157
2158tree
2159identifier_type_value (id)
2160 tree id;
2161{
2162 /* There is no type with that name, anywhere. */
2163 if (REAL_IDENTIFIER_TYPE_VALUE (id) == NULL_TREE)
2164 return NULL_TREE;
2165 /* This is not the type marker, but the real thing. */
2166 if (REAL_IDENTIFIER_TYPE_VALUE (id) != global_type_node)
2167 return REAL_IDENTIFIER_TYPE_VALUE (id);
2168 /* Have to search for it. It must be on the global level, now.
2169 Ask lookup_name not to return non-types. */
2170 id = lookup_name_real (id, 2, 1);
2171 if (id)
2172 return TREE_TYPE (id);
2173 return NULL_TREE;
2174}
2175
a9aedbc2
MS
2176/* Pop off extraneous binding levels left over due to syntax errors.
2177
2178 We don't pop past namespaces, as they might be valid. */
e92cc029 2179
8926095f
MS
2180void
2181pop_everything ()
2182{
2183#ifdef DEBUG_CP_BINDING_LEVELS
2184 fprintf (stderr, "XXX entering pop_everything ()\n");
2185#endif
a9aedbc2 2186 while (! toplevel_bindings_p () && ! pseudo_global_level_p ())
8926095f
MS
2187 {
2188 if (class_binding_level)
2189 pop_nested_class (1);
2190 else
2191 poplevel (0, 0, 0);
2192 }
2193#ifdef DEBUG_CP_BINDING_LEVELS
2194 fprintf (stderr, "XXX leaving pop_everything ()\n");
2195#endif
2196}
2197
8d08fdba 2198/* Push a tag name NAME for struct/class/union/enum type TYPE.
6757edfe 2199 Normally put it into the inner-most non-tag-transparent scope,
8d08fdba 2200 but if GLOBALIZE is true, put it in the inner-most non-class scope.
e92cc029 2201 The latter is needed for implicit declarations. */
8d08fdba
MS
2202
2203void
2204pushtag (name, type, globalize)
2205 tree name, type;
2206 int globalize;
2207{
2208 register struct binding_level *b;
7177d104 2209 tree context = 0;
2986ae00 2210 tree c_decl = 0;
8d08fdba
MS
2211
2212 b = inner_binding_level;
2213 while (b->tag_transparent
2214 || (globalize && b->parm_flag == 2))
2215 b = b->level_chain;
2216
a9aedbc2 2217 if (toplevel_bindings_p ())
8d08fdba
MS
2218 b->tags = perm_tree_cons (name, type, b->tags);
2219 else
2220 b->tags = saveable_tree_cons (name, type, b->tags);
2221
2222 if (name)
2223 {
7177d104 2224 context = type ? TYPE_CONTEXT (type) : NULL_TREE;
297dcfb3
MM
2225 if (! context)
2226 {
2227 tree cs = current_scope ();
2228
2229 if (! globalize)
2230 context = cs;
2231 else if (cs != NULL_TREE
2232 && TREE_CODE_CLASS (TREE_CODE (cs)) == 't')
2233 /* When declaring a friend class of a local class, we want
2234 to inject the newly named class into the scope
2235 containing the local class, not the namespace scope. */
2236 context = hack_decl_function_context (get_type_decl (cs));
2237 }
7177d104 2238 if (context)
2986ae00 2239 c_decl = TREE_CODE (context) == FUNCTION_DECL
e1cd6e56 2240 ? context : TYPE_MAIN_DECL (context);
8d08fdba 2241
2c73f9f5
ML
2242 if (!context)
2243 context = current_namespace;
2244
8d08fdba 2245 /* Do C++ gratuitous typedefing. */
db5ae43f 2246 if (IDENTIFIER_TYPE_VALUE (name) != type)
8d08fdba 2247 {
93cdc044
JM
2248 register tree d = NULL_TREE;
2249 int newdecl = 0, in_class = 0;
8d08fdba 2250
93cdc044
JM
2251 if ((b->pseudo_global && b->level_chain->parm_flag == 2)
2252 || b->parm_flag == 2)
2253 in_class = 1;
8d08fdba 2254 else
93cdc044
JM
2255 d = lookup_nested_type (type, c_decl);
2256
2257 if (d == NULL_TREE)
8d08fdba 2258 {
8d08fdba 2259 newdecl = 1;
a0a33927 2260 d = build_decl (TYPE_DECL, name, type);
00595019 2261 SET_DECL_ARTIFICIAL (d);
93cdc044
JM
2262 if (! in_class)
2263 set_identifier_type_value_with_scope (name, type, b);
2264 }
2265 else
2266 d = TYPE_MAIN_DECL (d);
2267
2268 TYPE_NAME (type) = d;
2269 DECL_CONTEXT (d) = context;
e1cd6e56 2270
6757edfe
MM
2271 if (IS_AGGR_TYPE (type)
2272 && (/* If !GLOBALIZE then we are looking at a
eae97bd9
MM
2273 definition. It may not be a primary template.
2274 (For example, in:
2275
2276 template <class T>
2277 struct S1 { class S2 {}; }
2278
2279 we have to push_template_decl for S2.) */
6757edfe 2280 (processing_template_decl && !globalize)
eae97bd9
MM
2281 /* If we are declaring a friend template class, we
2282 will have GLOBALIZE set, since something like:
6757edfe
MM
2283
2284 template <class T>
2285 struct S1 {
2286 template <class U>
2287 friend class S2;
2288 };
2289
eae97bd9
MM
2290 declares S2 to be at global scope. */
2291 || (processing_template_decl >
2292 template_class_depth (current_class_type))))
93cdc044 2293 {
6757edfe
MM
2294 d = push_template_decl_real (d, globalize);
2295 /* If the current binding level is the binding level for
2296 the template parameters (see the comment in
2297 begin_template_parm_list) and the enclosing level is
2298 a class scope, and we're not looking at a friend,
2299 push the declaration of the member class into the
2300 class scope. In the friend case, push_template_decl
2301 will already have put the friend into global scope,
2302 if appropriate. */
2303 if (!globalize && b->pseudo_global &&
2304 b->level_chain->parm_flag == 2)
93cdc044
JM
2305 pushdecl_with_scope (CLASSTYPE_TI_TEMPLATE (type),
2306 b->level_chain);
8d08fdba 2307 }
93cdc044
JM
2308
2309 if (b->parm_flag == 2)
2310 d = pushdecl_class_level (d);
2311 else
2312 d = pushdecl_with_scope (d, b);
2313
8ccc31eb 2314 if (newdecl)
8d08fdba 2315 {
d2e5ee5c
MS
2316 if (ANON_AGGRNAME_P (name))
2317 DECL_IGNORED_P (d) = 1;
8ccc31eb 2318
5566b478 2319 TYPE_CONTEXT (type) = DECL_CONTEXT (d);
1f06b267 2320 DECL_ASSEMBLER_NAME (d) = DECL_NAME (d);
8ccc31eb
MS
2321 DECL_ASSEMBLER_NAME (d)
2322 = get_identifier (build_overload_name (type, 1, 1));
8d08fdba 2323 }
8d08fdba
MS
2324 }
2325 if (b->parm_flag == 2)
2326 {
2327 TREE_NONLOCAL_FLAG (type) = 1;
2328 if (TYPE_SIZE (current_class_type) == NULL_TREE)
2329 CLASSTYPE_TAGS (current_class_type) = b->tags;
2330 }
2331 }
2332
2333 if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL)
2334 /* Use the canonical TYPE_DECL for this node. */
2335 TYPE_STUB_DECL (type) = TYPE_NAME (type);
2336 else
2337 {
2338 /* Create a fake NULL-named TYPE_DECL node whose TREE_TYPE
2339 will be the tagged type we just added to the current
2340 binding level. This fake NULL-named TYPE_DECL node helps
2341 dwarfout.c to know when it needs to output a
2342 representation of a tagged type, and it also gives us a
2343 convenient place to record the "scope start" address for
2344 the tagged type. */
2345
8d08fdba 2346 tree d = build_decl (TYPE_DECL, NULL_TREE, type);
8d08fdba
MS
2347 TYPE_STUB_DECL (type) = pushdecl_with_scope (d, b);
2348 }
2349}
2350
2351/* Counter used to create anonymous type names. */
e92cc029 2352
8d08fdba
MS
2353static int anon_cnt = 0;
2354
2355/* Return an IDENTIFIER which can be used as a name for
2356 anonymous structs and unions. */
e92cc029 2357
8d08fdba
MS
2358tree
2359make_anon_name ()
2360{
2361 char buf[32];
2362
2363 sprintf (buf, ANON_AGGRNAME_FORMAT, anon_cnt++);
2364 return get_identifier (buf);
2365}
2366
2367/* Clear the TREE_PURPOSE slot of tags which have anonymous typenames.
2368 This keeps dbxout from getting confused. */
e92cc029 2369
8d08fdba
MS
2370void
2371clear_anon_tags ()
2372{
2373 register struct binding_level *b;
2374 register tree tags;
2375 static int last_cnt = 0;
2376
2377 /* Fast out if no new anon names were declared. */
2378 if (last_cnt == anon_cnt)
2379 return;
2380
2381 b = current_binding_level;
2382 while (b->tag_transparent)
2383 b = b->level_chain;
2384 tags = b->tags;
2385 while (tags)
2386 {
2387 /* A NULL purpose means we have already processed all tags
2388 from here to the end of the list. */
2389 if (TREE_PURPOSE (tags) == NULL_TREE)
2390 break;
2391 if (ANON_AGGRNAME_P (TREE_PURPOSE (tags)))
2392 TREE_PURPOSE (tags) = NULL_TREE;
2393 tags = TREE_CHAIN (tags);
2394 }
2395 last_cnt = anon_cnt;
2396}
2397\f
2398/* Subroutine of duplicate_decls: return truthvalue of whether
2399 or not types of these decls match.
2400
2401 For C++, we must compare the parameter list so that `int' can match
2402 `int&' in a parameter position, but `int&' is not confused with
2403 `const int&'. */
e92cc029 2404
6060a796 2405int
8d08fdba
MS
2406decls_match (newdecl, olddecl)
2407 tree newdecl, olddecl;
2408{
2409 int types_match;
2410
a28e3c7f
MS
2411 if (TREE_CODE (newdecl) == FUNCTION_DECL
2412 && TREE_CODE (olddecl) == FUNCTION_DECL)
8d08fdba
MS
2413 {
2414 tree f1 = TREE_TYPE (newdecl);
2415 tree f2 = TREE_TYPE (olddecl);
2416 tree p1 = TYPE_ARG_TYPES (f1);
2417 tree p2 = TYPE_ARG_TYPES (f2);
2418
2419 /* When we parse a static member function definition,
2420 we put together a FUNCTION_DECL which thinks its type
2421 is METHOD_TYPE. Change that to FUNCTION_TYPE, and
2422 proceed. */
2423 if (TREE_CODE (f1) == METHOD_TYPE && DECL_STATIC_FUNCTION_P (olddecl))
700f8a87 2424 revert_static_member_fn (&newdecl, &f1, &p1);
8d08fdba
MS
2425 else if (TREE_CODE (f2) == METHOD_TYPE
2426 && DECL_STATIC_FUNCTION_P (newdecl))
700f8a87 2427 revert_static_member_fn (&olddecl, &f2, &p2);
8d08fdba
MS
2428
2429 /* Here we must take care of the case where new default
2430 parameters are specified. Also, warn if an old
2431 declaration becomes ambiguous because default
2432 parameters may cause the two to be ambiguous. */
2433 if (TREE_CODE (f1) != TREE_CODE (f2))
2434 {
2435 if (TREE_CODE (f1) == OFFSET_TYPE)
2436 cp_compiler_error ("`%D' redeclared as member function", newdecl);
2437 else
2438 cp_compiler_error ("`%D' redeclared as non-member function", newdecl);
2439 return 0;
2440 }
2441
39211cd5 2442 if (comptypes (TREE_TYPE (f1), TREE_TYPE (f2), 1))
8926095f 2443 {
a28e3c7f 2444 if (! strict_prototypes_lang_c && DECL_LANGUAGE (olddecl) == lang_c
8926095f 2445 && p2 == NULL_TREE)
a28e3c7f
MS
2446 {
2447 types_match = self_promoting_args_p (p1);
2448 if (p1 == void_list_node)
2449 TREE_TYPE (newdecl) = TREE_TYPE (olddecl);
2450 }
2451 else if (!strict_prototypes_lang_c && DECL_LANGUAGE (olddecl)==lang_c
2452 && DECL_LANGUAGE (newdecl) == lang_c && p1 == NULL_TREE)
2453 {
2454 types_match = self_promoting_args_p (p2);
2455 TREE_TYPE (newdecl) = TREE_TYPE (olddecl);
2456 }
8926095f 2457 else
a4443a08 2458 types_match = compparms (p1, p2, 3);
8926095f 2459 }
8d08fdba
MS
2460 else
2461 types_match = 0;
2462 }
51c184be
MS
2463 else if (TREE_CODE (newdecl) == TEMPLATE_DECL
2464 && TREE_CODE (olddecl) == TEMPLATE_DECL)
2465 {
f84b4be9
JM
2466 if (!comp_template_parms (DECL_TEMPLATE_PARMS (newdecl),
2467 DECL_TEMPLATE_PARMS (olddecl)))
2468 return 0;
2469
2470 if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL)
2471 types_match = 1;
2472 else
2473 types_match = decls_match (DECL_TEMPLATE_RESULT (olddecl),
2474 DECL_TEMPLATE_RESULT (newdecl));
51c184be 2475 }
8d08fdba
MS
2476 else
2477 {
2478 if (TREE_TYPE (newdecl) == error_mark_node)
2479 types_match = TREE_TYPE (olddecl) == error_mark_node;
2480 else if (TREE_TYPE (olddecl) == NULL_TREE)
2481 types_match = TREE_TYPE (newdecl) == NULL_TREE;
a0a33927
MS
2482 else if (TREE_TYPE (newdecl) == NULL_TREE)
2483 types_match = 0;
72b7eeff
MS
2484 /* Qualifiers must match, and they may be present on either, the type
2485 or the decl. */
2486 else if ((TREE_READONLY (newdecl)
2487 || TYPE_READONLY (TREE_TYPE (newdecl)))
2488 == (TREE_READONLY (olddecl)
2489 || TYPE_READONLY (TREE_TYPE (olddecl)))
2490 && (TREE_THIS_VOLATILE (newdecl)
2491 || TYPE_VOLATILE (TREE_TYPE (newdecl)))
2492 == (TREE_THIS_VOLATILE (olddecl)
2493 || TYPE_VOLATILE (TREE_TYPE (olddecl))))
2494 types_match = comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (newdecl)),
2495 TYPE_MAIN_VARIANT (TREE_TYPE (olddecl)), 1);
8d08fdba 2496 else
72b7eeff 2497 types_match = 0;
8d08fdba
MS
2498 }
2499
2500 return types_match;
2501}
2502
2503/* If NEWDECL is `static' and an `extern' was seen previously,
2504 warn about it. (OLDDECL may be NULL_TREE; NAME contains
2505 information about previous usage as an `extern'.)
2506
2507 Note that this does not apply to the C++ case of declaring
2508 a variable `extern const' and then later `const'.
2509
8d08fdba
MS
2510 Don't complain about built-in functions, since they are beyond
2511 the user's control. */
2512
2513static void
2514warn_extern_redeclared_static (newdecl, olddecl)
2515 tree newdecl, olddecl;
2516{
2517 tree name;
2518
2519 static char *explicit_extern_static_warning
2520 = "`%D' was declared `extern' and later `static'";
2521 static char *implicit_extern_static_warning
2522 = "`%D' was declared implicitly `extern' and later `static'";
2523
d22c8596 2524 if (TREE_CODE (newdecl) == TYPE_DECL)
8d08fdba
MS
2525 return;
2526
2527 name = DECL_ASSEMBLER_NAME (newdecl);
faae18ab 2528 if (TREE_PUBLIC (name) && DECL_THIS_STATIC (newdecl))
8d08fdba
MS
2529 {
2530 /* It's okay to redeclare an ANSI built-in function as static,
2531 or to declare a non-ANSI built-in function as anything. */
2532 if (! (TREE_CODE (newdecl) == FUNCTION_DECL
2533 && olddecl != NULL_TREE
2534 && TREE_CODE (olddecl) == FUNCTION_DECL
2535 && (DECL_BUILT_IN (olddecl)
2536 || DECL_BUILT_IN_NONANSI (olddecl))))
2537 {
a9aedbc2 2538 cp_pedwarn (IDENTIFIER_IMPLICIT_DECL (name)
8d08fdba
MS
2539 ? implicit_extern_static_warning
2540 : explicit_extern_static_warning, newdecl);
2541 if (olddecl != NULL_TREE)
a9aedbc2 2542 cp_pedwarn_at ("previous declaration of `%D'", olddecl);
8d08fdba
MS
2543 }
2544 }
2545}
2546
2547/* Handle when a new declaration NEWDECL has the same name as an old
2548 one OLDDECL in the same binding contour. Prints an error message
2549 if appropriate.
2550
2551 If safely possible, alter OLDDECL to look like NEWDECL, and return 1.
2552 Otherwise, return 0. */
2553
51c184be 2554int
8d08fdba 2555duplicate_decls (newdecl, olddecl)
824b9a4c 2556 tree newdecl, olddecl;
8d08fdba
MS
2557{
2558 extern struct obstack permanent_obstack;
2559 unsigned olddecl_uid = DECL_UID (olddecl);
2560 int olddecl_friend = 0, types_match = 0;
2561 int new_defines_function;
5566b478
MS
2562
2563 if (newdecl == olddecl)
2564 return 1;
8d08fdba 2565
f6abb50a 2566 if (TREE_CODE_CLASS (TREE_CODE (olddecl)) == 'd')
d9525bec
BK
2567 DECL_MACHINE_ATTRIBUTES (newdecl)
2568 = merge_machine_decl_attributes (olddecl, newdecl);
f6abb50a 2569
8926095f 2570 types_match = decls_match (newdecl, olddecl);
8d08fdba
MS
2571
2572 if (TREE_CODE (olddecl) != TREE_LIST)
2573 olddecl_friend = DECL_LANG_SPECIFIC (olddecl) && DECL_FRIEND_P (olddecl);
2574
2575 /* If either the type of the new decl or the type of the old decl is an
2576 error_mark_node, then that implies that we have already issued an
2577 error (earlier) for some bogus type specification, and in that case,
2578 it is rather pointless to harass the user with yet more error message
2579 about the same declaration, so well just pretent the types match here. */
bd6dd845
MS
2580 if (TREE_TYPE (newdecl) == error_mark_node
2581 || TREE_TYPE (olddecl) == error_mark_node)
8d08fdba
MS
2582 types_match = 1;
2583
d22c8596
MS
2584 if (TREE_CODE (olddecl) == FUNCTION_DECL
2585 && DECL_ARTIFICIAL (olddecl)
2586 && (DECL_BUILT_IN (olddecl) || DECL_BUILT_IN_NONANSI (olddecl)))
8d08fdba
MS
2587 {
2588 /* If you declare a built-in or predefined function name as static,
a4443a08
MS
2589 the old definition is overridden, but optionally warn this was a
2590 bad choice of name. Ditto for overloads. */
893de33c 2591 if (! TREE_PUBLIC (newdecl)
a4443a08
MS
2592 || (TREE_CODE (newdecl) == FUNCTION_DECL
2593 && DECL_LANGUAGE (newdecl) != DECL_LANGUAGE (olddecl)))
2594 {
2595 if (warn_shadow)
2596 cp_warning ("shadowing %s function `%#D'",
2597 DECL_BUILT_IN (olddecl) ? "built-in" : "library",
2598 olddecl);
2599 /* Discard the old built-in function. */
2600 return 0;
2601 }
2602 else if (! types_match)
8d08fdba 2603 {
a4443a08
MS
2604 if (TREE_CODE (newdecl) != FUNCTION_DECL)
2605 {
2606 /* If the built-in is not ansi, then programs can override
2607 it even globally without an error. */
2608 if (! DECL_BUILT_IN (olddecl))
2609 cp_warning ("library function `%#D' redeclared as non-function `%#D'",
2610 olddecl, newdecl);
2611 else
2612 {
2613 cp_error ("declaration of `%#D'", newdecl);
2614 cp_error ("conflicts with built-in declaration `%#D'",
2615 olddecl);
2616 }
2617 return 0;
2618 }
2619
8d08fdba
MS
2620 cp_warning ("declaration of `%#D'", newdecl);
2621 cp_warning ("conflicts with built-in declaration `%#D'",
2622 olddecl);
8d08fdba 2623 }
39211cd5
MS
2624 }
2625 else if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
2626 {
2627 if ((TREE_CODE (newdecl) == FUNCTION_DECL
5566b478 2628 && DECL_FUNCTION_TEMPLATE_P (olddecl))
39211cd5 2629 || (TREE_CODE (olddecl) == FUNCTION_DECL
5566b478 2630 && DECL_FUNCTION_TEMPLATE_P (newdecl)))
39211cd5
MS
2631 return 0;
2632
2633 cp_error ("`%#D' redeclared as different kind of symbol", newdecl);
2634 if (TREE_CODE (olddecl) == TREE_LIST)
2635 olddecl = TREE_VALUE (olddecl);
2636 cp_error_at ("previous declaration of `%#D'", olddecl);
2637
2638 /* New decl is completely inconsistent with the old one =>
2639 tell caller to replace the old one. */
2640
2641 return 0;
8d08fdba 2642 }
8d08fdba
MS
2643 else if (!types_match)
2644 {
8926095f 2645 if (TREE_CODE (newdecl) == TEMPLATE_DECL)
f0e01782
MS
2646 {
2647 /* The name of a class template may not be declared to refer to
2648 any other template, class, function, object, namespace, value,
e92cc029 2649 or type in the same scope. */
5566b478
MS
2650 if (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) == TYPE_DECL
2651 || TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL)
f0e01782
MS
2652 {
2653 cp_error ("declaration of template `%#D'", newdecl);
2654 cp_error_at ("conflicts with previous declaration `%#D'",
2655 olddecl);
2656 }
ec255269
MS
2657 else if (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) == FUNCTION_DECL
2658 && TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == FUNCTION_DECL
2659 && compparms (TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (olddecl))),
75650646
MM
2660 TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (newdecl))), 3)
2661 && comp_template_parms (DECL_TEMPLATE_PARMS (newdecl),
2662 DECL_TEMPLATE_PARMS (olddecl)))
ec255269
MS
2663 {
2664 cp_error ("new declaration `%#D'", newdecl);
2665 cp_error_at ("ambiguates old declaration `%#D'", olddecl);
2666 }
f0e01782
MS
2667 return 0;
2668 }
8926095f
MS
2669 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2670 {
2671 if (DECL_LANGUAGE (newdecl) == lang_c
2672 && DECL_LANGUAGE (olddecl) == lang_c)
2673 {
2674 cp_error ("declaration of C function `%#D' conflicts with",
2675 newdecl);
2676 cp_error_at ("previous declaration `%#D' here", olddecl);
2677 }
00595019 2678 else if (compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
28cbf42c 2679 TYPE_ARG_TYPES (TREE_TYPE (olddecl)), 3))
700f8a87
MS
2680 {
2681 cp_error ("new declaration `%#D'", newdecl);
2682 cp_error_at ("ambiguates old declaration `%#D'", olddecl);
2683 }
2684 else
2685 return 0;
8926095f 2686 }
8d08fdba
MS
2687
2688 /* Already complained about this, so don't do so again. */
a4443a08 2689 else if (current_class_type == NULL_TREE
8d08fdba
MS
2690 || IDENTIFIER_ERROR_LOCUS (DECL_ASSEMBLER_NAME (newdecl)) != current_class_type)
2691 {
f376e137 2692 cp_error ("conflicting types for `%#D'", newdecl);
8926095f 2693 cp_error_at ("previous declaration as `%#D'", olddecl);
8d08fdba
MS
2694 }
2695 }
75650646
MM
2696 else if (TREE_CODE (newdecl) == FUNCTION_DECL
2697 && ((DECL_TEMPLATE_SPECIALIZATION (olddecl)
2698 && (!DECL_TEMPLATE_INFO (newdecl)
2699 || (DECL_TI_TEMPLATE (newdecl)
2700 != DECL_TI_TEMPLATE (olddecl))))
2701 || (DECL_TEMPLATE_SPECIALIZATION (newdecl)
2702 && (!DECL_TEMPLATE_INFO (olddecl)
2703 || (DECL_TI_TEMPLATE (olddecl)
2704 != DECL_TI_TEMPLATE (newdecl))))))
386b8a85
JM
2705 /* It's OK to have a template specialization and a non-template
2706 with the same type, or to have specializations of two
75650646
MM
2707 different templates with the same type. Note that if one is a
2708 specialization, and the other is an instantiation of the same
2709 template, that we do not exit at this point. That situation
2710 can occur if we instantiate a template class, and then
2711 specialize one of its methods. This situation is legal, but
2712 the declarations must be merged in the usual way. */
2713 return 0;
2714 else if (TREE_CODE (newdecl) == FUNCTION_DECL
2715 && ((DECL_TEMPLATE_INSTANTIATION (olddecl)
2716 && !DECL_USE_TEMPLATE (newdecl))
2717 || (DECL_TEMPLATE_INSTANTIATION (newdecl)
2718 && !DECL_USE_TEMPLATE (olddecl))))
2719 /* One of the declarations is a template instantiation, and the
2720 other is not a template at all. That's OK. */
386b8a85 2721 return 0;
8d08fdba
MS
2722 else
2723 {
2724 char *errmsg = redeclaration_error_message (newdecl, olddecl);
2725 if (errmsg)
2726 {
51c184be 2727 cp_error (errmsg, newdecl);
8d08fdba
MS
2728 if (DECL_NAME (olddecl) != NULL_TREE)
2729 cp_error_at ((DECL_INITIAL (olddecl)
2c73f9f5 2730 && namespace_bindings_p ())
8d08fdba
MS
2731 ? "`%#D' previously defined here"
2732 : "`%#D' previously declared here", olddecl);
2733 }
2734 else if (TREE_CODE (olddecl) == FUNCTION_DECL
2735 && DECL_INITIAL (olddecl) != NULL_TREE
2736 && TYPE_ARG_TYPES (TREE_TYPE (olddecl)) == NULL_TREE
2737 && TYPE_ARG_TYPES (TREE_TYPE (newdecl)) != NULL_TREE)
2738 {
2739 /* Prototype decl follows defn w/o prototype. */
2740 cp_warning_at ("prototype for `%#D'", newdecl);
2741 cp_warning_at ("follows non-prototype definition here", olddecl);
2742 }
2743 else if (TREE_CODE (olddecl) == FUNCTION_DECL
2744 && DECL_LANGUAGE (newdecl) != DECL_LANGUAGE (olddecl))
8926095f
MS
2745 {
2746 /* extern "C" int foo ();
2747 int foo () { bar (); }
2748 is OK. */
2749 if (current_lang_stack == current_lang_base)
a28e3c7f 2750 DECL_LANGUAGE (newdecl) = DECL_LANGUAGE (olddecl);
8926095f
MS
2751 else
2752 {
2753 cp_error_at ("previous declaration of `%#D' with %L linkage",
2754 olddecl, DECL_LANGUAGE (olddecl));
2755 cp_error ("conflicts with new declaration with %L linkage",
2756 DECL_LANGUAGE (newdecl));
2757 }
2758 }
e1cd6e56 2759
a6f02587 2760 if (DECL_LANG_SPECIFIC (olddecl) && DECL_USE_TEMPLATE (olddecl))
c32381b1
JM
2761 ;
2762 else if (TREE_CODE (olddecl) == FUNCTION_DECL)
e1cd6e56
MS
2763 {
2764 tree t1 = TYPE_ARG_TYPES (TREE_TYPE (olddecl));
2765 tree t2 = TYPE_ARG_TYPES (TREE_TYPE (newdecl));
2766 int i = 1;
2767
2768 if (TREE_CODE (TREE_TYPE (newdecl)) == METHOD_TYPE)
2769 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2);
2770
2771 for (; t1 && t1 != void_list_node;
2772 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2), i++)
2773 if (TREE_PURPOSE (t1) && TREE_PURPOSE (t2))
2774 {
2507f3b5
RK
2775 if (1 == simple_cst_equal (TREE_PURPOSE (t1),
2776 TREE_PURPOSE (t2)))
e1cd6e56
MS
2777 {
2778 if (pedantic)
2779 {
2780 cp_pedwarn ("default argument given for parameter %d of `%#D'",
2781 i, newdecl);
2782 cp_pedwarn_at ("after previous specification in `%#D'",
2783 olddecl);
2784 }
2785 }
2786 else
2787 {
2788 cp_error ("default argument given for parameter %d of `%#D'",
2789 i, newdecl);
da20811c 2790 cp_error_at ("after previous specification in `%#D'",
e1cd6e56
MS
2791 olddecl);
2792 }
2793 }
a5894242 2794
7fcdf4c2
MS
2795 if (DECL_THIS_INLINE (newdecl) && ! DECL_THIS_INLINE (olddecl)
2796 && TREE_ADDRESSABLE (olddecl) && warn_inline)
dff6b454 2797 {
7fcdf4c2
MS
2798 cp_warning ("`%#D' was used before it was declared inline",
2799 newdecl);
2800 cp_warning_at ("previous non-inline declaration here",
2801 olddecl);
dff6b454 2802 }
e1cd6e56 2803 }
8ccc31eb
MS
2804 /* These bits are logically part of the type for non-functions. */
2805 else if (TREE_READONLY (newdecl) != TREE_READONLY (olddecl)
2806 || TREE_THIS_VOLATILE (newdecl) != TREE_THIS_VOLATILE (olddecl))
2807 {
2808 cp_pedwarn ("type qualifiers for `%#D'", newdecl);
2809 cp_pedwarn_at ("conflict with previous decl `%#D'", olddecl);
2810 }
8d08fdba
MS
2811 }
2812
2813 /* If new decl is `static' and an `extern' was seen previously,
2814 warn about it. */
2815 warn_extern_redeclared_static (newdecl, olddecl);
2816
e92cc029 2817 /* We have committed to returning 1 at this point. */
8d08fdba
MS
2818 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2819 {
2820 /* Now that functions must hold information normally held
2821 by field decls, there is extra work to do so that
2822 declaration information does not get destroyed during
2823 definition. */
2824 if (DECL_VINDEX (olddecl))
2825 DECL_VINDEX (newdecl) = DECL_VINDEX (olddecl);
2826 if (DECL_CONTEXT (olddecl))
2827 DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
2828 if (DECL_CLASS_CONTEXT (olddecl))
2829 DECL_CLASS_CONTEXT (newdecl) = DECL_CLASS_CONTEXT (olddecl);
8d08fdba
MS
2830 if (DECL_PENDING_INLINE_INFO (newdecl) == (struct pending_inline *)0)
2831 DECL_PENDING_INLINE_INFO (newdecl) = DECL_PENDING_INLINE_INFO (olddecl);
e1cd6e56
MS
2832 DECL_STATIC_CONSTRUCTOR (newdecl) |= DECL_STATIC_CONSTRUCTOR (olddecl);
2833 DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
7215f9a0 2834 DECL_ABSTRACT_VIRTUAL_P (newdecl) |= DECL_ABSTRACT_VIRTUAL_P (olddecl);
864b83b9 2835 DECL_VIRTUAL_P (newdecl) |= DECL_VIRTUAL_P (olddecl);
4a67c9e9 2836 DECL_NEEDS_FINAL_OVERRIDER_P (newdecl) |= DECL_NEEDS_FINAL_OVERRIDER_P (olddecl);
8d08fdba
MS
2837 }
2838
2839 /* Deal with C++: must preserve virtual function table size. */
2840 if (TREE_CODE (olddecl) == TYPE_DECL)
2841 {
2842 register tree newtype = TREE_TYPE (newdecl);
2843 register tree oldtype = TREE_TYPE (olddecl);
2844
2845 if (newtype != error_mark_node && oldtype != error_mark_node
2846 && TYPE_LANG_SPECIFIC (newtype) && TYPE_LANG_SPECIFIC (oldtype))
2847 {
2848 CLASSTYPE_VSIZE (newtype) = CLASSTYPE_VSIZE (oldtype);
2849 CLASSTYPE_FRIEND_CLASSES (newtype)
2850 = CLASSTYPE_FRIEND_CLASSES (oldtype);
2851 }
8d08fdba
MS
2852 }
2853
2854 /* Special handling ensues if new decl is a function definition. */
2855 new_defines_function = (TREE_CODE (newdecl) == FUNCTION_DECL
2856 && DECL_INITIAL (newdecl) != NULL_TREE);
2857
2858 /* Optionally warn about more than one declaration for the same name,
2859 but don't warn about a function declaration followed by a definition. */
2860 if (warn_redundant_decls
700f8a87 2861 && ! DECL_ARTIFICIAL (olddecl)
8d08fdba
MS
2862 && !(new_defines_function && DECL_INITIAL (olddecl) == NULL_TREE)
2863 /* Don't warn about extern decl followed by (tentative) definition. */
556ffb67
BK
2864 && !(DECL_EXTERNAL (olddecl) && ! DECL_EXTERNAL (newdecl))
2865 /* Don't warn about friends, let add_friend take care of it. */
70233af3 2866 && (TREE_CODE (newdecl) == FUNCTION_DECL && ! DECL_FRIEND_P (newdecl)))
8d08fdba
MS
2867 {
2868 cp_warning ("redundant redeclaration of `%D' in same scope", newdecl);
44a8d0b3 2869 cp_warning_at ("previous declaration of `%D'", olddecl);
8d08fdba
MS
2870 }
2871
2872 /* Copy all the DECL_... slots specified in the new decl
2873 except for any that we copy here from the old type. */
2874
5566b478
MS
2875 if (TREE_CODE (newdecl) == TEMPLATE_DECL)
2876 {
37dac039
JM
2877 if (DECL_INITIAL (DECL_TEMPLATE_RESULT (olddecl)) == NULL_TREE)
2878 {
3ac3d9ea
MM
2879 if (! duplicate_decls (DECL_TEMPLATE_RESULT (newdecl),
2880 DECL_TEMPLATE_RESULT (olddecl)))
2881 cp_error ("invalid redeclaration of %D", newdecl);
2882 TREE_TYPE (olddecl) = TREE_TYPE (DECL_TEMPLATE_RESULT (olddecl));
37dac039 2883 DECL_TEMPLATE_PARMS (olddecl) = DECL_TEMPLATE_PARMS (newdecl);
f84b4be9 2884 DECL_TEMPLATE_INFO (olddecl) = DECL_TEMPLATE_INFO (newdecl);
37dac039 2885 }
5566b478
MS
2886 return 1;
2887 }
2888
8d08fdba
MS
2889 if (types_match)
2890 {
2891 /* Automatically handles default parameters. */
2892 tree oldtype = TREE_TYPE (olddecl);
e1cd6e56 2893 tree newtype;
8d08fdba 2894
e1cd6e56
MS
2895 /* Make sure we put the new type in the same obstack as the old one. */
2896 if (oldtype)
39211cd5
MS
2897 push_obstacks (TYPE_OBSTACK (oldtype), TYPE_OBSTACK (oldtype));
2898 else
2899 {
2900 push_obstacks_nochange ();
2901 end_temporary_allocation ();
2902 }
2903
e1cd6e56
MS
2904 /* Merge the data types specified in the two decls. */
2905 newtype = common_type (TREE_TYPE (newdecl), TREE_TYPE (olddecl));
2906
8d08fdba
MS
2907 if (TREE_CODE (newdecl) == VAR_DECL)
2908 DECL_THIS_EXTERN (newdecl) |= DECL_THIS_EXTERN (olddecl);
2909 /* Do this after calling `common_type' so that default
2910 parameters don't confuse us. */
2911 else if (TREE_CODE (newdecl) == FUNCTION_DECL
2912 && (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (newdecl))
2913 != TYPE_RAISES_EXCEPTIONS (TREE_TYPE (olddecl))))
2914 {
f30432d7 2915 TREE_TYPE (newdecl) = build_exception_variant (newtype,
8d08fdba 2916 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (newdecl)));
f30432d7 2917 TREE_TYPE (olddecl) = build_exception_variant (newtype,
8d08fdba
MS
2918 TYPE_RAISES_EXCEPTIONS (oldtype));
2919
047f64a3
JM
2920 if ((pedantic || (! DECL_IN_SYSTEM_HEADER (olddecl)
2921 && DECL_SOURCE_LINE (olddecl) != 0))
da20811c 2922 && flag_exceptions
824b9a4c 2923 && ! compexcepttypes (TREE_TYPE (newdecl), TREE_TYPE (olddecl)))
8d08fdba 2924 {
824b9a4c 2925 cp_pedwarn ("declaration of `%D' throws different exceptions",
a28e3c7f 2926 newdecl);
824b9a4c 2927 cp_pedwarn_at ("previous declaration here", olddecl);
8d08fdba
MS
2928 }
2929 }
2930 TREE_TYPE (newdecl) = TREE_TYPE (olddecl) = newtype;
2931
2932 /* Lay the type out, unless already done. */
b7484fbe 2933 if (oldtype != TREE_TYPE (newdecl)
5566b478 2934 && TREE_TYPE (newdecl) != error_mark_node
5156628f 2935 && !(processing_template_decl && uses_template_parms (newdecl)))
b7484fbe
MS
2936 layout_type (TREE_TYPE (newdecl));
2937
5566b478
MS
2938 if ((TREE_CODE (newdecl) == VAR_DECL
2939 || TREE_CODE (newdecl) == PARM_DECL
2940 || TREE_CODE (newdecl) == RESULT_DECL
2941 || TREE_CODE (newdecl) == FIELD_DECL
2942 || TREE_CODE (newdecl) == TYPE_DECL)
5156628f 2943 && !(processing_template_decl && uses_template_parms (newdecl)))
b7484fbe 2944 layout_decl (newdecl, 0);
8d08fdba
MS
2945
2946 /* Merge the type qualifiers. */
2947 if (TREE_READONLY (newdecl))
2948 TREE_READONLY (olddecl) = 1;
2949 if (TREE_THIS_VOLATILE (newdecl))
2950 TREE_THIS_VOLATILE (olddecl) = 1;
2951
2952 /* Merge the initialization information. */
8926095f
MS
2953 if (DECL_INITIAL (newdecl) == NULL_TREE
2954 && DECL_INITIAL (olddecl) != NULL_TREE)
2955 {
2956 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
2957 DECL_SOURCE_FILE (newdecl) = DECL_SOURCE_FILE (olddecl);
2958 DECL_SOURCE_LINE (newdecl) = DECL_SOURCE_LINE (olddecl);
2959 }
39211cd5
MS
2960
2961 /* Merge the section attribute.
2962 We want to issue an error if the sections conflict but that must be
2963 done later in decl_attributes since we are called before attributes
2964 are assigned. */
2965 if (DECL_SECTION_NAME (newdecl) == NULL_TREE)
2966 DECL_SECTION_NAME (newdecl) = DECL_SECTION_NAME (olddecl);
2967
8d08fdba
MS
2968 /* Keep the old rtl since we can safely use it, unless it's the
2969 call to abort() used for abstract virtuals. */
2970 if ((DECL_LANG_SPECIFIC (olddecl)
2971 && !DECL_ABSTRACT_VIRTUAL_P (olddecl))
2972 || DECL_RTL (olddecl) != DECL_RTL (abort_fndecl))
2973 DECL_RTL (newdecl) = DECL_RTL (olddecl);
39211cd5
MS
2974
2975 pop_obstacks ();
8d08fdba
MS
2976 }
2977 /* If cannot merge, then use the new type and qualifiers,
2978 and don't preserve the old rtl. */
2979 else
2980 {
2981 /* Clean out any memory we had of the old declaration. */
2982 tree oldstatic = value_member (olddecl, static_aggregates);
2983 if (oldstatic)
2984 TREE_VALUE (oldstatic) = error_mark_node;
2985
2986 TREE_TYPE (olddecl) = TREE_TYPE (newdecl);
2987 TREE_READONLY (olddecl) = TREE_READONLY (newdecl);
2988 TREE_THIS_VOLATILE (olddecl) = TREE_THIS_VOLATILE (newdecl);
2989 TREE_SIDE_EFFECTS (olddecl) = TREE_SIDE_EFFECTS (newdecl);
2990 }
2991
2992 /* Merge the storage class information. */
a9aedbc2 2993 DECL_WEAK (newdecl) |= DECL_WEAK (olddecl);
e92cc029 2994 DECL_ONE_ONLY (newdecl) |= DECL_ONE_ONLY (olddecl);
a9aedbc2
MS
2995 TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
2996 TREE_STATIC (olddecl) = TREE_STATIC (newdecl) |= TREE_STATIC (olddecl);
2997 if (! DECL_EXTERNAL (olddecl))
2998 DECL_EXTERNAL (newdecl) = 0;
8d08fdba 2999
5566b478 3000 if (DECL_LANG_SPECIFIC (newdecl))
8d08fdba 3001 {
a9aedbc2
MS
3002 DECL_INTERFACE_KNOWN (newdecl) |= DECL_INTERFACE_KNOWN (olddecl);
3003 DECL_NOT_REALLY_EXTERN (newdecl) |= DECL_NOT_REALLY_EXTERN (olddecl);
77be6f82 3004 DECL_COMDAT (newdecl) |= DECL_COMDAT (olddecl);
8d08fdba
MS
3005 }
3006
8d08fdba
MS
3007 if (TREE_CODE (newdecl) == FUNCTION_DECL)
3008 {
75650646
MM
3009 if (DECL_TEMPLATE_INSTANTIATION (olddecl)
3010 && !DECL_TEMPLATE_INSTANTIATION (newdecl))
3011 {
3012 /* If newdecl is not a specialization, then it is not a
3013 template-related function at all. And that means that we
3014 shoud have exited above, returning 0. */
3015 my_friendly_assert (DECL_TEMPLATE_SPECIALIZATION (newdecl),
3016 0);
3017
3018 if (TREE_USED (olddecl))
3019 /* From [temp.expl.spec]:
3020
3021 If a template, a member template or the member of a class
3022 template is explicitly specialized then that
3023 specialization shall be declared before the first use of
3024 that specialization that would cause an implicit
3025 instantiation to take place, in every translation unit in
3026 which such a use occurs. */
3027 cp_error ("explicit specialization of %D after first use",
3028 olddecl);
3029
3030 SET_DECL_TEMPLATE_SPECIALIZATION (olddecl);
3031 }
faae18ab
MS
3032 DECL_THIS_INLINE (newdecl) |= DECL_THIS_INLINE (olddecl);
3033
3034 /* If either decl says `inline', this fn is inline, unless its
3035 definition was passed already. */
3036 if (DECL_INLINE (newdecl) && DECL_INITIAL (olddecl) == NULL_TREE)
3037 DECL_INLINE (olddecl) = 1;
3038 DECL_INLINE (newdecl) = DECL_INLINE (olddecl);
3039
700f8a87
MS
3040 if (! types_match)
3041 {
3042 DECL_LANGUAGE (olddecl) = DECL_LANGUAGE (newdecl);
3043 DECL_ASSEMBLER_NAME (olddecl) = DECL_ASSEMBLER_NAME (newdecl);
5566b478
MS
3044 DECL_RTL (olddecl) = DECL_RTL (newdecl);
3045 }
3046 if (! types_match || new_defines_function)
3047 {
3048 /* These need to be copied so that the names are available. */
700f8a87
MS
3049 DECL_ARGUMENTS (olddecl) = DECL_ARGUMENTS (newdecl);
3050 DECL_RESULT (olddecl) = DECL_RESULT (newdecl);
700f8a87 3051 }
8d08fdba
MS
3052 if (new_defines_function)
3053 /* If defining a function declared with other language
3054 linkage, use the previously declared language linkage. */
3055 DECL_LANGUAGE (newdecl) = DECL_LANGUAGE (olddecl);
3056 else
3057 {
3058 /* If redeclaring a builtin function, and not a definition,
3059 it stays built in. */
3060 if (DECL_BUILT_IN (olddecl))
3061 {
3062 DECL_BUILT_IN (newdecl) = 1;
39211cd5 3063 DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl);
8d08fdba
MS
3064 /* If we're keeping the built-in definition, keep the rtl,
3065 regardless of declaration matches. */
3066 DECL_RTL (newdecl) = DECL_RTL (olddecl);
3067 }
3068 else
3069 DECL_FRAME_SIZE (newdecl) = DECL_FRAME_SIZE (olddecl);
3070
3071 DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
8926095f 3072 if ((DECL_SAVED_INSNS (newdecl) = DECL_SAVED_INSNS (olddecl)))
8d08fdba
MS
3073 /* Previously saved insns go together with
3074 the function's previous definition. */
3075 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
3076 /* Don't clear out the arguments if we're redefining a function. */
3077 if (DECL_ARGUMENTS (olddecl))
3078 DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl);
3079 }
6060a796
MS
3080 if (DECL_LANG_SPECIFIC (olddecl))
3081 DECL_MAIN_VARIANT (newdecl) = DECL_MAIN_VARIANT (olddecl);
8d08fdba
MS
3082 }
3083
a9aedbc2
MS
3084 if (TREE_CODE (newdecl) == NAMESPACE_DECL)
3085 {
3086 NAMESPACE_LEVEL (newdecl) = NAMESPACE_LEVEL (olddecl);
3087 }
3088
8926095f
MS
3089 if (TREE_CODE (newdecl) == TEMPLATE_DECL)
3090 {
75650646
MM
3091 DECL_TEMPLATE_SPECIALIZATIONS (newdecl)
3092 = DECL_TEMPLATE_SPECIALIZATIONS (olddecl);
8926095f 3093 }
5566b478 3094
8d08fdba
MS
3095 /* Now preserve various other info from the definition. */
3096 TREE_ADDRESSABLE (newdecl) = TREE_ADDRESSABLE (olddecl);
3097 TREE_ASM_WRITTEN (newdecl) = TREE_ASM_WRITTEN (olddecl);
3098 DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
f376e137 3099 DECL_ASSEMBLER_NAME (newdecl) = DECL_ASSEMBLER_NAME (olddecl);
8d08fdba
MS
3100
3101 /* Don't really know how much of the language-specific
3102 values we should copy from old to new. */
3103 if (DECL_LANG_SPECIFIC (olddecl))
3104 {
3105 DECL_IN_AGGR_P (newdecl) = DECL_IN_AGGR_P (olddecl);
3106 DECL_ACCESS (newdecl) = DECL_ACCESS (olddecl);
db5ae43f 3107 DECL_NONCONVERTING_P (newdecl) = DECL_NONCONVERTING_P (olddecl);
5566b478
MS
3108 if (DECL_TEMPLATE_INFO (newdecl) == NULL_TREE)
3109 {
3110 DECL_TEMPLATE_INFO (newdecl) = DECL_TEMPLATE_INFO (olddecl);
3111 DECL_USE_TEMPLATE (newdecl) = DECL_USE_TEMPLATE (olddecl);
3112 }
8d08fdba
MS
3113 }
3114
3115 if (TREE_CODE (newdecl) == FUNCTION_DECL)
3116 {
3117 int function_size;
3118 struct lang_decl *ol = DECL_LANG_SPECIFIC (olddecl);
3119 struct lang_decl *nl = DECL_LANG_SPECIFIC (newdecl);
3120
3121 function_size = sizeof (struct tree_decl);
3122
3123 bcopy ((char *) newdecl + sizeof (struct tree_common),
3124 (char *) olddecl + sizeof (struct tree_common),
3125 function_size - sizeof (struct tree_common));
3126
3127 /* Can we safely free the storage used by newdecl? */
3128
3129#define ROUND(x) ((x + obstack_alignment_mask (&permanent_obstack)) \
3130 & ~ obstack_alignment_mask (&permanent_obstack))
3131
75650646
MM
3132 if (DECL_TEMPLATE_INSTANTIATION (newdecl))
3133 {
3134 /* If newdecl is a template instantiation, it is possible that
3135 the following sequence of events has occurred:
3136
3137 o A friend function was declared in a class template. The
3138 class template was instantiated.
3139
3140 o The instantiation of the friend declaration was
3141 recorded on the instantiation list, and is newdecl.
3142
3143 o Later, however, instantiate_class_template called pushdecl
3144 on the newdecl to perform name injection. But, pushdecl in
3145 turn called duplicate_decls when it discovered that another
3146 declaration of a global function with the same name already
3147 existed.
3148
3149 o Here, in duplicate_decls, we decided to clobber newdecl.
3150
3151 If we're going to do that, we'd better make sure that
3152 olddecl, and not newdecl, is on the list of
3153 instantiations so that if we try to do the instantiation
3154 again we won't get the clobbered declaration. */
3155
3156 tree tmpl = DECL_TI_TEMPLATE (newdecl);
3157 tree decls = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
3158
3159 for (; decls; decls = TREE_CHAIN (decls))
3160 if (TREE_VALUE (decls) == newdecl)
3161 TREE_VALUE (decls) = olddecl;
3162 }
3163
3a56f0ab
JM
3164 if (((char *)newdecl + ROUND (function_size) == (char *)nl
3165 && ((char *)newdecl + ROUND (function_size)
3166 + ROUND (sizeof (struct lang_decl))
3167 == obstack_next_free (&permanent_obstack)))
3168 || ((char *)newdecl + ROUND (function_size)
3169 == obstack_next_free (&permanent_obstack)))
8d08fdba
MS
3170 {
3171 DECL_MAIN_VARIANT (newdecl) = olddecl;
3172 DECL_LANG_SPECIFIC (olddecl) = ol;
3173 bcopy ((char *)nl, (char *)ol, sizeof (struct lang_decl));
3174
3175 obstack_free (&permanent_obstack, newdecl);
3176 }
d22c8596 3177 else if (LANG_DECL_PERMANENT (ol) && ol != nl)
8d08fdba
MS
3178 {
3179 if (DECL_MAIN_VARIANT (olddecl) == olddecl)
3180 {
3181 /* Save these lang_decls that would otherwise be lost. */
3182 extern tree free_lang_decl_chain;
3183 tree free_lang_decl = (tree) ol;
d22c8596
MS
3184
3185 if (DECL_LANG_SPECIFIC (olddecl) == ol)
3186 abort ();
3187
8d08fdba
MS
3188 TREE_CHAIN (free_lang_decl) = free_lang_decl_chain;
3189 free_lang_decl_chain = free_lang_decl;
3190 }
3191 else
3192 {
bd6dd845 3193 /* Storage leak. */;
8d08fdba
MS
3194 }
3195 }
3196 }
3197 else
3198 {
3199 bcopy ((char *) newdecl + sizeof (struct tree_common),
3200 (char *) olddecl + sizeof (struct tree_common),
3201 sizeof (struct tree_decl) - sizeof (struct tree_common)
3202 + tree_code_length [(int)TREE_CODE (newdecl)] * sizeof (char *));
3203 }
3204
3205 DECL_UID (olddecl) = olddecl_uid;
3206 if (olddecl_friend)
3207 DECL_FRIEND_P (olddecl) = 1;
3208
d9525bec
BK
3209 /* NEWDECL contains the merged attribute lists.
3210 Update OLDDECL to be the same. */
3211 DECL_MACHINE_ATTRIBUTES (olddecl) = DECL_MACHINE_ATTRIBUTES (newdecl);
3212
8d08fdba
MS
3213 return 1;
3214}
3215
3216/* Record a decl-node X as belonging to the current lexical scope.
3217 Check for errors (such as an incompatible declaration for the same
3218 name already seen in the same scope).
3219
3220 Returns either X or an old decl for the same name.
3221 If an old decl is returned, it may have been smashed
3222 to agree with what X says. */
3223
3224tree
3225pushdecl (x)
3226 tree x;
3227{
3228 register tree t;
8d08fdba 3229 register tree name = DECL_ASSEMBLER_NAME (x);
8d08fdba
MS
3230 register struct binding_level *b = current_binding_level;
3231
2c73f9f5
ML
3232 if (current_function_decl && x != current_function_decl
3233 /* A local declaration for a function doesn't constitute nesting. */
3234 && (TREE_CODE (x) != FUNCTION_DECL || DECL_INITIAL (x))
700f8a87
MS
3235 /* Don't change DECL_CONTEXT of virtual methods. */
3236 && (TREE_CODE (x) != FUNCTION_DECL || !DECL_VIRTUAL_P (x))
8d08fdba
MS
3237 && ! DECL_CONTEXT (x))
3238 DECL_CONTEXT (x) = current_function_decl;
2c73f9f5
ML
3239 if (!DECL_CONTEXT (x))
3240 DECL_CONTEXT (x) = current_namespace;
8d08fdba 3241
8d08fdba 3242 /* Type are looked up using the DECL_NAME, as that is what the rest of the
e92cc029 3243 compiler wants to use. */
a9aedbc2 3244 if (TREE_CODE (x) == TYPE_DECL || TREE_CODE (x) == VAR_DECL
73b0fce8
KL
3245 || TREE_CODE (x) == NAMESPACE_DECL || TREE_CODE (x) == TEMPLATE_TYPE_PARM
3246 || TREE_CODE (x) == TEMPLATE_TEMPLATE_PARM)
8d08fdba 3247 name = DECL_NAME (x);
8d08fdba
MS
3248
3249 if (name)
3250 {
5566b478
MS
3251#if 0
3252 /* Not needed...see below. */
8d08fdba
MS
3253 char *file;
3254 int line;
5566b478 3255#endif
386b8a85
JM
3256 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
3257 name = TREE_OPERAND (name, 0);
3258
2c73f9f5 3259 /* Namespace-scoped variables are not found in the current level. */
6eb3bb27 3260 if (TREE_CODE (x) == VAR_DECL && DECL_NAMESPACE_SCOPE_P (x))
2c73f9f5
ML
3261 t = namespace_binding (name, DECL_CONTEXT (x));
3262 else
3263 t = lookup_name_current_level (name);
8d08fdba
MS
3264 if (t == error_mark_node)
3265 {
3266 /* error_mark_node is 0 for a while during initialization! */
3267 t = NULL_TREE;
3268 cp_error_at ("`%#D' used prior to declaration", x);
3269 }
3270
51c184be 3271 else if (t != NULL_TREE)
8d08fdba 3272 {
5566b478
MS
3273#if 0
3274 /* This is turned off until I have time to do it right (bpk). */
e92cc029 3275 /* With the code below that uses it... */
8d6e462b
PB
3276 file = DECL_SOURCE_FILE (t);
3277 line = DECL_SOURCE_LINE (t);
5566b478 3278#endif
2ee887f2 3279 if (TREE_CODE (t) == PARM_DECL)
8d08fdba
MS
3280 {
3281 if (DECL_CONTEXT (t) == NULL_TREE)
3282 fatal ("parse errors have confused me too much");
be99da77 3283
e92cc029 3284 /* Check for duplicate params. */
be99da77
MS
3285 if (duplicate_decls (x, t))
3286 return t;
8d08fdba 3287 }
8d6e462b 3288 else if (((TREE_CODE (x) == FUNCTION_DECL && DECL_LANGUAGE (x) == lang_c)
5566b478
MS
3289 || DECL_FUNCTION_TEMPLATE_P (x))
3290 && is_overloaded_fn (t))
2c73f9f5 3291 /* Don't do anything just yet. */;
e1cd6e56
MS
3292 else if (t == wchar_decl_node)
3293 {
3294 if (pedantic && ! DECL_IN_SYSTEM_HEADER (x))
3295 cp_pedwarn ("redeclaration of wchar_t as `%T'", TREE_TYPE (x));
3296
3297 /* Throw away the redeclaration. */
3298 return t;
3299 }
8926095f 3300 else if (TREE_CODE (t) != TREE_CODE (x))
8d08fdba 3301 {
ec255269
MS
3302 if ((TREE_CODE (t) == TYPE_DECL && DECL_ARTIFICIAL (t)
3303 && TREE_CODE (x) != TYPE_DECL
3304 && ! (TREE_CODE (x) == TEMPLATE_DECL
3305 && TREE_CODE (DECL_TEMPLATE_RESULT (x)) == TYPE_DECL))
3306 || (TREE_CODE (x) == TYPE_DECL && DECL_ARTIFICIAL (x)
3307 && TREE_CODE (t) != TYPE_DECL
3308 && ! (TREE_CODE (t) == TEMPLATE_DECL
3309 && (TREE_CODE (DECL_TEMPLATE_RESULT (t))
3310 == TYPE_DECL))))
51c184be
MS
3311 {
3312 /* We do nothing special here, because C++ does such nasty
3313 things with TYPE_DECLs. Instead, just let the TYPE_DECL
3314 get shadowed, and know that if we need to find a TYPE_DECL
3315 for a given name, we can look in the IDENTIFIER_TYPE_VALUE
3316 slot of the identifier. */
3317 ;
3318 }
3319 else if (duplicate_decls (x, t))
3320 return t;
8d08fdba
MS
3321 }
3322 else if (duplicate_decls (x, t))
51c184be 3323 {
8d08fdba 3324#if 0
8926095f 3325 /* This is turned off until I have time to do it right (bpk). */
8d08fdba 3326
8926095f
MS
3327 /* Also warn if they did a prototype with `static' on it, but
3328 then later left the `static' off. */
3329 if (! TREE_PUBLIC (name) && TREE_PUBLIC (x))
8d08fdba 3330 {
8926095f
MS
3331 if (DECL_LANG_SPECIFIC (t) && DECL_FRIEND_P (t))
3332 return t;
8d08fdba 3333
8926095f
MS
3334 if (extra_warnings)
3335 {
a28e3c7f
MS
3336 cp_warning ("`static' missing from declaration of `%D'",
3337 t);
8926095f
MS
3338 warning_with_file_and_line (file, line,
3339 "previous declaration of `%s'",
3340 decl_as_string (t, 0));
3341 }
8d08fdba 3342
8926095f
MS
3343 /* Now fix things so it'll do what they expect. */
3344 if (current_function_decl)
3345 TREE_PUBLIC (current_function_decl) = 0;
3346 }
51c184be
MS
3347 /* Due to interference in memory reclamation (X may be
3348 obstack-deallocated at this point), we must guard against
8926095f
MS
3349 one really special case. [jason: This should be handled
3350 by start_function] */
51c184be
MS
3351 if (current_function_decl == x)
3352 current_function_decl = t;
8926095f 3353#endif
7177d104
MS
3354 if (TREE_CODE (t) == TYPE_DECL)
3355 SET_IDENTIFIER_TYPE_VALUE (name, TREE_TYPE (t));
f30432d7
MS
3356 else if (TREE_CODE (t) == FUNCTION_DECL)
3357 check_default_args (t);
7177d104 3358
51c184be
MS
3359 return t;
3360 }
8d08fdba 3361 }
8926095f
MS
3362
3363 if (TREE_CODE (x) == FUNCTION_DECL && ! DECL_FUNCTION_MEMBER_P (x))
3364 {
3365 t = push_overloaded_decl (x, 1);
3366 if (t != x || DECL_LANGUAGE (x) == lang_c)
3367 return t;
3368 }
6eb3bb27 3369 else if (DECL_FUNCTION_TEMPLATE_P (x) && DECL_NAMESPACE_SCOPE_P (x))
8926095f 3370 return push_overloaded_decl (x, 0);
8d08fdba 3371
a1774733
BK
3372 /* If declaring a type as a typedef, copy the type (unless we're
3373 at line 0), and install this TYPE_DECL as the new type's typedef
3374 name. See the extensive comment in ../c-decl.c (pushdecl). */
8d08fdba
MS
3375 if (TREE_CODE (x) == TYPE_DECL)
3376 {
3377 tree type = TREE_TYPE (x);
a1774733
BK
3378 if (DECL_SOURCE_LINE (x) == 0)
3379 {
3380 if (TYPE_NAME (type) == 0)
3381 TYPE_NAME (type) = x;
3382 }
3383 else if (type != error_mark_node && TYPE_NAME (type) != x)
3384 {
3385 DECL_ORIGINAL_TYPE (x) = type;
3386 type = build_type_copy (type);
3387 TYPE_STUB_DECL (type) = TYPE_STUB_DECL (DECL_ORIGINAL_TYPE (x));
3388 TYPE_NAME (type) = x;
3389 TREE_TYPE (x) = type;
3390 }
8d08fdba 3391
8d08fdba
MS
3392 if (type != error_mark_node
3393 && TYPE_NAME (type)
3394 && TYPE_IDENTIFIER (type))
3395 set_identifier_type_value_with_scope (DECL_NAME (x), type, b);
3396 }
3397
3398 /* Multiple external decls of the same identifier ought to match.
3399
3400 We get warnings about inline functions where they are defined.
39211cd5
MS
3401 We get warnings about other functions from push_overloaded_decl.
3402
8d08fdba 3403 Avoid duplicate warnings where they are used. */
39211cd5 3404 if (TREE_PUBLIC (x) && TREE_CODE (x) != FUNCTION_DECL)
8d08fdba
MS
3405 {
3406 tree decl;
30394414 3407 tree bindings = binding_for_name (name, current_namespace);
8d08fdba 3408
30394414
JM
3409 if (BINDING_VALUE (bindings) != NULL_TREE
3410 && (DECL_EXTERNAL (BINDING_VALUE (bindings))
3411 || TREE_PUBLIC (BINDING_VALUE (bindings))))
3412 decl = BINDING_VALUE (bindings);
8d08fdba
MS
3413 else
3414 decl = NULL_TREE;
3415
39211cd5 3416 if (decl
8d08fdba
MS
3417 /* If different sort of thing, we already gave an error. */
3418 && TREE_CODE (decl) == TREE_CODE (x)
39211cd5 3419 && ! comptypes (TREE_TYPE (x), TREE_TYPE (decl), 1))
8d08fdba
MS
3420 {
3421 cp_pedwarn ("type mismatch with previous external decl", x);
8926095f 3422 cp_pedwarn_at ("previous external decl of `%#D'", decl);
8d08fdba
MS
3423 }
3424 }
3425
8d08fdba
MS
3426 /* This name is new in its binding level.
3427 Install the new declaration and return it. */
2c73f9f5 3428 if (namespace_bindings_p ())
8d08fdba
MS
3429 {
3430 /* Install a global value. */
30394414 3431 tree bindings = binding_for_name (name, current_namespace);
8d08fdba 3432
8d08fdba
MS
3433 /* If the first global decl has external linkage,
3434 warn if we later see static one. */
30394414 3435 if (BINDING_VALUE (bindings) == NULL_TREE && TREE_PUBLIC (x))
8d08fdba
MS
3436 TREE_PUBLIC (name) = 1;
3437
e1cd6e56
MS
3438 /* Don't install an artificial TYPE_DECL if we already have
3439 another _DECL with that name. */
8d08fdba
MS
3440 if (TREE_CODE (x) != TYPE_DECL
3441 || t == NULL_TREE
e1cd6e56 3442 || ! DECL_ARTIFICIAL (x))
30394414
JM
3443 {
3444 if (TREE_CODE (x) == FUNCTION_DECL)
3445 my_friendly_assert ((BINDING_VALUE (bindings) == NULL_TREE)
3446 || BINDING_VALUE (bindings) == x, 378);
3447 BINDING_VALUE (bindings) = x;
3448 }
8d08fdba
MS
3449
3450 /* Don't forget if the function was used via an implicit decl. */
3451 if (IDENTIFIER_IMPLICIT_DECL (name)
3452 && TREE_USED (IDENTIFIER_IMPLICIT_DECL (name)))
3453 TREE_USED (x) = 1;
3454
3455 /* Don't forget if its address was taken in that way. */
3456 if (IDENTIFIER_IMPLICIT_DECL (name)
3457 && TREE_ADDRESSABLE (IDENTIFIER_IMPLICIT_DECL (name)))
3458 TREE_ADDRESSABLE (x) = 1;
3459
3460 /* Warn about mismatches against previous implicit decl. */
3461 if (IDENTIFIER_IMPLICIT_DECL (name) != NULL_TREE
3462 /* If this real decl matches the implicit, don't complain. */
3463 && ! (TREE_CODE (x) == FUNCTION_DECL
3464 && TREE_TYPE (TREE_TYPE (x)) == integer_type_node))
3465 cp_warning
3466 ("`%D' was previously implicitly declared to return `int'", x);
3467
3468 /* If new decl is `static' and an `extern' was seen previously,
3469 warn about it. */
a0a33927
MS
3470 if (x != NULL_TREE && t != NULL_TREE && decls_match (x, t))
3471 warn_extern_redeclared_static (x, t);
8d08fdba
MS
3472 }
3473 else
3474 {
3475 /* Here to install a non-global value. */
3476 tree oldlocal = IDENTIFIER_LOCAL_VALUE (name);
30394414 3477 tree oldglobal = binding_for_name (name, current_namespace);
8d08fdba 3478
e1cd6e56
MS
3479 /* Don't install an artificial TYPE_DECL if we already have
3480 another _DECL with that name. */
3481 if (TREE_CODE (x) != TYPE_DECL
3482 || t == NULL_TREE
3483 || ! DECL_ARTIFICIAL (x))
3484 {
3485 b->shadowed = tree_cons (name, oldlocal, b->shadowed);
3486 IDENTIFIER_LOCAL_VALUE (name) = x;
3487 }
8d08fdba
MS
3488
3489 /* If this is a TYPE_DECL, push it into the type value slot. */
3490 if (TREE_CODE (x) == TYPE_DECL)
3491 set_identifier_type_value_with_scope (name, TREE_TYPE (x), b);
3492
a9aedbc2
MS
3493 /* Clear out any TYPE_DECL shadowed by a namespace so that
3494 we won't think this is a type. The C struct hack doesn't
3495 go through namespaces. */
3496 if (TREE_CODE (x) == NAMESPACE_DECL)
3497 set_identifier_type_value_with_scope (name, NULL_TREE, b);
3498
8d08fdba
MS
3499 /* If this is an extern function declaration, see if we
3500 have a global definition or declaration for the function. */
3501 if (oldlocal == NULL_TREE
faae18ab 3502 && DECL_EXTERNAL (x)
30394414 3503 && BINDING_VALUE (oldglobal) != NULL_TREE
8d08fdba 3504 && TREE_CODE (x) == FUNCTION_DECL
30394414 3505 && TREE_CODE (BINDING_VALUE (oldglobal)) == FUNCTION_DECL)
8d08fdba
MS
3506 {
3507 /* We have one. Their types must agree. */
30394414 3508 if (decls_match (x, BINDING_VALUE (oldglobal)))
6060a796
MS
3509 /* OK */;
3510 else
8d08fdba
MS
3511 {
3512 cp_warning ("extern declaration of `%#D' doesn't match", x);
30394414 3513 cp_warning_at ("global declaration `%#D'", BINDING_VALUE (oldglobal));
8d08fdba 3514 }
8d08fdba
MS
3515 }
3516 /* If we have a local external declaration,
3517 and no file-scope declaration has yet been seen,
3518 then if we later have a file-scope decl it must not be static. */
3519 if (oldlocal == NULL_TREE
30394414 3520 && BINDING_VALUE (oldglobal) == NULL_TREE
8d08fdba
MS
3521 && DECL_EXTERNAL (x)
3522 && TREE_PUBLIC (x))
3523 {
3524 TREE_PUBLIC (name) = 1;
3525 }
3526
3527 if (DECL_FROM_INLINE (x))
3528 /* Inline decls shadow nothing. */;
3529
3530 /* Warn if shadowing an argument at the top level of the body. */
3531 else if (oldlocal != NULL_TREE && !DECL_EXTERNAL (x)
3532 && TREE_CODE (oldlocal) == PARM_DECL
3533 && TREE_CODE (x) != PARM_DECL)
3534 {
3535 /* Go to where the parms should be and see if we
3536 find them there. */
3537 struct binding_level *b = current_binding_level->level_chain;
3538
3539 if (cleanup_label)
3540 b = b->level_chain;
3541
3542 /* ARM $8.3 */
3543 if (b->parm_flag == 1)
3544 cp_error ("declaration of `%#D' shadows a parameter", name);
3545 }
72b7eeff 3546 else if (warn_shadow && oldlocal != NULL_TREE && b->is_for_scope
8d6e462b
PB
3547 && !DECL_DEAD_FOR_LOCAL (oldlocal))
3548 {
3549 warning ("variable `%s' shadows local",
3550 IDENTIFIER_POINTER (name));
3551 cp_warning_at (" this is the shadowed declaration", oldlocal);
3552 }
8d08fdba
MS
3553 /* Maybe warn if shadowing something else. */
3554 else if (warn_shadow && !DECL_EXTERNAL (x)
3555 /* No shadow warnings for internally generated vars. */
700f8a87 3556 && ! DECL_ARTIFICIAL (x)
8d08fdba
MS
3557 /* No shadow warnings for vars made for inlining. */
3558 && ! DECL_FROM_INLINE (x))
3559 {
3560 char *warnstring = NULL;
3561
3562 if (oldlocal != NULL_TREE && TREE_CODE (oldlocal) == PARM_DECL)
3563 warnstring = "declaration of `%s' shadows a parameter";
3564 else if (IDENTIFIER_CLASS_VALUE (name) != NULL_TREE
4ac14744 3565 && current_class_ptr
8d08fdba
MS
3566 && !TREE_STATIC (name))
3567 warnstring = "declaration of `%s' shadows a member of `this'";
3568 else if (oldlocal != NULL_TREE)
3569 warnstring = "declaration of `%s' shadows previous local";
30394414
JM
3570 else if (BINDING_VALUE (oldglobal) != NULL_TREE)
3571 /* XXX shadow warnings in outer-more namespaces */
8d08fdba
MS
3572 warnstring = "declaration of `%s' shadows global declaration";
3573
3574 if (warnstring)
3575 warning (warnstring, IDENTIFIER_POINTER (name));
3576 }
e1cd6e56 3577 }
8d08fdba 3578
e1cd6e56 3579 if (TREE_CODE (x) == FUNCTION_DECL)
f30432d7 3580 check_default_args (x);
8145f082
MS
3581
3582 /* Keep count of variables in this level with incomplete type. */
8145f082 3583 if (TREE_CODE (x) == VAR_DECL
28cbf42c 3584 && TREE_TYPE (x) != error_mark_node
f30432d7
MS
3585 && ((TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
3586 && PROMOTES_TO_AGGR_TYPE (TREE_TYPE (x), ARRAY_TYPE))
3587 /* RTTI TD entries are created while defining the type_info. */
3588 || (TYPE_LANG_SPECIFIC (TREE_TYPE (x))
3589 && TYPE_BEING_DEFINED (TREE_TYPE (x)))))
3590 b->incomplete = tree_cons (NULL_TREE, x, b->incomplete);
8d08fdba
MS
3591 }
3592
8d08fdba
MS
3593 /* Put decls on list in reverse order.
3594 We will reverse them later if necessary. */
3595 TREE_CHAIN (x) = b->names;
3596 b->names = x;
3597 if (! (b != global_binding_level || TREE_PERMANENT (x)))
3598 my_friendly_abort (124);
3599
3600 return x;
3601}
3602
5566b478
MS
3603/* Same as pushdecl, but define X in binding-level LEVEL. We rely on the
3604 caller to set DECL_CONTEXT properly. */
8d08fdba
MS
3605
3606static tree
3607pushdecl_with_scope (x, level)
3608 tree x;
3609 struct binding_level *level;
3610{
8d019cef 3611 register struct binding_level *b;
5566b478 3612 tree function_decl = current_function_decl;
8d08fdba 3613
5566b478 3614 current_function_decl = NULL_TREE;
8d019cef
JM
3615 if (level->parm_flag == 2)
3616 {
3617 b = class_binding_level;
3618 class_binding_level = level;
3619 pushdecl_class_level (x);
3620 class_binding_level = b;
3621 }
3622 else
3623 {
3624 b = current_binding_level;
3625 current_binding_level = level;
3626 x = pushdecl (x);
3627 current_binding_level = b;
3628 }
5566b478 3629 current_function_decl = function_decl;
8d08fdba
MS
3630 return x;
3631}
3632
2c73f9f5 3633/* Like pushdecl, only it places X in the current namespace,
8d08fdba 3634 if appropriate. */
e92cc029 3635
8d08fdba 3636tree
2c73f9f5 3637pushdecl_namespace_level (x)
8d08fdba
MS
3638 tree x;
3639{
3640 register struct binding_level *b = inner_binding_level;
2c73f9f5
ML
3641 register tree t;
3642
3643 t = pushdecl_with_scope (x, NAMESPACE_LEVEL (current_namespace));
8d08fdba
MS
3644
3645 /* Now, the type_shadowed stack may screw us. Munge it so it does
3646 what we want. */
3647 if (TREE_CODE (x) == TYPE_DECL)
3648 {
3649 tree name = DECL_NAME (x);
3650 tree newval;
3651 tree *ptr = (tree *)0;
3652 for (; b != global_binding_level; b = b->level_chain)
3653 {
3654 tree shadowed = b->type_shadowed;
3655 for (; shadowed; shadowed = TREE_CHAIN (shadowed))
3656 if (TREE_PURPOSE (shadowed) == name)
3657 {
3658 ptr = &TREE_VALUE (shadowed);
3659 /* Can't break out of the loop here because sometimes
3660 a binding level will have duplicate bindings for
3661 PT names. It's gross, but I haven't time to fix it. */
3662 }
3663 }
3664 newval = TREE_TYPE (x);
3665 if (ptr == (tree *)0)
3666 {
3667 /* @@ This shouldn't be needed. My test case "zstring.cc" trips
3668 up here if this is changed to an assertion. --KR */
3669 SET_IDENTIFIER_TYPE_VALUE (name, newval);
3670 }
3671 else
3672 {
8d08fdba
MS
3673 *ptr = newval;
3674 }
3675 }
3676 return t;
3677}
3678
2c73f9f5
ML
3679/* Like pushdecl, only it places X in GLOBAL_BINDING_LEVEL,
3680 if appropriate. */
3681
3682tree
3683pushdecl_top_level (x)
3684 tree x;
3685{
3686 tree cur_namespace = current_namespace;
3687 current_namespace = global_namespace;
3688 x = pushdecl_namespace_level (x);
3689 current_namespace = cur_namespace;
3690 return x;
3691}
3692
8d08fdba
MS
3693/* Like push_overloaded_decl, only it places X in GLOBAL_BINDING_LEVEL,
3694 if appropriate. */
e92cc029 3695
bd6dd845 3696static void
8d08fdba
MS
3697push_overloaded_decl_top_level (x, forget)
3698 tree x;
3699 int forget;
3700{
3701 struct binding_level *b = current_binding_level;
3702
3703 current_binding_level = global_binding_level;
3704 push_overloaded_decl (x, forget);
3705 current_binding_level = b;
3706}
3707
3708/* Make the declaration of X appear in CLASS scope. */
e92cc029 3709
8d08fdba
MS
3710tree
3711pushdecl_class_level (x)
3712 tree x;
3713{
3714 /* Don't use DECL_ASSEMBLER_NAME here! Everything that looks in class
3715 scope looks for the pre-mangled name. */
3716 register tree name = DECL_NAME (x);
3717
3718 if (name)
3719 {
8d2733ca
MS
3720 if (TYPE_BEING_DEFINED (current_class_type))
3721 {
3722 /* Check for inconsistent use of this name in the class body.
3723 Types, enums, and static vars are checked here; other
3724 members are checked in finish_struct. */
3725 tree icv = IDENTIFIER_CLASS_VALUE (name);
3726
5566b478 3727 if (icv && icv != x
a4443a08
MS
3728 /* Don't complain about inherited names. */
3729 && id_in_current_class (name)
3730 /* Or shadowed tags. */
3731 && !(TREE_CODE (icv) == TYPE_DECL
3732 && DECL_CONTEXT (icv) == current_class_type))
8d2733ca
MS
3733 {
3734 cp_error ("declaration of identifier `%D' as `%#D'", name, x);
3735 cp_error_at ("conflicts with previous use in class as `%#D'",
3736 icv);
3737 }
3738 }
3739
8d08fdba
MS
3740 push_class_level_binding (name, x);
3741 if (TREE_CODE (x) == TYPE_DECL)
3742 {
3743 set_identifier_type_value (name, TREE_TYPE (x));
8d08fdba
MS
3744 }
3745 }
3746 return x;
3747}
3748
bd6dd845 3749#if 0
7177d104
MS
3750/* This function is used to push the mangled decls for nested types into
3751 the appropriate scope. Previously pushdecl_top_level was used, but that
3752 is incorrect for members of local classes. */
e92cc029 3753
5566b478 3754void
7177d104
MS
3755pushdecl_nonclass_level (x)
3756 tree x;
3757{
3758 struct binding_level *b = current_binding_level;
3759
7177d104 3760 my_friendly_assert (b->parm_flag != 2, 180);
7177d104 3761
5566b478 3762#if 0
7177d104
MS
3763 /* Get out of template binding levels */
3764 while (b->pseudo_global)
3765 b = b->level_chain;
5566b478 3766#endif
7177d104
MS
3767
3768 pushdecl_with_scope (x, b);
3769}
bd6dd845 3770#endif
7177d104 3771
8d08fdba
MS
3772/* Make the declaration(s) of X appear in CLASS scope
3773 under the name NAME. */
e92cc029 3774
8d08fdba
MS
3775void
3776push_class_level_binding (name, x)
3777 tree name;
3778 tree x;
3779{
98c1c668
JM
3780 /* The class_binding_level will be NULL if x is a template
3781 parameter name in a member template. */
3782 if (!class_binding_level)
3783 return;
3784
e1cd6e56
MS
3785 if (TREE_CODE (x) == TYPE_DECL && DECL_ARTIFICIAL (x)
3786 && purpose_member (name, class_binding_level->class_shadowed))
3787 return;
3788
8d08fdba
MS
3789 maybe_push_cache_obstack ();
3790 class_binding_level->class_shadowed
3791 = tree_cons (name, IDENTIFIER_CLASS_VALUE (name),
3792 class_binding_level->class_shadowed);
3793 pop_obstacks ();
3794 IDENTIFIER_CLASS_VALUE (name) = x;
3795 obstack_ptr_grow (&decl_obstack, x);
3796}
3797
2c73f9f5
ML
3798/* Insert another USING_DECL into the current binding level,
3799 returning this declaration. If this is a redeclaration,
3800 do nothing and return NULL_TREE. */
e92cc029 3801
2c73f9f5
ML
3802tree
3803push_using_decl (scope, name)
3804 tree scope;
3805 tree name;
8d08fdba 3806{
2c73f9f5
ML
3807 tree decl;
3808
3809 if (!toplevel_bindings_p ())
3810 {
3811 sorry ("using declaration inside function");
3812 return NULL_TREE;
3813 }
8d08fdba 3814
2c73f9f5
ML
3815 my_friendly_assert (TREE_CODE (scope) == NAMESPACE_DECL, 383);
3816 my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, 384);
3817 for (decl = current_binding_level->usings; decl; decl = TREE_CHAIN (decl))
3818 if (DECL_INITIAL (decl) == scope && DECL_NAME (decl) == name)
3819 break;
3820 if (decl)
3821 return NULL_TREE;
3822 decl = build_lang_decl (USING_DECL, name, void_type_node);
3823 DECL_INITIAL (decl) = scope;
3824 TREE_CHAIN (decl) = current_binding_level->usings;
3825 current_binding_level->usings = decl;
3826 return decl;
8d08fdba
MS
3827}
3828
700f8a87
MS
3829/* DECL is a FUNCTION_DECL which may have other definitions already in
3830 place. We get around this by making the value of the identifier point
3831 to a list of all the things that want to be referenced by that name. It
3832 is then up to the users of that name to decide what to do with that
8d08fdba
MS
3833 list.
3834
3835 DECL may also be a TEMPLATE_DECL, with a FUNCTION_DECL in its DECL_RESULT
3836 slot. It is dealt with the same way.
3837
3838 The value returned may be a previous declaration if we guessed wrong
3839 about what language DECL should belong to (C or C++). Otherwise,
3840 it's always DECL (and never something that's not a _DECL). */
e92cc029 3841
bd6dd845 3842static tree
8d08fdba
MS
3843push_overloaded_decl (decl, forgettable)
3844 tree decl;
3845 int forgettable;
3846{
3847 tree orig_name = DECL_NAME (decl);
700f8a87 3848 tree old;
2c73f9f5 3849 int doing_global = (namespace_bindings_p () || ! forgettable);
700f8a87
MS
3850
3851 if (doing_global)
3852 {
2c73f9f5 3853 old = namespace_binding (orig_name, DECL_CONTEXT (decl));
700f8a87 3854 if (old && TREE_CODE (old) == FUNCTION_DECL
a4443a08 3855 && DECL_ARTIFICIAL (old)
700f8a87
MS
3856 && (DECL_BUILT_IN (old) || DECL_BUILT_IN_NONANSI (old)))
3857 {
a4443a08
MS
3858 if (duplicate_decls (decl, old))
3859 return old;
700f8a87
MS
3860 old = NULL_TREE;
3861 }
3862 }
3863 else
3864 {
3865 old = IDENTIFIER_LOCAL_VALUE (orig_name);
a4443a08 3866
700f8a87
MS
3867 if (! purpose_member (orig_name, current_binding_level->shadowed))
3868 {
3869 current_binding_level->shadowed
3870 = tree_cons (orig_name, old, current_binding_level->shadowed);
3871 old = NULL_TREE;
3872 }
3873 }
8d08fdba 3874
700f8a87 3875 if (old)
8d08fdba 3876 {
e1cd6e56 3877 if (TREE_CODE (old) == TYPE_DECL && DECL_ARTIFICIAL (old))
8926095f 3878 {
700f8a87 3879 tree t = TREE_TYPE (old);
cdf5b885
MS
3880 if (IS_AGGR_TYPE (t) && warn_shadow
3881 && (! DECL_IN_SYSTEM_HEADER (decl)
3882 || ! DECL_IN_SYSTEM_HEADER (old)))
8926095f 3883 cp_warning ("`%#D' hides constructor for `%#T'", decl, t);
5b605f68 3884 old = NULL_TREE;
8926095f 3885 }
700f8a87 3886 else if (is_overloaded_fn (old))
8d08fdba 3887 {
8d08fdba
MS
3888 tree tmp;
3889
2c73f9f5
ML
3890 for (tmp = old; tmp; tmp = OVL_NEXT (tmp))
3891 if (decl == OVL_CURRENT (tmp)
3892 || duplicate_decls (decl, OVL_CURRENT (tmp)))
3893 return OVL_CURRENT (tmp);
8d08fdba 3894 }
e1cd6e56
MS
3895 else
3896 {
3897 cp_error_at ("previous non-function declaration `%#D'", old);
3898 cp_error ("conflicts with function declaration `%#D'", decl);
a9aedbc2 3899 return decl;
e1cd6e56 3900 }
8d08fdba 3901 }
7177d104 3902
700f8a87 3903 if (old || TREE_CODE (decl) == TEMPLATE_DECL)
8d08fdba 3904 {
2c73f9f5
ML
3905 if (old && TREE_CODE (old) != OVERLOAD)
3906 old = ovl_cons (old, NULL_TREE);
3907 old = ovl_cons (decl, old);
8d08fdba
MS
3908 }
3909 else
3910 /* orig_name is not ambiguous. */
700f8a87
MS
3911 old = decl;
3912
3913 if (doing_global)
2c73f9f5 3914 set_namespace_binding (orig_name, DECL_CONTEXT (decl), old);
700f8a87
MS
3915 else
3916 IDENTIFIER_LOCAL_VALUE (orig_name) = old;
3917
8d08fdba
MS
3918 return decl;
3919}
3920\f
3921/* Generate an implicit declaration for identifier FUNCTIONID
3922 as a function of type int (). Print a warning if appropriate. */
3923
3924tree
3925implicitly_declare (functionid)
3926 tree functionid;
3927{
3928 register tree decl;
3929 int temp = allocation_temporary_p ();
3930
3931 push_obstacks_nochange ();
3932
3933 /* Save the decl permanently so we can warn if definition follows.
3934 In ANSI C, warn_implicit is usually false, so the saves little space.
3935 But in C++, it's usually true, hence the extra code. */
d22c8596 3936 if (temp && (! warn_implicit || toplevel_bindings_p ()))
8d08fdba
MS
3937 end_temporary_allocation ();
3938
3939 /* We used to reuse an old implicit decl here,
3940 but this loses with inline functions because it can clobber
3941 the saved decl chains. */
3942 decl = build_lang_decl (FUNCTION_DECL, functionid, default_function_type);
3943
3944 DECL_EXTERNAL (decl) = 1;
3945 TREE_PUBLIC (decl) = 1;
3946
3947 /* ANSI standard says implicit declarations are in the innermost block.
d22c8596 3948 So we record the decl in the standard fashion. */
8d08fdba
MS
3949 pushdecl (decl);
3950 rest_of_decl_compilation (decl, NULL_PTR, 0, 0);
3951
3952 if (warn_implicit
3953 /* Only one warning per identifier. */
3954 && IDENTIFIER_IMPLICIT_DECL (functionid) == NULL_TREE)
3955 {
3956 cp_pedwarn ("implicit declaration of function `%#D'", decl);
3957 }
3958
3959 SET_IDENTIFIER_IMPLICIT_DECL (functionid, decl);
3960
3961 pop_obstacks ();
3962
3963 return decl;
3964}
3965
3966/* Return zero if the declaration NEWDECL is valid
3967 when the declaration OLDDECL (assumed to be for the same name)
3968 has already been seen.
3969 Otherwise return an error message format string with a %s
3970 where the identifier should go. */
3971
3972static char *
3973redeclaration_error_message (newdecl, olddecl)
3974 tree newdecl, olddecl;
3975{
3976 if (TREE_CODE (newdecl) == TYPE_DECL)
3977 {
3978 /* Because C++ can put things into name space for free,
3979 constructs like "typedef struct foo { ... } foo"
3980 would look like an erroneous redeclaration. */
8926095f 3981 if (comptypes (TREE_TYPE (newdecl), TREE_TYPE (olddecl), 0))
8d08fdba
MS
3982 return 0;
3983 else
51c184be 3984 return "redefinition of `%#D'";
8d08fdba
MS
3985 }
3986 else if (TREE_CODE (newdecl) == FUNCTION_DECL)
3987 {
3988 /* If this is a pure function, its olddecl will actually be
3989 the original initialization to `0' (which we force to call
3990 abort()). Don't complain about redefinition in this case. */
3991 if (DECL_LANG_SPECIFIC (olddecl) && DECL_ABSTRACT_VIRTUAL_P (olddecl))
3992 return 0;
3993
2c73f9f5
ML
3994 /* If both functions come from different namespaces, this is not
3995 a redeclaration - this is a conflict with a used function. */
6eb3bb27 3996 if (DECL_NAMESPACE_SCOPE_P (olddecl)
2c73f9f5
ML
3997 && DECL_CONTEXT (olddecl) != DECL_CONTEXT (newdecl))
3998 return "`%D' conflicts with used function";
3999
db5ae43f
MS
4000 /* We'll complain about linkage mismatches in
4001 warn_extern_redeclared_static. */
4002
2c73f9f5 4003 /* Defining the same name twice is no good. */
8d08fdba 4004 if (DECL_INITIAL (olddecl) != NULL_TREE
faae18ab 4005 && DECL_INITIAL (newdecl) != NULL_TREE)
8d08fdba
MS
4006 {
4007 if (DECL_NAME (olddecl) == NULL_TREE)
51c184be 4008 return "`%#D' not declared in class";
8d08fdba 4009 else
51c184be 4010 return "redefinition of `%#D'";
8d08fdba
MS
4011 }
4012 return 0;
4013 }
51c184be
MS
4014 else if (TREE_CODE (newdecl) == TEMPLATE_DECL)
4015 {
ec255269
MS
4016 if ((TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == FUNCTION_DECL
4017 && DECL_INITIAL (DECL_TEMPLATE_RESULT (newdecl))
4018 && DECL_INITIAL (DECL_TEMPLATE_RESULT (olddecl)))
4019 || (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL
4020 && TYPE_SIZE (TREE_TYPE (newdecl))
4021 && TYPE_SIZE (TREE_TYPE (olddecl))))
51c184be
MS
4022 return "redefinition of `%#D'";
4023 return 0;
4024 }
5566b478 4025 else if (toplevel_bindings_p ())
8d08fdba
MS
4026 {
4027 /* Objects declared at top level: */
4028 /* If at least one is a reference, it's ok. */
4029 if (DECL_EXTERNAL (newdecl) || DECL_EXTERNAL (olddecl))
4030 return 0;
8926095f
MS
4031 /* Reject two definitions. */
4032 return "redefinition of `%#D'";
8d08fdba
MS
4033 }
4034 else
4035 {
4036 /* Objects declared with block scope: */
4037 /* Reject two definitions, and reject a definition
4038 together with an external reference. */
4039 if (!(DECL_EXTERNAL (newdecl) && DECL_EXTERNAL (olddecl)))
51c184be 4040 return "redeclaration of `%#D'";
8d08fdba
MS
4041 return 0;
4042 }
4043}
4044\f
4045/* Get the LABEL_DECL corresponding to identifier ID as a label.
4046 Create one if none exists so far for the current function.
4047 This function is called for both label definitions and label references. */
4048
4049tree
4050lookup_label (id)
4051 tree id;
4052{
4053 register tree decl = IDENTIFIER_LABEL_VALUE (id);
4054
4055 if (current_function_decl == NULL_TREE)
4056 {
4057 error ("label `%s' referenced outside of any function",
4058 IDENTIFIER_POINTER (id));
4059 return NULL_TREE;
4060 }
4061
4062 if ((decl == NULL_TREE
4063 || DECL_SOURCE_LINE (decl) == 0)
e349ee73
MS
4064 && (named_label_uses == NULL
4065 || named_label_uses->names_in_scope != current_binding_level->names
4066 || named_label_uses->label_decl != decl))
4067 {
4068 struct named_label_list *new_ent;
4069 new_ent
4070 = (struct named_label_list*)oballoc (sizeof (struct named_label_list));
4071 new_ent->label_decl = decl;
4072 new_ent->names_in_scope = current_binding_level->names;
4073 new_ent->binding_level = current_binding_level;
4074 new_ent->lineno_o_goto = lineno;
4075 new_ent->filename_o_goto = input_filename;
4076 new_ent->next = named_label_uses;
4077 named_label_uses = new_ent;
8d08fdba
MS
4078 }
4079
4080 /* Use a label already defined or ref'd with this name. */
4081 if (decl != NULL_TREE)
4082 {
4083 /* But not if it is inherited and wasn't declared to be inheritable. */
4084 if (DECL_CONTEXT (decl) != current_function_decl
4085 && ! C_DECLARED_LABEL_FLAG (decl))
4086 return shadow_label (id);
4087 return decl;
4088 }
4089
4090 decl = build_decl (LABEL_DECL, id, void_type_node);
4091
cffa8729
MS
4092 /* Make sure every label has an rtx. */
4093 label_rtx (decl);
4094
8d08fdba
MS
4095 /* A label not explicitly declared must be local to where it's ref'd. */
4096 DECL_CONTEXT (decl) = current_function_decl;
4097
4098 DECL_MODE (decl) = VOIDmode;
4099
4100 /* Say where one reference is to the label,
4101 for the sake of the error if it is not defined. */
4102 DECL_SOURCE_LINE (decl) = lineno;
4103 DECL_SOURCE_FILE (decl) = input_filename;
4104
4105 SET_IDENTIFIER_LABEL_VALUE (id, decl);
4106
4107 named_labels = tree_cons (NULL_TREE, decl, named_labels);
e349ee73 4108 named_label_uses->label_decl = decl;
8d08fdba
MS
4109
4110 return decl;
4111}
4112
4113/* Make a label named NAME in the current function,
4114 shadowing silently any that may be inherited from containing functions
4115 or containing scopes.
4116
4117 Note that valid use, if the label being shadowed
4118 comes from another scope in the same function,
4119 requires calling declare_nonlocal_label right away. */
4120
4121tree
4122shadow_label (name)
4123 tree name;
4124{
4125 register tree decl = IDENTIFIER_LABEL_VALUE (name);
4126
4127 if (decl != NULL_TREE)
4128 {
4129 shadowed_labels = tree_cons (NULL_TREE, decl, shadowed_labels);
4130 SET_IDENTIFIER_LABEL_VALUE (name, NULL_TREE);
4131 SET_IDENTIFIER_LABEL_VALUE (decl, NULL_TREE);
4132 }
4133
4134 return lookup_label (name);
4135}
4136
4137/* Define a label, specifying the location in the source file.
4138 Return the LABEL_DECL node for the label, if the definition is valid.
4139 Otherwise return 0. */
4140
4141tree
4142define_label (filename, line, name)
4143 char *filename;
4144 int line;
4145 tree name;
4146{
5566b478
MS
4147 tree decl;
4148
4149 if (minimal_parse_mode)
4150 {
4151 push_obstacks (&permanent_obstack, &permanent_obstack);
4152 decl = build_decl (LABEL_DECL, name, void_type_node);
4153 pop_obstacks ();
4154 DECL_SOURCE_LINE (decl) = line;
4155 DECL_SOURCE_FILE (decl) = filename;
4156 add_tree (decl);
4157 return decl;
4158 }
4159
4160 decl = lookup_label (name);
8d08fdba
MS
4161
4162 /* After labels, make any new cleanups go into their
4163 own new (temporary) binding contour. */
4164 current_binding_level->more_cleanups_ok = 0;
4165
4166 /* If label with this name is known from an outer context, shadow it. */
4167 if (decl != NULL_TREE && DECL_CONTEXT (decl) != current_function_decl)
4168 {
4169 shadowed_labels = tree_cons (NULL_TREE, decl, shadowed_labels);
4170 SET_IDENTIFIER_LABEL_VALUE (name, NULL_TREE);
4171 decl = lookup_label (name);
4172 }
4173
e1cd6e56
MS
4174 if (name == get_identifier ("wchar_t"))
4175 cp_pedwarn ("label named wchar_t");
4176
8d08fdba
MS
4177 if (DECL_INITIAL (decl) != NULL_TREE)
4178 {
4179 cp_error ("duplicate label `%D'", decl);
4180 return 0;
4181 }
4182 else
4183 {
e349ee73 4184 struct named_label_list *uses, *prev;
28cbf42c 4185 int identified = 0;
8d08fdba
MS
4186
4187 /* Mark label as having been defined. */
4188 DECL_INITIAL (decl) = error_mark_node;
4189 /* Say where in the source. */
4190 DECL_SOURCE_FILE (decl) = filename;
4191 DECL_SOURCE_LINE (decl) = line;
4192
e349ee73
MS
4193 prev = NULL;
4194 uses = named_label_uses;
4195 while (uses != NULL)
4196 if (uses->label_decl == decl)
8d08fdba
MS
4197 {
4198 struct binding_level *b = current_binding_level;
4199 while (b)
4200 {
4201 tree new_decls = b->names;
e349ee73
MS
4202 tree old_decls = (b == uses->binding_level)
4203 ? uses->names_in_scope : NULL_TREE;
8d08fdba
MS
4204 while (new_decls != old_decls)
4205 {
4206 if (TREE_CODE (new_decls) == VAR_DECL
4207 /* Don't complain about crossing initialization
4208 of internal entities. They can't be accessed,
4209 and they should be cleaned up
4210 by the time we get to the label. */
700f8a87 4211 && ! DECL_ARTIFICIAL (new_decls)
8d08fdba
MS
4212 && ((DECL_INITIAL (new_decls) != NULL_TREE
4213 && DECL_INITIAL (new_decls) != error_mark_node)
4214 || TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (new_decls))))
4215 {
e349ee73
MS
4216 if (! identified)
4217 {
4218 cp_error ("jump to label `%D'", decl);
4219 error_with_file_and_line (uses->filename_o_goto,
4220 uses->lineno_o_goto,
4221 " from here");
4222 identified = 1;
4223 }
28cbf42c
MS
4224 cp_error_at (" crosses initialization of `%#D'",
4225 new_decls);
8d08fdba
MS
4226 }
4227 new_decls = TREE_CHAIN (new_decls);
4228 }
e349ee73 4229 if (b == uses->binding_level)
8d08fdba
MS
4230 break;
4231 b = b->level_chain;
4232 }
4233
e349ee73
MS
4234 if (prev != NULL)
4235 prev->next = uses->next;
8d08fdba 4236 else
e349ee73
MS
4237 named_label_uses = uses->next;
4238
4239 uses = uses->next;
4240 }
4241 else
4242 {
4243 prev = uses;
4244 uses = uses->next;
8d08fdba
MS
4245 }
4246 current_function_return_value = NULL_TREE;
4247 return decl;
4248 }
4249}
4250
a5894242
MS
4251struct cp_switch
4252{
4253 struct binding_level *level;
4254 struct cp_switch *next;
4255};
4256
4257static struct cp_switch *switch_stack;
4258
4259void
4260push_switch ()
4261{
4262 struct cp_switch *p
4263 = (struct cp_switch *) oballoc (sizeof (struct cp_switch));
4264 p->level = current_binding_level;
4265 p->next = switch_stack;
4266 switch_stack = p;
4267}
4268
4269void
4270pop_switch ()
4271{
4272 switch_stack = switch_stack->next;
4273}
4274
8d08fdba
MS
4275/* Same, but for CASE labels. If DECL is NULL_TREE, it's the default. */
4276/* XXX Note decl is never actually used. (bpk) */
e92cc029 4277
8d08fdba
MS
4278void
4279define_case_label (decl)
4280 tree decl;
4281{
4282 tree cleanup = last_cleanup_this_contour ();
a5894242
MS
4283 struct binding_level *b = current_binding_level;
4284 int identified = 0;
4285
8d08fdba
MS
4286 if (cleanup)
4287 {
4288 static int explained = 0;
a5894242
MS
4289 cp_warning_at ("destructor needed for `%#D'", TREE_PURPOSE (cleanup));
4290 warning ("where case label appears here");
8d08fdba
MS
4291 if (!explained)
4292 {
a5894242
MS
4293 warning ("(enclose actions of previous case statements requiring");
4294 warning ("destructors in their own binding contours.)");
8d08fdba
MS
4295 explained = 1;
4296 }
4297 }
4298
a5894242
MS
4299 for (; b && b != switch_stack->level; b = b->level_chain)
4300 {
4301 tree new_decls = b->names;
4302 for (; new_decls; new_decls = TREE_CHAIN (new_decls))
4303 {
4304 if (TREE_CODE (new_decls) == VAR_DECL
4305 /* Don't complain about crossing initialization
4306 of internal entities. They can't be accessed,
4307 and they should be cleaned up
4308 by the time we get to the label. */
4309 && ! DECL_ARTIFICIAL (new_decls)
4310 && ((DECL_INITIAL (new_decls) != NULL_TREE
4311 && DECL_INITIAL (new_decls) != error_mark_node)
4312 || TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (new_decls))))
4313 {
4314 if (! identified)
4315 error ("jump to case label");
4316 identified = 1;
4317 cp_error_at (" crosses initialization of `%#D'",
4318 new_decls);
4319 }
4320 }
4321 }
4322
8d08fdba
MS
4323 /* After labels, make any new cleanups go into their
4324 own new (temporary) binding contour. */
4325
4326 current_binding_level->more_cleanups_ok = 0;
4327 current_function_return_value = NULL_TREE;
4328}
4329\f
4330/* Return the list of declarations of the current level.
4331 Note that this list is in reverse order unless/until
4332 you nreverse it; and when you do nreverse it, you must
4333 store the result back using `storedecls' or you will lose. */
4334
4335tree
4336getdecls ()
4337{
4338 return current_binding_level->names;
4339}
4340
4341/* Return the list of type-tags (for structs, etc) of the current level. */
4342
4343tree
4344gettags ()
4345{
4346 return current_binding_level->tags;
4347}
4348
4349/* Store the list of declarations of the current level.
4350 This is done for the parameter declarations of a function being defined,
4351 after they are modified in the light of any missing parameters. */
4352
4353static void
4354storedecls (decls)
4355 tree decls;
4356{
4357 current_binding_level->names = decls;
4358}
4359
4360/* Similarly, store the list of tags of the current level. */
4361
4362static void
4363storetags (tags)
4364 tree tags;
4365{
4366 current_binding_level->tags = tags;
4367}
4368\f
4369/* Given NAME, an IDENTIFIER_NODE,
4370 return the structure (or union or enum) definition for that name.
4371 Searches binding levels from BINDING_LEVEL up to the global level.
4372 If THISLEVEL_ONLY is nonzero, searches only the specified context
4373 (but skips any tag-transparent contexts to find one that is
4374 meaningful for tags).
4375 FORM says which kind of type the caller wants;
4376 it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
4377 If the wrong kind of type is found, and it's not a template, an error is
4378 reported. */
4379
4380static tree
4381lookup_tag (form, name, binding_level, thislevel_only)
4382 enum tree_code form;
8d08fdba 4383 tree name;
cffa8729 4384 struct binding_level *binding_level;
8d08fdba
MS
4385 int thislevel_only;
4386{
4387 register struct binding_level *level;
4388
4389 for (level = binding_level; level; level = level->level_chain)
4390 {
4391 register tree tail;
4392 if (ANON_AGGRNAME_P (name))
4393 for (tail = level->tags; tail; tail = TREE_CHAIN (tail))
4394 {
4395 /* There's no need for error checking here, because
4396 anon names are unique throughout the compilation. */
4397 if (TYPE_IDENTIFIER (TREE_VALUE (tail)) == name)
4398 return TREE_VALUE (tail);
4399 }
2c73f9f5
ML
4400 else if (level->namespace_p)
4401 /* Do namespace lookup. */
4402 /* XXX: is this a real lookup, considering using-directives etc. ??? */
4403 for (tail = current_namespace; 1; tail = DECL_CONTEXT (tail))
4404 {
4405 tree old = BINDING_TYPE (binding_for_name (name, tail));
4406 /* If it has an original type, it is a typedef, and we
4407 should not return it. */
4408 if (old && DECL_ORIGINAL_TYPE (TYPE_NAME (old)))
4409 old = NULL_TREE;
4410 if (old && TREE_CODE (old) != form
4411 && !(form != ENUMERAL_TYPE && TREE_CODE (old) == TEMPLATE_DECL))
4412 {
4413 cp_error ("`%#D' redeclared as %C", old, form);
4414 return NULL_TREE;
4415 }
4416 if (old)
4417 return old;
4418 if (thislevel_only || tail == global_namespace)
4419 return NULL_TREE;
4420 }
8d08fdba
MS
4421 else
4422 for (tail = level->tags; tail; tail = TREE_CHAIN (tail))
4423 {
a80e4195 4424 if (TREE_PURPOSE (tail) == name)
8d08fdba
MS
4425 {
4426 enum tree_code code = TREE_CODE (TREE_VALUE (tail));
4427 /* Should tighten this up; it'll probably permit
4428 UNION_TYPE and a struct template, for example. */
4429 if (code != form
5566b478 4430 && !(form != ENUMERAL_TYPE && code == TEMPLATE_DECL))
8d08fdba
MS
4431 {
4432 /* Definition isn't the kind we were looking for. */
4433 cp_error ("`%#D' redeclared as %C", TREE_VALUE (tail),
4434 form);
72b7eeff 4435 return NULL_TREE;
8d08fdba
MS
4436 }
4437 return TREE_VALUE (tail);
4438 }
4439 }
4440 if (thislevel_only && ! level->tag_transparent)
5566b478
MS
4441 {
4442 if (level->pseudo_global)
4443 {
30394414 4444 tree t = IDENTIFIER_NAMESPACE_VALUE (name);
5566b478
MS
4445 if (t && TREE_CODE (t) == TEMPLATE_DECL
4446 && TREE_CODE (DECL_TEMPLATE_RESULT (t)) == TYPE_DECL)
4447 return TREE_TYPE (t);
4448 }
4449 return NULL_TREE;
4450 }
2c73f9f5 4451 if (current_class_type && level->level_chain->namespace_p)
8d08fdba
MS
4452 {
4453 /* Try looking in this class's tags before heading into
4454 global binding level. */
4455 tree context = current_class_type;
4456 while (context)
4457 {
4458 switch (TREE_CODE_CLASS (TREE_CODE (context)))
4459 {
4460 tree these_tags;
4461 case 't':
4462 these_tags = CLASSTYPE_TAGS (context);
4463 if (ANON_AGGRNAME_P (name))
4464 while (these_tags)
4465 {
4466 if (TYPE_IDENTIFIER (TREE_VALUE (these_tags))
4467 == name)
4468 return TREE_VALUE (tail);
4469 these_tags = TREE_CHAIN (these_tags);
4470 }
4471 else
4472 while (these_tags)
4473 {
4474 if (TREE_PURPOSE (these_tags) == name)
4475 {
4476 if (TREE_CODE (TREE_VALUE (these_tags)) != form)
4477 {
4478 cp_error ("`%#D' redeclared as %C in class scope",
4479 TREE_VALUE (tail), form);
72b7eeff 4480 return NULL_TREE;
8d08fdba
MS
4481 }
4482 return TREE_VALUE (tail);
4483 }
4484 these_tags = TREE_CHAIN (these_tags);
4485 }
4486 /* If this type is not yet complete, then don't
4487 look at its context. */
4488 if (TYPE_SIZE (context) == NULL_TREE)
4489 goto no_context;
4490 /* Go to next enclosing type, if any. */
d2e5ee5c 4491 context = DECL_CONTEXT (TYPE_MAIN_DECL (context));
8d08fdba
MS
4492 break;
4493 case 'd':
4494 context = DECL_CONTEXT (context);
4495 break;
4496 default:
4497 my_friendly_abort (10);
4498 }
4499 continue;
4500 no_context:
4501 break;
4502 }
4503 }
4504 }
4505 return NULL_TREE;
4506}
4507
bd6dd845 4508#if 0
8d08fdba
MS
4509void
4510set_current_level_tags_transparency (tags_transparent)
4511 int tags_transparent;
4512{
4513 current_binding_level->tag_transparent = tags_transparent;
4514}
bd6dd845 4515#endif
8d08fdba
MS
4516
4517/* Given a type, find the tag that was defined for it and return the tag name.
4518 Otherwise return 0. However, the value can never be 0
4519 in the cases in which this is used.
4520
4521 C++: If NAME is non-zero, this is the new name to install. This is
4522 done when replacing anonymous tags with real tag names. */
4523
4524static tree
4525lookup_tag_reverse (type, name)
4526 tree type;
4527 tree name;
4528{
4529 register struct binding_level *level;
4530
4531 for (level = current_binding_level; level; level = level->level_chain)
4532 {
4533 register tree tail;
4534 for (tail = level->tags; tail; tail = TREE_CHAIN (tail))
4535 {
4536 if (TREE_VALUE (tail) == type)
4537 {
4538 if (name)
4539 TREE_PURPOSE (tail) = name;
4540 return TREE_PURPOSE (tail);
4541 }
4542 }
4543 }
4544 return NULL_TREE;
4545}
8d08fdba
MS
4546\f
4547/* Lookup TYPE in CONTEXT (a chain of nested types or a FUNCTION_DECL).
4548 Return the type value, or NULL_TREE if not found. */
e92cc029 4549
8d08fdba
MS
4550static tree
4551lookup_nested_type (type, context)
4552 tree type;
4553 tree context;
4554{
4555 if (context == NULL_TREE)
4556 return NULL_TREE;
4557 while (context)
4558 {
4559 switch (TREE_CODE (context))
4560 {
4561 case TYPE_DECL:
4562 {
4563 tree ctype = TREE_TYPE (context);
4564 tree match = value_member (type, CLASSTYPE_TAGS (ctype));
4565 if (match)
4566 return TREE_VALUE (match);
4567 context = DECL_CONTEXT (context);
4568
4569 /* When we have a nested class whose member functions have
4570 local types (e.g., a set of enums), we'll arrive here
4571 with the DECL_CONTEXT as the actual RECORD_TYPE node for
4572 the enclosing class. Instead, we want to make sure we
4573 come back in here with the TYPE_DECL, not the RECORD_TYPE. */
4574 if (context && TREE_CODE (context) == RECORD_TYPE)
4575 context = TREE_CHAIN (context);
4576 }
4577 break;
4578 case FUNCTION_DECL:
e1cd6e56
MS
4579 if (TYPE_NAME (type) && TYPE_IDENTIFIER (type))
4580 return lookup_name (TYPE_IDENTIFIER (type), 1);
4581 return NULL_TREE;
8d08fdba
MS
4582 default:
4583 my_friendly_abort (12);
4584 }
4585 }
4586 return NULL_TREE;
4587}
4588
a9aedbc2 4589/* Look up NAME in the NAMESPACE. */
e92cc029 4590
a9aedbc2
MS
4591tree
4592lookup_namespace_name (namespace, name)
4593 tree namespace, name;
4594{
2c73f9f5 4595 struct tree_binding _b;
30394414 4596 tree val;
2c73f9f5 4597
30394414
JM
4598 my_friendly_assert (TREE_CODE (namespace) == NAMESPACE_DECL, 370);
4599 my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, 373);
4600
2c73f9f5
ML
4601 val = binding_init (&_b);
4602 if (!qualified_lookup_using_namespace (name, namespace, val))
4603 return error_mark_node;
4604
4605 if (BINDING_VALUE (val))
4606 return BINDING_VALUE (val);
30394414
JM
4607 cp_error ("`%D' undeclared in namespace `%D'", name, namespace);
4608 return error_mark_node;
a9aedbc2
MS
4609}
4610
5566b478
MS
4611tree
4612make_typename_type (context, name)
4613 tree context, name;
4614{
a80e4195 4615 tree t, d;
b2b7d40a 4616 tree fullname;
a80e4195 4617
653cc74a
JM
4618 if (TREE_CODE_CLASS (TREE_CODE (name)) == 't')
4619 name = TYPE_IDENTIFIER (name);
4620 else if (TREE_CODE (name) == TYPE_DECL)
a80e4195 4621 name = DECL_NAME (name);
b2b7d40a
JM
4622
4623 fullname = name;
4624
4625 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
11686454
JM
4626 {
4627 name = TREE_OPERAND (name, 0);
4628 if (TREE_CODE (name) == TEMPLATE_DECL)
4629 name = TREE_OPERAND (fullname, 0) = DECL_NAME (name);
4630 }
b2b7d40a 4631 if (TREE_CODE (name) != IDENTIFIER_NODE)
a80e4195 4632 my_friendly_abort (2000);
5566b478 4633
85b71cf2 4634 if (! uses_template_parms (context)
7fcdf4c2 4635 || context == current_class_type)
5566b478 4636 {
b2b7d40a
JM
4637 if (TREE_CODE (fullname) == TEMPLATE_ID_EXPR)
4638 {
4639 if (IS_AGGR_TYPE (context))
4640 t = lookup_field (context, name, 0, 0);
4641 else
4642 t = NULL_TREE;
4643
4644 if (t == NULL_TREE || TREE_CODE (t) != TEMPLATE_DECL
4645 || TREE_CODE (DECL_RESULT (t)) != TYPE_DECL)
4646 {
4647 cp_error ("no class template named `%#T' in `%#T'",
4648 name, context);
4649 return error_mark_node;
4650 }
ffb690bd 4651
b2b7d40a
JM
4652 return lookup_template_class (t, TREE_OPERAND (fullname, 1),
4653 NULL_TREE, context);
4654 }
4655 else
5566b478 4656 {
b2b7d40a
JM
4657 if (IS_AGGR_TYPE (context))
4658 t = lookup_field (context, name, 0, 1);
4659 else
4660 t = NULL_TREE;
4661
4662 if (t == NULL_TREE)
4663 {
4664 cp_error ("no type named `%#T' in `%#T'", name, context);
4665 return error_mark_node;
4666 }
1107c4b3 4667
b2b7d40a 4668 return TREE_TYPE (t);
5566b478 4669 }
5566b478
MS
4670 }
4671
5156628f 4672 if (processing_template_decl)
5566b478
MS
4673 push_obstacks (&permanent_obstack, &permanent_obstack);
4674 t = make_lang_type (TYPENAME_TYPE);
b2b7d40a 4675 TYPENAME_TYPE_FULLNAME (t) = fullname;
5566b478 4676 d = build_decl (TYPE_DECL, name, t);
5156628f 4677 if (processing_template_decl)
5566b478
MS
4678 pop_obstacks ();
4679
4680 TYPE_CONTEXT (t) = context;
d2e5ee5c
MS
4681 TYPE_NAME (TREE_TYPE (d)) = d;
4682 TYPE_STUB_DECL (TREE_TYPE (d)) = d;
5566b478 4683 DECL_CONTEXT (d) = context;
7fcdf4c2 4684 CLASSTYPE_GOT_SEMICOLON (t) = 1;
5566b478
MS
4685
4686 return t;
4687}
4688
2c73f9f5
ML
4689/* Select the right _DECL from multiple choices. */
4690
4691static tree
4692select_decl (binding, prefer_type)
4693 tree binding;
4694 int prefer_type;
4695{
4696 tree val;
4697 val = BINDING_VALUE (binding);
4698 /* If we could have a type and
4699 we have nothing or we need a type and have none. */
4700 if (BINDING_TYPE (binding)
4701 && (!val || (prefer_type && TREE_CODE (val) != TYPE_DECL)))
4702 val = TYPE_STUB_DECL (BINDING_TYPE (binding));
4703 /* Don't return non-types if we really prefer types. */
4704 else if (val && prefer_type > 1 && TREE_CODE (val) != TYPE_DECL
4705 && (!looking_for_template || TREE_CODE (val) != TEMPLATE_DECL))
4706 val = NULL_TREE;
4707 return val;
4708}
4709
8d08fdba
MS
4710/* Look up NAME in the current binding level and its superiors in the
4711 namespace of variables, functions and typedefs. Return a ..._DECL
4712 node of some kind representing its definition if there is only one
4713 such declaration, or return a TREE_LIST with all the overloaded
4714 definitions if there are many, or return 0 if it is undefined.
4715
2c73f9f5
ML
4716 If PREFER_TYPE is > 0, we prefer TYPE_DECLs or namespaces.
4717 If PREFER_TYPE is > 1, we reject non-type decls (e.g. namespaces).
a28e3c7f 4718 If PREFER_TYPE is -2, we're being called from yylex(). (UGLY)
f84b4be9
JM
4719 Otherwise we prefer non-TYPE_DECLs.
4720
4721 If NONCLASS is non-zero, we don't look for the NAME in class scope,
4722 using IDENTIFIER_CLASS_VALUE. */
8d08fdba 4723
824b9a4c 4724static tree
700f8a87 4725lookup_name_real (name, prefer_type, nonclass)
8d08fdba 4726 tree name;
700f8a87 4727 int prefer_type, nonclass;
8d08fdba
MS
4728{
4729 register tree val;
a28e3c7f 4730 int yylex = 0;
e1cd6e56 4731 tree from_obj = NULL_TREE;
e76a2646 4732 tree locval, classval;
8d08fdba 4733
a28e3c7f
MS
4734 if (prefer_type == -2)
4735 {
4736 extern int looking_for_typename;
fc378698 4737 tree type = NULL_TREE;
a28e3c7f
MS
4738
4739 yylex = 1;
4740 prefer_type = looking_for_typename;
e1cd6e56 4741
653cc74a
JM
4742 /* std:: becomes :: for now. */
4743 if (got_scope == std_node)
4744 got_scope = void_type_node;
4745
e1cd6e56
MS
4746 if (got_scope)
4747 type = got_scope;
dff6b454 4748 else if (got_object != error_mark_node)
e1cd6e56 4749 type = got_object;
a28e3c7f 4750
e1cd6e56 4751 if (type)
a28e3c7f 4752 {
e1cd6e56 4753 if (type == error_mark_node)
f376e137 4754 return error_mark_node;
a80e4195
MS
4755 if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type))
4756 type = TREE_TYPE (type);
5566b478
MS
4757
4758 type = complete_type (type);
4759
a1774733 4760 if (TREE_CODE (type) == VOID_TYPE)
2c73f9f5
ML
4761 type = global_namespace;
4762 if (TREE_CODE (type) == NAMESPACE_DECL)
a9aedbc2 4763 {
2c73f9f5
ML
4764 struct tree_binding b;
4765 val = binding_init (&b);
4766 if (!qualified_lookup_using_namespace (name, type, val))
4767 return NULL_TREE;
4768 val = select_decl (val, prefer_type);
a9aedbc2 4769 }
5566b478 4770 else if (! IS_AGGR_TYPE (type)
5156628f 4771 || TREE_CODE (type) == TEMPLATE_TYPE_PARM
73b0fce8 4772 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM
5156628f 4773 || TREE_CODE (type) == TYPENAME_TYPE)
e92cc029 4774 /* Someone else will give an error about this if needed. */
a28e3c7f 4775 val = NULL_TREE;
e1cd6e56 4776 else if (TYPE_BEING_DEFINED (type))
700f8a87
MS
4777 {
4778 val = IDENTIFIER_CLASS_VALUE (name);
e1cd6e56 4779 if (val && DECL_CONTEXT (val) != type)
700f8a87
MS
4780 {
4781 struct binding_level *b = class_binding_level;
4782 for (val = NULL_TREE; b; b = b->level_chain)
4783 {
4784 tree t = purpose_member (name, b->class_shadowed);
4785 if (t && TREE_VALUE (t)
e1cd6e56 4786 && DECL_CONTEXT (TREE_VALUE (t)) == type)
700f8a87
MS
4787 {
4788 val = TREE_VALUE (t);
4789 break;
4790 }
4791 }
4792 }
5566b478 4793 if (val == NULL_TREE)
e1cd6e56 4794 val = lookup_field (type, name, 0, 1);
700f8a87 4795 }
e1cd6e56 4796 else if (type == current_class_type)
a28e3c7f 4797 val = IDENTIFIER_CLASS_VALUE (name);
a28e3c7f 4798 else
e1cd6e56 4799 val = lookup_field (type, name, 0, prefer_type);
a28e3c7f 4800 }
e1cd6e56
MS
4801 else
4802 val = NULL_TREE;
4803
594740f3 4804 if (got_scope)
e1cd6e56 4805 goto done;
594740f3 4806 else if (got_object && val)
e1cd6e56 4807 from_obj = val;
a28e3c7f 4808 }
e76a2646
MS
4809
4810 locval = classval = NULL_TREE;
4811
2c73f9f5 4812 if (!current_binding_level->namespace_p
8d08fdba 4813 && IDENTIFIER_LOCAL_VALUE (name))
e76a2646
MS
4814 locval = IDENTIFIER_LOCAL_VALUE (name);
4815
8d08fdba
MS
4816 /* In C++ class fields are between local and global scope,
4817 just before the global scope. */
e76a2646 4818 if (current_class_type && ! nonclass)
8d08fdba 4819 {
e76a2646
MS
4820 classval = IDENTIFIER_CLASS_VALUE (name);
4821 if (classval == NULL_TREE && TYPE_BEING_DEFINED (current_class_type))
700f8a87
MS
4822 /* Try to find values from base classes if we are presently
4823 defining a type. We are presently only interested in
4824 TYPE_DECLs. */
e76a2646 4825 classval = lookup_field (current_class_type, name, 0, 1);
8d08fdba 4826
b3709d9b 4827 /* Add implicit 'typename' to types from template bases. lookup_field
b9082e8a
JM
4828 will do this for us. If classval is actually from an enclosing
4829 scope, lookup_nested_field will get it for us. */
85b71cf2
JM
4830 if (processing_template_decl
4831 && classval && TREE_CODE (classval) == TYPE_DECL
b9082e8a 4832 && ! currently_open_class (DECL_CONTEXT (classval))
b3709d9b 4833 && uses_template_parms (current_class_type)
85b71cf2 4834 && ! DECL_ARTIFICIAL (classval))
b3709d9b 4835 classval = lookup_field (current_class_type, name, 0, 1);
b9082e8a
JM
4836
4837 /* yylex() calls this with -2, since we should never start digging for
4838 the nested name at the point where we haven't even, for example,
4839 created the COMPONENT_REF or anything like that. */
4840 if (classval == NULL_TREE)
4841 classval = lookup_nested_field (name, ! yylex);
e76a2646 4842 }
8d08fdba 4843
e76a2646
MS
4844 if (locval && classval)
4845 {
4846 if (current_scope () == current_function_decl
4847 && ! hack_decl_function_context (current_function_decl))
4848 /* Not in a nested function. */
4849 val = locval;
4850 else
4851 {
4852 /* This is incredibly horrible. The whole concept of
4853 IDENTIFIER_LOCAL_VALUE / IDENTIFIER_CLASS_VALUE /
4854 IDENTIFIER_GLOBAL_VALUE needs to be scrapped for local
4855 classes. */
4856 tree lctx = hack_decl_function_context (locval);
4857 tree cctx = hack_decl_function_context (classval);
4858
4859 if (lctx == current_scope ())
4860 val = locval;
4861 else if (lctx == cctx)
4862 val = classval;
4863 else
4864 /* I don't know which is right; let's just guess for now. */
4865 val = locval;
4866 }
8d08fdba 4867 }
e76a2646
MS
4868 else if (locval)
4869 val = locval;
4870 else if (classval)
4871 val = classval;
8d08fdba 4872 else
30394414 4873 {
2c73f9f5
ML
4874 /* Unscoped lookup of a global, iterate over namespaces,
4875 considering using namespace statements. */
4876 struct tree_binding _binding;
4877 tree b = binding_init (&_binding);
4878 tree initial = current_decl_namespace();
4879 tree scope = initial;
4880 val = NULL_TREE;
4881 while (!val)
4882 {
4883 val = binding_for_name (name, scope);
4884 /* Initialize binding for this context. */
4885 BINDING_VALUE (b) = BINDING_VALUE (val);
4886 BINDING_TYPE (b) = BINDING_TYPE (val);
4887 /* Add all _DECLs seen through using-directives. */
4888 if (!lookup_using_namespace (name, b, initial, scope))
4889 {
4890 /* Give up because of error. */
4891 val = NULL_TREE;
4892 break;
4893 }
4894 val = select_decl (b, prefer_type);
30394414
JM
4895 if (scope == global_namespace)
4896 break;
2c73f9f5 4897 scope = DECL_CONTEXT (scope);
30394414 4898 }
30394414 4899 }
8d08fdba 4900
a28e3c7f 4901 done:
8d08fdba
MS
4902 if (val)
4903 {
c91a56d2 4904 /* This should only warn about types used in qualified-ids. */
e1cd6e56 4905 if (from_obj && from_obj != val)
5566b478 4906 {
c91a56d2
MS
4907 if (looking_for_typename && TREE_CODE (from_obj) == TYPE_DECL
4908 && TREE_CODE (val) == TYPE_DECL
4909 && TREE_TYPE (from_obj) != TREE_TYPE (val))
4910 {
4911 cp_pedwarn ("lookup of `%D' in the scope of `%#T' (`%#T')",
4912 name, got_object, TREE_TYPE (from_obj));
4913 cp_pedwarn (" does not match lookup in the current scope (`%#T')",
4914 TREE_TYPE (val));
4915 }
594740f3
MS
4916
4917 val = from_obj;
5566b478 4918 }
e1cd6e56 4919
cf776105 4920 if ((TREE_CODE (val) == TEMPLATE_DECL && looking_for_template)
a0a33927 4921 || TREE_CODE (val) == TYPE_DECL || prefer_type <= 0)
e1cd6e56 4922 ;
2c73f9f5
ML
4923 /* Caller wants a class-or-namespace-name. */
4924 else if(prefer_type == 1 && TREE_CODE (val) == NAMESPACE_DECL)
4925 ;
e1cd6e56 4926 else if (IDENTIFIER_HAS_TYPE_VALUE (name))
d2e5ee5c 4927 val = TYPE_MAIN_DECL (IDENTIFIER_TYPE_VALUE (name));
e1cd6e56
MS
4928 else if (TREE_TYPE (val) == error_mark_node)
4929 val = error_mark_node;
8d08fdba 4930 }
e1cd6e56
MS
4931 else if (from_obj)
4932 val = from_obj;
8d08fdba
MS
4933
4934 return val;
4935}
4936
700f8a87
MS
4937tree
4938lookup_name_nonclass (name)
4939 tree name;
4940{
4941 return lookup_name_real (name, 0, 1);
4942}
4943
2c73f9f5
ML
4944tree
4945lookup_function_nonclass (name, args)
4946 tree name;
4947 tree args;
4948{
4949 return lookup_arg_dependent (name, lookup_name_nonclass (name), args);
4950}
4951
700f8a87
MS
4952tree
4953lookup_name (name, prefer_type)
4954 tree name;
4955 int prefer_type;
4956{
4957 return lookup_name_real (name, prefer_type, 0);
4958}
4959
8d08fdba
MS
4960/* Similar to `lookup_name' but look only at current binding level. */
4961
4962tree
4963lookup_name_current_level (name)
4964 tree name;
4965{
4966 register tree t = NULL_TREE;
4967
2c73f9f5 4968 if (current_binding_level->namespace_p)
8d08fdba 4969 {
30394414 4970 t = IDENTIFIER_NAMESPACE_VALUE (name);
8d08fdba
MS
4971
4972 /* extern "C" function() */
4973 if (t != NULL_TREE && TREE_CODE (t) == TREE_LIST)
4974 t = TREE_VALUE (t);
4975 }
4976 else if (IDENTIFIER_LOCAL_VALUE (name) != NULL_TREE)
4977 {
a4443a08
MS
4978 struct binding_level *b = current_binding_level;
4979 while (1)
4980 {
4981 for (t = b->names; t; t = TREE_CHAIN (t))
e1cd6e56 4982 if (DECL_NAME (t) == name || DECL_ASSEMBLER_NAME (t) == name)
a4443a08
MS
4983 goto out;
4984 if (b->keep == 2)
4985 b = b->level_chain;
4986 else
4987 break;
4988 }
4989 out:
4990 ;
8d08fdba
MS
4991 }
4992
4993 return t;
4994}
4995\f
4996/* Arrange for the user to get a source line number, even when the
4997 compiler is going down in flames, so that she at least has a
4998 chance of working around problems in the compiler. We used to
4999 call error(), but that let the segmentation fault continue
5000 through; now, it's much more passive by asking them to send the
5001 maintainers mail about the problem. */
5002
5003static void
5004signal_catch (sig)
7dee3f36 5005 int sig ATTRIBUTE_UNUSED;
8d08fdba
MS
5006{
5007 signal (SIGSEGV, SIG_DFL);
5008#ifdef SIGIOT
5009 signal (SIGIOT, SIG_DFL);
5010#endif
5011#ifdef SIGILL
5012 signal (SIGILL, SIG_DFL);
5013#endif
5014#ifdef SIGABRT
5015 signal (SIGABRT, SIG_DFL);
5016#endif
5017#ifdef SIGBUS
5018 signal (SIGBUS, SIG_DFL);
5019#endif
5020 my_friendly_abort (0);
5021}
5022
de22184b
MS
5023#if 0
5024/* Unused -- brendan 970107 */
8d08fdba
MS
5025/* Array for holding types considered "built-in". These types
5026 are output in the module in which `main' is defined. */
5027static tree *builtin_type_tdescs_arr;
5028static int builtin_type_tdescs_len, builtin_type_tdescs_max;
de22184b 5029#endif
8d08fdba
MS
5030
5031/* Push the declarations of builtin types into the namespace.
5032 RID_INDEX, if < RID_MAX is the index of the builtin type
5033 in the array RID_POINTERS. NAME is the name used when looking
5034 up the builtin type. TYPE is the _TYPE node for the builtin type. */
5035
5036static void
5037record_builtin_type (rid_index, name, type)
5038 enum rid rid_index;
5039 char *name;
5040 tree type;
5041{
5042 tree rname = NULL_TREE, tname = NULL_TREE;
a703fb38 5043 tree tdecl = NULL_TREE;
8d08fdba
MS
5044
5045 if ((int) rid_index < (int) RID_MAX)
5046 rname = ridpointers[(int) rid_index];
5047 if (name)
5048 tname = get_identifier (name);
5049
5050 TYPE_BUILT_IN (type) = 1;
5051
5052 if (tname)
5053 {
8d08fdba 5054 tdecl = pushdecl (build_decl (TYPE_DECL, tname, type));
8d08fdba
MS
5055 set_identifier_type_value (tname, NULL_TREE);
5056 if ((int) rid_index < (int) RID_MAX)
2c73f9f5
ML
5057 /* Built-in types live in the global namespace. */
5058 SET_IDENTIFIER_GLOBAL_VALUE (tname, tdecl);
8d08fdba
MS
5059 }
5060 if (rname != NULL_TREE)
5061 {
5062 if (tname != NULL_TREE)
5063 {
5064 set_identifier_type_value (rname, NULL_TREE);
2c73f9f5 5065 SET_IDENTIFIER_GLOBAL_VALUE (rname, tdecl);
8d08fdba
MS
5066 }
5067 else
5068 {
8d08fdba 5069 tdecl = pushdecl (build_decl (TYPE_DECL, rname, type));
8d08fdba
MS
5070 set_identifier_type_value (rname, NULL_TREE);
5071 }
5072 }
8d08fdba
MS
5073}
5074
8d08fdba
MS
5075/* Push overloaded decl, in global scope, with one argument so it
5076 can be used as a callback from define_function. */
e92cc029 5077
8d08fdba
MS
5078static void
5079push_overloaded_decl_1 (x)
5080 tree x;
5081{
5082 push_overloaded_decl (x, 0);
5083}
5084
8ccc31eb
MS
5085#ifdef __GNUC__
5086__inline
5087#endif
6b5fbb55
MS
5088tree
5089auto_function (name, type, code)
8ccc31eb
MS
5090 tree name, type;
5091 enum built_in_function code;
5092{
5093 return define_function
49c249e1 5094 (IDENTIFIER_POINTER (name), type, code, push_overloaded_decl_1,
8ccc31eb
MS
5095 IDENTIFIER_POINTER (build_decl_overload (name, TYPE_ARG_TYPES (type),
5096 0)));
5097}
5098
8d08fdba
MS
5099/* Create the predefined scalar types of C,
5100 and some nodes representing standard constants (0, 1, (void *)0).
5101 Initialize the global binding level.
5102 Make definitions for built-in primitive functions. */
5103
5104void
5105init_decl_processing ()
5106{
5107 tree decl;
de22184b 5108 register tree endlink, int_endlink, double_endlink, unsigned_endlink;
8d08fdba 5109 tree fields[20];
8d08fdba 5110 /* Data type of memcpy. */
cffa8729 5111 tree memcpy_ftype, strlen_ftype;
8d08fdba
MS
5112 int wchar_type_size;
5113 tree temp;
5114 tree array_domain_type;
e1cd6e56 5115 extern int flag_strict_prototype;
a703fb38 5116 tree vb_off_identifier = NULL_TREE;
5566b478
MS
5117 /* Function type `char *(char *, char *)' and similar ones */
5118 tree string_ftype_ptr_ptr, int_ftype_string_string;
de22184b
MS
5119 tree sizetype_endlink;
5120 tree ptr_ftype, ptr_ftype_unsigned, ptr_ftype_sizetype;
ca55abae 5121 tree void_ftype, void_ftype_int, void_ftype_ptr, ptr_ftype_void;
8d08fdba
MS
5122
5123 /* Have to make these distinct before we try using them. */
5124 lang_name_cplusplus = get_identifier ("C++");
5125 lang_name_c = get_identifier ("C");
a1774733 5126 lang_name_java = get_identifier ("Java");
8d08fdba 5127
2c73f9f5 5128 /* Enter the global namespace. */
30394414
JM
5129 my_friendly_assert (global_namespace == NULL_TREE, 375);
5130 my_friendly_assert (current_lang_name == NULL_TREE, 375);
5131 current_lang_name = lang_name_cplusplus;
5132 push_namespace (get_identifier ("::"));
5133 global_namespace = current_namespace;
5134 current_lang_name = NULL_TREE;
5135
e1cd6e56
MS
5136 if (flag_strict_prototype == 2)
5137 {
5138 if (pedantic)
5139 strict_prototypes_lang_c = strict_prototypes_lang_cplusplus;
5140 }
5141 else
5142 strict_prototypes_lang_c = flag_strict_prototype;
8926095f 5143
8d08fdba
MS
5144 /* Initially, C. */
5145 current_lang_name = lang_name_c;
5146
5147 current_function_decl = NULL_TREE;
5148 named_labels = NULL_TREE;
e349ee73 5149 named_label_uses = NULL;
8d08fdba
MS
5150 current_binding_level = NULL_BINDING_LEVEL;
5151 free_binding_level = NULL_BINDING_LEVEL;
5152
42976354 5153#ifndef __CYGWIN32__
8d08fdba
MS
5154 /* Because most segmentation signals can be traced back into user
5155 code, catch them and at least give the user a chance of working
e92cc029 5156 around compiler bugs. */
8d08fdba
MS
5157 signal (SIGSEGV, signal_catch);
5158
5159 /* We will also catch aborts in the back-end through signal_catch and
5160 give the user a chance to see where the error might be, and to defeat
5161 aborts in the back-end when there have been errors previously in their
e92cc029 5162 code. */
8d08fdba
MS
5163#ifdef SIGIOT
5164 signal (SIGIOT, signal_catch);
5165#endif
5166#ifdef SIGILL
5167 signal (SIGILL, signal_catch);
5168#endif
5169#ifdef SIGABRT
5170 signal (SIGABRT, signal_catch);
5171#endif
5172#ifdef SIGBUS
5173 signal (SIGBUS, signal_catch);
5174#endif
7834ab39
MS
5175#else /* ndef __CYGWIN32__ */
5176 /* Cygwin32 cannot handle catching signals other than
5177 SIGABRT yet. We hope this will cease to be the case soon. */
5178#ifdef SIGABRT
5179 signal (SIGABRT, signal_catch);
5180#endif
5181#endif /* ndef __CYGWIN32__ */
8d08fdba
MS
5182
5183 gcc_obstack_init (&decl_obstack);
8d08fdba
MS
5184
5185 /* Must lay these out before anything else gets laid out. */
5186 error_mark_node = make_node (ERROR_MARK);
5187 TREE_PERMANENT (error_mark_node) = 1;
5188 TREE_TYPE (error_mark_node) = error_mark_node;
5189 error_mark_list = build_tree_list (error_mark_node, error_mark_node);
5190 TREE_TYPE (error_mark_list) = error_mark_node;
5191
a28e3c7f
MS
5192 /* Make the binding_level structure for global names. */
5193 pushlevel (0);
8d08fdba 5194 global_binding_level = current_binding_level;
2c73f9f5
ML
5195 /* The global level is the namespace level of ::. */
5196 NAMESPACE_LEVEL (global_namespace) = global_binding_level;
5197 declare_namespace_level ();
8d08fdba
MS
5198
5199 this_identifier = get_identifier (THIS_NAME);
5200 in_charge_identifier = get_identifier (IN_CHARGE_NAME);
fc378698
MS
5201 ctor_identifier = get_identifier (CTOR_NAME);
5202 dtor_identifier = get_identifier (DTOR_NAME);
8d08fdba
MS
5203 pfn_identifier = get_identifier (VTABLE_PFN_NAME);
5204 index_identifier = get_identifier (VTABLE_INDEX_NAME);
5205 delta_identifier = get_identifier (VTABLE_DELTA_NAME);
5206 delta2_identifier = get_identifier (VTABLE_DELTA2_NAME);
5207 pfn_or_delta2_identifier = get_identifier ("__pfn_or_delta2");
dff6b454
RK
5208 if (flag_handle_signatures)
5209 {
5210 tag_identifier = get_identifier (SIGTABLE_TAG_NAME);
9dd70aa4
GB
5211 vb_off_identifier = get_identifier (SIGTABLE_VB_OFF_NAME);
5212 vt_off_identifier = get_identifier (SIGTABLE_VT_OFF_NAME);
dff6b454 5213 }
8d08fdba
MS
5214
5215 /* Define `int' and `char' first so that dbx will output them first. */
5216
5217 integer_type_node = make_signed_type (INT_TYPE_SIZE);
5218 record_builtin_type (RID_INT, NULL_PTR, integer_type_node);
5219
5220 /* Define `char', which is like either `signed char' or `unsigned char'
5221 but not the same as either. */
5222
beb53fb8
JM
5223 char_type_node
5224 = (flag_signed_char
5225 ? make_signed_type (CHAR_TYPE_SIZE)
5226 : make_unsigned_type (CHAR_TYPE_SIZE));
8d08fdba
MS
5227 record_builtin_type (RID_CHAR, "char", char_type_node);
5228
5229 long_integer_type_node = make_signed_type (LONG_TYPE_SIZE);
5230 record_builtin_type (RID_LONG, "long int", long_integer_type_node);
5231
5232 unsigned_type_node = make_unsigned_type (INT_TYPE_SIZE);
5233 record_builtin_type (RID_UNSIGNED, "unsigned int", unsigned_type_node);
5234
5235 long_unsigned_type_node = make_unsigned_type (LONG_TYPE_SIZE);
5236 record_builtin_type (RID_MAX, "long unsigned int", long_unsigned_type_node);
5237 record_builtin_type (RID_MAX, "unsigned long", long_unsigned_type_node);
5238
5239 long_long_integer_type_node = make_signed_type (LONG_LONG_TYPE_SIZE);
5240 record_builtin_type (RID_MAX, "long long int", long_long_integer_type_node);
5241
5242 long_long_unsigned_type_node = make_unsigned_type (LONG_LONG_TYPE_SIZE);
5243 record_builtin_type (RID_MAX, "long long unsigned int",
5244 long_long_unsigned_type_node);
5245 record_builtin_type (RID_MAX, "long long unsigned",
5246 long_long_unsigned_type_node);
5247
5156628f
MS
5248 short_integer_type_node = make_signed_type (SHORT_TYPE_SIZE);
5249 record_builtin_type (RID_SHORT, "short int", short_integer_type_node);
5250 short_unsigned_type_node = make_unsigned_type (SHORT_TYPE_SIZE);
5251 record_builtin_type (RID_MAX, "short unsigned int", short_unsigned_type_node);
5252 record_builtin_type (RID_MAX, "unsigned short", short_unsigned_type_node);
5253
8d08fdba 5254 /* `unsigned long' is the standard type for sizeof.
8d08fdba 5255 Note that stddef.h uses `unsigned long',
f5426d1e
R
5256 and this must agree, even if long and int are the same size. */
5257 set_sizetype
5258 (TREE_TYPE (IDENTIFIER_GLOBAL_VALUE (get_identifier (SIZE_TYPE))));
8d08fdba
MS
5259
5260 ptrdiff_type_node
5261 = TREE_TYPE (IDENTIFIER_GLOBAL_VALUE (get_identifier (PTRDIFF_TYPE)));
5262
8d08fdba
MS
5263 /* Define both `signed char' and `unsigned char'. */
5264 signed_char_type_node = make_signed_type (CHAR_TYPE_SIZE);
5265 record_builtin_type (RID_MAX, "signed char", signed_char_type_node);
5266 unsigned_char_type_node = make_unsigned_type (CHAR_TYPE_SIZE);
5267 record_builtin_type (RID_MAX, "unsigned char", unsigned_char_type_node);
5268
5269 /* These are types that type_for_size and type_for_mode use. */
5270 intQI_type_node = make_signed_type (GET_MODE_BITSIZE (QImode));
5271 pushdecl (build_decl (TYPE_DECL, NULL_TREE, intQI_type_node));
5272 intHI_type_node = make_signed_type (GET_MODE_BITSIZE (HImode));
5273 pushdecl (build_decl (TYPE_DECL, NULL_TREE, intHI_type_node));
5274 intSI_type_node = make_signed_type (GET_MODE_BITSIZE (SImode));
5275 pushdecl (build_decl (TYPE_DECL, NULL_TREE, intSI_type_node));
5276 intDI_type_node = make_signed_type (GET_MODE_BITSIZE (DImode));
5277 pushdecl (build_decl (TYPE_DECL, NULL_TREE, intDI_type_node));
f7554e8c
JL
5278 intTI_type_node = make_signed_type (GET_MODE_BITSIZE (TImode));
5279 pushdecl (build_decl (TYPE_DECL, NULL_TREE, intTI_type_node));
8d08fdba
MS
5280 unsigned_intQI_type_node = make_unsigned_type (GET_MODE_BITSIZE (QImode));
5281 pushdecl (build_decl (TYPE_DECL, NULL_TREE, unsigned_intQI_type_node));
5282 unsigned_intHI_type_node = make_unsigned_type (GET_MODE_BITSIZE (HImode));
5283 pushdecl (build_decl (TYPE_DECL, NULL_TREE, unsigned_intHI_type_node));
5284 unsigned_intSI_type_node = make_unsigned_type (GET_MODE_BITSIZE (SImode));
5285 pushdecl (build_decl (TYPE_DECL, NULL_TREE, unsigned_intSI_type_node));
5286 unsigned_intDI_type_node = make_unsigned_type (GET_MODE_BITSIZE (DImode));
5287 pushdecl (build_decl (TYPE_DECL, NULL_TREE, unsigned_intDI_type_node));
f7554e8c
JL
5288 unsigned_intTI_type_node = make_unsigned_type (GET_MODE_BITSIZE (TImode));
5289 pushdecl (build_decl (TYPE_DECL, NULL_TREE, unsigned_intTI_type_node));
8d08fdba
MS
5290
5291 float_type_node = make_node (REAL_TYPE);
5292 TYPE_PRECISION (float_type_node) = FLOAT_TYPE_SIZE;
5293 record_builtin_type (RID_FLOAT, NULL_PTR, float_type_node);
5294 layout_type (float_type_node);
5295
5296 double_type_node = make_node (REAL_TYPE);
5297 if (flag_short_double)
5298 TYPE_PRECISION (double_type_node) = FLOAT_TYPE_SIZE;
5299 else
5300 TYPE_PRECISION (double_type_node) = DOUBLE_TYPE_SIZE;
5301 record_builtin_type (RID_DOUBLE, NULL_PTR, double_type_node);
5302 layout_type (double_type_node);
5303
5304 long_double_type_node = make_node (REAL_TYPE);
5305 TYPE_PRECISION (long_double_type_node) = LONG_DOUBLE_TYPE_SIZE;
5306 record_builtin_type (RID_MAX, "long double", long_double_type_node);
5307 layout_type (long_double_type_node);
5308
37c46b43
MS
5309 complex_integer_type_node = make_node (COMPLEX_TYPE);
5310 pushdecl (build_decl (TYPE_DECL, get_identifier ("complex int"),
5311 complex_integer_type_node));
5312 TREE_TYPE (complex_integer_type_node) = integer_type_node;
5313 layout_type (complex_integer_type_node);
5314
5315 complex_float_type_node = make_node (COMPLEX_TYPE);
5316 pushdecl (build_decl (TYPE_DECL, get_identifier ("complex float"),
5317 complex_float_type_node));
5318 TREE_TYPE (complex_float_type_node) = float_type_node;
5319 layout_type (complex_float_type_node);
5320
5321 complex_double_type_node = make_node (COMPLEX_TYPE);
5322 pushdecl (build_decl (TYPE_DECL, get_identifier ("complex double"),
5323 complex_double_type_node));
5324 TREE_TYPE (complex_double_type_node) = double_type_node;
5325 layout_type (complex_double_type_node);
5326
5327 complex_long_double_type_node = make_node (COMPLEX_TYPE);
5328 pushdecl (build_decl (TYPE_DECL, get_identifier ("complex long double"),
5329 complex_long_double_type_node));
5330 TREE_TYPE (complex_long_double_type_node) = long_double_type_node;
5331 layout_type (complex_long_double_type_node);
5332
8d08fdba
MS
5333 integer_zero_node = build_int_2 (0, 0);
5334 TREE_TYPE (integer_zero_node) = integer_type_node;
5335 integer_one_node = build_int_2 (1, 0);
5336 TREE_TYPE (integer_one_node) = integer_type_node;
5337 integer_two_node = build_int_2 (2, 0);
5338 TREE_TYPE (integer_two_node) = integer_type_node;
5339 integer_three_node = build_int_2 (3, 0);
5340 TREE_TYPE (integer_three_node) = integer_type_node;
8d08fdba 5341
255512c1
JM
5342 boolean_type_node = make_unsigned_type (BOOL_TYPE_SIZE);
5343 TREE_SET_CODE (boolean_type_node, BOOLEAN_TYPE);
c1ea6a0b
BK
5344 TYPE_MAX_VALUE (boolean_type_node) = build_int_2 (1, 0);
5345 TREE_TYPE (TYPE_MAX_VALUE (boolean_type_node)) = boolean_type_node;
5346 TYPE_PRECISION (boolean_type_node) = 1;
255512c1
JM
5347 record_builtin_type (RID_BOOL, "bool", boolean_type_node);
5348 boolean_false_node = build_int_2 (0, 0);
5349 TREE_TYPE (boolean_false_node) = boolean_type_node;
5350 boolean_true_node = build_int_2 (1, 0);
5351 TREE_TYPE (boolean_true_node) = boolean_type_node;
2986ae00 5352
8d08fdba
MS
5353 /* These are needed by stor-layout.c. */
5354 size_zero_node = size_int (0);
5355 size_one_node = size_int (1);
5356
e92cc029
MS
5357 signed_size_zero_node = build_int_2 (0, 0);
5358 TREE_TYPE (signed_size_zero_node) = make_signed_type (TYPE_PRECISION (sizetype));
5359
8d08fdba
MS
5360 void_type_node = make_node (VOID_TYPE);
5361 record_builtin_type (RID_VOID, NULL_PTR, void_type_node);
5362 layout_type (void_type_node); /* Uses integer_zero_node. */
5363 void_list_node = build_tree_list (NULL_TREE, void_type_node);
5364 TREE_PARMLIST (void_list_node) = 1;
5365
d11ad92e 5366 null_pointer_node = build_int_2 (0, 0);
8d08fdba 5367 TREE_TYPE (null_pointer_node) = build_pointer_type (void_type_node);
d11ad92e
MS
5368 layout_type (TREE_TYPE (null_pointer_node));
5369
8d08fdba
MS
5370 /* Used for expressions that do nothing, but are not errors. */
5371 void_zero_node = build_int_2 (0, 0);
5372 TREE_TYPE (void_zero_node) = void_type_node;
5373
5374 string_type_node = build_pointer_type (char_type_node);
beb53fb8
JM
5375 const_string_type_node
5376 = build_pointer_type (build_type_variant (char_type_node, 1, 0));
6b5fbb55 5377#if 0
8d08fdba 5378 record_builtin_type (RID_MAX, NULL_PTR, string_type_node);
6b5fbb55 5379#endif
8d08fdba
MS
5380
5381 /* Make a type to be the domain of a few array types
5382 whose domains don't really matter.
5383 200 is small enough that it always fits in size_t
5384 and large enough that it can hold most function names for the
5385 initializations of __FUNCTION__ and __PRETTY_FUNCTION__. */
5386 array_domain_type = build_index_type (build_int_2 (200, 0));
5387
2c73f9f5 5388 /* Make a type for arrays of characters.
8d08fdba
MS
5389 With luck nothing will ever really depend on the length of this
5390 array type. */
5391 char_array_type_node
5392 = build_array_type (char_type_node, array_domain_type);
5393 /* Likewise for arrays of ints. */
5394 int_array_type_node
5395 = build_array_type (integer_type_node, array_domain_type);
5396
5397 /* This is just some anonymous class type. Nobody should ever
5398 need to look inside this envelope. */
5399 class_star_type_node = build_pointer_type (make_lang_type (RECORD_TYPE));
5400
5401 default_function_type
5402 = build_function_type (integer_type_node, NULL_TREE);
8d08fdba
MS
5403
5404 ptr_type_node = build_pointer_type (void_type_node);
beb53fb8
JM
5405 const_ptr_type_node
5406 = build_pointer_type (build_type_variant (void_type_node, 1, 0));
6b5fbb55 5407#if 0
8d08fdba 5408 record_builtin_type (RID_MAX, NULL_PTR, ptr_type_node);
6b5fbb55 5409#endif
8d08fdba
MS
5410 endlink = void_list_node;
5411 int_endlink = tree_cons (NULL_TREE, integer_type_node, endlink);
5412 double_endlink = tree_cons (NULL_TREE, double_type_node, endlink);
de22184b
MS
5413 unsigned_endlink = tree_cons (NULL_TREE, unsigned_type_node, endlink);
5414
5415 ptr_ftype = build_function_type (ptr_type_node, NULL_TREE);
5416 ptr_ftype_unsigned = build_function_type (ptr_type_node, unsigned_endlink);
5417 sizetype_endlink = tree_cons (NULL_TREE, sizetype, endlink);
5418 /* We realloc here because sizetype could be int or unsigned. S'ok. */
5419 ptr_ftype_sizetype = build_function_type (ptr_type_node, sizetype_endlink);
5420
5421 void_ftype = build_function_type (void_type_node, endlink);
5422 void_ftype_int = build_function_type (void_type_node, int_endlink);
5423 void_ftype_ptr
5424 = build_function_type (void_type_node,
5425 tree_cons (NULL_TREE, ptr_type_node, endlink));
824b9a4c
MS
5426 void_ftype_ptr
5427 = build_exception_variant (void_ftype_ptr,
5428 tree_cons (NULL_TREE, NULL_TREE, NULL_TREE));
8d08fdba 5429
cffa8729
MS
5430 float_ftype_float
5431 = build_function_type (float_type_node,
5432 tree_cons (NULL_TREE, float_type_node, endlink));
5433
8d08fdba
MS
5434 double_ftype_double
5435 = build_function_type (double_type_node, double_endlink);
5436
cffa8729
MS
5437 ldouble_ftype_ldouble
5438 = build_function_type (long_double_type_node,
5439 tree_cons (NULL_TREE, long_double_type_node,
5440 endlink));
5441
8d08fdba
MS
5442 double_ftype_double_double
5443 = build_function_type (double_type_node,
a28e3c7f
MS
5444 tree_cons (NULL_TREE, double_type_node,
5445 double_endlink));
8d08fdba
MS
5446
5447 int_ftype_int
5448 = build_function_type (integer_type_node, int_endlink);
5449
5450 long_ftype_long
5451 = build_function_type (long_integer_type_node,
a28e3c7f
MS
5452 tree_cons (NULL_TREE, long_integer_type_node,
5453 endlink));
8d08fdba 5454
8d08fdba
MS
5455 int_ftype_cptr_cptr_sizet
5456 = build_function_type (integer_type_node,
5457 tree_cons (NULL_TREE, const_ptr_type_node,
5458 tree_cons (NULL_TREE, const_ptr_type_node,
5459 tree_cons (NULL_TREE,
5460 sizetype,
5461 endlink))));
5462
8d08fdba
MS
5463 string_ftype_ptr_ptr /* strcpy prototype */
5464 = build_function_type (string_type_node,
5465 tree_cons (NULL_TREE, string_type_node,
5466 tree_cons (NULL_TREE,
5467 const_string_type_node,
5468 endlink)));
5469
8d08fdba
MS
5470 int_ftype_string_string /* strcmp prototype */
5471 = build_function_type (integer_type_node,
5472 tree_cons (NULL_TREE, const_string_type_node,
5473 tree_cons (NULL_TREE,
5474 const_string_type_node,
5475 endlink)));
5476
cffa8729 5477 strlen_ftype /* strlen prototype */
8d08fdba
MS
5478 = build_function_type (sizetype,
5479 tree_cons (NULL_TREE, const_string_type_node,
5480 endlink));
5481
8d08fdba 5482 memcpy_ftype /* memcpy prototype */
d22c8596 5483 = build_function_type (ptr_type_node,
8d08fdba
MS
5484 tree_cons (NULL_TREE, ptr_type_node,
5485 tree_cons (NULL_TREE, const_ptr_type_node,
de22184b 5486 sizetype_endlink)));
8d08fdba
MS
5487
5488 if (flag_huge_objects)
5489 delta_type_node = long_integer_type_node;
5490 else
5491 delta_type_node = short_integer_type_node;
5492
b9620d0e 5493 builtin_function ("__builtin_constant_p", default_function_type,
8d08fdba
MS
5494 BUILT_IN_CONSTANT_P, NULL_PTR);
5495
beb53fb8
JM
5496 builtin_return_address_fndecl
5497 = builtin_function ("__builtin_return_address", ptr_ftype_unsigned,
5498 BUILT_IN_RETURN_ADDRESS, NULL_PTR);
8926095f 5499
de22184b 5500 builtin_function ("__builtin_frame_address", ptr_ftype_unsigned,
8926095f
MS
5501 BUILT_IN_FRAME_ADDRESS, NULL_PTR);
5502
ca55abae
JM
5503 ptr_ftype_void = build_function_type (ptr_type_node, endlink);
5504 builtin_function ("__builtin_fp", ptr_ftype_void, BUILT_IN_FP, NULL_PTR);
5505 builtin_function ("__builtin_sp", ptr_ftype_void, BUILT_IN_SP, NULL_PTR);
5506
de22184b 5507 builtin_function ("__builtin_alloca", ptr_ftype_sizetype,
8d08fdba 5508 BUILT_IN_ALLOCA, "alloca");
cffa8729 5509 builtin_function ("__builtin_ffs", int_ftype_int, BUILT_IN_FFS, NULL_PTR);
00595019
MS
5510 /* Define alloca, ffs as builtins.
5511 Declare _exit just to mark it as volatile. */
5512 if (! flag_no_builtin && !flag_no_nonansi_builtin)
5513 {
de22184b 5514 temp = builtin_function ("alloca", ptr_ftype_sizetype,
00595019
MS
5515 BUILT_IN_ALLOCA, NULL_PTR);
5516 /* Suppress error if redefined as a non-function. */
5517 DECL_BUILT_IN_NONANSI (temp) = 1;
00595019
MS
5518 temp = builtin_function ("ffs", int_ftype_int, BUILT_IN_FFS, NULL_PTR);
5519 /* Suppress error if redefined as a non-function. */
5520 DECL_BUILT_IN_NONANSI (temp) = 1;
de22184b 5521 temp = builtin_function ("_exit", void_ftype_int,
00595019
MS
5522 NOT_BUILT_IN, NULL_PTR);
5523 TREE_THIS_VOLATILE (temp) = 1;
5524 TREE_SIDE_EFFECTS (temp) = 1;
5525 /* Suppress error if redefined as a non-function. */
5526 DECL_BUILT_IN_NONANSI (temp) = 1;
5527 }
8d08fdba 5528
cffa8729
MS
5529 builtin_function ("__builtin_abs", int_ftype_int, BUILT_IN_ABS, NULL_PTR);
5530 builtin_function ("__builtin_fabsf", float_ftype_float, BUILT_IN_FABS,
5531 NULL_PTR);
5532 builtin_function ("__builtin_fabs", double_ftype_double, BUILT_IN_FABS,
5533 NULL_PTR);
5534 builtin_function ("__builtin_fabsl", ldouble_ftype_ldouble, BUILT_IN_FABS,
5535 NULL_PTR);
8d08fdba
MS
5536 builtin_function ("__builtin_labs", long_ftype_long,
5537 BUILT_IN_LABS, NULL_PTR);
de22184b 5538 builtin_function ("__builtin_saveregs", ptr_ftype,
8d08fdba 5539 BUILT_IN_SAVEREGS, NULL_PTR);
8d08fdba
MS
5540 builtin_function ("__builtin_classify_type", default_function_type,
5541 BUILT_IN_CLASSIFY_TYPE, NULL_PTR);
de22184b 5542 builtin_function ("__builtin_next_arg", ptr_ftype,
8d08fdba 5543 BUILT_IN_NEXT_ARG, NULL_PTR);
de22184b 5544 builtin_function ("__builtin_args_info", int_ftype_int,
8d08fdba 5545 BUILT_IN_ARGS_INFO, NULL_PTR);
eb66be0e
MS
5546 builtin_function ("__builtin_setjmp",
5547 build_function_type (integer_type_node,
5548 tree_cons (NULL_TREE, ptr_type_node,
5549 endlink)),
5550 BUILT_IN_SETJMP, NULL_PTR);
5551 builtin_function ("__builtin_longjmp",
5552 build_function_type (integer_type_node,
5553 tree_cons (NULL_TREE, ptr_type_node,
5554 tree_cons (NULL_TREE,
5555 integer_type_node,
5556 endlink))),
5557 BUILT_IN_LONGJMP, NULL_PTR);
8d08fdba
MS
5558
5559 /* Untyped call and return. */
de22184b 5560 builtin_function ("__builtin_apply_args", ptr_ftype,
8d08fdba
MS
5561 BUILT_IN_APPLY_ARGS, NULL_PTR);
5562
5563 temp = tree_cons (NULL_TREE,
5564 build_pointer_type (build_function_type (void_type_node,
5565 NULL_TREE)),
2c73f9f5 5566 tree_cons (NULL_TREE, ptr_ftype_sizetype, NULL_TREE));
8d08fdba
MS
5567 builtin_function ("__builtin_apply",
5568 build_function_type (ptr_type_node, temp),
5569 BUILT_IN_APPLY, NULL_PTR);
de22184b 5570 builtin_function ("__builtin_return", void_ftype_ptr,
8d08fdba
MS
5571 BUILT_IN_RETURN, NULL_PTR);
5572
5573 /* Currently under experimentation. */
5574 builtin_function ("__builtin_memcpy", memcpy_ftype,
5575 BUILT_IN_MEMCPY, "memcpy");
5576 builtin_function ("__builtin_memcmp", int_ftype_cptr_cptr_sizet,
5577 BUILT_IN_MEMCMP, "memcmp");
5578 builtin_function ("__builtin_strcmp", int_ftype_string_string,
5579 BUILT_IN_STRCMP, "strcmp");
5580 builtin_function ("__builtin_strcpy", string_ftype_ptr_ptr,
5581 BUILT_IN_STRCPY, "strcpy");
cffa8729 5582 builtin_function ("__builtin_strlen", strlen_ftype,
8d08fdba 5583 BUILT_IN_STRLEN, "strlen");
cffa8729
MS
5584 builtin_function ("__builtin_sqrtf", float_ftype_float,
5585 BUILT_IN_FSQRT, "sqrtf");
5586 builtin_function ("__builtin_fsqrt", double_ftype_double,
5587 BUILT_IN_FSQRT, NULL_PTR);
5588 builtin_function ("__builtin_sqrtl", ldouble_ftype_ldouble,
5589 BUILT_IN_FSQRT, "sqrtl");
5590 builtin_function ("__builtin_sinf", float_ftype_float,
5591 BUILT_IN_SIN, "sinf");
5592 builtin_function ("__builtin_sin", double_ftype_double,
5593 BUILT_IN_SIN, "sin");
5594 builtin_function ("__builtin_sinl", ldouble_ftype_ldouble,
5595 BUILT_IN_SIN, "sinl");
5596 builtin_function ("__builtin_cosf", float_ftype_float,
5597 BUILT_IN_COS, "cosf");
5598 builtin_function ("__builtin_cos", double_ftype_double,
5599 BUILT_IN_COS, "cos");
5600 builtin_function ("__builtin_cosl", ldouble_ftype_ldouble,
5601 BUILT_IN_COS, "cosl");
8d08fdba
MS
5602
5603 if (!flag_no_builtin)
5604 {
8d08fdba
MS
5605 builtin_function ("abs", int_ftype_int, BUILT_IN_ABS, NULL_PTR);
5606 builtin_function ("fabs", double_ftype_double, BUILT_IN_FABS, NULL_PTR);
5607 builtin_function ("labs", long_ftype_long, BUILT_IN_LABS, NULL_PTR);
cffa8729
MS
5608 builtin_function ("fabsf", float_ftype_float, BUILT_IN_FABS, NULL_PTR);
5609 builtin_function ("fabsl", ldouble_ftype_ldouble, BUILT_IN_FABS,
5610 NULL_PTR);
8d08fdba
MS
5611 builtin_function ("memcpy", memcpy_ftype, BUILT_IN_MEMCPY, NULL_PTR);
5612 builtin_function ("memcmp", int_ftype_cptr_cptr_sizet, BUILT_IN_MEMCMP,
5613 NULL_PTR);
cffa8729
MS
5614 builtin_function ("strcmp", int_ftype_string_string, BUILT_IN_STRCMP,
5615 NULL_PTR);
a28e3c7f
MS
5616 builtin_function ("strcpy", string_ftype_ptr_ptr, BUILT_IN_STRCPY,
5617 NULL_PTR);
cffa8729
MS
5618 builtin_function ("strlen", strlen_ftype, BUILT_IN_STRLEN, NULL_PTR);
5619 builtin_function ("sqrtf", float_ftype_float, BUILT_IN_FSQRT, NULL_PTR);
5620 builtin_function ("sqrt", double_ftype_double, BUILT_IN_FSQRT, NULL_PTR);
5621 builtin_function ("sqrtl", ldouble_ftype_ldouble, BUILT_IN_FSQRT,
5622 NULL_PTR);
5623 builtin_function ("sinf", float_ftype_float, BUILT_IN_SIN, NULL_PTR);
8d08fdba 5624 builtin_function ("sin", double_ftype_double, BUILT_IN_SIN, NULL_PTR);
cffa8729
MS
5625 builtin_function ("sinl", ldouble_ftype_ldouble, BUILT_IN_SIN, NULL_PTR);
5626 builtin_function ("cosf", float_ftype_float, BUILT_IN_COS, NULL_PTR);
8d08fdba 5627 builtin_function ("cos", double_ftype_double, BUILT_IN_COS, NULL_PTR);
cffa8729 5628 builtin_function ("cosl", ldouble_ftype_ldouble, BUILT_IN_COS, NULL_PTR);
00595019
MS
5629
5630 /* Declare these functions volatile
5631 to avoid spurious "control drops through" warnings. */
de22184b 5632 temp = builtin_function ("abort", void_ftype,
00595019
MS
5633 NOT_BUILT_IN, NULL_PTR);
5634 TREE_THIS_VOLATILE (temp) = 1;
5635 TREE_SIDE_EFFECTS (temp) = 1;
5636 /* Well, these are actually ANSI, but we can't set DECL_BUILT_IN on
5637 them... */
5638 DECL_BUILT_IN_NONANSI (temp) = 1;
de22184b 5639 temp = builtin_function ("exit", void_ftype_int,
00595019
MS
5640 NOT_BUILT_IN, NULL_PTR);
5641 TREE_THIS_VOLATILE (temp) = 1;
5642 TREE_SIDE_EFFECTS (temp) = 1;
5643 DECL_BUILT_IN_NONANSI (temp) = 1;
8d08fdba
MS
5644 }
5645
5646#if 0
5647 /* Support for these has not been written in either expand_builtin
5648 or build_function_call. */
72b7eeff
MS
5649 builtin_function ("__builtin_div", default_ftype, BUILT_IN_DIV, NULL_PTR);
5650 builtin_function ("__builtin_ldiv", default_ftype, BUILT_IN_LDIV, NULL_PTR);
a28e3c7f 5651 builtin_function ("__builtin_ffloor", double_ftype_double, BUILT_IN_FFLOOR,
72b7eeff 5652 NULL_PTR);
cffa8729
MS
5653 builtin_function ("__builtin_fceil", double_ftype_double, BUILT_IN_FCEIL,
5654 NULL_PTR);
a28e3c7f 5655 builtin_function ("__builtin_fmod", double_ftype_double_double,
72b7eeff 5656 BUILT_IN_FMOD, NULL_PTR);
a28e3c7f 5657 builtin_function ("__builtin_frem", double_ftype_double_double,
72b7eeff 5658 BUILT_IN_FREM, NULL_PTR);
cffa8729
MS
5659 builtin_function ("__builtin_memset", ptr_ftype_ptr_int_int,
5660 BUILT_IN_MEMSET, NULL_PTR);
a28e3c7f 5661 builtin_function ("__builtin_getexp", double_ftype_double, BUILT_IN_GETEXP,
72b7eeff 5662 NULL_PTR);
a28e3c7f 5663 builtin_function ("__builtin_getman", double_ftype_double, BUILT_IN_GETMAN,
72b7eeff 5664 NULL_PTR);
8d08fdba
MS
5665#endif
5666
5667 /* C++ extensions */
5668
5669 unknown_type_node = make_node (UNKNOWN_TYPE);
8d08fdba
MS
5670 decl = pushdecl (build_decl (TYPE_DECL, get_identifier ("unknown type"),
5671 unknown_type_node));
5672 /* Make sure the "unknown type" typedecl gets ignored for debug info. */
5673 DECL_IGNORED_P (decl) = 1;
39211cd5 5674 TYPE_DECL_SUPPRESS_DEBUG (decl) = 1;
8d08fdba
MS
5675 TYPE_SIZE (unknown_type_node) = TYPE_SIZE (void_type_node);
5676 TYPE_ALIGN (unknown_type_node) = 1;
5677 TYPE_MODE (unknown_type_node) = TYPE_MODE (void_type_node);
5678 /* Indirecting an UNKNOWN_TYPE node yields an UNKNOWN_TYPE node. */
5679 TREE_TYPE (unknown_type_node) = unknown_type_node;
a6967cc0
JM
5680
5681 if (flag_ansi)
5682 TREE_TYPE (null_node) = type_for_size (POINTER_SIZE, 0);
5683 else
5684 TREE_TYPE (null_node) = build_pointer_type (unknown_type_node);
5685
5686 /* Looking up TYPE_POINTER_TO and TYPE_REFERENCE_TO yield the same
5687 result. */
8d08fdba
MS
5688 TYPE_POINTER_TO (unknown_type_node) = unknown_type_node;
5689 TYPE_REFERENCE_TO (unknown_type_node) = unknown_type_node;
5690
5691 /* This is for handling opaque types in signatures. */
5692 opaque_type_node = copy_node (ptr_type_node);
5693 TYPE_MAIN_VARIANT (opaque_type_node) = opaque_type_node;
5694 record_builtin_type (RID_MAX, 0, opaque_type_node);
5695
e92cc029 5696 /* This is special for C++ so functions can be overloaded. */
8d08fdba
MS
5697 wchar_type_node
5698 = TREE_TYPE (IDENTIFIER_GLOBAL_VALUE (get_identifier (WCHAR_TYPE)));
5699 wchar_type_size = TYPE_PRECISION (wchar_type_node);
5700 signed_wchar_type_node = make_signed_type (wchar_type_size);
5701 unsigned_wchar_type_node = make_unsigned_type (wchar_type_size);
5702 wchar_type_node
5703 = TREE_UNSIGNED (wchar_type_node)
5704 ? unsigned_wchar_type_node
5705 : signed_wchar_type_node;
5706 record_builtin_type (RID_WCHAR, "__wchar_t", wchar_type_node);
5707
f376e137
MS
5708 /* Artificial declaration of wchar_t -- can be bashed */
5709 wchar_decl_node = build_decl (TYPE_DECL, get_identifier ("wchar_t"),
5710 wchar_type_node);
5711 pushdecl (wchar_decl_node);
5712
8d08fdba
MS
5713 /* This is for wide string constants. */
5714 wchar_array_type_node
5715 = build_array_type (wchar_type_node, array_domain_type);
5716
8926095f 5717 if (flag_vtable_thunks)
700f8a87
MS
5718 {
5719 /* Make sure we get a unique function type, so we can give
5720 its pointer type a name. (This wins for gdb.) */
5721 tree vfunc_type = make_node (FUNCTION_TYPE);
5722 TREE_TYPE (vfunc_type) = integer_type_node;
5723 TYPE_ARG_TYPES (vfunc_type) = NULL_TREE;
5724 layout_type (vfunc_type);
5725
5726 vtable_entry_type = build_pointer_type (vfunc_type);
5727 }
8926095f 5728 else
700f8a87
MS
5729 {
5730 vtable_entry_type = make_lang_type (RECORD_TYPE);
5731 fields[0] = build_lang_field_decl (FIELD_DECL, delta_identifier,
5732 delta_type_node);
5733 fields[1] = build_lang_field_decl (FIELD_DECL, index_identifier,
5734 delta_type_node);
5735 fields[2] = build_lang_field_decl (FIELD_DECL, pfn_identifier,
5736 ptr_type_node);
5737 finish_builtin_type (vtable_entry_type, VTBL_PTR_TYPE, fields, 2,
5738 double_type_node);
5739
5740 /* Make this part of an invisible union. */
5741 fields[3] = copy_node (fields[2]);
5742 TREE_TYPE (fields[3]) = delta_type_node;
5743 DECL_NAME (fields[3]) = delta2_identifier;
5744 DECL_MODE (fields[3]) = TYPE_MODE (delta_type_node);
5745 DECL_SIZE (fields[3]) = TYPE_SIZE (delta_type_node);
5746 TREE_UNSIGNED (fields[3]) = 0;
5747 TREE_CHAIN (fields[2]) = fields[3];
5748 vtable_entry_type = build_type_variant (vtable_entry_type, 1, 0);
5749 }
5750 record_builtin_type (RID_MAX, VTBL_PTR_TYPE, vtable_entry_type);
8d08fdba 5751
8d08fdba
MS
5752 vtbl_type_node
5753 = build_array_type (vtable_entry_type, NULL_TREE);
5754 layout_type (vtbl_type_node);
f376e137 5755 vtbl_type_node = cp_build_type_variant (vtbl_type_node, 1, 0);
8d08fdba
MS
5756 record_builtin_type (RID_MAX, NULL_PTR, vtbl_type_node);
5757
5758 /* Simplify life by making a "sigtable_entry_type". Give its
5759 fields names so that the debugger can use them. */
5760
5761 if (flag_handle_signatures)
5762 {
5763 sigtable_entry_type = make_lang_type (RECORD_TYPE);
dff6b454
RK
5764 fields[0] = build_lang_field_decl (FIELD_DECL, tag_identifier,
5765 delta_type_node);
9dd70aa4 5766 fields[1] = build_lang_field_decl (FIELD_DECL, vb_off_identifier,
dff6b454 5767 delta_type_node);
9dd70aa4 5768 fields[2] = build_lang_field_decl (FIELD_DECL, delta_identifier,
dff6b454
RK
5769 delta_type_node);
5770 fields[3] = build_lang_field_decl (FIELD_DECL, index_identifier,
5771 delta_type_node);
9dd70aa4
GB
5772 fields[4] = build_lang_field_decl (FIELD_DECL, pfn_identifier,
5773 ptr_type_node);
5774
5775 /* Set the alignment to the max of the alignment of ptr_type_node and
5776 delta_type_node. Double alignment wastes a word on the Sparc. */
5777 finish_builtin_type (sigtable_entry_type, SIGTABLE_PTR_TYPE, fields, 4,
5778 (TYPE_ALIGN (ptr_type_node) > TYPE_ALIGN (delta_type_node))
5779 ? ptr_type_node
5780 : delta_type_node);
dff6b454
RK
5781
5782 /* Make this part of an invisible union. */
9dd70aa4
GB
5783 fields[5] = copy_node (fields[4]);
5784 TREE_TYPE (fields[5]) = delta_type_node;
5785 DECL_NAME (fields[5]) = vt_off_identifier;
5786 DECL_MODE (fields[5]) = TYPE_MODE (delta_type_node);
5787 DECL_SIZE (fields[5]) = TYPE_SIZE (delta_type_node);
5788 TREE_UNSIGNED (fields[5]) = 0;
5789 TREE_CHAIN (fields[4]) = fields[5];
dff6b454 5790
8d08fdba
MS
5791 sigtable_entry_type = build_type_variant (sigtable_entry_type, 1, 0);
5792 record_builtin_type (RID_MAX, SIGTABLE_PTR_TYPE, sigtable_entry_type);
5793 }
5794
2c73f9f5
ML
5795 std_node = build_decl (NAMESPACE_DECL,
5796 get_identifier (flag_honor_std ? "fake std":"std"),
6633d636
MS
5797 void_type_node);
5798 pushdecl (std_node);
5799
2c73f9f5
ML
5800 global_type_node = make_node (LANG_TYPE);
5801 record_builtin_type (RID_MAX, "global type", global_type_node);
5802
db5ae43f
MS
5803 /* Now, C++. */
5804 current_lang_name = lang_name_cplusplus;
8d08fdba 5805
ced78d8b 5806 {
2c73f9f5
ML
5807 tree bad_alloc_type_node, newtype, deltype;
5808 if (flag_honor_std)
5809 push_namespace (get_identifier ("std"));
5810 bad_alloc_type_node = xref_tag
ced78d8b 5811 (class_type_node, get_identifier ("bad_alloc"), NULL_TREE, 1);
2c73f9f5
ML
5812 if (flag_honor_std)
5813 pop_namespace ();
5814 newtype = build_exception_variant
ced78d8b 5815 (ptr_ftype_sizetype, build_tree_list (NULL_TREE, bad_alloc_type_node));
2c73f9f5 5816 deltype = build_exception_variant
ced78d8b
JM
5817 (void_ftype_ptr, build_tree_list (NULL_TREE, NULL_TREE));
5818 auto_function (ansi_opname[(int) NEW_EXPR], newtype, NOT_BUILT_IN);
5819 auto_function (ansi_opname[(int) VEC_NEW_EXPR], newtype, NOT_BUILT_IN);
5820 auto_function (ansi_opname[(int) DELETE_EXPR], deltype, NOT_BUILT_IN);
5821 auto_function (ansi_opname[(int) VEC_DELETE_EXPR], deltype, NOT_BUILT_IN);
5822 }
8d08fdba
MS
5823
5824 abort_fndecl
de22184b 5825 = define_function ("__pure_virtual", void_ftype,
8d08fdba
MS
5826 NOT_BUILT_IN, 0, 0);
5827
8d08fdba
MS
5828 /* Perform other language dependent initializations. */
5829 init_class_processing ();
5830 init_init_processing ();
5831 init_search_processing ();
1737fe20
BK
5832 if (flag_rtti)
5833 init_rtti_processing ();
8d08fdba 5834
6467930b 5835 if (flag_exceptions)
8d2733ca 5836 init_exception_processing ();
8d08fdba
MS
5837 if (flag_no_inline)
5838 {
5839 flag_inline_functions = 0;
8d08fdba 5840 }
9e9ff709 5841
7fcdf4c2 5842 if (! supports_one_only ())
72b7eeff 5843 flag_weak = 0;
8d08fdba
MS
5844
5845 /* Create the global bindings for __FUNCTION__ and __PRETTY_FUNCTION__. */
5846 declare_function_name ();
5847
5848 /* Prepare to check format strings against argument lists. */
5849 init_function_format_info ();
e9a25f70
JL
5850
5851 /* Show we use EH for cleanups. */
5852 using_eh_for_cleanups ();
8d08fdba
MS
5853}
5854
5855/* Make a definition for a builtin function named NAME and whose data type
5856 is TYPE. TYPE should be a function type with argument types.
5857 FUNCTION_CODE tells later passes how to compile calls to this function.
5858 See tree.h for its possible values.
5859
5860 If LIBRARY_NAME is nonzero, use that for DECL_ASSEMBLER_NAME,
5861 the name to be called if we can't opencode the function. */
5862
5863tree
5864define_function (name, type, function_code, pfn, library_name)
5865 char *name;
5866 tree type;
5867 enum built_in_function function_code;
49c249e1 5868 void (*pfn) PROTO((tree));
8d08fdba
MS
5869 char *library_name;
5870{
5871 tree decl = build_lang_decl (FUNCTION_DECL, get_identifier (name), type);
5872 DECL_EXTERNAL (decl) = 1;
5873 TREE_PUBLIC (decl) = 1;
863adfc0 5874 DECL_ARTIFICIAL (decl) = 1;
8d08fdba 5875
2c73f9f5
ML
5876 my_friendly_assert (DECL_CONTEXT (decl) == NULL_TREE, 392);
5877 DECL_CONTEXT (decl) = current_namespace;
5878
8d08fdba
MS
5879 /* Since `pushdecl' relies on DECL_ASSEMBLER_NAME instead of DECL_NAME,
5880 we cannot change DECL_ASSEMBLER_NAME until we have installed this
5881 function in the namespace. */
5882 if (pfn) (*pfn) (decl);
5883 if (library_name)
5884 DECL_ASSEMBLER_NAME (decl) = get_identifier (library_name);
5885 make_function_rtl (decl);
5886 if (function_code != NOT_BUILT_IN)
5887 {
5888 DECL_BUILT_IN (decl) = 1;
39211cd5 5889 DECL_FUNCTION_CODE (decl) = function_code;
8d08fdba
MS
5890 }
5891 return decl;
5892}
5893\f
5894/* Called when a declaration is seen that contains no names to declare.
5895 If its type is a reference to a structure, union or enum inherited
5896 from a containing scope, shadow that tag name for the current scope
5897 with a forward reference.
5898 If its type defines a new named structure or union
5899 or defines an enum, it is valid but we need not do anything here.
5900 Otherwise, it is an error.
5901
5902 C++: may have to grok the declspecs to learn about static,
5903 complain for anonymous unions. */
5904
5905void
5906shadow_tag (declspecs)
5907 tree declspecs;
5908{
5909 int found_tag = 0;
2986ae00 5910 tree ob_modifier = NULL_TREE;
8d08fdba
MS
5911 register tree link;
5912 register enum tree_code code, ok_code = ERROR_MARK;
5913 register tree t = NULL_TREE;
5914
5915 for (link = declspecs; link; link = TREE_CHAIN (link))
5916 {
5917 register tree value = TREE_VALUE (link);
5918
5919 code = TREE_CODE (value);
5920 if (IS_AGGR_TYPE_CODE (code) || code == ENUMERAL_TYPE)
8d08fdba 5921 {
d2e5ee5c 5922 my_friendly_assert (TYPE_MAIN_DECL (value) != NULL_TREE, 261);
8d08fdba 5923
fc378698 5924 if (IS_AGGR_TYPE (value) && CLASSTYPE_USE_TEMPLATE (value))
5566b478
MS
5925 {
5926 if (CLASSTYPE_IMPLICIT_INSTANTIATION (value)
5927 && TYPE_SIZE (value) == NULL_TREE)
73aad9b9
JM
5928 {
5929 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (value);
5156628f 5930 if (processing_template_decl)
73aad9b9
JM
5931 push_template_decl (TYPE_MAIN_DECL (value));
5932 }
5566b478
MS
5933 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (value))
5934 cp_error ("specialization after instantiation of `%T'", value);
5935 }
5936
b8bca6f2
JM
5937 t = value;
5938 ok_code = code;
5939 found_tag++;
8d08fdba
MS
5940 }
5941 else if (value == ridpointers[(int) RID_STATIC]
2986ae00
MS
5942 || value == ridpointers[(int) RID_EXTERN]
5943 || value == ridpointers[(int) RID_AUTO]
28cbf42c
MS
5944 || value == ridpointers[(int) RID_REGISTER]
5945 || value == ridpointers[(int) RID_INLINE]
5946 || value == ridpointers[(int) RID_VIRTUAL]
5947 || value == ridpointers[(int) RID_EXPLICIT])
2986ae00 5948 ob_modifier = value;
8d08fdba
MS
5949 }
5950
5951 /* This is where the variables in an anonymous union are
5952 declared. An anonymous union declaration looks like:
5953 union { ... } ;
5954 because there is no declarator after the union, the parser
5955 sends that declaration here. */
5956 if (ok_code == UNION_TYPE
5957 && t != NULL_TREE
5958 && ((TREE_CODE (TYPE_NAME (t)) == IDENTIFIER_NODE
5959 && ANON_AGGRNAME_P (TYPE_NAME (t)))
5960 || (TREE_CODE (TYPE_NAME (t)) == TYPE_DECL
5961 && ANON_AGGRNAME_P (TYPE_IDENTIFIER (t)))))
5962 {
d2e5ee5c 5963 /* See also grok_x_components. */
d2e5ee5c
MS
5964 tree *q;
5965
5966 /* Wipe out memory of synthesized methods */
5967 TYPE_HAS_CONSTRUCTOR (t) = 0;
5968 TYPE_HAS_DEFAULT_CONSTRUCTOR (t) = 0;
5969 TYPE_HAS_INIT_REF (t) = 0;
5970 TYPE_HAS_CONST_INIT_REF (t) = 0;
5971 TYPE_HAS_ASSIGN_REF (t) = 0;
5972 TYPE_HAS_ASSIGNMENT (t) = 0;
5973 TYPE_HAS_CONST_ASSIGN_REF (t) = 0;
5974
5975 q = &TYPE_METHODS (t);
5976 while (*q)
5977 {
5978 if (DECL_ARTIFICIAL (*q))
5979 *q = TREE_CHAIN (*q);
5980 else
5981 q = &TREE_CHAIN (*q);
5982 }
f49422da 5983
8d08fdba
MS
5984 /* ANSI C++ June 5 1992 WP 9.5.3. Anonymous unions may not have
5985 function members. */
d2e5ee5c
MS
5986 if (TYPE_METHODS (t))
5987 error ("an anonymous union cannot have function members");
c91a56d2 5988
8d08fdba
MS
5989 if (TYPE_FIELDS (t))
5990 {
a28e3c7f 5991 tree decl = grokdeclarator (NULL_TREE, declspecs, NORMAL, 0,
c11b6f21 5992 NULL_TREE);
8d08fdba
MS
5993 finish_anon_union (decl);
5994 }
8d08fdba
MS
5995 }
5996 else
5997 {
5998 /* Anonymous unions are objects, that's why we only check for
2986ae00 5999 inappropriate specifiers in this branch. */
28cbf42c 6000
2986ae00 6001 if (ob_modifier)
28cbf42c
MS
6002 {
6003 if (ob_modifier == ridpointers[(int) RID_INLINE]
6004 || ob_modifier == ridpointers[(int) RID_VIRTUAL])
6005 cp_error ("`%D' can only be specified for functions", ob_modifier);
6006 else if (ob_modifier == ridpointers[(int) RID_EXPLICIT])
6007 cp_error ("`%D' can only be specified for constructors",
6008 ob_modifier);
6009 else
6010 cp_error ("`%D' can only be specified for objects and functions",
6011 ob_modifier);
6012 }
8d08fdba 6013
b8bca6f2 6014 if (found_tag == 0)
2df06032 6015 cp_error ("abstract declarator used as declaration");
b8bca6f2 6016 else if (found_tag > 1)
2986ae00 6017 pedwarn ("multiple types in one declaration");
8d08fdba
MS
6018 }
6019}
6020\f
6021/* Decode a "typename", such as "int **", returning a ..._TYPE node. */
6022
6023tree
6024groktypename (typename)
6025 tree typename;
6026{
6027 if (TREE_CODE (typename) != TREE_LIST)
6028 return typename;
6029 return grokdeclarator (TREE_VALUE (typename),
6030 TREE_PURPOSE (typename),
c11b6f21 6031 TYPENAME, 0, NULL_TREE);
8d08fdba
MS
6032}
6033
6034/* Decode a declarator in an ordinary declaration or data definition.
6035 This is called as soon as the type information and variable name
6036 have been parsed, before parsing the initializer if any.
6037 Here we create the ..._DECL node, fill in its type,
6038 and put it on the list of decls for the current context.
6039 The ..._DECL node is returned as the value.
6040
6041 Exception: for arrays where the length is not specified,
82580166 6042 the type is left null, to be filled in by `cp_finish_decl'.
8d08fdba
MS
6043
6044 Function definitions do not come here; they go to start_function
6045 instead. However, external and forward declarations of functions
6046 do go through here. Structure field declarations are done by
6047 grokfield and not through here. */
6048
6049/* Set this to zero to debug not using the temporary obstack
6050 to parse initializers. */
6051int debug_temp_inits = 1;
6052
6053tree
a1774733 6054start_decl (declarator, declspecs, initialized, attributes, prefix_attributes)
8d08fdba
MS
6055 tree declarator, declspecs;
6056 int initialized;
a1774733 6057 tree attributes, prefix_attributes;
8d08fdba
MS
6058{
6059 register tree decl;
6060 register tree type, tem;
6061 tree context;
6062 extern int have_extern_spec;
6063 extern int used_extern_spec;
6064
5566b478
MS
6065#if 0
6066 /* See code below that used this. */
8d08fdba 6067 int init_written = initialized;
5566b478 6068#endif
8d08fdba 6069
e92cc029 6070 /* This should only be done once on the top most decl. */
8d08fdba
MS
6071 if (have_extern_spec && !used_extern_spec)
6072 {
a28e3c7f
MS
6073 declspecs = decl_tree_cons (NULL_TREE, get_identifier ("extern"),
6074 declspecs);
8d08fdba
MS
6075 used_extern_spec = 1;
6076 }
6077
c11b6f21 6078 decl = grokdeclarator (declarator, declspecs, NORMAL, initialized,
f30432d7 6079 NULL_TREE);
a1774733 6080 if (decl == NULL_TREE || TREE_CODE (decl) == VOID_TYPE)
8d08fdba
MS
6081 return NULL_TREE;
6082
6083 type = TREE_TYPE (decl);
6084
6085 /* Don't lose if destructors must be executed at file-level. */
5156628f 6086 if (! processing_template_decl && TREE_STATIC (decl)
ec255269 6087 && TYPE_NEEDS_DESTRUCTOR (complete_type (type))
8d08fdba
MS
6088 && !TREE_PERMANENT (decl))
6089 {
6090 push_obstacks (&permanent_obstack, &permanent_obstack);
6091 decl = copy_node (decl);
6092 if (TREE_CODE (type) == ARRAY_TYPE)
6093 {
6094 tree itype = TYPE_DOMAIN (type);
6095 if (itype && ! TREE_PERMANENT (itype))
6096 {
6097 itype = build_index_type (copy_to_permanent (TYPE_MAX_VALUE (itype)));
6098 type = build_cplus_array_type (TREE_TYPE (type), itype);
6099 TREE_TYPE (decl) = type;
6100 }
6101 }
6102 pop_obstacks ();
6103 }
6104
8d08fdba
MS
6105 context
6106 = (TREE_CODE (decl) == FUNCTION_DECL && DECL_VIRTUAL_P (decl))
6107 ? DECL_CLASS_CONTEXT (decl)
6108 : DECL_CONTEXT (decl);
6109
2c73f9f5
ML
6110 /* We are only interested in class contexts, later. */
6111 if (context && TREE_CODE (context) == NAMESPACE_DECL)
6112 context = NULL_TREE;
6113
8d08fdba
MS
6114 if (initialized)
6115 /* Is it valid for this decl to have an initializer at all?
6116 If not, set INITIALIZED to zero, which will indirectly
82580166 6117 tell `cp_finish_decl' to ignore the initializer once it is parsed. */
8d08fdba
MS
6118 switch (TREE_CODE (decl))
6119 {
6120 case TYPE_DECL:
6121 /* typedef foo = bar means give foo the same type as bar.
82580166 6122 We haven't parsed bar yet, so `cp_finish_decl' will fix that up.
8d08fdba
MS
6123 Any other case of an initialization in a TYPE_DECL is an error. */
6124 if (pedantic || list_length (declspecs) > 1)
6125 {
6126 cp_error ("typedef `%D' is initialized", decl);
6127 initialized = 0;
6128 }
6129 break;
6130
6131 case FUNCTION_DECL:
6132 cp_error ("function `%#D' is initialized like a variable", decl);
6133 initialized = 0;
6134 break;
6135
6136 default:
5156628f 6137 if (! processing_template_decl)
8d08fdba 6138 {
bd6dd845 6139 if (type != error_mark_node)
5156628f 6140 {
bd6dd845
MS
6141 if (TYPE_SIZE (type) != NULL_TREE
6142 && ! TREE_CONSTANT (TYPE_SIZE (type)))
6143 {
6144 cp_error
6145 ("variable-sized object `%D' may not be initialized",
6146 decl);
6147 initialized = 0;
6148 }
5156628f 6149
bd6dd845
MS
6150 if (TREE_CODE (type) == ARRAY_TYPE
6151 && TYPE_SIZE (complete_type (TREE_TYPE (type))) == NULL_TREE)
6152 {
6153 cp_error
6154 ("elements of array `%#D' have incomplete type", decl);
6155 initialized = 0;
6156 }
5156628f 6157 }
8d08fdba
MS
6158 }
6159 }
6160
8d08fdba
MS
6161 if (initialized)
6162 {
a9aedbc2 6163 if (! toplevel_bindings_p ()
8d08fdba
MS
6164 && DECL_EXTERNAL (decl))
6165 cp_warning ("declaration of `%#D' has `extern' and is initialized",
6166 decl);
6167 DECL_EXTERNAL (decl) = 0;
5566b478 6168 if (toplevel_bindings_p ())
8d08fdba
MS
6169 TREE_STATIC (decl) = 1;
6170
6171 /* Tell `pushdecl' this is an initialized decl
6172 even though we don't yet have the initializer expression.
82580166 6173 Also tell `cp_finish_decl' it may store the real initializer. */
8d08fdba
MS
6174 DECL_INITIAL (decl) = error_mark_node;
6175 }
6176
5566b478 6177 if (context && TYPE_SIZE (complete_type (context)) != NULL_TREE)
5b605f68
MS
6178 {
6179 if (TREE_CODE (decl) == VAR_DECL)
6180 {
6181 tree field = lookup_field (context, DECL_NAME (decl), 0, 0);
6182 if (field == NULL_TREE || TREE_CODE (field) != VAR_DECL)
6183 cp_error ("`%#D' is not a static member of `%#T'", decl, context);
e349ee73
MS
6184 else
6185 {
6186 if (DECL_CONTEXT (field) != context)
f2d773a2
JM
6187 {
6188 cp_pedwarn ("ANSI C++ does not permit `%T::%D' to be defined as `%T::%D'",
6189 DECL_CONTEXT (field), DECL_NAME (decl),
6190 context, DECL_NAME (decl));
6191 DECL_CONTEXT (decl) = DECL_CONTEXT (field);
6192 }
75650646
MM
6193 /* Static data member are tricky; an in-class initialization
6194 still doesn't provide a definition, so the in-class
6195 declaration will have DECL_EXTERNAL set, but will have an
6196 initialization. Thus, duplicate_decls won't warn
6197 about this situation, and so we check here. */
6198 if (DECL_INITIAL (decl) && DECL_INITIAL (field))
6199 cp_error ("duplicate initialization of %D", decl);
e349ee73
MS
6200 if (duplicate_decls (decl, field))
6201 decl = field;
6202 }
5b605f68 6203 }
f30432d7
MS
6204 else
6205 {
5566b478 6206 tree field = check_classfn (context, decl);
f30432d7
MS
6207 if (field && duplicate_decls (decl, field))
6208 decl = field;
6209 }
6210
6211 /* cp_finish_decl sets DECL_EXTERNAL if DECL_IN_AGGR_P is set. */
5b605f68
MS
6212 if (DECL_LANG_SPECIFIC (decl))
6213 DECL_IN_AGGR_P (decl) = 0;
f30432d7
MS
6214 if (DECL_USE_TEMPLATE (decl) || CLASSTYPE_USE_TEMPLATE (context))
6215 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
6216
6217 /* Stupid stupid stupid stupid (jason 7/21/95) */
6218 if (pedantic && DECL_EXTERNAL (decl)
6219 && ! DECL_TEMPLATE_SPECIALIZATION (decl))
6220 cp_pedwarn ("declaration of `%#D' outside of class is not definition",
6221 decl);
6222
5b605f68
MS
6223 pushclass (context, 2);
6224 }
6225
a1774733
BK
6226 /* Set attributes here so if duplicate decl, will have proper attributes. */
6227 cplus_decl_attributes (decl, attributes, prefix_attributes);
6228
8d08fdba
MS
6229 /* Add this decl to the current binding level, but not if it
6230 comes from another scope, e.g. a static member variable.
6231 TEM may equal DECL or it may be a previous decl of the same name. */
5b605f68 6232
6eb3bb27 6233 if ((TREE_CODE (decl) != PARM_DECL && DECL_CONTEXT (decl) != NULL_TREE)
2c73f9f5 6234 || (TREE_CODE (decl) == TEMPLATE_DECL && !namespace_bindings_p ())
75650646
MM
6235 || TREE_CODE (type) == LANG_TYPE
6236 /* The declaration of template specializations does not affect
6237 the functions available for overload resolution, so we do not
6238 call pushdecl. */
e1467ff2 6239 || (TREE_CODE (decl) == FUNCTION_DECL
75650646 6240 && DECL_TEMPLATE_SPECIALIZATION (decl)))
8d08fdba
MS
6241 tem = decl;
6242 else
8926095f 6243 tem = pushdecl (decl);
2ee887f2 6244
5156628f 6245 if (processing_template_decl)
5566b478
MS
6246 {
6247 if (! current_function_decl)
3ac3d9ea 6248 tem = push_template_decl (tem);
5566b478
MS
6249 else if (minimal_parse_mode)
6250 DECL_VINDEX (decl)
6251 = build_min_nt (DECL_STMT, copy_to_permanent (declarator),
6252 copy_to_permanent (declspecs),
c11b6f21 6253 NULL_TREE);
5566b478
MS
6254 }
6255
6256
2ee887f2 6257#if ! defined (ASM_OUTPUT_BSS) && ! defined (ASM_OUTPUT_ALIGNED_BSS)
a3203465 6258 /* Tell the back-end to use or not use .common as appropriate. If we say
a50f0918
MS
6259 -fconserve-space, we want this to save .data space, at the expense of
6260 wrong semantics. If we say -fno-conserve-space, we want this to
6261 produce errors about redefs; to do this we force variables into the
6262 data segment. */
a3203465 6263 DECL_COMMON (tem) = flag_conserve_space || ! TREE_PUBLIC (tem);
2ee887f2 6264#endif
8d08fdba 6265
5156628f 6266 if (! processing_template_decl)
5566b478 6267 start_decl_1 (tem);
8d08fdba 6268
5566b478
MS
6269 /* Corresponding pop_obstacks is done in `cp_finish_decl'. */
6270 push_obstacks_nochange ();
8d08fdba 6271
5566b478
MS
6272#if 0
6273 /* We have no way of knowing whether the initializer will need to be
6274 evaluated at run-time or not until we've parsed it, so let's just put
6275 it in the permanent obstack. (jason) */
8d08fdba
MS
6276 if (init_written
6277 && ! (TREE_CODE (tem) == PARM_DECL
6278 || (TREE_READONLY (tem)
6279 && (TREE_CODE (tem) == VAR_DECL
6280 || TREE_CODE (tem) == FIELD_DECL))))
6281 {
6282 /* When parsing and digesting the initializer,
6283 use temporary storage. Do this even if we will ignore the value. */
a9aedbc2 6284 if (toplevel_bindings_p () && debug_temp_inits)
8d08fdba 6285 {
5156628f 6286 if (processing_template_decl
5566b478 6287 || TYPE_NEEDS_CONSTRUCTING (type)
a28e3c7f 6288 || TREE_CODE (type) == REFERENCE_TYPE)
8d08fdba
MS
6289 /* In this case, the initializer must lay down in permanent
6290 storage, since it will be saved until `finish_file' is run. */
6291 ;
6292 else
6293 temporary_allocation ();
6294 }
6295 }
5566b478 6296#endif
8d08fdba 6297
8d08fdba
MS
6298 return tem;
6299}
6300
5566b478
MS
6301void
6302start_decl_1 (decl)
6303 tree decl;
8d08fdba 6304{
5566b478
MS
6305 tree type = TREE_TYPE (decl);
6306 int initialized = (DECL_INITIAL (decl) != NULL_TREE);
8d08fdba 6307
5566b478
MS
6308 /* If this type of object needs a cleanup, and control may
6309 jump past it, make a new binding level so that it is cleaned
6310 up only when it is initialized first. */
6311 if (TYPE_NEEDS_DESTRUCTOR (type)
6312 && current_binding_level->more_cleanups_ok == 0)
6313 pushlevel_temporary (1);
6314
6315 if (initialized)
6316 /* Is it valid for this decl to have an initializer at all?
6317 If not, set INITIALIZED to zero, which will indirectly
6318 tell `cp_finish_decl' to ignore the initializer once it is parsed. */
8d08fdba 6319 {
5566b478
MS
6320 /* Don't allow initializations for incomplete types except for
6321 arrays which might be completed by the initialization. */
6322 if (type == error_mark_node)
6323 ; /* Don't complain again. */
6324 else if (TYPE_SIZE (complete_type (type)) != NULL_TREE)
6325 ; /* A complete type is ok. */
6326 else if (TREE_CODE (type) != ARRAY_TYPE)
8d08fdba 6327 {
5566b478
MS
6328 cp_error ("variable `%#D' has initializer but incomplete type",
6329 decl);
6330 initialized = 0;
6331 }
6332 else if (TYPE_SIZE (complete_type (TREE_TYPE (type))) == NULL_TREE)
6333 {
6334 if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
6335 cp_error ("elements of array `%#D' have incomplete type", decl);
6336 /* else we already gave an error in start_decl. */
6337 initialized = 0;
8d08fdba 6338 }
8d08fdba
MS
6339 }
6340
5566b478
MS
6341 if (!initialized
6342 && TREE_CODE (decl) != TYPE_DECL
6343 && TREE_CODE (decl) != TEMPLATE_DECL
6344 && IS_AGGR_TYPE (type) && ! DECL_EXTERNAL (decl))
8d08fdba 6345 {
5156628f 6346 if ((! processing_template_decl || ! uses_template_parms (type))
7fcdf4c2 6347 && TYPE_SIZE (complete_type (type)) == NULL_TREE)
5566b478
MS
6348 {
6349 cp_error ("aggregate `%#D' has incomplete type and cannot be initialized",
6350 decl);
6351 /* Change the type so that assemble_variable will give
6352 DECL an rtl we can live with: (mem (const_int 0)). */
6353 TREE_TYPE (decl) = error_mark_node;
6354 type = error_mark_node;
6355 }
6356 else
6357 {
6358 /* If any base type in the hierarchy of TYPE needs a constructor,
6359 then we set initialized to 1. This way any nodes which are
6360 created for the purposes of initializing this aggregate
6361 will live as long as it does. This is necessary for global
6362 aggregates which do not have their initializers processed until
6363 the end of the file. */
6364 initialized = TYPE_NEEDS_CONSTRUCTING (type);
6365 }
6366 }
6367
6368#if 0
6369 /* We don't do this yet for GNU C++. */
6370 /* For a local variable, define the RTL now. */
6371 if (! toplevel_bindings_p ()
6372 /* But not if this is a duplicate decl
6373 and we preserved the rtl from the previous one
6374 (which may or may not happen). */
6375 && DECL_RTL (tem) == NULL_RTX)
6376 {
6377 if (TYPE_SIZE (TREE_TYPE (tem)) != NULL_TREE)
6378 expand_decl (tem);
6379 else if (TREE_CODE (TREE_TYPE (tem)) == ARRAY_TYPE
6380 && DECL_INITIAL (tem) != NULL_TREE)
6381 expand_decl (tem);
6382 }
6383#endif
6384
6385 if (! initialized)
6386 DECL_INITIAL (decl) = NULL_TREE;
6387}
6388
6389/* Handle initialization of references.
38e01259 6390 These three arguments are from `cp_finish_decl', and have the
e92cc029
MS
6391 same meaning here that they do there.
6392
6393 Quotes on semantics can be found in ARM 8.4.3. */
6394
5566b478 6395static void
a703fb38 6396grok_reference_init (decl, type, init)
5566b478 6397 tree decl, type, init;
5566b478
MS
6398{
6399 tree tmp;
6400
6401 if (init == NULL_TREE)
6402 {
6403 if ((DECL_LANG_SPECIFIC (decl) == 0
6404 || DECL_IN_AGGR_P (decl) == 0)
6405 && ! DECL_THIS_EXTERN (decl))
6406 {
6407 cp_error ("`%D' declared as reference but not initialized", decl);
6408 if (TREE_CODE (decl) == VAR_DECL)
6409 SET_DECL_REFERENCE_SLOT (decl, error_mark_node);
6410 }
6411 return;
6412 }
6413
6414 if (init == error_mark_node)
6415 return;
6416
6417 if (TREE_CODE (type) == REFERENCE_TYPE
6418 && TREE_CODE (init) == CONSTRUCTOR)
6419 {
6420 cp_error ("ANSI C++ forbids use of initializer list to initialize reference `%D'", decl);
8d08fdba
MS
6421 return;
6422 }
6423
ec255269
MS
6424 if (TREE_TYPE (init) && TREE_CODE (TREE_TYPE (init)) == UNKNOWN_TYPE)
6425 /* decay_conversion is probably wrong for references to functions. */
6426 init = decay_conversion (instantiate_type (TREE_TYPE (type), init, 1));
6427
8d08fdba
MS
6428 if (TREE_CODE (init) == TREE_LIST)
6429 init = build_compound_expr (init);
8d08fdba 6430
8ccc31eb
MS
6431 if (TREE_CODE (TREE_TYPE (init)) == REFERENCE_TYPE)
6432 init = convert_from_reference (init);
6433
8d08fdba
MS
6434 if (TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE
6435 && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE)
6436 {
a3203465 6437 /* Note: default conversion is only called in very special cases. */
8d08fdba
MS
6438 init = default_conversion (init);
6439 }
6440
a3203465 6441 tmp = convert_to_reference
9a3b49ac
MS
6442 (type, init, CONV_IMPLICIT,
6443 LOOKUP_SPECULATIVELY|LOOKUP_NORMAL|DIRECT_BIND, decl);
8d08fdba 6444
a3203465
MS
6445 if (tmp == error_mark_node)
6446 goto fail;
6447 else if (tmp != NULL_TREE)
8d08fdba 6448 {
a3203465 6449 init = tmp;
4c7bdca6 6450 DECL_INITIAL (decl) = save_expr (init);
8d08fdba 6451 }
a3203465 6452 else
8d08fdba 6453 {
a3203465
MS
6454 cp_error ("cannot initialize `%T' from `%T'", type, TREE_TYPE (init));
6455 goto fail;
8d08fdba 6456 }
8d08fdba 6457
8d08fdba
MS
6458 /* ?? Can this be optimized in some cases to
6459 hand back the DECL_INITIAL slot?? */
6460 if (TYPE_SIZE (TREE_TYPE (type)))
6461 {
6462 init = convert_from_reference (decl);
6463 if (TREE_PERMANENT (decl))
6464 init = copy_to_permanent (init);
6465 SET_DECL_REFERENCE_SLOT (decl, init);
6466 }
6467
6468 if (TREE_STATIC (decl) && ! TREE_CONSTANT (DECL_INITIAL (decl)))
6469 {
6470 expand_static_init (decl, DECL_INITIAL (decl));
6471 DECL_INITIAL (decl) = NULL_TREE;
6472 }
6473 return;
6474
6475 fail:
6476 if (TREE_CODE (decl) == VAR_DECL)
6477 SET_DECL_REFERENCE_SLOT (decl, error_mark_node);
6478 return;
6479}
6480
6060a796
MS
6481/* Fill in DECL_INITIAL with some magical value to prevent expand_decl from
6482 mucking with forces it does not comprehend (i.e. initialization with a
6483 constructor). If we are at global scope and won't go into COMMON, fill
6484 it in with a dummy CONSTRUCTOR to force the variable into .data;
6485 otherwise we can use error_mark_node. */
6486
28cbf42c
MS
6487static tree
6488obscure_complex_init (decl, init)
6489 tree decl, init;
6060a796 6490{
28cbf42c
MS
6491 if (! flag_no_inline && TREE_STATIC (decl))
6492 {
6493 if (extract_init (decl, init))
6494 return NULL_TREE;
6495 }
6496
2ee887f2 6497#if ! defined (ASM_OUTPUT_BSS) && ! defined (ASM_OUTPUT_ALIGNED_BSS)
a9aedbc2 6498 if (toplevel_bindings_p () && ! DECL_COMMON (decl))
6060a796
MS
6499 DECL_INITIAL (decl) = build (CONSTRUCTOR, TREE_TYPE (decl), NULL_TREE,
6500 NULL_TREE);
6501 else
2ee887f2 6502#endif
6060a796 6503 DECL_INITIAL (decl) = error_mark_node;
28cbf42c
MS
6504
6505 return init;
6060a796
MS
6506}
6507
8d08fdba
MS
6508/* Finish processing of a declaration;
6509 install its line number and initial value.
6510 If the length of an array type is not known before,
6511 it must be determined now, from the initial value, or it is an error.
6512
6513 Call `pop_obstacks' iff NEED_POP is nonzero.
6514
82580166 6515 For C++, `cp_finish_decl' must be fairly evasive: it must keep initializers
8d08fdba
MS
6516 for aggregates that have constructors alive on the permanent obstack,
6517 so that the global initializing functions can be written at the end.
6518
6519 INIT0 holds the value of an initializer that should be allowed to escape
6520 the normal rules.
6521
6060a796
MS
6522 FLAGS is LOOKUP_ONLYCONVERTING is the = init syntax was used, else 0
6523 if the (init) syntax was used.
6524
8d08fdba 6525 For functions that take default parameters, DECL points to its
82580166 6526 "maximal" instantiation. `cp_finish_decl' must then also declared its
8d08fdba
MS
6527 subsequently lower and lower forms of instantiation, checking for
6528 ambiguity as it goes. This can be sped up later. */
6529
6530void
82580166 6531cp_finish_decl (decl, init, asmspec_tree, need_pop, flags)
8d08fdba
MS
6532 tree decl, init;
6533 tree asmspec_tree;
6534 int need_pop;
6060a796 6535 int flags;
8d08fdba
MS
6536{
6537 register tree type;
a703fb38 6538 tree cleanup = NULL_TREE, ttype = NULL_TREE;
8d08fdba
MS
6539 int was_incomplete;
6540 int temporary = allocation_temporary_p ();
6541 char *asmspec = NULL;
6542 int was_readonly = 0;
0c4b14c4 6543 int already_used = 0;
8d08fdba
MS
6544
6545 /* If this is 0, then we did not change obstacks. */
6546 if (! decl)
6547 {
6548 if (init)
6549 error ("assignment (not initialization) in declaration");
6550 return;
6551 }
6552
a4443a08 6553 /* If a name was specified, get the string. */
8d08fdba 6554 if (asmspec_tree)
8d08fdba 6555 asmspec = TREE_STRING_POINTER (asmspec_tree);
8d08fdba 6556
2c73f9f5
ML
6557 if (init && TREE_CODE (init) == NAMESPACE_DECL)
6558 {
6559 cp_error ("Cannot initialize `%D' to namespace `%D'",
6560 decl, init);
6561 init = NULL_TREE;
6562 }
6563
8d08fdba
MS
6564 /* If the type of the thing we are declaring either has
6565 a constructor, or has a virtual function table pointer,
6566 AND its initialization was accepted by `start_decl',
6567 then we stayed on the permanent obstack through the
6568 declaration, otherwise, changed obstacks as GCC would. */
6569
6570 type = TREE_TYPE (decl);
6571
f376e137 6572 if (type == error_mark_node)
eac293a1 6573 {
a9aedbc2 6574 if (toplevel_bindings_p () && temporary)
eac293a1
MS
6575 end_temporary_allocation ();
6576
6577 return;
6578 }
f376e137 6579
5156628f 6580 if (processing_template_decl)
5566b478
MS
6581 {
6582 if (init && DECL_INITIAL (decl))
6583 DECL_INITIAL (decl) = init;
faf5394a 6584 if (minimal_parse_mode && ! DECL_ARTIFICIAL (decl))
5566b478
MS
6585 {
6586 tree stmt = DECL_VINDEX (decl);
e1467ff2 6587 /* If the decl is declaring a member of a local class (in a
8857f91e
MM
6588 template function), the DECL_VINDEX will either be NULL,
6589 or it will be an actual virtual function index, not a
6590 DECL_STMT. */
6591 if (stmt != NULL_TREE && TREE_CODE (stmt) == DECL_STMT)
75650646
MM
6592 {
6593 DECL_VINDEX (decl) = NULL_TREE;
6594 TREE_OPERAND (stmt, 2) = copy_to_permanent (init);
6595 add_tree (stmt);
6596 }
5566b478 6597 }
8d08fdba 6598
5566b478
MS
6599 goto finish_end0;
6600 }
8d08fdba
MS
6601 /* Take care of TYPE_DECLs up front. */
6602 if (TREE_CODE (decl) == TYPE_DECL)
6603 {
6604 if (init && DECL_INITIAL (decl))
6605 {
6606 /* typedef foo = bar; store the type of bar as the type of foo. */
6607 TREE_TYPE (decl) = type = TREE_TYPE (init);
6608 DECL_INITIAL (decl) = init = NULL_TREE;
6609 }
a0a33927
MS
6610 if (type != error_mark_node
6611 && IS_AGGR_TYPE (type) && DECL_NAME (decl))
8d08fdba
MS
6612 {
6613 if (TREE_TYPE (DECL_NAME (decl)) && TREE_TYPE (decl) != type)
6614 cp_warning ("shadowing previous type declaration of `%#D'", decl);
6615 set_identifier_type_value (DECL_NAME (decl), type);
6616 CLASSTYPE_GOT_SEMICOLON (type) = 1;
6617 }
6618 GNU_xref_decl (current_function_decl, decl);
cffa8729
MS
6619
6620 /* If we have installed this as the canonical typedef for this
6621 type, and that type has not been defined yet, delay emitting
956d6950 6622 the debug information for it, as we will emit it later. */
d2e5ee5c 6623 if (TYPE_MAIN_DECL (TREE_TYPE (decl)) == decl
cffa8729
MS
6624 && TYPE_SIZE (TREE_TYPE (decl)) == NULL_TREE)
6625 TYPE_DECL_SUPPRESS_DEBUG (decl) = 1;
6626
8d08fdba 6627 rest_of_decl_compilation (decl, NULL_PTR,
5566b478 6628 DECL_CONTEXT (decl) == NULL_TREE, at_eof);
8d08fdba
MS
6629 goto finish_end;
6630 }
8d08fdba
MS
6631 if (TREE_CODE (decl) != FUNCTION_DECL)
6632 {
6633 ttype = target_type (type);
8d08fdba
MS
6634 }
6635
6636 if (! DECL_EXTERNAL (decl) && TREE_READONLY (decl)
6637 && TYPE_NEEDS_CONSTRUCTING (type))
6638 {
6639
6640 /* Currently, GNU C++ puts constants in text space, making them
6641 impossible to initialize. In the future, one would hope for
6642 an operating system which understood the difference between
6643 initialization and the running of a program. */
6644 was_readonly = 1;
6645 TREE_READONLY (decl) = 0;
6646 }
6647
6648 if (TREE_CODE (decl) == FIELD_DECL)
6649 {
6650 if (init && init != error_mark_node)
6651 my_friendly_assert (TREE_PERMANENT (init), 147);
6652
6653 if (asmspec)
6654 {
6655 /* This must override the asm specifier which was placed
6060a796 6656 by grokclassfn. Lay this out fresh. */
8d08fdba
MS
6657 DECL_RTL (TREE_TYPE (decl)) = NULL_RTX;
6658 DECL_ASSEMBLER_NAME (decl) = get_identifier (asmspec);
6659 make_decl_rtl (decl, asmspec, 0);
6660 }
6661 }
6662 /* If `start_decl' didn't like having an initialization, ignore it now. */
6663 else if (init != NULL_TREE && DECL_INITIAL (decl) == NULL_TREE)
6664 init = NULL_TREE;
6665 else if (DECL_EXTERNAL (decl))
6666 ;
6667 else if (TREE_CODE (type) == REFERENCE_TYPE
6668 || (TYPE_LANG_SPECIFIC (type) && IS_SIGNATURE_REFERENCE (type)))
6669 {
f376e137
MS
6670 if (TREE_STATIC (decl))
6671 make_decl_rtl (decl, NULL_PTR,
a9aedbc2 6672 toplevel_bindings_p ()
f376e137 6673 || pseudo_global_level_p ());
a703fb38 6674 grok_reference_init (decl, type, init);
8d08fdba
MS
6675 init = NULL_TREE;
6676 }
6677
6678 GNU_xref_decl (current_function_decl, decl);
6679
a0a33927 6680 if (TREE_CODE (decl) == FIELD_DECL)
8d08fdba
MS
6681 ;
6682 else if (TREE_CODE (decl) == CONST_DECL)
6683 {
6684 my_friendly_assert (TREE_CODE (decl) != REFERENCE_TYPE, 148);
6685
6686 DECL_INITIAL (decl) = init;
6687
6688 /* This will keep us from needing to worry about our obstacks. */
6689 my_friendly_assert (init != NULL_TREE, 149);
6690 init = NULL_TREE;
6691 }
6692 else if (init)
6693 {
6694 if (TYPE_HAS_CONSTRUCTOR (type) || TYPE_NEEDS_CONSTRUCTING (type))
6695 {
6696 if (TREE_CODE (type) == ARRAY_TYPE)
6697 init = digest_init (type, init, (tree *) 0);
a3203465 6698 else if (TREE_CODE (init) == CONSTRUCTOR)
8d08fdba 6699 {
f30432d7 6700 if (TYPE_NON_AGGREGATE_CLASS (type))
8d08fdba 6701 {
a28e3c7f
MS
6702 cp_error ("`%D' must be initialized by constructor, not by `{...}'",
6703 decl);
8d08fdba
MS
6704 init = error_mark_node;
6705 }
6706 else
6707 goto dont_use_constructor;
6708 }
8d08fdba
MS
6709 }
6710 else
6711 {
6712 dont_use_constructor:
6713 if (TREE_CODE (init) != TREE_VEC)
6714 init = store_init_value (decl, init);
8d08fdba 6715 }
28cbf42c
MS
6716
6717 if (init)
6718 /* We must hide the initializer so that expand_decl
6719 won't try to do something it does not understand. */
6720 init = obscure_complex_init (decl, init);
8d08fdba 6721 }
a0a33927
MS
6722 else if (DECL_EXTERNAL (decl))
6723 ;
8d08fdba
MS
6724 else if (TREE_CODE_CLASS (TREE_CODE (type)) == 't'
6725 && (IS_AGGR_TYPE (type) || TYPE_NEEDS_CONSTRUCTING (type)))
6726 {
6727 tree ctype = type;
6728 while (TREE_CODE (ctype) == ARRAY_TYPE)
6729 ctype = TREE_TYPE (ctype);
6730 if (! TYPE_NEEDS_CONSTRUCTING (ctype))
6731 {
6732 if (CLASSTYPE_READONLY_FIELDS_NEED_INIT (ctype))
6733 cp_error ("structure `%D' with uninitialized const members", decl);
6734 if (CLASSTYPE_REF_FIELDS_NEED_INIT (ctype))
a28e3c7f
MS
6735 cp_error ("structure `%D' with uninitialized reference members",
6736 decl);
8d08fdba
MS
6737 }
6738
6739 if (TREE_CODE (decl) == VAR_DECL
6740 && !DECL_INITIAL (decl)
6741 && !TYPE_NEEDS_CONSTRUCTING (type)
6742 && (TYPE_READONLY (type) || TREE_READONLY (decl)))
6743 cp_error ("uninitialized const `%D'", decl);
6744
6060a796
MS
6745 if (TYPE_SIZE (type) != NULL_TREE
6746 && TYPE_NEEDS_CONSTRUCTING (type))
28cbf42c 6747 init = obscure_complex_init (decl, NULL_TREE);
8d08fdba
MS
6748 }
6749 else if (TREE_CODE (decl) == VAR_DECL
6750 && TREE_CODE (type) != REFERENCE_TYPE
6751 && (TYPE_READONLY (type) || TREE_READONLY (decl)))
6752 {
6753 /* ``Unless explicitly declared extern, a const object does not have
6754 external linkage and must be initialized. ($8.4; $12.1)'' ARM 7.1.6
6755 However, if it's `const int foo = 1; const int foo;', don't complain
6756 about the second decl, since it does have an initializer before.
6757 We deliberately don't complain about arrays, because they're
6758 supposed to be initialized by a constructor. */
6759 if (! DECL_INITIAL (decl)
6760 && TREE_CODE (type) != ARRAY_TYPE
6761 && (!pedantic || !current_class_type))
6762 cp_error ("uninitialized const `%#D'", decl);
6763 }
6764
6765 /* For top-level declaration, the initial value was read in
6766 the temporary obstack. MAXINDEX, rtl, etc. to be made below
6767 must go in the permanent obstack; but don't discard the
6768 temporary data yet. */
6769
a9aedbc2 6770 if (toplevel_bindings_p () && temporary)
8d08fdba
MS
6771 end_temporary_allocation ();
6772
6773 /* Deduce size of array from initialization, if not already known. */
6774
6775 if (TREE_CODE (type) == ARRAY_TYPE
6776 && TYPE_DOMAIN (type) == NULL_TREE
6777 && TREE_CODE (decl) != TYPE_DECL)
6778 {
6779 int do_default
6780 = (TREE_STATIC (decl)
6781 /* Even if pedantic, an external linkage array
6782 may have incomplete type at first. */
6783 ? pedantic && ! DECL_EXTERNAL (decl)
6784 : !DECL_EXTERNAL (decl));
6785 tree initializer = init ? init : DECL_INITIAL (decl);
6786 int failure = complete_array_type (type, initializer, do_default);
6787
6788 if (failure == 1)
6789 cp_error ("initializer fails to determine size of `%D'", decl);
6790
6791 if (failure == 2)
6792 {
6793 if (do_default)
6794 cp_error ("array size missing in `%D'", decl);
6795 /* If a `static' var's size isn't known, make it extern as
6796 well as static, so it does not get allocated. If it's not
6797 `static', then don't mark it extern; finish_incomplete_decl
6798 will give it a default size and it will get allocated. */
6799 else if (!pedantic && TREE_STATIC (decl) && !TREE_PUBLIC (decl))
6800 DECL_EXTERNAL (decl) = 1;
6801 }
6802
6803 if (pedantic && TYPE_DOMAIN (type) != NULL_TREE
6804 && tree_int_cst_lt (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
6805 integer_zero_node))
6806 cp_error ("zero-size array `%D'", decl);
6807
6808 layout_decl (decl, 0);
6809 }
6810
6811 if (TREE_CODE (decl) == VAR_DECL)
6812 {
6813 if (DECL_SIZE (decl) == NULL_TREE
ec255269 6814 && TYPE_SIZE (complete_type (TREE_TYPE (decl))) != NULL_TREE)
8d08fdba
MS
6815 layout_decl (decl, 0);
6816
6817 if (TREE_STATIC (decl) && DECL_SIZE (decl) == NULL_TREE)
6818 {
6819 /* A static variable with an incomplete type:
6820 that is an error if it is initialized.
6821 Otherwise, let it through, but if it is not `extern'
6822 then it may cause an error message later. */
6823 if (DECL_INITIAL (decl) != NULL_TREE)
6824 cp_error ("storage size of `%D' isn't known", decl);
6825 init = NULL_TREE;
6826 }
6827 else if (!DECL_EXTERNAL (decl) && DECL_SIZE (decl) == NULL_TREE)
6828 {
6829 /* An automatic variable with an incomplete type: that is an error.
6830 Don't talk about array types here, since we took care of that
6831 message in grokdeclarator. */
6832 cp_error ("storage size of `%D' isn't known", decl);
6833 TREE_TYPE (decl) = error_mark_node;
6834 }
6835 else if (!DECL_EXTERNAL (decl) && IS_AGGR_TYPE (ttype))
6836 /* Let debugger know it should output info for this type. */
6837 note_debug_info_needed (ttype);
6838
6eb3bb27 6839 if (TREE_STATIC (decl) && DECL_CLASS_SCOPE_P (decl))
d2e5ee5c
MS
6840 note_debug_info_needed (DECL_CONTEXT (decl));
6841
8d08fdba
MS
6842 if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl))
6843 && DECL_SIZE (decl) != NULL_TREE
6844 && ! TREE_CONSTANT (DECL_SIZE (decl)))
6845 {
6846 if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
6847 constant_expression_warning (DECL_SIZE (decl));
6848 else
6849 cp_error ("storage size of `%D' isn't constant", decl);
6850 }
6851
c91a56d2
MS
6852 if (! DECL_EXTERNAL (decl) && TYPE_NEEDS_DESTRUCTOR (type)
6853 /* Cleanups for static variables are handled by `finish_file'. */
6854 && ! TREE_STATIC (decl))
8d08fdba
MS
6855 {
6856 int yes = suspend_momentary ();
2ee887f2 6857 cleanup = maybe_build_cleanup (decl);
8d08fdba
MS
6858 resume_momentary (yes);
6859 }
6860 }
6861 /* PARM_DECLs get cleanups, too. */
6862 else if (TREE_CODE (decl) == PARM_DECL && TYPE_NEEDS_DESTRUCTOR (type))
6863 {
6864 if (temporary)
6865 end_temporary_allocation ();
6866 cleanup = maybe_build_cleanup (decl);
6867 if (temporary)
6868 resume_temporary_allocation ();
6869 }
6870
6871 /* Output the assembler code and/or RTL code for variables and functions,
6872 unless the type is an undefined structure or union.
6873 If not, it will get done when the type is completed. */
6874
5566b478
MS
6875 was_incomplete = (DECL_SIZE (decl) == NULL_TREE);
6876
8d08fdba
MS
6877 if (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == FUNCTION_DECL
6878 || TREE_CODE (decl) == RESULT_DECL)
6879 {
6880 /* ??? FIXME: What about nested classes? */
e1467ff2 6881 int toplev = toplevel_bindings_p () || pseudo_global_level_p ();
8d08fdba 6882 int was_temp
d22c8596 6883 = (TREE_STATIC (decl) && TYPE_NEEDS_DESTRUCTOR (type)
8d08fdba
MS
6884 && allocation_temporary_p ());
6885
6886 if (was_temp)
6887 end_temporary_allocation ();
6888
42976354
BK
6889 /* Extern inline function static data has external linkage. */
6890 if (TREE_CODE (decl) == VAR_DECL
6891 && TREE_STATIC (decl)
6892 && current_function_decl
6893 && DECL_CONTEXT (decl) == current_function_decl
6894 && DECL_THIS_INLINE (current_function_decl)
893de33c 6895 && TREE_PUBLIC (current_function_decl))
42976354 6896 {
818045b6
JM
6897 if (DECL_INTERFACE_KNOWN (current_function_decl))
6898 {
6899 TREE_PUBLIC (decl) = 1;
6900 DECL_EXTERNAL (decl) = DECL_EXTERNAL (current_function_decl);
6901 }
42976354
BK
6902 /* We can only do this if we can use common or weak, and we
6903 can't if it has been initialized and we don't support weak. */
818045b6
JM
6904 else if (DECL_INITIAL (decl) == NULL_TREE
6905 || DECL_INITIAL (decl) == error_mark_node)
42976354
BK
6906 {
6907 TREE_PUBLIC (decl) = 1;
6908 DECL_COMMON (decl) = 1;
6909 }
6910 else if (flag_weak)
6911 make_decl_one_only (decl);
6912
6913 if (TREE_PUBLIC (decl))
6914 DECL_ASSEMBLER_NAME (decl)
6915 = build_static_name (current_function_decl, DECL_NAME (decl));
6916 else if (! DECL_ARTIFICIAL (decl))
6917 cp_warning_at ("sorry: semantics of inline function static data `%#D' are wrong (you'll wind up with multiple copies)", decl);
6918 }
6919
77be6f82
JM
6920 else if (TREE_CODE (decl) == VAR_DECL
6921 && DECL_LANG_SPECIFIC (decl)
6922 && DECL_COMDAT (decl))
6923 {
6924 /* Dynamically initialized vars go into common. */
6925 if (DECL_INITIAL (decl) == NULL_TREE
6926 || DECL_INITIAL (decl) == error_mark_node)
6927 DECL_COMMON (decl) = 1;
6928 else if (EMPTY_CONSTRUCTOR_P (DECL_INITIAL (decl)))
6929 {
6930 DECL_COMMON (decl) = 1;
6931 DECL_INITIAL (decl) = error_mark_node;
6932 }
6933 else
6934 {
6935 /* Statically initialized vars are weak or comdat, if
6936 supported. */
6937 if (flag_weak)
6938 make_decl_one_only (decl);
6939 else
be343556 6940 {
2c73f9f5 6941 /* We can't do anything useful; leave vars for explicit
be343556
JM
6942 instantiation. */
6943 DECL_EXTERNAL (decl) = 1;
6944 DECL_NOT_REALLY_EXTERN (decl) = 0;
6945 }
77be6f82
JM
6946 }
6947 }
6948
8d08fdba
MS
6949 if (TREE_CODE (decl) == VAR_DECL && DECL_VIRTUAL_P (decl))
6950 make_decl_rtl (decl, NULL_PTR, toplev);
6951 else if (TREE_CODE (decl) == VAR_DECL
6952 && TREE_READONLY (decl)
6953 && DECL_INITIAL (decl) != NULL_TREE
6954 && DECL_INITIAL (decl) != error_mark_node
a3203465 6955 && ! EMPTY_CONSTRUCTOR_P (DECL_INITIAL (decl)))
8d08fdba
MS
6956 {
6957 DECL_INITIAL (decl) = save_expr (DECL_INITIAL (decl));
6958
6959 if (asmspec)
6960 DECL_ASSEMBLER_NAME (decl) = get_identifier (asmspec);
6961
6962 if (! toplev
6963 && TREE_STATIC (decl)
6964 && ! TREE_SIDE_EFFECTS (decl)
6965 && ! TREE_PUBLIC (decl)
6966 && ! DECL_EXTERNAL (decl)
6967 && ! TYPE_NEEDS_DESTRUCTOR (type)
6968 && DECL_MODE (decl) != BLKmode)
6969 {
6970 /* If this variable is really a constant, then fill its DECL_RTL
6971 slot with something which won't take up storage.
6972 If something later should take its address, we can always give
6973 it legitimate RTL at that time. */
6974 DECL_RTL (decl) = gen_reg_rtx (DECL_MODE (decl));
6975 store_expr (DECL_INITIAL (decl), DECL_RTL (decl), 0);
6976 TREE_ASM_WRITTEN (decl) = 1;
6977 }
a0a33927 6978 else if (toplev && ! TREE_PUBLIC (decl))
8d08fdba 6979 {
8d08fdba 6980 /* If this is a static const, change its apparent linkage
db5ae43f 6981 if it belongs to a #pragma interface. */
a0a33927 6982 if (!interface_unknown)
8d08fdba
MS
6983 {
6984 TREE_PUBLIC (decl) = 1;
6985 DECL_EXTERNAL (decl) = interface_only;
6986 }
6987 make_decl_rtl (decl, asmspec, toplev);
6988 }
6989 else
5566b478 6990 rest_of_decl_compilation (decl, asmspec, toplev, at_eof);
8d08fdba
MS
6991 }
6992 else if (TREE_CODE (decl) == VAR_DECL
6993 && DECL_LANG_SPECIFIC (decl)
6994 && DECL_IN_AGGR_P (decl))
6995 {
6996 if (TREE_STATIC (decl))
6997 {
6998 if (init == NULL_TREE
6999#ifdef DEFAULT_STATIC_DEFS
7000 /* If this code is dead, then users must
7001 explicitly declare static member variables
7002 outside the class def'n as well. */
7003 && TYPE_NEEDS_CONSTRUCTING (type)
7004#endif
7005 )
7006 {
7007 DECL_EXTERNAL (decl) = 1;
7008 make_decl_rtl (decl, asmspec, 1);
7009 }
7010 else
5566b478 7011 rest_of_decl_compilation (decl, asmspec, toplev, at_eof);
8d08fdba
MS
7012 }
7013 else
7014 /* Just a constant field. Should not need any rtl. */
7015 goto finish_end0;
7016 }
7017 else
5566b478 7018 rest_of_decl_compilation (decl, asmspec, toplev, at_eof);
8d08fdba
MS
7019
7020 if (was_temp)
7021 resume_temporary_allocation ();
7022
7023 if (type != error_mark_node
7024 && TYPE_LANG_SPECIFIC (type)
7025 && CLASSTYPE_ABSTRACT_VIRTUALS (type))
7026 abstract_virtuals_error (decl, type);
7027 else if ((TREE_CODE (type) == FUNCTION_TYPE
7028 || TREE_CODE (type) == METHOD_TYPE)
7029 && TYPE_LANG_SPECIFIC (TREE_TYPE (type))
7030 && CLASSTYPE_ABSTRACT_VIRTUALS (TREE_TYPE (type)))
7031 abstract_virtuals_error (decl, TREE_TYPE (type));
7032
7033 if (TYPE_LANG_SPECIFIC (type) && IS_SIGNATURE (type))
7034 signature_error (decl, type);
7035 else if ((TREE_CODE (type) == FUNCTION_TYPE
7036 || TREE_CODE (type) == METHOD_TYPE)
7037 && TYPE_LANG_SPECIFIC (TREE_TYPE (type))
7038 && IS_SIGNATURE (TREE_TYPE (type)))
7039 signature_error (decl, TREE_TYPE (type));
7040
7041 if (TREE_CODE (decl) == FUNCTION_DECL)
faae18ab 7042 ;
67d743fe
MS
7043 else if (DECL_EXTERNAL (decl)
7044 && ! (DECL_LANG_SPECIFIC (decl)
7045 && DECL_NOT_REALLY_EXTERN (decl)))
5566b478
MS
7046 {
7047 if (init)
7048 DECL_INITIAL (decl) = init;
7049 }
8d08fdba
MS
7050 else if (TREE_STATIC (decl) && type != error_mark_node)
7051 {
7052 /* Cleanups for static variables are handled by `finish_file'. */
f30432d7
MS
7053 if (TYPE_NEEDS_CONSTRUCTING (type) || init != NULL_TREE
7054 || TYPE_NEEDS_DESTRUCTOR (type))
8d08fdba 7055 expand_static_init (decl, init);
8d08fdba
MS
7056 }
7057 else if (! toplev)
7058 {
7059 /* This is a declared decl which must live until the
7060 end of the binding contour. It may need a cleanup. */
7061
7062 /* Recompute the RTL of a local array now
7063 if it used to be an incomplete type. */
7064 if (was_incomplete && ! TREE_STATIC (decl))
7065 {
7066 /* If we used it already as memory, it must stay in memory. */
7067 TREE_ADDRESSABLE (decl) = TREE_USED (decl);
7068 /* If it's still incomplete now, no init will save it. */
7069 if (DECL_SIZE (decl) == NULL_TREE)
7070 DECL_INITIAL (decl) = NULL_TREE;
7071 expand_decl (decl);
7072 }
7073 else if (! TREE_ASM_WRITTEN (decl)
7074 && (TYPE_SIZE (type) != NULL_TREE
7075 || TREE_CODE (type) == ARRAY_TYPE))
7076 {
7077 /* Do this here, because we did not expand this decl's
7078 rtl in start_decl. */
7079 if (DECL_RTL (decl) == NULL_RTX)
7080 expand_decl (decl);
7081 else if (cleanup)
7082 {
8d2733ca
MS
7083 /* XXX: Why don't we use decl here? */
7084 /* Ans: Because it was already expanded? */
e349ee73 7085 if (! expand_decl_cleanup (NULL_TREE, cleanup))
8d2733ca
MS
7086 cp_error ("parser lost in parsing declaration of `%D'",
7087 decl);
8d08fdba
MS
7088 /* Cleanup used up here. */
7089 cleanup = NULL_TREE;
7090 }
7091 }
7092
2ee887f2
MS
7093 if (current_binding_level->is_for_scope)
7094 {
7095 struct binding_level *outer = current_binding_level->level_chain;
7096
7097 /* Check to see if the same name is already bound at
7098 the outer level, either because it was directly declared,
7099 or because a dead for-decl got preserved. In either case,
d22c8596 7100 the code would not have been valid under the ARM
2ee887f2
MS
7101 scope rules, so clear is_for_scope for the
7102 current_binding_level.
7103
7104 Otherwise, we need to preserve the temp slot for decl
e92cc029 7105 to last into the outer binding level. */
2ee887f2
MS
7106
7107 int handling_dead_for_vars = 0;
7108 tree link = outer->names;
7109 for (; ; link = TREE_CHAIN (link))
7110 {
7111 if (link == NULL && handling_dead_for_vars == 0)
7112 {
7113 link = outer->dead_vars_from_for;
7114 handling_dead_for_vars = 1;
7115 }
7116 if (link == NULL)
7117 {
cffa8729 7118 if (DECL_IN_MEMORY_P (decl))
2ee887f2
MS
7119 preserve_temp_slots (DECL_RTL (decl));
7120 break;
7121 }
7122 if (DECL_NAME (link) == DECL_NAME (decl))
7123 {
7124 if (handling_dead_for_vars)
7125 {
7126 tree shadowing
7127 = purpose_member (DECL_NAME (decl),
7128 current_binding_level->shadowed);
7129 if (shadowing && TREE_VALUE (shadowing) == link)
7130 TREE_VALUE (shadowing)
7131 = DECL_SHADOWED_FOR_VAR (link);
7132 }
7133 current_binding_level->is_for_scope = 0;
7134 break;
7135 }
7136 }
7137 }
7138
eb66be0e 7139 expand_start_target_temps ();
72b7eeff 7140
8d08fdba
MS
7141 if (DECL_SIZE (decl) && type != error_mark_node)
7142 {
7143 /* Compute and store the initial value. */
7144 expand_decl_init (decl);
0c4b14c4 7145 already_used = TREE_USED (decl) || TREE_USED (type);
8d08fdba
MS
7146
7147 if (init || TYPE_NEEDS_CONSTRUCTING (type))
7148 {
a28e3c7f
MS
7149 emit_line_note (DECL_SOURCE_FILE (decl),
7150 DECL_SOURCE_LINE (decl));
6060a796 7151 expand_aggr_init (decl, init, 0, flags);
8d08fdba
MS
7152 }
7153
00595019
MS
7154 /* Set this to 0 so we can tell whether an aggregate which
7155 was initialized was ever used. Don't do this if it has a
7156 destructor, so we don't complain about the 'resource
7157 allocation is initialization' idiom. */
249555b0 7158 /* Now set attribute((unused)) on types so decls of
38e01259 7159 that type will be marked used. (see TREE_USED, above.)
249555b0
BK
7160 This avoids the warning problems this particular code
7161 tried to work around. */
0c4b14c4 7162
be99da77 7163 if (TYPE_NEEDS_CONSTRUCTING (type)
0c4b14c4 7164 && ! already_used
be99da77
MS
7165 && cleanup == NULL_TREE
7166 && DECL_NAME (decl))
8d08fdba 7167 TREE_USED (decl) = 0;
0c4b14c4
JM
7168
7169 if (already_used)
7170 TREE_USED (decl) = 1;
934c6b13 7171 }
eb66be0e 7172
934c6b13 7173 /* Cleanup any temporaries needed for the initial value. */
eb66be0e 7174 expand_end_target_temps ();
8d08fdba 7175
934c6b13
MS
7176 if (DECL_SIZE (decl) && type != error_mark_node)
7177 {
8d08fdba
MS
7178 /* Store the cleanup, if there was one. */
7179 if (cleanup)
7180 {
e349ee73 7181 if (! expand_decl_cleanup (decl, cleanup))
a28e3c7f
MS
7182 cp_error ("parser lost in parsing declaration of `%D'",
7183 decl);
8d08fdba
MS
7184 }
7185 }
7186 }
7187 finish_end0:
7188
7189 /* Undo call to `pushclass' that was done in `start_decl'
7190 due to initialization of qualified member variable.
7191 I.e., Foo::x = 10; */
7192 {
f30432d7 7193 tree context = DECL_REAL_CONTEXT (decl);
8d08fdba
MS
7194 if (context
7195 && TREE_CODE_CLASS (TREE_CODE (context)) == 't'
7196 && (TREE_CODE (decl) == VAR_DECL
7197 /* We also have a pushclass done that we need to undo here
7198 if we're at top level and declare a method. */
5566b478
MS
7199 || TREE_CODE (decl) == FUNCTION_DECL)
7200 /* If size hasn't been set, we're still defining it,
7201 and therefore inside the class body; don't pop
7202 the binding level.. */
7203 && TYPE_SIZE (context) != NULL_TREE
7204 && context == current_class_type)
8d08fdba
MS
7205 popclass (1);
7206 }
7207 }
7208
7209 finish_end:
7210
39211cd5
MS
7211 /* If requested, warn about definitions of large data objects. */
7212
7213 if (warn_larger_than
5156628f 7214 && ! processing_template_decl
39211cd5
MS
7215 && (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL)
7216 && !DECL_EXTERNAL (decl))
7217 {
7218 register tree decl_size = DECL_SIZE (decl);
7219
7220 if (decl_size && TREE_CODE (decl_size) == INTEGER_CST)
7221 {
7222 unsigned units = TREE_INT_CST_LOW (decl_size) / BITS_PER_UNIT;
7223
7224 if (units > larger_than_size)
7225 warning_with_decl (decl, "size of `%s' is %u bytes", units);
7226 }
7227 }
7228
8d08fdba
MS
7229 if (need_pop)
7230 {
7231 /* Resume permanent allocation, if not within a function. */
7232 /* The corresponding push_obstacks_nochange is in start_decl,
7233 start_method, groktypename, and in grokfield. */
7234 pop_obstacks ();
7235 }
7236
7237 if (was_readonly)
7238 TREE_READONLY (decl) = 1;
8d08fdba
MS
7239}
7240
82580166 7241/* This is here for a midend callback from c-common.c */
e92cc029 7242
82580166
MS
7243void
7244finish_decl (decl, init, asmspec_tree)
7245 tree decl, init;
7246 tree asmspec_tree;
7247{
7248 cp_finish_decl (decl, init, asmspec_tree, 1, 0);
7249}
7250
8d08fdba
MS
7251void
7252expand_static_init (decl, init)
7253 tree decl;
7254 tree init;
7255{
7256 tree oldstatic = value_member (decl, static_aggregates);
a4443a08 7257
8d08fdba
MS
7258 if (oldstatic)
7259 {
7260 if (TREE_PURPOSE (oldstatic) && init != NULL_TREE)
7261 cp_error ("multiple initializations given for `%D'", decl);
7262 }
a9aedbc2 7263 else if (! toplevel_bindings_p () && ! pseudo_global_level_p ())
8d08fdba
MS
7264 {
7265 /* Emit code to perform this initialization but once. */
7266 tree temp;
7267
e92cc029 7268 /* Remember this information until end of file. */
8d08fdba
MS
7269 push_obstacks (&permanent_obstack, &permanent_obstack);
7270
7271 /* Emit code to perform this initialization but once. */
7272 temp = get_temp_name (integer_type_node, 1);
7273 rest_of_decl_compilation (temp, NULL_PTR, 0, 0);
7274 expand_start_cond (build_binary_op (EQ_EXPR, temp,
7275 integer_zero_node, 1), 0);
eb66be0e 7276 expand_start_target_temps ();
72b7eeff 7277
8d08fdba 7278 expand_assignment (temp, integer_one_node, 0, 0);
28cbf42c 7279 if (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl))
f30432d7 7280 || (init && TREE_CODE (init) == TREE_LIST))
8d08fdba 7281 {
6060a796 7282 expand_aggr_init (decl, init, 0, 0);
8d08fdba
MS
7283 do_pending_stack_adjust ();
7284 }
f30432d7 7285 else if (init)
8d08fdba 7286 expand_assignment (decl, init, 0, 0);
f30432d7 7287
a4443a08 7288 /* Cleanup any temporaries needed for the initial value. */
eb66be0e 7289 expand_end_target_temps ();
72b7eeff
MS
7290
7291 if (TYPE_NEEDS_DESTRUCTOR (TREE_TYPE (decl)))
7292 {
7293 tree cleanup, fcall;
7294 static tree Atexit = 0;
7295 if (Atexit == 0)
7296 {
7297 tree atexit_fndecl, PFV, pfvlist;
e92cc029 7298 /* Remember this information until end of file. */
72b7eeff
MS
7299 push_obstacks (&permanent_obstack, &permanent_obstack);
7300 PFV = build_pointer_type (build_function_type
7301 (void_type_node, void_list_node));
7302
7303 pfvlist = tree_cons (NULL_TREE, PFV, void_list_node);
7304
7305 push_lang_context (lang_name_c);
beb53fb8
JM
7306 atexit_fndecl
7307 = builtin_function ("atexit",
7308 build_function_type (void_type_node,
7309 pfvlist),
7310 NOT_BUILT_IN, NULL_PTR);
be99da77 7311 assemble_external (atexit_fndecl);
72b7eeff
MS
7312 Atexit = default_conversion (atexit_fndecl);
7313 pop_lang_context ();
7314 pop_obstacks ();
7315 }
7316
7317 cleanup = start_anon_func ();
7318 expand_expr_stmt (build_cleanup (decl));
7319 end_anon_func ();
7320 mark_addressable (cleanup);
7321 cleanup = build_unary_op (ADDR_EXPR, cleanup, 0);
e66d884e 7322 fcall = build_function_call (Atexit, expr_tree_cons (NULL_TREE, cleanup, NULL_TREE));
72b7eeff
MS
7323 expand_expr_stmt (fcall);
7324 }
7325
8d08fdba
MS
7326 expand_end_cond ();
7327 if (TYPE_NEEDS_DESTRUCTOR (TREE_TYPE (decl)))
7328 {
7329 static_aggregates = perm_tree_cons (temp, decl, static_aggregates);
7330 TREE_STATIC (static_aggregates) = 1;
7331 }
7332
e92cc029 7333 /* Resume old (possibly temporary) allocation. */
8d08fdba
MS
7334 pop_obstacks ();
7335 }
7336 else
7337 {
7338 /* This code takes into account memory allocation
7339 policy of `start_decl'. Namely, if TYPE_NEEDS_CONSTRUCTING
7340 does not hold for this object, then we must make permanent
7341 the storage currently in the temporary obstack. */
7342 if (! TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl)))
7343 preserve_initializer ();
7344 static_aggregates = perm_tree_cons (init, decl, static_aggregates);
7345 }
7346}
7347\f
7348/* Make TYPE a complete type based on INITIAL_VALUE.
7349 Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
7350 2 if there was no information (in which case assume 1 if DO_DEFAULT). */
7351
7352int
7353complete_array_type (type, initial_value, do_default)
7354 tree type, initial_value;
7355 int do_default;
7356{
7357 register tree maxindex = NULL_TREE;
7358 int value = 0;
7359
7360 if (initial_value)
7361 {
7362 /* Note MAXINDEX is really the maximum index,
7363 one less than the size. */
7364 if (TREE_CODE (initial_value) == STRING_CST)
e1cd6e56
MS
7365 {
7366 int eltsize
7367 = int_size_in_bytes (TREE_TYPE (TREE_TYPE (initial_value)));
7368 maxindex = build_int_2 ((TREE_STRING_LENGTH (initial_value)
7369 / eltsize) - 1, 0);
7370 }
8d08fdba
MS
7371 else if (TREE_CODE (initial_value) == CONSTRUCTOR)
7372 {
e1cd6e56
MS
7373 tree elts = CONSTRUCTOR_ELTS (initial_value);
7374 maxindex = size_binop (MINUS_EXPR, integer_zero_node, size_one_node);
7375 for (; elts; elts = TREE_CHAIN (elts))
7376 {
7377 if (TREE_PURPOSE (elts))
7378 maxindex = TREE_PURPOSE (elts);
7379 else
7380 maxindex = size_binop (PLUS_EXPR, maxindex, size_one_node);
7381 }
7382 maxindex = copy_node (maxindex);
8d08fdba
MS
7383 }
7384 else
7385 {
7386 /* Make an error message unless that happened already. */
7387 if (initial_value != error_mark_node)
7388 value = 1;
7389
7390 /* Prevent further error messages. */
7391 maxindex = build_int_2 (0, 0);
7392 }
7393 }
7394
7395 if (!maxindex)
7396 {
7397 if (do_default)
7398 maxindex = build_int_2 (0, 0);
7399 value = 2;
7400 }
7401
7402 if (maxindex)
7403 {
51c184be
MS
7404 tree itype;
7405
8d08fdba 7406 TYPE_DOMAIN (type) = build_index_type (maxindex);
dff6b454 7407 if (! TREE_TYPE (maxindex))
8d08fdba 7408 TREE_TYPE (maxindex) = TYPE_DOMAIN (type);
51c184be
MS
7409 if (initial_value)
7410 itype = TREE_TYPE (initial_value);
7411 else
7412 itype = NULL;
7413 if (itype && !TYPE_DOMAIN (itype))
7414 TYPE_DOMAIN (itype) = TYPE_DOMAIN (type);
dff6b454
RK
7415 /* The type of the main variant should never be used for arrays
7416 of different sizes. It should only ever be completed with the
7417 size of the array. */
7418 if (! TYPE_DOMAIN (TYPE_MAIN_VARIANT (type)))
7419 TYPE_DOMAIN (TYPE_MAIN_VARIANT (type)) = TYPE_DOMAIN (type);
8d08fdba
MS
7420 }
7421
7422 /* Lay out the type now that we can get the real answer. */
7423
7424 layout_type (type);
7425
7426 return value;
7427}
7428\f
7429/* Return zero if something is declared to be a member of type
7430 CTYPE when in the context of CUR_TYPE. STRING is the error
7431 message to print in that case. Otherwise, quietly return 1. */
e92cc029 7432
8d08fdba
MS
7433static int
7434member_function_or_else (ctype, cur_type, string)
7435 tree ctype, cur_type;
7436 char *string;
7437{
7438 if (ctype && ctype != cur_type)
7439 {
7440 error (string, TYPE_NAME_STRING (ctype));
7441 return 0;
7442 }
7443 return 1;
7444}
7445\f
7446/* Subroutine of `grokdeclarator'. */
7447
7448/* Generate errors possibly applicable for a given set of specifiers.
7449 This is for ARM $7.1.2. */
e92cc029 7450
8d08fdba
MS
7451static void
7452bad_specifiers (object, type, virtualp, quals, inlinep, friendp, raises)
7453 tree object;
7454 char *type;
7455 int virtualp, quals, friendp, raises, inlinep;
7456{
7457 if (virtualp)
7458 cp_error ("`%D' declared as a `virtual' %s", object, type);
7459 if (inlinep)
7460 cp_error ("`%D' declared as an `inline' %s", object, type);
7461 if (quals)
a28e3c7f
MS
7462 cp_error ("`const' and `volatile' function specifiers on `%D' invalid in %s declaration",
7463 object, type);
8d08fdba
MS
7464 if (friendp)
7465 cp_error_at ("invalid friend declaration", object);
7466 if (raises)
6060a796 7467 cp_error_at ("invalid exception specifications", object);
8d08fdba
MS
7468}
7469
7470/* CTYPE is class type, or null if non-class.
7471 TYPE is type this FUNCTION_DECL should have, either FUNCTION_TYPE
7472 or METHOD_TYPE.
7473 DECLARATOR is the function's name.
7474 VIRTUALP is truthvalue of whether the function is virtual or not.
7475 FLAGS are to be passed through to `grokclassfn'.
7476 QUALS are qualifiers indicating whether the function is `const'
7477 or `volatile'.
7478 RAISES is a list of exceptions that this function can raise.
7479 CHECK is 1 if we must find this method in CTYPE, 0 if we should
7480 not look, and -1 if we should not call `grokclassfn' at all. */
e92cc029 7481
8d08fdba 7482static tree
386b8a85
JM
7483grokfndecl (ctype, type, declarator, orig_declarator, virtualp, flags, quals,
7484 raises, attrlist, check, friendp, publicp, inlinep, funcdef_flag,
2c73f9f5 7485 template_count, in_namespace)
8d08fdba
MS
7486 tree ctype, type;
7487 tree declarator;
386b8a85 7488 tree orig_declarator;
8d08fdba
MS
7489 int virtualp;
7490 enum overload_flags flags;
f30432d7 7491 tree quals, raises, attrlist;
386b8a85 7492 int check, friendp, publicp, inlinep, funcdef_flag, template_count;
2c73f9f5 7493 tree in_namespace;
8d08fdba
MS
7494{
7495 tree cname, decl;
7496 int staticp = ctype && TREE_CODE (type) == FUNCTION_TYPE;
42976354 7497 tree t;
8d08fdba
MS
7498
7499 if (ctype)
7500 cname = TREE_CODE (TYPE_NAME (ctype)) == TYPE_DECL
7501 ? TYPE_IDENTIFIER (ctype) : TYPE_NAME (ctype);
7502 else
7503 cname = NULL_TREE;
7504
7505 if (raises)
7506 {
f30432d7 7507 type = build_exception_variant (type, raises);
8d08fdba 7508 }
c11b6f21 7509
8d08fdba 7510 decl = build_lang_decl (FUNCTION_DECL, declarator, type);
2c73f9f5 7511 /* Propagate volatile out from type to decl. */
8d08fdba 7512 if (TYPE_VOLATILE (type))
893de33c 7513 TREE_THIS_VOLATILE (decl) = 1;
8d08fdba 7514
2c73f9f5
ML
7515 /* This decl is not from the current namespace. */
7516 if (in_namespace)
7517 set_decl_namespace (decl, in_namespace);
7518
8d08fdba
MS
7519 /* Should probably propagate const out from type to decl I bet (mrs). */
7520 if (staticp)
7521 {
7522 DECL_STATIC_FUNCTION_P (decl) = 1;
7523 DECL_CONTEXT (decl) = ctype;
8d08fdba
MS
7524 }
7525
e76a2646
MS
7526 if (ctype)
7527 DECL_CLASS_CONTEXT (decl) = ctype;
7528
faae18ab
MS
7529 if (ctype == NULL_TREE && ! strcmp (IDENTIFIER_POINTER (declarator), "main"))
7530 {
7531 if (inlinep)
7532 error ("cannot declare `main' to be inline");
7533 else if (! publicp)
7534 error ("cannot declare `main' to be static");
7535 inlinep = 0;
7536 publicp = 1;
7537 }
7538
893de33c 7539 TREE_PUBLIC (decl) = publicp;
faae18ab 7540 if (! publicp)
893de33c
JM
7541 {
7542 DECL_INTERFACE_KNOWN (decl) = 1;
7543 DECL_NOT_REALLY_EXTERN (decl) = 1;
7544 }
faae18ab
MS
7545
7546 if (inlinep)
7547 DECL_THIS_INLINE (decl) = DECL_INLINE (decl) = 1;
8d08fdba
MS
7548
7549 DECL_EXTERNAL (decl) = 1;
7550 if (quals != NULL_TREE && TREE_CODE (type) == FUNCTION_TYPE)
7551 {
7552 cp_error ("%smember function `%D' cannot have `%T' method qualifier",
7553 (ctype ? "static " : "non-"), decl, TREE_VALUE (quals));
7554 quals = NULL_TREE;
7555 }
7556
7557 if (IDENTIFIER_OPNAME_P (DECL_NAME (decl)))
7558 grok_op_properties (decl, virtualp, check < 0);
7559
e76a2646 7560 if (ctype && hack_decl_function_context (decl))
893de33c 7561 DECL_NO_STATIC_CHAIN (decl) = 1;
e76a2646 7562
42976354
BK
7563 for (t = TYPE_ARG_TYPES (TREE_TYPE (decl)); t; t = TREE_CHAIN (t))
7564 if (TREE_PURPOSE (t)
7565 && TREE_CODE (TREE_PURPOSE (t)) == DEFAULT_ARG)
7566 {
7567 add_defarg_fn (decl);
7568 break;
7569 }
7570
75650646
MM
7571 if (friendp &&
7572 TREE_CODE (orig_declarator) == TEMPLATE_ID_EXPR)
f84b4be9
JM
7573 {
7574 /* A friend declaration of the form friend void f<>(). Record
7575 the information in the TEMPLATE_ID_EXPR. */
7576 SET_DECL_IMPLICIT_INSTANTIATION (decl);
7577 DECL_TEMPLATE_INFO (decl)
7578 = perm_tree_cons (TREE_OPERAND (orig_declarator, 0),
7579 TREE_OPERAND (orig_declarator, 1),
7580 NULL_TREE);
7581 }
386b8a85 7582
75650646 7583 /* Caller will do the rest of this. */
8d08fdba
MS
7584 if (check < 0)
7585 return decl;
7586
5566b478 7587 if (check && funcdef_flag)
d2e5ee5c 7588 DECL_INITIAL (decl) = error_mark_node;
5566b478 7589
8d08fdba
MS
7590 if (flags == NO_SPECIAL && ctype && constructor_name (cname) == declarator)
7591 {
7592 tree tmp;
7593 /* Just handle constructors here. We could do this
7594 inside the following if stmt, but I think
7595 that the code is more legible by breaking this
7596 case out. See comments below for what each of
7597 the following calls is supposed to do. */
7598 DECL_CONSTRUCTOR_P (decl) = 1;
7599
7600 grokclassfn (ctype, declarator, decl, flags, quals);
386b8a85 7601
e1467ff2
MM
7602 decl = check_explicit_specialization (orig_declarator, decl,
7603 template_count,
f84b4be9
JM
7604 2 * (funcdef_flag != 0) +
7605 4 * (friendp != 0));
75650646 7606
8d08fdba 7607 if (check)
5566b478
MS
7608 {
7609 tmp = check_classfn (ctype, decl);
98c1c668
JM
7610
7611 if (tmp && TREE_CODE (tmp) == TEMPLATE_DECL)
7612 tmp = DECL_TEMPLATE_RESULT(tmp);
7613
e349ee73
MS
7614 if (tmp && DECL_ARTIFICIAL (tmp))
7615 cp_error ("definition of implicitly-declared `%D'", tmp);
5566b478
MS
7616 if (tmp && duplicate_decls (decl, tmp))
7617 return tmp;
7618 }
a0a33927
MS
7619 if (! grok_ctor_properties (ctype, decl))
7620 return NULL_TREE;
7621
7177d104 7622 if (check == 0 && ! current_function_decl)
8d08fdba 7623 {
2c73f9f5 7624 /* Assembler names live in the global namespace. */
37c46b43 7625 tmp = IDENTIFIER_GLOBAL_VALUE (DECL_ASSEMBLER_NAME (decl));
8d08fdba 7626 if (tmp == NULL_TREE)
2c73f9f5 7627 SET_IDENTIFIER_GLOBAL_VALUE (DECL_ASSEMBLER_NAME (decl), decl);
8d08fdba
MS
7628 else if (TREE_CODE (tmp) != TREE_CODE (decl))
7629 cp_error ("inconsistent declarations for `%D'", decl);
7630 else
7631 {
7632 duplicate_decls (decl, tmp);
7633 decl = tmp;
8d08fdba
MS
7634 }
7635 make_decl_rtl (decl, NULL_PTR, 1);
7636 }
7637 }
7638 else
7639 {
7640 tree tmp;
7641
7642 /* Function gets the ugly name, field gets the nice one.
7643 This call may change the type of the function (because
7644 of default parameters)! */
7645 if (ctype != NULL_TREE)
7646 grokclassfn (ctype, cname, decl, flags, quals);
7647
e1467ff2
MM
7648 decl = check_explicit_specialization (orig_declarator, decl,
7649 template_count,
f84b4be9
JM
7650 2 * (funcdef_flag != 0) +
7651 4 * (friendp != 0));
75650646 7652
8d08fdba 7653 if (ctype != NULL_TREE && check)
5566b478
MS
7654 {
7655 tmp = check_classfn (ctype, decl);
98c1c668
JM
7656
7657 if (tmp && TREE_CODE (tmp) == TEMPLATE_DECL)
75650646 7658 tmp = DECL_TEMPLATE_RESULT (tmp);
98c1c668 7659
5566b478
MS
7660 if (tmp && DECL_STATIC_FUNCTION_P (tmp)
7661 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE)
7662 {
7663 /* Remove the `this' parm added by grokclassfn.
7664 XXX Isn't this done in start_function, too? */
7665 revert_static_member_fn (&decl, NULL, NULL);
7666 last_function_parms = TREE_CHAIN (last_function_parms);
7667 }
e349ee73
MS
7668 if (tmp && DECL_ARTIFICIAL (tmp))
7669 cp_error ("definition of implicitly-declared `%D'", tmp);
7834ab39
MS
7670 if (tmp)
7671 {
7672 if (!duplicate_decls (decl, tmp))
7673 my_friendly_abort (892);
7674 return tmp;
7675 }
5566b478 7676 }
8d08fdba
MS
7677
7678 if (ctype == NULL_TREE || check)
7679 return decl;
7680
7177d104
MS
7681 /* Now install the declaration of this function so that others may
7682 find it (esp. its DECL_FRIENDLIST). Don't do this for local class
7683 methods, though. */
7684 if (! current_function_decl)
8d08fdba 7685 {
75650646 7686 if (!DECL_TEMPLATE_SPECIALIZATION (decl))
7177d104 7687 {
75650646
MM
7688 /* We don't do this for specializations since the
7689 equivalent checks will be done later. Also, at this
7690 point the DECL_ASSEMBLER_NAME is not yet fully
7691 accurate. */
7692
7693 /* FIXME: this should only need to look at
7694 IDENTIFIER_GLOBAL_VALUE. */
7695 tmp = lookup_name (DECL_ASSEMBLER_NAME (decl), 0);
7696 if (tmp == NULL_TREE)
2c73f9f5 7697 SET_IDENTIFIER_GLOBAL_VALUE (DECL_ASSEMBLER_NAME (decl), decl);
75650646
MM
7698 else if (TREE_CODE (tmp) != TREE_CODE (decl))
7699 cp_error ("inconsistent declarations for `%D'", decl);
7700 else
7701 {
7702 duplicate_decls (decl, tmp);
7703 decl = tmp;
75650646 7704 }
7177d104 7705 }
f30432d7
MS
7706
7707 if (attrlist)
7708 cplus_decl_attributes (decl, TREE_PURPOSE (attrlist),
7709 TREE_VALUE (attrlist));
7177d104 7710 make_decl_rtl (decl, NULL_PTR, 1);
8d08fdba 7711 }
8d08fdba
MS
7712 if (virtualp)
7713 {
2ee887f2 7714 DECL_VIRTUAL_P (decl) = 1;
8d08fdba
MS
7715 if (DECL_VINDEX (decl) == NULL_TREE)
7716 DECL_VINDEX (decl) = error_mark_node;
7717 IDENTIFIER_VIRTUAL_P (DECL_NAME (decl)) = 1;
8d08fdba
MS
7718 }
7719 }
7720 return decl;
7721}
7722
7723static tree
2c73f9f5 7724grokvardecl (type, declarator, specbits_in, initialized, constp, in_namespace)
8d08fdba
MS
7725 tree type;
7726 tree declarator;
d2e5ee5c 7727 RID_BIT_TYPE *specbits_in;
8d08fdba 7728 int initialized;
a9aedbc2 7729 int constp;
2c73f9f5 7730 tree in_namespace;
8d08fdba
MS
7731{
7732 tree decl;
f7da6097
MS
7733 RID_BIT_TYPE specbits;
7734
7735 specbits = *specbits_in;
8d08fdba
MS
7736
7737 if (TREE_CODE (type) == OFFSET_TYPE)
7738 {
7739 /* If you declare a static member so that it
7740 can be initialized, the code will reach here. */
5b605f68
MS
7741 tree basetype = TYPE_OFFSET_BASETYPE (type);
7742 type = TREE_TYPE (type);
7743 decl = build_lang_field_decl (VAR_DECL, declarator, type);
7744 DECL_CONTEXT (decl) = basetype;
7745 DECL_CLASS_CONTEXT (decl) = basetype;
f376e137 7746 DECL_ASSEMBLER_NAME (decl) = build_static_name (basetype, declarator);
8d08fdba
MS
7747 }
7748 else
30394414 7749 {
2c73f9f5 7750 tree context = in_namespace ? in_namespace : current_namespace;
30394414 7751 decl = build_decl (VAR_DECL, declarator, complete_type (type));
2c73f9f5
ML
7752 if (context != global_namespace)
7753 DECL_ASSEMBLER_NAME (decl) = build_static_name (context,
30394414
JM
7754 declarator);
7755 }
6060a796 7756
2c73f9f5
ML
7757 if (in_namespace)
7758 set_decl_namespace (decl, in_namespace);
7759
8d08fdba
MS
7760 if (RIDBIT_SETP (RID_EXTERN, specbits))
7761 {
7762 DECL_THIS_EXTERN (decl) = 1;
7763 DECL_EXTERNAL (decl) = !initialized;
7764 }
7765
7766 /* In class context, static means one per class,
7767 public access, and static storage. */
7768 if (DECL_FIELD_CONTEXT (decl) != NULL_TREE
7769 && IS_AGGR_TYPE (DECL_FIELD_CONTEXT (decl)))
7770 {
7771 TREE_PUBLIC (decl) = 1;
7772 TREE_STATIC (decl) = 1;
5b605f68 7773 DECL_EXTERNAL (decl) = 0;
8d08fdba
MS
7774 }
7775 /* At top level, either `static' or no s.c. makes a definition
7776 (perhaps tentative), and absence of `static' makes it public. */
a9aedbc2 7777 else if (toplevel_bindings_p ())
8d08fdba 7778 {
a9aedbc2 7779 TREE_PUBLIC (decl) = (RIDBIT_NOTSETP (RID_STATIC, specbits)
9fffd093 7780 && (DECL_THIS_EXTERN (decl) || ! constp));
8d08fdba
MS
7781 TREE_STATIC (decl) = ! DECL_EXTERNAL (decl);
7782 }
7783 /* Not at top level, only `static' makes a static definition. */
7784 else
7785 {
7786 TREE_STATIC (decl) = !! RIDBIT_SETP (RID_STATIC, specbits);
7787 TREE_PUBLIC (decl) = DECL_EXTERNAL (decl);
7788 }
7789 return decl;
7790}
7791
e92cc029 7792/* Create a canonical pointer to member function type. */
8d08fdba
MS
7793
7794tree
7795build_ptrmemfunc_type (type)
7796 tree type;
7797{
7798 tree fields[4];
7799 tree t;
7800 tree u;
7801
7802 /* If a canonical type already exists for this type, use it. We use
7803 this method instead of type_hash_canon, because it only does a
7804 simple equality check on the list of field members. */
7805
7806 if ((t = TYPE_GET_PTRMEMFUNC_TYPE (type)))
7807 return t;
7808
7809 push_obstacks (TYPE_OBSTACK (type), TYPE_OBSTACK (type));
7810
7811 u = make_lang_type (UNION_TYPE);
f30432d7 7812 IS_AGGR_TYPE (u) = 0;
8d08fdba 7813 fields[0] = build_lang_field_decl (FIELD_DECL, pfn_identifier, type);
a28e3c7f
MS
7814 fields[1] = build_lang_field_decl (FIELD_DECL, delta2_identifier,
7815 delta_type_node);
8d08fdba
MS
7816 finish_builtin_type (u, "__ptrmemfunc_type", fields, 1, ptr_type_node);
7817 TYPE_NAME (u) = NULL_TREE;
7818
7819 t = make_lang_type (RECORD_TYPE);
7820
2c73f9f5 7821 /* Let the front-end know this is a pointer to member function... */
db5ae43f 7822 TYPE_PTRMEMFUNC_FLAG (t) = 1;
2c73f9f5 7823 /* ... and not really an aggregate. */
f376e137 7824 IS_AGGR_TYPE (t) = 0;
8d08fdba 7825
a28e3c7f
MS
7826 fields[0] = build_lang_field_decl (FIELD_DECL, delta_identifier,
7827 delta_type_node);
7828 fields[1] = build_lang_field_decl (FIELD_DECL, index_identifier,
7829 delta_type_node);
8d08fdba
MS
7830 fields[2] = build_lang_field_decl (FIELD_DECL, pfn_or_delta2_identifier, u);
7831 finish_builtin_type (t, "__ptrmemfunc_type", fields, 2, ptr_type_node);
7832
7833 pop_obstacks ();
7834
7835 /* Zap out the name so that the back-end will give us the debugging
7836 information for this anonymous RECORD_TYPE. */
7837 TYPE_NAME (t) = NULL_TREE;
7838
7839 TYPE_SET_PTRMEMFUNC_TYPE (type, t);
7840
e92cc029 7841 /* Seems to be wanted. */
8d08fdba
MS
7842 CLASSTYPE_GOT_SEMICOLON (t) = 1;
7843 return t;
7844}
7845
7846/* Given declspecs and a declarator,
7847 determine the name and type of the object declared
7848 and construct a ..._DECL node for it.
7849 (In one case we can return a ..._TYPE node instead.
7850 For invalid input we sometimes return 0.)
7851
7852 DECLSPECS is a chain of tree_list nodes whose value fields
7853 are the storage classes and type specifiers.
7854
7855 DECL_CONTEXT says which syntactic context this declaration is in:
7856 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
7857 FUNCDEF for a function definition. Like NORMAL but a few different
7858 error messages in each case. Return value may be zero meaning
7859 this definition is too screwy to try to parse.
7860 MEMFUNCDEF for a function definition. Like FUNCDEF but prepares to
7861 handle member functions (which have FIELD context).
7862 Return value may be zero meaning this definition is too screwy to
7863 try to parse.
7864 PARM for a parameter declaration (either within a function prototype
7865 or before a function body). Make a PARM_DECL, or return void_type_node.
db5ae43f 7866 CATCHPARM for a parameter declaration before a catch clause.
8d08fdba
MS
7867 TYPENAME if for a typename (in a cast or sizeof).
7868 Don't make a DECL node; just return the ..._TYPE node.
7869 FIELD for a struct or union field; make a FIELD_DECL.
7870 BITFIELD for a field with specified width.
7871 INITIALIZED is 1 if the decl has an initializer.
7872
7873 In the TYPENAME case, DECLARATOR is really an absolute declarator.
7874 It may also be so in the PARM case, for a prototype where the
7875 argument type is specified but not the name.
7876
7877 This function is where the complicated C meanings of `static'
7878 and `extern' are interpreted.
7879
7880 For C++, if there is any monkey business to do, the function which
7881 calls this one must do it, i.e., prepending instance variables,
7882 renaming overloaded function names, etc.
7883
7884 Note that for this C++, it is an error to define a method within a class
7885 which does not belong to that class.
7886
7887 Except in the case where SCOPE_REFs are implicitly known (such as
7888 methods within a class being redundantly qualified),
7889 declarations which involve SCOPE_REFs are returned as SCOPE_REFs
7890 (class_name::decl_name). The caller must also deal with this.
7891
7892 If a constructor or destructor is seen, and the context is FIELD,
7893 then the type gains the attribute TREE_HAS_x. If such a declaration
7894 is erroneous, NULL_TREE is returned.
7895
7896 QUALS is used only for FUNCDEF and MEMFUNCDEF cases. For a member
7897 function, these are the qualifiers to give to the `this' pointer.
7898
7899 May return void_type_node if the declarator turned out to be a friend.
7900 See grokfield for details. */
7901
7902enum return_types { return_normal, return_ctor, return_dtor, return_conversion };
7903
7904tree
c11b6f21 7905grokdeclarator (declarator, declspecs, decl_context, initialized, attrlist)
8d08fdba
MS
7906 tree declspecs;
7907 tree declarator;
7908 enum decl_context decl_context;
7909 int initialized;
c11b6f21 7910 tree attrlist;
8d08fdba
MS
7911{
7912 RID_BIT_TYPE specbits;
7913 int nclasses = 0;
7914 tree spec;
7915 tree type = NULL_TREE;
7916 int longlong = 0;
7917 int constp;
7918 int volatilep;
db5ae43f 7919 int virtualp, explicitp, friendp, inlinep, staticp;
8d08fdba
MS
7920 int explicit_int = 0;
7921 int explicit_char = 0;
37c46b43 7922 int defaulted_int = 0;
8d08fdba
MS
7923 int opaque_typedef = 0;
7924 tree typedef_decl = NULL_TREE;
7925 char *name;
7926 tree typedef_type = NULL_TREE;
7927 int funcdef_flag = 0;
7928 enum tree_code innermost_code = ERROR_MARK;
7929 int bitfield = 0;
6125f3be
DE
7930#if 0
7931 /* See the code below that used this. */
f6abb50a 7932 tree decl_machine_attr = NULL_TREE;
6125f3be 7933#endif
8d08fdba
MS
7934 /* Set this to error_mark_node for FIELD_DECLs we could not handle properly.
7935 All FIELD_DECLs we build here have `init' put into their DECL_INITIAL. */
7936 tree init = NULL_TREE;
7937
7938 /* Keep track of what sort of function is being processed
7939 so that we can warn about default return values, or explicit
7940 return values which do not match prescribed defaults. */
7941 enum return_types return_type = return_normal;
7942
7943 tree dname = NULL_TREE;
7944 tree ctype = current_class_type;
7945 tree ctor_return_type = NULL_TREE;
7946 enum overload_flags flags = NO_SPECIAL;
8d08fdba 7947 tree quals = NULL_TREE;
c11b6f21 7948 tree raises = NULL_TREE;
386b8a85 7949 int template_count = 0;
2c73f9f5 7950 tree in_namespace = NULL_TREE;
8d08fdba
MS
7951
7952 RIDBIT_RESET_ALL (specbits);
7953 if (decl_context == FUNCDEF)
7954 funcdef_flag = 1, decl_context = NORMAL;
7955 else if (decl_context == MEMFUNCDEF)
7956 funcdef_flag = -1, decl_context = FIELD;
7957 else if (decl_context == BITFIELD)
7958 bitfield = 1, decl_context = FIELD;
7959
8d08fdba
MS
7960 /* Look inside a declarator for the name being declared
7961 and get it as a string, for an error message. */
7962 {
be99da77
MS
7963 tree *next = &declarator;
7964 register tree decl;
8d08fdba
MS
7965 name = NULL;
7966
be99da77
MS
7967 while (next && *next)
7968 {
7969 decl = *next;
7970 switch (TREE_CODE (decl))
8d08fdba 7971 {
be99da77
MS
7972 case COND_EXPR:
7973 ctype = NULL_TREE;
7974 next = &TREE_OPERAND (decl, 0);
7975 break;
8d08fdba 7976
2c73f9f5 7977 case BIT_NOT_EXPR: /* For C++ destructors! */
8d08fdba 7978 {
be99da77
MS
7979 tree name = TREE_OPERAND (decl, 0);
7980 tree rename = NULL_TREE;
7981
7982 my_friendly_assert (flags == NO_SPECIAL, 152);
7983 flags = DTOR_FLAG;
7984 return_type = return_dtor;
5566b478
MS
7985 if (TREE_CODE (name) == TYPE_DECL)
7986 TREE_OPERAND (decl, 0) = name = constructor_name (name);
be99da77
MS
7987 my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, 153);
7988 if (ctype == NULL_TREE)
7989 {
7990 if (current_class_type == NULL_TREE)
7991 {
7992 error ("destructors must be member functions");
7993 flags = NO_SPECIAL;
7994 }
7995 else
7996 {
7997 tree t = constructor_name (current_class_name);
7998 if (t != name)
7999 rename = t;
8000 }
8001 }
8d08fdba 8002 else
be99da77
MS
8003 {
8004 tree t = constructor_name (ctype);
8005 if (t != name)
8006 rename = t;
8007 }
51c184be 8008
be99da77 8009 if (rename)
39211cd5 8010 {
5566b478
MS
8011 cp_error ("destructor `%T' must match class name `%T'",
8012 name, rename);
be99da77 8013 TREE_OPERAND (decl, 0) = rename;
39211cd5 8014 }
be99da77 8015 next = &name;
51c184be 8016 }
be99da77 8017 break;
8d08fdba 8018
be99da77 8019 case ADDR_EXPR: /* C++ reference declaration */
2c73f9f5 8020 /* Fall through. */
be99da77
MS
8021 case ARRAY_REF:
8022 case INDIRECT_REF:
8023 ctype = NULL_TREE;
8024 innermost_code = TREE_CODE (decl);
8025 next = &TREE_OPERAND (decl, 0);
8026 break;
8d08fdba 8027
be99da77
MS
8028 case CALL_EXPR:
8029 if (parmlist_is_exprlist (TREE_OPERAND (decl, 1)))
8d08fdba 8030 {
be99da77
MS
8031 /* This is actually a variable declaration using constructor
8032 syntax. We need to call start_decl and cp_finish_decl so we
8033 can get the variable initialized... */
8034
8035 *next = TREE_OPERAND (decl, 0);
8036 init = TREE_OPERAND (decl, 1);
8037
a1774733 8038 decl = start_decl (declarator, declspecs, 1, NULL_TREE, NULL_TREE);
249555b0
BK
8039 /* Look for __unused__ attribute */
8040 if (TREE_USED (TREE_TYPE (decl)))
8041 TREE_USED (decl) = 1;
be99da77
MS
8042 finish_decl (decl, init, NULL_TREE);
8043 return 0;
8d08fdba 8044 }
be99da77
MS
8045 innermost_code = TREE_CODE (decl);
8046 if (decl_context == FIELD && ctype == NULL_TREE)
8047 ctype = current_class_type;
45537677 8048 if (ctype
c11b6f21 8049 && TREE_OPERAND (decl, 0)
45537677
MS
8050 && (TREE_CODE (TREE_OPERAND (decl, 0)) == TYPE_DECL
8051 && ((DECL_NAME (TREE_OPERAND (decl, 0))
8052 == constructor_name_full (ctype))
8053 || (DECL_NAME (TREE_OPERAND (decl, 0))
8054 == constructor_name (ctype)))))
be99da77
MS
8055 TREE_OPERAND (decl, 0) = constructor_name (ctype);
8056 next = &TREE_OPERAND (decl, 0);
8057 decl = *next;
8058 if (ctype != NULL_TREE
8059 && decl != NULL_TREE && flags != DTOR_FLAG
8060 && decl == constructor_name (ctype))
8d08fdba 8061 {
be99da77
MS
8062 return_type = return_ctor;
8063 ctor_return_type = ctype;
8d08fdba 8064 }
be99da77
MS
8065 ctype = NULL_TREE;
8066 break;
386b8a85
JM
8067
8068 case TEMPLATE_ID_EXPR:
8069 {
8070 tree fns = TREE_OPERAND (decl, 0);
8071
8072 if (TREE_CODE (fns) == LOOKUP_EXPR)
8073 fns = TREE_OPERAND (fns, 0);
8074
8075 if (TREE_CODE (fns) == IDENTIFIER_NODE)
8076 dname = fns;
f84b4be9 8077 else if (is_overloaded_fn (fns))
386b8a85
JM
8078 dname = DECL_NAME (get_first_fn (fns));
8079 else
f84b4be9 8080 my_friendly_abort (0);
386b8a85 8081 }
2c73f9f5 8082 /* Fall through. */
be99da77
MS
8083
8084 case IDENTIFIER_NODE:
386b8a85
JM
8085 if (TREE_CODE (decl) == IDENTIFIER_NODE)
8086 dname = decl;
8087
be99da77
MS
8088 next = 0;
8089
8090 if (is_rid (dname))
8d08fdba 8091 {
be99da77
MS
8092 cp_error ("declarator-id missing; using reserved word `%D'",
8093 dname);
8094 name = IDENTIFIER_POINTER (dname);
8d08fdba 8095 }
be99da77 8096 if (! IDENTIFIER_OPNAME_P (dname)
956d6950 8097 /* GNU/Linux headers use '__op'. Arrgh. */
a703fb38 8098 || (IDENTIFIER_TYPENAME_P (dname) && ! TREE_TYPE (dname)))
be99da77 8099 name = IDENTIFIER_POINTER (dname);
8d08fdba
MS
8100 else
8101 {
be99da77 8102 if (IDENTIFIER_TYPENAME_P (dname))
8d08fdba 8103 {
be99da77
MS
8104 my_friendly_assert (flags == NO_SPECIAL, 154);
8105 flags = TYPENAME_FLAG;
8106 ctor_return_type = TREE_TYPE (dname);
8107 return_type = return_conversion;
8d08fdba 8108 }
be99da77 8109 name = operator_name_string (dname);
8d08fdba 8110 }
be99da77 8111 break;
8d08fdba 8112
be99da77
MS
8113 /* C++ extension */
8114 case SCOPE_REF:
8115 {
8116 /* Perform error checking, and decide on a ctype. */
8117 tree cname = TREE_OPERAND (decl, 0);
8118 if (cname == NULL_TREE)
8119 ctype = NULL_TREE;
2c73f9f5
ML
8120 else if (TREE_CODE (cname) == NAMESPACE_DECL)
8121 {
8122 ctype = NULL_TREE;
8123 in_namespace = TREE_OPERAND (decl, 0);
8124 TREE_OPERAND (decl, 0) = NULL_TREE;
8125 }
be99da77
MS
8126 else if (! is_aggr_type (cname, 1))
8127 TREE_OPERAND (decl, 0) = NULL_TREE;
8128 /* Must test TREE_OPERAND (decl, 1), in case user gives
8129 us `typedef (class::memfunc)(int); memfunc *memfuncptr;' */
8130 else if (TREE_OPERAND (decl, 1)
8131 && TREE_CODE (TREE_OPERAND (decl, 1)) == INDIRECT_REF)
8132 ctype = cname;
73b0fce8
KL
8133 else if (TREE_CODE (cname) == TEMPLATE_TYPE_PARM
8134 || TREE_CODE (cname) == TEMPLATE_TEMPLATE_PARM)
5566b478
MS
8135 {
8136 cp_error ("`%T::%D' is not a valid declarator", cname,
8137 TREE_OPERAND (decl, 1));
8138 cp_error (" perhaps you want `typename %T::%D' to make it a type",
8139 cname, TREE_OPERAND (decl, 1));
8140 return void_type_node;
8141 }
be99da77
MS
8142 else if (ctype == NULL_TREE)
8143 ctype = cname;
8144 else if (TREE_COMPLEXITY (decl) == current_class_depth)
8145 TREE_OPERAND (decl, 0) = ctype;
8146 else
8147 {
8148 if (! UNIQUELY_DERIVED_FROM_P (cname, ctype))
8149 {
8150 cp_error ("type `%T' is not derived from type `%T'",
8151 cname, ctype);
8152 TREE_OPERAND (decl, 0) = NULL_TREE;
8153 }
8154 else
8155 ctype = cname;
8156 }
8157
c91a56d2
MS
8158 if (ctype && TREE_CODE (TREE_OPERAND (decl, 1)) == TYPE_DECL
8159 && ((DECL_NAME (TREE_OPERAND (decl, 1))
8160 == constructor_name_full (ctype))
8161 || (DECL_NAME (TREE_OPERAND (decl, 1))
8162 == constructor_name (ctype))))
be99da77
MS
8163 TREE_OPERAND (decl, 1) = constructor_name (ctype);
8164 next = &TREE_OPERAND (decl, 1);
8165 decl = *next;
8166 if (ctype)
8167 {
8168 if (TREE_CODE (decl) == IDENTIFIER_NODE
8169 && constructor_name (ctype) == decl)
8170 {
8171 return_type = return_ctor;
8172 ctor_return_type = ctype;
8173 }
8174 else if (TREE_CODE (decl) == BIT_NOT_EXPR
8175 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
8176 && (constructor_name (ctype) == TREE_OPERAND (decl, 0)
8177 || constructor_name_full (ctype) == TREE_OPERAND (decl, 0)))
8178 {
8179 return_type = return_dtor;
8180 ctor_return_type = ctype;
8181 flags = DTOR_FLAG;
8182 TREE_OPERAND (decl, 0) = constructor_name (ctype);
8183 next = &TREE_OPERAND (decl, 0);
8184 }
8185 }
8186 }
8187 break;
8188
8189 case ERROR_MARK:
8190 next = 0;
8191 break;
8192
45537677
MS
8193 case TYPE_DECL:
8194 /* Parse error puts this typespec where
8195 a declarator should go. */
8196 cp_error ("`%T' specified as declarator-id", DECL_NAME (decl));
8197 if (TREE_TYPE (decl) == current_class_type)
8198 cp_error (" perhaps you want `%T' for a constructor",
8199 current_class_name);
8200 dname = DECL_NAME (decl);
8201 name = IDENTIFIER_POINTER (dname);
8202
e92cc029 8203 /* Avoid giving two errors for this. */
45537677
MS
8204 IDENTIFIER_CLASS_VALUE (dname) = NULL_TREE;
8205
8206 declspecs = temp_tree_cons (NULL_TREE, integer_type_node,
8207 declspecs);
8208 *next = dname;
8209 next = 0;
8210 break;
8211
be99da77 8212 default:
be99da77
MS
8213 cp_compiler_error ("`%D' as declarator", decl);
8214 return 0; /* We used to do a 155 abort here. */
8d08fdba 8215 }
be99da77 8216 }
8d08fdba
MS
8217 if (name == NULL)
8218 name = "type name";
8219 }
8220
8221 /* A function definition's declarator must have the form of
8222 a function declarator. */
8223
8224 if (funcdef_flag && innermost_code != CALL_EXPR)
8225 return 0;
8226
e1cd6e56
MS
8227 if (((dname && IDENTIFIER_OPNAME_P (dname)) || flags == TYPENAME_FLAG)
8228 && innermost_code != CALL_EXPR
8229 && ! (ctype && declspecs == NULL_TREE))
8230 {
8231 cp_error ("declaration of `%D' as non-function", dname);
8232 return void_type_node;
8233 }
8234
8d08fdba
MS
8235 /* Anything declared one level down from the top level
8236 must be one of the parameters of a function
8237 (because the body is at least two levels down). */
8238
8239 /* This heuristic cannot be applied to C++ nodes! Fixed, however,
8240 by not allowing C++ class definitions to specify their parameters
8241 with xdecls (must be spec.d in the parmlist).
8242
8243 Since we now wait to push a class scope until we are sure that
8244 we are in a legitimate method context, we must set oldcname
a9aedbc2
MS
8245 explicitly (since current_class_name is not yet alive).
8246
8247 We also want to avoid calling this a PARM if it is in a namespace. */
8d08fdba 8248
5566b478
MS
8249 if (decl_context == NORMAL && ! namespace_bindings_p ()
8250 && ! pseudo_global_level_p ())
a9aedbc2
MS
8251 {
8252 struct binding_level *b = current_binding_level;
8253 current_binding_level = b->level_chain;
8254 if (current_binding_level != 0 && toplevel_bindings_p ())
8255 decl_context = PARM;
8256 current_binding_level = b;
8257 }
8d08fdba
MS
8258
8259 /* Look through the decl specs and record which ones appear.
8260 Some typespecs are defined as built-in typenames.
8261 Others, the ones that are modifiers of other types,
8262 are represented by bits in SPECBITS: set the bits for
8263 the modifiers that appear. Storage class keywords are also in SPECBITS.
8264
8265 If there is a typedef name or a type, store the type in TYPE.
8266 This includes builtin typedefs such as `int'.
8267
8268 Set EXPLICIT_INT if the type is `int' or `char' and did not
8269 come from a user typedef.
8270
8271 Set LONGLONG if `long' is mentioned twice.
8272
8273 For C++, constructors and destructors have their own fast treatment. */
8274
8275 for (spec = declspecs; spec; spec = TREE_CHAIN (spec))
8276 {
8277 register int i;
8278 register tree id;
8279
8280 /* Certain parse errors slip through. For example,
8281 `int class;' is not caught by the parser. Try
8282 weakly to recover here. */
8283 if (TREE_CODE (spec) != TREE_LIST)
8284 return 0;
8285
8286 id = TREE_VALUE (spec);
8287
8288 if (TREE_CODE (id) == IDENTIFIER_NODE)
8289 {
a3203465
MS
8290 if (id == ridpointers[(int) RID_INT]
8291 || id == ridpointers[(int) RID_CHAR]
8292 || id == ridpointers[(int) RID_BOOL]
8293 || id == ridpointers[(int) RID_WCHAR])
8d08fdba
MS
8294 {
8295 if (type)
8ccc31eb
MS
8296 {
8297 if (id == ridpointers[(int) RID_BOOL])
8298 error ("`bool' is now a keyword");
8299 else
8300 cp_error ("extraneous `%T' ignored", id);
8301 }
8d08fdba
MS
8302 else
8303 {
a3203465
MS
8304 if (id == ridpointers[(int) RID_INT])
8305 explicit_int = 1;
8306 else if (id == ridpointers[(int) RID_CHAR])
8307 explicit_char = 1;
8d08fdba
MS
8308 type = TREE_TYPE (IDENTIFIER_GLOBAL_VALUE (id));
8309 }
8310 goto found;
8311 }
e92cc029 8312 /* C++ aggregate types. */
8d08fdba
MS
8313 if (IDENTIFIER_HAS_TYPE_VALUE (id))
8314 {
8315 if (type)
8316 cp_error ("multiple declarations `%T' and `%T'", type, id);
8317 else
8318 type = IDENTIFIER_TYPE_VALUE (id);
8319 goto found;
8320 }
8321
f376e137 8322 for (i = (int) RID_FIRST_MODIFIER; i <= (int) RID_LAST_MODIFIER; i++)
8d08fdba
MS
8323 {
8324 if (ridpointers[i] == id)
8325 {
8326 if (i == (int) RID_LONG && RIDBIT_SETP (i, specbits))
8327 {
e1cd6e56
MS
8328 if (pedantic && ! in_system_header)
8329 pedwarn ("ANSI C++ does not support `long long'");
9a3b49ac 8330 if (longlong)
a0a33927 8331 error ("`long long long' is too long for GCC");
8d08fdba
MS
8332 else
8333 longlong = 1;
8334 }
8335 else if (RIDBIT_SETP (i, specbits))
a0a33927 8336 pedwarn ("duplicate `%s'", IDENTIFIER_POINTER (id));
8d08fdba
MS
8337 RIDBIT_SET (i, specbits);
8338 goto found;
8339 }
8340 }
8341 }
e92cc029 8342 /* C++ aggregate types. */
73b0fce8 8343 else if (TREE_CODE (id) == TYPE_DECL || TREE_CODE (id) == TEMPLATE_DECL)
45537677
MS
8344 {
8345 if (type)
8346 cp_error ("multiple declarations `%T' and `%T'", type,
8347 TREE_TYPE (id));
8348 else
5566b478
MS
8349 {
8350 type = TREE_TYPE (id);
8351 TREE_VALUE (spec) = type;
8352 }
45537677
MS
8353 goto found;
8354 }
8d08fdba
MS
8355 if (type)
8356 error ("two or more data types in declaration of `%s'", name);
8357 else if (TREE_CODE (id) == IDENTIFIER_NODE)
8358 {
8359 register tree t = lookup_name (id, 1);
8360 if (!t || TREE_CODE (t) != TYPE_DECL)
8361 error ("`%s' fails to be a typedef or built in type",
8362 IDENTIFIER_POINTER (id));
8363 else
8364 {
8365 type = TREE_TYPE (t);
6125f3be
DE
8366#if 0
8367 /* See the code below that used this. */
f6abb50a 8368 decl_machine_attr = DECL_MACHINE_ATTRIBUTES (id);
6125f3be 8369#endif
8d08fdba
MS
8370 typedef_decl = t;
8371 }
8372 }
bd6dd845 8373 else if (id != error_mark_node)
8d08fdba
MS
8374 /* Can't change CLASS nodes into RECORD nodes here! */
8375 type = id;
8376
8377 found: ;
8378 }
8379
8380 typedef_type = type;
8381
37c46b43 8382 /* No type at all: default to `int', and set DEFAULTED_INT
8d08fdba
MS
8383 because it was not a user-defined typedef.
8384 Except when we have a `typedef' inside a signature, in
8385 which case the type defaults to `unknown type' and is
8386 instantiated when assigning to a signature pointer or ref. */
8387
a3203465
MS
8388 if (type == NULL_TREE
8389 && (RIDBIT_SETP (RID_SIGNED, specbits)
8390 || RIDBIT_SETP (RID_UNSIGNED, specbits)
8391 || RIDBIT_SETP (RID_LONG, specbits)
8392 || RIDBIT_SETP (RID_SHORT, specbits)))
8393 {
8394 /* These imply 'int'. */
8395 type = integer_type_node;
37c46b43 8396 defaulted_int = 1;
a3203465
MS
8397 }
8398
8d08fdba
MS
8399 if (type == NULL_TREE)
8400 {
8401 explicit_int = -1;
8402 if (return_type == return_dtor)
8403 type = void_type_node;
8404 else if (return_type == return_ctor)
f30432d7 8405 type = build_pointer_type (ctor_return_type);
51c184be
MS
8406 else if (return_type == return_conversion)
8407 type = ctor_return_type;
8d08fdba
MS
8408 else if (current_class_type
8409 && IS_SIGNATURE (current_class_type)
fc378698 8410 && RIDBIT_SETP (RID_TYPEDEF, specbits)
8d08fdba
MS
8411 && (decl_context == FIELD || decl_context == NORMAL))
8412 {
8413 explicit_int = 0;
8414 opaque_typedef = 1;
8415 type = copy_node (opaque_type_node);
8416 }
8417 else
8418 {
a28e3c7f
MS
8419 if (funcdef_flag)
8420 {
8421 if (warn_return_type
a3203465 8422 && return_type == return_normal)
a28e3c7f
MS
8423 /* Save warning until we know what is really going on. */
8424 warn_about_return_type = 1;
8425 }
a3203465
MS
8426 else if (RIDBIT_SETP (RID_TYPEDEF, specbits))
8427 pedwarn ("ANSI C++ forbids typedef which does not specify a type");
343fdf03
JM
8428 else if (innermost_code != CALL_EXPR || pedantic
8429 || (warn_return_type && return_type == return_normal))
8430 {
8431 if (innermost_code == CALL_EXPR)
8432 cp_pedwarn ("return-type of `%D' defaults to `int'", dname);
8433 else
8434 cp_pedwarn ("ANSI C++ forbids declaration `%D' with no type",
8435 dname);
8436 }
8d08fdba
MS
8437 type = integer_type_node;
8438 }
8439 }
8440 else if (return_type == return_dtor)
8441 {
8442 error ("return type specification for destructor invalid");
8443 type = void_type_node;
8444 }
8445 else if (return_type == return_ctor)
8446 {
8447 error ("return type specification for constructor invalid");
f30432d7 8448 type = build_pointer_type (ctor_return_type);
8d08fdba 8449 }
51c184be
MS
8450 else if (return_type == return_conversion)
8451 {
e1cd6e56 8452 if (comptypes (type, ctor_return_type, 1) == 0)
51c184be
MS
8453 cp_error ("operator `%T' declared to return `%T'",
8454 ctor_return_type, type);
8455 else
8456 cp_pedwarn ("return type specified for `operator %T'",
8457 ctor_return_type);
8458
8459 type = ctor_return_type;
8460 }
8d08fdba
MS
8461
8462 ctype = NULL_TREE;
8463
8464 /* Now process the modifiers that were specified
8465 and check for invalid combinations. */
8466
8467 /* Long double is a special combination. */
8468
8469 if (RIDBIT_SETP (RID_LONG, specbits)
8470 && TYPE_MAIN_VARIANT (type) == double_type_node)
8471 {
8472 RIDBIT_RESET (RID_LONG, specbits);
8473 type = build_type_variant (long_double_type_node, TYPE_READONLY (type),
8474 TYPE_VOLATILE (type));
8475 }
8476
8477 /* Check all other uses of type modifiers. */
8478
8479 if (RIDBIT_SETP (RID_UNSIGNED, specbits)
8480 || RIDBIT_SETP (RID_SIGNED, specbits)
8481 || RIDBIT_SETP (RID_LONG, specbits)
8482 || RIDBIT_SETP (RID_SHORT, specbits))
8483 {
8484 int ok = 0;
8485
8486 if (TREE_CODE (type) == REAL_TYPE)
8487 error ("short, signed or unsigned invalid for `%s'", name);
b7484fbe 8488 else if (TREE_CODE (type) != INTEGER_TYPE)
8d08fdba
MS
8489 error ("long, short, signed or unsigned invalid for `%s'", name);
8490 else if (RIDBIT_SETP (RID_LONG, specbits)
8491 && RIDBIT_SETP (RID_SHORT, specbits))
8492 error ("long and short specified together for `%s'", name);
8493 else if ((RIDBIT_SETP (RID_LONG, specbits)
8494 || RIDBIT_SETP (RID_SHORT, specbits))
8495 && explicit_char)
8496 error ("long or short specified with char for `%s'", name);
8497 else if ((RIDBIT_SETP (RID_LONG, specbits)
8498 || RIDBIT_SETP (RID_SHORT, specbits))
8499 && TREE_CODE (type) == REAL_TYPE)
8500 error ("long or short specified with floating type for `%s'", name);
8501 else if (RIDBIT_SETP (RID_SIGNED, specbits)
8502 && RIDBIT_SETP (RID_UNSIGNED, specbits))
8503 error ("signed and unsigned given together for `%s'", name);
8504 else
8505 {
8506 ok = 1;
37c46b43 8507 if (!explicit_int && !defaulted_int && !explicit_char && pedantic)
8d08fdba
MS
8508 {
8509 pedwarn ("long, short, signed or unsigned used invalidly for `%s'",
8510 name);
8511 if (flag_pedantic_errors)
8512 ok = 0;
8513 }
8514 }
8515
8516 /* Discard the type modifiers if they are invalid. */
8517 if (! ok)
8518 {
8519 RIDBIT_RESET (RID_UNSIGNED, specbits);
8520 RIDBIT_RESET (RID_SIGNED, specbits);
8521 RIDBIT_RESET (RID_LONG, specbits);
8522 RIDBIT_RESET (RID_SHORT, specbits);
8523 longlong = 0;
8524 }
8525 }
8526
37c46b43
MS
8527 if (RIDBIT_SETP (RID_COMPLEX, specbits)
8528 && TREE_CODE (type) != INTEGER_TYPE && TREE_CODE (type) != REAL_TYPE)
8529 {
8530 error ("complex invalid for `%s'", name);
8531 RIDBIT_RESET (RID_COMPLEX, specbits);
8532 }
8533
8d08fdba
MS
8534 /* Decide whether an integer type is signed or not.
8535 Optionally treat bitfields as signed by default. */
8536 if (RIDBIT_SETP (RID_UNSIGNED, specbits)
8d08fdba 8537 || (bitfield && ! flag_signed_bitfields
37c46b43 8538 && (explicit_int || defaulted_int || explicit_char
8d08fdba
MS
8539 /* A typedef for plain `int' without `signed'
8540 can be controlled just like plain `int'. */
8541 || ! (typedef_decl != NULL_TREE
8542 && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl)))
8543 && TREE_CODE (type) != ENUMERAL_TYPE
8544 && RIDBIT_NOTSETP (RID_SIGNED, specbits)))
8545 {
8546 if (longlong)
8547 type = long_long_unsigned_type_node;
8548 else if (RIDBIT_SETP (RID_LONG, specbits))
8549 type = long_unsigned_type_node;
8550 else if (RIDBIT_SETP (RID_SHORT, specbits))
8551 type = short_unsigned_type_node;
8552 else if (type == char_type_node)
8553 type = unsigned_char_type_node;
8554 else if (typedef_decl)
8555 type = unsigned_type (type);
8556 else
8557 type = unsigned_type_node;
8558 }
8559 else if (RIDBIT_SETP (RID_SIGNED, specbits)
8560 && type == char_type_node)
8561 type = signed_char_type_node;
8562 else if (longlong)
8563 type = long_long_integer_type_node;
8564 else if (RIDBIT_SETP (RID_LONG, specbits))
8565 type = long_integer_type_node;
8566 else if (RIDBIT_SETP (RID_SHORT, specbits))
8567 type = short_integer_type_node;
8568
37c46b43
MS
8569 if (RIDBIT_SETP (RID_COMPLEX, specbits))
8570 {
8571 /* If we just have "complex", it is equivalent to
8572 "complex double", but if any modifiers at all are specified it is
8573 the complex form of TYPE. E.g, "complex short" is
8574 "complex short int". */
8575
8576 if (defaulted_int && ! longlong
8577 && ! (RIDBIT_SETP (RID_LONG, specbits)
8578 || RIDBIT_SETP (RID_SHORT, specbits)
8579 || RIDBIT_SETP (RID_SIGNED, specbits)
8580 || RIDBIT_SETP (RID_UNSIGNED, specbits)))
8581 type = complex_double_type_node;
8582 else if (type == integer_type_node)
8583 type = complex_integer_type_node;
8584 else if (type == float_type_node)
8585 type = complex_float_type_node;
8586 else if (type == double_type_node)
8587 type = complex_double_type_node;
8588 else if (type == long_double_type_node)
8589 type = complex_long_double_type_node;
8590 else
8591 type = build_complex_type (type);
8592 }
8593
3ac3d9ea
MM
8594 if (return_type == return_conversion
8595 && (RIDBIT_SETP (RID_CONST, specbits)
8596 || RIDBIT_SETP (RID_VOLATILE, specbits)))
8597 cp_error ("`operator %T' cannot be cv-qualified",
8598 ctor_return_type);
8599
8d08fdba
MS
8600 /* Set CONSTP if this declaration is `const', whether by
8601 explicit specification or via a typedef.
8602 Likewise for VOLATILEP. */
8603
8604 constp = !! RIDBIT_SETP (RID_CONST, specbits) + TYPE_READONLY (type);
8605 volatilep = !! RIDBIT_SETP (RID_VOLATILE, specbits) + TYPE_VOLATILE (type);
a1774733 8606 type = build_type_variant (type, 0, 0);
8d08fdba
MS
8607 staticp = 0;
8608 inlinep = !! RIDBIT_SETP (RID_INLINE, specbits);
8d08fdba 8609 virtualp = RIDBIT_SETP (RID_VIRTUAL, specbits);
db5ae43f
MS
8610 RIDBIT_RESET (RID_VIRTUAL, specbits);
8611 explicitp = RIDBIT_SETP (RID_EXPLICIT, specbits) != 0;
8612 RIDBIT_RESET (RID_EXPLICIT, specbits);
8d08fdba 8613
8d08fdba
MS
8614 if (RIDBIT_SETP (RID_STATIC, specbits))
8615 staticp = 1 + (decl_context == FIELD);
8616
8617 if (virtualp && staticp == 2)
8618 {
8619 cp_error ("member `%D' cannot be declared both virtual and static",
8620 dname);
8621 staticp = 0;
8622 }
8623 friendp = RIDBIT_SETP (RID_FRIEND, specbits);
8d08fdba
MS
8624 RIDBIT_RESET (RID_FRIEND, specbits);
8625
8626 if (RIDBIT_SETP (RID_MUTABLE, specbits))
8627 {
8628 if (decl_context == PARM)
8629 {
db5ae43f 8630 error ("non-member `%s' cannot be declared `mutable'", name);
8d08fdba
MS
8631 RIDBIT_RESET (RID_MUTABLE, specbits);
8632 }
8633 else if (friendp || decl_context == TYPENAME)
8634 {
db5ae43f 8635 error ("non-object member `%s' cannot be declared `mutable'", name);
8d08fdba
MS
8636 RIDBIT_RESET (RID_MUTABLE, specbits);
8637 }
8d08fdba
MS
8638 }
8639
8640 /* Warn if two storage classes are given. Default to `auto'. */
8641
8642 if (RIDBIT_ANY_SET (specbits))
8643 {
8644 if (RIDBIT_SETP (RID_STATIC, specbits)) nclasses++;
8645 if (RIDBIT_SETP (RID_EXTERN, specbits)) nclasses++;
8646 if (decl_context == PARM && nclasses > 0)
8647 error ("storage class specifiers invalid in parameter declarations");
8648 if (RIDBIT_SETP (RID_TYPEDEF, specbits))
8649 {
8650 if (decl_context == PARM)
8651 error ("typedef declaration invalid in parameter declaration");
8652 nclasses++;
8653 }
8654 if (RIDBIT_SETP (RID_AUTO, specbits)) nclasses++;
8655 if (RIDBIT_SETP (RID_REGISTER, specbits)) nclasses++;
8656 }
8657
8658 /* Give error if `virtual' is used outside of class declaration. */
b7484fbe
MS
8659 if (virtualp
8660 && (current_class_name == NULL_TREE || decl_context != FIELD))
8d08fdba
MS
8661 {
8662 error ("virtual outside class declaration");
8663 virtualp = 0;
8664 }
8665 if (current_class_name == NULL_TREE && RIDBIT_SETP (RID_MUTABLE, specbits))
8666 {
8667 error ("only members can be declared mutable");
8668 RIDBIT_RESET (RID_MUTABLE, specbits);
8669 }
8670
8671 /* Static anonymous unions are dealt with here. */
8672 if (staticp && decl_context == TYPENAME
8673 && TREE_CODE (declspecs) == TREE_LIST
8674 && TREE_CODE (TREE_VALUE (declspecs)) == UNION_TYPE
8675 && ANON_AGGRNAME_P (TYPE_IDENTIFIER (TREE_VALUE (declspecs))))
8676 decl_context = FIELD;
8677
8678 /* Give error if `const,' `volatile,' `inline,' `friend,' or `virtual'
8679 is used in a signature member function declaration. */
8680 if (decl_context == FIELD
8681 && IS_SIGNATURE (current_class_type)
fc378698 8682 && RIDBIT_NOTSETP (RID_TYPEDEF, specbits))
8d08fdba
MS
8683 {
8684 if (constp)
8685 {
8686 error ("`const' specified for signature member function `%s'", name);
8687 constp = 0;
8688 }
8689 if (volatilep)
8690 {
a28e3c7f
MS
8691 error ("`volatile' specified for signature member function `%s'",
8692 name);
8d08fdba
MS
8693 volatilep = 0;
8694 }
8695 if (inlinep)
8696 {
8697 error ("`inline' specified for signature member function `%s'", name);
8698 /* Later, we'll make signature member functions inline. */
8699 inlinep = 0;
8700 }
8701 if (friendp)
8702 {
8703 error ("`friend' declaration in signature definition");
8704 friendp = 0;
8705 }
8706 if (virtualp)
8707 {
a28e3c7f
MS
8708 error ("`virtual' specified for signature member function `%s'",
8709 name);
8d08fdba
MS
8710 /* Later, we'll make signature member functions virtual. */
8711 virtualp = 0;
8712 }
8713 }
8714
8715 /* Warn about storage classes that are invalid for certain
8716 kinds of declarations (parameters, typenames, etc.). */
8717
8718 if (nclasses > 1)
8719 error ("multiple storage classes in declaration of `%s'", name);
8720 else if (decl_context != NORMAL && nclasses > 0)
8721 {
db5ae43f 8722 if ((decl_context == PARM || decl_context == CATCHPARM)
8d08fdba
MS
8723 && (RIDBIT_SETP (RID_REGISTER, specbits)
8724 || RIDBIT_SETP (RID_AUTO, specbits)))
8725 ;
fc378698
MS
8726 else if (RIDBIT_SETP (RID_TYPEDEF, specbits))
8727 ;
8d08fdba 8728 else if (decl_context == FIELD
fc378698 8729 && ! IS_SIGNATURE (current_class_type)
8d08fdba
MS
8730 /* C++ allows static class elements */
8731 && RIDBIT_SETP (RID_STATIC, specbits))
8732 /* C++ also allows inlines and signed and unsigned elements,
8733 but in those cases we don't come in here. */
8734 ;
8735 else
8736 {
8737 if (decl_context == FIELD)
8738 {
b7484fbe
MS
8739 tree tmp = NULL_TREE;
8740 register int op = 0;
8741
8742 if (declarator)
8743 {
9e9ff709
MS
8744 /* Avoid trying to get an operand off an identifier node. */
8745 if (TREE_CODE (declarator) == IDENTIFIER_NODE)
8746 tmp = declarator;
8747 else
8748 tmp = TREE_OPERAND (declarator, 0);
b7484fbe
MS
8749 op = IDENTIFIER_OPNAME_P (tmp);
8750 }
8d08fdba
MS
8751 error ("storage class specified for %s `%s'",
8752 IS_SIGNATURE (current_class_type)
8753 ? (op
8754 ? "signature member operator"
8755 : "signature member function")
b7484fbe 8756 : (op ? "member operator" : "field"),
8d08fdba
MS
8757 op ? operator_name_string (tmp) : name);
8758 }
8759 else
db5ae43f 8760 error (((decl_context == PARM || decl_context == CATCHPARM)
8d08fdba
MS
8761 ? "storage class specified for parameter `%s'"
8762 : "storage class specified for typename"), name);
8763 RIDBIT_RESET (RID_REGISTER, specbits);
8764 RIDBIT_RESET (RID_AUTO, specbits);
8765 RIDBIT_RESET (RID_EXTERN, specbits);
8766
8767 if (decl_context == FIELD && IS_SIGNATURE (current_class_type))
8768 {
8769 RIDBIT_RESET (RID_STATIC, specbits);
8770 staticp = 0;
8771 }
8772 }
8773 }
8774 else if (RIDBIT_SETP (RID_EXTERN, specbits) && initialized && !funcdef_flag)
8775 {
a9aedbc2 8776 if (toplevel_bindings_p ())
8d08fdba 8777 {
59be0cdd 8778 /* It's common practice (and completely valid) to have a const
8d08fdba
MS
8779 be initialized and declared extern. */
8780 if (! constp)
8781 warning ("`%s' initialized and declared `extern'", name);
8782 }
8783 else
8784 error ("`%s' has both `extern' and initializer", name);
8785 }
8786 else if (RIDBIT_SETP (RID_EXTERN, specbits) && funcdef_flag
a9aedbc2 8787 && ! toplevel_bindings_p ())
8d08fdba 8788 error ("nested function `%s' declared `extern'", name);
a9aedbc2 8789 else if (toplevel_bindings_p ())
8d08fdba
MS
8790 {
8791 if (RIDBIT_SETP (RID_AUTO, specbits))
8792 error ("top-level declaration of `%s' specifies `auto'", name);
8d08fdba
MS
8793 }
8794
909e536a
MS
8795 if (nclasses > 0 && friendp)
8796 error ("storage class specifiers invalid in friend function declarations");
8797
8d08fdba
MS
8798 /* Now figure out the structure of the declarator proper.
8799 Descend through it, creating more complex types, until we reach
8800 the declared identifier (or NULL_TREE, in an absolute declarator). */
8801
386b8a85
JM
8802 while (declarator && TREE_CODE (declarator) != IDENTIFIER_NODE
8803 && TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
8d08fdba
MS
8804 {
8805 /* Each level of DECLARATOR is either an ARRAY_REF (for ...[..]),
8806 an INDIRECT_REF (for *...),
8807 a CALL_EXPR (for ...(...)),
8808 an identifier (for the name being declared)
8809 or a null pointer (for the place in an absolute declarator
8810 where the name was omitted).
8811 For the last two cases, we have just exited the loop.
8812
8813 For C++ it could also be
8814 a SCOPE_REF (for class :: ...). In this case, we have converted
8815 sensible names to types, and those are the values we use to
8816 qualify the member name.
8817 an ADDR_EXPR (for &...),
8818 a BIT_NOT_EXPR (for destructors)
8d08fdba
MS
8819
8820 At this point, TYPE is the type of elements of an array,
8821 or for a function to return, or for a pointer to point to.
8822 After this sequence of ifs, TYPE is the type of the
8823 array or function or pointer, and DECLARATOR has had its
8824 outermost layer removed. */
8825
bd6dd845 8826 if (type == error_mark_node)
8d08fdba
MS
8827 {
8828 if (TREE_CODE (declarator) == SCOPE_REF)
8829 declarator = TREE_OPERAND (declarator, 1);
8830 else
8831 declarator = TREE_OPERAND (declarator, 0);
8832 continue;
8833 }
8834 if (quals != NULL_TREE
8835 && (declarator == NULL_TREE
8836 || TREE_CODE (declarator) != SCOPE_REF))
8837 {
8838 if (ctype == NULL_TREE && TREE_CODE (type) == METHOD_TYPE)
8839 ctype = TYPE_METHOD_BASETYPE (type);
8840 if (ctype != NULL_TREE)
8841 {
8d08fdba 8842 tree dummy = build_decl (TYPE_DECL, NULL_TREE, type);
8d08fdba
MS
8843 ctype = grok_method_quals (ctype, dummy, quals);
8844 type = TREE_TYPE (dummy);
8845 quals = NULL_TREE;
8846 }
8847 }
8848 switch (TREE_CODE (declarator))
8849 {
8850 case ARRAY_REF:
8851 {
8852 register tree itype = NULL_TREE;
8853 register tree size = TREE_OPERAND (declarator, 1);
b7484fbe
MS
8854 /* The index is a signed object `sizetype' bits wide. */
8855 tree index_type = signed_type (sizetype);
8d08fdba
MS
8856
8857 declarator = TREE_OPERAND (declarator, 0);
8858
8859 /* Check for some types that there cannot be arrays of. */
8860
a1774733 8861 if (TREE_CODE (type) == VOID_TYPE)
8d08fdba
MS
8862 {
8863 cp_error ("declaration of `%D' as array of voids", dname);
8864 type = error_mark_node;
8865 }
8866
8867 if (TREE_CODE (type) == FUNCTION_TYPE)
8868 {
8869 cp_error ("declaration of `%D' as array of functions", dname);
8870 type = error_mark_node;
8871 }
8872
8873 /* ARM $8.4.3: Since you can't have a pointer to a reference,
8874 you can't have arrays of references. If we allowed them,
59be0cdd 8875 then we'd be saying x[i] is valid for an array x, but
8d08fdba
MS
8876 then you'd have to ask: what does `*(x + i)' mean? */
8877 if (TREE_CODE (type) == REFERENCE_TYPE)
8878 {
8879 if (decl_context == TYPENAME)
8880 cp_error ("cannot make arrays of references");
8881 else
8882 cp_error ("declaration of `%D' as array of references",
8883 dname);
8884 type = error_mark_node;
8885 }
8886
8887 if (TREE_CODE (type) == OFFSET_TYPE)
8888 {
8889 cp_error ("declaration of `%D' as array of data members",
8890 dname);
8891 type = error_mark_node;
8892 }
8893
8894 if (TREE_CODE (type) == METHOD_TYPE)
8895 {
8896 cp_error ("declaration of `%D' as array of function members",
8897 dname);
8898 type = error_mark_node;
8899 }
8900
8901 if (size == error_mark_node)
8902 type = error_mark_node;
8903
8904 if (type == error_mark_node)
8905 continue;
8906
8907 if (size)
8908 {
8909 /* Must suspend_momentary here because the index
8910 type may need to live until the end of the function.
8911 For example, it is used in the declaration of a
8912 variable which requires destructing at the end of
8913 the function; then build_vec_delete will need this
8914 value. */
8915 int yes = suspend_momentary ();
2c73f9f5 8916 /* Might be a cast. */
8d08fdba
MS
8917 if (TREE_CODE (size) == NOP_EXPR
8918 && TREE_TYPE (size) == TREE_TYPE (TREE_OPERAND (size, 0)))
8919 size = TREE_OPERAND (size, 0);
8920
5566b478
MS
8921 /* If this involves a template parameter, it'll be
8922 constant, but we don't know what the value is yet. */
5156628f 8923 if (processing_template_decl)
5566b478
MS
8924 {
8925 itype = make_node (INTEGER_TYPE);
8926 TYPE_MIN_VALUE (itype) = size_zero_node;
8927 TYPE_MAX_VALUE (itype) = build_min
8928 (MINUS_EXPR, sizetype, size, integer_one_node);
8929 goto dont_grok_size;
8930 }
8d08fdba
MS
8931
8932 if (TREE_CODE (TREE_TYPE (size)) != INTEGER_TYPE
8933 && TREE_CODE (TREE_TYPE (size)) != ENUMERAL_TYPE)
8934 {
8935 cp_error ("size of array `%D' has non-integer type",
8936 dname);
8937 size = integer_one_node;
8938 }
8939 if (TREE_READONLY_DECL_P (size))
8940 size = decl_constant_value (size);
e1cd6e56 8941 if (pedantic && integer_zerop (size))
8d08fdba
MS
8942 cp_pedwarn ("ANSI C++ forbids zero-size array `%D'", dname);
8943 if (TREE_CONSTANT (size))
8944 {
dff6b454
RK
8945 int old_flag_pedantic_errors = flag_pedantic_errors;
8946 int old_pedantic = pedantic;
8947 pedantic = flag_pedantic_errors = 1;
8948 /* Always give overflow errors on array subscripts. */
8d08fdba 8949 constant_expression_warning (size);
dff6b454
RK
8950 pedantic = old_pedantic;
8951 flag_pedantic_errors = old_flag_pedantic_errors;
8d08fdba
MS
8952 if (INT_CST_LT (size, integer_zero_node))
8953 {
8954 cp_error ("size of array `%D' is negative", dname);
8955 size = integer_one_node;
8956 }
8d08fdba
MS
8957 }
8958 else
8959 {
e1cd6e56 8960 if (pedantic)
a0a33927
MS
8961 {
8962 if (dname)
8963 cp_pedwarn ("ANSI C++ forbids variable-size array `%D'",
8964 dname);
8965 else
8966 cp_pedwarn ("ANSI C++ forbids variable-size array");
8967 }
8d08fdba 8968 }
b7484fbe 8969
beb53fb8
JM
8970 itype
8971 = fold (build_binary_op (MINUS_EXPR,
37c46b43
MS
8972 cp_convert (index_type, size),
8973 cp_convert (index_type,
8974 integer_one_node), 1));
b7484fbe
MS
8975 if (! TREE_CONSTANT (itype))
8976 itype = variable_size (itype);
594740f3
MS
8977 else if (TREE_OVERFLOW (itype))
8978 {
8979 error ("overflow in array dimension");
8980 TREE_OVERFLOW (itype) = 0;
8981 }
fc378698
MS
8982
8983 /* If we're a parm, we need to have a permanent type so
8984 mangling checks for re-use will work right. If both the
8985 element and index types are permanent, the array type
8986 will be, too. */
8987 if (decl_context == PARM
8988 && allocation_temporary_p () && TREE_PERMANENT (type))
8989 {
8990 push_obstacks (&permanent_obstack, &permanent_obstack);
8991 itype = build_index_type (itype);
8992 pop_obstacks ();
8993 }
8994 else
8995 itype = build_index_type (itype);
8996
5566b478 8997 dont_grok_size:
8d08fdba
MS
8998 resume_momentary (yes);
8999 }
9000
9001 /* Build the array type itself, then merge any constancy or
9002 volatility into the target type. We must do it in this order
9003 to ensure that the TYPE_MAIN_VARIANT field of the array type
9004 is set correctly. */
9005
9006 type = build_cplus_array_type (type, itype);
9007 if (constp || volatilep)
f376e137 9008 type = cp_build_type_variant (type, constp, volatilep);
8d08fdba
MS
9009
9010 ctype = NULL_TREE;
9011 }
9012 break;
9013
9014 case CALL_EXPR:
9015 {
9016 tree arg_types;
f376e137
MS
9017 int funcdecl_p;
9018 tree inner_parms = TREE_OPERAND (declarator, 1);
9019 tree inner_decl = TREE_OPERAND (declarator, 0);
8d08fdba
MS
9020
9021 /* Declaring a function type.
9022 Make sure we have a valid type for the function to return. */
9023#if 0
9024 /* Is this an error? Should they be merged into TYPE here? */
9025 if (pedantic && (constp || volatilep))
9026 pedwarn ("function declared to return const or volatile result");
9027#else
21474714
MS
9028 /* Merge any constancy or volatility into the function return
9029 type. */
8d08fdba
MS
9030
9031 if (constp || volatilep)
9032 {
f376e137 9033 type = cp_build_type_variant (type, constp, volatilep);
8d08fdba
MS
9034 if (IS_AGGR_TYPE (type))
9035 build_pointer_type (type);
9036 constp = 0;
9037 volatilep = 0;
9038 }
9039#endif
9040
9041 /* Warn about some types functions can't return. */
9042
9043 if (TREE_CODE (type) == FUNCTION_TYPE)
9044 {
9045 error ("`%s' declared as function returning a function", name);
9046 type = integer_type_node;
9047 }
9048 if (TREE_CODE (type) == ARRAY_TYPE)
9049 {
9050 error ("`%s' declared as function returning an array", name);
9051 type = integer_type_node;
9052 }
9053
f376e137
MS
9054 if (inner_decl && TREE_CODE (inner_decl) == SCOPE_REF)
9055 inner_decl = TREE_OPERAND (inner_decl, 1);
9056
386b8a85
JM
9057 if (inner_decl && TREE_CODE (inner_decl) == TEMPLATE_ID_EXPR)
9058 inner_decl = dname;
9059
b7484fbe
MS
9060 /* Pick up type qualifiers which should be applied to `this'. */
9061 quals = TREE_OPERAND (declarator, 2);
9062
c11b6f21
MS
9063 /* Pick up the exception specifications. */
9064 raises = TREE_TYPE (declarator);
9065
f376e137
MS
9066 /* Say it's a definition only for the CALL_EXPR
9067 closest to the identifier. */
beb53fb8 9068 funcdecl_p
386b8a85
JM
9069 = inner_decl
9070 && (TREE_CODE (inner_decl) == IDENTIFIER_NODE
9071 || TREE_CODE (inner_decl) == TEMPLATE_ID_EXPR
9072 || TREE_CODE (inner_decl) == BIT_NOT_EXPR);
9073
8d08fdba
MS
9074 if (ctype == NULL_TREE
9075 && decl_context == FIELD
f376e137 9076 && funcdecl_p
8d08fdba
MS
9077 && (friendp == 0 || dname == current_class_name))
9078 ctype = current_class_type;
9079
51c184be 9080 if (ctype && return_type == return_conversion)
8d08fdba
MS
9081 TYPE_HAS_CONVERSION (ctype) = 1;
9082 if (ctype && constructor_name (ctype) == dname)
9083 {
9084 /* We are within a class's scope. If our declarator name
9085 is the same as the class name, and we are defining
9086 a function, then it is a constructor/destructor, and
9087 therefore returns a void type. */
9088
9089 if (flags == DTOR_FLAG)
9090 {
9091 /* ANSI C++ June 5 1992 WP 12.4.1. A destructor may
9092 not be declared const or volatile. A destructor
9093 may not be static. */
9094 if (staticp == 2)
9095 error ("destructor cannot be static member function");
b7484fbe 9096 if (quals)
8d08fdba 9097 {
b7484fbe 9098 error ("destructors cannot be declared `const' or `volatile'");
8d08fdba
MS
9099 return void_type_node;
9100 }
9101 if (decl_context == FIELD)
9102 {
9103 if (! member_function_or_else (ctype, current_class_type,
9104 "destructor for alien class `%s' cannot be a member"))
9105 return void_type_node;
9106 }
9107 }
2c73f9f5 9108 else /* It's a constructor. */
8d08fdba 9109 {
db5ae43f
MS
9110 if (explicitp == 1)
9111 explicitp = 2;
8d08fdba
MS
9112 /* ANSI C++ June 5 1992 WP 12.1.2. A constructor may
9113 not be declared const or volatile. A constructor may
9114 not be virtual. A constructor may not be static. */
9115 if (staticp == 2)
9116 error ("constructor cannot be static member function");
9117 if (virtualp)
9118 {
9119 pedwarn ("constructors cannot be declared virtual");
9120 virtualp = 0;
9121 }
b7484fbe 9122 if (quals)
8d08fdba 9123 {
b7484fbe 9124 error ("constructors cannot be declared `const' or `volatile'");
8d08fdba
MS
9125 return void_type_node;
9126 }
8d08fdba 9127 {
51c184be 9128 RID_BIT_TYPE tmp_bits;
fc378698 9129 bcopy ((void*)&specbits, (void*)&tmp_bits, sizeof (RID_BIT_TYPE));
51c184be
MS
9130 RIDBIT_RESET (RID_INLINE, tmp_bits);
9131 RIDBIT_RESET (RID_STATIC, tmp_bits);
9132 if (RIDBIT_ANY_SET (tmp_bits))
8d08fdba 9133 error ("return value type specifier for constructor ignored");
8d08fdba 9134 }
f30432d7 9135 type = build_pointer_type (ctype);
beb53fb8
JM
9136 if (decl_context == FIELD
9137 && IS_SIGNATURE (current_class_type))
8d08fdba
MS
9138 {
9139 error ("constructor not allowed in signature");
9140 return void_type_node;
9141 }
9142 else if (decl_context == FIELD)
9143 {
9144 if (! member_function_or_else (ctype, current_class_type,
9145 "constructor for alien class `%s' cannot be member"))
9146 return void_type_node;
9147 TYPE_HAS_CONSTRUCTOR (ctype) = 1;
9148 if (return_type != return_ctor)
9149 return NULL_TREE;
9150 }
9151 }
9152 if (decl_context == FIELD)
9153 staticp = 0;
9154 }
b7484fbe 9155 else if (friendp)
8d08fdba 9156 {
b7484fbe
MS
9157 if (initialized)
9158 error ("can't initialize friend function `%s'", name);
9159 if (virtualp)
9160 {
9161 /* Cannot be both friend and virtual. */
9162 error ("virtual functions cannot be friends");
9163 RIDBIT_RESET (RID_FRIEND, specbits);
9164 friendp = 0;
9165 }
28cbf42c
MS
9166 if (decl_context == NORMAL)
9167 error ("friend declaration not in class definition");
9168 if (current_function_decl && funcdef_flag)
9169 cp_error ("can't define friend function `%s' in a local class definition",
9170 name);
8d08fdba
MS
9171 }
9172
8d08fdba
MS
9173 /* Construct the function type and go to the next
9174 inner layer of declarator. */
9175
f376e137 9176 declarator = TREE_OPERAND (declarator, 0);
8d08fdba 9177
f376e137
MS
9178 /* FIXME: This is where default args should be fully
9179 processed. */
8d08fdba 9180
f376e137 9181 arg_types = grokparms (inner_parms, funcdecl_p ? funcdef_flag : 0);
8d08fdba
MS
9182
9183 if (declarator)
9184 {
9185 /* Get past destructors, etc.
9186 We know we have one because FLAGS will be non-zero.
9187
9188 Complain about improper parameter lists here. */
9189 if (TREE_CODE (declarator) == BIT_NOT_EXPR)
9190 {
9191 declarator = TREE_OPERAND (declarator, 0);
9192
9193 if (strict_prototype == 0 && arg_types == NULL_TREE)
9194 arg_types = void_list_node;
9195 else if (arg_types == NULL_TREE
9196 || arg_types != void_list_node)
9197 {
9198 error ("destructors cannot be specified with parameters");
9199 arg_types = void_list_node;
9200 }
9201 }
9202 }
9203
d22c8596 9204 /* ANSI says that `const int foo ();'
8d08fdba 9205 does not make the function foo const. */
d22c8596 9206 type = build_function_type (type, arg_types);
42976354
BK
9207
9208 {
9209 tree t;
9210 for (t = arg_types; t; t = TREE_CHAIN (t))
9211 if (TREE_PURPOSE (t)
9212 && TREE_CODE (TREE_PURPOSE (t)) == DEFAULT_ARG)
9213 {
9214 add_defarg_fn (type);
9215 break;
9216 }
9217 }
8d08fdba
MS
9218 }
9219 break;
9220
9221 case ADDR_EXPR:
9222 case INDIRECT_REF:
9223 /* Filter out pointers-to-references and references-to-references.
9224 We can get these if a TYPE_DECL is used. */
9225
9226 if (TREE_CODE (type) == REFERENCE_TYPE)
9227 {
9228 error ("cannot declare %s to references",
9229 TREE_CODE (declarator) == ADDR_EXPR
9230 ? "references" : "pointers");
9231 declarator = TREE_OPERAND (declarator, 0);
9232 continue;
9233 }
9234
a5894242
MS
9235 if (TREE_CODE (type) == OFFSET_TYPE
9236 && (TREE_CODE (TREE_TYPE (type)) == VOID_TYPE
9237 || TREE_CODE (TREE_TYPE (type)) == REFERENCE_TYPE))
9238 {
9239 cp_error ("cannot declare pointer to `%#T' member",
9240 TREE_TYPE (type));
9241 type = TREE_TYPE (type);
9242 }
9243
8d08fdba
MS
9244 /* Merge any constancy or volatility into the target type
9245 for the pointer. */
9246
9247 if (constp || volatilep)
9248 {
9249 /* A const or volatile signature pointer/reference is
9250 pointing to a const or volatile object, i.e., the
9251 `optr' is const or volatile, respectively, not the
9252 signature pointer/reference itself. */
9253 if (! IS_SIGNATURE (type))
9254 {
f376e137 9255 type = cp_build_type_variant (type, constp, volatilep);
8d08fdba
MS
9256 if (IS_AGGR_TYPE (type))
9257 build_pointer_type (type);
9258 constp = 0;
9259 volatilep = 0;
9260 }
9261 }
9262
9263 if (IS_SIGNATURE (type))
9264 {
9265 if (TREE_CODE (declarator) == ADDR_EXPR)
9266 {
63718c49
GB
9267 if (CLASSTYPE_METHOD_VEC (type) == NULL_TREE
9268 && TYPE_SIZE (type))
9269 cp_warning ("empty signature `%T' used in signature reference declaration",
9270 type);
8d08fdba
MS
9271#if 0
9272 type = build_signature_reference_type (type,
9273 constp, volatilep);
9274#else
9275 sorry ("signature reference");
9276 return NULL_TREE;
9277#endif
9278 }
9279 else
9280 {
63718c49
GB
9281 if (CLASSTYPE_METHOD_VEC (type) == NULL_TREE
9282 && TYPE_SIZE (type))
9283 cp_warning ("empty signature `%T' used in signature pointer declaration",
9284 type);
8d08fdba
MS
9285 type = build_signature_pointer_type (type,
9286 constp, volatilep);
9287 }
9288 constp = 0;
9289 volatilep = 0;
9290 }
9291 else if (TREE_CODE (declarator) == ADDR_EXPR)
9292 {
9293 if (TREE_CODE (type) == FUNCTION_TYPE)
9294 {
9295 error ("cannot declare references to functions; use pointer to function instead");
9296 type = build_pointer_type (type);
9297 }
9298 else
9299 {
a1774733 9300 if (TREE_CODE (type) == VOID_TYPE)
8d08fdba
MS
9301 error ("invalid type: `void &'");
9302 else
9303 type = build_reference_type (type);
9304 }
9305 }
9306 else if (TREE_CODE (type) == METHOD_TYPE)
9307 {
9308 type = build_ptrmemfunc_type (build_pointer_type (type));
9309 }
9310 else
9311 type = build_pointer_type (type);
9312
9313 /* Process a list of type modifier keywords (such as
9314 const or volatile) that were given inside the `*' or `&'. */
9315
9316 if (TREE_TYPE (declarator))
9317 {
9318 register tree typemodlist;
9319 int erred = 0;
9320 for (typemodlist = TREE_TYPE (declarator); typemodlist;
9321 typemodlist = TREE_CHAIN (typemodlist))
9322 {
9323 if (TREE_VALUE (typemodlist) == ridpointers[(int) RID_CONST])
9324 constp++;
9325 else if (TREE_VALUE (typemodlist) == ridpointers[(int) RID_VOLATILE])
9326 volatilep++;
9327 else if (!erred)
9328 {
9329 erred = 1;
9330 error ("invalid type modifier within %s declarator",
9331 TREE_CODE (declarator) == ADDR_EXPR
9332 ? "reference" : "pointer");
9333 }
9334 }
9335 if (constp > 1)
a0a33927 9336 pedwarn ("duplicate `const'");
8d08fdba 9337 if (volatilep > 1)
a0a33927
MS
9338 pedwarn ("duplicate `volatile'");
9339 if (TREE_CODE (declarator) == ADDR_EXPR
9340 && (constp || volatilep))
9341 {
9342 if (constp)
e1cd6e56 9343 pedwarn ("discarding `const' applied to a reference");
a0a33927 9344 if (volatilep)
e1cd6e56 9345 pedwarn ("discarding `volatile' applied to a reference");
a0a33927
MS
9346 constp = volatilep = 0;
9347 }
8d08fdba
MS
9348 }
9349 declarator = TREE_OPERAND (declarator, 0);
9350 ctype = NULL_TREE;
9351 break;
9352
9353 case SCOPE_REF:
9354 {
9355 /* We have converted type names to NULL_TREE if the
9356 name was bogus, or to a _TYPE node, if not.
9357
9358 The variable CTYPE holds the type we will ultimately
9359 resolve to. The code here just needs to build
9360 up appropriate member types. */
9361 tree sname = TREE_OPERAND (declarator, 1);
386b8a85
JM
9362 tree t;
9363
8d08fdba
MS
9364 /* Destructors can have their visibilities changed as well. */
9365 if (TREE_CODE (sname) == BIT_NOT_EXPR)
9366 sname = TREE_OPERAND (sname, 0);
9367
9368 if (TREE_COMPLEXITY (declarator) == 0)
9369 /* This needs to be here, in case we are called
9370 multiple times. */ ;
9371 else if (friendp && (TREE_COMPLEXITY (declarator) < 2))
2c73f9f5 9372 /* Don't fall out into global scope. Hides real bug? --eichin */ ;
5566b478
MS
9373 else if (! IS_AGGR_TYPE_CODE
9374 (TREE_CODE (TREE_OPERAND (declarator, 0))))
9375 ;
8d08fdba
MS
9376 else if (TREE_COMPLEXITY (declarator) == current_class_depth)
9377 {
9a3b49ac
MS
9378 /* Resolve any TYPENAME_TYPEs from the decl-specifier-seq
9379 that refer to ctype. They couldn't be resolved earlier
9380 because we hadn't pushed into the class yet.
9381 Example: resolve 'B<T>::type' in
9382 'B<typename B<T>::type> B<T>::f () { }'. */
9383 if (current_template_parms
9384 && uses_template_parms (type)
9385 && uses_template_parms (current_class_type))
9386 {
9387 tree args = current_template_args ();
f7d98d58 9388 type = tsubst (type, args, NULL_TREE);
9a3b49ac
MS
9389 }
9390
a28e3c7f
MS
9391 /* This pop_nested_class corresponds to the
9392 push_nested_class used to push into class scope for
9393 parsing the argument list of a function decl, in
9394 qualified_id. */
8d08fdba
MS
9395 pop_nested_class (1);
9396 TREE_COMPLEXITY (declarator) = current_class_depth;
9397 }
9398 else
9399 my_friendly_abort (16);
9400
9401 if (TREE_OPERAND (declarator, 0) == NULL_TREE)
9402 {
9403 /* We had a reference to a global decl, or
9404 perhaps we were given a non-aggregate typedef,
9405 in which case we cleared this out, and should just
9406 keep going as though it wasn't there. */
9407 declarator = sname;
9408 continue;
9409 }
9410 ctype = TREE_OPERAND (declarator, 0);
9411
386b8a85
JM
9412 t = ctype;
9413 while (t != NULL_TREE)
9414 {
75650646
MM
9415 if (CLASSTYPE_TEMPLATE_INFO (t) &&
9416 !CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
386b8a85
JM
9417 template_count += 1;
9418 t = TYPE_MAIN_DECL (t);
9419 if (DECL_LANG_SPECIFIC (t))
9420 t = DECL_CLASS_CONTEXT (t);
9421 else
9422 t = NULL_TREE;
9423 }
9424
8d08fdba
MS
9425 if (sname == NULL_TREE)
9426 goto done_scoping;
9427
9428 if (TREE_CODE (sname) == IDENTIFIER_NODE)
9429 {
9430 /* This is the `standard' use of the scoping operator:
9431 basetype :: member . */
9432
db5ae43f 9433 if (ctype == current_class_type)
28cbf42c
MS
9434 {
9435 /* class A {
9436 void A::f ();
9437 };
9438
9439 Is this ill-formed? */
9440
9441 if (pedantic)
9442 cp_pedwarn ("extra qualification `%T::' on member `%s' ignored",
9443 ctype, name);
9444 }
db5ae43f 9445 else if (TREE_CODE (type) == FUNCTION_TYPE)
8d08fdba
MS
9446 {
9447 if (current_class_type == NULL_TREE
8d08fdba
MS
9448 || friendp)
9449 type = build_cplus_method_type (build_type_variant (ctype, constp, volatilep),
9450 TREE_TYPE (type), TYPE_ARG_TYPES (type));
9451 else
9452 {
5b605f68 9453 cp_error ("cannot declare member function `%T::%s' within `%T'",
a3203465 9454 ctype, name, current_class_type);
8d08fdba
MS
9455 return void_type_node;
9456 }
9457 }
5566b478
MS
9458 else if (RIDBIT_SETP (RID_TYPEDEF, specbits)
9459 || TYPE_SIZE (complete_type (ctype)) != NULL_TREE)
8d08fdba 9460 {
2c73f9f5 9461 /* Have to move this code elsewhere in this function.
db5ae43f
MS
9462 this code is used for i.e., typedef int A::M; M *pm;
9463
9464 It is? How? jason 10/2/94 */
8d08fdba 9465
8d08fdba
MS
9466 if (current_class_type)
9467 {
db5ae43f
MS
9468 cp_error ("cannot declare member `%T::%s' within `%T'",
9469 ctype, name, current_class_type);
9470 return void_type_node;
8d08fdba
MS
9471 }
9472 type = build_offset_type (ctype, type);
9473 }
9474 else if (uses_template_parms (ctype))
9475 {
8d08fdba 9476 if (TREE_CODE (type) == FUNCTION_TYPE)
beb53fb8
JM
9477 type
9478 = build_cplus_method_type (build_type_variant (ctype,
9479 constp,
9480 volatilep),
9481 TREE_TYPE (type),
9482 TYPE_ARG_TYPES (type));
8d08fdba
MS
9483 }
9484 else
9485 {
9486 cp_error ("structure `%T' not yet defined", ctype);
9487 return error_mark_node;
9488 }
9489
9490 declarator = sname;
9491 }
8d08fdba
MS
9492 else if (TREE_CODE (sname) == SCOPE_REF)
9493 my_friendly_abort (17);
9494 else
9495 {
9496 done_scoping:
9497 declarator = TREE_OPERAND (declarator, 1);
9498 if (declarator && TREE_CODE (declarator) == CALL_EXPR)
9499 /* In this case, we will deal with it later. */
9500 ;
9501 else
9502 {
9503 if (TREE_CODE (type) == FUNCTION_TYPE)
9504 type = build_cplus_method_type (build_type_variant (ctype, constp, volatilep), TREE_TYPE (type), TYPE_ARG_TYPES (type));
9505 else
9506 type = build_offset_type (ctype, type);
9507 }
9508 }
9509 }
9510 break;
9511
9512 case BIT_NOT_EXPR:
9513 declarator = TREE_OPERAND (declarator, 0);
9514 break;
9515
8d08fdba
MS
9516 case RECORD_TYPE:
9517 case UNION_TYPE:
9518 case ENUMERAL_TYPE:
9519 declarator = NULL_TREE;
9520 break;
9521
9522 case ERROR_MARK:
9523 declarator = NULL_TREE;
9524 break;
9525
9526 default:
9527 my_friendly_abort (158);
9528 }
9529 }
9530
db5ae43f
MS
9531 if (explicitp == 1)
9532 {
9533 error ("only constructors can be declared `explicit'");
9534 explicitp = 0;
9535 }
9536
8d08fdba
MS
9537 /* Now TYPE has the actual type. */
9538
9539 /* If this is declaring a typedef name, return a TYPE_DECL. */
9540
f30432d7
MS
9541 if (RIDBIT_SETP (RID_MUTABLE, specbits))
9542 {
9543 if (constp)
9544 {
9545 error ("const `%s' cannot be declared `mutable'", name);
9546 RIDBIT_RESET (RID_MUTABLE, specbits);
9547 }
9548 else if (staticp)
9549 {
9550 error ("static `%s' cannot be declared `mutable'", name);
9551 RIDBIT_RESET (RID_MUTABLE, specbits);
9552 }
9553 }
9554
fc378698 9555 if (RIDBIT_SETP (RID_TYPEDEF, specbits) && decl_context != TYPENAME)
8d08fdba
MS
9556 {
9557 tree decl;
9558
9559 /* Note that the grammar rejects storage classes
9560 in typenames, fields or parameters. */
9561 if (constp || volatilep)
f376e137 9562 type = cp_build_type_variant (type, constp, volatilep);
8d08fdba 9563
d2e5ee5c
MS
9564 if (decl_context == FIELD)
9565 {
9566 if (declarator == current_class_name)
9567 cp_pedwarn ("ANSI C++ forbids nested type `%D' with same name as enclosing class",
9568 declarator);
9569 decl = build_lang_decl (TYPE_DECL, declarator, type);
9570 if (IS_SIGNATURE (current_class_type) && opaque_typedef)
9571 SIGNATURE_HAS_OPAQUE_TYPEDECLS (current_class_type) = 1;
9572 }
a1774733
BK
9573 else if (current_lang_name == lang_name_java)
9574 decl = build_lang_decl (TYPE_DECL, declarator, type);
d2e5ee5c
MS
9575 else
9576 decl = build_decl (TYPE_DECL, declarator, type);
9577
8d08fdba
MS
9578 /* If the user declares "struct {...} foo" then `foo' will have
9579 an anonymous name. Fill that name in now. Nothing can
9580 refer to it, so nothing needs know about the name change.
9581 The TYPE_NAME field was filled in by build_struct_xref. */
9582 if (type != error_mark_node
9583 && TYPE_NAME (type)
9584 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
9585 && ANON_AGGRNAME_P (TYPE_IDENTIFIER (type)))
9586 {
2c73f9f5 9587 /* Replace the anonymous name with the real name everywhere. */
8d08fdba 9588 lookup_tag_reverse (type, declarator);
d2e5ee5c 9589 TYPE_NAME (type) = decl;
8d08fdba
MS
9590
9591 if (TYPE_LANG_SPECIFIC (type))
9592 TYPE_WAS_ANONYMOUS (type) = 1;
9593
2c73f9f5
ML
9594 /* XXX Temporarily set the scope.
9595 When returning, start_decl expects it as NULL_TREE,
9596 and will then then set it using pushdecl. */
9597 my_friendly_assert (DECL_CONTEXT (decl) == NULL_TREE, 980404);
9598 if (current_class_type)
9599 DECL_CONTEXT (decl) = current_class_type;
9600 else
9601 DECL_CONTEXT (decl) = current_namespace;
9602
d2e5ee5c
MS
9603 DECL_ASSEMBLER_NAME (decl) = DECL_NAME (decl);
9604 DECL_ASSEMBLER_NAME (decl)
9605 = get_identifier (build_overload_name (type, 1, 1));
2c73f9f5 9606 DECL_CONTEXT (decl) = NULL_TREE;
fc378698 9607 }
fc378698 9608
8d08fdba
MS
9609 if (TREE_CODE (type) == OFFSET_TYPE || TREE_CODE (type) == METHOD_TYPE)
9610 {
9611 cp_error_at ("typedef name may not be class-qualified", decl);
a3203465 9612 return NULL_TREE;
8d08fdba
MS
9613 }
9614 else if (quals)
9615 {
9616 if (ctype == NULL_TREE)
9617 {
9618 if (TREE_CODE (type) != METHOD_TYPE)
9619 cp_error_at ("invalid type qualifier for non-method type", decl);
9620 else
9621 ctype = TYPE_METHOD_BASETYPE (type);
9622 }
9623 if (ctype != NULL_TREE)
9624 grok_method_quals (ctype, decl, quals);
9625 }
9626
9627 if (RIDBIT_SETP (RID_SIGNED, specbits)
9628 || (typedef_decl && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl)))
9629 C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
9630
9631 if (RIDBIT_SETP (RID_MUTABLE, specbits))
9632 {
9633 error ("non-object member `%s' cannot be declared mutable", name);
9634 }
9635
c91a56d2
MS
9636 bad_specifiers (decl, "type", virtualp, quals != NULL_TREE,
9637 inlinep, friendp, raises != NULL_TREE);
9638
9639 if (initialized)
9640 error ("typedef declaration includes an initializer");
9641
8d08fdba
MS
9642 return decl;
9643 }
9644
9645 /* Detect the case of an array type of unspecified size
9646 which came, as such, direct from a typedef name.
9647 We must copy the type, so that each identifier gets
9648 a distinct type, so that each identifier's size can be
9649 controlled separately by its own initializer. */
9650
9651 if (type == typedef_type && TREE_CODE (type) == ARRAY_TYPE
9652 && TYPE_DOMAIN (type) == NULL_TREE)
9653 {
9654 type = build_cplus_array_type (TREE_TYPE (type), TYPE_DOMAIN (type));
9655 }
9656
9657 /* If this is a type name (such as, in a cast or sizeof),
9658 compute the type and return it now. */
9659
9660 if (decl_context == TYPENAME)
9661 {
9662 /* Note that the grammar rejects storage classes
9663 in typenames, fields or parameters. */
9664 if (constp || volatilep)
a703fb38
KG
9665 {
9666 if (IS_SIGNATURE (type))
9667 error ("`const' or `volatile' specified with signature type");
9668 else
9669 type = cp_build_type_variant (type, constp, volatilep);
9670 }
8d08fdba
MS
9671
9672 /* Special case: "friend class foo" looks like a TYPENAME context. */
9673 if (friendp)
9674 {
b7484fbe
MS
9675 if (volatilep)
9676 {
9677 cp_error ("`volatile' specified for friend class declaration");
9678 volatilep = 0;
9679 }
9680 if (inlinep)
9681 {
9682 cp_error ("`inline' specified for friend class declaration");
9683 inlinep = 0;
9684 }
9685
9686 /* Only try to do this stuff if we didn't already give up. */
9687 if (type != integer_type_node)
9688 {
9689 /* A friendly class? */
9690 if (current_class_type)
9691 make_friend_class (current_class_type, TYPE_MAIN_VARIANT (type));
9692 else
9693 error ("trying to make class `%s' a friend of global scope",
9694 TYPE_NAME_STRING (type));
9695 type = void_type_node;
9696 }
8d08fdba
MS
9697 }
9698 else if (quals)
9699 {
8d08fdba 9700 tree dummy = build_decl (TYPE_DECL, declarator, type);
8d08fdba
MS
9701 if (ctype == NULL_TREE)
9702 {
9703 my_friendly_assert (TREE_CODE (type) == METHOD_TYPE, 159);
9704 ctype = TYPE_METHOD_BASETYPE (type);
9705 }
9706 grok_method_quals (ctype, dummy, quals);
9707 type = TREE_TYPE (dummy);
9708 }
9709
9710 return type;
9711 }
9712 else if (declarator == NULL_TREE && decl_context != PARM
db5ae43f 9713 && decl_context != CATCHPARM
8d08fdba
MS
9714 && TREE_CODE (type) != UNION_TYPE
9715 && ! bitfield)
9716 {
9717 cp_error ("abstract declarator `%T' used as declaration", type);
9718 declarator = make_anon_name ();
9719 }
9720
9721 /* `void' at top level (not within pointer)
9722 is allowed only in typedefs or type names.
9723 We don't complain about parms either, but that is because
9724 a better error message can be made later. */
9725
a1774733 9726 if (TREE_CODE (type) == VOID_TYPE && decl_context != PARM)
8d08fdba 9727 {
b7484fbe
MS
9728 if (! declarator)
9729 error ("unnamed variable or field declared void");
9730 else if (TREE_CODE (declarator) == IDENTIFIER_NODE)
8d08fdba
MS
9731 {
9732 if (IDENTIFIER_OPNAME_P (declarator))
8d08fdba 9733 my_friendly_abort (356);
8d08fdba
MS
9734 else
9735 error ("variable or field `%s' declared void", name);
9736 }
9737 else
9738 error ("variable or field declared void");
9739 type = integer_type_node;
9740 }
9741
9742 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
9743 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
9744
9745 {
9746 register tree decl;
9747
9748 if (decl_context == PARM)
9749 {
8d08fdba
MS
9750 if (ctype)
9751 error ("cannot use `::' in parameter declaration");
9752
9753 /* A parameter declared as an array of T is really a pointer to T.
9754 One declared as a function is really a pointer to a function.
39211cd5 9755 One declared as a member is really a pointer to member. */
8d08fdba
MS
9756
9757 if (TREE_CODE (type) == ARRAY_TYPE)
9758 {
e92cc029 9759 /* Transfer const-ness of array into that of type pointed to. */
39211cd5 9760 type = build_pointer_type
f376e137 9761 (cp_build_type_variant (TREE_TYPE (type), constp, volatilep));
39211cd5 9762 volatilep = constp = 0;
8d08fdba
MS
9763 }
9764 else if (TREE_CODE (type) == FUNCTION_TYPE)
9765 type = build_pointer_type (type);
9766 else if (TREE_CODE (type) == OFFSET_TYPE)
9767 type = build_pointer_type (type);
a1774733 9768 else if (TREE_CODE (type) == VOID_TYPE && declarator)
b7484fbe
MS
9769 {
9770 error ("declaration of `%s' as void", name);
9771 return NULL_TREE;
9772 }
8d08fdba 9773
e349ee73 9774 decl = build_decl (PARM_DECL, declarator, complete_type (type));
8d08fdba
MS
9775
9776 bad_specifiers (decl, "parameter", virtualp, quals != NULL_TREE,
9777 inlinep, friendp, raises != NULL_TREE);
9778 if (current_class_type
9779 && IS_SIGNATURE (current_class_type))
9780 {
9781 if (inlinep)
9782 error ("parameter of signature member function declared `inline'");
9783 if (RIDBIT_SETP (RID_AUTO, specbits))
9784 error ("parameter of signature member function declared `auto'");
9785 if (RIDBIT_SETP (RID_REGISTER, specbits))
9786 error ("parameter of signature member function declared `register'");
9787 }
9788
9789 /* Compute the type actually passed in the parmlist,
9790 for the case where there is no prototype.
9791 (For example, shorts and chars are passed as ints.)
9792 When there is a prototype, this is overridden later. */
9793
39211cd5 9794 DECL_ARG_TYPE (decl) = type_promotes_to (type);
8d08fdba
MS
9795 }
9796 else if (decl_context == FIELD)
9797 {
9798 if (type == error_mark_node)
9799 {
9800 /* Happens when declaring arrays of sizes which
9801 are error_mark_node, for example. */
9802 decl = NULL_TREE;
9803 }
9804 else if (TREE_CODE (type) == FUNCTION_TYPE)
9805 {
9806 int publicp = 0;
e1467ff2 9807 tree function_context;
8d08fdba 9808
72b7eeff
MS
9809 /* We catch the others as conflicts with the builtin
9810 typedefs. */
9811 if (friendp && declarator == ridpointers[(int) RID_SIGNED])
9812 {
9813 cp_error ("function `%D' cannot be declared friend",
9814 declarator);
9815 friendp = 0;
9816 }
9817
8d08fdba
MS
9818 if (friendp == 0)
9819 {
9820 if (ctype == NULL_TREE)
9821 ctype = current_class_type;
9822
9823 if (ctype == NULL_TREE)
9824 {
9825 cp_error ("can't make `%D' into a method -- not in a class",
9826 declarator);
9827 return void_type_node;
9828 }
9829
9830 /* ``A union may [ ... ] not [ have ] virtual functions.''
9831 ARM 9.5 */
9832 if (virtualp && TREE_CODE (ctype) == UNION_TYPE)
9833 {
9834 cp_error ("function `%D' declared virtual inside a union",
9835 declarator);
9836 return void_type_node;
9837 }
9838
9839 if (declarator == ansi_opname[(int) NEW_EXPR]
a28e3c7f
MS
9840 || declarator == ansi_opname[(int) VEC_NEW_EXPR]
9841 || declarator == ansi_opname[(int) DELETE_EXPR]
9842 || declarator == ansi_opname[(int) VEC_DELETE_EXPR])
8d08fdba
MS
9843 {
9844 if (virtualp)
9845 {
9846 cp_error ("`%D' cannot be declared virtual, since it is always static",
9847 declarator);
9848 virtualp = 0;
9849 }
9850 }
9851 else if (staticp < 2)
9852 type = build_cplus_method_type (build_type_variant (ctype, constp, volatilep),
9853 TREE_TYPE (type), TYPE_ARG_TYPES (type));
9854 }
9855
9856 /* Tell grokfndecl if it needs to set TREE_PUBLIC on the node. */
e1467ff2
MM
9857 function_context = (ctype != NULL_TREE) ?
9858 hack_decl_function_context (TYPE_MAIN_DECL (ctype)) : NULL_TREE;
9859 publicp = (! friendp || ! staticp)
9860 && function_context == NULL_TREE;
386b8a85
JM
9861 decl = grokfndecl (ctype, type,
9862 TREE_CODE (declarator) != TEMPLATE_ID_EXPR
9863 ? declarator : dname,
9864 declarator,
f30432d7 9865 virtualp, flags, quals, raises, attrlist,
386b8a85 9866 friendp ? -1 : 0, friendp, publicp, inlinep,
2c73f9f5 9867 funcdef_flag, template_count, in_namespace);
f0e01782
MS
9868 if (decl == NULL_TREE)
9869 return NULL_TREE;
6125f3be
DE
9870#if 0
9871 /* This clobbers the attrs stored in `decl' from `attrlist'. */
9872 /* The decl and setting of decl_machine_attr is also turned off. */
71851aaa 9873 decl = build_decl_attribute_variant (decl, decl_machine_attr);
6125f3be 9874#endif
f0e01782 9875
db5ae43f
MS
9876 if (explicitp == 2)
9877 DECL_NONCONVERTING_P (decl) = 1;
8d08fdba
MS
9878 }
9879 else if (TREE_CODE (type) == METHOD_TYPE)
9880 {
faae18ab
MS
9881 /* We only get here for friend declarations of
9882 members of other classes. */
8d08fdba
MS
9883 /* All method decls are public, so tell grokfndecl to set
9884 TREE_PUBLIC, also. */
386b8a85 9885 decl = grokfndecl (ctype, type, declarator, declarator,
f30432d7 9886 virtualp, flags, quals, raises, attrlist,
386b8a85 9887 friendp ? -1 : 0, friendp, 1, 0, funcdef_flag,
2c73f9f5 9888 template_count, in_namespace);
f0e01782
MS
9889 if (decl == NULL_TREE)
9890 return NULL_TREE;
8d08fdba 9891 }
5566b478
MS
9892 else if (!staticp && ! processing_template_decl
9893 && TYPE_SIZE (complete_type (type)) == NULL_TREE
8d08fdba
MS
9894 && (TREE_CODE (type) != ARRAY_TYPE || initialized == 0))
9895 {
b7484fbe
MS
9896 if (declarator)
9897 cp_error ("field `%D' has incomplete type", declarator);
9898 else
9899 cp_error ("name `%T' has incomplete type", type);
8d08fdba
MS
9900
9901 /* If we're instantiating a template, tell them which
9902 instantiation made the field's type be incomplete. */
9903 if (current_class_type
9904 && TYPE_NAME (current_class_type)
d2e5ee5c 9905 && IDENTIFIER_TEMPLATE (TYPE_IDENTIFIER (current_class_type))
8d08fdba
MS
9906 && declspecs && TREE_VALUE (declspecs)
9907 && TREE_TYPE (TREE_VALUE (declspecs)) == type)
db5ae43f
MS
9908 cp_error (" in instantiation of template `%T'",
9909 current_class_type);
9910
8d08fdba
MS
9911 type = error_mark_node;
9912 decl = NULL_TREE;
9913 }
9914 else
9915 {
9916 if (friendp)
9917 {
9918 error ("`%s' is neither function nor method; cannot be declared friend",
9919 IDENTIFIER_POINTER (declarator));
9920 friendp = 0;
9921 }
9922 decl = NULL_TREE;
9923 }
9924
9925 if (friendp)
9926 {
9927 /* Friends are treated specially. */
9928 if (ctype == current_class_type)
9929 warning ("member functions are implicitly friends of their class");
9930 else
9931 {
9932 tree t = NULL_TREE;
9933 if (decl && DECL_NAME (decl))
9934 t = do_friend (ctype, declarator, decl,
5566b478
MS
9935 last_function_parms, flags, quals,
9936 funcdef_flag);
8d08fdba
MS
9937 if (t && funcdef_flag)
9938 return t;
9939
9940 return void_type_node;
9941 }
9942 }
9943
9944 /* Structure field. It may not be a function, except for C++ */
9945
9946 if (decl == NULL_TREE)
9947 {
8d08fdba
MS
9948 if (initialized)
9949 {
3ac3d9ea
MM
9950 if (!staticp)
9951 {
9952 /* An attempt is being made to initialize a non-static
9953 member. But, from [class.mem]:
9954
9955 4 A member-declarator can contain a
9956 constant-initializer only if it declares a static
9957 member (_class.static_) of integral or enumeration
9958 type, see _class.static.data_.
9959
9960 This used to be relatively common practice, but
9961 the rest of the compiler does not correctly
9962 handle the initialization unless the member is
9963 static so we make it static below. */
9964 cp_pedwarn ("ANSI C++ forbids initialization of %s `%D'",
9965 constp ? "const member" : "member",
9966 declarator);
9967 cp_pedwarn ("making `%D' static", declarator);
9968 staticp = 1;
9969 }
9970
a0a33927
MS
9971 /* Motion 10 at San Diego: If a static const integral data
9972 member is initialized with an integral constant
9973 expression, the initializer may appear either in the
9974 declaration (within the class), or in the definition,
9975 but not both. If it appears in the class, the member is
9976 a member constant. The file-scope definition is always
9977 required. */
3ac3d9ea
MM
9978 if (! constp)
9979 /* According to Mike Stump, we generate bad code for
9980 this case, so we might as well always make it an
9981 error. */
9982 cp_error ("ANSI C++ forbids in-class initialization of non-const static member `%D'",
9983 declarator);
9984
9985 if (pedantic && ! INTEGRAL_TYPE_P (type))
9986 cp_pedwarn ("ANSI C++ forbids initialization of member constant `%D' of non-integral type `%T'", declarator, type);
8d08fdba
MS
9987 }
9988
3ac3d9ea 9989 if (staticp)
8d08fdba 9990 {
f30432d7 9991 /* ANSI C++ Apr '95 wp 9.2 */
3ac3d9ea 9992 if (declarator == current_class_name)
f30432d7
MS
9993 cp_pedwarn ("ANSI C++ forbids static member `%D' with same name as enclosing class",
9994 declarator);
9995
8d08fdba
MS
9996 /* C++ allows static class members.
9997 All other work for this is done by grokfield.
9998 This VAR_DECL is built by build_lang_field_decl.
9999 All other VAR_DECLs are built by build_decl. */
10000 decl = build_lang_field_decl (VAR_DECL, declarator, type);
5b605f68
MS
10001 TREE_STATIC (decl) = 1;
10002 /* In class context, 'static' means public access. */
3ac3d9ea 10003 TREE_PUBLIC (decl) = DECL_EXTERNAL (decl) = 1;
8d08fdba
MS
10004 }
10005 else
10006 {
10007 decl = build_lang_field_decl (FIELD_DECL, declarator, type);
10008 if (RIDBIT_SETP (RID_MUTABLE, specbits))
10009 {
10010 DECL_MUTABLE_P (decl) = 1;
10011 RIDBIT_RESET (RID_MUTABLE, specbits);
10012 }
10013 }
10014
10015 bad_specifiers (decl, "field", virtualp, quals != NULL_TREE,
10016 inlinep, friendp, raises != NULL_TREE);
10017 }
10018 }
10019 else if (TREE_CODE (type) == FUNCTION_TYPE || TREE_CODE (type) == METHOD_TYPE)
10020 {
386b8a85 10021 tree original_name;
8d08fdba
MS
10022 int publicp = 0;
10023
10024 if (! declarator)
10025 return NULL_TREE;
10026
386b8a85
JM
10027 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
10028 original_name = dname;
10029 else
10030 original_name = declarator;
10031
8926095f
MS
10032 if (RIDBIT_SETP (RID_AUTO, specbits))
10033 error ("storage class `auto' invalid for function `%s'", name);
10034 else if (RIDBIT_SETP (RID_REGISTER, specbits))
10035 error ("storage class `register' invalid for function `%s'", name);
8d08fdba
MS
10036
10037 /* Function declaration not at top level.
10038 Storage classes other than `extern' are not allowed
10039 and `extern' makes no difference. */
a9aedbc2 10040 if (! toplevel_bindings_p ()
8926095f
MS
10041 && (RIDBIT_SETP (RID_STATIC, specbits)
10042 || RIDBIT_SETP (RID_INLINE, specbits))
8d08fdba 10043 && pedantic)
8926095f
MS
10044 {
10045 if (RIDBIT_SETP (RID_STATIC, specbits))
10046 pedwarn ("storage class `static' invalid for function `%s' declared out of global scope", name);
10047 else
10048 pedwarn ("storage class `inline' invalid for function `%s' declared out of global scope", name);
10049 }
10050
8d08fdba
MS
10051 if (ctype == NULL_TREE)
10052 {
10053 if (virtualp)
10054 {
10055 error ("virtual non-class function `%s'", name);
10056 virtualp = 0;
10057 }
10058
10059 if (current_lang_name == lang_name_cplusplus
5156628f 10060 && ! processing_template_decl
8d08fdba
MS
10061 && ! (IDENTIFIER_LENGTH (original_name) == 4
10062 && IDENTIFIER_POINTER (original_name)[0] == 'm'
10063 && strcmp (IDENTIFIER_POINTER (original_name), "main") == 0)
10064 && ! (IDENTIFIER_LENGTH (original_name) > 10
10065 && IDENTIFIER_POINTER (original_name)[0] == '_'
10066 && IDENTIFIER_POINTER (original_name)[1] == '_'
10067 && strncmp (IDENTIFIER_POINTER (original_name)+2, "builtin_", 8) == 0))
8926095f 10068 /* Plain overloading: will not be grok'd by grokclassfn. */
386b8a85
JM
10069 if (name_mangling_version < 1
10070 || TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
10071 declarator = build_decl_overload (dname, TYPE_ARG_TYPES (type), 0);
8d08fdba
MS
10072 }
10073 else if (TREE_CODE (type) == FUNCTION_TYPE && staticp < 2)
10074 type = build_cplus_method_type (build_type_variant (ctype, constp, volatilep),
10075 TREE_TYPE (type), TYPE_ARG_TYPES (type));
10076
eb66be0e 10077 /* Record presence of `static'. */
faae18ab 10078 publicp = (ctype != NULL_TREE
a9aedbc2 10079 || RIDBIT_SETP (RID_EXTERN, specbits)
eb66be0e 10080 || !RIDBIT_SETP (RID_STATIC, specbits));
8d08fdba 10081
386b8a85 10082 decl = grokfndecl (ctype, type, original_name, declarator,
f30432d7 10083 virtualp, flags, quals, raises, attrlist,
75650646 10084 1, friendp,
386b8a85 10085 publicp, inlinep, funcdef_flag,
2c73f9f5 10086 template_count, in_namespace);
f0e01782
MS
10087 if (decl == NULL_TREE)
10088 return NULL_TREE;
8d08fdba 10089
386b8a85 10090 if (ctype == NULL_TREE && DECL_LANGUAGE (decl) != lang_c
71cc65bd 10091 && (! DECL_USE_TEMPLATE (decl) ||
386b8a85 10092 name_mangling_version < 1))
30394414 10093 DECL_ASSEMBLER_NAME (decl) = declarator;
386b8a85 10094
8d08fdba
MS
10095 if (staticp == 1)
10096 {
10097 int illegal_static = 0;
10098
10099 /* Don't allow a static member function in a class, and forbid
10100 declaring main to be static. */
10101 if (TREE_CODE (type) == METHOD_TYPE)
10102 {
00595019 10103 cp_pedwarn ("cannot declare member function `%D' to have static linkage", decl);
8d08fdba
MS
10104 illegal_static = 1;
10105 }
8d08fdba
MS
10106 else if (current_function_decl)
10107 {
10108 /* FIXME need arm citation */
10109 error ("cannot declare static function inside another function");
10110 illegal_static = 1;
10111 }
10112
10113 if (illegal_static)
10114 {
10115 staticp = 0;
10116 RIDBIT_RESET (RID_STATIC, specbits);
10117 }
10118 }
8d08fdba
MS
10119 }
10120 else
10121 {
10122 /* It's a variable. */
10123
db5ae43f
MS
10124 if (decl_context == CATCHPARM)
10125 {
10126 if (ctype)
10127 {
10128 ctype = NULL_TREE;
10129 error ("cannot use `::' in parameter declaration");
10130 }
10131
10132 /* A parameter declared as an array of T is really a pointer to T.
10133 One declared as a function is really a pointer to a function.
10134 One declared as a member is really a pointer to member. */
10135
10136 if (TREE_CODE (type) == ARRAY_TYPE)
10137 {
e92cc029
MS
10138 /* Transfer const-ness of array into that of type
10139 pointed to. */
db5ae43f
MS
10140 type = build_pointer_type
10141 (cp_build_type_variant (TREE_TYPE (type), constp, volatilep));
10142 volatilep = constp = 0;
10143 }
10144 else if (TREE_CODE (type) == FUNCTION_TYPE)
10145 type = build_pointer_type (type);
10146 else if (TREE_CODE (type) == OFFSET_TYPE)
10147 type = build_pointer_type (type);
10148 }
10149
8d08fdba 10150 /* An uninitialized decl with `extern' is a reference. */
2c73f9f5
ML
10151 decl = grokvardecl (type, declarator, &specbits,
10152 initialized, constp, in_namespace);
8d08fdba
MS
10153 bad_specifiers (decl, "variable", virtualp, quals != NULL_TREE,
10154 inlinep, friendp, raises != NULL_TREE);
10155
10156 if (ctype)
10157 {
f0e01782 10158 DECL_CONTEXT (decl) = ctype;
8d08fdba
MS
10159 if (staticp == 1)
10160 {
f30432d7 10161 cp_pedwarn ("static member `%D' re-declared as static", decl);
8d08fdba
MS
10162 staticp = 0;
10163 RIDBIT_RESET (RID_STATIC, specbits);
10164 }
b7484fbe
MS
10165 if (RIDBIT_SETP (RID_REGISTER, specbits) && TREE_STATIC (decl))
10166 {
10167 cp_error ("static member `%D' declared `register'", decl);
10168 RIDBIT_RESET (RID_REGISTER, specbits);
10169 }
f30432d7 10170 if (RIDBIT_SETP (RID_EXTERN, specbits) && pedantic)
8d08fdba 10171 {
f30432d7
MS
10172 cp_pedwarn ("cannot explicitly declare member `%#D' to have extern linkage",
10173 decl);
8d08fdba
MS
10174 RIDBIT_RESET (RID_EXTERN, specbits);
10175 }
10176 }
10177 }
10178
10179 if (RIDBIT_SETP (RID_MUTABLE, specbits))
10180 {
10181 error ("`%s' cannot be declared mutable", name);
10182 }
10183
10184 /* Record `register' declaration for warnings on &
10185 and in case doing stupid register allocation. */
10186
10187 if (RIDBIT_SETP (RID_REGISTER, specbits))
10188 DECL_REGISTER (decl) = 1;
10189
8926095f
MS
10190 if (RIDBIT_SETP (RID_EXTERN, specbits))
10191 DECL_THIS_EXTERN (decl) = 1;
10192
faae18ab
MS
10193 if (RIDBIT_SETP (RID_STATIC, specbits))
10194 DECL_THIS_STATIC (decl) = 1;
10195
8d08fdba
MS
10196 /* Record constancy and volatility. */
10197
10198 if (constp)
10199 TREE_READONLY (decl) = TREE_CODE (type) != REFERENCE_TYPE;
10200 if (volatilep)
10201 {
10202 TREE_SIDE_EFFECTS (decl) = 1;
10203 TREE_THIS_VOLATILE (decl) = 1;
10204 }
10205
10206 return decl;
10207 }
10208}
10209\f
10210/* Tell if a parmlist/exprlist looks like an exprlist or a parmlist.
10211 An empty exprlist is a parmlist. An exprlist which
10212 contains only identifiers at the global level
10213 is a parmlist. Otherwise, it is an exprlist. */
e92cc029 10214
8d08fdba
MS
10215int
10216parmlist_is_exprlist (exprs)
10217 tree exprs;
10218{
10219 if (exprs == NULL_TREE || TREE_PARMLIST (exprs))
10220 return 0;
10221
a9aedbc2 10222 if (toplevel_bindings_p ())
8d08fdba
MS
10223 {
10224 /* At the global level, if these are all identifiers,
10225 then it is a parmlist. */
10226 while (exprs)
10227 {
10228 if (TREE_CODE (TREE_VALUE (exprs)) != IDENTIFIER_NODE)
10229 return 1;
10230 exprs = TREE_CHAIN (exprs);
10231 }
10232 return 0;
10233 }
10234 return 1;
10235}
10236
10237/* Subroutine of `grokparms'. In a fcn definition, arg types must
10238 be complete.
10239
10240 C++: also subroutine of `start_function'. */
e92cc029 10241
8d08fdba
MS
10242static void
10243require_complete_types_for_parms (parms)
10244 tree parms;
10245{
5566b478
MS
10246 if (processing_template_decl)
10247 return;
10248
8d08fdba
MS
10249 while (parms)
10250 {
10251 tree type = TREE_TYPE (parms);
5566b478 10252 if (TYPE_SIZE (complete_type (type)) == NULL_TREE)
8d08fdba
MS
10253 {
10254 if (DECL_NAME (parms))
10255 error ("parameter `%s' has incomplete type",
10256 IDENTIFIER_POINTER (DECL_NAME (parms)));
10257 else
10258 error ("parameter has incomplete type");
10259 TREE_TYPE (parms) = error_mark_node;
10260 }
10261#if 0
10262 /* If the arg types are incomplete in a declaration,
10263 they must include undefined tags.
10264 These tags can never be defined in the scope of the declaration,
10265 so the types can never be completed,
10266 and no call can be compiled successfully. */
10267 /* This is not the right behavior for C++, but not having
10268 it is also probably wrong. */
10269 else
10270 {
10271 /* Now warn if is a pointer to an incomplete type. */
10272 while (TREE_CODE (type) == POINTER_TYPE
10273 || TREE_CODE (type) == REFERENCE_TYPE)
10274 type = TREE_TYPE (type);
10275 type = TYPE_MAIN_VARIANT (type);
10276 if (TYPE_SIZE (type) == NULL_TREE)
10277 {
10278 if (DECL_NAME (parm) != NULL_TREE)
10279 warning ("parameter `%s' points to incomplete type",
10280 IDENTIFIER_POINTER (DECL_NAME (parm)));
10281 else
10282 warning ("parameter points to incomplete type");
10283 }
10284 }
10285#endif
10286 parms = TREE_CHAIN (parms);
10287 }
10288}
10289
10290/* Decode the list of parameter types for a function type.
10291 Given the list of things declared inside the parens,
10292 return a list of types.
10293
10294 The list we receive can have three kinds of elements:
10295 an IDENTIFIER_NODE for names given without types,
10296 a TREE_LIST node for arguments given as typespecs or names with typespecs,
10297 or void_type_node, to mark the end of an argument list
10298 when additional arguments are not permitted (... was not used).
10299
10300 FUNCDEF_FLAG is nonzero for a function definition, 0 for
10301 a mere declaration. A nonempty identifier-list gets an error message
10302 when FUNCDEF_FLAG is zero.
10303 If FUNCDEF_FLAG is 1, then parameter types must be complete.
10304 If FUNCDEF_FLAG is -1, then parameter types may be incomplete.
10305
10306 If all elements of the input list contain types,
10307 we return a list of the types.
10308 If all elements contain no type (except perhaps a void_type_node
10309 at the end), we return a null list.
10310 If some have types and some do not, it is an error, and we
10311 return a null list.
10312
10313 Also set last_function_parms to either
10314 a list of names (IDENTIFIER_NODEs) or a chain of PARM_DECLs.
10315 A list of names is converted to a chain of PARM_DECLs
10316 by store_parm_decls so that ultimately it is always a chain of decls.
10317
10318 Note that in C++, parameters can take default values. These default
10319 values are in the TREE_PURPOSE field of the TREE_LIST. It is
10320 an error to specify default values which are followed by parameters
10321 that have no default values, or an ELLIPSES. For simplicities sake,
10322 only parameters which are specified with their types can take on
10323 default values. */
10324
10325static tree
10326grokparms (first_parm, funcdef_flag)
10327 tree first_parm;
10328 int funcdef_flag;
10329{
10330 tree result = NULL_TREE;
10331 tree decls = NULL_TREE;
10332
10333 if (first_parm != NULL_TREE
10334 && TREE_CODE (TREE_VALUE (first_parm)) == IDENTIFIER_NODE)
10335 {
10336 if (! funcdef_flag)
10337 pedwarn ("parameter names (without types) in function declaration");
10338 last_function_parms = first_parm;
10339 return NULL_TREE;
10340 }
10341 else if (first_parm != NULL_TREE
10342 && TREE_CODE (TREE_VALUE (first_parm)) != TREE_LIST
a1774733 10343 && TREE_CODE (TREE_VALUE (first_parm)) != VOID_TYPE)
8d08fdba
MS
10344 my_friendly_abort (145);
10345 else
10346 {
10347 /* Types were specified. This is a list of declarators
10348 each represented as a TREE_LIST node. */
10349 register tree parm, chain;
5566b478 10350 int any_init = 0, any_error = 0;
8d08fdba
MS
10351
10352 if (first_parm != NULL_TREE)
10353 {
10354 tree last_result = NULL_TREE;
10355 tree last_decl = NULL_TREE;
10356
10357 for (parm = first_parm; parm != NULL_TREE; parm = chain)
10358 {
a703fb38 10359 tree type = NULL_TREE, list_node = parm;
8d08fdba
MS
10360 register tree decl = TREE_VALUE (parm);
10361 tree init = TREE_PURPOSE (parm);
10362
10363 chain = TREE_CHAIN (parm);
10364 /* @@ weak defense against parse errors. */
a1774733
BK
10365 if (TREE_CODE (decl) != VOID_TYPE
10366 && TREE_CODE (decl) != TREE_LIST)
8d08fdba
MS
10367 {
10368 /* Give various messages as the need arises. */
10369 if (TREE_CODE (decl) == STRING_CST)
fc378698 10370 cp_error ("invalid string constant `%E'", decl);
8d08fdba
MS
10371 else if (TREE_CODE (decl) == INTEGER_CST)
10372 error ("invalid integer constant in parameter list, did you forget to give parameter name?");
10373 continue;
10374 }
10375
a1774733 10376 if (TREE_CODE (decl) != VOID_TYPE)
8d08fdba 10377 {
8d08fdba
MS
10378 decl = grokdeclarator (TREE_VALUE (decl),
10379 TREE_PURPOSE (decl),
f30432d7 10380 PARM, init != NULL_TREE,
c11b6f21 10381 NULL_TREE);
8d08fdba
MS
10382 if (! decl)
10383 continue;
10384 type = TREE_TYPE (decl);
a1774733 10385 if (TREE_CODE (type) == VOID_TYPE)
8d08fdba
MS
10386 decl = void_type_node;
10387 else if (TREE_CODE (type) == METHOD_TYPE)
10388 {
10389 if (DECL_NAME (decl))
fc378698 10390 /* Cannot use the decl here because
8d08fdba 10391 we don't have DECL_CONTEXT set up yet. */
fc378698
MS
10392 cp_error ("parameter `%D' invalidly declared method type",
10393 DECL_NAME (decl));
8d08fdba
MS
10394 else
10395 error ("parameter invalidly declared method type");
10396 type = build_pointer_type (type);
10397 TREE_TYPE (decl) = type;
10398 }
10399 else if (TREE_CODE (type) == OFFSET_TYPE)
10400 {
10401 if (DECL_NAME (decl))
fc378698
MS
10402 cp_error ("parameter `%D' invalidly declared offset type",
10403 DECL_NAME (decl));
8d08fdba
MS
10404 else
10405 error ("parameter invalidly declared offset type");
10406 type = build_pointer_type (type);
10407 TREE_TYPE (decl) = type;
10408 }
10409 else if (TREE_CODE (type) == RECORD_TYPE
10410 && TYPE_LANG_SPECIFIC (type)
10411 && CLASSTYPE_ABSTRACT_VIRTUALS (type))
10412 {
10413 abstract_virtuals_error (decl, type);
2c73f9f5 10414 any_error = 1; /* Seems like a good idea. */
8d08fdba
MS
10415 }
10416 else if (TREE_CODE (type) == RECORD_TYPE
10417 && TYPE_LANG_SPECIFIC (type)
10418 && IS_SIGNATURE (type))
10419 {
10420 signature_error (decl, type);
2c73f9f5 10421 any_error = 1; /* Seems like a good idea. */
8d08fdba
MS
10422 }
10423 }
10424
a1774733 10425 if (TREE_CODE (decl) == VOID_TYPE)
8d08fdba
MS
10426 {
10427 if (result == NULL_TREE)
10428 {
10429 result = void_list_node;
10430 last_result = result;
10431 }
10432 else
10433 {
10434 TREE_CHAIN (last_result) = void_list_node;
10435 last_result = void_list_node;
10436 }
8d08fdba
MS
10437 if (chain
10438 && (chain != void_list_node || TREE_CHAIN (chain)))
10439 error ("`void' in parameter list must be entire list");
10440 break;
10441 }
10442
10443 /* Since there is a prototype, args are passed in their own types. */
10444 DECL_ARG_TYPE (decl) = TREE_TYPE (decl);
10445#ifdef PROMOTE_PROTOTYPES
10446 if ((TREE_CODE (type) == INTEGER_TYPE
10447 || TREE_CODE (type) == ENUMERAL_TYPE)
10448 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
10449 DECL_ARG_TYPE (decl) = integer_type_node;
10450#endif
10451 if (!any_error)
10452 {
10453 if (init)
10454 {
10455 any_init++;
10456 if (TREE_CODE (init) == SAVE_EXPR)
10457 PARM_DECL_EXPR (init) = 1;
5156628f 10458 else if (processing_template_decl)
5566b478 10459 ;
42976354
BK
10460 /* Unparsed default arg from in-class decl. */
10461 else if (TREE_CODE (init) == DEFAULT_ARG)
10462 ;
b7484fbe
MS
10463 else if (TREE_CODE (init) == VAR_DECL
10464 || TREE_CODE (init) == PARM_DECL)
8d08fdba
MS
10465 {
10466 if (IDENTIFIER_LOCAL_VALUE (DECL_NAME (init)))
10467 {
10468 /* ``Local variables may not be used in default
10469 argument expressions.'' dpANSI C++ 8.2.6 */
10470 /* If extern int i; within a function is not
10471 considered a local variable, then this code is
e92cc029 10472 wrong. */
8d08fdba
MS
10473 cp_error ("local variable `%D' may not be used as a default argument", init);
10474 any_error = 1;
10475 }
10476 else if (TREE_READONLY_DECL_P (init))
10477 init = decl_constant_value (init);
10478 }
10479 else
10480 init = require_instantiated_type (type, init, integer_zero_node);
5156628f 10481 if (! processing_template_decl
42976354 10482 && TREE_CODE (init) != DEFAULT_ARG
c11b6f21 10483 && ! can_convert_arg (type, TREE_TYPE (init), init))
c73964b2
MS
10484 cp_pedwarn ("invalid type `%T' for default argument to `%#D'",
10485 TREE_TYPE (init), decl);
8d08fdba
MS
10486 }
10487 }
10488 else
10489 init = NULL_TREE;
10490
10491 if (decls == NULL_TREE)
10492 {
10493 decls = decl;
10494 last_decl = decls;
10495 }
10496 else
10497 {
10498 TREE_CHAIN (last_decl) = decl;
10499 last_decl = decl;
10500 }
66f2e88d 10501 if (! current_function_decl && TREE_PERMANENT (list_node))
8d08fdba
MS
10502 {
10503 TREE_PURPOSE (list_node) = init;
10504 TREE_VALUE (list_node) = type;
10505 TREE_CHAIN (list_node) = NULL_TREE;
10506 }
10507 else
10508 list_node = saveable_tree_cons (init, type, NULL_TREE);
10509 if (result == NULL_TREE)
10510 {
10511 result = list_node;
10512 last_result = result;
10513 }
10514 else
10515 {
10516 TREE_CHAIN (last_result) = list_node;
10517 last_result = list_node;
10518 }
10519 }
10520 if (last_result)
10521 TREE_CHAIN (last_result) = NULL_TREE;
10522 /* If there are no parameters, and the function does not end
10523 with `...', then last_decl will be NULL_TREE. */
10524 if (last_decl != NULL_TREE)
10525 TREE_CHAIN (last_decl) = NULL_TREE;
10526 }
10527 }
10528
10529 last_function_parms = decls;
10530
10531 /* In a fcn definition, arg types must be complete. */
10532 if (funcdef_flag > 0)
10533 require_complete_types_for_parms (last_function_parms);
10534
10535 return result;
10536}
42976354
BK
10537
10538/* Called from the parser to update an element of TYPE_ARG_TYPES for some
10539 FUNCTION_TYPE with the newly parsed version of its default argument, which
10540 was previously digested as text. See snarf_defarg et al in lex.c. */
10541
10542void
10543replace_defarg (arg, init)
10544 tree arg, init;
10545{
aa5f3bad 10546 if (! processing_template_decl
42976354
BK
10547 && ! can_convert_arg (TREE_VALUE (arg), TREE_TYPE (init), init))
10548 cp_pedwarn ("invalid type `%T' for default argument to `%T'",
10549 TREE_TYPE (init), TREE_VALUE (arg));
10550 TREE_PURPOSE (arg) = init;
10551}
8d08fdba 10552\f
c11b6f21
MS
10553int
10554copy_args_p (d)
10555 tree d;
10556{
10557 tree t = FUNCTION_ARG_CHAIN (d);
67437d5b
JM
10558 if (DECL_CONSTRUCTOR_P (d)
10559 && TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (d)))
10560 t = TREE_CHAIN (t);
c11b6f21
MS
10561 if (t && TREE_CODE (TREE_VALUE (t)) == REFERENCE_TYPE
10562 && (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_VALUE (t)))
10563 == DECL_CLASS_CONTEXT (d))
10564 && (TREE_CHAIN (t) == NULL_TREE
10565 || TREE_CHAIN (t) == void_list_node
10566 || TREE_PURPOSE (TREE_CHAIN (t))))
10567 return 1;
10568 return 0;
10569}
10570
8d08fdba
MS
10571/* These memoizing functions keep track of special properties which
10572 a class may have. `grok_ctor_properties' notices whether a class
10573 has a constructor of the form X(X&), and also complains
10574 if the class has a constructor of the form X(X).
10575 `grok_op_properties' takes notice of the various forms of
10576 operator= which are defined, as well as what sorts of type conversion
10577 may apply. Both functions take a FUNCTION_DECL as an argument. */
e92cc029 10578
a0a33927 10579int
8d08fdba
MS
10580grok_ctor_properties (ctype, decl)
10581 tree ctype, decl;
10582{
10583 tree parmtypes = FUNCTION_ARG_CHAIN (decl);
10584 tree parmtype = parmtypes ? TREE_VALUE (parmtypes) : void_type_node;
10585
10586 /* When a type has virtual baseclasses, a magical first int argument is
10587 added to any ctor so we can tell if the class has been initialized
10588 yet. This could screw things up in this function, so we deliberately
10589 ignore the leading int if we're in that situation. */
10590 if (parmtypes
10591 && TREE_VALUE (parmtypes) == integer_type_node
10592 && TYPE_USES_VIRTUAL_BASECLASSES (ctype))
10593 {
10594 parmtypes = TREE_CHAIN (parmtypes);
10595 parmtype = TREE_VALUE (parmtypes);
10596 }
10597
10598 if (TREE_CODE (parmtype) == REFERENCE_TYPE
0b41abe6
JM
10599 && TYPE_MAIN_VARIANT (TREE_TYPE (parmtype)) == ctype
10600 && (TREE_CHAIN (parmtypes) == NULL_TREE
8d08fdba 10601 || TREE_CHAIN (parmtypes) == void_list_node
0b41abe6
JM
10602 || TREE_PURPOSE (TREE_CHAIN (parmtypes))))
10603 {
10604 TYPE_HAS_INIT_REF (ctype) = 1;
10605 if (TYPE_READONLY (TREE_TYPE (parmtype)))
10606 TYPE_HAS_CONST_INIT_REF (ctype) = 1;
8d08fdba 10607 }
0b41abe6
JM
10608 else if (TYPE_MAIN_VARIANT (parmtype) == ctype
10609 && TREE_CHAIN (parmtypes) != NULL_TREE
10610 && TREE_CHAIN (parmtypes) == void_list_node)
8d08fdba 10611 {
0b41abe6
JM
10612 cp_error ("invalid constructor; you probably meant `%T (const %T&)'",
10613 ctype, ctype);
10614 SET_IDENTIFIER_ERROR_LOCUS (DECL_NAME (decl), ctype);
10615 return 0;
8d08fdba
MS
10616 }
10617 else if (TREE_CODE (parmtype) == VOID_TYPE
10618 || TREE_PURPOSE (parmtypes) != NULL_TREE)
10619 TYPE_HAS_DEFAULT_CONSTRUCTOR (ctype) = 1;
a0a33927
MS
10620
10621 return 1;
8d08fdba
MS
10622}
10623
10624/* An operator with this name can be either unary or binary. */
e92cc029 10625
a28e3c7f
MS
10626static int
10627ambi_op_p (name)
8d08fdba
MS
10628 tree name;
10629{
10630 return (name == ansi_opname [(int) INDIRECT_REF]
10631 || name == ansi_opname [(int) ADDR_EXPR]
10632 || name == ansi_opname [(int) NEGATE_EXPR]
10633 || name == ansi_opname[(int) POSTINCREMENT_EXPR]
10634 || name == ansi_opname[(int) POSTDECREMENT_EXPR]
10635 || name == ansi_opname [(int) CONVERT_EXPR]);
10636}
10637
10638/* An operator with this name can only be unary. */
e92cc029 10639
a28e3c7f
MS
10640static int
10641unary_op_p (name)
8d08fdba
MS
10642 tree name;
10643{
10644 return (name == ansi_opname [(int) TRUTH_NOT_EXPR]
10645 || name == ansi_opname [(int) BIT_NOT_EXPR]
10646 || name == ansi_opname [(int) COMPONENT_REF]
10647 || OPERATOR_TYPENAME_P (name));
10648}
10649
10650/* Do a little sanity-checking on how they declared their operator. */
e92cc029 10651
5566b478 10652void
8d08fdba
MS
10653grok_op_properties (decl, virtualp, friendp)
10654 tree decl;
10655 int virtualp, friendp;
10656{
10657 tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
10658 int methodp = (TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE);
10659 tree name = DECL_NAME (decl);
8d08fdba 10660
a28e3c7f
MS
10661 if (current_class_type == NULL_TREE)
10662 friendp = 1;
8d08fdba 10663
a28e3c7f
MS
10664 if (! friendp)
10665 {
10666 if (name == ansi_opname[(int) MODIFY_EXPR])
10667 TYPE_HAS_ASSIGNMENT (current_class_type) = 1;
10668 else if (name == ansi_opname[(int) CALL_EXPR])
10669 TYPE_OVERLOADS_CALL_EXPR (current_class_type) = 1;
10670 else if (name == ansi_opname[(int) ARRAY_REF])
10671 TYPE_OVERLOADS_ARRAY_REF (current_class_type) = 1;
10672 else if (name == ansi_opname[(int) COMPONENT_REF]
10673 || name == ansi_opname[(int) MEMBER_REF])
10674 TYPE_OVERLOADS_ARROW (current_class_type) = 1;
10675 else if (name == ansi_opname[(int) NEW_EXPR])
10676 TYPE_GETS_NEW (current_class_type) |= 1;
10677 else if (name == ansi_opname[(int) DELETE_EXPR])
10678 TYPE_GETS_DELETE (current_class_type) |= 1;
10679 else if (name == ansi_opname[(int) VEC_NEW_EXPR])
10680 TYPE_GETS_NEW (current_class_type) |= 2;
10681 else if (name == ansi_opname[(int) VEC_DELETE_EXPR])
10682 TYPE_GETS_DELETE (current_class_type) |= 2;
10683 }
10684
10685 if (name == ansi_opname[(int) NEW_EXPR]
10686 || name == ansi_opname[(int) VEC_NEW_EXPR])
8d08fdba 10687 {
8d08fdba
MS
10688 /* When the compiler encounters the definition of A::operator new, it
10689 doesn't look at the class declaration to find out if it's static. */
a28e3c7f 10690 if (methodp)
700f8a87 10691 revert_static_member_fn (&decl, NULL, NULL);
8d08fdba
MS
10692
10693 /* Take care of function decl if we had syntax errors. */
10694 if (argtypes == NULL_TREE)
beb53fb8
JM
10695 TREE_TYPE (decl)
10696 = build_function_type (ptr_type_node,
10697 hash_tree_chain (integer_type_node,
10698 void_list_node));
8d08fdba 10699 else
a28e3c7f 10700 TREE_TYPE (decl) = coerce_new_type (TREE_TYPE (decl));
8d08fdba 10701 }
a28e3c7f
MS
10702 else if (name == ansi_opname[(int) DELETE_EXPR]
10703 || name == ansi_opname[(int) VEC_DELETE_EXPR])
8d08fdba 10704 {
a28e3c7f 10705 if (methodp)
700f8a87 10706 revert_static_member_fn (&decl, NULL, NULL);
8d08fdba
MS
10707
10708 if (argtypes == NULL_TREE)
beb53fb8
JM
10709 TREE_TYPE (decl)
10710 = build_function_type (void_type_node,
10711 hash_tree_chain (ptr_type_node,
10712 void_list_node));
8d08fdba 10713 else
a28e3c7f
MS
10714 {
10715 TREE_TYPE (decl) = coerce_delete_type (TREE_TYPE (decl));
10716
10717 if (! friendp && name == ansi_opname[(int) VEC_DELETE_EXPR]
10718 && (TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
10719 != void_list_node))
10720 TYPE_VEC_DELETE_TAKES_SIZE (current_class_type) = 1;
10721 }
8d08fdba 10722 }
8d08fdba
MS
10723 else
10724 {
10725 /* An operator function must either be a non-static member function
10726 or have at least one parameter of a class, a reference to a class,
10727 an enumeration, or a reference to an enumeration. 13.4.0.6 */
700f8a87 10728 if (! methodp || DECL_STATIC_FUNCTION_P (decl))
8d08fdba
MS
10729 {
10730 if (OPERATOR_TYPENAME_P (name)
10731 || name == ansi_opname[(int) CALL_EXPR]
10732 || name == ansi_opname[(int) MODIFY_EXPR]
10733 || name == ansi_opname[(int) COMPONENT_REF]
10734 || name == ansi_opname[(int) ARRAY_REF])
10735 cp_error ("`%D' must be a nonstatic member function", decl);
10736 else
10737 {
10738 tree p = argtypes;
10739
700f8a87
MS
10740 if (DECL_STATIC_FUNCTION_P (decl))
10741 cp_error ("`%D' must be either a non-static member function or a non-member function", decl);
10742
8d08fdba 10743 if (p)
a1774733 10744 for (; TREE_CODE (TREE_VALUE (p)) != VOID_TYPE ; p = TREE_CHAIN (p))
8d08fdba
MS
10745 {
10746 tree arg = TREE_VALUE (p);
10747 if (TREE_CODE (arg) == REFERENCE_TYPE)
10748 arg = TREE_TYPE (arg);
10749
10750 /* This lets bad template code slip through. */
10751 if (IS_AGGR_TYPE (arg)
10752 || TREE_CODE (arg) == ENUMERAL_TYPE
73b0fce8
KL
10753 || TREE_CODE (arg) == TEMPLATE_TYPE_PARM
10754 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
8d08fdba
MS
10755 goto foundaggr;
10756 }
10757 cp_error
10758 ("`%D' must have an argument of class or enumerated type",
10759 decl);
10760 foundaggr:
10761 ;
10762 }
10763 }
10764
277294d7 10765 if (name == ansi_opname[(int) CALL_EXPR])
2c73f9f5 10766 return; /* No restrictions on args. */
8d08fdba 10767
9a3b49ac 10768 if (IDENTIFIER_TYPENAME_P (name) && ! DECL_TEMPLATE_INFO (decl))
a0a33927
MS
10769 {
10770 tree t = TREE_TYPE (name);
10771 if (TREE_CODE (t) == VOID_TYPE)
10772 pedwarn ("void is not a valid type conversion operator");
10773 else if (! friendp)
10774 {
10775 int ref = (TREE_CODE (t) == REFERENCE_TYPE);
10776 char *what = 0;
10777 if (ref)
10778 t = TYPE_MAIN_VARIANT (TREE_TYPE (t));
10779
10780 if (t == current_class_type)
10781 what = "the same type";
9a3b49ac 10782 /* Don't force t to be complete here. */
a0a33927 10783 else if (IS_AGGR_TYPE (t)
9a3b49ac 10784 && TYPE_SIZE (t)
a0a33927
MS
10785 && DERIVED_FROM_P (t, current_class_type))
10786 what = "a base class";
10787
10788 if (what)
10789 warning ("conversion to %s%s will never use a type conversion operator",
10790 ref ? "a reference to " : "", what);
10791 }
10792 }
10793
8d08fdba
MS
10794 if (name == ansi_opname[(int) MODIFY_EXPR])
10795 {
10796 tree parmtype;
10797
10798 if (list_length (argtypes) != 3 && methodp)
10799 {
10800 cp_error ("`%D' must take exactly one argument", decl);
10801 return;
10802 }
10803 parmtype = TREE_VALUE (TREE_CHAIN (argtypes));
10804
f0e01782 10805 if (copy_assignment_arg_p (parmtype, virtualp)
a28e3c7f 10806 && ! friendp)
8d08fdba
MS
10807 {
10808 TYPE_HAS_ASSIGN_REF (current_class_type) = 1;
f0e01782
MS
10809 if (TREE_CODE (parmtype) != REFERENCE_TYPE
10810 || TYPE_READONLY (TREE_TYPE (parmtype)))
8d08fdba
MS
10811 TYPE_HAS_CONST_ASSIGN_REF (current_class_type) = 1;
10812 }
10813 }
10814 else if (name == ansi_opname[(int) COND_EXPR])
10815 {
10816 /* 13.4.0.3 */
10817 pedwarn ("ANSI C++ prohibits overloading operator ?:");
10818 if (list_length (argtypes) != 4)
10819 cp_error ("`%D' must take exactly three arguments", decl);
10820 }
10821 else if (ambi_op_p (name))
10822 {
10823 if (list_length (argtypes) == 2)
10824 /* prefix */;
10825 else if (list_length (argtypes) == 3)
10826 {
10827 if ((name == ansi_opname[(int) POSTINCREMENT_EXPR]
10828 || name == ansi_opname[(int) POSTDECREMENT_EXPR])
5156628f 10829 && ! processing_template_decl
8d08fdba
MS
10830 && TREE_VALUE (TREE_CHAIN (argtypes)) != integer_type_node)
10831 {
10832 if (methodp)
10833 cp_error ("postfix `%D' must take `int' as its argument",
10834 decl);
10835 else
10836 cp_error
10837 ("postfix `%D' must take `int' as its second argument",
10838 decl);
10839 }
10840 }
10841 else
10842 {
10843 if (methodp)
10844 cp_error ("`%D' must take either zero or one argument", decl);
10845 else
10846 cp_error ("`%D' must take either one or two arguments", decl);
10847 }
824b9a4c
MS
10848
10849 /* More Effective C++ rule 6. */
eb448459 10850 if (warn_ecpp
824b9a4c
MS
10851 && (name == ansi_opname[(int) POSTINCREMENT_EXPR]
10852 || name == ansi_opname[(int) POSTDECREMENT_EXPR]))
10853 {
10854 tree arg = TREE_VALUE (argtypes);
10855 tree ret = TREE_TYPE (TREE_TYPE (decl));
10856 if (methodp || TREE_CODE (arg) == REFERENCE_TYPE)
10857 arg = TREE_TYPE (arg);
10858 arg = TYPE_MAIN_VARIANT (arg);
10859 if (list_length (argtypes) == 2)
10860 {
10861 if (TREE_CODE (ret) != REFERENCE_TYPE
10862 || !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (ret)),
10863 arg, 1))
10864 cp_warning ("prefix `%D' should return `%T'", decl,
10865 build_reference_type (arg));
10866 }
10867 else
10868 {
10869 if (!comptypes (TYPE_MAIN_VARIANT (ret), arg, 1))
10870 cp_warning ("postfix `%D' should return `%T'", decl, arg);
10871 }
10872 }
8d08fdba
MS
10873 }
10874 else if (unary_op_p (name))
10875 {
10876 if (list_length (argtypes) != 2)
10877 {
10878 if (methodp)
10879 cp_error ("`%D' must take `void'", decl);
10880 else
10881 cp_error ("`%D' must take exactly one argument", decl);
10882 }
10883 }
10884 else /* if (binary_op_p (name)) */
10885 {
10886 if (list_length (argtypes) != 3)
10887 {
10888 if (methodp)
10889 cp_error ("`%D' must take exactly one argument", decl);
10890 else
10891 cp_error ("`%D' must take exactly two arguments", decl);
10892 }
824b9a4c
MS
10893
10894 /* More Effective C++ rule 7. */
eb448459 10895 if (warn_ecpp
824b9a4c
MS
10896 && (name == ansi_opname [TRUTH_ANDIF_EXPR]
10897 || name == ansi_opname [TRUTH_ORIF_EXPR]
10898 || name == ansi_opname [COMPOUND_EXPR]))
10899 cp_warning ("user-defined `%D' always evaluates both arguments",
10900 decl);
10901 }
10902
10903 /* Effective C++ rule 23. */
eb448459 10904 if (warn_ecpp
824b9a4c
MS
10905 && list_length (argtypes) == 3
10906 && (name == ansi_opname [PLUS_EXPR]
10907 || name == ansi_opname [MINUS_EXPR]
10908 || name == ansi_opname [TRUNC_DIV_EXPR]
10909 || name == ansi_opname [MULT_EXPR])
10910 && TREE_CODE (TREE_TYPE (TREE_TYPE (decl))) == REFERENCE_TYPE)
10911 cp_warning ("`%D' should return by value", decl);
8d08fdba
MS
10912
10913 /* 13.4.0.8 */
10914 if (argtypes)
10915 for (; argtypes != void_list_node ; argtypes = TREE_CHAIN (argtypes))
10916 if (TREE_PURPOSE (argtypes))
10917 {
10918 TREE_PURPOSE (argtypes) = NULL_TREE;
10919 if (name == ansi_opname[(int) POSTINCREMENT_EXPR]
10920 || name == ansi_opname[(int) POSTDECREMENT_EXPR])
10921 {
10922 if (pedantic)
10923 cp_pedwarn ("`%D' cannot have default arguments", decl);
10924 }
10925 else
10926 cp_error ("`%D' cannot have default arguments", decl);
10927 }
10928 }
10929}
10930\f
10931/* Get the struct, enum or union (CODE says which) with tag NAME.
10932 Define the tag as a forward-reference if it is not defined.
10933
10934 C++: If a class derivation is given, process it here, and report
10935 an error if multiple derivation declarations are not identical.
10936
10937 If this is a definition, come in through xref_tag and only look in
10938 the current frame for the name (since C++ allows new names in any
10939 scope.) */
10940
8d08fdba
MS
10941tree
10942xref_tag (code_type_node, name, binfo, globalize)
10943 tree code_type_node;
10944 tree name, binfo;
10945 int globalize;
10946{
10947 enum tag_types tag_code;
10948 enum tree_code code;
10949 int temp = 0;
8d08fdba
MS
10950 register tree ref, t;
10951 struct binding_level *b = inner_binding_level;
a80e4195 10952 int got_type = 0;
8d08fdba
MS
10953
10954 tag_code = (enum tag_types) TREE_INT_CST_LOW (code_type_node);
10955 switch (tag_code)
10956 {
10957 case record_type:
10958 case class_type:
8d08fdba
MS
10959 case signature_type:
10960 code = RECORD_TYPE;
8d08fdba
MS
10961 break;
10962 case union_type:
10963 code = UNION_TYPE;
8d08fdba
MS
10964 break;
10965 case enum_type:
10966 code = ENUMERAL_TYPE;
10967 break;
10968 default:
10969 my_friendly_abort (18);
10970 }
10971
10972 /* If a cross reference is requested, look up the type
10973 already defined for this tag and return it. */
be99da77
MS
10974 if (TREE_CODE_CLASS (TREE_CODE (name)) == 't')
10975 {
10976 t = name;
a80e4195
MS
10977 name = TYPE_IDENTIFIER (t);
10978 got_type = 1;
be99da77
MS
10979 }
10980 else
10981 t = IDENTIFIER_TYPE_VALUE (name);
73b0fce8
KL
10982 if (t && TREE_CODE (t) != code && TREE_CODE (t) != TEMPLATE_TYPE_PARM
10983 && TREE_CODE (t) != TEMPLATE_TEMPLATE_PARM)
a0a33927
MS
10984 t = NULL_TREE;
10985
8ccc31eb 10986 if (! globalize)
8d08fdba 10987 {
73b0fce8
KL
10988 if (pedantic && t && (TREE_CODE (t) == TEMPLATE_TYPE_PARM
10989 || TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM))
ec255269
MS
10990 {
10991 cp_pedwarn ("redeclaration of template type-parameter `%T'", name);
10992 cp_pedwarn_at (" previously declared here", t);
10993 }
a80e4195 10994 if (t && TYPE_CONTEXT (t) && got_type)
8ccc31eb 10995 ref = t;
8d08fdba 10996 else
e92cc029
MS
10997 {
10998 /* If we know we are defining this tag, only look it up in
10999 this scope and don't try to find it as a type. */
11000 ref = lookup_tag (code, name, b, 1);
11001 }
8d08fdba
MS
11002 }
11003 else
11004 {
11005 if (t)
11006 ref = t;
11007 else
11008 ref = lookup_tag (code, name, b, 0);
11009
11010 if (! ref)
11011 {
11012 /* Try finding it as a type declaration. If that wins, use it. */
11013 ref = lookup_name (name, 1);
6757edfe
MM
11014
11015 if (ref != NULL_TREE
11016 && processing_template_decl
11017 && DECL_CLASS_TEMPLATE_P (ref)
11018 && template_class_depth (current_class_type) == 0)
11019 /* Since GLOBALIZE is true, we're declaring a global
11020 template, so we want this type. */
11021 ref = DECL_RESULT (ref);
11022
8d08fdba
MS
11023 if (ref && TREE_CODE (ref) == TYPE_DECL
11024 && TREE_CODE (TREE_TYPE (ref)) == code)
11025 ref = TREE_TYPE (ref);
11026 else
11027 ref = NULL_TREE;
11028 }
11029 }
11030
11031 push_obstacks_nochange ();
11032
11033 if (! ref)
11034 {
11035 /* If no such tag is yet defined, create a forward-reference node
11036 and record it as the "definition".
11037 When a real declaration of this type is found,
11038 the forward-reference will be altered into a real type. */
11039
11040 /* In C++, since these migrate into the global scope, we must
11041 build them on the permanent obstack. */
11042
11043 temp = allocation_temporary_p ();
11044 if (temp)
11045 end_temporary_allocation ();
11046
11047 if (code == ENUMERAL_TYPE)
11048 {
fc378698
MS
11049 cp_error ("use of enum `%#D' without previous declaration", name);
11050
8d08fdba
MS
11051 ref = make_node (ENUMERAL_TYPE);
11052
11053 /* Give the type a default layout like unsigned int
11054 to avoid crashing if it does not get defined. */
11055 TYPE_MODE (ref) = TYPE_MODE (unsigned_type_node);
11056 TYPE_ALIGN (ref) = TYPE_ALIGN (unsigned_type_node);
11057 TREE_UNSIGNED (ref) = 1;
11058 TYPE_PRECISION (ref) = TYPE_PRECISION (unsigned_type_node);
11059 TYPE_MIN_VALUE (ref) = TYPE_MIN_VALUE (unsigned_type_node);
11060 TYPE_MAX_VALUE (ref) = TYPE_MAX_VALUE (unsigned_type_node);
11061
11062 /* Enable us to recognize when a type is created in class context.
11063 To do nested classes correctly, this should probably be cleared
11064 out when we leave this classes scope. Currently this in only
11065 done in `start_enum'. */
11066
11067 pushtag (name, ref, globalize);
8d08fdba 11068 }
8d08fdba
MS
11069 else
11070 {
8d08fdba 11071 struct binding_level *old_b = class_binding_level;
8d08fdba
MS
11072
11073 ref = make_lang_type (code);
11074
8d08fdba
MS
11075 if (tag_code == signature_type)
11076 {
11077 SET_SIGNATURE (ref);
f376e137
MS
11078 /* Since a signature type will be turned into the type
11079 of signature tables, it's not only an interface. */
8d08fdba 11080 CLASSTYPE_INTERFACE_ONLY (ref) = 0;
f376e137
MS
11081 SET_CLASSTYPE_INTERFACE_KNOWN (ref);
11082 /* A signature doesn't have a vtable. */
00595019 11083 CLASSTYPE_VTABLE_NEEDS_WRITING (ref) = 0;
8d08fdba
MS
11084 }
11085
8d08fdba
MS
11086#ifdef NONNESTED_CLASSES
11087 /* Class types don't nest the way enums do. */
11088 class_binding_level = (struct binding_level *)0;
11089#endif
11090 pushtag (name, ref, globalize);
11091 class_binding_level = old_b;
8d08fdba
MS
11092 }
11093 }
11094 else
11095 {
8d08fdba 11096 /* If it no longer looks like a nested type, make sure it's
30394414
JM
11097 in global scope.
11098 If it is not an IDENTIFIER, this is not a declaration */
2c73f9f5 11099 if (b->namespace_p && !class_binding_level
30394414
JM
11100 && TREE_CODE (name) == IDENTIFIER_NODE)
11101 {
2c73f9f5
ML
11102 if (IDENTIFIER_NAMESPACE_VALUE (name) == NULL_TREE)
11103 SET_IDENTIFIER_NAMESPACE_VALUE (name, TYPE_NAME (ref));
30394414 11104 }
7fe6899f
MM
11105
11106 if (!globalize && processing_template_decl && IS_AGGR_TYPE (ref))
6757edfe 11107 redeclare_class_template (ref, current_template_parms);
8d08fdba
MS
11108 }
11109
11110 if (binfo)
8ccc31eb 11111 xref_basetypes (code_type_node, name, ref, binfo);
8d08fdba 11112
8d08fdba
MS
11113 /* Until the type is defined, tentatively accept whatever
11114 structure tag the user hands us. */
11115 if (TYPE_SIZE (ref) == NULL_TREE
11116 && ref != current_class_type
11117 /* Have to check this, in case we have contradictory tag info. */
11118 && IS_AGGR_TYPE_CODE (TREE_CODE (ref)))
11119 {
11120 if (tag_code == class_type)
11121 CLASSTYPE_DECLARED_CLASS (ref) = 1;
11122 else if (tag_code == record_type || tag_code == signature_type)
11123 CLASSTYPE_DECLARED_CLASS (ref) = 0;
11124 }
11125
11126 pop_obstacks ();
11127
11128 return ref;
11129}
8ccc31eb 11130
fc378698
MS
11131tree
11132xref_tag_from_type (old, id, globalize)
11133 tree old, id;
11134 int globalize;
11135{
11136 tree code_type_node;
11137
11138 if (TREE_CODE (old) == RECORD_TYPE)
11139 code_type_node = (CLASSTYPE_DECLARED_CLASS (old)
11140 ? class_type_node : record_type_node);
11141 else
11142 code_type_node = union_type_node;
11143
11144 if (id == NULL_TREE)
11145 id = TYPE_IDENTIFIER (old);
11146
11147 return xref_tag (code_type_node, id, NULL_TREE, globalize);
11148}
11149
8ccc31eb
MS
11150void
11151xref_basetypes (code_type_node, name, ref, binfo)
11152 tree code_type_node;
11153 tree name, ref;
11154 tree binfo;
11155{
11156 /* In the declaration `A : X, Y, ... Z' we mark all the types
11157 (A, X, Y, ..., Z) so we can check for duplicates. */
11158 tree binfos;
11159 int i, len;
11160 enum tag_types tag_code = (enum tag_types) TREE_INT_CST_LOW (code_type_node);
11161
11162 if (tag_code == union_type)
11163 {
11164 cp_error ("derived union `%T' invalid", ref);
11165 return;
11166 }
11167
11168 len = list_length (binfo);
11169 push_obstacks (TYPE_OBSTACK (ref), TYPE_OBSTACK (ref));
11170
11171 SET_CLASSTYPE_MARKED (ref);
11172 BINFO_BASETYPES (TYPE_BINFO (ref)) = binfos = make_tree_vec (len);
11173
11174 for (i = 0; binfo; binfo = TREE_CHAIN (binfo))
11175 {
11176 /* The base of a derived struct is public by default. */
11177 int via_public
be99da77
MS
11178 = (TREE_PURPOSE (binfo) == access_public_node
11179 || TREE_PURPOSE (binfo) == access_public_virtual_node
8ccc31eb 11180 || (tag_code != class_type
be99da77
MS
11181 && (TREE_PURPOSE (binfo) == access_default_node
11182 || TREE_PURPOSE (binfo) == access_default_virtual_node)));
d8b55a76
JM
11183 int via_protected
11184 = (TREE_PURPOSE (binfo) == access_protected_node
11185 || TREE_PURPOSE (binfo) == access_protected_virtual_node);
8ccc31eb 11186 int via_virtual
be99da77 11187 = (TREE_PURPOSE (binfo) == access_private_virtual_node
d8b55a76 11188 || TREE_PURPOSE (binfo) == access_protected_virtual_node
be99da77
MS
11189 || TREE_PURPOSE (binfo) == access_public_virtual_node
11190 || TREE_PURPOSE (binfo) == access_default_virtual_node);
11191 tree basetype = TREE_VALUE (binfo);
8ccc31eb
MS
11192 tree base_binfo;
11193
11194 GNU_xref_hier (IDENTIFIER_POINTER (name),
11195 IDENTIFIER_POINTER (TREE_VALUE (binfo)),
11196 via_public, via_virtual, 0);
11197
11198 if (basetype && TREE_CODE (basetype) == TYPE_DECL)
11199 basetype = TREE_TYPE (basetype);
5566b478
MS
11200 if (!basetype
11201 || (TREE_CODE (basetype) != RECORD_TYPE
a80e4195 11202 && TREE_CODE (basetype) != TYPENAME_TYPE
73b0fce8
KL
11203 && TREE_CODE (basetype) != TEMPLATE_TYPE_PARM
11204 && TREE_CODE (basetype) != TEMPLATE_TEMPLATE_PARM))
8ccc31eb
MS
11205 {
11206 cp_error ("base type `%T' fails to be a struct or class type",
11207 TREE_VALUE (binfo));
11208 continue;
11209 }
11210#if 1
8adf5b5e
JM
11211 /* This code replaces similar code in layout_basetypes.
11212 We put the complete_type first for implicit `typename'. */
11213 else if (TYPE_SIZE (complete_type (basetype)) == NULL_TREE
11214 && ! (current_template_parms && uses_template_parms (basetype)))
8ccc31eb
MS
11215 {
11216 cp_error ("base class `%T' has incomplete type", basetype);
11217 continue;
11218 }
11219#endif
11220 else
11221 {
11222 if (CLASSTYPE_MARKED (basetype))
11223 {
11224 if (basetype == ref)
11225 cp_error ("recursive type `%T' undefined", basetype);
11226 else
11227 cp_error ("duplicate base type `%T' invalid", basetype);
11228 continue;
11229 }
11230
11231 /* Note that the BINFO records which describe individual
11232 inheritances are *not* shared in the lattice! They
11233 cannot be shared because a given baseclass may be
11234 inherited with different `accessibility' by different
11235 derived classes. (Each BINFO record describing an
11236 individual inheritance contains flags which say what
11237 the `accessibility' of that particular inheritance is.) */
11238
11239 base_binfo = make_binfo (integer_zero_node, basetype,
11240 TYPE_BINFO_VTABLE (basetype),
11241 TYPE_BINFO_VIRTUALS (basetype), NULL_TREE);
11242
11243 TREE_VEC_ELT (binfos, i) = base_binfo;
11244 TREE_VIA_PUBLIC (base_binfo) = via_public;
11245 TREE_VIA_PROTECTED (base_binfo) = via_protected;
11246 TREE_VIA_VIRTUAL (base_binfo) = via_virtual;
11247 BINFO_INHERITANCE_CHAIN (base_binfo) = TYPE_BINFO (ref);
11248
11249 SET_CLASSTYPE_MARKED (basetype);
9e9ff709 11250
8ccc31eb
MS
11251 /* We are free to modify these bits because they are meaningless
11252 at top level, and BASETYPE is a top-level type. */
11253 if (via_virtual || TYPE_USES_VIRTUAL_BASECLASSES (basetype))
11254 {
11255 TYPE_USES_VIRTUAL_BASECLASSES (ref) = 1;
11256 TYPE_USES_COMPLEX_INHERITANCE (ref) = 1;
11257 }
11258
8ccc31eb
MS
11259 TYPE_GETS_NEW (ref) |= TYPE_GETS_NEW (basetype);
11260 TYPE_GETS_DELETE (ref) |= TYPE_GETS_DELETE (basetype);
11261 CLASSTYPE_LOCAL_TYPEDECLS (ref) |= CLASSTYPE_LOCAL_TYPEDECLS (basetype);
11262 i += 1;
11263 }
11264 }
11265 if (i)
11266 TREE_VEC_LENGTH (binfos) = i;
11267 else
11268 BINFO_BASETYPES (TYPE_BINFO (ref)) = NULL_TREE;
11269
11270 if (i > 1)
11271 TYPE_USES_MULTIPLE_INHERITANCE (ref) = 1;
11272 else if (i == 1)
11273 TYPE_USES_MULTIPLE_INHERITANCE (ref)
11274 = TYPE_USES_MULTIPLE_INHERITANCE (BINFO_TYPE (TREE_VEC_ELT (binfos, 0)));
11275 if (TYPE_USES_MULTIPLE_INHERITANCE (ref))
11276 TYPE_USES_COMPLEX_INHERITANCE (ref) = 1;
11277
11278 /* Unmark all the types. */
11279 while (--i >= 0)
11280 CLEAR_CLASSTYPE_MARKED (BINFO_TYPE (TREE_VEC_ELT (binfos, i)));
11281 CLEAR_CLASSTYPE_MARKED (ref);
11282
11283 pop_obstacks ();
11284}
11285
8d08fdba 11286\f
b3d5a58b 11287tree current_local_enum = NULL_TREE;
8d08fdba
MS
11288
11289/* Begin compiling the definition of an enumeration type.
11290 NAME is its name (or null if anonymous).
11291 Returns the type object, as yet incomplete.
11292 Also records info about it so that build_enumerator
11293 may be used to declare the individual values as they are read. */
11294
11295tree
11296start_enum (name)
11297 tree name;
11298{
11299 register tree enumtype = NULL_TREE;
11300 struct binding_level *b = inner_binding_level;
11301
b87692e5
MS
11302 if (processing_template_decl && current_function_decl)
11303 end_temporary_allocation ();
11304
8d08fdba
MS
11305 /* If this is the real definition for a previous forward reference,
11306 fill in the contents in the same object that used to be the
11307 forward reference. */
11308
11309 if (name != NULL_TREE)
11310 enumtype = lookup_tag (ENUMERAL_TYPE, name, b, 1);
11311
11312 if (enumtype != NULL_TREE && TREE_CODE (enumtype) == ENUMERAL_TYPE)
c91a56d2 11313 cp_error ("multiple definition of `%#T'", enumtype);
8d08fdba
MS
11314 else
11315 {
11316 enumtype = make_node (ENUMERAL_TYPE);
11317 pushtag (name, enumtype, 0);
11318 }
11319
c91a56d2
MS
11320 if (b->pseudo_global)
11321 cp_error ("template declaration of `%#T'", enumtype);
11322
8d08fdba
MS
11323 if (current_class_type)
11324 TREE_ADDRESSABLE (b->tags) = 1;
c91a56d2 11325
8d08fdba
MS
11326 current_local_enum = NULL_TREE;
11327
e92cc029
MS
11328 /* We don't copy this value because build_enumerator needs to do it. */
11329 enum_next_value = integer_zero_node;
7177d104 11330 enum_overflow = 0;
8d08fdba
MS
11331
11332 GNU_xref_decl (current_function_decl, enumtype);
11333 return enumtype;
11334}
11335
11336/* After processing and defining all the values of an enumeration type,
11337 install their decls in the enumeration type and finish it off.
11338 ENUMTYPE is the type object and VALUES a list of name-value pairs.
11339 Returns ENUMTYPE. */
11340
11341tree
11342finish_enum (enumtype, values)
11343 register tree enumtype, values;
11344{
a703fb38 11345 register tree minnode = NULL_TREE, maxnode = NULL_TREE;
8d08fdba
MS
11346 /* Calculate the maximum value of any enumerator in this type. */
11347
11348 if (values)
11349 {
f376e137
MS
11350 register tree pair;
11351 register tree value = DECL_INITIAL (TREE_VALUE (values));
5566b478 11352
5156628f 11353 if (! processing_template_decl)
5566b478
MS
11354 {
11355 /* Speed up the main loop by performing some precalculations */
11356 TREE_TYPE (TREE_VALUE (values)) = enumtype;
11357 TREE_TYPE (value) = enumtype;
11358 minnode = maxnode = value;
11359 }
f376e137 11360 TREE_VALUE (values) = value;
8d08fdba
MS
11361
11362 for (pair = TREE_CHAIN (values); pair; pair = TREE_CHAIN (pair))
11363 {
f376e137 11364 value = DECL_INITIAL (TREE_VALUE (pair));
5156628f 11365 if (! processing_template_decl)
5566b478
MS
11366 {
11367 TREE_TYPE (TREE_VALUE (pair)) = enumtype;
11368 TREE_TYPE (value) = enumtype;
11369 if (tree_int_cst_lt (maxnode, value))
11370 maxnode = value;
11371 else if (tree_int_cst_lt (value, minnode))
11372 minnode = value;
11373 }
f376e137 11374 TREE_VALUE (pair) = value;
8d08fdba
MS
11375 }
11376 }
f376e137
MS
11377 else
11378 maxnode = minnode = integer_zero_node;
8d08fdba 11379
de22184b 11380 TYPE_VALUES (enumtype) = nreverse (values);
2986ae00 11381
5156628f 11382 if (processing_template_decl)
b87692e5
MS
11383 {
11384 if (current_function_decl)
11385 {
11386 add_tree (build_min (TAG_DEFN, enumtype));
11387 resume_temporary_allocation ();
11388 }
11389 return enumtype;
11390 }
5566b478 11391
f376e137
MS
11392 {
11393 int unsignedp = tree_int_cst_sgn (minnode) >= 0;
11394 int lowprec = min_precision (minnode, unsignedp);
11395 int highprec = min_precision (maxnode, unsignedp);
11396 int precision = MAX (lowprec, highprec);
8d08fdba 11397
6060a796 11398 TYPE_SIZE (enumtype) = NULL_TREE;
13fbb038 11399
8ccc31eb 11400 /* Set TYPE_MIN_VALUE and TYPE_MAX_VALUE according to `precision'. */
8d08fdba 11401
f376e137 11402 TYPE_PRECISION (enumtype) = precision;
f376e137 11403 if (unsignedp)
8d08fdba 11404 fixup_unsigned_type (enumtype);
f376e137
MS
11405 else
11406 fixup_signed_type (enumtype);
8ccc31eb 11407
5566b478 11408 if (flag_short_enums || (precision > TYPE_PRECISION (integer_type_node)))
8ccc31eb
MS
11409 /* Use the width of the narrowest normal C type which is wide enough. */
11410 TYPE_PRECISION (enumtype) = TYPE_PRECISION (type_for_size
11411 (precision, 1));
11412 else
11413 TYPE_PRECISION (enumtype) = TYPE_PRECISION (integer_type_node);
11414
11415 TYPE_SIZE (enumtype) = 0;
11416 layout_type (enumtype);
f376e137 11417 }
8d08fdba 11418
f376e137
MS
11419 {
11420 register tree tem;
11421
11422 /* Fix up all variant types of this enum type. */
11423 for (tem = TYPE_MAIN_VARIANT (enumtype); tem;
11424 tem = TYPE_NEXT_VARIANT (tem))
11425 {
11426 TYPE_VALUES (tem) = TYPE_VALUES (enumtype);
11427 TYPE_MIN_VALUE (tem) = TYPE_MIN_VALUE (enumtype);
11428 TYPE_MAX_VALUE (tem) = TYPE_MAX_VALUE (enumtype);
11429 TYPE_SIZE (tem) = TYPE_SIZE (enumtype);
11430 TYPE_MODE (tem) = TYPE_MODE (enumtype);
11431 TYPE_PRECISION (tem) = TYPE_PRECISION (enumtype);
11432 TYPE_ALIGN (tem) = TYPE_ALIGN (enumtype);
11433 TREE_UNSIGNED (tem) = TREE_UNSIGNED (enumtype);
11434 }
11435 }
8d08fdba
MS
11436
11437 /* Finish debugging output for this type. */
2c73f9f5 11438 rest_of_type_compilation (enumtype, namespace_bindings_p ());
8d08fdba
MS
11439
11440 return enumtype;
11441}
11442
11443/* Build and install a CONST_DECL for one value of the
11444 current enumeration type (one that was begun with start_enum).
11445 Return a tree-list containing the name and its value.
11446 Assignment of sequential values by default is handled here. */
11447
11448tree
11449build_enumerator (name, value)
11450 tree name, value;
11451{
11452 tree decl, result;
8d08fdba
MS
11453
11454 /* Remove no-op casts from the value. */
11455 if (value)
11456 STRIP_TYPE_NOPS (value);
11457
5156628f 11458 if (! processing_template_decl)
5566b478
MS
11459 {
11460 /* Validate and default VALUE. */
11461 if (value != NULL_TREE)
11462 {
11463 if (TREE_READONLY_DECL_P (value))
e92cc029 11464 value = decl_constant_value (value);
5566b478
MS
11465
11466 if (TREE_CODE (value) == INTEGER_CST)
11467 {
11468 value = default_conversion (value);
11469 constant_expression_warning (value);
11470 }
11471 else
11472 {
11473 cp_error ("enumerator value for `%D' not integer constant", name);
11474 value = NULL_TREE;
11475 }
11476 }
11477
5566b478 11478 /* Default based on previous value. */
5156628f 11479 if (value == NULL_TREE && ! processing_template_decl)
5566b478
MS
11480 {
11481 value = enum_next_value;
11482 if (enum_overflow)
11483 cp_error ("overflow in enumeration values at `%D'", name);
11484 }
11485
11486 /* Remove no-op casts from the value. */
11487 if (value)
11488 STRIP_TYPE_NOPS (value);
11489
e92cc029
MS
11490 /* We have to always copy here; not all INTEGER_CSTs are unshared,
11491 and there's no wedding ring. Look at size_int()...*/
11492 value = copy_node (value);
013bc8af
MS
11493#if 0
11494 /* To fix MAX_VAL enum consts. (bkoz) */
e92cc029 11495 TREE_TYPE (value) = integer_type_node;
013bc8af 11496#endif
5566b478 11497 }
8d08fdba 11498
8d08fdba
MS
11499 /* C++ associates enums with global, function, or class declarations. */
11500
7177d104
MS
11501 decl = current_scope ();
11502 if (decl && decl == current_class_type)
8d08fdba
MS
11503 {
11504 /* This enum declaration is local to the class, so we must put
11505 it in that class's list of decls. */
11506 decl = build_lang_field_decl (CONST_DECL, name, integer_type_node);
11507 DECL_INITIAL (decl) = value;
11508 TREE_READONLY (decl) = 1;
11509 pushdecl_class_level (decl);
11510 TREE_CHAIN (decl) = current_local_enum;
11511 current_local_enum = decl;
11512 }
11513 else
11514 {
11515 /* It's a global enum, or it's local to a function. (Note local to
11516 a function could mean local to a class method. */
11517 decl = build_decl (CONST_DECL, name, integer_type_node);
11518 DECL_INITIAL (decl) = value;
42976354 11519 TREE_READONLY (decl) = 1;
8d08fdba
MS
11520
11521 pushdecl (decl);
11522 GNU_xref_decl (current_function_decl, decl);
11523 }
11524
5156628f 11525 if (! processing_template_decl)
5566b478
MS
11526 {
11527 /* Set basis for default for next value. */
11528 enum_next_value = build_binary_op_nodefault (PLUS_EXPR, value,
11529 integer_one_node, PLUS_EXPR);
11530 enum_overflow = tree_int_cst_lt (enum_next_value, value);
5566b478 11531 }
8d08fdba 11532
8d2733ca 11533 result = saveable_tree_cons (name, decl, NULL_TREE);
8d08fdba
MS
11534 return result;
11535}
11536
11537tree
86052cc3
JM
11538grok_enum_decls (decl)
11539 tree decl;
8d08fdba
MS
11540{
11541 tree d = current_local_enum;
11542
11543 if (d == NULL_TREE)
11544 return decl;
11545
11546 while (1)
11547 {
8d08fdba
MS
11548 if (TREE_CHAIN (d) == NULL_TREE)
11549 {
11550 TREE_CHAIN (d) = decl;
11551 break;
11552 }
11553 d = TREE_CHAIN (d);
11554 }
11555
11556 decl = current_local_enum;
11557 current_local_enum = NULL_TREE;
11558
11559 return decl;
11560}
11561\f
5566b478
MS
11562static int function_depth;
11563
8d08fdba
MS
11564/* Create the FUNCTION_DECL for a function definition.
11565 DECLSPECS and DECLARATOR are the parts of the declaration;
11566 they describe the function's name and the type it returns,
11567 but twisted together in a fashion that parallels the syntax of C.
11568
11569 This function creates a binding context for the function body
11570 as well as setting up the FUNCTION_DECL in current_function_decl.
11571
11572 Returns 1 on success. If the DECLARATOR is not suitable for a function
11573 (it defines a datum instead), we return 0, which tells
11574 yyparse to report a parse error.
11575
11576 For C++, we must first check whether that datum makes any sense.
11577 For example, "class A local_a(1,2);" means that variable local_a
11578 is an aggregate of type A, which should have a constructor
11579 applied to it with the argument list [1, 2].
11580
11581 @@ There is currently no way to retrieve the storage
11582 @@ allocated to FUNCTION (or all of its parms) if we return
11583 @@ something we had previously. */
11584
11585int
c11b6f21
MS
11586start_function (declspecs, declarator, attrs, pre_parsed_p)
11587 tree declspecs, declarator, attrs;
8d08fdba
MS
11588 int pre_parsed_p;
11589{
5566b478 11590 tree decl1;
8d08fdba
MS
11591 tree ctype = NULL_TREE;
11592 tree fntype;
11593 tree restype;
11594 extern int have_extern_spec;
11595 extern int used_extern_spec;
11596 int doing_friend = 0;
11597
8d08fdba 11598 /* Sanity check. */
a1774733 11599 my_friendly_assert (TREE_CODE (TREE_VALUE (void_list_node)) == VOID_TYPE, 160);
8d08fdba
MS
11600 my_friendly_assert (TREE_CHAIN (void_list_node) == NULL_TREE, 161);
11601
e92cc029 11602 /* Assume, until we see it does. */
8d08fdba
MS
11603 current_function_returns_value = 0;
11604 current_function_returns_null = 0;
11605 warn_about_return_type = 0;
fd378c9d
JM
11606 named_labels = 0;
11607 shadowed_labels = 0;
8d08fdba
MS
11608 current_function_assigns_this = 0;
11609 current_function_just_assigned_this = 0;
11610 current_function_parms_stored = 0;
11611 original_result_rtx = NULL_RTX;
a9aedbc2 11612 base_init_expr = NULL_TREE;
79ff2c6c
MS
11613 current_base_init_list = NULL_TREE;
11614 current_member_init_list = NULL_TREE;
f30432d7 11615 ctor_label = dtor_label = NULL_TREE;
42976354 11616 static_labelno = 0;
8d08fdba
MS
11617
11618 clear_temp_name ();
11619
e92cc029 11620 /* This should only be done once on the top most decl. */
8d08fdba
MS
11621 if (have_extern_spec && !used_extern_spec)
11622 {
11623 declspecs = decl_tree_cons (NULL_TREE, get_identifier ("extern"), declspecs);
11624 used_extern_spec = 1;
11625 }
11626
11627 if (pre_parsed_p)
11628 {
11629 decl1 = declarator;
11630
11631 if (! DECL_ARGUMENTS (decl1)
11632 && !DECL_STATIC_FUNCTION_P (decl1)
6eb3bb27 11633 && DECL_CLASS_SCOPE_P (decl1)
d2e5ee5c
MS
11634 && TYPE_IDENTIFIER (DECL_CONTEXT (decl1))
11635 && IDENTIFIER_TEMPLATE (TYPE_IDENTIFIER (DECL_CONTEXT (decl1))))
8d08fdba 11636 {
30394414
JM
11637 tree binding = binding_for_name (DECL_NAME (decl1),
11638 current_namespace);
8d08fdba
MS
11639 cp_error ("redeclaration of `%#D'", decl1);
11640 if (IDENTIFIER_CLASS_VALUE (DECL_NAME (decl1)))
11641 cp_error_at ("previous declaration here", IDENTIFIER_CLASS_VALUE (DECL_NAME (decl1)));
30394414
JM
11642 else if (BINDING_VALUE (binding))
11643 cp_error_at ("previous declaration here", BINDING_VALUE (binding));
8d08fdba
MS
11644 }
11645
8d08fdba
MS
11646 fntype = TREE_TYPE (decl1);
11647 if (TREE_CODE (fntype) == METHOD_TYPE)
11648 ctype = TYPE_METHOD_BASETYPE (fntype);
11649
11650 /* ANSI C++ June 5 1992 WP 11.4.5. A friend function defined in a
11651 class is in the (lexical) scope of the class in which it is
11652 defined. */
11653 if (!ctype && DECL_FRIEND_P (decl1))
11654 {
11655 ctype = DECL_CLASS_CONTEXT (decl1);
11656
11657 /* CTYPE could be null here if we're dealing with a template;
11658 for example, `inline friend float foo()' inside a template
11659 will have no CTYPE set. */
11660 if (ctype && TREE_CODE (ctype) != RECORD_TYPE)
11661 ctype = NULL_TREE;
11662 else
11663 doing_friend = 1;
11664 }
11665
8d08fdba 11666 /* In a fcn definition, arg types must be complete. */
5566b478 11667 require_complete_types_for_parms (DECL_ARGUMENTS (decl1));
9fffd093
RK
11668
11669 /* In case some arg types were completed since the declaration was
11670 parsed, fix up the decls. */
11671 {
5566b478 11672 tree t = DECL_ARGUMENTS (decl1);
9fffd093
RK
11673 for (; t; t = TREE_CHAIN (t))
11674 layout_decl (t, 0);
11675 }
5566b478
MS
11676
11677 last_function_parms = DECL_ARGUMENTS (decl1);
11678 last_function_parm_tags = NULL_TREE;
8d08fdba
MS
11679 }
11680 else
11681 {
c11b6f21 11682 decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, 1, NULL_TREE);
8d08fdba
MS
11683 /* If the declarator is not suitable for a function definition,
11684 cause a syntax error. */
11685 if (decl1 == NULL_TREE || TREE_CODE (decl1) != FUNCTION_DECL) return 0;
11686
11687 fntype = TREE_TYPE (decl1);
11688
11689 restype = TREE_TYPE (fntype);
11690 if (IS_AGGR_TYPE (restype) && ! TYPE_PTRMEMFUNC_P (restype)
11691 && ! CLASSTYPE_GOT_SEMICOLON (restype))
11692 {
11693 cp_error ("semicolon missing after declaration of `%#T'", restype);
e66d884e 11694 shadow_tag (build_expr_list (NULL_TREE, restype));
8d08fdba
MS
11695 CLASSTYPE_GOT_SEMICOLON (restype) = 1;
11696 if (TREE_CODE (fntype) == FUNCTION_TYPE)
11697 fntype = build_function_type (integer_type_node,
11698 TYPE_ARG_TYPES (fntype));
11699 else
11700 fntype = build_cplus_method_type (build_type_variant (TYPE_METHOD_BASETYPE (fntype), TREE_READONLY (decl1), TREE_SIDE_EFFECTS (decl1)),
11701 integer_type_node,
11702 TYPE_ARG_TYPES (fntype));
11703 TREE_TYPE (decl1) = fntype;
11704 }
11705
11706 if (TREE_CODE (fntype) == METHOD_TYPE)
11707 ctype = TYPE_METHOD_BASETYPE (fntype);
11708 else if (IDENTIFIER_LENGTH (DECL_NAME (decl1)) == 4
11709 && ! strcmp (IDENTIFIER_POINTER (DECL_NAME (decl1)), "main")
11710 && DECL_CONTEXT (decl1) == NULL_TREE)
11711 {
11712 /* If this doesn't return integer_type, complain. */
11713 if (TREE_TYPE (TREE_TYPE (decl1)) != integer_type_node)
11714 {
a28e3c7f 11715 if (pedantic || warn_return_type)
5566b478 11716 pedwarn ("return type for `main' changed to `int'");
8d08fdba
MS
11717 TREE_TYPE (decl1) = fntype = default_function_type;
11718 }
11719 warn_about_return_type = 0;
11720 }
11721 }
11722
11723 /* Warn if function was previously implicitly declared
11724 (but not if we warned then). */
11725 if (! warn_implicit
11726 && IDENTIFIER_IMPLICIT_DECL (DECL_NAME (decl1)) != NULL_TREE)
11727 cp_warning_at ("`%D' implicitly declared before its definition", IDENTIFIER_IMPLICIT_DECL (DECL_NAME (decl1)));
11728
11729 current_function_decl = decl1;
5566b478
MS
11730 /* Save the parm names or decls from this function's declarator
11731 where store_parm_decls will find them. */
11732 current_function_parms = last_function_parms;
11733 current_function_parm_tags = last_function_parm_tags;
8d08fdba 11734
9e9ff709 11735 announce_function (decl1);
8d08fdba 11736
5156628f 11737 if (! processing_template_decl)
8d08fdba 11738 {
5566b478
MS
11739 if (TYPE_SIZE (complete_type (TREE_TYPE (fntype))) == NULL_TREE)
11740 {
11741 cp_error ("return-type `%#T' is an incomplete type",
11742 TREE_TYPE (fntype));
8d08fdba 11743
5566b478
MS
11744 /* Make it return void instead, but don't change the
11745 type of the DECL_RESULT, in case we have a named return value. */
11746 if (ctype)
11747 TREE_TYPE (decl1)
11748 = build_cplus_method_type (build_type_variant (ctype,
11749 TREE_READONLY (decl1),
11750 TREE_SIDE_EFFECTS (decl1)),
11751 void_type_node,
11752 FUNCTION_ARG_CHAIN (decl1));
11753 else
11754 TREE_TYPE (decl1)
11755 = build_function_type (void_type_node,
11756 TYPE_ARG_TYPES (TREE_TYPE (decl1)));
11757 DECL_RESULT (decl1)
11758 = build_decl (RESULT_DECL, 0, TYPE_MAIN_VARIANT (TREE_TYPE (fntype)));
11759 TREE_READONLY (DECL_RESULT (decl1)) = TYPE_READONLY (TREE_TYPE (fntype));
11760 TREE_THIS_VOLATILE (DECL_RESULT (decl1)) = TYPE_VOLATILE (TREE_TYPE (fntype));
11761 }
11762
11763 if (TYPE_LANG_SPECIFIC (TREE_TYPE (fntype))
11764 && CLASSTYPE_ABSTRACT_VIRTUALS (TREE_TYPE (fntype)))
11765 abstract_virtuals_error (decl1, TREE_TYPE (fntype));
11766 }
b7484fbe 11767
8d08fdba 11768 if (warn_about_return_type)
59f1f2c4 11769 pedwarn ("return-type defaults to `int'");
8d08fdba 11770
824b9a4c 11771 /* Effective C++ rule 15. See also c_expand_return. */
eb448459 11772 if (warn_ecpp
824b9a4c 11773 && DECL_NAME (decl1) == ansi_opname[(int) MODIFY_EXPR]
a1774733 11774 && TREE_CODE (TREE_TYPE (fntype)) == VOID_TYPE)
824b9a4c
MS
11775 cp_warning ("`operator=' should return a reference to `*this'");
11776
8d08fdba
MS
11777 /* Make the init_value nonzero so pushdecl knows this is not tentative.
11778 error_mark_node is replaced below (in poplevel) with the BLOCK. */
11779 DECL_INITIAL (decl1) = error_mark_node;
11780
8d08fdba
MS
11781 /* This function exists in static storage.
11782 (This does not mean `static' in the C sense!) */
11783 TREE_STATIC (decl1) = 1;
11784
878cd289
MS
11785 /* Record the decl so that the function name is defined.
11786 If we already have a decl for this name, and it is a FUNCTION_DECL,
11787 use the old decl. */
11788
5156628f 11789 if (processing_template_decl)
3ac3d9ea 11790 decl1 = push_template_decl (decl1);
5566b478 11791 else if (pre_parsed_p == 0)
878cd289 11792 {
75650646 11793 /* A specialization is not used to guide overload resolution. */
959d8796
JM
11794 if ((flag_guiding_decls
11795 || !DECL_TEMPLATE_SPECIALIZATION (decl1))
11796 && ! DECL_FUNCTION_MEMBER_P (decl1))
75650646 11797 decl1 = pushdecl (decl1);
2c73f9f5
ML
11798 else
11799 /* We need to set the DECL_CONTEXT. */
11800 if (!DECL_CONTEXT (decl1) && DECL_TEMPLATE_INFO (decl1))
11801 DECL_CONTEXT (decl1) = DECL_CONTEXT (DECL_TI_TEMPLATE (decl1));
878cd289
MS
11802 DECL_MAIN_VARIANT (decl1) = decl1;
11803 fntype = TREE_TYPE (decl1);
11804 }
5566b478
MS
11805
11806 current_function_decl = decl1;
878cd289 11807
db5ae43f 11808 if (DECL_INTERFACE_KNOWN (decl1))
faae18ab 11809 {
86052cc3
JM
11810 tree ctx = hack_decl_function_context (decl1);
11811
faae18ab
MS
11812 if (DECL_NOT_REALLY_EXTERN (decl1))
11813 DECL_EXTERNAL (decl1) = 0;
86052cc3
JM
11814
11815 if (ctx != NULL_TREE && DECL_THIS_INLINE (ctx)
11816 && TREE_PUBLIC (ctx))
11817 /* This is a function in a local class in an extern inline
11818 function. */
11819 comdat_linkage (decl1);
faae18ab 11820 }
8d08fdba
MS
11821 /* If this function belongs to an interface, it is public.
11822 If it belongs to someone else's interface, it is also external.
11823 It doesn't matter whether it's inline or not. */
5566b478
MS
11824 else if (interface_unknown == 0
11825 && (! DECL_TEMPLATE_INSTANTIATION (decl1)
d11ad92e 11826 || flag_alt_external_templates))
8d08fdba 11827 {
5566b478 11828 if (DECL_THIS_INLINE (decl1) || DECL_TEMPLATE_INSTANTIATION (decl1)
5156628f 11829 || processing_template_decl)
db5ae43f
MS
11830 DECL_EXTERNAL (decl1)
11831 = (interface_only
faae18ab 11832 || (DECL_THIS_INLINE (decl1) && ! flag_implement_inlines));
db5ae43f 11833 else
893de33c 11834 DECL_EXTERNAL (decl1) = 0;
e8abc66f 11835 DECL_NOT_REALLY_EXTERN (decl1) = 0;
db5ae43f 11836 DECL_INTERFACE_KNOWN (decl1) = 1;
8d08fdba
MS
11837 }
11838 else
a0a33927
MS
11839 {
11840 /* This is a definition, not a reference.
b7484fbe
MS
11841 So clear DECL_EXTERNAL. */
11842 DECL_EXTERNAL (decl1) = 0;
faae18ab 11843
5566b478
MS
11844 if ((DECL_THIS_INLINE (decl1) || DECL_TEMPLATE_INSTANTIATION (decl1))
11845 && ! DECL_INTERFACE_KNOWN (decl1)
11846 /* Don't try to defer nested functions for now. */
e76a2646 11847 && ! hack_decl_function_context (decl1))
878cd289
MS
11848 DECL_DEFER_OUTPUT (decl1) = 1;
11849 else
893de33c 11850 DECL_INTERFACE_KNOWN (decl1) = 1;
db5ae43f 11851 }
a9aedbc2 11852
8d08fdba
MS
11853 if (ctype != NULL_TREE && DECL_STATIC_FUNCTION_P (decl1))
11854 {
11855 if (TREE_CODE (fntype) == METHOD_TYPE)
11856 TREE_TYPE (decl1) = fntype
11857 = build_function_type (TREE_TYPE (fntype),
11858 TREE_CHAIN (TYPE_ARG_TYPES (fntype)));
5566b478
MS
11859 current_function_parms = TREE_CHAIN (current_function_parms);
11860 DECL_ARGUMENTS (decl1) = current_function_parms;
8d08fdba
MS
11861 ctype = NULL_TREE;
11862 }
11863 restype = TREE_TYPE (fntype);
11864
8d08fdba
MS
11865 if (ctype)
11866 {
11867 push_nested_class (ctype, 1);
11868
11869 /* If we're compiling a friend function, neither of the variables
4ac14744 11870 current_class_ptr nor current_class_type will have values. */
8d08fdba
MS
11871 if (! doing_friend)
11872 {
11873 /* We know that this was set up by `grokclassfn'.
11874 We do not wait until `store_parm_decls', since evil
11875 parse errors may never get us to that point. Here
11876 we keep the consistency between `current_class_type'
4ac14744 11877 and `current_class_ptr'. */
5566b478 11878 tree t = current_function_parms;
5f34005f
RK
11879
11880 my_friendly_assert (t != NULL_TREE
11881 && TREE_CODE (t) == PARM_DECL, 162);
11882
dff6b454
RK
11883 if (TREE_CODE (TREE_TYPE (t)) == POINTER_TYPE)
11884 {
11885 int i = suspend_momentary ();
11886
11887 /* Fool build_indirect_ref. */
4ac14744
MS
11888 current_class_ptr = NULL_TREE;
11889 current_class_ref = build_indirect_ref (t, NULL_PTR);
11890 current_class_ptr = t;
dff6b454
RK
11891 resume_momentary (i);
11892 }
11893 else
11894 /* We're having a signature pointer here. */
4ac14744 11895 current_class_ref = current_class_ptr = t;
dff6b454 11896
8d08fdba
MS
11897 }
11898 }
11899 else
11900 {
11901 if (DECL_STATIC_FUNCTION_P (decl1))
11902 push_nested_class (DECL_CONTEXT (decl1), 2);
11903 else
11904 push_memoized_context (0, 1);
4ac14744 11905 current_class_ptr = current_class_ref = NULL_TREE;
8d08fdba
MS
11906 }
11907
db5ae43f
MS
11908 pushlevel (0);
11909 current_binding_level->parm_flag = 1;
11910
db5ae43f
MS
11911 GNU_xref_function (decl1, current_function_parms);
11912
f30432d7
MS
11913 if (attrs)
11914 cplus_decl_attributes (decl1, NULL_TREE, attrs);
db5ae43f
MS
11915 make_function_rtl (decl1);
11916
8d08fdba
MS
11917 /* Promote the value to int before returning it. */
11918 if (C_PROMOTING_INTEGER_TYPE_P (restype))
5566b478
MS
11919 restype = type_promotes_to (restype);
11920
11921 /* If this fcn was already referenced via a block-scope `extern' decl
11922 (or an implicit decl), propagate certain information about the usage. */
11923 if (TREE_ADDRESSABLE (DECL_ASSEMBLER_NAME (decl1)))
11924 TREE_ADDRESSABLE (decl1) = 1;
11925
8d08fdba 11926 if (DECL_RESULT (decl1) == NULL_TREE)
f30432d7
MS
11927 {
11928 DECL_RESULT (decl1)
11929 = build_decl (RESULT_DECL, 0, TYPE_MAIN_VARIANT (restype));
11930 TREE_READONLY (DECL_RESULT (decl1)) = TYPE_READONLY (restype);
11931 TREE_THIS_VOLATILE (DECL_RESULT (decl1)) = TYPE_VOLATILE (restype);
11932 }
8d08fdba 11933
948edfa7
JW
11934 /* Allocate further tree nodes temporarily during compilation
11935 of this function only. Tiemann moved up here from bottom of fn. */
11936 /* If this is a nested function, then we must continue to allocate RTL
11937 on the permanent obstack in case we need to inline it later. */
11938 if (! hack_decl_function_context (decl1))
5566b478
MS
11939 temporary_allocation ();
11940
5156628f 11941 if (processing_template_decl)
5566b478 11942 {
5566b478
MS
11943 ++minimal_parse_mode;
11944 last_tree = DECL_SAVED_TREE (decl1)
11945 = build_nt (EXPR_STMT, void_zero_node);
11946 }
11947
11948 ++function_depth;
11949
f30432d7
MS
11950 if (DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (decl1))
11951 && DECL_LANGUAGE (decl1) == lang_cplusplus)
8d08fdba
MS
11952 {
11953 dtor_label = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE);
11954 ctor_label = NULL_TREE;
11955 }
11956 else
11957 {
11958 dtor_label = NULL_TREE;
11959 if (DECL_CONSTRUCTOR_P (decl1))
11960 ctor_label = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE);
11961 }
11962
8d08fdba
MS
11963 return 1;
11964}
11965\f
9664b89e
JM
11966/* Called after store_parm_decls for a function-try-block. We need to update
11967 last_parm_cleanup_insn so that the base initializers for a constructor
11968 are run within this block, not before it. */
6467930b
MS
11969
11970void
11971expand_start_early_try_stmts ()
11972{
6467930b 11973 expand_start_try_stmts ();
9664b89e 11974 last_parm_cleanup_insn = get_last_insn ();
f30432d7
MS
11975}
11976
8d08fdba
MS
11977/* Store the parameter declarations into the current function declaration.
11978 This is called after parsing the parameter declarations, before
11979 digesting the body of the function.
11980
11981 Also install to binding contour return value identifier, if any. */
11982
11983void
11984store_parm_decls ()
11985{
11986 register tree fndecl = current_function_decl;
11987 register tree parm;
11988 int parms_have_cleanups = 0;
eb66be0e 11989 tree cleanups = NULL_TREE;
8d08fdba
MS
11990
11991 /* This is either a chain of PARM_DECLs (when a prototype is used). */
11992 tree specparms = current_function_parms;
11993
11994 /* This is a list of types declared among parms in a prototype. */
11995 tree parmtags = current_function_parm_tags;
11996
11997 /* This is a chain of any other decls that came in among the parm
11998 declarations. If a parm is declared with enum {foo, bar} x;
11999 then CONST_DECLs for foo and bar are put here. */
12000 tree nonparms = NULL_TREE;
12001
a9aedbc2 12002 if (toplevel_bindings_p ())
8d08fdba
MS
12003 fatal ("parse errors have confused me too much");
12004
12005 /* Initialize RTL machinery. */
12006 init_function_start (fndecl, input_filename, lineno);
12007
12008 /* Declare __FUNCTION__ and __PRETTY_FUNCTION__ for this function. */
12009 declare_function_name ();
12010
12011 /* Create a binding level for the parms. */
12012 expand_start_bindings (0);
12013
8d08fdba
MS
12014 if (specparms != NULL_TREE)
12015 {
12016 /* This case is when the function was defined with an ANSI prototype.
12017 The parms already have decls, so we need not do anything here
12018 except record them as in effect
12019 and complain if any redundant old-style parm decls were written. */
12020
12021 register tree next;
12022
12023 /* Must clear this because it might contain TYPE_DECLs declared
12024 at class level. */
12025 storedecls (NULL_TREE);
5566b478 12026
8d08fdba
MS
12027 for (parm = nreverse (specparms); parm; parm = next)
12028 {
12029 next = TREE_CHAIN (parm);
12030 if (TREE_CODE (parm) == PARM_DECL)
12031 {
5566b478 12032 tree cleanup;
8d08fdba
MS
12033 if (DECL_NAME (parm) == NULL_TREE)
12034 {
8d08fdba 12035 pushdecl (parm);
8d08fdba 12036 }
a1774733 12037 else if (TREE_CODE (TREE_TYPE (parm)) == VOID_TYPE)
8d08fdba
MS
12038 cp_error ("parameter `%D' declared void", parm);
12039 else
12040 {
12041 /* Now fill in DECL_REFERENCE_SLOT for any of the parm decls.
12042 A parameter is assumed not to have any side effects.
12043 If this should change for any reason, then this
12044 will have to wrap the bashed reference type in a save_expr.
12045
12046 Also, if the parameter type is declared to be an X
12047 and there is an X(X&) constructor, we cannot lay it
12048 into the stack (any more), so we make this parameter
12049 look like it is really of reference type. Functions
12050 which pass parameters to this function will know to
12051 create a temporary in their frame, and pass a reference
12052 to that. */
12053
12054 if (TREE_CODE (TREE_TYPE (parm)) == REFERENCE_TYPE
12055 && TYPE_SIZE (TREE_TYPE (TREE_TYPE (parm))))
12056 SET_DECL_REFERENCE_SLOT (parm, convert_from_reference (parm));
12057
12058 pushdecl (parm);
12059 }
5156628f 12060 if (! processing_template_decl
5566b478 12061 && (cleanup = maybe_build_cleanup (parm), cleanup))
8d08fdba
MS
12062 {
12063 expand_decl (parm);
8d08fdba 12064 parms_have_cleanups = 1;
eb66be0e
MS
12065
12066 /* Keep track of the cleanups. */
12067 cleanups = tree_cons (parm, cleanup, cleanups);
8d08fdba
MS
12068 }
12069 }
12070 else
12071 {
12072 /* If we find an enum constant or a type tag,
12073 put it aside for the moment. */
12074 TREE_CHAIN (parm) = NULL_TREE;
12075 nonparms = chainon (nonparms, parm);
12076 }
12077 }
12078
12079 /* Get the decls in their original chain order
12080 and record in the function. This is all and only the
12081 PARM_DECLs that were pushed into scope by the loop above. */
12082 DECL_ARGUMENTS (fndecl) = getdecls ();
12083
12084 storetags (chainon (parmtags, gettags ()));
12085 }
12086 else
12087 DECL_ARGUMENTS (fndecl) = NULL_TREE;
12088
12089 /* Now store the final chain of decls for the arguments
12090 as the decl-chain of the current lexical scope.
12091 Put the enumerators in as well, at the front so that
12092 DECL_ARGUMENTS is not modified. */
12093
12094 storedecls (chainon (nonparms, DECL_ARGUMENTS (fndecl)));
12095
12096 /* Initialize the RTL code for the function. */
12097 DECL_SAVED_INSNS (fndecl) = NULL_RTX;
5156628f 12098 if (! processing_template_decl)
5566b478 12099 expand_function_start (fndecl, parms_have_cleanups);
8d08fdba 12100
eb448459
MS
12101 current_function_parms_stored = 1;
12102
12103 /* If this function is `main', emit a call to `__main'
12104 to run global initializers, etc. */
12105 if (DECL_NAME (fndecl)
12106 && IDENTIFIER_LENGTH (DECL_NAME (fndecl)) == 4
12107 && strcmp (IDENTIFIER_POINTER (DECL_NAME (fndecl)), "main") == 0
2c73f9f5 12108 && DECL_CONTEXT (fndecl) == global_namespace)
eb448459
MS
12109 {
12110 expand_main_function ();
12111 }
12112
eb66be0e
MS
12113 /* Now that we have initialized the parms, we can start their
12114 cleanups. We cannot do this before, since expand_decl_cleanup
12115 should not be called before the parm can be used. */
eb448459 12116 if (cleanups
eb66be0e
MS
12117 && ! processing_template_decl)
12118 {
12119 for (cleanups = nreverse (cleanups); cleanups; cleanups = TREE_CHAIN (cleanups))
12120 {
12121 if (! expand_decl_cleanup (TREE_PURPOSE (cleanups), TREE_VALUE (cleanups)))
12122 cp_error ("parser lost in parsing declaration of `%D'",
12123 TREE_PURPOSE (cleanups));
12124 }
12125 }
12126
8d08fdba
MS
12127 /* Create a binding contour which can be used to catch
12128 cleanup-generated temporaries. Also, if the return value needs or
12129 has initialization, deal with that now. */
12130 if (parms_have_cleanups)
12131 {
12132 pushlevel (0);
12133 expand_start_bindings (0);
12134 }
12135
5156628f 12136 if (! processing_template_decl && flag_exceptions)
f30432d7 12137 {
f30432d7
MS
12138 /* Do the starting of the exception specifications, if we have any. */
12139 if (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (current_function_decl)))
12140 expand_start_eh_spec ();
f30432d7 12141 }
eb448459 12142
5c825fc2 12143 last_parm_cleanup_insn = get_last_insn ();
72b7eeff 12144 last_dtor_insn = get_last_insn ();
8d08fdba
MS
12145}
12146
12147/* Bind a name and initialization to the return value of
12148 the current function. */
e92cc029 12149
8d08fdba
MS
12150void
12151store_return_init (return_id, init)
12152 tree return_id, init;
12153{
12154 tree decl = DECL_RESULT (current_function_decl);
12155
e1cd6e56 12156 if (pedantic)
8d08fdba
MS
12157 /* Give this error as many times as there are occurrences,
12158 so that users can use Emacs compilation buffers to find
12159 and fix all such places. */
8926095f 12160 pedwarn ("ANSI C++ does not permit named return values");
8d08fdba
MS
12161
12162 if (return_id != NULL_TREE)
12163 {
12164 if (DECL_NAME (decl) == NULL_TREE)
12165 {
12166 DECL_NAME (decl) = return_id;
12167 DECL_ASSEMBLER_NAME (decl) = return_id;
12168 }
12169 else
e76a2646 12170 cp_error ("return identifier `%D' already in place", decl);
8d08fdba
MS
12171 }
12172
12173 /* Can't let this happen for constructors. */
12174 if (DECL_CONSTRUCTOR_P (current_function_decl))
12175 {
12176 error ("can't redefine default return value for constructors");
12177 return;
12178 }
12179
12180 /* If we have a named return value, put that in our scope as well. */
12181 if (DECL_NAME (decl) != NULL_TREE)
12182 {
12183 /* If this named return value comes in a register,
12184 put it in a pseudo-register. */
12185 if (DECL_REGISTER (decl))
12186 {
12187 original_result_rtx = DECL_RTL (decl);
12188 DECL_RTL (decl) = gen_reg_rtx (DECL_MODE (decl));
12189 }
12190
82580166 12191 /* Let `cp_finish_decl' know that this initializer is ok. */
8d08fdba
MS
12192 DECL_INITIAL (decl) = init;
12193 pushdecl (decl);
e76a2646
MS
12194
12195 if (minimal_parse_mode)
12196 add_tree (build_min_nt (RETURN_INIT, return_id,
12197 copy_to_permanent (init)));
12198 else
dae1d6f6 12199 cp_finish_decl (decl, init, NULL_TREE, 0, 0);
8d08fdba
MS
12200 }
12201}
12202
8d08fdba
MS
12203\f
12204/* Finish up a function declaration and compile that function
12205 all the way to assembler language output. The free the storage
12206 for the function definition.
12207
12208 This is called after parsing the body of the function definition.
12209 LINENO is the current line number.
12210
12211 C++: CALL_POPLEVEL is non-zero if an extra call to poplevel
12212 (and expand_end_bindings) must be made to take care of the binding
12213 contour for the base initializers. This is only relevant for
e92cc029
MS
12214 constructors.
12215
12216 NESTED is nonzero if we were in the middle of compiling another function
12217 when we started on this one. */
8d08fdba
MS
12218
12219void
db5ae43f 12220finish_function (lineno, call_poplevel, nested)
8d08fdba
MS
12221 int lineno;
12222 int call_poplevel;
db5ae43f 12223 int nested;
8d08fdba
MS
12224{
12225 register tree fndecl = current_function_decl;
12226 tree fntype, ctype = NULL_TREE;
f30432d7 12227 rtx last_parm_insn, insns;
8d08fdba
MS
12228 /* Label to use if this function is supposed to return a value. */
12229 tree no_return_label = NULL_TREE;
12230 tree decls = NULL_TREE;
12231
12232 /* When we get some parse errors, we can end up without a
12233 current_function_decl, so cope. */
12234 if (fndecl == NULL_TREE)
12235 return;
12236
e92cc029
MS
12237 if (! nested && function_depth > 1)
12238 nested = 1;
12239
8d08fdba
MS
12240 fntype = TREE_TYPE (fndecl);
12241
12242/* TREE_READONLY (fndecl) = 1;
12243 This caused &foo to be of type ptr-to-const-function
12244 which then got a warning when stored in a ptr-to-function variable. */
12245
12246 /* This happens on strange parse errors. */
12247 if (! current_function_parms_stored)
12248 {
12249 call_poplevel = 0;
12250 store_parm_decls ();
12251 }
12252
5156628f 12253 if (processing_template_decl)
8d08fdba 12254 {
5566b478
MS
12255 if (DECL_CONSTRUCTOR_P (fndecl) && call_poplevel)
12256 {
12257 decls = getdecls ();
12258 expand_end_bindings (decls, decls != NULL_TREE, 0);
12259 poplevel (decls != NULL_TREE, 0, 0);
12260 }
12261 }
12262 else
12263 {
12264 if (write_symbols != NO_DEBUG /*&& TREE_CODE (fntype) != METHOD_TYPE*/)
8d08fdba 12265 {
5566b478
MS
12266 tree ttype = target_type (fntype);
12267 tree parmdecl;
12268
8d08fdba
MS
12269 if (IS_AGGR_TYPE (ttype))
12270 /* Let debugger know it should output info for this type. */
12271 note_debug_info_needed (ttype);
5566b478
MS
12272
12273 for (parmdecl = DECL_ARGUMENTS (fndecl); parmdecl; parmdecl = TREE_CHAIN (parmdecl))
12274 {
12275 ttype = target_type (TREE_TYPE (parmdecl));
12276 if (IS_AGGR_TYPE (ttype))
12277 /* Let debugger know it should output info for this type. */
12278 note_debug_info_needed (ttype);
12279 }
8d08fdba 12280 }
8d08fdba 12281
5566b478
MS
12282 /* Clean house because we will need to reorder insns here. */
12283 do_pending_stack_adjust ();
8d08fdba 12284
5566b478 12285 if (dtor_label)
8d08fdba 12286 {
5566b478
MS
12287 tree binfo = TYPE_BINFO (current_class_type);
12288 tree cond = integer_one_node;
12289 tree exprstmt;
12290 tree in_charge_node = lookup_name (in_charge_identifier, 0);
12291 tree virtual_size;
12292 int ok_to_optimize_dtor = 0;
12293 int empty_dtor = get_last_insn () == last_dtor_insn;
8d08fdba 12294
5566b478
MS
12295 if (current_function_assigns_this)
12296 cond = build (NE_EXPR, boolean_type_node,
4ac14744 12297 current_class_ptr, integer_zero_node);
5566b478
MS
12298 else
12299 {
12300 int n_baseclasses = CLASSTYPE_N_BASECLASSES (current_class_type);
12301
12302 /* If this destructor is empty, then we don't need to check
12303 whether `this' is NULL in some cases. */
12304 if ((flag_this_is_variable & 1) == 0)
12305 ok_to_optimize_dtor = 1;
12306 else if (empty_dtor)
12307 ok_to_optimize_dtor
12308 = (n_baseclasses == 0
12309 || (n_baseclasses == 1
12310 && TYPE_HAS_DESTRUCTOR (TYPE_BINFO_BASETYPE (current_class_type, 0))));
12311 }
8d08fdba 12312
5566b478
MS
12313 /* These initializations might go inline. Protect
12314 the binding level of the parms. */
12315 pushlevel (0);
12316 expand_start_bindings (0);
8d08fdba 12317
5566b478
MS
12318 if (current_function_assigns_this)
12319 {
12320 current_function_assigns_this = 0;
12321 current_function_just_assigned_this = 0;
12322 }
8d08fdba 12323
5566b478
MS
12324 /* Generate the code to call destructor on base class.
12325 If this destructor belongs to a class with virtual
12326 functions, then set the virtual function table
12327 pointer to represent the type of our base class. */
8d08fdba 12328
5566b478
MS
12329 /* This side-effect makes call to `build_delete' generate the
12330 code we have to have at the end of this destructor. */
12331 TYPE_HAS_DESTRUCTOR (current_class_type) = 0;
8d08fdba 12332
5566b478
MS
12333 /* These are two cases where we cannot delegate deletion. */
12334 if (TYPE_USES_VIRTUAL_BASECLASSES (current_class_type)
12335 || TYPE_GETS_REG_DELETE (current_class_type))
4ac14744 12336 exprstmt = build_delete (current_class_type, current_class_ref, integer_zero_node,
e92cc029 12337 LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR|LOOKUP_NORMAL, 0);
5566b478 12338 else
4ac14744 12339 exprstmt = build_delete (current_class_type, current_class_ref, in_charge_node,
e92cc029 12340 LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR|LOOKUP_NORMAL, 0);
5566b478
MS
12341
12342 /* If we did not assign to this, then `this' is non-zero at
12343 the end of a destructor. As a special optimization, don't
12344 emit test if this is an empty destructor. If it does nothing,
12345 it does nothing. If it calls a base destructor, the base
12346 destructor will perform the test. */
12347
12348 if (exprstmt != error_mark_node
12349 && (TREE_CODE (exprstmt) != NOP_EXPR
12350 || TREE_OPERAND (exprstmt, 0) != integer_zero_node
12351 || TYPE_USES_VIRTUAL_BASECLASSES (current_class_type)))
8d08fdba 12352 {
5566b478
MS
12353 expand_label (dtor_label);
12354 if (cond != integer_one_node)
12355 expand_start_cond (cond, 0);
12356 if (exprstmt != void_zero_node)
12357 /* Don't call `expand_expr_stmt' if we're not going to do
12358 anything, since -Wall will give a diagnostic. */
12359 expand_expr_stmt (exprstmt);
12360
12361 /* Run destructor on all virtual baseclasses. */
12362 if (TYPE_USES_VIRTUAL_BASECLASSES (current_class_type))
8d08fdba 12363 {
5566b478
MS
12364 tree vbases = nreverse (copy_list (CLASSTYPE_VBASECLASSES (current_class_type)));
12365 expand_start_cond (build (BIT_AND_EXPR, integer_type_node,
12366 in_charge_node, integer_two_node), 0);
12367 while (vbases)
8d08fdba 12368 {
5566b478
MS
12369 if (TYPE_NEEDS_DESTRUCTOR (BINFO_TYPE (vbases)))
12370 {
bd6dd845
MS
12371 tree vb = get_vbase
12372 (BINFO_TYPE (vbases),
12373 TYPE_BINFO (current_class_type));
e92cc029 12374 expand_expr_stmt
bd6dd845
MS
12375 (build_scoped_method_call
12376 (current_class_ref, vb, dtor_identifier,
e66d884e 12377 build_expr_list (NULL_TREE, integer_zero_node)));
5566b478
MS
12378 }
12379 vbases = TREE_CHAIN (vbases);
8d08fdba 12380 }
5566b478 12381 expand_end_cond ();
8d08fdba 12382 }
5566b478
MS
12383
12384 do_pending_stack_adjust ();
12385 if (cond != integer_one_node)
12386 expand_end_cond ();
8d08fdba
MS
12387 }
12388
5566b478
MS
12389 TYPE_HAS_DESTRUCTOR (current_class_type) = 1;
12390
12391 virtual_size = c_sizeof (current_class_type);
12392
12393 /* At the end, call delete if that's what's requested. */
12394 if (TYPE_GETS_REG_DELETE (current_class_type))
12395 /* This NOP_EXPR means we are in a static call context. */
beb53fb8
JM
12396 exprstmt
12397 = build_method_call (build_indirect_ref (build1 (NOP_EXPR,
12398 build_pointer_type (current_class_type),
12399 error_mark_node),
12400 NULL_PTR),
12401 ansi_opname[(int) DELETE_EXPR],
e66d884e
JM
12402 expr_tree_cons (NULL_TREE, current_class_ptr,
12403 build_expr_list (NULL_TREE, virtual_size)),
beb53fb8 12404 NULL_TREE, LOOKUP_NORMAL);
5566b478 12405 else if (TYPE_USES_VIRTUAL_BASECLASSES (current_class_type))
4ac14744 12406 exprstmt = build_x_delete (ptr_type_node, current_class_ptr, 0,
5566b478
MS
12407 virtual_size);
12408 else
12409 exprstmt = NULL_TREE;
8d08fdba 12410
5566b478
MS
12411 if (exprstmt)
12412 {
12413 cond = build (BIT_AND_EXPR, integer_type_node,
12414 in_charge_node, integer_one_node);
12415 expand_start_cond (cond, 0);
12416 expand_expr_stmt (exprstmt);
12417 expand_end_cond ();
12418 }
8d08fdba 12419
5566b478 12420 /* End of destructor. */
fc378698 12421 expand_end_bindings (NULL_TREE, getdecls () != NULL_TREE, 0);
5566b478
MS
12422 poplevel (2, 0, 0); /* XXX change to 1 */
12423
12424 /* Back to the top of destructor. */
956d6950 12425 /* Don't execute destructor code if `this' is NULL. */
5566b478
MS
12426
12427 start_sequence ();
12428
12429 /* If the dtor is empty, and we know there is not possible way we
12430 could use any vtable entries, before they are possibly set by
12431 a base class dtor, we don't have to setup the vtables, as we
12432 know that any base class dtoring will set up any vtables it
12433 needs. We avoid MI, because one base class dtor can do a
12434 virtual dispatch to an overridden function that would need to
12435 have a non-related vtable set up, we cannot avoid setting up
12436 vtables in that case. We could change this to see if there is
12437 just one vtable. */
12438 if (! empty_dtor || TYPE_USES_COMPLEX_INHERITANCE (current_class_type))
12439 {
12440 /* Make all virtual function table pointers in non-virtual base
12441 classes point to CURRENT_CLASS_TYPE's virtual function
12442 tables. */
4ac14744 12443 expand_direct_vtbls_init (binfo, binfo, 1, 0, current_class_ptr);
8d08fdba 12444
5566b478 12445 if (TYPE_USES_VIRTUAL_BASECLASSES (current_class_type))
4ac14744 12446 expand_indirect_vtbls_init (binfo, current_class_ref, current_class_ptr);
5566b478
MS
12447 }
12448
12449 if (! ok_to_optimize_dtor)
12450 {
12451 cond = build_binary_op (NE_EXPR,
4ac14744 12452 current_class_ptr, integer_zero_node, 1);
5566b478
MS
12453 expand_start_cond (cond, 0);
12454 }
8d08fdba 12455
5566b478
MS
12456 insns = get_insns ();
12457 end_sequence ();
f30432d7 12458
5566b478
MS
12459 last_parm_insn = get_first_nonparm_insn ();
12460 if (last_parm_insn == NULL_RTX)
12461 last_parm_insn = get_last_insn ();
12462 else
12463 last_parm_insn = previous_insn (last_parm_insn);
8d08fdba 12464
5566b478 12465 emit_insns_after (insns, last_parm_insn);
72b7eeff 12466
5566b478
MS
12467 if (! ok_to_optimize_dtor)
12468 expand_end_cond ();
72b7eeff 12469 }
5566b478 12470 else if (current_function_assigns_this)
8d08fdba 12471 {
5566b478
MS
12472 /* Does not need to call emit_base_init, because
12473 that is done (if needed) just after assignment to this
12474 is seen. */
12475
12476 if (DECL_CONSTRUCTOR_P (current_function_decl))
12477 {
12478 end_protect_partials ();
12479 expand_label (ctor_label);
12480 ctor_label = NULL_TREE;
12481
12482 if (call_poplevel)
12483 {
12484 decls = getdecls ();
12485 expand_end_bindings (decls, decls != NULL_TREE, 0);
12486 poplevel (decls != NULL_TREE, 0, 0);
12487 }
4ac14744 12488 c_expand_return (current_class_ptr);
5566b478 12489 }
a1774733 12490 else if (TREE_CODE (TREE_TYPE (DECL_RESULT (current_function_decl))) != VOID_TYPE
5566b478
MS
12491 && return_label != NULL_RTX)
12492 no_return_label = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE);
12493
12494 current_function_assigns_this = 0;
12495 current_function_just_assigned_this = 0;
12496 base_init_expr = NULL_TREE;
8d08fdba 12497 }
5566b478
MS
12498 else if (DECL_CONSTRUCTOR_P (fndecl))
12499 {
a703fb38 12500 tree cond = NULL_TREE, thenclause = NULL_TREE;
5566b478
MS
12501 /* Allow constructor for a type to get a new instance of the object
12502 using `build_new'. */
12503 tree abstract_virtuals = CLASSTYPE_ABSTRACT_VIRTUALS (current_class_type);
12504 CLASSTYPE_ABSTRACT_VIRTUALS (current_class_type) = NULL_TREE;
f30432d7 12505
5566b478 12506 DECL_RETURNS_FIRST_ARG (fndecl) = 1;
f30432d7 12507
5566b478
MS
12508 if (flag_this_is_variable > 0)
12509 {
12510 cond = build_binary_op (EQ_EXPR,
4ac14744
MS
12511 current_class_ptr, integer_zero_node, 1);
12512 thenclause = build_modify_expr (current_class_ptr, NOP_EXPR,
5566b478
MS
12513 build_new (NULL_TREE, current_class_type, void_type_node, 0));
12514 }
f30432d7 12515
5566b478 12516 CLASSTYPE_ABSTRACT_VIRTUALS (current_class_type) = abstract_virtuals;
f30432d7 12517
5566b478
MS
12518 start_sequence ();
12519
12520 if (flag_this_is_variable > 0)
12521 {
12522 expand_start_cond (cond, 0);
12523 expand_expr_stmt (thenclause);
12524 expand_end_cond ();
12525 }
12526
12527 /* Emit insns from `emit_base_init' which sets up virtual
12528 function table pointer(s). */
12529 if (base_init_expr)
12530 {
12531 expand_expr_stmt (base_init_expr);
12532 base_init_expr = NULL_TREE;
12533 }
12534
12535 insns = get_insns ();
12536 end_sequence ();
12537
b87692e5 12538 /* This is where the body of the constructor begins. */
5566b478 12539
b87692e5 12540 emit_insns_after (insns, last_parm_cleanup_insn);
8d08fdba 12541
b7484fbe 12542 end_protect_partials ();
5566b478
MS
12543
12544 /* This is where the body of the constructor ends. */
8d08fdba
MS
12545 expand_label (ctor_label);
12546 ctor_label = NULL_TREE;
12547
12548 if (call_poplevel)
12549 {
12550 decls = getdecls ();
8d08fdba 12551 expand_end_bindings (decls, decls != NULL_TREE, 0);
5566b478 12552 poplevel (decls != NULL_TREE, 1, 0);
8d08fdba 12553 }
8d08fdba 12554
4ac14744 12555 c_expand_return (current_class_ptr);
8d08fdba 12556
5566b478
MS
12557 current_function_assigns_this = 0;
12558 current_function_just_assigned_this = 0;
12559 }
12560 else if (IDENTIFIER_LENGTH (DECL_NAME (fndecl)) == 4
12561 && ! strcmp (IDENTIFIER_POINTER (DECL_NAME (fndecl)), "main")
2c73f9f5 12562 && DECL_CONTEXT (fndecl) == global_namespace)
8d08fdba 12563 {
5566b478
MS
12564 /* Make it so that `main' always returns 0 by default. */
12565#ifdef VMS
12566 c_expand_return (integer_one_node);
12567#else
12568 c_expand_return (integer_zero_node);
12569#endif
8d08fdba 12570 }
5566b478
MS
12571 else if (return_label != NULL_RTX
12572 && current_function_return_value == NULL_TREE
12573 && ! DECL_NAME (DECL_RESULT (current_function_decl)))
12574 no_return_label = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE);
8d08fdba 12575
eb448459
MS
12576 if (flag_exceptions)
12577 expand_exception_blocks ();
12578
5566b478
MS
12579 /* If this function is supposed to return a value, ensure that
12580 we do not fall into the cleanups by mistake. The end of our
12581 function will look like this:
12582
12583 user code (may have return stmt somewhere)
12584 goto no_return_label
12585 cleanup_label:
12586 cleanups
12587 goto return_label
12588 no_return_label:
12589 NOTE_INSN_FUNCTION_END
12590 return_label:
12591 things for return
12592
12593 If the user omits a return stmt in the USER CODE section, we
12594 will have a control path which reaches NOTE_INSN_FUNCTION_END.
12595 Otherwise, we won't. */
12596 if (no_return_label)
8d08fdba 12597 {
5566b478
MS
12598 DECL_CONTEXT (no_return_label) = fndecl;
12599 DECL_INITIAL (no_return_label) = error_mark_node;
12600 DECL_SOURCE_FILE (no_return_label) = input_filename;
12601 DECL_SOURCE_LINE (no_return_label) = lineno;
12602 expand_goto (no_return_label);
8d08fdba
MS
12603 }
12604
5566b478 12605 if (cleanup_label)
a9aedbc2 12606 {
2c73f9f5 12607 /* Remove the binding contour which is used
5566b478
MS
12608 to catch cleanup-generated temporaries. */
12609 expand_end_bindings (0, 0, 0);
12610 poplevel (0, 0, 0);
8d08fdba 12611
eb448459
MS
12612 /* Emit label at beginning of cleanup code for parameters. */
12613 emit_label (cleanup_label);
12614 }
b7484fbe 12615
5566b478
MS
12616 /* Get return value into register if that's where it's supposed to be. */
12617 if (original_result_rtx)
12618 fixup_result_decl (DECL_RESULT (fndecl), original_result_rtx);
8d08fdba 12619
5566b478
MS
12620 /* Finish building code that will trigger warnings if users forget
12621 to make their functions return values. */
12622 if (no_return_label || cleanup_label)
12623 emit_jump (return_label);
12624 if (no_return_label)
8d08fdba 12625 {
5566b478
MS
12626 /* We don't need to call `expand_*_return' here because we
12627 don't need any cleanups here--this path of code is only
12628 for error checking purposes. */
12629 expand_label (no_return_label);
8d08fdba
MS
12630 }
12631
5566b478
MS
12632 /* Generate rtl for function exit. */
12633 expand_function_end (input_filename, lineno, 1);
8d08fdba 12634 }
8d2733ca 12635
8d08fdba
MS
12636 /* This must come after expand_function_end because cleanups might
12637 have declarations (from inline functions) that need to go into
12638 this function's blocks. */
12639 if (current_binding_level->parm_flag != 1)
12640 my_friendly_abort (122);
12641 poplevel (1, 0, 1);
12642
2c73f9f5 12643 /* Reset scope for C++: if we were in the scope of a class,
db5ae43f
MS
12644 then when we finish this function, we are not longer so.
12645 This cannot be done until we know for sure that no more
12646 class members will ever be referenced in this function
12647 (i.e., calls to destructors). */
12648 if (current_class_name)
12649 {
12650 ctype = current_class_type;
12651 pop_nested_class (1);
12652 }
12653 else
12654 pop_memoized_context (1);
12655
8d08fdba 12656 /* Must mark the RESULT_DECL as being in this function. */
c73964b2 12657 DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
8d08fdba 12658
8d08fdba
MS
12659 /* Set the BLOCK_SUPERCONTEXT of the outermost function scope to point
12660 to the FUNCTION_DECL node itself. */
12661 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
12662
5156628f 12663 if (! processing_template_decl)
5566b478 12664 {
75650646
MM
12665 int saved_flag_keep_inline_functions =
12666 flag_keep_inline_functions;
12667
5566b478
MS
12668 /* So we can tell if jump_optimize sets it to 1. */
12669 can_reach_end = 0;
8d08fdba 12670
75650646 12671 if (DECL_CONTEXT (fndecl) != NULL_TREE
e1467ff2 12672 && hack_decl_function_context (fndecl))
75650646
MM
12673 /* Trick rest_of_compilation into not deferring output of this
12674 function, even if it is inline, since the rtl_obstack for
12675 this function is the function_obstack of the enclosing
12676 function and will be deallocated when the enclosing
12677 function is gone. See save_tree_status. */
12678 flag_keep_inline_functions = 1;
12679
6633d636
MS
12680 /* Run the optimizers and output the assembler code for this
12681 function. */
12682
12683 if (DECL_ARTIFICIAL (fndecl))
12684 {
12685 /* Do we really *want* to inline this synthesized method? */
12686
12687 int save_fif = flag_inline_functions;
12688 flag_inline_functions = 1;
12689
12690 /* Turn off DECL_INLINE for the moment so function_cannot_inline_p
12691 will check our size. */
12692 DECL_INLINE (fndecl) = 0;
12693
12694 rest_of_compilation (fndecl);
12695 flag_inline_functions = save_fif;
12696 }
12697 else
12698 rest_of_compilation (fndecl);
8d08fdba 12699
75650646
MM
12700 flag_keep_inline_functions = saved_flag_keep_inline_functions;
12701
5566b478
MS
12702 if (DECL_SAVED_INSNS (fndecl) && ! TREE_ASM_WRITTEN (fndecl))
12703 {
12704 /* Set DECL_EXTERNAL so that assemble_external will be called as
12705 necessary. We'll clear it again in finish_file. */
12706 if (! DECL_EXTERNAL (fndecl))
12707 DECL_NOT_REALLY_EXTERN (fndecl) = 1;
12708 DECL_EXTERNAL (fndecl) = 1;
12709 mark_inline_for_output (fndecl);
12710 }
8926095f 12711
d2e5ee5c
MS
12712 if (ctype && TREE_ASM_WRITTEN (fndecl))
12713 note_debug_info_needed (ctype);
12714
5566b478 12715 current_function_returns_null |= can_reach_end;
8d08fdba 12716
5566b478
MS
12717 /* Since we don't normally go through c_expand_return for constructors,
12718 this normally gets the wrong value.
12719 Also, named return values have their return codes emitted after
12720 NOTE_INSN_FUNCTION_END, confusing jump.c. */
12721 if (DECL_CONSTRUCTOR_P (fndecl)
12722 || DECL_NAME (DECL_RESULT (fndecl)) != NULL_TREE)
12723 current_function_returns_null = 0;
8d08fdba 12724
5566b478
MS
12725 if (TREE_THIS_VOLATILE (fndecl) && current_function_returns_null)
12726 cp_warning ("`noreturn' function `%D' does return", fndecl);
12727 else if ((warn_return_type || pedantic)
12728 && current_function_returns_null
a1774733 12729 && TREE_CODE (TREE_TYPE (fntype)) != VOID_TYPE)
5566b478
MS
12730 {
12731 /* If this function returns non-void and control can drop through,
12732 complain. */
14e403d4 12733 cp_warning ("control reaches end of non-void function `%D'", fndecl);
5566b478
MS
12734 }
12735 /* With just -W, complain only if function returns both with
12736 and without a value. */
12737 else if (extra_warnings
12738 && current_function_returns_value && current_function_returns_null)
12739 warning ("this function may return with or without a value");
8d08fdba 12740 }
5566b478
MS
12741
12742 --function_depth;
8d08fdba
MS
12743
12744 /* Free all the tree nodes making up this function. */
12745 /* Switch back to allocating nodes permanently
12746 until we start another function. */
5156628f 12747 if (processing_template_decl)
5566b478
MS
12748 {
12749 --minimal_parse_mode;
12750 DECL_SAVED_TREE (fndecl) = TREE_CHAIN (DECL_SAVED_TREE (fndecl));
12751 }
12752
db5ae43f
MS
12753 if (! nested)
12754 permanent_allocation (1);
8d08fdba 12755
8d08fdba
MS
12756 if (DECL_SAVED_INSNS (fndecl) == NULL_RTX)
12757 {
d2e5ee5c
MS
12758 tree t;
12759
8d08fdba
MS
12760 /* Stop pointing to the local nodes about to be freed. */
12761 /* But DECL_INITIAL must remain nonzero so we know this
12762 was an actual function definition. */
12763 DECL_INITIAL (fndecl) = error_mark_node;
d2e5ee5c 12764 for (t = DECL_ARGUMENTS (fndecl); t; t = TREE_CHAIN (t))
de22184b 12765 DECL_RTL (t) = DECL_INCOMING_RTL (t) = NULL_RTX;
8d08fdba
MS
12766 }
12767
e1cd6e56
MS
12768 if (DECL_STATIC_CONSTRUCTOR (fndecl))
12769 static_ctors = perm_tree_cons (NULL_TREE, fndecl, static_ctors);
12770 if (DECL_STATIC_DESTRUCTOR (fndecl))
12771 static_dtors = perm_tree_cons (NULL_TREE, fndecl, static_dtors);
12772
28cbf42c
MS
12773 if (! nested)
12774 {
12775 /* Let the error reporting routines know that we're outside a
12776 function. For a nested function, this value is used in
12777 pop_cp_function_context and then reset via pop_function_context. */
12778 current_function_decl = NULL_TREE;
12779 }
12780
e349ee73 12781 named_label_uses = NULL;
4ac14744
MS
12782 current_class_ptr = NULL_TREE;
12783 current_class_ref = NULL_TREE;
8d08fdba
MS
12784}
12785\f
12786/* Create the FUNCTION_DECL for a function definition.
12787 LINE1 is the line number that the definition absolutely begins on.
12788 LINE2 is the line number that the name of the function appears on.
12789 DECLSPECS and DECLARATOR are the parts of the declaration;
12790 they describe the return type and the name of the function,
12791 but twisted together in a fashion that parallels the syntax of C.
12792
12793 This function creates a binding context for the function body
12794 as well as setting up the FUNCTION_DECL in current_function_decl.
12795
12796 Returns a FUNCTION_DECL on success.
12797
12798 If the DECLARATOR is not suitable for a function (it defines a datum
12799 instead), we return 0, which tells yyparse to report a parse error.
12800
12801 May return void_type_node indicating that this method is actually
12802 a friend. See grokfield for more details.
12803
12804 Came here with a `.pushlevel' .
12805
12806 DO NOT MAKE ANY CHANGES TO THIS CODE WITHOUT MAKING CORRESPONDING
12807 CHANGES TO CODE IN `grokfield'. */
e92cc029 12808
8d08fdba 12809tree
c11b6f21
MS
12810start_method (declspecs, declarator)
12811 tree declarator, declspecs;
8d08fdba 12812{
c11b6f21 12813 tree fndecl = grokdeclarator (declarator, declspecs, MEMFUNCDEF, 0,
f30432d7 12814 NULL_TREE);
8d08fdba
MS
12815
12816 /* Something too ugly to handle. */
12817 if (fndecl == NULL_TREE)
12818 return NULL_TREE;
12819
12820 /* Pass friends other than inline friend functions back. */
a1774733 12821 if (fndecl == void_type_node)
8d08fdba
MS
12822 return fndecl;
12823
12824 if (TREE_CODE (fndecl) != FUNCTION_DECL)
12825 /* Not a function, tell parser to report parse error. */
12826 return NULL_TREE;
12827
12828 if (IS_SIGNATURE (current_class_type))
824b9a4c 12829 IS_DEFAULT_IMPLEMENTATION (fndecl) = 1;
8d08fdba
MS
12830
12831 if (DECL_IN_AGGR_P (fndecl))
12832 {
12833 if (IDENTIFIER_ERROR_LOCUS (DECL_ASSEMBLER_NAME (fndecl)) != current_class_type)
12834 {
2c73f9f5
ML
12835 if (DECL_CONTEXT (fndecl)
12836 && TREE_CODE( DECL_CONTEXT (fndecl)) != NAMESPACE_DECL)
8d08fdba
MS
12837 cp_error ("`%D' is already defined in class %s", fndecl,
12838 TYPE_NAME_STRING (DECL_CONTEXT (fndecl)));
12839 }
12840 return void_type_node;
12841 }
12842
faae18ab
MS
12843 DECL_THIS_INLINE (fndecl) = 1;
12844
8926095f 12845 if (flag_default_inline)
8d08fdba
MS
12846 DECL_INLINE (fndecl) = 1;
12847
75650646 12848 if (processing_template_decl)
3ac3d9ea 12849 fndecl = push_template_decl (fndecl);
a0a33927 12850
8d08fdba
MS
12851 /* We read in the parameters on the maybepermanent_obstack,
12852 but we won't be getting back to them until after we
12853 may have clobbered them. So the call to preserve_data
12854 will keep them safe. */
12855 preserve_data ();
12856
12857 if (! DECL_FRIEND_P (fndecl))
12858 {
8d08fdba
MS
12859 if (TREE_CHAIN (fndecl))
12860 {
12861 fndecl = copy_node (fndecl);
12862 TREE_CHAIN (fndecl) = NULL_TREE;
12863 }
12864
12865 if (DECL_CONSTRUCTOR_P (fndecl))
a0a33927
MS
12866 {
12867 if (! grok_ctor_properties (current_class_type, fndecl))
12868 return void_type_node;
12869 }
8d08fdba
MS
12870 else if (IDENTIFIER_OPNAME_P (DECL_NAME (fndecl)))
12871 grok_op_properties (fndecl, DECL_VIRTUAL_P (fndecl), 0);
12872 }
12873
82580166 12874 cp_finish_decl (fndecl, NULL_TREE, NULL_TREE, 0, 0);
8d08fdba
MS
12875
12876 /* Make a place for the parms */
12877 pushlevel (0);
12878 current_binding_level->parm_flag = 1;
12879
12880 DECL_IN_AGGR_P (fndecl) = 1;
12881 return fndecl;
12882}
12883
12884/* Go through the motions of finishing a function definition.
12885 We don't compile this method until after the whole class has
12886 been processed.
12887
12888 FINISH_METHOD must return something that looks as though it
12889 came from GROKFIELD (since we are defining a method, after all).
12890
12891 This is called after parsing the body of the function definition.
12892 STMTS is the chain of statements that makes up the function body.
12893
12894 DECL is the ..._DECL that `start_method' provided. */
12895
12896tree
12897finish_method (decl)
12898 tree decl;
12899{
12900 register tree fndecl = decl;
12901 tree old_initial;
8d08fdba
MS
12902
12903 register tree link;
12904
a1774733 12905 if (decl == void_type_node)
8d08fdba
MS
12906 return decl;
12907
12908 old_initial = DECL_INITIAL (fndecl);
12909
12910 /* Undo the level for the parms (from start_method).
12911 This is like poplevel, but it causes nothing to be
12912 saved. Saving information here confuses symbol-table
12913 output routines. Besides, this information will
12914 be correctly output when this method is actually
12915 compiled. */
12916
12917 /* Clear out the meanings of the local variables of this level;
12918 also record in each decl which block it belongs to. */
12919
12920 for (link = current_binding_level->names; link; link = TREE_CHAIN (link))
12921 {
12922 if (DECL_NAME (link) != NULL_TREE)
12923 IDENTIFIER_LOCAL_VALUE (DECL_NAME (link)) = 0;
12924 my_friendly_assert (TREE_CODE (link) != FUNCTION_DECL, 163);
12925 DECL_CONTEXT (link) = NULL_TREE;
12926 }
12927
12928 /* Restore all name-meanings of the outer levels
12929 that were shadowed by this level. */
12930
12931 for (link = current_binding_level->shadowed; link; link = TREE_CHAIN (link))
12932 IDENTIFIER_LOCAL_VALUE (TREE_PURPOSE (link)) = TREE_VALUE (link);
12933 for (link = current_binding_level->class_shadowed;
12934 link; link = TREE_CHAIN (link))
12935 IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (link)) = TREE_VALUE (link);
12936 for (link = current_binding_level->type_shadowed;
12937 link; link = TREE_CHAIN (link))
2c73f9f5 12938 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (link), TREE_VALUE (link));
8d08fdba
MS
12939
12940 GNU_xref_end_scope ((HOST_WIDE_INT) current_binding_level,
12941 (HOST_WIDE_INT) current_binding_level->level_chain,
12942 current_binding_level->parm_flag,
5566b478 12943 current_binding_level->keep);
8d08fdba
MS
12944
12945 poplevel (0, 0, 0);
12946
12947 DECL_INITIAL (fndecl) = old_initial;
12948
12949 /* We used to check if the context of FNDECL was different from
12950 current_class_type as another way to get inside here. This didn't work
12951 for String.cc in libg++. */
12952 if (DECL_FRIEND_P (fndecl))
12953 {
12954 CLASSTYPE_INLINE_FRIENDS (current_class_type)
12955 = tree_cons (NULL_TREE, fndecl, CLASSTYPE_INLINE_FRIENDS (current_class_type));
12956 decl = void_type_node;
12957 }
12958
12959 return decl;
12960}
12961\f
12962/* Called when a new struct TYPE is defined.
12963 If this structure or union completes the type of any previous
12964 variable declaration, lay it out and output its rtl. */
12965
12966void
12967hack_incomplete_structures (type)
12968 tree type;
12969{
f30432d7 12970 tree *list;
8d08fdba 12971
f30432d7 12972 if (current_binding_level->incomplete == NULL_TREE)
8d08fdba
MS
12973 return;
12974
12975 if (!type) /* Don't do this for class templates. */
12976 return;
12977
f30432d7
MS
12978 for (list = &current_binding_level->incomplete; *list; )
12979 {
12980 tree decl = TREE_VALUE (*list);
a703fb38 12981 if ((decl && TREE_TYPE (decl) == type)
f30432d7
MS
12982 || (TREE_TYPE (decl)
12983 && TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
12984 && TREE_TYPE (TREE_TYPE (decl)) == type))
12985 {
12986 int toplevel = toplevel_bindings_p ();
12987 if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
12988 && TREE_TYPE (TREE_TYPE (decl)) == type)
12989 layout_type (TREE_TYPE (decl));
12990 layout_decl (decl, 0);
12991 rest_of_decl_compilation (decl, NULL_PTR, toplevel, 0);
12992 if (! toplevel)
12993 {
12994 tree cleanup;
12995 expand_decl (decl);
12996 cleanup = maybe_build_cleanup (decl);
12997 expand_decl_init (decl);
e349ee73 12998 if (! expand_decl_cleanup (decl, cleanup))
f30432d7
MS
12999 cp_error ("parser lost in parsing declaration of `%D'",
13000 decl);
13001 }
13002 *list = TREE_CHAIN (*list);
13003 }
13004 else
13005 list = &TREE_CHAIN (*list);
13006 }
8d08fdba
MS
13007}
13008
8d08fdba 13009/* If DECL is of a type which needs a cleanup, build that cleanup here.
c73964b2 13010 See build_delete for information about AUTO_DELETE.
8d08fdba
MS
13011
13012 Don't build these on the momentary obstack; they must live
13013 the life of the binding contour. */
e92cc029 13014
c73964b2
MS
13015static tree
13016maybe_build_cleanup_1 (decl, auto_delete)
13017 tree decl, auto_delete;
8d08fdba
MS
13018{
13019 tree type = TREE_TYPE (decl);
13020 if (TYPE_NEEDS_DESTRUCTOR (type))
13021 {
13022 int temp = 0, flags = LOOKUP_NORMAL|LOOKUP_DESTRUCTOR;
13023 tree rval;
8d08fdba
MS
13024
13025 if (TREE_CODE (decl) != PARM_DECL)
13026 temp = suspend_momentary ();
13027
13028 if (TREE_CODE (type) == ARRAY_TYPE)
13029 rval = decl;
13030 else
13031 {
13032 mark_addressable (decl);
13033 rval = build_unary_op (ADDR_EXPR, decl, 0);
13034 }
13035
13036 /* Optimize for space over speed here. */
13037 if (! TYPE_USES_VIRTUAL_BASECLASSES (type)
13038 || flag_expensive_optimizations)
13039 flags |= LOOKUP_NONVIRTUAL;
13040
c73964b2 13041 rval = build_delete (TREE_TYPE (rval), rval, auto_delete, flags, 0);
8d08fdba
MS
13042
13043 if (TYPE_USES_VIRTUAL_BASECLASSES (type)
13044 && ! TYPE_HAS_DESTRUCTOR (type))
e66d884e
JM
13045 rval = build_compound_expr (expr_tree_cons (NULL_TREE, rval,
13046 build_expr_list (NULL_TREE, build_vbase_delete (type, decl))));
8d08fdba 13047
8d08fdba
MS
13048 if (TREE_CODE (decl) != PARM_DECL)
13049 resume_momentary (temp);
13050
8d08fdba
MS
13051 return rval;
13052 }
13053 return 0;
13054}
c73964b2
MS
13055
13056/* If DECL is of a type which needs a cleanup, build that cleanup
13057 here. The cleanup does free the storage with a call to delete. */
13058
13059tree
13060maybe_build_cleanup_and_delete (decl)
13061 tree decl;
13062{
13063 return maybe_build_cleanup_1 (decl, integer_three_node);
13064}
13065
13066/* If DECL is of a type which needs a cleanup, build that cleanup
13067 here. The cleanup does not free the storage with a call a delete. */
13068
13069tree
13070maybe_build_cleanup (decl)
13071 tree decl;
13072{
13073 return maybe_build_cleanup_1 (decl, integer_two_node);
13074}
8d08fdba
MS
13075\f
13076/* Expand a C++ expression at the statement level.
13077 This is needed to ferret out nodes which have UNKNOWN_TYPE.
13078 The C++ type checker should get all of these out when
13079 expressions are combined with other, type-providing, expressions,
13080 leaving only orphan expressions, such as:
13081
e92cc029 13082 &class::bar; / / takes its address, but does nothing with it. */
8d08fdba 13083
8d08fdba
MS
13084void
13085cplus_expand_expr_stmt (exp)
13086 tree exp;
13087{
5156628f 13088 if (processing_template_decl)
5566b478
MS
13089 {
13090 add_tree (build_min_nt (EXPR_STMT, exp));
13091 return;
13092 }
13093
eb66be0e
MS
13094 /* Arrange for all temps to disappear. */
13095 expand_start_target_temps ();
13096
8d08fdba
MS
13097 if (TREE_TYPE (exp) == unknown_type_node)
13098 {
13099 if (TREE_CODE (exp) == ADDR_EXPR || TREE_CODE (exp) == TREE_LIST)
13100 error ("address of overloaded function with no contextual type information");
13101 else if (TREE_CODE (exp) == COMPONENT_REF)
13102 warning ("useless reference to a member function name, did you forget the ()?");
13103 }
13104 else
13105 {
8d08fdba
MS
13106 if (TREE_CODE (exp) == FUNCTION_DECL)
13107 {
13108 cp_warning ("reference, not call, to function `%D'", exp);
13109 warning ("at this point in file");
13110 }
8d08fdba 13111
63c68bb7
MS
13112#if 0
13113 /* We should do this eventually, but right now this causes regex.o from
13114 libg++ to miscompile, and tString to core dump. */
5b605f68 13115 exp = build1 (CLEANUP_POINT_EXPR, TREE_TYPE (exp), exp);
63c68bb7 13116#endif
b3e401c2
JM
13117
13118 /* Strip unused implicit INDIRECT_REFs of references. */
13119 if (TREE_CODE (exp) == INDIRECT_REF
13120 && TREE_CODE (TREE_TYPE (TREE_OPERAND (exp, 0))) == REFERENCE_TYPE)
13121 exp = TREE_OPERAND (exp, 0);
13122
d2e5ee5c
MS
13123 /* If we don't do this, we end up down inside expand_expr
13124 trying to do TYPE_MODE on the ERROR_MARK, and really
13125 go outside the bounds of the type. */
13126 if (exp != error_mark_node)
13127 expand_expr_stmt (break_out_cleanups (exp));
8d08fdba
MS
13128 }
13129
13130 /* Clean up any pending cleanups. This happens when a function call
13131 returns a cleanup-needing value that nobody uses. */
eb66be0e 13132 expand_end_target_temps ();
8d08fdba
MS
13133}
13134
13135/* When a stmt has been parsed, this function is called.
13136
13137 Currently, this function only does something within a
13138 constructor's scope: if a stmt has just assigned to this,
13139 and we are in a derived class, we call `emit_base_init'. */
13140
13141void
13142finish_stmt ()
13143{
13144 extern struct nesting *cond_stack, *loop_stack, *case_stack;
13145
13146
13147 if (current_function_assigns_this
13148 || ! current_function_just_assigned_this)
13149 return;
13150 if (DECL_CONSTRUCTOR_P (current_function_decl))
13151 {
13152 /* Constructors must wait until we are out of control
13153 zones before calling base constructors. */
13154 if (cond_stack || loop_stack || case_stack)
13155 return;
a9aedbc2 13156 expand_expr_stmt (base_init_expr);
8d08fdba
MS
13157 check_base_init (current_class_type);
13158 }
13159 current_function_assigns_this = 1;
8d08fdba
MS
13160}
13161
8d08fdba 13162/* Change a static member function definition into a FUNCTION_TYPE, instead
700f8a87
MS
13163 of the METHOD_TYPE that we create when it's originally parsed.
13164
13165 WARNING: DO NOT pass &TREE_TYPE (decl) to FN or &TYPE_ARG_TYPES
13166 (TREE_TYPE (decl)) to ARGTYPES, as doing so will corrupt the types of
13167 other decls. Either pass the addresses of local variables or NULL. */
13168
8857f91e 13169void
700f8a87
MS
13170revert_static_member_fn (decl, fn, argtypes)
13171 tree *decl, *fn, *argtypes;
8d08fdba 13172{
700f8a87
MS
13173 tree tmp;
13174 tree function = fn ? *fn : TREE_TYPE (*decl);
13175 tree args = argtypes ? *argtypes : TYPE_ARG_TYPES (function);
8d08fdba 13176
f30432d7
MS
13177 if (TYPE_READONLY (TREE_TYPE (TREE_VALUE (args))))
13178 cp_error ("static member function `%#D' declared const", *decl);
13179 if (TYPE_VOLATILE (TREE_TYPE (TREE_VALUE (args))))
13180 cp_error ("static member function `%#D' declared volatile", *decl);
13181
700f8a87
MS
13182 args = TREE_CHAIN (args);
13183 tmp = build_function_type (TREE_TYPE (function), args);
8d08fdba
MS
13184 tmp = build_type_variant (tmp, TYPE_READONLY (function),
13185 TYPE_VOLATILE (function));
f30432d7 13186 tmp = build_exception_variant (tmp,
8d08fdba
MS
13187 TYPE_RAISES_EXCEPTIONS (function));
13188 TREE_TYPE (*decl) = tmp;
f30432d7
MS
13189 if (DECL_ARGUMENTS (*decl))
13190 DECL_ARGUMENTS (*decl) = TREE_CHAIN (DECL_ARGUMENTS (*decl));
8d08fdba 13191 DECL_STATIC_FUNCTION_P (*decl) = 1;
700f8a87
MS
13192 if (fn)
13193 *fn = tmp;
13194 if (argtypes)
13195 *argtypes = args;
8d08fdba 13196}
a4443a08
MS
13197
13198int
13199id_in_current_class (id)
13200 tree id;
13201{
13202 return !!purpose_member (id, class_binding_level->class_shadowed);
13203}
db5ae43f
MS
13204
13205struct cp_function
13206{
13207 int returns_value;
13208 int returns_null;
13209 int warn_about_return_type;
db5ae43f
MS
13210 int assigns_this;
13211 int just_assigned_this;
13212 int parms_stored;
7215f9a0 13213 int temp_name_counter;
db5ae43f
MS
13214 tree named_labels;
13215 tree shadowed_labels;
13216 tree ctor_label;
13217 tree dtor_label;
72b7eeff 13218 rtx last_dtor_insn;
b87692e5 13219 rtx last_parm_cleanup_insn;
79ff2c6c
MS
13220 tree base_init_list;
13221 tree member_init_list;
a9aedbc2 13222 tree base_init_expr;
4ac14744
MS
13223 tree current_class_ptr;
13224 tree current_class_ref;
db5ae43f
MS
13225 rtx result_rtx;
13226 struct cp_function *next;
13227 struct binding_level *binding_level;
42976354 13228 int static_labelno;
db5ae43f
MS
13229};
13230
bd6dd845 13231static struct cp_function *cp_function_chain;
db5ae43f 13232
7215f9a0
MS
13233extern int temp_name_counter;
13234
db5ae43f
MS
13235/* Save and reinitialize the variables
13236 used during compilation of a C++ function. */
13237
13238void
28cbf42c
MS
13239push_cp_function_context (context)
13240 tree context;
db5ae43f
MS
13241{
13242 struct cp_function *p
13243 = (struct cp_function *) xmalloc (sizeof (struct cp_function));
13244
28cbf42c 13245 push_function_context_to (context);
db5ae43f
MS
13246
13247 p->next = cp_function_chain;
13248 cp_function_chain = p;
13249
13250 p->named_labels = named_labels;
13251 p->shadowed_labels = shadowed_labels;
13252 p->returns_value = current_function_returns_value;
13253 p->returns_null = current_function_returns_null;
13254 p->warn_about_return_type = warn_about_return_type;
db5ae43f
MS
13255 p->binding_level = current_binding_level;
13256 p->ctor_label = ctor_label;
13257 p->dtor_label = dtor_label;
72b7eeff 13258 p->last_dtor_insn = last_dtor_insn;
b87692e5 13259 p->last_parm_cleanup_insn = last_parm_cleanup_insn;
db5ae43f
MS
13260 p->assigns_this = current_function_assigns_this;
13261 p->just_assigned_this = current_function_just_assigned_this;
13262 p->parms_stored = current_function_parms_stored;
13263 p->result_rtx = original_result_rtx;
a9aedbc2 13264 p->base_init_expr = base_init_expr;
7215f9a0 13265 p->temp_name_counter = temp_name_counter;
79ff2c6c
MS
13266 p->base_init_list = current_base_init_list;
13267 p->member_init_list = current_member_init_list;
4ac14744
MS
13268 p->current_class_ptr = current_class_ptr;
13269 p->current_class_ref = current_class_ref;
42976354 13270 p->static_labelno = static_labelno;
db5ae43f
MS
13271}
13272
13273/* Restore the variables used during compilation of a C++ function. */
13274
13275void
28cbf42c
MS
13276pop_cp_function_context (context)
13277 tree context;
db5ae43f
MS
13278{
13279 struct cp_function *p = cp_function_chain;
13280 tree link;
13281
13282 /* Bring back all the labels that were shadowed. */
13283 for (link = shadowed_labels; link; link = TREE_CHAIN (link))
13284 if (DECL_NAME (TREE_VALUE (link)) != 0)
13285 SET_IDENTIFIER_LABEL_VALUE (DECL_NAME (TREE_VALUE (link)),
13286 TREE_VALUE (link));
13287
28cbf42c 13288 pop_function_context_from (context);
db5ae43f
MS
13289
13290 cp_function_chain = p->next;
13291
13292 named_labels = p->named_labels;
13293 shadowed_labels = p->shadowed_labels;
13294 current_function_returns_value = p->returns_value;
13295 current_function_returns_null = p->returns_null;
13296 warn_about_return_type = p->warn_about_return_type;
db5ae43f
MS
13297 current_binding_level = p->binding_level;
13298 ctor_label = p->ctor_label;
13299 dtor_label = p->dtor_label;
72b7eeff 13300 last_dtor_insn = p->last_dtor_insn;
b87692e5 13301 last_parm_cleanup_insn = p->last_parm_cleanup_insn;
db5ae43f
MS
13302 current_function_assigns_this = p->assigns_this;
13303 current_function_just_assigned_this = p->just_assigned_this;
13304 current_function_parms_stored = p->parms_stored;
13305 original_result_rtx = p->result_rtx;
a9aedbc2 13306 base_init_expr = p->base_init_expr;
7215f9a0 13307 temp_name_counter = p->temp_name_counter;
79ff2c6c
MS
13308 current_base_init_list = p->base_init_list;
13309 current_member_init_list = p->member_init_list;
4ac14744
MS
13310 current_class_ptr = p->current_class_ptr;
13311 current_class_ref = p->current_class_ref;
42976354 13312 static_labelno = p->static_labelno;
db5ae43f
MS
13313
13314 free (p);
13315}
ebfc180f 13316
5566b478
MS
13317int
13318in_function_p ()
13319{
13320 return function_depth != 0;
13321}
This page took 3.104017 seconds and 5 git commands to generate.