]> gcc.gnu.org Git - gcc.git/blame - gcc/cp/decl.c
jump.c: Include insn-attr.h.
[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
JM
63extern void (*print_error_function) PROTO((char *));
64
8d08fdba
MS
65/* Stack of places to restore the search obstack back to. */
66
67/* Obstack used for remembering local class declarations (like
68 enums and static (const) members. */
69#include "stack.h"
5566b478 70struct obstack decl_obstack;
8d08fdba
MS
71static struct stack_level *decl_stack;
72
73#ifndef CHAR_TYPE_SIZE
74#define CHAR_TYPE_SIZE BITS_PER_UNIT
75#endif
76
77#ifndef SHORT_TYPE_SIZE
78#define SHORT_TYPE_SIZE (BITS_PER_UNIT * MIN ((UNITS_PER_WORD + 1) / 2, 2))
79#endif
80
81#ifndef INT_TYPE_SIZE
82#define INT_TYPE_SIZE BITS_PER_WORD
83#endif
84
85#ifndef LONG_TYPE_SIZE
86#define LONG_TYPE_SIZE BITS_PER_WORD
87#endif
88
89#ifndef LONG_LONG_TYPE_SIZE
90#define LONG_LONG_TYPE_SIZE (BITS_PER_WORD * 2)
91#endif
92
93#ifndef WCHAR_UNSIGNED
94#define WCHAR_UNSIGNED 0
95#endif
96
97#ifndef FLOAT_TYPE_SIZE
98#define FLOAT_TYPE_SIZE BITS_PER_WORD
99#endif
100
101#ifndef DOUBLE_TYPE_SIZE
102#define DOUBLE_TYPE_SIZE (BITS_PER_WORD * 2)
103#endif
104
105#ifndef LONG_DOUBLE_TYPE_SIZE
106#define LONG_DOUBLE_TYPE_SIZE (BITS_PER_WORD * 2)
107#endif
108
255512c1 109#ifndef BOOL_TYPE_SIZE
e1cd6e56 110#ifdef SLOW_BYTE_ACCESS
d2e5ee5c 111#define BOOL_TYPE_SIZE ((SLOW_BYTE_ACCESS) ? (POINTER_SIZE) : (CHAR_TYPE_SIZE))
e1cd6e56 112#else
d2e5ee5c 113#define BOOL_TYPE_SIZE CHAR_TYPE_SIZE
e1cd6e56 114#endif
255512c1
JM
115#endif
116
8d08fdba
MS
117/* We let tm.h override the types used here, to handle trivial differences
118 such as the choice of unsigned int or long unsigned int for size_t.
119 When machines start needing nontrivial differences in the size type,
120 it would be best to do something here to figure out automatically
121 from other information what type to use. */
122
123#ifndef SIZE_TYPE
124#define SIZE_TYPE "long unsigned int"
125#endif
126
127#ifndef PTRDIFF_TYPE
128#define PTRDIFF_TYPE "long int"
129#endif
130
131#ifndef WCHAR_TYPE
132#define WCHAR_TYPE "int"
133#endif
134
8d08fdba
MS
135static tree grokparms PROTO((tree, int));
136static tree lookup_nested_type PROTO((tree, tree));
8251199e 137static char *redeclaration_error_message PROTO((tree, tree));
8d08fdba 138
49c249e1
JM
139static struct stack_level *push_decl_level PROTO((struct stack_level *,
140 struct obstack *));
141static void push_binding_level PROTO((struct binding_level *, int,
142 int));
143static void pop_binding_level PROTO((void));
144static void suspend_binding_level PROTO((void));
145static void resume_binding_level PROTO((struct binding_level *));
146static struct binding_level *make_binding_level PROTO((void));
49c249e1 147static void declare_namespace_level PROTO((void));
1ddb2906 148static void signal_catch PROTO((int)) ATTRIBUTE_NORETURN;
49c249e1
JM
149static void storedecls PROTO((tree));
150static void storetags PROTO((tree));
151static void require_complete_types_for_parms PROTO((tree));
152static void push_overloaded_decl_1 PROTO((tree));
153static int ambi_op_p PROTO((tree));
154static int unary_op_p PROTO((tree));
155static tree store_bindings PROTO((tree, tree));
156static tree lookup_tag_reverse PROTO((tree, tree));
157static tree obscure_complex_init PROTO((tree, tree));
158static tree maybe_build_cleanup_1 PROTO((tree, tree));
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 *));
170static void record_builtin_type PROTO((enum rid, char *, tree));
036407f7 171static void record_unknown_type PROTO((tree, char *));
49c249e1
JM
172static int member_function_or_else PROTO((tree, tree, char *));
173static void bad_specifiers PROTO((tree, char *, int, int, int, int,
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*));
d8f8dca1
MM
181static void add_binding PROTO((tree, tree));
182static void pop_binding PROTO((tree, tree));
297e73d8 183static tree local_variable_p PROTO((tree));
8d08fdba 184
69ac77ce
JL
185#if defined (DEBUG_CP_BINDING_LEVELS)
186static void indent PROTO((void));
187#endif
188
2c73f9f5 189/* A node which has tree code ERROR_MARK, and whose type is itself.
8d08fdba
MS
190 All erroneous expressions are replaced with this node. All functions
191 that accept nodes as arguments should avoid generating error messages
192 if this node is one of the arguments, since it is undesirable to get
193 multiple error messages from one error in the input. */
194
195tree error_mark_node;
196
197/* Erroneous argument lists can use this *IFF* they do not modify it. */
198tree error_mark_list;
199
200/* INTEGER_TYPE and REAL_TYPE nodes for the standard data types */
201
202tree short_integer_type_node;
203tree integer_type_node;
204tree long_integer_type_node;
205tree long_long_integer_type_node;
206
207tree short_unsigned_type_node;
208tree unsigned_type_node;
209tree long_unsigned_type_node;
210tree long_long_unsigned_type_node;
211
212tree ptrdiff_type_node;
213
214tree unsigned_char_type_node;
215tree signed_char_type_node;
216tree char_type_node;
217tree wchar_type_node;
218tree signed_wchar_type_node;
219tree unsigned_wchar_type_node;
220
f376e137
MS
221tree wchar_decl_node;
222
8d08fdba
MS
223tree float_type_node;
224tree double_type_node;
225tree long_double_type_node;
226
37c46b43
MS
227tree complex_integer_type_node;
228tree complex_float_type_node;
229tree complex_double_type_node;
230tree complex_long_double_type_node;
231
8d08fdba
MS
232tree intQI_type_node;
233tree intHI_type_node;
234tree intSI_type_node;
235tree intDI_type_node;
946dc1c8 236#if HOST_BITS_PER_WIDE_INT >= 64
f7554e8c 237tree intTI_type_node;
946dc1c8 238#endif
8d08fdba
MS
239
240tree unsigned_intQI_type_node;
241tree unsigned_intHI_type_node;
242tree unsigned_intSI_type_node;
243tree unsigned_intDI_type_node;
946dc1c8 244#if HOST_BITS_PER_WIDE_INT >= 64
f7554e8c 245tree unsigned_intTI_type_node;
946dc1c8 246#endif
8d08fdba 247
eff71ab0
PB
248tree java_byte_type_node;
249tree java_short_type_node;
250tree java_int_type_node;
251tree java_long_type_node;
252tree java_float_type_node;
253tree java_double_type_node;
254tree java_char_type_node;
255tree java_boolean_type_node;
256
2c73f9f5 257/* A VOID_TYPE node, and the same, packaged in a TREE_LIST. */
8d08fdba
MS
258
259tree void_type_node, void_list_node;
260tree void_zero_node;
261
262/* Nodes for types `void *' and `const void *'. */
263
bd6dd845 264tree ptr_type_node;
6c20b7e9 265tree const_ptr_type_node;
8d08fdba
MS
266
267/* Nodes for types `char *' and `const char *'. */
268
269tree string_type_node, const_string_type_node;
270
271/* Type `char[256]' or something like it.
272 Used when an array of char is needed and the size is irrelevant. */
273
274tree char_array_type_node;
275
276/* Type `int[256]' or something like it.
277 Used when an array of int needed and the size is irrelevant. */
278
279tree int_array_type_node;
280
281/* Type `wchar_t[256]' or something like it.
282 Used when a wide string literal is created. */
283
284tree wchar_array_type_node;
285
2986ae00 286/* The bool data type, and constants */
255512c1 287tree boolean_type_node, boolean_true_node, boolean_false_node;
2986ae00 288
2c73f9f5 289/* Type `int ()' -- used for implicit declaration of functions. */
8d08fdba
MS
290
291tree default_function_type;
292
2c73f9f5 293/* Function types `double (double)' and `double (double, double)', etc. */
8d08fdba 294
bd6dd845
MS
295static tree double_ftype_double, double_ftype_double_double;
296static tree int_ftype_int, long_ftype_long;
297static tree float_ftype_float;
298static tree ldouble_ftype_ldouble;
8d08fdba 299
8d08fdba 300/* Function type `int (const void *, const void *, size_t)' */
5566b478 301static tree int_ftype_cptr_cptr_sizet;
8d08fdba
MS
302
303/* C++ extensions */
700f8a87 304tree vtable_entry_type;
8d08fdba 305tree delta_type_node;
db5ae43f 306#if 0
e92cc029 307/* Old rtti stuff. */
db5ae43f
MS
308tree __baselist_desc_type_node;
309tree __i_desc_type_node, __m_desc_type_node;
8d08fdba 310tree __t_desc_array_type, __i_desc_array_type, __m_desc_array_type;
db5ae43f 311#endif
5566b478
MS
312tree __t_desc_type_node;
313#if 0
314tree __tp_desc_type_node;
315#endif
db5ae43f
MS
316tree __access_mode_type_node;
317tree __bltn_desc_type_node, __user_desc_type_node, __class_desc_type_node;
318tree __ptr_desc_type_node, __attr_desc_type_node, __func_desc_type_node;
319tree __ptmf_desc_type_node, __ptmd_desc_type_node;
320#if 0
321/* Not needed yet? May be needed one day? */
322tree __bltn_desc_array_type, __user_desc_array_type, __class_desc_array_type;
323tree __ptr_desc_array_type, __attr_dec_array_type, __func_desc_array_type;
324tree __ptmf_desc_array_type, __ptmd_desc_array_type;
325#endif
326
2c73f9f5
ML
327/* Indicates that there is a type value in some namespace, although
328 that is not necessarily in scope at the moment. */
329
330static tree global_type_node;
331
8d08fdba
MS
332tree class_star_type_node;
333tree class_type_node, record_type_node, union_type_node, enum_type_node;
db5ae43f 334tree unknown_type_node;
8d08fdba
MS
335tree opaque_type_node, signature_type_node;
336tree sigtable_entry_type;
8d08fdba 337
8d08fdba
MS
338/* Array type `vtable_entry_type[]' */
339tree vtbl_type_node;
849da744 340tree vtbl_ptr_type_node;
8d08fdba 341
6633d636
MS
342/* namespace std */
343tree std_node;
2c73f9f5 344int in_std = 0;
6633d636 345
3e3f722c
ML
346/* Expect only namespace names now. */
347static int only_namespace_names;
348
8d08fdba
MS
349/* In a destructor, the point at which all derived class destroying
350 has been done, just before any base class destroying will be done. */
351
352tree dtor_label;
353
72b7eeff
MS
354/* In a destructor, the last insn emitted after the start of the
355 function and the parms. */
356
bd6dd845 357static rtx last_dtor_insn;
72b7eeff 358
b87692e5 359/* In a constructor, the last insn emitted after the start of the
9664b89e
JM
360 function and the parms, the exception specification and any
361 function-try-block. The constructor initializers are emitted after
362 this insn. */
b87692e5
MS
363
364static rtx last_parm_cleanup_insn;
365
8d08fdba
MS
366/* In a constructor, the point at which we are ready to return
367 the pointer to the initialized object. */
368
369tree ctor_label;
370
8d08fdba
MS
371/* A FUNCTION_DECL which can call `abort'. Not necessarily the
372 one that the user will declare, but sufficient to be called
373 by routines that want to abort the program. */
374
375tree abort_fndecl;
376
94e098d1
MM
377/* A FUNCTION_DECL for the default `::operator delete'. */
378
379tree global_delete_fndecl;
380
8d08fdba
MS
381extern rtx cleanup_label, return_label;
382
383/* If original DECL_RESULT of current function was a register,
384 but due to being an addressable named return value, would up
385 on the stack, this variable holds the named return value's
386 original location. */
bd6dd845 387static rtx original_result_rtx;
8d08fdba
MS
388
389/* Sequence of insns which represents base initialization. */
a9aedbc2 390tree base_init_expr;
8d08fdba
MS
391
392/* C++: Keep these around to reduce calls to `get_identifier'.
393 Identifiers for `this' in member functions and the auto-delete
394 parameter for destructors. */
395tree this_identifier, in_charge_identifier;
fc378698 396tree ctor_identifier, dtor_identifier;
e92cc029 397/* Used in pointer to member functions, in vtables, and in sigtables. */
8d08fdba 398tree pfn_identifier, index_identifier, delta_identifier, delta2_identifier;
9dd70aa4 399tree pfn_or_delta2_identifier, tag_identifier;
5566b478 400tree vt_off_identifier;
8d08fdba 401
e349ee73
MS
402struct named_label_list
403{
404 struct binding_level *binding_level;
405 tree names_in_scope;
406 tree label_decl;
407 char *filename_o_goto;
408 int lineno_o_goto;
409 struct named_label_list *next;
410};
411
8d08fdba
MS
412/* A list (chain of TREE_LIST nodes) of named label uses.
413 The TREE_PURPOSE field is the list of variables defined
38e01259 414 in the label's scope defined at the point of use.
8d08fdba
MS
415 The TREE_VALUE field is the LABEL_DECL used.
416 The TREE_TYPE field holds `current_binding_level' at the
417 point of the label's use.
418
e349ee73
MS
419 BWAHAHAAHAHahhahahahaah. No, no, no, said the little chicken.
420
421 Look at the pretty struct named_label_list. See the pretty struct
422 with the pretty named fields that describe what they do. See the
423 pretty lack of gratuitous casts. Notice the code got a lot cleaner.
424
8d08fdba
MS
425 Used only for jumps to as-yet undefined labels, since
426 jumps to defined labels can have their validity checked
427 by stmt.c. */
428
e349ee73 429static struct named_label_list *named_label_uses = NULL;
8d08fdba
MS
430
431/* A list of objects which have constructors or destructors
432 which reside in the global scope. The decl is stored in
433 the TREE_VALUE slot and the initializer is stored
434 in the TREE_PURPOSE slot. */
435tree static_aggregates;
436
8d08fdba
MS
437/* -- end of C++ */
438
439/* Two expressions that are constants with value zero.
440 The first is of type `int', the second of type `void *'. */
441
442tree integer_zero_node;
443tree null_pointer_node;
444
03d0f4af
MM
445/* The value for __null (NULL), namely, a zero of an integer type with
446 the same number of bits as a pointer. */
d11ad92e
MS
447tree null_node;
448
8d08fdba
MS
449/* A node for the integer constants 1, 2, and 3. */
450
451tree integer_one_node, integer_two_node, integer_three_node;
452
8d08fdba
MS
453/* While defining an enum type, this is 1 plus the last enumerator
454 constant value. */
455
456static tree enum_next_value;
457
7177d104
MS
458/* Nonzero means that there was overflow computing enum_next_value. */
459
460static int enum_overflow;
461
8d08fdba
MS
462/* Parsing a function declarator leaves a list of parameter names
463 or a chain or parameter decls here. */
464
465tree last_function_parms;
466
467/* Parsing a function declarator leaves here a chain of structure
468 and enum types declared in the parmlist. */
469
470static tree last_function_parm_tags;
471
472/* After parsing the declarator that starts a function definition,
473 `start_function' puts here the list of parameter names or chain of decls.
474 `store_parm_decls' finds it here. */
475
476static tree current_function_parms;
477
478/* Similar, for last_function_parm_tags. */
479static tree current_function_parm_tags;
480
481/* A list (chain of TREE_LIST nodes) of all LABEL_DECLs in the function
482 that have names. Here so we can clear out their names' definitions
483 at the end of the function. */
484
485static tree named_labels;
486
487/* A list of LABEL_DECLs from outer contexts that are currently shadowed. */
488
489static tree shadowed_labels;
490
8d08fdba
MS
491/* The FUNCTION_DECL for the function currently being compiled,
492 or 0 if between functions. */
493tree current_function_decl;
494
495/* Set to 0 at beginning of a function definition, set to 1 if
496 a return statement that specifies a return value is seen. */
497
498int current_function_returns_value;
499
500/* Set to 0 at beginning of a function definition, set to 1 if
501 a return statement with no argument is seen. */
502
503int current_function_returns_null;
504
505/* Set to 0 at beginning of a function definition, and whenever
506 a label (case or named) is defined. Set to value of expression
507 returned from function when that value can be transformed into
508 a named return value. */
509
510tree current_function_return_value;
511
8d08fdba
MS
512/* Nonzero means give `double' the same size as `float'. */
513
514extern int flag_short_double;
515
516/* Nonzero means don't recognize any builtin functions. */
517
518extern int flag_no_builtin;
519
00595019
MS
520/* Nonzero means don't recognize the non-ANSI builtin functions.
521 -ansi sets this. */
522
523extern int flag_no_nonansi_builtin;
524
e1cd6e56
MS
525/* Nonzero means enable obscure ANSI features and disable GNU extensions
526 that might cause ANSI-compliant code to be miscompiled. */
8d08fdba
MS
527
528extern int flag_ansi;
529
530/* Nonzero if we want to support huge (> 2^(sizeof(short)*8-1) bytes)
e92cc029 531 objects. */
8d08fdba
MS
532extern int flag_huge_objects;
533
534/* Nonzero if we want to conserve space in the .o files. We do this
535 by putting uninitialized data and runtime initialized data into
ddd5a7c1 536 .common instead of .data at the expense of not flagging multiple
8d08fdba
MS
537 definitions. */
538extern int flag_conserve_space;
539
540/* Pointers to the base and current top of the language name stack. */
541
542extern tree *current_lang_base, *current_lang_stack;
543\f
51c184be 544/* C and C++ flags are in decl2.c. */
8d08fdba
MS
545
546/* Set to 0 at beginning of a constructor, set to 1
547 if that function does an allocation before referencing its
548 instance variable. */
5566b478 549static int current_function_assigns_this;
8d08fdba
MS
550int current_function_just_assigned_this;
551
552/* Set to 0 at beginning of a function. Set non-zero when
553 store_parm_decls is called. Don't call store_parm_decls
554 if this flag is non-zero! */
555int current_function_parms_stored;
556
51c184be 557/* Flag used when debugging spew.c */
8d08fdba
MS
558
559extern int spew_debug;
560
561/* This is a copy of the class_shadowed list of the previous class binding
562 contour when at global scope. It's used to reset IDENTIFIER_CLASS_VALUEs
563 when entering another class scope (i.e. a cache miss). */
564extern tree previous_class_values;
565
e92cc029
MS
566/* A expression of value 0 with the same precision as a sizetype
567 node, but signed. */
568tree signed_size_zero_node;
569
0c8feefe
MM
570/* The name of the anonymous namespace, throughout this translation
571 unit. */
572tree anonymous_namespace_name;
573
8d08fdba
MS
574\f
575/* Allocate a level of searching. */
e92cc029 576
824b9a4c 577static
8d08fdba
MS
578struct stack_level *
579push_decl_level (stack, obstack)
580 struct stack_level *stack;
581 struct obstack *obstack;
582{
583 struct stack_level tem;
584 tem.prev = stack;
585
586 return push_stack_level (obstack, (char *)&tem, sizeof (tem));
587}
588\f
589/* For each binding contour we allocate a binding_level structure
e92cc029
MS
590 which records the names defined in that contour.
591 Contours include:
592 0) the global one
593 1) one for each function definition,
594 where internal declarations of the parameters appear.
595 2) one for each compound statement,
596 to record its declarations.
597
598 The current meaning of a name can be found by searching the levels
599 from the current one out to the global one.
600
601 Off to the side, may be the class_binding_level. This exists only
602 to catch class-local declarations. It is otherwise nonexistent.
603
604 Also there may be binding levels that catch cleanups that must be
d8f8dca1
MM
605 run when exceptions occur. Thus, to see whether a name is bound in
606 the current scope, it is not enough to look in the
607 CURRENT_BINDING_LEVEL. You should use lookup_name_current_level
608 instead. */
8d08fdba
MS
609
610/* Note that the information in the `names' component of the global contour
611 is duplicated in the IDENTIFIER_GLOBAL_VALUEs of all identifiers. */
612
613struct binding_level
614 {
615 /* A chain of _DECL nodes for all variables, constants, functions,
e92cc029 616 and typedef types. These are in the reverse of the order
f181d4ae
MM
617 supplied. There may be OVERLOADs on this list, too, but they
618 are wrapped in TREE_LISTs; the TREE_VALUE is the OVERLOAD. */
8d08fdba
MS
619 tree names;
620
e92cc029
MS
621 /* A list of structure, union and enum definitions, for looking up
622 tag names.
623 It is a chain of TREE_LIST nodes, each of whose TREE_PURPOSE is a name,
624 or NULL_TREE; and whose TREE_VALUE is a RECORD_TYPE, UNION_TYPE,
625 or ENUMERAL_TYPE node.
626
627 C++: the TREE_VALUE nodes can be simple types for
628 component_bindings. */
8d08fdba
MS
629 tree tags;
630
2c73f9f5
ML
631 /* A list of USING_DECL nodes. */
632 tree usings;
633
ea9635c7
ML
634 /* A list of used namespaces. PURPOSE is the namespace,
635 VALUE the common ancestor with this binding_level's namespace. */
636 tree using_directives;
637
f181d4ae
MM
638 /* If this binding level is the binding level for a class, then
639 class_shadowed is a TREE_LIST. The TREE_PURPOSE of each node
640 is the name of an entity bound in the class; the TREE_VALUE is
641 the IDENTIFIER_CLASS_VALUE before we entered the class. Thus,
642 when leaving class scope, we can restore the
d8f8dca1
MM
643 IDENTIFIER_CLASS_VALUE by walking this list. The TREE_TYPE is
644 the DECL bound by this name in the class. */
8d08fdba
MS
645 tree class_shadowed;
646
f181d4ae
MM
647 /* Similar to class_shadowed, but for IDENTIFIER_TYPE_VALUE, and
648 is used for all binding levels. */
8d08fdba
MS
649 tree type_shadowed;
650
651 /* For each level (except not the global one),
652 a chain of BLOCK nodes for all the levels
653 that were entered and exited one level down. */
654 tree blocks;
655
656 /* The BLOCK node for this level, if one has been preallocated.
657 If 0, the BLOCK is allocated (if needed) when the level is popped. */
658 tree this_block;
659
660 /* The binding level which this one is contained in (inherits from). */
661 struct binding_level *level_chain;
662
f30432d7 663 /* List of decls in `names' that have incomplete
8d08fdba 664 structure or union types. */
f30432d7 665 tree incomplete;
8d08fdba 666
8d6e462b 667 /* List of VAR_DECLS saved from a previous for statement.
abc95ed3 668 These would be dead in ANSI-conforming code, but might
f181d4ae
MM
669 be referenced in ARM-era code. These are stored in a
670 TREE_LIST; the TREE_VALUE is the actual declaration. */
8d6e462b
PB
671 tree dead_vars_from_for;
672
8d08fdba
MS
673 /* 1 for the level that holds the parameters of a function.
674 2 for the level that holds a class declaration.
675 3 for levels that hold parameter declarations. */
676 unsigned parm_flag : 4;
677
678 /* 1 means make a BLOCK for this level regardless of all else.
679 2 for temporary binding contours created by the compiler. */
680 unsigned keep : 3;
681
682 /* Nonzero if this level "doesn't exist" for tags. */
683 unsigned tag_transparent : 1;
684
685 /* Nonzero if this level can safely have additional
686 cleanup-needing variables added to it. */
687 unsigned more_cleanups_ok : 1;
688 unsigned have_cleanups : 1;
689
5566b478
MS
690 /* Nonzero if this level is for storing the decls for template
691 parameters and generic decls; these decls will be discarded and
692 replaced with a TEMPLATE_DECL. */
8d08fdba
MS
693 unsigned pseudo_global : 1;
694
a9aedbc2
MS
695 /* This is set for a namespace binding level. */
696 unsigned namespace_p : 1;
697
2ee887f2 698 /* True if this level is that of a for-statement where we need to
d22c8596 699 worry about ambiguous (ARM or ANSI) scope rules. */
8d6e462b
PB
700 unsigned is_for_scope : 1;
701
5566b478 702 /* Two bits left for this word. */
8d08fdba
MS
703
704#if defined(DEBUG_CP_BINDING_LEVELS)
705 /* Binding depth at which this level began. */
706 unsigned binding_depth;
707#endif /* defined(DEBUG_CP_BINDING_LEVELS) */
708 };
709
710#define NULL_BINDING_LEVEL ((struct binding_level *) NULL)
711
712/* The (non-class) binding level currently in effect. */
713
714static struct binding_level *current_binding_level;
715
716/* The binding level of the current class, if any. */
717
718static struct binding_level *class_binding_level;
719
720/* The current (class or non-class) binding level currently in effect. */
721
722#define inner_binding_level \
723 (class_binding_level ? class_binding_level : current_binding_level)
724
725/* A chain of binding_level structures awaiting reuse. */
726
727static struct binding_level *free_binding_level;
728
729/* The outermost binding level, for names of file scope.
730 This is created when the compiler is started and exists
731 through the entire run. */
732
733static struct binding_level *global_binding_level;
734
735/* Binding level structures are initialized by copying this one. */
736
737static struct binding_level clear_binding_level;
738
739/* Nonzero means unconditionally make a BLOCK for the next level pushed. */
740
741static int keep_next_level_flag;
742
743#if defined(DEBUG_CP_BINDING_LEVELS)
744static int binding_depth = 0;
745static int is_class_level = 0;
746
747static void
748indent ()
749{
750 register unsigned i;
751
752 for (i = 0; i < binding_depth*2; i++)
753 putc (' ', stderr);
754}
755#endif /* defined(DEBUG_CP_BINDING_LEVELS) */
756
757static tree pushdecl_with_scope PROTO((tree, struct binding_level *));
758
759static void
760push_binding_level (newlevel, tag_transparent, keep)
761 struct binding_level *newlevel;
762 int tag_transparent, keep;
763{
764 /* Add this level to the front of the chain (stack) of levels that
765 are active. */
766 *newlevel = clear_binding_level;
767 if (class_binding_level)
768 {
769 newlevel->level_chain = class_binding_level;
770 class_binding_level = (struct binding_level *)0;
771 }
772 else
773 {
774 newlevel->level_chain = current_binding_level;
775 }
776 current_binding_level = newlevel;
777 newlevel->tag_transparent = tag_transparent;
778 newlevel->more_cleanups_ok = 1;
8d08fdba
MS
779 newlevel->keep = keep;
780#if defined(DEBUG_CP_BINDING_LEVELS)
781 newlevel->binding_depth = binding_depth;
782 indent ();
783 fprintf (stderr, "push %s level 0x%08x line %d\n",
784 (is_class_level) ? "class" : "block", newlevel, lineno);
785 is_class_level = 0;
786 binding_depth++;
787#endif /* defined(DEBUG_CP_BINDING_LEVELS) */
788}
789
790static void
791pop_binding_level ()
792{
793 if (class_binding_level)
794 current_binding_level = class_binding_level;
795
796 if (global_binding_level)
797 {
2c73f9f5 798 /* Cannot pop a level, if there are none left to pop. */
8d08fdba
MS
799 if (current_binding_level == global_binding_level)
800 my_friendly_abort (123);
801 }
802 /* Pop the current level, and free the structure for reuse. */
803#if defined(DEBUG_CP_BINDING_LEVELS)
804 binding_depth--;
805 indent ();
806 fprintf (stderr, "pop %s level 0x%08x line %d\n",
807 (is_class_level) ? "class" : "block",
808 current_binding_level, lineno);
809 if (is_class_level != (current_binding_level == class_binding_level))
9e9ff709
MS
810 {
811 indent ();
812 fprintf (stderr, "XXX is_class_level != (current_binding_level == class_binding_level)\n");
813 }
8d08fdba
MS
814 is_class_level = 0;
815#endif /* defined(DEBUG_CP_BINDING_LEVELS) */
816 {
817 register struct binding_level *level = current_binding_level;
818 current_binding_level = current_binding_level->level_chain;
819 level->level_chain = free_binding_level;
820#if 0 /* defined(DEBUG_CP_BINDING_LEVELS) */
821 if (level->binding_depth != binding_depth)
822 abort ();
823#endif /* defined(DEBUG_CP_BINDING_LEVELS) */
824 free_binding_level = level;
825
826 class_binding_level = current_binding_level;
827 if (class_binding_level->parm_flag != 2)
828 class_binding_level = 0;
829 while (current_binding_level->parm_flag == 2)
830 current_binding_level = current_binding_level->level_chain;
831 }
832}
a9aedbc2
MS
833
834static void
835suspend_binding_level ()
836{
837 if (class_binding_level)
838 current_binding_level = class_binding_level;
839
840 if (global_binding_level)
841 {
2c73f9f5 842 /* Cannot suspend a level, if there are none left to suspend. */
a9aedbc2
MS
843 if (current_binding_level == global_binding_level)
844 my_friendly_abort (123);
845 }
846 /* Suspend the current level. */
847#if defined(DEBUG_CP_BINDING_LEVELS)
848 binding_depth--;
849 indent ();
850 fprintf (stderr, "suspend %s level 0x%08x line %d\n",
851 (is_class_level) ? "class" : "block",
852 current_binding_level, lineno);
853 if (is_class_level != (current_binding_level == class_binding_level))
9e9ff709
MS
854 {
855 indent ();
856 fprintf (stderr, "XXX is_class_level != (current_binding_level == class_binding_level)\n");
857 }
a9aedbc2
MS
858 is_class_level = 0;
859#endif /* defined(DEBUG_CP_BINDING_LEVELS) */
860 {
a9aedbc2 861 current_binding_level = current_binding_level->level_chain;
a9aedbc2
MS
862 class_binding_level = current_binding_level;
863 if (class_binding_level->parm_flag != 2)
864 class_binding_level = 0;
865 while (current_binding_level->parm_flag == 2)
866 current_binding_level = current_binding_level->level_chain;
867 }
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
a9aedbc2
MS
908/* Nonzero if we are currently in a toplevel binding level. This
909 means either the global binding level or a namespace in a toplevel
2c73f9f5
ML
910 binding level.
911 Since there are no non-toplevel namespace levels, this really
912 means any namespace or pseudo-global level. */
a9aedbc2
MS
913
914int
915toplevel_bindings_p ()
916{
2c73f9f5
ML
917 return current_binding_level->namespace_p
918 || current_binding_level->pseudo_global;
a9aedbc2
MS
919}
920
921/* Nonzero if this is a namespace scope. */
922
7bdbfa05 923int
a9aedbc2
MS
924namespace_bindings_p ()
925{
926 return current_binding_level->namespace_p;
927}
928
8d08fdba
MS
929void
930keep_next_level ()
931{
932 keep_next_level_flag = 1;
933}
934
935/* Nonzero if the current level needs to have a BLOCK made. */
936
937int
938kept_level_p ()
939{
940 return (current_binding_level->blocks != NULL_TREE
941 || current_binding_level->keep
942 || current_binding_level->names != NULL_TREE
943 || (current_binding_level->tags != NULL_TREE
944 && !current_binding_level->tag_transparent));
945}
946
947/* Identify this binding level as a level of parameters. */
948
949void
950declare_parm_level ()
951{
952 current_binding_level->parm_flag = 1;
953}
954
8d08fdba
MS
955void
956declare_pseudo_global_level ()
957{
958 current_binding_level->pseudo_global = 1;
959}
960
824b9a4c 961static void
a9aedbc2
MS
962declare_namespace_level ()
963{
964 current_binding_level->namespace_p = 1;
965}
966
8d08fdba
MS
967int
968pseudo_global_level_p ()
969{
970 return current_binding_level->pseudo_global;
971}
972
973void
974set_class_shadows (shadows)
975 tree shadows;
976{
977 class_binding_level->class_shadowed = shadows;
978}
979
980/* Enter a new binding level.
981 If TAG_TRANSPARENT is nonzero, do so only for the name space of variables,
982 not for that of tags. */
983
984void
985pushlevel (tag_transparent)
986 int tag_transparent;
987{
988 register struct binding_level *newlevel = NULL_BINDING_LEVEL;
989
990 /* If this is the top level of a function,
991 just make sure that NAMED_LABELS is 0.
992 They should have been set to 0 at the end of the previous function. */
993
994 if (current_binding_level == global_binding_level)
995 my_friendly_assert (named_labels == NULL_TREE, 134);
996
997 /* Reuse or create a struct for this binding level. */
998
999#if defined(DEBUG_CP_BINDING_LEVELS)
1000 if (0)
1001#else /* !defined(DEBUG_CP_BINDING_LEVELS) */
1002 if (free_binding_level)
1003#endif /* !defined(DEBUG_CP_BINDING_LEVELS) */
1004 {
1005 newlevel = free_binding_level;
1006 free_binding_level = free_binding_level->level_chain;
1007 }
1008 else
1009 {
cffa8729 1010 newlevel = make_binding_level ();
8d08fdba 1011 }
cffa8729 1012
8d08fdba
MS
1013 push_binding_level (newlevel, tag_transparent, keep_next_level_flag);
1014 GNU_xref_start_scope ((HOST_WIDE_INT) newlevel);
1015 keep_next_level_flag = 0;
1016}
1017
5566b478 1018void
8d6e462b
PB
1019note_level_for_for ()
1020{
1021 current_binding_level->is_for_scope = 1;
1022}
1023
8d08fdba
MS
1024void
1025pushlevel_temporary (tag_transparent)
1026 int tag_transparent;
1027{
1028 pushlevel (tag_transparent);
1029 current_binding_level->keep = 2;
1030 clear_last_expr ();
1031
1032 /* Note we don't call push_momentary() here. Otherwise, it would cause
1033 cleanups to be allocated on the momentary obstack, and they will be
1034 overwritten by the next statement. */
1035
1036 expand_start_bindings (0);
1037}
1038
f181d4ae
MM
1039/* For a binding between a name and an entity at a block scope,
1040 this is the `struct binding_level' for the block. */
1041#define BINDING_LEVEL(NODE) \
c7a932b1 1042 (((struct tree_binding*)NODE)->scope.level)
f181d4ae
MM
1043
1044/* These are currently unused, but permanent, CPLUS_BINDING nodes.
1045 They are kept here because they are allocated from the permanent
1046 obstack and cannot be easily freed. */
1047static tree free_binding_nodes;
1048
1049/* Make DECL the innermost binding for ID. The LEVEL is the binding
1050 level at which this declaration is being bound. */
1051
1052static void
1053push_binding (id, decl, level)
1054 tree id;
1055 tree decl;
1056 struct binding_level* level;
1057{
1058 tree binding;
1059
1060 if (!free_binding_nodes)
1061 {
1062 /* There are no free nodes, so we must build one here. */
1063 push_obstacks_nochange ();
1064 end_temporary_allocation ();
1065 binding = make_node (CPLUS_BINDING);
1066 pop_obstacks ();
1067 }
1068 else
1069 {
1070 /* There are nodes on the free list. Grab the first one. */
1071 binding = free_binding_nodes;
1072
1073 /* And update the free list. */
1074 free_binding_nodes = TREE_CHAIN (free_binding_nodes);
1075 }
1076
1077 /* Now, fill in the binding information. */
1078 BINDING_VALUE (binding) = decl;
d8f8dca1 1079 BINDING_TYPE (binding) = NULL_TREE;
f181d4ae
MM
1080 BINDING_LEVEL (binding) = level;
1081 LOCAL_BINDING_P (binding) = (level != class_binding_level);
1082
1083 /* And put it on the front of the ilst of bindings for ID. */
1084 TREE_CHAIN (binding) = IDENTIFIER_BINDING (id);
1085 IDENTIFIER_BINDING (id) = binding;
1086}
1087
d8f8dca1
MM
1088/* ID is already bound in the current scope. But, DECL is an
1089 additional binding for ID in the same scope. This is the `struct
1090 stat' hack whereby a non-typedef class-name or enum-name can be
1091 bound at the same level as some other kind of entity. It's the
1092 responsibility of the caller to check that inserting this name is
1093 legal here. */
1094static void
1095add_binding (id, decl)
1096 tree id;
1097 tree decl;
1098{
1099 tree binding = IDENTIFIER_BINDING (id);
1100
1101 if (TREE_CODE (decl) == TYPE_DECL && DECL_ARTIFICIAL (decl))
1102 /* The new name is the type name. */
1103 BINDING_TYPE (binding) = decl;
1104 else
1105 {
1106 /* The old name must be the type name. It was placed in
1107 IDENTIFIER_VALUE because it was thought, at the point it
1108 was declared, to be the only entity with such a name. */
1109 my_friendly_assert (TREE_CODE (BINDING_VALUE (binding)) == TYPE_DECL
1110 && DECL_ARTIFICIAL (BINDING_VALUE (binding)),
1111 0);
1112
1113 /* Move the type name into the type slot; it is now hidden by
1114 the new binding. */
1115 BINDING_TYPE (binding) = BINDING_VALUE (binding);
1116 BINDING_VALUE (binding) = decl;
1117 }
1118}
1119
0034cf72
JM
1120/* Bind DECL to ID in the current_binding_level.
1121 If PUSH_USING is set in FLAGS, we know that DECL doesn't really belong
1122 to this binding level, that it got here through a using-declaration. */
f181d4ae
MM
1123
1124void
0034cf72 1125push_local_binding (id, decl, flags)
f181d4ae
MM
1126 tree id;
1127 tree decl;
0034cf72 1128 int flags;
f181d4ae 1129{
d8f8dca1 1130 tree d = decl;
f181d4ae 1131
d8f8dca1
MM
1132 if (lookup_name_current_level (id))
1133 /* Supplement the existing binding. */
a06d48ef 1134 add_binding (id, d);
d8f8dca1
MM
1135 else
1136 /* Create a new binding. */
1137 push_binding (id, d, current_binding_level);
f181d4ae 1138
0034cf72 1139 if (TREE_CODE (decl) == OVERLOAD || (flags & PUSH_USING))
a06d48ef
JM
1140 /* We must put the OVERLOAD into a TREE_LIST since the
1141 TREE_CHAIN of an OVERLOAD is already used. Similarly for
1142 decls that got here through a using-declaration. */
1143 decl = build_tree_list (NULL_TREE, decl);
1144
f181d4ae
MM
1145 /* And put DECL on the list of things declared by the current
1146 binding level. */
1147 TREE_CHAIN (decl) = current_binding_level->names;
1148 current_binding_level->names = decl;
1149}
1150
1151/* Bind DECL to ID in the class_binding_level. */
1152
1153void
1154push_class_binding (id, decl)
1155 tree id;
1156 tree decl;
1157{
d8f8dca1
MM
1158 if (IDENTIFIER_BINDING (id)
1159 && BINDING_LEVEL (IDENTIFIER_BINDING (id)) == class_binding_level)
1160 /* Supplement the existing binding. */
1161 add_binding (id, decl);
1162 else
1163 /* Create a new binding. */
1164 push_binding (id, decl, class_binding_level);
1165
1166 /* Update the IDENTIFIER_CLASS_VALUE for this ID to be the
1167 class-level declaration. Note that we do not use DECL here
1168 because of the possibility of the `struct stat' hack; if DECL is
1169 a class-name or enum-name we might prefer a field-name, or some
1170 such. */
1171 IDENTIFIER_CLASS_VALUE (id) = BINDING_VALUE (IDENTIFIER_BINDING (id));
f181d4ae
MM
1172}
1173
d8f8dca1
MM
1174/* Remove the binding for DECL which should be the innermost binding
1175 for ID. */
f181d4ae
MM
1176
1177static void
d8f8dca1 1178pop_binding (id, decl)
f181d4ae 1179 tree id;
d8f8dca1 1180 tree decl;
f181d4ae
MM
1181{
1182 tree binding;
d8f8dca1 1183
f181d4ae
MM
1184 if (id == NULL_TREE)
1185 /* It's easiest to write the loops that call this function without
1186 checking whether or not the entities involved have names. We
1187 get here for such an entity. */
1188 return;
1189
d8f8dca1 1190 /* Get the innermost binding for ID. */
f181d4ae 1191 binding = IDENTIFIER_BINDING (id);
f181d4ae 1192
d8f8dca1
MM
1193 /* The name should be bound. */
1194 my_friendly_assert (binding != NULL_TREE, 0);
1195
1196 /* The DECL will be either the ordinary binding or the type
1197 binding for this identifier. Remove that binding. */
1198 if (BINDING_VALUE (binding) == decl)
1199 BINDING_VALUE (binding) = NULL_TREE;
1200 else if (BINDING_TYPE (binding) == decl)
1201 BINDING_TYPE (binding) = NULL_TREE;
1202 else
1203 my_friendly_abort (0);
1204
1205 if (!BINDING_VALUE (binding) && !BINDING_TYPE (binding))
1206 {
1207 /* We're completely done with the innermost binding for this
1208 identifier. Unhook it from the list of bindings. */
1209 IDENTIFIER_BINDING (id) = TREE_CHAIN (binding);
1210
1211 /* And place it on the free list. */
1212 TREE_CHAIN (binding) = free_binding_nodes;
1213 free_binding_nodes = binding;
1214 }
f181d4ae
MM
1215}
1216
8d08fdba
MS
1217/* Exit a binding level.
1218 Pop the level off, and restore the state of the identifier-decl mappings
1219 that were in effect when this level was entered.
1220
1221 If KEEP == 1, this level had explicit declarations, so
1222 and create a "block" (a BLOCK node) for the level
1223 to record its declarations and subblocks for symbol table output.
1224
8d08fdba
MS
1225 If FUNCTIONBODY is nonzero, this level is the body of a function,
1226 so create a block as if KEEP were set and also clear out all
1227 label names.
1228
1229 If REVERSE is nonzero, reverse the order of decls before putting
1230 them into the BLOCK. */
1231
1232tree
1233poplevel (keep, reverse, functionbody)
1234 int keep;
1235 int reverse;
1236 int functionbody;
1237{
1238 register tree link;
1239 /* The chain of decls was accumulated in reverse order.
1240 Put it into forward order, just for cleanliness. */
1241 tree decls;
1242 int tmp = functionbody;
8d08fdba
MS
1243 int real_functionbody = current_binding_level->keep == 2
1244 ? ((functionbody = 0), tmp) : functionbody;
1245 tree tags = functionbody >= 0 ? current_binding_level->tags : 0;
1246 tree subblocks = functionbody >= 0 ? current_binding_level->blocks : 0;
1247 tree block = NULL_TREE;
1248 tree decl;
1249 int block_previously_created;
f181d4ae
MM
1250 int leaving_for_scope;
1251
1252 if (current_binding_level->parm_flag == 2
1253 || current_binding_level->class_shadowed)
1254 /* We should not be using poplevel to pop a class binding level.
1255 Use poplevel_class instead. */
1256 my_friendly_abort (0);
8d08fdba 1257
536333d4
MM
1258 /* We used to use KEEP == 2 to indicate that the new block should go
1259 at the beginning of the list of blocks at this binding level,
1260 rather than the end. This hack is no longer used. */
1261 my_friendly_assert (keep == 0 || keep == 1, 0);
1262
8d08fdba
MS
1263 GNU_xref_end_scope ((HOST_WIDE_INT) current_binding_level,
1264 (HOST_WIDE_INT) current_binding_level->level_chain,
1265 current_binding_level->parm_flag,
5566b478 1266 current_binding_level->keep);
8d08fdba
MS
1267
1268 if (current_binding_level->keep == 1)
1269 keep = 1;
1270
8d08fdba
MS
1271 /* Get the decls in the order they were written.
1272 Usually current_binding_level->names is in reverse order.
1273 But parameter decls were previously put in forward order. */
1274
1275 if (reverse)
1276 current_binding_level->names
1277 = decls = nreverse (current_binding_level->names);
1278 else
1279 decls = current_binding_level->names;
1280
1281 /* Output any nested inline functions within this block
1282 if they weren't already output. */
1283
1284 for (decl = decls; decl; decl = TREE_CHAIN (decl))
1285 if (TREE_CODE (decl) == FUNCTION_DECL
1286 && ! TREE_ASM_WRITTEN (decl)
1287 && DECL_INITIAL (decl) != NULL_TREE
6060a796
MS
1288 && TREE_ADDRESSABLE (decl)
1289 && decl_function_context (decl) == current_function_decl)
8d08fdba
MS
1290 {
1291 /* If this decl was copied from a file-scope decl
1292 on account of a block-scope extern decl,
1293 propagate TREE_ADDRESSABLE to the file-scope decl. */
1294 if (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE)
1295 TREE_ADDRESSABLE (DECL_ABSTRACT_ORIGIN (decl)) = 1;
1296 else
1297 {
1298 push_function_context ();
1299 output_inline_function (decl);
1300 pop_function_context ();
1301 }
1302 }
1303
1304 /* If there were any declarations or structure tags in that level,
1305 or if this level is a function body,
1306 create a BLOCK to record them for the life of this function. */
1307
1308 block = NULL_TREE;
1309 block_previously_created = (current_binding_level->this_block != NULL_TREE);
1310 if (block_previously_created)
1311 block = current_binding_level->this_block;
1312 else if (keep == 1 || functionbody)
1313 block = make_node (BLOCK);
1314 if (block != NULL_TREE)
1315 {
72b7eeff
MS
1316 if (block_previously_created)
1317 {
1318 if (decls || tags || subblocks)
1319 {
be99da77 1320 if (BLOCK_VARS (block) || BLOCK_TYPE_TAGS (block))
f181d4ae
MM
1321 warning ("internal compiler error: debugging info corrupted");
1322
72b7eeff
MS
1323 BLOCK_VARS (block) = decls;
1324 BLOCK_TYPE_TAGS (block) = tags;
be99da77
MS
1325
1326 /* We can have previous subblocks and new subblocks when
1327 doing fixup_gotos with complex cleanups. We chain the new
1328 subblocks onto the end of any pre-existing subblocks. */
1329 BLOCK_SUBBLOCKS (block) = chainon (BLOCK_SUBBLOCKS (block),
1330 subblocks);
72b7eeff 1331 }
be99da77
MS
1332 /* If we created the block earlier on, and we are just
1333 diddling it now, then it already should have a proper
1334 BLOCK_END_NOTE value associated with it. */
72b7eeff
MS
1335 }
1336 else
1337 {
1338 BLOCK_VARS (block) = decls;
1339 BLOCK_TYPE_TAGS (block) = tags;
1340 BLOCK_SUBBLOCKS (block) = subblocks;
f181d4ae
MM
1341 /* Otherwise, for a new block, install a new BLOCK_END_NOTE
1342 value. */
72b7eeff
MS
1343 remember_end_note (block);
1344 }
8d08fdba
MS
1345 }
1346
1347 /* In each subblock, record that this is its superior. */
1348
1349 if (keep >= 0)
1350 for (link = subblocks; link; link = TREE_CHAIN (link))
1351 BLOCK_SUPERCONTEXT (link) = block;
1352
f181d4ae
MM
1353 /* We still support the old for-scope rules, whereby the variables
1354 in a for-init statement were in scope after the for-statement
1355 ended. We only use the new rules in flag_new_for_scope is
1356 nonzero. */
1357 leaving_for_scope
1358 = current_binding_level->is_for_scope && flag_new_for_scope == 1;
1359
1360 /* Remove declarations for all the DECLs in this level. */
1361 for (link = decls; link; link = TREE_CHAIN (link))
1362 {
1363 if (leaving_for_scope && TREE_CODE (link) == VAR_DECL)
1364 {
1365 tree outer_binding
1366 = TREE_CHAIN (IDENTIFIER_BINDING (DECL_NAME (link)));
1367 tree ns_binding;
1368
1369 if (!outer_binding)
1370 ns_binding = IDENTIFIER_NAMESPACE_VALUE (DECL_NAME (link));
c7dda1e3
MM
1371 else
1372 ns_binding = NULL_TREE;
1373
f181d4ae
MM
1374 if (outer_binding
1375 && (BINDING_LEVEL (outer_binding)
1376 == current_binding_level->level_chain))
1377 /* We have something like:
1378
1379 int i;
1380 for (int i; ;);
1381
1382 and we are leaving the `for' scope. There's no reason to
1383 keep the binding of the inner `i' in this case. */
d8f8dca1 1384 pop_binding (DECL_NAME (link), link);
f181d4ae
MM
1385 else if ((outer_binding
1386 && (TREE_CODE (BINDING_VALUE (outer_binding))
1387 == TYPE_DECL))
1388 || (ns_binding
1389 && TREE_CODE (ns_binding) == TYPE_DECL))
1390 /* Here, we have something like:
1391
1392 typedef int I;
1393
1394 void f () {
1395 for (int I; ;);
1396 }
1397
1398 We must pop the for-scope binding so we know what's a
1399 type and what isn't. */
d8f8dca1 1400 pop_binding (DECL_NAME (link), link);
e76a2646 1401 else
e76a2646 1402 {
f181d4ae
MM
1403 /* Mark this VAR_DECL as dead so that we can tell we left it
1404 there only for backward compatibility. */
1405 DECL_DEAD_FOR_LOCAL (link) = 1;
1406
1407 /* Keep track of what should of have happenned when we
1408 popped the binding. */
1409 if (outer_binding && BINDING_VALUE (outer_binding))
1410 DECL_SHADOWED_FOR_VAR (link)
1411 = BINDING_VALUE (outer_binding);
1412
1413 /* Add it to the list of dead variables in the next
1414 outermost binding to that we can remove these when we
1415 leave that binding. */
1416 current_binding_level->level_chain->dead_vars_from_for
1417 = tree_cons (NULL_TREE, link,
1418 current_binding_level->level_chain->
1419 dead_vars_from_for);
1420
1421 /* Although we don't pop the CPLUS_BINDING, we do clear
1422 its BINDING_LEVEL since the level is going away now. */
1423 BINDING_LEVEL (IDENTIFIER_BINDING (DECL_NAME (link)))
1424 = 0;
e76a2646 1425 }
2ee887f2 1426 }
f181d4ae 1427 else
8d6e462b 1428 {
f181d4ae 1429 /* Remove the binding. */
0034cf72
JM
1430 decl = link;
1431 if (TREE_CODE (decl) == TREE_LIST)
1432 decl = TREE_VALUE (decl);
1433 if (TREE_CODE_CLASS (TREE_CODE (decl)) == 'd')
1434 pop_binding (DECL_NAME (decl), decl);
1435 else if (TREE_CODE (decl) == OVERLOAD)
1436 pop_binding (DECL_NAME (OVL_FUNCTION (decl)), decl);
d8f8dca1 1437 else
f181d4ae 1438 my_friendly_abort (0);
8d08fdba 1439 }
f181d4ae 1440 }
8d08fdba 1441
f181d4ae
MM
1442 /* Remove declarations for any `for' variables from inner scopes
1443 that we kept around. */
1444 for (link = current_binding_level->dead_vars_from_for;
1445 link; link = TREE_CHAIN (link))
d8f8dca1 1446 pop_binding (DECL_NAME (TREE_VALUE (link)), TREE_VALUE (link));
2ee887f2 1447
f181d4ae
MM
1448 /* Restore the IDENTIFIER_TYPE_VALUEs. */
1449 for (link = current_binding_level->type_shadowed;
1450 link; link = TREE_CHAIN (link))
1451 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (link), TREE_VALUE (link));
1452
1453 /* There may be OVERLOADs (wrapped in TREE_LISTs) on the BLOCK_VARs
1454 list if a `using' declaration put them there. The debugging
1455 back-ends won't understand OVERLOAD, so we remove them here.
1456 Because the BLOCK_VARS are (temporarily) shared with
1457 CURRENT_BINDING_LEVEL->NAMES we must do this fixup after we have
1458 popped all the bindings. */
1459 if (block)
1460 {
1461 tree* d;
8d6e462b 1462
a06d48ef
JM
1463 for (d = &BLOCK_VARS (block); *d; )
1464 {
1465 if (TREE_CODE (*d) == TREE_LIST)
1466 *d = TREE_CHAIN (*d);
1467 else
1468 d = &TREE_CHAIN (*d);
1469 }
8d6e462b 1470 }
8d08fdba
MS
1471
1472 /* If the level being exited is the top level of a function,
1473 check over all the labels. */
1474
1475 if (functionbody)
1476 {
1477 /* If this is the top level block of a function,
1478 the vars are the function's parameters.
1479 Don't leave them in the BLOCK because they are
1480 found in the FUNCTION_DECL instead. */
1481
1482 BLOCK_VARS (block) = 0;
1483
1484 /* Clear out the definitions of all label names,
1485 since their scopes end here. */
1486
1487 for (link = named_labels; link; link = TREE_CHAIN (link))
1488 {
1489 register tree label = TREE_VALUE (link);
1490
1491 if (DECL_INITIAL (label) == NULL_TREE)
1492 {
8251199e 1493 cp_error_at ("label `%D' used but not defined", label);
8d08fdba
MS
1494 /* Avoid crashing later. */
1495 define_label (input_filename, 1, DECL_NAME (label));
1496 }
1497 else if (warn_unused && !TREE_USED (label))
8251199e 1498 cp_warning_at ("label `%D' defined but not used", label);
8d08fdba
MS
1499 SET_IDENTIFIER_LABEL_VALUE (DECL_NAME (label), NULL_TREE);
1500
1501 /* Put the labels into the "variables" of the
1502 top-level block, so debugger can see them. */
1503 TREE_CHAIN (label) = BLOCK_VARS (block);
1504 BLOCK_VARS (block) = label;
1505 }
1506
1507 named_labels = NULL_TREE;
1508 }
1509
1510 /* Any uses of undefined labels now operate under constraints
1511 of next binding contour. */
1512 {
1513 struct binding_level *level_chain;
1514 level_chain = current_binding_level->level_chain;
1515 if (level_chain)
1516 {
e349ee73
MS
1517 struct named_label_list *labels;
1518 for (labels = named_label_uses; labels; labels = labels->next)
1519 if (labels->binding_level == current_binding_level)
8d08fdba 1520 {
e349ee73
MS
1521 labels->binding_level = level_chain;
1522 labels->names_in_scope = level_chain->names;
8d08fdba
MS
1523 }
1524 }
1525 }
1526
1527 tmp = current_binding_level->keep;
1528
1529 pop_binding_level ();
1530 if (functionbody)
1531 DECL_INITIAL (current_function_decl) = block;
1532 else if (block)
1533 {
1534 if (!block_previously_created)
1535 current_binding_level->blocks
1536 = chainon (current_binding_level->blocks, block);
1537 }
1538 /* If we did not make a block for the level just exited,
1539 any blocks made for inner levels
1540 (since they cannot be recorded as subblocks in that level)
1541 must be carried forward so they will later become subblocks
1542 of something else. */
1543 else if (subblocks)
536333d4
MM
1544 current_binding_level->blocks
1545 = chainon (current_binding_level->blocks, subblocks);
8d08fdba
MS
1546
1547 /* Take care of compiler's internal binding structures. */
a4443a08 1548 if (tmp == 2)
8d08fdba 1549 {
8d08fdba
MS
1550 expand_end_bindings (getdecls (), keep, 1);
1551 /* Each and every BLOCK node created here in `poplevel' is important
1552 (e.g. for proper debugging information) so if we created one
1553 earlier, mark it as "used". */
1554 if (block)
1555 TREE_USED (block) = 1;
1556 block = poplevel (keep, reverse, real_functionbody);
1557 }
1558
1559 /* Each and every BLOCK node created here in `poplevel' is important
1560 (e.g. for proper debugging information) so if we created one
1561 earlier, mark it as "used". */
1562 if (block)
1563 TREE_USED (block) = 1;
1564 return block;
1565}
1566
1567/* Delete the node BLOCK from the current binding level.
1568 This is used for the block inside a stmt expr ({...})
1569 so that the block can be reinserted where appropriate. */
1570
1571void
1572delete_block (block)
1573 tree block;
1574{
1575 tree t;
1576 if (current_binding_level->blocks == block)
1577 current_binding_level->blocks = TREE_CHAIN (block);
1578 for (t = current_binding_level->blocks; t;)
1579 {
1580 if (TREE_CHAIN (t) == block)
1581 TREE_CHAIN (t) = TREE_CHAIN (block);
1582 else
1583 t = TREE_CHAIN (t);
1584 }
1585 TREE_CHAIN (block) = NULL_TREE;
1586 /* Clear TREE_USED which is always set by poplevel.
1587 The flag is set again if insert_block is called. */
1588 TREE_USED (block) = 0;
1589}
1590
1591/* Insert BLOCK at the end of the list of subblocks of the
1592 current binding level. This is used when a BIND_EXPR is expanded,
1593 to handle the BLOCK node inside the BIND_EXPR. */
1594
1595void
1596insert_block (block)
1597 tree block;
1598{
1599 TREE_USED (block) = 1;
1600 current_binding_level->blocks
1601 = chainon (current_binding_level->blocks, block);
1602}
1603
8d08fdba
MS
1604/* Set the BLOCK node for the innermost scope
1605 (the one we are currently in). */
1606
1607void
1608set_block (block)
1609 register tree block;
1610{
1611 current_binding_level->this_block = block;
1612}
1613
1614/* Do a pushlevel for class declarations. */
e92cc029 1615
8d08fdba
MS
1616void
1617pushlevel_class ()
1618{
1619 register struct binding_level *newlevel;
1620
1621 /* Reuse or create a struct for this binding level. */
1622#if defined(DEBUG_CP_BINDING_LEVELS)
1623 if (0)
1624#else /* !defined(DEBUG_CP_BINDING_LEVELS) */
1625 if (free_binding_level)
1626#endif /* !defined(DEBUG_CP_BINDING_LEVELS) */
1627 {
1628 newlevel = free_binding_level;
1629 free_binding_level = free_binding_level->level_chain;
1630 }
1631 else
f181d4ae 1632 newlevel = make_binding_level ();
8d08fdba
MS
1633
1634#if defined(DEBUG_CP_BINDING_LEVELS)
1635 is_class_level = 1;
1636#endif /* defined(DEBUG_CP_BINDING_LEVELS) */
1637
1638 push_binding_level (newlevel, 0, 0);
1639
1640 decl_stack = push_decl_level (decl_stack, &decl_obstack);
1641 class_binding_level = current_binding_level;
1642 class_binding_level->parm_flag = 2;
1643 /* We have just pushed into a new binding level. Now, fake out the rest
1644 of the compiler. Set the `current_binding_level' back to point to
1645 the most closely containing non-class binding level. */
1646 do
1647 {
1648 current_binding_level = current_binding_level->level_chain;
1649 }
1650 while (current_binding_level->parm_flag == 2);
1651}
1652
700f8a87
MS
1653/* ...and a poplevel for class declarations. FORCE is used to force
1654 clearing out of CLASS_VALUEs after a class definition. */
e92cc029 1655
8d08fdba 1656tree
700f8a87
MS
1657poplevel_class (force)
1658 int force;
8d08fdba
MS
1659{
1660 register struct binding_level *level = class_binding_level;
1661 tree block = NULL_TREE;
1662 tree shadowed;
1663
1664 my_friendly_assert (level != 0, 354);
1665
1666 decl_stack = pop_stack_level (decl_stack);
8d08fdba 1667 /* If we're leaving a toplevel class, don't bother to do the setting
ddd5a7c1 1668 of IDENTIFIER_CLASS_VALUE to NULL_TREE, since first of all this slot
8d08fdba 1669 shouldn't even be used when current_class_type isn't set, and second,
ddd5a7c1 1670 if we don't touch it here, we're able to use the cache effect if the
8d08fdba 1671 next time we're entering a class scope, it is the same class. */
700f8a87 1672 if (current_class_depth != 1 || force)
8d08fdba
MS
1673 for (shadowed = level->class_shadowed;
1674 shadowed;
1675 shadowed = TREE_CHAIN (shadowed))
1676 IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (shadowed)) = TREE_VALUE (shadowed);
1677 else
1678 /* Remember to save what IDENTIFIER's were bound in this scope so we
1679 can recover from cache misses. */
e76a2646
MS
1680 {
1681 previous_class_type = current_class_type;
1682 previous_class_values = class_binding_level->class_shadowed;
1683 }
8d08fdba
MS
1684 for (shadowed = level->type_shadowed;
1685 shadowed;
1686 shadowed = TREE_CHAIN (shadowed))
2c73f9f5 1687 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (shadowed), TREE_VALUE (shadowed));
8d08fdba 1688
f181d4ae
MM
1689 /* Remove the bindings for all of the class-level declarations. */
1690 for (shadowed = level->class_shadowed;
1691 shadowed;
1692 shadowed = TREE_CHAIN (shadowed))
d8f8dca1 1693 pop_binding (TREE_PURPOSE (shadowed), TREE_TYPE (shadowed));
f181d4ae 1694
8d08fdba
MS
1695 GNU_xref_end_scope ((HOST_WIDE_INT) class_binding_level,
1696 (HOST_WIDE_INT) class_binding_level->level_chain,
1697 class_binding_level->parm_flag,
5566b478 1698 class_binding_level->keep);
8d08fdba
MS
1699
1700 if (class_binding_level->parm_flag != 2)
1701 class_binding_level = (struct binding_level *)0;
1702
38e01259 1703 /* Now, pop out of the binding level which we created up in the
8d08fdba
MS
1704 `pushlevel_class' routine. */
1705#if defined(DEBUG_CP_BINDING_LEVELS)
1706 is_class_level = 1;
1707#endif /* defined(DEBUG_CP_BINDING_LEVELS) */
1708
1709 pop_binding_level ();
1710
1711 return block;
1712}
1713\f
1714/* For debugging. */
5566b478
MS
1715static int no_print_functions = 0;
1716static int no_print_builtins = 0;
8d08fdba
MS
1717
1718void
1719print_binding_level (lvl)
1720 struct binding_level *lvl;
1721{
1722 tree t;
1723 int i = 0, len;
1724 fprintf (stderr, " blocks=");
1725 fprintf (stderr, HOST_PTR_PRINTF, lvl->blocks);
1726 fprintf (stderr, " n_incomplete=%d parm_flag=%d keep=%d",
f30432d7 1727 list_length (lvl->incomplete), lvl->parm_flag, lvl->keep);
8d08fdba
MS
1728 if (lvl->tag_transparent)
1729 fprintf (stderr, " tag-transparent");
1730 if (lvl->more_cleanups_ok)
1731 fprintf (stderr, " more-cleanups-ok");
1732 if (lvl->have_cleanups)
1733 fprintf (stderr, " have-cleanups");
8d08fdba
MS
1734 fprintf (stderr, "\n");
1735 if (lvl->names)
1736 {
1737 fprintf (stderr, " names:\t");
1738 /* We can probably fit 3 names to a line? */
1739 for (t = lvl->names; t; t = TREE_CHAIN (t))
1740 {
fc378698 1741 if (no_print_functions && (TREE_CODE (t) == FUNCTION_DECL))
8d08fdba
MS
1742 continue;
1743 if (no_print_builtins
fc378698
MS
1744 && (TREE_CODE (t) == TYPE_DECL)
1745 && (!strcmp (DECL_SOURCE_FILE (t),"<built-in>")))
8d08fdba
MS
1746 continue;
1747
1748 /* Function decls tend to have longer names. */
1749 if (TREE_CODE (t) == FUNCTION_DECL)
1750 len = 3;
1751 else
1752 len = 2;
1753 i += len;
1754 if (i > 6)
1755 {
1756 fprintf (stderr, "\n\t");
1757 i = len;
1758 }
1759 print_node_brief (stderr, "", t, 0);
bd6dd845 1760 if (t == error_mark_node)
8d08fdba
MS
1761 break;
1762 }
1763 if (i)
1764 fprintf (stderr, "\n");
1765 }
1766 if (lvl->tags)
1767 {
1768 fprintf (stderr, " tags:\t");
1769 i = 0;
1770 for (t = lvl->tags; t; t = TREE_CHAIN (t))
1771 {
1772 if (TREE_PURPOSE (t) == NULL_TREE)
1773 len = 3;
1774 else if (TREE_PURPOSE (t) == TYPE_IDENTIFIER (TREE_VALUE (t)))
1775 len = 2;
1776 else
1777 len = 4;
1778 i += len;
1779 if (i > 5)
1780 {
1781 fprintf (stderr, "\n\t");
1782 i = len;
1783 }
1784 if (TREE_PURPOSE (t) == NULL_TREE)
1785 {
1786 print_node_brief (stderr, "<unnamed-typedef", TREE_VALUE (t), 0);
1787 fprintf (stderr, ">");
1788 }
1789 else if (TREE_PURPOSE (t) == TYPE_IDENTIFIER (TREE_VALUE (t)))
1790 print_node_brief (stderr, "", TREE_VALUE (t), 0);
1791 else
1792 {
1793 print_node_brief (stderr, "<typedef", TREE_PURPOSE (t), 0);
1794 print_node_brief (stderr, "", TREE_VALUE (t), 0);
1795 fprintf (stderr, ">");
1796 }
1797 }
1798 if (i)
1799 fprintf (stderr, "\n");
1800 }
8d08fdba
MS
1801 if (lvl->class_shadowed)
1802 {
1803 fprintf (stderr, " class-shadowed:");
1804 for (t = lvl->class_shadowed; t; t = TREE_CHAIN (t))
1805 {
1806 fprintf (stderr, " %s ", IDENTIFIER_POINTER (TREE_PURPOSE (t)));
1807 }
1808 fprintf (stderr, "\n");
1809 }
1810 if (lvl->type_shadowed)
1811 {
1812 fprintf (stderr, " type-shadowed:");
1813 for (t = lvl->type_shadowed; t; t = TREE_CHAIN (t))
1814 {
8d08fdba 1815 fprintf (stderr, " %s ", IDENTIFIER_POINTER (TREE_PURPOSE (t)));
8d08fdba
MS
1816 }
1817 fprintf (stderr, "\n");
1818 }
1819}
1820
1821void
1822print_other_binding_stack (stack)
1823 struct binding_level *stack;
1824{
1825 struct binding_level *level;
1826 for (level = stack; level != global_binding_level; level = level->level_chain)
1827 {
1828 fprintf (stderr, "binding level ");
1829 fprintf (stderr, HOST_PTR_PRINTF, level);
1830 fprintf (stderr, "\n");
1831 print_binding_level (level);
1832 }
1833}
1834
1835void
1836print_binding_stack ()
1837{
1838 struct binding_level *b;
1839 fprintf (stderr, "current_binding_level=");
1840 fprintf (stderr, HOST_PTR_PRINTF, current_binding_level);
1841 fprintf (stderr, "\nclass_binding_level=");
1842 fprintf (stderr, HOST_PTR_PRINTF, class_binding_level);
1843 fprintf (stderr, "\nglobal_binding_level=");
1844 fprintf (stderr, HOST_PTR_PRINTF, global_binding_level);
1845 fprintf (stderr, "\n");
1846 if (class_binding_level)
1847 {
1848 for (b = class_binding_level; b; b = b->level_chain)
1849 if (b == current_binding_level)
1850 break;
1851 if (b)
1852 b = class_binding_level;
1853 else
1854 b = current_binding_level;
1855 }
1856 else
1857 b = current_binding_level;
1858 print_other_binding_stack (b);
1859 fprintf (stderr, "global:\n");
1860 print_binding_level (global_binding_level);
1861}
a9aedbc2 1862
2c73f9f5
ML
1863/* Namespace binding access routines: The namespace_bindings field of
1864 the identifier is polymorphic, with three possible values:
1865 NULL_TREE, a list of CPLUS_BINDINGS, or any other tree_node
1866 indicating the BINDING_VALUE of global_namespace. */
30394414 1867
2c73f9f5
ML
1868/* Check whether the a binding for the name to scope is known.
1869 Assumes that the bindings of the name are already a list
1870 of bindings. Returns the binding found, or NULL_TREE. */
1871
1872static tree
1873find_binding (name, scope)
30394414
JM
1874 tree name;
1875 tree scope;
1876{
2c73f9f5 1877 tree iter, prev = NULL_TREE;
3e3f722c
ML
1878
1879 scope = ORIGINAL_NAMESPACE (scope);
1880
30394414
JM
1881 for (iter = IDENTIFIER_NAMESPACE_BINDINGS (name); iter;
1882 iter = TREE_CHAIN (iter))
1883 {
1884 my_friendly_assert (TREE_CODE (iter) == CPLUS_BINDING, 374);
1885 if (BINDING_SCOPE (iter) == scope)
2c73f9f5
ML
1886 {
1887 /* Move binding found to the fron of the list, so
1888 subsequent lookups will find it faster. */
1889 if (prev)
1890 {
1891 TREE_CHAIN (prev) = TREE_CHAIN (iter);
1892 TREE_CHAIN (iter) = IDENTIFIER_NAMESPACE_BINDINGS (name);
1893 IDENTIFIER_NAMESPACE_BINDINGS (name) = iter;
1894 }
1895 return iter;
1896 }
1897 prev = iter;
30394414 1898 }
2c73f9f5
ML
1899 return NULL_TREE;
1900}
1901
1902/* Always returns a binding for name in scope. If the
1903 namespace_bindings is not a list, convert it to one first.
1904 If no binding is found, make a new one. */
1905
1906tree
1907binding_for_name (name, scope)
1908 tree name;
1909 tree scope;
1910{
1911 tree b = IDENTIFIER_NAMESPACE_BINDINGS (name);
1912 tree result;
3e3f722c
ML
1913
1914 scope = ORIGINAL_NAMESPACE (scope);
1915
2c73f9f5
ML
1916 if (b && TREE_CODE (b) != CPLUS_BINDING)
1917 {
1918 /* Get rid of optimization for global scope. */
1919 IDENTIFIER_NAMESPACE_BINDINGS (name) = NULL_TREE;
1920 BINDING_VALUE (binding_for_name (name, global_namespace)) = b;
1921 b = IDENTIFIER_NAMESPACE_BINDINGS (name);
1922 }
1923 if (b && (result = find_binding (name, scope)))
1924 return result;
1925 /* Not found, make a new permanent one. */
30394414 1926 push_obstacks (&permanent_obstack, &permanent_obstack);
2c73f9f5
ML
1927 result = make_node (CPLUS_BINDING);
1928 TREE_CHAIN (result) = b;
1929 IDENTIFIER_NAMESPACE_BINDINGS (name) = result;
1930 BINDING_SCOPE (result) = scope;
1931 BINDING_TYPE (result) = NULL_TREE;
1932 BINDING_VALUE (result) = NULL_TREE;
30394414 1933 pop_obstacks ();
2c73f9f5
ML
1934 return result;
1935}
1936
1937/* Return the binding value for name in scope, considering that
1938 namespace_binding may or may not be a list of CPLUS_BINDINGS. */
1939
1940tree
1941namespace_binding (name, scope)
1942 tree name;
1943 tree scope;
1944{
1945 tree b = IDENTIFIER_NAMESPACE_BINDINGS (name);
1946 if (b == NULL_TREE)
1947 return NULL_TREE;
cb0dbb9a
JM
1948 if (scope == NULL_TREE)
1949 scope = global_namespace;
2c73f9f5
ML
1950 if (TREE_CODE (b) != CPLUS_BINDING)
1951 return (scope == global_namespace) ? b : NULL_TREE;
1952 name = find_binding (name,scope);
1953 if (name == NULL_TREE)
1954 return name;
1955 return BINDING_VALUE (name);
1956}
1957
1958/* Set the binding value for name in scope. If modifying the binding
1959 of global_namespace is attempted, try to optimize it. */
1960
1961void
1962set_namespace_binding (name, scope, val)
1963 tree name;
1964 tree scope;
1965 tree val;
1966{
1967 tree b;
cb0dbb9a
JM
1968
1969 if (scope == NULL_TREE)
1970 scope = global_namespace;
1971
2c73f9f5
ML
1972 if (scope == global_namespace)
1973 {
1974 b = IDENTIFIER_NAMESPACE_BINDINGS (name);
1975 if (b == NULL_TREE || TREE_CODE (b) != CPLUS_BINDING)
1976 {
1977 IDENTIFIER_NAMESPACE_BINDINGS (name) = val;
1978 return;
1979 }
1980 }
1981 b = binding_for_name (name, scope);
1982 BINDING_VALUE (b) = val;
30394414
JM
1983}
1984
dff6b454
RK
1985/* Push into the scope of the NAME namespace. If NAME is NULL_TREE, then we
1986 select a name that is unique to this compilation unit. */
e92cc029 1987
a9aedbc2
MS
1988void
1989push_namespace (name)
1990 tree name;
1991{
b370501f 1992 tree d = NULL_TREE;
30394414
JM
1993 int need_new = 1;
1994 int implicit_use = 0;
2c73f9f5 1995 int global = 0;
30394414
JM
1996 if (!global_namespace)
1997 {
2c73f9f5 1998 /* This must be ::. */
30394414 1999 my_friendly_assert (name == get_identifier ("::"), 377);
2c73f9f5 2000 global = 1;
30394414
JM
2001 }
2002 else if (!name)
2003 {
3ab52652
ML
2004 /* The name of anonymous namespace is unique for the translation
2005 unit. */
0c8feefe
MM
2006 if (!anonymous_namespace_name)
2007 anonymous_namespace_name = get_file_function_name ('N');
2008 name = anonymous_namespace_name;
3ab52652
ML
2009 d = IDENTIFIER_NAMESPACE_VALUE (name);
2010 if (d)
2011 /* Reopening anonymous namespace. */
2012 need_new = 0;
30394414 2013 implicit_use = 1;
2c73f9f5
ML
2014 }
2015 else if (current_namespace == global_namespace
2016 && name == DECL_NAME (std_node))
2017 {
2018 in_std++;
2019 return;
2020 }
30394414
JM
2021 else
2022 {
2c73f9f5 2023 /* Check whether this is an extended namespace definition. */
30394414
JM
2024 d = IDENTIFIER_NAMESPACE_VALUE (name);
2025 if (d != NULL_TREE && TREE_CODE (d) == NAMESPACE_DECL)
3e3f722c
ML
2026 {
2027 need_new = 0;
2028 if (DECL_NAMESPACE_ALIAS (d))
2029 {
8251199e 2030 cp_error ("namespace alias `%D' not allowed here, assuming `%D'",
3e3f722c
ML
2031 d, DECL_NAMESPACE_ALIAS (d));
2032 d = DECL_NAMESPACE_ALIAS (d);
2033 }
2034 }
30394414 2035 }
6633d636 2036
30394414
JM
2037 if (need_new)
2038 {
2c73f9f5 2039 /* Make a new namespace, binding the name to it. */
6b57ac29 2040 d = build_lang_decl (NAMESPACE_DECL, name, void_type_node);
2c73f9f5
ML
2041 /* The global namespace is not pushed, and the global binding
2042 level is set elsewhere. */
2043 if (!global)
2044 {
2045 d = pushdecl (d);
2046 pushlevel (0);
2047 declare_namespace_level ();
2048 NAMESPACE_LEVEL (d) = current_binding_level;
2049 }
30394414 2050 }
2c73f9f5
ML
2051 else
2052 resume_binding_level (NAMESPACE_LEVEL (d));
2053
30394414
JM
2054 if (implicit_use)
2055 do_using_directive (d);
2c73f9f5 2056 /* Enter the name space. */
30394414 2057 current_namespace = d;
a9aedbc2
MS
2058}
2059
2060/* Pop from the scope of the current namespace. */
e92cc029 2061
a9aedbc2
MS
2062void
2063pop_namespace ()
2064{
2c73f9f5
ML
2065 if (current_namespace == global_namespace)
2066 {
2067 my_friendly_assert (in_std>0, 980421);
2068 in_std--;
2069 return;
2070 }
cb0dbb9a 2071 current_namespace = CP_DECL_CONTEXT (current_namespace);
2c73f9f5
ML
2072 /* The binding level is not popped, as it might be re-opened later. */
2073 suspend_binding_level ();
2074}
a9aedbc2 2075
2c73f9f5 2076/* Concatenate the binding levels of all namespaces. */
a9aedbc2 2077
2c73f9f5
ML
2078void
2079cat_namespace_levels()
2080{
2081 tree current;
2082 tree last;
2083 struct binding_level *b;
a9aedbc2 2084
2c73f9f5
ML
2085 last = NAMESPACE_LEVEL (global_namespace) -> names;
2086 /* The nested namespaces appear in the names list of their ancestors. */
2087 for (current = last; current; current = TREE_CHAIN (current))
a9aedbc2 2088 {
a06d48ef
JM
2089 /* Catch simple infinite loops. */
2090 if (TREE_CHAIN (current) == current)
2091 my_friendly_abort (990126);
2092
85c6cbaf
ML
2093 if (TREE_CODE (current) != NAMESPACE_DECL
2094 || DECL_NAMESPACE_ALIAS (current))
2c73f9f5
ML
2095 continue;
2096 if (!DECL_LANG_SPECIFIC (current))
a9aedbc2 2097 {
2c73f9f5
ML
2098 /* Hmm. std. */
2099 my_friendly_assert (current == std_node, 393);
2100 continue;
a9aedbc2 2101 }
2c73f9f5
ML
2102 b = NAMESPACE_LEVEL (current);
2103 while (TREE_CHAIN (last))
2104 last = TREE_CHAIN (last);
2105 TREE_CHAIN (last) = NAMESPACE_LEVEL (current) -> names;
a9aedbc2 2106 }
a9aedbc2 2107}
8d08fdba
MS
2108\f
2109/* Subroutines for reverting temporarily to top-level for instantiation
2110 of templates and such. We actually need to clear out the class- and
2111 local-value slots of all identifiers, so that only the global values
2112 are at all visible. Simply setting current_binding_level to the global
2113 scope isn't enough, because more binding levels may be pushed. */
2114struct saved_scope {
2115 struct binding_level *old_binding_level;
2116 tree old_bindings;
2c73f9f5 2117 tree old_namespace;
8d08fdba 2118 struct saved_scope *prev;
61a127b3
MM
2119 tree class_name, class_type;
2120 tree access_specifier;
2121 tree function_decl;
8d08fdba 2122 struct binding_level *class_bindings;
51c184be
MS
2123 tree *lang_base, *lang_stack, lang_name;
2124 int lang_stacksize;
5566b478
MS
2125 int minimal_parse_mode;
2126 tree last_function_parms;
e76a2646 2127 tree template_parms;
5156628f 2128 HOST_WIDE_INT processing_template_decl;
a50f0918 2129 tree previous_class_type, previous_class_values;
e1467ff2
MM
2130 int processing_specialization;
2131 int processing_explicit_instantiation;
8d08fdba
MS
2132};
2133static struct saved_scope *current_saved_scope;
8d08fdba 2134
78957a2a
JM
2135/* A chain of the binding vecs created by store_bindings. We create a
2136 whole bunch of these during compilation, on permanent_obstack, so we
2137 can't just throw them away. */
2138static tree free_binding_vecs;
2139
e92cc029 2140static tree
45537677
MS
2141store_bindings (names, old_bindings)
2142 tree names, old_bindings;
2143{
2144 tree t;
2145 for (t = names; t; t = TREE_CHAIN (t))
2146 {
2147 tree binding, t1, id;
2148
2149 if (TREE_CODE (t) == TREE_LIST)
2150 id = TREE_PURPOSE (t);
2151 else
2152 id = DECL_NAME (t);
2153
f181d4ae
MM
2154 if (!id
2155 /* Note that we may have an IDENTIFIER_CLASS_VALUE even when
2156 we have no IDENTIFIER_BINDING if we have left the class
2157 scope, but cached the class-level declarations. */
2158 || !(IDENTIFIER_BINDING (id) || IDENTIFIER_CLASS_VALUE (id)))
45537677
MS
2159 continue;
2160
2161 for (t1 = old_bindings; t1; t1 = TREE_CHAIN (t1))
2162 if (TREE_VEC_ELT (t1, 0) == id)
2163 goto skip_it;
78957a2a
JM
2164
2165 if (free_binding_vecs)
2166 {
2167 binding = free_binding_vecs;
2168 free_binding_vecs = TREE_CHAIN (free_binding_vecs);
2169 }
2170 else
2171 binding = make_tree_vec (4);
2172
45537677
MS
2173 if (id)
2174 {
2175 my_friendly_assert (TREE_CODE (id) == IDENTIFIER_NODE, 135);
2176 TREE_VEC_ELT (binding, 0) = id;
2c73f9f5 2177 TREE_VEC_ELT (binding, 1) = REAL_IDENTIFIER_TYPE_VALUE (id);
f181d4ae 2178 TREE_VEC_ELT (binding, 2) = IDENTIFIER_BINDING (id);
45537677 2179 TREE_VEC_ELT (binding, 3) = IDENTIFIER_CLASS_VALUE (id);
f181d4ae 2180 IDENTIFIER_BINDING (id) = NULL_TREE;
45537677
MS
2181 IDENTIFIER_CLASS_VALUE (id) = NULL_TREE;
2182 }
2183 TREE_CHAIN (binding) = old_bindings;
2184 old_bindings = binding;
2185 skip_it:
2186 ;
2187 }
2188 return old_bindings;
2189}
2190
8d08fdba 2191void
5566b478
MS
2192maybe_push_to_top_level (pseudo)
2193 int pseudo;
8d08fdba 2194{
51c184be 2195 extern int current_lang_stacksize;
beb53fb8
JM
2196 struct saved_scope *s
2197 = (struct saved_scope *) xmalloc (sizeof (struct saved_scope));
5566b478 2198 struct binding_level *b = inner_binding_level;
8d08fdba
MS
2199 tree old_bindings = NULL_TREE;
2200
e349ee73
MS
2201 if (current_function_decl)
2202 push_cp_function_context (NULL_TREE);
2203
a50f0918
MS
2204 if (previous_class_type)
2205 old_bindings = store_bindings (previous_class_values, old_bindings);
2206
8d08fdba
MS
2207 /* Have to include global_binding_level, because class-level decls
2208 aren't listed anywhere useful. */
2209 for (; b; b = b->level_chain)
2210 {
2211 tree t;
2212
2c73f9f5
ML
2213 /* Template IDs are inserted into the global level. If they were
2214 inserted into namespace level, finish_file wouldn't find them
2215 when doing pending instantiations. Therefore, don't stop at
2216 namespace level, but continue until :: . */
5566b478
MS
2217 if (b == global_binding_level || (pseudo && b->pseudo_global))
2218 break;
8d08fdba 2219
45537677 2220 old_bindings = store_bindings (b->names, old_bindings);
cffa8729 2221 /* We also need to check class_shadowed to save class-level type
45537677
MS
2222 bindings, since pushclass doesn't fill in b->names. */
2223 if (b->parm_flag == 2)
cffa8729 2224 old_bindings = store_bindings (b->class_shadowed, old_bindings);
8d08fdba 2225
8d08fdba
MS
2226 /* Unwind type-value slots back to top level. */
2227 for (t = b->type_shadowed; t; t = TREE_CHAIN (t))
2228 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (t), TREE_VALUE (t));
2229 }
8d08fdba
MS
2230
2231 s->old_binding_level = current_binding_level;
5566b478 2232 current_binding_level = b;
8d08fdba 2233
2c73f9f5 2234 s->old_namespace = current_namespace;
8d08fdba
MS
2235 s->class_name = current_class_name;
2236 s->class_type = current_class_type;
61a127b3 2237 s->access_specifier = current_access_specifier;
8d08fdba
MS
2238 s->function_decl = current_function_decl;
2239 s->class_bindings = class_binding_level;
51c184be
MS
2240 s->lang_stack = current_lang_stack;
2241 s->lang_base = current_lang_base;
2242 s->lang_stacksize = current_lang_stacksize;
2243 s->lang_name = current_lang_name;
5566b478
MS
2244 s->minimal_parse_mode = minimal_parse_mode;
2245 s->last_function_parms = last_function_parms;
e76a2646 2246 s->template_parms = current_template_parms;
5156628f 2247 s->processing_template_decl = processing_template_decl;
a50f0918
MS
2248 s->previous_class_type = previous_class_type;
2249 s->previous_class_values = previous_class_values;
e1467ff2
MM
2250 s->processing_specialization = processing_specialization;
2251 s->processing_explicit_instantiation = processing_explicit_instantiation;
e349ee73 2252
5f34005f 2253 current_class_name = current_class_type = NULL_TREE;
8d08fdba
MS
2254 current_function_decl = NULL_TREE;
2255 class_binding_level = (struct binding_level *)0;
51c184be
MS
2256 current_lang_stacksize = 10;
2257 current_lang_stack = current_lang_base
2258 = (tree *) xmalloc (current_lang_stacksize * sizeof (tree));
2259 current_lang_name = lang_name_cplusplus;
2260 strict_prototype = strict_prototypes_lang_cplusplus;
2261 named_labels = NULL_TREE;
120722ac 2262 shadowed_labels = NULL_TREE;
5566b478 2263 minimal_parse_mode = 0;
a50f0918 2264 previous_class_type = previous_class_values = NULL_TREE;
e1467ff2
MM
2265 processing_specialization = 0;
2266 processing_explicit_instantiation = 0;
eae97bd9
MM
2267 current_template_parms = NULL_TREE;
2268 processing_template_decl = 0;
2c73f9f5 2269 current_namespace = global_namespace;
8d08fdba
MS
2270
2271 s->prev = current_saved_scope;
2272 s->old_bindings = old_bindings;
2273 current_saved_scope = s;
5566b478
MS
2274
2275 push_obstacks (&permanent_obstack, &permanent_obstack);
2276}
2277
2278void
2279push_to_top_level ()
2280{
2281 maybe_push_to_top_level (0);
8d08fdba
MS
2282}
2283
2284void
2285pop_from_top_level ()
2286{
51c184be 2287 extern int current_lang_stacksize;
8d08fdba
MS
2288 struct saved_scope *s = current_saved_scope;
2289 tree t;
2290
e76a2646 2291 /* Clear out class-level bindings cache. */
8d08fdba 2292 if (previous_class_type)
e76a2646
MS
2293 {
2294 popclass (-1);
2295 previous_class_type = NULL_TREE;
2296 }
8d08fdba 2297
5566b478
MS
2298 pop_obstacks ();
2299
8d08fdba
MS
2300 current_binding_level = s->old_binding_level;
2301 current_saved_scope = s->prev;
78957a2a 2302 for (t = s->old_bindings; t; )
8d08fdba 2303 {
78957a2a 2304 tree save = t;
8d08fdba
MS
2305 tree id = TREE_VEC_ELT (t, 0);
2306 if (id)
2307 {
2c73f9f5 2308 SET_IDENTIFIER_TYPE_VALUE (id, TREE_VEC_ELT (t, 1));
f181d4ae 2309 IDENTIFIER_BINDING (id) = TREE_VEC_ELT (t, 2);
8d08fdba
MS
2310 IDENTIFIER_CLASS_VALUE (id) = TREE_VEC_ELT (t, 3);
2311 }
78957a2a
JM
2312 t = TREE_CHAIN (t);
2313 TREE_CHAIN (save) = free_binding_vecs;
2314 free_binding_vecs = save;
8d08fdba 2315 }
2c73f9f5 2316 current_namespace = s->old_namespace;
8d08fdba
MS
2317 current_class_name = s->class_name;
2318 current_class_type = s->class_type;
61a127b3 2319 current_access_specifier = s->access_specifier;
8d08fdba
MS
2320 current_function_decl = s->function_decl;
2321 class_binding_level = s->class_bindings;
51c184be
MS
2322 free (current_lang_base);
2323 current_lang_base = s->lang_base;
2324 current_lang_stack = s->lang_stack;
2325 current_lang_name = s->lang_name;
2326 current_lang_stacksize = s->lang_stacksize;
2327 if (current_lang_name == lang_name_cplusplus)
2328 strict_prototype = strict_prototypes_lang_cplusplus;
2329 else if (current_lang_name == lang_name_c)
2330 strict_prototype = strict_prototypes_lang_c;
5566b478
MS
2331 minimal_parse_mode = s->minimal_parse_mode;
2332 last_function_parms = s->last_function_parms;
e76a2646 2333 current_template_parms = s->template_parms;
5156628f 2334 processing_template_decl = s->processing_template_decl;
a50f0918
MS
2335 previous_class_type = s->previous_class_type;
2336 previous_class_values = s->previous_class_values;
e1467ff2
MM
2337 processing_specialization = s->processing_specialization;
2338 processing_explicit_instantiation = s->processing_explicit_instantiation;
51c184be 2339
8d08fdba 2340 free (s);
e349ee73
MS
2341
2342 if (current_function_decl)
2343 pop_cp_function_context (NULL_TREE);
8d08fdba
MS
2344}
2345\f
2346/* Push a definition of struct, union or enum tag "name".
2347 into binding_level "b". "type" should be the type node,
2348 We assume that the tag "name" is not already defined.
2349
2350 Note that the definition may really be just a forward reference.
2351 In that case, the TYPE_SIZE will be a NULL_TREE.
2352
e92cc029 2353 C++ gratuitously puts all these tags in the name space. */
8d08fdba
MS
2354
2355/* When setting the IDENTIFIER_TYPE_VALUE field of an identifier ID,
2356 record the shadowed value for this binding contour. TYPE is
2357 the type that ID maps to. */
2358
2359static void
2360set_identifier_type_value_with_scope (id, type, b)
2361 tree id;
2362 tree type;
2363 struct binding_level *b;
2364{
2c73f9f5 2365 if (!b->namespace_p)
8d08fdba 2366 {
2c73f9f5
ML
2367 /* Shadow the marker, not the real thing, so that the marker
2368 gets restored later. */
2369 tree old_type_value = REAL_IDENTIFIER_TYPE_VALUE (id);
8d08fdba
MS
2370 b->type_shadowed
2371 = tree_cons (id, old_type_value, b->type_shadowed);
2372 }
2c73f9f5
ML
2373 else
2374 {
2375 tree binding = binding_for_name (id, current_namespace);
2376 BINDING_TYPE (binding) = type;
2377 /* Store marker instead of real type. */
2378 type = global_type_node;
2379 }
8d08fdba
MS
2380 SET_IDENTIFIER_TYPE_VALUE (id, type);
2381}
2382
e92cc029 2383/* As set_identifier_type_value_with_scope, but using inner_binding_level. */
8d08fdba
MS
2384
2385void
2386set_identifier_type_value (id, type)
2387 tree id;
2388 tree type;
2389{
2390 set_identifier_type_value_with_scope (id, type, inner_binding_level);
2391}
2392
2c73f9f5
ML
2393/* Return the type associated with id. */
2394
2395tree
2396identifier_type_value (id)
2397 tree id;
2398{
2399 /* There is no type with that name, anywhere. */
2400 if (REAL_IDENTIFIER_TYPE_VALUE (id) == NULL_TREE)
2401 return NULL_TREE;
2402 /* This is not the type marker, but the real thing. */
2403 if (REAL_IDENTIFIER_TYPE_VALUE (id) != global_type_node)
2404 return REAL_IDENTIFIER_TYPE_VALUE (id);
2405 /* Have to search for it. It must be on the global level, now.
2406 Ask lookup_name not to return non-types. */
3e3f722c 2407 id = lookup_name_real (id, 2, 1, 0);
2c73f9f5
ML
2408 if (id)
2409 return TREE_TYPE (id);
2410 return NULL_TREE;
2411}
2412
a9aedbc2
MS
2413/* Pop off extraneous binding levels left over due to syntax errors.
2414
2415 We don't pop past namespaces, as they might be valid. */
e92cc029 2416
8926095f
MS
2417void
2418pop_everything ()
2419{
2420#ifdef DEBUG_CP_BINDING_LEVELS
2421 fprintf (stderr, "XXX entering pop_everything ()\n");
2422#endif
a9aedbc2 2423 while (! toplevel_bindings_p () && ! pseudo_global_level_p ())
8926095f
MS
2424 {
2425 if (class_binding_level)
2426 pop_nested_class (1);
2427 else
2428 poplevel (0, 0, 0);
2429 }
2430#ifdef DEBUG_CP_BINDING_LEVELS
2431 fprintf (stderr, "XXX leaving pop_everything ()\n");
2432#endif
2433}
2434
39c01e4c
MM
2435/* The type TYPE is being declared. If it is a class template, or a
2436 specialization of a class template, do any processing required and
2437 perform error-checking. If IS_FRIEND is non-zero, this TYPE is
2438 being declared a friend. B is the binding level at which this TYPE
2439 should be bound.
2440
2441 Returns the TYPE_DECL for TYPE, which may have been altered by this
2442 processing. */
2443
2444static tree
2445maybe_process_template_type_declaration (type, globalize, b)
2446 tree type;
2447 int globalize;
2448 struct binding_level* b;
2449{
2450 tree decl = TYPE_NAME (type);
2451
2452 if (processing_template_parmlist)
2453 /* You can't declare a new template type in a template parameter
2454 list. But, you can declare a non-template type:
2455
2456 template <class A*> struct S;
2457
2458 is a forward-declaration of `A'. */
2459 ;
2460 else
2461 {
2462 maybe_check_template_type (type);
2463
ed44da02
MM
2464 my_friendly_assert (IS_AGGR_TYPE (type)
2465 || TREE_CODE (type) == ENUMERAL_TYPE, 0);
2466
2467
2468 if (/* If !GLOBALIZE then we are looking at a definition.
2469 It may not be a primary template. (For example, in:
39c01e4c 2470
ed44da02
MM
2471 template <class T>
2472 struct S1 { class S2 {}; }
39c01e4c 2473
ed44da02
MM
2474 we have to push_template_decl for S2.) */
2475 (processing_template_decl && !globalize)
2476 /* If we are declaring a friend template class, we will
2477 have GLOBALIZE set, since something like:
39c01e4c 2478
ed44da02
MM
2479 template <class T>
2480 struct S1 {
2481 template <class U>
2482 friend class S2;
2483 };
39c01e4c 2484
ed44da02
MM
2485 declares S2 to be at global scope. */
2486 || PROCESSING_REAL_TEMPLATE_DECL_P ())
39c01e4c
MM
2487 {
2488 /* This may change after the call to
2489 push_template_decl_real, but we want the original value. */
2490 tree name = DECL_NAME (decl);
2491
2492 decl = push_template_decl_real (decl, globalize);
2493 /* If the current binding level is the binding level for the
2494 template parameters (see the comment in
2495 begin_template_parm_list) and the enclosing level is a class
2496 scope, and we're not looking at a friend, push the
2497 declaration of the member class into the class scope. In the
2498 friend case, push_template_decl will already have put the
2499 friend into global scope, if appropriate. */
ed44da02
MM
2500 if (TREE_CODE (type) != ENUMERAL_TYPE
2501 && !globalize && b->pseudo_global
39c01e4c
MM
2502 && b->level_chain->parm_flag == 2)
2503 {
2504 pushdecl_with_scope (CLASSTYPE_TI_TEMPLATE (type),
2505 b->level_chain);
61a127b3 2506 finish_member_declaration (CLASSTYPE_TI_TEMPLATE (type));
39c01e4c
MM
2507 /* Put this tag on the list of tags for the class, since
2508 that won't happen below because B is not the class
2509 binding level, but is instead the pseudo-global level. */
2510 b->level_chain->tags =
2511 saveable_tree_cons (name, type, b->level_chain->tags);
2512 TREE_NONLOCAL_FLAG (type) = 1;
2513 if (TYPE_SIZE (current_class_type) == NULL_TREE)
2514 CLASSTYPE_TAGS (current_class_type) = b->level_chain->tags;
2515 }
2516 }
2517 }
2518
2519 return decl;
2520}
2521
8d08fdba 2522/* Push a tag name NAME for struct/class/union/enum type TYPE.
6757edfe 2523 Normally put it into the inner-most non-tag-transparent scope,
8d08fdba 2524 but if GLOBALIZE is true, put it in the inner-most non-class scope.
e92cc029 2525 The latter is needed for implicit declarations. */
8d08fdba
MS
2526
2527void
2528pushtag (name, type, globalize)
2529 tree name, type;
2530 int globalize;
2531{
2532 register struct binding_level *b;
7177d104 2533 tree context = 0;
2986ae00 2534 tree c_decl = 0;
8d08fdba
MS
2535
2536 b = inner_binding_level;
2537 while (b->tag_transparent
2538 || (globalize && b->parm_flag == 2))
2539 b = b->level_chain;
2540
a9aedbc2 2541 if (toplevel_bindings_p ())
8d08fdba
MS
2542 b->tags = perm_tree_cons (name, type, b->tags);
2543 else
2544 b->tags = saveable_tree_cons (name, type, b->tags);
2545
2546 if (name)
2547 {
7177d104 2548 context = type ? TYPE_CONTEXT (type) : NULL_TREE;
297dcfb3
MM
2549 if (! context)
2550 {
2551 tree cs = current_scope ();
2552
2553 if (! globalize)
2554 context = cs;
2555 else if (cs != NULL_TREE
2556 && TREE_CODE_CLASS (TREE_CODE (cs)) == 't')
2557 /* When declaring a friend class of a local class, we want
2558 to inject the newly named class into the scope
2559 containing the local class, not the namespace scope. */
2560 context = hack_decl_function_context (get_type_decl (cs));
2561 }
7177d104 2562 if (context)
2986ae00 2563 c_decl = TREE_CODE (context) == FUNCTION_DECL
e1cd6e56 2564 ? context : TYPE_MAIN_DECL (context);
8d08fdba 2565
2c73f9f5
ML
2566 if (!context)
2567 context = current_namespace;
2568
8d08fdba 2569 /* Do C++ gratuitous typedefing. */
db5ae43f 2570 if (IDENTIFIER_TYPE_VALUE (name) != type)
8d08fdba 2571 {
93cdc044
JM
2572 register tree d = NULL_TREE;
2573 int newdecl = 0, in_class = 0;
8d08fdba 2574
93cdc044
JM
2575 if ((b->pseudo_global && b->level_chain->parm_flag == 2)
2576 || b->parm_flag == 2)
2577 in_class = 1;
8d08fdba 2578 else
93cdc044
JM
2579 d = lookup_nested_type (type, c_decl);
2580
2581 if (d == NULL_TREE)
8d08fdba 2582 {
8d08fdba 2583 newdecl = 1;
a0a33927 2584 d = build_decl (TYPE_DECL, name, type);
eff71ab0
PB
2585 if (current_lang_name == lang_name_java)
2586 TYPE_FOR_JAVA (type) = 1;
00595019 2587 SET_DECL_ARTIFICIAL (d);
93cdc044
JM
2588 if (! in_class)
2589 set_identifier_type_value_with_scope (name, type, b);
2590 }
2591 else
2592 d = TYPE_MAIN_DECL (d);
2593
2594 TYPE_NAME (type) = d;
cb0dbb9a 2595 DECL_CONTEXT (d) = FROB_CONTEXT (context);
e1cd6e56 2596
39c01e4c
MM
2597 d = maybe_process_template_type_declaration (type,
2598 globalize, b);
93cdc044
JM
2599
2600 if (b->parm_flag == 2)
61a127b3
MM
2601 {
2602 pushdecl_class_level (d);
2603 if (newdecl && !PROCESSING_REAL_TEMPLATE_DECL_P ())
2604 /* Put this TYPE_DECL on the TYPE_FIELDS list for the
2605 class. But if it's a member template class, we
2606 want the TEMPLATE_DECL, not the TYPE_DECL, so this
2607 is done later. */
2608 finish_member_declaration (d);
2609 }
93cdc044
JM
2610 else
2611 d = pushdecl_with_scope (d, b);
2612
8ccc31eb 2613 if (newdecl)
8d08fdba 2614 {
d2e5ee5c
MS
2615 if (ANON_AGGRNAME_P (name))
2616 DECL_IGNORED_P (d) = 1;
8ccc31eb 2617
5566b478 2618 TYPE_CONTEXT (type) = DECL_CONTEXT (d);
1f06b267 2619 DECL_ASSEMBLER_NAME (d) = DECL_NAME (d);
e4a84209
MM
2620 if (!uses_template_parms (type))
2621 DECL_ASSEMBLER_NAME (d)
2622 = get_identifier (build_overload_name (type, 1, 1));
8d08fdba 2623 }
8d08fdba
MS
2624 }
2625 if (b->parm_flag == 2)
2626 {
2627 TREE_NONLOCAL_FLAG (type) = 1;
2628 if (TYPE_SIZE (current_class_type) == NULL_TREE)
2629 CLASSTYPE_TAGS (current_class_type) = b->tags;
2630 }
2631 }
2632
2633 if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL)
2634 /* Use the canonical TYPE_DECL for this node. */
2635 TYPE_STUB_DECL (type) = TYPE_NAME (type);
2636 else
2637 {
2638 /* Create a fake NULL-named TYPE_DECL node whose TREE_TYPE
2639 will be the tagged type we just added to the current
2640 binding level. This fake NULL-named TYPE_DECL node helps
2641 dwarfout.c to know when it needs to output a
2642 representation of a tagged type, and it also gives us a
2643 convenient place to record the "scope start" address for
2644 the tagged type. */
2645
8d08fdba 2646 tree d = build_decl (TYPE_DECL, NULL_TREE, type);
8d08fdba
MS
2647 TYPE_STUB_DECL (type) = pushdecl_with_scope (d, b);
2648 }
2649}
2650
2651/* Counter used to create anonymous type names. */
e92cc029 2652
8d08fdba
MS
2653static int anon_cnt = 0;
2654
2655/* Return an IDENTIFIER which can be used as a name for
2656 anonymous structs and unions. */
e92cc029 2657
8d08fdba
MS
2658tree
2659make_anon_name ()
2660{
2661 char buf[32];
2662
2663 sprintf (buf, ANON_AGGRNAME_FORMAT, anon_cnt++);
2664 return get_identifier (buf);
2665}
2666
2667/* Clear the TREE_PURPOSE slot of tags which have anonymous typenames.
2668 This keeps dbxout from getting confused. */
e92cc029 2669
8d08fdba
MS
2670void
2671clear_anon_tags ()
2672{
2673 register struct binding_level *b;
2674 register tree tags;
2675 static int last_cnt = 0;
2676
2677 /* Fast out if no new anon names were declared. */
2678 if (last_cnt == anon_cnt)
2679 return;
2680
2681 b = current_binding_level;
2682 while (b->tag_transparent)
2683 b = b->level_chain;
2684 tags = b->tags;
2685 while (tags)
2686 {
2687 /* A NULL purpose means we have already processed all tags
2688 from here to the end of the list. */
2689 if (TREE_PURPOSE (tags) == NULL_TREE)
2690 break;
2691 if (ANON_AGGRNAME_P (TREE_PURPOSE (tags)))
2692 TREE_PURPOSE (tags) = NULL_TREE;
2693 tags = TREE_CHAIN (tags);
2694 }
2695 last_cnt = anon_cnt;
2696}
2697\f
2698/* Subroutine of duplicate_decls: return truthvalue of whether
2699 or not types of these decls match.
2700
2701 For C++, we must compare the parameter list so that `int' can match
2702 `int&' in a parameter position, but `int&' is not confused with
2703 `const int&'. */
e92cc029 2704
6060a796 2705int
8d08fdba
MS
2706decls_match (newdecl, olddecl)
2707 tree newdecl, olddecl;
2708{
2709 int types_match;
2710
347d73d7
ML
2711 if (newdecl == olddecl)
2712 return 1;
2713
6b4b3deb
MM
2714 if (TREE_CODE (newdecl) != TREE_CODE (olddecl))
2715 /* If the two DECLs are not even the same kind of thing, we're not
2716 interested in their types. */
2717 return 0;
2718
2719 if (TREE_CODE (newdecl) == FUNCTION_DECL)
8d08fdba
MS
2720 {
2721 tree f1 = TREE_TYPE (newdecl);
2722 tree f2 = TREE_TYPE (olddecl);
2723 tree p1 = TYPE_ARG_TYPES (f1);
2724 tree p2 = TYPE_ARG_TYPES (f2);
2725
c5a6fc45
JM
2726 if (DECL_REAL_CONTEXT (newdecl) != DECL_REAL_CONTEXT (olddecl)
2727 && ! (DECL_LANGUAGE (newdecl) == lang_c
2728 && DECL_LANGUAGE (olddecl) == lang_c))
2729 return 0;
2730
8d08fdba
MS
2731 /* When we parse a static member function definition,
2732 we put together a FUNCTION_DECL which thinks its type
2733 is METHOD_TYPE. Change that to FUNCTION_TYPE, and
2734 proceed. */
2735 if (TREE_CODE (f1) == METHOD_TYPE && DECL_STATIC_FUNCTION_P (olddecl))
700f8a87 2736 revert_static_member_fn (&newdecl, &f1, &p1);
8d08fdba
MS
2737 else if (TREE_CODE (f2) == METHOD_TYPE
2738 && DECL_STATIC_FUNCTION_P (newdecl))
700f8a87 2739 revert_static_member_fn (&olddecl, &f2, &p2);
8d08fdba
MS
2740
2741 /* Here we must take care of the case where new default
2742 parameters are specified. Also, warn if an old
2743 declaration becomes ambiguous because default
2744 parameters may cause the two to be ambiguous. */
2745 if (TREE_CODE (f1) != TREE_CODE (f2))
2746 {
2747 if (TREE_CODE (f1) == OFFSET_TYPE)
8251199e 2748 cp_compiler_error ("`%D' redeclared as member function", newdecl);
8d08fdba 2749 else
8251199e 2750 cp_compiler_error ("`%D' redeclared as non-member function", newdecl);
8d08fdba
MS
2751 return 0;
2752 }
2753
3bfdc719 2754 if (same_type_p (TREE_TYPE (f1), TREE_TYPE (f2)))
8926095f 2755 {
a28e3c7f 2756 if (! strict_prototypes_lang_c && DECL_LANGUAGE (olddecl) == lang_c
8926095f 2757 && p2 == NULL_TREE)
a28e3c7f
MS
2758 {
2759 types_match = self_promoting_args_p (p1);
2760 if (p1 == void_list_node)
2761 TREE_TYPE (newdecl) = TREE_TYPE (olddecl);
2762 }
2763 else if (!strict_prototypes_lang_c && DECL_LANGUAGE (olddecl)==lang_c
2764 && DECL_LANGUAGE (newdecl) == lang_c && p1 == NULL_TREE)
2765 {
2766 types_match = self_promoting_args_p (p2);
2767 TREE_TYPE (newdecl) = TREE_TYPE (olddecl);
2768 }
8926095f 2769 else
91063b51 2770 types_match = compparms (p1, p2);
8926095f 2771 }
8d08fdba
MS
2772 else
2773 types_match = 0;
2774 }
6b4b3deb 2775 else if (TREE_CODE (newdecl) == TEMPLATE_DECL)
51c184be 2776 {
f84b4be9
JM
2777 if (!comp_template_parms (DECL_TEMPLATE_PARMS (newdecl),
2778 DECL_TEMPLATE_PARMS (olddecl)))
2779 return 0;
2780
2781 if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL)
2782 types_match = 1;
2783 else
2784 types_match = decls_match (DECL_TEMPLATE_RESULT (olddecl),
2785 DECL_TEMPLATE_RESULT (newdecl));
51c184be 2786 }
8d08fdba
MS
2787 else
2788 {
2789 if (TREE_TYPE (newdecl) == error_mark_node)
2790 types_match = TREE_TYPE (olddecl) == error_mark_node;
2791 else if (TREE_TYPE (olddecl) == NULL_TREE)
2792 types_match = TREE_TYPE (newdecl) == NULL_TREE;
a0a33927
MS
2793 else if (TREE_TYPE (newdecl) == NULL_TREE)
2794 types_match = 0;
8d08fdba 2795 else
01240200 2796 types_match = comptypes (TREE_TYPE (newdecl),
3bfdc719
MM
2797 TREE_TYPE (olddecl),
2798 COMPARE_REDECLARATION);
8d08fdba
MS
2799 }
2800
2801 return types_match;
2802}
2803
2804/* If NEWDECL is `static' and an `extern' was seen previously,
2805 warn about it. (OLDDECL may be NULL_TREE; NAME contains
2806 information about previous usage as an `extern'.)
2807
2808 Note that this does not apply to the C++ case of declaring
2809 a variable `extern const' and then later `const'.
2810
8d08fdba
MS
2811 Don't complain about built-in functions, since they are beyond
2812 the user's control. */
2813
2814static void
2815warn_extern_redeclared_static (newdecl, olddecl)
2816 tree newdecl, olddecl;
2817{
2818 tree name;
2819
8251199e
JM
2820 static char *explicit_extern_static_warning
2821 = "`%D' was declared `extern' and later `static'";
2822 static char *implicit_extern_static_warning
2823 = "`%D' was declared implicitly `extern' and later `static'";
2824
d22c8596 2825 if (TREE_CODE (newdecl) == TYPE_DECL)
8d08fdba
MS
2826 return;
2827
2828 name = DECL_ASSEMBLER_NAME (newdecl);
faae18ab 2829 if (TREE_PUBLIC (name) && DECL_THIS_STATIC (newdecl))
8d08fdba
MS
2830 {
2831 /* It's okay to redeclare an ANSI built-in function as static,
2832 or to declare a non-ANSI built-in function as anything. */
2833 if (! (TREE_CODE (newdecl) == FUNCTION_DECL
2834 && olddecl != NULL_TREE
2835 && TREE_CODE (olddecl) == FUNCTION_DECL
2836 && (DECL_BUILT_IN (olddecl)
2837 || DECL_BUILT_IN_NONANSI (olddecl))))
2838 {
a9aedbc2 2839 cp_pedwarn (IDENTIFIER_IMPLICIT_DECL (name)
8251199e
JM
2840 ? implicit_extern_static_warning
2841 : explicit_extern_static_warning, newdecl);
8d08fdba 2842 if (olddecl != NULL_TREE)
8251199e 2843 cp_pedwarn_at ("previous declaration of `%D'", olddecl);
8d08fdba
MS
2844 }
2845 }
2846}
2847
2848/* Handle when a new declaration NEWDECL has the same name as an old
2849 one OLDDECL in the same binding contour. Prints an error message
2850 if appropriate.
2851
2852 If safely possible, alter OLDDECL to look like NEWDECL, and return 1.
2853 Otherwise, return 0. */
2854
51c184be 2855int
8d08fdba 2856duplicate_decls (newdecl, olddecl)
824b9a4c 2857 tree newdecl, olddecl;
8d08fdba
MS
2858{
2859 extern struct obstack permanent_obstack;
2860 unsigned olddecl_uid = DECL_UID (olddecl);
2861 int olddecl_friend = 0, types_match = 0;
0b60dfe3 2862 int new_defines_function = 0;
5566b478
MS
2863
2864 if (newdecl == olddecl)
2865 return 1;
8d08fdba 2866
8926095f 2867 types_match = decls_match (newdecl, olddecl);
8d08fdba 2868
8d08fdba
MS
2869 /* If either the type of the new decl or the type of the old decl is an
2870 error_mark_node, then that implies that we have already issued an
2871 error (earlier) for some bogus type specification, and in that case,
2872 it is rather pointless to harass the user with yet more error message
0b60dfe3 2873 about the same declaration, so just pretend the types match here. */
bd6dd845
MS
2874 if (TREE_TYPE (newdecl) == error_mark_node
2875 || TREE_TYPE (olddecl) == error_mark_node)
8d08fdba 2876 types_match = 1;
0b60dfe3
BK
2877
2878 /* Check for redeclaration and other discrepancies. */
d22c8596
MS
2879 if (TREE_CODE (olddecl) == FUNCTION_DECL
2880 && DECL_ARTIFICIAL (olddecl)
2881 && (DECL_BUILT_IN (olddecl) || DECL_BUILT_IN_NONANSI (olddecl)))
8d08fdba
MS
2882 {
2883 /* If you declare a built-in or predefined function name as static,
a4443a08
MS
2884 the old definition is overridden, but optionally warn this was a
2885 bad choice of name. Ditto for overloads. */
893de33c 2886 if (! TREE_PUBLIC (newdecl)
a4443a08
MS
2887 || (TREE_CODE (newdecl) == FUNCTION_DECL
2888 && DECL_LANGUAGE (newdecl) != DECL_LANGUAGE (olddecl)))
2889 {
2890 if (warn_shadow)
8251199e 2891 cp_warning ("shadowing %s function `%#D'",
a4443a08
MS
2892 DECL_BUILT_IN (olddecl) ? "built-in" : "library",
2893 olddecl);
2894 /* Discard the old built-in function. */
2895 return 0;
2896 }
2897 else if (! types_match)
8d08fdba 2898 {
a4443a08
MS
2899 if (TREE_CODE (newdecl) != FUNCTION_DECL)
2900 {
2901 /* If the built-in is not ansi, then programs can override
2902 it even globally without an error. */
2903 if (! DECL_BUILT_IN (olddecl))
8251199e 2904 cp_warning ("library function `%#D' redeclared as non-function `%#D'",
a4443a08
MS
2905 olddecl, newdecl);
2906 else
2907 {
8251199e
JM
2908 cp_error ("declaration of `%#D'", newdecl);
2909 cp_error ("conflicts with built-in declaration `%#D'",
a4443a08
MS
2910 olddecl);
2911 }
2912 return 0;
2913 }
2914
8251199e
JM
2915 cp_warning ("declaration of `%#D'", newdecl);
2916 cp_warning ("conflicts with built-in declaration `%#D'",
8d08fdba 2917 olddecl);
8d08fdba 2918 }
39211cd5
MS
2919 }
2920 else if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
2921 {
9ed182dc
JM
2922 if ((TREE_CODE (olddecl) == TYPE_DECL && DECL_ARTIFICIAL (olddecl)
2923 && TREE_CODE (newdecl) != TYPE_DECL
2924 && ! (TREE_CODE (newdecl) == TEMPLATE_DECL
2925 && TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL))
2926 || (TREE_CODE (newdecl) == TYPE_DECL && DECL_ARTIFICIAL (newdecl)
2927 && TREE_CODE (olddecl) != TYPE_DECL
2928 && ! (TREE_CODE (olddecl) == TEMPLATE_DECL
2929 && (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl))
2930 == TYPE_DECL))))
2931 {
2932 /* We do nothing special here, because C++ does such nasty
2933 things with TYPE_DECLs. Instead, just let the TYPE_DECL
2934 get shadowed, and know that if we need to find a TYPE_DECL
2935 for a given name, we can look in the IDENTIFIER_TYPE_VALUE
2936 slot of the identifier. */
2937 return 0;
2938 }
2939
39211cd5 2940 if ((TREE_CODE (newdecl) == FUNCTION_DECL
5566b478 2941 && DECL_FUNCTION_TEMPLATE_P (olddecl))
39211cd5 2942 || (TREE_CODE (olddecl) == FUNCTION_DECL
5566b478 2943 && DECL_FUNCTION_TEMPLATE_P (newdecl)))
39211cd5 2944 return 0;
9ed182dc 2945
8251199e 2946 cp_error ("`%#D' redeclared as different kind of symbol", newdecl);
39211cd5
MS
2947 if (TREE_CODE (olddecl) == TREE_LIST)
2948 olddecl = TREE_VALUE (olddecl);
8251199e 2949 cp_error_at ("previous declaration of `%#D'", olddecl);
39211cd5
MS
2950
2951 /* New decl is completely inconsistent with the old one =>
2952 tell caller to replace the old one. */
2953
2954 return 0;
8d08fdba 2955 }
8d08fdba
MS
2956 else if (!types_match)
2957 {
7bdbfa05
MM
2958 if (DECL_REAL_CONTEXT (newdecl) != DECL_REAL_CONTEXT (olddecl))
2959 /* These are certainly not duplicate declarations; they're
2960 from different scopes. */
2961 return 0;
2962
8926095f 2963 if (TREE_CODE (newdecl) == TEMPLATE_DECL)
f0e01782
MS
2964 {
2965 /* The name of a class template may not be declared to refer to
2966 any other template, class, function, object, namespace, value,
e92cc029 2967 or type in the same scope. */
5566b478
MS
2968 if (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) == TYPE_DECL
2969 || TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL)
f0e01782 2970 {
8251199e
JM
2971 cp_error ("declaration of template `%#D'", newdecl);
2972 cp_error_at ("conflicts with previous declaration `%#D'",
f0e01782
MS
2973 olddecl);
2974 }
ec255269
MS
2975 else if (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) == FUNCTION_DECL
2976 && TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == FUNCTION_DECL
2977 && compparms (TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (olddecl))),
91063b51 2978 TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (newdecl))))
75650646
MM
2979 && comp_template_parms (DECL_TEMPLATE_PARMS (newdecl),
2980 DECL_TEMPLATE_PARMS (olddecl)))
ec255269 2981 {
8251199e
JM
2982 cp_error ("new declaration `%#D'", newdecl);
2983 cp_error_at ("ambiguates old declaration `%#D'", olddecl);
ec255269 2984 }
f0e01782
MS
2985 return 0;
2986 }
8926095f
MS
2987 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2988 {
2989 if (DECL_LANGUAGE (newdecl) == lang_c
2990 && DECL_LANGUAGE (olddecl) == lang_c)
2991 {
8251199e 2992 cp_error ("declaration of C function `%#D' conflicts with",
8926095f 2993 newdecl);
8251199e 2994 cp_error_at ("previous declaration `%#D' here", olddecl);
8926095f 2995 }
00595019 2996 else if (compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
91063b51 2997 TYPE_ARG_TYPES (TREE_TYPE (olddecl))))
700f8a87 2998 {
8251199e
JM
2999 cp_error ("new declaration `%#D'", newdecl);
3000 cp_error_at ("ambiguates old declaration `%#D'", olddecl);
700f8a87
MS
3001 }
3002 else
3003 return 0;
8926095f 3004 }
8d08fdba
MS
3005
3006 /* Already complained about this, so don't do so again. */
a4443a08 3007 else if (current_class_type == NULL_TREE
8d08fdba
MS
3008 || IDENTIFIER_ERROR_LOCUS (DECL_ASSEMBLER_NAME (newdecl)) != current_class_type)
3009 {
8251199e
JM
3010 cp_error ("conflicting types for `%#D'", newdecl);
3011 cp_error_at ("previous declaration as `%#D'", olddecl);
8d08fdba
MS
3012 }
3013 }
75650646
MM
3014 else if (TREE_CODE (newdecl) == FUNCTION_DECL
3015 && ((DECL_TEMPLATE_SPECIALIZATION (olddecl)
3016 && (!DECL_TEMPLATE_INFO (newdecl)
3017 || (DECL_TI_TEMPLATE (newdecl)
3018 != DECL_TI_TEMPLATE (olddecl))))
3019 || (DECL_TEMPLATE_SPECIALIZATION (newdecl)
3020 && (!DECL_TEMPLATE_INFO (olddecl)
3021 || (DECL_TI_TEMPLATE (olddecl)
3022 != DECL_TI_TEMPLATE (newdecl))))))
386b8a85
JM
3023 /* It's OK to have a template specialization and a non-template
3024 with the same type, or to have specializations of two
75650646
MM
3025 different templates with the same type. Note that if one is a
3026 specialization, and the other is an instantiation of the same
3027 template, that we do not exit at this point. That situation
3028 can occur if we instantiate a template class, and then
3029 specialize one of its methods. This situation is legal, but
3030 the declarations must be merged in the usual way. */
3031 return 0;
3032 else if (TREE_CODE (newdecl) == FUNCTION_DECL
3033 && ((DECL_TEMPLATE_INSTANTIATION (olddecl)
3034 && !DECL_USE_TEMPLATE (newdecl))
3035 || (DECL_TEMPLATE_INSTANTIATION (newdecl)
3036 && !DECL_USE_TEMPLATE (olddecl))))
3037 /* One of the declarations is a template instantiation, and the
3038 other is not a template at all. That's OK. */
386b8a85 3039 return 0;
85c6cbaf
ML
3040 else if (TREE_CODE (newdecl) == NAMESPACE_DECL
3041 && DECL_NAMESPACE_ALIAS (newdecl)
3042 && DECL_NAMESPACE_ALIAS (newdecl) == DECL_NAMESPACE_ALIAS (olddecl))
3043 /* Redeclaration of namespace alias, ignore it. */
3044 return 1;
8d08fdba
MS
3045 else
3046 {
8251199e
JM
3047 char *errmsg = redeclaration_error_message (newdecl, olddecl);
3048 if (errmsg)
8d08fdba 3049 {
8251199e 3050 cp_error (errmsg, newdecl);
8d08fdba
MS
3051 if (DECL_NAME (olddecl) != NULL_TREE)
3052 cp_error_at ((DECL_INITIAL (olddecl)
2c73f9f5 3053 && namespace_bindings_p ())
8251199e
JM
3054 ? "`%#D' previously defined here"
3055 : "`%#D' previously declared here", olddecl);
8d08fdba
MS
3056 }
3057 else if (TREE_CODE (olddecl) == FUNCTION_DECL
3058 && DECL_INITIAL (olddecl) != NULL_TREE
3059 && TYPE_ARG_TYPES (TREE_TYPE (olddecl)) == NULL_TREE
3060 && TYPE_ARG_TYPES (TREE_TYPE (newdecl)) != NULL_TREE)
3061 {
3062 /* Prototype decl follows defn w/o prototype. */
8251199e
JM
3063 cp_warning_at ("prototype for `%#D'", newdecl);
3064 cp_warning_at ("follows non-prototype definition here", olddecl);
8d08fdba
MS
3065 }
3066 else if (TREE_CODE (olddecl) == FUNCTION_DECL
3067 && DECL_LANGUAGE (newdecl) != DECL_LANGUAGE (olddecl))
8926095f
MS
3068 {
3069 /* extern "C" int foo ();
3070 int foo () { bar (); }
3071 is OK. */
3072 if (current_lang_stack == current_lang_base)
a28e3c7f 3073 DECL_LANGUAGE (newdecl) = DECL_LANGUAGE (olddecl);
8926095f
MS
3074 else
3075 {
8251199e 3076 cp_error_at ("previous declaration of `%#D' with %L linkage",
8926095f 3077 olddecl, DECL_LANGUAGE (olddecl));
8251199e 3078 cp_error ("conflicts with new declaration with %L linkage",
8926095f
MS
3079 DECL_LANGUAGE (newdecl));
3080 }
3081 }
e1cd6e56 3082
a6f02587 3083 if (DECL_LANG_SPECIFIC (olddecl) && DECL_USE_TEMPLATE (olddecl))
c32381b1
JM
3084 ;
3085 else if (TREE_CODE (olddecl) == FUNCTION_DECL)
e1cd6e56
MS
3086 {
3087 tree t1 = TYPE_ARG_TYPES (TREE_TYPE (olddecl));
3088 tree t2 = TYPE_ARG_TYPES (TREE_TYPE (newdecl));
3089 int i = 1;
3090
3091 if (TREE_CODE (TREE_TYPE (newdecl)) == METHOD_TYPE)
3092 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2);
3093
3094 for (; t1 && t1 != void_list_node;
3095 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2), i++)
3096 if (TREE_PURPOSE (t1) && TREE_PURPOSE (t2))
3097 {
2507f3b5
RK
3098 if (1 == simple_cst_equal (TREE_PURPOSE (t1),
3099 TREE_PURPOSE (t2)))
e1cd6e56
MS
3100 {
3101 if (pedantic)
3102 {
8251199e 3103 cp_pedwarn ("default argument given for parameter %d of `%#D'",
e1cd6e56 3104 i, newdecl);
8251199e 3105 cp_pedwarn_at ("after previous specification in `%#D'",
e1cd6e56
MS
3106 olddecl);
3107 }
3108 }
3109 else
3110 {
8251199e 3111 cp_error ("default argument given for parameter %d of `%#D'",
e1cd6e56 3112 i, newdecl);
8251199e 3113 cp_error_at ("after previous specification in `%#D'",
e1cd6e56
MS
3114 olddecl);
3115 }
3116 }
a5894242 3117
7fcdf4c2
MS
3118 if (DECL_THIS_INLINE (newdecl) && ! DECL_THIS_INLINE (olddecl)
3119 && TREE_ADDRESSABLE (olddecl) && warn_inline)
dff6b454 3120 {
8251199e 3121 cp_warning ("`%#D' was used before it was declared inline",
7fcdf4c2 3122 newdecl);
8251199e 3123 cp_warning_at ("previous non-inline declaration here",
7fcdf4c2 3124 olddecl);
dff6b454 3125 }
e1cd6e56 3126 }
8d08fdba
MS
3127 }
3128
3129 /* If new decl is `static' and an `extern' was seen previously,
3130 warn about it. */
3131 warn_extern_redeclared_static (newdecl, olddecl);
3132
e92cc029 3133 /* We have committed to returning 1 at this point. */
8d08fdba
MS
3134 if (TREE_CODE (newdecl) == FUNCTION_DECL)
3135 {
3136 /* Now that functions must hold information normally held
3137 by field decls, there is extra work to do so that
3138 declaration information does not get destroyed during
3139 definition. */
3140 if (DECL_VINDEX (olddecl))
3141 DECL_VINDEX (newdecl) = DECL_VINDEX (olddecl);
3142 if (DECL_CONTEXT (olddecl))
3143 DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
3144 if (DECL_CLASS_CONTEXT (olddecl))
3145 DECL_CLASS_CONTEXT (newdecl) = DECL_CLASS_CONTEXT (olddecl);
8d08fdba
MS
3146 if (DECL_PENDING_INLINE_INFO (newdecl) == (struct pending_inline *)0)
3147 DECL_PENDING_INLINE_INFO (newdecl) = DECL_PENDING_INLINE_INFO (olddecl);
e1cd6e56
MS
3148 DECL_STATIC_CONSTRUCTOR (newdecl) |= DECL_STATIC_CONSTRUCTOR (olddecl);
3149 DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
7215f9a0 3150 DECL_ABSTRACT_VIRTUAL_P (newdecl) |= DECL_ABSTRACT_VIRTUAL_P (olddecl);
864b83b9 3151 DECL_VIRTUAL_P (newdecl) |= DECL_VIRTUAL_P (olddecl);
4a67c9e9 3152 DECL_NEEDS_FINAL_OVERRIDER_P (newdecl) |= DECL_NEEDS_FINAL_OVERRIDER_P (olddecl);
0b60dfe3
BK
3153 new_defines_function = DECL_INITIAL (newdecl) != NULL_TREE;
3154
3155 /* Optionally warn about more than one declaration for the same
3156 name, but don't warn about a function declaration followed by a
3157 definition. */
3158 if (warn_redundant_decls && ! DECL_ARTIFICIAL (olddecl)
3159 && !(new_defines_function && DECL_INITIAL (olddecl) == NULL_TREE)
3160 /* Don't warn about extern decl followed by definition. */
3161 && !(DECL_EXTERNAL (olddecl) && ! DECL_EXTERNAL (newdecl))
3162 /* Don't warn about friends, let add_friend take care of it. */
3163 && ! DECL_FRIEND_P (newdecl))
3164 {
8251199e
JM
3165 cp_warning ("redundant redeclaration of `%D' in same scope", newdecl);
3166 cp_warning_at ("previous declaration of `%D'", olddecl);
0b60dfe3 3167 }
8d08fdba
MS
3168 }
3169
3170 /* Deal with C++: must preserve virtual function table size. */
3171 if (TREE_CODE (olddecl) == TYPE_DECL)
3172 {
3173 register tree newtype = TREE_TYPE (newdecl);
3174 register tree oldtype = TREE_TYPE (olddecl);
3175
3176 if (newtype != error_mark_node && oldtype != error_mark_node
3177 && TYPE_LANG_SPECIFIC (newtype) && TYPE_LANG_SPECIFIC (oldtype))
3178 {
3179 CLASSTYPE_VSIZE (newtype) = CLASSTYPE_VSIZE (oldtype);
3180 CLASSTYPE_FRIEND_CLASSES (newtype)
3181 = CLASSTYPE_FRIEND_CLASSES (oldtype);
3182 }
8d08fdba
MS
3183 }
3184
8d08fdba
MS
3185 /* Copy all the DECL_... slots specified in the new decl
3186 except for any that we copy here from the old type. */
0b60dfe3
BK
3187 DECL_MACHINE_ATTRIBUTES (newdecl)
3188 = merge_machine_decl_attributes (olddecl, newdecl);
8d08fdba 3189
5566b478
MS
3190 if (TREE_CODE (newdecl) == TEMPLATE_DECL)
3191 {
4d85e00e
MM
3192 if (! duplicate_decls (DECL_TEMPLATE_RESULT (newdecl),
3193 DECL_TEMPLATE_RESULT (olddecl)))
8251199e 3194 cp_error ("invalid redeclaration of %D", newdecl);
4d85e00e 3195 TREE_TYPE (olddecl) = TREE_TYPE (DECL_TEMPLATE_RESULT (olddecl));
4d85e00e
MM
3196 DECL_TEMPLATE_SPECIALIZATIONS (olddecl)
3197 = chainon (DECL_TEMPLATE_SPECIALIZATIONS (olddecl),
3198 DECL_TEMPLATE_SPECIALIZATIONS (newdecl));
0b60dfe3 3199
5566b478
MS
3200 return 1;
3201 }
0b60dfe3 3202
8d08fdba
MS
3203 if (types_match)
3204 {
3205 /* Automatically handles default parameters. */
3206 tree oldtype = TREE_TYPE (olddecl);
e1cd6e56 3207 tree newtype;
8d08fdba 3208
e1cd6e56
MS
3209 /* Make sure we put the new type in the same obstack as the old one. */
3210 if (oldtype)
39211cd5
MS
3211 push_obstacks (TYPE_OBSTACK (oldtype), TYPE_OBSTACK (oldtype));
3212 else
3213 {
3214 push_obstacks_nochange ();
3215 end_temporary_allocation ();
3216 }
3217
e1cd6e56
MS
3218 /* Merge the data types specified in the two decls. */
3219 newtype = common_type (TREE_TYPE (newdecl), TREE_TYPE (olddecl));
3220
8d08fdba
MS
3221 if (TREE_CODE (newdecl) == VAR_DECL)
3222 DECL_THIS_EXTERN (newdecl) |= DECL_THIS_EXTERN (olddecl);
3223 /* Do this after calling `common_type' so that default
3224 parameters don't confuse us. */
3225 else if (TREE_CODE (newdecl) == FUNCTION_DECL
3226 && (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (newdecl))
3227 != TYPE_RAISES_EXCEPTIONS (TREE_TYPE (olddecl))))
3228 {
f30432d7 3229 TREE_TYPE (newdecl) = build_exception_variant (newtype,
8d08fdba 3230 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (newdecl)));
f30432d7 3231 TREE_TYPE (olddecl) = build_exception_variant (newtype,
8d08fdba
MS
3232 TYPE_RAISES_EXCEPTIONS (oldtype));
3233
9a224b4a
JM
3234 if ((pedantic || ! DECL_IN_SYSTEM_HEADER (olddecl))
3235 && DECL_SOURCE_LINE (olddecl) != 0
da20811c 3236 && flag_exceptions
824b9a4c 3237 && ! compexcepttypes (TREE_TYPE (newdecl), TREE_TYPE (olddecl)))
8d08fdba 3238 {
8251199e 3239 cp_pedwarn ("declaration of `%D' throws different exceptions",
a28e3c7f 3240 newdecl);
8251199e 3241 cp_pedwarn_at ("previous declaration here", olddecl);
8d08fdba
MS
3242 }
3243 }
3244 TREE_TYPE (newdecl) = TREE_TYPE (olddecl) = newtype;
3245
3246 /* Lay the type out, unless already done. */
53929c47 3247 if (newtype != canonical_type_variant (oldtype)
5566b478 3248 && TREE_TYPE (newdecl) != error_mark_node
5156628f 3249 && !(processing_template_decl && uses_template_parms (newdecl)))
b7484fbe
MS
3250 layout_type (TREE_TYPE (newdecl));
3251
5566b478
MS
3252 if ((TREE_CODE (newdecl) == VAR_DECL
3253 || TREE_CODE (newdecl) == PARM_DECL
3254 || TREE_CODE (newdecl) == RESULT_DECL
3255 || TREE_CODE (newdecl) == FIELD_DECL
3256 || TREE_CODE (newdecl) == TYPE_DECL)
5156628f 3257 && !(processing_template_decl && uses_template_parms (newdecl)))
b7484fbe 3258 layout_decl (newdecl, 0);
8d08fdba
MS
3259
3260 /* Merge the type qualifiers. */
3261 if (TREE_READONLY (newdecl))
3262 TREE_READONLY (olddecl) = 1;
3263 if (TREE_THIS_VOLATILE (newdecl))
3264 TREE_THIS_VOLATILE (olddecl) = 1;
3265
3266 /* Merge the initialization information. */
8926095f
MS
3267 if (DECL_INITIAL (newdecl) == NULL_TREE
3268 && DECL_INITIAL (olddecl) != NULL_TREE)
3269 {
3270 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
3271 DECL_SOURCE_FILE (newdecl) = DECL_SOURCE_FILE (olddecl);
3272 DECL_SOURCE_LINE (newdecl) = DECL_SOURCE_LINE (olddecl);
4d85e00e
MM
3273 if (DECL_LANG_SPECIFIC (newdecl)
3274 && DECL_LANG_SPECIFIC (olddecl))
3275 DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
8926095f 3276 }
39211cd5
MS
3277
3278 /* Merge the section attribute.
3279 We want to issue an error if the sections conflict but that must be
3280 done later in decl_attributes since we are called before attributes
3281 are assigned. */
3282 if (DECL_SECTION_NAME (newdecl) == NULL_TREE)
3283 DECL_SECTION_NAME (newdecl) = DECL_SECTION_NAME (olddecl);
3284
8d08fdba
MS
3285 /* Keep the old rtl since we can safely use it, unless it's the
3286 call to abort() used for abstract virtuals. */
3287 if ((DECL_LANG_SPECIFIC (olddecl)
3288 && !DECL_ABSTRACT_VIRTUAL_P (olddecl))
3289 || DECL_RTL (olddecl) != DECL_RTL (abort_fndecl))
3290 DECL_RTL (newdecl) = DECL_RTL (olddecl);
39211cd5
MS
3291
3292 pop_obstacks ();
8d08fdba
MS
3293 }
3294 /* If cannot merge, then use the new type and qualifiers,
3295 and don't preserve the old rtl. */
3296 else
3297 {
3298 /* Clean out any memory we had of the old declaration. */
3299 tree oldstatic = value_member (olddecl, static_aggregates);
3300 if (oldstatic)
3301 TREE_VALUE (oldstatic) = error_mark_node;
3302
3303 TREE_TYPE (olddecl) = TREE_TYPE (newdecl);
3304 TREE_READONLY (olddecl) = TREE_READONLY (newdecl);
3305 TREE_THIS_VOLATILE (olddecl) = TREE_THIS_VOLATILE (newdecl);
3306 TREE_SIDE_EFFECTS (olddecl) = TREE_SIDE_EFFECTS (newdecl);
3307 }
3308
3309 /* Merge the storage class information. */
a9aedbc2 3310 DECL_WEAK (newdecl) |= DECL_WEAK (olddecl);
e92cc029 3311 DECL_ONE_ONLY (newdecl) |= DECL_ONE_ONLY (olddecl);
a9aedbc2
MS
3312 TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
3313 TREE_STATIC (olddecl) = TREE_STATIC (newdecl) |= TREE_STATIC (olddecl);
3314 if (! DECL_EXTERNAL (olddecl))
3315 DECL_EXTERNAL (newdecl) = 0;
0b60dfe3
BK
3316
3317 if (DECL_LANG_SPECIFIC (newdecl) && DECL_LANG_SPECIFIC (olddecl))
8d08fdba 3318 {
a9aedbc2
MS
3319 DECL_INTERFACE_KNOWN (newdecl) |= DECL_INTERFACE_KNOWN (olddecl);
3320 DECL_NOT_REALLY_EXTERN (newdecl) |= DECL_NOT_REALLY_EXTERN (olddecl);
77be6f82 3321 DECL_COMDAT (newdecl) |= DECL_COMDAT (olddecl);
0b60dfe3
BK
3322 /* Don't really know how much of the language-specific
3323 values we should copy from old to new. */
3324 DECL_IN_AGGR_P (newdecl) = DECL_IN_AGGR_P (olddecl);
3325 DECL_ACCESS (newdecl) = DECL_ACCESS (olddecl);
3326 DECL_NONCONVERTING_P (newdecl) = DECL_NONCONVERTING_P (olddecl);
fbf1c34b 3327 DECL_TEMPLATE_INFO (newdecl) = DECL_TEMPLATE_INFO (olddecl);
0b60dfe3 3328 olddecl_friend = DECL_FRIEND_P (olddecl);
8d08fdba
MS
3329 }
3330
8d08fdba
MS
3331 if (TREE_CODE (newdecl) == FUNCTION_DECL)
3332 {
75650646
MM
3333 if (DECL_TEMPLATE_INSTANTIATION (olddecl)
3334 && !DECL_TEMPLATE_INSTANTIATION (newdecl))
3335 {
3336 /* If newdecl is not a specialization, then it is not a
3337 template-related function at all. And that means that we
3338 shoud have exited above, returning 0. */
3339 my_friendly_assert (DECL_TEMPLATE_SPECIALIZATION (newdecl),
3340 0);
3341
3342 if (TREE_USED (olddecl))
3343 /* From [temp.expl.spec]:
3344
3345 If a template, a member template or the member of a class
3346 template is explicitly specialized then that
3347 specialization shall be declared before the first use of
3348 that specialization that would cause an implicit
3349 instantiation to take place, in every translation unit in
3350 which such a use occurs. */
8251199e 3351 cp_error ("explicit specialization of %D after first use",
75650646
MM
3352 olddecl);
3353
3354 SET_DECL_TEMPLATE_SPECIALIZATION (olddecl);
3355 }
faae18ab
MS
3356 DECL_THIS_INLINE (newdecl) |= DECL_THIS_INLINE (olddecl);
3357
3358 /* If either decl says `inline', this fn is inline, unless its
3359 definition was passed already. */
3360 if (DECL_INLINE (newdecl) && DECL_INITIAL (olddecl) == NULL_TREE)
3361 DECL_INLINE (olddecl) = 1;
3362 DECL_INLINE (newdecl) = DECL_INLINE (olddecl);
3363
700f8a87
MS
3364 if (! types_match)
3365 {
3366 DECL_LANGUAGE (olddecl) = DECL_LANGUAGE (newdecl);
3367 DECL_ASSEMBLER_NAME (olddecl) = DECL_ASSEMBLER_NAME (newdecl);
5566b478
MS
3368 DECL_RTL (olddecl) = DECL_RTL (newdecl);
3369 }
3370 if (! types_match || new_defines_function)
3371 {
3372 /* These need to be copied so that the names are available. */
700f8a87
MS
3373 DECL_ARGUMENTS (olddecl) = DECL_ARGUMENTS (newdecl);
3374 DECL_RESULT (olddecl) = DECL_RESULT (newdecl);
700f8a87 3375 }
8d08fdba
MS
3376 if (new_defines_function)
3377 /* If defining a function declared with other language
3378 linkage, use the previously declared language linkage. */
3379 DECL_LANGUAGE (newdecl) = DECL_LANGUAGE (olddecl);
3380 else
3381 {
3382 /* If redeclaring a builtin function, and not a definition,
3383 it stays built in. */
3384 if (DECL_BUILT_IN (olddecl))
3385 {
3386 DECL_BUILT_IN (newdecl) = 1;
39211cd5 3387 DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl);
8d08fdba
MS
3388 /* If we're keeping the built-in definition, keep the rtl,
3389 regardless of declaration matches. */
3390 DECL_RTL (newdecl) = DECL_RTL (olddecl);
3391 }
3392 else
3393 DECL_FRAME_SIZE (newdecl) = DECL_FRAME_SIZE (olddecl);
3394
3395 DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
8926095f 3396 if ((DECL_SAVED_INSNS (newdecl) = DECL_SAVED_INSNS (olddecl)))
8d08fdba
MS
3397 /* Previously saved insns go together with
3398 the function's previous definition. */
3399 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
3400 /* Don't clear out the arguments if we're redefining a function. */
3401 if (DECL_ARGUMENTS (olddecl))
3402 DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl);
3403 }
6060a796
MS
3404 if (DECL_LANG_SPECIFIC (olddecl))
3405 DECL_MAIN_VARIANT (newdecl) = DECL_MAIN_VARIANT (olddecl);
8d08fdba
MS
3406 }
3407
a9aedbc2
MS
3408 if (TREE_CODE (newdecl) == NAMESPACE_DECL)
3409 {
3410 NAMESPACE_LEVEL (newdecl) = NAMESPACE_LEVEL (olddecl);
3411 }
3412
8d08fdba
MS
3413 /* Now preserve various other info from the definition. */
3414 TREE_ADDRESSABLE (newdecl) = TREE_ADDRESSABLE (olddecl);
3415 TREE_ASM_WRITTEN (newdecl) = TREE_ASM_WRITTEN (olddecl);
3416 DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
f376e137 3417 DECL_ASSEMBLER_NAME (newdecl) = DECL_ASSEMBLER_NAME (olddecl);
8d08fdba 3418
8d08fdba
MS
3419 if (TREE_CODE (newdecl) == FUNCTION_DECL)
3420 {
3421 int function_size;
3422 struct lang_decl *ol = DECL_LANG_SPECIFIC (olddecl);
3423 struct lang_decl *nl = DECL_LANG_SPECIFIC (newdecl);
3424
3425 function_size = sizeof (struct tree_decl);
3426
3427 bcopy ((char *) newdecl + sizeof (struct tree_common),
3428 (char *) olddecl + sizeof (struct tree_common),
3429 function_size - sizeof (struct tree_common));
3430
3431 /* Can we safely free the storage used by newdecl? */
3432
3433#define ROUND(x) ((x + obstack_alignment_mask (&permanent_obstack)) \
3434 & ~ obstack_alignment_mask (&permanent_obstack))
3435
75650646
MM
3436 if (DECL_TEMPLATE_INSTANTIATION (newdecl))
3437 {
3438 /* If newdecl is a template instantiation, it is possible that
3439 the following sequence of events has occurred:
3440
3441 o A friend function was declared in a class template. The
3442 class template was instantiated.
3443
3444 o The instantiation of the friend declaration was
3445 recorded on the instantiation list, and is newdecl.
3446
3447 o Later, however, instantiate_class_template called pushdecl
3448 on the newdecl to perform name injection. But, pushdecl in
3449 turn called duplicate_decls when it discovered that another
3450 declaration of a global function with the same name already
3451 existed.
3452
3453 o Here, in duplicate_decls, we decided to clobber newdecl.
3454
3455 If we're going to do that, we'd better make sure that
3456 olddecl, and not newdecl, is on the list of
3457 instantiations so that if we try to do the instantiation
3458 again we won't get the clobbered declaration. */
3459
3460 tree tmpl = DECL_TI_TEMPLATE (newdecl);
3461 tree decls = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
3462
3463 for (; decls; decls = TREE_CHAIN (decls))
3464 if (TREE_VALUE (decls) == newdecl)
3465 TREE_VALUE (decls) = olddecl;
3466 }
3467
3a56f0ab
JM
3468 if (((char *)newdecl + ROUND (function_size) == (char *)nl
3469 && ((char *)newdecl + ROUND (function_size)
3470 + ROUND (sizeof (struct lang_decl))
3471 == obstack_next_free (&permanent_obstack)))
3472 || ((char *)newdecl + ROUND (function_size)
3473 == obstack_next_free (&permanent_obstack)))
8d08fdba
MS
3474 {
3475 DECL_MAIN_VARIANT (newdecl) = olddecl;
3476 DECL_LANG_SPECIFIC (olddecl) = ol;
3477 bcopy ((char *)nl, (char *)ol, sizeof (struct lang_decl));
3478
3479 obstack_free (&permanent_obstack, newdecl);
3480 }
d22c8596 3481 else if (LANG_DECL_PERMANENT (ol) && ol != nl)
8d08fdba
MS
3482 {
3483 if (DECL_MAIN_VARIANT (olddecl) == olddecl)
3484 {
3485 /* Save these lang_decls that would otherwise be lost. */
3486 extern tree free_lang_decl_chain;
3487 tree free_lang_decl = (tree) ol;
d22c8596
MS
3488
3489 if (DECL_LANG_SPECIFIC (olddecl) == ol)
3490 abort ();
3491
8d08fdba
MS
3492 TREE_CHAIN (free_lang_decl) = free_lang_decl_chain;
3493 free_lang_decl_chain = free_lang_decl;
3494 }
3495 else
3496 {
bd6dd845 3497 /* Storage leak. */;
8d08fdba
MS
3498 }
3499 }
3500 }
3501 else
3502 {
3503 bcopy ((char *) newdecl + sizeof (struct tree_common),
3504 (char *) olddecl + sizeof (struct tree_common),
3505 sizeof (struct tree_decl) - sizeof (struct tree_common)
3506 + tree_code_length [(int)TREE_CODE (newdecl)] * sizeof (char *));
3507 }
3508
3509 DECL_UID (olddecl) = olddecl_uid;
3510 if (olddecl_friend)
3511 DECL_FRIEND_P (olddecl) = 1;
3512
d9525bec
BK
3513 /* NEWDECL contains the merged attribute lists.
3514 Update OLDDECL to be the same. */
3515 DECL_MACHINE_ATTRIBUTES (olddecl) = DECL_MACHINE_ATTRIBUTES (newdecl);
3516
8d08fdba
MS
3517 return 1;
3518}
3519
3520/* Record a decl-node X as belonging to the current lexical scope.
3521 Check for errors (such as an incompatible declaration for the same
3522 name already seen in the same scope).
3523
3524 Returns either X or an old decl for the same name.
3525 If an old decl is returned, it may have been smashed
3526 to agree with what X says. */
3527
3528tree
3529pushdecl (x)
3530 tree x;
3531{
3532 register tree t;
8d08fdba 3533 register tree name = DECL_ASSEMBLER_NAME (x);
f181d4ae 3534 int need_new_binding = 1;
8d08fdba 3535
50714e79
MM
3536 if (DECL_TEMPLATE_PARM_P (x))
3537 /* Template parameters have no context; they are not X::T even
3538 when declared within a class or namespace. */
3539 ;
3540 else
3541 {
3542 if (current_function_decl && x != current_function_decl
3543 /* A local declaration for a function doesn't constitute
3544 nesting. */
3545 && (TREE_CODE (x) != FUNCTION_DECL || DECL_INITIAL (x))
3546 /* Don't change DECL_CONTEXT of virtual methods. */
3547 && (TREE_CODE (x) != FUNCTION_DECL || !DECL_VIRTUAL_P (x))
3548 && !DECL_CONTEXT (x))
3549 DECL_CONTEXT (x) = current_function_decl;
3550 if (!DECL_CONTEXT (x))
3551 DECL_CONTEXT (x) = FROB_CONTEXT (current_namespace);
3552 }
8d08fdba 3553
8d08fdba 3554 /* Type are looked up using the DECL_NAME, as that is what the rest of the
e92cc029 3555 compiler wants to use. */
a9aedbc2 3556 if (TREE_CODE (x) == TYPE_DECL || TREE_CODE (x) == VAR_DECL
73b0fce8
KL
3557 || TREE_CODE (x) == NAMESPACE_DECL || TREE_CODE (x) == TEMPLATE_TYPE_PARM
3558 || TREE_CODE (x) == TEMPLATE_TEMPLATE_PARM)
8d08fdba 3559 name = DECL_NAME (x);
8d08fdba
MS
3560
3561 if (name)
3562 {
5566b478
MS
3563#if 0
3564 /* Not needed...see below. */
8d08fdba
MS
3565 char *file;
3566 int line;
5566b478 3567#endif
386b8a85
JM
3568 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
3569 name = TREE_OPERAND (name, 0);
3570
2c73f9f5 3571 /* Namespace-scoped variables are not found in the current level. */
6eb3bb27 3572 if (TREE_CODE (x) == VAR_DECL && DECL_NAMESPACE_SCOPE_P (x))
2c73f9f5
ML
3573 t = namespace_binding (name, DECL_CONTEXT (x));
3574 else
3575 t = lookup_name_current_level (name);
8d08fdba
MS
3576 if (t == error_mark_node)
3577 {
3578 /* error_mark_node is 0 for a while during initialization! */
3579 t = NULL_TREE;
8251199e 3580 cp_error_at ("`%#D' used prior to declaration", x);
8d08fdba
MS
3581 }
3582
51c184be 3583 else if (t != NULL_TREE)
8d08fdba 3584 {
5566b478
MS
3585#if 0
3586 /* This is turned off until I have time to do it right (bpk). */
e92cc029 3587 /* With the code below that uses it... */
8d6e462b
PB
3588 file = DECL_SOURCE_FILE (t);
3589 line = DECL_SOURCE_LINE (t);
5566b478 3590#endif
2ee887f2 3591 if (TREE_CODE (t) == PARM_DECL)
8d08fdba
MS
3592 {
3593 if (DECL_CONTEXT (t) == NULL_TREE)
3594 fatal ("parse errors have confused me too much");
be99da77 3595
e92cc029 3596 /* Check for duplicate params. */
be99da77
MS
3597 if (duplicate_decls (x, t))
3598 return t;
8d08fdba 3599 }
8d6e462b 3600 else if (((TREE_CODE (x) == FUNCTION_DECL && DECL_LANGUAGE (x) == lang_c)
5566b478
MS
3601 || DECL_FUNCTION_TEMPLATE_P (x))
3602 && is_overloaded_fn (t))
2c73f9f5 3603 /* Don't do anything just yet. */;
e1cd6e56
MS
3604 else if (t == wchar_decl_node)
3605 {
3606 if (pedantic && ! DECL_IN_SYSTEM_HEADER (x))
8251199e 3607 cp_pedwarn ("redeclaration of wchar_t as `%T'", TREE_TYPE (x));
e1cd6e56
MS
3608
3609 /* Throw away the redeclaration. */
3610 return t;
3611 }
8926095f 3612 else if (TREE_CODE (t) != TREE_CODE (x))
8d08fdba 3613 {
9ed182dc 3614 if (duplicate_decls (x, t))
51c184be 3615 return t;
8d08fdba
MS
3616 }
3617 else if (duplicate_decls (x, t))
51c184be 3618 {
8d08fdba 3619#if 0
8926095f 3620 /* This is turned off until I have time to do it right (bpk). */
8d08fdba 3621
8926095f
MS
3622 /* Also warn if they did a prototype with `static' on it, but
3623 then later left the `static' off. */
3624 if (! TREE_PUBLIC (name) && TREE_PUBLIC (x))
8d08fdba 3625 {
8926095f
MS
3626 if (DECL_LANG_SPECIFIC (t) && DECL_FRIEND_P (t))
3627 return t;
8d08fdba 3628
8926095f
MS
3629 if (extra_warnings)
3630 {
8251199e 3631 cp_warning ("`static' missing from declaration of `%D'",
a28e3c7f 3632 t);
8926095f
MS
3633 warning_with_file_and_line (file, line,
3634 "previous declaration of `%s'",
3635 decl_as_string (t, 0));
3636 }
8d08fdba 3637
8926095f
MS
3638 /* Now fix things so it'll do what they expect. */
3639 if (current_function_decl)
3640 TREE_PUBLIC (current_function_decl) = 0;
3641 }
51c184be
MS
3642 /* Due to interference in memory reclamation (X may be
3643 obstack-deallocated at this point), we must guard against
8926095f
MS
3644 one really special case. [jason: This should be handled
3645 by start_function] */
51c184be
MS
3646 if (current_function_decl == x)
3647 current_function_decl = t;
8926095f 3648#endif
7177d104
MS
3649 if (TREE_CODE (t) == TYPE_DECL)
3650 SET_IDENTIFIER_TYPE_VALUE (name, TREE_TYPE (t));
f30432d7
MS
3651 else if (TREE_CODE (t) == FUNCTION_DECL)
3652 check_default_args (t);
7177d104 3653
51c184be
MS
3654 return t;
3655 }
35680744
MM
3656 else if (DECL_MAIN_P (x))
3657 {
3658 /* A redeclaration of main, but not a duplicate of the
3659 previous one.
3660
3661 [basic.start.main]
3662
3663 This function shall not be overloaded. */
8251199e
JM
3664 cp_error_at ("invalid redeclaration of `%D'", t);
3665 cp_error ("as `%D'", x);
35680744
MM
3666 /* We don't try to push this declaration since that
3667 causes a crash. */
3668 return x;
3669 }
8d08fdba 3670 }
8926095f 3671
f3400fe2
JM
3672 check_template_shadow (x);
3673
fcfcdfc8
JM
3674 /* If this is a function conjured up by the backend, massage it
3675 so it looks friendly. */
3676 if (TREE_CODE (x) == FUNCTION_DECL
3677 && ! DECL_LANG_SPECIFIC (x))
3678 {
3679 retrofit_lang_decl (x);
3680 DECL_LANGUAGE (x) = lang_c;
3681 }
3682
8926095f
MS
3683 if (TREE_CODE (x) == FUNCTION_DECL && ! DECL_FUNCTION_MEMBER_P (x))
3684 {
7bdbfa05 3685 t = push_overloaded_decl (x, PUSH_LOCAL);
8926095f
MS
3686 if (t != x || DECL_LANGUAGE (x) == lang_c)
3687 return t;
f181d4ae
MM
3688 if (!namespace_bindings_p ())
3689 /* We do not need to create a binding for this name;
3690 push_overloaded_decl will have already done so if
3691 necessary. */
3692 need_new_binding = 0;
8926095f 3693 }
6eb3bb27 3694 else if (DECL_FUNCTION_TEMPLATE_P (x) && DECL_NAMESPACE_SCOPE_P (x))
7bdbfa05 3695 return push_overloaded_decl (x, PUSH_GLOBAL);
8d08fdba 3696
a1774733
BK
3697 /* If declaring a type as a typedef, copy the type (unless we're
3698 at line 0), and install this TYPE_DECL as the new type's typedef
3699 name. See the extensive comment in ../c-decl.c (pushdecl). */
8d08fdba
MS
3700 if (TREE_CODE (x) == TYPE_DECL)
3701 {
3702 tree type = TREE_TYPE (x);
a1774733
BK
3703 if (DECL_SOURCE_LINE (x) == 0)
3704 {
3705 if (TYPE_NAME (type) == 0)
3706 TYPE_NAME (type) = x;
3707 }
1c80fb65
MM
3708 else if (type != error_mark_node && TYPE_NAME (type) != x
3709 /* We don't want to copy the type when all we're
3710 doing is making a TYPE_DECL for the purposes of
3711 inlining. */
3712 && (!TYPE_NAME (type)
3713 || TYPE_NAME (type) != DECL_ABSTRACT_ORIGIN (x)))
a1774733 3714 {
ae0a6181
MM
3715 push_obstacks (TYPE_OBSTACK (type), TYPE_OBSTACK (type));
3716
a1774733
BK
3717 DECL_ORIGINAL_TYPE (x) = type;
3718 type = build_type_copy (type);
3719 TYPE_STUB_DECL (type) = TYPE_STUB_DECL (DECL_ORIGINAL_TYPE (x));
3720 TYPE_NAME (type) = x;
3721 TREE_TYPE (x) = type;
ae0a6181
MM
3722
3723 pop_obstacks ();
a1774733 3724 }
8d08fdba 3725
8d08fdba
MS
3726 if (type != error_mark_node
3727 && TYPE_NAME (type)
3728 && TYPE_IDENTIFIER (type))
f181d4ae
MM
3729 set_identifier_type_value_with_scope (DECL_NAME (x), type,
3730 current_binding_level);
3731
8d08fdba
MS
3732 }
3733
3734 /* Multiple external decls of the same identifier ought to match.
3735
3736 We get warnings about inline functions where they are defined.
39211cd5
MS
3737 We get warnings about other functions from push_overloaded_decl.
3738
8d08fdba 3739 Avoid duplicate warnings where they are used. */
39211cd5 3740 if (TREE_PUBLIC (x) && TREE_CODE (x) != FUNCTION_DECL)
8d08fdba
MS
3741 {
3742 tree decl;
3743
31928556
JM
3744 if (IDENTIFIER_NAMESPACE_VALUE (name) != NULL_TREE
3745 && (DECL_EXTERNAL (IDENTIFIER_NAMESPACE_VALUE (name))
3746 || TREE_PUBLIC (IDENTIFIER_NAMESPACE_VALUE (name))))
3747 decl = IDENTIFIER_NAMESPACE_VALUE (name);
8d08fdba
MS
3748 else
3749 decl = NULL_TREE;
3750
39211cd5 3751 if (decl
8d08fdba
MS
3752 /* If different sort of thing, we already gave an error. */
3753 && TREE_CODE (decl) == TREE_CODE (x)
3bfdc719 3754 && !same_type_p (TREE_TYPE (x), TREE_TYPE (decl)))
8d08fdba 3755 {
8251199e
JM
3756 cp_pedwarn ("type mismatch with previous external decl", x);
3757 cp_pedwarn_at ("previous external decl of `%#D'", decl);
8d08fdba
MS
3758 }
3759 }
3760
8d08fdba
MS
3761 /* This name is new in its binding level.
3762 Install the new declaration and return it. */
2c73f9f5 3763 if (namespace_bindings_p ())
8d08fdba
MS
3764 {
3765 /* Install a global value. */
3766
8d08fdba
MS
3767 /* If the first global decl has external linkage,
3768 warn if we later see static one. */
31928556 3769 if (IDENTIFIER_GLOBAL_VALUE (name) == NULL_TREE && TREE_PUBLIC (x))
8d08fdba
MS
3770 TREE_PUBLIC (name) = 1;
3771
d8f8dca1
MM
3772 if (!(TREE_CODE (x) == TYPE_DECL && DECL_ARTIFICIAL (x)
3773 && t != NULL_TREE))
30394414
JM
3774 {
3775 if (TREE_CODE (x) == FUNCTION_DECL)
31928556
JM
3776 my_friendly_assert
3777 ((IDENTIFIER_GLOBAL_VALUE (name) == NULL_TREE)
3778 || (IDENTIFIER_GLOBAL_VALUE (name) == x), 378);
3779 SET_IDENTIFIER_NAMESPACE_VALUE (name, x);
30394414 3780 }
8d08fdba
MS
3781
3782 /* Don't forget if the function was used via an implicit decl. */
3783 if (IDENTIFIER_IMPLICIT_DECL (name)
3784 && TREE_USED (IDENTIFIER_IMPLICIT_DECL (name)))
3785 TREE_USED (x) = 1;
3786
3787 /* Don't forget if its address was taken in that way. */
3788 if (IDENTIFIER_IMPLICIT_DECL (name)
3789 && TREE_ADDRESSABLE (IDENTIFIER_IMPLICIT_DECL (name)))
3790 TREE_ADDRESSABLE (x) = 1;
3791
3792 /* Warn about mismatches against previous implicit decl. */
3793 if (IDENTIFIER_IMPLICIT_DECL (name) != NULL_TREE
3794 /* If this real decl matches the implicit, don't complain. */
3795 && ! (TREE_CODE (x) == FUNCTION_DECL
3796 && TREE_TYPE (TREE_TYPE (x)) == integer_type_node))
8251199e
JM
3797 cp_warning
3798 ("`%D' was previously implicitly declared to return `int'", x);
8d08fdba
MS
3799
3800 /* If new decl is `static' and an `extern' was seen previously,
3801 warn about it. */
a0a33927
MS
3802 if (x != NULL_TREE && t != NULL_TREE && decls_match (x, t))
3803 warn_extern_redeclared_static (x, t);
8d08fdba
MS
3804 }
3805 else
3806 {
3807 /* Here to install a non-global value. */
f181d4ae 3808 tree oldlocal = IDENTIFIER_VALUE (name);
31928556 3809 tree oldglobal = IDENTIFIER_NAMESPACE_VALUE (name);
8d08fdba 3810
f181d4ae
MM
3811 if (need_new_binding)
3812 {
0034cf72 3813 push_local_binding (name, x, 0);
f181d4ae
MM
3814 /* Because push_local_binding will hook X on to the
3815 current_binding_level's name list, we don't want to
3816 do that again below. */
3817 need_new_binding = 0;
3818 }
8d08fdba
MS
3819
3820 /* If this is a TYPE_DECL, push it into the type value slot. */
3821 if (TREE_CODE (x) == TYPE_DECL)
f181d4ae
MM
3822 set_identifier_type_value_with_scope (name, TREE_TYPE (x),
3823 current_binding_level);
8d08fdba 3824
a9aedbc2
MS
3825 /* Clear out any TYPE_DECL shadowed by a namespace so that
3826 we won't think this is a type. The C struct hack doesn't
3827 go through namespaces. */
3828 if (TREE_CODE (x) == NAMESPACE_DECL)
f181d4ae
MM
3829 set_identifier_type_value_with_scope (name, NULL_TREE,
3830 current_binding_level);
a9aedbc2 3831
8d08fdba
MS
3832 /* If this is an extern function declaration, see if we
3833 have a global definition or declaration for the function. */
3834 if (oldlocal == NULL_TREE
faae18ab 3835 && DECL_EXTERNAL (x)
31928556 3836 && oldglobal != NULL_TREE
8d08fdba 3837 && TREE_CODE (x) == FUNCTION_DECL
31928556 3838 && TREE_CODE (oldglobal) == FUNCTION_DECL)
8d08fdba
MS
3839 {
3840 /* We have one. Their types must agree. */
31928556 3841 if (decls_match (x, oldglobal))
6060a796
MS
3842 /* OK */;
3843 else
8d08fdba 3844 {
8251199e
JM
3845 cp_warning ("extern declaration of `%#D' doesn't match", x);
3846 cp_warning_at ("global declaration `%#D'", oldglobal);
8d08fdba 3847 }
8d08fdba
MS
3848 }
3849 /* If we have a local external declaration,
3850 and no file-scope declaration has yet been seen,
3851 then if we later have a file-scope decl it must not be static. */
3852 if (oldlocal == NULL_TREE
31928556 3853 && oldglobal == NULL_TREE
8d08fdba
MS
3854 && DECL_EXTERNAL (x)
3855 && TREE_PUBLIC (x))
f181d4ae 3856 TREE_PUBLIC (name) = 1;
8d08fdba
MS
3857
3858 if (DECL_FROM_INLINE (x))
3859 /* Inline decls shadow nothing. */;
3860
3861 /* Warn if shadowing an argument at the top level of the body. */
3862 else if (oldlocal != NULL_TREE && !DECL_EXTERNAL (x)
3863 && TREE_CODE (oldlocal) == PARM_DECL
3864 && TREE_CODE (x) != PARM_DECL)
3865 {
3866 /* Go to where the parms should be and see if we
3867 find them there. */
3868 struct binding_level *b = current_binding_level->level_chain;
3869
3870 if (cleanup_label)
3871 b = b->level_chain;
3872
3873 /* ARM $8.3 */
3874 if (b->parm_flag == 1)
8251199e 3875 cp_error ("declaration of `%#D' shadows a parameter", name);
8d08fdba 3876 }
f181d4ae
MM
3877 else if (warn_shadow && oldlocal != NULL_TREE
3878 && current_binding_level->is_for_scope
8d6e462b
PB
3879 && !DECL_DEAD_FOR_LOCAL (oldlocal))
3880 {
8251199e 3881 warning ("variable `%s' shadows local",
8d6e462b 3882 IDENTIFIER_POINTER (name));
8251199e 3883 cp_warning_at (" this is the shadowed declaration", oldlocal);
8d6e462b 3884 }
8d08fdba
MS
3885 /* Maybe warn if shadowing something else. */
3886 else if (warn_shadow && !DECL_EXTERNAL (x)
3887 /* No shadow warnings for internally generated vars. */
700f8a87 3888 && ! DECL_ARTIFICIAL (x)
8d08fdba
MS
3889 /* No shadow warnings for vars made for inlining. */
3890 && ! DECL_FROM_INLINE (x))
3891 {
3892 char *warnstring = NULL;
3893
3894 if (oldlocal != NULL_TREE && TREE_CODE (oldlocal) == PARM_DECL)
3895 warnstring = "declaration of `%s' shadows a parameter";
3896 else if (IDENTIFIER_CLASS_VALUE (name) != NULL_TREE
4ac14744 3897 && current_class_ptr
8d08fdba
MS
3898 && !TREE_STATIC (name))
3899 warnstring = "declaration of `%s' shadows a member of `this'";
3900 else if (oldlocal != NULL_TREE)
3901 warnstring = "declaration of `%s' shadows previous local";
31928556 3902 else if (oldglobal != NULL_TREE)
30394414 3903 /* XXX shadow warnings in outer-more namespaces */
8d08fdba
MS
3904 warnstring = "declaration of `%s' shadows global declaration";
3905
3906 if (warnstring)
3907 warning (warnstring, IDENTIFIER_POINTER (name));
3908 }
e1cd6e56 3909 }
8d08fdba 3910
e1cd6e56 3911 if (TREE_CODE (x) == FUNCTION_DECL)
f30432d7 3912 check_default_args (x);
8145f082
MS
3913
3914 /* Keep count of variables in this level with incomplete type. */
8145f082 3915 if (TREE_CODE (x) == VAR_DECL
28cbf42c 3916 && TREE_TYPE (x) != error_mark_node
f30432d7
MS
3917 && ((TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
3918 && PROMOTES_TO_AGGR_TYPE (TREE_TYPE (x), ARRAY_TYPE))
3919 /* RTTI TD entries are created while defining the type_info. */
3920 || (TYPE_LANG_SPECIFIC (TREE_TYPE (x))
3921 && TYPE_BEING_DEFINED (TREE_TYPE (x)))))
f181d4ae
MM
3922 current_binding_level->incomplete
3923 = tree_cons (NULL_TREE, x, current_binding_level->incomplete);
8d08fdba
MS
3924 }
3925
f181d4ae
MM
3926 if (need_new_binding)
3927 {
3928 /* Put decls on list in reverse order.
3929 We will reverse them later if necessary. */
3930 TREE_CHAIN (x) = current_binding_level->names;
3931 current_binding_level->names = x;
3932 if (! (current_binding_level != global_binding_level
3933 || TREE_PERMANENT (x)))
3934 my_friendly_abort (124);
3935 }
8d08fdba
MS
3936
3937 return x;
3938}
3939
5566b478
MS
3940/* Same as pushdecl, but define X in binding-level LEVEL. We rely on the
3941 caller to set DECL_CONTEXT properly. */
8d08fdba
MS
3942
3943static tree
3944pushdecl_with_scope (x, level)
3945 tree x;
3946 struct binding_level *level;
3947{
8d019cef 3948 register struct binding_level *b;
5566b478 3949 tree function_decl = current_function_decl;
8d08fdba 3950
5566b478 3951 current_function_decl = NULL_TREE;
8d019cef
JM
3952 if (level->parm_flag == 2)
3953 {
3954 b = class_binding_level;
3955 class_binding_level = level;
3956 pushdecl_class_level (x);
3957 class_binding_level = b;
3958 }
3959 else
3960 {
3961 b = current_binding_level;
3962 current_binding_level = level;
3963 x = pushdecl (x);
3964 current_binding_level = b;
3965 }
5566b478 3966 current_function_decl = function_decl;
8d08fdba
MS
3967 return x;
3968}
3969
2c73f9f5 3970/* Like pushdecl, only it places X in the current namespace,
8d08fdba 3971 if appropriate. */
e92cc029 3972
8d08fdba 3973tree
2c73f9f5 3974pushdecl_namespace_level (x)
8d08fdba
MS
3975 tree x;
3976{
3977 register struct binding_level *b = inner_binding_level;
2c73f9f5
ML
3978 register tree t;
3979
3980 t = pushdecl_with_scope (x, NAMESPACE_LEVEL (current_namespace));
8d08fdba
MS
3981
3982 /* Now, the type_shadowed stack may screw us. Munge it so it does
3983 what we want. */
3984 if (TREE_CODE (x) == TYPE_DECL)
3985 {
3986 tree name = DECL_NAME (x);
3987 tree newval;
3988 tree *ptr = (tree *)0;
3989 for (; b != global_binding_level; b = b->level_chain)
3990 {
3991 tree shadowed = b->type_shadowed;
3992 for (; shadowed; shadowed = TREE_CHAIN (shadowed))
3993 if (TREE_PURPOSE (shadowed) == name)
3994 {
3995 ptr = &TREE_VALUE (shadowed);
3996 /* Can't break out of the loop here because sometimes
3997 a binding level will have duplicate bindings for
3998 PT names. It's gross, but I haven't time to fix it. */
3999 }
4000 }
4001 newval = TREE_TYPE (x);
4002 if (ptr == (tree *)0)
4003 {
4004 /* @@ This shouldn't be needed. My test case "zstring.cc" trips
4005 up here if this is changed to an assertion. --KR */
4006 SET_IDENTIFIER_TYPE_VALUE (name, newval);
4007 }
4008 else
4009 {
8d08fdba
MS
4010 *ptr = newval;
4011 }
4012 }
4013 return t;
4014}
4015
2c73f9f5
ML
4016/* Like pushdecl, only it places X in GLOBAL_BINDING_LEVEL,
4017 if appropriate. */
4018
4019tree
4020pushdecl_top_level (x)
4021 tree x;
4022{
4023 tree cur_namespace = current_namespace;
4024 current_namespace = global_namespace;
4025 x = pushdecl_namespace_level (x);
4026 current_namespace = cur_namespace;
4027 return x;
4028}
4029
8d08fdba 4030/* Make the declaration of X appear in CLASS scope. */
e92cc029 4031
61a127b3 4032void
8d08fdba
MS
4033pushdecl_class_level (x)
4034 tree x;
4035{
4036 /* Don't use DECL_ASSEMBLER_NAME here! Everything that looks in class
4037 scope looks for the pre-mangled name. */
4038 register tree name = DECL_NAME (x);
4039
4040 if (name)
4041 {
8d2733ca
MS
4042 if (TYPE_BEING_DEFINED (current_class_type))
4043 {
57f01f45
JM
4044 /* A name N used in a class S shall refer to the same declaration
4045 in its context and when re-evaluated in the completed scope of S.
8d2733ca
MS
4046 Types, enums, and static vars are checked here; other
4047 members are checked in finish_struct. */
4048 tree icv = IDENTIFIER_CLASS_VALUE (name);
4049
4d7614e9 4050 /* This should match check_member_decl_is_same_in_complete_scope. */
5566b478 4051 if (icv && icv != x
dc8263bc 4052 && flag_optional_diags
a4443a08
MS
4053 /* Don't complain about inherited names. */
4054 && id_in_current_class (name)
4055 /* Or shadowed tags. */
35acd3f2 4056 && !(DECL_DECLARES_TYPE_P (icv)
a4443a08 4057 && DECL_CONTEXT (icv) == current_class_type))
8d2733ca 4058 {
8251199e
JM
4059 cp_pedwarn ("declaration of identifier `%D' as `%#D'", name, x);
4060 cp_pedwarn_at ("conflicts with previous use in class as `%#D'",
57f01f45 4061 icv);
8d2733ca 4062 }
a0d9f322 4063
f3400fe2 4064 check_template_shadow (x);
8d2733ca
MS
4065 }
4066
8d08fdba
MS
4067 push_class_level_binding (name, x);
4068 if (TREE_CODE (x) == TYPE_DECL)
f181d4ae 4069 set_identifier_type_value (name, TREE_TYPE (x));
8d08fdba 4070 }
8d08fdba
MS
4071}
4072
bd6dd845 4073#if 0
7177d104
MS
4074/* This function is used to push the mangled decls for nested types into
4075 the appropriate scope. Previously pushdecl_top_level was used, but that
4076 is incorrect for members of local classes. */
e92cc029 4077
5566b478 4078void
7177d104
MS
4079pushdecl_nonclass_level (x)
4080 tree x;
4081{
4082 struct binding_level *b = current_binding_level;
4083
7177d104 4084 my_friendly_assert (b->parm_flag != 2, 180);
7177d104 4085
5566b478 4086#if 0
7177d104
MS
4087 /* Get out of template binding levels */
4088 while (b->pseudo_global)
4089 b = b->level_chain;
5566b478 4090#endif
7177d104
MS
4091
4092 pushdecl_with_scope (x, b);
4093}
bd6dd845 4094#endif
7177d104 4095
8d08fdba
MS
4096/* Make the declaration(s) of X appear in CLASS scope
4097 under the name NAME. */
e92cc029 4098
8d08fdba
MS
4099void
4100push_class_level_binding (name, x)
4101 tree name;
4102 tree x;
4103{
98c1c668
JM
4104 /* The class_binding_level will be NULL if x is a template
4105 parameter name in a member template. */
4106 if (!class_binding_level)
4107 return;
4108
f181d4ae
MM
4109 /* If this declaration shadows a declaration from an enclosing
4110 class, then we will need to restore IDENTIFIER_CLASS_VALUE when
4111 we leave this class. Record the shadowed declaration here. */
8d08fdba
MS
4112 maybe_push_cache_obstack ();
4113 class_binding_level->class_shadowed
d8f8dca1
MM
4114 = tree_cons (name, IDENTIFIER_CLASS_VALUE (name),
4115 class_binding_level->class_shadowed);
4116 TREE_TYPE (class_binding_level->class_shadowed)
4117 = x;
8d08fdba 4118 pop_obstacks ();
f181d4ae
MM
4119
4120 /* Put the binding on the stack of bindings for the identifier, and
4121 update IDENTIFIER_CLASS_VALUE. */
4122 push_class_binding (name, x);
f181d4ae 4123
8d08fdba
MS
4124 obstack_ptr_grow (&decl_obstack, x);
4125}
4126
2c73f9f5
ML
4127/* Insert another USING_DECL into the current binding level,
4128 returning this declaration. If this is a redeclaration,
4129 do nothing and return NULL_TREE. */
e92cc029 4130
2c73f9f5
ML
4131tree
4132push_using_decl (scope, name)
4133 tree scope;
4134 tree name;
8d08fdba 4135{
2c73f9f5
ML
4136 tree decl;
4137
2c73f9f5
ML
4138 my_friendly_assert (TREE_CODE (scope) == NAMESPACE_DECL, 383);
4139 my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, 384);
4140 for (decl = current_binding_level->usings; decl; decl = TREE_CHAIN (decl))
4141 if (DECL_INITIAL (decl) == scope && DECL_NAME (decl) == name)
4142 break;
4143 if (decl)
4144 return NULL_TREE;
4145 decl = build_lang_decl (USING_DECL, name, void_type_node);
4146 DECL_INITIAL (decl) = scope;
4147 TREE_CHAIN (decl) = current_binding_level->usings;
4148 current_binding_level->usings = decl;
4149 return decl;
8d08fdba
MS
4150}
4151
ea9635c7
ML
4152/* Add namespace to using_directives. Return NULL_TREE if nothing was
4153 changed (i.e. there was already a directive), or the fresh
4154 TREE_LIST otherwise. */
4155
4156tree
9ed182dc 4157push_using_directive (used)
ea9635c7 4158 tree used;
ea9635c7
ML
4159{
4160 tree ud = current_binding_level->using_directives;
9ed182dc 4161 tree iter, ancestor;
ea9635c7
ML
4162
4163 /* Check if we already have this. */
4164 if (purpose_member (used, ud) != NULL_TREE)
4165 return NULL_TREE;
00dc6358
JM
4166
4167 /* Recursively add all namespaces used. */
4168 for (iter = DECL_NAMESPACE_USING (used); iter; iter = TREE_CHAIN (iter))
9ed182dc 4169 push_using_directive (TREE_PURPOSE (iter));
00dc6358 4170
9ed182dc 4171 ancestor = namespace_ancestor (current_decl_namespace (), used);
00dc6358 4172 ud = current_binding_level->using_directives;
ea9635c7
ML
4173 ud = perm_tree_cons (used, ancestor, ud);
4174 current_binding_level->using_directives = ud;
4175 return ud;
4176}
4177
f181d4ae
MM
4178/* DECL is a FUNCTION_DECL for a non-member function, which may have
4179 other definitions already in place. We get around this by making
4180 the value of the identifier point to a list of all the things that
4181 want to be referenced by that name. It is then up to the users of
4182 that name to decide what to do with that list.
8d08fdba
MS
4183
4184 DECL may also be a TEMPLATE_DECL, with a FUNCTION_DECL in its DECL_RESULT
4185 slot. It is dealt with the same way.
4186
7bdbfa05
MM
4187 FLAGS is a bitwise-or of the following values:
4188 PUSH_LOCAL: Bind DECL in the current scope, rather than at
4189 namespace scope.
4190 PUSH_USING: DECL is being pushed as the result of a using
4191 declaration.
4192
8d08fdba
MS
4193 The value returned may be a previous declaration if we guessed wrong
4194 about what language DECL should belong to (C or C++). Otherwise,
4195 it's always DECL (and never something that's not a _DECL). */
e92cc029 4196
7bdbfa05
MM
4197tree
4198push_overloaded_decl (decl, flags)
8d08fdba 4199 tree decl;
7bdbfa05 4200 int flags;
8d08fdba 4201{
f181d4ae 4202 tree name = DECL_NAME (decl);
700f8a87 4203 tree old;
f181d4ae 4204 tree new_binding;
7bdbfa05 4205 int doing_global = (namespace_bindings_p () || !(flags & PUSH_LOCAL));
700f8a87
MS
4206
4207 if (doing_global)
4208 {
f181d4ae 4209 old = namespace_binding (name, DECL_CONTEXT (decl));
700f8a87 4210 if (old && TREE_CODE (old) == FUNCTION_DECL
a4443a08 4211 && DECL_ARTIFICIAL (old)
700f8a87
MS
4212 && (DECL_BUILT_IN (old) || DECL_BUILT_IN_NONANSI (old)))
4213 {
a4443a08
MS
4214 if (duplicate_decls (decl, old))
4215 return old;
700f8a87
MS
4216 old = NULL_TREE;
4217 }
4218 }
4219 else
f181d4ae 4220 old = lookup_name_current_level (name);
8d08fdba 4221
700f8a87 4222 if (old)
8d08fdba 4223 {
e1cd6e56 4224 if (TREE_CODE (old) == TYPE_DECL && DECL_ARTIFICIAL (old))
8926095f 4225 {
700f8a87 4226 tree t = TREE_TYPE (old);
cdf5b885
MS
4227 if (IS_AGGR_TYPE (t) && warn_shadow
4228 && (! DECL_IN_SYSTEM_HEADER (decl)
4229 || ! DECL_IN_SYSTEM_HEADER (old)))
8251199e 4230 cp_warning ("`%#D' hides constructor for `%#T'", decl, t);
5b605f68 4231 old = NULL_TREE;
8926095f 4232 }
700f8a87 4233 else if (is_overloaded_fn (old))
8d08fdba 4234 {
8d08fdba
MS
4235 tree tmp;
4236
2c73f9f5 4237 for (tmp = old; tmp; tmp = OVL_NEXT (tmp))
7bdbfa05
MM
4238 {
4239 tree fn = OVL_CURRENT (tmp);
4240
4241 if (TREE_CODE (tmp) == OVERLOAD && OVL_USED (tmp)
4242 && !(flags & PUSH_USING)
4243 && compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
4244 TYPE_ARG_TYPES (TREE_TYPE (decl))))
4245 cp_error ("`%#D' conflicts with previous using declaration `%#D'",
4246 decl, fn);
4247
4248 if (duplicate_decls (decl, fn))
4249 return fn;
4250 }
8d08fdba 4251 }
e1cd6e56
MS
4252 else
4253 {
8251199e
JM
4254 cp_error_at ("previous non-function declaration `%#D'", old);
4255 cp_error ("conflicts with function declaration `%#D'", decl);
a9aedbc2 4256 return decl;
e1cd6e56 4257 }
8d08fdba 4258 }
7177d104 4259
700f8a87 4260 if (old || TREE_CODE (decl) == TEMPLATE_DECL)
8d08fdba 4261 {
2c73f9f5 4262 if (old && TREE_CODE (old) != OVERLOAD)
f181d4ae
MM
4263 new_binding = ovl_cons (decl, ovl_cons (old, NULL_TREE));
4264 else
4265 new_binding = ovl_cons (decl, old);
347d73d7
ML
4266 if (flags & PUSH_USING)
4267 OVL_USED (new_binding) = 1;
8d08fdba
MS
4268 }
4269 else
f181d4ae
MM
4270 /* NAME is not ambiguous. */
4271 new_binding = decl;
700f8a87
MS
4272
4273 if (doing_global)
f181d4ae 4274 set_namespace_binding (name, current_namespace, new_binding);
700f8a87 4275 else
f181d4ae
MM
4276 {
4277 /* We only create an OVERLOAD if there was a previous binding at
4278 this level. In that case, we need to remove the old binding
4279 and replace it with the new binding. We must also run
d8f8dca1
MM
4280 through the NAMES on the binding level where the name was
4281 bound to update the chain. */
f181d4ae
MM
4282 if (TREE_CODE (new_binding) == OVERLOAD)
4283 {
4284 tree *d;
4285
4286 for (d = &BINDING_LEVEL (IDENTIFIER_BINDING (name))->names;
4287 *d;
4288 d = &TREE_CHAIN (*d))
4289 if (*d == old
4290 || (TREE_CODE (*d) == TREE_LIST
4291 && TREE_VALUE (*d) == old))
4292 {
d8f8dca1
MM
4293 if (TREE_CODE (*d) == TREE_LIST)
4294 /* Just replace the old binding with the new. */
4295 TREE_VALUE (*d) = new_binding;
4296 else
4297 /* Build a TREE_LIST to wrap the OVERLOAD. */
4298 *d = build_tree_list (NULL_TREE, new_binding);
4299
4300 /* And update the CPLUS_BINDING node. */
4301 BINDING_VALUE (IDENTIFIER_BINDING (name))
4302 = new_binding;
4303 return decl;
f181d4ae
MM
4304 }
4305
d8f8dca1
MM
4306 /* We should always find a previous binding in this case. */
4307 my_friendly_abort (0);
f181d4ae
MM
4308 }
4309
4310 /* Install the new binding. */
0034cf72 4311 push_local_binding (name, new_binding, flags);
f181d4ae 4312 }
700f8a87 4313
8d08fdba
MS
4314 return decl;
4315}
4316\f
4317/* Generate an implicit declaration for identifier FUNCTIONID
4318 as a function of type int (). Print a warning if appropriate. */
4319
4320tree
4321implicitly_declare (functionid)
4322 tree functionid;
4323{
4324 register tree decl;
4325 int temp = allocation_temporary_p ();
4326
4327 push_obstacks_nochange ();
4328
4329 /* Save the decl permanently so we can warn if definition follows.
4330 In ANSI C, warn_implicit is usually false, so the saves little space.
4331 But in C++, it's usually true, hence the extra code. */
d22c8596 4332 if (temp && (! warn_implicit || toplevel_bindings_p ()))
8d08fdba
MS
4333 end_temporary_allocation ();
4334
4335 /* We used to reuse an old implicit decl here,
4336 but this loses with inline functions because it can clobber
4337 the saved decl chains. */
4338 decl = build_lang_decl (FUNCTION_DECL, functionid, default_function_type);
4339
4340 DECL_EXTERNAL (decl) = 1;
4341 TREE_PUBLIC (decl) = 1;
4342
4343 /* ANSI standard says implicit declarations are in the innermost block.
d22c8596 4344 So we record the decl in the standard fashion. */
8d08fdba
MS
4345 pushdecl (decl);
4346 rest_of_decl_compilation (decl, NULL_PTR, 0, 0);
4347
4348 if (warn_implicit
4349 /* Only one warning per identifier. */
4350 && IDENTIFIER_IMPLICIT_DECL (functionid) == NULL_TREE)
4351 {
8251199e 4352 cp_pedwarn ("implicit declaration of function `%#D'", decl);
8d08fdba
MS
4353 }
4354
4355 SET_IDENTIFIER_IMPLICIT_DECL (functionid, decl);
4356
4357 pop_obstacks ();
4358
4359 return decl;
4360}
4361
8251199e 4362/* Return zero if the declaration NEWDECL is valid
8d08fdba
MS
4363 when the declaration OLDDECL (assumed to be for the same name)
4364 has already been seen.
4365 Otherwise return an error message format string with a %s
4366 where the identifier should go. */
4367
8251199e 4368static char *
8d08fdba
MS
4369redeclaration_error_message (newdecl, olddecl)
4370 tree newdecl, olddecl;
4371{
4372 if (TREE_CODE (newdecl) == TYPE_DECL)
4373 {
4374 /* Because C++ can put things into name space for free,
4375 constructs like "typedef struct foo { ... } foo"
4376 would look like an erroneous redeclaration. */
3bfdc719 4377 if (same_type_p (TREE_TYPE (newdecl), TREE_TYPE (olddecl)))
8251199e 4378 return 0;
8d08fdba 4379 else
8251199e 4380 return "redefinition of `%#D'";
8d08fdba
MS
4381 }
4382 else if (TREE_CODE (newdecl) == FUNCTION_DECL)
4383 {
4384 /* If this is a pure function, its olddecl will actually be
4385 the original initialization to `0' (which we force to call
4386 abort()). Don't complain about redefinition in this case. */
4387 if (DECL_LANG_SPECIFIC (olddecl) && DECL_ABSTRACT_VIRTUAL_P (olddecl))
8251199e 4388 return 0;
8d08fdba 4389
2c73f9f5
ML
4390 /* If both functions come from different namespaces, this is not
4391 a redeclaration - this is a conflict with a used function. */
6eb3bb27 4392 if (DECL_NAMESPACE_SCOPE_P (olddecl)
2c73f9f5 4393 && DECL_CONTEXT (olddecl) != DECL_CONTEXT (newdecl))
8251199e 4394 return "`%D' conflicts with used function";
2c73f9f5 4395
db5ae43f
MS
4396 /* We'll complain about linkage mismatches in
4397 warn_extern_redeclared_static. */
4398
2c73f9f5 4399 /* Defining the same name twice is no good. */
8d08fdba 4400 if (DECL_INITIAL (olddecl) != NULL_TREE
faae18ab 4401 && DECL_INITIAL (newdecl) != NULL_TREE)
8d08fdba
MS
4402 {
4403 if (DECL_NAME (olddecl) == NULL_TREE)
8251199e 4404 return "`%#D' not declared in class";
8d08fdba 4405 else
8251199e 4406 return "redefinition of `%#D'";
8d08fdba 4407 }
8251199e 4408 return 0;
8d08fdba 4409 }
51c184be
MS
4410 else if (TREE_CODE (newdecl) == TEMPLATE_DECL)
4411 {
ec255269
MS
4412 if ((TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == FUNCTION_DECL
4413 && DECL_INITIAL (DECL_TEMPLATE_RESULT (newdecl))
4414 && DECL_INITIAL (DECL_TEMPLATE_RESULT (olddecl)))
4415 || (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL
4416 && TYPE_SIZE (TREE_TYPE (newdecl))
4417 && TYPE_SIZE (TREE_TYPE (olddecl))))
8251199e
JM
4418 return "redefinition of `%#D'";
4419 return 0;
51c184be 4420 }
5566b478 4421 else if (toplevel_bindings_p ())
8d08fdba
MS
4422 {
4423 /* Objects declared at top level: */
4424 /* If at least one is a reference, it's ok. */
4425 if (DECL_EXTERNAL (newdecl) || DECL_EXTERNAL (olddecl))
8251199e 4426 return 0;
8926095f 4427 /* Reject two definitions. */
8251199e 4428 return "redefinition of `%#D'";
8d08fdba
MS
4429 }
4430 else
4431 {
4432 /* Objects declared with block scope: */
4433 /* Reject two definitions, and reject a definition
4434 together with an external reference. */
4435 if (!(DECL_EXTERNAL (newdecl) && DECL_EXTERNAL (olddecl)))
8251199e
JM
4436 return "redeclaration of `%#D'";
4437 return 0;
8d08fdba
MS
4438 }
4439}
4440\f
4441/* Get the LABEL_DECL corresponding to identifier ID as a label.
4442 Create one if none exists so far for the current function.
4443 This function is called for both label definitions and label references. */
4444
4445tree
4446lookup_label (id)
4447 tree id;
4448{
4449 register tree decl = IDENTIFIER_LABEL_VALUE (id);
4450
4451 if (current_function_decl == NULL_TREE)
4452 {
8251199e 4453 error ("label `%s' referenced outside of any function",
8d08fdba
MS
4454 IDENTIFIER_POINTER (id));
4455 return NULL_TREE;
4456 }
4457
4458 if ((decl == NULL_TREE
4459 || DECL_SOURCE_LINE (decl) == 0)
e349ee73
MS
4460 && (named_label_uses == NULL
4461 || named_label_uses->names_in_scope != current_binding_level->names
4462 || named_label_uses->label_decl != decl))
4463 {
4464 struct named_label_list *new_ent;
4465 new_ent
4466 = (struct named_label_list*)oballoc (sizeof (struct named_label_list));
4467 new_ent->label_decl = decl;
4468 new_ent->names_in_scope = current_binding_level->names;
4469 new_ent->binding_level = current_binding_level;
4470 new_ent->lineno_o_goto = lineno;
4471 new_ent->filename_o_goto = input_filename;
4472 new_ent->next = named_label_uses;
4473 named_label_uses = new_ent;
8d08fdba
MS
4474 }
4475
4476 /* Use a label already defined or ref'd with this name. */
4477 if (decl != NULL_TREE)
4478 {
4479 /* But not if it is inherited and wasn't declared to be inheritable. */
4480 if (DECL_CONTEXT (decl) != current_function_decl
4481 && ! C_DECLARED_LABEL_FLAG (decl))
4482 return shadow_label (id);
4483 return decl;
4484 }
4485
4486 decl = build_decl (LABEL_DECL, id, void_type_node);
4487
cffa8729
MS
4488 /* Make sure every label has an rtx. */
4489 label_rtx (decl);
4490
8d08fdba
MS
4491 /* A label not explicitly declared must be local to where it's ref'd. */
4492 DECL_CONTEXT (decl) = current_function_decl;
4493
4494 DECL_MODE (decl) = VOIDmode;
4495
4496 /* Say where one reference is to the label,
4497 for the sake of the error if it is not defined. */
4498 DECL_SOURCE_LINE (decl) = lineno;
4499 DECL_SOURCE_FILE (decl) = input_filename;
4500
4501 SET_IDENTIFIER_LABEL_VALUE (id, decl);
4502
4503 named_labels = tree_cons (NULL_TREE, decl, named_labels);
e349ee73 4504 named_label_uses->label_decl = decl;
8d08fdba
MS
4505
4506 return decl;
4507}
4508
4509/* Make a label named NAME in the current function,
4510 shadowing silently any that may be inherited from containing functions
4511 or containing scopes.
4512
4513 Note that valid use, if the label being shadowed
4514 comes from another scope in the same function,
4515 requires calling declare_nonlocal_label right away. */
4516
4517tree
4518shadow_label (name)
4519 tree name;
4520{
4521 register tree decl = IDENTIFIER_LABEL_VALUE (name);
4522
4523 if (decl != NULL_TREE)
4524 {
4525 shadowed_labels = tree_cons (NULL_TREE, decl, shadowed_labels);
4526 SET_IDENTIFIER_LABEL_VALUE (name, NULL_TREE);
8d08fdba
MS
4527 }
4528
4529 return lookup_label (name);
4530}
4531
4532/* Define a label, specifying the location in the source file.
4533 Return the LABEL_DECL node for the label, if the definition is valid.
4534 Otherwise return 0. */
4535
4536tree
4537define_label (filename, line, name)
4538 char *filename;
4539 int line;
4540 tree name;
4541{
5566b478
MS
4542 tree decl;
4543
4544 if (minimal_parse_mode)
4545 {
4546 push_obstacks (&permanent_obstack, &permanent_obstack);
4547 decl = build_decl (LABEL_DECL, name, void_type_node);
4548 pop_obstacks ();
4549 DECL_SOURCE_LINE (decl) = line;
4550 DECL_SOURCE_FILE (decl) = filename;
4551 add_tree (decl);
4552 return decl;
4553 }
4554
4555 decl = lookup_label (name);
8d08fdba
MS
4556
4557 /* After labels, make any new cleanups go into their
4558 own new (temporary) binding contour. */
4559 current_binding_level->more_cleanups_ok = 0;
4560
4561 /* If label with this name is known from an outer context, shadow it. */
4562 if (decl != NULL_TREE && DECL_CONTEXT (decl) != current_function_decl)
4563 {
4564 shadowed_labels = tree_cons (NULL_TREE, decl, shadowed_labels);
4565 SET_IDENTIFIER_LABEL_VALUE (name, NULL_TREE);
4566 decl = lookup_label (name);
4567 }
4568
e1cd6e56 4569 if (name == get_identifier ("wchar_t"))
8251199e 4570 cp_pedwarn ("label named wchar_t");
e1cd6e56 4571
8d08fdba
MS
4572 if (DECL_INITIAL (decl) != NULL_TREE)
4573 {
8251199e 4574 cp_error ("duplicate label `%D'", decl);
8d08fdba
MS
4575 return 0;
4576 }
4577 else
4578 {
e349ee73 4579 struct named_label_list *uses, *prev;
28cbf42c 4580 int identified = 0;
8d08fdba
MS
4581
4582 /* Mark label as having been defined. */
4583 DECL_INITIAL (decl) = error_mark_node;
4584 /* Say where in the source. */
4585 DECL_SOURCE_FILE (decl) = filename;
4586 DECL_SOURCE_LINE (decl) = line;
4587
e349ee73
MS
4588 prev = NULL;
4589 uses = named_label_uses;
4590 while (uses != NULL)
4591 if (uses->label_decl == decl)
8d08fdba
MS
4592 {
4593 struct binding_level *b = current_binding_level;
4594 while (b)
4595 {
4596 tree new_decls = b->names;
e349ee73
MS
4597 tree old_decls = (b == uses->binding_level)
4598 ? uses->names_in_scope : NULL_TREE;
8d08fdba
MS
4599 while (new_decls != old_decls)
4600 {
4601 if (TREE_CODE (new_decls) == VAR_DECL
4602 /* Don't complain about crossing initialization
4603 of internal entities. They can't be accessed,
4604 and they should be cleaned up
4605 by the time we get to the label. */
700f8a87 4606 && ! DECL_ARTIFICIAL (new_decls)
5524676d
JM
4607 && !(DECL_INITIAL (new_decls) == NULL_TREE
4608 && pod_type_p (TREE_TYPE (new_decls))))
8d08fdba 4609 {
e349ee73
MS
4610 if (! identified)
4611 {
8251199e 4612 cp_error ("jump to label `%D'", decl);
e349ee73
MS
4613 error_with_file_and_line (uses->filename_o_goto,
4614 uses->lineno_o_goto,
4615 " from here");
4616 identified = 1;
4617 }
5524676d
JM
4618 if (DECL_INITIAL (new_decls)
4619 || TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (new_decls)))
4620 cp_error_at (" crosses initialization of `%#D'",
4621 new_decls);
4622 else
4623 cp_error_at (" enters scope of non-POD `%#D'",
4624 new_decls);
8d08fdba
MS
4625 }
4626 new_decls = TREE_CHAIN (new_decls);
4627 }
e349ee73 4628 if (b == uses->binding_level)
8d08fdba
MS
4629 break;
4630 b = b->level_chain;
4631 }
4632
e349ee73
MS
4633 if (prev != NULL)
4634 prev->next = uses->next;
8d08fdba 4635 else
e349ee73
MS
4636 named_label_uses = uses->next;
4637
4638 uses = uses->next;
4639 }
4640 else
4641 {
4642 prev = uses;
4643 uses = uses->next;
8d08fdba
MS
4644 }
4645 current_function_return_value = NULL_TREE;
4646 return decl;
4647 }
4648}
4649
a5894242
MS
4650struct cp_switch
4651{
4652 struct binding_level *level;
4653 struct cp_switch *next;
4654};
4655
4656static struct cp_switch *switch_stack;
4657
4658void
4659push_switch ()
4660{
4661 struct cp_switch *p
4662 = (struct cp_switch *) oballoc (sizeof (struct cp_switch));
4663 p->level = current_binding_level;
4664 p->next = switch_stack;
4665 switch_stack = p;
4666}
4667
4668void
4669pop_switch ()
4670{
4671 switch_stack = switch_stack->next;
4672}
4673
8d08fdba
MS
4674/* Same, but for CASE labels. If DECL is NULL_TREE, it's the default. */
4675/* XXX Note decl is never actually used. (bpk) */
e92cc029 4676
8d08fdba 4677void
b370501f 4678define_case_label ()
8d08fdba
MS
4679{
4680 tree cleanup = last_cleanup_this_contour ();
a5894242
MS
4681 struct binding_level *b = current_binding_level;
4682 int identified = 0;
4683
8d08fdba
MS
4684 if (cleanup)
4685 {
4686 static int explained = 0;
8251199e
JM
4687 cp_warning_at ("destructor needed for `%#D'", TREE_PURPOSE (cleanup));
4688 warning ("where case label appears here");
8d08fdba
MS
4689 if (!explained)
4690 {
8251199e
JM
4691 warning ("(enclose actions of previous case statements requiring");
4692 warning ("destructors in their own binding contours.)");
8d08fdba
MS
4693 explained = 1;
4694 }
4695 }
4696
a5894242
MS
4697 for (; b && b != switch_stack->level; b = b->level_chain)
4698 {
4699 tree new_decls = b->names;
4700 for (; new_decls; new_decls = TREE_CHAIN (new_decls))
4701 {
4702 if (TREE_CODE (new_decls) == VAR_DECL
4703 /* Don't complain about crossing initialization
4704 of internal entities. They can't be accessed,
4705 and they should be cleaned up
4706 by the time we get to the label. */
4707 && ! DECL_ARTIFICIAL (new_decls)
4708 && ((DECL_INITIAL (new_decls) != NULL_TREE
4709 && DECL_INITIAL (new_decls) != error_mark_node)
4710 || TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (new_decls))))
4711 {
4712 if (! identified)
8251199e 4713 error ("jump to case label");
a5894242 4714 identified = 1;
8251199e 4715 cp_error_at (" crosses initialization of `%#D'",
a5894242
MS
4716 new_decls);
4717 }
4718 }
4719 }
4720
8d08fdba
MS
4721 /* After labels, make any new cleanups go into their
4722 own new (temporary) binding contour. */
4723
4724 current_binding_level->more_cleanups_ok = 0;
4725 current_function_return_value = NULL_TREE;
4726}
4727\f
4728/* Return the list of declarations of the current level.
4729 Note that this list is in reverse order unless/until
4730 you nreverse it; and when you do nreverse it, you must
4731 store the result back using `storedecls' or you will lose. */
4732
4733tree
4734getdecls ()
4735{
4736 return current_binding_level->names;
4737}
4738
4739/* Return the list of type-tags (for structs, etc) of the current level. */
4740
4741tree
4742gettags ()
4743{
4744 return current_binding_level->tags;
4745}
4746
4747/* Store the list of declarations of the current level.
4748 This is done for the parameter declarations of a function being defined,
4749 after they are modified in the light of any missing parameters. */
4750
4751static void
4752storedecls (decls)
4753 tree decls;
4754{
4755 current_binding_level->names = decls;
4756}
4757
4758/* Similarly, store the list of tags of the current level. */
4759
4760static void
4761storetags (tags)
4762 tree tags;
4763{
4764 current_binding_level->tags = tags;
4765}
4766\f
4767/* Given NAME, an IDENTIFIER_NODE,
4768 return the structure (or union or enum) definition for that name.
4769 Searches binding levels from BINDING_LEVEL up to the global level.
4770 If THISLEVEL_ONLY is nonzero, searches only the specified context
4771 (but skips any tag-transparent contexts to find one that is
4772 meaningful for tags).
4773 FORM says which kind of type the caller wants;
4774 it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
4775 If the wrong kind of type is found, and it's not a template, an error is
4776 reported. */
4777
4778static tree
4779lookup_tag (form, name, binding_level, thislevel_only)
4780 enum tree_code form;
8d08fdba 4781 tree name;
cffa8729 4782 struct binding_level *binding_level;
8d08fdba
MS
4783 int thislevel_only;
4784{
4785 register struct binding_level *level;
36a117a5
MM
4786 /* Non-zero if, we should look past a pseudo-global level, even if
4787 THISLEVEL_ONLY. */
4788 int allow_pseudo_global = 1;
8d08fdba
MS
4789
4790 for (level = binding_level; level; level = level->level_chain)
4791 {
4792 register tree tail;
4793 if (ANON_AGGRNAME_P (name))
4794 for (tail = level->tags; tail; tail = TREE_CHAIN (tail))
4795 {
4796 /* There's no need for error checking here, because
4797 anon names are unique throughout the compilation. */
4798 if (TYPE_IDENTIFIER (TREE_VALUE (tail)) == name)
4799 return TREE_VALUE (tail);
4800 }
2c73f9f5
ML
4801 else if (level->namespace_p)
4802 /* Do namespace lookup. */
6c011b01 4803 for (tail = current_namespace; 1; tail = CP_DECL_CONTEXT (tail))
2c73f9f5 4804 {
36a117a5
MM
4805 tree old = binding_for_name (name, tail);
4806
4807 /* If we just skipped past a pseudo global level, even
4808 though THISLEVEL_ONLY, and we find a template class
4809 declaration, then we use the _TYPE node for the
4810 template. See the example below. */
4811 if (thislevel_only && !allow_pseudo_global
4812 && old && BINDING_VALUE (old)
4813 && DECL_CLASS_TEMPLATE_P (BINDING_VALUE (old)))
4814 old = TREE_TYPE (BINDING_VALUE (old));
4815 else
4816 old = BINDING_TYPE (old);
4817
2c73f9f5
ML
4818 /* If it has an original type, it is a typedef, and we
4819 should not return it. */
4820 if (old && DECL_ORIGINAL_TYPE (TYPE_NAME (old)))
4821 old = NULL_TREE;
4822 if (old && TREE_CODE (old) != form
4823 && !(form != ENUMERAL_TYPE && TREE_CODE (old) == TEMPLATE_DECL))
4824 {
8251199e 4825 cp_error ("`%#D' redeclared as %C", old, form);
2c73f9f5
ML
4826 return NULL_TREE;
4827 }
4828 if (old)
4829 return old;
4830 if (thislevel_only || tail == global_namespace)
4831 return NULL_TREE;
4832 }
8d08fdba
MS
4833 else
4834 for (tail = level->tags; tail; tail = TREE_CHAIN (tail))
4835 {
a80e4195 4836 if (TREE_PURPOSE (tail) == name)
8d08fdba
MS
4837 {
4838 enum tree_code code = TREE_CODE (TREE_VALUE (tail));
4839 /* Should tighten this up; it'll probably permit
4840 UNION_TYPE and a struct template, for example. */
4841 if (code != form
5566b478 4842 && !(form != ENUMERAL_TYPE && code == TEMPLATE_DECL))
8d08fdba
MS
4843 {
4844 /* Definition isn't the kind we were looking for. */
8251199e 4845 cp_error ("`%#D' redeclared as %C", TREE_VALUE (tail),
8d08fdba 4846 form);
72b7eeff 4847 return NULL_TREE;
8d08fdba
MS
4848 }
4849 return TREE_VALUE (tail);
4850 }
4851 }
4852 if (thislevel_only && ! level->tag_transparent)
5566b478 4853 {
36a117a5 4854 if (level->pseudo_global && allow_pseudo_global)
5566b478 4855 {
36a117a5
MM
4856 /* We must deal with cases like this:
4857
4858 template <class T> struct S;
4859 template <class T> struct S {};
4860
4861 When looking up `S', for the second declaration, we
4862 would like to find the first declaration. But, we
4863 are in the pseudo-global level created for the
4864 template parameters, rather than the (surrounding)
4865 namespace level. Thus, we keep going one more level,
4866 even though THISLEVEL_ONLY is non-zero. */
4867 allow_pseudo_global = 0;
4868 continue;
5566b478 4869 }
36a117a5
MM
4870 else
4871 return NULL_TREE;
5566b478 4872 }
2c73f9f5 4873 if (current_class_type && level->level_chain->namespace_p)
8d08fdba
MS
4874 {
4875 /* Try looking in this class's tags before heading into
4876 global binding level. */
4877 tree context = current_class_type;
4878 while (context)
4879 {
4880 switch (TREE_CODE_CLASS (TREE_CODE (context)))
4881 {
4882 tree these_tags;
4883 case 't':
4884 these_tags = CLASSTYPE_TAGS (context);
4885 if (ANON_AGGRNAME_P (name))
4886 while (these_tags)
4887 {
4888 if (TYPE_IDENTIFIER (TREE_VALUE (these_tags))
4889 == name)
4890 return TREE_VALUE (tail);
4891 these_tags = TREE_CHAIN (these_tags);
4892 }
4893 else
4894 while (these_tags)
4895 {
4896 if (TREE_PURPOSE (these_tags) == name)
4897 {
4898 if (TREE_CODE (TREE_VALUE (these_tags)) != form)
4899 {
8251199e 4900 cp_error ("`%#D' redeclared as %C in class scope",
8d08fdba 4901 TREE_VALUE (tail), form);
72b7eeff 4902 return NULL_TREE;
8d08fdba
MS
4903 }
4904 return TREE_VALUE (tail);
4905 }
4906 these_tags = TREE_CHAIN (these_tags);
4907 }
4908 /* If this type is not yet complete, then don't
4909 look at its context. */
4910 if (TYPE_SIZE (context) == NULL_TREE)
4911 goto no_context;
4912 /* Go to next enclosing type, if any. */
d2e5ee5c 4913 context = DECL_CONTEXT (TYPE_MAIN_DECL (context));
8d08fdba
MS
4914 break;
4915 case 'd':
4916 context = DECL_CONTEXT (context);
4917 break;
4918 default:
4919 my_friendly_abort (10);
4920 }
4921 continue;
4922 no_context:
4923 break;
4924 }
4925 }
4926 }
4927 return NULL_TREE;
4928}
4929
bd6dd845 4930#if 0
8d08fdba
MS
4931void
4932set_current_level_tags_transparency (tags_transparent)
4933 int tags_transparent;
4934{
4935 current_binding_level->tag_transparent = tags_transparent;
4936}
bd6dd845 4937#endif
8d08fdba
MS
4938
4939/* Given a type, find the tag that was defined for it and return the tag name.
4940 Otherwise return 0. However, the value can never be 0
4941 in the cases in which this is used.
4942
4943 C++: If NAME is non-zero, this is the new name to install. This is
4944 done when replacing anonymous tags with real tag names. */
4945
4946static tree
4947lookup_tag_reverse (type, name)
4948 tree type;
4949 tree name;
4950{
4951 register struct binding_level *level;
4952
4953 for (level = current_binding_level; level; level = level->level_chain)
4954 {
4955 register tree tail;
4956 for (tail = level->tags; tail; tail = TREE_CHAIN (tail))
4957 {
4958 if (TREE_VALUE (tail) == type)
4959 {
4960 if (name)
4961 TREE_PURPOSE (tail) = name;
4962 return TREE_PURPOSE (tail);
4963 }
4964 }
4965 }
4966 return NULL_TREE;
4967}
8d08fdba
MS
4968\f
4969/* Lookup TYPE in CONTEXT (a chain of nested types or a FUNCTION_DECL).
4970 Return the type value, or NULL_TREE if not found. */
e92cc029 4971
8d08fdba
MS
4972static tree
4973lookup_nested_type (type, context)
4974 tree type;
4975 tree context;
4976{
4977 if (context == NULL_TREE)
4978 return NULL_TREE;
4979 while (context)
4980 {
4981 switch (TREE_CODE (context))
4982 {
4983 case TYPE_DECL:
4984 {
4985 tree ctype = TREE_TYPE (context);
4986 tree match = value_member (type, CLASSTYPE_TAGS (ctype));
4987 if (match)
4988 return TREE_VALUE (match);
4989 context = DECL_CONTEXT (context);
4990
4991 /* When we have a nested class whose member functions have
4992 local types (e.g., a set of enums), we'll arrive here
4993 with the DECL_CONTEXT as the actual RECORD_TYPE node for
4994 the enclosing class. Instead, we want to make sure we
4995 come back in here with the TYPE_DECL, not the RECORD_TYPE. */
4996 if (context && TREE_CODE (context) == RECORD_TYPE)
4997 context = TREE_CHAIN (context);
4998 }
4999 break;
5000 case FUNCTION_DECL:
e1cd6e56
MS
5001 if (TYPE_NAME (type) && TYPE_IDENTIFIER (type))
5002 return lookup_name (TYPE_IDENTIFIER (type), 1);
5003 return NULL_TREE;
8d08fdba
MS
5004 default:
5005 my_friendly_abort (12);
5006 }
5007 }
5008 return NULL_TREE;
5009}
5010
a9aedbc2 5011/* Look up NAME in the NAMESPACE. */
e92cc029 5012
a9aedbc2
MS
5013tree
5014lookup_namespace_name (namespace, name)
5015 tree namespace, name;
5016{
2c73f9f5 5017 struct tree_binding _b;
30394414 5018 tree val;
2c73f9f5 5019
30394414 5020 my_friendly_assert (TREE_CODE (namespace) == NAMESPACE_DECL, 370);
1231fb96 5021
1231fb96 5022 if (TREE_CODE (name) == NAMESPACE_DECL)
d8f8dca1 5023 /* This happens for A::B<int> when B is a namespace. */
1231fb96 5024 return name;
d8f8dca1
MM
5025 else if (TREE_CODE (name) == TEMPLATE_DECL)
5026 {
5027 /* This happens for A::B where B is a template, and there are no
5028 template arguments. */
5029 cp_error ("invalid use of `%D'", name);
5030 return error_mark_node;
5031 }
1231fb96 5032
30394414
JM
5033 my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, 373);
5034
2c73f9f5 5035 val = binding_init (&_b);
52c11ef6 5036 if (!qualified_lookup_using_namespace (name, namespace, val, 0))
2c73f9f5
ML
5037 return error_mark_node;
5038
5039 if (BINDING_VALUE (val))
1c35f5b6
JM
5040 {
5041 val = BINDING_VALUE (val);
5042
5043 /* If we have a single function from a using decl, pull it out. */
5044 if (TREE_CODE (val) == OVERLOAD && ! really_overloaded_fn (val))
5045 val = OVL_FUNCTION (val);
5046 return val;
5047 }
5048
8251199e 5049 cp_error ("`%D' undeclared in namespace `%D'", name, namespace);
30394414 5050 return error_mark_node;
a9aedbc2
MS
5051}
5052
7ddedda4
MM
5053/* Hash a TYPENAME_TYPE. K is really of type `tree'. */
5054
5055static unsigned long
5056typename_hash (k)
5057 hash_table_key k;
5058{
5059 unsigned long hash;
5060 tree t;
5061
5062 t = (tree) k;
5063 hash = (((unsigned long) TYPE_CONTEXT (t))
5064 ^ ((unsigned long) DECL_NAME (TYPE_NAME (t))));
5065
5066 return hash;
5067}
5068
5069/* Compare two TYPENAME_TYPEs. K1 and K2 are really of type `tree'. */
5070
5071static boolean
5072typename_compare (k1, k2)
5073 hash_table_key k1;
5074 hash_table_key k2;
5075{
5076 tree t1;
5077 tree t2;
5078 tree d1;
5079 tree d2;
5080
5081 t1 = (tree) k1;
5082 t2 = (tree) k2;
5083 d1 = TYPE_NAME (t1);
5084 d2 = TYPE_NAME (t2);
5085
5086 return (DECL_NAME (d1) == DECL_NAME (d2)
5087 && same_type_p (TYPE_CONTEXT (t1), TYPE_CONTEXT (t2))
5088 && ((TREE_TYPE (t1) != NULL_TREE)
5089 == (TREE_TYPE (t2) != NULL_TREE))
5090 && same_type_p (TREE_TYPE (t1), TREE_TYPE (t2))
5091 && TYPENAME_TYPE_FULLNAME (t1) == TYPENAME_TYPE_FULLNAME (t2));
5092}
5093
45869a6c
MM
5094/* Build a TYPENAME_TYPE. If the type is `typename T::t', CONTEXT is
5095 the type of `T', NAME is the IDENTIFIER_NODE for `t'. If BASE_TYPE
5096 is non-NULL, this type is being created by the implicit typename
5097 extension, and BASE_TYPE is a type named `t' in some base class of
5098 `T' which depends on template parameters.
5099
5100 Returns the new TYPENAME_TYPE. */
5101
5102tree
5103build_typename_type (context, name, fullname, base_type)
5104 tree context;
5105 tree name;
5106 tree fullname;
5107 tree base_type;
5108{
5109 tree t;
5110 tree d;
7ddedda4 5111 struct hash_entry* e;
45869a6c 5112
7ddedda4
MM
5113 static struct hash_table ht;
5114
5115 push_obstacks (&permanent_obstack, &permanent_obstack);
5116
5117 if (!ht.table
5118 && !hash_table_init (&ht, &hash_newfunc, &typename_hash,
5119 &typename_compare))
5120 fatal ("virtual memory exhausted");
45869a6c
MM
5121
5122 /* Build the TYPENAME_TYPE. */
5123 t = make_lang_type (TYPENAME_TYPE);
5124 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
5125 TYPENAME_TYPE_FULLNAME (t) = fullname;
5126 TREE_TYPE (t) = base_type;
45869a6c
MM
5127
5128 /* Build the corresponding TYPE_DECL. */
5129 d = build_decl (TYPE_DECL, name, t);
5130 TYPE_NAME (TREE_TYPE (d)) = d;
5131 TYPE_STUB_DECL (TREE_TYPE (d)) = d;
5132 DECL_CONTEXT (d) = FROB_CONTEXT (context);
2642b9bf 5133 DECL_ARTIFICIAL (d) = 1;
45869a6c 5134
7ddedda4
MM
5135 /* See if we already have this type. */
5136 e = hash_lookup (&ht, t, /*create=*/false, /*copy=*/0);
5137 if (e)
5138 {
5139 /* This will free not only TREE_TYPE, but the lang-specific data
5140 and the TYPE_DECL as well. */
5141 obstack_free (&permanent_obstack, t);
5142 t = (tree) e->key;
5143 }
5144 else
5145 /* Insert the type into the table. */
5146 hash_lookup (&ht, t, /*create=*/true, /*copy=*/0);
5147
5148 pop_obstacks ();
45869a6c
MM
5149
5150 return t;
5151}
5152
5566b478
MS
5153tree
5154make_typename_type (context, name)
5155 tree context, name;
5156{
45869a6c 5157 tree t;
b2b7d40a 5158 tree fullname;
a80e4195 5159
653cc74a
JM
5160 if (TREE_CODE_CLASS (TREE_CODE (name)) == 't')
5161 name = TYPE_IDENTIFIER (name);
5162 else if (TREE_CODE (name) == TYPE_DECL)
a80e4195 5163 name = DECL_NAME (name);
b2b7d40a
JM
5164
5165 fullname = name;
5166
5167 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
11686454
JM
5168 {
5169 name = TREE_OPERAND (name, 0);
5170 if (TREE_CODE (name) == TEMPLATE_DECL)
5171 name = TREE_OPERAND (fullname, 0) = DECL_NAME (name);
5172 }
b2b7d40a 5173 if (TREE_CODE (name) != IDENTIFIER_NODE)
a80e4195 5174 my_friendly_abort (2000);
5566b478 5175
04ddee1b
BK
5176 if (TREE_CODE (context) == NAMESPACE_DECL)
5177 {
5178 /* We can get here from typename_sub0 in the explicit_template_type
5179 expansion. Just fail. */
5180 cp_error ("no class template named `%#T' in `%#T'",
5181 name, context);
5182 return error_mark_node;
5183 }
5184
85b71cf2 5185 if (! uses_template_parms (context)
b77ead33 5186 || currently_open_class (context))
5566b478 5187 {
b2b7d40a
JM
5188 if (TREE_CODE (fullname) == TEMPLATE_ID_EXPR)
5189 {
5190 if (IS_AGGR_TYPE (context))
5191 t = lookup_field (context, name, 0, 0);
5192 else
5193 t = NULL_TREE;
5194
5195 if (t == NULL_TREE || TREE_CODE (t) != TEMPLATE_DECL
5196 || TREE_CODE (DECL_RESULT (t)) != TYPE_DECL)
5197 {
8251199e 5198 cp_error ("no class template named `%#T' in `%#T'",
b2b7d40a
JM
5199 name, context);
5200 return error_mark_node;
5201 }
ffb690bd 5202
b2b7d40a 5203 return lookup_template_class (t, TREE_OPERAND (fullname, 1),
36a117a5
MM
5204 NULL_TREE, context,
5205 /*entering_scope=*/0);
b2b7d40a
JM
5206 }
5207 else
5566b478 5208 {
b2b7d40a
JM
5209 if (IS_AGGR_TYPE (context))
5210 t = lookup_field (context, name, 0, 1);
5211 else
5212 t = NULL_TREE;
5213
5214 if (t == NULL_TREE)
5215 {
8251199e 5216 cp_error ("no type named `%#T' in `%#T'", name, context);
b2b7d40a
JM
5217 return error_mark_node;
5218 }
1107c4b3 5219
b2b7d40a 5220 return TREE_TYPE (t);
5566b478 5221 }
5566b478 5222 }
45869a6c
MM
5223
5224 return build_typename_type (context, name, fullname, NULL_TREE);
5566b478
MS
5225}
5226
2c73f9f5
ML
5227/* Select the right _DECL from multiple choices. */
5228
5229static tree
52c11ef6 5230select_decl (binding, flags)
2c73f9f5 5231 tree binding;
52c11ef6 5232 int flags;
2c73f9f5
ML
5233{
5234 tree val;
5235 val = BINDING_VALUE (binding);
52c11ef6 5236 if (LOOKUP_NAMESPACES_ONLY (flags))
3e3f722c
ML
5237 {
5238 /* We are not interested in types. */
5239 if (val && TREE_CODE (val) == NAMESPACE_DECL)
5240 return val;
5241 return NULL_TREE;
5242 }
5243
2c73f9f5
ML
5244 /* If we could have a type and
5245 we have nothing or we need a type and have none. */
5246 if (BINDING_TYPE (binding)
52c11ef6
JM
5247 && (!val || ((flags & LOOKUP_PREFER_TYPES)
5248 && TREE_CODE (val) != TYPE_DECL)))
2c73f9f5
ML
5249 val = TYPE_STUB_DECL (BINDING_TYPE (binding));
5250 /* Don't return non-types if we really prefer types. */
52c11ef6 5251 else if (val && LOOKUP_TYPES_ONLY (flags) && TREE_CODE (val) != TYPE_DECL
2c73f9f5
ML
5252 && (!looking_for_template || TREE_CODE (val) != TEMPLATE_DECL))
5253 val = NULL_TREE;
1c35f5b6 5254
2c73f9f5
ML
5255 return val;
5256}
5257
ea9635c7
ML
5258/* Unscoped lookup of a global, iterate over namespaces, considering
5259 using namespace statements. */
5260
5261static tree
52c11ef6 5262unqualified_namespace_lookup (name, flags)
ea9635c7 5263 tree name;
52c11ef6 5264 int flags;
ea9635c7
ML
5265{
5266 struct tree_binding _binding;
5267 tree b = binding_init (&_binding);
5268 tree initial = current_decl_namespace();
5269 tree scope = initial;
5270 tree siter;
5271 struct binding_level *level;
5272 tree val = NULL_TREE;
5273
5274 while (!val)
5275 {
5276 val = binding_for_name (name, scope);
5277
5278 /* Initialize binding for this context. */
5279 BINDING_VALUE (b) = BINDING_VALUE (val);
5280 BINDING_TYPE (b) = BINDING_TYPE (val);
5281
5282 /* Add all _DECLs seen through local using-directives. */
5283 for (level = current_binding_level;
5284 !level->namespace_p;
5285 level = level->level_chain)
52c11ef6
JM
5286 if (!lookup_using_namespace (name, b, level->using_directives,
5287 scope, flags))
ea9635c7
ML
5288 /* Give up because of error. */
5289 return NULL_TREE;
5290
5291 /* Add all _DECLs seen through global using-directives. */
5292 /* XXX local and global using lists should work equally. */
5293 siter = initial;
5294 while (1)
5295 {
5296 if (!lookup_using_namespace (name, b, DECL_NAMESPACE_USING (siter),
52c11ef6 5297 scope, flags))
ea9635c7
ML
5298 /* Give up because of error. */
5299 return NULL_TREE;
5300 if (siter == scope) break;
5301 siter = CP_DECL_CONTEXT (siter);
5302 }
5303
52c11ef6 5304 val = select_decl (b, flags);
ea9635c7
ML
5305 if (scope == global_namespace)
5306 break;
0c64a9ca 5307 scope = CP_DECL_CONTEXT (scope);
ea9635c7
ML
5308 }
5309 return val;
5310}
5311
52c11ef6
JM
5312/* Combine prefer_type and namespaces_only into flags. */
5313
5314static int
5315lookup_flags (prefer_type, namespaces_only)
5316 int prefer_type, namespaces_only;
5317{
5318 if (namespaces_only)
5319 return LOOKUP_PREFER_NAMESPACES;
5320 if (prefer_type > 1)
5321 return LOOKUP_PREFER_TYPES;
5322 if (prefer_type > 0)
5323 return LOOKUP_PREFER_BOTH;
5324 return 0;
5325}
5326
5327/* Given a lookup that returned VAL, use FLAGS to decide if we want to
5328 ignore it or not. Subroutine of lookup_name_real. */
5329
5330static tree
5331qualify_lookup (val, flags)
5332 tree val;
5333 int flags;
5334{
5335 if (val == NULL_TREE)
5336 return val;
6b945830
JM
5337 if ((flags & LOOKUP_PREFER_NAMESPACES) && TREE_CODE (val) == NAMESPACE_DECL)
5338 return val;
5339 if ((flags & LOOKUP_PREFER_TYPES)
5340 && (TREE_CODE (val) == TYPE_DECL
5341 || ((flags & LOOKUP_TEMPLATES_EXPECTED)
5342 && DECL_CLASS_TEMPLATE_P (val))))
5343 return val;
5344 if (flags & (LOOKUP_PREFER_NAMESPACES | LOOKUP_PREFER_TYPES))
52c11ef6
JM
5345 return NULL_TREE;
5346 return val;
5347}
5348
8d08fdba
MS
5349/* Look up NAME in the current binding level and its superiors in the
5350 namespace of variables, functions and typedefs. Return a ..._DECL
5351 node of some kind representing its definition if there is only one
5352 such declaration, or return a TREE_LIST with all the overloaded
5353 definitions if there are many, or return 0 if it is undefined.
5354
2c73f9f5
ML
5355 If PREFER_TYPE is > 0, we prefer TYPE_DECLs or namespaces.
5356 If PREFER_TYPE is > 1, we reject non-type decls (e.g. namespaces).
a28e3c7f 5357 If PREFER_TYPE is -2, we're being called from yylex(). (UGLY)
f84b4be9
JM
5358 Otherwise we prefer non-TYPE_DECLs.
5359
5360 If NONCLASS is non-zero, we don't look for the NAME in class scope,
5361 using IDENTIFIER_CLASS_VALUE. */
8d08fdba 5362
824b9a4c 5363static tree
3e3f722c 5364lookup_name_real (name, prefer_type, nonclass, namespaces_only)
8d08fdba 5365 tree name;
3e3f722c 5366 int prefer_type, nonclass, namespaces_only;
8d08fdba
MS
5367{
5368 register tree val;
a28e3c7f 5369 int yylex = 0;
e1cd6e56 5370 tree from_obj = NULL_TREE;
52c11ef6 5371 int flags;
8d08fdba 5372
3e3f722c
ML
5373 /* Hack: copy flag set by parser, if set. */
5374 if (only_namespace_names)
5375 namespaces_only = 1;
52c11ef6 5376
a28e3c7f
MS
5377 if (prefer_type == -2)
5378 {
5379 extern int looking_for_typename;
fc378698 5380 tree type = NULL_TREE;
a28e3c7f
MS
5381
5382 yylex = 1;
5383 prefer_type = looking_for_typename;
e1cd6e56 5384
52c11ef6
JM
5385 flags = lookup_flags (prefer_type, namespaces_only);
5386 /* During parsing, we need to complain. */
5387 flags |= LOOKUP_COMPLAIN;
5388 /* If the next thing is '<', class templates are types. */
5389 if (looking_for_template)
5390 flags |= LOOKUP_TEMPLATES_EXPECTED;
5391
653cc74a
JM
5392 /* std:: becomes :: for now. */
5393 if (got_scope == std_node)
5394 got_scope = void_type_node;
5395
e1cd6e56
MS
5396 if (got_scope)
5397 type = got_scope;
dff6b454 5398 else if (got_object != error_mark_node)
e1cd6e56 5399 type = got_object;
a28e3c7f 5400
e1cd6e56 5401 if (type)
a28e3c7f 5402 {
e1cd6e56 5403 if (type == error_mark_node)
f376e137 5404 return error_mark_node;
a80e4195
MS
5405 if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type))
5406 type = TREE_TYPE (type);
5566b478 5407
2b9dc906
JM
5408 if (TYPE_P (type))
5409 type = complete_type (type);
5566b478 5410
a1774733 5411 if (TREE_CODE (type) == VOID_TYPE)
2c73f9f5
ML
5412 type = global_namespace;
5413 if (TREE_CODE (type) == NAMESPACE_DECL)
a9aedbc2 5414 {
2c73f9f5
ML
5415 struct tree_binding b;
5416 val = binding_init (&b);
52c11ef6 5417 if (!qualified_lookup_using_namespace (name, type, val, flags))
2c73f9f5 5418 return NULL_TREE;
52c11ef6 5419 val = select_decl (val, flags);
a9aedbc2 5420 }
5566b478 5421 else if (! IS_AGGR_TYPE (type)
5156628f 5422 || TREE_CODE (type) == TEMPLATE_TYPE_PARM
73b0fce8 5423 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM
5156628f 5424 || TREE_CODE (type) == TYPENAME_TYPE)
e92cc029 5425 /* Someone else will give an error about this if needed. */
a28e3c7f 5426 val = NULL_TREE;
e1cd6e56 5427 else if (TYPE_BEING_DEFINED (type))
700f8a87
MS
5428 {
5429 val = IDENTIFIER_CLASS_VALUE (name);
e1cd6e56 5430 if (val && DECL_CONTEXT (val) != type)
700f8a87
MS
5431 {
5432 struct binding_level *b = class_binding_level;
5433 for (val = NULL_TREE; b; b = b->level_chain)
5434 {
5435 tree t = purpose_member (name, b->class_shadowed);
5436 if (t && TREE_VALUE (t)
e1cd6e56 5437 && DECL_CONTEXT (TREE_VALUE (t)) == type)
700f8a87
MS
5438 {
5439 val = TREE_VALUE (t);
5440 break;
5441 }
5442 }
5443 }
5566b478 5444 if (val == NULL_TREE)
e1cd6e56 5445 val = lookup_field (type, name, 0, 1);
700f8a87 5446 }
e1cd6e56 5447 else if (type == current_class_type)
a28e3c7f 5448 val = IDENTIFIER_CLASS_VALUE (name);
a28e3c7f 5449 else
d23a1bb1 5450 val = lookup_member (type, name, 0, prefer_type);
a28e3c7f 5451 }
e1cd6e56
MS
5452 else
5453 val = NULL_TREE;
5454
594740f3 5455 if (got_scope)
e1cd6e56 5456 goto done;
594740f3 5457 else if (got_object && val)
e1cd6e56 5458 from_obj = val;
a28e3c7f 5459 }
52c11ef6
JM
5460 else
5461 flags = lookup_flags (prefer_type, namespaces_only);
e76a2646 5462
d8f8dca1 5463 /* First, look in non-namespace scopes. */
d8f8dca1
MM
5464 for (val = IDENTIFIER_BINDING (name); val; val = TREE_CHAIN (val))
5465 {
5466 if (!LOCAL_BINDING_P (val) && nonclass)
5467 /* We're not looking for class-scoped bindings, so keep going. */
5468 continue;
5469
5470 /* If this is the kind of thing we're looking for, we're done. */
5471 if (qualify_lookup (BINDING_VALUE (val), flags))
5472 {
5473 val = BINDING_VALUE (val);
5474 break;
5475 }
5476 else if ((flags & LOOKUP_PREFER_TYPES)
5477 && qualify_lookup (BINDING_TYPE (val), flags))
5478 {
5479 val = BINDING_TYPE (val);
5480 break;
5481 }
5482 }
f181d4ae
MM
5483
5484 /* If VAL is a type from a dependent base, we're not really supposed
5485 to be able to see it; the fact that we can is the "implicit
5486 typename" extension. We call lookup_field here to turn VAL into
5487 a TYPE_DECL for a TYPENAME_TYPE. */
5488 if (processing_template_decl && val
5489 && val == IDENTIFIER_CLASS_VALUE (name)
5490 && TREE_CODE (val) == TYPE_DECL
5491 && !currently_open_class (DECL_CONTEXT (val))
5492 && uses_template_parms (current_class_type))
5493 val = lookup_field (current_class_type, name, 0, 1);
5494
f181d4ae
MM
5495 /* We don't put names from baseclasses onto the IDENTIFIER_BINDING
5496 list when we're defining a type. It would probably be simpler to
5497 do this, but we don't. So, we must lookup names from base
5498 classes explicitly. */
5499 if (!val && !nonclass
5500 && current_class_type && TYPE_BEING_DEFINED (current_class_type))
5501 {
5502 val = qualify_lookup (lookup_field (current_class_type, name, 0, 0),
5503 flags);
5504 if (!val)
5505 val = qualify_lookup (lookup_nested_field (name, !yylex),
5506 flags);
e76a2646 5507 }
f181d4ae
MM
5508
5509 /* If we found a type from a dependent base class (using the
d8f8dca1
MM
5510 implicit typename extension) make sure that there's not some
5511 global name which should be chosen instead. */
f181d4ae
MM
5512 if (val && TREE_CODE (val) == TYPE_DECL
5513 && IMPLICIT_TYPENAME_P (TREE_TYPE (val)))
e76a2646 5514 {
f181d4ae 5515 tree global_val;
8e52e063 5516
f181d4ae
MM
5517 /* Any other name takes precedence over an implicit typename. Warn the
5518 user about this potentially confusing lookup. */
5519 global_val = unqualified_namespace_lookup (name, flags);
8e52e063 5520
f181d4ae 5521 if (global_val)
c1def683 5522 {
5eea678f
JM
5523 tree subtype;
5524
d7b3753c
JM
5525 /* Only warn when not lexing; we don't want to warn if they
5526 use this name as a declarator. */
f181d4ae 5527 subtype = TREE_TYPE (TREE_TYPE (val));
d7b3753c 5528 if (! yylex
f181d4ae 5529 && ! (TREE_CODE (global_val) == TEMPLATE_DECL
5eea678f 5530 && CLASSTYPE_TEMPLATE_INFO (subtype)
f181d4ae
MM
5531 && CLASSTYPE_TI_TEMPLATE (subtype) == global_val)
5532 && ! (TREE_CODE (global_val) == TYPE_DECL
5533 && same_type_p (TREE_TYPE (global_val), subtype)))
c1def683 5534 {
f181d4ae 5535 cp_warning ("lookup of `%D' finds `%#D'", name, global_val);
24c65984 5536 cp_warning (" instead of `%D' from dependent base class",
f181d4ae 5537 val);
24c65984
JM
5538 cp_warning (" (use `typename %T::%D' if that's what you meant)",
5539 constructor_name (current_class_type), name);
c1def683 5540 }
f181d4ae
MM
5541
5542 /* Use the global value instead of the implicit typename. */
5543 val = global_val;
c1def683
JM
5544 }
5545 }
f181d4ae
MM
5546 else if (!val)
5547 /* No local, or class-scoped binding. Look for a namespace-scope
5548 declaration. */
5549 val = unqualified_namespace_lookup (name, flags);
c1def683 5550
a28e3c7f 5551 done:
8d08fdba
MS
5552 if (val)
5553 {
c91a56d2 5554 /* This should only warn about types used in qualified-ids. */
e1cd6e56 5555 if (from_obj && from_obj != val)
5566b478 5556 {
c91a56d2
MS
5557 if (looking_for_typename && TREE_CODE (from_obj) == TYPE_DECL
5558 && TREE_CODE (val) == TYPE_DECL
5559 && TREE_TYPE (from_obj) != TREE_TYPE (val))
5560 {
8251199e 5561 cp_pedwarn ("lookup of `%D' in the scope of `%#T' (`%#T')",
c91a56d2 5562 name, got_object, TREE_TYPE (from_obj));
8251199e 5563 cp_pedwarn (" does not match lookup in the current scope (`%#T')",
c91a56d2
MS
5564 TREE_TYPE (val));
5565 }
594740f3 5566
b8b1a3c1
JM
5567 /* We don't change val to from_obj if got_object depends on
5568 template parms because that breaks implicit typename for
5569 destructor calls. */
5570 if (! uses_template_parms (got_object))
53fdbb3b 5571 val = from_obj;
5566b478 5572 }
e1cd6e56 5573
0c64a9ca
JM
5574 /* If we have a single function from a using decl, pull it out. */
5575 if (TREE_CODE (val) == OVERLOAD && ! really_overloaded_fn (val))
5576 val = OVL_FUNCTION (val);
8d08fdba 5577 }
e1cd6e56
MS
5578 else if (from_obj)
5579 val = from_obj;
8d08fdba
MS
5580
5581 return val;
5582}
5583
700f8a87
MS
5584tree
5585lookup_name_nonclass (name)
5586 tree name;
5587{
3e3f722c 5588 return lookup_name_real (name, 0, 1, 0);
700f8a87
MS
5589}
5590
2c73f9f5
ML
5591tree
5592lookup_function_nonclass (name, args)
5593 tree name;
5594 tree args;
5595{
5596 return lookup_arg_dependent (name, lookup_name_nonclass (name), args);
5597}
5598
3e3f722c
ML
5599tree
5600lookup_name_namespace_only (name)
5601 tree name;
5602{
5603 /* type-or-namespace, nonclass, namespace_only */
5604 return lookup_name_real (name, 1, 1, 1);
5605}
5606
700f8a87
MS
5607tree
5608lookup_name (name, prefer_type)
5609 tree name;
5610 int prefer_type;
5611{
3e3f722c 5612 return lookup_name_real (name, prefer_type, 0, 0);
700f8a87
MS
5613}
5614
8d08fdba
MS
5615/* Similar to `lookup_name' but look only at current binding level. */
5616
5617tree
5618lookup_name_current_level (name)
5619 tree name;
5620{
5621 register tree t = NULL_TREE;
5622
2c73f9f5 5623 if (current_binding_level->namespace_p)
8d08fdba 5624 {
30394414 5625 t = IDENTIFIER_NAMESPACE_VALUE (name);
8d08fdba
MS
5626
5627 /* extern "C" function() */
5628 if (t != NULL_TREE && TREE_CODE (t) == TREE_LIST)
5629 t = TREE_VALUE (t);
5630 }
f181d4ae
MM
5631 else if (IDENTIFIER_BINDING (name)
5632 && LOCAL_BINDING_P (IDENTIFIER_BINDING (name)))
8d08fdba 5633 {
a4443a08 5634 struct binding_level *b = current_binding_level;
f181d4ae 5635
a4443a08
MS
5636 while (1)
5637 {
f181d4ae
MM
5638 if (BINDING_LEVEL (IDENTIFIER_BINDING (name)) == b)
5639 return IDENTIFIER_VALUE (name);
5640
9ed182dc
JM
5641 if (b->keep == 2)
5642 b = b->level_chain;
5643 else
5644 break;
5645 }
5646 }
5647
5648 return t;
5649}
5650
5651/* Like lookup_name_current_level, but for types. */
5652
5653tree
5654lookup_type_current_level (name)
5655 tree name;
5656{
5657 register tree t = NULL_TREE;
5658
5659 my_friendly_assert (! current_binding_level->namespace_p, 980716);
5660
5661 if (REAL_IDENTIFIER_TYPE_VALUE (name) != NULL_TREE
5662 && REAL_IDENTIFIER_TYPE_VALUE (name) != global_type_node)
5663 {
5664 struct binding_level *b = current_binding_level;
5665 while (1)
5666 {
5667 if (purpose_member (name, b->type_shadowed))
5668 return REAL_IDENTIFIER_TYPE_VALUE (name);
a4443a08
MS
5669 if (b->keep == 2)
5670 b = b->level_chain;
5671 else
5672 break;
5673 }
8d08fdba
MS
5674 }
5675
5676 return t;
5677}
3e3f722c
ML
5678
5679void
5680begin_only_namespace_names ()
5681{
5682 only_namespace_names = 1;
5683}
5684
5685void
5686end_only_namespace_names ()
5687{
5688 only_namespace_names = 0;
5689}
8d08fdba
MS
5690\f
5691/* Arrange for the user to get a source line number, even when the
5692 compiler is going down in flames, so that she at least has a
5693 chance of working around problems in the compiler. We used to
5694 call error(), but that let the segmentation fault continue
5695 through; now, it's much more passive by asking them to send the
5696 maintainers mail about the problem. */
5697
5698static void
5699signal_catch (sig)
7dee3f36 5700 int sig ATTRIBUTE_UNUSED;
8d08fdba
MS
5701{
5702 signal (SIGSEGV, SIG_DFL);
5703#ifdef SIGIOT
5704 signal (SIGIOT, SIG_DFL);
5705#endif
5706#ifdef SIGILL
5707 signal (SIGILL, SIG_DFL);
5708#endif
5709#ifdef SIGABRT
5710 signal (SIGABRT, SIG_DFL);
5711#endif
5712#ifdef SIGBUS
5713 signal (SIGBUS, SIG_DFL);
5714#endif
5715 my_friendly_abort (0);
5716}
5717
de22184b
MS
5718#if 0
5719/* Unused -- brendan 970107 */
8d08fdba
MS
5720/* Array for holding types considered "built-in". These types
5721 are output in the module in which `main' is defined. */
5722static tree *builtin_type_tdescs_arr;
5723static int builtin_type_tdescs_len, builtin_type_tdescs_max;
de22184b 5724#endif
8d08fdba
MS
5725
5726/* Push the declarations of builtin types into the namespace.
5727 RID_INDEX, if < RID_MAX is the index of the builtin type
5728 in the array RID_POINTERS. NAME is the name used when looking
5729 up the builtin type. TYPE is the _TYPE node for the builtin type. */
5730
5731static void
5732record_builtin_type (rid_index, name, type)
5733 enum rid rid_index;
5734 char *name;
5735 tree type;
5736{
5737 tree rname = NULL_TREE, tname = NULL_TREE;
a703fb38 5738 tree tdecl = NULL_TREE;
8d08fdba
MS
5739
5740 if ((int) rid_index < (int) RID_MAX)
5741 rname = ridpointers[(int) rid_index];
5742 if (name)
5743 tname = get_identifier (name);
5744
5745 TYPE_BUILT_IN (type) = 1;
5746
5747 if (tname)
5748 {
8d08fdba 5749 tdecl = pushdecl (build_decl (TYPE_DECL, tname, type));
8d08fdba
MS
5750 set_identifier_type_value (tname, NULL_TREE);
5751 if ((int) rid_index < (int) RID_MAX)
2c73f9f5
ML
5752 /* Built-in types live in the global namespace. */
5753 SET_IDENTIFIER_GLOBAL_VALUE (tname, tdecl);
8d08fdba
MS
5754 }
5755 if (rname != NULL_TREE)
5756 {
5757 if (tname != NULL_TREE)
5758 {
5759 set_identifier_type_value (rname, NULL_TREE);
2c73f9f5 5760 SET_IDENTIFIER_GLOBAL_VALUE (rname, tdecl);
8d08fdba
MS
5761 }
5762 else
5763 {
8d08fdba 5764 tdecl = pushdecl (build_decl (TYPE_DECL, rname, type));
8d08fdba
MS
5765 set_identifier_type_value (rname, NULL_TREE);
5766 }
5767 }
8d08fdba
MS
5768}
5769
eff71ab0
PB
5770/* Record one of the standard Java types.
5771 * Declare it as having the given NAME.
5772 * If SIZE > 0, it is the size of one of the integral types;
5773 * otherwise it is the negative of the size of one of the other types. */
5774
5775static tree
5776record_builtin_java_type (name, size)
5777 char *name;
5778 int size;
5779{
5780 tree type, decl;
5781 if (size > 0)
5782 type = make_signed_type (size);
5783 else if (size > -32)
5784 { /* "__java_char" or ""__java_boolean". */
5785 type = make_unsigned_type (-size);
5786 /*if (size == -1) TREE_SET_CODE (type, BOOLEAN_TYPE);*/
5787 }
5788 else
5789 { /* "__java_float" or ""__java_double". */
5790 type = make_node (REAL_TYPE);
5791 TYPE_PRECISION (type) = - size;
5792 layout_type (type);
5793 }
5794 record_builtin_type (RID_MAX, name, type);
5795 decl = TYPE_NAME (type);
5796 DECL_IGNORED_P (decl) = 1;
5797 TYPE_FOR_JAVA (type) = 1;
5798 return type;
5799}
5800
036407f7
ML
5801/* Push a type into the namespace so that the back-ends ignore it. */
5802
5803static void
5804record_unknown_type (type, name)
5805 tree type;
5806 char *name;
5807{
5808 tree decl = pushdecl (build_decl (TYPE_DECL, get_identifier (name), type));
5809 /* Make sure the "unknown type" typedecl gets ignored for debug info. */
5810 DECL_IGNORED_P (decl) = 1;
5811 TYPE_DECL_SUPPRESS_DEBUG (decl) = 1;
5812 TYPE_SIZE (type) = TYPE_SIZE (void_type_node);
5813 TYPE_ALIGN (type) = 1;
5814 TYPE_MODE (type) = TYPE_MODE (void_type_node);
5815}
5816
8d08fdba
MS
5817/* Push overloaded decl, in global scope, with one argument so it
5818 can be used as a callback from define_function. */
e92cc029 5819
8d08fdba
MS
5820static void
5821push_overloaded_decl_1 (x)
5822 tree x;
5823{
7bdbfa05 5824 push_overloaded_decl (x, PUSH_GLOBAL);
8d08fdba
MS
5825}
5826
8ccc31eb
MS
5827#ifdef __GNUC__
5828__inline
5829#endif
6b5fbb55
MS
5830tree
5831auto_function (name, type, code)
8ccc31eb
MS
5832 tree name, type;
5833 enum built_in_function code;
5834{
5835 return define_function
49c249e1 5836 (IDENTIFIER_POINTER (name), type, code, push_overloaded_decl_1,
8ccc31eb
MS
5837 IDENTIFIER_POINTER (build_decl_overload (name, TYPE_ARG_TYPES (type),
5838 0)));
5839}
5840
8d08fdba
MS
5841/* Create the predefined scalar types of C,
5842 and some nodes representing standard constants (0, 1, (void *)0).
5843 Initialize the global binding level.
5844 Make definitions for built-in primitive functions. */
5845
5846void
5847init_decl_processing ()
5848{
de22184b 5849 register tree endlink, int_endlink, double_endlink, unsigned_endlink;
8d08fdba 5850 tree fields[20];
8d08fdba 5851 /* Data type of memcpy. */
cffa8729 5852 tree memcpy_ftype, strlen_ftype;
8d08fdba
MS
5853 int wchar_type_size;
5854 tree temp;
5855 tree array_domain_type;
a703fb38 5856 tree vb_off_identifier = NULL_TREE;
5566b478
MS
5857 /* Function type `char *(char *, char *)' and similar ones */
5858 tree string_ftype_ptr_ptr, int_ftype_string_string;
de22184b
MS
5859 tree sizetype_endlink;
5860 tree ptr_ftype, ptr_ftype_unsigned, ptr_ftype_sizetype;
5eea678f 5861 tree void_ftype, void_ftype_int, void_ftype_ptr;
8d08fdba
MS
5862
5863 /* Have to make these distinct before we try using them. */
5864 lang_name_cplusplus = get_identifier ("C++");
5865 lang_name_c = get_identifier ("C");
a1774733 5866 lang_name_java = get_identifier ("Java");
8d08fdba 5867
2c73f9f5 5868 /* Enter the global namespace. */
30394414
JM
5869 my_friendly_assert (global_namespace == NULL_TREE, 375);
5870 my_friendly_assert (current_lang_name == NULL_TREE, 375);
5871 current_lang_name = lang_name_cplusplus;
5872 push_namespace (get_identifier ("::"));
5873 global_namespace = current_namespace;
5874 current_lang_name = NULL_TREE;
5875
e1cd6e56 5876 if (flag_strict_prototype == 2)
830fcda8 5877 flag_strict_prototype = pedantic;
2642b9bf
JM
5878 if (! flag_permissive && ! pedantic)
5879 flag_pedantic_errors = 1;
830fcda8
JM
5880
5881 strict_prototypes_lang_c = flag_strict_prototype;
8926095f 5882
8d08fdba
MS
5883 /* Initially, C. */
5884 current_lang_name = lang_name_c;
5885
5886 current_function_decl = NULL_TREE;
5887 named_labels = NULL_TREE;
e349ee73 5888 named_label_uses = NULL;
8d08fdba
MS
5889 current_binding_level = NULL_BINDING_LEVEL;
5890 free_binding_level = NULL_BINDING_LEVEL;
5891
5892 /* Because most segmentation signals can be traced back into user
5893 code, catch them and at least give the user a chance of working
e92cc029 5894 around compiler bugs. */
8d08fdba
MS
5895 signal (SIGSEGV, signal_catch);
5896
5897 /* We will also catch aborts in the back-end through signal_catch and
5898 give the user a chance to see where the error might be, and to defeat
5899 aborts in the back-end when there have been errors previously in their
e92cc029 5900 code. */
8d08fdba
MS
5901#ifdef SIGIOT
5902 signal (SIGIOT, signal_catch);
5903#endif
5904#ifdef SIGILL
5905 signal (SIGILL, signal_catch);
5906#endif
5907#ifdef SIGABRT
5908 signal (SIGABRT, signal_catch);
5909#endif
5910#ifdef SIGBUS
5911 signal (SIGBUS, signal_catch);
5912#endif
5913
5914 gcc_obstack_init (&decl_obstack);
8d08fdba
MS
5915
5916 /* Must lay these out before anything else gets laid out. */
5917 error_mark_node = make_node (ERROR_MARK);
5918 TREE_PERMANENT (error_mark_node) = 1;
5919 TREE_TYPE (error_mark_node) = error_mark_node;
5920 error_mark_list = build_tree_list (error_mark_node, error_mark_node);
5921 TREE_TYPE (error_mark_list) = error_mark_node;
5922
a28e3c7f
MS
5923 /* Make the binding_level structure for global names. */
5924 pushlevel (0);
8d08fdba 5925 global_binding_level = current_binding_level;
2c73f9f5
ML
5926 /* The global level is the namespace level of ::. */
5927 NAMESPACE_LEVEL (global_namespace) = global_binding_level;
5928 declare_namespace_level ();
8d08fdba
MS
5929
5930 this_identifier = get_identifier (THIS_NAME);
5931 in_charge_identifier = get_identifier (IN_CHARGE_NAME);
fc378698
MS
5932 ctor_identifier = get_identifier (CTOR_NAME);
5933 dtor_identifier = get_identifier (DTOR_NAME);
8d08fdba
MS
5934 pfn_identifier = get_identifier (VTABLE_PFN_NAME);
5935 index_identifier = get_identifier (VTABLE_INDEX_NAME);
5936 delta_identifier = get_identifier (VTABLE_DELTA_NAME);
5937 delta2_identifier = get_identifier (VTABLE_DELTA2_NAME);
5938 pfn_or_delta2_identifier = get_identifier ("__pfn_or_delta2");
dff6b454
RK
5939 if (flag_handle_signatures)
5940 {
5941 tag_identifier = get_identifier (SIGTABLE_TAG_NAME);
9dd70aa4
GB
5942 vb_off_identifier = get_identifier (SIGTABLE_VB_OFF_NAME);
5943 vt_off_identifier = get_identifier (SIGTABLE_VT_OFF_NAME);
dff6b454 5944 }
8d08fdba
MS
5945
5946 /* Define `int' and `char' first so that dbx will output them first. */
5947
5948 integer_type_node = make_signed_type (INT_TYPE_SIZE);
5949 record_builtin_type (RID_INT, NULL_PTR, integer_type_node);
5950
5951 /* Define `char', which is like either `signed char' or `unsigned char'
5952 but not the same as either. */
5953
beb53fb8
JM
5954 char_type_node
5955 = (flag_signed_char
5956 ? make_signed_type (CHAR_TYPE_SIZE)
5957 : make_unsigned_type (CHAR_TYPE_SIZE));
8d08fdba
MS
5958 record_builtin_type (RID_CHAR, "char", char_type_node);
5959
5960 long_integer_type_node = make_signed_type (LONG_TYPE_SIZE);
5961 record_builtin_type (RID_LONG, "long int", long_integer_type_node);
5962
5963 unsigned_type_node = make_unsigned_type (INT_TYPE_SIZE);
5964 record_builtin_type (RID_UNSIGNED, "unsigned int", unsigned_type_node);
5965
5966 long_unsigned_type_node = make_unsigned_type (LONG_TYPE_SIZE);
5967 record_builtin_type (RID_MAX, "long unsigned int", long_unsigned_type_node);
5968 record_builtin_type (RID_MAX, "unsigned long", long_unsigned_type_node);
5969
5970 long_long_integer_type_node = make_signed_type (LONG_LONG_TYPE_SIZE);
5971 record_builtin_type (RID_MAX, "long long int", long_long_integer_type_node);
5972
5973 long_long_unsigned_type_node = make_unsigned_type (LONG_LONG_TYPE_SIZE);
5974 record_builtin_type (RID_MAX, "long long unsigned int",
5975 long_long_unsigned_type_node);
5976 record_builtin_type (RID_MAX, "long long unsigned",
5977 long_long_unsigned_type_node);
5978
5156628f
MS
5979 short_integer_type_node = make_signed_type (SHORT_TYPE_SIZE);
5980 record_builtin_type (RID_SHORT, "short int", short_integer_type_node);
5981 short_unsigned_type_node = make_unsigned_type (SHORT_TYPE_SIZE);
5982 record_builtin_type (RID_MAX, "short unsigned int", short_unsigned_type_node);
5983 record_builtin_type (RID_MAX, "unsigned short", short_unsigned_type_node);
5984
8d08fdba 5985 /* `unsigned long' is the standard type for sizeof.
8d08fdba 5986 Note that stddef.h uses `unsigned long',
f5426d1e
R
5987 and this must agree, even if long and int are the same size. */
5988 set_sizetype
5989 (TREE_TYPE (IDENTIFIER_GLOBAL_VALUE (get_identifier (SIZE_TYPE))));
8d08fdba
MS
5990
5991 ptrdiff_type_node
5992 = TREE_TYPE (IDENTIFIER_GLOBAL_VALUE (get_identifier (PTRDIFF_TYPE)));
5993
8d08fdba
MS
5994 /* Define both `signed char' and `unsigned char'. */
5995 signed_char_type_node = make_signed_type (CHAR_TYPE_SIZE);
5996 record_builtin_type (RID_MAX, "signed char", signed_char_type_node);
5997 unsigned_char_type_node = make_unsigned_type (CHAR_TYPE_SIZE);
5998 record_builtin_type (RID_MAX, "unsigned char", unsigned_char_type_node);
5999
6000 /* These are types that type_for_size and type_for_mode use. */
6001 intQI_type_node = make_signed_type (GET_MODE_BITSIZE (QImode));
6002 pushdecl (build_decl (TYPE_DECL, NULL_TREE, intQI_type_node));
6003 intHI_type_node = make_signed_type (GET_MODE_BITSIZE (HImode));
6004 pushdecl (build_decl (TYPE_DECL, NULL_TREE, intHI_type_node));
6005 intSI_type_node = make_signed_type (GET_MODE_BITSIZE (SImode));
6006 pushdecl (build_decl (TYPE_DECL, NULL_TREE, intSI_type_node));
6007 intDI_type_node = make_signed_type (GET_MODE_BITSIZE (DImode));
6008 pushdecl (build_decl (TYPE_DECL, NULL_TREE, intDI_type_node));
946dc1c8 6009#if HOST_BITS_PER_WIDE_INT >= 64
f7554e8c 6010 intTI_type_node = make_signed_type (GET_MODE_BITSIZE (TImode));
5ebcdddb 6011 pushdecl (build_decl (TYPE_DECL, get_identifier ("__int128_t"), intTI_type_node));
946dc1c8 6012#endif
8d08fdba
MS
6013 unsigned_intQI_type_node = make_unsigned_type (GET_MODE_BITSIZE (QImode));
6014 pushdecl (build_decl (TYPE_DECL, NULL_TREE, unsigned_intQI_type_node));
6015 unsigned_intHI_type_node = make_unsigned_type (GET_MODE_BITSIZE (HImode));
6016 pushdecl (build_decl (TYPE_DECL, NULL_TREE, unsigned_intHI_type_node));
6017 unsigned_intSI_type_node = make_unsigned_type (GET_MODE_BITSIZE (SImode));
6018 pushdecl (build_decl (TYPE_DECL, NULL_TREE, unsigned_intSI_type_node));
6019 unsigned_intDI_type_node = make_unsigned_type (GET_MODE_BITSIZE (DImode));
6020 pushdecl (build_decl (TYPE_DECL, NULL_TREE, unsigned_intDI_type_node));
946dc1c8 6021#if HOST_BITS_PER_WIDE_INT >= 64
f7554e8c 6022 unsigned_intTI_type_node = make_unsigned_type (GET_MODE_BITSIZE (TImode));
5ebcdddb 6023 pushdecl (build_decl (TYPE_DECL, get_identifier ("__uint128_t"), unsigned_intTI_type_node));
946dc1c8 6024#endif
8d08fdba
MS
6025
6026 float_type_node = make_node (REAL_TYPE);
6027 TYPE_PRECISION (float_type_node) = FLOAT_TYPE_SIZE;
6028 record_builtin_type (RID_FLOAT, NULL_PTR, float_type_node);
6029 layout_type (float_type_node);
6030
6031 double_type_node = make_node (REAL_TYPE);
6032 if (flag_short_double)
6033 TYPE_PRECISION (double_type_node) = FLOAT_TYPE_SIZE;
6034 else
6035 TYPE_PRECISION (double_type_node) = DOUBLE_TYPE_SIZE;
6036 record_builtin_type (RID_DOUBLE, NULL_PTR, double_type_node);
6037 layout_type (double_type_node);
6038
6039 long_double_type_node = make_node (REAL_TYPE);
6040 TYPE_PRECISION (long_double_type_node) = LONG_DOUBLE_TYPE_SIZE;
6041 record_builtin_type (RID_MAX, "long double", long_double_type_node);
6042 layout_type (long_double_type_node);
6043
37c46b43
MS
6044 complex_integer_type_node = make_node (COMPLEX_TYPE);
6045 pushdecl (build_decl (TYPE_DECL, get_identifier ("complex int"),
6046 complex_integer_type_node));
6047 TREE_TYPE (complex_integer_type_node) = integer_type_node;
6048 layout_type (complex_integer_type_node);
6049
6050 complex_float_type_node = make_node (COMPLEX_TYPE);
6051 pushdecl (build_decl (TYPE_DECL, get_identifier ("complex float"),
6052 complex_float_type_node));
6053 TREE_TYPE (complex_float_type_node) = float_type_node;
6054 layout_type (complex_float_type_node);
6055
6056 complex_double_type_node = make_node (COMPLEX_TYPE);
6057 pushdecl (build_decl (TYPE_DECL, get_identifier ("complex double"),
6058 complex_double_type_node));
6059 TREE_TYPE (complex_double_type_node) = double_type_node;
6060 layout_type (complex_double_type_node);
6061
6062 complex_long_double_type_node = make_node (COMPLEX_TYPE);
6063 pushdecl (build_decl (TYPE_DECL, get_identifier ("complex long double"),
6064 complex_long_double_type_node));
6065 TREE_TYPE (complex_long_double_type_node) = long_double_type_node;
6066 layout_type (complex_long_double_type_node);
6067
eff71ab0
PB
6068 java_byte_type_node = record_builtin_java_type ("__java_byte", 8);
6069 java_short_type_node = record_builtin_java_type ("__java_short", 16);
6070 java_int_type_node = record_builtin_java_type ("__java_int", 32);
6071 java_long_type_node = record_builtin_java_type ("__java_long", 64);
6072 java_float_type_node = record_builtin_java_type ("__java_float", -32);
6073 java_double_type_node = record_builtin_java_type ("__java_double", -64);
6074 java_char_type_node = record_builtin_java_type ("__java_char", -16);
6075 java_boolean_type_node = record_builtin_java_type ("__java_boolean", -1);
6076
8d08fdba
MS
6077 integer_zero_node = build_int_2 (0, 0);
6078 TREE_TYPE (integer_zero_node) = integer_type_node;
6079 integer_one_node = build_int_2 (1, 0);
6080 TREE_TYPE (integer_one_node) = integer_type_node;
6081 integer_two_node = build_int_2 (2, 0);
6082 TREE_TYPE (integer_two_node) = integer_type_node;
6083 integer_three_node = build_int_2 (3, 0);
6084 TREE_TYPE (integer_three_node) = integer_type_node;
8d08fdba 6085
255512c1
JM
6086 boolean_type_node = make_unsigned_type (BOOL_TYPE_SIZE);
6087 TREE_SET_CODE (boolean_type_node, BOOLEAN_TYPE);
c1ea6a0b
BK
6088 TYPE_MAX_VALUE (boolean_type_node) = build_int_2 (1, 0);
6089 TREE_TYPE (TYPE_MAX_VALUE (boolean_type_node)) = boolean_type_node;
6090 TYPE_PRECISION (boolean_type_node) = 1;
255512c1
JM
6091 record_builtin_type (RID_BOOL, "bool", boolean_type_node);
6092 boolean_false_node = build_int_2 (0, 0);
6093 TREE_TYPE (boolean_false_node) = boolean_type_node;
6094 boolean_true_node = build_int_2 (1, 0);
6095 TREE_TYPE (boolean_true_node) = boolean_type_node;
2986ae00 6096
8d08fdba
MS
6097 /* These are needed by stor-layout.c. */
6098 size_zero_node = size_int (0);
6099 size_one_node = size_int (1);
6100
e92cc029
MS
6101 signed_size_zero_node = build_int_2 (0, 0);
6102 TREE_TYPE (signed_size_zero_node) = make_signed_type (TYPE_PRECISION (sizetype));
6103
8d08fdba
MS
6104 void_type_node = make_node (VOID_TYPE);
6105 record_builtin_type (RID_VOID, NULL_PTR, void_type_node);
6106 layout_type (void_type_node); /* Uses integer_zero_node. */
6107 void_list_node = build_tree_list (NULL_TREE, void_type_node);
6108 TREE_PARMLIST (void_list_node) = 1;
6109
d11ad92e 6110 null_pointer_node = build_int_2 (0, 0);
8d08fdba 6111 TREE_TYPE (null_pointer_node) = build_pointer_type (void_type_node);
d11ad92e
MS
6112 layout_type (TREE_TYPE (null_pointer_node));
6113
8d08fdba
MS
6114 /* Used for expressions that do nothing, but are not errors. */
6115 void_zero_node = build_int_2 (0, 0);
6116 TREE_TYPE (void_zero_node) = void_type_node;
6117
6118 string_type_node = build_pointer_type (char_type_node);
beb53fb8 6119 const_string_type_node
91063b51
MM
6120 = build_pointer_type (build_qualified_type (char_type_node,
6121 TYPE_QUAL_CONST));
6b5fbb55 6122#if 0
8d08fdba 6123 record_builtin_type (RID_MAX, NULL_PTR, string_type_node);
6b5fbb55 6124#endif
8d08fdba
MS
6125
6126 /* Make a type to be the domain of a few array types
6127 whose domains don't really matter.
6128 200 is small enough that it always fits in size_t
6129 and large enough that it can hold most function names for the
6130 initializations of __FUNCTION__ and __PRETTY_FUNCTION__. */
6131 array_domain_type = build_index_type (build_int_2 (200, 0));
6132
2c73f9f5 6133 /* Make a type for arrays of characters.
8d08fdba
MS
6134 With luck nothing will ever really depend on the length of this
6135 array type. */
6136 char_array_type_node
6137 = build_array_type (char_type_node, array_domain_type);
6138 /* Likewise for arrays of ints. */
6139 int_array_type_node
6140 = build_array_type (integer_type_node, array_domain_type);
6141
6142 /* This is just some anonymous class type. Nobody should ever
6143 need to look inside this envelope. */
6144 class_star_type_node = build_pointer_type (make_lang_type (RECORD_TYPE));
6145
6146 default_function_type
6147 = build_function_type (integer_type_node, NULL_TREE);
8d08fdba
MS
6148
6149 ptr_type_node = build_pointer_type (void_type_node);
beb53fb8 6150 const_ptr_type_node
91063b51
MM
6151 = build_pointer_type (build_qualified_type (void_type_node,
6152 TYPE_QUAL_CONST));
6b5fbb55 6153#if 0
8d08fdba 6154 record_builtin_type (RID_MAX, NULL_PTR, ptr_type_node);
6b5fbb55 6155#endif
8d08fdba
MS
6156 endlink = void_list_node;
6157 int_endlink = tree_cons (NULL_TREE, integer_type_node, endlink);
6158 double_endlink = tree_cons (NULL_TREE, double_type_node, endlink);
de22184b
MS
6159 unsigned_endlink = tree_cons (NULL_TREE, unsigned_type_node, endlink);
6160
6161 ptr_ftype = build_function_type (ptr_type_node, NULL_TREE);
6162 ptr_ftype_unsigned = build_function_type (ptr_type_node, unsigned_endlink);
6163 sizetype_endlink = tree_cons (NULL_TREE, sizetype, endlink);
6164 /* We realloc here because sizetype could be int or unsigned. S'ok. */
6165 ptr_ftype_sizetype = build_function_type (ptr_type_node, sizetype_endlink);
6166
6167 void_ftype = build_function_type (void_type_node, endlink);
6168 void_ftype_int = build_function_type (void_type_node, int_endlink);
6169 void_ftype_ptr
6170 = build_function_type (void_type_node,
6171 tree_cons (NULL_TREE, ptr_type_node, endlink));
824b9a4c
MS
6172 void_ftype_ptr
6173 = build_exception_variant (void_ftype_ptr,
6174 tree_cons (NULL_TREE, NULL_TREE, NULL_TREE));
8d08fdba 6175
cffa8729
MS
6176 float_ftype_float
6177 = build_function_type (float_type_node,
6178 tree_cons (NULL_TREE, float_type_node, endlink));
6179
8d08fdba
MS
6180 double_ftype_double
6181 = build_function_type (double_type_node, double_endlink);
6182
cffa8729
MS
6183 ldouble_ftype_ldouble
6184 = build_function_type (long_double_type_node,
6185 tree_cons (NULL_TREE, long_double_type_node,
6186 endlink));
6187
8d08fdba
MS
6188 double_ftype_double_double
6189 = build_function_type (double_type_node,
a28e3c7f
MS
6190 tree_cons (NULL_TREE, double_type_node,
6191 double_endlink));
8d08fdba
MS
6192
6193 int_ftype_int
6194 = build_function_type (integer_type_node, int_endlink);
6195
6196 long_ftype_long
6197 = build_function_type (long_integer_type_node,
a28e3c7f
MS
6198 tree_cons (NULL_TREE, long_integer_type_node,
6199 endlink));
8d08fdba 6200
8d08fdba
MS
6201 int_ftype_cptr_cptr_sizet
6202 = build_function_type (integer_type_node,
6203 tree_cons (NULL_TREE, const_ptr_type_node,
6204 tree_cons (NULL_TREE, const_ptr_type_node,
6205 tree_cons (NULL_TREE,
6206 sizetype,
6207 endlink))));
6208
8d08fdba
MS
6209 string_ftype_ptr_ptr /* strcpy prototype */
6210 = build_function_type (string_type_node,
6211 tree_cons (NULL_TREE, string_type_node,
6212 tree_cons (NULL_TREE,
6213 const_string_type_node,
6214 endlink)));
6215
8d08fdba
MS
6216 int_ftype_string_string /* strcmp prototype */
6217 = build_function_type (integer_type_node,
6218 tree_cons (NULL_TREE, const_string_type_node,
6219 tree_cons (NULL_TREE,
6220 const_string_type_node,
6221 endlink)));
6222
cffa8729 6223 strlen_ftype /* strlen prototype */
8d08fdba
MS
6224 = build_function_type (sizetype,
6225 tree_cons (NULL_TREE, const_string_type_node,
6226 endlink));
6227
8d08fdba 6228 memcpy_ftype /* memcpy prototype */
d22c8596 6229 = build_function_type (ptr_type_node,
8d08fdba
MS
6230 tree_cons (NULL_TREE, ptr_type_node,
6231 tree_cons (NULL_TREE, const_ptr_type_node,
de22184b 6232 sizetype_endlink)));
8d08fdba
MS
6233
6234 if (flag_huge_objects)
6235 delta_type_node = long_integer_type_node;
6236 else
6237 delta_type_node = short_integer_type_node;
6238
b9620d0e 6239 builtin_function ("__builtin_constant_p", default_function_type,
8d08fdba
MS
6240 BUILT_IN_CONSTANT_P, NULL_PTR);
6241
beb53fb8
JM
6242 builtin_return_address_fndecl
6243 = builtin_function ("__builtin_return_address", ptr_ftype_unsigned,
6244 BUILT_IN_RETURN_ADDRESS, NULL_PTR);
8926095f 6245
de22184b 6246 builtin_function ("__builtin_frame_address", ptr_ftype_unsigned,
8926095f
MS
6247 BUILT_IN_FRAME_ADDRESS, NULL_PTR);
6248
de22184b 6249 builtin_function ("__builtin_alloca", ptr_ftype_sizetype,
8d08fdba 6250 BUILT_IN_ALLOCA, "alloca");
cffa8729 6251 builtin_function ("__builtin_ffs", int_ftype_int, BUILT_IN_FFS, NULL_PTR);
00595019
MS
6252 /* Define alloca, ffs as builtins.
6253 Declare _exit just to mark it as volatile. */
6254 if (! flag_no_builtin && !flag_no_nonansi_builtin)
6255 {
de22184b 6256 temp = builtin_function ("alloca", ptr_ftype_sizetype,
00595019
MS
6257 BUILT_IN_ALLOCA, NULL_PTR);
6258 /* Suppress error if redefined as a non-function. */
6259 DECL_BUILT_IN_NONANSI (temp) = 1;
00595019
MS
6260 temp = builtin_function ("ffs", int_ftype_int, BUILT_IN_FFS, NULL_PTR);
6261 /* Suppress error if redefined as a non-function. */
6262 DECL_BUILT_IN_NONANSI (temp) = 1;
de22184b 6263 temp = builtin_function ("_exit", void_ftype_int,
00595019
MS
6264 NOT_BUILT_IN, NULL_PTR);
6265 TREE_THIS_VOLATILE (temp) = 1;
6266 TREE_SIDE_EFFECTS (temp) = 1;
6267 /* Suppress error if redefined as a non-function. */
6268 DECL_BUILT_IN_NONANSI (temp) = 1;
6269 }
8d08fdba 6270
cffa8729
MS
6271 builtin_function ("__builtin_abs", int_ftype_int, BUILT_IN_ABS, NULL_PTR);
6272 builtin_function ("__builtin_fabsf", float_ftype_float, BUILT_IN_FABS,
6273 NULL_PTR);
6274 builtin_function ("__builtin_fabs", double_ftype_double, BUILT_IN_FABS,
6275 NULL_PTR);
6276 builtin_function ("__builtin_fabsl", ldouble_ftype_ldouble, BUILT_IN_FABS,
6277 NULL_PTR);
8d08fdba
MS
6278 builtin_function ("__builtin_labs", long_ftype_long,
6279 BUILT_IN_LABS, NULL_PTR);
de22184b 6280 builtin_function ("__builtin_saveregs", ptr_ftype,
8d08fdba 6281 BUILT_IN_SAVEREGS, NULL_PTR);
8d08fdba
MS
6282 builtin_function ("__builtin_classify_type", default_function_type,
6283 BUILT_IN_CLASSIFY_TYPE, NULL_PTR);
de22184b 6284 builtin_function ("__builtin_next_arg", ptr_ftype,
8d08fdba 6285 BUILT_IN_NEXT_ARG, NULL_PTR);
de22184b 6286 builtin_function ("__builtin_args_info", int_ftype_int,
8d08fdba 6287 BUILT_IN_ARGS_INFO, NULL_PTR);
eb66be0e
MS
6288 builtin_function ("__builtin_setjmp",
6289 build_function_type (integer_type_node,
6290 tree_cons (NULL_TREE, ptr_type_node,
6291 endlink)),
6292 BUILT_IN_SETJMP, NULL_PTR);
6293 builtin_function ("__builtin_longjmp",
6294 build_function_type (integer_type_node,
6295 tree_cons (NULL_TREE, ptr_type_node,
6296 tree_cons (NULL_TREE,
6297 integer_type_node,
6298 endlink))),
6299 BUILT_IN_LONGJMP, NULL_PTR);
8d08fdba
MS
6300
6301 /* Untyped call and return. */
de22184b 6302 builtin_function ("__builtin_apply_args", ptr_ftype,
8d08fdba
MS
6303 BUILT_IN_APPLY_ARGS, NULL_PTR);
6304
6305 temp = tree_cons (NULL_TREE,
6306 build_pointer_type (build_function_type (void_type_node,
6307 NULL_TREE)),
2c73f9f5 6308 tree_cons (NULL_TREE, ptr_ftype_sizetype, NULL_TREE));
8d08fdba
MS
6309 builtin_function ("__builtin_apply",
6310 build_function_type (ptr_type_node, temp),
6311 BUILT_IN_APPLY, NULL_PTR);
de22184b 6312 builtin_function ("__builtin_return", void_ftype_ptr,
8d08fdba
MS
6313 BUILT_IN_RETURN, NULL_PTR);
6314
6315 /* Currently under experimentation. */
6316 builtin_function ("__builtin_memcpy", memcpy_ftype,
6317 BUILT_IN_MEMCPY, "memcpy");
6318 builtin_function ("__builtin_memcmp", int_ftype_cptr_cptr_sizet,
6319 BUILT_IN_MEMCMP, "memcmp");
6320 builtin_function ("__builtin_strcmp", int_ftype_string_string,
6321 BUILT_IN_STRCMP, "strcmp");
6322 builtin_function ("__builtin_strcpy", string_ftype_ptr_ptr,
6323 BUILT_IN_STRCPY, "strcpy");
cffa8729 6324 builtin_function ("__builtin_strlen", strlen_ftype,
8d08fdba 6325 BUILT_IN_STRLEN, "strlen");
cffa8729
MS
6326 builtin_function ("__builtin_sqrtf", float_ftype_float,
6327 BUILT_IN_FSQRT, "sqrtf");
6328 builtin_function ("__builtin_fsqrt", double_ftype_double,
6329 BUILT_IN_FSQRT, NULL_PTR);
6330 builtin_function ("__builtin_sqrtl", ldouble_ftype_ldouble,
6331 BUILT_IN_FSQRT, "sqrtl");
6332 builtin_function ("__builtin_sinf", float_ftype_float,
6333 BUILT_IN_SIN, "sinf");
6334 builtin_function ("__builtin_sin", double_ftype_double,
6335 BUILT_IN_SIN, "sin");
6336 builtin_function ("__builtin_sinl", ldouble_ftype_ldouble,
6337 BUILT_IN_SIN, "sinl");
6338 builtin_function ("__builtin_cosf", float_ftype_float,
6339 BUILT_IN_COS, "cosf");
6340 builtin_function ("__builtin_cos", double_ftype_double,
6341 BUILT_IN_COS, "cos");
6342 builtin_function ("__builtin_cosl", ldouble_ftype_ldouble,
6343 BUILT_IN_COS, "cosl");
8d08fdba
MS
6344
6345 if (!flag_no_builtin)
6346 {
8d08fdba
MS
6347 builtin_function ("abs", int_ftype_int, BUILT_IN_ABS, NULL_PTR);
6348 builtin_function ("fabs", double_ftype_double, BUILT_IN_FABS, NULL_PTR);
6349 builtin_function ("labs", long_ftype_long, BUILT_IN_LABS, NULL_PTR);
cffa8729
MS
6350 builtin_function ("fabsf", float_ftype_float, BUILT_IN_FABS, NULL_PTR);
6351 builtin_function ("fabsl", ldouble_ftype_ldouble, BUILT_IN_FABS,
6352 NULL_PTR);
8d08fdba
MS
6353 builtin_function ("memcpy", memcpy_ftype, BUILT_IN_MEMCPY, NULL_PTR);
6354 builtin_function ("memcmp", int_ftype_cptr_cptr_sizet, BUILT_IN_MEMCMP,
6355 NULL_PTR);
cffa8729
MS
6356 builtin_function ("strcmp", int_ftype_string_string, BUILT_IN_STRCMP,
6357 NULL_PTR);
a28e3c7f
MS
6358 builtin_function ("strcpy", string_ftype_ptr_ptr, BUILT_IN_STRCPY,
6359 NULL_PTR);
cffa8729
MS
6360 builtin_function ("strlen", strlen_ftype, BUILT_IN_STRLEN, NULL_PTR);
6361 builtin_function ("sqrtf", float_ftype_float, BUILT_IN_FSQRT, NULL_PTR);
6362 builtin_function ("sqrt", double_ftype_double, BUILT_IN_FSQRT, NULL_PTR);
6363 builtin_function ("sqrtl", ldouble_ftype_ldouble, BUILT_IN_FSQRT,
6364 NULL_PTR);
6365 builtin_function ("sinf", float_ftype_float, BUILT_IN_SIN, NULL_PTR);
8d08fdba 6366 builtin_function ("sin", double_ftype_double, BUILT_IN_SIN, NULL_PTR);
cffa8729
MS
6367 builtin_function ("sinl", ldouble_ftype_ldouble, BUILT_IN_SIN, NULL_PTR);
6368 builtin_function ("cosf", float_ftype_float, BUILT_IN_COS, NULL_PTR);
8d08fdba 6369 builtin_function ("cos", double_ftype_double, BUILT_IN_COS, NULL_PTR);
cffa8729 6370 builtin_function ("cosl", ldouble_ftype_ldouble, BUILT_IN_COS, NULL_PTR);
00595019
MS
6371
6372 /* Declare these functions volatile
6373 to avoid spurious "control drops through" warnings. */
de22184b 6374 temp = builtin_function ("abort", void_ftype,
00595019
MS
6375 NOT_BUILT_IN, NULL_PTR);
6376 TREE_THIS_VOLATILE (temp) = 1;
6377 TREE_SIDE_EFFECTS (temp) = 1;
6378 /* Well, these are actually ANSI, but we can't set DECL_BUILT_IN on
6379 them... */
6380 DECL_BUILT_IN_NONANSI (temp) = 1;
de22184b 6381 temp = builtin_function ("exit", void_ftype_int,
00595019
MS
6382 NOT_BUILT_IN, NULL_PTR);
6383 TREE_THIS_VOLATILE (temp) = 1;
6384 TREE_SIDE_EFFECTS (temp) = 1;
6385 DECL_BUILT_IN_NONANSI (temp) = 1;
8d08fdba
MS
6386 }
6387
6388#if 0
6389 /* Support for these has not been written in either expand_builtin
6390 or build_function_call. */
72b7eeff
MS
6391 builtin_function ("__builtin_div", default_ftype, BUILT_IN_DIV, NULL_PTR);
6392 builtin_function ("__builtin_ldiv", default_ftype, BUILT_IN_LDIV, NULL_PTR);
a28e3c7f 6393 builtin_function ("__builtin_ffloor", double_ftype_double, BUILT_IN_FFLOOR,
72b7eeff 6394 NULL_PTR);
cffa8729
MS
6395 builtin_function ("__builtin_fceil", double_ftype_double, BUILT_IN_FCEIL,
6396 NULL_PTR);
a28e3c7f 6397 builtin_function ("__builtin_fmod", double_ftype_double_double,
72b7eeff 6398 BUILT_IN_FMOD, NULL_PTR);
a28e3c7f 6399 builtin_function ("__builtin_frem", double_ftype_double_double,
72b7eeff 6400 BUILT_IN_FREM, NULL_PTR);
cffa8729
MS
6401 builtin_function ("__builtin_memset", ptr_ftype_ptr_int_int,
6402 BUILT_IN_MEMSET, NULL_PTR);
a28e3c7f 6403 builtin_function ("__builtin_getexp", double_ftype_double, BUILT_IN_GETEXP,
72b7eeff 6404 NULL_PTR);
a28e3c7f 6405 builtin_function ("__builtin_getman", double_ftype_double, BUILT_IN_GETMAN,
72b7eeff 6406 NULL_PTR);
8d08fdba
MS
6407#endif
6408
6409 /* C++ extensions */
6410
6411 unknown_type_node = make_node (UNKNOWN_TYPE);
036407f7
ML
6412 record_unknown_type (unknown_type_node, "unknown type");
6413
8d08fdba
MS
6414 /* Indirecting an UNKNOWN_TYPE node yields an UNKNOWN_TYPE node. */
6415 TREE_TYPE (unknown_type_node) = unknown_type_node;
a6967cc0 6416
03d0f4af 6417 TREE_TYPE (null_node) = type_for_size (POINTER_SIZE, 0);
a6967cc0
JM
6418
6419 /* Looking up TYPE_POINTER_TO and TYPE_REFERENCE_TO yield the same
6420 result. */
8d08fdba
MS
6421 TYPE_POINTER_TO (unknown_type_node) = unknown_type_node;
6422 TYPE_REFERENCE_TO (unknown_type_node) = unknown_type_node;
6423
6424 /* This is for handling opaque types in signatures. */
6425 opaque_type_node = copy_node (ptr_type_node);
6426 TYPE_MAIN_VARIANT (opaque_type_node) = opaque_type_node;
6427 record_builtin_type (RID_MAX, 0, opaque_type_node);
6428
e92cc029 6429 /* This is special for C++ so functions can be overloaded. */
8d08fdba
MS
6430 wchar_type_node
6431 = TREE_TYPE (IDENTIFIER_GLOBAL_VALUE (get_identifier (WCHAR_TYPE)));
6432 wchar_type_size = TYPE_PRECISION (wchar_type_node);
6433 signed_wchar_type_node = make_signed_type (wchar_type_size);
6434 unsigned_wchar_type_node = make_unsigned_type (wchar_type_size);
6435 wchar_type_node
6436 = TREE_UNSIGNED (wchar_type_node)
6437 ? unsigned_wchar_type_node
6438 : signed_wchar_type_node;
6439 record_builtin_type (RID_WCHAR, "__wchar_t", wchar_type_node);
6440
f376e137
MS
6441 /* Artificial declaration of wchar_t -- can be bashed */
6442 wchar_decl_node = build_decl (TYPE_DECL, get_identifier ("wchar_t"),
6443 wchar_type_node);
6444 pushdecl (wchar_decl_node);
6445
8d08fdba
MS
6446 /* This is for wide string constants. */
6447 wchar_array_type_node
6448 = build_array_type (wchar_type_node, array_domain_type);
6449
8926095f 6450 if (flag_vtable_thunks)
700f8a87
MS
6451 {
6452 /* Make sure we get a unique function type, so we can give
6453 its pointer type a name. (This wins for gdb.) */
6454 tree vfunc_type = make_node (FUNCTION_TYPE);
6455 TREE_TYPE (vfunc_type) = integer_type_node;
6456 TYPE_ARG_TYPES (vfunc_type) = NULL_TREE;
6457 layout_type (vfunc_type);
6458
6459 vtable_entry_type = build_pointer_type (vfunc_type);
6460 }
8926095f 6461 else
700f8a87
MS
6462 {
6463 vtable_entry_type = make_lang_type (RECORD_TYPE);
6464 fields[0] = build_lang_field_decl (FIELD_DECL, delta_identifier,
6465 delta_type_node);
6466 fields[1] = build_lang_field_decl (FIELD_DECL, index_identifier,
6467 delta_type_node);
6468 fields[2] = build_lang_field_decl (FIELD_DECL, pfn_identifier,
6469 ptr_type_node);
6470 finish_builtin_type (vtable_entry_type, VTBL_PTR_TYPE, fields, 2,
6471 double_type_node);
6472
6473 /* Make this part of an invisible union. */
6474 fields[3] = copy_node (fields[2]);
6475 TREE_TYPE (fields[3]) = delta_type_node;
6476 DECL_NAME (fields[3]) = delta2_identifier;
6477 DECL_MODE (fields[3]) = TYPE_MODE (delta_type_node);
6478 DECL_SIZE (fields[3]) = TYPE_SIZE (delta_type_node);
6479 TREE_UNSIGNED (fields[3]) = 0;
6480 TREE_CHAIN (fields[2]) = fields[3];
91063b51
MM
6481 vtable_entry_type = build_qualified_type (vtable_entry_type,
6482 TYPE_QUAL_CONST);
700f8a87
MS
6483 }
6484 record_builtin_type (RID_MAX, VTBL_PTR_TYPE, vtable_entry_type);
8d08fdba 6485
8d08fdba 6486 vtbl_type_node
52bf7d5d 6487 = build_cplus_array_type (vtable_entry_type, NULL_TREE);
8d08fdba 6488 layout_type (vtbl_type_node);
91063b51 6489 vtbl_type_node = build_qualified_type (vtbl_type_node, TYPE_QUAL_CONST);
8d08fdba 6490 record_builtin_type (RID_MAX, NULL_PTR, vtbl_type_node);
849da744
MM
6491 vtbl_ptr_type_node = build_pointer_type (vtable_entry_type);
6492 layout_type (vtbl_ptr_type_node);
6493 record_builtin_type (RID_MAX, NULL_PTR, vtbl_ptr_type_node);
8d08fdba
MS
6494
6495 /* Simplify life by making a "sigtable_entry_type". Give its
6496 fields names so that the debugger can use them. */
6497
6498 if (flag_handle_signatures)
6499 {
6500 sigtable_entry_type = make_lang_type (RECORD_TYPE);
dff6b454
RK
6501 fields[0] = build_lang_field_decl (FIELD_DECL, tag_identifier,
6502 delta_type_node);
9dd70aa4 6503 fields[1] = build_lang_field_decl (FIELD_DECL, vb_off_identifier,
dff6b454 6504 delta_type_node);
9dd70aa4 6505 fields[2] = build_lang_field_decl (FIELD_DECL, delta_identifier,
dff6b454
RK
6506 delta_type_node);
6507 fields[3] = build_lang_field_decl (FIELD_DECL, index_identifier,
6508 delta_type_node);
9dd70aa4
GB
6509 fields[4] = build_lang_field_decl (FIELD_DECL, pfn_identifier,
6510 ptr_type_node);
6511
6512 /* Set the alignment to the max of the alignment of ptr_type_node and
6513 delta_type_node. Double alignment wastes a word on the Sparc. */
6514 finish_builtin_type (sigtable_entry_type, SIGTABLE_PTR_TYPE, fields, 4,
6515 (TYPE_ALIGN (ptr_type_node) > TYPE_ALIGN (delta_type_node))
6516 ? ptr_type_node
6517 : delta_type_node);
dff6b454
RK
6518
6519 /* Make this part of an invisible union. */
9dd70aa4
GB
6520 fields[5] = copy_node (fields[4]);
6521 TREE_TYPE (fields[5]) = delta_type_node;
6522 DECL_NAME (fields[5]) = vt_off_identifier;
6523 DECL_MODE (fields[5]) = TYPE_MODE (delta_type_node);
6524 DECL_SIZE (fields[5]) = TYPE_SIZE (delta_type_node);
6525 TREE_UNSIGNED (fields[5]) = 0;
6526 TREE_CHAIN (fields[4]) = fields[5];
dff6b454 6527
91063b51
MM
6528 sigtable_entry_type = build_qualified_type (sigtable_entry_type,
6529 TYPE_QUAL_CONST);
8d08fdba
MS
6530 record_builtin_type (RID_MAX, SIGTABLE_PTR_TYPE, sigtable_entry_type);
6531 }
6532
2c73f9f5
ML
6533 std_node = build_decl (NAMESPACE_DECL,
6534 get_identifier (flag_honor_std ? "fake std":"std"),
6633d636
MS
6535 void_type_node);
6536 pushdecl (std_node);
6537
2c73f9f5 6538 global_type_node = make_node (LANG_TYPE);
036407f7 6539 record_unknown_type (global_type_node, "global type");
2c73f9f5 6540
db5ae43f
MS
6541 /* Now, C++. */
6542 current_lang_name = lang_name_cplusplus;
8d08fdba 6543
ced78d8b 6544 {
2c73f9f5
ML
6545 tree bad_alloc_type_node, newtype, deltype;
6546 if (flag_honor_std)
6547 push_namespace (get_identifier ("std"));
6548 bad_alloc_type_node = xref_tag
ca107ded 6549 (class_type_node, get_identifier ("bad_alloc"), 1);
2c73f9f5
ML
6550 if (flag_honor_std)
6551 pop_namespace ();
6552 newtype = build_exception_variant
ced78d8b 6553 (ptr_ftype_sizetype, build_tree_list (NULL_TREE, bad_alloc_type_node));
2c73f9f5 6554 deltype = build_exception_variant
ced78d8b
JM
6555 (void_ftype_ptr, build_tree_list (NULL_TREE, NULL_TREE));
6556 auto_function (ansi_opname[(int) NEW_EXPR], newtype, NOT_BUILT_IN);
6557 auto_function (ansi_opname[(int) VEC_NEW_EXPR], newtype, NOT_BUILT_IN);
94e098d1
MM
6558 global_delete_fndecl
6559 = auto_function (ansi_opname[(int) DELETE_EXPR], deltype, NOT_BUILT_IN);
ced78d8b
JM
6560 auto_function (ansi_opname[(int) VEC_DELETE_EXPR], deltype, NOT_BUILT_IN);
6561 }
8d08fdba
MS
6562
6563 abort_fndecl
de22184b 6564 = define_function ("__pure_virtual", void_ftype,
8d08fdba
MS
6565 NOT_BUILT_IN, 0, 0);
6566
8d08fdba
MS
6567 /* Perform other language dependent initializations. */
6568 init_class_processing ();
6569 init_init_processing ();
6570 init_search_processing ();
1737fe20
BK
6571 if (flag_rtti)
6572 init_rtti_processing ();
8d08fdba 6573
6467930b 6574 if (flag_exceptions)
8d2733ca 6575 init_exception_processing ();
8d08fdba
MS
6576 if (flag_no_inline)
6577 {
6578 flag_inline_functions = 0;
8d08fdba 6579 }
9e9ff709 6580
7fcdf4c2 6581 if (! supports_one_only ())
72b7eeff 6582 flag_weak = 0;
8d08fdba
MS
6583
6584 /* Create the global bindings for __FUNCTION__ and __PRETTY_FUNCTION__. */
6585 declare_function_name ();
6586
6587 /* Prepare to check format strings against argument lists. */
6588 init_function_format_info ();
e9a25f70
JL
6589
6590 /* Show we use EH for cleanups. */
6591 using_eh_for_cleanups ();
62c154ed
JM
6592
6593 print_error_function = lang_print_error_function;
501ba25a 6594 lang_get_alias_set = &c_get_alias_set;
d9cf7c82
JM
6595
6596 /* Maintain consistency. Perhaps we should just complain if they
6597 say -fwritable-strings? */
6598 if (flag_writable_strings)
6599 flag_const_strings = 0;
62c154ed
JM
6600}
6601
6602/* Function to print any language-specific context for an error message. */
6603
6604static void
6605lang_print_error_function (file)
6606 char *file;
6607{
6608 default_print_error_function (file);
6609 maybe_print_template_context ();
8d08fdba
MS
6610}
6611
6612/* Make a definition for a builtin function named NAME and whose data type
6613 is TYPE. TYPE should be a function type with argument types.
6614 FUNCTION_CODE tells later passes how to compile calls to this function.
6615 See tree.h for its possible values.
6616
6617 If LIBRARY_NAME is nonzero, use that for DECL_ASSEMBLER_NAME,
6618 the name to be called if we can't opencode the function. */
6619
6620tree
6621define_function (name, type, function_code, pfn, library_name)
6622 char *name;
6623 tree type;
6624 enum built_in_function function_code;
49c249e1 6625 void (*pfn) PROTO((tree));
8d08fdba
MS
6626 char *library_name;
6627{
6628 tree decl = build_lang_decl (FUNCTION_DECL, get_identifier (name), type);
6629 DECL_EXTERNAL (decl) = 1;
6630 TREE_PUBLIC (decl) = 1;
863adfc0 6631 DECL_ARTIFICIAL (decl) = 1;
8d08fdba 6632
2c73f9f5 6633 my_friendly_assert (DECL_CONTEXT (decl) == NULL_TREE, 392);
cb0dbb9a 6634 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
2c73f9f5 6635
8d08fdba
MS
6636 /* Since `pushdecl' relies on DECL_ASSEMBLER_NAME instead of DECL_NAME,
6637 we cannot change DECL_ASSEMBLER_NAME until we have installed this
6638 function in the namespace. */
6639 if (pfn) (*pfn) (decl);
6640 if (library_name)
6641 DECL_ASSEMBLER_NAME (decl) = get_identifier (library_name);
6642 make_function_rtl (decl);
6643 if (function_code != NOT_BUILT_IN)
6644 {
6645 DECL_BUILT_IN (decl) = 1;
39211cd5 6646 DECL_FUNCTION_CODE (decl) = function_code;
8d08fdba
MS
6647 }
6648 return decl;
6649}
6650\f
61a127b3
MM
6651/* When we call finish_struct for an anonymous union, we create
6652 default copy constructors and such. But, an anonymous union
6653 shouldn't have such things; this function undoes the damage to the
6654 anonymous union type T.
6655
6656 (The reason that we create the synthesized methods is that we don't
6657 distinguish `union { int i; }' from `typedef union { int i; } U'.
6658 The first is an anonymous union; the second is just an ordinary
6659 union type.) */
6660
6661void
6662fixup_anonymous_union (t)
6663 tree t;
6664{
6665 tree *q;
6666
6667 /* Wipe out memory of synthesized methods */
6668 TYPE_HAS_CONSTRUCTOR (t) = 0;
6669 TYPE_HAS_DEFAULT_CONSTRUCTOR (t) = 0;
6670 TYPE_HAS_INIT_REF (t) = 0;
6671 TYPE_HAS_CONST_INIT_REF (t) = 0;
6672 TYPE_HAS_ASSIGN_REF (t) = 0;
6673 TYPE_HAS_ASSIGNMENT (t) = 0;
6674 TYPE_HAS_CONST_ASSIGN_REF (t) = 0;
6675
6676 /* Splice the implicitly generated functions out of the TYPE_METHODS
6677 list. */
6678 q = &TYPE_METHODS (t);
6679 while (*q)
6680 {
6681 if (DECL_ARTIFICIAL (*q))
6682 *q = TREE_CHAIN (*q);
6683 else
6684 q = &TREE_CHAIN (*q);
6685 }
6686
6687 /* ANSI C++ June 5 1992 WP 9.5.3. Anonymous unions may not have
6688 function members. */
6689 if (TYPE_METHODS (t))
6690 error ("an anonymous union cannot have function members");
6691}
6692
72a93143
JM
6693/* Make sure that a declaration with no declarator is well-formed, i.e.
6694 just defines a tagged type or anonymous union.
8d08fdba 6695
72a93143 6696 Returns the type defined, if any. */
8d08fdba 6697
72a93143
JM
6698tree
6699check_tag_decl (declspecs)
8d08fdba
MS
6700 tree declspecs;
6701{
72a93143 6702 int found_type = 0;
2986ae00 6703 tree ob_modifier = NULL_TREE;
8d08fdba 6704 register tree link;
8d08fdba
MS
6705 register tree t = NULL_TREE;
6706
6707 for (link = declspecs; link; link = TREE_CHAIN (link))
6708 {
6709 register tree value = TREE_VALUE (link);
6710
72a93143 6711 if (TYPE_P (value))
8d08fdba 6712 {
72a93143 6713 ++found_type;
5566b478 6714
72a93143
JM
6715 if (IS_AGGR_TYPE (value) || TREE_CODE (value) == ENUMERAL_TYPE)
6716 {
6717 my_friendly_assert (TYPE_MAIN_DECL (value) != NULL_TREE, 261);
6718 t = value;
6719 }
8d08fdba 6720 }
83f660b7
JM
6721 else if (value == ridpointers[(int) RID_FRIEND])
6722 {
83f660b7
JM
6723 if (current_class_type == NULL_TREE
6724 || current_scope () != current_class_type)
6725 ob_modifier = value;
6726 }
8d08fdba 6727 else if (value == ridpointers[(int) RID_STATIC]
2986ae00
MS
6728 || value == ridpointers[(int) RID_EXTERN]
6729 || value == ridpointers[(int) RID_AUTO]
28cbf42c
MS
6730 || value == ridpointers[(int) RID_REGISTER]
6731 || value == ridpointers[(int) RID_INLINE]
6732 || value == ridpointers[(int) RID_VIRTUAL]
72a93143
JM
6733 || value == ridpointers[(int) RID_CONST]
6734 || value == ridpointers[(int) RID_VOLATILE]
28cbf42c 6735 || value == ridpointers[(int) RID_EXPLICIT])
2986ae00 6736 ob_modifier = value;
8d08fdba
MS
6737 }
6738
72a93143
JM
6739 if (found_type > 1)
6740 error ("multiple types in one declaration");
7e2067ca
JM
6741
6742 /* Inside a class, we might be in a friend or access declaration.
6743 Until we have a good way of detecting the latter, don't warn. */
6744 if (t == NULL_TREE && ! current_class_type)
6745 pedwarn ("declaration does not declare anything");
6746 else if (t && ANON_UNION_TYPE_P (t))
83f660b7
JM
6747 /* Anonymous unions are objects, so they can have specifiers. */;
6748 else if (ob_modifier)
8d08fdba 6749 {
83f660b7
JM
6750 if (ob_modifier == ridpointers[(int) RID_INLINE]
6751 || ob_modifier == ridpointers[(int) RID_VIRTUAL])
6752 cp_error ("`%D' can only be specified for functions", ob_modifier);
6753 else if (ob_modifier == ridpointers[(int) RID_FRIEND])
6754 cp_error ("`%D' can only be specified inside a class", ob_modifier);
6755 else if (ob_modifier == ridpointers[(int) RID_EXPLICIT])
6756 cp_error ("`%D' can only be specified for constructors",
6757 ob_modifier);
6758 else
6759 cp_error ("`%D' can only be specified for objects and functions",
6760 ob_modifier);
72a93143 6761 }
8d08fdba 6762
72a93143
JM
6763 return t;
6764}
6765
6766/* Called when a declaration is seen that contains no names to declare.
6767 If its type is a reference to a structure, union or enum inherited
6768 from a containing scope, shadow that tag name for the current scope
6769 with a forward reference.
6770 If its type defines a new named structure or union
6771 or defines an enum, it is valid but we need not do anything here.
6772 Otherwise, it is an error.
6773
6774 C++: may have to grok the declspecs to learn about static,
6775 complain for anonymous unions. */
6776
6777void
6778shadow_tag (declspecs)
6779 tree declspecs;
6780{
6781 tree t = check_tag_decl (declspecs);
6782
6783 if (t)
6784 maybe_process_partial_specialization (t);
6785
6786 /* This is where the variables in an anonymous union are
6787 declared. An anonymous union declaration looks like:
6788 union { ... } ;
6789 because there is no declarator after the union, the parser
6790 sends that declaration here. */
6791 if (t && ANON_UNION_TYPE_P (t))
6792 {
6793 fixup_anonymous_union (t);
6794
6795 if (TYPE_FIELDS (t))
6796 {
6797 tree decl = grokdeclarator (NULL_TREE, declspecs, NORMAL, 0,
6798 NULL_TREE);
6799 finish_anon_union (decl);
6800 }
8d08fdba
MS
6801 }
6802}
6803\f
6804/* Decode a "typename", such as "int **", returning a ..._TYPE node. */
6805
6806tree
6807groktypename (typename)
6808 tree typename;
6809{
6810 if (TREE_CODE (typename) != TREE_LIST)
6811 return typename;
6812 return grokdeclarator (TREE_VALUE (typename),
6813 TREE_PURPOSE (typename),
c11b6f21 6814 TYPENAME, 0, NULL_TREE);
8d08fdba
MS
6815}
6816
6817/* Decode a declarator in an ordinary declaration or data definition.
6818 This is called as soon as the type information and variable name
6819 have been parsed, before parsing the initializer if any.
6820 Here we create the ..._DECL node, fill in its type,
6821 and put it on the list of decls for the current context.
6822 The ..._DECL node is returned as the value.
6823
6824 Exception: for arrays where the length is not specified,
82580166 6825 the type is left null, to be filled in by `cp_finish_decl'.
8d08fdba
MS
6826
6827 Function definitions do not come here; they go to start_function
6828 instead. However, external and forward declarations of functions
6829 do go through here. Structure field declarations are done by
6830 grokfield and not through here. */
6831
6832/* Set this to zero to debug not using the temporary obstack
6833 to parse initializers. */
6834int debug_temp_inits = 1;
6835
6836tree
a1774733 6837start_decl (declarator, declspecs, initialized, attributes, prefix_attributes)
8d08fdba
MS
6838 tree declarator, declspecs;
6839 int initialized;
a1774733 6840 tree attributes, prefix_attributes;
8d08fdba
MS
6841{
6842 register tree decl;
6843 register tree type, tem;
6844 tree context;
6845 extern int have_extern_spec;
6846 extern int used_extern_spec;
6847
5566b478
MS
6848#if 0
6849 /* See code below that used this. */
8d08fdba 6850 int init_written = initialized;
5566b478 6851#endif
8d08fdba 6852
e92cc029 6853 /* This should only be done once on the top most decl. */
8d08fdba
MS
6854 if (have_extern_spec && !used_extern_spec)
6855 {
a28e3c7f
MS
6856 declspecs = decl_tree_cons (NULL_TREE, get_identifier ("extern"),
6857 declspecs);
8d08fdba
MS
6858 used_extern_spec = 1;
6859 }
6860
c11b6f21 6861 decl = grokdeclarator (declarator, declspecs, NORMAL, initialized,
f30432d7 6862 NULL_TREE);
a1774733 6863 if (decl == NULL_TREE || TREE_CODE (decl) == VOID_TYPE)
8d08fdba
MS
6864 return NULL_TREE;
6865
6866 type = TREE_TYPE (decl);
6867
6868 /* Don't lose if destructors must be executed at file-level. */
5156628f 6869 if (! processing_template_decl && TREE_STATIC (decl)
ec255269 6870 && TYPE_NEEDS_DESTRUCTOR (complete_type (type))
8d08fdba
MS
6871 && !TREE_PERMANENT (decl))
6872 {
6873 push_obstacks (&permanent_obstack, &permanent_obstack);
6874 decl = copy_node (decl);
6875 if (TREE_CODE (type) == ARRAY_TYPE)
6876 {
6877 tree itype = TYPE_DOMAIN (type);
6878 if (itype && ! TREE_PERMANENT (itype))
6879 {
6880 itype = build_index_type (copy_to_permanent (TYPE_MAX_VALUE (itype)));
6881 type = build_cplus_array_type (TREE_TYPE (type), itype);
6882 TREE_TYPE (decl) = type;
6883 }
6884 }
6885 pop_obstacks ();
6886 }
6887
8d08fdba
MS
6888 context
6889 = (TREE_CODE (decl) == FUNCTION_DECL && DECL_VIRTUAL_P (decl))
6890 ? DECL_CLASS_CONTEXT (decl)
6891 : DECL_CONTEXT (decl);
6892
9a68c51f
JM
6893 if (initialized && context && TREE_CODE (context) == NAMESPACE_DECL
6894 && context != current_namespace && TREE_CODE (decl) == VAR_DECL)
6895 {
6896 /* When parsing the initializer, lookup should use the object's
6897 namespace. */
6898 push_decl_namespace (context);
6899 }
6900
2c73f9f5
ML
6901 /* We are only interested in class contexts, later. */
6902 if (context && TREE_CODE (context) == NAMESPACE_DECL)
6903 context = NULL_TREE;
6904
8d08fdba
MS
6905 if (initialized)
6906 /* Is it valid for this decl to have an initializer at all?
6907 If not, set INITIALIZED to zero, which will indirectly
82580166 6908 tell `cp_finish_decl' to ignore the initializer once it is parsed. */
8d08fdba
MS
6909 switch (TREE_CODE (decl))
6910 {
6911 case TYPE_DECL:
6912 /* typedef foo = bar means give foo the same type as bar.
82580166 6913 We haven't parsed bar yet, so `cp_finish_decl' will fix that up.
8d08fdba
MS
6914 Any other case of an initialization in a TYPE_DECL is an error. */
6915 if (pedantic || list_length (declspecs) > 1)
6916 {
8251199e 6917 cp_error ("typedef `%D' is initialized", decl);
8d08fdba
MS
6918 initialized = 0;
6919 }
6920 break;
6921
6922 case FUNCTION_DECL:
8251199e 6923 cp_error ("function `%#D' is initialized like a variable", decl);
8d08fdba
MS
6924 initialized = 0;
6925 break;
6926
6927 default:
5156628f 6928 if (! processing_template_decl)
8d08fdba 6929 {
bd6dd845 6930 if (type != error_mark_node)
5156628f 6931 {
bd6dd845
MS
6932 if (TYPE_SIZE (type) != NULL_TREE
6933 && ! TREE_CONSTANT (TYPE_SIZE (type)))
6934 {
8251199e
JM
6935 cp_error
6936 ("variable-sized object `%D' may not be initialized",
bd6dd845
MS
6937 decl);
6938 initialized = 0;
6939 }
5156628f 6940
bd6dd845
MS
6941 if (TREE_CODE (type) == ARRAY_TYPE
6942 && TYPE_SIZE (complete_type (TREE_TYPE (type))) == NULL_TREE)
6943 {
8251199e
JM
6944 cp_error
6945 ("elements of array `%#D' have incomplete type", decl);
bd6dd845
MS
6946 initialized = 0;
6947 }
5156628f 6948 }
8d08fdba
MS
6949 }
6950 }
6951
8d08fdba
MS
6952 if (initialized)
6953 {
a9aedbc2 6954 if (! toplevel_bindings_p ()
8d08fdba 6955 && DECL_EXTERNAL (decl))
8251199e 6956 cp_warning ("declaration of `%#D' has `extern' and is initialized",
8d08fdba
MS
6957 decl);
6958 DECL_EXTERNAL (decl) = 0;
5566b478 6959 if (toplevel_bindings_p ())
8d08fdba
MS
6960 TREE_STATIC (decl) = 1;
6961
6962 /* Tell `pushdecl' this is an initialized decl
6963 even though we don't yet have the initializer expression.
82580166 6964 Also tell `cp_finish_decl' it may store the real initializer. */
8d08fdba
MS
6965 DECL_INITIAL (decl) = error_mark_node;
6966 }
6967
5566b478 6968 if (context && TYPE_SIZE (complete_type (context)) != NULL_TREE)
5b605f68 6969 {
e97e5263
JM
6970 pushclass (context, 2);
6971
5b605f68
MS
6972 if (TREE_CODE (decl) == VAR_DECL)
6973 {
6974 tree field = lookup_field (context, DECL_NAME (decl), 0, 0);
6975 if (field == NULL_TREE || TREE_CODE (field) != VAR_DECL)
8251199e 6976 cp_error ("`%#D' is not a static member of `%#T'", decl, context);
e349ee73
MS
6977 else
6978 {
6979 if (DECL_CONTEXT (field) != context)
f2d773a2 6980 {
8251199e 6981 cp_pedwarn ("ANSI C++ does not permit `%T::%D' to be defined as `%T::%D'",
f2d773a2
JM
6982 DECL_CONTEXT (field), DECL_NAME (decl),
6983 context, DECL_NAME (decl));
6984 DECL_CONTEXT (decl) = DECL_CONTEXT (field);
6985 }
75650646
MM
6986 /* Static data member are tricky; an in-class initialization
6987 still doesn't provide a definition, so the in-class
6988 declaration will have DECL_EXTERNAL set, but will have an
6989 initialization. Thus, duplicate_decls won't warn
6990 about this situation, and so we check here. */
6991 if (DECL_INITIAL (decl) && DECL_INITIAL (field))
8251199e 6992 cp_error ("duplicate initialization of %D", decl);
e349ee73
MS
6993 if (duplicate_decls (decl, field))
6994 decl = field;
6995 }
5b605f68 6996 }
f30432d7
MS
6997 else
6998 {
5566b478 6999 tree field = check_classfn (context, decl);
f30432d7
MS
7000 if (field && duplicate_decls (decl, field))
7001 decl = field;
7002 }
7003
7004 /* cp_finish_decl sets DECL_EXTERNAL if DECL_IN_AGGR_P is set. */
dff3e828
BK
7005 DECL_IN_AGGR_P (decl) = 0;
7006 if ((DECL_LANG_SPECIFIC (decl) && DECL_USE_TEMPLATE (decl))
7007 || CLASSTYPE_USE_TEMPLATE (context))
84e6233f
JM
7008 {
7009 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
7010 /* [temp.expl.spec] An explicit specialization of a static data
7011 member of a template is a definition if the declaration
7012 includes an initializer; otherwise, it is a declaration.
7013
7014 We check for processing_specialization so this only applies
7015 to the new specialization syntax. */
7016 if (DECL_INITIAL (decl) == NULL_TREE && processing_specialization)
7017 DECL_EXTERNAL (decl) = 1;
7018 }
f30432d7 7019
b7698cf0 7020 if (DECL_EXTERNAL (decl) && ! DECL_TEMPLATE_SPECIALIZATION (decl))
8251199e 7021 cp_pedwarn ("declaration of `%#D' outside of class is not definition",
f30432d7 7022 decl);
5b605f68
MS
7023 }
7024
3a846e6e
NC
7025#ifdef SET_DEFAULT_DECL_ATTRIBUTES
7026 SET_DEFAULT_DECL_ATTRIBUTES (decl, attributes);
7027#endif
7028
a1774733
BK
7029 /* Set attributes here so if duplicate decl, will have proper attributes. */
7030 cplus_decl_attributes (decl, attributes, prefix_attributes);
7031
8d08fdba
MS
7032 /* Add this decl to the current binding level, but not if it
7033 comes from another scope, e.g. a static member variable.
7034 TEM may equal DECL or it may be a previous decl of the same name. */
5b605f68 7035
51bcf661
JM
7036 if ((TREE_CODE (decl) != PARM_DECL && DECL_CONTEXT (decl) != NULL_TREE
7037 /* Definitions of namespace members outside their namespace are
7038 possible. */
7039 && TREE_CODE (DECL_CONTEXT (decl)) != NAMESPACE_DECL)
2c73f9f5 7040 || (TREE_CODE (decl) == TEMPLATE_DECL && !namespace_bindings_p ())
75650646
MM
7041 || TREE_CODE (type) == LANG_TYPE
7042 /* The declaration of template specializations does not affect
7043 the functions available for overload resolution, so we do not
7044 call pushdecl. */
e1467ff2 7045 || (TREE_CODE (decl) == FUNCTION_DECL
75650646 7046 && DECL_TEMPLATE_SPECIALIZATION (decl)))
8d08fdba
MS
7047 tem = decl;
7048 else
8926095f 7049 tem = pushdecl (decl);
2ee887f2 7050
5156628f 7051 if (processing_template_decl)
5566b478
MS
7052 {
7053 if (! current_function_decl)
3ac3d9ea 7054 tem = push_template_decl (tem);
5566b478 7055 else if (minimal_parse_mode)
b5effa19 7056 DECL_VINDEX (tem)
5566b478
MS
7057 = build_min_nt (DECL_STMT, copy_to_permanent (declarator),
7058 copy_to_permanent (declspecs),
c11b6f21 7059 NULL_TREE);
5566b478
MS
7060 }
7061
7062
2ee887f2 7063#if ! defined (ASM_OUTPUT_BSS) && ! defined (ASM_OUTPUT_ALIGNED_BSS)
a3203465 7064 /* Tell the back-end to use or not use .common as appropriate. If we say
a50f0918
MS
7065 -fconserve-space, we want this to save .data space, at the expense of
7066 wrong semantics. If we say -fno-conserve-space, we want this to
7067 produce errors about redefs; to do this we force variables into the
7068 data segment. */
a3203465 7069 DECL_COMMON (tem) = flag_conserve_space || ! TREE_PUBLIC (tem);
2ee887f2 7070#endif
3a846e6e 7071
5156628f 7072 if (! processing_template_decl)
5566b478 7073 start_decl_1 (tem);
8d08fdba 7074
5566b478
MS
7075 /* Corresponding pop_obstacks is done in `cp_finish_decl'. */
7076 push_obstacks_nochange ();
8d08fdba 7077
5566b478
MS
7078#if 0
7079 /* We have no way of knowing whether the initializer will need to be
7080 evaluated at run-time or not until we've parsed it, so let's just put
7081 it in the permanent obstack. (jason) */
8d08fdba
MS
7082 if (init_written
7083 && ! (TREE_CODE (tem) == PARM_DECL
7084 || (TREE_READONLY (tem)
7085 && (TREE_CODE (tem) == VAR_DECL
7086 || TREE_CODE (tem) == FIELD_DECL))))
7087 {
7088 /* When parsing and digesting the initializer,
7089 use temporary storage. Do this even if we will ignore the value. */
a9aedbc2 7090 if (toplevel_bindings_p () && debug_temp_inits)
8d08fdba 7091 {
5156628f 7092 if (processing_template_decl
5566b478 7093 || TYPE_NEEDS_CONSTRUCTING (type)
a28e3c7f 7094 || TREE_CODE (type) == REFERENCE_TYPE)
8d08fdba
MS
7095 /* In this case, the initializer must lay down in permanent
7096 storage, since it will be saved until `finish_file' is run. */
7097 ;
7098 else
7099 temporary_allocation ();
7100 }
7101 }
5566b478 7102#endif
8d08fdba 7103
8d08fdba
MS
7104 return tem;
7105}
7106
5566b478
MS
7107void
7108start_decl_1 (decl)
7109 tree decl;
8d08fdba 7110{
5566b478
MS
7111 tree type = TREE_TYPE (decl);
7112 int initialized = (DECL_INITIAL (decl) != NULL_TREE);
8d08fdba 7113
5566b478
MS
7114 /* If this type of object needs a cleanup, and control may
7115 jump past it, make a new binding level so that it is cleaned
7116 up only when it is initialized first. */
7117 if (TYPE_NEEDS_DESTRUCTOR (type)
7118 && current_binding_level->more_cleanups_ok == 0)
7119 pushlevel_temporary (1);
7120
7121 if (initialized)
7122 /* Is it valid for this decl to have an initializer at all?
7123 If not, set INITIALIZED to zero, which will indirectly
7124 tell `cp_finish_decl' to ignore the initializer once it is parsed. */
8d08fdba 7125 {
5566b478
MS
7126 /* Don't allow initializations for incomplete types except for
7127 arrays which might be completed by the initialization. */
7128 if (type == error_mark_node)
7129 ; /* Don't complain again. */
7130 else if (TYPE_SIZE (complete_type (type)) != NULL_TREE)
7131 ; /* A complete type is ok. */
7132 else if (TREE_CODE (type) != ARRAY_TYPE)
8d08fdba 7133 {
8251199e 7134 cp_error ("variable `%#D' has initializer but incomplete type",
5566b478
MS
7135 decl);
7136 initialized = 0;
25eb19ff 7137 type = TREE_TYPE (decl) = error_mark_node;
5566b478
MS
7138 }
7139 else if (TYPE_SIZE (complete_type (TREE_TYPE (type))) == NULL_TREE)
7140 {
7141 if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
8251199e 7142 cp_error ("elements of array `%#D' have incomplete type", decl);
5566b478
MS
7143 /* else we already gave an error in start_decl. */
7144 initialized = 0;
8d08fdba 7145 }
8d08fdba
MS
7146 }
7147
5566b478
MS
7148 if (!initialized
7149 && TREE_CODE (decl) != TYPE_DECL
7150 && TREE_CODE (decl) != TEMPLATE_DECL
7151 && IS_AGGR_TYPE (type) && ! DECL_EXTERNAL (decl))
8d08fdba 7152 {
5156628f 7153 if ((! processing_template_decl || ! uses_template_parms (type))
7fcdf4c2 7154 && TYPE_SIZE (complete_type (type)) == NULL_TREE)
5566b478 7155 {
8251199e 7156 cp_error ("aggregate `%#D' has incomplete type and cannot be initialized",
5566b478
MS
7157 decl);
7158 /* Change the type so that assemble_variable will give
7159 DECL an rtl we can live with: (mem (const_int 0)). */
25eb19ff 7160 type = TREE_TYPE (decl) = error_mark_node;
5566b478
MS
7161 }
7162 else
7163 {
7164 /* If any base type in the hierarchy of TYPE needs a constructor,
7165 then we set initialized to 1. This way any nodes which are
7166 created for the purposes of initializing this aggregate
7167 will live as long as it does. This is necessary for global
7168 aggregates which do not have their initializers processed until
7169 the end of the file. */
7170 initialized = TYPE_NEEDS_CONSTRUCTING (type);
7171 }
7172 }
7173
7174#if 0
7175 /* We don't do this yet for GNU C++. */
7176 /* For a local variable, define the RTL now. */
7177 if (! toplevel_bindings_p ()
7178 /* But not if this is a duplicate decl
7179 and we preserved the rtl from the previous one
7180 (which may or may not happen). */
7181 && DECL_RTL (tem) == NULL_RTX)
7182 {
7183 if (TYPE_SIZE (TREE_TYPE (tem)) != NULL_TREE)
7184 expand_decl (tem);
7185 else if (TREE_CODE (TREE_TYPE (tem)) == ARRAY_TYPE
7186 && DECL_INITIAL (tem) != NULL_TREE)
7187 expand_decl (tem);
7188 }
7189#endif
7190
7191 if (! initialized)
7192 DECL_INITIAL (decl) = NULL_TREE;
7193}
7194
7195/* Handle initialization of references.
38e01259 7196 These three arguments are from `cp_finish_decl', and have the
e92cc029
MS
7197 same meaning here that they do there.
7198
7199 Quotes on semantics can be found in ARM 8.4.3. */
7200
5566b478 7201static void
a703fb38 7202grok_reference_init (decl, type, init)
5566b478 7203 tree decl, type, init;
5566b478
MS
7204{
7205 tree tmp;
7206
7207 if (init == NULL_TREE)
7208 {
7209 if ((DECL_LANG_SPECIFIC (decl) == 0
7210 || DECL_IN_AGGR_P (decl) == 0)
7211 && ! DECL_THIS_EXTERN (decl))
7212 {
8251199e 7213 cp_error ("`%D' declared as reference but not initialized", decl);
5566b478
MS
7214 if (TREE_CODE (decl) == VAR_DECL)
7215 SET_DECL_REFERENCE_SLOT (decl, error_mark_node);
7216 }
7217 return;
7218 }
7219
7220 if (init == error_mark_node)
7221 return;
7222
7223 if (TREE_CODE (type) == REFERENCE_TYPE
7224 && TREE_CODE (init) == CONSTRUCTOR)
7225 {
8251199e 7226 cp_error ("ANSI C++ forbids use of initializer list to initialize reference `%D'", decl);
8d08fdba
MS
7227 return;
7228 }
7229
7230 if (TREE_CODE (init) == TREE_LIST)
7231 init = build_compound_expr (init);
8d08fdba 7232
8ccc31eb
MS
7233 if (TREE_CODE (TREE_TYPE (init)) == REFERENCE_TYPE)
7234 init = convert_from_reference (init);
7235
8d08fdba
MS
7236 if (TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE
7237 && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE)
7238 {
a3203465 7239 /* Note: default conversion is only called in very special cases. */
8d08fdba
MS
7240 init = default_conversion (init);
7241 }
7242
a3203465 7243 tmp = convert_to_reference
9a3b49ac
MS
7244 (type, init, CONV_IMPLICIT,
7245 LOOKUP_SPECULATIVELY|LOOKUP_NORMAL|DIRECT_BIND, decl);
8d08fdba 7246
a3203465
MS
7247 if (tmp == error_mark_node)
7248 goto fail;
7249 else if (tmp != NULL_TREE)
8d08fdba 7250 {
a3203465 7251 init = tmp;
4c7bdca6 7252 DECL_INITIAL (decl) = save_expr (init);
8d08fdba 7253 }
a3203465 7254 else
8d08fdba 7255 {
8251199e 7256 cp_error ("cannot initialize `%T' from `%T'", type, TREE_TYPE (init));
a3203465 7257 goto fail;
8d08fdba 7258 }
8d08fdba 7259
8d08fdba
MS
7260 /* ?? Can this be optimized in some cases to
7261 hand back the DECL_INITIAL slot?? */
7262 if (TYPE_SIZE (TREE_TYPE (type)))
7263 {
7264 init = convert_from_reference (decl);
7265 if (TREE_PERMANENT (decl))
7266 init = copy_to_permanent (init);
7267 SET_DECL_REFERENCE_SLOT (decl, init);
7268 }
7269
7270 if (TREE_STATIC (decl) && ! TREE_CONSTANT (DECL_INITIAL (decl)))
7271 {
7272 expand_static_init (decl, DECL_INITIAL (decl));
7273 DECL_INITIAL (decl) = NULL_TREE;
7274 }
7275 return;
7276
7277 fail:
7278 if (TREE_CODE (decl) == VAR_DECL)
7279 SET_DECL_REFERENCE_SLOT (decl, error_mark_node);
7280 return;
7281}
7282
6060a796
MS
7283/* Fill in DECL_INITIAL with some magical value to prevent expand_decl from
7284 mucking with forces it does not comprehend (i.e. initialization with a
7285 constructor). If we are at global scope and won't go into COMMON, fill
7286 it in with a dummy CONSTRUCTOR to force the variable into .data;
7287 otherwise we can use error_mark_node. */
7288
28cbf42c
MS
7289static tree
7290obscure_complex_init (decl, init)
7291 tree decl, init;
6060a796 7292{
28cbf42c
MS
7293 if (! flag_no_inline && TREE_STATIC (decl))
7294 {
7295 if (extract_init (decl, init))
7296 return NULL_TREE;
7297 }
7298
2ee887f2 7299#if ! defined (ASM_OUTPUT_BSS) && ! defined (ASM_OUTPUT_ALIGNED_BSS)
a9aedbc2 7300 if (toplevel_bindings_p () && ! DECL_COMMON (decl))
6060a796
MS
7301 DECL_INITIAL (decl) = build (CONSTRUCTOR, TREE_TYPE (decl), NULL_TREE,
7302 NULL_TREE);
7303 else
2ee887f2 7304#endif
6060a796 7305 DECL_INITIAL (decl) = error_mark_node;
28cbf42c
MS
7306
7307 return init;
6060a796
MS
7308}
7309
91063b51
MM
7310/* Issue an error message if DECL is an uninitialized const variable. */
7311
7312static void
7313check_for_uninitialized_const_var (decl)
7314 tree decl;
7315{
7316 tree type = TREE_TYPE (decl);
7317
7318 /* ``Unless explicitly declared extern, a const object does not have
7319 external linkage and must be initialized. ($8.4; $12.1)'' ARM
7320 7.1.6 */
7321 if (TREE_CODE (decl) == VAR_DECL
7322 && TREE_CODE (type) != REFERENCE_TYPE
7323 && CP_TYPE_CONST_P (type)
7324 && !TYPE_NEEDS_CONSTRUCTING (type)
7325 && !DECL_INITIAL (decl))
7326 cp_error ("uninitialized const `%D'", decl);
7327}
7328
8d08fdba
MS
7329/* Finish processing of a declaration;
7330 install its line number and initial value.
7331 If the length of an array type is not known before,
7332 it must be determined now, from the initial value, or it is an error.
7333
7334 Call `pop_obstacks' iff NEED_POP is nonzero.
7335
82580166 7336 For C++, `cp_finish_decl' must be fairly evasive: it must keep initializers
8d08fdba
MS
7337 for aggregates that have constructors alive on the permanent obstack,
7338 so that the global initializing functions can be written at the end.
7339
7340 INIT0 holds the value of an initializer that should be allowed to escape
7341 the normal rules.
7342
6060a796
MS
7343 FLAGS is LOOKUP_ONLYCONVERTING is the = init syntax was used, else 0
7344 if the (init) syntax was used.
7345
8d08fdba 7346 For functions that take default parameters, DECL points to its
82580166 7347 "maximal" instantiation. `cp_finish_decl' must then also declared its
8d08fdba
MS
7348 subsequently lower and lower forms of instantiation, checking for
7349 ambiguity as it goes. This can be sped up later. */
7350
7351void
82580166 7352cp_finish_decl (decl, init, asmspec_tree, need_pop, flags)
8d08fdba
MS
7353 tree decl, init;
7354 tree asmspec_tree;
7355 int need_pop;
6060a796 7356 int flags;
8d08fdba
MS
7357{
7358 register tree type;
a703fb38 7359 tree cleanup = NULL_TREE, ttype = NULL_TREE;
8d08fdba
MS
7360 int was_incomplete;
7361 int temporary = allocation_temporary_p ();
7362 char *asmspec = NULL;
7363 int was_readonly = 0;
0c4b14c4 7364 int already_used = 0;
8d08fdba
MS
7365
7366 /* If this is 0, then we did not change obstacks. */
7367 if (! decl)
7368 {
7369 if (init)
8251199e 7370 error ("assignment (not initialization) in declaration");
8d08fdba
MS
7371 return;
7372 }
7373
a4443a08 7374 /* If a name was specified, get the string. */
8d08fdba 7375 if (asmspec_tree)
8d08fdba 7376 asmspec = TREE_STRING_POINTER (asmspec_tree);
8d08fdba 7377
2c73f9f5
ML
7378 if (init && TREE_CODE (init) == NAMESPACE_DECL)
7379 {
8251199e 7380 cp_error ("Cannot initialize `%D' to namespace `%D'",
2c73f9f5
ML
7381 decl, init);
7382 init = NULL_TREE;
7383 }
7384
6ba89f8e
MM
7385 if (current_class_type
7386 && DECL_REAL_CONTEXT (decl) == current_class_type
7387 && TYPE_BEING_DEFINED (current_class_type)
7388 && (DECL_INITIAL (decl) || init))
7389 DECL_DEFINED_IN_CLASS_P (decl) = 1;
7390
9a68c51f
JM
7391 if (TREE_CODE (decl) == VAR_DECL
7392 && DECL_CONTEXT (decl)
7393 && TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL
7394 && DECL_CONTEXT (decl) != current_namespace
7395 && init)
7396 {
7397 /* Leave the namespace of the object. */
7398 pop_decl_namespace ();
7399 }
7400
8d08fdba
MS
7401 /* If the type of the thing we are declaring either has
7402 a constructor, or has a virtual function table pointer,
7403 AND its initialization was accepted by `start_decl',
7404 then we stayed on the permanent obstack through the
7405 declaration, otherwise, changed obstacks as GCC would. */
7406
7407 type = TREE_TYPE (decl);
7408
f376e137 7409 if (type == error_mark_node)
eac293a1 7410 {
a9aedbc2 7411 if (toplevel_bindings_p () && temporary)
eac293a1
MS
7412 end_temporary_allocation ();
7413
7414 return;
7415 }
f376e137 7416
a7a7710d
NS
7417 if (TYPE_HAS_MUTABLE_P (type))
7418 TREE_READONLY (decl) = 0;
7419
5156628f 7420 if (processing_template_decl)
5566b478
MS
7421 {
7422 if (init && DECL_INITIAL (decl))
7423 DECL_INITIAL (decl) = init;
faf5394a 7424 if (minimal_parse_mode && ! DECL_ARTIFICIAL (decl))
5566b478
MS
7425 {
7426 tree stmt = DECL_VINDEX (decl);
e1467ff2 7427 /* If the decl is declaring a member of a local class (in a
8857f91e
MM
7428 template function), the DECL_VINDEX will either be NULL,
7429 or it will be an actual virtual function index, not a
7430 DECL_STMT. */
7431 if (stmt != NULL_TREE && TREE_CODE (stmt) == DECL_STMT)
75650646
MM
7432 {
7433 DECL_VINDEX (decl) = NULL_TREE;
7434 TREE_OPERAND (stmt, 2) = copy_to_permanent (init);
7435 add_tree (stmt);
7436 }
5566b478 7437 }
8d08fdba 7438
5566b478
MS
7439 goto finish_end0;
7440 }
8d08fdba
MS
7441 /* Take care of TYPE_DECLs up front. */
7442 if (TREE_CODE (decl) == TYPE_DECL)
7443 {
7444 if (init && DECL_INITIAL (decl))
7445 {
7446 /* typedef foo = bar; store the type of bar as the type of foo. */
7447 TREE_TYPE (decl) = type = TREE_TYPE (init);
7448 DECL_INITIAL (decl) = init = NULL_TREE;
7449 }
a0a33927
MS
7450 if (type != error_mark_node
7451 && IS_AGGR_TYPE (type) && DECL_NAME (decl))
8d08fdba
MS
7452 {
7453 if (TREE_TYPE (DECL_NAME (decl)) && TREE_TYPE (decl) != type)
8251199e 7454 cp_warning ("shadowing previous type declaration of `%#D'", decl);
8d08fdba
MS
7455 set_identifier_type_value (DECL_NAME (decl), type);
7456 CLASSTYPE_GOT_SEMICOLON (type) = 1;
7457 }
7458 GNU_xref_decl (current_function_decl, decl);
cffa8729
MS
7459
7460 /* If we have installed this as the canonical typedef for this
7461 type, and that type has not been defined yet, delay emitting
956d6950 7462 the debug information for it, as we will emit it later. */
d2e5ee5c 7463 if (TYPE_MAIN_DECL (TREE_TYPE (decl)) == decl
cffa8729
MS
7464 && TYPE_SIZE (TREE_TYPE (decl)) == NULL_TREE)
7465 TYPE_DECL_SUPPRESS_DEBUG (decl) = 1;
7466
8d08fdba 7467 rest_of_decl_compilation (decl, NULL_PTR,
5566b478 7468 DECL_CONTEXT (decl) == NULL_TREE, at_eof);
8d08fdba
MS
7469 goto finish_end;
7470 }
8d08fdba
MS
7471 if (TREE_CODE (decl) != FUNCTION_DECL)
7472 {
7473 ttype = target_type (type);
8d08fdba
MS
7474 }
7475
7476 if (! DECL_EXTERNAL (decl) && TREE_READONLY (decl)
7477 && TYPE_NEEDS_CONSTRUCTING (type))
7478 {
7479
7480 /* Currently, GNU C++ puts constants in text space, making them
7481 impossible to initialize. In the future, one would hope for
7482 an operating system which understood the difference between
7483 initialization and the running of a program. */
7484 was_readonly = 1;
7485 TREE_READONLY (decl) = 0;
7486 }
7487
7488 if (TREE_CODE (decl) == FIELD_DECL)
7489 {
7490 if (init && init != error_mark_node)
7491 my_friendly_assert (TREE_PERMANENT (init), 147);
7492
7493 if (asmspec)
7494 {
7495 /* This must override the asm specifier which was placed
6060a796 7496 by grokclassfn. Lay this out fresh. */
8d08fdba
MS
7497 DECL_RTL (TREE_TYPE (decl)) = NULL_RTX;
7498 DECL_ASSEMBLER_NAME (decl) = get_identifier (asmspec);
7499 make_decl_rtl (decl, asmspec, 0);
7500 }
7501 }
7502 /* If `start_decl' didn't like having an initialization, ignore it now. */
7503 else if (init != NULL_TREE && DECL_INITIAL (decl) == NULL_TREE)
7504 init = NULL_TREE;
7505 else if (DECL_EXTERNAL (decl))
7506 ;
7507 else if (TREE_CODE (type) == REFERENCE_TYPE
7508 || (TYPE_LANG_SPECIFIC (type) && IS_SIGNATURE_REFERENCE (type)))
7509 {
f376e137
MS
7510 if (TREE_STATIC (decl))
7511 make_decl_rtl (decl, NULL_PTR,
a9aedbc2 7512 toplevel_bindings_p ()
f376e137 7513 || pseudo_global_level_p ());
a703fb38 7514 grok_reference_init (decl, type, init);
8d08fdba
MS
7515 init = NULL_TREE;
7516 }
7517
7518 GNU_xref_decl (current_function_decl, decl);
7519
a0a33927 7520 if (TREE_CODE (decl) == FIELD_DECL)
8d08fdba
MS
7521 ;
7522 else if (TREE_CODE (decl) == CONST_DECL)
7523 {
7524 my_friendly_assert (TREE_CODE (decl) != REFERENCE_TYPE, 148);
7525
7526 DECL_INITIAL (decl) = init;
7527
7528 /* This will keep us from needing to worry about our obstacks. */
7529 my_friendly_assert (init != NULL_TREE, 149);
7530 init = NULL_TREE;
7531 }
7532 else if (init)
7533 {
7534 if (TYPE_HAS_CONSTRUCTOR (type) || TYPE_NEEDS_CONSTRUCTING (type))
7535 {
7536 if (TREE_CODE (type) == ARRAY_TYPE)
7537 init = digest_init (type, init, (tree *) 0);
848b92e1
JM
7538 else if (TREE_CODE (init) == CONSTRUCTOR
7539 && TREE_HAS_CONSTRUCTOR (init))
8d08fdba 7540 {
f30432d7 7541 if (TYPE_NON_AGGREGATE_CLASS (type))
8d08fdba 7542 {
8251199e 7543 cp_error ("`%D' must be initialized by constructor, not by `{...}'",
a28e3c7f 7544 decl);
8d08fdba
MS
7545 init = error_mark_node;
7546 }
7547 else
7548 goto dont_use_constructor;
7549 }
8d08fdba
MS
7550 }
7551 else
7552 {
7553 dont_use_constructor:
7554 if (TREE_CODE (init) != TREE_VEC)
7555 init = store_init_value (decl, init);
8d08fdba 7556 }
28cbf42c
MS
7557
7558 if (init)
7559 /* We must hide the initializer so that expand_decl
7560 won't try to do something it does not understand. */
7561 init = obscure_complex_init (decl, init);
8d08fdba 7562 }
a0a33927
MS
7563 else if (DECL_EXTERNAL (decl))
7564 ;
8d08fdba
MS
7565 else if (TREE_CODE_CLASS (TREE_CODE (type)) == 't'
7566 && (IS_AGGR_TYPE (type) || TYPE_NEEDS_CONSTRUCTING (type)))
7567 {
7568 tree ctype = type;
7569 while (TREE_CODE (ctype) == ARRAY_TYPE)
7570 ctype = TREE_TYPE (ctype);
7571 if (! TYPE_NEEDS_CONSTRUCTING (ctype))
7572 {
7573 if (CLASSTYPE_READONLY_FIELDS_NEED_INIT (ctype))
8251199e 7574 cp_error ("structure `%D' with uninitialized const members", decl);
8d08fdba 7575 if (CLASSTYPE_REF_FIELDS_NEED_INIT (ctype))
8251199e 7576 cp_error ("structure `%D' with uninitialized reference members",
a28e3c7f 7577 decl);
8d08fdba
MS
7578 }
7579
91063b51 7580 check_for_uninitialized_const_var (decl);
8d08fdba 7581
6060a796
MS
7582 if (TYPE_SIZE (type) != NULL_TREE
7583 && TYPE_NEEDS_CONSTRUCTING (type))
28cbf42c 7584 init = obscure_complex_init (decl, NULL_TREE);
8d08fdba 7585
91063b51
MM
7586 }
7587 else
7588 check_for_uninitialized_const_var (decl);
7589
8d08fdba
MS
7590 /* For top-level declaration, the initial value was read in
7591 the temporary obstack. MAXINDEX, rtl, etc. to be made below
7592 must go in the permanent obstack; but don't discard the
7593 temporary data yet. */
7594
a9aedbc2 7595 if (toplevel_bindings_p () && temporary)
8d08fdba
MS
7596 end_temporary_allocation ();
7597
7598 /* Deduce size of array from initialization, if not already known. */
7599
7600 if (TREE_CODE (type) == ARRAY_TYPE
7601 && TYPE_DOMAIN (type) == NULL_TREE
7602 && TREE_CODE (decl) != TYPE_DECL)
7603 {
7604 int do_default
7605 = (TREE_STATIC (decl)
7606 /* Even if pedantic, an external linkage array
7607 may have incomplete type at first. */
7608 ? pedantic && ! DECL_EXTERNAL (decl)
7609 : !DECL_EXTERNAL (decl));
7610 tree initializer = init ? init : DECL_INITIAL (decl);
7611 int failure = complete_array_type (type, initializer, do_default);
7612
7613 if (failure == 1)
8251199e 7614 cp_error ("initializer fails to determine size of `%D'", decl);
8d08fdba
MS
7615
7616 if (failure == 2)
7617 {
7618 if (do_default)
8251199e 7619 cp_error ("array size missing in `%D'", decl);
8d08fdba
MS
7620 /* If a `static' var's size isn't known, make it extern as
7621 well as static, so it does not get allocated. If it's not
7622 `static', then don't mark it extern; finish_incomplete_decl
7623 will give it a default size and it will get allocated. */
7624 else if (!pedantic && TREE_STATIC (decl) && !TREE_PUBLIC (decl))
7625 DECL_EXTERNAL (decl) = 1;
7626 }
7627
7628 if (pedantic && TYPE_DOMAIN (type) != NULL_TREE
7629 && tree_int_cst_lt (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
7630 integer_zero_node))
8251199e 7631 cp_error ("zero-size array `%D'", decl);
8d08fdba
MS
7632
7633 layout_decl (decl, 0);
7634 }
7635
7636 if (TREE_CODE (decl) == VAR_DECL)
7637 {
7638 if (DECL_SIZE (decl) == NULL_TREE
ec255269 7639 && TYPE_SIZE (complete_type (TREE_TYPE (decl))) != NULL_TREE)
8d08fdba
MS
7640 layout_decl (decl, 0);
7641
7642 if (TREE_STATIC (decl) && DECL_SIZE (decl) == NULL_TREE)
7643 {
7644 /* A static variable with an incomplete type:
7645 that is an error if it is initialized.
7646 Otherwise, let it through, but if it is not `extern'
7647 then it may cause an error message later. */
7648 if (DECL_INITIAL (decl) != NULL_TREE)
8251199e 7649 cp_error ("storage size of `%D' isn't known", decl);
8d08fdba
MS
7650 init = NULL_TREE;
7651 }
7652 else if (!DECL_EXTERNAL (decl) && DECL_SIZE (decl) == NULL_TREE)
7653 {
7654 /* An automatic variable with an incomplete type: that is an error.
7655 Don't talk about array types here, since we took care of that
7656 message in grokdeclarator. */
8251199e 7657 cp_error ("storage size of `%D' isn't known", decl);
8d08fdba
MS
7658 TREE_TYPE (decl) = error_mark_node;
7659 }
7660 else if (!DECL_EXTERNAL (decl) && IS_AGGR_TYPE (ttype))
7661 /* Let debugger know it should output info for this type. */
7662 note_debug_info_needed (ttype);
7663
6eb3bb27 7664 if (TREE_STATIC (decl) && DECL_CLASS_SCOPE_P (decl))
d2e5ee5c
MS
7665 note_debug_info_needed (DECL_CONTEXT (decl));
7666
8d08fdba
MS
7667 if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl))
7668 && DECL_SIZE (decl) != NULL_TREE
7669 && ! TREE_CONSTANT (DECL_SIZE (decl)))
7670 {
7671 if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
7672 constant_expression_warning (DECL_SIZE (decl));
7673 else
8251199e 7674 cp_error ("storage size of `%D' isn't constant", decl);
8d08fdba
MS
7675 }
7676
c91a56d2
MS
7677 if (! DECL_EXTERNAL (decl) && TYPE_NEEDS_DESTRUCTOR (type)
7678 /* Cleanups for static variables are handled by `finish_file'. */
7679 && ! TREE_STATIC (decl))
8d08fdba
MS
7680 {
7681 int yes = suspend_momentary ();
2ee887f2 7682 cleanup = maybe_build_cleanup (decl);
8d08fdba
MS
7683 resume_momentary (yes);
7684 }
7685 }
7686 /* PARM_DECLs get cleanups, too. */
7687 else if (TREE_CODE (decl) == PARM_DECL && TYPE_NEEDS_DESTRUCTOR (type))
7688 {
7689 if (temporary)
7690 end_temporary_allocation ();
7691 cleanup = maybe_build_cleanup (decl);
7692 if (temporary)
7693 resume_temporary_allocation ();
7694 }
7695
7696 /* Output the assembler code and/or RTL code for variables and functions,
7697 unless the type is an undefined structure or union.
7698 If not, it will get done when the type is completed. */
7699
5566b478
MS
7700 was_incomplete = (DECL_SIZE (decl) == NULL_TREE);
7701
8d08fdba
MS
7702 if (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == FUNCTION_DECL
7703 || TREE_CODE (decl) == RESULT_DECL)
7704 {
7705 /* ??? FIXME: What about nested classes? */
e1467ff2 7706 int toplev = toplevel_bindings_p () || pseudo_global_level_p ();
8d08fdba 7707 int was_temp
d22c8596 7708 = (TREE_STATIC (decl) && TYPE_NEEDS_DESTRUCTOR (type)
8d08fdba
MS
7709 && allocation_temporary_p ());
7710
7711 if (was_temp)
7712 end_temporary_allocation ();
7713
5f2c99c4
JM
7714 /* Static data in a function with comdat linkage also has comdat
7715 linkage. */
42976354
BK
7716 if (TREE_CODE (decl) == VAR_DECL
7717 && TREE_STATIC (decl)
5f2c99c4 7718 /* Don't mess with __FUNCTION__. */
aeb302bb 7719 && ! TREE_ASM_WRITTEN (decl)
42976354
BK
7720 && current_function_decl
7721 && DECL_CONTEXT (decl) == current_function_decl
5f2c99c4
JM
7722 && (DECL_THIS_INLINE (current_function_decl)
7723 || DECL_TEMPLATE_INSTANTIATION (current_function_decl))
893de33c 7724 && TREE_PUBLIC (current_function_decl))
42976354 7725 {
5f2c99c4
JM
7726 /* Rather than try to get this right with inlining, we suppress
7727 inlining of such functions. */
aeb302bb
JM
7728 current_function_cannot_inline
7729 = "function with static variable cannot be inline";
5f2c99c4
JM
7730
7731 /* If flag_weak, we don't need to mess with this, as we can just
7732 make the function weak, and let it refer to its unique local
7733 copy. This works because we don't allow the function to be
7734 inlined. */
7735 if (! flag_weak)
7736 {
7737 if (DECL_INTERFACE_KNOWN (current_function_decl))
7738 {
7739 TREE_PUBLIC (decl) = 1;
7740 DECL_EXTERNAL (decl) = DECL_EXTERNAL (current_function_decl);
7741 }
7742 else if (DECL_INITIAL (decl) == NULL_TREE
7743 || DECL_INITIAL (decl) == error_mark_node)
7744 {
7745 TREE_PUBLIC (decl) = 1;
7746 DECL_COMMON (decl) = 1;
7747 }
7748 /* else we lose. We can only do this if we can use common,
7749 which we can't if it has been initialized. */
7750
7751 if (TREE_PUBLIC (decl))
7752 DECL_ASSEMBLER_NAME (decl)
7753 = build_static_name (current_function_decl, DECL_NAME (decl));
7754 else if (! DECL_ARTIFICIAL (decl))
7755 {
7756 cp_warning_at ("sorry: semantics of inline function static data `%#D' are wrong (you'll wind up with multiple copies)", decl);
7757 cp_warning_at (" you can work around this by removing the initializer"), decl;
7758 }
7759 }
42976354
BK
7760 }
7761
77be6f82
JM
7762 else if (TREE_CODE (decl) == VAR_DECL
7763 && DECL_LANG_SPECIFIC (decl)
7764 && DECL_COMDAT (decl))
7765 {
7766 /* Dynamically initialized vars go into common. */
7767 if (DECL_INITIAL (decl) == NULL_TREE
7768 || DECL_INITIAL (decl) == error_mark_node)
7769 DECL_COMMON (decl) = 1;
7770 else if (EMPTY_CONSTRUCTOR_P (DECL_INITIAL (decl)))
7771 {
7772 DECL_COMMON (decl) = 1;
7773 DECL_INITIAL (decl) = error_mark_node;
7774 }
7775 else
7776 {
7777 /* Statically initialized vars are weak or comdat, if
7778 supported. */
7779 if (flag_weak)
7780 make_decl_one_only (decl);
7781 else
be343556 7782 {
2c73f9f5 7783 /* We can't do anything useful; leave vars for explicit
be343556
JM
7784 instantiation. */
7785 DECL_EXTERNAL (decl) = 1;
7786 DECL_NOT_REALLY_EXTERN (decl) = 0;
7787 }
77be6f82
JM
7788 }
7789 }
7790
8d08fdba
MS
7791 if (TREE_CODE (decl) == VAR_DECL && DECL_VIRTUAL_P (decl))
7792 make_decl_rtl (decl, NULL_PTR, toplev);
7793 else if (TREE_CODE (decl) == VAR_DECL
7794 && TREE_READONLY (decl)
7795 && DECL_INITIAL (decl) != NULL_TREE
7796 && DECL_INITIAL (decl) != error_mark_node
a3203465 7797 && ! EMPTY_CONSTRUCTOR_P (DECL_INITIAL (decl)))
8d08fdba
MS
7798 {
7799 DECL_INITIAL (decl) = save_expr (DECL_INITIAL (decl));
7800
7801 if (asmspec)
7802 DECL_ASSEMBLER_NAME (decl) = get_identifier (asmspec);
7803
7804 if (! toplev
7805 && TREE_STATIC (decl)
7806 && ! TREE_SIDE_EFFECTS (decl)
7807 && ! TREE_PUBLIC (decl)
7808 && ! DECL_EXTERNAL (decl)
7809 && ! TYPE_NEEDS_DESTRUCTOR (type)
7810 && DECL_MODE (decl) != BLKmode)
7811 {
7812 /* If this variable is really a constant, then fill its DECL_RTL
7813 slot with something which won't take up storage.
7814 If something later should take its address, we can always give
7815 it legitimate RTL at that time. */
7816 DECL_RTL (decl) = gen_reg_rtx (DECL_MODE (decl));
7817 store_expr (DECL_INITIAL (decl), DECL_RTL (decl), 0);
7818 TREE_ASM_WRITTEN (decl) = 1;
7819 }
a0a33927 7820 else if (toplev && ! TREE_PUBLIC (decl))
8d08fdba 7821 {
8d08fdba 7822 /* If this is a static const, change its apparent linkage
db5ae43f 7823 if it belongs to a #pragma interface. */
a0a33927 7824 if (!interface_unknown)
8d08fdba
MS
7825 {
7826 TREE_PUBLIC (decl) = 1;
7827 DECL_EXTERNAL (decl) = interface_only;
7828 }
7829 make_decl_rtl (decl, asmspec, toplev);
7830 }
7831 else
5566b478 7832 rest_of_decl_compilation (decl, asmspec, toplev, at_eof);
8d08fdba
MS
7833 }
7834 else if (TREE_CODE (decl) == VAR_DECL
7835 && DECL_LANG_SPECIFIC (decl)
7836 && DECL_IN_AGGR_P (decl))
7837 {
7838 if (TREE_STATIC (decl))
7839 {
7840 if (init == NULL_TREE
7841#ifdef DEFAULT_STATIC_DEFS
7842 /* If this code is dead, then users must
7843 explicitly declare static member variables
7844 outside the class def'n as well. */
7845 && TYPE_NEEDS_CONSTRUCTING (type)
7846#endif
7847 )
7848 {
7849 DECL_EXTERNAL (decl) = 1;
7850 make_decl_rtl (decl, asmspec, 1);
7851 }
7852 else
5566b478 7853 rest_of_decl_compilation (decl, asmspec, toplev, at_eof);
8d08fdba
MS
7854 }
7855 else
7856 /* Just a constant field. Should not need any rtl. */
7857 goto finish_end0;
7858 }
7859 else
5566b478 7860 rest_of_decl_compilation (decl, asmspec, toplev, at_eof);
8d08fdba
MS
7861
7862 if (was_temp)
7863 resume_temporary_allocation ();
7864
7865 if (type != error_mark_node
7866 && TYPE_LANG_SPECIFIC (type)
7867 && CLASSTYPE_ABSTRACT_VIRTUALS (type))
7868 abstract_virtuals_error (decl, type);
7869 else if ((TREE_CODE (type) == FUNCTION_TYPE
7870 || TREE_CODE (type) == METHOD_TYPE)
7871 && TYPE_LANG_SPECIFIC (TREE_TYPE (type))
7872 && CLASSTYPE_ABSTRACT_VIRTUALS (TREE_TYPE (type)))
7873 abstract_virtuals_error (decl, TREE_TYPE (type));
7874
7875 if (TYPE_LANG_SPECIFIC (type) && IS_SIGNATURE (type))
7876 signature_error (decl, type);
7877 else if ((TREE_CODE (type) == FUNCTION_TYPE
7878 || TREE_CODE (type) == METHOD_TYPE)
7879 && TYPE_LANG_SPECIFIC (TREE_TYPE (type))
7880 && IS_SIGNATURE (TREE_TYPE (type)))
7881 signature_error (decl, TREE_TYPE (type));
7882
7883 if (TREE_CODE (decl) == FUNCTION_DECL)
faae18ab 7884 ;
67d743fe
MS
7885 else if (DECL_EXTERNAL (decl)
7886 && ! (DECL_LANG_SPECIFIC (decl)
7887 && DECL_NOT_REALLY_EXTERN (decl)))
5566b478
MS
7888 {
7889 if (init)
7890 DECL_INITIAL (decl) = init;
7891 }
8d08fdba
MS
7892 else if (TREE_STATIC (decl) && type != error_mark_node)
7893 {
7894 /* Cleanups for static variables are handled by `finish_file'. */
f30432d7
MS
7895 if (TYPE_NEEDS_CONSTRUCTING (type) || init != NULL_TREE
7896 || TYPE_NEEDS_DESTRUCTOR (type))
8d08fdba 7897 expand_static_init (decl, init);
8d08fdba
MS
7898 }
7899 else if (! toplev)
7900 {
7901 /* This is a declared decl which must live until the
7902 end of the binding contour. It may need a cleanup. */
7903
7904 /* Recompute the RTL of a local array now
7905 if it used to be an incomplete type. */
7906 if (was_incomplete && ! TREE_STATIC (decl))
7907 {
7908 /* If we used it already as memory, it must stay in memory. */
7909 TREE_ADDRESSABLE (decl) = TREE_USED (decl);
7910 /* If it's still incomplete now, no init will save it. */
7911 if (DECL_SIZE (decl) == NULL_TREE)
7912 DECL_INITIAL (decl) = NULL_TREE;
7913 expand_decl (decl);
7914 }
7915 else if (! TREE_ASM_WRITTEN (decl)
7916 && (TYPE_SIZE (type) != NULL_TREE
7917 || TREE_CODE (type) == ARRAY_TYPE))
7918 {
7919 /* Do this here, because we did not expand this decl's
7920 rtl in start_decl. */
7921 if (DECL_RTL (decl) == NULL_RTX)
7922 expand_decl (decl);
7923 else if (cleanup)
7924 {
8d2733ca
MS
7925 /* XXX: Why don't we use decl here? */
7926 /* Ans: Because it was already expanded? */
e349ee73 7927 if (! expand_decl_cleanup (NULL_TREE, cleanup))
8251199e 7928 cp_error ("parser lost in parsing declaration of `%D'",
8d2733ca 7929 decl);
8d08fdba
MS
7930 /* Cleanup used up here. */
7931 cleanup = NULL_TREE;
7932 }
7933 }
7934
2ee887f2
MS
7935 if (current_binding_level->is_for_scope)
7936 {
f181d4ae
MM
7937 struct binding_level *outer
7938 = current_binding_level->level_chain;
2ee887f2
MS
7939
7940 /* Check to see if the same name is already bound at
7941 the outer level, either because it was directly declared,
7942 or because a dead for-decl got preserved. In either case,
d22c8596 7943 the code would not have been valid under the ARM
2ee887f2
MS
7944 scope rules, so clear is_for_scope for the
7945 current_binding_level.
7946
7947 Otherwise, we need to preserve the temp slot for decl
e92cc029 7948 to last into the outer binding level. */
2ee887f2 7949
f181d4ae
MM
7950 tree outer_binding
7951 = TREE_CHAIN (IDENTIFIER_BINDING (DECL_NAME (decl)));
7952
7953 if (outer_binding && BINDING_LEVEL (outer_binding) == outer
7954 && (TREE_CODE (BINDING_VALUE (outer_binding))
7955 == VAR_DECL)
7956 && DECL_DEAD_FOR_LOCAL (BINDING_VALUE (outer_binding)))
2ee887f2 7957 {
f181d4ae
MM
7958 BINDING_VALUE (outer_binding)
7959 = DECL_SHADOWED_FOR_VAR (BINDING_VALUE (outer_binding));
7960 current_binding_level->is_for_scope = 0;
2ee887f2 7961 }
f181d4ae
MM
7962 else if (DECL_IN_MEMORY_P (decl))
7963 preserve_temp_slots (DECL_RTL (decl));
2ee887f2
MS
7964 }
7965
eb66be0e 7966 expand_start_target_temps ();
72b7eeff 7967
8d08fdba
MS
7968 if (DECL_SIZE (decl) && type != error_mark_node)
7969 {
7970 /* Compute and store the initial value. */
7971 expand_decl_init (decl);
0c4b14c4 7972 already_used = TREE_USED (decl) || TREE_USED (type);
8d08fdba
MS
7973
7974 if (init || TYPE_NEEDS_CONSTRUCTING (type))
7975 {
a28e3c7f
MS
7976 emit_line_note (DECL_SOURCE_FILE (decl),
7977 DECL_SOURCE_LINE (decl));
b370501f 7978 expand_aggr_init (decl, init, flags);
8d08fdba
MS
7979 }
7980
00595019
MS
7981 /* Set this to 0 so we can tell whether an aggregate which
7982 was initialized was ever used. Don't do this if it has a
7983 destructor, so we don't complain about the 'resource
7984 allocation is initialization' idiom. */
249555b0 7985 /* Now set attribute((unused)) on types so decls of
38e01259 7986 that type will be marked used. (see TREE_USED, above.)
249555b0
BK
7987 This avoids the warning problems this particular code
7988 tried to work around. */
0c4b14c4 7989
be99da77 7990 if (TYPE_NEEDS_CONSTRUCTING (type)
0c4b14c4 7991 && ! already_used
be99da77
MS
7992 && cleanup == NULL_TREE
7993 && DECL_NAME (decl))
8d08fdba 7994 TREE_USED (decl) = 0;
0c4b14c4
JM
7995
7996 if (already_used)
7997 TREE_USED (decl) = 1;
934c6b13 7998 }
eb66be0e 7999
934c6b13 8000 /* Cleanup any temporaries needed for the initial value. */
eb66be0e 8001 expand_end_target_temps ();
8d08fdba 8002
934c6b13
MS
8003 if (DECL_SIZE (decl) && type != error_mark_node)
8004 {
8d08fdba
MS
8005 /* Store the cleanup, if there was one. */
8006 if (cleanup)
8007 {
e349ee73 8008 if (! expand_decl_cleanup (decl, cleanup))
8251199e 8009 cp_error ("parser lost in parsing declaration of `%D'",
a28e3c7f 8010 decl);
8d08fdba
MS
8011 }
8012 }
8013 }
8014 finish_end0:
8015
8016 /* Undo call to `pushclass' that was done in `start_decl'
8017 due to initialization of qualified member variable.
8018 I.e., Foo::x = 10; */
8019 {
f30432d7 8020 tree context = DECL_REAL_CONTEXT (decl);
8d08fdba
MS
8021 if (context
8022 && TREE_CODE_CLASS (TREE_CODE (context)) == 't'
8023 && (TREE_CODE (decl) == VAR_DECL
8024 /* We also have a pushclass done that we need to undo here
8025 if we're at top level and declare a method. */
5566b478
MS
8026 || TREE_CODE (decl) == FUNCTION_DECL)
8027 /* If size hasn't been set, we're still defining it,
8028 and therefore inside the class body; don't pop
8029 the binding level.. */
8030 && TYPE_SIZE (context) != NULL_TREE
8031 && context == current_class_type)
8d08fdba
MS
8032 popclass (1);
8033 }
8034 }
8035
8036 finish_end:
8037
39211cd5
MS
8038 /* If requested, warn about definitions of large data objects. */
8039
8040 if (warn_larger_than
5156628f 8041 && ! processing_template_decl
39211cd5
MS
8042 && (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL)
8043 && !DECL_EXTERNAL (decl))
8044 {
8045 register tree decl_size = DECL_SIZE (decl);
8046
8047 if (decl_size && TREE_CODE (decl_size) == INTEGER_CST)
8048 {
8049 unsigned units = TREE_INT_CST_LOW (decl_size) / BITS_PER_UNIT;
8050
8051 if (units > larger_than_size)
8052 warning_with_decl (decl, "size of `%s' is %u bytes", units);
8053 }
8054 }
8055
8d08fdba
MS
8056 if (need_pop)
8057 {
8058 /* Resume permanent allocation, if not within a function. */
8059 /* The corresponding push_obstacks_nochange is in start_decl,
8060 start_method, groktypename, and in grokfield. */
8061 pop_obstacks ();
8062 }
8063
8064 if (was_readonly)
8065 TREE_READONLY (decl) = 1;
8d08fdba
MS
8066}
8067
82580166 8068/* This is here for a midend callback from c-common.c */
e92cc029 8069
82580166
MS
8070void
8071finish_decl (decl, init, asmspec_tree)
8072 tree decl, init;
8073 tree asmspec_tree;
8074{
8075 cp_finish_decl (decl, init, asmspec_tree, 1, 0);
8076}
8077
8d08fdba
MS
8078void
8079expand_static_init (decl, init)
8080 tree decl;
8081 tree init;
8082{
8083 tree oldstatic = value_member (decl, static_aggregates);
a4443a08 8084
8d08fdba
MS
8085 if (oldstatic)
8086 {
8087 if (TREE_PURPOSE (oldstatic) && init != NULL_TREE)
8251199e 8088 cp_error ("multiple initializations given for `%D'", decl);
8d08fdba 8089 }
a9aedbc2 8090 else if (! toplevel_bindings_p () && ! pseudo_global_level_p ())
8d08fdba
MS
8091 {
8092 /* Emit code to perform this initialization but once. */
8093 tree temp;
8094
e92cc029 8095 /* Remember this information until end of file. */
8d08fdba
MS
8096 push_obstacks (&permanent_obstack, &permanent_obstack);
8097
2036a15c
MM
8098 /* Emit code to perform this initialization but once. This code
8099 looks like:
8100
8101 static int temp = 0;
8102 if (!temp) {
8103 // Do initialization.
8104 temp = 1;
8105 // Register variable for destruction at end of program.
8106 }
8107
8108 Note that the `temp' variable is only set to 1 *after* the
8109 initialization is complete. This ensures that an exception,
8110 thrown during the construction, will cause the variable to
8111 reinitialized when we pass through this code again, as per:
8112
8113 [stmt.dcl]
8114
8115 If the initialization exits by throwing an exception, the
8116 initialization is not complete, so it will be tried again
8117 the next time control enters the declaration.
8118
8119 In theory, this process should be thread-safe, too; multiple
8120 threads should not be able to initialize the variable more
8121 than once. We don't yet attempt to ensure thread-safety. */
8d08fdba
MS
8122 temp = get_temp_name (integer_type_node, 1);
8123 rest_of_decl_compilation (temp, NULL_PTR, 0, 0);
2036a15c
MM
8124
8125 /* Begin the conditional initialization. */
8d08fdba
MS
8126 expand_start_cond (build_binary_op (EQ_EXPR, temp,
8127 integer_zero_node, 1), 0);
eb66be0e 8128 expand_start_target_temps ();
72b7eeff 8129
2036a15c 8130 /* Do the initialization itself. */
28cbf42c 8131 if (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl))
f30432d7 8132 || (init && TREE_CODE (init) == TREE_LIST))
8d08fdba 8133 {
b370501f 8134 expand_aggr_init (decl, init, 0);
8d08fdba
MS
8135 do_pending_stack_adjust ();
8136 }
f30432d7 8137 else if (init)
8d08fdba 8138 expand_assignment (decl, init, 0, 0);
f30432d7 8139
2036a15c
MM
8140 /* Set TEMP to 1. */
8141 expand_assignment (temp, integer_one_node, 0, 0);
8142
8143 /* Cleanup any temporaries needed for the initial value. If
8144 destroying one of the temporaries causes an exception to be
8145 thrown, then the object itself has still been fully
8146 constructed. */
eb66be0e 8147 expand_end_target_temps ();
72b7eeff 8148
2036a15c
MM
8149 /* Use atexit to register a function for destroying this static
8150 variable. */
72b7eeff
MS
8151 if (TYPE_NEEDS_DESTRUCTOR (TREE_TYPE (decl)))
8152 {
8153 tree cleanup, fcall;
8154 static tree Atexit = 0;
8155 if (Atexit == 0)
8156 {
8157 tree atexit_fndecl, PFV, pfvlist;
e92cc029 8158 /* Remember this information until end of file. */
72b7eeff
MS
8159 push_obstacks (&permanent_obstack, &permanent_obstack);
8160 PFV = build_pointer_type (build_function_type
8161 (void_type_node, void_list_node));
8162
8163 pfvlist = tree_cons (NULL_TREE, PFV, void_list_node);
8164
8165 push_lang_context (lang_name_c);
beb53fb8
JM
8166 atexit_fndecl
8167 = builtin_function ("atexit",
8168 build_function_type (void_type_node,
8169 pfvlist),
8170 NOT_BUILT_IN, NULL_PTR);
a6ecf8b6 8171 mark_used (atexit_fndecl);
72b7eeff
MS
8172 Atexit = default_conversion (atexit_fndecl);
8173 pop_lang_context ();
8174 pop_obstacks ();
8175 }
8176
7cfac595
MM
8177 /* Call build_cleanup before we enter the anonymous function
8178 so that any access checks will be done relative to the
8179 current scope, rather than the scope of the anonymous
8180 function. */
8181 fcall = build_cleanup (decl);
72b7eeff 8182 cleanup = start_anon_func ();
7cfac595 8183 expand_expr_stmt (fcall);
72b7eeff
MS
8184 end_anon_func ();
8185 mark_addressable (cleanup);
8186 cleanup = build_unary_op (ADDR_EXPR, cleanup, 0);
e66d884e 8187 fcall = build_function_call (Atexit, expr_tree_cons (NULL_TREE, cleanup, NULL_TREE));
72b7eeff
MS
8188 expand_expr_stmt (fcall);
8189 }
8190
8d08fdba
MS
8191 expand_end_cond ();
8192 if (TYPE_NEEDS_DESTRUCTOR (TREE_TYPE (decl)))
8193 {
8194 static_aggregates = perm_tree_cons (temp, decl, static_aggregates);
8195 TREE_STATIC (static_aggregates) = 1;
8196 }
8197
e92cc029 8198 /* Resume old (possibly temporary) allocation. */
8d08fdba
MS
8199 pop_obstacks ();
8200 }
8201 else
8202 {
8203 /* This code takes into account memory allocation
8204 policy of `start_decl'. Namely, if TYPE_NEEDS_CONSTRUCTING
8205 does not hold for this object, then we must make permanent
8206 the storage currently in the temporary obstack. */
8207 if (! TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl)))
8208 preserve_initializer ();
8209 static_aggregates = perm_tree_cons (init, decl, static_aggregates);
8210 }
8211}
8212\f
8213/* Make TYPE a complete type based on INITIAL_VALUE.
8214 Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
8215 2 if there was no information (in which case assume 1 if DO_DEFAULT). */
8216
8217int
8218complete_array_type (type, initial_value, do_default)
8219 tree type, initial_value;
8220 int do_default;
8221{
8222 register tree maxindex = NULL_TREE;
8223 int value = 0;
8224
8225 if (initial_value)
8226 {
8227 /* Note MAXINDEX is really the maximum index,
8228 one less than the size. */
8229 if (TREE_CODE (initial_value) == STRING_CST)
e1cd6e56
MS
8230 {
8231 int eltsize
8232 = int_size_in_bytes (TREE_TYPE (TREE_TYPE (initial_value)));
8233 maxindex = build_int_2 ((TREE_STRING_LENGTH (initial_value)
8234 / eltsize) - 1, 0);
8235 }
8d08fdba
MS
8236 else if (TREE_CODE (initial_value) == CONSTRUCTOR)
8237 {
e1cd6e56
MS
8238 tree elts = CONSTRUCTOR_ELTS (initial_value);
8239 maxindex = size_binop (MINUS_EXPR, integer_zero_node, size_one_node);
8240 for (; elts; elts = TREE_CHAIN (elts))
8241 {
8242 if (TREE_PURPOSE (elts))
8243 maxindex = TREE_PURPOSE (elts);
8244 else
8245 maxindex = size_binop (PLUS_EXPR, maxindex, size_one_node);
8246 }
8247 maxindex = copy_node (maxindex);
8d08fdba
MS
8248 }
8249 else
8250 {
8251 /* Make an error message unless that happened already. */
8252 if (initial_value != error_mark_node)
8253 value = 1;
8254
8255 /* Prevent further error messages. */
8256 maxindex = build_int_2 (0, 0);
8257 }
8258 }
8259
8260 if (!maxindex)
8261 {
8262 if (do_default)
8263 maxindex = build_int_2 (0, 0);
8264 value = 2;
8265 }
8266
8267 if (maxindex)
8268 {
51c184be
MS
8269 tree itype;
8270
8d08fdba 8271 TYPE_DOMAIN (type) = build_index_type (maxindex);
dff6b454 8272 if (! TREE_TYPE (maxindex))
8d08fdba 8273 TREE_TYPE (maxindex) = TYPE_DOMAIN (type);
51c184be
MS
8274 if (initial_value)
8275 itype = TREE_TYPE (initial_value);
8276 else
8277 itype = NULL;
8278 if (itype && !TYPE_DOMAIN (itype))
8279 TYPE_DOMAIN (itype) = TYPE_DOMAIN (type);
dff6b454
RK
8280 /* The type of the main variant should never be used for arrays
8281 of different sizes. It should only ever be completed with the
8282 size of the array. */
8283 if (! TYPE_DOMAIN (TYPE_MAIN_VARIANT (type)))
8284 TYPE_DOMAIN (TYPE_MAIN_VARIANT (type)) = TYPE_DOMAIN (type);
8d08fdba
MS
8285 }
8286
8287 /* Lay out the type now that we can get the real answer. */
8288
8289 layout_type (type);
8290
8291 return value;
8292}
8293\f
8294/* Return zero if something is declared to be a member of type
8295 CTYPE when in the context of CUR_TYPE. STRING is the error
8296 message to print in that case. Otherwise, quietly return 1. */
e92cc029 8297
8d08fdba
MS
8298static int
8299member_function_or_else (ctype, cur_type, string)
8300 tree ctype, cur_type;
8301 char *string;
8302{
8303 if (ctype && ctype != cur_type)
8304 {
8305 error (string, TYPE_NAME_STRING (ctype));
8306 return 0;
8307 }
8308 return 1;
8309}
8310\f
8311/* Subroutine of `grokdeclarator'. */
8312
8313/* Generate errors possibly applicable for a given set of specifiers.
8314 This is for ARM $7.1.2. */
e92cc029 8315
8d08fdba
MS
8316static void
8317bad_specifiers (object, type, virtualp, quals, inlinep, friendp, raises)
8318 tree object;
8319 char *type;
8320 int virtualp, quals, friendp, raises, inlinep;
8321{
8322 if (virtualp)
8251199e 8323 cp_error ("`%D' declared as a `virtual' %s", object, type);
8d08fdba 8324 if (inlinep)
8251199e 8325 cp_error ("`%D' declared as an `inline' %s", object, type);
8d08fdba 8326 if (quals)
8251199e 8327 cp_error ("`const' and `volatile' function specifiers on `%D' invalid in %s declaration",
a28e3c7f 8328 object, type);
8d08fdba 8329 if (friendp)
8251199e 8330 cp_error_at ("invalid friend declaration", object);
8d08fdba 8331 if (raises)
8251199e 8332 cp_error_at ("invalid exception specifications", object);
8d08fdba
MS
8333}
8334
8335/* CTYPE is class type, or null if non-class.
8336 TYPE is type this FUNCTION_DECL should have, either FUNCTION_TYPE
8337 or METHOD_TYPE.
8338 DECLARATOR is the function's name.
8339 VIRTUALP is truthvalue of whether the function is virtual or not.
8340 FLAGS are to be passed through to `grokclassfn'.
8341 QUALS are qualifiers indicating whether the function is `const'
8342 or `volatile'.
8343 RAISES is a list of exceptions that this function can raise.
8344 CHECK is 1 if we must find this method in CTYPE, 0 if we should
3ddfb0e6
MM
8345 not look, and -1 if we should not call `grokclassfn' at all.
8346
8347 Returns `error_mark_node' if something goes wrong, after issuing
8348 applicable error messages. */
e92cc029 8349
8d08fdba 8350static tree
386b8a85
JM
8351grokfndecl (ctype, type, declarator, orig_declarator, virtualp, flags, quals,
8352 raises, attrlist, check, friendp, publicp, inlinep, funcdef_flag,
2c73f9f5 8353 template_count, in_namespace)
8d08fdba
MS
8354 tree ctype, type;
8355 tree declarator;
386b8a85 8356 tree orig_declarator;
8d08fdba
MS
8357 int virtualp;
8358 enum overload_flags flags;
f30432d7 8359 tree quals, raises, attrlist;
386b8a85 8360 int check, friendp, publicp, inlinep, funcdef_flag, template_count;
2c73f9f5 8361 tree in_namespace;
8d08fdba
MS
8362{
8363 tree cname, decl;
8364 int staticp = ctype && TREE_CODE (type) == FUNCTION_TYPE;
42976354 8365 tree t;
8d08fdba
MS
8366
8367 if (ctype)
8368 cname = TREE_CODE (TYPE_NAME (ctype)) == TYPE_DECL
8369 ? TYPE_IDENTIFIER (ctype) : TYPE_NAME (ctype);
8370 else
8371 cname = NULL_TREE;
8372
8373 if (raises)
8374 {
f30432d7 8375 type = build_exception_variant (type, raises);
8d08fdba 8376 }
c11b6f21 8377
8d08fdba 8378 decl = build_lang_decl (FUNCTION_DECL, declarator, type);
2c73f9f5 8379 /* Propagate volatile out from type to decl. */
8d08fdba 8380 if (TYPE_VOLATILE (type))
893de33c 8381 TREE_THIS_VOLATILE (decl) = 1;
8d08fdba 8382
79c4d4b7 8383 /* If this decl has namespace scope, set that up. */
2c73f9f5
ML
8384 if (in_namespace)
8385 set_decl_namespace (decl, in_namespace);
79c4d4b7
JM
8386 else if (publicp && ! ctype)
8387 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
2c73f9f5 8388
0f8766b8
JM
8389 /* `main' and builtins have implicit 'C' linkage. */
8390 if ((MAIN_NAME_P (declarator)
8391 || (IDENTIFIER_LENGTH (declarator) > 10
8392 && IDENTIFIER_POINTER (declarator)[0] == '_'
8393 && IDENTIFIER_POINTER (declarator)[1] == '_'
8394 && strncmp (IDENTIFIER_POINTER (declarator)+2, "builtin_", 8) == 0))
8395 && current_lang_name == lang_name_cplusplus
94706a5c 8396 && ctype == NULL_TREE
79c4d4b7
JM
8397 /* NULL_TREE means global namespace. */
8398 && DECL_CONTEXT (decl) == NULL_TREE)
0f8766b8
JM
8399 DECL_LANGUAGE (decl) = lang_c;
8400
8d08fdba
MS
8401 /* Should probably propagate const out from type to decl I bet (mrs). */
8402 if (staticp)
8403 {
8404 DECL_STATIC_FUNCTION_P (decl) = 1;
8405 DECL_CONTEXT (decl) = ctype;
8d08fdba
MS
8406 }
8407
e76a2646
MS
8408 if (ctype)
8409 DECL_CLASS_CONTEXT (decl) = ctype;
8410
0f8766b8 8411 if (ctype == NULL_TREE && DECL_MAIN_P (decl))
faae18ab 8412 {
848b92e1
JM
8413 if (processing_template_decl)
8414 error ("cannot declare `main' to be a template");
faae18ab 8415 if (inlinep)
8251199e 8416 error ("cannot declare `main' to be inline");
faae18ab 8417 else if (! publicp)
8251199e 8418 error ("cannot declare `main' to be static");
faae18ab
MS
8419 inlinep = 0;
8420 publicp = 1;
8421 }
50a6dbd7 8422
59e76fc6
JM
8423 /* Members of anonymous types and local classes have no linkage; make
8424 them internal. */
8425 if (ctype && (ANON_AGGRNAME_P (TYPE_IDENTIFIER (ctype))
8426 || hack_decl_function_context (TYPE_MAIN_DECL (ctype))))
50a6dbd7
JM
8427 publicp = 0;
8428
8429 if (publicp)
8430 {
8431 /* [basic.link]: A name with no linkage (notably, the name of a class
8432 or enumeration declared in a local scope) shall not be used to
8433 declare an entity with linkage.
8434
8435 Only check this for public decls for now. */
8436 t = no_linkage_check (TREE_TYPE (decl));
8437 if (t)
8438 {
7f7c930e
JM
8439 if (ANON_AGGRNAME_P (TYPE_IDENTIFIER (t)))
8440 {
8441 if (DECL_LANGUAGE (decl) == lang_c)
8442 /* Allow this; it's pretty common in C. */;
8443 else
8444 cp_pedwarn ("non-local function `%#D' uses anonymous type",
8445 decl);
8446 }
50a6dbd7 8447 else
8251199e 8448 cp_pedwarn ("non-local function `%#D' uses local type `%T'",
cce2be43 8449 decl, t);
50a6dbd7
JM
8450 }
8451 }
8452
893de33c 8453 TREE_PUBLIC (decl) = publicp;
faae18ab 8454 if (! publicp)
893de33c
JM
8455 {
8456 DECL_INTERFACE_KNOWN (decl) = 1;
8457 DECL_NOT_REALLY_EXTERN (decl) = 1;
8458 }
faae18ab
MS
8459
8460 if (inlinep)
8461 DECL_THIS_INLINE (decl) = DECL_INLINE (decl) = 1;
8d08fdba
MS
8462
8463 DECL_EXTERNAL (decl) = 1;
8464 if (quals != NULL_TREE && TREE_CODE (type) == FUNCTION_TYPE)
8465 {
8251199e 8466 cp_error ("%smember function `%D' cannot have `%T' method qualifier",
8d08fdba
MS
8467 (ctype ? "static " : "non-"), decl, TREE_VALUE (quals));
8468 quals = NULL_TREE;
8469 }
8470
8471 if (IDENTIFIER_OPNAME_P (DECL_NAME (decl)))
8472 grok_op_properties (decl, virtualp, check < 0);
8473
e76a2646 8474 if (ctype && hack_decl_function_context (decl))
893de33c 8475 DECL_NO_STATIC_CHAIN (decl) = 1;
e76a2646 8476
42976354
BK
8477 for (t = TYPE_ARG_TYPES (TREE_TYPE (decl)); t; t = TREE_CHAIN (t))
8478 if (TREE_PURPOSE (t)
8479 && TREE_CODE (TREE_PURPOSE (t)) == DEFAULT_ARG)
8480 {
8481 add_defarg_fn (decl);
8482 break;
8483 }
8484
f9d94ea4
JM
8485 if (friendp
8486 && TREE_CODE (orig_declarator) == TEMPLATE_ID_EXPR)
8487 {
8488 if (funcdef_flag)
8251199e
JM
8489 cp_error
8490 ("defining explicit specialization `%D' in friend declaration",
f9d94ea4
JM
8491 orig_declarator);
8492 else
8493 {
7e2421f7
MM
8494 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
8495 {
8496 /* Something like `template <class T> friend void f<T>()'. */
8251199e 8497 cp_error ("template-id `%D' in declaration of primary template",
7e2421f7
MM
8498 orig_declarator);
8499 return error_mark_node;
8500 }
8501
f9d94ea4
JM
8502 /* A friend declaration of the form friend void f<>(). Record
8503 the information in the TEMPLATE_ID_EXPR. */
8504 SET_DECL_IMPLICIT_INSTANTIATION (decl);
8505 DECL_TEMPLATE_INFO (decl)
8506 = perm_tree_cons (TREE_OPERAND (orig_declarator, 0),
8507 TREE_OPERAND (orig_declarator, 1),
8508 NULL_TREE);
8509 }
f84b4be9 8510 }
386b8a85 8511
0f8766b8
JM
8512 /* Plain overloading: will not be grok'd by grokclassfn. */
8513 if (! ctype && ! processing_template_decl
8514 && DECL_LANGUAGE (decl) != lang_c
8515 && (! DECL_USE_TEMPLATE (decl) || name_mangling_version < 1))
8516 set_mangled_name_for_decl (decl);
8517
1eb0072d
JM
8518 if (funcdef_flag)
8519 /* Make the init_value nonzero so pushdecl knows this is not
8520 tentative. error_mark_node is replaced later with the BLOCK. */
8521 DECL_INITIAL (decl) = error_mark_node;
8522
75650646 8523 /* Caller will do the rest of this. */
8d08fdba
MS
8524 if (check < 0)
8525 return decl;
8526
5566b478 8527 if (check && funcdef_flag)
d2e5ee5c 8528 DECL_INITIAL (decl) = error_mark_node;
5566b478 8529
8d08fdba
MS
8530 if (flags == NO_SPECIAL && ctype && constructor_name (cname) == declarator)
8531 {
8532 tree tmp;
8533 /* Just handle constructors here. We could do this
8534 inside the following if stmt, but I think
8535 that the code is more legible by breaking this
8536 case out. See comments below for what each of
8537 the following calls is supposed to do. */
8538 DECL_CONSTRUCTOR_P (decl) = 1;
8539
b370501f 8540 grokclassfn (ctype, decl, flags, quals);
386b8a85 8541
e1467ff2
MM
8542 decl = check_explicit_specialization (orig_declarator, decl,
8543 template_count,
f84b4be9
JM
8544 2 * (funcdef_flag != 0) +
8545 4 * (friendp != 0));
6c30752f
MM
8546 if (decl == error_mark_node)
8547 return error_mark_node;
75650646 8548
b370501f 8549 if ((! TYPE_FOR_JAVA (ctype) || check_java_method (decl))
eff71ab0 8550 && check)
5566b478
MS
8551 {
8552 tmp = check_classfn (ctype, decl);
98c1c668
JM
8553
8554 if (tmp && TREE_CODE (tmp) == TEMPLATE_DECL)
8555 tmp = DECL_TEMPLATE_RESULT(tmp);
8556
e349ee73 8557 if (tmp && DECL_ARTIFICIAL (tmp))
8251199e 8558 cp_error ("definition of implicitly-declared `%D'", tmp);
5566b478
MS
8559 if (tmp && duplicate_decls (decl, tmp))
8560 return tmp;
8561 }
a0a33927 8562 if (! grok_ctor_properties (ctype, decl))
3ddfb0e6 8563 return error_mark_node;
8d08fdba
MS
8564 }
8565 else
8566 {
8567 tree tmp;
8568
8569 /* Function gets the ugly name, field gets the nice one.
8570 This call may change the type of the function (because
8571 of default parameters)! */
8572 if (ctype != NULL_TREE)
b370501f 8573 grokclassfn (ctype, decl, flags, quals);
8d08fdba 8574
e1467ff2
MM
8575 decl = check_explicit_specialization (orig_declarator, decl,
8576 template_count,
f84b4be9
JM
8577 2 * (funcdef_flag != 0) +
8578 4 * (friendp != 0));
6c30752f
MM
8579 if (decl == error_mark_node)
8580 return error_mark_node;
8581
eff71ab0 8582 if (ctype != NULL_TREE
b370501f 8583 && (! TYPE_FOR_JAVA (ctype) || check_java_method (decl))
eff71ab0 8584 && check)
5566b478
MS
8585 {
8586 tmp = check_classfn (ctype, decl);
98c1c668
JM
8587
8588 if (tmp && TREE_CODE (tmp) == TEMPLATE_DECL)
75650646 8589 tmp = DECL_TEMPLATE_RESULT (tmp);
98c1c668 8590
5566b478
MS
8591 if (tmp && DECL_STATIC_FUNCTION_P (tmp)
8592 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE)
8593 {
8594 /* Remove the `this' parm added by grokclassfn.
8595 XXX Isn't this done in start_function, too? */
8596 revert_static_member_fn (&decl, NULL, NULL);
8597 last_function_parms = TREE_CHAIN (last_function_parms);
8598 }
e349ee73 8599 if (tmp && DECL_ARTIFICIAL (tmp))
8251199e 8600 cp_error ("definition of implicitly-declared `%D'", tmp);
7834ab39
MS
8601 if (tmp)
8602 {
8603 if (!duplicate_decls (decl, tmp))
8604 my_friendly_abort (892);
8605 return tmp;
8606 }
5566b478 8607 }
8d08fdba
MS
8608
8609 if (ctype == NULL_TREE || check)
8610 return decl;
8611
7cfac595
MM
8612 if (attrlist)
8613 cplus_decl_attributes (decl, TREE_PURPOSE (attrlist),
8614 TREE_VALUE (attrlist));
8615 make_decl_rtl (decl, NULL_PTR, 1);
8616
8d08fdba
MS
8617 if (virtualp)
8618 {
2ee887f2 8619 DECL_VIRTUAL_P (decl) = 1;
8d08fdba
MS
8620 if (DECL_VINDEX (decl) == NULL_TREE)
8621 DECL_VINDEX (decl) = error_mark_node;
8622 IDENTIFIER_VIRTUAL_P (DECL_NAME (decl)) = 1;
8d08fdba
MS
8623 }
8624 }
8625 return decl;
8626}
8627
8628static tree
2c73f9f5 8629grokvardecl (type, declarator, specbits_in, initialized, constp, in_namespace)
8d08fdba
MS
8630 tree type;
8631 tree declarator;
d2e5ee5c 8632 RID_BIT_TYPE *specbits_in;
8d08fdba 8633 int initialized;
a9aedbc2 8634 int constp;
2c73f9f5 8635 tree in_namespace;
8d08fdba
MS
8636{
8637 tree decl;
f7da6097
MS
8638 RID_BIT_TYPE specbits;
8639
8640 specbits = *specbits_in;
8d08fdba
MS
8641
8642 if (TREE_CODE (type) == OFFSET_TYPE)
8643 {
8644 /* If you declare a static member so that it
8645 can be initialized, the code will reach here. */
5b605f68
MS
8646 tree basetype = TYPE_OFFSET_BASETYPE (type);
8647 type = TREE_TYPE (type);
8648 decl = build_lang_field_decl (VAR_DECL, declarator, type);
8649 DECL_CONTEXT (decl) = basetype;
8650 DECL_CLASS_CONTEXT (decl) = basetype;
f376e137 8651 DECL_ASSEMBLER_NAME (decl) = build_static_name (basetype, declarator);
8d08fdba
MS
8652 }
8653 else
30394414 8654 {
79c4d4b7
JM
8655 tree context;
8656
8657 if (in_namespace)
8658 context = in_namespace;
8659 else if (namespace_bindings_p () || RIDBIT_SETP (RID_EXTERN, specbits))
8660 context = current_namespace;
820fcad8
JM
8661 else
8662 context = NULL_TREE;
79c4d4b7 8663
30394414 8664 decl = build_decl (VAR_DECL, declarator, complete_type (type));
79c4d4b7
JM
8665
8666 if (context)
8667 set_decl_namespace (decl, context);
8668
8669 context = DECL_CONTEXT (decl);
8670 if (declarator && context && current_lang_name != lang_name_c)
8671 DECL_ASSEMBLER_NAME (decl) = build_static_name (context, declarator);
30394414 8672 }
6060a796 8673
2c73f9f5
ML
8674 if (in_namespace)
8675 set_decl_namespace (decl, in_namespace);
8676
8d08fdba
MS
8677 if (RIDBIT_SETP (RID_EXTERN, specbits))
8678 {
8679 DECL_THIS_EXTERN (decl) = 1;
8680 DECL_EXTERNAL (decl) = !initialized;
8681 }
8682
8683 /* In class context, static means one per class,
8684 public access, and static storage. */
2b9dc906 8685 if (DECL_CLASS_SCOPE_P (decl))
8d08fdba
MS
8686 {
8687 TREE_PUBLIC (decl) = 1;
8688 TREE_STATIC (decl) = 1;
5b605f68 8689 DECL_EXTERNAL (decl) = 0;
8d08fdba
MS
8690 }
8691 /* At top level, either `static' or no s.c. makes a definition
8692 (perhaps tentative), and absence of `static' makes it public. */
a9aedbc2 8693 else if (toplevel_bindings_p ())
8d08fdba 8694 {
a9aedbc2 8695 TREE_PUBLIC (decl) = (RIDBIT_NOTSETP (RID_STATIC, specbits)
9fffd093 8696 && (DECL_THIS_EXTERN (decl) || ! constp));
8d08fdba
MS
8697 TREE_STATIC (decl) = ! DECL_EXTERNAL (decl);
8698 }
8699 /* Not at top level, only `static' makes a static definition. */
8700 else
8701 {
8702 TREE_STATIC (decl) = !! RIDBIT_SETP (RID_STATIC, specbits);
8703 TREE_PUBLIC (decl) = DECL_EXTERNAL (decl);
8704 }
50a6dbd7
JM
8705
8706 if (TREE_PUBLIC (decl))
8707 {
8708 /* [basic.link]: A name with no linkage (notably, the name of a class
8709 or enumeration declared in a local scope) shall not be used to
8710 declare an entity with linkage.
8711
8712 Only check this for public decls for now. */
8713 tree t = no_linkage_check (TREE_TYPE (decl));
8714 if (t)
8715 {
8716 if (ANON_AGGRNAME_P (TYPE_IDENTIFIER (t)))
8717 /* Ignore for now; `enum { foo } e' is pretty common. */;
8718 else
8251199e 8719 cp_pedwarn ("non-local variable `%#D' uses local type `%T'",
50a6dbd7
JM
8720 decl, t);
8721 }
8722 }
8723
8d08fdba
MS
8724 return decl;
8725}
8726
d8f8dca1
MM
8727/* Create and return a canonical pointer to member function type, for
8728 TYPE, which is a POINTER_TYPE to a METHOD_TYPE. */
8d08fdba
MS
8729
8730tree
8731build_ptrmemfunc_type (type)
8732 tree type;
8733{
8734 tree fields[4];
8735 tree t;
8736 tree u;
8737
8738 /* If a canonical type already exists for this type, use it. We use
8739 this method instead of type_hash_canon, because it only does a
8740 simple equality check on the list of field members. */
8741
8742 if ((t = TYPE_GET_PTRMEMFUNC_TYPE (type)))
8743 return t;
8744
8745 push_obstacks (TYPE_OBSTACK (type), TYPE_OBSTACK (type));
8746
8747 u = make_lang_type (UNION_TYPE);
7ddedda4 8748 SET_IS_AGGR_TYPE (u, 0);
8d08fdba 8749 fields[0] = build_lang_field_decl (FIELD_DECL, pfn_identifier, type);
a28e3c7f
MS
8750 fields[1] = build_lang_field_decl (FIELD_DECL, delta2_identifier,
8751 delta_type_node);
8d08fdba
MS
8752 finish_builtin_type (u, "__ptrmemfunc_type", fields, 1, ptr_type_node);
8753 TYPE_NAME (u) = NULL_TREE;
8754
8755 t = make_lang_type (RECORD_TYPE);
8756
2c73f9f5 8757 /* Let the front-end know this is a pointer to member function... */
db5ae43f 8758 TYPE_PTRMEMFUNC_FLAG (t) = 1;
2c73f9f5 8759 /* ... and not really an aggregate. */
7ddedda4 8760 SET_IS_AGGR_TYPE (t, 0);
8d08fdba 8761
a28e3c7f
MS
8762 fields[0] = build_lang_field_decl (FIELD_DECL, delta_identifier,
8763 delta_type_node);
8764 fields[1] = build_lang_field_decl (FIELD_DECL, index_identifier,
8765 delta_type_node);
8d08fdba
MS
8766 fields[2] = build_lang_field_decl (FIELD_DECL, pfn_or_delta2_identifier, u);
8767 finish_builtin_type (t, "__ptrmemfunc_type", fields, 2, ptr_type_node);
8768
8769 pop_obstacks ();
8770
8771 /* Zap out the name so that the back-end will give us the debugging
8772 information for this anonymous RECORD_TYPE. */
8773 TYPE_NAME (t) = NULL_TREE;
8774
8775 TYPE_SET_PTRMEMFUNC_TYPE (type, t);
8776
e92cc029 8777 /* Seems to be wanted. */
8d08fdba
MS
8778 CLASSTYPE_GOT_SEMICOLON (t) = 1;
8779 return t;
8780}
8781
8782/* Given declspecs and a declarator,
8783 determine the name and type of the object declared
8784 and construct a ..._DECL node for it.
8785 (In one case we can return a ..._TYPE node instead.
8786 For invalid input we sometimes return 0.)
8787
8788 DECLSPECS is a chain of tree_list nodes whose value fields
8789 are the storage classes and type specifiers.
8790
8791 DECL_CONTEXT says which syntactic context this declaration is in:
8792 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
8793 FUNCDEF for a function definition. Like NORMAL but a few different
8794 error messages in each case. Return value may be zero meaning
8795 this definition is too screwy to try to parse.
8796 MEMFUNCDEF for a function definition. Like FUNCDEF but prepares to
8797 handle member functions (which have FIELD context).
8798 Return value may be zero meaning this definition is too screwy to
8799 try to parse.
8800 PARM for a parameter declaration (either within a function prototype
8801 or before a function body). Make a PARM_DECL, or return void_type_node.
db5ae43f 8802 CATCHPARM for a parameter declaration before a catch clause.
8d08fdba
MS
8803 TYPENAME if for a typename (in a cast or sizeof).
8804 Don't make a DECL node; just return the ..._TYPE node.
8805 FIELD for a struct or union field; make a FIELD_DECL.
8806 BITFIELD for a field with specified width.
8807 INITIALIZED is 1 if the decl has an initializer.
8808
8809 In the TYPENAME case, DECLARATOR is really an absolute declarator.
8810 It may also be so in the PARM case, for a prototype where the
8811 argument type is specified but not the name.
8812
8813 This function is where the complicated C meanings of `static'
8814 and `extern' are interpreted.
8815
8816 For C++, if there is any monkey business to do, the function which
8817 calls this one must do it, i.e., prepending instance variables,
8818 renaming overloaded function names, etc.
8819
8820 Note that for this C++, it is an error to define a method within a class
8821 which does not belong to that class.
8822
8823 Except in the case where SCOPE_REFs are implicitly known (such as
8824 methods within a class being redundantly qualified),
8825 declarations which involve SCOPE_REFs are returned as SCOPE_REFs
8826 (class_name::decl_name). The caller must also deal with this.
8827
8828 If a constructor or destructor is seen, and the context is FIELD,
8829 then the type gains the attribute TREE_HAS_x. If such a declaration
8830 is erroneous, NULL_TREE is returned.
8831
8832 QUALS is used only for FUNCDEF and MEMFUNCDEF cases. For a member
8833 function, these are the qualifiers to give to the `this' pointer.
8834
8835 May return void_type_node if the declarator turned out to be a friend.
8836 See grokfield for details. */
8837
8838enum return_types { return_normal, return_ctor, return_dtor, return_conversion };
8839
6ba89f8e
MM
8840/* DECL is a VAR_DECL defined in-class, whose TYPE is also given.
8841 Check to see that the definition is valid. Issue appropriate error
8842 messages. Return 1 if the definition is particularly bad, or 0
8843 otherwise. */
8844
8845int
8846check_static_variable_definition (decl, type)
8847 tree decl;
8848 tree type;
8849{
8850 /* Motion 10 at San Diego: If a static const integral data member is
8851 initialized with an integral constant expression, the initializer
8852 may appear either in the declaration (within the class), or in
8853 the definition, but not both. If it appears in the class, the
8854 member is a member constant. The file-scope definition is always
8855 required. */
8856 if (CLASS_TYPE_P (type) || TREE_CODE (type) == REFERENCE_TYPE)
8857 {
8858 cp_error ("in-class initialization of static data member of non-integral type `%T'",
8859 type);
8860 /* If we just return the declaration, crashes will sometimes
8861 occur. We therefore return void_type_node, as if this was a
8862 friend declaration, to cause callers to completely ignore
8863 this declaration. */
8864 return 1;
8865 }
8866 else if (!CP_TYPE_CONST_P (type))
8867 cp_error ("ANSI C++ forbids in-class initialization of non-const static member `%D'",
8868 decl);
8869 else if (pedantic && !INTEGRAL_TYPE_P (type))
8870 cp_pedwarn ("ANSI C++ forbids initialization of member constant `%D' of non-integral type `%T'", decl, type);
8871
8872 return 0;
8873}
8874
8d08fdba 8875tree
c11b6f21 8876grokdeclarator (declarator, declspecs, decl_context, initialized, attrlist)
8d08fdba
MS
8877 tree declspecs;
8878 tree declarator;
8879 enum decl_context decl_context;
8880 int initialized;
c11b6f21 8881 tree attrlist;
8d08fdba
MS
8882{
8883 RID_BIT_TYPE specbits;
8884 int nclasses = 0;
8885 tree spec;
8886 tree type = NULL_TREE;
8887 int longlong = 0;
8888 int constp;
91063b51 8889 int restrictp;
8d08fdba 8890 int volatilep;
91063b51 8891 int type_quals;
db5ae43f 8892 int virtualp, explicitp, friendp, inlinep, staticp;
8d08fdba
MS
8893 int explicit_int = 0;
8894 int explicit_char = 0;
37c46b43 8895 int defaulted_int = 0;
8d08fdba
MS
8896 int opaque_typedef = 0;
8897 tree typedef_decl = NULL_TREE;
8898 char *name;
8899 tree typedef_type = NULL_TREE;
8900 int funcdef_flag = 0;
8901 enum tree_code innermost_code = ERROR_MARK;
8902 int bitfield = 0;
6125f3be
DE
8903#if 0
8904 /* See the code below that used this. */
f6abb50a 8905 tree decl_machine_attr = NULL_TREE;
6125f3be 8906#endif
8d08fdba
MS
8907 /* Set this to error_mark_node for FIELD_DECLs we could not handle properly.
8908 All FIELD_DECLs we build here have `init' put into their DECL_INITIAL. */
8909 tree init = NULL_TREE;
8910
8911 /* Keep track of what sort of function is being processed
8912 so that we can warn about default return values, or explicit
8913 return values which do not match prescribed defaults. */
8914 enum return_types return_type = return_normal;
8915
8916 tree dname = NULL_TREE;
8917 tree ctype = current_class_type;
8918 tree ctor_return_type = NULL_TREE;
8919 enum overload_flags flags = NO_SPECIAL;
8d08fdba 8920 tree quals = NULL_TREE;
c11b6f21 8921 tree raises = NULL_TREE;
386b8a85 8922 int template_count = 0;
2c73f9f5 8923 tree in_namespace = NULL_TREE;
1eab9b56
JM
8924 tree inner_attrs;
8925 int ignore_attrs;
8d08fdba
MS
8926
8927 RIDBIT_RESET_ALL (specbits);
8928 if (decl_context == FUNCDEF)
8929 funcdef_flag = 1, decl_context = NORMAL;
8930 else if (decl_context == MEMFUNCDEF)
8931 funcdef_flag = -1, decl_context = FIELD;
8932 else if (decl_context == BITFIELD)
8933 bitfield = 1, decl_context = FIELD;
8934
8d08fdba
MS
8935 /* Look inside a declarator for the name being declared
8936 and get it as a string, for an error message. */
8937 {
be99da77
MS
8938 tree *next = &declarator;
8939 register tree decl;
8d08fdba
MS
8940 name = NULL;
8941
be99da77
MS
8942 while (next && *next)
8943 {
8944 decl = *next;
8945 switch (TREE_CODE (decl))
8d08fdba 8946 {
52fbc847
JM
8947 case TREE_LIST:
8948 /* For attributes. */
8949 next = &TREE_VALUE (decl);
8950 break;
8951
be99da77
MS
8952 case COND_EXPR:
8953 ctype = NULL_TREE;
8954 next = &TREE_OPERAND (decl, 0);
8955 break;
8d08fdba 8956
2c73f9f5 8957 case BIT_NOT_EXPR: /* For C++ destructors! */
8d08fdba 8958 {
be99da77
MS
8959 tree name = TREE_OPERAND (decl, 0);
8960 tree rename = NULL_TREE;
8961
8962 my_friendly_assert (flags == NO_SPECIAL, 152);
8963 flags = DTOR_FLAG;
8964 return_type = return_dtor;
5566b478
MS
8965 if (TREE_CODE (name) == TYPE_DECL)
8966 TREE_OPERAND (decl, 0) = name = constructor_name (name);
be99da77
MS
8967 my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, 153);
8968 if (ctype == NULL_TREE)
8969 {
8970 if (current_class_type == NULL_TREE)
8971 {
8251199e 8972 error ("destructors must be member functions");
be99da77
MS
8973 flags = NO_SPECIAL;
8974 }
8975 else
8976 {
8977 tree t = constructor_name (current_class_name);
8978 if (t != name)
8979 rename = t;
8980 }
8981 }
8d08fdba 8982 else
be99da77
MS
8983 {
8984 tree t = constructor_name (ctype);
8985 if (t != name)
8986 rename = t;
8987 }
51c184be 8988
be99da77 8989 if (rename)
39211cd5 8990 {
8251199e 8991 cp_error ("destructor `%T' must match class name `%T'",
5566b478 8992 name, rename);
be99da77 8993 TREE_OPERAND (decl, 0) = rename;
39211cd5 8994 }
be99da77 8995 next = &name;
51c184be 8996 }
be99da77 8997 break;
8d08fdba 8998
be99da77 8999 case ADDR_EXPR: /* C++ reference declaration */
2c73f9f5 9000 /* Fall through. */
be99da77
MS
9001 case ARRAY_REF:
9002 case INDIRECT_REF:
9003 ctype = NULL_TREE;
9004 innermost_code = TREE_CODE (decl);
9005 next = &TREE_OPERAND (decl, 0);
9006 break;
8d08fdba 9007
be99da77
MS
9008 case CALL_EXPR:
9009 if (parmlist_is_exprlist (TREE_OPERAND (decl, 1)))
8d08fdba 9010 {
be99da77
MS
9011 /* This is actually a variable declaration using constructor
9012 syntax. We need to call start_decl and cp_finish_decl so we
9013 can get the variable initialized... */
9014
9015 *next = TREE_OPERAND (decl, 0);
9016 init = TREE_OPERAND (decl, 1);
9017
a1774733 9018 decl = start_decl (declarator, declspecs, 1, NULL_TREE, NULL_TREE);
1eb0072d
JM
9019 if (decl)
9020 {
9021 /* Look for __unused__ attribute */
9022 if (TREE_USED (TREE_TYPE (decl)))
9023 TREE_USED (decl) = 1;
9024 finish_decl (decl, init, NULL_TREE);
9025 }
9026 else
9027 cp_error ("invalid declarator");
be99da77 9028 return 0;
8d08fdba 9029 }
be99da77
MS
9030 innermost_code = TREE_CODE (decl);
9031 if (decl_context == FIELD && ctype == NULL_TREE)
9032 ctype = current_class_type;
45537677 9033 if (ctype
c11b6f21 9034 && TREE_OPERAND (decl, 0)
45537677
MS
9035 && (TREE_CODE (TREE_OPERAND (decl, 0)) == TYPE_DECL
9036 && ((DECL_NAME (TREE_OPERAND (decl, 0))
9037 == constructor_name_full (ctype))
9038 || (DECL_NAME (TREE_OPERAND (decl, 0))
9039 == constructor_name (ctype)))))
be99da77
MS
9040 TREE_OPERAND (decl, 0) = constructor_name (ctype);
9041 next = &TREE_OPERAND (decl, 0);
9042 decl = *next;
9043 if (ctype != NULL_TREE
9044 && decl != NULL_TREE && flags != DTOR_FLAG
9045 && decl == constructor_name (ctype))
8d08fdba 9046 {
be99da77
MS
9047 return_type = return_ctor;
9048 ctor_return_type = ctype;
8d08fdba 9049 }
be99da77
MS
9050 ctype = NULL_TREE;
9051 break;
386b8a85
JM
9052
9053 case TEMPLATE_ID_EXPR:
9054 {
9055 tree fns = TREE_OPERAND (decl, 0);
9056
9057 if (TREE_CODE (fns) == LOOKUP_EXPR)
9058 fns = TREE_OPERAND (fns, 0);
9059
9060 if (TREE_CODE (fns) == IDENTIFIER_NODE)
9061 dname = fns;
f84b4be9 9062 else if (is_overloaded_fn (fns))
386b8a85
JM
9063 dname = DECL_NAME (get_first_fn (fns));
9064 else
f84b4be9 9065 my_friendly_abort (0);
386b8a85 9066 }
2c73f9f5 9067 /* Fall through. */
be99da77
MS
9068
9069 case IDENTIFIER_NODE:
386b8a85
JM
9070 if (TREE_CODE (decl) == IDENTIFIER_NODE)
9071 dname = decl;
9072
be99da77
MS
9073 next = 0;
9074
9075 if (is_rid (dname))
8d08fdba 9076 {
8251199e 9077 cp_error ("declarator-id missing; using reserved word `%D'",
be99da77
MS
9078 dname);
9079 name = IDENTIFIER_POINTER (dname);
8d08fdba 9080 }
be99da77 9081 if (! IDENTIFIER_OPNAME_P (dname)
956d6950 9082 /* GNU/Linux headers use '__op'. Arrgh. */
a703fb38 9083 || (IDENTIFIER_TYPENAME_P (dname) && ! TREE_TYPE (dname)))
be99da77 9084 name = IDENTIFIER_POINTER (dname);
8d08fdba
MS
9085 else
9086 {
be99da77 9087 if (IDENTIFIER_TYPENAME_P (dname))
8d08fdba 9088 {
be99da77
MS
9089 my_friendly_assert (flags == NO_SPECIAL, 154);
9090 flags = TYPENAME_FLAG;
9091 ctor_return_type = TREE_TYPE (dname);
9092 return_type = return_conversion;
8d08fdba 9093 }
be99da77 9094 name = operator_name_string (dname);
8d08fdba 9095 }
be99da77 9096 break;
8d08fdba 9097
be99da77
MS
9098 /* C++ extension */
9099 case SCOPE_REF:
9100 {
9101 /* Perform error checking, and decide on a ctype. */
9102 tree cname = TREE_OPERAND (decl, 0);
9103 if (cname == NULL_TREE)
9104 ctype = NULL_TREE;
2c73f9f5
ML
9105 else if (TREE_CODE (cname) == NAMESPACE_DECL)
9106 {
9107 ctype = NULL_TREE;
9108 in_namespace = TREE_OPERAND (decl, 0);
9109 TREE_OPERAND (decl, 0) = NULL_TREE;
9110 }
be99da77
MS
9111 else if (! is_aggr_type (cname, 1))
9112 TREE_OPERAND (decl, 0) = NULL_TREE;
9113 /* Must test TREE_OPERAND (decl, 1), in case user gives
9114 us `typedef (class::memfunc)(int); memfunc *memfuncptr;' */
9115 else if (TREE_OPERAND (decl, 1)
9116 && TREE_CODE (TREE_OPERAND (decl, 1)) == INDIRECT_REF)
9117 ctype = cname;
73b0fce8
KL
9118 else if (TREE_CODE (cname) == TEMPLATE_TYPE_PARM
9119 || TREE_CODE (cname) == TEMPLATE_TEMPLATE_PARM)
5566b478 9120 {
8251199e 9121 cp_error ("`%T::%D' is not a valid declarator", cname,
5566b478 9122 TREE_OPERAND (decl, 1));
8251199e 9123 cp_error (" perhaps you want `typename %T::%D' to make it a type",
5566b478
MS
9124 cname, TREE_OPERAND (decl, 1));
9125 return void_type_node;
9126 }
be99da77
MS
9127 else if (ctype == NULL_TREE)
9128 ctype = cname;
9129 else if (TREE_COMPLEXITY (decl) == current_class_depth)
9130 TREE_OPERAND (decl, 0) = ctype;
9131 else
9132 {
9133 if (! UNIQUELY_DERIVED_FROM_P (cname, ctype))
9134 {
8251199e 9135 cp_error ("type `%T' is not derived from type `%T'",
be99da77
MS
9136 cname, ctype);
9137 TREE_OPERAND (decl, 0) = NULL_TREE;
9138 }
9139 else
9140 ctype = cname;
9141 }
9142
c91a56d2
MS
9143 if (ctype && TREE_CODE (TREE_OPERAND (decl, 1)) == TYPE_DECL
9144 && ((DECL_NAME (TREE_OPERAND (decl, 1))
9145 == constructor_name_full (ctype))
9146 || (DECL_NAME (TREE_OPERAND (decl, 1))
9147 == constructor_name (ctype))))
be99da77
MS
9148 TREE_OPERAND (decl, 1) = constructor_name (ctype);
9149 next = &TREE_OPERAND (decl, 1);
9150 decl = *next;
9151 if (ctype)
9152 {
9153 if (TREE_CODE (decl) == IDENTIFIER_NODE
9154 && constructor_name (ctype) == decl)
9155 {
9156 return_type = return_ctor;
9157 ctor_return_type = ctype;
9158 }
9159 else if (TREE_CODE (decl) == BIT_NOT_EXPR
9160 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
9161 && (constructor_name (ctype) == TREE_OPERAND (decl, 0)
9162 || constructor_name_full (ctype) == TREE_OPERAND (decl, 0)))
9163 {
9164 return_type = return_dtor;
9165 ctor_return_type = ctype;
9166 flags = DTOR_FLAG;
9167 TREE_OPERAND (decl, 0) = constructor_name (ctype);
9168 next = &TREE_OPERAND (decl, 0);
9169 }
9170 }
9171 }
9172 break;
9173
9174 case ERROR_MARK:
9175 next = 0;
9176 break;
9177
45537677
MS
9178 case TYPE_DECL:
9179 /* Parse error puts this typespec where
9180 a declarator should go. */
8251199e 9181 cp_error ("`%T' specified as declarator-id", DECL_NAME (decl));
45537677 9182 if (TREE_TYPE (decl) == current_class_type)
8251199e 9183 cp_error (" perhaps you want `%T' for a constructor",
45537677
MS
9184 current_class_name);
9185 dname = DECL_NAME (decl);
9186 name = IDENTIFIER_POINTER (dname);
9187
e92cc029 9188 /* Avoid giving two errors for this. */
45537677
MS
9189 IDENTIFIER_CLASS_VALUE (dname) = NULL_TREE;
9190
9191 declspecs = temp_tree_cons (NULL_TREE, integer_type_node,
9192 declspecs);
9193 *next = dname;
9194 next = 0;
9195 break;
9196
be99da77 9197 default:
8251199e 9198 cp_compiler_error ("`%D' as declarator", decl);
be99da77 9199 return 0; /* We used to do a 155 abort here. */
8d08fdba 9200 }
be99da77 9201 }
8d08fdba
MS
9202 if (name == NULL)
9203 name = "type name";
9204 }
9205
9206 /* A function definition's declarator must have the form of
9207 a function declarator. */
9208
9209 if (funcdef_flag && innermost_code != CALL_EXPR)
9210 return 0;
9211
e1cd6e56
MS
9212 if (((dname && IDENTIFIER_OPNAME_P (dname)) || flags == TYPENAME_FLAG)
9213 && innermost_code != CALL_EXPR
9214 && ! (ctype && declspecs == NULL_TREE))
9215 {
8251199e 9216 cp_error ("declaration of `%D' as non-function", dname);
e1cd6e56
MS
9217 return void_type_node;
9218 }
9219
8d08fdba
MS
9220 /* Anything declared one level down from the top level
9221 must be one of the parameters of a function
9222 (because the body is at least two levels down). */
9223
9224 /* This heuristic cannot be applied to C++ nodes! Fixed, however,
9225 by not allowing C++ class definitions to specify their parameters
9226 with xdecls (must be spec.d in the parmlist).
9227
9228 Since we now wait to push a class scope until we are sure that
9229 we are in a legitimate method context, we must set oldcname
a9aedbc2
MS
9230 explicitly (since current_class_name is not yet alive).
9231
9232 We also want to avoid calling this a PARM if it is in a namespace. */
8d08fdba 9233
5566b478
MS
9234 if (decl_context == NORMAL && ! namespace_bindings_p ()
9235 && ! pseudo_global_level_p ())
a9aedbc2
MS
9236 {
9237 struct binding_level *b = current_binding_level;
9238 current_binding_level = b->level_chain;
9239 if (current_binding_level != 0 && toplevel_bindings_p ())
9240 decl_context = PARM;
9241 current_binding_level = b;
9242 }
8d08fdba
MS
9243
9244 /* Look through the decl specs and record which ones appear.
9245 Some typespecs are defined as built-in typenames.
9246 Others, the ones that are modifiers of other types,
9247 are represented by bits in SPECBITS: set the bits for
9248 the modifiers that appear. Storage class keywords are also in SPECBITS.
9249
9250 If there is a typedef name or a type, store the type in TYPE.
9251 This includes builtin typedefs such as `int'.
9252
9253 Set EXPLICIT_INT if the type is `int' or `char' and did not
9254 come from a user typedef.
9255
9256 Set LONGLONG if `long' is mentioned twice.
9257
9258 For C++, constructors and destructors have their own fast treatment. */
9259
9260 for (spec = declspecs; spec; spec = TREE_CHAIN (spec))
9261 {
9262 register int i;
9263 register tree id;
9264
9265 /* Certain parse errors slip through. For example,
9266 `int class;' is not caught by the parser. Try
9267 weakly to recover here. */
9268 if (TREE_CODE (spec) != TREE_LIST)
9269 return 0;
9270
9271 id = TREE_VALUE (spec);
9272
9273 if (TREE_CODE (id) == IDENTIFIER_NODE)
9274 {
a3203465
MS
9275 if (id == ridpointers[(int) RID_INT]
9276 || id == ridpointers[(int) RID_CHAR]
9277 || id == ridpointers[(int) RID_BOOL]
9278 || id == ridpointers[(int) RID_WCHAR])
8d08fdba
MS
9279 {
9280 if (type)
8ccc31eb
MS
9281 {
9282 if (id == ridpointers[(int) RID_BOOL])
8251199e 9283 error ("`bool' is now a keyword");
8ccc31eb 9284 else
8251199e 9285 cp_error ("extraneous `%T' ignored", id);
8ccc31eb 9286 }
8d08fdba
MS
9287 else
9288 {
a3203465
MS
9289 if (id == ridpointers[(int) RID_INT])
9290 explicit_int = 1;
9291 else if (id == ridpointers[(int) RID_CHAR])
9292 explicit_char = 1;
8d08fdba
MS
9293 type = TREE_TYPE (IDENTIFIER_GLOBAL_VALUE (id));
9294 }
9295 goto found;
9296 }
e92cc029 9297 /* C++ aggregate types. */
8d08fdba
MS
9298 if (IDENTIFIER_HAS_TYPE_VALUE (id))
9299 {
9300 if (type)
8251199e 9301 cp_error ("multiple declarations `%T' and `%T'", type, id);
8d08fdba
MS
9302 else
9303 type = IDENTIFIER_TYPE_VALUE (id);
9304 goto found;
9305 }
9306
f376e137 9307 for (i = (int) RID_FIRST_MODIFIER; i <= (int) RID_LAST_MODIFIER; i++)
8d08fdba
MS
9308 {
9309 if (ridpointers[i] == id)
9310 {
9311 if (i == (int) RID_LONG && RIDBIT_SETP (i, specbits))
9312 {
795add94 9313 if (pedantic && ! in_system_header && warn_long_long)
8251199e 9314 pedwarn ("ANSI C++ does not support `long long'");
9a3b49ac 9315 if (longlong)
8251199e 9316 error ("`long long long' is too long for GCC");
8d08fdba
MS
9317 else
9318 longlong = 1;
9319 }
9320 else if (RIDBIT_SETP (i, specbits))
8251199e 9321 pedwarn ("duplicate `%s'", IDENTIFIER_POINTER (id));
8d08fdba
MS
9322 RIDBIT_SET (i, specbits);
9323 goto found;
9324 }
9325 }
9326 }
e92cc029 9327 /* C++ aggregate types. */
73b0fce8 9328 else if (TREE_CODE (id) == TYPE_DECL || TREE_CODE (id) == TEMPLATE_DECL)
45537677
MS
9329 {
9330 if (type)
8251199e 9331 cp_error ("multiple declarations `%T' and `%T'", type,
45537677
MS
9332 TREE_TYPE (id));
9333 else
5566b478
MS
9334 {
9335 type = TREE_TYPE (id);
9336 TREE_VALUE (spec) = type;
9337 }
45537677
MS
9338 goto found;
9339 }
8d08fdba 9340 if (type)
8251199e 9341 error ("two or more data types in declaration of `%s'", name);
8d08fdba
MS
9342 else if (TREE_CODE (id) == IDENTIFIER_NODE)
9343 {
9344 register tree t = lookup_name (id, 1);
9345 if (!t || TREE_CODE (t) != TYPE_DECL)
8251199e 9346 error ("`%s' fails to be a typedef or built in type",
8d08fdba
MS
9347 IDENTIFIER_POINTER (id));
9348 else
9349 {
9350 type = TREE_TYPE (t);
6125f3be
DE
9351#if 0
9352 /* See the code below that used this. */
f6abb50a 9353 decl_machine_attr = DECL_MACHINE_ATTRIBUTES (id);
6125f3be 9354#endif
8d08fdba
MS
9355 typedef_decl = t;
9356 }
9357 }
bd6dd845 9358 else if (id != error_mark_node)
8d08fdba
MS
9359 /* Can't change CLASS nodes into RECORD nodes here! */
9360 type = id;
9361
9362 found: ;
9363 }
9364
9365 typedef_type = type;
9366
37c46b43 9367 /* No type at all: default to `int', and set DEFAULTED_INT
8d08fdba
MS
9368 because it was not a user-defined typedef.
9369 Except when we have a `typedef' inside a signature, in
9370 which case the type defaults to `unknown type' and is
9371 instantiated when assigning to a signature pointer or ref. */
9372
a3203465
MS
9373 if (type == NULL_TREE
9374 && (RIDBIT_SETP (RID_SIGNED, specbits)
9375 || RIDBIT_SETP (RID_UNSIGNED, specbits)
9376 || RIDBIT_SETP (RID_LONG, specbits)
9377 || RIDBIT_SETP (RID_SHORT, specbits)))
9378 {
9379 /* These imply 'int'. */
9380 type = integer_type_node;
37c46b43 9381 defaulted_int = 1;
a3203465
MS
9382 }
9383
8d08fdba
MS
9384 if (type == NULL_TREE)
9385 {
9386 explicit_int = -1;
9387 if (return_type == return_dtor)
9388 type = void_type_node;
9389 else if (return_type == return_ctor)
f30432d7 9390 type = build_pointer_type (ctor_return_type);
51c184be
MS
9391 else if (return_type == return_conversion)
9392 type = ctor_return_type;
8d08fdba
MS
9393 else if (current_class_type
9394 && IS_SIGNATURE (current_class_type)
fc378698 9395 && RIDBIT_SETP (RID_TYPEDEF, specbits)
8d08fdba
MS
9396 && (decl_context == FIELD || decl_context == NORMAL))
9397 {
9398 explicit_int = 0;
9399 opaque_typedef = 1;
9400 type = copy_node (opaque_type_node);
9401 }
9402 else
9403 {
41eff652
JM
9404 /* We handle `main' specially here, because 'main () { }' is so
9405 common. With no options, it is allowed. With -Wreturn-type,
9406 it is a warning. It is only an error with -pedantic-errors. */
9407 int is_main = (funcdef_flag
9408 && MAIN_NAME_P (dname)
9409 && ctype == NULL_TREE
9410 && in_namespace == NULL_TREE
9411 && current_namespace == global_namespace);
9412
1f901793
JM
9413 if (in_system_header)
9414 /* Allow it, sigh. */;
9415 else if (pedantic || ! is_main)
ff0cf464
JM
9416 cp_pedwarn ("ANSI C++ forbids declaration `%D' with no type",
9417 dname);
41eff652
JM
9418 else if (warn_return_type)
9419 cp_warning ("ANSI C++ forbids declaration `%D' with no type",
9420 dname);
9421
8d08fdba
MS
9422 type = integer_type_node;
9423 }
9424 }
9425 else if (return_type == return_dtor)
9426 {
8251199e 9427 error ("return type specification for destructor invalid");
8d08fdba
MS
9428 type = void_type_node;
9429 }
9430 else if (return_type == return_ctor)
9431 {
8251199e 9432 error ("return type specification for constructor invalid");
f30432d7 9433 type = build_pointer_type (ctor_return_type);
8d08fdba 9434 }
51c184be
MS
9435 else if (return_type == return_conversion)
9436 {
3bfdc719 9437 if (!same_type_p (type, ctor_return_type))
8251199e 9438 cp_error ("operator `%T' declared to return `%T'",
51c184be
MS
9439 ctor_return_type, type);
9440 else
8251199e 9441 cp_pedwarn ("return type specified for `operator %T'",
51c184be
MS
9442 ctor_return_type);
9443
9444 type = ctor_return_type;
9445 }
8d08fdba
MS
9446
9447 ctype = NULL_TREE;
9448
9449 /* Now process the modifiers that were specified
9450 and check for invalid combinations. */
9451
9452 /* Long double is a special combination. */
9453
9454 if (RIDBIT_SETP (RID_LONG, specbits)
9455 && TYPE_MAIN_VARIANT (type) == double_type_node)
9456 {
9457 RIDBIT_RESET (RID_LONG, specbits);
91063b51
MM
9458 type = build_qualified_type (long_double_type_node,
9459 CP_TYPE_QUALS (type));
8d08fdba
MS
9460 }
9461
9462 /* Check all other uses of type modifiers. */
9463
9464 if (RIDBIT_SETP (RID_UNSIGNED, specbits)
9465 || RIDBIT_SETP (RID_SIGNED, specbits)
9466 || RIDBIT_SETP (RID_LONG, specbits)
9467 || RIDBIT_SETP (RID_SHORT, specbits))
9468 {
9469 int ok = 0;
9470
9471 if (TREE_CODE (type) == REAL_TYPE)
8251199e 9472 error ("short, signed or unsigned invalid for `%s'", name);
b7484fbe 9473 else if (TREE_CODE (type) != INTEGER_TYPE)
8251199e 9474 error ("long, short, signed or unsigned invalid for `%s'", name);
8d08fdba
MS
9475 else if (RIDBIT_SETP (RID_LONG, specbits)
9476 && RIDBIT_SETP (RID_SHORT, specbits))
8251199e 9477 error ("long and short specified together for `%s'", name);
8d08fdba
MS
9478 else if ((RIDBIT_SETP (RID_LONG, specbits)
9479 || RIDBIT_SETP (RID_SHORT, specbits))
9480 && explicit_char)
8251199e 9481 error ("long or short specified with char for `%s'", name);
8d08fdba
MS
9482 else if ((RIDBIT_SETP (RID_LONG, specbits)
9483 || RIDBIT_SETP (RID_SHORT, specbits))
9484 && TREE_CODE (type) == REAL_TYPE)
8251199e 9485 error ("long or short specified with floating type for `%s'", name);
8d08fdba
MS
9486 else if (RIDBIT_SETP (RID_SIGNED, specbits)
9487 && RIDBIT_SETP (RID_UNSIGNED, specbits))
8251199e 9488 error ("signed and unsigned given together for `%s'", name);
8d08fdba
MS
9489 else
9490 {
9491 ok = 1;
37c46b43 9492 if (!explicit_int && !defaulted_int && !explicit_char && pedantic)
8d08fdba 9493 {
8251199e 9494 pedwarn ("long, short, signed or unsigned used invalidly for `%s'",
8d08fdba
MS
9495 name);
9496 if (flag_pedantic_errors)
9497 ok = 0;
9498 }
9499 }
9500
9501 /* Discard the type modifiers if they are invalid. */
9502 if (! ok)
9503 {
9504 RIDBIT_RESET (RID_UNSIGNED, specbits);
9505 RIDBIT_RESET (RID_SIGNED, specbits);
9506 RIDBIT_RESET (RID_LONG, specbits);
9507 RIDBIT_RESET (RID_SHORT, specbits);
9508 longlong = 0;
9509 }
9510 }
9511
37c46b43
MS
9512 if (RIDBIT_SETP (RID_COMPLEX, specbits)
9513 && TREE_CODE (type) != INTEGER_TYPE && TREE_CODE (type) != REAL_TYPE)
9514 {
8251199e 9515 error ("complex invalid for `%s'", name);
37c46b43
MS
9516 RIDBIT_RESET (RID_COMPLEX, specbits);
9517 }
9518
8d08fdba
MS
9519 /* Decide whether an integer type is signed or not.
9520 Optionally treat bitfields as signed by default. */
9521 if (RIDBIT_SETP (RID_UNSIGNED, specbits)
8d08fdba 9522 || (bitfield && ! flag_signed_bitfields
37c46b43 9523 && (explicit_int || defaulted_int || explicit_char
8d08fdba
MS
9524 /* A typedef for plain `int' without `signed'
9525 can be controlled just like plain `int'. */
9526 || ! (typedef_decl != NULL_TREE
9527 && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl)))
9528 && TREE_CODE (type) != ENUMERAL_TYPE
9529 && RIDBIT_NOTSETP (RID_SIGNED, specbits)))
9530 {
9531 if (longlong)
9532 type = long_long_unsigned_type_node;
9533 else if (RIDBIT_SETP (RID_LONG, specbits))
9534 type = long_unsigned_type_node;
9535 else if (RIDBIT_SETP (RID_SHORT, specbits))
9536 type = short_unsigned_type_node;
9537 else if (type == char_type_node)
9538 type = unsigned_char_type_node;
9539 else if (typedef_decl)
9540 type = unsigned_type (type);
9541 else
9542 type = unsigned_type_node;
9543 }
9544 else if (RIDBIT_SETP (RID_SIGNED, specbits)
9545 && type == char_type_node)
9546 type = signed_char_type_node;
9547 else if (longlong)
9548 type = long_long_integer_type_node;
9549 else if (RIDBIT_SETP (RID_LONG, specbits))
9550 type = long_integer_type_node;
9551 else if (RIDBIT_SETP (RID_SHORT, specbits))
9552 type = short_integer_type_node;
9553
37c46b43
MS
9554 if (RIDBIT_SETP (RID_COMPLEX, specbits))
9555 {
9556 /* If we just have "complex", it is equivalent to
9557 "complex double", but if any modifiers at all are specified it is
9558 the complex form of TYPE. E.g, "complex short" is
9559 "complex short int". */
9560
9561 if (defaulted_int && ! longlong
9562 && ! (RIDBIT_SETP (RID_LONG, specbits)
9563 || RIDBIT_SETP (RID_SHORT, specbits)
9564 || RIDBIT_SETP (RID_SIGNED, specbits)
9565 || RIDBIT_SETP (RID_UNSIGNED, specbits)))
9566 type = complex_double_type_node;
9567 else if (type == integer_type_node)
9568 type = complex_integer_type_node;
9569 else if (type == float_type_node)
9570 type = complex_float_type_node;
9571 else if (type == double_type_node)
9572 type = complex_double_type_node;
9573 else if (type == long_double_type_node)
9574 type = complex_long_double_type_node;
9575 else
9576 type = build_complex_type (type);
9577 }
9578
3ac3d9ea
MM
9579 if (return_type == return_conversion
9580 && (RIDBIT_SETP (RID_CONST, specbits)
91063b51
MM
9581 || RIDBIT_SETP (RID_VOLATILE, specbits)
9582 || RIDBIT_SETP (RID_RESTRICT, specbits)))
9583 cp_error ("qualifiers are not allowed on declaration of `operator %T'",
3ac3d9ea
MM
9584 ctor_return_type);
9585
8d08fdba
MS
9586 /* Set CONSTP if this declaration is `const', whether by
9587 explicit specification or via a typedef.
9588 Likewise for VOLATILEP. */
9589
91063b51
MM
9590 constp = !! RIDBIT_SETP (RID_CONST, specbits) + CP_TYPE_CONST_P (type);
9591 restrictp =
9592 !! RIDBIT_SETP (RID_RESTRICT, specbits) + CP_TYPE_RESTRICT_P (type);
9593 volatilep =
9594 !! RIDBIT_SETP (RID_VOLATILE, specbits) + CP_TYPE_VOLATILE_P (type);
9595 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
9596 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
9597 | (volatilep ? TYPE_QUAL_VOLATILE : 0));
9598 type = cp_build_qualified_type (type, type_quals);
8d08fdba
MS
9599 staticp = 0;
9600 inlinep = !! RIDBIT_SETP (RID_INLINE, specbits);
8d08fdba 9601 virtualp = RIDBIT_SETP (RID_VIRTUAL, specbits);
db5ae43f
MS
9602 RIDBIT_RESET (RID_VIRTUAL, specbits);
9603 explicitp = RIDBIT_SETP (RID_EXPLICIT, specbits) != 0;
9604 RIDBIT_RESET (RID_EXPLICIT, specbits);
8d08fdba 9605
8d08fdba
MS
9606 if (RIDBIT_SETP (RID_STATIC, specbits))
9607 staticp = 1 + (decl_context == FIELD);
9608
9609 if (virtualp && staticp == 2)
9610 {
8251199e 9611 cp_error ("member `%D' cannot be declared both virtual and static",
8d08fdba
MS
9612 dname);
9613 staticp = 0;
9614 }
9615 friendp = RIDBIT_SETP (RID_FRIEND, specbits);
8d08fdba
MS
9616 RIDBIT_RESET (RID_FRIEND, specbits);
9617
7e83af84
BK
9618 /* $7.1.2, Function specifiers */
9619 if (friendp && explicitp)
9620 error ("only declarations of constructors can be `explicit'");
9621
8d08fdba
MS
9622 if (RIDBIT_SETP (RID_MUTABLE, specbits))
9623 {
9624 if (decl_context == PARM)
9625 {
8251199e 9626 error ("non-member `%s' cannot be declared `mutable'", name);
8d08fdba
MS
9627 RIDBIT_RESET (RID_MUTABLE, specbits);
9628 }
9629 else if (friendp || decl_context == TYPENAME)
9630 {
8251199e 9631 error ("non-object member `%s' cannot be declared `mutable'", name);
8d08fdba
MS
9632 RIDBIT_RESET (RID_MUTABLE, specbits);
9633 }
8d08fdba
MS
9634 }
9635
9636 /* Warn if two storage classes are given. Default to `auto'. */
9637
9638 if (RIDBIT_ANY_SET (specbits))
9639 {
9640 if (RIDBIT_SETP (RID_STATIC, specbits)) nclasses++;
9641 if (RIDBIT_SETP (RID_EXTERN, specbits)) nclasses++;
9642 if (decl_context == PARM && nclasses > 0)
8251199e 9643 error ("storage class specifiers invalid in parameter declarations");
8d08fdba
MS
9644 if (RIDBIT_SETP (RID_TYPEDEF, specbits))
9645 {
9646 if (decl_context == PARM)
8251199e 9647 error ("typedef declaration invalid in parameter declaration");
8d08fdba
MS
9648 nclasses++;
9649 }
9650 if (RIDBIT_SETP (RID_AUTO, specbits)) nclasses++;
9651 if (RIDBIT_SETP (RID_REGISTER, specbits)) nclasses++;
9652 }
9653
9654 /* Give error if `virtual' is used outside of class declaration. */
b7484fbe
MS
9655 if (virtualp
9656 && (current_class_name == NULL_TREE || decl_context != FIELD))
8d08fdba 9657 {
8251199e 9658 error ("virtual outside class declaration");
8d08fdba
MS
9659 virtualp = 0;
9660 }
9661 if (current_class_name == NULL_TREE && RIDBIT_SETP (RID_MUTABLE, specbits))
9662 {
8251199e 9663 error ("only members can be declared mutable");
8d08fdba
MS
9664 RIDBIT_RESET (RID_MUTABLE, specbits);
9665 }
9666
9667 /* Static anonymous unions are dealt with here. */
9668 if (staticp && decl_context == TYPENAME
9669 && TREE_CODE (declspecs) == TREE_LIST
67ffc812 9670 && ANON_UNION_TYPE_P (TREE_VALUE (declspecs)))
8d08fdba
MS
9671 decl_context = FIELD;
9672
9673 /* Give error if `const,' `volatile,' `inline,' `friend,' or `virtual'
9674 is used in a signature member function declaration. */
9675 if (decl_context == FIELD
9676 && IS_SIGNATURE (current_class_type)
fc378698 9677 && RIDBIT_NOTSETP (RID_TYPEDEF, specbits))
8d08fdba 9678 {
91063b51 9679 if (type_quals != TYPE_UNQUALIFIED)
8d08fdba 9680 {
91063b51
MM
9681 error ("type qualifiers specified for signature member function `%s'", name);
9682 type_quals = TYPE_UNQUALIFIED;
8d08fdba
MS
9683 }
9684 if (inlinep)
9685 {
8251199e 9686 error ("`inline' specified for signature member function `%s'", name);
8d08fdba
MS
9687 /* Later, we'll make signature member functions inline. */
9688 inlinep = 0;
9689 }
9690 if (friendp)
9691 {
8251199e 9692 error ("`friend' declaration in signature definition");
8d08fdba
MS
9693 friendp = 0;
9694 }
9695 if (virtualp)
9696 {
8251199e 9697 error ("`virtual' specified for signature member function `%s'",
a28e3c7f 9698 name);
8d08fdba
MS
9699 /* Later, we'll make signature member functions virtual. */
9700 virtualp = 0;
9701 }
9702 }
9703
9704 /* Warn about storage classes that are invalid for certain
9705 kinds of declarations (parameters, typenames, etc.). */
9706
9707 if (nclasses > 1)
8251199e 9708 error ("multiple storage classes in declaration of `%s'", name);
8d08fdba
MS
9709 else if (decl_context != NORMAL && nclasses > 0)
9710 {
db5ae43f 9711 if ((decl_context == PARM || decl_context == CATCHPARM)
8d08fdba
MS
9712 && (RIDBIT_SETP (RID_REGISTER, specbits)
9713 || RIDBIT_SETP (RID_AUTO, specbits)))
9714 ;
fc378698
MS
9715 else if (RIDBIT_SETP (RID_TYPEDEF, specbits))
9716 ;
8d08fdba 9717 else if (decl_context == FIELD
fc378698 9718 && ! IS_SIGNATURE (current_class_type)
8d08fdba
MS
9719 /* C++ allows static class elements */
9720 && RIDBIT_SETP (RID_STATIC, specbits))
9721 /* C++ also allows inlines and signed and unsigned elements,
9722 but in those cases we don't come in here. */
9723 ;
9724 else
9725 {
9726 if (decl_context == FIELD)
9727 {
b7484fbe
MS
9728 tree tmp = NULL_TREE;
9729 register int op = 0;
9730
9731 if (declarator)
9732 {
9e9ff709
MS
9733 /* Avoid trying to get an operand off an identifier node. */
9734 if (TREE_CODE (declarator) == IDENTIFIER_NODE)
9735 tmp = declarator;
9736 else
9737 tmp = TREE_OPERAND (declarator, 0);
b7484fbe
MS
9738 op = IDENTIFIER_OPNAME_P (tmp);
9739 }
8251199e 9740 error ("storage class specified for %s `%s'",
8d08fdba
MS
9741 IS_SIGNATURE (current_class_type)
9742 ? (op
9743 ? "signature member operator"
9744 : "signature member function")
b7484fbe 9745 : (op ? "member operator" : "field"),
8d08fdba
MS
9746 op ? operator_name_string (tmp) : name);
9747 }
9748 else
db5ae43f 9749 error (((decl_context == PARM || decl_context == CATCHPARM)
8d08fdba
MS
9750 ? "storage class specified for parameter `%s'"
9751 : "storage class specified for typename"), name);
9752 RIDBIT_RESET (RID_REGISTER, specbits);
9753 RIDBIT_RESET (RID_AUTO, specbits);
9754 RIDBIT_RESET (RID_EXTERN, specbits);
9755
9756 if (decl_context == FIELD && IS_SIGNATURE (current_class_type))
9757 {
9758 RIDBIT_RESET (RID_STATIC, specbits);
9759 staticp = 0;
9760 }
9761 }
9762 }
9763 else if (RIDBIT_SETP (RID_EXTERN, specbits) && initialized && !funcdef_flag)
9764 {
a9aedbc2 9765 if (toplevel_bindings_p ())
8d08fdba 9766 {
59be0cdd 9767 /* It's common practice (and completely valid) to have a const
8d08fdba 9768 be initialized and declared extern. */
950ad3c3 9769 if (!(type_quals & TYPE_QUAL_CONST))
8251199e 9770 warning ("`%s' initialized and declared `extern'", name);
8d08fdba
MS
9771 }
9772 else
8251199e 9773 error ("`%s' has both `extern' and initializer", name);
8d08fdba
MS
9774 }
9775 else if (RIDBIT_SETP (RID_EXTERN, specbits) && funcdef_flag
a9aedbc2 9776 && ! toplevel_bindings_p ())
8251199e 9777 error ("nested function `%s' declared `extern'", name);
a9aedbc2 9778 else if (toplevel_bindings_p ())
8d08fdba
MS
9779 {
9780 if (RIDBIT_SETP (RID_AUTO, specbits))
8251199e 9781 error ("top-level declaration of `%s' specifies `auto'", name);
8d08fdba
MS
9782 }
9783
909e536a 9784 if (nclasses > 0 && friendp)
8251199e 9785 error ("storage class specifiers invalid in friend function declarations");
909e536a 9786
8d08fdba
MS
9787 /* Now figure out the structure of the declarator proper.
9788 Descend through it, creating more complex types, until we reach
9789 the declared identifier (or NULL_TREE, in an absolute declarator). */
9790
1eab9b56
JM
9791 inner_attrs = NULL_TREE;
9792 ignore_attrs = 0;
9793
386b8a85
JM
9794 while (declarator && TREE_CODE (declarator) != IDENTIFIER_NODE
9795 && TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
8d08fdba
MS
9796 {
9797 /* Each level of DECLARATOR is either an ARRAY_REF (for ...[..]),
9798 an INDIRECT_REF (for *...),
9799 a CALL_EXPR (for ...(...)),
9800 an identifier (for the name being declared)
9801 or a null pointer (for the place in an absolute declarator
9802 where the name was omitted).
9803 For the last two cases, we have just exited the loop.
9804
9805 For C++ it could also be
9806 a SCOPE_REF (for class :: ...). In this case, we have converted
9807 sensible names to types, and those are the values we use to
9808 qualify the member name.
9809 an ADDR_EXPR (for &...),
9810 a BIT_NOT_EXPR (for destructors)
8d08fdba
MS
9811
9812 At this point, TYPE is the type of elements of an array,
9813 or for a function to return, or for a pointer to point to.
9814 After this sequence of ifs, TYPE is the type of the
9815 array or function or pointer, and DECLARATOR has had its
9816 outermost layer removed. */
9817
bd6dd845 9818 if (type == error_mark_node)
8d08fdba
MS
9819 {
9820 if (TREE_CODE (declarator) == SCOPE_REF)
9821 declarator = TREE_OPERAND (declarator, 1);
9822 else
9823 declarator = TREE_OPERAND (declarator, 0);
9824 continue;
9825 }
9826 if (quals != NULL_TREE
9827 && (declarator == NULL_TREE
9828 || TREE_CODE (declarator) != SCOPE_REF))
9829 {
9830 if (ctype == NULL_TREE && TREE_CODE (type) == METHOD_TYPE)
9831 ctype = TYPE_METHOD_BASETYPE (type);
9832 if (ctype != NULL_TREE)
9833 {
8d08fdba 9834 tree dummy = build_decl (TYPE_DECL, NULL_TREE, type);
8d08fdba
MS
9835 ctype = grok_method_quals (ctype, dummy, quals);
9836 type = TREE_TYPE (dummy);
9837 quals = NULL_TREE;
9838 }
9839 }
1eab9b56
JM
9840
9841 /* See the comment for the TREE_LIST case, below. */
9842 if (ignore_attrs)
9843 ignore_attrs = 0;
9844 else if (inner_attrs)
9845 {
9846 decl_attributes (type, inner_attrs, NULL_TREE);
9847 inner_attrs = NULL_TREE;
9848 }
9849
8d08fdba
MS
9850 switch (TREE_CODE (declarator))
9851 {
52fbc847
JM
9852 case TREE_LIST:
9853 {
9854 /* We encode a declarator with embedded attributes using
1eab9b56
JM
9855 a TREE_LIST. The attributes apply to the declarator
9856 directly inside them, so we have to skip an iteration
9857 before applying them to the type. If the declarator just
9858 inside is the declarator-id, we apply the attrs to the
9859 decl itself. */
9860 inner_attrs = TREE_PURPOSE (declarator);
9861 ignore_attrs = 1;
52fbc847 9862 declarator = TREE_VALUE (declarator);
52fbc847
JM
9863 }
9864 break;
9865
8d08fdba
MS
9866 case ARRAY_REF:
9867 {
9868 register tree itype = NULL_TREE;
9869 register tree size = TREE_OPERAND (declarator, 1);
b7484fbe
MS
9870 /* The index is a signed object `sizetype' bits wide. */
9871 tree index_type = signed_type (sizetype);
8d08fdba
MS
9872
9873 declarator = TREE_OPERAND (declarator, 0);
9874
9875 /* Check for some types that there cannot be arrays of. */
9876
a1774733 9877 if (TREE_CODE (type) == VOID_TYPE)
8d08fdba 9878 {
8251199e 9879 cp_error ("declaration of `%D' as array of voids", dname);
8d08fdba
MS
9880 type = error_mark_node;
9881 }
9882
9883 if (TREE_CODE (type) == FUNCTION_TYPE)
9884 {
8251199e 9885 cp_error ("declaration of `%D' as array of functions", dname);
8d08fdba
MS
9886 type = error_mark_node;
9887 }
9888
9889 /* ARM $8.4.3: Since you can't have a pointer to a reference,
9890 you can't have arrays of references. If we allowed them,
59be0cdd 9891 then we'd be saying x[i] is valid for an array x, but
8d08fdba
MS
9892 then you'd have to ask: what does `*(x + i)' mean? */
9893 if (TREE_CODE (type) == REFERENCE_TYPE)
9894 {
9895 if (decl_context == TYPENAME)
8251199e 9896 cp_error ("cannot make arrays of references");
8d08fdba 9897 else
8251199e 9898 cp_error ("declaration of `%D' as array of references",
8d08fdba
MS
9899 dname);
9900 type = error_mark_node;
9901 }
9902
9903 if (TREE_CODE (type) == OFFSET_TYPE)
9904 {
8251199e 9905 cp_error ("declaration of `%D' as array of data members",
8d08fdba
MS
9906 dname);
9907 type = error_mark_node;
9908 }
9909
9910 if (TREE_CODE (type) == METHOD_TYPE)
9911 {
8251199e 9912 cp_error ("declaration of `%D' as array of function members",
8d08fdba
MS
9913 dname);
9914 type = error_mark_node;
9915 }
9916
9917 if (size == error_mark_node)
9918 type = error_mark_node;
3bfdc719
MM
9919 else if (TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type))
9920 {
9921 /* [dcl.array]
9922
9923 the constant expressions that specify the bounds of
9924 the arrays can be omitted only for the first member
9925 of the sequence. */
9926 cp_error ("declaration of `%D' as multidimensional array",
9927 dname);
9928 cp_error ("must have bounds for all dimensions except the first");
9929 type = error_mark_node;
9930 }
8d08fdba
MS
9931
9932 if (type == error_mark_node)
9933 continue;
9934
9935 if (size)
9936 {
9937 /* Must suspend_momentary here because the index
9938 type may need to live until the end of the function.
9939 For example, it is used in the declaration of a
9940 variable which requires destructing at the end of
9941 the function; then build_vec_delete will need this
9942 value. */
9943 int yes = suspend_momentary ();
2c73f9f5 9944 /* Might be a cast. */
8d08fdba
MS
9945 if (TREE_CODE (size) == NOP_EXPR
9946 && TREE_TYPE (size) == TREE_TYPE (TREE_OPERAND (size, 0)))
9947 size = TREE_OPERAND (size, 0);
9948
5db698f6
MM
9949 /* If this involves a template parameter, it will be a
9950 constant at instantiation time, but we don't know
9951 what the value is yet. Even if no template
9952 parameters are involved, we may an expression that
9953 is not a constant; we don't even simplify `1 + 2'
9954 when processing a template. */
9955 if (processing_template_decl)
5566b478 9956 {
e97e5263
JM
9957 /* Resolve a qualified reference to an enumerator or
9958 static const data member of ours. */
9959 if (TREE_CODE (size) == SCOPE_REF
9960 && TREE_OPERAND (size, 0) == current_class_type)
9961 {
9962 tree t = lookup_field (current_class_type,
9963 TREE_OPERAND (size, 1), 0, 0);
9964 if (t)
9965 size = t;
9966 }
9967
5566b478
MS
9968 itype = make_node (INTEGER_TYPE);
9969 TYPE_MIN_VALUE (itype) = size_zero_node;
9970 TYPE_MAX_VALUE (itype) = build_min
9971 (MINUS_EXPR, sizetype, size, integer_one_node);
9972 goto dont_grok_size;
9973 }
8d08fdba
MS
9974
9975 if (TREE_CODE (TREE_TYPE (size)) != INTEGER_TYPE
161c12b0
JM
9976 && TREE_CODE (TREE_TYPE (size)) != ENUMERAL_TYPE
9977 && TREE_CODE (TREE_TYPE (size)) != BOOLEAN_TYPE)
8d08fdba 9978 {
8251199e 9979 cp_error ("size of array `%D' has non-integer type",
8d08fdba
MS
9980 dname);
9981 size = integer_one_node;
9982 }
9983 if (TREE_READONLY_DECL_P (size))
9984 size = decl_constant_value (size);
e1cd6e56 9985 if (pedantic && integer_zerop (size))
8251199e 9986 cp_pedwarn ("ANSI C++ forbids zero-size array `%D'", dname);
8d08fdba
MS
9987 if (TREE_CONSTANT (size))
9988 {
dff6b454
RK
9989 int old_flag_pedantic_errors = flag_pedantic_errors;
9990 int old_pedantic = pedantic;
9991 pedantic = flag_pedantic_errors = 1;
9992 /* Always give overflow errors on array subscripts. */
8d08fdba 9993 constant_expression_warning (size);
dff6b454
RK
9994 pedantic = old_pedantic;
9995 flag_pedantic_errors = old_flag_pedantic_errors;
8d08fdba
MS
9996 if (INT_CST_LT (size, integer_zero_node))
9997 {
8251199e 9998 cp_error ("size of array `%D' is negative", dname);
8d08fdba
MS
9999 size = integer_one_node;
10000 }
8d08fdba
MS
10001 }
10002 else
10003 {
e1cd6e56 10004 if (pedantic)
a0a33927
MS
10005 {
10006 if (dname)
8251199e 10007 cp_pedwarn ("ANSI C++ forbids variable-size array `%D'",
a0a33927
MS
10008 dname);
10009 else
8251199e 10010 cp_pedwarn ("ANSI C++ forbids variable-size array");
a0a33927 10011 }
8d08fdba 10012 }
b7484fbe 10013
beb53fb8
JM
10014 itype
10015 = fold (build_binary_op (MINUS_EXPR,
37c46b43
MS
10016 cp_convert (index_type, size),
10017 cp_convert (index_type,
10018 integer_one_node), 1));
b7484fbe
MS
10019 if (! TREE_CONSTANT (itype))
10020 itype = variable_size (itype);
594740f3
MS
10021 else if (TREE_OVERFLOW (itype))
10022 {
8251199e 10023 error ("overflow in array dimension");
594740f3
MS
10024 TREE_OVERFLOW (itype) = 0;
10025 }
fc378698
MS
10026
10027 /* If we're a parm, we need to have a permanent type so
10028 mangling checks for re-use will work right. If both the
10029 element and index types are permanent, the array type
10030 will be, too. */
10031 if (decl_context == PARM
10032 && allocation_temporary_p () && TREE_PERMANENT (type))
10033 {
10034 push_obstacks (&permanent_obstack, &permanent_obstack);
10035 itype = build_index_type (itype);
10036 pop_obstacks ();
10037 }
10038 else
10039 itype = build_index_type (itype);
10040
5566b478 10041 dont_grok_size:
8d08fdba
MS
10042 resume_momentary (yes);
10043 }
10044
8d08fdba 10045 type = build_cplus_array_type (type, itype);
8d08fdba
MS
10046 ctype = NULL_TREE;
10047 }
10048 break;
10049
10050 case CALL_EXPR:
10051 {
10052 tree arg_types;
f376e137
MS
10053 int funcdecl_p;
10054 tree inner_parms = TREE_OPERAND (declarator, 1);
10055 tree inner_decl = TREE_OPERAND (declarator, 0);
8d08fdba
MS
10056
10057 /* Declaring a function type.
10058 Make sure we have a valid type for the function to return. */
8d08fdba 10059
91063b51 10060 /* We now know that the TYPE_QUALS don't apply to the
14ae7e7d 10061 decl, but to its return type. */
91063b51 10062 type_quals = TYPE_UNQUALIFIED;
8d08fdba
MS
10063
10064 /* Warn about some types functions can't return. */
10065
10066 if (TREE_CODE (type) == FUNCTION_TYPE)
10067 {
8251199e 10068 error ("`%s' declared as function returning a function", name);
8d08fdba
MS
10069 type = integer_type_node;
10070 }
10071 if (TREE_CODE (type) == ARRAY_TYPE)
10072 {
8251199e 10073 error ("`%s' declared as function returning an array", name);
8d08fdba
MS
10074 type = integer_type_node;
10075 }
10076
f376e137
MS
10077 if (inner_decl && TREE_CODE (inner_decl) == SCOPE_REF)
10078 inner_decl = TREE_OPERAND (inner_decl, 1);
10079
386b8a85
JM
10080 if (inner_decl && TREE_CODE (inner_decl) == TEMPLATE_ID_EXPR)
10081 inner_decl = dname;
10082
b7484fbe
MS
10083 /* Pick up type qualifiers which should be applied to `this'. */
10084 quals = TREE_OPERAND (declarator, 2);
10085
c11b6f21
MS
10086 /* Pick up the exception specifications. */
10087 raises = TREE_TYPE (declarator);
10088
f376e137
MS
10089 /* Say it's a definition only for the CALL_EXPR
10090 closest to the identifier. */
beb53fb8 10091 funcdecl_p
386b8a85
JM
10092 = inner_decl
10093 && (TREE_CODE (inner_decl) == IDENTIFIER_NODE
10094 || TREE_CODE (inner_decl) == TEMPLATE_ID_EXPR
10095 || TREE_CODE (inner_decl) == BIT_NOT_EXPR);
10096
8d08fdba
MS
10097 if (ctype == NULL_TREE
10098 && decl_context == FIELD
f376e137 10099 && funcdecl_p
8d08fdba
MS
10100 && (friendp == 0 || dname == current_class_name))
10101 ctype = current_class_type;
10102
51c184be 10103 if (ctype && return_type == return_conversion)
8d08fdba
MS
10104 TYPE_HAS_CONVERSION (ctype) = 1;
10105 if (ctype && constructor_name (ctype) == dname)
10106 {
10107 /* We are within a class's scope. If our declarator name
10108 is the same as the class name, and we are defining
10109 a function, then it is a constructor/destructor, and
10110 therefore returns a void type. */
10111
10112 if (flags == DTOR_FLAG)
10113 {
10114 /* ANSI C++ June 5 1992 WP 12.4.1. A destructor may
10115 not be declared const or volatile. A destructor
10116 may not be static. */
10117 if (staticp == 2)
8251199e 10118 error ("destructor cannot be static member function");
b7484fbe 10119 if (quals)
8d08fdba 10120 {
4f70a846
MM
10121 cp_error ("destructors may not be `%s'",
10122 IDENTIFIER_POINTER (TREE_VALUE (quals)));
10123 quals = NULL_TREE;
8d08fdba
MS
10124 }
10125 if (decl_context == FIELD)
10126 {
10127 if (! member_function_or_else (ctype, current_class_type,
10128 "destructor for alien class `%s' cannot be a member"))
10129 return void_type_node;
10130 }
10131 }
2c73f9f5 10132 else /* It's a constructor. */
8d08fdba 10133 {
db5ae43f
MS
10134 if (explicitp == 1)
10135 explicitp = 2;
8d08fdba
MS
10136 /* ANSI C++ June 5 1992 WP 12.1.2. A constructor may
10137 not be declared const or volatile. A constructor may
10138 not be virtual. A constructor may not be static. */
10139 if (staticp == 2)
8251199e 10140 error ("constructor cannot be static member function");
8d08fdba
MS
10141 if (virtualp)
10142 {
8251199e 10143 pedwarn ("constructors cannot be declared virtual");
8d08fdba
MS
10144 virtualp = 0;
10145 }
b7484fbe 10146 if (quals)
8d08fdba 10147 {
4f70a846
MM
10148 cp_error ("constructors may not be `%s'",
10149 IDENTIFIER_POINTER (TREE_VALUE (quals)));
10150 quals = NULL_TREE;
8d08fdba 10151 }
8d08fdba 10152 {
51c184be 10153 RID_BIT_TYPE tmp_bits;
fc378698 10154 bcopy ((void*)&specbits, (void*)&tmp_bits, sizeof (RID_BIT_TYPE));
51c184be
MS
10155 RIDBIT_RESET (RID_INLINE, tmp_bits);
10156 RIDBIT_RESET (RID_STATIC, tmp_bits);
10157 if (RIDBIT_ANY_SET (tmp_bits))
8251199e 10158 error ("return value type specifier for constructor ignored");
8d08fdba 10159 }
f30432d7 10160 type = build_pointer_type (ctype);
beb53fb8
JM
10161 if (decl_context == FIELD
10162 && IS_SIGNATURE (current_class_type))
8d08fdba 10163 {
8251199e 10164 error ("constructor not allowed in signature");
8d08fdba
MS
10165 return void_type_node;
10166 }
10167 else if (decl_context == FIELD)
10168 {
10169 if (! member_function_or_else (ctype, current_class_type,
10170 "constructor for alien class `%s' cannot be member"))
10171 return void_type_node;
10172 TYPE_HAS_CONSTRUCTOR (ctype) = 1;
10173 if (return_type != return_ctor)
10174 return NULL_TREE;
10175 }
10176 }
10177 if (decl_context == FIELD)
10178 staticp = 0;
10179 }
b7484fbe 10180 else if (friendp)
8d08fdba 10181 {
b7484fbe 10182 if (initialized)
8251199e 10183 error ("can't initialize friend function `%s'", name);
b7484fbe
MS
10184 if (virtualp)
10185 {
10186 /* Cannot be both friend and virtual. */
8251199e 10187 error ("virtual functions cannot be friends");
b7484fbe
MS
10188 RIDBIT_RESET (RID_FRIEND, specbits);
10189 friendp = 0;
10190 }
28cbf42c 10191 if (decl_context == NORMAL)
8251199e 10192 error ("friend declaration not in class definition");
28cbf42c 10193 if (current_function_decl && funcdef_flag)
8251199e 10194 cp_error ("can't define friend function `%s' in a local class definition",
28cbf42c 10195 name);
8d08fdba
MS
10196 }
10197
8d08fdba
MS
10198 /* Construct the function type and go to the next
10199 inner layer of declarator. */
10200
f376e137 10201 declarator = TREE_OPERAND (declarator, 0);
8d08fdba 10202
f376e137
MS
10203 /* FIXME: This is where default args should be fully
10204 processed. */
8d08fdba 10205
f376e137 10206 arg_types = grokparms (inner_parms, funcdecl_p ? funcdef_flag : 0);
8d08fdba 10207
4f70a846 10208 if (declarator && flags == DTOR_FLAG)
8d08fdba 10209 {
4f70a846
MM
10210 /* A destructor declared in the body of a class will
10211 be represented as a BIT_NOT_EXPR. But, we just
10212 want the underlying IDENTIFIER. */
8d08fdba 10213 if (TREE_CODE (declarator) == BIT_NOT_EXPR)
4f70a846
MM
10214 declarator = TREE_OPERAND (declarator, 0);
10215
10216 if (strict_prototype == 0 && arg_types == NULL_TREE)
10217 arg_types = void_list_node;
10218 else if (arg_types == NULL_TREE
10219 || arg_types != void_list_node)
8d08fdba 10220 {
4f70a846
MM
10221 cp_error ("destructors may not have parameters");
10222 arg_types = void_list_node;
10223 last_function_parms = NULL_TREE;
8d08fdba
MS
10224 }
10225 }
10226
d22c8596 10227 /* ANSI says that `const int foo ();'
8d08fdba 10228 does not make the function foo const. */
d22c8596 10229 type = build_function_type (type, arg_types);
42976354
BK
10230
10231 {
10232 tree t;
10233 for (t = arg_types; t; t = TREE_CHAIN (t))
10234 if (TREE_PURPOSE (t)
10235 && TREE_CODE (TREE_PURPOSE (t)) == DEFAULT_ARG)
10236 {
10237 add_defarg_fn (type);
10238 break;
10239 }
10240 }
8d08fdba
MS
10241 }
10242 break;
10243
10244 case ADDR_EXPR:
10245 case INDIRECT_REF:
10246 /* Filter out pointers-to-references and references-to-references.
10247 We can get these if a TYPE_DECL is used. */
10248
10249 if (TREE_CODE (type) == REFERENCE_TYPE)
10250 {
8251199e 10251 error ("cannot declare %s to references",
8d08fdba
MS
10252 TREE_CODE (declarator) == ADDR_EXPR
10253 ? "references" : "pointers");
10254 declarator = TREE_OPERAND (declarator, 0);
10255 continue;
10256 }
10257
a5894242
MS
10258 if (TREE_CODE (type) == OFFSET_TYPE
10259 && (TREE_CODE (TREE_TYPE (type)) == VOID_TYPE
10260 || TREE_CODE (TREE_TYPE (type)) == REFERENCE_TYPE))
10261 {
8251199e 10262 cp_error ("cannot declare pointer to `%#T' member",
a5894242
MS
10263 TREE_TYPE (type));
10264 type = TREE_TYPE (type);
10265 }
10266
61a127b3
MM
10267 /* Merge any constancy or volatility into the target type
10268 for the pointer. */
10269
91063b51
MM
10270 /* We now know that the TYPE_QUALS don't apply to the decl,
10271 but to the target of the pointer. */
10272 type_quals = TYPE_UNQUALIFIED;
8d08fdba
MS
10273
10274 if (IS_SIGNATURE (type))
10275 {
10276 if (TREE_CODE (declarator) == ADDR_EXPR)
10277 {
63718c49
GB
10278 if (CLASSTYPE_METHOD_VEC (type) == NULL_TREE
10279 && TYPE_SIZE (type))
8251199e 10280 cp_warning ("empty signature `%T' used in signature reference declaration",
63718c49 10281 type);
8d08fdba 10282#if 0
14ae7e7d 10283 type = build_signature_reference_type (type);
8d08fdba
MS
10284#else
10285 sorry ("signature reference");
10286 return NULL_TREE;
10287#endif
10288 }
10289 else
10290 {
63718c49
GB
10291 if (CLASSTYPE_METHOD_VEC (type) == NULL_TREE
10292 && TYPE_SIZE (type))
8251199e 10293 cp_warning ("empty signature `%T' used in signature pointer declaration",
63718c49 10294 type);
14ae7e7d 10295 type = build_signature_pointer_type (type);
8d08fdba 10296 }
8d08fdba
MS
10297 }
10298 else if (TREE_CODE (declarator) == ADDR_EXPR)
10299 {
69851283
MM
10300 if (TREE_CODE (type) == VOID_TYPE)
10301 error ("invalid type: `void &'");
8d08fdba 10302 else
69851283 10303 type = build_reference_type (type);
8d08fdba
MS
10304 }
10305 else if (TREE_CODE (type) == METHOD_TYPE)
d8f8dca1 10306 type = build_ptrmemfunc_type (build_pointer_type (type));
8d08fdba
MS
10307 else
10308 type = build_pointer_type (type);
10309
10310 /* Process a list of type modifier keywords (such as
10311 const or volatile) that were given inside the `*' or `&'. */
10312
10313 if (TREE_TYPE (declarator))
10314 {
10315 register tree typemodlist;
10316 int erred = 0;
91063b51
MM
10317
10318 constp = 0;
10319 volatilep = 0;
10320 restrictp = 0;
8d08fdba
MS
10321 for (typemodlist = TREE_TYPE (declarator); typemodlist;
10322 typemodlist = TREE_CHAIN (typemodlist))
10323 {
91063b51
MM
10324 tree qualifier = TREE_VALUE (typemodlist);
10325
10326 if (qualifier == ridpointers[(int) RID_CONST])
8d08fdba 10327 constp++;
91063b51 10328 else if (qualifier == ridpointers[(int) RID_VOLATILE])
8d08fdba 10329 volatilep++;
91063b51
MM
10330 else if (qualifier == ridpointers[(int) RID_RESTRICT])
10331 restrictp++;
8d08fdba
MS
10332 else if (!erred)
10333 {
10334 erred = 1;
91063b51 10335 error ("invalid type modifier within pointer declarator");
8d08fdba
MS
10336 }
10337 }
10338 if (constp > 1)
8251199e 10339 pedwarn ("duplicate `const'");
8d08fdba 10340 if (volatilep > 1)
8251199e 10341 pedwarn ("duplicate `volatile'");
91063b51
MM
10342 if (restrictp > 1)
10343 pedwarn ("duplicate `restrict'");
10344
10345 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
10346 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
10347 | (volatilep ? TYPE_QUAL_VOLATILE : 0));
a0a33927
MS
10348 if (TREE_CODE (declarator) == ADDR_EXPR
10349 && (constp || volatilep))
10350 {
10351 if (constp)
8251199e 10352 pedwarn ("discarding `const' applied to a reference");
a0a33927 10353 if (volatilep)
8251199e 10354 pedwarn ("discarding `volatile' applied to a reference");
91063b51 10355 type_quals &= ~(TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE);
a0a33927 10356 }
91063b51 10357 type = cp_build_qualified_type (type, type_quals);
8d08fdba
MS
10358 }
10359 declarator = TREE_OPERAND (declarator, 0);
10360 ctype = NULL_TREE;
10361 break;
10362
10363 case SCOPE_REF:
10364 {
10365 /* We have converted type names to NULL_TREE if the
10366 name was bogus, or to a _TYPE node, if not.
10367
10368 The variable CTYPE holds the type we will ultimately
10369 resolve to. The code here just needs to build
10370 up appropriate member types. */
10371 tree sname = TREE_OPERAND (declarator, 1);
386b8a85
JM
10372 tree t;
10373
8d08fdba
MS
10374 /* Destructors can have their visibilities changed as well. */
10375 if (TREE_CODE (sname) == BIT_NOT_EXPR)
10376 sname = TREE_OPERAND (sname, 0);
10377
10378 if (TREE_COMPLEXITY (declarator) == 0)
10379 /* This needs to be here, in case we are called
10380 multiple times. */ ;
9a68c51f
JM
10381 else if (TREE_COMPLEXITY (declarator) == -1)
10382 /* Namespace member. */
10383 pop_decl_namespace ();
8d08fdba 10384 else if (friendp && (TREE_COMPLEXITY (declarator) < 2))
2c73f9f5 10385 /* Don't fall out into global scope. Hides real bug? --eichin */ ;
5566b478
MS
10386 else if (! IS_AGGR_TYPE_CODE
10387 (TREE_CODE (TREE_OPERAND (declarator, 0))))
10388 ;
8d08fdba
MS
10389 else if (TREE_COMPLEXITY (declarator) == current_class_depth)
10390 {
9a3b49ac
MS
10391 /* Resolve any TYPENAME_TYPEs from the decl-specifier-seq
10392 that refer to ctype. They couldn't be resolved earlier
10393 because we hadn't pushed into the class yet.
10394 Example: resolve 'B<T>::type' in
10395 'B<typename B<T>::type> B<T>::f () { }'. */
10396 if (current_template_parms
10397 && uses_template_parms (type)
10398 && uses_template_parms (current_class_type))
10399 {
10400 tree args = current_template_args ();
f7d98d58 10401 type = tsubst (type, args, NULL_TREE);
9a3b49ac
MS
10402 }
10403
a28e3c7f
MS
10404 /* This pop_nested_class corresponds to the
10405 push_nested_class used to push into class scope for
10406 parsing the argument list of a function decl, in
10407 qualified_id. */
8d08fdba
MS
10408 pop_nested_class (1);
10409 TREE_COMPLEXITY (declarator) = current_class_depth;
10410 }
10411 else
10412 my_friendly_abort (16);
10413
10414 if (TREE_OPERAND (declarator, 0) == NULL_TREE)
10415 {
10416 /* We had a reference to a global decl, or
10417 perhaps we were given a non-aggregate typedef,
10418 in which case we cleared this out, and should just
10419 keep going as though it wasn't there. */
10420 declarator = sname;
10421 continue;
10422 }
10423 ctype = TREE_OPERAND (declarator, 0);
10424
386b8a85 10425 t = ctype;
7ddedda4 10426 while (t != NULL_TREE && CLASS_TYPE_P (t))
386b8a85 10427 {
75650646
MM
10428 if (CLASSTYPE_TEMPLATE_INFO (t) &&
10429 !CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
386b8a85
JM
10430 template_count += 1;
10431 t = TYPE_MAIN_DECL (t);
10432 if (DECL_LANG_SPECIFIC (t))
10433 t = DECL_CLASS_CONTEXT (t);
10434 else
10435 t = NULL_TREE;
10436 }
10437
8d08fdba
MS
10438 if (sname == NULL_TREE)
10439 goto done_scoping;
10440
10441 if (TREE_CODE (sname) == IDENTIFIER_NODE)
10442 {
10443 /* This is the `standard' use of the scoping operator:
10444 basetype :: member . */
10445
db5ae43f 10446 if (ctype == current_class_type)
28cbf42c
MS
10447 {
10448 /* class A {
10449 void A::f ();
10450 };
10451
10452 Is this ill-formed? */
10453
10454 if (pedantic)
8251199e 10455 cp_pedwarn ("extra qualification `%T::' on member `%s' ignored",
28cbf42c
MS
10456 ctype, name);
10457 }
db5ae43f 10458 else if (TREE_CODE (type) == FUNCTION_TYPE)
8d08fdba
MS
10459 {
10460 if (current_class_type == NULL_TREE
8d08fdba 10461 || friendp)
14ae7e7d
JM
10462 type = build_cplus_method_type (ctype, TREE_TYPE (type),
10463 TYPE_ARG_TYPES (type));
8d08fdba
MS
10464 else
10465 {
8251199e 10466 cp_error ("cannot declare member function `%T::%s' within `%T'",
a3203465 10467 ctype, name, current_class_type);
8d08fdba
MS
10468 return void_type_node;
10469 }
10470 }
5566b478
MS
10471 else if (RIDBIT_SETP (RID_TYPEDEF, specbits)
10472 || TYPE_SIZE (complete_type (ctype)) != NULL_TREE)
8d08fdba 10473 {
2c73f9f5 10474 /* Have to move this code elsewhere in this function.
db5ae43f
MS
10475 this code is used for i.e., typedef int A::M; M *pm;
10476
10477 It is? How? jason 10/2/94 */
8d08fdba 10478
8d08fdba
MS
10479 if (current_class_type)
10480 {
8251199e 10481 cp_error ("cannot declare member `%T::%s' within `%T'",
db5ae43f
MS
10482 ctype, name, current_class_type);
10483 return void_type_node;
8d08fdba
MS
10484 }
10485 type = build_offset_type (ctype, type);
10486 }
10487 else if (uses_template_parms (ctype))
10488 {
8d08fdba 10489 if (TREE_CODE (type) == FUNCTION_TYPE)
beb53fb8 10490 type
14ae7e7d 10491 = build_cplus_method_type (ctype, TREE_TYPE (type),
beb53fb8 10492 TYPE_ARG_TYPES (type));
8d08fdba
MS
10493 }
10494 else
10495 {
8251199e 10496 cp_error ("structure `%T' not yet defined", ctype);
8d08fdba
MS
10497 return error_mark_node;
10498 }
10499
10500 declarator = sname;
10501 }
8d08fdba
MS
10502 else if (TREE_CODE (sname) == SCOPE_REF)
10503 my_friendly_abort (17);
10504 else
10505 {
10506 done_scoping:
10507 declarator = TREE_OPERAND (declarator, 1);
10508 if (declarator && TREE_CODE (declarator) == CALL_EXPR)
10509 /* In this case, we will deal with it later. */
10510 ;
10511 else
10512 {
10513 if (TREE_CODE (type) == FUNCTION_TYPE)
14ae7e7d
JM
10514 type = build_cplus_method_type (ctype, TREE_TYPE (type),
10515 TYPE_ARG_TYPES (type));
8d08fdba
MS
10516 else
10517 type = build_offset_type (ctype, type);
10518 }
10519 }
10520 }
10521 break;
10522
10523 case BIT_NOT_EXPR:
10524 declarator = TREE_OPERAND (declarator, 0);
10525 break;
10526
8d08fdba
MS
10527 case RECORD_TYPE:
10528 case UNION_TYPE:
10529 case ENUMERAL_TYPE:
10530 declarator = NULL_TREE;
10531 break;
10532
10533 case ERROR_MARK:
10534 declarator = NULL_TREE;
10535 break;
10536
10537 default:
10538 my_friendly_abort (158);
10539 }
10540 }
10541
1eab9b56
JM
10542 /* See the comment for the TREE_LIST case, above. */
10543 if (inner_attrs)
10544 {
10545 if (! ignore_attrs)
10546 decl_attributes (type, inner_attrs, NULL_TREE);
10547 else if (attrlist)
10548 TREE_VALUE (attrlist) = chainon (inner_attrs, TREE_VALUE (attrlist));
10549 else
10550 attrlist = build_decl_list (NULL_TREE, inner_attrs);
10551 }
10552
db5ae43f
MS
10553 if (explicitp == 1)
10554 {
8251199e 10555 error ("only constructors can be declared `explicit'");
db5ae43f
MS
10556 explicitp = 0;
10557 }
10558
8d08fdba
MS
10559 /* Now TYPE has the actual type. */
10560
10561 /* If this is declaring a typedef name, return a TYPE_DECL. */
10562
f30432d7
MS
10563 if (RIDBIT_SETP (RID_MUTABLE, specbits))
10564 {
91063b51 10565 if (type_quals & TYPE_QUAL_CONST)
f30432d7 10566 {
8251199e 10567 error ("const `%s' cannot be declared `mutable'", name);
f30432d7
MS
10568 RIDBIT_RESET (RID_MUTABLE, specbits);
10569 }
10570 else if (staticp)
10571 {
8251199e 10572 error ("static `%s' cannot be declared `mutable'", name);
f30432d7
MS
10573 RIDBIT_RESET (RID_MUTABLE, specbits);
10574 }
10575 }
10576
efe38fab
JM
10577 if (declarator == NULL_TREE
10578 || TREE_CODE (declarator) == IDENTIFIER_NODE
10579 || (TREE_CODE (declarator) == TEMPLATE_ID_EXPR
10580 && (TREE_CODE (type) == FUNCTION_TYPE
10581 || TREE_CODE (type) == METHOD_TYPE)))
10582 /* OK */;
10583 else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
10584 {
10585 cp_error ("template-id `%D' used as a declarator", declarator);
10586 declarator = dname;
10587 }
10588 else
10589 my_friendly_abort (990210);
10590
fc378698 10591 if (RIDBIT_SETP (RID_TYPEDEF, specbits) && decl_context != TYPENAME)
8d08fdba
MS
10592 {
10593 tree decl;
10594
10595 /* Note that the grammar rejects storage classes
10596 in typenames, fields or parameters. */
eff71ab0
PB
10597 if (current_lang_name == lang_name_java)
10598 TYPE_FOR_JAVA (type) = 1;
8d08fdba 10599
d2e5ee5c
MS
10600 if (decl_context == FIELD)
10601 {
094fe153 10602 if (declarator == constructor_name (current_class_type))
8251199e 10603 cp_pedwarn ("ANSI C++ forbids nested type `%D' with same name as enclosing class",
d2e5ee5c
MS
10604 declarator);
10605 decl = build_lang_decl (TYPE_DECL, declarator, type);
10606 if (IS_SIGNATURE (current_class_type) && opaque_typedef)
10607 SIGNATURE_HAS_OPAQUE_TYPEDECLS (current_class_type) = 1;
10608 }
10609 else
7fb4a8f7
JM
10610 {
10611 /* Make sure this typedef lives as long as its type,
10612 since it might be used as a template parameter. */
1aed5355
MM
10613 if (type != error_mark_node)
10614 push_obstacks (TYPE_OBSTACK (type), TYPE_OBSTACK (type));
7fb4a8f7 10615 decl = build_decl (TYPE_DECL, declarator, type);
1aed5355
MM
10616 if (type != error_mark_node)
10617 pop_obstacks ();
7fb4a8f7 10618 }
d2e5ee5c 10619
8d08fdba
MS
10620 /* If the user declares "struct {...} foo" then `foo' will have
10621 an anonymous name. Fill that name in now. Nothing can
10622 refer to it, so nothing needs know about the name change.
10623 The TYPE_NAME field was filled in by build_struct_xref. */
10624 if (type != error_mark_node
10625 && TYPE_NAME (type)
10626 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
10627 && ANON_AGGRNAME_P (TYPE_IDENTIFIER (type)))
10628 {
61a127b3
MM
10629 /* FIXME: This is bogus; we should not be doing this for
10630 cv-qualified types. */
10631
0d2a8b1b
BK
10632 /* For anonymous structs that are cv-qualified, need to use
10633 TYPE_MAIN_VARIANT so that name will mangle correctly. As
10634 type not referenced after this block, don't bother
10635 resetting type to original type, ie. TREE_TYPE (decl). */
10636 type = TYPE_MAIN_VARIANT (type);
10637
2c73f9f5 10638 /* Replace the anonymous name with the real name everywhere. */
8d08fdba 10639 lookup_tag_reverse (type, declarator);
d2e5ee5c 10640 TYPE_NAME (type) = decl;
8d08fdba
MS
10641
10642 if (TYPE_LANG_SPECIFIC (type))
10643 TYPE_WAS_ANONYMOUS (type) = 1;
10644
33964bf4
MM
10645 /* If this is a typedef within a template class, the nested
10646 type is a (non-primary) template. The name for the
10647 template needs updating as well. */
10648 if (TYPE_LANG_SPECIFIC (type) && CLASSTYPE_TEMPLATE_INFO (type))
10649 DECL_NAME (CLASSTYPE_TI_TEMPLATE (type))
10650 = TYPE_IDENTIFIER (type);
10651
2c73f9f5
ML
10652 /* XXX Temporarily set the scope.
10653 When returning, start_decl expects it as NULL_TREE,
10654 and will then then set it using pushdecl. */
10655 my_friendly_assert (DECL_CONTEXT (decl) == NULL_TREE, 980404);
10656 if (current_class_type)
10657 DECL_CONTEXT (decl) = current_class_type;
10658 else
cb0dbb9a 10659 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
2c73f9f5 10660
d2e5ee5c
MS
10661 DECL_ASSEMBLER_NAME (decl) = DECL_NAME (decl);
10662 DECL_ASSEMBLER_NAME (decl)
10663 = get_identifier (build_overload_name (type, 1, 1));
2c73f9f5 10664 DECL_CONTEXT (decl) = NULL_TREE;
50a6dbd7
JM
10665
10666 /* FIXME remangle member functions; member functions of a
10667 type with external linkage have external linkage. */
fc378698 10668 }
fc378698 10669
8d08fdba
MS
10670 if (TREE_CODE (type) == OFFSET_TYPE || TREE_CODE (type) == METHOD_TYPE)
10671 {
8251199e 10672 cp_error_at ("typedef name may not be class-qualified", decl);
a3203465 10673 return NULL_TREE;
8d08fdba
MS
10674 }
10675 else if (quals)
10676 {
10677 if (ctype == NULL_TREE)
10678 {
10679 if (TREE_CODE (type) != METHOD_TYPE)
8251199e 10680 cp_error_at ("invalid type qualifier for non-method type", decl);
8d08fdba
MS
10681 else
10682 ctype = TYPE_METHOD_BASETYPE (type);
10683 }
10684 if (ctype != NULL_TREE)
10685 grok_method_quals (ctype, decl, quals);
10686 }
10687
10688 if (RIDBIT_SETP (RID_SIGNED, specbits)
10689 || (typedef_decl && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl)))
10690 C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
10691
10692 if (RIDBIT_SETP (RID_MUTABLE, specbits))
61a127b3 10693 error ("non-object member `%s' cannot be declared mutable", name);
8d08fdba 10694
c91a56d2
MS
10695 bad_specifiers (decl, "type", virtualp, quals != NULL_TREE,
10696 inlinep, friendp, raises != NULL_TREE);
10697
10698 if (initialized)
8251199e 10699 error ("typedef declaration includes an initializer");
c91a56d2 10700
8d08fdba
MS
10701 return decl;
10702 }
10703
10704 /* Detect the case of an array type of unspecified size
10705 which came, as such, direct from a typedef name.
10706 We must copy the type, so that each identifier gets
10707 a distinct type, so that each identifier's size can be
10708 controlled separately by its own initializer. */
10709
10710 if (type == typedef_type && TREE_CODE (type) == ARRAY_TYPE
10711 && TYPE_DOMAIN (type) == NULL_TREE)
10712 {
10713 type = build_cplus_array_type (TREE_TYPE (type), TYPE_DOMAIN (type));
10714 }
10715
10716 /* If this is a type name (such as, in a cast or sizeof),
10717 compute the type and return it now. */
10718
10719 if (decl_context == TYPENAME)
10720 {
10721 /* Note that the grammar rejects storage classes
10722 in typenames, fields or parameters. */
91063b51 10723 if (type_quals != TYPE_UNQUALIFIED)
a703fb38
KG
10724 {
10725 if (IS_SIGNATURE (type))
91063b51
MM
10726 error ("type qualifiers specified for signature type");
10727 type_quals = TYPE_UNQUALIFIED;
a703fb38 10728 }
8d08fdba
MS
10729
10730 /* Special case: "friend class foo" looks like a TYPENAME context. */
10731 if (friendp)
10732 {
91063b51 10733 if (type_quals != TYPE_UNQUALIFIED)
b7484fbe 10734 {
91063b51
MM
10735 cp_error ("type qualifiers specified for friend class declaration");
10736 type_quals = TYPE_UNQUALIFIED;
b7484fbe
MS
10737 }
10738 if (inlinep)
10739 {
8251199e 10740 cp_error ("`inline' specified for friend class declaration");
b7484fbe
MS
10741 inlinep = 0;
10742 }
10743
10744 /* Only try to do this stuff if we didn't already give up. */
10745 if (type != integer_type_node)
10746 {
10747 /* A friendly class? */
10748 if (current_class_type)
10749 make_friend_class (current_class_type, TYPE_MAIN_VARIANT (type));
10750 else
8251199e 10751 error ("trying to make class `%s' a friend of global scope",
b7484fbe
MS
10752 TYPE_NAME_STRING (type));
10753 type = void_type_node;
10754 }
8d08fdba
MS
10755 }
10756 else if (quals)
10757 {
8d08fdba 10758 tree dummy = build_decl (TYPE_DECL, declarator, type);
8d08fdba
MS
10759 if (ctype == NULL_TREE)
10760 {
10761 my_friendly_assert (TREE_CODE (type) == METHOD_TYPE, 159);
10762 ctype = TYPE_METHOD_BASETYPE (type);
10763 }
10764 grok_method_quals (ctype, dummy, quals);
10765 type = TREE_TYPE (dummy);
10766 }
10767
10768 return type;
10769 }
10770 else if (declarator == NULL_TREE && decl_context != PARM
db5ae43f 10771 && decl_context != CATCHPARM
8d08fdba
MS
10772 && TREE_CODE (type) != UNION_TYPE
10773 && ! bitfield)
10774 {
8251199e 10775 cp_error ("abstract declarator `%T' used as declaration", type);
8d08fdba
MS
10776 declarator = make_anon_name ();
10777 }
10778
10779 /* `void' at top level (not within pointer)
10780 is allowed only in typedefs or type names.
10781 We don't complain about parms either, but that is because
10782 a better error message can be made later. */
10783
a1774733 10784 if (TREE_CODE (type) == VOID_TYPE && decl_context != PARM)
8d08fdba 10785 {
b7484fbe 10786 if (! declarator)
8251199e 10787 error ("unnamed variable or field declared void");
b7484fbe 10788 else if (TREE_CODE (declarator) == IDENTIFIER_NODE)
8d08fdba
MS
10789 {
10790 if (IDENTIFIER_OPNAME_P (declarator))
8d08fdba 10791 my_friendly_abort (356);
8d08fdba 10792 else
8251199e 10793 error ("variable or field `%s' declared void", name);
8d08fdba
MS
10794 }
10795 else
8251199e 10796 error ("variable or field declared void");
8d08fdba
MS
10797 type = integer_type_node;
10798 }
10799
10800 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
10801 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
10802
14ae7e7d
JM
10803 if (decl_context == PARM || decl_context == CATCHPARM)
10804 {
10805 if (ctype || in_namespace)
10806 error ("cannot use `::' in parameter declaration");
10807
10808 /* A parameter declared as an array of T is really a pointer to T.
10809 One declared as a function is really a pointer to a function.
10810 One declared as a member is really a pointer to member. */
10811
10812 if (TREE_CODE (type) == ARRAY_TYPE)
10813 {
10814 /* Transfer const-ness of array into that of type pointed to. */
10815 type = build_pointer_type (TREE_TYPE (type));
91063b51 10816 type_quals = TYPE_UNQUALIFIED;
14ae7e7d
JM
10817 }
10818 else if (TREE_CODE (type) == FUNCTION_TYPE)
10819 type = build_pointer_type (type);
10820 else if (TREE_CODE (type) == OFFSET_TYPE)
10821 type = build_pointer_type (type);
10822 else if (TREE_CODE (type) == VOID_TYPE && declarator)
10823 {
10824 error ("declaration of `%s' as void", name);
10825 return NULL_TREE;
10826 }
10827 }
10828
8d08fdba
MS
10829 {
10830 register tree decl;
10831
10832 if (decl_context == PARM)
10833 {
ff350acd 10834 decl = build_decl (PARM_DECL, declarator, type);
8d08fdba
MS
10835
10836 bad_specifiers (decl, "parameter", virtualp, quals != NULL_TREE,
10837 inlinep, friendp, raises != NULL_TREE);
10838 if (current_class_type
10839 && IS_SIGNATURE (current_class_type))
10840 {
10841 if (inlinep)
8251199e 10842 error ("parameter of signature member function declared `inline'");
8d08fdba 10843 if (RIDBIT_SETP (RID_AUTO, specbits))
8251199e 10844 error ("parameter of signature member function declared `auto'");
8d08fdba 10845 if (RIDBIT_SETP (RID_REGISTER, specbits))
8251199e 10846 error ("parameter of signature member function declared `register'");
8d08fdba
MS
10847 }
10848
10849 /* Compute the type actually passed in the parmlist,
10850 for the case where there is no prototype.
10851 (For example, shorts and chars are passed as ints.)
10852 When there is a prototype, this is overridden later. */
10853
39211cd5 10854 DECL_ARG_TYPE (decl) = type_promotes_to (type);
8d08fdba
MS
10855 }
10856 else if (decl_context == FIELD)
10857 {
10858 if (type == error_mark_node)
10859 {
10860 /* Happens when declaring arrays of sizes which
10861 are error_mark_node, for example. */
10862 decl = NULL_TREE;
10863 }
997a088c 10864 else if (in_namespace && !friendp)
05008fb9
MM
10865 {
10866 /* Something like struct S { int N::j; }; */
8251199e 10867 cp_error ("invalid use of `::'");
05008fb9
MM
10868 decl = NULL_TREE;
10869 }
8d08fdba
MS
10870 else if (TREE_CODE (type) == FUNCTION_TYPE)
10871 {
10872 int publicp = 0;
e1467ff2 10873 tree function_context;
8d08fdba 10874
72b7eeff
MS
10875 /* We catch the others as conflicts with the builtin
10876 typedefs. */
10877 if (friendp && declarator == ridpointers[(int) RID_SIGNED])
10878 {
8251199e 10879 cp_error ("function `%D' cannot be declared friend",
72b7eeff
MS
10880 declarator);
10881 friendp = 0;
10882 }
10883
8d08fdba
MS
10884 if (friendp == 0)
10885 {
10886 if (ctype == NULL_TREE)
10887 ctype = current_class_type;
10888
10889 if (ctype == NULL_TREE)
10890 {
8251199e 10891 cp_error ("can't make `%D' into a method -- not in a class",
8d08fdba
MS
10892 declarator);
10893 return void_type_node;
10894 }
10895
10896 /* ``A union may [ ... ] not [ have ] virtual functions.''
10897 ARM 9.5 */
10898 if (virtualp && TREE_CODE (ctype) == UNION_TYPE)
10899 {
8251199e 10900 cp_error ("function `%D' declared virtual inside a union",
8d08fdba
MS
10901 declarator);
10902 return void_type_node;
10903 }
10904
10905 if (declarator == ansi_opname[(int) NEW_EXPR]
a28e3c7f
MS
10906 || declarator == ansi_opname[(int) VEC_NEW_EXPR]
10907 || declarator == ansi_opname[(int) DELETE_EXPR]
10908 || declarator == ansi_opname[(int) VEC_DELETE_EXPR])
8d08fdba
MS
10909 {
10910 if (virtualp)
10911 {
8251199e 10912 cp_error ("`%D' cannot be declared virtual, since it is always static",
8d08fdba
MS
10913 declarator);
10914 virtualp = 0;
10915 }
10916 }
10917 else if (staticp < 2)
14ae7e7d
JM
10918 type = build_cplus_method_type (ctype, TREE_TYPE (type),
10919 TYPE_ARG_TYPES (type));
8d08fdba
MS
10920 }
10921
10922 /* Tell grokfndecl if it needs to set TREE_PUBLIC on the node. */
e1467ff2
MM
10923 function_context = (ctype != NULL_TREE) ?
10924 hack_decl_function_context (TYPE_MAIN_DECL (ctype)) : NULL_TREE;
10925 publicp = (! friendp || ! staticp)
10926 && function_context == NULL_TREE;
386b8a85
JM
10927 decl = grokfndecl (ctype, type,
10928 TREE_CODE (declarator) != TEMPLATE_ID_EXPR
10929 ? declarator : dname,
10930 declarator,
f30432d7 10931 virtualp, flags, quals, raises, attrlist,
386b8a85 10932 friendp ? -1 : 0, friendp, publicp, inlinep,
2c73f9f5 10933 funcdef_flag, template_count, in_namespace);
3ddfb0e6
MM
10934 if (decl == NULL_TREE || decl == error_mark_node)
10935 return decl;
6125f3be
DE
10936#if 0
10937 /* This clobbers the attrs stored in `decl' from `attrlist'. */
10938 /* The decl and setting of decl_machine_attr is also turned off. */
71851aaa 10939 decl = build_decl_attribute_variant (decl, decl_machine_attr);
6125f3be 10940#endif
f0e01782 10941
cc804e51
MM
10942 /* [class.conv.ctor]
10943
10944 A constructor declared without the function-specifier
10945 explicit that can be called with a single parameter
10946 specifies a conversion from the type of its first
10947 parameter to the type of its class. Such a constructor
10948 is called a converting constructor. */
db5ae43f
MS
10949 if (explicitp == 2)
10950 DECL_NONCONVERTING_P (decl) = 1;
cc804e51
MM
10951 else if (DECL_CONSTRUCTOR_P (decl))
10952 {
10953 /* The constructor can be called with exactly one
10954 parameter if there is at least one parameter, and
10955 any subsequent parameters have default arguments.
10956 We don't look at the first parameter, which is
10957 really just the `this' parameter for the new
10958 object. */
10959 tree arg_types =
10960 TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)));
10961
10962 /* Skip the `in_chrg' argument too, if present. */
10963 if (TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (decl)))
10964 arg_types = TREE_CHAIN (arg_types);
10965
10966 if (arg_types == void_list_node
10967 || (arg_types
10968 && TREE_CHAIN (arg_types)
10969 && TREE_CHAIN (arg_types) != void_list_node
10970 && !TREE_PURPOSE (TREE_CHAIN (arg_types))))
10971 DECL_NONCONVERTING_P (decl) = 1;
10972 }
8d08fdba
MS
10973 }
10974 else if (TREE_CODE (type) == METHOD_TYPE)
10975 {
faae18ab
MS
10976 /* We only get here for friend declarations of
10977 members of other classes. */
8d08fdba
MS
10978 /* All method decls are public, so tell grokfndecl to set
10979 TREE_PUBLIC, also. */
386b8a85 10980 decl = grokfndecl (ctype, type, declarator, declarator,
f30432d7 10981 virtualp, flags, quals, raises, attrlist,
386b8a85 10982 friendp ? -1 : 0, friendp, 1, 0, funcdef_flag,
2c73f9f5 10983 template_count, in_namespace);
f0e01782
MS
10984 if (decl == NULL_TREE)
10985 return NULL_TREE;
8d08fdba 10986 }
5566b478
MS
10987 else if (!staticp && ! processing_template_decl
10988 && TYPE_SIZE (complete_type (type)) == NULL_TREE
8d08fdba
MS
10989 && (TREE_CODE (type) != ARRAY_TYPE || initialized == 0))
10990 {
b7484fbe 10991 if (declarator)
8251199e 10992 cp_error ("field `%D' has incomplete type", declarator);
b7484fbe 10993 else
8251199e 10994 cp_error ("name `%T' has incomplete type", type);
8d08fdba
MS
10995
10996 /* If we're instantiating a template, tell them which
10997 instantiation made the field's type be incomplete. */
10998 if (current_class_type
10999 && TYPE_NAME (current_class_type)
d2e5ee5c 11000 && IDENTIFIER_TEMPLATE (TYPE_IDENTIFIER (current_class_type))
8d08fdba
MS
11001 && declspecs && TREE_VALUE (declspecs)
11002 && TREE_TYPE (TREE_VALUE (declspecs)) == type)
8251199e 11003 cp_error (" in instantiation of template `%T'",
db5ae43f
MS
11004 current_class_type);
11005
8d08fdba
MS
11006 type = error_mark_node;
11007 decl = NULL_TREE;
11008 }
11009 else
11010 {
11011 if (friendp)
11012 {
8251199e 11013 error ("`%s' is neither function nor method; cannot be declared friend",
8d08fdba
MS
11014 IDENTIFIER_POINTER (declarator));
11015 friendp = 0;
11016 }
11017 decl = NULL_TREE;
11018 }
11019
11020 if (friendp)
11021 {
11022 /* Friends are treated specially. */
11023 if (ctype == current_class_type)
8251199e 11024 warning ("member functions are implicitly friends of their class");
8d08fdba
MS
11025 else
11026 {
11027 tree t = NULL_TREE;
11028 if (decl && DECL_NAME (decl))
36a117a5
MM
11029 {
11030 if (template_class_depth (current_class_type) == 0)
6c30752f
MM
11031 {
11032 decl
11033 = check_explicit_specialization
11034 (declarator, decl,
11035 template_count, 2 * (funcdef_flag != 0) + 4);
11036 if (decl == error_mark_node)
11037 return error_mark_node;
11038 }
11039
36a117a5
MM
11040 t = do_friend (ctype, declarator, decl,
11041 last_function_parms, flags, quals,
11042 funcdef_flag);
11043 }
8d08fdba
MS
11044 if (t && funcdef_flag)
11045 return t;
11046
11047 return void_type_node;
11048 }
11049 }
11050
11051 /* Structure field. It may not be a function, except for C++ */
11052
11053 if (decl == NULL_TREE)
11054 {
8d08fdba
MS
11055 if (initialized)
11056 {
3ac3d9ea
MM
11057 if (!staticp)
11058 {
11059 /* An attempt is being made to initialize a non-static
11060 member. But, from [class.mem]:
11061
11062 4 A member-declarator can contain a
11063 constant-initializer only if it declares a static
11064 member (_class.static_) of integral or enumeration
11065 type, see _class.static.data_.
11066
11067 This used to be relatively common practice, but
11068 the rest of the compiler does not correctly
11069 handle the initialization unless the member is
11070 static so we make it static below. */
950ad3c3 11071 cp_pedwarn ("ANSI C++ forbids initialization of member `%D'",
3ac3d9ea 11072 declarator);
8251199e 11073 cp_pedwarn ("making `%D' static", declarator);
3ac3d9ea
MM
11074 staticp = 1;
11075 }
11076
6ba89f8e
MM
11077 if (uses_template_parms (type))
11078 /* We'll check at instantiation time. */
11079 ;
11080 else if (check_static_variable_definition (declarator,
11081 type))
11082 /* If we just return the declaration, crashes
11083 will sometimes occur. We therefore return
72a93143
JM
11084 void_type_node, as if this was a friend
11085 declaration, to cause callers to completely
11086 ignore this declaration. */
6ba89f8e 11087 return void_type_node;
8d08fdba
MS
11088 }
11089
8ebeee52 11090 /* 9.2p13 [class.mem] */
4d7614e9
JM
11091 if (declarator == constructor_name (current_class_type)
11092 /* Divergence from the standard: In extern "C", we
11093 allow non-static data members here, because C does
11094 and /usr/include/netinet/in.h uses that. */
1f901793 11095 && (staticp || ! in_system_header))
8ebeee52
JM
11096 cp_pedwarn ("ANSI C++ forbids data member `%D' with same name as enclosing class",
11097 declarator);
11098
3ac3d9ea 11099 if (staticp)
8d08fdba
MS
11100 {
11101 /* C++ allows static class members.
11102 All other work for this is done by grokfield.
d7ceab7b 11103 This VAR_DCL is built by build_lang_field_decl.
8d08fdba
MS
11104 All other VAR_DECLs are built by build_decl. */
11105 decl = build_lang_field_decl (VAR_DECL, declarator, type);
5b605f68
MS
11106 TREE_STATIC (decl) = 1;
11107 /* In class context, 'static' means public access. */
3ac3d9ea 11108 TREE_PUBLIC (decl) = DECL_EXTERNAL (decl) = 1;
8d08fdba
MS
11109 }
11110 else
11111 {
11112 decl = build_lang_field_decl (FIELD_DECL, declarator, type);
11113 if (RIDBIT_SETP (RID_MUTABLE, specbits))
11114 {
11115 DECL_MUTABLE_P (decl) = 1;
11116 RIDBIT_RESET (RID_MUTABLE, specbits);
11117 }
11118 }
11119
11120 bad_specifiers (decl, "field", virtualp, quals != NULL_TREE,
11121 inlinep, friendp, raises != NULL_TREE);
11122 }
11123 }
11124 else if (TREE_CODE (type) == FUNCTION_TYPE || TREE_CODE (type) == METHOD_TYPE)
11125 {
386b8a85 11126 tree original_name;
8d08fdba
MS
11127 int publicp = 0;
11128
11129 if (! declarator)
11130 return NULL_TREE;
11131
386b8a85
JM
11132 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
11133 original_name = dname;
11134 else
11135 original_name = declarator;
11136
8926095f 11137 if (RIDBIT_SETP (RID_AUTO, specbits))
8251199e 11138 error ("storage class `auto' invalid for function `%s'", name);
8926095f 11139 else if (RIDBIT_SETP (RID_REGISTER, specbits))
8251199e 11140 error ("storage class `register' invalid for function `%s'", name);
8d08fdba
MS
11141
11142 /* Function declaration not at top level.
11143 Storage classes other than `extern' are not allowed
11144 and `extern' makes no difference. */
a9aedbc2 11145 if (! toplevel_bindings_p ()
8926095f
MS
11146 && (RIDBIT_SETP (RID_STATIC, specbits)
11147 || RIDBIT_SETP (RID_INLINE, specbits))
8d08fdba 11148 && pedantic)
8926095f
MS
11149 {
11150 if (RIDBIT_SETP (RID_STATIC, specbits))
8251199e 11151 pedwarn ("storage class `static' invalid for function `%s' declared out of global scope", name);
8926095f 11152 else
8251199e 11153 pedwarn ("storage class `inline' invalid for function `%s' declared out of global scope", name);
8926095f
MS
11154 }
11155
8d08fdba
MS
11156 if (ctype == NULL_TREE)
11157 {
11158 if (virtualp)
11159 {
8251199e 11160 error ("virtual non-class function `%s'", name);
8d08fdba
MS
11161 virtualp = 0;
11162 }
8d08fdba
MS
11163 }
11164 else if (TREE_CODE (type) == FUNCTION_TYPE && staticp < 2)
14ae7e7d
JM
11165 type = build_cplus_method_type (ctype, TREE_TYPE (type),
11166 TYPE_ARG_TYPES (type));
8d08fdba 11167
eb66be0e 11168 /* Record presence of `static'. */
faae18ab 11169 publicp = (ctype != NULL_TREE
a9aedbc2 11170 || RIDBIT_SETP (RID_EXTERN, specbits)
eb66be0e 11171 || !RIDBIT_SETP (RID_STATIC, specbits));
8d08fdba 11172
386b8a85 11173 decl = grokfndecl (ctype, type, original_name, declarator,
f30432d7 11174 virtualp, flags, quals, raises, attrlist,
75650646 11175 1, friendp,
386b8a85 11176 publicp, inlinep, funcdef_flag,
2c73f9f5 11177 template_count, in_namespace);
f0e01782
MS
11178 if (decl == NULL_TREE)
11179 return NULL_TREE;
8d08fdba 11180
f2e48b67
BK
11181 /* Among other times, could occur from check_explicit_specialization
11182 returning an error_mark_node. */
11183 if (decl == error_mark_node)
11184 return error_mark_node;
11185
8d08fdba
MS
11186 if (staticp == 1)
11187 {
11188 int illegal_static = 0;
11189
11190 /* Don't allow a static member function in a class, and forbid
11191 declaring main to be static. */
11192 if (TREE_CODE (type) == METHOD_TYPE)
11193 {
8251199e 11194 cp_pedwarn ("cannot declare member function `%D' to have static linkage", decl);
8d08fdba
MS
11195 illegal_static = 1;
11196 }
8d08fdba
MS
11197 else if (current_function_decl)
11198 {
11199 /* FIXME need arm citation */
8251199e 11200 error ("cannot declare static function inside another function");
8d08fdba
MS
11201 illegal_static = 1;
11202 }
11203
11204 if (illegal_static)
11205 {
11206 staticp = 0;
11207 RIDBIT_RESET (RID_STATIC, specbits);
11208 }
11209 }
8d08fdba
MS
11210 }
11211 else
11212 {
11213 /* It's a variable. */
11214
11215 /* An uninitialized decl with `extern' is a reference. */
2c73f9f5 11216 decl = grokvardecl (type, declarator, &specbits,
950ad3c3
MM
11217 initialized,
11218 (type_quals & TYPE_QUAL_CONST) != 0,
11219 in_namespace);
8d08fdba
MS
11220 bad_specifiers (decl, "variable", virtualp, quals != NULL_TREE,
11221 inlinep, friendp, raises != NULL_TREE);
11222
11223 if (ctype)
11224 {
f0e01782 11225 DECL_CONTEXT (decl) = ctype;
8d08fdba
MS
11226 if (staticp == 1)
11227 {
8251199e 11228 cp_pedwarn ("static member `%D' re-declared as static", decl);
8d08fdba
MS
11229 staticp = 0;
11230 RIDBIT_RESET (RID_STATIC, specbits);
11231 }
b7484fbe
MS
11232 if (RIDBIT_SETP (RID_REGISTER, specbits) && TREE_STATIC (decl))
11233 {
8251199e 11234 cp_error ("static member `%D' declared `register'", decl);
b7484fbe
MS
11235 RIDBIT_RESET (RID_REGISTER, specbits);
11236 }
f30432d7 11237 if (RIDBIT_SETP (RID_EXTERN, specbits) && pedantic)
8d08fdba 11238 {
8251199e 11239 cp_pedwarn ("cannot explicitly declare member `%#D' to have extern linkage",
f30432d7 11240 decl);
8d08fdba
MS
11241 RIDBIT_RESET (RID_EXTERN, specbits);
11242 }
11243 }
11244 }
11245
11246 if (RIDBIT_SETP (RID_MUTABLE, specbits))
11247 {
8251199e 11248 error ("`%s' cannot be declared mutable", name);
8d08fdba
MS
11249 }
11250
11251 /* Record `register' declaration for warnings on &
11252 and in case doing stupid register allocation. */
11253
11254 if (RIDBIT_SETP (RID_REGISTER, specbits))
11255 DECL_REGISTER (decl) = 1;
11256
8926095f
MS
11257 if (RIDBIT_SETP (RID_EXTERN, specbits))
11258 DECL_THIS_EXTERN (decl) = 1;
11259
faae18ab
MS
11260 if (RIDBIT_SETP (RID_STATIC, specbits))
11261 DECL_THIS_STATIC (decl) = 1;
11262
8d08fdba 11263 /* Record constancy and volatility. */
91063b51
MM
11264 /* FIXME: Disallow `restrict' pointer-to-member declarations. */
11265 c_apply_type_quals_to_decl (type_quals, decl);
8d08fdba
MS
11266
11267 return decl;
11268 }
11269}
11270\f
11271/* Tell if a parmlist/exprlist looks like an exprlist or a parmlist.
11272 An empty exprlist is a parmlist. An exprlist which
11273 contains only identifiers at the global level
11274 is a parmlist. Otherwise, it is an exprlist. */
e92cc029 11275
8d08fdba
MS
11276int
11277parmlist_is_exprlist (exprs)
11278 tree exprs;
11279{
11280 if (exprs == NULL_TREE || TREE_PARMLIST (exprs))
11281 return 0;
11282
a9aedbc2 11283 if (toplevel_bindings_p ())
8d08fdba
MS
11284 {
11285 /* At the global level, if these are all identifiers,
11286 then it is a parmlist. */
11287 while (exprs)
11288 {
11289 if (TREE_CODE (TREE_VALUE (exprs)) != IDENTIFIER_NODE)
11290 return 1;
11291 exprs = TREE_CHAIN (exprs);
11292 }
11293 return 0;
11294 }
11295 return 1;
11296}
11297
f181d4ae
MM
11298/* Subroutine of start_function. Ensure that each of the parameter
11299 types (as listed in PARMS) is complete, as is required for a
11300 function definition. */
e92cc029 11301
8d08fdba
MS
11302static void
11303require_complete_types_for_parms (parms)
11304 tree parms;
11305{
11306 while (parms)
11307 {
11308 tree type = TREE_TYPE (parms);
5566b478 11309 if (TYPE_SIZE (complete_type (type)) == NULL_TREE)
8d08fdba
MS
11310 {
11311 if (DECL_NAME (parms))
8251199e 11312 error ("parameter `%s' has incomplete type",
8d08fdba
MS
11313 IDENTIFIER_POINTER (DECL_NAME (parms)));
11314 else
8251199e 11315 error ("parameter has incomplete type");
8d08fdba
MS
11316 TREE_TYPE (parms) = error_mark_node;
11317 }
42f989ff
JM
11318 else
11319 layout_decl (parms, 0);
f181d4ae 11320
8d08fdba
MS
11321 parms = TREE_CHAIN (parms);
11322 }
11323}
11324
297e73d8
MM
11325/* Returns DECL if DECL is a local variable (or parameter). Returns
11326 NULL_TREE otherwise. */
11327
11328static tree
11329local_variable_p (t)
11330 tree t;
11331{
11332 if ((TREE_CODE (t) == VAR_DECL
11333 /* A VAR_DECL with a context that is a _TYPE is a static data
11334 member. */
11335 && !TYPE_P (CP_DECL_CONTEXT (t))
11336 /* Any other non-local variable must be at namespace scope. */
11337 && TREE_CODE (CP_DECL_CONTEXT (t)) != NAMESPACE_DECL)
11338 || (TREE_CODE (t) == PARM_DECL))
11339 return t;
11340
11341 return NULL_TREE;
11342}
11343
11344/* Check that ARG, which is a default-argument expression for a
11345 parameter DECL, is legal. Returns ARG, or ERROR_MARK_NODE, if
11346 something goes wrong. DECL may also be a _TYPE node, rather than a
11347 DECL, if there is no DECL available. */
11348
11349tree
11350check_default_argument (decl, arg)
11351 tree decl;
11352 tree arg;
11353{
11354 tree var;
11355 tree decl_type;
11356
11357 if (TREE_CODE (arg) == DEFAULT_ARG)
11358 /* We get a DEFAULT_ARG when looking at an in-class declaration
11359 with a default argument. Ignore the argument for now; we'll
11360 deal with it after the class is complete. */
11361 return arg;
11362
11363 if (processing_template_decl || uses_template_parms (arg))
11364 /* We don't do anything checking until instantiation-time. Note
11365 that there may be uninstantiated arguments even for an
11366 instantiated function, since default arguments are not
11367 instantiated until they are needed. */
11368 return arg;
11369
11370 if (TYPE_P (decl))
11371 {
11372 decl_type = decl;
11373 decl = NULL_TREE;
11374 }
11375 else
11376 decl_type = TREE_TYPE (decl);
11377
11378 if (arg == error_mark_node
11379 || decl == error_mark_node
11380 || TREE_TYPE (arg) == error_mark_node
11381 || decl_type == error_mark_node)
11382 /* Something already went wrong. There's no need to check
11383 further. */
11384 return error_mark_node;
11385
11386 /* [dcl.fct.default]
11387
11388 A default argument expression is implicitly converted to the
11389 parameter type. */
11390 if (!TREE_TYPE (arg)
11391 || !can_convert_arg (decl_type, TREE_TYPE (arg), arg))
11392 {
11393 if (decl)
11394 cp_error ("default argument for `%#D' has type `%T'",
11395 decl, TREE_TYPE (arg));
11396 else
11397 cp_error ("default argument for paramter of type `%T' has type `%T'",
11398 decl_type, TREE_TYPE (arg));
11399
11400 return error_mark_node;
11401 }
11402
11403 /* [dcl.fct.default]
11404
11405 Local variables shall not be used in default argument
11406 expressions.
11407
11408 The keyword `this' shall not be used in a default argument of a
11409 member function. */
11410 var = search_tree (arg, local_variable_p);
11411 if (var)
11412 {
11413 cp_error ("default argument `%E' uses local variable `%D'",
11414 arg, var);
11415 return error_mark_node;
11416 }
11417
11418 /* All is well. */
11419 return arg;
11420}
11421
8d08fdba
MS
11422/* Decode the list of parameter types for a function type.
11423 Given the list of things declared inside the parens,
11424 return a list of types.
11425
11426 The list we receive can have three kinds of elements:
11427 an IDENTIFIER_NODE for names given without types,
11428 a TREE_LIST node for arguments given as typespecs or names with typespecs,
11429 or void_type_node, to mark the end of an argument list
11430 when additional arguments are not permitted (... was not used).
11431
11432 FUNCDEF_FLAG is nonzero for a function definition, 0 for
11433 a mere declaration. A nonempty identifier-list gets an error message
11434 when FUNCDEF_FLAG is zero.
11435 If FUNCDEF_FLAG is 1, then parameter types must be complete.
11436 If FUNCDEF_FLAG is -1, then parameter types may be incomplete.
11437
11438 If all elements of the input list contain types,
11439 we return a list of the types.
11440 If all elements contain no type (except perhaps a void_type_node
11441 at the end), we return a null list.
11442 If some have types and some do not, it is an error, and we
11443 return a null list.
11444
11445 Also set last_function_parms to either
11446 a list of names (IDENTIFIER_NODEs) or a chain of PARM_DECLs.
11447 A list of names is converted to a chain of PARM_DECLs
11448 by store_parm_decls so that ultimately it is always a chain of decls.
11449
11450 Note that in C++, parameters can take default values. These default
11451 values are in the TREE_PURPOSE field of the TREE_LIST. It is
11452 an error to specify default values which are followed by parameters
11453 that have no default values, or an ELLIPSES. For simplicities sake,
11454 only parameters which are specified with their types can take on
11455 default values. */
11456
11457static tree
11458grokparms (first_parm, funcdef_flag)
11459 tree first_parm;
11460 int funcdef_flag;
11461{
11462 tree result = NULL_TREE;
11463 tree decls = NULL_TREE;
11464
11465 if (first_parm != NULL_TREE
11466 && TREE_CODE (TREE_VALUE (first_parm)) == IDENTIFIER_NODE)
11467 {
11468 if (! funcdef_flag)
8251199e 11469 pedwarn ("parameter names (without types) in function declaration");
8d08fdba
MS
11470 last_function_parms = first_parm;
11471 return NULL_TREE;
11472 }
11473 else if (first_parm != NULL_TREE
11474 && TREE_CODE (TREE_VALUE (first_parm)) != TREE_LIST
a1774733 11475 && TREE_CODE (TREE_VALUE (first_parm)) != VOID_TYPE)
8d08fdba
MS
11476 my_friendly_abort (145);
11477 else
11478 {
11479 /* Types were specified. This is a list of declarators
11480 each represented as a TREE_LIST node. */
11481 register tree parm, chain;
5566b478 11482 int any_init = 0, any_error = 0;
8d08fdba
MS
11483
11484 if (first_parm != NULL_TREE)
11485 {
11486 tree last_result = NULL_TREE;
11487 tree last_decl = NULL_TREE;
11488
11489 for (parm = first_parm; parm != NULL_TREE; parm = chain)
11490 {
a703fb38 11491 tree type = NULL_TREE, list_node = parm;
8d08fdba
MS
11492 register tree decl = TREE_VALUE (parm);
11493 tree init = TREE_PURPOSE (parm);
11494
11495 chain = TREE_CHAIN (parm);
11496 /* @@ weak defense against parse errors. */
a1774733
BK
11497 if (TREE_CODE (decl) != VOID_TYPE
11498 && TREE_CODE (decl) != TREE_LIST)
8d08fdba
MS
11499 {
11500 /* Give various messages as the need arises. */
11501 if (TREE_CODE (decl) == STRING_CST)
8251199e 11502 cp_error ("invalid string constant `%E'", decl);
8d08fdba 11503 else if (TREE_CODE (decl) == INTEGER_CST)
8251199e 11504 error ("invalid integer constant in parameter list, did you forget to give parameter name?");
8d08fdba
MS
11505 continue;
11506 }
11507
a1774733 11508 if (TREE_CODE (decl) != VOID_TYPE)
8d08fdba 11509 {
8d08fdba
MS
11510 decl = grokdeclarator (TREE_VALUE (decl),
11511 TREE_PURPOSE (decl),
f30432d7 11512 PARM, init != NULL_TREE,
c11b6f21 11513 NULL_TREE);
3bfdc719 11514 if (! decl || TREE_TYPE (decl) == error_mark_node)
8d08fdba 11515 continue;
01240200
MM
11516
11517 /* Top-level qualifiers on the parameters are
11518 ignored for function types. */
11519 type = TYPE_MAIN_VARIANT (TREE_TYPE (decl));
11520
a1774733 11521 if (TREE_CODE (type) == VOID_TYPE)
8d08fdba
MS
11522 decl = void_type_node;
11523 else if (TREE_CODE (type) == METHOD_TYPE)
11524 {
11525 if (DECL_NAME (decl))
fc378698 11526 /* Cannot use the decl here because
8d08fdba 11527 we don't have DECL_CONTEXT set up yet. */
8251199e 11528 cp_error ("parameter `%D' invalidly declared method type",
fc378698 11529 DECL_NAME (decl));
8d08fdba 11530 else
8251199e 11531 error ("parameter invalidly declared method type");
8d08fdba
MS
11532 type = build_pointer_type (type);
11533 TREE_TYPE (decl) = type;
11534 }
11535 else if (TREE_CODE (type) == OFFSET_TYPE)
11536 {
11537 if (DECL_NAME (decl))
8251199e 11538 cp_error ("parameter `%D' invalidly declared offset type",
fc378698 11539 DECL_NAME (decl));
8d08fdba 11540 else
8251199e 11541 error ("parameter invalidly declared offset type");
8d08fdba
MS
11542 type = build_pointer_type (type);
11543 TREE_TYPE (decl) = type;
11544 }
11545 else if (TREE_CODE (type) == RECORD_TYPE
11546 && TYPE_LANG_SPECIFIC (type)
11547 && CLASSTYPE_ABSTRACT_VIRTUALS (type))
11548 {
11549 abstract_virtuals_error (decl, type);
2c73f9f5 11550 any_error = 1; /* Seems like a good idea. */
8d08fdba
MS
11551 }
11552 else if (TREE_CODE (type) == RECORD_TYPE
11553 && TYPE_LANG_SPECIFIC (type)
11554 && IS_SIGNATURE (type))
11555 {
11556 signature_error (decl, type);
2c73f9f5 11557 any_error = 1; /* Seems like a good idea. */
8d08fdba 11558 }
482b883f
JM
11559 else if (POINTER_TYPE_P (type))
11560 {
11561 tree t = type;
11562 while (POINTER_TYPE_P (t)
11563 || (TREE_CODE (t) == ARRAY_TYPE
11564 && TYPE_DOMAIN (t) != NULL_TREE))
11565 t = TREE_TYPE (t);
11566 if (TREE_CODE (t) == ARRAY_TYPE)
57af8358
MM
11567 cp_error ("parameter type `%T' includes %s to array of unknown bound",
11568 type,
11569 TYPE_PTR_P (type) ? "pointer" : "reference");
482b883f 11570 }
8d08fdba
MS
11571 }
11572
a1774733 11573 if (TREE_CODE (decl) == VOID_TYPE)
8d08fdba
MS
11574 {
11575 if (result == NULL_TREE)
11576 {
11577 result = void_list_node;
11578 last_result = result;
11579 }
11580 else
11581 {
11582 TREE_CHAIN (last_result) = void_list_node;
11583 last_result = void_list_node;
11584 }
8d08fdba
MS
11585 if (chain
11586 && (chain != void_list_node || TREE_CHAIN (chain)))
8251199e 11587 error ("`void' in parameter list must be entire list");
8d08fdba
MS
11588 break;
11589 }
11590
11591 /* Since there is a prototype, args are passed in their own types. */
11592 DECL_ARG_TYPE (decl) = TREE_TYPE (decl);
11593#ifdef PROMOTE_PROTOTYPES
11594 if ((TREE_CODE (type) == INTEGER_TYPE
11595 || TREE_CODE (type) == ENUMERAL_TYPE)
11596 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
11597 DECL_ARG_TYPE (decl) = integer_type_node;
11598#endif
297e73d8 11599 if (!any_error && init)
8d08fdba 11600 {
297e73d8
MM
11601 any_init++;
11602 init = check_default_argument (decl, init);
8d08fdba
MS
11603 }
11604 else
11605 init = NULL_TREE;
11606
11607 if (decls == NULL_TREE)
11608 {
11609 decls = decl;
11610 last_decl = decls;
11611 }
11612 else
11613 {
11614 TREE_CHAIN (last_decl) = decl;
11615 last_decl = decl;
11616 }
66f2e88d 11617 if (! current_function_decl && TREE_PERMANENT (list_node))
8d08fdba
MS
11618 {
11619 TREE_PURPOSE (list_node) = init;
11620 TREE_VALUE (list_node) = type;
11621 TREE_CHAIN (list_node) = NULL_TREE;
11622 }
11623 else
11624 list_node = saveable_tree_cons (init, type, NULL_TREE);
11625 if (result == NULL_TREE)
11626 {
11627 result = list_node;
11628 last_result = result;
11629 }
11630 else
11631 {
11632 TREE_CHAIN (last_result) = list_node;
11633 last_result = list_node;
11634 }
11635 }
11636 if (last_result)
11637 TREE_CHAIN (last_result) = NULL_TREE;
11638 /* If there are no parameters, and the function does not end
11639 with `...', then last_decl will be NULL_TREE. */
11640 if (last_decl != NULL_TREE)
11641 TREE_CHAIN (last_decl) = NULL_TREE;
11642 }
11643 }
11644
11645 last_function_parms = decls;
11646
8d08fdba
MS
11647 return result;
11648}
42976354
BK
11649
11650/* Called from the parser to update an element of TYPE_ARG_TYPES for some
11651 FUNCTION_TYPE with the newly parsed version of its default argument, which
11652 was previously digested as text. See snarf_defarg et al in lex.c. */
11653
11654void
11655replace_defarg (arg, init)
11656 tree arg, init;
11657{
aa5f3bad 11658 if (! processing_template_decl
42976354 11659 && ! can_convert_arg (TREE_VALUE (arg), TREE_TYPE (init), init))
8251199e 11660 cp_pedwarn ("invalid type `%T' for default argument to `%T'",
42976354
BK
11661 TREE_TYPE (init), TREE_VALUE (arg));
11662 TREE_PURPOSE (arg) = init;
11663}
8d08fdba 11664\f
c11b6f21
MS
11665int
11666copy_args_p (d)
11667 tree d;
11668{
11669 tree t = FUNCTION_ARG_CHAIN (d);
67437d5b
JM
11670 if (DECL_CONSTRUCTOR_P (d)
11671 && TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (d)))
11672 t = TREE_CHAIN (t);
c11b6f21
MS
11673 if (t && TREE_CODE (TREE_VALUE (t)) == REFERENCE_TYPE
11674 && (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_VALUE (t)))
11675 == DECL_CLASS_CONTEXT (d))
11676 && (TREE_CHAIN (t) == NULL_TREE
11677 || TREE_CHAIN (t) == void_list_node
11678 || TREE_PURPOSE (TREE_CHAIN (t))))
11679 return 1;
11680 return 0;
11681}
11682
8d08fdba
MS
11683/* These memoizing functions keep track of special properties which
11684 a class may have. `grok_ctor_properties' notices whether a class
11685 has a constructor of the form X(X&), and also complains
11686 if the class has a constructor of the form X(X).
11687 `grok_op_properties' takes notice of the various forms of
11688 operator= which are defined, as well as what sorts of type conversion
11689 may apply. Both functions take a FUNCTION_DECL as an argument. */
e92cc029 11690
a0a33927 11691int
8d08fdba
MS
11692grok_ctor_properties (ctype, decl)
11693 tree ctype, decl;
11694{
11695 tree parmtypes = FUNCTION_ARG_CHAIN (decl);
11696 tree parmtype = parmtypes ? TREE_VALUE (parmtypes) : void_type_node;
11697
11698 /* When a type has virtual baseclasses, a magical first int argument is
11699 added to any ctor so we can tell if the class has been initialized
11700 yet. This could screw things up in this function, so we deliberately
11701 ignore the leading int if we're in that situation. */
711734a9 11702 if (TYPE_USES_VIRTUAL_BASECLASSES (ctype))
8d08fdba 11703 {
711734a9
JM
11704 my_friendly_assert (parmtypes
11705 && TREE_VALUE (parmtypes) == integer_type_node,
11706 980529);
8d08fdba
MS
11707 parmtypes = TREE_CHAIN (parmtypes);
11708 parmtype = TREE_VALUE (parmtypes);
11709 }
11710
f181d4ae
MM
11711 /* [class.copy]
11712
11713 A non-template constructor for class X is a copy constructor if
11714 its first parameter is of type X&, const X&, volatile X& or const
11715 volatile X&, and either there are no other parameters or else all
11716 other parameters have default arguments. */
8d08fdba 11717 if (TREE_CODE (parmtype) == REFERENCE_TYPE
0b41abe6
JM
11718 && TYPE_MAIN_VARIANT (TREE_TYPE (parmtype)) == ctype
11719 && (TREE_CHAIN (parmtypes) == NULL_TREE
8d08fdba 11720 || TREE_CHAIN (parmtypes) == void_list_node
f181d4ae
MM
11721 || TREE_PURPOSE (TREE_CHAIN (parmtypes)))
11722 && !(DECL_TEMPLATE_INSTANTIATION (decl)
11723 && is_member_template (DECL_TI_TEMPLATE (decl))))
0b41abe6
JM
11724 {
11725 TYPE_HAS_INIT_REF (ctype) = 1;
91063b51 11726 if (CP_TYPE_CONST_P (TREE_TYPE (parmtype)))
0b41abe6 11727 TYPE_HAS_CONST_INIT_REF (ctype) = 1;
8d08fdba 11728 }
f181d4ae
MM
11729 /* [class.copy]
11730
11731 A declaration of a constructor for a class X is ill-formed if its
11732 first parameter is of type (optionally cv-qualified) X and either
11733 there are no other parameters or else all other parameters have
11734 default arguments.
11735
11736 We *don't* complain about member template instantiations that
11737 have this form, though; they can occur as we try to decide what
11738 constructor to use during overload resolution. Since overload
11739 resolution will never prefer such a constructor to the
11740 non-template copy constructor (which is either explicitly or
11741 implicitly defined), there's no need to worry about their
11742 existence. Theoretically, they should never even be
11743 instantiated, but that's hard to forestall. */
0b41abe6 11744 else if (TYPE_MAIN_VARIANT (parmtype) == ctype
59e76fc6
JM
11745 && (TREE_CHAIN (parmtypes) == NULL_TREE
11746 || TREE_CHAIN (parmtypes) == void_list_node
f181d4ae
MM
11747 || TREE_PURPOSE (TREE_CHAIN (parmtypes)))
11748 && !(DECL_TEMPLATE_INSTANTIATION (decl)
11749 && is_member_template (DECL_TI_TEMPLATE (decl))))
8d08fdba 11750 {
8251199e 11751 cp_error ("invalid constructor; you probably meant `%T (const %T&)'",
0b41abe6
JM
11752 ctype, ctype);
11753 SET_IDENTIFIER_ERROR_LOCUS (DECL_NAME (decl), ctype);
11754 return 0;
8d08fdba
MS
11755 }
11756 else if (TREE_CODE (parmtype) == VOID_TYPE
11757 || TREE_PURPOSE (parmtypes) != NULL_TREE)
11758 TYPE_HAS_DEFAULT_CONSTRUCTOR (ctype) = 1;
a0a33927
MS
11759
11760 return 1;
8d08fdba
MS
11761}
11762
11763/* An operator with this name can be either unary or binary. */
e92cc029 11764
a28e3c7f
MS
11765static int
11766ambi_op_p (name)
8d08fdba
MS
11767 tree name;
11768{
11769 return (name == ansi_opname [(int) INDIRECT_REF]
11770 || name == ansi_opname [(int) ADDR_EXPR]
11771 || name == ansi_opname [(int) NEGATE_EXPR]
11772 || name == ansi_opname[(int) POSTINCREMENT_EXPR]
11773 || name == ansi_opname[(int) POSTDECREMENT_EXPR]
11774 || name == ansi_opname [(int) CONVERT_EXPR]);
11775}
11776
11777/* An operator with this name can only be unary. */
e92cc029 11778
a28e3c7f
MS
11779static int
11780unary_op_p (name)
8d08fdba
MS
11781 tree name;
11782{
11783 return (name == ansi_opname [(int) TRUTH_NOT_EXPR]
11784 || name == ansi_opname [(int) BIT_NOT_EXPR]
11785 || name == ansi_opname [(int) COMPONENT_REF]
4c571114 11786 || IDENTIFIER_TYPENAME_P (name));
8d08fdba
MS
11787}
11788
11789/* Do a little sanity-checking on how they declared their operator. */
e92cc029 11790
5566b478 11791void
8d08fdba
MS
11792grok_op_properties (decl, virtualp, friendp)
11793 tree decl;
11794 int virtualp, friendp;
11795{
11796 tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
11797 int methodp = (TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE);
11798 tree name = DECL_NAME (decl);
8d08fdba 11799
a28e3c7f
MS
11800 if (current_class_type == NULL_TREE)
11801 friendp = 1;
8d08fdba 11802
a28e3c7f
MS
11803 if (! friendp)
11804 {
f181d4ae
MM
11805 /* [class.copy]
11806
11807 A user-declared copy assignment operator X::operator= is a
11808 non-static non-template member function of class X with
11809 exactly one parameter of type X, X&, const X&, volatile X& or
11810 const volatile X&. */
11811 if (name == ansi_opname[(int) MODIFY_EXPR]
11812 && !(DECL_TEMPLATE_INSTANTIATION (decl)
11813 && is_member_template (DECL_TI_TEMPLATE (decl))))
a28e3c7f
MS
11814 TYPE_HAS_ASSIGNMENT (current_class_type) = 1;
11815 else if (name == ansi_opname[(int) CALL_EXPR])
11816 TYPE_OVERLOADS_CALL_EXPR (current_class_type) = 1;
11817 else if (name == ansi_opname[(int) ARRAY_REF])
11818 TYPE_OVERLOADS_ARRAY_REF (current_class_type) = 1;
11819 else if (name == ansi_opname[(int) COMPONENT_REF]
11820 || name == ansi_opname[(int) MEMBER_REF])
11821 TYPE_OVERLOADS_ARROW (current_class_type) = 1;
11822 else if (name == ansi_opname[(int) NEW_EXPR])
11823 TYPE_GETS_NEW (current_class_type) |= 1;
11824 else if (name == ansi_opname[(int) DELETE_EXPR])
11825 TYPE_GETS_DELETE (current_class_type) |= 1;
11826 else if (name == ansi_opname[(int) VEC_NEW_EXPR])
11827 TYPE_GETS_NEW (current_class_type) |= 2;
11828 else if (name == ansi_opname[(int) VEC_DELETE_EXPR])
11829 TYPE_GETS_DELETE (current_class_type) |= 2;
11830 }
11831
11832 if (name == ansi_opname[(int) NEW_EXPR]
11833 || name == ansi_opname[(int) VEC_NEW_EXPR])
8d08fdba 11834 {
8d08fdba
MS
11835 /* When the compiler encounters the definition of A::operator new, it
11836 doesn't look at the class declaration to find out if it's static. */
a28e3c7f 11837 if (methodp)
700f8a87 11838 revert_static_member_fn (&decl, NULL, NULL);
8d08fdba
MS
11839
11840 /* Take care of function decl if we had syntax errors. */
11841 if (argtypes == NULL_TREE)
beb53fb8
JM
11842 TREE_TYPE (decl)
11843 = build_function_type (ptr_type_node,
11844 hash_tree_chain (integer_type_node,
11845 void_list_node));
8d08fdba 11846 else
a28e3c7f 11847 TREE_TYPE (decl) = coerce_new_type (TREE_TYPE (decl));
8d08fdba 11848 }
a28e3c7f
MS
11849 else if (name == ansi_opname[(int) DELETE_EXPR]
11850 || name == ansi_opname[(int) VEC_DELETE_EXPR])
8d08fdba 11851 {
a28e3c7f 11852 if (methodp)
700f8a87 11853 revert_static_member_fn (&decl, NULL, NULL);
8d08fdba
MS
11854
11855 if (argtypes == NULL_TREE)
beb53fb8
JM
11856 TREE_TYPE (decl)
11857 = build_function_type (void_type_node,
11858 hash_tree_chain (ptr_type_node,
11859 void_list_node));
8d08fdba 11860 else
a28e3c7f
MS
11861 {
11862 TREE_TYPE (decl) = coerce_delete_type (TREE_TYPE (decl));
11863
11864 if (! friendp && name == ansi_opname[(int) VEC_DELETE_EXPR]
11865 && (TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
11866 != void_list_node))
11867 TYPE_VEC_DELETE_TAKES_SIZE (current_class_type) = 1;
11868 }
8d08fdba 11869 }
8d08fdba
MS
11870 else
11871 {
11872 /* An operator function must either be a non-static member function
11873 or have at least one parameter of a class, a reference to a class,
11874 an enumeration, or a reference to an enumeration. 13.4.0.6 */
700f8a87 11875 if (! methodp || DECL_STATIC_FUNCTION_P (decl))
8d08fdba 11876 {
4c571114 11877 if (IDENTIFIER_TYPENAME_P (name)
8d08fdba
MS
11878 || name == ansi_opname[(int) CALL_EXPR]
11879 || name == ansi_opname[(int) MODIFY_EXPR]
11880 || name == ansi_opname[(int) COMPONENT_REF]
11881 || name == ansi_opname[(int) ARRAY_REF])
8251199e 11882 cp_error ("`%D' must be a nonstatic member function", decl);
8d08fdba
MS
11883 else
11884 {
11885 tree p = argtypes;
11886
700f8a87 11887 if (DECL_STATIC_FUNCTION_P (decl))
8251199e 11888 cp_error ("`%D' must be either a non-static member function or a non-member function", decl);
700f8a87 11889
8d08fdba 11890 if (p)
a1774733 11891 for (; TREE_CODE (TREE_VALUE (p)) != VOID_TYPE ; p = TREE_CHAIN (p))
8d08fdba
MS
11892 {
11893 tree arg = TREE_VALUE (p);
11894 if (TREE_CODE (arg) == REFERENCE_TYPE)
11895 arg = TREE_TYPE (arg);
11896
11897 /* This lets bad template code slip through. */
11898 if (IS_AGGR_TYPE (arg)
11899 || TREE_CODE (arg) == ENUMERAL_TYPE
73b0fce8
KL
11900 || TREE_CODE (arg) == TEMPLATE_TYPE_PARM
11901 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
8d08fdba
MS
11902 goto foundaggr;
11903 }
8251199e
JM
11904 cp_error
11905 ("`%D' must have an argument of class or enumerated type",
8d08fdba
MS
11906 decl);
11907 foundaggr:
11908 ;
11909 }
11910 }
11911
277294d7 11912 if (name == ansi_opname[(int) CALL_EXPR])
2c73f9f5 11913 return; /* No restrictions on args. */
8d08fdba 11914
9a3b49ac 11915 if (IDENTIFIER_TYPENAME_P (name) && ! DECL_TEMPLATE_INFO (decl))
a0a33927
MS
11916 {
11917 tree t = TREE_TYPE (name);
11918 if (TREE_CODE (t) == VOID_TYPE)
8251199e 11919 pedwarn ("void is not a valid type conversion operator");
a0a33927
MS
11920 else if (! friendp)
11921 {
11922 int ref = (TREE_CODE (t) == REFERENCE_TYPE);
11923 char *what = 0;
11924 if (ref)
11925 t = TYPE_MAIN_VARIANT (TREE_TYPE (t));
11926
11927 if (t == current_class_type)
11928 what = "the same type";
9a3b49ac 11929 /* Don't force t to be complete here. */
a0a33927 11930 else if (IS_AGGR_TYPE (t)
9a3b49ac 11931 && TYPE_SIZE (t)
a0a33927
MS
11932 && DERIVED_FROM_P (t, current_class_type))
11933 what = "a base class";
11934
11935 if (what)
8251199e 11936 warning ("conversion to %s%s will never use a type conversion operator",
a0a33927
MS
11937 ref ? "a reference to " : "", what);
11938 }
11939 }
11940
8d08fdba
MS
11941 if (name == ansi_opname[(int) MODIFY_EXPR])
11942 {
11943 tree parmtype;
11944
11945 if (list_length (argtypes) != 3 && methodp)
11946 {
8251199e 11947 cp_error ("`%D' must take exactly one argument", decl);
8d08fdba
MS
11948 return;
11949 }
11950 parmtype = TREE_VALUE (TREE_CHAIN (argtypes));
11951
f0e01782 11952 if (copy_assignment_arg_p (parmtype, virtualp)
a28e3c7f 11953 && ! friendp)
8d08fdba
MS
11954 {
11955 TYPE_HAS_ASSIGN_REF (current_class_type) = 1;
f0e01782 11956 if (TREE_CODE (parmtype) != REFERENCE_TYPE
91063b51 11957 || CP_TYPE_CONST_P (TREE_TYPE (parmtype)))
8d08fdba
MS
11958 TYPE_HAS_CONST_ASSIGN_REF (current_class_type) = 1;
11959 }
11960 }
11961 else if (name == ansi_opname[(int) COND_EXPR])
11962 {
11963 /* 13.4.0.3 */
8251199e 11964 pedwarn ("ANSI C++ prohibits overloading operator ?:");
8d08fdba 11965 if (list_length (argtypes) != 4)
8251199e 11966 cp_error ("`%D' must take exactly three arguments", decl);
8d08fdba
MS
11967 }
11968 else if (ambi_op_p (name))
11969 {
11970 if (list_length (argtypes) == 2)
11971 /* prefix */;
11972 else if (list_length (argtypes) == 3)
11973 {
11974 if ((name == ansi_opname[(int) POSTINCREMENT_EXPR]
11975 || name == ansi_opname[(int) POSTDECREMENT_EXPR])
5156628f 11976 && ! processing_template_decl
007e5fea 11977 && ! same_type_p (TREE_VALUE (TREE_CHAIN (argtypes)), integer_type_node))
8d08fdba
MS
11978 {
11979 if (methodp)
8251199e 11980 cp_error ("postfix `%D' must take `int' as its argument",
8d08fdba
MS
11981 decl);
11982 else
8251199e
JM
11983 cp_error
11984 ("postfix `%D' must take `int' as its second argument",
11985 decl);
8d08fdba
MS
11986 }
11987 }
11988 else
11989 {
11990 if (methodp)
8251199e 11991 cp_error ("`%D' must take either zero or one argument", decl);
8d08fdba 11992 else
8251199e 11993 cp_error ("`%D' must take either one or two arguments", decl);
8d08fdba 11994 }
824b9a4c
MS
11995
11996 /* More Effective C++ rule 6. */
eb448459 11997 if (warn_ecpp
824b9a4c
MS
11998 && (name == ansi_opname[(int) POSTINCREMENT_EXPR]
11999 || name == ansi_opname[(int) POSTDECREMENT_EXPR]))
12000 {
12001 tree arg = TREE_VALUE (argtypes);
12002 tree ret = TREE_TYPE (TREE_TYPE (decl));
12003 if (methodp || TREE_CODE (arg) == REFERENCE_TYPE)
12004 arg = TREE_TYPE (arg);
12005 arg = TYPE_MAIN_VARIANT (arg);
12006 if (list_length (argtypes) == 2)
12007 {
12008 if (TREE_CODE (ret) != REFERENCE_TYPE
3bfdc719
MM
12009 || !same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (ret)),
12010 arg))
8251199e 12011 cp_warning ("prefix `%D' should return `%T'", decl,
824b9a4c
MS
12012 build_reference_type (arg));
12013 }
12014 else
12015 {
3bfdc719 12016 if (!same_type_p (TYPE_MAIN_VARIANT (ret), arg))
8251199e 12017 cp_warning ("postfix `%D' should return `%T'", decl, arg);
824b9a4c
MS
12018 }
12019 }
8d08fdba
MS
12020 }
12021 else if (unary_op_p (name))
12022 {
12023 if (list_length (argtypes) != 2)
12024 {
12025 if (methodp)
8251199e 12026 cp_error ("`%D' must take `void'", decl);
8d08fdba 12027 else
8251199e 12028 cp_error ("`%D' must take exactly one argument", decl);
8d08fdba
MS
12029 }
12030 }
12031 else /* if (binary_op_p (name)) */
12032 {
12033 if (list_length (argtypes) != 3)
12034 {
12035 if (methodp)
8251199e 12036 cp_error ("`%D' must take exactly one argument", decl);
8d08fdba 12037 else
8251199e 12038 cp_error ("`%D' must take exactly two arguments", decl);
8d08fdba 12039 }
824b9a4c
MS
12040
12041 /* More Effective C++ rule 7. */
eb448459 12042 if (warn_ecpp
824b9a4c
MS
12043 && (name == ansi_opname [TRUTH_ANDIF_EXPR]
12044 || name == ansi_opname [TRUTH_ORIF_EXPR]
12045 || name == ansi_opname [COMPOUND_EXPR]))
8251199e 12046 cp_warning ("user-defined `%D' always evaluates both arguments",
824b9a4c
MS
12047 decl);
12048 }
12049
12050 /* Effective C++ rule 23. */
eb448459 12051 if (warn_ecpp
824b9a4c
MS
12052 && list_length (argtypes) == 3
12053 && (name == ansi_opname [PLUS_EXPR]
12054 || name == ansi_opname [MINUS_EXPR]
12055 || name == ansi_opname [TRUNC_DIV_EXPR]
12056 || name == ansi_opname [MULT_EXPR])
12057 && TREE_CODE (TREE_TYPE (TREE_TYPE (decl))) == REFERENCE_TYPE)
8251199e 12058 cp_warning ("`%D' should return by value", decl);
8d08fdba
MS
12059
12060 /* 13.4.0.8 */
12061 if (argtypes)
12062 for (; argtypes != void_list_node ; argtypes = TREE_CHAIN (argtypes))
12063 if (TREE_PURPOSE (argtypes))
12064 {
12065 TREE_PURPOSE (argtypes) = NULL_TREE;
12066 if (name == ansi_opname[(int) POSTINCREMENT_EXPR]
12067 || name == ansi_opname[(int) POSTDECREMENT_EXPR])
12068 {
12069 if (pedantic)
8251199e 12070 cp_pedwarn ("`%D' cannot have default arguments", decl);
8d08fdba
MS
12071 }
12072 else
8251199e 12073 cp_error ("`%D' cannot have default arguments", decl);
8d08fdba
MS
12074 }
12075 }
12076}
12077\f
094fe153
JM
12078static char *
12079tag_name (code)
12080 enum tag_types code;
12081{
12082 switch (code)
12083 {
12084 case record_type:
12085 return "struct";
12086 case class_type:
12087 return "class";
12088 case union_type:
12089 return "union ";
12090 case enum_type:
12091 return "enum";
12092 case signature_type:
12093 return "signature";
12094 default:
12095 my_friendly_abort (981122);
12096 }
12097}
12098
8d08fdba
MS
12099/* Get the struct, enum or union (CODE says which) with tag NAME.
12100 Define the tag as a forward-reference if it is not defined.
12101
12102 C++: If a class derivation is given, process it here, and report
12103 an error if multiple derivation declarations are not identical.
12104
12105 If this is a definition, come in through xref_tag and only look in
12106 the current frame for the name (since C++ allows new names in any
12107 scope.) */
12108
8d08fdba 12109tree
ca107ded 12110xref_tag (code_type_node, name, globalize)
8d08fdba 12111 tree code_type_node;
ca107ded 12112 tree name;
8d08fdba
MS
12113 int globalize;
12114{
12115 enum tag_types tag_code;
12116 enum tree_code code;
12117 int temp = 0;
8d08fdba
MS
12118 register tree ref, t;
12119 struct binding_level *b = inner_binding_level;
a80e4195 12120 int got_type = 0;
dc8263bc
JM
12121 tree attributes = NULL_TREE;
12122
12123 /* If we are called from the parser, code_type_node will sometimes be a
12124 TREE_LIST. This indicates that the user wrote
12125 "class __attribute__ ((foo)) bar". Extract the attributes so we can
12126 use them later. */
12127 if (TREE_CODE (code_type_node) == TREE_LIST)
12128 {
12129 attributes = TREE_PURPOSE (code_type_node);
12130 code_type_node = TREE_VALUE (code_type_node);
12131 }
8d08fdba
MS
12132
12133 tag_code = (enum tag_types) TREE_INT_CST_LOW (code_type_node);
12134 switch (tag_code)
12135 {
12136 case record_type:
12137 case class_type:
8d08fdba
MS
12138 case signature_type:
12139 code = RECORD_TYPE;
8d08fdba
MS
12140 break;
12141 case union_type:
12142 code = UNION_TYPE;
8d08fdba
MS
12143 break;
12144 case enum_type:
12145 code = ENUMERAL_TYPE;
12146 break;
12147 default:
12148 my_friendly_abort (18);
12149 }
12150
12151 /* If a cross reference is requested, look up the type
12152 already defined for this tag and return it. */
be99da77
MS
12153 if (TREE_CODE_CLASS (TREE_CODE (name)) == 't')
12154 {
12155 t = name;
a80e4195
MS
12156 name = TYPE_IDENTIFIER (t);
12157 got_type = 1;
be99da77
MS
12158 }
12159 else
12160 t = IDENTIFIER_TYPE_VALUE (name);
61a127b3 12161
73b0fce8
KL
12162 if (t && TREE_CODE (t) != code && TREE_CODE (t) != TEMPLATE_TYPE_PARM
12163 && TREE_CODE (t) != TEMPLATE_TEMPLATE_PARM)
a0a33927
MS
12164 t = NULL_TREE;
12165
8ccc31eb 12166 if (! globalize)
8d08fdba 12167 {
f3400fe2
JM
12168 /* If we know we are defining this tag, only look it up in
12169 this scope and don't try to find it as a type. */
12170 ref = lookup_tag (code, name, b, 1);
8d08fdba
MS
12171 }
12172 else
12173 {
36a117a5
MM
12174 if (current_class_type
12175 && template_class_depth (current_class_type)
39c01e4c 12176 && PROCESSING_REAL_TEMPLATE_DECL_P ())
36a117a5
MM
12177 /* Since GLOBALIZE is non-zero, we are not looking at a
12178 definition of this tag. Since, in addition, we are currently
12179 processing a (member) template declaration of a template
12180 class, we don't want to do any lookup at all; consider:
12181
12182 template <class X>
12183 struct S1
12184
12185 template <class U>
12186 struct S2
12187 { template <class V>
12188 friend struct S1; };
12189
12190 Here, the S2::S1 declaration should not be confused with the
12191 outer declaration. In particular, the inner version should
12192 have a template parameter of level 2, not level 1. This
12193 would be particularly important if the member declaration
12194 were instead:
12195
12196 template <class V = U> friend struct S1;
12197
12198 say, when we should tsubst into `U' when instantiating S2. */
12199 ref = NULL_TREE;
12200 else
12201 {
12202 if (t)
094fe153
JM
12203 {
12204 if (t != TYPE_MAIN_VARIANT (t))
12205 cp_pedwarn ("using typedef-name `%D' after `%s'",
12206 TYPE_NAME (t), tag_name (tag_code));
12207 ref = t;
12208 }
36a117a5
MM
12209 else
12210 ref = lookup_tag (code, name, b, 0);
12211
12212 if (! ref)
12213 {
12214 /* Try finding it as a type declaration. If that wins,
12215 use it. */
12216 ref = lookup_name (name, 1);
12217
12218 if (ref != NULL_TREE
12219 && processing_template_decl
12220 && DECL_CLASS_TEMPLATE_P (ref)
12221 && template_class_depth (current_class_type) == 0)
12222 /* Since GLOBALIZE is true, we're declaring a global
6757edfe 12223 template, so we want this type. */
36a117a5 12224 ref = DECL_RESULT (ref);
6757edfe 12225
36a117a5
MM
12226 if (ref && TREE_CODE (ref) == TYPE_DECL
12227 && TREE_CODE (TREE_TYPE (ref)) == code)
12228 ref = TREE_TYPE (ref);
12229 else
12230 ref = NULL_TREE;
12231 }
8d08fdba
MS
12232 }
12233 }
12234
12235 push_obstacks_nochange ();
12236
12237 if (! ref)
12238 {
12239 /* If no such tag is yet defined, create a forward-reference node
12240 and record it as the "definition".
12241 When a real declaration of this type is found,
12242 the forward-reference will be altered into a real type. */
12243
12244 /* In C++, since these migrate into the global scope, we must
12245 build them on the permanent obstack. */
12246
12247 temp = allocation_temporary_p ();
12248 if (temp)
12249 end_temporary_allocation ();
12250
12251 if (code == ENUMERAL_TYPE)
12252 {
8251199e 12253 cp_error ("use of enum `%#D' without previous declaration", name);
fc378698 12254
8d08fdba
MS
12255 ref = make_node (ENUMERAL_TYPE);
12256
12257 /* Give the type a default layout like unsigned int
12258 to avoid crashing if it does not get defined. */
12259 TYPE_MODE (ref) = TYPE_MODE (unsigned_type_node);
12260 TYPE_ALIGN (ref) = TYPE_ALIGN (unsigned_type_node);
12261 TREE_UNSIGNED (ref) = 1;
12262 TYPE_PRECISION (ref) = TYPE_PRECISION (unsigned_type_node);
12263 TYPE_MIN_VALUE (ref) = TYPE_MIN_VALUE (unsigned_type_node);
12264 TYPE_MAX_VALUE (ref) = TYPE_MAX_VALUE (unsigned_type_node);
12265
12266 /* Enable us to recognize when a type is created in class context.
12267 To do nested classes correctly, this should probably be cleared
12268 out when we leave this classes scope. Currently this in only
12269 done in `start_enum'. */
12270
12271 pushtag (name, ref, globalize);
8d08fdba 12272 }
8d08fdba
MS
12273 else
12274 {
8d08fdba 12275 struct binding_level *old_b = class_binding_level;
8d08fdba
MS
12276
12277 ref = make_lang_type (code);
12278
8d08fdba
MS
12279 if (tag_code == signature_type)
12280 {
12281 SET_SIGNATURE (ref);
f376e137
MS
12282 /* Since a signature type will be turned into the type
12283 of signature tables, it's not only an interface. */
8d08fdba 12284 CLASSTYPE_INTERFACE_ONLY (ref) = 0;
f376e137
MS
12285 SET_CLASSTYPE_INTERFACE_KNOWN (ref);
12286 /* A signature doesn't have a vtable. */
00595019 12287 CLASSTYPE_VTABLE_NEEDS_WRITING (ref) = 0;
8d08fdba
MS
12288 }
12289
8d08fdba
MS
12290#ifdef NONNESTED_CLASSES
12291 /* Class types don't nest the way enums do. */
12292 class_binding_level = (struct binding_level *)0;
12293#endif
12294 pushtag (name, ref, globalize);
12295 class_binding_level = old_b;
8d08fdba
MS
12296 }
12297 }
12298 else
12299 {
8d08fdba 12300 /* If it no longer looks like a nested type, make sure it's
30394414
JM
12301 in global scope.
12302 If it is not an IDENTIFIER, this is not a declaration */
2c73f9f5 12303 if (b->namespace_p && !class_binding_level
30394414
JM
12304 && TREE_CODE (name) == IDENTIFIER_NODE)
12305 {
2c73f9f5
ML
12306 if (IDENTIFIER_NAMESPACE_VALUE (name) == NULL_TREE)
12307 SET_IDENTIFIER_NAMESPACE_VALUE (name, TYPE_NAME (ref));
30394414 12308 }
7fe6899f
MM
12309
12310 if (!globalize && processing_template_decl && IS_AGGR_TYPE (ref))
6757edfe 12311 redeclare_class_template (ref, current_template_parms);
8d08fdba
MS
12312 }
12313
8d08fdba
MS
12314 /* Until the type is defined, tentatively accept whatever
12315 structure tag the user hands us. */
12316 if (TYPE_SIZE (ref) == NULL_TREE
12317 && ref != current_class_type
12318 /* Have to check this, in case we have contradictory tag info. */
12319 && IS_AGGR_TYPE_CODE (TREE_CODE (ref)))
12320 {
12321 if (tag_code == class_type)
12322 CLASSTYPE_DECLARED_CLASS (ref) = 1;
12323 else if (tag_code == record_type || tag_code == signature_type)
12324 CLASSTYPE_DECLARED_CLASS (ref) = 0;
12325 }
12326
12327 pop_obstacks ();
12328
dc8263bc
JM
12329 TREE_TYPE (ref) = attributes;
12330
61a127b3
MM
12331 if (ref && TYPE_P (ref))
12332 {
12333 /* [dcl.type.elab]
12334
12335 If the identifier resolves to a typedef-name or a template
12336 type-parameter, the elaborated-type-specifier is
12337 ill-formed. */
12338 if (TYPE_LANG_SPECIFIC (ref) && TYPE_WAS_ANONYMOUS (ref))
12339 cp_error ("`%T' is a typedef name", ref);
12340 else if (TREE_CODE (ref) == TEMPLATE_TYPE_PARM)
12341 cp_error ("`%T' is a template type paramter", ref);
12342 }
12343
8d08fdba
MS
12344 return ref;
12345}
8ccc31eb 12346
fc378698
MS
12347tree
12348xref_tag_from_type (old, id, globalize)
12349 tree old, id;
12350 int globalize;
12351{
12352 tree code_type_node;
12353
12354 if (TREE_CODE (old) == RECORD_TYPE)
12355 code_type_node = (CLASSTYPE_DECLARED_CLASS (old)
12356 ? class_type_node : record_type_node);
12357 else
12358 code_type_node = union_type_node;
12359
12360 if (id == NULL_TREE)
12361 id = TYPE_IDENTIFIER (old);
12362
ca107ded 12363 return xref_tag (code_type_node, id, globalize);
fc378698
MS
12364}
12365
8ccc31eb
MS
12366void
12367xref_basetypes (code_type_node, name, ref, binfo)
12368 tree code_type_node;
12369 tree name, ref;
12370 tree binfo;
12371{
12372 /* In the declaration `A : X, Y, ... Z' we mark all the types
12373 (A, X, Y, ..., Z) so we can check for duplicates. */
12374 tree binfos;
12375 int i, len;
12376 enum tag_types tag_code = (enum tag_types) TREE_INT_CST_LOW (code_type_node);
12377
12378 if (tag_code == union_type)
12379 {
8251199e 12380 cp_error ("derived union `%T' invalid", ref);
8ccc31eb
MS
12381 return;
12382 }
12383
12384 len = list_length (binfo);
12385 push_obstacks (TYPE_OBSTACK (ref), TYPE_OBSTACK (ref));
12386
12387 SET_CLASSTYPE_MARKED (ref);
12388 BINFO_BASETYPES (TYPE_BINFO (ref)) = binfos = make_tree_vec (len);
12389
12390 for (i = 0; binfo; binfo = TREE_CHAIN (binfo))
12391 {
12392 /* The base of a derived struct is public by default. */
12393 int via_public
be99da77
MS
12394 = (TREE_PURPOSE (binfo) == access_public_node
12395 || TREE_PURPOSE (binfo) == access_public_virtual_node
8ccc31eb 12396 || (tag_code != class_type
be99da77
MS
12397 && (TREE_PURPOSE (binfo) == access_default_node
12398 || TREE_PURPOSE (binfo) == access_default_virtual_node)));
d8b55a76
JM
12399 int via_protected
12400 = (TREE_PURPOSE (binfo) == access_protected_node
12401 || TREE_PURPOSE (binfo) == access_protected_virtual_node);
8ccc31eb 12402 int via_virtual
be99da77 12403 = (TREE_PURPOSE (binfo) == access_private_virtual_node
d8b55a76 12404 || TREE_PURPOSE (binfo) == access_protected_virtual_node
be99da77
MS
12405 || TREE_PURPOSE (binfo) == access_public_virtual_node
12406 || TREE_PURPOSE (binfo) == access_default_virtual_node);
12407 tree basetype = TREE_VALUE (binfo);
8ccc31eb
MS
12408 tree base_binfo;
12409
8ccc31eb
MS
12410 if (basetype && TREE_CODE (basetype) == TYPE_DECL)
12411 basetype = TREE_TYPE (basetype);
5566b478
MS
12412 if (!basetype
12413 || (TREE_CODE (basetype) != RECORD_TYPE
a80e4195 12414 && TREE_CODE (basetype) != TYPENAME_TYPE
73b0fce8
KL
12415 && TREE_CODE (basetype) != TEMPLATE_TYPE_PARM
12416 && TREE_CODE (basetype) != TEMPLATE_TEMPLATE_PARM))
8ccc31eb 12417 {
8251199e 12418 cp_error ("base type `%T' fails to be a struct or class type",
8ccc31eb
MS
12419 TREE_VALUE (binfo));
12420 continue;
12421 }
2b9dc906 12422
11b5139c 12423 GNU_xref_hier (name, basetype, via_public, via_virtual, 0);
2b9dc906 12424
8ccc31eb 12425#if 1
8adf5b5e
JM
12426 /* This code replaces similar code in layout_basetypes.
12427 We put the complete_type first for implicit `typename'. */
2b9dc906
JM
12428 if (TYPE_SIZE (complete_type (basetype)) == NULL_TREE
12429 && ! (current_template_parms && uses_template_parms (basetype)))
8ccc31eb 12430 {
8251199e 12431 cp_error ("base class `%T' has incomplete type", basetype);
8ccc31eb
MS
12432 continue;
12433 }
12434#endif
12435 else
12436 {
12437 if (CLASSTYPE_MARKED (basetype))
12438 {
12439 if (basetype == ref)
8251199e 12440 cp_error ("recursive type `%T' undefined", basetype);
8ccc31eb 12441 else
8251199e 12442 cp_error ("duplicate base type `%T' invalid", basetype);
8ccc31eb
MS
12443 continue;
12444 }
12445
eff71ab0
PB
12446 if (TYPE_FOR_JAVA (basetype)
12447 && current_lang_stack == current_lang_base)
12448 TYPE_FOR_JAVA (ref) = 1;
12449
8ccc31eb
MS
12450 /* Note that the BINFO records which describe individual
12451 inheritances are *not* shared in the lattice! They
12452 cannot be shared because a given baseclass may be
12453 inherited with different `accessibility' by different
12454 derived classes. (Each BINFO record describing an
12455 individual inheritance contains flags which say what
12456 the `accessibility' of that particular inheritance is.) */
12457
7ddedda4
MM
12458 base_binfo
12459 = make_binfo (integer_zero_node, basetype,
12460 CLASS_TYPE_P (basetype)
12461 ? TYPE_BINFO_VTABLE (basetype) : NULL_TREE,
12462 CLASS_TYPE_P (basetype)
12463 ? TYPE_BINFO_VIRTUALS (basetype) : NULL_TREE);
8ccc31eb
MS
12464
12465 TREE_VEC_ELT (binfos, i) = base_binfo;
12466 TREE_VIA_PUBLIC (base_binfo) = via_public;
12467 TREE_VIA_PROTECTED (base_binfo) = via_protected;
12468 TREE_VIA_VIRTUAL (base_binfo) = via_virtual;
12469 BINFO_INHERITANCE_CHAIN (base_binfo) = TYPE_BINFO (ref);
12470
dfbcd65a
JM
12471 /* We need to unshare the binfos now so that lookups during class
12472 definition work. */
12473 unshare_base_binfos (base_binfo);
12474
8ccc31eb 12475 SET_CLASSTYPE_MARKED (basetype);
9e9ff709 12476
8ccc31eb
MS
12477 /* We are free to modify these bits because they are meaningless
12478 at top level, and BASETYPE is a top-level type. */
12479 if (via_virtual || TYPE_USES_VIRTUAL_BASECLASSES (basetype))
12480 {
12481 TYPE_USES_VIRTUAL_BASECLASSES (ref) = 1;
12482 TYPE_USES_COMPLEX_INHERITANCE (ref) = 1;
12483 }
12484
7ddedda4
MM
12485 if (CLASS_TYPE_P (basetype))
12486 {
12487 TYPE_GETS_NEW (ref) |= TYPE_GETS_NEW (basetype);
12488 TYPE_GETS_DELETE (ref) |= TYPE_GETS_DELETE (basetype);
12489 }
12490
8ccc31eb
MS
12491 i += 1;
12492 }
12493 }
12494 if (i)
12495 TREE_VEC_LENGTH (binfos) = i;
12496 else
12497 BINFO_BASETYPES (TYPE_BINFO (ref)) = NULL_TREE;
12498
12499 if (i > 1)
12500 TYPE_USES_MULTIPLE_INHERITANCE (ref) = 1;
12501 else if (i == 1)
7ddedda4
MM
12502 {
12503 tree basetype = BINFO_TYPE (TREE_VEC_ELT (binfos, 0));
12504
12505 if (CLASS_TYPE_P (basetype))
12506 TYPE_USES_MULTIPLE_INHERITANCE (ref)
12507 = TYPE_USES_MULTIPLE_INHERITANCE (basetype);
12508 }
12509
8ccc31eb
MS
12510 if (TYPE_USES_MULTIPLE_INHERITANCE (ref))
12511 TYPE_USES_COMPLEX_INHERITANCE (ref) = 1;
12512
12513 /* Unmark all the types. */
12514 while (--i >= 0)
12515 CLEAR_CLASSTYPE_MARKED (BINFO_TYPE (TREE_VEC_ELT (binfos, i)));
12516 CLEAR_CLASSTYPE_MARKED (ref);
12517
12518 pop_obstacks ();
12519}
12520
8d08fdba 12521\f
8d08fdba
MS
12522/* Begin compiling the definition of an enumeration type.
12523 NAME is its name (or null if anonymous).
12524 Returns the type object, as yet incomplete.
12525 Also records info about it so that build_enumerator
12526 may be used to declare the individual values as they are read. */
12527
12528tree
12529start_enum (name)
12530 tree name;
12531{
12532 register tree enumtype = NULL_TREE;
12533 struct binding_level *b = inner_binding_level;
12534
8c1e8edc 12535 /* We are wasting space here and putting these on the permanent_obstack so
6e687e5e 12536 that typeid(local enum) will work correctly. */
cbf882af 12537 push_obstacks (&permanent_obstack, &permanent_obstack);
b87692e5 12538
8d08fdba
MS
12539 /* If this is the real definition for a previous forward reference,
12540 fill in the contents in the same object that used to be the
12541 forward reference. */
12542
12543 if (name != NULL_TREE)
12544 enumtype = lookup_tag (ENUMERAL_TYPE, name, b, 1);
12545
12546 if (enumtype != NULL_TREE && TREE_CODE (enumtype) == ENUMERAL_TYPE)
8251199e 12547 cp_error ("multiple definition of `%#T'", enumtype);
8d08fdba
MS
12548 else
12549 {
12550 enumtype = make_node (ENUMERAL_TYPE);
12551 pushtag (name, enumtype, 0);
12552 }
12553
12554 if (current_class_type)
12555 TREE_ADDRESSABLE (b->tags) = 1;
c91a56d2 12556
e92cc029
MS
12557 /* We don't copy this value because build_enumerator needs to do it. */
12558 enum_next_value = integer_zero_node;
7177d104 12559 enum_overflow = 0;
8d08fdba
MS
12560
12561 GNU_xref_decl (current_function_decl, enumtype);
12562 return enumtype;
12563}
12564
12565/* After processing and defining all the values of an enumeration type,
12566 install their decls in the enumeration type and finish it off.
12567 ENUMTYPE is the type object and VALUES a list of name-value pairs.
12568 Returns ENUMTYPE. */
12569
12570tree
dbfe2124
MM
12571finish_enum (enumtype)
12572 tree enumtype;
8d08fdba 12573{
a703fb38 12574 register tree minnode = NULL_TREE, maxnode = NULL_TREE;
8d08fdba
MS
12575 /* Calculate the maximum value of any enumerator in this type. */
12576
dbfe2124 12577 tree values = TYPE_VALUES (enumtype);
8d08fdba
MS
12578 if (values)
12579 {
ed44da02 12580 tree pair;
5566b478 12581
ed44da02 12582 for (pair = values; pair; pair = TREE_CHAIN (pair))
8d08fdba 12583 {
ed44da02
MM
12584 tree decl;
12585 tree value;
12586
12587 /* The TREE_VALUE is a CONST_DECL for this enumeration
12588 constant. */
12589 decl = TREE_VALUE (pair);
12590
ed44da02
MM
12591 /* The DECL_INITIAL will be NULL if we are processing a
12592 template declaration and this enumeration constant had no
12593 explicit initializer. */
12594 value = DECL_INITIAL (decl);
079e1098 12595 if (value && !processing_template_decl)
5566b478 12596 {
079e1098
MM
12597 /* Set the TREE_TYPE for the VALUE as well. That's so
12598 that when we call decl_constant_value we get an
12599 entity of the right type (but with the constant
12600 value). Since we shouldn't ever call
12601 decl_constant_value on a template type, there's no
12602 reason to do that when processing_template_decl.
12603 And, if the expression is something like a
12604 TEMPLATE_PARM_INDEX or a CAST_EXPR doing so will
12605 wreak havoc on the intended type of the expression.
12606
12607 Of course, there's also no point in trying to compute
12608 minimum or maximum values if we're in a template. */
5566b478 12609 TREE_TYPE (value) = enumtype;
ed44da02 12610
079e1098
MM
12611 if (!minnode)
12612 minnode = maxnode = value;
12613 else if (tree_int_cst_lt (maxnode, value))
12614 maxnode = value;
12615 else if (tree_int_cst_lt (value, minnode))
12616 minnode = value;
5566b478 12617 }
ed44da02 12618
72f2bd78
MM
12619 if (processing_template_decl)
12620 /* If this is just a template, leave the CONST_DECL
12621 alone. That way tsubst_copy will find CONST_DECLs for
12622 CONST_DECLs, and not INTEGER_CSTs. */
12623 ;
12624 else
12625 /* In the list we're building up, we want the enumeration
12626 values, not the CONST_DECLs. */
12627 TREE_VALUE (pair) = value;
8d08fdba
MS
12628 }
12629 }
f376e137
MS
12630 else
12631 maxnode = minnode = integer_zero_node;
8d08fdba 12632
de22184b 12633 TYPE_VALUES (enumtype) = nreverse (values);
2986ae00 12634
5156628f 12635 if (processing_template_decl)
b87692e5 12636 {
9360ac70
MM
12637 tree scope = current_scope ();
12638 if (scope && TREE_CODE (scope) == FUNCTION_DECL)
cbf882af 12639 add_tree (build_min (TAG_DEFN, enumtype));
b87692e5 12640 }
cbf882af
MM
12641 else
12642 {
12643 int unsignedp = tree_int_cst_sgn (minnode) >= 0;
12644 int lowprec = min_precision (minnode, unsignedp);
12645 int highprec = min_precision (maxnode, unsignedp);
12646 int precision = MAX (lowprec, highprec);
12647 tree tem;
5566b478 12648
cbf882af 12649 TYPE_SIZE (enumtype) = NULL_TREE;
8d08fdba 12650
cbf882af 12651 /* Set TYPE_MIN_VALUE and TYPE_MAX_VALUE according to `precision'. */
8ccc31eb 12652
cbf882af
MM
12653 TYPE_PRECISION (enumtype) = precision;
12654 if (unsignedp)
12655 fixup_unsigned_type (enumtype);
12656 else
12657 fixup_signed_type (enumtype);
8ccc31eb 12658
cbf882af
MM
12659 if (flag_short_enums || (precision > TYPE_PRECISION (integer_type_node)))
12660 /* Use the width of the narrowest normal C type which is wide
12661 enough. */
12662 TYPE_PRECISION (enumtype) = TYPE_PRECISION (type_for_size
12663 (precision, 1));
12664 else
12665 TYPE_PRECISION (enumtype) = TYPE_PRECISION (integer_type_node);
8d08fdba 12666
cbf882af
MM
12667 TYPE_SIZE (enumtype) = 0;
12668 layout_type (enumtype);
f376e137 12669
cbf882af
MM
12670 /* Fix up all variant types of this enum type. */
12671 for (tem = TYPE_MAIN_VARIANT (enumtype); tem;
12672 tem = TYPE_NEXT_VARIANT (tem))
12673 {
12674 TYPE_VALUES (tem) = TYPE_VALUES (enumtype);
12675 TYPE_MIN_VALUE (tem) = TYPE_MIN_VALUE (enumtype);
12676 TYPE_MAX_VALUE (tem) = TYPE_MAX_VALUE (enumtype);
12677 TYPE_SIZE (tem) = TYPE_SIZE (enumtype);
12678 TYPE_SIZE_UNIT (tem) = TYPE_SIZE_UNIT (enumtype);
12679 TYPE_MODE (tem) = TYPE_MODE (enumtype);
12680 TYPE_PRECISION (tem) = TYPE_PRECISION (enumtype);
12681 TYPE_ALIGN (tem) = TYPE_ALIGN (enumtype);
12682 TREE_UNSIGNED (tem) = TREE_UNSIGNED (enumtype);
12683 }
8d08fdba 12684
cbf882af
MM
12685 /* Finish debugging output for this type. */
12686 rest_of_type_compilation (enumtype, namespace_bindings_p ());
12687 }
12688
12689 /* In start_enum we pushed obstacks. Here, we must pop them. */
12690 pop_obstacks ();
8d08fdba
MS
12691
12692 return enumtype;
12693}
12694
079e1098
MM
12695/* Build and install a CONST_DECL for an enumeration constant of the
12696 enumeration type TYPE whose NAME and VALUE (if any) are provided.
8d08fdba
MS
12697 Assignment of sequential values by default is handled here. */
12698
12699tree
079e1098
MM
12700build_enumerator (name, value, type)
12701 tree name;
12702 tree value;
12703 tree type;
8d08fdba
MS
12704{
12705 tree decl, result;
e8bd800e 12706 tree context;
8d08fdba
MS
12707
12708 /* Remove no-op casts from the value. */
12709 if (value)
12710 STRIP_TYPE_NOPS (value);
12711
5156628f 12712 if (! processing_template_decl)
5566b478
MS
12713 {
12714 /* Validate and default VALUE. */
12715 if (value != NULL_TREE)
12716 {
12717 if (TREE_READONLY_DECL_P (value))
e92cc029 12718 value = decl_constant_value (value);
5566b478
MS
12719
12720 if (TREE_CODE (value) == INTEGER_CST)
12721 {
12722 value = default_conversion (value);
12723 constant_expression_warning (value);
12724 }
12725 else
12726 {
8251199e 12727 cp_error ("enumerator value for `%D' not integer constant", name);
5566b478
MS
12728 value = NULL_TREE;
12729 }
12730 }
12731
5566b478 12732 /* Default based on previous value. */
5156628f 12733 if (value == NULL_TREE && ! processing_template_decl)
5566b478
MS
12734 {
12735 value = enum_next_value;
12736 if (enum_overflow)
8251199e 12737 cp_error ("overflow in enumeration values at `%D'", name);
5566b478
MS
12738 }
12739
12740 /* Remove no-op casts from the value. */
12741 if (value)
12742 STRIP_TYPE_NOPS (value);
013bc8af
MS
12743#if 0
12744 /* To fix MAX_VAL enum consts. (bkoz) */
e92cc029 12745 TREE_TYPE (value) = integer_type_node;
013bc8af 12746#endif
5566b478 12747 }
8d08fdba 12748
079e1098
MM
12749 /* We always have to copy here; not all INTEGER_CSTs are unshared.
12750 Even in other cases, we will later (in finish_enum) be setting the
12751 type of VALUE. */
ed44da02
MM
12752 if (value != NULL_TREE)
12753 value = copy_node (value);
12754
8d08fdba 12755 /* C++ associates enums with global, function, or class declarations. */
e8bd800e
MM
12756
12757 context = current_scope ();
12758 if (context && context == current_class_type)
12759 /* This enum declaration is local to the class. */
079e1098 12760 decl = build_lang_field_decl (CONST_DECL, name, type);
e8bd800e
MM
12761 else
12762 /* It's a global enum, or it's local to a function. (Note local to
12763 a function could mean local to a class method. */
079e1098 12764 decl = build_decl (CONST_DECL, name, type);
e8bd800e
MM
12765
12766 DECL_CONTEXT (decl) = FROB_CONTEXT (context);
12767 DECL_INITIAL (decl) = value;
12768 TREE_READONLY (decl) = 1;
12769
12770 if (context && context == current_class_type)
12771 {
12772 pushdecl_class_level (decl);
61a127b3
MM
12773 /* In something like `struct S { enum E { i = 7 }; };' we put `i'
12774 on the TYPE_FIELDS list for `S'. (That's so that you can say
12775 things like `S::i' later.) */
12776 finish_member_declaration (decl);
e8bd800e
MM
12777 }
12778 else
12779 {
12780 pushdecl (decl);
12781 GNU_xref_decl (current_function_decl, decl);
12782 }
8d08fdba 12783
5156628f 12784 if (! processing_template_decl)
5566b478
MS
12785 {
12786 /* Set basis for default for next value. */
12787 enum_next_value = build_binary_op_nodefault (PLUS_EXPR, value,
12788 integer_one_node, PLUS_EXPR);
12789 enum_overflow = tree_int_cst_lt (enum_next_value, value);
5566b478 12790 }
8d08fdba 12791
8d2733ca 12792 result = saveable_tree_cons (name, decl, NULL_TREE);
8d08fdba
MS
12793 return result;
12794}
12795
8d08fdba 12796\f
5566b478
MS
12797static int function_depth;
12798
8d08fdba
MS
12799/* Create the FUNCTION_DECL for a function definition.
12800 DECLSPECS and DECLARATOR are the parts of the declaration;
12801 they describe the function's name and the type it returns,
12802 but twisted together in a fashion that parallels the syntax of C.
12803
f181d4ae
MM
12804 If PRE_PARSED_P is non-zero then DECLARATOR is really the DECL for
12805 the function we are about to process; DECLSPECS are ignored. For
12806 example, we set PRE_PARSED_P when processing the definition of
12807 inline function that was defined in-class; the definition is
12808 actually processed when the class is complete. In this case,
12809 PRE_PARSED_P is 2. We also set PRE_PARSED_P when instanting the
12810 body of a template function, and when constructing thunk functions
12811 and such; in these cases PRE_PARSED_P is 1.
12812
8d08fdba
MS
12813 This function creates a binding context for the function body
12814 as well as setting up the FUNCTION_DECL in current_function_decl.
12815
12816 Returns 1 on success. If the DECLARATOR is not suitable for a function
12817 (it defines a datum instead), we return 0, which tells
12818 yyparse to report a parse error.
12819
12820 For C++, we must first check whether that datum makes any sense.
12821 For example, "class A local_a(1,2);" means that variable local_a
12822 is an aggregate of type A, which should have a constructor
12823 applied to it with the argument list [1, 2].
12824
12825 @@ There is currently no way to retrieve the storage
12826 @@ allocated to FUNCTION (or all of its parms) if we return
12827 @@ something we had previously. */
12828
12829int
c11b6f21
MS
12830start_function (declspecs, declarator, attrs, pre_parsed_p)
12831 tree declspecs, declarator, attrs;
8d08fdba
MS
12832 int pre_parsed_p;
12833{
5566b478 12834 tree decl1;
8d08fdba
MS
12835 tree ctype = NULL_TREE;
12836 tree fntype;
12837 tree restype;
12838 extern int have_extern_spec;
12839 extern int used_extern_spec;
12840 int doing_friend = 0;
12841
8d08fdba 12842 /* Sanity check. */
a1774733 12843 my_friendly_assert (TREE_CODE (TREE_VALUE (void_list_node)) == VOID_TYPE, 160);
8d08fdba
MS
12844 my_friendly_assert (TREE_CHAIN (void_list_node) == NULL_TREE, 161);
12845
e92cc029 12846 /* Assume, until we see it does. */
8d08fdba
MS
12847 current_function_returns_value = 0;
12848 current_function_returns_null = 0;
fd378c9d
JM
12849 named_labels = 0;
12850 shadowed_labels = 0;
8d08fdba
MS
12851 current_function_assigns_this = 0;
12852 current_function_just_assigned_this = 0;
12853 current_function_parms_stored = 0;
12854 original_result_rtx = NULL_RTX;
a9aedbc2 12855 base_init_expr = NULL_TREE;
79ff2c6c
MS
12856 current_base_init_list = NULL_TREE;
12857 current_member_init_list = NULL_TREE;
f30432d7 12858 ctor_label = dtor_label = NULL_TREE;
42976354 12859 static_labelno = 0;
8d08fdba
MS
12860
12861 clear_temp_name ();
12862
e92cc029 12863 /* This should only be done once on the top most decl. */
8d08fdba
MS
12864 if (have_extern_spec && !used_extern_spec)
12865 {
12866 declspecs = decl_tree_cons (NULL_TREE, get_identifier ("extern"), declspecs);
12867 used_extern_spec = 1;
12868 }
12869
12870 if (pre_parsed_p)
12871 {
12872 decl1 = declarator;
12873
67f7c391
JM
12874#if 0
12875 /* What was this testing for, exactly? */
8d08fdba
MS
12876 if (! DECL_ARGUMENTS (decl1)
12877 && !DECL_STATIC_FUNCTION_P (decl1)
67f7c391 12878 && !DECL_ARTIFICIAL (decl1)
6eb3bb27 12879 && DECL_CLASS_SCOPE_P (decl1)
d2e5ee5c
MS
12880 && TYPE_IDENTIFIER (DECL_CONTEXT (decl1))
12881 && IDENTIFIER_TEMPLATE (TYPE_IDENTIFIER (DECL_CONTEXT (decl1))))
8d08fdba 12882 {
30394414
JM
12883 tree binding = binding_for_name (DECL_NAME (decl1),
12884 current_namespace);
8251199e 12885 cp_error ("redeclaration of `%#D'", decl1);
8d08fdba 12886 if (IDENTIFIER_CLASS_VALUE (DECL_NAME (decl1)))
8251199e 12887 cp_error_at ("previous declaration here", IDENTIFIER_CLASS_VALUE (DECL_NAME (decl1)));
30394414 12888 else if (BINDING_VALUE (binding))
8251199e 12889 cp_error_at ("previous declaration here", BINDING_VALUE (binding));
8d08fdba 12890 }
67f7c391 12891#endif
8d08fdba 12892
8d08fdba
MS
12893 fntype = TREE_TYPE (decl1);
12894 if (TREE_CODE (fntype) == METHOD_TYPE)
12895 ctype = TYPE_METHOD_BASETYPE (fntype);
12896
12897 /* ANSI C++ June 5 1992 WP 11.4.5. A friend function defined in a
12898 class is in the (lexical) scope of the class in which it is
12899 defined. */
12900 if (!ctype && DECL_FRIEND_P (decl1))
12901 {
12902 ctype = DECL_CLASS_CONTEXT (decl1);
12903
12904 /* CTYPE could be null here if we're dealing with a template;
12905 for example, `inline friend float foo()' inside a template
12906 will have no CTYPE set. */
12907 if (ctype && TREE_CODE (ctype) != RECORD_TYPE)
12908 ctype = NULL_TREE;
12909 else
12910 doing_friend = 1;
12911 }
12912
5566b478
MS
12913 last_function_parms = DECL_ARGUMENTS (decl1);
12914 last_function_parm_tags = NULL_TREE;
8d08fdba
MS
12915 }
12916 else
12917 {
c11b6f21 12918 decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, 1, NULL_TREE);
8d08fdba
MS
12919 /* If the declarator is not suitable for a function definition,
12920 cause a syntax error. */
12921 if (decl1 == NULL_TREE || TREE_CODE (decl1) != FUNCTION_DECL) return 0;
12922
12923 fntype = TREE_TYPE (decl1);
12924
12925 restype = TREE_TYPE (fntype);
7ddedda4 12926 if (CLASS_TYPE_P (restype) && !CLASSTYPE_GOT_SEMICOLON (restype))
8d08fdba 12927 {
8251199e 12928 cp_error ("semicolon missing after declaration of `%#T'", restype);
e66d884e 12929 shadow_tag (build_expr_list (NULL_TREE, restype));
8d08fdba
MS
12930 CLASSTYPE_GOT_SEMICOLON (restype) = 1;
12931 if (TREE_CODE (fntype) == FUNCTION_TYPE)
12932 fntype = build_function_type (integer_type_node,
12933 TYPE_ARG_TYPES (fntype));
12934 else
12935 fntype = build_cplus_method_type (build_type_variant (TYPE_METHOD_BASETYPE (fntype), TREE_READONLY (decl1), TREE_SIDE_EFFECTS (decl1)),
12936 integer_type_node,
12937 TYPE_ARG_TYPES (fntype));
12938 TREE_TYPE (decl1) = fntype;
12939 }
12940
12941 if (TREE_CODE (fntype) == METHOD_TYPE)
12942 ctype = TYPE_METHOD_BASETYPE (fntype);
35680744 12943 else if (DECL_MAIN_P (decl1))
8d08fdba
MS
12944 {
12945 /* If this doesn't return integer_type, complain. */
12946 if (TREE_TYPE (TREE_TYPE (decl1)) != integer_type_node)
12947 {
a28e3c7f 12948 if (pedantic || warn_return_type)
8251199e 12949 pedwarn ("return type for `main' changed to `int'");
8d08fdba
MS
12950 TREE_TYPE (decl1) = fntype = default_function_type;
12951 }
8d08fdba
MS
12952 }
12953 }
12954
12955 /* Warn if function was previously implicitly declared
12956 (but not if we warned then). */
12957 if (! warn_implicit
12958 && IDENTIFIER_IMPLICIT_DECL (DECL_NAME (decl1)) != NULL_TREE)
8251199e 12959 cp_warning_at ("`%D' implicitly declared before its definition", IDENTIFIER_IMPLICIT_DECL (DECL_NAME (decl1)));
8d08fdba 12960
f181d4ae
MM
12961 announce_function (decl1);
12962
12963 /* Set up current_class_type, and enter the scope of the class, if
12964 appropriate. */
12965 if (ctype)
12966 push_nested_class (ctype, 1);
12967 else if (DECL_STATIC_FUNCTION_P (decl1))
12968 push_nested_class (DECL_CONTEXT (decl1), 2);
12969
12970 /* Now that we have entered the scope of the class, we must restore
12971 the bindings for any template parameters surrounding DECL1, if it
12972 is an inline member template. (Order is important; consider the
12973 case where a template parameter has the same name as a field of
12974 the class.) It is not until after this point that
12975 PROCESSING_TEMPLATE_DECL is guaranteed to be set up correctly. */
12976 if (pre_parsed_p == 2)
12977 maybe_begin_member_template_processing (decl1);
12978
12979 /* We are now in the scope of the function being defined. */
8d08fdba 12980 current_function_decl = decl1;
f181d4ae 12981
5566b478
MS
12982 /* Save the parm names or decls from this function's declarator
12983 where store_parm_decls will find them. */
12984 current_function_parms = last_function_parms;
12985 current_function_parm_tags = last_function_parm_tags;
8d08fdba 12986
5156628f 12987 if (! processing_template_decl)
8d08fdba 12988 {
d490621d
MM
12989 /* In a function definition, arg types must be complete. */
12990 require_complete_types_for_parms (current_function_parms);
f181d4ae 12991
5566b478
MS
12992 if (TYPE_SIZE (complete_type (TREE_TYPE (fntype))) == NULL_TREE)
12993 {
8251199e 12994 cp_error ("return-type `%#T' is an incomplete type",
5566b478 12995 TREE_TYPE (fntype));
8d08fdba 12996
5566b478
MS
12997 /* Make it return void instead, but don't change the
12998 type of the DECL_RESULT, in case we have a named return value. */
12999 if (ctype)
13000 TREE_TYPE (decl1)
13001 = build_cplus_method_type (build_type_variant (ctype,
13002 TREE_READONLY (decl1),
13003 TREE_SIDE_EFFECTS (decl1)),
13004 void_type_node,
13005 FUNCTION_ARG_CHAIN (decl1));
13006 else
13007 TREE_TYPE (decl1)
13008 = build_function_type (void_type_node,
13009 TYPE_ARG_TYPES (TREE_TYPE (decl1)));
13010 DECL_RESULT (decl1)
13011 = build_decl (RESULT_DECL, 0, TYPE_MAIN_VARIANT (TREE_TYPE (fntype)));
91063b51
MM
13012 TREE_READONLY (DECL_RESULT (decl1))
13013 = CP_TYPE_CONST_P (TREE_TYPE (fntype));
13014 TREE_THIS_VOLATILE (DECL_RESULT (decl1))
13015 = CP_TYPE_VOLATILE_P (TREE_TYPE (fntype));
5566b478
MS
13016 }
13017
13018 if (TYPE_LANG_SPECIFIC (TREE_TYPE (fntype))
13019 && CLASSTYPE_ABSTRACT_VIRTUALS (TREE_TYPE (fntype)))
13020 abstract_virtuals_error (decl1, TREE_TYPE (fntype));
13021 }
b7484fbe 13022
824b9a4c 13023 /* Effective C++ rule 15. See also c_expand_return. */
eb448459 13024 if (warn_ecpp
824b9a4c 13025 && DECL_NAME (decl1) == ansi_opname[(int) MODIFY_EXPR]
a1774733 13026 && TREE_CODE (TREE_TYPE (fntype)) == VOID_TYPE)
8251199e 13027 cp_warning ("`operator=' should return a reference to `*this'");
824b9a4c 13028
2a2480e1
JM
13029 /* Make the init_value nonzero so pushdecl knows this is not tentative.
13030 error_mark_node is replaced below (in poplevel) with the BLOCK. */
13031 DECL_INITIAL (decl1) = error_mark_node;
13032
3a846e6e 13033#ifdef SET_DEFAULT_DECL_ATTRIBUTES
76fb7947 13034 SET_DEFAULT_DECL_ATTRIBUTES (decl1, attrs);
3a846e6e
NC
13035#endif
13036
8d08fdba
MS
13037 /* This function exists in static storage.
13038 (This does not mean `static' in the C sense!) */
13039 TREE_STATIC (decl1) = 1;
13040
36a117a5
MM
13041 /* We must call push_template_decl after current_class_type is set
13042 up. (If we are processing inline definitions after exiting a
13043 class scope, current_class_type will be NULL_TREE until set above
13044 by push_nested_class.) */
13045 if (processing_template_decl)
13046 decl1 = push_template_decl (decl1);
13047
878cd289
MS
13048 /* Record the decl so that the function name is defined.
13049 If we already have a decl for this name, and it is a FUNCTION_DECL,
13050 use the old decl. */
36a117a5 13051 if (!processing_template_decl && pre_parsed_p == 0)
878cd289 13052 {
75650646 13053 /* A specialization is not used to guide overload resolution. */
959d8796
JM
13054 if ((flag_guiding_decls
13055 || !DECL_TEMPLATE_SPECIALIZATION (decl1))
13056 && ! DECL_FUNCTION_MEMBER_P (decl1))
75650646 13057 decl1 = pushdecl (decl1);
2c73f9f5 13058 else
b7698cf0
JM
13059 {
13060 /* We need to set the DECL_CONTEXT. */
13061 if (!DECL_CONTEXT (decl1) && DECL_TEMPLATE_INFO (decl1))
13062 DECL_CONTEXT (decl1) = DECL_CONTEXT (DECL_TI_TEMPLATE (decl1));
13063 /* And make sure we have enough default args. */
13064 check_default_args (decl1);
13065 }
878cd289
MS
13066 DECL_MAIN_VARIANT (decl1) = decl1;
13067 fntype = TREE_TYPE (decl1);
13068 }
5566b478
MS
13069
13070 current_function_decl = decl1;
878cd289 13071
db5ae43f 13072 if (DECL_INTERFACE_KNOWN (decl1))
faae18ab 13073 {
86052cc3
JM
13074 tree ctx = hack_decl_function_context (decl1);
13075
faae18ab
MS
13076 if (DECL_NOT_REALLY_EXTERN (decl1))
13077 DECL_EXTERNAL (decl1) = 0;
86052cc3
JM
13078
13079 if (ctx != NULL_TREE && DECL_THIS_INLINE (ctx)
13080 && TREE_PUBLIC (ctx))
13081 /* This is a function in a local class in an extern inline
13082 function. */
13083 comdat_linkage (decl1);
faae18ab 13084 }
8d08fdba
MS
13085 /* If this function belongs to an interface, it is public.
13086 If it belongs to someone else's interface, it is also external.
1f901793 13087 This only affects inlines and template instantiations. */
5566b478
MS
13088 else if (interface_unknown == 0
13089 && (! DECL_TEMPLATE_INSTANTIATION (decl1)
d11ad92e 13090 || flag_alt_external_templates))
8d08fdba 13091 {
5566b478 13092 if (DECL_THIS_INLINE (decl1) || DECL_TEMPLATE_INSTANTIATION (decl1)
5156628f 13093 || processing_template_decl)
1f901793
JM
13094 {
13095 DECL_EXTERNAL (decl1)
13096 = (interface_only
13097 || (DECL_THIS_INLINE (decl1) && ! flag_implement_inlines));
13098
13099 /* For WIN32 we also want to put these in linkonce sections. */
13100 maybe_make_one_only (decl1);
13101 }
db5ae43f 13102 else
893de33c 13103 DECL_EXTERNAL (decl1) = 0;
e8abc66f 13104 DECL_NOT_REALLY_EXTERN (decl1) = 0;
db5ae43f 13105 DECL_INTERFACE_KNOWN (decl1) = 1;
8d08fdba
MS
13106 }
13107 else
a0a33927
MS
13108 {
13109 /* This is a definition, not a reference.
b7484fbe
MS
13110 So clear DECL_EXTERNAL. */
13111 DECL_EXTERNAL (decl1) = 0;
faae18ab 13112
5566b478
MS
13113 if ((DECL_THIS_INLINE (decl1) || DECL_TEMPLATE_INSTANTIATION (decl1))
13114 && ! DECL_INTERFACE_KNOWN (decl1)
13115 /* Don't try to defer nested functions for now. */
e76a2646 13116 && ! hack_decl_function_context (decl1))
878cd289
MS
13117 DECL_DEFER_OUTPUT (decl1) = 1;
13118 else
893de33c 13119 DECL_INTERFACE_KNOWN (decl1) = 1;
db5ae43f 13120 }
a9aedbc2 13121
8d08fdba
MS
13122 if (ctype != NULL_TREE && DECL_STATIC_FUNCTION_P (decl1))
13123 {
13124 if (TREE_CODE (fntype) == METHOD_TYPE)
13125 TREE_TYPE (decl1) = fntype
13126 = build_function_type (TREE_TYPE (fntype),
13127 TREE_CHAIN (TYPE_ARG_TYPES (fntype)));
5566b478
MS
13128 current_function_parms = TREE_CHAIN (current_function_parms);
13129 DECL_ARGUMENTS (decl1) = current_function_parms;
8d08fdba
MS
13130 ctype = NULL_TREE;
13131 }
13132 restype = TREE_TYPE (fntype);
13133
8d08fdba
MS
13134 if (ctype)
13135 {
8d08fdba 13136 /* If we're compiling a friend function, neither of the variables
4ac14744 13137 current_class_ptr nor current_class_type will have values. */
8d08fdba
MS
13138 if (! doing_friend)
13139 {
13140 /* We know that this was set up by `grokclassfn'.
13141 We do not wait until `store_parm_decls', since evil
13142 parse errors may never get us to that point. Here
13143 we keep the consistency between `current_class_type'
4ac14744 13144 and `current_class_ptr'. */
5566b478 13145 tree t = current_function_parms;
5f34005f
RK
13146
13147 my_friendly_assert (t != NULL_TREE
13148 && TREE_CODE (t) == PARM_DECL, 162);
13149
dff6b454
RK
13150 if (TREE_CODE (TREE_TYPE (t)) == POINTER_TYPE)
13151 {
7ddedda4 13152 int i;
dff6b454 13153
7ddedda4
MM
13154 if (! hack_decl_function_context (decl1))
13155 temporary_allocation ();
13156 i = suspend_momentary ();
13157
13158 /* Normally, build_indirect_ref returns
13159 current_class_ref whenever current_class_ptr is
13160 dereferenced. This time, however, we want it to
13161 *create* current_class_ref, so we temporarily clear
13162 current_class_ptr to fool it. */
4ac14744
MS
13163 current_class_ptr = NULL_TREE;
13164 current_class_ref = build_indirect_ref (t, NULL_PTR);
13165 current_class_ptr = t;
7ddedda4 13166
dff6b454 13167 resume_momentary (i);
7ddedda4
MM
13168 if (! hack_decl_function_context (decl1))
13169 end_temporary_allocation ();
dff6b454
RK
13170 }
13171 else
13172 /* We're having a signature pointer here. */
4ac14744 13173 current_class_ref = current_class_ptr = t;
dff6b454 13174
8d08fdba
MS
13175 }
13176 }
13177 else
7ddedda4 13178 current_class_ptr = current_class_ref = NULL_TREE;
8d08fdba 13179
db5ae43f
MS
13180 pushlevel (0);
13181 current_binding_level->parm_flag = 1;
13182
db5ae43f
MS
13183 GNU_xref_function (decl1, current_function_parms);
13184
f30432d7
MS
13185 if (attrs)
13186 cplus_decl_attributes (decl1, NULL_TREE, attrs);
3a846e6e 13187
db5ae43f
MS
13188 make_function_rtl (decl1);
13189
8d08fdba
MS
13190 /* Promote the value to int before returning it. */
13191 if (C_PROMOTING_INTEGER_TYPE_P (restype))
5566b478
MS
13192 restype = type_promotes_to (restype);
13193
13194 /* If this fcn was already referenced via a block-scope `extern' decl
13195 (or an implicit decl), propagate certain information about the usage. */
13196 if (TREE_ADDRESSABLE (DECL_ASSEMBLER_NAME (decl1)))
13197 TREE_ADDRESSABLE (decl1) = 1;
13198
8d08fdba 13199 if (DECL_RESULT (decl1) == NULL_TREE)
f30432d7
MS
13200 {
13201 DECL_RESULT (decl1)
13202 = build_decl (RESULT_DECL, 0, TYPE_MAIN_VARIANT (restype));
91063b51
MM
13203 TREE_READONLY (DECL_RESULT (decl1)) = CP_TYPE_CONST_P (restype);
13204 TREE_THIS_VOLATILE (DECL_RESULT (decl1)) = CP_TYPE_VOLATILE_P (restype);
f30432d7 13205 }
8d08fdba 13206
948edfa7
JW
13207 /* Allocate further tree nodes temporarily during compilation
13208 of this function only. Tiemann moved up here from bottom of fn. */
13209 /* If this is a nested function, then we must continue to allocate RTL
13210 on the permanent obstack in case we need to inline it later. */
13211 if (! hack_decl_function_context (decl1))
5566b478
MS
13212 temporary_allocation ();
13213
5156628f 13214 if (processing_template_decl)
5566b478 13215 {
5566b478
MS
13216 ++minimal_parse_mode;
13217 last_tree = DECL_SAVED_TREE (decl1)
13218 = build_nt (EXPR_STMT, void_zero_node);
13219 }
13220
13221 ++function_depth;
13222
f30432d7
MS
13223 if (DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (decl1))
13224 && DECL_LANGUAGE (decl1) == lang_cplusplus)
8d08fdba
MS
13225 {
13226 dtor_label = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE);
13227 ctor_label = NULL_TREE;
13228 }
13229 else
13230 {
13231 dtor_label = NULL_TREE;
13232 if (DECL_CONSTRUCTOR_P (decl1))
13233 ctor_label = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE);
13234 }
13235
8d08fdba
MS
13236 return 1;
13237}
13238\f
9664b89e
JM
13239/* Called after store_parm_decls for a function-try-block. We need to update
13240 last_parm_cleanup_insn so that the base initializers for a constructor
13241 are run within this block, not before it. */
6467930b
MS
13242
13243void
13244expand_start_early_try_stmts ()
13245{
6467930b 13246 expand_start_try_stmts ();
9664b89e 13247 last_parm_cleanup_insn = get_last_insn ();
f30432d7
MS
13248}
13249
8d08fdba
MS
13250/* Store the parameter declarations into the current function declaration.
13251 This is called after parsing the parameter declarations, before
13252 digesting the body of the function.
13253
13254 Also install to binding contour return value identifier, if any. */
13255
13256void
13257store_parm_decls ()
13258{
13259 register tree fndecl = current_function_decl;
13260 register tree parm;
13261 int parms_have_cleanups = 0;
eb66be0e 13262 tree cleanups = NULL_TREE;
8d08fdba
MS
13263
13264 /* This is either a chain of PARM_DECLs (when a prototype is used). */
13265 tree specparms = current_function_parms;
13266
13267 /* This is a list of types declared among parms in a prototype. */
13268 tree parmtags = current_function_parm_tags;
13269
13270 /* This is a chain of any other decls that came in among the parm
13271 declarations. If a parm is declared with enum {foo, bar} x;
13272 then CONST_DECLs for foo and bar are put here. */
13273 tree nonparms = NULL_TREE;
13274
a9aedbc2 13275 if (toplevel_bindings_p ())
8d08fdba
MS
13276 fatal ("parse errors have confused me too much");
13277
13278 /* Initialize RTL machinery. */
13279 init_function_start (fndecl, input_filename, lineno);
13280
8d08fdba
MS
13281 /* Create a binding level for the parms. */
13282 expand_start_bindings (0);
13283
8d08fdba
MS
13284 if (specparms != NULL_TREE)
13285 {
13286 /* This case is when the function was defined with an ANSI prototype.
13287 The parms already have decls, so we need not do anything here
13288 except record them as in effect
13289 and complain if any redundant old-style parm decls were written. */
13290
13291 register tree next;
13292
13293 /* Must clear this because it might contain TYPE_DECLs declared
13294 at class level. */
13295 storedecls (NULL_TREE);
5566b478 13296
8d08fdba
MS
13297 for (parm = nreverse (specparms); parm; parm = next)
13298 {
13299 next = TREE_CHAIN (parm);
13300 if (TREE_CODE (parm) == PARM_DECL)
13301 {
5566b478 13302 tree cleanup;
8d08fdba
MS
13303 if (DECL_NAME (parm) == NULL_TREE)
13304 {
8d08fdba 13305 pushdecl (parm);
8d08fdba 13306 }
a1774733 13307 else if (TREE_CODE (TREE_TYPE (parm)) == VOID_TYPE)
8251199e 13308 cp_error ("parameter `%D' declared void", parm);
8d08fdba
MS
13309 else
13310 {
13311 /* Now fill in DECL_REFERENCE_SLOT for any of the parm decls.
13312 A parameter is assumed not to have any side effects.
13313 If this should change for any reason, then this
13314 will have to wrap the bashed reference type in a save_expr.
13315
13316 Also, if the parameter type is declared to be an X
13317 and there is an X(X&) constructor, we cannot lay it
13318 into the stack (any more), so we make this parameter
13319 look like it is really of reference type. Functions
13320 which pass parameters to this function will know to
13321 create a temporary in their frame, and pass a reference
13322 to that. */
13323
13324 if (TREE_CODE (TREE_TYPE (parm)) == REFERENCE_TYPE
13325 && TYPE_SIZE (TREE_TYPE (TREE_TYPE (parm))))
13326 SET_DECL_REFERENCE_SLOT (parm, convert_from_reference (parm));
13327
13328 pushdecl (parm);
13329 }
5156628f 13330 if (! processing_template_decl
5566b478 13331 && (cleanup = maybe_build_cleanup (parm), cleanup))
8d08fdba
MS
13332 {
13333 expand_decl (parm);
8d08fdba 13334 parms_have_cleanups = 1;
eb66be0e
MS
13335
13336 /* Keep track of the cleanups. */
13337 cleanups = tree_cons (parm, cleanup, cleanups);
8d08fdba
MS
13338 }
13339 }
13340 else
13341 {
13342 /* If we find an enum constant or a type tag,
13343 put it aside for the moment. */
13344 TREE_CHAIN (parm) = NULL_TREE;
13345 nonparms = chainon (nonparms, parm);
13346 }
13347 }
13348
13349 /* Get the decls in their original chain order
13350 and record in the function. This is all and only the
13351 PARM_DECLs that were pushed into scope by the loop above. */
13352 DECL_ARGUMENTS (fndecl) = getdecls ();
13353
13354 storetags (chainon (parmtags, gettags ()));
13355 }
13356 else
13357 DECL_ARGUMENTS (fndecl) = NULL_TREE;
13358
13359 /* Now store the final chain of decls for the arguments
13360 as the decl-chain of the current lexical scope.
13361 Put the enumerators in as well, at the front so that
13362 DECL_ARGUMENTS is not modified. */
13363
13364 storedecls (chainon (nonparms, DECL_ARGUMENTS (fndecl)));
13365
f181d4ae
MM
13366 /* Declare __FUNCTION__ and __PRETTY_FUNCTION__ for this function. */
13367 declare_function_name ();
13368
8d08fdba
MS
13369 /* Initialize the RTL code for the function. */
13370 DECL_SAVED_INSNS (fndecl) = NULL_RTX;
5156628f 13371 if (! processing_template_decl)
5566b478 13372 expand_function_start (fndecl, parms_have_cleanups);
8d08fdba 13373
eb448459
MS
13374 current_function_parms_stored = 1;
13375
13376 /* If this function is `main', emit a call to `__main'
13377 to run global initializers, etc. */
35680744
MM
13378 if (DECL_MAIN_P (fndecl))
13379 expand_main_function ();
eb448459 13380
eb66be0e
MS
13381 /* Now that we have initialized the parms, we can start their
13382 cleanups. We cannot do this before, since expand_decl_cleanup
13383 should not be called before the parm can be used. */
eb448459 13384 if (cleanups
eb66be0e
MS
13385 && ! processing_template_decl)
13386 {
13387 for (cleanups = nreverse (cleanups); cleanups; cleanups = TREE_CHAIN (cleanups))
13388 {
13389 if (! expand_decl_cleanup (TREE_PURPOSE (cleanups), TREE_VALUE (cleanups)))
8251199e 13390 cp_error ("parser lost in parsing declaration of `%D'",
eb66be0e
MS
13391 TREE_PURPOSE (cleanups));
13392 }
13393 }
13394
8d08fdba
MS
13395 /* Create a binding contour which can be used to catch
13396 cleanup-generated temporaries. Also, if the return value needs or
13397 has initialization, deal with that now. */
13398 if (parms_have_cleanups)
13399 {
13400 pushlevel (0);
13401 expand_start_bindings (0);
13402 }
13403
5156628f 13404 if (! processing_template_decl && flag_exceptions)
f30432d7 13405 {
f30432d7
MS
13406 /* Do the starting of the exception specifications, if we have any. */
13407 if (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (current_function_decl)))
13408 expand_start_eh_spec ();
f30432d7 13409 }
eb448459 13410
5c825fc2 13411 last_parm_cleanup_insn = get_last_insn ();
72b7eeff 13412 last_dtor_insn = get_last_insn ();
8d08fdba
MS
13413}
13414
13415/* Bind a name and initialization to the return value of
13416 the current function. */
e92cc029 13417
8d08fdba
MS
13418void
13419store_return_init (return_id, init)
13420 tree return_id, init;
13421{
13422 tree decl = DECL_RESULT (current_function_decl);
13423
e1cd6e56 13424 if (pedantic)
8d08fdba
MS
13425 /* Give this error as many times as there are occurrences,
13426 so that users can use Emacs compilation buffers to find
13427 and fix all such places. */
8251199e 13428 pedwarn ("ANSI C++ does not permit named return values");
8d08fdba
MS
13429
13430 if (return_id != NULL_TREE)
13431 {
13432 if (DECL_NAME (decl) == NULL_TREE)
13433 {
13434 DECL_NAME (decl) = return_id;
13435 DECL_ASSEMBLER_NAME (decl) = return_id;
13436 }
13437 else
8251199e 13438 cp_error ("return identifier `%D' already in place", decl);
8d08fdba
MS
13439 }
13440
13441 /* Can't let this happen for constructors. */
13442 if (DECL_CONSTRUCTOR_P (current_function_decl))
13443 {
8251199e 13444 error ("can't redefine default return value for constructors");
8d08fdba
MS
13445 return;
13446 }
13447
13448 /* If we have a named return value, put that in our scope as well. */
13449 if (DECL_NAME (decl) != NULL_TREE)
13450 {
13451 /* If this named return value comes in a register,
13452 put it in a pseudo-register. */
13453 if (DECL_REGISTER (decl))
13454 {
13455 original_result_rtx = DECL_RTL (decl);
13456 DECL_RTL (decl) = gen_reg_rtx (DECL_MODE (decl));
13457 }
13458
82580166 13459 /* Let `cp_finish_decl' know that this initializer is ok. */
8d08fdba
MS
13460 DECL_INITIAL (decl) = init;
13461 pushdecl (decl);
e76a2646
MS
13462
13463 if (minimal_parse_mode)
13464 add_tree (build_min_nt (RETURN_INIT, return_id,
13465 copy_to_permanent (init)));
13466 else
dae1d6f6 13467 cp_finish_decl (decl, init, NULL_TREE, 0, 0);
8d08fdba
MS
13468 }
13469}
13470
8d08fdba
MS
13471\f
13472/* Finish up a function declaration and compile that function
13473 all the way to assembler language output. The free the storage
13474 for the function definition.
13475
13476 This is called after parsing the body of the function definition.
13477 LINENO is the current line number.
13478
f181d4ae
MM
13479 FLAGS is a bitwise or of the following values:
13480 1 - CALL_POPLEVEL
13481 An extra call to poplevel (and expand_end_bindings) must be
13482 made to take care of the binding contour for the base
13483 initializers. This is only relevant for constructors.
13484 2 - INCLASS_INLINE
13485 We just finished processing the body of an in-class inline
13486 function definition. (This processing will have taken place
13487 after the class definition is complete.)
e92cc029
MS
13488
13489 NESTED is nonzero if we were in the middle of compiling another function
13490 when we started on this one. */
8d08fdba
MS
13491
13492void
f181d4ae 13493finish_function (lineno, flags, nested)
8d08fdba 13494 int lineno;
f181d4ae 13495 int flags;
db5ae43f 13496 int nested;
8d08fdba
MS
13497{
13498 register tree fndecl = current_function_decl;
13499 tree fntype, ctype = NULL_TREE;
f30432d7 13500 rtx last_parm_insn, insns;
8d08fdba
MS
13501 /* Label to use if this function is supposed to return a value. */
13502 tree no_return_label = NULL_TREE;
13503 tree decls = NULL_TREE;
f181d4ae
MM
13504 int call_poplevel = (flags & 1) != 0;
13505 int inclass_inline = (flags & 2) != 0;
13506 int in_template;
8d08fdba
MS
13507
13508 /* When we get some parse errors, we can end up without a
13509 current_function_decl, so cope. */
13510 if (fndecl == NULL_TREE)
13511 return;
13512
e92cc029
MS
13513 if (! nested && function_depth > 1)
13514 nested = 1;
13515
8d08fdba
MS
13516 fntype = TREE_TYPE (fndecl);
13517
13518/* TREE_READONLY (fndecl) = 1;
13519 This caused &foo to be of type ptr-to-const-function
13520 which then got a warning when stored in a ptr-to-function variable. */
13521
13522 /* This happens on strange parse errors. */
13523 if (! current_function_parms_stored)
13524 {
13525 call_poplevel = 0;
13526 store_parm_decls ();
13527 }
13528
5156628f 13529 if (processing_template_decl)
8d08fdba 13530 {
5566b478
MS
13531 if (DECL_CONSTRUCTOR_P (fndecl) && call_poplevel)
13532 {
13533 decls = getdecls ();
13534 expand_end_bindings (decls, decls != NULL_TREE, 0);
13535 poplevel (decls != NULL_TREE, 0, 0);
13536 }
13537 }
13538 else
13539 {
13540 if (write_symbols != NO_DEBUG /*&& TREE_CODE (fntype) != METHOD_TYPE*/)
8d08fdba 13541 {
5566b478
MS
13542 tree ttype = target_type (fntype);
13543 tree parmdecl;
13544
8d08fdba
MS
13545 if (IS_AGGR_TYPE (ttype))
13546 /* Let debugger know it should output info for this type. */
13547 note_debug_info_needed (ttype);
5566b478
MS
13548
13549 for (parmdecl = DECL_ARGUMENTS (fndecl); parmdecl; parmdecl = TREE_CHAIN (parmdecl))
13550 {
13551 ttype = target_type (TREE_TYPE (parmdecl));
13552 if (IS_AGGR_TYPE (ttype))
13553 /* Let debugger know it should output info for this type. */
13554 note_debug_info_needed (ttype);
13555 }
8d08fdba 13556 }
8d08fdba 13557
5566b478
MS
13558 /* Clean house because we will need to reorder insns here. */
13559 do_pending_stack_adjust ();
8d08fdba 13560
5566b478 13561 if (dtor_label)
8d08fdba 13562 {
5566b478
MS
13563 tree binfo = TYPE_BINFO (current_class_type);
13564 tree cond = integer_one_node;
13565 tree exprstmt;
13566 tree in_charge_node = lookup_name (in_charge_identifier, 0);
13567 tree virtual_size;
13568 int ok_to_optimize_dtor = 0;
13569 int empty_dtor = get_last_insn () == last_dtor_insn;
8d08fdba 13570
5566b478
MS
13571 if (current_function_assigns_this)
13572 cond = build (NE_EXPR, boolean_type_node,
4ac14744 13573 current_class_ptr, integer_zero_node);
5566b478
MS
13574 else
13575 {
13576 int n_baseclasses = CLASSTYPE_N_BASECLASSES (current_class_type);
13577
13578 /* If this destructor is empty, then we don't need to check
13579 whether `this' is NULL in some cases. */
13580 if ((flag_this_is_variable & 1) == 0)
13581 ok_to_optimize_dtor = 1;
13582 else if (empty_dtor)
13583 ok_to_optimize_dtor
13584 = (n_baseclasses == 0
13585 || (n_baseclasses == 1
13586 && TYPE_HAS_DESTRUCTOR (TYPE_BINFO_BASETYPE (current_class_type, 0))));
13587 }
8d08fdba 13588
5566b478
MS
13589 /* These initializations might go inline. Protect
13590 the binding level of the parms. */
13591 pushlevel (0);
13592 expand_start_bindings (0);
8d08fdba 13593
5566b478
MS
13594 if (current_function_assigns_this)
13595 {
13596 current_function_assigns_this = 0;
13597 current_function_just_assigned_this = 0;
13598 }
8d08fdba 13599
5566b478
MS
13600 /* Generate the code to call destructor on base class.
13601 If this destructor belongs to a class with virtual
13602 functions, then set the virtual function table
13603 pointer to represent the type of our base class. */
8d08fdba 13604
5566b478 13605 /* This side-effect makes call to `build_delete' generate the
2a2480e1
JM
13606 code we have to have at the end of this destructor.
13607 `build_delete' will set the flag again. */
5566b478 13608 TYPE_HAS_DESTRUCTOR (current_class_type) = 0;
8d08fdba 13609
5566b478
MS
13610 /* These are two cases where we cannot delegate deletion. */
13611 if (TYPE_USES_VIRTUAL_BASECLASSES (current_class_type)
13612 || TYPE_GETS_REG_DELETE (current_class_type))
4ac14744 13613 exprstmt = build_delete (current_class_type, current_class_ref, integer_zero_node,
e92cc029 13614 LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR|LOOKUP_NORMAL, 0);
5566b478 13615 else
4ac14744 13616 exprstmt = build_delete (current_class_type, current_class_ref, in_charge_node,
e92cc029 13617 LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR|LOOKUP_NORMAL, 0);
5566b478
MS
13618
13619 /* If we did not assign to this, then `this' is non-zero at
13620 the end of a destructor. As a special optimization, don't
13621 emit test if this is an empty destructor. If it does nothing,
13622 it does nothing. If it calls a base destructor, the base
13623 destructor will perform the test. */
13624
13625 if (exprstmt != error_mark_node
13626 && (TREE_CODE (exprstmt) != NOP_EXPR
13627 || TREE_OPERAND (exprstmt, 0) != integer_zero_node
13628 || TYPE_USES_VIRTUAL_BASECLASSES (current_class_type)))
8d08fdba 13629 {
5566b478
MS
13630 expand_label (dtor_label);
13631 if (cond != integer_one_node)
13632 expand_start_cond (cond, 0);
13633 if (exprstmt != void_zero_node)
13634 /* Don't call `expand_expr_stmt' if we're not going to do
13635 anything, since -Wall will give a diagnostic. */
13636 expand_expr_stmt (exprstmt);
13637
13638 /* Run destructor on all virtual baseclasses. */
13639 if (TYPE_USES_VIRTUAL_BASECLASSES (current_class_type))
8d08fdba 13640 {
5566b478
MS
13641 tree vbases = nreverse (copy_list (CLASSTYPE_VBASECLASSES (current_class_type)));
13642 expand_start_cond (build (BIT_AND_EXPR, integer_type_node,
13643 in_charge_node, integer_two_node), 0);
13644 while (vbases)
8d08fdba 13645 {
5566b478
MS
13646 if (TYPE_NEEDS_DESTRUCTOR (BINFO_TYPE (vbases)))
13647 {
bd6dd845
MS
13648 tree vb = get_vbase
13649 (BINFO_TYPE (vbases),
8ea2460f 13650 TYPE_BINFO (current_class_type));
e92cc029 13651 expand_expr_stmt
bd6dd845
MS
13652 (build_scoped_method_call
13653 (current_class_ref, vb, dtor_identifier,
e66d884e 13654 build_expr_list (NULL_TREE, integer_zero_node)));
5566b478
MS
13655 }
13656 vbases = TREE_CHAIN (vbases);
8d08fdba 13657 }
5566b478 13658 expand_end_cond ();
8d08fdba 13659 }
5566b478
MS
13660
13661 do_pending_stack_adjust ();
13662 if (cond != integer_one_node)
13663 expand_end_cond ();
8d08fdba
MS
13664 }
13665
5566b478
MS
13666 virtual_size = c_sizeof (current_class_type);
13667
13668 /* At the end, call delete if that's what's requested. */
519ebd1e
JM
13669
13670 /* FDIS sez: At the point of definition of a virtual destructor
13671 (including an implicit definition), non-placement operator
13672 delete shall be looked up in the scope of the destructor's
13673 class and if found shall be accessible and unambiguous.
13674
13675 This is somewhat unclear, but I take it to mean that if the
13676 class only defines placement deletes we don't do anything here.
13677 So we pass LOOKUP_SPECULATIVELY; delete_sanity will complain
13678 for us if they ever try to delete one of these. */
13679
13680 if (TYPE_GETS_REG_DELETE (current_class_type)
13681 || TYPE_USES_VIRTUAL_BASECLASSES (current_class_type))
13682 exprstmt = build_op_delete_call
13683 (DELETE_EXPR, current_class_ptr, virtual_size,
13684 LOOKUP_NORMAL | LOOKUP_SPECULATIVELY, NULL_TREE);
5566b478
MS
13685 else
13686 exprstmt = NULL_TREE;
8d08fdba 13687
5566b478
MS
13688 if (exprstmt)
13689 {
13690 cond = build (BIT_AND_EXPR, integer_type_node,
13691 in_charge_node, integer_one_node);
13692 expand_start_cond (cond, 0);
13693 expand_expr_stmt (exprstmt);
13694 expand_end_cond ();
13695 }
8d08fdba 13696
5566b478 13697 /* End of destructor. */
fc378698 13698 expand_end_bindings (NULL_TREE, getdecls () != NULL_TREE, 0);
536333d4 13699 poplevel (getdecls () != NULL_TREE, 0, 0);
5566b478
MS
13700
13701 /* Back to the top of destructor. */
956d6950 13702 /* Don't execute destructor code if `this' is NULL. */
5566b478
MS
13703
13704 start_sequence ();
13705
13706 /* If the dtor is empty, and we know there is not possible way we
13707 could use any vtable entries, before they are possibly set by
13708 a base class dtor, we don't have to setup the vtables, as we
13709 know that any base class dtoring will set up any vtables it
13710 needs. We avoid MI, because one base class dtor can do a
13711 virtual dispatch to an overridden function that would need to
13712 have a non-related vtable set up, we cannot avoid setting up
13713 vtables in that case. We could change this to see if there is
13714 just one vtable. */
13715 if (! empty_dtor || TYPE_USES_COMPLEX_INHERITANCE (current_class_type))
13716 {
13717 /* Make all virtual function table pointers in non-virtual base
13718 classes point to CURRENT_CLASS_TYPE's virtual function
13719 tables. */
4ac14744 13720 expand_direct_vtbls_init (binfo, binfo, 1, 0, current_class_ptr);
8d08fdba 13721
5566b478 13722 if (TYPE_USES_VIRTUAL_BASECLASSES (current_class_type))
4ac14744 13723 expand_indirect_vtbls_init (binfo, current_class_ref, current_class_ptr);
5566b478
MS
13724 }
13725
13726 if (! ok_to_optimize_dtor)
13727 {
13728 cond = build_binary_op (NE_EXPR,
4ac14744 13729 current_class_ptr, integer_zero_node, 1);
5566b478
MS
13730 expand_start_cond (cond, 0);
13731 }
8d08fdba 13732
5566b478
MS
13733 insns = get_insns ();
13734 end_sequence ();
f30432d7 13735
5566b478
MS
13736 last_parm_insn = get_first_nonparm_insn ();
13737 if (last_parm_insn == NULL_RTX)
13738 last_parm_insn = get_last_insn ();
13739 else
13740 last_parm_insn = previous_insn (last_parm_insn);
8d08fdba 13741
5566b478 13742 emit_insns_after (insns, last_parm_insn);
72b7eeff 13743
5566b478
MS
13744 if (! ok_to_optimize_dtor)
13745 expand_end_cond ();
72b7eeff 13746 }
5566b478 13747 else if (current_function_assigns_this)
8d08fdba 13748 {
5566b478
MS
13749 /* Does not need to call emit_base_init, because
13750 that is done (if needed) just after assignment to this
13751 is seen. */
13752
13753 if (DECL_CONSTRUCTOR_P (current_function_decl))
13754 {
13755 end_protect_partials ();
13756 expand_label (ctor_label);
13757 ctor_label = NULL_TREE;
13758
13759 if (call_poplevel)
13760 {
13761 decls = getdecls ();
13762 expand_end_bindings (decls, decls != NULL_TREE, 0);
13763 poplevel (decls != NULL_TREE, 0, 0);
13764 }
1c2c08a5
JM
13765 /* c_expand_return knows to return 'this' from a constructor. */
13766 c_expand_return (NULL_TREE);
5566b478 13767 }
a1774733 13768 else if (TREE_CODE (TREE_TYPE (DECL_RESULT (current_function_decl))) != VOID_TYPE
5566b478
MS
13769 && return_label != NULL_RTX)
13770 no_return_label = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE);
13771
13772 current_function_assigns_this = 0;
13773 current_function_just_assigned_this = 0;
13774 base_init_expr = NULL_TREE;
8d08fdba 13775 }
5566b478
MS
13776 else if (DECL_CONSTRUCTOR_P (fndecl))
13777 {
a703fb38 13778 tree cond = NULL_TREE, thenclause = NULL_TREE;
5566b478
MS
13779 /* Allow constructor for a type to get a new instance of the object
13780 using `build_new'. */
13781 tree abstract_virtuals = CLASSTYPE_ABSTRACT_VIRTUALS (current_class_type);
13782 CLASSTYPE_ABSTRACT_VIRTUALS (current_class_type) = NULL_TREE;
f30432d7 13783
5566b478 13784 DECL_RETURNS_FIRST_ARG (fndecl) = 1;
f30432d7 13785
5566b478
MS
13786 if (flag_this_is_variable > 0)
13787 {
13788 cond = build_binary_op (EQ_EXPR,
4ac14744
MS
13789 current_class_ptr, integer_zero_node, 1);
13790 thenclause = build_modify_expr (current_class_ptr, NOP_EXPR,
5566b478
MS
13791 build_new (NULL_TREE, current_class_type, void_type_node, 0));
13792 }
f30432d7 13793
5566b478 13794 CLASSTYPE_ABSTRACT_VIRTUALS (current_class_type) = abstract_virtuals;
f30432d7 13795
5566b478
MS
13796 start_sequence ();
13797
13798 if (flag_this_is_variable > 0)
13799 {
13800 expand_start_cond (cond, 0);
13801 expand_expr_stmt (thenclause);
13802 expand_end_cond ();
13803 }
13804
13805 /* Emit insns from `emit_base_init' which sets up virtual
13806 function table pointer(s). */
13807 if (base_init_expr)
13808 {
13809 expand_expr_stmt (base_init_expr);
13810 base_init_expr = NULL_TREE;
13811 }
13812
13813 insns = get_insns ();
13814 end_sequence ();
13815
b87692e5 13816 /* This is where the body of the constructor begins. */
5566b478 13817
b87692e5 13818 emit_insns_after (insns, last_parm_cleanup_insn);
8d08fdba 13819
b7484fbe 13820 end_protect_partials ();
5566b478
MS
13821
13822 /* This is where the body of the constructor ends. */
8d08fdba
MS
13823 expand_label (ctor_label);
13824 ctor_label = NULL_TREE;
13825
13826 if (call_poplevel)
13827 {
13828 decls = getdecls ();
8d08fdba 13829 expand_end_bindings (decls, decls != NULL_TREE, 0);
5566b478 13830 poplevel (decls != NULL_TREE, 1, 0);
8d08fdba 13831 }
8d08fdba 13832
1c2c08a5
JM
13833 /* c_expand_return knows to return 'this' from a constructor. */
13834 c_expand_return (NULL_TREE);
8d08fdba 13835
5566b478
MS
13836 current_function_assigns_this = 0;
13837 current_function_just_assigned_this = 0;
13838 }
35680744 13839 else if (DECL_MAIN_P (fndecl))
8d08fdba 13840 {
5566b478
MS
13841 /* Make it so that `main' always returns 0 by default. */
13842#ifdef VMS
13843 c_expand_return (integer_one_node);
13844#else
13845 c_expand_return (integer_zero_node);
13846#endif
8d08fdba 13847 }
5566b478
MS
13848 else if (return_label != NULL_RTX
13849 && current_function_return_value == NULL_TREE
13850 && ! DECL_NAME (DECL_RESULT (current_function_decl)))
13851 no_return_label = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE);
8d08fdba 13852
eb448459
MS
13853 if (flag_exceptions)
13854 expand_exception_blocks ();
13855
5566b478
MS
13856 /* If this function is supposed to return a value, ensure that
13857 we do not fall into the cleanups by mistake. The end of our
13858 function will look like this:
13859
13860 user code (may have return stmt somewhere)
13861 goto no_return_label
13862 cleanup_label:
13863 cleanups
13864 goto return_label
13865 no_return_label:
13866 NOTE_INSN_FUNCTION_END
13867 return_label:
13868 things for return
13869
13870 If the user omits a return stmt in the USER CODE section, we
13871 will have a control path which reaches NOTE_INSN_FUNCTION_END.
13872 Otherwise, we won't. */
13873 if (no_return_label)
8d08fdba 13874 {
5566b478
MS
13875 DECL_CONTEXT (no_return_label) = fndecl;
13876 DECL_INITIAL (no_return_label) = error_mark_node;
13877 DECL_SOURCE_FILE (no_return_label) = input_filename;
13878 DECL_SOURCE_LINE (no_return_label) = lineno;
13879 expand_goto (no_return_label);
8d08fdba
MS
13880 }
13881
5566b478 13882 if (cleanup_label)
a9aedbc2 13883 {
2c73f9f5 13884 /* Remove the binding contour which is used
5566b478
MS
13885 to catch cleanup-generated temporaries. */
13886 expand_end_bindings (0, 0, 0);
13887 poplevel (0, 0, 0);
8d08fdba 13888
eb448459
MS
13889 /* Emit label at beginning of cleanup code for parameters. */
13890 emit_label (cleanup_label);
13891 }
b7484fbe 13892
5566b478
MS
13893 /* Get return value into register if that's where it's supposed to be. */
13894 if (original_result_rtx)
13895 fixup_result_decl (DECL_RESULT (fndecl), original_result_rtx);
8d08fdba 13896
5566b478
MS
13897 /* Finish building code that will trigger warnings if users forget
13898 to make their functions return values. */
13899 if (no_return_label || cleanup_label)
13900 emit_jump (return_label);
13901 if (no_return_label)
8d08fdba 13902 {
5566b478
MS
13903 /* We don't need to call `expand_*_return' here because we
13904 don't need any cleanups here--this path of code is only
13905 for error checking purposes. */
13906 expand_label (no_return_label);
8d08fdba
MS
13907 }
13908
5566b478
MS
13909 /* Generate rtl for function exit. */
13910 expand_function_end (input_filename, lineno, 1);
8d08fdba 13911 }
f181d4ae 13912
f181d4ae
MM
13913 /* If we're processing a template, squirrel away the definition
13914 until we do an instantiation. */
13915 if (processing_template_decl)
13916 {
13917 --minimal_parse_mode;
13918 DECL_SAVED_TREE (fndecl) = TREE_CHAIN (DECL_SAVED_TREE (fndecl));
13919 /* We have to save this value here in case
13920 maybe_end_member_template_processing decides to pop all the
13921 template parameters. */
13922 in_template = 1;
13923 }
13924 else
13925 in_template = 0;
8d2733ca 13926
8d08fdba
MS
13927 /* This must come after expand_function_end because cleanups might
13928 have declarations (from inline functions) that need to go into
13929 this function's blocks. */
13930 if (current_binding_level->parm_flag != 1)
13931 my_friendly_abort (122);
13932 poplevel (1, 0, 1);
13933
f181d4ae
MM
13934 /* If this is a in-class inline definition, we may have to pop the
13935 bindings for the template parameters that we added in
13936 maybe_begin_member_template_processing when start_function was
13937 called. */
13938 if (inclass_inline)
13939 maybe_end_member_template_processing ();
13940
2c73f9f5 13941 /* Reset scope for C++: if we were in the scope of a class,
db5ae43f
MS
13942 then when we finish this function, we are not longer so.
13943 This cannot be done until we know for sure that no more
13944 class members will ever be referenced in this function
13945 (i.e., calls to destructors). */
13946 if (current_class_name)
13947 {
13948 ctype = current_class_type;
13949 pop_nested_class (1);
13950 }
db5ae43f 13951
1caa11d3
MM
13952 /* Must mark the RESULT_DECL as being in this function. */
13953 DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
13954
13955 /* Set the BLOCK_SUPERCONTEXT of the outermost function scope to point
13956 to the FUNCTION_DECL node itself. */
13957 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
13958
f181d4ae 13959 if (!in_template)
5566b478 13960 {
75650646
MM
13961 int saved_flag_keep_inline_functions =
13962 flag_keep_inline_functions;
13963
5566b478
MS
13964 /* So we can tell if jump_optimize sets it to 1. */
13965 can_reach_end = 0;
8d08fdba 13966
75650646 13967 if (DECL_CONTEXT (fndecl) != NULL_TREE
e1467ff2 13968 && hack_decl_function_context (fndecl))
75650646
MM
13969 /* Trick rest_of_compilation into not deferring output of this
13970 function, even if it is inline, since the rtl_obstack for
13971 this function is the function_obstack of the enclosing
13972 function and will be deallocated when the enclosing
13973 function is gone. See save_tree_status. */
13974 flag_keep_inline_functions = 1;
13975
6633d636
MS
13976 /* Run the optimizers and output the assembler code for this
13977 function. */
13978
13979 if (DECL_ARTIFICIAL (fndecl))
13980 {
13981 /* Do we really *want* to inline this synthesized method? */
13982
13983 int save_fif = flag_inline_functions;
13984 flag_inline_functions = 1;
13985
13986 /* Turn off DECL_INLINE for the moment so function_cannot_inline_p
13987 will check our size. */
13988 DECL_INLINE (fndecl) = 0;
13989
13990 rest_of_compilation (fndecl);
13991 flag_inline_functions = save_fif;
13992 }
13993 else
13994 rest_of_compilation (fndecl);
8d08fdba 13995
75650646
MM
13996 flag_keep_inline_functions = saved_flag_keep_inline_functions;
13997
5566b478
MS
13998 if (DECL_SAVED_INSNS (fndecl) && ! TREE_ASM_WRITTEN (fndecl))
13999 {
14000 /* Set DECL_EXTERNAL so that assemble_external will be called as
14001 necessary. We'll clear it again in finish_file. */
14002 if (! DECL_EXTERNAL (fndecl))
14003 DECL_NOT_REALLY_EXTERN (fndecl) = 1;
14004 DECL_EXTERNAL (fndecl) = 1;
14005 mark_inline_for_output (fndecl);
14006 }
8926095f 14007
d2e5ee5c
MS
14008 if (ctype && TREE_ASM_WRITTEN (fndecl))
14009 note_debug_info_needed (ctype);
14010
5566b478 14011 current_function_returns_null |= can_reach_end;
8d08fdba 14012
5566b478
MS
14013 /* Since we don't normally go through c_expand_return for constructors,
14014 this normally gets the wrong value.
14015 Also, named return values have their return codes emitted after
14016 NOTE_INSN_FUNCTION_END, confusing jump.c. */
14017 if (DECL_CONSTRUCTOR_P (fndecl)
14018 || DECL_NAME (DECL_RESULT (fndecl)) != NULL_TREE)
14019 current_function_returns_null = 0;
8d08fdba 14020
5566b478 14021 if (TREE_THIS_VOLATILE (fndecl) && current_function_returns_null)
8251199e 14022 cp_warning ("`noreturn' function `%D' does return", fndecl);
5566b478
MS
14023 else if ((warn_return_type || pedantic)
14024 && current_function_returns_null
a1774733 14025 && TREE_CODE (TREE_TYPE (fntype)) != VOID_TYPE)
5566b478
MS
14026 {
14027 /* If this function returns non-void and control can drop through,
14028 complain. */
8251199e 14029 cp_warning ("control reaches end of non-void function `%D'", fndecl);
5566b478
MS
14030 }
14031 /* With just -W, complain only if function returns both with
14032 and without a value. */
14033 else if (extra_warnings
14034 && current_function_returns_value && current_function_returns_null)
8251199e 14035 warning ("this function may return with or without a value");
8d08fdba 14036 }
5566b478
MS
14037
14038 --function_depth;
8d08fdba
MS
14039
14040 /* Free all the tree nodes making up this function. */
14041 /* Switch back to allocating nodes permanently
14042 until we start another function. */
db5ae43f
MS
14043 if (! nested)
14044 permanent_allocation (1);
8d08fdba 14045
8d08fdba
MS
14046 if (DECL_SAVED_INSNS (fndecl) == NULL_RTX)
14047 {
d2e5ee5c
MS
14048 tree t;
14049
8d08fdba
MS
14050 /* Stop pointing to the local nodes about to be freed. */
14051 /* But DECL_INITIAL must remain nonzero so we know this
14052 was an actual function definition. */
14053 DECL_INITIAL (fndecl) = error_mark_node;
d2e5ee5c 14054 for (t = DECL_ARGUMENTS (fndecl); t; t = TREE_CHAIN (t))
de22184b 14055 DECL_RTL (t) = DECL_INCOMING_RTL (t) = NULL_RTX;
8d08fdba
MS
14056 }
14057
e1cd6e56
MS
14058 if (DECL_STATIC_CONSTRUCTOR (fndecl))
14059 static_ctors = perm_tree_cons (NULL_TREE, fndecl, static_ctors);
14060 if (DECL_STATIC_DESTRUCTOR (fndecl))
14061 static_dtors = perm_tree_cons (NULL_TREE, fndecl, static_dtors);
14062
28cbf42c
MS
14063 if (! nested)
14064 {
14065 /* Let the error reporting routines know that we're outside a
14066 function. For a nested function, this value is used in
14067 pop_cp_function_context and then reset via pop_function_context. */
14068 current_function_decl = NULL_TREE;
14069 }
14070
e349ee73 14071 named_label_uses = NULL;
4ac14744
MS
14072 current_class_ptr = NULL_TREE;
14073 current_class_ref = NULL_TREE;
8d08fdba
MS
14074}
14075\f
14076/* Create the FUNCTION_DECL for a function definition.
8d08fdba
MS
14077 DECLSPECS and DECLARATOR are the parts of the declaration;
14078 they describe the return type and the name of the function,
14079 but twisted together in a fashion that parallels the syntax of C.
14080
14081 This function creates a binding context for the function body
14082 as well as setting up the FUNCTION_DECL in current_function_decl.
14083
14084 Returns a FUNCTION_DECL on success.
14085
14086 If the DECLARATOR is not suitable for a function (it defines a datum
14087 instead), we return 0, which tells yyparse to report a parse error.
14088
14089 May return void_type_node indicating that this method is actually
14090 a friend. See grokfield for more details.
14091
14092 Came here with a `.pushlevel' .
14093
14094 DO NOT MAKE ANY CHANGES TO THIS CODE WITHOUT MAKING CORRESPONDING
14095 CHANGES TO CODE IN `grokfield'. */
e92cc029 14096
8d08fdba 14097tree
acf82af2
JM
14098start_method (declspecs, declarator, attrlist)
14099 tree declarator, declspecs, attrlist;
8d08fdba 14100{
c11b6f21 14101 tree fndecl = grokdeclarator (declarator, declspecs, MEMFUNCDEF, 0,
acf82af2 14102 attrlist);
8d08fdba
MS
14103
14104 /* Something too ugly to handle. */
14105 if (fndecl == NULL_TREE)
14106 return NULL_TREE;
14107
14108 /* Pass friends other than inline friend functions back. */
a1774733 14109 if (fndecl == void_type_node)
8d08fdba
MS
14110 return fndecl;
14111
14112 if (TREE_CODE (fndecl) != FUNCTION_DECL)
14113 /* Not a function, tell parser to report parse error. */
14114 return NULL_TREE;
14115
14116 if (IS_SIGNATURE (current_class_type))
824b9a4c 14117 IS_DEFAULT_IMPLEMENTATION (fndecl) = 1;
8d08fdba
MS
14118
14119 if (DECL_IN_AGGR_P (fndecl))
14120 {
14121 if (IDENTIFIER_ERROR_LOCUS (DECL_ASSEMBLER_NAME (fndecl)) != current_class_type)
14122 {
2c73f9f5
ML
14123 if (DECL_CONTEXT (fndecl)
14124 && TREE_CODE( DECL_CONTEXT (fndecl)) != NAMESPACE_DECL)
8251199e
JM
14125 cp_error ("`%D' is already defined in class %s", fndecl,
14126 TYPE_NAME_STRING (DECL_CONTEXT (fndecl)));
8d08fdba
MS
14127 }
14128 return void_type_node;
14129 }
14130
f3400fe2
JM
14131 check_template_shadow (fndecl);
14132
faae18ab
MS
14133 DECL_THIS_INLINE (fndecl) = 1;
14134
8926095f 14135 if (flag_default_inline)
8d08fdba
MS
14136 DECL_INLINE (fndecl) = 1;
14137
36a117a5
MM
14138 /* We process method specializations in finish_struct_1. */
14139 if (processing_template_decl && !DECL_TEMPLATE_SPECIALIZATION (fndecl))
3ac3d9ea 14140 fndecl = push_template_decl (fndecl);
a0a33927 14141
8d08fdba
MS
14142 /* We read in the parameters on the maybepermanent_obstack,
14143 but we won't be getting back to them until after we
14144 may have clobbered them. So the call to preserve_data
14145 will keep them safe. */
14146 preserve_data ();
14147
14148 if (! DECL_FRIEND_P (fndecl))
14149 {
8d08fdba
MS
14150 if (TREE_CHAIN (fndecl))
14151 {
14152 fndecl = copy_node (fndecl);
14153 TREE_CHAIN (fndecl) = NULL_TREE;
14154 }
14155
14156 if (DECL_CONSTRUCTOR_P (fndecl))
a0a33927
MS
14157 {
14158 if (! grok_ctor_properties (current_class_type, fndecl))
14159 return void_type_node;
14160 }
8d08fdba
MS
14161 else if (IDENTIFIER_OPNAME_P (DECL_NAME (fndecl)))
14162 grok_op_properties (fndecl, DECL_VIRTUAL_P (fndecl), 0);
14163 }
14164
82580166 14165 cp_finish_decl (fndecl, NULL_TREE, NULL_TREE, 0, 0);
8d08fdba
MS
14166
14167 /* Make a place for the parms */
14168 pushlevel (0);
14169 current_binding_level->parm_flag = 1;
14170
14171 DECL_IN_AGGR_P (fndecl) = 1;
14172 return fndecl;
14173}
14174
14175/* Go through the motions of finishing a function definition.
14176 We don't compile this method until after the whole class has
14177 been processed.
14178
14179 FINISH_METHOD must return something that looks as though it
14180 came from GROKFIELD (since we are defining a method, after all).
14181
14182 This is called after parsing the body of the function definition.
14183 STMTS is the chain of statements that makes up the function body.
14184
14185 DECL is the ..._DECL that `start_method' provided. */
14186
14187tree
14188finish_method (decl)
14189 tree decl;
14190{
14191 register tree fndecl = decl;
14192 tree old_initial;
8d08fdba
MS
14193
14194 register tree link;
14195
a1774733 14196 if (decl == void_type_node)
8d08fdba
MS
14197 return decl;
14198
14199 old_initial = DECL_INITIAL (fndecl);
14200
14201 /* Undo the level for the parms (from start_method).
14202 This is like poplevel, but it causes nothing to be
14203 saved. Saving information here confuses symbol-table
14204 output routines. Besides, this information will
14205 be correctly output when this method is actually
14206 compiled. */
14207
14208 /* Clear out the meanings of the local variables of this level;
14209 also record in each decl which block it belongs to. */
14210
14211 for (link = current_binding_level->names; link; link = TREE_CHAIN (link))
14212 {
14213 if (DECL_NAME (link) != NULL_TREE)
d8f8dca1 14214 pop_binding (DECL_NAME (link), link);
8d08fdba
MS
14215 my_friendly_assert (TREE_CODE (link) != FUNCTION_DECL, 163);
14216 DECL_CONTEXT (link) = NULL_TREE;
14217 }
14218
8d08fdba
MS
14219 GNU_xref_end_scope ((HOST_WIDE_INT) current_binding_level,
14220 (HOST_WIDE_INT) current_binding_level->level_chain,
14221 current_binding_level->parm_flag,
5566b478 14222 current_binding_level->keep);
8d08fdba
MS
14223
14224 poplevel (0, 0, 0);
14225
14226 DECL_INITIAL (fndecl) = old_initial;
14227
14228 /* We used to check if the context of FNDECL was different from
14229 current_class_type as another way to get inside here. This didn't work
14230 for String.cc in libg++. */
14231 if (DECL_FRIEND_P (fndecl))
14232 {
14233 CLASSTYPE_INLINE_FRIENDS (current_class_type)
14234 = tree_cons (NULL_TREE, fndecl, CLASSTYPE_INLINE_FRIENDS (current_class_type));
14235 decl = void_type_node;
14236 }
14237
14238 return decl;
14239}
14240\f
14241/* Called when a new struct TYPE is defined.
14242 If this structure or union completes the type of any previous
14243 variable declaration, lay it out and output its rtl. */
14244
14245void
14246hack_incomplete_structures (type)
14247 tree type;
14248{
f30432d7 14249 tree *list;
8d08fdba 14250
f30432d7 14251 if (current_binding_level->incomplete == NULL_TREE)
8d08fdba
MS
14252 return;
14253
14254 if (!type) /* Don't do this for class templates. */
14255 return;
14256
f30432d7
MS
14257 for (list = &current_binding_level->incomplete; *list; )
14258 {
14259 tree decl = TREE_VALUE (*list);
a703fb38 14260 if ((decl && TREE_TYPE (decl) == type)
f30432d7
MS
14261 || (TREE_TYPE (decl)
14262 && TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
14263 && TREE_TYPE (TREE_TYPE (decl)) == type))
14264 {
14265 int toplevel = toplevel_bindings_p ();
14266 if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
14267 && TREE_TYPE (TREE_TYPE (decl)) == type)
14268 layout_type (TREE_TYPE (decl));
14269 layout_decl (decl, 0);
14270 rest_of_decl_compilation (decl, NULL_PTR, toplevel, 0);
14271 if (! toplevel)
14272 {
14273 tree cleanup;
14274 expand_decl (decl);
14275 cleanup = maybe_build_cleanup (decl);
14276 expand_decl_init (decl);
e349ee73 14277 if (! expand_decl_cleanup (decl, cleanup))
8251199e 14278 cp_error ("parser lost in parsing declaration of `%D'",
f30432d7
MS
14279 decl);
14280 }
14281 *list = TREE_CHAIN (*list);
14282 }
14283 else
14284 list = &TREE_CHAIN (*list);
14285 }
8d08fdba
MS
14286}
14287
8d08fdba 14288/* If DECL is of a type which needs a cleanup, build that cleanup here.
c73964b2 14289 See build_delete for information about AUTO_DELETE.
8d08fdba
MS
14290
14291 Don't build these on the momentary obstack; they must live
14292 the life of the binding contour. */
e92cc029 14293
c73964b2
MS
14294static tree
14295maybe_build_cleanup_1 (decl, auto_delete)
14296 tree decl, auto_delete;
8d08fdba
MS
14297{
14298 tree type = TREE_TYPE (decl);
14299 if (TYPE_NEEDS_DESTRUCTOR (type))
14300 {
14301 int temp = 0, flags = LOOKUP_NORMAL|LOOKUP_DESTRUCTOR;
14302 tree rval;
8d08fdba
MS
14303
14304 if (TREE_CODE (decl) != PARM_DECL)
14305 temp = suspend_momentary ();
14306
14307 if (TREE_CODE (type) == ARRAY_TYPE)
14308 rval = decl;
14309 else
14310 {
14311 mark_addressable (decl);
14312 rval = build_unary_op (ADDR_EXPR, decl, 0);
14313 }
14314
14315 /* Optimize for space over speed here. */
14316 if (! TYPE_USES_VIRTUAL_BASECLASSES (type)
14317 || flag_expensive_optimizations)
14318 flags |= LOOKUP_NONVIRTUAL;
14319
c73964b2 14320 rval = build_delete (TREE_TYPE (rval), rval, auto_delete, flags, 0);
8d08fdba
MS
14321
14322 if (TYPE_USES_VIRTUAL_BASECLASSES (type)
14323 && ! TYPE_HAS_DESTRUCTOR (type))
e66d884e
JM
14324 rval = build_compound_expr (expr_tree_cons (NULL_TREE, rval,
14325 build_expr_list (NULL_TREE, build_vbase_delete (type, decl))));
8d08fdba 14326
8d08fdba
MS
14327 if (TREE_CODE (decl) != PARM_DECL)
14328 resume_momentary (temp);
14329
8d08fdba
MS
14330 return rval;
14331 }
14332 return 0;
14333}
c73964b2
MS
14334
14335/* If DECL is of a type which needs a cleanup, build that cleanup
14336 here. The cleanup does free the storage with a call to delete. */
14337
14338tree
14339maybe_build_cleanup_and_delete (decl)
14340 tree decl;
14341{
14342 return maybe_build_cleanup_1 (decl, integer_three_node);
14343}
14344
14345/* If DECL is of a type which needs a cleanup, build that cleanup
14346 here. The cleanup does not free the storage with a call a delete. */
14347
14348tree
14349maybe_build_cleanup (decl)
14350 tree decl;
14351{
14352 return maybe_build_cleanup_1 (decl, integer_two_node);
14353}
8d08fdba
MS
14354\f
14355/* Expand a C++ expression at the statement level.
14356 This is needed to ferret out nodes which have UNKNOWN_TYPE.
14357 The C++ type checker should get all of these out when
14358 expressions are combined with other, type-providing, expressions,
14359 leaving only orphan expressions, such as:
14360
e92cc029 14361 &class::bar; / / takes its address, but does nothing with it. */
8d08fdba 14362
8d08fdba
MS
14363void
14364cplus_expand_expr_stmt (exp)
14365 tree exp;
14366{
5156628f 14367 if (processing_template_decl)
5566b478
MS
14368 {
14369 add_tree (build_min_nt (EXPR_STMT, exp));
14370 return;
14371 }
14372
eb66be0e
MS
14373 /* Arrange for all temps to disappear. */
14374 expand_start_target_temps ();
14375
8d08fdba
MS
14376 if (TREE_TYPE (exp) == unknown_type_node)
14377 {
59e76fc6 14378 if (TREE_CODE (exp) == COMPONENT_REF)
848b92e1 14379 error ("invalid reference to a member function name, did you forget the ()?");
59e76fc6
JM
14380 else
14381 error ("address of overloaded function with no contextual type information");
8d08fdba
MS
14382 }
14383 else
14384 {
8d08fdba
MS
14385 if (TREE_CODE (exp) == FUNCTION_DECL)
14386 {
8251199e
JM
14387 cp_warning ("reference, not call, to function `%D'", exp);
14388 warning ("at this point in file");
8d08fdba 14389 }
8d08fdba 14390
63c68bb7
MS
14391#if 0
14392 /* We should do this eventually, but right now this causes regex.o from
14393 libg++ to miscompile, and tString to core dump. */
5b605f68 14394 exp = build1 (CLEANUP_POINT_EXPR, TREE_TYPE (exp), exp);
63c68bb7 14395#endif
b3e401c2
JM
14396
14397 /* Strip unused implicit INDIRECT_REFs of references. */
14398 if (TREE_CODE (exp) == INDIRECT_REF
14399 && TREE_CODE (TREE_TYPE (TREE_OPERAND (exp, 0))) == REFERENCE_TYPE)
14400 exp = TREE_OPERAND (exp, 0);
14401
d2e5ee5c
MS
14402 /* If we don't do this, we end up down inside expand_expr
14403 trying to do TYPE_MODE on the ERROR_MARK, and really
14404 go outside the bounds of the type. */
14405 if (exp != error_mark_node)
14406 expand_expr_stmt (break_out_cleanups (exp));
8d08fdba
MS
14407 }
14408
14409 /* Clean up any pending cleanups. This happens when a function call
14410 returns a cleanup-needing value that nobody uses. */
eb66be0e 14411 expand_end_target_temps ();
8d08fdba
MS
14412}
14413
14414/* When a stmt has been parsed, this function is called.
14415
14416 Currently, this function only does something within a
14417 constructor's scope: if a stmt has just assigned to this,
14418 and we are in a derived class, we call `emit_base_init'. */
14419
14420void
14421finish_stmt ()
14422{
14423 extern struct nesting *cond_stack, *loop_stack, *case_stack;
14424
14425
14426 if (current_function_assigns_this
14427 || ! current_function_just_assigned_this)
14428 return;
14429 if (DECL_CONSTRUCTOR_P (current_function_decl))
14430 {
14431 /* Constructors must wait until we are out of control
14432 zones before calling base constructors. */
14433 if (cond_stack || loop_stack || case_stack)
14434 return;
a9aedbc2 14435 expand_expr_stmt (base_init_expr);
8d08fdba
MS
14436 check_base_init (current_class_type);
14437 }
14438 current_function_assigns_this = 1;
8d08fdba
MS
14439}
14440
8d08fdba 14441/* Change a static member function definition into a FUNCTION_TYPE, instead
700f8a87
MS
14442 of the METHOD_TYPE that we create when it's originally parsed.
14443
14444 WARNING: DO NOT pass &TREE_TYPE (decl) to FN or &TYPE_ARG_TYPES
14445 (TREE_TYPE (decl)) to ARGTYPES, as doing so will corrupt the types of
14446 other decls. Either pass the addresses of local variables or NULL. */
14447
8857f91e 14448void
700f8a87
MS
14449revert_static_member_fn (decl, fn, argtypes)
14450 tree *decl, *fn, *argtypes;
8d08fdba 14451{
700f8a87
MS
14452 tree tmp;
14453 tree function = fn ? *fn : TREE_TYPE (*decl);
14454 tree args = argtypes ? *argtypes : TYPE_ARG_TYPES (function);
8d08fdba 14455
91063b51
MM
14456 if (CP_TYPE_QUALS (TREE_TYPE (TREE_VALUE (args)))
14457 != TYPE_UNQUALIFIED)
14458 cp_error ("static member function `%#D' declared with type qualifiers",
14459 *decl);
f30432d7 14460
700f8a87
MS
14461 args = TREE_CHAIN (args);
14462 tmp = build_function_type (TREE_TYPE (function), args);
91063b51 14463 tmp = build_qualified_type (tmp, CP_TYPE_QUALS (function));
f30432d7 14464 tmp = build_exception_variant (tmp,
8d08fdba
MS
14465 TYPE_RAISES_EXCEPTIONS (function));
14466 TREE_TYPE (*decl) = tmp;
f30432d7
MS
14467 if (DECL_ARGUMENTS (*decl))
14468 DECL_ARGUMENTS (*decl) = TREE_CHAIN (DECL_ARGUMENTS (*decl));
8d08fdba 14469 DECL_STATIC_FUNCTION_P (*decl) = 1;
700f8a87
MS
14470 if (fn)
14471 *fn = tmp;
14472 if (argtypes)
14473 *argtypes = args;
8d08fdba 14474}
a4443a08
MS
14475
14476int
14477id_in_current_class (id)
14478 tree id;
14479{
14480 return !!purpose_member (id, class_binding_level->class_shadowed);
14481}
db5ae43f
MS
14482
14483struct cp_function
14484{
14485 int returns_value;
14486 int returns_null;
db5ae43f
MS
14487 int assigns_this;
14488 int just_assigned_this;
14489 int parms_stored;
7215f9a0 14490 int temp_name_counter;
db5ae43f 14491 tree named_labels;
5524676d 14492 struct named_label_list *named_label_uses;
db5ae43f
MS
14493 tree shadowed_labels;
14494 tree ctor_label;
14495 tree dtor_label;
72b7eeff 14496 rtx last_dtor_insn;
b87692e5 14497 rtx last_parm_cleanup_insn;
79ff2c6c
MS
14498 tree base_init_list;
14499 tree member_init_list;
a9aedbc2 14500 tree base_init_expr;
4ac14744
MS
14501 tree current_class_ptr;
14502 tree current_class_ref;
db5ae43f
MS
14503 rtx result_rtx;
14504 struct cp_function *next;
14505 struct binding_level *binding_level;
42976354 14506 int static_labelno;
db5ae43f
MS
14507};
14508
bd6dd845 14509static struct cp_function *cp_function_chain;
db5ae43f 14510
7215f9a0
MS
14511extern int temp_name_counter;
14512
db5ae43f
MS
14513/* Save and reinitialize the variables
14514 used during compilation of a C++ function. */
14515
14516void
28cbf42c
MS
14517push_cp_function_context (context)
14518 tree context;
db5ae43f
MS
14519{
14520 struct cp_function *p
14521 = (struct cp_function *) xmalloc (sizeof (struct cp_function));
14522
28cbf42c 14523 push_function_context_to (context);
db5ae43f
MS
14524
14525 p->next = cp_function_chain;
14526 cp_function_chain = p;
14527
14528 p->named_labels = named_labels;
5524676d 14529 p->named_label_uses = named_label_uses;
db5ae43f
MS
14530 p->shadowed_labels = shadowed_labels;
14531 p->returns_value = current_function_returns_value;
14532 p->returns_null = current_function_returns_null;
db5ae43f
MS
14533 p->binding_level = current_binding_level;
14534 p->ctor_label = ctor_label;
14535 p->dtor_label = dtor_label;
72b7eeff 14536 p->last_dtor_insn = last_dtor_insn;
b87692e5 14537 p->last_parm_cleanup_insn = last_parm_cleanup_insn;
db5ae43f
MS
14538 p->assigns_this = current_function_assigns_this;
14539 p->just_assigned_this = current_function_just_assigned_this;
14540 p->parms_stored = current_function_parms_stored;
14541 p->result_rtx = original_result_rtx;
a9aedbc2 14542 p->base_init_expr = base_init_expr;
7215f9a0 14543 p->temp_name_counter = temp_name_counter;
79ff2c6c
MS
14544 p->base_init_list = current_base_init_list;
14545 p->member_init_list = current_member_init_list;
4ac14744
MS
14546 p->current_class_ptr = current_class_ptr;
14547 p->current_class_ref = current_class_ref;
42976354 14548 p->static_labelno = static_labelno;
db5ae43f
MS
14549}
14550
14551/* Restore the variables used during compilation of a C++ function. */
14552
14553void
28cbf42c
MS
14554pop_cp_function_context (context)
14555 tree context;
db5ae43f
MS
14556{
14557 struct cp_function *p = cp_function_chain;
14558 tree link;
14559
14560 /* Bring back all the labels that were shadowed. */
14561 for (link = shadowed_labels; link; link = TREE_CHAIN (link))
14562 if (DECL_NAME (TREE_VALUE (link)) != 0)
14563 SET_IDENTIFIER_LABEL_VALUE (DECL_NAME (TREE_VALUE (link)),
14564 TREE_VALUE (link));
14565
28cbf42c 14566 pop_function_context_from (context);
db5ae43f
MS
14567
14568 cp_function_chain = p->next;
14569
14570 named_labels = p->named_labels;
5524676d 14571 named_label_uses = p->named_label_uses;
db5ae43f
MS
14572 shadowed_labels = p->shadowed_labels;
14573 current_function_returns_value = p->returns_value;
14574 current_function_returns_null = p->returns_null;
db5ae43f
MS
14575 current_binding_level = p->binding_level;
14576 ctor_label = p->ctor_label;
14577 dtor_label = p->dtor_label;
72b7eeff 14578 last_dtor_insn = p->last_dtor_insn;
b87692e5 14579 last_parm_cleanup_insn = p->last_parm_cleanup_insn;
db5ae43f
MS
14580 current_function_assigns_this = p->assigns_this;
14581 current_function_just_assigned_this = p->just_assigned_this;
14582 current_function_parms_stored = p->parms_stored;
14583 original_result_rtx = p->result_rtx;
a9aedbc2 14584 base_init_expr = p->base_init_expr;
7215f9a0 14585 temp_name_counter = p->temp_name_counter;
79ff2c6c
MS
14586 current_base_init_list = p->base_init_list;
14587 current_member_init_list = p->member_init_list;
4ac14744
MS
14588 current_class_ptr = p->current_class_ptr;
14589 current_class_ref = p->current_class_ref;
42976354 14590 static_labelno = p->static_labelno;
db5ae43f
MS
14591
14592 free (p);
14593}
ebfc180f 14594
5566b478
MS
14595int
14596in_function_p ()
14597{
14598 return function_depth != 0;
14599}
This page took 3.336116 seconds and 5 git commands to generate.