]> gcc.gnu.org Git - gcc.git/blame - gcc/cp/decl.c
arm.c (arm_override_options): Set arm_constant_limit to 2 instead of 1 when optimize_...
[gcc.git] / gcc / cp / decl.c
CommitLineData
3fd5abcf 1/* Process declarations and variables for C++ compiler.
d363e7bf 2 Copyright (C) 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
3fd5abcf 3 2001, 2002, 2003 Free Software Foundation, Inc.
e5e809f4 4 Contributed by Michael Tiemann (tiemann@cygnus.com)
8d08fdba 5
1c313945 6This file is part of GCC.
8d08fdba 7
1c313945 8GCC is free software; you can redistribute it and/or modify
8d08fdba
MS
9it under the terms of the GNU General Public License as published by
10the Free Software Foundation; either version 2, or (at your option)
11any later version.
12
1c313945 13GCC is distributed in the hope that it will be useful,
8d08fdba
MS
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16GNU General Public License for more details.
17
18You should have received a copy of the GNU General Public License
1c313945 19along with GCC; see the file COPYING. If not, write to
e9fa0c7c
RK
20the Free Software Foundation, 59 Temple Place - Suite 330,
21Boston, MA 02111-1307, USA. */
8d08fdba
MS
22
23
1c313945 24/* Process declarations and symbol lookup for C++ front end.
8d08fdba
MS
25 Also constructs types; the standard scalar types at initialization,
26 and structure, union, array and enum types when they are declared. */
27
28/* ??? not all decl nodes are given the most useful possible
29 line numbers. For example, the CONST_DECLs for enum values. */
30
8d08fdba 31#include "config.h"
8d052bc7 32#include "system.h"
4977bab6
ZW
33#include "coretypes.h"
34#include "tm.h"
8d08fdba
MS
35#include "tree.h"
36#include "rtl.h"
3bdf5ad1 37#include "expr.h"
8d08fdba
MS
38#include "flags.h"
39#include "cp-tree.h"
25af8512 40#include "tree-inline.h"
8d08fdba
MS
41#include "decl.h"
42#include "lex.h"
49c249e1
JM
43#include "output.h"
44#include "except.h"
54f92bfb 45#include "toplev.h"
e2500fed 46#include "hashtab.h"
0e9295cf 47#include "tm_p.h"
672a6f42 48#include "target.h"
26f943fd 49#include "c-common.h"
ecb0eece 50#include "c-pragma.h"
7437519c 51#include "diagnostic.h"
e2500fed 52#include "debug.h"
22ffcc6f 53#include "timevar.h"
8d08fdba 54
11f6b451
NN
55static tree grokparms (tree);
56static const char *redeclaration_error_message (tree, tree);
57
11f6b451 58static int decl_jump_unsafe (tree);
11f6b451
NN
59static void require_complete_types_for_parms (tree);
60static int ambi_op_p (enum tree_code);
61static int unary_op_p (enum tree_code);
11f6b451 62static void push_local_name (tree);
7e99327d 63static tree grok_reference_init (tree, tree, tree, tree *);
11f6b451
NN
64static tree grokfndecl (tree, tree, tree, tree, int,
65 enum overload_flags, tree,
66 tree, int, int, int, int, int, int, tree);
67static tree grokvardecl (tree, tree, RID_BIT_TYPE *, int, int, tree);
11f6b451
NN
68static void record_unknown_type (tree, const char *);
69static tree builtin_function_1 (const char *, tree, tree, int,
70 enum built_in_class, const char *,
71 tree);
72static tree build_library_fn_1 (tree, enum tree_code, tree);
73static int member_function_or_else (tree, tree, enum overload_flags);
74static void bad_specifiers (tree, const char *, int, int, int, int,
75 int);
11f6b451
NN
76static void check_for_uninitialized_const_var (tree);
77static hashval_t typename_hash (const void *);
78static int typename_compare (const void *, const void *);
11f6b451 79static tree local_variable_p_walkfn (tree *, int *, void *);
11f6b451
NN
80static tree record_builtin_java_type (const char *, int);
81static const char *tag_name (enum tag_types code);
11f6b451
NN
82static int walk_namespaces_r (tree, walk_namespaces_fn, void *);
83static int walk_globals_r (tree, void*);
84static int walk_vtables_r (tree, void*);
11f6b451
NN
85static tree make_label_decl (tree, int);
86static void use_label (tree);
87static void check_previous_goto_1 (tree, struct cp_binding_level *, tree,
152c16a9 88 const location_t *);
11f6b451
NN
89static void check_previous_goto (struct named_label_use_list *);
90static void check_switch_goto (struct cp_binding_level *);
91static void check_previous_gotos (tree);
92static void pop_label (tree, tree);
93static void pop_labels (tree);
94static void maybe_deduce_size_from_array_init (tree, tree);
95static void layout_var_decl (tree);
96static void maybe_commonize_var (tree);
7e99327d 97static tree check_initializer (tree, tree, int, tree *);
11f6b451
NN
98static void make_rtl_for_nonlocal_decl (tree, tree, const char *);
99static void save_function_data (tree);
100static void check_function_type (tree, tree);
101static void begin_constructor_body (void);
102static void finish_constructor_body (void);
103static void begin_destructor_body (void);
104static void finish_destructor_body (void);
105static tree create_array_type_for_decl (tree, tree, tree);
106static tree get_atexit_node (void);
107static tree get_dso_handle_node (void);
108static tree start_cleanup_fn (void);
109static void end_cleanup_fn (void);
110static tree cp_make_fname_decl (tree, int);
111static void initialize_predefined_identifiers (void);
112static tree check_special_function_return_type
113 (special_function_kind, tree, tree);
114static tree push_cp_library_fn (enum tree_code, tree);
115static tree build_cp_library_fn (tree, enum tree_code, tree);
116static void store_parm_decls (tree);
117static int cp_missing_noreturn_ok_p (tree);
8e3df2de
MM
118static void initialize_local_var (tree, tree);
119static void expand_static_init (tree, tree);
120static tree next_initializable_field (tree);
121static tree reshape_init (tree, tree *);
8fbc5ae7 122static tree build_typename_type (tree, tree, tree);
8d08fdba 123
8d08fdba
MS
124/* Erroneous argument lists can use this *IFF* they do not modify it. */
125tree error_mark_list;
126
7f4edbcb 127/* The following symbols are subsumed in the cp_global_trees array, and
68642fb6 128 listed here individually for documentation purposes.
8d08fdba 129
7f4edbcb
BS
130 C++ extensions
131 tree wchar_decl_node;
37c46b43 132
7f4edbcb
BS
133 tree vtable_entry_type;
134 tree delta_type_node;
7f4edbcb 135 tree __t_desc_type_node;
db1147b2 136 tree ti_desc_type_node;
1f4cb92b 137 tree bltn_desc_type_node, ptr_desc_type_node;
db1147b2
NS
138 tree ary_desc_type_node, func_desc_type_node, enum_desc_type_node;
139 tree class_desc_type_node, si_class_desc_type_node, vmi_class_desc_type_node;
387769ed 140 tree ptm_desc_type_node;
db1147b2 141 tree base_desc_type_node;
8d08fdba 142
7f4edbcb
BS
143 tree class_type_node, record_type_node, union_type_node, enum_type_node;
144 tree unknown_type_node;
8d08fdba 145
7f4edbcb 146 Array type `vtable_entry_type[]'
8d08fdba 147
7f4edbcb
BS
148 tree vtbl_type_node;
149 tree vtbl_ptr_type_node;
8d08fdba 150
2854d3c6 151 Namespaces,
8d08fdba 152
7f4edbcb 153 tree std_node;
2854d3c6 154 tree abi_node;
8d08fdba 155
7f4edbcb
BS
156 A FUNCTION_DECL which can call `abort'. Not necessarily the
157 one that the user will declare, but sufficient to be called
158 by routines that want to abort the program.
8d08fdba 159
7f4edbcb 160 tree abort_fndecl;
8d08fdba 161
7f4edbcb 162 The FUNCTION_DECL for the default `::operator delete'.
2986ae00 163
7f4edbcb 164 tree global_delete_fndecl;
8d08fdba 165
7f4edbcb 166 Used by RTTI
669ec2b4
JM
167 tree type_info_type_node, tinfo_decl_id, tinfo_decl_type;
168 tree tinfo_var_id;
169
7f4edbcb 170*/
8d08fdba 171
7f4edbcb 172tree cp_global_trees[CPTI_MAX];
8d08fdba 173
2c73f9f5 174/* Indicates that there is a type value in some namespace, although
7f4edbcb 175 that is not necessarily in scope at the moment. */
2c73f9f5 176
00e8de68 177tree global_type_node;
2c73f9f5 178
82a2669e 179/* The node that holds the "name" of the global scope. */
00e8de68 180tree global_scope_name;
82a2669e 181
6625cdb5
JM
182/* Used only for jumps to as-yet undefined labels, since jumps to
183 defined labels can have their validity checked immediately. */
184
e2500fed 185struct named_label_use_list GTY(())
e349ee73 186{
e2500fed 187 struct cp_binding_level *binding_level;
e349ee73
MS
188 tree names_in_scope;
189 tree label_decl;
152c16a9 190 location_t o_goto_locus;
6625cdb5 191 struct named_label_use_list *next;
e349ee73
MS
192};
193
ed5511d9 194#define named_label_uses cp_function_chain->x_named_label_uses
8d08fdba 195
8e4ce833
JJ
196#define local_names cp_function_chain->x_local_names
197
8d08fdba
MS
198/* A list of objects which have constructors or destructors
199 which reside in the global scope. The decl is stored in
200 the TREE_VALUE slot and the initializer is stored
201 in the TREE_PURPOSE slot. */
202tree static_aggregates;
203
8d08fdba
MS
204/* -- end of C++ */
205
81b3411c 206/* A node for the integer constants 2, and 3. */
d11ad92e 207
81b3411c 208tree integer_two_node, integer_three_node;
8d08fdba 209
8d08fdba 210/* Similar, for last_function_parm_tags. */
9cd64686 211tree last_function_parms;
8d08fdba 212
6625cdb5
JM
213/* A list of all LABEL_DECLs in the function that have names. Here so
214 we can clear out their names' definitions at the end of the
215 function, and so we can check the validity of jumps to these labels. */
216
e2500fed 217struct named_label_list GTY(())
6625cdb5 218{
e2500fed 219 struct cp_binding_level *binding_level;
6625cdb5
JM
220 tree names_in_scope;
221 tree old_value;
222 tree label_decl;
223 tree bad_decls;
6625cdb5 224 struct named_label_list *next;
826840d9
RH
225 unsigned int in_try_scope : 1;
226 unsigned int in_catch_scope : 1;
6625cdb5 227};
8d08fdba 228
4519c0a8 229#define named_labels cp_function_chain->x_named_labels
8d08fdba 230\f
0c8feefe
MM
231/* The name of the anonymous namespace, throughout this translation
232 unit. */
233tree anonymous_namespace_name;
234
b2244c65
MM
235/* The number of function bodies which we are currently processing.
236 (Zero if we are at namespace scope, one inside the body of a
237 function, two inside the body of a function in a local class, etc.) */
238int function_depth;
e23bd218
IR
239
240/* States indicating how grokdeclarator() should handle declspecs marked
241 with __attribute__((deprecated)). An object declared as
242 __attribute__((deprecated)) suppresses warnings of uses of other
243 deprecated items. */
244
245enum deprecated_states {
246 DEPRECATED_NORMAL,
247 DEPRECATED_SUPPRESS
248};
249
250static enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
251
252/* Set by add_implicitly_declared_members() to keep those members from
253 being flagged as deprecated or reported as using deprecated
254 types. */
255int adding_implicit_members = 0;
594bb0e7
MM
256
257/* True if a declaration with an `extern' linkage specifier is being
258 processed. */
259bool have_extern_spec;
260
8d08fdba 261\f
0154eaa8
MM
262/* A TREE_LIST of VAR_DECLs. The TREE_PURPOSE is a RECORD_TYPE or
263 UNION_TYPE; the TREE_VALUE is a VAR_DECL with that type. At the
264 time the VAR_DECL was declared, the type was incomplete. */
265
e2500fed 266static GTY(()) tree incomplete_vars;
8d08fdba 267\f
74b846e0
MM
268/* Returns the kind of template specialization we are currently
269 processing, given that it's declaration contained N_CLASS_SCOPES
270 explicit scope qualifications. */
271
272tmpl_spec_kind
11f6b451 273current_tmpl_spec_kind (int n_class_scopes)
74b846e0
MM
274{
275 int n_template_parm_scopes = 0;
276 int seen_specialization_p = 0;
277 int innermost_specialization_p = 0;
e2500fed 278 struct cp_binding_level *b;
74b846e0
MM
279
280 /* Scan through the template parameter scopes. */
a7e8c268
MM
281 for (b = current_binding_level;
282 b->kind == sk_template_parms;
283 b = b->level_chain)
74b846e0
MM
284 {
285 /* If we see a specialization scope inside a parameter scope,
286 then something is wrong. That corresponds to a declaration
287 like:
288
289 template <class T> template <> ...
290
0e339752 291 which is always invalid since [temp.expl.spec] forbids the
74b846e0
MM
292 specialization of a class member template if the enclosing
293 class templates are not explicitly specialized as well. */
a7e8c268 294 if (b->explicit_spec_p)
74b846e0
MM
295 {
296 if (n_template_parm_scopes == 0)
297 innermost_specialization_p = 1;
298 else
299 seen_specialization_p = 1;
300 }
301 else if (seen_specialization_p == 1)
302 return tsk_invalid_member_spec;
303
304 ++n_template_parm_scopes;
305 }
306
307 /* Handle explicit instantiations. */
308 if (processing_explicit_instantiation)
309 {
310 if (n_template_parm_scopes != 0)
311 /* We've seen a template parameter list during an explicit
312 instantiation. For example:
313
314 template <class T> template void f(int);
315
316 This is erroneous. */
317 return tsk_invalid_expl_inst;
318 else
319 return tsk_expl_inst;
320 }
321
322 if (n_template_parm_scopes < n_class_scopes)
323 /* We've not seen enough template headers to match all the
324 specialized classes present. For example:
325
326 template <class T> void R<T>::S<T>::f(int);
327
0e339752 328 This is invalid; there needs to be one set of template
74b846e0
MM
329 parameters for each class. */
330 return tsk_insufficient_parms;
331 else if (n_template_parm_scopes == n_class_scopes)
332 /* We're processing a non-template declaration (even though it may
333 be a member of a template class.) For example:
334
335 template <class T> void S<T>::f(int);
336
337 The `class T' maches the `S<T>', leaving no template headers
338 corresponding to the `f'. */
339 return tsk_none;
340 else if (n_template_parm_scopes > n_class_scopes + 1)
341 /* We've got too many template headers. For example:
342
343 template <> template <class T> void f (T);
344
345 There need to be more enclosing classes. */
346 return tsk_excessive_parms;
347 else
348 /* This must be a template. It's of the form:
349
350 template <class T> template <class U> void S<T>::f(U);
351
352 This is a specialization if the innermost level was a
353 specialization; otherwise it's just a definition of the
354 template. */
355 return innermost_specialization_p ? tsk_expl_spec : tsk_template;
8d08fdba
MS
356}
357
74b846e0
MM
358/* Exit the current scope. */
359
360void
11f6b451 361finish_scope (void)
74b846e0
MM
362{
363 poplevel (0, 0, 0);
364}
365
acef433b
MM
366/* When a label goes out of scope, check to see if that label was used
367 in a valid manner, and issue any appropriate warnings or errors. */
368
369static void
11f6b451 370pop_label (tree label, tree old_value)
acef433b 371{
a7e8c268 372 if (!processing_template_decl)
acef433b 373 {
88848bde
MM
374 if (DECL_INITIAL (label) == NULL_TREE)
375 {
5b030314
NS
376 location_t location;
377
88848bde 378 cp_error_at ("label `%D' used but not defined", label);
5b030314
NS
379 location.file = input_filename;
380 location.line = 0;
88848bde 381 /* Avoid crashing later. */
5b030314 382 define_label (location, DECL_NAME (label));
88848bde 383 }
078721e1 384 else if (warn_unused_label && !TREE_USED (label))
88848bde 385 cp_warning_at ("label `%D' defined but not used", label);
acef433b 386 }
acef433b 387
6625cdb5 388 SET_IDENTIFIER_LABEL_VALUE (DECL_NAME (label), old_value);
acef433b
MM
389}
390
0811ea8f 391/* At the end of a function, all labels declared within the function
68642fb6 392 go out of scope. BLOCK is the top-level block for the
acef433b
MM
393 function. */
394
395static void
11f6b451 396pop_labels (tree block)
acef433b 397{
6625cdb5 398 struct named_label_list *link;
acef433b
MM
399
400 /* Clear out the definitions of all label names, since their scopes
401 end here. */
6625cdb5 402 for (link = named_labels; link; link = link->next)
acef433b 403 {
6625cdb5 404 pop_label (link->label_decl, link->old_value);
acef433b
MM
405 /* Put the labels into the "variables" of the top-level block,
406 so debugger can see them. */
6625cdb5
JM
407 TREE_CHAIN (link->label_decl) = BLOCK_VARS (block);
408 BLOCK_VARS (block) = link->label_decl;
acef433b
MM
409 }
410
6625cdb5 411 named_labels = NULL;
acef433b
MM
412}
413
8d08fdba
MS
414/* Exit a binding level.
415 Pop the level off, and restore the state of the identifier-decl mappings
416 that were in effect when this level was entered.
417
418 If KEEP == 1, this level had explicit declarations, so
419 and create a "block" (a BLOCK node) for the level
420 to record its declarations and subblocks for symbol table output.
421
8d08fdba
MS
422 If FUNCTIONBODY is nonzero, this level is the body of a function,
423 so create a block as if KEEP were set and also clear out all
424 label names.
425
426 If REVERSE is nonzero, reverse the order of decls before putting
427 them into the BLOCK. */
428
429tree
11f6b451 430poplevel (int keep, int reverse, int functionbody)
8d08fdba
MS
431{
432 register tree link;
433 /* The chain of decls was accumulated in reverse order.
434 Put it into forward order, just for cleanliness. */
435 tree decls;
436 int tmp = functionbody;
b35d4555 437 int real_functionbody;
b35d4555 438 tree subblocks;
8d08fdba
MS
439 tree block = NULL_TREE;
440 tree decl;
f181d4ae 441 int leaving_for_scope;
ac20c67a 442 scope_kind kind;
f181d4ae 443
22ffcc6f 444 timevar_push (TV_NAME_LOOKUP);
b35d4555 445
a7e8c268 446 my_friendly_assert (current_binding_level->kind != sk_class, 19990916);
b74a0560 447
ac20c67a 448 real_functionbody = (current_binding_level->kind == sk_cleanup
b35d4555 449 ? ((functionbody = 0), tmp) : functionbody);
b35d4555
MM
450 subblocks = functionbody >= 0 ? current_binding_level->blocks : 0;
451
b74a0560
MM
452 my_friendly_assert (!current_binding_level->class_shadowed,
453 19990414);
8d08fdba 454
536333d4
MM
455 /* We used to use KEEP == 2 to indicate that the new block should go
456 at the beginning of the list of blocks at this binding level,
457 rather than the end. This hack is no longer used. */
458 my_friendly_assert (keep == 0 || keep == 1, 0);
459
ac20c67a 460 if (current_binding_level->keep)
8d08fdba
MS
461 keep = 1;
462
6625cdb5
JM
463 /* Any uses of undefined labels, and any defined labels, now operate
464 under constraints of next binding contour. */
465 if (cfun && !functionbody)
466 {
e2500fed 467 struct cp_binding_level *level_chain;
6625cdb5
JM
468 level_chain = current_binding_level->level_chain;
469 if (level_chain)
470 {
471 struct named_label_use_list *uses;
472 struct named_label_list *labels;
473 for (labels = named_labels; labels; labels = labels->next)
474 if (labels->binding_level == current_binding_level)
475 {
476 tree decl;
a7e8c268 477 if (current_binding_level->kind == sk_try)
826840d9 478 labels->in_try_scope = 1;
a7e8c268 479 if (current_binding_level->kind == sk_catch)
826840d9 480 labels->in_catch_scope = 1;
6625cdb5
JM
481 for (decl = labels->names_in_scope; decl;
482 decl = TREE_CHAIN (decl))
483 if (decl_jump_unsafe (decl))
484 labels->bad_decls = tree_cons (NULL_TREE, decl,
485 labels->bad_decls);
486 labels->binding_level = level_chain;
487 labels->names_in_scope = level_chain->names;
488 }
489
490 for (uses = named_label_uses; uses; uses = uses->next)
491 if (uses->binding_level == current_binding_level)
492 {
493 uses->binding_level = level_chain;
494 uses->names_in_scope = level_chain->names;
495 }
496 }
497 }
498
8d08fdba
MS
499 /* Get the decls in the order they were written.
500 Usually current_binding_level->names is in reverse order.
501 But parameter decls were previously put in forward order. */
502
503 if (reverse)
504 current_binding_level->names
505 = decls = nreverse (current_binding_level->names);
506 else
507 decls = current_binding_level->names;
508
509 /* Output any nested inline functions within this block
510 if they weren't already output. */
8d08fdba
MS
511 for (decl = decls; decl; decl = TREE_CHAIN (decl))
512 if (TREE_CODE (decl) == FUNCTION_DECL
513 && ! TREE_ASM_WRITTEN (decl)
514 && DECL_INITIAL (decl) != NULL_TREE
6060a796
MS
515 && TREE_ADDRESSABLE (decl)
516 && decl_function_context (decl) == current_function_decl)
8d08fdba
MS
517 {
518 /* If this decl was copied from a file-scope decl
519 on account of a block-scope extern decl,
520 propagate TREE_ADDRESSABLE to the file-scope decl. */
521 if (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE)
522 TREE_ADDRESSABLE (DECL_ABSTRACT_ORIGIN (decl)) = 1;
523 else
524 {
525 push_function_context ();
526 output_inline_function (decl);
527 pop_function_context ();
528 }
529 }
530
d9b2d9da
MM
531 /* When not in function-at-a-time mode, expand_end_bindings will
532 warn about unused variables. But, in function-at-a-time mode
533 expand_end_bindings is not passed the list of variables in the
534 current scope, and therefore no warning is emitted. So, we
535 explicitly warn here. */
536 if (!processing_template_decl)
537 warn_about_unused_variables (getdecls ());
538
8d08fdba
MS
539 /* If there were any declarations or structure tags in that level,
540 or if this level is a function body,
541 create a BLOCK to record them for the life of this function. */
8d08fdba 542 block = NULL_TREE;
f444e36b 543 if (keep == 1 || functionbody)
8d08fdba
MS
544 block = make_node (BLOCK);
545 if (block != NULL_TREE)
546 {
f444e36b
MM
547 BLOCK_VARS (block) = decls;
548 BLOCK_SUBBLOCKS (block) = subblocks;
8d08fdba
MS
549 }
550
551 /* In each subblock, record that this is its superior. */
8d08fdba
MS
552 if (keep >= 0)
553 for (link = subblocks; link; link = TREE_CHAIN (link))
554 BLOCK_SUPERCONTEXT (link) = block;
555
f181d4ae
MM
556 /* We still support the old for-scope rules, whereby the variables
557 in a for-init statement were in scope after the for-statement
92bc1323 558 ended. We only use the new rules if flag_new_for_scope is
f181d4ae 559 nonzero. */
68642fb6 560 leaving_for_scope
a7e8c268 561 = current_binding_level->kind == sk_for && flag_new_for_scope == 1;
f181d4ae
MM
562
563 /* Remove declarations for all the DECLs in this level. */
564 for (link = decls; link; link = TREE_CHAIN (link))
565 {
c3783399
NS
566 if (leaving_for_scope && TREE_CODE (link) == VAR_DECL
567 && DECL_NAME (link))
f181d4ae 568 {
af6fd53f
GDR
569 cxx_binding *outer_binding
570 = IDENTIFIER_BINDING (DECL_NAME (link))->previous;
f181d4ae
MM
571 tree ns_binding;
572
573 if (!outer_binding)
574 ns_binding = IDENTIFIER_NAMESPACE_VALUE (DECL_NAME (link));
c7dda1e3
MM
575 else
576 ns_binding = NULL_TREE;
577
68642fb6 578 if (outer_binding
147135cc 579 && outer_binding->scope == current_binding_level->level_chain)
f181d4ae 580 /* We have something like:
68642fb6 581
f181d4ae
MM
582 int i;
583 for (int i; ;);
68642fb6 584
f181d4ae
MM
585 and we are leaving the `for' scope. There's no reason to
586 keep the binding of the inner `i' in this case. */
d8f8dca1 587 pop_binding (DECL_NAME (link), link);
68642fb6 588 else if ((outer_binding
147135cc
GDR
589 && (TREE_CODE (outer_binding->value) == TYPE_DECL))
590 || (ns_binding && TREE_CODE (ns_binding) == TYPE_DECL))
f181d4ae
MM
591 /* Here, we have something like:
592
593 typedef int I;
594
595 void f () {
596 for (int I; ;);
597 }
598
599 We must pop the for-scope binding so we know what's a
600 type and what isn't. */
d8f8dca1 601 pop_binding (DECL_NAME (link), link);
e76a2646 602 else
e76a2646 603 {
f181d4ae
MM
604 /* Mark this VAR_DECL as dead so that we can tell we left it
605 there only for backward compatibility. */
606 DECL_DEAD_FOR_LOCAL (link) = 1;
68642fb6 607
517f3315 608 /* Keep track of what should have happened when we
f181d4ae 609 popped the binding. */
147135cc
GDR
610 if (outer_binding && outer_binding->value)
611 DECL_SHADOWED_FOR_VAR (link) = outer_binding->value;
f181d4ae
MM
612
613 /* Add it to the list of dead variables in the next
614 outermost binding to that we can remove these when we
615 leave that binding. */
616 current_binding_level->level_chain->dead_vars_from_for
617 = tree_cons (NULL_TREE, link,
618 current_binding_level->level_chain->
619 dead_vars_from_for);
620
af6fd53f 621 /* Although we don't pop the cxx_binding, we do clear
147135cc
GDR
622 its SCOPE since the scope is going away now. */
623 IDENTIFIER_BINDING (DECL_NAME (link))->scope = NULL;
e76a2646 624 }
2ee887f2 625 }
68642fb6 626 else
8d6e462b 627 {
f181d4ae 628 /* Remove the binding. */
0034cf72
JM
629 decl = link;
630 if (TREE_CODE (decl) == TREE_LIST)
631 decl = TREE_VALUE (decl);
2f939d94 632 if (DECL_P (decl))
0034cf72
JM
633 pop_binding (DECL_NAME (decl), decl);
634 else if (TREE_CODE (decl) == OVERLOAD)
635 pop_binding (DECL_NAME (OVL_FUNCTION (decl)), decl);
68642fb6 636 else
a98facb0 637 abort ();
8d08fdba 638 }
f181d4ae 639 }
8d08fdba 640
f181d4ae
MM
641 /* Remove declarations for any `for' variables from inner scopes
642 that we kept around. */
643 for (link = current_binding_level->dead_vars_from_for;
644 link; link = TREE_CHAIN (link))
d8f8dca1 645 pop_binding (DECL_NAME (TREE_VALUE (link)), TREE_VALUE (link));
2ee887f2 646
f181d4ae
MM
647 /* Restore the IDENTIFIER_TYPE_VALUEs. */
648 for (link = current_binding_level->type_shadowed;
649 link; link = TREE_CHAIN (link))
650 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (link), TREE_VALUE (link));
acef433b
MM
651
652 /* Restore the IDENTIFIER_LABEL_VALUEs for local labels. */
653 for (link = current_binding_level->shadowed_labels;
68642fb6 654 link;
acef433b 655 link = TREE_CHAIN (link))
6625cdb5 656 pop_label (TREE_VALUE (link), TREE_PURPOSE (link));
acef433b 657
f181d4ae
MM
658 /* There may be OVERLOADs (wrapped in TREE_LISTs) on the BLOCK_VARs
659 list if a `using' declaration put them there. The debugging
660 back-ends won't understand OVERLOAD, so we remove them here.
661 Because the BLOCK_VARS are (temporarily) shared with
662 CURRENT_BINDING_LEVEL->NAMES we must do this fixup after we have
663 popped all the bindings. */
664 if (block)
665 {
666 tree* d;
8d6e462b 667
a06d48ef
JM
668 for (d = &BLOCK_VARS (block); *d; )
669 {
670 if (TREE_CODE (*d) == TREE_LIST)
671 *d = TREE_CHAIN (*d);
672 else
673 d = &TREE_CHAIN (*d);
674 }
8d6e462b 675 }
8d08fdba
MS
676
677 /* If the level being exited is the top level of a function,
678 check over all the labels. */
8d08fdba
MS
679 if (functionbody)
680 {
acef433b
MM
681 /* Since this is the top level block of a function, the vars are
682 the function's parameters. Don't leave them in the BLOCK
683 because they are found in the FUNCTION_DECL instead. */
8d08fdba 684 BLOCK_VARS (block) = 0;
acef433b 685 pop_labels (block);
8d08fdba
MS
686 }
687
ac20c67a 688 kind = current_binding_level->kind;
8d08fdba 689
1a6daf15 690 leave_scope ();
8d08fdba
MS
691 if (functionbody)
692 DECL_INITIAL (current_function_decl) = block;
693 else if (block)
f444e36b
MM
694 current_binding_level->blocks
695 = chainon (current_binding_level->blocks, block);
696
8d08fdba
MS
697 /* If we did not make a block for the level just exited,
698 any blocks made for inner levels
699 (since they cannot be recorded as subblocks in that level)
700 must be carried forward so they will later become subblocks
701 of something else. */
702 else if (subblocks)
536333d4
MM
703 current_binding_level->blocks
704 = chainon (current_binding_level->blocks, subblocks);
8d08fdba 705
d9b2d9da
MM
706 /* Each and every BLOCK node created here in `poplevel' is important
707 (e.g. for proper debugging information) so if we created one
708 earlier, mark it as "used". */
709 if (block)
710 TREE_USED (block) = 1;
711
8d08fdba 712 /* Take care of compiler's internal binding structures. */
ac20c67a 713 if (kind == sk_cleanup)
8d08fdba 714 {
d9b2d9da
MM
715 tree scope_stmts;
716
68642fb6 717 scope_stmts
d9b2d9da 718 = add_scope_stmt (/*begin_p=*/0, /*partial_p=*/1);
8d08fdba 719 if (block)
d9b2d9da
MM
720 {
721 SCOPE_STMT_BLOCK (TREE_PURPOSE (scope_stmts)) = block;
722 SCOPE_STMT_BLOCK (TREE_VALUE (scope_stmts)) = block;
723 }
724
8f471b0d 725 block = poplevel (keep, reverse, functionbody);
8d08fdba
MS
726 }
727
22ffcc6f 728 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, block);
8d08fdba
MS
729}
730
731/* Delete the node BLOCK from the current binding level.
732 This is used for the block inside a stmt expr ({...})
733 so that the block can be reinserted where appropriate. */
734
735void
11f6b451 736delete_block (tree block)
8d08fdba
MS
737{
738 tree t;
739 if (current_binding_level->blocks == block)
740 current_binding_level->blocks = TREE_CHAIN (block);
741 for (t = current_binding_level->blocks; t;)
742 {
743 if (TREE_CHAIN (t) == block)
744 TREE_CHAIN (t) = TREE_CHAIN (block);
745 else
746 t = TREE_CHAIN (t);
747 }
748 TREE_CHAIN (block) = NULL_TREE;
749 /* Clear TREE_USED which is always set by poplevel.
750 The flag is set again if insert_block is called. */
751 TREE_USED (block) = 0;
752}
753
754/* Insert BLOCK at the end of the list of subblocks of the
755 current binding level. This is used when a BIND_EXPR is expanded,
756 to handle the BLOCK node inside the BIND_EXPR. */
757
758void
11f6b451 759insert_block (tree block)
8d08fdba
MS
760{
761 TREE_USED (block) = 1;
762 current_binding_level->blocks
763 = chainon (current_binding_level->blocks, block);
764}
765
8d08fdba
MS
766/* Set the BLOCK node for the innermost scope
767 (the one we are currently in). */
768
769void
11f6b451 770set_block (tree block ATTRIBUTE_UNUSED )
8d08fdba 771{
f444e36b
MM
772 /* The RTL expansion machinery requires us to provide this callback,
773 but it is not applicable in function-at-a-time mode. */
8d08fdba
MS
774}
775
838dfd8a 776/* Returns nonzero if T is a virtual function table. */
0aafb128
MM
777
778int
11f6b451 779vtable_decl_p (tree t, void* data ATTRIBUTE_UNUSED )
0aafb128
MM
780{
781 return (TREE_CODE (t) == VAR_DECL && DECL_VIRTUAL_P (t));
782}
783
838dfd8a 784/* Returns nonzero if T is a TYPE_DECL for a type with virtual
0aafb128
MM
785 functions. */
786
787int
11f6b451 788vtype_decl_p (tree t, void *data ATTRIBUTE_UNUSED )
0aafb128
MM
789{
790 return (TREE_CODE (t) == TYPE_DECL
34249844
NS
791 && TREE_CODE (TREE_TYPE (t)) == RECORD_TYPE
792 && TYPE_POLYMORPHIC_P (TREE_TYPE (t)));
0aafb128
MM
793}
794
70b76b34
DP
795struct walk_globals_data {
796 walk_globals_pred p;
797 walk_globals_fn f;
798 void *data;
799};
800
801/* Walk the vtable declarations in NAMESPACE. Whenever one is found
838dfd8a
KH
802 for which P returns nonzero, call F with its address. If any call
803 to F returns a nonzero value, return a nonzero value. */
70b76b34
DP
804
805static int
11f6b451 806walk_vtables_r (tree namespace, void* data)
70b76b34
DP
807{
808 struct walk_globals_data* wgd = (struct walk_globals_data *) data;
809 walk_globals_fn f = wgd->f;
810 void *d = wgd->data;
811 tree decl = NAMESPACE_LEVEL (namespace)->vtables;
812 int result = 0;
813
814 for (; decl ; decl = TREE_CHAIN (decl))
0246d49e 815 result |= (*f) (&decl, d);
70b76b34
DP
816
817 return result;
818}
819
820/* Walk the vtable declarations. Whenever one is found for which P
838dfd8a
KH
821 returns nonzero, call F with its address. If any call to F
822 returns a nonzero value, return a nonzero value. */
848eed92
GDR
823bool
824walk_vtables (walk_globals_pred p, walk_globals_fn f, void *data)
70b76b34
DP
825{
826 struct walk_globals_data wgd;
827 wgd.p = p;
828 wgd.f = f;
829 wgd.data = data;
830
831 return walk_namespaces (walk_vtables_r, &wgd);
832}
833
0aafb128
MM
834/* Walk all the namespaces contained NAMESPACE, including NAMESPACE
835 itself, calling F for each. The DATA is passed to F as well. */
836
00e8de68
GDR
837static int
838walk_namespaces_r (tree namespace, walk_namespaces_fn f, void* data)
839{
840 int result = 0;
841 tree current = NAMESPACE_LEVEL (namespace)->namespaces;
8d08fdba 842
00e8de68 843 result |= (*f) (namespace, data);
8d08fdba 844
00e8de68
GDR
845 for (; current; current = TREE_CHAIN (current))
846 result |= walk_namespaces_r (current, f, data);
fc0e7bf5 847
00e8de68 848 return result;
5566b478
MS
849}
850
00e8de68
GDR
851/* Walk all the namespaces, calling F for each. The DATA is passed to
852 F as well. */
853
854int
855walk_namespaces (walk_namespaces_fn f, void* data)
5566b478 856{
00e8de68 857 return walk_namespaces_r (global_namespace, f, data);
8d08fdba
MS
858}
859
00e8de68
GDR
860/* Walk the global declarations in NAMESPACE. Whenever one is found
861 for which P returns nonzero, call F with its address. If any call
862 to F returns a nonzero value, return a nonzero value. */
8d08fdba 863
00e8de68
GDR
864static int
865walk_globals_r (tree namespace, void* data)
866{
867 struct walk_globals_data* wgd = (struct walk_globals_data *) data;
868 walk_globals_pred p = wgd->p;
869 walk_globals_fn f = wgd->f;
870 void *d = wgd->data;
871 tree *t;
872 int result = 0;
8d08fdba 873
00e8de68 874 t = &NAMESPACE_LEVEL (namespace)->names;
9cd64686 875
00e8de68 876 while (*t)
8d08fdba 877 {
00e8de68 878 tree glbl = *t;
8d08fdba 879
00e8de68
GDR
880 if ((*p) (glbl, d))
881 result |= (*f) (t, d);
4b0d3cbe 882
00e8de68
GDR
883 /* If F changed *T, then *T still points at the next item to
884 examine. */
885 if (*t == glbl)
886 t = &TREE_CHAIN (*t);
2c73f9f5 887 }
8d08fdba 888
00e8de68 889 return result;
8d08fdba
MS
890}
891
00e8de68
GDR
892/* Walk the global declarations. Whenever one is found for which P
893 returns true, call F with its address. If any call to F
894 returns true, return true. */
2c73f9f5 895
00e8de68
GDR
896bool
897walk_globals (walk_globals_pred p, walk_globals_fn f, void *data)
2c73f9f5 898{
00e8de68
GDR
899 struct walk_globals_data wgd;
900 wgd.p = p;
901 wgd.f = f;
902 wgd.data = data;
e92cc029 903
00e8de68 904 return walk_namespaces (walk_globals_r, &wgd);
8926095f
MS
905}
906
00e8de68
GDR
907/* Call wrapup_globals_declarations for the globals in NAMESPACE. If
908 DATA is non-NULL, this is the last time we will call
909 wrapup_global_declarations for this NAMESPACE. */
39c01e4c 910
00e8de68
GDR
911int
912wrapup_globals_for_namespace (tree namespace, void* data)
39c01e4c 913{
00e8de68
GDR
914 struct cp_binding_level *level = NAMESPACE_LEVEL (namespace);
915 varray_type statics = level->static_decls;
916 tree *vec = &VARRAY_TREE (statics, 0);
917 int len = VARRAY_ACTIVE_SIZE (statics);
918 int last_time = (data != 0);
68642fb6 919
00e8de68 920 if (last_time)
39c01e4c 921 {
00e8de68
GDR
922 check_global_declarations (vec, len);
923 return 0;
39c01e4c
MM
924 }
925
00e8de68
GDR
926 /* Write out any globals that need to be output. */
927 return wrapup_global_declarations (vec, len);
39c01e4c
MM
928}
929
00e8de68 930\f
9188c363
MM
931/* In C++, you don't have to write `struct S' to refer to `S'; you
932 can just use `S'. We accomplish this by creating a TYPE_DECL as
933 if the user had written `typedef struct S S'. Create and return
934 the TYPE_DECL for TYPE. */
935
936tree
11f6b451 937create_implicit_typedef (tree name, tree type)
9188c363
MM
938{
939 tree decl;
940
941 decl = build_decl (TYPE_DECL, name, type);
c727aa5e 942 DECL_ARTIFICIAL (decl) = 1;
9188c363
MM
943 /* There are other implicit type declarations, like the one *within*
944 a class that allows you to write `S::S'. We must distinguish
945 amongst these. */
946 SET_DECL_IMPLICIT_TYPEDEF_P (decl);
947 TYPE_NAME (type) = decl;
948
949 return decl;
950}
951
8e4ce833
JJ
952/* Remember a local name for name-mangling purposes. */
953
954static void
11f6b451 955push_local_name (tree decl)
8e4ce833
JJ
956{
957 size_t i, nelts;
958 tree t, name;
959
22ffcc6f 960 timevar_push (TV_NAME_LOOKUP);
8e4ce833
JJ
961 if (!local_names)
962 VARRAY_TREE_INIT (local_names, 8, "local_names");
963
964 name = DECL_NAME (decl);
965
966 nelts = VARRAY_ACTIVE_SIZE (local_names);
967 for (i = 0; i < nelts; i++)
968 {
969 t = VARRAY_TREE (local_names, i);
970 if (DECL_NAME (t) == name)
971 {
972 if (!DECL_LANG_SPECIFIC (decl))
973 retrofit_lang_decl (decl);
e2500fed 974 DECL_LANG_SPECIFIC (decl)->decl_flags.u2sel = 1;
8e4ce833
JJ
975 if (DECL_LANG_SPECIFIC (t))
976 DECL_DISCRIMINATOR (decl) = DECL_DISCRIMINATOR (t) + 1;
977 else
978 DECL_DISCRIMINATOR (decl) = 1;
979
980 VARRAY_TREE (local_names, i) = decl;
de94b46c
KG
981 timevar_pop (TV_NAME_LOOKUP);
982 return;
8e4ce833
JJ
983 }
984 }
985
986 VARRAY_PUSH_TREE (local_names, decl);
22ffcc6f 987 timevar_pop (TV_NAME_LOOKUP);
8e4ce833 988}
8d08fdba
MS
989\f
990/* Subroutine of duplicate_decls: return truthvalue of whether
991 or not types of these decls match.
992
993 For C++, we must compare the parameter list so that `int' can match
994 `int&' in a parameter position, but `int&' is not confused with
995 `const int&'. */
e92cc029 996
6060a796 997int
11f6b451 998decls_match (tree newdecl, tree olddecl)
8d08fdba
MS
999{
1000 int types_match;
1001
347d73d7
ML
1002 if (newdecl == olddecl)
1003 return 1;
1004
6b4b3deb
MM
1005 if (TREE_CODE (newdecl) != TREE_CODE (olddecl))
1006 /* If the two DECLs are not even the same kind of thing, we're not
1007 interested in their types. */
1008 return 0;
1009
1010 if (TREE_CODE (newdecl) == FUNCTION_DECL)
8d08fdba
MS
1011 {
1012 tree f1 = TREE_TYPE (newdecl);
1013 tree f2 = TREE_TYPE (olddecl);
1014 tree p1 = TYPE_ARG_TYPES (f1);
1015 tree p2 = TYPE_ARG_TYPES (f2);
1016
4f1c5b7d 1017 if (CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl)
eb68cb58
MM
1018 && ! (DECL_EXTERN_C_P (newdecl)
1019 && DECL_EXTERN_C_P (olddecl)))
c5a6fc45
JM
1020 return 0;
1021
8d08fdba 1022 if (TREE_CODE (f1) != TREE_CODE (f2))
e867a179 1023 return 0;
8d08fdba 1024
3bfdc719 1025 if (same_type_p (TREE_TYPE (f1), TREE_TYPE (f2)))
8926095f 1026 {
8b27e9ef
NS
1027 if (p2 == NULL_TREE && DECL_EXTERN_C_P (olddecl)
1028 && (DECL_BUILT_IN (olddecl)
1029#ifndef NO_IMPLICIT_EXTERN_C
1030 || (DECL_IN_SYSTEM_HEADER (newdecl) && !DECL_CLASS_SCOPE_P (newdecl))
1031 || (DECL_IN_SYSTEM_HEADER (olddecl) && !DECL_CLASS_SCOPE_P (olddecl))
1032#endif
1033 ))
a28e3c7f
MS
1034 {
1035 types_match = self_promoting_args_p (p1);
1036 if (p1 == void_list_node)
1037 TREE_TYPE (newdecl) = TREE_TYPE (olddecl);
1038 }
8b27e9ef
NS
1039#ifndef NO_IMPLICIT_EXTERN_C
1040 else if (p1 == NULL_TREE
1041 && (DECL_EXTERN_C_P (olddecl)
1042 && DECL_IN_SYSTEM_HEADER (olddecl)
1043 && !DECL_CLASS_SCOPE_P (olddecl))
1044 && (DECL_EXTERN_C_P (newdecl)
1045 && DECL_IN_SYSTEM_HEADER (newdecl)
1046 && !DECL_CLASS_SCOPE_P (newdecl)))
a28e3c7f
MS
1047 {
1048 types_match = self_promoting_args_p (p2);
1049 TREE_TYPE (newdecl) = TREE_TYPE (olddecl);
1050 }
8b27e9ef 1051#endif
8926095f 1052 else
91063b51 1053 types_match = compparms (p1, p2);
8926095f 1054 }
8d08fdba
MS
1055 else
1056 types_match = 0;
1057 }
6b4b3deb 1058 else if (TREE_CODE (newdecl) == TEMPLATE_DECL)
51c184be 1059 {
2bb5d995
JM
1060 if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl))
1061 != TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)))
1062 return 0;
1063
ee935db4
MM
1064 if (!comp_template_parms (DECL_TEMPLATE_PARMS (newdecl),
1065 DECL_TEMPLATE_PARMS (olddecl)))
1066 return 0;
1067
f84b4be9 1068 if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL)
ee935db4
MM
1069 types_match = same_type_p (TREE_TYPE (DECL_TEMPLATE_RESULT (olddecl)),
1070 TREE_TYPE (DECL_TEMPLATE_RESULT (newdecl)));
f84b4be9
JM
1071 else
1072 types_match = decls_match (DECL_TEMPLATE_RESULT (olddecl),
1073 DECL_TEMPLATE_RESULT (newdecl));
51c184be 1074 }
8d08fdba
MS
1075 else
1076 {
1077 if (TREE_TYPE (newdecl) == error_mark_node)
1078 types_match = TREE_TYPE (olddecl) == error_mark_node;
1079 else if (TREE_TYPE (olddecl) == NULL_TREE)
1080 types_match = TREE_TYPE (newdecl) == NULL_TREE;
a0a33927
MS
1081 else if (TREE_TYPE (newdecl) == NULL_TREE)
1082 types_match = 0;
8d08fdba 1083 else
01240200 1084 types_match = comptypes (TREE_TYPE (newdecl),
3bfdc719
MM
1085 TREE_TYPE (olddecl),
1086 COMPARE_REDECLARATION);
8d08fdba
MS
1087 }
1088
1089 return types_match;
1090}
1091
1092/* If NEWDECL is `static' and an `extern' was seen previously,
24f30ed4 1093 warn about it. OLDDECL is the previous declaration.
8d08fdba
MS
1094
1095 Note that this does not apply to the C++ case of declaring
1096 a variable `extern const' and then later `const'.
1097
8d08fdba
MS
1098 Don't complain about built-in functions, since they are beyond
1099 the user's control. */
1100
a5e6b29b 1101void
11f6b451 1102warn_extern_redeclared_static (tree newdecl, tree olddecl)
8d08fdba 1103{
83182544 1104 static const char *const explicit_extern_static_warning
8251199e 1105 = "`%D' was declared `extern' and later `static'";
83182544 1106 static const char *const implicit_extern_static_warning
8251199e
JM
1107 = "`%D' was declared implicitly `extern' and later `static'";
1108
24f30ed4
MM
1109 tree name;
1110
68642fb6 1111 if (TREE_CODE (newdecl) == TYPE_DECL
655dc6ee 1112 || TREE_CODE (newdecl) == TEMPLATE_DECL
a2a9e21c
GDR
1113 || TREE_CODE (newdecl) == CONST_DECL
1114 || TREE_CODE (newdecl) == NAMESPACE_DECL)
8d08fdba 1115 return;
68642fb6 1116
963d5758
MM
1117 /* Don't get confused by static member functions; that's a different
1118 use of `static'. */
1119 if (TREE_CODE (newdecl) == FUNCTION_DECL
1120 && DECL_STATIC_FUNCTION_P (newdecl))
1121 return;
8d08fdba 1122
24f30ed4
MM
1123 /* If the old declaration was `static', or the new one isn't, then
1124 then everything is OK. */
1125 if (DECL_THIS_STATIC (olddecl) || !DECL_THIS_STATIC (newdecl))
1126 return;
1127
1128 /* It's OK to declare a builtin function as `static'. */
1129 if (TREE_CODE (olddecl) == FUNCTION_DECL
1130 && DECL_ARTIFICIAL (olddecl))
1131 return;
1132
8d08fdba 1133 name = DECL_ASSEMBLER_NAME (newdecl);
33bd39a2 1134 pedwarn (IDENTIFIER_IMPLICIT_DECL (name)
24f30ed4
MM
1135 ? implicit_extern_static_warning
1136 : explicit_extern_static_warning, newdecl);
1137 cp_pedwarn_at ("previous declaration of `%D'", olddecl);
8d08fdba
MS
1138}
1139
1140/* Handle when a new declaration NEWDECL has the same name as an old
1141 one OLDDECL in the same binding contour. Prints an error message
1142 if appropriate.
1143
1144 If safely possible, alter OLDDECL to look like NEWDECL, and return 1.
1145 Otherwise, return 0. */
1146
51c184be 1147int
11f6b451 1148duplicate_decls (tree newdecl, tree olddecl)
8d08fdba 1149{
8d08fdba
MS
1150 unsigned olddecl_uid = DECL_UID (olddecl);
1151 int olddecl_friend = 0, types_match = 0;
0b60dfe3 1152 int new_defines_function = 0;
5566b478
MS
1153
1154 if (newdecl == olddecl)
1155 return 1;
8d08fdba 1156
8926095f 1157 types_match = decls_match (newdecl, olddecl);
8d08fdba 1158
8d08fdba
MS
1159 /* If either the type of the new decl or the type of the old decl is an
1160 error_mark_node, then that implies that we have already issued an
1161 error (earlier) for some bogus type specification, and in that case,
1162 it is rather pointless to harass the user with yet more error message
0b60dfe3 1163 about the same declaration, so just pretend the types match here. */
bd6dd845
MS
1164 if (TREE_TYPE (newdecl) == error_mark_node
1165 || TREE_TYPE (olddecl) == error_mark_node)
8d08fdba 1166 types_match = 1;
68642fb6 1167
97055d5c
AO
1168 if (DECL_P (olddecl)
1169 && TREE_CODE (newdecl) == FUNCTION_DECL
1170 && TREE_CODE (olddecl) == FUNCTION_DECL
1171 && (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl)))
1172 {
1173 if (DECL_DECLARED_INLINE_P (newdecl)
1174 && DECL_UNINLINABLE (newdecl)
1175 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
1176 /* Already warned elsewhere. */;
1177 else if (DECL_DECLARED_INLINE_P (olddecl)
1178 && DECL_UNINLINABLE (olddecl)
1179 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
1180 /* Already warned. */;
1181 else if (DECL_DECLARED_INLINE_P (newdecl)
1182 && DECL_UNINLINABLE (olddecl)
1183 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
1184 {
ddd2d57e
RH
1185 warning ("%Jfunction '%D' redeclared as inline", newdecl, newdecl);
1186 warning ("%Jprevious declaration of '%D' with attribute noinline",
1187 olddecl, olddecl);
97055d5c
AO
1188 }
1189 else if (DECL_DECLARED_INLINE_P (olddecl)
1190 && DECL_UNINLINABLE (newdecl)
1191 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
1192 {
ddd2d57e
RH
1193 warning ("%Jfunction '%D' redeclared with attribute noinline",
1194 newdecl, newdecl);
1195 warning ("%Jprevious declaration of '%D' was inline",
1196 olddecl, olddecl);
97055d5c
AO
1197 }
1198 }
1199
a1c65f9f 1200 /* Check for redeclaration and other discrepancies. */
d22c8596 1201 if (TREE_CODE (olddecl) == FUNCTION_DECL
9f33663b
JM
1202 && DECL_ARTIFICIAL (olddecl))
1203 {
1204 if (TREE_CODE (newdecl) != FUNCTION_DECL)
1205 {
d52e4867
RS
1206 /* Avoid warnings redeclaring anticipated built-ins. */
1207 if (DECL_ANTICIPATED (olddecl))
1208 return 0;
1209
9f33663b
JM
1210 /* If you declare a built-in or predefined function name as static,
1211 the old definition is overridden, but optionally warn this was a
1212 bad choice of name. */
1213 if (! TREE_PUBLIC (newdecl))
1214 {
1215 if (warn_shadow)
33bd39a2 1216 warning ("shadowing %s function `%#D'",
9f33663b
JM
1217 DECL_BUILT_IN (olddecl) ? "built-in" : "library",
1218 olddecl);
1219 /* Discard the old built-in function. */
1220 return 0;
1221 }
1222 /* If the built-in is not ansi, then programs can override
1223 it even globally without an error. */
1224 else if (! DECL_BUILT_IN (olddecl))
33bd39a2 1225 warning ("library function `%#D' redeclared as non-function `%#D'",
9f33663b
JM
1226 olddecl, newdecl);
1227 else
1228 {
33bd39a2
ZW
1229 error ("declaration of `%#D'", newdecl);
1230 error ("conflicts with built-in declaration `%#D'",
a4443a08 1231 olddecl);
9f33663b 1232 }
a4443a08
MS
1233 return 0;
1234 }
9f33663b 1235 else if (!types_match)
8d08fdba 1236 {
d52e4867
RS
1237 /* Avoid warnings redeclaring anticipated built-ins. */
1238 if (DECL_ANTICIPATED (olddecl))
1239 ; /* Do nothing yet. */
1240 else if ((DECL_EXTERN_C_P (newdecl)
7e99327d
MM
1241 && DECL_EXTERN_C_P (olddecl))
1242 || compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
1243 TYPE_ARG_TYPES (TREE_TYPE (olddecl))))
a4443a08 1244 {
9f33663b
JM
1245 /* A near match; override the builtin. */
1246
1247 if (TREE_PUBLIC (newdecl))
a4443a08 1248 {
33bd39a2
ZW
1249 warning ("new declaration `%#D'", newdecl);
1250 warning ("ambiguates built-in declaration `%#D'",
9f33663b 1251 olddecl);
a4443a08 1252 }
9f33663b 1253 else if (warn_shadow)
33bd39a2 1254 warning ("shadowing %s function `%#D'",
9f33663b
JM
1255 DECL_BUILT_IN (olddecl) ? "built-in" : "library",
1256 olddecl);
a4443a08 1257 }
9f33663b
JM
1258 else
1259 /* Discard the old built-in function. */
1260 return 0;
04a9d080
RS
1261
1262 /* Replace the old RTL to avoid problems with inlining. */
1263 SET_DECL_RTL (olddecl, DECL_RTL (newdecl));
8d08fdba 1264 }
10326855 1265 /* Even if the types match, prefer the new declarations type
7df87825 1266 for anticipated built-ins, for exception lists, etc... */
10326855
RS
1267 else if (DECL_ANTICIPATED (olddecl))
1268 TREE_TYPE (olddecl) = TREE_TYPE (newdecl);
68642fb6 1269
7e99327d
MM
1270 /* Whether or not the builtin can throw exceptions has no
1271 bearing on this declarator. */
1272 TREE_NOTHROW (olddecl) = 0;
1273
24f30ed4
MM
1274 if (DECL_THIS_STATIC (newdecl) && !DECL_THIS_STATIC (olddecl))
1275 {
1276 /* If a builtin function is redeclared as `static', merge
1277 the declarations, but make the original one static. */
1278 DECL_THIS_STATIC (olddecl) = 1;
1279 TREE_PUBLIC (olddecl) = 0;
9bfadf57 1280
421844e7
MM
1281 /* Make the old declaration consistent with the new one so
1282 that all remnants of the builtin-ness of this function
1283 will be banished. */
5d2ed28c 1284 SET_DECL_LANGUAGE (olddecl, DECL_LANGUAGE (newdecl));
19e7881c 1285 SET_DECL_RTL (olddecl, DECL_RTL (newdecl));
24f30ed4 1286 }
39211cd5
MS
1287 }
1288 else if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
1289 {
9ed182dc
JM
1290 if ((TREE_CODE (olddecl) == TYPE_DECL && DECL_ARTIFICIAL (olddecl)
1291 && TREE_CODE (newdecl) != TYPE_DECL
1292 && ! (TREE_CODE (newdecl) == TEMPLATE_DECL
1293 && TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL))
1294 || (TREE_CODE (newdecl) == TYPE_DECL && DECL_ARTIFICIAL (newdecl)
1295 && TREE_CODE (olddecl) != TYPE_DECL
1296 && ! (TREE_CODE (olddecl) == TEMPLATE_DECL
1297 && (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl))
1298 == TYPE_DECL))))
1299 {
1300 /* We do nothing special here, because C++ does such nasty
1301 things with TYPE_DECLs. Instead, just let the TYPE_DECL
1302 get shadowed, and know that if we need to find a TYPE_DECL
1303 for a given name, we can look in the IDENTIFIER_TYPE_VALUE
1304 slot of the identifier. */
1305 return 0;
1306 }
1307
39211cd5 1308 if ((TREE_CODE (newdecl) == FUNCTION_DECL
5566b478 1309 && DECL_FUNCTION_TEMPLATE_P (olddecl))
39211cd5 1310 || (TREE_CODE (olddecl) == FUNCTION_DECL
5566b478 1311 && DECL_FUNCTION_TEMPLATE_P (newdecl)))
39211cd5 1312 return 0;
9ed182dc 1313
33bd39a2 1314 error ("`%#D' redeclared as different kind of symbol", newdecl);
39211cd5
MS
1315 if (TREE_CODE (olddecl) == TREE_LIST)
1316 olddecl = TREE_VALUE (olddecl);
8251199e 1317 cp_error_at ("previous declaration of `%#D'", olddecl);
39211cd5
MS
1318
1319 /* New decl is completely inconsistent with the old one =>
1320 tell caller to replace the old one. */
1321
1322 return 0;
8d08fdba 1323 }
8d08fdba
MS
1324 else if (!types_match)
1325 {
4f1c5b7d 1326 if (CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl))
7bdbfa05
MM
1327 /* These are certainly not duplicate declarations; they're
1328 from different scopes. */
1329 return 0;
1330
8926095f 1331 if (TREE_CODE (newdecl) == TEMPLATE_DECL)
f0e01782
MS
1332 {
1333 /* The name of a class template may not be declared to refer to
1334 any other template, class, function, object, namespace, value,
e92cc029 1335 or type in the same scope. */
5566b478
MS
1336 if (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) == TYPE_DECL
1337 || TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL)
f0e01782 1338 {
33bd39a2 1339 error ("declaration of template `%#D'", newdecl);
8251199e 1340 cp_error_at ("conflicts with previous declaration `%#D'",
f0e01782
MS
1341 olddecl);
1342 }
ec255269
MS
1343 else if (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) == FUNCTION_DECL
1344 && TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == FUNCTION_DECL
1345 && compparms (TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (olddecl))),
91063b51 1346 TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (newdecl))))
75650646 1347 && comp_template_parms (DECL_TEMPLATE_PARMS (newdecl),
b9201622
NS
1348 DECL_TEMPLATE_PARMS (olddecl))
1349 /* Template functions can be disambiguated by
1350 return type. */
1351 && same_type_p (TREE_TYPE (TREE_TYPE (newdecl)),
1352 TREE_TYPE (TREE_TYPE (olddecl))))
ec255269 1353 {
33bd39a2 1354 error ("new declaration `%#D'", newdecl);
8251199e 1355 cp_error_at ("ambiguates old declaration `%#D'", olddecl);
ec255269 1356 }
f0e01782
MS
1357 return 0;
1358 }
8926095f
MS
1359 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1360 {
eb68cb58 1361 if (DECL_EXTERN_C_P (newdecl) && DECL_EXTERN_C_P (olddecl))
8926095f 1362 {
33bd39a2 1363 error ("declaration of C function `%#D' conflicts with",
8926095f 1364 newdecl);
8251199e 1365 cp_error_at ("previous declaration `%#D' here", olddecl);
8926095f 1366 }
00595019 1367 else if (compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
91063b51 1368 TYPE_ARG_TYPES (TREE_TYPE (olddecl))))
700f8a87 1369 {
33bd39a2 1370 error ("new declaration `%#D'", newdecl);
8251199e 1371 cp_error_at ("ambiguates old declaration `%#D'", olddecl);
700f8a87
MS
1372 }
1373 else
1374 return 0;
8926095f 1375 }
8d08fdba
MS
1376
1377 /* Already complained about this, so don't do so again. */
a4443a08 1378 else if (current_class_type == NULL_TREE
8d08fdba
MS
1379 || IDENTIFIER_ERROR_LOCUS (DECL_ASSEMBLER_NAME (newdecl)) != current_class_type)
1380 {
a2a9e21c
GDR
1381 error ("conflicting declaration '%#D'", newdecl);
1382 cp_error_at ("'%D' has a previous declaration as `%#D'",
1383 olddecl, olddecl);
1384 return false;
8d08fdba
MS
1385 }
1386 }
68642fb6 1387 else if (TREE_CODE (newdecl) == FUNCTION_DECL
75650646
MM
1388 && ((DECL_TEMPLATE_SPECIALIZATION (olddecl)
1389 && (!DECL_TEMPLATE_INFO (newdecl)
68642fb6 1390 || (DECL_TI_TEMPLATE (newdecl)
75650646
MM
1391 != DECL_TI_TEMPLATE (olddecl))))
1392 || (DECL_TEMPLATE_SPECIALIZATION (newdecl)
1393 && (!DECL_TEMPLATE_INFO (olddecl)
68642fb6 1394 || (DECL_TI_TEMPLATE (olddecl)
75650646 1395 != DECL_TI_TEMPLATE (newdecl))))))
386b8a85
JM
1396 /* It's OK to have a template specialization and a non-template
1397 with the same type, or to have specializations of two
75650646
MM
1398 different templates with the same type. Note that if one is a
1399 specialization, and the other is an instantiation of the same
1400 template, that we do not exit at this point. That situation
1401 can occur if we instantiate a template class, and then
0e339752 1402 specialize one of its methods. This situation is valid, but
75650646
MM
1403 the declarations must be merged in the usual way. */
1404 return 0;
68642fb6
UD
1405 else if (TREE_CODE (newdecl) == FUNCTION_DECL
1406 && ((DECL_TEMPLATE_INSTANTIATION (olddecl)
75650646
MM
1407 && !DECL_USE_TEMPLATE (newdecl))
1408 || (DECL_TEMPLATE_INSTANTIATION (newdecl)
1409 && !DECL_USE_TEMPLATE (olddecl))))
1410 /* One of the declarations is a template instantiation, and the
1411 other is not a template at all. That's OK. */
386b8a85 1412 return 0;
85c6cbaf
ML
1413 else if (TREE_CODE (newdecl) == NAMESPACE_DECL
1414 && DECL_NAMESPACE_ALIAS (newdecl)
1415 && DECL_NAMESPACE_ALIAS (newdecl) == DECL_NAMESPACE_ALIAS (olddecl))
a1c65f9f 1416 /* Redeclaration of namespace alias, ignore it. */
85c6cbaf 1417 return 1;
8d08fdba
MS
1418 else
1419 {
d8e178a0 1420 const char *errmsg = redeclaration_error_message (newdecl, olddecl);
8251199e 1421 if (errmsg)
8d08fdba 1422 {
33bd39a2 1423 error (errmsg, newdecl);
8d08fdba
MS
1424 if (DECL_NAME (olddecl) != NULL_TREE)
1425 cp_error_at ((DECL_INITIAL (olddecl)
2c73f9f5 1426 && namespace_bindings_p ())
8251199e
JM
1427 ? "`%#D' previously defined here"
1428 : "`%#D' previously declared here", olddecl);
00fae85d 1429 return 0;
8d08fdba
MS
1430 }
1431 else if (TREE_CODE (olddecl) == FUNCTION_DECL
1432 && DECL_INITIAL (olddecl) != NULL_TREE
1433 && TYPE_ARG_TYPES (TREE_TYPE (olddecl)) == NULL_TREE
1434 && TYPE_ARG_TYPES (TREE_TYPE (newdecl)) != NULL_TREE)
1435 {
1436 /* Prototype decl follows defn w/o prototype. */
8251199e 1437 cp_warning_at ("prototype for `%#D'", newdecl);
ddd2d57e 1438 warning ("%Jfollows non-prototype definition here", olddecl);
8d08fdba
MS
1439 }
1440 else if (TREE_CODE (olddecl) == FUNCTION_DECL
1441 && DECL_LANGUAGE (newdecl) != DECL_LANGUAGE (olddecl))
8926095f
MS
1442 {
1443 /* extern "C" int foo ();
1444 int foo () { bar (); }
1445 is OK. */
46ccf50a 1446 if (current_lang_depth () == 0)
5d2ed28c 1447 SET_DECL_LANGUAGE (newdecl, DECL_LANGUAGE (olddecl));
8926095f
MS
1448 else
1449 {
8251199e 1450 cp_error_at ("previous declaration of `%#D' with %L linkage",
8926095f 1451 olddecl, DECL_LANGUAGE (olddecl));
33bd39a2 1452 error ("conflicts with new declaration with %L linkage",
8926095f
MS
1453 DECL_LANGUAGE (newdecl));
1454 }
1455 }
e1cd6e56 1456
a6f02587 1457 if (DECL_LANG_SPECIFIC (olddecl) && DECL_USE_TEMPLATE (olddecl))
c32381b1
JM
1458 ;
1459 else if (TREE_CODE (olddecl) == FUNCTION_DECL)
e1cd6e56
MS
1460 {
1461 tree t1 = TYPE_ARG_TYPES (TREE_TYPE (olddecl));
1462 tree t2 = TYPE_ARG_TYPES (TREE_TYPE (newdecl));
1463 int i = 1;
1464
1465 if (TREE_CODE (TREE_TYPE (newdecl)) == METHOD_TYPE)
1466 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2);
68642fb6 1467
e1cd6e56
MS
1468 for (; t1 && t1 != void_list_node;
1469 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2), i++)
1470 if (TREE_PURPOSE (t1) && TREE_PURPOSE (t2))
1471 {
2507f3b5
RK
1472 if (1 == simple_cst_equal (TREE_PURPOSE (t1),
1473 TREE_PURPOSE (t2)))
e1cd6e56 1474 {
624a5800
CR
1475 pedwarn ("default argument given for parameter %d of `%#D'",
1476 i, newdecl);
1477 cp_pedwarn_at ("after previous specification in `%#D'",
1478 olddecl);
e1cd6e56
MS
1479 }
1480 else
1481 {
33bd39a2 1482 error ("default argument given for parameter %d of `%#D'",
e1cd6e56 1483 i, newdecl);
8251199e 1484 cp_error_at ("after previous specification in `%#D'",
e1cd6e56
MS
1485 olddecl);
1486 }
1487 }
a5894242 1488
79065db2
MM
1489 if (DECL_DECLARED_INLINE_P (newdecl)
1490 && ! DECL_DECLARED_INLINE_P (olddecl)
7fcdf4c2 1491 && TREE_ADDRESSABLE (olddecl) && warn_inline)
dff6b454 1492 {
ddd2d57e
RH
1493 warning ("`%#D' was used before it was declared inline", newdecl);
1494 warning ("%Jprevious non-inline declaration here", olddecl);
dff6b454 1495 }
e1cd6e56 1496 }
8d08fdba
MS
1497 }
1498
4782bd5b
RS
1499 /* Do not merge an implicit typedef with an explicit one. In:
1500
1501 class A;
1502 ...
1503 typedef class A A __attribute__ ((foo));
1504
1505 the attribute should apply only to the typedef. */
1506 if (TREE_CODE (olddecl) == TYPE_DECL
1507 && (DECL_IMPLICIT_TYPEDEF_P (olddecl)
1508 || DECL_IMPLICIT_TYPEDEF_P (newdecl)))
1509 return 0;
1510
8d08fdba
MS
1511 /* If new decl is `static' and an `extern' was seen previously,
1512 warn about it. */
1513 warn_extern_redeclared_static (newdecl, olddecl);
1514
e92cc029 1515 /* We have committed to returning 1 at this point. */
8d08fdba
MS
1516 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1517 {
1518 /* Now that functions must hold information normally held
1519 by field decls, there is extra work to do so that
1520 declaration information does not get destroyed during
1521 definition. */
1522 if (DECL_VINDEX (olddecl))
1523 DECL_VINDEX (newdecl) = DECL_VINDEX (olddecl);
1524 if (DECL_CONTEXT (olddecl))
1525 DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
e1cd6e56
MS
1526 DECL_STATIC_CONSTRUCTOR (newdecl) |= DECL_STATIC_CONSTRUCTOR (olddecl);
1527 DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
fee7654e 1528 DECL_PURE_VIRTUAL_P (newdecl) |= DECL_PURE_VIRTUAL_P (olddecl);
864b83b9 1529 DECL_VIRTUAL_P (newdecl) |= DECL_VIRTUAL_P (olddecl);
4a67c9e9 1530 DECL_NEEDS_FINAL_OVERRIDER_P (newdecl) |= DECL_NEEDS_FINAL_OVERRIDER_P (olddecl);
963d5758 1531 DECL_THIS_STATIC (newdecl) |= DECL_THIS_STATIC (olddecl);
e0fff4b3
JM
1532 if (DECL_OVERLOADED_OPERATOR_P (olddecl) != ERROR_MARK)
1533 SET_OVERLOADED_OPERATOR_CODE
1534 (newdecl, DECL_OVERLOADED_OPERATOR_P (olddecl));
0b60dfe3 1535 new_defines_function = DECL_INITIAL (newdecl) != NULL_TREE;
68642fb6 1536
0b60dfe3
BK
1537 /* Optionally warn about more than one declaration for the same
1538 name, but don't warn about a function declaration followed by a
1539 definition. */
1540 if (warn_redundant_decls && ! DECL_ARTIFICIAL (olddecl)
1541 && !(new_defines_function && DECL_INITIAL (olddecl) == NULL_TREE)
a1c65f9f 1542 /* Don't warn about extern decl followed by definition. */
0b60dfe3 1543 && !(DECL_EXTERNAL (olddecl) && ! DECL_EXTERNAL (newdecl))
a1c65f9f 1544 /* Don't warn about friends, let add_friend take care of it. */
3581fae0 1545 && ! (DECL_FRIEND_P (newdecl) || DECL_FRIEND_P (olddecl)))
0b60dfe3 1546 {
33bd39a2 1547 warning ("redundant redeclaration of `%D' in same scope", newdecl);
8251199e 1548 cp_warning_at ("previous declaration of `%D'", olddecl);
0b60dfe3 1549 }
8d08fdba
MS
1550 }
1551
1552 /* Deal with C++: must preserve virtual function table size. */
1553 if (TREE_CODE (olddecl) == TYPE_DECL)
1554 {
1555 register tree newtype = TREE_TYPE (newdecl);
1556 register tree oldtype = TREE_TYPE (olddecl);
1557
1558 if (newtype != error_mark_node && oldtype != error_mark_node
1559 && TYPE_LANG_SPECIFIC (newtype) && TYPE_LANG_SPECIFIC (oldtype))
e93ee644
MM
1560 CLASSTYPE_FRIEND_CLASSES (newtype)
1561 = CLASSTYPE_FRIEND_CLASSES (oldtype);
7df87825 1562
970d6386 1563 DECL_ORIGINAL_TYPE (newdecl) = DECL_ORIGINAL_TYPE (olddecl);
8d08fdba
MS
1564 }
1565
8d08fdba
MS
1566 /* Copy all the DECL_... slots specified in the new decl
1567 except for any that we copy here from the old type. */
91d231cb 1568 DECL_ATTRIBUTES (newdecl)
f6897b10 1569 = (*targetm.merge_decl_attributes) (olddecl, newdecl);
8d08fdba 1570
5566b478
MS
1571 if (TREE_CODE (newdecl) == TEMPLATE_DECL)
1572 {
4d85e00e 1573 TREE_TYPE (olddecl) = TREE_TYPE (DECL_TEMPLATE_RESULT (olddecl));
68642fb6 1574 DECL_TEMPLATE_SPECIALIZATIONS (olddecl)
4d85e00e
MM
1575 = chainon (DECL_TEMPLATE_SPECIALIZATIONS (olddecl),
1576 DECL_TEMPLATE_SPECIALIZATIONS (newdecl));
68642fb6 1577
daa8df65
MM
1578 /* If the new declaration is a definition, update the file and
1579 line information on the declaration. */
06d5e633
NS
1580 if (DECL_INITIAL (DECL_TEMPLATE_RESULT (olddecl)) == NULL_TREE
1581 && DECL_INITIAL (DECL_TEMPLATE_RESULT (newdecl)) != NULL_TREE)
daa8df65 1582 {
f31686a3
RH
1583 DECL_SOURCE_LOCATION (olddecl)
1584 = DECL_SOURCE_LOCATION (DECL_TEMPLATE_RESULT (olddecl))
1585 = DECL_SOURCE_LOCATION (newdecl);
daa8df65
MM
1586 }
1587
5566b478
MS
1588 return 1;
1589 }
68642fb6 1590
8d08fdba
MS
1591 if (types_match)
1592 {
1593 /* Automatically handles default parameters. */
1594 tree oldtype = TREE_TYPE (olddecl);
e1cd6e56 1595 tree newtype;
8d08fdba 1596
e1cd6e56 1597 /* Merge the data types specified in the two decls. */
6da794e8 1598 newtype = merge_types (TREE_TYPE (newdecl), TREE_TYPE (olddecl));
e1cd6e56 1599
6da794e8 1600 /* If merge_types produces a non-typedef type, just use the old type. */
9076e292
JM
1601 if (TREE_CODE (newdecl) == TYPE_DECL
1602 && newtype == DECL_ORIGINAL_TYPE (newdecl))
1603 newtype = oldtype;
1604
8d08fdba 1605 if (TREE_CODE (newdecl) == VAR_DECL)
17bbb839
MM
1606 {
1607 DECL_THIS_EXTERN (newdecl) |= DECL_THIS_EXTERN (olddecl);
1608 DECL_INITIALIZED_P (newdecl) |= DECL_INITIALIZED_P (olddecl);
39703eb9
MM
1609 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (newdecl)
1610 |= DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (olddecl);
17bbb839
MM
1611 }
1612
6da794e8 1613 /* Do this after calling `merge_types' so that default
8d08fdba
MS
1614 parameters don't confuse us. */
1615 else if (TREE_CODE (newdecl) == FUNCTION_DECL
1616 && (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (newdecl))
1617 != TYPE_RAISES_EXCEPTIONS (TREE_TYPE (olddecl))))
1618 {
f30432d7 1619 TREE_TYPE (newdecl) = build_exception_variant (newtype,
8d08fdba 1620 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (newdecl)));
f30432d7 1621 TREE_TYPE (olddecl) = build_exception_variant (newtype,
8d08fdba
MS
1622 TYPE_RAISES_EXCEPTIONS (oldtype));
1623
9a224b4a 1624 if ((pedantic || ! DECL_IN_SYSTEM_HEADER (olddecl))
f31686a3 1625 && DECL_SOURCE_LINE (olddecl) != 0
da20811c 1626 && flag_exceptions
4cc1d462
NS
1627 && !comp_except_specs (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (newdecl)),
1628 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (olddecl)), 1))
8d08fdba 1629 {
33bd39a2 1630 error ("declaration of `%F' throws different exceptions",
f31686a3 1631 newdecl);
a09ba2e0 1632 cp_error_at ("than previous declaration `%F'", olddecl);
8d08fdba
MS
1633 }
1634 }
1635 TREE_TYPE (newdecl) = TREE_TYPE (olddecl) = newtype;
1636
1637 /* Lay the type out, unless already done. */
407f03b8 1638 if (! same_type_p (newtype, oldtype)
5566b478 1639 && TREE_TYPE (newdecl) != error_mark_node
5156628f 1640 && !(processing_template_decl && uses_template_parms (newdecl)))
b7484fbe
MS
1641 layout_type (TREE_TYPE (newdecl));
1642
5566b478
MS
1643 if ((TREE_CODE (newdecl) == VAR_DECL
1644 || TREE_CODE (newdecl) == PARM_DECL
1645 || TREE_CODE (newdecl) == RESULT_DECL
1646 || TREE_CODE (newdecl) == FIELD_DECL
1647 || TREE_CODE (newdecl) == TYPE_DECL)
5156628f 1648 && !(processing_template_decl && uses_template_parms (newdecl)))
b7484fbe 1649 layout_decl (newdecl, 0);
8d08fdba
MS
1650
1651 /* Merge the type qualifiers. */
1652 if (TREE_READONLY (newdecl))
1653 TREE_READONLY (olddecl) = 1;
1654 if (TREE_THIS_VOLATILE (newdecl))
1655 TREE_THIS_VOLATILE (olddecl) = 1;
1656
1657 /* Merge the initialization information. */
8926095f
MS
1658 if (DECL_INITIAL (newdecl) == NULL_TREE
1659 && DECL_INITIAL (olddecl) != NULL_TREE)
1660 {
1661 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
f31686a3 1662 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
b0d06515
MM
1663 if (CAN_HAVE_FULL_LANG_DECL_P (newdecl)
1664 && DECL_LANG_SPECIFIC (newdecl)
4d85e00e 1665 && DECL_LANG_SPECIFIC (olddecl))
69ee8086
RH
1666 {
1667 DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
1668 DECL_SAVED_INSNS (newdecl) = DECL_SAVED_INSNS (olddecl);
1669 }
8926095f 1670 }
39211cd5
MS
1671
1672 /* Merge the section attribute.
1673 We want to issue an error if the sections conflict but that must be
1674 done later in decl_attributes since we are called before attributes
1675 are assigned. */
1676 if (DECL_SECTION_NAME (newdecl) == NULL_TREE)
1677 DECL_SECTION_NAME (newdecl) = DECL_SECTION_NAME (olddecl);
1678
a157febd
GK
1679 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1680 {
1681 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
1682 |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
1331d16f 1683 DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
220a35cc
RS
1684 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1685 TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
1686 TREE_NOTHROW (newdecl) |= TREE_NOTHROW (olddecl);
1331d16f
RS
1687 DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
1688 DECL_IS_PURE (newdecl) |= DECL_IS_PURE (olddecl);
c740732f
MM
1689 /* Keep the old RTL. */
1690 COPY_DECL_RTL (olddecl, newdecl);
1691 }
1692 else if (TREE_CODE (newdecl) == VAR_DECL
1693 && (DECL_SIZE (olddecl) || !DECL_SIZE (newdecl)))
1694 {
1695 /* Keep the old RTL. We cannot keep the old RTL if the old
1696 declaration was for an incomplete object and the new
1697 declaration is not since many attributes of the RTL will
1698 change. */
1699 COPY_DECL_RTL (olddecl, newdecl);
a157febd 1700 }
8d08fdba
MS
1701 }
1702 /* If cannot merge, then use the new type and qualifiers,
1703 and don't preserve the old rtl. */
1704 else
1705 {
1706 /* Clean out any memory we had of the old declaration. */
1707 tree oldstatic = value_member (olddecl, static_aggregates);
1708 if (oldstatic)
1709 TREE_VALUE (oldstatic) = error_mark_node;
1710
1711 TREE_TYPE (olddecl) = TREE_TYPE (newdecl);
1712 TREE_READONLY (olddecl) = TREE_READONLY (newdecl);
1713 TREE_THIS_VOLATILE (olddecl) = TREE_THIS_VOLATILE (newdecl);
1714 TREE_SIDE_EFFECTS (olddecl) = TREE_SIDE_EFFECTS (newdecl);
1715 }
1716
1717 /* Merge the storage class information. */
45806a3f
FS
1718 merge_weak (newdecl, olddecl);
1719
e92cc029 1720 DECL_ONE_ONLY (newdecl) |= DECL_ONE_ONLY (olddecl);
794d4a61 1721 DECL_DEFER_OUTPUT (newdecl) |= DECL_DEFER_OUTPUT (olddecl);
a9aedbc2
MS
1722 TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
1723 TREE_STATIC (olddecl) = TREE_STATIC (newdecl) |= TREE_STATIC (olddecl);
1724 if (! DECL_EXTERNAL (olddecl))
1725 DECL_EXTERNAL (newdecl) = 0;
68642fb6 1726
0b60dfe3 1727 if (DECL_LANG_SPECIFIC (newdecl) && DECL_LANG_SPECIFIC (olddecl))
8d08fdba 1728 {
a9aedbc2
MS
1729 DECL_INTERFACE_KNOWN (newdecl) |= DECL_INTERFACE_KNOWN (olddecl);
1730 DECL_NOT_REALLY_EXTERN (newdecl) |= DECL_NOT_REALLY_EXTERN (olddecl);
77be6f82 1731 DECL_COMDAT (newdecl) |= DECL_COMDAT (olddecl);
68642fb6 1732 DECL_TEMPLATE_INSTANTIATED (newdecl)
2b0a63a3 1733 |= DECL_TEMPLATE_INSTANTIATED (olddecl);
0b60dfe3
BK
1734 /* Don't really know how much of the language-specific
1735 values we should copy from old to new. */
1736 DECL_IN_AGGR_P (newdecl) = DECL_IN_AGGR_P (olddecl);
e2500fed
GK
1737 DECL_LANG_SPECIFIC (newdecl)->decl_flags.u2 =
1738 DECL_LANG_SPECIFIC (olddecl)->decl_flags.u2;
0b60dfe3 1739 DECL_NONCONVERTING_P (newdecl) = DECL_NONCONVERTING_P (olddecl);
fbf1c34b 1740 DECL_TEMPLATE_INFO (newdecl) = DECL_TEMPLATE_INFO (olddecl);
3febd123
NS
1741 DECL_INITIALIZED_IN_CLASS_P (newdecl)
1742 |= DECL_INITIALIZED_IN_CLASS_P (olddecl);
0b60dfe3 1743 olddecl_friend = DECL_FRIEND_P (olddecl);
6a629cac
MM
1744
1745 /* Only functions have DECL_BEFRIENDING_CLASSES. */
1746 if (TREE_CODE (newdecl) == FUNCTION_DECL
1747 || DECL_FUNCTION_TEMPLATE_P (newdecl))
bb5e8a7f
MM
1748 {
1749 DECL_BEFRIENDING_CLASSES (newdecl)
1750 = chainon (DECL_BEFRIENDING_CLASSES (newdecl),
1751 DECL_BEFRIENDING_CLASSES (olddecl));
ca9e1382
KL
1752 /* DECL_THUNKS is only valid for virtual functions,
1753 otherwise it is a DECL_FRIEND_CONTEXT. */
1754 if (DECL_VIRTUAL_P (newdecl))
1755 DECL_THUNKS (newdecl) = DECL_THUNKS (olddecl);
bb5e8a7f 1756 }
8d08fdba
MS
1757 }
1758
8d08fdba
MS
1759 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1760 {
68642fb6
UD
1761 if (DECL_TEMPLATE_INSTANTIATION (olddecl)
1762 && !DECL_TEMPLATE_INSTANTIATION (newdecl))
75650646
MM
1763 {
1764 /* If newdecl is not a specialization, then it is not a
1765 template-related function at all. And that means that we
bf7c02dd 1766 should have exited above, returning 0. */
75650646
MM
1767 my_friendly_assert (DECL_TEMPLATE_SPECIALIZATION (newdecl),
1768 0);
1769
68642fb6 1770 if (TREE_USED (olddecl))
75650646 1771 /* From [temp.expl.spec]:
68642fb6 1772
75650646
MM
1773 If a template, a member template or the member of a class
1774 template is explicitly specialized then that
1775 specialization shall be declared before the first use of
1776 that specialization that would cause an implicit
1777 instantiation to take place, in every translation unit in
1778 which such a use occurs. */
33bd39a2 1779 error ("explicit specialization of %D after first use",
75650646
MM
1780 olddecl);
1781
1782 SET_DECL_TEMPLATE_SPECIALIZATION (olddecl);
d03d18e8
KL
1783
1784 /* [temp.expl.spec/14] We don't inline explicit specialization
1785 just because the primary template says so. */
75650646 1786 }
d03d18e8
KL
1787 else
1788 {
1789 if (DECL_PENDING_INLINE_INFO (newdecl) == 0)
1790 DECL_PENDING_INLINE_INFO (newdecl) = DECL_PENDING_INLINE_INFO (olddecl);
faae18ab 1791
d03d18e8 1792 DECL_DECLARED_INLINE_P (newdecl) |= DECL_DECLARED_INLINE_P (olddecl);
faae18ab 1793
d03d18e8
KL
1794 /* If either decl says `inline', this fn is inline, unless
1795 its definition was passed already. */
1796 if (DECL_INLINE (newdecl) && DECL_INITIAL (olddecl) == NULL_TREE)
1797 DECL_INLINE (olddecl) = 1;
1798 DECL_INLINE (newdecl) = DECL_INLINE (olddecl);
1799
1800 DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
1801 = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
1802 }
97055d5c 1803
5daf7c0a
JM
1804 /* Preserve abstractness on cloned [cd]tors. */
1805 DECL_ABSTRACT (newdecl) = DECL_ABSTRACT (olddecl);
1806
700f8a87
MS
1807 if (! types_match)
1808 {
5d2ed28c 1809 SET_DECL_LANGUAGE (olddecl, DECL_LANGUAGE (newdecl));
92643fea 1810 COPY_DECL_ASSEMBLER_NAME (newdecl, olddecl);
19e7881c 1811 SET_DECL_RTL (olddecl, DECL_RTL (newdecl));
5566b478
MS
1812 }
1813 if (! types_match || new_defines_function)
1814 {
6f1b4c42
JM
1815 /* These need to be copied so that the names are available.
1816 Note that if the types do match, we'll preserve inline
1817 info and other bits, but if not, we won't. */
700f8a87
MS
1818 DECL_ARGUMENTS (olddecl) = DECL_ARGUMENTS (newdecl);
1819 DECL_RESULT (olddecl) = DECL_RESULT (newdecl);
700f8a87 1820 }
8d08fdba
MS
1821 if (new_defines_function)
1822 /* If defining a function declared with other language
1823 linkage, use the previously declared language linkage. */
5d2ed28c 1824 SET_DECL_LANGUAGE (newdecl, DECL_LANGUAGE (olddecl));
6f1b4c42 1825 else if (types_match)
8d08fdba
MS
1826 {
1827 /* If redeclaring a builtin function, and not a definition,
1828 it stays built in. */
1829 if (DECL_BUILT_IN (olddecl))
1830 {
26db82d8 1831 DECL_BUILT_IN_CLASS (newdecl) = DECL_BUILT_IN_CLASS (olddecl);
39211cd5 1832 DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl);
8d08fdba
MS
1833 /* If we're keeping the built-in definition, keep the rtl,
1834 regardless of declaration matches. */
19e7881c 1835 SET_DECL_RTL (newdecl, DECL_RTL (olddecl));
8d08fdba
MS
1836 }
1837 else
084c1779 1838 DECL_ESTIMATED_INSNS (newdecl) = DECL_ESTIMATED_INSNS (olddecl);
8d08fdba
MS
1839
1840 DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
8d08fdba
MS
1841 /* Don't clear out the arguments if we're redefining a function. */
1842 if (DECL_ARGUMENTS (olddecl))
1843 DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl);
1844 }
1845 }
0b1161fc
MM
1846 else if (TREE_CODE (newdecl) == NAMESPACE_DECL)
1847 NAMESPACE_LEVEL (newdecl) = NAMESPACE_LEVEL (olddecl);
a9aedbc2 1848
8d08fdba
MS
1849 /* Now preserve various other info from the definition. */
1850 TREE_ADDRESSABLE (newdecl) = TREE_ADDRESSABLE (olddecl);
1851 TREE_ASM_WRITTEN (newdecl) = TREE_ASM_WRITTEN (olddecl);
1852 DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
92643fea 1853 COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
8d08fdba 1854
8d08fdba
MS
1855 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1856 {
1857 int function_size;
8d08fdba
MS
1858
1859 function_size = sizeof (struct tree_decl);
1860
730e1556
KG
1861 memcpy ((char *) olddecl + sizeof (struct tree_common),
1862 (char *) newdecl + sizeof (struct tree_common),
1863 function_size - sizeof (struct tree_common));
8d08fdba 1864
75650646 1865 if (DECL_TEMPLATE_INSTANTIATION (newdecl))
b3445994
MM
1866 /* If newdecl is a template instantiation, it is possible that
1867 the following sequence of events has occurred:
1868
1869 o A friend function was declared in a class template. The
1870 class template was instantiated.
1871
1872 o The instantiation of the friend declaration was
1873 recorded on the instantiation list, and is newdecl.
1874
1875 o Later, however, instantiate_class_template called pushdecl
1876 on the newdecl to perform name injection. But, pushdecl in
1877 turn called duplicate_decls when it discovered that another
1878 declaration of a global function with the same name already
1879 existed.
1880
1881 o Here, in duplicate_decls, we decided to clobber newdecl.
1882
1883 If we're going to do that, we'd better make sure that
1884 olddecl, and not newdecl, is on the list of
1885 instantiations so that if we try to do the instantiation
1886 again we won't get the clobbered declaration. */
1887 reregister_specialization (newdecl,
1888 DECL_TI_TEMPLATE (newdecl),
1889 olddecl);
8d08fdba
MS
1890 }
1891 else
1892 {
730e1556
KG
1893 memcpy ((char *) olddecl + sizeof (struct tree_common),
1894 (char *) newdecl + sizeof (struct tree_common),
1895 sizeof (struct tree_decl) - sizeof (struct tree_common)
2f9834e8 1896 + TREE_CODE_LENGTH (TREE_CODE (newdecl)) * sizeof (char *));
8d08fdba
MS
1897 }
1898
1899 DECL_UID (olddecl) = olddecl_uid;
1900 if (olddecl_friend)
1901 DECL_FRIEND_P (olddecl) = 1;
1902
d9525bec
BK
1903 /* NEWDECL contains the merged attribute lists.
1904 Update OLDDECL to be the same. */
91d231cb 1905 DECL_ATTRIBUTES (olddecl) = DECL_ATTRIBUTES (newdecl);
d9525bec 1906
16e31964
DS
1907 /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
1908 so that encode_section_info has a chance to look at the new decl
1909 flags and attributes. */
1910 if (DECL_RTL_SET_P (olddecl)
1911 && (TREE_CODE (olddecl) == FUNCTION_DECL
1912 || (TREE_CODE (olddecl) == VAR_DECL
1913 && TREE_STATIC (olddecl))))
1914 make_decl_rtl (olddecl, NULL);
1915
8d08fdba
MS
1916 return 1;
1917}
8d08fdba
MS
1918\f
1919/* Generate an implicit declaration for identifier FUNCTIONID
1920 as a function of type int (). Print a warning if appropriate. */
1921
1922tree
11f6b451 1923implicitly_declare (tree functionid)
8d08fdba
MS
1924{
1925 register tree decl;
8d08fdba
MS
1926
1927 /* We used to reuse an old implicit decl here,
1928 but this loses with inline functions because it can clobber
1929 the saved decl chains. */
1930 decl = build_lang_decl (FUNCTION_DECL, functionid, default_function_type);
1931
1932 DECL_EXTERNAL (decl) = 1;
1933 TREE_PUBLIC (decl) = 1;
1934
cab1f180 1935 /* ISO standard says implicit declarations are in the innermost block.
d22c8596 1936 So we record the decl in the standard fashion. */
8d08fdba 1937 pushdecl (decl);
3e411c3f 1938 rest_of_decl_compilation (decl, NULL, 0, 0);
8d08fdba
MS
1939
1940 if (warn_implicit
1941 /* Only one warning per identifier. */
1942 && IDENTIFIER_IMPLICIT_DECL (functionid) == NULL_TREE)
1943 {
33bd39a2 1944 pedwarn ("implicit declaration of function `%#D'", decl);
8d08fdba
MS
1945 }
1946
1947 SET_IDENTIFIER_IMPLICIT_DECL (functionid, decl);
1948
8d08fdba
MS
1949 return decl;
1950}
1951
8251199e 1952/* Return zero if the declaration NEWDECL is valid
8d08fdba
MS
1953 when the declaration OLDDECL (assumed to be for the same name)
1954 has already been seen.
1955 Otherwise return an error message format string with a %s
1956 where the identifier should go. */
1957
d8e178a0 1958static const char *
11f6b451 1959redeclaration_error_message (tree newdecl, tree olddecl)
8d08fdba
MS
1960{
1961 if (TREE_CODE (newdecl) == TYPE_DECL)
1962 {
1963 /* Because C++ can put things into name space for free,
1964 constructs like "typedef struct foo { ... } foo"
1965 would look like an erroneous redeclaration. */
3bfdc719 1966 if (same_type_p (TREE_TYPE (newdecl), TREE_TYPE (olddecl)))
8251199e 1967 return 0;
8d08fdba 1968 else
8251199e 1969 return "redefinition of `%#D'";
8d08fdba
MS
1970 }
1971 else if (TREE_CODE (newdecl) == FUNCTION_DECL)
1972 {
1973 /* If this is a pure function, its olddecl will actually be
1974 the original initialization to `0' (which we force to call
1975 abort()). Don't complain about redefinition in this case. */
fee7654e 1976 if (DECL_LANG_SPECIFIC (olddecl) && DECL_PURE_VIRTUAL_P (olddecl))
8251199e 1977 return 0;
8d08fdba 1978
2c73f9f5 1979 /* If both functions come from different namespaces, this is not
a1c65f9f 1980 a redeclaration - this is a conflict with a used function. */
6eb3bb27 1981 if (DECL_NAMESPACE_SCOPE_P (olddecl)
2c73f9f5 1982 && DECL_CONTEXT (olddecl) != DECL_CONTEXT (newdecl))
8251199e 1983 return "`%D' conflicts with used function";
2c73f9f5 1984
db5ae43f
MS
1985 /* We'll complain about linkage mismatches in
1986 warn_extern_redeclared_static. */
1987
2c73f9f5 1988 /* Defining the same name twice is no good. */
8d08fdba 1989 if (DECL_INITIAL (olddecl) != NULL_TREE
faae18ab 1990 && DECL_INITIAL (newdecl) != NULL_TREE)
8d08fdba
MS
1991 {
1992 if (DECL_NAME (olddecl) == NULL_TREE)
8251199e 1993 return "`%#D' not declared in class";
8d08fdba 1994 else
8251199e 1995 return "redefinition of `%#D'";
8d08fdba 1996 }
8251199e 1997 return 0;
8d08fdba 1998 }
51c184be
MS
1999 else if (TREE_CODE (newdecl) == TEMPLATE_DECL)
2000 {
ec255269 2001 if ((TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == FUNCTION_DECL
5362b086 2002 && (DECL_TEMPLATE_RESULT (newdecl)
f4381d62 2003 != DECL_TEMPLATE_RESULT (olddecl))
ec255269
MS
2004 && DECL_INITIAL (DECL_TEMPLATE_RESULT (newdecl))
2005 && DECL_INITIAL (DECL_TEMPLATE_RESULT (olddecl)))
2006 || (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL
d0f062fb
NS
2007 && COMPLETE_TYPE_P (TREE_TYPE (newdecl))
2008 && COMPLETE_TYPE_P (TREE_TYPE (olddecl))))
8251199e
JM
2009 return "redefinition of `%#D'";
2010 return 0;
51c184be 2011 }
1f51a992 2012 else if (toplevel_bindings_p () || DECL_NAMESPACE_SCOPE_P (newdecl))
8d08fdba
MS
2013 {
2014 /* Objects declared at top level: */
2015 /* If at least one is a reference, it's ok. */
2016 if (DECL_EXTERNAL (newdecl) || DECL_EXTERNAL (olddecl))
8251199e 2017 return 0;
8926095f 2018 /* Reject two definitions. */
8251199e 2019 return "redefinition of `%#D'";
8d08fdba
MS
2020 }
2021 else
2022 {
2023 /* Objects declared with block scope: */
2024 /* Reject two definitions, and reject a definition
2025 together with an external reference. */
2026 if (!(DECL_EXTERNAL (newdecl) && DECL_EXTERNAL (olddecl)))
8251199e
JM
2027 return "redeclaration of `%#D'";
2028 return 0;
8d08fdba
MS
2029 }
2030}
2031\f
acef433b 2032/* Create a new label, named ID. */
8d08fdba 2033
acef433b 2034static tree
11f6b451 2035make_label_decl (tree id, int local_p)
8d08fdba 2036{
acef433b 2037 tree decl;
8d08fdba 2038
acef433b 2039 decl = build_decl (LABEL_DECL, id, void_type_node);
acef433b
MM
2040
2041 DECL_CONTEXT (decl) = current_function_decl;
2042 DECL_MODE (decl) = VOIDmode;
2043 C_DECLARED_LABEL_FLAG (decl) = local_p;
8d08fdba 2044
acef433b
MM
2045 /* Say where one reference is to the label, for the sake of the
2046 error if it is not defined. */
f31686a3 2047 DECL_SOURCE_LOCATION (decl) = input_location;
acef433b
MM
2048
2049 /* Record the fact that this identifier is bound to this label. */
2050 SET_IDENTIFIER_LABEL_VALUE (id, decl);
2051
6625cdb5
JM
2052 return decl;
2053}
2054
2055/* Record this label on the list of used labels so that we can check
2056 at the end of the function to see whether or not the label was
2057 actually defined, and so we can check when the label is defined whether
2058 this use is valid. */
2059
2060static void
11f6b451 2061use_label (tree decl)
6625cdb5
JM
2062{
2063 if (named_label_uses == NULL
2064 || named_label_uses->names_in_scope != current_binding_level->names
2065 || named_label_uses->label_decl != decl)
2066 {
2067 struct named_label_use_list *new_ent;
c68b0a84 2068 new_ent = ggc_alloc (sizeof (struct named_label_use_list));
e349ee73
MS
2069 new_ent->label_decl = decl;
2070 new_ent->names_in_scope = current_binding_level->names;
2071 new_ent->binding_level = current_binding_level;
82a98427 2072 new_ent->o_goto_locus = input_location;
e349ee73
MS
2073 new_ent->next = named_label_uses;
2074 named_label_uses = new_ent;
8d08fdba 2075 }
acef433b 2076}
8d08fdba 2077
acef433b
MM
2078/* Look for a label named ID in the current function. If one cannot
2079 be found, create one. (We keep track of used, but undefined,
2080 labels, and complain about them at the end of a function.) */
8d08fdba 2081
68642fb6 2082tree
11f6b451 2083lookup_label (tree id)
acef433b
MM
2084{
2085 tree decl;
6625cdb5 2086 struct named_label_list *ent;
8d08fdba 2087
22ffcc6f 2088 timevar_push (TV_NAME_LOOKUP);
acef433b
MM
2089 /* You can't use labels at global scope. */
2090 if (current_function_decl == NULL_TREE)
2091 {
2092 error ("label `%s' referenced outside of any function",
2093 IDENTIFIER_POINTER (id));
22ffcc6f 2094 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
acef433b 2095 }
68642fb6 2096
acef433b
MM
2097 /* See if we've already got this label. */
2098 decl = IDENTIFIER_LABEL_VALUE (id);
2099 if (decl != NULL_TREE && DECL_CONTEXT (decl) == current_function_decl)
22ffcc6f 2100 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
8d08fdba 2101
acef433b
MM
2102 /* Record this label on the list of labels used in this function.
2103 We do this before calling make_label_decl so that we get the
2104 IDENTIFIER_LABEL_VALUE before the new label is declared. */
c68b0a84 2105 ent = ggc_alloc_cleared (sizeof (struct named_label_list));
6625cdb5
JM
2106 ent->old_value = IDENTIFIER_LABEL_VALUE (id);
2107 ent->next = named_labels;
2108 named_labels = ent;
2109
acef433b
MM
2110 /* We need a new label. */
2111 decl = make_label_decl (id, /*local_p=*/0);
6625cdb5 2112
acef433b 2113 /* Now fill in the information we didn't have before. */
6625cdb5 2114 ent->label_decl = decl;
8d08fdba 2115
22ffcc6f 2116 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
8d08fdba
MS
2117}
2118
acef433b 2119/* Declare a local label named ID. */
8d08fdba
MS
2120
2121tree
11f6b451 2122declare_local_label (tree id)
8d08fdba 2123{
acef433b 2124 tree decl;
8d08fdba 2125
acef433b
MM
2126 /* Add a new entry to the SHADOWED_LABELS list so that when we leave
2127 this scope we can restore the old value of
2128 IDENTIFIER_TYPE_VALUE. */
68642fb6 2129 current_binding_level->shadowed_labels
acef433b
MM
2130 = tree_cons (IDENTIFIER_LABEL_VALUE (id), NULL_TREE,
2131 current_binding_level->shadowed_labels);
2132 /* Look for the label. */
2133 decl = make_label_decl (id, /*local_p=*/1);
2134 /* Now fill in the information we didn't have before. */
2135 TREE_VALUE (current_binding_level->shadowed_labels) = decl;
68642fb6 2136
acef433b 2137 return decl;
8d08fdba
MS
2138}
2139
6625cdb5
JM
2140/* Returns nonzero if it is ill-formed to jump past the declaration of
2141 DECL. Returns 2 if it's also a real problem. */
2142
2143static int
11f6b451 2144decl_jump_unsafe (tree decl)
6625cdb5
JM
2145{
2146 if (TREE_CODE (decl) != VAR_DECL || TREE_STATIC (decl))
2147 return 0;
2148
2149 if (DECL_INITIAL (decl) == NULL_TREE
2150 && pod_type_p (TREE_TYPE (decl)))
2151 return 0;
2152
2153 /* This is really only important if we're crossing an initialization.
2154 The POD stuff is just pedantry; why should it matter if the class
2155 contains a field of pointer to member type? */
2156 if (DECL_INITIAL (decl)
2157 || (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl))))
2158 return 2;
2159 return 1;
2160}
2161
2162/* Check that a single previously seen jump to a newly defined label
2163 is OK. DECL is the LABEL_DECL or 0; LEVEL is the binding_level for
2164 the jump context; NAMES are the names in scope in LEVEL at the jump
2165 context; FILE and LINE are the source position of the jump or 0. */
2166
2167static void
11f6b451
NN
2168check_previous_goto_1 (tree decl,
2169 struct cp_binding_level* level,
152c16a9 2170 tree names, const location_t *locus)
6625cdb5
JM
2171{
2172 int identified = 0;
2173 int saw_eh = 0;
e2500fed 2174 struct cp_binding_level *b = current_binding_level;
6625cdb5
JM
2175 for (; b; b = b->level_chain)
2176 {
2177 tree new_decls = b->names;
2178 tree old_decls = (b == level ? names : NULL_TREE);
2179 for (; new_decls != old_decls;
2180 new_decls = TREE_CHAIN (new_decls))
2181 {
2182 int problem = decl_jump_unsafe (new_decls);
2183 if (! problem)
2184 continue;
2185
2186 if (! identified)
2187 {
2188 if (decl)
33bd39a2 2189 pedwarn ("jump to label `%D'", decl);
6625cdb5
JM
2190 else
2191 pedwarn ("jump to case label");
2192
152c16a9
GDR
2193 if (locus)
2194 pedwarn ("%H from here", locus);
6625cdb5
JM
2195 identified = 1;
2196 }
2197
826840d9 2198 if (problem > 1)
6625cdb5
JM
2199 cp_error_at (" crosses initialization of `%#D'",
2200 new_decls);
2201 else
2202 cp_pedwarn_at (" enters scope of non-POD `%#D'",
2203 new_decls);
2204 }
2205
2206 if (b == level)
2207 break;
a7e8c268 2208 if ((b->kind == sk_try || b->kind == sk_catch) && ! saw_eh)
6625cdb5
JM
2209 {
2210 if (! identified)
2211 {
2212 if (decl)
33bd39a2 2213 pedwarn ("jump to label `%D'", decl);
6625cdb5
JM
2214 else
2215 pedwarn ("jump to case label");
2216
152c16a9
GDR
2217 if (locus)
2218 pedwarn ("%H from here", locus);
6625cdb5
JM
2219 identified = 1;
2220 }
a7e8c268 2221 if (b->kind == sk_try)
826840d9
RH
2222 error (" enters try block");
2223 else
2224 error (" enters catch block");
6625cdb5
JM
2225 saw_eh = 1;
2226 }
2227 }
2228}
2229
2230static void
11f6b451 2231check_previous_goto (struct named_label_use_list* use)
6625cdb5
JM
2232{
2233 check_previous_goto_1 (use->label_decl, use->binding_level,
152c16a9 2234 use->names_in_scope, &use->o_goto_locus);
6625cdb5
JM
2235}
2236
2237static void
11f6b451 2238check_switch_goto (struct cp_binding_level* level)
6625cdb5 2239{
152c16a9 2240 check_previous_goto_1 (NULL_TREE, level, level->names, NULL);
6625cdb5
JM
2241}
2242
2243/* Check that any previously seen jumps to a newly defined label DECL
2244 are OK. Called by define_label. */
2245
2246static void
11f6b451 2247check_previous_gotos (tree decl)
6625cdb5
JM
2248{
2249 struct named_label_use_list **usep;
2250
2251 if (! TREE_USED (decl))
2252 return;
2253
2254 for (usep = &named_label_uses; *usep; )
2255 {
2256 struct named_label_use_list *use = *usep;
2257 if (use->label_decl == decl)
2258 {
2259 check_previous_goto (use);
2260 *usep = use->next;
2261 }
2262 else
2263 usep = &(use->next);
2264 }
2265}
2266
2267/* Check that a new jump to a label DECL is OK. Called by
2268 finish_goto_stmt. */
2269
2270void
11f6b451 2271check_goto (tree decl)
6625cdb5
JM
2272{
2273 int identified = 0;
2274 tree bad;
2275 struct named_label_list *lab;
2276
e3cd9945
APB
2277 /* We can't know where a computed goto is jumping. So we assume
2278 that it's OK. */
2279 if (! DECL_P (decl))
2280 return;
2281
6625cdb5
JM
2282 /* If the label hasn't been defined yet, defer checking. */
2283 if (! DECL_INITIAL (decl))
2284 {
2285 use_label (decl);
2286 return;
2287 }
2288
2289 for (lab = named_labels; lab; lab = lab->next)
2290 if (decl == lab->label_decl)
2291 break;
2292
2293 /* If the label is not on named_labels it's a gcc local label, so
2294 it must be in an outer scope, so jumping to it is always OK. */
2295 if (lab == 0)
2296 return;
2297
826840d9
RH
2298 if ((lab->in_try_scope || lab->in_catch_scope || lab->bad_decls)
2299 && !identified)
6625cdb5
JM
2300 {
2301 cp_pedwarn_at ("jump to label `%D'", decl);
2302 pedwarn (" from here");
2303 identified = 1;
2304 }
2305
2306 for (bad = lab->bad_decls; bad; bad = TREE_CHAIN (bad))
2307 {
2308 tree b = TREE_VALUE (bad);
2309 int u = decl_jump_unsafe (b);
2310
2311 if (u > 1 && DECL_ARTIFICIAL (b))
2312 /* Can't skip init of __exception_info. */
ddd2d57e 2313 error ("%J enters catch block", b);
6625cdb5
JM
2314 else if (u > 1)
2315 cp_error_at (" skips initialization of `%#D'", b);
2316 else
2317 cp_pedwarn_at (" enters scope of non-POD `%#D'", b);
2318 }
2319
826840d9 2320 if (lab->in_try_scope)
6625cdb5 2321 error (" enters try block");
826840d9
RH
2322 else if (lab->in_catch_scope)
2323 error (" enters catch block");
6625cdb5
JM
2324}
2325
8d08fdba 2326/* Define a label, specifying the location in the source file.
b9c87401 2327 Return the LABEL_DECL node for the label. */
8d08fdba
MS
2328
2329tree
5b030314 2330define_label (location_t location, tree name)
8d08fdba 2331{
f01b0acb 2332 tree decl = lookup_label (name);
6625cdb5 2333 struct named_label_list *ent;
e2500fed 2334 register struct cp_binding_level *p;
6625cdb5 2335
22ffcc6f 2336 timevar_push (TV_NAME_LOOKUP);
6625cdb5
JM
2337 for (ent = named_labels; ent; ent = ent->next)
2338 if (ent->label_decl == decl)
2339 break;
8d08fdba 2340
3dc9aec6 2341 /* After labels, make any new cleanups in the function go into their
8d08fdba 2342 own new (temporary) binding contour. */
a7e8c268
MM
2343 for (p = current_binding_level;
2344 p->kind != sk_function_parms;
2345 p = p->level_chain)
3dc9aec6 2346 p->more_cleanups_ok = 0;
8d08fdba 2347
e1cd6e56 2348 if (name == get_identifier ("wchar_t"))
33bd39a2 2349 pedwarn ("label named wchar_t");
e1cd6e56 2350
8d08fdba 2351 if (DECL_INITIAL (decl) != NULL_TREE)
b9c87401 2352 error ("duplicate label `%D'", decl);
8d08fdba
MS
2353 else
2354 {
8d08fdba
MS
2355 /* Mark label as having been defined. */
2356 DECL_INITIAL (decl) = error_mark_node;
2357 /* Say where in the source. */
f31686a3 2358 DECL_SOURCE_LOCATION (decl) = location;
6625cdb5
JM
2359 if (ent)
2360 {
2361 ent->names_in_scope = current_binding_level->names;
2362 ent->binding_level = current_binding_level;
2363 }
2364 check_previous_gotos (decl);
8d08fdba 2365 }
b9c87401 2366
22ffcc6f 2367 timevar_pop (TV_NAME_LOOKUP);
b9c87401 2368 return decl;
8d08fdba
MS
2369}
2370
a5894242
MS
2371struct cp_switch
2372{
e2500fed 2373 struct cp_binding_level *level;
a5894242 2374 struct cp_switch *next;
56cb9733
MM
2375 /* The SWITCH_STMT being built. */
2376 tree switch_stmt;
2377 /* A splay-tree mapping the low element of a case range to the high
2378 element, or NULL_TREE if there is no high element. Used to
2379 determine whether or not a new case label duplicates an old case
2380 label. We need a tree, rather than simply a hash table, because
2381 of the GNU case range extension. */
2382 splay_tree cases;
a5894242
MS
2383};
2384
56cb9733
MM
2385/* A stack of the currently active switch statements. The innermost
2386 switch statement is on the top of the stack. There is no need to
2387 mark the stack for garbage collection because it is only active
2388 during the processing of the body of a function, and we never
2389 collect at that point. */
5362b086 2390
a5894242
MS
2391static struct cp_switch *switch_stack;
2392
56cb9733
MM
2393/* Called right after a switch-statement condition is parsed.
2394 SWITCH_STMT is the switch statement being parsed. */
2395
a5894242 2396void
11f6b451 2397push_switch (tree switch_stmt)
a5894242 2398{
c68b0a84 2399 struct cp_switch *p = xmalloc (sizeof (struct cp_switch));
a5894242
MS
2400 p->level = current_binding_level;
2401 p->next = switch_stack;
56cb9733
MM
2402 p->switch_stmt = switch_stmt;
2403 p->cases = splay_tree_new (case_compare, NULL, NULL);
a5894242
MS
2404 switch_stack = p;
2405}
2406
2407void
11f6b451 2408pop_switch (void)
a5894242 2409{
bedda2da 2410 struct cp_switch *cs;
5362b086 2411
bedda2da 2412 cs = switch_stack;
56cb9733 2413 splay_tree_delete (cs->cases);
a5894242 2414 switch_stack = switch_stack->next;
bedda2da 2415 free (cs);
a5894242
MS
2416}
2417
b0a1da19
JM
2418/* Note that we've seen a definition of a case label, and complain if this
2419 is a bad place for one. */
e92cc029 2420
3e4d04a1 2421tree
11f6b451 2422finish_case_label (tree low_value, tree high_value)
8d08fdba 2423{
3e4d04a1 2424 tree cond, r;
e2500fed 2425 register struct cp_binding_level *p;
a5894242 2426
b0a1da19 2427 if (! switch_stack)
56cb9733
MM
2428 {
2429 if (high_value)
2430 error ("case label not within a switch statement");
2431 else if (low_value)
33bd39a2 2432 error ("case label `%E' not within a switch statement",
56cb9733
MM
2433 low_value);
2434 else
2435 error ("`default' label not within a switch statement");
3e4d04a1 2436 return NULL_TREE;
56cb9733
MM
2437 }
2438
56cb9733
MM
2439 if (processing_template_decl)
2440 {
8f17b5c5
MM
2441 tree label;
2442
56cb9733
MM
2443 /* For templates, just add the case label; we'll do semantic
2444 analysis at instantiation-time. */
8f17b5c5 2445 label = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE);
3e4d04a1 2446 return add_stmt (build_case_label (low_value, high_value, label));
56cb9733
MM
2447 }
2448
2449 /* Find the condition on which this switch statement depends. */
2450 cond = SWITCH_COND (switch_stack->switch_stmt);
2451 if (cond && TREE_CODE (cond) == TREE_LIST)
2452 cond = TREE_VALUE (cond);
56cb9733 2453
3e4d04a1 2454 r = c_add_case_label (switch_stack->cases, cond, low_value, high_value);
8d08fdba 2455
6625cdb5 2456 check_switch_goto (switch_stack->level);
a5894242 2457
3dc9aec6 2458 /* After labels, make any new cleanups in the function go into their
8d08fdba 2459 own new (temporary) binding contour. */
a7e8c268
MM
2460 for (p = current_binding_level;
2461 p->kind != sk_function_parms;
2462 p = p->level_chain)
3dc9aec6 2463 p->more_cleanups_ok = 0;
3e4d04a1
RH
2464
2465 return r;
8d08fdba
MS
2466}
2467\f
7ddedda4
MM
2468/* Hash a TYPENAME_TYPE. K is really of type `tree'. */
2469
e2500fed 2470static hashval_t
11f6b451 2471typename_hash (const void* k)
7ddedda4 2472{
e2500fed
GK
2473 hashval_t hash;
2474 tree t = (tree) k;
7ddedda4 2475
7bdfd72e
KG
2476 hash = (htab_hash_pointer (TYPE_CONTEXT (t))
2477 ^ htab_hash_pointer (DECL_NAME (TYPE_NAME (t))));
7ddedda4
MM
2478
2479 return hash;
2480}
2481
2482/* Compare two TYPENAME_TYPEs. K1 and K2 are really of type `tree'. */
2483
e2500fed 2484static int
11f6b451 2485typename_compare (const void * k1, const void * k2)
7ddedda4
MM
2486{
2487 tree t1;
2488 tree t2;
2489 tree d1;
2490 tree d2;
2491
2492 t1 = (tree) k1;
2493 t2 = (tree) k2;
2494 d1 = TYPE_NAME (t1);
2495 d2 = TYPE_NAME (t2);
68642fb6 2496
7ddedda4 2497 return (DECL_NAME (d1) == DECL_NAME (d2)
e2500fed 2498 && TYPE_CONTEXT (t1) == TYPE_CONTEXT (t2)
68642fb6 2499 && ((TREE_TYPE (t1) != NULL_TREE)
7ddedda4
MM
2500 == (TREE_TYPE (t2) != NULL_TREE))
2501 && same_type_p (TREE_TYPE (t1), TREE_TYPE (t2))
2502 && TYPENAME_TYPE_FULLNAME (t1) == TYPENAME_TYPE_FULLNAME (t2));
2503}
2504
45869a6c
MM
2505/* Build a TYPENAME_TYPE. If the type is `typename T::t', CONTEXT is
2506 the type of `T', NAME is the IDENTIFIER_NODE for `t'. If BASE_TYPE
2507 is non-NULL, this type is being created by the implicit typename
2508 extension, and BASE_TYPE is a type named `t' in some base class of
68642fb6 2509 `T' which depends on template parameters.
45869a6c
MM
2510
2511 Returns the new TYPENAME_TYPE. */
2512
e2500fed
GK
2513static GTY ((param_is (union tree_node))) htab_t typename_htab;
2514
8ce33230 2515static tree
8fbc5ae7 2516build_typename_type (tree context, tree name, tree fullname)
45869a6c
MM
2517{
2518 tree t;
2519 tree d;
fad205ff 2520 void **e;
45869a6c 2521
e2500fed 2522 if (typename_htab == NULL)
9cd64686 2523 {
e2500fed
GK
2524 typename_htab = htab_create_ggc (61, &typename_hash,
2525 &typename_compare, NULL);
9cd64686 2526 }
45869a6c
MM
2527
2528 /* Build the TYPENAME_TYPE. */
33848bb0 2529 t = make_aggr_type (TYPENAME_TYPE);
45869a6c
MM
2530 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
2531 TYPENAME_TYPE_FULLNAME (t) = fullname;
45869a6c
MM
2532
2533 /* Build the corresponding TYPE_DECL. */
2534 d = build_decl (TYPE_DECL, name, t);
2535 TYPE_NAME (TREE_TYPE (d)) = d;
2536 TYPE_STUB_DECL (TREE_TYPE (d)) = d;
2537 DECL_CONTEXT (d) = FROB_CONTEXT (context);
2642b9bf 2538 DECL_ARTIFICIAL (d) = 1;
45869a6c 2539
7ddedda4 2540 /* See if we already have this type. */
e2500fed
GK
2541 e = htab_find_slot (typename_htab, t, INSERT);
2542 if (*e)
2543 t = (tree) *e;
7ddedda4 2544 else
e2500fed 2545 *e = t;
7ddedda4 2546
45869a6c
MM
2547 return t;
2548}
2549
3baa501d
MM
2550/* Resolve `typename CONTEXT::NAME'. Returns an appropriate type,
2551 unless an error occurs, in which case error_mark_node is returned.
4f2b0fb2
NS
2552 If we locate a non-artificial TYPE_DECL and TF_KEEP_TYPE_DECL is
2553 set, we return that, rather than the _TYPE it corresponds to, in
2554 other cases we look through the type decl. If TF_ERROR is set,
2555 complain about errors, otherwise be quiet. */
3baa501d 2556
5566b478 2557tree
11f6b451 2558make_typename_type (tree context, tree name, tsubst_flags_t complain)
5566b478 2559{
b2b7d40a 2560 tree fullname;
a80e4195 2561
5dc5d13c
KL
2562 if (name == error_mark_node
2563 || context == NULL_TREE
2564 || context == error_mark_node)
2565 return error_mark_node;
2566
2f939d94 2567 if (TYPE_P (name))
78638e24 2568 {
68642fb6
UD
2569 if (!(TYPE_LANG_SPECIFIC (name)
2570 && (CLASSTYPE_IS_TEMPLATE (name)
78638e24
MM
2571 || CLASSTYPE_USE_TEMPLATE (name))))
2572 name = TYPE_IDENTIFIER (name);
2573 else
2574 /* Create a TEMPLATE_ID_EXPR for the type. */
2575 name = build_nt (TEMPLATE_ID_EXPR,
2576 CLASSTYPE_TI_TEMPLATE (name),
2577 CLASSTYPE_TI_ARGS (name));
2578 }
653cc74a 2579 else if (TREE_CODE (name) == TYPE_DECL)
a80e4195 2580 name = DECL_NAME (name);
b2b7d40a
JM
2581
2582 fullname = name;
2583
2584 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
11686454
JM
2585 {
2586 name = TREE_OPERAND (name, 0);
2587 if (TREE_CODE (name) == TEMPLATE_DECL)
2588 name = TREE_OPERAND (fullname, 0) = DECL_NAME (name);
2589 }
8a2b77e7
JM
2590 if (TREE_CODE (name) == TEMPLATE_DECL)
2591 {
33bd39a2 2592 error ("`%D' used without template parameters", name);
8a2b77e7
JM
2593 return error_mark_node;
2594 }
5d872564
NS
2595 my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, 20030802);
2596
04ddee1b
BK
2597 if (TREE_CODE (context) == NAMESPACE_DECL)
2598 {
2599 /* We can get here from typename_sub0 in the explicit_template_type
2600 expansion. Just fail. */
4f2b0fb2 2601 if (complain & tf_error)
33bd39a2 2602 error ("no class template named `%#T' in `%#T'",
3baa501d 2603 name, context);
04ddee1b
BK
2604 return error_mark_node;
2605 }
2606
9579624e 2607 if (!dependent_type_p (context)
b77ead33 2608 || currently_open_class (context))
5566b478 2609 {
b2b7d40a
JM
2610 if (TREE_CODE (fullname) == TEMPLATE_ID_EXPR)
2611 {
ad810b22 2612 tree tmpl = NULL_TREE;
b2b7d40a 2613 if (IS_AGGR_TYPE (context))
86ac0575 2614 tmpl = lookup_field (context, name, 0, false);
ad810b22 2615 if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl))
b2b7d40a 2616 {
4f2b0fb2 2617 if (complain & tf_error)
33bd39a2 2618 error ("no class template named `%#T' in `%#T'",
3baa501d 2619 name, context);
b2b7d40a
JM
2620 return error_mark_node;
2621 }
ffb690bd 2622
2b59fc25 2623 if (complain & tf_error)
6df5158a 2624 perform_or_defer_access_check (TYPE_BINFO (context), tmpl);
ba59df78 2625
68642fb6 2626 return lookup_template_class (tmpl,
ad810b22 2627 TREE_OPERAND (fullname, 1),
68642fb6 2628 NULL_TREE, context,
f9c244b8 2629 /*entering_scope=*/0,
42eaed49 2630 tf_error | tf_warning | tf_user);
b2b7d40a
JM
2631 }
2632 else
5566b478 2633 {
b4f70b3d 2634 tree t;
68642fb6 2635
b4f70b3d 2636 if (!IS_AGGR_TYPE (context))
b2b7d40a 2637 {
4f2b0fb2 2638 if (complain & tf_error)
33bd39a2 2639 error ("no type named `%#T' in `%#T'", name, context);
b2b7d40a
JM
2640 return error_mark_node;
2641 }
1107c4b3 2642
86ac0575 2643 t = lookup_field (context, name, 0, true);
7d4bdeed 2644 if (t)
4f2b0fb2 2645 {
8c081e84
MM
2646 if (TREE_CODE (t) != TYPE_DECL)
2647 {
2648 if (complain & tf_error)
2649 error ("no type named `%#T' in `%#T'", name, context);
2650 return error_mark_node;
2651 }
2652
2b59fc25 2653 if (complain & tf_error)
6df5158a 2654 perform_or_defer_access_check (TYPE_BINFO (context), t);
ba59df78 2655
4f2b0fb2
NS
2656 if (DECL_ARTIFICIAL (t) || !(complain & tf_keep_type_decl))
2657 t = TREE_TYPE (t);
ba3307c0 2658
4f2b0fb2
NS
2659 return t;
2660 }
5566b478 2661 }
5566b478 2662 }
11249cf0
MM
2663
2664 /* If the CONTEXT is not a template type, then either the field is
2665 there now or its never going to be. */
9579624e 2666 if (!dependent_type_p (context))
11249cf0 2667 {
4f2b0fb2 2668 if (complain & tf_error)
33bd39a2 2669 error ("no type named `%#T' in `%#T'", name, context);
11249cf0
MM
2670 return error_mark_node;
2671 }
68642fb6 2672
8fbc5ae7 2673 return build_typename_type (context, name, fullname);
5566b478
MS
2674}
2675
b8c6534b
KL
2676/* Resolve `CONTEXT::template NAME'. Returns an appropriate type,
2677 unless an error occurs, in which case error_mark_node is returned.
4f2b0fb2
NS
2678 If we locate a TYPE_DECL, we return that, rather than the _TYPE it
2679 corresponds to. If COMPLAIN zero, don't complain about any errors
2680 that occur. */
b8c6534b
KL
2681
2682tree
11f6b451 2683make_unbound_class_template (tree context, tree name, tsubst_flags_t complain)
b8c6534b
KL
2684{
2685 tree t;
2686 tree d;
2687
2688 if (TYPE_P (name))
2689 name = TYPE_IDENTIFIER (name);
2690 else if (DECL_P (name))
2691 name = DECL_NAME (name);
2692 if (TREE_CODE (name) != IDENTIFIER_NODE)
a98facb0 2693 abort ();
b8c6534b 2694
9579624e 2695 if (!dependent_type_p (context)
b8c6534b
KL
2696 || currently_open_class (context))
2697 {
2698 tree tmpl = NULL_TREE;
2699
2700 if (IS_AGGR_TYPE (context))
86ac0575 2701 tmpl = lookup_field (context, name, 0, false);
b8c6534b
KL
2702
2703 if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl))
2704 {
4f09be91 2705 if (complain & tf_error)
33bd39a2 2706 error ("no class template named `%#T' in `%#T'", name, context);
b8c6534b
KL
2707 return error_mark_node;
2708 }
2709
2b59fc25 2710 if (complain & tf_error)
6df5158a 2711 perform_or_defer_access_check (TYPE_BINFO (context), tmpl);
b8c6534b
KL
2712
2713 return tmpl;
2714 }
2715
2716 /* Build the UNBOUND_CLASS_TEMPLATE. */
2717 t = make_aggr_type (UNBOUND_CLASS_TEMPLATE);
2718 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
2719 TREE_TYPE (t) = NULL_TREE;
2720
2721 /* Build the corresponding TEMPLATE_DECL. */
2722 d = build_decl (TEMPLATE_DECL, name, t);
2723 TYPE_NAME (TREE_TYPE (d)) = d;
2724 TYPE_STUB_DECL (TREE_TYPE (d)) = d;
2725 DECL_CONTEXT (d) = FROB_CONTEXT (context);
2726 DECL_ARTIFICIAL (d) = 1;
2727
2728 return t;
2729}
2730
8d08fdba 2731\f
47aa0df4
MM
2732
2733/* A chain of TYPE_DECLs for the builtin types. */
2734
2735static GTY(()) tree builtin_type_decls;
2736
2737/* Return a chain of TYPE_DECLs for the builtin types. */
2738
2739tree
edaf3e03 2740cxx_builtin_type_decls (void)
47aa0df4
MM
2741{
2742 return builtin_type_decls;
2743}
2744
8d08fdba 2745/* Push the declarations of builtin types into the namespace.
4b0d3cbe
MM
2746 RID_INDEX is the index of the builtin type in the array
2747 RID_POINTERS. NAME is the name used when looking up the builtin
2748 type. TYPE is the _TYPE node for the builtin type. */
8d08fdba 2749
eaa7c03f 2750void
11f6b451
NN
2751record_builtin_type (enum rid rid_index,
2752 const char* name,
2753 tree type)
8d08fdba
MS
2754{
2755 tree rname = NULL_TREE, tname = NULL_TREE;
a703fb38 2756 tree tdecl = NULL_TREE;
8d08fdba 2757
0e5921e8 2758 if ((int) rid_index < (int) RID_MAX)
8d08fdba
MS
2759 rname = ridpointers[(int) rid_index];
2760 if (name)
2761 tname = get_identifier (name);
2762
4b0d3cbe
MM
2763 /* The calls to SET_IDENTIFIER_GLOBAL_VALUE below should be
2764 eliminated. Built-in types should not be looked up name; their
2765 names are keywords that the parser can recognize. However, there
2766 is code in c-common.c that uses identifier_global_value to look
2767 up built-in types by name. */
8d08fdba
MS
2768 if (tname)
2769 {
4b0d3cbe
MM
2770 tdecl = build_decl (TYPE_DECL, tname, type);
2771 DECL_ARTIFICIAL (tdecl) = 1;
a7e8c268 2772 SET_IDENTIFIER_GLOBAL_VALUE (tname, tdecl);
8d08fdba 2773 }
4b0d3cbe 2774 if (rname)
8d08fdba 2775 {
4b0d3cbe 2776 if (!tdecl)
8d08fdba 2777 {
4b0d3cbe
MM
2778 tdecl = build_decl (TYPE_DECL, rname, type);
2779 DECL_ARTIFICIAL (tdecl) = 1;
8d08fdba 2780 }
4b0d3cbe 2781 SET_IDENTIFIER_GLOBAL_VALUE (rname, tdecl);
8d08fdba 2782 }
4b0d3cbe
MM
2783
2784 if (!TYPE_NAME (type))
2785 TYPE_NAME (type) = tdecl;
47aa0df4
MM
2786
2787 if (tdecl)
2788 {
2789 TREE_CHAIN (tdecl) = builtin_type_decls;
2790 builtin_type_decls = tdecl;
2791 }
8d08fdba
MS
2792}
2793
eff71ab0 2794/* Record one of the standard Java types.
4d8a1dd6
MM
2795 * Declare it as having the given NAME.
2796 * If SIZE > 0, it is the size of one of the integral types;
2797 * otherwise it is the negative of the size of one of the other types. */
eff71ab0
PB
2798
2799static tree
11f6b451 2800record_builtin_java_type (const char* name, int size)
eff71ab0
PB
2801{
2802 tree type, decl;
2803 if (size > 0)
2804 type = make_signed_type (size);
2805 else if (size > -32)
a1c65f9f 2806 { /* "__java_char" or ""__java_boolean". */
eff71ab0
PB
2807 type = make_unsigned_type (-size);
2808 /*if (size == -1) TREE_SET_CODE (type, BOOLEAN_TYPE);*/
2809 }
2810 else
a1c65f9f 2811 { /* "__java_float" or ""__java_double". */
eff71ab0
PB
2812 type = make_node (REAL_TYPE);
2813 TYPE_PRECISION (type) = - size;
2814 layout_type (type);
2815 }
0e5921e8 2816 record_builtin_type (RID_MAX, name, type);
eff71ab0 2817 decl = TYPE_NAME (type);
e229f2cd
PB
2818
2819 /* Suppress generate debug symbol entries for these types,
2820 since for normal C++ they are just clutter.
a1c65f9f 2821 However, push_lang_context undoes this if extern "Java" is seen. */
eff71ab0 2822 DECL_IGNORED_P (decl) = 1;
e229f2cd 2823
eff71ab0
PB
2824 TYPE_FOR_JAVA (type) = 1;
2825 return type;
2826}
2827
a1c65f9f 2828/* Push a type into the namespace so that the back-ends ignore it. */
036407f7
ML
2829
2830static void
11f6b451 2831record_unknown_type (tree type, const char* name)
036407f7
ML
2832{
2833 tree decl = pushdecl (build_decl (TYPE_DECL, get_identifier (name), type));
2834 /* Make sure the "unknown type" typedecl gets ignored for debug info. */
2835 DECL_IGNORED_P (decl) = 1;
2836 TYPE_DECL_SUPPRESS_DEBUG (decl) = 1;
2837 TYPE_SIZE (type) = TYPE_SIZE (void_type_node);
2838 TYPE_ALIGN (type) = 1;
11cf4d18 2839 TYPE_USER_ALIGN (type) = 0;
036407f7 2840 TYPE_MODE (type) = TYPE_MODE (void_type_node);
68642fb6 2841}
036407f7 2842
d43829f9
MM
2843/* An string for which we should create an IDENTIFIER_NODE at
2844 startup. */
2845
2846typedef struct predefined_identifier
2847{
2848 /* The name of the identifier. */
8b60264b 2849 const char *const name;
d43829f9 2850 /* The place where the IDENTIFIER_NODE should be stored. */
8b60264b 2851 tree *const node;
838dfd8a 2852 /* Nonzero if this is the name of a constructor or destructor. */
8b60264b 2853 const int ctor_or_dtor_p;
d43829f9
MM
2854} predefined_identifier;
2855
2856/* Create all the predefined identifiers. */
2857
2858static void
11f6b451 2859initialize_predefined_identifiers (void)
d43829f9 2860{
8b60264b 2861 const predefined_identifier *pid;
d43829f9
MM
2862
2863 /* A table of identifiers to create at startup. */
8b60264b 2864 static const predefined_identifier predefined_identifiers[] = {
298d6f60
MM
2865 { "C++", &lang_name_cplusplus, 0 },
2866 { "C", &lang_name_c, 0 },
2867 { "Java", &lang_name_java, 0 },
2868 { CTOR_NAME, &ctor_identifier, 1 },
2869 { "__base_ctor", &base_ctor_identifier, 1 },
2870 { "__comp_ctor", &complete_ctor_identifier, 1 },
2871 { DTOR_NAME, &dtor_identifier, 1 },
2872 { "__comp_dtor", &complete_dtor_identifier, 1 },
2873 { "__base_dtor", &base_dtor_identifier, 1 },
2874 { "__deleting_dtor", &deleting_dtor_identifier, 1 },
298d6f60 2875 { IN_CHARGE_NAME, &in_charge_identifier, 0 },
298d6f60
MM
2876 { "nelts", &nelts_identifier, 0 },
2877 { THIS_NAME, &this_identifier, 0 },
c4372ef4 2878 { VTABLE_DELTA_NAME, &delta_identifier, 0 },
298d6f60 2879 { VTABLE_PFN_NAME, &pfn_identifier, 0 },
298d6f60 2880 { "_vptr", &vptr_identifier, 0 },
3ec6bad3 2881 { "__vtt_parm", &vtt_parm_identifier, 0 },
82a2669e 2882 { "::", &global_scope_name, 0 },
1f6e1acc 2883 { "std", &std_identifier, 0 },
298d6f60 2884 { NULL, NULL, 0 }
d43829f9
MM
2885 };
2886
2887 for (pid = predefined_identifiers; pid->name; ++pid)
298d6f60
MM
2888 {
2889 *pid->node = get_identifier (pid->name);
2890 if (pid->ctor_or_dtor_p)
2891 IDENTIFIER_CTOR_OR_DTOR_P (*pid->node) = 1;
2892 }
d43829f9
MM
2893}
2894
8d08fdba
MS
2895/* Create the predefined scalar types of C,
2896 and some nodes representing standard constants (0, 1, (void *)0).
2897 Initialize the global binding level.
2898 Make definitions for built-in primitive functions. */
2899
2900void
11f6b451 2901cxx_init_decl_processing (void)
8d08fdba 2902{
10841285
MM
2903 tree void_ftype;
2904 tree void_ftype_ptr;
8d08fdba 2905
d43829f9
MM
2906 /* Create all the identifiers we need. */
2907 initialize_predefined_identifiers ();
8d08fdba 2908
8f17b5c5 2909 /* Fill in back-end hooks. */
d363e7bf 2910 lang_missing_noreturn_ok_p = &cp_missing_noreturn_ok_p;
99dccabc 2911
9cd64686
MM
2912 /* Create the global variables. */
2913 push_to_top_level ();
8012c983 2914
82a2669e 2915 current_function_decl = NULL_TREE;
1ec57cf0 2916 current_binding_level = NULL;
a1c65f9f 2917 /* Enter the global namespace. */
30394414 2918 my_friendly_assert (global_namespace == NULL_TREE, 375);
82a2669e
GDR
2919 global_namespace = build_lang_decl (NAMESPACE_DECL, global_scope_name,
2920 void_type_node);
ac20c67a 2921 begin_scope (sk_namespace, global_namespace);
82a2669e 2922
30394414
JM
2923 current_lang_name = NULL_TREE;
2924
bccd95ae 2925 /* Adjust various flags based on command-line settings. */
4a386498 2926 if (!flag_permissive)
2642b9bf 2927 flag_pedantic_errors = 1;
bccd95ae 2928 if (!flag_no_inline)
b850de4f
MM
2929 {
2930 flag_inline_trees = 1;
2931 flag_no_inline = 1;
2932 }
acc72c37
MM
2933 if (flag_inline_functions)
2934 {
2935 flag_inline_trees = 2;
2936 flag_inline_functions = 0;
2937 }
830fcda8 2938
f963b5d9
RS
2939 /* Force minimum function alignment if using the least significant
2940 bit of function pointers to store the virtual bit. */
2941 if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_pfn
2942 && force_align_functions_log < 1)
2943 force_align_functions_log = 1;
2944
8d08fdba
MS
2945 /* Initially, C. */
2946 current_lang_name = lang_name_c;
2947
81b3411c
BS
2948 build_common_tree_nodes (flag_signed_char);
2949
8d08fdba
MS
2950 error_mark_list = build_tree_list (error_mark_node, error_mark_node);
2951 TREE_TYPE (error_mark_list) = error_mark_node;
2952
6bcedb4e 2953 /* Create the `std' namespace. */
1dbb6023
NS
2954 push_namespace (std_identifier);
2955 std_node = current_namespace;
2956 pop_namespace ();
5362b086 2957
eaa7c03f 2958 c_common_nodes_and_builtins ();
37c46b43 2959
4d8a1dd6
MM
2960 java_byte_type_node = record_builtin_java_type ("__java_byte", 8);
2961 java_short_type_node = record_builtin_java_type ("__java_short", 16);
2962 java_int_type_node = record_builtin_java_type ("__java_int", 32);
2963 java_long_type_node = record_builtin_java_type ("__java_long", 64);
2964 java_float_type_node = record_builtin_java_type ("__java_float", -32);
2965 java_double_type_node = record_builtin_java_type ("__java_double", -64);
2966 java_char_type_node = record_builtin_java_type ("__java_char", -16);
2967 java_boolean_type_node = record_builtin_java_type ("__java_boolean", -1);
eff71ab0 2968
8d08fdba
MS
2969 integer_two_node = build_int_2 (2, 0);
2970 TREE_TYPE (integer_two_node) = integer_type_node;
2971 integer_three_node = build_int_2 (3, 0);
2972 TREE_TYPE (integer_three_node) = integer_type_node;
8d08fdba 2973
255512c1 2974 record_builtin_type (RID_BOOL, "bool", boolean_type_node);
de7df9eb
JM
2975 truthvalue_type_node = boolean_type_node;
2976 truthvalue_false_node = boolean_false_node;
2977 truthvalue_true_node = boolean_true_node;
2986ae00 2978
4cc1d462 2979 empty_except_spec = build_tree_list (NULL_TREE, NULL_TREE);
5fd8e536
JM
2980
2981#if 0
3e411c3f 2982 record_builtin_type (RID_MAX, NULL, string_type_node);
5fd8e536
JM
2983#endif
2984
1f84ec23
MM
2985 delta_type_node = ptrdiff_type_node;
2986 vtable_index_type = ptrdiff_type_node;
c7e266a6 2987
3ec6bad3 2988 vtt_parm_type = build_pointer_type (const_ptr_type_node);
10841285
MM
2989 void_ftype = build_function_type (void_type_node, void_list_node);
2990 void_ftype_ptr = build_function_type (void_type_node,
2991 tree_cons (NULL_TREE,
2992 ptr_type_node,
2993 void_list_node));
824b9a4c 2994 void_ftype_ptr
4cc1d462 2995 = build_exception_variant (void_ftype_ptr, empty_except_spec);
8d08fdba 2996
8d08fdba
MS
2997 /* C++ extensions */
2998
2999 unknown_type_node = make_node (UNKNOWN_TYPE);
036407f7
ML
3000 record_unknown_type (unknown_type_node, "unknown type");
3001
8d08fdba
MS
3002 /* Indirecting an UNKNOWN_TYPE node yields an UNKNOWN_TYPE node. */
3003 TREE_TYPE (unknown_type_node) = unknown_type_node;
a6967cc0 3004
a6967cc0
JM
3005 /* Looking up TYPE_POINTER_TO and TYPE_REFERENCE_TO yield the same
3006 result. */
8d08fdba
MS
3007 TYPE_POINTER_TO (unknown_type_node) = unknown_type_node;
3008 TYPE_REFERENCE_TO (unknown_type_node) = unknown_type_node;
3009
c4372ef4
NS
3010 {
3011 /* Make sure we get a unique function type, so we can give
3012 its pointer type a name. (This wins for gdb.) */
3013 tree vfunc_type = make_node (FUNCTION_TYPE);
3014 TREE_TYPE (vfunc_type) = integer_type_node;
3015 TYPE_ARG_TYPES (vfunc_type) = NULL_TREE;
3016 layout_type (vfunc_type);
3017
3018 vtable_entry_type = build_pointer_type (vfunc_type);
3019 }
0e5921e8 3020 record_builtin_type (RID_MAX, VTBL_PTR_TYPE, vtable_entry_type);
8d08fdba 3021
8d08fdba 3022 vtbl_type_node
52bf7d5d 3023 = build_cplus_array_type (vtable_entry_type, NULL_TREE);
8d08fdba 3024 layout_type (vtbl_type_node);
91063b51 3025 vtbl_type_node = build_qualified_type (vtbl_type_node, TYPE_QUAL_CONST);
3e411c3f 3026 record_builtin_type (RID_MAX, NULL, vtbl_type_node);
849da744
MM
3027 vtbl_ptr_type_node = build_pointer_type (vtable_entry_type);
3028 layout_type (vtbl_ptr_type_node);
3e411c3f 3029 record_builtin_type (RID_MAX, NULL, vtbl_ptr_type_node);
8d08fdba 3030
1f84ec23
MM
3031 push_namespace (get_identifier ("__cxxabiv1"));
3032 abi_node = current_namespace;
3033 pop_namespace ();
6633d636 3034
2c73f9f5 3035 global_type_node = make_node (LANG_TYPE);
036407f7 3036 record_unknown_type (global_type_node, "global type");
2c73f9f5 3037
db5ae43f
MS
3038 /* Now, C++. */
3039 current_lang_name = lang_name_cplusplus;
8d08fdba 3040
ced78d8b 3041 {
4b0d3cbe
MM
3042 tree bad_alloc_id;
3043 tree bad_alloc_type_node;
3044 tree bad_alloc_decl;
3045 tree newtype, deltype;
10841285
MM
3046 tree ptr_ftype_sizetype;
3047
1dbb6023 3048 push_namespace (std_identifier);
4b0d3cbe
MM
3049 bad_alloc_id = get_identifier ("bad_alloc");
3050 bad_alloc_type_node = make_aggr_type (RECORD_TYPE);
3051 TYPE_CONTEXT (bad_alloc_type_node) = current_namespace;
3052 bad_alloc_decl
3053 = create_implicit_typedef (bad_alloc_id, bad_alloc_type_node);
3054 DECL_CONTEXT (bad_alloc_decl) = current_namespace;
3055 TYPE_STUB_DECL (bad_alloc_type_node) = bad_alloc_decl;
1dbb6023 3056 pop_namespace ();
4b0d3cbe 3057
10841285
MM
3058 ptr_ftype_sizetype
3059 = build_function_type (ptr_type_node,
3060 tree_cons (NULL_TREE,
c9f8536c 3061 size_type_node,
10841285 3062 void_list_node));
2c73f9f5 3063 newtype = build_exception_variant
0ba8a114
NS
3064 (ptr_ftype_sizetype, add_exception_specifier
3065 (NULL_TREE, bad_alloc_type_node, -1));
4cc1d462 3066 deltype = build_exception_variant (void_ftype_ptr, empty_except_spec);
596ea4e5
AS
3067 push_cp_library_fn (NEW_EXPR, newtype);
3068 push_cp_library_fn (VEC_NEW_EXPR, newtype);
3069 global_delete_fndecl = push_cp_library_fn (DELETE_EXPR, deltype);
3070 push_cp_library_fn (VEC_DELETE_EXPR, deltype);
ced78d8b 3071 }
8d08fdba
MS
3072
3073 abort_fndecl
1f84ec23 3074 = build_library_fn_ptr ("__cxa_pure_virtual", void_ftype);
8d08fdba 3075
8d08fdba
MS
3076 /* Perform other language dependent initializations. */
3077 init_class_processing ();
8d08fdba 3078 init_search_processing ();
db48b831 3079 init_rtti_processing ();
8d08fdba 3080
6467930b 3081 if (flag_exceptions)
8d2733ca 3082 init_exception_processing ();
9e9ff709 3083
7fcdf4c2 3084 if (! supports_one_only ())
72b7eeff 3085 flag_weak = 0;
8d08fdba 3086
2ce07e2d 3087 make_fname_decl = cp_make_fname_decl;
0ba8a114 3088 start_fname_decls ();
8d08fdba 3089
e9a25f70
JL
3090 /* Show we use EH for cleanups. */
3091 using_eh_for_cleanups ();
62c154ed 3092
d9cf7c82
JM
3093 /* Maintain consistency. Perhaps we should just complain if they
3094 say -fwritable-strings? */
3095 if (flag_writable_strings)
3096 flag_const_strings = 0;
62c154ed
JM
3097}
3098
0ba8a114
NS
3099/* Generate an initializer for a function naming variable from
3100 NAME. NAME may be NULL, in which case we generate a special
a1c65f9f 3101 ERROR_MARK node which should be replaced later. */
0ba8a114
NS
3102
3103tree
11f6b451 3104cp_fname_init (const char* name)
0ba8a114
NS
3105{
3106 tree domain = NULL_TREE;
3107 tree type;
3108 tree init = NULL_TREE;
3109 size_t length = 0;
3110
3111 if (name)
3112 {
3113 length = strlen (name);
3114 domain = build_index_type (size_int (length));
3115 init = build_string (length + 1, name);
3116 }
3117
3118 type = build_qualified_type (char_type_node, TYPE_QUAL_CONST);
3119 type = build_cplus_array_type (type, domain);
3120
3121 if (init)
3122 TREE_TYPE (init) = type;
3123 else
3124 /* We don't know the value until instantiation time. Make
a1c65f9f 3125 something which will be digested now, but replaced later. */
0ba8a114
NS
3126 init = build (ERROR_MARK, type);
3127
3128 return init;
3129}
3130
2ce07e2d
NS
3131/* Create the VAR_DECL for __FUNCTION__ etc. ID is the name to give the
3132 decl, NAME is the initialization string and TYPE_DEP indicates whether
3133 NAME depended on the type of the function. We make use of that to detect
0ba8a114
NS
3134 __PRETTY_FUNCTION__ inside a template fn. This is being done
3135 lazily at the point of first use, so we musn't push the decl now. */
2ce07e2d
NS
3136
3137static tree
11f6b451 3138cp_make_fname_decl (tree id, int type_dep)
2ce07e2d 3139{
83182544 3140 const char *const name = (type_dep && processing_template_decl
e913996d 3141 ? NULL : fname_as_string (type_dep));
0ba8a114
NS
3142 tree init = cp_fname_init (name);
3143 tree decl = build_decl (VAR_DECL, id, TREE_TYPE (init));
2ce07e2d 3144
6cce57b0 3145 /* As we're using pushdecl_with_scope, we must set the context. */
0ba8a114
NS
3146 DECL_CONTEXT (decl) = current_function_decl;
3147 DECL_PRETTY_FUNCTION_P (decl) = type_dep;
3148
2ce07e2d
NS
3149 TREE_STATIC (decl) = 1;
3150 TREE_READONLY (decl) = 1;
2ce07e2d 3151 DECL_ARTIFICIAL (decl) = 1;
2ce07e2d 3152 DECL_INITIAL (decl) = init;
0ba8a114
NS
3153
3154 TREE_USED (decl) = 1;
5362b086 3155
6cce57b0
JM
3156 if (current_function_decl)
3157 {
3158 struct cp_binding_level *b = current_binding_level;
a7e8c268 3159 while (b->level_chain->kind != sk_function_parms)
6cce57b0
JM
3160 b = b->level_chain;
3161 pushdecl_with_scope (decl, b);
e913996d 3162 }
6cce57b0 3163
0ba8a114
NS
3164 cp_finish_decl (decl, init, NULL_TREE, LOOKUP_ONLYCONVERTING);
3165
2ce07e2d
NS
3166 return decl;
3167}
3168
d52e4867
RS
3169/* Make a definition for a builtin function named NAME in the current
3170 namespace, whose data type is TYPE and whose context is CONTEXT.
3171 TYPE should be a function type with argument types.
8d08fdba 3172
0c11ada6
JM
3173 CLASS and CODE tell later passes how to compile calls to this function.
3174 See tree.h for possible values.
3175
3176 If LIBNAME is nonzero, use that for DECL_ASSEMBLER_NAME,
6a2dd09a
RS
3177 the name to be called if we can't opencode the function.
3178 If ATTRS is nonzero, use that for the function's attribute
3179 list. */
8d08fdba 3180
d52e4867 3181static tree
11f6b451
NN
3182builtin_function_1 (const char* name,
3183 tree type,
3184 tree context,
3185 int code,
3186 enum built_in_class class,
3187 const char* libname,
3188 tree attrs)
8d08fdba 3189{
596ea4e5 3190 tree decl = build_library_fn_1 (get_identifier (name), ERROR_MARK, type);
0c11ada6
JM
3191 DECL_BUILT_IN_CLASS (decl) = class;
3192 DECL_FUNCTION_CODE (decl) = code;
d52e4867 3193 DECL_CONTEXT (decl) = context;
12a22e76 3194
6bcedb4e 3195 pushdecl (decl);
6bcedb4e 3196
8d08fdba
MS
3197 /* Since `pushdecl' relies on DECL_ASSEMBLER_NAME instead of DECL_NAME,
3198 we cannot change DECL_ASSEMBLER_NAME until we have installed this
3199 function in the namespace. */
0c11ada6 3200 if (libname)
92643fea
MM
3201 SET_DECL_ASSEMBLER_NAME (decl, get_identifier (libname));
3202 make_decl_rtl (decl, NULL);
935d1834
ZW
3203
3204 /* Warn if a function in the namespace for users
3205 is used without an occasion to consider it declared. */
3206 if (name[0] != '_' || name[1] != '_')
3207 DECL_ANTICIPATED (decl) = 1;
3208
6431177a 3209 /* Possibly apply some default attributes to this built-in function. */
6a2dd09a
RS
3210 if (attrs)
3211 decl_attributes (&decl, attrs, ATTR_FLAG_BUILT_IN);
3212 else
3213 decl_attributes (&decl, NULL_TREE, 0);
6431177a 3214
8d08fdba
MS
3215 return decl;
3216}
7f4edbcb 3217
d52e4867
RS
3218/* Entry point for the benefit of c_common_nodes_and_builtins.
3219
34cd5ae7 3220 Make a definition for a builtin function named NAME and whose data type
d52e4867
RS
3221 is TYPE. TYPE should be a function type with argument types. This
3222 function places the anticipated declaration in the global namespace
3223 and additionally in the std namespace if appropriate.
3224
3225 CLASS and CODE tell later passes how to compile calls to this function.
3226 See tree.h for possible values.
3227
3228 If LIBNAME is nonzero, use that for DECL_ASSEMBLER_NAME,
6a2dd09a
RS
3229 the name to be called if we can't opencode the function.
3230
3231 If ATTRS is nonzero, use that for the function's attribute
3232 list. */
d52e4867
RS
3233
3234tree
11f6b451
NN
3235builtin_function (const char* name,
3236 tree type,
3237 int code,
3238 enum built_in_class class,
3239 const char* libname,
3240 tree attrs)
d52e4867
RS
3241{
3242 /* All builtins that don't begin with an '_' should additionally
3243 go in the 'std' namespace. */
3244 if (name[0] != '_')
3245 {
3246 push_namespace (std_identifier);
6a2dd09a 3247 builtin_function_1 (name, type, std_node, code, class, libname, attrs);
d52e4867
RS
3248 pop_namespace ();
3249 }
3250
6a2dd09a
RS
3251 return builtin_function_1 (name, type, NULL_TREE, code,
3252 class, libname, attrs);
d52e4867
RS
3253}
3254
0c11ada6
JM
3255/* Generate a FUNCTION_DECL with the typical flags for a runtime library
3256 function. Not called directly. */
3257
3258static tree
11f6b451 3259build_library_fn_1 (tree name, enum tree_code operator_code, tree type)
0c11ada6
JM
3260{
3261 tree fn = build_lang_decl (FUNCTION_DECL, name, type);
3262 DECL_EXTERNAL (fn) = 1;
3263 TREE_PUBLIC (fn) = 1;
3264 DECL_ARTIFICIAL (fn) = 1;
3265 TREE_NOTHROW (fn) = 1;
596ea4e5 3266 SET_OVERLOADED_OPERATOR_CODE (fn, operator_code);
5d2ed28c 3267 SET_DECL_LANGUAGE (fn, lang_c);
0c11ada6
JM
3268 return fn;
3269}
c2a37c55 3270
0c11ada6
JM
3271/* Returns the _DECL for a library function with C linkage.
3272 We assume that such functions never throw; if this is incorrect,
3273 callers should unset TREE_NOTHROW. */
c2a37c55 3274
7f4edbcb 3275tree
11f6b451 3276build_library_fn (tree name, tree type)
0c11ada6 3277{
19e7881c 3278 return build_library_fn_1 (name, ERROR_MARK, type);
0c11ada6
JM
3279}
3280
3281/* Returns the _DECL for a library function with C++ linkage. */
3282
596ea4e5 3283static tree
11f6b451 3284build_cp_library_fn (tree name, enum tree_code operator_code, tree type)
0c11ada6 3285{
596ea4e5 3286 tree fn = build_library_fn_1 (name, operator_code, type);
0c11ada6 3287 TREE_NOTHROW (fn) = TYPE_NOTHROW_P (type);
6bbf1598 3288 DECL_CONTEXT (fn) = FROB_CONTEXT (current_namespace);
5d2ed28c 3289 SET_DECL_LANGUAGE (fn, lang_cplusplus);
0c11ada6 3290 set_mangled_name_for_decl (fn);
0c11ada6
JM
3291 return fn;
3292}
3293
3294/* Like build_library_fn, but takes a C string instead of an
3295 IDENTIFIER_NODE. */
3296
3297tree
11f6b451 3298build_library_fn_ptr (const char* name, tree type)
7f4edbcb 3299{
0c11ada6
JM
3300 return build_library_fn (get_identifier (name), type);
3301}
3302
3303/* Like build_cp_library_fn, but takes a C string instead of an
3304 IDENTIFIER_NODE. */
3305
3306tree
11f6b451 3307build_cp_library_fn_ptr (const char* name, tree type)
0c11ada6 3308{
596ea4e5 3309 return build_cp_library_fn (get_identifier (name), ERROR_MARK, type);
0c11ada6
JM
3310}
3311
3312/* Like build_library_fn, but also pushes the function so that we will
3313 be able to find it via IDENTIFIER_GLOBAL_VALUE. */
3314
3315tree
11f6b451 3316push_library_fn (tree name, tree type)
0c11ada6
JM
3317{
3318 tree fn = build_library_fn (name, type);
3319 pushdecl_top_level (fn);
3320 return fn;
3321}
3322
3323/* Like build_cp_library_fn, but also pushes the function so that it
3324 will be found by normal lookup. */
3325
596ea4e5 3326static tree
11f6b451 3327push_cp_library_fn (enum tree_code operator_code, tree type)
0c11ada6 3328{
5362b086 3329 tree fn = build_cp_library_fn (ansi_opname (operator_code),
596ea4e5
AS
3330 operator_code,
3331 type);
0c11ada6
JM
3332 pushdecl (fn);
3333 return fn;
3334}
3335
3336/* Like push_library_fn, but takes a TREE_LIST of parm types rather than
3337 a FUNCTION_TYPE. */
3338
3339tree
11f6b451 3340push_void_library_fn (tree name, tree parmtypes)
0c11ada6
JM
3341{
3342 tree type = build_function_type (void_type_node, parmtypes);
3343 return push_library_fn (name, type);
3344}
3345
cf74fb86 3346/* Like push_library_fn, but also note that this function throws
0c11ada6
JM
3347 and does not return. Used for __throw_foo and the like. */
3348
3349tree
11f6b451 3350push_throw_library_fn (tree name, tree type)
0c11ada6 3351{
cf74fb86 3352 tree fn = push_library_fn (name, type);
0c11ada6
JM
3353 TREE_THIS_VOLATILE (fn) = 1;
3354 TREE_NOTHROW (fn) = 0;
3355 return fn;
7f4edbcb 3356}
8d08fdba 3357\f
61a127b3
MM
3358/* When we call finish_struct for an anonymous union, we create
3359 default copy constructors and such. But, an anonymous union
3360 shouldn't have such things; this function undoes the damage to the
3361 anonymous union type T.
3362
3363 (The reason that we create the synthesized methods is that we don't
3364 distinguish `union { int i; }' from `typedef union { int i; } U'.
3365 The first is an anonymous union; the second is just an ordinary
3366 union type.) */
3367
3368void
11f6b451 3369fixup_anonymous_aggr (tree t)
61a127b3
MM
3370{
3371 tree *q;
3372
3373 /* Wipe out memory of synthesized methods */
3374 TYPE_HAS_CONSTRUCTOR (t) = 0;
3375 TYPE_HAS_DEFAULT_CONSTRUCTOR (t) = 0;
3376 TYPE_HAS_INIT_REF (t) = 0;
3377 TYPE_HAS_CONST_INIT_REF (t) = 0;
3378 TYPE_HAS_ASSIGN_REF (t) = 0;
61a127b3
MM
3379 TYPE_HAS_CONST_ASSIGN_REF (t) = 0;
3380
3381 /* Splice the implicitly generated functions out of the TYPE_METHODS
3382 list. */
3383 q = &TYPE_METHODS (t);
3384 while (*q)
3385 {
3386 if (DECL_ARTIFICIAL (*q))
3387 *q = TREE_CHAIN (*q);
3388 else
3389 q = &TREE_CHAIN (*q);
3390 }
3391
cab1f180 3392 /* ISO C++ 9.5.3. Anonymous unions may not have function members. */
61a127b3 3393 if (TYPE_METHODS (t))
ddd2d57e
RH
3394 error ("%Jan anonymous union cannot have function members",
3395 TYPE_MAIN_DECL (t));
a1c2b86d
JJ
3396
3397 /* Anonymous aggregates cannot have fields with ctors, dtors or complex
3398 assignment operators (because they cannot have these methods themselves).
3399 For anonymous unions this is already checked because they are not allowed
3400 in any union, otherwise we have to check it. */
3401 if (TREE_CODE (t) != UNION_TYPE)
3402 {
3403 tree field, type;
3404
3405 for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
3406 if (TREE_CODE (field) == FIELD_DECL)
3407 {
3408 type = TREE_TYPE (field);
3409 if (CLASS_TYPE_P (type))
3410 {
3411 if (TYPE_NEEDS_CONSTRUCTING (type))
3412 cp_error_at ("member %#D' with constructor not allowed in anonymous aggregate",
3413 field);
3414 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
3415 cp_error_at ("member %#D' with destructor not allowed in anonymous aggregate",
3416 field);
3417 if (TYPE_HAS_COMPLEX_ASSIGN_REF (type))
3418 cp_error_at ("member %#D' with copy assignment operator not allowed in anonymous aggregate",
3419 field);
3420 }
3421 }
3422 }
61a127b3
MM
3423}
3424
72a93143 3425/* Make sure that a declaration with no declarator is well-formed, i.e.
a723baf1 3426 just declares a tagged type or anonymous union.
8d08fdba 3427
a723baf1 3428 Returns the type declared; or NULL_TREE if none. */
8d08fdba 3429
72a93143 3430tree
11f6b451 3431check_tag_decl (tree declspecs)
8d08fdba 3432{
72a93143 3433 int found_type = 0;
2bdb0643 3434 int saw_friend = 0;
e8186ecf 3435 int saw_typedef = 0;
2986ae00 3436 tree ob_modifier = NULL_TREE;
8d08fdba 3437 register tree link;
a723baf1
MM
3438 /* If a class, struct, or enum type is declared by the DECLSPECS
3439 (i.e, if a class-specifier, enum-specifier, or non-typename
3440 elaborated-type-specifier appears in the DECLSPECS),
3441 DECLARED_TYPE is set to the corresponding type. */
3442 tree declared_type = NULL_TREE;
3443 bool error_p = false;
8d08fdba
MS
3444
3445 for (link = declspecs; link; link = TREE_CHAIN (link))
3446 {
a723baf1 3447 tree value = TREE_VALUE (link);
8d08fdba 3448
c003e212 3449 if (TYPE_P (value) || TREE_CODE (value) == TYPE_DECL
2bdb0643 3450 || (TREE_CODE (value) == IDENTIFIER_NODE
c003e212 3451 && is_typename_at_global_scope (value)))
8d08fdba 3452 {
72a93143 3453 ++found_type;
5566b478 3454
bd0d5d4a
JM
3455 if (found_type == 2 && TREE_CODE (value) == IDENTIFIER_NODE)
3456 {
3457 if (! in_system_header)
33bd39a2 3458 pedwarn ("redeclaration of C++ built-in type `%T'", value);
bd0d5d4a
JM
3459 return NULL_TREE;
3460 }
3461
3462 if (TYPE_P (value)
3463 && ((TREE_CODE (value) != TYPENAME_TYPE && IS_AGGR_TYPE (value))
3464 || TREE_CODE (value) == ENUMERAL_TYPE))
72a93143
JM
3465 {
3466 my_friendly_assert (TYPE_MAIN_DECL (value) != NULL_TREE, 261);
a723baf1 3467 declared_type = value;
72a93143 3468 }
8d08fdba 3469 }
e8186ecf
GDR
3470 else if (value == ridpointers[(int) RID_TYPEDEF])
3471 saw_typedef = 1;
83f660b7
JM
3472 else if (value == ridpointers[(int) RID_FRIEND])
3473 {
83f660b7
JM
3474 if (current_class_type == NULL_TREE
3475 || current_scope () != current_class_type)
3476 ob_modifier = value;
2bdb0643
JM
3477 else
3478 saw_friend = 1;
83f660b7 3479 }
8d08fdba 3480 else if (value == ridpointers[(int) RID_STATIC]
2986ae00
MS
3481 || value == ridpointers[(int) RID_EXTERN]
3482 || value == ridpointers[(int) RID_AUTO]
28cbf42c
MS
3483 || value == ridpointers[(int) RID_REGISTER]
3484 || value == ridpointers[(int) RID_INLINE]
3485 || value == ridpointers[(int) RID_VIRTUAL]
72a93143
JM
3486 || value == ridpointers[(int) RID_CONST]
3487 || value == ridpointers[(int) RID_VOLATILE]
7a1f3f5f
RH
3488 || value == ridpointers[(int) RID_EXPLICIT]
3489 || value == ridpointers[(int) RID_THREAD])
2986ae00 3490 ob_modifier = value;
a723baf1
MM
3491 else if (value == error_mark_node)
3492 error_p = true;
8d08fdba
MS
3493 }
3494
72a93143
JM
3495 if (found_type > 1)
3496 error ("multiple types in one declaration");
7e2067ca 3497
a723baf1 3498 if (declared_type == NULL_TREE && ! saw_friend && !error_p)
7e2067ca 3499 pedwarn ("declaration does not declare anything");
1951a1b6 3500 /* Check for an anonymous union. */
a723baf1
MM
3501 else if (declared_type && IS_AGGR_TYPE_CODE (TREE_CODE (declared_type))
3502 && TYPE_ANONYMOUS_P (declared_type))
0dd3962d 3503 {
e8186ecf
GDR
3504 /* 7/3 In a simple-declaration, the optional init-declarator-list
3505 can be omitted only when declaring a class (clause 9) or
3506 enumeration (7.2), that is, when the decl-specifier-seq contains
3507 either a class-specifier, an elaborated-type-specifier with
3508 a class-key (9.1), or an enum-specifier. In these cases and
3509 whenever a class-specifier or enum-specifier is present in the
3510 decl-specifier-seq, the identifiers in these specifiers are among
3511 the names being declared by the declaration (as class-name,
3512 enum-names, or enumerators, depending on the syntax). In such
3513 cases, and except for the declaration of an unnamed bit-field (9.6),
3514 the decl-specifier-seq shall introduce one or more names into the
3515 program, or shall redeclare a name introduced by a previous
3516 declaration. [Example:
3517 enum { }; // ill-formed
3518 typedef class { }; // ill-formed
3519 --end example] */
3520 if (saw_typedef)
3521 {
357351e5 3522 error ("missing type-name in typedef-declaration");
e8186ecf
GDR
3523 return NULL_TREE;
3524 }
0dd3962d 3525 /* Anonymous unions are objects, so they can have specifiers. */;
a723baf1 3526 SET_ANON_AGGR_TYPE_P (declared_type);
6bdb8141 3527
a723baf1
MM
3528 if (TREE_CODE (declared_type) != UNION_TYPE && pedantic
3529 && !in_system_header)
6bdb8141 3530 pedwarn ("ISO C++ prohibits anonymous structs");
0dd3962d
JM
3531 }
3532
83f660b7 3533 else if (ob_modifier)
8d08fdba 3534 {
83f660b7
JM
3535 if (ob_modifier == ridpointers[(int) RID_INLINE]
3536 || ob_modifier == ridpointers[(int) RID_VIRTUAL])
33bd39a2 3537 error ("`%D' can only be specified for functions", ob_modifier);
83f660b7 3538 else if (ob_modifier == ridpointers[(int) RID_FRIEND])
33bd39a2 3539 error ("`%D' can only be specified inside a class", ob_modifier);
83f660b7 3540 else if (ob_modifier == ridpointers[(int) RID_EXPLICIT])
33bd39a2 3541 error ("`%D' can only be specified for constructors",
83f660b7
JM
3542 ob_modifier);
3543 else
33bd39a2 3544 error ("`%D' can only be specified for objects and functions",
83f660b7 3545 ob_modifier);
72a93143 3546 }
8d08fdba 3547
a723baf1 3548 return declared_type;
72a93143
JM
3549}
3550
3551/* Called when a declaration is seen that contains no names to declare.
3552 If its type is a reference to a structure, union or enum inherited
3553 from a containing scope, shadow that tag name for the current scope
3554 with a forward reference.
3555 If its type defines a new named structure or union
3556 or defines an enum, it is valid but we need not do anything here.
3557 Otherwise, it is an error.
3558
3559 C++: may have to grok the declspecs to learn about static,
a723baf1 3560 complain for anonymous unions.
72a93143 3561
a723baf1
MM
3562 Returns the TYPE declared -- or NULL_TREE if none. */
3563
3564tree
11f6b451 3565shadow_tag (tree declspecs)
72a93143
JM
3566{
3567 tree t = check_tag_decl (declspecs);
3568
a723baf1
MM
3569 if (!t)
3570 return NULL_TREE;
3571
3572 maybe_process_partial_specialization (t);
72a93143
JM
3573
3574 /* This is where the variables in an anonymous union are
3575 declared. An anonymous union declaration looks like:
3576 union { ... } ;
3577 because there is no declarator after the union, the parser
3578 sends that declaration here. */
a723baf1 3579 if (ANON_AGGR_TYPE_P (t))
72a93143 3580 {
6bdb8141 3581 fixup_anonymous_aggr (t);
72a93143
JM
3582
3583 if (TYPE_FIELDS (t))
3584 {
3585 tree decl = grokdeclarator (NULL_TREE, declspecs, NORMAL, 0,
91d231cb 3586 NULL);
72a93143
JM
3587 finish_anon_union (decl);
3588 }
8d08fdba 3589 }
a723baf1
MM
3590
3591 return t;
8d08fdba
MS
3592}
3593\f
3594/* Decode a "typename", such as "int **", returning a ..._TYPE node. */
3595
3596tree
11f6b451 3597groktypename (tree typename)
8d08fdba 3598{
98884b26
JM
3599 tree specs, attrs;
3600 tree type;
8d08fdba
MS
3601 if (TREE_CODE (typename) != TREE_LIST)
3602 return typename;
98884b26
JM
3603 split_specs_attrs (TREE_PURPOSE (typename), &specs, &attrs);
3604 type = grokdeclarator (TREE_VALUE (typename), specs,
3605 TYPENAME, 0, &attrs);
3606 if (attrs)
3607 cplus_decl_attributes (&type, attrs, 0);
3608 return type;
8d08fdba
MS
3609}
3610
3611/* Decode a declarator in an ordinary declaration or data definition.
3612 This is called as soon as the type information and variable name
3613 have been parsed, before parsing the initializer if any.
3614 Here we create the ..._DECL node, fill in its type,
3615 and put it on the list of decls for the current context.
3616 The ..._DECL node is returned as the value.
3617
3618 Exception: for arrays where the length is not specified,
82580166 3619 the type is left null, to be filled in by `cp_finish_decl'.
8d08fdba
MS
3620
3621 Function definitions do not come here; they go to start_function
3622 instead. However, external and forward declarations of functions
3623 do go through here. Structure field declarations are done by
3624 grokfield and not through here. */
3625
8d08fdba 3626tree
11f6b451
NN
3627start_decl (tree declarator,
3628 tree declspecs,
3629 int initialized,
3630 tree attributes,
3631 tree prefix_attributes)
8d08fdba 3632{
59387d2e 3633 tree decl;
8d08fdba
MS
3634 register tree type, tem;
3635 tree context;
8d08fdba 3636
e92cc029 3637 /* This should only be done once on the top most decl. */
594bb0e7 3638 if (have_extern_spec)
8d08fdba 3639 {
1f8f4a0b
MM
3640 declspecs = tree_cons (NULL_TREE, get_identifier ("extern"),
3641 declspecs);
594bb0e7 3642 have_extern_spec = false;
8d08fdba
MS
3643 }
3644
e23bd218
IR
3645 /* An object declared as __attribute__((deprecated)) suppresses
3646 warnings of uses of other deprecated items. */
3647 if (lookup_attribute ("deprecated", attributes))
3648 deprecated_state = DEPRECATED_SUPPRESS;
3649
91d231cb 3650 attributes = chainon (attributes, prefix_attributes);
b17e2870 3651
c11b6f21 3652 decl = grokdeclarator (declarator, declspecs, NORMAL, initialized,
91d231cb 3653 &attributes);
68642fb6 3654
e23bd218
IR
3655 deprecated_state = DEPRECATED_NORMAL;
3656
a1774733 3657 if (decl == NULL_TREE || TREE_CODE (decl) == VOID_TYPE)
8d08fdba
MS
3658 return NULL_TREE;
3659
3660 type = TREE_TYPE (decl);
3661
44689c12
ML
3662 if (type == error_mark_node)
3663 return NULL_TREE;
3664
4f1c5b7d 3665 context = DECL_CONTEXT (decl);
8d08fdba 3666
9a68c51f
JM
3667 if (initialized && context && TREE_CODE (context) == NAMESPACE_DECL
3668 && context != current_namespace && TREE_CODE (decl) == VAR_DECL)
3669 {
3670 /* When parsing the initializer, lookup should use the object's
a1c65f9f 3671 namespace. */
9a68c51f
JM
3672 push_decl_namespace (context);
3673 }
3674
a1c65f9f 3675 /* We are only interested in class contexts, later. */
2c73f9f5
ML
3676 if (context && TREE_CODE (context) == NAMESPACE_DECL)
3677 context = NULL_TREE;
3678
8d08fdba
MS
3679 if (initialized)
3680 /* Is it valid for this decl to have an initializer at all?
3681 If not, set INITIALIZED to zero, which will indirectly
82580166 3682 tell `cp_finish_decl' to ignore the initializer once it is parsed. */
8d08fdba
MS
3683 switch (TREE_CODE (decl))
3684 {
3685 case TYPE_DECL:
4a7510cb 3686 error ("typedef `%D' is initialized (use __typeof__ instead)", decl);
95f79357 3687 initialized = 0;
8d08fdba
MS
3688 break;
3689
3690 case FUNCTION_DECL:
33bd39a2 3691 error ("function `%#D' is initialized like a variable", decl);
8d08fdba
MS
3692 initialized = 0;
3693 break;
3694
3695 default:
3e41d13b 3696 break;
8d08fdba
MS
3697 }
3698
8d08fdba
MS
3699 if (initialized)
3700 {
a9aedbc2 3701 if (! toplevel_bindings_p ()
8d08fdba 3702 && DECL_EXTERNAL (decl))
33bd39a2 3703 warning ("declaration of `%#D' has `extern' and is initialized",
8d08fdba
MS
3704 decl);
3705 DECL_EXTERNAL (decl) = 0;
5566b478 3706 if (toplevel_bindings_p ())
8d08fdba
MS
3707 TREE_STATIC (decl) = 1;
3708
3709 /* Tell `pushdecl' this is an initialized decl
3710 even though we don't yet have the initializer expression.
82580166 3711 Also tell `cp_finish_decl' it may store the real initializer. */
8d08fdba
MS
3712 DECL_INITIAL (decl) = error_mark_node;
3713 }
3714
fa20888b 3715 /* Set attributes here so if duplicate decl, will have proper attributes. */
91d231cb 3716 cplus_decl_attributes (&decl, attributes, 0);
fa20888b 3717
ecb0eece 3718 /* If #pragma weak was used, mark the decl weak now. */
5f52c0e0 3719 if (global_scope_p (current_binding_level))
ecb0eece
RH
3720 maybe_apply_pragma_weak (decl);
3721
97055d5c
AO
3722 if (TREE_CODE (decl) == FUNCTION_DECL
3723 && DECL_DECLARED_INLINE_P (decl)
3724 && DECL_UNINLINABLE (decl)
3725 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
ddd2d57e 3726 warning ("%Jinline function '%D' given attribute noinline", decl, decl);
97055d5c 3727
d0f062fb 3728 if (context && COMPLETE_TYPE_P (complete_type (context)))
5b605f68 3729 {
14d22dd6 3730 push_nested_class (context);
e97e5263 3731
5b605f68
MS
3732 if (TREE_CODE (decl) == VAR_DECL)
3733 {
86ac0575 3734 tree field = lookup_field (context, DECL_NAME (decl), 0, false);
5b605f68 3735 if (field == NULL_TREE || TREE_CODE (field) != VAR_DECL)
33bd39a2 3736 error ("`%#D' is not a static member of `%#T'", decl, context);
e349ee73
MS
3737 else
3738 {
3739 if (DECL_CONTEXT (field) != context)
f2d773a2 3740 {
a723baf1
MM
3741 if (!same_type_p (DECL_CONTEXT (field), context))
3742 pedwarn ("ISO C++ does not permit `%T::%D' to be defined as `%T::%D'",
3743 DECL_CONTEXT (field), DECL_NAME (decl),
3744 context, DECL_NAME (decl));
f2d773a2
JM
3745 DECL_CONTEXT (decl) = DECL_CONTEXT (field);
3746 }
75650646
MM
3747 /* Static data member are tricky; an in-class initialization
3748 still doesn't provide a definition, so the in-class
3749 declaration will have DECL_EXTERNAL set, but will have an
3750 initialization. Thus, duplicate_decls won't warn
3751 about this situation, and so we check here. */
3752 if (DECL_INITIAL (decl) && DECL_INITIAL (field))
33bd39a2 3753 error ("duplicate initialization of %D", decl);
e349ee73
MS
3754 if (duplicate_decls (decl, field))
3755 decl = field;
3756 }
5b605f68 3757 }
f30432d7
MS
3758 else
3759 {
5566b478 3760 tree field = check_classfn (context, decl);
f30432d7
MS
3761 if (field && duplicate_decls (decl, field))
3762 decl = field;
3763 }
3764
3765 /* cp_finish_decl sets DECL_EXTERNAL if DECL_IN_AGGR_P is set. */
dff3e828 3766 DECL_IN_AGGR_P (decl) = 0;
68642fb6 3767 if ((DECL_LANG_SPECIFIC (decl) && DECL_USE_TEMPLATE (decl))
1f6e1acc 3768 || CLASSTYPE_TEMPLATE_INSTANTIATION (context))
84e6233f
JM
3769 {
3770 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
3771 /* [temp.expl.spec] An explicit specialization of a static data
3772 member of a template is a definition if the declaration
3773 includes an initializer; otherwise, it is a declaration.
3774
3775 We check for processing_specialization so this only applies
3776 to the new specialization syntax. */
3777 if (DECL_INITIAL (decl) == NULL_TREE && processing_specialization)
3778 DECL_EXTERNAL (decl) = 1;
3779 }
f30432d7 3780
b7698cf0 3781 if (DECL_EXTERNAL (decl) && ! DECL_TEMPLATE_SPECIALIZATION (decl))
33bd39a2 3782 pedwarn ("declaration of `%#D' outside of class is not definition",
f30432d7 3783 decl);
5b605f68
MS
3784 }
3785
9188c363
MM
3786 /* Enter this declaration into the symbol table. */
3787 tem = maybe_push_decl (decl);
2ee887f2 3788
5156628f 3789 if (processing_template_decl)
cd9f6678 3790 tem = push_template_decl (tem);
5566b478 3791
2ee887f2 3792#if ! defined (ASM_OUTPUT_BSS) && ! defined (ASM_OUTPUT_ALIGNED_BSS)
a3203465 3793 /* Tell the back-end to use or not use .common as appropriate. If we say
a50f0918
MS
3794 -fconserve-space, we want this to save .data space, at the expense of
3795 wrong semantics. If we say -fno-conserve-space, we want this to
3796 produce errors about redefs; to do this we force variables into the
3797 data segment. */
ca5dd8b7
JJ
3798 DECL_COMMON (tem) = ((TREE_CODE (tem) != VAR_DECL
3799 || !DECL_THREAD_LOCAL (tem))
c456a45a 3800 && (flag_conserve_space || ! TREE_PUBLIC (tem)));
2ee887f2 3801#endif
68642fb6 3802
5156628f 3803 if (! processing_template_decl)
5566b478 3804 start_decl_1 (tem);
8d08fdba 3805
8d08fdba
MS
3806 return tem;
3807}
3808
5566b478 3809void
11f6b451 3810start_decl_1 (tree decl)
8d08fdba 3811{
5566b478
MS
3812 tree type = TREE_TYPE (decl);
3813 int initialized = (DECL_INITIAL (decl) != NULL_TREE);
8d08fdba 3814
44689c12
ML
3815 if (type == error_mark_node)
3816 return;
3817
bd0d5d4a 3818 maybe_push_cleanup_level (type);
5566b478
MS
3819
3820 if (initialized)
3821 /* Is it valid for this decl to have an initializer at all?
3822 If not, set INITIALIZED to zero, which will indirectly
3823 tell `cp_finish_decl' to ignore the initializer once it is parsed. */
8d08fdba 3824 {
5566b478
MS
3825 /* Don't allow initializations for incomplete types except for
3826 arrays which might be completed by the initialization. */
d0f062fb 3827 if (COMPLETE_TYPE_P (complete_type (type)))
5566b478
MS
3828 ; /* A complete type is ok. */
3829 else if (TREE_CODE (type) != ARRAY_TYPE)
8d08fdba 3830 {
33bd39a2 3831 error ("variable `%#D' has initializer but incomplete type",
5566b478
MS
3832 decl);
3833 initialized = 0;
25eb19ff 3834 type = TREE_TYPE (decl) = error_mark_node;
5566b478 3835 }
d0f062fb 3836 else if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (type))))
5566b478
MS
3837 {
3838 if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
33bd39a2 3839 error ("elements of array `%#D' have incomplete type", decl);
5566b478
MS
3840 /* else we already gave an error in start_decl. */
3841 initialized = 0;
8d08fdba 3842 }
8d08fdba
MS
3843 }
3844
5566b478
MS
3845 if (!initialized
3846 && TREE_CODE (decl) != TYPE_DECL
3847 && TREE_CODE (decl) != TEMPLATE_DECL
07c88314 3848 && type != error_mark_node
5362b086 3849 && IS_AGGR_TYPE (type)
07c88314 3850 && ! DECL_EXTERNAL (decl))
8d08fdba 3851 {
5156628f 3852 if ((! processing_template_decl || ! uses_template_parms (type))
d0f062fb 3853 && !COMPLETE_TYPE_P (complete_type (type)))
5566b478 3854 {
0154eaa8 3855 error ("aggregate `%#D' has incomplete type and cannot be defined",
5566b478
MS
3856 decl);
3857 /* Change the type so that assemble_variable will give
3858 DECL an rtl we can live with: (mem (const_int 0)). */
25eb19ff 3859 type = TREE_TYPE (decl) = error_mark_node;
5566b478
MS
3860 }
3861 else
3862 {
3863 /* If any base type in the hierarchy of TYPE needs a constructor,
3864 then we set initialized to 1. This way any nodes which are
3865 created for the purposes of initializing this aggregate
3866 will live as long as it does. This is necessary for global
3867 aggregates which do not have their initializers processed until
3868 the end of the file. */
3869 initialized = TYPE_NEEDS_CONSTRUCTING (type);
3870 }
3871 }
3872
5566b478
MS
3873 if (! initialized)
3874 DECL_INITIAL (decl) = NULL_TREE;
3875}
3876
7e99327d
MM
3877/* Handle initialization of references. DECL, TYPE, and INIT have the
3878 same meaning as in cp_finish_decl. *CLEANUP must be NULL on entry,
3879 but will be set to a new CLEANUP_STMT if a temporary is created
3880 that must be destroeyd subsequently.
3881
3882 Returns an initializer expression to use to initialize DECL, or
3883 NULL if the initialization can be performed statically.
e92cc029
MS
3884
3885 Quotes on semantics can be found in ARM 8.4.3. */
3886
8e4ce833 3887static tree
7e99327d 3888grok_reference_init (tree decl, tree type, tree init, tree *cleanup)
5566b478
MS
3889{
3890 tree tmp;
3891
3892 if (init == NULL_TREE)
3893 {
3894 if ((DECL_LANG_SPECIFIC (decl) == 0
3895 || DECL_IN_AGGR_P (decl) == 0)
3896 && ! DECL_THIS_EXTERN (decl))
33bd39a2 3897 error ("`%D' declared as reference but not initialized", decl);
8e4ce833 3898 return NULL_TREE;
5566b478
MS
3899 }
3900
ed5511d9 3901 if (TREE_CODE (init) == CONSTRUCTOR)
5566b478 3902 {
33bd39a2 3903 error ("ISO C++ forbids use of initializer list to initialize reference `%D'", decl);
8e4ce833 3904 return NULL_TREE;
8d08fdba
MS
3905 }
3906
3907 if (TREE_CODE (init) == TREE_LIST)
c7b62f14 3908 init = build_x_compound_expr_from_list (init, "initializer");
8d08fdba 3909
8ccc31eb
MS
3910 if (TREE_CODE (TREE_TYPE (init)) == REFERENCE_TYPE)
3911 init = convert_from_reference (init);
3912
8d08fdba
MS
3913 if (TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE
3914 && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE)
0a72704b
MM
3915 /* Note: default conversion is only called in very special cases. */
3916 init = decay_conversion (init);
68642fb6 3917
24bef158
MM
3918 /* Convert INIT to the reference type TYPE. This may involve the
3919 creation of a temporary, whose lifetime must be the same as that
3920 of the reference. If so, a DECL_STMT for the temporary will be
3921 added just after the DECL_STMT for DECL. That's why we don't set
3922 DECL_INITIAL for local references (instead assigning to them
3923 explicitly); we need to allow the temporary to be initialized
3924 first. */
7e99327d 3925 tmp = initialize_reference (type, init, decl, cleanup);
8d08fdba 3926
a3203465 3927 if (tmp == error_mark_node)
8e4ce833
JJ
3928 return NULL_TREE;
3929 else if (tmp == NULL_TREE)
8d08fdba 3930 {
33bd39a2 3931 error ("cannot initialize `%T' from `%T'", type, TREE_TYPE (init));
8e4ce833 3932 return NULL_TREE;
8d08fdba 3933 }
8d08fdba 3934
8e4ce833
JJ
3935 if (TREE_STATIC (decl) && !TREE_CONSTANT (tmp))
3936 return tmp;
3937
08ac397c 3938 DECL_INITIAL (decl) = tmp;
8e4ce833
JJ
3939
3940 return NULL_TREE;
8d08fdba
MS
3941}
3942
27778b73
MM
3943/* When parsing `int a[] = {1, 2};' we don't know the size of the
3944 array until we finish parsing the initializer. If that's the
3945 situation we're in, update DECL accordingly. */
3946
3947static void
11f6b451 3948maybe_deduce_size_from_array_init (tree decl, tree init)
27778b73
MM
3949{
3950 tree type = TREE_TYPE (decl);
3951
3952 if (TREE_CODE (type) == ARRAY_TYPE
3953 && TYPE_DOMAIN (type) == NULL_TREE
3954 && TREE_CODE (decl) != TYPE_DECL)
3955 {
f2ae0c45
JM
3956 /* do_default is really a C-ism to deal with tentative definitions.
3957 But let's leave it here to ease the eventual merge. */
3958 int do_default = !DECL_EXTERNAL (decl);
27778b73
MM
3959 tree initializer = init ? init : DECL_INITIAL (decl);
3960 int failure = complete_array_type (type, initializer, do_default);
3961
3962 if (failure == 1)
33bd39a2 3963 error ("initializer fails to determine size of `%D'", decl);
27778b73
MM
3964
3965 if (failure == 2)
3966 {
3967 if (do_default)
33bd39a2 3968 error ("array size missing in `%D'", decl);
27778b73
MM
3969 /* If a `static' var's size isn't known, make it extern as
3970 well as static, so it does not get allocated. If it's not
3971 `static', then don't mark it extern; finish_incomplete_decl
3972 will give it a default size and it will get allocated. */
3973 else if (!pedantic && TREE_STATIC (decl) && !TREE_PUBLIC (decl))
3974 DECL_EXTERNAL (decl) = 1;
3975 }
3976
3977 if (pedantic && TYPE_DOMAIN (type) != NULL_TREE
3978 && tree_int_cst_lt (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
3979 integer_zero_node))
33bd39a2 3980 error ("zero-size array `%D'", decl);
27778b73
MM
3981
3982 layout_decl (decl, 0);
3983 }
3984}
3985
3986/* Set DECL_SIZE, DECL_ALIGN, etc. for DECL (a VAR_DECL), and issue
57b52417 3987 any appropriate error messages regarding the layout. */
27778b73 3988
57b52417 3989static void
11f6b451 3990layout_var_decl (tree decl)
27778b73 3991{
57b52417 3992 tree type = TREE_TYPE (decl);
c95cd22e 3993#if 0
57b52417 3994 tree ttype = target_type (type);
c95cd22e 3995#endif
57b52417
MM
3996
3997 /* If we haven't already layed out this declaration, do so now.
3998 Note that we must not call complete type for an external object
3999 because it's type might involve templates that we are not
34cd5ae7 4000 supposed to instantiate yet. (And it's perfectly valid to say
57b52417
MM
4001 `extern X x' for some incomplete type `X'.) */
4002 if (!DECL_EXTERNAL (decl))
4003 complete_type (type);
7de85f7e 4004 if (!DECL_SIZE (decl)
328de7c2 4005 && TREE_TYPE (decl) != error_mark_node
7de85f7e
MM
4006 && (COMPLETE_TYPE_P (type)
4007 || (TREE_CODE (type) == ARRAY_TYPE
4008 && !TYPE_DOMAIN (type)
4009 && COMPLETE_TYPE_P (TREE_TYPE (type)))))
27778b73
MM
4010 layout_decl (decl, 0);
4011
c82dbd95 4012 if (!DECL_EXTERNAL (decl) && DECL_SIZE (decl) == NULL_TREE)
27778b73
MM
4013 {
4014 /* An automatic variable with an incomplete type: that is an error.
4015 Don't talk about array types here, since we took care of that
4016 message in grokdeclarator. */
33bd39a2 4017 error ("storage size of `%D' isn't known", decl);
27778b73
MM
4018 TREE_TYPE (decl) = error_mark_node;
4019 }
ae673f14
JM
4020#if 0
4021 /* Keep this code around in case we later want to control debug info
4022 based on whether a type is "used". (jason 1999-11-11) */
4023
27778b73
MM
4024 else if (!DECL_EXTERNAL (decl) && IS_AGGR_TYPE (ttype))
4025 /* Let debugger know it should output info for this type. */
4026 note_debug_info_needed (ttype);
4027
4028 if (TREE_STATIC (decl) && DECL_CLASS_SCOPE_P (decl))
4029 note_debug_info_needed (DECL_CONTEXT (decl));
ae673f14 4030#endif
27778b73
MM
4031
4032 if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl))
4033 && DECL_SIZE (decl) != NULL_TREE
4034 && ! TREE_CONSTANT (DECL_SIZE (decl)))
4035 {
4036 if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
4037 constant_expression_warning (DECL_SIZE (decl));
4038 else
33bd39a2 4039 error ("storage size of `%D' isn't constant", decl);
27778b73 4040 }
8e4ce833
JJ
4041
4042 if (TREE_STATIC (decl)
4043 && !DECL_ARTIFICIAL (decl)
4044 && current_function_decl
4045 && DECL_CONTEXT (decl) == current_function_decl)
4046 push_local_name (decl);
27778b73
MM
4047}
4048
27778b73
MM
4049/* If a local static variable is declared in an inline function, or if
4050 we have a weak definition, we must endeavor to create only one
4051 instance of the variable at link-time. */
4052
4053static void
11f6b451 4054maybe_commonize_var (tree decl)
27778b73
MM
4055{
4056 /* Static data in a function with comdat linkage also has comdat
4057 linkage. */
4058 if (TREE_STATIC (decl)
4059 /* Don't mess with __FUNCTION__. */
cf74fb86 4060 && ! DECL_ARTIFICIAL (decl)
0b50d7f1
MM
4061 && DECL_FUNCTION_SCOPE_P (decl)
4062 /* Unfortunately, import_export_decl has not always been called
4063 before the function is processed, so we cannot simply check
4064 DECL_COMDAT. */
9e6aaf5e 4065 && (DECL_COMDAT (DECL_CONTEXT (decl))
0b50d7f1
MM
4066 || ((DECL_DECLARED_INLINE_P (DECL_CONTEXT (decl))
4067 || DECL_TEMPLATE_INSTANTIATION (DECL_CONTEXT (decl)))
4068 && TREE_PUBLIC (DECL_CONTEXT (decl)))))
27778b73 4069 {
0dbc5cd3 4070 if (flag_weak)
27778b73 4071 {
0dbc5cd3
MM
4072 /* With weak symbols, we simply make the variable COMDAT;
4073 that will cause copies in multiple translations units to
4074 be merged. */
4075 comdat_linkage (decl);
4076 }
4077 else
4078 {
4079 if (DECL_INITIAL (decl) == NULL_TREE
4080 || DECL_INITIAL (decl) == error_mark_node)
27778b73 4081 {
0dbc5cd3
MM
4082 /* Without weak symbols, we can use COMMON to merge
4083 uninitialized variables. */
27778b73
MM
4084 TREE_PUBLIC (decl) = 1;
4085 DECL_COMMON (decl) = 1;
4086 }
0dbc5cd3 4087 else
27778b73 4088 {
0dbc5cd3
MM
4089 /* While for initialized variables, we must use internal
4090 linkage -- which means that multiple copies will not
4091 be merged. */
4092 TREE_PUBLIC (decl) = 0;
4093 DECL_COMMON (decl) = 0;
27778b73 4094 cp_warning_at ("sorry: semantics of inline function static data `%#D' are wrong (you'll wind up with multiple copies)", decl);
ddd2d57e
RH
4095 warning ("%J you can work around this by removing the initializer",
4096 decl);
27778b73
MM
4097 }
4098 }
4099 }
4100 else if (DECL_LANG_SPECIFIC (decl) && DECL_COMDAT (decl))
4101 /* Set it up again; we might have set DECL_INITIAL since the last
4102 time. */
4103 comdat_linkage (decl);
4104}
4105
91063b51
MM
4106/* Issue an error message if DECL is an uninitialized const variable. */
4107
4108static void
11f6b451 4109check_for_uninitialized_const_var (tree decl)
91063b51
MM
4110{
4111 tree type = TREE_TYPE (decl);
4112
4113 /* ``Unless explicitly declared extern, a const object does not have
4114 external linkage and must be initialized. ($8.4; $12.1)'' ARM
4115 7.1.6 */
4116 if (TREE_CODE (decl) == VAR_DECL
4117 && TREE_CODE (type) != REFERENCE_TYPE
4118 && CP_TYPE_CONST_P (type)
4119 && !TYPE_NEEDS_CONSTRUCTING (type)
4120 && !DECL_INITIAL (decl))
33bd39a2 4121 error ("uninitialized const `%D'", decl);
91063b51
MM
4122}
4123
8e3df2de
MM
4124/* FIELD is a FIELD_DECL or NULL. In the former case, the value
4125 returned is the next FIELD_DECL (possibly FIELD itself) that can be
4126 initialized. If there are no more such fields, the return value
4127 will be NULL. */
4128
4129static tree
4130next_initializable_field (tree field)
4131{
4132 while (field
4133 && (TREE_CODE (field) != FIELD_DECL
4134 || (DECL_C_BIT_FIELD (field) && !DECL_NAME (field))
4135 || DECL_ARTIFICIAL (field)))
4136 field = TREE_CHAIN (field);
4137
4138 return field;
4139}
4140
4141/* Undo the brace-elision allowed by [dcl.init.aggr] in a
4142 brace-enclosed aggregate initializer.
4143
4144 *INITP is one of a list of initializers describing a brace-enclosed
4145 initializer for an entity of the indicated aggregate TYPE. It may
4146 not presently match the shape of the TYPE; for example:
4147
4148 struct S { int a; int b; };
4149 struct S a[] = { 1, 2, 3, 4 };
4150
4151 Here *INITP will point to TREE_LIST of four elements, rather than a
4152 list of two elements, each itself a list of two elements. This
4153 routine transforms INIT from the former form into the latter. The
4154 revised initializer is returned. */
4155
4156static tree
4157reshape_init (tree type, tree *initp)
4158{
4159 tree inits;
4160 tree old_init;
4161 tree old_init_value;
4162 tree new_init;
4163 bool brace_enclosed_p;
4164
4165 old_init = *initp;
4166 old_init_value = (TREE_CODE (*initp) == TREE_LIST
4167 ? TREE_VALUE (*initp) : old_init);
4168
ee20f4ee 4169 my_friendly_assert (old_init_value, 20030723);
ee7ecb29 4170
8e3df2de
MM
4171 /* If the initializer is brace-enclosed, pull initializers from the
4172 enclosed elements. Advance past the brace-enclosed initializer
4173 now. */
dbe85b80
JM
4174 if (TREE_CODE (old_init_value) == CONSTRUCTOR
4175 && TREE_TYPE (old_init_value) == NULL_TREE
8e3df2de
MM
4176 && TREE_HAS_CONSTRUCTOR (old_init_value))
4177 {
4178 *initp = TREE_CHAIN (old_init);
4179 TREE_CHAIN (old_init) = NULL_TREE;
4180 inits = CONSTRUCTOR_ELTS (old_init_value);
4181 initp = &inits;
4182 brace_enclosed_p = true;
4183 }
4184 else
4185 {
4186 inits = NULL_TREE;
4187 brace_enclosed_p = false;
4188 }
4189
4190 /* A non-aggregate type is always initialized with a single
4191 initializer. */
4192 if (!CP_AGGREGATE_TYPE_P (type))
4193 {
4194 *initp = TREE_CHAIN (old_init);
4195 TREE_CHAIN (old_init) = NULL_TREE;
4196 /* It is invalid to initialize a non-aggregate type with a
4197 brace-enclosed initializer. */
4198 if (brace_enclosed_p)
4199 {
4200 error ("brace-enclosed initializer used to initialize `%T'",
4201 type);
4202 if (TREE_CODE (old_init) == TREE_LIST)
4203 TREE_VALUE (old_init) = error_mark_node;
4204 else
4205 old_init = error_mark_node;
4206 }
4207
4208 return old_init;
4209 }
4210
4211 /* [dcl.init.aggr]
4212
4213 All implicit type conversions (clause _conv_) are considered when
4214 initializing the aggregate member with an initializer from an
4215 initializer-list. If the initializer can initialize a member,
4216 the member is initialized. Otherwise, if the member is itself a
4217 non-empty subaggregate, brace elision is assumed and the
4218 initializer is considered for the initialization of the first
4219 member of the subaggregate. */
dbe85b80 4220 if (!brace_enclosed_p
8e3df2de
MM
4221 && can_convert_arg (type, TREE_TYPE (old_init_value), old_init_value))
4222 {
4223 *initp = TREE_CHAIN (old_init);
4224 TREE_CHAIN (old_init) = NULL_TREE;
4225 return old_init;
4226 }
4227
3fa3c4bd 4228 if (TREE_CODE (old_init_value) == STRING_CST
8e3df2de 4229 && TREE_CODE (type) == ARRAY_TYPE
42328048 4230 && char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type))))
8e3df2de
MM
4231 {
4232 /* [dcl.init.string]
4233
4234 A char array (whether plain char, signed char, or unsigned char)
4235 can be initialized by a string-literal (optionally enclosed in
4236 braces); a wchar_t array can be initialized by a wide
4237 string-literal (optionally enclosed in braces). */
4238 new_init = old_init;
4239 /* Move past the initializer. */
4240 *initp = TREE_CHAIN (old_init);
4241 TREE_CHAIN (old_init) = NULL_TREE;
4242 }
4243 else
4244 {
4245 /* Build a CONSTRUCTOR to hold the contents of the aggregate. */
dcf92453 4246 new_init = build_constructor (type, NULL_TREE);
8e3df2de
MM
4247 TREE_HAS_CONSTRUCTOR (new_init) = 1;
4248
4249 if (CLASS_TYPE_P (type))
4250 {
4251 tree field;
4252
4253 field = next_initializable_field (TYPE_FIELDS (type));
4254
4255 if (!field)
4256 {
4257 /* [dcl.init.aggr]
4258
4259 An initializer for an aggregate member that is an
4260 empty class shall have the form of an empty
4261 initializer-list {}. */
4262 if (!brace_enclosed_p)
4263 error ("initializer for `%T' must be brace-enclosed",
4264 type);
4265 }
4266 else
4267 {
4268 /* Loop through the initializable fields, gathering
4269 initializers. */
446d5e47 4270 while (*initp)
8e3df2de
MM
4271 {
4272 tree field_init;
4273
446d5e47
MM
4274 /* Handle designated initializers, as an extension. */
4275 if (TREE_PURPOSE (*initp))
4276 {
4277 if (pedantic)
4278 pedwarn ("ISO C++ does not allow designated initializers");
4279 field = lookup_field_1 (type, TREE_PURPOSE (*initp),
4280 /*want_type=*/false);
4281 if (!field || TREE_CODE (field) != FIELD_DECL)
4282 error ("`%T' has no non-static data member named `%D'",
4283 type, TREE_PURPOSE (*initp));
4284 }
4285 if (!field)
4286 break;
4287
8e3df2de
MM
4288 field_init = reshape_init (TREE_TYPE (field), initp);
4289 TREE_CHAIN (field_init) = CONSTRUCTOR_ELTS (new_init);
4290 CONSTRUCTOR_ELTS (new_init) = field_init;
4291 /* [dcl.init.aggr]
4292
4293 When a union is initialized with a brace-enclosed
4294 initializer, the braces shall only contain an
4295 initializer for the first member of the union. */
4296 if (TREE_CODE (type) == UNION_TYPE)
4297 break;
8e3df2de
MM
4298 field = next_initializable_field (TREE_CHAIN (field));
4299 }
4300 }
4301 }
4302 else if (TREE_CODE (type) == ARRAY_TYPE)
4303 {
4304 tree index;
4305 tree max_index;
4306
4307 /* If the bound of the array is known, take no more initializers
4308 than are allowed. */
4309 max_index = (TYPE_DOMAIN (type)
4310 ? array_type_nelts (type) : NULL_TREE);
4311 /* Loop through the array elements, gathering initializers. */
4312 for (index = size_zero_node;
4313 *initp && (!max_index || !tree_int_cst_lt (max_index, index));
4314 index = size_binop (PLUS_EXPR, index, size_one_node))
4315 {
4316 tree element_init;
4317
4318 element_init = reshape_init (TREE_TYPE (type), initp);
4319 TREE_CHAIN (element_init) = CONSTRUCTOR_ELTS (new_init);
4320 CONSTRUCTOR_ELTS (new_init) = element_init;
4321 if (TREE_PURPOSE (element_init))
4322 index = TREE_PURPOSE (element_init);
4323 }
4324 }
4325 else
4326 abort ();
4327
4328 /* The initializers were placed in reverse order in the
4329 CONSTRUCTOR. */
4330 CONSTRUCTOR_ELTS (new_init) = nreverse (CONSTRUCTOR_ELTS (new_init));
4331
4332 if (TREE_CODE (old_init) == TREE_LIST)
4333 new_init = build_tree_list (TREE_PURPOSE (old_init), new_init);
4334 }
4335
4336 /* If this was a brace-enclosed initializer and all of the
4337 initializers were not used up, there is a problem. */
4338 if (brace_enclosed_p && *initp)
4339 error ("too many initializers for `%T'", type);
4340
4341 return new_init;
4342}
4343
c82dbd95 4344/* Verify INIT (the initializer for DECL), and record the
7e99327d
MM
4345 initialization in DECL_INITIAL, if appropriate. CLEANUP is as for
4346 grok_reference_init.
8e3df2de
MM
4347
4348 If the return value is non-NULL, it is an expression that must be
4349 evaluated dynamically to initialize DECL. */
27778b73 4350
c82dbd95 4351static tree
7e99327d 4352check_initializer (tree decl, tree init, int flags, tree *cleanup)
27778b73 4353{
17bbb839 4354 tree type = TREE_TYPE (decl);
fc0e7bf5 4355
27778b73
MM
4356 /* If `start_decl' didn't like having an initialization, ignore it now. */
4357 if (init != NULL_TREE && DECL_INITIAL (decl) == NULL_TREE)
4358 init = NULL_TREE;
27778b73 4359
17bbb839
MM
4360 /* If an initializer is present, DECL_INITIAL has been
4361 error_mark_node, to indicate that an as-of-yet unevaluated
4362 initialization will occur. From now on, DECL_INITIAL reflects
4363 the static initialization -- if any -- of DECL. */
4364 DECL_INITIAL (decl) = NULL_TREE;
4365
328de7c2
MM
4366 /* Things that are going to be initialized need to have complete
4367 type. */
4368 TREE_TYPE (decl) = type = complete_type (TREE_TYPE (decl));
c82dbd95 4369
328de7c2
MM
4370 if (type == error_mark_node)
4371 /* We will have already complained. */
4372 init = NULL_TREE;
4373 else if (init && COMPLETE_TYPE_P (type)
4374 && !TREE_CONSTANT (TYPE_SIZE (type)))
4375 {
4376 error ("variable-sized object `%D' may not be initialized", decl);
4377 init = NULL_TREE;
4378 }
4379 else if (TREE_CODE (type) == ARRAY_TYPE
4380 && !COMPLETE_TYPE_P (complete_type (TREE_TYPE (type))))
4381 {
4382 error ("elements of array `%#D' have incomplete type", decl);
4383 init = NULL_TREE;
4384 }
4385 else if (TREE_CODE (type) != ARRAY_TYPE && !COMPLETE_TYPE_P (type))
4386 {
4387 error ("`%D' has incomplete type", decl);
4388 TREE_TYPE (decl) = error_mark_node;
4389 init = NULL_TREE;
27778b73
MM
4390 }
4391
4392 if (TREE_CODE (decl) == CONST_DECL)
4393 {
4394 my_friendly_assert (TREE_CODE (decl) != REFERENCE_TYPE, 148);
4395
4396 DECL_INITIAL (decl) = init;
4397
27778b73
MM
4398 my_friendly_assert (init != NULL_TREE, 149);
4399 init = NULL_TREE;
4400 }
c82dbd95 4401 else if (!DECL_EXTERNAL (decl) && TREE_CODE (type) == REFERENCE_TYPE)
7e99327d 4402 init = grok_reference_init (decl, type, init, cleanup);
27778b73
MM
4403 else if (init)
4404 {
8e3df2de 4405 if (TREE_CODE (init) == CONSTRUCTOR && TREE_HAS_CONSTRUCTOR (init))
a16f2357
GK
4406 {
4407 /* [dcl.init] paragraph 13,
4408 If T is a scalar type, then a declaration of the form
4409 T x = { a };
4410 is equivalent to
4411 T x = a;
4412
4413 reshape_init will complain about the extra braces,
4414 and doesn't do anything useful in the case where TYPE is
4415 scalar, so just don't call it. */
4416 if (CP_AGGREGATE_TYPE_P (type))
4417 init = reshape_init (type, &init);
6e1b3a7c
AH
4418
4419 if ((*targetm.vector_opaque_p) (type))
4420 {
4421 error ("opaque vector types cannot be initialized");
4422 init = error_mark_node;
4423 }
a16f2357 4424 }
8e3df2de
MM
4425
4426 /* If DECL has an array type without a specific bound, deduce the
4427 array size from the initializer. */
4428 maybe_deduce_size_from_array_init (decl, init);
4429 type = TREE_TYPE (decl);
4430 if (TREE_CODE (init) == CONSTRUCTOR && TREE_HAS_CONSTRUCTOR (init))
4431 TREE_TYPE (init) = type;
4432
27778b73
MM
4433 if (TYPE_HAS_CONSTRUCTOR (type) || TYPE_NEEDS_CONSTRUCTING (type))
4434 {
4435 if (TREE_CODE (type) == ARRAY_TYPE)
8e3df2de 4436 goto initialize_aggr;
27778b73
MM
4437 else if (TREE_CODE (init) == CONSTRUCTOR
4438 && TREE_HAS_CONSTRUCTOR (init))
4439 {
4440 if (TYPE_NON_AGGREGATE_CLASS (type))
4441 {
33bd39a2 4442 error ("`%D' must be initialized by constructor, not by `{...}'",
8e3df2de 4443 decl);
27778b73
MM
4444 init = error_mark_node;
4445 }
4446 else
4447 goto dont_use_constructor;
4448 }
8e3df2de
MM
4449 else
4450 {
4451 int saved_stmts_are_full_exprs_p;
4452
4453 initialize_aggr:
4454 saved_stmts_are_full_exprs_p = 0;
4455 if (building_stmt_tree ())
4456 {
4457 saved_stmts_are_full_exprs_p = stmts_are_full_exprs_p ();
4458 current_stmt_tree ()->stmts_are_full_exprs_p = 1;
4459 }
4460 init = build_aggr_init (decl, init, flags);
4461 if (building_stmt_tree ())
4462 current_stmt_tree ()->stmts_are_full_exprs_p =
4463 saved_stmts_are_full_exprs_p;
4464 return init;
4465 }
27778b73
MM
4466 }
4467 else
4468 {
4469 dont_use_constructor:
4470 if (TREE_CODE (init) != TREE_VEC)
4471 init = store_init_value (decl, init);
4472 }
27778b73
MM
4473 }
4474 else if (DECL_EXTERNAL (decl))
4475 ;
8e3df2de
MM
4476 else if (TYPE_P (type) && TYPE_NEEDS_CONSTRUCTING (type))
4477 goto initialize_aggr;
4478 else if (IS_AGGR_TYPE (type))
27778b73
MM
4479 {
4480 tree core_type = strip_array_types (type);
4481
8e3df2de
MM
4482 if (CLASSTYPE_READONLY_FIELDS_NEED_INIT (core_type))
4483 error ("structure `%D' with uninitialized const members", decl);
4484 if (CLASSTYPE_REF_FIELDS_NEED_INIT (core_type))
4485 error ("structure `%D' with uninitialized reference members",
4486 decl);
27778b73
MM
4487
4488 check_for_uninitialized_const_var (decl);
27778b73
MM
4489 }
4490 else
4491 check_for_uninitialized_const_var (decl);
68642fb6 4492
8e3df2de
MM
4493 if (init && init != error_mark_node)
4494 init = build (INIT_EXPR, type, decl, init);
4495
c82dbd95 4496 return init;
27778b73
MM
4497}
4498
4499/* If DECL is not a local variable, give it RTL. */
4500
4501static void
11f6b451 4502make_rtl_for_nonlocal_decl (tree decl, tree init, const char* asmspec)
27778b73 4503{
95ee998c
MM
4504 int toplev = toplevel_bindings_p ();
4505 int defer_p;
27778b73 4506
f39ee884
MM
4507 /* Handle non-variables up front. */
4508 if (TREE_CODE (decl) != VAR_DECL)
4509 {
4510 rest_of_decl_compilation (decl, asmspec, toplev, at_eof);
4511 return;
4512 }
4513
95ee998c
MM
4514 /* If we see a class member here, it should be a static data
4515 member. */
4516 if (DECL_LANG_SPECIFIC (decl) && DECL_IN_AGGR_P (decl))
4517 {
4518 my_friendly_assert (TREE_STATIC (decl), 19990828);
4519 /* An in-class declaration of a static data member should be
4520 external; it is only a declaration, and not a definition. */
4521 if (init == NULL_TREE)
4522 my_friendly_assert (DECL_EXTERNAL (decl), 20000723);
4523 }
4524
f39ee884
MM
4525 /* Set the DECL_ASSEMBLER_NAME for the variable. */
4526 if (asmspec)
7ed47c04
MM
4527 {
4528 SET_DECL_ASSEMBLER_NAME (decl, get_identifier (asmspec));
4529 /* The `register' keyword, when used together with an
4530 asm-specification, indicates that the variable should be
4531 placed in a particular register. */
4532 if (DECL_REGISTER (decl))
4533 DECL_C_HARD_REGISTER (decl) = 1;
4534 }
f39ee884 4535
95ee998c
MM
4536 /* We don't create any RTL for local variables. */
4537 if (DECL_FUNCTION_SCOPE_P (decl) && !TREE_STATIC (decl))
4538 return;
27778b73 4539
95ee998c
MM
4540 /* We defer emission of local statics until the corresponding
4541 DECL_STMT is expanded. */
4542 defer_p = DECL_FUNCTION_SCOPE_P (decl) || DECL_VIRTUAL_P (decl);
4543
b602511f
JM
4544 /* We try to defer namespace-scope static constants so that they are
4545 not emitted into the object file unnecessarily. */
4546 if (!DECL_VIRTUAL_P (decl)
4547 && TREE_READONLY (decl)
4548 && DECL_INITIAL (decl) != NULL_TREE
4549 && DECL_INITIAL (decl) != error_mark_node
4550 && ! EMPTY_CONSTRUCTOR_P (DECL_INITIAL (decl))
4551 && toplev
4552 && !TREE_PUBLIC (decl))
5cc90635
JM
4553 {
4554 /* Fool with the linkage of static consts according to #pragma
4555 interface. */
4556 if (!interface_unknown && !TREE_PUBLIC (decl))
27778b73 4557 {
95ee998c
MM
4558 TREE_PUBLIC (decl) = 1;
4559 DECL_EXTERNAL (decl) = interface_only;
27778b73 4560 }
27778b73 4561
95ee998c 4562 defer_p = 1;
27778b73 4563 }
b602511f
JM
4564 /* Likewise for template instantiations. */
4565 else if (DECL_COMDAT (decl))
4566 defer_p = 1;
95ee998c 4567
92643fea
MM
4568 /* If we're deferring the variable, we only need to make RTL if
4569 there's an ASMSPEC. Otherwise, we'll lazily create it later when
4570 we need it. (There's no way to lazily create RTL for things that
4571 have assembly specs because the information about the specifier
4572 isn't stored in the tree, yet) */
4573 if (defer_p && asmspec)
6c418184 4574 make_decl_rtl (decl, asmspec);
95ee998c 4575 /* If we're not deferring, go ahead and assemble the variable. */
92643fea 4576 else if (!defer_p)
27778b73 4577 rest_of_decl_compilation (decl, asmspec, toplev, at_eof);
27778b73
MM
4578}
4579
ed5511d9 4580/* Generate code to initialize DECL (a local variable). */
27778b73 4581
8e3df2de 4582static void
11f6b451 4583initialize_local_var (tree decl, tree init)
27778b73 4584{
9ed9e79a 4585 tree type = TREE_TYPE (decl);
170b020f 4586 tree cleanup;
27778b73 4587
8e3df2de
MM
4588 my_friendly_assert (TREE_CODE (decl) == VAR_DECL
4589 || TREE_CODE (decl) == RESULT_DECL,
4590 20021010);
4591 my_friendly_assert (!TREE_STATIC (decl), 20021010);
b7b8bcd2 4592
8e3df2de 4593 if (DECL_SIZE (decl) == NULL_TREE)
b7b8bcd2
MM
4594 {
4595 /* If we used it already as memory, it must stay in memory. */
4596 DECL_INITIAL (decl) = NULL_TREE;
4597 TREE_ADDRESSABLE (decl) = TREE_USED (decl);
4598 }
4599
27778b73
MM
4600 if (DECL_SIZE (decl) && type != error_mark_node)
4601 {
4602 int already_used;
68642fb6 4603
27778b73 4604 /* Compute and store the initial value. */
27778b73
MM
4605 already_used = TREE_USED (decl) || TREE_USED (type);
4606
8e3df2de
MM
4607 /* Perform the initialization. */
4608 if (init)
27778b73 4609 {
24bef158
MM
4610 int saved_stmts_are_full_exprs_p;
4611
3a0d3e1e 4612 my_friendly_assert (building_stmt_tree (), 20000906);
f2c5f623 4613 saved_stmts_are_full_exprs_p = stmts_are_full_exprs_p ();
ae499cce 4614 current_stmt_tree ()->stmts_are_full_exprs_p = 1;
8e3df2de 4615 finish_expr_stmt (init);
5362b086 4616 current_stmt_tree ()->stmts_are_full_exprs_p =
ae499cce 4617 saved_stmts_are_full_exprs_p;
27778b73
MM
4618 }
4619
4620 /* Set this to 0 so we can tell whether an aggregate which was
4621 initialized was ever used. Don't do this if it has a
4622 destructor, so we don't complain about the 'resource
b7b8bcd2
MM
4623 allocation is initialization' idiom. Now set
4624 attribute((unused)) on types so decls of that type will be
4625 marked used. (see TREE_USED, above.) */
27778b73
MM
4626 if (TYPE_NEEDS_CONSTRUCTING (type)
4627 && ! already_used
834c6dff 4628 && TYPE_HAS_TRIVIAL_DESTRUCTOR (type)
27778b73
MM
4629 && DECL_NAME (decl))
4630 TREE_USED (decl) = 0;
b7b8bcd2 4631 else if (already_used)
27778b73
MM
4632 TREE_USED (decl) = 1;
4633 }
24bef158 4634
8e3df2de 4635 /* Generate a cleanup, if necessary. */
170b020f
MM
4636 cleanup = cxx_maybe_build_cleanup (decl);
4637 if (DECL_SIZE (decl) && cleanup)
4638 finish_decl_cleanup (decl, cleanup);
24bef158
MM
4639}
4640
8d08fdba
MS
4641/* Finish processing of a declaration;
4642 install its line number and initial value.
4643 If the length of an array type is not known before,
4644 it must be determined now, from the initial value, or it is an error.
4645
8b27e9ef 4646 INIT holds the value of an initializer that should be allowed to escape
8d08fdba
MS
4647 the normal rules.
4648
920f9474 4649 FLAGS is LOOKUP_ONLYCONVERTING if the = init syntax was used, else 0
f78c7bc6 4650 if the (init) syntax was used. */
8d08fdba
MS
4651
4652void
11f6b451 4653cp_finish_decl (tree decl, tree init, tree asmspec_tree, int flags)
8d08fdba 4654{
7e99327d 4655 tree type;
27778b73 4656 tree ttype = NULL_TREE;
7e99327d 4657 tree cleanup;
9c0758dd 4658 const char *asmspec = NULL;
8d08fdba
MS
4659 int was_readonly = 0;
4660
11325dcd
KL
4661 if (decl == error_mark_node)
4662 return;
4663 else if (! decl)
8d08fdba
MS
4664 {
4665 if (init)
8251199e 4666 error ("assignment (not initialization) in declaration");
8d08fdba
MS
4667 return;
4668 }
4669
170b020f
MM
4670 my_friendly_assert (TREE_CODE (decl) != RESULT_DECL, 20030619);
4671
7e99327d
MM
4672 /* Assume no cleanup is required. */
4673 cleanup = NULL_TREE;
4674
a4443a08 4675 /* If a name was specified, get the string. */
5f52c0e0 4676 if (global_scope_p (current_binding_level))
41c64394 4677 asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
8d08fdba 4678 if (asmspec_tree)
5cc90635 4679 asmspec = TREE_STRING_POINTER (asmspec_tree);
8d08fdba 4680
2c73f9f5
ML
4681 if (init && TREE_CODE (init) == NAMESPACE_DECL)
4682 {
33bd39a2 4683 error ("cannot initialize `%D' to namespace `%D'",
2c73f9f5
ML
4684 decl, init);
4685 init = NULL_TREE;
4686 }
4687
6ba89f8e 4688 if (current_class_type
4f1c5b7d 4689 && CP_DECL_CONTEXT (decl) == current_class_type
6ba89f8e
MM
4690 && TYPE_BEING_DEFINED (current_class_type)
4691 && (DECL_INITIAL (decl) || init))
3febd123 4692 DECL_INITIALIZED_IN_CLASS_P (decl) = 1;
6ba89f8e 4693
68642fb6 4694 if (TREE_CODE (decl) == VAR_DECL
9a68c51f
JM
4695 && DECL_CONTEXT (decl)
4696 && TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL
4697 && DECL_CONTEXT (decl) != current_namespace
4698 && init)
4699 {
a1c65f9f 4700 /* Leave the namespace of the object. */
9a68c51f
JM
4701 pop_decl_namespace ();
4702 }
4703
c82dbd95 4704 type = TREE_TYPE (decl);
8d08fdba 4705
f376e137 4706 if (type == error_mark_node)
df7f7b9c 4707 goto finish_end0;
5362b086 4708
a7a7710d
NS
4709 if (TYPE_HAS_MUTABLE_P (type))
4710 TREE_READONLY (decl) = 0;
24bef158 4711
5156628f 4712 if (processing_template_decl)
5566b478 4713 {
08ac397c 4714 /* Add this declaration to the statement-tree. */
170b020f 4715 if (at_function_scope_p ())
08ac397c
JM
4716 add_decl_stmt (decl);
4717
5566b478 4718 if (init && DECL_INITIAL (decl))
2a1e9fdd 4719 DECL_INITIAL (decl) = init;
5566b478
MS
4720 goto finish_end0;
4721 }
3e41d13b 4722
27778b73
MM
4723 /* Parameters are handled by store_parm_decls, not cp_finish_decl. */
4724 my_friendly_assert (TREE_CODE (decl) != PARM_DECL, 19990828);
4725
8d08fdba
MS
4726 /* Take care of TYPE_DECLs up front. */
4727 if (TREE_CODE (decl) == TYPE_DECL)
4728 {
a0a33927
MS
4729 if (type != error_mark_node
4730 && IS_AGGR_TYPE (type) && DECL_NAME (decl))
8d08fdba
MS
4731 {
4732 if (TREE_TYPE (DECL_NAME (decl)) && TREE_TYPE (decl) != type)
33bd39a2 4733 warning ("shadowing previous type declaration of `%#D'", decl);
4b0d3cbe 4734 set_identifier_type_value (DECL_NAME (decl), decl);
8d08fdba 4735 }
cffa8729
MS
4736
4737 /* If we have installed this as the canonical typedef for this
4738 type, and that type has not been defined yet, delay emitting
956d6950 4739 the debug information for it, as we will emit it later. */
d2e5ee5c 4740 if (TYPE_MAIN_DECL (TREE_TYPE (decl)) == decl
d0f062fb 4741 && !COMPLETE_TYPE_P (TREE_TYPE (decl)))
cffa8729
MS
4742 TYPE_DECL_SUPPRESS_DEBUG (decl) = 1;
4743
3e411c3f 4744 rest_of_decl_compilation (decl, NULL,
5566b478 4745 DECL_CONTEXT (decl) == NULL_TREE, at_eof);
8d08fdba
MS
4746 goto finish_end;
4747 }
3e41d13b 4748
8d08fdba 4749 if (TREE_CODE (decl) != FUNCTION_DECL)
3e41d13b 4750 ttype = target_type (type);
8d08fdba
MS
4751
4752 if (! DECL_EXTERNAL (decl) && TREE_READONLY (decl)
e455bc27
MM
4753 && (TYPE_NEEDS_CONSTRUCTING (type)
4754 || TREE_CODE (type) == REFERENCE_TYPE))
8d08fdba 4755 {
8d08fdba
MS
4756 /* Currently, GNU C++ puts constants in text space, making them
4757 impossible to initialize. In the future, one would hope for
4758 an operating system which understood the difference between
4759 initialization and the running of a program. */
4760 was_readonly = 1;
4761 TREE_READONLY (decl) = 0;
4762 }
4763
27778b73 4764 if (TREE_CODE (decl) == FIELD_DECL && asmspec)
8d08fdba 4765 {
27778b73
MM
4766 /* This must override the asm specifier which was placed by
4767 grokclassfn. Lay this out fresh. */
19e7881c 4768 SET_DECL_RTL (TREE_TYPE (decl), NULL_RTX);
92643fea 4769 SET_DECL_ASSEMBLER_NAME (decl, get_identifier (asmspec));
6c418184 4770 make_decl_rtl (decl, asmspec);
8d08fdba 4771 }
17bbb839
MM
4772 else if (TREE_CODE (decl) == VAR_DECL)
4773 {
4774 /* Only PODs can have thread-local storage. Other types may require
4775 various kinds of non-trivial initialization. */
4776 if (DECL_THREAD_LOCAL (decl) && !pod_type_p (TREE_TYPE (decl)))
4777 error ("`%D' cannot be thread-local because it has non-POD type `%T'",
4778 decl, TREE_TYPE (decl));
4779 /* Convert the initializer to the type of DECL, if we have not
4780 already initialized DECL. */
4781 if (!DECL_INITIALIZED_P (decl)
4782 /* If !DECL_EXTERNAL then DECL is being defined. In the
8e3df2de 4783 case of a static data member initialized inside the
17bbb839
MM
4784 class-specifier, there can be an initializer even if DECL
4785 is *not* defined. */
4786 && (!DECL_EXTERNAL (decl) || init))
4787 {
7e99327d 4788 init = check_initializer (decl, init, flags, &cleanup);
b4c20e52
MM
4789 /* Thread-local storage cannot be dynamically initialized. */
4790 if (DECL_THREAD_LOCAL (decl) && init)
4791 {
4792 error ("`%D' is thread-local and so cannot be dynamically "
4793 "initialized", decl);
4794 init = NULL_TREE;
4795 }
17bbb839
MM
4796 /* Handle:
4797
4798 [dcl.init]
4799
4800 The memory occupied by any object of static storage
4801 duration is zero-initialized at program startup before
4802 any other initialization takes place.
4803
4804 We cannot create an appropriate initializer until after
4805 the type of DECL is finalized. If DECL_INITIAL is set,
4806 then the DECL is statically initialized, and any
4807 necessary zero-initialization has already been performed. */
4808 if (TREE_STATIC (decl) && !DECL_INITIAL (decl))
4809 DECL_INITIAL (decl) = build_zero_init (TREE_TYPE (decl),
1cb8292f 4810 /*nelts=*/NULL_TREE,
17bbb839
MM
4811 /*static_storage_p=*/true);
4812 /* Remember that the initialization for this variable has
4813 taken place. */
4814 DECL_INITIALIZED_P (decl) = 1;
4815 }
d881ff25
MM
4816 /* If the variable has an array type, lay out the type, even if
4817 there is no initializer. It is valid to index through the
4818 array, and we must get TYPE_ALIGN set correctly on the array
4819 type. */
4820 else if (TREE_CODE (type) == ARRAY_TYPE)
4821 layout_type (type);
17bbb839 4822 }
3e41d13b 4823
57cf57fb
JM
4824 /* Add this declaration to the statement-tree. This needs to happen
4825 after the call to check_initializer so that the DECL_STMT for a
4826 reference temp is added before the DECL_STMT for the reference itself. */
170b020f 4827 if (at_function_scope_p ())
08ac397c
JM
4828 add_decl_stmt (decl);
4829
8d08fdba 4830 if (TREE_CODE (decl) == VAR_DECL)
57b52417 4831 layout_var_decl (decl);
8d08fdba
MS
4832
4833 /* Output the assembler code and/or RTL code for variables and functions,
4834 unless the type is an undefined structure or union.
4835 If not, it will get done when the type is completed. */
170b020f 4836 if (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == FUNCTION_DECL)
8d08fdba 4837 {
27778b73
MM
4838 if (TREE_CODE (decl) == VAR_DECL)
4839 maybe_commonize_var (decl);
8d08fdba 4840
27778b73 4841 make_rtl_for_nonlocal_decl (decl, init, asmspec);
8d08fdba 4842
68642fb6 4843 if (TREE_CODE (type) == FUNCTION_TYPE
27778b73 4844 || TREE_CODE (type) == METHOD_TYPE)
68642fb6 4845 abstract_virtuals_error (decl,
27778b73 4846 strip_array_types (TREE_TYPE (type)));
68642fb6 4847 else
27778b73 4848 abstract_virtuals_error (decl, strip_array_types (type));
8d08fdba 4849
8e3df2de
MM
4850 if (TREE_CODE (decl) == FUNCTION_DECL
4851 || TREE_TYPE (decl) == error_mark_node)
4852 /* No initialization required. */
faae18ab 4853 ;
67d743fe
MS
4854 else if (DECL_EXTERNAL (decl)
4855 && ! (DECL_LANG_SPECIFIC (decl)
4856 && DECL_NOT_REALLY_EXTERN (decl)))
5566b478
MS
4857 {
4858 if (init)
4859 DECL_INITIAL (decl) = init;
4860 }
8e3df2de 4861 else
8d08fdba 4862 {
8e3df2de
MM
4863 /* A variable definition. */
4864 if (DECL_FUNCTION_SCOPE_P (decl))
24bef158 4865 {
8e3df2de 4866 /* This is a local declaration. */
a7e8c268 4867 maybe_inject_for_scope_var (decl);
8e3df2de
MM
4868 /* Initialize the local variable. */
4869 if (processing_template_decl)
4870 {
4871 if (init || DECL_INITIAL (decl) == error_mark_node)
4872 DECL_INITIAL (decl) = init;
4873 }
4874 else if (!TREE_STATIC (decl))
4875 initialize_local_var (decl, init);
24bef158 4876 }
8e3df2de
MM
4877
4878 if (TREE_STATIC (decl))
9ed9e79a
MM
4879 expand_static_init (decl, init);
4880 }
8d08fdba
MS
4881 finish_end0:
4882
4883 /* Undo call to `pushclass' that was done in `start_decl'
4884 due to initialization of qualified member variable.
4885 I.e., Foo::x = 10; */
4886 {
4f1c5b7d 4887 tree context = CP_DECL_CONTEXT (decl);
8d08fdba 4888 if (context
2f939d94 4889 && TYPE_P (context)
8d08fdba
MS
4890 && (TREE_CODE (decl) == VAR_DECL
4891 /* We also have a pushclass done that we need to undo here
4892 if we're at top level and declare a method. */
5566b478
MS
4893 || TREE_CODE (decl) == FUNCTION_DECL)
4894 /* If size hasn't been set, we're still defining it,
4895 and therefore inside the class body; don't pop
4896 the binding level.. */
d0f062fb 4897 && COMPLETE_TYPE_P (context)
5566b478 4898 && context == current_class_type)
6b400b21 4899 pop_nested_class ();
8d08fdba
MS
4900 }
4901 }
4902
7e99327d
MM
4903 /* If a CLEANUP_STMT was created to destroy a temporary bound to a
4904 reference, insert it in the statement-tree now. */
4905 if (cleanup)
4906 add_stmt (cleanup);
4907
8d08fdba
MS
4908 finish_end:
4909
8d08fdba
MS
4910 if (was_readonly)
4911 TREE_READONLY (decl) = 1;
d1bd0ded
GK
4912
4913 /* If this was marked 'used', be sure it will be output. */
4914 if (lookup_attribute ("used", DECL_ATTRIBUTES (decl)))
4915 mark_referenced (DECL_ASSEMBLER_NAME (decl));
8d08fdba
MS
4916}
4917
82580166 4918/* This is here for a midend callback from c-common.c */
e92cc029 4919
82580166 4920void
11f6b451 4921finish_decl (tree decl, tree init, tree asmspec_tree)
82580166 4922{
cd9f6678 4923 cp_finish_decl (decl, init, asmspec_tree, 0);
82580166
MS
4924}
4925
db4283a0
MM
4926/* Returns a declaration for a VAR_DECL as if:
4927
4928 extern "C" TYPE NAME;
4929
4930 had been seen. Used to create compiler-generated global
4931 variables. */
4932
4933tree
11f6b451 4934declare_global_var (tree name, tree type)
db4283a0
MM
4935{
4936 tree decl;
4937
4938 push_to_top_level ();
4939 decl = build_decl (VAR_DECL, name, type);
4940 TREE_PUBLIC (decl) = 1;
4941 DECL_EXTERNAL (decl) = 1;
4942 DECL_ARTIFICIAL (decl) = 1;
4943 pushdecl (decl);
4944 cp_finish_decl (decl, NULL_TREE, NULL_TREE, 0);
4945 pop_from_top_level ();
4946
4947 return decl;
4948}
4949
4950/* Returns a pointer to the `atexit' function. Note that if
838dfd8a 4951 FLAG_USE_CXA_ATEXIT is nonzero, then this will actually be the new
db4283a0
MM
4952 `__cxa_atexit' function specified in the IA64 C++ ABI. */
4953
4954static tree
11f6b451 4955get_atexit_node (void)
db4283a0
MM
4956{
4957 tree atexit_fndecl;
4958 tree arg_types;
4959 tree fn_type;
4960 tree fn_ptr_type;
4961 const char *name;
4962
4963 if (atexit_node)
4964 return atexit_node;
4965
4966 if (flag_use_cxa_atexit)
4967 {
4968 /* The declaration for `__cxa_atexit' is:
4969
4970 int __cxa_atexit (void (*)(void *), void *, void *)
4971
4972 We build up the argument types and then then function type
4973 itself. */
68642fb6 4974
db4283a0
MM
4975 /* First, build the pointer-to-function type for the first
4976 argument. */
4977 arg_types = tree_cons (NULL_TREE, ptr_type_node, void_list_node);
4978 fn_type = build_function_type (void_type_node, arg_types);
4979 fn_ptr_type = build_pointer_type (fn_type);
4980 /* Then, build the rest of the argument types. */
4981 arg_types = tree_cons (NULL_TREE, ptr_type_node, void_list_node);
4982 arg_types = tree_cons (NULL_TREE, ptr_type_node, arg_types);
4983 arg_types = tree_cons (NULL_TREE, fn_ptr_type, arg_types);
4984 /* And the final __cxa_atexit type. */
4985 fn_type = build_function_type (integer_type_node, arg_types);
4986 fn_ptr_type = build_pointer_type (fn_type);
4987 name = "__cxa_atexit";
4988 }
4989 else
4990 {
4991 /* The declaration for `atexit' is:
68642fb6 4992
db4283a0
MM
4993 int atexit (void (*)());
4994
4995 We build up the argument types and then then function type
4996 itself. */
4997 fn_type = build_function_type (void_type_node, void_list_node);
4998 fn_ptr_type = build_pointer_type (fn_type);
4999 arg_types = tree_cons (NULL_TREE, fn_ptr_type, void_list_node);
5000 /* Build the final atexit type. */
5001 fn_type = build_function_type (integer_type_node, arg_types);
5002 name = "atexit";
5003 }
5004
5005 /* Now, build the function declaration. */
5006 push_lang_context (lang_name_c);
0c11ada6 5007 atexit_fndecl = build_library_fn_ptr (name, fn_type);
db4283a0
MM
5008 mark_used (atexit_fndecl);
5009 pop_lang_context ();
0a72704b 5010 atexit_node = decay_conversion (atexit_fndecl);
db4283a0
MM
5011
5012 return atexit_node;
5013}
5014
5015/* Returns the __dso_handle VAR_DECL. */
5016
5017static tree
11f6b451 5018get_dso_handle_node (void)
db4283a0
MM
5019{
5020 if (dso_handle_node)
5021 return dso_handle_node;
5022
5023 /* Declare the variable. */
5024 dso_handle_node = declare_global_var (get_identifier ("__dso_handle"),
5025 ptr_type_node);
5026
5027 return dso_handle_node;
5028}
5029
5030/* Begin a new function with internal linkage whose job will be simply
5031 to destroy some particular variable. */
5032
d192828a
GK
5033static GTY(()) int start_cleanup_cnt;
5034
db4283a0 5035static tree
11f6b451 5036start_cleanup_fn (void)
db4283a0 5037{
23ccd1f3 5038 int old_interface_only = interface_only;
db4283a0
MM
5039 int old_interface_unknown = interface_unknown;
5040 char name[32];
5041 tree parmtypes;
5042 tree fntype;
5043 tree fndecl;
5044
5045 push_to_top_level ();
5046
5047 /* No need to mangle this. */
5048 push_lang_context (lang_name_c);
5049
23ccd1f3 5050 interface_only = 0;
db4283a0
MM
5051 interface_unknown = 1;
5052
5053 /* Build the parameter-types. */
5054 parmtypes = void_list_node;
5055 /* Functions passed to __cxa_atexit take an additional parameter.
5056 We'll just ignore it. After we implement the new calling
5057 convention for destructors, we can eliminate the use of
5058 additional cleanup functions entirely in the -fnew-abi case. */
5059 if (flag_use_cxa_atexit)
5060 parmtypes = tree_cons (NULL_TREE, ptr_type_node, parmtypes);
5061 /* Build the function type itself. */
5062 fntype = build_function_type (void_type_node, parmtypes);
5063 /* Build the name of the function. */
d192828a 5064 sprintf (name, "__tcf_%d", start_cleanup_cnt++);
db4283a0
MM
5065 /* Build the function declaration. */
5066 fndecl = build_lang_decl (FUNCTION_DECL, get_identifier (name), fntype);
5067 /* It's a function with internal linkage, generated by the
5068 compiler. */
5069 TREE_PUBLIC (fndecl) = 0;
5070 DECL_ARTIFICIAL (fndecl) = 1;
5a728aca
MM
5071 /* Make the function `inline' so that it is only emitted if it is
5072 actually needed. It is unlikely that it will be inlined, since
aba649ba 5073 it is only called via a function pointer, but we avoid unnecessary
5a728aca
MM
5074 emissions this way. */
5075 DECL_INLINE (fndecl) = 1;
d4d1ebc1
JH
5076 DECL_DECLARED_INLINE_P (fndecl) = 1;
5077 DECL_INTERFACE_KNOWN (fndecl) = 1;
db4283a0
MM
5078 /* Build the parameter. */
5079 if (flag_use_cxa_atexit)
5080 {
5081 tree parmdecl;
5082
8e51619a 5083 parmdecl = cp_build_parm_decl (NULL_TREE, ptr_type_node);
db4283a0 5084 DECL_CONTEXT (parmdecl) = fndecl;
db4283a0
MM
5085 TREE_USED (parmdecl) = 1;
5086 DECL_ARGUMENTS (fndecl) = parmdecl;
5087 }
5088
09ed39ad 5089 pushdecl (fndecl);
db4283a0 5090 start_function (/*specs=*/NULL_TREE, fndecl, NULL_TREE, SF_PRE_PARSED);
db4283a0
MM
5091
5092 interface_unknown = old_interface_unknown;
23ccd1f3 5093 interface_only = old_interface_only;
db4283a0
MM
5094
5095 pop_lang_context ();
5096
5097 return current_function_decl;
5098}
5099
5100/* Finish the cleanup function begun by start_cleanup_fn. */
5101
5102static void
11f6b451 5103end_cleanup_fn (void)
db4283a0 5104{
8cd2462c 5105 expand_or_defer_fn (finish_function (0));
db4283a0
MM
5106
5107 pop_from_top_level ();
5108}
5109
bf419747
MM
5110/* Generate code to handle the destruction of DECL, an object with
5111 static storage duration. */
f0105ed3 5112
bf419747 5113void
11f6b451 5114register_dtor_fn (tree decl)
f0105ed3 5115{
db4283a0 5116 tree cleanup;
f0105ed3 5117 tree compound_stmt;
db4283a0
MM
5118 tree args;
5119 tree fcall;
f0105ed3 5120
834c6dff 5121 if (TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
bf419747
MM
5122 return;
5123
f0105ed3
MM
5124 /* Call build_cleanup before we enter the anonymous function so that
5125 any access checks will be done relative to the current scope,
5126 rather than the scope of the anonymous function. */
5127 build_cleanup (decl);
5128
5129 /* Now start the function. */
db4283a0 5130 cleanup = start_cleanup_fn ();
f0105ed3
MM
5131
5132 /* Now, recompute the cleanup. It may contain SAVE_EXPRs that refer
5133 to the original function, rather than the anonymous one. That
5134 will make the back-end think that nested functions are in use,
5135 which causes confusion. */
78757caa
KL
5136
5137 push_deferring_access_checks (dk_no_check);
f0105ed3 5138 fcall = build_cleanup (decl);
78757caa 5139 pop_deferring_access_checks ();
f0105ed3
MM
5140
5141 /* Create the body of the anonymous function. */
7a3397c7 5142 compound_stmt = begin_compound_stmt (/*has_no_scope=*/false);
f0105ed3 5143 finish_expr_stmt (fcall);
7a3397c7 5144 finish_compound_stmt (compound_stmt);
db4283a0 5145 end_cleanup_fn ();
f0105ed3
MM
5146
5147 /* Call atexit with the cleanup function. */
dffd7eb6 5148 cxx_mark_addressable (cleanup);
bb4f6e6b 5149 mark_used (cleanup);
f0105ed3 5150 cleanup = build_unary_op (ADDR_EXPR, cleanup, 0);
db4283a0
MM
5151 if (flag_use_cxa_atexit)
5152 {
0f268005
MM
5153 args = tree_cons (NULL_TREE,
5154 build_unary_op (ADDR_EXPR, get_dso_handle_node (), 0),
5155 NULL_TREE);
db4283a0
MM
5156 args = tree_cons (NULL_TREE, null_pointer_node, args);
5157 args = tree_cons (NULL_TREE, cleanup, args);
5158 }
5159 else
5160 args = tree_cons (NULL_TREE, cleanup, NULL_TREE);
5161 finish_expr_stmt (build_function_call (get_atexit_node (), args));
f0105ed3
MM
5162}
5163
8e3df2de
MM
5164/* DECL is a VAR_DECL with static storage duration. INIT, if present,
5165 is its initializer. Generate code to handle the construction
5166 and destruction of DECL. */
5167
5168static void
11f6b451 5169expand_static_init (tree decl, tree init)
8d08fdba 5170{
8e3df2de
MM
5171 my_friendly_assert (TREE_CODE (decl) == VAR_DECL, 20021010);
5172 my_friendly_assert (TREE_STATIC (decl), 20021010);
5173
5174 /* Some variables require no initialization. */
5175 if (!init
5176 && !TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl))
5177 && TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
5178 return;
5179
4b0d3cbe 5180 if (! toplevel_bindings_p ())
8d08fdba
MS
5181 {
5182 /* Emit code to perform this initialization but once. */
b7b8bcd2 5183 tree if_stmt;
f0105ed3 5184 tree then_clause;
f1dedc31 5185 tree assignment;
c395453c
MM
5186 tree guard;
5187 tree guard_init;
8d08fdba 5188
2036a15c
MM
5189 /* Emit code to perform this initialization but once. This code
5190 looks like:
5191
c395453c
MM
5192 static int guard = 0;
5193 if (!guard) {
2036a15c 5194 // Do initialization.
c395453c 5195 guard = 1;
2036a15c
MM
5196 // Register variable for destruction at end of program.
5197 }
5198
5199 Note that the `temp' variable is only set to 1 *after* the
5200 initialization is complete. This ensures that an exception,
5201 thrown during the construction, will cause the variable to
5202 reinitialized when we pass through this code again, as per:
68642fb6 5203
2036a15c
MM
5204 [stmt.dcl]
5205
5206 If the initialization exits by throwing an exception, the
5207 initialization is not complete, so it will be tried again
5208 the next time control enters the declaration.
5209
5210 In theory, this process should be thread-safe, too; multiple
5211 threads should not be able to initialize the variable more
5212 than once. We don't yet attempt to ensure thread-safety. */
c395453c
MM
5213
5214 /* Create the guard variable. */
5215 guard = get_guard (decl);
2036a15c
MM
5216
5217 /* Begin the conditional initialization. */
b7b8bcd2 5218 if_stmt = begin_if_stmt ();
c395453c 5219 finish_if_stmt_cond (get_guard_cond (guard), if_stmt);
7a3397c7 5220 then_clause = begin_compound_stmt (/*has_no_scope=*/false);
72b7eeff 5221
2036a15c 5222 /* Do the initialization itself. */
8e3df2de 5223 assignment = init ? init : NULL_TREE;
f1dedc31
MM
5224
5225 /* Once the assignment is complete, set TEMP to 1. Since the
5226 construction of the static object is complete at this point,
5227 we want to make sure TEMP is set to 1 even if a temporary
5228 constructed during the initialization throws an exception
5229 when it is destroyed. So, we combine the initialization and
5230 the assignment to TEMP into a single expression, ensuring
5231 that when we call finish_expr_stmt the cleanups will not be
5232 run until after TEMP is set to 1. */
c395453c 5233 guard_init = set_guard (guard);
f1dedc31 5234 if (assignment)
c7b62f14 5235 assignment = build_compound_expr (assignment, guard_init);
f1dedc31 5236 else
c395453c 5237 assignment = guard_init;
f1dedc31 5238 finish_expr_stmt (assignment);
72b7eeff 5239
2036a15c
MM
5240 /* Use atexit to register a function for destroying this static
5241 variable. */
bf419747 5242 register_dtor_fn (decl);
72b7eeff 5243
7a3397c7 5244 finish_compound_stmt (then_clause);
b7b8bcd2
MM
5245 finish_then_clause (if_stmt);
5246 finish_if_stmt ();
8d08fdba
MS
5247 }
5248 else
bbd15aac 5249 static_aggregates = tree_cons (init, decl, static_aggregates);
8d08fdba 5250}
3c5c0849
MM
5251
5252/* Finish the declaration of a catch-parameter. */
5253
b35d4555 5254tree
11f6b451 5255start_handler_parms (tree declspecs, tree declarator)
3c5c0849
MM
5256{
5257 tree decl;
5258 if (declspecs)
5259 {
5260 decl = grokdeclarator (declarator, declspecs, CATCHPARM,
91d231cb 5261 1, NULL);
3c5c0849
MM
5262 if (decl == NULL_TREE)
5263 error ("invalid catch parameter");
5264 }
5265 else
5266 decl = NULL_TREE;
b35d4555
MM
5267
5268 return decl;
3c5c0849
MM
5269}
5270
8d08fdba
MS
5271\f
5272/* Make TYPE a complete type based on INITIAL_VALUE.
5273 Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
6ab5c740 5274 2 if there was no information (in which case assume 0 if DO_DEFAULT). */
8d08fdba
MS
5275
5276int
11f6b451 5277complete_array_type (tree type, tree initial_value, int do_default)
8d08fdba
MS
5278{
5279 register tree maxindex = NULL_TREE;
5280 int value = 0;
68642fb6 5281
8d08fdba
MS
5282 if (initial_value)
5283 {
7b019c19
MM
5284 /* An array of character type can be initialized from a
5285 brace-enclosed string constant. */
5286 if (char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type)))
5287 && TREE_CODE (initial_value) == CONSTRUCTOR
5288 && CONSTRUCTOR_ELTS (initial_value)
5289 && (TREE_CODE (TREE_VALUE (CONSTRUCTOR_ELTS (initial_value)))
5290 == STRING_CST)
5291 && TREE_CHAIN (CONSTRUCTOR_ELTS (initial_value)) == NULL_TREE)
5292 initial_value = TREE_VALUE (CONSTRUCTOR_ELTS (initial_value));
5293
5294 /* Note MAXINDEX is really the maximum index, one less than the
5295 size. */
8d08fdba 5296 if (TREE_CODE (initial_value) == STRING_CST)
e1cd6e56
MS
5297 {
5298 int eltsize
5299 = int_size_in_bytes (TREE_TYPE (TREE_TYPE (initial_value)));
5300 maxindex = build_int_2 ((TREE_STRING_LENGTH (initial_value)
5301 / eltsize) - 1, 0);
5302 }
8d08fdba
MS
5303 else if (TREE_CODE (initial_value) == CONSTRUCTOR)
5304 {
e1cd6e56 5305 tree elts = CONSTRUCTOR_ELTS (initial_value);
fed3cef0
RK
5306
5307 maxindex = ssize_int (-1);
e1cd6e56
MS
5308 for (; elts; elts = TREE_CHAIN (elts))
5309 {
5310 if (TREE_PURPOSE (elts))
5311 maxindex = TREE_PURPOSE (elts);
5312 else
fed3cef0 5313 maxindex = size_binop (PLUS_EXPR, maxindex, ssize_int (1));
e1cd6e56
MS
5314 }
5315 maxindex = copy_node (maxindex);
8d08fdba
MS
5316 }
5317 else
5318 {
5319 /* Make an error message unless that happened already. */
5320 if (initial_value != error_mark_node)
5321 value = 1;
0db982be
ML
5322 else
5323 initial_value = NULL_TREE;
8d08fdba
MS
5324
5325 /* Prevent further error messages. */
5326 maxindex = build_int_2 (0, 0);
5327 }
5328 }
5329
5330 if (!maxindex)
5331 {
5332 if (do_default)
5333 maxindex = build_int_2 (0, 0);
5334 value = 2;
5335 }
5336
5337 if (maxindex)
5338 {
51c184be 5339 tree itype;
6ab5c740
NS
5340 tree domain;
5341
5342 domain = build_index_type (maxindex);
5343 TYPE_DOMAIN (type) = domain;
51c184be 5344
dff6b454 5345 if (! TREE_TYPE (maxindex))
6ab5c740 5346 TREE_TYPE (maxindex) = domain;
51c184be
MS
5347 if (initial_value)
5348 itype = TREE_TYPE (initial_value);
5349 else
5350 itype = NULL;
5351 if (itype && !TYPE_DOMAIN (itype))
6ab5c740 5352 TYPE_DOMAIN (itype) = domain;
dff6b454
RK
5353 /* The type of the main variant should never be used for arrays
5354 of different sizes. It should only ever be completed with the
5355 size of the array. */
5356 if (! TYPE_DOMAIN (TYPE_MAIN_VARIANT (type)))
6ab5c740 5357 TYPE_DOMAIN (TYPE_MAIN_VARIANT (type)) = domain;
8d08fdba
MS
5358 }
5359
5360 /* Lay out the type now that we can get the real answer. */
5361
5362 layout_type (type);
5363
5364 return value;
5365}
5366\f
5367/* Return zero if something is declared to be a member of type
5368 CTYPE when in the context of CUR_TYPE. STRING is the error
5369 message to print in that case. Otherwise, quietly return 1. */
e92cc029 5370
8d08fdba 5371static int
11f6b451 5372member_function_or_else (tree ctype, tree cur_type, enum overload_flags flags)
8d08fdba
MS
5373{
5374 if (ctype && ctype != cur_type)
5375 {
4dacf5bd 5376 if (flags == DTOR_FLAG)
33bd39a2 5377 error ("destructor for alien class `%T' cannot be a member",
2ae7bada 5378 ctype);
4dacf5bd 5379 else
33bd39a2 5380 error ("constructor for alien class `%T' cannot be a member",
2ae7bada 5381 ctype);
8d08fdba
MS
5382 return 0;
5383 }
5384 return 1;
5385}
5386\f
5387/* Subroutine of `grokdeclarator'. */
5388
5389/* Generate errors possibly applicable for a given set of specifiers.
5390 This is for ARM $7.1.2. */
e92cc029 5391
8d08fdba 5392static void
11f6b451
NN
5393bad_specifiers (tree object,
5394 const char* type,
5395 int virtualp,
5396 int quals,
45225759 5397 int inlinep,
11f6b451 5398 int friendp,
45225759 5399 int raises)
8d08fdba
MS
5400{
5401 if (virtualp)
33bd39a2 5402 error ("`%D' declared as a `virtual' %s", object, type);
8d08fdba 5403 if (inlinep)
33bd39a2 5404 error ("`%D' declared as an `inline' %s", object, type);
8d08fdba 5405 if (quals)
33bd39a2 5406 error ("`const' and `volatile' function specifiers on `%D' invalid in %s declaration",
a28e3c7f 5407 object, type);
8d08fdba 5408 if (friendp)
f8e55f34 5409 cp_error_at ("`%D' declared as a friend", object);
e1be26f4
RS
5410 if (raises
5411 && (TREE_CODE (object) == TYPE_DECL
5412 || (!TYPE_PTRFN_P (TREE_TYPE (object))
742a37d5 5413 && !TYPE_REFFN_P (TREE_TYPE (object))
e1be26f4 5414 && !TYPE_PTRMEMFUNC_P (TREE_TYPE (object)))))
f8e55f34 5415 cp_error_at ("`%D' declared with an exception specification", object);
8d08fdba
MS
5416}
5417
5418/* CTYPE is class type, or null if non-class.
5419 TYPE is type this FUNCTION_DECL should have, either FUNCTION_TYPE
5420 or METHOD_TYPE.
5421 DECLARATOR is the function's name.
5422 VIRTUALP is truthvalue of whether the function is virtual or not.
5423 FLAGS are to be passed through to `grokclassfn'.
5424 QUALS are qualifiers indicating whether the function is `const'
5425 or `volatile'.
5426 RAISES is a list of exceptions that this function can raise.
5427 CHECK is 1 if we must find this method in CTYPE, 0 if we should
68642fb6 5428 not look, and -1 if we should not call `grokclassfn' at all.
3ddfb0e6 5429
20496fa2 5430 Returns `NULL_TREE' if something goes wrong, after issuing
3ddfb0e6 5431 applicable error messages. */
e92cc029 5432
8d08fdba 5433static tree
11f6b451
NN
5434grokfndecl (tree ctype,
5435 tree type,
5436 tree declarator,
5437 tree orig_declarator,
5438 int virtualp,
5439 enum overload_flags flags,
5440 tree quals,
5441 tree raises,
5442 int check,
5443 int friendp,
5444 int publicp,
5445 int inlinep,
5446 int funcdef_flag,
5447 int template_count,
5448 tree in_namespace)
8d08fdba 5449{
1951a1b6 5450 tree decl;
8d08fdba 5451 int staticp = ctype && TREE_CODE (type) == FUNCTION_TYPE;
856216bb 5452 int has_default_arg = 0;
42976354 5453 tree t;
8d08fdba 5454
8d08fdba 5455 if (raises)
271e6f02 5456 type = build_exception_variant (type, raises);
c11b6f21 5457
8d08fdba 5458 decl = build_lang_decl (FUNCTION_DECL, declarator, type);
a1c65f9f 5459 /* Propagate volatile out from type to decl. */
8d08fdba 5460 if (TYPE_VOLATILE (type))
893de33c 5461 TREE_THIS_VOLATILE (decl) = 1;
8d08fdba 5462
79c4d4b7 5463 /* If this decl has namespace scope, set that up. */
2c73f9f5 5464 if (in_namespace)
b262d64c 5465 set_decl_namespace (decl, in_namespace, friendp);
adae082f 5466 else if (!ctype)
79c4d4b7 5467 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
2c73f9f5 5468
0f8766b8
JM
5469 /* `main' and builtins have implicit 'C' linkage. */
5470 if ((MAIN_NAME_P (declarator)
5471 || (IDENTIFIER_LENGTH (declarator) > 10
5472 && IDENTIFIER_POINTER (declarator)[0] == '_'
5473 && IDENTIFIER_POINTER (declarator)[1] == '_'
5474 && strncmp (IDENTIFIER_POINTER (declarator)+2, "builtin_", 8) == 0))
5475 && current_lang_name == lang_name_cplusplus
94706a5c 5476 && ctype == NULL_TREE
79c4d4b7
JM
5477 /* NULL_TREE means global namespace. */
5478 && DECL_CONTEXT (decl) == NULL_TREE)
5d2ed28c 5479 SET_DECL_LANGUAGE (decl, lang_c);
0f8766b8 5480
8d08fdba
MS
5481 /* Should probably propagate const out from type to decl I bet (mrs). */
5482 if (staticp)
5483 {
5484 DECL_STATIC_FUNCTION_P (decl) = 1;
5485 DECL_CONTEXT (decl) = ctype;
8d08fdba
MS
5486 }
5487
e76a2646 5488 if (ctype)
4f1c5b7d 5489 DECL_CONTEXT (decl) = ctype;
e76a2646 5490
0f8766b8 5491 if (ctype == NULL_TREE && DECL_MAIN_P (decl))
faae18ab 5492 {
848b92e1 5493 if (processing_template_decl)
cb9a3ff8 5494 error ("cannot declare `::main' to be a template");
faae18ab 5495 if (inlinep)
cb9a3ff8 5496 error ("cannot declare `::main' to be inline");
f22967f3 5497 if (!publicp)
cb9a3ff8 5498 error ("cannot declare `::main' to be static");
f22967f3
MM
5499 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
5500 integer_type_node))
5501 error ("`main' must return `int'");
faae18ab
MS
5502 inlinep = 0;
5503 publicp = 1;
5504 }
50a6dbd7 5505
59e76fc6
JM
5506 /* Members of anonymous types and local classes have no linkage; make
5507 them internal. */
1951a1b6
JM
5508 /* FIXME what if it gets a name from typedef? */
5509 if (ctype && (TYPE_ANONYMOUS_P (ctype)
4f1c5b7d 5510 || decl_function_context (TYPE_MAIN_DECL (ctype))))
50a6dbd7
JM
5511 publicp = 0;
5512
5513 if (publicp)
5514 {
5515 /* [basic.link]: A name with no linkage (notably, the name of a class
5516 or enumeration declared in a local scope) shall not be used to
5517 declare an entity with linkage.
5518
50b97e0f 5519 Only check this for public decls for now. See core 319, 389. */
50a6dbd7
JM
5520 t = no_linkage_check (TREE_TYPE (decl));
5521 if (t)
5522 {
1951a1b6 5523 if (TYPE_ANONYMOUS_P (t))
7f7c930e 5524 {
eb68cb58 5525 if (DECL_EXTERN_C_P (decl))
7f7c930e
JM
5526 /* Allow this; it's pretty common in C. */;
5527 else
1951a1b6 5528 {
33bd39a2 5529 pedwarn ("non-local function `%#D' uses anonymous type",
1951a1b6
JM
5530 decl);
5531 if (DECL_ORIGINAL_TYPE (TYPE_NAME (t)))
5532 cp_pedwarn_at ("\
5533`%#D' does not refer to the unqualified type, so it is not used for linkage",
5534 TYPE_NAME (t));
5535 }
7f7c930e 5536 }
50a6dbd7 5537 else
33bd39a2 5538 pedwarn ("non-local function `%#D' uses local type `%T'",
cce2be43 5539 decl, t);
50a6dbd7
JM
5540 }
5541 }
5542
893de33c 5543 TREE_PUBLIC (decl) = publicp;
faae18ab 5544 if (! publicp)
893de33c
JM
5545 {
5546 DECL_INTERFACE_KNOWN (decl) = 1;
5547 DECL_NOT_REALLY_EXTERN (decl) = 1;
5548 }
faae18ab 5549
acc72c37 5550 /* If the declaration was declared inline, mark it as such. */
faae18ab 5551 if (inlinep)
acc72c37
MM
5552 DECL_DECLARED_INLINE_P (decl) = 1;
5553 /* We inline functions that are explicitly declared inline, or, when
5554 the user explicitly asks us to, all functions. */
b3c3af2f
SB
5555 if (DECL_DECLARED_INLINE_P (decl)
5556 || (flag_inline_trees == 2 && !DECL_INLINE (decl) && funcdef_flag))
acc72c37 5557 DECL_INLINE (decl) = 1;
8d08fdba
MS
5558
5559 DECL_EXTERNAL (decl) = 1;
5560 if (quals != NULL_TREE && TREE_CODE (type) == FUNCTION_TYPE)
5561 {
33bd39a2 5562 error ("%smember function `%D' cannot have `%T' method qualifier",
8d08fdba
MS
5563 (ctype ? "static " : "non-"), decl, TREE_VALUE (quals));
5564 quals = NULL_TREE;
5565 }
5566
596ea4e5 5567 if (IDENTIFIER_OPNAME_P (DECL_NAME (decl)))
4b0d3cbe 5568 grok_op_properties (decl, friendp, /*complain=*/true);
8d08fdba 5569
4f1c5b7d 5570 if (ctype && decl_function_context (decl))
893de33c 5571 DECL_NO_STATIC_CHAIN (decl) = 1;
e76a2646 5572
42976354
BK
5573 for (t = TYPE_ARG_TYPES (TREE_TYPE (decl)); t; t = TREE_CHAIN (t))
5574 if (TREE_PURPOSE (t)
5575 && TREE_CODE (TREE_PURPOSE (t)) == DEFAULT_ARG)
5576 {
856216bb 5577 has_default_arg = 1;
42976354
BK
5578 break;
5579 }
5580
f9d94ea4
JM
5581 if (friendp
5582 && TREE_CODE (orig_declarator) == TEMPLATE_ID_EXPR)
5583 {
5584 if (funcdef_flag)
33bd39a2 5585 error
8251199e 5586 ("defining explicit specialization `%D' in friend declaration",
f9d94ea4
JM
5587 orig_declarator);
5588 else
5589 {
76e57b45
NS
5590 tree fns = TREE_OPERAND (orig_declarator, 0);
5591 tree args = TREE_OPERAND (orig_declarator, 1);
d363e7bf 5592
7e2421f7
MM
5593 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
5594 {
5595 /* Something like `template <class T> friend void f<T>()'. */
33bd39a2 5596 error ("invalid use of template-id `%D' in declaration of primary template",
7e2421f7 5597 orig_declarator);
20496fa2 5598 return NULL_TREE;
7e2421f7
MM
5599 }
5600
856216bb 5601
f9d94ea4
JM
5602 /* A friend declaration of the form friend void f<>(). Record
5603 the information in the TEMPLATE_ID_EXPR. */
5604 SET_DECL_IMPLICIT_INSTANTIATION (decl);
76e57b45
NS
5605
5606 if (TREE_CODE (fns) == COMPONENT_REF)
5607 {
5608 /* Due to bison parser ickiness, we will have already looked
5609 up an operator_name or PFUNCNAME within the current class
5610 (see template_id in parse.y). If the current class contains
a1c65f9f 5611 such a name, we'll get a COMPONENT_REF here. Undo that. */
d363e7bf 5612
76e57b45
NS
5613 my_friendly_assert (TREE_TYPE (TREE_OPERAND (fns, 0))
5614 == current_class_type, 20001120);
5615 fns = TREE_OPERAND (fns, 1);
5616 }
5617 my_friendly_assert (TREE_CODE (fns) == IDENTIFIER_NODE
76e57b45
NS
5618 || TREE_CODE (fns) == OVERLOAD, 20001120);
5619 DECL_TEMPLATE_INFO (decl) = tree_cons (fns, args, NULL_TREE);
856216bb
MM
5620
5621 if (has_default_arg)
5622 {
33bd39a2 5623 error ("default arguments are not allowed in declaration of friend template specialization `%D'",
856216bb
MM
5624 decl);
5625 return NULL_TREE;
5626 }
5627
5628 if (inlinep)
5629 {
33bd39a2 5630 error ("`inline' is not allowed in declaration of friend template specialization `%D'",
856216bb
MM
5631 decl);
5632 return NULL_TREE;
5633 }
f9d94ea4 5634 }
f84b4be9 5635 }
386b8a85 5636
1eb0072d
JM
5637 if (funcdef_flag)
5638 /* Make the init_value nonzero so pushdecl knows this is not
5639 tentative. error_mark_node is replaced later with the BLOCK. */
5640 DECL_INITIAL (decl) = error_mark_node;
5641
93ca4ba7 5642 if (TYPE_NOTHROW_P (type) || nothrow_libfn_p (decl))
1660cb3a
JM
5643 TREE_NOTHROW (decl) = 1;
5644
75650646 5645 /* Caller will do the rest of this. */
8d08fdba
MS
5646 if (check < 0)
5647 return decl;
5648
a723baf1 5649 if (flags == NO_SPECIAL && ctype && constructor_name_p (declarator, ctype))
74b846e0
MM
5650 DECL_CONSTRUCTOR_P (decl) = 1;
5651
5652 /* Function gets the ugly name, field gets the nice one. This call
5653 may change the type of the function (because of default
5654 parameters)! */
5655 if (ctype != NULL_TREE)
5656 grokclassfn (ctype, decl, flags, quals);
5657
5658 decl = check_explicit_specialization (orig_declarator, decl,
5659 template_count,
5660 2 * (funcdef_flag != 0) +
5661 4 * (friendp != 0));
5662 if (decl == error_mark_node)
5663 return NULL_TREE;
98c1c668 5664
74b846e0
MM
5665 if (ctype != NULL_TREE
5666 && (! TYPE_FOR_JAVA (ctype) || check_java_method (decl))
5667 && check)
8d08fdba 5668 {
74b846e0 5669 tree old_decl;
8d08fdba 5670
74b846e0 5671 old_decl = check_classfn (ctype, decl);
8d08fdba 5672
74b846e0
MM
5673 if (old_decl && TREE_CODE (old_decl) == TEMPLATE_DECL)
5674 /* Because grokfndecl is always supposed to return a
5675 FUNCTION_DECL, we pull out the DECL_TEMPLATE_RESULT
5676 here. We depend on our callers to figure out that its
5677 really a template that's being returned. */
5678 old_decl = DECL_TEMPLATE_RESULT (old_decl);
6c30752f 5679
74b846e0
MM
5680 if (old_decl && DECL_STATIC_FUNCTION_P (old_decl)
5681 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE)
5566b478 5682 {
74b846e0
MM
5683 /* Remove the `this' parm added by grokclassfn.
5684 XXX Isn't this done in start_function, too? */
3afb32a4 5685 revert_static_member_fn (decl);
74b846e0 5686 last_function_parms = TREE_CHAIN (last_function_parms);
5566b478 5687 }
74b846e0 5688 if (old_decl && DECL_ARTIFICIAL (old_decl))
33bd39a2 5689 error ("definition of implicitly-declared `%D'", old_decl);
8d08fdba 5690
74b846e0 5691 if (old_decl)
8d08fdba 5692 {
316a2456
MM
5693 bool ok;
5694
74b846e0
MM
5695 /* Since we've smashed OLD_DECL to its
5696 DECL_TEMPLATE_RESULT, we must do the same to DECL. */
5697 if (TREE_CODE (decl) == TEMPLATE_DECL)
5698 decl = DECL_TEMPLATE_RESULT (decl);
5699
5700 /* Attempt to merge the declarations. This can fail, in
0e339752 5701 the case of some invalid specialization declarations. */
14d22dd6 5702 push_scope (ctype);
316a2456 5703 ok = duplicate_decls (decl, old_decl);
14d22dd6 5704 pop_scope (ctype);
316a2456
MM
5705 if (!ok)
5706 {
5707 error ("no `%#D' member function declared in class `%T'",
5708 decl, ctype);
5709 return NULL_TREE;
5710 }
74b846e0 5711 return old_decl;
8d08fdba
MS
5712 }
5713 }
74b846e0
MM
5714
5715 if (DECL_CONSTRUCTOR_P (decl) && !grok_ctor_properties (ctype, decl))
5716 return NULL_TREE;
5717
5718 if (ctype == NULL_TREE || check)
5719 return decl;
5720
5721 if (virtualp)
cbb40945 5722 DECL_VIRTUAL_P (decl) = 1;
74b846e0 5723
8d08fdba
MS
5724 return decl;
5725}
5726
9e259dd1
MM
5727/* Create a VAR_DECL named NAME with the indicated TYPE.
5728
5729 If SCOPE is non-NULL, it is the class type or namespace containing
5730 the variable. If SCOPE is NULL, the variable should is created in
5731 the innermost enclosings scope. */
5732
8d08fdba 5733static tree
11f6b451
NN
5734grokvardecl (tree type,
5735 tree name,
5736 RID_BIT_TYPE * specbits_in,
5737 int initialized,
5738 int constp,
5739 tree scope)
8d08fdba
MS
5740{
5741 tree decl;
f7da6097
MS
5742 RID_BIT_TYPE specbits;
5743
9e259dd1
MM
5744 my_friendly_assert (!name || TREE_CODE (name) == IDENTIFIER_NODE,
5745 20020808);
5746
f7da6097 5747 specbits = *specbits_in;
8d08fdba 5748
9e259dd1
MM
5749 /* Compute the scope in which to place the variable. */
5750 if (!scope)
8d08fdba 5751 {
9e259dd1
MM
5752 /* An explicit "extern" specifier indicates a namespace-scope
5753 variable. */
5754 if (RIDBIT_SETP (RID_EXTERN, specbits))
5755 scope = current_namespace;
5756 else if (!at_function_scope_p ())
5757 {
5758 scope = current_scope ();
5759 if (!scope)
5760 scope = current_namespace;
5761 }
5762 }
5763
5764 if (scope
5765 && (/* If the variable is a namespace-scope variable declared in a
5766 template, we need DECL_LANG_SPECIFIC. */
5767 (TREE_CODE (scope) == NAMESPACE_DECL && processing_template_decl)
5768 /* Similarly for namespace-scope variables with language linkage
5769 other than C++. */
5770 || (TREE_CODE (scope) == NAMESPACE_DECL
5771 && current_lang_name != lang_name_cplusplus)
5772 /* Similarly for static data members. */
5773 || TYPE_P (scope)))
5774 decl = build_lang_decl (VAR_DECL, name, type);
8d08fdba 5775 else
9e259dd1 5776 decl = build_decl (VAR_DECL, name, type);
79c4d4b7 5777
9e259dd1
MM
5778 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
5779 set_decl_namespace (decl, scope, 0);
5780 else
5781 DECL_CONTEXT (decl) = scope;
6060a796 5782
9e259dd1
MM
5783 if (name && scope && current_lang_name != lang_name_c)
5784 /* We can't mangle lazily here because we don't have any
5785 way to recover whether or not a variable was `extern
5786 "C"' later. */
5787 mangle_decl (decl);
2c73f9f5 5788
8d08fdba
MS
5789 if (RIDBIT_SETP (RID_EXTERN, specbits))
5790 {
5791 DECL_THIS_EXTERN (decl) = 1;
5792 DECL_EXTERNAL (decl) = !initialized;
5793 }
5794
5795 /* In class context, static means one per class,
5796 public access, and static storage. */
2b9dc906 5797 if (DECL_CLASS_SCOPE_P (decl))
8d08fdba
MS
5798 {
5799 TREE_PUBLIC (decl) = 1;
5800 TREE_STATIC (decl) = 1;
5b605f68 5801 DECL_EXTERNAL (decl) = 0;
8d08fdba
MS
5802 }
5803 /* At top level, either `static' or no s.c. makes a definition
5804 (perhaps tentative), and absence of `static' makes it public. */
a9aedbc2 5805 else if (toplevel_bindings_p ())
8d08fdba 5806 {
a9aedbc2 5807 TREE_PUBLIC (decl) = (RIDBIT_NOTSETP (RID_STATIC, specbits)
9fffd093 5808 && (DECL_THIS_EXTERN (decl) || ! constp));
8d08fdba
MS
5809 TREE_STATIC (decl) = ! DECL_EXTERNAL (decl);
5810 }
5811 /* Not at top level, only `static' makes a static definition. */
5812 else
5813 {
5814 TREE_STATIC (decl) = !! RIDBIT_SETP (RID_STATIC, specbits);
5815 TREE_PUBLIC (decl) = DECL_EXTERNAL (decl);
5816 }
50a6dbd7 5817
7a1f3f5f
RH
5818 if (RIDBIT_SETP (RID_THREAD, specbits))
5819 {
5820 if (targetm.have_tls)
5821 DECL_THREAD_LOCAL (decl) = 1;
5822 else
5823 /* A mere warning is sure to result in improper semantics
5824 at runtime. Don't bother to allow this to compile. */
5825 error ("thread-local storage not supported for this target");
5826 }
5827
50a6dbd7
JM
5828 if (TREE_PUBLIC (decl))
5829 {
5830 /* [basic.link]: A name with no linkage (notably, the name of a class
5831 or enumeration declared in a local scope) shall not be used to
5832 declare an entity with linkage.
5833
5834 Only check this for public decls for now. */
5835 tree t = no_linkage_check (TREE_TYPE (decl));
5836 if (t)
5837 {
1951a1b6 5838 if (TYPE_ANONYMOUS_P (t))
50a6dbd7
JM
5839 /* Ignore for now; `enum { foo } e' is pretty common. */;
5840 else
33bd39a2 5841 pedwarn ("non-local variable `%#D' uses local type `%T'",
50a6dbd7
JM
5842 decl, t);
5843 }
5844 }
5845
8d08fdba
MS
5846 return decl;
5847}
5848
d8f8dca1
MM
5849/* Create and return a canonical pointer to member function type, for
5850 TYPE, which is a POINTER_TYPE to a METHOD_TYPE. */
8d08fdba
MS
5851
5852tree
11f6b451 5853build_ptrmemfunc_type (tree type)
8d08fdba 5854{
4977bab6 5855 tree field, fields;
8d08fdba 5856 tree t;
46cbda4a 5857 tree unqualified_variant = NULL_TREE;
8d08fdba 5858
d48ebde1
NS
5859 if (type == error_mark_node)
5860 return type;
d363e7bf 5861
8d08fdba
MS
5862 /* If a canonical type already exists for this type, use it. We use
5863 this method instead of type_hash_canon, because it only does a
5864 simple equality check on the list of field members. */
5865
5866 if ((t = TYPE_GET_PTRMEMFUNC_TYPE (type)))
5867 return t;
5868
46cbda4a
MM
5869 /* Make sure that we always have the unqualified pointer-to-member
5870 type first. */
89d684bb 5871 if (cp_type_quals (type) != TYPE_UNQUALIFIED)
68642fb6 5872 unqualified_variant
46cbda4a
MM
5873 = build_ptrmemfunc_type (TYPE_MAIN_VARIANT (type));
5874
33848bb0 5875 t = make_aggr_type (RECORD_TYPE);
2c73f9f5 5876 /* Let the front-end know this is a pointer to member function... */
db5ae43f 5877 TYPE_PTRMEMFUNC_FLAG (t) = 1;
2c73f9f5 5878 /* ... and not really an aggregate. */
7ddedda4 5879 SET_IS_AGGR_TYPE (t, 0);
8d08fdba 5880
4977bab6
ZW
5881 field = build_decl (FIELD_DECL, pfn_identifier, type);
5882 fields = field;
5883
5884 field = build_decl (FIELD_DECL, delta_identifier, delta_type_node);
5885 TREE_CHAIN (field) = fields;
5886 fields = field;
5887
5888 finish_builtin_struct (t, "__ptrmemfunc_type", fields, ptr_type_node);
8d08fdba 5889
8d08fdba
MS
5890 /* Zap out the name so that the back-end will give us the debugging
5891 information for this anonymous RECORD_TYPE. */
5892 TYPE_NAME (t) = NULL_TREE;
5893
46cbda4a
MM
5894 /* If this is not the unqualified form of this pointer-to-member
5895 type, set the TYPE_MAIN_VARIANT for this type to be the
5896 unqualified type. Since they are actually RECORD_TYPEs that are
5897 not variants of each other, we must do this manually. */
89d684bb 5898 if (cp_type_quals (type) != TYPE_UNQUALIFIED)
46cbda4a 5899 {
89d684bb 5900 t = build_qualified_type (t, cp_type_quals (type));
46cbda4a
MM
5901 TYPE_MAIN_VARIANT (t) = unqualified_variant;
5902 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (unqualified_variant);
5903 TYPE_NEXT_VARIANT (unqualified_variant) = t;
5904 }
5905
5906 /* Cache this pointer-to-member type so that we can find it again
5907 later. */
8d08fdba
MS
5908 TYPE_SET_PTRMEMFUNC_TYPE (type, t);
5909
8d08fdba
MS
5910 return t;
5911}
5912
9e259dd1
MM
5913/* Create and return a pointer to data member type. */
5914
5915tree
5916build_ptrmem_type (tree class_type, tree member_type)
5917{
a5ac359a
MM
5918 if (TREE_CODE (member_type) == METHOD_TYPE)
5919 {
5920 tree arg_types;
5921
5922 arg_types = TYPE_ARG_TYPES (member_type);
5923 class_type = (cp_build_qualified_type
5924 (class_type,
5925 cp_type_quals (TREE_TYPE (TREE_VALUE (arg_types)))));
5926 member_type
43dc123f
MM
5927 = build_method_type_directly (class_type,
5928 TREE_TYPE (member_type),
5929 TREE_CHAIN (arg_types));
a5ac359a
MM
5930 return build_ptrmemfunc_type (build_pointer_type (member_type));
5931 }
5932 else
5933 {
5934 my_friendly_assert (TREE_CODE (member_type) != FUNCTION_TYPE,
5935 20030716);
5936 return build_offset_type (class_type, member_type);
5937 }
9e259dd1
MM
5938}
5939
b17e2870
JM
5940/* DECL is a VAR_DECL defined in-class, whose TYPE is also given.
5941 Check to see that the definition is valid. Issue appropriate error
5942 messages. Return 1 if the definition is particularly bad, or 0
5943 otherwise. */
5944
5945int
11f6b451 5946check_static_variable_definition (tree decl, tree type)
b17e2870
JM
5947{
5948 /* Motion 10 at San Diego: If a static const integral data member is
5949 initialized with an integral constant expression, the initializer
5950 may appear either in the declaration (within the class), or in
5951 the definition, but not both. If it appears in the class, the
5952 member is a member constant. The file-scope definition is always
5953 required. */
dcba9b0f 5954 if (!ARITHMETIC_TYPE_P (type) && TREE_CODE (type) != ENUMERAL_TYPE)
b17e2870 5955 {
33bd39a2 5956 error ("invalid in-class initialization of static data member of non-integral type `%T'",
dcba9b0f 5957 type);
b17e2870
JM
5958 /* If we just return the declaration, crashes will sometimes
5959 occur. We therefore return void_type_node, as if this was a
5960 friend declaration, to cause callers to completely ignore
5961 this declaration. */
5962 return 1;
5963 }
5964 else if (!CP_TYPE_CONST_P (type))
33bd39a2 5965 error ("ISO C++ forbids in-class initialization of non-const static member `%D'",
b17e2870
JM
5966 decl);
5967 else if (pedantic && !INTEGRAL_TYPE_P (type))
33bd39a2 5968 pedwarn ("ISO C++ forbids initialization of member constant `%D' of non-integral type `%T'", decl, type);
b17e2870
JM
5969
5970 return 0;
5971}
5972
2848ad0f
MM
5973/* Given the SIZE (i.e., number of elements) in an array, compute an
5974 appropriate index type for the array. If non-NULL, NAME is the
5975 name of the thing being declared. */
5976
c95cd22e 5977tree
11f6b451 5978compute_array_index_type (tree name, tree size)
2848ad0f
MM
5979{
5980 tree itype;
5981
2848ad0f
MM
5982 /* If this involves a template parameter, it will be a constant at
5983 instantiation time, but we don't know what the value is yet.
5984 Even if no template parameters are involved, we may an expression
5985 that is not a constant; we don't even simplify `1 + 2' when
5986 processing a template. */
5987 if (processing_template_decl)
5988 {
5989 /* Resolve a qualified reference to an enumerator or static
5990 const data member of ours. */
5991 if (TREE_CODE (size) == SCOPE_REF
5992 && TREE_OPERAND (size, 0) == current_class_type)
5993 {
5994 tree t = lookup_field (current_class_type,
86ac0575 5995 TREE_OPERAND (size, 1), 0, false);
2848ad0f
MM
5996 if (t)
5997 size = t;
5998 }
5999
6000 return build_index_type (build_min (MINUS_EXPR, sizetype,
6001 size, integer_one_node));
6002 }
6003
a1c65f9f 6004 /* The size might be the result of a cast. */
80f5bb34
MM
6005 STRIP_TYPE_NOPS (size);
6006
6007 /* It might be a const variable or enumeration constant. */
6008 size = decl_constant_value (size);
6009
2848ad0f
MM
6010 /* The array bound must be an integer type. */
6011 if (TREE_CODE (TREE_TYPE (size)) != INTEGER_TYPE
6012 && TREE_CODE (TREE_TYPE (size)) != ENUMERAL_TYPE
6013 && TREE_CODE (TREE_TYPE (size)) != BOOLEAN_TYPE)
6014 {
d67cdbc3 6015 if (name)
33bd39a2 6016 error ("size of array `%D' has non-integer type", name);
d67cdbc3 6017 else
33bd39a2 6018 error ("size of array has non-integer type");
2848ad0f
MM
6019 size = integer_one_node;
6020 }
6021
6022 /* Normally, the array-bound will be a constant. */
2bb5d995 6023 if (TREE_CODE (size) == INTEGER_CST)
2848ad0f
MM
6024 {
6025 /* Check to see if the array bound overflowed. Make that an
6026 error, no matter how generous we're being. */
6027 int old_flag_pedantic_errors = flag_pedantic_errors;
6028 int old_pedantic = pedantic;
6029 pedantic = flag_pedantic_errors = 1;
6030 constant_expression_warning (size);
6031 pedantic = old_pedantic;
6032 flag_pedantic_errors = old_flag_pedantic_errors;
6033
6034 /* An array must have a positive number of elements. */
6035 if (INT_CST_LT (size, integer_zero_node))
6036 {
d67cdbc3 6037 if (name)
33bd39a2 6038 error ("size of array `%D' is negative", name);
d67cdbc3 6039 else
33bd39a2 6040 error ("size of array is negative");
2848ad0f
MM
6041 size = integer_one_node;
6042 }
6043 /* Except that an extension we allow zero-sized arrays. We
68642fb6 6044 always allow them in system headers because glibc uses
2848ad0f
MM
6045 them. */
6046 else if (integer_zerop (size) && pedantic && !in_system_header)
d67cdbc3
JM
6047 {
6048 if (name)
33bd39a2 6049 pedwarn ("ISO C++ forbids zero-size array `%D'", name);
d67cdbc3 6050 else
33bd39a2 6051 pedwarn ("ISO C++ forbids zero-size array");
d67cdbc3 6052 }
2848ad0f 6053 }
2bb5d995
JM
6054 else if (TREE_CONSTANT (size))
6055 {
6056 /* `(int) &fn' is not a valid array bound. */
6057 if (name)
33bd39a2 6058 error ("size of array `%D' is not an integral constant-expression",
2bb5d995
JM
6059 name);
6060 else
33bd39a2 6061 error ("size of array is not an integral constant-expression");
2bb5d995 6062 }
2848ad0f
MM
6063
6064 /* Compute the index of the largest element in the array. It is
6065 one less than the number of elements in the array. */
6066 itype
ab76ca54
MM
6067 = fold (cp_build_binary_op (MINUS_EXPR,
6068 cp_convert (ssizetype, size),
6069 cp_convert (ssizetype,
6070 integer_one_node)));
68642fb6 6071
2848ad0f
MM
6072 /* Check for variable-sized arrays. We allow such things as an
6073 extension, even though they are not allowed in ANSI/ISO C++. */
6074 if (!TREE_CONSTANT (itype))
6075 {
6076 if (pedantic)
6077 {
6078 if (name)
33bd39a2 6079 pedwarn ("ISO C++ forbids variable-size array `%D'",
2848ad0f
MM
6080 name);
6081 else
33bd39a2 6082 pedwarn ("ISO C++ forbids variable-size array");
2848ad0f
MM
6083 }
6084
6085 /* Create a variable-sized array index type. */
6086 itype = variable_size (itype);
6087 }
6088 /* Make sure that there was no overflow when creating to a signed
6089 index type. (For example, on a 32-bit machine, an array with
6090 size 2^32 - 1 is too big.) */
6091 else if (TREE_OVERFLOW (itype))
6092 {
6093 error ("overflow in array dimension");
6094 TREE_OVERFLOW (itype) = 0;
6095 }
68642fb6 6096
2848ad0f
MM
6097 /* Create and return the appropriate index type. */
6098 return build_index_type (itype);
6099}
6100
a723baf1
MM
6101/* Returns the scope (if any) in which the entity declared by
6102 DECLARATOR will be located. If the entity was declared with an
6103 unqualified name, NULL_TREE is returned. */
6104
6105tree
11f6b451 6106get_scope_of_declarator (tree declarator)
a723baf1
MM
6107{
6108 if (!declarator)
6109 return NULL_TREE;
6110
6111 switch (TREE_CODE (declarator))
6112 {
6113 case CALL_EXPR:
6114 case ARRAY_REF:
6115 case INDIRECT_REF:
6116 case ADDR_EXPR:
6117 /* For any of these, the main declarator is the first operand. */
6118 return get_scope_of_declarator (TREE_OPERAND
6119 (declarator, 0));
6120
6121 case SCOPE_REF:
6122 /* For a pointer-to-member, continue descending. */
6123 if (TREE_CODE (TREE_OPERAND (declarator, 1))
6124 == INDIRECT_REF)
6125 return get_scope_of_declarator (TREE_OPERAND
6126 (declarator, 1));
6127 /* Otherwise, if the declarator-id is a SCOPE_REF, the scope in
6128 which the declaration occurs is the first operand. */
6129 return TREE_OPERAND (declarator, 0);
6130
6131 case TREE_LIST:
6132 /* Attributes to be applied. The declarator is TREE_VALUE. */
6133 return get_scope_of_declarator (TREE_VALUE (declarator));
6134
6135 default:
6136 /* Otherwise, we have a declarator-id which is not a qualified
6137 name; the entity will be declared in the current scope. */
6138 return NULL_TREE;
6139 }
6140}
6141
2848ad0f
MM
6142/* Returns an ARRAY_TYPE for an array with SIZE elements of the
6143 indicated TYPE. If non-NULL, NAME is the NAME of the declaration
6144 with this type. */
6145
6146static tree
11f6b451 6147create_array_type_for_decl (tree name, tree type, tree size)
2848ad0f
MM
6148{
6149 tree itype = NULL_TREE;
6150 const char* error_msg;
6151
6152 /* If things have already gone awry, bail now. */
6153 if (type == error_mark_node || size == error_mark_node)
6154 return error_mark_node;
6155
6156 /* Assume that everything will go OK. */
6157 error_msg = NULL;
6158
6159 /* There are some types which cannot be array elements. */
6160 switch (TREE_CODE (type))
6161 {
6162 case VOID_TYPE:
6163 error_msg = "array of void";
6164 break;
6165
6166 case FUNCTION_TYPE:
6167 error_msg = "array of functions";
6168 break;
6169
6170 case REFERENCE_TYPE:
6171 error_msg = "array of references";
6172 break;
6173
2848ad0f
MM
6174 case METHOD_TYPE:
6175 error_msg = "array of function members";
6176 break;
6177
6178 default:
6179 break;
6180 }
6181
6182 /* If something went wrong, issue an error-message and return. */
6183 if (error_msg)
6184 {
6185 if (name)
33bd39a2 6186 error ("declaration of `%D' as %s", name, error_msg);
2848ad0f 6187 else
33bd39a2 6188 error ("creating %s", error_msg);
2848ad0f
MM
6189
6190 return error_mark_node;
6191 }
6192
6193 /* [dcl.array]
68642fb6 6194
2848ad0f
MM
6195 The constant expressions that specify the bounds of the arrays
6196 can be omitted only for the first member of the sequence. */
6197 if (TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type))
6198 {
b3faacfd 6199 if (name)
33bd39a2 6200 error ("declaration of `%D' as multidimensional array must have bounds for all dimensions except the first",
b3faacfd
KL
6201 name);
6202 else
33bd39a2 6203 error ("multidimensional array must have bounds for all dimensions except the first");
2848ad0f
MM
6204
6205 return error_mark_node;
6206 }
6207
6208 /* Figure out the index type for the array. */
6209 if (size)
6210 itype = compute_array_index_type (name, size);
6211
6212 return build_cplus_array_type (type, itype);
6213}
6214
3dbc07b6
MM
6215/* Check that it's OK to declare a function with the indicated TYPE.
6216 SFK indicates the kind of special function (if any) that this
1f84ec23 6217 function is. OPTYPE is the type given in a conversion operator
3dbc07b6
MM
6218 declaration. Returns the actual return type of the function; that
6219 may be different than TYPE if an error occurs, or for certain
6220 special functions. */
6221
6222static tree
11f6b451
NN
6223check_special_function_return_type (special_function_kind sfk,
6224 tree type,
6225 tree optype)
3dbc07b6
MM
6226{
6227 switch (sfk)
6228 {
6229 case sfk_constructor:
6230 if (type)
33bd39a2 6231 error ("return type specification for constructor invalid");
5362b086 6232
1f84ec23 6233 type = void_type_node;
3dbc07b6
MM
6234 break;
6235
6236 case sfk_destructor:
6237 if (type)
33bd39a2 6238 error ("return type specification for destructor invalid");
3dbc07b6
MM
6239 type = void_type_node;
6240 break;
6241
6242 case sfk_conversion:
6243 if (type && !same_type_p (type, optype))
33bd39a2 6244 error ("operator `%T' declared to return `%T'", optype, type);
3dbc07b6 6245 else if (type)
33bd39a2 6246 pedwarn ("return type specified for `operator %T'", optype);
3dbc07b6
MM
6247 type = optype;
6248 break;
6249
6250 default:
a98facb0 6251 abort ();
3dbc07b6
MM
6252 break;
6253 }
6254
6255 return type;
6256}
6257
a723baf1
MM
6258/* Given declspecs and a declarator (abstract or otherwise), determine
6259 the name and type of the object declared and construct a DECL node
6260 for it.
8d08fdba
MS
6261
6262 DECLSPECS is a chain of tree_list nodes whose value fields
6263 are the storage classes and type specifiers.
6264
6265 DECL_CONTEXT says which syntactic context this declaration is in:
6266 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
6267 FUNCDEF for a function definition. Like NORMAL but a few different
6268 error messages in each case. Return value may be zero meaning
6269 this definition is too screwy to try to parse.
6270 MEMFUNCDEF for a function definition. Like FUNCDEF but prepares to
6271 handle member functions (which have FIELD context).
6272 Return value may be zero meaning this definition is too screwy to
6273 try to parse.
6274 PARM for a parameter declaration (either within a function prototype
6275 or before a function body). Make a PARM_DECL, or return void_type_node.
db5ae43f 6276 CATCHPARM for a parameter declaration before a catch clause.
8d08fdba
MS
6277 TYPENAME if for a typename (in a cast or sizeof).
6278 Don't make a DECL node; just return the ..._TYPE node.
6279 FIELD for a struct or union field; make a FIELD_DECL.
6280 BITFIELD for a field with specified width.
6281 INITIALIZED is 1 if the decl has an initializer.
6282
91d231cb
JM
6283 ATTRLIST is a pointer to the list of attributes, which may be NULL
6284 if there are none; *ATTRLIST may be modified if attributes from inside
6285 the declarator should be applied to the declaration.
b17e2870 6286
a723baf1
MM
6287 When this function is called, scoping variables (such as
6288 CURRENT_CLASS_TYPE) should reflect the scope in which the
6289 declaration occurs, not the scope in which the new declaration will
6290 be placed. For example, on:
8d08fdba 6291
a723baf1 6292 void S::f() { ... }
8d08fdba 6293
a723baf1
MM
6294 when grokdeclarator is called for `S::f', the CURRENT_CLASS_TYPE
6295 should not be `S'. */
8d08fdba 6296
8d08fdba 6297tree
11f6b451
NN
6298grokdeclarator (tree declarator,
6299 tree declspecs,
6300 enum decl_context decl_context,
6301 int initialized,
6302 tree* attrlist)
8d08fdba
MS
6303{
6304 RID_BIT_TYPE specbits;
6305 int nclasses = 0;
6306 tree spec;
6307 tree type = NULL_TREE;
6308 int longlong = 0;
91063b51 6309 int type_quals;
db5ae43f 6310 int virtualp, explicitp, friendp, inlinep, staticp;
8d08fdba
MS
6311 int explicit_int = 0;
6312 int explicit_char = 0;
37c46b43 6313 int defaulted_int = 0;
270d8c65 6314 int extern_langp = 0;
d5614afb 6315 tree dependant_name = NULL_TREE;
270d8c65 6316
8d08fdba 6317 tree typedef_decl = NULL_TREE;
9c0758dd 6318 const char *name;
8d08fdba
MS
6319 tree typedef_type = NULL_TREE;
6320 int funcdef_flag = 0;
6321 enum tree_code innermost_code = ERROR_MARK;
6322 int bitfield = 0;
6125f3be
DE
6323#if 0
6324 /* See the code below that used this. */
91d231cb 6325 tree decl_attr = NULL_TREE;
6125f3be 6326#endif
8d08fdba
MS
6327
6328 /* Keep track of what sort of function is being processed
6329 so that we can warn about default return values, or explicit
6330 return values which do not match prescribed defaults. */
3dbc07b6 6331 special_function_kind sfk = sfk_none;
8d08fdba
MS
6332
6333 tree dname = NULL_TREE;
6334 tree ctype = current_class_type;
6335 tree ctor_return_type = NULL_TREE;
6336 enum overload_flags flags = NO_SPECIAL;
8d08fdba 6337 tree quals = NULL_TREE;
c11b6f21 6338 tree raises = NULL_TREE;
386b8a85 6339 int template_count = 0;
2c73f9f5 6340 tree in_namespace = NULL_TREE;
91d231cb 6341 tree returned_attrs = NULL_TREE;
a723baf1 6342 tree scope = NULL_TREE;
8d08fdba
MS
6343
6344 RIDBIT_RESET_ALL (specbits);
6345 if (decl_context == FUNCDEF)
6346 funcdef_flag = 1, decl_context = NORMAL;
6347 else if (decl_context == MEMFUNCDEF)
6348 funcdef_flag = -1, decl_context = FIELD;
6349 else if (decl_context == BITFIELD)
6350 bitfield = 1, decl_context = FIELD;
6351
8d08fdba
MS
6352 /* Look inside a declarator for the name being declared
6353 and get it as a string, for an error message. */
6354 {
be99da77
MS
6355 tree *next = &declarator;
6356 register tree decl;
8d08fdba
MS
6357 name = NULL;
6358
be99da77
MS
6359 while (next && *next)
6360 {
6361 decl = *next;
6362 switch (TREE_CODE (decl))
8d08fdba 6363 {
52fbc847
JM
6364 case TREE_LIST:
6365 /* For attributes. */
6366 next = &TREE_VALUE (decl);
6367 break;
6368
be99da77
MS
6369 case COND_EXPR:
6370 ctype = NULL_TREE;
6371 next = &TREE_OPERAND (decl, 0);
6372 break;
8d08fdba 6373
2c73f9f5 6374 case BIT_NOT_EXPR: /* For C++ destructors! */
8d08fdba 6375 {
be99da77
MS
6376 tree name = TREE_OPERAND (decl, 0);
6377 tree rename = NULL_TREE;
6378
6379 my_friendly_assert (flags == NO_SPECIAL, 152);
6380 flags = DTOR_FLAG;
3dbc07b6 6381 sfk = sfk_destructor;
a723baf1 6382 if (TYPE_P (name))
5566b478 6383 TREE_OPERAND (decl, 0) = name = constructor_name (name);
be99da77
MS
6384 my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, 153);
6385 if (ctype == NULL_TREE)
6386 {
6387 if (current_class_type == NULL_TREE)
6388 {
8251199e 6389 error ("destructors must be member functions");
be99da77
MS
6390 flags = NO_SPECIAL;
6391 }
6392 else
6393 {
a723baf1 6394 tree t = constructor_name (current_class_type);
be99da77
MS
6395 if (t != name)
6396 rename = t;
6397 }
6398 }
8d08fdba 6399 else
be99da77
MS
6400 {
6401 tree t = constructor_name (ctype);
6402 if (t != name)
6403 rename = t;
6404 }
51c184be 6405
be99da77 6406 if (rename)
39211cd5 6407 {
33bd39a2 6408 error ("destructor `%T' must match class name `%T'",
5566b478 6409 name, rename);
be99da77 6410 TREE_OPERAND (decl, 0) = rename;
39211cd5 6411 }
be99da77 6412 next = &name;
51c184be 6413 }
be99da77 6414 break;
8d08fdba 6415
be99da77 6416 case ADDR_EXPR: /* C++ reference declaration */
a1c65f9f 6417 /* Fall through. */
be99da77
MS
6418 case ARRAY_REF:
6419 case INDIRECT_REF:
6420 ctype = NULL_TREE;
6421 innermost_code = TREE_CODE (decl);
6422 next = &TREE_OPERAND (decl, 0);
6423 break;
8d08fdba 6424
be99da77 6425 case CALL_EXPR:
be99da77
MS
6426 innermost_code = TREE_CODE (decl);
6427 if (decl_context == FIELD && ctype == NULL_TREE)
6428 ctype = current_class_type;
45537677 6429 if (ctype
c11b6f21 6430 && TREE_OPERAND (decl, 0)
45537677 6431 && (TREE_CODE (TREE_OPERAND (decl, 0)) == TYPE_DECL
8ba658ee
MM
6432 && constructor_name_p (DECL_NAME (TREE_OPERAND (decl, 0)),
6433 ctype)))
be99da77
MS
6434 TREE_OPERAND (decl, 0) = constructor_name (ctype);
6435 next = &TREE_OPERAND (decl, 0);
6436 decl = *next;
6437 if (ctype != NULL_TREE
6438 && decl != NULL_TREE && flags != DTOR_FLAG
a723baf1 6439 && constructor_name_p (decl, ctype))
8d08fdba 6440 {
3dbc07b6 6441 sfk = sfk_constructor;
be99da77 6442 ctor_return_type = ctype;
8d08fdba 6443 }
be99da77
MS
6444 ctype = NULL_TREE;
6445 break;
68642fb6 6446
386b8a85
JM
6447 case TEMPLATE_ID_EXPR:
6448 {
6449 tree fns = TREE_OPERAND (decl, 0);
6450
8f032717
MM
6451 dname = fns;
6452 if (TREE_CODE (dname) == COMPONENT_REF)
6453 dname = TREE_OPERAND (dname, 1);
6454 if (TREE_CODE (dname) != IDENTIFIER_NODE)
6455 {
6456 my_friendly_assert (is_overloaded_fn (dname),
6457 19990331);
6458 dname = DECL_NAME (get_first_fn (dname));
6459 }
386b8a85 6460 }
a1c65f9f 6461 /* Fall through. */
be99da77
MS
6462
6463 case IDENTIFIER_NODE:
386b8a85
JM
6464 if (TREE_CODE (decl) == IDENTIFIER_NODE)
6465 dname = decl;
6466
be99da77
MS
6467 next = 0;
6468
0e5921e8 6469 if (C_IS_RESERVED_WORD (dname))
8d08fdba 6470 {
33bd39a2 6471 error ("declarator-id missing; using reserved word `%D'",
be99da77
MS
6472 dname);
6473 name = IDENTIFIER_POINTER (dname);
8d08fdba 6474 }
596ea4e5 6475 else if (!IDENTIFIER_TYPENAME_P (dname))
be99da77 6476 name = IDENTIFIER_POINTER (dname);
8d08fdba
MS
6477 else
6478 {
596ea4e5
AS
6479 my_friendly_assert (flags == NO_SPECIAL, 154);
6480 flags = TYPENAME_FLAG;
6481 ctor_return_type = TREE_TYPE (dname);
6482 sfk = sfk_conversion;
c003e212 6483 if (is_typename_at_global_scope (dname))
596ea4e5
AS
6484 name = IDENTIFIER_POINTER (dname);
6485 else
6486 name = "<invalid operator>";
8d08fdba 6487 }
be99da77 6488 break;
8d08fdba 6489
be99da77
MS
6490 /* C++ extension */
6491 case SCOPE_REF:
6492 {
6493 /* Perform error checking, and decide on a ctype. */
6494 tree cname = TREE_OPERAND (decl, 0);
6495 if (cname == NULL_TREE)
6496 ctype = NULL_TREE;
2c73f9f5
ML
6497 else if (TREE_CODE (cname) == NAMESPACE_DECL)
6498 {
6499 ctype = NULL_TREE;
6500 in_namespace = TREE_OPERAND (decl, 0);
2c73f9f5 6501 }
be99da77 6502 else if (! is_aggr_type (cname, 1))
a723baf1 6503 ctype = NULL_TREE;
be99da77
MS
6504 /* Must test TREE_OPERAND (decl, 1), in case user gives
6505 us `typedef (class::memfunc)(int); memfunc *memfuncptr;' */
6506 else if (TREE_OPERAND (decl, 1)
6507 && TREE_CODE (TREE_OPERAND (decl, 1)) == INDIRECT_REF)
6508 ctype = cname;
73b0fce8 6509 else if (TREE_CODE (cname) == TEMPLATE_TYPE_PARM
a1281f45 6510 || TREE_CODE (cname) == BOUND_TEMPLATE_TEMPLATE_PARM)
5566b478 6511 {
d5614afb
NS
6512 /* This might be declaring a member of a template
6513 parm to be a friend. */
6514 ctype = cname;
6515 dependant_name = TREE_OPERAND (decl, 1);
5566b478 6516 }
be99da77
MS
6517 else if (ctype == NULL_TREE)
6518 ctype = cname;
6519 else if (TREE_COMPLEXITY (decl) == current_class_depth)
a723baf1 6520 ;
be99da77
MS
6521 else
6522 {
6523 if (! UNIQUELY_DERIVED_FROM_P (cname, ctype))
6524 {
33bd39a2 6525 error ("type `%T' is not derived from type `%T'",
be99da77 6526 cname, ctype);
a723baf1 6527 ctype = NULL_TREE;
be99da77
MS
6528 }
6529 else
6530 ctype = cname;
6531 }
6532
a723baf1
MM
6533 /* It is valid to write:
6534
6535 class C { void f(); };
6536 typedef C D;
6537 void D::f();
6538
6539 The standard is not clear about whether `typedef const C D' is
6540 legal; as of 2002-09-15 the committee is considering
6541 that question. EDG 3.0 allows that syntax.
6542 Therefore, we do as well. */
6543 if (ctype)
6544 ctype = TYPE_MAIN_VARIANT (ctype);
6545 /* Update the declarator so that when we process it
6546 again the correct type is present. */
6547 TREE_OPERAND (decl, 0) = ctype;
6548
c91a56d2 6549 if (ctype && TREE_CODE (TREE_OPERAND (decl, 1)) == TYPE_DECL
8ba658ee
MM
6550 && constructor_name_p (DECL_NAME (TREE_OPERAND (decl, 1)),
6551 ctype))
be99da77
MS
6552 TREE_OPERAND (decl, 1) = constructor_name (ctype);
6553 next = &TREE_OPERAND (decl, 1);
6554 decl = *next;
6555 if (ctype)
6556 {
633221db
NS
6557 tree name = decl;
6558
6559 if (TREE_CODE (name) == BIT_NOT_EXPR)
6560 name = TREE_OPERAND (name, 0);
6561
6562 if (!constructor_name_p (decl, ctype))
6563 ;
6564 else if (decl == name)
be99da77 6565 {
3dbc07b6 6566 sfk = sfk_constructor;
be99da77
MS
6567 ctor_return_type = ctype;
6568 }
633221db 6569 else
be99da77 6570 {
3dbc07b6 6571 sfk = sfk_destructor;
be99da77
MS
6572 ctor_return_type = ctype;
6573 flags = DTOR_FLAG;
6574 TREE_OPERAND (decl, 0) = constructor_name (ctype);
6575 next = &TREE_OPERAND (decl, 0);
6576 }
6577 }
6578 }
6579 break;
6580
6581 case ERROR_MARK:
6582 next = 0;
6583 break;
6584
45537677
MS
6585 case TYPE_DECL:
6586 /* Parse error puts this typespec where
6587 a declarator should go. */
33bd39a2 6588 error ("`%T' specified as declarator-id", DECL_NAME (decl));
45537677 6589 if (TREE_TYPE (decl) == current_class_type)
33bd39a2 6590 error (" perhaps you want `%T' for a constructor",
45537677
MS
6591 current_class_name);
6592 dname = DECL_NAME (decl);
6593 name = IDENTIFIER_POINTER (dname);
6594
e92cc029 6595 /* Avoid giving two errors for this. */
45537677
MS
6596 IDENTIFIER_CLASS_VALUE (dname) = NULL_TREE;
6597
58010b57 6598 declspecs = tree_cons (NULL_TREE, integer_type_node, declspecs);
45537677
MS
6599 *next = dname;
6600 next = 0;
6601 break;
6602
50ad9642
MM
6603 case BASELINK:
6604 next = &BASELINK_FUNCTIONS (decl);
6605 break;
2ee366b5
GDR
6606
6607 case TEMPLATE_DECL:
6608 /* Sometimes, we see a template-name used as part of a
6609 decl-specifier like in
6610 std::allocator alloc;
6611 Handle that gracefully. */
6612 error ("invalid use of template-name '%E' in a declarator", decl);
6613 return error_mark_node;
6614 break;
50ad9642 6615
be99da77 6616 default:
dd29188b 6617 my_friendly_assert (0, 20020917);
8d08fdba 6618 }
be99da77 6619 }
8d08fdba
MS
6620 }
6621
6622 /* A function definition's declarator must have the form of
6623 a function declarator. */
6624
6625 if (funcdef_flag && innermost_code != CALL_EXPR)
6626 return 0;
6627
e1cd6e56
MS
6628 if (((dname && IDENTIFIER_OPNAME_P (dname)) || flags == TYPENAME_FLAG)
6629 && innermost_code != CALL_EXPR
6630 && ! (ctype && declspecs == NULL_TREE))
6631 {
33bd39a2 6632 error ("declaration of `%D' as non-function", dname);
e1cd6e56
MS
6633 return void_type_node;
6634 }
6635
8d08fdba
MS
6636 /* Anything declared one level down from the top level
6637 must be one of the parameters of a function
6638 (because the body is at least two levels down). */
6639
6640 /* This heuristic cannot be applied to C++ nodes! Fixed, however,
6641 by not allowing C++ class definitions to specify their parameters
6642 with xdecls (must be spec.d in the parmlist).
6643
6644 Since we now wait to push a class scope until we are sure that
6645 we are in a legitimate method context, we must set oldcname
a9aedbc2
MS
6646 explicitly (since current_class_name is not yet alive).
6647
6648 We also want to avoid calling this a PARM if it is in a namespace. */
8d08fdba 6649
9188c363 6650 if (decl_context == NORMAL && !toplevel_bindings_p ())
a9aedbc2 6651 {
e2500fed 6652 struct cp_binding_level *b = current_binding_level;
ff955512 6653 current_binding_level = b->level_chain;
a9aedbc2
MS
6654 if (current_binding_level != 0 && toplevel_bindings_p ())
6655 decl_context = PARM;
ff955512 6656 current_binding_level = b;
a9aedbc2 6657 }
8d08fdba 6658
34fc957d
NS
6659 if (name == NULL)
6660 name = decl_context == PARM ? "parameter" : "type name";
68642fb6 6661
8d08fdba
MS
6662 /* Look through the decl specs and record which ones appear.
6663 Some typespecs are defined as built-in typenames.
6664 Others, the ones that are modifiers of other types,
6665 are represented by bits in SPECBITS: set the bits for
6666 the modifiers that appear. Storage class keywords are also in SPECBITS.
6667
6668 If there is a typedef name or a type, store the type in TYPE.
6669 This includes builtin typedefs such as `int'.
6670
6671 Set EXPLICIT_INT if the type is `int' or `char' and did not
6672 come from a user typedef.
6673
6674 Set LONGLONG if `long' is mentioned twice.
6675
6676 For C++, constructors and destructors have their own fast treatment. */
6677
6678 for (spec = declspecs; spec; spec = TREE_CHAIN (spec))
6679 {
6680 register int i;
6681 register tree id;
6682
6683 /* Certain parse errors slip through. For example,
6684 `int class;' is not caught by the parser. Try
6685 weakly to recover here. */
6686 if (TREE_CODE (spec) != TREE_LIST)
6687 return 0;
6688
6689 id = TREE_VALUE (spec);
6690
e23bd218
IR
6691 /* If the entire declaration is itself tagged as deprecated then
6692 suppress reports of deprecated items. */
6693 if (!adding_implicit_members && id && TREE_DEPRECATED (id))
6694 {
6695 if (deprecated_state != DEPRECATED_SUPPRESS)
6696 warn_deprecated_use (id);
6697 }
6698
8d08fdba
MS
6699 if (TREE_CODE (id) == IDENTIFIER_NODE)
6700 {
a3203465
MS
6701 if (id == ridpointers[(int) RID_INT]
6702 || id == ridpointers[(int) RID_CHAR]
6703 || id == ridpointers[(int) RID_BOOL]
6704 || id == ridpointers[(int) RID_WCHAR])
8d08fdba
MS
6705 {
6706 if (type)
8ccc31eb
MS
6707 {
6708 if (id == ridpointers[(int) RID_BOOL])
8251199e 6709 error ("`bool' is now a keyword");
8ccc31eb 6710 else
33bd39a2 6711 error ("extraneous `%T' ignored", id);
8ccc31eb 6712 }
8d08fdba
MS
6713 else
6714 {
a3203465
MS
6715 if (id == ridpointers[(int) RID_INT])
6716 explicit_int = 1;
6717 else if (id == ridpointers[(int) RID_CHAR])
6718 explicit_char = 1;
8d08fdba
MS
6719 type = TREE_TYPE (IDENTIFIER_GLOBAL_VALUE (id));
6720 }
6721 goto found;
6722 }
e92cc029 6723 /* C++ aggregate types. */
8d08fdba
MS
6724 if (IDENTIFIER_HAS_TYPE_VALUE (id))
6725 {
6726 if (type)
33bd39a2 6727 error ("multiple declarations `%T' and `%T'", type, id);
8d08fdba
MS
6728 else
6729 type = IDENTIFIER_TYPE_VALUE (id);
6730 goto found;
6731 }
6732
f376e137 6733 for (i = (int) RID_FIRST_MODIFIER; i <= (int) RID_LAST_MODIFIER; i++)
8d08fdba
MS
6734 {
6735 if (ridpointers[i] == id)
6736 {
6737 if (i == (int) RID_LONG && RIDBIT_SETP (i, specbits))
6738 {
795add94 6739 if (pedantic && ! in_system_header && warn_long_long)
cb9a3ff8 6740 pedwarn ("ISO C++ does not support `long long'");
9a3b49ac 6741 if (longlong)
8251199e 6742 error ("`long long long' is too long for GCC");
8d08fdba
MS
6743 else
6744 longlong = 1;
6745 }
6746 else if (RIDBIT_SETP (i, specbits))
8251199e 6747 pedwarn ("duplicate `%s'", IDENTIFIER_POINTER (id));
7a1f3f5f 6748
946d5e45
MM
6749 /* Diagnose "__thread extern" or "__thread static". */
6750 if (RIDBIT_SETP (RID_THREAD, specbits))
7a1f3f5f 6751 {
946d5e45 6752 if (i == (int)RID_EXTERN)
7a1f3f5f 6753 error ("`__thread' before `extern'");
946d5e45 6754 else if (i == (int)RID_STATIC)
7a1f3f5f
RH
6755 error ("`__thread' before `static'");
6756 }
6757
270d8c65
NS
6758 if (i == (int)RID_EXTERN
6759 && TREE_PURPOSE (spec) == error_mark_node)
6760 /* This extern was part of a language linkage. */
6761 extern_langp = 1;
7a1f3f5f 6762
8d08fdba
MS
6763 RIDBIT_SET (i, specbits);
6764 goto found;
6765 }
6766 }
6767 }
1899c3a4 6768 else if (TREE_CODE (id) == TYPE_DECL)
45537677
MS
6769 {
6770 if (type)
33bd39a2 6771 error ("multiple declarations `%T' and `%T'", type,
45537677
MS
6772 TREE_TYPE (id));
6773 else
5566b478
MS
6774 {
6775 type = TREE_TYPE (id);
6776 TREE_VALUE (spec) = type;
8d6e459d 6777 typedef_decl = id;
5566b478 6778 }
45537677
MS
6779 goto found;
6780 }
8d08fdba 6781 if (type)
8251199e 6782 error ("two or more data types in declaration of `%s'", name);
8d08fdba
MS
6783 else if (TREE_CODE (id) == IDENTIFIER_NODE)
6784 {
6785 register tree t = lookup_name (id, 1);
6786 if (!t || TREE_CODE (t) != TYPE_DECL)
8251199e 6787 error ("`%s' fails to be a typedef or built in type",
8d08fdba
MS
6788 IDENTIFIER_POINTER (id));
6789 else
6790 {
6791 type = TREE_TYPE (t);
6792 typedef_decl = t;
6793 }
6794 }
bd6dd845 6795 else if (id != error_mark_node)
8d08fdba
MS
6796 /* Can't change CLASS nodes into RECORD nodes here! */
6797 type = id;
6798
6799 found: ;
6800 }
6801
8d6e459d
NS
6802#if 0
6803 /* See the code below that used this. */
6804 if (typedef_decl)
6805 decl_attr = DECL_ATTRIBUTES (typedef_decl);
6806#endif
8d08fdba
MS
6807 typedef_type = type;
6808
37c46b43 6809 /* No type at all: default to `int', and set DEFAULTED_INT
6eabb241 6810 because it was not a user-defined typedef. */
8d08fdba 6811
a3203465
MS
6812 if (type == NULL_TREE
6813 && (RIDBIT_SETP (RID_SIGNED, specbits)
6814 || RIDBIT_SETP (RID_UNSIGNED, specbits)
6815 || RIDBIT_SETP (RID_LONG, specbits)
6816 || RIDBIT_SETP (RID_SHORT, specbits)))
6817 {
6818 /* These imply 'int'. */
6819 type = integer_type_node;
37c46b43 6820 defaulted_int = 1;
a3203465
MS
6821 }
6822
3dbc07b6
MM
6823 if (sfk != sfk_none)
6824 type = check_special_function_return_type (sfk, type,
3dbc07b6
MM
6825 ctor_return_type);
6826 else if (type == NULL_TREE)
8d08fdba 6827 {
3dbc07b6
MM
6828 int is_main;
6829
8d08fdba 6830 explicit_int = -1;
3dbc07b6 6831
0fd0b7ee
JM
6832 /* We handle `main' specially here, because 'main () { }' is so
6833 common. With no options, it is allowed. With -Wreturn-type,
6834 it is a warning. It is only an error with -pedantic-errors. */
3dbc07b6 6835 is_main = (funcdef_flag
7437519c 6836 && dname && MAIN_NAME_P (dname)
3dbc07b6
MM
6837 && ctype == NULL_TREE
6838 && in_namespace == NULL_TREE
6839 && current_namespace == global_namespace);
6840
6841 if (in_system_header || flag_ms_extensions)
6842 /* Allow it, sigh. */;
6843 else if (pedantic || ! is_main)
33bd39a2 6844 pedwarn ("ISO C++ forbids declaration of `%s' with no type",
3dbc07b6
MM
6845 name);
6846 else if (warn_return_type)
33bd39a2 6847 warning ("ISO C++ forbids declaration of `%s' with no type",
3dbc07b6 6848 name);
41eff652 6849
3dbc07b6 6850 type = integer_type_node;
51c184be 6851 }
ad1a6d45 6852
8d08fdba
MS
6853 ctype = NULL_TREE;
6854
6855 /* Now process the modifiers that were specified
6856 and check for invalid combinations. */
6857
6858 /* Long double is a special combination. */
6859
6860 if (RIDBIT_SETP (RID_LONG, specbits)
6861 && TYPE_MAIN_VARIANT (type) == double_type_node)
6862 {
6863 RIDBIT_RESET (RID_LONG, specbits);
68642fb6 6864 type = build_qualified_type (long_double_type_node,
89d684bb 6865 cp_type_quals (type));
8d08fdba
MS
6866 }
6867
6868 /* Check all other uses of type modifiers. */
6869
6870 if (RIDBIT_SETP (RID_UNSIGNED, specbits)
6871 || RIDBIT_SETP (RID_SIGNED, specbits)
6872 || RIDBIT_SETP (RID_LONG, specbits)
6873 || RIDBIT_SETP (RID_SHORT, specbits))
6874 {
6875 int ok = 0;
6876
6877 if (TREE_CODE (type) == REAL_TYPE)
8251199e 6878 error ("short, signed or unsigned invalid for `%s'", name);
b7484fbe 6879 else if (TREE_CODE (type) != INTEGER_TYPE)
8251199e 6880 error ("long, short, signed or unsigned invalid for `%s'", name);
8d08fdba
MS
6881 else if (RIDBIT_SETP (RID_LONG, specbits)
6882 && RIDBIT_SETP (RID_SHORT, specbits))
8251199e 6883 error ("long and short specified together for `%s'", name);
8d08fdba
MS
6884 else if ((RIDBIT_SETP (RID_LONG, specbits)
6885 || RIDBIT_SETP (RID_SHORT, specbits))
6886 && explicit_char)
8251199e 6887 error ("long or short specified with char for `%s'", name);
8d08fdba
MS
6888 else if ((RIDBIT_SETP (RID_LONG, specbits)
6889 || RIDBIT_SETP (RID_SHORT, specbits))
6890 && TREE_CODE (type) == REAL_TYPE)
8251199e 6891 error ("long or short specified with floating type for `%s'", name);
8d08fdba
MS
6892 else if (RIDBIT_SETP (RID_SIGNED, specbits)
6893 && RIDBIT_SETP (RID_UNSIGNED, specbits))
8251199e 6894 error ("signed and unsigned given together for `%s'", name);
8d08fdba
MS
6895 else
6896 {
6897 ok = 1;
37c46b43 6898 if (!explicit_int && !defaulted_int && !explicit_char && pedantic)
8d08fdba 6899 {
8251199e 6900 pedwarn ("long, short, signed or unsigned used invalidly for `%s'",
8d08fdba
MS
6901 name);
6902 if (flag_pedantic_errors)
6903 ok = 0;
6904 }
6905 }
6906
6907 /* Discard the type modifiers if they are invalid. */
6908 if (! ok)
6909 {
6910 RIDBIT_RESET (RID_UNSIGNED, specbits);
6911 RIDBIT_RESET (RID_SIGNED, specbits);
6912 RIDBIT_RESET (RID_LONG, specbits);
6913 RIDBIT_RESET (RID_SHORT, specbits);
6914 longlong = 0;
6915 }
6916 }
6917
37c46b43
MS
6918 if (RIDBIT_SETP (RID_COMPLEX, specbits)
6919 && TREE_CODE (type) != INTEGER_TYPE && TREE_CODE (type) != REAL_TYPE)
6920 {
8251199e 6921 error ("complex invalid for `%s'", name);
37c46b43
MS
6922 RIDBIT_RESET (RID_COMPLEX, specbits);
6923 }
6924
8d08fdba
MS
6925 /* Decide whether an integer type is signed or not.
6926 Optionally treat bitfields as signed by default. */
6927 if (RIDBIT_SETP (RID_UNSIGNED, specbits)
b89c5a7b
MM
6928 /* [class.bit]
6929
6930 It is implementation-defined whether a plain (neither
6931 explicitly signed or unsigned) char, short, int, or long
6932 bit-field is signed or unsigned.
68642fb6 6933
b89c5a7b
MM
6934 Naturally, we extend this to long long as well. Note that
6935 this does not include wchar_t. */
6936 || (bitfield && !flag_signed_bitfields
6937 && RIDBIT_NOTSETP (RID_SIGNED, specbits)
6938 /* A typedef for plain `int' without `signed' can be
6939 controlled just like plain `int', but a typedef for
6940 `signed int' cannot be so controlled. */
68642fb6 6941 && !(typedef_decl
29bbeb1c 6942 && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl))
b89c5a7b
MM
6943 && (TREE_CODE (type) == INTEGER_TYPE
6944 || TREE_CODE (type) == CHAR_TYPE)
29bbeb1c 6945 && !same_type_p (TYPE_MAIN_VARIANT (type), wchar_type_node)))
8d08fdba
MS
6946 {
6947 if (longlong)
6948 type = long_long_unsigned_type_node;
6949 else if (RIDBIT_SETP (RID_LONG, specbits))
6950 type = long_unsigned_type_node;
6951 else if (RIDBIT_SETP (RID_SHORT, specbits))
6952 type = short_unsigned_type_node;
6953 else if (type == char_type_node)
6954 type = unsigned_char_type_node;
6955 else if (typedef_decl)
ceef8ce4 6956 type = c_common_unsigned_type (type);
8d08fdba
MS
6957 else
6958 type = unsigned_type_node;
6959 }
6960 else if (RIDBIT_SETP (RID_SIGNED, specbits)
6961 && type == char_type_node)
6962 type = signed_char_type_node;
6963 else if (longlong)
6964 type = long_long_integer_type_node;
6965 else if (RIDBIT_SETP (RID_LONG, specbits))
6966 type = long_integer_type_node;
6967 else if (RIDBIT_SETP (RID_SHORT, specbits))
6968 type = short_integer_type_node;
6969
37c46b43
MS
6970 if (RIDBIT_SETP (RID_COMPLEX, specbits))
6971 {
6972 /* If we just have "complex", it is equivalent to
6973 "complex double", but if any modifiers at all are specified it is
6974 the complex form of TYPE. E.g, "complex short" is
6975 "complex short int". */
6976
6977 if (defaulted_int && ! longlong
6978 && ! (RIDBIT_SETP (RID_LONG, specbits)
6979 || RIDBIT_SETP (RID_SHORT, specbits)
6980 || RIDBIT_SETP (RID_SIGNED, specbits)
6981 || RIDBIT_SETP (RID_UNSIGNED, specbits)))
6982 type = complex_double_type_node;
6983 else if (type == integer_type_node)
6984 type = complex_integer_type_node;
6985 else if (type == float_type_node)
6986 type = complex_float_type_node;
6987 else if (type == double_type_node)
6988 type = complex_double_type_node;
6989 else if (type == long_double_type_node)
6990 type = complex_long_double_type_node;
6991 else
6992 type = build_complex_type (type);
6993 }
6994
4f2b0fb2
NS
6995 type_quals = TYPE_UNQUALIFIED;
6996 if (RIDBIT_SETP (RID_CONST, specbits))
6997 type_quals |= TYPE_QUAL_CONST;
6998 if (RIDBIT_SETP (RID_VOLATILE, specbits))
6999 type_quals |= TYPE_QUAL_VOLATILE;
7000 if (RIDBIT_SETP (RID_RESTRICT, specbits))
7001 type_quals |= TYPE_QUAL_RESTRICT;
7002 if (sfk == sfk_conversion && type_quals != TYPE_UNQUALIFIED)
33bd39a2 7003 error ("qualifiers are not allowed on declaration of `operator %T'",
3ac3d9ea
MM
7004 ctor_return_type);
7005
4f2b0fb2
NS
7006 type_quals |= cp_type_quals (type);
7007 type = cp_build_qualified_type_real
7008 (type, type_quals, ((typedef_decl && !DECL_ARTIFICIAL (typedef_decl)
7009 ? tf_ignore_bad_quals : 0) | tf_error | tf_warning));
7010 /* We might have ignored or rejected some of the qualifiers. */
7011 type_quals = cp_type_quals (type);
7012
8d08fdba
MS
7013 staticp = 0;
7014 inlinep = !! RIDBIT_SETP (RID_INLINE, specbits);
8d08fdba 7015 virtualp = RIDBIT_SETP (RID_VIRTUAL, specbits);
db5ae43f
MS
7016 RIDBIT_RESET (RID_VIRTUAL, specbits);
7017 explicitp = RIDBIT_SETP (RID_EXPLICIT, specbits) != 0;
7018 RIDBIT_RESET (RID_EXPLICIT, specbits);
8d08fdba 7019
8d08fdba
MS
7020 if (RIDBIT_SETP (RID_STATIC, specbits))
7021 staticp = 1 + (decl_context == FIELD);
7022
7023 if (virtualp && staticp == 2)
7024 {
33bd39a2 7025 error ("member `%D' cannot be declared both virtual and static",
8d08fdba
MS
7026 dname);
7027 staticp = 0;
7028 }
7029 friendp = RIDBIT_SETP (RID_FRIEND, specbits);
8d08fdba
MS
7030 RIDBIT_RESET (RID_FRIEND, specbits);
7031
d5614afb
NS
7032 if (dependant_name && !friendp)
7033 {
7034 error ("`%T::%D' is not a valid declarator", ctype, dependant_name);
7035 return void_type_node;
7036 }
7037
8d08fdba
MS
7038 /* Warn if two storage classes are given. Default to `auto'. */
7039
7040 if (RIDBIT_ANY_SET (specbits))
7041 {
7042 if (RIDBIT_SETP (RID_STATIC, specbits)) nclasses++;
270d8c65 7043 if (RIDBIT_SETP (RID_EXTERN, specbits) && !extern_langp) nclasses++;
7a1f3f5f 7044 if (RIDBIT_SETP (RID_THREAD, specbits)) nclasses++;
8d08fdba 7045 if (decl_context == PARM && nclasses > 0)
8251199e 7046 error ("storage class specifiers invalid in parameter declarations");
8d08fdba
MS
7047 if (RIDBIT_SETP (RID_TYPEDEF, specbits))
7048 {
7049 if (decl_context == PARM)
8251199e 7050 error ("typedef declaration invalid in parameter declaration");
8d08fdba
MS
7051 nclasses++;
7052 }
7053 if (RIDBIT_SETP (RID_AUTO, specbits)) nclasses++;
7054 if (RIDBIT_SETP (RID_REGISTER, specbits)) nclasses++;
270d8c65
NS
7055 if (!nclasses && !friendp && extern_langp)
7056 nclasses++;
8d08fdba
MS
7057 }
7058
7059 /* Give error if `virtual' is used outside of class declaration. */
b7484fbe
MS
7060 if (virtualp
7061 && (current_class_name == NULL_TREE || decl_context != FIELD))
8d08fdba 7062 {
8251199e 7063 error ("virtual outside class declaration");
8d08fdba
MS
7064 virtualp = 0;
7065 }
8d08fdba
MS
7066
7067 /* Static anonymous unions are dealt with here. */
7068 if (staticp && decl_context == TYPENAME
7069 && TREE_CODE (declspecs) == TREE_LIST
6bdb8141 7070 && ANON_AGGR_TYPE_P (TREE_VALUE (declspecs)))
8d08fdba
MS
7071 decl_context = FIELD;
7072
8d08fdba
MS
7073 /* Warn about storage classes that are invalid for certain
7074 kinds of declarations (parameters, typenames, etc.). */
7075
7a1f3f5f
RH
7076 /* "static __thread" and "extern __thread" are allowed. */
7077 if (nclasses == 2
7078 && RIDBIT_SETP (RID_THREAD, specbits)
7079 && (RIDBIT_SETP (RID_EXTERN, specbits)
7080 || RIDBIT_SETP (RID_STATIC, specbits)))
7081 nclasses = 1;
7082
8d08fdba 7083 if (nclasses > 1)
8251199e 7084 error ("multiple storage classes in declaration of `%s'", name);
8d08fdba
MS
7085 else if (decl_context != NORMAL && nclasses > 0)
7086 {
db5ae43f 7087 if ((decl_context == PARM || decl_context == CATCHPARM)
8d08fdba
MS
7088 && (RIDBIT_SETP (RID_REGISTER, specbits)
7089 || RIDBIT_SETP (RID_AUTO, specbits)))
7090 ;
fc378698
MS
7091 else if (RIDBIT_SETP (RID_TYPEDEF, specbits))
7092 ;
8d08fdba 7093 else if (decl_context == FIELD
d363e7bf
AJ
7094 /* C++ allows static class elements */
7095 && RIDBIT_SETP (RID_STATIC, specbits))
7096 /* C++ also allows inlines and signed and unsigned elements,
7097 but in those cases we don't come in here. */
8d08fdba
MS
7098 ;
7099 else
7100 {
7101 if (decl_context == FIELD)
7102 {
b7484fbe
MS
7103 tree tmp = NULL_TREE;
7104 register int op = 0;
7105
7106 if (declarator)
7107 {
68642fb6 7108 /* Avoid trying to get an operand off an identifier node. */
9e9ff709
MS
7109 if (TREE_CODE (declarator) == IDENTIFIER_NODE)
7110 tmp = declarator;
7111 else
7112 tmp = TREE_OPERAND (declarator, 0);
b7484fbe 7113 op = IDENTIFIER_OPNAME_P (tmp);
596ea4e5
AS
7114 if (IDENTIFIER_TYPENAME_P (tmp))
7115 {
c003e212 7116 if (is_typename_at_global_scope (tmp))
596ea4e5
AS
7117 name = IDENTIFIER_POINTER (tmp);
7118 else
7119 name = "<invalid operator>";
7120 }
b7484fbe 7121 }
8251199e 7122 error ("storage class specified for %s `%s'",
6eabb241 7123 op ? "member operator" : "field",
596ea4e5 7124 name);
8d08fdba
MS
7125 }
7126 else
b9d12519
KG
7127 {
7128 if (decl_context == PARM || decl_context == CATCHPARM)
7129 error ("storage class specified for parameter `%s'", name);
7130 else
7131 error ("storage class specified for typename");
7132 }
8d08fdba
MS
7133 RIDBIT_RESET (RID_REGISTER, specbits);
7134 RIDBIT_RESET (RID_AUTO, specbits);
7135 RIDBIT_RESET (RID_EXTERN, specbits);
7a1f3f5f 7136 RIDBIT_RESET (RID_THREAD, specbits);
8d08fdba
MS
7137 }
7138 }
7139 else if (RIDBIT_SETP (RID_EXTERN, specbits) && initialized && !funcdef_flag)
7140 {
a9aedbc2 7141 if (toplevel_bindings_p ())
8d08fdba 7142 {
59be0cdd 7143 /* It's common practice (and completely valid) to have a const
8d08fdba 7144 be initialized and declared extern. */
950ad3c3 7145 if (!(type_quals & TYPE_QUAL_CONST))
8251199e 7146 warning ("`%s' initialized and declared `extern'", name);
8d08fdba
MS
7147 }
7148 else
8251199e 7149 error ("`%s' has both `extern' and initializer", name);
8d08fdba
MS
7150 }
7151 else if (RIDBIT_SETP (RID_EXTERN, specbits) && funcdef_flag
a9aedbc2 7152 && ! toplevel_bindings_p ())
8251199e 7153 error ("nested function `%s' declared `extern'", name);
a9aedbc2 7154 else if (toplevel_bindings_p ())
8d08fdba
MS
7155 {
7156 if (RIDBIT_SETP (RID_AUTO, specbits))
8251199e 7157 error ("top-level declaration of `%s' specifies `auto'", name);
8d08fdba 7158 }
7a1f3f5f
RH
7159 else if (RIDBIT_SETP (RID_THREAD, specbits)
7160 && !RIDBIT_SETP (RID_EXTERN, specbits)
7161 && !RIDBIT_SETP (RID_STATIC, specbits))
7162 {
7163 error ("function-scope `%s' implicitly auto and declared `__thread'",
7164 name);
7165 RIDBIT_RESET (RID_THREAD, specbits);
7166 }
8d08fdba 7167
909e536a 7168 if (nclasses > 0 && friendp)
8251199e 7169 error ("storage class specifiers invalid in friend function declarations");
909e536a 7170
a723baf1
MM
7171 scope = get_scope_of_declarator (declarator);
7172
8d08fdba
MS
7173 /* Now figure out the structure of the declarator proper.
7174 Descend through it, creating more complex types, until we reach
9e259dd1 7175 the declared identifier (or NULL_TREE, in an abstract declarator). */
8d08fdba 7176
386b8a85
JM
7177 while (declarator && TREE_CODE (declarator) != IDENTIFIER_NODE
7178 && TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
8d08fdba
MS
7179 {
7180 /* Each level of DECLARATOR is either an ARRAY_REF (for ...[..]),
7181 an INDIRECT_REF (for *...),
7182 a CALL_EXPR (for ...(...)),
7183 an identifier (for the name being declared)
7184 or a null pointer (for the place in an absolute declarator
7185 where the name was omitted).
7186 For the last two cases, we have just exited the loop.
7187
7188 For C++ it could also be
7189 a SCOPE_REF (for class :: ...). In this case, we have converted
7190 sensible names to types, and those are the values we use to
7191 qualify the member name.
7192 an ADDR_EXPR (for &...),
7193 a BIT_NOT_EXPR (for destructors)
8d08fdba
MS
7194
7195 At this point, TYPE is the type of elements of an array,
7196 or for a function to return, or for a pointer to point to.
7197 After this sequence of ifs, TYPE is the type of the
7198 array or function or pointer, and DECLARATOR has had its
7199 outermost layer removed. */
7200
bd6dd845 7201 if (type == error_mark_node)
8d08fdba 7202 {
88d5a16e
R
7203 if (declarator == error_mark_node)
7204 return error_mark_node;
7205 else if (TREE_CODE (declarator) == SCOPE_REF)
8d08fdba
MS
7206 declarator = TREE_OPERAND (declarator, 1);
7207 else
7208 declarator = TREE_OPERAND (declarator, 0);
7209 continue;
7210 }
7211 if (quals != NULL_TREE
7212 && (declarator == NULL_TREE
7213 || TREE_CODE (declarator) != SCOPE_REF))
7214 {
7215 if (ctype == NULL_TREE && TREE_CODE (type) == METHOD_TYPE)
7216 ctype = TYPE_METHOD_BASETYPE (type);
7217 if (ctype != NULL_TREE)
7218 {
8d08fdba 7219 tree dummy = build_decl (TYPE_DECL, NULL_TREE, type);
535233a8 7220 grok_method_quals (ctype, dummy, quals);
8d08fdba
MS
7221 type = TREE_TYPE (dummy);
7222 quals = NULL_TREE;
7223 }
7224 }
1eab9b56 7225
8d08fdba
MS
7226 switch (TREE_CODE (declarator))
7227 {
52fbc847
JM
7228 case TREE_LIST:
7229 {
7230 /* We encode a declarator with embedded attributes using
91d231cb
JM
7231 a TREE_LIST. */
7232 tree attrs = TREE_PURPOSE (declarator);
7233 tree inner_decl;
c9574c9f
R
7234 int attr_flags;
7235
52fbc847 7236 declarator = TREE_VALUE (declarator);
91d231cb
JM
7237 inner_decl = declarator;
7238 while (inner_decl != NULL_TREE
7239 && TREE_CODE (inner_decl) == TREE_LIST)
7240 inner_decl = TREE_VALUE (inner_decl);
c9574c9f 7241 attr_flags = 0;
91d231cb
JM
7242 if (inner_decl == NULL_TREE
7243 || TREE_CODE (inner_decl) == IDENTIFIER_NODE)
7244 attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
7245 if (TREE_CODE (inner_decl) == CALL_EXPR)
7246 attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
7247 if (TREE_CODE (inner_decl) == ARRAY_REF)
7248 attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
7249 returned_attrs = decl_attributes (&type,
7250 chainon (returned_attrs, attrs),
7251 attr_flags);
52fbc847
JM
7252 }
7253 break;
7254
8d08fdba
MS
7255 case ARRAY_REF:
7256 {
01bf0f3e 7257 tree size = TREE_OPERAND (declarator, 1);
817aed6f 7258 declarator = TREE_OPERAND (declarator, 0);
8d08fdba 7259
2848ad0f 7260 type = create_array_type_for_decl (dname, type, size);
c1b177ec 7261
8d08fdba
MS
7262 ctype = NULL_TREE;
7263 }
7264 break;
7265
7266 case CALL_EXPR:
7267 {
7268 tree arg_types;
f376e137 7269 int funcdecl_p;
43f887f9 7270 tree inner_parms = CALL_DECLARATOR_PARMS (declarator);
f376e137 7271 tree inner_decl = TREE_OPERAND (declarator, 0);
8d08fdba
MS
7272
7273 /* Declaring a function type.
7274 Make sure we have a valid type for the function to return. */
8d08fdba 7275
91063b51 7276 /* We now know that the TYPE_QUALS don't apply to the
14ae7e7d 7277 decl, but to its return type. */
91063b51 7278 type_quals = TYPE_UNQUALIFIED;
8d08fdba
MS
7279
7280 /* Warn about some types functions can't return. */
7281
7282 if (TREE_CODE (type) == FUNCTION_TYPE)
7283 {
8251199e 7284 error ("`%s' declared as function returning a function", name);
8d08fdba
MS
7285 type = integer_type_node;
7286 }
7287 if (TREE_CODE (type) == ARRAY_TYPE)
7288 {
8251199e 7289 error ("`%s' declared as function returning an array", name);
8d08fdba
MS
7290 type = integer_type_node;
7291 }
7292
f376e137
MS
7293 if (inner_decl && TREE_CODE (inner_decl) == SCOPE_REF)
7294 inner_decl = TREE_OPERAND (inner_decl, 1);
7295
68642fb6 7296 if (inner_decl && TREE_CODE (inner_decl) == TEMPLATE_ID_EXPR)
386b8a85
JM
7297 inner_decl = dname;
7298
b7484fbe 7299 /* Pick up type qualifiers which should be applied to `this'. */
43f887f9 7300 quals = CALL_DECLARATOR_QUALS (declarator);
b7484fbe 7301
c11b6f21 7302 /* Pick up the exception specifications. */
43f887f9 7303 raises = CALL_DECLARATOR_EXCEPTION_SPEC (declarator);
c11b6f21 7304
f376e137
MS
7305 /* Say it's a definition only for the CALL_EXPR
7306 closest to the identifier. */
beb53fb8 7307 funcdecl_p
68642fb6 7308 = inner_decl
386b8a85 7309 && (TREE_CODE (inner_decl) == IDENTIFIER_NODE
68642fb6 7310 || TREE_CODE (inner_decl) == TEMPLATE_ID_EXPR
386b8a85 7311 || TREE_CODE (inner_decl) == BIT_NOT_EXPR);
68642fb6 7312
8d08fdba
MS
7313 if (ctype == NULL_TREE
7314 && decl_context == FIELD
f376e137 7315 && funcdecl_p
8d08fdba
MS
7316 && (friendp == 0 || dname == current_class_name))
7317 ctype = current_class_type;
7318
3dbc07b6 7319 if (ctype && sfk == sfk_conversion)
8d08fdba 7320 TYPE_HAS_CONVERSION (ctype) = 1;
a723baf1 7321 if (ctype && constructor_name_p (dname, ctype))
8d08fdba
MS
7322 {
7323 /* We are within a class's scope. If our declarator name
7324 is the same as the class name, and we are defining
7325 a function, then it is a constructor/destructor, and
7326 therefore returns a void type. */
7327
7328 if (flags == DTOR_FLAG)
7329 {
cab1f180
ML
7330 /* ISO C++ 12.4/2. A destructor may not be
7331 declared const or volatile. A destructor may
7332 not be static. */
8d08fdba 7333 if (staticp == 2)
8251199e 7334 error ("destructor cannot be static member function");
b7484fbe 7335 if (quals)
8d08fdba 7336 {
33bd39a2 7337 error ("destructors may not be `%s'",
4f70a846
MM
7338 IDENTIFIER_POINTER (TREE_VALUE (quals)));
7339 quals = NULL_TREE;
8d08fdba
MS
7340 }
7341 if (decl_context == FIELD)
7342 {
4dacf5bd
KG
7343 if (! member_function_or_else (ctype,
7344 current_class_type,
7345 flags))
8d08fdba
MS
7346 return void_type_node;
7347 }
7348 }
2c73f9f5 7349 else /* It's a constructor. */
8d08fdba 7350 {
db5ae43f
MS
7351 if (explicitp == 1)
7352 explicitp = 2;
cab1f180
ML
7353 /* ISO C++ 12.1. A constructor may not be
7354 declared const or volatile. A constructor may
7355 not be virtual. A constructor may not be
7356 static. */
8d08fdba 7357 if (staticp == 2)
8251199e 7358 error ("constructor cannot be static member function");
8d08fdba
MS
7359 if (virtualp)
7360 {
8251199e 7361 pedwarn ("constructors cannot be declared virtual");
8d08fdba
MS
7362 virtualp = 0;
7363 }
b7484fbe 7364 if (quals)
8d08fdba 7365 {
33bd39a2 7366 error ("constructors may not be `%s'",
4f70a846
MM
7367 IDENTIFIER_POINTER (TREE_VALUE (quals)));
7368 quals = NULL_TREE;
d363e7bf 7369 }
8d08fdba 7370 {
51c184be 7371 RID_BIT_TYPE tmp_bits;
4e135bdd 7372 memcpy (&tmp_bits, &specbits, sizeof (RID_BIT_TYPE));
51c184be
MS
7373 RIDBIT_RESET (RID_INLINE, tmp_bits);
7374 RIDBIT_RESET (RID_STATIC, tmp_bits);
7375 if (RIDBIT_ANY_SET (tmp_bits))
8251199e 7376 error ("return value type specifier for constructor ignored");
8d08fdba 7377 }
6eabb241 7378 if (decl_context == FIELD)
8d08fdba 7379 {
4dacf5bd
KG
7380 if (! member_function_or_else (ctype,
7381 current_class_type,
7382 flags))
8d08fdba
MS
7383 return void_type_node;
7384 TYPE_HAS_CONSTRUCTOR (ctype) = 1;
3dbc07b6 7385 if (sfk != sfk_constructor)
8d08fdba
MS
7386 return NULL_TREE;
7387 }
7388 }
7389 if (decl_context == FIELD)
7390 staticp = 0;
7391 }
b7484fbe 7392 else if (friendp)
8d08fdba 7393 {
b7484fbe 7394 if (initialized)
8251199e 7395 error ("can't initialize friend function `%s'", name);
b7484fbe
MS
7396 if (virtualp)
7397 {
7398 /* Cannot be both friend and virtual. */
8251199e 7399 error ("virtual functions cannot be friends");
b7484fbe
MS
7400 RIDBIT_RESET (RID_FRIEND, specbits);
7401 friendp = 0;
7402 }
28cbf42c 7403 if (decl_context == NORMAL)
8251199e 7404 error ("friend declaration not in class definition");
28cbf42c 7405 if (current_function_decl && funcdef_flag)
33bd39a2 7406 error ("can't define friend function `%s' in a local class definition",
28cbf42c 7407 name);
8d08fdba
MS
7408 }
7409
8d08fdba
MS
7410 /* Construct the function type and go to the next
7411 inner layer of declarator. */
7412
f376e137 7413 declarator = TREE_OPERAND (declarator, 0);
8d08fdba 7414
5cce22b6 7415 arg_types = grokparms (inner_parms);
8d08fdba 7416
4f70a846 7417 if (declarator && flags == DTOR_FLAG)
8d08fdba 7418 {
4f70a846
MM
7419 /* A destructor declared in the body of a class will
7420 be represented as a BIT_NOT_EXPR. But, we just
7421 want the underlying IDENTIFIER. */
8d08fdba 7422 if (TREE_CODE (declarator) == BIT_NOT_EXPR)
4f70a846 7423 declarator = TREE_OPERAND (declarator, 0);
68642fb6 7424
8b27e9ef 7425 if (arg_types != void_list_node)
8d08fdba 7426 {
33bd39a2 7427 error ("destructors may not have parameters");
4f70a846
MM
7428 arg_types = void_list_node;
7429 last_function_parms = NULL_TREE;
8d08fdba
MS
7430 }
7431 }
7432
d22c8596 7433 /* ANSI says that `const int foo ();'
8d08fdba 7434 does not make the function foo const. */
d22c8596 7435 type = build_function_type (type, arg_types);
8d08fdba
MS
7436 }
7437 break;
7438
7439 case ADDR_EXPR:
7440 case INDIRECT_REF:
7441 /* Filter out pointers-to-references and references-to-references.
7442 We can get these if a TYPE_DECL is used. */
7443
7444 if (TREE_CODE (type) == REFERENCE_TYPE)
7445 {
dd29188b
NS
7446 error (TREE_CODE (declarator) == ADDR_EXPR
7447 ? "cannot declare reference to `%#T'"
7448 : "cannot declare pointer to `%#T'", type);
a5894242
MS
7449 type = TREE_TYPE (type);
7450 }
dd29188b
NS
7451 else if (VOID_TYPE_P (type)
7452 && (ctype || TREE_CODE (declarator) == ADDR_EXPR))
7453 error (ctype ? "cannot declare pointer to `%#T' member"
7454 : "cannot declare reference to `%#T'", type);
a5894242 7455
61a127b3
MM
7456 /* Merge any constancy or volatility into the target type
7457 for the pointer. */
7458
91063b51
MM
7459 /* We now know that the TYPE_QUALS don't apply to the decl,
7460 but to the target of the pointer. */
7461 type_quals = TYPE_UNQUALIFIED;
8d08fdba 7462
6eabb241 7463 if (TREE_CODE (declarator) == ADDR_EXPR)
8d08fdba 7464 {
dd29188b 7465 if (!VOID_TYPE_P (type))
69851283 7466 type = build_reference_type (type);
8d08fdba
MS
7467 }
7468 else if (TREE_CODE (type) == METHOD_TYPE)
d8f8dca1 7469 type = build_ptrmemfunc_type (build_pointer_type (type));
9e259dd1
MM
7470 else if (ctype)
7471 type = build_ptrmem_type (ctype, type);
8d08fdba
MS
7472 else
7473 type = build_pointer_type (type);
7474
7475 /* Process a list of type modifier keywords (such as
7476 const or volatile) that were given inside the `*' or `&'. */
7477
7478 if (TREE_TYPE (declarator))
7479 {
7480 register tree typemodlist;
7481 int erred = 0;
4f2b0fb2
NS
7482 int constp = 0;
7483 int volatilep = 0;
7484 int restrictp = 0;
7485
8d08fdba
MS
7486 for (typemodlist = TREE_TYPE (declarator); typemodlist;
7487 typemodlist = TREE_CHAIN (typemodlist))
7488 {
91063b51
MM
7489 tree qualifier = TREE_VALUE (typemodlist);
7490
7491 if (qualifier == ridpointers[(int) RID_CONST])
4f2b0fb2
NS
7492 {
7493 constp++;
7494 type_quals |= TYPE_QUAL_CONST;
7495 }
91063b51 7496 else if (qualifier == ridpointers[(int) RID_VOLATILE])
4f2b0fb2
NS
7497 {
7498 volatilep++;
7499 type_quals |= TYPE_QUAL_VOLATILE;
7500 }
91063b51 7501 else if (qualifier == ridpointers[(int) RID_RESTRICT])
4f2b0fb2
NS
7502 {
7503 restrictp++;
7504 type_quals |= TYPE_QUAL_RESTRICT;
7505 }
8d08fdba
MS
7506 else if (!erred)
7507 {
7508 erred = 1;
91063b51 7509 error ("invalid type modifier within pointer declarator");
8d08fdba
MS
7510 }
7511 }
7512 if (constp > 1)
8251199e 7513 pedwarn ("duplicate `const'");
8d08fdba 7514 if (volatilep > 1)
8251199e 7515 pedwarn ("duplicate `volatile'");
91063b51
MM
7516 if (restrictp > 1)
7517 pedwarn ("duplicate `restrict'");
91063b51 7518 type = cp_build_qualified_type (type, type_quals);
4f2b0fb2 7519 type_quals = cp_type_quals (type);
8d08fdba
MS
7520 }
7521 declarator = TREE_OPERAND (declarator, 0);
7522 ctype = NULL_TREE;
7523 break;
7524
7525 case SCOPE_REF:
7526 {
7527 /* We have converted type names to NULL_TREE if the
7528 name was bogus, or to a _TYPE node, if not.
7529
7530 The variable CTYPE holds the type we will ultimately
7531 resolve to. The code here just needs to build
7532 up appropriate member types. */
7533 tree sname = TREE_OPERAND (declarator, 1);
386b8a85
JM
7534 tree t;
7535
8d08fdba
MS
7536 /* Destructors can have their visibilities changed as well. */
7537 if (TREE_CODE (sname) == BIT_NOT_EXPR)
7538 sname = TREE_OPERAND (sname, 0);
7539
8d08fdba
MS
7540 if (TREE_OPERAND (declarator, 0) == NULL_TREE)
7541 {
7542 /* We had a reference to a global decl, or
7543 perhaps we were given a non-aggregate typedef,
7544 in which case we cleared this out, and should just
7545 keep going as though it wasn't there. */
7546 declarator = sname;
7547 continue;
7548 }
7549 ctype = TREE_OPERAND (declarator, 0);
7550
386b8a85 7551 t = ctype;
68642fb6 7552 while (t != NULL_TREE && CLASS_TYPE_P (t))
386b8a85 7553 {
5362b086 7554 /* You're supposed to have one `template <...>'
da8a7456
MM
7555 for every template class, but you don't need one
7556 for a full specialization. For example:
7557
7558 template <class T> struct S{};
7559 template <> struct S<int> { void f(); };
7560 void S<int>::f () {}
7561
7562 is correct; there shouldn't be a `template <>' for
7563 the definition of `S<int>::f'. */
7564 if (CLASSTYPE_TEMPLATE_INFO (t)
7565 && (CLASSTYPE_TEMPLATE_INSTANTIATION (t)
92ecdfb7
NS
7566 || uses_template_parms (CLASSTYPE_TI_ARGS (t)))
7567 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t)))
386b8a85 7568 template_count += 1;
da8a7456 7569
386b8a85 7570 t = TYPE_MAIN_DECL (t);
92ecdfb7 7571 t = DECL_CONTEXT (t);
386b8a85
JM
7572 }
7573
8d08fdba
MS
7574 if (sname == NULL_TREE)
7575 goto done_scoping;
7576
7577 if (TREE_CODE (sname) == IDENTIFIER_NODE)
7578 {
7579 /* This is the `standard' use of the scoping operator:
7580 basetype :: member . */
7581
db5ae43f 7582 if (ctype == current_class_type)
28cbf42c
MS
7583 {
7584 /* class A {
7585 void A::f ();
7586 };
7587
7588 Is this ill-formed? */
7589
7590 if (pedantic)
33bd39a2 7591 pedwarn ("extra qualification `%T::' on member `%s' ignored",
28cbf42c
MS
7592 ctype, name);
7593 }
db5ae43f 7594 else if (TREE_CODE (type) == FUNCTION_TYPE)
8d08fdba 7595 {
eeb75383 7596 if (current_class_type == NULL_TREE || friendp)
43dc123f
MM
7597 type
7598 = build_method_type_directly (ctype,
7599 TREE_TYPE (type),
14ae7e7d 7600 TYPE_ARG_TYPES (type));
8d08fdba
MS
7601 else
7602 {
33bd39a2 7603 error ("cannot declare member function `%T::%s' within `%T'",
a3203465 7604 ctype, name, current_class_type);
a723baf1 7605 return error_mark_node;
8d08fdba
MS
7606 }
7607 }
5566b478 7608 else if (RIDBIT_SETP (RID_TYPEDEF, specbits)
d0f062fb 7609 || COMPLETE_TYPE_P (complete_type (ctype)))
8d08fdba 7610 {
2c73f9f5 7611 /* Have to move this code elsewhere in this function.
db5ae43f
MS
7612 this code is used for i.e., typedef int A::M; M *pm;
7613
7614 It is? How? jason 10/2/94 */
8d08fdba 7615
8d08fdba
MS
7616 if (current_class_type)
7617 {
33bd39a2 7618 error ("cannot declare member `%T::%s' within `%T'",
db5ae43f
MS
7619 ctype, name, current_class_type);
7620 return void_type_node;
8d08fdba 7621 }
8d08fdba 7622 }
8d08fdba 7623 else
eeb75383 7624 {
7a228918 7625 cxx_incomplete_type_error (NULL_TREE, ctype);
eeb75383 7626 return error_mark_node;
d363e7bf 7627 }
8d08fdba
MS
7628
7629 declarator = sname;
7630 }
8d08fdba 7631 else if (TREE_CODE (sname) == SCOPE_REF)
a98facb0 7632 abort ();
8d08fdba
MS
7633 else
7634 {
7635 done_scoping:
7636 declarator = TREE_OPERAND (declarator, 1);
7637 if (declarator && TREE_CODE (declarator) == CALL_EXPR)
7638 /* In this case, we will deal with it later. */
7639 ;
9e259dd1 7640 else if (TREE_CODE (type) == FUNCTION_TYPE)
43dc123f
MM
7641 type = build_method_type_directly (ctype,
7642 TREE_TYPE (type),
7643 TYPE_ARG_TYPES (type));
8d08fdba
MS
7644 }
7645 }
7646 break;
7647
7648 case BIT_NOT_EXPR:
7649 declarator = TREE_OPERAND (declarator, 0);
7650 break;
7651
50ad9642
MM
7652 case BASELINK:
7653 declarator = BASELINK_FUNCTIONS (declarator);
7654 break;
7655
8d08fdba
MS
7656 case RECORD_TYPE:
7657 case UNION_TYPE:
7658 case ENUMERAL_TYPE:
7659 declarator = NULL_TREE;
7660 break;
7661
7662 case ERROR_MARK:
7663 declarator = NULL_TREE;
7664 break;
7665
7666 default:
a98facb0 7667 abort ();
8d08fdba
MS
7668 }
7669 }
7670
91d231cb 7671 if (returned_attrs)
1eab9b56 7672 {
91d231cb
JM
7673 if (attrlist)
7674 *attrlist = chainon (returned_attrs, *attrlist);
1eab9b56 7675 else
91d231cb 7676 attrlist = &returned_attrs;
1eab9b56
JM
7677 }
7678
419c6212
JM
7679 /* Now TYPE has the actual type. */
7680
55b3d665
JM
7681 /* Did array size calculations overflow? */
7682
7683 if (TREE_CODE (type) == ARRAY_TYPE
7684 && COMPLETE_TYPE_P (type)
7685 && TREE_OVERFLOW (TYPE_SIZE (type)))
7686 {
7687 error ("size of array `%s' is too large", name);
aba649ba 7688 /* If we proceed with the array type as it is, we'll eventually
55b3d665
JM
7689 crash in tree_low_cst(). */
7690 type = error_mark_node;
7691 }
7692
2fff6d71 7693 if ((decl_context == FIELD || decl_context == PARM)
dac45b5c
MM
7694 && !processing_template_decl
7695 && variably_modified_type_p (type))
7696 {
2fff6d71
MM
7697 if (decl_context == FIELD)
7698 error ("data member may not have variably modified type `%T'", type);
7699 else
7700 error ("parameter may not have variably modified type `%T'", type);
dac45b5c
MM
7701 type = error_mark_node;
7702 }
7703
34fc957d 7704 if (explicitp == 1 || (explicitp && friendp))
db5ae43f 7705 {
34fc957d
NS
7706 /* [dcl.fct.spec] The explicit specifier shall only be used in
7707 declarations of constructors within a class definition. */
7708 error ("only declarations of constructors can be `explicit'");
db5ae43f
MS
7709 explicitp = 0;
7710 }
7711
f30432d7
MS
7712 if (RIDBIT_SETP (RID_MUTABLE, specbits))
7713 {
4223f82f 7714 if (decl_context != FIELD || friendp)
34fc957d
NS
7715 {
7716 error ("non-member `%s' cannot be declared `mutable'", name);
7717 RIDBIT_RESET (RID_MUTABLE, specbits);
7718 }
7719 else if (decl_context == TYPENAME || RIDBIT_SETP (RID_TYPEDEF, specbits))
f30432d7 7720 {
34fc957d 7721 error ("non-object member `%s' cannot be declared `mutable'", name);
f30432d7
MS
7722 RIDBIT_RESET (RID_MUTABLE, specbits);
7723 }
34fc957d
NS
7724 else if (TREE_CODE (type) == FUNCTION_TYPE
7725 || TREE_CODE (type) == METHOD_TYPE)
7726 {
7727 error ("function `%s' cannot be declared `mutable'", name);
7728 RIDBIT_RESET (RID_MUTABLE, specbits);
7729 }
f30432d7
MS
7730 else if (staticp)
7731 {
8251199e 7732 error ("static `%s' cannot be declared `mutable'", name);
f30432d7
MS
7733 RIDBIT_RESET (RID_MUTABLE, specbits);
7734 }
34fc957d
NS
7735 else if (type_quals & TYPE_QUAL_CONST)
7736 {
7737 error ("const `%s' cannot be declared `mutable'", name);
4f2b0fb2 7738 RIDBIT_RESET (RID_MUTABLE, specbits);
34fc957d 7739 }
f30432d7
MS
7740 }
7741
efe38fab
JM
7742 if (declarator == NULL_TREE
7743 || TREE_CODE (declarator) == IDENTIFIER_NODE
7744 || (TREE_CODE (declarator) == TEMPLATE_ID_EXPR
7745 && (TREE_CODE (type) == FUNCTION_TYPE
7746 || TREE_CODE (type) == METHOD_TYPE)))
7747 /* OK */;
7748 else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
7749 {
33bd39a2 7750 error ("template-id `%D' used as a declarator", declarator);
efe38fab
JM
7751 declarator = dname;
7752 }
7753 else
419c6212 7754 /* Unexpected declarator format. */
a98facb0 7755 abort ();
efe38fab 7756
419c6212
JM
7757 /* If this is declaring a typedef name, return a TYPE_DECL. */
7758
fc378698 7759 if (RIDBIT_SETP (RID_TYPEDEF, specbits) && decl_context != TYPENAME)
8d08fdba
MS
7760 {
7761 tree decl;
7762
7763 /* Note that the grammar rejects storage classes
7764 in typenames, fields or parameters. */
eff71ab0
PB
7765 if (current_lang_name == lang_name_java)
7766 TYPE_FOR_JAVA (type) = 1;
8d08fdba 7767
d2e5ee5c
MS
7768 if (decl_context == FIELD)
7769 {
a723baf1 7770 if (constructor_name_p (declarator, current_class_type))
33bd39a2 7771 pedwarn ("ISO C++ forbids nested type `%D' with same name as enclosing class",
d2e5ee5c
MS
7772 declarator);
7773 decl = build_lang_decl (TYPE_DECL, declarator, type);
d2e5ee5c
MS
7774 }
7775 else
6bda7a5e
NS
7776 {
7777 decl = build_decl (TYPE_DECL, declarator, type);
a723baf1 7778 if (in_namespace || ctype)
ddd2d57e 7779 error ("%Jtypedef name may not be a nested-name-specifier", decl);
6bda7a5e
NS
7780 if (!current_function_decl)
7781 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
7782 }
7783
9188c363
MM
7784 /* If the user declares "typedef struct {...} foo" then the
7785 struct will have an anonymous name. Fill that name in now.
7786 Nothing can refer to it, so nothing needs know about the name
7787 change. */
8d08fdba 7788 if (type != error_mark_node
2bdb0643 7789 && declarator
8d08fdba
MS
7790 && TYPE_NAME (type)
7791 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
1951a1b6 7792 && TYPE_ANONYMOUS_P (type)
ac905924
JM
7793 /* Don't do this if there are attributes. */
7794 && (!attrlist || !*attrlist)
89d684bb 7795 && cp_type_quals (type) == TYPE_UNQUALIFIED)
8d08fdba 7796 {
dcd08efc
JM
7797 tree oldname = TYPE_NAME (type);
7798 tree t;
7799
2c73f9f5 7800 /* Replace the anonymous name with the real name everywhere. */
8d08fdba 7801 lookup_tag_reverse (type, declarator);
dcd08efc
JM
7802 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
7803 if (TYPE_NAME (t) == oldname)
7804 TYPE_NAME (t) = decl;
8d08fdba
MS
7805
7806 if (TYPE_LANG_SPECIFIC (type))
7807 TYPE_WAS_ANONYMOUS (type) = 1;
7808
33964bf4
MM
7809 /* If this is a typedef within a template class, the nested
7810 type is a (non-primary) template. The name for the
7811 template needs updating as well. */
7812 if (TYPE_LANG_SPECIFIC (type) && CLASSTYPE_TEMPLATE_INFO (type))
68642fb6 7813 DECL_NAME (CLASSTYPE_TI_TEMPLATE (type))
33964bf4
MM
7814 = TYPE_IDENTIFIER (type);
7815
50a6dbd7
JM
7816 /* FIXME remangle member functions; member functions of a
7817 type with external linkage have external linkage. */
fc378698 7818 }
fc378698 7819
a723baf1 7820 if (quals)
8d08fdba
MS
7821 {
7822 if (ctype == NULL_TREE)
7823 {
7824 if (TREE_CODE (type) != METHOD_TYPE)
ddd2d57e
RH
7825 error ("%Jinvalid type qualifier for non-member function type",
7826 decl);
8d08fdba
MS
7827 else
7828 ctype = TYPE_METHOD_BASETYPE (type);
7829 }
7830 if (ctype != NULL_TREE)
7831 grok_method_quals (ctype, decl, quals);
7832 }
7833
7834 if (RIDBIT_SETP (RID_SIGNED, specbits)
7835 || (typedef_decl && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl)))
7836 C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
7837
c91a56d2
MS
7838 bad_specifiers (decl, "type", virtualp, quals != NULL_TREE,
7839 inlinep, friendp, raises != NULL_TREE);
7840
8d08fdba
MS
7841 return decl;
7842 }
7843
7844 /* Detect the case of an array type of unspecified size
7845 which came, as such, direct from a typedef name.
8d6e459d
NS
7846 We must copy the type, so that the array's domain can be
7847 individually set by the object's initializer. */
8d08fdba 7848
8d6e459d
NS
7849 if (type && typedef_type
7850 && TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type)
55b3d665 7851 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (typedef_type))
8d6e459d 7852 type = build_cplus_array_type (TREE_TYPE (type), NULL_TREE);
8d08fdba 7853
969fd501
NS
7854 /* Detect where we're using a typedef of function type to declare a
7855 function. last_function_parms will not be set, so we must create
7856 it now. */
7857
7858 if (type == typedef_type && TREE_CODE (type) == FUNCTION_TYPE)
7859 {
7860 tree decls = NULL_TREE;
7861 tree args;
7862
7863 for (args = TYPE_ARG_TYPES (type); args; args = TREE_CHAIN (args))
7864 {
8e51619a 7865 tree decl = cp_build_parm_decl (NULL_TREE, TREE_VALUE (args));
969fd501
NS
7866
7867 TREE_CHAIN (decl) = decls;
7868 decls = decl;
7869 }
7870
7871 last_function_parms = nreverse (decls);
7872 }
7873
8d08fdba
MS
7874 /* If this is a type name (such as, in a cast or sizeof),
7875 compute the type and return it now. */
7876
7877 if (decl_context == TYPENAME)
7878 {
7879 /* Note that the grammar rejects storage classes
7880 in typenames, fields or parameters. */
91063b51 7881 if (type_quals != TYPE_UNQUALIFIED)
6eabb241 7882 type_quals = TYPE_UNQUALIFIED;
8d08fdba
MS
7883
7884 /* Special case: "friend class foo" looks like a TYPENAME context. */
7885 if (friendp)
7886 {
91063b51 7887 if (type_quals != TYPE_UNQUALIFIED)
b7484fbe 7888 {
33bd39a2 7889 error ("type qualifiers specified for friend class declaration");
91063b51 7890 type_quals = TYPE_UNQUALIFIED;
b7484fbe
MS
7891 }
7892 if (inlinep)
7893 {
33bd39a2 7894 error ("`inline' specified for friend class declaration");
b7484fbe
MS
7895 inlinep = 0;
7896 }
f2ae0c45 7897
218e0eb6 7898 if (!current_aggr)
4b054b80 7899 {
218e0eb6 7900 /* Don't allow friend declaration without a class-key. */
4b054b80 7901 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
218e0eb6 7902 pedwarn ("template parameters cannot be friends");
f2ae0c45 7903 else if (TREE_CODE (type) == TYPENAME_TYPE)
218e0eb6
KL
7904 pedwarn ("friend declaration requires class-key, "
7905 "i.e. `friend class %T::%D'",
7906 TYPE_CONTEXT (type), TYPENAME_TYPE_FULLNAME (type));
4b054b80 7907 else
218e0eb6
KL
7908 pedwarn ("friend declaration requires class-key, "
7909 "i.e. `friend %#T'",
7910 type);
4b054b80 7911 }
b7484fbe
MS
7912
7913 /* Only try to do this stuff if we didn't already give up. */
7914 if (type != integer_type_node)
7915 {
7916 /* A friendly class? */
7917 if (current_class_type)
19db77ce
KL
7918 make_friend_class (current_class_type, TYPE_MAIN_VARIANT (type),
7919 /*complain=*/true);
b7484fbe 7920 else
33bd39a2 7921 error ("trying to make class `%T' a friend of global scope",
2ae7bada 7922 type);
d363e7bf 7923
b7484fbe
MS
7924 type = void_type_node;
7925 }
8d08fdba
MS
7926 }
7927 else if (quals)
7928 {
8d08fdba
MS
7929 if (ctype == NULL_TREE)
7930 {
41cbc04c 7931 if (TREE_CODE (type) != METHOD_TYPE)
33bd39a2 7932 error ("invalid qualifiers on non-member function type");
41cbc04c
NS
7933 else
7934 ctype = TYPE_METHOD_BASETYPE (type);
7935 }
7936 if (ctype)
7937 {
7938 tree dummy = build_decl (TYPE_DECL, declarator, type);
7939 grok_method_quals (ctype, dummy, quals);
7940 type = TREE_TYPE (dummy);
8d08fdba 7941 }
8d08fdba
MS
7942 }
7943
7944 return type;
7945 }
7946 else if (declarator == NULL_TREE && decl_context != PARM
db5ae43f 7947 && decl_context != CATCHPARM
8d08fdba
MS
7948 && TREE_CODE (type) != UNION_TYPE
7949 && ! bitfield)
7950 {
33bd39a2 7951 error ("abstract declarator `%T' used as declaration", type);
8d08fdba
MS
7952 declarator = make_anon_name ();
7953 }
7954
7955 /* `void' at top level (not within pointer)
7956 is allowed only in typedefs or type names.
7957 We don't complain about parms either, but that is because
7958 a better error message can be made later. */
7959
a1774733 7960 if (TREE_CODE (type) == VOID_TYPE && decl_context != PARM)
8d08fdba 7961 {
b7484fbe 7962 if (! declarator)
8251199e 7963 error ("unnamed variable or field declared void");
b7484fbe 7964 else if (TREE_CODE (declarator) == IDENTIFIER_NODE)
8d08fdba
MS
7965 {
7966 if (IDENTIFIER_OPNAME_P (declarator))
a98facb0 7967 abort ();
8d08fdba 7968 else
8251199e 7969 error ("variable or field `%s' declared void", name);
8d08fdba
MS
7970 }
7971 else
8251199e 7972 error ("variable or field declared void");
8d08fdba
MS
7973 type = integer_type_node;
7974 }
7975
7976 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
7977 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
7978
14ae7e7d
JM
7979 if (decl_context == PARM || decl_context == CATCHPARM)
7980 {
7981 if (ctype || in_namespace)
7982 error ("cannot use `::' in parameter declaration");
7983
7984 /* A parameter declared as an array of T is really a pointer to T.
7985 One declared as a function is really a pointer to a function.
7986 One declared as a member is really a pointer to member. */
7987
7988 if (TREE_CODE (type) == ARRAY_TYPE)
7989 {
7990 /* Transfer const-ness of array into that of type pointed to. */
7991 type = build_pointer_type (TREE_TYPE (type));
91063b51 7992 type_quals = TYPE_UNQUALIFIED;
14ae7e7d
JM
7993 }
7994 else if (TREE_CODE (type) == FUNCTION_TYPE)
7995 type = build_pointer_type (type);
14ae7e7d 7996 }
68642fb6 7997
8d08fdba
MS
7998 {
7999 register tree decl;
8000
8001 if (decl_context == PARM)
8002 {
8e51619a 8003 decl = cp_build_parm_decl (declarator, type);
8d08fdba
MS
8004
8005 bad_specifiers (decl, "parameter", virtualp, quals != NULL_TREE,
8006 inlinep, friendp, raises != NULL_TREE);
8d08fdba
MS
8007 }
8008 else if (decl_context == FIELD)
8009 {
01bf0f3e
JM
8010 /* The C99 flexible array extension. */
8011 if (!staticp && TREE_CODE (type) == ARRAY_TYPE
8012 && TYPE_DOMAIN (type) == NULL_TREE)
8013 {
8014 tree itype = compute_array_index_type (dname, integer_zero_node);
8015 type = build_cplus_array_type (TREE_TYPE (type), itype);
8016 }
8017
8d08fdba
MS
8018 if (type == error_mark_node)
8019 {
8020 /* Happens when declaring arrays of sizes which
8021 are error_mark_node, for example. */
8022 decl = NULL_TREE;
8023 }
997a088c 8024 else if (in_namespace && !friendp)
05008fb9
MM
8025 {
8026 /* Something like struct S { int N::j; }; */
33bd39a2 8027 error ("invalid use of `::'");
05008fb9
MM
8028 decl = NULL_TREE;
8029 }
8d08fdba
MS
8030 else if (TREE_CODE (type) == FUNCTION_TYPE)
8031 {
8032 int publicp = 0;
e1467ff2 8033 tree function_context;
8d08fdba 8034
72b7eeff
MS
8035 /* We catch the others as conflicts with the builtin
8036 typedefs. */
8037 if (friendp && declarator == ridpointers[(int) RID_SIGNED])
8038 {
33bd39a2 8039 error ("function `%D' cannot be declared friend",
72b7eeff
MS
8040 declarator);
8041 friendp = 0;
8042 }
8043
8d08fdba
MS
8044 if (friendp == 0)
8045 {
8046 if (ctype == NULL_TREE)
8047 ctype = current_class_type;
8048
8049 if (ctype == NULL_TREE)
8050 {
33bd39a2 8051 error ("can't make `%D' into a method -- not in a class",
8d08fdba
MS
8052 declarator);
8053 return void_type_node;
8054 }
8055
8056 /* ``A union may [ ... ] not [ have ] virtual functions.''
8057 ARM 9.5 */
8058 if (virtualp && TREE_CODE (ctype) == UNION_TYPE)
8059 {
33bd39a2 8060 error ("function `%D' declared virtual inside a union",
8d08fdba
MS
8061 declarator);
8062 return void_type_node;
8063 }
8064
596ea4e5
AS
8065 if (declarator == ansi_opname (NEW_EXPR)
8066 || declarator == ansi_opname (VEC_NEW_EXPR)
8067 || declarator == ansi_opname (DELETE_EXPR)
8068 || declarator == ansi_opname (VEC_DELETE_EXPR))
8d08fdba
MS
8069 {
8070 if (virtualp)
8071 {
33bd39a2 8072 error ("`%D' cannot be declared virtual, since it is always static",
8d08fdba
MS
8073 declarator);
8074 virtualp = 0;
8075 }
8076 }
8077 else if (staticp < 2)
43dc123f
MM
8078 type = build_method_type_directly (ctype,
8079 TREE_TYPE (type),
8080 TYPE_ARG_TYPES (type));
8d08fdba
MS
8081 }
8082
8083 /* Tell grokfndecl if it needs to set TREE_PUBLIC on the node. */
68642fb6 8084 function_context = (ctype != NULL_TREE) ?
4f1c5b7d 8085 decl_function_context (TYPE_MAIN_DECL (ctype)) : NULL_TREE;
e1467ff2
MM
8086 publicp = (! friendp || ! staticp)
8087 && function_context == NULL_TREE;
68642fb6 8088 decl = grokfndecl (ctype, type,
386b8a85
JM
8089 TREE_CODE (declarator) != TEMPLATE_ID_EXPR
8090 ? declarator : dname,
8091 declarator,
7a8f9fa9 8092 virtualp, flags, quals, raises,
386b8a85 8093 friendp ? -1 : 0, friendp, publicp, inlinep,
2c73f9f5 8094 funcdef_flag, template_count, in_namespace);
20496fa2 8095 if (decl == NULL_TREE)
3ddfb0e6 8096 return decl;
6125f3be
DE
8097#if 0
8098 /* This clobbers the attrs stored in `decl' from `attrlist'. */
91d231cb
JM
8099 /* The decl and setting of decl_attr is also turned off. */
8100 decl = build_decl_attribute_variant (decl, decl_attr);
6125f3be 8101#endif
f0e01782 8102
cc804e51
MM
8103 /* [class.conv.ctor]
8104
8105 A constructor declared without the function-specifier
8106 explicit that can be called with a single parameter
8107 specifies a conversion from the type of its first
8108 parameter to the type of its class. Such a constructor
8109 is called a converting constructor. */
db5ae43f
MS
8110 if (explicitp == 2)
8111 DECL_NONCONVERTING_P (decl) = 1;
cc804e51
MM
8112 else if (DECL_CONSTRUCTOR_P (decl))
8113 {
8114 /* The constructor can be called with exactly one
8115 parameter if there is at least one parameter, and
8116 any subsequent parameters have default arguments.
e0fff4b3
JM
8117 Ignore any compiler-added parms. */
8118 tree arg_types = FUNCTION_FIRST_USER_PARMTYPE (decl);
cc804e51
MM
8119
8120 if (arg_types == void_list_node
68642fb6
UD
8121 || (arg_types
8122 && TREE_CHAIN (arg_types)
cc804e51
MM
8123 && TREE_CHAIN (arg_types) != void_list_node
8124 && !TREE_PURPOSE (TREE_CHAIN (arg_types))))
8125 DECL_NONCONVERTING_P (decl) = 1;
8126 }
8d08fdba
MS
8127 }
8128 else if (TREE_CODE (type) == METHOD_TYPE)
8129 {
faae18ab
MS
8130 /* We only get here for friend declarations of
8131 members of other classes. */
8d08fdba
MS
8132 /* All method decls are public, so tell grokfndecl to set
8133 TREE_PUBLIC, also. */
866eb556
KL
8134 decl = grokfndecl (ctype, type,
8135 TREE_CODE (declarator) != TEMPLATE_ID_EXPR
8136 ? declarator : dname,
8137 declarator,
7a8f9fa9 8138 virtualp, flags, quals, raises,
386b8a85 8139 friendp ? -1 : 0, friendp, 1, 0, funcdef_flag,
2c73f9f5 8140 template_count, in_namespace);
f0e01782
MS
8141 if (decl == NULL_TREE)
8142 return NULL_TREE;
8d08fdba 8143 }
5566b478 8144 else if (!staticp && ! processing_template_decl
d0f062fb 8145 && !COMPLETE_TYPE_P (complete_type (type))
8d08fdba
MS
8146 && (TREE_CODE (type) != ARRAY_TYPE || initialized == 0))
8147 {
b7484fbe 8148 if (declarator)
33bd39a2 8149 error ("field `%D' has incomplete type", declarator);
b7484fbe 8150 else
33bd39a2 8151 error ("name `%T' has incomplete type", type);
8d08fdba
MS
8152
8153 /* If we're instantiating a template, tell them which
8154 instantiation made the field's type be incomplete. */
8155 if (current_class_type
8156 && TYPE_NAME (current_class_type)
d2e5ee5c 8157 && IDENTIFIER_TEMPLATE (TYPE_IDENTIFIER (current_class_type))
8d08fdba
MS
8158 && declspecs && TREE_VALUE (declspecs)
8159 && TREE_TYPE (TREE_VALUE (declspecs)) == type)
33bd39a2 8160 error (" in instantiation of template `%T'",
db5ae43f
MS
8161 current_class_type);
8162
8d08fdba
MS
8163 type = error_mark_node;
8164 decl = NULL_TREE;
8165 }
8166 else
8167 {
8168 if (friendp)
8169 {
cb9a3ff8 8170 error ("`%s' is neither function nor member function; cannot be declared friend",
8d08fdba
MS
8171 IDENTIFIER_POINTER (declarator));
8172 friendp = 0;
8173 }
8174 decl = NULL_TREE;
8175 }
8176
8177 if (friendp)
8178 {
8179 /* Friends are treated specially. */
8180 if (ctype == current_class_type)
8251199e 8181 warning ("member functions are implicitly friends of their class");
8db1028e
NS
8182 else if (decl && DECL_NAME (decl))
8183 {
8184 if (template_class_depth (current_class_type) == 0)
8185 {
8186 decl = check_explicit_specialization
8187 (declarator, decl, template_count,
8188 2 * (funcdef_flag != 0) + 4);
8189 if (decl == error_mark_node)
8190 return error_mark_node;
8191 }
8192
8193 decl = do_friend (ctype, declarator, decl,
8194 last_function_parms, *attrlist,
8195 flags, quals, funcdef_flag);
8196 return decl;
8197 }
8198 else
8199 return void_type_node;
8d08fdba
MS
8200 }
8201
8202 /* Structure field. It may not be a function, except for C++ */
8203
8204 if (decl == NULL_TREE)
8205 {
8d08fdba
MS
8206 if (initialized)
8207 {
3ac3d9ea
MM
8208 if (!staticp)
8209 {
8210 /* An attempt is being made to initialize a non-static
8211 member. But, from [class.mem]:
68642fb6 8212
3ac3d9ea
MM
8213 4 A member-declarator can contain a
8214 constant-initializer only if it declares a static
8215 member (_class.static_) of integral or enumeration
68642fb6 8216 type, see _class.static.data_.
3ac3d9ea
MM
8217
8218 This used to be relatively common practice, but
8219 the rest of the compiler does not correctly
8220 handle the initialization unless the member is
8221 static so we make it static below. */
33bd39a2 8222 pedwarn ("ISO C++ forbids initialization of member `%D'",
3ac3d9ea 8223 declarator);
33bd39a2 8224 pedwarn ("making `%D' static", declarator);
3ac3d9ea
MM
8225 staticp = 1;
8226 }
8227
6ba89f8e
MM
8228 if (uses_template_parms (type))
8229 /* We'll check at instantiation time. */
8230 ;
8231 else if (check_static_variable_definition (declarator,
8232 type))
8233 /* If we just return the declaration, crashes
8234 will sometimes occur. We therefore return
72a93143
JM
8235 void_type_node, as if this was a friend
8236 declaration, to cause callers to completely
8237 ignore this declaration. */
6ba89f8e 8238 return void_type_node;
8d08fdba
MS
8239 }
8240
3ac3d9ea 8241 if (staticp)
8d08fdba 8242 {
c717c5af
MM
8243 /* [class.mem] forbids static data members with the
8244 same name as the enclosing class. Non-static data
8245 members are checked in check_field_decls. */
8246 if (constructor_name_p (declarator, current_class_type))
8247 pedwarn ("ISO C++ forbids static data member `%D' with same name as enclosing class",
8248 declarator);
8249
f18a14bc
MM
8250 /* C++ allows static class members. All other work
8251 for this is done by grokfield. */
4ce3d537 8252 decl = build_lang_decl (VAR_DECL, declarator, type);
5b605f68
MS
8253 TREE_STATIC (decl) = 1;
8254 /* In class context, 'static' means public access. */
3ac3d9ea 8255 TREE_PUBLIC (decl) = DECL_EXTERNAL (decl) = 1;
8d08fdba
MS
8256 }
8257 else
8258 {
721c3b42 8259 decl = build_decl (FIELD_DECL, declarator, type);
2bf105ab 8260 DECL_NONADDRESSABLE_P (decl) = bitfield;
8d08fdba
MS
8261 if (RIDBIT_SETP (RID_MUTABLE, specbits))
8262 {
8263 DECL_MUTABLE_P (decl) = 1;
8264 RIDBIT_RESET (RID_MUTABLE, specbits);
8265 }
8266 }
8267
8268 bad_specifiers (decl, "field", virtualp, quals != NULL_TREE,
8269 inlinep, friendp, raises != NULL_TREE);
8270 }
8271 }
fd9aef9d
NS
8272 else if (TREE_CODE (type) == FUNCTION_TYPE
8273 || TREE_CODE (type) == METHOD_TYPE)
8d08fdba 8274 {
386b8a85 8275 tree original_name;
8d08fdba
MS
8276 int publicp = 0;
8277
8278 if (! declarator)
8279 return NULL_TREE;
8280
386b8a85
JM
8281 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
8282 original_name = dname;
8283 else
8284 original_name = declarator;
8285
8926095f 8286 if (RIDBIT_SETP (RID_AUTO, specbits))
8251199e 8287 error ("storage class `auto' invalid for function `%s'", name);
8926095f 8288 else if (RIDBIT_SETP (RID_REGISTER, specbits))
8251199e 8289 error ("storage class `register' invalid for function `%s'", name);
7a1f3f5f
RH
8290 else if (RIDBIT_SETP (RID_THREAD, specbits))
8291 error ("storage class `__thread' invalid for function `%s'", name);
8d08fdba
MS
8292
8293 /* Function declaration not at top level.
8294 Storage classes other than `extern' are not allowed
8295 and `extern' makes no difference. */
a9aedbc2 8296 if (! toplevel_bindings_p ()
8926095f
MS
8297 && (RIDBIT_SETP (RID_STATIC, specbits)
8298 || RIDBIT_SETP (RID_INLINE, specbits))
8d08fdba 8299 && pedantic)
8926095f
MS
8300 {
8301 if (RIDBIT_SETP (RID_STATIC, specbits))
8251199e 8302 pedwarn ("storage class `static' invalid for function `%s' declared out of global scope", name);
8926095f 8303 else
8251199e 8304 pedwarn ("storage class `inline' invalid for function `%s' declared out of global scope", name);
8926095f 8305 }
68642fb6 8306
8d08fdba
MS
8307 if (ctype == NULL_TREE)
8308 {
8309 if (virtualp)
8310 {
8251199e 8311 error ("virtual non-class function `%s'", name);
8d08fdba
MS
8312 virtualp = 0;
8313 }
8d08fdba
MS
8314 }
8315 else if (TREE_CODE (type) == FUNCTION_TYPE && staticp < 2)
43dc123f
MM
8316 type = build_method_type_directly (ctype,
8317 TREE_TYPE (type),
8318 TYPE_ARG_TYPES (type));
8d08fdba 8319
eb66be0e 8320 /* Record presence of `static'. */
faae18ab 8321 publicp = (ctype != NULL_TREE
a9aedbc2 8322 || RIDBIT_SETP (RID_EXTERN, specbits)
eb66be0e 8323 || !RIDBIT_SETP (RID_STATIC, specbits));
8d08fdba 8324
386b8a85 8325 decl = grokfndecl (ctype, type, original_name, declarator,
7a8f9fa9 8326 virtualp, flags, quals, raises,
75650646 8327 1, friendp,
68642fb6 8328 publicp, inlinep, funcdef_flag,
2c73f9f5 8329 template_count, in_namespace);
f0e01782
MS
8330 if (decl == NULL_TREE)
8331 return NULL_TREE;
8d08fdba 8332
8d08fdba
MS
8333 if (staticp == 1)
8334 {
0e339752 8335 int invalid_static = 0;
8d08fdba
MS
8336
8337 /* Don't allow a static member function in a class, and forbid
8338 declaring main to be static. */
8339 if (TREE_CODE (type) == METHOD_TYPE)
8340 {
33bd39a2 8341 pedwarn ("cannot declare member function `%D' to have static linkage", decl);
0e339752 8342 invalid_static = 1;
8d08fdba 8343 }
8d08fdba
MS
8344 else if (current_function_decl)
8345 {
8346 /* FIXME need arm citation */
8251199e 8347 error ("cannot declare static function inside another function");
0e339752 8348 invalid_static = 1;
8d08fdba
MS
8349 }
8350
0e339752 8351 if (invalid_static)
8d08fdba
MS
8352 {
8353 staticp = 0;
8354 RIDBIT_RESET (RID_STATIC, specbits);
8355 }
8356 }
8d08fdba
MS
8357 }
8358 else
8359 {
8360 /* It's a variable. */
8361
8362 /* An uninitialized decl with `extern' is a reference. */
68642fb6
UD
8363 decl = grokvardecl (type, declarator, &specbits,
8364 initialized,
8365 (type_quals & TYPE_QUAL_CONST) != 0,
9e259dd1 8366 ctype ? ctype : in_namespace);
8d08fdba
MS
8367 bad_specifiers (decl, "variable", virtualp, quals != NULL_TREE,
8368 inlinep, friendp, raises != NULL_TREE);
8369
8370 if (ctype)
8371 {
f0e01782 8372 DECL_CONTEXT (decl) = ctype;
8d08fdba
MS
8373 if (staticp == 1)
8374 {
33bd39a2 8375 pedwarn ("`static' may not be used when defining (as opposed to declaring) a static data member");
8d08fdba
MS
8376 staticp = 0;
8377 RIDBIT_RESET (RID_STATIC, specbits);
8378 }
b7484fbe
MS
8379 if (RIDBIT_SETP (RID_REGISTER, specbits) && TREE_STATIC (decl))
8380 {
33bd39a2 8381 error ("static member `%D' declared `register'", decl);
b7484fbe
MS
8382 RIDBIT_RESET (RID_REGISTER, specbits);
8383 }
f30432d7 8384 if (RIDBIT_SETP (RID_EXTERN, specbits) && pedantic)
8d08fdba 8385 {
33bd39a2 8386 pedwarn ("cannot explicitly declare member `%#D' to have extern linkage",
f30432d7 8387 decl);
8d08fdba
MS
8388 RIDBIT_RESET (RID_EXTERN, specbits);
8389 }
8390 }
8391 }
8392
34fc957d 8393 my_friendly_assert (!RIDBIT_SETP (RID_MUTABLE, specbits), 19990927);
8d08fdba
MS
8394
8395 /* Record `register' declaration for warnings on &
8396 and in case doing stupid register allocation. */
8397
8398 if (RIDBIT_SETP (RID_REGISTER, specbits))
8399 DECL_REGISTER (decl) = 1;
8400
8926095f
MS
8401 if (RIDBIT_SETP (RID_EXTERN, specbits))
8402 DECL_THIS_EXTERN (decl) = 1;
8403
faae18ab
MS
8404 if (RIDBIT_SETP (RID_STATIC, specbits))
8405 DECL_THIS_STATIC (decl) = 1;
8406
adecb3f4
MM
8407 /* Record constancy and volatility. There's no need to do this
8408 when processing a template; we'll do this for the instantiated
8409 declaration based on the type of DECL. */
8410 if (!processing_template_decl)
8411 c_apply_type_quals_to_decl (type_quals, decl);
8d08fdba
MS
8412
8413 return decl;
8414 }
8415}
8416\f
f181d4ae
MM
8417/* Subroutine of start_function. Ensure that each of the parameter
8418 types (as listed in PARMS) is complete, as is required for a
8419 function definition. */
e92cc029 8420
8d08fdba 8421static void
11f6b451 8422require_complete_types_for_parms (tree parms)
8d08fdba 8423{
07c88314 8424 for (; parms; parms = TREE_CHAIN (parms))
8d08fdba 8425 {
5cce22b6
NS
8426 if (VOID_TYPE_P (TREE_TYPE (parms)))
8427 /* grokparms will have already issued an error */
8428 TREE_TYPE (parms) = error_mark_node;
8429 else if (complete_type_or_else (TREE_TYPE (parms), parms))
753225c1
JM
8430 {
8431 layout_decl (parms, 0);
8432 DECL_ARG_TYPE (parms) = type_passed_as (TREE_TYPE (parms));
8433 }
5cce22b6
NS
8434 else
8435 TREE_TYPE (parms) = error_mark_node;
8d08fdba
MS
8436 }
8437}
8438
838dfd8a 8439/* Returns nonzero if T is a local variable. */
297e73d8 8440
46e8c075 8441int
11f6b451 8442local_variable_p (tree t)
297e73d8 8443{
68642fb6 8444 if ((TREE_CODE (t) == VAR_DECL
297e73d8
MM
8445 /* A VAR_DECL with a context that is a _TYPE is a static data
8446 member. */
8447 && !TYPE_P (CP_DECL_CONTEXT (t))
8448 /* Any other non-local variable must be at namespace scope. */
46e8c075 8449 && !DECL_NAMESPACE_SCOPE_P (t))
297e73d8 8450 || (TREE_CODE (t) == PARM_DECL))
46e8c075 8451 return 1;
297e73d8 8452
46e8c075
MM
8453 return 0;
8454}
8455
838dfd8a 8456/* Returns nonzero if T is an automatic local variable or a label.
46e8c075
MM
8457 (These are the declarations that need to be remapped when the code
8458 containing them is duplicated.) */
8459
8460int
11f6b451 8461nonstatic_local_decl_p (tree t)
46e8c075
MM
8462{
8463 return ((local_variable_p (t) && !TREE_STATIC (t))
8464 || TREE_CODE (t) == LABEL_DECL
8465 || TREE_CODE (t) == RESULT_DECL);
8466}
8467
8468/* Like local_variable_p, but suitable for use as a tree-walking
8469 function. */
8470
8471static tree
11f6b451
NN
8472local_variable_p_walkfn (tree* tp,
8473 int* walk_subtrees ATTRIBUTE_UNUSED ,
8474 void* data ATTRIBUTE_UNUSED )
46e8c075 8475{
68642fb6 8476 return ((local_variable_p (*tp) && !DECL_ARTIFICIAL (*tp))
46e8c075 8477 ? *tp : NULL_TREE);
297e73d8
MM
8478}
8479
8480/* Check that ARG, which is a default-argument expression for a
0e339752 8481 parameter DECL, is valid. Returns ARG, or ERROR_MARK_NODE, if
297e73d8
MM
8482 something goes wrong. DECL may also be a _TYPE node, rather than a
8483 DECL, if there is no DECL available. */
8484
8485tree
11f6b451 8486check_default_argument (tree decl, tree arg)
297e73d8
MM
8487{
8488 tree var;
8489 tree decl_type;
8490
8491 if (TREE_CODE (arg) == DEFAULT_ARG)
8492 /* We get a DEFAULT_ARG when looking at an in-class declaration
8493 with a default argument. Ignore the argument for now; we'll
8494 deal with it after the class is complete. */
8495 return arg;
8496
8497 if (processing_template_decl || uses_template_parms (arg))
8498 /* We don't do anything checking until instantiation-time. Note
8499 that there may be uninstantiated arguments even for an
8500 instantiated function, since default arguments are not
8501 instantiated until they are needed. */
8502 return arg;
8503
8504 if (TYPE_P (decl))
8505 {
8506 decl_type = decl;
8507 decl = NULL_TREE;
8508 }
8509 else
8510 decl_type = TREE_TYPE (decl);
8511
68642fb6 8512 if (arg == error_mark_node
297e73d8
MM
8513 || decl == error_mark_node
8514 || TREE_TYPE (arg) == error_mark_node
8515 || decl_type == error_mark_node)
8516 /* Something already went wrong. There's no need to check
8517 further. */
8518 return error_mark_node;
8519
8520 /* [dcl.fct.default]
68642fb6 8521
297e73d8
MM
8522 A default argument expression is implicitly converted to the
8523 parameter type. */
8524 if (!TREE_TYPE (arg)
8525 || !can_convert_arg (decl_type, TREE_TYPE (arg), arg))
8526 {
8527 if (decl)
33bd39a2 8528 error ("default argument for `%#D' has type `%T'",
297e73d8
MM
8529 decl, TREE_TYPE (arg));
8530 else
33bd39a2 8531 error ("default argument for parameter of type `%T' has type `%T'",
297e73d8
MM
8532 decl_type, TREE_TYPE (arg));
8533
8534 return error_mark_node;
8535 }
8536
8537 /* [dcl.fct.default]
8538
8539 Local variables shall not be used in default argument
68642fb6 8540 expressions.
297e73d8
MM
8541
8542 The keyword `this' shall not be used in a default argument of a
8543 member function. */
5362b086 8544 var = walk_tree_without_duplicates (&arg, local_variable_p_walkfn,
ee94fce6 8545 NULL);
297e73d8
MM
8546 if (var)
8547 {
33bd39a2 8548 error ("default argument `%E' uses local variable `%D'",
297e73d8
MM
8549 arg, var);
8550 return error_mark_node;
8551 }
8552
8553 /* All is well. */
8554 return arg;
8555}
8556
8d08fdba
MS
8557/* Decode the list of parameter types for a function type.
8558 Given the list of things declared inside the parens,
8559 return a list of types.
8560
5cce22b6
NS
8561 We determine whether ellipsis parms are used by PARMLIST_ELLIPSIS_P
8562 flag. If unset, we append void_list_node. A parmlist declared
8563 as `(void)' is accepted as the empty parmlist.
8564
8565 Also set last_function_parms to the chain of PARM_DECLs. */
8d08fdba
MS
8566
8567static tree
11f6b451 8568grokparms (tree first_parm)
8d08fdba
MS
8569{
8570 tree result = NULL_TREE;
8571 tree decls = NULL_TREE;
5cce22b6
NS
8572 int ellipsis = !first_parm || PARMLIST_ELLIPSIS_P (first_parm);
8573 tree parm, chain;
8574 int any_error = 0;
8d08fdba 8575
5cce22b6
NS
8576 my_friendly_assert (!first_parm || TREE_PARMLIST (first_parm), 20001115);
8577
8578 for (parm = first_parm; parm != NULL_TREE; parm = chain)
8d08fdba 8579 {
0657c69c 8580 tree type = NULL_TREE;
98884b26 8581 tree decl = TREE_VALUE (parm);
5cce22b6 8582 tree init = TREE_PURPOSE (parm);
98884b26 8583 tree specs, attrs;
8d08fdba 8584
5cce22b6
NS
8585 chain = TREE_CHAIN (parm);
8586 /* @@ weak defense against parse errors. */
8587 if (TREE_CODE (decl) != VOID_TYPE
8588 && TREE_CODE (decl) != TREE_LIST)
8d08fdba 8589 {
5cce22b6
NS
8590 /* Give various messages as the need arises. */
8591 if (TREE_CODE (decl) == STRING_CST)
33bd39a2 8592 error ("invalid string constant `%E'", decl);
5cce22b6
NS
8593 else if (TREE_CODE (decl) == INTEGER_CST)
8594 error ("invalid integer constant in parameter list, did you forget to give parameter name?");
8595 continue;
8596 }
8d08fdba 8597
5cce22b6
NS
8598 if (parm == void_list_node)
8599 break;
8d08fdba 8600
98884b26
JM
8601 split_specs_attrs (TREE_PURPOSE (decl), &specs, &attrs);
8602 decl = grokdeclarator (TREE_VALUE (decl), specs,
8603 PARM, init != NULL_TREE, &attrs);
5cce22b6
NS
8604 if (! decl || TREE_TYPE (decl) == error_mark_node)
8605 continue;
d363e7bf 8606
98884b26
JM
8607 if (attrs)
8608 cplus_decl_attributes (&decl, attrs, 0);
8609
5cce22b6
NS
8610 type = TREE_TYPE (decl);
8611 if (VOID_TYPE_P (type))
8612 {
8613 if (same_type_p (type, void_type_node)
8614 && !DECL_NAME (decl) && !result && !chain && !ellipsis)
8615 /* this is a parmlist of `(void)', which is ok. */
8616 break;
7a228918 8617 cxx_incomplete_type_error (decl, type);
0657c69c
MM
8618 /* It's not a good idea to actually create parameters of
8619 type `void'; other parts of the compiler assume that a
8620 void type terminates the parameter list. */
04f3dc2b 8621 type = error_mark_node;
0657c69c 8622 TREE_TYPE (decl) = error_mark_node;
5cce22b6 8623 }
8d08fdba 8624
d363e7bf 8625 if (type != error_mark_node)
04f3dc2b
MM
8626 {
8627 /* Top-level qualifiers on the parameters are
8628 ignored for function types. */
79a1a736 8629 type = cp_build_qualified_type (type, 0);
04f3dc2b
MM
8630 if (TREE_CODE (type) == METHOD_TYPE)
8631 {
33bd39a2 8632 error ("parameter `%D' invalidly declared method type", decl);
04f3dc2b
MM
8633 type = build_pointer_type (type);
8634 TREE_TYPE (decl) = type;
8635 }
04f3dc2b 8636 else if (abstract_virtuals_error (decl, type))
a1c65f9f 8637 any_error = 1; /* Seems like a good idea. */
04f3dc2b
MM
8638 else if (POINTER_TYPE_P (type))
8639 {
8640 /* [dcl.fct]/6, parameter types cannot contain pointers
8641 (references) to arrays of unknown bound. */
98979fe0
NS
8642 tree t = TREE_TYPE (type);
8643 int ptr = TYPE_PTR_P (type);
8644
8645 while (1)
8646 {
8647 if (TYPE_PTR_P (t))
8648 ptr = 1;
8649 else if (TREE_CODE (t) != ARRAY_TYPE)
8650 break;
8651 else if (!TYPE_DOMAIN (t))
8652 break;
8653 t = TREE_TYPE (t);
8654 }
04f3dc2b 8655 if (TREE_CODE (t) == ARRAY_TYPE)
33bd39a2 8656 error ("parameter `%D' includes %s to array of unknown bound `%T'",
98979fe0 8657 decl, ptr ? "pointer" : "reference", t);
04f3dc2b
MM
8658 }
8659
04f3dc2b
MM
8660 if (!any_error && init)
8661 init = check_default_argument (decl, init);
8662 else
8663 init = NULL_TREE;
8664 }
8d08fdba 8665
5cce22b6
NS
8666 TREE_CHAIN (decl) = decls;
8667 decls = decl;
0657c69c 8668 result = tree_cons (init, type, result);
8d08fdba 8669 }
5cce22b6
NS
8670 decls = nreverse (decls);
8671 result = nreverse (result);
8672 if (!ellipsis)
8673 result = chainon (result, void_list_node);
8d08fdba
MS
8674 last_function_parms = decls;
8675
8d08fdba
MS
8676 return result;
8677}
42976354 8678
8d08fdba 8679\f
271e6f02
NS
8680/* D is a constructor or overloaded `operator='.
8681
8682 Let T be the class in which D is declared. Then, this function
8683 returns:
8684
8685 -1 if D's is an ill-formed constructor or copy assignment operator
8686 whose first parameter is of type `T'.
8687 0 if D is not a copy constructor or copy assignment
8688 operator.
8689 1 if D is a copy constructor or copy assignment operator whose
8690 first parameter is a reference to const qualified T.
8691 2 if D is a copy constructor or copy assignment operator whose
8692 first parameter is a reference to non-const qualified T.
8693
8694 This function can be used as a predicate. Positive values indicate
838dfd8a 8695 a copy constructor and nonzero values indicate a copy assignment
4f1c5b7d
MM
8696 operator. */
8697
c11b6f21 8698int
11f6b451 8699copy_fn_p (tree d)
c11b6f21 8700{
271e6f02
NS
8701 tree args;
8702 tree arg_type;
8703 int result = 1;
8704
8705 my_friendly_assert (DECL_FUNCTION_MEMBER_P (d), 20011208);
4f1c5b7d 8706
271e6f02
NS
8707 if (DECL_TEMPLATE_INFO (d) && is_member_template (DECL_TI_TEMPLATE (d)))
8708 /* Instantiations of template member functions are never copy
8709 functions. Note that member functions of templated classes are
8710 represented as template functions internally, and we must
8711 accept those as copy functions. */
8712 return 0;
8713
8714 args = FUNCTION_FIRST_USER_PARMTYPE (d);
8715 if (!args)
4f1c5b7d
MM
8716 return 0;
8717
271e6f02
NS
8718 arg_type = TREE_VALUE (args);
8719
8720 if (TYPE_MAIN_VARIANT (arg_type) == DECL_CONTEXT (d))
8721 {
8722 /* Pass by value copy assignment operator. */
8723 result = -1;
8724 }
8725 else if (TREE_CODE (arg_type) == REFERENCE_TYPE
8726 && TYPE_MAIN_VARIANT (TREE_TYPE (arg_type)) == DECL_CONTEXT (d))
8727 {
8728 if (CP_TYPE_CONST_P (TREE_TYPE (arg_type)))
8729 result = 2;
8730 }
8731 else
8732 return 0;
8733
8734 args = TREE_CHAIN (args);
8735
8736 if (args && args != void_list_node && !TREE_PURPOSE (args))
8737 /* There are more non-optional args. */
8738 return 0;
8739
8740 return result;
8741}
8742
8743/* Remember any special properties of member function DECL. */
8744
11f6b451 8745void grok_special_member_properties (tree decl)
271e6f02
NS
8746{
8747 if (!DECL_NONSTATIC_MEMBER_FUNCTION_P(decl))
8748 ; /* Not special. */
8749 else if (DECL_CONSTRUCTOR_P (decl))
8750 {
8751 int ctor = copy_fn_p (decl);
8752
8753 if (ctor > 0)
8754 {
8755 /* [class.copy]
8756
8757 A non-template constructor for class X is a copy
8758 constructor if its first parameter is of type X&, const
8759 X&, volatile X& or const volatile X&, and either there
8760 are no other parameters or else all other parameters have
8761 default arguments. */
8762 TYPE_HAS_INIT_REF (DECL_CONTEXT (decl)) = 1;
8763 if (ctor > 1)
8764 TYPE_HAS_CONST_INIT_REF (DECL_CONTEXT (decl)) = 1;
8765 }
8766 else if (sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (decl)))
8767 TYPE_HAS_DEFAULT_CONSTRUCTOR (DECL_CONTEXT (decl)) = 1;
8768 }
8769 else if (DECL_OVERLOADED_OPERATOR_P (decl) == NOP_EXPR)
8770 {
8771 /* [class.copy]
8772
8773 A non-template assignment operator for class X is a copy
8774 assignment operator if its parameter is of type X, X&, const
8775 X&, volatile X& or const volatile X&. */
8776
8777 int assop = copy_fn_p (decl);
8778
8779 if (assop)
8780 {
8781 TYPE_HAS_ASSIGN_REF (DECL_CONTEXT (decl)) = 1;
8782 if (assop != 1)
8783 TYPE_HAS_CONST_ASSIGN_REF (DECL_CONTEXT (decl)) = 1;
8784 if (DECL_PURE_VIRTUAL_P (decl))
8785 TYPE_HAS_ABSTRACT_ASSIGN_REF (DECL_CONTEXT (decl)) = 1;
8786 }
8787 }
c11b6f21
MS
8788}
8789
271e6f02
NS
8790/* Check a constructor DECL has the correct form. Complains
8791 if the class has a constructor of the form X(X). */
e92cc029 8792
a0a33927 8793int
11f6b451 8794grok_ctor_properties (tree ctype, tree decl)
8d08fdba 8795{
271e6f02
NS
8796 int ctor_parm = copy_fn_p (decl);
8797
8798 if (ctor_parm < 0)
8799 {
8800 /* [class.copy]
8801
8802 A declaration of a constructor for a class X is ill-formed if
8803 its first parameter is of type (optionally cv-qualified) X
8804 and either there are no other parameters or else all other
8805 parameters have default arguments.
8806
8807 We *don't* complain about member template instantiations that
8808 have this form, though; they can occur as we try to decide
8809 what constructor to use during overload resolution. Since
8810 overload resolution will never prefer such a constructor to
8811 the non-template copy constructor (which is either explicitly
8812 or implicitly defined), there's no need to worry about their
8813 existence. Theoretically, they should never even be
8814 instantiated, but that's hard to forestall. */
33bd39a2 8815 error ("invalid constructor; you probably meant `%T (const %T&)'",
0b41abe6
JM
8816 ctype, ctype);
8817 SET_IDENTIFIER_ERROR_LOCUS (DECL_NAME (decl), ctype);
8818 return 0;
8d08fdba 8819 }
271e6f02 8820
a0a33927 8821 return 1;
8d08fdba
MS
8822}
8823
596ea4e5 8824/* An operator with this code is unary, but can also be binary. */
e92cc029 8825
a28e3c7f 8826static int
11f6b451 8827ambi_op_p (enum tree_code code)
8d08fdba 8828{
596ea4e5
AS
8829 return (code == INDIRECT_REF
8830 || code == ADDR_EXPR
8831 || code == CONVERT_EXPR
8832 || code == NEGATE_EXPR
8833 || code == PREINCREMENT_EXPR
8834 || code == PREDECREMENT_EXPR);
8d08fdba
MS
8835}
8836
8837/* An operator with this name can only be unary. */
e92cc029 8838
a28e3c7f 8839static int
11f6b451 8840unary_op_p (enum tree_code code)
8d08fdba 8841{
596ea4e5
AS
8842 return (code == TRUTH_NOT_EXPR
8843 || code == BIT_NOT_EXPR
8844 || code == COMPONENT_REF
8845 || code == TYPE_EXPR);
8d08fdba
MS
8846}
8847
4b0d3cbe
MM
8848/* DECL is a declaration for an overloaded operator. Returns true if
8849 the declaration is valid; false otherwise. If COMPLAIN is true,
8850 errors are issued for invalid declarations. */
e92cc029 8851
4b0d3cbe
MM
8852bool
8853grok_op_properties (tree decl, int friendp, bool complain)
8d08fdba
MS
8854{
8855 tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
596ea4e5 8856 tree argtype;
8d08fdba
MS
8857 int methodp = (TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE);
8858 tree name = DECL_NAME (decl);
596ea4e5
AS
8859 enum tree_code operator_code;
8860 int arity;
4b0d3cbe
MM
8861 bool ok;
8862
8863 /* Assume that the declaration is valid. */
8864 ok = true;
596ea4e5
AS
8865
8866 /* Count the number of arguments. */
8867 for (argtype = argtypes, arity = 0;
8868 argtype && argtype != void_list_node;
8869 argtype = TREE_CHAIN (argtype))
8870 ++arity;
8d08fdba 8871
a28e3c7f
MS
8872 if (current_class_type == NULL_TREE)
8873 friendp = 1;
8d08fdba 8874
596ea4e5
AS
8875 if (DECL_CONV_FN_P (decl))
8876 operator_code = TYPE_EXPR;
8877 else
8878 do
8879 {
0c918ce5
MM
8880#define DEF_OPERATOR(NAME, CODE, MANGLING, ARITY, ASSN_P) \
8881 if (ansi_opname (CODE) == name) \
8882 { \
75ac8dec 8883 operator_code = (CODE); \
0c918ce5
MM
8884 break; \
8885 } \
8886 else if (ansi_assopname (CODE) == name) \
8887 { \
75ac8dec 8888 operator_code = (CODE); \
0c918ce5
MM
8889 DECL_ASSIGNMENT_OPERATOR_P (decl) = 1; \
8890 break; \
596ea4e5
AS
8891 }
8892
8893#include "operators.def"
8894#undef DEF_OPERATOR
8895
a98facb0 8896 abort ();
596ea4e5
AS
8897 }
8898 while (0);
8899 my_friendly_assert (operator_code != LAST_CPLUS_TREE_CODE, 20000526);
8900 SET_OVERLOADED_OPERATOR_CODE (decl, operator_code);
8901
a28e3c7f
MS
8902 if (! friendp)
8903 {
596ea4e5
AS
8904 switch (operator_code)
8905 {
596ea4e5
AS
8906 case NEW_EXPR:
8907 TYPE_HAS_NEW_OPERATOR (current_class_type) = 1;
8908 break;
5362b086 8909
596ea4e5
AS
8910 case DELETE_EXPR:
8911 TYPE_GETS_DELETE (current_class_type) |= 1;
8912 break;
5362b086 8913
596ea4e5
AS
8914 case VEC_NEW_EXPR:
8915 TYPE_HAS_ARRAY_NEW_OPERATOR (current_class_type) = 1;
8916 break;
5362b086 8917
596ea4e5
AS
8918 case VEC_DELETE_EXPR:
8919 TYPE_GETS_DELETE (current_class_type) |= 2;
8920 break;
8921
8922 default:
8923 break;
8924 }
8925 }
8926
8927 if (operator_code == NEW_EXPR || operator_code == VEC_NEW_EXPR)
8d08fdba 8928 {
8d08fdba
MS
8929 /* When the compiler encounters the definition of A::operator new, it
8930 doesn't look at the class declaration to find out if it's static. */
a28e3c7f 8931 if (methodp)
3afb32a4 8932 revert_static_member_fn (decl);
68642fb6 8933
36791f1e 8934 TREE_TYPE (decl) = coerce_new_type (TREE_TYPE (decl));
8d08fdba 8935 }
596ea4e5 8936 else if (operator_code == DELETE_EXPR || operator_code == VEC_DELETE_EXPR)
8d08fdba 8937 {
a28e3c7f 8938 if (methodp)
3afb32a4 8939 revert_static_member_fn (decl);
68642fb6 8940
36791f1e 8941 TREE_TYPE (decl) = coerce_delete_type (TREE_TYPE (decl));
8d08fdba 8942 }
8d08fdba
MS
8943 else
8944 {
8945 /* An operator function must either be a non-static member function
8946 or have at least one parameter of a class, a reference to a class,
8947 an enumeration, or a reference to an enumeration. 13.4.0.6 */
700f8a87 8948 if (! methodp || DECL_STATIC_FUNCTION_P (decl))
8d08fdba 8949 {
596ea4e5
AS
8950 if (operator_code == TYPE_EXPR
8951 || operator_code == CALL_EXPR
8952 || operator_code == COMPONENT_REF
8953 || operator_code == ARRAY_REF
8954 || operator_code == NOP_EXPR)
33bd39a2 8955 error ("`%D' must be a nonstatic member function", decl);
8d08fdba
MS
8956 else
8957 {
4b0d3cbe 8958 tree p;
8d08fdba 8959
700f8a87 8960 if (DECL_STATIC_FUNCTION_P (decl))
33bd39a2 8961 error ("`%D' must be either a non-static member function or a non-member function", decl);
700f8a87 8962
4b0d3cbe
MM
8963 for (p = argtypes; p && p != void_list_node; p = TREE_CHAIN (p))
8964 {
8965 tree arg = non_reference (TREE_VALUE (p));
8966 /* IS_AGGR_TYPE, rather than CLASS_TYPE_P, is used
8967 because these checks are performed even on
8968 template functions. */
8969 if (IS_AGGR_TYPE (arg) || TREE_CODE (arg) == ENUMERAL_TYPE)
8970 break;
8971 }
8972
8973 if (!p || p == void_list_node)
8974 {
8975 if (!complain)
8976 return false;
8977
8978 error ("`%D' must have an argument of class or "
8979 "enumerated type",
8980 decl);
8981 ok = false;
8982 }
8d08fdba
MS
8983 }
8984 }
68642fb6 8985
4b0d3cbe
MM
8986 /* There are no restrictions on the arguments to an overloaded
8987 "operator ()". */
596ea4e5 8988 if (operator_code == CALL_EXPR)
4b0d3cbe 8989 return ok;
8d08fdba 8990
9a3b49ac 8991 if (IDENTIFIER_TYPENAME_P (name) && ! DECL_TEMPLATE_INFO (decl))
a0a33927
MS
8992 {
8993 tree t = TREE_TYPE (name);
17708e90 8994 if (! friendp)
a0a33927
MS
8995 {
8996 int ref = (TREE_CODE (t) == REFERENCE_TYPE);
d8e178a0 8997 const char *what = 0;
5362b086 8998
a0a33927
MS
8999 if (ref)
9000 t = TYPE_MAIN_VARIANT (TREE_TYPE (t));
9001
17708e90
NS
9002 if (TREE_CODE (t) == VOID_TYPE)
9003 what = "void";
9004 else if (t == current_class_type)
a0a33927 9005 what = "the same type";
9a3b49ac 9006 /* Don't force t to be complete here. */
a0a33927 9007 else if (IS_AGGR_TYPE (t)
d0f062fb 9008 && COMPLETE_TYPE_P (t)
a0a33927
MS
9009 && DERIVED_FROM_P (t, current_class_type))
9010 what = "a base class";
9011
dfbeb061 9012 if (what && warn_conversion)
8251199e 9013 warning ("conversion to %s%s will never use a type conversion operator",
a0a33927
MS
9014 ref ? "a reference to " : "", what);
9015 }
9016 }
271e6f02 9017 if (operator_code == COND_EXPR)
8d08fdba
MS
9018 {
9019 /* 13.4.0.3 */
33bd39a2 9020 error ("ISO C++ prohibits overloading operator ?:");
68642fb6 9021 }
596ea4e5 9022 else if (ambi_op_p (operator_code))
8d08fdba 9023 {
596ea4e5
AS
9024 if (arity == 1)
9025 /* We pick the one-argument operator codes by default, so
9026 we don't have to change anything. */
9027 ;
9028 else if (arity == 2)
8d08fdba 9029 {
596ea4e5
AS
9030 /* If we thought this was a unary operator, we now know
9031 it to be a binary operator. */
9032 switch (operator_code)
9033 {
9034 case INDIRECT_REF:
9035 operator_code = MULT_EXPR;
9036 break;
9037
9038 case ADDR_EXPR:
9039 operator_code = BIT_AND_EXPR;
9040 break;
9041
9042 case CONVERT_EXPR:
9043 operator_code = PLUS_EXPR;
9044 break;
9045
9046 case NEGATE_EXPR:
9047 operator_code = MINUS_EXPR;
9048 break;
9049
9050 case PREINCREMENT_EXPR:
9051 operator_code = POSTINCREMENT_EXPR;
9052 break;
9053
9054 case PREDECREMENT_EXPR:
655dc6ee 9055 operator_code = POSTDECREMENT_EXPR;
596ea4e5
AS
9056 break;
9057
9058 default:
a98facb0 9059 abort ();
596ea4e5
AS
9060 }
9061
9062 SET_OVERLOADED_OPERATOR_CODE (decl, operator_code);
9063
9064 if ((operator_code == POSTINCREMENT_EXPR
9065 || operator_code == POSTDECREMENT_EXPR)
5156628f 9066 && ! processing_template_decl
007e5fea 9067 && ! same_type_p (TREE_VALUE (TREE_CHAIN (argtypes)), integer_type_node))
8d08fdba
MS
9068 {
9069 if (methodp)
33bd39a2 9070 error ("postfix `%D' must take `int' as its argument",
8d08fdba
MS
9071 decl);
9072 else
33bd39a2 9073 error
8251199e
JM
9074 ("postfix `%D' must take `int' as its second argument",
9075 decl);
8d08fdba
MS
9076 }
9077 }
9078 else
9079 {
9080 if (methodp)
33bd39a2 9081 error ("`%D' must take either zero or one argument", decl);
8d08fdba 9082 else
33bd39a2 9083 error ("`%D' must take either one or two arguments", decl);
8d08fdba 9084 }
824b9a4c
MS
9085
9086 /* More Effective C++ rule 6. */
eb448459 9087 if (warn_ecpp
596ea4e5
AS
9088 && (operator_code == POSTINCREMENT_EXPR
9089 || operator_code == POSTDECREMENT_EXPR
9090 || operator_code == PREINCREMENT_EXPR
9091 || operator_code == PREDECREMENT_EXPR))
824b9a4c
MS
9092 {
9093 tree arg = TREE_VALUE (argtypes);
9094 tree ret = TREE_TYPE (TREE_TYPE (decl));
9095 if (methodp || TREE_CODE (arg) == REFERENCE_TYPE)
9096 arg = TREE_TYPE (arg);
9097 arg = TYPE_MAIN_VARIANT (arg);
596ea4e5
AS
9098 if (operator_code == PREINCREMENT_EXPR
9099 || operator_code == PREDECREMENT_EXPR)
824b9a4c
MS
9100 {
9101 if (TREE_CODE (ret) != REFERENCE_TYPE
3bfdc719
MM
9102 || !same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (ret)),
9103 arg))
33bd39a2 9104 warning ("prefix `%D' should return `%T'", decl,
824b9a4c
MS
9105 build_reference_type (arg));
9106 }
9107 else
9108 {
3bfdc719 9109 if (!same_type_p (TYPE_MAIN_VARIANT (ret), arg))
33bd39a2 9110 warning ("postfix `%D' should return `%T'", decl, arg);
824b9a4c
MS
9111 }
9112 }
8d08fdba 9113 }
596ea4e5 9114 else if (unary_op_p (operator_code))
8d08fdba 9115 {
596ea4e5 9116 if (arity != 1)
8d08fdba
MS
9117 {
9118 if (methodp)
33bd39a2 9119 error ("`%D' must take `void'", decl);
8d08fdba 9120 else
33bd39a2 9121 error ("`%D' must take exactly one argument", decl);
8d08fdba
MS
9122 }
9123 }
596ea4e5 9124 else /* if (binary_op_p (operator_code)) */
8d08fdba 9125 {
596ea4e5 9126 if (arity != 2)
8d08fdba
MS
9127 {
9128 if (methodp)
33bd39a2 9129 error ("`%D' must take exactly one argument", decl);
8d08fdba 9130 else
33bd39a2 9131 error ("`%D' must take exactly two arguments", decl);
8d08fdba 9132 }
824b9a4c
MS
9133
9134 /* More Effective C++ rule 7. */
eb448459 9135 if (warn_ecpp
596ea4e5
AS
9136 && (operator_code == TRUTH_ANDIF_EXPR
9137 || operator_code == TRUTH_ORIF_EXPR
9138 || operator_code == COMPOUND_EXPR))
33bd39a2 9139 warning ("user-defined `%D' always evaluates both arguments",
824b9a4c
MS
9140 decl);
9141 }
9142
9143 /* Effective C++ rule 23. */
eb448459 9144 if (warn_ecpp
596ea4e5 9145 && arity == 2
4bd7c270 9146 && !DECL_ASSIGNMENT_OPERATOR_P (decl)
596ea4e5
AS
9147 && (operator_code == PLUS_EXPR
9148 || operator_code == MINUS_EXPR
9149 || operator_code == TRUNC_DIV_EXPR
4bd7c270
NS
9150 || operator_code == MULT_EXPR
9151 || operator_code == TRUNC_MOD_EXPR)
824b9a4c 9152 && TREE_CODE (TREE_TYPE (TREE_TYPE (decl))) == REFERENCE_TYPE)
33bd39a2 9153 warning ("`%D' should return by value", decl);
8d08fdba 9154
271e6f02 9155 /* [over.oper]/8 */
34332678
CT
9156 for (; argtypes && argtypes != void_list_node;
9157 argtypes = TREE_CHAIN (argtypes))
9158 if (TREE_PURPOSE (argtypes))
9159 {
9160 TREE_PURPOSE (argtypes) = NULL_TREE;
596ea4e5
AS
9161 if (operator_code == POSTINCREMENT_EXPR
9162 || operator_code == POSTDECREMENT_EXPR)
34332678
CT
9163 {
9164 if (pedantic)
33bd39a2 9165 pedwarn ("`%D' cannot have default arguments", decl);
34332678
CT
9166 }
9167 else
33bd39a2 9168 error ("`%D' cannot have default arguments", decl);
34332678
CT
9169 }
9170
8d08fdba 9171 }
4b0d3cbe
MM
9172
9173 return ok;
8d08fdba
MS
9174}
9175\f
d8e178a0 9176static const char *
11f6b451 9177tag_name (enum tag_types code)
094fe153
JM
9178{
9179 switch (code)
9180 {
9181 case record_type:
9182 return "struct";
9183 case class_type:
9184 return "class";
9185 case union_type:
9186 return "union ";
9187 case enum_type:
9188 return "enum";
094fe153 9189 default:
a98facb0 9190 abort ();
094fe153
JM
9191 }
9192}
9193
befcd99b 9194/* Name lookup in an elaborated-type-specifier (after the keyword
4b0d3cbe 9195 indicated by TAG_CODE) has found the TYPE_DECL DECL. If the
befcd99b 9196 elaborated-type-specifier is invalid, issue a diagnostic and return
4b0d3cbe 9197 error_mark_node; otherwise, return the *_TYPE to which it referred.
cbd63935 9198 If ALLOW_TEMPLATE_P is true, TYPE may be a class template. */
befcd99b 9199
560ad596 9200tree
befcd99b 9201check_elaborated_type_specifier (enum tag_types tag_code,
4b0d3cbe 9202 tree decl,
cbd63935 9203 bool allow_template_p)
befcd99b 9204{
4b0d3cbe 9205 tree type;
befcd99b 9206
4b0d3cbe
MM
9207 /* In the case of:
9208
9209 struct S { struct S *p; };
9210
9211 name lookup will find the TYPE_DECL for the implicit "S::S"
9212 typedef. Adjust for that here. */
9213 if (DECL_SELF_REFERENCE_P (decl))
9214 decl = TYPE_NAME (TREE_TYPE (decl));
9215
9216 type = TREE_TYPE (decl);
9217
9218 /* [dcl.type.elab]
9219
9220 If the identifier resolves to a typedef-name or a template
9221 type-parameter, the elaborated-type-specifier is ill-formed.
9222
9223 In other words, the only legitimate declaration to use in the
9224 elaborated type specifier is the implicit typedef created when
9225 the type is declared. */
9226 if (!DECL_IMPLICIT_TYPEDEF_P (decl))
befcd99b 9227 {
4b0d3cbe
MM
9228 error ("using typedef-name `%D' after `%s'", decl, tag_name (tag_code));
9229 return IS_AGGR_TYPE (type) ? type : error_mark_node;
befcd99b 9230 }
4b0d3cbe
MM
9231
9232 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
befcd99b
MM
9233 {
9234 error ("using template type parameter `%T' after `%s'",
9235 type, tag_name (tag_code));
4b0d3cbe 9236 return error_mark_node;
befcd99b 9237 }
cbd63935
KL
9238 else if (TREE_CODE (type) != RECORD_TYPE
9239 && TREE_CODE (type) != UNION_TYPE
9240 && tag_code != enum_type)
9241 {
9242 error ("`%T' referred to as `%s'", type, tag_name (tag_code));
4b0d3cbe 9243 return error_mark_node;
cbd63935
KL
9244 }
9245 else if (TREE_CODE (type) != ENUMERAL_TYPE
9246 && tag_code == enum_type)
9247 {
9248 error ("`%T' referred to as enum", type);
4b0d3cbe 9249 return error_mark_node;
cbd63935
KL
9250 }
9251 else if (!allow_template_p
9252 && TREE_CODE (type) == RECORD_TYPE
9253 && CLASSTYPE_IS_TEMPLATE (type))
9254 {
9255 /* If a class template appears as elaborated type specifier
9256 without a template header such as:
9257
9258 template <class T> class C {};
9259 void f(class C); // No template header here
9260
9261 then the required template argument is missing. */
9262
9263 error ("template argument required for `%s %T'",
9264 tag_name (tag_code),
9265 DECL_NAME (CLASSTYPE_TI_TEMPLATE (type)));
4b0d3cbe 9266 return error_mark_node;
cbd63935 9267 }
befcd99b 9268
4b0d3cbe 9269 return type;
befcd99b
MM
9270}
9271
cbd63935 9272/* Get the struct, enum or union (TAG_CODE says which) with tag NAME.
8d08fdba
MS
9273 Define the tag as a forward-reference if it is not defined.
9274
cbd63935
KL
9275 If a declaration is given, process it here, and report an error if
9276 multiple declarations are not identical. ATTRIBUTE is the attribute
9277 appeared in this declaration.
8d08fdba 9278
cbd63935 9279 GLOBALIZE is false when this is also a definition. Only look in
8d08fdba 9280 the current frame for the name (since C++ allows new names in any
cbd63935
KL
9281 scope.)
9282
9283 TEMPLATE_HEADER_P is true when this declaration is preceded by
9284 a set of template parameters. */
8d08fdba 9285
8d08fdba 9286tree
88e5899c 9287xref_tag (enum tag_types tag_code, tree name, tree attributes,
cbd63935 9288 bool globalize, bool template_header_p)
8d08fdba 9289{
8d08fdba 9290 enum tree_code code;
cbd63935 9291 register tree t;
e2500fed 9292 struct cp_binding_level *b = current_binding_level;
25aab5d0 9293 tree context = NULL_TREE;
dc8263bc 9294
22ffcc6f 9295 timevar_push (TV_NAME_LOOKUP);
cbd63935
KL
9296
9297 my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, 0);
9298
8d08fdba
MS
9299 switch (tag_code)
9300 {
9301 case record_type:
9302 case class_type:
8d08fdba 9303 code = RECORD_TYPE;
8d08fdba
MS
9304 break;
9305 case union_type:
9306 code = UNION_TYPE;
8d08fdba
MS
9307 break;
9308 case enum_type:
9309 code = ENUMERAL_TYPE;
9310 break;
9311 default:
a98facb0 9312 abort ();
8d08fdba
MS
9313 }
9314
8ccc31eb 9315 if (! globalize)
8d08fdba 9316 {
f3400fe2
JM
9317 /* If we know we are defining this tag, only look it up in
9318 this scope and don't try to find it as a type. */
cbd63935 9319 t = lookup_tag (code, name, b, 1);
8d08fdba
MS
9320 }
9321 else
9322 {
cbd63935
KL
9323 tree decl = lookup_name (name, 1);
9324
9325 if (decl && DECL_CLASS_TEMPLATE_P (decl))
9326 decl = DECL_TEMPLATE_RESULT (decl);
68642fb6 9327
cbd63935 9328 if (decl && TREE_CODE (decl) == TYPE_DECL)
25aab5d0 9329 {
cbd63935
KL
9330 /* Two cases we need to consider when deciding if a class
9331 template is allowed as an elaborated type specifier:
9332 1. It is a self reference to its own class.
9333 2. It comes with a template header.
25aab5d0 9334
cbd63935 9335 For example:
6757edfe 9336
cbd63935
KL
9337 template <class T> class C {
9338 class C *c1; // DECL_SELF_REFERENCE_P is true
9339 class D;
9340 };
9341 template <class U> class C; // template_header_p is true
9342 template <class T> class C<T>::D {
9343 class C *c2; // DECL_SELF_REFERENCE_P is true
9344 }; */
9345
4b0d3cbe
MM
9346 t = check_elaborated_type_specifier (tag_code,
9347 decl,
cbd63935
KL
9348 template_header_p
9349 | DECL_SELF_REFERENCE_P (decl));
9350 if (t == error_mark_node)
9351 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
25aab5d0 9352 }
cbd63935
KL
9353 else
9354 t = NULL_TREE;
25aab5d0 9355
cbd63935 9356 if (t && current_class_type
68642fb6 9357 && template_class_depth (current_class_type)
cbd63935 9358 && template_header_p)
25aab5d0 9359 {
838dfd8a 9360 /* Since GLOBALIZE is nonzero, we are not looking at a
25aab5d0
MM
9361 definition of this tag. Since, in addition, we are currently
9362 processing a (member) template declaration of a template
9363 class, we must be very careful; consider:
9364
9365 template <class X>
9366 struct S1
9367
9368 template <class U>
9369 struct S2
9370 { template <class V>
9371 friend struct S1; };
9372
9373 Here, the S2::S1 declaration should not be confused with the
9374 outer declaration. In particular, the inner version should
9375 have a template parameter of level 2, not level 1. This
9376 would be particularly important if the member declaration
9377 were instead:
9378
9379 template <class V = U> friend struct S1;
9380
9381 say, when we should tsubst into `U' when instantiating
9382 S2. On the other hand, when presented with:
9383
9384 template <class T>
9385 struct S1 {
9386 template <class U>
9387 struct S2 {};
9388 template <class U>
9389 friend struct S2;
9390 };
9391
9392 we must find the inner binding eventually. We
9393 accomplish this by making sure that the new type we
9394 create to represent this declaration has the right
9395 TYPE_CONTEXT. */
cbd63935
KL
9396 context = TYPE_CONTEXT (t);
9397 t = NULL_TREE;
8d08fdba
MS
9398 }
9399 }
9400
cbd63935 9401 if (! t)
8d08fdba
MS
9402 {
9403 /* If no such tag is yet defined, create a forward-reference node
9404 and record it as the "definition".
9405 When a real declaration of this type is found,
9406 the forward-reference will be altered into a real type. */
8d08fdba
MS
9407 if (code == ENUMERAL_TYPE)
9408 {
33bd39a2 9409 error ("use of enum `%#D' without previous declaration", name);
fc378698 9410
cbd63935 9411 t = make_node (ENUMERAL_TYPE);
8d08fdba
MS
9412
9413 /* Give the type a default layout like unsigned int
9414 to avoid crashing if it does not get defined. */
cbd63935
KL
9415 TYPE_MODE (t) = TYPE_MODE (unsigned_type_node);
9416 TYPE_ALIGN (t) = TYPE_ALIGN (unsigned_type_node);
9417 TYPE_USER_ALIGN (t) = 0;
9418 TREE_UNSIGNED (t) = 1;
9419 TYPE_PRECISION (t) = TYPE_PRECISION (unsigned_type_node);
9420 TYPE_MIN_VALUE (t) = TYPE_MIN_VALUE (unsigned_type_node);
9421 TYPE_MAX_VALUE (t) = TYPE_MAX_VALUE (unsigned_type_node);
8d08fdba
MS
9422
9423 /* Enable us to recognize when a type is created in class context.
9424 To do nested classes correctly, this should probably be cleared
9425 out when we leave this classes scope. Currently this in only
9426 done in `start_enum'. */
9427
cbd63935 9428 pushtag (name, t, globalize);
8d08fdba 9429 }
8d08fdba
MS
9430 else
9431 {
cbd63935
KL
9432 t = make_aggr_type (code);
9433 TYPE_CONTEXT (t) = context;
9434 pushtag (name, t, globalize);
8d08fdba
MS
9435 }
9436 }
9437 else
9438 {
cbd63935
KL
9439 if (!globalize && processing_template_decl && IS_AGGR_TYPE (t))
9440 redeclare_class_template (t, current_template_parms);
8d08fdba
MS
9441 }
9442
cbd63935 9443 TYPE_ATTRIBUTES (t) = attributes;
dc8263bc 9444
cbd63935 9445 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
8d08fdba 9446}
8ccc31eb 9447
fc378698 9448tree
11f6b451 9449xref_tag_from_type (tree old, tree id, int globalize)
fc378698 9450{
88e5899c 9451 enum tag_types tag_kind;
fc378698
MS
9452
9453 if (TREE_CODE (old) == RECORD_TYPE)
88e5899c 9454 tag_kind = (CLASSTYPE_DECLARED_CLASS (old) ? class_type : record_type);
fc378698 9455 else
88e5899c 9456 tag_kind = union_type;
fc378698
MS
9457
9458 if (id == NULL_TREE)
9459 id = TYPE_IDENTIFIER (old);
9460
cbd63935 9461 return xref_tag (tag_kind, id, /*attributes=*/NULL_TREE, globalize, false);
fc378698
MS
9462}
9463
3fd71a52 9464/* REF is a type (named NAME), for which we have just seen some
dbbf88d1 9465 baseclasses. BASE_LIST is a list of those baseclasses; the
3fd71a52 9466 TREE_PURPOSE is an access_* node, and the TREE_VALUE is the type of
dbbf88d1
NS
9467 the base-class. TREE_VIA_VIRTUAL indicates virtual
9468 inheritance. CODE_TYPE_NODE indicates whether REF is a class,
3fd71a52
MM
9469 struct, or union. */
9470
8ccc31eb 9471void
dbbf88d1 9472xref_basetypes (tree ref, tree base_list)
8ccc31eb
MS
9473{
9474 /* In the declaration `A : X, Y, ... Z' we mark all the types
9475 (A, X, Y, ..., Z) so we can check for duplicates. */
8fbc5ae7 9476 tree *basep;
d6479fe7 9477
8fbc5ae7 9478 int i;
591382c4 9479 enum tag_types tag_code;
a5d7c4a3 9480
bef89e9e
KL
9481 if (ref == error_mark_node)
9482 return;
9483
da15dae6 9484 if (TREE_CODE (ref) == UNION_TYPE)
8ccc31eb 9485 {
33bd39a2 9486 error ("derived union `%T' invalid", ref);
8ccc31eb
MS
9487 return;
9488 }
9489
da15dae6
MM
9490 tag_code = (CLASSTYPE_DECLARED_CLASS (ref) ? class_type : record_type);
9491
d6479fe7
MM
9492 /* First, make sure that any templates in base-classes are
9493 instantiated. This ensures that if we call ourselves recursively
9494 we do not get confused about which classes are marked and which
9495 are not. */
dbbf88d1 9496 basep = &base_list;
8fbc5ae7
MM
9497 while (*basep)
9498 {
9499 tree basetype = TREE_VALUE (*basep);
9500 if (!(processing_template_decl && uses_template_parms (basetype))
9501 && !complete_type_or_else (basetype, NULL))
dbbf88d1 9502 /* An incomplete type. Remove it from the list. */
8fbc5ae7
MM
9503 *basep = TREE_CHAIN (*basep);
9504 else
9505 basep = &TREE_CHAIN (*basep);
9506 }
d6479fe7 9507
8ccc31eb 9508 SET_CLASSTYPE_MARKED (ref);
dbbf88d1
NS
9509 i = list_length (base_list);
9510 if (i)
9511 {
9512 tree binfo = TYPE_BINFO (ref);
9513 tree binfos = make_tree_vec (i);
9514 tree accesses = make_tree_vec (i);
9515
9516 BINFO_BASETYPES (binfo) = binfos;
9517 BINFO_BASEACCESSES (binfo) = accesses;
9518
9519 for (i = 0; base_list; base_list = TREE_CHAIN (base_list))
8ccc31eb 9520 {
dbbf88d1
NS
9521 tree access = TREE_PURPOSE (base_list);
9522 int via_virtual = TREE_VIA_VIRTUAL (base_list);
9523 tree basetype = TREE_VALUE (base_list);
9524 tree base_binfo;
9525
9526 if (access == access_default_node)
9527 /* The base of a derived struct is public by default. */
9528 access = (tag_code == class_type
9529 ? access_private_node : access_public_node);
9530
9531 if (basetype && TREE_CODE (basetype) == TYPE_DECL)
9532 basetype = TREE_TYPE (basetype);
9533 if (!basetype
9534 || (TREE_CODE (basetype) != RECORD_TYPE
9535 && TREE_CODE (basetype) != TYPENAME_TYPE
9536 && TREE_CODE (basetype) != TEMPLATE_TYPE_PARM
9537 && TREE_CODE (basetype) != BOUND_TEMPLATE_TEMPLATE_PARM))
9538 {
9539 error ("base type `%T' fails to be a struct or class type",
9540 basetype);
9541 continue;
9542 }
9543
9544 if (CLASSTYPE_MARKED (basetype))
9545 {
9546 if (basetype == ref)
9547 error ("recursive type `%T' undefined", basetype);
9548 else
9549 error ("duplicate base type `%T' invalid", basetype);
9550 continue;
9551 }
9552
9553 if (TYPE_FOR_JAVA (basetype)
9554 && (current_lang_depth () == 0))
9555 TYPE_FOR_JAVA (ref) = 1;
9556
9557 if (CLASS_TYPE_P (basetype))
9558 {
9559 base_binfo = TYPE_BINFO (basetype);
9560 /* This flag will be in the binfo of the base type, we must
9561 clear it after copying the base binfos. */
9562 BINFO_DEPENDENT_BASE_P (base_binfo)
9563 = dependent_type_p (basetype);
9564 }
8fbc5ae7 9565 else
dbbf88d1
NS
9566 base_binfo = make_binfo (size_zero_node, basetype,
9567 NULL_TREE, NULL_TREE);
9568
9569 TREE_VEC_ELT (binfos, i) = base_binfo;
9570 TREE_VEC_ELT (accesses, i) = access;
9571 /* This flag will be in the binfo of the base type, we must
9572 clear it after copying the base binfos. */
9573 TREE_VIA_VIRTUAL (base_binfo) = via_virtual;
9574
9575 SET_CLASSTYPE_MARKED (basetype);
9576
9577 /* We are free to modify these bits because they are meaningless
9578 at top level, and BASETYPE is a top-level type. */
9579 if (via_virtual || TYPE_USES_VIRTUAL_BASECLASSES (basetype))
9580 {
9581 TYPE_USES_VIRTUAL_BASECLASSES (ref) = 1;
9582 /* Converting to a virtual base class requires looking
9583 up the offset of the virtual base. */
9584 TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (ref) = 1;
9585 }
9586
9587 if (CLASS_TYPE_P (basetype))
9588 {
9589 TYPE_HAS_NEW_OPERATOR (ref)
9590 |= TYPE_HAS_NEW_OPERATOR (basetype);
9591 TYPE_HAS_ARRAY_NEW_OPERATOR (ref)
9592 |= TYPE_HAS_ARRAY_NEW_OPERATOR (basetype);
9593 TYPE_GETS_DELETE (ref) |= TYPE_GETS_DELETE (basetype);
9594 /* If the base-class uses multiple inheritance, so do we. */
9595 TYPE_USES_MULTIPLE_INHERITANCE (ref)
9596 |= TYPE_USES_MULTIPLE_INHERITANCE (basetype);
9597 /* Likewise, if converting to a base of the base may require
9598 code, then we may need to generate code to convert to a
9599 base as well. */
9600 TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (ref)
9601 |= TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (basetype);
9602 }
9603 i++;
8ccc31eb 9604 }
dbbf88d1
NS
9605 if (i)
9606 TREE_VEC_LENGTH (accesses) = TREE_VEC_LENGTH (binfos) = i;
9607 else
9608 BINFO_BASEACCESSES (binfo) = BINFO_BASETYPES (binfo) = NULL_TREE;
9609
9610 if (i > 1)
9611 {
9612 TYPE_USES_MULTIPLE_INHERITANCE (ref) = 1;
9613 /* If there is more than one non-empty they cannot be at the same
9614 address. */
8fbc5ae7
MM
9615 TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (ref) = 1;
9616 }
dbbf88d1
NS
9617 }
9618
9619 /* Copy the base binfos, collect the virtual bases and set the
9620 inheritance order chain. */
9621 copy_base_binfos (TYPE_BINFO (ref), ref, NULL_TREE);
9622 CLASSTYPE_VBASECLASSES (ref) = nreverse (CLASSTYPE_VBASECLASSES (ref));
9623
9624 /* Unmark all the types. */
9625 while (i--)
9626 {
9627 tree basetype = BINFO_TYPE (BINFO_BASETYPE (TYPE_BINFO (ref), i));
9628
9629 CLEAR_CLASSTYPE_MARKED (basetype);
8fbc5ae7
MM
9630 if (CLASS_TYPE_P (basetype))
9631 {
dbbf88d1
NS
9632 TREE_VIA_VIRTUAL (TYPE_BINFO (basetype)) = 0;
9633 BINFO_DEPENDENT_BASE_P (TYPE_BINFO (basetype)) = 0;
8fbc5ae7 9634 }
7ddedda4 9635 }
8ccc31eb 9636 CLEAR_CLASSTYPE_MARKED (ref);
8ccc31eb 9637}
68642fb6 9638
8d08fdba 9639\f
8d08fdba
MS
9640/* Begin compiling the definition of an enumeration type.
9641 NAME is its name (or null if anonymous).
9642 Returns the type object, as yet incomplete.
9643 Also records info about it so that build_enumerator
9644 may be used to declare the individual values as they are read. */
9645
9646tree
11f6b451 9647start_enum (tree name)
8d08fdba
MS
9648{
9649 register tree enumtype = NULL_TREE;
e2500fed 9650 struct cp_binding_level *b = current_binding_level;
8d08fdba
MS
9651
9652 /* If this is the real definition for a previous forward reference,
9653 fill in the contents in the same object that used to be the
9654 forward reference. */
9655
9656 if (name != NULL_TREE)
9657 enumtype = lookup_tag (ENUMERAL_TYPE, name, b, 1);
9658
9659 if (enumtype != NULL_TREE && TREE_CODE (enumtype) == ENUMERAL_TYPE)
30ff8252 9660 {
33bd39a2 9661 error ("multiple definition of `%#T'", enumtype);
ddd2d57e 9662 error ("%Jprevious definition here", TYPE_MAIN_DECL (enumtype));
58595203
MM
9663 /* Clear out TYPE_VALUES, and start again. */
9664 TYPE_VALUES (enumtype) = NULL_TREE;
30ff8252 9665 }
8d08fdba
MS
9666 else
9667 {
9668 enumtype = make_node (ENUMERAL_TYPE);
9669 pushtag (name, enumtype, 0);
9670 }
9671
8d08fdba
MS
9672 return enumtype;
9673}
9674
9675/* After processing and defining all the values of an enumeration type,
9676 install their decls in the enumeration type and finish it off.
968b956a 9677 ENUMTYPE is the type object and VALUES a list of name-value pairs. */
8d08fdba 9678
968b956a 9679void
11f6b451 9680finish_enum (tree enumtype)
8d08fdba 9681{
7b6d72fc
MM
9682 tree values;
9683 tree decl;
9684 tree value;
968b956a
MM
9685 tree minnode;
9686 tree maxnode;
9687 tree t;
9688 bool unsignedp;
9689 int lowprec;
9690 int highprec;
9691 int precision;
7b6d72fc 9692 integer_type_kind itk;
ad96995b 9693 tree underlying_type = NULL_TREE;
968b956a
MM
9694
9695 /* We built up the VALUES in reverse order. */
9696 TYPE_VALUES (enumtype) = nreverse (TYPE_VALUES (enumtype));
9697
f5d70cc0
JM
9698 /* For an enum defined in a template, just set the type of the values;
9699 all further processing is postponed until the template is
9700 instantiated. We need to set the type so that tsubst of a CONST_DECL
9701 works. */
968b956a
MM
9702 if (processing_template_decl)
9703 {
7b6d72fc
MM
9704 for (values = TYPE_VALUES (enumtype);
9705 values;
9706 values = TREE_CHAIN (values))
9707 TREE_TYPE (TREE_VALUE (values)) = enumtype;
5f261ba9 9708 if (at_function_scope_p ())
968b956a 9709 add_stmt (build_min (TAG_DEFN, enumtype));
968b956a
MM
9710 return;
9711 }
9712
7b6d72fc 9713 /* Determine the minimum and maximum values of the enumerators. */
968b956a 9714 if (TYPE_VALUES (enumtype))
8d08fdba 9715 {
968b956a 9716 minnode = maxnode = NULL_TREE;
5566b478 9717
7b6d72fc
MM
9718 for (values = TYPE_VALUES (enumtype);
9719 values;
9720 values = TREE_CHAIN (values))
8d08fdba 9721 {
7b6d72fc 9722 decl = TREE_VALUE (values);
f5d70cc0
JM
9723
9724 /* [dcl.enum]: Following the closing brace of an enum-specifier,
9725 each enumerator has the type of its enumeration. Prior to the
9726 closing brace, the type of each enumerator is the type of its
9727 initializing value. */
9728 TREE_TYPE (decl) = enumtype;
9729
7b6d72fc
MM
9730 /* Update the minimum and maximum values, if appropriate. */
9731 value = DECL_INITIAL (decl);
f5d70cc0
JM
9732 /* Figure out what the minimum and maximum values of the
9733 enumerators are. */
9734 if (!minnode)
9735 minnode = maxnode = value;
9736 else if (tree_int_cst_lt (maxnode, value))
9737 maxnode = value;
9738 else if (tree_int_cst_lt (value, minnode))
9739 minnode = value;
9740
9741 /* Set the TREE_TYPE for the values as well. That's so that when
9742 we call decl_constant_value we get an entity of the right type
9743 (but with the constant value). But first make a copy so we
9744 don't clobber shared INTEGER_CSTs. */
9745 if (TREE_TYPE (value) != enumtype)
fdedded1 9746 {
f5d70cc0
JM
9747 value = DECL_INITIAL (decl) = copy_node (value);
9748 TREE_TYPE (value) = enumtype;
fdedded1 9749 }
8d08fdba
MS
9750 }
9751 }
f376e137 9752 else
7b6d72fc
MM
9753 /* [dcl.enum]
9754
9755 If the enumerator-list is empty, the underlying type is as if
9756 the enumeration had a single enumerator with value 0. */
968b956a
MM
9757 minnode = maxnode = integer_zero_node;
9758
9759 /* Compute the number of bits require to represent all values of the
9760 enumeration. We must do this before the type of MINNODE and
9761 MAXNODE are transformed, since min_precision relies on the
9762 TREE_TYPE of the value it is passed. */
9763 unsignedp = tree_int_cst_sgn (minnode) >= 0;
9764 lowprec = min_precision (minnode, unsignedp);
9765 highprec = min_precision (maxnode, unsignedp);
9766 precision = MAX (lowprec, highprec);
9767
7b6d72fc
MM
9768 /* Determine the underlying type of the enumeration.
9769
9770 [dcl.enum]
9771
9772 The underlying type of an enumeration is an integral type that
9773 can represent all the enumerator values defined in the
9774 enumeration. It is implementation-defined which integral type is
9775 used as the underlying type for an enumeration except that the
9776 underlying type shall not be larger than int unless the value of
9777 an enumerator cannot fit in an int or unsigned int.
9778
9779 We use "int" or an "unsigned int" as the underlying type, even if
9780 a smaller integral type would work, unless the user has
9781 explicitly requested that we use the smallest possible type. */
9782 for (itk = (flag_short_enums ? itk_char : itk_int);
9783 itk != itk_none;
9784 itk++)
e455bc27 9785 {
7b6d72fc
MM
9786 underlying_type = integer_types[itk];
9787 if (TYPE_PRECISION (underlying_type) >= precision
9788 && TREE_UNSIGNED (underlying_type) == unsignedp)
9789 break;
9790 }
9791 if (itk == itk_none)
9792 {
9793 /* DR 377
9794
9795 IF no integral type can represent all the enumerator values, the
9796 enumeration is ill-formed. */
e455bc27
MM
9797 error ("no integral type can represent all of the enumerator values "
9798 "for `%T'", enumtype);
9799 precision = TYPE_PRECISION (long_long_integer_type_node);
7b6d72fc 9800 underlying_type = integer_types[itk_unsigned_long_long];
e455bc27
MM
9801 }
9802
7b6d72fc 9803 /* Compute the minium and maximum values for the type.
8d08fdba 9804
7b6d72fc
MM
9805 [dcl.enum]
9806
9807 For an enumeration where emin is the smallest enumerator and emax
9808 is the largest, the values of the enumeration are the values of the
9809 underlying type in the range bmin to bmax, where bmin and bmax are,
9810 respectively, the smallest and largest values of the smallest bit-
9811 field that can store emin and emax. */
9812 TYPE_PRECISION (enumtype) = precision;
9813 set_min_and_max_values_for_integral_type (enumtype, precision, unsignedp);
9814
9815 /* [dcl.enum]
9816
9817 The value of sizeof() applied to an enumeration type, an object
9818 of an enumeration type, or an enumerator, is the value of sizeof()
9819 applied to the underlying type. */
9820 TYPE_SIZE (enumtype) = TYPE_SIZE (underlying_type);
9821 TYPE_SIZE_UNIT (enumtype) = TYPE_SIZE_UNIT (underlying_type);
9822 TYPE_MODE (enumtype) = TYPE_MODE (underlying_type);
9823 TYPE_ALIGN (enumtype) = TYPE_ALIGN (underlying_type);
9824 TYPE_USER_ALIGN (enumtype) = TYPE_USER_ALIGN (underlying_type);
9825 TREE_UNSIGNED (enumtype) = TREE_UNSIGNED (underlying_type);
9826
9827 /* Convert each of the enumerators to the type of the underlying
9828 type of the enumeration. */
9829 for (values = TYPE_VALUES (enumtype); values; values = TREE_CHAIN (values))
9830 {
9831 decl = TREE_VALUE (values);
9832 value = perform_implicit_conversion (underlying_type,
9833 DECL_INITIAL (decl));
9834 TREE_TYPE (value) = enumtype;
9835 DECL_INITIAL (decl) = value;
9836 TREE_VALUE (values) = value;
e455bc27 9837 }
8d08fdba 9838
968b956a
MM
9839 /* Fix up all variant types of this enum type. */
9840 for (t = TYPE_MAIN_VARIANT (enumtype); t; t = TYPE_NEXT_VARIANT (t))
9841 {
9842 TYPE_VALUES (t) = TYPE_VALUES (enumtype);
9843 TYPE_MIN_VALUE (t) = TYPE_MIN_VALUE (enumtype);
9844 TYPE_MAX_VALUE (t) = TYPE_MAX_VALUE (enumtype);
9845 TYPE_SIZE (t) = TYPE_SIZE (enumtype);
9846 TYPE_SIZE_UNIT (t) = TYPE_SIZE_UNIT (enumtype);
9847 TYPE_MODE (t) = TYPE_MODE (enumtype);
9848 TYPE_PRECISION (t) = TYPE_PRECISION (enumtype);
9849 TYPE_ALIGN (t) = TYPE_ALIGN (enumtype);
9850 TYPE_USER_ALIGN (t) = TYPE_USER_ALIGN (enumtype);
9851 TREE_UNSIGNED (t) = TREE_UNSIGNED (enumtype);
cbf882af
MM
9852 }
9853
968b956a
MM
9854 /* Finish debugging output for this type. */
9855 rest_of_type_compilation (enumtype, namespace_bindings_p ());
8d08fdba
MS
9856}
9857
079e1098 9858/* Build and install a CONST_DECL for an enumeration constant of the
58595203 9859 enumeration type ENUMTYPE whose NAME and VALUE (if any) are provided.
8d08fdba
MS
9860 Assignment of sequential values by default is handled here. */
9861
58595203 9862void
11f6b451 9863build_enumerator (tree name, tree value, tree enumtype)
8d08fdba 9864{
58595203 9865 tree decl;
e8bd800e 9866 tree context;
58595203 9867 tree type;
8d08fdba
MS
9868
9869 /* Remove no-op casts from the value. */
9870 if (value)
9871 STRIP_TYPE_NOPS (value);
9872
58595203
MM
9873 if (! processing_template_decl)
9874 {
9875 /* Validate and default VALUE. */
9876 if (value != NULL_TREE)
9877 {
fc611ce0 9878 value = decl_constant_value (value);
58595203
MM
9879
9880 if (TREE_CODE (value) == INTEGER_CST)
9881 {
0a72704b 9882 value = perform_integral_promotions (value);
58595203
MM
9883 constant_expression_warning (value);
9884 }
9885 else
9886 {
33bd39a2 9887 error ("enumerator value for `%D' not integer constant", name);
58595203
MM
9888 value = NULL_TREE;
9889 }
9890 }
9891
9892 /* Default based on previous value. */
6a540f3c 9893 if (value == NULL_TREE)
58595203
MM
9894 {
9895 tree prev_value;
9896
9897 if (TYPE_VALUES (enumtype))
9898 {
a1c65f9f 9899 /* The next value is the previous value ... */
58595203
MM
9900 prev_value = DECL_INITIAL (TREE_VALUE (TYPE_VALUES (enumtype)));
9901 /* ... plus one. */
ab76ca54
MM
9902 value = cp_build_binary_op (PLUS_EXPR,
9903 prev_value,
9904 integer_one_node);
68642fb6 9905
58595203 9906 if (tree_int_cst_lt (value, prev_value))
33bd39a2 9907 error ("overflow in enumeration values at `%D'", name);
58595203
MM
9908 }
9909 else
9910 value = integer_zero_node;
9911 }
9912
9913 /* Remove no-op casts from the value. */
f5d70cc0 9914 STRIP_TYPE_NOPS (value);
58595203 9915 }
8d08fdba 9916
8d08fdba 9917 /* C++ associates enums with global, function, or class declarations. */
58595203 9918 context = current_scope ();
7b6d72fc
MM
9919 if (!context)
9920 context = current_namespace;
58595203
MM
9921
9922 /* Build the actual enumeration constant. Note that the enumeration
9923 constants have the type of their initializers until the
9924 enumeration is complete:
9925
9926 [ dcl.enum ]
9927
9928 Following the closing brace of an enum-specifier, each enumer-
9929 ator has the type of its enumeration. Prior to the closing
9930 brace, the type of each enumerator is the type of its
9931 initializing value.
9932
9933 In finish_enum we will reset the type. Of course, if we're
a1c65f9f 9934 processing a template, there may be no value. */
58595203
MM
9935 type = value ? TREE_TYPE (value) : NULL_TREE;
9936
9937 if (context && context == current_class_type)
9938 /* This enum declaration is local to the class. We need the full
8f17b5c5 9939 lang_decl so that we can record DECL_CLASS_CONTEXT, for example. */
58595203
MM
9940 decl = build_lang_decl (CONST_DECL, name, type);
9941 else
9942 /* It's a global enum, or it's local to a function. (Note local to
e8bd800e 9943 a function could mean local to a class method. */
58595203 9944 decl = build_decl (CONST_DECL, name, type);
e8bd800e 9945
58595203 9946 DECL_CONTEXT (decl) = FROB_CONTEXT (context);
3e4a3562 9947 TREE_CONSTANT (decl) = TREE_READONLY (decl) = 1;
58595203 9948 DECL_INITIAL (decl) = value;
e8bd800e 9949
58595203
MM
9950 if (context && context == current_class_type)
9951 /* In something like `struct S { enum E { i = 7 }; };' we put `i'
7b6d72fc
MM
9952 on the TYPE_FIELDS list for `S'. (That's so that you can say
9953 things like `S::i' later.) */
58595203
MM
9954 finish_member_declaration (decl);
9955 else
9780c24f 9956 pushdecl (decl);
58595203
MM
9957
9958 /* Add this enumeration constant to the list for this type. */
9959 TYPE_VALUES (enumtype) = tree_cons (name, decl, TYPE_VALUES (enumtype));
8d08fdba
MS
9960}
9961
8d08fdba 9962\f
a8f73d4b
MM
9963/* We're defining DECL. Make sure that it's type is OK. */
9964
9965static void
11f6b451 9966check_function_type (tree decl, tree current_function_parms)
a8f73d4b
MM
9967{
9968 tree fntype = TREE_TYPE (decl);
d0f062fb 9969 tree return_type = complete_type (TREE_TYPE (fntype));
a8f73d4b
MM
9970
9971 /* In a function definition, arg types must be complete. */
9972 require_complete_types_for_parms (current_function_parms);
9973
d0f062fb 9974 if (!COMPLETE_OR_VOID_TYPE_P (return_type))
a8f73d4b 9975 {
33bd39a2 9976 error ("return type `%#T' is incomplete", TREE_TYPE (fntype));
a8f73d4b
MM
9977
9978 /* Make it return void instead, but don't change the
9979 type of the DECL_RESULT, in case we have a named return value. */
9980 if (TREE_CODE (fntype) == METHOD_TYPE)
9981 {
9982 tree ctype = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (fntype)));
9983 TREE_TYPE (decl)
43dc123f
MM
9984 = build_method_type_directly (ctype,
9985 void_type_node,
9986 FUNCTION_ARG_CHAIN (decl));
a8f73d4b
MM
9987 }
9988 else
9989 TREE_TYPE (decl)
9990 = build_function_type (void_type_node,
9991 TYPE_ARG_TYPES (TREE_TYPE (decl)));
68642fb6 9992 TREE_TYPE (decl)
a8f73d4b
MM
9993 = build_exception_variant (fntype,
9994 TYPE_RAISES_EXCEPTIONS (fntype));
9995 }
9996 else
9997 abstract_virtuals_error (decl, TREE_TYPE (fntype));
9998}
9999
8d08fdba
MS
10000/* Create the FUNCTION_DECL for a function definition.
10001 DECLSPECS and DECLARATOR are the parts of the declaration;
10002 they describe the function's name and the type it returns,
10003 but twisted together in a fashion that parallels the syntax of C.
10004
a8f73d4b
MM
10005 FLAGS is a bitwise or of SF_PRE_PARSED (indicating that the
10006 DECLARATOR is really the DECL for the function we are about to
10007 process and that DECLSPECS should be ignored), SF_INCLASS_INLINE
f444e36b 10008 indicating that the function is an inline defined in-class.
68642fb6 10009
8d08fdba
MS
10010 This function creates a binding context for the function body
10011 as well as setting up the FUNCTION_DECL in current_function_decl.
10012
10013 Returns 1 on success. If the DECLARATOR is not suitable for a function
10014 (it defines a datum instead), we return 0, which tells
10015 yyparse to report a parse error.
10016
10017 For C++, we must first check whether that datum makes any sense.
10018 For example, "class A local_a(1,2);" means that variable local_a
10019 is an aggregate of type A, which should have a constructor
87e3dbc9 10020 applied to it with the argument list [1, 2]. */
8d08fdba
MS
10021
10022int
11f6b451 10023start_function (tree declspecs, tree declarator, tree attrs, int flags)
8d08fdba 10024{
5566b478 10025 tree decl1;
8d08fdba
MS
10026 tree ctype = NULL_TREE;
10027 tree fntype;
10028 tree restype;
8d08fdba 10029 int doing_friend = 0;
e2500fed 10030 struct cp_binding_level *bl;
f444e36b 10031 tree current_function_parms;
8d08fdba 10032
8d08fdba 10033 /* Sanity check. */
a1774733 10034 my_friendly_assert (TREE_CODE (TREE_VALUE (void_list_node)) == VOID_TYPE, 160);
8d08fdba
MS
10035 my_friendly_assert (TREE_CHAIN (void_list_node) == NULL_TREE, 161);
10036
e92cc029 10037 /* This should only be done once on the top most decl. */
594bb0e7 10038 if (have_extern_spec)
8d08fdba 10039 {
1f8f4a0b 10040 declspecs = tree_cons (NULL_TREE, get_identifier ("extern"), declspecs);
594bb0e7 10041 have_extern_spec = false;
8d08fdba
MS
10042 }
10043
a8f73d4b 10044 if (flags & SF_PRE_PARSED)
8d08fdba
MS
10045 {
10046 decl1 = declarator;
10047
8d08fdba
MS
10048 fntype = TREE_TYPE (decl1);
10049 if (TREE_CODE (fntype) == METHOD_TYPE)
10050 ctype = TYPE_METHOD_BASETYPE (fntype);
10051
cab1f180
ML
10052 /* ISO C++ 11.4/5. A friend function defined in a class is in
10053 the (lexical) scope of the class in which it is defined. */
8d08fdba
MS
10054 if (!ctype && DECL_FRIEND_P (decl1))
10055 {
4f1c5b7d 10056 ctype = DECL_FRIEND_CONTEXT (decl1);
8d08fdba
MS
10057
10058 /* CTYPE could be null here if we're dealing with a template;
10059 for example, `inline friend float foo()' inside a template
10060 will have no CTYPE set. */
10061 if (ctype && TREE_CODE (ctype) != RECORD_TYPE)
10062 ctype = NULL_TREE;
10063 else
10064 doing_friend = 1;
10065 }
10066
5566b478 10067 last_function_parms = DECL_ARGUMENTS (decl1);
8d08fdba
MS
10068 }
10069 else
10070 {
d04a575f 10071 decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, 1, &attrs);
8d08fdba
MS
10072 /* If the declarator is not suitable for a function definition,
10073 cause a syntax error. */
e89a6075
JM
10074 if (decl1 == NULL_TREE || TREE_CODE (decl1) != FUNCTION_DECL)
10075 return 0;
10076
10077 cplus_decl_attributes (&decl1, attrs, 0);
8d08fdba 10078
ecb0eece 10079 /* If #pragma weak was used, mark the decl weak now. */
5f52c0e0 10080 if (global_scope_p (current_binding_level))
ecb0eece
RH
10081 maybe_apply_pragma_weak (decl1);
10082
8d08fdba
MS
10083 fntype = TREE_TYPE (decl1);
10084
10085 restype = TREE_TYPE (fntype);
8d08fdba
MS
10086
10087 if (TREE_CODE (fntype) == METHOD_TYPE)
10088 ctype = TYPE_METHOD_BASETYPE (fntype);
35680744 10089 else if (DECL_MAIN_P (decl1))
8d08fdba 10090 {
4b41d9eb
KL
10091 /* If this doesn't return integer_type, or a typedef to
10092 integer_type, complain. */
10093 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl1)), integer_type_node))
8d08fdba 10094 {
a28e3c7f 10095 if (pedantic || warn_return_type)
8251199e 10096 pedwarn ("return type for `main' changed to `int'");
8d08fdba
MS
10097 TREE_TYPE (decl1) = fntype = default_function_type;
10098 }
8d08fdba
MS
10099 }
10100 }
68642fb6 10101
97055d5c
AO
10102 if (DECL_DECLARED_INLINE_P (decl1)
10103 && lookup_attribute ("noinline", attrs))
ddd2d57e 10104 warning ("%Jinline function '%D' given attribute noinline", decl1, decl1);
97055d5c 10105
5f6eeeb3
NS
10106 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (decl1))
10107 /* This is a constructor, we must ensure that any default args
10108 introduced by this definition are propagated to the clones
10109 now. The clones are used directly in overload resolution. */
10110 adjust_clone_args (decl1);
10111
b35d4555
MM
10112 /* Sometimes we don't notice that a function is a static member, and
10113 build a METHOD_TYPE for it. Fix that up now. */
10114 if (ctype != NULL_TREE && DECL_STATIC_FUNCTION_P (decl1)
10115 && TREE_CODE (TREE_TYPE (decl1)) == METHOD_TYPE)
10116 {
3afb32a4 10117 revert_static_member_fn (decl1);
b35d4555
MM
10118 last_function_parms = TREE_CHAIN (last_function_parms);
10119 ctype = NULL_TREE;
10120 }
8d08fdba
MS
10121
10122 /* Warn if function was previously implicitly declared
10123 (but not if we warned then). */
10124 if (! warn_implicit
10125 && IDENTIFIER_IMPLICIT_DECL (DECL_NAME (decl1)) != NULL_TREE)
8251199e 10126 cp_warning_at ("`%D' implicitly declared before its definition", IDENTIFIER_IMPLICIT_DECL (DECL_NAME (decl1)));
8d08fdba 10127
f181d4ae
MM
10128 /* Set up current_class_type, and enter the scope of the class, if
10129 appropriate. */
10130 if (ctype)
14d22dd6 10131 push_nested_class (ctype);
f181d4ae 10132 else if (DECL_STATIC_FUNCTION_P (decl1))
14d22dd6 10133 push_nested_class (DECL_CONTEXT (decl1));
f181d4ae
MM
10134
10135 /* Now that we have entered the scope of the class, we must restore
10136 the bindings for any template parameters surrounding DECL1, if it
10137 is an inline member template. (Order is important; consider the
10138 case where a template parameter has the same name as a field of
10139 the class.) It is not until after this point that
10140 PROCESSING_TEMPLATE_DECL is guaranteed to be set up correctly. */
a8f73d4b 10141 if (flags & SF_INCLASS_INLINE)
f181d4ae
MM
10142 maybe_begin_member_template_processing (decl1);
10143
56cb9733 10144 /* Effective C++ rule 15. */
9188c363 10145 if (warn_ecpp
596ea4e5 10146 && DECL_OVERLOADED_OPERATOR_P (decl1) == NOP_EXPR
9188c363 10147 && TREE_CODE (TREE_TYPE (fntype)) == VOID_TYPE)
33bd39a2 10148 warning ("`operator=' should return a reference to `*this'");
9188c363
MM
10149
10150 /* Make the init_value nonzero so pushdecl knows this is not tentative.
10151 error_mark_node is replaced below (in poplevel) with the BLOCK. */
b35d4555
MM
10152 if (!DECL_INITIAL (decl1))
10153 DECL_INITIAL (decl1) = error_mark_node;
9188c363 10154
9188c363
MM
10155 /* This function exists in static storage.
10156 (This does not mean `static' in the C sense!) */
10157 TREE_STATIC (decl1) = 1;
10158
10159 /* We must call push_template_decl after current_class_type is set
10160 up. (If we are processing inline definitions after exiting a
10161 class scope, current_class_type will be NULL_TREE until set above
10162 by push_nested_class.) */
10163 if (processing_template_decl)
10164 decl1 = push_template_decl (decl1);
10165
f181d4ae 10166 /* We are now in the scope of the function being defined. */
8d08fdba 10167 current_function_decl = decl1;
f181d4ae 10168
5566b478
MS
10169 /* Save the parm names or decls from this function's declarator
10170 where store_parm_decls will find them. */
10171 current_function_parms = last_function_parms;
8d08fdba 10172
a8f73d4b
MM
10173 /* Make sure the parameter and return types are reasonable. When
10174 you declare a function, these types can be incomplete, but they
10175 must be complete when you define the function. */
5156628f 10176 if (! processing_template_decl)
f444e36b 10177 check_function_type (decl1, current_function_parms);
f181d4ae 10178
a8f73d4b
MM
10179 /* Build the return declaration for the function. */
10180 restype = TREE_TYPE (fntype);
e89a6075
JM
10181 /* Promote the value to int before returning it. */
10182 if (c_promoting_integer_type_p (restype))
10183 restype = type_promotes_to (restype);
10184 if (DECL_RESULT (decl1) == NULL_TREE)
a8f73d4b 10185 {
e89a6075
JM
10186 DECL_RESULT (decl1)
10187 = build_decl (RESULT_DECL, 0, TYPE_MAIN_VARIANT (restype));
10188 c_apply_type_quals_to_decl (cp_type_quals (restype),
10189 DECL_RESULT (decl1));
5566b478 10190 }
a8f73d4b
MM
10191
10192 /* Initialize RTL machinery. We cannot do this until
10193 CURRENT_FUNCTION_DECL and DECL_RESULT are set up. We do this
10194 even when processing a template; this is how we get
c00996a3
JM
10195 CFUN set up, and our per-function variables initialized.
10196 FIXME factor out the non-RTL stuff. */
a8f73d4b 10197 bl = current_binding_level;
4985cde3 10198 allocate_struct_function (decl1);
ff955512 10199 current_binding_level = bl;
a8f73d4b
MM
10200
10201 /* Even though we're inside a function body, we still don't want to
10202 call expand_expr to calculate the size of a variable-sized array.
10203 We haven't necessarily assigned RTL to all variables yet, so it's
10204 not safe to try to expand expressions involving them. */
10205 immediate_size_expand = 0;
01d939e8 10206 cfun->x_dont_save_pending_sizes_p = 1;
a8f73d4b 10207
f444e36b
MM
10208 /* Start the statement-tree, start the tree now. */
10209 begin_stmt_tree (&DECL_SAVED_TREE (decl1));
6f80451c 10210
d1a74aa7 10211 /* Don't double-count statements in templates. */
084c1779 10212 DECL_ESTIMATED_INSNS (decl1) = 0;
d1a74aa7 10213
a8f73d4b 10214 /* Let the user know we're compiling this function. */
ea11ca7e 10215 announce_function (decl1);
b7484fbe 10216
878cd289
MS
10217 /* Record the decl so that the function name is defined.
10218 If we already have a decl for this name, and it is a FUNCTION_DECL,
10219 use the old decl. */
a8f73d4b 10220 if (!processing_template_decl && !(flags & SF_PRE_PARSED))
878cd289 10221 {
75650646 10222 /* A specialization is not used to guide overload resolution. */
2228d450 10223 if (!DECL_TEMPLATE_SPECIALIZATION (decl1)
959d8796 10224 && ! DECL_FUNCTION_MEMBER_P (decl1))
75650646 10225 decl1 = pushdecl (decl1);
2c73f9f5 10226 else
b7698cf0 10227 {
a1c65f9f 10228 /* We need to set the DECL_CONTEXT. */
b7698cf0
JM
10229 if (!DECL_CONTEXT (decl1) && DECL_TEMPLATE_INFO (decl1))
10230 DECL_CONTEXT (decl1) = DECL_CONTEXT (DECL_TI_TEMPLATE (decl1));
10231 /* And make sure we have enough default args. */
10232 check_default_args (decl1);
10233 }
878cd289
MS
10234 fntype = TREE_TYPE (decl1);
10235 }
5566b478 10236
a8f73d4b 10237 /* Reset these in case the call to pushdecl changed them. */
5566b478 10238 current_function_decl = decl1;
01d939e8 10239 cfun->decl = decl1;
878cd289 10240
78c120b5
MM
10241 /* If we are (erroneously) defining a function that we have already
10242 defined before, wipe out what we knew before. */
e2500fed
GK
10243 if (!DECL_PENDING_INLINE_P (decl1))
10244 DECL_SAVED_FUNCTION_DATA (decl1) = NULL;
b35d4555 10245
f444e36b 10246 if (ctype && !doing_friend && !DECL_STATIC_FUNCTION_P (decl1))
b35d4555
MM
10247 {
10248 /* We know that this was set up by `grokclassfn'. We do not
10249 wait until `store_parm_decls', since evil parse errors may
10250 never get us to that point. Here we keep the consistency
10251 between `current_class_type' and `current_class_ptr'. */
10252 tree t = DECL_ARGUMENTS (decl1);
68642fb6
UD
10253
10254 my_friendly_assert (t != NULL_TREE && TREE_CODE (t) == PARM_DECL,
b35d4555
MM
10255 162);
10256 my_friendly_assert (TREE_CODE (TREE_TYPE (t)) == POINTER_TYPE,
10257 19990811);
68642fb6
UD
10258
10259 cp_function_chain->x_current_class_ref
3e411c3f 10260 = build_indirect_ref (t, NULL);
b35d4555
MM
10261 cp_function_chain->x_current_class_ptr = t;
10262
018fc244
MM
10263 /* Constructors and destructors need to know whether they're "in
10264 charge" of initializing virtual base classes. */
e0fff4b3 10265 t = TREE_CHAIN (t);
454fa7a7 10266 if (DECL_HAS_IN_CHARGE_PARM_P (decl1))
e0fff4b3
JM
10267 {
10268 current_in_charge_parm = t;
10269 t = TREE_CHAIN (t);
10270 }
10271 if (DECL_HAS_VTT_PARM_P (decl1))
10272 {
10273 if (DECL_NAME (t) != vtt_parm_identifier)
10274 abort ();
10275 current_vtt_parm = t;
10276 }
b35d4555
MM
10277 }
10278
db5ae43f 10279 if (DECL_INTERFACE_KNOWN (decl1))
faae18ab 10280 {
4f1c5b7d 10281 tree ctx = decl_function_context (decl1);
86052cc3 10282
faae18ab
MS
10283 if (DECL_NOT_REALLY_EXTERN (decl1))
10284 DECL_EXTERNAL (decl1) = 0;
86052cc3 10285
79065db2 10286 if (ctx != NULL_TREE && DECL_DECLARED_INLINE_P (ctx)
86052cc3
JM
10287 && TREE_PUBLIC (ctx))
10288 /* This is a function in a local class in an extern inline
10289 function. */
10290 comdat_linkage (decl1);
faae18ab 10291 }
8d08fdba
MS
10292 /* If this function belongs to an interface, it is public.
10293 If it belongs to someone else's interface, it is also external.
1f901793 10294 This only affects inlines and template instantiations. */
5566b478
MS
10295 else if (interface_unknown == 0
10296 && (! DECL_TEMPLATE_INSTANTIATION (decl1)
d11ad92e 10297 || flag_alt_external_templates))
8d08fdba 10298 {
79065db2
MM
10299 if (DECL_DECLARED_INLINE_P (decl1)
10300 || DECL_TEMPLATE_INSTANTIATION (decl1)
5156628f 10301 || processing_template_decl)
1f901793
JM
10302 {
10303 DECL_EXTERNAL (decl1)
10304 = (interface_only
79065db2
MM
10305 || (DECL_DECLARED_INLINE_P (decl1)
10306 && ! flag_implement_inlines
9c73ec84 10307 && !DECL_VINDEX (decl1)));
1f901793
JM
10308
10309 /* For WIN32 we also want to put these in linkonce sections. */
10310 maybe_make_one_only (decl1);
10311 }
db5ae43f 10312 else
893de33c 10313 DECL_EXTERNAL (decl1) = 0;
e8abc66f 10314 DECL_NOT_REALLY_EXTERN (decl1) = 0;
db5ae43f 10315 DECL_INTERFACE_KNOWN (decl1) = 1;
8d08fdba 10316 }
c16c47fb
JM
10317 else if (interface_unknown && interface_only
10318 && (! DECL_TEMPLATE_INSTANTIATION (decl1)
10319 || flag_alt_external_templates))
10320 {
10321 /* If MULTIPLE_SYMBOL_SPACES is defined and we saw a #pragma
10322 interface, we will have interface_only set but not
10323 interface_known. In that case, we don't want to use the normal
10324 heuristics because someone will supply a #pragma implementation
10325 elsewhere, and deducing it here would produce a conflict. */
10326 comdat_linkage (decl1);
10327 DECL_EXTERNAL (decl1) = 0;
10328 DECL_INTERFACE_KNOWN (decl1) = 1;
10329 DECL_DEFER_OUTPUT (decl1) = 1;
10330 }
8d08fdba 10331 else
a0a33927
MS
10332 {
10333 /* This is a definition, not a reference.
b7484fbe
MS
10334 So clear DECL_EXTERNAL. */
10335 DECL_EXTERNAL (decl1) = 0;
faae18ab 10336
79065db2
MM
10337 if ((DECL_DECLARED_INLINE_P (decl1)
10338 || DECL_TEMPLATE_INSTANTIATION (decl1))
5566b478
MS
10339 && ! DECL_INTERFACE_KNOWN (decl1)
10340 /* Don't try to defer nested functions for now. */
4f1c5b7d 10341 && ! decl_function_context (decl1))
878cd289
MS
10342 DECL_DEFER_OUTPUT (decl1) = 1;
10343 else
893de33c 10344 DECL_INTERFACE_KNOWN (decl1) = 1;
db5ae43f 10345 }
a9aedbc2 10346
ac20c67a 10347 begin_scope (sk_function_parms, decl1);
8d08fdba 10348
5566b478
MS
10349 ++function_depth;
10350
0d9eb3ba 10351 if (DECL_DESTRUCTOR_P (decl1))
46e8c075
MM
10352 {
10353 dtor_label = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE);
10354 DECL_CONTEXT (dtor_label) = current_function_decl;
10355 }
8d08fdba 10356
0ba8a114
NS
10357 start_fname_decls ();
10358
f444e36b
MM
10359 store_parm_decls (current_function_parms);
10360
8d08fdba
MS
10361 return 1;
10362}
10363\f
10364/* Store the parameter declarations into the current function declaration.
10365 This is called after parsing the parameter declarations, before
10366 digesting the body of the function.
10367
10368 Also install to binding contour return value identifier, if any. */
10369
f444e36b 10370static void
11f6b451 10371store_parm_decls (tree current_function_parms)
8d08fdba
MS
10372{
10373 register tree fndecl = current_function_decl;
10374 register tree parm;
8d08fdba 10375
8d08fdba
MS
10376 /* This is a chain of any other decls that came in among the parm
10377 declarations. If a parm is declared with enum {foo, bar} x;
10378 then CONST_DECLs for foo and bar are put here. */
10379 tree nonparms = NULL_TREE;
10380
b35d4555 10381 if (current_function_parms)
8d08fdba
MS
10382 {
10383 /* This case is when the function was defined with an ANSI prototype.
10384 The parms already have decls, so we need not do anything here
10385 except record them as in effect
10386 and complain if any redundant old-style parm decls were written. */
10387
b35d4555
MM
10388 tree specparms = current_function_parms;
10389 tree next;
10390
f444e36b 10391 /* Must clear this because it might contain TYPE_DECLs declared
b35d4555 10392 at class level. */
00e8de68 10393 current_binding_level->names = NULL;
8d08fdba 10394
f444e36b 10395 /* If we're doing semantic analysis, then we'll call pushdecl
b35d4555
MM
10396 for each of these. We must do them in reverse order so that
10397 they end in the correct forward order. */
f444e36b 10398 specparms = nreverse (specparms);
5566b478 10399
b35d4555 10400 for (parm = specparms; parm; parm = next)
8d08fdba
MS
10401 {
10402 next = TREE_CHAIN (parm);
10403 if (TREE_CODE (parm) == PARM_DECL)
10404 {
f444e36b
MM
10405 if (DECL_NAME (parm) == NULL_TREE
10406 || TREE_CODE (parm) != VOID_TYPE)
10407 pushdecl (parm);
10408 else
33bd39a2 10409 error ("parameter `%D' declared void", parm);
8d08fdba
MS
10410 }
10411 else
10412 {
10413 /* If we find an enum constant or a type tag,
10414 put it aside for the moment. */
10415 TREE_CHAIN (parm) = NULL_TREE;
10416 nonparms = chainon (nonparms, parm);
10417 }
10418 }
10419
f444e36b
MM
10420 /* Get the decls in their original chain order and record in the
10421 function. This is all and only the PARM_DECLs that were
10422 pushed into scope by the loop above. */
10423 DECL_ARGUMENTS (fndecl) = getdecls ();
8d08fdba
MS
10424 }
10425 else
10426 DECL_ARGUMENTS (fndecl) = NULL_TREE;
10427
10428 /* Now store the final chain of decls for the arguments
10429 as the decl-chain of the current lexical scope.
10430 Put the enumerators in as well, at the front so that
10431 DECL_ARGUMENTS is not modified. */
00e8de68 10432 current_binding_level->names = chainon (nonparms, DECL_ARGUMENTS (fndecl));
eb448459 10433
b35d4555 10434 /* Do the starting of the exception specifications, if we have any. */
68642fb6 10435 if (flag_exceptions && !processing_template_decl
1660cb3a 10436 && flag_enforce_eh_specs
b35d4555 10437 && TYPE_RAISES_EXCEPTIONS (TREE_TYPE (current_function_decl)))
52a11cbf 10438 current_eh_spec_block = begin_eh_spec_block ();
8d08fdba
MS
10439}
10440
8d08fdba 10441\f
59026e79
MM
10442/* We have finished doing semantic analysis on DECL, but have not yet
10443 generated RTL for its body. Save away our current state, so that
10444 when we want to generate RTL later we know what to do. */
10445
10446static void
11f6b451 10447save_function_data (tree decl)
59026e79 10448{
e2500fed 10449 struct language_function *f;
59026e79
MM
10450
10451 /* Save the language-specific per-function data so that we can
10452 get it back when we really expand this function. */
10453 my_friendly_assert (!DECL_PENDING_INLINE_P (decl),
10454 19990908);
68642fb6 10455
59026e79 10456 /* Make a copy. */
c68b0a84 10457 f = ggc_alloc (sizeof (struct language_function));
e2500fed 10458 memcpy (f, cp_function_chain, sizeof (struct language_function));
59026e79
MM
10459 DECL_SAVED_FUNCTION_DATA (decl) = f;
10460
10461 /* Clear out the bits we don't need. */
ae499cce
MM
10462 f->base.x_stmt_tree.x_last_stmt = NULL_TREE;
10463 f->base.x_stmt_tree.x_last_expr_type = NULL_TREE;
59026e79
MM
10464 f->x_named_label_uses = NULL;
10465 f->bindings = NULL;
8e4ce833 10466 f->x_local_names = NULL;
59026e79 10467
914653a2
MM
10468 /* If we've already decided that we cannot inline this function, we
10469 must remember that fact when we actually go to expand the
10470 function. */
acc72c37
MM
10471 if (current_function_cannot_inline)
10472 {
10473 f->cannot_inline = current_function_cannot_inline;
10474 DECL_INLINE (decl) = 0;
10475 }
59026e79
MM
10476}
10477
cdd2559c
JM
10478/* Add a note to mark the beginning of the main body of the constructor.
10479 This is used to set up the data structures for the cleanup regions for
10480 fully-constructed bases and members. */
10481
10482static void
11f6b451 10483begin_constructor_body (void)
cdd2559c 10484{
cdd2559c
JM
10485}
10486
ade3dc07
JM
10487/* Add a note to mark the end of the main body of the constructor. This is
10488 used to end the cleanup regions for fully-constructed bases and
10489 members. */
efee38a9
MM
10490
10491static void
11f6b451 10492finish_constructor_body (void)
efee38a9 10493{
efee38a9
MM
10494}
10495
cdd2559c
JM
10496/* Do all the processing for the beginning of a destructor; set up the
10497 vtable pointers and cleanups for bases and members. */
10498
10499static void
11f6b451 10500begin_destructor_body (void)
cdd2559c
JM
10501{
10502 tree if_stmt;
10503 tree compound_stmt;
10504
10505 /* If the dtor is empty, and we know there is not any possible
10506 way we could use any vtable entries, before they are possibly
10507 set by a base class dtor, we don't have to setup the vtables,
10508 as we know that any base class dtor will set up any vtables
10509 it needs. We avoid MI, because one base class dtor can do a
10510 virtual dispatch to an overridden function that would need to
10511 have a non-related vtable set up, we cannot avoid setting up
10512 vtables in that case. We could change this to see if there
10513 is just one vtable.
10514
10515 ??? In the destructor for a class, the vtables are set
10516 appropriately for that class. There will be no non-related
10517 vtables. jason 2001-12-11. */
10518 if_stmt = begin_if_stmt ();
10519
10520 /* If it is not safe to avoid setting up the vtables, then
10521 someone will change the condition to be boolean_true_node.
10522 (Actually, for now, we do not have code to set the condition
10523 appropriately, so we just assume that we always need to
10524 initialize the vtables.) */
10525 finish_if_stmt_cond (boolean_true_node, if_stmt);
cdd2559c 10526
7a3397c7 10527 compound_stmt = begin_compound_stmt (/*has_no_scope=*/false);
cdd2559c
JM
10528
10529 /* Make all virtual function table pointers in non-virtual base
10530 classes point to CURRENT_CLASS_TYPE's virtual function
10531 tables. */
10532 initialize_vtbl_ptrs (current_class_ptr);
10533
7a3397c7 10534 finish_compound_stmt (compound_stmt);
cdd2559c
JM
10535 finish_then_clause (if_stmt);
10536 finish_if_stmt ();
10537
10538 /* And insert cleanups for our bases and members so that they
10539 will be properly destroyed if we throw. */
10540 push_base_cleanups ();
10541}
10542
ade3dc07
JM
10543/* At the end of every destructor we generate code to delete the object if
10544 necessary. Do that now. */
9bfadf57
MM
10545
10546static void
11f6b451 10547finish_destructor_body (void)
9bfadf57 10548{
9bfadf57
MM
10549 tree exprstmt;
10550
5633b37c
MM
10551 /* Any return from a destructor will end up here; that way all base
10552 and member cleanups will be run when the function returns. */
10553 add_stmt (build_stmt (LABEL_STMT, dtor_label));
10554
52682a1b
MM
10555 /* In a virtual destructor, we must call delete. */
10556 if (DECL_VIRTUAL_P (current_function_decl))
10557 {
10558 tree if_stmt;
fa72b064 10559 tree virtual_size = cxx_sizeof (current_class_type);
68642fb6 10560
52682a1b 10561 /* [class.dtor]
68642fb6 10562
ade3dc07
JM
10563 At the point of definition of a virtual destructor (including
10564 an implicit definition), non-placement operator delete shall
10565 be looked up in the scope of the destructor's class and if
10566 found shall be accessible and unambiguous. */
52682a1b
MM
10567 exprstmt = build_op_delete_call
10568 (DELETE_EXPR, current_class_ptr, virtual_size,
10569 LOOKUP_NORMAL | LOOKUP_SPECULATIVELY, NULL_TREE);
298d6f60 10570
52682a1b
MM
10571 if_stmt = begin_if_stmt ();
10572 finish_if_stmt_cond (build (BIT_AND_EXPR, integer_type_node,
10573 current_in_charge_parm,
10574 integer_one_node),
10575 if_stmt);
10576 finish_expr_stmt (exprstmt);
10577 finish_then_clause (if_stmt);
10578 finish_if_stmt ();
10579 }
ade3dc07 10580}
9bfadf57 10581
ade3dc07
JM
10582/* Do the necessary processing for the beginning of a function body, which
10583 in this case includes member-initializers, but not the catch clauses of
10584 a function-try-block. Currently, this means opening a binding level
10585 for the member-initializers (in a ctor) and member cleanups (in a dtor).
10586 In other functions, this isn't necessary, but it doesn't hurt. */
10587
10588tree
11f6b451 10589begin_function_body (void)
ade3dc07 10590{
cdd2559c
JM
10591 tree stmt;
10592
b5856475
JM
10593 if (processing_template_decl)
10594 /* Do nothing now. */;
10595 else
10596 /* Always keep the BLOCK node associated with the outermost pair of
10597 curly braces of a function. These are needed for correct
10598 operation of dwarfout.c. */
ac20c67a 10599 keep_next_level (true);
b5856475 10600
7a3397c7 10601 stmt = begin_compound_stmt (/*has_no_scope=*/false);
ade3dc07 10602 COMPOUND_STMT_BODY_BLOCK (stmt) = 1;
cdd2559c
JM
10603
10604 if (processing_template_decl)
10605 /* Do nothing now. */;
10606 else if (DECL_CONSTRUCTOR_P (current_function_decl))
10607 begin_constructor_body ();
10608 else if (DECL_DESTRUCTOR_P (current_function_decl))
10609 begin_destructor_body ();
10610
ade3dc07 10611 return stmt;
9bfadf57
MM
10612}
10613
ade3dc07
JM
10614/* Do the processing for the end of a function body. Currently, this means
10615 closing out the cleanups for fully-constructed bases and members, and in
10616 the case of the destructor, deleting the object if desired. Again, this
10617 is only meaningful for [cd]tors, since they are the only functions where
10618 there is a significant distinction between the main body and any
10619 function catch clauses. Handling, say, main() return semantics here
10620 would be wrong, as flowing off the end of a function catch clause for
10621 main() would also need to return 0. */
10622
10623void
11f6b451 10624finish_function_body (tree compstmt)
ade3dc07 10625{
5633b37c 10626 /* Close the block. */
7a3397c7 10627 finish_compound_stmt (compstmt);
ade3dc07
JM
10628
10629 if (processing_template_decl)
10630 /* Do nothing now. */;
10631 else if (DECL_CONSTRUCTOR_P (current_function_decl))
10632 finish_constructor_body ();
10633 else if (DECL_DESTRUCTOR_P (current_function_decl))
10634 finish_destructor_body ();
10635}
10636
8d08fdba
MS
10637/* Finish up a function declaration and compile that function
10638 all the way to assembler language output. The free the storage
10639 for the function definition.
10640
68642fb6 10641 FLAGS is a bitwise or of the following values:
f181d4ae
MM
10642 2 - INCLASS_INLINE
10643 We just finished processing the body of an in-class inline
10644 function definition. (This processing will have taken place
87e3dbc9 10645 after the class definition is complete.) */
8d08fdba 10646
4d6abc1c 10647tree
11f6b451 10648finish_function (int flags)
8d08fdba
MS
10649{
10650 register tree fndecl = current_function_decl;
10651 tree fntype, ctype = NULL_TREE;
f181d4ae 10652 int inclass_inline = (flags & 2) != 0;
87e3dbc9 10653 int nested;
8d08fdba
MS
10654
10655 /* When we get some parse errors, we can end up without a
10656 current_function_decl, so cope. */
10657 if (fndecl == NULL_TREE)
4d6abc1c 10658 return error_mark_node;
8d08fdba 10659
9aad8f83
MA
10660 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fndecl)
10661 && DECL_VIRTUAL_P (fndecl)
10662 && !processing_template_decl)
10663 {
10664 tree fnclass = DECL_CONTEXT (fndecl);
10665 if (fndecl == CLASSTYPE_KEY_METHOD (fnclass))
10666 keyed_classes = tree_cons (NULL_TREE, fnclass, keyed_classes);
10667 }
10668
87e3dbc9 10669 nested = function_depth > 1;
8d08fdba
MS
10670 fntype = TREE_TYPE (fndecl);
10671
9bfadf57
MM
10672 /* TREE_READONLY (fndecl) = 1;
10673 This caused &foo to be of type ptr-to-const-function
10674 which then got a warning when stored in a ptr-to-function variable. */
8d08fdba 10675
f444e36b 10676 my_friendly_assert (building_stmt_tree (), 20000911);
8d08fdba 10677
0ba8a114
NS
10678 finish_fname_decls ();
10679
db9b2174
MM
10680 /* For a cloned function, we've already got all the code we need;
10681 there's no need to add any extra bits. */
f444e36b 10682 if (!DECL_CLONED_FUNCTION_P (fndecl))
8d08fdba 10683 {
ade3dc07 10684 if (DECL_MAIN_P (current_function_decl))
efee38a9
MM
10685 {
10686 /* Make it so that `main' always returns 0 by default. */
08c7ae5a 10687#if VMS_TARGET
efee38a9
MM
10688 finish_return_stmt (integer_one_node);
10689#else
10690 finish_return_stmt (integer_zero_node);
10691#endif
10692 }
87e3dbc9 10693
b35d4555
MM
10694 /* Finish dealing with exception specifiers. */
10695 if (flag_exceptions && !processing_template_decl
1660cb3a 10696 && flag_enforce_eh_specs
b35d4555 10697 && TYPE_RAISES_EXCEPTIONS (TREE_TYPE (current_function_decl)))
52a11cbf
RH
10698 finish_eh_spec_block (TYPE_RAISES_EXCEPTIONS
10699 (TREE_TYPE (current_function_decl)),
10700 current_eh_spec_block);
5566b478 10701 }
68642fb6 10702
558475f0 10703 /* If we're saving up tree structure, tie off the function now. */
f444e36b 10704 finish_stmt_tree (&DECL_SAVED_TREE (fndecl));
8d2733ca 10705
b2dd096b
MM
10706 /* If this function can't throw any exceptions, remember that. */
10707 if (!processing_template_decl
10708 && !cp_function_chain->can_throw
10709 && !flag_non_call_exceptions)
10710 TREE_NOTHROW (fndecl) = 1;
10711
8d08fdba
MS
10712 /* This must come after expand_function_end because cleanups might
10713 have declarations (from inline functions) that need to go into
10714 this function's blocks. */
7437519c
ZW
10715
10716 /* If the current binding level isn't the outermost binding level
10717 for this function, either there is a bug, or we have experienced
10718 syntax errors and the statement tree is malformed. */
a7e8c268 10719 if (current_binding_level->kind != sk_function_parms)
7437519c
ZW
10720 {
10721 /* Make sure we have already experienced errors. */
10722 if (errorcount == 0)
10723 abort ();
10724
10725 /* Throw away the broken statement tree and extra binding
10726 levels. */
10727 DECL_SAVED_TREE (fndecl) = build_stmt (COMPOUND_STMT, NULL_TREE);
10728
a7e8c268 10729 while (current_binding_level->kind != sk_function_parms)
7437519c 10730 {
a7e8c268 10731 if (current_binding_level->kind == sk_class)
7437519c
ZW
10732 pop_nested_class ();
10733 else
10734 poplevel (0, 0, 0);
10735 }
10736 }
f444e36b 10737 poplevel (1, 0, 1);
8d08fdba 10738
4985cde3
RH
10739 /* Statements should always be full-expressions at the outermost set
10740 of curly braces for a function. */
10741 my_friendly_assert (stmts_are_full_exprs_p (), 19990831);
10742
07b2f2fd
JM
10743 /* Set up the named return value optimization, if we can. Here, we
10744 eliminate the copy from the nrv into the RESULT_DECL and any cleanup
10745 for the nrv. genrtl_start_function and declare_return_variable
10746 handle making the nrv and RESULT_DECL share space. */
10747 if (current_function_return_value)
10748 {
10749 tree r = current_function_return_value;
35e939ae
JM
10750 tree outer;
10751
07b2f2fd 10752 if (r != error_mark_node
35e939ae
JM
10753 /* This is only worth doing for fns that return in memory--and
10754 simpler, since we don't have to worry about promoted modes. */
61f71b34 10755 && aggregate_value_p (TREE_TYPE (TREE_TYPE (fndecl)), fndecl)
35e939ae
JM
10756 /* Only allow this for variables declared in the outer scope of
10757 the function so we know that their lifetime always ends with a
10758 return; see g++.dg/opt/nrv6.C. We could be more flexible if
10759 we were to do this optimization in tree-ssa. */
10760 /* Skip the artificial function body block. */
10761 && (outer = BLOCK_SUBBLOCKS (BLOCK_SUBBLOCKS (DECL_INITIAL (fndecl))),
10762 chain_member (r, BLOCK_VARS (outer))))
07b2f2fd 10763 {
35e939ae 10764
07b2f2fd
JM
10765 DECL_ALIGN (r) = DECL_ALIGN (DECL_RESULT (fndecl));
10766 walk_tree_without_duplicates (&DECL_SAVED_TREE (fndecl),
10767 nullify_returns_r, r);
10768 }
10769 else
10770 /* Clear it so genrtl_start_function and declare_return_variable
10771 know we're not optimizing. */
10772 current_function_return_value = NULL_TREE;
10773 }
10774
a8f73d4b 10775 /* Remember that we were in class scope. */
db5ae43f 10776 if (current_class_name)
a8f73d4b 10777 ctype = current_class_type;
db5ae43f 10778
1caa11d3
MM
10779 /* Must mark the RESULT_DECL as being in this function. */
10780 DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
10781
10782 /* Set the BLOCK_SUPERCONTEXT of the outermost function scope to point
10783 to the FUNCTION_DECL node itself. */
10784 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
10785
59026e79 10786 /* Save away current state, if appropriate. */
f444e36b 10787 if (!processing_template_decl)
59026e79
MM
10788 save_function_data (fndecl);
10789
95fabfd3
MM
10790 /* If this function calls `setjmp' it cannot be inlined. When
10791 `longjmp' is called it is not guaranteed to restore the value of
10792 local variables that have been modified since the call to
10793 `setjmp'. So, if were to inline this function into some caller
10794 `c', then when we `longjmp', we might not restore all variables
10795 in `c'. (It might seem, at first blush, that there's no way for
10796 this function to modify local variables in `c', but their
10797 addresses may have been stored somewhere accessible to this
10798 function.) */
f444e36b 10799 if (!processing_template_decl && calls_setjmp_p (fndecl))
95fabfd3
MM
10800 DECL_UNINLINABLE (fndecl) = 1;
10801
efe49da0 10802 /* Complain if there's just no return statement. */
46cfb101 10803 if (warn_return_type
efe49da0 10804 && TREE_CODE (TREE_TYPE (fntype)) != VOID_TYPE
57d5032b 10805 && !current_function_returns_value && !current_function_returns_null
efe49da0
JM
10806 /* Don't complain if we abort or throw. */
10807 && !current_function_returns_abnormally
46cfb101
JM
10808 && !DECL_NAME (DECL_RESULT (fndecl))
10809 /* Normally, with -Wreturn-type, flow will complain. Unless we're an
efe49da0 10810 inline function, as we might never be compiled separately. */
efc7052d 10811 && (DECL_INLINE (fndecl) || processing_template_decl))
efe49da0 10812 warning ("no return statement in function returning non-void");
4985cde3
RH
10813
10814 /* We're leaving the context of this function, so zap cfun. It's still in
10815 DECL_SAVED_INSNS, and we'll restore it in tree_rest_of_compilation. */
f444e36b 10816 cfun = NULL;
c1f927e8 10817 current_function_decl = NULL;
a8f73d4b 10818
27631dae 10819 /* If this is an in-class inline definition, we may have to pop the
a8f73d4b
MM
10820 bindings for the template parameters that we added in
10821 maybe_begin_member_template_processing when start_function was
10822 called. */
10823 if (inclass_inline)
10824 maybe_end_member_template_processing ();
10825
10826 /* Leave the scope of the class. */
10827 if (ctype)
10828 pop_nested_class ();
5566b478
MS
10829
10830 --function_depth;
8d08fdba 10831
4d6abc1c 10832 /* Clean up. */
28cbf42c 10833 if (! nested)
1f8f4a0b
MM
10834 /* Let the error reporting routines know that we're outside a
10835 function. For a nested function, this value is used in
b03e38e1 10836 cxx_pop_function_context and then reset via pop_function_context. */
1f8f4a0b 10837 current_function_decl = NULL_TREE;
4d6abc1c
MM
10838
10839 return fndecl;
8d08fdba
MS
10840}
10841\f
10842/* Create the FUNCTION_DECL for a function definition.
8d08fdba
MS
10843 DECLSPECS and DECLARATOR are the parts of the declaration;
10844 they describe the return type and the name of the function,
10845 but twisted together in a fashion that parallels the syntax of C.
10846
10847 This function creates a binding context for the function body
10848 as well as setting up the FUNCTION_DECL in current_function_decl.
10849
10850 Returns a FUNCTION_DECL on success.
10851
10852 If the DECLARATOR is not suitable for a function (it defines a datum
10853 instead), we return 0, which tells yyparse to report a parse error.
10854
10855 May return void_type_node indicating that this method is actually
10856 a friend. See grokfield for more details.
10857
10858 Came here with a `.pushlevel' .
10859
10860 DO NOT MAKE ANY CHANGES TO THIS CODE WITHOUT MAKING CORRESPONDING
10861 CHANGES TO CODE IN `grokfield'. */
e92cc029 10862
8d08fdba 10863tree
11f6b451 10864start_method (tree declspecs, tree declarator, tree attrlist)
8d08fdba 10865{
c11b6f21 10866 tree fndecl = grokdeclarator (declarator, declspecs, MEMFUNCDEF, 0,
91d231cb 10867 &attrlist);
8d08fdba 10868
a723baf1
MM
10869 if (fndecl == error_mark_node)
10870 return error_mark_node;
10871
10872 if (fndecl == NULL || TREE_CODE (fndecl) != FUNCTION_DECL)
10873 {
10874 error ("invalid member function declaration");
10875 return error_mark_node;
10876 }
8d08fdba 10877
195a5def
IR
10878 if (attrlist)
10879 cplus_decl_attributes (&fndecl, attrlist, 0);
10880
8d08fdba 10881 /* Pass friends other than inline friend functions back. */
a1774733 10882 if (fndecl == void_type_node)
8d08fdba
MS
10883 return fndecl;
10884
8d08fdba
MS
10885 if (DECL_IN_AGGR_P (fndecl))
10886 {
10887 if (IDENTIFIER_ERROR_LOCUS (DECL_ASSEMBLER_NAME (fndecl)) != current_class_type)
10888 {
68642fb6 10889 if (DECL_CONTEXT (fndecl)
2c73f9f5 10890 && TREE_CODE( DECL_CONTEXT (fndecl)) != NAMESPACE_DECL)
33bd39a2 10891 error ("`%D' is already defined in class `%T'", fndecl,
2ae7bada 10892 DECL_CONTEXT (fndecl));
8d08fdba
MS
10893 }
10894 return void_type_node;
10895 }
10896
f3400fe2
JM
10897 check_template_shadow (fndecl);
10898
79065db2 10899 DECL_DECLARED_INLINE_P (fndecl) = 1;
8926095f 10900 if (flag_default_inline)
8d08fdba
MS
10901 DECL_INLINE (fndecl) = 1;
10902
36a117a5
MM
10903 /* We process method specializations in finish_struct_1. */
10904 if (processing_template_decl && !DECL_TEMPLATE_SPECIALIZATION (fndecl))
4b0d3cbe
MM
10905 {
10906 fndecl = push_template_decl (fndecl);
10907 if (fndecl == error_mark_node)
10908 return fndecl;
10909 }
a0a33927 10910
8d08fdba
MS
10911 if (! DECL_FRIEND_P (fndecl))
10912 {
8d08fdba
MS
10913 if (TREE_CHAIN (fndecl))
10914 {
10915 fndecl = copy_node (fndecl);
10916 TREE_CHAIN (fndecl) = NULL_TREE;
10917 }
271e6f02 10918 grok_special_member_properties (fndecl);
8d08fdba
MS
10919 }
10920
cd9f6678 10921 cp_finish_decl (fndecl, NULL_TREE, NULL_TREE, 0);
8d08fdba
MS
10922
10923 /* Make a place for the parms */
ac20c67a 10924 begin_scope (sk_function_parms, fndecl);
68642fb6 10925
8d08fdba
MS
10926 DECL_IN_AGGR_P (fndecl) = 1;
10927 return fndecl;
10928}
10929
10930/* Go through the motions of finishing a function definition.
10931 We don't compile this method until after the whole class has
10932 been processed.
10933
10934 FINISH_METHOD must return something that looks as though it
10935 came from GROKFIELD (since we are defining a method, after all).
10936
10937 This is called after parsing the body of the function definition.
10938 STMTS is the chain of statements that makes up the function body.
10939
10940 DECL is the ..._DECL that `start_method' provided. */
10941
10942tree
11f6b451 10943finish_method (tree decl)
8d08fdba
MS
10944{
10945 register tree fndecl = decl;
10946 tree old_initial;
8d08fdba
MS
10947
10948 register tree link;
10949
a1774733 10950 if (decl == void_type_node)
8d08fdba
MS
10951 return decl;
10952
10953 old_initial = DECL_INITIAL (fndecl);
10954
10955 /* Undo the level for the parms (from start_method).
10956 This is like poplevel, but it causes nothing to be
10957 saved. Saving information here confuses symbol-table
10958 output routines. Besides, this information will
10959 be correctly output when this method is actually
10960 compiled. */
10961
10962 /* Clear out the meanings of the local variables of this level;
10963 also record in each decl which block it belongs to. */
10964
10965 for (link = current_binding_level->names; link; link = TREE_CHAIN (link))
10966 {
10967 if (DECL_NAME (link) != NULL_TREE)
d8f8dca1 10968 pop_binding (DECL_NAME (link), link);
8d08fdba
MS
10969 my_friendly_assert (TREE_CODE (link) != FUNCTION_DECL, 163);
10970 DECL_CONTEXT (link) = NULL_TREE;
10971 }
10972
8d08fdba
MS
10973 poplevel (0, 0, 0);
10974
10975 DECL_INITIAL (fndecl) = old_initial;
10976
10977 /* We used to check if the context of FNDECL was different from
10978 current_class_type as another way to get inside here. This didn't work
10979 for String.cc in libg++. */
10980 if (DECL_FRIEND_P (fndecl))
10981 {
10982 CLASSTYPE_INLINE_FRIENDS (current_class_type)
10983 = tree_cons (NULL_TREE, fndecl, CLASSTYPE_INLINE_FRIENDS (current_class_type));
10984 decl = void_type_node;
10985 }
10986
10987 return decl;
10988}
10989\f
0154eaa8
MM
10990
10991/* VAR is a VAR_DECL. If its type is incomplete, remember VAR so that
10992 we can lay it out later, when and if its type becomes complete. */
8d08fdba
MS
10993
10994void
11f6b451 10995maybe_register_incomplete_var (tree var)
8d08fdba 10996{
0154eaa8 10997 my_friendly_assert (TREE_CODE (var) == VAR_DECL, 20020406);
8d08fdba 10998
0154eaa8
MM
10999 /* Keep track of variables with incomplete types. */
11000 if (!processing_template_decl && TREE_TYPE (var) != error_mark_node
11001 && DECL_EXTERNAL (var))
70adf8a9 11002 {
0154eaa8
MM
11003 tree inner_type = TREE_TYPE (var);
11004
11005 while (TREE_CODE (inner_type) == ARRAY_TYPE)
11006 inner_type = TREE_TYPE (inner_type);
11007 inner_type = TYPE_MAIN_VARIANT (inner_type);
11008
11009 if ((!COMPLETE_TYPE_P (inner_type) && CLASS_TYPE_P (inner_type))
11010 /* RTTI TD entries are created while defining the type_info. */
11011 || (TYPE_LANG_SPECIFIC (inner_type)
11012 && TYPE_BEING_DEFINED (inner_type)))
11013 incomplete_vars = tree_cons (inner_type, var, incomplete_vars);
70adf8a9 11014 }
0154eaa8 11015}
70adf8a9 11016
0154eaa8
MM
11017/* Called when a class type (given by TYPE) is defined. If there are
11018 any existing VAR_DECLs whose type hsa been completed by this
11019 declaration, update them now. */
70adf8a9 11020
0154eaa8 11021void
11f6b451 11022complete_vars (tree type)
0154eaa8
MM
11023{
11024 tree *list = &incomplete_vars;
11025
11026 my_friendly_assert (CLASS_TYPE_P (type), 20020406);
11027 while (*list)
11028 {
11029 if (same_type_p (type, TREE_PURPOSE (*list)))
70adf8a9 11030 {
0154eaa8 11031 tree var = TREE_VALUE (*list);
c740732f
MM
11032 /* Complete the type of the variable. The VAR_DECL itself
11033 will be laid out in expand_expr. */
11034 complete_type (TREE_TYPE (var));
0154eaa8
MM
11035 /* Remove this entry from the list. */
11036 *list = TREE_CHAIN (*list);
f30432d7
MS
11037 }
11038 else
0154eaa8 11039 list = &TREE_CHAIN (*list);
f30432d7 11040 }
8d08fdba
MS
11041}
11042
86f45d2c
MM
11043/* If DECL is of a type which needs a cleanup, build that cleanup
11044 here. */
e92cc029 11045
86f45d2c 11046tree
11f6b451 11047cxx_maybe_build_cleanup (tree decl)
8d08fdba
MS
11048{
11049 tree type = TREE_TYPE (decl);
86f45d2c 11050
834c6dff 11051 if (type != error_mark_node && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
8d08fdba 11052 {
80048418 11053 int flags = LOOKUP_NORMAL|LOOKUP_DESTRUCTOR;
8d08fdba 11054 tree rval;
8d08fdba 11055
8d08fdba
MS
11056 if (TREE_CODE (type) == ARRAY_TYPE)
11057 rval = decl;
11058 else
11059 {
dffd7eb6 11060 cxx_mark_addressable (decl);
8d08fdba
MS
11061 rval = build_unary_op (ADDR_EXPR, decl, 0);
11062 }
11063
11064 /* Optimize for space over speed here. */
11065 if (! TYPE_USES_VIRTUAL_BASECLASSES (type)
11066 || flag_expensive_optimizations)
11067 flags |= LOOKUP_NONVIRTUAL;
11068
86f45d2c
MM
11069 rval = build_delete (TREE_TYPE (rval), rval,
11070 sfk_complete_destructor, flags, 0);
8d08fdba
MS
11071
11072 if (TYPE_USES_VIRTUAL_BASECLASSES (type)
11073 && ! TYPE_HAS_DESTRUCTOR (type))
c7b62f14 11074 rval = build_compound_expr (rval, build_vbase_delete (type, decl));
8d08fdba 11075
8d08fdba
MS
11076 return rval;
11077 }
6e4ae815 11078 return NULL_TREE;
8d08fdba
MS
11079}
11080\f
558475f0 11081/* When a stmt has been parsed, this function is called. */
8d08fdba
MS
11082
11083void
11f6b451 11084finish_stmt (void)
8d08fdba 11085{
558475f0
MM
11086 /* Always assume this statement was not an expression statement. If
11087 it actually was an expression statement, its our callers
11088 responsibility to fix this up. */
11089 last_expr_type = NULL_TREE;
8d08fdba
MS
11090}
11091
3afb32a4
MM
11092/* DECL was originally constructed as a non-static member function,
11093 but turned out to be static. Update it accordingly. */
700f8a87 11094
8857f91e 11095void
11f6b451 11096revert_static_member_fn (tree decl)
8d08fdba 11097{
700f8a87 11098 tree tmp;
3afb32a4
MM
11099 tree function = TREE_TYPE (decl);
11100 tree args = TYPE_ARG_TYPES (function);
8d08fdba 11101
89d684bb 11102 if (cp_type_quals (TREE_TYPE (TREE_VALUE (args)))
91063b51 11103 != TYPE_UNQUALIFIED)
33bd39a2 11104 error ("static member function `%#D' declared with type qualifiers",
11306230 11105 decl);
f30432d7 11106
700f8a87
MS
11107 args = TREE_CHAIN (args);
11108 tmp = build_function_type (TREE_TYPE (function), args);
89d684bb 11109 tmp = build_qualified_type (tmp, cp_type_quals (function));
f30432d7 11110 tmp = build_exception_variant (tmp,
8d08fdba 11111 TYPE_RAISES_EXCEPTIONS (function));
3afb32a4
MM
11112 TREE_TYPE (decl) = tmp;
11113 if (DECL_ARGUMENTS (decl))
11114 DECL_ARGUMENTS (decl) = TREE_CHAIN (DECL_ARGUMENTS (decl));
11115 DECL_STATIC_FUNCTION_P (decl) = 1;
8d08fdba 11116}
a4443a08 11117
68642fb6
UD
11118/* Initialize the variables used during compilation of a C++
11119 function. */
db5ae43f 11120
b03e38e1 11121void
11f6b451 11122cxx_push_function_context (struct function * f)
99dccabc 11123{
e2500fed 11124 struct language_function *p
c68b0a84 11125 = ggc_alloc_cleared (sizeof (struct language_function));
e2500fed 11126 f->language = p;
db5ae43f 11127
f1dedc31
MM
11128 /* Whenever we start a new function, we destroy temporaries in the
11129 usual way. */
ae499cce 11130 current_stmt_tree ()->stmts_are_full_exprs_p = 1;
4985cde3
RH
11131
11132 if (f->decl)
11133 {
11134 tree fn = f->decl;
11135
11136 current_function_is_thunk = DECL_THUNK_P (fn);
11137
11138 if (DECL_SAVED_FUNCTION_DATA (fn))
11139 {
11140 /* If we already parsed this function, and we're just expanding it
11141 now, restore saved state. */
11142 *cp_function_chain = *DECL_SAVED_FUNCTION_DATA (fn);
11143
11144 /* If we decided that we didn't want to inline this function,
11145 make sure the back-end knows that. */
11146 if (!current_function_cannot_inline)
11147 current_function_cannot_inline = cp_function_chain->cannot_inline;
11148
11149 /* We don't need the saved data anymore. Unless this is an inline
11150 function; we need the named return value info for
11151 cp_copy_res_decl_for_inlining. */
11152 if (! DECL_INLINE (fn))
11153 DECL_SAVED_FUNCTION_DATA (fn) = NULL;
11154 }
11155 }
db5ae43f
MS
11156}
11157
a8f73d4b
MM
11158/* Free the language-specific parts of F, now that we've finished
11159 compiling the function. */
db5ae43f 11160
b03e38e1 11161void
11f6b451 11162cxx_pop_function_context (struct function * f)
db5ae43f 11163{
99dccabc 11164 f->language = 0;
db5ae43f 11165}
ebfc180f 11166
e2500fed
GK
11167/* Return which tree structure is used by T, or TS_CP_GENERIC if T is
11168 one of the language-independent trees. */
4519c0a8 11169
e2500fed 11170enum cp_tree_node_structure_enum
11f6b451 11171cp_tree_node_structure (union lang_tree_node * t)
4519c0a8 11172{
e2500fed 11173 switch (TREE_CODE (&t->generic))
4519c0a8 11174 {
a723baf1 11175 case DEFAULT_ARG: return TS_CP_DEFAULT_ARG;
e2500fed 11176 case IDENTIFIER_NODE: return TS_CP_IDENTIFIER;
e2500fed
GK
11177 case OVERLOAD: return TS_CP_OVERLOAD;
11178 case TEMPLATE_PARM_INDEX: return TS_CP_TPI;
11179 case PTRMEM_CST: return TS_CP_PTRMEM;
5dae1114 11180 case BASELINK: return TS_CP_BASELINK;
e2500fed 11181 case WRAPPER: return TS_CP_WRAPPER;
e2500fed 11182 default: return TS_CP_GENERIC;
4519c0a8
MM
11183 }
11184}
5fd8e536 11185
eaa7c03f
JM
11186/* Build the void_list_node (void_type_node having been created). */
11187tree
11f6b451 11188build_void_list_node (void)
eaa7c03f
JM
11189{
11190 tree t = build_tree_list (NULL_TREE, void_type_node);
11191 TREE_PARMLIST (t) = 1;
11192 return t;
11193}
11194
d363e7bf 11195static int
11f6b451 11196cp_missing_noreturn_ok_p (tree decl)
d363e7bf
AJ
11197{
11198 /* A missing noreturn is ok for the `main' function. */
92643fea 11199 return DECL_MAIN_P (decl);
d363e7bf 11200}
e2500fed
GK
11201
11202#include "gt-cp-decl.h"
11203#include "gtype-cp.h"
This page took 5.28281 seconds and 5 git commands to generate.