]> gcc.gnu.org Git - gcc.git/blame - gcc/cp/decl.c
cp-tree.h (build_binary_op): Remove unneeded parameter.
[gcc.git] / gcc / cp / decl.c
CommitLineData
e5e809f4 1/* Process declarations and variables for C compiler.
c12f5242 2 Copyright (C) 1988, 92-98, 1999 Free Software Foundation, Inc.
e5e809f4 3 Contributed by Michael Tiemann (tiemann@cygnus.com)
8d08fdba
MS
4
5This file is part of GNU CC.
6
7GNU CC is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation; either version 2, or (at your option)
10any later version.
11
12GNU CC is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with GNU CC; see the file COPYING. If not, write to
e9fa0c7c
RK
19the Free Software Foundation, 59 Temple Place - Suite 330,
20Boston, MA 02111-1307, USA. */
8d08fdba
MS
21
22
23/* Process declarations and symbol lookup for C front end.
24 Also constructs types; the standard scalar types at initialization,
25 and structure, union, array and enum types when they are declared. */
26
27/* ??? not all decl nodes are given the most useful possible
28 line numbers. For example, the CONST_DECLs for enum values. */
29
8d08fdba 30#include "config.h"
8d052bc7 31#include "system.h"
8d08fdba
MS
32#include "tree.h"
33#include "rtl.h"
34#include "flags.h"
35#include "cp-tree.h"
36#include "decl.h"
37#include "lex.h"
8d08fdba
MS
38#include <signal.h>
39#include "obstack.h"
72b7eeff 40#include "defaults.h"
49c249e1
JM
41#include "output.h"
42#include "except.h"
54f92bfb 43#include "toplev.h"
7ddedda4 44#include "../hash.h"
8d08fdba
MS
45
46#define obstack_chunk_alloc xmalloc
47#define obstack_chunk_free free
48
8d2733ca
MS
49extern tree builtin_return_address_fndecl;
50
8d08fdba 51extern struct obstack permanent_obstack;
ae0a6181 52extern struct obstack* saveable_obstack;
8d08fdba
MS
53
54extern int current_class_depth;
55
e1cd6e56
MS
56extern tree static_ctors, static_dtors;
57
42976354
BK
58extern int static_labelno;
59
30394414
JM
60extern tree current_namespace;
61extern tree global_namespace;
62
62c154ed 63extern void (*print_error_function) PROTO((char *));
e5dc5fb2 64extern int (*valid_lang_attribute) PROTO ((tree, tree, tree, tree));
62c154ed 65
8d08fdba
MS
66/* Stack of places to restore the search obstack back to. */
67
68/* Obstack used for remembering local class declarations (like
69 enums and static (const) members. */
70#include "stack.h"
5566b478 71struct obstack decl_obstack;
8d08fdba
MS
72static struct stack_level *decl_stack;
73
74#ifndef CHAR_TYPE_SIZE
75#define CHAR_TYPE_SIZE BITS_PER_UNIT
76#endif
77
78#ifndef SHORT_TYPE_SIZE
79#define SHORT_TYPE_SIZE (BITS_PER_UNIT * MIN ((UNITS_PER_WORD + 1) / 2, 2))
80#endif
81
82#ifndef INT_TYPE_SIZE
83#define INT_TYPE_SIZE BITS_PER_WORD
84#endif
85
86#ifndef LONG_TYPE_SIZE
87#define LONG_TYPE_SIZE BITS_PER_WORD
88#endif
89
90#ifndef LONG_LONG_TYPE_SIZE
91#define LONG_LONG_TYPE_SIZE (BITS_PER_WORD * 2)
92#endif
93
94#ifndef WCHAR_UNSIGNED
95#define WCHAR_UNSIGNED 0
96#endif
97
98#ifndef FLOAT_TYPE_SIZE
99#define FLOAT_TYPE_SIZE BITS_PER_WORD
100#endif
101
102#ifndef DOUBLE_TYPE_SIZE
103#define DOUBLE_TYPE_SIZE (BITS_PER_WORD * 2)
104#endif
105
106#ifndef LONG_DOUBLE_TYPE_SIZE
107#define LONG_DOUBLE_TYPE_SIZE (BITS_PER_WORD * 2)
108#endif
109
255512c1 110#ifndef BOOL_TYPE_SIZE
e1cd6e56 111#ifdef SLOW_BYTE_ACCESS
d2e5ee5c 112#define BOOL_TYPE_SIZE ((SLOW_BYTE_ACCESS) ? (POINTER_SIZE) : (CHAR_TYPE_SIZE))
e1cd6e56 113#else
d2e5ee5c 114#define BOOL_TYPE_SIZE CHAR_TYPE_SIZE
e1cd6e56 115#endif
255512c1
JM
116#endif
117
8d08fdba
MS
118/* We let tm.h override the types used here, to handle trivial differences
119 such as the choice of unsigned int or long unsigned int for size_t.
120 When machines start needing nontrivial differences in the size type,
121 it would be best to do something here to figure out automatically
122 from other information what type to use. */
123
124#ifndef SIZE_TYPE
125#define SIZE_TYPE "long unsigned int"
126#endif
127
128#ifndef PTRDIFF_TYPE
129#define PTRDIFF_TYPE "long int"
130#endif
131
132#ifndef WCHAR_TYPE
133#define WCHAR_TYPE "int"
134#endif
135
8d08fdba
MS
136static tree grokparms PROTO((tree, int));
137static tree lookup_nested_type PROTO((tree, tree));
d8e178a0 138static const char *redeclaration_error_message PROTO((tree, tree));
8d08fdba 139
49c249e1
JM
140static struct stack_level *push_decl_level PROTO((struct stack_level *,
141 struct obstack *));
142static void push_binding_level PROTO((struct binding_level *, int,
143 int));
144static void pop_binding_level PROTO((void));
145static void suspend_binding_level PROTO((void));
146static void resume_binding_level PROTO((struct binding_level *));
147static struct binding_level *make_binding_level PROTO((void));
49c249e1 148static void declare_namespace_level PROTO((void));
1ddb2906 149static void signal_catch PROTO((int)) ATTRIBUTE_NORETURN;
49c249e1 150static void storedecls PROTO((tree));
49c249e1
JM
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));
3e3f722c 159static tree lookup_name_real PROTO((tree, int, int, int));
49c249e1 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 *));
d8e178a0
KG
170static void record_builtin_type PROTO((enum rid, const char *, tree));
171static void record_unknown_type PROTO((tree, const char *));
172static int member_function_or_else PROTO((tree, tree, const char *));
173static void bad_specifiers PROTO((tree, const char *, int, int, int, int,
49c249e1 174 int));
62c154ed 175static void lang_print_error_function PROTO((char *));
39c01e4c 176static tree maybe_process_template_type_declaration PROTO((tree, int, struct binding_level*));
91063b51 177static void check_for_uninitialized_const_var PROTO((tree));
7ddedda4
MM
178static unsigned long typename_hash PROTO((hash_table_key));
179static boolean typename_compare PROTO((hash_table_key, hash_table_key));
f181d4ae 180static void push_binding PROTO((tree, tree, struct binding_level*));
8f032717 181static int add_binding PROTO((tree, tree));
d8f8dca1 182static void pop_binding PROTO((tree, tree));
297e73d8 183static tree local_variable_p PROTO((tree));
d8e178a0
KG
184static tree find_binding PROTO((tree, tree));
185static tree select_decl PROTO((tree, int));
186static tree unqualified_namespace_lookup PROTO((tree, int));
187static int lookup_flags PROTO((int, int));
188static tree qualify_lookup PROTO((tree, int));
189static tree record_builtin_java_type PROTO((const char *, int));
190static const char *tag_name PROTO((enum tag_types code));
8f032717
MM
191static void find_class_binding_level PROTO((void));
192static struct binding_level *innermost_nonclass_level PROTO((void));
b74a0560 193static tree poplevel_class PROTO((void));
235f734d 194static void warn_about_implicit_typename_lookup PROTO((tree, tree));
8d08fdba 195
69ac77ce
JL
196#if defined (DEBUG_CP_BINDING_LEVELS)
197static void indent PROTO((void));
198#endif
199
2c73f9f5 200/* A node which has tree code ERROR_MARK, and whose type is itself.
8d08fdba
MS
201 All erroneous expressions are replaced with this node. All functions
202 that accept nodes as arguments should avoid generating error messages
203 if this node is one of the arguments, since it is undesirable to get
204 multiple error messages from one error in the input. */
205
206tree error_mark_node;
207
208/* Erroneous argument lists can use this *IFF* they do not modify it. */
209tree error_mark_list;
210
211/* INTEGER_TYPE and REAL_TYPE nodes for the standard data types */
212
213tree short_integer_type_node;
214tree integer_type_node;
215tree long_integer_type_node;
216tree long_long_integer_type_node;
217
218tree short_unsigned_type_node;
219tree unsigned_type_node;
220tree long_unsigned_type_node;
221tree long_long_unsigned_type_node;
222
223tree ptrdiff_type_node;
224
225tree unsigned_char_type_node;
226tree signed_char_type_node;
227tree char_type_node;
228tree wchar_type_node;
229tree signed_wchar_type_node;
230tree unsigned_wchar_type_node;
231
f376e137
MS
232tree wchar_decl_node;
233
8d08fdba
MS
234tree float_type_node;
235tree double_type_node;
236tree long_double_type_node;
237
37c46b43
MS
238tree complex_integer_type_node;
239tree complex_float_type_node;
240tree complex_double_type_node;
241tree complex_long_double_type_node;
242
8d08fdba
MS
243tree intQI_type_node;
244tree intHI_type_node;
245tree intSI_type_node;
246tree intDI_type_node;
946dc1c8 247#if HOST_BITS_PER_WIDE_INT >= 64
f7554e8c 248tree intTI_type_node;
946dc1c8 249#endif
8d08fdba
MS
250
251tree unsigned_intQI_type_node;
252tree unsigned_intHI_type_node;
253tree unsigned_intSI_type_node;
254tree unsigned_intDI_type_node;
946dc1c8 255#if HOST_BITS_PER_WIDE_INT >= 64
f7554e8c 256tree unsigned_intTI_type_node;
946dc1c8 257#endif
8d08fdba 258
eff71ab0
PB
259tree java_byte_type_node;
260tree java_short_type_node;
261tree java_int_type_node;
262tree java_long_type_node;
263tree java_float_type_node;
264tree java_double_type_node;
265tree java_char_type_node;
266tree java_boolean_type_node;
267
2c73f9f5 268/* A VOID_TYPE node, and the same, packaged in a TREE_LIST. */
8d08fdba
MS
269
270tree void_type_node, void_list_node;
271tree void_zero_node;
272
273/* Nodes for types `void *' and `const void *'. */
274
bd6dd845 275tree ptr_type_node;
6c20b7e9 276tree const_ptr_type_node;
8d08fdba
MS
277
278/* Nodes for types `char *' and `const char *'. */
279
280tree string_type_node, const_string_type_node;
281
282/* Type `char[256]' or something like it.
283 Used when an array of char is needed and the size is irrelevant. */
284
285tree char_array_type_node;
286
287/* Type `int[256]' or something like it.
288 Used when an array of int needed and the size is irrelevant. */
289
290tree int_array_type_node;
291
292/* Type `wchar_t[256]' or something like it.
293 Used when a wide string literal is created. */
294
295tree wchar_array_type_node;
296
2986ae00 297/* The bool data type, and constants */
255512c1 298tree boolean_type_node, boolean_true_node, boolean_false_node;
2986ae00 299
2c73f9f5 300/* Type `int ()' -- used for implicit declaration of functions. */
8d08fdba
MS
301
302tree default_function_type;
303
2c73f9f5 304/* Function types `double (double)' and `double (double, double)', etc. */
8d08fdba 305
bd6dd845
MS
306static tree double_ftype_double, double_ftype_double_double;
307static tree int_ftype_int, long_ftype_long;
308static tree float_ftype_float;
309static tree ldouble_ftype_ldouble;
8d08fdba 310
8d08fdba 311/* Function type `int (const void *, const void *, size_t)' */
5566b478 312static tree int_ftype_cptr_cptr_sizet;
8d08fdba
MS
313
314/* C++ extensions */
700f8a87 315tree vtable_entry_type;
8d08fdba 316tree delta_type_node;
db5ae43f 317#if 0
e92cc029 318/* Old rtti stuff. */
db5ae43f
MS
319tree __baselist_desc_type_node;
320tree __i_desc_type_node, __m_desc_type_node;
8d08fdba 321tree __t_desc_array_type, __i_desc_array_type, __m_desc_array_type;
db5ae43f 322#endif
5566b478
MS
323tree __t_desc_type_node;
324#if 0
325tree __tp_desc_type_node;
326#endif
db5ae43f
MS
327tree __access_mode_type_node;
328tree __bltn_desc_type_node, __user_desc_type_node, __class_desc_type_node;
329tree __ptr_desc_type_node, __attr_desc_type_node, __func_desc_type_node;
330tree __ptmf_desc_type_node, __ptmd_desc_type_node;
331#if 0
332/* Not needed yet? May be needed one day? */
333tree __bltn_desc_array_type, __user_desc_array_type, __class_desc_array_type;
334tree __ptr_desc_array_type, __attr_dec_array_type, __func_desc_array_type;
335tree __ptmf_desc_array_type, __ptmd_desc_array_type;
336#endif
337
2c73f9f5
ML
338/* Indicates that there is a type value in some namespace, although
339 that is not necessarily in scope at the moment. */
340
341static tree global_type_node;
342
8d08fdba
MS
343tree class_star_type_node;
344tree class_type_node, record_type_node, union_type_node, enum_type_node;
db5ae43f 345tree unknown_type_node;
8d08fdba
MS
346tree opaque_type_node, signature_type_node;
347tree sigtable_entry_type;
8d08fdba 348
8d08fdba
MS
349/* Array type `vtable_entry_type[]' */
350tree vtbl_type_node;
849da744 351tree vtbl_ptr_type_node;
8d08fdba 352
6633d636
MS
353/* namespace std */
354tree std_node;
2c73f9f5 355int in_std = 0;
6633d636 356
3e3f722c
ML
357/* Expect only namespace names now. */
358static int only_namespace_names;
359
8d08fdba
MS
360/* In a destructor, the point at which all derived class destroying
361 has been done, just before any base class destroying will be done. */
362
363tree dtor_label;
364
72b7eeff
MS
365/* In a destructor, the last insn emitted after the start of the
366 function and the parms. */
367
bd6dd845 368static rtx last_dtor_insn;
72b7eeff 369
b87692e5 370/* In a constructor, the last insn emitted after the start of the
9664b89e
JM
371 function and the parms, the exception specification and any
372 function-try-block. The constructor initializers are emitted after
373 this insn. */
b87692e5
MS
374
375static rtx last_parm_cleanup_insn;
376
8d08fdba
MS
377/* In a constructor, the point at which we are ready to return
378 the pointer to the initialized object. */
379
380tree ctor_label;
381
8d08fdba
MS
382/* A FUNCTION_DECL which can call `abort'. Not necessarily the
383 one that the user will declare, but sufficient to be called
384 by routines that want to abort the program. */
385
386tree abort_fndecl;
387
94e098d1
MM
388/* A FUNCTION_DECL for the default `::operator delete'. */
389
390tree global_delete_fndecl;
391
8d08fdba
MS
392extern rtx cleanup_label, return_label;
393
394/* If original DECL_RESULT of current function was a register,
395 but due to being an addressable named return value, would up
396 on the stack, this variable holds the named return value's
397 original location. */
bd6dd845 398static rtx original_result_rtx;
8d08fdba
MS
399
400/* Sequence of insns which represents base initialization. */
a9aedbc2 401tree base_init_expr;
8d08fdba
MS
402
403/* C++: Keep these around to reduce calls to `get_identifier'.
404 Identifiers for `this' in member functions and the auto-delete
405 parameter for destructors. */
406tree this_identifier, in_charge_identifier;
fc378698 407tree ctor_identifier, dtor_identifier;
e92cc029 408/* Used in pointer to member functions, in vtables, and in sigtables. */
8d08fdba 409tree pfn_identifier, index_identifier, delta_identifier, delta2_identifier;
9dd70aa4 410tree pfn_or_delta2_identifier, tag_identifier;
5566b478 411tree vt_off_identifier;
8d08fdba 412
e349ee73
MS
413struct named_label_list
414{
415 struct binding_level *binding_level;
416 tree names_in_scope;
417 tree label_decl;
418 char *filename_o_goto;
419 int lineno_o_goto;
420 struct named_label_list *next;
421};
422
8d08fdba
MS
423/* A list (chain of TREE_LIST nodes) of named label uses.
424 The TREE_PURPOSE field is the list of variables defined
38e01259 425 in the label's scope defined at the point of use.
8d08fdba
MS
426 The TREE_VALUE field is the LABEL_DECL used.
427 The TREE_TYPE field holds `current_binding_level' at the
428 point of the label's use.
429
e349ee73
MS
430 BWAHAHAAHAHahhahahahaah. No, no, no, said the little chicken.
431
432 Look at the pretty struct named_label_list. See the pretty struct
433 with the pretty named fields that describe what they do. See the
434 pretty lack of gratuitous casts. Notice the code got a lot cleaner.
435
8d08fdba
MS
436 Used only for jumps to as-yet undefined labels, since
437 jumps to defined labels can have their validity checked
438 by stmt.c. */
439
e349ee73 440static struct named_label_list *named_label_uses = NULL;
8d08fdba
MS
441
442/* A list of objects which have constructors or destructors
443 which reside in the global scope. The decl is stored in
444 the TREE_VALUE slot and the initializer is stored
445 in the TREE_PURPOSE slot. */
446tree static_aggregates;
447
8d08fdba
MS
448/* -- end of C++ */
449
450/* Two expressions that are constants with value zero.
451 The first is of type `int', the second of type `void *'. */
452
453tree integer_zero_node;
454tree null_pointer_node;
455
03d0f4af
MM
456/* The value for __null (NULL), namely, a zero of an integer type with
457 the same number of bits as a pointer. */
d11ad92e
MS
458tree null_node;
459
8d08fdba
MS
460/* A node for the integer constants 1, 2, and 3. */
461
462tree integer_one_node, integer_two_node, integer_three_node;
463
8d08fdba
MS
464/* While defining an enum type, this is 1 plus the last enumerator
465 constant value. */
466
467static tree enum_next_value;
468
7177d104
MS
469/* Nonzero means that there was overflow computing enum_next_value. */
470
471static int enum_overflow;
472
8d08fdba
MS
473/* Parsing a function declarator leaves a list of parameter names
474 or a chain or parameter decls here. */
475
476tree last_function_parms;
477
478/* Parsing a function declarator leaves here a chain of structure
479 and enum types declared in the parmlist. */
480
481static tree last_function_parm_tags;
482
483/* After parsing the declarator that starts a function definition,
484 `start_function' puts here the list of parameter names or chain of decls.
485 `store_parm_decls' finds it here. */
486
487static tree current_function_parms;
488
489/* Similar, for last_function_parm_tags. */
490static tree current_function_parm_tags;
491
492/* A list (chain of TREE_LIST nodes) of all LABEL_DECLs in the function
493 that have names. Here so we can clear out their names' definitions
494 at the end of the function. */
495
496static tree named_labels;
497
498/* A list of LABEL_DECLs from outer contexts that are currently shadowed. */
499
500static tree shadowed_labels;
501
8d08fdba
MS
502/* The FUNCTION_DECL for the function currently being compiled,
503 or 0 if between functions. */
504tree current_function_decl;
505
506/* Set to 0 at beginning of a function definition, set to 1 if
507 a return statement that specifies a return value is seen. */
508
509int current_function_returns_value;
510
511/* Set to 0 at beginning of a function definition, set to 1 if
512 a return statement with no argument is seen. */
513
514int current_function_returns_null;
515
516/* Set to 0 at beginning of a function definition, and whenever
517 a label (case or named) is defined. Set to value of expression
518 returned from function when that value can be transformed into
519 a named return value. */
520
521tree current_function_return_value;
522
8d08fdba
MS
523/* Nonzero means give `double' the same size as `float'. */
524
525extern int flag_short_double;
526
527/* Nonzero means don't recognize any builtin functions. */
528
529extern int flag_no_builtin;
530
00595019
MS
531/* Nonzero means don't recognize the non-ANSI builtin functions.
532 -ansi sets this. */
533
534extern int flag_no_nonansi_builtin;
535
e1cd6e56
MS
536/* Nonzero means enable obscure ANSI features and disable GNU extensions
537 that might cause ANSI-compliant code to be miscompiled. */
8d08fdba
MS
538
539extern int flag_ansi;
540
541/* Nonzero if we want to support huge (> 2^(sizeof(short)*8-1) bytes)
e92cc029 542 objects. */
8d08fdba
MS
543extern int flag_huge_objects;
544
545/* Nonzero if we want to conserve space in the .o files. We do this
546 by putting uninitialized data and runtime initialized data into
ddd5a7c1 547 .common instead of .data at the expense of not flagging multiple
8d08fdba
MS
548 definitions. */
549extern int flag_conserve_space;
550
551/* Pointers to the base and current top of the language name stack. */
552
553extern tree *current_lang_base, *current_lang_stack;
554\f
51c184be 555/* C and C++ flags are in decl2.c. */
8d08fdba
MS
556
557/* Set to 0 at beginning of a constructor, set to 1
558 if that function does an allocation before referencing its
559 instance variable. */
5566b478 560static int current_function_assigns_this;
8d08fdba
MS
561int current_function_just_assigned_this;
562
563/* Set to 0 at beginning of a function. Set non-zero when
564 store_parm_decls is called. Don't call store_parm_decls
565 if this flag is non-zero! */
566int current_function_parms_stored;
567
51c184be 568/* Flag used when debugging spew.c */
8d08fdba
MS
569
570extern int spew_debug;
571
572/* This is a copy of the class_shadowed list of the previous class binding
573 contour when at global scope. It's used to reset IDENTIFIER_CLASS_VALUEs
574 when entering another class scope (i.e. a cache miss). */
575extern tree previous_class_values;
576
e92cc029
MS
577/* A expression of value 0 with the same precision as a sizetype
578 node, but signed. */
579tree signed_size_zero_node;
580
0c8feefe
MM
581/* The name of the anonymous namespace, throughout this translation
582 unit. */
583tree anonymous_namespace_name;
584
8d08fdba
MS
585\f
586/* Allocate a level of searching. */
e92cc029 587
824b9a4c 588static
8d08fdba
MS
589struct stack_level *
590push_decl_level (stack, obstack)
591 struct stack_level *stack;
592 struct obstack *obstack;
593{
594 struct stack_level tem;
595 tem.prev = stack;
596
597 return push_stack_level (obstack, (char *)&tem, sizeof (tem));
598}
599\f
600/* For each binding contour we allocate a binding_level structure
e92cc029
MS
601 which records the names defined in that contour.
602 Contours include:
603 0) the global one
604 1) one for each function definition,
605 where internal declarations of the parameters appear.
606 2) one for each compound statement,
607 to record its declarations.
608
609 The current meaning of a name can be found by searching the levels
610 from the current one out to the global one.
611
612 Off to the side, may be the class_binding_level. This exists only
613 to catch class-local declarations. It is otherwise nonexistent.
614
615 Also there may be binding levels that catch cleanups that must be
d8f8dca1
MM
616 run when exceptions occur. Thus, to see whether a name is bound in
617 the current scope, it is not enough to look in the
618 CURRENT_BINDING_LEVEL. You should use lookup_name_current_level
619 instead. */
8d08fdba
MS
620
621/* Note that the information in the `names' component of the global contour
622 is duplicated in the IDENTIFIER_GLOBAL_VALUEs of all identifiers. */
623
624struct binding_level
625 {
626 /* A chain of _DECL nodes for all variables, constants, functions,
e92cc029 627 and typedef types. These are in the reverse of the order
f181d4ae
MM
628 supplied. There may be OVERLOADs on this list, too, but they
629 are wrapped in TREE_LISTs; the TREE_VALUE is the OVERLOAD. */
8d08fdba
MS
630 tree names;
631
e92cc029
MS
632 /* A list of structure, union and enum definitions, for looking up
633 tag names.
634 It is a chain of TREE_LIST nodes, each of whose TREE_PURPOSE is a name,
635 or NULL_TREE; and whose TREE_VALUE is a RECORD_TYPE, UNION_TYPE,
636 or ENUMERAL_TYPE node.
637
638 C++: the TREE_VALUE nodes can be simple types for
639 component_bindings. */
8d08fdba
MS
640 tree tags;
641
2c73f9f5
ML
642 /* A list of USING_DECL nodes. */
643 tree usings;
644
ea9635c7
ML
645 /* A list of used namespaces. PURPOSE is the namespace,
646 VALUE the common ancestor with this binding_level's namespace. */
647 tree using_directives;
648
f181d4ae
MM
649 /* If this binding level is the binding level for a class, then
650 class_shadowed is a TREE_LIST. The TREE_PURPOSE of each node
651 is the name of an entity bound in the class; the TREE_VALUE is
652 the IDENTIFIER_CLASS_VALUE before we entered the class. Thus,
653 when leaving class scope, we can restore the
d8f8dca1
MM
654 IDENTIFIER_CLASS_VALUE by walking this list. The TREE_TYPE is
655 the DECL bound by this name in the class. */
8d08fdba
MS
656 tree class_shadowed;
657
f181d4ae
MM
658 /* Similar to class_shadowed, but for IDENTIFIER_TYPE_VALUE, and
659 is used for all binding levels. */
8d08fdba
MS
660 tree type_shadowed;
661
662 /* For each level (except not the global one),
663 a chain of BLOCK nodes for all the levels
664 that were entered and exited one level down. */
665 tree blocks;
666
667 /* The BLOCK node for this level, if one has been preallocated.
668 If 0, the BLOCK is allocated (if needed) when the level is popped. */
669 tree this_block;
670
671 /* The binding level which this one is contained in (inherits from). */
672 struct binding_level *level_chain;
673
f30432d7 674 /* List of decls in `names' that have incomplete
8d08fdba 675 structure or union types. */
f30432d7 676 tree incomplete;
8d08fdba 677
8d6e462b 678 /* List of VAR_DECLS saved from a previous for statement.
abc95ed3 679 These would be dead in ANSI-conforming code, but might
f181d4ae
MM
680 be referenced in ARM-era code. These are stored in a
681 TREE_LIST; the TREE_VALUE is the actual declaration. */
8d6e462b
PB
682 tree dead_vars_from_for;
683
8d08fdba
MS
684 /* 1 for the level that holds the parameters of a function.
685 2 for the level that holds a class declaration.
686 3 for levels that hold parameter declarations. */
687 unsigned parm_flag : 4;
688
689 /* 1 means make a BLOCK for this level regardless of all else.
690 2 for temporary binding contours created by the compiler. */
691 unsigned keep : 3;
692
693 /* Nonzero if this level "doesn't exist" for tags. */
694 unsigned tag_transparent : 1;
695
696 /* Nonzero if this level can safely have additional
697 cleanup-needing variables added to it. */
698 unsigned more_cleanups_ok : 1;
699 unsigned have_cleanups : 1;
700
5566b478
MS
701 /* Nonzero if this level is for storing the decls for template
702 parameters and generic decls; these decls will be discarded and
703 replaced with a TEMPLATE_DECL. */
8d08fdba
MS
704 unsigned pseudo_global : 1;
705
a9aedbc2
MS
706 /* This is set for a namespace binding level. */
707 unsigned namespace_p : 1;
708
2ee887f2 709 /* True if this level is that of a for-statement where we need to
d22c8596 710 worry about ambiguous (ARM or ANSI) scope rules. */
8d6e462b
PB
711 unsigned is_for_scope : 1;
712
5566b478 713 /* Two bits left for this word. */
8d08fdba
MS
714
715#if defined(DEBUG_CP_BINDING_LEVELS)
716 /* Binding depth at which this level began. */
717 unsigned binding_depth;
718#endif /* defined(DEBUG_CP_BINDING_LEVELS) */
719 };
720
721#define NULL_BINDING_LEVEL ((struct binding_level *) NULL)
722
8f032717 723/* The binding level currently in effect. */
8d08fdba
MS
724
725static struct binding_level *current_binding_level;
726
727/* The binding level of the current class, if any. */
728
729static struct binding_level *class_binding_level;
730
8d08fdba
MS
731/* A chain of binding_level structures awaiting reuse. */
732
733static struct binding_level *free_binding_level;
734
735/* The outermost binding level, for names of file scope.
736 This is created when the compiler is started and exists
737 through the entire run. */
738
739static struct binding_level *global_binding_level;
740
741/* Binding level structures are initialized by copying this one. */
742
743static struct binding_level clear_binding_level;
744
745/* Nonzero means unconditionally make a BLOCK for the next level pushed. */
746
747static int keep_next_level_flag;
748
749#if defined(DEBUG_CP_BINDING_LEVELS)
750static int binding_depth = 0;
751static int is_class_level = 0;
752
753static void
754indent ()
755{
756 register unsigned i;
757
758 for (i = 0; i < binding_depth*2; i++)
759 putc (' ', stderr);
760}
761#endif /* defined(DEBUG_CP_BINDING_LEVELS) */
762
763static tree pushdecl_with_scope PROTO((tree, struct binding_level *));
764
765static void
766push_binding_level (newlevel, tag_transparent, keep)
767 struct binding_level *newlevel;
768 int tag_transparent, keep;
769{
770 /* Add this level to the front of the chain (stack) of levels that
771 are active. */
772 *newlevel = clear_binding_level;
8f032717 773 newlevel->level_chain = current_binding_level;
8d08fdba
MS
774 current_binding_level = newlevel;
775 newlevel->tag_transparent = tag_transparent;
776 newlevel->more_cleanups_ok = 1;
8d08fdba
MS
777 newlevel->keep = keep;
778#if defined(DEBUG_CP_BINDING_LEVELS)
779 newlevel->binding_depth = binding_depth;
780 indent ();
781 fprintf (stderr, "push %s level 0x%08x line %d\n",
782 (is_class_level) ? "class" : "block", newlevel, lineno);
783 is_class_level = 0;
784 binding_depth++;
785#endif /* defined(DEBUG_CP_BINDING_LEVELS) */
786}
787
8f032717
MM
788/* Find the innermost enclosing class scope, and reset
789 CLASS_BINDING_LEVEL appropriately. */
790
791static void
792find_class_binding_level ()
793{
794 struct binding_level *level = current_binding_level;
795
796 while (level && level->parm_flag != 2)
797 level = level->level_chain;
798 if (level && level->parm_flag == 2)
799 class_binding_level = level;
800 else
801 class_binding_level = 0;
802}
803
8d08fdba
MS
804static void
805pop_binding_level ()
806{
8d08fdba
MS
807 if (global_binding_level)
808 {
2c73f9f5 809 /* Cannot pop a level, if there are none left to pop. */
8d08fdba
MS
810 if (current_binding_level == global_binding_level)
811 my_friendly_abort (123);
812 }
813 /* Pop the current level, and free the structure for reuse. */
814#if defined(DEBUG_CP_BINDING_LEVELS)
815 binding_depth--;
816 indent ();
817 fprintf (stderr, "pop %s level 0x%08x line %d\n",
818 (is_class_level) ? "class" : "block",
819 current_binding_level, lineno);
820 if (is_class_level != (current_binding_level == class_binding_level))
9e9ff709
MS
821 {
822 indent ();
823 fprintf (stderr, "XXX is_class_level != (current_binding_level == class_binding_level)\n");
824 }
8d08fdba
MS
825 is_class_level = 0;
826#endif /* defined(DEBUG_CP_BINDING_LEVELS) */
827 {
828 register struct binding_level *level = current_binding_level;
829 current_binding_level = current_binding_level->level_chain;
830 level->level_chain = free_binding_level;
831#if 0 /* defined(DEBUG_CP_BINDING_LEVELS) */
832 if (level->binding_depth != binding_depth)
833 abort ();
834#endif /* defined(DEBUG_CP_BINDING_LEVELS) */
8f032717
MM
835 free_binding_level = level;
836 find_class_binding_level ();
8d08fdba
MS
837 }
838}
a9aedbc2
MS
839
840static void
841suspend_binding_level ()
842{
843 if (class_binding_level)
844 current_binding_level = class_binding_level;
845
846 if (global_binding_level)
847 {
2c73f9f5 848 /* Cannot suspend a level, if there are none left to suspend. */
a9aedbc2
MS
849 if (current_binding_level == global_binding_level)
850 my_friendly_abort (123);
851 }
852 /* Suspend the current level. */
853#if defined(DEBUG_CP_BINDING_LEVELS)
854 binding_depth--;
855 indent ();
856 fprintf (stderr, "suspend %s level 0x%08x line %d\n",
857 (is_class_level) ? "class" : "block",
858 current_binding_level, lineno);
859 if (is_class_level != (current_binding_level == class_binding_level))
9e9ff709
MS
860 {
861 indent ();
862 fprintf (stderr, "XXX is_class_level != (current_binding_level == class_binding_level)\n");
863 }
a9aedbc2
MS
864 is_class_level = 0;
865#endif /* defined(DEBUG_CP_BINDING_LEVELS) */
8f032717
MM
866 current_binding_level = current_binding_level->level_chain;
867 find_class_binding_level ();
a9aedbc2
MS
868}
869
824b9a4c 870static void
a9aedbc2
MS
871resume_binding_level (b)
872 struct binding_level *b;
873{
2c73f9f5
ML
874 /* Resuming binding levels is meant only for namespaces,
875 and those cannot nest into classes. */
876 my_friendly_assert(!class_binding_level, 386);
877 /* Also, resuming a non-directly nested namespace is a no-no. */
878 my_friendly_assert(b->level_chain == current_binding_level, 386);
a9aedbc2
MS
879 current_binding_level = b;
880#if defined(DEBUG_CP_BINDING_LEVELS)
881 b->binding_depth = binding_depth;
882 indent ();
883 fprintf (stderr, "resume %s level 0x%08x line %d\n",
884 (is_class_level) ? "class" : "block", b, lineno);
885 is_class_level = 0;
886 binding_depth++;
887#endif /* defined(DEBUG_CP_BINDING_LEVELS) */
888}
8d08fdba 889\f
cffa8729
MS
890/* Create a new `struct binding_level'. */
891
892static
893struct binding_level *
894make_binding_level ()
895{
896 /* NOSTRICT */
897 return (struct binding_level *) xmalloc (sizeof (struct binding_level));
898}
899
8d08fdba
MS
900/* Nonzero if we are currently in the global binding level. */
901
902int
903global_bindings_p ()
904{
905 return current_binding_level == global_binding_level;
906}
907
8f032717
MM
908/* Return the innermost binding level that is not for a class scope. */
909
910static struct binding_level *
911innermost_nonclass_level ()
912{
913 struct binding_level *b;
914
915 b = current_binding_level;
916 while (b->parm_flag == 2)
917 b = b->level_chain;
918
919 return b;
920}
921
a9aedbc2
MS
922/* Nonzero if we are currently in a toplevel binding level. This
923 means either the global binding level or a namespace in a toplevel
8f032717
MM
924 binding level. Since there are no non-toplevel namespace levels,
925 this really means any namespace or pseudo-global level. We also
926 include a class whose context is toplevel. */
a9aedbc2
MS
927
928int
929toplevel_bindings_p ()
930{
8f032717
MM
931 struct binding_level *b = innermost_nonclass_level ();
932
933 return b->namespace_p || b->pseudo_global;
a9aedbc2
MS
934}
935
8f032717
MM
936/* Nonzero if this is a namespace scope, or if we are defining a class
937 which is itself at namespace scope, or whose enclosing class is
938 such a class, etc. */
a9aedbc2 939
7bdbfa05 940int
a9aedbc2
MS
941namespace_bindings_p ()
942{
8f032717
MM
943 struct binding_level *b = innermost_nonclass_level ();
944
945 return b->namespace_p;
a9aedbc2
MS
946}
947
8d08fdba
MS
948void
949keep_next_level ()
950{
951 keep_next_level_flag = 1;
952}
953
954/* Nonzero if the current level needs to have a BLOCK made. */
955
956int
957kept_level_p ()
958{
959 return (current_binding_level->blocks != NULL_TREE
960 || current_binding_level->keep
961 || current_binding_level->names != NULL_TREE
962 || (current_binding_level->tags != NULL_TREE
963 && !current_binding_level->tag_transparent));
964}
965
966/* Identify this binding level as a level of parameters. */
967
968void
969declare_parm_level ()
970{
971 current_binding_level->parm_flag = 1;
972}
973
8d08fdba
MS
974void
975declare_pseudo_global_level ()
976{
977 current_binding_level->pseudo_global = 1;
978}
979
824b9a4c 980static void
a9aedbc2
MS
981declare_namespace_level ()
982{
983 current_binding_level->namespace_p = 1;
984}
985
8d08fdba
MS
986int
987pseudo_global_level_p ()
988{
8f032717
MM
989 struct binding_level *b = innermost_nonclass_level ();
990
991 return b->pseudo_global;
8d08fdba
MS
992}
993
994void
995set_class_shadows (shadows)
996 tree shadows;
997{
998 class_binding_level->class_shadowed = shadows;
999}
1000
1001/* Enter a new binding level.
1002 If TAG_TRANSPARENT is nonzero, do so only for the name space of variables,
1003 not for that of tags. */
1004
1005void
1006pushlevel (tag_transparent)
1007 int tag_transparent;
1008{
1009 register struct binding_level *newlevel = NULL_BINDING_LEVEL;
1010
1011 /* If this is the top level of a function,
1012 just make sure that NAMED_LABELS is 0.
1013 They should have been set to 0 at the end of the previous function. */
1014
1015 if (current_binding_level == global_binding_level)
1016 my_friendly_assert (named_labels == NULL_TREE, 134);
1017
1018 /* Reuse or create a struct for this binding level. */
1019
1020#if defined(DEBUG_CP_BINDING_LEVELS)
1021 if (0)
1022#else /* !defined(DEBUG_CP_BINDING_LEVELS) */
1023 if (free_binding_level)
1024#endif /* !defined(DEBUG_CP_BINDING_LEVELS) */
1025 {
1026 newlevel = free_binding_level;
1027 free_binding_level = free_binding_level->level_chain;
1028 }
1029 else
1030 {
cffa8729 1031 newlevel = make_binding_level ();
8d08fdba 1032 }
cffa8729 1033
8d08fdba
MS
1034 push_binding_level (newlevel, tag_transparent, keep_next_level_flag);
1035 GNU_xref_start_scope ((HOST_WIDE_INT) newlevel);
1036 keep_next_level_flag = 0;
1037}
1038
5566b478 1039void
8d6e462b
PB
1040note_level_for_for ()
1041{
1042 current_binding_level->is_for_scope = 1;
1043}
1044
8d08fdba
MS
1045void
1046pushlevel_temporary (tag_transparent)
1047 int tag_transparent;
1048{
1049 pushlevel (tag_transparent);
1050 current_binding_level->keep = 2;
1051 clear_last_expr ();
1052
1053 /* Note we don't call push_momentary() here. Otherwise, it would cause
1054 cleanups to be allocated on the momentary obstack, and they will be
1055 overwritten by the next statement. */
1056
1057 expand_start_bindings (0);
1058}
1059
f181d4ae
MM
1060/* For a binding between a name and an entity at a block scope,
1061 this is the `struct binding_level' for the block. */
1062#define BINDING_LEVEL(NODE) \
c7a932b1 1063 (((struct tree_binding*)NODE)->scope.level)
f181d4ae
MM
1064
1065/* These are currently unused, but permanent, CPLUS_BINDING nodes.
1066 They are kept here because they are allocated from the permanent
1067 obstack and cannot be easily freed. */
1068static tree free_binding_nodes;
1069
1070/* Make DECL the innermost binding for ID. The LEVEL is the binding
1071 level at which this declaration is being bound. */
1072
1073static void
1074push_binding (id, decl, level)
1075 tree id;
1076 tree decl;
1077 struct binding_level* level;
1078{
1079 tree binding;
1080
1081 if (!free_binding_nodes)
1082 {
1083 /* There are no free nodes, so we must build one here. */
1084 push_obstacks_nochange ();
1085 end_temporary_allocation ();
1086 binding = make_node (CPLUS_BINDING);
1087 pop_obstacks ();
1088 }
1089 else
1090 {
1091 /* There are nodes on the free list. Grab the first one. */
1092 binding = free_binding_nodes;
1093
1094 /* And update the free list. */
1095 free_binding_nodes = TREE_CHAIN (free_binding_nodes);
1096 }
1097
1098 /* Now, fill in the binding information. */
1099 BINDING_VALUE (binding) = decl;
d8f8dca1 1100 BINDING_TYPE (binding) = NULL_TREE;
f181d4ae 1101 BINDING_LEVEL (binding) = level;
8f032717 1102 INHERITED_VALUE_BINDING_P (binding) = 0;
f181d4ae
MM
1103 LOCAL_BINDING_P (binding) = (level != class_binding_level);
1104
c45df9c1 1105 /* And put it on the front of the list of bindings for ID. */
f181d4ae
MM
1106 TREE_CHAIN (binding) = IDENTIFIER_BINDING (id);
1107 IDENTIFIER_BINDING (id) = binding;
1108}
1109
d8f8dca1
MM
1110/* ID is already bound in the current scope. But, DECL is an
1111 additional binding for ID in the same scope. This is the `struct
1112 stat' hack whereby a non-typedef class-name or enum-name can be
1113 bound at the same level as some other kind of entity. It's the
1114 responsibility of the caller to check that inserting this name is
8f032717
MM
1115 legal here. Returns nonzero if the new binding was successful. */
1116static int
d8f8dca1
MM
1117add_binding (id, decl)
1118 tree id;
1119 tree decl;
1120{
1121 tree binding = IDENTIFIER_BINDING (id);
8f032717 1122 int ok = 1;
d8f8dca1
MM
1123
1124 if (TREE_CODE (decl) == TYPE_DECL && DECL_ARTIFICIAL (decl))
1125 /* The new name is the type name. */
1126 BINDING_TYPE (binding) = decl;
8f032717
MM
1127 else if (!BINDING_VALUE (binding))
1128 /* This situation arises when push_class_level_binding moves an
1129 inherited type-binding out of the way to make room for a new
1130 value binding. */
1131 BINDING_VALUE (binding) = decl;
1132 else if (TREE_CODE (BINDING_VALUE (binding)) == TYPE_DECL
1133 && DECL_ARTIFICIAL (BINDING_VALUE (binding)))
1134 {
1135 /* The old binding was a type name. It was placed in
1136 BINDING_VALUE because it was thought, at the point it was
1137 declared, to be the only entity with such a name. Move the
1138 type name into the type slot; it is now hidden by the new
1139 binding. */
d8f8dca1
MM
1140 BINDING_TYPE (binding) = BINDING_VALUE (binding);
1141 BINDING_VALUE (binding) = decl;
8f032717 1142 INHERITED_VALUE_BINDING_P (binding) = 0;
d8f8dca1 1143 }
8f032717
MM
1144 else
1145 {
1146 cp_error ("declaration of `%#D'", decl);
1147 cp_error_at ("conflicts with previous declaration `%#D'",
1148 BINDING_VALUE (binding));
1149 ok = 0;
1150 }
1151
1152 return ok;
d8f8dca1
MM
1153}
1154
0034cf72
JM
1155/* Bind DECL to ID in the current_binding_level.
1156 If PUSH_USING is set in FLAGS, we know that DECL doesn't really belong
1157 to this binding level, that it got here through a using-declaration. */
f181d4ae
MM
1158
1159void
0034cf72 1160push_local_binding (id, decl, flags)
f181d4ae
MM
1161 tree id;
1162 tree decl;
0034cf72 1163 int flags;
f181d4ae 1164{
8f032717
MM
1165 struct binding_level *b;
1166
1167 /* Skip over any local classes. This makes sense if we call
1168 push_local_binding with a friend decl of a local class. */
1169 b = current_binding_level;
1170 while (b->parm_flag == 2)
1171 b = b->level_chain;
f181d4ae 1172
d8f8dca1 1173 if (lookup_name_current_level (id))
8f032717
MM
1174 {
1175 /* Supplement the existing binding. */
1176 if (!add_binding (id, decl))
1177 /* It didn't work. Something else must be bound at this
1178 level. Do not add DECL to the list of things to pop
1179 later. */
1180 return;
1181 }
d8f8dca1
MM
1182 else
1183 /* Create a new binding. */
8f032717 1184 push_binding (id, decl, b);
f181d4ae 1185
0034cf72 1186 if (TREE_CODE (decl) == OVERLOAD || (flags & PUSH_USING))
a06d48ef
JM
1187 /* We must put the OVERLOAD into a TREE_LIST since the
1188 TREE_CHAIN of an OVERLOAD is already used. Similarly for
1189 decls that got here through a using-declaration. */
1190 decl = build_tree_list (NULL_TREE, decl);
1191
f181d4ae
MM
1192 /* And put DECL on the list of things declared by the current
1193 binding level. */
8f032717
MM
1194 TREE_CHAIN (decl) = b->names;
1195 b->names = decl;
f181d4ae
MM
1196}
1197
8f032717
MM
1198/* Bind DECL to ID in the class_binding_level. Returns nonzero if the
1199 binding was successful. */
f181d4ae 1200
8f032717 1201int
f181d4ae
MM
1202push_class_binding (id, decl)
1203 tree id;
1204 tree decl;
1205{
8f032717
MM
1206 int result = 1;
1207 tree binding = IDENTIFIER_BINDING (id);
1208 tree context;
1209
1210 /* Note that we declared this value so that we can issue an error if
1211 this an illegal redeclaration of a name already used for some
1212 other purpose. */
1213 note_name_declared_in_class (id, decl);
1214
1215 if (binding && BINDING_LEVEL (binding) == class_binding_level)
d8f8dca1 1216 /* Supplement the existing binding. */
8f032717 1217 result = add_binding (id, decl);
d8f8dca1
MM
1218 else
1219 /* Create a new binding. */
1220 push_binding (id, decl, class_binding_level);
1221
1222 /* Update the IDENTIFIER_CLASS_VALUE for this ID to be the
1223 class-level declaration. Note that we do not use DECL here
1224 because of the possibility of the `struct stat' hack; if DECL is
1225 a class-name or enum-name we might prefer a field-name, or some
1226 such. */
1227 IDENTIFIER_CLASS_VALUE (id) = BINDING_VALUE (IDENTIFIER_BINDING (id));
8f032717
MM
1228
1229 /* If this is a binding from a base class, mark it as such. */
1230 binding = IDENTIFIER_BINDING (id);
1231 if (BINDING_VALUE (binding) == decl && TREE_CODE (decl) != TREE_LIST)
1232 {
280f9385
MM
1233 /* Any implicit typename must be from a base-class. The
1234 context for an implicit typename declaration is always
1235 the derived class in which the lookup was done, so the checks
1236 based on the context of DECL below will not trigger. */
1237 if (TREE_CODE (decl) == TYPE_DECL
1238 && IMPLICIT_TYPENAME_P (TREE_TYPE (decl)))
1239 INHERITED_VALUE_BINDING_P (binding) = 1;
8f032717
MM
1240 else
1241 {
280f9385
MM
1242 if (TREE_CODE (decl) == OVERLOAD)
1243 context = DECL_REAL_CONTEXT (OVL_CURRENT (decl));
1244 else
1245 {
1246 my_friendly_assert (TREE_CODE_CLASS (TREE_CODE (decl)) == 'd',
1247 0);
1248 context = DECL_REAL_CONTEXT (decl);
1249 }
8f032717 1250
280f9385
MM
1251 if (is_properly_derived_from (current_class_type, context))
1252 INHERITED_VALUE_BINDING_P (binding) = 1;
1253 else
1254 INHERITED_VALUE_BINDING_P (binding) = 0;
1255 }
8f032717
MM
1256 }
1257 else if (BINDING_VALUE (binding) == decl)
1258 /* We only encounter a TREE_LIST when push_class_decls detects an
1259 ambiguity. Such an ambiguity can be overridden by a definition
1260 in this class. */
1261 INHERITED_VALUE_BINDING_P (binding) = 1;
1262
1263 return result;
f181d4ae
MM
1264}
1265
d8f8dca1
MM
1266/* Remove the binding for DECL which should be the innermost binding
1267 for ID. */
f181d4ae
MM
1268
1269static void
d8f8dca1 1270pop_binding (id, decl)
f181d4ae 1271 tree id;
d8f8dca1 1272 tree decl;
f181d4ae
MM
1273{
1274 tree binding;
d8f8dca1 1275
f181d4ae
MM
1276 if (id == NULL_TREE)
1277 /* It's easiest to write the loops that call this function without
1278 checking whether or not the entities involved have names. We
1279 get here for such an entity. */
1280 return;
1281
d8f8dca1 1282 /* Get the innermost binding for ID. */
f181d4ae 1283 binding = IDENTIFIER_BINDING (id);
f181d4ae 1284
d8f8dca1
MM
1285 /* The name should be bound. */
1286 my_friendly_assert (binding != NULL_TREE, 0);
1287
1288 /* The DECL will be either the ordinary binding or the type
1289 binding for this identifier. Remove that binding. */
1290 if (BINDING_VALUE (binding) == decl)
1291 BINDING_VALUE (binding) = NULL_TREE;
1292 else if (BINDING_TYPE (binding) == decl)
1293 BINDING_TYPE (binding) = NULL_TREE;
1294 else
1295 my_friendly_abort (0);
1296
1297 if (!BINDING_VALUE (binding) && !BINDING_TYPE (binding))
1298 {
1299 /* We're completely done with the innermost binding for this
1300 identifier. Unhook it from the list of bindings. */
1301 IDENTIFIER_BINDING (id) = TREE_CHAIN (binding);
1302
1303 /* And place it on the free list. */
1304 TREE_CHAIN (binding) = free_binding_nodes;
1305 free_binding_nodes = binding;
1306 }
f181d4ae
MM
1307}
1308
8d08fdba
MS
1309/* Exit a binding level.
1310 Pop the level off, and restore the state of the identifier-decl mappings
1311 that were in effect when this level was entered.
1312
1313 If KEEP == 1, this level had explicit declarations, so
1314 and create a "block" (a BLOCK node) for the level
1315 to record its declarations and subblocks for symbol table output.
1316
8d08fdba
MS
1317 If FUNCTIONBODY is nonzero, this level is the body of a function,
1318 so create a block as if KEEP were set and also clear out all
1319 label names.
1320
1321 If REVERSE is nonzero, reverse the order of decls before putting
1322 them into the BLOCK. */
1323
1324tree
1325poplevel (keep, reverse, functionbody)
1326 int keep;
1327 int reverse;
1328 int functionbody;
1329{
1330 register tree link;
1331 /* The chain of decls was accumulated in reverse order.
1332 Put it into forward order, just for cleanliness. */
1333 tree decls;
1334 int tmp = functionbody;
8d08fdba
MS
1335 int real_functionbody = current_binding_level->keep == 2
1336 ? ((functionbody = 0), tmp) : functionbody;
1337 tree tags = functionbody >= 0 ? current_binding_level->tags : 0;
1338 tree subblocks = functionbody >= 0 ? current_binding_level->blocks : 0;
1339 tree block = NULL_TREE;
1340 tree decl;
1341 int block_previously_created;
f181d4ae
MM
1342 int leaving_for_scope;
1343
b74a0560
MM
1344 if (current_binding_level->parm_flag == 2)
1345 return poplevel_class ();
1346
1347 my_friendly_assert (!current_binding_level->class_shadowed,
1348 19990414);
8d08fdba 1349
536333d4
MM
1350 /* We used to use KEEP == 2 to indicate that the new block should go
1351 at the beginning of the list of blocks at this binding level,
1352 rather than the end. This hack is no longer used. */
1353 my_friendly_assert (keep == 0 || keep == 1, 0);
1354
8d08fdba
MS
1355 GNU_xref_end_scope ((HOST_WIDE_INT) current_binding_level,
1356 (HOST_WIDE_INT) current_binding_level->level_chain,
1357 current_binding_level->parm_flag,
5566b478 1358 current_binding_level->keep);
8d08fdba
MS
1359
1360 if (current_binding_level->keep == 1)
1361 keep = 1;
1362
8d08fdba
MS
1363 /* Get the decls in the order they were written.
1364 Usually current_binding_level->names is in reverse order.
1365 But parameter decls were previously put in forward order. */
1366
1367 if (reverse)
1368 current_binding_level->names
1369 = decls = nreverse (current_binding_level->names);
1370 else
1371 decls = current_binding_level->names;
1372
1373 /* Output any nested inline functions within this block
1374 if they weren't already output. */
1375
1376 for (decl = decls; decl; decl = TREE_CHAIN (decl))
1377 if (TREE_CODE (decl) == FUNCTION_DECL
1378 && ! TREE_ASM_WRITTEN (decl)
1379 && DECL_INITIAL (decl) != NULL_TREE
6060a796
MS
1380 && TREE_ADDRESSABLE (decl)
1381 && decl_function_context (decl) == current_function_decl)
8d08fdba
MS
1382 {
1383 /* If this decl was copied from a file-scope decl
1384 on account of a block-scope extern decl,
1385 propagate TREE_ADDRESSABLE to the file-scope decl. */
1386 if (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE)
1387 TREE_ADDRESSABLE (DECL_ABSTRACT_ORIGIN (decl)) = 1;
1388 else
1389 {
1390 push_function_context ();
1391 output_inline_function (decl);
1392 pop_function_context ();
1393 }
1394 }
1395
1396 /* If there were any declarations or structure tags in that level,
1397 or if this level is a function body,
1398 create a BLOCK to record them for the life of this function. */
1399
1400 block = NULL_TREE;
1401 block_previously_created = (current_binding_level->this_block != NULL_TREE);
1402 if (block_previously_created)
1403 block = current_binding_level->this_block;
1404 else if (keep == 1 || functionbody)
1405 block = make_node (BLOCK);
1406 if (block != NULL_TREE)
1407 {
72b7eeff
MS
1408 if (block_previously_created)
1409 {
1410 if (decls || tags || subblocks)
1411 {
be99da77 1412 if (BLOCK_VARS (block) || BLOCK_TYPE_TAGS (block))
f181d4ae
MM
1413 warning ("internal compiler error: debugging info corrupted");
1414
72b7eeff
MS
1415 BLOCK_VARS (block) = decls;
1416 BLOCK_TYPE_TAGS (block) = tags;
be99da77
MS
1417
1418 /* We can have previous subblocks and new subblocks when
1419 doing fixup_gotos with complex cleanups. We chain the new
1420 subblocks onto the end of any pre-existing subblocks. */
1421 BLOCK_SUBBLOCKS (block) = chainon (BLOCK_SUBBLOCKS (block),
1422 subblocks);
72b7eeff 1423 }
be99da77
MS
1424 /* If we created the block earlier on, and we are just
1425 diddling it now, then it already should have a proper
1426 BLOCK_END_NOTE value associated with it. */
72b7eeff
MS
1427 }
1428 else
1429 {
1430 BLOCK_VARS (block) = decls;
1431 BLOCK_TYPE_TAGS (block) = tags;
1432 BLOCK_SUBBLOCKS (block) = subblocks;
f181d4ae
MM
1433 /* Otherwise, for a new block, install a new BLOCK_END_NOTE
1434 value. */
72b7eeff
MS
1435 remember_end_note (block);
1436 }
8d08fdba
MS
1437 }
1438
1439 /* In each subblock, record that this is its superior. */
1440
1441 if (keep >= 0)
1442 for (link = subblocks; link; link = TREE_CHAIN (link))
1443 BLOCK_SUPERCONTEXT (link) = block;
1444
f181d4ae
MM
1445 /* We still support the old for-scope rules, whereby the variables
1446 in a for-init statement were in scope after the for-statement
1447 ended. We only use the new rules in flag_new_for_scope is
1448 nonzero. */
1449 leaving_for_scope
1450 = current_binding_level->is_for_scope && flag_new_for_scope == 1;
1451
1452 /* Remove declarations for all the DECLs in this level. */
1453 for (link = decls; link; link = TREE_CHAIN (link))
1454 {
1455 if (leaving_for_scope && TREE_CODE (link) == VAR_DECL)
1456 {
1457 tree outer_binding
1458 = TREE_CHAIN (IDENTIFIER_BINDING (DECL_NAME (link)));
1459 tree ns_binding;
1460
1461 if (!outer_binding)
1462 ns_binding = IDENTIFIER_NAMESPACE_VALUE (DECL_NAME (link));
c7dda1e3
MM
1463 else
1464 ns_binding = NULL_TREE;
1465
f181d4ae
MM
1466 if (outer_binding
1467 && (BINDING_LEVEL (outer_binding)
1468 == current_binding_level->level_chain))
1469 /* We have something like:
1470
1471 int i;
1472 for (int i; ;);
1473
1474 and we are leaving the `for' scope. There's no reason to
1475 keep the binding of the inner `i' in this case. */
d8f8dca1 1476 pop_binding (DECL_NAME (link), link);
f181d4ae
MM
1477 else if ((outer_binding
1478 && (TREE_CODE (BINDING_VALUE (outer_binding))
1479 == TYPE_DECL))
1480 || (ns_binding
1481 && TREE_CODE (ns_binding) == TYPE_DECL))
1482 /* Here, we have something like:
1483
1484 typedef int I;
1485
1486 void f () {
1487 for (int I; ;);
1488 }
1489
1490 We must pop the for-scope binding so we know what's a
1491 type and what isn't. */
d8f8dca1 1492 pop_binding (DECL_NAME (link), link);
e76a2646 1493 else
e76a2646 1494 {
f181d4ae
MM
1495 /* Mark this VAR_DECL as dead so that we can tell we left it
1496 there only for backward compatibility. */
1497 DECL_DEAD_FOR_LOCAL (link) = 1;
1498
1499 /* Keep track of what should of have happenned when we
1500 popped the binding. */
1501 if (outer_binding && BINDING_VALUE (outer_binding))
1502 DECL_SHADOWED_FOR_VAR (link)
1503 = BINDING_VALUE (outer_binding);
1504
1505 /* Add it to the list of dead variables in the next
1506 outermost binding to that we can remove these when we
1507 leave that binding. */
1508 current_binding_level->level_chain->dead_vars_from_for
1509 = tree_cons (NULL_TREE, link,
1510 current_binding_level->level_chain->
1511 dead_vars_from_for);
1512
1513 /* Although we don't pop the CPLUS_BINDING, we do clear
1514 its BINDING_LEVEL since the level is going away now. */
1515 BINDING_LEVEL (IDENTIFIER_BINDING (DECL_NAME (link)))
1516 = 0;
e76a2646 1517 }
2ee887f2 1518 }
f181d4ae 1519 else
8d6e462b 1520 {
f181d4ae 1521 /* Remove the binding. */
0034cf72
JM
1522 decl = link;
1523 if (TREE_CODE (decl) == TREE_LIST)
1524 decl = TREE_VALUE (decl);
1525 if (TREE_CODE_CLASS (TREE_CODE (decl)) == 'd')
1526 pop_binding (DECL_NAME (decl), decl);
1527 else if (TREE_CODE (decl) == OVERLOAD)
1528 pop_binding (DECL_NAME (OVL_FUNCTION (decl)), decl);
d8f8dca1 1529 else
f181d4ae 1530 my_friendly_abort (0);
8d08fdba 1531 }
f181d4ae 1532 }
8d08fdba 1533
f181d4ae
MM
1534 /* Remove declarations for any `for' variables from inner scopes
1535 that we kept around. */
1536 for (link = current_binding_level->dead_vars_from_for;
1537 link; link = TREE_CHAIN (link))
d8f8dca1 1538 pop_binding (DECL_NAME (TREE_VALUE (link)), TREE_VALUE (link));
2ee887f2 1539
f181d4ae
MM
1540 /* Restore the IDENTIFIER_TYPE_VALUEs. */
1541 for (link = current_binding_level->type_shadowed;
1542 link; link = TREE_CHAIN (link))
1543 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (link), TREE_VALUE (link));
1544
1545 /* There may be OVERLOADs (wrapped in TREE_LISTs) on the BLOCK_VARs
1546 list if a `using' declaration put them there. The debugging
1547 back-ends won't understand OVERLOAD, so we remove them here.
1548 Because the BLOCK_VARS are (temporarily) shared with
1549 CURRENT_BINDING_LEVEL->NAMES we must do this fixup after we have
1550 popped all the bindings. */
1551 if (block)
1552 {
1553 tree* d;
8d6e462b 1554
a06d48ef
JM
1555 for (d = &BLOCK_VARS (block); *d; )
1556 {
1557 if (TREE_CODE (*d) == TREE_LIST)
1558 *d = TREE_CHAIN (*d);
1559 else
1560 d = &TREE_CHAIN (*d);
1561 }
8d6e462b 1562 }
8d08fdba
MS
1563
1564 /* If the level being exited is the top level of a function,
1565 check over all the labels. */
1566
1567 if (functionbody)
1568 {
1569 /* If this is the top level block of a function,
1570 the vars are the function's parameters.
1571 Don't leave them in the BLOCK because they are
1572 found in the FUNCTION_DECL instead. */
1573
1574 BLOCK_VARS (block) = 0;
1575
1576 /* Clear out the definitions of all label names,
1577 since their scopes end here. */
1578
1579 for (link = named_labels; link; link = TREE_CHAIN (link))
1580 {
1581 register tree label = TREE_VALUE (link);
1582
1583 if (DECL_INITIAL (label) == NULL_TREE)
1584 {
8251199e 1585 cp_error_at ("label `%D' used but not defined", label);
8d08fdba
MS
1586 /* Avoid crashing later. */
1587 define_label (input_filename, 1, DECL_NAME (label));
1588 }
1589 else if (warn_unused && !TREE_USED (label))
8251199e 1590 cp_warning_at ("label `%D' defined but not used", label);
8d08fdba
MS
1591 SET_IDENTIFIER_LABEL_VALUE (DECL_NAME (label), NULL_TREE);
1592
1593 /* Put the labels into the "variables" of the
1594 top-level block, so debugger can see them. */
1595 TREE_CHAIN (label) = BLOCK_VARS (block);
1596 BLOCK_VARS (block) = label;
1597 }
1598
1599 named_labels = NULL_TREE;
1600 }
1601
1602 /* Any uses of undefined labels now operate under constraints
1603 of next binding contour. */
1604 {
1605 struct binding_level *level_chain;
1606 level_chain = current_binding_level->level_chain;
1607 if (level_chain)
1608 {
e349ee73
MS
1609 struct named_label_list *labels;
1610 for (labels = named_label_uses; labels; labels = labels->next)
1611 if (labels->binding_level == current_binding_level)
8d08fdba 1612 {
e349ee73
MS
1613 labels->binding_level = level_chain;
1614 labels->names_in_scope = level_chain->names;
8d08fdba
MS
1615 }
1616 }
1617 }
1618
1619 tmp = current_binding_level->keep;
1620
1621 pop_binding_level ();
1622 if (functionbody)
1623 DECL_INITIAL (current_function_decl) = block;
1624 else if (block)
1625 {
1626 if (!block_previously_created)
1627 current_binding_level->blocks
1628 = chainon (current_binding_level->blocks, block);
1629 }
1630 /* If we did not make a block for the level just exited,
1631 any blocks made for inner levels
1632 (since they cannot be recorded as subblocks in that level)
1633 must be carried forward so they will later become subblocks
1634 of something else. */
1635 else if (subblocks)
536333d4
MM
1636 current_binding_level->blocks
1637 = chainon (current_binding_level->blocks, subblocks);
8d08fdba
MS
1638
1639 /* Take care of compiler's internal binding structures. */
a4443a08 1640 if (tmp == 2)
8d08fdba 1641 {
8d08fdba
MS
1642 expand_end_bindings (getdecls (), keep, 1);
1643 /* Each and every BLOCK node created here in `poplevel' is important
1644 (e.g. for proper debugging information) so if we created one
1645 earlier, mark it as "used". */
1646 if (block)
1647 TREE_USED (block) = 1;
1648 block = poplevel (keep, reverse, real_functionbody);
1649 }
1650
1651 /* Each and every BLOCK node created here in `poplevel' is important
1652 (e.g. for proper debugging information) so if we created one
1653 earlier, mark it as "used". */
1654 if (block)
1655 TREE_USED (block) = 1;
1656 return block;
1657}
1658
1659/* Delete the node BLOCK from the current binding level.
1660 This is used for the block inside a stmt expr ({...})
1661 so that the block can be reinserted where appropriate. */
1662
1663void
1664delete_block (block)
1665 tree block;
1666{
1667 tree t;
1668 if (current_binding_level->blocks == block)
1669 current_binding_level->blocks = TREE_CHAIN (block);
1670 for (t = current_binding_level->blocks; t;)
1671 {
1672 if (TREE_CHAIN (t) == block)
1673 TREE_CHAIN (t) = TREE_CHAIN (block);
1674 else
1675 t = TREE_CHAIN (t);
1676 }
1677 TREE_CHAIN (block) = NULL_TREE;
1678 /* Clear TREE_USED which is always set by poplevel.
1679 The flag is set again if insert_block is called. */
1680 TREE_USED (block) = 0;
1681}
1682
1683/* Insert BLOCK at the end of the list of subblocks of the
1684 current binding level. This is used when a BIND_EXPR is expanded,
1685 to handle the BLOCK node inside the BIND_EXPR. */
1686
1687void
1688insert_block (block)
1689 tree block;
1690{
1691 TREE_USED (block) = 1;
1692 current_binding_level->blocks
1693 = chainon (current_binding_level->blocks, block);
1694}
1695
8d08fdba
MS
1696/* Set the BLOCK node for the innermost scope
1697 (the one we are currently in). */
1698
1699void
1700set_block (block)
1701 register tree block;
1702{
1703 current_binding_level->this_block = block;
1704}
1705
1706/* Do a pushlevel for class declarations. */
e92cc029 1707
8d08fdba
MS
1708void
1709pushlevel_class ()
1710{
1711 register struct binding_level *newlevel;
1712
1713 /* Reuse or create a struct for this binding level. */
1714#if defined(DEBUG_CP_BINDING_LEVELS)
1715 if (0)
1716#else /* !defined(DEBUG_CP_BINDING_LEVELS) */
1717 if (free_binding_level)
1718#endif /* !defined(DEBUG_CP_BINDING_LEVELS) */
1719 {
1720 newlevel = free_binding_level;
1721 free_binding_level = free_binding_level->level_chain;
1722 }
1723 else
f181d4ae 1724 newlevel = make_binding_level ();
8d08fdba
MS
1725
1726#if defined(DEBUG_CP_BINDING_LEVELS)
1727 is_class_level = 1;
1728#endif /* defined(DEBUG_CP_BINDING_LEVELS) */
1729
1730 push_binding_level (newlevel, 0, 0);
1731
1732 decl_stack = push_decl_level (decl_stack, &decl_obstack);
1733 class_binding_level = current_binding_level;
1734 class_binding_level->parm_flag = 2;
8d08fdba
MS
1735}
1736
b74a0560 1737/* ...and a poplevel for class declarations. */
e92cc029 1738
b74a0560
MM
1739static tree
1740poplevel_class ()
8d08fdba
MS
1741{
1742 register struct binding_level *level = class_binding_level;
8d08fdba
MS
1743 tree shadowed;
1744
1745 my_friendly_assert (level != 0, 354);
1746
1747 decl_stack = pop_stack_level (decl_stack);
8d08fdba 1748 /* If we're leaving a toplevel class, don't bother to do the setting
ddd5a7c1 1749 of IDENTIFIER_CLASS_VALUE to NULL_TREE, since first of all this slot
8d08fdba 1750 shouldn't even be used when current_class_type isn't set, and second,
ddd5a7c1 1751 if we don't touch it here, we're able to use the cache effect if the
8d08fdba 1752 next time we're entering a class scope, it is the same class. */
b74a0560 1753 if (current_class_depth != 1)
8f032717
MM
1754 {
1755 struct binding_level* b;
1756
1757 /* Clear out our IDENTIFIER_CLASS_VALUEs. */
1758 for (shadowed = level->class_shadowed;
1759 shadowed;
1760 shadowed = TREE_CHAIN (shadowed))
1761 IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (shadowed)) = NULL_TREE;
1762
1763 /* Find the next enclosing class, and recreate
1764 IDENTIFIER_CLASS_VALUEs appropriate for that class. */
1765 b = level->level_chain;
1766 while (b && b->parm_flag != 2)
1767 b = b->level_chain;
1768
1769 if (b)
1770 for (shadowed = b->class_shadowed;
1771 shadowed;
1772 shadowed = TREE_CHAIN (shadowed))
1773 {
1774 tree t;
1775
1776 t = IDENTIFIER_BINDING (TREE_PURPOSE (shadowed));
1777 while (t && BINDING_LEVEL (t) != b)
1778 t = TREE_CHAIN (t);
1779
1780 if (t)
1781 IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (shadowed))
1782 = BINDING_VALUE (t);
1783 }
1784 }
8d08fdba
MS
1785 else
1786 /* Remember to save what IDENTIFIER's were bound in this scope so we
1787 can recover from cache misses. */
e76a2646
MS
1788 {
1789 previous_class_type = current_class_type;
1790 previous_class_values = class_binding_level->class_shadowed;
1791 }
8d08fdba
MS
1792 for (shadowed = level->type_shadowed;
1793 shadowed;
1794 shadowed = TREE_CHAIN (shadowed))
2c73f9f5 1795 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (shadowed), TREE_VALUE (shadowed));
8d08fdba 1796
f181d4ae
MM
1797 /* Remove the bindings for all of the class-level declarations. */
1798 for (shadowed = level->class_shadowed;
1799 shadowed;
1800 shadowed = TREE_CHAIN (shadowed))
d8f8dca1 1801 pop_binding (TREE_PURPOSE (shadowed), TREE_TYPE (shadowed));
f181d4ae 1802
8d08fdba
MS
1803 GNU_xref_end_scope ((HOST_WIDE_INT) class_binding_level,
1804 (HOST_WIDE_INT) class_binding_level->level_chain,
1805 class_binding_level->parm_flag,
5566b478 1806 class_binding_level->keep);
8d08fdba 1807
38e01259 1808 /* Now, pop out of the binding level which we created up in the
8d08fdba
MS
1809 `pushlevel_class' routine. */
1810#if defined(DEBUG_CP_BINDING_LEVELS)
1811 is_class_level = 1;
1812#endif /* defined(DEBUG_CP_BINDING_LEVELS) */
1813
1814 pop_binding_level ();
1815
b74a0560 1816 return NULL_TREE;
8d08fdba 1817}
8f032717
MM
1818
1819/* We are entering the scope of a class. Clear IDENTIFIER_CLASS_VALUE
1820 for any names in enclosing classes. */
1821
1822void
1823clear_identifier_class_values ()
1824{
1825 tree t;
1826
1827 if (!class_binding_level)
1828 return;
1829
1830 for (t = class_binding_level->class_shadowed;
1831 t;
1832 t = TREE_CHAIN (t))
1833 IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (t)) = NULL_TREE;
1834}
1835
8d08fdba
MS
1836\f
1837/* For debugging. */
5566b478
MS
1838static int no_print_functions = 0;
1839static int no_print_builtins = 0;
8d08fdba
MS
1840
1841void
1842print_binding_level (lvl)
1843 struct binding_level *lvl;
1844{
1845 tree t;
1846 int i = 0, len;
1847 fprintf (stderr, " blocks=");
1848 fprintf (stderr, HOST_PTR_PRINTF, lvl->blocks);
1849 fprintf (stderr, " n_incomplete=%d parm_flag=%d keep=%d",
f30432d7 1850 list_length (lvl->incomplete), lvl->parm_flag, lvl->keep);
8d08fdba
MS
1851 if (lvl->tag_transparent)
1852 fprintf (stderr, " tag-transparent");
1853 if (lvl->more_cleanups_ok)
1854 fprintf (stderr, " more-cleanups-ok");
1855 if (lvl->have_cleanups)
1856 fprintf (stderr, " have-cleanups");
8d08fdba
MS
1857 fprintf (stderr, "\n");
1858 if (lvl->names)
1859 {
1860 fprintf (stderr, " names:\t");
1861 /* We can probably fit 3 names to a line? */
1862 for (t = lvl->names; t; t = TREE_CHAIN (t))
1863 {
fc378698 1864 if (no_print_functions && (TREE_CODE (t) == FUNCTION_DECL))
8d08fdba
MS
1865 continue;
1866 if (no_print_builtins
fc378698
MS
1867 && (TREE_CODE (t) == TYPE_DECL)
1868 && (!strcmp (DECL_SOURCE_FILE (t),"<built-in>")))
8d08fdba
MS
1869 continue;
1870
1871 /* Function decls tend to have longer names. */
1872 if (TREE_CODE (t) == FUNCTION_DECL)
1873 len = 3;
1874 else
1875 len = 2;
1876 i += len;
1877 if (i > 6)
1878 {
1879 fprintf (stderr, "\n\t");
1880 i = len;
1881 }
1882 print_node_brief (stderr, "", t, 0);
bd6dd845 1883 if (t == error_mark_node)
8d08fdba
MS
1884 break;
1885 }
1886 if (i)
1887 fprintf (stderr, "\n");
1888 }
1889 if (lvl->tags)
1890 {
1891 fprintf (stderr, " tags:\t");
1892 i = 0;
1893 for (t = lvl->tags; t; t = TREE_CHAIN (t))
1894 {
1895 if (TREE_PURPOSE (t) == NULL_TREE)
1896 len = 3;
1897 else if (TREE_PURPOSE (t) == TYPE_IDENTIFIER (TREE_VALUE (t)))
1898 len = 2;
1899 else
1900 len = 4;
1901 i += len;
1902 if (i > 5)
1903 {
1904 fprintf (stderr, "\n\t");
1905 i = len;
1906 }
1907 if (TREE_PURPOSE (t) == NULL_TREE)
1908 {
1909 print_node_brief (stderr, "<unnamed-typedef", TREE_VALUE (t), 0);
1910 fprintf (stderr, ">");
1911 }
1912 else if (TREE_PURPOSE (t) == TYPE_IDENTIFIER (TREE_VALUE (t)))
1913 print_node_brief (stderr, "", TREE_VALUE (t), 0);
1914 else
1915 {
1916 print_node_brief (stderr, "<typedef", TREE_PURPOSE (t), 0);
1917 print_node_brief (stderr, "", TREE_VALUE (t), 0);
1918 fprintf (stderr, ">");
1919 }
1920 }
1921 if (i)
1922 fprintf (stderr, "\n");
1923 }
8d08fdba
MS
1924 if (lvl->class_shadowed)
1925 {
1926 fprintf (stderr, " class-shadowed:");
1927 for (t = lvl->class_shadowed; t; t = TREE_CHAIN (t))
1928 {
1929 fprintf (stderr, " %s ", IDENTIFIER_POINTER (TREE_PURPOSE (t)));
1930 }
1931 fprintf (stderr, "\n");
1932 }
1933 if (lvl->type_shadowed)
1934 {
1935 fprintf (stderr, " type-shadowed:");
1936 for (t = lvl->type_shadowed; t; t = TREE_CHAIN (t))
1937 {
8d08fdba 1938 fprintf (stderr, " %s ", IDENTIFIER_POINTER (TREE_PURPOSE (t)));
8d08fdba
MS
1939 }
1940 fprintf (stderr, "\n");
1941 }
1942}
1943
1944void
1945print_other_binding_stack (stack)
1946 struct binding_level *stack;
1947{
1948 struct binding_level *level;
1949 for (level = stack; level != global_binding_level; level = level->level_chain)
1950 {
1951 fprintf (stderr, "binding level ");
1952 fprintf (stderr, HOST_PTR_PRINTF, level);
1953 fprintf (stderr, "\n");
1954 print_binding_level (level);
1955 }
1956}
1957
1958void
1959print_binding_stack ()
1960{
1961 struct binding_level *b;
1962 fprintf (stderr, "current_binding_level=");
1963 fprintf (stderr, HOST_PTR_PRINTF, current_binding_level);
1964 fprintf (stderr, "\nclass_binding_level=");
1965 fprintf (stderr, HOST_PTR_PRINTF, class_binding_level);
1966 fprintf (stderr, "\nglobal_binding_level=");
1967 fprintf (stderr, HOST_PTR_PRINTF, global_binding_level);
1968 fprintf (stderr, "\n");
1969 if (class_binding_level)
1970 {
1971 for (b = class_binding_level; b; b = b->level_chain)
1972 if (b == current_binding_level)
1973 break;
1974 if (b)
1975 b = class_binding_level;
1976 else
1977 b = current_binding_level;
1978 }
1979 else
1980 b = current_binding_level;
1981 print_other_binding_stack (b);
1982 fprintf (stderr, "global:\n");
1983 print_binding_level (global_binding_level);
1984}
a9aedbc2 1985
2c73f9f5
ML
1986/* Namespace binding access routines: The namespace_bindings field of
1987 the identifier is polymorphic, with three possible values:
1988 NULL_TREE, a list of CPLUS_BINDINGS, or any other tree_node
1989 indicating the BINDING_VALUE of global_namespace. */
30394414 1990
2c73f9f5
ML
1991/* Check whether the a binding for the name to scope is known.
1992 Assumes that the bindings of the name are already a list
1993 of bindings. Returns the binding found, or NULL_TREE. */
1994
1995static tree
1996find_binding (name, scope)
30394414
JM
1997 tree name;
1998 tree scope;
1999{
2c73f9f5 2000 tree iter, prev = NULL_TREE;
3e3f722c
ML
2001
2002 scope = ORIGINAL_NAMESPACE (scope);
2003
30394414
JM
2004 for (iter = IDENTIFIER_NAMESPACE_BINDINGS (name); iter;
2005 iter = TREE_CHAIN (iter))
2006 {
2007 my_friendly_assert (TREE_CODE (iter) == CPLUS_BINDING, 374);
2008 if (BINDING_SCOPE (iter) == scope)
2c73f9f5
ML
2009 {
2010 /* Move binding found to the fron of the list, so
2011 subsequent lookups will find it faster. */
2012 if (prev)
2013 {
2014 TREE_CHAIN (prev) = TREE_CHAIN (iter);
2015 TREE_CHAIN (iter) = IDENTIFIER_NAMESPACE_BINDINGS (name);
2016 IDENTIFIER_NAMESPACE_BINDINGS (name) = iter;
2017 }
2018 return iter;
2019 }
2020 prev = iter;
30394414 2021 }
2c73f9f5
ML
2022 return NULL_TREE;
2023}
2024
2025/* Always returns a binding for name in scope. If the
2026 namespace_bindings is not a list, convert it to one first.
2027 If no binding is found, make a new one. */
2028
2029tree
2030binding_for_name (name, scope)
2031 tree name;
2032 tree scope;
2033{
2034 tree b = IDENTIFIER_NAMESPACE_BINDINGS (name);
2035 tree result;
3e3f722c
ML
2036
2037 scope = ORIGINAL_NAMESPACE (scope);
2038
2c73f9f5
ML
2039 if (b && TREE_CODE (b) != CPLUS_BINDING)
2040 {
2041 /* Get rid of optimization for global scope. */
2042 IDENTIFIER_NAMESPACE_BINDINGS (name) = NULL_TREE;
2043 BINDING_VALUE (binding_for_name (name, global_namespace)) = b;
2044 b = IDENTIFIER_NAMESPACE_BINDINGS (name);
2045 }
2046 if (b && (result = find_binding (name, scope)))
2047 return result;
2048 /* Not found, make a new permanent one. */
30394414 2049 push_obstacks (&permanent_obstack, &permanent_obstack);
2c73f9f5
ML
2050 result = make_node (CPLUS_BINDING);
2051 TREE_CHAIN (result) = b;
2052 IDENTIFIER_NAMESPACE_BINDINGS (name) = result;
2053 BINDING_SCOPE (result) = scope;
2054 BINDING_TYPE (result) = NULL_TREE;
2055 BINDING_VALUE (result) = NULL_TREE;
30394414 2056 pop_obstacks ();
2c73f9f5
ML
2057 return result;
2058}
2059
2060/* Return the binding value for name in scope, considering that
2061 namespace_binding may or may not be a list of CPLUS_BINDINGS. */
2062
2063tree
2064namespace_binding (name, scope)
2065 tree name;
2066 tree scope;
2067{
2068 tree b = IDENTIFIER_NAMESPACE_BINDINGS (name);
2069 if (b == NULL_TREE)
2070 return NULL_TREE;
cb0dbb9a
JM
2071 if (scope == NULL_TREE)
2072 scope = global_namespace;
2c73f9f5
ML
2073 if (TREE_CODE (b) != CPLUS_BINDING)
2074 return (scope == global_namespace) ? b : NULL_TREE;
2075 name = find_binding (name,scope);
2076 if (name == NULL_TREE)
2077 return name;
2078 return BINDING_VALUE (name);
2079}
2080
2081/* Set the binding value for name in scope. If modifying the binding
2082 of global_namespace is attempted, try to optimize it. */
2083
2084void
2085set_namespace_binding (name, scope, val)
2086 tree name;
2087 tree scope;
2088 tree val;
2089{
2090 tree b;
cb0dbb9a
JM
2091
2092 if (scope == NULL_TREE)
2093 scope = global_namespace;
2094
2c73f9f5
ML
2095 if (scope == global_namespace)
2096 {
2097 b = IDENTIFIER_NAMESPACE_BINDINGS (name);
2098 if (b == NULL_TREE || TREE_CODE (b) != CPLUS_BINDING)
2099 {
2100 IDENTIFIER_NAMESPACE_BINDINGS (name) = val;
2101 return;
2102 }
2103 }
2104 b = binding_for_name (name, scope);
2105 BINDING_VALUE (b) = val;
30394414
JM
2106}
2107
dff6b454
RK
2108/* Push into the scope of the NAME namespace. If NAME is NULL_TREE, then we
2109 select a name that is unique to this compilation unit. */
e92cc029 2110
a9aedbc2
MS
2111void
2112push_namespace (name)
2113 tree name;
2114{
b370501f 2115 tree d = NULL_TREE;
30394414
JM
2116 int need_new = 1;
2117 int implicit_use = 0;
2c73f9f5 2118 int global = 0;
30394414
JM
2119 if (!global_namespace)
2120 {
2c73f9f5 2121 /* This must be ::. */
30394414 2122 my_friendly_assert (name == get_identifier ("::"), 377);
2c73f9f5 2123 global = 1;
30394414
JM
2124 }
2125 else if (!name)
2126 {
3ab52652
ML
2127 /* The name of anonymous namespace is unique for the translation
2128 unit. */
0c8feefe
MM
2129 if (!anonymous_namespace_name)
2130 anonymous_namespace_name = get_file_function_name ('N');
2131 name = anonymous_namespace_name;
3ab52652
ML
2132 d = IDENTIFIER_NAMESPACE_VALUE (name);
2133 if (d)
2134 /* Reopening anonymous namespace. */
2135 need_new = 0;
30394414 2136 implicit_use = 1;
2c73f9f5
ML
2137 }
2138 else if (current_namespace == global_namespace
2139 && name == DECL_NAME (std_node))
2140 {
2141 in_std++;
2142 return;
2143 }
30394414
JM
2144 else
2145 {
2c73f9f5 2146 /* Check whether this is an extended namespace definition. */
30394414
JM
2147 d = IDENTIFIER_NAMESPACE_VALUE (name);
2148 if (d != NULL_TREE && TREE_CODE (d) == NAMESPACE_DECL)
3e3f722c
ML
2149 {
2150 need_new = 0;
2151 if (DECL_NAMESPACE_ALIAS (d))
2152 {
8251199e 2153 cp_error ("namespace alias `%D' not allowed here, assuming `%D'",
3e3f722c
ML
2154 d, DECL_NAMESPACE_ALIAS (d));
2155 d = DECL_NAMESPACE_ALIAS (d);
2156 }
2157 }
30394414 2158 }
6633d636 2159
30394414
JM
2160 if (need_new)
2161 {
2c73f9f5 2162 /* Make a new namespace, binding the name to it. */
6b57ac29 2163 d = build_lang_decl (NAMESPACE_DECL, name, void_type_node);
2c73f9f5
ML
2164 /* The global namespace is not pushed, and the global binding
2165 level is set elsewhere. */
2166 if (!global)
2167 {
2168 d = pushdecl (d);
2169 pushlevel (0);
2170 declare_namespace_level ();
2171 NAMESPACE_LEVEL (d) = current_binding_level;
2172 }
30394414 2173 }
2c73f9f5
ML
2174 else
2175 resume_binding_level (NAMESPACE_LEVEL (d));
2176
30394414
JM
2177 if (implicit_use)
2178 do_using_directive (d);
2c73f9f5 2179 /* Enter the name space. */
30394414 2180 current_namespace = d;
a9aedbc2
MS
2181}
2182
2183/* Pop from the scope of the current namespace. */
e92cc029 2184
a9aedbc2
MS
2185void
2186pop_namespace ()
2187{
2c73f9f5
ML
2188 if (current_namespace == global_namespace)
2189 {
2190 my_friendly_assert (in_std>0, 980421);
2191 in_std--;
2192 return;
2193 }
cb0dbb9a 2194 current_namespace = CP_DECL_CONTEXT (current_namespace);
2c73f9f5
ML
2195 /* The binding level is not popped, as it might be re-opened later. */
2196 suspend_binding_level ();
2197}
a9aedbc2 2198
2c73f9f5 2199/* Concatenate the binding levels of all namespaces. */
a9aedbc2 2200
2c73f9f5
ML
2201void
2202cat_namespace_levels()
2203{
2204 tree current;
2205 tree last;
2206 struct binding_level *b;
a9aedbc2 2207
2c73f9f5
ML
2208 last = NAMESPACE_LEVEL (global_namespace) -> names;
2209 /* The nested namespaces appear in the names list of their ancestors. */
2210 for (current = last; current; current = TREE_CHAIN (current))
a9aedbc2 2211 {
a06d48ef
JM
2212 /* Catch simple infinite loops. */
2213 if (TREE_CHAIN (current) == current)
2214 my_friendly_abort (990126);
2215
85c6cbaf
ML
2216 if (TREE_CODE (current) != NAMESPACE_DECL
2217 || DECL_NAMESPACE_ALIAS (current))
2c73f9f5
ML
2218 continue;
2219 if (!DECL_LANG_SPECIFIC (current))
a9aedbc2 2220 {
2c73f9f5
ML
2221 /* Hmm. std. */
2222 my_friendly_assert (current == std_node, 393);
2223 continue;
a9aedbc2 2224 }
2c73f9f5
ML
2225 b = NAMESPACE_LEVEL (current);
2226 while (TREE_CHAIN (last))
2227 last = TREE_CHAIN (last);
2228 TREE_CHAIN (last) = NAMESPACE_LEVEL (current) -> names;
a9aedbc2 2229 }
a9aedbc2 2230}
8d08fdba
MS
2231\f
2232/* Subroutines for reverting temporarily to top-level for instantiation
2233 of templates and such. We actually need to clear out the class- and
2234 local-value slots of all identifiers, so that only the global values
2235 are at all visible. Simply setting current_binding_level to the global
2236 scope isn't enough, because more binding levels may be pushed. */
2237struct saved_scope {
2238 struct binding_level *old_binding_level;
2239 tree old_bindings;
2c73f9f5 2240 tree old_namespace;
8d08fdba 2241 struct saved_scope *prev;
61a127b3
MM
2242 tree class_name, class_type;
2243 tree access_specifier;
2244 tree function_decl;
8d08fdba 2245 struct binding_level *class_bindings;
51c184be
MS
2246 tree *lang_base, *lang_stack, lang_name;
2247 int lang_stacksize;
5566b478
MS
2248 int minimal_parse_mode;
2249 tree last_function_parms;
e76a2646 2250 tree template_parms;
5156628f 2251 HOST_WIDE_INT processing_template_decl;
a50f0918 2252 tree previous_class_type, previous_class_values;
e1467ff2
MM
2253 int processing_specialization;
2254 int processing_explicit_instantiation;
8d08fdba
MS
2255};
2256static struct saved_scope *current_saved_scope;
8d08fdba 2257
78957a2a
JM
2258/* A chain of the binding vecs created by store_bindings. We create a
2259 whole bunch of these during compilation, on permanent_obstack, so we
2260 can't just throw them away. */
2261static tree free_binding_vecs;
2262
e92cc029 2263static tree
45537677
MS
2264store_bindings (names, old_bindings)
2265 tree names, old_bindings;
2266{
2267 tree t;
2268 for (t = names; t; t = TREE_CHAIN (t))
2269 {
2270 tree binding, t1, id;
2271
2272 if (TREE_CODE (t) == TREE_LIST)
2273 id = TREE_PURPOSE (t);
2274 else
2275 id = DECL_NAME (t);
2276
f181d4ae
MM
2277 if (!id
2278 /* Note that we may have an IDENTIFIER_CLASS_VALUE even when
2279 we have no IDENTIFIER_BINDING if we have left the class
2280 scope, but cached the class-level declarations. */
2281 || !(IDENTIFIER_BINDING (id) || IDENTIFIER_CLASS_VALUE (id)))
45537677
MS
2282 continue;
2283
2284 for (t1 = old_bindings; t1; t1 = TREE_CHAIN (t1))
2285 if (TREE_VEC_ELT (t1, 0) == id)
2286 goto skip_it;
78957a2a
JM
2287
2288 if (free_binding_vecs)
2289 {
2290 binding = free_binding_vecs;
2291 free_binding_vecs = TREE_CHAIN (free_binding_vecs);
2292 }
2293 else
2294 binding = make_tree_vec (4);
2295
45537677
MS
2296 if (id)
2297 {
2298 my_friendly_assert (TREE_CODE (id) == IDENTIFIER_NODE, 135);
2299 TREE_VEC_ELT (binding, 0) = id;
2c73f9f5 2300 TREE_VEC_ELT (binding, 1) = REAL_IDENTIFIER_TYPE_VALUE (id);
f181d4ae 2301 TREE_VEC_ELT (binding, 2) = IDENTIFIER_BINDING (id);
45537677 2302 TREE_VEC_ELT (binding, 3) = IDENTIFIER_CLASS_VALUE (id);
f181d4ae 2303 IDENTIFIER_BINDING (id) = NULL_TREE;
45537677
MS
2304 IDENTIFIER_CLASS_VALUE (id) = NULL_TREE;
2305 }
2306 TREE_CHAIN (binding) = old_bindings;
2307 old_bindings = binding;
2308 skip_it:
2309 ;
2310 }
2311 return old_bindings;
2312}
2313
8d08fdba 2314void
5566b478
MS
2315maybe_push_to_top_level (pseudo)
2316 int pseudo;
8d08fdba 2317{
51c184be 2318 extern int current_lang_stacksize;
beb53fb8
JM
2319 struct saved_scope *s
2320 = (struct saved_scope *) xmalloc (sizeof (struct saved_scope));
8f032717 2321 struct binding_level *b = current_binding_level;
8d08fdba
MS
2322 tree old_bindings = NULL_TREE;
2323
642b32a5 2324 push_cp_function_context (NULL_TREE);
e349ee73 2325
a50f0918
MS
2326 if (previous_class_type)
2327 old_bindings = store_bindings (previous_class_values, old_bindings);
2328
8d08fdba
MS
2329 /* Have to include global_binding_level, because class-level decls
2330 aren't listed anywhere useful. */
2331 for (; b; b = b->level_chain)
2332 {
2333 tree t;
2334
2c73f9f5
ML
2335 /* Template IDs are inserted into the global level. If they were
2336 inserted into namespace level, finish_file wouldn't find them
2337 when doing pending instantiations. Therefore, don't stop at
2338 namespace level, but continue until :: . */
5566b478
MS
2339 if (b == global_binding_level || (pseudo && b->pseudo_global))
2340 break;
8d08fdba 2341
45537677 2342 old_bindings = store_bindings (b->names, old_bindings);
cffa8729 2343 /* We also need to check class_shadowed to save class-level type
45537677
MS
2344 bindings, since pushclass doesn't fill in b->names. */
2345 if (b->parm_flag == 2)
cffa8729 2346 old_bindings = store_bindings (b->class_shadowed, old_bindings);
8d08fdba 2347
8d08fdba
MS
2348 /* Unwind type-value slots back to top level. */
2349 for (t = b->type_shadowed; t; t = TREE_CHAIN (t))
2350 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (t), TREE_VALUE (t));
2351 }
8d08fdba
MS
2352
2353 s->old_binding_level = current_binding_level;
5566b478 2354 current_binding_level = b;
8d08fdba 2355
2c73f9f5 2356 s->old_namespace = current_namespace;
8d08fdba
MS
2357 s->class_name = current_class_name;
2358 s->class_type = current_class_type;
61a127b3 2359 s->access_specifier = current_access_specifier;
8d08fdba
MS
2360 s->function_decl = current_function_decl;
2361 s->class_bindings = class_binding_level;
51c184be
MS
2362 s->lang_stack = current_lang_stack;
2363 s->lang_base = current_lang_base;
2364 s->lang_stacksize = current_lang_stacksize;
2365 s->lang_name = current_lang_name;
5566b478
MS
2366 s->minimal_parse_mode = minimal_parse_mode;
2367 s->last_function_parms = last_function_parms;
e76a2646 2368 s->template_parms = current_template_parms;
5156628f 2369 s->processing_template_decl = processing_template_decl;
a50f0918
MS
2370 s->previous_class_type = previous_class_type;
2371 s->previous_class_values = previous_class_values;
e1467ff2
MM
2372 s->processing_specialization = processing_specialization;
2373 s->processing_explicit_instantiation = processing_explicit_instantiation;
e349ee73 2374
5f34005f 2375 current_class_name = current_class_type = NULL_TREE;
8d08fdba
MS
2376 current_function_decl = NULL_TREE;
2377 class_binding_level = (struct binding_level *)0;
51c184be
MS
2378 current_lang_stacksize = 10;
2379 current_lang_stack = current_lang_base
2380 = (tree *) xmalloc (current_lang_stacksize * sizeof (tree));
2381 current_lang_name = lang_name_cplusplus;
2382 strict_prototype = strict_prototypes_lang_cplusplus;
2383 named_labels = NULL_TREE;
120722ac 2384 shadowed_labels = NULL_TREE;
5566b478 2385 minimal_parse_mode = 0;
a50f0918 2386 previous_class_type = previous_class_values = NULL_TREE;
e1467ff2
MM
2387 processing_specialization = 0;
2388 processing_explicit_instantiation = 0;
eae97bd9
MM
2389 current_template_parms = NULL_TREE;
2390 processing_template_decl = 0;
2c73f9f5 2391 current_namespace = global_namespace;
8d08fdba
MS
2392
2393 s->prev = current_saved_scope;
2394 s->old_bindings = old_bindings;
2395 current_saved_scope = s;
5566b478
MS
2396
2397 push_obstacks (&permanent_obstack, &permanent_obstack);
2398}
2399
2400void
2401push_to_top_level ()
2402{
2403 maybe_push_to_top_level (0);
8d08fdba
MS
2404}
2405
2406void
2407pop_from_top_level ()
2408{
51c184be 2409 extern int current_lang_stacksize;
8d08fdba
MS
2410 struct saved_scope *s = current_saved_scope;
2411 tree t;
2412
e76a2646 2413 /* Clear out class-level bindings cache. */
8d08fdba 2414 if (previous_class_type)
8f032717 2415 invalidate_class_lookup_cache ();
8d08fdba 2416
5566b478
MS
2417 pop_obstacks ();
2418
8d08fdba
MS
2419 current_binding_level = s->old_binding_level;
2420 current_saved_scope = s->prev;
78957a2a 2421 for (t = s->old_bindings; t; )
8d08fdba 2422 {
78957a2a 2423 tree save = t;
8d08fdba
MS
2424 tree id = TREE_VEC_ELT (t, 0);
2425 if (id)
2426 {
2c73f9f5 2427 SET_IDENTIFIER_TYPE_VALUE (id, TREE_VEC_ELT (t, 1));
f181d4ae 2428 IDENTIFIER_BINDING (id) = TREE_VEC_ELT (t, 2);
8d08fdba
MS
2429 IDENTIFIER_CLASS_VALUE (id) = TREE_VEC_ELT (t, 3);
2430 }
78957a2a
JM
2431 t = TREE_CHAIN (t);
2432 TREE_CHAIN (save) = free_binding_vecs;
2433 free_binding_vecs = save;
8d08fdba 2434 }
2c73f9f5 2435 current_namespace = s->old_namespace;
8d08fdba
MS
2436 current_class_name = s->class_name;
2437 current_class_type = s->class_type;
61a127b3 2438 current_access_specifier = s->access_specifier;
8d08fdba
MS
2439 current_function_decl = s->function_decl;
2440 class_binding_level = s->class_bindings;
51c184be
MS
2441 free (current_lang_base);
2442 current_lang_base = s->lang_base;
2443 current_lang_stack = s->lang_stack;
2444 current_lang_name = s->lang_name;
2445 current_lang_stacksize = s->lang_stacksize;
2446 if (current_lang_name == lang_name_cplusplus)
2447 strict_prototype = strict_prototypes_lang_cplusplus;
2448 else if (current_lang_name == lang_name_c)
2449 strict_prototype = strict_prototypes_lang_c;
5566b478
MS
2450 minimal_parse_mode = s->minimal_parse_mode;
2451 last_function_parms = s->last_function_parms;
e76a2646 2452 current_template_parms = s->template_parms;
5156628f 2453 processing_template_decl = s->processing_template_decl;
a50f0918
MS
2454 previous_class_type = s->previous_class_type;
2455 previous_class_values = s->previous_class_values;
e1467ff2
MM
2456 processing_specialization = s->processing_specialization;
2457 processing_explicit_instantiation = s->processing_explicit_instantiation;
51c184be 2458
8d08fdba 2459 free (s);
e349ee73 2460
642b32a5 2461 pop_cp_function_context (NULL_TREE);
8d08fdba
MS
2462}
2463\f
2464/* Push a definition of struct, union or enum tag "name".
2465 into binding_level "b". "type" should be the type node,
2466 We assume that the tag "name" is not already defined.
2467
2468 Note that the definition may really be just a forward reference.
2469 In that case, the TYPE_SIZE will be a NULL_TREE.
2470
e92cc029 2471 C++ gratuitously puts all these tags in the name space. */
8d08fdba
MS
2472
2473/* When setting the IDENTIFIER_TYPE_VALUE field of an identifier ID,
2474 record the shadowed value for this binding contour. TYPE is
2475 the type that ID maps to. */
2476
2477static void
2478set_identifier_type_value_with_scope (id, type, b)
2479 tree id;
2480 tree type;
2481 struct binding_level *b;
2482{
2c73f9f5 2483 if (!b->namespace_p)
8d08fdba 2484 {
2c73f9f5
ML
2485 /* Shadow the marker, not the real thing, so that the marker
2486 gets restored later. */
2487 tree old_type_value = REAL_IDENTIFIER_TYPE_VALUE (id);
8d08fdba
MS
2488 b->type_shadowed
2489 = tree_cons (id, old_type_value, b->type_shadowed);
2490 }
2c73f9f5
ML
2491 else
2492 {
2493 tree binding = binding_for_name (id, current_namespace);
2494 BINDING_TYPE (binding) = type;
2495 /* Store marker instead of real type. */
2496 type = global_type_node;
2497 }
8d08fdba
MS
2498 SET_IDENTIFIER_TYPE_VALUE (id, type);
2499}
2500
8f032717 2501/* As set_identifier_type_value_with_scope, but using current_binding_level. */
8d08fdba
MS
2502
2503void
2504set_identifier_type_value (id, type)
2505 tree id;
2506 tree type;
2507{
8f032717 2508 set_identifier_type_value_with_scope (id, type, current_binding_level);
8d08fdba
MS
2509}
2510
2c73f9f5
ML
2511/* Return the type associated with id. */
2512
2513tree
2514identifier_type_value (id)
2515 tree id;
2516{
2517 /* There is no type with that name, anywhere. */
2518 if (REAL_IDENTIFIER_TYPE_VALUE (id) == NULL_TREE)
2519 return NULL_TREE;
2520 /* This is not the type marker, but the real thing. */
2521 if (REAL_IDENTIFIER_TYPE_VALUE (id) != global_type_node)
2522 return REAL_IDENTIFIER_TYPE_VALUE (id);
2523 /* Have to search for it. It must be on the global level, now.
2524 Ask lookup_name not to return non-types. */
3e3f722c 2525 id = lookup_name_real (id, 2, 1, 0);
2c73f9f5
ML
2526 if (id)
2527 return TREE_TYPE (id);
2528 return NULL_TREE;
2529}
2530
a9aedbc2
MS
2531/* Pop off extraneous binding levels left over due to syntax errors.
2532
2533 We don't pop past namespaces, as they might be valid. */
e92cc029 2534
8926095f
MS
2535void
2536pop_everything ()
2537{
2538#ifdef DEBUG_CP_BINDING_LEVELS
2539 fprintf (stderr, "XXX entering pop_everything ()\n");
2540#endif
8f032717 2541 while (!toplevel_bindings_p ())
8926095f 2542 {
8f032717 2543 if (current_binding_level->parm_flag == 2)
b74a0560 2544 pop_nested_class ();
8926095f
MS
2545 else
2546 poplevel (0, 0, 0);
2547 }
2548#ifdef DEBUG_CP_BINDING_LEVELS
2549 fprintf (stderr, "XXX leaving pop_everything ()\n");
2550#endif
2551}
2552
39c01e4c
MM
2553/* The type TYPE is being declared. If it is a class template, or a
2554 specialization of a class template, do any processing required and
2555 perform error-checking. If IS_FRIEND is non-zero, this TYPE is
2556 being declared a friend. B is the binding level at which this TYPE
2557 should be bound.
2558
2559 Returns the TYPE_DECL for TYPE, which may have been altered by this
2560 processing. */
2561
2562static tree
2563maybe_process_template_type_declaration (type, globalize, b)
2564 tree type;
2565 int globalize;
2566 struct binding_level* b;
2567{
2568 tree decl = TYPE_NAME (type);
2569
2570 if (processing_template_parmlist)
2571 /* You can't declare a new template type in a template parameter
2572 list. But, you can declare a non-template type:
2573
2574 template <class A*> struct S;
2575
2576 is a forward-declaration of `A'. */
2577 ;
2578 else
2579 {
2580 maybe_check_template_type (type);
2581
ed44da02
MM
2582 my_friendly_assert (IS_AGGR_TYPE (type)
2583 || TREE_CODE (type) == ENUMERAL_TYPE, 0);
2584
2585
2586 if (/* If !GLOBALIZE then we are looking at a definition.
2587 It may not be a primary template. (For example, in:
39c01e4c 2588
ed44da02
MM
2589 template <class T>
2590 struct S1 { class S2 {}; }
39c01e4c 2591
ed44da02
MM
2592 we have to push_template_decl for S2.) */
2593 (processing_template_decl && !globalize)
2594 /* If we are declaring a friend template class, we will
2595 have GLOBALIZE set, since something like:
39c01e4c 2596
ed44da02
MM
2597 template <class T>
2598 struct S1 {
2599 template <class U>
2600 friend class S2;
2601 };
39c01e4c 2602
ed44da02
MM
2603 declares S2 to be at global scope. */
2604 || PROCESSING_REAL_TEMPLATE_DECL_P ())
39c01e4c
MM
2605 {
2606 /* This may change after the call to
2607 push_template_decl_real, but we want the original value. */
2608 tree name = DECL_NAME (decl);
2609
2610 decl = push_template_decl_real (decl, globalize);
2611 /* If the current binding level is the binding level for the
2612 template parameters (see the comment in
2613 begin_template_parm_list) and the enclosing level is a class
2614 scope, and we're not looking at a friend, push the
2615 declaration of the member class into the class scope. In the
2616 friend case, push_template_decl will already have put the
2617 friend into global scope, if appropriate. */
ed44da02
MM
2618 if (TREE_CODE (type) != ENUMERAL_TYPE
2619 && !globalize && b->pseudo_global
39c01e4c
MM
2620 && b->level_chain->parm_flag == 2)
2621 {
61a127b3 2622 finish_member_declaration (CLASSTYPE_TI_TEMPLATE (type));
39c01e4c
MM
2623 /* Put this tag on the list of tags for the class, since
2624 that won't happen below because B is not the class
2625 binding level, but is instead the pseudo-global level. */
2626 b->level_chain->tags =
2627 saveable_tree_cons (name, type, b->level_chain->tags);
39c01e4c
MM
2628 if (TYPE_SIZE (current_class_type) == NULL_TREE)
2629 CLASSTYPE_TAGS (current_class_type) = b->level_chain->tags;
2630 }
2631 }
2632 }
2633
2634 return decl;
2635}
2636
8d08fdba 2637/* Push a tag name NAME for struct/class/union/enum type TYPE.
6757edfe 2638 Normally put it into the inner-most non-tag-transparent scope,
8d08fdba 2639 but if GLOBALIZE is true, put it in the inner-most non-class scope.
e92cc029 2640 The latter is needed for implicit declarations. */
8d08fdba
MS
2641
2642void
2643pushtag (name, type, globalize)
2644 tree name, type;
2645 int globalize;
2646{
2647 register struct binding_level *b;
8d08fdba 2648
8f032717 2649 b = current_binding_level;
8d08fdba
MS
2650 while (b->tag_transparent
2651 || (globalize && b->parm_flag == 2))
2652 b = b->level_chain;
2653
a9aedbc2 2654 if (toplevel_bindings_p ())
8d08fdba
MS
2655 b->tags = perm_tree_cons (name, type, b->tags);
2656 else
2657 b->tags = saveable_tree_cons (name, type, b->tags);
2658
2659 if (name)
2660 {
8d08fdba 2661 /* Do C++ gratuitous typedefing. */
db5ae43f 2662 if (IDENTIFIER_TYPE_VALUE (name) != type)
8d08fdba 2663 {
93cdc044
JM
2664 register tree d = NULL_TREE;
2665 int newdecl = 0, in_class = 0;
280f9385
MM
2666 tree context;
2667 tree c_decl = NULL_TREE;
2668
2669 context = type ? TYPE_CONTEXT (type) : NULL_TREE;
2670 if (! context)
2671 {
2672 tree cs = current_scope ();
2673
2674 if (! globalize)
2675 context = cs;
2676 else if (cs != NULL_TREE
2677 && TREE_CODE_CLASS (TREE_CODE (cs)) == 't')
2678 /* When declaring a friend class of a local class, we want
2679 to inject the newly named class into the scope
2680 containing the local class, not the namespace scope. */
2681 context = hack_decl_function_context (get_type_decl (cs));
2682 }
2683 if (context)
2684 c_decl = TREE_CODE (context) == FUNCTION_DECL
2685 ? context : TYPE_MAIN_DECL (context);
2686
2687 if (!context)
2688 context = current_namespace;
8d08fdba 2689
93cdc044
JM
2690 if ((b->pseudo_global && b->level_chain->parm_flag == 2)
2691 || b->parm_flag == 2)
2692 in_class = 1;
8d08fdba 2693 else
93cdc044
JM
2694 d = lookup_nested_type (type, c_decl);
2695
2696 if (d == NULL_TREE)
8d08fdba 2697 {
8d08fdba 2698 newdecl = 1;
a0a33927 2699 d = build_decl (TYPE_DECL, name, type);
eff71ab0
PB
2700 if (current_lang_name == lang_name_java)
2701 TYPE_FOR_JAVA (type) = 1;
00595019 2702 SET_DECL_ARTIFICIAL (d);
93cdc044
JM
2703 if (! in_class)
2704 set_identifier_type_value_with_scope (name, type, b);
2705 }
2706 else
2707 d = TYPE_MAIN_DECL (d);
2708
2709 TYPE_NAME (type) = d;
cb0dbb9a 2710 DECL_CONTEXT (d) = FROB_CONTEXT (context);
e1cd6e56 2711
39c01e4c
MM
2712 d = maybe_process_template_type_declaration (type,
2713 globalize, b);
93cdc044
JM
2714
2715 if (b->parm_flag == 2)
61a127b3 2716 {
61a127b3
MM
2717 if (newdecl && !PROCESSING_REAL_TEMPLATE_DECL_P ())
2718 /* Put this TYPE_DECL on the TYPE_FIELDS list for the
2719 class. But if it's a member template class, we
2720 want the TEMPLATE_DECL, not the TYPE_DECL, so this
2721 is done later. */
2722 finish_member_declaration (d);
8f032717
MM
2723 else
2724 pushdecl_class_level (d);
61a127b3 2725 }
93cdc044
JM
2726 else
2727 d = pushdecl_with_scope (d, b);
2728
8ccc31eb 2729 if (newdecl)
8d08fdba 2730 {
d2e5ee5c
MS
2731 if (ANON_AGGRNAME_P (name))
2732 DECL_IGNORED_P (d) = 1;
8ccc31eb 2733
5566b478 2734 TYPE_CONTEXT (type) = DECL_CONTEXT (d);
1f06b267 2735 DECL_ASSEMBLER_NAME (d) = DECL_NAME (d);
e4a84209
MM
2736 if (!uses_template_parms (type))
2737 DECL_ASSEMBLER_NAME (d)
2738 = get_identifier (build_overload_name (type, 1, 1));
8d08fdba 2739 }
8d08fdba
MS
2740 }
2741 if (b->parm_flag == 2)
2742 {
8d08fdba
MS
2743 if (TYPE_SIZE (current_class_type) == NULL_TREE)
2744 CLASSTYPE_TAGS (current_class_type) = b->tags;
2745 }
2746 }
2747
2748 if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL)
2749 /* Use the canonical TYPE_DECL for this node. */
2750 TYPE_STUB_DECL (type) = TYPE_NAME (type);
2751 else
2752 {
2753 /* Create a fake NULL-named TYPE_DECL node whose TREE_TYPE
2754 will be the tagged type we just added to the current
2755 binding level. This fake NULL-named TYPE_DECL node helps
2756 dwarfout.c to know when it needs to output a
2757 representation of a tagged type, and it also gives us a
2758 convenient place to record the "scope start" address for
2759 the tagged type. */
2760
8d08fdba 2761 tree d = build_decl (TYPE_DECL, NULL_TREE, type);
8d08fdba
MS
2762 TYPE_STUB_DECL (type) = pushdecl_with_scope (d, b);
2763 }
2764}
2765
2766/* Counter used to create anonymous type names. */
e92cc029 2767
8d08fdba
MS
2768static int anon_cnt = 0;
2769
2770/* Return an IDENTIFIER which can be used as a name for
2771 anonymous structs and unions. */
e92cc029 2772
8d08fdba
MS
2773tree
2774make_anon_name ()
2775{
2776 char buf[32];
2777
2778 sprintf (buf, ANON_AGGRNAME_FORMAT, anon_cnt++);
2779 return get_identifier (buf);
2780}
2781
2782/* Clear the TREE_PURPOSE slot of tags which have anonymous typenames.
2783 This keeps dbxout from getting confused. */
e92cc029 2784
8d08fdba
MS
2785void
2786clear_anon_tags ()
2787{
2788 register struct binding_level *b;
2789 register tree tags;
2790 static int last_cnt = 0;
2791
2792 /* Fast out if no new anon names were declared. */
2793 if (last_cnt == anon_cnt)
2794 return;
2795
2796 b = current_binding_level;
2797 while (b->tag_transparent)
2798 b = b->level_chain;
2799 tags = b->tags;
2800 while (tags)
2801 {
2802 /* A NULL purpose means we have already processed all tags
2803 from here to the end of the list. */
2804 if (TREE_PURPOSE (tags) == NULL_TREE)
2805 break;
2806 if (ANON_AGGRNAME_P (TREE_PURPOSE (tags)))
2807 TREE_PURPOSE (tags) = NULL_TREE;
2808 tags = TREE_CHAIN (tags);
2809 }
2810 last_cnt = anon_cnt;
2811}
2812\f
2813/* Subroutine of duplicate_decls: return truthvalue of whether
2814 or not types of these decls match.
2815
2816 For C++, we must compare the parameter list so that `int' can match
2817 `int&' in a parameter position, but `int&' is not confused with
2818 `const int&'. */
e92cc029 2819
6060a796 2820int
8d08fdba
MS
2821decls_match (newdecl, olddecl)
2822 tree newdecl, olddecl;
2823{
2824 int types_match;
2825
347d73d7
ML
2826 if (newdecl == olddecl)
2827 return 1;
2828
6b4b3deb
MM
2829 if (TREE_CODE (newdecl) != TREE_CODE (olddecl))
2830 /* If the two DECLs are not even the same kind of thing, we're not
2831 interested in their types. */
2832 return 0;
2833
2834 if (TREE_CODE (newdecl) == FUNCTION_DECL)
8d08fdba
MS
2835 {
2836 tree f1 = TREE_TYPE (newdecl);
2837 tree f2 = TREE_TYPE (olddecl);
2838 tree p1 = TYPE_ARG_TYPES (f1);
2839 tree p2 = TYPE_ARG_TYPES (f2);
2840
c5a6fc45
JM
2841 if (DECL_REAL_CONTEXT (newdecl) != DECL_REAL_CONTEXT (olddecl)
2842 && ! (DECL_LANGUAGE (newdecl) == lang_c
2843 && DECL_LANGUAGE (olddecl) == lang_c))
2844 return 0;
2845
8d08fdba
MS
2846 /* When we parse a static member function definition,
2847 we put together a FUNCTION_DECL which thinks its type
2848 is METHOD_TYPE. Change that to FUNCTION_TYPE, and
2849 proceed. */
2850 if (TREE_CODE (f1) == METHOD_TYPE && DECL_STATIC_FUNCTION_P (olddecl))
700f8a87 2851 revert_static_member_fn (&newdecl, &f1, &p1);
8d08fdba
MS
2852 else if (TREE_CODE (f2) == METHOD_TYPE
2853 && DECL_STATIC_FUNCTION_P (newdecl))
700f8a87 2854 revert_static_member_fn (&olddecl, &f2, &p2);
8d08fdba
MS
2855
2856 /* Here we must take care of the case where new default
2857 parameters are specified. Also, warn if an old
2858 declaration becomes ambiguous because default
2859 parameters may cause the two to be ambiguous. */
2860 if (TREE_CODE (f1) != TREE_CODE (f2))
2861 {
2862 if (TREE_CODE (f1) == OFFSET_TYPE)
8251199e 2863 cp_compiler_error ("`%D' redeclared as member function", newdecl);
8d08fdba 2864 else
8251199e 2865 cp_compiler_error ("`%D' redeclared as non-member function", newdecl);
8d08fdba
MS
2866 return 0;
2867 }
2868
3bfdc719 2869 if (same_type_p (TREE_TYPE (f1), TREE_TYPE (f2)))
8926095f 2870 {
a28e3c7f 2871 if (! strict_prototypes_lang_c && DECL_LANGUAGE (olddecl) == lang_c
8926095f 2872 && p2 == NULL_TREE)
a28e3c7f
MS
2873 {
2874 types_match = self_promoting_args_p (p1);
2875 if (p1 == void_list_node)
2876 TREE_TYPE (newdecl) = TREE_TYPE (olddecl);
2877 }
2878 else if (!strict_prototypes_lang_c && DECL_LANGUAGE (olddecl)==lang_c
2879 && DECL_LANGUAGE (newdecl) == lang_c && p1 == NULL_TREE)
2880 {
2881 types_match = self_promoting_args_p (p2);
2882 TREE_TYPE (newdecl) = TREE_TYPE (olddecl);
2883 }
8926095f 2884 else
91063b51 2885 types_match = compparms (p1, p2);
8926095f 2886 }
8d08fdba
MS
2887 else
2888 types_match = 0;
2889 }
6b4b3deb 2890 else if (TREE_CODE (newdecl) == TEMPLATE_DECL)
51c184be 2891 {
f84b4be9
JM
2892 if (!comp_template_parms (DECL_TEMPLATE_PARMS (newdecl),
2893 DECL_TEMPLATE_PARMS (olddecl)))
2894 return 0;
2895
2896 if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL)
2897 types_match = 1;
2898 else
2899 types_match = decls_match (DECL_TEMPLATE_RESULT (olddecl),
2900 DECL_TEMPLATE_RESULT (newdecl));
51c184be 2901 }
8d08fdba
MS
2902 else
2903 {
2904 if (TREE_TYPE (newdecl) == error_mark_node)
2905 types_match = TREE_TYPE (olddecl) == error_mark_node;
2906 else if (TREE_TYPE (olddecl) == NULL_TREE)
2907 types_match = TREE_TYPE (newdecl) == NULL_TREE;
a0a33927
MS
2908 else if (TREE_TYPE (newdecl) == NULL_TREE)
2909 types_match = 0;
8d08fdba 2910 else
01240200 2911 types_match = comptypes (TREE_TYPE (newdecl),
3bfdc719
MM
2912 TREE_TYPE (olddecl),
2913 COMPARE_REDECLARATION);
8d08fdba
MS
2914 }
2915
2916 return types_match;
2917}
2918
2919/* If NEWDECL is `static' and an `extern' was seen previously,
2920 warn about it. (OLDDECL may be NULL_TREE; NAME contains
2921 information about previous usage as an `extern'.)
2922
2923 Note that this does not apply to the C++ case of declaring
2924 a variable `extern const' and then later `const'.
2925
8d08fdba
MS
2926 Don't complain about built-in functions, since they are beyond
2927 the user's control. */
2928
2929static void
2930warn_extern_redeclared_static (newdecl, olddecl)
2931 tree newdecl, olddecl;
2932{
2933 tree name;
2934
d8e178a0 2935 static const char *explicit_extern_static_warning
8251199e 2936 = "`%D' was declared `extern' and later `static'";
d8e178a0 2937 static const char *implicit_extern_static_warning
8251199e
JM
2938 = "`%D' was declared implicitly `extern' and later `static'";
2939
d22c8596 2940 if (TREE_CODE (newdecl) == TYPE_DECL)
8d08fdba
MS
2941 return;
2942
2943 name = DECL_ASSEMBLER_NAME (newdecl);
faae18ab 2944 if (TREE_PUBLIC (name) && DECL_THIS_STATIC (newdecl))
8d08fdba
MS
2945 {
2946 /* It's okay to redeclare an ANSI built-in function as static,
2947 or to declare a non-ANSI built-in function as anything. */
2948 if (! (TREE_CODE (newdecl) == FUNCTION_DECL
2949 && olddecl != NULL_TREE
2950 && TREE_CODE (olddecl) == FUNCTION_DECL
2951 && (DECL_BUILT_IN (olddecl)
2952 || DECL_BUILT_IN_NONANSI (olddecl))))
2953 {
a9aedbc2 2954 cp_pedwarn (IDENTIFIER_IMPLICIT_DECL (name)
8251199e
JM
2955 ? implicit_extern_static_warning
2956 : explicit_extern_static_warning, newdecl);
8d08fdba 2957 if (olddecl != NULL_TREE)
8251199e 2958 cp_pedwarn_at ("previous declaration of `%D'", olddecl);
8d08fdba
MS
2959 }
2960 }
2961}
2962
2963/* Handle when a new declaration NEWDECL has the same name as an old
2964 one OLDDECL in the same binding contour. Prints an error message
2965 if appropriate.
2966
2967 If safely possible, alter OLDDECL to look like NEWDECL, and return 1.
2968 Otherwise, return 0. */
2969
51c184be 2970int
8d08fdba 2971duplicate_decls (newdecl, olddecl)
824b9a4c 2972 tree newdecl, olddecl;
8d08fdba
MS
2973{
2974 extern struct obstack permanent_obstack;
2975 unsigned olddecl_uid = DECL_UID (olddecl);
2976 int olddecl_friend = 0, types_match = 0;
0b60dfe3 2977 int new_defines_function = 0;
5566b478
MS
2978
2979 if (newdecl == olddecl)
2980 return 1;
8d08fdba 2981
8926095f 2982 types_match = decls_match (newdecl, olddecl);
8d08fdba 2983
8d08fdba
MS
2984 /* If either the type of the new decl or the type of the old decl is an
2985 error_mark_node, then that implies that we have already issued an
2986 error (earlier) for some bogus type specification, and in that case,
2987 it is rather pointless to harass the user with yet more error message
0b60dfe3 2988 about the same declaration, so just pretend the types match here. */
bd6dd845
MS
2989 if (TREE_TYPE (newdecl) == error_mark_node
2990 || TREE_TYPE (olddecl) == error_mark_node)
8d08fdba 2991 types_match = 1;
0b60dfe3
BK
2992
2993 /* Check for redeclaration and other discrepancies. */
d22c8596
MS
2994 if (TREE_CODE (olddecl) == FUNCTION_DECL
2995 && DECL_ARTIFICIAL (olddecl)
2996 && (DECL_BUILT_IN (olddecl) || DECL_BUILT_IN_NONANSI (olddecl)))
8d08fdba
MS
2997 {
2998 /* If you declare a built-in or predefined function name as static,
a4443a08
MS
2999 the old definition is overridden, but optionally warn this was a
3000 bad choice of name. Ditto for overloads. */
893de33c 3001 if (! TREE_PUBLIC (newdecl)
a4443a08
MS
3002 || (TREE_CODE (newdecl) == FUNCTION_DECL
3003 && DECL_LANGUAGE (newdecl) != DECL_LANGUAGE (olddecl)))
3004 {
3005 if (warn_shadow)
8251199e 3006 cp_warning ("shadowing %s function `%#D'",
a4443a08
MS
3007 DECL_BUILT_IN (olddecl) ? "built-in" : "library",
3008 olddecl);
3009 /* Discard the old built-in function. */
3010 return 0;
3011 }
3012 else if (! types_match)
8d08fdba 3013 {
a4443a08
MS
3014 if (TREE_CODE (newdecl) != FUNCTION_DECL)
3015 {
3016 /* If the built-in is not ansi, then programs can override
3017 it even globally without an error. */
3018 if (! DECL_BUILT_IN (olddecl))
8251199e 3019 cp_warning ("library function `%#D' redeclared as non-function `%#D'",
a4443a08
MS
3020 olddecl, newdecl);
3021 else
3022 {
8251199e
JM
3023 cp_error ("declaration of `%#D'", newdecl);
3024 cp_error ("conflicts with built-in declaration `%#D'",
a4443a08
MS
3025 olddecl);
3026 }
3027 return 0;
3028 }
3029
8251199e
JM
3030 cp_warning ("declaration of `%#D'", newdecl);
3031 cp_warning ("conflicts with built-in declaration `%#D'",
8d08fdba 3032 olddecl);
8d08fdba 3033 }
39211cd5
MS
3034 }
3035 else if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
3036 {
9ed182dc
JM
3037 if ((TREE_CODE (olddecl) == TYPE_DECL && DECL_ARTIFICIAL (olddecl)
3038 && TREE_CODE (newdecl) != TYPE_DECL
3039 && ! (TREE_CODE (newdecl) == TEMPLATE_DECL
3040 && TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL))
3041 || (TREE_CODE (newdecl) == TYPE_DECL && DECL_ARTIFICIAL (newdecl)
3042 && TREE_CODE (olddecl) != TYPE_DECL
3043 && ! (TREE_CODE (olddecl) == TEMPLATE_DECL
3044 && (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl))
3045 == TYPE_DECL))))
3046 {
3047 /* We do nothing special here, because C++ does such nasty
3048 things with TYPE_DECLs. Instead, just let the TYPE_DECL
3049 get shadowed, and know that if we need to find a TYPE_DECL
3050 for a given name, we can look in the IDENTIFIER_TYPE_VALUE
3051 slot of the identifier. */
3052 return 0;
3053 }
3054
39211cd5 3055 if ((TREE_CODE (newdecl) == FUNCTION_DECL
5566b478 3056 && DECL_FUNCTION_TEMPLATE_P (olddecl))
39211cd5 3057 || (TREE_CODE (olddecl) == FUNCTION_DECL
5566b478 3058 && DECL_FUNCTION_TEMPLATE_P (newdecl)))
39211cd5 3059 return 0;
9ed182dc 3060
8251199e 3061 cp_error ("`%#D' redeclared as different kind of symbol", newdecl);
39211cd5
MS
3062 if (TREE_CODE (olddecl) == TREE_LIST)
3063 olddecl = TREE_VALUE (olddecl);
8251199e 3064 cp_error_at ("previous declaration of `%#D'", olddecl);
39211cd5
MS
3065
3066 /* New decl is completely inconsistent with the old one =>
3067 tell caller to replace the old one. */
3068
3069 return 0;
8d08fdba 3070 }
8d08fdba
MS
3071 else if (!types_match)
3072 {
7bdbfa05
MM
3073 if (DECL_REAL_CONTEXT (newdecl) != DECL_REAL_CONTEXT (olddecl))
3074 /* These are certainly not duplicate declarations; they're
3075 from different scopes. */
3076 return 0;
3077
8926095f 3078 if (TREE_CODE (newdecl) == TEMPLATE_DECL)
f0e01782
MS
3079 {
3080 /* The name of a class template may not be declared to refer to
3081 any other template, class, function, object, namespace, value,
e92cc029 3082 or type in the same scope. */
5566b478
MS
3083 if (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) == TYPE_DECL
3084 || TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL)
f0e01782 3085 {
8251199e
JM
3086 cp_error ("declaration of template `%#D'", newdecl);
3087 cp_error_at ("conflicts with previous declaration `%#D'",
f0e01782
MS
3088 olddecl);
3089 }
ec255269
MS
3090 else if (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) == FUNCTION_DECL
3091 && TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == FUNCTION_DECL
3092 && compparms (TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (olddecl))),
91063b51 3093 TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (newdecl))))
75650646
MM
3094 && comp_template_parms (DECL_TEMPLATE_PARMS (newdecl),
3095 DECL_TEMPLATE_PARMS (olddecl)))
ec255269 3096 {
8251199e
JM
3097 cp_error ("new declaration `%#D'", newdecl);
3098 cp_error_at ("ambiguates old declaration `%#D'", olddecl);
ec255269 3099 }
f0e01782
MS
3100 return 0;
3101 }
8926095f
MS
3102 if (TREE_CODE (newdecl) == FUNCTION_DECL)
3103 {
3104 if (DECL_LANGUAGE (newdecl) == lang_c
3105 && DECL_LANGUAGE (olddecl) == lang_c)
3106 {
8251199e 3107 cp_error ("declaration of C function `%#D' conflicts with",
8926095f 3108 newdecl);
8251199e 3109 cp_error_at ("previous declaration `%#D' here", olddecl);
8926095f 3110 }
00595019 3111 else if (compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
91063b51 3112 TYPE_ARG_TYPES (TREE_TYPE (olddecl))))
700f8a87 3113 {
8251199e
JM
3114 cp_error ("new declaration `%#D'", newdecl);
3115 cp_error_at ("ambiguates old declaration `%#D'", olddecl);
700f8a87
MS
3116 }
3117 else
3118 return 0;
8926095f 3119 }
8d08fdba
MS
3120
3121 /* Already complained about this, so don't do so again. */
a4443a08 3122 else if (current_class_type == NULL_TREE
8d08fdba
MS
3123 || IDENTIFIER_ERROR_LOCUS (DECL_ASSEMBLER_NAME (newdecl)) != current_class_type)
3124 {
8251199e
JM
3125 cp_error ("conflicting types for `%#D'", newdecl);
3126 cp_error_at ("previous declaration as `%#D'", olddecl);
8d08fdba
MS
3127 }
3128 }
75650646
MM
3129 else if (TREE_CODE (newdecl) == FUNCTION_DECL
3130 && ((DECL_TEMPLATE_SPECIALIZATION (olddecl)
3131 && (!DECL_TEMPLATE_INFO (newdecl)
3132 || (DECL_TI_TEMPLATE (newdecl)
3133 != DECL_TI_TEMPLATE (olddecl))))
3134 || (DECL_TEMPLATE_SPECIALIZATION (newdecl)
3135 && (!DECL_TEMPLATE_INFO (olddecl)
3136 || (DECL_TI_TEMPLATE (olddecl)
3137 != DECL_TI_TEMPLATE (newdecl))))))
386b8a85
JM
3138 /* It's OK to have a template specialization and a non-template
3139 with the same type, or to have specializations of two
75650646
MM
3140 different templates with the same type. Note that if one is a
3141 specialization, and the other is an instantiation of the same
3142 template, that we do not exit at this point. That situation
3143 can occur if we instantiate a template class, and then
3144 specialize one of its methods. This situation is legal, but
3145 the declarations must be merged in the usual way. */
3146 return 0;
3147 else if (TREE_CODE (newdecl) == FUNCTION_DECL
3148 && ((DECL_TEMPLATE_INSTANTIATION (olddecl)
3149 && !DECL_USE_TEMPLATE (newdecl))
3150 || (DECL_TEMPLATE_INSTANTIATION (newdecl)
3151 && !DECL_USE_TEMPLATE (olddecl))))
3152 /* One of the declarations is a template instantiation, and the
3153 other is not a template at all. That's OK. */
386b8a85 3154 return 0;
85c6cbaf
ML
3155 else if (TREE_CODE (newdecl) == NAMESPACE_DECL
3156 && DECL_NAMESPACE_ALIAS (newdecl)
3157 && DECL_NAMESPACE_ALIAS (newdecl) == DECL_NAMESPACE_ALIAS (olddecl))
3158 /* Redeclaration of namespace alias, ignore it. */
3159 return 1;
8d08fdba
MS
3160 else
3161 {
d8e178a0 3162 const char *errmsg = redeclaration_error_message (newdecl, olddecl);
8251199e 3163 if (errmsg)
8d08fdba 3164 {
8251199e 3165 cp_error (errmsg, newdecl);
8d08fdba
MS
3166 if (DECL_NAME (olddecl) != NULL_TREE)
3167 cp_error_at ((DECL_INITIAL (olddecl)
2c73f9f5 3168 && namespace_bindings_p ())
8251199e
JM
3169 ? "`%#D' previously defined here"
3170 : "`%#D' previously declared here", olddecl);
8d08fdba
MS
3171 }
3172 else if (TREE_CODE (olddecl) == FUNCTION_DECL
3173 && DECL_INITIAL (olddecl) != NULL_TREE
3174 && TYPE_ARG_TYPES (TREE_TYPE (olddecl)) == NULL_TREE
3175 && TYPE_ARG_TYPES (TREE_TYPE (newdecl)) != NULL_TREE)
3176 {
3177 /* Prototype decl follows defn w/o prototype. */
8251199e
JM
3178 cp_warning_at ("prototype for `%#D'", newdecl);
3179 cp_warning_at ("follows non-prototype definition here", olddecl);
8d08fdba
MS
3180 }
3181 else if (TREE_CODE (olddecl) == FUNCTION_DECL
3182 && DECL_LANGUAGE (newdecl) != DECL_LANGUAGE (olddecl))
8926095f
MS
3183 {
3184 /* extern "C" int foo ();
3185 int foo () { bar (); }
3186 is OK. */
3187 if (current_lang_stack == current_lang_base)
a28e3c7f 3188 DECL_LANGUAGE (newdecl) = DECL_LANGUAGE (olddecl);
8926095f
MS
3189 else
3190 {
8251199e 3191 cp_error_at ("previous declaration of `%#D' with %L linkage",
8926095f 3192 olddecl, DECL_LANGUAGE (olddecl));
8251199e 3193 cp_error ("conflicts with new declaration with %L linkage",
8926095f
MS
3194 DECL_LANGUAGE (newdecl));
3195 }
3196 }
e1cd6e56 3197
a6f02587 3198 if (DECL_LANG_SPECIFIC (olddecl) && DECL_USE_TEMPLATE (olddecl))
c32381b1
JM
3199 ;
3200 else if (TREE_CODE (olddecl) == FUNCTION_DECL)
e1cd6e56
MS
3201 {
3202 tree t1 = TYPE_ARG_TYPES (TREE_TYPE (olddecl));
3203 tree t2 = TYPE_ARG_TYPES (TREE_TYPE (newdecl));
3204 int i = 1;
3205
3206 if (TREE_CODE (TREE_TYPE (newdecl)) == METHOD_TYPE)
3207 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2);
3208
3209 for (; t1 && t1 != void_list_node;
3210 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2), i++)
3211 if (TREE_PURPOSE (t1) && TREE_PURPOSE (t2))
3212 {
2507f3b5
RK
3213 if (1 == simple_cst_equal (TREE_PURPOSE (t1),
3214 TREE_PURPOSE (t2)))
e1cd6e56
MS
3215 {
3216 if (pedantic)
3217 {
8251199e 3218 cp_pedwarn ("default argument given for parameter %d of `%#D'",
e1cd6e56 3219 i, newdecl);
8251199e 3220 cp_pedwarn_at ("after previous specification in `%#D'",
e1cd6e56
MS
3221 olddecl);
3222 }
3223 }
3224 else
3225 {
8251199e 3226 cp_error ("default argument given for parameter %d of `%#D'",
e1cd6e56 3227 i, newdecl);
8251199e 3228 cp_error_at ("after previous specification in `%#D'",
e1cd6e56
MS
3229 olddecl);
3230 }
3231 }
a5894242 3232
7fcdf4c2
MS
3233 if (DECL_THIS_INLINE (newdecl) && ! DECL_THIS_INLINE (olddecl)
3234 && TREE_ADDRESSABLE (olddecl) && warn_inline)
dff6b454 3235 {
8251199e 3236 cp_warning ("`%#D' was used before it was declared inline",
7fcdf4c2 3237 newdecl);
8251199e 3238 cp_warning_at ("previous non-inline declaration here",
7fcdf4c2 3239 olddecl);
dff6b454 3240 }
e1cd6e56 3241 }
8d08fdba
MS
3242 }
3243
3244 /* If new decl is `static' and an `extern' was seen previously,
3245 warn about it. */
3246 warn_extern_redeclared_static (newdecl, olddecl);
3247
e92cc029 3248 /* We have committed to returning 1 at this point. */
8d08fdba
MS
3249 if (TREE_CODE (newdecl) == FUNCTION_DECL)
3250 {
3251 /* Now that functions must hold information normally held
3252 by field decls, there is extra work to do so that
3253 declaration information does not get destroyed during
3254 definition. */
3255 if (DECL_VINDEX (olddecl))
3256 DECL_VINDEX (newdecl) = DECL_VINDEX (olddecl);
3257 if (DECL_CONTEXT (olddecl))
3258 DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
3259 if (DECL_CLASS_CONTEXT (olddecl))
3260 DECL_CLASS_CONTEXT (newdecl) = DECL_CLASS_CONTEXT (olddecl);
8d08fdba
MS
3261 if (DECL_PENDING_INLINE_INFO (newdecl) == (struct pending_inline *)0)
3262 DECL_PENDING_INLINE_INFO (newdecl) = DECL_PENDING_INLINE_INFO (olddecl);
e1cd6e56
MS
3263 DECL_STATIC_CONSTRUCTOR (newdecl) |= DECL_STATIC_CONSTRUCTOR (olddecl);
3264 DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
7215f9a0 3265 DECL_ABSTRACT_VIRTUAL_P (newdecl) |= DECL_ABSTRACT_VIRTUAL_P (olddecl);
864b83b9 3266 DECL_VIRTUAL_P (newdecl) |= DECL_VIRTUAL_P (olddecl);
4a67c9e9 3267 DECL_NEEDS_FINAL_OVERRIDER_P (newdecl) |= DECL_NEEDS_FINAL_OVERRIDER_P (olddecl);
0b60dfe3
BK
3268 new_defines_function = DECL_INITIAL (newdecl) != NULL_TREE;
3269
3270 /* Optionally warn about more than one declaration for the same
3271 name, but don't warn about a function declaration followed by a
3272 definition. */
3273 if (warn_redundant_decls && ! DECL_ARTIFICIAL (olddecl)
3274 && !(new_defines_function && DECL_INITIAL (olddecl) == NULL_TREE)
3275 /* Don't warn about extern decl followed by definition. */
3276 && !(DECL_EXTERNAL (olddecl) && ! DECL_EXTERNAL (newdecl))
3277 /* Don't warn about friends, let add_friend take care of it. */
3278 && ! DECL_FRIEND_P (newdecl))
3279 {
8251199e
JM
3280 cp_warning ("redundant redeclaration of `%D' in same scope", newdecl);
3281 cp_warning_at ("previous declaration of `%D'", olddecl);
0b60dfe3 3282 }
8d08fdba
MS
3283 }
3284
3285 /* Deal with C++: must preserve virtual function table size. */
3286 if (TREE_CODE (olddecl) == TYPE_DECL)
3287 {
3288 register tree newtype = TREE_TYPE (newdecl);
3289 register tree oldtype = TREE_TYPE (olddecl);
3290
3291 if (newtype != error_mark_node && oldtype != error_mark_node
3292 && TYPE_LANG_SPECIFIC (newtype) && TYPE_LANG_SPECIFIC (oldtype))
3293 {
3294 CLASSTYPE_VSIZE (newtype) = CLASSTYPE_VSIZE (oldtype);
3295 CLASSTYPE_FRIEND_CLASSES (newtype)
3296 = CLASSTYPE_FRIEND_CLASSES (oldtype);
3297 }
8d08fdba
MS
3298 }
3299
8d08fdba
MS
3300 /* Copy all the DECL_... slots specified in the new decl
3301 except for any that we copy here from the old type. */
0b60dfe3
BK
3302 DECL_MACHINE_ATTRIBUTES (newdecl)
3303 = merge_machine_decl_attributes (olddecl, newdecl);
8d08fdba 3304
5566b478
MS
3305 if (TREE_CODE (newdecl) == TEMPLATE_DECL)
3306 {
4d85e00e
MM
3307 if (! duplicate_decls (DECL_TEMPLATE_RESULT (newdecl),
3308 DECL_TEMPLATE_RESULT (olddecl)))
8251199e 3309 cp_error ("invalid redeclaration of %D", newdecl);
4d85e00e 3310 TREE_TYPE (olddecl) = TREE_TYPE (DECL_TEMPLATE_RESULT (olddecl));
4d85e00e
MM
3311 DECL_TEMPLATE_SPECIALIZATIONS (olddecl)
3312 = chainon (DECL_TEMPLATE_SPECIALIZATIONS (olddecl),
3313 DECL_TEMPLATE_SPECIALIZATIONS (newdecl));
0b60dfe3 3314
5566b478
MS
3315 return 1;
3316 }
0b60dfe3 3317
8d08fdba
MS
3318 if (types_match)
3319 {
3320 /* Automatically handles default parameters. */
3321 tree oldtype = TREE_TYPE (olddecl);
e1cd6e56 3322 tree newtype;
8d08fdba 3323
e1cd6e56
MS
3324 /* Make sure we put the new type in the same obstack as the old one. */
3325 if (oldtype)
39211cd5
MS
3326 push_obstacks (TYPE_OBSTACK (oldtype), TYPE_OBSTACK (oldtype));
3327 else
3328 {
3329 push_obstacks_nochange ();
3330 end_temporary_allocation ();
3331 }
3332
e1cd6e56
MS
3333 /* Merge the data types specified in the two decls. */
3334 newtype = common_type (TREE_TYPE (newdecl), TREE_TYPE (olddecl));
3335
8d08fdba
MS
3336 if (TREE_CODE (newdecl) == VAR_DECL)
3337 DECL_THIS_EXTERN (newdecl) |= DECL_THIS_EXTERN (olddecl);
3338 /* Do this after calling `common_type' so that default
3339 parameters don't confuse us. */
3340 else if (TREE_CODE (newdecl) == FUNCTION_DECL
3341 && (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (newdecl))
3342 != TYPE_RAISES_EXCEPTIONS (TREE_TYPE (olddecl))))
3343 {
f30432d7 3344 TREE_TYPE (newdecl) = build_exception_variant (newtype,
8d08fdba 3345 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (newdecl)));
f30432d7 3346 TREE_TYPE (olddecl) = build_exception_variant (newtype,
8d08fdba
MS
3347 TYPE_RAISES_EXCEPTIONS (oldtype));
3348
9a224b4a
JM
3349 if ((pedantic || ! DECL_IN_SYSTEM_HEADER (olddecl))
3350 && DECL_SOURCE_LINE (olddecl) != 0
da20811c 3351 && flag_exceptions
824b9a4c 3352 && ! compexcepttypes (TREE_TYPE (newdecl), TREE_TYPE (olddecl)))
8d08fdba 3353 {
8251199e 3354 cp_pedwarn ("declaration of `%D' throws different exceptions",
a28e3c7f 3355 newdecl);
8251199e 3356 cp_pedwarn_at ("previous declaration here", olddecl);
8d08fdba
MS
3357 }
3358 }
3359 TREE_TYPE (newdecl) = TREE_TYPE (olddecl) = newtype;
3360
3361 /* Lay the type out, unless already done. */
407f03b8 3362 if (! same_type_p (newtype, oldtype)
5566b478 3363 && TREE_TYPE (newdecl) != error_mark_node
5156628f 3364 && !(processing_template_decl && uses_template_parms (newdecl)))
b7484fbe
MS
3365 layout_type (TREE_TYPE (newdecl));
3366
5566b478
MS
3367 if ((TREE_CODE (newdecl) == VAR_DECL
3368 || TREE_CODE (newdecl) == PARM_DECL
3369 || TREE_CODE (newdecl) == RESULT_DECL
3370 || TREE_CODE (newdecl) == FIELD_DECL
3371 || TREE_CODE (newdecl) == TYPE_DECL)
5156628f 3372 && !(processing_template_decl && uses_template_parms (newdecl)))
b7484fbe 3373 layout_decl (newdecl, 0);
8d08fdba
MS
3374
3375 /* Merge the type qualifiers. */
3376 if (TREE_READONLY (newdecl))
3377 TREE_READONLY (olddecl) = 1;
3378 if (TREE_THIS_VOLATILE (newdecl))
3379 TREE_THIS_VOLATILE (olddecl) = 1;
3380
3381 /* Merge the initialization information. */
8926095f
MS
3382 if (DECL_INITIAL (newdecl) == NULL_TREE
3383 && DECL_INITIAL (olddecl) != NULL_TREE)
3384 {
3385 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
3386 DECL_SOURCE_FILE (newdecl) = DECL_SOURCE_FILE (olddecl);
3387 DECL_SOURCE_LINE (newdecl) = DECL_SOURCE_LINE (olddecl);
4d85e00e
MM
3388 if (DECL_LANG_SPECIFIC (newdecl)
3389 && DECL_LANG_SPECIFIC (olddecl))
3390 DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
8926095f 3391 }
39211cd5
MS
3392
3393 /* Merge the section attribute.
3394 We want to issue an error if the sections conflict but that must be
3395 done later in decl_attributes since we are called before attributes
3396 are assigned. */
3397 if (DECL_SECTION_NAME (newdecl) == NULL_TREE)
3398 DECL_SECTION_NAME (newdecl) = DECL_SECTION_NAME (olddecl);
3399
8d08fdba
MS
3400 /* Keep the old rtl since we can safely use it, unless it's the
3401 call to abort() used for abstract virtuals. */
3402 if ((DECL_LANG_SPECIFIC (olddecl)
3403 && !DECL_ABSTRACT_VIRTUAL_P (olddecl))
3404 || DECL_RTL (olddecl) != DECL_RTL (abort_fndecl))
3405 DECL_RTL (newdecl) = DECL_RTL (olddecl);
39211cd5
MS
3406
3407 pop_obstacks ();
8d08fdba
MS
3408 }
3409 /* If cannot merge, then use the new type and qualifiers,
3410 and don't preserve the old rtl. */
3411 else
3412 {
3413 /* Clean out any memory we had of the old declaration. */
3414 tree oldstatic = value_member (olddecl, static_aggregates);
3415 if (oldstatic)
3416 TREE_VALUE (oldstatic) = error_mark_node;
3417
3418 TREE_TYPE (olddecl) = TREE_TYPE (newdecl);
3419 TREE_READONLY (olddecl) = TREE_READONLY (newdecl);
3420 TREE_THIS_VOLATILE (olddecl) = TREE_THIS_VOLATILE (newdecl);
3421 TREE_SIDE_EFFECTS (olddecl) = TREE_SIDE_EFFECTS (newdecl);
3422 }
3423
3424 /* Merge the storage class information. */
a9aedbc2 3425 DECL_WEAK (newdecl) |= DECL_WEAK (olddecl);
e92cc029 3426 DECL_ONE_ONLY (newdecl) |= DECL_ONE_ONLY (olddecl);
a9aedbc2
MS
3427 TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
3428 TREE_STATIC (olddecl) = TREE_STATIC (newdecl) |= TREE_STATIC (olddecl);
3429 if (! DECL_EXTERNAL (olddecl))
3430 DECL_EXTERNAL (newdecl) = 0;
0b60dfe3
BK
3431
3432 if (DECL_LANG_SPECIFIC (newdecl) && DECL_LANG_SPECIFIC (olddecl))
8d08fdba 3433 {
a9aedbc2
MS
3434 DECL_INTERFACE_KNOWN (newdecl) |= DECL_INTERFACE_KNOWN (olddecl);
3435 DECL_NOT_REALLY_EXTERN (newdecl) |= DECL_NOT_REALLY_EXTERN (olddecl);
77be6f82 3436 DECL_COMDAT (newdecl) |= DECL_COMDAT (olddecl);
2b0a63a3
MM
3437 DECL_TEMPLATE_INSTANTIATED (newdecl)
3438 |= DECL_TEMPLATE_INSTANTIATED (olddecl);
0b60dfe3
BK
3439 /* Don't really know how much of the language-specific
3440 values we should copy from old to new. */
3441 DECL_IN_AGGR_P (newdecl) = DECL_IN_AGGR_P (olddecl);
3442 DECL_ACCESS (newdecl) = DECL_ACCESS (olddecl);
3443 DECL_NONCONVERTING_P (newdecl) = DECL_NONCONVERTING_P (olddecl);
fbf1c34b 3444 DECL_TEMPLATE_INFO (newdecl) = DECL_TEMPLATE_INFO (olddecl);
0b60dfe3 3445 olddecl_friend = DECL_FRIEND_P (olddecl);
6a629cac
MM
3446
3447 /* Only functions have DECL_BEFRIENDING_CLASSES. */
3448 if (TREE_CODE (newdecl) == FUNCTION_DECL
3449 || DECL_FUNCTION_TEMPLATE_P (newdecl))
3450 DECL_BEFRIENDING_CLASSES (newdecl)
3451 = chainon (DECL_BEFRIENDING_CLASSES (newdecl),
3452 DECL_BEFRIENDING_CLASSES (olddecl));
8d08fdba
MS
3453 }
3454
8d08fdba
MS
3455 if (TREE_CODE (newdecl) == FUNCTION_DECL)
3456 {
75650646
MM
3457 if (DECL_TEMPLATE_INSTANTIATION (olddecl)
3458 && !DECL_TEMPLATE_INSTANTIATION (newdecl))
3459 {
3460 /* If newdecl is not a specialization, then it is not a
3461 template-related function at all. And that means that we
3462 shoud have exited above, returning 0. */
3463 my_friendly_assert (DECL_TEMPLATE_SPECIALIZATION (newdecl),
3464 0);
3465
3466 if (TREE_USED (olddecl))
3467 /* From [temp.expl.spec]:
3468
3469 If a template, a member template or the member of a class
3470 template is explicitly specialized then that
3471 specialization shall be declared before the first use of
3472 that specialization that would cause an implicit
3473 instantiation to take place, in every translation unit in
3474 which such a use occurs. */
8251199e 3475 cp_error ("explicit specialization of %D after first use",
75650646
MM
3476 olddecl);
3477
3478 SET_DECL_TEMPLATE_SPECIALIZATION (olddecl);
3479 }
faae18ab
MS
3480 DECL_THIS_INLINE (newdecl) |= DECL_THIS_INLINE (olddecl);
3481
3482 /* If either decl says `inline', this fn is inline, unless its
3483 definition was passed already. */
3484 if (DECL_INLINE (newdecl) && DECL_INITIAL (olddecl) == NULL_TREE)
3485 DECL_INLINE (olddecl) = 1;
3486 DECL_INLINE (newdecl) = DECL_INLINE (olddecl);
3487
700f8a87
MS
3488 if (! types_match)
3489 {
3490 DECL_LANGUAGE (olddecl) = DECL_LANGUAGE (newdecl);
3491 DECL_ASSEMBLER_NAME (olddecl) = DECL_ASSEMBLER_NAME (newdecl);
5566b478
MS
3492 DECL_RTL (olddecl) = DECL_RTL (newdecl);
3493 }
3494 if (! types_match || new_defines_function)
3495 {
3496 /* These need to be copied so that the names are available. */
700f8a87
MS
3497 DECL_ARGUMENTS (olddecl) = DECL_ARGUMENTS (newdecl);
3498 DECL_RESULT (olddecl) = DECL_RESULT (newdecl);
700f8a87 3499 }
8d08fdba
MS
3500 if (new_defines_function)
3501 /* If defining a function declared with other language
3502 linkage, use the previously declared language linkage. */
3503 DECL_LANGUAGE (newdecl) = DECL_LANGUAGE (olddecl);
3504 else
3505 {
3506 /* If redeclaring a builtin function, and not a definition,
3507 it stays built in. */
3508 if (DECL_BUILT_IN (olddecl))
3509 {
3510 DECL_BUILT_IN (newdecl) = 1;
39211cd5 3511 DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl);
8d08fdba
MS
3512 /* If we're keeping the built-in definition, keep the rtl,
3513 regardless of declaration matches. */
3514 DECL_RTL (newdecl) = DECL_RTL (olddecl);
3515 }
3516 else
3517 DECL_FRAME_SIZE (newdecl) = DECL_FRAME_SIZE (olddecl);
3518
3519 DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
8926095f 3520 if ((DECL_SAVED_INSNS (newdecl) = DECL_SAVED_INSNS (olddecl)))
8d08fdba
MS
3521 /* Previously saved insns go together with
3522 the function's previous definition. */
3523 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
3524 /* Don't clear out the arguments if we're redefining a function. */
3525 if (DECL_ARGUMENTS (olddecl))
3526 DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl);
3527 }
6060a796
MS
3528 if (DECL_LANG_SPECIFIC (olddecl))
3529 DECL_MAIN_VARIANT (newdecl) = DECL_MAIN_VARIANT (olddecl);
8d08fdba
MS
3530 }
3531
a9aedbc2
MS
3532 if (TREE_CODE (newdecl) == NAMESPACE_DECL)
3533 {
3534 NAMESPACE_LEVEL (newdecl) = NAMESPACE_LEVEL (olddecl);
3535 }
3536
8d08fdba
MS
3537 /* Now preserve various other info from the definition. */
3538 TREE_ADDRESSABLE (newdecl) = TREE_ADDRESSABLE (olddecl);
3539 TREE_ASM_WRITTEN (newdecl) = TREE_ASM_WRITTEN (olddecl);
3540 DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
f376e137 3541 DECL_ASSEMBLER_NAME (newdecl) = DECL_ASSEMBLER_NAME (olddecl);
8d08fdba 3542
8d08fdba
MS
3543 if (TREE_CODE (newdecl) == FUNCTION_DECL)
3544 {
3545 int function_size;
3546 struct lang_decl *ol = DECL_LANG_SPECIFIC (olddecl);
3547 struct lang_decl *nl = DECL_LANG_SPECIFIC (newdecl);
3548
3549 function_size = sizeof (struct tree_decl);
3550
3551 bcopy ((char *) newdecl + sizeof (struct tree_common),
3552 (char *) olddecl + sizeof (struct tree_common),
3553 function_size - sizeof (struct tree_common));
3554
3555 /* Can we safely free the storage used by newdecl? */
3556
3557#define ROUND(x) ((x + obstack_alignment_mask (&permanent_obstack)) \
3558 & ~ obstack_alignment_mask (&permanent_obstack))
3559
75650646
MM
3560 if (DECL_TEMPLATE_INSTANTIATION (newdecl))
3561 {
3562 /* If newdecl is a template instantiation, it is possible that
3563 the following sequence of events has occurred:
3564
3565 o A friend function was declared in a class template. The
3566 class template was instantiated.
3567
3568 o The instantiation of the friend declaration was
3569 recorded on the instantiation list, and is newdecl.
3570
3571 o Later, however, instantiate_class_template called pushdecl
3572 on the newdecl to perform name injection. But, pushdecl in
3573 turn called duplicate_decls when it discovered that another
3574 declaration of a global function with the same name already
3575 existed.
3576
3577 o Here, in duplicate_decls, we decided to clobber newdecl.
3578
3579 If we're going to do that, we'd better make sure that
3580 olddecl, and not newdecl, is on the list of
3581 instantiations so that if we try to do the instantiation
3582 again we won't get the clobbered declaration. */
3583
3584 tree tmpl = DECL_TI_TEMPLATE (newdecl);
3585 tree decls = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
3586
3587 for (; decls; decls = TREE_CHAIN (decls))
3588 if (TREE_VALUE (decls) == newdecl)
3589 TREE_VALUE (decls) = olddecl;
3590 }
3591
3a56f0ab
JM
3592 if (((char *)newdecl + ROUND (function_size) == (char *)nl
3593 && ((char *)newdecl + ROUND (function_size)
3594 + ROUND (sizeof (struct lang_decl))
3595 == obstack_next_free (&permanent_obstack)))
3596 || ((char *)newdecl + ROUND (function_size)
3597 == obstack_next_free (&permanent_obstack)))
8d08fdba
MS
3598 {
3599 DECL_MAIN_VARIANT (newdecl) = olddecl;
3600 DECL_LANG_SPECIFIC (olddecl) = ol;
3601 bcopy ((char *)nl, (char *)ol, sizeof (struct lang_decl));
3602
3603 obstack_free (&permanent_obstack, newdecl);
3604 }
d22c8596 3605 else if (LANG_DECL_PERMANENT (ol) && ol != nl)
8d08fdba
MS
3606 {
3607 if (DECL_MAIN_VARIANT (olddecl) == olddecl)
3608 {
3609 /* Save these lang_decls that would otherwise be lost. */
3610 extern tree free_lang_decl_chain;
3611 tree free_lang_decl = (tree) ol;
d22c8596
MS
3612
3613 if (DECL_LANG_SPECIFIC (olddecl) == ol)
3614 abort ();
3615
8d08fdba
MS
3616 TREE_CHAIN (free_lang_decl) = free_lang_decl_chain;
3617 free_lang_decl_chain = free_lang_decl;
3618 }
3619 else
3620 {
bd6dd845 3621 /* Storage leak. */;
8d08fdba
MS
3622 }
3623 }
3624 }
3625 else
3626 {
3627 bcopy ((char *) newdecl + sizeof (struct tree_common),
3628 (char *) olddecl + sizeof (struct tree_common),
3629 sizeof (struct tree_decl) - sizeof (struct tree_common)
3630 + tree_code_length [(int)TREE_CODE (newdecl)] * sizeof (char *));
3631 }
3632
3633 DECL_UID (olddecl) = olddecl_uid;
3634 if (olddecl_friend)
3635 DECL_FRIEND_P (olddecl) = 1;
3636
d9525bec
BK
3637 /* NEWDECL contains the merged attribute lists.
3638 Update OLDDECL to be the same. */
3639 DECL_MACHINE_ATTRIBUTES (olddecl) = DECL_MACHINE_ATTRIBUTES (newdecl);
3640
8d08fdba
MS
3641 return 1;
3642}
3643
3644/* Record a decl-node X as belonging to the current lexical scope.
3645 Check for errors (such as an incompatible declaration for the same
3646 name already seen in the same scope).
3647
3648 Returns either X or an old decl for the same name.
3649 If an old decl is returned, it may have been smashed
3650 to agree with what X says. */
3651
3652tree
3653pushdecl (x)
3654 tree x;
3655{
3656 register tree t;
8d08fdba 3657 register tree name = DECL_ASSEMBLER_NAME (x);
f181d4ae 3658 int need_new_binding = 1;
8d08fdba 3659
50714e79
MM
3660 if (DECL_TEMPLATE_PARM_P (x))
3661 /* Template parameters have no context; they are not X::T even
3662 when declared within a class or namespace. */
3663 ;
3664 else
3665 {
3666 if (current_function_decl && x != current_function_decl
3667 /* A local declaration for a function doesn't constitute
3668 nesting. */
3669 && (TREE_CODE (x) != FUNCTION_DECL || DECL_INITIAL (x))
3670 /* Don't change DECL_CONTEXT of virtual methods. */
3671 && (TREE_CODE (x) != FUNCTION_DECL || !DECL_VIRTUAL_P (x))
3672 && !DECL_CONTEXT (x))
3673 DECL_CONTEXT (x) = current_function_decl;
3674 if (!DECL_CONTEXT (x))
3675 DECL_CONTEXT (x) = FROB_CONTEXT (current_namespace);
3676 }
8d08fdba 3677
8d08fdba 3678 /* Type are looked up using the DECL_NAME, as that is what the rest of the
e92cc029 3679 compiler wants to use. */
a9aedbc2 3680 if (TREE_CODE (x) == TYPE_DECL || TREE_CODE (x) == VAR_DECL
0c20d3d6 3681 || TREE_CODE (x) == NAMESPACE_DECL)
8d08fdba 3682 name = DECL_NAME (x);
8d08fdba
MS
3683
3684 if (name)
3685 {
5566b478
MS
3686#if 0
3687 /* Not needed...see below. */
8d08fdba
MS
3688 char *file;
3689 int line;
5566b478 3690#endif
386b8a85
JM
3691 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
3692 name = TREE_OPERAND (name, 0);
3693
2c73f9f5 3694 /* Namespace-scoped variables are not found in the current level. */
6eb3bb27 3695 if (TREE_CODE (x) == VAR_DECL && DECL_NAMESPACE_SCOPE_P (x))
2c73f9f5
ML
3696 t = namespace_binding (name, DECL_CONTEXT (x));
3697 else
3698 t = lookup_name_current_level (name);
8d08fdba
MS
3699 if (t == error_mark_node)
3700 {
3701 /* error_mark_node is 0 for a while during initialization! */
3702 t = NULL_TREE;
8251199e 3703 cp_error_at ("`%#D' used prior to declaration", x);
8d08fdba
MS
3704 }
3705
51c184be 3706 else if (t != NULL_TREE)
8d08fdba 3707 {
5566b478
MS
3708#if 0
3709 /* This is turned off until I have time to do it right (bpk). */
e92cc029 3710 /* With the code below that uses it... */
8d6e462b
PB
3711 file = DECL_SOURCE_FILE (t);
3712 line = DECL_SOURCE_LINE (t);
5566b478 3713#endif
2ee887f2 3714 if (TREE_CODE (t) == PARM_DECL)
8d08fdba
MS
3715 {
3716 if (DECL_CONTEXT (t) == NULL_TREE)
3717 fatal ("parse errors have confused me too much");
be99da77 3718
e92cc029 3719 /* Check for duplicate params. */
be99da77
MS
3720 if (duplicate_decls (x, t))
3721 return t;
8d08fdba 3722 }
8d6e462b 3723 else if (((TREE_CODE (x) == FUNCTION_DECL && DECL_LANGUAGE (x) == lang_c)
5566b478
MS
3724 || DECL_FUNCTION_TEMPLATE_P (x))
3725 && is_overloaded_fn (t))
2c73f9f5 3726 /* Don't do anything just yet. */;
e1cd6e56
MS
3727 else if (t == wchar_decl_node)
3728 {
3729 if (pedantic && ! DECL_IN_SYSTEM_HEADER (x))
8251199e 3730 cp_pedwarn ("redeclaration of wchar_t as `%T'", TREE_TYPE (x));
e1cd6e56
MS
3731
3732 /* Throw away the redeclaration. */
3733 return t;
3734 }
8926095f 3735 else if (TREE_CODE (t) != TREE_CODE (x))
8d08fdba 3736 {
9ed182dc 3737 if (duplicate_decls (x, t))
51c184be 3738 return t;
8d08fdba
MS
3739 }
3740 else if (duplicate_decls (x, t))
51c184be 3741 {
8d08fdba 3742#if 0
8926095f 3743 /* This is turned off until I have time to do it right (bpk). */
8d08fdba 3744
8926095f
MS
3745 /* Also warn if they did a prototype with `static' on it, but
3746 then later left the `static' off. */
3747 if (! TREE_PUBLIC (name) && TREE_PUBLIC (x))
8d08fdba 3748 {
8926095f
MS
3749 if (DECL_LANG_SPECIFIC (t) && DECL_FRIEND_P (t))
3750 return t;
8d08fdba 3751
8926095f
MS
3752 if (extra_warnings)
3753 {
8251199e 3754 cp_warning ("`static' missing from declaration of `%D'",
a28e3c7f 3755 t);
8926095f
MS
3756 warning_with_file_and_line (file, line,
3757 "previous declaration of `%s'",
3758 decl_as_string (t, 0));
3759 }
8d08fdba 3760
8926095f
MS
3761 /* Now fix things so it'll do what they expect. */
3762 if (current_function_decl)
3763 TREE_PUBLIC (current_function_decl) = 0;
3764 }
51c184be
MS
3765 /* Due to interference in memory reclamation (X may be
3766 obstack-deallocated at this point), we must guard against
8926095f
MS
3767 one really special case. [jason: This should be handled
3768 by start_function] */
51c184be
MS
3769 if (current_function_decl == x)
3770 current_function_decl = t;
8926095f 3771#endif
7177d104
MS
3772 if (TREE_CODE (t) == TYPE_DECL)
3773 SET_IDENTIFIER_TYPE_VALUE (name, TREE_TYPE (t));
f30432d7
MS
3774 else if (TREE_CODE (t) == FUNCTION_DECL)
3775 check_default_args (t);
7177d104 3776
51c184be
MS
3777 return t;
3778 }
35680744
MM
3779 else if (DECL_MAIN_P (x))
3780 {
3781 /* A redeclaration of main, but not a duplicate of the
3782 previous one.
3783
3784 [basic.start.main]
3785
3786 This function shall not be overloaded. */
8251199e
JM
3787 cp_error_at ("invalid redeclaration of `%D'", t);
3788 cp_error ("as `%D'", x);
35680744
MM
3789 /* We don't try to push this declaration since that
3790 causes a crash. */
3791 return x;
3792 }
8d08fdba 3793 }
8926095f 3794
f3400fe2
JM
3795 check_template_shadow (x);
3796
fcfcdfc8
JM
3797 /* If this is a function conjured up by the backend, massage it
3798 so it looks friendly. */
3799 if (TREE_CODE (x) == FUNCTION_DECL
3800 && ! DECL_LANG_SPECIFIC (x))
3801 {
3802 retrofit_lang_decl (x);
3803 DECL_LANGUAGE (x) = lang_c;
3804 }
3805
8926095f
MS
3806 if (TREE_CODE (x) == FUNCTION_DECL && ! DECL_FUNCTION_MEMBER_P (x))
3807 {
7bdbfa05 3808 t = push_overloaded_decl (x, PUSH_LOCAL);
8926095f
MS
3809 if (t != x || DECL_LANGUAGE (x) == lang_c)
3810 return t;
f181d4ae
MM
3811 if (!namespace_bindings_p ())
3812 /* We do not need to create a binding for this name;
3813 push_overloaded_decl will have already done so if
3814 necessary. */
3815 need_new_binding = 0;
8926095f 3816 }
6eb3bb27 3817 else if (DECL_FUNCTION_TEMPLATE_P (x) && DECL_NAMESPACE_SCOPE_P (x))
7bdbfa05 3818 return push_overloaded_decl (x, PUSH_GLOBAL);
8d08fdba 3819
a1774733
BK
3820 /* If declaring a type as a typedef, copy the type (unless we're
3821 at line 0), and install this TYPE_DECL as the new type's typedef
3822 name. See the extensive comment in ../c-decl.c (pushdecl). */
8d08fdba
MS
3823 if (TREE_CODE (x) == TYPE_DECL)
3824 {
3825 tree type = TREE_TYPE (x);
a1774733
BK
3826 if (DECL_SOURCE_LINE (x) == 0)
3827 {
3828 if (TYPE_NAME (type) == 0)
3829 TYPE_NAME (type) = x;
3830 }
1c80fb65
MM
3831 else if (type != error_mark_node && TYPE_NAME (type) != x
3832 /* We don't want to copy the type when all we're
3833 doing is making a TYPE_DECL for the purposes of
3834 inlining. */
3835 && (!TYPE_NAME (type)
3836 || TYPE_NAME (type) != DECL_ABSTRACT_ORIGIN (x)))
a1774733 3837 {
ae0a6181
MM
3838 push_obstacks (TYPE_OBSTACK (type), TYPE_OBSTACK (type));
3839
a1774733
BK
3840 DECL_ORIGINAL_TYPE (x) = type;
3841 type = build_type_copy (type);
3842 TYPE_STUB_DECL (type) = TYPE_STUB_DECL (DECL_ORIGINAL_TYPE (x));
3843 TYPE_NAME (type) = x;
3844 TREE_TYPE (x) = type;
ae0a6181
MM
3845
3846 pop_obstacks ();
a1774733 3847 }
8d08fdba 3848
8d08fdba
MS
3849 if (type != error_mark_node
3850 && TYPE_NAME (type)
3851 && TYPE_IDENTIFIER (type))
f181d4ae
MM
3852 set_identifier_type_value_with_scope (DECL_NAME (x), type,
3853 current_binding_level);
3854
8d08fdba
MS
3855 }
3856
3857 /* Multiple external decls of the same identifier ought to match.
3858
3859 We get warnings about inline functions where they are defined.
39211cd5
MS
3860 We get warnings about other functions from push_overloaded_decl.
3861
8d08fdba 3862 Avoid duplicate warnings where they are used. */
39211cd5 3863 if (TREE_PUBLIC (x) && TREE_CODE (x) != FUNCTION_DECL)
8d08fdba
MS
3864 {
3865 tree decl;
3866
31928556
JM
3867 if (IDENTIFIER_NAMESPACE_VALUE (name) != NULL_TREE
3868 && (DECL_EXTERNAL (IDENTIFIER_NAMESPACE_VALUE (name))
3869 || TREE_PUBLIC (IDENTIFIER_NAMESPACE_VALUE (name))))
3870 decl = IDENTIFIER_NAMESPACE_VALUE (name);
8d08fdba
MS
3871 else
3872 decl = NULL_TREE;
3873
39211cd5 3874 if (decl
8d08fdba
MS
3875 /* If different sort of thing, we already gave an error. */
3876 && TREE_CODE (decl) == TREE_CODE (x)
3bfdc719 3877 && !same_type_p (TREE_TYPE (x), TREE_TYPE (decl)))
8d08fdba 3878 {
8251199e
JM
3879 cp_pedwarn ("type mismatch with previous external decl", x);
3880 cp_pedwarn_at ("previous external decl of `%#D'", decl);
8d08fdba
MS
3881 }
3882 }
3883
8d08fdba
MS
3884 /* This name is new in its binding level.
3885 Install the new declaration and return it. */
2c73f9f5 3886 if (namespace_bindings_p ())
8d08fdba
MS
3887 {
3888 /* Install a global value. */
3889
8d08fdba
MS
3890 /* If the first global decl has external linkage,
3891 warn if we later see static one. */
31928556 3892 if (IDENTIFIER_GLOBAL_VALUE (name) == NULL_TREE && TREE_PUBLIC (x))
8d08fdba
MS
3893 TREE_PUBLIC (name) = 1;
3894
d8f8dca1
MM
3895 if (!(TREE_CODE (x) == TYPE_DECL && DECL_ARTIFICIAL (x)
3896 && t != NULL_TREE))
30394414
JM
3897 {
3898 if (TREE_CODE (x) == FUNCTION_DECL)
31928556
JM
3899 my_friendly_assert
3900 ((IDENTIFIER_GLOBAL_VALUE (name) == NULL_TREE)
3901 || (IDENTIFIER_GLOBAL_VALUE (name) == x), 378);
3902 SET_IDENTIFIER_NAMESPACE_VALUE (name, x);
30394414 3903 }
8d08fdba
MS
3904
3905 /* Don't forget if the function was used via an implicit decl. */
3906 if (IDENTIFIER_IMPLICIT_DECL (name)
3907 && TREE_USED (IDENTIFIER_IMPLICIT_DECL (name)))
3908 TREE_USED (x) = 1;
3909
3910 /* Don't forget if its address was taken in that way. */
3911 if (IDENTIFIER_IMPLICIT_DECL (name)
3912 && TREE_ADDRESSABLE (IDENTIFIER_IMPLICIT_DECL (name)))
3913 TREE_ADDRESSABLE (x) = 1;
3914
3915 /* Warn about mismatches against previous implicit decl. */
3916 if (IDENTIFIER_IMPLICIT_DECL (name) != NULL_TREE
3917 /* If this real decl matches the implicit, don't complain. */
3918 && ! (TREE_CODE (x) == FUNCTION_DECL
3919 && TREE_TYPE (TREE_TYPE (x)) == integer_type_node))
8251199e
JM
3920 cp_warning
3921 ("`%D' was previously implicitly declared to return `int'", x);
8d08fdba
MS
3922
3923 /* If new decl is `static' and an `extern' was seen previously,
3924 warn about it. */
a0a33927
MS
3925 if (x != NULL_TREE && t != NULL_TREE && decls_match (x, t))
3926 warn_extern_redeclared_static (x, t);
8d08fdba
MS
3927 }
3928 else
3929 {
3930 /* Here to install a non-global value. */
f181d4ae 3931 tree oldlocal = IDENTIFIER_VALUE (name);
31928556 3932 tree oldglobal = IDENTIFIER_NAMESPACE_VALUE (name);
8d08fdba 3933
f181d4ae
MM
3934 if (need_new_binding)
3935 {
0034cf72 3936 push_local_binding (name, x, 0);
f181d4ae
MM
3937 /* Because push_local_binding will hook X on to the
3938 current_binding_level's name list, we don't want to
3939 do that again below. */
3940 need_new_binding = 0;
3941 }
8d08fdba
MS
3942
3943 /* If this is a TYPE_DECL, push it into the type value slot. */
3944 if (TREE_CODE (x) == TYPE_DECL)
f181d4ae
MM
3945 set_identifier_type_value_with_scope (name, TREE_TYPE (x),
3946 current_binding_level);
8d08fdba 3947
a9aedbc2
MS
3948 /* Clear out any TYPE_DECL shadowed by a namespace so that
3949 we won't think this is a type. The C struct hack doesn't
3950 go through namespaces. */
3951 if (TREE_CODE (x) == NAMESPACE_DECL)
f181d4ae
MM
3952 set_identifier_type_value_with_scope (name, NULL_TREE,
3953 current_binding_level);
a9aedbc2 3954
8d08fdba
MS
3955 /* If this is an extern function declaration, see if we
3956 have a global definition or declaration for the function. */
3957 if (oldlocal == NULL_TREE
faae18ab 3958 && DECL_EXTERNAL (x)
31928556 3959 && oldglobal != NULL_TREE
8d08fdba 3960 && TREE_CODE (x) == FUNCTION_DECL
31928556 3961 && TREE_CODE (oldglobal) == FUNCTION_DECL)
8d08fdba
MS
3962 {
3963 /* We have one. Their types must agree. */
31928556 3964 if (decls_match (x, oldglobal))
6060a796
MS
3965 /* OK */;
3966 else
8d08fdba 3967 {
8251199e
JM
3968 cp_warning ("extern declaration of `%#D' doesn't match", x);
3969 cp_warning_at ("global declaration `%#D'", oldglobal);
8d08fdba 3970 }
8d08fdba
MS
3971 }
3972 /* If we have a local external declaration,
3973 and no file-scope declaration has yet been seen,
3974 then if we later have a file-scope decl it must not be static. */
3975 if (oldlocal == NULL_TREE
31928556 3976 && oldglobal == NULL_TREE
8d08fdba
MS
3977 && DECL_EXTERNAL (x)
3978 && TREE_PUBLIC (x))
f181d4ae 3979 TREE_PUBLIC (name) = 1;
8d08fdba
MS
3980
3981 if (DECL_FROM_INLINE (x))
3982 /* Inline decls shadow nothing. */;
3983
3984 /* Warn if shadowing an argument at the top level of the body. */
3985 else if (oldlocal != NULL_TREE && !DECL_EXTERNAL (x)
3986 && TREE_CODE (oldlocal) == PARM_DECL
3987 && TREE_CODE (x) != PARM_DECL)
3988 {
3989 /* Go to where the parms should be and see if we
3990 find them there. */
3991 struct binding_level *b = current_binding_level->level_chain;
3992
3993 if (cleanup_label)
3994 b = b->level_chain;
3995
3996 /* ARM $8.3 */
3997 if (b->parm_flag == 1)
8251199e 3998 cp_error ("declaration of `%#D' shadows a parameter", name);
8d08fdba 3999 }
f181d4ae
MM
4000 else if (warn_shadow && oldlocal != NULL_TREE
4001 && current_binding_level->is_for_scope
8d6e462b
PB
4002 && !DECL_DEAD_FOR_LOCAL (oldlocal))
4003 {
8251199e 4004 warning ("variable `%s' shadows local",
8d6e462b 4005 IDENTIFIER_POINTER (name));
8251199e 4006 cp_warning_at (" this is the shadowed declaration", oldlocal);
8d6e462b 4007 }
8d08fdba
MS
4008 /* Maybe warn if shadowing something else. */
4009 else if (warn_shadow && !DECL_EXTERNAL (x)
4010 /* No shadow warnings for internally generated vars. */
700f8a87 4011 && ! DECL_ARTIFICIAL (x)
8d08fdba
MS
4012 /* No shadow warnings for vars made for inlining. */
4013 && ! DECL_FROM_INLINE (x))
4014 {
d8e178a0 4015 const char *warnstring = NULL;
8d08fdba
MS
4016
4017 if (oldlocal != NULL_TREE && TREE_CODE (oldlocal) == PARM_DECL)
4018 warnstring = "declaration of `%s' shadows a parameter";
4019 else if (IDENTIFIER_CLASS_VALUE (name) != NULL_TREE
4ac14744 4020 && current_class_ptr
8d08fdba
MS
4021 && !TREE_STATIC (name))
4022 warnstring = "declaration of `%s' shadows a member of `this'";
4023 else if (oldlocal != NULL_TREE)
4024 warnstring = "declaration of `%s' shadows previous local";
31928556 4025 else if (oldglobal != NULL_TREE)
30394414 4026 /* XXX shadow warnings in outer-more namespaces */
8d08fdba
MS
4027 warnstring = "declaration of `%s' shadows global declaration";
4028
4029 if (warnstring)
4030 warning (warnstring, IDENTIFIER_POINTER (name));
4031 }
e1cd6e56 4032 }
8d08fdba 4033
e1cd6e56 4034 if (TREE_CODE (x) == FUNCTION_DECL)
f30432d7 4035 check_default_args (x);
8145f082
MS
4036
4037 /* Keep count of variables in this level with incomplete type. */
8145f082 4038 if (TREE_CODE (x) == VAR_DECL
28cbf42c 4039 && TREE_TYPE (x) != error_mark_node
f30432d7
MS
4040 && ((TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
4041 && PROMOTES_TO_AGGR_TYPE (TREE_TYPE (x), ARRAY_TYPE))
4042 /* RTTI TD entries are created while defining the type_info. */
4043 || (TYPE_LANG_SPECIFIC (TREE_TYPE (x))
4044 && TYPE_BEING_DEFINED (TREE_TYPE (x)))))
f181d4ae
MM
4045 current_binding_level->incomplete
4046 = tree_cons (NULL_TREE, x, current_binding_level->incomplete);
8d08fdba
MS
4047 }
4048
f181d4ae
MM
4049 if (need_new_binding)
4050 {
4051 /* Put decls on list in reverse order.
4052 We will reverse them later if necessary. */
4053 TREE_CHAIN (x) = current_binding_level->names;
4054 current_binding_level->names = x;
8f032717
MM
4055 if (current_binding_level == global_binding_level
4056 && !TREE_PERMANENT (x))
f181d4ae
MM
4057 my_friendly_abort (124);
4058 }
8d08fdba
MS
4059
4060 return x;
4061}
4062
5566b478
MS
4063/* Same as pushdecl, but define X in binding-level LEVEL. We rely on the
4064 caller to set DECL_CONTEXT properly. */
8d08fdba
MS
4065
4066static tree
4067pushdecl_with_scope (x, level)
4068 tree x;
4069 struct binding_level *level;
4070{
8d019cef 4071 register struct binding_level *b;
5566b478 4072 tree function_decl = current_function_decl;
8d08fdba 4073
5566b478 4074 current_function_decl = NULL_TREE;
8d019cef
JM
4075 if (level->parm_flag == 2)
4076 {
4077 b = class_binding_level;
4078 class_binding_level = level;
4079 pushdecl_class_level (x);
4080 class_binding_level = b;
4081 }
4082 else
4083 {
4084 b = current_binding_level;
4085 current_binding_level = level;
4086 x = pushdecl (x);
4087 current_binding_level = b;
4088 }
5566b478 4089 current_function_decl = function_decl;
8d08fdba
MS
4090 return x;
4091}
4092
2c73f9f5 4093/* Like pushdecl, only it places X in the current namespace,
8d08fdba 4094 if appropriate. */
e92cc029 4095
8d08fdba 4096tree
2c73f9f5 4097pushdecl_namespace_level (x)
8d08fdba
MS
4098 tree x;
4099{
8f032717 4100 register struct binding_level *b = current_binding_level;
2c73f9f5
ML
4101 register tree t;
4102
4103 t = pushdecl_with_scope (x, NAMESPACE_LEVEL (current_namespace));
8d08fdba
MS
4104
4105 /* Now, the type_shadowed stack may screw us. Munge it so it does
4106 what we want. */
4107 if (TREE_CODE (x) == TYPE_DECL)
4108 {
4109 tree name = DECL_NAME (x);
4110 tree newval;
4111 tree *ptr = (tree *)0;
4112 for (; b != global_binding_level; b = b->level_chain)
4113 {
4114 tree shadowed = b->type_shadowed;
4115 for (; shadowed; shadowed = TREE_CHAIN (shadowed))
4116 if (TREE_PURPOSE (shadowed) == name)
4117 {
4118 ptr = &TREE_VALUE (shadowed);
4119 /* Can't break out of the loop here because sometimes
4120 a binding level will have duplicate bindings for
4121 PT names. It's gross, but I haven't time to fix it. */
4122 }
4123 }
4124 newval = TREE_TYPE (x);
4125 if (ptr == (tree *)0)
4126 {
4127 /* @@ This shouldn't be needed. My test case "zstring.cc" trips
4128 up here if this is changed to an assertion. --KR */
4129 SET_IDENTIFIER_TYPE_VALUE (name, newval);
4130 }
4131 else
4132 {
8d08fdba
MS
4133 *ptr = newval;
4134 }
4135 }
4136 return t;
4137}
4138
2c73f9f5
ML
4139/* Like pushdecl, only it places X in GLOBAL_BINDING_LEVEL,
4140 if appropriate. */
4141
4142tree
4143pushdecl_top_level (x)
4144 tree x;
4145{
4146 tree cur_namespace = current_namespace;
4147 current_namespace = global_namespace;
4148 x = pushdecl_namespace_level (x);
4149 current_namespace = cur_namespace;
4150 return x;
4151}
4152
8d08fdba 4153/* Make the declaration of X appear in CLASS scope. */
e92cc029 4154
61a127b3 4155void
8d08fdba
MS
4156pushdecl_class_level (x)
4157 tree x;
4158{
4159 /* Don't use DECL_ASSEMBLER_NAME here! Everything that looks in class
4160 scope looks for the pre-mangled name. */
8f032717
MM
4161 register tree name;
4162
4163 if (TREE_CODE (x) == OVERLOAD)
4164 x = OVL_CURRENT (x);
4165 name = DECL_NAME (x);
8d08fdba
MS
4166
4167 if (name)
4168 {
4169 push_class_level_binding (name, x);
4170 if (TREE_CODE (x) == TYPE_DECL)
f181d4ae 4171 set_identifier_type_value (name, TREE_TYPE (x));
8d08fdba 4172 }
8f032717
MM
4173 else if (ANON_UNION_TYPE_P (TREE_TYPE (x)))
4174 {
4175 tree f;
4176
4177 for (f = TYPE_FIELDS (TREE_TYPE (x));
4178 f;
4179 f = TREE_CHAIN (f))
4180 pushdecl_class_level (f);
4181 }
8d08fdba
MS
4182}
4183
bd6dd845 4184#if 0
7177d104
MS
4185/* This function is used to push the mangled decls for nested types into
4186 the appropriate scope. Previously pushdecl_top_level was used, but that
4187 is incorrect for members of local classes. */
e92cc029 4188
5566b478 4189void
7177d104
MS
4190pushdecl_nonclass_level (x)
4191 tree x;
4192{
4193 struct binding_level *b = current_binding_level;
4194
7177d104 4195 my_friendly_assert (b->parm_flag != 2, 180);
7177d104 4196
5566b478 4197#if 0
7177d104
MS
4198 /* Get out of template binding levels */
4199 while (b->pseudo_global)
4200 b = b->level_chain;
5566b478 4201#endif
7177d104
MS
4202
4203 pushdecl_with_scope (x, b);
4204}
bd6dd845 4205#endif
7177d104 4206
8d08fdba
MS
4207/* Make the declaration(s) of X appear in CLASS scope
4208 under the name NAME. */
e92cc029 4209
8d08fdba
MS
4210void
4211push_class_level_binding (name, x)
4212 tree name;
4213 tree x;
4214{
8f032717 4215 tree binding;
98c1c668
JM
4216 /* The class_binding_level will be NULL if x is a template
4217 parameter name in a member template. */
4218 if (!class_binding_level)
4219 return;
4220
908c4e83
MM
4221 /* Make sure that this new member does not have the same name
4222 as a template parameter. */
4223 if (TYPE_BEING_DEFINED (current_class_type))
4224 check_template_shadow (x);
4225
f181d4ae
MM
4226 /* If this declaration shadows a declaration from an enclosing
4227 class, then we will need to restore IDENTIFIER_CLASS_VALUE when
4228 we leave this class. Record the shadowed declaration here. */
8f032717
MM
4229 binding = IDENTIFIER_BINDING (name);
4230 if (binding
4231 && ((TREE_CODE (x) == OVERLOAD
4232 && BINDING_VALUE (binding)
4233 && is_overloaded_fn (BINDING_VALUE (binding)))
4234 || INHERITED_VALUE_BINDING_P (binding)))
4235 {
4236 tree shadow;
4237 tree old_decl;
4238
4239 /* If the old binding was from a base class, and was for a tag
4240 name, slide it over to make room for the new binding. The
4241 old binding is still visible if explicitly qualified with a
4242 class-key. */
4243 if (INHERITED_VALUE_BINDING_P (binding)
4244 && BINDING_VALUE (binding)
4245 && TREE_CODE (BINDING_VALUE (binding)) == TYPE_DECL
4246 && DECL_ARTIFICIAL (BINDING_VALUE (binding))
4247 && !(TREE_CODE (x) == TYPE_DECL && DECL_ARTIFICIAL (x)))
4248 {
4249 old_decl = BINDING_TYPE (binding);
4250 BINDING_TYPE (binding) = BINDING_VALUE (binding);
4251 BINDING_VALUE (binding) = NULL_TREE;
4252 INHERITED_VALUE_BINDING_P (binding) = 0;
4253 }
4254 else
4255 old_decl = BINDING_VALUE (binding);
4256
4257 /* There was already a binding for X containing fewer
4258 functions than are named in X. Find the previous
4259 declaration of X on the class-shadowed list, and update it. */
4260 for (shadow = class_binding_level->class_shadowed;
4261 shadow;
4262 shadow = TREE_CHAIN (shadow))
4263 if (TREE_PURPOSE (shadow) == name
4264 && TREE_TYPE (shadow) == old_decl)
4265 {
4266 BINDING_VALUE (binding) = x;
4267 INHERITED_VALUE_BINDING_P (binding) = 0;
4268 TREE_TYPE (shadow) = x;
4269 return;
4270 }
4271 }
f181d4ae 4272
8f032717
MM
4273 /* If we didn't replace an existing binding, put the binding on the
4274 stack of bindings for the identifier, and update
4275 IDENTIFIER_CLASS_VALUE. */
4276 if (push_class_binding (name, x))
4277 {
4278 maybe_push_cache_obstack ();
4279 class_binding_level->class_shadowed
4280 = tree_cons (name, IDENTIFIER_CLASS_VALUE (name),
4281 class_binding_level->class_shadowed);
4282 pop_obstacks ();
4283 /* Record the value we are binding NAME to so that we can know
4284 what to pop later. */
4285 TREE_TYPE (class_binding_level->class_shadowed) = x;
4286 }
8d08fdba
MS
4287}
4288
2c73f9f5
ML
4289/* Insert another USING_DECL into the current binding level,
4290 returning this declaration. If this is a redeclaration,
4291 do nothing and return NULL_TREE. */
e92cc029 4292
2c73f9f5
ML
4293tree
4294push_using_decl (scope, name)
4295 tree scope;
4296 tree name;
8d08fdba 4297{
2c73f9f5
ML
4298 tree decl;
4299
2c73f9f5
ML
4300 my_friendly_assert (TREE_CODE (scope) == NAMESPACE_DECL, 383);
4301 my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, 384);
4302 for (decl = current_binding_level->usings; decl; decl = TREE_CHAIN (decl))
4303 if (DECL_INITIAL (decl) == scope && DECL_NAME (decl) == name)
4304 break;
4305 if (decl)
4306 return NULL_TREE;
4307 decl = build_lang_decl (USING_DECL, name, void_type_node);
4308 DECL_INITIAL (decl) = scope;
4309 TREE_CHAIN (decl) = current_binding_level->usings;
4310 current_binding_level->usings = decl;
4311 return decl;
8d08fdba
MS
4312}
4313
ea9635c7
ML
4314/* Add namespace to using_directives. Return NULL_TREE if nothing was
4315 changed (i.e. there was already a directive), or the fresh
4316 TREE_LIST otherwise. */
4317
4318tree
9ed182dc 4319push_using_directive (used)
ea9635c7 4320 tree used;
ea9635c7
ML
4321{
4322 tree ud = current_binding_level->using_directives;
9ed182dc 4323 tree iter, ancestor;
ea9635c7
ML
4324
4325 /* Check if we already have this. */
4326 if (purpose_member (used, ud) != NULL_TREE)
4327 return NULL_TREE;
00dc6358
JM
4328
4329 /* Recursively add all namespaces used. */
4330 for (iter = DECL_NAMESPACE_USING (used); iter; iter = TREE_CHAIN (iter))
9ed182dc 4331 push_using_directive (TREE_PURPOSE (iter));
00dc6358 4332
9ed182dc 4333 ancestor = namespace_ancestor (current_decl_namespace (), used);
00dc6358 4334 ud = current_binding_level->using_directives;
ea9635c7
ML
4335 ud = perm_tree_cons (used, ancestor, ud);
4336 current_binding_level->using_directives = ud;
4337 return ud;
4338}
4339
f181d4ae
MM
4340/* DECL is a FUNCTION_DECL for a non-member function, which may have
4341 other definitions already in place. We get around this by making
4342 the value of the identifier point to a list of all the things that
4343 want to be referenced by that name. It is then up to the users of
4344 that name to decide what to do with that list.
8d08fdba
MS
4345
4346 DECL may also be a TEMPLATE_DECL, with a FUNCTION_DECL in its DECL_RESULT
4347 slot. It is dealt with the same way.
4348
7bdbfa05
MM
4349 FLAGS is a bitwise-or of the following values:
4350 PUSH_LOCAL: Bind DECL in the current scope, rather than at
4351 namespace scope.
4352 PUSH_USING: DECL is being pushed as the result of a using
4353 declaration.
4354
8d08fdba
MS
4355 The value returned may be a previous declaration if we guessed wrong
4356 about what language DECL should belong to (C or C++). Otherwise,
4357 it's always DECL (and never something that's not a _DECL). */
e92cc029 4358
7bdbfa05
MM
4359tree
4360push_overloaded_decl (decl, flags)
8d08fdba 4361 tree decl;
7bdbfa05 4362 int flags;
8d08fdba 4363{
f181d4ae 4364 tree name = DECL_NAME (decl);
700f8a87 4365 tree old;
f181d4ae 4366 tree new_binding;
7bdbfa05 4367 int doing_global = (namespace_bindings_p () || !(flags & PUSH_LOCAL));
700f8a87
MS
4368
4369 if (doing_global)
4370 {
f181d4ae 4371 old = namespace_binding (name, DECL_CONTEXT (decl));
700f8a87 4372 if (old && TREE_CODE (old) == FUNCTION_DECL
a4443a08 4373 && DECL_ARTIFICIAL (old)
700f8a87
MS
4374 && (DECL_BUILT_IN (old) || DECL_BUILT_IN_NONANSI (old)))
4375 {
a4443a08
MS
4376 if (duplicate_decls (decl, old))
4377 return old;
700f8a87
MS
4378 old = NULL_TREE;
4379 }
4380 }
4381 else
f181d4ae 4382 old = lookup_name_current_level (name);
8d08fdba 4383
700f8a87 4384 if (old)
8d08fdba 4385 {
e1cd6e56 4386 if (TREE_CODE (old) == TYPE_DECL && DECL_ARTIFICIAL (old))
8926095f 4387 {
700f8a87 4388 tree t = TREE_TYPE (old);
cdf5b885
MS
4389 if (IS_AGGR_TYPE (t) && warn_shadow
4390 && (! DECL_IN_SYSTEM_HEADER (decl)
4391 || ! DECL_IN_SYSTEM_HEADER (old)))
8251199e 4392 cp_warning ("`%#D' hides constructor for `%#T'", decl, t);
5b605f68 4393 old = NULL_TREE;
8926095f 4394 }
700f8a87 4395 else if (is_overloaded_fn (old))
8d08fdba 4396 {
8d08fdba
MS
4397 tree tmp;
4398
2c73f9f5 4399 for (tmp = old; tmp; tmp = OVL_NEXT (tmp))
7bdbfa05
MM
4400 {
4401 tree fn = OVL_CURRENT (tmp);
4402
4403 if (TREE_CODE (tmp) == OVERLOAD && OVL_USED (tmp)
4404 && !(flags & PUSH_USING)
4405 && compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
4406 TYPE_ARG_TYPES (TREE_TYPE (decl))))
4407 cp_error ("`%#D' conflicts with previous using declaration `%#D'",
4408 decl, fn);
4409
4410 if (duplicate_decls (decl, fn))
4411 return fn;
4412 }
8d08fdba 4413 }
e1cd6e56
MS
4414 else
4415 {
8251199e
JM
4416 cp_error_at ("previous non-function declaration `%#D'", old);
4417 cp_error ("conflicts with function declaration `%#D'", decl);
a9aedbc2 4418 return decl;
e1cd6e56 4419 }
8d08fdba 4420 }
7177d104 4421
700f8a87 4422 if (old || TREE_CODE (decl) == TEMPLATE_DECL)
8d08fdba 4423 {
2c73f9f5 4424 if (old && TREE_CODE (old) != OVERLOAD)
f181d4ae
MM
4425 new_binding = ovl_cons (decl, ovl_cons (old, NULL_TREE));
4426 else
4427 new_binding = ovl_cons (decl, old);
347d73d7
ML
4428 if (flags & PUSH_USING)
4429 OVL_USED (new_binding) = 1;
8d08fdba
MS
4430 }
4431 else
f181d4ae
MM
4432 /* NAME is not ambiguous. */
4433 new_binding = decl;
700f8a87
MS
4434
4435 if (doing_global)
f181d4ae 4436 set_namespace_binding (name, current_namespace, new_binding);
700f8a87 4437 else
f181d4ae
MM
4438 {
4439 /* We only create an OVERLOAD if there was a previous binding at
0580c9aa
ML
4440 this level, or if decl is a template. In the former case, we
4441 need to remove the old binding and replace it with the new
4442 binding. We must also run through the NAMES on the binding
4443 level where the name was bound to update the chain. */
4444
4445 if (TREE_CODE (new_binding) == OVERLOAD && old)
f181d4ae
MM
4446 {
4447 tree *d;
4448
4449 for (d = &BINDING_LEVEL (IDENTIFIER_BINDING (name))->names;
4450 *d;
4451 d = &TREE_CHAIN (*d))
4452 if (*d == old
4453 || (TREE_CODE (*d) == TREE_LIST
4454 && TREE_VALUE (*d) == old))
4455 {
d8f8dca1
MM
4456 if (TREE_CODE (*d) == TREE_LIST)
4457 /* Just replace the old binding with the new. */
4458 TREE_VALUE (*d) = new_binding;
4459 else
4460 /* Build a TREE_LIST to wrap the OVERLOAD. */
4461 *d = build_tree_list (NULL_TREE, new_binding);
4462
4463 /* And update the CPLUS_BINDING node. */
4464 BINDING_VALUE (IDENTIFIER_BINDING (name))
4465 = new_binding;
4466 return decl;
f181d4ae
MM
4467 }
4468
d8f8dca1
MM
4469 /* We should always find a previous binding in this case. */
4470 my_friendly_abort (0);
f181d4ae
MM
4471 }
4472
4473 /* Install the new binding. */
0034cf72 4474 push_local_binding (name, new_binding, flags);
f181d4ae 4475 }
700f8a87 4476
8d08fdba
MS
4477 return decl;
4478}
4479\f
4480/* Generate an implicit declaration for identifier FUNCTIONID
4481 as a function of type int (). Print a warning if appropriate. */
4482
4483tree
4484implicitly_declare (functionid)
4485 tree functionid;
4486{
4487 register tree decl;
4488 int temp = allocation_temporary_p ();
4489
4490 push_obstacks_nochange ();
4491
4492 /* Save the decl permanently so we can warn if definition follows.
4493 In ANSI C, warn_implicit is usually false, so the saves little space.
4494 But in C++, it's usually true, hence the extra code. */
d22c8596 4495 if (temp && (! warn_implicit || toplevel_bindings_p ()))
8d08fdba
MS
4496 end_temporary_allocation ();
4497
4498 /* We used to reuse an old implicit decl here,
4499 but this loses with inline functions because it can clobber
4500 the saved decl chains. */
4501 decl = build_lang_decl (FUNCTION_DECL, functionid, default_function_type);
4502
4503 DECL_EXTERNAL (decl) = 1;
4504 TREE_PUBLIC (decl) = 1;
4505
4506 /* ANSI standard says implicit declarations are in the innermost block.
d22c8596 4507 So we record the decl in the standard fashion. */
8d08fdba
MS
4508 pushdecl (decl);
4509 rest_of_decl_compilation (decl, NULL_PTR, 0, 0);
4510
4511 if (warn_implicit
4512 /* Only one warning per identifier. */
4513 && IDENTIFIER_IMPLICIT_DECL (functionid) == NULL_TREE)
4514 {
8251199e 4515 cp_pedwarn ("implicit declaration of function `%#D'", decl);
8d08fdba
MS
4516 }
4517
4518 SET_IDENTIFIER_IMPLICIT_DECL (functionid, decl);
4519
4520 pop_obstacks ();
4521
4522 return decl;
4523}
4524
8251199e 4525/* Return zero if the declaration NEWDECL is valid
8d08fdba
MS
4526 when the declaration OLDDECL (assumed to be for the same name)
4527 has already been seen.
4528 Otherwise return an error message format string with a %s
4529 where the identifier should go. */
4530
d8e178a0 4531static const char *
8d08fdba
MS
4532redeclaration_error_message (newdecl, olddecl)
4533 tree newdecl, olddecl;
4534{
4535 if (TREE_CODE (newdecl) == TYPE_DECL)
4536 {
4537 /* Because C++ can put things into name space for free,
4538 constructs like "typedef struct foo { ... } foo"
4539 would look like an erroneous redeclaration. */
3bfdc719 4540 if (same_type_p (TREE_TYPE (newdecl), TREE_TYPE (olddecl)))
8251199e 4541 return 0;
8d08fdba 4542 else
8251199e 4543 return "redefinition of `%#D'";
8d08fdba
MS
4544 }
4545 else if (TREE_CODE (newdecl) == FUNCTION_DECL)
4546 {
4547 /* If this is a pure function, its olddecl will actually be
4548 the original initialization to `0' (which we force to call
4549 abort()). Don't complain about redefinition in this case. */
4550 if (DECL_LANG_SPECIFIC (olddecl) && DECL_ABSTRACT_VIRTUAL_P (olddecl))
8251199e 4551 return 0;
8d08fdba 4552
2c73f9f5
ML
4553 /* If both functions come from different namespaces, this is not
4554 a redeclaration - this is a conflict with a used function. */
6eb3bb27 4555 if (DECL_NAMESPACE_SCOPE_P (olddecl)
2c73f9f5 4556 && DECL_CONTEXT (olddecl) != DECL_CONTEXT (newdecl))
8251199e 4557 return "`%D' conflicts with used function";
2c73f9f5 4558
db5ae43f
MS
4559 /* We'll complain about linkage mismatches in
4560 warn_extern_redeclared_static. */
4561
2c73f9f5 4562 /* Defining the same name twice is no good. */
8d08fdba 4563 if (DECL_INITIAL (olddecl) != NULL_TREE
faae18ab 4564 && DECL_INITIAL (newdecl) != NULL_TREE)
8d08fdba
MS
4565 {
4566 if (DECL_NAME (olddecl) == NULL_TREE)
8251199e 4567 return "`%#D' not declared in class";
8d08fdba 4568 else
8251199e 4569 return "redefinition of `%#D'";
8d08fdba 4570 }
8251199e 4571 return 0;
8d08fdba 4572 }
51c184be
MS
4573 else if (TREE_CODE (newdecl) == TEMPLATE_DECL)
4574 {
ec255269
MS
4575 if ((TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == FUNCTION_DECL
4576 && DECL_INITIAL (DECL_TEMPLATE_RESULT (newdecl))
4577 && DECL_INITIAL (DECL_TEMPLATE_RESULT (olddecl)))
4578 || (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL
4579 && TYPE_SIZE (TREE_TYPE (newdecl))
4580 && TYPE_SIZE (TREE_TYPE (olddecl))))
8251199e
JM
4581 return "redefinition of `%#D'";
4582 return 0;
51c184be 4583 }
5566b478 4584 else if (toplevel_bindings_p ())
8d08fdba
MS
4585 {
4586 /* Objects declared at top level: */
4587 /* If at least one is a reference, it's ok. */
4588 if (DECL_EXTERNAL (newdecl) || DECL_EXTERNAL (olddecl))
8251199e 4589 return 0;
8926095f 4590 /* Reject two definitions. */
8251199e 4591 return "redefinition of `%#D'";
8d08fdba
MS
4592 }
4593 else
4594 {
4595 /* Objects declared with block scope: */
4596 /* Reject two definitions, and reject a definition
4597 together with an external reference. */
4598 if (!(DECL_EXTERNAL (newdecl) && DECL_EXTERNAL (olddecl)))
8251199e
JM
4599 return "redeclaration of `%#D'";
4600 return 0;
8d08fdba
MS
4601 }
4602}
4603\f
4604/* Get the LABEL_DECL corresponding to identifier ID as a label.
4605 Create one if none exists so far for the current function.
4606 This function is called for both label definitions and label references. */
4607
4608tree
4609lookup_label (id)
4610 tree id;
4611{
4612 register tree decl = IDENTIFIER_LABEL_VALUE (id);
4613
4614 if (current_function_decl == NULL_TREE)
4615 {
8251199e 4616 error ("label `%s' referenced outside of any function",
8d08fdba
MS
4617 IDENTIFIER_POINTER (id));
4618 return NULL_TREE;
4619 }
4620
4621 if ((decl == NULL_TREE
4622 || DECL_SOURCE_LINE (decl) == 0)
e349ee73
MS
4623 && (named_label_uses == NULL
4624 || named_label_uses->names_in_scope != current_binding_level->names
4625 || named_label_uses->label_decl != decl))
4626 {
4627 struct named_label_list *new_ent;
4628 new_ent
4629 = (struct named_label_list*)oballoc (sizeof (struct named_label_list));
4630 new_ent->label_decl = decl;
4631 new_ent->names_in_scope = current_binding_level->names;
4632 new_ent->binding_level = current_binding_level;
4633 new_ent->lineno_o_goto = lineno;
4634 new_ent->filename_o_goto = input_filename;
4635 new_ent->next = named_label_uses;
4636 named_label_uses = new_ent;
8d08fdba
MS
4637 }
4638
4639 /* Use a label already defined or ref'd with this name. */
4640 if (decl != NULL_TREE)
4641 {
4642 /* But not if it is inherited and wasn't declared to be inheritable. */
4643 if (DECL_CONTEXT (decl) != current_function_decl
4644 && ! C_DECLARED_LABEL_FLAG (decl))
4645 return shadow_label (id);
4646 return decl;
4647 }
4648
4649 decl = build_decl (LABEL_DECL, id, void_type_node);
4650
cffa8729
MS
4651 /* Make sure every label has an rtx. */
4652 label_rtx (decl);
4653
8d08fdba
MS
4654 /* A label not explicitly declared must be local to where it's ref'd. */
4655 DECL_CONTEXT (decl) = current_function_decl;
4656
4657 DECL_MODE (decl) = VOIDmode;
4658
4659 /* Say where one reference is to the label,
4660 for the sake of the error if it is not defined. */
4661 DECL_SOURCE_LINE (decl) = lineno;
4662 DECL_SOURCE_FILE (decl) = input_filename;
4663
4664 SET_IDENTIFIER_LABEL_VALUE (id, decl);
4665
4666 named_labels = tree_cons (NULL_TREE, decl, named_labels);
e349ee73 4667 named_label_uses->label_decl = decl;
8d08fdba
MS
4668
4669 return decl;
4670}
4671
4672/* Make a label named NAME in the current function,
4673 shadowing silently any that may be inherited from containing functions
4674 or containing scopes.
4675
4676 Note that valid use, if the label being shadowed
4677 comes from another scope in the same function,
4678 requires calling declare_nonlocal_label right away. */
4679
4680tree
4681shadow_label (name)
4682 tree name;
4683{
4684 register tree decl = IDENTIFIER_LABEL_VALUE (name);
4685
4686 if (decl != NULL_TREE)
4687 {
4688 shadowed_labels = tree_cons (NULL_TREE, decl, shadowed_labels);
4689 SET_IDENTIFIER_LABEL_VALUE (name, NULL_TREE);
8d08fdba
MS
4690 }
4691
4692 return lookup_label (name);
4693}
4694
4695/* Define a label, specifying the location in the source file.
4696 Return the LABEL_DECL node for the label, if the definition is valid.
4697 Otherwise return 0. */
4698
4699tree
4700define_label (filename, line, name)
4701 char *filename;
4702 int line;
4703 tree name;
4704{
5566b478
MS
4705 tree decl;
4706
4707 if (minimal_parse_mode)
4708 {
4709 push_obstacks (&permanent_obstack, &permanent_obstack);
4710 decl = build_decl (LABEL_DECL, name, void_type_node);
4711 pop_obstacks ();
4712 DECL_SOURCE_LINE (decl) = line;
4713 DECL_SOURCE_FILE (decl) = filename;
4714 add_tree (decl);
4715 return decl;
4716 }
4717
4718 decl = lookup_label (name);
8d08fdba
MS
4719
4720 /* After labels, make any new cleanups go into their
4721 own new (temporary) binding contour. */
4722 current_binding_level->more_cleanups_ok = 0;
4723
4724 /* If label with this name is known from an outer context, shadow it. */
4725 if (decl != NULL_TREE && DECL_CONTEXT (decl) != current_function_decl)
4726 {
4727 shadowed_labels = tree_cons (NULL_TREE, decl, shadowed_labels);
4728 SET_IDENTIFIER_LABEL_VALUE (name, NULL_TREE);
4729 decl = lookup_label (name);
4730 }
4731
e1cd6e56 4732 if (name == get_identifier ("wchar_t"))
8251199e 4733 cp_pedwarn ("label named wchar_t");
e1cd6e56 4734
8d08fdba
MS
4735 if (DECL_INITIAL (decl) != NULL_TREE)
4736 {
8251199e 4737 cp_error ("duplicate label `%D'", decl);
8d08fdba
MS
4738 return 0;
4739 }
4740 else
4741 {
e349ee73 4742 struct named_label_list *uses, *prev;
28cbf42c 4743 int identified = 0;
8d08fdba
MS
4744
4745 /* Mark label as having been defined. */
4746 DECL_INITIAL (decl) = error_mark_node;
4747 /* Say where in the source. */
4748 DECL_SOURCE_FILE (decl) = filename;
4749 DECL_SOURCE_LINE (decl) = line;
4750
e349ee73
MS
4751 prev = NULL;
4752 uses = named_label_uses;
4753 while (uses != NULL)
4754 if (uses->label_decl == decl)
8d08fdba
MS
4755 {
4756 struct binding_level *b = current_binding_level;
4757 while (b)
4758 {
4759 tree new_decls = b->names;
e349ee73
MS
4760 tree old_decls = (b == uses->binding_level)
4761 ? uses->names_in_scope : NULL_TREE;
8d08fdba
MS
4762 while (new_decls != old_decls)
4763 {
4764 if (TREE_CODE (new_decls) == VAR_DECL
4765 /* Don't complain about crossing initialization
4766 of internal entities. They can't be accessed,
4767 and they should be cleaned up
4768 by the time we get to the label. */
700f8a87 4769 && ! DECL_ARTIFICIAL (new_decls)
5524676d
JM
4770 && !(DECL_INITIAL (new_decls) == NULL_TREE
4771 && pod_type_p (TREE_TYPE (new_decls))))
8d08fdba 4772 {
b607c87f
JM
4773 /* This is really only important if we're crossing
4774 an initialization. The POD stuff is just
4775 pedantry; why should it matter if the class
4776 contains a field of pointer to member type? */
4777 int problem = (DECL_INITIAL (new_decls)
4778 || (TYPE_NEEDS_CONSTRUCTING
4779 (TREE_TYPE (new_decls))));
4780
4781 if (! identified)
e349ee73 4782 {
b607c87f
JM
4783 if (problem)
4784 {
4785 cp_error ("jump to label `%D'", decl);
4786 error_with_file_and_line
4787 (uses->filename_o_goto,
4788 uses->lineno_o_goto, " from here");
4789 }
4790 else
4791 {
4792 cp_pedwarn ("jump to label `%D'", decl);
4793 pedwarn_with_file_and_line
4794 (uses->filename_o_goto,
4795 uses->lineno_o_goto, " from here");
4796 }
e349ee73
MS
4797 identified = 1;
4798 }
b607c87f
JM
4799
4800 if (problem)
5524676d
JM
4801 cp_error_at (" crosses initialization of `%#D'",
4802 new_decls);
4803 else
b607c87f 4804 cp_pedwarn_at (" enters scope of non-POD `%#D'",
5524676d 4805 new_decls);
8d08fdba
MS
4806 }
4807 new_decls = TREE_CHAIN (new_decls);
4808 }
e349ee73 4809 if (b == uses->binding_level)
8d08fdba
MS
4810 break;
4811 b = b->level_chain;
4812 }
4813
e349ee73
MS
4814 if (prev != NULL)
4815 prev->next = uses->next;
8d08fdba 4816 else
e349ee73
MS
4817 named_label_uses = uses->next;
4818
4819 uses = uses->next;
4820 }
4821 else
4822 {
4823 prev = uses;
4824 uses = uses->next;
8d08fdba
MS
4825 }
4826 current_function_return_value = NULL_TREE;
4827 return decl;
4828 }
4829}
4830
a5894242
MS
4831struct cp_switch
4832{
4833 struct binding_level *level;
4834 struct cp_switch *next;
4835};
4836
4837static struct cp_switch *switch_stack;
4838
4839void
4840push_switch ()
4841{
4842 struct cp_switch *p
4843 = (struct cp_switch *) oballoc (sizeof (struct cp_switch));
4844 p->level = current_binding_level;
4845 p->next = switch_stack;
4846 switch_stack = p;
4847}
4848
4849void
4850pop_switch ()
4851{
4852 switch_stack = switch_stack->next;
4853}
4854
8d08fdba
MS
4855/* Same, but for CASE labels. If DECL is NULL_TREE, it's the default. */
4856/* XXX Note decl is never actually used. (bpk) */
e92cc029 4857
8d08fdba 4858void
b370501f 4859define_case_label ()
8d08fdba
MS
4860{
4861 tree cleanup = last_cleanup_this_contour ();
a5894242
MS
4862 struct binding_level *b = current_binding_level;
4863 int identified = 0;
4864
8d08fdba
MS
4865 if (cleanup)
4866 {
4867 static int explained = 0;
8251199e
JM
4868 cp_warning_at ("destructor needed for `%#D'", TREE_PURPOSE (cleanup));
4869 warning ("where case label appears here");
8d08fdba
MS
4870 if (!explained)
4871 {
8251199e
JM
4872 warning ("(enclose actions of previous case statements requiring");
4873 warning ("destructors in their own binding contours.)");
8d08fdba
MS
4874 explained = 1;
4875 }
4876 }
4877
a5894242
MS
4878 for (; b && b != switch_stack->level; b = b->level_chain)
4879 {
4880 tree new_decls = b->names;
4881 for (; new_decls; new_decls = TREE_CHAIN (new_decls))
4882 {
4883 if (TREE_CODE (new_decls) == VAR_DECL
4884 /* Don't complain about crossing initialization
4885 of internal entities. They can't be accessed,
4886 and they should be cleaned up
4887 by the time we get to the label. */
4888 && ! DECL_ARTIFICIAL (new_decls)
4889 && ((DECL_INITIAL (new_decls) != NULL_TREE
4890 && DECL_INITIAL (new_decls) != error_mark_node)
4891 || TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (new_decls))))
4892 {
4893 if (! identified)
8251199e 4894 error ("jump to case label");
a5894242 4895 identified = 1;
8251199e 4896 cp_error_at (" crosses initialization of `%#D'",
a5894242
MS
4897 new_decls);
4898 }
4899 }
4900 }
4901
8d08fdba
MS
4902 /* After labels, make any new cleanups go into their
4903 own new (temporary) binding contour. */
4904
4905 current_binding_level->more_cleanups_ok = 0;
4906 current_function_return_value = NULL_TREE;
4907}
4908\f
4909/* Return the list of declarations of the current level.
4910 Note that this list is in reverse order unless/until
4911 you nreverse it; and when you do nreverse it, you must
4912 store the result back using `storedecls' or you will lose. */
4913
4914tree
4915getdecls ()
4916{
4917 return current_binding_level->names;
4918}
4919
4920/* Return the list of type-tags (for structs, etc) of the current level. */
4921
4922tree
4923gettags ()
4924{
4925 return current_binding_level->tags;
4926}
4927
4928/* Store the list of declarations of the current level.
4929 This is done for the parameter declarations of a function being defined,
4930 after they are modified in the light of any missing parameters. */
4931
4932static void
4933storedecls (decls)
4934 tree decls;
4935{
4936 current_binding_level->names = decls;
4937}
4938
4939/* Similarly, store the list of tags of the current level. */
4940
280f9385 4941void
8d08fdba
MS
4942storetags (tags)
4943 tree tags;
4944{
4945 current_binding_level->tags = tags;
4946}
4947\f
4948/* Given NAME, an IDENTIFIER_NODE,
4949 return the structure (or union or enum) definition for that name.
4950 Searches binding levels from BINDING_LEVEL up to the global level.
4951 If THISLEVEL_ONLY is nonzero, searches only the specified context
4952 (but skips any tag-transparent contexts to find one that is
4953 meaningful for tags).
4954 FORM says which kind of type the caller wants;
4955 it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
4956 If the wrong kind of type is found, and it's not a template, an error is
4957 reported. */
4958
4959static tree
4960lookup_tag (form, name, binding_level, thislevel_only)
4961 enum tree_code form;
8d08fdba 4962 tree name;
cffa8729 4963 struct binding_level *binding_level;
8d08fdba
MS
4964 int thislevel_only;
4965{
4966 register struct binding_level *level;
36a117a5
MM
4967 /* Non-zero if, we should look past a pseudo-global level, even if
4968 THISLEVEL_ONLY. */
4969 int allow_pseudo_global = 1;
8d08fdba
MS
4970
4971 for (level = binding_level; level; level = level->level_chain)
4972 {
4973 register tree tail;
4974 if (ANON_AGGRNAME_P (name))
4975 for (tail = level->tags; tail; tail = TREE_CHAIN (tail))
4976 {
4977 /* There's no need for error checking here, because
4978 anon names are unique throughout the compilation. */
4979 if (TYPE_IDENTIFIER (TREE_VALUE (tail)) == name)
4980 return TREE_VALUE (tail);
4981 }
2c73f9f5
ML
4982 else if (level->namespace_p)
4983 /* Do namespace lookup. */
6c011b01 4984 for (tail = current_namespace; 1; tail = CP_DECL_CONTEXT (tail))
2c73f9f5 4985 {
36a117a5
MM
4986 tree old = binding_for_name (name, tail);
4987
4988 /* If we just skipped past a pseudo global level, even
4989 though THISLEVEL_ONLY, and we find a template class
4990 declaration, then we use the _TYPE node for the
4991 template. See the example below. */
4992 if (thislevel_only && !allow_pseudo_global
4993 && old && BINDING_VALUE (old)
4994 && DECL_CLASS_TEMPLATE_P (BINDING_VALUE (old)))
4995 old = TREE_TYPE (BINDING_VALUE (old));
4996 else
4997 old = BINDING_TYPE (old);
4998
2c73f9f5
ML
4999 /* If it has an original type, it is a typedef, and we
5000 should not return it. */
5001 if (old && DECL_ORIGINAL_TYPE (TYPE_NAME (old)))
5002 old = NULL_TREE;
5003 if (old && TREE_CODE (old) != form
5004 && !(form != ENUMERAL_TYPE && TREE_CODE (old) == TEMPLATE_DECL))
5005 {
8251199e 5006 cp_error ("`%#D' redeclared as %C", old, form);
2c73f9f5
ML
5007 return NULL_TREE;
5008 }
5009 if (old)
5010 return old;
5011 if (thislevel_only || tail == global_namespace)
5012 return NULL_TREE;
5013 }
8d08fdba
MS
5014 else
5015 for (tail = level->tags; tail; tail = TREE_CHAIN (tail))
5016 {
a80e4195 5017 if (TREE_PURPOSE (tail) == name)
8d08fdba
MS
5018 {
5019 enum tree_code code = TREE_CODE (TREE_VALUE (tail));
5020 /* Should tighten this up; it'll probably permit
5021 UNION_TYPE and a struct template, for example. */
5022 if (code != form
5566b478 5023 && !(form != ENUMERAL_TYPE && code == TEMPLATE_DECL))
8d08fdba
MS
5024 {
5025 /* Definition isn't the kind we were looking for. */
8251199e 5026 cp_error ("`%#D' redeclared as %C", TREE_VALUE (tail),
8d08fdba 5027 form);
72b7eeff 5028 return NULL_TREE;
8d08fdba
MS
5029 }
5030 return TREE_VALUE (tail);
5031 }
5032 }
5033 if (thislevel_only && ! level->tag_transparent)
5566b478 5034 {
36a117a5 5035 if (level->pseudo_global && allow_pseudo_global)
5566b478 5036 {
36a117a5
MM
5037 /* We must deal with cases like this:
5038
5039 template <class T> struct S;
5040 template <class T> struct S {};
5041
5042 When looking up `S', for the second declaration, we
5043 would like to find the first declaration. But, we
5044 are in the pseudo-global level created for the
5045 template parameters, rather than the (surrounding)
5046 namespace level. Thus, we keep going one more level,
5047 even though THISLEVEL_ONLY is non-zero. */
5048 allow_pseudo_global = 0;
5049 continue;
5566b478 5050 }
36a117a5
MM
5051 else
5052 return NULL_TREE;
5566b478 5053 }
2c73f9f5 5054 if (current_class_type && level->level_chain->namespace_p)
8d08fdba
MS
5055 {
5056 /* Try looking in this class's tags before heading into
5057 global binding level. */
5058 tree context = current_class_type;
5059 while (context)
5060 {
5061 switch (TREE_CODE_CLASS (TREE_CODE (context)))
5062 {
5063 tree these_tags;
5064 case 't':
5065 these_tags = CLASSTYPE_TAGS (context);
5066 if (ANON_AGGRNAME_P (name))
5067 while (these_tags)
5068 {
5069 if (TYPE_IDENTIFIER (TREE_VALUE (these_tags))
5070 == name)
5071 return TREE_VALUE (tail);
5072 these_tags = TREE_CHAIN (these_tags);
5073 }
5074 else
5075 while (these_tags)
5076 {
5077 if (TREE_PURPOSE (these_tags) == name)
5078 {
5079 if (TREE_CODE (TREE_VALUE (these_tags)) != form)
5080 {
8251199e 5081 cp_error ("`%#D' redeclared as %C in class scope",
8d08fdba 5082 TREE_VALUE (tail), form);
72b7eeff 5083 return NULL_TREE;
8d08fdba
MS
5084 }
5085 return TREE_VALUE (tail);
5086 }
5087 these_tags = TREE_CHAIN (these_tags);
5088 }
5089 /* If this type is not yet complete, then don't
5090 look at its context. */
5091 if (TYPE_SIZE (context) == NULL_TREE)
5092 goto no_context;
5093 /* Go to next enclosing type, if any. */
d2e5ee5c 5094 context = DECL_CONTEXT (TYPE_MAIN_DECL (context));
8d08fdba
MS
5095 break;
5096 case 'd':
5097 context = DECL_CONTEXT (context);
5098 break;
5099 default:
5100 my_friendly_abort (10);
5101 }
5102 continue;
5103 no_context:
5104 break;
5105 }
5106 }
5107 }
5108 return NULL_TREE;
5109}
5110
bd6dd845 5111#if 0
8d08fdba
MS
5112void
5113set_current_level_tags_transparency (tags_transparent)
5114 int tags_transparent;
5115{
5116 current_binding_level->tag_transparent = tags_transparent;
5117}
bd6dd845 5118#endif
8d08fdba
MS
5119
5120/* Given a type, find the tag that was defined for it and return the tag name.
5121 Otherwise return 0. However, the value can never be 0
5122 in the cases in which this is used.
5123
5124 C++: If NAME is non-zero, this is the new name to install. This is
5125 done when replacing anonymous tags with real tag names. */
5126
5127static tree
5128lookup_tag_reverse (type, name)
5129 tree type;
5130 tree name;
5131{
5132 register struct binding_level *level;
5133
5134 for (level = current_binding_level; level; level = level->level_chain)
5135 {
5136 register tree tail;
5137 for (tail = level->tags; tail; tail = TREE_CHAIN (tail))
5138 {
5139 if (TREE_VALUE (tail) == type)
5140 {
5141 if (name)
5142 TREE_PURPOSE (tail) = name;
5143 return TREE_PURPOSE (tail);
5144 }
5145 }
5146 }
5147 return NULL_TREE;
5148}
8d08fdba
MS
5149\f
5150/* Lookup TYPE in CONTEXT (a chain of nested types or a FUNCTION_DECL).
5151 Return the type value, or NULL_TREE if not found. */
e92cc029 5152
8d08fdba
MS
5153static tree
5154lookup_nested_type (type, context)
5155 tree type;
5156 tree context;
5157{
5158 if (context == NULL_TREE)
5159 return NULL_TREE;
5160 while (context)
5161 {
5162 switch (TREE_CODE (context))
5163 {
5164 case TYPE_DECL:
5165 {
5166 tree ctype = TREE_TYPE (context);
5167 tree match = value_member (type, CLASSTYPE_TAGS (ctype));
5168 if (match)
5169 return TREE_VALUE (match);
5170 context = DECL_CONTEXT (context);
5171
5172 /* When we have a nested class whose member functions have
5173 local types (e.g., a set of enums), we'll arrive here
5174 with the DECL_CONTEXT as the actual RECORD_TYPE node for
5175 the enclosing class. Instead, we want to make sure we
5176 come back in here with the TYPE_DECL, not the RECORD_TYPE. */
5177 if (context && TREE_CODE (context) == RECORD_TYPE)
5178 context = TREE_CHAIN (context);
5179 }
5180 break;
5181 case FUNCTION_DECL:
e1cd6e56
MS
5182 if (TYPE_NAME (type) && TYPE_IDENTIFIER (type))
5183 return lookup_name (TYPE_IDENTIFIER (type), 1);
5184 return NULL_TREE;
8d08fdba
MS
5185 default:
5186 my_friendly_abort (12);
5187 }
5188 }
5189 return NULL_TREE;
5190}
5191
a9aedbc2 5192/* Look up NAME in the NAMESPACE. */
e92cc029 5193
a9aedbc2
MS
5194tree
5195lookup_namespace_name (namespace, name)
5196 tree namespace, name;
5197{
2c73f9f5 5198 struct tree_binding _b;
30394414 5199 tree val;
2c73f9f5 5200
30394414 5201 my_friendly_assert (TREE_CODE (namespace) == NAMESPACE_DECL, 370);
1231fb96 5202
1231fb96 5203 if (TREE_CODE (name) == NAMESPACE_DECL)
d8f8dca1 5204 /* This happens for A::B<int> when B is a namespace. */
1231fb96 5205 return name;
d8f8dca1
MM
5206 else if (TREE_CODE (name) == TEMPLATE_DECL)
5207 {
5208 /* This happens for A::B where B is a template, and there are no
5209 template arguments. */
5210 cp_error ("invalid use of `%D'", name);
5211 return error_mark_node;
5212 }
1231fb96 5213
b262d64c
JM
5214 namespace = ORIGINAL_NAMESPACE (namespace);
5215
30394414
JM
5216 my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, 373);
5217
2c73f9f5 5218 val = binding_init (&_b);
52c11ef6 5219 if (!qualified_lookup_using_namespace (name, namespace, val, 0))
2c73f9f5
ML
5220 return error_mark_node;
5221
5222 if (BINDING_VALUE (val))
1c35f5b6
JM
5223 {
5224 val = BINDING_VALUE (val);
5225
5226 /* If we have a single function from a using decl, pull it out. */
5227 if (TREE_CODE (val) == OVERLOAD && ! really_overloaded_fn (val))
5228 val = OVL_FUNCTION (val);
5229 return val;
5230 }
5231
8251199e 5232 cp_error ("`%D' undeclared in namespace `%D'", name, namespace);
30394414 5233 return error_mark_node;
a9aedbc2
MS
5234}
5235
7ddedda4
MM
5236/* Hash a TYPENAME_TYPE. K is really of type `tree'. */
5237
5238static unsigned long
5239typename_hash (k)
5240 hash_table_key k;
5241{
5242 unsigned long hash;
5243 tree t;
5244
5245 t = (tree) k;
5246 hash = (((unsigned long) TYPE_CONTEXT (t))
5247 ^ ((unsigned long) DECL_NAME (TYPE_NAME (t))));
5248
5249 return hash;
5250}
5251
5252/* Compare two TYPENAME_TYPEs. K1 and K2 are really of type `tree'. */
5253
5254static boolean
5255typename_compare (k1, k2)
5256 hash_table_key k1;
5257 hash_table_key k2;
5258{
5259 tree t1;
5260 tree t2;
5261 tree d1;
5262 tree d2;
5263
5264 t1 = (tree) k1;
5265 t2 = (tree) k2;
5266 d1 = TYPE_NAME (t1);
5267 d2 = TYPE_NAME (t2);
5268
5269 return (DECL_NAME (d1) == DECL_NAME (d2)
5270 && same_type_p (TYPE_CONTEXT (t1), TYPE_CONTEXT (t2))
5271 && ((TREE_TYPE (t1) != NULL_TREE)
5272 == (TREE_TYPE (t2) != NULL_TREE))
5273 && same_type_p (TREE_TYPE (t1), TREE_TYPE (t2))
5274 && TYPENAME_TYPE_FULLNAME (t1) == TYPENAME_TYPE_FULLNAME (t2));
5275}
5276
45869a6c
MM
5277/* Build a TYPENAME_TYPE. If the type is `typename T::t', CONTEXT is
5278 the type of `T', NAME is the IDENTIFIER_NODE for `t'. If BASE_TYPE
5279 is non-NULL, this type is being created by the implicit typename
5280 extension, and BASE_TYPE is a type named `t' in some base class of
5281 `T' which depends on template parameters.
5282
5283 Returns the new TYPENAME_TYPE. */
5284
5285tree
5286build_typename_type (context, name, fullname, base_type)
5287 tree context;
5288 tree name;
5289 tree fullname;
5290 tree base_type;
5291{
5292 tree t;
5293 tree d;
7ddedda4 5294 struct hash_entry* e;
45869a6c 5295
7ddedda4
MM
5296 static struct hash_table ht;
5297
5298 push_obstacks (&permanent_obstack, &permanent_obstack);
5299
5300 if (!ht.table
5301 && !hash_table_init (&ht, &hash_newfunc, &typename_hash,
5302 &typename_compare))
5303 fatal ("virtual memory exhausted");
45869a6c 5304
78638e24
MM
5305 /* The FULLNAME needs to exist for the life of the hash table, i.e.,
5306 for the entire compilation. */
5307 if (!TREE_PERMANENT (fullname))
5308 fullname = copy_to_permanent (fullname);
5309
45869a6c
MM
5310 /* Build the TYPENAME_TYPE. */
5311 t = make_lang_type (TYPENAME_TYPE);
5312 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
5313 TYPENAME_TYPE_FULLNAME (t) = fullname;
5314 TREE_TYPE (t) = base_type;
45869a6c
MM
5315
5316 /* Build the corresponding TYPE_DECL. */
5317 d = build_decl (TYPE_DECL, name, t);
5318 TYPE_NAME (TREE_TYPE (d)) = d;
5319 TYPE_STUB_DECL (TREE_TYPE (d)) = d;
5320 DECL_CONTEXT (d) = FROB_CONTEXT (context);
2642b9bf 5321 DECL_ARTIFICIAL (d) = 1;
45869a6c 5322
7ddedda4
MM
5323 /* See if we already have this type. */
5324 e = hash_lookup (&ht, t, /*create=*/false, /*copy=*/0);
5325 if (e)
5326 {
5327 /* This will free not only TREE_TYPE, but the lang-specific data
5328 and the TYPE_DECL as well. */
5329 obstack_free (&permanent_obstack, t);
5330 t = (tree) e->key;
5331 }
5332 else
5333 /* Insert the type into the table. */
5334 hash_lookup (&ht, t, /*create=*/true, /*copy=*/0);
5335
5336 pop_obstacks ();
45869a6c
MM
5337
5338 return t;
5339}
5340
5566b478
MS
5341tree
5342make_typename_type (context, name)
5343 tree context, name;
5344{
45869a6c 5345 tree t;
b2b7d40a 5346 tree fullname;
a80e4195 5347
653cc74a 5348 if (TREE_CODE_CLASS (TREE_CODE (name)) == 't')
78638e24
MM
5349 {
5350 if (!(TYPE_LANG_SPECIFIC (name)
5351 && (CLASSTYPE_IS_TEMPLATE (name)
5352 || CLASSTYPE_USE_TEMPLATE (name))))
5353 name = TYPE_IDENTIFIER (name);
5354 else
5355 /* Create a TEMPLATE_ID_EXPR for the type. */
5356 name = build_nt (TEMPLATE_ID_EXPR,
5357 CLASSTYPE_TI_TEMPLATE (name),
5358 CLASSTYPE_TI_ARGS (name));
5359 }
653cc74a 5360 else if (TREE_CODE (name) == TYPE_DECL)
a80e4195 5361 name = DECL_NAME (name);
b2b7d40a
JM
5362
5363 fullname = name;
5364
5365 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
11686454
JM
5366 {
5367 name = TREE_OPERAND (name, 0);
5368 if (TREE_CODE (name) == TEMPLATE_DECL)
5369 name = TREE_OPERAND (fullname, 0) = DECL_NAME (name);
5370 }
b2b7d40a 5371 if (TREE_CODE (name) != IDENTIFIER_NODE)
a80e4195 5372 my_friendly_abort (2000);
5566b478 5373
04ddee1b
BK
5374 if (TREE_CODE (context) == NAMESPACE_DECL)
5375 {
5376 /* We can get here from typename_sub0 in the explicit_template_type
5377 expansion. Just fail. */
5378 cp_error ("no class template named `%#T' in `%#T'",
5379 name, context);
5380 return error_mark_node;
5381 }
5382
85b71cf2 5383 if (! uses_template_parms (context)
b77ead33 5384 || currently_open_class (context))
5566b478 5385 {
b2b7d40a
JM
5386 if (TREE_CODE (fullname) == TEMPLATE_ID_EXPR)
5387 {
5388 if (IS_AGGR_TYPE (context))
5389 t = lookup_field (context, name, 0, 0);
5390 else
b2b7d40a 5391 {
8251199e 5392 cp_error ("no class template named `%#T' in `%#T'",
b2b7d40a
JM
5393 name, context);
5394 return error_mark_node;
5395 }
ffb690bd 5396
7d4bdeed
MM
5397 if (t && DECL_CLASS_TEMPLATE_P (t))
5398 return lookup_template_class (t, TREE_OPERAND (fullname, 1),
5399 NULL_TREE, context,
5400 /*entering_scope=*/0);
b2b7d40a
JM
5401 }
5402 else
5566b478 5403 {
b2b7d40a
JM
5404 if (IS_AGGR_TYPE (context))
5405 t = lookup_field (context, name, 0, 1);
5406 else
b2b7d40a 5407 {
8251199e 5408 cp_error ("no type named `%#T' in `%#T'", name, context);
b2b7d40a
JM
5409 return error_mark_node;
5410 }
1107c4b3 5411
7d4bdeed
MM
5412 if (t)
5413 return TREE_TYPE (t);
5566b478 5414 }
5566b478 5415 }
11249cf0
MM
5416
5417 /* If the CONTEXT is not a template type, then either the field is
5418 there now or its never going to be. */
5419 if (!uses_template_parms (context) && !t)
5420 {
5421 cp_error ("no type named `%#T' in `%#T'", name, context);
5422 return error_mark_node;
5423 }
5424
45869a6c
MM
5425
5426 return build_typename_type (context, name, fullname, NULL_TREE);
5566b478
MS
5427}
5428
2c73f9f5
ML
5429/* Select the right _DECL from multiple choices. */
5430
5431static tree
52c11ef6 5432select_decl (binding, flags)
2c73f9f5 5433 tree binding;
52c11ef6 5434 int flags;
2c73f9f5
ML
5435{
5436 tree val;
5437 val = BINDING_VALUE (binding);
52c11ef6 5438 if (LOOKUP_NAMESPACES_ONLY (flags))
3e3f722c
ML
5439 {
5440 /* We are not interested in types. */
5441 if (val && TREE_CODE (val) == NAMESPACE_DECL)
5442 return val;
5443 return NULL_TREE;
5444 }
5445
2c73f9f5
ML
5446 /* If we could have a type and
5447 we have nothing or we need a type and have none. */
5448 if (BINDING_TYPE (binding)
52c11ef6
JM
5449 && (!val || ((flags & LOOKUP_PREFER_TYPES)
5450 && TREE_CODE (val) != TYPE_DECL)))
2c73f9f5
ML
5451 val = TYPE_STUB_DECL (BINDING_TYPE (binding));
5452 /* Don't return non-types if we really prefer types. */
52c11ef6 5453 else if (val && LOOKUP_TYPES_ONLY (flags) && TREE_CODE (val) != TYPE_DECL
c592d5d2
ML
5454 && (TREE_CODE (val) != TEMPLATE_DECL
5455 || !DECL_CLASS_TEMPLATE_P (val)))
2c73f9f5 5456 val = NULL_TREE;
1c35f5b6 5457
2c73f9f5
ML
5458 return val;
5459}
5460
ea9635c7
ML
5461/* Unscoped lookup of a global, iterate over namespaces, considering
5462 using namespace statements. */
5463
5464static tree
52c11ef6 5465unqualified_namespace_lookup (name, flags)
ea9635c7 5466 tree name;
52c11ef6 5467 int flags;
ea9635c7
ML
5468{
5469 struct tree_binding _binding;
5470 tree b = binding_init (&_binding);
5471 tree initial = current_decl_namespace();
5472 tree scope = initial;
5473 tree siter;
5474 struct binding_level *level;
5475 tree val = NULL_TREE;
5476
5477 while (!val)
5478 {
5479 val = binding_for_name (name, scope);
5480
5481 /* Initialize binding for this context. */
5482 BINDING_VALUE (b) = BINDING_VALUE (val);
5483 BINDING_TYPE (b) = BINDING_TYPE (val);
5484
5485 /* Add all _DECLs seen through local using-directives. */
5486 for (level = current_binding_level;
5487 !level->namespace_p;
5488 level = level->level_chain)
52c11ef6
JM
5489 if (!lookup_using_namespace (name, b, level->using_directives,
5490 scope, flags))
ea9635c7 5491 /* Give up because of error. */
5b163de4 5492 return error_mark_node;
ea9635c7
ML
5493
5494 /* Add all _DECLs seen through global using-directives. */
5495 /* XXX local and global using lists should work equally. */
5496 siter = initial;
5497 while (1)
5498 {
5499 if (!lookup_using_namespace (name, b, DECL_NAMESPACE_USING (siter),
52c11ef6 5500 scope, flags))
ea9635c7 5501 /* Give up because of error. */
5b163de4 5502 return error_mark_node;
ea9635c7
ML
5503 if (siter == scope) break;
5504 siter = CP_DECL_CONTEXT (siter);
5505 }
5506
52c11ef6 5507 val = select_decl (b, flags);
ea9635c7
ML
5508 if (scope == global_namespace)
5509 break;
0c64a9ca 5510 scope = CP_DECL_CONTEXT (scope);
ea9635c7
ML
5511 }
5512 return val;
5513}
5514
52c11ef6
JM
5515/* Combine prefer_type and namespaces_only into flags. */
5516
5517static int
5518lookup_flags (prefer_type, namespaces_only)
5519 int prefer_type, namespaces_only;
5520{
5521 if (namespaces_only)
5522 return LOOKUP_PREFER_NAMESPACES;
5523 if (prefer_type > 1)
5524 return LOOKUP_PREFER_TYPES;
5525 if (prefer_type > 0)
5526 return LOOKUP_PREFER_BOTH;
5527 return 0;
5528}
5529
5530/* Given a lookup that returned VAL, use FLAGS to decide if we want to
5531 ignore it or not. Subroutine of lookup_name_real. */
5532
5533static tree
5534qualify_lookup (val, flags)
5535 tree val;
5536 int flags;
5537{
5538 if (val == NULL_TREE)
5539 return val;
6b945830
JM
5540 if ((flags & LOOKUP_PREFER_NAMESPACES) && TREE_CODE (val) == NAMESPACE_DECL)
5541 return val;
5542 if ((flags & LOOKUP_PREFER_TYPES)
5543 && (TREE_CODE (val) == TYPE_DECL
5544 || ((flags & LOOKUP_TEMPLATES_EXPECTED)
5545 && DECL_CLASS_TEMPLATE_P (val))))
5546 return val;
5547 if (flags & (LOOKUP_PREFER_NAMESPACES | LOOKUP_PREFER_TYPES))
52c11ef6
JM
5548 return NULL_TREE;
5549 return val;
5550}
5551
235f734d
MM
5552/* Any other BINDING overrides an implicit TYPENAME. Warn about
5553 that. */
5554
5555static void
5556warn_about_implicit_typename_lookup (typename, binding)
5557 tree typename;
5558 tree binding;
5559{
5560 tree subtype = TREE_TYPE (TREE_TYPE (typename));
5561 tree name = DECL_NAME (typename);
5562
5563 if (! (TREE_CODE (binding) == TEMPLATE_DECL
5564 && CLASSTYPE_TEMPLATE_INFO (subtype)
5565 && CLASSTYPE_TI_TEMPLATE (subtype) == binding)
5566 && ! (TREE_CODE (binding) == TYPE_DECL
5567 && same_type_p (TREE_TYPE (binding), subtype)))
5568 {
5569 cp_warning ("lookup of `%D' finds `%#D'",
5570 name, binding);
5571 cp_warning (" instead of `%D' from dependent base class",
5572 typename);
5573 cp_warning (" (use `typename %T::%D' if that's what you meant)",
5574 constructor_name (current_class_type), name);
5575 }
5576}
5577
8d08fdba
MS
5578/* Look up NAME in the current binding level and its superiors in the
5579 namespace of variables, functions and typedefs. Return a ..._DECL
5580 node of some kind representing its definition if there is only one
5581 such declaration, or return a TREE_LIST with all the overloaded
5582 definitions if there are many, or return 0 if it is undefined.
5583
2c73f9f5
ML
5584 If PREFER_TYPE is > 0, we prefer TYPE_DECLs or namespaces.
5585 If PREFER_TYPE is > 1, we reject non-type decls (e.g. namespaces).
a28e3c7f 5586 If PREFER_TYPE is -2, we're being called from yylex(). (UGLY)
f84b4be9
JM
5587 Otherwise we prefer non-TYPE_DECLs.
5588
5589 If NONCLASS is non-zero, we don't look for the NAME in class scope,
5590 using IDENTIFIER_CLASS_VALUE. */
8d08fdba 5591
824b9a4c 5592static tree
3e3f722c 5593lookup_name_real (name, prefer_type, nonclass, namespaces_only)
8d08fdba 5594 tree name;
3e3f722c 5595 int prefer_type, nonclass, namespaces_only;
8d08fdba 5596{
235f734d
MM
5597 tree t;
5598 tree val = NULL_TREE;
a28e3c7f 5599 int yylex = 0;
e1cd6e56 5600 tree from_obj = NULL_TREE;
52c11ef6 5601 int flags;
235f734d 5602 int val_is_implicit_typename = 0;
8d08fdba 5603
3e3f722c
ML
5604 /* Hack: copy flag set by parser, if set. */
5605 if (only_namespace_names)
5606 namespaces_only = 1;
52c11ef6 5607
a28e3c7f
MS
5608 if (prefer_type == -2)
5609 {
5610 extern int looking_for_typename;
fc378698 5611 tree type = NULL_TREE;
a28e3c7f
MS
5612
5613 yylex = 1;
5614 prefer_type = looking_for_typename;
e1cd6e56 5615
52c11ef6 5616 flags = lookup_flags (prefer_type, namespaces_only);
52c11ef6
JM
5617 /* If the next thing is '<', class templates are types. */
5618 if (looking_for_template)
5619 flags |= LOOKUP_TEMPLATES_EXPECTED;
5620
653cc74a
JM
5621 /* std:: becomes :: for now. */
5622 if (got_scope == std_node)
5623 got_scope = void_type_node;
5624
e1cd6e56
MS
5625 if (got_scope)
5626 type = got_scope;
dff6b454 5627 else if (got_object != error_mark_node)
e1cd6e56 5628 type = got_object;
a28e3c7f 5629
e1cd6e56 5630 if (type)
a28e3c7f 5631 {
e1cd6e56 5632 if (type == error_mark_node)
f376e137 5633 return error_mark_node;
a80e4195
MS
5634 if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type))
5635 type = TREE_TYPE (type);
5566b478 5636
2b9dc906
JM
5637 if (TYPE_P (type))
5638 type = complete_type (type);
5566b478 5639
a1774733 5640 if (TREE_CODE (type) == VOID_TYPE)
2c73f9f5
ML
5641 type = global_namespace;
5642 if (TREE_CODE (type) == NAMESPACE_DECL)
a9aedbc2 5643 {
2c73f9f5
ML
5644 struct tree_binding b;
5645 val = binding_init (&b);
52c11ef6 5646 if (!qualified_lookup_using_namespace (name, type, val, flags))
2c73f9f5 5647 return NULL_TREE;
52c11ef6 5648 val = select_decl (val, flags);
a9aedbc2 5649 }
5566b478 5650 else if (! IS_AGGR_TYPE (type)
5156628f 5651 || TREE_CODE (type) == TEMPLATE_TYPE_PARM
73b0fce8 5652 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM
5156628f 5653 || TREE_CODE (type) == TYPENAME_TYPE)
e92cc029 5654 /* Someone else will give an error about this if needed. */
a28e3c7f 5655 val = NULL_TREE;
e1cd6e56 5656 else if (type == current_class_type)
a28e3c7f 5657 val = IDENTIFIER_CLASS_VALUE (name);
a28e3c7f 5658 else
d23a1bb1 5659 val = lookup_member (type, name, 0, prefer_type);
a28e3c7f 5660 }
e1cd6e56
MS
5661 else
5662 val = NULL_TREE;
5663
594740f3 5664 if (got_scope)
e1cd6e56 5665 goto done;
594740f3 5666 else if (got_object && val)
e1cd6e56 5667 from_obj = val;
a28e3c7f 5668 }
52c11ef6 5669 else
5b163de4
JM
5670 {
5671 flags = lookup_flags (prefer_type, namespaces_only);
5672 /* If we're not parsing, we need to complain. */
5673 flags |= LOOKUP_COMPLAIN;
5674 }
e76a2646 5675
d8f8dca1 5676 /* First, look in non-namespace scopes. */
235f734d 5677 for (t = IDENTIFIER_BINDING (name); t; t = TREE_CHAIN (t))
d8f8dca1 5678 {
235f734d
MM
5679 tree binding;
5680
5681 if (!LOCAL_BINDING_P (t) && nonclass)
d8f8dca1
MM
5682 /* We're not looking for class-scoped bindings, so keep going. */
5683 continue;
5684
5685 /* If this is the kind of thing we're looking for, we're done. */
235f734d
MM
5686 if (qualify_lookup (BINDING_VALUE (t), flags))
5687 binding = BINDING_VALUE (t);
d8f8dca1 5688 else if ((flags & LOOKUP_PREFER_TYPES)
235f734d
MM
5689 && qualify_lookup (BINDING_TYPE (t), flags))
5690 binding = BINDING_TYPE (t);
5691 else
5692 binding = NULL_TREE;
5693
5694 if (binding
5695 && (!val || !(TREE_CODE (binding) == TYPE_DECL
5696 && IMPLICIT_TYPENAME_P (TREE_TYPE (binding)))))
d8f8dca1 5697 {
235f734d
MM
5698 if (val_is_implicit_typename && !yylex)
5699 warn_about_implicit_typename_lookup (val, binding);
5700 val = binding;
5701 val_is_implicit_typename
5702 = (TREE_CODE (val) == TYPE_DECL
5703 && IMPLICIT_TYPENAME_P (TREE_TYPE (val)));
5704 if (!val_is_implicit_typename)
5705 break;
d8f8dca1
MM
5706 }
5707 }
f181d4ae 5708
235f734d
MM
5709 /* Now lookup in namespace scopes. */
5710 if (!val || val_is_implicit_typename)
e76a2646 5711 {
235f734d
MM
5712 t = unqualified_namespace_lookup (name, flags);
5713 if (t)
c1def683 5714 {
235f734d
MM
5715 if (val_is_implicit_typename && !yylex)
5716 warn_about_implicit_typename_lookup (val, t);
5717 val = t;
c1def683
JM
5718 }
5719 }
5720
a28e3c7f 5721 done:
8d08fdba
MS
5722 if (val)
5723 {
c91a56d2 5724 /* This should only warn about types used in qualified-ids. */
e1cd6e56 5725 if (from_obj && from_obj != val)
5566b478 5726 {
c91a56d2
MS
5727 if (looking_for_typename && TREE_CODE (from_obj) == TYPE_DECL
5728 && TREE_CODE (val) == TYPE_DECL
5729 && TREE_TYPE (from_obj) != TREE_TYPE (val))
5730 {
8251199e 5731 cp_pedwarn ("lookup of `%D' in the scope of `%#T' (`%#T')",
c91a56d2 5732 name, got_object, TREE_TYPE (from_obj));
8251199e 5733 cp_pedwarn (" does not match lookup in the current scope (`%#T')",
c91a56d2
MS
5734 TREE_TYPE (val));
5735 }
594740f3 5736
b8b1a3c1
JM
5737 /* We don't change val to from_obj if got_object depends on
5738 template parms because that breaks implicit typename for
5739 destructor calls. */
5740 if (! uses_template_parms (got_object))
53fdbb3b 5741 val = from_obj;
5566b478 5742 }
e1cd6e56 5743
0c64a9ca
JM
5744 /* If we have a single function from a using decl, pull it out. */
5745 if (TREE_CODE (val) == OVERLOAD && ! really_overloaded_fn (val))
5746 val = OVL_FUNCTION (val);
8d08fdba 5747 }
e1cd6e56
MS
5748 else if (from_obj)
5749 val = from_obj;
8d08fdba
MS
5750
5751 return val;
5752}
5753
700f8a87
MS
5754tree
5755lookup_name_nonclass (name)
5756 tree name;
5757{
3e3f722c 5758 return lookup_name_real (name, 0, 1, 0);
700f8a87
MS
5759}
5760
2c73f9f5
ML
5761tree
5762lookup_function_nonclass (name, args)
5763 tree name;
5764 tree args;
5765{
5766 return lookup_arg_dependent (name, lookup_name_nonclass (name), args);
5767}
5768
3e3f722c
ML
5769tree
5770lookup_name_namespace_only (name)
5771 tree name;
5772{
5773 /* type-or-namespace, nonclass, namespace_only */
5774 return lookup_name_real (name, 1, 1, 1);
5775}
5776
700f8a87
MS
5777tree
5778lookup_name (name, prefer_type)
5779 tree name;
5780 int prefer_type;
5781{
3e3f722c 5782 return lookup_name_real (name, prefer_type, 0, 0);
700f8a87
MS
5783}
5784
a7d2d407
MM
5785/* Similar to `lookup_name' but look only in the innermost non-class
5786 binding level. */
8d08fdba
MS
5787
5788tree
5789lookup_name_current_level (name)
5790 tree name;
5791{
a7d2d407
MM
5792 struct binding_level *b;
5793 tree t = NULL_TREE;
8d08fdba 5794
a7d2d407
MM
5795 b = current_binding_level;
5796 while (b->parm_flag == 2)
5797 b = b->level_chain;
5798
5799 if (b->namespace_p)
8d08fdba 5800 {
30394414 5801 t = IDENTIFIER_NAMESPACE_VALUE (name);
8d08fdba
MS
5802
5803 /* extern "C" function() */
5804 if (t != NULL_TREE && TREE_CODE (t) == TREE_LIST)
5805 t = TREE_VALUE (t);
5806 }
f181d4ae
MM
5807 else if (IDENTIFIER_BINDING (name)
5808 && LOCAL_BINDING_P (IDENTIFIER_BINDING (name)))
8d08fdba 5809 {
a4443a08
MS
5810 while (1)
5811 {
f181d4ae
MM
5812 if (BINDING_LEVEL (IDENTIFIER_BINDING (name)) == b)
5813 return IDENTIFIER_VALUE (name);
5814
9ed182dc
JM
5815 if (b->keep == 2)
5816 b = b->level_chain;
5817 else
5818 break;
5819 }
5820 }
5821
5822 return t;
5823}
5824
5825/* Like lookup_name_current_level, but for types. */
5826
5827tree
5828lookup_type_current_level (name)
5829 tree name;
5830{
5831 register tree t = NULL_TREE;
5832
5833 my_friendly_assert (! current_binding_level->namespace_p, 980716);
5834
5835 if (REAL_IDENTIFIER_TYPE_VALUE (name) != NULL_TREE
5836 && REAL_IDENTIFIER_TYPE_VALUE (name) != global_type_node)
5837 {
5838 struct binding_level *b = current_binding_level;
5839 while (1)
5840 {
5841 if (purpose_member (name, b->type_shadowed))
5842 return REAL_IDENTIFIER_TYPE_VALUE (name);
a4443a08
MS
5843 if (b->keep == 2)
5844 b = b->level_chain;
5845 else
5846 break;
5847 }
8d08fdba
MS
5848 }
5849
5850 return t;
5851}
3e3f722c
ML
5852
5853void
5854begin_only_namespace_names ()
5855{
5856 only_namespace_names = 1;
5857}
5858
5859void
5860end_only_namespace_names ()
5861{
5862 only_namespace_names = 0;
5863}
8d08fdba
MS
5864\f
5865/* Arrange for the user to get a source line number, even when the
5866 compiler is going down in flames, so that she at least has a
5867 chance of working around problems in the compiler. We used to
5868 call error(), but that let the segmentation fault continue
5869 through; now, it's much more passive by asking them to send the
5870 maintainers mail about the problem. */
5871
5872static void
5873signal_catch (sig)
7dee3f36 5874 int sig ATTRIBUTE_UNUSED;
8d08fdba
MS
5875{
5876 signal (SIGSEGV, SIG_DFL);
5877#ifdef SIGIOT
5878 signal (SIGIOT, SIG_DFL);
5879#endif
5880#ifdef SIGILL
5881 signal (SIGILL, SIG_DFL);
5882#endif
5883#ifdef SIGABRT
5884 signal (SIGABRT, SIG_DFL);
5885#endif
5886#ifdef SIGBUS
5887 signal (SIGBUS, SIG_DFL);
5888#endif
5889 my_friendly_abort (0);
5890}
5891
de22184b
MS
5892#if 0
5893/* Unused -- brendan 970107 */
8d08fdba
MS
5894/* Array for holding types considered "built-in". These types
5895 are output in the module in which `main' is defined. */
5896static tree *builtin_type_tdescs_arr;
5897static int builtin_type_tdescs_len, builtin_type_tdescs_max;
de22184b 5898#endif
8d08fdba
MS
5899
5900/* Push the declarations of builtin types into the namespace.
5901 RID_INDEX, if < RID_MAX is the index of the builtin type
5902 in the array RID_POINTERS. NAME is the name used when looking
5903 up the builtin type. TYPE is the _TYPE node for the builtin type. */
5904
5905static void
5906record_builtin_type (rid_index, name, type)
5907 enum rid rid_index;
d8e178a0 5908 const char *name;
8d08fdba
MS
5909 tree type;
5910{
5911 tree rname = NULL_TREE, tname = NULL_TREE;
a703fb38 5912 tree tdecl = NULL_TREE;
8d08fdba
MS
5913
5914 if ((int) rid_index < (int) RID_MAX)
5915 rname = ridpointers[(int) rid_index];
5916 if (name)
5917 tname = get_identifier (name);
5918
5919 TYPE_BUILT_IN (type) = 1;
5920
5921 if (tname)
5922 {
8d08fdba 5923 tdecl = pushdecl (build_decl (TYPE_DECL, tname, type));
8d08fdba
MS
5924 set_identifier_type_value (tname, NULL_TREE);
5925 if ((int) rid_index < (int) RID_MAX)
2c73f9f5
ML
5926 /* Built-in types live in the global namespace. */
5927 SET_IDENTIFIER_GLOBAL_VALUE (tname, tdecl);
8d08fdba
MS
5928 }
5929 if (rname != NULL_TREE)
5930 {
5931 if (tname != NULL_TREE)
5932 {
5933 set_identifier_type_value (rname, NULL_TREE);
2c73f9f5 5934 SET_IDENTIFIER_GLOBAL_VALUE (rname, tdecl);
8d08fdba
MS
5935 }
5936 else
5937 {
8d08fdba 5938 tdecl = pushdecl (build_decl (TYPE_DECL, rname, type));
8d08fdba
MS
5939 set_identifier_type_value (rname, NULL_TREE);
5940 }
5941 }
8d08fdba
MS
5942}
5943
eff71ab0
PB
5944/* Record one of the standard Java types.
5945 * Declare it as having the given NAME.
5946 * If SIZE > 0, it is the size of one of the integral types;
5947 * otherwise it is the negative of the size of one of the other types. */
5948
5949static tree
5950record_builtin_java_type (name, size)
d8e178a0 5951 const char *name;
eff71ab0
PB
5952 int size;
5953{
5954 tree type, decl;
5955 if (size > 0)
5956 type = make_signed_type (size);
5957 else if (size > -32)
5958 { /* "__java_char" or ""__java_boolean". */
5959 type = make_unsigned_type (-size);
5960 /*if (size == -1) TREE_SET_CODE (type, BOOLEAN_TYPE);*/
5961 }
5962 else
5963 { /* "__java_float" or ""__java_double". */
5964 type = make_node (REAL_TYPE);
5965 TYPE_PRECISION (type) = - size;
5966 layout_type (type);
5967 }
5968 record_builtin_type (RID_MAX, name, type);
5969 decl = TYPE_NAME (type);
5970 DECL_IGNORED_P (decl) = 1;
5971 TYPE_FOR_JAVA (type) = 1;
5972 return type;
5973}
5974
036407f7
ML
5975/* Push a type into the namespace so that the back-ends ignore it. */
5976
5977static void
5978record_unknown_type (type, name)
5979 tree type;
d8e178a0 5980 const char *name;
036407f7
ML
5981{
5982 tree decl = pushdecl (build_decl (TYPE_DECL, get_identifier (name), type));
5983 /* Make sure the "unknown type" typedecl gets ignored for debug info. */
5984 DECL_IGNORED_P (decl) = 1;
5985 TYPE_DECL_SUPPRESS_DEBUG (decl) = 1;
5986 TYPE_SIZE (type) = TYPE_SIZE (void_type_node);
5987 TYPE_ALIGN (type) = 1;
5988 TYPE_MODE (type) = TYPE_MODE (void_type_node);
5989}
5990
8d08fdba
MS
5991/* Push overloaded decl, in global scope, with one argument so it
5992 can be used as a callback from define_function. */
e92cc029 5993
8d08fdba
MS
5994static void
5995push_overloaded_decl_1 (x)
5996 tree x;
5997{
7bdbfa05 5998 push_overloaded_decl (x, PUSH_GLOBAL);
8d08fdba
MS
5999}
6000
8ccc31eb
MS
6001#ifdef __GNUC__
6002__inline
6003#endif
6b5fbb55
MS
6004tree
6005auto_function (name, type, code)
8ccc31eb
MS
6006 tree name, type;
6007 enum built_in_function code;
6008{
6009 return define_function
49c249e1 6010 (IDENTIFIER_POINTER (name), type, code, push_overloaded_decl_1,
8ccc31eb
MS
6011 IDENTIFIER_POINTER (build_decl_overload (name, TYPE_ARG_TYPES (type),
6012 0)));
6013}
6014
8d08fdba
MS
6015/* Create the predefined scalar types of C,
6016 and some nodes representing standard constants (0, 1, (void *)0).
6017 Initialize the global binding level.
6018 Make definitions for built-in primitive functions. */
6019
6020void
6021init_decl_processing ()
6022{
de22184b 6023 register tree endlink, int_endlink, double_endlink, unsigned_endlink;
8d08fdba 6024 tree fields[20];
8d08fdba 6025 /* Data type of memcpy. */
cffa8729 6026 tree memcpy_ftype, strlen_ftype;
8d08fdba
MS
6027 int wchar_type_size;
6028 tree temp;
6029 tree array_domain_type;
a703fb38 6030 tree vb_off_identifier = NULL_TREE;
5566b478
MS
6031 /* Function type `char *(char *, char *)' and similar ones */
6032 tree string_ftype_ptr_ptr, int_ftype_string_string;
de22184b
MS
6033 tree sizetype_endlink;
6034 tree ptr_ftype, ptr_ftype_unsigned, ptr_ftype_sizetype;
5eea678f 6035 tree void_ftype, void_ftype_int, void_ftype_ptr;
8d08fdba
MS
6036
6037 /* Have to make these distinct before we try using them. */
6038 lang_name_cplusplus = get_identifier ("C++");
6039 lang_name_c = get_identifier ("C");
a1774733 6040 lang_name_java = get_identifier ("Java");
8d08fdba 6041
2c73f9f5 6042 /* Enter the global namespace. */
30394414
JM
6043 my_friendly_assert (global_namespace == NULL_TREE, 375);
6044 my_friendly_assert (current_lang_name == NULL_TREE, 375);
6045 current_lang_name = lang_name_cplusplus;
6046 push_namespace (get_identifier ("::"));
6047 global_namespace = current_namespace;
6048 current_lang_name = NULL_TREE;
6049
e1cd6e56 6050 if (flag_strict_prototype == 2)
830fcda8 6051 flag_strict_prototype = pedantic;
2642b9bf
JM
6052 if (! flag_permissive && ! pedantic)
6053 flag_pedantic_errors = 1;
830fcda8
JM
6054
6055 strict_prototypes_lang_c = flag_strict_prototype;
8926095f 6056
8d08fdba
MS
6057 /* Initially, C. */
6058 current_lang_name = lang_name_c;
6059
6060 current_function_decl = NULL_TREE;
6061 named_labels = NULL_TREE;
e349ee73 6062 named_label_uses = NULL;
8d08fdba
MS
6063 current_binding_level = NULL_BINDING_LEVEL;
6064 free_binding_level = NULL_BINDING_LEVEL;
6065
6066 /* Because most segmentation signals can be traced back into user
6067 code, catch them and at least give the user a chance of working
e92cc029 6068 around compiler bugs. */
8d08fdba
MS
6069 signal (SIGSEGV, signal_catch);
6070
6071 /* We will also catch aborts in the back-end through signal_catch and
6072 give the user a chance to see where the error might be, and to defeat
6073 aborts in the back-end when there have been errors previously in their
e92cc029 6074 code. */
8d08fdba
MS
6075#ifdef SIGIOT
6076 signal (SIGIOT, signal_catch);
6077#endif
6078#ifdef SIGILL
6079 signal (SIGILL, signal_catch);
6080#endif
6081#ifdef SIGABRT
6082 signal (SIGABRT, signal_catch);
6083#endif
6084#ifdef SIGBUS
6085 signal (SIGBUS, signal_catch);
6086#endif
6087
6088 gcc_obstack_init (&decl_obstack);
8d08fdba
MS
6089
6090 /* Must lay these out before anything else gets laid out. */
6091 error_mark_node = make_node (ERROR_MARK);
6092 TREE_PERMANENT (error_mark_node) = 1;
6093 TREE_TYPE (error_mark_node) = error_mark_node;
6094 error_mark_list = build_tree_list (error_mark_node, error_mark_node);
6095 TREE_TYPE (error_mark_list) = error_mark_node;
6096
a28e3c7f
MS
6097 /* Make the binding_level structure for global names. */
6098 pushlevel (0);
8d08fdba 6099 global_binding_level = current_binding_level;
2c73f9f5
ML
6100 /* The global level is the namespace level of ::. */
6101 NAMESPACE_LEVEL (global_namespace) = global_binding_level;
6102 declare_namespace_level ();
8d08fdba
MS
6103
6104 this_identifier = get_identifier (THIS_NAME);
6105 in_charge_identifier = get_identifier (IN_CHARGE_NAME);
fc378698
MS
6106 ctor_identifier = get_identifier (CTOR_NAME);
6107 dtor_identifier = get_identifier (DTOR_NAME);
8d08fdba
MS
6108 pfn_identifier = get_identifier (VTABLE_PFN_NAME);
6109 index_identifier = get_identifier (VTABLE_INDEX_NAME);
6110 delta_identifier = get_identifier (VTABLE_DELTA_NAME);
6111 delta2_identifier = get_identifier (VTABLE_DELTA2_NAME);
6112 pfn_or_delta2_identifier = get_identifier ("__pfn_or_delta2");
dff6b454
RK
6113 if (flag_handle_signatures)
6114 {
6115 tag_identifier = get_identifier (SIGTABLE_TAG_NAME);
9dd70aa4
GB
6116 vb_off_identifier = get_identifier (SIGTABLE_VB_OFF_NAME);
6117 vt_off_identifier = get_identifier (SIGTABLE_VT_OFF_NAME);
dff6b454 6118 }
8d08fdba
MS
6119
6120 /* Define `int' and `char' first so that dbx will output them first. */
6121
6122 integer_type_node = make_signed_type (INT_TYPE_SIZE);
6123 record_builtin_type (RID_INT, NULL_PTR, integer_type_node);
6124
6125 /* Define `char', which is like either `signed char' or `unsigned char'
6126 but not the same as either. */
6127
beb53fb8
JM
6128 char_type_node
6129 = (flag_signed_char
6130 ? make_signed_type (CHAR_TYPE_SIZE)
6131 : make_unsigned_type (CHAR_TYPE_SIZE));
8d08fdba
MS
6132 record_builtin_type (RID_CHAR, "char", char_type_node);
6133
45075bf3
NS
6134 /* `signed' is the same as `int' */
6135 record_builtin_type (RID_SIGNED, NULL_PTR, integer_type_node);
6136
8d08fdba
MS
6137 long_integer_type_node = make_signed_type (LONG_TYPE_SIZE);
6138 record_builtin_type (RID_LONG, "long int", long_integer_type_node);
6139
6140 unsigned_type_node = make_unsigned_type (INT_TYPE_SIZE);
6141 record_builtin_type (RID_UNSIGNED, "unsigned int", unsigned_type_node);
6142
6143 long_unsigned_type_node = make_unsigned_type (LONG_TYPE_SIZE);
6144 record_builtin_type (RID_MAX, "long unsigned int", long_unsigned_type_node);
6145 record_builtin_type (RID_MAX, "unsigned long", long_unsigned_type_node);
6146
6147 long_long_integer_type_node = make_signed_type (LONG_LONG_TYPE_SIZE);
6148 record_builtin_type (RID_MAX, "long long int", long_long_integer_type_node);
6149
6150 long_long_unsigned_type_node = make_unsigned_type (LONG_LONG_TYPE_SIZE);
6151 record_builtin_type (RID_MAX, "long long unsigned int",
6152 long_long_unsigned_type_node);
6153 record_builtin_type (RID_MAX, "long long unsigned",
6154 long_long_unsigned_type_node);
6155
5156628f
MS
6156 short_integer_type_node = make_signed_type (SHORT_TYPE_SIZE);
6157 record_builtin_type (RID_SHORT, "short int", short_integer_type_node);
6158 short_unsigned_type_node = make_unsigned_type (SHORT_TYPE_SIZE);
6159 record_builtin_type (RID_MAX, "short unsigned int", short_unsigned_type_node);
6160 record_builtin_type (RID_MAX, "unsigned short", short_unsigned_type_node);
6161
8d08fdba 6162 /* `unsigned long' is the standard type for sizeof.
8d08fdba 6163 Note that stddef.h uses `unsigned long',
f5426d1e
R
6164 and this must agree, even if long and int are the same size. */
6165 set_sizetype
6166 (TREE_TYPE (IDENTIFIER_GLOBAL_VALUE (get_identifier (SIZE_TYPE))));
8d08fdba
MS
6167
6168 ptrdiff_type_node
6169 = TREE_TYPE (IDENTIFIER_GLOBAL_VALUE (get_identifier (PTRDIFF_TYPE)));
6170
8d08fdba
MS
6171 /* Define both `signed char' and `unsigned char'. */
6172 signed_char_type_node = make_signed_type (CHAR_TYPE_SIZE);
6173 record_builtin_type (RID_MAX, "signed char", signed_char_type_node);
6174 unsigned_char_type_node = make_unsigned_type (CHAR_TYPE_SIZE);
6175 record_builtin_type (RID_MAX, "unsigned char", unsigned_char_type_node);
6176
6177 /* These are types that type_for_size and type_for_mode use. */
6178 intQI_type_node = make_signed_type (GET_MODE_BITSIZE (QImode));
6179 pushdecl (build_decl (TYPE_DECL, NULL_TREE, intQI_type_node));
6180 intHI_type_node = make_signed_type (GET_MODE_BITSIZE (HImode));
6181 pushdecl (build_decl (TYPE_DECL, NULL_TREE, intHI_type_node));
6182 intSI_type_node = make_signed_type (GET_MODE_BITSIZE (SImode));
6183 pushdecl (build_decl (TYPE_DECL, NULL_TREE, intSI_type_node));
6184 intDI_type_node = make_signed_type (GET_MODE_BITSIZE (DImode));
6185 pushdecl (build_decl (TYPE_DECL, NULL_TREE, intDI_type_node));
946dc1c8 6186#if HOST_BITS_PER_WIDE_INT >= 64
f7554e8c 6187 intTI_type_node = make_signed_type (GET_MODE_BITSIZE (TImode));
5ebcdddb 6188 pushdecl (build_decl (TYPE_DECL, get_identifier ("__int128_t"), intTI_type_node));
946dc1c8 6189#endif
8d08fdba
MS
6190 unsigned_intQI_type_node = make_unsigned_type (GET_MODE_BITSIZE (QImode));
6191 pushdecl (build_decl (TYPE_DECL, NULL_TREE, unsigned_intQI_type_node));
6192 unsigned_intHI_type_node = make_unsigned_type (GET_MODE_BITSIZE (HImode));
6193 pushdecl (build_decl (TYPE_DECL, NULL_TREE, unsigned_intHI_type_node));
6194 unsigned_intSI_type_node = make_unsigned_type (GET_MODE_BITSIZE (SImode));
6195 pushdecl (build_decl (TYPE_DECL, NULL_TREE, unsigned_intSI_type_node));
6196 unsigned_intDI_type_node = make_unsigned_type (GET_MODE_BITSIZE (DImode));
6197 pushdecl (build_decl (TYPE_DECL, NULL_TREE, unsigned_intDI_type_node));
946dc1c8 6198#if HOST_BITS_PER_WIDE_INT >= 64
f7554e8c 6199 unsigned_intTI_type_node = make_unsigned_type (GET_MODE_BITSIZE (TImode));
5ebcdddb 6200 pushdecl (build_decl (TYPE_DECL, get_identifier ("__uint128_t"), unsigned_intTI_type_node));
946dc1c8 6201#endif
8d08fdba
MS
6202
6203 float_type_node = make_node (REAL_TYPE);
6204 TYPE_PRECISION (float_type_node) = FLOAT_TYPE_SIZE;
6205 record_builtin_type (RID_FLOAT, NULL_PTR, float_type_node);
6206 layout_type (float_type_node);
6207
6208 double_type_node = make_node (REAL_TYPE);
6209 if (flag_short_double)
6210 TYPE_PRECISION (double_type_node) = FLOAT_TYPE_SIZE;
6211 else
6212 TYPE_PRECISION (double_type_node) = DOUBLE_TYPE_SIZE;
6213 record_builtin_type (RID_DOUBLE, NULL_PTR, double_type_node);
6214 layout_type (double_type_node);
6215
6216 long_double_type_node = make_node (REAL_TYPE);
6217 TYPE_PRECISION (long_double_type_node) = LONG_DOUBLE_TYPE_SIZE;
6218 record_builtin_type (RID_MAX, "long double", long_double_type_node);
6219 layout_type (long_double_type_node);
6220
37c46b43
MS
6221 complex_integer_type_node = make_node (COMPLEX_TYPE);
6222 pushdecl (build_decl (TYPE_DECL, get_identifier ("complex int"),
6223 complex_integer_type_node));
6224 TREE_TYPE (complex_integer_type_node) = integer_type_node;
6225 layout_type (complex_integer_type_node);
6226
6227 complex_float_type_node = make_node (COMPLEX_TYPE);
6228 pushdecl (build_decl (TYPE_DECL, get_identifier ("complex float"),
6229 complex_float_type_node));
6230 TREE_TYPE (complex_float_type_node) = float_type_node;
6231 layout_type (complex_float_type_node);
6232
6233 complex_double_type_node = make_node (COMPLEX_TYPE);
6234 pushdecl (build_decl (TYPE_DECL, get_identifier ("complex double"),
6235 complex_double_type_node));
6236 TREE_TYPE (complex_double_type_node) = double_type_node;
6237 layout_type (complex_double_type_node);
6238
6239 complex_long_double_type_node = make_node (COMPLEX_TYPE);
6240 pushdecl (build_decl (TYPE_DECL, get_identifier ("complex long double"),
6241 complex_long_double_type_node));
6242 TREE_TYPE (complex_long_double_type_node) = long_double_type_node;
6243 layout_type (complex_long_double_type_node);
6244
eff71ab0
PB
6245 java_byte_type_node = record_builtin_java_type ("__java_byte", 8);
6246 java_short_type_node = record_builtin_java_type ("__java_short", 16);
6247 java_int_type_node = record_builtin_java_type ("__java_int", 32);
6248 java_long_type_node = record_builtin_java_type ("__java_long", 64);
6249 java_float_type_node = record_builtin_java_type ("__java_float", -32);
6250 java_double_type_node = record_builtin_java_type ("__java_double", -64);
6251 java_char_type_node = record_builtin_java_type ("__java_char", -16);
6252 java_boolean_type_node = record_builtin_java_type ("__java_boolean", -1);
6253
8d08fdba
MS
6254 integer_zero_node = build_int_2 (0, 0);
6255 TREE_TYPE (integer_zero_node) = integer_type_node;
6256 integer_one_node = build_int_2 (1, 0);
6257 TREE_TYPE (integer_one_node) = integer_type_node;
6258 integer_two_node = build_int_2 (2, 0);
6259 TREE_TYPE (integer_two_node) = integer_type_node;
6260 integer_three_node = build_int_2 (3, 0);
6261 TREE_TYPE (integer_three_node) = integer_type_node;
8d08fdba 6262
255512c1
JM
6263 boolean_type_node = make_unsigned_type (BOOL_TYPE_SIZE);
6264 TREE_SET_CODE (boolean_type_node, BOOLEAN_TYPE);
c1ea6a0b
BK
6265 TYPE_MAX_VALUE (boolean_type_node) = build_int_2 (1, 0);
6266 TREE_TYPE (TYPE_MAX_VALUE (boolean_type_node)) = boolean_type_node;
6267 TYPE_PRECISION (boolean_type_node) = 1;
255512c1
JM
6268 record_builtin_type (RID_BOOL, "bool", boolean_type_node);
6269 boolean_false_node = build_int_2 (0, 0);
6270 TREE_TYPE (boolean_false_node) = boolean_type_node;
6271 boolean_true_node = build_int_2 (1, 0);
6272 TREE_TYPE (boolean_true_node) = boolean_type_node;
2986ae00 6273
8d08fdba
MS
6274 /* These are needed by stor-layout.c. */
6275 size_zero_node = size_int (0);
6276 size_one_node = size_int (1);
6277
e92cc029
MS
6278 signed_size_zero_node = build_int_2 (0, 0);
6279 TREE_TYPE (signed_size_zero_node) = make_signed_type (TYPE_PRECISION (sizetype));
6280
8d08fdba
MS
6281 void_type_node = make_node (VOID_TYPE);
6282 record_builtin_type (RID_VOID, NULL_PTR, void_type_node);
6283 layout_type (void_type_node); /* Uses integer_zero_node. */
6284 void_list_node = build_tree_list (NULL_TREE, void_type_node);
6285 TREE_PARMLIST (void_list_node) = 1;
6286
d11ad92e 6287 null_pointer_node = build_int_2 (0, 0);
8d08fdba 6288 TREE_TYPE (null_pointer_node) = build_pointer_type (void_type_node);
d11ad92e
MS
6289 layout_type (TREE_TYPE (null_pointer_node));
6290
8d08fdba
MS
6291 /* Used for expressions that do nothing, but are not errors. */
6292 void_zero_node = build_int_2 (0, 0);
6293 TREE_TYPE (void_zero_node) = void_type_node;
6294
6295 string_type_node = build_pointer_type (char_type_node);
beb53fb8 6296 const_string_type_node
91063b51
MM
6297 = build_pointer_type (build_qualified_type (char_type_node,
6298 TYPE_QUAL_CONST));
6b5fbb55 6299#if 0
8d08fdba 6300 record_builtin_type (RID_MAX, NULL_PTR, string_type_node);
6b5fbb55 6301#endif
8d08fdba
MS
6302
6303 /* Make a type to be the domain of a few array types
6304 whose domains don't really matter.
6305 200 is small enough that it always fits in size_t
6306 and large enough that it can hold most function names for the
6307 initializations of __FUNCTION__ and __PRETTY_FUNCTION__. */
6308 array_domain_type = build_index_type (build_int_2 (200, 0));
6309
2c73f9f5 6310 /* Make a type for arrays of characters.
8d08fdba
MS
6311 With luck nothing will ever really depend on the length of this
6312 array type. */
6313 char_array_type_node
6314 = build_array_type (char_type_node, array_domain_type);
6315 /* Likewise for arrays of ints. */
6316 int_array_type_node
6317 = build_array_type (integer_type_node, array_domain_type);
6318
6319 /* This is just some anonymous class type. Nobody should ever
6320 need to look inside this envelope. */
6321 class_star_type_node = build_pointer_type (make_lang_type (RECORD_TYPE));
6322
6323 default_function_type
6324 = build_function_type (integer_type_node, NULL_TREE);
8d08fdba
MS
6325
6326 ptr_type_node = build_pointer_type (void_type_node);
beb53fb8 6327 const_ptr_type_node
91063b51
MM
6328 = build_pointer_type (build_qualified_type (void_type_node,
6329 TYPE_QUAL_CONST));
6b5fbb55 6330#if 0
8d08fdba 6331 record_builtin_type (RID_MAX, NULL_PTR, ptr_type_node);
6b5fbb55 6332#endif
8d08fdba
MS
6333 endlink = void_list_node;
6334 int_endlink = tree_cons (NULL_TREE, integer_type_node, endlink);
6335 double_endlink = tree_cons (NULL_TREE, double_type_node, endlink);
de22184b
MS
6336 unsigned_endlink = tree_cons (NULL_TREE, unsigned_type_node, endlink);
6337
6338 ptr_ftype = build_function_type (ptr_type_node, NULL_TREE);
6339 ptr_ftype_unsigned = build_function_type (ptr_type_node, unsigned_endlink);
6340 sizetype_endlink = tree_cons (NULL_TREE, sizetype, endlink);
6341 /* We realloc here because sizetype could be int or unsigned. S'ok. */
6342 ptr_ftype_sizetype = build_function_type (ptr_type_node, sizetype_endlink);
6343
6344 void_ftype = build_function_type (void_type_node, endlink);
6345 void_ftype_int = build_function_type (void_type_node, int_endlink);
6346 void_ftype_ptr
6347 = build_function_type (void_type_node,
6348 tree_cons (NULL_TREE, ptr_type_node, endlink));
824b9a4c
MS
6349 void_ftype_ptr
6350 = build_exception_variant (void_ftype_ptr,
6351 tree_cons (NULL_TREE, NULL_TREE, NULL_TREE));
8d08fdba 6352
cffa8729
MS
6353 float_ftype_float
6354 = build_function_type (float_type_node,
6355 tree_cons (NULL_TREE, float_type_node, endlink));
6356
8d08fdba
MS
6357 double_ftype_double
6358 = build_function_type (double_type_node, double_endlink);
6359
cffa8729
MS
6360 ldouble_ftype_ldouble
6361 = build_function_type (long_double_type_node,
6362 tree_cons (NULL_TREE, long_double_type_node,
6363 endlink));
6364
8d08fdba
MS
6365 double_ftype_double_double
6366 = build_function_type (double_type_node,
a28e3c7f
MS
6367 tree_cons (NULL_TREE, double_type_node,
6368 double_endlink));
8d08fdba
MS
6369
6370 int_ftype_int
6371 = build_function_type (integer_type_node, int_endlink);
6372
6373 long_ftype_long
6374 = build_function_type (long_integer_type_node,
a28e3c7f
MS
6375 tree_cons (NULL_TREE, long_integer_type_node,
6376 endlink));
8d08fdba 6377
8d08fdba
MS
6378 int_ftype_cptr_cptr_sizet
6379 = build_function_type (integer_type_node,
6380 tree_cons (NULL_TREE, const_ptr_type_node,
6381 tree_cons (NULL_TREE, const_ptr_type_node,
6382 tree_cons (NULL_TREE,
6383 sizetype,
6384 endlink))));
6385
8d08fdba
MS
6386 string_ftype_ptr_ptr /* strcpy prototype */
6387 = build_function_type (string_type_node,
6388 tree_cons (NULL_TREE, string_type_node,
6389 tree_cons (NULL_TREE,
6390 const_string_type_node,
6391 endlink)));
6392
8d08fdba
MS
6393 int_ftype_string_string /* strcmp prototype */
6394 = build_function_type (integer_type_node,
6395 tree_cons (NULL_TREE, const_string_type_node,
6396 tree_cons (NULL_TREE,
6397 const_string_type_node,
6398 endlink)));
6399
cffa8729 6400 strlen_ftype /* strlen prototype */
8d08fdba
MS
6401 = build_function_type (sizetype,
6402 tree_cons (NULL_TREE, const_string_type_node,
6403 endlink));
6404
8d08fdba 6405 memcpy_ftype /* memcpy prototype */
d22c8596 6406 = build_function_type (ptr_type_node,
8d08fdba
MS
6407 tree_cons (NULL_TREE, ptr_type_node,
6408 tree_cons (NULL_TREE, const_ptr_type_node,
de22184b 6409 sizetype_endlink)));
8d08fdba
MS
6410
6411 if (flag_huge_objects)
6412 delta_type_node = long_integer_type_node;
6413 else
6414 delta_type_node = short_integer_type_node;
6415
b9620d0e 6416 builtin_function ("__builtin_constant_p", default_function_type,
8d08fdba
MS
6417 BUILT_IN_CONSTANT_P, NULL_PTR);
6418
beb53fb8
JM
6419 builtin_return_address_fndecl
6420 = builtin_function ("__builtin_return_address", ptr_ftype_unsigned,
6421 BUILT_IN_RETURN_ADDRESS, NULL_PTR);
8926095f 6422
de22184b 6423 builtin_function ("__builtin_frame_address", ptr_ftype_unsigned,
8926095f
MS
6424 BUILT_IN_FRAME_ADDRESS, NULL_PTR);
6425
de22184b 6426 builtin_function ("__builtin_alloca", ptr_ftype_sizetype,
8d08fdba 6427 BUILT_IN_ALLOCA, "alloca");
cffa8729 6428 builtin_function ("__builtin_ffs", int_ftype_int, BUILT_IN_FFS, NULL_PTR);
00595019
MS
6429 /* Define alloca, ffs as builtins.
6430 Declare _exit just to mark it as volatile. */
6431 if (! flag_no_builtin && !flag_no_nonansi_builtin)
6432 {
de22184b 6433 temp = builtin_function ("alloca", ptr_ftype_sizetype,
00595019
MS
6434 BUILT_IN_ALLOCA, NULL_PTR);
6435 /* Suppress error if redefined as a non-function. */
6436 DECL_BUILT_IN_NONANSI (temp) = 1;
00595019
MS
6437 temp = builtin_function ("ffs", int_ftype_int, BUILT_IN_FFS, NULL_PTR);
6438 /* Suppress error if redefined as a non-function. */
6439 DECL_BUILT_IN_NONANSI (temp) = 1;
de22184b 6440 temp = builtin_function ("_exit", void_ftype_int,
00595019
MS
6441 NOT_BUILT_IN, NULL_PTR);
6442 TREE_THIS_VOLATILE (temp) = 1;
6443 TREE_SIDE_EFFECTS (temp) = 1;
6444 /* Suppress error if redefined as a non-function. */
6445 DECL_BUILT_IN_NONANSI (temp) = 1;
6446 }
8d08fdba 6447
cffa8729
MS
6448 builtin_function ("__builtin_abs", int_ftype_int, BUILT_IN_ABS, NULL_PTR);
6449 builtin_function ("__builtin_fabsf", float_ftype_float, BUILT_IN_FABS,
6450 NULL_PTR);
6451 builtin_function ("__builtin_fabs", double_ftype_double, BUILT_IN_FABS,
6452 NULL_PTR);
6453 builtin_function ("__builtin_fabsl", ldouble_ftype_ldouble, BUILT_IN_FABS,
6454 NULL_PTR);
8d08fdba
MS
6455 builtin_function ("__builtin_labs", long_ftype_long,
6456 BUILT_IN_LABS, NULL_PTR);
de22184b 6457 builtin_function ("__builtin_saveregs", ptr_ftype,
8d08fdba 6458 BUILT_IN_SAVEREGS, NULL_PTR);
8d08fdba
MS
6459 builtin_function ("__builtin_classify_type", default_function_type,
6460 BUILT_IN_CLASSIFY_TYPE, NULL_PTR);
de22184b 6461 builtin_function ("__builtin_next_arg", ptr_ftype,
8d08fdba 6462 BUILT_IN_NEXT_ARG, NULL_PTR);
de22184b 6463 builtin_function ("__builtin_args_info", int_ftype_int,
8d08fdba 6464 BUILT_IN_ARGS_INFO, NULL_PTR);
eb66be0e
MS
6465 builtin_function ("__builtin_setjmp",
6466 build_function_type (integer_type_node,
6467 tree_cons (NULL_TREE, ptr_type_node,
6468 endlink)),
6469 BUILT_IN_SETJMP, NULL_PTR);
6470 builtin_function ("__builtin_longjmp",
6471 build_function_type (integer_type_node,
6472 tree_cons (NULL_TREE, ptr_type_node,
6473 tree_cons (NULL_TREE,
6474 integer_type_node,
6475 endlink))),
6476 BUILT_IN_LONGJMP, NULL_PTR);
8d08fdba
MS
6477
6478 /* Untyped call and return. */
de22184b 6479 builtin_function ("__builtin_apply_args", ptr_ftype,
8d08fdba
MS
6480 BUILT_IN_APPLY_ARGS, NULL_PTR);
6481
6482 temp = tree_cons (NULL_TREE,
6483 build_pointer_type (build_function_type (void_type_node,
6484 NULL_TREE)),
2c73f9f5 6485 tree_cons (NULL_TREE, ptr_ftype_sizetype, NULL_TREE));
8d08fdba
MS
6486 builtin_function ("__builtin_apply",
6487 build_function_type (ptr_type_node, temp),
6488 BUILT_IN_APPLY, NULL_PTR);
de22184b 6489 builtin_function ("__builtin_return", void_ftype_ptr,
8d08fdba
MS
6490 BUILT_IN_RETURN, NULL_PTR);
6491
6492 /* Currently under experimentation. */
6493 builtin_function ("__builtin_memcpy", memcpy_ftype,
6494 BUILT_IN_MEMCPY, "memcpy");
6495 builtin_function ("__builtin_memcmp", int_ftype_cptr_cptr_sizet,
6496 BUILT_IN_MEMCMP, "memcmp");
6497 builtin_function ("__builtin_strcmp", int_ftype_string_string,
6498 BUILT_IN_STRCMP, "strcmp");
6499 builtin_function ("__builtin_strcpy", string_ftype_ptr_ptr,
6500 BUILT_IN_STRCPY, "strcpy");
cffa8729 6501 builtin_function ("__builtin_strlen", strlen_ftype,
8d08fdba 6502 BUILT_IN_STRLEN, "strlen");
cffa8729
MS
6503 builtin_function ("__builtin_sqrtf", float_ftype_float,
6504 BUILT_IN_FSQRT, "sqrtf");
6505 builtin_function ("__builtin_fsqrt", double_ftype_double,
6506 BUILT_IN_FSQRT, NULL_PTR);
6507 builtin_function ("__builtin_sqrtl", ldouble_ftype_ldouble,
6508 BUILT_IN_FSQRT, "sqrtl");
6509 builtin_function ("__builtin_sinf", float_ftype_float,
6510 BUILT_IN_SIN, "sinf");
6511 builtin_function ("__builtin_sin", double_ftype_double,
6512 BUILT_IN_SIN, "sin");
6513 builtin_function ("__builtin_sinl", ldouble_ftype_ldouble,
6514 BUILT_IN_SIN, "sinl");
6515 builtin_function ("__builtin_cosf", float_ftype_float,
6516 BUILT_IN_COS, "cosf");
6517 builtin_function ("__builtin_cos", double_ftype_double,
6518 BUILT_IN_COS, "cos");
6519 builtin_function ("__builtin_cosl", ldouble_ftype_ldouble,
6520 BUILT_IN_COS, "cosl");
8d08fdba
MS
6521
6522 if (!flag_no_builtin)
6523 {
8d08fdba
MS
6524 builtin_function ("abs", int_ftype_int, BUILT_IN_ABS, NULL_PTR);
6525 builtin_function ("fabs", double_ftype_double, BUILT_IN_FABS, NULL_PTR);
6526 builtin_function ("labs", long_ftype_long, BUILT_IN_LABS, NULL_PTR);
cffa8729
MS
6527 builtin_function ("fabsf", float_ftype_float, BUILT_IN_FABS, NULL_PTR);
6528 builtin_function ("fabsl", ldouble_ftype_ldouble, BUILT_IN_FABS,
6529 NULL_PTR);
8d08fdba
MS
6530 builtin_function ("memcpy", memcpy_ftype, BUILT_IN_MEMCPY, NULL_PTR);
6531 builtin_function ("memcmp", int_ftype_cptr_cptr_sizet, BUILT_IN_MEMCMP,
6532 NULL_PTR);
cffa8729
MS
6533 builtin_function ("strcmp", int_ftype_string_string, BUILT_IN_STRCMP,
6534 NULL_PTR);
a28e3c7f
MS
6535 builtin_function ("strcpy", string_ftype_ptr_ptr, BUILT_IN_STRCPY,
6536 NULL_PTR);
cffa8729
MS
6537 builtin_function ("strlen", strlen_ftype, BUILT_IN_STRLEN, NULL_PTR);
6538 builtin_function ("sqrtf", float_ftype_float, BUILT_IN_FSQRT, NULL_PTR);
6539 builtin_function ("sqrt", double_ftype_double, BUILT_IN_FSQRT, NULL_PTR);
6540 builtin_function ("sqrtl", ldouble_ftype_ldouble, BUILT_IN_FSQRT,
6541 NULL_PTR);
6542 builtin_function ("sinf", float_ftype_float, BUILT_IN_SIN, NULL_PTR);
8d08fdba 6543 builtin_function ("sin", double_ftype_double, BUILT_IN_SIN, NULL_PTR);
cffa8729
MS
6544 builtin_function ("sinl", ldouble_ftype_ldouble, BUILT_IN_SIN, NULL_PTR);
6545 builtin_function ("cosf", float_ftype_float, BUILT_IN_COS, NULL_PTR);
8d08fdba 6546 builtin_function ("cos", double_ftype_double, BUILT_IN_COS, NULL_PTR);
cffa8729 6547 builtin_function ("cosl", ldouble_ftype_ldouble, BUILT_IN_COS, NULL_PTR);
00595019
MS
6548
6549 /* Declare these functions volatile
6550 to avoid spurious "control drops through" warnings. */
de22184b 6551 temp = builtin_function ("abort", void_ftype,
00595019
MS
6552 NOT_BUILT_IN, NULL_PTR);
6553 TREE_THIS_VOLATILE (temp) = 1;
6554 TREE_SIDE_EFFECTS (temp) = 1;
6555 /* Well, these are actually ANSI, but we can't set DECL_BUILT_IN on
6556 them... */
6557 DECL_BUILT_IN_NONANSI (temp) = 1;
de22184b 6558 temp = builtin_function ("exit", void_ftype_int,
00595019
MS
6559 NOT_BUILT_IN, NULL_PTR);
6560 TREE_THIS_VOLATILE (temp) = 1;
6561 TREE_SIDE_EFFECTS (temp) = 1;
6562 DECL_BUILT_IN_NONANSI (temp) = 1;
8d08fdba
MS
6563 }
6564
6565#if 0
6566 /* Support for these has not been written in either expand_builtin
6567 or build_function_call. */
72b7eeff
MS
6568 builtin_function ("__builtin_div", default_ftype, BUILT_IN_DIV, NULL_PTR);
6569 builtin_function ("__builtin_ldiv", default_ftype, BUILT_IN_LDIV, NULL_PTR);
a28e3c7f 6570 builtin_function ("__builtin_ffloor", double_ftype_double, BUILT_IN_FFLOOR,
72b7eeff 6571 NULL_PTR);
cffa8729
MS
6572 builtin_function ("__builtin_fceil", double_ftype_double, BUILT_IN_FCEIL,
6573 NULL_PTR);
a28e3c7f 6574 builtin_function ("__builtin_fmod", double_ftype_double_double,
72b7eeff 6575 BUILT_IN_FMOD, NULL_PTR);
a28e3c7f 6576 builtin_function ("__builtin_frem", double_ftype_double_double,
72b7eeff 6577 BUILT_IN_FREM, NULL_PTR);
cffa8729
MS
6578 builtin_function ("__builtin_memset", ptr_ftype_ptr_int_int,
6579 BUILT_IN_MEMSET, NULL_PTR);
a28e3c7f 6580 builtin_function ("__builtin_getexp", double_ftype_double, BUILT_IN_GETEXP,
72b7eeff 6581 NULL_PTR);
a28e3c7f 6582 builtin_function ("__builtin_getman", double_ftype_double, BUILT_IN_GETMAN,
72b7eeff 6583 NULL_PTR);
8d08fdba
MS
6584#endif
6585
6586 /* C++ extensions */
6587
6588 unknown_type_node = make_node (UNKNOWN_TYPE);
036407f7
ML
6589 record_unknown_type (unknown_type_node, "unknown type");
6590
8d08fdba
MS
6591 /* Indirecting an UNKNOWN_TYPE node yields an UNKNOWN_TYPE node. */
6592 TREE_TYPE (unknown_type_node) = unknown_type_node;
a6967cc0 6593
03d0f4af 6594 TREE_TYPE (null_node) = type_for_size (POINTER_SIZE, 0);
a6967cc0
JM
6595
6596 /* Looking up TYPE_POINTER_TO and TYPE_REFERENCE_TO yield the same
6597 result. */
8d08fdba
MS
6598 TYPE_POINTER_TO (unknown_type_node) = unknown_type_node;
6599 TYPE_REFERENCE_TO (unknown_type_node) = unknown_type_node;
6600
6601 /* This is for handling opaque types in signatures. */
6602 opaque_type_node = copy_node (ptr_type_node);
6603 TYPE_MAIN_VARIANT (opaque_type_node) = opaque_type_node;
6604 record_builtin_type (RID_MAX, 0, opaque_type_node);
6605
e92cc029 6606 /* This is special for C++ so functions can be overloaded. */
8d08fdba
MS
6607 wchar_type_node
6608 = TREE_TYPE (IDENTIFIER_GLOBAL_VALUE (get_identifier (WCHAR_TYPE)));
6609 wchar_type_size = TYPE_PRECISION (wchar_type_node);
6610 signed_wchar_type_node = make_signed_type (wchar_type_size);
6611 unsigned_wchar_type_node = make_unsigned_type (wchar_type_size);
6612 wchar_type_node
6613 = TREE_UNSIGNED (wchar_type_node)
6614 ? unsigned_wchar_type_node
6615 : signed_wchar_type_node;
6616 record_builtin_type (RID_WCHAR, "__wchar_t", wchar_type_node);
6617
f376e137
MS
6618 /* Artificial declaration of wchar_t -- can be bashed */
6619 wchar_decl_node = build_decl (TYPE_DECL, get_identifier ("wchar_t"),
6620 wchar_type_node);
6621 pushdecl (wchar_decl_node);
6622
8d08fdba
MS
6623 /* This is for wide string constants. */
6624 wchar_array_type_node
6625 = build_array_type (wchar_type_node, array_domain_type);
6626
8926095f 6627 if (flag_vtable_thunks)
700f8a87
MS
6628 {
6629 /* Make sure we get a unique function type, so we can give
6630 its pointer type a name. (This wins for gdb.) */
6631 tree vfunc_type = make_node (FUNCTION_TYPE);
6632 TREE_TYPE (vfunc_type) = integer_type_node;
6633 TYPE_ARG_TYPES (vfunc_type) = NULL_TREE;
6634 layout_type (vfunc_type);
6635
6636 vtable_entry_type = build_pointer_type (vfunc_type);
6637 }
8926095f 6638 else
700f8a87
MS
6639 {
6640 vtable_entry_type = make_lang_type (RECORD_TYPE);
6641 fields[0] = build_lang_field_decl (FIELD_DECL, delta_identifier,
6642 delta_type_node);
6643 fields[1] = build_lang_field_decl (FIELD_DECL, index_identifier,
6644 delta_type_node);
6645 fields[2] = build_lang_field_decl (FIELD_DECL, pfn_identifier,
6646 ptr_type_node);
6647 finish_builtin_type (vtable_entry_type, VTBL_PTR_TYPE, fields, 2,
6648 double_type_node);
6649
6650 /* Make this part of an invisible union. */
6651 fields[3] = copy_node (fields[2]);
6652 TREE_TYPE (fields[3]) = delta_type_node;
6653 DECL_NAME (fields[3]) = delta2_identifier;
6654 DECL_MODE (fields[3]) = TYPE_MODE (delta_type_node);
6655 DECL_SIZE (fields[3]) = TYPE_SIZE (delta_type_node);
6656 TREE_UNSIGNED (fields[3]) = 0;
6657 TREE_CHAIN (fields[2]) = fields[3];
91063b51
MM
6658 vtable_entry_type = build_qualified_type (vtable_entry_type,
6659 TYPE_QUAL_CONST);
700f8a87
MS
6660 }
6661 record_builtin_type (RID_MAX, VTBL_PTR_TYPE, vtable_entry_type);
8d08fdba 6662
8d08fdba 6663 vtbl_type_node
52bf7d5d 6664 = build_cplus_array_type (vtable_entry_type, NULL_TREE);
8d08fdba 6665 layout_type (vtbl_type_node);
91063b51 6666 vtbl_type_node = build_qualified_type (vtbl_type_node, TYPE_QUAL_CONST);
8d08fdba 6667 record_builtin_type (RID_MAX, NULL_PTR, vtbl_type_node);
849da744
MM
6668 vtbl_ptr_type_node = build_pointer_type (vtable_entry_type);
6669 layout_type (vtbl_ptr_type_node);
6670 record_builtin_type (RID_MAX, NULL_PTR, vtbl_ptr_type_node);
8d08fdba
MS
6671
6672 /* Simplify life by making a "sigtable_entry_type". Give its
6673 fields names so that the debugger can use them. */
6674
6675 if (flag_handle_signatures)
6676 {
6677 sigtable_entry_type = make_lang_type (RECORD_TYPE);
dff6b454
RK
6678 fields[0] = build_lang_field_decl (FIELD_DECL, tag_identifier,
6679 delta_type_node);
9dd70aa4 6680 fields[1] = build_lang_field_decl (FIELD_DECL, vb_off_identifier,
dff6b454 6681 delta_type_node);
9dd70aa4 6682 fields[2] = build_lang_field_decl (FIELD_DECL, delta_identifier,
dff6b454
RK
6683 delta_type_node);
6684 fields[3] = build_lang_field_decl (FIELD_DECL, index_identifier,
6685 delta_type_node);
9dd70aa4
GB
6686 fields[4] = build_lang_field_decl (FIELD_DECL, pfn_identifier,
6687 ptr_type_node);
6688
6689 /* Set the alignment to the max of the alignment of ptr_type_node and
6690 delta_type_node. Double alignment wastes a word on the Sparc. */
6691 finish_builtin_type (sigtable_entry_type, SIGTABLE_PTR_TYPE, fields, 4,
6692 (TYPE_ALIGN (ptr_type_node) > TYPE_ALIGN (delta_type_node))
6693 ? ptr_type_node
6694 : delta_type_node);
dff6b454
RK
6695
6696 /* Make this part of an invisible union. */
9dd70aa4
GB
6697 fields[5] = copy_node (fields[4]);
6698 TREE_TYPE (fields[5]) = delta_type_node;
6699 DECL_NAME (fields[5]) = vt_off_identifier;
6700 DECL_MODE (fields[5]) = TYPE_MODE (delta_type_node);
6701 DECL_SIZE (fields[5]) = TYPE_SIZE (delta_type_node);
6702 TREE_UNSIGNED (fields[5]) = 0;
6703 TREE_CHAIN (fields[4]) = fields[5];
dff6b454 6704
91063b51
MM
6705 sigtable_entry_type = build_qualified_type (sigtable_entry_type,
6706 TYPE_QUAL_CONST);
8d08fdba
MS
6707 record_builtin_type (RID_MAX, SIGTABLE_PTR_TYPE, sigtable_entry_type);
6708 }
6709
2c73f9f5
ML
6710 std_node = build_decl (NAMESPACE_DECL,
6711 get_identifier (flag_honor_std ? "fake std":"std"),
6633d636
MS
6712 void_type_node);
6713 pushdecl (std_node);
6714
2c73f9f5 6715 global_type_node = make_node (LANG_TYPE);
036407f7 6716 record_unknown_type (global_type_node, "global type");
2c73f9f5 6717
db5ae43f
MS
6718 /* Now, C++. */
6719 current_lang_name = lang_name_cplusplus;
8d08fdba 6720
ced78d8b 6721 {
2c73f9f5
ML
6722 tree bad_alloc_type_node, newtype, deltype;
6723 if (flag_honor_std)
6724 push_namespace (get_identifier ("std"));
6725 bad_alloc_type_node = xref_tag
ca107ded 6726 (class_type_node, get_identifier ("bad_alloc"), 1);
2c73f9f5
ML
6727 if (flag_honor_std)
6728 pop_namespace ();
6729 newtype = build_exception_variant
ced78d8b 6730 (ptr_ftype_sizetype, build_tree_list (NULL_TREE, bad_alloc_type_node));
2c73f9f5 6731 deltype = build_exception_variant
ced78d8b
JM
6732 (void_ftype_ptr, build_tree_list (NULL_TREE, NULL_TREE));
6733 auto_function (ansi_opname[(int) NEW_EXPR], newtype, NOT_BUILT_IN);
6734 auto_function (ansi_opname[(int) VEC_NEW_EXPR], newtype, NOT_BUILT_IN);
94e098d1
MM
6735 global_delete_fndecl
6736 = auto_function (ansi_opname[(int) DELETE_EXPR], deltype, NOT_BUILT_IN);
ced78d8b
JM
6737 auto_function (ansi_opname[(int) VEC_DELETE_EXPR], deltype, NOT_BUILT_IN);
6738 }
8d08fdba
MS
6739
6740 abort_fndecl
de22184b 6741 = define_function ("__pure_virtual", void_ftype,
8d08fdba
MS
6742 NOT_BUILT_IN, 0, 0);
6743
8d08fdba
MS
6744 /* Perform other language dependent initializations. */
6745 init_class_processing ();
6746 init_init_processing ();
6747 init_search_processing ();
1737fe20
BK
6748 if (flag_rtti)
6749 init_rtti_processing ();
8d08fdba 6750
6467930b 6751 if (flag_exceptions)
8d2733ca 6752 init_exception_processing ();
8d08fdba
MS
6753 if (flag_no_inline)
6754 {
6755 flag_inline_functions = 0;
8d08fdba 6756 }
9e9ff709 6757
7fcdf4c2 6758 if (! supports_one_only ())
72b7eeff 6759 flag_weak = 0;
8d08fdba
MS
6760
6761 /* Create the global bindings for __FUNCTION__ and __PRETTY_FUNCTION__. */
6762 declare_function_name ();
6763
6764 /* Prepare to check format strings against argument lists. */
6765 init_function_format_info ();
e9a25f70
JL
6766
6767 /* Show we use EH for cleanups. */
6768 using_eh_for_cleanups ();
62c154ed
JM
6769
6770 print_error_function = lang_print_error_function;
501ba25a 6771 lang_get_alias_set = &c_get_alias_set;
e5dc5fb2 6772 valid_lang_attribute = cp_valid_lang_attribute;
d9cf7c82
JM
6773
6774 /* Maintain consistency. Perhaps we should just complain if they
6775 say -fwritable-strings? */
6776 if (flag_writable_strings)
6777 flag_const_strings = 0;
62c154ed
JM
6778}
6779
6780/* Function to print any language-specific context for an error message. */
6781
6782static void
6783lang_print_error_function (file)
6784 char *file;
6785{
6786 default_print_error_function (file);
6787 maybe_print_template_context ();
8d08fdba
MS
6788}
6789
6790/* Make a definition for a builtin function named NAME and whose data type
6791 is TYPE. TYPE should be a function type with argument types.
6792 FUNCTION_CODE tells later passes how to compile calls to this function.
6793 See tree.h for its possible values.
6794
6795 If LIBRARY_NAME is nonzero, use that for DECL_ASSEMBLER_NAME,
6796 the name to be called if we can't opencode the function. */
6797
6798tree
6799define_function (name, type, function_code, pfn, library_name)
d8e178a0 6800 const char *name;
8d08fdba
MS
6801 tree type;
6802 enum built_in_function function_code;
49c249e1 6803 void (*pfn) PROTO((tree));
d8e178a0 6804 const char *library_name;
8d08fdba
MS
6805{
6806 tree decl = build_lang_decl (FUNCTION_DECL, get_identifier (name), type);
6807 DECL_EXTERNAL (decl) = 1;
6808 TREE_PUBLIC (decl) = 1;
863adfc0 6809 DECL_ARTIFICIAL (decl) = 1;
8d08fdba 6810
2c73f9f5 6811 my_friendly_assert (DECL_CONTEXT (decl) == NULL_TREE, 392);
cb0dbb9a 6812 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
2c73f9f5 6813
8d08fdba
MS
6814 /* Since `pushdecl' relies on DECL_ASSEMBLER_NAME instead of DECL_NAME,
6815 we cannot change DECL_ASSEMBLER_NAME until we have installed this
6816 function in the namespace. */
6817 if (pfn) (*pfn) (decl);
6818 if (library_name)
6819 DECL_ASSEMBLER_NAME (decl) = get_identifier (library_name);
6820 make_function_rtl (decl);
6821 if (function_code != NOT_BUILT_IN)
6822 {
6823 DECL_BUILT_IN (decl) = 1;
39211cd5 6824 DECL_FUNCTION_CODE (decl) = function_code;
8d08fdba
MS
6825 }
6826 return decl;
6827}
6828\f
61a127b3
MM
6829/* When we call finish_struct for an anonymous union, we create
6830 default copy constructors and such. But, an anonymous union
6831 shouldn't have such things; this function undoes the damage to the
6832 anonymous union type T.
6833
6834 (The reason that we create the synthesized methods is that we don't
6835 distinguish `union { int i; }' from `typedef union { int i; } U'.
6836 The first is an anonymous union; the second is just an ordinary
6837 union type.) */
6838
6839void
6840fixup_anonymous_union (t)
6841 tree t;
6842{
6843 tree *q;
6844
6845 /* Wipe out memory of synthesized methods */
6846 TYPE_HAS_CONSTRUCTOR (t) = 0;
6847 TYPE_HAS_DEFAULT_CONSTRUCTOR (t) = 0;
6848 TYPE_HAS_INIT_REF (t) = 0;
6849 TYPE_HAS_CONST_INIT_REF (t) = 0;
6850 TYPE_HAS_ASSIGN_REF (t) = 0;
61a127b3
MM
6851 TYPE_HAS_CONST_ASSIGN_REF (t) = 0;
6852
6853 /* Splice the implicitly generated functions out of the TYPE_METHODS
6854 list. */
6855 q = &TYPE_METHODS (t);
6856 while (*q)
6857 {
6858 if (DECL_ARTIFICIAL (*q))
6859 *q = TREE_CHAIN (*q);
6860 else
6861 q = &TREE_CHAIN (*q);
6862 }
6863
6864 /* ANSI C++ June 5 1992 WP 9.5.3. Anonymous unions may not have
6865 function members. */
6866 if (TYPE_METHODS (t))
6867 error ("an anonymous union cannot have function members");
6868}
6869
72a93143
JM
6870/* Make sure that a declaration with no declarator is well-formed, i.e.
6871 just defines a tagged type or anonymous union.
8d08fdba 6872
72a93143 6873 Returns the type defined, if any. */
8d08fdba 6874
72a93143
JM
6875tree
6876check_tag_decl (declspecs)
8d08fdba
MS
6877 tree declspecs;
6878{
72a93143 6879 int found_type = 0;
2986ae00 6880 tree ob_modifier = NULL_TREE;
8d08fdba 6881 register tree link;
8d08fdba
MS
6882 register tree t = NULL_TREE;
6883
6884 for (link = declspecs; link; link = TREE_CHAIN (link))
6885 {
6886 register tree value = TREE_VALUE (link);
6887
72a93143 6888 if (TYPE_P (value))
8d08fdba 6889 {
72a93143 6890 ++found_type;
5566b478 6891
72a93143
JM
6892 if (IS_AGGR_TYPE (value) || TREE_CODE (value) == ENUMERAL_TYPE)
6893 {
6894 my_friendly_assert (TYPE_MAIN_DECL (value) != NULL_TREE, 261);
6895 t = value;
6896 }
8d08fdba 6897 }
83f660b7
JM
6898 else if (value == ridpointers[(int) RID_FRIEND])
6899 {
83f660b7
JM
6900 if (current_class_type == NULL_TREE
6901 || current_scope () != current_class_type)
6902 ob_modifier = value;
6903 }
8d08fdba 6904 else if (value == ridpointers[(int) RID_STATIC]
2986ae00
MS
6905 || value == ridpointers[(int) RID_EXTERN]
6906 || value == ridpointers[(int) RID_AUTO]
28cbf42c
MS
6907 || value == ridpointers[(int) RID_REGISTER]
6908 || value == ridpointers[(int) RID_INLINE]
6909 || value == ridpointers[(int) RID_VIRTUAL]
72a93143
JM
6910 || value == ridpointers[(int) RID_CONST]
6911 || value == ridpointers[(int) RID_VOLATILE]
28cbf42c 6912 || value == ridpointers[(int) RID_EXPLICIT])
2986ae00 6913 ob_modifier = value;
8d08fdba
MS
6914 }
6915
72a93143
JM
6916 if (found_type > 1)
6917 error ("multiple types in one declaration");
7e2067ca
JM
6918
6919 /* Inside a class, we might be in a friend or access declaration.
6920 Until we have a good way of detecting the latter, don't warn. */
6921 if (t == NULL_TREE && ! current_class_type)
6922 pedwarn ("declaration does not declare anything");
0dd3962d
JM
6923
6924 /* Check for an anonymous union. We're careful
6925 accessing TYPE_IDENTIFIER because some built-in types, like
6926 pointer-to-member types, do not have TYPE_NAME. */
6927 else if (t && TREE_CODE (t) == UNION_TYPE
6928 && TYPE_NAME (t)
6929 && ANON_AGGRNAME_P (TYPE_IDENTIFIER (t)))
6930 {
6931 /* Anonymous unions are objects, so they can have specifiers. */;
6932 SET_ANON_UNION_TYPE_P (t);
6933 }
6934
83f660b7 6935 else if (ob_modifier)
8d08fdba 6936 {
83f660b7
JM
6937 if (ob_modifier == ridpointers[(int) RID_INLINE]
6938 || ob_modifier == ridpointers[(int) RID_VIRTUAL])
6939 cp_error ("`%D' can only be specified for functions", ob_modifier);
6940 else if (ob_modifier == ridpointers[(int) RID_FRIEND])
6941 cp_error ("`%D' can only be specified inside a class", ob_modifier);
6942 else if (ob_modifier == ridpointers[(int) RID_EXPLICIT])
6943 cp_error ("`%D' can only be specified for constructors",
6944 ob_modifier);
6945 else
6946 cp_error ("`%D' can only be specified for objects and functions",
6947 ob_modifier);
72a93143 6948 }
8d08fdba 6949
72a93143
JM
6950 return t;
6951}
6952
6953/* Called when a declaration is seen that contains no names to declare.
6954 If its type is a reference to a structure, union or enum inherited
6955 from a containing scope, shadow that tag name for the current scope
6956 with a forward reference.
6957 If its type defines a new named structure or union
6958 or defines an enum, it is valid but we need not do anything here.
6959 Otherwise, it is an error.
6960
6961 C++: may have to grok the declspecs to learn about static,
6962 complain for anonymous unions. */
6963
6964void
6965shadow_tag (declspecs)
6966 tree declspecs;
6967{
6968 tree t = check_tag_decl (declspecs);
6969
6970 if (t)
6971 maybe_process_partial_specialization (t);
6972
6973 /* This is where the variables in an anonymous union are
6974 declared. An anonymous union declaration looks like:
6975 union { ... } ;
6976 because there is no declarator after the union, the parser
6977 sends that declaration here. */
6978 if (t && ANON_UNION_TYPE_P (t))
6979 {
6980 fixup_anonymous_union (t);
6981
6982 if (TYPE_FIELDS (t))
6983 {
6984 tree decl = grokdeclarator (NULL_TREE, declspecs, NORMAL, 0,
6985 NULL_TREE);
6986 finish_anon_union (decl);
6987 }
8d08fdba
MS
6988 }
6989}
6990\f
6991/* Decode a "typename", such as "int **", returning a ..._TYPE node. */
6992
6993tree
6994groktypename (typename)
6995 tree typename;
6996{
6997 if (TREE_CODE (typename) != TREE_LIST)
6998 return typename;
6999 return grokdeclarator (TREE_VALUE (typename),
7000 TREE_PURPOSE (typename),
c11b6f21 7001 TYPENAME, 0, NULL_TREE);
8d08fdba
MS
7002}
7003
7004/* Decode a declarator in an ordinary declaration or data definition.
7005 This is called as soon as the type information and variable name
7006 have been parsed, before parsing the initializer if any.
7007 Here we create the ..._DECL node, fill in its type,
7008 and put it on the list of decls for the current context.
7009 The ..._DECL node is returned as the value.
7010
7011 Exception: for arrays where the length is not specified,
82580166 7012 the type is left null, to be filled in by `cp_finish_decl'.
8d08fdba
MS
7013
7014 Function definitions do not come here; they go to start_function
7015 instead. However, external and forward declarations of functions
7016 do go through here. Structure field declarations are done by
7017 grokfield and not through here. */
7018
7019/* Set this to zero to debug not using the temporary obstack
7020 to parse initializers. */
7021int debug_temp_inits = 1;
7022
7023tree
a1774733 7024start_decl (declarator, declspecs, initialized, attributes, prefix_attributes)
8d08fdba
MS
7025 tree declarator, declspecs;
7026 int initialized;
a1774733 7027 tree attributes, prefix_attributes;
8d08fdba
MS
7028{
7029 register tree decl;
7030 register tree type, tem;
7031 tree context;
7032 extern int have_extern_spec;
7033 extern int used_extern_spec;
b17e2870 7034 tree attrlist;
8d08fdba 7035
5566b478
MS
7036#if 0
7037 /* See code below that used this. */
8d08fdba 7038 int init_written = initialized;
5566b478 7039#endif
8d08fdba 7040
e92cc029 7041 /* This should only be done once on the top most decl. */
8d08fdba
MS
7042 if (have_extern_spec && !used_extern_spec)
7043 {
a28e3c7f
MS
7044 declspecs = decl_tree_cons (NULL_TREE, get_identifier ("extern"),
7045 declspecs);
8d08fdba
MS
7046 used_extern_spec = 1;
7047 }
7048
b17e2870
JM
7049 if (attributes || prefix_attributes)
7050 attrlist = build_scratch_list (attributes, prefix_attributes);
7051 else
7052 attrlist = NULL_TREE;
7053
c11b6f21 7054 decl = grokdeclarator (declarator, declspecs, NORMAL, initialized,
b17e2870
JM
7055 attrlist);
7056
a1774733 7057 if (decl == NULL_TREE || TREE_CODE (decl) == VOID_TYPE)
8d08fdba
MS
7058 return NULL_TREE;
7059
7060 type = TREE_TYPE (decl);
7061
44689c12
ML
7062 if (type == error_mark_node)
7063 return NULL_TREE;
7064
8d08fdba 7065 /* Don't lose if destructors must be executed at file-level. */
5156628f 7066 if (! processing_template_decl && TREE_STATIC (decl)
ec255269 7067 && TYPE_NEEDS_DESTRUCTOR (complete_type (type))
8d08fdba
MS
7068 && !TREE_PERMANENT (decl))
7069 {
7070 push_obstacks (&permanent_obstack, &permanent_obstack);
7071 decl = copy_node (decl);
7072 if (TREE_CODE (type) == ARRAY_TYPE)
7073 {
7074 tree itype = TYPE_DOMAIN (type);
7075 if (itype && ! TREE_PERMANENT (itype))
7076 {
7077 itype = build_index_type (copy_to_permanent (TYPE_MAX_VALUE (itype)));
7078 type = build_cplus_array_type (TREE_TYPE (type), itype);
7079 TREE_TYPE (decl) = type;
7080 }
7081 }
7082 pop_obstacks ();
7083 }
7084
8d08fdba
MS
7085 context
7086 = (TREE_CODE (decl) == FUNCTION_DECL && DECL_VIRTUAL_P (decl))
7087 ? DECL_CLASS_CONTEXT (decl)
7088 : DECL_CONTEXT (decl);
7089
9a68c51f
JM
7090 if (initialized && context && TREE_CODE (context) == NAMESPACE_DECL
7091 && context != current_namespace && TREE_CODE (decl) == VAR_DECL)
7092 {
7093 /* When parsing the initializer, lookup should use the object's
7094 namespace. */
7095 push_decl_namespace (context);
7096 }
7097
2c73f9f5
ML
7098 /* We are only interested in class contexts, later. */
7099 if (context && TREE_CODE (context) == NAMESPACE_DECL)
7100 context = NULL_TREE;
7101
8d08fdba
MS
7102 if (initialized)
7103 /* Is it valid for this decl to have an initializer at all?
7104 If not, set INITIALIZED to zero, which will indirectly
82580166 7105 tell `cp_finish_decl' to ignore the initializer once it is parsed. */
8d08fdba
MS
7106 switch (TREE_CODE (decl))
7107 {
7108 case TYPE_DECL:
7109 /* typedef foo = bar means give foo the same type as bar.
82580166 7110 We haven't parsed bar yet, so `cp_finish_decl' will fix that up.
8d08fdba
MS
7111 Any other case of an initialization in a TYPE_DECL is an error. */
7112 if (pedantic || list_length (declspecs) > 1)
7113 {
8251199e 7114 cp_error ("typedef `%D' is initialized", decl);
8d08fdba
MS
7115 initialized = 0;
7116 }
7117 break;
7118
7119 case FUNCTION_DECL:
8251199e 7120 cp_error ("function `%#D' is initialized like a variable", decl);
8d08fdba
MS
7121 initialized = 0;
7122 break;
7123
7124 default:
5156628f 7125 if (! processing_template_decl)
8d08fdba 7126 {
bd6dd845 7127 if (type != error_mark_node)
5156628f 7128 {
bd6dd845
MS
7129 if (TYPE_SIZE (type) != NULL_TREE
7130 && ! TREE_CONSTANT (TYPE_SIZE (type)))
7131 {
8251199e
JM
7132 cp_error
7133 ("variable-sized object `%D' may not be initialized",
bd6dd845
MS
7134 decl);
7135 initialized = 0;
7136 }
5156628f 7137
bd6dd845
MS
7138 if (TREE_CODE (type) == ARRAY_TYPE
7139 && TYPE_SIZE (complete_type (TREE_TYPE (type))) == NULL_TREE)
7140 {
8251199e
JM
7141 cp_error
7142 ("elements of array `%#D' have incomplete type", decl);
bd6dd845
MS
7143 initialized = 0;
7144 }
5156628f 7145 }
8d08fdba
MS
7146 }
7147 }
7148
8d08fdba
MS
7149 if (initialized)
7150 {
a9aedbc2 7151 if (! toplevel_bindings_p ()
8d08fdba 7152 && DECL_EXTERNAL (decl))
8251199e 7153 cp_warning ("declaration of `%#D' has `extern' and is initialized",
8d08fdba
MS
7154 decl);
7155 DECL_EXTERNAL (decl) = 0;
5566b478 7156 if (toplevel_bindings_p ())
8d08fdba
MS
7157 TREE_STATIC (decl) = 1;
7158
7159 /* Tell `pushdecl' this is an initialized decl
7160 even though we don't yet have the initializer expression.
82580166 7161 Also tell `cp_finish_decl' it may store the real initializer. */
8d08fdba
MS
7162 DECL_INITIAL (decl) = error_mark_node;
7163 }
7164
5566b478 7165 if (context && TYPE_SIZE (complete_type (context)) != NULL_TREE)
5b605f68 7166 {
6b400b21 7167 push_nested_class (context, 2);
e97e5263 7168
5b605f68
MS
7169 if (TREE_CODE (decl) == VAR_DECL)
7170 {
7171 tree field = lookup_field (context, DECL_NAME (decl), 0, 0);
7172 if (field == NULL_TREE || TREE_CODE (field) != VAR_DECL)
8251199e 7173 cp_error ("`%#D' is not a static member of `%#T'", decl, context);
e349ee73
MS
7174 else
7175 {
7176 if (DECL_CONTEXT (field) != context)
f2d773a2 7177 {
8251199e 7178 cp_pedwarn ("ANSI C++ does not permit `%T::%D' to be defined as `%T::%D'",
f2d773a2
JM
7179 DECL_CONTEXT (field), DECL_NAME (decl),
7180 context, DECL_NAME (decl));
7181 DECL_CONTEXT (decl) = DECL_CONTEXT (field);
7182 }
75650646
MM
7183 /* Static data member are tricky; an in-class initialization
7184 still doesn't provide a definition, so the in-class
7185 declaration will have DECL_EXTERNAL set, but will have an
7186 initialization. Thus, duplicate_decls won't warn
7187 about this situation, and so we check here. */
7188 if (DECL_INITIAL (decl) && DECL_INITIAL (field))
8251199e 7189 cp_error ("duplicate initialization of %D", decl);
e349ee73
MS
7190 if (duplicate_decls (decl, field))
7191 decl = field;
7192 }
5b605f68 7193 }
f30432d7
MS
7194 else
7195 {
5566b478 7196 tree field = check_classfn (context, decl);
f30432d7
MS
7197 if (field && duplicate_decls (decl, field))
7198 decl = field;
7199 }
7200
7201 /* cp_finish_decl sets DECL_EXTERNAL if DECL_IN_AGGR_P is set. */
dff3e828
BK
7202 DECL_IN_AGGR_P (decl) = 0;
7203 if ((DECL_LANG_SPECIFIC (decl) && DECL_USE_TEMPLATE (decl))
7204 || CLASSTYPE_USE_TEMPLATE (context))
84e6233f
JM
7205 {
7206 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
7207 /* [temp.expl.spec] An explicit specialization of a static data
7208 member of a template is a definition if the declaration
7209 includes an initializer; otherwise, it is a declaration.
7210
7211 We check for processing_specialization so this only applies
7212 to the new specialization syntax. */
7213 if (DECL_INITIAL (decl) == NULL_TREE && processing_specialization)
7214 DECL_EXTERNAL (decl) = 1;
7215 }
f30432d7 7216
b7698cf0 7217 if (DECL_EXTERNAL (decl) && ! DECL_TEMPLATE_SPECIALIZATION (decl))
8251199e 7218 cp_pedwarn ("declaration of `%#D' outside of class is not definition",
f30432d7 7219 decl);
5b605f68
MS
7220 }
7221
3a846e6e
NC
7222#ifdef SET_DEFAULT_DECL_ATTRIBUTES
7223 SET_DEFAULT_DECL_ATTRIBUTES (decl, attributes);
7224#endif
7225
a1774733
BK
7226 /* Set attributes here so if duplicate decl, will have proper attributes. */
7227 cplus_decl_attributes (decl, attributes, prefix_attributes);
7228
8d08fdba
MS
7229 /* Add this decl to the current binding level, but not if it
7230 comes from another scope, e.g. a static member variable.
7231 TEM may equal DECL or it may be a previous decl of the same name. */
5b605f68 7232
51bcf661
JM
7233 if ((TREE_CODE (decl) != PARM_DECL && DECL_CONTEXT (decl) != NULL_TREE
7234 /* Definitions of namespace members outside their namespace are
7235 possible. */
7236 && TREE_CODE (DECL_CONTEXT (decl)) != NAMESPACE_DECL)
2c73f9f5 7237 || (TREE_CODE (decl) == TEMPLATE_DECL && !namespace_bindings_p ())
75650646
MM
7238 || TREE_CODE (type) == LANG_TYPE
7239 /* The declaration of template specializations does not affect
7240 the functions available for overload resolution, so we do not
7241 call pushdecl. */
e1467ff2 7242 || (TREE_CODE (decl) == FUNCTION_DECL
75650646 7243 && DECL_TEMPLATE_SPECIALIZATION (decl)))
8d08fdba
MS
7244 tem = decl;
7245 else
8926095f 7246 tem = pushdecl (decl);
2ee887f2 7247
5156628f 7248 if (processing_template_decl)
5566b478
MS
7249 {
7250 if (! current_function_decl)
3ac3d9ea 7251 tem = push_template_decl (tem);
5566b478 7252 else if (minimal_parse_mode)
b5effa19 7253 DECL_VINDEX (tem)
5566b478
MS
7254 = build_min_nt (DECL_STMT, copy_to_permanent (declarator),
7255 copy_to_permanent (declspecs),
c11b6f21 7256 NULL_TREE);
5566b478
MS
7257 }
7258
7259
2ee887f2 7260#if ! defined (ASM_OUTPUT_BSS) && ! defined (ASM_OUTPUT_ALIGNED_BSS)
a3203465 7261 /* Tell the back-end to use or not use .common as appropriate. If we say
a50f0918
MS
7262 -fconserve-space, we want this to save .data space, at the expense of
7263 wrong semantics. If we say -fno-conserve-space, we want this to
7264 produce errors about redefs; to do this we force variables into the
7265 data segment. */
a3203465 7266 DECL_COMMON (tem) = flag_conserve_space || ! TREE_PUBLIC (tem);
2ee887f2 7267#endif
3a846e6e 7268
5156628f 7269 if (! processing_template_decl)
5566b478 7270 start_decl_1 (tem);
8d08fdba 7271
5566b478
MS
7272 /* Corresponding pop_obstacks is done in `cp_finish_decl'. */
7273 push_obstacks_nochange ();
8d08fdba 7274
5566b478
MS
7275#if 0
7276 /* We have no way of knowing whether the initializer will need to be
7277 evaluated at run-time or not until we've parsed it, so let's just put
7278 it in the permanent obstack. (jason) */
8d08fdba
MS
7279 if (init_written
7280 && ! (TREE_CODE (tem) == PARM_DECL
7281 || (TREE_READONLY (tem)
7282 && (TREE_CODE (tem) == VAR_DECL
7283 || TREE_CODE (tem) == FIELD_DECL))))
7284 {
7285 /* When parsing and digesting the initializer,
7286 use temporary storage. Do this even if we will ignore the value. */
a9aedbc2 7287 if (toplevel_bindings_p () && debug_temp_inits)
8d08fdba 7288 {
5156628f 7289 if (processing_template_decl
5566b478 7290 || TYPE_NEEDS_CONSTRUCTING (type)
a28e3c7f 7291 || TREE_CODE (type) == REFERENCE_TYPE)
8d08fdba
MS
7292 /* In this case, the initializer must lay down in permanent
7293 storage, since it will be saved until `finish_file' is run. */
7294 ;
7295 else
7296 temporary_allocation ();
7297 }
7298 }
5566b478 7299#endif
8d08fdba 7300
8d08fdba
MS
7301 return tem;
7302}
7303
5566b478
MS
7304void
7305start_decl_1 (decl)
7306 tree decl;
8d08fdba 7307{
5566b478
MS
7308 tree type = TREE_TYPE (decl);
7309 int initialized = (DECL_INITIAL (decl) != NULL_TREE);
8d08fdba 7310
44689c12
ML
7311 if (type == error_mark_node)
7312 return;
7313
5566b478
MS
7314 /* If this type of object needs a cleanup, and control may
7315 jump past it, make a new binding level so that it is cleaned
7316 up only when it is initialized first. */
7317 if (TYPE_NEEDS_DESTRUCTOR (type)
7318 && current_binding_level->more_cleanups_ok == 0)
7319 pushlevel_temporary (1);
7320
7321 if (initialized)
7322 /* Is it valid for this decl to have an initializer at all?
7323 If not, set INITIALIZED to zero, which will indirectly
7324 tell `cp_finish_decl' to ignore the initializer once it is parsed. */
8d08fdba 7325 {
5566b478
MS
7326 /* Don't allow initializations for incomplete types except for
7327 arrays which might be completed by the initialization. */
44689c12 7328 if (TYPE_SIZE (complete_type (type)) != NULL_TREE)
5566b478
MS
7329 ; /* A complete type is ok. */
7330 else if (TREE_CODE (type) != ARRAY_TYPE)
8d08fdba 7331 {
8251199e 7332 cp_error ("variable `%#D' has initializer but incomplete type",
5566b478
MS
7333 decl);
7334 initialized = 0;
25eb19ff 7335 type = TREE_TYPE (decl) = error_mark_node;
5566b478
MS
7336 }
7337 else if (TYPE_SIZE (complete_type (TREE_TYPE (type))) == NULL_TREE)
7338 {
7339 if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
8251199e 7340 cp_error ("elements of array `%#D' have incomplete type", decl);
5566b478
MS
7341 /* else we already gave an error in start_decl. */
7342 initialized = 0;
8d08fdba 7343 }
8d08fdba
MS
7344 }
7345
5566b478
MS
7346 if (!initialized
7347 && TREE_CODE (decl) != TYPE_DECL
7348 && TREE_CODE (decl) != TEMPLATE_DECL
7349 && IS_AGGR_TYPE (type) && ! DECL_EXTERNAL (decl))
8d08fdba 7350 {
5156628f 7351 if ((! processing_template_decl || ! uses_template_parms (type))
7fcdf4c2 7352 && TYPE_SIZE (complete_type (type)) == NULL_TREE)
5566b478 7353 {
8251199e 7354 cp_error ("aggregate `%#D' has incomplete type and cannot be initialized",
5566b478
MS
7355 decl);
7356 /* Change the type so that assemble_variable will give
7357 DECL an rtl we can live with: (mem (const_int 0)). */
25eb19ff 7358 type = TREE_TYPE (decl) = error_mark_node;
5566b478
MS
7359 }
7360 else
7361 {
7362 /* If any base type in the hierarchy of TYPE needs a constructor,
7363 then we set initialized to 1. This way any nodes which are
7364 created for the purposes of initializing this aggregate
7365 will live as long as it does. This is necessary for global
7366 aggregates which do not have their initializers processed until
7367 the end of the file. */
7368 initialized = TYPE_NEEDS_CONSTRUCTING (type);
7369 }
7370 }
7371
7372#if 0
7373 /* We don't do this yet for GNU C++. */
7374 /* For a local variable, define the RTL now. */
7375 if (! toplevel_bindings_p ()
7376 /* But not if this is a duplicate decl
7377 and we preserved the rtl from the previous one
7378 (which may or may not happen). */
7379 && DECL_RTL (tem) == NULL_RTX)
7380 {
7381 if (TYPE_SIZE (TREE_TYPE (tem)) != NULL_TREE)
7382 expand_decl (tem);
7383 else if (TREE_CODE (TREE_TYPE (tem)) == ARRAY_TYPE
7384 && DECL_INITIAL (tem) != NULL_TREE)
7385 expand_decl (tem);
7386 }
7387#endif
7388
7389 if (! initialized)
7390 DECL_INITIAL (decl) = NULL_TREE;
7391}
7392
7393/* Handle initialization of references.
38e01259 7394 These three arguments are from `cp_finish_decl', and have the
e92cc029
MS
7395 same meaning here that they do there.
7396
7397 Quotes on semantics can be found in ARM 8.4.3. */
7398
5566b478 7399static void
a703fb38 7400grok_reference_init (decl, type, init)
5566b478 7401 tree decl, type, init;
5566b478
MS
7402{
7403 tree tmp;
7404
7405 if (init == NULL_TREE)
7406 {
7407 if ((DECL_LANG_SPECIFIC (decl) == 0
7408 || DECL_IN_AGGR_P (decl) == 0)
7409 && ! DECL_THIS_EXTERN (decl))
7410 {
8251199e 7411 cp_error ("`%D' declared as reference but not initialized", decl);
5566b478
MS
7412 if (TREE_CODE (decl) == VAR_DECL)
7413 SET_DECL_REFERENCE_SLOT (decl, error_mark_node);
7414 }
7415 return;
7416 }
7417
7418 if (init == error_mark_node)
7419 return;
7420
7421 if (TREE_CODE (type) == REFERENCE_TYPE
7422 && TREE_CODE (init) == CONSTRUCTOR)
7423 {
8251199e 7424 cp_error ("ANSI C++ forbids use of initializer list to initialize reference `%D'", decl);
8d08fdba
MS
7425 return;
7426 }
7427
7428 if (TREE_CODE (init) == TREE_LIST)
7429 init = build_compound_expr (init);
8d08fdba 7430
8ccc31eb
MS
7431 if (TREE_CODE (TREE_TYPE (init)) == REFERENCE_TYPE)
7432 init = convert_from_reference (init);
7433
8d08fdba
MS
7434 if (TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE
7435 && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE)
7436 {
a3203465 7437 /* Note: default conversion is only called in very special cases. */
8d08fdba
MS
7438 init = default_conversion (init);
7439 }
7440
a3203465 7441 tmp = convert_to_reference
9a3b49ac
MS
7442 (type, init, CONV_IMPLICIT,
7443 LOOKUP_SPECULATIVELY|LOOKUP_NORMAL|DIRECT_BIND, decl);
8d08fdba 7444
a3203465
MS
7445 if (tmp == error_mark_node)
7446 goto fail;
7447 else if (tmp != NULL_TREE)
8d08fdba 7448 {
a3203465 7449 init = tmp;
4c7bdca6 7450 DECL_INITIAL (decl) = save_expr (init);
8d08fdba 7451 }
a3203465 7452 else
8d08fdba 7453 {
8251199e 7454 cp_error ("cannot initialize `%T' from `%T'", type, TREE_TYPE (init));
a3203465 7455 goto fail;
8d08fdba 7456 }
8d08fdba 7457
8d08fdba
MS
7458 /* ?? Can this be optimized in some cases to
7459 hand back the DECL_INITIAL slot?? */
7460 if (TYPE_SIZE (TREE_TYPE (type)))
7461 {
7462 init = convert_from_reference (decl);
7463 if (TREE_PERMANENT (decl))
7464 init = copy_to_permanent (init);
7465 SET_DECL_REFERENCE_SLOT (decl, init);
7466 }
7467
7468 if (TREE_STATIC (decl) && ! TREE_CONSTANT (DECL_INITIAL (decl)))
7469 {
7470 expand_static_init (decl, DECL_INITIAL (decl));
7471 DECL_INITIAL (decl) = NULL_TREE;
7472 }
7473 return;
7474
7475 fail:
7476 if (TREE_CODE (decl) == VAR_DECL)
7477 SET_DECL_REFERENCE_SLOT (decl, error_mark_node);
7478 return;
7479}
7480
6060a796
MS
7481/* Fill in DECL_INITIAL with some magical value to prevent expand_decl from
7482 mucking with forces it does not comprehend (i.e. initialization with a
7483 constructor). If we are at global scope and won't go into COMMON, fill
7484 it in with a dummy CONSTRUCTOR to force the variable into .data;
7485 otherwise we can use error_mark_node. */
7486
28cbf42c
MS
7487static tree
7488obscure_complex_init (decl, init)
7489 tree decl, init;
6060a796 7490{
28cbf42c
MS
7491 if (! flag_no_inline && TREE_STATIC (decl))
7492 {
7493 if (extract_init (decl, init))
7494 return NULL_TREE;
7495 }
7496
2ee887f2 7497#if ! defined (ASM_OUTPUT_BSS) && ! defined (ASM_OUTPUT_ALIGNED_BSS)
a9aedbc2 7498 if (toplevel_bindings_p () && ! DECL_COMMON (decl))
6060a796
MS
7499 DECL_INITIAL (decl) = build (CONSTRUCTOR, TREE_TYPE (decl), NULL_TREE,
7500 NULL_TREE);
7501 else
2ee887f2 7502#endif
6060a796 7503 DECL_INITIAL (decl) = error_mark_node;
28cbf42c
MS
7504
7505 return init;
6060a796
MS
7506}
7507
91063b51
MM
7508/* Issue an error message if DECL is an uninitialized const variable. */
7509
7510static void
7511check_for_uninitialized_const_var (decl)
7512 tree decl;
7513{
7514 tree type = TREE_TYPE (decl);
7515
7516 /* ``Unless explicitly declared extern, a const object does not have
7517 external linkage and must be initialized. ($8.4; $12.1)'' ARM
7518 7.1.6 */
7519 if (TREE_CODE (decl) == VAR_DECL
7520 && TREE_CODE (type) != REFERENCE_TYPE
7521 && CP_TYPE_CONST_P (type)
7522 && !TYPE_NEEDS_CONSTRUCTING (type)
7523 && !DECL_INITIAL (decl))
7524 cp_error ("uninitialized const `%D'", decl);
7525}
7526
8d08fdba
MS
7527/* Finish processing of a declaration;
7528 install its line number and initial value.
7529 If the length of an array type is not known before,
7530 it must be determined now, from the initial value, or it is an error.
7531
7532 Call `pop_obstacks' iff NEED_POP is nonzero.
7533
82580166 7534 For C++, `cp_finish_decl' must be fairly evasive: it must keep initializers
8d08fdba
MS
7535 for aggregates that have constructors alive on the permanent obstack,
7536 so that the global initializing functions can be written at the end.
7537
7538 INIT0 holds the value of an initializer that should be allowed to escape
7539 the normal rules.
7540
6060a796
MS
7541 FLAGS is LOOKUP_ONLYCONVERTING is the = init syntax was used, else 0
7542 if the (init) syntax was used.
7543
8d08fdba 7544 For functions that take default parameters, DECL points to its
82580166 7545 "maximal" instantiation. `cp_finish_decl' must then also declared its
8d08fdba
MS
7546 subsequently lower and lower forms of instantiation, checking for
7547 ambiguity as it goes. This can be sped up later. */
7548
7549void
82580166 7550cp_finish_decl (decl, init, asmspec_tree, need_pop, flags)
8d08fdba
MS
7551 tree decl, init;
7552 tree asmspec_tree;
7553 int need_pop;
6060a796 7554 int flags;
8d08fdba
MS
7555{
7556 register tree type;
a703fb38 7557 tree cleanup = NULL_TREE, ttype = NULL_TREE;
8d08fdba
MS
7558 int was_incomplete;
7559 int temporary = allocation_temporary_p ();
7560 char *asmspec = NULL;
7561 int was_readonly = 0;
0c4b14c4 7562 int already_used = 0;
8d08fdba
MS
7563
7564 /* If this is 0, then we did not change obstacks. */
7565 if (! decl)
7566 {
7567 if (init)
8251199e 7568 error ("assignment (not initialization) in declaration");
8d08fdba
MS
7569 return;
7570 }
7571
a4443a08 7572 /* If a name was specified, get the string. */
8d08fdba 7573 if (asmspec_tree)
8d08fdba 7574 asmspec = TREE_STRING_POINTER (asmspec_tree);
8d08fdba 7575
2c73f9f5
ML
7576 if (init && TREE_CODE (init) == NAMESPACE_DECL)
7577 {
8251199e 7578 cp_error ("Cannot initialize `%D' to namespace `%D'",
2c73f9f5
ML
7579 decl, init);
7580 init = NULL_TREE;
7581 }
7582
6ba89f8e
MM
7583 if (current_class_type
7584 && DECL_REAL_CONTEXT (decl) == current_class_type
7585 && TYPE_BEING_DEFINED (current_class_type)
7586 && (DECL_INITIAL (decl) || init))
7587 DECL_DEFINED_IN_CLASS_P (decl) = 1;
7588
9a68c51f
JM
7589 if (TREE_CODE (decl) == VAR_DECL
7590 && DECL_CONTEXT (decl)
7591 && TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL
7592 && DECL_CONTEXT (decl) != current_namespace
7593 && init)
7594 {
7595 /* Leave the namespace of the object. */
7596 pop_decl_namespace ();
7597 }
7598
8d08fdba
MS
7599 /* If the type of the thing we are declaring either has
7600 a constructor, or has a virtual function table pointer,
7601 AND its initialization was accepted by `start_decl',
7602 then we stayed on the permanent obstack through the
7603 declaration, otherwise, changed obstacks as GCC would. */
7604
7605 type = TREE_TYPE (decl);
7606
f376e137 7607 if (type == error_mark_node)
eac293a1 7608 {
a9aedbc2 7609 if (toplevel_bindings_p () && temporary)
eac293a1
MS
7610 end_temporary_allocation ();
7611
7612 return;
7613 }
f376e137 7614
a7a7710d
NS
7615 if (TYPE_HAS_MUTABLE_P (type))
7616 TREE_READONLY (decl) = 0;
7617
5156628f 7618 if (processing_template_decl)
5566b478
MS
7619 {
7620 if (init && DECL_INITIAL (decl))
7621 DECL_INITIAL (decl) = init;
faf5394a 7622 if (minimal_parse_mode && ! DECL_ARTIFICIAL (decl))
5566b478
MS
7623 {
7624 tree stmt = DECL_VINDEX (decl);
e1467ff2 7625 /* If the decl is declaring a member of a local class (in a
8857f91e
MM
7626 template function), the DECL_VINDEX will either be NULL,
7627 or it will be an actual virtual function index, not a
7628 DECL_STMT. */
7629 if (stmt != NULL_TREE && TREE_CODE (stmt) == DECL_STMT)
75650646
MM
7630 {
7631 DECL_VINDEX (decl) = NULL_TREE;
7632 TREE_OPERAND (stmt, 2) = copy_to_permanent (init);
7633 add_tree (stmt);
7634 }
5566b478 7635 }
8d08fdba 7636
5566b478
MS
7637 goto finish_end0;
7638 }
8d08fdba
MS
7639 /* Take care of TYPE_DECLs up front. */
7640 if (TREE_CODE (decl) == TYPE_DECL)
7641 {
7642 if (init && DECL_INITIAL (decl))
7643 {
7644 /* typedef foo = bar; store the type of bar as the type of foo. */
7645 TREE_TYPE (decl) = type = TREE_TYPE (init);
7646 DECL_INITIAL (decl) = init = NULL_TREE;
7647 }
a0a33927
MS
7648 if (type != error_mark_node
7649 && IS_AGGR_TYPE (type) && DECL_NAME (decl))
8d08fdba
MS
7650 {
7651 if (TREE_TYPE (DECL_NAME (decl)) && TREE_TYPE (decl) != type)
8251199e 7652 cp_warning ("shadowing previous type declaration of `%#D'", decl);
8d08fdba
MS
7653 set_identifier_type_value (DECL_NAME (decl), type);
7654 CLASSTYPE_GOT_SEMICOLON (type) = 1;
7655 }
7656 GNU_xref_decl (current_function_decl, decl);
cffa8729
MS
7657
7658 /* If we have installed this as the canonical typedef for this
7659 type, and that type has not been defined yet, delay emitting
956d6950 7660 the debug information for it, as we will emit it later. */
d2e5ee5c 7661 if (TYPE_MAIN_DECL (TREE_TYPE (decl)) == decl
cffa8729
MS
7662 && TYPE_SIZE (TREE_TYPE (decl)) == NULL_TREE)
7663 TYPE_DECL_SUPPRESS_DEBUG (decl) = 1;
7664
8d08fdba 7665 rest_of_decl_compilation (decl, NULL_PTR,
5566b478 7666 DECL_CONTEXT (decl) == NULL_TREE, at_eof);
8d08fdba
MS
7667 goto finish_end;
7668 }
8d08fdba
MS
7669 if (TREE_CODE (decl) != FUNCTION_DECL)
7670 {
7671 ttype = target_type (type);
8d08fdba
MS
7672 }
7673
7674 if (! DECL_EXTERNAL (decl) && TREE_READONLY (decl)
7675 && TYPE_NEEDS_CONSTRUCTING (type))
7676 {
7677
7678 /* Currently, GNU C++ puts constants in text space, making them
7679 impossible to initialize. In the future, one would hope for
7680 an operating system which understood the difference between
7681 initialization and the running of a program. */
7682 was_readonly = 1;
7683 TREE_READONLY (decl) = 0;
7684 }
7685
7686 if (TREE_CODE (decl) == FIELD_DECL)
7687 {
7688 if (init && init != error_mark_node)
7689 my_friendly_assert (TREE_PERMANENT (init), 147);
7690
7691 if (asmspec)
7692 {
7693 /* This must override the asm specifier which was placed
6060a796 7694 by grokclassfn. Lay this out fresh. */
8d08fdba
MS
7695 DECL_RTL (TREE_TYPE (decl)) = NULL_RTX;
7696 DECL_ASSEMBLER_NAME (decl) = get_identifier (asmspec);
7697 make_decl_rtl (decl, asmspec, 0);
7698 }
7699 }
7700 /* If `start_decl' didn't like having an initialization, ignore it now. */
7701 else if (init != NULL_TREE && DECL_INITIAL (decl) == NULL_TREE)
7702 init = NULL_TREE;
7703 else if (DECL_EXTERNAL (decl))
7704 ;
7705 else if (TREE_CODE (type) == REFERENCE_TYPE
7706 || (TYPE_LANG_SPECIFIC (type) && IS_SIGNATURE_REFERENCE (type)))
7707 {
f376e137
MS
7708 if (TREE_STATIC (decl))
7709 make_decl_rtl (decl, NULL_PTR,
a9aedbc2 7710 toplevel_bindings_p ()
f376e137 7711 || pseudo_global_level_p ());
a703fb38 7712 grok_reference_init (decl, type, init);
8d08fdba
MS
7713 init = NULL_TREE;
7714 }
7715
7716 GNU_xref_decl (current_function_decl, decl);
7717
a0a33927 7718 if (TREE_CODE (decl) == FIELD_DECL)
8d08fdba
MS
7719 ;
7720 else if (TREE_CODE (decl) == CONST_DECL)
7721 {
7722 my_friendly_assert (TREE_CODE (decl) != REFERENCE_TYPE, 148);
7723
7724 DECL_INITIAL (decl) = init;
7725
7726 /* This will keep us from needing to worry about our obstacks. */
7727 my_friendly_assert (init != NULL_TREE, 149);
7728 init = NULL_TREE;
7729 }
7730 else if (init)
7731 {
7732 if (TYPE_HAS_CONSTRUCTOR (type) || TYPE_NEEDS_CONSTRUCTING (type))
7733 {
7734 if (TREE_CODE (type) == ARRAY_TYPE)
7735 init = digest_init (type, init, (tree *) 0);
848b92e1
JM
7736 else if (TREE_CODE (init) == CONSTRUCTOR
7737 && TREE_HAS_CONSTRUCTOR (init))
8d08fdba 7738 {
f30432d7 7739 if (TYPE_NON_AGGREGATE_CLASS (type))
8d08fdba 7740 {
8251199e 7741 cp_error ("`%D' must be initialized by constructor, not by `{...}'",
a28e3c7f 7742 decl);
8d08fdba
MS
7743 init = error_mark_node;
7744 }
7745 else
7746 goto dont_use_constructor;
7747 }
8d08fdba
MS
7748 }
7749 else
7750 {
7751 dont_use_constructor:
7752 if (TREE_CODE (init) != TREE_VEC)
7753 init = store_init_value (decl, init);
8d08fdba 7754 }
28cbf42c
MS
7755
7756 if (init)
7757 /* We must hide the initializer so that expand_decl
7758 won't try to do something it does not understand. */
7759 init = obscure_complex_init (decl, init);
8d08fdba 7760 }
a0a33927
MS
7761 else if (DECL_EXTERNAL (decl))
7762 ;
8d08fdba
MS
7763 else if (TREE_CODE_CLASS (TREE_CODE (type)) == 't'
7764 && (IS_AGGR_TYPE (type) || TYPE_NEEDS_CONSTRUCTING (type)))
7765 {
7766 tree ctype = type;
7767 while (TREE_CODE (ctype) == ARRAY_TYPE)
7768 ctype = TREE_TYPE (ctype);
7769 if (! TYPE_NEEDS_CONSTRUCTING (ctype))
7770 {
7771 if (CLASSTYPE_READONLY_FIELDS_NEED_INIT (ctype))
8251199e 7772 cp_error ("structure `%D' with uninitialized const members", decl);
8d08fdba 7773 if (CLASSTYPE_REF_FIELDS_NEED_INIT (ctype))
8251199e 7774 cp_error ("structure `%D' with uninitialized reference members",
a28e3c7f 7775 decl);
8d08fdba
MS
7776 }
7777
91063b51 7778 check_for_uninitialized_const_var (decl);
8d08fdba 7779
6060a796
MS
7780 if (TYPE_SIZE (type) != NULL_TREE
7781 && TYPE_NEEDS_CONSTRUCTING (type))
28cbf42c 7782 init = obscure_complex_init (decl, NULL_TREE);
8d08fdba 7783
91063b51
MM
7784 }
7785 else
7786 check_for_uninitialized_const_var (decl);
7787
8d08fdba
MS
7788 /* For top-level declaration, the initial value was read in
7789 the temporary obstack. MAXINDEX, rtl, etc. to be made below
7790 must go in the permanent obstack; but don't discard the
7791 temporary data yet. */
7792
a9aedbc2 7793 if (toplevel_bindings_p () && temporary)
8d08fdba
MS
7794 end_temporary_allocation ();
7795
7796 /* Deduce size of array from initialization, if not already known. */
7797
7798 if (TREE_CODE (type) == ARRAY_TYPE
7799 && TYPE_DOMAIN (type) == NULL_TREE
7800 && TREE_CODE (decl) != TYPE_DECL)
7801 {
7802 int do_default
7803 = (TREE_STATIC (decl)
7804 /* Even if pedantic, an external linkage array
7805 may have incomplete type at first. */
7806 ? pedantic && ! DECL_EXTERNAL (decl)
7807 : !DECL_EXTERNAL (decl));
7808 tree initializer = init ? init : DECL_INITIAL (decl);
7809 int failure = complete_array_type (type, initializer, do_default);
7810
7811 if (failure == 1)
8251199e 7812 cp_error ("initializer fails to determine size of `%D'", decl);
8d08fdba
MS
7813
7814 if (failure == 2)
7815 {
7816 if (do_default)
8251199e 7817 cp_error ("array size missing in `%D'", decl);
8d08fdba
MS
7818 /* If a `static' var's size isn't known, make it extern as
7819 well as static, so it does not get allocated. If it's not
7820 `static', then don't mark it extern; finish_incomplete_decl
7821 will give it a default size and it will get allocated. */
7822 else if (!pedantic && TREE_STATIC (decl) && !TREE_PUBLIC (decl))
7823 DECL_EXTERNAL (decl) = 1;
7824 }
7825
7826 if (pedantic && TYPE_DOMAIN (type) != NULL_TREE
7827 && tree_int_cst_lt (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
7828 integer_zero_node))
8251199e 7829 cp_error ("zero-size array `%D'", decl);
8d08fdba
MS
7830
7831 layout_decl (decl, 0);
7832 }
7833
7834 if (TREE_CODE (decl) == VAR_DECL)
7835 {
7836 if (DECL_SIZE (decl) == NULL_TREE
ec255269 7837 && TYPE_SIZE (complete_type (TREE_TYPE (decl))) != NULL_TREE)
8d08fdba
MS
7838 layout_decl (decl, 0);
7839
7840 if (TREE_STATIC (decl) && DECL_SIZE (decl) == NULL_TREE)
7841 {
7842 /* A static variable with an incomplete type:
7843 that is an error if it is initialized.
7844 Otherwise, let it through, but if it is not `extern'
7845 then it may cause an error message later. */
7846 if (DECL_INITIAL (decl) != NULL_TREE)
8251199e 7847 cp_error ("storage size of `%D' isn't known", decl);
8d08fdba
MS
7848 init = NULL_TREE;
7849 }
7850 else if (!DECL_EXTERNAL (decl) && DECL_SIZE (decl) == NULL_TREE)
7851 {
7852 /* An automatic variable with an incomplete type: that is an error.
7853 Don't talk about array types here, since we took care of that
7854 message in grokdeclarator. */
8251199e 7855 cp_error ("storage size of `%D' isn't known", decl);
8d08fdba
MS
7856 TREE_TYPE (decl) = error_mark_node;
7857 }
7858 else if (!DECL_EXTERNAL (decl) && IS_AGGR_TYPE (ttype))
7859 /* Let debugger know it should output info for this type. */
7860 note_debug_info_needed (ttype);
7861
6eb3bb27 7862 if (TREE_STATIC (decl) && DECL_CLASS_SCOPE_P (decl))
d2e5ee5c
MS
7863 note_debug_info_needed (DECL_CONTEXT (decl));
7864
8d08fdba
MS
7865 if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl))
7866 && DECL_SIZE (decl) != NULL_TREE
7867 && ! TREE_CONSTANT (DECL_SIZE (decl)))
7868 {
7869 if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
7870 constant_expression_warning (DECL_SIZE (decl));
7871 else
8251199e 7872 cp_error ("storage size of `%D' isn't constant", decl);
8d08fdba
MS
7873 }
7874
c91a56d2
MS
7875 if (! DECL_EXTERNAL (decl) && TYPE_NEEDS_DESTRUCTOR (type)
7876 /* Cleanups for static variables are handled by `finish_file'. */
7877 && ! TREE_STATIC (decl))
8d08fdba
MS
7878 {
7879 int yes = suspend_momentary ();
2ee887f2 7880 cleanup = maybe_build_cleanup (decl);
8d08fdba
MS
7881 resume_momentary (yes);
7882 }
7883 }
7884 /* PARM_DECLs get cleanups, too. */
7885 else if (TREE_CODE (decl) == PARM_DECL && TYPE_NEEDS_DESTRUCTOR (type))
7886 {
7887 if (temporary)
7888 end_temporary_allocation ();
7889 cleanup = maybe_build_cleanup (decl);
7890 if (temporary)
7891 resume_temporary_allocation ();
7892 }
7893
7894 /* Output the assembler code and/or RTL code for variables and functions,
7895 unless the type is an undefined structure or union.
7896 If not, it will get done when the type is completed. */
7897
5566b478
MS
7898 was_incomplete = (DECL_SIZE (decl) == NULL_TREE);
7899
8d08fdba
MS
7900 if (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == FUNCTION_DECL
7901 || TREE_CODE (decl) == RESULT_DECL)
7902 {
7903 /* ??? FIXME: What about nested classes? */
e1467ff2 7904 int toplev = toplevel_bindings_p () || pseudo_global_level_p ();
8d08fdba 7905 int was_temp
d22c8596 7906 = (TREE_STATIC (decl) && TYPE_NEEDS_DESTRUCTOR (type)
8d08fdba
MS
7907 && allocation_temporary_p ());
7908
7909 if (was_temp)
7910 end_temporary_allocation ();
7911
5f2c99c4
JM
7912 /* Static data in a function with comdat linkage also has comdat
7913 linkage. */
42976354
BK
7914 if (TREE_CODE (decl) == VAR_DECL
7915 && TREE_STATIC (decl)
5f2c99c4 7916 /* Don't mess with __FUNCTION__. */
aeb302bb 7917 && ! TREE_ASM_WRITTEN (decl)
42976354
BK
7918 && current_function_decl
7919 && DECL_CONTEXT (decl) == current_function_decl
5f2c99c4
JM
7920 && (DECL_THIS_INLINE (current_function_decl)
7921 || DECL_TEMPLATE_INSTANTIATION (current_function_decl))
893de33c 7922 && TREE_PUBLIC (current_function_decl))
42976354 7923 {
5f2c99c4
JM
7924 /* Rather than try to get this right with inlining, we suppress
7925 inlining of such functions. */
aeb302bb
JM
7926 current_function_cannot_inline
7927 = "function with static variable cannot be inline";
5f2c99c4
JM
7928
7929 /* If flag_weak, we don't need to mess with this, as we can just
7930 make the function weak, and let it refer to its unique local
7931 copy. This works because we don't allow the function to be
7932 inlined. */
7933 if (! flag_weak)
7934 {
7935 if (DECL_INTERFACE_KNOWN (current_function_decl))
7936 {
7937 TREE_PUBLIC (decl) = 1;
7938 DECL_EXTERNAL (decl) = DECL_EXTERNAL (current_function_decl);
7939 }
7940 else if (DECL_INITIAL (decl) == NULL_TREE
7941 || DECL_INITIAL (decl) == error_mark_node)
7942 {
7943 TREE_PUBLIC (decl) = 1;
7944 DECL_COMMON (decl) = 1;
7945 }
7946 /* else we lose. We can only do this if we can use common,
7947 which we can't if it has been initialized. */
7948
7949 if (TREE_PUBLIC (decl))
7950 DECL_ASSEMBLER_NAME (decl)
7951 = build_static_name (current_function_decl, DECL_NAME (decl));
7952 else if (! DECL_ARTIFICIAL (decl))
7953 {
7954 cp_warning_at ("sorry: semantics of inline function static data `%#D' are wrong (you'll wind up with multiple copies)", decl);
7955 cp_warning_at (" you can work around this by removing the initializer"), decl;
7956 }
7957 }
42976354
BK
7958 }
7959
77be6f82
JM
7960 else if (TREE_CODE (decl) == VAR_DECL
7961 && DECL_LANG_SPECIFIC (decl)
7962 && DECL_COMDAT (decl))
ea735e02
JM
7963 /* Set it up again; we might have set DECL_INITIAL since the
7964 last time. */
7965 comdat_linkage (decl);
77be6f82 7966
8d08fdba
MS
7967 if (TREE_CODE (decl) == VAR_DECL && DECL_VIRTUAL_P (decl))
7968 make_decl_rtl (decl, NULL_PTR, toplev);
7969 else if (TREE_CODE (decl) == VAR_DECL
7970 && TREE_READONLY (decl)
7971 && DECL_INITIAL (decl) != NULL_TREE
7972 && DECL_INITIAL (decl) != error_mark_node
a3203465 7973 && ! EMPTY_CONSTRUCTOR_P (DECL_INITIAL (decl)))
8d08fdba
MS
7974 {
7975 DECL_INITIAL (decl) = save_expr (DECL_INITIAL (decl));
7976
7977 if (asmspec)
7978 DECL_ASSEMBLER_NAME (decl) = get_identifier (asmspec);
7979
7980 if (! toplev
7981 && TREE_STATIC (decl)
7982 && ! TREE_SIDE_EFFECTS (decl)
7983 && ! TREE_PUBLIC (decl)
7984 && ! DECL_EXTERNAL (decl)
7985 && ! TYPE_NEEDS_DESTRUCTOR (type)
7986 && DECL_MODE (decl) != BLKmode)
7987 {
7988 /* If this variable is really a constant, then fill its DECL_RTL
7989 slot with something which won't take up storage.
7990 If something later should take its address, we can always give
7991 it legitimate RTL at that time. */
7992 DECL_RTL (decl) = gen_reg_rtx (DECL_MODE (decl));
7993 store_expr (DECL_INITIAL (decl), DECL_RTL (decl), 0);
7994 TREE_ASM_WRITTEN (decl) = 1;
7995 }
a0a33927 7996 else if (toplev && ! TREE_PUBLIC (decl))
8d08fdba 7997 {
8d08fdba 7998 /* If this is a static const, change its apparent linkage
db5ae43f 7999 if it belongs to a #pragma interface. */
a0a33927 8000 if (!interface_unknown)
8d08fdba
MS
8001 {
8002 TREE_PUBLIC (decl) = 1;
8003 DECL_EXTERNAL (decl) = interface_only;
8004 }
8005 make_decl_rtl (decl, asmspec, toplev);
8006 }
8007 else
5566b478 8008 rest_of_decl_compilation (decl, asmspec, toplev, at_eof);
8d08fdba
MS
8009 }
8010 else if (TREE_CODE (decl) == VAR_DECL
8011 && DECL_LANG_SPECIFIC (decl)
8012 && DECL_IN_AGGR_P (decl))
8013 {
8014 if (TREE_STATIC (decl))
8015 {
8016 if (init == NULL_TREE
8017#ifdef DEFAULT_STATIC_DEFS
8018 /* If this code is dead, then users must
8019 explicitly declare static member variables
8020 outside the class def'n as well. */
8021 && TYPE_NEEDS_CONSTRUCTING (type)
8022#endif
8023 )
8024 {
8025 DECL_EXTERNAL (decl) = 1;
8026 make_decl_rtl (decl, asmspec, 1);
8027 }
8028 else
5566b478 8029 rest_of_decl_compilation (decl, asmspec, toplev, at_eof);
8d08fdba
MS
8030 }
8031 else
8032 /* Just a constant field. Should not need any rtl. */
8033 goto finish_end0;
8034 }
8035 else
5566b478 8036 rest_of_decl_compilation (decl, asmspec, toplev, at_eof);
8d08fdba
MS
8037
8038 if (was_temp)
8039 resume_temporary_allocation ();
8040
8041 if (type != error_mark_node
8042 && TYPE_LANG_SPECIFIC (type)
8043 && CLASSTYPE_ABSTRACT_VIRTUALS (type))
8044 abstract_virtuals_error (decl, type);
8045 else if ((TREE_CODE (type) == FUNCTION_TYPE
8046 || TREE_CODE (type) == METHOD_TYPE)
8047 && TYPE_LANG_SPECIFIC (TREE_TYPE (type))
8048 && CLASSTYPE_ABSTRACT_VIRTUALS (TREE_TYPE (type)))
8049 abstract_virtuals_error (decl, TREE_TYPE (type));
8050
8051 if (TYPE_LANG_SPECIFIC (type) && IS_SIGNATURE (type))
8052 signature_error (decl, type);
8053 else if ((TREE_CODE (type) == FUNCTION_TYPE
8054 || TREE_CODE (type) == METHOD_TYPE)
8055 && TYPE_LANG_SPECIFIC (TREE_TYPE (type))
8056 && IS_SIGNATURE (TREE_TYPE (type)))
8057 signature_error (decl, TREE_TYPE (type));
8058
8059 if (TREE_CODE (decl) == FUNCTION_DECL)
faae18ab 8060 ;
67d743fe
MS
8061 else if (DECL_EXTERNAL (decl)
8062 && ! (DECL_LANG_SPECIFIC (decl)
8063 && DECL_NOT_REALLY_EXTERN (decl)))
5566b478
MS
8064 {
8065 if (init)
8066 DECL_INITIAL (decl) = init;
8067 }
8d08fdba
MS
8068 else if (TREE_STATIC (decl) && type != error_mark_node)
8069 {
8070 /* Cleanups for static variables are handled by `finish_file'. */
f30432d7
MS
8071 if (TYPE_NEEDS_CONSTRUCTING (type) || init != NULL_TREE
8072 || TYPE_NEEDS_DESTRUCTOR (type))
8d08fdba 8073 expand_static_init (decl, init);
8d08fdba
MS
8074 }
8075 else if (! toplev)
8076 {
8077 /* This is a declared decl which must live until the
8078 end of the binding contour. It may need a cleanup. */
8079
8080 /* Recompute the RTL of a local array now
8081 if it used to be an incomplete type. */
8082 if (was_incomplete && ! TREE_STATIC (decl))
8083 {
8084 /* If we used it already as memory, it must stay in memory. */
8085 TREE_ADDRESSABLE (decl) = TREE_USED (decl);
8086 /* If it's still incomplete now, no init will save it. */
8087 if (DECL_SIZE (decl) == NULL_TREE)
8088 DECL_INITIAL (decl) = NULL_TREE;
8089 expand_decl (decl);
8090 }
8091 else if (! TREE_ASM_WRITTEN (decl)
8092 && (TYPE_SIZE (type) != NULL_TREE
8093 || TREE_CODE (type) == ARRAY_TYPE))
8094 {
8095 /* Do this here, because we did not expand this decl's
8096 rtl in start_decl. */
8097 if (DECL_RTL (decl) == NULL_RTX)
8098 expand_decl (decl);
8099 else if (cleanup)
8100 {
8d2733ca
MS
8101 /* XXX: Why don't we use decl here? */
8102 /* Ans: Because it was already expanded? */
e349ee73 8103 if (! expand_decl_cleanup (NULL_TREE, cleanup))
8251199e 8104 cp_error ("parser lost in parsing declaration of `%D'",
8d2733ca 8105 decl);
8d08fdba
MS
8106 /* Cleanup used up here. */
8107 cleanup = NULL_TREE;
8108 }
8109 }
8110
2ee887f2
MS
8111 if (current_binding_level->is_for_scope)
8112 {
f181d4ae
MM
8113 struct binding_level *outer
8114 = current_binding_level->level_chain;
2ee887f2
MS
8115
8116 /* Check to see if the same name is already bound at
8117 the outer level, either because it was directly declared,
8118 or because a dead for-decl got preserved. In either case,
d22c8596 8119 the code would not have been valid under the ARM
2ee887f2
MS
8120 scope rules, so clear is_for_scope for the
8121 current_binding_level.
8122
8123 Otherwise, we need to preserve the temp slot for decl
e92cc029 8124 to last into the outer binding level. */
2ee887f2 8125
f181d4ae
MM
8126 tree outer_binding
8127 = TREE_CHAIN (IDENTIFIER_BINDING (DECL_NAME (decl)));
8128
8129 if (outer_binding && BINDING_LEVEL (outer_binding) == outer
8130 && (TREE_CODE (BINDING_VALUE (outer_binding))
8131 == VAR_DECL)
8132 && DECL_DEAD_FOR_LOCAL (BINDING_VALUE (outer_binding)))
2ee887f2 8133 {
f181d4ae
MM
8134 BINDING_VALUE (outer_binding)
8135 = DECL_SHADOWED_FOR_VAR (BINDING_VALUE (outer_binding));
8136 current_binding_level->is_for_scope = 0;
2ee887f2 8137 }
f181d4ae
MM
8138 else if (DECL_IN_MEMORY_P (decl))
8139 preserve_temp_slots (DECL_RTL (decl));
2ee887f2
MS
8140 }
8141
eb66be0e 8142 expand_start_target_temps ();
72b7eeff 8143
8d08fdba
MS
8144 if (DECL_SIZE (decl) && type != error_mark_node)
8145 {
8146 /* Compute and store the initial value. */
8147 expand_decl_init (decl);
0c4b14c4 8148 already_used = TREE_USED (decl) || TREE_USED (type);
8d08fdba
MS
8149
8150 if (init || TYPE_NEEDS_CONSTRUCTING (type))
8151 {
a28e3c7f
MS
8152 emit_line_note (DECL_SOURCE_FILE (decl),
8153 DECL_SOURCE_LINE (decl));
b370501f 8154 expand_aggr_init (decl, init, flags);
8d08fdba
MS
8155 }
8156
00595019
MS
8157 /* Set this to 0 so we can tell whether an aggregate which
8158 was initialized was ever used. Don't do this if it has a
8159 destructor, so we don't complain about the 'resource
8160 allocation is initialization' idiom. */
249555b0 8161 /* Now set attribute((unused)) on types so decls of
38e01259 8162 that type will be marked used. (see TREE_USED, above.)
249555b0
BK
8163 This avoids the warning problems this particular code
8164 tried to work around. */
0c4b14c4 8165
be99da77 8166 if (TYPE_NEEDS_CONSTRUCTING (type)
0c4b14c4 8167 && ! already_used
be99da77
MS
8168 && cleanup == NULL_TREE
8169 && DECL_NAME (decl))
8d08fdba 8170 TREE_USED (decl) = 0;
0c4b14c4
JM
8171
8172 if (already_used)
8173 TREE_USED (decl) = 1;
934c6b13 8174 }
eb66be0e 8175
934c6b13 8176 /* Cleanup any temporaries needed for the initial value. */
eb66be0e 8177 expand_end_target_temps ();
8d08fdba 8178
934c6b13
MS
8179 if (DECL_SIZE (decl) && type != error_mark_node)
8180 {
8d08fdba
MS
8181 /* Store the cleanup, if there was one. */
8182 if (cleanup)
8183 {
e349ee73 8184 if (! expand_decl_cleanup (decl, cleanup))
8251199e 8185 cp_error ("parser lost in parsing declaration of `%D'",
a28e3c7f 8186 decl);
8d08fdba
MS
8187 }
8188 }
8189 }
8190 finish_end0:
8191
8192 /* Undo call to `pushclass' that was done in `start_decl'
8193 due to initialization of qualified member variable.
8194 I.e., Foo::x = 10; */
8195 {
f30432d7 8196 tree context = DECL_REAL_CONTEXT (decl);
8d08fdba
MS
8197 if (context
8198 && TREE_CODE_CLASS (TREE_CODE (context)) == 't'
8199 && (TREE_CODE (decl) == VAR_DECL
8200 /* We also have a pushclass done that we need to undo here
8201 if we're at top level and declare a method. */
5566b478
MS
8202 || TREE_CODE (decl) == FUNCTION_DECL)
8203 /* If size hasn't been set, we're still defining it,
8204 and therefore inside the class body; don't pop
8205 the binding level.. */
8206 && TYPE_SIZE (context) != NULL_TREE
8207 && context == current_class_type)
6b400b21 8208 pop_nested_class ();
8d08fdba
MS
8209 }
8210 }
8211
8212 finish_end:
8213
39211cd5
MS
8214 /* If requested, warn about definitions of large data objects. */
8215
8216 if (warn_larger_than
5156628f 8217 && ! processing_template_decl
39211cd5
MS
8218 && (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL)
8219 && !DECL_EXTERNAL (decl))
8220 {
8221 register tree decl_size = DECL_SIZE (decl);
8222
8223 if (decl_size && TREE_CODE (decl_size) == INTEGER_CST)
8224 {
8225 unsigned units = TREE_INT_CST_LOW (decl_size) / BITS_PER_UNIT;
8226
8227 if (units > larger_than_size)
8228 warning_with_decl (decl, "size of `%s' is %u bytes", units);
8229 }
8230 }
8231
8d08fdba
MS
8232 if (need_pop)
8233 {
8234 /* Resume permanent allocation, if not within a function. */
8235 /* The corresponding push_obstacks_nochange is in start_decl,
8236 start_method, groktypename, and in grokfield. */
8237 pop_obstacks ();
8238 }
8239
8240 if (was_readonly)
8241 TREE_READONLY (decl) = 1;
8d08fdba
MS
8242}
8243
82580166 8244/* This is here for a midend callback from c-common.c */
e92cc029 8245
82580166
MS
8246void
8247finish_decl (decl, init, asmspec_tree)
8248 tree decl, init;
8249 tree asmspec_tree;
8250{
8251 cp_finish_decl (decl, init, asmspec_tree, 1, 0);
8252}
8253
8d08fdba
MS
8254void
8255expand_static_init (decl, init)
8256 tree decl;
8257 tree init;
8258{
8259 tree oldstatic = value_member (decl, static_aggregates);
a4443a08 8260
ea735e02
JM
8261 /* If at_eof is 2, we're too late. */
8262 my_friendly_assert (at_eof <= 1, 990323);
8263
8d08fdba
MS
8264 if (oldstatic)
8265 {
8266 if (TREE_PURPOSE (oldstatic) && init != NULL_TREE)
8251199e 8267 cp_error ("multiple initializations given for `%D'", decl);
8d08fdba 8268 }
a9aedbc2 8269 else if (! toplevel_bindings_p () && ! pseudo_global_level_p ())
8d08fdba
MS
8270 {
8271 /* Emit code to perform this initialization but once. */
8272 tree temp;
8273
e92cc029 8274 /* Remember this information until end of file. */
8d08fdba
MS
8275 push_obstacks (&permanent_obstack, &permanent_obstack);
8276
2036a15c
MM
8277 /* Emit code to perform this initialization but once. This code
8278 looks like:
8279
8280 static int temp = 0;
8281 if (!temp) {
8282 // Do initialization.
8283 temp = 1;
8284 // Register variable for destruction at end of program.
8285 }
8286
8287 Note that the `temp' variable is only set to 1 *after* the
8288 initialization is complete. This ensures that an exception,
8289 thrown during the construction, will cause the variable to
8290 reinitialized when we pass through this code again, as per:
8291
8292 [stmt.dcl]
8293
8294 If the initialization exits by throwing an exception, the
8295 initialization is not complete, so it will be tried again
8296 the next time control enters the declaration.
8297
8298 In theory, this process should be thread-safe, too; multiple
8299 threads should not be able to initialize the variable more
8300 than once. We don't yet attempt to ensure thread-safety. */
8d08fdba
MS
8301 temp = get_temp_name (integer_type_node, 1);
8302 rest_of_decl_compilation (temp, NULL_PTR, 0, 0);
2036a15c
MM
8303
8304 /* Begin the conditional initialization. */
8d08fdba 8305 expand_start_cond (build_binary_op (EQ_EXPR, temp,
337c90cc 8306 integer_zero_node), 0);
eb66be0e 8307 expand_start_target_temps ();
72b7eeff 8308
2036a15c 8309 /* Do the initialization itself. */
28cbf42c 8310 if (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl))
f30432d7 8311 || (init && TREE_CODE (init) == TREE_LIST))
8d08fdba 8312 {
b370501f 8313 expand_aggr_init (decl, init, 0);
8d08fdba
MS
8314 do_pending_stack_adjust ();
8315 }
f30432d7 8316 else if (init)
8d08fdba 8317 expand_assignment (decl, init, 0, 0);
f30432d7 8318
2036a15c
MM
8319 /* Set TEMP to 1. */
8320 expand_assignment (temp, integer_one_node, 0, 0);
8321
8322 /* Cleanup any temporaries needed for the initial value. If
8323 destroying one of the temporaries causes an exception to be
8324 thrown, then the object itself has still been fully
8325 constructed. */
eb66be0e 8326 expand_end_target_temps ();
72b7eeff 8327
2036a15c
MM
8328 /* Use atexit to register a function for destroying this static
8329 variable. */
72b7eeff
MS
8330 if (TYPE_NEEDS_DESTRUCTOR (TREE_TYPE (decl)))
8331 {
8332 tree cleanup, fcall;
8333 static tree Atexit = 0;
8334 if (Atexit == 0)
8335 {
8336 tree atexit_fndecl, PFV, pfvlist;
e92cc029 8337 /* Remember this information until end of file. */
72b7eeff
MS
8338 push_obstacks (&permanent_obstack, &permanent_obstack);
8339 PFV = build_pointer_type (build_function_type
8340 (void_type_node, void_list_node));
8341
8342 pfvlist = tree_cons (NULL_TREE, PFV, void_list_node);
8343
8344 push_lang_context (lang_name_c);
beb53fb8
JM
8345 atexit_fndecl
8346 = builtin_function ("atexit",
8347 build_function_type (void_type_node,
8348 pfvlist),
8349 NOT_BUILT_IN, NULL_PTR);
a6ecf8b6 8350 mark_used (atexit_fndecl);
72b7eeff
MS
8351 Atexit = default_conversion (atexit_fndecl);
8352 pop_lang_context ();
8353 pop_obstacks ();
8354 }
8355
7cfac595
MM
8356 /* Call build_cleanup before we enter the anonymous function
8357 so that any access checks will be done relative to the
8358 current scope, rather than the scope of the anonymous
8359 function. */
8360 fcall = build_cleanup (decl);
72b7eeff 8361 cleanup = start_anon_func ();
7cfac595 8362 expand_expr_stmt (fcall);
72b7eeff
MS
8363 end_anon_func ();
8364 mark_addressable (cleanup);
8365 cleanup = build_unary_op (ADDR_EXPR, cleanup, 0);
e66d884e 8366 fcall = build_function_call (Atexit, expr_tree_cons (NULL_TREE, cleanup, NULL_TREE));
72b7eeff
MS
8367 expand_expr_stmt (fcall);
8368 }
8369
8d08fdba
MS
8370 expand_end_cond ();
8371 if (TYPE_NEEDS_DESTRUCTOR (TREE_TYPE (decl)))
8372 {
8373 static_aggregates = perm_tree_cons (temp, decl, static_aggregates);
8374 TREE_STATIC (static_aggregates) = 1;
8375 }
8376
e92cc029 8377 /* Resume old (possibly temporary) allocation. */
8d08fdba
MS
8378 pop_obstacks ();
8379 }
8380 else
8381 {
8382 /* This code takes into account memory allocation
8383 policy of `start_decl'. Namely, if TYPE_NEEDS_CONSTRUCTING
8384 does not hold for this object, then we must make permanent
8385 the storage currently in the temporary obstack. */
8386 if (! TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl)))
8387 preserve_initializer ();
8388 static_aggregates = perm_tree_cons (init, decl, static_aggregates);
8389 }
8390}
8391\f
8392/* Make TYPE a complete type based on INITIAL_VALUE.
8393 Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
8394 2 if there was no information (in which case assume 1 if DO_DEFAULT). */
8395
8396int
8397complete_array_type (type, initial_value, do_default)
8398 tree type, initial_value;
8399 int do_default;
8400{
8401 register tree maxindex = NULL_TREE;
8402 int value = 0;
8403
8404 if (initial_value)
8405 {
8406 /* Note MAXINDEX is really the maximum index,
8407 one less than the size. */
8408 if (TREE_CODE (initial_value) == STRING_CST)
e1cd6e56
MS
8409 {
8410 int eltsize
8411 = int_size_in_bytes (TREE_TYPE (TREE_TYPE (initial_value)));
8412 maxindex = build_int_2 ((TREE_STRING_LENGTH (initial_value)
8413 / eltsize) - 1, 0);
8414 }
8d08fdba
MS
8415 else if (TREE_CODE (initial_value) == CONSTRUCTOR)
8416 {
e1cd6e56
MS
8417 tree elts = CONSTRUCTOR_ELTS (initial_value);
8418 maxindex = size_binop (MINUS_EXPR, integer_zero_node, size_one_node);
8419 for (; elts; elts = TREE_CHAIN (elts))
8420 {
8421 if (TREE_PURPOSE (elts))
8422 maxindex = TREE_PURPOSE (elts);
8423 else
8424 maxindex = size_binop (PLUS_EXPR, maxindex, size_one_node);
8425 }
8426 maxindex = copy_node (maxindex);
8d08fdba
MS
8427 }
8428 else
8429 {
8430 /* Make an error message unless that happened already. */
8431 if (initial_value != error_mark_node)
8432 value = 1;
0db982be
ML
8433 else
8434 initial_value = NULL_TREE;
8d08fdba
MS
8435
8436 /* Prevent further error messages. */
8437 maxindex = build_int_2 (0, 0);
8438 }
8439 }
8440
8441 if (!maxindex)
8442 {
8443 if (do_default)
8444 maxindex = build_int_2 (0, 0);
8445 value = 2;
8446 }
8447
8448 if (maxindex)
8449 {
51c184be
MS
8450 tree itype;
8451
8d08fdba 8452 TYPE_DOMAIN (type) = build_index_type (maxindex);
dff6b454 8453 if (! TREE_TYPE (maxindex))
8d08fdba 8454 TREE_TYPE (maxindex) = TYPE_DOMAIN (type);
51c184be
MS
8455 if (initial_value)
8456 itype = TREE_TYPE (initial_value);
8457 else
8458 itype = NULL;
8459 if (itype && !TYPE_DOMAIN (itype))
8460 TYPE_DOMAIN (itype) = TYPE_DOMAIN (type);
dff6b454
RK
8461 /* The type of the main variant should never be used for arrays
8462 of different sizes. It should only ever be completed with the
8463 size of the array. */
8464 if (! TYPE_DOMAIN (TYPE_MAIN_VARIANT (type)))
8465 TYPE_DOMAIN (TYPE_MAIN_VARIANT (type)) = TYPE_DOMAIN (type);
8d08fdba
MS
8466 }
8467
8468 /* Lay out the type now that we can get the real answer. */
8469
8470 layout_type (type);
8471
8472 return value;
8473}
8474\f
8475/* Return zero if something is declared to be a member of type
8476 CTYPE when in the context of CUR_TYPE. STRING is the error
8477 message to print in that case. Otherwise, quietly return 1. */
e92cc029 8478
8d08fdba
MS
8479static int
8480member_function_or_else (ctype, cur_type, string)
8481 tree ctype, cur_type;
d8e178a0 8482 const char *string;
8d08fdba
MS
8483{
8484 if (ctype && ctype != cur_type)
8485 {
8486 error (string, TYPE_NAME_STRING (ctype));
8487 return 0;
8488 }
8489 return 1;
8490}
8491\f
8492/* Subroutine of `grokdeclarator'. */
8493
8494/* Generate errors possibly applicable for a given set of specifiers.
8495 This is for ARM $7.1.2. */
e92cc029 8496
8d08fdba
MS
8497static void
8498bad_specifiers (object, type, virtualp, quals, inlinep, friendp, raises)
8499 tree object;
d8e178a0 8500 const char *type;
8d08fdba
MS
8501 int virtualp, quals, friendp, raises, inlinep;
8502{
8503 if (virtualp)
8251199e 8504 cp_error ("`%D' declared as a `virtual' %s", object, type);
8d08fdba 8505 if (inlinep)
8251199e 8506 cp_error ("`%D' declared as an `inline' %s", object, type);
8d08fdba 8507 if (quals)
8251199e 8508 cp_error ("`const' and `volatile' function specifiers on `%D' invalid in %s declaration",
a28e3c7f 8509 object, type);
8d08fdba 8510 if (friendp)
8251199e 8511 cp_error_at ("invalid friend declaration", object);
8d08fdba 8512 if (raises)
8251199e 8513 cp_error_at ("invalid exception specifications", object);
8d08fdba
MS
8514}
8515
8516/* CTYPE is class type, or null if non-class.
8517 TYPE is type this FUNCTION_DECL should have, either FUNCTION_TYPE
8518 or METHOD_TYPE.
8519 DECLARATOR is the function's name.
8520 VIRTUALP is truthvalue of whether the function is virtual or not.
8521 FLAGS are to be passed through to `grokclassfn'.
8522 QUALS are qualifiers indicating whether the function is `const'
8523 or `volatile'.
8524 RAISES is a list of exceptions that this function can raise.
8525 CHECK is 1 if we must find this method in CTYPE, 0 if we should
3ddfb0e6
MM
8526 not look, and -1 if we should not call `grokclassfn' at all.
8527
20496fa2 8528 Returns `NULL_TREE' if something goes wrong, after issuing
3ddfb0e6 8529 applicable error messages. */
e92cc029 8530
8d08fdba 8531static tree
386b8a85
JM
8532grokfndecl (ctype, type, declarator, orig_declarator, virtualp, flags, quals,
8533 raises, attrlist, check, friendp, publicp, inlinep, funcdef_flag,
2c73f9f5 8534 template_count, in_namespace)
8d08fdba
MS
8535 tree ctype, type;
8536 tree declarator;
386b8a85 8537 tree orig_declarator;
8d08fdba
MS
8538 int virtualp;
8539 enum overload_flags flags;
f30432d7 8540 tree quals, raises, attrlist;
386b8a85 8541 int check, friendp, publicp, inlinep, funcdef_flag, template_count;
2c73f9f5 8542 tree in_namespace;
8d08fdba
MS
8543{
8544 tree cname, decl;
8545 int staticp = ctype && TREE_CODE (type) == FUNCTION_TYPE;
42976354 8546 tree t;
8d08fdba
MS
8547
8548 if (ctype)
8549 cname = TREE_CODE (TYPE_NAME (ctype)) == TYPE_DECL
8550 ? TYPE_IDENTIFIER (ctype) : TYPE_NAME (ctype);
8551 else
8552 cname = NULL_TREE;
8553
8554 if (raises)
8555 {
f30432d7 8556 type = build_exception_variant (type, raises);
8d08fdba 8557 }
c11b6f21 8558
8d08fdba 8559 decl = build_lang_decl (FUNCTION_DECL, declarator, type);
2c73f9f5 8560 /* Propagate volatile out from type to decl. */
8d08fdba 8561 if (TYPE_VOLATILE (type))
893de33c 8562 TREE_THIS_VOLATILE (decl) = 1;
8d08fdba 8563
79c4d4b7 8564 /* If this decl has namespace scope, set that up. */
2c73f9f5 8565 if (in_namespace)
b262d64c 8566 set_decl_namespace (decl, in_namespace, friendp);
79c4d4b7
JM
8567 else if (publicp && ! ctype)
8568 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
2c73f9f5 8569
0f8766b8
JM
8570 /* `main' and builtins have implicit 'C' linkage. */
8571 if ((MAIN_NAME_P (declarator)
8572 || (IDENTIFIER_LENGTH (declarator) > 10
8573 && IDENTIFIER_POINTER (declarator)[0] == '_'
8574 && IDENTIFIER_POINTER (declarator)[1] == '_'
8575 && strncmp (IDENTIFIER_POINTER (declarator)+2, "builtin_", 8) == 0))
8576 && current_lang_name == lang_name_cplusplus
94706a5c 8577 && ctype == NULL_TREE
79c4d4b7
JM
8578 /* NULL_TREE means global namespace. */
8579 && DECL_CONTEXT (decl) == NULL_TREE)
0f8766b8
JM
8580 DECL_LANGUAGE (decl) = lang_c;
8581
8d08fdba
MS
8582 /* Should probably propagate const out from type to decl I bet (mrs). */
8583 if (staticp)
8584 {
8585 DECL_STATIC_FUNCTION_P (decl) = 1;
8586 DECL_CONTEXT (decl) = ctype;
8d08fdba
MS
8587 }
8588
e76a2646
MS
8589 if (ctype)
8590 DECL_CLASS_CONTEXT (decl) = ctype;
8591
0f8766b8 8592 if (ctype == NULL_TREE && DECL_MAIN_P (decl))
faae18ab 8593 {
848b92e1
JM
8594 if (processing_template_decl)
8595 error ("cannot declare `main' to be a template");
faae18ab 8596 if (inlinep)
8251199e 8597 error ("cannot declare `main' to be inline");
faae18ab 8598 else if (! publicp)
8251199e 8599 error ("cannot declare `main' to be static");
faae18ab
MS
8600 inlinep = 0;
8601 publicp = 1;
8602 }
50a6dbd7 8603
59e76fc6
JM
8604 /* Members of anonymous types and local classes have no linkage; make
8605 them internal. */
8606 if (ctype && (ANON_AGGRNAME_P (TYPE_IDENTIFIER (ctype))
8607 || hack_decl_function_context (TYPE_MAIN_DECL (ctype))))
50a6dbd7
JM
8608 publicp = 0;
8609
8610 if (publicp)
8611 {
8612 /* [basic.link]: A name with no linkage (notably, the name of a class
8613 or enumeration declared in a local scope) shall not be used to
8614 declare an entity with linkage.
8615
8616 Only check this for public decls for now. */
8617 t = no_linkage_check (TREE_TYPE (decl));
8618 if (t)
8619 {
7f7c930e
JM
8620 if (ANON_AGGRNAME_P (TYPE_IDENTIFIER (t)))
8621 {
8622 if (DECL_LANGUAGE (decl) == lang_c)
8623 /* Allow this; it's pretty common in C. */;
8624 else
8625 cp_pedwarn ("non-local function `%#D' uses anonymous type",
8626 decl);
8627 }
50a6dbd7 8628 else
8251199e 8629 cp_pedwarn ("non-local function `%#D' uses local type `%T'",
cce2be43 8630 decl, t);
50a6dbd7
JM
8631 }
8632 }
8633
893de33c 8634 TREE_PUBLIC (decl) = publicp;
faae18ab 8635 if (! publicp)
893de33c
JM
8636 {
8637 DECL_INTERFACE_KNOWN (decl) = 1;
8638 DECL_NOT_REALLY_EXTERN (decl) = 1;
8639 }
faae18ab
MS
8640
8641 if (inlinep)
8642 DECL_THIS_INLINE (decl) = DECL_INLINE (decl) = 1;
8d08fdba
MS
8643
8644 DECL_EXTERNAL (decl) = 1;
8645 if (quals != NULL_TREE && TREE_CODE (type) == FUNCTION_TYPE)
8646 {
8251199e 8647 cp_error ("%smember function `%D' cannot have `%T' method qualifier",
8d08fdba
MS
8648 (ctype ? "static " : "non-"), decl, TREE_VALUE (quals));
8649 quals = NULL_TREE;
8650 }
8651
8652 if (IDENTIFIER_OPNAME_P (DECL_NAME (decl)))
8653 grok_op_properties (decl, virtualp, check < 0);
8654
e76a2646 8655 if (ctype && hack_decl_function_context (decl))
893de33c 8656 DECL_NO_STATIC_CHAIN (decl) = 1;
e76a2646 8657
42976354
BK
8658 for (t = TYPE_ARG_TYPES (TREE_TYPE (decl)); t; t = TREE_CHAIN (t))
8659 if (TREE_PURPOSE (t)
8660 && TREE_CODE (TREE_PURPOSE (t)) == DEFAULT_ARG)
8661 {
8662 add_defarg_fn (decl);
8663 break;
8664 }
8665
f9d94ea4
JM
8666 if (friendp
8667 && TREE_CODE (orig_declarator) == TEMPLATE_ID_EXPR)
8668 {
8669 if (funcdef_flag)
8251199e
JM
8670 cp_error
8671 ("defining explicit specialization `%D' in friend declaration",
f9d94ea4
JM
8672 orig_declarator);
8673 else
8674 {
7e2421f7
MM
8675 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
8676 {
8677 /* Something like `template <class T> friend void f<T>()'. */
8251199e 8678 cp_error ("template-id `%D' in declaration of primary template",
7e2421f7 8679 orig_declarator);
20496fa2 8680 return NULL_TREE;
7e2421f7
MM
8681 }
8682
f9d94ea4
JM
8683 /* A friend declaration of the form friend void f<>(). Record
8684 the information in the TEMPLATE_ID_EXPR. */
8685 SET_DECL_IMPLICIT_INSTANTIATION (decl);
8686 DECL_TEMPLATE_INFO (decl)
8687 = perm_tree_cons (TREE_OPERAND (orig_declarator, 0),
8688 TREE_OPERAND (orig_declarator, 1),
8689 NULL_TREE);
8690 }
f84b4be9 8691 }
386b8a85 8692
0f8766b8
JM
8693 /* Plain overloading: will not be grok'd by grokclassfn. */
8694 if (! ctype && ! processing_template_decl
8695 && DECL_LANGUAGE (decl) != lang_c
8696 && (! DECL_USE_TEMPLATE (decl) || name_mangling_version < 1))
8697 set_mangled_name_for_decl (decl);
8698
1eb0072d
JM
8699 if (funcdef_flag)
8700 /* Make the init_value nonzero so pushdecl knows this is not
8701 tentative. error_mark_node is replaced later with the BLOCK. */
8702 DECL_INITIAL (decl) = error_mark_node;
8703
419c6212
JM
8704 if (attrlist)
8705 cplus_decl_attributes (decl, TREE_PURPOSE (attrlist),
8706 TREE_VALUE (attrlist));
8707
75650646 8708 /* Caller will do the rest of this. */
8d08fdba
MS
8709 if (check < 0)
8710 return decl;
8711
5566b478 8712 if (check && funcdef_flag)
d2e5ee5c 8713 DECL_INITIAL (decl) = error_mark_node;
5566b478 8714
8d08fdba
MS
8715 if (flags == NO_SPECIAL && ctype && constructor_name (cname) == declarator)
8716 {
8717 tree tmp;
8718 /* Just handle constructors here. We could do this
8719 inside the following if stmt, but I think
8720 that the code is more legible by breaking this
8721 case out. See comments below for what each of
8722 the following calls is supposed to do. */
8723 DECL_CONSTRUCTOR_P (decl) = 1;
8724
b370501f 8725 grokclassfn (ctype, decl, flags, quals);
386b8a85 8726
e1467ff2
MM
8727 decl = check_explicit_specialization (orig_declarator, decl,
8728 template_count,
f84b4be9
JM
8729 2 * (funcdef_flag != 0) +
8730 4 * (friendp != 0));
6c30752f 8731 if (decl == error_mark_node)
20496fa2 8732 return NULL_TREE;
75650646 8733
b370501f 8734 if ((! TYPE_FOR_JAVA (ctype) || check_java_method (decl))
eff71ab0 8735 && check)
5566b478
MS
8736 {
8737 tmp = check_classfn (ctype, decl);
98c1c668
JM
8738
8739 if (tmp && TREE_CODE (tmp) == TEMPLATE_DECL)
8740 tmp = DECL_TEMPLATE_RESULT(tmp);
8741
e349ee73 8742 if (tmp && DECL_ARTIFICIAL (tmp))
8251199e 8743 cp_error ("definition of implicitly-declared `%D'", tmp);
5566b478
MS
8744 if (tmp && duplicate_decls (decl, tmp))
8745 return tmp;
8746 }
a0a33927 8747 if (! grok_ctor_properties (ctype, decl))
20496fa2 8748 return NULL_TREE;
8d08fdba
MS
8749 }
8750 else
8751 {
8752 tree tmp;
8753
8754 /* Function gets the ugly name, field gets the nice one.
8755 This call may change the type of the function (because
8756 of default parameters)! */
8757 if (ctype != NULL_TREE)
b370501f 8758 grokclassfn (ctype, decl, flags, quals);
8d08fdba 8759
e1467ff2
MM
8760 decl = check_explicit_specialization (orig_declarator, decl,
8761 template_count,
f84b4be9
JM
8762 2 * (funcdef_flag != 0) +
8763 4 * (friendp != 0));
6c30752f 8764 if (decl == error_mark_node)
20496fa2 8765 return NULL_TREE;
6c30752f 8766
eff71ab0 8767 if (ctype != NULL_TREE
b370501f 8768 && (! TYPE_FOR_JAVA (ctype) || check_java_method (decl))
eff71ab0 8769 && check)
5566b478
MS
8770 {
8771 tmp = check_classfn (ctype, decl);
98c1c668
JM
8772
8773 if (tmp && TREE_CODE (tmp) == TEMPLATE_DECL)
75650646 8774 tmp = DECL_TEMPLATE_RESULT (tmp);
98c1c668 8775
5566b478
MS
8776 if (tmp && DECL_STATIC_FUNCTION_P (tmp)
8777 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE)
8778 {
8779 /* Remove the `this' parm added by grokclassfn.
8780 XXX Isn't this done in start_function, too? */
8781 revert_static_member_fn (&decl, NULL, NULL);
8782 last_function_parms = TREE_CHAIN (last_function_parms);
8783 }
e349ee73 8784 if (tmp && DECL_ARTIFICIAL (tmp))
8251199e 8785 cp_error ("definition of implicitly-declared `%D'", tmp);
7834ab39
MS
8786 if (tmp)
8787 {
03017874
MM
8788 /* Attempt to merge the declarations. This can fail, in
8789 the case of some illegal specialization declarations. */
7834ab39 8790 if (!duplicate_decls (decl, tmp))
03017874
MM
8791 cp_error ("no `%#D' member function declared in class `%T'",
8792 decl, ctype);
7834ab39
MS
8793 return tmp;
8794 }
5566b478 8795 }
8d08fdba
MS
8796
8797 if (ctype == NULL_TREE || check)
8798 return decl;
8799
7cfac595
MM
8800 make_decl_rtl (decl, NULL_PTR, 1);
8801
8d08fdba
MS
8802 if (virtualp)
8803 {
2ee887f2 8804 DECL_VIRTUAL_P (decl) = 1;
8d08fdba
MS
8805 if (DECL_VINDEX (decl) == NULL_TREE)
8806 DECL_VINDEX (decl) = error_mark_node;
8807 IDENTIFIER_VIRTUAL_P (DECL_NAME (decl)) = 1;
8d08fdba
MS
8808 }
8809 }
8810 return decl;
8811}
8812
8813static tree
2c73f9f5 8814grokvardecl (type, declarator, specbits_in, initialized, constp, in_namespace)
8d08fdba
MS
8815 tree type;
8816 tree declarator;
d2e5ee5c 8817 RID_BIT_TYPE *specbits_in;
8d08fdba 8818 int initialized;
a9aedbc2 8819 int constp;
2c73f9f5 8820 tree in_namespace;
8d08fdba
MS
8821{
8822 tree decl;
f7da6097
MS
8823 RID_BIT_TYPE specbits;
8824
8825 specbits = *specbits_in;
8d08fdba
MS
8826
8827 if (TREE_CODE (type) == OFFSET_TYPE)
8828 {
8829 /* If you declare a static member so that it
8830 can be initialized, the code will reach here. */
5b605f68
MS
8831 tree basetype = TYPE_OFFSET_BASETYPE (type);
8832 type = TREE_TYPE (type);
8833 decl = build_lang_field_decl (VAR_DECL, declarator, type);
8834 DECL_CONTEXT (decl) = basetype;
8835 DECL_CLASS_CONTEXT (decl) = basetype;
f376e137 8836 DECL_ASSEMBLER_NAME (decl) = build_static_name (basetype, declarator);
8d08fdba
MS
8837 }
8838 else
30394414 8839 {
79c4d4b7
JM
8840 tree context;
8841
8842 if (in_namespace)
8843 context = in_namespace;
8844 else if (namespace_bindings_p () || RIDBIT_SETP (RID_EXTERN, specbits))
8845 context = current_namespace;
820fcad8
JM
8846 else
8847 context = NULL_TREE;
79c4d4b7 8848
30394414 8849 decl = build_decl (VAR_DECL, declarator, complete_type (type));
79c4d4b7
JM
8850
8851 if (context)
b262d64c 8852 set_decl_namespace (decl, context, 0);
79c4d4b7
JM
8853
8854 context = DECL_CONTEXT (decl);
8855 if (declarator && context && current_lang_name != lang_name_c)
8856 DECL_ASSEMBLER_NAME (decl) = build_static_name (context, declarator);
30394414 8857 }
6060a796 8858
2c73f9f5 8859 if (in_namespace)
b262d64c 8860 set_decl_namespace (decl, in_namespace, 0);
2c73f9f5 8861
8d08fdba
MS
8862 if (RIDBIT_SETP (RID_EXTERN, specbits))
8863 {
8864 DECL_THIS_EXTERN (decl) = 1;
8865 DECL_EXTERNAL (decl) = !initialized;
8866 }
8867
8868 /* In class context, static means one per class,
8869 public access, and static storage. */
2b9dc906 8870 if (DECL_CLASS_SCOPE_P (decl))
8d08fdba
MS
8871 {
8872 TREE_PUBLIC (decl) = 1;
8873 TREE_STATIC (decl) = 1;
5b605f68 8874 DECL_EXTERNAL (decl) = 0;
8d08fdba
MS
8875 }
8876 /* At top level, either `static' or no s.c. makes a definition
8877 (perhaps tentative), and absence of `static' makes it public. */
a9aedbc2 8878 else if (toplevel_bindings_p ())
8d08fdba 8879 {
a9aedbc2 8880 TREE_PUBLIC (decl) = (RIDBIT_NOTSETP (RID_STATIC, specbits)
9fffd093 8881 && (DECL_THIS_EXTERN (decl) || ! constp));
8d08fdba
MS
8882 TREE_STATIC (decl) = ! DECL_EXTERNAL (decl);
8883 }
8884 /* Not at top level, only `static' makes a static definition. */
8885 else
8886 {
8887 TREE_STATIC (decl) = !! RIDBIT_SETP (RID_STATIC, specbits);
8888 TREE_PUBLIC (decl) = DECL_EXTERNAL (decl);
8889 }
50a6dbd7
JM
8890
8891 if (TREE_PUBLIC (decl))
8892 {
8893 /* [basic.link]: A name with no linkage (notably, the name of a class
8894 or enumeration declared in a local scope) shall not be used to
8895 declare an entity with linkage.
8896
8897 Only check this for public decls for now. */
8898 tree t = no_linkage_check (TREE_TYPE (decl));
8899 if (t)
8900 {
8901 if (ANON_AGGRNAME_P (TYPE_IDENTIFIER (t)))
8902 /* Ignore for now; `enum { foo } e' is pretty common. */;
8903 else
8251199e 8904 cp_pedwarn ("non-local variable `%#D' uses local type `%T'",
50a6dbd7
JM
8905 decl, t);
8906 }
8907 }
8908
8d08fdba
MS
8909 return decl;
8910}
8911
d8f8dca1
MM
8912/* Create and return a canonical pointer to member function type, for
8913 TYPE, which is a POINTER_TYPE to a METHOD_TYPE. */
8d08fdba
MS
8914
8915tree
8916build_ptrmemfunc_type (type)
8917 tree type;
8918{
8919 tree fields[4];
8920 tree t;
8921 tree u;
8922
8923 /* If a canonical type already exists for this type, use it. We use
8924 this method instead of type_hash_canon, because it only does a
8925 simple equality check on the list of field members. */
8926
8927 if ((t = TYPE_GET_PTRMEMFUNC_TYPE (type)))
8928 return t;
8929
8930 push_obstacks (TYPE_OBSTACK (type), TYPE_OBSTACK (type));
8931
8932 u = make_lang_type (UNION_TYPE);
7ddedda4 8933 SET_IS_AGGR_TYPE (u, 0);
8d08fdba 8934 fields[0] = build_lang_field_decl (FIELD_DECL, pfn_identifier, type);
a28e3c7f
MS
8935 fields[1] = build_lang_field_decl (FIELD_DECL, delta2_identifier,
8936 delta_type_node);
8d08fdba
MS
8937 finish_builtin_type (u, "__ptrmemfunc_type", fields, 1, ptr_type_node);
8938 TYPE_NAME (u) = NULL_TREE;
8939
8940 t = make_lang_type (RECORD_TYPE);
8941
2c73f9f5 8942 /* Let the front-end know this is a pointer to member function... */
db5ae43f 8943 TYPE_PTRMEMFUNC_FLAG (t) = 1;
2c73f9f5 8944 /* ... and not really an aggregate. */
7ddedda4 8945 SET_IS_AGGR_TYPE (t, 0);
8d08fdba 8946
a28e3c7f
MS
8947 fields[0] = build_lang_field_decl (FIELD_DECL, delta_identifier,
8948 delta_type_node);
8949 fields[1] = build_lang_field_decl (FIELD_DECL, index_identifier,
8950 delta_type_node);
8d08fdba
MS
8951 fields[2] = build_lang_field_decl (FIELD_DECL, pfn_or_delta2_identifier, u);
8952 finish_builtin_type (t, "__ptrmemfunc_type", fields, 2, ptr_type_node);
8953
8954 pop_obstacks ();
8955
8956 /* Zap out the name so that the back-end will give us the debugging
8957 information for this anonymous RECORD_TYPE. */
8958 TYPE_NAME (t) = NULL_TREE;
8959
8960 TYPE_SET_PTRMEMFUNC_TYPE (type, t);
8961
e92cc029 8962 /* Seems to be wanted. */
8d08fdba
MS
8963 CLASSTYPE_GOT_SEMICOLON (t) = 1;
8964 return t;
8965}
8966
b17e2870
JM
8967/* DECL is a VAR_DECL defined in-class, whose TYPE is also given.
8968 Check to see that the definition is valid. Issue appropriate error
8969 messages. Return 1 if the definition is particularly bad, or 0
8970 otherwise. */
8971
8972int
8973check_static_variable_definition (decl, type)
8974 tree decl;
8975 tree type;
8976{
8977 /* Motion 10 at San Diego: If a static const integral data member is
8978 initialized with an integral constant expression, the initializer
8979 may appear either in the declaration (within the class), or in
8980 the definition, but not both. If it appears in the class, the
8981 member is a member constant. The file-scope definition is always
8982 required. */
8983 if (CLASS_TYPE_P (type) || TREE_CODE (type) == REFERENCE_TYPE)
8984 {
8985 cp_error ("in-class initialization of static data member of non-integral type `%T'",
8986 type);
8987 /* If we just return the declaration, crashes will sometimes
8988 occur. We therefore return void_type_node, as if this was a
8989 friend declaration, to cause callers to completely ignore
8990 this declaration. */
8991 return 1;
8992 }
8993 else if (!CP_TYPE_CONST_P (type))
8994 cp_error ("ANSI C++ forbids in-class initialization of non-const static member `%D'",
8995 decl);
8996 else if (pedantic && !INTEGRAL_TYPE_P (type))
8997 cp_pedwarn ("ANSI C++ forbids initialization of member constant `%D' of non-integral type `%T'", decl, type);
8998
8999 return 0;
9000}
9001
8d08fdba
MS
9002/* Given declspecs and a declarator,
9003 determine the name and type of the object declared
9004 and construct a ..._DECL node for it.
9005 (In one case we can return a ..._TYPE node instead.
9006 For invalid input we sometimes return 0.)
9007
9008 DECLSPECS is a chain of tree_list nodes whose value fields
9009 are the storage classes and type specifiers.
9010
9011 DECL_CONTEXT says which syntactic context this declaration is in:
9012 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
9013 FUNCDEF for a function definition. Like NORMAL but a few different
9014 error messages in each case. Return value may be zero meaning
9015 this definition is too screwy to try to parse.
9016 MEMFUNCDEF for a function definition. Like FUNCDEF but prepares to
9017 handle member functions (which have FIELD context).
9018 Return value may be zero meaning this definition is too screwy to
9019 try to parse.
9020 PARM for a parameter declaration (either within a function prototype
9021 or before a function body). Make a PARM_DECL, or return void_type_node.
db5ae43f 9022 CATCHPARM for a parameter declaration before a catch clause.
8d08fdba
MS
9023 TYPENAME if for a typename (in a cast or sizeof).
9024 Don't make a DECL node; just return the ..._TYPE node.
9025 FIELD for a struct or union field; make a FIELD_DECL.
9026 BITFIELD for a field with specified width.
9027 INITIALIZED is 1 if the decl has an initializer.
9028
b17e2870
JM
9029 ATTRLIST is a TREE_LIST node with prefix attributes in TREE_VALUE and
9030 normal attributes in TREE_PURPOSE, or NULL_TREE.
9031
8d08fdba
MS
9032 In the TYPENAME case, DECLARATOR is really an absolute declarator.
9033 It may also be so in the PARM case, for a prototype where the
9034 argument type is specified but not the name.
9035
9036 This function is where the complicated C meanings of `static'
9037 and `extern' are interpreted.
9038
9039 For C++, if there is any monkey business to do, the function which
9040 calls this one must do it, i.e., prepending instance variables,
9041 renaming overloaded function names, etc.
9042
9043 Note that for this C++, it is an error to define a method within a class
9044 which does not belong to that class.
9045
9046 Except in the case where SCOPE_REFs are implicitly known (such as
9047 methods within a class being redundantly qualified),
9048 declarations which involve SCOPE_REFs are returned as SCOPE_REFs
9049 (class_name::decl_name). The caller must also deal with this.
9050
9051 If a constructor or destructor is seen, and the context is FIELD,
9052 then the type gains the attribute TREE_HAS_x. If such a declaration
9053 is erroneous, NULL_TREE is returned.
9054
9055 QUALS is used only for FUNCDEF and MEMFUNCDEF cases. For a member
9056 function, these are the qualifiers to give to the `this' pointer.
9057
9058 May return void_type_node if the declarator turned out to be a friend.
9059 See grokfield for details. */
9060
9061enum return_types { return_normal, return_ctor, return_dtor, return_conversion };
9062
9063tree
c11b6f21 9064grokdeclarator (declarator, declspecs, decl_context, initialized, attrlist)
8d08fdba
MS
9065 tree declspecs;
9066 tree declarator;
9067 enum decl_context decl_context;
9068 int initialized;
c11b6f21 9069 tree attrlist;
8d08fdba
MS
9070{
9071 RID_BIT_TYPE specbits;
9072 int nclasses = 0;
9073 tree spec;
9074 tree type = NULL_TREE;
9075 int longlong = 0;
9076 int constp;
91063b51 9077 int restrictp;
8d08fdba 9078 int volatilep;
91063b51 9079 int type_quals;
db5ae43f 9080 int virtualp, explicitp, friendp, inlinep, staticp;
8d08fdba
MS
9081 int explicit_int = 0;
9082 int explicit_char = 0;
37c46b43 9083 int defaulted_int = 0;
8d08fdba
MS
9084 int opaque_typedef = 0;
9085 tree typedef_decl = NULL_TREE;
9086 char *name;
9087 tree typedef_type = NULL_TREE;
9088 int funcdef_flag = 0;
9089 enum tree_code innermost_code = ERROR_MARK;
9090 int bitfield = 0;
6125f3be
DE
9091#if 0
9092 /* See the code below that used this. */
f6abb50a 9093 tree decl_machine_attr = NULL_TREE;
6125f3be 9094#endif
8d08fdba
MS
9095 /* Set this to error_mark_node for FIELD_DECLs we could not handle properly.
9096 All FIELD_DECLs we build here have `init' put into their DECL_INITIAL. */
9097 tree init = NULL_TREE;
9098
9099 /* Keep track of what sort of function is being processed
9100 so that we can warn about default return values, or explicit
9101 return values which do not match prescribed defaults. */
9102 enum return_types return_type = return_normal;
9103
9104 tree dname = NULL_TREE;
9105 tree ctype = current_class_type;
9106 tree ctor_return_type = NULL_TREE;
9107 enum overload_flags flags = NO_SPECIAL;
8d08fdba 9108 tree quals = NULL_TREE;
c11b6f21 9109 tree raises = NULL_TREE;
386b8a85 9110 int template_count = 0;
2c73f9f5 9111 tree in_namespace = NULL_TREE;
1eab9b56
JM
9112 tree inner_attrs;
9113 int ignore_attrs;
8d08fdba
MS
9114
9115 RIDBIT_RESET_ALL (specbits);
9116 if (decl_context == FUNCDEF)
9117 funcdef_flag = 1, decl_context = NORMAL;
9118 else if (decl_context == MEMFUNCDEF)
9119 funcdef_flag = -1, decl_context = FIELD;
9120 else if (decl_context == BITFIELD)
9121 bitfield = 1, decl_context = FIELD;
9122
8d08fdba
MS
9123 /* Look inside a declarator for the name being declared
9124 and get it as a string, for an error message. */
9125 {
be99da77
MS
9126 tree *next = &declarator;
9127 register tree decl;
8d08fdba
MS
9128 name = NULL;
9129
be99da77
MS
9130 while (next && *next)
9131 {
9132 decl = *next;
9133 switch (TREE_CODE (decl))
8d08fdba 9134 {
52fbc847
JM
9135 case TREE_LIST:
9136 /* For attributes. */
9137 next = &TREE_VALUE (decl);
9138 break;
9139
be99da77
MS
9140 case COND_EXPR:
9141 ctype = NULL_TREE;
9142 next = &TREE_OPERAND (decl, 0);
9143 break;
8d08fdba 9144
2c73f9f5 9145 case BIT_NOT_EXPR: /* For C++ destructors! */
8d08fdba 9146 {
be99da77
MS
9147 tree name = TREE_OPERAND (decl, 0);
9148 tree rename = NULL_TREE;
9149
9150 my_friendly_assert (flags == NO_SPECIAL, 152);
9151 flags = DTOR_FLAG;
9152 return_type = return_dtor;
5566b478
MS
9153 if (TREE_CODE (name) == TYPE_DECL)
9154 TREE_OPERAND (decl, 0) = name = constructor_name (name);
be99da77
MS
9155 my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, 153);
9156 if (ctype == NULL_TREE)
9157 {
9158 if (current_class_type == NULL_TREE)
9159 {
8251199e 9160 error ("destructors must be member functions");
be99da77
MS
9161 flags = NO_SPECIAL;
9162 }
9163 else
9164 {
9165 tree t = constructor_name (current_class_name);
9166 if (t != name)
9167 rename = t;
9168 }
9169 }
8d08fdba 9170 else
be99da77
MS
9171 {
9172 tree t = constructor_name (ctype);
9173 if (t != name)
9174 rename = t;
9175 }
51c184be 9176
be99da77 9177 if (rename)
39211cd5 9178 {
8251199e 9179 cp_error ("destructor `%T' must match class name `%T'",
5566b478 9180 name, rename);
be99da77 9181 TREE_OPERAND (decl, 0) = rename;
39211cd5 9182 }
be99da77 9183 next = &name;
51c184be 9184 }
be99da77 9185 break;
8d08fdba 9186
be99da77 9187 case ADDR_EXPR: /* C++ reference declaration */
2c73f9f5 9188 /* Fall through. */
be99da77
MS
9189 case ARRAY_REF:
9190 case INDIRECT_REF:
9191 ctype = NULL_TREE;
9192 innermost_code = TREE_CODE (decl);
9193 next = &TREE_OPERAND (decl, 0);
9194 break;
8d08fdba 9195
be99da77
MS
9196 case CALL_EXPR:
9197 if (parmlist_is_exprlist (TREE_OPERAND (decl, 1)))
8d08fdba 9198 {
b17e2870
JM
9199 /* This is actually a variable declaration using
9200 constructor syntax. We need to call start_decl and
9201 cp_finish_decl so we can get the variable
9202 initialized... */
9203
9204 tree attributes, prefix_attributes;
be99da77
MS
9205
9206 *next = TREE_OPERAND (decl, 0);
9207 init = TREE_OPERAND (decl, 1);
9208
b17e2870
JM
9209 if (attrlist)
9210 {
9211 attributes = TREE_PURPOSE (attrlist);
9212 prefix_attributes = TREE_VALUE (attrlist);
9213 }
9214 else
9215 {
9216 attributes = NULL_TREE;
9217 prefix_attributes = NULL_TREE;
9218 }
9219
9220 decl = start_decl (declarator, declspecs, 1,
9221 attributes, prefix_attributes);
1eb0072d
JM
9222 if (decl)
9223 {
9224 /* Look for __unused__ attribute */
9225 if (TREE_USED (TREE_TYPE (decl)))
9226 TREE_USED (decl) = 1;
9227 finish_decl (decl, init, NULL_TREE);
9228 }
9229 else
9230 cp_error ("invalid declarator");
be99da77 9231 return 0;
8d08fdba 9232 }
be99da77
MS
9233 innermost_code = TREE_CODE (decl);
9234 if (decl_context == FIELD && ctype == NULL_TREE)
9235 ctype = current_class_type;
45537677 9236 if (ctype
c11b6f21 9237 && TREE_OPERAND (decl, 0)
45537677
MS
9238 && (TREE_CODE (TREE_OPERAND (decl, 0)) == TYPE_DECL
9239 && ((DECL_NAME (TREE_OPERAND (decl, 0))
9240 == constructor_name_full (ctype))
9241 || (DECL_NAME (TREE_OPERAND (decl, 0))
9242 == constructor_name (ctype)))))
be99da77
MS
9243 TREE_OPERAND (decl, 0) = constructor_name (ctype);
9244 next = &TREE_OPERAND (decl, 0);
9245 decl = *next;
9246 if (ctype != NULL_TREE
9247 && decl != NULL_TREE && flags != DTOR_FLAG
9248 && decl == constructor_name (ctype))
8d08fdba 9249 {
be99da77
MS
9250 return_type = return_ctor;
9251 ctor_return_type = ctype;
8d08fdba 9252 }
be99da77
MS
9253 ctype = NULL_TREE;
9254 break;
386b8a85
JM
9255
9256 case TEMPLATE_ID_EXPR:
9257 {
9258 tree fns = TREE_OPERAND (decl, 0);
9259
9260 if (TREE_CODE (fns) == LOOKUP_EXPR)
9261 fns = TREE_OPERAND (fns, 0);
9262
8f032717
MM
9263 dname = fns;
9264 if (TREE_CODE (dname) == COMPONENT_REF)
9265 dname = TREE_OPERAND (dname, 1);
9266 if (TREE_CODE (dname) != IDENTIFIER_NODE)
9267 {
9268 my_friendly_assert (is_overloaded_fn (dname),
9269 19990331);
9270 dname = DECL_NAME (get_first_fn (dname));
9271 }
386b8a85 9272 }
2c73f9f5 9273 /* Fall through. */
be99da77
MS
9274
9275 case IDENTIFIER_NODE:
386b8a85
JM
9276 if (TREE_CODE (decl) == IDENTIFIER_NODE)
9277 dname = decl;
9278
be99da77
MS
9279 next = 0;
9280
9281 if (is_rid (dname))
8d08fdba 9282 {
8251199e 9283 cp_error ("declarator-id missing; using reserved word `%D'",
be99da77
MS
9284 dname);
9285 name = IDENTIFIER_POINTER (dname);
8d08fdba 9286 }
be99da77 9287 if (! IDENTIFIER_OPNAME_P (dname)
956d6950 9288 /* GNU/Linux headers use '__op'. Arrgh. */
a703fb38 9289 || (IDENTIFIER_TYPENAME_P (dname) && ! TREE_TYPE (dname)))
be99da77 9290 name = IDENTIFIER_POINTER (dname);
8d08fdba
MS
9291 else
9292 {
be99da77 9293 if (IDENTIFIER_TYPENAME_P (dname))
8d08fdba 9294 {
be99da77
MS
9295 my_friendly_assert (flags == NO_SPECIAL, 154);
9296 flags = TYPENAME_FLAG;
9297 ctor_return_type = TREE_TYPE (dname);
9298 return_type = return_conversion;
8d08fdba 9299 }
be99da77 9300 name = operator_name_string (dname);
8d08fdba 9301 }
be99da77 9302 break;
8d08fdba 9303
be99da77
MS
9304 /* C++ extension */
9305 case SCOPE_REF:
9306 {
9307 /* Perform error checking, and decide on a ctype. */
9308 tree cname = TREE_OPERAND (decl, 0);
9309 if (cname == NULL_TREE)
9310 ctype = NULL_TREE;
2c73f9f5
ML
9311 else if (TREE_CODE (cname) == NAMESPACE_DECL)
9312 {
9313 ctype = NULL_TREE;
9314 in_namespace = TREE_OPERAND (decl, 0);
9315 TREE_OPERAND (decl, 0) = NULL_TREE;
9316 }
be99da77
MS
9317 else if (! is_aggr_type (cname, 1))
9318 TREE_OPERAND (decl, 0) = NULL_TREE;
9319 /* Must test TREE_OPERAND (decl, 1), in case user gives
9320 us `typedef (class::memfunc)(int); memfunc *memfuncptr;' */
9321 else if (TREE_OPERAND (decl, 1)
9322 && TREE_CODE (TREE_OPERAND (decl, 1)) == INDIRECT_REF)
9323 ctype = cname;
73b0fce8
KL
9324 else if (TREE_CODE (cname) == TEMPLATE_TYPE_PARM
9325 || TREE_CODE (cname) == TEMPLATE_TEMPLATE_PARM)
5566b478 9326 {
8251199e 9327 cp_error ("`%T::%D' is not a valid declarator", cname,
5566b478 9328 TREE_OPERAND (decl, 1));
8251199e 9329 cp_error (" perhaps you want `typename %T::%D' to make it a type",
5566b478
MS
9330 cname, TREE_OPERAND (decl, 1));
9331 return void_type_node;
9332 }
be99da77
MS
9333 else if (ctype == NULL_TREE)
9334 ctype = cname;
9335 else if (TREE_COMPLEXITY (decl) == current_class_depth)
9336 TREE_OPERAND (decl, 0) = ctype;
9337 else
9338 {
9339 if (! UNIQUELY_DERIVED_FROM_P (cname, ctype))
9340 {
8251199e 9341 cp_error ("type `%T' is not derived from type `%T'",
be99da77
MS
9342 cname, ctype);
9343 TREE_OPERAND (decl, 0) = NULL_TREE;
9344 }
9345 else
9346 ctype = cname;
9347 }
9348
c91a56d2
MS
9349 if (ctype && TREE_CODE (TREE_OPERAND (decl, 1)) == TYPE_DECL
9350 && ((DECL_NAME (TREE_OPERAND (decl, 1))
9351 == constructor_name_full (ctype))
9352 || (DECL_NAME (TREE_OPERAND (decl, 1))
9353 == constructor_name (ctype))))
be99da77
MS
9354 TREE_OPERAND (decl, 1) = constructor_name (ctype);
9355 next = &TREE_OPERAND (decl, 1);
9356 decl = *next;
9357 if (ctype)
9358 {
9359 if (TREE_CODE (decl) == IDENTIFIER_NODE
9360 && constructor_name (ctype) == decl)
9361 {
9362 return_type = return_ctor;
9363 ctor_return_type = ctype;
9364 }
9365 else if (TREE_CODE (decl) == BIT_NOT_EXPR
9366 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
9367 && (constructor_name (ctype) == TREE_OPERAND (decl, 0)
9368 || constructor_name_full (ctype) == TREE_OPERAND (decl, 0)))
9369 {
9370 return_type = return_dtor;
9371 ctor_return_type = ctype;
9372 flags = DTOR_FLAG;
9373 TREE_OPERAND (decl, 0) = constructor_name (ctype);
9374 next = &TREE_OPERAND (decl, 0);
9375 }
9376 }
9377 }
9378 break;
9379
9380 case ERROR_MARK:
9381 next = 0;
9382 break;
9383
45537677
MS
9384 case TYPE_DECL:
9385 /* Parse error puts this typespec where
9386 a declarator should go. */
8251199e 9387 cp_error ("`%T' specified as declarator-id", DECL_NAME (decl));
45537677 9388 if (TREE_TYPE (decl) == current_class_type)
8251199e 9389 cp_error (" perhaps you want `%T' for a constructor",
45537677
MS
9390 current_class_name);
9391 dname = DECL_NAME (decl);
9392 name = IDENTIFIER_POINTER (dname);
9393
e92cc029 9394 /* Avoid giving two errors for this. */
45537677
MS
9395 IDENTIFIER_CLASS_VALUE (dname) = NULL_TREE;
9396
9397 declspecs = temp_tree_cons (NULL_TREE, integer_type_node,
9398 declspecs);
9399 *next = dname;
9400 next = 0;
9401 break;
9402
be99da77 9403 default:
8251199e 9404 cp_compiler_error ("`%D' as declarator", decl);
be99da77 9405 return 0; /* We used to do a 155 abort here. */
8d08fdba 9406 }
be99da77 9407 }
8d08fdba
MS
9408 if (name == NULL)
9409 name = "type name";
9410 }
9411
9412 /* A function definition's declarator must have the form of
9413 a function declarator. */
9414
9415 if (funcdef_flag && innermost_code != CALL_EXPR)
9416 return 0;
9417
e1cd6e56
MS
9418 if (((dname && IDENTIFIER_OPNAME_P (dname)) || flags == TYPENAME_FLAG)
9419 && innermost_code != CALL_EXPR
9420 && ! (ctype && declspecs == NULL_TREE))
9421 {
8251199e 9422 cp_error ("declaration of `%D' as non-function", dname);
e1cd6e56
MS
9423 return void_type_node;
9424 }
9425
8d08fdba
MS
9426 /* Anything declared one level down from the top level
9427 must be one of the parameters of a function
9428 (because the body is at least two levels down). */
9429
9430 /* This heuristic cannot be applied to C++ nodes! Fixed, however,
9431 by not allowing C++ class definitions to specify their parameters
9432 with xdecls (must be spec.d in the parmlist).
9433
9434 Since we now wait to push a class scope until we are sure that
9435 we are in a legitimate method context, we must set oldcname
a9aedbc2
MS
9436 explicitly (since current_class_name is not yet alive).
9437
9438 We also want to avoid calling this a PARM if it is in a namespace. */
8d08fdba 9439
5566b478
MS
9440 if (decl_context == NORMAL && ! namespace_bindings_p ()
9441 && ! pseudo_global_level_p ())
a9aedbc2
MS
9442 {
9443 struct binding_level *b = current_binding_level;
9444 current_binding_level = b->level_chain;
9445 if (current_binding_level != 0 && toplevel_bindings_p ())
9446 decl_context = PARM;
9447 current_binding_level = b;
9448 }
8d08fdba
MS
9449
9450 /* Look through the decl specs and record which ones appear.
9451 Some typespecs are defined as built-in typenames.
9452 Others, the ones that are modifiers of other types,
9453 are represented by bits in SPECBITS: set the bits for
9454 the modifiers that appear. Storage class keywords are also in SPECBITS.
9455
9456 If there is a typedef name or a type, store the type in TYPE.
9457 This includes builtin typedefs such as `int'.
9458
9459 Set EXPLICIT_INT if the type is `int' or `char' and did not
9460 come from a user typedef.
9461
9462 Set LONGLONG if `long' is mentioned twice.
9463
9464 For C++, constructors and destructors have their own fast treatment. */
9465
9466 for (spec = declspecs; spec; spec = TREE_CHAIN (spec))
9467 {
9468 register int i;
9469 register tree id;
9470
9471 /* Certain parse errors slip through. For example,
9472 `int class;' is not caught by the parser. Try
9473 weakly to recover here. */
9474 if (TREE_CODE (spec) != TREE_LIST)
9475 return 0;
9476
9477 id = TREE_VALUE (spec);
9478
9479 if (TREE_CODE (id) == IDENTIFIER_NODE)
9480 {
a3203465
MS
9481 if (id == ridpointers[(int) RID_INT]
9482 || id == ridpointers[(int) RID_CHAR]
9483 || id == ridpointers[(int) RID_BOOL]
9484 || id == ridpointers[(int) RID_WCHAR])
8d08fdba
MS
9485 {
9486 if (type)
8ccc31eb
MS
9487 {
9488 if (id == ridpointers[(int) RID_BOOL])
8251199e 9489 error ("`bool' is now a keyword");
8ccc31eb 9490 else
8251199e 9491 cp_error ("extraneous `%T' ignored", id);
8ccc31eb 9492 }
8d08fdba
MS
9493 else
9494 {
a3203465
MS
9495 if (id == ridpointers[(int) RID_INT])
9496 explicit_int = 1;
9497 else if (id == ridpointers[(int) RID_CHAR])
9498 explicit_char = 1;
8d08fdba
MS
9499 type = TREE_TYPE (IDENTIFIER_GLOBAL_VALUE (id));
9500 }
9501 goto found;
9502 }
e92cc029 9503 /* C++ aggregate types. */
8d08fdba
MS
9504 if (IDENTIFIER_HAS_TYPE_VALUE (id))
9505 {
9506 if (type)
8251199e 9507 cp_error ("multiple declarations `%T' and `%T'", type, id);
8d08fdba
MS
9508 else
9509 type = IDENTIFIER_TYPE_VALUE (id);
9510 goto found;
9511 }
9512
f376e137 9513 for (i = (int) RID_FIRST_MODIFIER; i <= (int) RID_LAST_MODIFIER; i++)
8d08fdba
MS
9514 {
9515 if (ridpointers[i] == id)
9516 {
9517 if (i == (int) RID_LONG && RIDBIT_SETP (i, specbits))
9518 {
795add94 9519 if (pedantic && ! in_system_header && warn_long_long)
8251199e 9520 pedwarn ("ANSI C++ does not support `long long'");
9a3b49ac 9521 if (longlong)
8251199e 9522 error ("`long long long' is too long for GCC");
8d08fdba
MS
9523 else
9524 longlong = 1;
9525 }
9526 else if (RIDBIT_SETP (i, specbits))
8251199e 9527 pedwarn ("duplicate `%s'", IDENTIFIER_POINTER (id));
8d08fdba
MS
9528 RIDBIT_SET (i, specbits);
9529 goto found;
9530 }
9531 }
9532 }
e92cc029 9533 /* C++ aggregate types. */
73b0fce8 9534 else if (TREE_CODE (id) == TYPE_DECL || TREE_CODE (id) == TEMPLATE_DECL)
45537677
MS
9535 {
9536 if (type)
8251199e 9537 cp_error ("multiple declarations `%T' and `%T'", type,
45537677
MS
9538 TREE_TYPE (id));
9539 else
5566b478
MS
9540 {
9541 type = TREE_TYPE (id);
9542 TREE_VALUE (spec) = type;
9543 }
45537677
MS
9544 goto found;
9545 }
8d08fdba 9546 if (type)
8251199e 9547 error ("two or more data types in declaration of `%s'", name);
8d08fdba
MS
9548 else if (TREE_CODE (id) == IDENTIFIER_NODE)
9549 {
9550 register tree t = lookup_name (id, 1);
9551 if (!t || TREE_CODE (t) != TYPE_DECL)
8251199e 9552 error ("`%s' fails to be a typedef or built in type",
8d08fdba
MS
9553 IDENTIFIER_POINTER (id));
9554 else
9555 {
9556 type = TREE_TYPE (t);
6125f3be
DE
9557#if 0
9558 /* See the code below that used this. */
f6abb50a 9559 decl_machine_attr = DECL_MACHINE_ATTRIBUTES (id);
6125f3be 9560#endif
8d08fdba
MS
9561 typedef_decl = t;
9562 }
9563 }
bd6dd845 9564 else if (id != error_mark_node)
8d08fdba
MS
9565 /* Can't change CLASS nodes into RECORD nodes here! */
9566 type = id;
9567
9568 found: ;
9569 }
9570
9571 typedef_type = type;
9572
37c46b43 9573 /* No type at all: default to `int', and set DEFAULTED_INT
8d08fdba
MS
9574 because it was not a user-defined typedef.
9575 Except when we have a `typedef' inside a signature, in
9576 which case the type defaults to `unknown type' and is
9577 instantiated when assigning to a signature pointer or ref. */
9578
a3203465
MS
9579 if (type == NULL_TREE
9580 && (RIDBIT_SETP (RID_SIGNED, specbits)
9581 || RIDBIT_SETP (RID_UNSIGNED, specbits)
9582 || RIDBIT_SETP (RID_LONG, specbits)
9583 || RIDBIT_SETP (RID_SHORT, specbits)))
9584 {
9585 /* These imply 'int'. */
9586 type = integer_type_node;
37c46b43 9587 defaulted_int = 1;
a3203465
MS
9588 }
9589
8d08fdba
MS
9590 if (type == NULL_TREE)
9591 {
9592 explicit_int = -1;
9593 if (return_type == return_dtor)
9594 type = void_type_node;
9595 else if (return_type == return_ctor)
f30432d7 9596 type = build_pointer_type (ctor_return_type);
51c184be
MS
9597 else if (return_type == return_conversion)
9598 type = ctor_return_type;
8d08fdba
MS
9599 else if (current_class_type
9600 && IS_SIGNATURE (current_class_type)
fc378698 9601 && RIDBIT_SETP (RID_TYPEDEF, specbits)
8d08fdba
MS
9602 && (decl_context == FIELD || decl_context == NORMAL))
9603 {
9604 explicit_int = 0;
9605 opaque_typedef = 1;
9606 type = copy_node (opaque_type_node);
9607 }
9608 else
9609 {
41eff652
JM
9610 /* We handle `main' specially here, because 'main () { }' is so
9611 common. With no options, it is allowed. With -Wreturn-type,
9612 it is a warning. It is only an error with -pedantic-errors. */
9613 int is_main = (funcdef_flag
9614 && MAIN_NAME_P (dname)
9615 && ctype == NULL_TREE
9616 && in_namespace == NULL_TREE
9617 && current_namespace == global_namespace);
9618
1f901793
JM
9619 if (in_system_header)
9620 /* Allow it, sigh. */;
9621 else if (pedantic || ! is_main)
ff0cf464
JM
9622 cp_pedwarn ("ANSI C++ forbids declaration `%D' with no type",
9623 dname);
41eff652
JM
9624 else if (warn_return_type)
9625 cp_warning ("ANSI C++ forbids declaration `%D' with no type",
9626 dname);
9627
8d08fdba
MS
9628 type = integer_type_node;
9629 }
9630 }
9631 else if (return_type == return_dtor)
9632 {
8251199e 9633 error ("return type specification for destructor invalid");
8d08fdba
MS
9634 type = void_type_node;
9635 }
9636 else if (return_type == return_ctor)
9637 {
8251199e 9638 error ("return type specification for constructor invalid");
f30432d7 9639 type = build_pointer_type (ctor_return_type);
8d08fdba 9640 }
51c184be
MS
9641 else if (return_type == return_conversion)
9642 {
3bfdc719 9643 if (!same_type_p (type, ctor_return_type))
8251199e 9644 cp_error ("operator `%T' declared to return `%T'",
51c184be
MS
9645 ctor_return_type, type);
9646 else
8251199e 9647 cp_pedwarn ("return type specified for `operator %T'",
51c184be
MS
9648 ctor_return_type);
9649
9650 type = ctor_return_type;
9651 }
8d08fdba
MS
9652
9653 ctype = NULL_TREE;
9654
9655 /* Now process the modifiers that were specified
9656 and check for invalid combinations. */
9657
9658 /* Long double is a special combination. */
9659
9660 if (RIDBIT_SETP (RID_LONG, specbits)
9661 && TYPE_MAIN_VARIANT (type) == double_type_node)
9662 {
9663 RIDBIT_RESET (RID_LONG, specbits);
91063b51
MM
9664 type = build_qualified_type (long_double_type_node,
9665 CP_TYPE_QUALS (type));
8d08fdba
MS
9666 }
9667
9668 /* Check all other uses of type modifiers. */
9669
9670 if (RIDBIT_SETP (RID_UNSIGNED, specbits)
9671 || RIDBIT_SETP (RID_SIGNED, specbits)
9672 || RIDBIT_SETP (RID_LONG, specbits)
9673 || RIDBIT_SETP (RID_SHORT, specbits))
9674 {
9675 int ok = 0;
9676
9677 if (TREE_CODE (type) == REAL_TYPE)
8251199e 9678 error ("short, signed or unsigned invalid for `%s'", name);
b7484fbe 9679 else if (TREE_CODE (type) != INTEGER_TYPE)
8251199e 9680 error ("long, short, signed or unsigned invalid for `%s'", name);
8d08fdba
MS
9681 else if (RIDBIT_SETP (RID_LONG, specbits)
9682 && RIDBIT_SETP (RID_SHORT, specbits))
8251199e 9683 error ("long and short specified together for `%s'", name);
8d08fdba
MS
9684 else if ((RIDBIT_SETP (RID_LONG, specbits)
9685 || RIDBIT_SETP (RID_SHORT, specbits))
9686 && explicit_char)
8251199e 9687 error ("long or short specified with char for `%s'", name);
8d08fdba
MS
9688 else if ((RIDBIT_SETP (RID_LONG, specbits)
9689 || RIDBIT_SETP (RID_SHORT, specbits))
9690 && TREE_CODE (type) == REAL_TYPE)
8251199e 9691 error ("long or short specified with floating type for `%s'", name);
8d08fdba
MS
9692 else if (RIDBIT_SETP (RID_SIGNED, specbits)
9693 && RIDBIT_SETP (RID_UNSIGNED, specbits))
8251199e 9694 error ("signed and unsigned given together for `%s'", name);
8d08fdba
MS
9695 else
9696 {
9697 ok = 1;
37c46b43 9698 if (!explicit_int && !defaulted_int && !explicit_char && pedantic)
8d08fdba 9699 {
8251199e 9700 pedwarn ("long, short, signed or unsigned used invalidly for `%s'",
8d08fdba
MS
9701 name);
9702 if (flag_pedantic_errors)
9703 ok = 0;
9704 }
9705 }
9706
9707 /* Discard the type modifiers if they are invalid. */
9708 if (! ok)
9709 {
9710 RIDBIT_RESET (RID_UNSIGNED, specbits);
9711 RIDBIT_RESET (RID_SIGNED, specbits);
9712 RIDBIT_RESET (RID_LONG, specbits);
9713 RIDBIT_RESET (RID_SHORT, specbits);
9714 longlong = 0;
9715 }
9716 }
9717
37c46b43
MS
9718 if (RIDBIT_SETP (RID_COMPLEX, specbits)
9719 && TREE_CODE (type) != INTEGER_TYPE && TREE_CODE (type) != REAL_TYPE)
9720 {
8251199e 9721 error ("complex invalid for `%s'", name);
37c46b43
MS
9722 RIDBIT_RESET (RID_COMPLEX, specbits);
9723 }
9724
8d08fdba
MS
9725 /* Decide whether an integer type is signed or not.
9726 Optionally treat bitfields as signed by default. */
9727 if (RIDBIT_SETP (RID_UNSIGNED, specbits)
8d08fdba 9728 || (bitfield && ! flag_signed_bitfields
37c46b43 9729 && (explicit_int || defaulted_int || explicit_char
8d08fdba
MS
9730 /* A typedef for plain `int' without `signed'
9731 can be controlled just like plain `int'. */
9732 || ! (typedef_decl != NULL_TREE
9733 && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl)))
9734 && TREE_CODE (type) != ENUMERAL_TYPE
9735 && RIDBIT_NOTSETP (RID_SIGNED, specbits)))
9736 {
9737 if (longlong)
9738 type = long_long_unsigned_type_node;
9739 else if (RIDBIT_SETP (RID_LONG, specbits))
9740 type = long_unsigned_type_node;
9741 else if (RIDBIT_SETP (RID_SHORT, specbits))
9742 type = short_unsigned_type_node;
9743 else if (type == char_type_node)
9744 type = unsigned_char_type_node;
9745 else if (typedef_decl)
9746 type = unsigned_type (type);
9747 else
9748 type = unsigned_type_node;
9749 }
9750 else if (RIDBIT_SETP (RID_SIGNED, specbits)
9751 && type == char_type_node)
9752 type = signed_char_type_node;
9753 else if (longlong)
9754 type = long_long_integer_type_node;
9755 else if (RIDBIT_SETP (RID_LONG, specbits))
9756 type = long_integer_type_node;
9757 else if (RIDBIT_SETP (RID_SHORT, specbits))
9758 type = short_integer_type_node;
9759
37c46b43
MS
9760 if (RIDBIT_SETP (RID_COMPLEX, specbits))
9761 {
9762 /* If we just have "complex", it is equivalent to
9763 "complex double", but if any modifiers at all are specified it is
9764 the complex form of TYPE. E.g, "complex short" is
9765 "complex short int". */
9766
9767 if (defaulted_int && ! longlong
9768 && ! (RIDBIT_SETP (RID_LONG, specbits)
9769 || RIDBIT_SETP (RID_SHORT, specbits)
9770 || RIDBIT_SETP (RID_SIGNED, specbits)
9771 || RIDBIT_SETP (RID_UNSIGNED, specbits)))
9772 type = complex_double_type_node;
9773 else if (type == integer_type_node)
9774 type = complex_integer_type_node;
9775 else if (type == float_type_node)
9776 type = complex_float_type_node;
9777 else if (type == double_type_node)
9778 type = complex_double_type_node;
9779 else if (type == long_double_type_node)
9780 type = complex_long_double_type_node;
9781 else
9782 type = build_complex_type (type);
9783 }
9784
3ac3d9ea
MM
9785 if (return_type == return_conversion
9786 && (RIDBIT_SETP (RID_CONST, specbits)
91063b51
MM
9787 || RIDBIT_SETP (RID_VOLATILE, specbits)
9788 || RIDBIT_SETP (RID_RESTRICT, specbits)))
9789 cp_error ("qualifiers are not allowed on declaration of `operator %T'",
3ac3d9ea
MM
9790 ctor_return_type);
9791
8d08fdba
MS
9792 /* Set CONSTP if this declaration is `const', whether by
9793 explicit specification or via a typedef.
9794 Likewise for VOLATILEP. */
9795
91063b51
MM
9796 constp = !! RIDBIT_SETP (RID_CONST, specbits) + CP_TYPE_CONST_P (type);
9797 restrictp =
9798 !! RIDBIT_SETP (RID_RESTRICT, specbits) + CP_TYPE_RESTRICT_P (type);
9799 volatilep =
9800 !! RIDBIT_SETP (RID_VOLATILE, specbits) + CP_TYPE_VOLATILE_P (type);
9801 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
9802 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
9803 | (volatilep ? TYPE_QUAL_VOLATILE : 0));
9804 type = cp_build_qualified_type (type, type_quals);
8d08fdba
MS
9805 staticp = 0;
9806 inlinep = !! RIDBIT_SETP (RID_INLINE, specbits);
8d08fdba 9807 virtualp = RIDBIT_SETP (RID_VIRTUAL, specbits);
db5ae43f
MS
9808 RIDBIT_RESET (RID_VIRTUAL, specbits);
9809 explicitp = RIDBIT_SETP (RID_EXPLICIT, specbits) != 0;
9810 RIDBIT_RESET (RID_EXPLICIT, specbits);
8d08fdba 9811
8d08fdba
MS
9812 if (RIDBIT_SETP (RID_STATIC, specbits))
9813 staticp = 1 + (decl_context == FIELD);
9814
9815 if (virtualp && staticp == 2)
9816 {
8251199e 9817 cp_error ("member `%D' cannot be declared both virtual and static",
8d08fdba
MS
9818 dname);
9819 staticp = 0;
9820 }
9821 friendp = RIDBIT_SETP (RID_FRIEND, specbits);
8d08fdba
MS
9822 RIDBIT_RESET (RID_FRIEND, specbits);
9823
7e83af84
BK
9824 /* $7.1.2, Function specifiers */
9825 if (friendp && explicitp)
9826 error ("only declarations of constructors can be `explicit'");
9827
8d08fdba
MS
9828 if (RIDBIT_SETP (RID_MUTABLE, specbits))
9829 {
9830 if (decl_context == PARM)
9831 {
8251199e 9832 error ("non-member `%s' cannot be declared `mutable'", name);
8d08fdba
MS
9833 RIDBIT_RESET (RID_MUTABLE, specbits);
9834 }
9835 else if (friendp || decl_context == TYPENAME)
9836 {
8251199e 9837 error ("non-object member `%s' cannot be declared `mutable'", name);
8d08fdba
MS
9838 RIDBIT_RESET (RID_MUTABLE, specbits);
9839 }
8d08fdba
MS
9840 }
9841
9842 /* Warn if two storage classes are given. Default to `auto'. */
9843
9844 if (RIDBIT_ANY_SET (specbits))
9845 {
9846 if (RIDBIT_SETP (RID_STATIC, specbits)) nclasses++;
9847 if (RIDBIT_SETP (RID_EXTERN, specbits)) nclasses++;
9848 if (decl_context == PARM && nclasses > 0)
8251199e 9849 error ("storage class specifiers invalid in parameter declarations");
8d08fdba
MS
9850 if (RIDBIT_SETP (RID_TYPEDEF, specbits))
9851 {
9852 if (decl_context == PARM)
8251199e 9853 error ("typedef declaration invalid in parameter declaration");
8d08fdba
MS
9854 nclasses++;
9855 }
9856 if (RIDBIT_SETP (RID_AUTO, specbits)) nclasses++;
9857 if (RIDBIT_SETP (RID_REGISTER, specbits)) nclasses++;
9858 }
9859
9860 /* Give error if `virtual' is used outside of class declaration. */
b7484fbe
MS
9861 if (virtualp
9862 && (current_class_name == NULL_TREE || decl_context != FIELD))
8d08fdba 9863 {
8251199e 9864 error ("virtual outside class declaration");
8d08fdba
MS
9865 virtualp = 0;
9866 }
9867 if (current_class_name == NULL_TREE && RIDBIT_SETP (RID_MUTABLE, specbits))
9868 {
8251199e 9869 error ("only members can be declared mutable");
8d08fdba
MS
9870 RIDBIT_RESET (RID_MUTABLE, specbits);
9871 }
9872
9873 /* Static anonymous unions are dealt with here. */
9874 if (staticp && decl_context == TYPENAME
9875 && TREE_CODE (declspecs) == TREE_LIST
67ffc812 9876 && ANON_UNION_TYPE_P (TREE_VALUE (declspecs)))
8d08fdba
MS
9877 decl_context = FIELD;
9878
9879 /* Give error if `const,' `volatile,' `inline,' `friend,' or `virtual'
9880 is used in a signature member function declaration. */
9881 if (decl_context == FIELD
9882 && IS_SIGNATURE (current_class_type)
fc378698 9883 && RIDBIT_NOTSETP (RID_TYPEDEF, specbits))
8d08fdba 9884 {
91063b51 9885 if (type_quals != TYPE_UNQUALIFIED)
8d08fdba 9886 {
91063b51
MM
9887 error ("type qualifiers specified for signature member function `%s'", name);
9888 type_quals = TYPE_UNQUALIFIED;
8d08fdba
MS
9889 }
9890 if (inlinep)
9891 {
8251199e 9892 error ("`inline' specified for signature member function `%s'", name);
8d08fdba
MS
9893 /* Later, we'll make signature member functions inline. */
9894 inlinep = 0;
9895 }
9896 if (friendp)
9897 {
8251199e 9898 error ("`friend' declaration in signature definition");
8d08fdba
MS
9899 friendp = 0;
9900 }
9901 if (virtualp)
9902 {
8251199e 9903 error ("`virtual' specified for signature member function `%s'",
a28e3c7f 9904 name);
8d08fdba
MS
9905 /* Later, we'll make signature member functions virtual. */
9906 virtualp = 0;
9907 }
9908 }
9909
9910 /* Warn about storage classes that are invalid for certain
9911 kinds of declarations (parameters, typenames, etc.). */
9912
9913 if (nclasses > 1)
8251199e 9914 error ("multiple storage classes in declaration of `%s'", name);
8d08fdba
MS
9915 else if (decl_context != NORMAL && nclasses > 0)
9916 {
db5ae43f 9917 if ((decl_context == PARM || decl_context == CATCHPARM)
8d08fdba
MS
9918 && (RIDBIT_SETP (RID_REGISTER, specbits)
9919 || RIDBIT_SETP (RID_AUTO, specbits)))
9920 ;
fc378698
MS
9921 else if (RIDBIT_SETP (RID_TYPEDEF, specbits))
9922 ;
8d08fdba 9923 else if (decl_context == FIELD
fc378698 9924 && ! IS_SIGNATURE (current_class_type)
8d08fdba
MS
9925 /* C++ allows static class elements */
9926 && RIDBIT_SETP (RID_STATIC, specbits))
9927 /* C++ also allows inlines and signed and unsigned elements,
9928 but in those cases we don't come in here. */
9929 ;
9930 else
9931 {
9932 if (decl_context == FIELD)
9933 {
b7484fbe
MS
9934 tree tmp = NULL_TREE;
9935 register int op = 0;
9936
9937 if (declarator)
9938 {
9e9ff709
MS
9939 /* Avoid trying to get an operand off an identifier node. */
9940 if (TREE_CODE (declarator) == IDENTIFIER_NODE)
9941 tmp = declarator;
9942 else
9943 tmp = TREE_OPERAND (declarator, 0);
b7484fbe
MS
9944 op = IDENTIFIER_OPNAME_P (tmp);
9945 }
8251199e 9946 error ("storage class specified for %s `%s'",
8d08fdba
MS
9947 IS_SIGNATURE (current_class_type)
9948 ? (op
9949 ? "signature member operator"
9950 : "signature member function")
b7484fbe 9951 : (op ? "member operator" : "field"),
8d08fdba
MS
9952 op ? operator_name_string (tmp) : name);
9953 }
9954 else
db5ae43f 9955 error (((decl_context == PARM || decl_context == CATCHPARM)
8d08fdba
MS
9956 ? "storage class specified for parameter `%s'"
9957 : "storage class specified for typename"), name);
9958 RIDBIT_RESET (RID_REGISTER, specbits);
9959 RIDBIT_RESET (RID_AUTO, specbits);
9960 RIDBIT_RESET (RID_EXTERN, specbits);
9961
9962 if (decl_context == FIELD && IS_SIGNATURE (current_class_type))
9963 {
9964 RIDBIT_RESET (RID_STATIC, specbits);
9965 staticp = 0;
9966 }
9967 }
9968 }
9969 else if (RIDBIT_SETP (RID_EXTERN, specbits) && initialized && !funcdef_flag)
9970 {
a9aedbc2 9971 if (toplevel_bindings_p ())
8d08fdba 9972 {
59be0cdd 9973 /* It's common practice (and completely valid) to have a const
8d08fdba 9974 be initialized and declared extern. */
950ad3c3 9975 if (!(type_quals & TYPE_QUAL_CONST))
8251199e 9976 warning ("`%s' initialized and declared `extern'", name);
8d08fdba
MS
9977 }
9978 else
8251199e 9979 error ("`%s' has both `extern' and initializer", name);
8d08fdba
MS
9980 }
9981 else if (RIDBIT_SETP (RID_EXTERN, specbits) && funcdef_flag
a9aedbc2 9982 && ! toplevel_bindings_p ())
8251199e 9983 error ("nested function `%s' declared `extern'", name);
a9aedbc2 9984 else if (toplevel_bindings_p ())
8d08fdba
MS
9985 {
9986 if (RIDBIT_SETP (RID_AUTO, specbits))
8251199e 9987 error ("top-level declaration of `%s' specifies `auto'", name);
8d08fdba
MS
9988 }
9989
909e536a 9990 if (nclasses > 0 && friendp)
8251199e 9991 error ("storage class specifiers invalid in friend function declarations");
909e536a 9992
8d08fdba
MS
9993 /* Now figure out the structure of the declarator proper.
9994 Descend through it, creating more complex types, until we reach
9995 the declared identifier (or NULL_TREE, in an absolute declarator). */
9996
1eab9b56
JM
9997 inner_attrs = NULL_TREE;
9998 ignore_attrs = 0;
9999
386b8a85
JM
10000 while (declarator && TREE_CODE (declarator) != IDENTIFIER_NODE
10001 && TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
8d08fdba
MS
10002 {
10003 /* Each level of DECLARATOR is either an ARRAY_REF (for ...[..]),
10004 an INDIRECT_REF (for *...),
10005 a CALL_EXPR (for ...(...)),
10006 an identifier (for the name being declared)
10007 or a null pointer (for the place in an absolute declarator
10008 where the name was omitted).
10009 For the last two cases, we have just exited the loop.
10010
10011 For C++ it could also be
10012 a SCOPE_REF (for class :: ...). In this case, we have converted
10013 sensible names to types, and those are the values we use to
10014 qualify the member name.
10015 an ADDR_EXPR (for &...),
10016 a BIT_NOT_EXPR (for destructors)
8d08fdba
MS
10017
10018 At this point, TYPE is the type of elements of an array,
10019 or for a function to return, or for a pointer to point to.
10020 After this sequence of ifs, TYPE is the type of the
10021 array or function or pointer, and DECLARATOR has had its
10022 outermost layer removed. */
10023
bd6dd845 10024 if (type == error_mark_node)
8d08fdba
MS
10025 {
10026 if (TREE_CODE (declarator) == SCOPE_REF)
10027 declarator = TREE_OPERAND (declarator, 1);
10028 else
10029 declarator = TREE_OPERAND (declarator, 0);
10030 continue;
10031 }
10032 if (quals != NULL_TREE
10033 && (declarator == NULL_TREE
10034 || TREE_CODE (declarator) != SCOPE_REF))
10035 {
10036 if (ctype == NULL_TREE && TREE_CODE (type) == METHOD_TYPE)
10037 ctype = TYPE_METHOD_BASETYPE (type);
10038 if (ctype != NULL_TREE)
10039 {
8d08fdba 10040 tree dummy = build_decl (TYPE_DECL, NULL_TREE, type);
8d08fdba
MS
10041 ctype = grok_method_quals (ctype, dummy, quals);
10042 type = TREE_TYPE (dummy);
10043 quals = NULL_TREE;
10044 }
10045 }
1eab9b56
JM
10046
10047 /* See the comment for the TREE_LIST case, below. */
10048 if (ignore_attrs)
10049 ignore_attrs = 0;
10050 else if (inner_attrs)
10051 {
10052 decl_attributes (type, inner_attrs, NULL_TREE);
10053 inner_attrs = NULL_TREE;
10054 }
10055
8d08fdba
MS
10056 switch (TREE_CODE (declarator))
10057 {
52fbc847
JM
10058 case TREE_LIST:
10059 {
10060 /* We encode a declarator with embedded attributes using
1eab9b56
JM
10061 a TREE_LIST. The attributes apply to the declarator
10062 directly inside them, so we have to skip an iteration
10063 before applying them to the type. If the declarator just
10064 inside is the declarator-id, we apply the attrs to the
10065 decl itself. */
10066 inner_attrs = TREE_PURPOSE (declarator);
10067 ignore_attrs = 1;
52fbc847 10068 declarator = TREE_VALUE (declarator);
52fbc847
JM
10069 }
10070 break;
10071
8d08fdba
MS
10072 case ARRAY_REF:
10073 {
10074 register tree itype = NULL_TREE;
10075 register tree size = TREE_OPERAND (declarator, 1);
b7484fbe
MS
10076 /* The index is a signed object `sizetype' bits wide. */
10077 tree index_type = signed_type (sizetype);
8d08fdba
MS
10078
10079 declarator = TREE_OPERAND (declarator, 0);
10080
10081 /* Check for some types that there cannot be arrays of. */
10082
a1774733 10083 if (TREE_CODE (type) == VOID_TYPE)
8d08fdba 10084 {
8251199e 10085 cp_error ("declaration of `%D' as array of voids", dname);
8d08fdba
MS
10086 type = error_mark_node;
10087 }
10088
10089 if (TREE_CODE (type) == FUNCTION_TYPE)
10090 {
8251199e 10091 cp_error ("declaration of `%D' as array of functions", dname);
8d08fdba
MS
10092 type = error_mark_node;
10093 }
10094
10095 /* ARM $8.4.3: Since you can't have a pointer to a reference,
10096 you can't have arrays of references. If we allowed them,
59be0cdd 10097 then we'd be saying x[i] is valid for an array x, but
8d08fdba
MS
10098 then you'd have to ask: what does `*(x + i)' mean? */
10099 if (TREE_CODE (type) == REFERENCE_TYPE)
10100 {
10101 if (decl_context == TYPENAME)
8251199e 10102 cp_error ("cannot make arrays of references");
8d08fdba 10103 else
8251199e 10104 cp_error ("declaration of `%D' as array of references",
8d08fdba
MS
10105 dname);
10106 type = error_mark_node;
10107 }
10108
10109 if (TREE_CODE (type) == OFFSET_TYPE)
10110 {
8251199e 10111 cp_error ("declaration of `%D' as array of data members",
8d08fdba
MS
10112 dname);
10113 type = error_mark_node;
10114 }
10115
10116 if (TREE_CODE (type) == METHOD_TYPE)
10117 {
8251199e 10118 cp_error ("declaration of `%D' as array of function members",
8d08fdba
MS
10119 dname);
10120 type = error_mark_node;
10121 }
10122
10123 if (size == error_mark_node)
10124 type = error_mark_node;
3bfdc719
MM
10125 else if (TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type))
10126 {
10127 /* [dcl.array]
10128
10129 the constant expressions that specify the bounds of
10130 the arrays can be omitted only for the first member
10131 of the sequence. */
10132 cp_error ("declaration of `%D' as multidimensional array",
10133 dname);
10134 cp_error ("must have bounds for all dimensions except the first");
10135 type = error_mark_node;
10136 }
8d08fdba
MS
10137
10138 if (type == error_mark_node)
10139 continue;
10140
f2e2cbd4
JM
10141 /* VC++ spells a zero-sized array with []. */
10142 if (size == NULL_TREE && decl_context == FIELD && ! staticp)
10143 size = integer_zero_node;
10144
8d08fdba
MS
10145 if (size)
10146 {
10147 /* Must suspend_momentary here because the index
10148 type may need to live until the end of the function.
10149 For example, it is used in the declaration of a
10150 variable which requires destructing at the end of
10151 the function; then build_vec_delete will need this
10152 value. */
10153 int yes = suspend_momentary ();
2c73f9f5 10154 /* Might be a cast. */
8d08fdba
MS
10155 if (TREE_CODE (size) == NOP_EXPR
10156 && TREE_TYPE (size) == TREE_TYPE (TREE_OPERAND (size, 0)))
10157 size = TREE_OPERAND (size, 0);
10158
5db698f6
MM
10159 /* If this involves a template parameter, it will be a
10160 constant at instantiation time, but we don't know
10161 what the value is yet. Even if no template
10162 parameters are involved, we may an expression that
10163 is not a constant; we don't even simplify `1 + 2'
10164 when processing a template. */
10165 if (processing_template_decl)
5566b478 10166 {
e97e5263
JM
10167 /* Resolve a qualified reference to an enumerator or
10168 static const data member of ours. */
10169 if (TREE_CODE (size) == SCOPE_REF
10170 && TREE_OPERAND (size, 0) == current_class_type)
10171 {
10172 tree t = lookup_field (current_class_type,
10173 TREE_OPERAND (size, 1), 0, 0);
10174 if (t)
10175 size = t;
10176 }
10177
5566b478
MS
10178 itype = make_node (INTEGER_TYPE);
10179 TYPE_MIN_VALUE (itype) = size_zero_node;
10180 TYPE_MAX_VALUE (itype) = build_min
10181 (MINUS_EXPR, sizetype, size, integer_one_node);
10182 goto dont_grok_size;
10183 }
8d08fdba
MS
10184
10185 if (TREE_CODE (TREE_TYPE (size)) != INTEGER_TYPE
161c12b0
JM
10186 && TREE_CODE (TREE_TYPE (size)) != ENUMERAL_TYPE
10187 && TREE_CODE (TREE_TYPE (size)) != BOOLEAN_TYPE)
8d08fdba 10188 {
8251199e 10189 cp_error ("size of array `%D' has non-integer type",
8d08fdba
MS
10190 dname);
10191 size = integer_one_node;
10192 }
10193 if (TREE_READONLY_DECL_P (size))
10194 size = decl_constant_value (size);
e1cd6e56 10195 if (pedantic && integer_zerop (size))
8251199e 10196 cp_pedwarn ("ANSI C++ forbids zero-size array `%D'", dname);
8d08fdba
MS
10197 if (TREE_CONSTANT (size))
10198 {
dff6b454
RK
10199 int old_flag_pedantic_errors = flag_pedantic_errors;
10200 int old_pedantic = pedantic;
10201 pedantic = flag_pedantic_errors = 1;
10202 /* Always give overflow errors on array subscripts. */
8d08fdba 10203 constant_expression_warning (size);
dff6b454
RK
10204 pedantic = old_pedantic;
10205 flag_pedantic_errors = old_flag_pedantic_errors;
8d08fdba
MS
10206 if (INT_CST_LT (size, integer_zero_node))
10207 {
8251199e 10208 cp_error ("size of array `%D' is negative", dname);
8d08fdba
MS
10209 size = integer_one_node;
10210 }
8d08fdba
MS
10211 }
10212 else
10213 {
e1cd6e56 10214 if (pedantic)
a0a33927
MS
10215 {
10216 if (dname)
8251199e 10217 cp_pedwarn ("ANSI C++ forbids variable-size array `%D'",
a0a33927
MS
10218 dname);
10219 else
8251199e 10220 cp_pedwarn ("ANSI C++ forbids variable-size array");
a0a33927 10221 }
8d08fdba 10222 }
b7484fbe 10223
beb53fb8
JM
10224 itype
10225 = fold (build_binary_op (MINUS_EXPR,
37c46b43
MS
10226 cp_convert (index_type, size),
10227 cp_convert (index_type,
337c90cc 10228 integer_one_node)));
b7484fbe
MS
10229 if (! TREE_CONSTANT (itype))
10230 itype = variable_size (itype);
594740f3
MS
10231 else if (TREE_OVERFLOW (itype))
10232 {
8251199e 10233 error ("overflow in array dimension");
594740f3
MS
10234 TREE_OVERFLOW (itype) = 0;
10235 }
fc378698
MS
10236
10237 /* If we're a parm, we need to have a permanent type so
10238 mangling checks for re-use will work right. If both the
10239 element and index types are permanent, the array type
10240 will be, too. */
10241 if (decl_context == PARM
10242 && allocation_temporary_p () && TREE_PERMANENT (type))
10243 {
10244 push_obstacks (&permanent_obstack, &permanent_obstack);
10245 itype = build_index_type (itype);
10246 pop_obstacks ();
10247 }
10248 else
10249 itype = build_index_type (itype);
10250
5566b478 10251 dont_grok_size:
8d08fdba
MS
10252 resume_momentary (yes);
10253 }
10254
8d08fdba 10255 type = build_cplus_array_type (type, itype);
8d08fdba
MS
10256 ctype = NULL_TREE;
10257 }
10258 break;
10259
10260 case CALL_EXPR:
10261 {
10262 tree arg_types;
f376e137
MS
10263 int funcdecl_p;
10264 tree inner_parms = TREE_OPERAND (declarator, 1);
10265 tree inner_decl = TREE_OPERAND (declarator, 0);
8d08fdba
MS
10266
10267 /* Declaring a function type.
10268 Make sure we have a valid type for the function to return. */
8d08fdba 10269
91063b51 10270 /* We now know that the TYPE_QUALS don't apply to the
14ae7e7d 10271 decl, but to its return type. */
91063b51 10272 type_quals = TYPE_UNQUALIFIED;
8d08fdba
MS
10273
10274 /* Warn about some types functions can't return. */
10275
10276 if (TREE_CODE (type) == FUNCTION_TYPE)
10277 {
8251199e 10278 error ("`%s' declared as function returning a function", name);
8d08fdba
MS
10279 type = integer_type_node;
10280 }
10281 if (TREE_CODE (type) == ARRAY_TYPE)
10282 {
8251199e 10283 error ("`%s' declared as function returning an array", name);
8d08fdba
MS
10284 type = integer_type_node;
10285 }
10286
f376e137
MS
10287 if (inner_decl && TREE_CODE (inner_decl) == SCOPE_REF)
10288 inner_decl = TREE_OPERAND (inner_decl, 1);
10289
386b8a85
JM
10290 if (inner_decl && TREE_CODE (inner_decl) == TEMPLATE_ID_EXPR)
10291 inner_decl = dname;
10292
b7484fbe
MS
10293 /* Pick up type qualifiers which should be applied to `this'. */
10294 quals = TREE_OPERAND (declarator, 2);
10295
c11b6f21
MS
10296 /* Pick up the exception specifications. */
10297 raises = TREE_TYPE (declarator);
10298
f376e137
MS
10299 /* Say it's a definition only for the CALL_EXPR
10300 closest to the identifier. */
beb53fb8 10301 funcdecl_p
386b8a85
JM
10302 = inner_decl
10303 && (TREE_CODE (inner_decl) == IDENTIFIER_NODE
10304 || TREE_CODE (inner_decl) == TEMPLATE_ID_EXPR
10305 || TREE_CODE (inner_decl) == BIT_NOT_EXPR);
10306
8d08fdba
MS
10307 if (ctype == NULL_TREE
10308 && decl_context == FIELD
f376e137 10309 && funcdecl_p
8d08fdba
MS
10310 && (friendp == 0 || dname == current_class_name))
10311 ctype = current_class_type;
10312
51c184be 10313 if (ctype && return_type == return_conversion)
8d08fdba
MS
10314 TYPE_HAS_CONVERSION (ctype) = 1;
10315 if (ctype && constructor_name (ctype) == dname)
10316 {
10317 /* We are within a class's scope. If our declarator name
10318 is the same as the class name, and we are defining
10319 a function, then it is a constructor/destructor, and
10320 therefore returns a void type. */
10321
10322 if (flags == DTOR_FLAG)
10323 {
10324 /* ANSI C++ June 5 1992 WP 12.4.1. A destructor may
10325 not be declared const or volatile. A destructor
10326 may not be static. */
10327 if (staticp == 2)
8251199e 10328 error ("destructor cannot be static member function");
b7484fbe 10329 if (quals)
8d08fdba 10330 {
4f70a846
MM
10331 cp_error ("destructors may not be `%s'",
10332 IDENTIFIER_POINTER (TREE_VALUE (quals)));
10333 quals = NULL_TREE;
8d08fdba
MS
10334 }
10335 if (decl_context == FIELD)
10336 {
10337 if (! member_function_or_else (ctype, current_class_type,
10338 "destructor for alien class `%s' cannot be a member"))
10339 return void_type_node;
10340 }
10341 }
2c73f9f5 10342 else /* It's a constructor. */
8d08fdba 10343 {
db5ae43f
MS
10344 if (explicitp == 1)
10345 explicitp = 2;
8d08fdba
MS
10346 /* ANSI C++ June 5 1992 WP 12.1.2. A constructor may
10347 not be declared const or volatile. A constructor may
10348 not be virtual. A constructor may not be static. */
10349 if (staticp == 2)
8251199e 10350 error ("constructor cannot be static member function");
8d08fdba
MS
10351 if (virtualp)
10352 {
8251199e 10353 pedwarn ("constructors cannot be declared virtual");
8d08fdba
MS
10354 virtualp = 0;
10355 }
b7484fbe 10356 if (quals)
8d08fdba 10357 {
4f70a846
MM
10358 cp_error ("constructors may not be `%s'",
10359 IDENTIFIER_POINTER (TREE_VALUE (quals)));
10360 quals = NULL_TREE;
8d08fdba 10361 }
8d08fdba 10362 {
51c184be 10363 RID_BIT_TYPE tmp_bits;
fc378698 10364 bcopy ((void*)&specbits, (void*)&tmp_bits, sizeof (RID_BIT_TYPE));
51c184be
MS
10365 RIDBIT_RESET (RID_INLINE, tmp_bits);
10366 RIDBIT_RESET (RID_STATIC, tmp_bits);
10367 if (RIDBIT_ANY_SET (tmp_bits))
8251199e 10368 error ("return value type specifier for constructor ignored");
8d08fdba 10369 }
f30432d7 10370 type = build_pointer_type (ctype);
beb53fb8
JM
10371 if (decl_context == FIELD
10372 && IS_SIGNATURE (current_class_type))
8d08fdba 10373 {
8251199e 10374 error ("constructor not allowed in signature");
8d08fdba
MS
10375 return void_type_node;
10376 }
10377 else if (decl_context == FIELD)
10378 {
10379 if (! member_function_or_else (ctype, current_class_type,
10380 "constructor for alien class `%s' cannot be member"))
10381 return void_type_node;
10382 TYPE_HAS_CONSTRUCTOR (ctype) = 1;
10383 if (return_type != return_ctor)
10384 return NULL_TREE;
10385 }
10386 }
10387 if (decl_context == FIELD)
10388 staticp = 0;
10389 }
b7484fbe 10390 else if (friendp)
8d08fdba 10391 {
b7484fbe 10392 if (initialized)
8251199e 10393 error ("can't initialize friend function `%s'", name);
b7484fbe
MS
10394 if (virtualp)
10395 {
10396 /* Cannot be both friend and virtual. */
8251199e 10397 error ("virtual functions cannot be friends");
b7484fbe
MS
10398 RIDBIT_RESET (RID_FRIEND, specbits);
10399 friendp = 0;
10400 }
28cbf42c 10401 if (decl_context == NORMAL)
8251199e 10402 error ("friend declaration not in class definition");
28cbf42c 10403 if (current_function_decl && funcdef_flag)
8251199e 10404 cp_error ("can't define friend function `%s' in a local class definition",
28cbf42c 10405 name);
8d08fdba
MS
10406 }
10407
8d08fdba
MS
10408 /* Construct the function type and go to the next
10409 inner layer of declarator. */
10410
f376e137 10411 declarator = TREE_OPERAND (declarator, 0);
8d08fdba 10412
f376e137
MS
10413 /* FIXME: This is where default args should be fully
10414 processed. */
8d08fdba 10415
f376e137 10416 arg_types = grokparms (inner_parms, funcdecl_p ? funcdef_flag : 0);
8d08fdba 10417
4f70a846 10418 if (declarator && flags == DTOR_FLAG)
8d08fdba 10419 {
4f70a846
MM
10420 /* A destructor declared in the body of a class will
10421 be represented as a BIT_NOT_EXPR. But, we just
10422 want the underlying IDENTIFIER. */
8d08fdba 10423 if (TREE_CODE (declarator) == BIT_NOT_EXPR)
4f70a846
MM
10424 declarator = TREE_OPERAND (declarator, 0);
10425
10426 if (strict_prototype == 0 && arg_types == NULL_TREE)
10427 arg_types = void_list_node;
10428 else if (arg_types == NULL_TREE
10429 || arg_types != void_list_node)
8d08fdba 10430 {
4f70a846
MM
10431 cp_error ("destructors may not have parameters");
10432 arg_types = void_list_node;
10433 last_function_parms = NULL_TREE;
8d08fdba
MS
10434 }
10435 }
10436
d22c8596 10437 /* ANSI says that `const int foo ();'
8d08fdba 10438 does not make the function foo const. */
d22c8596 10439 type = build_function_type (type, arg_types);
42976354
BK
10440
10441 {
10442 tree t;
10443 for (t = arg_types; t; t = TREE_CHAIN (t))
10444 if (TREE_PURPOSE (t)
10445 && TREE_CODE (TREE_PURPOSE (t)) == DEFAULT_ARG)
10446 {
10447 add_defarg_fn (type);
10448 break;
10449 }
10450 }
8d08fdba
MS
10451 }
10452 break;
10453
10454 case ADDR_EXPR:
10455 case INDIRECT_REF:
10456 /* Filter out pointers-to-references and references-to-references.
10457 We can get these if a TYPE_DECL is used. */
10458
10459 if (TREE_CODE (type) == REFERENCE_TYPE)
10460 {
8251199e 10461 error ("cannot declare %s to references",
8d08fdba
MS
10462 TREE_CODE (declarator) == ADDR_EXPR
10463 ? "references" : "pointers");
10464 declarator = TREE_OPERAND (declarator, 0);
10465 continue;
10466 }
10467
a5894242
MS
10468 if (TREE_CODE (type) == OFFSET_TYPE
10469 && (TREE_CODE (TREE_TYPE (type)) == VOID_TYPE
10470 || TREE_CODE (TREE_TYPE (type)) == REFERENCE_TYPE))
10471 {
8251199e 10472 cp_error ("cannot declare pointer to `%#T' member",
a5894242
MS
10473 TREE_TYPE (type));
10474 type = TREE_TYPE (type);
10475 }
10476
61a127b3
MM
10477 /* Merge any constancy or volatility into the target type
10478 for the pointer. */
10479
91063b51
MM
10480 /* We now know that the TYPE_QUALS don't apply to the decl,
10481 but to the target of the pointer. */
10482 type_quals = TYPE_UNQUALIFIED;
8d08fdba
MS
10483
10484 if (IS_SIGNATURE (type))
10485 {
10486 if (TREE_CODE (declarator) == ADDR_EXPR)
10487 {
63718c49
GB
10488 if (CLASSTYPE_METHOD_VEC (type) == NULL_TREE
10489 && TYPE_SIZE (type))
8251199e 10490 cp_warning ("empty signature `%T' used in signature reference declaration",
63718c49 10491 type);
8d08fdba 10492#if 0
14ae7e7d 10493 type = build_signature_reference_type (type);
8d08fdba
MS
10494#else
10495 sorry ("signature reference");
10496 return NULL_TREE;
10497#endif
10498 }
10499 else
10500 {
63718c49
GB
10501 if (CLASSTYPE_METHOD_VEC (type) == NULL_TREE
10502 && TYPE_SIZE (type))
8251199e 10503 cp_warning ("empty signature `%T' used in signature pointer declaration",
63718c49 10504 type);
14ae7e7d 10505 type = build_signature_pointer_type (type);
8d08fdba 10506 }
8d08fdba
MS
10507 }
10508 else if (TREE_CODE (declarator) == ADDR_EXPR)
10509 {
69851283
MM
10510 if (TREE_CODE (type) == VOID_TYPE)
10511 error ("invalid type: `void &'");
8d08fdba 10512 else
69851283 10513 type = build_reference_type (type);
8d08fdba
MS
10514 }
10515 else if (TREE_CODE (type) == METHOD_TYPE)
d8f8dca1 10516 type = build_ptrmemfunc_type (build_pointer_type (type));
8d08fdba
MS
10517 else
10518 type = build_pointer_type (type);
10519
10520 /* Process a list of type modifier keywords (such as
10521 const or volatile) that were given inside the `*' or `&'. */
10522
10523 if (TREE_TYPE (declarator))
10524 {
10525 register tree typemodlist;
10526 int erred = 0;
91063b51
MM
10527
10528 constp = 0;
10529 volatilep = 0;
10530 restrictp = 0;
8d08fdba
MS
10531 for (typemodlist = TREE_TYPE (declarator); typemodlist;
10532 typemodlist = TREE_CHAIN (typemodlist))
10533 {
91063b51
MM
10534 tree qualifier = TREE_VALUE (typemodlist);
10535
10536 if (qualifier == ridpointers[(int) RID_CONST])
8d08fdba 10537 constp++;
91063b51 10538 else if (qualifier == ridpointers[(int) RID_VOLATILE])
8d08fdba 10539 volatilep++;
91063b51
MM
10540 else if (qualifier == ridpointers[(int) RID_RESTRICT])
10541 restrictp++;
8d08fdba
MS
10542 else if (!erred)
10543 {
10544 erred = 1;
91063b51 10545 error ("invalid type modifier within pointer declarator");
8d08fdba
MS
10546 }
10547 }
10548 if (constp > 1)
8251199e 10549 pedwarn ("duplicate `const'");
8d08fdba 10550 if (volatilep > 1)
8251199e 10551 pedwarn ("duplicate `volatile'");
91063b51
MM
10552 if (restrictp > 1)
10553 pedwarn ("duplicate `restrict'");
10554
10555 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
10556 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
10557 | (volatilep ? TYPE_QUAL_VOLATILE : 0));
a0a33927
MS
10558 if (TREE_CODE (declarator) == ADDR_EXPR
10559 && (constp || volatilep))
10560 {
10561 if (constp)
8251199e 10562 pedwarn ("discarding `const' applied to a reference");
a0a33927 10563 if (volatilep)
8251199e 10564 pedwarn ("discarding `volatile' applied to a reference");
91063b51 10565 type_quals &= ~(TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE);
a0a33927 10566 }
91063b51 10567 type = cp_build_qualified_type (type, type_quals);
8d08fdba
MS
10568 }
10569 declarator = TREE_OPERAND (declarator, 0);
10570 ctype = NULL_TREE;
10571 break;
10572
10573 case SCOPE_REF:
10574 {
10575 /* We have converted type names to NULL_TREE if the
10576 name was bogus, or to a _TYPE node, if not.
10577
10578 The variable CTYPE holds the type we will ultimately
10579 resolve to. The code here just needs to build
10580 up appropriate member types. */
10581 tree sname = TREE_OPERAND (declarator, 1);
386b8a85
JM
10582 tree t;
10583
8d08fdba
MS
10584 /* Destructors can have their visibilities changed as well. */
10585 if (TREE_CODE (sname) == BIT_NOT_EXPR)
10586 sname = TREE_OPERAND (sname, 0);
10587
10588 if (TREE_COMPLEXITY (declarator) == 0)
10589 /* This needs to be here, in case we are called
10590 multiple times. */ ;
9a68c51f
JM
10591 else if (TREE_COMPLEXITY (declarator) == -1)
10592 /* Namespace member. */
10593 pop_decl_namespace ();
8d08fdba 10594 else if (friendp && (TREE_COMPLEXITY (declarator) < 2))
2c73f9f5 10595 /* Don't fall out into global scope. Hides real bug? --eichin */ ;
5566b478
MS
10596 else if (! IS_AGGR_TYPE_CODE
10597 (TREE_CODE (TREE_OPERAND (declarator, 0))))
10598 ;
8d08fdba
MS
10599 else if (TREE_COMPLEXITY (declarator) == current_class_depth)
10600 {
9a3b49ac
MS
10601 /* Resolve any TYPENAME_TYPEs from the decl-specifier-seq
10602 that refer to ctype. They couldn't be resolved earlier
10603 because we hadn't pushed into the class yet.
10604 Example: resolve 'B<T>::type' in
10605 'B<typename B<T>::type> B<T>::f () { }'. */
10606 if (current_template_parms
10607 && uses_template_parms (type)
10608 && uses_template_parms (current_class_type))
10609 {
10610 tree args = current_template_args ();
4393e105 10611 type = tsubst (type, args, /*complain=*/1, NULL_TREE);
9a3b49ac
MS
10612 }
10613
a28e3c7f
MS
10614 /* This pop_nested_class corresponds to the
10615 push_nested_class used to push into class scope for
10616 parsing the argument list of a function decl, in
10617 qualified_id. */
b74a0560 10618 pop_nested_class ();
8d08fdba
MS
10619 TREE_COMPLEXITY (declarator) = current_class_depth;
10620 }
10621 else
10622 my_friendly_abort (16);
10623
10624 if (TREE_OPERAND (declarator, 0) == NULL_TREE)
10625 {
10626 /* We had a reference to a global decl, or
10627 perhaps we were given a non-aggregate typedef,
10628 in which case we cleared this out, and should just
10629 keep going as though it wasn't there. */
10630 declarator = sname;
10631 continue;
10632 }
10633 ctype = TREE_OPERAND (declarator, 0);
10634
386b8a85 10635 t = ctype;
7ddedda4 10636 while (t != NULL_TREE && CLASS_TYPE_P (t))
386b8a85 10637 {
75650646
MM
10638 if (CLASSTYPE_TEMPLATE_INFO (t) &&
10639 !CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
386b8a85
JM
10640 template_count += 1;
10641 t = TYPE_MAIN_DECL (t);
10642 if (DECL_LANG_SPECIFIC (t))
10643 t = DECL_CLASS_CONTEXT (t);
10644 else
10645 t = NULL_TREE;
10646 }
10647
8d08fdba
MS
10648 if (sname == NULL_TREE)
10649 goto done_scoping;
10650
10651 if (TREE_CODE (sname) == IDENTIFIER_NODE)
10652 {
10653 /* This is the `standard' use of the scoping operator:
10654 basetype :: member . */
10655
db5ae43f 10656 if (ctype == current_class_type)
28cbf42c
MS
10657 {
10658 /* class A {
10659 void A::f ();
10660 };
10661
10662 Is this ill-formed? */
10663
10664 if (pedantic)
8251199e 10665 cp_pedwarn ("extra qualification `%T::' on member `%s' ignored",
28cbf42c
MS
10666 ctype, name);
10667 }
db5ae43f 10668 else if (TREE_CODE (type) == FUNCTION_TYPE)
8d08fdba
MS
10669 {
10670 if (current_class_type == NULL_TREE
8d08fdba 10671 || friendp)
14ae7e7d
JM
10672 type = build_cplus_method_type (ctype, TREE_TYPE (type),
10673 TYPE_ARG_TYPES (type));
8d08fdba
MS
10674 else
10675 {
8251199e 10676 cp_error ("cannot declare member function `%T::%s' within `%T'",
a3203465 10677 ctype, name, current_class_type);
8d08fdba
MS
10678 return void_type_node;
10679 }
10680 }
5566b478
MS
10681 else if (RIDBIT_SETP (RID_TYPEDEF, specbits)
10682 || TYPE_SIZE (complete_type (ctype)) != NULL_TREE)
8d08fdba 10683 {
2c73f9f5 10684 /* Have to move this code elsewhere in this function.
db5ae43f
MS
10685 this code is used for i.e., typedef int A::M; M *pm;
10686
10687 It is? How? jason 10/2/94 */
8d08fdba 10688
8d08fdba
MS
10689 if (current_class_type)
10690 {
8251199e 10691 cp_error ("cannot declare member `%T::%s' within `%T'",
db5ae43f
MS
10692 ctype, name, current_class_type);
10693 return void_type_node;
8d08fdba
MS
10694 }
10695 type = build_offset_type (ctype, type);
10696 }
10697 else if (uses_template_parms (ctype))
10698 {
8d08fdba 10699 if (TREE_CODE (type) == FUNCTION_TYPE)
beb53fb8 10700 type
14ae7e7d 10701 = build_cplus_method_type (ctype, TREE_TYPE (type),
beb53fb8 10702 TYPE_ARG_TYPES (type));
8d08fdba
MS
10703 }
10704 else
10705 {
8251199e 10706 cp_error ("structure `%T' not yet defined", ctype);
8d08fdba
MS
10707 return error_mark_node;
10708 }
10709
10710 declarator = sname;
10711 }
8d08fdba
MS
10712 else if (TREE_CODE (sname) == SCOPE_REF)
10713 my_friendly_abort (17);
10714 else
10715 {
10716 done_scoping:
10717 declarator = TREE_OPERAND (declarator, 1);
10718 if (declarator && TREE_CODE (declarator) == CALL_EXPR)
10719 /* In this case, we will deal with it later. */
10720 ;
10721 else
10722 {
10723 if (TREE_CODE (type) == FUNCTION_TYPE)
14ae7e7d
JM
10724 type = build_cplus_method_type (ctype, TREE_TYPE (type),
10725 TYPE_ARG_TYPES (type));
8d08fdba
MS
10726 else
10727 type = build_offset_type (ctype, type);
10728 }
10729 }
10730 }
10731 break;
10732
10733 case BIT_NOT_EXPR:
10734 declarator = TREE_OPERAND (declarator, 0);
10735 break;
10736
8d08fdba
MS
10737 case RECORD_TYPE:
10738 case UNION_TYPE:
10739 case ENUMERAL_TYPE:
10740 declarator = NULL_TREE;
10741 break;
10742
10743 case ERROR_MARK:
10744 declarator = NULL_TREE;
10745 break;
10746
10747 default:
10748 my_friendly_abort (158);
10749 }
10750 }
10751
1eab9b56
JM
10752 /* See the comment for the TREE_LIST case, above. */
10753 if (inner_attrs)
10754 {
10755 if (! ignore_attrs)
10756 decl_attributes (type, inner_attrs, NULL_TREE);
10757 else if (attrlist)
10758 TREE_VALUE (attrlist) = chainon (inner_attrs, TREE_VALUE (attrlist));
10759 else
10760 attrlist = build_decl_list (NULL_TREE, inner_attrs);
10761 }
10762
419c6212
JM
10763 /* Now TYPE has the actual type. */
10764
db5ae43f
MS
10765 if (explicitp == 1)
10766 {
8251199e 10767 error ("only constructors can be declared `explicit'");
db5ae43f
MS
10768 explicitp = 0;
10769 }
10770
f30432d7
MS
10771 if (RIDBIT_SETP (RID_MUTABLE, specbits))
10772 {
91063b51 10773 if (type_quals & TYPE_QUAL_CONST)
f30432d7 10774 {
8251199e 10775 error ("const `%s' cannot be declared `mutable'", name);
f30432d7
MS
10776 RIDBIT_RESET (RID_MUTABLE, specbits);
10777 }
10778 else if (staticp)
10779 {
8251199e 10780 error ("static `%s' cannot be declared `mutable'", name);
f30432d7
MS
10781 RIDBIT_RESET (RID_MUTABLE, specbits);
10782 }
10783 }
10784
efe38fab
JM
10785 if (declarator == NULL_TREE
10786 || TREE_CODE (declarator) == IDENTIFIER_NODE
10787 || (TREE_CODE (declarator) == TEMPLATE_ID_EXPR
10788 && (TREE_CODE (type) == FUNCTION_TYPE
10789 || TREE_CODE (type) == METHOD_TYPE)))
10790 /* OK */;
10791 else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
10792 {
10793 cp_error ("template-id `%D' used as a declarator", declarator);
10794 declarator = dname;
10795 }
10796 else
419c6212 10797 /* Unexpected declarator format. */
efe38fab
JM
10798 my_friendly_abort (990210);
10799
419c6212
JM
10800 /* If this is declaring a typedef name, return a TYPE_DECL. */
10801
fc378698 10802 if (RIDBIT_SETP (RID_TYPEDEF, specbits) && decl_context != TYPENAME)
8d08fdba
MS
10803 {
10804 tree decl;
10805
10806 /* Note that the grammar rejects storage classes
10807 in typenames, fields or parameters. */
eff71ab0
PB
10808 if (current_lang_name == lang_name_java)
10809 TYPE_FOR_JAVA (type) = 1;
8d08fdba 10810
d2e5ee5c
MS
10811 if (decl_context == FIELD)
10812 {
094fe153 10813 if (declarator == constructor_name (current_class_type))
8251199e 10814 cp_pedwarn ("ANSI C++ forbids nested type `%D' with same name as enclosing class",
d2e5ee5c
MS
10815 declarator);
10816 decl = build_lang_decl (TYPE_DECL, declarator, type);
10817 if (IS_SIGNATURE (current_class_type) && opaque_typedef)
10818 SIGNATURE_HAS_OPAQUE_TYPEDECLS (current_class_type) = 1;
10819 }
10820 else
7fb4a8f7
JM
10821 {
10822 /* Make sure this typedef lives as long as its type,
10823 since it might be used as a template parameter. */
1aed5355
MM
10824 if (type != error_mark_node)
10825 push_obstacks (TYPE_OBSTACK (type), TYPE_OBSTACK (type));
7fb4a8f7 10826 decl = build_decl (TYPE_DECL, declarator, type);
1aed5355
MM
10827 if (type != error_mark_node)
10828 pop_obstacks ();
7fb4a8f7 10829 }
d2e5ee5c 10830
8d08fdba
MS
10831 /* If the user declares "struct {...} foo" then `foo' will have
10832 an anonymous name. Fill that name in now. Nothing can
10833 refer to it, so nothing needs know about the name change.
10834 The TYPE_NAME field was filled in by build_struct_xref. */
10835 if (type != error_mark_node
10836 && TYPE_NAME (type)
10837 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
10838 && ANON_AGGRNAME_P (TYPE_IDENTIFIER (type)))
10839 {
61a127b3
MM
10840 /* FIXME: This is bogus; we should not be doing this for
10841 cv-qualified types. */
10842
0d2a8b1b
BK
10843 /* For anonymous structs that are cv-qualified, need to use
10844 TYPE_MAIN_VARIANT so that name will mangle correctly. As
10845 type not referenced after this block, don't bother
10846 resetting type to original type, ie. TREE_TYPE (decl). */
10847 type = TYPE_MAIN_VARIANT (type);
10848
2c73f9f5 10849 /* Replace the anonymous name with the real name everywhere. */
8d08fdba 10850 lookup_tag_reverse (type, declarator);
d2e5ee5c 10851 TYPE_NAME (type) = decl;
8d08fdba
MS
10852
10853 if (TYPE_LANG_SPECIFIC (type))
10854 TYPE_WAS_ANONYMOUS (type) = 1;
10855
33964bf4
MM
10856 /* If this is a typedef within a template class, the nested
10857 type is a (non-primary) template. The name for the
10858 template needs updating as well. */
10859 if (TYPE_LANG_SPECIFIC (type) && CLASSTYPE_TEMPLATE_INFO (type))
10860 DECL_NAME (CLASSTYPE_TI_TEMPLATE (type))
10861 = TYPE_IDENTIFIER (type);
10862
2c73f9f5
ML
10863 /* XXX Temporarily set the scope.
10864 When returning, start_decl expects it as NULL_TREE,
10865 and will then then set it using pushdecl. */
10866 my_friendly_assert (DECL_CONTEXT (decl) == NULL_TREE, 980404);
10867 if (current_class_type)
10868 DECL_CONTEXT (decl) = current_class_type;
10869 else
cb0dbb9a 10870 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
2c73f9f5 10871
d2e5ee5c
MS
10872 DECL_ASSEMBLER_NAME (decl) = DECL_NAME (decl);
10873 DECL_ASSEMBLER_NAME (decl)
10874 = get_identifier (build_overload_name (type, 1, 1));
2c73f9f5 10875 DECL_CONTEXT (decl) = NULL_TREE;
50a6dbd7
JM
10876
10877 /* FIXME remangle member functions; member functions of a
10878 type with external linkage have external linkage. */
fc378698 10879 }
fc378698 10880
8d08fdba
MS
10881 if (TREE_CODE (type) == OFFSET_TYPE || TREE_CODE (type) == METHOD_TYPE)
10882 {
8251199e 10883 cp_error_at ("typedef name may not be class-qualified", decl);
a3203465 10884 return NULL_TREE;
8d08fdba
MS
10885 }
10886 else if (quals)
10887 {
10888 if (ctype == NULL_TREE)
10889 {
10890 if (TREE_CODE (type) != METHOD_TYPE)
8251199e 10891 cp_error_at ("invalid type qualifier for non-method type", decl);
8d08fdba
MS
10892 else
10893 ctype = TYPE_METHOD_BASETYPE (type);
10894 }
10895 if (ctype != NULL_TREE)
10896 grok_method_quals (ctype, decl, quals);
10897 }
10898
10899 if (RIDBIT_SETP (RID_SIGNED, specbits)
10900 || (typedef_decl && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl)))
10901 C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
10902
10903 if (RIDBIT_SETP (RID_MUTABLE, specbits))
61a127b3 10904 error ("non-object member `%s' cannot be declared mutable", name);
8d08fdba 10905
c91a56d2
MS
10906 bad_specifiers (decl, "type", virtualp, quals != NULL_TREE,
10907 inlinep, friendp, raises != NULL_TREE);
10908
10909 if (initialized)
8251199e 10910 error ("typedef declaration includes an initializer");
c91a56d2 10911
8d08fdba
MS
10912 return decl;
10913 }
10914
10915 /* Detect the case of an array type of unspecified size
10916 which came, as such, direct from a typedef name.
10917 We must copy the type, so that each identifier gets
10918 a distinct type, so that each identifier's size can be
10919 controlled separately by its own initializer. */
10920
10921 if (type == typedef_type && TREE_CODE (type) == ARRAY_TYPE
10922 && TYPE_DOMAIN (type) == NULL_TREE)
10923 {
10924 type = build_cplus_array_type (TREE_TYPE (type), TYPE_DOMAIN (type));
10925 }
10926
10927 /* If this is a type name (such as, in a cast or sizeof),
10928 compute the type and return it now. */
10929
10930 if (decl_context == TYPENAME)
10931 {
10932 /* Note that the grammar rejects storage classes
10933 in typenames, fields or parameters. */
91063b51 10934 if (type_quals != TYPE_UNQUALIFIED)
a703fb38
KG
10935 {
10936 if (IS_SIGNATURE (type))
91063b51
MM
10937 error ("type qualifiers specified for signature type");
10938 type_quals = TYPE_UNQUALIFIED;
a703fb38 10939 }
8d08fdba
MS
10940
10941 /* Special case: "friend class foo" looks like a TYPENAME context. */
10942 if (friendp)
10943 {
91063b51 10944 if (type_quals != TYPE_UNQUALIFIED)
b7484fbe 10945 {
91063b51
MM
10946 cp_error ("type qualifiers specified for friend class declaration");
10947 type_quals = TYPE_UNQUALIFIED;
b7484fbe
MS
10948 }
10949 if (inlinep)
10950 {
8251199e 10951 cp_error ("`inline' specified for friend class declaration");
b7484fbe
MS
10952 inlinep = 0;
10953 }
10954
10955 /* Only try to do this stuff if we didn't already give up. */
10956 if (type != integer_type_node)
10957 {
10958 /* A friendly class? */
10959 if (current_class_type)
10960 make_friend_class (current_class_type, TYPE_MAIN_VARIANT (type));
10961 else
8251199e 10962 error ("trying to make class `%s' a friend of global scope",
b7484fbe
MS
10963 TYPE_NAME_STRING (type));
10964 type = void_type_node;
10965 }
8d08fdba
MS
10966 }
10967 else if (quals)
10968 {
8d08fdba 10969 tree dummy = build_decl (TYPE_DECL, declarator, type);
8d08fdba
MS
10970 if (ctype == NULL_TREE)
10971 {
10972 my_friendly_assert (TREE_CODE (type) == METHOD_TYPE, 159);
10973 ctype = TYPE_METHOD_BASETYPE (type);
10974 }
10975 grok_method_quals (ctype, dummy, quals);
10976 type = TREE_TYPE (dummy);
10977 }
10978
10979 return type;
10980 }
10981 else if (declarator == NULL_TREE && decl_context != PARM
db5ae43f 10982 && decl_context != CATCHPARM
8d08fdba
MS
10983 && TREE_CODE (type) != UNION_TYPE
10984 && ! bitfield)
10985 {
8251199e 10986 cp_error ("abstract declarator `%T' used as declaration", type);
8d08fdba
MS
10987 declarator = make_anon_name ();
10988 }
10989
10990 /* `void' at top level (not within pointer)
10991 is allowed only in typedefs or type names.
10992 We don't complain about parms either, but that is because
10993 a better error message can be made later. */
10994
a1774733 10995 if (TREE_CODE (type) == VOID_TYPE && decl_context != PARM)
8d08fdba 10996 {
b7484fbe 10997 if (! declarator)
8251199e 10998 error ("unnamed variable or field declared void");
b7484fbe 10999 else if (TREE_CODE (declarator) == IDENTIFIER_NODE)
8d08fdba
MS
11000 {
11001 if (IDENTIFIER_OPNAME_P (declarator))
8d08fdba 11002 my_friendly_abort (356);
8d08fdba 11003 else
8251199e 11004 error ("variable or field `%s' declared void", name);
8d08fdba
MS
11005 }
11006 else
8251199e 11007 error ("variable or field declared void");
8d08fdba
MS
11008 type = integer_type_node;
11009 }
11010
11011 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
11012 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
11013
14ae7e7d
JM
11014 if (decl_context == PARM || decl_context == CATCHPARM)
11015 {
11016 if (ctype || in_namespace)
11017 error ("cannot use `::' in parameter declaration");
11018
11019 /* A parameter declared as an array of T is really a pointer to T.
11020 One declared as a function is really a pointer to a function.
11021 One declared as a member is really a pointer to member. */
11022
11023 if (TREE_CODE (type) == ARRAY_TYPE)
11024 {
11025 /* Transfer const-ness of array into that of type pointed to. */
11026 type = build_pointer_type (TREE_TYPE (type));
91063b51 11027 type_quals = TYPE_UNQUALIFIED;
14ae7e7d
JM
11028 }
11029 else if (TREE_CODE (type) == FUNCTION_TYPE)
11030 type = build_pointer_type (type);
11031 else if (TREE_CODE (type) == OFFSET_TYPE)
11032 type = build_pointer_type (type);
11033 else if (TREE_CODE (type) == VOID_TYPE && declarator)
11034 {
11035 error ("declaration of `%s' as void", name);
11036 return NULL_TREE;
11037 }
11038 }
11039
8d08fdba
MS
11040 {
11041 register tree decl;
11042
11043 if (decl_context == PARM)
11044 {
ff350acd 11045 decl = build_decl (PARM_DECL, declarator, type);
8d08fdba
MS
11046
11047 bad_specifiers (decl, "parameter", virtualp, quals != NULL_TREE,
11048 inlinep, friendp, raises != NULL_TREE);
11049 if (current_class_type
11050 && IS_SIGNATURE (current_class_type))
11051 {
11052 if (inlinep)
8251199e 11053 error ("parameter of signature member function declared `inline'");
8d08fdba 11054 if (RIDBIT_SETP (RID_AUTO, specbits))
8251199e 11055 error ("parameter of signature member function declared `auto'");
8d08fdba 11056 if (RIDBIT_SETP (RID_REGISTER, specbits))
8251199e 11057 error ("parameter of signature member function declared `register'");
8d08fdba
MS
11058 }
11059
11060 /* Compute the type actually passed in the parmlist,
11061 for the case where there is no prototype.
11062 (For example, shorts and chars are passed as ints.)
11063 When there is a prototype, this is overridden later. */
11064
39211cd5 11065 DECL_ARG_TYPE (decl) = type_promotes_to (type);
8d08fdba
MS
11066 }
11067 else if (decl_context == FIELD)
11068 {
11069 if (type == error_mark_node)
11070 {
11071 /* Happens when declaring arrays of sizes which
11072 are error_mark_node, for example. */
11073 decl = NULL_TREE;
11074 }
997a088c 11075 else if (in_namespace && !friendp)
05008fb9
MM
11076 {
11077 /* Something like struct S { int N::j; }; */
8251199e 11078 cp_error ("invalid use of `::'");
05008fb9
MM
11079 decl = NULL_TREE;
11080 }
8d08fdba
MS
11081 else if (TREE_CODE (type) == FUNCTION_TYPE)
11082 {
11083 int publicp = 0;
e1467ff2 11084 tree function_context;
8d08fdba 11085
72b7eeff
MS
11086 /* We catch the others as conflicts with the builtin
11087 typedefs. */
11088 if (friendp && declarator == ridpointers[(int) RID_SIGNED])
11089 {
8251199e 11090 cp_error ("function `%D' cannot be declared friend",
72b7eeff
MS
11091 declarator);
11092 friendp = 0;
11093 }
11094
8d08fdba
MS
11095 if (friendp == 0)
11096 {
11097 if (ctype == NULL_TREE)
11098 ctype = current_class_type;
11099
11100 if (ctype == NULL_TREE)
11101 {
8251199e 11102 cp_error ("can't make `%D' into a method -- not in a class",
8d08fdba
MS
11103 declarator);
11104 return void_type_node;
11105 }
11106
11107 /* ``A union may [ ... ] not [ have ] virtual functions.''
11108 ARM 9.5 */
11109 if (virtualp && TREE_CODE (ctype) == UNION_TYPE)
11110 {
8251199e 11111 cp_error ("function `%D' declared virtual inside a union",
8d08fdba
MS
11112 declarator);
11113 return void_type_node;
11114 }
11115
11116 if (declarator == ansi_opname[(int) NEW_EXPR]
a28e3c7f
MS
11117 || declarator == ansi_opname[(int) VEC_NEW_EXPR]
11118 || declarator == ansi_opname[(int) DELETE_EXPR]
11119 || declarator == ansi_opname[(int) VEC_DELETE_EXPR])
8d08fdba
MS
11120 {
11121 if (virtualp)
11122 {
8251199e 11123 cp_error ("`%D' cannot be declared virtual, since it is always static",
8d08fdba
MS
11124 declarator);
11125 virtualp = 0;
11126 }
11127 }
11128 else if (staticp < 2)
14ae7e7d
JM
11129 type = build_cplus_method_type (ctype, TREE_TYPE (type),
11130 TYPE_ARG_TYPES (type));
8d08fdba
MS
11131 }
11132
11133 /* Tell grokfndecl if it needs to set TREE_PUBLIC on the node. */
e1467ff2
MM
11134 function_context = (ctype != NULL_TREE) ?
11135 hack_decl_function_context (TYPE_MAIN_DECL (ctype)) : NULL_TREE;
11136 publicp = (! friendp || ! staticp)
11137 && function_context == NULL_TREE;
386b8a85
JM
11138 decl = grokfndecl (ctype, type,
11139 TREE_CODE (declarator) != TEMPLATE_ID_EXPR
11140 ? declarator : dname,
11141 declarator,
f30432d7 11142 virtualp, flags, quals, raises, attrlist,
386b8a85 11143 friendp ? -1 : 0, friendp, publicp, inlinep,
2c73f9f5 11144 funcdef_flag, template_count, in_namespace);
20496fa2 11145 if (decl == NULL_TREE)
3ddfb0e6 11146 return decl;
6125f3be
DE
11147#if 0
11148 /* This clobbers the attrs stored in `decl' from `attrlist'. */
11149 /* The decl and setting of decl_machine_attr is also turned off. */
71851aaa 11150 decl = build_decl_attribute_variant (decl, decl_machine_attr);
6125f3be 11151#endif
f0e01782 11152
cc804e51
MM
11153 /* [class.conv.ctor]
11154
11155 A constructor declared without the function-specifier
11156 explicit that can be called with a single parameter
11157 specifies a conversion from the type of its first
11158 parameter to the type of its class. Such a constructor
11159 is called a converting constructor. */
db5ae43f
MS
11160 if (explicitp == 2)
11161 DECL_NONCONVERTING_P (decl) = 1;
cc804e51
MM
11162 else if (DECL_CONSTRUCTOR_P (decl))
11163 {
11164 /* The constructor can be called with exactly one
11165 parameter if there is at least one parameter, and
11166 any subsequent parameters have default arguments.
11167 We don't look at the first parameter, which is
11168 really just the `this' parameter for the new
11169 object. */
11170 tree arg_types =
11171 TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)));
11172
11173 /* Skip the `in_chrg' argument too, if present. */
11174 if (TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (decl)))
11175 arg_types = TREE_CHAIN (arg_types);
11176
11177 if (arg_types == void_list_node
11178 || (arg_types
11179 && TREE_CHAIN (arg_types)
11180 && TREE_CHAIN (arg_types) != void_list_node
11181 && !TREE_PURPOSE (TREE_CHAIN (arg_types))))
11182 DECL_NONCONVERTING_P (decl) = 1;
11183 }
8d08fdba
MS
11184 }
11185 else if (TREE_CODE (type) == METHOD_TYPE)
11186 {
faae18ab
MS
11187 /* We only get here for friend declarations of
11188 members of other classes. */
8d08fdba
MS
11189 /* All method decls are public, so tell grokfndecl to set
11190 TREE_PUBLIC, also. */
386b8a85 11191 decl = grokfndecl (ctype, type, declarator, declarator,
f30432d7 11192 virtualp, flags, quals, raises, attrlist,
386b8a85 11193 friendp ? -1 : 0, friendp, 1, 0, funcdef_flag,
2c73f9f5 11194 template_count, in_namespace);
f0e01782
MS
11195 if (decl == NULL_TREE)
11196 return NULL_TREE;
8d08fdba 11197 }
5566b478
MS
11198 else if (!staticp && ! processing_template_decl
11199 && TYPE_SIZE (complete_type (type)) == NULL_TREE
8d08fdba
MS
11200 && (TREE_CODE (type) != ARRAY_TYPE || initialized == 0))
11201 {
b7484fbe 11202 if (declarator)
8251199e 11203 cp_error ("field `%D' has incomplete type", declarator);
b7484fbe 11204 else
8251199e 11205 cp_error ("name `%T' has incomplete type", type);
8d08fdba
MS
11206
11207 /* If we're instantiating a template, tell them which
11208 instantiation made the field's type be incomplete. */
11209 if (current_class_type
11210 && TYPE_NAME (current_class_type)
d2e5ee5c 11211 && IDENTIFIER_TEMPLATE (TYPE_IDENTIFIER (current_class_type))
8d08fdba
MS
11212 && declspecs && TREE_VALUE (declspecs)
11213 && TREE_TYPE (TREE_VALUE (declspecs)) == type)
8251199e 11214 cp_error (" in instantiation of template `%T'",
db5ae43f
MS
11215 current_class_type);
11216
8d08fdba
MS
11217 type = error_mark_node;
11218 decl = NULL_TREE;
11219 }
11220 else
11221 {
11222 if (friendp)
11223 {
8251199e 11224 error ("`%s' is neither function nor method; cannot be declared friend",
8d08fdba
MS
11225 IDENTIFIER_POINTER (declarator));
11226 friendp = 0;
11227 }
11228 decl = NULL_TREE;
11229 }
11230
11231 if (friendp)
11232 {
11233 /* Friends are treated specially. */
11234 if (ctype == current_class_type)
8251199e 11235 warning ("member functions are implicitly friends of their class");
8d08fdba
MS
11236 else
11237 {
11238 tree t = NULL_TREE;
11239 if (decl && DECL_NAME (decl))
36a117a5
MM
11240 {
11241 if (template_class_depth (current_class_type) == 0)
6c30752f
MM
11242 {
11243 decl
11244 = check_explicit_specialization
11245 (declarator, decl,
11246 template_count, 2 * (funcdef_flag != 0) + 4);
11247 if (decl == error_mark_node)
11248 return error_mark_node;
11249 }
11250
36a117a5
MM
11251 t = do_friend (ctype, declarator, decl,
11252 last_function_parms, flags, quals,
11253 funcdef_flag);
11254 }
8d08fdba
MS
11255 if (t && funcdef_flag)
11256 return t;
11257
11258 return void_type_node;
11259 }
11260 }
11261
11262 /* Structure field. It may not be a function, except for C++ */
11263
11264 if (decl == NULL_TREE)
11265 {
8d08fdba
MS
11266 if (initialized)
11267 {
3ac3d9ea
MM
11268 if (!staticp)
11269 {
11270 /* An attempt is being made to initialize a non-static
11271 member. But, from [class.mem]:
11272
11273 4 A member-declarator can contain a
11274 constant-initializer only if it declares a static
11275 member (_class.static_) of integral or enumeration
11276 type, see _class.static.data_.
11277
11278 This used to be relatively common practice, but
11279 the rest of the compiler does not correctly
11280 handle the initialization unless the member is
11281 static so we make it static below. */
950ad3c3 11282 cp_pedwarn ("ANSI C++ forbids initialization of member `%D'",
3ac3d9ea 11283 declarator);
8251199e 11284 cp_pedwarn ("making `%D' static", declarator);
3ac3d9ea
MM
11285 staticp = 1;
11286 }
11287
6ba89f8e
MM
11288 if (uses_template_parms (type))
11289 /* We'll check at instantiation time. */
11290 ;
11291 else if (check_static_variable_definition (declarator,
11292 type))
11293 /* If we just return the declaration, crashes
11294 will sometimes occur. We therefore return
72a93143
JM
11295 void_type_node, as if this was a friend
11296 declaration, to cause callers to completely
11297 ignore this declaration. */
6ba89f8e 11298 return void_type_node;
8d08fdba
MS
11299 }
11300
8ebeee52 11301 /* 9.2p13 [class.mem] */
4d7614e9
JM
11302 if (declarator == constructor_name (current_class_type)
11303 /* Divergence from the standard: In extern "C", we
11304 allow non-static data members here, because C does
11305 and /usr/include/netinet/in.h uses that. */
1f901793 11306 && (staticp || ! in_system_header))
8ebeee52
JM
11307 cp_pedwarn ("ANSI C++ forbids data member `%D' with same name as enclosing class",
11308 declarator);
11309
3ac3d9ea 11310 if (staticp)
8d08fdba
MS
11311 {
11312 /* C++ allows static class members.
11313 All other work for this is done by grokfield.
d7ceab7b 11314 This VAR_DCL is built by build_lang_field_decl.
8d08fdba
MS
11315 All other VAR_DECLs are built by build_decl. */
11316 decl = build_lang_field_decl (VAR_DECL, declarator, type);
5b605f68
MS
11317 TREE_STATIC (decl) = 1;
11318 /* In class context, 'static' means public access. */
3ac3d9ea 11319 TREE_PUBLIC (decl) = DECL_EXTERNAL (decl) = 1;
8d08fdba
MS
11320 }
11321 else
11322 {
11323 decl = build_lang_field_decl (FIELD_DECL, declarator, type);
11324 if (RIDBIT_SETP (RID_MUTABLE, specbits))
11325 {
11326 DECL_MUTABLE_P (decl) = 1;
11327 RIDBIT_RESET (RID_MUTABLE, specbits);
11328 }
11329 }
11330
11331 bad_specifiers (decl, "field", virtualp, quals != NULL_TREE,
11332 inlinep, friendp, raises != NULL_TREE);
11333 }
11334 }
11335 else if (TREE_CODE (type) == FUNCTION_TYPE || TREE_CODE (type) == METHOD_TYPE)
11336 {
386b8a85 11337 tree original_name;
8d08fdba
MS
11338 int publicp = 0;
11339
11340 if (! declarator)
11341 return NULL_TREE;
11342
386b8a85
JM
11343 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
11344 original_name = dname;
11345 else
11346 original_name = declarator;
11347
8926095f 11348 if (RIDBIT_SETP (RID_AUTO, specbits))
8251199e 11349 error ("storage class `auto' invalid for function `%s'", name);
8926095f 11350 else if (RIDBIT_SETP (RID_REGISTER, specbits))
8251199e 11351 error ("storage class `register' invalid for function `%s'", name);
8d08fdba
MS
11352
11353 /* Function declaration not at top level.
11354 Storage classes other than `extern' are not allowed
11355 and `extern' makes no difference. */
a9aedbc2 11356 if (! toplevel_bindings_p ()
8926095f
MS
11357 && (RIDBIT_SETP (RID_STATIC, specbits)
11358 || RIDBIT_SETP (RID_INLINE, specbits))
8d08fdba 11359 && pedantic)
8926095f
MS
11360 {
11361 if (RIDBIT_SETP (RID_STATIC, specbits))
8251199e 11362 pedwarn ("storage class `static' invalid for function `%s' declared out of global scope", name);
8926095f 11363 else
8251199e 11364 pedwarn ("storage class `inline' invalid for function `%s' declared out of global scope", name);
8926095f
MS
11365 }
11366
8d08fdba
MS
11367 if (ctype == NULL_TREE)
11368 {
11369 if (virtualp)
11370 {
8251199e 11371 error ("virtual non-class function `%s'", name);
8d08fdba
MS
11372 virtualp = 0;
11373 }
8d08fdba
MS
11374 }
11375 else if (TREE_CODE (type) == FUNCTION_TYPE && staticp < 2)
14ae7e7d
JM
11376 type = build_cplus_method_type (ctype, TREE_TYPE (type),
11377 TYPE_ARG_TYPES (type));
8d08fdba 11378
eb66be0e 11379 /* Record presence of `static'. */
faae18ab 11380 publicp = (ctype != NULL_TREE
a9aedbc2 11381 || RIDBIT_SETP (RID_EXTERN, specbits)
eb66be0e 11382 || !RIDBIT_SETP (RID_STATIC, specbits));
8d08fdba 11383
386b8a85 11384 decl = grokfndecl (ctype, type, original_name, declarator,
f30432d7 11385 virtualp, flags, quals, raises, attrlist,
75650646 11386 1, friendp,
386b8a85 11387 publicp, inlinep, funcdef_flag,
2c73f9f5 11388 template_count, in_namespace);
f0e01782
MS
11389 if (decl == NULL_TREE)
11390 return NULL_TREE;
8d08fdba 11391
8d08fdba
MS
11392 if (staticp == 1)
11393 {
11394 int illegal_static = 0;
11395
11396 /* Don't allow a static member function in a class, and forbid
11397 declaring main to be static. */
11398 if (TREE_CODE (type) == METHOD_TYPE)
11399 {
8251199e 11400 cp_pedwarn ("cannot declare member function `%D' to have static linkage", decl);
8d08fdba
MS
11401 illegal_static = 1;
11402 }
8d08fdba
MS
11403 else if (current_function_decl)
11404 {
11405 /* FIXME need arm citation */
8251199e 11406 error ("cannot declare static function inside another function");
8d08fdba
MS
11407 illegal_static = 1;
11408 }
11409
11410 if (illegal_static)
11411 {
11412 staticp = 0;
11413 RIDBIT_RESET (RID_STATIC, specbits);
11414 }
11415 }
8d08fdba
MS
11416 }
11417 else
11418 {
11419 /* It's a variable. */
11420
11421 /* An uninitialized decl with `extern' is a reference. */
2c73f9f5 11422 decl = grokvardecl (type, declarator, &specbits,
950ad3c3
MM
11423 initialized,
11424 (type_quals & TYPE_QUAL_CONST) != 0,
11425 in_namespace);
8d08fdba
MS
11426 bad_specifiers (decl, "variable", virtualp, quals != NULL_TREE,
11427 inlinep, friendp, raises != NULL_TREE);
11428
11429 if (ctype)
11430 {
f0e01782 11431 DECL_CONTEXT (decl) = ctype;
8d08fdba
MS
11432 if (staticp == 1)
11433 {
8251199e 11434 cp_pedwarn ("static member `%D' re-declared as static", decl);
8d08fdba
MS
11435 staticp = 0;
11436 RIDBIT_RESET (RID_STATIC, specbits);
11437 }
b7484fbe
MS
11438 if (RIDBIT_SETP (RID_REGISTER, specbits) && TREE_STATIC (decl))
11439 {
8251199e 11440 cp_error ("static member `%D' declared `register'", decl);
b7484fbe
MS
11441 RIDBIT_RESET (RID_REGISTER, specbits);
11442 }
f30432d7 11443 if (RIDBIT_SETP (RID_EXTERN, specbits) && pedantic)
8d08fdba 11444 {
8251199e 11445 cp_pedwarn ("cannot explicitly declare member `%#D' to have extern linkage",
f30432d7 11446 decl);
8d08fdba
MS
11447 RIDBIT_RESET (RID_EXTERN, specbits);
11448 }
11449 }
11450 }
11451
11452 if (RIDBIT_SETP (RID_MUTABLE, specbits))
11453 {
8251199e 11454 error ("`%s' cannot be declared mutable", name);
8d08fdba
MS
11455 }
11456
11457 /* Record `register' declaration for warnings on &
11458 and in case doing stupid register allocation. */
11459
11460 if (RIDBIT_SETP (RID_REGISTER, specbits))
11461 DECL_REGISTER (decl) = 1;
11462
8926095f
MS
11463 if (RIDBIT_SETP (RID_EXTERN, specbits))
11464 DECL_THIS_EXTERN (decl) = 1;
11465
faae18ab
MS
11466 if (RIDBIT_SETP (RID_STATIC, specbits))
11467 DECL_THIS_STATIC (decl) = 1;
11468
8d08fdba 11469 /* Record constancy and volatility. */
91063b51
MM
11470 /* FIXME: Disallow `restrict' pointer-to-member declarations. */
11471 c_apply_type_quals_to_decl (type_quals, decl);
8d08fdba
MS
11472
11473 return decl;
11474 }
11475}
11476\f
11477/* Tell if a parmlist/exprlist looks like an exprlist or a parmlist.
11478 An empty exprlist is a parmlist. An exprlist which
11479 contains only identifiers at the global level
11480 is a parmlist. Otherwise, it is an exprlist. */
e92cc029 11481
8d08fdba
MS
11482int
11483parmlist_is_exprlist (exprs)
11484 tree exprs;
11485{
11486 if (exprs == NULL_TREE || TREE_PARMLIST (exprs))
11487 return 0;
11488
a9aedbc2 11489 if (toplevel_bindings_p ())
8d08fdba
MS
11490 {
11491 /* At the global level, if these are all identifiers,
11492 then it is a parmlist. */
11493 while (exprs)
11494 {
11495 if (TREE_CODE (TREE_VALUE (exprs)) != IDENTIFIER_NODE)
11496 return 1;
11497 exprs = TREE_CHAIN (exprs);
11498 }
11499 return 0;
11500 }
11501 return 1;
11502}
11503
f181d4ae
MM
11504/* Subroutine of start_function. Ensure that each of the parameter
11505 types (as listed in PARMS) is complete, as is required for a
11506 function definition. */
e92cc029 11507
8d08fdba
MS
11508static void
11509require_complete_types_for_parms (parms)
11510 tree parms;
11511{
11512 while (parms)
11513 {
11514 tree type = TREE_TYPE (parms);
5566b478 11515 if (TYPE_SIZE (complete_type (type)) == NULL_TREE)
8d08fdba
MS
11516 {
11517 if (DECL_NAME (parms))
8251199e 11518 error ("parameter `%s' has incomplete type",
8d08fdba
MS
11519 IDENTIFIER_POINTER (DECL_NAME (parms)));
11520 else
8251199e 11521 error ("parameter has incomplete type");
8d08fdba
MS
11522 TREE_TYPE (parms) = error_mark_node;
11523 }
42f989ff
JM
11524 else
11525 layout_decl (parms, 0);
f181d4ae 11526
8d08fdba
MS
11527 parms = TREE_CHAIN (parms);
11528 }
11529}
11530
297e73d8
MM
11531/* Returns DECL if DECL is a local variable (or parameter). Returns
11532 NULL_TREE otherwise. */
11533
11534static tree
11535local_variable_p (t)
11536 tree t;
11537{
11538 if ((TREE_CODE (t) == VAR_DECL
11539 /* A VAR_DECL with a context that is a _TYPE is a static data
11540 member. */
11541 && !TYPE_P (CP_DECL_CONTEXT (t))
11542 /* Any other non-local variable must be at namespace scope. */
11543 && TREE_CODE (CP_DECL_CONTEXT (t)) != NAMESPACE_DECL)
11544 || (TREE_CODE (t) == PARM_DECL))
11545 return t;
11546
11547 return NULL_TREE;
11548}
11549
11550/* Check that ARG, which is a default-argument expression for a
11551 parameter DECL, is legal. Returns ARG, or ERROR_MARK_NODE, if
11552 something goes wrong. DECL may also be a _TYPE node, rather than a
11553 DECL, if there is no DECL available. */
11554
11555tree
11556check_default_argument (decl, arg)
11557 tree decl;
11558 tree arg;
11559{
11560 tree var;
11561 tree decl_type;
11562
11563 if (TREE_CODE (arg) == DEFAULT_ARG)
11564 /* We get a DEFAULT_ARG when looking at an in-class declaration
11565 with a default argument. Ignore the argument for now; we'll
11566 deal with it after the class is complete. */
11567 return arg;
11568
11569 if (processing_template_decl || uses_template_parms (arg))
11570 /* We don't do anything checking until instantiation-time. Note
11571 that there may be uninstantiated arguments even for an
11572 instantiated function, since default arguments are not
11573 instantiated until they are needed. */
11574 return arg;
11575
11576 if (TYPE_P (decl))
11577 {
11578 decl_type = decl;
11579 decl = NULL_TREE;
11580 }
11581 else
11582 decl_type = TREE_TYPE (decl);
11583
11584 if (arg == error_mark_node
11585 || decl == error_mark_node
11586 || TREE_TYPE (arg) == error_mark_node
11587 || decl_type == error_mark_node)
11588 /* Something already went wrong. There's no need to check
11589 further. */
11590 return error_mark_node;
11591
11592 /* [dcl.fct.default]
11593
11594 A default argument expression is implicitly converted to the
11595 parameter type. */
11596 if (!TREE_TYPE (arg)
11597 || !can_convert_arg (decl_type, TREE_TYPE (arg), arg))
11598 {
11599 if (decl)
11600 cp_error ("default argument for `%#D' has type `%T'",
11601 decl, TREE_TYPE (arg));
11602 else
11603 cp_error ("default argument for paramter of type `%T' has type `%T'",
11604 decl_type, TREE_TYPE (arg));
11605
11606 return error_mark_node;
11607 }
11608
11609 /* [dcl.fct.default]
11610
11611 Local variables shall not be used in default argument
11612 expressions.
11613
11614 The keyword `this' shall not be used in a default argument of a
11615 member function. */
11616 var = search_tree (arg, local_variable_p);
11617 if (var)
11618 {
11619 cp_error ("default argument `%E' uses local variable `%D'",
11620 arg, var);
11621 return error_mark_node;
11622 }
11623
11624 /* All is well. */
11625 return arg;
11626}
11627
8d08fdba
MS
11628/* Decode the list of parameter types for a function type.
11629 Given the list of things declared inside the parens,
11630 return a list of types.
11631
11632 The list we receive can have three kinds of elements:
11633 an IDENTIFIER_NODE for names given without types,
11634 a TREE_LIST node for arguments given as typespecs or names with typespecs,
11635 or void_type_node, to mark the end of an argument list
11636 when additional arguments are not permitted (... was not used).
11637
11638 FUNCDEF_FLAG is nonzero for a function definition, 0 for
11639 a mere declaration. A nonempty identifier-list gets an error message
11640 when FUNCDEF_FLAG is zero.
11641 If FUNCDEF_FLAG is 1, then parameter types must be complete.
11642 If FUNCDEF_FLAG is -1, then parameter types may be incomplete.
11643
11644 If all elements of the input list contain types,
11645 we return a list of the types.
11646 If all elements contain no type (except perhaps a void_type_node
11647 at the end), we return a null list.
11648 If some have types and some do not, it is an error, and we
11649 return a null list.
11650
11651 Also set last_function_parms to either
11652 a list of names (IDENTIFIER_NODEs) or a chain of PARM_DECLs.
11653 A list of names is converted to a chain of PARM_DECLs
11654 by store_parm_decls so that ultimately it is always a chain of decls.
11655
11656 Note that in C++, parameters can take default values. These default
11657 values are in the TREE_PURPOSE field of the TREE_LIST. It is
11658 an error to specify default values which are followed by parameters
11659 that have no default values, or an ELLIPSES. For simplicities sake,
11660 only parameters which are specified with their types can take on
11661 default values. */
11662
11663static tree
11664grokparms (first_parm, funcdef_flag)
11665 tree first_parm;
11666 int funcdef_flag;
11667{
11668 tree result = NULL_TREE;
11669 tree decls = NULL_TREE;
11670
11671 if (first_parm != NULL_TREE
11672 && TREE_CODE (TREE_VALUE (first_parm)) == IDENTIFIER_NODE)
11673 {
11674 if (! funcdef_flag)
8251199e 11675 pedwarn ("parameter names (without types) in function declaration");
8d08fdba
MS
11676 last_function_parms = first_parm;
11677 return NULL_TREE;
11678 }
11679 else if (first_parm != NULL_TREE
11680 && TREE_CODE (TREE_VALUE (first_parm)) != TREE_LIST
a1774733 11681 && TREE_CODE (TREE_VALUE (first_parm)) != VOID_TYPE)
8d08fdba
MS
11682 my_friendly_abort (145);
11683 else
11684 {
11685 /* Types were specified. This is a list of declarators
11686 each represented as a TREE_LIST node. */
11687 register tree parm, chain;
5566b478 11688 int any_init = 0, any_error = 0;
8d08fdba
MS
11689
11690 if (first_parm != NULL_TREE)
11691 {
11692 tree last_result = NULL_TREE;
11693 tree last_decl = NULL_TREE;
11694
11695 for (parm = first_parm; parm != NULL_TREE; parm = chain)
11696 {
a703fb38 11697 tree type = NULL_TREE, list_node = parm;
8d08fdba
MS
11698 register tree decl = TREE_VALUE (parm);
11699 tree init = TREE_PURPOSE (parm);
11700
11701 chain = TREE_CHAIN (parm);
11702 /* @@ weak defense against parse errors. */
a1774733
BK
11703 if (TREE_CODE (decl) != VOID_TYPE
11704 && TREE_CODE (decl) != TREE_LIST)
8d08fdba
MS
11705 {
11706 /* Give various messages as the need arises. */
11707 if (TREE_CODE (decl) == STRING_CST)
8251199e 11708 cp_error ("invalid string constant `%E'", decl);
8d08fdba 11709 else if (TREE_CODE (decl) == INTEGER_CST)
8251199e 11710 error ("invalid integer constant in parameter list, did you forget to give parameter name?");
8d08fdba
MS
11711 continue;
11712 }
11713
a1774733 11714 if (TREE_CODE (decl) != VOID_TYPE)
8d08fdba 11715 {
8d08fdba
MS
11716 decl = grokdeclarator (TREE_VALUE (decl),
11717 TREE_PURPOSE (decl),
f30432d7 11718 PARM, init != NULL_TREE,
c11b6f21 11719 NULL_TREE);
3bfdc719 11720 if (! decl || TREE_TYPE (decl) == error_mark_node)
8d08fdba 11721 continue;
01240200
MM
11722
11723 /* Top-level qualifiers on the parameters are
11724 ignored for function types. */
11725 type = TYPE_MAIN_VARIANT (TREE_TYPE (decl));
11726
a1774733 11727 if (TREE_CODE (type) == VOID_TYPE)
8d08fdba
MS
11728 decl = void_type_node;
11729 else if (TREE_CODE (type) == METHOD_TYPE)
11730 {
11731 if (DECL_NAME (decl))
fc378698 11732 /* Cannot use the decl here because
8d08fdba 11733 we don't have DECL_CONTEXT set up yet. */
8251199e 11734 cp_error ("parameter `%D' invalidly declared method type",
fc378698 11735 DECL_NAME (decl));
8d08fdba 11736 else
8251199e 11737 error ("parameter invalidly declared method type");
8d08fdba
MS
11738 type = build_pointer_type (type);
11739 TREE_TYPE (decl) = type;
11740 }
11741 else if (TREE_CODE (type) == OFFSET_TYPE)
11742 {
11743 if (DECL_NAME (decl))
8251199e 11744 cp_error ("parameter `%D' invalidly declared offset type",
fc378698 11745 DECL_NAME (decl));
8d08fdba 11746 else
8251199e 11747 error ("parameter invalidly declared offset type");
8d08fdba
MS
11748 type = build_pointer_type (type);
11749 TREE_TYPE (decl) = type;
11750 }
11751 else if (TREE_CODE (type) == RECORD_TYPE
11752 && TYPE_LANG_SPECIFIC (type)
11753 && CLASSTYPE_ABSTRACT_VIRTUALS (type))
11754 {
11755 abstract_virtuals_error (decl, type);
2c73f9f5 11756 any_error = 1; /* Seems like a good idea. */
8d08fdba
MS
11757 }
11758 else if (TREE_CODE (type) == RECORD_TYPE
11759 && TYPE_LANG_SPECIFIC (type)
11760 && IS_SIGNATURE (type))
11761 {
11762 signature_error (decl, type);
2c73f9f5 11763 any_error = 1; /* Seems like a good idea. */
8d08fdba 11764 }
482b883f
JM
11765 else if (POINTER_TYPE_P (type))
11766 {
11767 tree t = type;
11768 while (POINTER_TYPE_P (t)
11769 || (TREE_CODE (t) == ARRAY_TYPE
11770 && TYPE_DOMAIN (t) != NULL_TREE))
11771 t = TREE_TYPE (t);
11772 if (TREE_CODE (t) == ARRAY_TYPE)
57af8358
MM
11773 cp_error ("parameter type `%T' includes %s to array of unknown bound",
11774 type,
11775 TYPE_PTR_P (type) ? "pointer" : "reference");
482b883f 11776 }
8d08fdba
MS
11777 }
11778
a1774733 11779 if (TREE_CODE (decl) == VOID_TYPE)
8d08fdba
MS
11780 {
11781 if (result == NULL_TREE)
11782 {
11783 result = void_list_node;
11784 last_result = result;
11785 }
11786 else
11787 {
11788 TREE_CHAIN (last_result) = void_list_node;
11789 last_result = void_list_node;
11790 }
8d08fdba
MS
11791 if (chain
11792 && (chain != void_list_node || TREE_CHAIN (chain)))
8251199e 11793 error ("`void' in parameter list must be entire list");
8d08fdba
MS
11794 break;
11795 }
11796
11797 /* Since there is a prototype, args are passed in their own types. */
11798 DECL_ARG_TYPE (decl) = TREE_TYPE (decl);
11799#ifdef PROMOTE_PROTOTYPES
11800 if ((TREE_CODE (type) == INTEGER_TYPE
11801 || TREE_CODE (type) == ENUMERAL_TYPE)
11802 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
11803 DECL_ARG_TYPE (decl) = integer_type_node;
11804#endif
297e73d8 11805 if (!any_error && init)
8d08fdba 11806 {
297e73d8
MM
11807 any_init++;
11808 init = check_default_argument (decl, init);
8d08fdba
MS
11809 }
11810 else
11811 init = NULL_TREE;
11812
11813 if (decls == NULL_TREE)
11814 {
11815 decls = decl;
11816 last_decl = decls;
11817 }
11818 else
11819 {
11820 TREE_CHAIN (last_decl) = decl;
11821 last_decl = decl;
11822 }
66f2e88d 11823 if (! current_function_decl && TREE_PERMANENT (list_node))
8d08fdba
MS
11824 {
11825 TREE_PURPOSE (list_node) = init;
11826 TREE_VALUE (list_node) = type;
11827 TREE_CHAIN (list_node) = NULL_TREE;
11828 }
11829 else
11830 list_node = saveable_tree_cons (init, type, NULL_TREE);
11831 if (result == NULL_TREE)
11832 {
11833 result = list_node;
11834 last_result = result;
11835 }
11836 else
11837 {
11838 TREE_CHAIN (last_result) = list_node;
11839 last_result = list_node;
11840 }
11841 }
11842 if (last_result)
11843 TREE_CHAIN (last_result) = NULL_TREE;
11844 /* If there are no parameters, and the function does not end
11845 with `...', then last_decl will be NULL_TREE. */
11846 if (last_decl != NULL_TREE)
11847 TREE_CHAIN (last_decl) = NULL_TREE;
11848 }
11849 }
11850
11851 last_function_parms = decls;
11852
8d08fdba
MS
11853 return result;
11854}
42976354
BK
11855
11856/* Called from the parser to update an element of TYPE_ARG_TYPES for some
11857 FUNCTION_TYPE with the newly parsed version of its default argument, which
11858 was previously digested as text. See snarf_defarg et al in lex.c. */
11859
11860void
11861replace_defarg (arg, init)
11862 tree arg, init;
11863{
aa5f3bad 11864 if (! processing_template_decl
42976354 11865 && ! can_convert_arg (TREE_VALUE (arg), TREE_TYPE (init), init))
8251199e 11866 cp_pedwarn ("invalid type `%T' for default argument to `%T'",
42976354
BK
11867 TREE_TYPE (init), TREE_VALUE (arg));
11868 TREE_PURPOSE (arg) = init;
11869}
8d08fdba 11870\f
c11b6f21
MS
11871int
11872copy_args_p (d)
11873 tree d;
11874{
11875 tree t = FUNCTION_ARG_CHAIN (d);
67437d5b
JM
11876 if (DECL_CONSTRUCTOR_P (d)
11877 && TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (d)))
11878 t = TREE_CHAIN (t);
c11b6f21
MS
11879 if (t && TREE_CODE (TREE_VALUE (t)) == REFERENCE_TYPE
11880 && (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_VALUE (t)))
11881 == DECL_CLASS_CONTEXT (d))
11882 && (TREE_CHAIN (t) == NULL_TREE
11883 || TREE_CHAIN (t) == void_list_node
11884 || TREE_PURPOSE (TREE_CHAIN (t))))
11885 return 1;
11886 return 0;
11887}
11888
8d08fdba
MS
11889/* These memoizing functions keep track of special properties which
11890 a class may have. `grok_ctor_properties' notices whether a class
11891 has a constructor of the form X(X&), and also complains
11892 if the class has a constructor of the form X(X).
11893 `grok_op_properties' takes notice of the various forms of
11894 operator= which are defined, as well as what sorts of type conversion
11895 may apply. Both functions take a FUNCTION_DECL as an argument. */
e92cc029 11896
a0a33927 11897int
8d08fdba
MS
11898grok_ctor_properties (ctype, decl)
11899 tree ctype, decl;
11900{
11901 tree parmtypes = FUNCTION_ARG_CHAIN (decl);
11902 tree parmtype = parmtypes ? TREE_VALUE (parmtypes) : void_type_node;
11903
11904 /* When a type has virtual baseclasses, a magical first int argument is
11905 added to any ctor so we can tell if the class has been initialized
11906 yet. This could screw things up in this function, so we deliberately
11907 ignore the leading int if we're in that situation. */
711734a9 11908 if (TYPE_USES_VIRTUAL_BASECLASSES (ctype))
8d08fdba 11909 {
711734a9
JM
11910 my_friendly_assert (parmtypes
11911 && TREE_VALUE (parmtypes) == integer_type_node,
11912 980529);
8d08fdba
MS
11913 parmtypes = TREE_CHAIN (parmtypes);
11914 parmtype = TREE_VALUE (parmtypes);
11915 }
11916
f181d4ae
MM
11917 /* [class.copy]
11918
11919 A non-template constructor for class X is a copy constructor if
11920 its first parameter is of type X&, const X&, volatile X& or const
11921 volatile X&, and either there are no other parameters or else all
11922 other parameters have default arguments. */
8d08fdba 11923 if (TREE_CODE (parmtype) == REFERENCE_TYPE
0b41abe6
JM
11924 && TYPE_MAIN_VARIANT (TREE_TYPE (parmtype)) == ctype
11925 && (TREE_CHAIN (parmtypes) == NULL_TREE
8d08fdba 11926 || TREE_CHAIN (parmtypes) == void_list_node
f181d4ae
MM
11927 || TREE_PURPOSE (TREE_CHAIN (parmtypes)))
11928 && !(DECL_TEMPLATE_INSTANTIATION (decl)
11929 && is_member_template (DECL_TI_TEMPLATE (decl))))
0b41abe6
JM
11930 {
11931 TYPE_HAS_INIT_REF (ctype) = 1;
91063b51 11932 if (CP_TYPE_CONST_P (TREE_TYPE (parmtype)))
0b41abe6 11933 TYPE_HAS_CONST_INIT_REF (ctype) = 1;
8d08fdba 11934 }
f181d4ae
MM
11935 /* [class.copy]
11936
11937 A declaration of a constructor for a class X is ill-formed if its
11938 first parameter is of type (optionally cv-qualified) X and either
11939 there are no other parameters or else all other parameters have
11940 default arguments.
11941
11942 We *don't* complain about member template instantiations that
11943 have this form, though; they can occur as we try to decide what
11944 constructor to use during overload resolution. Since overload
11945 resolution will never prefer such a constructor to the
11946 non-template copy constructor (which is either explicitly or
11947 implicitly defined), there's no need to worry about their
11948 existence. Theoretically, they should never even be
11949 instantiated, but that's hard to forestall. */
0b41abe6 11950 else if (TYPE_MAIN_VARIANT (parmtype) == ctype
59e76fc6
JM
11951 && (TREE_CHAIN (parmtypes) == NULL_TREE
11952 || TREE_CHAIN (parmtypes) == void_list_node
f181d4ae
MM
11953 || TREE_PURPOSE (TREE_CHAIN (parmtypes)))
11954 && !(DECL_TEMPLATE_INSTANTIATION (decl)
11955 && is_member_template (DECL_TI_TEMPLATE (decl))))
8d08fdba 11956 {
8251199e 11957 cp_error ("invalid constructor; you probably meant `%T (const %T&)'",
0b41abe6
JM
11958 ctype, ctype);
11959 SET_IDENTIFIER_ERROR_LOCUS (DECL_NAME (decl), ctype);
11960 return 0;
8d08fdba
MS
11961 }
11962 else if (TREE_CODE (parmtype) == VOID_TYPE
11963 || TREE_PURPOSE (parmtypes) != NULL_TREE)
11964 TYPE_HAS_DEFAULT_CONSTRUCTOR (ctype) = 1;
a0a33927
MS
11965
11966 return 1;
8d08fdba
MS
11967}
11968
11969/* An operator with this name can be either unary or binary. */
e92cc029 11970
a28e3c7f
MS
11971static int
11972ambi_op_p (name)
8d08fdba
MS
11973 tree name;
11974{
11975 return (name == ansi_opname [(int) INDIRECT_REF]
11976 || name == ansi_opname [(int) ADDR_EXPR]
11977 || name == ansi_opname [(int) NEGATE_EXPR]
11978 || name == ansi_opname[(int) POSTINCREMENT_EXPR]
11979 || name == ansi_opname[(int) POSTDECREMENT_EXPR]
11980 || name == ansi_opname [(int) CONVERT_EXPR]);
11981}
11982
11983/* An operator with this name can only be unary. */
e92cc029 11984
a28e3c7f
MS
11985static int
11986unary_op_p (name)
8d08fdba
MS
11987 tree name;
11988{
11989 return (name == ansi_opname [(int) TRUTH_NOT_EXPR]
11990 || name == ansi_opname [(int) BIT_NOT_EXPR]
11991 || name == ansi_opname [(int) COMPONENT_REF]
4c571114 11992 || IDENTIFIER_TYPENAME_P (name));
8d08fdba
MS
11993}
11994
11995/* Do a little sanity-checking on how they declared their operator. */
e92cc029 11996
5566b478 11997void
8d08fdba
MS
11998grok_op_properties (decl, virtualp, friendp)
11999 tree decl;
12000 int virtualp, friendp;
12001{
12002 tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
12003 int methodp = (TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE);
12004 tree name = DECL_NAME (decl);
8d08fdba 12005
a28e3c7f
MS
12006 if (current_class_type == NULL_TREE)
12007 friendp = 1;
8d08fdba 12008
a28e3c7f
MS
12009 if (! friendp)
12010 {
f181d4ae
MM
12011 /* [class.copy]
12012
12013 A user-declared copy assignment operator X::operator= is a
12014 non-static non-template member function of class X with
12015 exactly one parameter of type X, X&, const X&, volatile X& or
12016 const volatile X&. */
12017 if (name == ansi_opname[(int) MODIFY_EXPR]
12018 && !(DECL_TEMPLATE_INSTANTIATION (decl)
12019 && is_member_template (DECL_TI_TEMPLATE (decl))))
6a629cac 12020 ;
a28e3c7f
MS
12021 else if (name == ansi_opname[(int) CALL_EXPR])
12022 TYPE_OVERLOADS_CALL_EXPR (current_class_type) = 1;
12023 else if (name == ansi_opname[(int) ARRAY_REF])
12024 TYPE_OVERLOADS_ARRAY_REF (current_class_type) = 1;
12025 else if (name == ansi_opname[(int) COMPONENT_REF]
12026 || name == ansi_opname[(int) MEMBER_REF])
12027 TYPE_OVERLOADS_ARROW (current_class_type) = 1;
12028 else if (name == ansi_opname[(int) NEW_EXPR])
12029 TYPE_GETS_NEW (current_class_type) |= 1;
12030 else if (name == ansi_opname[(int) DELETE_EXPR])
12031 TYPE_GETS_DELETE (current_class_type) |= 1;
12032 else if (name == ansi_opname[(int) VEC_NEW_EXPR])
12033 TYPE_GETS_NEW (current_class_type) |= 2;
12034 else if (name == ansi_opname[(int) VEC_DELETE_EXPR])
12035 TYPE_GETS_DELETE (current_class_type) |= 2;
12036 }
12037
12038 if (name == ansi_opname[(int) NEW_EXPR]
12039 || name == ansi_opname[(int) VEC_NEW_EXPR])
8d08fdba 12040 {
8d08fdba
MS
12041 /* When the compiler encounters the definition of A::operator new, it
12042 doesn't look at the class declaration to find out if it's static. */
a28e3c7f 12043 if (methodp)
700f8a87 12044 revert_static_member_fn (&decl, NULL, NULL);
8d08fdba
MS
12045
12046 /* Take care of function decl if we had syntax errors. */
12047 if (argtypes == NULL_TREE)
beb53fb8
JM
12048 TREE_TYPE (decl)
12049 = build_function_type (ptr_type_node,
12050 hash_tree_chain (integer_type_node,
12051 void_list_node));
8d08fdba 12052 else
a28e3c7f 12053 TREE_TYPE (decl) = coerce_new_type (TREE_TYPE (decl));
8d08fdba 12054 }
a28e3c7f
MS
12055 else if (name == ansi_opname[(int) DELETE_EXPR]
12056 || name == ansi_opname[(int) VEC_DELETE_EXPR])
8d08fdba 12057 {
a28e3c7f 12058 if (methodp)
700f8a87 12059 revert_static_member_fn (&decl, NULL, NULL);
8d08fdba
MS
12060
12061 if (argtypes == NULL_TREE)
beb53fb8
JM
12062 TREE_TYPE (decl)
12063 = build_function_type (void_type_node,
12064 hash_tree_chain (ptr_type_node,
12065 void_list_node));
8d08fdba 12066 else
a28e3c7f
MS
12067 {
12068 TREE_TYPE (decl) = coerce_delete_type (TREE_TYPE (decl));
12069
12070 if (! friendp && name == ansi_opname[(int) VEC_DELETE_EXPR]
12071 && (TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
12072 != void_list_node))
12073 TYPE_VEC_DELETE_TAKES_SIZE (current_class_type) = 1;
12074 }
8d08fdba 12075 }
8d08fdba
MS
12076 else
12077 {
12078 /* An operator function must either be a non-static member function
12079 or have at least one parameter of a class, a reference to a class,
12080 an enumeration, or a reference to an enumeration. 13.4.0.6 */
700f8a87 12081 if (! methodp || DECL_STATIC_FUNCTION_P (decl))
8d08fdba 12082 {
4c571114 12083 if (IDENTIFIER_TYPENAME_P (name)
8d08fdba
MS
12084 || name == ansi_opname[(int) CALL_EXPR]
12085 || name == ansi_opname[(int) MODIFY_EXPR]
12086 || name == ansi_opname[(int) COMPONENT_REF]
12087 || name == ansi_opname[(int) ARRAY_REF])
8251199e 12088 cp_error ("`%D' must be a nonstatic member function", decl);
8d08fdba
MS
12089 else
12090 {
12091 tree p = argtypes;
12092
700f8a87 12093 if (DECL_STATIC_FUNCTION_P (decl))
8251199e 12094 cp_error ("`%D' must be either a non-static member function or a non-member function", decl);
700f8a87 12095
8d08fdba 12096 if (p)
a1774733 12097 for (; TREE_CODE (TREE_VALUE (p)) != VOID_TYPE ; p = TREE_CHAIN (p))
8d08fdba
MS
12098 {
12099 tree arg = TREE_VALUE (p);
12100 if (TREE_CODE (arg) == REFERENCE_TYPE)
12101 arg = TREE_TYPE (arg);
12102
12103 /* This lets bad template code slip through. */
12104 if (IS_AGGR_TYPE (arg)
12105 || TREE_CODE (arg) == ENUMERAL_TYPE
73b0fce8
KL
12106 || TREE_CODE (arg) == TEMPLATE_TYPE_PARM
12107 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
8d08fdba
MS
12108 goto foundaggr;
12109 }
8251199e
JM
12110 cp_error
12111 ("`%D' must have an argument of class or enumerated type",
8d08fdba
MS
12112 decl);
12113 foundaggr:
12114 ;
12115 }
12116 }
12117
277294d7 12118 if (name == ansi_opname[(int) CALL_EXPR])
2c73f9f5 12119 return; /* No restrictions on args. */
8d08fdba 12120
9a3b49ac 12121 if (IDENTIFIER_TYPENAME_P (name) && ! DECL_TEMPLATE_INFO (decl))
a0a33927
MS
12122 {
12123 tree t = TREE_TYPE (name);
12124 if (TREE_CODE (t) == VOID_TYPE)
8251199e 12125 pedwarn ("void is not a valid type conversion operator");
a0a33927
MS
12126 else if (! friendp)
12127 {
12128 int ref = (TREE_CODE (t) == REFERENCE_TYPE);
d8e178a0 12129 const char *what = 0;
a0a33927
MS
12130 if (ref)
12131 t = TYPE_MAIN_VARIANT (TREE_TYPE (t));
12132
12133 if (t == current_class_type)
12134 what = "the same type";
9a3b49ac 12135 /* Don't force t to be complete here. */
a0a33927 12136 else if (IS_AGGR_TYPE (t)
9a3b49ac 12137 && TYPE_SIZE (t)
a0a33927
MS
12138 && DERIVED_FROM_P (t, current_class_type))
12139 what = "a base class";
12140
12141 if (what)
8251199e 12142 warning ("conversion to %s%s will never use a type conversion operator",
a0a33927
MS
12143 ref ? "a reference to " : "", what);
12144 }
12145 }
12146
8d08fdba
MS
12147 if (name == ansi_opname[(int) MODIFY_EXPR])
12148 {
12149 tree parmtype;
12150
12151 if (list_length (argtypes) != 3 && methodp)
12152 {
8251199e 12153 cp_error ("`%D' must take exactly one argument", decl);
8d08fdba
MS
12154 return;
12155 }
12156 parmtype = TREE_VALUE (TREE_CHAIN (argtypes));
12157
f0e01782 12158 if (copy_assignment_arg_p (parmtype, virtualp)
a28e3c7f 12159 && ! friendp)
8d08fdba
MS
12160 {
12161 TYPE_HAS_ASSIGN_REF (current_class_type) = 1;
f0e01782 12162 if (TREE_CODE (parmtype) != REFERENCE_TYPE
91063b51 12163 || CP_TYPE_CONST_P (TREE_TYPE (parmtype)))
8d08fdba
MS
12164 TYPE_HAS_CONST_ASSIGN_REF (current_class_type) = 1;
12165 }
12166 }
12167 else if (name == ansi_opname[(int) COND_EXPR])
12168 {
12169 /* 13.4.0.3 */
8251199e 12170 pedwarn ("ANSI C++ prohibits overloading operator ?:");
8d08fdba 12171 if (list_length (argtypes) != 4)
8251199e 12172 cp_error ("`%D' must take exactly three arguments", decl);
8d08fdba
MS
12173 }
12174 else if (ambi_op_p (name))
12175 {
12176 if (list_length (argtypes) == 2)
12177 /* prefix */;
12178 else if (list_length (argtypes) == 3)
12179 {
12180 if ((name == ansi_opname[(int) POSTINCREMENT_EXPR]
12181 || name == ansi_opname[(int) POSTDECREMENT_EXPR])
5156628f 12182 && ! processing_template_decl
007e5fea 12183 && ! same_type_p (TREE_VALUE (TREE_CHAIN (argtypes)), integer_type_node))
8d08fdba
MS
12184 {
12185 if (methodp)
8251199e 12186 cp_error ("postfix `%D' must take `int' as its argument",
8d08fdba
MS
12187 decl);
12188 else
8251199e
JM
12189 cp_error
12190 ("postfix `%D' must take `int' as its second argument",
12191 decl);
8d08fdba
MS
12192 }
12193 }
12194 else
12195 {
12196 if (methodp)
8251199e 12197 cp_error ("`%D' must take either zero or one argument", decl);
8d08fdba 12198 else
8251199e 12199 cp_error ("`%D' must take either one or two arguments", decl);
8d08fdba 12200 }
824b9a4c
MS
12201
12202 /* More Effective C++ rule 6. */
eb448459 12203 if (warn_ecpp
824b9a4c
MS
12204 && (name == ansi_opname[(int) POSTINCREMENT_EXPR]
12205 || name == ansi_opname[(int) POSTDECREMENT_EXPR]))
12206 {
12207 tree arg = TREE_VALUE (argtypes);
12208 tree ret = TREE_TYPE (TREE_TYPE (decl));
12209 if (methodp || TREE_CODE (arg) == REFERENCE_TYPE)
12210 arg = TREE_TYPE (arg);
12211 arg = TYPE_MAIN_VARIANT (arg);
12212 if (list_length (argtypes) == 2)
12213 {
12214 if (TREE_CODE (ret) != REFERENCE_TYPE
3bfdc719
MM
12215 || !same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (ret)),
12216 arg))
8251199e 12217 cp_warning ("prefix `%D' should return `%T'", decl,
824b9a4c
MS
12218 build_reference_type (arg));
12219 }
12220 else
12221 {
3bfdc719 12222 if (!same_type_p (TYPE_MAIN_VARIANT (ret), arg))
8251199e 12223 cp_warning ("postfix `%D' should return `%T'", decl, arg);
824b9a4c
MS
12224 }
12225 }
8d08fdba
MS
12226 }
12227 else if (unary_op_p (name))
12228 {
12229 if (list_length (argtypes) != 2)
12230 {
12231 if (methodp)
8251199e 12232 cp_error ("`%D' must take `void'", decl);
8d08fdba 12233 else
8251199e 12234 cp_error ("`%D' must take exactly one argument", decl);
8d08fdba
MS
12235 }
12236 }
12237 else /* if (binary_op_p (name)) */
12238 {
12239 if (list_length (argtypes) != 3)
12240 {
12241 if (methodp)
8251199e 12242 cp_error ("`%D' must take exactly one argument", decl);
8d08fdba 12243 else
8251199e 12244 cp_error ("`%D' must take exactly two arguments", decl);
8d08fdba 12245 }
824b9a4c
MS
12246
12247 /* More Effective C++ rule 7. */
eb448459 12248 if (warn_ecpp
824b9a4c
MS
12249 && (name == ansi_opname [TRUTH_ANDIF_EXPR]
12250 || name == ansi_opname [TRUTH_ORIF_EXPR]
12251 || name == ansi_opname [COMPOUND_EXPR]))
8251199e 12252 cp_warning ("user-defined `%D' always evaluates both arguments",
824b9a4c
MS
12253 decl);
12254 }
12255
12256 /* Effective C++ rule 23. */
eb448459 12257 if (warn_ecpp
824b9a4c
MS
12258 && list_length (argtypes) == 3
12259 && (name == ansi_opname [PLUS_EXPR]
12260 || name == ansi_opname [MINUS_EXPR]
12261 || name == ansi_opname [TRUNC_DIV_EXPR]
12262 || name == ansi_opname [MULT_EXPR])
12263 && TREE_CODE (TREE_TYPE (TREE_TYPE (decl))) == REFERENCE_TYPE)
8251199e 12264 cp_warning ("`%D' should return by value", decl);
8d08fdba
MS
12265
12266 /* 13.4.0.8 */
12267 if (argtypes)
12268 for (; argtypes != void_list_node ; argtypes = TREE_CHAIN (argtypes))
12269 if (TREE_PURPOSE (argtypes))
12270 {
12271 TREE_PURPOSE (argtypes) = NULL_TREE;
12272 if (name == ansi_opname[(int) POSTINCREMENT_EXPR]
12273 || name == ansi_opname[(int) POSTDECREMENT_EXPR])
12274 {
12275 if (pedantic)
8251199e 12276 cp_pedwarn ("`%D' cannot have default arguments", decl);
8d08fdba
MS
12277 }
12278 else
8251199e 12279 cp_error ("`%D' cannot have default arguments", decl);
8d08fdba
MS
12280 }
12281 }
12282}
12283\f
d8e178a0 12284static const char *
094fe153
JM
12285tag_name (code)
12286 enum tag_types code;
12287{
12288 switch (code)
12289 {
12290 case record_type:
12291 return "struct";
12292 case class_type:
12293 return "class";
12294 case union_type:
12295 return "union ";
12296 case enum_type:
12297 return "enum";
12298 case signature_type:
12299 return "signature";
12300 default:
12301 my_friendly_abort (981122);
12302 }
12303}
12304
8d08fdba
MS
12305/* Get the struct, enum or union (CODE says which) with tag NAME.
12306 Define the tag as a forward-reference if it is not defined.
12307
12308 C++: If a class derivation is given, process it here, and report
12309 an error if multiple derivation declarations are not identical.
12310
12311 If this is a definition, come in through xref_tag and only look in
12312 the current frame for the name (since C++ allows new names in any
12313 scope.) */
12314
8d08fdba 12315tree
ca107ded 12316xref_tag (code_type_node, name, globalize)
8d08fdba 12317 tree code_type_node;
ca107ded 12318 tree name;
8d08fdba
MS
12319 int globalize;
12320{
12321 enum tag_types tag_code;
12322 enum tree_code code;
12323 int temp = 0;
8d08fdba 12324 register tree ref, t;
8f032717 12325 struct binding_level *b = current_binding_level;
a80e4195 12326 int got_type = 0;
dc8263bc 12327 tree attributes = NULL_TREE;
25aab5d0 12328 tree context = NULL_TREE;
dc8263bc
JM
12329
12330 /* If we are called from the parser, code_type_node will sometimes be a
12331 TREE_LIST. This indicates that the user wrote
12332 "class __attribute__ ((foo)) bar". Extract the attributes so we can
12333 use them later. */
12334 if (TREE_CODE (code_type_node) == TREE_LIST)
12335 {
12336 attributes = TREE_PURPOSE (code_type_node);
12337 code_type_node = TREE_VALUE (code_type_node);
12338 }
8d08fdba
MS
12339
12340 tag_code = (enum tag_types) TREE_INT_CST_LOW (code_type_node);
12341 switch (tag_code)
12342 {
12343 case record_type:
12344 case class_type:
8d08fdba
MS
12345 case signature_type:
12346 code = RECORD_TYPE;
8d08fdba
MS
12347 break;
12348 case union_type:
12349 code = UNION_TYPE;
8d08fdba
MS
12350 break;
12351 case enum_type:
12352 code = ENUMERAL_TYPE;
12353 break;
12354 default:
12355 my_friendly_abort (18);
12356 }
12357
12358 /* If a cross reference is requested, look up the type
12359 already defined for this tag and return it. */
be99da77
MS
12360 if (TREE_CODE_CLASS (TREE_CODE (name)) == 't')
12361 {
12362 t = name;
a80e4195
MS
12363 name = TYPE_IDENTIFIER (t);
12364 got_type = 1;
be99da77
MS
12365 }
12366 else
12367 t = IDENTIFIER_TYPE_VALUE (name);
61a127b3 12368
73b0fce8
KL
12369 if (t && TREE_CODE (t) != code && TREE_CODE (t) != TEMPLATE_TYPE_PARM
12370 && TREE_CODE (t) != TEMPLATE_TEMPLATE_PARM)
a0a33927
MS
12371 t = NULL_TREE;
12372
8ccc31eb 12373 if (! globalize)
8d08fdba 12374 {
f3400fe2
JM
12375 /* If we know we are defining this tag, only look it up in
12376 this scope and don't try to find it as a type. */
12377 ref = lookup_tag (code, name, b, 1);
8d08fdba
MS
12378 }
12379 else
12380 {
25aab5d0 12381 if (t)
36a117a5 12382 {
25aab5d0
MM
12383 /* [dcl.type.elab] If the identifier resolves to a
12384 typedef-name or a template type-parameter, the
12385 elaborated-type-specifier is ill-formed. */
12386 if (t != TYPE_MAIN_VARIANT (t)
12387 || (CLASS_TYPE_P (t) && TYPE_WAS_ANONYMOUS (t)))
12388 cp_pedwarn ("using typedef-name `%D' after `%s'",
12389 TYPE_NAME (t), tag_name (tag_code));
12390 else if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
12391 cp_error ("using template type parameter `%T' after `%s'",
12392 t, tag_name (tag_code));
12393
12394 ref = t;
12395 }
12396 else
12397 ref = lookup_tag (code, name, b, 0);
36a117a5 12398
25aab5d0
MM
12399 if (! ref)
12400 {
12401 /* Try finding it as a type declaration. If that wins,
12402 use it. */
12403 ref = lookup_name (name, 1);
12404
12405 if (ref != NULL_TREE
12406 && processing_template_decl
12407 && DECL_CLASS_TEMPLATE_P (ref)
12408 && template_class_depth (current_class_type) == 0)
12409 /* Since GLOBALIZE is true, we're declaring a global
6757edfe 12410 template, so we want this type. */
25aab5d0 12411 ref = DECL_RESULT (ref);
6757edfe 12412
25aab5d0
MM
12413 if (ref && TREE_CODE (ref) == TYPE_DECL
12414 && TREE_CODE (TREE_TYPE (ref)) == code)
12415 ref = TREE_TYPE (ref);
12416 else
12417 ref = NULL_TREE;
12418 }
12419
12420 if (ref && current_class_type
12421 && template_class_depth (current_class_type)
12422 && PROCESSING_REAL_TEMPLATE_DECL_P ())
12423 {
12424 /* Since GLOBALIZE is non-zero, we are not looking at a
12425 definition of this tag. Since, in addition, we are currently
12426 processing a (member) template declaration of a template
12427 class, we must be very careful; consider:
12428
12429 template <class X>
12430 struct S1
12431
12432 template <class U>
12433 struct S2
12434 { template <class V>
12435 friend struct S1; };
12436
12437 Here, the S2::S1 declaration should not be confused with the
12438 outer declaration. In particular, the inner version should
12439 have a template parameter of level 2, not level 1. This
12440 would be particularly important if the member declaration
12441 were instead:
12442
12443 template <class V = U> friend struct S1;
12444
12445 say, when we should tsubst into `U' when instantiating
12446 S2. On the other hand, when presented with:
12447
12448 template <class T>
12449 struct S1 {
12450 template <class U>
12451 struct S2 {};
12452 template <class U>
12453 friend struct S2;
12454 };
12455
12456 we must find the inner binding eventually. We
12457 accomplish this by making sure that the new type we
12458 create to represent this declaration has the right
12459 TYPE_CONTEXT. */
12460 context = TYPE_CONTEXT (ref);
12461 ref = NULL_TREE;
8d08fdba
MS
12462 }
12463 }
12464
12465 push_obstacks_nochange ();
12466
12467 if (! ref)
12468 {
12469 /* If no such tag is yet defined, create a forward-reference node
12470 and record it as the "definition".
12471 When a real declaration of this type is found,
12472 the forward-reference will be altered into a real type. */
12473
12474 /* In C++, since these migrate into the global scope, we must
12475 build them on the permanent obstack. */
12476
12477 temp = allocation_temporary_p ();
12478 if (temp)
12479 end_temporary_allocation ();
12480
12481 if (code == ENUMERAL_TYPE)
12482 {
8251199e 12483 cp_error ("use of enum `%#D' without previous declaration", name);
fc378698 12484
8d08fdba
MS
12485 ref = make_node (ENUMERAL_TYPE);
12486
12487 /* Give the type a default layout like unsigned int
12488 to avoid crashing if it does not get defined. */
12489 TYPE_MODE (ref) = TYPE_MODE (unsigned_type_node);
12490 TYPE_ALIGN (ref) = TYPE_ALIGN (unsigned_type_node);
12491 TREE_UNSIGNED (ref) = 1;
12492 TYPE_PRECISION (ref) = TYPE_PRECISION (unsigned_type_node);
12493 TYPE_MIN_VALUE (ref) = TYPE_MIN_VALUE (unsigned_type_node);
12494 TYPE_MAX_VALUE (ref) = TYPE_MAX_VALUE (unsigned_type_node);
12495
12496 /* Enable us to recognize when a type is created in class context.
12497 To do nested classes correctly, this should probably be cleared
12498 out when we leave this classes scope. Currently this in only
12499 done in `start_enum'. */
12500
12501 pushtag (name, ref, globalize);
8d08fdba 12502 }
8d08fdba
MS
12503 else
12504 {
8d08fdba 12505 struct binding_level *old_b = class_binding_level;
8d08fdba
MS
12506
12507 ref = make_lang_type (code);
25aab5d0 12508 TYPE_CONTEXT (ref) = context;
8d08fdba 12509
8d08fdba
MS
12510 if (tag_code == signature_type)
12511 {
12512 SET_SIGNATURE (ref);
f376e137
MS
12513 /* Since a signature type will be turned into the type
12514 of signature tables, it's not only an interface. */
8d08fdba 12515 CLASSTYPE_INTERFACE_ONLY (ref) = 0;
f376e137
MS
12516 SET_CLASSTYPE_INTERFACE_KNOWN (ref);
12517 /* A signature doesn't have a vtable. */
00595019 12518 CLASSTYPE_VTABLE_NEEDS_WRITING (ref) = 0;
8d08fdba
MS
12519 }
12520
8d08fdba
MS
12521#ifdef NONNESTED_CLASSES
12522 /* Class types don't nest the way enums do. */
12523 class_binding_level = (struct binding_level *)0;
12524#endif
12525 pushtag (name, ref, globalize);
12526 class_binding_level = old_b;
8d08fdba
MS
12527 }
12528 }
12529 else
12530 {
8d08fdba 12531 /* If it no longer looks like a nested type, make sure it's
30394414
JM
12532 in global scope.
12533 If it is not an IDENTIFIER, this is not a declaration */
2c73f9f5 12534 if (b->namespace_p && !class_binding_level
30394414
JM
12535 && TREE_CODE (name) == IDENTIFIER_NODE)
12536 {
2c73f9f5
ML
12537 if (IDENTIFIER_NAMESPACE_VALUE (name) == NULL_TREE)
12538 SET_IDENTIFIER_NAMESPACE_VALUE (name, TYPE_NAME (ref));
30394414 12539 }
7fe6899f
MM
12540
12541 if (!globalize && processing_template_decl && IS_AGGR_TYPE (ref))
6757edfe 12542 redeclare_class_template (ref, current_template_parms);
8d08fdba
MS
12543 }
12544
8d08fdba
MS
12545 /* Until the type is defined, tentatively accept whatever
12546 structure tag the user hands us. */
12547 if (TYPE_SIZE (ref) == NULL_TREE
12548 && ref != current_class_type
12549 /* Have to check this, in case we have contradictory tag info. */
12550 && IS_AGGR_TYPE_CODE (TREE_CODE (ref)))
12551 {
12552 if (tag_code == class_type)
12553 CLASSTYPE_DECLARED_CLASS (ref) = 1;
12554 else if (tag_code == record_type || tag_code == signature_type)
12555 CLASSTYPE_DECLARED_CLASS (ref) = 0;
12556 }
12557
12558 pop_obstacks ();
12559
dc8263bc
JM
12560 TREE_TYPE (ref) = attributes;
12561
8d08fdba
MS
12562 return ref;
12563}
8ccc31eb 12564
fc378698
MS
12565tree
12566xref_tag_from_type (old, id, globalize)
12567 tree old, id;
12568 int globalize;
12569{
12570 tree code_type_node;
12571
12572 if (TREE_CODE (old) == RECORD_TYPE)
12573 code_type_node = (CLASSTYPE_DECLARED_CLASS (old)
12574 ? class_type_node : record_type_node);
12575 else
12576 code_type_node = union_type_node;
12577
12578 if (id == NULL_TREE)
12579 id = TYPE_IDENTIFIER (old);
12580
ca107ded 12581 return xref_tag (code_type_node, id, globalize);
fc378698
MS
12582}
12583
3fd71a52
MM
12584/* REF is a type (named NAME), for which we have just seen some
12585 baseclasses. BINFO is a list of those baseclasses; the
12586 TREE_PURPOSE is an access_* node, and the TREE_VALUE is the type of
12587 the base-class. CODE_TYPE_NODE indicates whether REF is a class,
12588 struct, or union. */
12589
8ccc31eb
MS
12590void
12591xref_basetypes (code_type_node, name, ref, binfo)
12592 tree code_type_node;
12593 tree name, ref;
12594 tree binfo;
12595{
12596 /* In the declaration `A : X, Y, ... Z' we mark all the types
12597 (A, X, Y, ..., Z) so we can check for duplicates. */
12598 tree binfos;
d6479fe7
MM
12599 tree base;
12600
8ccc31eb
MS
12601 int i, len;
12602 enum tag_types tag_code = (enum tag_types) TREE_INT_CST_LOW (code_type_node);
12603
12604 if (tag_code == union_type)
12605 {
8251199e 12606 cp_error ("derived union `%T' invalid", ref);
8ccc31eb
MS
12607 return;
12608 }
12609
12610 len = list_length (binfo);
12611 push_obstacks (TYPE_OBSTACK (ref), TYPE_OBSTACK (ref));
12612
d6479fe7
MM
12613 /* First, make sure that any templates in base-classes are
12614 instantiated. This ensures that if we call ourselves recursively
12615 we do not get confused about which classes are marked and which
12616 are not. */
12617 for (base = binfo; base; base = TREE_CHAIN (base))
12618 complete_type (TREE_VALUE (base));
12619
8ccc31eb
MS
12620 SET_CLASSTYPE_MARKED (ref);
12621 BINFO_BASETYPES (TYPE_BINFO (ref)) = binfos = make_tree_vec (len);
12622
12623 for (i = 0; binfo; binfo = TREE_CHAIN (binfo))
12624 {
12625 /* The base of a derived struct is public by default. */
12626 int via_public
be99da77
MS
12627 = (TREE_PURPOSE (binfo) == access_public_node
12628 || TREE_PURPOSE (binfo) == access_public_virtual_node
8ccc31eb 12629 || (tag_code != class_type
be99da77
MS
12630 && (TREE_PURPOSE (binfo) == access_default_node
12631 || TREE_PURPOSE (binfo) == access_default_virtual_node)));
d8b55a76
JM
12632 int via_protected
12633 = (TREE_PURPOSE (binfo) == access_protected_node
12634 || TREE_PURPOSE (binfo) == access_protected_virtual_node);
8ccc31eb 12635 int via_virtual
be99da77 12636 = (TREE_PURPOSE (binfo) == access_private_virtual_node
d8b55a76 12637 || TREE_PURPOSE (binfo) == access_protected_virtual_node
be99da77
MS
12638 || TREE_PURPOSE (binfo) == access_public_virtual_node
12639 || TREE_PURPOSE (binfo) == access_default_virtual_node);
12640 tree basetype = TREE_VALUE (binfo);
8ccc31eb
MS
12641 tree base_binfo;
12642
8ccc31eb
MS
12643 if (basetype && TREE_CODE (basetype) == TYPE_DECL)
12644 basetype = TREE_TYPE (basetype);
5566b478
MS
12645 if (!basetype
12646 || (TREE_CODE (basetype) != RECORD_TYPE
a80e4195 12647 && TREE_CODE (basetype) != TYPENAME_TYPE
73b0fce8
KL
12648 && TREE_CODE (basetype) != TEMPLATE_TYPE_PARM
12649 && TREE_CODE (basetype) != TEMPLATE_TEMPLATE_PARM))
8ccc31eb 12650 {
8251199e 12651 cp_error ("base type `%T' fails to be a struct or class type",
8ccc31eb
MS
12652 TREE_VALUE (binfo));
12653 continue;
12654 }
2b9dc906 12655
11b5139c 12656 GNU_xref_hier (name, basetype, via_public, via_virtual, 0);
2b9dc906 12657
8adf5b5e
JM
12658 /* This code replaces similar code in layout_basetypes.
12659 We put the complete_type first for implicit `typename'. */
d6479fe7 12660 if (TYPE_SIZE (basetype) == NULL_TREE
2b9dc906 12661 && ! (current_template_parms && uses_template_parms (basetype)))
8ccc31eb 12662 {
8251199e 12663 cp_error ("base class `%T' has incomplete type", basetype);
8ccc31eb
MS
12664 continue;
12665 }
8ccc31eb
MS
12666 else
12667 {
12668 if (CLASSTYPE_MARKED (basetype))
12669 {
12670 if (basetype == ref)
8251199e 12671 cp_error ("recursive type `%T' undefined", basetype);
8ccc31eb 12672 else
8251199e 12673 cp_error ("duplicate base type `%T' invalid", basetype);
8ccc31eb
MS
12674 continue;
12675 }
12676
eff71ab0
PB
12677 if (TYPE_FOR_JAVA (basetype)
12678 && current_lang_stack == current_lang_base)
12679 TYPE_FOR_JAVA (ref) = 1;
12680
8ccc31eb
MS
12681 /* Note that the BINFO records which describe individual
12682 inheritances are *not* shared in the lattice! They
12683 cannot be shared because a given baseclass may be
12684 inherited with different `accessibility' by different
12685 derived classes. (Each BINFO record describing an
12686 individual inheritance contains flags which say what
12687 the `accessibility' of that particular inheritance is.) */
12688
7ddedda4
MM
12689 base_binfo
12690 = make_binfo (integer_zero_node, basetype,
12691 CLASS_TYPE_P (basetype)
12692 ? TYPE_BINFO_VTABLE (basetype) : NULL_TREE,
12693 CLASS_TYPE_P (basetype)
12694 ? TYPE_BINFO_VIRTUALS (basetype) : NULL_TREE);
8ccc31eb
MS
12695
12696 TREE_VEC_ELT (binfos, i) = base_binfo;
12697 TREE_VIA_PUBLIC (base_binfo) = via_public;
12698 TREE_VIA_PROTECTED (base_binfo) = via_protected;
12699 TREE_VIA_VIRTUAL (base_binfo) = via_virtual;
12700 BINFO_INHERITANCE_CHAIN (base_binfo) = TYPE_BINFO (ref);
12701
dfbcd65a
JM
12702 /* We need to unshare the binfos now so that lookups during class
12703 definition work. */
12704 unshare_base_binfos (base_binfo);
12705
8ccc31eb 12706 SET_CLASSTYPE_MARKED (basetype);
9e9ff709 12707
8ccc31eb
MS
12708 /* We are free to modify these bits because they are meaningless
12709 at top level, and BASETYPE is a top-level type. */
12710 if (via_virtual || TYPE_USES_VIRTUAL_BASECLASSES (basetype))
12711 {
12712 TYPE_USES_VIRTUAL_BASECLASSES (ref) = 1;
12713 TYPE_USES_COMPLEX_INHERITANCE (ref) = 1;
12714 }
12715
7ddedda4
MM
12716 if (CLASS_TYPE_P (basetype))
12717 {
12718 TYPE_GETS_NEW (ref) |= TYPE_GETS_NEW (basetype);
12719 TYPE_GETS_DELETE (ref) |= TYPE_GETS_DELETE (basetype);
12720 }
12721
8ccc31eb
MS
12722 i += 1;
12723 }
12724 }
12725 if (i)
12726 TREE_VEC_LENGTH (binfos) = i;
12727 else
12728 BINFO_BASETYPES (TYPE_BINFO (ref)) = NULL_TREE;
12729
12730 if (i > 1)
12731 TYPE_USES_MULTIPLE_INHERITANCE (ref) = 1;
12732 else if (i == 1)
7ddedda4
MM
12733 {
12734 tree basetype = BINFO_TYPE (TREE_VEC_ELT (binfos, 0));
12735
12736 if (CLASS_TYPE_P (basetype))
12737 TYPE_USES_MULTIPLE_INHERITANCE (ref)
12738 = TYPE_USES_MULTIPLE_INHERITANCE (basetype);
12739 }
12740
8ccc31eb
MS
12741 if (TYPE_USES_MULTIPLE_INHERITANCE (ref))
12742 TYPE_USES_COMPLEX_INHERITANCE (ref) = 1;
12743
12744 /* Unmark all the types. */
12745 while (--i >= 0)
12746 CLEAR_CLASSTYPE_MARKED (BINFO_TYPE (TREE_VEC_ELT (binfos, i)));
12747 CLEAR_CLASSTYPE_MARKED (ref);
12748
70c532b5
MM
12749 /* Now that we know all the base-classes, set up the list of virtual
12750 bases. */
12751 CLASSTYPE_VBASECLASSES (ref) = get_vbase_types (ref);
12752
8ccc31eb
MS
12753 pop_obstacks ();
12754}
12755
8d08fdba 12756\f
8d08fdba
MS
12757/* Begin compiling the definition of an enumeration type.
12758 NAME is its name (or null if anonymous).
12759 Returns the type object, as yet incomplete.
12760 Also records info about it so that build_enumerator
12761 may be used to declare the individual values as they are read. */
12762
12763tree
12764start_enum (name)
12765 tree name;
12766{
12767 register tree enumtype = NULL_TREE;
8f032717 12768 struct binding_level *b = current_binding_level;
8d08fdba 12769
8c1e8edc 12770 /* We are wasting space here and putting these on the permanent_obstack so
6e687e5e 12771 that typeid(local enum) will work correctly. */
cbf882af 12772 push_obstacks (&permanent_obstack, &permanent_obstack);
b87692e5 12773
8d08fdba
MS
12774 /* If this is the real definition for a previous forward reference,
12775 fill in the contents in the same object that used to be the
12776 forward reference. */
12777
12778 if (name != NULL_TREE)
12779 enumtype = lookup_tag (ENUMERAL_TYPE, name, b, 1);
12780
12781 if (enumtype != NULL_TREE && TREE_CODE (enumtype) == ENUMERAL_TYPE)
8251199e 12782 cp_error ("multiple definition of `%#T'", enumtype);
8d08fdba
MS
12783 else
12784 {
12785 enumtype = make_node (ENUMERAL_TYPE);
12786 pushtag (name, enumtype, 0);
12787 }
12788
12789 if (current_class_type)
12790 TREE_ADDRESSABLE (b->tags) = 1;
c91a56d2 12791
e92cc029
MS
12792 /* We don't copy this value because build_enumerator needs to do it. */
12793 enum_next_value = integer_zero_node;
7177d104 12794 enum_overflow = 0;
8d08fdba
MS
12795
12796 GNU_xref_decl (current_function_decl, enumtype);
12797 return enumtype;
12798}
12799
12800/* After processing and defining all the values of an enumeration type,
12801 install their decls in the enumeration type and finish it off.
12802 ENUMTYPE is the type object and VALUES a list of name-value pairs.
12803 Returns ENUMTYPE. */
12804
12805tree
dbfe2124
MM
12806finish_enum (enumtype)
12807 tree enumtype;
8d08fdba 12808{
a703fb38 12809 register tree minnode = NULL_TREE, maxnode = NULL_TREE;
8d08fdba
MS
12810 /* Calculate the maximum value of any enumerator in this type. */
12811
dbfe2124 12812 tree values = TYPE_VALUES (enumtype);
8d08fdba
MS
12813 if (values)
12814 {
ed44da02 12815 tree pair;
5566b478 12816
ed44da02 12817 for (pair = values; pair; pair = TREE_CHAIN (pair))
8d08fdba 12818 {
ed44da02
MM
12819 tree decl;
12820 tree value;
12821
12822 /* The TREE_VALUE is a CONST_DECL for this enumeration
12823 constant. */
12824 decl = TREE_VALUE (pair);
12825
ed44da02
MM
12826 /* The DECL_INITIAL will be NULL if we are processing a
12827 template declaration and this enumeration constant had no
12828 explicit initializer. */
12829 value = DECL_INITIAL (decl);
079e1098 12830 if (value && !processing_template_decl)
5566b478 12831 {
079e1098
MM
12832 /* Set the TREE_TYPE for the VALUE as well. That's so
12833 that when we call decl_constant_value we get an
12834 entity of the right type (but with the constant
12835 value). Since we shouldn't ever call
12836 decl_constant_value on a template type, there's no
12837 reason to do that when processing_template_decl.
12838 And, if the expression is something like a
12839 TEMPLATE_PARM_INDEX or a CAST_EXPR doing so will
12840 wreak havoc on the intended type of the expression.
12841
12842 Of course, there's also no point in trying to compute
12843 minimum or maximum values if we're in a template. */
5566b478 12844 TREE_TYPE (value) = enumtype;
ed44da02 12845
079e1098
MM
12846 if (!minnode)
12847 minnode = maxnode = value;
12848 else if (tree_int_cst_lt (maxnode, value))
12849 maxnode = value;
12850 else if (tree_int_cst_lt (value, minnode))
12851 minnode = value;
5566b478 12852 }
ed44da02 12853
72f2bd78
MM
12854 if (processing_template_decl)
12855 /* If this is just a template, leave the CONST_DECL
12856 alone. That way tsubst_copy will find CONST_DECLs for
12857 CONST_DECLs, and not INTEGER_CSTs. */
12858 ;
12859 else
12860 /* In the list we're building up, we want the enumeration
12861 values, not the CONST_DECLs. */
12862 TREE_VALUE (pair) = value;
8d08fdba
MS
12863 }
12864 }
f376e137
MS
12865 else
12866 maxnode = minnode = integer_zero_node;
8d08fdba 12867
de22184b 12868 TYPE_VALUES (enumtype) = nreverse (values);
2986ae00 12869
5156628f 12870 if (processing_template_decl)
b87692e5 12871 {
9360ac70
MM
12872 tree scope = current_scope ();
12873 if (scope && TREE_CODE (scope) == FUNCTION_DECL)
cbf882af 12874 add_tree (build_min (TAG_DEFN, enumtype));
b87692e5 12875 }
cbf882af
MM
12876 else
12877 {
12878 int unsignedp = tree_int_cst_sgn (minnode) >= 0;
12879 int lowprec = min_precision (minnode, unsignedp);
12880 int highprec = min_precision (maxnode, unsignedp);
12881 int precision = MAX (lowprec, highprec);
12882 tree tem;
5566b478 12883
cbf882af 12884 TYPE_SIZE (enumtype) = NULL_TREE;
8d08fdba 12885
cbf882af 12886 /* Set TYPE_MIN_VALUE and TYPE_MAX_VALUE according to `precision'. */
8ccc31eb 12887
cbf882af
MM
12888 TYPE_PRECISION (enumtype) = precision;
12889 if (unsignedp)
12890 fixup_unsigned_type (enumtype);
12891 else
12892 fixup_signed_type (enumtype);
8ccc31eb 12893
cbf882af
MM
12894 if (flag_short_enums || (precision > TYPE_PRECISION (integer_type_node)))
12895 /* Use the width of the narrowest normal C type which is wide
12896 enough. */
12897 TYPE_PRECISION (enumtype) = TYPE_PRECISION (type_for_size
12898 (precision, 1));
12899 else
12900 TYPE_PRECISION (enumtype) = TYPE_PRECISION (integer_type_node);
8d08fdba 12901
cbf882af
MM
12902 TYPE_SIZE (enumtype) = 0;
12903 layout_type (enumtype);
f376e137 12904
cbf882af
MM
12905 /* Fix up all variant types of this enum type. */
12906 for (tem = TYPE_MAIN_VARIANT (enumtype); tem;
12907 tem = TYPE_NEXT_VARIANT (tem))
12908 {
12909 TYPE_VALUES (tem) = TYPE_VALUES (enumtype);
12910 TYPE_MIN_VALUE (tem) = TYPE_MIN_VALUE (enumtype);
12911 TYPE_MAX_VALUE (tem) = TYPE_MAX_VALUE (enumtype);
12912 TYPE_SIZE (tem) = TYPE_SIZE (enumtype);
12913 TYPE_SIZE_UNIT (tem) = TYPE_SIZE_UNIT (enumtype);
12914 TYPE_MODE (tem) = TYPE_MODE (enumtype);
12915 TYPE_PRECISION (tem) = TYPE_PRECISION (enumtype);
12916 TYPE_ALIGN (tem) = TYPE_ALIGN (enumtype);
12917 TREE_UNSIGNED (tem) = TREE_UNSIGNED (enumtype);
12918 }
8d08fdba 12919
cbf882af
MM
12920 /* Finish debugging output for this type. */
12921 rest_of_type_compilation (enumtype, namespace_bindings_p ());
12922 }
12923
12924 /* In start_enum we pushed obstacks. Here, we must pop them. */
12925 pop_obstacks ();
8d08fdba
MS
12926
12927 return enumtype;
12928}
12929
079e1098
MM
12930/* Build and install a CONST_DECL for an enumeration constant of the
12931 enumeration type TYPE whose NAME and VALUE (if any) are provided.
8d08fdba
MS
12932 Assignment of sequential values by default is handled here. */
12933
12934tree
079e1098
MM
12935build_enumerator (name, value, type)
12936 tree name;
12937 tree value;
12938 tree type;
8d08fdba
MS
12939{
12940 tree decl, result;
e8bd800e 12941 tree context;
8d08fdba
MS
12942
12943 /* Remove no-op casts from the value. */
12944 if (value)
12945 STRIP_TYPE_NOPS (value);
12946
5156628f 12947 if (! processing_template_decl)
5566b478
MS
12948 {
12949 /* Validate and default VALUE. */
12950 if (value != NULL_TREE)
12951 {
12952 if (TREE_READONLY_DECL_P (value))
e92cc029 12953 value = decl_constant_value (value);
5566b478
MS
12954
12955 if (TREE_CODE (value) == INTEGER_CST)
12956 {
12957 value = default_conversion (value);
12958 constant_expression_warning (value);
12959 }
12960 else
12961 {
8251199e 12962 cp_error ("enumerator value for `%D' not integer constant", name);
5566b478
MS
12963 value = NULL_TREE;
12964 }
12965 }
12966
5566b478 12967 /* Default based on previous value. */
5156628f 12968 if (value == NULL_TREE && ! processing_template_decl)
5566b478
MS
12969 {
12970 value = enum_next_value;
12971 if (enum_overflow)
8251199e 12972 cp_error ("overflow in enumeration values at `%D'", name);
5566b478
MS
12973 }
12974
12975 /* Remove no-op casts from the value. */
12976 if (value)
12977 STRIP_TYPE_NOPS (value);
013bc8af
MS
12978#if 0
12979 /* To fix MAX_VAL enum consts. (bkoz) */
e92cc029 12980 TREE_TYPE (value) = integer_type_node;
013bc8af 12981#endif
5566b478 12982 }
8d08fdba 12983
079e1098
MM
12984 /* We always have to copy here; not all INTEGER_CSTs are unshared.
12985 Even in other cases, we will later (in finish_enum) be setting the
12986 type of VALUE. */
ed44da02
MM
12987 if (value != NULL_TREE)
12988 value = copy_node (value);
12989
8d08fdba 12990 /* C++ associates enums with global, function, or class declarations. */
e8bd800e
MM
12991
12992 context = current_scope ();
12993 if (context && context == current_class_type)
12994 /* This enum declaration is local to the class. */
079e1098 12995 decl = build_lang_field_decl (CONST_DECL, name, type);
e8bd800e
MM
12996 else
12997 /* It's a global enum, or it's local to a function. (Note local to
12998 a function could mean local to a class method. */
079e1098 12999 decl = build_decl (CONST_DECL, name, type);
e8bd800e
MM
13000
13001 DECL_CONTEXT (decl) = FROB_CONTEXT (context);
13002 DECL_INITIAL (decl) = value;
13003 TREE_READONLY (decl) = 1;
13004
13005 if (context && context == current_class_type)
8f032717
MM
13006 /* In something like `struct S { enum E { i = 7 }; };' we put `i'
13007 on the TYPE_FIELDS list for `S'. (That's so that you can say
13008 things like `S::i' later.) */
13009 finish_member_declaration (decl);
e8bd800e
MM
13010 else
13011 {
13012 pushdecl (decl);
13013 GNU_xref_decl (current_function_decl, decl);
13014 }
8d08fdba 13015
5156628f 13016 if (! processing_template_decl)
5566b478
MS
13017 {
13018 /* Set basis for default for next value. */
13019 enum_next_value = build_binary_op_nodefault (PLUS_EXPR, value,
13020 integer_one_node, PLUS_EXPR);
13021 enum_overflow = tree_int_cst_lt (enum_next_value, value);
5566b478 13022 }
8d08fdba 13023
8d2733ca 13024 result = saveable_tree_cons (name, decl, NULL_TREE);
8d08fdba
MS
13025 return result;
13026}
13027
8d08fdba 13028\f
5566b478
MS
13029static int function_depth;
13030
8d08fdba
MS
13031/* Create the FUNCTION_DECL for a function definition.
13032 DECLSPECS and DECLARATOR are the parts of the declaration;
13033 they describe the function's name and the type it returns,
13034 but twisted together in a fashion that parallels the syntax of C.
13035
f181d4ae
MM
13036 If PRE_PARSED_P is non-zero then DECLARATOR is really the DECL for
13037 the function we are about to process; DECLSPECS are ignored. For
13038 example, we set PRE_PARSED_P when processing the definition of
13039 inline function that was defined in-class; the definition is
13040 actually processed when the class is complete. In this case,
13041 PRE_PARSED_P is 2. We also set PRE_PARSED_P when instanting the
13042 body of a template function, and when constructing thunk functions
13043 and such; in these cases PRE_PARSED_P is 1.
13044
8d08fdba
MS
13045 This function creates a binding context for the function body
13046 as well as setting up the FUNCTION_DECL in current_function_decl.
13047
13048 Returns 1 on success. If the DECLARATOR is not suitable for a function
13049 (it defines a datum instead), we return 0, which tells
13050 yyparse to report a parse error.
13051
13052 For C++, we must first check whether that datum makes any sense.
13053 For example, "class A local_a(1,2);" means that variable local_a
13054 is an aggregate of type A, which should have a constructor
13055 applied to it with the argument list [1, 2].
13056
13057 @@ There is currently no way to retrieve the storage
13058 @@ allocated to FUNCTION (or all of its parms) if we return
13059 @@ something we had previously. */
13060
13061int
c11b6f21
MS
13062start_function (declspecs, declarator, attrs, pre_parsed_p)
13063 tree declspecs, declarator, attrs;
8d08fdba
MS
13064 int pre_parsed_p;
13065{
5566b478 13066 tree decl1;
8d08fdba
MS
13067 tree ctype = NULL_TREE;
13068 tree fntype;
13069 tree restype;
13070 extern int have_extern_spec;
13071 extern int used_extern_spec;
13072 int doing_friend = 0;
13073
8d08fdba 13074 /* Sanity check. */
a1774733 13075 my_friendly_assert (TREE_CODE (TREE_VALUE (void_list_node)) == VOID_TYPE, 160);
8d08fdba
MS
13076 my_friendly_assert (TREE_CHAIN (void_list_node) == NULL_TREE, 161);
13077
e92cc029 13078 /* Assume, until we see it does. */
8d08fdba
MS
13079 current_function_returns_value = 0;
13080 current_function_returns_null = 0;
fd378c9d
JM
13081 named_labels = 0;
13082 shadowed_labels = 0;
8d08fdba
MS
13083 current_function_assigns_this = 0;
13084 current_function_just_assigned_this = 0;
13085 current_function_parms_stored = 0;
13086 original_result_rtx = NULL_RTX;
a9aedbc2 13087 base_init_expr = NULL_TREE;
79ff2c6c
MS
13088 current_base_init_list = NULL_TREE;
13089 current_member_init_list = NULL_TREE;
f30432d7 13090 ctor_label = dtor_label = NULL_TREE;
42976354 13091 static_labelno = 0;
8d08fdba
MS
13092
13093 clear_temp_name ();
13094
e92cc029 13095 /* This should only be done once on the top most decl. */
8d08fdba
MS
13096 if (have_extern_spec && !used_extern_spec)
13097 {
13098 declspecs = decl_tree_cons (NULL_TREE, get_identifier ("extern"), declspecs);
13099 used_extern_spec = 1;
13100 }
13101
13102 if (pre_parsed_p)
13103 {
13104 decl1 = declarator;
13105
67f7c391
JM
13106#if 0
13107 /* What was this testing for, exactly? */
8d08fdba
MS
13108 if (! DECL_ARGUMENTS (decl1)
13109 && !DECL_STATIC_FUNCTION_P (decl1)
67f7c391 13110 && !DECL_ARTIFICIAL (decl1)
6eb3bb27 13111 && DECL_CLASS_SCOPE_P (decl1)
d2e5ee5c
MS
13112 && TYPE_IDENTIFIER (DECL_CONTEXT (decl1))
13113 && IDENTIFIER_TEMPLATE (TYPE_IDENTIFIER (DECL_CONTEXT (decl1))))
8d08fdba 13114 {
30394414
JM
13115 tree binding = binding_for_name (DECL_NAME (decl1),
13116 current_namespace);
8251199e 13117 cp_error ("redeclaration of `%#D'", decl1);
8d08fdba 13118 if (IDENTIFIER_CLASS_VALUE (DECL_NAME (decl1)))
8251199e 13119 cp_error_at ("previous declaration here", IDENTIFIER_CLASS_VALUE (DECL_NAME (decl1)));
30394414 13120 else if (BINDING_VALUE (binding))
8251199e 13121 cp_error_at ("previous declaration here", BINDING_VALUE (binding));
8d08fdba 13122 }
67f7c391 13123#endif
8d08fdba 13124
8d08fdba
MS
13125 fntype = TREE_TYPE (decl1);
13126 if (TREE_CODE (fntype) == METHOD_TYPE)
13127 ctype = TYPE_METHOD_BASETYPE (fntype);
13128
13129 /* ANSI C++ June 5 1992 WP 11.4.5. A friend function defined in a
13130 class is in the (lexical) scope of the class in which it is
13131 defined. */
13132 if (!ctype && DECL_FRIEND_P (decl1))
13133 {
13134 ctype = DECL_CLASS_CONTEXT (decl1);
13135
13136 /* CTYPE could be null here if we're dealing with a template;
13137 for example, `inline friend float foo()' inside a template
13138 will have no CTYPE set. */
13139 if (ctype && TREE_CODE (ctype) != RECORD_TYPE)
13140 ctype = NULL_TREE;
13141 else
13142 doing_friend = 1;
13143 }
13144
5566b478
MS
13145 last_function_parms = DECL_ARGUMENTS (decl1);
13146 last_function_parm_tags = NULL_TREE;
8d08fdba
MS
13147 }
13148 else
13149 {
c11b6f21 13150 decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, 1, NULL_TREE);
8d08fdba
MS
13151 /* If the declarator is not suitable for a function definition,
13152 cause a syntax error. */
13153 if (decl1 == NULL_TREE || TREE_CODE (decl1) != FUNCTION_DECL) return 0;
13154
13155 fntype = TREE_TYPE (decl1);
13156
13157 restype = TREE_TYPE (fntype);
7ddedda4 13158 if (CLASS_TYPE_P (restype) && !CLASSTYPE_GOT_SEMICOLON (restype))
8d08fdba 13159 {
8251199e 13160 cp_error ("semicolon missing after declaration of `%#T'", restype);
e66d884e 13161 shadow_tag (build_expr_list (NULL_TREE, restype));
8d08fdba
MS
13162 CLASSTYPE_GOT_SEMICOLON (restype) = 1;
13163 if (TREE_CODE (fntype) == FUNCTION_TYPE)
13164 fntype = build_function_type (integer_type_node,
13165 TYPE_ARG_TYPES (fntype));
13166 else
13167 fntype = build_cplus_method_type (build_type_variant (TYPE_METHOD_BASETYPE (fntype), TREE_READONLY (decl1), TREE_SIDE_EFFECTS (decl1)),
13168 integer_type_node,
13169 TYPE_ARG_TYPES (fntype));
13170 TREE_TYPE (decl1) = fntype;
13171 }
13172
13173 if (TREE_CODE (fntype) == METHOD_TYPE)
13174 ctype = TYPE_METHOD_BASETYPE (fntype);
35680744 13175 else if (DECL_MAIN_P (decl1))
8d08fdba
MS
13176 {
13177 /* If this doesn't return integer_type, complain. */
13178 if (TREE_TYPE (TREE_TYPE (decl1)) != integer_type_node)
13179 {
a28e3c7f 13180 if (pedantic || warn_return_type)
8251199e 13181 pedwarn ("return type for `main' changed to `int'");
8d08fdba
MS
13182 TREE_TYPE (decl1) = fntype = default_function_type;
13183 }
8d08fdba
MS
13184 }
13185 }
13186
13187 /* Warn if function was previously implicitly declared
13188 (but not if we warned then). */
13189 if (! warn_implicit
13190 && IDENTIFIER_IMPLICIT_DECL (DECL_NAME (decl1)) != NULL_TREE)
8251199e 13191 cp_warning_at ("`%D' implicitly declared before its definition", IDENTIFIER_IMPLICIT_DECL (DECL_NAME (decl1)));
8d08fdba 13192
f181d4ae
MM
13193 announce_function (decl1);
13194
13195 /* Set up current_class_type, and enter the scope of the class, if
13196 appropriate. */
13197 if (ctype)
13198 push_nested_class (ctype, 1);
13199 else if (DECL_STATIC_FUNCTION_P (decl1))
13200 push_nested_class (DECL_CONTEXT (decl1), 2);
13201
13202 /* Now that we have entered the scope of the class, we must restore
13203 the bindings for any template parameters surrounding DECL1, if it
13204 is an inline member template. (Order is important; consider the
13205 case where a template parameter has the same name as a field of
13206 the class.) It is not until after this point that
13207 PROCESSING_TEMPLATE_DECL is guaranteed to be set up correctly. */
13208 if (pre_parsed_p == 2)
13209 maybe_begin_member_template_processing (decl1);
13210
13211 /* We are now in the scope of the function being defined. */
8d08fdba 13212 current_function_decl = decl1;
f181d4ae 13213
5566b478
MS
13214 /* Save the parm names or decls from this function's declarator
13215 where store_parm_decls will find them. */
13216 current_function_parms = last_function_parms;
13217 current_function_parm_tags = last_function_parm_tags;
8d08fdba 13218
5156628f 13219 if (! processing_template_decl)
8d08fdba 13220 {
d490621d
MM
13221 /* In a function definition, arg types must be complete. */
13222 require_complete_types_for_parms (current_function_parms);
f181d4ae 13223
5566b478
MS
13224 if (TYPE_SIZE (complete_type (TREE_TYPE (fntype))) == NULL_TREE)
13225 {
8251199e 13226 cp_error ("return-type `%#T' is an incomplete type",
5566b478 13227 TREE_TYPE (fntype));
8d08fdba 13228
5566b478
MS
13229 /* Make it return void instead, but don't change the
13230 type of the DECL_RESULT, in case we have a named return value. */
13231 if (ctype)
13232 TREE_TYPE (decl1)
13233 = build_cplus_method_type (build_type_variant (ctype,
13234 TREE_READONLY (decl1),
13235 TREE_SIDE_EFFECTS (decl1)),
13236 void_type_node,
13237 FUNCTION_ARG_CHAIN (decl1));
13238 else
13239 TREE_TYPE (decl1)
13240 = build_function_type (void_type_node,
13241 TYPE_ARG_TYPES (TREE_TYPE (decl1)));
13242 DECL_RESULT (decl1)
13243 = build_decl (RESULT_DECL, 0, TYPE_MAIN_VARIANT (TREE_TYPE (fntype)));
91063b51
MM
13244 TREE_READONLY (DECL_RESULT (decl1))
13245 = CP_TYPE_CONST_P (TREE_TYPE (fntype));
13246 TREE_THIS_VOLATILE (DECL_RESULT (decl1))
13247 = CP_TYPE_VOLATILE_P (TREE_TYPE (fntype));
5566b478
MS
13248 }
13249
13250 if (TYPE_LANG_SPECIFIC (TREE_TYPE (fntype))
13251 && CLASSTYPE_ABSTRACT_VIRTUALS (TREE_TYPE (fntype)))
13252 abstract_virtuals_error (decl1, TREE_TYPE (fntype));
13253 }
b7484fbe 13254
824b9a4c 13255 /* Effective C++ rule 15. See also c_expand_return. */
eb448459 13256 if (warn_ecpp
824b9a4c 13257 && DECL_NAME (decl1) == ansi_opname[(int) MODIFY_EXPR]
a1774733 13258 && TREE_CODE (TREE_TYPE (fntype)) == VOID_TYPE)
8251199e 13259 cp_warning ("`operator=' should return a reference to `*this'");
824b9a4c 13260
2a2480e1
JM
13261 /* Make the init_value nonzero so pushdecl knows this is not tentative.
13262 error_mark_node is replaced below (in poplevel) with the BLOCK. */
13263 DECL_INITIAL (decl1) = error_mark_node;
13264
3a846e6e 13265#ifdef SET_DEFAULT_DECL_ATTRIBUTES
76fb7947 13266 SET_DEFAULT_DECL_ATTRIBUTES (decl1, attrs);
3a846e6e
NC
13267#endif
13268
8d08fdba
MS
13269 /* This function exists in static storage.
13270 (This does not mean `static' in the C sense!) */
13271 TREE_STATIC (decl1) = 1;
13272
36a117a5
MM
13273 /* We must call push_template_decl after current_class_type is set
13274 up. (If we are processing inline definitions after exiting a
13275 class scope, current_class_type will be NULL_TREE until set above
13276 by push_nested_class.) */
13277 if (processing_template_decl)
13278 decl1 = push_template_decl (decl1);
13279
878cd289
MS
13280 /* Record the decl so that the function name is defined.
13281 If we already have a decl for this name, and it is a FUNCTION_DECL,
13282 use the old decl. */
36a117a5 13283 if (!processing_template_decl && pre_parsed_p == 0)
878cd289 13284 {
75650646 13285 /* A specialization is not used to guide overload resolution. */
959d8796
JM
13286 if ((flag_guiding_decls
13287 || !DECL_TEMPLATE_SPECIALIZATION (decl1))
13288 && ! DECL_FUNCTION_MEMBER_P (decl1))
75650646 13289 decl1 = pushdecl (decl1);
2c73f9f5 13290 else
b7698cf0
JM
13291 {
13292 /* We need to set the DECL_CONTEXT. */
13293 if (!DECL_CONTEXT (decl1) && DECL_TEMPLATE_INFO (decl1))
13294 DECL_CONTEXT (decl1) = DECL_CONTEXT (DECL_TI_TEMPLATE (decl1));
13295 /* And make sure we have enough default args. */
13296 check_default_args (decl1);
13297 }
878cd289
MS
13298 DECL_MAIN_VARIANT (decl1) = decl1;
13299 fntype = TREE_TYPE (decl1);
13300 }
5566b478
MS
13301
13302 current_function_decl = decl1;
878cd289 13303
db5ae43f 13304 if (DECL_INTERFACE_KNOWN (decl1))
faae18ab 13305 {
86052cc3
JM
13306 tree ctx = hack_decl_function_context (decl1);
13307
faae18ab
MS
13308 if (DECL_NOT_REALLY_EXTERN (decl1))
13309 DECL_EXTERNAL (decl1) = 0;
86052cc3
JM
13310
13311 if (ctx != NULL_TREE && DECL_THIS_INLINE (ctx)
13312 && TREE_PUBLIC (ctx))
13313 /* This is a function in a local class in an extern inline
13314 function. */
13315 comdat_linkage (decl1);
faae18ab 13316 }
8d08fdba
MS
13317 /* If this function belongs to an interface, it is public.
13318 If it belongs to someone else's interface, it is also external.
1f901793 13319 This only affects inlines and template instantiations. */
5566b478
MS
13320 else if (interface_unknown == 0
13321 && (! DECL_TEMPLATE_INSTANTIATION (decl1)
d11ad92e 13322 || flag_alt_external_templates))
8d08fdba 13323 {
5566b478 13324 if (DECL_THIS_INLINE (decl1) || DECL_TEMPLATE_INSTANTIATION (decl1)
5156628f 13325 || processing_template_decl)
1f901793
JM
13326 {
13327 DECL_EXTERNAL (decl1)
13328 = (interface_only
9c73ec84
MS
13329 || (DECL_THIS_INLINE (decl1) && ! flag_implement_inlines
13330 && !DECL_VINDEX (decl1)));
1f901793
JM
13331
13332 /* For WIN32 we also want to put these in linkonce sections. */
13333 maybe_make_one_only (decl1);
13334 }
db5ae43f 13335 else
893de33c 13336 DECL_EXTERNAL (decl1) = 0;
e8abc66f 13337 DECL_NOT_REALLY_EXTERN (decl1) = 0;
db5ae43f 13338 DECL_INTERFACE_KNOWN (decl1) = 1;
8d08fdba 13339 }
c16c47fb
JM
13340 else if (interface_unknown && interface_only
13341 && (! DECL_TEMPLATE_INSTANTIATION (decl1)
13342 || flag_alt_external_templates))
13343 {
13344 /* If MULTIPLE_SYMBOL_SPACES is defined and we saw a #pragma
13345 interface, we will have interface_only set but not
13346 interface_known. In that case, we don't want to use the normal
13347 heuristics because someone will supply a #pragma implementation
13348 elsewhere, and deducing it here would produce a conflict. */
13349 comdat_linkage (decl1);
13350 DECL_EXTERNAL (decl1) = 0;
13351 DECL_INTERFACE_KNOWN (decl1) = 1;
13352 DECL_DEFER_OUTPUT (decl1) = 1;
13353 }
8d08fdba 13354 else
a0a33927
MS
13355 {
13356 /* This is a definition, not a reference.
b7484fbe
MS
13357 So clear DECL_EXTERNAL. */
13358 DECL_EXTERNAL (decl1) = 0;
faae18ab 13359
5566b478
MS
13360 if ((DECL_THIS_INLINE (decl1) || DECL_TEMPLATE_INSTANTIATION (decl1))
13361 && ! DECL_INTERFACE_KNOWN (decl1)
13362 /* Don't try to defer nested functions for now. */
e76a2646 13363 && ! hack_decl_function_context (decl1))
878cd289
MS
13364 DECL_DEFER_OUTPUT (decl1) = 1;
13365 else
893de33c 13366 DECL_INTERFACE_KNOWN (decl1) = 1;
db5ae43f 13367 }
a9aedbc2 13368
8d08fdba
MS
13369 if (ctype != NULL_TREE && DECL_STATIC_FUNCTION_P (decl1))
13370 {
13371 if (TREE_CODE (fntype) == METHOD_TYPE)
13372 TREE_TYPE (decl1) = fntype
13373 = build_function_type (TREE_TYPE (fntype),
13374 TREE_CHAIN (TYPE_ARG_TYPES (fntype)));
5566b478
MS
13375 current_function_parms = TREE_CHAIN (current_function_parms);
13376 DECL_ARGUMENTS (decl1) = current_function_parms;
8d08fdba
MS
13377 ctype = NULL_TREE;
13378 }
13379 restype = TREE_TYPE (fntype);
13380
8d08fdba
MS
13381 if (ctype)
13382 {
8d08fdba 13383 /* If we're compiling a friend function, neither of the variables
4ac14744 13384 current_class_ptr nor current_class_type will have values. */
8d08fdba
MS
13385 if (! doing_friend)
13386 {
13387 /* We know that this was set up by `grokclassfn'.
13388 We do not wait until `store_parm_decls', since evil
13389 parse errors may never get us to that point. Here
13390 we keep the consistency between `current_class_type'
4ac14744 13391 and `current_class_ptr'. */
5566b478 13392 tree t = current_function_parms;
5f34005f
RK
13393
13394 my_friendly_assert (t != NULL_TREE
13395 && TREE_CODE (t) == PARM_DECL, 162);
13396
dff6b454
RK
13397 if (TREE_CODE (TREE_TYPE (t)) == POINTER_TYPE)
13398 {
7ddedda4 13399 int i;
dff6b454 13400
7ddedda4
MM
13401 if (! hack_decl_function_context (decl1))
13402 temporary_allocation ();
13403 i = suspend_momentary ();
13404
13405 /* Normally, build_indirect_ref returns
13406 current_class_ref whenever current_class_ptr is
13407 dereferenced. This time, however, we want it to
13408 *create* current_class_ref, so we temporarily clear
13409 current_class_ptr to fool it. */
4ac14744
MS
13410 current_class_ptr = NULL_TREE;
13411 current_class_ref = build_indirect_ref (t, NULL_PTR);
13412 current_class_ptr = t;
7ddedda4 13413
dff6b454 13414 resume_momentary (i);
7ddedda4
MM
13415 if (! hack_decl_function_context (decl1))
13416 end_temporary_allocation ();
dff6b454
RK
13417 }
13418 else
13419 /* We're having a signature pointer here. */
4ac14744 13420 current_class_ref = current_class_ptr = t;
dff6b454 13421
8d08fdba
MS
13422 }
13423 }
13424 else
7ddedda4 13425 current_class_ptr = current_class_ref = NULL_TREE;
8d08fdba 13426
db5ae43f
MS
13427 pushlevel (0);
13428 current_binding_level->parm_flag = 1;
13429
db5ae43f
MS
13430 GNU_xref_function (decl1, current_function_parms);
13431
f30432d7
MS
13432 if (attrs)
13433 cplus_decl_attributes (decl1, NULL_TREE, attrs);
3a846e6e 13434
db5ae43f
MS
13435 make_function_rtl (decl1);
13436
8d08fdba
MS
13437 /* Promote the value to int before returning it. */
13438 if (C_PROMOTING_INTEGER_TYPE_P (restype))
5566b478
MS
13439 restype = type_promotes_to (restype);
13440
13441 /* If this fcn was already referenced via a block-scope `extern' decl
13442 (or an implicit decl), propagate certain information about the usage. */
13443 if (TREE_ADDRESSABLE (DECL_ASSEMBLER_NAME (decl1)))
13444 TREE_ADDRESSABLE (decl1) = 1;
13445
8d08fdba 13446 if (DECL_RESULT (decl1) == NULL_TREE)
f30432d7
MS
13447 {
13448 DECL_RESULT (decl1)
13449 = build_decl (RESULT_DECL, 0, TYPE_MAIN_VARIANT (restype));
91063b51
MM
13450 TREE_READONLY (DECL_RESULT (decl1)) = CP_TYPE_CONST_P (restype);
13451 TREE_THIS_VOLATILE (DECL_RESULT (decl1)) = CP_TYPE_VOLATILE_P (restype);
f30432d7 13452 }
8d08fdba 13453
948edfa7
JW
13454 /* Allocate further tree nodes temporarily during compilation
13455 of this function only. Tiemann moved up here from bottom of fn. */
13456 /* If this is a nested function, then we must continue to allocate RTL
13457 on the permanent obstack in case we need to inline it later. */
13458 if (! hack_decl_function_context (decl1))
5566b478
MS
13459 temporary_allocation ();
13460
5156628f 13461 if (processing_template_decl)
5566b478 13462 {
5566b478
MS
13463 ++minimal_parse_mode;
13464 last_tree = DECL_SAVED_TREE (decl1)
13465 = build_nt (EXPR_STMT, void_zero_node);
13466 }
13467
13468 ++function_depth;
13469
f30432d7
MS
13470 if (DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (decl1))
13471 && DECL_LANGUAGE (decl1) == lang_cplusplus)
8d08fdba
MS
13472 {
13473 dtor_label = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE);
13474 ctor_label = NULL_TREE;
13475 }
13476 else
13477 {
13478 dtor_label = NULL_TREE;
13479 if (DECL_CONSTRUCTOR_P (decl1))
13480 ctor_label = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE);
13481 }
13482
8d08fdba
MS
13483 return 1;
13484}
13485\f
9664b89e
JM
13486/* Called after store_parm_decls for a function-try-block. We need to update
13487 last_parm_cleanup_insn so that the base initializers for a constructor
13488 are run within this block, not before it. */
6467930b
MS
13489
13490void
13491expand_start_early_try_stmts ()
13492{
6467930b 13493 expand_start_try_stmts ();
9664b89e 13494 last_parm_cleanup_insn = get_last_insn ();
f30432d7
MS
13495}
13496
8d08fdba
MS
13497/* Store the parameter declarations into the current function declaration.
13498 This is called after parsing the parameter declarations, before
13499 digesting the body of the function.
13500
13501 Also install to binding contour return value identifier, if any. */
13502
13503void
13504store_parm_decls ()
13505{
13506 register tree fndecl = current_function_decl;
13507 register tree parm;
13508 int parms_have_cleanups = 0;
eb66be0e 13509 tree cleanups = NULL_TREE;
8d08fdba
MS
13510
13511 /* This is either a chain of PARM_DECLs (when a prototype is used). */
13512 tree specparms = current_function_parms;
13513
13514 /* This is a list of types declared among parms in a prototype. */
13515 tree parmtags = current_function_parm_tags;
13516
13517 /* This is a chain of any other decls that came in among the parm
13518 declarations. If a parm is declared with enum {foo, bar} x;
13519 then CONST_DECLs for foo and bar are put here. */
13520 tree nonparms = NULL_TREE;
13521
a9aedbc2 13522 if (toplevel_bindings_p ())
8d08fdba
MS
13523 fatal ("parse errors have confused me too much");
13524
13525 /* Initialize RTL machinery. */
13526 init_function_start (fndecl, input_filename, lineno);
13527
8d08fdba
MS
13528 /* Create a binding level for the parms. */
13529 expand_start_bindings (0);
13530
8d08fdba
MS
13531 if (specparms != NULL_TREE)
13532 {
13533 /* This case is when the function was defined with an ANSI prototype.
13534 The parms already have decls, so we need not do anything here
13535 except record them as in effect
13536 and complain if any redundant old-style parm decls were written. */
13537
13538 register tree next;
13539
13540 /* Must clear this because it might contain TYPE_DECLs declared
13541 at class level. */
13542 storedecls (NULL_TREE);
5566b478 13543
8d08fdba
MS
13544 for (parm = nreverse (specparms); parm; parm = next)
13545 {
13546 next = TREE_CHAIN (parm);
13547 if (TREE_CODE (parm) == PARM_DECL)
13548 {
5566b478 13549 tree cleanup;
8d08fdba
MS
13550 if (DECL_NAME (parm) == NULL_TREE)
13551 {
8d08fdba 13552 pushdecl (parm);
8d08fdba 13553 }
a1774733 13554 else if (TREE_CODE (TREE_TYPE (parm)) == VOID_TYPE)
8251199e 13555 cp_error ("parameter `%D' declared void", parm);
8d08fdba
MS
13556 else
13557 {
13558 /* Now fill in DECL_REFERENCE_SLOT for any of the parm decls.
13559 A parameter is assumed not to have any side effects.
13560 If this should change for any reason, then this
13561 will have to wrap the bashed reference type in a save_expr.
13562
13563 Also, if the parameter type is declared to be an X
13564 and there is an X(X&) constructor, we cannot lay it
13565 into the stack (any more), so we make this parameter
13566 look like it is really of reference type. Functions
13567 which pass parameters to this function will know to
13568 create a temporary in their frame, and pass a reference
13569 to that. */
13570
13571 if (TREE_CODE (TREE_TYPE (parm)) == REFERENCE_TYPE
13572 && TYPE_SIZE (TREE_TYPE (TREE_TYPE (parm))))
13573 SET_DECL_REFERENCE_SLOT (parm, convert_from_reference (parm));
13574
13575 pushdecl (parm);
13576 }
5156628f 13577 if (! processing_template_decl
5566b478 13578 && (cleanup = maybe_build_cleanup (parm), cleanup))
8d08fdba
MS
13579 {
13580 expand_decl (parm);
8d08fdba 13581 parms_have_cleanups = 1;
eb66be0e
MS
13582
13583 /* Keep track of the cleanups. */
13584 cleanups = tree_cons (parm, cleanup, cleanups);
8d08fdba
MS
13585 }
13586 }
13587 else
13588 {
13589 /* If we find an enum constant or a type tag,
13590 put it aside for the moment. */
13591 TREE_CHAIN (parm) = NULL_TREE;
13592 nonparms = chainon (nonparms, parm);
13593 }
13594 }
13595
13596 /* Get the decls in their original chain order
13597 and record in the function. This is all and only the
13598 PARM_DECLs that were pushed into scope by the loop above. */
13599 DECL_ARGUMENTS (fndecl) = getdecls ();
13600
13601 storetags (chainon (parmtags, gettags ()));
13602 }
13603 else
13604 DECL_ARGUMENTS (fndecl) = NULL_TREE;
13605
13606 /* Now store the final chain of decls for the arguments
13607 as the decl-chain of the current lexical scope.
13608 Put the enumerators in as well, at the front so that
13609 DECL_ARGUMENTS is not modified. */
13610
13611 storedecls (chainon (nonparms, DECL_ARGUMENTS (fndecl)));
13612
f181d4ae
MM
13613 /* Declare __FUNCTION__ and __PRETTY_FUNCTION__ for this function. */
13614 declare_function_name ();
13615
8d08fdba
MS
13616 /* Initialize the RTL code for the function. */
13617 DECL_SAVED_INSNS (fndecl) = NULL_RTX;
5156628f 13618 if (! processing_template_decl)
5566b478 13619 expand_function_start (fndecl, parms_have_cleanups);
8d08fdba 13620
eb448459
MS
13621 current_function_parms_stored = 1;
13622
13623 /* If this function is `main', emit a call to `__main'
13624 to run global initializers, etc. */
35680744
MM
13625 if (DECL_MAIN_P (fndecl))
13626 expand_main_function ();
eb448459 13627
eb66be0e
MS
13628 /* Now that we have initialized the parms, we can start their
13629 cleanups. We cannot do this before, since expand_decl_cleanup
13630 should not be called before the parm can be used. */
eb448459 13631 if (cleanups
eb66be0e
MS
13632 && ! processing_template_decl)
13633 {
13634 for (cleanups = nreverse (cleanups); cleanups; cleanups = TREE_CHAIN (cleanups))
13635 {
13636 if (! expand_decl_cleanup (TREE_PURPOSE (cleanups), TREE_VALUE (cleanups)))
8251199e 13637 cp_error ("parser lost in parsing declaration of `%D'",
eb66be0e
MS
13638 TREE_PURPOSE (cleanups));
13639 }
13640 }
13641
8d08fdba
MS
13642 /* Create a binding contour which can be used to catch
13643 cleanup-generated temporaries. Also, if the return value needs or
13644 has initialization, deal with that now. */
13645 if (parms_have_cleanups)
13646 {
13647 pushlevel (0);
13648 expand_start_bindings (0);
13649 }
13650
5156628f 13651 if (! processing_template_decl && flag_exceptions)
f30432d7 13652 {
f30432d7
MS
13653 /* Do the starting of the exception specifications, if we have any. */
13654 if (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (current_function_decl)))
13655 expand_start_eh_spec ();
f30432d7 13656 }
eb448459 13657
5c825fc2 13658 last_parm_cleanup_insn = get_last_insn ();
72b7eeff 13659 last_dtor_insn = get_last_insn ();
8d08fdba
MS
13660}
13661
13662/* Bind a name and initialization to the return value of
13663 the current function. */
e92cc029 13664
8d08fdba
MS
13665void
13666store_return_init (return_id, init)
13667 tree return_id, init;
13668{
13669 tree decl = DECL_RESULT (current_function_decl);
13670
e1cd6e56 13671 if (pedantic)
8d08fdba
MS
13672 /* Give this error as many times as there are occurrences,
13673 so that users can use Emacs compilation buffers to find
13674 and fix all such places. */
8251199e 13675 pedwarn ("ANSI C++ does not permit named return values");
8d08fdba
MS
13676
13677 if (return_id != NULL_TREE)
13678 {
13679 if (DECL_NAME (decl) == NULL_TREE)
13680 {
13681 DECL_NAME (decl) = return_id;
13682 DECL_ASSEMBLER_NAME (decl) = return_id;
13683 }
13684 else
8251199e 13685 cp_error ("return identifier `%D' already in place", decl);
8d08fdba
MS
13686 }
13687
13688 /* Can't let this happen for constructors. */
13689 if (DECL_CONSTRUCTOR_P (current_function_decl))
13690 {
8251199e 13691 error ("can't redefine default return value for constructors");
8d08fdba
MS
13692 return;
13693 }
13694
13695 /* If we have a named return value, put that in our scope as well. */
13696 if (DECL_NAME (decl) != NULL_TREE)
13697 {
13698 /* If this named return value comes in a register,
13699 put it in a pseudo-register. */
13700 if (DECL_REGISTER (decl))
13701 {
13702 original_result_rtx = DECL_RTL (decl);
13703 DECL_RTL (decl) = gen_reg_rtx (DECL_MODE (decl));
13704 }
13705
82580166 13706 /* Let `cp_finish_decl' know that this initializer is ok. */
8d08fdba
MS
13707 DECL_INITIAL (decl) = init;
13708 pushdecl (decl);
e76a2646
MS
13709
13710 if (minimal_parse_mode)
13711 add_tree (build_min_nt (RETURN_INIT, return_id,
13712 copy_to_permanent (init)));
13713 else
dae1d6f6 13714 cp_finish_decl (decl, init, NULL_TREE, 0, 0);
8d08fdba
MS
13715 }
13716}
13717
8d08fdba
MS
13718\f
13719/* Finish up a function declaration and compile that function
13720 all the way to assembler language output. The free the storage
13721 for the function definition.
13722
13723 This is called after parsing the body of the function definition.
13724 LINENO is the current line number.
13725
f181d4ae
MM
13726 FLAGS is a bitwise or of the following values:
13727 1 - CALL_POPLEVEL
13728 An extra call to poplevel (and expand_end_bindings) must be
13729 made to take care of the binding contour for the base
13730 initializers. This is only relevant for constructors.
13731 2 - INCLASS_INLINE
13732 We just finished processing the body of an in-class inline
13733 function definition. (This processing will have taken place
13734 after the class definition is complete.)
e92cc029
MS
13735
13736 NESTED is nonzero if we were in the middle of compiling another function
13737 when we started on this one. */
8d08fdba
MS
13738
13739void
f181d4ae 13740finish_function (lineno, flags, nested)
8d08fdba 13741 int lineno;
f181d4ae 13742 int flags;
db5ae43f 13743 int nested;
8d08fdba
MS
13744{
13745 register tree fndecl = current_function_decl;
13746 tree fntype, ctype = NULL_TREE;
f30432d7 13747 rtx last_parm_insn, insns;
8d08fdba
MS
13748 /* Label to use if this function is supposed to return a value. */
13749 tree no_return_label = NULL_TREE;
13750 tree decls = NULL_TREE;
f181d4ae
MM
13751 int call_poplevel = (flags & 1) != 0;
13752 int inclass_inline = (flags & 2) != 0;
13753 int in_template;
8d08fdba
MS
13754
13755 /* When we get some parse errors, we can end up without a
13756 current_function_decl, so cope. */
13757 if (fndecl == NULL_TREE)
13758 return;
13759
e92cc029
MS
13760 if (! nested && function_depth > 1)
13761 nested = 1;
13762
8d08fdba
MS
13763 fntype = TREE_TYPE (fndecl);
13764
13765/* TREE_READONLY (fndecl) = 1;
13766 This caused &foo to be of type ptr-to-const-function
13767 which then got a warning when stored in a ptr-to-function variable. */
13768
13769 /* This happens on strange parse errors. */
13770 if (! current_function_parms_stored)
13771 {
13772 call_poplevel = 0;
13773 store_parm_decls ();
13774 }
13775
5156628f 13776 if (processing_template_decl)
8d08fdba 13777 {
5566b478
MS
13778 if (DECL_CONSTRUCTOR_P (fndecl) && call_poplevel)
13779 {
13780 decls = getdecls ();
13781 expand_end_bindings (decls, decls != NULL_TREE, 0);
13782 poplevel (decls != NULL_TREE, 0, 0);
13783 }
13784 }
13785 else
13786 {
13787 if (write_symbols != NO_DEBUG /*&& TREE_CODE (fntype) != METHOD_TYPE*/)
8d08fdba 13788 {
5566b478
MS
13789 tree ttype = target_type (fntype);
13790 tree parmdecl;
13791
8d08fdba
MS
13792 if (IS_AGGR_TYPE (ttype))
13793 /* Let debugger know it should output info for this type. */
13794 note_debug_info_needed (ttype);
5566b478
MS
13795
13796 for (parmdecl = DECL_ARGUMENTS (fndecl); parmdecl; parmdecl = TREE_CHAIN (parmdecl))
13797 {
13798 ttype = target_type (TREE_TYPE (parmdecl));
13799 if (IS_AGGR_TYPE (ttype))
13800 /* Let debugger know it should output info for this type. */
13801 note_debug_info_needed (ttype);
13802 }
8d08fdba 13803 }
8d08fdba 13804
5566b478
MS
13805 /* Clean house because we will need to reorder insns here. */
13806 do_pending_stack_adjust ();
8d08fdba 13807
5566b478 13808 if (dtor_label)
8d08fdba 13809 {
5566b478
MS
13810 tree binfo = TYPE_BINFO (current_class_type);
13811 tree cond = integer_one_node;
13812 tree exprstmt;
13813 tree in_charge_node = lookup_name (in_charge_identifier, 0);
13814 tree virtual_size;
13815 int ok_to_optimize_dtor = 0;
13816 int empty_dtor = get_last_insn () == last_dtor_insn;
8d08fdba 13817
5566b478
MS
13818 if (current_function_assigns_this)
13819 cond = build (NE_EXPR, boolean_type_node,
4ac14744 13820 current_class_ptr, integer_zero_node);
5566b478
MS
13821 else
13822 {
13823 int n_baseclasses = CLASSTYPE_N_BASECLASSES (current_class_type);
13824
13825 /* If this destructor is empty, then we don't need to check
13826 whether `this' is NULL in some cases. */
13827 if ((flag_this_is_variable & 1) == 0)
13828 ok_to_optimize_dtor = 1;
13829 else if (empty_dtor)
13830 ok_to_optimize_dtor
13831 = (n_baseclasses == 0
13832 || (n_baseclasses == 1
13833 && TYPE_HAS_DESTRUCTOR (TYPE_BINFO_BASETYPE (current_class_type, 0))));
13834 }
8d08fdba 13835
5566b478
MS
13836 /* These initializations might go inline. Protect
13837 the binding level of the parms. */
13838 pushlevel (0);
13839 expand_start_bindings (0);
8d08fdba 13840
5566b478
MS
13841 if (current_function_assigns_this)
13842 {
13843 current_function_assigns_this = 0;
13844 current_function_just_assigned_this = 0;
13845 }
8d08fdba 13846
5566b478
MS
13847 /* Generate the code to call destructor on base class.
13848 If this destructor belongs to a class with virtual
13849 functions, then set the virtual function table
13850 pointer to represent the type of our base class. */
8d08fdba 13851
5566b478 13852 /* This side-effect makes call to `build_delete' generate the
2a2480e1
JM
13853 code we have to have at the end of this destructor.
13854 `build_delete' will set the flag again. */
5566b478 13855 TYPE_HAS_DESTRUCTOR (current_class_type) = 0;
8d08fdba 13856
5566b478
MS
13857 /* These are two cases where we cannot delegate deletion. */
13858 if (TYPE_USES_VIRTUAL_BASECLASSES (current_class_type)
13859 || TYPE_GETS_REG_DELETE (current_class_type))
4ac14744 13860 exprstmt = build_delete (current_class_type, current_class_ref, integer_zero_node,
e92cc029 13861 LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR|LOOKUP_NORMAL, 0);
5566b478 13862 else
4ac14744 13863 exprstmt = build_delete (current_class_type, current_class_ref, in_charge_node,
e92cc029 13864 LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR|LOOKUP_NORMAL, 0);
5566b478
MS
13865
13866 /* If we did not assign to this, then `this' is non-zero at
13867 the end of a destructor. As a special optimization, don't
13868 emit test if this is an empty destructor. If it does nothing,
13869 it does nothing. If it calls a base destructor, the base
13870 destructor will perform the test. */
13871
13872 if (exprstmt != error_mark_node
13873 && (TREE_CODE (exprstmt) != NOP_EXPR
13874 || TREE_OPERAND (exprstmt, 0) != integer_zero_node
13875 || TYPE_USES_VIRTUAL_BASECLASSES (current_class_type)))
8d08fdba 13876 {
5566b478
MS
13877 expand_label (dtor_label);
13878 if (cond != integer_one_node)
13879 expand_start_cond (cond, 0);
13880 if (exprstmt != void_zero_node)
13881 /* Don't call `expand_expr_stmt' if we're not going to do
13882 anything, since -Wall will give a diagnostic. */
13883 expand_expr_stmt (exprstmt);
13884
13885 /* Run destructor on all virtual baseclasses. */
13886 if (TYPE_USES_VIRTUAL_BASECLASSES (current_class_type))
8d08fdba 13887 {
5566b478
MS
13888 tree vbases = nreverse (copy_list (CLASSTYPE_VBASECLASSES (current_class_type)));
13889 expand_start_cond (build (BIT_AND_EXPR, integer_type_node,
13890 in_charge_node, integer_two_node), 0);
13891 while (vbases)
8d08fdba 13892 {
5566b478
MS
13893 if (TYPE_NEEDS_DESTRUCTOR (BINFO_TYPE (vbases)))
13894 {
bd6dd845
MS
13895 tree vb = get_vbase
13896 (BINFO_TYPE (vbases),
8ea2460f 13897 TYPE_BINFO (current_class_type));
e92cc029 13898 expand_expr_stmt
bd6dd845
MS
13899 (build_scoped_method_call
13900 (current_class_ref, vb, dtor_identifier,
e66d884e 13901 build_expr_list (NULL_TREE, integer_zero_node)));
5566b478
MS
13902 }
13903 vbases = TREE_CHAIN (vbases);
8d08fdba 13904 }
5566b478 13905 expand_end_cond ();
8d08fdba 13906 }
5566b478
MS
13907
13908 do_pending_stack_adjust ();
13909 if (cond != integer_one_node)
13910 expand_end_cond ();
8d08fdba
MS
13911 }
13912
5566b478
MS
13913 virtual_size = c_sizeof (current_class_type);
13914
13915 /* At the end, call delete if that's what's requested. */
519ebd1e
JM
13916
13917 /* FDIS sez: At the point of definition of a virtual destructor
13918 (including an implicit definition), non-placement operator
13919 delete shall be looked up in the scope of the destructor's
13920 class and if found shall be accessible and unambiguous.
13921
13922 This is somewhat unclear, but I take it to mean that if the
13923 class only defines placement deletes we don't do anything here.
13924 So we pass LOOKUP_SPECULATIVELY; delete_sanity will complain
13925 for us if they ever try to delete one of these. */
13926
13927 if (TYPE_GETS_REG_DELETE (current_class_type)
13928 || TYPE_USES_VIRTUAL_BASECLASSES (current_class_type))
13929 exprstmt = build_op_delete_call
13930 (DELETE_EXPR, current_class_ptr, virtual_size,
13931 LOOKUP_NORMAL | LOOKUP_SPECULATIVELY, NULL_TREE);
5566b478
MS
13932 else
13933 exprstmt = NULL_TREE;
8d08fdba 13934
5566b478
MS
13935 if (exprstmt)
13936 {
13937 cond = build (BIT_AND_EXPR, integer_type_node,
13938 in_charge_node, integer_one_node);
13939 expand_start_cond (cond, 0);
13940 expand_expr_stmt (exprstmt);
13941 expand_end_cond ();
13942 }
8d08fdba 13943
5566b478 13944 /* End of destructor. */
fc378698 13945 expand_end_bindings (NULL_TREE, getdecls () != NULL_TREE, 0);
536333d4 13946 poplevel (getdecls () != NULL_TREE, 0, 0);
5566b478
MS
13947
13948 /* Back to the top of destructor. */
956d6950 13949 /* Don't execute destructor code if `this' is NULL. */
5566b478
MS
13950
13951 start_sequence ();
13952
13953 /* If the dtor is empty, and we know there is not possible way we
13954 could use any vtable entries, before they are possibly set by
13955 a base class dtor, we don't have to setup the vtables, as we
13956 know that any base class dtoring will set up any vtables it
13957 needs. We avoid MI, because one base class dtor can do a
13958 virtual dispatch to an overridden function that would need to
13959 have a non-related vtable set up, we cannot avoid setting up
13960 vtables in that case. We could change this to see if there is
13961 just one vtable. */
13962 if (! empty_dtor || TYPE_USES_COMPLEX_INHERITANCE (current_class_type))
13963 {
13964 /* Make all virtual function table pointers in non-virtual base
13965 classes point to CURRENT_CLASS_TYPE's virtual function
13966 tables. */
4ac14744 13967 expand_direct_vtbls_init (binfo, binfo, 1, 0, current_class_ptr);
8d08fdba 13968
5566b478 13969 if (TYPE_USES_VIRTUAL_BASECLASSES (current_class_type))
4ac14744 13970 expand_indirect_vtbls_init (binfo, current_class_ref, current_class_ptr);
5566b478
MS
13971 }
13972
13973 if (! ok_to_optimize_dtor)
13974 {
13975 cond = build_binary_op (NE_EXPR,
337c90cc 13976 current_class_ptr, integer_zero_node);
5566b478
MS
13977 expand_start_cond (cond, 0);
13978 }
8d08fdba 13979
5566b478
MS
13980 insns = get_insns ();
13981 end_sequence ();
f30432d7 13982
5566b478
MS
13983 last_parm_insn = get_first_nonparm_insn ();
13984 if (last_parm_insn == NULL_RTX)
13985 last_parm_insn = get_last_insn ();
13986 else
13987 last_parm_insn = previous_insn (last_parm_insn);
8d08fdba 13988
5566b478 13989 emit_insns_after (insns, last_parm_insn);
72b7eeff 13990
5566b478
MS
13991 if (! ok_to_optimize_dtor)
13992 expand_end_cond ();
72b7eeff 13993 }
5566b478 13994 else if (current_function_assigns_this)
8d08fdba 13995 {
5566b478
MS
13996 /* Does not need to call emit_base_init, because
13997 that is done (if needed) just after assignment to this
13998 is seen. */
13999
14000 if (DECL_CONSTRUCTOR_P (current_function_decl))
14001 {
14002 end_protect_partials ();
14003 expand_label (ctor_label);
14004 ctor_label = NULL_TREE;
14005
14006 if (call_poplevel)
14007 {
14008 decls = getdecls ();
14009 expand_end_bindings (decls, decls != NULL_TREE, 0);
14010 poplevel (decls != NULL_TREE, 0, 0);
14011 }
1c2c08a5
JM
14012 /* c_expand_return knows to return 'this' from a constructor. */
14013 c_expand_return (NULL_TREE);
5566b478 14014 }
a1774733 14015 else if (TREE_CODE (TREE_TYPE (DECL_RESULT (current_function_decl))) != VOID_TYPE
5566b478
MS
14016 && return_label != NULL_RTX)
14017 no_return_label = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE);
14018
14019 current_function_assigns_this = 0;
14020 current_function_just_assigned_this = 0;
14021 base_init_expr = NULL_TREE;
8d08fdba 14022 }
5566b478
MS
14023 else if (DECL_CONSTRUCTOR_P (fndecl))
14024 {
a703fb38 14025 tree cond = NULL_TREE, thenclause = NULL_TREE;
5566b478
MS
14026 /* Allow constructor for a type to get a new instance of the object
14027 using `build_new'. */
14028 tree abstract_virtuals = CLASSTYPE_ABSTRACT_VIRTUALS (current_class_type);
14029 CLASSTYPE_ABSTRACT_VIRTUALS (current_class_type) = NULL_TREE;
f30432d7 14030
5566b478 14031 DECL_RETURNS_FIRST_ARG (fndecl) = 1;
f30432d7 14032
5566b478
MS
14033 if (flag_this_is_variable > 0)
14034 {
14035 cond = build_binary_op (EQ_EXPR,
337c90cc 14036 current_class_ptr, integer_zero_node);
4ac14744 14037 thenclause = build_modify_expr (current_class_ptr, NOP_EXPR,
5566b478
MS
14038 build_new (NULL_TREE, current_class_type, void_type_node, 0));
14039 }
f30432d7 14040
5566b478 14041 CLASSTYPE_ABSTRACT_VIRTUALS (current_class_type) = abstract_virtuals;
f30432d7 14042
5566b478
MS
14043 start_sequence ();
14044
14045 if (flag_this_is_variable > 0)
14046 {
14047 expand_start_cond (cond, 0);
14048 expand_expr_stmt (thenclause);
14049 expand_end_cond ();
14050 }
14051
14052 /* Emit insns from `emit_base_init' which sets up virtual
14053 function table pointer(s). */
14054 if (base_init_expr)
14055 {
14056 expand_expr_stmt (base_init_expr);
14057 base_init_expr = NULL_TREE;
14058 }
14059
14060 insns = get_insns ();
14061 end_sequence ();
14062
b87692e5 14063 /* This is where the body of the constructor begins. */
5566b478 14064
b87692e5 14065 emit_insns_after (insns, last_parm_cleanup_insn);
8d08fdba 14066
b7484fbe 14067 end_protect_partials ();
5566b478
MS
14068
14069 /* This is where the body of the constructor ends. */
8d08fdba
MS
14070 expand_label (ctor_label);
14071 ctor_label = NULL_TREE;
14072
14073 if (call_poplevel)
14074 {
14075 decls = getdecls ();
8d08fdba 14076 expand_end_bindings (decls, decls != NULL_TREE, 0);
5566b478 14077 poplevel (decls != NULL_TREE, 1, 0);
8d08fdba 14078 }
8d08fdba 14079
1c2c08a5
JM
14080 /* c_expand_return knows to return 'this' from a constructor. */
14081 c_expand_return (NULL_TREE);
8d08fdba 14082
5566b478
MS
14083 current_function_assigns_this = 0;
14084 current_function_just_assigned_this = 0;
14085 }
35680744 14086 else if (DECL_MAIN_P (fndecl))
8d08fdba 14087 {
5566b478
MS
14088 /* Make it so that `main' always returns 0 by default. */
14089#ifdef VMS
14090 c_expand_return (integer_one_node);
14091#else
14092 c_expand_return (integer_zero_node);
14093#endif
8d08fdba 14094 }
5566b478
MS
14095 else if (return_label != NULL_RTX
14096 && current_function_return_value == NULL_TREE
14097 && ! DECL_NAME (DECL_RESULT (current_function_decl)))
14098 no_return_label = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE);
8d08fdba 14099
eb448459
MS
14100 if (flag_exceptions)
14101 expand_exception_blocks ();
14102
5566b478
MS
14103 /* If this function is supposed to return a value, ensure that
14104 we do not fall into the cleanups by mistake. The end of our
14105 function will look like this:
14106
14107 user code (may have return stmt somewhere)
14108 goto no_return_label
14109 cleanup_label:
14110 cleanups
14111 goto return_label
14112 no_return_label:
14113 NOTE_INSN_FUNCTION_END
14114 return_label:
14115 things for return
14116
14117 If the user omits a return stmt in the USER CODE section, we
14118 will have a control path which reaches NOTE_INSN_FUNCTION_END.
14119 Otherwise, we won't. */
14120 if (no_return_label)
8d08fdba 14121 {
5566b478
MS
14122 DECL_CONTEXT (no_return_label) = fndecl;
14123 DECL_INITIAL (no_return_label) = error_mark_node;
14124 DECL_SOURCE_FILE (no_return_label) = input_filename;
14125 DECL_SOURCE_LINE (no_return_label) = lineno;
14126 expand_goto (no_return_label);
8d08fdba
MS
14127 }
14128
5566b478 14129 if (cleanup_label)
a9aedbc2 14130 {
2c73f9f5 14131 /* Remove the binding contour which is used
5566b478
MS
14132 to catch cleanup-generated temporaries. */
14133 expand_end_bindings (0, 0, 0);
14134 poplevel (0, 0, 0);
8d08fdba 14135
eb448459
MS
14136 /* Emit label at beginning of cleanup code for parameters. */
14137 emit_label (cleanup_label);
14138 }
b7484fbe 14139
5566b478
MS
14140 /* Get return value into register if that's where it's supposed to be. */
14141 if (original_result_rtx)
14142 fixup_result_decl (DECL_RESULT (fndecl), original_result_rtx);
8d08fdba 14143
5566b478
MS
14144 /* Finish building code that will trigger warnings if users forget
14145 to make their functions return values. */
14146 if (no_return_label || cleanup_label)
14147 emit_jump (return_label);
14148 if (no_return_label)
8d08fdba 14149 {
5566b478
MS
14150 /* We don't need to call `expand_*_return' here because we
14151 don't need any cleanups here--this path of code is only
14152 for error checking purposes. */
14153 expand_label (no_return_label);
8d08fdba
MS
14154 }
14155
5566b478
MS
14156 /* Generate rtl for function exit. */
14157 expand_function_end (input_filename, lineno, 1);
8d08fdba 14158 }
f181d4ae 14159
f181d4ae
MM
14160 /* If we're processing a template, squirrel away the definition
14161 until we do an instantiation. */
14162 if (processing_template_decl)
14163 {
14164 --minimal_parse_mode;
14165 DECL_SAVED_TREE (fndecl) = TREE_CHAIN (DECL_SAVED_TREE (fndecl));
14166 /* We have to save this value here in case
14167 maybe_end_member_template_processing decides to pop all the
14168 template parameters. */
14169 in_template = 1;
14170 }
14171 else
14172 in_template = 0;
8d2733ca 14173
8d08fdba
MS
14174 /* This must come after expand_function_end because cleanups might
14175 have declarations (from inline functions) that need to go into
14176 this function's blocks. */
14177 if (current_binding_level->parm_flag != 1)
14178 my_friendly_abort (122);
14179 poplevel (1, 0, 1);
14180
f181d4ae
MM
14181 /* If this is a in-class inline definition, we may have to pop the
14182 bindings for the template parameters that we added in
14183 maybe_begin_member_template_processing when start_function was
14184 called. */
14185 if (inclass_inline)
14186 maybe_end_member_template_processing ();
14187
2c73f9f5 14188 /* Reset scope for C++: if we were in the scope of a class,
db5ae43f
MS
14189 then when we finish this function, we are not longer so.
14190 This cannot be done until we know for sure that no more
14191 class members will ever be referenced in this function
14192 (i.e., calls to destructors). */
14193 if (current_class_name)
14194 {
14195 ctype = current_class_type;
b74a0560 14196 pop_nested_class ();
db5ae43f 14197 }
db5ae43f 14198
1caa11d3
MM
14199 /* Must mark the RESULT_DECL as being in this function. */
14200 DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
14201
14202 /* Set the BLOCK_SUPERCONTEXT of the outermost function scope to point
14203 to the FUNCTION_DECL node itself. */
14204 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
14205
f181d4ae 14206 if (!in_template)
5566b478 14207 {
75650646
MM
14208 int saved_flag_keep_inline_functions =
14209 flag_keep_inline_functions;
14210
5566b478
MS
14211 /* So we can tell if jump_optimize sets it to 1. */
14212 can_reach_end = 0;
8d08fdba 14213
75650646 14214 if (DECL_CONTEXT (fndecl) != NULL_TREE
e1467ff2 14215 && hack_decl_function_context (fndecl))
75650646
MM
14216 /* Trick rest_of_compilation into not deferring output of this
14217 function, even if it is inline, since the rtl_obstack for
14218 this function is the function_obstack of the enclosing
14219 function and will be deallocated when the enclosing
14220 function is gone. See save_tree_status. */
14221 flag_keep_inline_functions = 1;
14222
6633d636
MS
14223 /* Run the optimizers and output the assembler code for this
14224 function. */
14225
14226 if (DECL_ARTIFICIAL (fndecl))
14227 {
14228 /* Do we really *want* to inline this synthesized method? */
14229
14230 int save_fif = flag_inline_functions;
14231 flag_inline_functions = 1;
14232
14233 /* Turn off DECL_INLINE for the moment so function_cannot_inline_p
14234 will check our size. */
14235 DECL_INLINE (fndecl) = 0;
14236
14237 rest_of_compilation (fndecl);
14238 flag_inline_functions = save_fif;
14239 }
14240 else
14241 rest_of_compilation (fndecl);
8d08fdba 14242
75650646
MM
14243 flag_keep_inline_functions = saved_flag_keep_inline_functions;
14244
5566b478
MS
14245 if (DECL_SAVED_INSNS (fndecl) && ! TREE_ASM_WRITTEN (fndecl))
14246 {
14247 /* Set DECL_EXTERNAL so that assemble_external will be called as
14248 necessary. We'll clear it again in finish_file. */
14249 if (! DECL_EXTERNAL (fndecl))
14250 DECL_NOT_REALLY_EXTERN (fndecl) = 1;
14251 DECL_EXTERNAL (fndecl) = 1;
14252 mark_inline_for_output (fndecl);
14253 }
8926095f 14254
d2e5ee5c
MS
14255 if (ctype && TREE_ASM_WRITTEN (fndecl))
14256 note_debug_info_needed (ctype);
14257
5566b478 14258 current_function_returns_null |= can_reach_end;
8d08fdba 14259
5566b478
MS
14260 /* Since we don't normally go through c_expand_return for constructors,
14261 this normally gets the wrong value.
14262 Also, named return values have their return codes emitted after
14263 NOTE_INSN_FUNCTION_END, confusing jump.c. */
14264 if (DECL_CONSTRUCTOR_P (fndecl)
14265 || DECL_NAME (DECL_RESULT (fndecl)) != NULL_TREE)
14266 current_function_returns_null = 0;
8d08fdba 14267
5566b478 14268 if (TREE_THIS_VOLATILE (fndecl) && current_function_returns_null)
8251199e 14269 cp_warning ("`noreturn' function `%D' does return", fndecl);
5566b478
MS
14270 else if ((warn_return_type || pedantic)
14271 && current_function_returns_null
a1774733 14272 && TREE_CODE (TREE_TYPE (fntype)) != VOID_TYPE)
5566b478
MS
14273 {
14274 /* If this function returns non-void and control can drop through,
14275 complain. */
8251199e 14276 cp_warning ("control reaches end of non-void function `%D'", fndecl);
5566b478
MS
14277 }
14278 /* With just -W, complain only if function returns both with
14279 and without a value. */
14280 else if (extra_warnings
14281 && current_function_returns_value && current_function_returns_null)
8251199e 14282 warning ("this function may return with or without a value");
8d08fdba 14283 }
5566b478
MS
14284
14285 --function_depth;
8d08fdba
MS
14286
14287 /* Free all the tree nodes making up this function. */
14288 /* Switch back to allocating nodes permanently
14289 until we start another function. */
db5ae43f
MS
14290 if (! nested)
14291 permanent_allocation (1);
8d08fdba 14292
8d08fdba
MS
14293 if (DECL_SAVED_INSNS (fndecl) == NULL_RTX)
14294 {
d2e5ee5c
MS
14295 tree t;
14296
8d08fdba
MS
14297 /* Stop pointing to the local nodes about to be freed. */
14298 /* But DECL_INITIAL must remain nonzero so we know this
14299 was an actual function definition. */
14300 DECL_INITIAL (fndecl) = error_mark_node;
d2e5ee5c 14301 for (t = DECL_ARGUMENTS (fndecl); t; t = TREE_CHAIN (t))
de22184b 14302 DECL_RTL (t) = DECL_INCOMING_RTL (t) = NULL_RTX;
8d08fdba
MS
14303 }
14304
e1cd6e56
MS
14305 if (DECL_STATIC_CONSTRUCTOR (fndecl))
14306 static_ctors = perm_tree_cons (NULL_TREE, fndecl, static_ctors);
14307 if (DECL_STATIC_DESTRUCTOR (fndecl))
14308 static_dtors = perm_tree_cons (NULL_TREE, fndecl, static_dtors);
14309
28cbf42c
MS
14310 if (! nested)
14311 {
14312 /* Let the error reporting routines know that we're outside a
14313 function. For a nested function, this value is used in
14314 pop_cp_function_context and then reset via pop_function_context. */
14315 current_function_decl = NULL_TREE;
14316 }
14317
e349ee73 14318 named_label_uses = NULL;
4ac14744
MS
14319 current_class_ptr = NULL_TREE;
14320 current_class_ref = NULL_TREE;
8d08fdba
MS
14321}
14322\f
14323/* Create the FUNCTION_DECL for a function definition.
8d08fdba
MS
14324 DECLSPECS and DECLARATOR are the parts of the declaration;
14325 they describe the return type and the name of the function,
14326 but twisted together in a fashion that parallels the syntax of C.
14327
14328 This function creates a binding context for the function body
14329 as well as setting up the FUNCTION_DECL in current_function_decl.
14330
14331 Returns a FUNCTION_DECL on success.
14332
14333 If the DECLARATOR is not suitable for a function (it defines a datum
14334 instead), we return 0, which tells yyparse to report a parse error.
14335
14336 May return void_type_node indicating that this method is actually
14337 a friend. See grokfield for more details.
14338
14339 Came here with a `.pushlevel' .
14340
14341 DO NOT MAKE ANY CHANGES TO THIS CODE WITHOUT MAKING CORRESPONDING
14342 CHANGES TO CODE IN `grokfield'. */
e92cc029 14343
8d08fdba 14344tree
acf82af2
JM
14345start_method (declspecs, declarator, attrlist)
14346 tree declarator, declspecs, attrlist;
8d08fdba 14347{
c11b6f21 14348 tree fndecl = grokdeclarator (declarator, declspecs, MEMFUNCDEF, 0,
acf82af2 14349 attrlist);
8d08fdba
MS
14350
14351 /* Something too ugly to handle. */
14352 if (fndecl == NULL_TREE)
14353 return NULL_TREE;
14354
14355 /* Pass friends other than inline friend functions back. */
a1774733 14356 if (fndecl == void_type_node)
8d08fdba
MS
14357 return fndecl;
14358
14359 if (TREE_CODE (fndecl) != FUNCTION_DECL)
14360 /* Not a function, tell parser to report parse error. */
14361 return NULL_TREE;
14362
14363 if (IS_SIGNATURE (current_class_type))
824b9a4c 14364 IS_DEFAULT_IMPLEMENTATION (fndecl) = 1;
8d08fdba
MS
14365
14366 if (DECL_IN_AGGR_P (fndecl))
14367 {
14368 if (IDENTIFIER_ERROR_LOCUS (DECL_ASSEMBLER_NAME (fndecl)) != current_class_type)
14369 {
2c73f9f5
ML
14370 if (DECL_CONTEXT (fndecl)
14371 && TREE_CODE( DECL_CONTEXT (fndecl)) != NAMESPACE_DECL)
8251199e
JM
14372 cp_error ("`%D' is already defined in class %s", fndecl,
14373 TYPE_NAME_STRING (DECL_CONTEXT (fndecl)));
8d08fdba
MS
14374 }
14375 return void_type_node;
14376 }
14377
f3400fe2
JM
14378 check_template_shadow (fndecl);
14379
faae18ab
MS
14380 DECL_THIS_INLINE (fndecl) = 1;
14381
8926095f 14382 if (flag_default_inline)
8d08fdba
MS
14383 DECL_INLINE (fndecl) = 1;
14384
36a117a5
MM
14385 /* We process method specializations in finish_struct_1. */
14386 if (processing_template_decl && !DECL_TEMPLATE_SPECIALIZATION (fndecl))
3ac3d9ea 14387 fndecl = push_template_decl (fndecl);
a0a33927 14388
8d08fdba
MS
14389 /* We read in the parameters on the maybepermanent_obstack,
14390 but we won't be getting back to them until after we
14391 may have clobbered them. So the call to preserve_data
14392 will keep them safe. */
14393 preserve_data ();
14394
14395 if (! DECL_FRIEND_P (fndecl))
14396 {
8d08fdba
MS
14397 if (TREE_CHAIN (fndecl))
14398 {
14399 fndecl = copy_node (fndecl);
14400 TREE_CHAIN (fndecl) = NULL_TREE;
14401 }
14402
14403 if (DECL_CONSTRUCTOR_P (fndecl))
a0a33927
MS
14404 {
14405 if (! grok_ctor_properties (current_class_type, fndecl))
14406 return void_type_node;
14407 }
8d08fdba
MS
14408 else if (IDENTIFIER_OPNAME_P (DECL_NAME (fndecl)))
14409 grok_op_properties (fndecl, DECL_VIRTUAL_P (fndecl), 0);
14410 }
14411
82580166 14412 cp_finish_decl (fndecl, NULL_TREE, NULL_TREE, 0, 0);
8d08fdba
MS
14413
14414 /* Make a place for the parms */
14415 pushlevel (0);
14416 current_binding_level->parm_flag = 1;
14417
14418 DECL_IN_AGGR_P (fndecl) = 1;
14419 return fndecl;
14420}
14421
14422/* Go through the motions of finishing a function definition.
14423 We don't compile this method until after the whole class has
14424 been processed.
14425
14426 FINISH_METHOD must return something that looks as though it
14427 came from GROKFIELD (since we are defining a method, after all).
14428
14429 This is called after parsing the body of the function definition.
14430 STMTS is the chain of statements that makes up the function body.
14431
14432 DECL is the ..._DECL that `start_method' provided. */
14433
14434tree
14435finish_method (decl)
14436 tree decl;
14437{
14438 register tree fndecl = decl;
14439 tree old_initial;
8d08fdba
MS
14440
14441 register tree link;
14442
a1774733 14443 if (decl == void_type_node)
8d08fdba
MS
14444 return decl;
14445
14446 old_initial = DECL_INITIAL (fndecl);
14447
14448 /* Undo the level for the parms (from start_method).
14449 This is like poplevel, but it causes nothing to be
14450 saved. Saving information here confuses symbol-table
14451 output routines. Besides, this information will
14452 be correctly output when this method is actually
14453 compiled. */
14454
14455 /* Clear out the meanings of the local variables of this level;
14456 also record in each decl which block it belongs to. */
14457
14458 for (link = current_binding_level->names; link; link = TREE_CHAIN (link))
14459 {
14460 if (DECL_NAME (link) != NULL_TREE)
d8f8dca1 14461 pop_binding (DECL_NAME (link), link);
8d08fdba
MS
14462 my_friendly_assert (TREE_CODE (link) != FUNCTION_DECL, 163);
14463 DECL_CONTEXT (link) = NULL_TREE;
14464 }
14465
8d08fdba
MS
14466 GNU_xref_end_scope ((HOST_WIDE_INT) current_binding_level,
14467 (HOST_WIDE_INT) current_binding_level->level_chain,
14468 current_binding_level->parm_flag,
5566b478 14469 current_binding_level->keep);
8d08fdba
MS
14470
14471 poplevel (0, 0, 0);
14472
14473 DECL_INITIAL (fndecl) = old_initial;
14474
14475 /* We used to check if the context of FNDECL was different from
14476 current_class_type as another way to get inside here. This didn't work
14477 for String.cc in libg++. */
14478 if (DECL_FRIEND_P (fndecl))
14479 {
14480 CLASSTYPE_INLINE_FRIENDS (current_class_type)
14481 = tree_cons (NULL_TREE, fndecl, CLASSTYPE_INLINE_FRIENDS (current_class_type));
14482 decl = void_type_node;
14483 }
14484
14485 return decl;
14486}
14487\f
14488/* Called when a new struct TYPE is defined.
14489 If this structure or union completes the type of any previous
14490 variable declaration, lay it out and output its rtl. */
14491
14492void
14493hack_incomplete_structures (type)
14494 tree type;
14495{
f30432d7 14496 tree *list;
8d08fdba 14497
f30432d7 14498 if (current_binding_level->incomplete == NULL_TREE)
8d08fdba
MS
14499 return;
14500
14501 if (!type) /* Don't do this for class templates. */
14502 return;
14503
f30432d7
MS
14504 for (list = &current_binding_level->incomplete; *list; )
14505 {
14506 tree decl = TREE_VALUE (*list);
a703fb38 14507 if ((decl && TREE_TYPE (decl) == type)
f30432d7
MS
14508 || (TREE_TYPE (decl)
14509 && TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
14510 && TREE_TYPE (TREE_TYPE (decl)) == type))
14511 {
14512 int toplevel = toplevel_bindings_p ();
14513 if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
14514 && TREE_TYPE (TREE_TYPE (decl)) == type)
14515 layout_type (TREE_TYPE (decl));
14516 layout_decl (decl, 0);
14517 rest_of_decl_compilation (decl, NULL_PTR, toplevel, 0);
14518 if (! toplevel)
14519 {
14520 tree cleanup;
14521 expand_decl (decl);
14522 cleanup = maybe_build_cleanup (decl);
14523 expand_decl_init (decl);
e349ee73 14524 if (! expand_decl_cleanup (decl, cleanup))
8251199e 14525 cp_error ("parser lost in parsing declaration of `%D'",
f30432d7
MS
14526 decl);
14527 }
14528 *list = TREE_CHAIN (*list);
14529 }
14530 else
14531 list = &TREE_CHAIN (*list);
14532 }
8d08fdba
MS
14533}
14534
8d08fdba 14535/* If DECL is of a type which needs a cleanup, build that cleanup here.
c73964b2 14536 See build_delete for information about AUTO_DELETE.
8d08fdba
MS
14537
14538 Don't build these on the momentary obstack; they must live
14539 the life of the binding contour. */
e92cc029 14540
c73964b2
MS
14541static tree
14542maybe_build_cleanup_1 (decl, auto_delete)
14543 tree decl, auto_delete;
8d08fdba
MS
14544{
14545 tree type = TREE_TYPE (decl);
44689c12 14546 if (type != error_mark_node && TYPE_NEEDS_DESTRUCTOR (type))
8d08fdba
MS
14547 {
14548 int temp = 0, flags = LOOKUP_NORMAL|LOOKUP_DESTRUCTOR;
14549 tree rval;
8d08fdba
MS
14550
14551 if (TREE_CODE (decl) != PARM_DECL)
14552 temp = suspend_momentary ();
14553
14554 if (TREE_CODE (type) == ARRAY_TYPE)
14555 rval = decl;
14556 else
14557 {
14558 mark_addressable (decl);
14559 rval = build_unary_op (ADDR_EXPR, decl, 0);
14560 }
14561
14562 /* Optimize for space over speed here. */
14563 if (! TYPE_USES_VIRTUAL_BASECLASSES (type)
14564 || flag_expensive_optimizations)
14565 flags |= LOOKUP_NONVIRTUAL;
14566
c73964b2 14567 rval = build_delete (TREE_TYPE (rval), rval, auto_delete, flags, 0);
8d08fdba
MS
14568
14569 if (TYPE_USES_VIRTUAL_BASECLASSES (type)
14570 && ! TYPE_HAS_DESTRUCTOR (type))
e66d884e
JM
14571 rval = build_compound_expr (expr_tree_cons (NULL_TREE, rval,
14572 build_expr_list (NULL_TREE, build_vbase_delete (type, decl))));
8d08fdba 14573
8d08fdba
MS
14574 if (TREE_CODE (decl) != PARM_DECL)
14575 resume_momentary (temp);
14576
8d08fdba
MS
14577 return rval;
14578 }
14579 return 0;
14580}
c73964b2
MS
14581
14582/* If DECL is of a type which needs a cleanup, build that cleanup
14583 here. The cleanup does free the storage with a call to delete. */
14584
14585tree
14586maybe_build_cleanup_and_delete (decl)
14587 tree decl;
14588{
14589 return maybe_build_cleanup_1 (decl, integer_three_node);
14590}
14591
14592/* If DECL is of a type which needs a cleanup, build that cleanup
14593 here. The cleanup does not free the storage with a call a delete. */
14594
14595tree
14596maybe_build_cleanup (decl)
14597 tree decl;
14598{
14599 return maybe_build_cleanup_1 (decl, integer_two_node);
14600}
8d08fdba
MS
14601\f
14602/* Expand a C++ expression at the statement level.
14603 This is needed to ferret out nodes which have UNKNOWN_TYPE.
14604 The C++ type checker should get all of these out when
14605 expressions are combined with other, type-providing, expressions,
14606 leaving only orphan expressions, such as:
14607
e92cc029 14608 &class::bar; / / takes its address, but does nothing with it. */
8d08fdba 14609
8d08fdba
MS
14610void
14611cplus_expand_expr_stmt (exp)
14612 tree exp;
14613{
5156628f 14614 if (processing_template_decl)
5566b478
MS
14615 {
14616 add_tree (build_min_nt (EXPR_STMT, exp));
14617 return;
14618 }
14619
eb66be0e
MS
14620 /* Arrange for all temps to disappear. */
14621 expand_start_target_temps ();
14622
66543169
NS
14623 exp = require_complete_type_in_void (exp);
14624
14625 if (TREE_CODE (exp) == FUNCTION_DECL)
8d08fdba 14626 {
66543169
NS
14627 cp_warning ("reference, not call, to function `%D'", exp);
14628 warning ("at this point in file");
8d08fdba 14629 }
8d08fdba 14630
63c68bb7 14631#if 0
66543169
NS
14632 /* We should do this eventually, but right now this causes regex.o from
14633 libg++ to miscompile, and tString to core dump. */
14634 exp = build1 (CLEANUP_POINT_EXPR, TREE_TYPE (exp), exp);
63c68bb7 14635#endif
b3e401c2 14636
66543169
NS
14637 /* Strip unused implicit INDIRECT_REFs of references. */
14638 if (TREE_CODE (exp) == INDIRECT_REF
14639 && TREE_CODE (TREE_TYPE (TREE_OPERAND (exp, 0))) == REFERENCE_TYPE)
14640 exp = TREE_OPERAND (exp, 0);
b3e401c2 14641
66543169
NS
14642 /* If we don't do this, we end up down inside expand_expr
14643 trying to do TYPE_MODE on the ERROR_MARK, and really
14644 go outside the bounds of the type. */
14645 if (exp != error_mark_node)
14646 expand_expr_stmt (break_out_cleanups (exp));
8d08fdba
MS
14647
14648 /* Clean up any pending cleanups. This happens when a function call
14649 returns a cleanup-needing value that nobody uses. */
eb66be0e 14650 expand_end_target_temps ();
8d08fdba
MS
14651}
14652
14653/* When a stmt has been parsed, this function is called.
14654
14655 Currently, this function only does something within a
14656 constructor's scope: if a stmt has just assigned to this,
14657 and we are in a derived class, we call `emit_base_init'. */
14658
14659void
14660finish_stmt ()
14661{
14662 extern struct nesting *cond_stack, *loop_stack, *case_stack;
14663
14664
14665 if (current_function_assigns_this
14666 || ! current_function_just_assigned_this)
14667 return;
14668 if (DECL_CONSTRUCTOR_P (current_function_decl))
14669 {
14670 /* Constructors must wait until we are out of control
14671 zones before calling base constructors. */
14672 if (cond_stack || loop_stack || case_stack)
14673 return;
a9aedbc2 14674 expand_expr_stmt (base_init_expr);
8d08fdba
MS
14675 check_base_init (current_class_type);
14676 }
14677 current_function_assigns_this = 1;
8d08fdba
MS
14678}
14679
8d08fdba 14680/* Change a static member function definition into a FUNCTION_TYPE, instead
700f8a87
MS
14681 of the METHOD_TYPE that we create when it's originally parsed.
14682
14683 WARNING: DO NOT pass &TREE_TYPE (decl) to FN or &TYPE_ARG_TYPES
14684 (TREE_TYPE (decl)) to ARGTYPES, as doing so will corrupt the types of
14685 other decls. Either pass the addresses of local variables or NULL. */
14686
8857f91e 14687void
700f8a87
MS
14688revert_static_member_fn (decl, fn, argtypes)
14689 tree *decl, *fn, *argtypes;
8d08fdba 14690{
700f8a87
MS
14691 tree tmp;
14692 tree function = fn ? *fn : TREE_TYPE (*decl);
14693 tree args = argtypes ? *argtypes : TYPE_ARG_TYPES (function);
8d08fdba 14694
91063b51
MM
14695 if (CP_TYPE_QUALS (TREE_TYPE (TREE_VALUE (args)))
14696 != TYPE_UNQUALIFIED)
14697 cp_error ("static member function `%#D' declared with type qualifiers",
14698 *decl);
f30432d7 14699
700f8a87
MS
14700 args = TREE_CHAIN (args);
14701 tmp = build_function_type (TREE_TYPE (function), args);
91063b51 14702 tmp = build_qualified_type (tmp, CP_TYPE_QUALS (function));
f30432d7 14703 tmp = build_exception_variant (tmp,
8d08fdba
MS
14704 TYPE_RAISES_EXCEPTIONS (function));
14705 TREE_TYPE (*decl) = tmp;
f30432d7
MS
14706 if (DECL_ARGUMENTS (*decl))
14707 DECL_ARGUMENTS (*decl) = TREE_CHAIN (DECL_ARGUMENTS (*decl));
8d08fdba 14708 DECL_STATIC_FUNCTION_P (*decl) = 1;
700f8a87
MS
14709 if (fn)
14710 *fn = tmp;
14711 if (argtypes)
14712 *argtypes = args;
8d08fdba 14713}
a4443a08 14714
db5ae43f
MS
14715struct cp_function
14716{
14717 int returns_value;
14718 int returns_null;
db5ae43f
MS
14719 int assigns_this;
14720 int just_assigned_this;
14721 int parms_stored;
7215f9a0 14722 int temp_name_counter;
db5ae43f 14723 tree named_labels;
5524676d 14724 struct named_label_list *named_label_uses;
db5ae43f
MS
14725 tree shadowed_labels;
14726 tree ctor_label;
14727 tree dtor_label;
72b7eeff 14728 rtx last_dtor_insn;
b87692e5 14729 rtx last_parm_cleanup_insn;
79ff2c6c
MS
14730 tree base_init_list;
14731 tree member_init_list;
a9aedbc2 14732 tree base_init_expr;
4ac14744
MS
14733 tree current_class_ptr;
14734 tree current_class_ref;
db5ae43f
MS
14735 rtx result_rtx;
14736 struct cp_function *next;
14737 struct binding_level *binding_level;
42976354 14738 int static_labelno;
db5ae43f
MS
14739};
14740
bd6dd845 14741static struct cp_function *cp_function_chain;
db5ae43f 14742
7215f9a0
MS
14743extern int temp_name_counter;
14744
db5ae43f
MS
14745/* Save and reinitialize the variables
14746 used during compilation of a C++ function. */
14747
14748void
28cbf42c
MS
14749push_cp_function_context (context)
14750 tree context;
db5ae43f
MS
14751{
14752 struct cp_function *p
14753 = (struct cp_function *) xmalloc (sizeof (struct cp_function));
14754
28cbf42c 14755 push_function_context_to (context);
db5ae43f
MS
14756
14757 p->next = cp_function_chain;
14758 cp_function_chain = p;
14759
14760 p->named_labels = named_labels;
5524676d 14761 p->named_label_uses = named_label_uses;
db5ae43f
MS
14762 p->shadowed_labels = shadowed_labels;
14763 p->returns_value = current_function_returns_value;
14764 p->returns_null = current_function_returns_null;
db5ae43f
MS
14765 p->binding_level = current_binding_level;
14766 p->ctor_label = ctor_label;
14767 p->dtor_label = dtor_label;
72b7eeff 14768 p->last_dtor_insn = last_dtor_insn;
b87692e5 14769 p->last_parm_cleanup_insn = last_parm_cleanup_insn;
db5ae43f
MS
14770 p->assigns_this = current_function_assigns_this;
14771 p->just_assigned_this = current_function_just_assigned_this;
14772 p->parms_stored = current_function_parms_stored;
14773 p->result_rtx = original_result_rtx;
a9aedbc2 14774 p->base_init_expr = base_init_expr;
7215f9a0 14775 p->temp_name_counter = temp_name_counter;
79ff2c6c
MS
14776 p->base_init_list = current_base_init_list;
14777 p->member_init_list = current_member_init_list;
4ac14744
MS
14778 p->current_class_ptr = current_class_ptr;
14779 p->current_class_ref = current_class_ref;
42976354 14780 p->static_labelno = static_labelno;
db5ae43f
MS
14781}
14782
14783/* Restore the variables used during compilation of a C++ function. */
14784
14785void
28cbf42c
MS
14786pop_cp_function_context (context)
14787 tree context;
db5ae43f
MS
14788{
14789 struct cp_function *p = cp_function_chain;
14790 tree link;
14791
14792 /* Bring back all the labels that were shadowed. */
14793 for (link = shadowed_labels; link; link = TREE_CHAIN (link))
14794 if (DECL_NAME (TREE_VALUE (link)) != 0)
14795 SET_IDENTIFIER_LABEL_VALUE (DECL_NAME (TREE_VALUE (link)),
14796 TREE_VALUE (link));
14797
28cbf42c 14798 pop_function_context_from (context);
db5ae43f
MS
14799
14800 cp_function_chain = p->next;
14801
14802 named_labels = p->named_labels;
5524676d 14803 named_label_uses = p->named_label_uses;
db5ae43f
MS
14804 shadowed_labels = p->shadowed_labels;
14805 current_function_returns_value = p->returns_value;
14806 current_function_returns_null = p->returns_null;
db5ae43f
MS
14807 current_binding_level = p->binding_level;
14808 ctor_label = p->ctor_label;
14809 dtor_label = p->dtor_label;
72b7eeff 14810 last_dtor_insn = p->last_dtor_insn;
b87692e5 14811 last_parm_cleanup_insn = p->last_parm_cleanup_insn;
db5ae43f
MS
14812 current_function_assigns_this = p->assigns_this;
14813 current_function_just_assigned_this = p->just_assigned_this;
14814 current_function_parms_stored = p->parms_stored;
14815 original_result_rtx = p->result_rtx;
a9aedbc2 14816 base_init_expr = p->base_init_expr;
7215f9a0 14817 temp_name_counter = p->temp_name_counter;
79ff2c6c
MS
14818 current_base_init_list = p->base_init_list;
14819 current_member_init_list = p->member_init_list;
4ac14744
MS
14820 current_class_ptr = p->current_class_ptr;
14821 current_class_ref = p->current_class_ref;
42976354 14822 static_labelno = p->static_labelno;
db5ae43f
MS
14823
14824 free (p);
14825}
ebfc180f 14826
5566b478
MS
14827int
14828in_function_p ()
14829{
14830 return function_depth != 0;
14831}
This page took 3.256831 seconds and 5 git commands to generate.