]> gcc.gnu.org Git - gcc.git/blame - gcc/cp/decl.c
980520-1.f: New test.
[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 50extern struct obstack permanent_obstack;
ae0a6181 51extern struct obstack* saveable_obstack;
8d08fdba
MS
52
53extern int current_class_depth;
54
e1cd6e56
MS
55extern tree static_ctors, static_dtors;
56
42976354
BK
57extern int static_labelno;
58
30394414
JM
59extern tree current_namespace;
60extern tree global_namespace;
61
8d08fdba
MS
62/* Stack of places to restore the search obstack back to. */
63
64/* Obstack used for remembering local class declarations (like
65 enums and static (const) members. */
66#include "stack.h"
5566b478 67struct obstack decl_obstack;
8d08fdba
MS
68static struct stack_level *decl_stack;
69
70#ifndef CHAR_TYPE_SIZE
71#define CHAR_TYPE_SIZE BITS_PER_UNIT
72#endif
73
74#ifndef SHORT_TYPE_SIZE
75#define SHORT_TYPE_SIZE (BITS_PER_UNIT * MIN ((UNITS_PER_WORD + 1) / 2, 2))
76#endif
77
78#ifndef INT_TYPE_SIZE
79#define INT_TYPE_SIZE BITS_PER_WORD
80#endif
81
82#ifndef LONG_TYPE_SIZE
83#define LONG_TYPE_SIZE BITS_PER_WORD
84#endif
85
86#ifndef LONG_LONG_TYPE_SIZE
87#define LONG_LONG_TYPE_SIZE (BITS_PER_WORD * 2)
88#endif
89
90#ifndef WCHAR_UNSIGNED
91#define WCHAR_UNSIGNED 0
92#endif
93
94#ifndef FLOAT_TYPE_SIZE
95#define FLOAT_TYPE_SIZE BITS_PER_WORD
96#endif
97
98#ifndef DOUBLE_TYPE_SIZE
99#define DOUBLE_TYPE_SIZE (BITS_PER_WORD * 2)
100#endif
101
102#ifndef LONG_DOUBLE_TYPE_SIZE
103#define LONG_DOUBLE_TYPE_SIZE (BITS_PER_WORD * 2)
104#endif
105
255512c1 106#ifndef BOOL_TYPE_SIZE
e1cd6e56 107#ifdef SLOW_BYTE_ACCESS
d2e5ee5c 108#define BOOL_TYPE_SIZE ((SLOW_BYTE_ACCESS) ? (POINTER_SIZE) : (CHAR_TYPE_SIZE))
e1cd6e56 109#else
d2e5ee5c 110#define BOOL_TYPE_SIZE CHAR_TYPE_SIZE
e1cd6e56 111#endif
255512c1
JM
112#endif
113
8d08fdba
MS
114/* We let tm.h override the types used here, to handle trivial differences
115 such as the choice of unsigned int or long unsigned int for size_t.
116 When machines start needing nontrivial differences in the size type,
117 it would be best to do something here to figure out automatically
118 from other information what type to use. */
119
120#ifndef SIZE_TYPE
121#define SIZE_TYPE "long unsigned int"
122#endif
123
124#ifndef PTRDIFF_TYPE
125#define PTRDIFF_TYPE "long int"
126#endif
127
128#ifndef WCHAR_TYPE
129#define WCHAR_TYPE "int"
130#endif
131
8d08fdba
MS
132static tree grokparms PROTO((tree, int));
133static tree lookup_nested_type PROTO((tree, tree));
134static char *redeclaration_error_message PROTO((tree, tree));
bd6dd845 135static tree push_overloaded_decl 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;
0b60dfe3 2561 int new_defines_function = 0;
5566b478
MS
2562
2563 if (newdecl == olddecl)
2564 return 1;
8d08fdba 2565
8926095f 2566 types_match = decls_match (newdecl, olddecl);
8d08fdba 2567
8d08fdba
MS
2568 /* If either the type of the new decl or the type of the old decl is an
2569 error_mark_node, then that implies that we have already issued an
2570 error (earlier) for some bogus type specification, and in that case,
2571 it is rather pointless to harass the user with yet more error message
0b60dfe3 2572 about the same declaration, so just pretend the types match here. */
bd6dd845
MS
2573 if (TREE_TYPE (newdecl) == error_mark_node
2574 || TREE_TYPE (olddecl) == error_mark_node)
8d08fdba 2575 types_match = 1;
0b60dfe3
BK
2576
2577 /* Check for redeclaration and other discrepancies. */
d22c8596
MS
2578 if (TREE_CODE (olddecl) == FUNCTION_DECL
2579 && DECL_ARTIFICIAL (olddecl)
2580 && (DECL_BUILT_IN (olddecl) || DECL_BUILT_IN_NONANSI (olddecl)))
8d08fdba
MS
2581 {
2582 /* If you declare a built-in or predefined function name as static,
a4443a08
MS
2583 the old definition is overridden, but optionally warn this was a
2584 bad choice of name. Ditto for overloads. */
893de33c 2585 if (! TREE_PUBLIC (newdecl)
a4443a08
MS
2586 || (TREE_CODE (newdecl) == FUNCTION_DECL
2587 && DECL_LANGUAGE (newdecl) != DECL_LANGUAGE (olddecl)))
2588 {
2589 if (warn_shadow)
2590 cp_warning ("shadowing %s function `%#D'",
2591 DECL_BUILT_IN (olddecl) ? "built-in" : "library",
2592 olddecl);
2593 /* Discard the old built-in function. */
2594 return 0;
2595 }
2596 else if (! types_match)
8d08fdba 2597 {
a4443a08
MS
2598 if (TREE_CODE (newdecl) != FUNCTION_DECL)
2599 {
2600 /* If the built-in is not ansi, then programs can override
2601 it even globally without an error. */
2602 if (! DECL_BUILT_IN (olddecl))
2603 cp_warning ("library function `%#D' redeclared as non-function `%#D'",
2604 olddecl, newdecl);
2605 else
2606 {
2607 cp_error ("declaration of `%#D'", newdecl);
2608 cp_error ("conflicts with built-in declaration `%#D'",
2609 olddecl);
2610 }
2611 return 0;
2612 }
2613
8d08fdba
MS
2614 cp_warning ("declaration of `%#D'", newdecl);
2615 cp_warning ("conflicts with built-in declaration `%#D'",
2616 olddecl);
8d08fdba 2617 }
39211cd5
MS
2618 }
2619 else if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
2620 {
2621 if ((TREE_CODE (newdecl) == FUNCTION_DECL
5566b478 2622 && DECL_FUNCTION_TEMPLATE_P (olddecl))
39211cd5 2623 || (TREE_CODE (olddecl) == FUNCTION_DECL
5566b478 2624 && DECL_FUNCTION_TEMPLATE_P (newdecl)))
39211cd5
MS
2625 return 0;
2626
2627 cp_error ("`%#D' redeclared as different kind of symbol", newdecl);
2628 if (TREE_CODE (olddecl) == TREE_LIST)
2629 olddecl = TREE_VALUE (olddecl);
2630 cp_error_at ("previous declaration of `%#D'", olddecl);
2631
2632 /* New decl is completely inconsistent with the old one =>
2633 tell caller to replace the old one. */
2634
2635 return 0;
8d08fdba 2636 }
8d08fdba
MS
2637 else if (!types_match)
2638 {
8926095f 2639 if (TREE_CODE (newdecl) == TEMPLATE_DECL)
f0e01782
MS
2640 {
2641 /* The name of a class template may not be declared to refer to
2642 any other template, class, function, object, namespace, value,
e92cc029 2643 or type in the same scope. */
5566b478
MS
2644 if (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) == TYPE_DECL
2645 || TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL)
f0e01782
MS
2646 {
2647 cp_error ("declaration of template `%#D'", newdecl);
2648 cp_error_at ("conflicts with previous declaration `%#D'",
2649 olddecl);
2650 }
ec255269
MS
2651 else if (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) == FUNCTION_DECL
2652 && TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == FUNCTION_DECL
2653 && compparms (TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (olddecl))),
75650646
MM
2654 TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (newdecl))), 3)
2655 && comp_template_parms (DECL_TEMPLATE_PARMS (newdecl),
2656 DECL_TEMPLATE_PARMS (olddecl)))
ec255269
MS
2657 {
2658 cp_error ("new declaration `%#D'", newdecl);
2659 cp_error_at ("ambiguates old declaration `%#D'", olddecl);
2660 }
f0e01782
MS
2661 return 0;
2662 }
8926095f
MS
2663 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2664 {
2665 if (DECL_LANGUAGE (newdecl) == lang_c
2666 && DECL_LANGUAGE (olddecl) == lang_c)
2667 {
2668 cp_error ("declaration of C function `%#D' conflicts with",
2669 newdecl);
2670 cp_error_at ("previous declaration `%#D' here", olddecl);
2671 }
00595019 2672 else if (compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
28cbf42c 2673 TYPE_ARG_TYPES (TREE_TYPE (olddecl)), 3))
700f8a87
MS
2674 {
2675 cp_error ("new declaration `%#D'", newdecl);
2676 cp_error_at ("ambiguates old declaration `%#D'", olddecl);
2677 }
2678 else
2679 return 0;
8926095f 2680 }
8d08fdba
MS
2681
2682 /* Already complained about this, so don't do so again. */
a4443a08 2683 else if (current_class_type == NULL_TREE
8d08fdba
MS
2684 || IDENTIFIER_ERROR_LOCUS (DECL_ASSEMBLER_NAME (newdecl)) != current_class_type)
2685 {
f376e137 2686 cp_error ("conflicting types for `%#D'", newdecl);
8926095f 2687 cp_error_at ("previous declaration as `%#D'", olddecl);
8d08fdba
MS
2688 }
2689 }
75650646
MM
2690 else if (TREE_CODE (newdecl) == FUNCTION_DECL
2691 && ((DECL_TEMPLATE_SPECIALIZATION (olddecl)
2692 && (!DECL_TEMPLATE_INFO (newdecl)
2693 || (DECL_TI_TEMPLATE (newdecl)
2694 != DECL_TI_TEMPLATE (olddecl))))
2695 || (DECL_TEMPLATE_SPECIALIZATION (newdecl)
2696 && (!DECL_TEMPLATE_INFO (olddecl)
2697 || (DECL_TI_TEMPLATE (olddecl)
2698 != DECL_TI_TEMPLATE (newdecl))))))
386b8a85
JM
2699 /* It's OK to have a template specialization and a non-template
2700 with the same type, or to have specializations of two
75650646
MM
2701 different templates with the same type. Note that if one is a
2702 specialization, and the other is an instantiation of the same
2703 template, that we do not exit at this point. That situation
2704 can occur if we instantiate a template class, and then
2705 specialize one of its methods. This situation is legal, but
2706 the declarations must be merged in the usual way. */
2707 return 0;
2708 else if (TREE_CODE (newdecl) == FUNCTION_DECL
2709 && ((DECL_TEMPLATE_INSTANTIATION (olddecl)
2710 && !DECL_USE_TEMPLATE (newdecl))
2711 || (DECL_TEMPLATE_INSTANTIATION (newdecl)
2712 && !DECL_USE_TEMPLATE (olddecl))))
2713 /* One of the declarations is a template instantiation, and the
2714 other is not a template at all. That's OK. */
386b8a85 2715 return 0;
8d08fdba
MS
2716 else
2717 {
2718 char *errmsg = redeclaration_error_message (newdecl, olddecl);
2719 if (errmsg)
2720 {
51c184be 2721 cp_error (errmsg, newdecl);
8d08fdba
MS
2722 if (DECL_NAME (olddecl) != NULL_TREE)
2723 cp_error_at ((DECL_INITIAL (olddecl)
2c73f9f5 2724 && namespace_bindings_p ())
8d08fdba
MS
2725 ? "`%#D' previously defined here"
2726 : "`%#D' previously declared here", olddecl);
2727 }
2728 else if (TREE_CODE (olddecl) == FUNCTION_DECL
2729 && DECL_INITIAL (olddecl) != NULL_TREE
2730 && TYPE_ARG_TYPES (TREE_TYPE (olddecl)) == NULL_TREE
2731 && TYPE_ARG_TYPES (TREE_TYPE (newdecl)) != NULL_TREE)
2732 {
2733 /* Prototype decl follows defn w/o prototype. */
2734 cp_warning_at ("prototype for `%#D'", newdecl);
2735 cp_warning_at ("follows non-prototype definition here", olddecl);
2736 }
2737 else if (TREE_CODE (olddecl) == FUNCTION_DECL
2738 && DECL_LANGUAGE (newdecl) != DECL_LANGUAGE (olddecl))
8926095f
MS
2739 {
2740 /* extern "C" int foo ();
2741 int foo () { bar (); }
2742 is OK. */
2743 if (current_lang_stack == current_lang_base)
a28e3c7f 2744 DECL_LANGUAGE (newdecl) = DECL_LANGUAGE (olddecl);
8926095f
MS
2745 else
2746 {
2747 cp_error_at ("previous declaration of `%#D' with %L linkage",
2748 olddecl, DECL_LANGUAGE (olddecl));
2749 cp_error ("conflicts with new declaration with %L linkage",
2750 DECL_LANGUAGE (newdecl));
2751 }
2752 }
e1cd6e56 2753
a6f02587 2754 if (DECL_LANG_SPECIFIC (olddecl) && DECL_USE_TEMPLATE (olddecl))
c32381b1
JM
2755 ;
2756 else if (TREE_CODE (olddecl) == FUNCTION_DECL)
e1cd6e56
MS
2757 {
2758 tree t1 = TYPE_ARG_TYPES (TREE_TYPE (olddecl));
2759 tree t2 = TYPE_ARG_TYPES (TREE_TYPE (newdecl));
2760 int i = 1;
2761
2762 if (TREE_CODE (TREE_TYPE (newdecl)) == METHOD_TYPE)
2763 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2);
2764
2765 for (; t1 && t1 != void_list_node;
2766 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2), i++)
2767 if (TREE_PURPOSE (t1) && TREE_PURPOSE (t2))
2768 {
2507f3b5
RK
2769 if (1 == simple_cst_equal (TREE_PURPOSE (t1),
2770 TREE_PURPOSE (t2)))
e1cd6e56
MS
2771 {
2772 if (pedantic)
2773 {
2774 cp_pedwarn ("default argument given for parameter %d of `%#D'",
2775 i, newdecl);
2776 cp_pedwarn_at ("after previous specification in `%#D'",
2777 olddecl);
2778 }
2779 }
2780 else
2781 {
2782 cp_error ("default argument given for parameter %d of `%#D'",
2783 i, newdecl);
da20811c 2784 cp_error_at ("after previous specification in `%#D'",
e1cd6e56
MS
2785 olddecl);
2786 }
2787 }
a5894242 2788
7fcdf4c2
MS
2789 if (DECL_THIS_INLINE (newdecl) && ! DECL_THIS_INLINE (olddecl)
2790 && TREE_ADDRESSABLE (olddecl) && warn_inline)
dff6b454 2791 {
7fcdf4c2
MS
2792 cp_warning ("`%#D' was used before it was declared inline",
2793 newdecl);
2794 cp_warning_at ("previous non-inline declaration here",
2795 olddecl);
dff6b454 2796 }
e1cd6e56 2797 }
8ccc31eb
MS
2798 /* These bits are logically part of the type for non-functions. */
2799 else if (TREE_READONLY (newdecl) != TREE_READONLY (olddecl)
2800 || TREE_THIS_VOLATILE (newdecl) != TREE_THIS_VOLATILE (olddecl))
2801 {
2802 cp_pedwarn ("type qualifiers for `%#D'", newdecl);
2803 cp_pedwarn_at ("conflict with previous decl `%#D'", olddecl);
2804 }
8d08fdba
MS
2805 }
2806
2807 /* If new decl is `static' and an `extern' was seen previously,
2808 warn about it. */
2809 warn_extern_redeclared_static (newdecl, olddecl);
2810
e92cc029 2811 /* We have committed to returning 1 at this point. */
8d08fdba
MS
2812 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2813 {
2814 /* Now that functions must hold information normally held
2815 by field decls, there is extra work to do so that
2816 declaration information does not get destroyed during
2817 definition. */
2818 if (DECL_VINDEX (olddecl))
2819 DECL_VINDEX (newdecl) = DECL_VINDEX (olddecl);
2820 if (DECL_CONTEXT (olddecl))
2821 DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
2822 if (DECL_CLASS_CONTEXT (olddecl))
2823 DECL_CLASS_CONTEXT (newdecl) = DECL_CLASS_CONTEXT (olddecl);
8d08fdba
MS
2824 if (DECL_PENDING_INLINE_INFO (newdecl) == (struct pending_inline *)0)
2825 DECL_PENDING_INLINE_INFO (newdecl) = DECL_PENDING_INLINE_INFO (olddecl);
e1cd6e56
MS
2826 DECL_STATIC_CONSTRUCTOR (newdecl) |= DECL_STATIC_CONSTRUCTOR (olddecl);
2827 DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
7215f9a0 2828 DECL_ABSTRACT_VIRTUAL_P (newdecl) |= DECL_ABSTRACT_VIRTUAL_P (olddecl);
864b83b9 2829 DECL_VIRTUAL_P (newdecl) |= DECL_VIRTUAL_P (olddecl);
4a67c9e9 2830 DECL_NEEDS_FINAL_OVERRIDER_P (newdecl) |= DECL_NEEDS_FINAL_OVERRIDER_P (olddecl);
0b60dfe3
BK
2831 new_defines_function = DECL_INITIAL (newdecl) != NULL_TREE;
2832
2833 /* Optionally warn about more than one declaration for the same
2834 name, but don't warn about a function declaration followed by a
2835 definition. */
2836 if (warn_redundant_decls && ! DECL_ARTIFICIAL (olddecl)
2837 && !(new_defines_function && DECL_INITIAL (olddecl) == NULL_TREE)
2838 /* Don't warn about extern decl followed by definition. */
2839 && !(DECL_EXTERNAL (olddecl) && ! DECL_EXTERNAL (newdecl))
2840 /* Don't warn about friends, let add_friend take care of it. */
2841 && ! DECL_FRIEND_P (newdecl))
2842 {
2843 cp_warning ("redundant redeclaration of `%D' in same scope", newdecl);
2844 cp_warning_at ("previous declaration of `%D'", olddecl);
2845 }
8d08fdba
MS
2846 }
2847
2848 /* Deal with C++: must preserve virtual function table size. */
2849 if (TREE_CODE (olddecl) == TYPE_DECL)
2850 {
2851 register tree newtype = TREE_TYPE (newdecl);
2852 register tree oldtype = TREE_TYPE (olddecl);
2853
2854 if (newtype != error_mark_node && oldtype != error_mark_node
2855 && TYPE_LANG_SPECIFIC (newtype) && TYPE_LANG_SPECIFIC (oldtype))
2856 {
2857 CLASSTYPE_VSIZE (newtype) = CLASSTYPE_VSIZE (oldtype);
2858 CLASSTYPE_FRIEND_CLASSES (newtype)
2859 = CLASSTYPE_FRIEND_CLASSES (oldtype);
2860 }
8d08fdba
MS
2861 }
2862
8d08fdba
MS
2863 /* Copy all the DECL_... slots specified in the new decl
2864 except for any that we copy here from the old type. */
0b60dfe3
BK
2865 DECL_MACHINE_ATTRIBUTES (newdecl)
2866 = merge_machine_decl_attributes (olddecl, newdecl);
8d08fdba 2867
5566b478
MS
2868 if (TREE_CODE (newdecl) == TEMPLATE_DECL)
2869 {
37dac039
JM
2870 if (DECL_INITIAL (DECL_TEMPLATE_RESULT (olddecl)) == NULL_TREE)
2871 {
3ac3d9ea
MM
2872 if (! duplicate_decls (DECL_TEMPLATE_RESULT (newdecl),
2873 DECL_TEMPLATE_RESULT (olddecl)))
2874 cp_error ("invalid redeclaration of %D", newdecl);
2875 TREE_TYPE (olddecl) = TREE_TYPE (DECL_TEMPLATE_RESULT (olddecl));
37dac039 2876 DECL_TEMPLATE_PARMS (olddecl) = DECL_TEMPLATE_PARMS (newdecl);
f84b4be9 2877 DECL_TEMPLATE_INFO (olddecl) = DECL_TEMPLATE_INFO (newdecl);
37dac039 2878 }
0b60dfe3
BK
2879 DECL_TEMPLATE_SPECIALIZATIONS (newdecl)
2880 = DECL_TEMPLATE_SPECIALIZATIONS (olddecl);
2881
5566b478
MS
2882 return 1;
2883 }
0b60dfe3 2884
8d08fdba
MS
2885 if (types_match)
2886 {
2887 /* Automatically handles default parameters. */
2888 tree oldtype = TREE_TYPE (olddecl);
e1cd6e56 2889 tree newtype;
8d08fdba 2890
e1cd6e56
MS
2891 /* Make sure we put the new type in the same obstack as the old one. */
2892 if (oldtype)
39211cd5
MS
2893 push_obstacks (TYPE_OBSTACK (oldtype), TYPE_OBSTACK (oldtype));
2894 else
2895 {
2896 push_obstacks_nochange ();
2897 end_temporary_allocation ();
2898 }
2899
e1cd6e56
MS
2900 /* Merge the data types specified in the two decls. */
2901 newtype = common_type (TREE_TYPE (newdecl), TREE_TYPE (olddecl));
2902
8d08fdba
MS
2903 if (TREE_CODE (newdecl) == VAR_DECL)
2904 DECL_THIS_EXTERN (newdecl) |= DECL_THIS_EXTERN (olddecl);
2905 /* Do this after calling `common_type' so that default
2906 parameters don't confuse us. */
2907 else if (TREE_CODE (newdecl) == FUNCTION_DECL
2908 && (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (newdecl))
2909 != TYPE_RAISES_EXCEPTIONS (TREE_TYPE (olddecl))))
2910 {
f30432d7 2911 TREE_TYPE (newdecl) = build_exception_variant (newtype,
8d08fdba 2912 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (newdecl)));
f30432d7 2913 TREE_TYPE (olddecl) = build_exception_variant (newtype,
8d08fdba
MS
2914 TYPE_RAISES_EXCEPTIONS (oldtype));
2915
047f64a3
JM
2916 if ((pedantic || (! DECL_IN_SYSTEM_HEADER (olddecl)
2917 && DECL_SOURCE_LINE (olddecl) != 0))
da20811c 2918 && flag_exceptions
824b9a4c 2919 && ! compexcepttypes (TREE_TYPE (newdecl), TREE_TYPE (olddecl)))
8d08fdba 2920 {
824b9a4c 2921 cp_pedwarn ("declaration of `%D' throws different exceptions",
a28e3c7f 2922 newdecl);
824b9a4c 2923 cp_pedwarn_at ("previous declaration here", olddecl);
8d08fdba
MS
2924 }
2925 }
2926 TREE_TYPE (newdecl) = TREE_TYPE (olddecl) = newtype;
2927
2928 /* Lay the type out, unless already done. */
f77f4f33 2929 if (newtype != CANONICAL_TYPE_VARIANT (oldtype)
5566b478 2930 && TREE_TYPE (newdecl) != error_mark_node
5156628f 2931 && !(processing_template_decl && uses_template_parms (newdecl)))
b7484fbe
MS
2932 layout_type (TREE_TYPE (newdecl));
2933
5566b478
MS
2934 if ((TREE_CODE (newdecl) == VAR_DECL
2935 || TREE_CODE (newdecl) == PARM_DECL
2936 || TREE_CODE (newdecl) == RESULT_DECL
2937 || TREE_CODE (newdecl) == FIELD_DECL
2938 || TREE_CODE (newdecl) == TYPE_DECL)
5156628f 2939 && !(processing_template_decl && uses_template_parms (newdecl)))
b7484fbe 2940 layout_decl (newdecl, 0);
8d08fdba
MS
2941
2942 /* Merge the type qualifiers. */
2943 if (TREE_READONLY (newdecl))
2944 TREE_READONLY (olddecl) = 1;
2945 if (TREE_THIS_VOLATILE (newdecl))
2946 TREE_THIS_VOLATILE (olddecl) = 1;
2947
2948 /* Merge the initialization information. */
8926095f
MS
2949 if (DECL_INITIAL (newdecl) == NULL_TREE
2950 && DECL_INITIAL (olddecl) != NULL_TREE)
2951 {
2952 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
2953 DECL_SOURCE_FILE (newdecl) = DECL_SOURCE_FILE (olddecl);
2954 DECL_SOURCE_LINE (newdecl) = DECL_SOURCE_LINE (olddecl);
2955 }
39211cd5
MS
2956
2957 /* Merge the section attribute.
2958 We want to issue an error if the sections conflict but that must be
2959 done later in decl_attributes since we are called before attributes
2960 are assigned. */
2961 if (DECL_SECTION_NAME (newdecl) == NULL_TREE)
2962 DECL_SECTION_NAME (newdecl) = DECL_SECTION_NAME (olddecl);
2963
8d08fdba
MS
2964 /* Keep the old rtl since we can safely use it, unless it's the
2965 call to abort() used for abstract virtuals. */
2966 if ((DECL_LANG_SPECIFIC (olddecl)
2967 && !DECL_ABSTRACT_VIRTUAL_P (olddecl))
2968 || DECL_RTL (olddecl) != DECL_RTL (abort_fndecl))
2969 DECL_RTL (newdecl) = DECL_RTL (olddecl);
39211cd5
MS
2970
2971 pop_obstacks ();
8d08fdba
MS
2972 }
2973 /* If cannot merge, then use the new type and qualifiers,
2974 and don't preserve the old rtl. */
2975 else
2976 {
2977 /* Clean out any memory we had of the old declaration. */
2978 tree oldstatic = value_member (olddecl, static_aggregates);
2979 if (oldstatic)
2980 TREE_VALUE (oldstatic) = error_mark_node;
2981
2982 TREE_TYPE (olddecl) = TREE_TYPE (newdecl);
2983 TREE_READONLY (olddecl) = TREE_READONLY (newdecl);
2984 TREE_THIS_VOLATILE (olddecl) = TREE_THIS_VOLATILE (newdecl);
2985 TREE_SIDE_EFFECTS (olddecl) = TREE_SIDE_EFFECTS (newdecl);
2986 }
2987
2988 /* Merge the storage class information. */
a9aedbc2 2989 DECL_WEAK (newdecl) |= DECL_WEAK (olddecl);
e92cc029 2990 DECL_ONE_ONLY (newdecl) |= DECL_ONE_ONLY (olddecl);
a9aedbc2
MS
2991 TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
2992 TREE_STATIC (olddecl) = TREE_STATIC (newdecl) |= TREE_STATIC (olddecl);
2993 if (! DECL_EXTERNAL (olddecl))
2994 DECL_EXTERNAL (newdecl) = 0;
0b60dfe3
BK
2995
2996 if (DECL_LANG_SPECIFIC (newdecl) && DECL_LANG_SPECIFIC (olddecl))
8d08fdba 2997 {
a9aedbc2
MS
2998 DECL_INTERFACE_KNOWN (newdecl) |= DECL_INTERFACE_KNOWN (olddecl);
2999 DECL_NOT_REALLY_EXTERN (newdecl) |= DECL_NOT_REALLY_EXTERN (olddecl);
77be6f82 3000 DECL_COMDAT (newdecl) |= DECL_COMDAT (olddecl);
0b60dfe3
BK
3001 /* Don't really know how much of the language-specific
3002 values we should copy from old to new. */
3003 DECL_IN_AGGR_P (newdecl) = DECL_IN_AGGR_P (olddecl);
3004 DECL_ACCESS (newdecl) = DECL_ACCESS (olddecl);
3005 DECL_NONCONVERTING_P (newdecl) = DECL_NONCONVERTING_P (olddecl);
3006 if (DECL_TEMPLATE_INFO (newdecl) == NULL_TREE)
3007 {
3008 DECL_TEMPLATE_INFO (newdecl) = DECL_TEMPLATE_INFO (olddecl);
3009 DECL_USE_TEMPLATE (newdecl) = DECL_USE_TEMPLATE (olddecl);
3010 }
3011 olddecl_friend = DECL_FRIEND_P (olddecl);
8d08fdba
MS
3012 }
3013
8d08fdba
MS
3014 if (TREE_CODE (newdecl) == FUNCTION_DECL)
3015 {
75650646
MM
3016 if (DECL_TEMPLATE_INSTANTIATION (olddecl)
3017 && !DECL_TEMPLATE_INSTANTIATION (newdecl))
3018 {
3019 /* If newdecl is not a specialization, then it is not a
3020 template-related function at all. And that means that we
3021 shoud have exited above, returning 0. */
3022 my_friendly_assert (DECL_TEMPLATE_SPECIALIZATION (newdecl),
3023 0);
3024
3025 if (TREE_USED (olddecl))
3026 /* From [temp.expl.spec]:
3027
3028 If a template, a member template or the member of a class
3029 template is explicitly specialized then that
3030 specialization shall be declared before the first use of
3031 that specialization that would cause an implicit
3032 instantiation to take place, in every translation unit in
3033 which such a use occurs. */
3034 cp_error ("explicit specialization of %D after first use",
3035 olddecl);
3036
3037 SET_DECL_TEMPLATE_SPECIALIZATION (olddecl);
3038 }
faae18ab
MS
3039 DECL_THIS_INLINE (newdecl) |= DECL_THIS_INLINE (olddecl);
3040
3041 /* If either decl says `inline', this fn is inline, unless its
3042 definition was passed already. */
3043 if (DECL_INLINE (newdecl) && DECL_INITIAL (olddecl) == NULL_TREE)
3044 DECL_INLINE (olddecl) = 1;
3045 DECL_INLINE (newdecl) = DECL_INLINE (olddecl);
3046
700f8a87
MS
3047 if (! types_match)
3048 {
3049 DECL_LANGUAGE (olddecl) = DECL_LANGUAGE (newdecl);
3050 DECL_ASSEMBLER_NAME (olddecl) = DECL_ASSEMBLER_NAME (newdecl);
5566b478
MS
3051 DECL_RTL (olddecl) = DECL_RTL (newdecl);
3052 }
3053 if (! types_match || new_defines_function)
3054 {
3055 /* These need to be copied so that the names are available. */
700f8a87
MS
3056 DECL_ARGUMENTS (olddecl) = DECL_ARGUMENTS (newdecl);
3057 DECL_RESULT (olddecl) = DECL_RESULT (newdecl);
700f8a87 3058 }
8d08fdba
MS
3059 if (new_defines_function)
3060 /* If defining a function declared with other language
3061 linkage, use the previously declared language linkage. */
3062 DECL_LANGUAGE (newdecl) = DECL_LANGUAGE (olddecl);
3063 else
3064 {
3065 /* If redeclaring a builtin function, and not a definition,
3066 it stays built in. */
3067 if (DECL_BUILT_IN (olddecl))
3068 {
3069 DECL_BUILT_IN (newdecl) = 1;
39211cd5 3070 DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl);
8d08fdba
MS
3071 /* If we're keeping the built-in definition, keep the rtl,
3072 regardless of declaration matches. */
3073 DECL_RTL (newdecl) = DECL_RTL (olddecl);
3074 }
3075 else
3076 DECL_FRAME_SIZE (newdecl) = DECL_FRAME_SIZE (olddecl);
3077
3078 DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
8926095f 3079 if ((DECL_SAVED_INSNS (newdecl) = DECL_SAVED_INSNS (olddecl)))
8d08fdba
MS
3080 /* Previously saved insns go together with
3081 the function's previous definition. */
3082 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
3083 /* Don't clear out the arguments if we're redefining a function. */
3084 if (DECL_ARGUMENTS (olddecl))
3085 DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl);
3086 }
6060a796
MS
3087 if (DECL_LANG_SPECIFIC (olddecl))
3088 DECL_MAIN_VARIANT (newdecl) = DECL_MAIN_VARIANT (olddecl);
8d08fdba
MS
3089 }
3090
a9aedbc2
MS
3091 if (TREE_CODE (newdecl) == NAMESPACE_DECL)
3092 {
3093 NAMESPACE_LEVEL (newdecl) = NAMESPACE_LEVEL (olddecl);
3094 }
3095
8d08fdba
MS
3096 /* Now preserve various other info from the definition. */
3097 TREE_ADDRESSABLE (newdecl) = TREE_ADDRESSABLE (olddecl);
3098 TREE_ASM_WRITTEN (newdecl) = TREE_ASM_WRITTEN (olddecl);
3099 DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
f376e137 3100 DECL_ASSEMBLER_NAME (newdecl) = DECL_ASSEMBLER_NAME (olddecl);
8d08fdba 3101
8d08fdba
MS
3102 if (TREE_CODE (newdecl) == FUNCTION_DECL)
3103 {
3104 int function_size;
3105 struct lang_decl *ol = DECL_LANG_SPECIFIC (olddecl);
3106 struct lang_decl *nl = DECL_LANG_SPECIFIC (newdecl);
3107
3108 function_size = sizeof (struct tree_decl);
3109
3110 bcopy ((char *) newdecl + sizeof (struct tree_common),
3111 (char *) olddecl + sizeof (struct tree_common),
3112 function_size - sizeof (struct tree_common));
3113
3114 /* Can we safely free the storage used by newdecl? */
3115
3116#define ROUND(x) ((x + obstack_alignment_mask (&permanent_obstack)) \
3117 & ~ obstack_alignment_mask (&permanent_obstack))
3118
75650646
MM
3119 if (DECL_TEMPLATE_INSTANTIATION (newdecl))
3120 {
3121 /* If newdecl is a template instantiation, it is possible that
3122 the following sequence of events has occurred:
3123
3124 o A friend function was declared in a class template. The
3125 class template was instantiated.
3126
3127 o The instantiation of the friend declaration was
3128 recorded on the instantiation list, and is newdecl.
3129
3130 o Later, however, instantiate_class_template called pushdecl
3131 on the newdecl to perform name injection. But, pushdecl in
3132 turn called duplicate_decls when it discovered that another
3133 declaration of a global function with the same name already
3134 existed.
3135
3136 o Here, in duplicate_decls, we decided to clobber newdecl.
3137
3138 If we're going to do that, we'd better make sure that
3139 olddecl, and not newdecl, is on the list of
3140 instantiations so that if we try to do the instantiation
3141 again we won't get the clobbered declaration. */
3142
3143 tree tmpl = DECL_TI_TEMPLATE (newdecl);
3144 tree decls = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
3145
3146 for (; decls; decls = TREE_CHAIN (decls))
3147 if (TREE_VALUE (decls) == newdecl)
3148 TREE_VALUE (decls) = olddecl;
3149 }
3150
3a56f0ab
JM
3151 if (((char *)newdecl + ROUND (function_size) == (char *)nl
3152 && ((char *)newdecl + ROUND (function_size)
3153 + ROUND (sizeof (struct lang_decl))
3154 == obstack_next_free (&permanent_obstack)))
3155 || ((char *)newdecl + ROUND (function_size)
3156 == obstack_next_free (&permanent_obstack)))
8d08fdba
MS
3157 {
3158 DECL_MAIN_VARIANT (newdecl) = olddecl;
3159 DECL_LANG_SPECIFIC (olddecl) = ol;
3160 bcopy ((char *)nl, (char *)ol, sizeof (struct lang_decl));
3161
3162 obstack_free (&permanent_obstack, newdecl);
3163 }
d22c8596 3164 else if (LANG_DECL_PERMANENT (ol) && ol != nl)
8d08fdba
MS
3165 {
3166 if (DECL_MAIN_VARIANT (olddecl) == olddecl)
3167 {
3168 /* Save these lang_decls that would otherwise be lost. */
3169 extern tree free_lang_decl_chain;
3170 tree free_lang_decl = (tree) ol;
d22c8596
MS
3171
3172 if (DECL_LANG_SPECIFIC (olddecl) == ol)
3173 abort ();
3174
8d08fdba
MS
3175 TREE_CHAIN (free_lang_decl) = free_lang_decl_chain;
3176 free_lang_decl_chain = free_lang_decl;
3177 }
3178 else
3179 {
bd6dd845 3180 /* Storage leak. */;
8d08fdba
MS
3181 }
3182 }
3183 }
3184 else
3185 {
3186 bcopy ((char *) newdecl + sizeof (struct tree_common),
3187 (char *) olddecl + sizeof (struct tree_common),
3188 sizeof (struct tree_decl) - sizeof (struct tree_common)
3189 + tree_code_length [(int)TREE_CODE (newdecl)] * sizeof (char *));
3190 }
3191
3192 DECL_UID (olddecl) = olddecl_uid;
3193 if (olddecl_friend)
3194 DECL_FRIEND_P (olddecl) = 1;
3195
d9525bec
BK
3196 /* NEWDECL contains the merged attribute lists.
3197 Update OLDDECL to be the same. */
3198 DECL_MACHINE_ATTRIBUTES (olddecl) = DECL_MACHINE_ATTRIBUTES (newdecl);
3199
8d08fdba
MS
3200 return 1;
3201}
3202
3203/* Record a decl-node X as belonging to the current lexical scope.
3204 Check for errors (such as an incompatible declaration for the same
3205 name already seen in the same scope).
3206
3207 Returns either X or an old decl for the same name.
3208 If an old decl is returned, it may have been smashed
3209 to agree with what X says. */
3210
3211tree
3212pushdecl (x)
3213 tree x;
3214{
3215 register tree t;
8d08fdba 3216 register tree name = DECL_ASSEMBLER_NAME (x);
8d08fdba
MS
3217 register struct binding_level *b = current_binding_level;
3218
2c73f9f5
ML
3219 if (current_function_decl && x != current_function_decl
3220 /* A local declaration for a function doesn't constitute nesting. */
3221 && (TREE_CODE (x) != FUNCTION_DECL || DECL_INITIAL (x))
700f8a87
MS
3222 /* Don't change DECL_CONTEXT of virtual methods. */
3223 && (TREE_CODE (x) != FUNCTION_DECL || !DECL_VIRTUAL_P (x))
8d08fdba
MS
3224 && ! DECL_CONTEXT (x))
3225 DECL_CONTEXT (x) = current_function_decl;
2c73f9f5
ML
3226 if (!DECL_CONTEXT (x))
3227 DECL_CONTEXT (x) = current_namespace;
8d08fdba 3228
8d08fdba 3229 /* Type are looked up using the DECL_NAME, as that is what the rest of the
e92cc029 3230 compiler wants to use. */
a9aedbc2 3231 if (TREE_CODE (x) == TYPE_DECL || TREE_CODE (x) == VAR_DECL
73b0fce8
KL
3232 || TREE_CODE (x) == NAMESPACE_DECL || TREE_CODE (x) == TEMPLATE_TYPE_PARM
3233 || TREE_CODE (x) == TEMPLATE_TEMPLATE_PARM)
8d08fdba 3234 name = DECL_NAME (x);
8d08fdba
MS
3235
3236 if (name)
3237 {
5566b478
MS
3238#if 0
3239 /* Not needed...see below. */
8d08fdba
MS
3240 char *file;
3241 int line;
5566b478 3242#endif
386b8a85
JM
3243 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
3244 name = TREE_OPERAND (name, 0);
3245
2c73f9f5 3246 /* Namespace-scoped variables are not found in the current level. */
6eb3bb27 3247 if (TREE_CODE (x) == VAR_DECL && DECL_NAMESPACE_SCOPE_P (x))
2c73f9f5
ML
3248 t = namespace_binding (name, DECL_CONTEXT (x));
3249 else
3250 t = lookup_name_current_level (name);
8d08fdba
MS
3251 if (t == error_mark_node)
3252 {
3253 /* error_mark_node is 0 for a while during initialization! */
3254 t = NULL_TREE;
3255 cp_error_at ("`%#D' used prior to declaration", x);
3256 }
3257
51c184be 3258 else if (t != NULL_TREE)
8d08fdba 3259 {
5566b478
MS
3260#if 0
3261 /* This is turned off until I have time to do it right (bpk). */
e92cc029 3262 /* With the code below that uses it... */
8d6e462b
PB
3263 file = DECL_SOURCE_FILE (t);
3264 line = DECL_SOURCE_LINE (t);
5566b478 3265#endif
2ee887f2 3266 if (TREE_CODE (t) == PARM_DECL)
8d08fdba
MS
3267 {
3268 if (DECL_CONTEXT (t) == NULL_TREE)
3269 fatal ("parse errors have confused me too much");
be99da77 3270
e92cc029 3271 /* Check for duplicate params. */
be99da77
MS
3272 if (duplicate_decls (x, t))
3273 return t;
8d08fdba 3274 }
8d6e462b 3275 else if (((TREE_CODE (x) == FUNCTION_DECL && DECL_LANGUAGE (x) == lang_c)
5566b478
MS
3276 || DECL_FUNCTION_TEMPLATE_P (x))
3277 && is_overloaded_fn (t))
2c73f9f5 3278 /* Don't do anything just yet. */;
e1cd6e56
MS
3279 else if (t == wchar_decl_node)
3280 {
3281 if (pedantic && ! DECL_IN_SYSTEM_HEADER (x))
3282 cp_pedwarn ("redeclaration of wchar_t as `%T'", TREE_TYPE (x));
3283
3284 /* Throw away the redeclaration. */
3285 return t;
3286 }
8926095f 3287 else if (TREE_CODE (t) != TREE_CODE (x))
8d08fdba 3288 {
ec255269
MS
3289 if ((TREE_CODE (t) == TYPE_DECL && DECL_ARTIFICIAL (t)
3290 && TREE_CODE (x) != TYPE_DECL
3291 && ! (TREE_CODE (x) == TEMPLATE_DECL
3292 && TREE_CODE (DECL_TEMPLATE_RESULT (x)) == TYPE_DECL))
3293 || (TREE_CODE (x) == TYPE_DECL && DECL_ARTIFICIAL (x)
3294 && TREE_CODE (t) != TYPE_DECL
3295 && ! (TREE_CODE (t) == TEMPLATE_DECL
3296 && (TREE_CODE (DECL_TEMPLATE_RESULT (t))
3297 == TYPE_DECL))))
51c184be
MS
3298 {
3299 /* We do nothing special here, because C++ does such nasty
3300 things with TYPE_DECLs. Instead, just let the TYPE_DECL
3301 get shadowed, and know that if we need to find a TYPE_DECL
3302 for a given name, we can look in the IDENTIFIER_TYPE_VALUE
3303 slot of the identifier. */
3304 ;
3305 }
3306 else if (duplicate_decls (x, t))
3307 return t;
8d08fdba
MS
3308 }
3309 else if (duplicate_decls (x, t))
51c184be 3310 {
8d08fdba 3311#if 0
8926095f 3312 /* This is turned off until I have time to do it right (bpk). */
8d08fdba 3313
8926095f
MS
3314 /* Also warn if they did a prototype with `static' on it, but
3315 then later left the `static' off. */
3316 if (! TREE_PUBLIC (name) && TREE_PUBLIC (x))
8d08fdba 3317 {
8926095f
MS
3318 if (DECL_LANG_SPECIFIC (t) && DECL_FRIEND_P (t))
3319 return t;
8d08fdba 3320
8926095f
MS
3321 if (extra_warnings)
3322 {
a28e3c7f
MS
3323 cp_warning ("`static' missing from declaration of `%D'",
3324 t);
8926095f
MS
3325 warning_with_file_and_line (file, line,
3326 "previous declaration of `%s'",
3327 decl_as_string (t, 0));
3328 }
8d08fdba 3329
8926095f
MS
3330 /* Now fix things so it'll do what they expect. */
3331 if (current_function_decl)
3332 TREE_PUBLIC (current_function_decl) = 0;
3333 }
51c184be
MS
3334 /* Due to interference in memory reclamation (X may be
3335 obstack-deallocated at this point), we must guard against
8926095f
MS
3336 one really special case. [jason: This should be handled
3337 by start_function] */
51c184be
MS
3338 if (current_function_decl == x)
3339 current_function_decl = t;
8926095f 3340#endif
7177d104
MS
3341 if (TREE_CODE (t) == TYPE_DECL)
3342 SET_IDENTIFIER_TYPE_VALUE (name, TREE_TYPE (t));
f30432d7
MS
3343 else if (TREE_CODE (t) == FUNCTION_DECL)
3344 check_default_args (t);
7177d104 3345
51c184be
MS
3346 return t;
3347 }
8d08fdba 3348 }
8926095f
MS
3349
3350 if (TREE_CODE (x) == FUNCTION_DECL && ! DECL_FUNCTION_MEMBER_P (x))
3351 {
3352 t = push_overloaded_decl (x, 1);
3353 if (t != x || DECL_LANGUAGE (x) == lang_c)
3354 return t;
3355 }
6eb3bb27 3356 else if (DECL_FUNCTION_TEMPLATE_P (x) && DECL_NAMESPACE_SCOPE_P (x))
8926095f 3357 return push_overloaded_decl (x, 0);
8d08fdba 3358
a1774733
BK
3359 /* If declaring a type as a typedef, copy the type (unless we're
3360 at line 0), and install this TYPE_DECL as the new type's typedef
3361 name. See the extensive comment in ../c-decl.c (pushdecl). */
8d08fdba
MS
3362 if (TREE_CODE (x) == TYPE_DECL)
3363 {
3364 tree type = TREE_TYPE (x);
a1774733
BK
3365 if (DECL_SOURCE_LINE (x) == 0)
3366 {
3367 if (TYPE_NAME (type) == 0)
3368 TYPE_NAME (type) = x;
3369 }
3370 else if (type != error_mark_node && TYPE_NAME (type) != x)
3371 {
ae0a6181
MM
3372 push_obstacks (TYPE_OBSTACK (type), TYPE_OBSTACK (type));
3373
3374 if (!TREE_PERMANENT (x)
3375 && TYPE_OBSTACK (type) != saveable_obstack)
3376 {
3377 /* X should have been allocated on the saveable
3378 obstack. Since the type was not, the type may
3379 outlive X, unless we make a copy of X. Here are
3380 some examples:
3381
3382 template <class T>
3383 void f()
3384 {
3385 typedef S<T> Type_t;
3386 Type_t::foo();
3387 }
3388
3389 Here, we will create a SCOPE_REF with Type_t as
3390 its first argument, and save the SCOPE_REF for
3391 later, so that we can tsubst into it. But, that
3392 means we need to save the TYPE_DECL for Type_t.
3393
3394 But, we must save the TYPE_DECL even when not
3395 processing_template_decl. For example,
3396
3397 void f()
3398 {
3399 typedef int I;
3400 g<I>();
3401 }
3402
3403 may create a declaration of g with `I' as one of
3404 the arguments. In the old days, we used to use
3405 the underlying types for things, but now we try
3406 to use the typedef names for readability. */
3407 x = copy_node (x);
3408 copy_lang_decl (x);
3409 }
3410
a1774733
BK
3411 DECL_ORIGINAL_TYPE (x) = type;
3412 type = build_type_copy (type);
3413 TYPE_STUB_DECL (type) = TYPE_STUB_DECL (DECL_ORIGINAL_TYPE (x));
3414 TYPE_NAME (type) = x;
3415 TREE_TYPE (x) = type;
ae0a6181
MM
3416
3417 pop_obstacks ();
a1774733 3418 }
8d08fdba 3419
8d08fdba
MS
3420 if (type != error_mark_node
3421 && TYPE_NAME (type)
3422 && TYPE_IDENTIFIER (type))
3423 set_identifier_type_value_with_scope (DECL_NAME (x), type, b);
3424 }
3425
3426 /* Multiple external decls of the same identifier ought to match.
3427
3428 We get warnings about inline functions where they are defined.
39211cd5
MS
3429 We get warnings about other functions from push_overloaded_decl.
3430
8d08fdba 3431 Avoid duplicate warnings where they are used. */
39211cd5 3432 if (TREE_PUBLIC (x) && TREE_CODE (x) != FUNCTION_DECL)
8d08fdba
MS
3433 {
3434 tree decl;
30394414 3435 tree bindings = binding_for_name (name, current_namespace);
8d08fdba 3436
30394414
JM
3437 if (BINDING_VALUE (bindings) != NULL_TREE
3438 && (DECL_EXTERNAL (BINDING_VALUE (bindings))
3439 || TREE_PUBLIC (BINDING_VALUE (bindings))))
3440 decl = BINDING_VALUE (bindings);
8d08fdba
MS
3441 else
3442 decl = NULL_TREE;
3443
39211cd5 3444 if (decl
8d08fdba
MS
3445 /* If different sort of thing, we already gave an error. */
3446 && TREE_CODE (decl) == TREE_CODE (x)
39211cd5 3447 && ! comptypes (TREE_TYPE (x), TREE_TYPE (decl), 1))
8d08fdba
MS
3448 {
3449 cp_pedwarn ("type mismatch with previous external decl", x);
8926095f 3450 cp_pedwarn_at ("previous external decl of `%#D'", decl);
8d08fdba
MS
3451 }
3452 }
3453
8d08fdba
MS
3454 /* This name is new in its binding level.
3455 Install the new declaration and return it. */
2c73f9f5 3456 if (namespace_bindings_p ())
8d08fdba
MS
3457 {
3458 /* Install a global value. */
30394414 3459 tree bindings = binding_for_name (name, current_namespace);
8d08fdba 3460
8d08fdba
MS
3461 /* If the first global decl has external linkage,
3462 warn if we later see static one. */
30394414 3463 if (BINDING_VALUE (bindings) == NULL_TREE && TREE_PUBLIC (x))
8d08fdba
MS
3464 TREE_PUBLIC (name) = 1;
3465
e1cd6e56
MS
3466 /* Don't install an artificial TYPE_DECL if we already have
3467 another _DECL with that name. */
8d08fdba
MS
3468 if (TREE_CODE (x) != TYPE_DECL
3469 || t == NULL_TREE
e1cd6e56 3470 || ! DECL_ARTIFICIAL (x))
30394414
JM
3471 {
3472 if (TREE_CODE (x) == FUNCTION_DECL)
3473 my_friendly_assert ((BINDING_VALUE (bindings) == NULL_TREE)
3474 || BINDING_VALUE (bindings) == x, 378);
3475 BINDING_VALUE (bindings) = x;
3476 }
8d08fdba
MS
3477
3478 /* Don't forget if the function was used via an implicit decl. */
3479 if (IDENTIFIER_IMPLICIT_DECL (name)
3480 && TREE_USED (IDENTIFIER_IMPLICIT_DECL (name)))
3481 TREE_USED (x) = 1;
3482
3483 /* Don't forget if its address was taken in that way. */
3484 if (IDENTIFIER_IMPLICIT_DECL (name)
3485 && TREE_ADDRESSABLE (IDENTIFIER_IMPLICIT_DECL (name)))
3486 TREE_ADDRESSABLE (x) = 1;
3487
3488 /* Warn about mismatches against previous implicit decl. */
3489 if (IDENTIFIER_IMPLICIT_DECL (name) != NULL_TREE
3490 /* If this real decl matches the implicit, don't complain. */
3491 && ! (TREE_CODE (x) == FUNCTION_DECL
3492 && TREE_TYPE (TREE_TYPE (x)) == integer_type_node))
3493 cp_warning
3494 ("`%D' was previously implicitly declared to return `int'", x);
3495
3496 /* If new decl is `static' and an `extern' was seen previously,
3497 warn about it. */
a0a33927
MS
3498 if (x != NULL_TREE && t != NULL_TREE && decls_match (x, t))
3499 warn_extern_redeclared_static (x, t);
8d08fdba
MS
3500 }
3501 else
3502 {
3503 /* Here to install a non-global value. */
3504 tree oldlocal = IDENTIFIER_LOCAL_VALUE (name);
30394414 3505 tree oldglobal = binding_for_name (name, current_namespace);
8d08fdba 3506
e1cd6e56
MS
3507 /* Don't install an artificial TYPE_DECL if we already have
3508 another _DECL with that name. */
3509 if (TREE_CODE (x) != TYPE_DECL
3510 || t == NULL_TREE
3511 || ! DECL_ARTIFICIAL (x))
3512 {
3513 b->shadowed = tree_cons (name, oldlocal, b->shadowed);
3514 IDENTIFIER_LOCAL_VALUE (name) = x;
3515 }
8d08fdba
MS
3516
3517 /* If this is a TYPE_DECL, push it into the type value slot. */
3518 if (TREE_CODE (x) == TYPE_DECL)
3519 set_identifier_type_value_with_scope (name, TREE_TYPE (x), b);
3520
a9aedbc2
MS
3521 /* Clear out any TYPE_DECL shadowed by a namespace so that
3522 we won't think this is a type. The C struct hack doesn't
3523 go through namespaces. */
3524 if (TREE_CODE (x) == NAMESPACE_DECL)
3525 set_identifier_type_value_with_scope (name, NULL_TREE, b);
3526
8d08fdba
MS
3527 /* If this is an extern function declaration, see if we
3528 have a global definition or declaration for the function. */
3529 if (oldlocal == NULL_TREE
faae18ab 3530 && DECL_EXTERNAL (x)
30394414 3531 && BINDING_VALUE (oldglobal) != NULL_TREE
8d08fdba 3532 && TREE_CODE (x) == FUNCTION_DECL
30394414 3533 && TREE_CODE (BINDING_VALUE (oldglobal)) == FUNCTION_DECL)
8d08fdba
MS
3534 {
3535 /* We have one. Their types must agree. */
30394414 3536 if (decls_match (x, BINDING_VALUE (oldglobal)))
6060a796
MS
3537 /* OK */;
3538 else
8d08fdba
MS
3539 {
3540 cp_warning ("extern declaration of `%#D' doesn't match", x);
30394414 3541 cp_warning_at ("global declaration `%#D'", BINDING_VALUE (oldglobal));
8d08fdba 3542 }
8d08fdba
MS
3543 }
3544 /* If we have a local external declaration,
3545 and no file-scope declaration has yet been seen,
3546 then if we later have a file-scope decl it must not be static. */
3547 if (oldlocal == NULL_TREE
30394414 3548 && BINDING_VALUE (oldglobal) == NULL_TREE
8d08fdba
MS
3549 && DECL_EXTERNAL (x)
3550 && TREE_PUBLIC (x))
3551 {
3552 TREE_PUBLIC (name) = 1;
3553 }
3554
3555 if (DECL_FROM_INLINE (x))
3556 /* Inline decls shadow nothing. */;
3557
3558 /* Warn if shadowing an argument at the top level of the body. */
3559 else if (oldlocal != NULL_TREE && !DECL_EXTERNAL (x)
3560 && TREE_CODE (oldlocal) == PARM_DECL
3561 && TREE_CODE (x) != PARM_DECL)
3562 {
3563 /* Go to where the parms should be and see if we
3564 find them there. */
3565 struct binding_level *b = current_binding_level->level_chain;
3566
3567 if (cleanup_label)
3568 b = b->level_chain;
3569
3570 /* ARM $8.3 */
3571 if (b->parm_flag == 1)
3572 cp_error ("declaration of `%#D' shadows a parameter", name);
3573 }
72b7eeff 3574 else if (warn_shadow && oldlocal != NULL_TREE && b->is_for_scope
8d6e462b
PB
3575 && !DECL_DEAD_FOR_LOCAL (oldlocal))
3576 {
3577 warning ("variable `%s' shadows local",
3578 IDENTIFIER_POINTER (name));
3579 cp_warning_at (" this is the shadowed declaration", oldlocal);
3580 }
8d08fdba
MS
3581 /* Maybe warn if shadowing something else. */
3582 else if (warn_shadow && !DECL_EXTERNAL (x)
3583 /* No shadow warnings for internally generated vars. */
700f8a87 3584 && ! DECL_ARTIFICIAL (x)
8d08fdba
MS
3585 /* No shadow warnings for vars made for inlining. */
3586 && ! DECL_FROM_INLINE (x))
3587 {
3588 char *warnstring = NULL;
3589
3590 if (oldlocal != NULL_TREE && TREE_CODE (oldlocal) == PARM_DECL)
3591 warnstring = "declaration of `%s' shadows a parameter";
3592 else if (IDENTIFIER_CLASS_VALUE (name) != NULL_TREE
4ac14744 3593 && current_class_ptr
8d08fdba
MS
3594 && !TREE_STATIC (name))
3595 warnstring = "declaration of `%s' shadows a member of `this'";
3596 else if (oldlocal != NULL_TREE)
3597 warnstring = "declaration of `%s' shadows previous local";
30394414
JM
3598 else if (BINDING_VALUE (oldglobal) != NULL_TREE)
3599 /* XXX shadow warnings in outer-more namespaces */
8d08fdba
MS
3600 warnstring = "declaration of `%s' shadows global declaration";
3601
3602 if (warnstring)
3603 warning (warnstring, IDENTIFIER_POINTER (name));
3604 }
e1cd6e56 3605 }
8d08fdba 3606
e1cd6e56 3607 if (TREE_CODE (x) == FUNCTION_DECL)
f30432d7 3608 check_default_args (x);
8145f082
MS
3609
3610 /* Keep count of variables in this level with incomplete type. */
8145f082 3611 if (TREE_CODE (x) == VAR_DECL
28cbf42c 3612 && TREE_TYPE (x) != error_mark_node
f30432d7
MS
3613 && ((TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
3614 && PROMOTES_TO_AGGR_TYPE (TREE_TYPE (x), ARRAY_TYPE))
3615 /* RTTI TD entries are created while defining the type_info. */
3616 || (TYPE_LANG_SPECIFIC (TREE_TYPE (x))
3617 && TYPE_BEING_DEFINED (TREE_TYPE (x)))))
3618 b->incomplete = tree_cons (NULL_TREE, x, b->incomplete);
8d08fdba
MS
3619 }
3620
8d08fdba
MS
3621 /* Put decls on list in reverse order.
3622 We will reverse them later if necessary. */
3623 TREE_CHAIN (x) = b->names;
3624 b->names = x;
3625 if (! (b != global_binding_level || TREE_PERMANENT (x)))
3626 my_friendly_abort (124);
3627
3628 return x;
3629}
3630
5566b478
MS
3631/* Same as pushdecl, but define X in binding-level LEVEL. We rely on the
3632 caller to set DECL_CONTEXT properly. */
8d08fdba
MS
3633
3634static tree
3635pushdecl_with_scope (x, level)
3636 tree x;
3637 struct binding_level *level;
3638{
8d019cef 3639 register struct binding_level *b;
5566b478 3640 tree function_decl = current_function_decl;
8d08fdba 3641
5566b478 3642 current_function_decl = NULL_TREE;
8d019cef
JM
3643 if (level->parm_flag == 2)
3644 {
3645 b = class_binding_level;
3646 class_binding_level = level;
3647 pushdecl_class_level (x);
3648 class_binding_level = b;
3649 }
3650 else
3651 {
3652 b = current_binding_level;
3653 current_binding_level = level;
3654 x = pushdecl (x);
3655 current_binding_level = b;
3656 }
5566b478 3657 current_function_decl = function_decl;
8d08fdba
MS
3658 return x;
3659}
3660
2c73f9f5 3661/* Like pushdecl, only it places X in the current namespace,
8d08fdba 3662 if appropriate. */
e92cc029 3663
8d08fdba 3664tree
2c73f9f5 3665pushdecl_namespace_level (x)
8d08fdba
MS
3666 tree x;
3667{
3668 register struct binding_level *b = inner_binding_level;
2c73f9f5
ML
3669 register tree t;
3670
3671 t = pushdecl_with_scope (x, NAMESPACE_LEVEL (current_namespace));
8d08fdba
MS
3672
3673 /* Now, the type_shadowed stack may screw us. Munge it so it does
3674 what we want. */
3675 if (TREE_CODE (x) == TYPE_DECL)
3676 {
3677 tree name = DECL_NAME (x);
3678 tree newval;
3679 tree *ptr = (tree *)0;
3680 for (; b != global_binding_level; b = b->level_chain)
3681 {
3682 tree shadowed = b->type_shadowed;
3683 for (; shadowed; shadowed = TREE_CHAIN (shadowed))
3684 if (TREE_PURPOSE (shadowed) == name)
3685 {
3686 ptr = &TREE_VALUE (shadowed);
3687 /* Can't break out of the loop here because sometimes
3688 a binding level will have duplicate bindings for
3689 PT names. It's gross, but I haven't time to fix it. */
3690 }
3691 }
3692 newval = TREE_TYPE (x);
3693 if (ptr == (tree *)0)
3694 {
3695 /* @@ This shouldn't be needed. My test case "zstring.cc" trips
3696 up here if this is changed to an assertion. --KR */
3697 SET_IDENTIFIER_TYPE_VALUE (name, newval);
3698 }
3699 else
3700 {
8d08fdba
MS
3701 *ptr = newval;
3702 }
3703 }
3704 return t;
3705}
3706
2c73f9f5
ML
3707/* Like pushdecl, only it places X in GLOBAL_BINDING_LEVEL,
3708 if appropriate. */
3709
3710tree
3711pushdecl_top_level (x)
3712 tree x;
3713{
3714 tree cur_namespace = current_namespace;
3715 current_namespace = global_namespace;
3716 x = pushdecl_namespace_level (x);
3717 current_namespace = cur_namespace;
3718 return x;
3719}
3720
8d08fdba 3721/* Make the declaration of X appear in CLASS scope. */
e92cc029 3722
8d08fdba
MS
3723tree
3724pushdecl_class_level (x)
3725 tree x;
3726{
3727 /* Don't use DECL_ASSEMBLER_NAME here! Everything that looks in class
3728 scope looks for the pre-mangled name. */
3729 register tree name = DECL_NAME (x);
3730
3731 if (name)
3732 {
8d2733ca
MS
3733 if (TYPE_BEING_DEFINED (current_class_type))
3734 {
3735 /* Check for inconsistent use of this name in the class body.
3736 Types, enums, and static vars are checked here; other
3737 members are checked in finish_struct. */
3738 tree icv = IDENTIFIER_CLASS_VALUE (name);
3739
5566b478 3740 if (icv && icv != x
a4443a08
MS
3741 /* Don't complain about inherited names. */
3742 && id_in_current_class (name)
3743 /* Or shadowed tags. */
3744 && !(TREE_CODE (icv) == TYPE_DECL
3745 && DECL_CONTEXT (icv) == current_class_type))
8d2733ca
MS
3746 {
3747 cp_error ("declaration of identifier `%D' as `%#D'", name, x);
3748 cp_error_at ("conflicts with previous use in class as `%#D'",
3749 icv);
3750 }
3751 }
3752
8d08fdba
MS
3753 push_class_level_binding (name, x);
3754 if (TREE_CODE (x) == TYPE_DECL)
3755 {
3756 set_identifier_type_value (name, TREE_TYPE (x));
8d08fdba
MS
3757 }
3758 }
3759 return x;
3760}
3761
bd6dd845 3762#if 0
7177d104
MS
3763/* This function is used to push the mangled decls for nested types into
3764 the appropriate scope. Previously pushdecl_top_level was used, but that
3765 is incorrect for members of local classes. */
e92cc029 3766
5566b478 3767void
7177d104
MS
3768pushdecl_nonclass_level (x)
3769 tree x;
3770{
3771 struct binding_level *b = current_binding_level;
3772
7177d104 3773 my_friendly_assert (b->parm_flag != 2, 180);
7177d104 3774
5566b478 3775#if 0
7177d104
MS
3776 /* Get out of template binding levels */
3777 while (b->pseudo_global)
3778 b = b->level_chain;
5566b478 3779#endif
7177d104
MS
3780
3781 pushdecl_with_scope (x, b);
3782}
bd6dd845 3783#endif
7177d104 3784
8d08fdba
MS
3785/* Make the declaration(s) of X appear in CLASS scope
3786 under the name NAME. */
e92cc029 3787
8d08fdba
MS
3788void
3789push_class_level_binding (name, x)
3790 tree name;
3791 tree x;
3792{
98c1c668
JM
3793 /* The class_binding_level will be NULL if x is a template
3794 parameter name in a member template. */
3795 if (!class_binding_level)
3796 return;
3797
e1cd6e56
MS
3798 if (TREE_CODE (x) == TYPE_DECL && DECL_ARTIFICIAL (x)
3799 && purpose_member (name, class_binding_level->class_shadowed))
3800 return;
3801
8d08fdba
MS
3802 maybe_push_cache_obstack ();
3803 class_binding_level->class_shadowed
3804 = tree_cons (name, IDENTIFIER_CLASS_VALUE (name),
3805 class_binding_level->class_shadowed);
3806 pop_obstacks ();
3807 IDENTIFIER_CLASS_VALUE (name) = x;
3808 obstack_ptr_grow (&decl_obstack, x);
3809}
3810
2c73f9f5
ML
3811/* Insert another USING_DECL into the current binding level,
3812 returning this declaration. If this is a redeclaration,
3813 do nothing and return NULL_TREE. */
e92cc029 3814
2c73f9f5
ML
3815tree
3816push_using_decl (scope, name)
3817 tree scope;
3818 tree name;
8d08fdba 3819{
2c73f9f5
ML
3820 tree decl;
3821
3822 if (!toplevel_bindings_p ())
3823 {
3824 sorry ("using declaration inside function");
3825 return NULL_TREE;
3826 }
8d08fdba 3827
2c73f9f5
ML
3828 my_friendly_assert (TREE_CODE (scope) == NAMESPACE_DECL, 383);
3829 my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, 384);
3830 for (decl = current_binding_level->usings; decl; decl = TREE_CHAIN (decl))
3831 if (DECL_INITIAL (decl) == scope && DECL_NAME (decl) == name)
3832 break;
3833 if (decl)
3834 return NULL_TREE;
3835 decl = build_lang_decl (USING_DECL, name, void_type_node);
3836 DECL_INITIAL (decl) = scope;
3837 TREE_CHAIN (decl) = current_binding_level->usings;
3838 current_binding_level->usings = decl;
3839 return decl;
8d08fdba
MS
3840}
3841
700f8a87
MS
3842/* DECL is a FUNCTION_DECL which may have other definitions already in
3843 place. We get around this by making the value of the identifier point
3844 to a list of all the things that want to be referenced by that name. It
3845 is then up to the users of that name to decide what to do with that
8d08fdba
MS
3846 list.
3847
3848 DECL may also be a TEMPLATE_DECL, with a FUNCTION_DECL in its DECL_RESULT
3849 slot. It is dealt with the same way.
3850
3851 The value returned may be a previous declaration if we guessed wrong
3852 about what language DECL should belong to (C or C++). Otherwise,
3853 it's always DECL (and never something that's not a _DECL). */
e92cc029 3854
bd6dd845 3855static tree
8d08fdba
MS
3856push_overloaded_decl (decl, forgettable)
3857 tree decl;
3858 int forgettable;
3859{
3860 tree orig_name = DECL_NAME (decl);
700f8a87 3861 tree old;
2c73f9f5 3862 int doing_global = (namespace_bindings_p () || ! forgettable);
700f8a87
MS
3863
3864 if (doing_global)
3865 {
2c73f9f5 3866 old = namespace_binding (orig_name, DECL_CONTEXT (decl));
700f8a87 3867 if (old && TREE_CODE (old) == FUNCTION_DECL
a4443a08 3868 && DECL_ARTIFICIAL (old)
700f8a87
MS
3869 && (DECL_BUILT_IN (old) || DECL_BUILT_IN_NONANSI (old)))
3870 {
a4443a08
MS
3871 if (duplicate_decls (decl, old))
3872 return old;
700f8a87
MS
3873 old = NULL_TREE;
3874 }
3875 }
3876 else
3877 {
3878 old = IDENTIFIER_LOCAL_VALUE (orig_name);
a4443a08 3879
700f8a87
MS
3880 if (! purpose_member (orig_name, current_binding_level->shadowed))
3881 {
3882 current_binding_level->shadowed
3883 = tree_cons (orig_name, old, current_binding_level->shadowed);
3884 old = NULL_TREE;
3885 }
3886 }
8d08fdba 3887
700f8a87 3888 if (old)
8d08fdba 3889 {
e1cd6e56 3890 if (TREE_CODE (old) == TYPE_DECL && DECL_ARTIFICIAL (old))
8926095f 3891 {
700f8a87 3892 tree t = TREE_TYPE (old);
cdf5b885
MS
3893 if (IS_AGGR_TYPE (t) && warn_shadow
3894 && (! DECL_IN_SYSTEM_HEADER (decl)
3895 || ! DECL_IN_SYSTEM_HEADER (old)))
8926095f 3896 cp_warning ("`%#D' hides constructor for `%#T'", decl, t);
5b605f68 3897 old = NULL_TREE;
8926095f 3898 }
700f8a87 3899 else if (is_overloaded_fn (old))
8d08fdba 3900 {
8d08fdba
MS
3901 tree tmp;
3902
2c73f9f5
ML
3903 for (tmp = old; tmp; tmp = OVL_NEXT (tmp))
3904 if (decl == OVL_CURRENT (tmp)
3905 || duplicate_decls (decl, OVL_CURRENT (tmp)))
3906 return OVL_CURRENT (tmp);
8d08fdba 3907 }
e1cd6e56
MS
3908 else
3909 {
3910 cp_error_at ("previous non-function declaration `%#D'", old);
3911 cp_error ("conflicts with function declaration `%#D'", decl);
a9aedbc2 3912 return decl;
e1cd6e56 3913 }
8d08fdba 3914 }
7177d104 3915
700f8a87 3916 if (old || TREE_CODE (decl) == TEMPLATE_DECL)
8d08fdba 3917 {
2c73f9f5
ML
3918 if (old && TREE_CODE (old) != OVERLOAD)
3919 old = ovl_cons (old, NULL_TREE);
3920 old = ovl_cons (decl, old);
8d08fdba
MS
3921 }
3922 else
3923 /* orig_name is not ambiguous. */
700f8a87
MS
3924 old = decl;
3925
3926 if (doing_global)
2c73f9f5 3927 set_namespace_binding (orig_name, DECL_CONTEXT (decl), old);
700f8a87
MS
3928 else
3929 IDENTIFIER_LOCAL_VALUE (orig_name) = old;
3930
8d08fdba
MS
3931 return decl;
3932}
3933\f
3934/* Generate an implicit declaration for identifier FUNCTIONID
3935 as a function of type int (). Print a warning if appropriate. */
3936
3937tree
3938implicitly_declare (functionid)
3939 tree functionid;
3940{
3941 register tree decl;
3942 int temp = allocation_temporary_p ();
3943
3944 push_obstacks_nochange ();
3945
3946 /* Save the decl permanently so we can warn if definition follows.
3947 In ANSI C, warn_implicit is usually false, so the saves little space.
3948 But in C++, it's usually true, hence the extra code. */
d22c8596 3949 if (temp && (! warn_implicit || toplevel_bindings_p ()))
8d08fdba
MS
3950 end_temporary_allocation ();
3951
3952 /* We used to reuse an old implicit decl here,
3953 but this loses with inline functions because it can clobber
3954 the saved decl chains. */
3955 decl = build_lang_decl (FUNCTION_DECL, functionid, default_function_type);
3956
3957 DECL_EXTERNAL (decl) = 1;
3958 TREE_PUBLIC (decl) = 1;
3959
3960 /* ANSI standard says implicit declarations are in the innermost block.
d22c8596 3961 So we record the decl in the standard fashion. */
8d08fdba
MS
3962 pushdecl (decl);
3963 rest_of_decl_compilation (decl, NULL_PTR, 0, 0);
3964
3965 if (warn_implicit
3966 /* Only one warning per identifier. */
3967 && IDENTIFIER_IMPLICIT_DECL (functionid) == NULL_TREE)
3968 {
3969 cp_pedwarn ("implicit declaration of function `%#D'", decl);
3970 }
3971
3972 SET_IDENTIFIER_IMPLICIT_DECL (functionid, decl);
3973
3974 pop_obstacks ();
3975
3976 return decl;
3977}
3978
3979/* Return zero if the declaration NEWDECL is valid
3980 when the declaration OLDDECL (assumed to be for the same name)
3981 has already been seen.
3982 Otherwise return an error message format string with a %s
3983 where the identifier should go. */
3984
3985static char *
3986redeclaration_error_message (newdecl, olddecl)
3987 tree newdecl, olddecl;
3988{
3989 if (TREE_CODE (newdecl) == TYPE_DECL)
3990 {
3991 /* Because C++ can put things into name space for free,
3992 constructs like "typedef struct foo { ... } foo"
3993 would look like an erroneous redeclaration. */
8926095f 3994 if (comptypes (TREE_TYPE (newdecl), TREE_TYPE (olddecl), 0))
8d08fdba
MS
3995 return 0;
3996 else
51c184be 3997 return "redefinition of `%#D'";
8d08fdba
MS
3998 }
3999 else if (TREE_CODE (newdecl) == FUNCTION_DECL)
4000 {
4001 /* If this is a pure function, its olddecl will actually be
4002 the original initialization to `0' (which we force to call
4003 abort()). Don't complain about redefinition in this case. */
4004 if (DECL_LANG_SPECIFIC (olddecl) && DECL_ABSTRACT_VIRTUAL_P (olddecl))
4005 return 0;
4006
2c73f9f5
ML
4007 /* If both functions come from different namespaces, this is not
4008 a redeclaration - this is a conflict with a used function. */
6eb3bb27 4009 if (DECL_NAMESPACE_SCOPE_P (olddecl)
2c73f9f5
ML
4010 && DECL_CONTEXT (olddecl) != DECL_CONTEXT (newdecl))
4011 return "`%D' conflicts with used function";
4012
db5ae43f
MS
4013 /* We'll complain about linkage mismatches in
4014 warn_extern_redeclared_static. */
4015
2c73f9f5 4016 /* Defining the same name twice is no good. */
8d08fdba 4017 if (DECL_INITIAL (olddecl) != NULL_TREE
faae18ab 4018 && DECL_INITIAL (newdecl) != NULL_TREE)
8d08fdba
MS
4019 {
4020 if (DECL_NAME (olddecl) == NULL_TREE)
51c184be 4021 return "`%#D' not declared in class";
8d08fdba 4022 else
51c184be 4023 return "redefinition of `%#D'";
8d08fdba
MS
4024 }
4025 return 0;
4026 }
51c184be
MS
4027 else if (TREE_CODE (newdecl) == TEMPLATE_DECL)
4028 {
ec255269
MS
4029 if ((TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == FUNCTION_DECL
4030 && DECL_INITIAL (DECL_TEMPLATE_RESULT (newdecl))
4031 && DECL_INITIAL (DECL_TEMPLATE_RESULT (olddecl)))
4032 || (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL
4033 && TYPE_SIZE (TREE_TYPE (newdecl))
4034 && TYPE_SIZE (TREE_TYPE (olddecl))))
51c184be
MS
4035 return "redefinition of `%#D'";
4036 return 0;
4037 }
5566b478 4038 else if (toplevel_bindings_p ())
8d08fdba
MS
4039 {
4040 /* Objects declared at top level: */
4041 /* If at least one is a reference, it's ok. */
4042 if (DECL_EXTERNAL (newdecl) || DECL_EXTERNAL (olddecl))
4043 return 0;
8926095f
MS
4044 /* Reject two definitions. */
4045 return "redefinition of `%#D'";
8d08fdba
MS
4046 }
4047 else
4048 {
4049 /* Objects declared with block scope: */
4050 /* Reject two definitions, and reject a definition
4051 together with an external reference. */
4052 if (!(DECL_EXTERNAL (newdecl) && DECL_EXTERNAL (olddecl)))
51c184be 4053 return "redeclaration of `%#D'";
8d08fdba
MS
4054 return 0;
4055 }
4056}
4057\f
4058/* Get the LABEL_DECL corresponding to identifier ID as a label.
4059 Create one if none exists so far for the current function.
4060 This function is called for both label definitions and label references. */
4061
4062tree
4063lookup_label (id)
4064 tree id;
4065{
4066 register tree decl = IDENTIFIER_LABEL_VALUE (id);
4067
4068 if (current_function_decl == NULL_TREE)
4069 {
4070 error ("label `%s' referenced outside of any function",
4071 IDENTIFIER_POINTER (id));
4072 return NULL_TREE;
4073 }
4074
4075 if ((decl == NULL_TREE
4076 || DECL_SOURCE_LINE (decl) == 0)
e349ee73
MS
4077 && (named_label_uses == NULL
4078 || named_label_uses->names_in_scope != current_binding_level->names
4079 || named_label_uses->label_decl != decl))
4080 {
4081 struct named_label_list *new_ent;
4082 new_ent
4083 = (struct named_label_list*)oballoc (sizeof (struct named_label_list));
4084 new_ent->label_decl = decl;
4085 new_ent->names_in_scope = current_binding_level->names;
4086 new_ent->binding_level = current_binding_level;
4087 new_ent->lineno_o_goto = lineno;
4088 new_ent->filename_o_goto = input_filename;
4089 new_ent->next = named_label_uses;
4090 named_label_uses = new_ent;
8d08fdba
MS
4091 }
4092
4093 /* Use a label already defined or ref'd with this name. */
4094 if (decl != NULL_TREE)
4095 {
4096 /* But not if it is inherited and wasn't declared to be inheritable. */
4097 if (DECL_CONTEXT (decl) != current_function_decl
4098 && ! C_DECLARED_LABEL_FLAG (decl))
4099 return shadow_label (id);
4100 return decl;
4101 }
4102
4103 decl = build_decl (LABEL_DECL, id, void_type_node);
4104
cffa8729
MS
4105 /* Make sure every label has an rtx. */
4106 label_rtx (decl);
4107
8d08fdba
MS
4108 /* A label not explicitly declared must be local to where it's ref'd. */
4109 DECL_CONTEXT (decl) = current_function_decl;
4110
4111 DECL_MODE (decl) = VOIDmode;
4112
4113 /* Say where one reference is to the label,
4114 for the sake of the error if it is not defined. */
4115 DECL_SOURCE_LINE (decl) = lineno;
4116 DECL_SOURCE_FILE (decl) = input_filename;
4117
4118 SET_IDENTIFIER_LABEL_VALUE (id, decl);
4119
4120 named_labels = tree_cons (NULL_TREE, decl, named_labels);
e349ee73 4121 named_label_uses->label_decl = decl;
8d08fdba
MS
4122
4123 return decl;
4124}
4125
4126/* Make a label named NAME in the current function,
4127 shadowing silently any that may be inherited from containing functions
4128 or containing scopes.
4129
4130 Note that valid use, if the label being shadowed
4131 comes from another scope in the same function,
4132 requires calling declare_nonlocal_label right away. */
4133
4134tree
4135shadow_label (name)
4136 tree name;
4137{
4138 register tree decl = IDENTIFIER_LABEL_VALUE (name);
4139
4140 if (decl != NULL_TREE)
4141 {
4142 shadowed_labels = tree_cons (NULL_TREE, decl, shadowed_labels);
4143 SET_IDENTIFIER_LABEL_VALUE (name, NULL_TREE);
4144 SET_IDENTIFIER_LABEL_VALUE (decl, NULL_TREE);
4145 }
4146
4147 return lookup_label (name);
4148}
4149
4150/* Define a label, specifying the location in the source file.
4151 Return the LABEL_DECL node for the label, if the definition is valid.
4152 Otherwise return 0. */
4153
4154tree
4155define_label (filename, line, name)
4156 char *filename;
4157 int line;
4158 tree name;
4159{
5566b478
MS
4160 tree decl;
4161
4162 if (minimal_parse_mode)
4163 {
4164 push_obstacks (&permanent_obstack, &permanent_obstack);
4165 decl = build_decl (LABEL_DECL, name, void_type_node);
4166 pop_obstacks ();
4167 DECL_SOURCE_LINE (decl) = line;
4168 DECL_SOURCE_FILE (decl) = filename;
4169 add_tree (decl);
4170 return decl;
4171 }
4172
4173 decl = lookup_label (name);
8d08fdba
MS
4174
4175 /* After labels, make any new cleanups go into their
4176 own new (temporary) binding contour. */
4177 current_binding_level->more_cleanups_ok = 0;
4178
4179 /* If label with this name is known from an outer context, shadow it. */
4180 if (decl != NULL_TREE && DECL_CONTEXT (decl) != current_function_decl)
4181 {
4182 shadowed_labels = tree_cons (NULL_TREE, decl, shadowed_labels);
4183 SET_IDENTIFIER_LABEL_VALUE (name, NULL_TREE);
4184 decl = lookup_label (name);
4185 }
4186
e1cd6e56
MS
4187 if (name == get_identifier ("wchar_t"))
4188 cp_pedwarn ("label named wchar_t");
4189
8d08fdba
MS
4190 if (DECL_INITIAL (decl) != NULL_TREE)
4191 {
4192 cp_error ("duplicate label `%D'", decl);
4193 return 0;
4194 }
4195 else
4196 {
e349ee73 4197 struct named_label_list *uses, *prev;
28cbf42c 4198 int identified = 0;
8d08fdba
MS
4199
4200 /* Mark label as having been defined. */
4201 DECL_INITIAL (decl) = error_mark_node;
4202 /* Say where in the source. */
4203 DECL_SOURCE_FILE (decl) = filename;
4204 DECL_SOURCE_LINE (decl) = line;
4205
e349ee73
MS
4206 prev = NULL;
4207 uses = named_label_uses;
4208 while (uses != NULL)
4209 if (uses->label_decl == decl)
8d08fdba
MS
4210 {
4211 struct binding_level *b = current_binding_level;
4212 while (b)
4213 {
4214 tree new_decls = b->names;
e349ee73
MS
4215 tree old_decls = (b == uses->binding_level)
4216 ? uses->names_in_scope : NULL_TREE;
8d08fdba
MS
4217 while (new_decls != old_decls)
4218 {
4219 if (TREE_CODE (new_decls) == VAR_DECL
4220 /* Don't complain about crossing initialization
4221 of internal entities. They can't be accessed,
4222 and they should be cleaned up
4223 by the time we get to the label. */
700f8a87 4224 && ! DECL_ARTIFICIAL (new_decls)
8d08fdba
MS
4225 && ((DECL_INITIAL (new_decls) != NULL_TREE
4226 && DECL_INITIAL (new_decls) != error_mark_node)
4227 || TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (new_decls))))
4228 {
e349ee73
MS
4229 if (! identified)
4230 {
4231 cp_error ("jump to label `%D'", decl);
4232 error_with_file_and_line (uses->filename_o_goto,
4233 uses->lineno_o_goto,
4234 " from here");
4235 identified = 1;
4236 }
28cbf42c
MS
4237 cp_error_at (" crosses initialization of `%#D'",
4238 new_decls);
8d08fdba
MS
4239 }
4240 new_decls = TREE_CHAIN (new_decls);
4241 }
e349ee73 4242 if (b == uses->binding_level)
8d08fdba
MS
4243 break;
4244 b = b->level_chain;
4245 }
4246
e349ee73
MS
4247 if (prev != NULL)
4248 prev->next = uses->next;
8d08fdba 4249 else
e349ee73
MS
4250 named_label_uses = uses->next;
4251
4252 uses = uses->next;
4253 }
4254 else
4255 {
4256 prev = uses;
4257 uses = uses->next;
8d08fdba
MS
4258 }
4259 current_function_return_value = NULL_TREE;
4260 return decl;
4261 }
4262}
4263
a5894242
MS
4264struct cp_switch
4265{
4266 struct binding_level *level;
4267 struct cp_switch *next;
4268};
4269
4270static struct cp_switch *switch_stack;
4271
4272void
4273push_switch ()
4274{
4275 struct cp_switch *p
4276 = (struct cp_switch *) oballoc (sizeof (struct cp_switch));
4277 p->level = current_binding_level;
4278 p->next = switch_stack;
4279 switch_stack = p;
4280}
4281
4282void
4283pop_switch ()
4284{
4285 switch_stack = switch_stack->next;
4286}
4287
8d08fdba
MS
4288/* Same, but for CASE labels. If DECL is NULL_TREE, it's the default. */
4289/* XXX Note decl is never actually used. (bpk) */
e92cc029 4290
8d08fdba
MS
4291void
4292define_case_label (decl)
4293 tree decl;
4294{
4295 tree cleanup = last_cleanup_this_contour ();
a5894242
MS
4296 struct binding_level *b = current_binding_level;
4297 int identified = 0;
4298
8d08fdba
MS
4299 if (cleanup)
4300 {
4301 static int explained = 0;
a5894242
MS
4302 cp_warning_at ("destructor needed for `%#D'", TREE_PURPOSE (cleanup));
4303 warning ("where case label appears here");
8d08fdba
MS
4304 if (!explained)
4305 {
a5894242
MS
4306 warning ("(enclose actions of previous case statements requiring");
4307 warning ("destructors in their own binding contours.)");
8d08fdba
MS
4308 explained = 1;
4309 }
4310 }
4311
a5894242
MS
4312 for (; b && b != switch_stack->level; b = b->level_chain)
4313 {
4314 tree new_decls = b->names;
4315 for (; new_decls; new_decls = TREE_CHAIN (new_decls))
4316 {
4317 if (TREE_CODE (new_decls) == VAR_DECL
4318 /* Don't complain about crossing initialization
4319 of internal entities. They can't be accessed,
4320 and they should be cleaned up
4321 by the time we get to the label. */
4322 && ! DECL_ARTIFICIAL (new_decls)
4323 && ((DECL_INITIAL (new_decls) != NULL_TREE
4324 && DECL_INITIAL (new_decls) != error_mark_node)
4325 || TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (new_decls))))
4326 {
4327 if (! identified)
4328 error ("jump to case label");
4329 identified = 1;
4330 cp_error_at (" crosses initialization of `%#D'",
4331 new_decls);
4332 }
4333 }
4334 }
4335
8d08fdba
MS
4336 /* After labels, make any new cleanups go into their
4337 own new (temporary) binding contour. */
4338
4339 current_binding_level->more_cleanups_ok = 0;
4340 current_function_return_value = NULL_TREE;
4341}
4342\f
4343/* Return the list of declarations of the current level.
4344 Note that this list is in reverse order unless/until
4345 you nreverse it; and when you do nreverse it, you must
4346 store the result back using `storedecls' or you will lose. */
4347
4348tree
4349getdecls ()
4350{
4351 return current_binding_level->names;
4352}
4353
4354/* Return the list of type-tags (for structs, etc) of the current level. */
4355
4356tree
4357gettags ()
4358{
4359 return current_binding_level->tags;
4360}
4361
4362/* Store the list of declarations of the current level.
4363 This is done for the parameter declarations of a function being defined,
4364 after they are modified in the light of any missing parameters. */
4365
4366static void
4367storedecls (decls)
4368 tree decls;
4369{
4370 current_binding_level->names = decls;
4371}
4372
4373/* Similarly, store the list of tags of the current level. */
4374
4375static void
4376storetags (tags)
4377 tree tags;
4378{
4379 current_binding_level->tags = tags;
4380}
4381\f
4382/* Given NAME, an IDENTIFIER_NODE,
4383 return the structure (or union or enum) definition for that name.
4384 Searches binding levels from BINDING_LEVEL up to the global level.
4385 If THISLEVEL_ONLY is nonzero, searches only the specified context
4386 (but skips any tag-transparent contexts to find one that is
4387 meaningful for tags).
4388 FORM says which kind of type the caller wants;
4389 it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
4390 If the wrong kind of type is found, and it's not a template, an error is
4391 reported. */
4392
4393static tree
4394lookup_tag (form, name, binding_level, thislevel_only)
4395 enum tree_code form;
8d08fdba 4396 tree name;
cffa8729 4397 struct binding_level *binding_level;
8d08fdba
MS
4398 int thislevel_only;
4399{
4400 register struct binding_level *level;
4401
4402 for (level = binding_level; level; level = level->level_chain)
4403 {
4404 register tree tail;
4405 if (ANON_AGGRNAME_P (name))
4406 for (tail = level->tags; tail; tail = TREE_CHAIN (tail))
4407 {
4408 /* There's no need for error checking here, because
4409 anon names are unique throughout the compilation. */
4410 if (TYPE_IDENTIFIER (TREE_VALUE (tail)) == name)
4411 return TREE_VALUE (tail);
4412 }
2c73f9f5
ML
4413 else if (level->namespace_p)
4414 /* Do namespace lookup. */
4415 /* XXX: is this a real lookup, considering using-directives etc. ??? */
4416 for (tail = current_namespace; 1; tail = DECL_CONTEXT (tail))
4417 {
4418 tree old = BINDING_TYPE (binding_for_name (name, tail));
4419 /* If it has an original type, it is a typedef, and we
4420 should not return it. */
4421 if (old && DECL_ORIGINAL_TYPE (TYPE_NAME (old)))
4422 old = NULL_TREE;
4423 if (old && TREE_CODE (old) != form
4424 && !(form != ENUMERAL_TYPE && TREE_CODE (old) == TEMPLATE_DECL))
4425 {
4426 cp_error ("`%#D' redeclared as %C", old, form);
4427 return NULL_TREE;
4428 }
4429 if (old)
4430 return old;
4431 if (thislevel_only || tail == global_namespace)
4432 return NULL_TREE;
4433 }
8d08fdba
MS
4434 else
4435 for (tail = level->tags; tail; tail = TREE_CHAIN (tail))
4436 {
a80e4195 4437 if (TREE_PURPOSE (tail) == name)
8d08fdba
MS
4438 {
4439 enum tree_code code = TREE_CODE (TREE_VALUE (tail));
4440 /* Should tighten this up; it'll probably permit
4441 UNION_TYPE and a struct template, for example. */
4442 if (code != form
5566b478 4443 && !(form != ENUMERAL_TYPE && code == TEMPLATE_DECL))
8d08fdba
MS
4444 {
4445 /* Definition isn't the kind we were looking for. */
4446 cp_error ("`%#D' redeclared as %C", TREE_VALUE (tail),
4447 form);
72b7eeff 4448 return NULL_TREE;
8d08fdba
MS
4449 }
4450 return TREE_VALUE (tail);
4451 }
4452 }
4453 if (thislevel_only && ! level->tag_transparent)
5566b478
MS
4454 {
4455 if (level->pseudo_global)
4456 {
30394414 4457 tree t = IDENTIFIER_NAMESPACE_VALUE (name);
5566b478
MS
4458 if (t && TREE_CODE (t) == TEMPLATE_DECL
4459 && TREE_CODE (DECL_TEMPLATE_RESULT (t)) == TYPE_DECL)
4460 return TREE_TYPE (t);
4461 }
4462 return NULL_TREE;
4463 }
2c73f9f5 4464 if (current_class_type && level->level_chain->namespace_p)
8d08fdba
MS
4465 {
4466 /* Try looking in this class's tags before heading into
4467 global binding level. */
4468 tree context = current_class_type;
4469 while (context)
4470 {
4471 switch (TREE_CODE_CLASS (TREE_CODE (context)))
4472 {
4473 tree these_tags;
4474 case 't':
4475 these_tags = CLASSTYPE_TAGS (context);
4476 if (ANON_AGGRNAME_P (name))
4477 while (these_tags)
4478 {
4479 if (TYPE_IDENTIFIER (TREE_VALUE (these_tags))
4480 == name)
4481 return TREE_VALUE (tail);
4482 these_tags = TREE_CHAIN (these_tags);
4483 }
4484 else
4485 while (these_tags)
4486 {
4487 if (TREE_PURPOSE (these_tags) == name)
4488 {
4489 if (TREE_CODE (TREE_VALUE (these_tags)) != form)
4490 {
4491 cp_error ("`%#D' redeclared as %C in class scope",
4492 TREE_VALUE (tail), form);
72b7eeff 4493 return NULL_TREE;
8d08fdba
MS
4494 }
4495 return TREE_VALUE (tail);
4496 }
4497 these_tags = TREE_CHAIN (these_tags);
4498 }
4499 /* If this type is not yet complete, then don't
4500 look at its context. */
4501 if (TYPE_SIZE (context) == NULL_TREE)
4502 goto no_context;
4503 /* Go to next enclosing type, if any. */
d2e5ee5c 4504 context = DECL_CONTEXT (TYPE_MAIN_DECL (context));
8d08fdba
MS
4505 break;
4506 case 'd':
4507 context = DECL_CONTEXT (context);
4508 break;
4509 default:
4510 my_friendly_abort (10);
4511 }
4512 continue;
4513 no_context:
4514 break;
4515 }
4516 }
4517 }
4518 return NULL_TREE;
4519}
4520
bd6dd845 4521#if 0
8d08fdba
MS
4522void
4523set_current_level_tags_transparency (tags_transparent)
4524 int tags_transparent;
4525{
4526 current_binding_level->tag_transparent = tags_transparent;
4527}
bd6dd845 4528#endif
8d08fdba
MS
4529
4530/* Given a type, find the tag that was defined for it and return the tag name.
4531 Otherwise return 0. However, the value can never be 0
4532 in the cases in which this is used.
4533
4534 C++: If NAME is non-zero, this is the new name to install. This is
4535 done when replacing anonymous tags with real tag names. */
4536
4537static tree
4538lookup_tag_reverse (type, name)
4539 tree type;
4540 tree name;
4541{
4542 register struct binding_level *level;
4543
4544 for (level = current_binding_level; level; level = level->level_chain)
4545 {
4546 register tree tail;
4547 for (tail = level->tags; tail; tail = TREE_CHAIN (tail))
4548 {
4549 if (TREE_VALUE (tail) == type)
4550 {
4551 if (name)
4552 TREE_PURPOSE (tail) = name;
4553 return TREE_PURPOSE (tail);
4554 }
4555 }
4556 }
4557 return NULL_TREE;
4558}
8d08fdba
MS
4559\f
4560/* Lookup TYPE in CONTEXT (a chain of nested types or a FUNCTION_DECL).
4561 Return the type value, or NULL_TREE if not found. */
e92cc029 4562
8d08fdba
MS
4563static tree
4564lookup_nested_type (type, context)
4565 tree type;
4566 tree context;
4567{
4568 if (context == NULL_TREE)
4569 return NULL_TREE;
4570 while (context)
4571 {
4572 switch (TREE_CODE (context))
4573 {
4574 case TYPE_DECL:
4575 {
4576 tree ctype = TREE_TYPE (context);
4577 tree match = value_member (type, CLASSTYPE_TAGS (ctype));
4578 if (match)
4579 return TREE_VALUE (match);
4580 context = DECL_CONTEXT (context);
4581
4582 /* When we have a nested class whose member functions have
4583 local types (e.g., a set of enums), we'll arrive here
4584 with the DECL_CONTEXT as the actual RECORD_TYPE node for
4585 the enclosing class. Instead, we want to make sure we
4586 come back in here with the TYPE_DECL, not the RECORD_TYPE. */
4587 if (context && TREE_CODE (context) == RECORD_TYPE)
4588 context = TREE_CHAIN (context);
4589 }
4590 break;
4591 case FUNCTION_DECL:
e1cd6e56
MS
4592 if (TYPE_NAME (type) && TYPE_IDENTIFIER (type))
4593 return lookup_name (TYPE_IDENTIFIER (type), 1);
4594 return NULL_TREE;
8d08fdba
MS
4595 default:
4596 my_friendly_abort (12);
4597 }
4598 }
4599 return NULL_TREE;
4600}
4601
a9aedbc2 4602/* Look up NAME in the NAMESPACE. */
e92cc029 4603
a9aedbc2
MS
4604tree
4605lookup_namespace_name (namespace, name)
4606 tree namespace, name;
4607{
2c73f9f5 4608 struct tree_binding _b;
30394414 4609 tree val;
2c73f9f5 4610
30394414
JM
4611 my_friendly_assert (TREE_CODE (namespace) == NAMESPACE_DECL, 370);
4612 my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, 373);
4613
2c73f9f5
ML
4614 val = binding_init (&_b);
4615 if (!qualified_lookup_using_namespace (name, namespace, val))
4616 return error_mark_node;
4617
4618 if (BINDING_VALUE (val))
4619 return BINDING_VALUE (val);
30394414
JM
4620 cp_error ("`%D' undeclared in namespace `%D'", name, namespace);
4621 return error_mark_node;
a9aedbc2
MS
4622}
4623
5566b478
MS
4624tree
4625make_typename_type (context, name)
4626 tree context, name;
4627{
a80e4195 4628 tree t, d;
b2b7d40a 4629 tree fullname;
a80e4195 4630
653cc74a
JM
4631 if (TREE_CODE_CLASS (TREE_CODE (name)) == 't')
4632 name = TYPE_IDENTIFIER (name);
4633 else if (TREE_CODE (name) == TYPE_DECL)
a80e4195 4634 name = DECL_NAME (name);
b2b7d40a
JM
4635
4636 fullname = name;
4637
4638 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
11686454
JM
4639 {
4640 name = TREE_OPERAND (name, 0);
4641 if (TREE_CODE (name) == TEMPLATE_DECL)
4642 name = TREE_OPERAND (fullname, 0) = DECL_NAME (name);
4643 }
b2b7d40a 4644 if (TREE_CODE (name) != IDENTIFIER_NODE)
a80e4195 4645 my_friendly_abort (2000);
5566b478 4646
85b71cf2 4647 if (! uses_template_parms (context)
b77ead33 4648 || currently_open_class (context))
5566b478 4649 {
b2b7d40a
JM
4650 if (TREE_CODE (fullname) == TEMPLATE_ID_EXPR)
4651 {
4652 if (IS_AGGR_TYPE (context))
4653 t = lookup_field (context, name, 0, 0);
4654 else
4655 t = NULL_TREE;
4656
4657 if (t == NULL_TREE || TREE_CODE (t) != TEMPLATE_DECL
4658 || TREE_CODE (DECL_RESULT (t)) != TYPE_DECL)
4659 {
4660 cp_error ("no class template named `%#T' in `%#T'",
4661 name, context);
4662 return error_mark_node;
4663 }
ffb690bd 4664
b2b7d40a
JM
4665 return lookup_template_class (t, TREE_OPERAND (fullname, 1),
4666 NULL_TREE, context);
4667 }
4668 else
5566b478 4669 {
b2b7d40a
JM
4670 if (IS_AGGR_TYPE (context))
4671 t = lookup_field (context, name, 0, 1);
4672 else
4673 t = NULL_TREE;
4674
4675 if (t == NULL_TREE)
4676 {
4677 cp_error ("no type named `%#T' in `%#T'", name, context);
4678 return error_mark_node;
4679 }
1107c4b3 4680
b2b7d40a 4681 return TREE_TYPE (t);
5566b478 4682 }
5566b478
MS
4683 }
4684
5156628f 4685 if (processing_template_decl)
5566b478
MS
4686 push_obstacks (&permanent_obstack, &permanent_obstack);
4687 t = make_lang_type (TYPENAME_TYPE);
b2b7d40a 4688 TYPENAME_TYPE_FULLNAME (t) = fullname;
5566b478 4689 d = build_decl (TYPE_DECL, name, t);
5156628f 4690 if (processing_template_decl)
5566b478
MS
4691 pop_obstacks ();
4692
4693 TYPE_CONTEXT (t) = context;
d2e5ee5c
MS
4694 TYPE_NAME (TREE_TYPE (d)) = d;
4695 TYPE_STUB_DECL (TREE_TYPE (d)) = d;
5566b478 4696 DECL_CONTEXT (d) = context;
7fcdf4c2 4697 CLASSTYPE_GOT_SEMICOLON (t) = 1;
5566b478
MS
4698
4699 return t;
4700}
4701
2c73f9f5
ML
4702/* Select the right _DECL from multiple choices. */
4703
4704static tree
4705select_decl (binding, prefer_type)
4706 tree binding;
4707 int prefer_type;
4708{
4709 tree val;
4710 val = BINDING_VALUE (binding);
4711 /* If we could have a type and
4712 we have nothing or we need a type and have none. */
4713 if (BINDING_TYPE (binding)
4714 && (!val || (prefer_type && TREE_CODE (val) != TYPE_DECL)))
4715 val = TYPE_STUB_DECL (BINDING_TYPE (binding));
4716 /* Don't return non-types if we really prefer types. */
4717 else if (val && prefer_type > 1 && TREE_CODE (val) != TYPE_DECL
4718 && (!looking_for_template || TREE_CODE (val) != TEMPLATE_DECL))
4719 val = NULL_TREE;
4720 return val;
4721}
4722
8d08fdba
MS
4723/* Look up NAME in the current binding level and its superiors in the
4724 namespace of variables, functions and typedefs. Return a ..._DECL
4725 node of some kind representing its definition if there is only one
4726 such declaration, or return a TREE_LIST with all the overloaded
4727 definitions if there are many, or return 0 if it is undefined.
4728
2c73f9f5
ML
4729 If PREFER_TYPE is > 0, we prefer TYPE_DECLs or namespaces.
4730 If PREFER_TYPE is > 1, we reject non-type decls (e.g. namespaces).
a28e3c7f 4731 If PREFER_TYPE is -2, we're being called from yylex(). (UGLY)
f84b4be9
JM
4732 Otherwise we prefer non-TYPE_DECLs.
4733
4734 If NONCLASS is non-zero, we don't look for the NAME in class scope,
4735 using IDENTIFIER_CLASS_VALUE. */
8d08fdba 4736
824b9a4c 4737static tree
700f8a87 4738lookup_name_real (name, prefer_type, nonclass)
8d08fdba 4739 tree name;
700f8a87 4740 int prefer_type, nonclass;
8d08fdba
MS
4741{
4742 register tree val;
a28e3c7f 4743 int yylex = 0;
e1cd6e56 4744 tree from_obj = NULL_TREE;
e76a2646 4745 tree locval, classval;
8d08fdba 4746
a28e3c7f
MS
4747 if (prefer_type == -2)
4748 {
4749 extern int looking_for_typename;
fc378698 4750 tree type = NULL_TREE;
a28e3c7f
MS
4751
4752 yylex = 1;
4753 prefer_type = looking_for_typename;
e1cd6e56 4754
653cc74a
JM
4755 /* std:: becomes :: for now. */
4756 if (got_scope == std_node)
4757 got_scope = void_type_node;
4758
e1cd6e56
MS
4759 if (got_scope)
4760 type = got_scope;
dff6b454 4761 else if (got_object != error_mark_node)
e1cd6e56 4762 type = got_object;
a28e3c7f 4763
e1cd6e56 4764 if (type)
a28e3c7f 4765 {
e1cd6e56 4766 if (type == error_mark_node)
f376e137 4767 return error_mark_node;
a80e4195
MS
4768 if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type))
4769 type = TREE_TYPE (type);
5566b478
MS
4770
4771 type = complete_type (type);
4772
a1774733 4773 if (TREE_CODE (type) == VOID_TYPE)
2c73f9f5
ML
4774 type = global_namespace;
4775 if (TREE_CODE (type) == NAMESPACE_DECL)
a9aedbc2 4776 {
2c73f9f5
ML
4777 struct tree_binding b;
4778 val = binding_init (&b);
4779 if (!qualified_lookup_using_namespace (name, type, val))
4780 return NULL_TREE;
4781 val = select_decl (val, prefer_type);
a9aedbc2 4782 }
5566b478 4783 else if (! IS_AGGR_TYPE (type)
5156628f 4784 || TREE_CODE (type) == TEMPLATE_TYPE_PARM
73b0fce8 4785 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM
5156628f 4786 || TREE_CODE (type) == TYPENAME_TYPE)
e92cc029 4787 /* Someone else will give an error about this if needed. */
a28e3c7f 4788 val = NULL_TREE;
e1cd6e56 4789 else if (TYPE_BEING_DEFINED (type))
700f8a87
MS
4790 {
4791 val = IDENTIFIER_CLASS_VALUE (name);
e1cd6e56 4792 if (val && DECL_CONTEXT (val) != type)
700f8a87
MS
4793 {
4794 struct binding_level *b = class_binding_level;
4795 for (val = NULL_TREE; b; b = b->level_chain)
4796 {
4797 tree t = purpose_member (name, b->class_shadowed);
4798 if (t && TREE_VALUE (t)
e1cd6e56 4799 && DECL_CONTEXT (TREE_VALUE (t)) == type)
700f8a87
MS
4800 {
4801 val = TREE_VALUE (t);
4802 break;
4803 }
4804 }
4805 }
5566b478 4806 if (val == NULL_TREE)
e1cd6e56 4807 val = lookup_field (type, name, 0, 1);
700f8a87 4808 }
e1cd6e56 4809 else if (type == current_class_type)
a28e3c7f 4810 val = IDENTIFIER_CLASS_VALUE (name);
a28e3c7f 4811 else
e1cd6e56 4812 val = lookup_field (type, name, 0, prefer_type);
a28e3c7f 4813 }
e1cd6e56
MS
4814 else
4815 val = NULL_TREE;
4816
594740f3 4817 if (got_scope)
e1cd6e56 4818 goto done;
594740f3 4819 else if (got_object && val)
e1cd6e56 4820 from_obj = val;
a28e3c7f 4821 }
e76a2646
MS
4822
4823 locval = classval = NULL_TREE;
4824
2c73f9f5 4825 if (!current_binding_level->namespace_p
b77ead33
JM
4826 && IDENTIFIER_LOCAL_VALUE (name)
4827 /* Kludge to avoid infinite recursion with identifier_type_value. */
4828 && (prefer_type <= 0
4829 || TREE_CODE (IDENTIFIER_LOCAL_VALUE (name)) == TYPE_DECL))
e76a2646
MS
4830 locval = IDENTIFIER_LOCAL_VALUE (name);
4831
8d08fdba
MS
4832 /* In C++ class fields are between local and global scope,
4833 just before the global scope. */
e76a2646 4834 if (current_class_type && ! nonclass)
8d08fdba 4835 {
e76a2646
MS
4836 classval = IDENTIFIER_CLASS_VALUE (name);
4837 if (classval == NULL_TREE && TYPE_BEING_DEFINED (current_class_type))
700f8a87
MS
4838 /* Try to find values from base classes if we are presently
4839 defining a type. We are presently only interested in
4840 TYPE_DECLs. */
e76a2646 4841 classval = lookup_field (current_class_type, name, 0, 1);
8d08fdba 4842
b3709d9b 4843 /* Add implicit 'typename' to types from template bases. lookup_field
b9082e8a
JM
4844 will do this for us. If classval is actually from an enclosing
4845 scope, lookup_nested_field will get it for us. */
85b71cf2
JM
4846 if (processing_template_decl
4847 && classval && TREE_CODE (classval) == TYPE_DECL
b9082e8a 4848 && ! currently_open_class (DECL_CONTEXT (classval))
b3709d9b 4849 && uses_template_parms (current_class_type)
85b71cf2 4850 && ! DECL_ARTIFICIAL (classval))
b3709d9b 4851 classval = lookup_field (current_class_type, name, 0, 1);
b9082e8a
JM
4852
4853 /* yylex() calls this with -2, since we should never start digging for
4854 the nested name at the point where we haven't even, for example,
4855 created the COMPONENT_REF or anything like that. */
4856 if (classval == NULL_TREE)
4857 classval = lookup_nested_field (name, ! yylex);
e76a2646 4858 }
8d08fdba 4859
e76a2646
MS
4860 if (locval && classval)
4861 {
4862 if (current_scope () == current_function_decl
4863 && ! hack_decl_function_context (current_function_decl))
4864 /* Not in a nested function. */
4865 val = locval;
4866 else
4867 {
4868 /* This is incredibly horrible. The whole concept of
4869 IDENTIFIER_LOCAL_VALUE / IDENTIFIER_CLASS_VALUE /
4870 IDENTIFIER_GLOBAL_VALUE needs to be scrapped for local
4871 classes. */
4872 tree lctx = hack_decl_function_context (locval);
4873 tree cctx = hack_decl_function_context (classval);
4874
4875 if (lctx == current_scope ())
4876 val = locval;
4877 else if (lctx == cctx)
4878 val = classval;
4879 else
4880 /* I don't know which is right; let's just guess for now. */
4881 val = locval;
4882 }
8d08fdba 4883 }
e76a2646
MS
4884 else if (locval)
4885 val = locval;
4886 else if (classval)
4887 val = classval;
8d08fdba 4888 else
30394414 4889 {
2c73f9f5
ML
4890 /* Unscoped lookup of a global, iterate over namespaces,
4891 considering using namespace statements. */
4892 struct tree_binding _binding;
4893 tree b = binding_init (&_binding);
4894 tree initial = current_decl_namespace();
4895 tree scope = initial;
4896 val = NULL_TREE;
4897 while (!val)
4898 {
4899 val = binding_for_name (name, scope);
4900 /* Initialize binding for this context. */
4901 BINDING_VALUE (b) = BINDING_VALUE (val);
4902 BINDING_TYPE (b) = BINDING_TYPE (val);
4903 /* Add all _DECLs seen through using-directives. */
4904 if (!lookup_using_namespace (name, b, initial, scope))
4905 {
4906 /* Give up because of error. */
4907 val = NULL_TREE;
4908 break;
4909 }
4910 val = select_decl (b, prefer_type);
30394414
JM
4911 if (scope == global_namespace)
4912 break;
2c73f9f5 4913 scope = DECL_CONTEXT (scope);
30394414 4914 }
30394414 4915 }
8d08fdba 4916
a28e3c7f 4917 done:
8d08fdba
MS
4918 if (val)
4919 {
c91a56d2 4920 /* This should only warn about types used in qualified-ids. */
e1cd6e56 4921 if (from_obj && from_obj != val)
5566b478 4922 {
c91a56d2
MS
4923 if (looking_for_typename && TREE_CODE (from_obj) == TYPE_DECL
4924 && TREE_CODE (val) == TYPE_DECL
4925 && TREE_TYPE (from_obj) != TREE_TYPE (val))
4926 {
4927 cp_pedwarn ("lookup of `%D' in the scope of `%#T' (`%#T')",
4928 name, got_object, TREE_TYPE (from_obj));
4929 cp_pedwarn (" does not match lookup in the current scope (`%#T')",
4930 TREE_TYPE (val));
4931 }
594740f3
MS
4932
4933 val = from_obj;
5566b478 4934 }
e1cd6e56 4935
cf776105 4936 if ((TREE_CODE (val) == TEMPLATE_DECL && looking_for_template)
a0a33927 4937 || TREE_CODE (val) == TYPE_DECL || prefer_type <= 0)
e1cd6e56 4938 ;
2c73f9f5 4939 /* Caller wants a class-or-namespace-name. */
b77ead33 4940 else if (prefer_type == 1 && TREE_CODE (val) == NAMESPACE_DECL)
2c73f9f5 4941 ;
e1cd6e56 4942 else if (IDENTIFIER_HAS_TYPE_VALUE (name))
d2e5ee5c 4943 val = TYPE_MAIN_DECL (IDENTIFIER_TYPE_VALUE (name));
e1cd6e56
MS
4944 else if (TREE_TYPE (val) == error_mark_node)
4945 val = error_mark_node;
8d08fdba 4946 }
e1cd6e56
MS
4947 else if (from_obj)
4948 val = from_obj;
8d08fdba
MS
4949
4950 return val;
4951}
4952
700f8a87
MS
4953tree
4954lookup_name_nonclass (name)
4955 tree name;
4956{
4957 return lookup_name_real (name, 0, 1);
4958}
4959
2c73f9f5
ML
4960tree
4961lookup_function_nonclass (name, args)
4962 tree name;
4963 tree args;
4964{
4965 return lookup_arg_dependent (name, lookup_name_nonclass (name), args);
4966}
4967
700f8a87
MS
4968tree
4969lookup_name (name, prefer_type)
4970 tree name;
4971 int prefer_type;
4972{
4973 return lookup_name_real (name, prefer_type, 0);
4974}
4975
8d08fdba
MS
4976/* Similar to `lookup_name' but look only at current binding level. */
4977
4978tree
4979lookup_name_current_level (name)
4980 tree name;
4981{
4982 register tree t = NULL_TREE;
4983
2c73f9f5 4984 if (current_binding_level->namespace_p)
8d08fdba 4985 {
30394414 4986 t = IDENTIFIER_NAMESPACE_VALUE (name);
8d08fdba
MS
4987
4988 /* extern "C" function() */
4989 if (t != NULL_TREE && TREE_CODE (t) == TREE_LIST)
4990 t = TREE_VALUE (t);
4991 }
4992 else if (IDENTIFIER_LOCAL_VALUE (name) != NULL_TREE)
4993 {
a4443a08
MS
4994 struct binding_level *b = current_binding_level;
4995 while (1)
4996 {
4997 for (t = b->names; t; t = TREE_CHAIN (t))
e1cd6e56 4998 if (DECL_NAME (t) == name || DECL_ASSEMBLER_NAME (t) == name)
a4443a08
MS
4999 goto out;
5000 if (b->keep == 2)
5001 b = b->level_chain;
5002 else
5003 break;
5004 }
5005 out:
5006 ;
8d08fdba
MS
5007 }
5008
5009 return t;
5010}
5011\f
5012/* Arrange for the user to get a source line number, even when the
5013 compiler is going down in flames, so that she at least has a
5014 chance of working around problems in the compiler. We used to
5015 call error(), but that let the segmentation fault continue
5016 through; now, it's much more passive by asking them to send the
5017 maintainers mail about the problem. */
5018
5019static void
5020signal_catch (sig)
7dee3f36 5021 int sig ATTRIBUTE_UNUSED;
8d08fdba
MS
5022{
5023 signal (SIGSEGV, SIG_DFL);
5024#ifdef SIGIOT
5025 signal (SIGIOT, SIG_DFL);
5026#endif
5027#ifdef SIGILL
5028 signal (SIGILL, SIG_DFL);
5029#endif
5030#ifdef SIGABRT
5031 signal (SIGABRT, SIG_DFL);
5032#endif
5033#ifdef SIGBUS
5034 signal (SIGBUS, SIG_DFL);
5035#endif
5036 my_friendly_abort (0);
5037}
5038
de22184b
MS
5039#if 0
5040/* Unused -- brendan 970107 */
8d08fdba
MS
5041/* Array for holding types considered "built-in". These types
5042 are output in the module in which `main' is defined. */
5043static tree *builtin_type_tdescs_arr;
5044static int builtin_type_tdescs_len, builtin_type_tdescs_max;
de22184b 5045#endif
8d08fdba
MS
5046
5047/* Push the declarations of builtin types into the namespace.
5048 RID_INDEX, if < RID_MAX is the index of the builtin type
5049 in the array RID_POINTERS. NAME is the name used when looking
5050 up the builtin type. TYPE is the _TYPE node for the builtin type. */
5051
5052static void
5053record_builtin_type (rid_index, name, type)
5054 enum rid rid_index;
5055 char *name;
5056 tree type;
5057{
5058 tree rname = NULL_TREE, tname = NULL_TREE;
a703fb38 5059 tree tdecl = NULL_TREE;
8d08fdba
MS
5060
5061 if ((int) rid_index < (int) RID_MAX)
5062 rname = ridpointers[(int) rid_index];
5063 if (name)
5064 tname = get_identifier (name);
5065
5066 TYPE_BUILT_IN (type) = 1;
5067
5068 if (tname)
5069 {
8d08fdba 5070 tdecl = pushdecl (build_decl (TYPE_DECL, tname, type));
8d08fdba
MS
5071 set_identifier_type_value (tname, NULL_TREE);
5072 if ((int) rid_index < (int) RID_MAX)
2c73f9f5
ML
5073 /* Built-in types live in the global namespace. */
5074 SET_IDENTIFIER_GLOBAL_VALUE (tname, tdecl);
8d08fdba
MS
5075 }
5076 if (rname != NULL_TREE)
5077 {
5078 if (tname != NULL_TREE)
5079 {
5080 set_identifier_type_value (rname, NULL_TREE);
2c73f9f5 5081 SET_IDENTIFIER_GLOBAL_VALUE (rname, tdecl);
8d08fdba
MS
5082 }
5083 else
5084 {
8d08fdba 5085 tdecl = pushdecl (build_decl (TYPE_DECL, rname, type));
8d08fdba
MS
5086 set_identifier_type_value (rname, NULL_TREE);
5087 }
5088 }
8d08fdba
MS
5089}
5090
8d08fdba
MS
5091/* Push overloaded decl, in global scope, with one argument so it
5092 can be used as a callback from define_function. */
e92cc029 5093
8d08fdba
MS
5094static void
5095push_overloaded_decl_1 (x)
5096 tree x;
5097{
5098 push_overloaded_decl (x, 0);
5099}
5100
8ccc31eb
MS
5101#ifdef __GNUC__
5102__inline
5103#endif
6b5fbb55
MS
5104tree
5105auto_function (name, type, code)
8ccc31eb
MS
5106 tree name, type;
5107 enum built_in_function code;
5108{
5109 return define_function
49c249e1 5110 (IDENTIFIER_POINTER (name), type, code, push_overloaded_decl_1,
8ccc31eb
MS
5111 IDENTIFIER_POINTER (build_decl_overload (name, TYPE_ARG_TYPES (type),
5112 0)));
5113}
5114
8d08fdba
MS
5115/* Create the predefined scalar types of C,
5116 and some nodes representing standard constants (0, 1, (void *)0).
5117 Initialize the global binding level.
5118 Make definitions for built-in primitive functions. */
5119
5120void
5121init_decl_processing ()
5122{
5123 tree decl;
de22184b 5124 register tree endlink, int_endlink, double_endlink, unsigned_endlink;
8d08fdba 5125 tree fields[20];
8d08fdba 5126 /* Data type of memcpy. */
cffa8729 5127 tree memcpy_ftype, strlen_ftype;
8d08fdba
MS
5128 int wchar_type_size;
5129 tree temp;
5130 tree array_domain_type;
e1cd6e56 5131 extern int flag_strict_prototype;
a703fb38 5132 tree vb_off_identifier = NULL_TREE;
5566b478
MS
5133 /* Function type `char *(char *, char *)' and similar ones */
5134 tree string_ftype_ptr_ptr, int_ftype_string_string;
de22184b
MS
5135 tree sizetype_endlink;
5136 tree ptr_ftype, ptr_ftype_unsigned, ptr_ftype_sizetype;
ca55abae 5137 tree void_ftype, void_ftype_int, void_ftype_ptr, ptr_ftype_void;
8d08fdba
MS
5138
5139 /* Have to make these distinct before we try using them. */
5140 lang_name_cplusplus = get_identifier ("C++");
5141 lang_name_c = get_identifier ("C");
a1774733 5142 lang_name_java = get_identifier ("Java");
8d08fdba 5143
2c73f9f5 5144 /* Enter the global namespace. */
30394414
JM
5145 my_friendly_assert (global_namespace == NULL_TREE, 375);
5146 my_friendly_assert (current_lang_name == NULL_TREE, 375);
5147 current_lang_name = lang_name_cplusplus;
5148 push_namespace (get_identifier ("::"));
5149 global_namespace = current_namespace;
5150 current_lang_name = NULL_TREE;
5151
e1cd6e56
MS
5152 if (flag_strict_prototype == 2)
5153 {
5154 if (pedantic)
5155 strict_prototypes_lang_c = strict_prototypes_lang_cplusplus;
5156 }
5157 else
5158 strict_prototypes_lang_c = flag_strict_prototype;
8926095f 5159
8d08fdba
MS
5160 /* Initially, C. */
5161 current_lang_name = lang_name_c;
5162
5163 current_function_decl = NULL_TREE;
5164 named_labels = NULL_TREE;
e349ee73 5165 named_label_uses = NULL;
8d08fdba
MS
5166 current_binding_level = NULL_BINDING_LEVEL;
5167 free_binding_level = NULL_BINDING_LEVEL;
5168
42976354 5169#ifndef __CYGWIN32__
8d08fdba
MS
5170 /* Because most segmentation signals can be traced back into user
5171 code, catch them and at least give the user a chance of working
e92cc029 5172 around compiler bugs. */
8d08fdba
MS
5173 signal (SIGSEGV, signal_catch);
5174
5175 /* We will also catch aborts in the back-end through signal_catch and
5176 give the user a chance to see where the error might be, and to defeat
5177 aborts in the back-end when there have been errors previously in their
e92cc029 5178 code. */
8d08fdba
MS
5179#ifdef SIGIOT
5180 signal (SIGIOT, signal_catch);
5181#endif
5182#ifdef SIGILL
5183 signal (SIGILL, signal_catch);
5184#endif
5185#ifdef SIGABRT
5186 signal (SIGABRT, signal_catch);
5187#endif
5188#ifdef SIGBUS
5189 signal (SIGBUS, signal_catch);
5190#endif
7834ab39
MS
5191#else /* ndef __CYGWIN32__ */
5192 /* Cygwin32 cannot handle catching signals other than
5193 SIGABRT yet. We hope this will cease to be the case soon. */
5194#ifdef SIGABRT
5195 signal (SIGABRT, signal_catch);
5196#endif
5197#endif /* ndef __CYGWIN32__ */
8d08fdba
MS
5198
5199 gcc_obstack_init (&decl_obstack);
8d08fdba
MS
5200
5201 /* Must lay these out before anything else gets laid out. */
5202 error_mark_node = make_node (ERROR_MARK);
5203 TREE_PERMANENT (error_mark_node) = 1;
5204 TREE_TYPE (error_mark_node) = error_mark_node;
5205 error_mark_list = build_tree_list (error_mark_node, error_mark_node);
5206 TREE_TYPE (error_mark_list) = error_mark_node;
5207
a28e3c7f
MS
5208 /* Make the binding_level structure for global names. */
5209 pushlevel (0);
8d08fdba 5210 global_binding_level = current_binding_level;
2c73f9f5
ML
5211 /* The global level is the namespace level of ::. */
5212 NAMESPACE_LEVEL (global_namespace) = global_binding_level;
5213 declare_namespace_level ();
8d08fdba
MS
5214
5215 this_identifier = get_identifier (THIS_NAME);
5216 in_charge_identifier = get_identifier (IN_CHARGE_NAME);
fc378698
MS
5217 ctor_identifier = get_identifier (CTOR_NAME);
5218 dtor_identifier = get_identifier (DTOR_NAME);
8d08fdba
MS
5219 pfn_identifier = get_identifier (VTABLE_PFN_NAME);
5220 index_identifier = get_identifier (VTABLE_INDEX_NAME);
5221 delta_identifier = get_identifier (VTABLE_DELTA_NAME);
5222 delta2_identifier = get_identifier (VTABLE_DELTA2_NAME);
5223 pfn_or_delta2_identifier = get_identifier ("__pfn_or_delta2");
dff6b454
RK
5224 if (flag_handle_signatures)
5225 {
5226 tag_identifier = get_identifier (SIGTABLE_TAG_NAME);
9dd70aa4
GB
5227 vb_off_identifier = get_identifier (SIGTABLE_VB_OFF_NAME);
5228 vt_off_identifier = get_identifier (SIGTABLE_VT_OFF_NAME);
dff6b454 5229 }
8d08fdba
MS
5230
5231 /* Define `int' and `char' first so that dbx will output them first. */
5232
5233 integer_type_node = make_signed_type (INT_TYPE_SIZE);
5234 record_builtin_type (RID_INT, NULL_PTR, integer_type_node);
5235
5236 /* Define `char', which is like either `signed char' or `unsigned char'
5237 but not the same as either. */
5238
beb53fb8
JM
5239 char_type_node
5240 = (flag_signed_char
5241 ? make_signed_type (CHAR_TYPE_SIZE)
5242 : make_unsigned_type (CHAR_TYPE_SIZE));
8d08fdba
MS
5243 record_builtin_type (RID_CHAR, "char", char_type_node);
5244
5245 long_integer_type_node = make_signed_type (LONG_TYPE_SIZE);
5246 record_builtin_type (RID_LONG, "long int", long_integer_type_node);
5247
5248 unsigned_type_node = make_unsigned_type (INT_TYPE_SIZE);
5249 record_builtin_type (RID_UNSIGNED, "unsigned int", unsigned_type_node);
5250
5251 long_unsigned_type_node = make_unsigned_type (LONG_TYPE_SIZE);
5252 record_builtin_type (RID_MAX, "long unsigned int", long_unsigned_type_node);
5253 record_builtin_type (RID_MAX, "unsigned long", long_unsigned_type_node);
5254
5255 long_long_integer_type_node = make_signed_type (LONG_LONG_TYPE_SIZE);
5256 record_builtin_type (RID_MAX, "long long int", long_long_integer_type_node);
5257
5258 long_long_unsigned_type_node = make_unsigned_type (LONG_LONG_TYPE_SIZE);
5259 record_builtin_type (RID_MAX, "long long unsigned int",
5260 long_long_unsigned_type_node);
5261 record_builtin_type (RID_MAX, "long long unsigned",
5262 long_long_unsigned_type_node);
5263
5156628f
MS
5264 short_integer_type_node = make_signed_type (SHORT_TYPE_SIZE);
5265 record_builtin_type (RID_SHORT, "short int", short_integer_type_node);
5266 short_unsigned_type_node = make_unsigned_type (SHORT_TYPE_SIZE);
5267 record_builtin_type (RID_MAX, "short unsigned int", short_unsigned_type_node);
5268 record_builtin_type (RID_MAX, "unsigned short", short_unsigned_type_node);
5269
8d08fdba 5270 /* `unsigned long' is the standard type for sizeof.
8d08fdba 5271 Note that stddef.h uses `unsigned long',
f5426d1e
R
5272 and this must agree, even if long and int are the same size. */
5273 set_sizetype
5274 (TREE_TYPE (IDENTIFIER_GLOBAL_VALUE (get_identifier (SIZE_TYPE))));
8d08fdba
MS
5275
5276 ptrdiff_type_node
5277 = TREE_TYPE (IDENTIFIER_GLOBAL_VALUE (get_identifier (PTRDIFF_TYPE)));
5278
8d08fdba
MS
5279 /* Define both `signed char' and `unsigned char'. */
5280 signed_char_type_node = make_signed_type (CHAR_TYPE_SIZE);
5281 record_builtin_type (RID_MAX, "signed char", signed_char_type_node);
5282 unsigned_char_type_node = make_unsigned_type (CHAR_TYPE_SIZE);
5283 record_builtin_type (RID_MAX, "unsigned char", unsigned_char_type_node);
5284
5285 /* These are types that type_for_size and type_for_mode use. */
5286 intQI_type_node = make_signed_type (GET_MODE_BITSIZE (QImode));
5287 pushdecl (build_decl (TYPE_DECL, NULL_TREE, intQI_type_node));
5288 intHI_type_node = make_signed_type (GET_MODE_BITSIZE (HImode));
5289 pushdecl (build_decl (TYPE_DECL, NULL_TREE, intHI_type_node));
5290 intSI_type_node = make_signed_type (GET_MODE_BITSIZE (SImode));
5291 pushdecl (build_decl (TYPE_DECL, NULL_TREE, intSI_type_node));
5292 intDI_type_node = make_signed_type (GET_MODE_BITSIZE (DImode));
5293 pushdecl (build_decl (TYPE_DECL, NULL_TREE, intDI_type_node));
f7554e8c
JL
5294 intTI_type_node = make_signed_type (GET_MODE_BITSIZE (TImode));
5295 pushdecl (build_decl (TYPE_DECL, NULL_TREE, intTI_type_node));
8d08fdba
MS
5296 unsigned_intQI_type_node = make_unsigned_type (GET_MODE_BITSIZE (QImode));
5297 pushdecl (build_decl (TYPE_DECL, NULL_TREE, unsigned_intQI_type_node));
5298 unsigned_intHI_type_node = make_unsigned_type (GET_MODE_BITSIZE (HImode));
5299 pushdecl (build_decl (TYPE_DECL, NULL_TREE, unsigned_intHI_type_node));
5300 unsigned_intSI_type_node = make_unsigned_type (GET_MODE_BITSIZE (SImode));
5301 pushdecl (build_decl (TYPE_DECL, NULL_TREE, unsigned_intSI_type_node));
5302 unsigned_intDI_type_node = make_unsigned_type (GET_MODE_BITSIZE (DImode));
5303 pushdecl (build_decl (TYPE_DECL, NULL_TREE, unsigned_intDI_type_node));
f7554e8c
JL
5304 unsigned_intTI_type_node = make_unsigned_type (GET_MODE_BITSIZE (TImode));
5305 pushdecl (build_decl (TYPE_DECL, NULL_TREE, unsigned_intTI_type_node));
8d08fdba
MS
5306
5307 float_type_node = make_node (REAL_TYPE);
5308 TYPE_PRECISION (float_type_node) = FLOAT_TYPE_SIZE;
5309 record_builtin_type (RID_FLOAT, NULL_PTR, float_type_node);
5310 layout_type (float_type_node);
5311
5312 double_type_node = make_node (REAL_TYPE);
5313 if (flag_short_double)
5314 TYPE_PRECISION (double_type_node) = FLOAT_TYPE_SIZE;
5315 else
5316 TYPE_PRECISION (double_type_node) = DOUBLE_TYPE_SIZE;
5317 record_builtin_type (RID_DOUBLE, NULL_PTR, double_type_node);
5318 layout_type (double_type_node);
5319
5320 long_double_type_node = make_node (REAL_TYPE);
5321 TYPE_PRECISION (long_double_type_node) = LONG_DOUBLE_TYPE_SIZE;
5322 record_builtin_type (RID_MAX, "long double", long_double_type_node);
5323 layout_type (long_double_type_node);
5324
37c46b43
MS
5325 complex_integer_type_node = make_node (COMPLEX_TYPE);
5326 pushdecl (build_decl (TYPE_DECL, get_identifier ("complex int"),
5327 complex_integer_type_node));
5328 TREE_TYPE (complex_integer_type_node) = integer_type_node;
5329 layout_type (complex_integer_type_node);
5330
5331 complex_float_type_node = make_node (COMPLEX_TYPE);
5332 pushdecl (build_decl (TYPE_DECL, get_identifier ("complex float"),
5333 complex_float_type_node));
5334 TREE_TYPE (complex_float_type_node) = float_type_node;
5335 layout_type (complex_float_type_node);
5336
5337 complex_double_type_node = make_node (COMPLEX_TYPE);
5338 pushdecl (build_decl (TYPE_DECL, get_identifier ("complex double"),
5339 complex_double_type_node));
5340 TREE_TYPE (complex_double_type_node) = double_type_node;
5341 layout_type (complex_double_type_node);
5342
5343 complex_long_double_type_node = make_node (COMPLEX_TYPE);
5344 pushdecl (build_decl (TYPE_DECL, get_identifier ("complex long double"),
5345 complex_long_double_type_node));
5346 TREE_TYPE (complex_long_double_type_node) = long_double_type_node;
5347 layout_type (complex_long_double_type_node);
5348
8d08fdba
MS
5349 integer_zero_node = build_int_2 (0, 0);
5350 TREE_TYPE (integer_zero_node) = integer_type_node;
5351 integer_one_node = build_int_2 (1, 0);
5352 TREE_TYPE (integer_one_node) = integer_type_node;
5353 integer_two_node = build_int_2 (2, 0);
5354 TREE_TYPE (integer_two_node) = integer_type_node;
5355 integer_three_node = build_int_2 (3, 0);
5356 TREE_TYPE (integer_three_node) = integer_type_node;
8d08fdba 5357
255512c1
JM
5358 boolean_type_node = make_unsigned_type (BOOL_TYPE_SIZE);
5359 TREE_SET_CODE (boolean_type_node, BOOLEAN_TYPE);
c1ea6a0b
BK
5360 TYPE_MAX_VALUE (boolean_type_node) = build_int_2 (1, 0);
5361 TREE_TYPE (TYPE_MAX_VALUE (boolean_type_node)) = boolean_type_node;
5362 TYPE_PRECISION (boolean_type_node) = 1;
255512c1
JM
5363 record_builtin_type (RID_BOOL, "bool", boolean_type_node);
5364 boolean_false_node = build_int_2 (0, 0);
5365 TREE_TYPE (boolean_false_node) = boolean_type_node;
5366 boolean_true_node = build_int_2 (1, 0);
5367 TREE_TYPE (boolean_true_node) = boolean_type_node;
2986ae00 5368
8d08fdba
MS
5369 /* These are needed by stor-layout.c. */
5370 size_zero_node = size_int (0);
5371 size_one_node = size_int (1);
5372
e92cc029
MS
5373 signed_size_zero_node = build_int_2 (0, 0);
5374 TREE_TYPE (signed_size_zero_node) = make_signed_type (TYPE_PRECISION (sizetype));
5375
8d08fdba
MS
5376 void_type_node = make_node (VOID_TYPE);
5377 record_builtin_type (RID_VOID, NULL_PTR, void_type_node);
5378 layout_type (void_type_node); /* Uses integer_zero_node. */
5379 void_list_node = build_tree_list (NULL_TREE, void_type_node);
5380 TREE_PARMLIST (void_list_node) = 1;
5381
d11ad92e 5382 null_pointer_node = build_int_2 (0, 0);
8d08fdba 5383 TREE_TYPE (null_pointer_node) = build_pointer_type (void_type_node);
d11ad92e
MS
5384 layout_type (TREE_TYPE (null_pointer_node));
5385
8d08fdba
MS
5386 /* Used for expressions that do nothing, but are not errors. */
5387 void_zero_node = build_int_2 (0, 0);
5388 TREE_TYPE (void_zero_node) = void_type_node;
5389
5390 string_type_node = build_pointer_type (char_type_node);
beb53fb8
JM
5391 const_string_type_node
5392 = build_pointer_type (build_type_variant (char_type_node, 1, 0));
6b5fbb55 5393#if 0
8d08fdba 5394 record_builtin_type (RID_MAX, NULL_PTR, string_type_node);
6b5fbb55 5395#endif
8d08fdba
MS
5396
5397 /* Make a type to be the domain of a few array types
5398 whose domains don't really matter.
5399 200 is small enough that it always fits in size_t
5400 and large enough that it can hold most function names for the
5401 initializations of __FUNCTION__ and __PRETTY_FUNCTION__. */
5402 array_domain_type = build_index_type (build_int_2 (200, 0));
5403
2c73f9f5 5404 /* Make a type for arrays of characters.
8d08fdba
MS
5405 With luck nothing will ever really depend on the length of this
5406 array type. */
5407 char_array_type_node
5408 = build_array_type (char_type_node, array_domain_type);
5409 /* Likewise for arrays of ints. */
5410 int_array_type_node
5411 = build_array_type (integer_type_node, array_domain_type);
5412
5413 /* This is just some anonymous class type. Nobody should ever
5414 need to look inside this envelope. */
5415 class_star_type_node = build_pointer_type (make_lang_type (RECORD_TYPE));
5416
5417 default_function_type
5418 = build_function_type (integer_type_node, NULL_TREE);
8d08fdba
MS
5419
5420 ptr_type_node = build_pointer_type (void_type_node);
beb53fb8
JM
5421 const_ptr_type_node
5422 = build_pointer_type (build_type_variant (void_type_node, 1, 0));
6b5fbb55 5423#if 0
8d08fdba 5424 record_builtin_type (RID_MAX, NULL_PTR, ptr_type_node);
6b5fbb55 5425#endif
8d08fdba
MS
5426 endlink = void_list_node;
5427 int_endlink = tree_cons (NULL_TREE, integer_type_node, endlink);
5428 double_endlink = tree_cons (NULL_TREE, double_type_node, endlink);
de22184b
MS
5429 unsigned_endlink = tree_cons (NULL_TREE, unsigned_type_node, endlink);
5430
5431 ptr_ftype = build_function_type (ptr_type_node, NULL_TREE);
5432 ptr_ftype_unsigned = build_function_type (ptr_type_node, unsigned_endlink);
5433 sizetype_endlink = tree_cons (NULL_TREE, sizetype, endlink);
5434 /* We realloc here because sizetype could be int or unsigned. S'ok. */
5435 ptr_ftype_sizetype = build_function_type (ptr_type_node, sizetype_endlink);
5436
5437 void_ftype = build_function_type (void_type_node, endlink);
5438 void_ftype_int = build_function_type (void_type_node, int_endlink);
5439 void_ftype_ptr
5440 = build_function_type (void_type_node,
5441 tree_cons (NULL_TREE, ptr_type_node, endlink));
824b9a4c
MS
5442 void_ftype_ptr
5443 = build_exception_variant (void_ftype_ptr,
5444 tree_cons (NULL_TREE, NULL_TREE, NULL_TREE));
8d08fdba 5445
cffa8729
MS
5446 float_ftype_float
5447 = build_function_type (float_type_node,
5448 tree_cons (NULL_TREE, float_type_node, endlink));
5449
8d08fdba
MS
5450 double_ftype_double
5451 = build_function_type (double_type_node, double_endlink);
5452
cffa8729
MS
5453 ldouble_ftype_ldouble
5454 = build_function_type (long_double_type_node,
5455 tree_cons (NULL_TREE, long_double_type_node,
5456 endlink));
5457
8d08fdba
MS
5458 double_ftype_double_double
5459 = build_function_type (double_type_node,
a28e3c7f
MS
5460 tree_cons (NULL_TREE, double_type_node,
5461 double_endlink));
8d08fdba
MS
5462
5463 int_ftype_int
5464 = build_function_type (integer_type_node, int_endlink);
5465
5466 long_ftype_long
5467 = build_function_type (long_integer_type_node,
a28e3c7f
MS
5468 tree_cons (NULL_TREE, long_integer_type_node,
5469 endlink));
8d08fdba 5470
8d08fdba
MS
5471 int_ftype_cptr_cptr_sizet
5472 = build_function_type (integer_type_node,
5473 tree_cons (NULL_TREE, const_ptr_type_node,
5474 tree_cons (NULL_TREE, const_ptr_type_node,
5475 tree_cons (NULL_TREE,
5476 sizetype,
5477 endlink))));
5478
8d08fdba
MS
5479 string_ftype_ptr_ptr /* strcpy prototype */
5480 = build_function_type (string_type_node,
5481 tree_cons (NULL_TREE, string_type_node,
5482 tree_cons (NULL_TREE,
5483 const_string_type_node,
5484 endlink)));
5485
8d08fdba
MS
5486 int_ftype_string_string /* strcmp prototype */
5487 = build_function_type (integer_type_node,
5488 tree_cons (NULL_TREE, const_string_type_node,
5489 tree_cons (NULL_TREE,
5490 const_string_type_node,
5491 endlink)));
5492
cffa8729 5493 strlen_ftype /* strlen prototype */
8d08fdba
MS
5494 = build_function_type (sizetype,
5495 tree_cons (NULL_TREE, const_string_type_node,
5496 endlink));
5497
8d08fdba 5498 memcpy_ftype /* memcpy prototype */
d22c8596 5499 = build_function_type (ptr_type_node,
8d08fdba
MS
5500 tree_cons (NULL_TREE, ptr_type_node,
5501 tree_cons (NULL_TREE, const_ptr_type_node,
de22184b 5502 sizetype_endlink)));
8d08fdba
MS
5503
5504 if (flag_huge_objects)
5505 delta_type_node = long_integer_type_node;
5506 else
5507 delta_type_node = short_integer_type_node;
5508
b9620d0e 5509 builtin_function ("__builtin_constant_p", default_function_type,
8d08fdba
MS
5510 BUILT_IN_CONSTANT_P, NULL_PTR);
5511
beb53fb8
JM
5512 builtin_return_address_fndecl
5513 = builtin_function ("__builtin_return_address", ptr_ftype_unsigned,
5514 BUILT_IN_RETURN_ADDRESS, NULL_PTR);
8926095f 5515
de22184b 5516 builtin_function ("__builtin_frame_address", ptr_ftype_unsigned,
8926095f
MS
5517 BUILT_IN_FRAME_ADDRESS, NULL_PTR);
5518
ca55abae
JM
5519 ptr_ftype_void = build_function_type (ptr_type_node, endlink);
5520 builtin_function ("__builtin_fp", ptr_ftype_void, BUILT_IN_FP, NULL_PTR);
5521 builtin_function ("__builtin_sp", ptr_ftype_void, BUILT_IN_SP, NULL_PTR);
5522
de22184b 5523 builtin_function ("__builtin_alloca", ptr_ftype_sizetype,
8d08fdba 5524 BUILT_IN_ALLOCA, "alloca");
cffa8729 5525 builtin_function ("__builtin_ffs", int_ftype_int, BUILT_IN_FFS, NULL_PTR);
00595019
MS
5526 /* Define alloca, ffs as builtins.
5527 Declare _exit just to mark it as volatile. */
5528 if (! flag_no_builtin && !flag_no_nonansi_builtin)
5529 {
de22184b 5530 temp = builtin_function ("alloca", ptr_ftype_sizetype,
00595019
MS
5531 BUILT_IN_ALLOCA, NULL_PTR);
5532 /* Suppress error if redefined as a non-function. */
5533 DECL_BUILT_IN_NONANSI (temp) = 1;
00595019
MS
5534 temp = builtin_function ("ffs", int_ftype_int, BUILT_IN_FFS, NULL_PTR);
5535 /* Suppress error if redefined as a non-function. */
5536 DECL_BUILT_IN_NONANSI (temp) = 1;
de22184b 5537 temp = builtin_function ("_exit", void_ftype_int,
00595019
MS
5538 NOT_BUILT_IN, NULL_PTR);
5539 TREE_THIS_VOLATILE (temp) = 1;
5540 TREE_SIDE_EFFECTS (temp) = 1;
5541 /* Suppress error if redefined as a non-function. */
5542 DECL_BUILT_IN_NONANSI (temp) = 1;
5543 }
8d08fdba 5544
cffa8729
MS
5545 builtin_function ("__builtin_abs", int_ftype_int, BUILT_IN_ABS, NULL_PTR);
5546 builtin_function ("__builtin_fabsf", float_ftype_float, BUILT_IN_FABS,
5547 NULL_PTR);
5548 builtin_function ("__builtin_fabs", double_ftype_double, BUILT_IN_FABS,
5549 NULL_PTR);
5550 builtin_function ("__builtin_fabsl", ldouble_ftype_ldouble, BUILT_IN_FABS,
5551 NULL_PTR);
8d08fdba
MS
5552 builtin_function ("__builtin_labs", long_ftype_long,
5553 BUILT_IN_LABS, NULL_PTR);
de22184b 5554 builtin_function ("__builtin_saveregs", ptr_ftype,
8d08fdba 5555 BUILT_IN_SAVEREGS, NULL_PTR);
8d08fdba
MS
5556 builtin_function ("__builtin_classify_type", default_function_type,
5557 BUILT_IN_CLASSIFY_TYPE, NULL_PTR);
de22184b 5558 builtin_function ("__builtin_next_arg", ptr_ftype,
8d08fdba 5559 BUILT_IN_NEXT_ARG, NULL_PTR);
de22184b 5560 builtin_function ("__builtin_args_info", int_ftype_int,
8d08fdba 5561 BUILT_IN_ARGS_INFO, NULL_PTR);
eb66be0e
MS
5562 builtin_function ("__builtin_setjmp",
5563 build_function_type (integer_type_node,
5564 tree_cons (NULL_TREE, ptr_type_node,
5565 endlink)),
5566 BUILT_IN_SETJMP, NULL_PTR);
5567 builtin_function ("__builtin_longjmp",
5568 build_function_type (integer_type_node,
5569 tree_cons (NULL_TREE, ptr_type_node,
5570 tree_cons (NULL_TREE,
5571 integer_type_node,
5572 endlink))),
5573 BUILT_IN_LONGJMP, NULL_PTR);
8d08fdba
MS
5574
5575 /* Untyped call and return. */
de22184b 5576 builtin_function ("__builtin_apply_args", ptr_ftype,
8d08fdba
MS
5577 BUILT_IN_APPLY_ARGS, NULL_PTR);
5578
5579 temp = tree_cons (NULL_TREE,
5580 build_pointer_type (build_function_type (void_type_node,
5581 NULL_TREE)),
2c73f9f5 5582 tree_cons (NULL_TREE, ptr_ftype_sizetype, NULL_TREE));
8d08fdba
MS
5583 builtin_function ("__builtin_apply",
5584 build_function_type (ptr_type_node, temp),
5585 BUILT_IN_APPLY, NULL_PTR);
de22184b 5586 builtin_function ("__builtin_return", void_ftype_ptr,
8d08fdba
MS
5587 BUILT_IN_RETURN, NULL_PTR);
5588
5589 /* Currently under experimentation. */
5590 builtin_function ("__builtin_memcpy", memcpy_ftype,
5591 BUILT_IN_MEMCPY, "memcpy");
5592 builtin_function ("__builtin_memcmp", int_ftype_cptr_cptr_sizet,
5593 BUILT_IN_MEMCMP, "memcmp");
5594 builtin_function ("__builtin_strcmp", int_ftype_string_string,
5595 BUILT_IN_STRCMP, "strcmp");
5596 builtin_function ("__builtin_strcpy", string_ftype_ptr_ptr,
5597 BUILT_IN_STRCPY, "strcpy");
cffa8729 5598 builtin_function ("__builtin_strlen", strlen_ftype,
8d08fdba 5599 BUILT_IN_STRLEN, "strlen");
cffa8729
MS
5600 builtin_function ("__builtin_sqrtf", float_ftype_float,
5601 BUILT_IN_FSQRT, "sqrtf");
5602 builtin_function ("__builtin_fsqrt", double_ftype_double,
5603 BUILT_IN_FSQRT, NULL_PTR);
5604 builtin_function ("__builtin_sqrtl", ldouble_ftype_ldouble,
5605 BUILT_IN_FSQRT, "sqrtl");
5606 builtin_function ("__builtin_sinf", float_ftype_float,
5607 BUILT_IN_SIN, "sinf");
5608 builtin_function ("__builtin_sin", double_ftype_double,
5609 BUILT_IN_SIN, "sin");
5610 builtin_function ("__builtin_sinl", ldouble_ftype_ldouble,
5611 BUILT_IN_SIN, "sinl");
5612 builtin_function ("__builtin_cosf", float_ftype_float,
5613 BUILT_IN_COS, "cosf");
5614 builtin_function ("__builtin_cos", double_ftype_double,
5615 BUILT_IN_COS, "cos");
5616 builtin_function ("__builtin_cosl", ldouble_ftype_ldouble,
5617 BUILT_IN_COS, "cosl");
8d08fdba
MS
5618
5619 if (!flag_no_builtin)
5620 {
8d08fdba
MS
5621 builtin_function ("abs", int_ftype_int, BUILT_IN_ABS, NULL_PTR);
5622 builtin_function ("fabs", double_ftype_double, BUILT_IN_FABS, NULL_PTR);
5623 builtin_function ("labs", long_ftype_long, BUILT_IN_LABS, NULL_PTR);
cffa8729
MS
5624 builtin_function ("fabsf", float_ftype_float, BUILT_IN_FABS, NULL_PTR);
5625 builtin_function ("fabsl", ldouble_ftype_ldouble, BUILT_IN_FABS,
5626 NULL_PTR);
8d08fdba
MS
5627 builtin_function ("memcpy", memcpy_ftype, BUILT_IN_MEMCPY, NULL_PTR);
5628 builtin_function ("memcmp", int_ftype_cptr_cptr_sizet, BUILT_IN_MEMCMP,
5629 NULL_PTR);
cffa8729
MS
5630 builtin_function ("strcmp", int_ftype_string_string, BUILT_IN_STRCMP,
5631 NULL_PTR);
a28e3c7f
MS
5632 builtin_function ("strcpy", string_ftype_ptr_ptr, BUILT_IN_STRCPY,
5633 NULL_PTR);
cffa8729
MS
5634 builtin_function ("strlen", strlen_ftype, BUILT_IN_STRLEN, NULL_PTR);
5635 builtin_function ("sqrtf", float_ftype_float, BUILT_IN_FSQRT, NULL_PTR);
5636 builtin_function ("sqrt", double_ftype_double, BUILT_IN_FSQRT, NULL_PTR);
5637 builtin_function ("sqrtl", ldouble_ftype_ldouble, BUILT_IN_FSQRT,
5638 NULL_PTR);
5639 builtin_function ("sinf", float_ftype_float, BUILT_IN_SIN, NULL_PTR);
8d08fdba 5640 builtin_function ("sin", double_ftype_double, BUILT_IN_SIN, NULL_PTR);
cffa8729
MS
5641 builtin_function ("sinl", ldouble_ftype_ldouble, BUILT_IN_SIN, NULL_PTR);
5642 builtin_function ("cosf", float_ftype_float, BUILT_IN_COS, NULL_PTR);
8d08fdba 5643 builtin_function ("cos", double_ftype_double, BUILT_IN_COS, NULL_PTR);
cffa8729 5644 builtin_function ("cosl", ldouble_ftype_ldouble, BUILT_IN_COS, NULL_PTR);
00595019
MS
5645
5646 /* Declare these functions volatile
5647 to avoid spurious "control drops through" warnings. */
de22184b 5648 temp = builtin_function ("abort", void_ftype,
00595019
MS
5649 NOT_BUILT_IN, NULL_PTR);
5650 TREE_THIS_VOLATILE (temp) = 1;
5651 TREE_SIDE_EFFECTS (temp) = 1;
5652 /* Well, these are actually ANSI, but we can't set DECL_BUILT_IN on
5653 them... */
5654 DECL_BUILT_IN_NONANSI (temp) = 1;
de22184b 5655 temp = builtin_function ("exit", void_ftype_int,
00595019
MS
5656 NOT_BUILT_IN, NULL_PTR);
5657 TREE_THIS_VOLATILE (temp) = 1;
5658 TREE_SIDE_EFFECTS (temp) = 1;
5659 DECL_BUILT_IN_NONANSI (temp) = 1;
8d08fdba
MS
5660 }
5661
5662#if 0
5663 /* Support for these has not been written in either expand_builtin
5664 or build_function_call. */
72b7eeff
MS
5665 builtin_function ("__builtin_div", default_ftype, BUILT_IN_DIV, NULL_PTR);
5666 builtin_function ("__builtin_ldiv", default_ftype, BUILT_IN_LDIV, NULL_PTR);
a28e3c7f 5667 builtin_function ("__builtin_ffloor", double_ftype_double, BUILT_IN_FFLOOR,
72b7eeff 5668 NULL_PTR);
cffa8729
MS
5669 builtin_function ("__builtin_fceil", double_ftype_double, BUILT_IN_FCEIL,
5670 NULL_PTR);
a28e3c7f 5671 builtin_function ("__builtin_fmod", double_ftype_double_double,
72b7eeff 5672 BUILT_IN_FMOD, NULL_PTR);
a28e3c7f 5673 builtin_function ("__builtin_frem", double_ftype_double_double,
72b7eeff 5674 BUILT_IN_FREM, NULL_PTR);
cffa8729
MS
5675 builtin_function ("__builtin_memset", ptr_ftype_ptr_int_int,
5676 BUILT_IN_MEMSET, NULL_PTR);
a28e3c7f 5677 builtin_function ("__builtin_getexp", double_ftype_double, BUILT_IN_GETEXP,
72b7eeff 5678 NULL_PTR);
a28e3c7f 5679 builtin_function ("__builtin_getman", double_ftype_double, BUILT_IN_GETMAN,
72b7eeff 5680 NULL_PTR);
8d08fdba
MS
5681#endif
5682
5683 /* C++ extensions */
5684
5685 unknown_type_node = make_node (UNKNOWN_TYPE);
8d08fdba
MS
5686 decl = pushdecl (build_decl (TYPE_DECL, get_identifier ("unknown type"),
5687 unknown_type_node));
5688 /* Make sure the "unknown type" typedecl gets ignored for debug info. */
5689 DECL_IGNORED_P (decl) = 1;
39211cd5 5690 TYPE_DECL_SUPPRESS_DEBUG (decl) = 1;
8d08fdba
MS
5691 TYPE_SIZE (unknown_type_node) = TYPE_SIZE (void_type_node);
5692 TYPE_ALIGN (unknown_type_node) = 1;
5693 TYPE_MODE (unknown_type_node) = TYPE_MODE (void_type_node);
5694 /* Indirecting an UNKNOWN_TYPE node yields an UNKNOWN_TYPE node. */
5695 TREE_TYPE (unknown_type_node) = unknown_type_node;
a6967cc0
JM
5696
5697 if (flag_ansi)
5698 TREE_TYPE (null_node) = type_for_size (POINTER_SIZE, 0);
5699 else
5700 TREE_TYPE (null_node) = build_pointer_type (unknown_type_node);
5701
5702 /* Looking up TYPE_POINTER_TO and TYPE_REFERENCE_TO yield the same
5703 result. */
8d08fdba
MS
5704 TYPE_POINTER_TO (unknown_type_node) = unknown_type_node;
5705 TYPE_REFERENCE_TO (unknown_type_node) = unknown_type_node;
5706
5707 /* This is for handling opaque types in signatures. */
5708 opaque_type_node = copy_node (ptr_type_node);
5709 TYPE_MAIN_VARIANT (opaque_type_node) = opaque_type_node;
5710 record_builtin_type (RID_MAX, 0, opaque_type_node);
5711
e92cc029 5712 /* This is special for C++ so functions can be overloaded. */
8d08fdba
MS
5713 wchar_type_node
5714 = TREE_TYPE (IDENTIFIER_GLOBAL_VALUE (get_identifier (WCHAR_TYPE)));
5715 wchar_type_size = TYPE_PRECISION (wchar_type_node);
5716 signed_wchar_type_node = make_signed_type (wchar_type_size);
5717 unsigned_wchar_type_node = make_unsigned_type (wchar_type_size);
5718 wchar_type_node
5719 = TREE_UNSIGNED (wchar_type_node)
5720 ? unsigned_wchar_type_node
5721 : signed_wchar_type_node;
5722 record_builtin_type (RID_WCHAR, "__wchar_t", wchar_type_node);
5723
f376e137
MS
5724 /* Artificial declaration of wchar_t -- can be bashed */
5725 wchar_decl_node = build_decl (TYPE_DECL, get_identifier ("wchar_t"),
5726 wchar_type_node);
5727 pushdecl (wchar_decl_node);
5728
8d08fdba
MS
5729 /* This is for wide string constants. */
5730 wchar_array_type_node
5731 = build_array_type (wchar_type_node, array_domain_type);
5732
8926095f 5733 if (flag_vtable_thunks)
700f8a87
MS
5734 {
5735 /* Make sure we get a unique function type, so we can give
5736 its pointer type a name. (This wins for gdb.) */
5737 tree vfunc_type = make_node (FUNCTION_TYPE);
5738 TREE_TYPE (vfunc_type) = integer_type_node;
5739 TYPE_ARG_TYPES (vfunc_type) = NULL_TREE;
5740 layout_type (vfunc_type);
5741
5742 vtable_entry_type = build_pointer_type (vfunc_type);
5743 }
8926095f 5744 else
700f8a87
MS
5745 {
5746 vtable_entry_type = make_lang_type (RECORD_TYPE);
5747 fields[0] = build_lang_field_decl (FIELD_DECL, delta_identifier,
5748 delta_type_node);
5749 fields[1] = build_lang_field_decl (FIELD_DECL, index_identifier,
5750 delta_type_node);
5751 fields[2] = build_lang_field_decl (FIELD_DECL, pfn_identifier,
5752 ptr_type_node);
5753 finish_builtin_type (vtable_entry_type, VTBL_PTR_TYPE, fields, 2,
5754 double_type_node);
5755
5756 /* Make this part of an invisible union. */
5757 fields[3] = copy_node (fields[2]);
5758 TREE_TYPE (fields[3]) = delta_type_node;
5759 DECL_NAME (fields[3]) = delta2_identifier;
5760 DECL_MODE (fields[3]) = TYPE_MODE (delta_type_node);
5761 DECL_SIZE (fields[3]) = TYPE_SIZE (delta_type_node);
5762 TREE_UNSIGNED (fields[3]) = 0;
5763 TREE_CHAIN (fields[2]) = fields[3];
5764 vtable_entry_type = build_type_variant (vtable_entry_type, 1, 0);
5765 }
5766 record_builtin_type (RID_MAX, VTBL_PTR_TYPE, vtable_entry_type);
8d08fdba 5767
8d08fdba
MS
5768 vtbl_type_node
5769 = build_array_type (vtable_entry_type, NULL_TREE);
5770 layout_type (vtbl_type_node);
f376e137 5771 vtbl_type_node = cp_build_type_variant (vtbl_type_node, 1, 0);
8d08fdba
MS
5772 record_builtin_type (RID_MAX, NULL_PTR, vtbl_type_node);
5773
5774 /* Simplify life by making a "sigtable_entry_type". Give its
5775 fields names so that the debugger can use them. */
5776
5777 if (flag_handle_signatures)
5778 {
5779 sigtable_entry_type = make_lang_type (RECORD_TYPE);
dff6b454
RK
5780 fields[0] = build_lang_field_decl (FIELD_DECL, tag_identifier,
5781 delta_type_node);
9dd70aa4 5782 fields[1] = build_lang_field_decl (FIELD_DECL, vb_off_identifier,
dff6b454 5783 delta_type_node);
9dd70aa4 5784 fields[2] = build_lang_field_decl (FIELD_DECL, delta_identifier,
dff6b454
RK
5785 delta_type_node);
5786 fields[3] = build_lang_field_decl (FIELD_DECL, index_identifier,
5787 delta_type_node);
9dd70aa4
GB
5788 fields[4] = build_lang_field_decl (FIELD_DECL, pfn_identifier,
5789 ptr_type_node);
5790
5791 /* Set the alignment to the max of the alignment of ptr_type_node and
5792 delta_type_node. Double alignment wastes a word on the Sparc. */
5793 finish_builtin_type (sigtable_entry_type, SIGTABLE_PTR_TYPE, fields, 4,
5794 (TYPE_ALIGN (ptr_type_node) > TYPE_ALIGN (delta_type_node))
5795 ? ptr_type_node
5796 : delta_type_node);
dff6b454
RK
5797
5798 /* Make this part of an invisible union. */
9dd70aa4
GB
5799 fields[5] = copy_node (fields[4]);
5800 TREE_TYPE (fields[5]) = delta_type_node;
5801 DECL_NAME (fields[5]) = vt_off_identifier;
5802 DECL_MODE (fields[5]) = TYPE_MODE (delta_type_node);
5803 DECL_SIZE (fields[5]) = TYPE_SIZE (delta_type_node);
5804 TREE_UNSIGNED (fields[5]) = 0;
5805 TREE_CHAIN (fields[4]) = fields[5];
dff6b454 5806
8d08fdba
MS
5807 sigtable_entry_type = build_type_variant (sigtable_entry_type, 1, 0);
5808 record_builtin_type (RID_MAX, SIGTABLE_PTR_TYPE, sigtable_entry_type);
5809 }
5810
2c73f9f5
ML
5811 std_node = build_decl (NAMESPACE_DECL,
5812 get_identifier (flag_honor_std ? "fake std":"std"),
6633d636
MS
5813 void_type_node);
5814 pushdecl (std_node);
5815
2c73f9f5
ML
5816 global_type_node = make_node (LANG_TYPE);
5817 record_builtin_type (RID_MAX, "global type", global_type_node);
5818
db5ae43f
MS
5819 /* Now, C++. */
5820 current_lang_name = lang_name_cplusplus;
8d08fdba 5821
ced78d8b 5822 {
2c73f9f5
ML
5823 tree bad_alloc_type_node, newtype, deltype;
5824 if (flag_honor_std)
5825 push_namespace (get_identifier ("std"));
5826 bad_alloc_type_node = xref_tag
ced78d8b 5827 (class_type_node, get_identifier ("bad_alloc"), NULL_TREE, 1);
2c73f9f5
ML
5828 if (flag_honor_std)
5829 pop_namespace ();
5830 newtype = build_exception_variant
ced78d8b 5831 (ptr_ftype_sizetype, build_tree_list (NULL_TREE, bad_alloc_type_node));
2c73f9f5 5832 deltype = build_exception_variant
ced78d8b
JM
5833 (void_ftype_ptr, build_tree_list (NULL_TREE, NULL_TREE));
5834 auto_function (ansi_opname[(int) NEW_EXPR], newtype, NOT_BUILT_IN);
5835 auto_function (ansi_opname[(int) VEC_NEW_EXPR], newtype, NOT_BUILT_IN);
5836 auto_function (ansi_opname[(int) DELETE_EXPR], deltype, NOT_BUILT_IN);
5837 auto_function (ansi_opname[(int) VEC_DELETE_EXPR], deltype, NOT_BUILT_IN);
5838 }
8d08fdba
MS
5839
5840 abort_fndecl
de22184b 5841 = define_function ("__pure_virtual", void_ftype,
8d08fdba
MS
5842 NOT_BUILT_IN, 0, 0);
5843
8d08fdba
MS
5844 /* Perform other language dependent initializations. */
5845 init_class_processing ();
5846 init_init_processing ();
5847 init_search_processing ();
1737fe20
BK
5848 if (flag_rtti)
5849 init_rtti_processing ();
8d08fdba 5850
6467930b 5851 if (flag_exceptions)
8d2733ca 5852 init_exception_processing ();
8d08fdba
MS
5853 if (flag_no_inline)
5854 {
5855 flag_inline_functions = 0;
8d08fdba 5856 }
9e9ff709 5857
7fcdf4c2 5858 if (! supports_one_only ())
72b7eeff 5859 flag_weak = 0;
8d08fdba
MS
5860
5861 /* Create the global bindings for __FUNCTION__ and __PRETTY_FUNCTION__. */
5862 declare_function_name ();
5863
5864 /* Prepare to check format strings against argument lists. */
5865 init_function_format_info ();
e9a25f70
JL
5866
5867 /* Show we use EH for cleanups. */
5868 using_eh_for_cleanups ();
8d08fdba
MS
5869}
5870
5871/* Make a definition for a builtin function named NAME and whose data type
5872 is TYPE. TYPE should be a function type with argument types.
5873 FUNCTION_CODE tells later passes how to compile calls to this function.
5874 See tree.h for its possible values.
5875
5876 If LIBRARY_NAME is nonzero, use that for DECL_ASSEMBLER_NAME,
5877 the name to be called if we can't opencode the function. */
5878
5879tree
5880define_function (name, type, function_code, pfn, library_name)
5881 char *name;
5882 tree type;
5883 enum built_in_function function_code;
49c249e1 5884 void (*pfn) PROTO((tree));
8d08fdba
MS
5885 char *library_name;
5886{
5887 tree decl = build_lang_decl (FUNCTION_DECL, get_identifier (name), type);
5888 DECL_EXTERNAL (decl) = 1;
5889 TREE_PUBLIC (decl) = 1;
863adfc0 5890 DECL_ARTIFICIAL (decl) = 1;
8d08fdba 5891
2c73f9f5
ML
5892 my_friendly_assert (DECL_CONTEXT (decl) == NULL_TREE, 392);
5893 DECL_CONTEXT (decl) = current_namespace;
5894
8d08fdba
MS
5895 /* Since `pushdecl' relies on DECL_ASSEMBLER_NAME instead of DECL_NAME,
5896 we cannot change DECL_ASSEMBLER_NAME until we have installed this
5897 function in the namespace. */
5898 if (pfn) (*pfn) (decl);
5899 if (library_name)
5900 DECL_ASSEMBLER_NAME (decl) = get_identifier (library_name);
5901 make_function_rtl (decl);
5902 if (function_code != NOT_BUILT_IN)
5903 {
5904 DECL_BUILT_IN (decl) = 1;
39211cd5 5905 DECL_FUNCTION_CODE (decl) = function_code;
8d08fdba
MS
5906 }
5907 return decl;
5908}
5909\f
5910/* Called when a declaration is seen that contains no names to declare.
5911 If its type is a reference to a structure, union or enum inherited
5912 from a containing scope, shadow that tag name for the current scope
5913 with a forward reference.
5914 If its type defines a new named structure or union
5915 or defines an enum, it is valid but we need not do anything here.
5916 Otherwise, it is an error.
5917
5918 C++: may have to grok the declspecs to learn about static,
5919 complain for anonymous unions. */
5920
5921void
5922shadow_tag (declspecs)
5923 tree declspecs;
5924{
5925 int found_tag = 0;
2986ae00 5926 tree ob_modifier = NULL_TREE;
8d08fdba
MS
5927 register tree link;
5928 register enum tree_code code, ok_code = ERROR_MARK;
5929 register tree t = NULL_TREE;
5930
5931 for (link = declspecs; link; link = TREE_CHAIN (link))
5932 {
5933 register tree value = TREE_VALUE (link);
5934
5935 code = TREE_CODE (value);
5936 if (IS_AGGR_TYPE_CODE (code) || code == ENUMERAL_TYPE)
8d08fdba 5937 {
d2e5ee5c 5938 my_friendly_assert (TYPE_MAIN_DECL (value) != NULL_TREE, 261);
8d08fdba 5939
fc378698 5940 if (IS_AGGR_TYPE (value) && CLASSTYPE_USE_TEMPLATE (value))
5566b478
MS
5941 {
5942 if (CLASSTYPE_IMPLICIT_INSTANTIATION (value)
5943 && TYPE_SIZE (value) == NULL_TREE)
73aad9b9
JM
5944 {
5945 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (value);
5156628f 5946 if (processing_template_decl)
73aad9b9
JM
5947 push_template_decl (TYPE_MAIN_DECL (value));
5948 }
5566b478
MS
5949 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (value))
5950 cp_error ("specialization after instantiation of `%T'", value);
5951 }
5952
b8bca6f2
JM
5953 t = value;
5954 ok_code = code;
5955 found_tag++;
8d08fdba
MS
5956 }
5957 else if (value == ridpointers[(int) RID_STATIC]
2986ae00
MS
5958 || value == ridpointers[(int) RID_EXTERN]
5959 || value == ridpointers[(int) RID_AUTO]
28cbf42c
MS
5960 || value == ridpointers[(int) RID_REGISTER]
5961 || value == ridpointers[(int) RID_INLINE]
5962 || value == ridpointers[(int) RID_VIRTUAL]
5963 || value == ridpointers[(int) RID_EXPLICIT])
2986ae00 5964 ob_modifier = value;
8d08fdba
MS
5965 }
5966
5967 /* This is where the variables in an anonymous union are
5968 declared. An anonymous union declaration looks like:
5969 union { ... } ;
5970 because there is no declarator after the union, the parser
5971 sends that declaration here. */
5972 if (ok_code == UNION_TYPE
5973 && t != NULL_TREE
5974 && ((TREE_CODE (TYPE_NAME (t)) == IDENTIFIER_NODE
5975 && ANON_AGGRNAME_P (TYPE_NAME (t)))
5976 || (TREE_CODE (TYPE_NAME (t)) == TYPE_DECL
5977 && ANON_AGGRNAME_P (TYPE_IDENTIFIER (t)))))
5978 {
d2e5ee5c 5979 /* See also grok_x_components. */
d2e5ee5c
MS
5980 tree *q;
5981
5982 /* Wipe out memory of synthesized methods */
5983 TYPE_HAS_CONSTRUCTOR (t) = 0;
5984 TYPE_HAS_DEFAULT_CONSTRUCTOR (t) = 0;
5985 TYPE_HAS_INIT_REF (t) = 0;
5986 TYPE_HAS_CONST_INIT_REF (t) = 0;
5987 TYPE_HAS_ASSIGN_REF (t) = 0;
5988 TYPE_HAS_ASSIGNMENT (t) = 0;
5989 TYPE_HAS_CONST_ASSIGN_REF (t) = 0;
5990
5991 q = &TYPE_METHODS (t);
5992 while (*q)
5993 {
5994 if (DECL_ARTIFICIAL (*q))
5995 *q = TREE_CHAIN (*q);
5996 else
5997 q = &TREE_CHAIN (*q);
5998 }
f49422da 5999
8d08fdba
MS
6000 /* ANSI C++ June 5 1992 WP 9.5.3. Anonymous unions may not have
6001 function members. */
d2e5ee5c
MS
6002 if (TYPE_METHODS (t))
6003 error ("an anonymous union cannot have function members");
c91a56d2 6004
8d08fdba
MS
6005 if (TYPE_FIELDS (t))
6006 {
a28e3c7f 6007 tree decl = grokdeclarator (NULL_TREE, declspecs, NORMAL, 0,
c11b6f21 6008 NULL_TREE);
8d08fdba
MS
6009 finish_anon_union (decl);
6010 }
8d08fdba
MS
6011 }
6012 else
6013 {
6014 /* Anonymous unions are objects, that's why we only check for
2986ae00 6015 inappropriate specifiers in this branch. */
28cbf42c 6016
2986ae00 6017 if (ob_modifier)
28cbf42c
MS
6018 {
6019 if (ob_modifier == ridpointers[(int) RID_INLINE]
6020 || ob_modifier == ridpointers[(int) RID_VIRTUAL])
6021 cp_error ("`%D' can only be specified for functions", ob_modifier);
6022 else if (ob_modifier == ridpointers[(int) RID_EXPLICIT])
6023 cp_error ("`%D' can only be specified for constructors",
6024 ob_modifier);
6025 else
6026 cp_error ("`%D' can only be specified for objects and functions",
6027 ob_modifier);
6028 }
8d08fdba 6029
b8bca6f2 6030 if (found_tag == 0)
2df06032 6031 cp_error ("abstract declarator used as declaration");
b8bca6f2 6032 else if (found_tag > 1)
2986ae00 6033 pedwarn ("multiple types in one declaration");
8d08fdba
MS
6034 }
6035}
6036\f
6037/* Decode a "typename", such as "int **", returning a ..._TYPE node. */
6038
6039tree
6040groktypename (typename)
6041 tree typename;
6042{
6043 if (TREE_CODE (typename) != TREE_LIST)
6044 return typename;
6045 return grokdeclarator (TREE_VALUE (typename),
6046 TREE_PURPOSE (typename),
c11b6f21 6047 TYPENAME, 0, NULL_TREE);
8d08fdba
MS
6048}
6049
6050/* Decode a declarator in an ordinary declaration or data definition.
6051 This is called as soon as the type information and variable name
6052 have been parsed, before parsing the initializer if any.
6053 Here we create the ..._DECL node, fill in its type,
6054 and put it on the list of decls for the current context.
6055 The ..._DECL node is returned as the value.
6056
6057 Exception: for arrays where the length is not specified,
82580166 6058 the type is left null, to be filled in by `cp_finish_decl'.
8d08fdba
MS
6059
6060 Function definitions do not come here; they go to start_function
6061 instead. However, external and forward declarations of functions
6062 do go through here. Structure field declarations are done by
6063 grokfield and not through here. */
6064
6065/* Set this to zero to debug not using the temporary obstack
6066 to parse initializers. */
6067int debug_temp_inits = 1;
6068
6069tree
a1774733 6070start_decl (declarator, declspecs, initialized, attributes, prefix_attributes)
8d08fdba
MS
6071 tree declarator, declspecs;
6072 int initialized;
a1774733 6073 tree attributes, prefix_attributes;
8d08fdba
MS
6074{
6075 register tree decl;
6076 register tree type, tem;
6077 tree context;
6078 extern int have_extern_spec;
6079 extern int used_extern_spec;
6080
5566b478
MS
6081#if 0
6082 /* See code below that used this. */
8d08fdba 6083 int init_written = initialized;
5566b478 6084#endif
8d08fdba 6085
e92cc029 6086 /* This should only be done once on the top most decl. */
8d08fdba
MS
6087 if (have_extern_spec && !used_extern_spec)
6088 {
a28e3c7f
MS
6089 declspecs = decl_tree_cons (NULL_TREE, get_identifier ("extern"),
6090 declspecs);
8d08fdba
MS
6091 used_extern_spec = 1;
6092 }
6093
c11b6f21 6094 decl = grokdeclarator (declarator, declspecs, NORMAL, initialized,
f30432d7 6095 NULL_TREE);
a1774733 6096 if (decl == NULL_TREE || TREE_CODE (decl) == VOID_TYPE)
8d08fdba
MS
6097 return NULL_TREE;
6098
6099 type = TREE_TYPE (decl);
6100
6101 /* Don't lose if destructors must be executed at file-level. */
5156628f 6102 if (! processing_template_decl && TREE_STATIC (decl)
ec255269 6103 && TYPE_NEEDS_DESTRUCTOR (complete_type (type))
8d08fdba
MS
6104 && !TREE_PERMANENT (decl))
6105 {
6106 push_obstacks (&permanent_obstack, &permanent_obstack);
6107 decl = copy_node (decl);
6108 if (TREE_CODE (type) == ARRAY_TYPE)
6109 {
6110 tree itype = TYPE_DOMAIN (type);
6111 if (itype && ! TREE_PERMANENT (itype))
6112 {
6113 itype = build_index_type (copy_to_permanent (TYPE_MAX_VALUE (itype)));
6114 type = build_cplus_array_type (TREE_TYPE (type), itype);
6115 TREE_TYPE (decl) = type;
6116 }
6117 }
6118 pop_obstacks ();
6119 }
6120
8d08fdba
MS
6121 context
6122 = (TREE_CODE (decl) == FUNCTION_DECL && DECL_VIRTUAL_P (decl))
6123 ? DECL_CLASS_CONTEXT (decl)
6124 : DECL_CONTEXT (decl);
6125
2c73f9f5
ML
6126 /* We are only interested in class contexts, later. */
6127 if (context && TREE_CODE (context) == NAMESPACE_DECL)
6128 context = NULL_TREE;
6129
8d08fdba
MS
6130 if (initialized)
6131 /* Is it valid for this decl to have an initializer at all?
6132 If not, set INITIALIZED to zero, which will indirectly
82580166 6133 tell `cp_finish_decl' to ignore the initializer once it is parsed. */
8d08fdba
MS
6134 switch (TREE_CODE (decl))
6135 {
6136 case TYPE_DECL:
6137 /* typedef foo = bar means give foo the same type as bar.
82580166 6138 We haven't parsed bar yet, so `cp_finish_decl' will fix that up.
8d08fdba
MS
6139 Any other case of an initialization in a TYPE_DECL is an error. */
6140 if (pedantic || list_length (declspecs) > 1)
6141 {
6142 cp_error ("typedef `%D' is initialized", decl);
6143 initialized = 0;
6144 }
6145 break;
6146
6147 case FUNCTION_DECL:
6148 cp_error ("function `%#D' is initialized like a variable", decl);
6149 initialized = 0;
6150 break;
6151
6152 default:
5156628f 6153 if (! processing_template_decl)
8d08fdba 6154 {
bd6dd845 6155 if (type != error_mark_node)
5156628f 6156 {
bd6dd845
MS
6157 if (TYPE_SIZE (type) != NULL_TREE
6158 && ! TREE_CONSTANT (TYPE_SIZE (type)))
6159 {
6160 cp_error
6161 ("variable-sized object `%D' may not be initialized",
6162 decl);
6163 initialized = 0;
6164 }
5156628f 6165
bd6dd845
MS
6166 if (TREE_CODE (type) == ARRAY_TYPE
6167 && TYPE_SIZE (complete_type (TREE_TYPE (type))) == NULL_TREE)
6168 {
6169 cp_error
6170 ("elements of array `%#D' have incomplete type", decl);
6171 initialized = 0;
6172 }
5156628f 6173 }
8d08fdba
MS
6174 }
6175 }
6176
8d08fdba
MS
6177 if (initialized)
6178 {
a9aedbc2 6179 if (! toplevel_bindings_p ()
8d08fdba
MS
6180 && DECL_EXTERNAL (decl))
6181 cp_warning ("declaration of `%#D' has `extern' and is initialized",
6182 decl);
6183 DECL_EXTERNAL (decl) = 0;
5566b478 6184 if (toplevel_bindings_p ())
8d08fdba
MS
6185 TREE_STATIC (decl) = 1;
6186
6187 /* Tell `pushdecl' this is an initialized decl
6188 even though we don't yet have the initializer expression.
82580166 6189 Also tell `cp_finish_decl' it may store the real initializer. */
8d08fdba
MS
6190 DECL_INITIAL (decl) = error_mark_node;
6191 }
6192
5566b478 6193 if (context && TYPE_SIZE (complete_type (context)) != NULL_TREE)
5b605f68
MS
6194 {
6195 if (TREE_CODE (decl) == VAR_DECL)
6196 {
6197 tree field = lookup_field (context, DECL_NAME (decl), 0, 0);
6198 if (field == NULL_TREE || TREE_CODE (field) != VAR_DECL)
6199 cp_error ("`%#D' is not a static member of `%#T'", decl, context);
e349ee73
MS
6200 else
6201 {
6202 if (DECL_CONTEXT (field) != context)
f2d773a2
JM
6203 {
6204 cp_pedwarn ("ANSI C++ does not permit `%T::%D' to be defined as `%T::%D'",
6205 DECL_CONTEXT (field), DECL_NAME (decl),
6206 context, DECL_NAME (decl));
6207 DECL_CONTEXT (decl) = DECL_CONTEXT (field);
6208 }
75650646
MM
6209 /* Static data member are tricky; an in-class initialization
6210 still doesn't provide a definition, so the in-class
6211 declaration will have DECL_EXTERNAL set, but will have an
6212 initialization. Thus, duplicate_decls won't warn
6213 about this situation, and so we check here. */
6214 if (DECL_INITIAL (decl) && DECL_INITIAL (field))
6215 cp_error ("duplicate initialization of %D", decl);
e349ee73
MS
6216 if (duplicate_decls (decl, field))
6217 decl = field;
6218 }
5b605f68 6219 }
f30432d7
MS
6220 else
6221 {
5566b478 6222 tree field = check_classfn (context, decl);
f30432d7
MS
6223 if (field && duplicate_decls (decl, field))
6224 decl = field;
6225 }
6226
6227 /* cp_finish_decl sets DECL_EXTERNAL if DECL_IN_AGGR_P is set. */
5b605f68
MS
6228 if (DECL_LANG_SPECIFIC (decl))
6229 DECL_IN_AGGR_P (decl) = 0;
f30432d7
MS
6230 if (DECL_USE_TEMPLATE (decl) || CLASSTYPE_USE_TEMPLATE (context))
6231 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
6232
6233 /* Stupid stupid stupid stupid (jason 7/21/95) */
6234 if (pedantic && DECL_EXTERNAL (decl)
6235 && ! DECL_TEMPLATE_SPECIALIZATION (decl))
6236 cp_pedwarn ("declaration of `%#D' outside of class is not definition",
6237 decl);
6238
5b605f68
MS
6239 pushclass (context, 2);
6240 }
6241
a1774733
BK
6242 /* Set attributes here so if duplicate decl, will have proper attributes. */
6243 cplus_decl_attributes (decl, attributes, prefix_attributes);
6244
8d08fdba
MS
6245 /* Add this decl to the current binding level, but not if it
6246 comes from another scope, e.g. a static member variable.
6247 TEM may equal DECL or it may be a previous decl of the same name. */
5b605f68 6248
51bcf661
JM
6249 if ((TREE_CODE (decl) != PARM_DECL && DECL_CONTEXT (decl) != NULL_TREE
6250 /* Definitions of namespace members outside their namespace are
6251 possible. */
6252 && TREE_CODE (DECL_CONTEXT (decl)) != NAMESPACE_DECL)
2c73f9f5 6253 || (TREE_CODE (decl) == TEMPLATE_DECL && !namespace_bindings_p ())
75650646
MM
6254 || TREE_CODE (type) == LANG_TYPE
6255 /* The declaration of template specializations does not affect
6256 the functions available for overload resolution, so we do not
6257 call pushdecl. */
e1467ff2 6258 || (TREE_CODE (decl) == FUNCTION_DECL
75650646 6259 && DECL_TEMPLATE_SPECIALIZATION (decl)))
8d08fdba
MS
6260 tem = decl;
6261 else
8926095f 6262 tem = pushdecl (decl);
2ee887f2 6263
5156628f 6264 if (processing_template_decl)
5566b478
MS
6265 {
6266 if (! current_function_decl)
3ac3d9ea 6267 tem = push_template_decl (tem);
5566b478 6268 else if (minimal_parse_mode)
b5effa19 6269 DECL_VINDEX (tem)
5566b478
MS
6270 = build_min_nt (DECL_STMT, copy_to_permanent (declarator),
6271 copy_to_permanent (declspecs),
c11b6f21 6272 NULL_TREE);
5566b478
MS
6273 }
6274
6275
2ee887f2 6276#if ! defined (ASM_OUTPUT_BSS) && ! defined (ASM_OUTPUT_ALIGNED_BSS)
a3203465 6277 /* Tell the back-end to use or not use .common as appropriate. If we say
a50f0918
MS
6278 -fconserve-space, we want this to save .data space, at the expense of
6279 wrong semantics. If we say -fno-conserve-space, we want this to
6280 produce errors about redefs; to do this we force variables into the
6281 data segment. */
a3203465 6282 DECL_COMMON (tem) = flag_conserve_space || ! TREE_PUBLIC (tem);
2ee887f2 6283#endif
8d08fdba 6284
5156628f 6285 if (! processing_template_decl)
5566b478 6286 start_decl_1 (tem);
8d08fdba 6287
5566b478
MS
6288 /* Corresponding pop_obstacks is done in `cp_finish_decl'. */
6289 push_obstacks_nochange ();
8d08fdba 6290
5566b478
MS
6291#if 0
6292 /* We have no way of knowing whether the initializer will need to be
6293 evaluated at run-time or not until we've parsed it, so let's just put
6294 it in the permanent obstack. (jason) */
8d08fdba
MS
6295 if (init_written
6296 && ! (TREE_CODE (tem) == PARM_DECL
6297 || (TREE_READONLY (tem)
6298 && (TREE_CODE (tem) == VAR_DECL
6299 || TREE_CODE (tem) == FIELD_DECL))))
6300 {
6301 /* When parsing and digesting the initializer,
6302 use temporary storage. Do this even if we will ignore the value. */
a9aedbc2 6303 if (toplevel_bindings_p () && debug_temp_inits)
8d08fdba 6304 {
5156628f 6305 if (processing_template_decl
5566b478 6306 || TYPE_NEEDS_CONSTRUCTING (type)
a28e3c7f 6307 || TREE_CODE (type) == REFERENCE_TYPE)
8d08fdba
MS
6308 /* In this case, the initializer must lay down in permanent
6309 storage, since it will be saved until `finish_file' is run. */
6310 ;
6311 else
6312 temporary_allocation ();
6313 }
6314 }
5566b478 6315#endif
8d08fdba 6316
8d08fdba
MS
6317 return tem;
6318}
6319
5566b478
MS
6320void
6321start_decl_1 (decl)
6322 tree decl;
8d08fdba 6323{
5566b478
MS
6324 tree type = TREE_TYPE (decl);
6325 int initialized = (DECL_INITIAL (decl) != NULL_TREE);
8d08fdba 6326
5566b478
MS
6327 /* If this type of object needs a cleanup, and control may
6328 jump past it, make a new binding level so that it is cleaned
6329 up only when it is initialized first. */
6330 if (TYPE_NEEDS_DESTRUCTOR (type)
6331 && current_binding_level->more_cleanups_ok == 0)
6332 pushlevel_temporary (1);
6333
6334 if (initialized)
6335 /* Is it valid for this decl to have an initializer at all?
6336 If not, set INITIALIZED to zero, which will indirectly
6337 tell `cp_finish_decl' to ignore the initializer once it is parsed. */
8d08fdba 6338 {
5566b478
MS
6339 /* Don't allow initializations for incomplete types except for
6340 arrays which might be completed by the initialization. */
6341 if (type == error_mark_node)
6342 ; /* Don't complain again. */
6343 else if (TYPE_SIZE (complete_type (type)) != NULL_TREE)
6344 ; /* A complete type is ok. */
6345 else if (TREE_CODE (type) != ARRAY_TYPE)
8d08fdba 6346 {
5566b478
MS
6347 cp_error ("variable `%#D' has initializer but incomplete type",
6348 decl);
6349 initialized = 0;
6350 }
6351 else if (TYPE_SIZE (complete_type (TREE_TYPE (type))) == NULL_TREE)
6352 {
6353 if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
6354 cp_error ("elements of array `%#D' have incomplete type", decl);
6355 /* else we already gave an error in start_decl. */
6356 initialized = 0;
8d08fdba 6357 }
8d08fdba
MS
6358 }
6359
5566b478
MS
6360 if (!initialized
6361 && TREE_CODE (decl) != TYPE_DECL
6362 && TREE_CODE (decl) != TEMPLATE_DECL
6363 && IS_AGGR_TYPE (type) && ! DECL_EXTERNAL (decl))
8d08fdba 6364 {
5156628f 6365 if ((! processing_template_decl || ! uses_template_parms (type))
7fcdf4c2 6366 && TYPE_SIZE (complete_type (type)) == NULL_TREE)
5566b478
MS
6367 {
6368 cp_error ("aggregate `%#D' has incomplete type and cannot be initialized",
6369 decl);
6370 /* Change the type so that assemble_variable will give
6371 DECL an rtl we can live with: (mem (const_int 0)). */
6372 TREE_TYPE (decl) = error_mark_node;
6373 type = error_mark_node;
6374 }
6375 else
6376 {
6377 /* If any base type in the hierarchy of TYPE needs a constructor,
6378 then we set initialized to 1. This way any nodes which are
6379 created for the purposes of initializing this aggregate
6380 will live as long as it does. This is necessary for global
6381 aggregates which do not have their initializers processed until
6382 the end of the file. */
6383 initialized = TYPE_NEEDS_CONSTRUCTING (type);
6384 }
6385 }
6386
6387#if 0
6388 /* We don't do this yet for GNU C++. */
6389 /* For a local variable, define the RTL now. */
6390 if (! toplevel_bindings_p ()
6391 /* But not if this is a duplicate decl
6392 and we preserved the rtl from the previous one
6393 (which may or may not happen). */
6394 && DECL_RTL (tem) == NULL_RTX)
6395 {
6396 if (TYPE_SIZE (TREE_TYPE (tem)) != NULL_TREE)
6397 expand_decl (tem);
6398 else if (TREE_CODE (TREE_TYPE (tem)) == ARRAY_TYPE
6399 && DECL_INITIAL (tem) != NULL_TREE)
6400 expand_decl (tem);
6401 }
6402#endif
6403
6404 if (! initialized)
6405 DECL_INITIAL (decl) = NULL_TREE;
6406}
6407
6408/* Handle initialization of references.
38e01259 6409 These three arguments are from `cp_finish_decl', and have the
e92cc029
MS
6410 same meaning here that they do there.
6411
6412 Quotes on semantics can be found in ARM 8.4.3. */
6413
5566b478 6414static void
a703fb38 6415grok_reference_init (decl, type, init)
5566b478 6416 tree decl, type, init;
5566b478
MS
6417{
6418 tree tmp;
6419
6420 if (init == NULL_TREE)
6421 {
6422 if ((DECL_LANG_SPECIFIC (decl) == 0
6423 || DECL_IN_AGGR_P (decl) == 0)
6424 && ! DECL_THIS_EXTERN (decl))
6425 {
6426 cp_error ("`%D' declared as reference but not initialized", decl);
6427 if (TREE_CODE (decl) == VAR_DECL)
6428 SET_DECL_REFERENCE_SLOT (decl, error_mark_node);
6429 }
6430 return;
6431 }
6432
6433 if (init == error_mark_node)
6434 return;
6435
6436 if (TREE_CODE (type) == REFERENCE_TYPE
6437 && TREE_CODE (init) == CONSTRUCTOR)
6438 {
6439 cp_error ("ANSI C++ forbids use of initializer list to initialize reference `%D'", decl);
8d08fdba
MS
6440 return;
6441 }
6442
ec255269
MS
6443 if (TREE_TYPE (init) && TREE_CODE (TREE_TYPE (init)) == UNKNOWN_TYPE)
6444 /* decay_conversion is probably wrong for references to functions. */
6445 init = decay_conversion (instantiate_type (TREE_TYPE (type), init, 1));
6446
8d08fdba
MS
6447 if (TREE_CODE (init) == TREE_LIST)
6448 init = build_compound_expr (init);
8d08fdba 6449
8ccc31eb
MS
6450 if (TREE_CODE (TREE_TYPE (init)) == REFERENCE_TYPE)
6451 init = convert_from_reference (init);
6452
8d08fdba
MS
6453 if (TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE
6454 && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE)
6455 {
a3203465 6456 /* Note: default conversion is only called in very special cases. */
8d08fdba
MS
6457 init = default_conversion (init);
6458 }
6459
a3203465 6460 tmp = convert_to_reference
9a3b49ac
MS
6461 (type, init, CONV_IMPLICIT,
6462 LOOKUP_SPECULATIVELY|LOOKUP_NORMAL|DIRECT_BIND, decl);
8d08fdba 6463
a3203465
MS
6464 if (tmp == error_mark_node)
6465 goto fail;
6466 else if (tmp != NULL_TREE)
8d08fdba 6467 {
a3203465 6468 init = tmp;
4c7bdca6 6469 DECL_INITIAL (decl) = save_expr (init);
8d08fdba 6470 }
a3203465 6471 else
8d08fdba 6472 {
a3203465
MS
6473 cp_error ("cannot initialize `%T' from `%T'", type, TREE_TYPE (init));
6474 goto fail;
8d08fdba 6475 }
8d08fdba 6476
8d08fdba
MS
6477 /* ?? Can this be optimized in some cases to
6478 hand back the DECL_INITIAL slot?? */
6479 if (TYPE_SIZE (TREE_TYPE (type)))
6480 {
6481 init = convert_from_reference (decl);
6482 if (TREE_PERMANENT (decl))
6483 init = copy_to_permanent (init);
6484 SET_DECL_REFERENCE_SLOT (decl, init);
6485 }
6486
6487 if (TREE_STATIC (decl) && ! TREE_CONSTANT (DECL_INITIAL (decl)))
6488 {
6489 expand_static_init (decl, DECL_INITIAL (decl));
6490 DECL_INITIAL (decl) = NULL_TREE;
6491 }
6492 return;
6493
6494 fail:
6495 if (TREE_CODE (decl) == VAR_DECL)
6496 SET_DECL_REFERENCE_SLOT (decl, error_mark_node);
6497 return;
6498}
6499
6060a796
MS
6500/* Fill in DECL_INITIAL with some magical value to prevent expand_decl from
6501 mucking with forces it does not comprehend (i.e. initialization with a
6502 constructor). If we are at global scope and won't go into COMMON, fill
6503 it in with a dummy CONSTRUCTOR to force the variable into .data;
6504 otherwise we can use error_mark_node. */
6505
28cbf42c
MS
6506static tree
6507obscure_complex_init (decl, init)
6508 tree decl, init;
6060a796 6509{
28cbf42c
MS
6510 if (! flag_no_inline && TREE_STATIC (decl))
6511 {
6512 if (extract_init (decl, init))
6513 return NULL_TREE;
6514 }
6515
2ee887f2 6516#if ! defined (ASM_OUTPUT_BSS) && ! defined (ASM_OUTPUT_ALIGNED_BSS)
a9aedbc2 6517 if (toplevel_bindings_p () && ! DECL_COMMON (decl))
6060a796
MS
6518 DECL_INITIAL (decl) = build (CONSTRUCTOR, TREE_TYPE (decl), NULL_TREE,
6519 NULL_TREE);
6520 else
2ee887f2 6521#endif
6060a796 6522 DECL_INITIAL (decl) = error_mark_node;
28cbf42c
MS
6523
6524 return init;
6060a796
MS
6525}
6526
8d08fdba
MS
6527/* Finish processing of a declaration;
6528 install its line number and initial value.
6529 If the length of an array type is not known before,
6530 it must be determined now, from the initial value, or it is an error.
6531
6532 Call `pop_obstacks' iff NEED_POP is nonzero.
6533
82580166 6534 For C++, `cp_finish_decl' must be fairly evasive: it must keep initializers
8d08fdba
MS
6535 for aggregates that have constructors alive on the permanent obstack,
6536 so that the global initializing functions can be written at the end.
6537
6538 INIT0 holds the value of an initializer that should be allowed to escape
6539 the normal rules.
6540
6060a796
MS
6541 FLAGS is LOOKUP_ONLYCONVERTING is the = init syntax was used, else 0
6542 if the (init) syntax was used.
6543
8d08fdba 6544 For functions that take default parameters, DECL points to its
82580166 6545 "maximal" instantiation. `cp_finish_decl' must then also declared its
8d08fdba
MS
6546 subsequently lower and lower forms of instantiation, checking for
6547 ambiguity as it goes. This can be sped up later. */
6548
6549void
82580166 6550cp_finish_decl (decl, init, asmspec_tree, need_pop, flags)
8d08fdba
MS
6551 tree decl, init;
6552 tree asmspec_tree;
6553 int need_pop;
6060a796 6554 int flags;
8d08fdba
MS
6555{
6556 register tree type;
a703fb38 6557 tree cleanup = NULL_TREE, ttype = NULL_TREE;
8d08fdba
MS
6558 int was_incomplete;
6559 int temporary = allocation_temporary_p ();
6560 char *asmspec = NULL;
6561 int was_readonly = 0;
0c4b14c4 6562 int already_used = 0;
8d08fdba
MS
6563
6564 /* If this is 0, then we did not change obstacks. */
6565 if (! decl)
6566 {
6567 if (init)
6568 error ("assignment (not initialization) in declaration");
6569 return;
6570 }
6571
a4443a08 6572 /* If a name was specified, get the string. */
8d08fdba 6573 if (asmspec_tree)
8d08fdba 6574 asmspec = TREE_STRING_POINTER (asmspec_tree);
8d08fdba 6575
2c73f9f5
ML
6576 if (init && TREE_CODE (init) == NAMESPACE_DECL)
6577 {
6578 cp_error ("Cannot initialize `%D' to namespace `%D'",
6579 decl, init);
6580 init = NULL_TREE;
6581 }
6582
8d08fdba
MS
6583 /* If the type of the thing we are declaring either has
6584 a constructor, or has a virtual function table pointer,
6585 AND its initialization was accepted by `start_decl',
6586 then we stayed on the permanent obstack through the
6587 declaration, otherwise, changed obstacks as GCC would. */
6588
6589 type = TREE_TYPE (decl);
6590
f376e137 6591 if (type == error_mark_node)
eac293a1 6592 {
a9aedbc2 6593 if (toplevel_bindings_p () && temporary)
eac293a1
MS
6594 end_temporary_allocation ();
6595
6596 return;
6597 }
f376e137 6598
5156628f 6599 if (processing_template_decl)
5566b478
MS
6600 {
6601 if (init && DECL_INITIAL (decl))
6602 DECL_INITIAL (decl) = init;
faf5394a 6603 if (minimal_parse_mode && ! DECL_ARTIFICIAL (decl))
5566b478
MS
6604 {
6605 tree stmt = DECL_VINDEX (decl);
e1467ff2 6606 /* If the decl is declaring a member of a local class (in a
8857f91e
MM
6607 template function), the DECL_VINDEX will either be NULL,
6608 or it will be an actual virtual function index, not a
6609 DECL_STMT. */
6610 if (stmt != NULL_TREE && TREE_CODE (stmt) == DECL_STMT)
75650646
MM
6611 {
6612 DECL_VINDEX (decl) = NULL_TREE;
6613 TREE_OPERAND (stmt, 2) = copy_to_permanent (init);
6614 add_tree (stmt);
6615 }
5566b478 6616 }
8d08fdba 6617
5566b478
MS
6618 goto finish_end0;
6619 }
8d08fdba
MS
6620 /* Take care of TYPE_DECLs up front. */
6621 if (TREE_CODE (decl) == TYPE_DECL)
6622 {
6623 if (init && DECL_INITIAL (decl))
6624 {
6625 /* typedef foo = bar; store the type of bar as the type of foo. */
6626 TREE_TYPE (decl) = type = TREE_TYPE (init);
6627 DECL_INITIAL (decl) = init = NULL_TREE;
6628 }
a0a33927
MS
6629 if (type != error_mark_node
6630 && IS_AGGR_TYPE (type) && DECL_NAME (decl))
8d08fdba
MS
6631 {
6632 if (TREE_TYPE (DECL_NAME (decl)) && TREE_TYPE (decl) != type)
6633 cp_warning ("shadowing previous type declaration of `%#D'", decl);
6634 set_identifier_type_value (DECL_NAME (decl), type);
6635 CLASSTYPE_GOT_SEMICOLON (type) = 1;
6636 }
6637 GNU_xref_decl (current_function_decl, decl);
cffa8729
MS
6638
6639 /* If we have installed this as the canonical typedef for this
6640 type, and that type has not been defined yet, delay emitting
956d6950 6641 the debug information for it, as we will emit it later. */
d2e5ee5c 6642 if (TYPE_MAIN_DECL (TREE_TYPE (decl)) == decl
cffa8729
MS
6643 && TYPE_SIZE (TREE_TYPE (decl)) == NULL_TREE)
6644 TYPE_DECL_SUPPRESS_DEBUG (decl) = 1;
6645
8d08fdba 6646 rest_of_decl_compilation (decl, NULL_PTR,
5566b478 6647 DECL_CONTEXT (decl) == NULL_TREE, at_eof);
8d08fdba
MS
6648 goto finish_end;
6649 }
8d08fdba
MS
6650 if (TREE_CODE (decl) != FUNCTION_DECL)
6651 {
6652 ttype = target_type (type);
8d08fdba
MS
6653 }
6654
6655 if (! DECL_EXTERNAL (decl) && TREE_READONLY (decl)
6656 && TYPE_NEEDS_CONSTRUCTING (type))
6657 {
6658
6659 /* Currently, GNU C++ puts constants in text space, making them
6660 impossible to initialize. In the future, one would hope for
6661 an operating system which understood the difference between
6662 initialization and the running of a program. */
6663 was_readonly = 1;
6664 TREE_READONLY (decl) = 0;
6665 }
6666
6667 if (TREE_CODE (decl) == FIELD_DECL)
6668 {
6669 if (init && init != error_mark_node)
6670 my_friendly_assert (TREE_PERMANENT (init), 147);
6671
6672 if (asmspec)
6673 {
6674 /* This must override the asm specifier which was placed
6060a796 6675 by grokclassfn. Lay this out fresh. */
8d08fdba
MS
6676 DECL_RTL (TREE_TYPE (decl)) = NULL_RTX;
6677 DECL_ASSEMBLER_NAME (decl) = get_identifier (asmspec);
6678 make_decl_rtl (decl, asmspec, 0);
6679 }
6680 }
6681 /* If `start_decl' didn't like having an initialization, ignore it now. */
6682 else if (init != NULL_TREE && DECL_INITIAL (decl) == NULL_TREE)
6683 init = NULL_TREE;
6684 else if (DECL_EXTERNAL (decl))
6685 ;
6686 else if (TREE_CODE (type) == REFERENCE_TYPE
6687 || (TYPE_LANG_SPECIFIC (type) && IS_SIGNATURE_REFERENCE (type)))
6688 {
f376e137
MS
6689 if (TREE_STATIC (decl))
6690 make_decl_rtl (decl, NULL_PTR,
a9aedbc2 6691 toplevel_bindings_p ()
f376e137 6692 || pseudo_global_level_p ());
a703fb38 6693 grok_reference_init (decl, type, init);
8d08fdba
MS
6694 init = NULL_TREE;
6695 }
6696
6697 GNU_xref_decl (current_function_decl, decl);
6698
a0a33927 6699 if (TREE_CODE (decl) == FIELD_DECL)
8d08fdba
MS
6700 ;
6701 else if (TREE_CODE (decl) == CONST_DECL)
6702 {
6703 my_friendly_assert (TREE_CODE (decl) != REFERENCE_TYPE, 148);
6704
6705 DECL_INITIAL (decl) = init;
6706
6707 /* This will keep us from needing to worry about our obstacks. */
6708 my_friendly_assert (init != NULL_TREE, 149);
6709 init = NULL_TREE;
6710 }
6711 else if (init)
6712 {
6713 if (TYPE_HAS_CONSTRUCTOR (type) || TYPE_NEEDS_CONSTRUCTING (type))
6714 {
6715 if (TREE_CODE (type) == ARRAY_TYPE)
6716 init = digest_init (type, init, (tree *) 0);
a3203465 6717 else if (TREE_CODE (init) == CONSTRUCTOR)
8d08fdba 6718 {
f30432d7 6719 if (TYPE_NON_AGGREGATE_CLASS (type))
8d08fdba 6720 {
a28e3c7f
MS
6721 cp_error ("`%D' must be initialized by constructor, not by `{...}'",
6722 decl);
8d08fdba
MS
6723 init = error_mark_node;
6724 }
6725 else
6726 goto dont_use_constructor;
6727 }
8d08fdba
MS
6728 }
6729 else
6730 {
6731 dont_use_constructor:
6732 if (TREE_CODE (init) != TREE_VEC)
6733 init = store_init_value (decl, init);
8d08fdba 6734 }
28cbf42c
MS
6735
6736 if (init)
6737 /* We must hide the initializer so that expand_decl
6738 won't try to do something it does not understand. */
6739 init = obscure_complex_init (decl, init);
8d08fdba 6740 }
a0a33927
MS
6741 else if (DECL_EXTERNAL (decl))
6742 ;
8d08fdba
MS
6743 else if (TREE_CODE_CLASS (TREE_CODE (type)) == 't'
6744 && (IS_AGGR_TYPE (type) || TYPE_NEEDS_CONSTRUCTING (type)))
6745 {
6746 tree ctype = type;
6747 while (TREE_CODE (ctype) == ARRAY_TYPE)
6748 ctype = TREE_TYPE (ctype);
6749 if (! TYPE_NEEDS_CONSTRUCTING (ctype))
6750 {
6751 if (CLASSTYPE_READONLY_FIELDS_NEED_INIT (ctype))
6752 cp_error ("structure `%D' with uninitialized const members", decl);
6753 if (CLASSTYPE_REF_FIELDS_NEED_INIT (ctype))
a28e3c7f
MS
6754 cp_error ("structure `%D' with uninitialized reference members",
6755 decl);
8d08fdba
MS
6756 }
6757
6758 if (TREE_CODE (decl) == VAR_DECL
6759 && !DECL_INITIAL (decl)
6760 && !TYPE_NEEDS_CONSTRUCTING (type)
6761 && (TYPE_READONLY (type) || TREE_READONLY (decl)))
6762 cp_error ("uninitialized const `%D'", decl);
6763
6060a796
MS
6764 if (TYPE_SIZE (type) != NULL_TREE
6765 && TYPE_NEEDS_CONSTRUCTING (type))
28cbf42c 6766 init = obscure_complex_init (decl, NULL_TREE);
8d08fdba
MS
6767 }
6768 else if (TREE_CODE (decl) == VAR_DECL
6769 && TREE_CODE (type) != REFERENCE_TYPE
6770 && (TYPE_READONLY (type) || TREE_READONLY (decl)))
6771 {
6772 /* ``Unless explicitly declared extern, a const object does not have
6773 external linkage and must be initialized. ($8.4; $12.1)'' ARM 7.1.6
6774 However, if it's `const int foo = 1; const int foo;', don't complain
6775 about the second decl, since it does have an initializer before.
6776 We deliberately don't complain about arrays, because they're
6777 supposed to be initialized by a constructor. */
6778 if (! DECL_INITIAL (decl)
6779 && TREE_CODE (type) != ARRAY_TYPE
6780 && (!pedantic || !current_class_type))
6781 cp_error ("uninitialized const `%#D'", decl);
6782 }
6783
6784 /* For top-level declaration, the initial value was read in
6785 the temporary obstack. MAXINDEX, rtl, etc. to be made below
6786 must go in the permanent obstack; but don't discard the
6787 temporary data yet. */
6788
a9aedbc2 6789 if (toplevel_bindings_p () && temporary)
8d08fdba
MS
6790 end_temporary_allocation ();
6791
6792 /* Deduce size of array from initialization, if not already known. */
6793
6794 if (TREE_CODE (type) == ARRAY_TYPE
6795 && TYPE_DOMAIN (type) == NULL_TREE
6796 && TREE_CODE (decl) != TYPE_DECL)
6797 {
6798 int do_default
6799 = (TREE_STATIC (decl)
6800 /* Even if pedantic, an external linkage array
6801 may have incomplete type at first. */
6802 ? pedantic && ! DECL_EXTERNAL (decl)
6803 : !DECL_EXTERNAL (decl));
6804 tree initializer = init ? init : DECL_INITIAL (decl);
6805 int failure = complete_array_type (type, initializer, do_default);
6806
6807 if (failure == 1)
6808 cp_error ("initializer fails to determine size of `%D'", decl);
6809
6810 if (failure == 2)
6811 {
6812 if (do_default)
6813 cp_error ("array size missing in `%D'", decl);
6814 /* If a `static' var's size isn't known, make it extern as
6815 well as static, so it does not get allocated. If it's not
6816 `static', then don't mark it extern; finish_incomplete_decl
6817 will give it a default size and it will get allocated. */
6818 else if (!pedantic && TREE_STATIC (decl) && !TREE_PUBLIC (decl))
6819 DECL_EXTERNAL (decl) = 1;
6820 }
6821
6822 if (pedantic && TYPE_DOMAIN (type) != NULL_TREE
6823 && tree_int_cst_lt (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
6824 integer_zero_node))
6825 cp_error ("zero-size array `%D'", decl);
6826
6827 layout_decl (decl, 0);
6828 }
6829
6830 if (TREE_CODE (decl) == VAR_DECL)
6831 {
6832 if (DECL_SIZE (decl) == NULL_TREE
ec255269 6833 && TYPE_SIZE (complete_type (TREE_TYPE (decl))) != NULL_TREE)
8d08fdba
MS
6834 layout_decl (decl, 0);
6835
6836 if (TREE_STATIC (decl) && DECL_SIZE (decl) == NULL_TREE)
6837 {
6838 /* A static variable with an incomplete type:
6839 that is an error if it is initialized.
6840 Otherwise, let it through, but if it is not `extern'
6841 then it may cause an error message later. */
6842 if (DECL_INITIAL (decl) != NULL_TREE)
6843 cp_error ("storage size of `%D' isn't known", decl);
6844 init = NULL_TREE;
6845 }
6846 else if (!DECL_EXTERNAL (decl) && DECL_SIZE (decl) == NULL_TREE)
6847 {
6848 /* An automatic variable with an incomplete type: that is an error.
6849 Don't talk about array types here, since we took care of that
6850 message in grokdeclarator. */
6851 cp_error ("storage size of `%D' isn't known", decl);
6852 TREE_TYPE (decl) = error_mark_node;
6853 }
6854 else if (!DECL_EXTERNAL (decl) && IS_AGGR_TYPE (ttype))
6855 /* Let debugger know it should output info for this type. */
6856 note_debug_info_needed (ttype);
6857
6eb3bb27 6858 if (TREE_STATIC (decl) && DECL_CLASS_SCOPE_P (decl))
d2e5ee5c
MS
6859 note_debug_info_needed (DECL_CONTEXT (decl));
6860
8d08fdba
MS
6861 if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl))
6862 && DECL_SIZE (decl) != NULL_TREE
6863 && ! TREE_CONSTANT (DECL_SIZE (decl)))
6864 {
6865 if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
6866 constant_expression_warning (DECL_SIZE (decl));
6867 else
6868 cp_error ("storage size of `%D' isn't constant", decl);
6869 }
6870
c91a56d2
MS
6871 if (! DECL_EXTERNAL (decl) && TYPE_NEEDS_DESTRUCTOR (type)
6872 /* Cleanups for static variables are handled by `finish_file'. */
6873 && ! TREE_STATIC (decl))
8d08fdba
MS
6874 {
6875 int yes = suspend_momentary ();
2ee887f2 6876 cleanup = maybe_build_cleanup (decl);
8d08fdba
MS
6877 resume_momentary (yes);
6878 }
6879 }
6880 /* PARM_DECLs get cleanups, too. */
6881 else if (TREE_CODE (decl) == PARM_DECL && TYPE_NEEDS_DESTRUCTOR (type))
6882 {
6883 if (temporary)
6884 end_temporary_allocation ();
6885 cleanup = maybe_build_cleanup (decl);
6886 if (temporary)
6887 resume_temporary_allocation ();
6888 }
6889
6890 /* Output the assembler code and/or RTL code for variables and functions,
6891 unless the type is an undefined structure or union.
6892 If not, it will get done when the type is completed. */
6893
5566b478
MS
6894 was_incomplete = (DECL_SIZE (decl) == NULL_TREE);
6895
8d08fdba
MS
6896 if (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == FUNCTION_DECL
6897 || TREE_CODE (decl) == RESULT_DECL)
6898 {
6899 /* ??? FIXME: What about nested classes? */
e1467ff2 6900 int toplev = toplevel_bindings_p () || pseudo_global_level_p ();
8d08fdba 6901 int was_temp
d22c8596 6902 = (TREE_STATIC (decl) && TYPE_NEEDS_DESTRUCTOR (type)
8d08fdba
MS
6903 && allocation_temporary_p ());
6904
6905 if (was_temp)
6906 end_temporary_allocation ();
6907
42976354
BK
6908 /* Extern inline function static data has external linkage. */
6909 if (TREE_CODE (decl) == VAR_DECL
6910 && TREE_STATIC (decl)
6911 && current_function_decl
6912 && DECL_CONTEXT (decl) == current_function_decl
6913 && DECL_THIS_INLINE (current_function_decl)
893de33c 6914 && TREE_PUBLIC (current_function_decl))
42976354 6915 {
818045b6
JM
6916 if (DECL_INTERFACE_KNOWN (current_function_decl))
6917 {
6918 TREE_PUBLIC (decl) = 1;
6919 DECL_EXTERNAL (decl) = DECL_EXTERNAL (current_function_decl);
6920 }
42976354
BK
6921 /* We can only do this if we can use common or weak, and we
6922 can't if it has been initialized and we don't support weak. */
818045b6
JM
6923 else if (DECL_INITIAL (decl) == NULL_TREE
6924 || DECL_INITIAL (decl) == error_mark_node)
42976354
BK
6925 {
6926 TREE_PUBLIC (decl) = 1;
6927 DECL_COMMON (decl) = 1;
6928 }
6929 else if (flag_weak)
6930 make_decl_one_only (decl);
6931
6932 if (TREE_PUBLIC (decl))
6933 DECL_ASSEMBLER_NAME (decl)
6934 = build_static_name (current_function_decl, DECL_NAME (decl));
6935 else if (! DECL_ARTIFICIAL (decl))
6936 cp_warning_at ("sorry: semantics of inline function static data `%#D' are wrong (you'll wind up with multiple copies)", decl);
6937 }
6938
77be6f82
JM
6939 else if (TREE_CODE (decl) == VAR_DECL
6940 && DECL_LANG_SPECIFIC (decl)
6941 && DECL_COMDAT (decl))
6942 {
6943 /* Dynamically initialized vars go into common. */
6944 if (DECL_INITIAL (decl) == NULL_TREE
6945 || DECL_INITIAL (decl) == error_mark_node)
6946 DECL_COMMON (decl) = 1;
6947 else if (EMPTY_CONSTRUCTOR_P (DECL_INITIAL (decl)))
6948 {
6949 DECL_COMMON (decl) = 1;
6950 DECL_INITIAL (decl) = error_mark_node;
6951 }
6952 else
6953 {
6954 /* Statically initialized vars are weak or comdat, if
6955 supported. */
6956 if (flag_weak)
6957 make_decl_one_only (decl);
6958 else
be343556 6959 {
2c73f9f5 6960 /* We can't do anything useful; leave vars for explicit
be343556
JM
6961 instantiation. */
6962 DECL_EXTERNAL (decl) = 1;
6963 DECL_NOT_REALLY_EXTERN (decl) = 0;
6964 }
77be6f82
JM
6965 }
6966 }
6967
8d08fdba
MS
6968 if (TREE_CODE (decl) == VAR_DECL && DECL_VIRTUAL_P (decl))
6969 make_decl_rtl (decl, NULL_PTR, toplev);
6970 else if (TREE_CODE (decl) == VAR_DECL
6971 && TREE_READONLY (decl)
6972 && DECL_INITIAL (decl) != NULL_TREE
6973 && DECL_INITIAL (decl) != error_mark_node
a3203465 6974 && ! EMPTY_CONSTRUCTOR_P (DECL_INITIAL (decl)))
8d08fdba
MS
6975 {
6976 DECL_INITIAL (decl) = save_expr (DECL_INITIAL (decl));
6977
6978 if (asmspec)
6979 DECL_ASSEMBLER_NAME (decl) = get_identifier (asmspec);
6980
6981 if (! toplev
6982 && TREE_STATIC (decl)
6983 && ! TREE_SIDE_EFFECTS (decl)
6984 && ! TREE_PUBLIC (decl)
6985 && ! DECL_EXTERNAL (decl)
6986 && ! TYPE_NEEDS_DESTRUCTOR (type)
6987 && DECL_MODE (decl) != BLKmode)
6988 {
6989 /* If this variable is really a constant, then fill its DECL_RTL
6990 slot with something which won't take up storage.
6991 If something later should take its address, we can always give
6992 it legitimate RTL at that time. */
6993 DECL_RTL (decl) = gen_reg_rtx (DECL_MODE (decl));
6994 store_expr (DECL_INITIAL (decl), DECL_RTL (decl), 0);
6995 TREE_ASM_WRITTEN (decl) = 1;
6996 }
a0a33927 6997 else if (toplev && ! TREE_PUBLIC (decl))
8d08fdba 6998 {
8d08fdba 6999 /* If this is a static const, change its apparent linkage
db5ae43f 7000 if it belongs to a #pragma interface. */
a0a33927 7001 if (!interface_unknown)
8d08fdba
MS
7002 {
7003 TREE_PUBLIC (decl) = 1;
7004 DECL_EXTERNAL (decl) = interface_only;
7005 }
7006 make_decl_rtl (decl, asmspec, toplev);
7007 }
7008 else
5566b478 7009 rest_of_decl_compilation (decl, asmspec, toplev, at_eof);
8d08fdba
MS
7010 }
7011 else if (TREE_CODE (decl) == VAR_DECL
7012 && DECL_LANG_SPECIFIC (decl)
7013 && DECL_IN_AGGR_P (decl))
7014 {
7015 if (TREE_STATIC (decl))
7016 {
7017 if (init == NULL_TREE
7018#ifdef DEFAULT_STATIC_DEFS
7019 /* If this code is dead, then users must
7020 explicitly declare static member variables
7021 outside the class def'n as well. */
7022 && TYPE_NEEDS_CONSTRUCTING (type)
7023#endif
7024 )
7025 {
7026 DECL_EXTERNAL (decl) = 1;
7027 make_decl_rtl (decl, asmspec, 1);
7028 }
7029 else
5566b478 7030 rest_of_decl_compilation (decl, asmspec, toplev, at_eof);
8d08fdba
MS
7031 }
7032 else
7033 /* Just a constant field. Should not need any rtl. */
7034 goto finish_end0;
7035 }
7036 else
5566b478 7037 rest_of_decl_compilation (decl, asmspec, toplev, at_eof);
8d08fdba
MS
7038
7039 if (was_temp)
7040 resume_temporary_allocation ();
7041
7042 if (type != error_mark_node
7043 && TYPE_LANG_SPECIFIC (type)
7044 && CLASSTYPE_ABSTRACT_VIRTUALS (type))
7045 abstract_virtuals_error (decl, type);
7046 else if ((TREE_CODE (type) == FUNCTION_TYPE
7047 || TREE_CODE (type) == METHOD_TYPE)
7048 && TYPE_LANG_SPECIFIC (TREE_TYPE (type))
7049 && CLASSTYPE_ABSTRACT_VIRTUALS (TREE_TYPE (type)))
7050 abstract_virtuals_error (decl, TREE_TYPE (type));
7051
7052 if (TYPE_LANG_SPECIFIC (type) && IS_SIGNATURE (type))
7053 signature_error (decl, type);
7054 else if ((TREE_CODE (type) == FUNCTION_TYPE
7055 || TREE_CODE (type) == METHOD_TYPE)
7056 && TYPE_LANG_SPECIFIC (TREE_TYPE (type))
7057 && IS_SIGNATURE (TREE_TYPE (type)))
7058 signature_error (decl, TREE_TYPE (type));
7059
7060 if (TREE_CODE (decl) == FUNCTION_DECL)
faae18ab 7061 ;
67d743fe
MS
7062 else if (DECL_EXTERNAL (decl)
7063 && ! (DECL_LANG_SPECIFIC (decl)
7064 && DECL_NOT_REALLY_EXTERN (decl)))
5566b478
MS
7065 {
7066 if (init)
7067 DECL_INITIAL (decl) = init;
7068 }
8d08fdba
MS
7069 else if (TREE_STATIC (decl) && type != error_mark_node)
7070 {
7071 /* Cleanups for static variables are handled by `finish_file'. */
f30432d7
MS
7072 if (TYPE_NEEDS_CONSTRUCTING (type) || init != NULL_TREE
7073 || TYPE_NEEDS_DESTRUCTOR (type))
8d08fdba 7074 expand_static_init (decl, init);
8d08fdba
MS
7075 }
7076 else if (! toplev)
7077 {
7078 /* This is a declared decl which must live until the
7079 end of the binding contour. It may need a cleanup. */
7080
7081 /* Recompute the RTL of a local array now
7082 if it used to be an incomplete type. */
7083 if (was_incomplete && ! TREE_STATIC (decl))
7084 {
7085 /* If we used it already as memory, it must stay in memory. */
7086 TREE_ADDRESSABLE (decl) = TREE_USED (decl);
7087 /* If it's still incomplete now, no init will save it. */
7088 if (DECL_SIZE (decl) == NULL_TREE)
7089 DECL_INITIAL (decl) = NULL_TREE;
7090 expand_decl (decl);
7091 }
7092 else if (! TREE_ASM_WRITTEN (decl)
7093 && (TYPE_SIZE (type) != NULL_TREE
7094 || TREE_CODE (type) == ARRAY_TYPE))
7095 {
7096 /* Do this here, because we did not expand this decl's
7097 rtl in start_decl. */
7098 if (DECL_RTL (decl) == NULL_RTX)
7099 expand_decl (decl);
7100 else if (cleanup)
7101 {
8d2733ca
MS
7102 /* XXX: Why don't we use decl here? */
7103 /* Ans: Because it was already expanded? */
e349ee73 7104 if (! expand_decl_cleanup (NULL_TREE, cleanup))
8d2733ca
MS
7105 cp_error ("parser lost in parsing declaration of `%D'",
7106 decl);
8d08fdba
MS
7107 /* Cleanup used up here. */
7108 cleanup = NULL_TREE;
7109 }
7110 }
7111
2ee887f2
MS
7112 if (current_binding_level->is_for_scope)
7113 {
7114 struct binding_level *outer = current_binding_level->level_chain;
7115
7116 /* Check to see if the same name is already bound at
7117 the outer level, either because it was directly declared,
7118 or because a dead for-decl got preserved. In either case,
d22c8596 7119 the code would not have been valid under the ARM
2ee887f2
MS
7120 scope rules, so clear is_for_scope for the
7121 current_binding_level.
7122
7123 Otherwise, we need to preserve the temp slot for decl
e92cc029 7124 to last into the outer binding level. */
2ee887f2
MS
7125
7126 int handling_dead_for_vars = 0;
7127 tree link = outer->names;
7128 for (; ; link = TREE_CHAIN (link))
7129 {
7130 if (link == NULL && handling_dead_for_vars == 0)
7131 {
7132 link = outer->dead_vars_from_for;
7133 handling_dead_for_vars = 1;
7134 }
7135 if (link == NULL)
7136 {
cffa8729 7137 if (DECL_IN_MEMORY_P (decl))
2ee887f2
MS
7138 preserve_temp_slots (DECL_RTL (decl));
7139 break;
7140 }
7141 if (DECL_NAME (link) == DECL_NAME (decl))
7142 {
7143 if (handling_dead_for_vars)
7144 {
7145 tree shadowing
7146 = purpose_member (DECL_NAME (decl),
7147 current_binding_level->shadowed);
7148 if (shadowing && TREE_VALUE (shadowing) == link)
7149 TREE_VALUE (shadowing)
7150 = DECL_SHADOWED_FOR_VAR (link);
7151 }
7152 current_binding_level->is_for_scope = 0;
7153 break;
7154 }
7155 }
7156 }
7157
eb66be0e 7158 expand_start_target_temps ();
72b7eeff 7159
8d08fdba
MS
7160 if (DECL_SIZE (decl) && type != error_mark_node)
7161 {
7162 /* Compute and store the initial value. */
7163 expand_decl_init (decl);
0c4b14c4 7164 already_used = TREE_USED (decl) || TREE_USED (type);
8d08fdba
MS
7165
7166 if (init || TYPE_NEEDS_CONSTRUCTING (type))
7167 {
a28e3c7f
MS
7168 emit_line_note (DECL_SOURCE_FILE (decl),
7169 DECL_SOURCE_LINE (decl));
6060a796 7170 expand_aggr_init (decl, init, 0, flags);
8d08fdba
MS
7171 }
7172
00595019
MS
7173 /* Set this to 0 so we can tell whether an aggregate which
7174 was initialized was ever used. Don't do this if it has a
7175 destructor, so we don't complain about the 'resource
7176 allocation is initialization' idiom. */
249555b0 7177 /* Now set attribute((unused)) on types so decls of
38e01259 7178 that type will be marked used. (see TREE_USED, above.)
249555b0
BK
7179 This avoids the warning problems this particular code
7180 tried to work around. */
0c4b14c4 7181
be99da77 7182 if (TYPE_NEEDS_CONSTRUCTING (type)
0c4b14c4 7183 && ! already_used
be99da77
MS
7184 && cleanup == NULL_TREE
7185 && DECL_NAME (decl))
8d08fdba 7186 TREE_USED (decl) = 0;
0c4b14c4
JM
7187
7188 if (already_used)
7189 TREE_USED (decl) = 1;
934c6b13 7190 }
eb66be0e 7191
934c6b13 7192 /* Cleanup any temporaries needed for the initial value. */
eb66be0e 7193 expand_end_target_temps ();
8d08fdba 7194
934c6b13
MS
7195 if (DECL_SIZE (decl) && type != error_mark_node)
7196 {
8d08fdba
MS
7197 /* Store the cleanup, if there was one. */
7198 if (cleanup)
7199 {
e349ee73 7200 if (! expand_decl_cleanup (decl, cleanup))
a28e3c7f
MS
7201 cp_error ("parser lost in parsing declaration of `%D'",
7202 decl);
8d08fdba
MS
7203 }
7204 }
7205 }
7206 finish_end0:
7207
7208 /* Undo call to `pushclass' that was done in `start_decl'
7209 due to initialization of qualified member variable.
7210 I.e., Foo::x = 10; */
7211 {
f30432d7 7212 tree context = DECL_REAL_CONTEXT (decl);
8d08fdba
MS
7213 if (context
7214 && TREE_CODE_CLASS (TREE_CODE (context)) == 't'
7215 && (TREE_CODE (decl) == VAR_DECL
7216 /* We also have a pushclass done that we need to undo here
7217 if we're at top level and declare a method. */
5566b478
MS
7218 || TREE_CODE (decl) == FUNCTION_DECL)
7219 /* If size hasn't been set, we're still defining it,
7220 and therefore inside the class body; don't pop
7221 the binding level.. */
7222 && TYPE_SIZE (context) != NULL_TREE
7223 && context == current_class_type)
8d08fdba
MS
7224 popclass (1);
7225 }
7226 }
7227
7228 finish_end:
7229
39211cd5
MS
7230 /* If requested, warn about definitions of large data objects. */
7231
7232 if (warn_larger_than
5156628f 7233 && ! processing_template_decl
39211cd5
MS
7234 && (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL)
7235 && !DECL_EXTERNAL (decl))
7236 {
7237 register tree decl_size = DECL_SIZE (decl);
7238
7239 if (decl_size && TREE_CODE (decl_size) == INTEGER_CST)
7240 {
7241 unsigned units = TREE_INT_CST_LOW (decl_size) / BITS_PER_UNIT;
7242
7243 if (units > larger_than_size)
7244 warning_with_decl (decl, "size of `%s' is %u bytes", units);
7245 }
7246 }
7247
8d08fdba
MS
7248 if (need_pop)
7249 {
7250 /* Resume permanent allocation, if not within a function. */
7251 /* The corresponding push_obstacks_nochange is in start_decl,
7252 start_method, groktypename, and in grokfield. */
7253 pop_obstacks ();
7254 }
7255
7256 if (was_readonly)
7257 TREE_READONLY (decl) = 1;
8d08fdba
MS
7258}
7259
82580166 7260/* This is here for a midend callback from c-common.c */
e92cc029 7261
82580166
MS
7262void
7263finish_decl (decl, init, asmspec_tree)
7264 tree decl, init;
7265 tree asmspec_tree;
7266{
7267 cp_finish_decl (decl, init, asmspec_tree, 1, 0);
7268}
7269
8d08fdba
MS
7270void
7271expand_static_init (decl, init)
7272 tree decl;
7273 tree init;
7274{
7275 tree oldstatic = value_member (decl, static_aggregates);
a4443a08 7276
8d08fdba
MS
7277 if (oldstatic)
7278 {
7279 if (TREE_PURPOSE (oldstatic) && init != NULL_TREE)
7280 cp_error ("multiple initializations given for `%D'", decl);
7281 }
a9aedbc2 7282 else if (! toplevel_bindings_p () && ! pseudo_global_level_p ())
8d08fdba
MS
7283 {
7284 /* Emit code to perform this initialization but once. */
7285 tree temp;
7286
e92cc029 7287 /* Remember this information until end of file. */
8d08fdba
MS
7288 push_obstacks (&permanent_obstack, &permanent_obstack);
7289
7290 /* Emit code to perform this initialization but once. */
7291 temp = get_temp_name (integer_type_node, 1);
7292 rest_of_decl_compilation (temp, NULL_PTR, 0, 0);
7293 expand_start_cond (build_binary_op (EQ_EXPR, temp,
7294 integer_zero_node, 1), 0);
eb66be0e 7295 expand_start_target_temps ();
72b7eeff 7296
8d08fdba 7297 expand_assignment (temp, integer_one_node, 0, 0);
28cbf42c 7298 if (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl))
f30432d7 7299 || (init && TREE_CODE (init) == TREE_LIST))
8d08fdba 7300 {
6060a796 7301 expand_aggr_init (decl, init, 0, 0);
8d08fdba
MS
7302 do_pending_stack_adjust ();
7303 }
f30432d7 7304 else if (init)
8d08fdba 7305 expand_assignment (decl, init, 0, 0);
f30432d7 7306
a4443a08 7307 /* Cleanup any temporaries needed for the initial value. */
eb66be0e 7308 expand_end_target_temps ();
72b7eeff
MS
7309
7310 if (TYPE_NEEDS_DESTRUCTOR (TREE_TYPE (decl)))
7311 {
7312 tree cleanup, fcall;
7313 static tree Atexit = 0;
7314 if (Atexit == 0)
7315 {
7316 tree atexit_fndecl, PFV, pfvlist;
e92cc029 7317 /* Remember this information until end of file. */
72b7eeff
MS
7318 push_obstacks (&permanent_obstack, &permanent_obstack);
7319 PFV = build_pointer_type (build_function_type
7320 (void_type_node, void_list_node));
7321
7322 pfvlist = tree_cons (NULL_TREE, PFV, void_list_node);
7323
7324 push_lang_context (lang_name_c);
beb53fb8
JM
7325 atexit_fndecl
7326 = builtin_function ("atexit",
7327 build_function_type (void_type_node,
7328 pfvlist),
7329 NOT_BUILT_IN, NULL_PTR);
be99da77 7330 assemble_external (atexit_fndecl);
72b7eeff
MS
7331 Atexit = default_conversion (atexit_fndecl);
7332 pop_lang_context ();
7333 pop_obstacks ();
7334 }
7335
7336 cleanup = start_anon_func ();
7337 expand_expr_stmt (build_cleanup (decl));
7338 end_anon_func ();
7339 mark_addressable (cleanup);
7340 cleanup = build_unary_op (ADDR_EXPR, cleanup, 0);
e66d884e 7341 fcall = build_function_call (Atexit, expr_tree_cons (NULL_TREE, cleanup, NULL_TREE));
72b7eeff
MS
7342 expand_expr_stmt (fcall);
7343 }
7344
8d08fdba
MS
7345 expand_end_cond ();
7346 if (TYPE_NEEDS_DESTRUCTOR (TREE_TYPE (decl)))
7347 {
7348 static_aggregates = perm_tree_cons (temp, decl, static_aggregates);
7349 TREE_STATIC (static_aggregates) = 1;
7350 }
7351
e92cc029 7352 /* Resume old (possibly temporary) allocation. */
8d08fdba
MS
7353 pop_obstacks ();
7354 }
7355 else
7356 {
7357 /* This code takes into account memory allocation
7358 policy of `start_decl'. Namely, if TYPE_NEEDS_CONSTRUCTING
7359 does not hold for this object, then we must make permanent
7360 the storage currently in the temporary obstack. */
7361 if (! TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl)))
7362 preserve_initializer ();
7363 static_aggregates = perm_tree_cons (init, decl, static_aggregates);
7364 }
7365}
7366\f
7367/* Make TYPE a complete type based on INITIAL_VALUE.
7368 Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
7369 2 if there was no information (in which case assume 1 if DO_DEFAULT). */
7370
7371int
7372complete_array_type (type, initial_value, do_default)
7373 tree type, initial_value;
7374 int do_default;
7375{
7376 register tree maxindex = NULL_TREE;
7377 int value = 0;
7378
7379 if (initial_value)
7380 {
7381 /* Note MAXINDEX is really the maximum index,
7382 one less than the size. */
7383 if (TREE_CODE (initial_value) == STRING_CST)
e1cd6e56
MS
7384 {
7385 int eltsize
7386 = int_size_in_bytes (TREE_TYPE (TREE_TYPE (initial_value)));
7387 maxindex = build_int_2 ((TREE_STRING_LENGTH (initial_value)
7388 / eltsize) - 1, 0);
7389 }
8d08fdba
MS
7390 else if (TREE_CODE (initial_value) == CONSTRUCTOR)
7391 {
e1cd6e56
MS
7392 tree elts = CONSTRUCTOR_ELTS (initial_value);
7393 maxindex = size_binop (MINUS_EXPR, integer_zero_node, size_one_node);
7394 for (; elts; elts = TREE_CHAIN (elts))
7395 {
7396 if (TREE_PURPOSE (elts))
7397 maxindex = TREE_PURPOSE (elts);
7398 else
7399 maxindex = size_binop (PLUS_EXPR, maxindex, size_one_node);
7400 }
7401 maxindex = copy_node (maxindex);
8d08fdba
MS
7402 }
7403 else
7404 {
7405 /* Make an error message unless that happened already. */
7406 if (initial_value != error_mark_node)
7407 value = 1;
7408
7409 /* Prevent further error messages. */
7410 maxindex = build_int_2 (0, 0);
7411 }
7412 }
7413
7414 if (!maxindex)
7415 {
7416 if (do_default)
7417 maxindex = build_int_2 (0, 0);
7418 value = 2;
7419 }
7420
7421 if (maxindex)
7422 {
51c184be
MS
7423 tree itype;
7424
8d08fdba 7425 TYPE_DOMAIN (type) = build_index_type (maxindex);
dff6b454 7426 if (! TREE_TYPE (maxindex))
8d08fdba 7427 TREE_TYPE (maxindex) = TYPE_DOMAIN (type);
51c184be
MS
7428 if (initial_value)
7429 itype = TREE_TYPE (initial_value);
7430 else
7431 itype = NULL;
7432 if (itype && !TYPE_DOMAIN (itype))
7433 TYPE_DOMAIN (itype) = TYPE_DOMAIN (type);
dff6b454
RK
7434 /* The type of the main variant should never be used for arrays
7435 of different sizes. It should only ever be completed with the
7436 size of the array. */
7437 if (! TYPE_DOMAIN (TYPE_MAIN_VARIANT (type)))
7438 TYPE_DOMAIN (TYPE_MAIN_VARIANT (type)) = TYPE_DOMAIN (type);
8d08fdba
MS
7439 }
7440
7441 /* Lay out the type now that we can get the real answer. */
7442
7443 layout_type (type);
7444
7445 return value;
7446}
7447\f
7448/* Return zero if something is declared to be a member of type
7449 CTYPE when in the context of CUR_TYPE. STRING is the error
7450 message to print in that case. Otherwise, quietly return 1. */
e92cc029 7451
8d08fdba
MS
7452static int
7453member_function_or_else (ctype, cur_type, string)
7454 tree ctype, cur_type;
7455 char *string;
7456{
7457 if (ctype && ctype != cur_type)
7458 {
7459 error (string, TYPE_NAME_STRING (ctype));
7460 return 0;
7461 }
7462 return 1;
7463}
7464\f
7465/* Subroutine of `grokdeclarator'. */
7466
7467/* Generate errors possibly applicable for a given set of specifiers.
7468 This is for ARM $7.1.2. */
e92cc029 7469
8d08fdba
MS
7470static void
7471bad_specifiers (object, type, virtualp, quals, inlinep, friendp, raises)
7472 tree object;
7473 char *type;
7474 int virtualp, quals, friendp, raises, inlinep;
7475{
7476 if (virtualp)
7477 cp_error ("`%D' declared as a `virtual' %s", object, type);
7478 if (inlinep)
7479 cp_error ("`%D' declared as an `inline' %s", object, type);
7480 if (quals)
a28e3c7f
MS
7481 cp_error ("`const' and `volatile' function specifiers on `%D' invalid in %s declaration",
7482 object, type);
8d08fdba
MS
7483 if (friendp)
7484 cp_error_at ("invalid friend declaration", object);
7485 if (raises)
6060a796 7486 cp_error_at ("invalid exception specifications", object);
8d08fdba
MS
7487}
7488
7489/* CTYPE is class type, or null if non-class.
7490 TYPE is type this FUNCTION_DECL should have, either FUNCTION_TYPE
7491 or METHOD_TYPE.
7492 DECLARATOR is the function's name.
7493 VIRTUALP is truthvalue of whether the function is virtual or not.
7494 FLAGS are to be passed through to `grokclassfn'.
7495 QUALS are qualifiers indicating whether the function is `const'
7496 or `volatile'.
7497 RAISES is a list of exceptions that this function can raise.
7498 CHECK is 1 if we must find this method in CTYPE, 0 if we should
7499 not look, and -1 if we should not call `grokclassfn' at all. */
e92cc029 7500
8d08fdba 7501static tree
386b8a85
JM
7502grokfndecl (ctype, type, declarator, orig_declarator, virtualp, flags, quals,
7503 raises, attrlist, check, friendp, publicp, inlinep, funcdef_flag,
2c73f9f5 7504 template_count, in_namespace)
8d08fdba
MS
7505 tree ctype, type;
7506 tree declarator;
386b8a85 7507 tree orig_declarator;
8d08fdba
MS
7508 int virtualp;
7509 enum overload_flags flags;
f30432d7 7510 tree quals, raises, attrlist;
386b8a85 7511 int check, friendp, publicp, inlinep, funcdef_flag, template_count;
2c73f9f5 7512 tree in_namespace;
8d08fdba
MS
7513{
7514 tree cname, decl;
7515 int staticp = ctype && TREE_CODE (type) == FUNCTION_TYPE;
42976354 7516 tree t;
8d08fdba
MS
7517
7518 if (ctype)
7519 cname = TREE_CODE (TYPE_NAME (ctype)) == TYPE_DECL
7520 ? TYPE_IDENTIFIER (ctype) : TYPE_NAME (ctype);
7521 else
7522 cname = NULL_TREE;
7523
7524 if (raises)
7525 {
f30432d7 7526 type = build_exception_variant (type, raises);
8d08fdba 7527 }
c11b6f21 7528
8d08fdba 7529 decl = build_lang_decl (FUNCTION_DECL, declarator, type);
2c73f9f5 7530 /* Propagate volatile out from type to decl. */
8d08fdba 7531 if (TYPE_VOLATILE (type))
893de33c 7532 TREE_THIS_VOLATILE (decl) = 1;
8d08fdba 7533
2c73f9f5
ML
7534 /* This decl is not from the current namespace. */
7535 if (in_namespace)
7536 set_decl_namespace (decl, in_namespace);
7537
8d08fdba
MS
7538 /* Should probably propagate const out from type to decl I bet (mrs). */
7539 if (staticp)
7540 {
7541 DECL_STATIC_FUNCTION_P (decl) = 1;
7542 DECL_CONTEXT (decl) = ctype;
8d08fdba
MS
7543 }
7544
e76a2646
MS
7545 if (ctype)
7546 DECL_CLASS_CONTEXT (decl) = ctype;
7547
faae18ab
MS
7548 if (ctype == NULL_TREE && ! strcmp (IDENTIFIER_POINTER (declarator), "main"))
7549 {
7550 if (inlinep)
7551 error ("cannot declare `main' to be inline");
7552 else if (! publicp)
7553 error ("cannot declare `main' to be static");
7554 inlinep = 0;
7555 publicp = 1;
7556 }
7557
893de33c 7558 TREE_PUBLIC (decl) = publicp;
faae18ab 7559 if (! publicp)
893de33c
JM
7560 {
7561 DECL_INTERFACE_KNOWN (decl) = 1;
7562 DECL_NOT_REALLY_EXTERN (decl) = 1;
7563 }
faae18ab
MS
7564
7565 if (inlinep)
7566 DECL_THIS_INLINE (decl) = DECL_INLINE (decl) = 1;
8d08fdba
MS
7567
7568 DECL_EXTERNAL (decl) = 1;
7569 if (quals != NULL_TREE && TREE_CODE (type) == FUNCTION_TYPE)
7570 {
7571 cp_error ("%smember function `%D' cannot have `%T' method qualifier",
7572 (ctype ? "static " : "non-"), decl, TREE_VALUE (quals));
7573 quals = NULL_TREE;
7574 }
7575
7576 if (IDENTIFIER_OPNAME_P (DECL_NAME (decl)))
7577 grok_op_properties (decl, virtualp, check < 0);
7578
e76a2646 7579 if (ctype && hack_decl_function_context (decl))
893de33c 7580 DECL_NO_STATIC_CHAIN (decl) = 1;
e76a2646 7581
42976354
BK
7582 for (t = TYPE_ARG_TYPES (TREE_TYPE (decl)); t; t = TREE_CHAIN (t))
7583 if (TREE_PURPOSE (t)
7584 && TREE_CODE (TREE_PURPOSE (t)) == DEFAULT_ARG)
7585 {
7586 add_defarg_fn (decl);
7587 break;
7588 }
7589
75650646
MM
7590 if (friendp &&
7591 TREE_CODE (orig_declarator) == TEMPLATE_ID_EXPR)
f84b4be9
JM
7592 {
7593 /* A friend declaration of the form friend void f<>(). Record
7594 the information in the TEMPLATE_ID_EXPR. */
7595 SET_DECL_IMPLICIT_INSTANTIATION (decl);
7596 DECL_TEMPLATE_INFO (decl)
7597 = perm_tree_cons (TREE_OPERAND (orig_declarator, 0),
7598 TREE_OPERAND (orig_declarator, 1),
7599 NULL_TREE);
7600 }
386b8a85 7601
75650646 7602 /* Caller will do the rest of this. */
8d08fdba
MS
7603 if (check < 0)
7604 return decl;
7605
5566b478 7606 if (check && funcdef_flag)
d2e5ee5c 7607 DECL_INITIAL (decl) = error_mark_node;
5566b478 7608
8d08fdba
MS
7609 if (flags == NO_SPECIAL && ctype && constructor_name (cname) == declarator)
7610 {
7611 tree tmp;
7612 /* Just handle constructors here. We could do this
7613 inside the following if stmt, but I think
7614 that the code is more legible by breaking this
7615 case out. See comments below for what each of
7616 the following calls is supposed to do. */
7617 DECL_CONSTRUCTOR_P (decl) = 1;
7618
7619 grokclassfn (ctype, declarator, decl, flags, quals);
386b8a85 7620
e1467ff2
MM
7621 decl = check_explicit_specialization (orig_declarator, decl,
7622 template_count,
f84b4be9
JM
7623 2 * (funcdef_flag != 0) +
7624 4 * (friendp != 0));
75650646 7625
8d08fdba 7626 if (check)
5566b478
MS
7627 {
7628 tmp = check_classfn (ctype, decl);
98c1c668
JM
7629
7630 if (tmp && TREE_CODE (tmp) == TEMPLATE_DECL)
7631 tmp = DECL_TEMPLATE_RESULT(tmp);
7632
e349ee73
MS
7633 if (tmp && DECL_ARTIFICIAL (tmp))
7634 cp_error ("definition of implicitly-declared `%D'", tmp);
5566b478
MS
7635 if (tmp && duplicate_decls (decl, tmp))
7636 return tmp;
7637 }
a0a33927
MS
7638 if (! grok_ctor_properties (ctype, decl))
7639 return NULL_TREE;
7640
7177d104 7641 if (check == 0 && ! current_function_decl)
8d08fdba 7642 {
2c73f9f5 7643 /* Assembler names live in the global namespace. */
37c46b43 7644 tmp = IDENTIFIER_GLOBAL_VALUE (DECL_ASSEMBLER_NAME (decl));
8d08fdba 7645 if (tmp == NULL_TREE)
2c73f9f5 7646 SET_IDENTIFIER_GLOBAL_VALUE (DECL_ASSEMBLER_NAME (decl), decl);
8d08fdba
MS
7647 else if (TREE_CODE (tmp) != TREE_CODE (decl))
7648 cp_error ("inconsistent declarations for `%D'", decl);
7649 else
7650 {
7651 duplicate_decls (decl, tmp);
7652 decl = tmp;
8d08fdba
MS
7653 }
7654 make_decl_rtl (decl, NULL_PTR, 1);
7655 }
7656 }
7657 else
7658 {
7659 tree tmp;
7660
7661 /* Function gets the ugly name, field gets the nice one.
7662 This call may change the type of the function (because
7663 of default parameters)! */
7664 if (ctype != NULL_TREE)
7665 grokclassfn (ctype, cname, decl, flags, quals);
7666
e1467ff2
MM
7667 decl = check_explicit_specialization (orig_declarator, decl,
7668 template_count,
f84b4be9
JM
7669 2 * (funcdef_flag != 0) +
7670 4 * (friendp != 0));
75650646 7671
8d08fdba 7672 if (ctype != NULL_TREE && check)
5566b478
MS
7673 {
7674 tmp = check_classfn (ctype, decl);
98c1c668
JM
7675
7676 if (tmp && TREE_CODE (tmp) == TEMPLATE_DECL)
75650646 7677 tmp = DECL_TEMPLATE_RESULT (tmp);
98c1c668 7678
5566b478
MS
7679 if (tmp && DECL_STATIC_FUNCTION_P (tmp)
7680 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE)
7681 {
7682 /* Remove the `this' parm added by grokclassfn.
7683 XXX Isn't this done in start_function, too? */
7684 revert_static_member_fn (&decl, NULL, NULL);
7685 last_function_parms = TREE_CHAIN (last_function_parms);
7686 }
e349ee73
MS
7687 if (tmp && DECL_ARTIFICIAL (tmp))
7688 cp_error ("definition of implicitly-declared `%D'", tmp);
7834ab39
MS
7689 if (tmp)
7690 {
7691 if (!duplicate_decls (decl, tmp))
7692 my_friendly_abort (892);
7693 return tmp;
7694 }
5566b478 7695 }
8d08fdba
MS
7696
7697 if (ctype == NULL_TREE || check)
7698 return decl;
7699
7177d104
MS
7700 /* Now install the declaration of this function so that others may
7701 find it (esp. its DECL_FRIENDLIST). Don't do this for local class
7702 methods, though. */
7703 if (! current_function_decl)
8d08fdba 7704 {
75650646 7705 if (!DECL_TEMPLATE_SPECIALIZATION (decl))
7177d104 7706 {
75650646
MM
7707 /* We don't do this for specializations since the
7708 equivalent checks will be done later. Also, at this
7709 point the DECL_ASSEMBLER_NAME is not yet fully
7710 accurate. */
7711
7712 /* FIXME: this should only need to look at
7713 IDENTIFIER_GLOBAL_VALUE. */
7714 tmp = lookup_name (DECL_ASSEMBLER_NAME (decl), 0);
7715 if (tmp == NULL_TREE)
2c73f9f5 7716 SET_IDENTIFIER_GLOBAL_VALUE (DECL_ASSEMBLER_NAME (decl), decl);
75650646
MM
7717 else if (TREE_CODE (tmp) != TREE_CODE (decl))
7718 cp_error ("inconsistent declarations for `%D'", decl);
7719 else
7720 {
7721 duplicate_decls (decl, tmp);
7722 decl = tmp;
75650646 7723 }
7177d104 7724 }
f30432d7
MS
7725
7726 if (attrlist)
7727 cplus_decl_attributes (decl, TREE_PURPOSE (attrlist),
7728 TREE_VALUE (attrlist));
7177d104 7729 make_decl_rtl (decl, NULL_PTR, 1);
8d08fdba 7730 }
8d08fdba
MS
7731 if (virtualp)
7732 {
2ee887f2 7733 DECL_VIRTUAL_P (decl) = 1;
8d08fdba
MS
7734 if (DECL_VINDEX (decl) == NULL_TREE)
7735 DECL_VINDEX (decl) = error_mark_node;
7736 IDENTIFIER_VIRTUAL_P (DECL_NAME (decl)) = 1;
8d08fdba
MS
7737 }
7738 }
7739 return decl;
7740}
7741
7742static tree
2c73f9f5 7743grokvardecl (type, declarator, specbits_in, initialized, constp, in_namespace)
8d08fdba
MS
7744 tree type;
7745 tree declarator;
d2e5ee5c 7746 RID_BIT_TYPE *specbits_in;
8d08fdba 7747 int initialized;
a9aedbc2 7748 int constp;
2c73f9f5 7749 tree in_namespace;
8d08fdba
MS
7750{
7751 tree decl;
f7da6097
MS
7752 RID_BIT_TYPE specbits;
7753
7754 specbits = *specbits_in;
8d08fdba
MS
7755
7756 if (TREE_CODE (type) == OFFSET_TYPE)
7757 {
7758 /* If you declare a static member so that it
7759 can be initialized, the code will reach here. */
5b605f68
MS
7760 tree basetype = TYPE_OFFSET_BASETYPE (type);
7761 type = TREE_TYPE (type);
7762 decl = build_lang_field_decl (VAR_DECL, declarator, type);
7763 DECL_CONTEXT (decl) = basetype;
7764 DECL_CLASS_CONTEXT (decl) = basetype;
f376e137 7765 DECL_ASSEMBLER_NAME (decl) = build_static_name (basetype, declarator);
8d08fdba
MS
7766 }
7767 else
30394414 7768 {
2c73f9f5 7769 tree context = in_namespace ? in_namespace : current_namespace;
30394414 7770 decl = build_decl (VAR_DECL, declarator, complete_type (type));
2c73f9f5
ML
7771 if (context != global_namespace)
7772 DECL_ASSEMBLER_NAME (decl) = build_static_name (context,
30394414
JM
7773 declarator);
7774 }
6060a796 7775
2c73f9f5
ML
7776 if (in_namespace)
7777 set_decl_namespace (decl, in_namespace);
7778
8d08fdba
MS
7779 if (RIDBIT_SETP (RID_EXTERN, specbits))
7780 {
7781 DECL_THIS_EXTERN (decl) = 1;
7782 DECL_EXTERNAL (decl) = !initialized;
7783 }
7784
7785 /* In class context, static means one per class,
7786 public access, and static storage. */
7787 if (DECL_FIELD_CONTEXT (decl) != NULL_TREE
7788 && IS_AGGR_TYPE (DECL_FIELD_CONTEXT (decl)))
7789 {
7790 TREE_PUBLIC (decl) = 1;
7791 TREE_STATIC (decl) = 1;
5b605f68 7792 DECL_EXTERNAL (decl) = 0;
8d08fdba
MS
7793 }
7794 /* At top level, either `static' or no s.c. makes a definition
7795 (perhaps tentative), and absence of `static' makes it public. */
a9aedbc2 7796 else if (toplevel_bindings_p ())
8d08fdba 7797 {
a9aedbc2 7798 TREE_PUBLIC (decl) = (RIDBIT_NOTSETP (RID_STATIC, specbits)
9fffd093 7799 && (DECL_THIS_EXTERN (decl) || ! constp));
8d08fdba
MS
7800 TREE_STATIC (decl) = ! DECL_EXTERNAL (decl);
7801 }
7802 /* Not at top level, only `static' makes a static definition. */
7803 else
7804 {
7805 TREE_STATIC (decl) = !! RIDBIT_SETP (RID_STATIC, specbits);
7806 TREE_PUBLIC (decl) = DECL_EXTERNAL (decl);
7807 }
7808 return decl;
7809}
7810
e92cc029 7811/* Create a canonical pointer to member function type. */
8d08fdba
MS
7812
7813tree
7814build_ptrmemfunc_type (type)
7815 tree type;
7816{
7817 tree fields[4];
7818 tree t;
7819 tree u;
7820
7821 /* If a canonical type already exists for this type, use it. We use
7822 this method instead of type_hash_canon, because it only does a
7823 simple equality check on the list of field members. */
7824
7825 if ((t = TYPE_GET_PTRMEMFUNC_TYPE (type)))
7826 return t;
7827
7828 push_obstacks (TYPE_OBSTACK (type), TYPE_OBSTACK (type));
7829
7830 u = make_lang_type (UNION_TYPE);
f30432d7 7831 IS_AGGR_TYPE (u) = 0;
8d08fdba 7832 fields[0] = build_lang_field_decl (FIELD_DECL, pfn_identifier, type);
a28e3c7f
MS
7833 fields[1] = build_lang_field_decl (FIELD_DECL, delta2_identifier,
7834 delta_type_node);
8d08fdba
MS
7835 finish_builtin_type (u, "__ptrmemfunc_type", fields, 1, ptr_type_node);
7836 TYPE_NAME (u) = NULL_TREE;
7837
7838 t = make_lang_type (RECORD_TYPE);
7839
2c73f9f5 7840 /* Let the front-end know this is a pointer to member function... */
db5ae43f 7841 TYPE_PTRMEMFUNC_FLAG (t) = 1;
2c73f9f5 7842 /* ... and not really an aggregate. */
f376e137 7843 IS_AGGR_TYPE (t) = 0;
8d08fdba 7844
a28e3c7f
MS
7845 fields[0] = build_lang_field_decl (FIELD_DECL, delta_identifier,
7846 delta_type_node);
7847 fields[1] = build_lang_field_decl (FIELD_DECL, index_identifier,
7848 delta_type_node);
8d08fdba
MS
7849 fields[2] = build_lang_field_decl (FIELD_DECL, pfn_or_delta2_identifier, u);
7850 finish_builtin_type (t, "__ptrmemfunc_type", fields, 2, ptr_type_node);
7851
7852 pop_obstacks ();
7853
7854 /* Zap out the name so that the back-end will give us the debugging
7855 information for this anonymous RECORD_TYPE. */
7856 TYPE_NAME (t) = NULL_TREE;
7857
7858 TYPE_SET_PTRMEMFUNC_TYPE (type, t);
7859
e92cc029 7860 /* Seems to be wanted. */
8d08fdba
MS
7861 CLASSTYPE_GOT_SEMICOLON (t) = 1;
7862 return t;
7863}
7864
7865/* Given declspecs and a declarator,
7866 determine the name and type of the object declared
7867 and construct a ..._DECL node for it.
7868 (In one case we can return a ..._TYPE node instead.
7869 For invalid input we sometimes return 0.)
7870
7871 DECLSPECS is a chain of tree_list nodes whose value fields
7872 are the storage classes and type specifiers.
7873
7874 DECL_CONTEXT says which syntactic context this declaration is in:
7875 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
7876 FUNCDEF for a function definition. Like NORMAL but a few different
7877 error messages in each case. Return value may be zero meaning
7878 this definition is too screwy to try to parse.
7879 MEMFUNCDEF for a function definition. Like FUNCDEF but prepares to
7880 handle member functions (which have FIELD context).
7881 Return value may be zero meaning this definition is too screwy to
7882 try to parse.
7883 PARM for a parameter declaration (either within a function prototype
7884 or before a function body). Make a PARM_DECL, or return void_type_node.
db5ae43f 7885 CATCHPARM for a parameter declaration before a catch clause.
8d08fdba
MS
7886 TYPENAME if for a typename (in a cast or sizeof).
7887 Don't make a DECL node; just return the ..._TYPE node.
7888 FIELD for a struct or union field; make a FIELD_DECL.
7889 BITFIELD for a field with specified width.
7890 INITIALIZED is 1 if the decl has an initializer.
7891
7892 In the TYPENAME case, DECLARATOR is really an absolute declarator.
7893 It may also be so in the PARM case, for a prototype where the
7894 argument type is specified but not the name.
7895
7896 This function is where the complicated C meanings of `static'
7897 and `extern' are interpreted.
7898
7899 For C++, if there is any monkey business to do, the function which
7900 calls this one must do it, i.e., prepending instance variables,
7901 renaming overloaded function names, etc.
7902
7903 Note that for this C++, it is an error to define a method within a class
7904 which does not belong to that class.
7905
7906 Except in the case where SCOPE_REFs are implicitly known (such as
7907 methods within a class being redundantly qualified),
7908 declarations which involve SCOPE_REFs are returned as SCOPE_REFs
7909 (class_name::decl_name). The caller must also deal with this.
7910
7911 If a constructor or destructor is seen, and the context is FIELD,
7912 then the type gains the attribute TREE_HAS_x. If such a declaration
7913 is erroneous, NULL_TREE is returned.
7914
7915 QUALS is used only for FUNCDEF and MEMFUNCDEF cases. For a member
7916 function, these are the qualifiers to give to the `this' pointer.
7917
7918 May return void_type_node if the declarator turned out to be a friend.
7919 See grokfield for details. */
7920
7921enum return_types { return_normal, return_ctor, return_dtor, return_conversion };
7922
7923tree
c11b6f21 7924grokdeclarator (declarator, declspecs, decl_context, initialized, attrlist)
8d08fdba
MS
7925 tree declspecs;
7926 tree declarator;
7927 enum decl_context decl_context;
7928 int initialized;
c11b6f21 7929 tree attrlist;
8d08fdba
MS
7930{
7931 RID_BIT_TYPE specbits;
7932 int nclasses = 0;
7933 tree spec;
7934 tree type = NULL_TREE;
7935 int longlong = 0;
7936 int constp;
7937 int volatilep;
db5ae43f 7938 int virtualp, explicitp, friendp, inlinep, staticp;
8d08fdba
MS
7939 int explicit_int = 0;
7940 int explicit_char = 0;
37c46b43 7941 int defaulted_int = 0;
8d08fdba
MS
7942 int opaque_typedef = 0;
7943 tree typedef_decl = NULL_TREE;
7944 char *name;
7945 tree typedef_type = NULL_TREE;
7946 int funcdef_flag = 0;
7947 enum tree_code innermost_code = ERROR_MARK;
7948 int bitfield = 0;
6125f3be
DE
7949#if 0
7950 /* See the code below that used this. */
f6abb50a 7951 tree decl_machine_attr = NULL_TREE;
6125f3be 7952#endif
8d08fdba
MS
7953 /* Set this to error_mark_node for FIELD_DECLs we could not handle properly.
7954 All FIELD_DECLs we build here have `init' put into their DECL_INITIAL. */
7955 tree init = NULL_TREE;
7956
7957 /* Keep track of what sort of function is being processed
7958 so that we can warn about default return values, or explicit
7959 return values which do not match prescribed defaults. */
7960 enum return_types return_type = return_normal;
7961
7962 tree dname = NULL_TREE;
7963 tree ctype = current_class_type;
7964 tree ctor_return_type = NULL_TREE;
7965 enum overload_flags flags = NO_SPECIAL;
8d08fdba 7966 tree quals = NULL_TREE;
c11b6f21 7967 tree raises = NULL_TREE;
386b8a85 7968 int template_count = 0;
2c73f9f5 7969 tree in_namespace = NULL_TREE;
8d08fdba
MS
7970
7971 RIDBIT_RESET_ALL (specbits);
7972 if (decl_context == FUNCDEF)
7973 funcdef_flag = 1, decl_context = NORMAL;
7974 else if (decl_context == MEMFUNCDEF)
7975 funcdef_flag = -1, decl_context = FIELD;
7976 else if (decl_context == BITFIELD)
7977 bitfield = 1, decl_context = FIELD;
7978
8d08fdba
MS
7979 /* Look inside a declarator for the name being declared
7980 and get it as a string, for an error message. */
7981 {
be99da77
MS
7982 tree *next = &declarator;
7983 register tree decl;
8d08fdba
MS
7984 name = NULL;
7985
be99da77
MS
7986 while (next && *next)
7987 {
7988 decl = *next;
7989 switch (TREE_CODE (decl))
8d08fdba 7990 {
be99da77
MS
7991 case COND_EXPR:
7992 ctype = NULL_TREE;
7993 next = &TREE_OPERAND (decl, 0);
7994 break;
8d08fdba 7995
2c73f9f5 7996 case BIT_NOT_EXPR: /* For C++ destructors! */
8d08fdba 7997 {
be99da77
MS
7998 tree name = TREE_OPERAND (decl, 0);
7999 tree rename = NULL_TREE;
8000
8001 my_friendly_assert (flags == NO_SPECIAL, 152);
8002 flags = DTOR_FLAG;
8003 return_type = return_dtor;
5566b478
MS
8004 if (TREE_CODE (name) == TYPE_DECL)
8005 TREE_OPERAND (decl, 0) = name = constructor_name (name);
be99da77
MS
8006 my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, 153);
8007 if (ctype == NULL_TREE)
8008 {
8009 if (current_class_type == NULL_TREE)
8010 {
8011 error ("destructors must be member functions");
8012 flags = NO_SPECIAL;
8013 }
8014 else
8015 {
8016 tree t = constructor_name (current_class_name);
8017 if (t != name)
8018 rename = t;
8019 }
8020 }
8d08fdba 8021 else
be99da77
MS
8022 {
8023 tree t = constructor_name (ctype);
8024 if (t != name)
8025 rename = t;
8026 }
51c184be 8027
be99da77 8028 if (rename)
39211cd5 8029 {
5566b478
MS
8030 cp_error ("destructor `%T' must match class name `%T'",
8031 name, rename);
be99da77 8032 TREE_OPERAND (decl, 0) = rename;
39211cd5 8033 }
be99da77 8034 next = &name;
51c184be 8035 }
be99da77 8036 break;
8d08fdba 8037
be99da77 8038 case ADDR_EXPR: /* C++ reference declaration */
2c73f9f5 8039 /* Fall through. */
be99da77
MS
8040 case ARRAY_REF:
8041 case INDIRECT_REF:
8042 ctype = NULL_TREE;
8043 innermost_code = TREE_CODE (decl);
8044 next = &TREE_OPERAND (decl, 0);
8045 break;
8d08fdba 8046
be99da77
MS
8047 case CALL_EXPR:
8048 if (parmlist_is_exprlist (TREE_OPERAND (decl, 1)))
8d08fdba 8049 {
be99da77
MS
8050 /* This is actually a variable declaration using constructor
8051 syntax. We need to call start_decl and cp_finish_decl so we
8052 can get the variable initialized... */
8053
8054 *next = TREE_OPERAND (decl, 0);
8055 init = TREE_OPERAND (decl, 1);
8056
a1774733 8057 decl = start_decl (declarator, declspecs, 1, NULL_TREE, NULL_TREE);
249555b0
BK
8058 /* Look for __unused__ attribute */
8059 if (TREE_USED (TREE_TYPE (decl)))
8060 TREE_USED (decl) = 1;
be99da77
MS
8061 finish_decl (decl, init, NULL_TREE);
8062 return 0;
8d08fdba 8063 }
be99da77
MS
8064 innermost_code = TREE_CODE (decl);
8065 if (decl_context == FIELD && ctype == NULL_TREE)
8066 ctype = current_class_type;
45537677 8067 if (ctype
c11b6f21 8068 && TREE_OPERAND (decl, 0)
45537677
MS
8069 && (TREE_CODE (TREE_OPERAND (decl, 0)) == TYPE_DECL
8070 && ((DECL_NAME (TREE_OPERAND (decl, 0))
8071 == constructor_name_full (ctype))
8072 || (DECL_NAME (TREE_OPERAND (decl, 0))
8073 == constructor_name (ctype)))))
be99da77
MS
8074 TREE_OPERAND (decl, 0) = constructor_name (ctype);
8075 next = &TREE_OPERAND (decl, 0);
8076 decl = *next;
8077 if (ctype != NULL_TREE
8078 && decl != NULL_TREE && flags != DTOR_FLAG
8079 && decl == constructor_name (ctype))
8d08fdba 8080 {
be99da77
MS
8081 return_type = return_ctor;
8082 ctor_return_type = ctype;
8d08fdba 8083 }
be99da77
MS
8084 ctype = NULL_TREE;
8085 break;
386b8a85
JM
8086
8087 case TEMPLATE_ID_EXPR:
8088 {
8089 tree fns = TREE_OPERAND (decl, 0);
8090
8091 if (TREE_CODE (fns) == LOOKUP_EXPR)
8092 fns = TREE_OPERAND (fns, 0);
8093
8094 if (TREE_CODE (fns) == IDENTIFIER_NODE)
8095 dname = fns;
f84b4be9 8096 else if (is_overloaded_fn (fns))
386b8a85
JM
8097 dname = DECL_NAME (get_first_fn (fns));
8098 else
f84b4be9 8099 my_friendly_abort (0);
386b8a85 8100 }
2c73f9f5 8101 /* Fall through. */
be99da77
MS
8102
8103 case IDENTIFIER_NODE:
386b8a85
JM
8104 if (TREE_CODE (decl) == IDENTIFIER_NODE)
8105 dname = decl;
8106
be99da77
MS
8107 next = 0;
8108
8109 if (is_rid (dname))
8d08fdba 8110 {
be99da77
MS
8111 cp_error ("declarator-id missing; using reserved word `%D'",
8112 dname);
8113 name = IDENTIFIER_POINTER (dname);
8d08fdba 8114 }
be99da77 8115 if (! IDENTIFIER_OPNAME_P (dname)
956d6950 8116 /* GNU/Linux headers use '__op'. Arrgh. */
a703fb38 8117 || (IDENTIFIER_TYPENAME_P (dname) && ! TREE_TYPE (dname)))
be99da77 8118 name = IDENTIFIER_POINTER (dname);
8d08fdba
MS
8119 else
8120 {
be99da77 8121 if (IDENTIFIER_TYPENAME_P (dname))
8d08fdba 8122 {
be99da77
MS
8123 my_friendly_assert (flags == NO_SPECIAL, 154);
8124 flags = TYPENAME_FLAG;
8125 ctor_return_type = TREE_TYPE (dname);
8126 return_type = return_conversion;
8d08fdba 8127 }
be99da77 8128 name = operator_name_string (dname);
8d08fdba 8129 }
be99da77 8130 break;
8d08fdba 8131
be99da77
MS
8132 /* C++ extension */
8133 case SCOPE_REF:
8134 {
8135 /* Perform error checking, and decide on a ctype. */
8136 tree cname = TREE_OPERAND (decl, 0);
8137 if (cname == NULL_TREE)
8138 ctype = NULL_TREE;
2c73f9f5
ML
8139 else if (TREE_CODE (cname) == NAMESPACE_DECL)
8140 {
8141 ctype = NULL_TREE;
8142 in_namespace = TREE_OPERAND (decl, 0);
8143 TREE_OPERAND (decl, 0) = NULL_TREE;
8144 }
be99da77
MS
8145 else if (! is_aggr_type (cname, 1))
8146 TREE_OPERAND (decl, 0) = NULL_TREE;
8147 /* Must test TREE_OPERAND (decl, 1), in case user gives
8148 us `typedef (class::memfunc)(int); memfunc *memfuncptr;' */
8149 else if (TREE_OPERAND (decl, 1)
8150 && TREE_CODE (TREE_OPERAND (decl, 1)) == INDIRECT_REF)
8151 ctype = cname;
73b0fce8
KL
8152 else if (TREE_CODE (cname) == TEMPLATE_TYPE_PARM
8153 || TREE_CODE (cname) == TEMPLATE_TEMPLATE_PARM)
5566b478
MS
8154 {
8155 cp_error ("`%T::%D' is not a valid declarator", cname,
8156 TREE_OPERAND (decl, 1));
8157 cp_error (" perhaps you want `typename %T::%D' to make it a type",
8158 cname, TREE_OPERAND (decl, 1));
8159 return void_type_node;
8160 }
be99da77
MS
8161 else if (ctype == NULL_TREE)
8162 ctype = cname;
8163 else if (TREE_COMPLEXITY (decl) == current_class_depth)
8164 TREE_OPERAND (decl, 0) = ctype;
8165 else
8166 {
8167 if (! UNIQUELY_DERIVED_FROM_P (cname, ctype))
8168 {
8169 cp_error ("type `%T' is not derived from type `%T'",
8170 cname, ctype);
8171 TREE_OPERAND (decl, 0) = NULL_TREE;
8172 }
8173 else
8174 ctype = cname;
8175 }
8176
c91a56d2
MS
8177 if (ctype && TREE_CODE (TREE_OPERAND (decl, 1)) == TYPE_DECL
8178 && ((DECL_NAME (TREE_OPERAND (decl, 1))
8179 == constructor_name_full (ctype))
8180 || (DECL_NAME (TREE_OPERAND (decl, 1))
8181 == constructor_name (ctype))))
be99da77
MS
8182 TREE_OPERAND (decl, 1) = constructor_name (ctype);
8183 next = &TREE_OPERAND (decl, 1);
8184 decl = *next;
8185 if (ctype)
8186 {
8187 if (TREE_CODE (decl) == IDENTIFIER_NODE
8188 && constructor_name (ctype) == decl)
8189 {
8190 return_type = return_ctor;
8191 ctor_return_type = ctype;
8192 }
8193 else if (TREE_CODE (decl) == BIT_NOT_EXPR
8194 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
8195 && (constructor_name (ctype) == TREE_OPERAND (decl, 0)
8196 || constructor_name_full (ctype) == TREE_OPERAND (decl, 0)))
8197 {
8198 return_type = return_dtor;
8199 ctor_return_type = ctype;
8200 flags = DTOR_FLAG;
8201 TREE_OPERAND (decl, 0) = constructor_name (ctype);
8202 next = &TREE_OPERAND (decl, 0);
8203 }
8204 }
8205 }
8206 break;
8207
8208 case ERROR_MARK:
8209 next = 0;
8210 break;
8211
45537677
MS
8212 case TYPE_DECL:
8213 /* Parse error puts this typespec where
8214 a declarator should go. */
8215 cp_error ("`%T' specified as declarator-id", DECL_NAME (decl));
8216 if (TREE_TYPE (decl) == current_class_type)
8217 cp_error (" perhaps you want `%T' for a constructor",
8218 current_class_name);
8219 dname = DECL_NAME (decl);
8220 name = IDENTIFIER_POINTER (dname);
8221
e92cc029 8222 /* Avoid giving two errors for this. */
45537677
MS
8223 IDENTIFIER_CLASS_VALUE (dname) = NULL_TREE;
8224
8225 declspecs = temp_tree_cons (NULL_TREE, integer_type_node,
8226 declspecs);
8227 *next = dname;
8228 next = 0;
8229 break;
8230
be99da77 8231 default:
be99da77
MS
8232 cp_compiler_error ("`%D' as declarator", decl);
8233 return 0; /* We used to do a 155 abort here. */
8d08fdba 8234 }
be99da77 8235 }
8d08fdba
MS
8236 if (name == NULL)
8237 name = "type name";
8238 }
8239
8240 /* A function definition's declarator must have the form of
8241 a function declarator. */
8242
8243 if (funcdef_flag && innermost_code != CALL_EXPR)
8244 return 0;
8245
e1cd6e56
MS
8246 if (((dname && IDENTIFIER_OPNAME_P (dname)) || flags == TYPENAME_FLAG)
8247 && innermost_code != CALL_EXPR
8248 && ! (ctype && declspecs == NULL_TREE))
8249 {
8250 cp_error ("declaration of `%D' as non-function", dname);
8251 return void_type_node;
8252 }
8253
8d08fdba
MS
8254 /* Anything declared one level down from the top level
8255 must be one of the parameters of a function
8256 (because the body is at least two levels down). */
8257
8258 /* This heuristic cannot be applied to C++ nodes! Fixed, however,
8259 by not allowing C++ class definitions to specify their parameters
8260 with xdecls (must be spec.d in the parmlist).
8261
8262 Since we now wait to push a class scope until we are sure that
8263 we are in a legitimate method context, we must set oldcname
a9aedbc2
MS
8264 explicitly (since current_class_name is not yet alive).
8265
8266 We also want to avoid calling this a PARM if it is in a namespace. */
8d08fdba 8267
5566b478
MS
8268 if (decl_context == NORMAL && ! namespace_bindings_p ()
8269 && ! pseudo_global_level_p ())
a9aedbc2
MS
8270 {
8271 struct binding_level *b = current_binding_level;
8272 current_binding_level = b->level_chain;
8273 if (current_binding_level != 0 && toplevel_bindings_p ())
8274 decl_context = PARM;
8275 current_binding_level = b;
8276 }
8d08fdba
MS
8277
8278 /* Look through the decl specs and record which ones appear.
8279 Some typespecs are defined as built-in typenames.
8280 Others, the ones that are modifiers of other types,
8281 are represented by bits in SPECBITS: set the bits for
8282 the modifiers that appear. Storage class keywords are also in SPECBITS.
8283
8284 If there is a typedef name or a type, store the type in TYPE.
8285 This includes builtin typedefs such as `int'.
8286
8287 Set EXPLICIT_INT if the type is `int' or `char' and did not
8288 come from a user typedef.
8289
8290 Set LONGLONG if `long' is mentioned twice.
8291
8292 For C++, constructors and destructors have their own fast treatment. */
8293
8294 for (spec = declspecs; spec; spec = TREE_CHAIN (spec))
8295 {
8296 register int i;
8297 register tree id;
8298
8299 /* Certain parse errors slip through. For example,
8300 `int class;' is not caught by the parser. Try
8301 weakly to recover here. */
8302 if (TREE_CODE (spec) != TREE_LIST)
8303 return 0;
8304
8305 id = TREE_VALUE (spec);
8306
8307 if (TREE_CODE (id) == IDENTIFIER_NODE)
8308 {
a3203465
MS
8309 if (id == ridpointers[(int) RID_INT]
8310 || id == ridpointers[(int) RID_CHAR]
8311 || id == ridpointers[(int) RID_BOOL]
8312 || id == ridpointers[(int) RID_WCHAR])
8d08fdba
MS
8313 {
8314 if (type)
8ccc31eb
MS
8315 {
8316 if (id == ridpointers[(int) RID_BOOL])
8317 error ("`bool' is now a keyword");
8318 else
8319 cp_error ("extraneous `%T' ignored", id);
8320 }
8d08fdba
MS
8321 else
8322 {
a3203465
MS
8323 if (id == ridpointers[(int) RID_INT])
8324 explicit_int = 1;
8325 else if (id == ridpointers[(int) RID_CHAR])
8326 explicit_char = 1;
8d08fdba
MS
8327 type = TREE_TYPE (IDENTIFIER_GLOBAL_VALUE (id));
8328 }
8329 goto found;
8330 }
e92cc029 8331 /* C++ aggregate types. */
8d08fdba
MS
8332 if (IDENTIFIER_HAS_TYPE_VALUE (id))
8333 {
8334 if (type)
8335 cp_error ("multiple declarations `%T' and `%T'", type, id);
8336 else
8337 type = IDENTIFIER_TYPE_VALUE (id);
8338 goto found;
8339 }
8340
f376e137 8341 for (i = (int) RID_FIRST_MODIFIER; i <= (int) RID_LAST_MODIFIER; i++)
8d08fdba
MS
8342 {
8343 if (ridpointers[i] == id)
8344 {
8345 if (i == (int) RID_LONG && RIDBIT_SETP (i, specbits))
8346 {
e1cd6e56
MS
8347 if (pedantic && ! in_system_header)
8348 pedwarn ("ANSI C++ does not support `long long'");
9a3b49ac 8349 if (longlong)
a0a33927 8350 error ("`long long long' is too long for GCC");
8d08fdba
MS
8351 else
8352 longlong = 1;
8353 }
8354 else if (RIDBIT_SETP (i, specbits))
a0a33927 8355 pedwarn ("duplicate `%s'", IDENTIFIER_POINTER (id));
8d08fdba
MS
8356 RIDBIT_SET (i, specbits);
8357 goto found;
8358 }
8359 }
8360 }
e92cc029 8361 /* C++ aggregate types. */
73b0fce8 8362 else if (TREE_CODE (id) == TYPE_DECL || TREE_CODE (id) == TEMPLATE_DECL)
45537677
MS
8363 {
8364 if (type)
8365 cp_error ("multiple declarations `%T' and `%T'", type,
8366 TREE_TYPE (id));
8367 else
5566b478
MS
8368 {
8369 type = TREE_TYPE (id);
8370 TREE_VALUE (spec) = type;
8371 }
45537677
MS
8372 goto found;
8373 }
8d08fdba
MS
8374 if (type)
8375 error ("two or more data types in declaration of `%s'", name);
8376 else if (TREE_CODE (id) == IDENTIFIER_NODE)
8377 {
8378 register tree t = lookup_name (id, 1);
8379 if (!t || TREE_CODE (t) != TYPE_DECL)
8380 error ("`%s' fails to be a typedef or built in type",
8381 IDENTIFIER_POINTER (id));
8382 else
8383 {
8384 type = TREE_TYPE (t);
6125f3be
DE
8385#if 0
8386 /* See the code below that used this. */
f6abb50a 8387 decl_machine_attr = DECL_MACHINE_ATTRIBUTES (id);
6125f3be 8388#endif
8d08fdba
MS
8389 typedef_decl = t;
8390 }
8391 }
bd6dd845 8392 else if (id != error_mark_node)
8d08fdba
MS
8393 /* Can't change CLASS nodes into RECORD nodes here! */
8394 type = id;
8395
8396 found: ;
8397 }
8398
8399 typedef_type = type;
8400
37c46b43 8401 /* No type at all: default to `int', and set DEFAULTED_INT
8d08fdba
MS
8402 because it was not a user-defined typedef.
8403 Except when we have a `typedef' inside a signature, in
8404 which case the type defaults to `unknown type' and is
8405 instantiated when assigning to a signature pointer or ref. */
8406
a3203465
MS
8407 if (type == NULL_TREE
8408 && (RIDBIT_SETP (RID_SIGNED, specbits)
8409 || RIDBIT_SETP (RID_UNSIGNED, specbits)
8410 || RIDBIT_SETP (RID_LONG, specbits)
8411 || RIDBIT_SETP (RID_SHORT, specbits)))
8412 {
8413 /* These imply 'int'. */
8414 type = integer_type_node;
37c46b43 8415 defaulted_int = 1;
a3203465
MS
8416 }
8417
8d08fdba
MS
8418 if (type == NULL_TREE)
8419 {
8420 explicit_int = -1;
8421 if (return_type == return_dtor)
8422 type = void_type_node;
8423 else if (return_type == return_ctor)
f30432d7 8424 type = build_pointer_type (ctor_return_type);
51c184be
MS
8425 else if (return_type == return_conversion)
8426 type = ctor_return_type;
8d08fdba
MS
8427 else if (current_class_type
8428 && IS_SIGNATURE (current_class_type)
fc378698 8429 && RIDBIT_SETP (RID_TYPEDEF, specbits)
8d08fdba
MS
8430 && (decl_context == FIELD || decl_context == NORMAL))
8431 {
8432 explicit_int = 0;
8433 opaque_typedef = 1;
8434 type = copy_node (opaque_type_node);
8435 }
8436 else
8437 {
a28e3c7f
MS
8438 if (funcdef_flag)
8439 {
8440 if (warn_return_type
a3203465 8441 && return_type == return_normal)
a28e3c7f
MS
8442 /* Save warning until we know what is really going on. */
8443 warn_about_return_type = 1;
8444 }
a3203465
MS
8445 else if (RIDBIT_SETP (RID_TYPEDEF, specbits))
8446 pedwarn ("ANSI C++ forbids typedef which does not specify a type");
343fdf03
JM
8447 else if (innermost_code != CALL_EXPR || pedantic
8448 || (warn_return_type && return_type == return_normal))
8449 {
8450 if (innermost_code == CALL_EXPR)
8451 cp_pedwarn ("return-type of `%D' defaults to `int'", dname);
8452 else
8453 cp_pedwarn ("ANSI C++ forbids declaration `%D' with no type",
8454 dname);
8455 }
8d08fdba
MS
8456 type = integer_type_node;
8457 }
8458 }
8459 else if (return_type == return_dtor)
8460 {
8461 error ("return type specification for destructor invalid");
8462 type = void_type_node;
8463 }
8464 else if (return_type == return_ctor)
8465 {
8466 error ("return type specification for constructor invalid");
f30432d7 8467 type = build_pointer_type (ctor_return_type);
8d08fdba 8468 }
51c184be
MS
8469 else if (return_type == return_conversion)
8470 {
e1cd6e56 8471 if (comptypes (type, ctor_return_type, 1) == 0)
51c184be
MS
8472 cp_error ("operator `%T' declared to return `%T'",
8473 ctor_return_type, type);
8474 else
8475 cp_pedwarn ("return type specified for `operator %T'",
8476 ctor_return_type);
8477
8478 type = ctor_return_type;
8479 }
8d08fdba
MS
8480
8481 ctype = NULL_TREE;
8482
8483 /* Now process the modifiers that were specified
8484 and check for invalid combinations. */
8485
8486 /* Long double is a special combination. */
8487
8488 if (RIDBIT_SETP (RID_LONG, specbits)
8489 && TYPE_MAIN_VARIANT (type) == double_type_node)
8490 {
8491 RIDBIT_RESET (RID_LONG, specbits);
8492 type = build_type_variant (long_double_type_node, TYPE_READONLY (type),
8493 TYPE_VOLATILE (type));
8494 }
8495
8496 /* Check all other uses of type modifiers. */
8497
8498 if (RIDBIT_SETP (RID_UNSIGNED, specbits)
8499 || RIDBIT_SETP (RID_SIGNED, specbits)
8500 || RIDBIT_SETP (RID_LONG, specbits)
8501 || RIDBIT_SETP (RID_SHORT, specbits))
8502 {
8503 int ok = 0;
8504
8505 if (TREE_CODE (type) == REAL_TYPE)
8506 error ("short, signed or unsigned invalid for `%s'", name);
b7484fbe 8507 else if (TREE_CODE (type) != INTEGER_TYPE)
8d08fdba
MS
8508 error ("long, short, signed or unsigned invalid for `%s'", name);
8509 else if (RIDBIT_SETP (RID_LONG, specbits)
8510 && RIDBIT_SETP (RID_SHORT, specbits))
8511 error ("long and short specified together for `%s'", name);
8512 else if ((RIDBIT_SETP (RID_LONG, specbits)
8513 || RIDBIT_SETP (RID_SHORT, specbits))
8514 && explicit_char)
8515 error ("long or short specified with char for `%s'", name);
8516 else if ((RIDBIT_SETP (RID_LONG, specbits)
8517 || RIDBIT_SETP (RID_SHORT, specbits))
8518 && TREE_CODE (type) == REAL_TYPE)
8519 error ("long or short specified with floating type for `%s'", name);
8520 else if (RIDBIT_SETP (RID_SIGNED, specbits)
8521 && RIDBIT_SETP (RID_UNSIGNED, specbits))
8522 error ("signed and unsigned given together for `%s'", name);
8523 else
8524 {
8525 ok = 1;
37c46b43 8526 if (!explicit_int && !defaulted_int && !explicit_char && pedantic)
8d08fdba
MS
8527 {
8528 pedwarn ("long, short, signed or unsigned used invalidly for `%s'",
8529 name);
8530 if (flag_pedantic_errors)
8531 ok = 0;
8532 }
8533 }
8534
8535 /* Discard the type modifiers if they are invalid. */
8536 if (! ok)
8537 {
8538 RIDBIT_RESET (RID_UNSIGNED, specbits);
8539 RIDBIT_RESET (RID_SIGNED, specbits);
8540 RIDBIT_RESET (RID_LONG, specbits);
8541 RIDBIT_RESET (RID_SHORT, specbits);
8542 longlong = 0;
8543 }
8544 }
8545
37c46b43
MS
8546 if (RIDBIT_SETP (RID_COMPLEX, specbits)
8547 && TREE_CODE (type) != INTEGER_TYPE && TREE_CODE (type) != REAL_TYPE)
8548 {
8549 error ("complex invalid for `%s'", name);
8550 RIDBIT_RESET (RID_COMPLEX, specbits);
8551 }
8552
8d08fdba
MS
8553 /* Decide whether an integer type is signed or not.
8554 Optionally treat bitfields as signed by default. */
8555 if (RIDBIT_SETP (RID_UNSIGNED, specbits)
8d08fdba 8556 || (bitfield && ! flag_signed_bitfields
37c46b43 8557 && (explicit_int || defaulted_int || explicit_char
8d08fdba
MS
8558 /* A typedef for plain `int' without `signed'
8559 can be controlled just like plain `int'. */
8560 || ! (typedef_decl != NULL_TREE
8561 && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl)))
8562 && TREE_CODE (type) != ENUMERAL_TYPE
8563 && RIDBIT_NOTSETP (RID_SIGNED, specbits)))
8564 {
8565 if (longlong)
8566 type = long_long_unsigned_type_node;
8567 else if (RIDBIT_SETP (RID_LONG, specbits))
8568 type = long_unsigned_type_node;
8569 else if (RIDBIT_SETP (RID_SHORT, specbits))
8570 type = short_unsigned_type_node;
8571 else if (type == char_type_node)
8572 type = unsigned_char_type_node;
8573 else if (typedef_decl)
8574 type = unsigned_type (type);
8575 else
8576 type = unsigned_type_node;
8577 }
8578 else if (RIDBIT_SETP (RID_SIGNED, specbits)
8579 && type == char_type_node)
8580 type = signed_char_type_node;
8581 else if (longlong)
8582 type = long_long_integer_type_node;
8583 else if (RIDBIT_SETP (RID_LONG, specbits))
8584 type = long_integer_type_node;
8585 else if (RIDBIT_SETP (RID_SHORT, specbits))
8586 type = short_integer_type_node;
8587
37c46b43
MS
8588 if (RIDBIT_SETP (RID_COMPLEX, specbits))
8589 {
8590 /* If we just have "complex", it is equivalent to
8591 "complex double", but if any modifiers at all are specified it is
8592 the complex form of TYPE. E.g, "complex short" is
8593 "complex short int". */
8594
8595 if (defaulted_int && ! longlong
8596 && ! (RIDBIT_SETP (RID_LONG, specbits)
8597 || RIDBIT_SETP (RID_SHORT, specbits)
8598 || RIDBIT_SETP (RID_SIGNED, specbits)
8599 || RIDBIT_SETP (RID_UNSIGNED, specbits)))
8600 type = complex_double_type_node;
8601 else if (type == integer_type_node)
8602 type = complex_integer_type_node;
8603 else if (type == float_type_node)
8604 type = complex_float_type_node;
8605 else if (type == double_type_node)
8606 type = complex_double_type_node;
8607 else if (type == long_double_type_node)
8608 type = complex_long_double_type_node;
8609 else
8610 type = build_complex_type (type);
8611 }
8612
3ac3d9ea
MM
8613 if (return_type == return_conversion
8614 && (RIDBIT_SETP (RID_CONST, specbits)
8615 || RIDBIT_SETP (RID_VOLATILE, specbits)))
8616 cp_error ("`operator %T' cannot be cv-qualified",
8617 ctor_return_type);
8618
8d08fdba
MS
8619 /* Set CONSTP if this declaration is `const', whether by
8620 explicit specification or via a typedef.
8621 Likewise for VOLATILEP. */
8622
8623 constp = !! RIDBIT_SETP (RID_CONST, specbits) + TYPE_READONLY (type);
8624 volatilep = !! RIDBIT_SETP (RID_VOLATILE, specbits) + TYPE_VOLATILE (type);
a1774733 8625 type = build_type_variant (type, 0, 0);
8d08fdba
MS
8626 staticp = 0;
8627 inlinep = !! RIDBIT_SETP (RID_INLINE, specbits);
8d08fdba 8628 virtualp = RIDBIT_SETP (RID_VIRTUAL, specbits);
db5ae43f
MS
8629 RIDBIT_RESET (RID_VIRTUAL, specbits);
8630 explicitp = RIDBIT_SETP (RID_EXPLICIT, specbits) != 0;
8631 RIDBIT_RESET (RID_EXPLICIT, specbits);
8d08fdba 8632
8d08fdba
MS
8633 if (RIDBIT_SETP (RID_STATIC, specbits))
8634 staticp = 1 + (decl_context == FIELD);
8635
8636 if (virtualp && staticp == 2)
8637 {
8638 cp_error ("member `%D' cannot be declared both virtual and static",
8639 dname);
8640 staticp = 0;
8641 }
8642 friendp = RIDBIT_SETP (RID_FRIEND, specbits);
8d08fdba
MS
8643 RIDBIT_RESET (RID_FRIEND, specbits);
8644
8645 if (RIDBIT_SETP (RID_MUTABLE, specbits))
8646 {
8647 if (decl_context == PARM)
8648 {
db5ae43f 8649 error ("non-member `%s' cannot be declared `mutable'", name);
8d08fdba
MS
8650 RIDBIT_RESET (RID_MUTABLE, specbits);
8651 }
8652 else if (friendp || decl_context == TYPENAME)
8653 {
db5ae43f 8654 error ("non-object member `%s' cannot be declared `mutable'", name);
8d08fdba
MS
8655 RIDBIT_RESET (RID_MUTABLE, specbits);
8656 }
8d08fdba
MS
8657 }
8658
8659 /* Warn if two storage classes are given. Default to `auto'. */
8660
8661 if (RIDBIT_ANY_SET (specbits))
8662 {
8663 if (RIDBIT_SETP (RID_STATIC, specbits)) nclasses++;
8664 if (RIDBIT_SETP (RID_EXTERN, specbits)) nclasses++;
8665 if (decl_context == PARM && nclasses > 0)
8666 error ("storage class specifiers invalid in parameter declarations");
8667 if (RIDBIT_SETP (RID_TYPEDEF, specbits))
8668 {
8669 if (decl_context == PARM)
8670 error ("typedef declaration invalid in parameter declaration");
8671 nclasses++;
8672 }
8673 if (RIDBIT_SETP (RID_AUTO, specbits)) nclasses++;
8674 if (RIDBIT_SETP (RID_REGISTER, specbits)) nclasses++;
8675 }
8676
8677 /* Give error if `virtual' is used outside of class declaration. */
b7484fbe
MS
8678 if (virtualp
8679 && (current_class_name == NULL_TREE || decl_context != FIELD))
8d08fdba
MS
8680 {
8681 error ("virtual outside class declaration");
8682 virtualp = 0;
8683 }
8684 if (current_class_name == NULL_TREE && RIDBIT_SETP (RID_MUTABLE, specbits))
8685 {
8686 error ("only members can be declared mutable");
8687 RIDBIT_RESET (RID_MUTABLE, specbits);
8688 }
8689
8690 /* Static anonymous unions are dealt with here. */
8691 if (staticp && decl_context == TYPENAME
8692 && TREE_CODE (declspecs) == TREE_LIST
8693 && TREE_CODE (TREE_VALUE (declspecs)) == UNION_TYPE
8694 && ANON_AGGRNAME_P (TYPE_IDENTIFIER (TREE_VALUE (declspecs))))
8695 decl_context = FIELD;
8696
8697 /* Give error if `const,' `volatile,' `inline,' `friend,' or `virtual'
8698 is used in a signature member function declaration. */
8699 if (decl_context == FIELD
8700 && IS_SIGNATURE (current_class_type)
fc378698 8701 && RIDBIT_NOTSETP (RID_TYPEDEF, specbits))
8d08fdba
MS
8702 {
8703 if (constp)
8704 {
8705 error ("`const' specified for signature member function `%s'", name);
8706 constp = 0;
8707 }
8708 if (volatilep)
8709 {
a28e3c7f
MS
8710 error ("`volatile' specified for signature member function `%s'",
8711 name);
8d08fdba
MS
8712 volatilep = 0;
8713 }
8714 if (inlinep)
8715 {
8716 error ("`inline' specified for signature member function `%s'", name);
8717 /* Later, we'll make signature member functions inline. */
8718 inlinep = 0;
8719 }
8720 if (friendp)
8721 {
8722 error ("`friend' declaration in signature definition");
8723 friendp = 0;
8724 }
8725 if (virtualp)
8726 {
a28e3c7f
MS
8727 error ("`virtual' specified for signature member function `%s'",
8728 name);
8d08fdba
MS
8729 /* Later, we'll make signature member functions virtual. */
8730 virtualp = 0;
8731 }
8732 }
8733
8734 /* Warn about storage classes that are invalid for certain
8735 kinds of declarations (parameters, typenames, etc.). */
8736
8737 if (nclasses > 1)
8738 error ("multiple storage classes in declaration of `%s'", name);
8739 else if (decl_context != NORMAL && nclasses > 0)
8740 {
db5ae43f 8741 if ((decl_context == PARM || decl_context == CATCHPARM)
8d08fdba
MS
8742 && (RIDBIT_SETP (RID_REGISTER, specbits)
8743 || RIDBIT_SETP (RID_AUTO, specbits)))
8744 ;
fc378698
MS
8745 else if (RIDBIT_SETP (RID_TYPEDEF, specbits))
8746 ;
8d08fdba 8747 else if (decl_context == FIELD
fc378698 8748 && ! IS_SIGNATURE (current_class_type)
8d08fdba
MS
8749 /* C++ allows static class elements */
8750 && RIDBIT_SETP (RID_STATIC, specbits))
8751 /* C++ also allows inlines and signed and unsigned elements,
8752 but in those cases we don't come in here. */
8753 ;
8754 else
8755 {
8756 if (decl_context == FIELD)
8757 {
b7484fbe
MS
8758 tree tmp = NULL_TREE;
8759 register int op = 0;
8760
8761 if (declarator)
8762 {
9e9ff709
MS
8763 /* Avoid trying to get an operand off an identifier node. */
8764 if (TREE_CODE (declarator) == IDENTIFIER_NODE)
8765 tmp = declarator;
8766 else
8767 tmp = TREE_OPERAND (declarator, 0);
b7484fbe
MS
8768 op = IDENTIFIER_OPNAME_P (tmp);
8769 }
8d08fdba
MS
8770 error ("storage class specified for %s `%s'",
8771 IS_SIGNATURE (current_class_type)
8772 ? (op
8773 ? "signature member operator"
8774 : "signature member function")
b7484fbe 8775 : (op ? "member operator" : "field"),
8d08fdba
MS
8776 op ? operator_name_string (tmp) : name);
8777 }
8778 else
db5ae43f 8779 error (((decl_context == PARM || decl_context == CATCHPARM)
8d08fdba
MS
8780 ? "storage class specified for parameter `%s'"
8781 : "storage class specified for typename"), name);
8782 RIDBIT_RESET (RID_REGISTER, specbits);
8783 RIDBIT_RESET (RID_AUTO, specbits);
8784 RIDBIT_RESET (RID_EXTERN, specbits);
8785
8786 if (decl_context == FIELD && IS_SIGNATURE (current_class_type))
8787 {
8788 RIDBIT_RESET (RID_STATIC, specbits);
8789 staticp = 0;
8790 }
8791 }
8792 }
8793 else if (RIDBIT_SETP (RID_EXTERN, specbits) && initialized && !funcdef_flag)
8794 {
a9aedbc2 8795 if (toplevel_bindings_p ())
8d08fdba 8796 {
59be0cdd 8797 /* It's common practice (and completely valid) to have a const
8d08fdba
MS
8798 be initialized and declared extern. */
8799 if (! constp)
8800 warning ("`%s' initialized and declared `extern'", name);
8801 }
8802 else
8803 error ("`%s' has both `extern' and initializer", name);
8804 }
8805 else if (RIDBIT_SETP (RID_EXTERN, specbits) && funcdef_flag
a9aedbc2 8806 && ! toplevel_bindings_p ())
8d08fdba 8807 error ("nested function `%s' declared `extern'", name);
a9aedbc2 8808 else if (toplevel_bindings_p ())
8d08fdba
MS
8809 {
8810 if (RIDBIT_SETP (RID_AUTO, specbits))
8811 error ("top-level declaration of `%s' specifies `auto'", name);
8d08fdba
MS
8812 }
8813
909e536a
MS
8814 if (nclasses > 0 && friendp)
8815 error ("storage class specifiers invalid in friend function declarations");
8816
8d08fdba
MS
8817 /* Now figure out the structure of the declarator proper.
8818 Descend through it, creating more complex types, until we reach
8819 the declared identifier (or NULL_TREE, in an absolute declarator). */
8820
386b8a85
JM
8821 while (declarator && TREE_CODE (declarator) != IDENTIFIER_NODE
8822 && TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
8d08fdba
MS
8823 {
8824 /* Each level of DECLARATOR is either an ARRAY_REF (for ...[..]),
8825 an INDIRECT_REF (for *...),
8826 a CALL_EXPR (for ...(...)),
8827 an identifier (for the name being declared)
8828 or a null pointer (for the place in an absolute declarator
8829 where the name was omitted).
8830 For the last two cases, we have just exited the loop.
8831
8832 For C++ it could also be
8833 a SCOPE_REF (for class :: ...). In this case, we have converted
8834 sensible names to types, and those are the values we use to
8835 qualify the member name.
8836 an ADDR_EXPR (for &...),
8837 a BIT_NOT_EXPR (for destructors)
8d08fdba
MS
8838
8839 At this point, TYPE is the type of elements of an array,
8840 or for a function to return, or for a pointer to point to.
8841 After this sequence of ifs, TYPE is the type of the
8842 array or function or pointer, and DECLARATOR has had its
8843 outermost layer removed. */
8844
bd6dd845 8845 if (type == error_mark_node)
8d08fdba
MS
8846 {
8847 if (TREE_CODE (declarator) == SCOPE_REF)
8848 declarator = TREE_OPERAND (declarator, 1);
8849 else
8850 declarator = TREE_OPERAND (declarator, 0);
8851 continue;
8852 }
8853 if (quals != NULL_TREE
8854 && (declarator == NULL_TREE
8855 || TREE_CODE (declarator) != SCOPE_REF))
8856 {
8857 if (ctype == NULL_TREE && TREE_CODE (type) == METHOD_TYPE)
8858 ctype = TYPE_METHOD_BASETYPE (type);
8859 if (ctype != NULL_TREE)
8860 {
8d08fdba 8861 tree dummy = build_decl (TYPE_DECL, NULL_TREE, type);
8d08fdba
MS
8862 ctype = grok_method_quals (ctype, dummy, quals);
8863 type = TREE_TYPE (dummy);
8864 quals = NULL_TREE;
8865 }
8866 }
8867 switch (TREE_CODE (declarator))
8868 {
8869 case ARRAY_REF:
8870 {
8871 register tree itype = NULL_TREE;
8872 register tree size = TREE_OPERAND (declarator, 1);
b7484fbe
MS
8873 /* The index is a signed object `sizetype' bits wide. */
8874 tree index_type = signed_type (sizetype);
8d08fdba
MS
8875
8876 declarator = TREE_OPERAND (declarator, 0);
8877
8878 /* Check for some types that there cannot be arrays of. */
8879
a1774733 8880 if (TREE_CODE (type) == VOID_TYPE)
8d08fdba
MS
8881 {
8882 cp_error ("declaration of `%D' as array of voids", dname);
8883 type = error_mark_node;
8884 }
8885
8886 if (TREE_CODE (type) == FUNCTION_TYPE)
8887 {
8888 cp_error ("declaration of `%D' as array of functions", dname);
8889 type = error_mark_node;
8890 }
8891
8892 /* ARM $8.4.3: Since you can't have a pointer to a reference,
8893 you can't have arrays of references. If we allowed them,
59be0cdd 8894 then we'd be saying x[i] is valid for an array x, but
8d08fdba
MS
8895 then you'd have to ask: what does `*(x + i)' mean? */
8896 if (TREE_CODE (type) == REFERENCE_TYPE)
8897 {
8898 if (decl_context == TYPENAME)
8899 cp_error ("cannot make arrays of references");
8900 else
8901 cp_error ("declaration of `%D' as array of references",
8902 dname);
8903 type = error_mark_node;
8904 }
8905
8906 if (TREE_CODE (type) == OFFSET_TYPE)
8907 {
8908 cp_error ("declaration of `%D' as array of data members",
8909 dname);
8910 type = error_mark_node;
8911 }
8912
8913 if (TREE_CODE (type) == METHOD_TYPE)
8914 {
8915 cp_error ("declaration of `%D' as array of function members",
8916 dname);
8917 type = error_mark_node;
8918 }
8919
8920 if (size == error_mark_node)
8921 type = error_mark_node;
8922
8923 if (type == error_mark_node)
8924 continue;
8925
8926 if (size)
8927 {
8928 /* Must suspend_momentary here because the index
8929 type may need to live until the end of the function.
8930 For example, it is used in the declaration of a
8931 variable which requires destructing at the end of
8932 the function; then build_vec_delete will need this
8933 value. */
8934 int yes = suspend_momentary ();
2c73f9f5 8935 /* Might be a cast. */
8d08fdba
MS
8936 if (TREE_CODE (size) == NOP_EXPR
8937 && TREE_TYPE (size) == TREE_TYPE (TREE_OPERAND (size, 0)))
8938 size = TREE_OPERAND (size, 0);
8939
5566b478
MS
8940 /* If this involves a template parameter, it'll be
8941 constant, but we don't know what the value is yet. */
5156628f 8942 if (processing_template_decl)
5566b478
MS
8943 {
8944 itype = make_node (INTEGER_TYPE);
8945 TYPE_MIN_VALUE (itype) = size_zero_node;
8946 TYPE_MAX_VALUE (itype) = build_min
8947 (MINUS_EXPR, sizetype, size, integer_one_node);
8948 goto dont_grok_size;
8949 }
8d08fdba
MS
8950
8951 if (TREE_CODE (TREE_TYPE (size)) != INTEGER_TYPE
8952 && TREE_CODE (TREE_TYPE (size)) != ENUMERAL_TYPE)
8953 {
8954 cp_error ("size of array `%D' has non-integer type",
8955 dname);
8956 size = integer_one_node;
8957 }
8958 if (TREE_READONLY_DECL_P (size))
8959 size = decl_constant_value (size);
e1cd6e56 8960 if (pedantic && integer_zerop (size))
8d08fdba
MS
8961 cp_pedwarn ("ANSI C++ forbids zero-size array `%D'", dname);
8962 if (TREE_CONSTANT (size))
8963 {
dff6b454
RK
8964 int old_flag_pedantic_errors = flag_pedantic_errors;
8965 int old_pedantic = pedantic;
8966 pedantic = flag_pedantic_errors = 1;
8967 /* Always give overflow errors on array subscripts. */
8d08fdba 8968 constant_expression_warning (size);
dff6b454
RK
8969 pedantic = old_pedantic;
8970 flag_pedantic_errors = old_flag_pedantic_errors;
8d08fdba
MS
8971 if (INT_CST_LT (size, integer_zero_node))
8972 {
8973 cp_error ("size of array `%D' is negative", dname);
8974 size = integer_one_node;
8975 }
8d08fdba
MS
8976 }
8977 else
8978 {
e1cd6e56 8979 if (pedantic)
a0a33927
MS
8980 {
8981 if (dname)
8982 cp_pedwarn ("ANSI C++ forbids variable-size array `%D'",
8983 dname);
8984 else
8985 cp_pedwarn ("ANSI C++ forbids variable-size array");
8986 }
8d08fdba 8987 }
b7484fbe 8988
beb53fb8
JM
8989 itype
8990 = fold (build_binary_op (MINUS_EXPR,
37c46b43
MS
8991 cp_convert (index_type, size),
8992 cp_convert (index_type,
8993 integer_one_node), 1));
b7484fbe
MS
8994 if (! TREE_CONSTANT (itype))
8995 itype = variable_size (itype);
594740f3
MS
8996 else if (TREE_OVERFLOW (itype))
8997 {
8998 error ("overflow in array dimension");
8999 TREE_OVERFLOW (itype) = 0;
9000 }
fc378698
MS
9001
9002 /* If we're a parm, we need to have a permanent type so
9003 mangling checks for re-use will work right. If both the
9004 element and index types are permanent, the array type
9005 will be, too. */
9006 if (decl_context == PARM
9007 && allocation_temporary_p () && TREE_PERMANENT (type))
9008 {
9009 push_obstacks (&permanent_obstack, &permanent_obstack);
9010 itype = build_index_type (itype);
9011 pop_obstacks ();
9012 }
9013 else
9014 itype = build_index_type (itype);
9015
5566b478 9016 dont_grok_size:
8d08fdba
MS
9017 resume_momentary (yes);
9018 }
9019
9020 /* Build the array type itself, then merge any constancy or
9021 volatility into the target type. We must do it in this order
9022 to ensure that the TYPE_MAIN_VARIANT field of the array type
9023 is set correctly. */
9024
9025 type = build_cplus_array_type (type, itype);
9026 if (constp || volatilep)
f376e137 9027 type = cp_build_type_variant (type, constp, volatilep);
8d08fdba
MS
9028
9029 ctype = NULL_TREE;
9030 }
9031 break;
9032
9033 case CALL_EXPR:
9034 {
9035 tree arg_types;
f376e137
MS
9036 int funcdecl_p;
9037 tree inner_parms = TREE_OPERAND (declarator, 1);
9038 tree inner_decl = TREE_OPERAND (declarator, 0);
8d08fdba
MS
9039
9040 /* Declaring a function type.
9041 Make sure we have a valid type for the function to return. */
9042#if 0
9043 /* Is this an error? Should they be merged into TYPE here? */
9044 if (pedantic && (constp || volatilep))
9045 pedwarn ("function declared to return const or volatile result");
9046#else
21474714
MS
9047 /* Merge any constancy or volatility into the function return
9048 type. */
8d08fdba
MS
9049
9050 if (constp || volatilep)
9051 {
f376e137 9052 type = cp_build_type_variant (type, constp, volatilep);
8d08fdba
MS
9053 if (IS_AGGR_TYPE (type))
9054 build_pointer_type (type);
9055 constp = 0;
9056 volatilep = 0;
9057 }
9058#endif
9059
9060 /* Warn about some types functions can't return. */
9061
9062 if (TREE_CODE (type) == FUNCTION_TYPE)
9063 {
9064 error ("`%s' declared as function returning a function", name);
9065 type = integer_type_node;
9066 }
9067 if (TREE_CODE (type) == ARRAY_TYPE)
9068 {
9069 error ("`%s' declared as function returning an array", name);
9070 type = integer_type_node;
9071 }
9072
f376e137
MS
9073 if (inner_decl && TREE_CODE (inner_decl) == SCOPE_REF)
9074 inner_decl = TREE_OPERAND (inner_decl, 1);
9075
386b8a85
JM
9076 if (inner_decl && TREE_CODE (inner_decl) == TEMPLATE_ID_EXPR)
9077 inner_decl = dname;
9078
b7484fbe
MS
9079 /* Pick up type qualifiers which should be applied to `this'. */
9080 quals = TREE_OPERAND (declarator, 2);
9081
c11b6f21
MS
9082 /* Pick up the exception specifications. */
9083 raises = TREE_TYPE (declarator);
9084
f376e137
MS
9085 /* Say it's a definition only for the CALL_EXPR
9086 closest to the identifier. */
beb53fb8 9087 funcdecl_p
386b8a85
JM
9088 = inner_decl
9089 && (TREE_CODE (inner_decl) == IDENTIFIER_NODE
9090 || TREE_CODE (inner_decl) == TEMPLATE_ID_EXPR
9091 || TREE_CODE (inner_decl) == BIT_NOT_EXPR);
9092
8d08fdba
MS
9093 if (ctype == NULL_TREE
9094 && decl_context == FIELD
f376e137 9095 && funcdecl_p
8d08fdba
MS
9096 && (friendp == 0 || dname == current_class_name))
9097 ctype = current_class_type;
9098
51c184be 9099 if (ctype && return_type == return_conversion)
8d08fdba
MS
9100 TYPE_HAS_CONVERSION (ctype) = 1;
9101 if (ctype && constructor_name (ctype) == dname)
9102 {
9103 /* We are within a class's scope. If our declarator name
9104 is the same as the class name, and we are defining
9105 a function, then it is a constructor/destructor, and
9106 therefore returns a void type. */
9107
9108 if (flags == DTOR_FLAG)
9109 {
9110 /* ANSI C++ June 5 1992 WP 12.4.1. A destructor may
9111 not be declared const or volatile. A destructor
9112 may not be static. */
9113 if (staticp == 2)
9114 error ("destructor cannot be static member function");
b7484fbe 9115 if (quals)
8d08fdba 9116 {
b7484fbe 9117 error ("destructors cannot be declared `const' or `volatile'");
8d08fdba
MS
9118 return void_type_node;
9119 }
9120 if (decl_context == FIELD)
9121 {
9122 if (! member_function_or_else (ctype, current_class_type,
9123 "destructor for alien class `%s' cannot be a member"))
9124 return void_type_node;
9125 }
9126 }
2c73f9f5 9127 else /* It's a constructor. */
8d08fdba 9128 {
db5ae43f
MS
9129 if (explicitp == 1)
9130 explicitp = 2;
8d08fdba
MS
9131 /* ANSI C++ June 5 1992 WP 12.1.2. A constructor may
9132 not be declared const or volatile. A constructor may
9133 not be virtual. A constructor may not be static. */
9134 if (staticp == 2)
9135 error ("constructor cannot be static member function");
9136 if (virtualp)
9137 {
9138 pedwarn ("constructors cannot be declared virtual");
9139 virtualp = 0;
9140 }
b7484fbe 9141 if (quals)
8d08fdba 9142 {
b7484fbe 9143 error ("constructors cannot be declared `const' or `volatile'");
8d08fdba
MS
9144 return void_type_node;
9145 }
8d08fdba 9146 {
51c184be 9147 RID_BIT_TYPE tmp_bits;
fc378698 9148 bcopy ((void*)&specbits, (void*)&tmp_bits, sizeof (RID_BIT_TYPE));
51c184be
MS
9149 RIDBIT_RESET (RID_INLINE, tmp_bits);
9150 RIDBIT_RESET (RID_STATIC, tmp_bits);
9151 if (RIDBIT_ANY_SET (tmp_bits))
8d08fdba 9152 error ("return value type specifier for constructor ignored");
8d08fdba 9153 }
f30432d7 9154 type = build_pointer_type (ctype);
beb53fb8
JM
9155 if (decl_context == FIELD
9156 && IS_SIGNATURE (current_class_type))
8d08fdba
MS
9157 {
9158 error ("constructor not allowed in signature");
9159 return void_type_node;
9160 }
9161 else if (decl_context == FIELD)
9162 {
9163 if (! member_function_or_else (ctype, current_class_type,
9164 "constructor for alien class `%s' cannot be member"))
9165 return void_type_node;
9166 TYPE_HAS_CONSTRUCTOR (ctype) = 1;
9167 if (return_type != return_ctor)
9168 return NULL_TREE;
9169 }
9170 }
9171 if (decl_context == FIELD)
9172 staticp = 0;
9173 }
b7484fbe 9174 else if (friendp)
8d08fdba 9175 {
b7484fbe
MS
9176 if (initialized)
9177 error ("can't initialize friend function `%s'", name);
9178 if (virtualp)
9179 {
9180 /* Cannot be both friend and virtual. */
9181 error ("virtual functions cannot be friends");
9182 RIDBIT_RESET (RID_FRIEND, specbits);
9183 friendp = 0;
9184 }
28cbf42c
MS
9185 if (decl_context == NORMAL)
9186 error ("friend declaration not in class definition");
9187 if (current_function_decl && funcdef_flag)
9188 cp_error ("can't define friend function `%s' in a local class definition",
9189 name);
8d08fdba
MS
9190 }
9191
8d08fdba
MS
9192 /* Construct the function type and go to the next
9193 inner layer of declarator. */
9194
f376e137 9195 declarator = TREE_OPERAND (declarator, 0);
8d08fdba 9196
f376e137
MS
9197 /* FIXME: This is where default args should be fully
9198 processed. */
8d08fdba 9199
f376e137 9200 arg_types = grokparms (inner_parms, funcdecl_p ? funcdef_flag : 0);
8d08fdba
MS
9201
9202 if (declarator)
9203 {
9204 /* Get past destructors, etc.
9205 We know we have one because FLAGS will be non-zero.
9206
9207 Complain about improper parameter lists here. */
9208 if (TREE_CODE (declarator) == BIT_NOT_EXPR)
9209 {
9210 declarator = TREE_OPERAND (declarator, 0);
9211
9212 if (strict_prototype == 0 && arg_types == NULL_TREE)
9213 arg_types = void_list_node;
9214 else if (arg_types == NULL_TREE
9215 || arg_types != void_list_node)
9216 {
9217 error ("destructors cannot be specified with parameters");
9218 arg_types = void_list_node;
9219 }
9220 }
9221 }
9222
d22c8596 9223 /* ANSI says that `const int foo ();'
8d08fdba 9224 does not make the function foo const. */
d22c8596 9225 type = build_function_type (type, arg_types);
42976354
BK
9226
9227 {
9228 tree t;
9229 for (t = arg_types; t; t = TREE_CHAIN (t))
9230 if (TREE_PURPOSE (t)
9231 && TREE_CODE (TREE_PURPOSE (t)) == DEFAULT_ARG)
9232 {
9233 add_defarg_fn (type);
9234 break;
9235 }
9236 }
8d08fdba
MS
9237 }
9238 break;
9239
9240 case ADDR_EXPR:
9241 case INDIRECT_REF:
9242 /* Filter out pointers-to-references and references-to-references.
9243 We can get these if a TYPE_DECL is used. */
9244
9245 if (TREE_CODE (type) == REFERENCE_TYPE)
9246 {
9247 error ("cannot declare %s to references",
9248 TREE_CODE (declarator) == ADDR_EXPR
9249 ? "references" : "pointers");
9250 declarator = TREE_OPERAND (declarator, 0);
9251 continue;
9252 }
9253
a5894242
MS
9254 if (TREE_CODE (type) == OFFSET_TYPE
9255 && (TREE_CODE (TREE_TYPE (type)) == VOID_TYPE
9256 || TREE_CODE (TREE_TYPE (type)) == REFERENCE_TYPE))
9257 {
9258 cp_error ("cannot declare pointer to `%#T' member",
9259 TREE_TYPE (type));
9260 type = TREE_TYPE (type);
9261 }
9262
8d08fdba
MS
9263 /* Merge any constancy or volatility into the target type
9264 for the pointer. */
9265
9266 if (constp || volatilep)
9267 {
9268 /* A const or volatile signature pointer/reference is
9269 pointing to a const or volatile object, i.e., the
9270 `optr' is const or volatile, respectively, not the
9271 signature pointer/reference itself. */
9272 if (! IS_SIGNATURE (type))
9273 {
f376e137 9274 type = cp_build_type_variant (type, constp, volatilep);
8d08fdba
MS
9275 if (IS_AGGR_TYPE (type))
9276 build_pointer_type (type);
9277 constp = 0;
9278 volatilep = 0;
9279 }
9280 }
9281
9282 if (IS_SIGNATURE (type))
9283 {
9284 if (TREE_CODE (declarator) == ADDR_EXPR)
9285 {
63718c49
GB
9286 if (CLASSTYPE_METHOD_VEC (type) == NULL_TREE
9287 && TYPE_SIZE (type))
9288 cp_warning ("empty signature `%T' used in signature reference declaration",
9289 type);
8d08fdba
MS
9290#if 0
9291 type = build_signature_reference_type (type,
9292 constp, volatilep);
9293#else
9294 sorry ("signature reference");
9295 return NULL_TREE;
9296#endif
9297 }
9298 else
9299 {
63718c49
GB
9300 if (CLASSTYPE_METHOD_VEC (type) == NULL_TREE
9301 && TYPE_SIZE (type))
9302 cp_warning ("empty signature `%T' used in signature pointer declaration",
9303 type);
8d08fdba
MS
9304 type = build_signature_pointer_type (type,
9305 constp, volatilep);
9306 }
9307 constp = 0;
9308 volatilep = 0;
9309 }
9310 else if (TREE_CODE (declarator) == ADDR_EXPR)
9311 {
9312 if (TREE_CODE (type) == FUNCTION_TYPE)
9313 {
9314 error ("cannot declare references to functions; use pointer to function instead");
9315 type = build_pointer_type (type);
9316 }
9317 else
9318 {
a1774733 9319 if (TREE_CODE (type) == VOID_TYPE)
8d08fdba
MS
9320 error ("invalid type: `void &'");
9321 else
9322 type = build_reference_type (type);
9323 }
9324 }
9325 else if (TREE_CODE (type) == METHOD_TYPE)
9326 {
9327 type = build_ptrmemfunc_type (build_pointer_type (type));
9328 }
9329 else
9330 type = build_pointer_type (type);
9331
9332 /* Process a list of type modifier keywords (such as
9333 const or volatile) that were given inside the `*' or `&'. */
9334
9335 if (TREE_TYPE (declarator))
9336 {
9337 register tree typemodlist;
9338 int erred = 0;
9339 for (typemodlist = TREE_TYPE (declarator); typemodlist;
9340 typemodlist = TREE_CHAIN (typemodlist))
9341 {
9342 if (TREE_VALUE (typemodlist) == ridpointers[(int) RID_CONST])
9343 constp++;
9344 else if (TREE_VALUE (typemodlist) == ridpointers[(int) RID_VOLATILE])
9345 volatilep++;
9346 else if (!erred)
9347 {
9348 erred = 1;
9349 error ("invalid type modifier within %s declarator",
9350 TREE_CODE (declarator) == ADDR_EXPR
9351 ? "reference" : "pointer");
9352 }
9353 }
9354 if (constp > 1)
a0a33927 9355 pedwarn ("duplicate `const'");
8d08fdba 9356 if (volatilep > 1)
a0a33927
MS
9357 pedwarn ("duplicate `volatile'");
9358 if (TREE_CODE (declarator) == ADDR_EXPR
9359 && (constp || volatilep))
9360 {
9361 if (constp)
e1cd6e56 9362 pedwarn ("discarding `const' applied to a reference");
a0a33927 9363 if (volatilep)
e1cd6e56 9364 pedwarn ("discarding `volatile' applied to a reference");
a0a33927
MS
9365 constp = volatilep = 0;
9366 }
8d08fdba
MS
9367 }
9368 declarator = TREE_OPERAND (declarator, 0);
9369 ctype = NULL_TREE;
9370 break;
9371
9372 case SCOPE_REF:
9373 {
9374 /* We have converted type names to NULL_TREE if the
9375 name was bogus, or to a _TYPE node, if not.
9376
9377 The variable CTYPE holds the type we will ultimately
9378 resolve to. The code here just needs to build
9379 up appropriate member types. */
9380 tree sname = TREE_OPERAND (declarator, 1);
386b8a85
JM
9381 tree t;
9382
8d08fdba
MS
9383 /* Destructors can have their visibilities changed as well. */
9384 if (TREE_CODE (sname) == BIT_NOT_EXPR)
9385 sname = TREE_OPERAND (sname, 0);
9386
9387 if (TREE_COMPLEXITY (declarator) == 0)
9388 /* This needs to be here, in case we are called
9389 multiple times. */ ;
9390 else if (friendp && (TREE_COMPLEXITY (declarator) < 2))
2c73f9f5 9391 /* Don't fall out into global scope. Hides real bug? --eichin */ ;
5566b478
MS
9392 else if (! IS_AGGR_TYPE_CODE
9393 (TREE_CODE (TREE_OPERAND (declarator, 0))))
9394 ;
8d08fdba
MS
9395 else if (TREE_COMPLEXITY (declarator) == current_class_depth)
9396 {
9a3b49ac
MS
9397 /* Resolve any TYPENAME_TYPEs from the decl-specifier-seq
9398 that refer to ctype. They couldn't be resolved earlier
9399 because we hadn't pushed into the class yet.
9400 Example: resolve 'B<T>::type' in
9401 'B<typename B<T>::type> B<T>::f () { }'. */
9402 if (current_template_parms
9403 && uses_template_parms (type)
9404 && uses_template_parms (current_class_type))
9405 {
9406 tree args = current_template_args ();
f7d98d58 9407 type = tsubst (type, args, NULL_TREE);
9a3b49ac
MS
9408 }
9409
a28e3c7f
MS
9410 /* This pop_nested_class corresponds to the
9411 push_nested_class used to push into class scope for
9412 parsing the argument list of a function decl, in
9413 qualified_id. */
8d08fdba
MS
9414 pop_nested_class (1);
9415 TREE_COMPLEXITY (declarator) = current_class_depth;
9416 }
9417 else
9418 my_friendly_abort (16);
9419
9420 if (TREE_OPERAND (declarator, 0) == NULL_TREE)
9421 {
9422 /* We had a reference to a global decl, or
9423 perhaps we were given a non-aggregate typedef,
9424 in which case we cleared this out, and should just
9425 keep going as though it wasn't there. */
9426 declarator = sname;
9427 continue;
9428 }
9429 ctype = TREE_OPERAND (declarator, 0);
9430
386b8a85
JM
9431 t = ctype;
9432 while (t != NULL_TREE)
9433 {
75650646
MM
9434 if (CLASSTYPE_TEMPLATE_INFO (t) &&
9435 !CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
386b8a85
JM
9436 template_count += 1;
9437 t = TYPE_MAIN_DECL (t);
9438 if (DECL_LANG_SPECIFIC (t))
9439 t = DECL_CLASS_CONTEXT (t);
9440 else
9441 t = NULL_TREE;
9442 }
9443
8d08fdba
MS
9444 if (sname == NULL_TREE)
9445 goto done_scoping;
9446
9447 if (TREE_CODE (sname) == IDENTIFIER_NODE)
9448 {
9449 /* This is the `standard' use of the scoping operator:
9450 basetype :: member . */
9451
db5ae43f 9452 if (ctype == current_class_type)
28cbf42c
MS
9453 {
9454 /* class A {
9455 void A::f ();
9456 };
9457
9458 Is this ill-formed? */
9459
9460 if (pedantic)
9461 cp_pedwarn ("extra qualification `%T::' on member `%s' ignored",
9462 ctype, name);
9463 }
db5ae43f 9464 else if (TREE_CODE (type) == FUNCTION_TYPE)
8d08fdba
MS
9465 {
9466 if (current_class_type == NULL_TREE
8d08fdba
MS
9467 || friendp)
9468 type = build_cplus_method_type (build_type_variant (ctype, constp, volatilep),
9469 TREE_TYPE (type), TYPE_ARG_TYPES (type));
9470 else
9471 {
5b605f68 9472 cp_error ("cannot declare member function `%T::%s' within `%T'",
a3203465 9473 ctype, name, current_class_type);
8d08fdba
MS
9474 return void_type_node;
9475 }
9476 }
5566b478
MS
9477 else if (RIDBIT_SETP (RID_TYPEDEF, specbits)
9478 || TYPE_SIZE (complete_type (ctype)) != NULL_TREE)
8d08fdba 9479 {
2c73f9f5 9480 /* Have to move this code elsewhere in this function.
db5ae43f
MS
9481 this code is used for i.e., typedef int A::M; M *pm;
9482
9483 It is? How? jason 10/2/94 */
8d08fdba 9484
8d08fdba
MS
9485 if (current_class_type)
9486 {
db5ae43f
MS
9487 cp_error ("cannot declare member `%T::%s' within `%T'",
9488 ctype, name, current_class_type);
9489 return void_type_node;
8d08fdba
MS
9490 }
9491 type = build_offset_type (ctype, type);
9492 }
9493 else if (uses_template_parms (ctype))
9494 {
8d08fdba 9495 if (TREE_CODE (type) == FUNCTION_TYPE)
beb53fb8
JM
9496 type
9497 = build_cplus_method_type (build_type_variant (ctype,
9498 constp,
9499 volatilep),
9500 TREE_TYPE (type),
9501 TYPE_ARG_TYPES (type));
8d08fdba
MS
9502 }
9503 else
9504 {
9505 cp_error ("structure `%T' not yet defined", ctype);
9506 return error_mark_node;
9507 }
9508
9509 declarator = sname;
9510 }
8d08fdba
MS
9511 else if (TREE_CODE (sname) == SCOPE_REF)
9512 my_friendly_abort (17);
9513 else
9514 {
9515 done_scoping:
9516 declarator = TREE_OPERAND (declarator, 1);
9517 if (declarator && TREE_CODE (declarator) == CALL_EXPR)
9518 /* In this case, we will deal with it later. */
9519 ;
9520 else
9521 {
9522 if (TREE_CODE (type) == FUNCTION_TYPE)
9523 type = build_cplus_method_type (build_type_variant (ctype, constp, volatilep), TREE_TYPE (type), TYPE_ARG_TYPES (type));
9524 else
9525 type = build_offset_type (ctype, type);
9526 }
9527 }
9528 }
9529 break;
9530
9531 case BIT_NOT_EXPR:
9532 declarator = TREE_OPERAND (declarator, 0);
9533 break;
9534
8d08fdba
MS
9535 case RECORD_TYPE:
9536 case UNION_TYPE:
9537 case ENUMERAL_TYPE:
9538 declarator = NULL_TREE;
9539 break;
9540
9541 case ERROR_MARK:
9542 declarator = NULL_TREE;
9543 break;
9544
9545 default:
9546 my_friendly_abort (158);
9547 }
9548 }
9549
db5ae43f
MS
9550 if (explicitp == 1)
9551 {
9552 error ("only constructors can be declared `explicit'");
9553 explicitp = 0;
9554 }
9555
8d08fdba
MS
9556 /* Now TYPE has the actual type. */
9557
9558 /* If this is declaring a typedef name, return a TYPE_DECL. */
9559
f30432d7
MS
9560 if (RIDBIT_SETP (RID_MUTABLE, specbits))
9561 {
9562 if (constp)
9563 {
9564 error ("const `%s' cannot be declared `mutable'", name);
9565 RIDBIT_RESET (RID_MUTABLE, specbits);
9566 }
9567 else if (staticp)
9568 {
9569 error ("static `%s' cannot be declared `mutable'", name);
9570 RIDBIT_RESET (RID_MUTABLE, specbits);
9571 }
9572 }
9573
fc378698 9574 if (RIDBIT_SETP (RID_TYPEDEF, specbits) && decl_context != TYPENAME)
8d08fdba
MS
9575 {
9576 tree decl;
9577
9578 /* Note that the grammar rejects storage classes
9579 in typenames, fields or parameters. */
9580 if (constp || volatilep)
f376e137 9581 type = cp_build_type_variant (type, constp, volatilep);
8d08fdba 9582
d2e5ee5c
MS
9583 if (decl_context == FIELD)
9584 {
9585 if (declarator == current_class_name)
9586 cp_pedwarn ("ANSI C++ forbids nested type `%D' with same name as enclosing class",
9587 declarator);
9588 decl = build_lang_decl (TYPE_DECL, declarator, type);
9589 if (IS_SIGNATURE (current_class_type) && opaque_typedef)
9590 SIGNATURE_HAS_OPAQUE_TYPEDECLS (current_class_type) = 1;
9591 }
a1774733
BK
9592 else if (current_lang_name == lang_name_java)
9593 decl = build_lang_decl (TYPE_DECL, declarator, type);
d2e5ee5c
MS
9594 else
9595 decl = build_decl (TYPE_DECL, declarator, type);
9596
8d08fdba
MS
9597 /* If the user declares "struct {...} foo" then `foo' will have
9598 an anonymous name. Fill that name in now. Nothing can
9599 refer to it, so nothing needs know about the name change.
9600 The TYPE_NAME field was filled in by build_struct_xref. */
9601 if (type != error_mark_node
9602 && TYPE_NAME (type)
9603 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
9604 && ANON_AGGRNAME_P (TYPE_IDENTIFIER (type)))
9605 {
2c73f9f5 9606 /* Replace the anonymous name with the real name everywhere. */
8d08fdba 9607 lookup_tag_reverse (type, declarator);
d2e5ee5c 9608 TYPE_NAME (type) = decl;
8d08fdba
MS
9609
9610 if (TYPE_LANG_SPECIFIC (type))
9611 TYPE_WAS_ANONYMOUS (type) = 1;
9612
2c73f9f5
ML
9613 /* XXX Temporarily set the scope.
9614 When returning, start_decl expects it as NULL_TREE,
9615 and will then then set it using pushdecl. */
9616 my_friendly_assert (DECL_CONTEXT (decl) == NULL_TREE, 980404);
9617 if (current_class_type)
9618 DECL_CONTEXT (decl) = current_class_type;
9619 else
9620 DECL_CONTEXT (decl) = current_namespace;
9621
d2e5ee5c
MS
9622 DECL_ASSEMBLER_NAME (decl) = DECL_NAME (decl);
9623 DECL_ASSEMBLER_NAME (decl)
9624 = get_identifier (build_overload_name (type, 1, 1));
2c73f9f5 9625 DECL_CONTEXT (decl) = NULL_TREE;
fc378698 9626 }
fc378698 9627
8d08fdba
MS
9628 if (TREE_CODE (type) == OFFSET_TYPE || TREE_CODE (type) == METHOD_TYPE)
9629 {
9630 cp_error_at ("typedef name may not be class-qualified", decl);
a3203465 9631 return NULL_TREE;
8d08fdba
MS
9632 }
9633 else if (quals)
9634 {
9635 if (ctype == NULL_TREE)
9636 {
9637 if (TREE_CODE (type) != METHOD_TYPE)
9638 cp_error_at ("invalid type qualifier for non-method type", decl);
9639 else
9640 ctype = TYPE_METHOD_BASETYPE (type);
9641 }
9642 if (ctype != NULL_TREE)
9643 grok_method_quals (ctype, decl, quals);
9644 }
9645
9646 if (RIDBIT_SETP (RID_SIGNED, specbits)
9647 || (typedef_decl && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl)))
9648 C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
9649
9650 if (RIDBIT_SETP (RID_MUTABLE, specbits))
9651 {
9652 error ("non-object member `%s' cannot be declared mutable", name);
9653 }
9654
c91a56d2
MS
9655 bad_specifiers (decl, "type", virtualp, quals != NULL_TREE,
9656 inlinep, friendp, raises != NULL_TREE);
9657
9658 if (initialized)
9659 error ("typedef declaration includes an initializer");
9660
8d08fdba
MS
9661 return decl;
9662 }
9663
9664 /* Detect the case of an array type of unspecified size
9665 which came, as such, direct from a typedef name.
9666 We must copy the type, so that each identifier gets
9667 a distinct type, so that each identifier's size can be
9668 controlled separately by its own initializer. */
9669
9670 if (type == typedef_type && TREE_CODE (type) == ARRAY_TYPE
9671 && TYPE_DOMAIN (type) == NULL_TREE)
9672 {
9673 type = build_cplus_array_type (TREE_TYPE (type), TYPE_DOMAIN (type));
9674 }
9675
9676 /* If this is a type name (such as, in a cast or sizeof),
9677 compute the type and return it now. */
9678
9679 if (decl_context == TYPENAME)
9680 {
9681 /* Note that the grammar rejects storage classes
9682 in typenames, fields or parameters. */
9683 if (constp || volatilep)
a703fb38
KG
9684 {
9685 if (IS_SIGNATURE (type))
9686 error ("`const' or `volatile' specified with signature type");
9687 else
9688 type = cp_build_type_variant (type, constp, volatilep);
9689 }
8d08fdba
MS
9690
9691 /* Special case: "friend class foo" looks like a TYPENAME context. */
9692 if (friendp)
9693 {
b7484fbe
MS
9694 if (volatilep)
9695 {
9696 cp_error ("`volatile' specified for friend class declaration");
9697 volatilep = 0;
9698 }
9699 if (inlinep)
9700 {
9701 cp_error ("`inline' specified for friend class declaration");
9702 inlinep = 0;
9703 }
9704
9705 /* Only try to do this stuff if we didn't already give up. */
9706 if (type != integer_type_node)
9707 {
9708 /* A friendly class? */
9709 if (current_class_type)
9710 make_friend_class (current_class_type, TYPE_MAIN_VARIANT (type));
9711 else
9712 error ("trying to make class `%s' a friend of global scope",
9713 TYPE_NAME_STRING (type));
9714 type = void_type_node;
9715 }
8d08fdba
MS
9716 }
9717 else if (quals)
9718 {
8d08fdba 9719 tree dummy = build_decl (TYPE_DECL, declarator, type);
8d08fdba
MS
9720 if (ctype == NULL_TREE)
9721 {
9722 my_friendly_assert (TREE_CODE (type) == METHOD_TYPE, 159);
9723 ctype = TYPE_METHOD_BASETYPE (type);
9724 }
9725 grok_method_quals (ctype, dummy, quals);
9726 type = TREE_TYPE (dummy);
9727 }
9728
9729 return type;
9730 }
9731 else if (declarator == NULL_TREE && decl_context != PARM
db5ae43f 9732 && decl_context != CATCHPARM
8d08fdba
MS
9733 && TREE_CODE (type) != UNION_TYPE
9734 && ! bitfield)
9735 {
9736 cp_error ("abstract declarator `%T' used as declaration", type);
9737 declarator = make_anon_name ();
9738 }
9739
9740 /* `void' at top level (not within pointer)
9741 is allowed only in typedefs or type names.
9742 We don't complain about parms either, but that is because
9743 a better error message can be made later. */
9744
a1774733 9745 if (TREE_CODE (type) == VOID_TYPE && decl_context != PARM)
8d08fdba 9746 {
b7484fbe
MS
9747 if (! declarator)
9748 error ("unnamed variable or field declared void");
9749 else if (TREE_CODE (declarator) == IDENTIFIER_NODE)
8d08fdba
MS
9750 {
9751 if (IDENTIFIER_OPNAME_P (declarator))
8d08fdba 9752 my_friendly_abort (356);
8d08fdba
MS
9753 else
9754 error ("variable or field `%s' declared void", name);
9755 }
9756 else
9757 error ("variable or field declared void");
9758 type = integer_type_node;
9759 }
9760
9761 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
9762 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
9763
9764 {
9765 register tree decl;
9766
9767 if (decl_context == PARM)
9768 {
8d08fdba
MS
9769 if (ctype)
9770 error ("cannot use `::' in parameter declaration");
9771
9772 /* A parameter declared as an array of T is really a pointer to T.
9773 One declared as a function is really a pointer to a function.
39211cd5 9774 One declared as a member is really a pointer to member. */
8d08fdba
MS
9775
9776 if (TREE_CODE (type) == ARRAY_TYPE)
9777 {
e92cc029 9778 /* Transfer const-ness of array into that of type pointed to. */
39211cd5 9779 type = build_pointer_type
f376e137 9780 (cp_build_type_variant (TREE_TYPE (type), constp, volatilep));
39211cd5 9781 volatilep = constp = 0;
8d08fdba
MS
9782 }
9783 else if (TREE_CODE (type) == FUNCTION_TYPE)
9784 type = build_pointer_type (type);
9785 else if (TREE_CODE (type) == OFFSET_TYPE)
9786 type = build_pointer_type (type);
a1774733 9787 else if (TREE_CODE (type) == VOID_TYPE && declarator)
b7484fbe
MS
9788 {
9789 error ("declaration of `%s' as void", name);
9790 return NULL_TREE;
9791 }
8d08fdba 9792
e349ee73 9793 decl = build_decl (PARM_DECL, declarator, complete_type (type));
8d08fdba
MS
9794
9795 bad_specifiers (decl, "parameter", virtualp, quals != NULL_TREE,
9796 inlinep, friendp, raises != NULL_TREE);
9797 if (current_class_type
9798 && IS_SIGNATURE (current_class_type))
9799 {
9800 if (inlinep)
9801 error ("parameter of signature member function declared `inline'");
9802 if (RIDBIT_SETP (RID_AUTO, specbits))
9803 error ("parameter of signature member function declared `auto'");
9804 if (RIDBIT_SETP (RID_REGISTER, specbits))
9805 error ("parameter of signature member function declared `register'");
9806 }
9807
9808 /* Compute the type actually passed in the parmlist,
9809 for the case where there is no prototype.
9810 (For example, shorts and chars are passed as ints.)
9811 When there is a prototype, this is overridden later. */
9812
39211cd5 9813 DECL_ARG_TYPE (decl) = type_promotes_to (type);
8d08fdba
MS
9814 }
9815 else if (decl_context == FIELD)
9816 {
9817 if (type == error_mark_node)
9818 {
9819 /* Happens when declaring arrays of sizes which
9820 are error_mark_node, for example. */
9821 decl = NULL_TREE;
9822 }
9823 else if (TREE_CODE (type) == FUNCTION_TYPE)
9824 {
9825 int publicp = 0;
e1467ff2 9826 tree function_context;
8d08fdba 9827
72b7eeff
MS
9828 /* We catch the others as conflicts with the builtin
9829 typedefs. */
9830 if (friendp && declarator == ridpointers[(int) RID_SIGNED])
9831 {
9832 cp_error ("function `%D' cannot be declared friend",
9833 declarator);
9834 friendp = 0;
9835 }
9836
8d08fdba
MS
9837 if (friendp == 0)
9838 {
9839 if (ctype == NULL_TREE)
9840 ctype = current_class_type;
9841
9842 if (ctype == NULL_TREE)
9843 {
9844 cp_error ("can't make `%D' into a method -- not in a class",
9845 declarator);
9846 return void_type_node;
9847 }
9848
9849 /* ``A union may [ ... ] not [ have ] virtual functions.''
9850 ARM 9.5 */
9851 if (virtualp && TREE_CODE (ctype) == UNION_TYPE)
9852 {
9853 cp_error ("function `%D' declared virtual inside a union",
9854 declarator);
9855 return void_type_node;
9856 }
9857
9858 if (declarator == ansi_opname[(int) NEW_EXPR]
a28e3c7f
MS
9859 || declarator == ansi_opname[(int) VEC_NEW_EXPR]
9860 || declarator == ansi_opname[(int) DELETE_EXPR]
9861 || declarator == ansi_opname[(int) VEC_DELETE_EXPR])
8d08fdba
MS
9862 {
9863 if (virtualp)
9864 {
9865 cp_error ("`%D' cannot be declared virtual, since it is always static",
9866 declarator);
9867 virtualp = 0;
9868 }
9869 }
9870 else if (staticp < 2)
9871 type = build_cplus_method_type (build_type_variant (ctype, constp, volatilep),
9872 TREE_TYPE (type), TYPE_ARG_TYPES (type));
9873 }
9874
9875 /* Tell grokfndecl if it needs to set TREE_PUBLIC on the node. */
e1467ff2
MM
9876 function_context = (ctype != NULL_TREE) ?
9877 hack_decl_function_context (TYPE_MAIN_DECL (ctype)) : NULL_TREE;
9878 publicp = (! friendp || ! staticp)
9879 && function_context == NULL_TREE;
386b8a85
JM
9880 decl = grokfndecl (ctype, type,
9881 TREE_CODE (declarator) != TEMPLATE_ID_EXPR
9882 ? declarator : dname,
9883 declarator,
f30432d7 9884 virtualp, flags, quals, raises, attrlist,
386b8a85 9885 friendp ? -1 : 0, friendp, publicp, inlinep,
2c73f9f5 9886 funcdef_flag, template_count, in_namespace);
f0e01782
MS
9887 if (decl == NULL_TREE)
9888 return NULL_TREE;
6125f3be
DE
9889#if 0
9890 /* This clobbers the attrs stored in `decl' from `attrlist'. */
9891 /* The decl and setting of decl_machine_attr is also turned off. */
71851aaa 9892 decl = build_decl_attribute_variant (decl, decl_machine_attr);
6125f3be 9893#endif
f0e01782 9894
db5ae43f
MS
9895 if (explicitp == 2)
9896 DECL_NONCONVERTING_P (decl) = 1;
8d08fdba
MS
9897 }
9898 else if (TREE_CODE (type) == METHOD_TYPE)
9899 {
faae18ab
MS
9900 /* We only get here for friend declarations of
9901 members of other classes. */
8d08fdba
MS
9902 /* All method decls are public, so tell grokfndecl to set
9903 TREE_PUBLIC, also. */
386b8a85 9904 decl = grokfndecl (ctype, type, declarator, declarator,
f30432d7 9905 virtualp, flags, quals, raises, attrlist,
386b8a85 9906 friendp ? -1 : 0, friendp, 1, 0, funcdef_flag,
2c73f9f5 9907 template_count, in_namespace);
f0e01782
MS
9908 if (decl == NULL_TREE)
9909 return NULL_TREE;
8d08fdba 9910 }
5566b478
MS
9911 else if (!staticp && ! processing_template_decl
9912 && TYPE_SIZE (complete_type (type)) == NULL_TREE
8d08fdba
MS
9913 && (TREE_CODE (type) != ARRAY_TYPE || initialized == 0))
9914 {
b7484fbe
MS
9915 if (declarator)
9916 cp_error ("field `%D' has incomplete type", declarator);
9917 else
9918 cp_error ("name `%T' has incomplete type", type);
8d08fdba
MS
9919
9920 /* If we're instantiating a template, tell them which
9921 instantiation made the field's type be incomplete. */
9922 if (current_class_type
9923 && TYPE_NAME (current_class_type)
d2e5ee5c 9924 && IDENTIFIER_TEMPLATE (TYPE_IDENTIFIER (current_class_type))
8d08fdba
MS
9925 && declspecs && TREE_VALUE (declspecs)
9926 && TREE_TYPE (TREE_VALUE (declspecs)) == type)
db5ae43f
MS
9927 cp_error (" in instantiation of template `%T'",
9928 current_class_type);
9929
8d08fdba
MS
9930 type = error_mark_node;
9931 decl = NULL_TREE;
9932 }
9933 else
9934 {
9935 if (friendp)
9936 {
9937 error ("`%s' is neither function nor method; cannot be declared friend",
9938 IDENTIFIER_POINTER (declarator));
9939 friendp = 0;
9940 }
9941 decl = NULL_TREE;
9942 }
9943
9944 if (friendp)
9945 {
9946 /* Friends are treated specially. */
9947 if (ctype == current_class_type)
9948 warning ("member functions are implicitly friends of their class");
9949 else
9950 {
9951 tree t = NULL_TREE;
9952 if (decl && DECL_NAME (decl))
9953 t = do_friend (ctype, declarator, decl,
5566b478
MS
9954 last_function_parms, flags, quals,
9955 funcdef_flag);
8d08fdba
MS
9956 if (t && funcdef_flag)
9957 return t;
9958
9959 return void_type_node;
9960 }
9961 }
9962
9963 /* Structure field. It may not be a function, except for C++ */
9964
9965 if (decl == NULL_TREE)
9966 {
8d08fdba
MS
9967 if (initialized)
9968 {
3ac3d9ea
MM
9969 if (!staticp)
9970 {
9971 /* An attempt is being made to initialize a non-static
9972 member. But, from [class.mem]:
9973
9974 4 A member-declarator can contain a
9975 constant-initializer only if it declares a static
9976 member (_class.static_) of integral or enumeration
9977 type, see _class.static.data_.
9978
9979 This used to be relatively common practice, but
9980 the rest of the compiler does not correctly
9981 handle the initialization unless the member is
9982 static so we make it static below. */
9983 cp_pedwarn ("ANSI C++ forbids initialization of %s `%D'",
9984 constp ? "const member" : "member",
9985 declarator);
9986 cp_pedwarn ("making `%D' static", declarator);
9987 staticp = 1;
9988 }
9989
a0a33927
MS
9990 /* Motion 10 at San Diego: If a static const integral data
9991 member is initialized with an integral constant
9992 expression, the initializer may appear either in the
9993 declaration (within the class), or in the definition,
9994 but not both. If it appears in the class, the member is
9995 a member constant. The file-scope definition is always
9996 required. */
3ac3d9ea
MM
9997 if (! constp)
9998 /* According to Mike Stump, we generate bad code for
9999 this case, so we might as well always make it an
10000 error. */
10001 cp_error ("ANSI C++ forbids in-class initialization of non-const static member `%D'",
10002 declarator);
10003
10004 if (pedantic && ! INTEGRAL_TYPE_P (type))
10005 cp_pedwarn ("ANSI C++ forbids initialization of member constant `%D' of non-integral type `%T'", declarator, type);
8d08fdba
MS
10006 }
10007
3ac3d9ea 10008 if (staticp)
8d08fdba 10009 {
f30432d7 10010 /* ANSI C++ Apr '95 wp 9.2 */
3ac3d9ea 10011 if (declarator == current_class_name)
f30432d7
MS
10012 cp_pedwarn ("ANSI C++ forbids static member `%D' with same name as enclosing class",
10013 declarator);
10014
8d08fdba
MS
10015 /* C++ allows static class members.
10016 All other work for this is done by grokfield.
10017 This VAR_DECL is built by build_lang_field_decl.
10018 All other VAR_DECLs are built by build_decl. */
10019 decl = build_lang_field_decl (VAR_DECL, declarator, type);
5b605f68
MS
10020 TREE_STATIC (decl) = 1;
10021 /* In class context, 'static' means public access. */
3ac3d9ea 10022 TREE_PUBLIC (decl) = DECL_EXTERNAL (decl) = 1;
8d08fdba
MS
10023 }
10024 else
10025 {
10026 decl = build_lang_field_decl (FIELD_DECL, declarator, type);
10027 if (RIDBIT_SETP (RID_MUTABLE, specbits))
10028 {
10029 DECL_MUTABLE_P (decl) = 1;
10030 RIDBIT_RESET (RID_MUTABLE, specbits);
10031 }
10032 }
10033
10034 bad_specifiers (decl, "field", virtualp, quals != NULL_TREE,
10035 inlinep, friendp, raises != NULL_TREE);
10036 }
10037 }
10038 else if (TREE_CODE (type) == FUNCTION_TYPE || TREE_CODE (type) == METHOD_TYPE)
10039 {
386b8a85 10040 tree original_name;
8d08fdba
MS
10041 int publicp = 0;
10042
10043 if (! declarator)
10044 return NULL_TREE;
10045
386b8a85
JM
10046 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
10047 original_name = dname;
10048 else
10049 original_name = declarator;
10050
8926095f
MS
10051 if (RIDBIT_SETP (RID_AUTO, specbits))
10052 error ("storage class `auto' invalid for function `%s'", name);
10053 else if (RIDBIT_SETP (RID_REGISTER, specbits))
10054 error ("storage class `register' invalid for function `%s'", name);
8d08fdba
MS
10055
10056 /* Function declaration not at top level.
10057 Storage classes other than `extern' are not allowed
10058 and `extern' makes no difference. */
a9aedbc2 10059 if (! toplevel_bindings_p ()
8926095f
MS
10060 && (RIDBIT_SETP (RID_STATIC, specbits)
10061 || RIDBIT_SETP (RID_INLINE, specbits))
8d08fdba 10062 && pedantic)
8926095f
MS
10063 {
10064 if (RIDBIT_SETP (RID_STATIC, specbits))
10065 pedwarn ("storage class `static' invalid for function `%s' declared out of global scope", name);
10066 else
10067 pedwarn ("storage class `inline' invalid for function `%s' declared out of global scope", name);
10068 }
10069
8d08fdba
MS
10070 if (ctype == NULL_TREE)
10071 {
10072 if (virtualp)
10073 {
10074 error ("virtual non-class function `%s'", name);
10075 virtualp = 0;
10076 }
10077
10078 if (current_lang_name == lang_name_cplusplus
5156628f 10079 && ! processing_template_decl
8d08fdba
MS
10080 && ! (IDENTIFIER_LENGTH (original_name) == 4
10081 && IDENTIFIER_POINTER (original_name)[0] == 'm'
10082 && strcmp (IDENTIFIER_POINTER (original_name), "main") == 0)
10083 && ! (IDENTIFIER_LENGTH (original_name) > 10
10084 && IDENTIFIER_POINTER (original_name)[0] == '_'
10085 && IDENTIFIER_POINTER (original_name)[1] == '_'
10086 && strncmp (IDENTIFIER_POINTER (original_name)+2, "builtin_", 8) == 0))
8926095f 10087 /* Plain overloading: will not be grok'd by grokclassfn. */
386b8a85
JM
10088 if (name_mangling_version < 1
10089 || TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
10090 declarator = build_decl_overload (dname, TYPE_ARG_TYPES (type), 0);
8d08fdba
MS
10091 }
10092 else if (TREE_CODE (type) == FUNCTION_TYPE && staticp < 2)
10093 type = build_cplus_method_type (build_type_variant (ctype, constp, volatilep),
10094 TREE_TYPE (type), TYPE_ARG_TYPES (type));
10095
eb66be0e 10096 /* Record presence of `static'. */
faae18ab 10097 publicp = (ctype != NULL_TREE
a9aedbc2 10098 || RIDBIT_SETP (RID_EXTERN, specbits)
eb66be0e 10099 || !RIDBIT_SETP (RID_STATIC, specbits));
8d08fdba 10100
386b8a85 10101 decl = grokfndecl (ctype, type, original_name, declarator,
f30432d7 10102 virtualp, flags, quals, raises, attrlist,
75650646 10103 1, friendp,
386b8a85 10104 publicp, inlinep, funcdef_flag,
2c73f9f5 10105 template_count, in_namespace);
f0e01782
MS
10106 if (decl == NULL_TREE)
10107 return NULL_TREE;
8d08fdba 10108
386b8a85 10109 if (ctype == NULL_TREE && DECL_LANGUAGE (decl) != lang_c
71cc65bd 10110 && (! DECL_USE_TEMPLATE (decl) ||
386b8a85 10111 name_mangling_version < 1))
30394414 10112 DECL_ASSEMBLER_NAME (decl) = declarator;
386b8a85 10113
8d08fdba
MS
10114 if (staticp == 1)
10115 {
10116 int illegal_static = 0;
10117
10118 /* Don't allow a static member function in a class, and forbid
10119 declaring main to be static. */
10120 if (TREE_CODE (type) == METHOD_TYPE)
10121 {
00595019 10122 cp_pedwarn ("cannot declare member function `%D' to have static linkage", decl);
8d08fdba
MS
10123 illegal_static = 1;
10124 }
8d08fdba
MS
10125 else if (current_function_decl)
10126 {
10127 /* FIXME need arm citation */
10128 error ("cannot declare static function inside another function");
10129 illegal_static = 1;
10130 }
10131
10132 if (illegal_static)
10133 {
10134 staticp = 0;
10135 RIDBIT_RESET (RID_STATIC, specbits);
10136 }
10137 }
8d08fdba
MS
10138 }
10139 else
10140 {
10141 /* It's a variable. */
10142
db5ae43f
MS
10143 if (decl_context == CATCHPARM)
10144 {
10145 if (ctype)
10146 {
10147 ctype = NULL_TREE;
10148 error ("cannot use `::' in parameter declaration");
10149 }
10150
10151 /* A parameter declared as an array of T is really a pointer to T.
10152 One declared as a function is really a pointer to a function.
10153 One declared as a member is really a pointer to member. */
10154
10155 if (TREE_CODE (type) == ARRAY_TYPE)
10156 {
e92cc029
MS
10157 /* Transfer const-ness of array into that of type
10158 pointed to. */
db5ae43f
MS
10159 type = build_pointer_type
10160 (cp_build_type_variant (TREE_TYPE (type), constp, volatilep));
10161 volatilep = constp = 0;
10162 }
10163 else if (TREE_CODE (type) == FUNCTION_TYPE)
10164 type = build_pointer_type (type);
10165 else if (TREE_CODE (type) == OFFSET_TYPE)
10166 type = build_pointer_type (type);
10167 }
10168
8d08fdba 10169 /* An uninitialized decl with `extern' is a reference. */
2c73f9f5
ML
10170 decl = grokvardecl (type, declarator, &specbits,
10171 initialized, constp, in_namespace);
8d08fdba
MS
10172 bad_specifiers (decl, "variable", virtualp, quals != NULL_TREE,
10173 inlinep, friendp, raises != NULL_TREE);
10174
10175 if (ctype)
10176 {
f0e01782 10177 DECL_CONTEXT (decl) = ctype;
8d08fdba
MS
10178 if (staticp == 1)
10179 {
f30432d7 10180 cp_pedwarn ("static member `%D' re-declared as static", decl);
8d08fdba
MS
10181 staticp = 0;
10182 RIDBIT_RESET (RID_STATIC, specbits);
10183 }
b7484fbe
MS
10184 if (RIDBIT_SETP (RID_REGISTER, specbits) && TREE_STATIC (decl))
10185 {
10186 cp_error ("static member `%D' declared `register'", decl);
10187 RIDBIT_RESET (RID_REGISTER, specbits);
10188 }
f30432d7 10189 if (RIDBIT_SETP (RID_EXTERN, specbits) && pedantic)
8d08fdba 10190 {
f30432d7
MS
10191 cp_pedwarn ("cannot explicitly declare member `%#D' to have extern linkage",
10192 decl);
8d08fdba
MS
10193 RIDBIT_RESET (RID_EXTERN, specbits);
10194 }
10195 }
10196 }
10197
10198 if (RIDBIT_SETP (RID_MUTABLE, specbits))
10199 {
10200 error ("`%s' cannot be declared mutable", name);
10201 }
10202
10203 /* Record `register' declaration for warnings on &
10204 and in case doing stupid register allocation. */
10205
10206 if (RIDBIT_SETP (RID_REGISTER, specbits))
10207 DECL_REGISTER (decl) = 1;
10208
8926095f
MS
10209 if (RIDBIT_SETP (RID_EXTERN, specbits))
10210 DECL_THIS_EXTERN (decl) = 1;
10211
faae18ab
MS
10212 if (RIDBIT_SETP (RID_STATIC, specbits))
10213 DECL_THIS_STATIC (decl) = 1;
10214
8d08fdba
MS
10215 /* Record constancy and volatility. */
10216
10217 if (constp)
10218 TREE_READONLY (decl) = TREE_CODE (type) != REFERENCE_TYPE;
10219 if (volatilep)
10220 {
10221 TREE_SIDE_EFFECTS (decl) = 1;
10222 TREE_THIS_VOLATILE (decl) = 1;
10223 }
10224
10225 return decl;
10226 }
10227}
10228\f
10229/* Tell if a parmlist/exprlist looks like an exprlist or a parmlist.
10230 An empty exprlist is a parmlist. An exprlist which
10231 contains only identifiers at the global level
10232 is a parmlist. Otherwise, it is an exprlist. */
e92cc029 10233
8d08fdba
MS
10234int
10235parmlist_is_exprlist (exprs)
10236 tree exprs;
10237{
10238 if (exprs == NULL_TREE || TREE_PARMLIST (exprs))
10239 return 0;
10240
a9aedbc2 10241 if (toplevel_bindings_p ())
8d08fdba
MS
10242 {
10243 /* At the global level, if these are all identifiers,
10244 then it is a parmlist. */
10245 while (exprs)
10246 {
10247 if (TREE_CODE (TREE_VALUE (exprs)) != IDENTIFIER_NODE)
10248 return 1;
10249 exprs = TREE_CHAIN (exprs);
10250 }
10251 return 0;
10252 }
10253 return 1;
10254}
10255
10256/* Subroutine of `grokparms'. In a fcn definition, arg types must
10257 be complete.
10258
10259 C++: also subroutine of `start_function'. */
e92cc029 10260
8d08fdba
MS
10261static void
10262require_complete_types_for_parms (parms)
10263 tree parms;
10264{
5566b478
MS
10265 if (processing_template_decl)
10266 return;
10267
8d08fdba
MS
10268 while (parms)
10269 {
10270 tree type = TREE_TYPE (parms);
5566b478 10271 if (TYPE_SIZE (complete_type (type)) == NULL_TREE)
8d08fdba
MS
10272 {
10273 if (DECL_NAME (parms))
10274 error ("parameter `%s' has incomplete type",
10275 IDENTIFIER_POINTER (DECL_NAME (parms)));
10276 else
10277 error ("parameter has incomplete type");
10278 TREE_TYPE (parms) = error_mark_node;
10279 }
10280#if 0
10281 /* If the arg types are incomplete in a declaration,
10282 they must include undefined tags.
10283 These tags can never be defined in the scope of the declaration,
10284 so the types can never be completed,
10285 and no call can be compiled successfully. */
10286 /* This is not the right behavior for C++, but not having
10287 it is also probably wrong. */
10288 else
10289 {
10290 /* Now warn if is a pointer to an incomplete type. */
10291 while (TREE_CODE (type) == POINTER_TYPE
10292 || TREE_CODE (type) == REFERENCE_TYPE)
10293 type = TREE_TYPE (type);
10294 type = TYPE_MAIN_VARIANT (type);
10295 if (TYPE_SIZE (type) == NULL_TREE)
10296 {
10297 if (DECL_NAME (parm) != NULL_TREE)
10298 warning ("parameter `%s' points to incomplete type",
10299 IDENTIFIER_POINTER (DECL_NAME (parm)));
10300 else
10301 warning ("parameter points to incomplete type");
10302 }
10303 }
10304#endif
10305 parms = TREE_CHAIN (parms);
10306 }
10307}
10308
10309/* Decode the list of parameter types for a function type.
10310 Given the list of things declared inside the parens,
10311 return a list of types.
10312
10313 The list we receive can have three kinds of elements:
10314 an IDENTIFIER_NODE for names given without types,
10315 a TREE_LIST node for arguments given as typespecs or names with typespecs,
10316 or void_type_node, to mark the end of an argument list
10317 when additional arguments are not permitted (... was not used).
10318
10319 FUNCDEF_FLAG is nonzero for a function definition, 0 for
10320 a mere declaration. A nonempty identifier-list gets an error message
10321 when FUNCDEF_FLAG is zero.
10322 If FUNCDEF_FLAG is 1, then parameter types must be complete.
10323 If FUNCDEF_FLAG is -1, then parameter types may be incomplete.
10324
10325 If all elements of the input list contain types,
10326 we return a list of the types.
10327 If all elements contain no type (except perhaps a void_type_node
10328 at the end), we return a null list.
10329 If some have types and some do not, it is an error, and we
10330 return a null list.
10331
10332 Also set last_function_parms to either
10333 a list of names (IDENTIFIER_NODEs) or a chain of PARM_DECLs.
10334 A list of names is converted to a chain of PARM_DECLs
10335 by store_parm_decls so that ultimately it is always a chain of decls.
10336
10337 Note that in C++, parameters can take default values. These default
10338 values are in the TREE_PURPOSE field of the TREE_LIST. It is
10339 an error to specify default values which are followed by parameters
10340 that have no default values, or an ELLIPSES. For simplicities sake,
10341 only parameters which are specified with their types can take on
10342 default values. */
10343
10344static tree
10345grokparms (first_parm, funcdef_flag)
10346 tree first_parm;
10347 int funcdef_flag;
10348{
10349 tree result = NULL_TREE;
10350 tree decls = NULL_TREE;
10351
10352 if (first_parm != NULL_TREE
10353 && TREE_CODE (TREE_VALUE (first_parm)) == IDENTIFIER_NODE)
10354 {
10355 if (! funcdef_flag)
10356 pedwarn ("parameter names (without types) in function declaration");
10357 last_function_parms = first_parm;
10358 return NULL_TREE;
10359 }
10360 else if (first_parm != NULL_TREE
10361 && TREE_CODE (TREE_VALUE (first_parm)) != TREE_LIST
a1774733 10362 && TREE_CODE (TREE_VALUE (first_parm)) != VOID_TYPE)
8d08fdba
MS
10363 my_friendly_abort (145);
10364 else
10365 {
10366 /* Types were specified. This is a list of declarators
10367 each represented as a TREE_LIST node. */
10368 register tree parm, chain;
5566b478 10369 int any_init = 0, any_error = 0;
8d08fdba
MS
10370
10371 if (first_parm != NULL_TREE)
10372 {
10373 tree last_result = NULL_TREE;
10374 tree last_decl = NULL_TREE;
10375
10376 for (parm = first_parm; parm != NULL_TREE; parm = chain)
10377 {
a703fb38 10378 tree type = NULL_TREE, list_node = parm;
8d08fdba
MS
10379 register tree decl = TREE_VALUE (parm);
10380 tree init = TREE_PURPOSE (parm);
10381
10382 chain = TREE_CHAIN (parm);
10383 /* @@ weak defense against parse errors. */
a1774733
BK
10384 if (TREE_CODE (decl) != VOID_TYPE
10385 && TREE_CODE (decl) != TREE_LIST)
8d08fdba
MS
10386 {
10387 /* Give various messages as the need arises. */
10388 if (TREE_CODE (decl) == STRING_CST)
fc378698 10389 cp_error ("invalid string constant `%E'", decl);
8d08fdba
MS
10390 else if (TREE_CODE (decl) == INTEGER_CST)
10391 error ("invalid integer constant in parameter list, did you forget to give parameter name?");
10392 continue;
10393 }
10394
a1774733 10395 if (TREE_CODE (decl) != VOID_TYPE)
8d08fdba 10396 {
8d08fdba
MS
10397 decl = grokdeclarator (TREE_VALUE (decl),
10398 TREE_PURPOSE (decl),
f30432d7 10399 PARM, init != NULL_TREE,
c11b6f21 10400 NULL_TREE);
8d08fdba
MS
10401 if (! decl)
10402 continue;
10403 type = TREE_TYPE (decl);
a1774733 10404 if (TREE_CODE (type) == VOID_TYPE)
8d08fdba
MS
10405 decl = void_type_node;
10406 else if (TREE_CODE (type) == METHOD_TYPE)
10407 {
10408 if (DECL_NAME (decl))
fc378698 10409 /* Cannot use the decl here because
8d08fdba 10410 we don't have DECL_CONTEXT set up yet. */
fc378698
MS
10411 cp_error ("parameter `%D' invalidly declared method type",
10412 DECL_NAME (decl));
8d08fdba
MS
10413 else
10414 error ("parameter invalidly declared method type");
10415 type = build_pointer_type (type);
10416 TREE_TYPE (decl) = type;
10417 }
10418 else if (TREE_CODE (type) == OFFSET_TYPE)
10419 {
10420 if (DECL_NAME (decl))
fc378698
MS
10421 cp_error ("parameter `%D' invalidly declared offset type",
10422 DECL_NAME (decl));
8d08fdba
MS
10423 else
10424 error ("parameter invalidly declared offset type");
10425 type = build_pointer_type (type);
10426 TREE_TYPE (decl) = type;
10427 }
10428 else if (TREE_CODE (type) == RECORD_TYPE
10429 && TYPE_LANG_SPECIFIC (type)
10430 && CLASSTYPE_ABSTRACT_VIRTUALS (type))
10431 {
10432 abstract_virtuals_error (decl, type);
2c73f9f5 10433 any_error = 1; /* Seems like a good idea. */
8d08fdba
MS
10434 }
10435 else if (TREE_CODE (type) == RECORD_TYPE
10436 && TYPE_LANG_SPECIFIC (type)
10437 && IS_SIGNATURE (type))
10438 {
10439 signature_error (decl, type);
2c73f9f5 10440 any_error = 1; /* Seems like a good idea. */
8d08fdba
MS
10441 }
10442 }
10443
a1774733 10444 if (TREE_CODE (decl) == VOID_TYPE)
8d08fdba
MS
10445 {
10446 if (result == NULL_TREE)
10447 {
10448 result = void_list_node;
10449 last_result = result;
10450 }
10451 else
10452 {
10453 TREE_CHAIN (last_result) = void_list_node;
10454 last_result = void_list_node;
10455 }
8d08fdba
MS
10456 if (chain
10457 && (chain != void_list_node || TREE_CHAIN (chain)))
10458 error ("`void' in parameter list must be entire list");
10459 break;
10460 }
10461
10462 /* Since there is a prototype, args are passed in their own types. */
10463 DECL_ARG_TYPE (decl) = TREE_TYPE (decl);
10464#ifdef PROMOTE_PROTOTYPES
10465 if ((TREE_CODE (type) == INTEGER_TYPE
10466 || TREE_CODE (type) == ENUMERAL_TYPE)
10467 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
10468 DECL_ARG_TYPE (decl) = integer_type_node;
10469#endif
10470 if (!any_error)
10471 {
10472 if (init)
10473 {
10474 any_init++;
10475 if (TREE_CODE (init) == SAVE_EXPR)
10476 PARM_DECL_EXPR (init) = 1;
5156628f 10477 else if (processing_template_decl)
5566b478 10478 ;
42976354
BK
10479 /* Unparsed default arg from in-class decl. */
10480 else if (TREE_CODE (init) == DEFAULT_ARG)
10481 ;
b7484fbe
MS
10482 else if (TREE_CODE (init) == VAR_DECL
10483 || TREE_CODE (init) == PARM_DECL)
8d08fdba
MS
10484 {
10485 if (IDENTIFIER_LOCAL_VALUE (DECL_NAME (init)))
10486 {
10487 /* ``Local variables may not be used in default
10488 argument expressions.'' dpANSI C++ 8.2.6 */
10489 /* If extern int i; within a function is not
10490 considered a local variable, then this code is
e92cc029 10491 wrong. */
8d08fdba
MS
10492 cp_error ("local variable `%D' may not be used as a default argument", init);
10493 any_error = 1;
10494 }
10495 else if (TREE_READONLY_DECL_P (init))
10496 init = decl_constant_value (init);
10497 }
10498 else
10499 init = require_instantiated_type (type, init, integer_zero_node);
5156628f 10500 if (! processing_template_decl
42976354 10501 && TREE_CODE (init) != DEFAULT_ARG
c11b6f21 10502 && ! can_convert_arg (type, TREE_TYPE (init), init))
c73964b2
MS
10503 cp_pedwarn ("invalid type `%T' for default argument to `%#D'",
10504 TREE_TYPE (init), decl);
8d08fdba
MS
10505 }
10506 }
10507 else
10508 init = NULL_TREE;
10509
10510 if (decls == NULL_TREE)
10511 {
10512 decls = decl;
10513 last_decl = decls;
10514 }
10515 else
10516 {
10517 TREE_CHAIN (last_decl) = decl;
10518 last_decl = decl;
10519 }
66f2e88d 10520 if (! current_function_decl && TREE_PERMANENT (list_node))
8d08fdba
MS
10521 {
10522 TREE_PURPOSE (list_node) = init;
10523 TREE_VALUE (list_node) = type;
10524 TREE_CHAIN (list_node) = NULL_TREE;
10525 }
10526 else
10527 list_node = saveable_tree_cons (init, type, NULL_TREE);
10528 if (result == NULL_TREE)
10529 {
10530 result = list_node;
10531 last_result = result;
10532 }
10533 else
10534 {
10535 TREE_CHAIN (last_result) = list_node;
10536 last_result = list_node;
10537 }
10538 }
10539 if (last_result)
10540 TREE_CHAIN (last_result) = NULL_TREE;
10541 /* If there are no parameters, and the function does not end
10542 with `...', then last_decl will be NULL_TREE. */
10543 if (last_decl != NULL_TREE)
10544 TREE_CHAIN (last_decl) = NULL_TREE;
10545 }
10546 }
10547
10548 last_function_parms = decls;
10549
10550 /* In a fcn definition, arg types must be complete. */
10551 if (funcdef_flag > 0)
10552 require_complete_types_for_parms (last_function_parms);
10553
10554 return result;
10555}
42976354
BK
10556
10557/* Called from the parser to update an element of TYPE_ARG_TYPES for some
10558 FUNCTION_TYPE with the newly parsed version of its default argument, which
10559 was previously digested as text. See snarf_defarg et al in lex.c. */
10560
10561void
10562replace_defarg (arg, init)
10563 tree arg, init;
10564{
aa5f3bad 10565 if (! processing_template_decl
42976354
BK
10566 && ! can_convert_arg (TREE_VALUE (arg), TREE_TYPE (init), init))
10567 cp_pedwarn ("invalid type `%T' for default argument to `%T'",
10568 TREE_TYPE (init), TREE_VALUE (arg));
10569 TREE_PURPOSE (arg) = init;
10570}
8d08fdba 10571\f
c11b6f21
MS
10572int
10573copy_args_p (d)
10574 tree d;
10575{
10576 tree t = FUNCTION_ARG_CHAIN (d);
67437d5b
JM
10577 if (DECL_CONSTRUCTOR_P (d)
10578 && TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (d)))
10579 t = TREE_CHAIN (t);
c11b6f21
MS
10580 if (t && TREE_CODE (TREE_VALUE (t)) == REFERENCE_TYPE
10581 && (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_VALUE (t)))
10582 == DECL_CLASS_CONTEXT (d))
10583 && (TREE_CHAIN (t) == NULL_TREE
10584 || TREE_CHAIN (t) == void_list_node
10585 || TREE_PURPOSE (TREE_CHAIN (t))))
10586 return 1;
10587 return 0;
10588}
10589
8d08fdba
MS
10590/* These memoizing functions keep track of special properties which
10591 a class may have. `grok_ctor_properties' notices whether a class
10592 has a constructor of the form X(X&), and also complains
10593 if the class has a constructor of the form X(X).
10594 `grok_op_properties' takes notice of the various forms of
10595 operator= which are defined, as well as what sorts of type conversion
10596 may apply. Both functions take a FUNCTION_DECL as an argument. */
e92cc029 10597
a0a33927 10598int
8d08fdba
MS
10599grok_ctor_properties (ctype, decl)
10600 tree ctype, decl;
10601{
10602 tree parmtypes = FUNCTION_ARG_CHAIN (decl);
10603 tree parmtype = parmtypes ? TREE_VALUE (parmtypes) : void_type_node;
10604
10605 /* When a type has virtual baseclasses, a magical first int argument is
10606 added to any ctor so we can tell if the class has been initialized
10607 yet. This could screw things up in this function, so we deliberately
10608 ignore the leading int if we're in that situation. */
10609 if (parmtypes
10610 && TREE_VALUE (parmtypes) == integer_type_node
10611 && TYPE_USES_VIRTUAL_BASECLASSES (ctype))
10612 {
10613 parmtypes = TREE_CHAIN (parmtypes);
10614 parmtype = TREE_VALUE (parmtypes);
10615 }
10616
10617 if (TREE_CODE (parmtype) == REFERENCE_TYPE
0b41abe6
JM
10618 && TYPE_MAIN_VARIANT (TREE_TYPE (parmtype)) == ctype
10619 && (TREE_CHAIN (parmtypes) == NULL_TREE
8d08fdba 10620 || TREE_CHAIN (parmtypes) == void_list_node
0b41abe6
JM
10621 || TREE_PURPOSE (TREE_CHAIN (parmtypes))))
10622 {
10623 TYPE_HAS_INIT_REF (ctype) = 1;
10624 if (TYPE_READONLY (TREE_TYPE (parmtype)))
10625 TYPE_HAS_CONST_INIT_REF (ctype) = 1;
8d08fdba 10626 }
0b41abe6
JM
10627 else if (TYPE_MAIN_VARIANT (parmtype) == ctype
10628 && TREE_CHAIN (parmtypes) != NULL_TREE
10629 && TREE_CHAIN (parmtypes) == void_list_node)
8d08fdba 10630 {
0b41abe6
JM
10631 cp_error ("invalid constructor; you probably meant `%T (const %T&)'",
10632 ctype, ctype);
10633 SET_IDENTIFIER_ERROR_LOCUS (DECL_NAME (decl), ctype);
10634 return 0;
8d08fdba
MS
10635 }
10636 else if (TREE_CODE (parmtype) == VOID_TYPE
10637 || TREE_PURPOSE (parmtypes) != NULL_TREE)
10638 TYPE_HAS_DEFAULT_CONSTRUCTOR (ctype) = 1;
a0a33927
MS
10639
10640 return 1;
8d08fdba
MS
10641}
10642
10643/* An operator with this name can be either unary or binary. */
e92cc029 10644
a28e3c7f
MS
10645static int
10646ambi_op_p (name)
8d08fdba
MS
10647 tree name;
10648{
10649 return (name == ansi_opname [(int) INDIRECT_REF]
10650 || name == ansi_opname [(int) ADDR_EXPR]
10651 || name == ansi_opname [(int) NEGATE_EXPR]
10652 || name == ansi_opname[(int) POSTINCREMENT_EXPR]
10653 || name == ansi_opname[(int) POSTDECREMENT_EXPR]
10654 || name == ansi_opname [(int) CONVERT_EXPR]);
10655}
10656
10657/* An operator with this name can only be unary. */
e92cc029 10658
a28e3c7f
MS
10659static int
10660unary_op_p (name)
8d08fdba
MS
10661 tree name;
10662{
10663 return (name == ansi_opname [(int) TRUTH_NOT_EXPR]
10664 || name == ansi_opname [(int) BIT_NOT_EXPR]
10665 || name == ansi_opname [(int) COMPONENT_REF]
10666 || OPERATOR_TYPENAME_P (name));
10667}
10668
10669/* Do a little sanity-checking on how they declared their operator. */
e92cc029 10670
5566b478 10671void
8d08fdba
MS
10672grok_op_properties (decl, virtualp, friendp)
10673 tree decl;
10674 int virtualp, friendp;
10675{
10676 tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
10677 int methodp = (TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE);
10678 tree name = DECL_NAME (decl);
8d08fdba 10679
a28e3c7f
MS
10680 if (current_class_type == NULL_TREE)
10681 friendp = 1;
8d08fdba 10682
a28e3c7f
MS
10683 if (! friendp)
10684 {
10685 if (name == ansi_opname[(int) MODIFY_EXPR])
10686 TYPE_HAS_ASSIGNMENT (current_class_type) = 1;
10687 else if (name == ansi_opname[(int) CALL_EXPR])
10688 TYPE_OVERLOADS_CALL_EXPR (current_class_type) = 1;
10689 else if (name == ansi_opname[(int) ARRAY_REF])
10690 TYPE_OVERLOADS_ARRAY_REF (current_class_type) = 1;
10691 else if (name == ansi_opname[(int) COMPONENT_REF]
10692 || name == ansi_opname[(int) MEMBER_REF])
10693 TYPE_OVERLOADS_ARROW (current_class_type) = 1;
10694 else if (name == ansi_opname[(int) NEW_EXPR])
10695 TYPE_GETS_NEW (current_class_type) |= 1;
10696 else if (name == ansi_opname[(int) DELETE_EXPR])
10697 TYPE_GETS_DELETE (current_class_type) |= 1;
10698 else if (name == ansi_opname[(int) VEC_NEW_EXPR])
10699 TYPE_GETS_NEW (current_class_type) |= 2;
10700 else if (name == ansi_opname[(int) VEC_DELETE_EXPR])
10701 TYPE_GETS_DELETE (current_class_type) |= 2;
10702 }
10703
10704 if (name == ansi_opname[(int) NEW_EXPR]
10705 || name == ansi_opname[(int) VEC_NEW_EXPR])
8d08fdba 10706 {
8d08fdba
MS
10707 /* When the compiler encounters the definition of A::operator new, it
10708 doesn't look at the class declaration to find out if it's static. */
a28e3c7f 10709 if (methodp)
700f8a87 10710 revert_static_member_fn (&decl, NULL, NULL);
8d08fdba
MS
10711
10712 /* Take care of function decl if we had syntax errors. */
10713 if (argtypes == NULL_TREE)
beb53fb8
JM
10714 TREE_TYPE (decl)
10715 = build_function_type (ptr_type_node,
10716 hash_tree_chain (integer_type_node,
10717 void_list_node));
8d08fdba 10718 else
a28e3c7f 10719 TREE_TYPE (decl) = coerce_new_type (TREE_TYPE (decl));
8d08fdba 10720 }
a28e3c7f
MS
10721 else if (name == ansi_opname[(int) DELETE_EXPR]
10722 || name == ansi_opname[(int) VEC_DELETE_EXPR])
8d08fdba 10723 {
a28e3c7f 10724 if (methodp)
700f8a87 10725 revert_static_member_fn (&decl, NULL, NULL);
8d08fdba
MS
10726
10727 if (argtypes == NULL_TREE)
beb53fb8
JM
10728 TREE_TYPE (decl)
10729 = build_function_type (void_type_node,
10730 hash_tree_chain (ptr_type_node,
10731 void_list_node));
8d08fdba 10732 else
a28e3c7f
MS
10733 {
10734 TREE_TYPE (decl) = coerce_delete_type (TREE_TYPE (decl));
10735
10736 if (! friendp && name == ansi_opname[(int) VEC_DELETE_EXPR]
10737 && (TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
10738 != void_list_node))
10739 TYPE_VEC_DELETE_TAKES_SIZE (current_class_type) = 1;
10740 }
8d08fdba 10741 }
8d08fdba
MS
10742 else
10743 {
10744 /* An operator function must either be a non-static member function
10745 or have at least one parameter of a class, a reference to a class,
10746 an enumeration, or a reference to an enumeration. 13.4.0.6 */
700f8a87 10747 if (! methodp || DECL_STATIC_FUNCTION_P (decl))
8d08fdba
MS
10748 {
10749 if (OPERATOR_TYPENAME_P (name)
10750 || name == ansi_opname[(int) CALL_EXPR]
10751 || name == ansi_opname[(int) MODIFY_EXPR]
10752 || name == ansi_opname[(int) COMPONENT_REF]
10753 || name == ansi_opname[(int) ARRAY_REF])
10754 cp_error ("`%D' must be a nonstatic member function", decl);
10755 else
10756 {
10757 tree p = argtypes;
10758
700f8a87
MS
10759 if (DECL_STATIC_FUNCTION_P (decl))
10760 cp_error ("`%D' must be either a non-static member function or a non-member function", decl);
10761
8d08fdba 10762 if (p)
a1774733 10763 for (; TREE_CODE (TREE_VALUE (p)) != VOID_TYPE ; p = TREE_CHAIN (p))
8d08fdba
MS
10764 {
10765 tree arg = TREE_VALUE (p);
10766 if (TREE_CODE (arg) == REFERENCE_TYPE)
10767 arg = TREE_TYPE (arg);
10768
10769 /* This lets bad template code slip through. */
10770 if (IS_AGGR_TYPE (arg)
10771 || TREE_CODE (arg) == ENUMERAL_TYPE
73b0fce8
KL
10772 || TREE_CODE (arg) == TEMPLATE_TYPE_PARM
10773 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
8d08fdba
MS
10774 goto foundaggr;
10775 }
10776 cp_error
10777 ("`%D' must have an argument of class or enumerated type",
10778 decl);
10779 foundaggr:
10780 ;
10781 }
10782 }
10783
277294d7 10784 if (name == ansi_opname[(int) CALL_EXPR])
2c73f9f5 10785 return; /* No restrictions on args. */
8d08fdba 10786
9a3b49ac 10787 if (IDENTIFIER_TYPENAME_P (name) && ! DECL_TEMPLATE_INFO (decl))
a0a33927
MS
10788 {
10789 tree t = TREE_TYPE (name);
10790 if (TREE_CODE (t) == VOID_TYPE)
10791 pedwarn ("void is not a valid type conversion operator");
10792 else if (! friendp)
10793 {
10794 int ref = (TREE_CODE (t) == REFERENCE_TYPE);
10795 char *what = 0;
10796 if (ref)
10797 t = TYPE_MAIN_VARIANT (TREE_TYPE (t));
10798
10799 if (t == current_class_type)
10800 what = "the same type";
9a3b49ac 10801 /* Don't force t to be complete here. */
a0a33927 10802 else if (IS_AGGR_TYPE (t)
9a3b49ac 10803 && TYPE_SIZE (t)
a0a33927
MS
10804 && DERIVED_FROM_P (t, current_class_type))
10805 what = "a base class";
10806
10807 if (what)
10808 warning ("conversion to %s%s will never use a type conversion operator",
10809 ref ? "a reference to " : "", what);
10810 }
10811 }
10812
8d08fdba
MS
10813 if (name == ansi_opname[(int) MODIFY_EXPR])
10814 {
10815 tree parmtype;
10816
10817 if (list_length (argtypes) != 3 && methodp)
10818 {
10819 cp_error ("`%D' must take exactly one argument", decl);
10820 return;
10821 }
10822 parmtype = TREE_VALUE (TREE_CHAIN (argtypes));
10823
f0e01782 10824 if (copy_assignment_arg_p (parmtype, virtualp)
a28e3c7f 10825 && ! friendp)
8d08fdba
MS
10826 {
10827 TYPE_HAS_ASSIGN_REF (current_class_type) = 1;
f0e01782
MS
10828 if (TREE_CODE (parmtype) != REFERENCE_TYPE
10829 || TYPE_READONLY (TREE_TYPE (parmtype)))
8d08fdba
MS
10830 TYPE_HAS_CONST_ASSIGN_REF (current_class_type) = 1;
10831 }
10832 }
10833 else if (name == ansi_opname[(int) COND_EXPR])
10834 {
10835 /* 13.4.0.3 */
10836 pedwarn ("ANSI C++ prohibits overloading operator ?:");
10837 if (list_length (argtypes) != 4)
10838 cp_error ("`%D' must take exactly three arguments", decl);
10839 }
10840 else if (ambi_op_p (name))
10841 {
10842 if (list_length (argtypes) == 2)
10843 /* prefix */;
10844 else if (list_length (argtypes) == 3)
10845 {
10846 if ((name == ansi_opname[(int) POSTINCREMENT_EXPR]
10847 || name == ansi_opname[(int) POSTDECREMENT_EXPR])
5156628f 10848 && ! processing_template_decl
8d08fdba
MS
10849 && TREE_VALUE (TREE_CHAIN (argtypes)) != integer_type_node)
10850 {
10851 if (methodp)
10852 cp_error ("postfix `%D' must take `int' as its argument",
10853 decl);
10854 else
10855 cp_error
10856 ("postfix `%D' must take `int' as its second argument",
10857 decl);
10858 }
10859 }
10860 else
10861 {
10862 if (methodp)
10863 cp_error ("`%D' must take either zero or one argument", decl);
10864 else
10865 cp_error ("`%D' must take either one or two arguments", decl);
10866 }
824b9a4c
MS
10867
10868 /* More Effective C++ rule 6. */
eb448459 10869 if (warn_ecpp
824b9a4c
MS
10870 && (name == ansi_opname[(int) POSTINCREMENT_EXPR]
10871 || name == ansi_opname[(int) POSTDECREMENT_EXPR]))
10872 {
10873 tree arg = TREE_VALUE (argtypes);
10874 tree ret = TREE_TYPE (TREE_TYPE (decl));
10875 if (methodp || TREE_CODE (arg) == REFERENCE_TYPE)
10876 arg = TREE_TYPE (arg);
10877 arg = TYPE_MAIN_VARIANT (arg);
10878 if (list_length (argtypes) == 2)
10879 {
10880 if (TREE_CODE (ret) != REFERENCE_TYPE
10881 || !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (ret)),
10882 arg, 1))
10883 cp_warning ("prefix `%D' should return `%T'", decl,
10884 build_reference_type (arg));
10885 }
10886 else
10887 {
10888 if (!comptypes (TYPE_MAIN_VARIANT (ret), arg, 1))
10889 cp_warning ("postfix `%D' should return `%T'", decl, arg);
10890 }
10891 }
8d08fdba
MS
10892 }
10893 else if (unary_op_p (name))
10894 {
10895 if (list_length (argtypes) != 2)
10896 {
10897 if (methodp)
10898 cp_error ("`%D' must take `void'", decl);
10899 else
10900 cp_error ("`%D' must take exactly one argument", decl);
10901 }
10902 }
10903 else /* if (binary_op_p (name)) */
10904 {
10905 if (list_length (argtypes) != 3)
10906 {
10907 if (methodp)
10908 cp_error ("`%D' must take exactly one argument", decl);
10909 else
10910 cp_error ("`%D' must take exactly two arguments", decl);
10911 }
824b9a4c
MS
10912
10913 /* More Effective C++ rule 7. */
eb448459 10914 if (warn_ecpp
824b9a4c
MS
10915 && (name == ansi_opname [TRUTH_ANDIF_EXPR]
10916 || name == ansi_opname [TRUTH_ORIF_EXPR]
10917 || name == ansi_opname [COMPOUND_EXPR]))
10918 cp_warning ("user-defined `%D' always evaluates both arguments",
10919 decl);
10920 }
10921
10922 /* Effective C++ rule 23. */
eb448459 10923 if (warn_ecpp
824b9a4c
MS
10924 && list_length (argtypes) == 3
10925 && (name == ansi_opname [PLUS_EXPR]
10926 || name == ansi_opname [MINUS_EXPR]
10927 || name == ansi_opname [TRUNC_DIV_EXPR]
10928 || name == ansi_opname [MULT_EXPR])
10929 && TREE_CODE (TREE_TYPE (TREE_TYPE (decl))) == REFERENCE_TYPE)
10930 cp_warning ("`%D' should return by value", decl);
8d08fdba
MS
10931
10932 /* 13.4.0.8 */
10933 if (argtypes)
10934 for (; argtypes != void_list_node ; argtypes = TREE_CHAIN (argtypes))
10935 if (TREE_PURPOSE (argtypes))
10936 {
10937 TREE_PURPOSE (argtypes) = NULL_TREE;
10938 if (name == ansi_opname[(int) POSTINCREMENT_EXPR]
10939 || name == ansi_opname[(int) POSTDECREMENT_EXPR])
10940 {
10941 if (pedantic)
10942 cp_pedwarn ("`%D' cannot have default arguments", decl);
10943 }
10944 else
10945 cp_error ("`%D' cannot have default arguments", decl);
10946 }
10947 }
10948}
10949\f
10950/* Get the struct, enum or union (CODE says which) with tag NAME.
10951 Define the tag as a forward-reference if it is not defined.
10952
10953 C++: If a class derivation is given, process it here, and report
10954 an error if multiple derivation declarations are not identical.
10955
10956 If this is a definition, come in through xref_tag and only look in
10957 the current frame for the name (since C++ allows new names in any
10958 scope.) */
10959
8d08fdba
MS
10960tree
10961xref_tag (code_type_node, name, binfo, globalize)
10962 tree code_type_node;
10963 tree name, binfo;
10964 int globalize;
10965{
10966 enum tag_types tag_code;
10967 enum tree_code code;
10968 int temp = 0;
8d08fdba
MS
10969 register tree ref, t;
10970 struct binding_level *b = inner_binding_level;
a80e4195 10971 int got_type = 0;
8d08fdba
MS
10972
10973 tag_code = (enum tag_types) TREE_INT_CST_LOW (code_type_node);
10974 switch (tag_code)
10975 {
10976 case record_type:
10977 case class_type:
8d08fdba
MS
10978 case signature_type:
10979 code = RECORD_TYPE;
8d08fdba
MS
10980 break;
10981 case union_type:
10982 code = UNION_TYPE;
8d08fdba
MS
10983 break;
10984 case enum_type:
10985 code = ENUMERAL_TYPE;
10986 break;
10987 default:
10988 my_friendly_abort (18);
10989 }
10990
10991 /* If a cross reference is requested, look up the type
10992 already defined for this tag and return it. */
be99da77
MS
10993 if (TREE_CODE_CLASS (TREE_CODE (name)) == 't')
10994 {
10995 t = name;
a80e4195
MS
10996 name = TYPE_IDENTIFIER (t);
10997 got_type = 1;
be99da77
MS
10998 }
10999 else
11000 t = IDENTIFIER_TYPE_VALUE (name);
73b0fce8
KL
11001 if (t && TREE_CODE (t) != code && TREE_CODE (t) != TEMPLATE_TYPE_PARM
11002 && TREE_CODE (t) != TEMPLATE_TEMPLATE_PARM)
a0a33927
MS
11003 t = NULL_TREE;
11004
8ccc31eb 11005 if (! globalize)
8d08fdba 11006 {
73b0fce8
KL
11007 if (pedantic && t && (TREE_CODE (t) == TEMPLATE_TYPE_PARM
11008 || TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM))
ec255269
MS
11009 {
11010 cp_pedwarn ("redeclaration of template type-parameter `%T'", name);
11011 cp_pedwarn_at (" previously declared here", t);
11012 }
a80e4195 11013 if (t && TYPE_CONTEXT (t) && got_type)
8ccc31eb 11014 ref = t;
8d08fdba 11015 else
e92cc029
MS
11016 {
11017 /* If we know we are defining this tag, only look it up in
11018 this scope and don't try to find it as a type. */
11019 ref = lookup_tag (code, name, b, 1);
11020 }
8d08fdba
MS
11021 }
11022 else
11023 {
11024 if (t)
11025 ref = t;
11026 else
11027 ref = lookup_tag (code, name, b, 0);
11028
11029 if (! ref)
11030 {
11031 /* Try finding it as a type declaration. If that wins, use it. */
11032 ref = lookup_name (name, 1);
6757edfe
MM
11033
11034 if (ref != NULL_TREE
11035 && processing_template_decl
11036 && DECL_CLASS_TEMPLATE_P (ref)
11037 && template_class_depth (current_class_type) == 0)
11038 /* Since GLOBALIZE is true, we're declaring a global
11039 template, so we want this type. */
11040 ref = DECL_RESULT (ref);
11041
8d08fdba
MS
11042 if (ref && TREE_CODE (ref) == TYPE_DECL
11043 && TREE_CODE (TREE_TYPE (ref)) == code)
11044 ref = TREE_TYPE (ref);
11045 else
11046 ref = NULL_TREE;
11047 }
11048 }
11049
11050 push_obstacks_nochange ();
11051
11052 if (! ref)
11053 {
11054 /* If no such tag is yet defined, create a forward-reference node
11055 and record it as the "definition".
11056 When a real declaration of this type is found,
11057 the forward-reference will be altered into a real type. */
11058
11059 /* In C++, since these migrate into the global scope, we must
11060 build them on the permanent obstack. */
11061
11062 temp = allocation_temporary_p ();
11063 if (temp)
11064 end_temporary_allocation ();
11065
11066 if (code == ENUMERAL_TYPE)
11067 {
fc378698
MS
11068 cp_error ("use of enum `%#D' without previous declaration", name);
11069
8d08fdba
MS
11070 ref = make_node (ENUMERAL_TYPE);
11071
11072 /* Give the type a default layout like unsigned int
11073 to avoid crashing if it does not get defined. */
11074 TYPE_MODE (ref) = TYPE_MODE (unsigned_type_node);
11075 TYPE_ALIGN (ref) = TYPE_ALIGN (unsigned_type_node);
11076 TREE_UNSIGNED (ref) = 1;
11077 TYPE_PRECISION (ref) = TYPE_PRECISION (unsigned_type_node);
11078 TYPE_MIN_VALUE (ref) = TYPE_MIN_VALUE (unsigned_type_node);
11079 TYPE_MAX_VALUE (ref) = TYPE_MAX_VALUE (unsigned_type_node);
11080
11081 /* Enable us to recognize when a type is created in class context.
11082 To do nested classes correctly, this should probably be cleared
11083 out when we leave this classes scope. Currently this in only
11084 done in `start_enum'. */
11085
11086 pushtag (name, ref, globalize);
8d08fdba 11087 }
8d08fdba
MS
11088 else
11089 {
8d08fdba 11090 struct binding_level *old_b = class_binding_level;
8d08fdba
MS
11091
11092 ref = make_lang_type (code);
11093
8d08fdba
MS
11094 if (tag_code == signature_type)
11095 {
11096 SET_SIGNATURE (ref);
f376e137
MS
11097 /* Since a signature type will be turned into the type
11098 of signature tables, it's not only an interface. */
8d08fdba 11099 CLASSTYPE_INTERFACE_ONLY (ref) = 0;
f376e137
MS
11100 SET_CLASSTYPE_INTERFACE_KNOWN (ref);
11101 /* A signature doesn't have a vtable. */
00595019 11102 CLASSTYPE_VTABLE_NEEDS_WRITING (ref) = 0;
8d08fdba
MS
11103 }
11104
8d08fdba
MS
11105#ifdef NONNESTED_CLASSES
11106 /* Class types don't nest the way enums do. */
11107 class_binding_level = (struct binding_level *)0;
11108#endif
11109 pushtag (name, ref, globalize);
11110 class_binding_level = old_b;
8d08fdba
MS
11111 }
11112 }
11113 else
11114 {
8d08fdba 11115 /* If it no longer looks like a nested type, make sure it's
30394414
JM
11116 in global scope.
11117 If it is not an IDENTIFIER, this is not a declaration */
2c73f9f5 11118 if (b->namespace_p && !class_binding_level
30394414
JM
11119 && TREE_CODE (name) == IDENTIFIER_NODE)
11120 {
2c73f9f5
ML
11121 if (IDENTIFIER_NAMESPACE_VALUE (name) == NULL_TREE)
11122 SET_IDENTIFIER_NAMESPACE_VALUE (name, TYPE_NAME (ref));
30394414 11123 }
7fe6899f
MM
11124
11125 if (!globalize && processing_template_decl && IS_AGGR_TYPE (ref))
6757edfe 11126 redeclare_class_template (ref, current_template_parms);
8d08fdba
MS
11127 }
11128
11129 if (binfo)
8ccc31eb 11130 xref_basetypes (code_type_node, name, ref, binfo);
8d08fdba 11131
8d08fdba
MS
11132 /* Until the type is defined, tentatively accept whatever
11133 structure tag the user hands us. */
11134 if (TYPE_SIZE (ref) == NULL_TREE
11135 && ref != current_class_type
11136 /* Have to check this, in case we have contradictory tag info. */
11137 && IS_AGGR_TYPE_CODE (TREE_CODE (ref)))
11138 {
11139 if (tag_code == class_type)
11140 CLASSTYPE_DECLARED_CLASS (ref) = 1;
11141 else if (tag_code == record_type || tag_code == signature_type)
11142 CLASSTYPE_DECLARED_CLASS (ref) = 0;
11143 }
11144
11145 pop_obstacks ();
11146
11147 return ref;
11148}
8ccc31eb 11149
fc378698
MS
11150tree
11151xref_tag_from_type (old, id, globalize)
11152 tree old, id;
11153 int globalize;
11154{
11155 tree code_type_node;
11156
11157 if (TREE_CODE (old) == RECORD_TYPE)
11158 code_type_node = (CLASSTYPE_DECLARED_CLASS (old)
11159 ? class_type_node : record_type_node);
11160 else
11161 code_type_node = union_type_node;
11162
11163 if (id == NULL_TREE)
11164 id = TYPE_IDENTIFIER (old);
11165
11166 return xref_tag (code_type_node, id, NULL_TREE, globalize);
11167}
11168
8ccc31eb
MS
11169void
11170xref_basetypes (code_type_node, name, ref, binfo)
11171 tree code_type_node;
11172 tree name, ref;
11173 tree binfo;
11174{
11175 /* In the declaration `A : X, Y, ... Z' we mark all the types
11176 (A, X, Y, ..., Z) so we can check for duplicates. */
11177 tree binfos;
11178 int i, len;
11179 enum tag_types tag_code = (enum tag_types) TREE_INT_CST_LOW (code_type_node);
11180
11181 if (tag_code == union_type)
11182 {
11183 cp_error ("derived union `%T' invalid", ref);
11184 return;
11185 }
11186
11187 len = list_length (binfo);
11188 push_obstacks (TYPE_OBSTACK (ref), TYPE_OBSTACK (ref));
11189
11190 SET_CLASSTYPE_MARKED (ref);
11191 BINFO_BASETYPES (TYPE_BINFO (ref)) = binfos = make_tree_vec (len);
11192
11193 for (i = 0; binfo; binfo = TREE_CHAIN (binfo))
11194 {
11195 /* The base of a derived struct is public by default. */
11196 int via_public
be99da77
MS
11197 = (TREE_PURPOSE (binfo) == access_public_node
11198 || TREE_PURPOSE (binfo) == access_public_virtual_node
8ccc31eb 11199 || (tag_code != class_type
be99da77
MS
11200 && (TREE_PURPOSE (binfo) == access_default_node
11201 || TREE_PURPOSE (binfo) == access_default_virtual_node)));
d8b55a76
JM
11202 int via_protected
11203 = (TREE_PURPOSE (binfo) == access_protected_node
11204 || TREE_PURPOSE (binfo) == access_protected_virtual_node);
8ccc31eb 11205 int via_virtual
be99da77 11206 = (TREE_PURPOSE (binfo) == access_private_virtual_node
d8b55a76 11207 || TREE_PURPOSE (binfo) == access_protected_virtual_node
be99da77
MS
11208 || TREE_PURPOSE (binfo) == access_public_virtual_node
11209 || TREE_PURPOSE (binfo) == access_default_virtual_node);
11210 tree basetype = TREE_VALUE (binfo);
8ccc31eb
MS
11211 tree base_binfo;
11212
11213 GNU_xref_hier (IDENTIFIER_POINTER (name),
11214 IDENTIFIER_POINTER (TREE_VALUE (binfo)),
11215 via_public, via_virtual, 0);
11216
11217 if (basetype && TREE_CODE (basetype) == TYPE_DECL)
11218 basetype = TREE_TYPE (basetype);
5566b478
MS
11219 if (!basetype
11220 || (TREE_CODE (basetype) != RECORD_TYPE
a80e4195 11221 && TREE_CODE (basetype) != TYPENAME_TYPE
73b0fce8
KL
11222 && TREE_CODE (basetype) != TEMPLATE_TYPE_PARM
11223 && TREE_CODE (basetype) != TEMPLATE_TEMPLATE_PARM))
8ccc31eb
MS
11224 {
11225 cp_error ("base type `%T' fails to be a struct or class type",
11226 TREE_VALUE (binfo));
11227 continue;
11228 }
11229#if 1
8adf5b5e
JM
11230 /* This code replaces similar code in layout_basetypes.
11231 We put the complete_type first for implicit `typename'. */
11232 else if (TYPE_SIZE (complete_type (basetype)) == NULL_TREE
11233 && ! (current_template_parms && uses_template_parms (basetype)))
8ccc31eb
MS
11234 {
11235 cp_error ("base class `%T' has incomplete type", basetype);
11236 continue;
11237 }
11238#endif
11239 else
11240 {
11241 if (CLASSTYPE_MARKED (basetype))
11242 {
11243 if (basetype == ref)
11244 cp_error ("recursive type `%T' undefined", basetype);
11245 else
11246 cp_error ("duplicate base type `%T' invalid", basetype);
11247 continue;
11248 }
11249
11250 /* Note that the BINFO records which describe individual
11251 inheritances are *not* shared in the lattice! They
11252 cannot be shared because a given baseclass may be
11253 inherited with different `accessibility' by different
11254 derived classes. (Each BINFO record describing an
11255 individual inheritance contains flags which say what
11256 the `accessibility' of that particular inheritance is.) */
11257
11258 base_binfo = make_binfo (integer_zero_node, basetype,
11259 TYPE_BINFO_VTABLE (basetype),
11260 TYPE_BINFO_VIRTUALS (basetype), NULL_TREE);
11261
11262 TREE_VEC_ELT (binfos, i) = base_binfo;
11263 TREE_VIA_PUBLIC (base_binfo) = via_public;
11264 TREE_VIA_PROTECTED (base_binfo) = via_protected;
11265 TREE_VIA_VIRTUAL (base_binfo) = via_virtual;
11266 BINFO_INHERITANCE_CHAIN (base_binfo) = TYPE_BINFO (ref);
11267
11268 SET_CLASSTYPE_MARKED (basetype);
9e9ff709 11269
8ccc31eb
MS
11270 /* We are free to modify these bits because they are meaningless
11271 at top level, and BASETYPE is a top-level type. */
11272 if (via_virtual || TYPE_USES_VIRTUAL_BASECLASSES (basetype))
11273 {
11274 TYPE_USES_VIRTUAL_BASECLASSES (ref) = 1;
11275 TYPE_USES_COMPLEX_INHERITANCE (ref) = 1;
11276 }
11277
8ccc31eb
MS
11278 TYPE_GETS_NEW (ref) |= TYPE_GETS_NEW (basetype);
11279 TYPE_GETS_DELETE (ref) |= TYPE_GETS_DELETE (basetype);
11280 CLASSTYPE_LOCAL_TYPEDECLS (ref) |= CLASSTYPE_LOCAL_TYPEDECLS (basetype);
11281 i += 1;
11282 }
11283 }
11284 if (i)
11285 TREE_VEC_LENGTH (binfos) = i;
11286 else
11287 BINFO_BASETYPES (TYPE_BINFO (ref)) = NULL_TREE;
11288
11289 if (i > 1)
11290 TYPE_USES_MULTIPLE_INHERITANCE (ref) = 1;
11291 else if (i == 1)
11292 TYPE_USES_MULTIPLE_INHERITANCE (ref)
11293 = TYPE_USES_MULTIPLE_INHERITANCE (BINFO_TYPE (TREE_VEC_ELT (binfos, 0)));
11294 if (TYPE_USES_MULTIPLE_INHERITANCE (ref))
11295 TYPE_USES_COMPLEX_INHERITANCE (ref) = 1;
11296
11297 /* Unmark all the types. */
11298 while (--i >= 0)
11299 CLEAR_CLASSTYPE_MARKED (BINFO_TYPE (TREE_VEC_ELT (binfos, i)));
11300 CLEAR_CLASSTYPE_MARKED (ref);
11301
11302 pop_obstacks ();
11303}
11304
8d08fdba 11305\f
b3d5a58b 11306tree current_local_enum = NULL_TREE;
8d08fdba
MS
11307
11308/* Begin compiling the definition of an enumeration type.
11309 NAME is its name (or null if anonymous).
11310 Returns the type object, as yet incomplete.
11311 Also records info about it so that build_enumerator
11312 may be used to declare the individual values as they are read. */
11313
11314tree
11315start_enum (name)
11316 tree name;
11317{
11318 register tree enumtype = NULL_TREE;
11319 struct binding_level *b = inner_binding_level;
11320
b87692e5
MS
11321 if (processing_template_decl && current_function_decl)
11322 end_temporary_allocation ();
11323
8d08fdba
MS
11324 /* If this is the real definition for a previous forward reference,
11325 fill in the contents in the same object that used to be the
11326 forward reference. */
11327
11328 if (name != NULL_TREE)
11329 enumtype = lookup_tag (ENUMERAL_TYPE, name, b, 1);
11330
11331 if (enumtype != NULL_TREE && TREE_CODE (enumtype) == ENUMERAL_TYPE)
c91a56d2 11332 cp_error ("multiple definition of `%#T'", enumtype);
8d08fdba
MS
11333 else
11334 {
11335 enumtype = make_node (ENUMERAL_TYPE);
11336 pushtag (name, enumtype, 0);
11337 }
11338
c91a56d2
MS
11339 if (b->pseudo_global)
11340 cp_error ("template declaration of `%#T'", enumtype);
11341
8d08fdba
MS
11342 if (current_class_type)
11343 TREE_ADDRESSABLE (b->tags) = 1;
c91a56d2 11344
8d08fdba
MS
11345 current_local_enum = NULL_TREE;
11346
e92cc029
MS
11347 /* We don't copy this value because build_enumerator needs to do it. */
11348 enum_next_value = integer_zero_node;
7177d104 11349 enum_overflow = 0;
8d08fdba
MS
11350
11351 GNU_xref_decl (current_function_decl, enumtype);
11352 return enumtype;
11353}
11354
11355/* After processing and defining all the values of an enumeration type,
11356 install their decls in the enumeration type and finish it off.
11357 ENUMTYPE is the type object and VALUES a list of name-value pairs.
11358 Returns ENUMTYPE. */
11359
11360tree
11361finish_enum (enumtype, values)
11362 register tree enumtype, values;
11363{
a703fb38 11364 register tree minnode = NULL_TREE, maxnode = NULL_TREE;
8d08fdba
MS
11365 /* Calculate the maximum value of any enumerator in this type. */
11366
11367 if (values)
11368 {
f376e137
MS
11369 register tree pair;
11370 register tree value = DECL_INITIAL (TREE_VALUE (values));
5566b478 11371
5156628f 11372 if (! processing_template_decl)
5566b478
MS
11373 {
11374 /* Speed up the main loop by performing some precalculations */
11375 TREE_TYPE (TREE_VALUE (values)) = enumtype;
11376 TREE_TYPE (value) = enumtype;
11377 minnode = maxnode = value;
11378 }
f376e137 11379 TREE_VALUE (values) = value;
8d08fdba
MS
11380
11381 for (pair = TREE_CHAIN (values); pair; pair = TREE_CHAIN (pair))
11382 {
f376e137 11383 value = DECL_INITIAL (TREE_VALUE (pair));
5156628f 11384 if (! processing_template_decl)
5566b478
MS
11385 {
11386 TREE_TYPE (TREE_VALUE (pair)) = enumtype;
11387 TREE_TYPE (value) = enumtype;
11388 if (tree_int_cst_lt (maxnode, value))
11389 maxnode = value;
11390 else if (tree_int_cst_lt (value, minnode))
11391 minnode = value;
11392 }
f376e137 11393 TREE_VALUE (pair) = value;
8d08fdba
MS
11394 }
11395 }
f376e137
MS
11396 else
11397 maxnode = minnode = integer_zero_node;
8d08fdba 11398
de22184b 11399 TYPE_VALUES (enumtype) = nreverse (values);
2986ae00 11400
5156628f 11401 if (processing_template_decl)
b87692e5
MS
11402 {
11403 if (current_function_decl)
11404 {
11405 add_tree (build_min (TAG_DEFN, enumtype));
11406 resume_temporary_allocation ();
11407 }
11408 return enumtype;
11409 }
5566b478 11410
f376e137
MS
11411 {
11412 int unsignedp = tree_int_cst_sgn (minnode) >= 0;
11413 int lowprec = min_precision (minnode, unsignedp);
11414 int highprec = min_precision (maxnode, unsignedp);
11415 int precision = MAX (lowprec, highprec);
8d08fdba 11416
6060a796 11417 TYPE_SIZE (enumtype) = NULL_TREE;
13fbb038 11418
8ccc31eb 11419 /* Set TYPE_MIN_VALUE and TYPE_MAX_VALUE according to `precision'. */
8d08fdba 11420
f376e137 11421 TYPE_PRECISION (enumtype) = precision;
f376e137 11422 if (unsignedp)
8d08fdba 11423 fixup_unsigned_type (enumtype);
f376e137
MS
11424 else
11425 fixup_signed_type (enumtype);
8ccc31eb 11426
5566b478 11427 if (flag_short_enums || (precision > TYPE_PRECISION (integer_type_node)))
8ccc31eb
MS
11428 /* Use the width of the narrowest normal C type which is wide enough. */
11429 TYPE_PRECISION (enumtype) = TYPE_PRECISION (type_for_size
11430 (precision, 1));
11431 else
11432 TYPE_PRECISION (enumtype) = TYPE_PRECISION (integer_type_node);
11433
11434 TYPE_SIZE (enumtype) = 0;
11435 layout_type (enumtype);
f376e137 11436 }
8d08fdba 11437
f376e137
MS
11438 {
11439 register tree tem;
11440
11441 /* Fix up all variant types of this enum type. */
11442 for (tem = TYPE_MAIN_VARIANT (enumtype); tem;
11443 tem = TYPE_NEXT_VARIANT (tem))
11444 {
11445 TYPE_VALUES (tem) = TYPE_VALUES (enumtype);
11446 TYPE_MIN_VALUE (tem) = TYPE_MIN_VALUE (enumtype);
11447 TYPE_MAX_VALUE (tem) = TYPE_MAX_VALUE (enumtype);
11448 TYPE_SIZE (tem) = TYPE_SIZE (enumtype);
11449 TYPE_MODE (tem) = TYPE_MODE (enumtype);
11450 TYPE_PRECISION (tem) = TYPE_PRECISION (enumtype);
11451 TYPE_ALIGN (tem) = TYPE_ALIGN (enumtype);
11452 TREE_UNSIGNED (tem) = TREE_UNSIGNED (enumtype);
11453 }
11454 }
8d08fdba
MS
11455
11456 /* Finish debugging output for this type. */
2c73f9f5 11457 rest_of_type_compilation (enumtype, namespace_bindings_p ());
8d08fdba
MS
11458
11459 return enumtype;
11460}
11461
11462/* Build and install a CONST_DECL for one value of the
11463 current enumeration type (one that was begun with start_enum).
11464 Return a tree-list containing the name and its value.
11465 Assignment of sequential values by default is handled here. */
11466
11467tree
11468build_enumerator (name, value)
11469 tree name, value;
11470{
11471 tree decl, result;
8d08fdba
MS
11472
11473 /* Remove no-op casts from the value. */
11474 if (value)
11475 STRIP_TYPE_NOPS (value);
11476
5156628f 11477 if (! processing_template_decl)
5566b478
MS
11478 {
11479 /* Validate and default VALUE. */
11480 if (value != NULL_TREE)
11481 {
11482 if (TREE_READONLY_DECL_P (value))
e92cc029 11483 value = decl_constant_value (value);
5566b478
MS
11484
11485 if (TREE_CODE (value) == INTEGER_CST)
11486 {
11487 value = default_conversion (value);
11488 constant_expression_warning (value);
11489 }
11490 else
11491 {
11492 cp_error ("enumerator value for `%D' not integer constant", name);
11493 value = NULL_TREE;
11494 }
11495 }
11496
5566b478 11497 /* Default based on previous value. */
5156628f 11498 if (value == NULL_TREE && ! processing_template_decl)
5566b478
MS
11499 {
11500 value = enum_next_value;
11501 if (enum_overflow)
11502 cp_error ("overflow in enumeration values at `%D'", name);
11503 }
11504
11505 /* Remove no-op casts from the value. */
11506 if (value)
11507 STRIP_TYPE_NOPS (value);
11508
e92cc029
MS
11509 /* We have to always copy here; not all INTEGER_CSTs are unshared,
11510 and there's no wedding ring. Look at size_int()...*/
11511 value = copy_node (value);
013bc8af
MS
11512#if 0
11513 /* To fix MAX_VAL enum consts. (bkoz) */
e92cc029 11514 TREE_TYPE (value) = integer_type_node;
013bc8af 11515#endif
5566b478 11516 }
8d08fdba 11517
8d08fdba
MS
11518 /* C++ associates enums with global, function, or class declarations. */
11519
7177d104
MS
11520 decl = current_scope ();
11521 if (decl && decl == current_class_type)
8d08fdba
MS
11522 {
11523 /* This enum declaration is local to the class, so we must put
11524 it in that class's list of decls. */
11525 decl = build_lang_field_decl (CONST_DECL, name, integer_type_node);
11526 DECL_INITIAL (decl) = value;
11527 TREE_READONLY (decl) = 1;
11528 pushdecl_class_level (decl);
11529 TREE_CHAIN (decl) = current_local_enum;
11530 current_local_enum = decl;
11531 }
11532 else
11533 {
11534 /* It's a global enum, or it's local to a function. (Note local to
11535 a function could mean local to a class method. */
11536 decl = build_decl (CONST_DECL, name, integer_type_node);
11537 DECL_INITIAL (decl) = value;
42976354 11538 TREE_READONLY (decl) = 1;
8d08fdba
MS
11539
11540 pushdecl (decl);
11541 GNU_xref_decl (current_function_decl, decl);
11542 }
11543
5156628f 11544 if (! processing_template_decl)
5566b478
MS
11545 {
11546 /* Set basis for default for next value. */
11547 enum_next_value = build_binary_op_nodefault (PLUS_EXPR, value,
11548 integer_one_node, PLUS_EXPR);
11549 enum_overflow = tree_int_cst_lt (enum_next_value, value);
5566b478 11550 }
8d08fdba 11551
8d2733ca 11552 result = saveable_tree_cons (name, decl, NULL_TREE);
8d08fdba
MS
11553 return result;
11554}
11555
11556tree
86052cc3
JM
11557grok_enum_decls (decl)
11558 tree decl;
8d08fdba
MS
11559{
11560 tree d = current_local_enum;
11561
11562 if (d == NULL_TREE)
11563 return decl;
11564
11565 while (1)
11566 {
8d08fdba
MS
11567 if (TREE_CHAIN (d) == NULL_TREE)
11568 {
11569 TREE_CHAIN (d) = decl;
11570 break;
11571 }
11572 d = TREE_CHAIN (d);
11573 }
11574
11575 decl = current_local_enum;
11576 current_local_enum = NULL_TREE;
11577
11578 return decl;
11579}
11580\f
5566b478
MS
11581static int function_depth;
11582
8d08fdba
MS
11583/* Create the FUNCTION_DECL for a function definition.
11584 DECLSPECS and DECLARATOR are the parts of the declaration;
11585 they describe the function's name and the type it returns,
11586 but twisted together in a fashion that parallels the syntax of C.
11587
11588 This function creates a binding context for the function body
11589 as well as setting up the FUNCTION_DECL in current_function_decl.
11590
11591 Returns 1 on success. If the DECLARATOR is not suitable for a function
11592 (it defines a datum instead), we return 0, which tells
11593 yyparse to report a parse error.
11594
11595 For C++, we must first check whether that datum makes any sense.
11596 For example, "class A local_a(1,2);" means that variable local_a
11597 is an aggregate of type A, which should have a constructor
11598 applied to it with the argument list [1, 2].
11599
11600 @@ There is currently no way to retrieve the storage
11601 @@ allocated to FUNCTION (or all of its parms) if we return
11602 @@ something we had previously. */
11603
11604int
c11b6f21
MS
11605start_function (declspecs, declarator, attrs, pre_parsed_p)
11606 tree declspecs, declarator, attrs;
8d08fdba
MS
11607 int pre_parsed_p;
11608{
5566b478 11609 tree decl1;
8d08fdba
MS
11610 tree ctype = NULL_TREE;
11611 tree fntype;
11612 tree restype;
11613 extern int have_extern_spec;
11614 extern int used_extern_spec;
11615 int doing_friend = 0;
11616
8d08fdba 11617 /* Sanity check. */
a1774733 11618 my_friendly_assert (TREE_CODE (TREE_VALUE (void_list_node)) == VOID_TYPE, 160);
8d08fdba
MS
11619 my_friendly_assert (TREE_CHAIN (void_list_node) == NULL_TREE, 161);
11620
e92cc029 11621 /* Assume, until we see it does. */
8d08fdba
MS
11622 current_function_returns_value = 0;
11623 current_function_returns_null = 0;
11624 warn_about_return_type = 0;
fd378c9d
JM
11625 named_labels = 0;
11626 shadowed_labels = 0;
8d08fdba
MS
11627 current_function_assigns_this = 0;
11628 current_function_just_assigned_this = 0;
11629 current_function_parms_stored = 0;
11630 original_result_rtx = NULL_RTX;
a9aedbc2 11631 base_init_expr = NULL_TREE;
79ff2c6c
MS
11632 current_base_init_list = NULL_TREE;
11633 current_member_init_list = NULL_TREE;
f30432d7 11634 ctor_label = dtor_label = NULL_TREE;
42976354 11635 static_labelno = 0;
8d08fdba
MS
11636
11637 clear_temp_name ();
11638
e92cc029 11639 /* This should only be done once on the top most decl. */
8d08fdba
MS
11640 if (have_extern_spec && !used_extern_spec)
11641 {
11642 declspecs = decl_tree_cons (NULL_TREE, get_identifier ("extern"), declspecs);
11643 used_extern_spec = 1;
11644 }
11645
11646 if (pre_parsed_p)
11647 {
11648 decl1 = declarator;
11649
11650 if (! DECL_ARGUMENTS (decl1)
11651 && !DECL_STATIC_FUNCTION_P (decl1)
6eb3bb27 11652 && DECL_CLASS_SCOPE_P (decl1)
d2e5ee5c
MS
11653 && TYPE_IDENTIFIER (DECL_CONTEXT (decl1))
11654 && IDENTIFIER_TEMPLATE (TYPE_IDENTIFIER (DECL_CONTEXT (decl1))))
8d08fdba 11655 {
30394414
JM
11656 tree binding = binding_for_name (DECL_NAME (decl1),
11657 current_namespace);
8d08fdba
MS
11658 cp_error ("redeclaration of `%#D'", decl1);
11659 if (IDENTIFIER_CLASS_VALUE (DECL_NAME (decl1)))
11660 cp_error_at ("previous declaration here", IDENTIFIER_CLASS_VALUE (DECL_NAME (decl1)));
30394414
JM
11661 else if (BINDING_VALUE (binding))
11662 cp_error_at ("previous declaration here", BINDING_VALUE (binding));
8d08fdba
MS
11663 }
11664
8d08fdba
MS
11665 fntype = TREE_TYPE (decl1);
11666 if (TREE_CODE (fntype) == METHOD_TYPE)
11667 ctype = TYPE_METHOD_BASETYPE (fntype);
11668
11669 /* ANSI C++ June 5 1992 WP 11.4.5. A friend function defined in a
11670 class is in the (lexical) scope of the class in which it is
11671 defined. */
11672 if (!ctype && DECL_FRIEND_P (decl1))
11673 {
11674 ctype = DECL_CLASS_CONTEXT (decl1);
11675
11676 /* CTYPE could be null here if we're dealing with a template;
11677 for example, `inline friend float foo()' inside a template
11678 will have no CTYPE set. */
11679 if (ctype && TREE_CODE (ctype) != RECORD_TYPE)
11680 ctype = NULL_TREE;
11681 else
11682 doing_friend = 1;
11683 }
11684
8d08fdba 11685 /* In a fcn definition, arg types must be complete. */
5566b478 11686 require_complete_types_for_parms (DECL_ARGUMENTS (decl1));
9fffd093
RK
11687
11688 /* In case some arg types were completed since the declaration was
11689 parsed, fix up the decls. */
11690 {
5566b478 11691 tree t = DECL_ARGUMENTS (decl1);
9fffd093
RK
11692 for (; t; t = TREE_CHAIN (t))
11693 layout_decl (t, 0);
11694 }
5566b478
MS
11695
11696 last_function_parms = DECL_ARGUMENTS (decl1);
11697 last_function_parm_tags = NULL_TREE;
8d08fdba
MS
11698 }
11699 else
11700 {
c11b6f21 11701 decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, 1, NULL_TREE);
8d08fdba
MS
11702 /* If the declarator is not suitable for a function definition,
11703 cause a syntax error. */
11704 if (decl1 == NULL_TREE || TREE_CODE (decl1) != FUNCTION_DECL) return 0;
11705
11706 fntype = TREE_TYPE (decl1);
11707
11708 restype = TREE_TYPE (fntype);
11709 if (IS_AGGR_TYPE (restype) && ! TYPE_PTRMEMFUNC_P (restype)
11710 && ! CLASSTYPE_GOT_SEMICOLON (restype))
11711 {
11712 cp_error ("semicolon missing after declaration of `%#T'", restype);
e66d884e 11713 shadow_tag (build_expr_list (NULL_TREE, restype));
8d08fdba
MS
11714 CLASSTYPE_GOT_SEMICOLON (restype) = 1;
11715 if (TREE_CODE (fntype) == FUNCTION_TYPE)
11716 fntype = build_function_type (integer_type_node,
11717 TYPE_ARG_TYPES (fntype));
11718 else
11719 fntype = build_cplus_method_type (build_type_variant (TYPE_METHOD_BASETYPE (fntype), TREE_READONLY (decl1), TREE_SIDE_EFFECTS (decl1)),
11720 integer_type_node,
11721 TYPE_ARG_TYPES (fntype));
11722 TREE_TYPE (decl1) = fntype;
11723 }
11724
11725 if (TREE_CODE (fntype) == METHOD_TYPE)
11726 ctype = TYPE_METHOD_BASETYPE (fntype);
11727 else if (IDENTIFIER_LENGTH (DECL_NAME (decl1)) == 4
11728 && ! strcmp (IDENTIFIER_POINTER (DECL_NAME (decl1)), "main")
11729 && DECL_CONTEXT (decl1) == NULL_TREE)
11730 {
11731 /* If this doesn't return integer_type, complain. */
11732 if (TREE_TYPE (TREE_TYPE (decl1)) != integer_type_node)
11733 {
a28e3c7f 11734 if (pedantic || warn_return_type)
5566b478 11735 pedwarn ("return type for `main' changed to `int'");
8d08fdba
MS
11736 TREE_TYPE (decl1) = fntype = default_function_type;
11737 }
11738 warn_about_return_type = 0;
11739 }
11740 }
11741
11742 /* Warn if function was previously implicitly declared
11743 (but not if we warned then). */
11744 if (! warn_implicit
11745 && IDENTIFIER_IMPLICIT_DECL (DECL_NAME (decl1)) != NULL_TREE)
11746 cp_warning_at ("`%D' implicitly declared before its definition", IDENTIFIER_IMPLICIT_DECL (DECL_NAME (decl1)));
11747
11748 current_function_decl = decl1;
5566b478
MS
11749 /* Save the parm names or decls from this function's declarator
11750 where store_parm_decls will find them. */
11751 current_function_parms = last_function_parms;
11752 current_function_parm_tags = last_function_parm_tags;
8d08fdba 11753
9e9ff709 11754 announce_function (decl1);
8d08fdba 11755
5156628f 11756 if (! processing_template_decl)
8d08fdba 11757 {
5566b478
MS
11758 if (TYPE_SIZE (complete_type (TREE_TYPE (fntype))) == NULL_TREE)
11759 {
11760 cp_error ("return-type `%#T' is an incomplete type",
11761 TREE_TYPE (fntype));
8d08fdba 11762
5566b478
MS
11763 /* Make it return void instead, but don't change the
11764 type of the DECL_RESULT, in case we have a named return value. */
11765 if (ctype)
11766 TREE_TYPE (decl1)
11767 = build_cplus_method_type (build_type_variant (ctype,
11768 TREE_READONLY (decl1),
11769 TREE_SIDE_EFFECTS (decl1)),
11770 void_type_node,
11771 FUNCTION_ARG_CHAIN (decl1));
11772 else
11773 TREE_TYPE (decl1)
11774 = build_function_type (void_type_node,
11775 TYPE_ARG_TYPES (TREE_TYPE (decl1)));
11776 DECL_RESULT (decl1)
11777 = build_decl (RESULT_DECL, 0, TYPE_MAIN_VARIANT (TREE_TYPE (fntype)));
11778 TREE_READONLY (DECL_RESULT (decl1)) = TYPE_READONLY (TREE_TYPE (fntype));
11779 TREE_THIS_VOLATILE (DECL_RESULT (decl1)) = TYPE_VOLATILE (TREE_TYPE (fntype));
11780 }
11781
11782 if (TYPE_LANG_SPECIFIC (TREE_TYPE (fntype))
11783 && CLASSTYPE_ABSTRACT_VIRTUALS (TREE_TYPE (fntype)))
11784 abstract_virtuals_error (decl1, TREE_TYPE (fntype));
11785 }
b7484fbe 11786
8d08fdba 11787 if (warn_about_return_type)
59f1f2c4 11788 pedwarn ("return-type defaults to `int'");
8d08fdba 11789
824b9a4c 11790 /* Effective C++ rule 15. See also c_expand_return. */
eb448459 11791 if (warn_ecpp
824b9a4c 11792 && DECL_NAME (decl1) == ansi_opname[(int) MODIFY_EXPR]
a1774733 11793 && TREE_CODE (TREE_TYPE (fntype)) == VOID_TYPE)
824b9a4c
MS
11794 cp_warning ("`operator=' should return a reference to `*this'");
11795
8d08fdba
MS
11796 /* Make the init_value nonzero so pushdecl knows this is not tentative.
11797 error_mark_node is replaced below (in poplevel) with the BLOCK. */
11798 DECL_INITIAL (decl1) = error_mark_node;
11799
8d08fdba
MS
11800 /* This function exists in static storage.
11801 (This does not mean `static' in the C sense!) */
11802 TREE_STATIC (decl1) = 1;
11803
878cd289
MS
11804 /* Record the decl so that the function name is defined.
11805 If we already have a decl for this name, and it is a FUNCTION_DECL,
11806 use the old decl. */
11807
5156628f 11808 if (processing_template_decl)
3ac3d9ea 11809 decl1 = push_template_decl (decl1);
5566b478 11810 else if (pre_parsed_p == 0)
878cd289 11811 {
75650646 11812 /* A specialization is not used to guide overload resolution. */
959d8796
JM
11813 if ((flag_guiding_decls
11814 || !DECL_TEMPLATE_SPECIALIZATION (decl1))
11815 && ! DECL_FUNCTION_MEMBER_P (decl1))
75650646 11816 decl1 = pushdecl (decl1);
2c73f9f5
ML
11817 else
11818 /* We need to set the DECL_CONTEXT. */
11819 if (!DECL_CONTEXT (decl1) && DECL_TEMPLATE_INFO (decl1))
11820 DECL_CONTEXT (decl1) = DECL_CONTEXT (DECL_TI_TEMPLATE (decl1));
878cd289
MS
11821 DECL_MAIN_VARIANT (decl1) = decl1;
11822 fntype = TREE_TYPE (decl1);
11823 }
5566b478
MS
11824
11825 current_function_decl = decl1;
878cd289 11826
db5ae43f 11827 if (DECL_INTERFACE_KNOWN (decl1))
faae18ab 11828 {
86052cc3
JM
11829 tree ctx = hack_decl_function_context (decl1);
11830
faae18ab
MS
11831 if (DECL_NOT_REALLY_EXTERN (decl1))
11832 DECL_EXTERNAL (decl1) = 0;
86052cc3
JM
11833
11834 if (ctx != NULL_TREE && DECL_THIS_INLINE (ctx)
11835 && TREE_PUBLIC (ctx))
11836 /* This is a function in a local class in an extern inline
11837 function. */
11838 comdat_linkage (decl1);
faae18ab 11839 }
8d08fdba
MS
11840 /* If this function belongs to an interface, it is public.
11841 If it belongs to someone else's interface, it is also external.
11842 It doesn't matter whether it's inline or not. */
5566b478
MS
11843 else if (interface_unknown == 0
11844 && (! DECL_TEMPLATE_INSTANTIATION (decl1)
d11ad92e 11845 || flag_alt_external_templates))
8d08fdba 11846 {
5566b478 11847 if (DECL_THIS_INLINE (decl1) || DECL_TEMPLATE_INSTANTIATION (decl1)
5156628f 11848 || processing_template_decl)
db5ae43f
MS
11849 DECL_EXTERNAL (decl1)
11850 = (interface_only
faae18ab 11851 || (DECL_THIS_INLINE (decl1) && ! flag_implement_inlines));
db5ae43f 11852 else
893de33c 11853 DECL_EXTERNAL (decl1) = 0;
e8abc66f 11854 DECL_NOT_REALLY_EXTERN (decl1) = 0;
db5ae43f 11855 DECL_INTERFACE_KNOWN (decl1) = 1;
8d08fdba
MS
11856 }
11857 else
a0a33927
MS
11858 {
11859 /* This is a definition, not a reference.
b7484fbe
MS
11860 So clear DECL_EXTERNAL. */
11861 DECL_EXTERNAL (decl1) = 0;
faae18ab 11862
5566b478
MS
11863 if ((DECL_THIS_INLINE (decl1) || DECL_TEMPLATE_INSTANTIATION (decl1))
11864 && ! DECL_INTERFACE_KNOWN (decl1)
11865 /* Don't try to defer nested functions for now. */
e76a2646 11866 && ! hack_decl_function_context (decl1))
878cd289
MS
11867 DECL_DEFER_OUTPUT (decl1) = 1;
11868 else
893de33c 11869 DECL_INTERFACE_KNOWN (decl1) = 1;
db5ae43f 11870 }
a9aedbc2 11871
8d08fdba
MS
11872 if (ctype != NULL_TREE && DECL_STATIC_FUNCTION_P (decl1))
11873 {
11874 if (TREE_CODE (fntype) == METHOD_TYPE)
11875 TREE_TYPE (decl1) = fntype
11876 = build_function_type (TREE_TYPE (fntype),
11877 TREE_CHAIN (TYPE_ARG_TYPES (fntype)));
5566b478
MS
11878 current_function_parms = TREE_CHAIN (current_function_parms);
11879 DECL_ARGUMENTS (decl1) = current_function_parms;
8d08fdba
MS
11880 ctype = NULL_TREE;
11881 }
11882 restype = TREE_TYPE (fntype);
11883
8d08fdba
MS
11884 if (ctype)
11885 {
11886 push_nested_class (ctype, 1);
11887
11888 /* If we're compiling a friend function, neither of the variables
4ac14744 11889 current_class_ptr nor current_class_type will have values. */
8d08fdba
MS
11890 if (! doing_friend)
11891 {
11892 /* We know that this was set up by `grokclassfn'.
11893 We do not wait until `store_parm_decls', since evil
11894 parse errors may never get us to that point. Here
11895 we keep the consistency between `current_class_type'
4ac14744 11896 and `current_class_ptr'. */
5566b478 11897 tree t = current_function_parms;
5f34005f
RK
11898
11899 my_friendly_assert (t != NULL_TREE
11900 && TREE_CODE (t) == PARM_DECL, 162);
11901
dff6b454
RK
11902 if (TREE_CODE (TREE_TYPE (t)) == POINTER_TYPE)
11903 {
11904 int i = suspend_momentary ();
11905
11906 /* Fool build_indirect_ref. */
4ac14744
MS
11907 current_class_ptr = NULL_TREE;
11908 current_class_ref = build_indirect_ref (t, NULL_PTR);
11909 current_class_ptr = t;
dff6b454
RK
11910 resume_momentary (i);
11911 }
11912 else
11913 /* We're having a signature pointer here. */
4ac14744 11914 current_class_ref = current_class_ptr = t;
dff6b454 11915
8d08fdba
MS
11916 }
11917 }
11918 else
11919 {
11920 if (DECL_STATIC_FUNCTION_P (decl1))
11921 push_nested_class (DECL_CONTEXT (decl1), 2);
11922 else
11923 push_memoized_context (0, 1);
4ac14744 11924 current_class_ptr = current_class_ref = NULL_TREE;
8d08fdba
MS
11925 }
11926
db5ae43f
MS
11927 pushlevel (0);
11928 current_binding_level->parm_flag = 1;
11929
db5ae43f
MS
11930 GNU_xref_function (decl1, current_function_parms);
11931
f30432d7
MS
11932 if (attrs)
11933 cplus_decl_attributes (decl1, NULL_TREE, attrs);
db5ae43f
MS
11934 make_function_rtl (decl1);
11935
8d08fdba
MS
11936 /* Promote the value to int before returning it. */
11937 if (C_PROMOTING_INTEGER_TYPE_P (restype))
5566b478
MS
11938 restype = type_promotes_to (restype);
11939
11940 /* If this fcn was already referenced via a block-scope `extern' decl
11941 (or an implicit decl), propagate certain information about the usage. */
11942 if (TREE_ADDRESSABLE (DECL_ASSEMBLER_NAME (decl1)))
11943 TREE_ADDRESSABLE (decl1) = 1;
11944
8d08fdba 11945 if (DECL_RESULT (decl1) == NULL_TREE)
f30432d7
MS
11946 {
11947 DECL_RESULT (decl1)
11948 = build_decl (RESULT_DECL, 0, TYPE_MAIN_VARIANT (restype));
11949 TREE_READONLY (DECL_RESULT (decl1)) = TYPE_READONLY (restype);
11950 TREE_THIS_VOLATILE (DECL_RESULT (decl1)) = TYPE_VOLATILE (restype);
11951 }
8d08fdba 11952
948edfa7
JW
11953 /* Allocate further tree nodes temporarily during compilation
11954 of this function only. Tiemann moved up here from bottom of fn. */
11955 /* If this is a nested function, then we must continue to allocate RTL
11956 on the permanent obstack in case we need to inline it later. */
11957 if (! hack_decl_function_context (decl1))
5566b478
MS
11958 temporary_allocation ();
11959
5156628f 11960 if (processing_template_decl)
5566b478 11961 {
5566b478
MS
11962 ++minimal_parse_mode;
11963 last_tree = DECL_SAVED_TREE (decl1)
11964 = build_nt (EXPR_STMT, void_zero_node);
11965 }
11966
11967 ++function_depth;
11968
f30432d7
MS
11969 if (DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (decl1))
11970 && DECL_LANGUAGE (decl1) == lang_cplusplus)
8d08fdba
MS
11971 {
11972 dtor_label = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE);
11973 ctor_label = NULL_TREE;
11974 }
11975 else
11976 {
11977 dtor_label = NULL_TREE;
11978 if (DECL_CONSTRUCTOR_P (decl1))
11979 ctor_label = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE);
11980 }
11981
8d08fdba
MS
11982 return 1;
11983}
11984\f
9664b89e
JM
11985/* Called after store_parm_decls for a function-try-block. We need to update
11986 last_parm_cleanup_insn so that the base initializers for a constructor
11987 are run within this block, not before it. */
6467930b
MS
11988
11989void
11990expand_start_early_try_stmts ()
11991{
6467930b 11992 expand_start_try_stmts ();
9664b89e 11993 last_parm_cleanup_insn = get_last_insn ();
f30432d7
MS
11994}
11995
8d08fdba
MS
11996/* Store the parameter declarations into the current function declaration.
11997 This is called after parsing the parameter declarations, before
11998 digesting the body of the function.
11999
12000 Also install to binding contour return value identifier, if any. */
12001
12002void
12003store_parm_decls ()
12004{
12005 register tree fndecl = current_function_decl;
12006 register tree parm;
12007 int parms_have_cleanups = 0;
eb66be0e 12008 tree cleanups = NULL_TREE;
8d08fdba
MS
12009
12010 /* This is either a chain of PARM_DECLs (when a prototype is used). */
12011 tree specparms = current_function_parms;
12012
12013 /* This is a list of types declared among parms in a prototype. */
12014 tree parmtags = current_function_parm_tags;
12015
12016 /* This is a chain of any other decls that came in among the parm
12017 declarations. If a parm is declared with enum {foo, bar} x;
12018 then CONST_DECLs for foo and bar are put here. */
12019 tree nonparms = NULL_TREE;
12020
a9aedbc2 12021 if (toplevel_bindings_p ())
8d08fdba
MS
12022 fatal ("parse errors have confused me too much");
12023
12024 /* Initialize RTL machinery. */
12025 init_function_start (fndecl, input_filename, lineno);
12026
12027 /* Declare __FUNCTION__ and __PRETTY_FUNCTION__ for this function. */
12028 declare_function_name ();
12029
12030 /* Create a binding level for the parms. */
12031 expand_start_bindings (0);
12032
8d08fdba
MS
12033 if (specparms != NULL_TREE)
12034 {
12035 /* This case is when the function was defined with an ANSI prototype.
12036 The parms already have decls, so we need not do anything here
12037 except record them as in effect
12038 and complain if any redundant old-style parm decls were written. */
12039
12040 register tree next;
12041
12042 /* Must clear this because it might contain TYPE_DECLs declared
12043 at class level. */
12044 storedecls (NULL_TREE);
5566b478 12045
8d08fdba
MS
12046 for (parm = nreverse (specparms); parm; parm = next)
12047 {
12048 next = TREE_CHAIN (parm);
12049 if (TREE_CODE (parm) == PARM_DECL)
12050 {
5566b478 12051 tree cleanup;
8d08fdba
MS
12052 if (DECL_NAME (parm) == NULL_TREE)
12053 {
8d08fdba 12054 pushdecl (parm);
8d08fdba 12055 }
a1774733 12056 else if (TREE_CODE (TREE_TYPE (parm)) == VOID_TYPE)
8d08fdba
MS
12057 cp_error ("parameter `%D' declared void", parm);
12058 else
12059 {
12060 /* Now fill in DECL_REFERENCE_SLOT for any of the parm decls.
12061 A parameter is assumed not to have any side effects.
12062 If this should change for any reason, then this
12063 will have to wrap the bashed reference type in a save_expr.
12064
12065 Also, if the parameter type is declared to be an X
12066 and there is an X(X&) constructor, we cannot lay it
12067 into the stack (any more), so we make this parameter
12068 look like it is really of reference type. Functions
12069 which pass parameters to this function will know to
12070 create a temporary in their frame, and pass a reference
12071 to that. */
12072
12073 if (TREE_CODE (TREE_TYPE (parm)) == REFERENCE_TYPE
12074 && TYPE_SIZE (TREE_TYPE (TREE_TYPE (parm))))
12075 SET_DECL_REFERENCE_SLOT (parm, convert_from_reference (parm));
12076
12077 pushdecl (parm);
12078 }
5156628f 12079 if (! processing_template_decl
5566b478 12080 && (cleanup = maybe_build_cleanup (parm), cleanup))
8d08fdba
MS
12081 {
12082 expand_decl (parm);
8d08fdba 12083 parms_have_cleanups = 1;
eb66be0e
MS
12084
12085 /* Keep track of the cleanups. */
12086 cleanups = tree_cons (parm, cleanup, cleanups);
8d08fdba
MS
12087 }
12088 }
12089 else
12090 {
12091 /* If we find an enum constant or a type tag,
12092 put it aside for the moment. */
12093 TREE_CHAIN (parm) = NULL_TREE;
12094 nonparms = chainon (nonparms, parm);
12095 }
12096 }
12097
12098 /* Get the decls in their original chain order
12099 and record in the function. This is all and only the
12100 PARM_DECLs that were pushed into scope by the loop above. */
12101 DECL_ARGUMENTS (fndecl) = getdecls ();
12102
12103 storetags (chainon (parmtags, gettags ()));
12104 }
12105 else
12106 DECL_ARGUMENTS (fndecl) = NULL_TREE;
12107
12108 /* Now store the final chain of decls for the arguments
12109 as the decl-chain of the current lexical scope.
12110 Put the enumerators in as well, at the front so that
12111 DECL_ARGUMENTS is not modified. */
12112
12113 storedecls (chainon (nonparms, DECL_ARGUMENTS (fndecl)));
12114
12115 /* Initialize the RTL code for the function. */
12116 DECL_SAVED_INSNS (fndecl) = NULL_RTX;
5156628f 12117 if (! processing_template_decl)
5566b478 12118 expand_function_start (fndecl, parms_have_cleanups);
8d08fdba 12119
eb448459
MS
12120 current_function_parms_stored = 1;
12121
12122 /* If this function is `main', emit a call to `__main'
12123 to run global initializers, etc. */
12124 if (DECL_NAME (fndecl)
12125 && IDENTIFIER_LENGTH (DECL_NAME (fndecl)) == 4
12126 && strcmp (IDENTIFIER_POINTER (DECL_NAME (fndecl)), "main") == 0
2c73f9f5 12127 && DECL_CONTEXT (fndecl) == global_namespace)
eb448459
MS
12128 {
12129 expand_main_function ();
12130 }
12131
eb66be0e
MS
12132 /* Now that we have initialized the parms, we can start their
12133 cleanups. We cannot do this before, since expand_decl_cleanup
12134 should not be called before the parm can be used. */
eb448459 12135 if (cleanups
eb66be0e
MS
12136 && ! processing_template_decl)
12137 {
12138 for (cleanups = nreverse (cleanups); cleanups; cleanups = TREE_CHAIN (cleanups))
12139 {
12140 if (! expand_decl_cleanup (TREE_PURPOSE (cleanups), TREE_VALUE (cleanups)))
12141 cp_error ("parser lost in parsing declaration of `%D'",
12142 TREE_PURPOSE (cleanups));
12143 }
12144 }
12145
8d08fdba
MS
12146 /* Create a binding contour which can be used to catch
12147 cleanup-generated temporaries. Also, if the return value needs or
12148 has initialization, deal with that now. */
12149 if (parms_have_cleanups)
12150 {
12151 pushlevel (0);
12152 expand_start_bindings (0);
12153 }
12154
5156628f 12155 if (! processing_template_decl && flag_exceptions)
f30432d7 12156 {
f30432d7
MS
12157 /* Do the starting of the exception specifications, if we have any. */
12158 if (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (current_function_decl)))
12159 expand_start_eh_spec ();
f30432d7 12160 }
eb448459 12161
5c825fc2 12162 last_parm_cleanup_insn = get_last_insn ();
72b7eeff 12163 last_dtor_insn = get_last_insn ();
8d08fdba
MS
12164}
12165
12166/* Bind a name and initialization to the return value of
12167 the current function. */
e92cc029 12168
8d08fdba
MS
12169void
12170store_return_init (return_id, init)
12171 tree return_id, init;
12172{
12173 tree decl = DECL_RESULT (current_function_decl);
12174
e1cd6e56 12175 if (pedantic)
8d08fdba
MS
12176 /* Give this error as many times as there are occurrences,
12177 so that users can use Emacs compilation buffers to find
12178 and fix all such places. */
8926095f 12179 pedwarn ("ANSI C++ does not permit named return values");
8d08fdba
MS
12180
12181 if (return_id != NULL_TREE)
12182 {
12183 if (DECL_NAME (decl) == NULL_TREE)
12184 {
12185 DECL_NAME (decl) = return_id;
12186 DECL_ASSEMBLER_NAME (decl) = return_id;
12187 }
12188 else
e76a2646 12189 cp_error ("return identifier `%D' already in place", decl);
8d08fdba
MS
12190 }
12191
12192 /* Can't let this happen for constructors. */
12193 if (DECL_CONSTRUCTOR_P (current_function_decl))
12194 {
12195 error ("can't redefine default return value for constructors");
12196 return;
12197 }
12198
12199 /* If we have a named return value, put that in our scope as well. */
12200 if (DECL_NAME (decl) != NULL_TREE)
12201 {
12202 /* If this named return value comes in a register,
12203 put it in a pseudo-register. */
12204 if (DECL_REGISTER (decl))
12205 {
12206 original_result_rtx = DECL_RTL (decl);
12207 DECL_RTL (decl) = gen_reg_rtx (DECL_MODE (decl));
12208 }
12209
82580166 12210 /* Let `cp_finish_decl' know that this initializer is ok. */
8d08fdba
MS
12211 DECL_INITIAL (decl) = init;
12212 pushdecl (decl);
e76a2646
MS
12213
12214 if (minimal_parse_mode)
12215 add_tree (build_min_nt (RETURN_INIT, return_id,
12216 copy_to_permanent (init)));
12217 else
dae1d6f6 12218 cp_finish_decl (decl, init, NULL_TREE, 0, 0);
8d08fdba
MS
12219 }
12220}
12221
8d08fdba
MS
12222\f
12223/* Finish up a function declaration and compile that function
12224 all the way to assembler language output. The free the storage
12225 for the function definition.
12226
12227 This is called after parsing the body of the function definition.
12228 LINENO is the current line number.
12229
12230 C++: CALL_POPLEVEL is non-zero if an extra call to poplevel
12231 (and expand_end_bindings) must be made to take care of the binding
12232 contour for the base initializers. This is only relevant for
e92cc029
MS
12233 constructors.
12234
12235 NESTED is nonzero if we were in the middle of compiling another function
12236 when we started on this one. */
8d08fdba
MS
12237
12238void
db5ae43f 12239finish_function (lineno, call_poplevel, nested)
8d08fdba
MS
12240 int lineno;
12241 int call_poplevel;
db5ae43f 12242 int nested;
8d08fdba
MS
12243{
12244 register tree fndecl = current_function_decl;
12245 tree fntype, ctype = NULL_TREE;
f30432d7 12246 rtx last_parm_insn, insns;
8d08fdba
MS
12247 /* Label to use if this function is supposed to return a value. */
12248 tree no_return_label = NULL_TREE;
12249 tree decls = NULL_TREE;
12250
12251 /* When we get some parse errors, we can end up without a
12252 current_function_decl, so cope. */
12253 if (fndecl == NULL_TREE)
12254 return;
12255
e92cc029
MS
12256 if (! nested && function_depth > 1)
12257 nested = 1;
12258
8d08fdba
MS
12259 fntype = TREE_TYPE (fndecl);
12260
12261/* TREE_READONLY (fndecl) = 1;
12262 This caused &foo to be of type ptr-to-const-function
12263 which then got a warning when stored in a ptr-to-function variable. */
12264
12265 /* This happens on strange parse errors. */
12266 if (! current_function_parms_stored)
12267 {
12268 call_poplevel = 0;
12269 store_parm_decls ();
12270 }
12271
5156628f 12272 if (processing_template_decl)
8d08fdba 12273 {
5566b478
MS
12274 if (DECL_CONSTRUCTOR_P (fndecl) && call_poplevel)
12275 {
12276 decls = getdecls ();
12277 expand_end_bindings (decls, decls != NULL_TREE, 0);
12278 poplevel (decls != NULL_TREE, 0, 0);
12279 }
12280 }
12281 else
12282 {
12283 if (write_symbols != NO_DEBUG /*&& TREE_CODE (fntype) != METHOD_TYPE*/)
8d08fdba 12284 {
5566b478
MS
12285 tree ttype = target_type (fntype);
12286 tree parmdecl;
12287
8d08fdba
MS
12288 if (IS_AGGR_TYPE (ttype))
12289 /* Let debugger know it should output info for this type. */
12290 note_debug_info_needed (ttype);
5566b478
MS
12291
12292 for (parmdecl = DECL_ARGUMENTS (fndecl); parmdecl; parmdecl = TREE_CHAIN (parmdecl))
12293 {
12294 ttype = target_type (TREE_TYPE (parmdecl));
12295 if (IS_AGGR_TYPE (ttype))
12296 /* Let debugger know it should output info for this type. */
12297 note_debug_info_needed (ttype);
12298 }
8d08fdba 12299 }
8d08fdba 12300
5566b478
MS
12301 /* Clean house because we will need to reorder insns here. */
12302 do_pending_stack_adjust ();
8d08fdba 12303
5566b478 12304 if (dtor_label)
8d08fdba 12305 {
5566b478
MS
12306 tree binfo = TYPE_BINFO (current_class_type);
12307 tree cond = integer_one_node;
12308 tree exprstmt;
12309 tree in_charge_node = lookup_name (in_charge_identifier, 0);
12310 tree virtual_size;
12311 int ok_to_optimize_dtor = 0;
12312 int empty_dtor = get_last_insn () == last_dtor_insn;
8d08fdba 12313
5566b478
MS
12314 if (current_function_assigns_this)
12315 cond = build (NE_EXPR, boolean_type_node,
4ac14744 12316 current_class_ptr, integer_zero_node);
5566b478
MS
12317 else
12318 {
12319 int n_baseclasses = CLASSTYPE_N_BASECLASSES (current_class_type);
12320
12321 /* If this destructor is empty, then we don't need to check
12322 whether `this' is NULL in some cases. */
12323 if ((flag_this_is_variable & 1) == 0)
12324 ok_to_optimize_dtor = 1;
12325 else if (empty_dtor)
12326 ok_to_optimize_dtor
12327 = (n_baseclasses == 0
12328 || (n_baseclasses == 1
12329 && TYPE_HAS_DESTRUCTOR (TYPE_BINFO_BASETYPE (current_class_type, 0))));
12330 }
8d08fdba 12331
5566b478
MS
12332 /* These initializations might go inline. Protect
12333 the binding level of the parms. */
12334 pushlevel (0);
12335 expand_start_bindings (0);
8d08fdba 12336
5566b478
MS
12337 if (current_function_assigns_this)
12338 {
12339 current_function_assigns_this = 0;
12340 current_function_just_assigned_this = 0;
12341 }
8d08fdba 12342
5566b478
MS
12343 /* Generate the code to call destructor on base class.
12344 If this destructor belongs to a class with virtual
12345 functions, then set the virtual function table
12346 pointer to represent the type of our base class. */
8d08fdba 12347
5566b478
MS
12348 /* This side-effect makes call to `build_delete' generate the
12349 code we have to have at the end of this destructor. */
12350 TYPE_HAS_DESTRUCTOR (current_class_type) = 0;
8d08fdba 12351
5566b478
MS
12352 /* These are two cases where we cannot delegate deletion. */
12353 if (TYPE_USES_VIRTUAL_BASECLASSES (current_class_type)
12354 || TYPE_GETS_REG_DELETE (current_class_type))
4ac14744 12355 exprstmt = build_delete (current_class_type, current_class_ref, integer_zero_node,
e92cc029 12356 LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR|LOOKUP_NORMAL, 0);
5566b478 12357 else
4ac14744 12358 exprstmt = build_delete (current_class_type, current_class_ref, in_charge_node,
e92cc029 12359 LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR|LOOKUP_NORMAL, 0);
5566b478
MS
12360
12361 /* If we did not assign to this, then `this' is non-zero at
12362 the end of a destructor. As a special optimization, don't
12363 emit test if this is an empty destructor. If it does nothing,
12364 it does nothing. If it calls a base destructor, the base
12365 destructor will perform the test. */
12366
12367 if (exprstmt != error_mark_node
12368 && (TREE_CODE (exprstmt) != NOP_EXPR
12369 || TREE_OPERAND (exprstmt, 0) != integer_zero_node
12370 || TYPE_USES_VIRTUAL_BASECLASSES (current_class_type)))
8d08fdba 12371 {
5566b478
MS
12372 expand_label (dtor_label);
12373 if (cond != integer_one_node)
12374 expand_start_cond (cond, 0);
12375 if (exprstmt != void_zero_node)
12376 /* Don't call `expand_expr_stmt' if we're not going to do
12377 anything, since -Wall will give a diagnostic. */
12378 expand_expr_stmt (exprstmt);
12379
12380 /* Run destructor on all virtual baseclasses. */
12381 if (TYPE_USES_VIRTUAL_BASECLASSES (current_class_type))
8d08fdba 12382 {
5566b478
MS
12383 tree vbases = nreverse (copy_list (CLASSTYPE_VBASECLASSES (current_class_type)));
12384 expand_start_cond (build (BIT_AND_EXPR, integer_type_node,
12385 in_charge_node, integer_two_node), 0);
12386 while (vbases)
8d08fdba 12387 {
5566b478
MS
12388 if (TYPE_NEEDS_DESTRUCTOR (BINFO_TYPE (vbases)))
12389 {
bd6dd845
MS
12390 tree vb = get_vbase
12391 (BINFO_TYPE (vbases),
12392 TYPE_BINFO (current_class_type));
e92cc029 12393 expand_expr_stmt
bd6dd845
MS
12394 (build_scoped_method_call
12395 (current_class_ref, vb, dtor_identifier,
e66d884e 12396 build_expr_list (NULL_TREE, integer_zero_node)));
5566b478
MS
12397 }
12398 vbases = TREE_CHAIN (vbases);
8d08fdba 12399 }
5566b478 12400 expand_end_cond ();
8d08fdba 12401 }
5566b478
MS
12402
12403 do_pending_stack_adjust ();
12404 if (cond != integer_one_node)
12405 expand_end_cond ();
8d08fdba
MS
12406 }
12407
5566b478
MS
12408 TYPE_HAS_DESTRUCTOR (current_class_type) = 1;
12409
12410 virtual_size = c_sizeof (current_class_type);
12411
12412 /* At the end, call delete if that's what's requested. */
12413 if (TYPE_GETS_REG_DELETE (current_class_type))
12414 /* This NOP_EXPR means we are in a static call context. */
beb53fb8
JM
12415 exprstmt
12416 = build_method_call (build_indirect_ref (build1 (NOP_EXPR,
12417 build_pointer_type (current_class_type),
12418 error_mark_node),
12419 NULL_PTR),
12420 ansi_opname[(int) DELETE_EXPR],
e66d884e
JM
12421 expr_tree_cons (NULL_TREE, current_class_ptr,
12422 build_expr_list (NULL_TREE, virtual_size)),
beb53fb8 12423 NULL_TREE, LOOKUP_NORMAL);
5566b478 12424 else if (TYPE_USES_VIRTUAL_BASECLASSES (current_class_type))
4ac14744 12425 exprstmt = build_x_delete (ptr_type_node, current_class_ptr, 0,
5566b478
MS
12426 virtual_size);
12427 else
12428 exprstmt = NULL_TREE;
8d08fdba 12429
5566b478
MS
12430 if (exprstmt)
12431 {
12432 cond = build (BIT_AND_EXPR, integer_type_node,
12433 in_charge_node, integer_one_node);
12434 expand_start_cond (cond, 0);
12435 expand_expr_stmt (exprstmt);
12436 expand_end_cond ();
12437 }
8d08fdba 12438
5566b478 12439 /* End of destructor. */
fc378698 12440 expand_end_bindings (NULL_TREE, getdecls () != NULL_TREE, 0);
5566b478
MS
12441 poplevel (2, 0, 0); /* XXX change to 1 */
12442
12443 /* Back to the top of destructor. */
956d6950 12444 /* Don't execute destructor code if `this' is NULL. */
5566b478
MS
12445
12446 start_sequence ();
12447
12448 /* If the dtor is empty, and we know there is not possible way we
12449 could use any vtable entries, before they are possibly set by
12450 a base class dtor, we don't have to setup the vtables, as we
12451 know that any base class dtoring will set up any vtables it
12452 needs. We avoid MI, because one base class dtor can do a
12453 virtual dispatch to an overridden function that would need to
12454 have a non-related vtable set up, we cannot avoid setting up
12455 vtables in that case. We could change this to see if there is
12456 just one vtable. */
12457 if (! empty_dtor || TYPE_USES_COMPLEX_INHERITANCE (current_class_type))
12458 {
12459 /* Make all virtual function table pointers in non-virtual base
12460 classes point to CURRENT_CLASS_TYPE's virtual function
12461 tables. */
4ac14744 12462 expand_direct_vtbls_init (binfo, binfo, 1, 0, current_class_ptr);
8d08fdba 12463
5566b478 12464 if (TYPE_USES_VIRTUAL_BASECLASSES (current_class_type))
4ac14744 12465 expand_indirect_vtbls_init (binfo, current_class_ref, current_class_ptr);
5566b478
MS
12466 }
12467
12468 if (! ok_to_optimize_dtor)
12469 {
12470 cond = build_binary_op (NE_EXPR,
4ac14744 12471 current_class_ptr, integer_zero_node, 1);
5566b478
MS
12472 expand_start_cond (cond, 0);
12473 }
8d08fdba 12474
5566b478
MS
12475 insns = get_insns ();
12476 end_sequence ();
f30432d7 12477
5566b478
MS
12478 last_parm_insn = get_first_nonparm_insn ();
12479 if (last_parm_insn == NULL_RTX)
12480 last_parm_insn = get_last_insn ();
12481 else
12482 last_parm_insn = previous_insn (last_parm_insn);
8d08fdba 12483
5566b478 12484 emit_insns_after (insns, last_parm_insn);
72b7eeff 12485
5566b478
MS
12486 if (! ok_to_optimize_dtor)
12487 expand_end_cond ();
72b7eeff 12488 }
5566b478 12489 else if (current_function_assigns_this)
8d08fdba 12490 {
5566b478
MS
12491 /* Does not need to call emit_base_init, because
12492 that is done (if needed) just after assignment to this
12493 is seen. */
12494
12495 if (DECL_CONSTRUCTOR_P (current_function_decl))
12496 {
12497 end_protect_partials ();
12498 expand_label (ctor_label);
12499 ctor_label = NULL_TREE;
12500
12501 if (call_poplevel)
12502 {
12503 decls = getdecls ();
12504 expand_end_bindings (decls, decls != NULL_TREE, 0);
12505 poplevel (decls != NULL_TREE, 0, 0);
12506 }
4ac14744 12507 c_expand_return (current_class_ptr);
5566b478 12508 }
a1774733 12509 else if (TREE_CODE (TREE_TYPE (DECL_RESULT (current_function_decl))) != VOID_TYPE
5566b478
MS
12510 && return_label != NULL_RTX)
12511 no_return_label = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE);
12512
12513 current_function_assigns_this = 0;
12514 current_function_just_assigned_this = 0;
12515 base_init_expr = NULL_TREE;
8d08fdba 12516 }
5566b478
MS
12517 else if (DECL_CONSTRUCTOR_P (fndecl))
12518 {
a703fb38 12519 tree cond = NULL_TREE, thenclause = NULL_TREE;
5566b478
MS
12520 /* Allow constructor for a type to get a new instance of the object
12521 using `build_new'. */
12522 tree abstract_virtuals = CLASSTYPE_ABSTRACT_VIRTUALS (current_class_type);
12523 CLASSTYPE_ABSTRACT_VIRTUALS (current_class_type) = NULL_TREE;
f30432d7 12524
5566b478 12525 DECL_RETURNS_FIRST_ARG (fndecl) = 1;
f30432d7 12526
5566b478
MS
12527 if (flag_this_is_variable > 0)
12528 {
12529 cond = build_binary_op (EQ_EXPR,
4ac14744
MS
12530 current_class_ptr, integer_zero_node, 1);
12531 thenclause = build_modify_expr (current_class_ptr, NOP_EXPR,
5566b478
MS
12532 build_new (NULL_TREE, current_class_type, void_type_node, 0));
12533 }
f30432d7 12534
5566b478 12535 CLASSTYPE_ABSTRACT_VIRTUALS (current_class_type) = abstract_virtuals;
f30432d7 12536
5566b478
MS
12537 start_sequence ();
12538
12539 if (flag_this_is_variable > 0)
12540 {
12541 expand_start_cond (cond, 0);
12542 expand_expr_stmt (thenclause);
12543 expand_end_cond ();
12544 }
12545
12546 /* Emit insns from `emit_base_init' which sets up virtual
12547 function table pointer(s). */
12548 if (base_init_expr)
12549 {
12550 expand_expr_stmt (base_init_expr);
12551 base_init_expr = NULL_TREE;
12552 }
12553
12554 insns = get_insns ();
12555 end_sequence ();
12556
b87692e5 12557 /* This is where the body of the constructor begins. */
5566b478 12558
b87692e5 12559 emit_insns_after (insns, last_parm_cleanup_insn);
8d08fdba 12560
b7484fbe 12561 end_protect_partials ();
5566b478
MS
12562
12563 /* This is where the body of the constructor ends. */
8d08fdba
MS
12564 expand_label (ctor_label);
12565 ctor_label = NULL_TREE;
12566
12567 if (call_poplevel)
12568 {
12569 decls = getdecls ();
8d08fdba 12570 expand_end_bindings (decls, decls != NULL_TREE, 0);
5566b478 12571 poplevel (decls != NULL_TREE, 1, 0);
8d08fdba 12572 }
8d08fdba 12573
4ac14744 12574 c_expand_return (current_class_ptr);
8d08fdba 12575
5566b478
MS
12576 current_function_assigns_this = 0;
12577 current_function_just_assigned_this = 0;
12578 }
12579 else if (IDENTIFIER_LENGTH (DECL_NAME (fndecl)) == 4
12580 && ! strcmp (IDENTIFIER_POINTER (DECL_NAME (fndecl)), "main")
2c73f9f5 12581 && DECL_CONTEXT (fndecl) == global_namespace)
8d08fdba 12582 {
5566b478
MS
12583 /* Make it so that `main' always returns 0 by default. */
12584#ifdef VMS
12585 c_expand_return (integer_one_node);
12586#else
12587 c_expand_return (integer_zero_node);
12588#endif
8d08fdba 12589 }
5566b478
MS
12590 else if (return_label != NULL_RTX
12591 && current_function_return_value == NULL_TREE
12592 && ! DECL_NAME (DECL_RESULT (current_function_decl)))
12593 no_return_label = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE);
8d08fdba 12594
eb448459
MS
12595 if (flag_exceptions)
12596 expand_exception_blocks ();
12597
5566b478
MS
12598 /* If this function is supposed to return a value, ensure that
12599 we do not fall into the cleanups by mistake. The end of our
12600 function will look like this:
12601
12602 user code (may have return stmt somewhere)
12603 goto no_return_label
12604 cleanup_label:
12605 cleanups
12606 goto return_label
12607 no_return_label:
12608 NOTE_INSN_FUNCTION_END
12609 return_label:
12610 things for return
12611
12612 If the user omits a return stmt in the USER CODE section, we
12613 will have a control path which reaches NOTE_INSN_FUNCTION_END.
12614 Otherwise, we won't. */
12615 if (no_return_label)
8d08fdba 12616 {
5566b478
MS
12617 DECL_CONTEXT (no_return_label) = fndecl;
12618 DECL_INITIAL (no_return_label) = error_mark_node;
12619 DECL_SOURCE_FILE (no_return_label) = input_filename;
12620 DECL_SOURCE_LINE (no_return_label) = lineno;
12621 expand_goto (no_return_label);
8d08fdba
MS
12622 }
12623
5566b478 12624 if (cleanup_label)
a9aedbc2 12625 {
2c73f9f5 12626 /* Remove the binding contour which is used
5566b478
MS
12627 to catch cleanup-generated temporaries. */
12628 expand_end_bindings (0, 0, 0);
12629 poplevel (0, 0, 0);
8d08fdba 12630
eb448459
MS
12631 /* Emit label at beginning of cleanup code for parameters. */
12632 emit_label (cleanup_label);
12633 }
b7484fbe 12634
5566b478
MS
12635 /* Get return value into register if that's where it's supposed to be. */
12636 if (original_result_rtx)
12637 fixup_result_decl (DECL_RESULT (fndecl), original_result_rtx);
8d08fdba 12638
5566b478
MS
12639 /* Finish building code that will trigger warnings if users forget
12640 to make their functions return values. */
12641 if (no_return_label || cleanup_label)
12642 emit_jump (return_label);
12643 if (no_return_label)
8d08fdba 12644 {
5566b478
MS
12645 /* We don't need to call `expand_*_return' here because we
12646 don't need any cleanups here--this path of code is only
12647 for error checking purposes. */
12648 expand_label (no_return_label);
8d08fdba
MS
12649 }
12650
5566b478
MS
12651 /* Generate rtl for function exit. */
12652 expand_function_end (input_filename, lineno, 1);
8d08fdba 12653 }
8d2733ca 12654
8d08fdba
MS
12655 /* This must come after expand_function_end because cleanups might
12656 have declarations (from inline functions) that need to go into
12657 this function's blocks. */
12658 if (current_binding_level->parm_flag != 1)
12659 my_friendly_abort (122);
12660 poplevel (1, 0, 1);
12661
2c73f9f5 12662 /* Reset scope for C++: if we were in the scope of a class,
db5ae43f
MS
12663 then when we finish this function, we are not longer so.
12664 This cannot be done until we know for sure that no more
12665 class members will ever be referenced in this function
12666 (i.e., calls to destructors). */
12667 if (current_class_name)
12668 {
12669 ctype = current_class_type;
12670 pop_nested_class (1);
12671 }
12672 else
12673 pop_memoized_context (1);
12674
8d08fdba 12675 /* Must mark the RESULT_DECL as being in this function. */
c73964b2 12676 DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
8d08fdba 12677
8d08fdba
MS
12678 /* Set the BLOCK_SUPERCONTEXT of the outermost function scope to point
12679 to the FUNCTION_DECL node itself. */
12680 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
12681
5156628f 12682 if (! processing_template_decl)
5566b478 12683 {
75650646
MM
12684 int saved_flag_keep_inline_functions =
12685 flag_keep_inline_functions;
12686
5566b478
MS
12687 /* So we can tell if jump_optimize sets it to 1. */
12688 can_reach_end = 0;
8d08fdba 12689
75650646 12690 if (DECL_CONTEXT (fndecl) != NULL_TREE
e1467ff2 12691 && hack_decl_function_context (fndecl))
75650646
MM
12692 /* Trick rest_of_compilation into not deferring output of this
12693 function, even if it is inline, since the rtl_obstack for
12694 this function is the function_obstack of the enclosing
12695 function and will be deallocated when the enclosing
12696 function is gone. See save_tree_status. */
12697 flag_keep_inline_functions = 1;
12698
6633d636
MS
12699 /* Run the optimizers and output the assembler code for this
12700 function. */
12701
12702 if (DECL_ARTIFICIAL (fndecl))
12703 {
12704 /* Do we really *want* to inline this synthesized method? */
12705
12706 int save_fif = flag_inline_functions;
12707 flag_inline_functions = 1;
12708
12709 /* Turn off DECL_INLINE for the moment so function_cannot_inline_p
12710 will check our size. */
12711 DECL_INLINE (fndecl) = 0;
12712
12713 rest_of_compilation (fndecl);
12714 flag_inline_functions = save_fif;
12715 }
12716 else
12717 rest_of_compilation (fndecl);
8d08fdba 12718
75650646
MM
12719 flag_keep_inline_functions = saved_flag_keep_inline_functions;
12720
5566b478
MS
12721 if (DECL_SAVED_INSNS (fndecl) && ! TREE_ASM_WRITTEN (fndecl))
12722 {
12723 /* Set DECL_EXTERNAL so that assemble_external will be called as
12724 necessary. We'll clear it again in finish_file. */
12725 if (! DECL_EXTERNAL (fndecl))
12726 DECL_NOT_REALLY_EXTERN (fndecl) = 1;
12727 DECL_EXTERNAL (fndecl) = 1;
12728 mark_inline_for_output (fndecl);
12729 }
8926095f 12730
d2e5ee5c
MS
12731 if (ctype && TREE_ASM_WRITTEN (fndecl))
12732 note_debug_info_needed (ctype);
12733
5566b478 12734 current_function_returns_null |= can_reach_end;
8d08fdba 12735
5566b478
MS
12736 /* Since we don't normally go through c_expand_return for constructors,
12737 this normally gets the wrong value.
12738 Also, named return values have their return codes emitted after
12739 NOTE_INSN_FUNCTION_END, confusing jump.c. */
12740 if (DECL_CONSTRUCTOR_P (fndecl)
12741 || DECL_NAME (DECL_RESULT (fndecl)) != NULL_TREE)
12742 current_function_returns_null = 0;
8d08fdba 12743
5566b478
MS
12744 if (TREE_THIS_VOLATILE (fndecl) && current_function_returns_null)
12745 cp_warning ("`noreturn' function `%D' does return", fndecl);
12746 else if ((warn_return_type || pedantic)
12747 && current_function_returns_null
a1774733 12748 && TREE_CODE (TREE_TYPE (fntype)) != VOID_TYPE)
5566b478
MS
12749 {
12750 /* If this function returns non-void and control can drop through,
12751 complain. */
14e403d4 12752 cp_warning ("control reaches end of non-void function `%D'", fndecl);
5566b478
MS
12753 }
12754 /* With just -W, complain only if function returns both with
12755 and without a value. */
12756 else if (extra_warnings
12757 && current_function_returns_value && current_function_returns_null)
12758 warning ("this function may return with or without a value");
8d08fdba 12759 }
5566b478
MS
12760
12761 --function_depth;
8d08fdba
MS
12762
12763 /* Free all the tree nodes making up this function. */
12764 /* Switch back to allocating nodes permanently
12765 until we start another function. */
5156628f 12766 if (processing_template_decl)
5566b478
MS
12767 {
12768 --minimal_parse_mode;
12769 DECL_SAVED_TREE (fndecl) = TREE_CHAIN (DECL_SAVED_TREE (fndecl));
12770 }
12771
db5ae43f
MS
12772 if (! nested)
12773 permanent_allocation (1);
8d08fdba 12774
8d08fdba
MS
12775 if (DECL_SAVED_INSNS (fndecl) == NULL_RTX)
12776 {
d2e5ee5c
MS
12777 tree t;
12778
8d08fdba
MS
12779 /* Stop pointing to the local nodes about to be freed. */
12780 /* But DECL_INITIAL must remain nonzero so we know this
12781 was an actual function definition. */
12782 DECL_INITIAL (fndecl) = error_mark_node;
d2e5ee5c 12783 for (t = DECL_ARGUMENTS (fndecl); t; t = TREE_CHAIN (t))
de22184b 12784 DECL_RTL (t) = DECL_INCOMING_RTL (t) = NULL_RTX;
8d08fdba
MS
12785 }
12786
e1cd6e56
MS
12787 if (DECL_STATIC_CONSTRUCTOR (fndecl))
12788 static_ctors = perm_tree_cons (NULL_TREE, fndecl, static_ctors);
12789 if (DECL_STATIC_DESTRUCTOR (fndecl))
12790 static_dtors = perm_tree_cons (NULL_TREE, fndecl, static_dtors);
12791
28cbf42c
MS
12792 if (! nested)
12793 {
12794 /* Let the error reporting routines know that we're outside a
12795 function. For a nested function, this value is used in
12796 pop_cp_function_context and then reset via pop_function_context. */
12797 current_function_decl = NULL_TREE;
12798 }
12799
e349ee73 12800 named_label_uses = NULL;
4ac14744
MS
12801 current_class_ptr = NULL_TREE;
12802 current_class_ref = NULL_TREE;
8d08fdba
MS
12803}
12804\f
12805/* Create the FUNCTION_DECL for a function definition.
12806 LINE1 is the line number that the definition absolutely begins on.
12807 LINE2 is the line number that the name of the function appears on.
12808 DECLSPECS and DECLARATOR are the parts of the declaration;
12809 they describe the return type and the name of the function,
12810 but twisted together in a fashion that parallels the syntax of C.
12811
12812 This function creates a binding context for the function body
12813 as well as setting up the FUNCTION_DECL in current_function_decl.
12814
12815 Returns a FUNCTION_DECL on success.
12816
12817 If the DECLARATOR is not suitable for a function (it defines a datum
12818 instead), we return 0, which tells yyparse to report a parse error.
12819
12820 May return void_type_node indicating that this method is actually
12821 a friend. See grokfield for more details.
12822
12823 Came here with a `.pushlevel' .
12824
12825 DO NOT MAKE ANY CHANGES TO THIS CODE WITHOUT MAKING CORRESPONDING
12826 CHANGES TO CODE IN `grokfield'. */
e92cc029 12827
8d08fdba 12828tree
c11b6f21
MS
12829start_method (declspecs, declarator)
12830 tree declarator, declspecs;
8d08fdba 12831{
c11b6f21 12832 tree fndecl = grokdeclarator (declarator, declspecs, MEMFUNCDEF, 0,
f30432d7 12833 NULL_TREE);
8d08fdba
MS
12834
12835 /* Something too ugly to handle. */
12836 if (fndecl == NULL_TREE)
12837 return NULL_TREE;
12838
12839 /* Pass friends other than inline friend functions back. */
a1774733 12840 if (fndecl == void_type_node)
8d08fdba
MS
12841 return fndecl;
12842
12843 if (TREE_CODE (fndecl) != FUNCTION_DECL)
12844 /* Not a function, tell parser to report parse error. */
12845 return NULL_TREE;
12846
12847 if (IS_SIGNATURE (current_class_type))
824b9a4c 12848 IS_DEFAULT_IMPLEMENTATION (fndecl) = 1;
8d08fdba
MS
12849
12850 if (DECL_IN_AGGR_P (fndecl))
12851 {
12852 if (IDENTIFIER_ERROR_LOCUS (DECL_ASSEMBLER_NAME (fndecl)) != current_class_type)
12853 {
2c73f9f5
ML
12854 if (DECL_CONTEXT (fndecl)
12855 && TREE_CODE( DECL_CONTEXT (fndecl)) != NAMESPACE_DECL)
8d08fdba
MS
12856 cp_error ("`%D' is already defined in class %s", fndecl,
12857 TYPE_NAME_STRING (DECL_CONTEXT (fndecl)));
12858 }
12859 return void_type_node;
12860 }
12861
faae18ab
MS
12862 DECL_THIS_INLINE (fndecl) = 1;
12863
8926095f 12864 if (flag_default_inline)
8d08fdba
MS
12865 DECL_INLINE (fndecl) = 1;
12866
75650646 12867 if (processing_template_decl)
3ac3d9ea 12868 fndecl = push_template_decl (fndecl);
a0a33927 12869
8d08fdba
MS
12870 /* We read in the parameters on the maybepermanent_obstack,
12871 but we won't be getting back to them until after we
12872 may have clobbered them. So the call to preserve_data
12873 will keep them safe. */
12874 preserve_data ();
12875
12876 if (! DECL_FRIEND_P (fndecl))
12877 {
8d08fdba
MS
12878 if (TREE_CHAIN (fndecl))
12879 {
12880 fndecl = copy_node (fndecl);
12881 TREE_CHAIN (fndecl) = NULL_TREE;
12882 }
12883
12884 if (DECL_CONSTRUCTOR_P (fndecl))
a0a33927
MS
12885 {
12886 if (! grok_ctor_properties (current_class_type, fndecl))
12887 return void_type_node;
12888 }
8d08fdba
MS
12889 else if (IDENTIFIER_OPNAME_P (DECL_NAME (fndecl)))
12890 grok_op_properties (fndecl, DECL_VIRTUAL_P (fndecl), 0);
12891 }
12892
82580166 12893 cp_finish_decl (fndecl, NULL_TREE, NULL_TREE, 0, 0);
8d08fdba
MS
12894
12895 /* Make a place for the parms */
12896 pushlevel (0);
12897 current_binding_level->parm_flag = 1;
12898
12899 DECL_IN_AGGR_P (fndecl) = 1;
12900 return fndecl;
12901}
12902
12903/* Go through the motions of finishing a function definition.
12904 We don't compile this method until after the whole class has
12905 been processed.
12906
12907 FINISH_METHOD must return something that looks as though it
12908 came from GROKFIELD (since we are defining a method, after all).
12909
12910 This is called after parsing the body of the function definition.
12911 STMTS is the chain of statements that makes up the function body.
12912
12913 DECL is the ..._DECL that `start_method' provided. */
12914
12915tree
12916finish_method (decl)
12917 tree decl;
12918{
12919 register tree fndecl = decl;
12920 tree old_initial;
8d08fdba
MS
12921
12922 register tree link;
12923
a1774733 12924 if (decl == void_type_node)
8d08fdba
MS
12925 return decl;
12926
12927 old_initial = DECL_INITIAL (fndecl);
12928
12929 /* Undo the level for the parms (from start_method).
12930 This is like poplevel, but it causes nothing to be
12931 saved. Saving information here confuses symbol-table
12932 output routines. Besides, this information will
12933 be correctly output when this method is actually
12934 compiled. */
12935
12936 /* Clear out the meanings of the local variables of this level;
12937 also record in each decl which block it belongs to. */
12938
12939 for (link = current_binding_level->names; link; link = TREE_CHAIN (link))
12940 {
12941 if (DECL_NAME (link) != NULL_TREE)
12942 IDENTIFIER_LOCAL_VALUE (DECL_NAME (link)) = 0;
12943 my_friendly_assert (TREE_CODE (link) != FUNCTION_DECL, 163);
12944 DECL_CONTEXT (link) = NULL_TREE;
12945 }
12946
12947 /* Restore all name-meanings of the outer levels
12948 that were shadowed by this level. */
12949
12950 for (link = current_binding_level->shadowed; link; link = TREE_CHAIN (link))
12951 IDENTIFIER_LOCAL_VALUE (TREE_PURPOSE (link)) = TREE_VALUE (link);
12952 for (link = current_binding_level->class_shadowed;
12953 link; link = TREE_CHAIN (link))
12954 IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (link)) = TREE_VALUE (link);
12955 for (link = current_binding_level->type_shadowed;
12956 link; link = TREE_CHAIN (link))
2c73f9f5 12957 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (link), TREE_VALUE (link));
8d08fdba
MS
12958
12959 GNU_xref_end_scope ((HOST_WIDE_INT) current_binding_level,
12960 (HOST_WIDE_INT) current_binding_level->level_chain,
12961 current_binding_level->parm_flag,
5566b478 12962 current_binding_level->keep);
8d08fdba
MS
12963
12964 poplevel (0, 0, 0);
12965
12966 DECL_INITIAL (fndecl) = old_initial;
12967
12968 /* We used to check if the context of FNDECL was different from
12969 current_class_type as another way to get inside here. This didn't work
12970 for String.cc in libg++. */
12971 if (DECL_FRIEND_P (fndecl))
12972 {
12973 CLASSTYPE_INLINE_FRIENDS (current_class_type)
12974 = tree_cons (NULL_TREE, fndecl, CLASSTYPE_INLINE_FRIENDS (current_class_type));
12975 decl = void_type_node;
12976 }
12977
12978 return decl;
12979}
12980\f
12981/* Called when a new struct TYPE is defined.
12982 If this structure or union completes the type of any previous
12983 variable declaration, lay it out and output its rtl. */
12984
12985void
12986hack_incomplete_structures (type)
12987 tree type;
12988{
f30432d7 12989 tree *list;
8d08fdba 12990
f30432d7 12991 if (current_binding_level->incomplete == NULL_TREE)
8d08fdba
MS
12992 return;
12993
12994 if (!type) /* Don't do this for class templates. */
12995 return;
12996
f30432d7
MS
12997 for (list = &current_binding_level->incomplete; *list; )
12998 {
12999 tree decl = TREE_VALUE (*list);
a703fb38 13000 if ((decl && TREE_TYPE (decl) == type)
f30432d7
MS
13001 || (TREE_TYPE (decl)
13002 && TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
13003 && TREE_TYPE (TREE_TYPE (decl)) == type))
13004 {
13005 int toplevel = toplevel_bindings_p ();
13006 if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
13007 && TREE_TYPE (TREE_TYPE (decl)) == type)
13008 layout_type (TREE_TYPE (decl));
13009 layout_decl (decl, 0);
13010 rest_of_decl_compilation (decl, NULL_PTR, toplevel, 0);
13011 if (! toplevel)
13012 {
13013 tree cleanup;
13014 expand_decl (decl);
13015 cleanup = maybe_build_cleanup (decl);
13016 expand_decl_init (decl);
e349ee73 13017 if (! expand_decl_cleanup (decl, cleanup))
f30432d7
MS
13018 cp_error ("parser lost in parsing declaration of `%D'",
13019 decl);
13020 }
13021 *list = TREE_CHAIN (*list);
13022 }
13023 else
13024 list = &TREE_CHAIN (*list);
13025 }
8d08fdba
MS
13026}
13027
8d08fdba 13028/* If DECL is of a type which needs a cleanup, build that cleanup here.
c73964b2 13029 See build_delete for information about AUTO_DELETE.
8d08fdba
MS
13030
13031 Don't build these on the momentary obstack; they must live
13032 the life of the binding contour. */
e92cc029 13033
c73964b2
MS
13034static tree
13035maybe_build_cleanup_1 (decl, auto_delete)
13036 tree decl, auto_delete;
8d08fdba
MS
13037{
13038 tree type = TREE_TYPE (decl);
13039 if (TYPE_NEEDS_DESTRUCTOR (type))
13040 {
13041 int temp = 0, flags = LOOKUP_NORMAL|LOOKUP_DESTRUCTOR;
13042 tree rval;
8d08fdba
MS
13043
13044 if (TREE_CODE (decl) != PARM_DECL)
13045 temp = suspend_momentary ();
13046
13047 if (TREE_CODE (type) == ARRAY_TYPE)
13048 rval = decl;
13049 else
13050 {
13051 mark_addressable (decl);
13052 rval = build_unary_op (ADDR_EXPR, decl, 0);
13053 }
13054
13055 /* Optimize for space over speed here. */
13056 if (! TYPE_USES_VIRTUAL_BASECLASSES (type)
13057 || flag_expensive_optimizations)
13058 flags |= LOOKUP_NONVIRTUAL;
13059
c73964b2 13060 rval = build_delete (TREE_TYPE (rval), rval, auto_delete, flags, 0);
8d08fdba
MS
13061
13062 if (TYPE_USES_VIRTUAL_BASECLASSES (type)
13063 && ! TYPE_HAS_DESTRUCTOR (type))
e66d884e
JM
13064 rval = build_compound_expr (expr_tree_cons (NULL_TREE, rval,
13065 build_expr_list (NULL_TREE, build_vbase_delete (type, decl))));
8d08fdba 13066
8d08fdba
MS
13067 if (TREE_CODE (decl) != PARM_DECL)
13068 resume_momentary (temp);
13069
8d08fdba
MS
13070 return rval;
13071 }
13072 return 0;
13073}
c73964b2
MS
13074
13075/* If DECL is of a type which needs a cleanup, build that cleanup
13076 here. The cleanup does free the storage with a call to delete. */
13077
13078tree
13079maybe_build_cleanup_and_delete (decl)
13080 tree decl;
13081{
13082 return maybe_build_cleanup_1 (decl, integer_three_node);
13083}
13084
13085/* If DECL is of a type which needs a cleanup, build that cleanup
13086 here. The cleanup does not free the storage with a call a delete. */
13087
13088tree
13089maybe_build_cleanup (decl)
13090 tree decl;
13091{
13092 return maybe_build_cleanup_1 (decl, integer_two_node);
13093}
8d08fdba
MS
13094\f
13095/* Expand a C++ expression at the statement level.
13096 This is needed to ferret out nodes which have UNKNOWN_TYPE.
13097 The C++ type checker should get all of these out when
13098 expressions are combined with other, type-providing, expressions,
13099 leaving only orphan expressions, such as:
13100
e92cc029 13101 &class::bar; / / takes its address, but does nothing with it. */
8d08fdba 13102
8d08fdba
MS
13103void
13104cplus_expand_expr_stmt (exp)
13105 tree exp;
13106{
5156628f 13107 if (processing_template_decl)
5566b478
MS
13108 {
13109 add_tree (build_min_nt (EXPR_STMT, exp));
13110 return;
13111 }
13112
eb66be0e
MS
13113 /* Arrange for all temps to disappear. */
13114 expand_start_target_temps ();
13115
8d08fdba
MS
13116 if (TREE_TYPE (exp) == unknown_type_node)
13117 {
13118 if (TREE_CODE (exp) == ADDR_EXPR || TREE_CODE (exp) == TREE_LIST)
13119 error ("address of overloaded function with no contextual type information");
13120 else if (TREE_CODE (exp) == COMPONENT_REF)
13121 warning ("useless reference to a member function name, did you forget the ()?");
13122 }
13123 else
13124 {
8d08fdba
MS
13125 if (TREE_CODE (exp) == FUNCTION_DECL)
13126 {
13127 cp_warning ("reference, not call, to function `%D'", exp);
13128 warning ("at this point in file");
13129 }
8d08fdba 13130
63c68bb7
MS
13131#if 0
13132 /* We should do this eventually, but right now this causes regex.o from
13133 libg++ to miscompile, and tString to core dump. */
5b605f68 13134 exp = build1 (CLEANUP_POINT_EXPR, TREE_TYPE (exp), exp);
63c68bb7 13135#endif
b3e401c2
JM
13136
13137 /* Strip unused implicit INDIRECT_REFs of references. */
13138 if (TREE_CODE (exp) == INDIRECT_REF
13139 && TREE_CODE (TREE_TYPE (TREE_OPERAND (exp, 0))) == REFERENCE_TYPE)
13140 exp = TREE_OPERAND (exp, 0);
13141
d2e5ee5c
MS
13142 /* If we don't do this, we end up down inside expand_expr
13143 trying to do TYPE_MODE on the ERROR_MARK, and really
13144 go outside the bounds of the type. */
13145 if (exp != error_mark_node)
13146 expand_expr_stmt (break_out_cleanups (exp));
8d08fdba
MS
13147 }
13148
13149 /* Clean up any pending cleanups. This happens when a function call
13150 returns a cleanup-needing value that nobody uses. */
eb66be0e 13151 expand_end_target_temps ();
8d08fdba
MS
13152}
13153
13154/* When a stmt has been parsed, this function is called.
13155
13156 Currently, this function only does something within a
13157 constructor's scope: if a stmt has just assigned to this,
13158 and we are in a derived class, we call `emit_base_init'. */
13159
13160void
13161finish_stmt ()
13162{
13163 extern struct nesting *cond_stack, *loop_stack, *case_stack;
13164
13165
13166 if (current_function_assigns_this
13167 || ! current_function_just_assigned_this)
13168 return;
13169 if (DECL_CONSTRUCTOR_P (current_function_decl))
13170 {
13171 /* Constructors must wait until we are out of control
13172 zones before calling base constructors. */
13173 if (cond_stack || loop_stack || case_stack)
13174 return;
a9aedbc2 13175 expand_expr_stmt (base_init_expr);
8d08fdba
MS
13176 check_base_init (current_class_type);
13177 }
13178 current_function_assigns_this = 1;
8d08fdba
MS
13179}
13180
8d08fdba 13181/* Change a static member function definition into a FUNCTION_TYPE, instead
700f8a87
MS
13182 of the METHOD_TYPE that we create when it's originally parsed.
13183
13184 WARNING: DO NOT pass &TREE_TYPE (decl) to FN or &TYPE_ARG_TYPES
13185 (TREE_TYPE (decl)) to ARGTYPES, as doing so will corrupt the types of
13186 other decls. Either pass the addresses of local variables or NULL. */
13187
8857f91e 13188void
700f8a87
MS
13189revert_static_member_fn (decl, fn, argtypes)
13190 tree *decl, *fn, *argtypes;
8d08fdba 13191{
700f8a87
MS
13192 tree tmp;
13193 tree function = fn ? *fn : TREE_TYPE (*decl);
13194 tree args = argtypes ? *argtypes : TYPE_ARG_TYPES (function);
8d08fdba 13195
f30432d7
MS
13196 if (TYPE_READONLY (TREE_TYPE (TREE_VALUE (args))))
13197 cp_error ("static member function `%#D' declared const", *decl);
13198 if (TYPE_VOLATILE (TREE_TYPE (TREE_VALUE (args))))
13199 cp_error ("static member function `%#D' declared volatile", *decl);
13200
700f8a87
MS
13201 args = TREE_CHAIN (args);
13202 tmp = build_function_type (TREE_TYPE (function), args);
8d08fdba
MS
13203 tmp = build_type_variant (tmp, TYPE_READONLY (function),
13204 TYPE_VOLATILE (function));
f30432d7 13205 tmp = build_exception_variant (tmp,
8d08fdba
MS
13206 TYPE_RAISES_EXCEPTIONS (function));
13207 TREE_TYPE (*decl) = tmp;
f30432d7
MS
13208 if (DECL_ARGUMENTS (*decl))
13209 DECL_ARGUMENTS (*decl) = TREE_CHAIN (DECL_ARGUMENTS (*decl));
8d08fdba 13210 DECL_STATIC_FUNCTION_P (*decl) = 1;
700f8a87
MS
13211 if (fn)
13212 *fn = tmp;
13213 if (argtypes)
13214 *argtypes = args;
8d08fdba 13215}
a4443a08
MS
13216
13217int
13218id_in_current_class (id)
13219 tree id;
13220{
13221 return !!purpose_member (id, class_binding_level->class_shadowed);
13222}
db5ae43f
MS
13223
13224struct cp_function
13225{
13226 int returns_value;
13227 int returns_null;
13228 int warn_about_return_type;
db5ae43f
MS
13229 int assigns_this;
13230 int just_assigned_this;
13231 int parms_stored;
7215f9a0 13232 int temp_name_counter;
db5ae43f
MS
13233 tree named_labels;
13234 tree shadowed_labels;
13235 tree ctor_label;
13236 tree dtor_label;
72b7eeff 13237 rtx last_dtor_insn;
b87692e5 13238 rtx last_parm_cleanup_insn;
79ff2c6c
MS
13239 tree base_init_list;
13240 tree member_init_list;
a9aedbc2 13241 tree base_init_expr;
4ac14744
MS
13242 tree current_class_ptr;
13243 tree current_class_ref;
db5ae43f
MS
13244 rtx result_rtx;
13245 struct cp_function *next;
13246 struct binding_level *binding_level;
42976354 13247 int static_labelno;
db5ae43f
MS
13248};
13249
bd6dd845 13250static struct cp_function *cp_function_chain;
db5ae43f 13251
7215f9a0
MS
13252extern int temp_name_counter;
13253
db5ae43f
MS
13254/* Save and reinitialize the variables
13255 used during compilation of a C++ function. */
13256
13257void
28cbf42c
MS
13258push_cp_function_context (context)
13259 tree context;
db5ae43f
MS
13260{
13261 struct cp_function *p
13262 = (struct cp_function *) xmalloc (sizeof (struct cp_function));
13263
28cbf42c 13264 push_function_context_to (context);
db5ae43f
MS
13265
13266 p->next = cp_function_chain;
13267 cp_function_chain = p;
13268
13269 p->named_labels = named_labels;
13270 p->shadowed_labels = shadowed_labels;
13271 p->returns_value = current_function_returns_value;
13272 p->returns_null = current_function_returns_null;
13273 p->warn_about_return_type = warn_about_return_type;
db5ae43f
MS
13274 p->binding_level = current_binding_level;
13275 p->ctor_label = ctor_label;
13276 p->dtor_label = dtor_label;
72b7eeff 13277 p->last_dtor_insn = last_dtor_insn;
b87692e5 13278 p->last_parm_cleanup_insn = last_parm_cleanup_insn;
db5ae43f
MS
13279 p->assigns_this = current_function_assigns_this;
13280 p->just_assigned_this = current_function_just_assigned_this;
13281 p->parms_stored = current_function_parms_stored;
13282 p->result_rtx = original_result_rtx;
a9aedbc2 13283 p->base_init_expr = base_init_expr;
7215f9a0 13284 p->temp_name_counter = temp_name_counter;
79ff2c6c
MS
13285 p->base_init_list = current_base_init_list;
13286 p->member_init_list = current_member_init_list;
4ac14744
MS
13287 p->current_class_ptr = current_class_ptr;
13288 p->current_class_ref = current_class_ref;
42976354 13289 p->static_labelno = static_labelno;
db5ae43f
MS
13290}
13291
13292/* Restore the variables used during compilation of a C++ function. */
13293
13294void
28cbf42c
MS
13295pop_cp_function_context (context)
13296 tree context;
db5ae43f
MS
13297{
13298 struct cp_function *p = cp_function_chain;
13299 tree link;
13300
13301 /* Bring back all the labels that were shadowed. */
13302 for (link = shadowed_labels; link; link = TREE_CHAIN (link))
13303 if (DECL_NAME (TREE_VALUE (link)) != 0)
13304 SET_IDENTIFIER_LABEL_VALUE (DECL_NAME (TREE_VALUE (link)),
13305 TREE_VALUE (link));
13306
28cbf42c 13307 pop_function_context_from (context);
db5ae43f
MS
13308
13309 cp_function_chain = p->next;
13310
13311 named_labels = p->named_labels;
13312 shadowed_labels = p->shadowed_labels;
13313 current_function_returns_value = p->returns_value;
13314 current_function_returns_null = p->returns_null;
13315 warn_about_return_type = p->warn_about_return_type;
db5ae43f
MS
13316 current_binding_level = p->binding_level;
13317 ctor_label = p->ctor_label;
13318 dtor_label = p->dtor_label;
72b7eeff 13319 last_dtor_insn = p->last_dtor_insn;
b87692e5 13320 last_parm_cleanup_insn = p->last_parm_cleanup_insn;
db5ae43f
MS
13321 current_function_assigns_this = p->assigns_this;
13322 current_function_just_assigned_this = p->just_assigned_this;
13323 current_function_parms_stored = p->parms_stored;
13324 original_result_rtx = p->result_rtx;
a9aedbc2 13325 base_init_expr = p->base_init_expr;
7215f9a0 13326 temp_name_counter = p->temp_name_counter;
79ff2c6c
MS
13327 current_base_init_list = p->base_init_list;
13328 current_member_init_list = p->member_init_list;
4ac14744
MS
13329 current_class_ptr = p->current_class_ptr;
13330 current_class_ref = p->current_class_ref;
42976354 13331 static_labelno = p->static_labelno;
db5ae43f
MS
13332
13333 free (p);
13334}
ebfc180f 13335
5566b478
MS
13336int
13337in_function_p ()
13338{
13339 return function_depth != 0;
13340}
This page took 2.518213 seconds and 5 git commands to generate.