]> gcc.gnu.org Git - gcc.git/blame - gcc/cp/decl.c
2007-07-24 Dorit Nuzman <dorit@il.ibm.com>
[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,
ab53bae2 3 2001, 2002, 2003, 2004, 2005, 2006, 2007 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
1788952f
KC
20the Free Software Foundation, 51 Franklin Street, Fifth Floor,
21Boston, MA 02110-1301, 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 41#include "decl.h"
49c249e1
JM
42#include "output.h"
43#include "except.h"
54f92bfb 44#include "toplev.h"
e2500fed 45#include "hashtab.h"
0e9295cf 46#include "tm_p.h"
672a6f42 47#include "target.h"
26f943fd 48#include "c-common.h"
ecb0eece 49#include "c-pragma.h"
7437519c 50#include "diagnostic.h"
e7c41c99 51#include "intl.h"
e2500fed 52#include "debug.h"
22ffcc6f 53#include "timevar.h"
6de9cd9a 54#include "tree-flow.h"
0d83bf5a 55#include "pointer-set.h"
8d08fdba 56
62d1db17 57static tree grokparms (cp_parameter_declarator *, tree *);
11f6b451
NN
58static const char *redeclaration_error_message (tree, tree);
59
11f6b451 60static int decl_jump_unsafe (tree);
11f6b451
NN
61static void require_complete_types_for_parms (tree);
62static int ambi_op_p (enum tree_code);
63static int unary_op_p (enum tree_code);
11f6b451 64static void push_local_name (tree);
7e99327d 65static tree grok_reference_init (tree, tree, tree, tree *);
caf93cb0 66static tree grokvardecl (tree, tree, const cp_decl_specifier_seq *,
1ff3c076 67 int, int, tree);
11f6b451 68static void record_unknown_type (tree, const char *);
c79efc4d 69static tree builtin_function_1 (tree, tree);
11f6b451
NN
70static tree build_library_fn_1 (tree, enum tree_code, tree);
71static int member_function_or_else (tree, tree, enum overload_flags);
72static void bad_specifiers (tree, const char *, int, int, int, int,
73 int);
11f6b451
NN
74static void check_for_uninitialized_const_var (tree);
75static hashval_t typename_hash (const void *);
76static int typename_compare (const void *, const void *);
11f6b451 77static tree local_variable_p_walkfn (tree *, int *, void *);
11f6b451 78static tree record_builtin_java_type (const char *, int);
461c6fce 79static const char *tag_name (enum tag_types);
29ef83de 80static tree lookup_and_check_tag (enum tag_types, tree, tag_scope, bool);
11f6b451 81static int walk_namespaces_r (tree, walk_namespaces_fn, void *);
11f6b451
NN
82static void maybe_deduce_size_from_array_init (tree, tree);
83static void layout_var_decl (tree);
84static void maybe_commonize_var (tree);
7e99327d 85static tree check_initializer (tree, tree, int, tree *);
11f6b451
NN
86static void make_rtl_for_nonlocal_decl (tree, tree, const char *);
87static void save_function_data (tree);
88static void check_function_type (tree, tree);
11f6b451
NN
89static void finish_constructor_body (void);
90static void begin_destructor_body (void);
91static void finish_destructor_body (void);
92static tree create_array_type_for_decl (tree, tree, tree);
93static tree get_atexit_node (void);
94static tree get_dso_handle_node (void);
95static tree start_cleanup_fn (void);
96static void end_cleanup_fn (void);
97static tree cp_make_fname_decl (tree, int);
98static void initialize_predefined_identifiers (void);
caf93cb0 99static tree check_special_function_return_type
11f6b451
NN
100 (special_function_kind, tree, tree);
101static tree push_cp_library_fn (enum tree_code, tree);
102static tree build_cp_library_fn (tree, enum tree_code, tree);
103static void store_parm_decls (tree);
8e3df2de
MM
104static void initialize_local_var (tree, tree);
105static void expand_static_init (tree, tree);
106static tree next_initializable_field (tree);
8d08fdba 107
7f4edbcb 108/* The following symbols are subsumed in the cp_global_trees array, and
68642fb6 109 listed here individually for documentation purposes.
8d08fdba 110
7f4edbcb
BS
111 C++ extensions
112 tree wchar_decl_node;
37c46b43 113
7f4edbcb
BS
114 tree vtable_entry_type;
115 tree delta_type_node;
7f4edbcb 116 tree __t_desc_type_node;
8d08fdba 117
9e62871e 118 tree class_type_node;
7f4edbcb 119 tree unknown_type_node;
8d08fdba 120
7f4edbcb 121 Array type `vtable_entry_type[]'
8d08fdba 122
7f4edbcb
BS
123 tree vtbl_type_node;
124 tree vtbl_ptr_type_node;
8d08fdba 125
2854d3c6 126 Namespaces,
8d08fdba 127
7f4edbcb 128 tree std_node;
2854d3c6 129 tree abi_node;
8d08fdba 130
7f4edbcb
BS
131 A FUNCTION_DECL which can call `abort'. Not necessarily the
132 one that the user will declare, but sufficient to be called
133 by routines that want to abort the program.
8d08fdba 134
7f4edbcb 135 tree abort_fndecl;
8d08fdba 136
7f4edbcb 137 The FUNCTION_DECL for the default `::operator delete'.
2986ae00 138
7f4edbcb 139 tree global_delete_fndecl;
8d08fdba 140
7f4edbcb 141 Used by RTTI
669ec2b4 142 tree type_info_type_node, tinfo_decl_id, tinfo_decl_type;
3db45ab5 143 tree tinfo_var_id; */
8d08fdba 144
7f4edbcb 145tree cp_global_trees[CPTI_MAX];
8d08fdba 146
2c73f9f5 147/* Indicates that there is a type value in some namespace, although
7f4edbcb 148 that is not necessarily in scope at the moment. */
2c73f9f5 149
00e8de68 150tree global_type_node;
2c73f9f5 151
82a2669e 152/* The node that holds the "name" of the global scope. */
00e8de68 153tree global_scope_name;
82a2669e 154
8e4ce833
JJ
155#define local_names cp_function_chain->x_local_names
156
8d08fdba
MS
157/* A list of objects which have constructors or destructors
158 which reside in the global scope. The decl is stored in
159 the TREE_VALUE slot and the initializer is stored
160 in the TREE_PURPOSE slot. */
161tree static_aggregates;
162
8d08fdba
MS
163/* -- end of C++ */
164
81b3411c 165/* A node for the integer constants 2, and 3. */
d11ad92e 166
81b3411c 167tree integer_two_node, integer_three_node;
8d08fdba 168
1799e5d5
RH
169/* Used only for jumps to as-yet undefined labels, since jumps to
170 defined labels can have their validity checked immediately. */
171
172struct named_label_use_entry GTY(())
173{
174 struct named_label_use_entry *next;
175 /* The binding level to which this entry is *currently* attached.
176 This is initially the binding level in which the goto appeared,
177 but is modified as scopes are closed. */
178 struct cp_binding_level *binding_level;
179 /* The head of the names list that was current when the goto appeared,
180 or the inner scope popped. These are the decls that will *not* be
181 skipped when jumping to the label. */
182 tree names_in_scope;
183 /* The location of the goto, for error reporting. */
184 location_t o_goto_locus;
185 /* True if an OpenMP structured block scope has been closed since
186 the goto appeared. This means that the branch from the label will
187 illegally exit an OpenMP scope. */
188 bool in_omp_scope;
189};
190
6625cdb5
JM
191/* A list of all LABEL_DECLs in the function that have names. Here so
192 we can clear out their names' definitions at the end of the
193 function, and so we can check the validity of jumps to these labels. */
194
1799e5d5 195struct named_label_entry GTY(())
6625cdb5 196{
1799e5d5
RH
197 /* The decl itself. */
198 tree label_decl;
199
200 /* The binding level to which the label is *currently* attached.
201 This is initially set to the binding level in which the label
202 is defined, but is modified as scopes are closed. */
e2500fed 203 struct cp_binding_level *binding_level;
3db45ab5 204 /* The head of the names list that was current when the label was
1799e5d5
RH
205 defined, or the inner scope popped. These are the decls that will
206 be skipped when jumping to the label. */
6625cdb5 207 tree names_in_scope;
1799e5d5
RH
208 /* A tree list of all decls from all binding levels that would be
209 crossed by a backward branch to the label. */
6625cdb5 210 tree bad_decls;
1799e5d5
RH
211
212 /* A list of uses of the label, before the label is defined. */
213 struct named_label_use_entry *uses;
214
215 /* The following bits are set after the label is defined, and are
216 updated as scopes are popped. They indicate that a backward jump
ddd0b831 217 to the label will illegally enter a scope of the given flavor. */
1799e5d5
RH
218 bool in_try_scope;
219 bool in_catch_scope;
220 bool in_omp_scope;
6625cdb5 221};
8d08fdba 222
4519c0a8 223#define named_labels cp_function_chain->x_named_labels
8d08fdba 224\f
b2244c65
MM
225/* The number of function bodies which we are currently processing.
226 (Zero if we are at namespace scope, one inside the body of a
227 function, two inside the body of a function in a local class, etc.) */
228int function_depth;
e23bd218
IR
229
230/* States indicating how grokdeclarator() should handle declspecs marked
231 with __attribute__((deprecated)). An object declared as
232 __attribute__((deprecated)) suppresses warnings of uses of other
233 deprecated items. */
caf93cb0 234
e23bd218
IR
235enum deprecated_states {
236 DEPRECATED_NORMAL,
237 DEPRECATED_SUPPRESS
238};
239
240static enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
241
8d08fdba 242\f
0154eaa8
MM
243/* A TREE_LIST of VAR_DECLs. The TREE_PURPOSE is a RECORD_TYPE or
244 UNION_TYPE; the TREE_VALUE is a VAR_DECL with that type. At the
245 time the VAR_DECL was declared, the type was incomplete. */
246
e2500fed 247static GTY(()) tree incomplete_vars;
8d08fdba 248\f
74b846e0
MM
249/* Returns the kind of template specialization we are currently
250 processing, given that it's declaration contained N_CLASS_SCOPES
251 explicit scope qualifications. */
252
253tmpl_spec_kind
11f6b451 254current_tmpl_spec_kind (int n_class_scopes)
74b846e0
MM
255{
256 int n_template_parm_scopes = 0;
257 int seen_specialization_p = 0;
258 int innermost_specialization_p = 0;
e2500fed 259 struct cp_binding_level *b;
74b846e0
MM
260
261 /* Scan through the template parameter scopes. */
caf93cb0
EC
262 for (b = current_binding_level;
263 b->kind == sk_template_parms;
a7e8c268 264 b = b->level_chain)
74b846e0
MM
265 {
266 /* If we see a specialization scope inside a parameter scope,
267 then something is wrong. That corresponds to a declaration
268 like:
269
270 template <class T> template <> ...
271
0e339752 272 which is always invalid since [temp.expl.spec] forbids the
74b846e0
MM
273 specialization of a class member template if the enclosing
274 class templates are not explicitly specialized as well. */
a7e8c268 275 if (b->explicit_spec_p)
74b846e0
MM
276 {
277 if (n_template_parm_scopes == 0)
278 innermost_specialization_p = 1;
279 else
280 seen_specialization_p = 1;
281 }
282 else if (seen_specialization_p == 1)
283 return tsk_invalid_member_spec;
284
285 ++n_template_parm_scopes;
286 }
287
288 /* Handle explicit instantiations. */
289 if (processing_explicit_instantiation)
290 {
291 if (n_template_parm_scopes != 0)
292 /* We've seen a template parameter list during an explicit
293 instantiation. For example:
294
295 template <class T> template void f(int);
296
297 This is erroneous. */
298 return tsk_invalid_expl_inst;
299 else
300 return tsk_expl_inst;
301 }
302
303 if (n_template_parm_scopes < n_class_scopes)
304 /* We've not seen enough template headers to match all the
305 specialized classes present. For example:
306
0cbd7506 307 template <class T> void R<T>::S<T>::f(int);
74b846e0 308
0e339752 309 This is invalid; there needs to be one set of template
74b846e0
MM
310 parameters for each class. */
311 return tsk_insufficient_parms;
312 else if (n_template_parm_scopes == n_class_scopes)
313 /* We're processing a non-template declaration (even though it may
314 be a member of a template class.) For example:
315
0cbd7506 316 template <class T> void S<T>::f(int);
74b846e0
MM
317
318 The `class T' maches the `S<T>', leaving no template headers
319 corresponding to the `f'. */
320 return tsk_none;
321 else if (n_template_parm_scopes > n_class_scopes + 1)
322 /* We've got too many template headers. For example:
323
0cbd7506 324 template <> template <class T> void f (T);
74b846e0
MM
325
326 There need to be more enclosing classes. */
327 return tsk_excessive_parms;
328 else
329 /* This must be a template. It's of the form:
330
0cbd7506 331 template <class T> template <class U> void S<T>::f(U);
74b846e0
MM
332
333 This is a specialization if the innermost level was a
334 specialization; otherwise it's just a definition of the
335 template. */
336 return innermost_specialization_p ? tsk_expl_spec : tsk_template;
8d08fdba
MS
337}
338
74b846e0
MM
339/* Exit the current scope. */
340
341void
11f6b451 342finish_scope (void)
74b846e0
MM
343{
344 poplevel (0, 0, 0);
345}
346
acef433b
MM
347/* When a label goes out of scope, check to see if that label was used
348 in a valid manner, and issue any appropriate warnings or errors. */
349
350static void
11f6b451 351pop_label (tree label, tree old_value)
acef433b 352{
a7e8c268 353 if (!processing_template_decl)
acef433b 354 {
88848bde
MM
355 if (DECL_INITIAL (label) == NULL_TREE)
356 {
5b030314
NS
357 location_t location;
358
dee15844 359 error ("label %q+D used but not defined", label);
93409b8c
PB
360#ifdef USE_MAPPED_LOCATION
361 location = input_location; /* FIXME want (input_filename, (line)0) */
362#else
0cbd7506 363 location.file = input_filename;
5b030314 364 location.line = 0;
93409b8c 365#endif
88848bde 366 /* Avoid crashing later. */
5b030314 367 define_label (location, DECL_NAME (label));
88848bde 368 }
c616e51b
MLI
369 else
370 warn_for_unused_label (label);
acef433b 371 }
acef433b 372
6625cdb5 373 SET_IDENTIFIER_LABEL_VALUE (DECL_NAME (label), old_value);
acef433b
MM
374}
375
0811ea8f 376/* At the end of a function, all labels declared within the function
68642fb6 377 go out of scope. BLOCK is the top-level block for the
acef433b
MM
378 function. */
379
1799e5d5
RH
380static int
381pop_labels_1 (void **slot, void *data)
382{
383 struct named_label_entry *ent = (struct named_label_entry *) *slot;
384 tree block = (tree) data;
385
386 pop_label (ent->label_decl, NULL_TREE);
387
388 /* Put the labels into the "variables" of the top-level block,
389 so debugger can see them. */
390 TREE_CHAIN (ent->label_decl) = BLOCK_VARS (block);
391 BLOCK_VARS (block) = ent->label_decl;
392
393 htab_clear_slot (named_labels, slot);
394
395 return 1;
396}
397
acef433b 398static void
11f6b451 399pop_labels (tree block)
acef433b 400{
1799e5d5 401 if (named_labels)
acef433b 402 {
1799e5d5
RH
403 htab_traverse (named_labels, pop_labels_1, block);
404 named_labels = NULL;
acef433b 405 }
1799e5d5
RH
406}
407
408/* At the end of a block with local labels, restore the outer definition. */
acef433b 409
1799e5d5
RH
410static void
411pop_local_label (tree label, tree old_value)
412{
413 struct named_label_entry dummy;
414 void **slot;
415
416 pop_label (label, old_value);
417
418 dummy.label_decl = label;
419 slot = htab_find_slot (named_labels, &dummy, NO_INSERT);
420 htab_clear_slot (named_labels, slot);
acef433b
MM
421}
422
80aa8340
ZL
423/* The following two routines are used to interface to Objective-C++.
424 The binding level is purposely treated as an opaque type. */
425
426void *
427objc_get_current_scope (void)
428{
429 return current_binding_level;
430}
431
432/* The following routine is used by the NeXT-style SJLJ exceptions;
433 variables get marked 'volatile' so as to not be clobbered by
f749f3c2
ZL
434 _setjmp()/_longjmp() calls. All variables in the current scope,
435 as well as parent scopes up to (but not including) ENCLOSING_BLK
436 shall be thusly marked. */
80aa8340
ZL
437
438void
439objc_mark_locals_volatile (void *enclosing_blk)
440{
441 struct cp_binding_level *scope;
442
443 for (scope = current_binding_level;
e58a9aa1 444 scope && scope != enclosing_blk;
80aa8340
ZL
445 scope = scope->level_chain)
446 {
447 tree decl;
448
449 for (decl = scope->names; decl; decl = TREE_CHAIN (decl))
660845bf 450 objc_volatilize_decl (decl);
e58a9aa1
ZL
451
452 /* Do not climb up past the current function. */
453 if (scope->kind == sk_function_parms)
454 break;
80aa8340
ZL
455 }
456}
457
1799e5d5
RH
458/* Update data for defined and undefined labels when leaving a scope. */
459
460static int
461poplevel_named_label_1 (void **slot, void *data)
462{
463 struct named_label_entry *ent = (struct named_label_entry *) *slot;
464 struct cp_binding_level *bl = (struct cp_binding_level *) data;
465 struct cp_binding_level *obl = bl->level_chain;
466
467 if (ent->binding_level == bl)
468 {
469 tree decl;
470
471 for (decl = ent->names_in_scope; decl; decl = TREE_CHAIN (decl))
472 if (decl_jump_unsafe (decl))
473 ent->bad_decls = tree_cons (NULL, decl, ent->bad_decls);
474
475 ent->binding_level = obl;
476 ent->names_in_scope = obl->names;
477 switch (bl->kind)
478 {
479 case sk_try:
480 ent->in_try_scope = true;
481 break;
482 case sk_catch:
483 ent->in_catch_scope = true;
484 break;
485 case sk_omp:
486 ent->in_omp_scope = true;
487 break;
488 default:
489 break;
490 }
491 }
492 else if (ent->uses)
493 {
494 struct named_label_use_entry *use;
495
496 for (use = ent->uses; use ; use = use->next)
497 if (use->binding_level == bl)
498 {
499 use->binding_level = obl;
500 use->names_in_scope = obl->names;
501 if (bl->kind == sk_omp)
502 use->in_omp_scope = true;
503 }
504 }
505
506 return 1;
507}
508
8d08fdba
MS
509/* Exit a binding level.
510 Pop the level off, and restore the state of the identifier-decl mappings
511 that were in effect when this level was entered.
512
513 If KEEP == 1, this level had explicit declarations, so
514 and create a "block" (a BLOCK node) for the level
515 to record its declarations and subblocks for symbol table output.
516
8d08fdba
MS
517 If FUNCTIONBODY is nonzero, this level is the body of a function,
518 so create a block as if KEEP were set and also clear out all
519 label names.
520
521 If REVERSE is nonzero, reverse the order of decls before putting
522 them into the BLOCK. */
523
524tree
11f6b451 525poplevel (int keep, int reverse, int functionbody)
8d08fdba 526{
926ce8bd 527 tree link;
8d08fdba
MS
528 /* The chain of decls was accumulated in reverse order.
529 Put it into forward order, just for cleanliness. */
530 tree decls;
531 int tmp = functionbody;
b35d4555 532 int real_functionbody;
b35d4555 533 tree subblocks;
325c3691 534 tree block;
8d08fdba 535 tree decl;
f181d4ae 536 int leaving_for_scope;
ac20c67a 537 scope_kind kind;
f181d4ae 538
22ffcc6f 539 timevar_push (TV_NAME_LOOKUP);
325c3691
RH
540 restart:
541
542 block = NULL_TREE;
b35d4555 543
50bc768d 544 gcc_assert (current_binding_level->kind != sk_class);
b74a0560 545
ac20c67a 546 real_functionbody = (current_binding_level->kind == sk_cleanup
b35d4555 547 ? ((functionbody = 0), tmp) : functionbody);
b35d4555
MM
548 subblocks = functionbody >= 0 ? current_binding_level->blocks : 0;
549
50bc768d
NS
550 gcc_assert (!VEC_length(cp_class_binding,
551 current_binding_level->class_shadowed));
8d08fdba 552
536333d4
MM
553 /* We used to use KEEP == 2 to indicate that the new block should go
554 at the beginning of the list of blocks at this binding level,
555 rather than the end. This hack is no longer used. */
50bc768d 556 gcc_assert (keep == 0 || keep == 1);
536333d4 557
ac20c67a 558 if (current_binding_level->keep)
8d08fdba
MS
559 keep = 1;
560
6625cdb5
JM
561 /* Any uses of undefined labels, and any defined labels, now operate
562 under constraints of next binding contour. */
1799e5d5
RH
563 if (cfun && !functionbody && named_labels)
564 htab_traverse (named_labels, poplevel_named_label_1,
565 current_binding_level);
6625cdb5 566
8d08fdba
MS
567 /* Get the decls in the order they were written.
568 Usually current_binding_level->names is in reverse order.
569 But parameter decls were previously put in forward order. */
570
571 if (reverse)
572 current_binding_level->names
573 = decls = nreverse (current_binding_level->names);
574 else
575 decls = current_binding_level->names;
576
8d08fdba
MS
577 /* If there were any declarations or structure tags in that level,
578 or if this level is a function body,
579 create a BLOCK to record them for the life of this function. */
8d08fdba 580 block = NULL_TREE;
f444e36b 581 if (keep == 1 || functionbody)
8d08fdba
MS
582 block = make_node (BLOCK);
583 if (block != NULL_TREE)
584 {
f444e36b
MM
585 BLOCK_VARS (block) = decls;
586 BLOCK_SUBBLOCKS (block) = subblocks;
8d08fdba
MS
587 }
588
589 /* In each subblock, record that this is its superior. */
8d08fdba
MS
590 if (keep >= 0)
591 for (link = subblocks; link; link = TREE_CHAIN (link))
592 BLOCK_SUPERCONTEXT (link) = block;
593
f181d4ae
MM
594 /* We still support the old for-scope rules, whereby the variables
595 in a for-init statement were in scope after the for-statement
92bc1323 596 ended. We only use the new rules if flag_new_for_scope is
f181d4ae 597 nonzero. */
68642fb6 598 leaving_for_scope
a7e8c268 599 = current_binding_level->kind == sk_for && flag_new_for_scope == 1;
f181d4ae 600
caf93cb0
EC
601 /* Before we remove the declarations first check for unused variables. */
602 if (warn_unused_variable
603 && !processing_template_decl)
604 for (decl = getdecls (); decl; decl = TREE_CHAIN (decl))
605 if (TREE_CODE (decl) == VAR_DECL
606 && ! TREE_USED (decl)
607 && ! DECL_IN_SYSTEM_HEADER (decl)
608 && DECL_NAME (decl) && ! DECL_ARTIFICIAL (decl))
b323323f 609 warning (OPT_Wunused_variable, "unused variable %q+D", decl);
caf93cb0 610
f181d4ae
MM
611 /* Remove declarations for all the DECLs in this level. */
612 for (link = decls; link; link = TREE_CHAIN (link))
613 {
c3783399 614 if (leaving_for_scope && TREE_CODE (link) == VAR_DECL
0cbd7506 615 && DECL_NAME (link))
f181d4ae 616 {
90ea9897
MM
617 tree name = DECL_NAME (link);
618 cxx_binding *ob;
f181d4ae
MM
619 tree ns_binding;
620
90ea9897
MM
621 ob = outer_binding (name,
622 IDENTIFIER_BINDING (name),
623 /*class_p=*/true);
624 if (!ob)
625 ns_binding = IDENTIFIER_NAMESPACE_VALUE (name);
c7dda1e3
MM
626 else
627 ns_binding = NULL_TREE;
628
90ea9897 629 if (ob && ob->scope == current_binding_level->level_chain)
f181d4ae 630 /* We have something like:
68642fb6 631
0cbd7506
MS
632 int i;
633 for (int i; ;);
68642fb6 634
f181d4ae
MM
635 and we are leaving the `for' scope. There's no reason to
636 keep the binding of the inner `i' in this case. */
90ea9897
MM
637 pop_binding (name, link);
638 else if ((ob && (TREE_CODE (ob->value) == TYPE_DECL))
147135cc 639 || (ns_binding && TREE_CODE (ns_binding) == TYPE_DECL))
f181d4ae
MM
640 /* Here, we have something like:
641
642 typedef int I;
643
644 void f () {
645 for (int I; ;);
646 }
647
648 We must pop the for-scope binding so we know what's a
649 type and what isn't. */
90ea9897 650 pop_binding (name, link);
e76a2646 651 else
e76a2646 652 {
f181d4ae
MM
653 /* Mark this VAR_DECL as dead so that we can tell we left it
654 there only for backward compatibility. */
655 DECL_DEAD_FOR_LOCAL (link) = 1;
68642fb6 656
517f3315 657 /* Keep track of what should have happened when we
f181d4ae 658 popped the binding. */
90ea9897 659 if (ob && ob->value)
820cc88f
DB
660 {
661 SET_DECL_SHADOWED_FOR_VAR (link, ob->value);
662 DECL_HAS_SHADOWED_FOR_VAR_P (link) = 1;
663 }
f181d4ae
MM
664
665 /* Add it to the list of dead variables in the next
666 outermost binding to that we can remove these when we
667 leave that binding. */
668 current_binding_level->level_chain->dead_vars_from_for
669 = tree_cons (NULL_TREE, link,
670 current_binding_level->level_chain->
671 dead_vars_from_for);
672
af6fd53f 673 /* Although we don't pop the cxx_binding, we do clear
147135cc 674 its SCOPE since the scope is going away now. */
caf93cb0 675 IDENTIFIER_BINDING (name)->scope
90ea9897 676 = current_binding_level->level_chain;
e76a2646 677 }
2ee887f2 678 }
68642fb6 679 else
8d6e462b 680 {
8dc2b103 681 tree name;
c8094d83 682
f181d4ae 683 /* Remove the binding. */
0034cf72 684 decl = link;
caf93cb0 685
0034cf72
JM
686 if (TREE_CODE (decl) == TREE_LIST)
687 decl = TREE_VALUE (decl);
8dc2b103 688 name = decl;
c8094d83 689
8dc2b103
NS
690 if (TREE_CODE (name) == OVERLOAD)
691 name = OVL_FUNCTION (name);
caf93cb0 692
8dc2b103
NS
693 gcc_assert (DECL_P (name));
694 pop_binding (DECL_NAME (name), decl);
8d08fdba 695 }
f181d4ae 696 }
8d08fdba 697
f181d4ae
MM
698 /* Remove declarations for any `for' variables from inner scopes
699 that we kept around. */
700 for (link = current_binding_level->dead_vars_from_for;
701 link; link = TREE_CHAIN (link))
d8f8dca1 702 pop_binding (DECL_NAME (TREE_VALUE (link)), TREE_VALUE (link));
2ee887f2 703
f181d4ae
MM
704 /* Restore the IDENTIFIER_TYPE_VALUEs. */
705 for (link = current_binding_level->type_shadowed;
706 link; link = TREE_CHAIN (link))
707 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (link), TREE_VALUE (link));
acef433b
MM
708
709 /* Restore the IDENTIFIER_LABEL_VALUEs for local labels. */
710 for (link = current_binding_level->shadowed_labels;
68642fb6 711 link;
acef433b 712 link = TREE_CHAIN (link))
1799e5d5 713 pop_local_label (TREE_VALUE (link), TREE_PURPOSE (link));
acef433b 714
f181d4ae
MM
715 /* There may be OVERLOADs (wrapped in TREE_LISTs) on the BLOCK_VARs
716 list if a `using' declaration put them there. The debugging
3b426391 717 back ends won't understand OVERLOAD, so we remove them here.
f181d4ae
MM
718 Because the BLOCK_VARS are (temporarily) shared with
719 CURRENT_BINDING_LEVEL->NAMES we must do this fixup after we have
720 popped all the bindings. */
721 if (block)
722 {
723 tree* d;
8d6e462b 724
a06d48ef
JM
725 for (d = &BLOCK_VARS (block); *d; )
726 {
727 if (TREE_CODE (*d) == TREE_LIST)
728 *d = TREE_CHAIN (*d);
729 else
730 d = &TREE_CHAIN (*d);
731 }
8d6e462b 732 }
8d08fdba
MS
733
734 /* If the level being exited is the top level of a function,
735 check over all the labels. */
8d08fdba
MS
736 if (functionbody)
737 {
acef433b
MM
738 /* Since this is the top level block of a function, the vars are
739 the function's parameters. Don't leave them in the BLOCK
740 because they are found in the FUNCTION_DECL instead. */
8d08fdba 741 BLOCK_VARS (block) = 0;
acef433b 742 pop_labels (block);
8d08fdba
MS
743 }
744
ac20c67a 745 kind = current_binding_level->kind;
325c3691
RH
746 if (kind == sk_cleanup)
747 {
748 tree stmt;
749
750 /* If this is a temporary binding created for a cleanup, then we'll
751 have pushed a statement list level. Pop that, create a new
752 BIND_EXPR for the block, and insert it into the stream. */
753 stmt = pop_stmt_list (current_binding_level->statement_list);
754 stmt = c_build_bind_expr (block, stmt);
755 add_stmt (stmt);
756 }
8d08fdba 757
1a6daf15 758 leave_scope ();
8d08fdba 759 if (functionbody)
72c4a4ca
GK
760 {
761 /* The current function is being defined, so its DECL_INITIAL
762 should be error_mark_node. */
763 gcc_assert (DECL_INITIAL (current_function_decl) == error_mark_node);
764 DECL_INITIAL (current_function_decl) = block;
765 }
8d08fdba 766 else if (block)
f444e36b
MM
767 current_binding_level->blocks
768 = chainon (current_binding_level->blocks, block);
769
8d08fdba
MS
770 /* If we did not make a block for the level just exited,
771 any blocks made for inner levels
772 (since they cannot be recorded as subblocks in that level)
773 must be carried forward so they will later become subblocks
774 of something else. */
775 else if (subblocks)
536333d4
MM
776 current_binding_level->blocks
777 = chainon (current_binding_level->blocks, subblocks);
8d08fdba 778
d9b2d9da
MM
779 /* Each and every BLOCK node created here in `poplevel' is important
780 (e.g. for proper debugging information) so if we created one
781 earlier, mark it as "used". */
782 if (block)
783 TREE_USED (block) = 1;
784
325c3691 785 /* All temporary bindings created for cleanups are popped silently. */
ac20c67a 786 if (kind == sk_cleanup)
325c3691 787 goto restart;
8d08fdba 788
22ffcc6f 789 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, block);
8d08fdba
MS
790}
791
8d08fdba
MS
792/* Insert BLOCK at the end of the list of subblocks of the
793 current binding level. This is used when a BIND_EXPR is expanded,
794 to handle the BLOCK node inside the BIND_EXPR. */
795
796void
11f6b451 797insert_block (tree block)
8d08fdba
MS
798{
799 TREE_USED (block) = 1;
800 current_binding_level->blocks
801 = chainon (current_binding_level->blocks, block);
802}
803
0aafb128
MM
804/* Walk all the namespaces contained NAMESPACE, including NAMESPACE
805 itself, calling F for each. The DATA is passed to F as well. */
806
00e8de68
GDR
807static int
808walk_namespaces_r (tree namespace, walk_namespaces_fn f, void* data)
809{
810 int result = 0;
caf93cb0 811 tree current = NAMESPACE_LEVEL (namespace)->namespaces;
8d08fdba 812
00e8de68 813 result |= (*f) (namespace, data);
8d08fdba 814
00e8de68
GDR
815 for (; current; current = TREE_CHAIN (current))
816 result |= walk_namespaces_r (current, f, data);
fc0e7bf5 817
00e8de68 818 return result;
5566b478
MS
819}
820
00e8de68
GDR
821/* Walk all the namespaces, calling F for each. The DATA is passed to
822 F as well. */
823
824int
825walk_namespaces (walk_namespaces_fn f, void* data)
5566b478 826{
00e8de68 827 return walk_namespaces_r (global_namespace, f, data);
8d08fdba
MS
828}
829
00e8de68
GDR
830/* Call wrapup_globals_declarations for the globals in NAMESPACE. If
831 DATA is non-NULL, this is the last time we will call
832 wrapup_global_declarations for this NAMESPACE. */
39c01e4c 833
00e8de68
GDR
834int
835wrapup_globals_for_namespace (tree namespace, void* data)
39c01e4c 836{
00e8de68 837 struct cp_binding_level *level = NAMESPACE_LEVEL (namespace);
9857866d
KH
838 VEC(tree,gc) *statics = level->static_decls;
839 tree *vec = VEC_address (tree, statics);
840 int len = VEC_length (tree, statics);
00e8de68 841 int last_time = (data != 0);
68642fb6 842
00e8de68 843 if (last_time)
39c01e4c 844 {
00e8de68 845 check_global_declarations (vec, len);
db5f8b93 846 emit_debug_global_declarations (vec, len);
00e8de68 847 return 0;
39c01e4c
MM
848 }
849
00e8de68
GDR
850 /* Write out any globals that need to be output. */
851 return wrapup_global_declarations (vec, len);
39c01e4c
MM
852}
853
00e8de68 854\f
9188c363
MM
855/* In C++, you don't have to write `struct S' to refer to `S'; you
856 can just use `S'. We accomplish this by creating a TYPE_DECL as
857 if the user had written `typedef struct S S'. Create and return
858 the TYPE_DECL for TYPE. */
859
860tree
11f6b451 861create_implicit_typedef (tree name, tree type)
9188c363
MM
862{
863 tree decl;
864
865 decl = build_decl (TYPE_DECL, name, type);
c727aa5e 866 DECL_ARTIFICIAL (decl) = 1;
9188c363
MM
867 /* There are other implicit type declarations, like the one *within*
868 a class that allows you to write `S::S'. We must distinguish
869 amongst these. */
870 SET_DECL_IMPLICIT_TYPEDEF_P (decl);
871 TYPE_NAME (type) = decl;
872
873 return decl;
874}
875
8e4ce833
JJ
876/* Remember a local name for name-mangling purposes. */
877
878static void
11f6b451 879push_local_name (tree decl)
8e4ce833
JJ
880{
881 size_t i, nelts;
882 tree t, name;
883
22ffcc6f 884 timevar_push (TV_NAME_LOOKUP);
8e4ce833
JJ
885
886 name = DECL_NAME (decl);
887
9acaf84a 888 nelts = VEC_length (tree, local_names);
8e4ce833
JJ
889 for (i = 0; i < nelts; i++)
890 {
9acaf84a 891 t = VEC_index (tree, local_names, i);
8e4ce833
JJ
892 if (DECL_NAME (t) == name)
893 {
894 if (!DECL_LANG_SPECIFIC (decl))
895 retrofit_lang_decl (decl);
e2500fed 896 DECL_LANG_SPECIFIC (decl)->decl_flags.u2sel = 1;
8e4ce833
JJ
897 if (DECL_LANG_SPECIFIC (t))
898 DECL_DISCRIMINATOR (decl) = DECL_DISCRIMINATOR (t) + 1;
899 else
900 DECL_DISCRIMINATOR (decl) = 1;
901
9acaf84a 902 VEC_replace (tree, local_names, i, decl);
de94b46c
KG
903 timevar_pop (TV_NAME_LOOKUP);
904 return;
8e4ce833
JJ
905 }
906 }
907
9acaf84a 908 VEC_safe_push (tree, gc, local_names, decl);
22ffcc6f 909 timevar_pop (TV_NAME_LOOKUP);
8e4ce833 910}
8d08fdba
MS
911\f
912/* Subroutine of duplicate_decls: return truthvalue of whether
913 or not types of these decls match.
914
915 For C++, we must compare the parameter list so that `int' can match
916 `int&' in a parameter position, but `int&' is not confused with
917 `const int&'. */
e92cc029 918
6060a796 919int
11f6b451 920decls_match (tree newdecl, tree olddecl)
8d08fdba
MS
921{
922 int types_match;
923
347d73d7
ML
924 if (newdecl == olddecl)
925 return 1;
926
6b4b3deb
MM
927 if (TREE_CODE (newdecl) != TREE_CODE (olddecl))
928 /* If the two DECLs are not even the same kind of thing, we're not
929 interested in their types. */
930 return 0;
931
932 if (TREE_CODE (newdecl) == FUNCTION_DECL)
8d08fdba
MS
933 {
934 tree f1 = TREE_TYPE (newdecl);
935 tree f2 = TREE_TYPE (olddecl);
936 tree p1 = TYPE_ARG_TYPES (f1);
937 tree p2 = TYPE_ARG_TYPES (f2);
938
4f1c5b7d 939 if (CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl)
eb68cb58
MM
940 && ! (DECL_EXTERN_C_P (newdecl)
941 && DECL_EXTERN_C_P (olddecl)))
c5a6fc45
JM
942 return 0;
943
8d08fdba 944 if (TREE_CODE (f1) != TREE_CODE (f2))
0cbd7506 945 return 0;
8d08fdba 946
3bfdc719 947 if (same_type_p (TREE_TYPE (f1), TREE_TYPE (f2)))
8926095f 948 {
8b27e9ef
NS
949 if (p2 == NULL_TREE && DECL_EXTERN_C_P (olddecl)
950 && (DECL_BUILT_IN (olddecl)
951#ifndef NO_IMPLICIT_EXTERN_C
0cbd7506
MS
952 || (DECL_IN_SYSTEM_HEADER (newdecl) && !DECL_CLASS_SCOPE_P (newdecl))
953 || (DECL_IN_SYSTEM_HEADER (olddecl) && !DECL_CLASS_SCOPE_P (olddecl))
8b27e9ef
NS
954#endif
955 ))
a28e3c7f
MS
956 {
957 types_match = self_promoting_args_p (p1);
958 if (p1 == void_list_node)
959 TREE_TYPE (newdecl) = TREE_TYPE (olddecl);
960 }
8b27e9ef
NS
961#ifndef NO_IMPLICIT_EXTERN_C
962 else if (p1 == NULL_TREE
963 && (DECL_EXTERN_C_P (olddecl)
0cbd7506
MS
964 && DECL_IN_SYSTEM_HEADER (olddecl)
965 && !DECL_CLASS_SCOPE_P (olddecl))
8b27e9ef 966 && (DECL_EXTERN_C_P (newdecl)
0cbd7506
MS
967 && DECL_IN_SYSTEM_HEADER (newdecl)
968 && !DECL_CLASS_SCOPE_P (newdecl)))
a28e3c7f
MS
969 {
970 types_match = self_promoting_args_p (p2);
971 TREE_TYPE (newdecl) = TREE_TYPE (olddecl);
972 }
8b27e9ef 973#endif
8926095f 974 else
91063b51 975 types_match = compparms (p1, p2);
8926095f 976 }
8d08fdba
MS
977 else
978 types_match = 0;
979 }
6b4b3deb 980 else if (TREE_CODE (newdecl) == TEMPLATE_DECL)
51c184be 981 {
2bb5d995
JM
982 if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl))
983 != TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)))
984 return 0;
985
ee935db4
MM
986 if (!comp_template_parms (DECL_TEMPLATE_PARMS (newdecl),
987 DECL_TEMPLATE_PARMS (olddecl)))
988 return 0;
989
f84b4be9 990 if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL)
ee935db4
MM
991 types_match = same_type_p (TREE_TYPE (DECL_TEMPLATE_RESULT (olddecl)),
992 TREE_TYPE (DECL_TEMPLATE_RESULT (newdecl)));
f84b4be9
JM
993 else
994 types_match = decls_match (DECL_TEMPLATE_RESULT (olddecl),
995 DECL_TEMPLATE_RESULT (newdecl));
51c184be 996 }
8d08fdba
MS
997 else
998 {
91a5bc9f
KL
999 /* Need to check scope for variable declaration (VAR_DECL).
1000 For typedef (TYPE_DECL), scope is ignored. */
1001 if (TREE_CODE (newdecl) == VAR_DECL
ace4831d
MM
1002 && CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl)
1003 /* [dcl.link]
1004 Two declarations for an object with C language linkage
1005 with the same name (ignoring the namespace that qualify
1006 it) that appear in different namespace scopes refer to
1007 the same object. */
1008 && !(DECL_EXTERN_C_P (olddecl) && DECL_EXTERN_C_P (newdecl)))
91a5bc9f
KL
1009 return 0;
1010
8d08fdba
MS
1011 if (TREE_TYPE (newdecl) == error_mark_node)
1012 types_match = TREE_TYPE (olddecl) == error_mark_node;
1013 else if (TREE_TYPE (olddecl) == NULL_TREE)
1014 types_match = TREE_TYPE (newdecl) == NULL_TREE;
a0a33927
MS
1015 else if (TREE_TYPE (newdecl) == NULL_TREE)
1016 types_match = 0;
8d08fdba 1017 else
01240200 1018 types_match = comptypes (TREE_TYPE (newdecl),
3bfdc719
MM
1019 TREE_TYPE (olddecl),
1020 COMPARE_REDECLARATION);
8d08fdba
MS
1021 }
1022
1023 return types_match;
1024}
1025
1026/* If NEWDECL is `static' and an `extern' was seen previously,
24f30ed4 1027 warn about it. OLDDECL is the previous declaration.
8d08fdba
MS
1028
1029 Note that this does not apply to the C++ case of declaring
1030 a variable `extern const' and then later `const'.
1031
8d08fdba
MS
1032 Don't complain about built-in functions, since they are beyond
1033 the user's control. */
1034
a5e6b29b 1035void
11f6b451 1036warn_extern_redeclared_static (tree newdecl, tree olddecl)
8d08fdba 1037{
24f30ed4
MM
1038 tree name;
1039
68642fb6 1040 if (TREE_CODE (newdecl) == TYPE_DECL
655dc6ee 1041 || TREE_CODE (newdecl) == TEMPLATE_DECL
a2a9e21c
GDR
1042 || TREE_CODE (newdecl) == CONST_DECL
1043 || TREE_CODE (newdecl) == NAMESPACE_DECL)
8d08fdba 1044 return;
68642fb6 1045
963d5758
MM
1046 /* Don't get confused by static member functions; that's a different
1047 use of `static'. */
1048 if (TREE_CODE (newdecl) == FUNCTION_DECL
1049 && DECL_STATIC_FUNCTION_P (newdecl))
1050 return;
8d08fdba 1051
24f30ed4
MM
1052 /* If the old declaration was `static', or the new one isn't, then
1053 then everything is OK. */
1054 if (DECL_THIS_STATIC (olddecl) || !DECL_THIS_STATIC (newdecl))
1055 return;
1056
1057 /* It's OK to declare a builtin function as `static'. */
1058 if (TREE_CODE (olddecl) == FUNCTION_DECL
1059 && DECL_ARTIFICIAL (olddecl))
1060 return;
1061
8d08fdba 1062 name = DECL_ASSEMBLER_NAME (newdecl);
2d01edd7 1063 pedwarn ("%qD was declared %<extern%> and later %<static%>", newdecl);
dee15844 1064 pedwarn ("previous declaration of %q+D", olddecl);
8d08fdba
MS
1065}
1066
e1f0e7a6
MM
1067/* NEW_DECL is a redeclaration of OLD_DECL; both are functions or
1068 function templates. If their exception specifications do not
b9704fc5 1069 match, issue a diagnostic. */
e1f0e7a6
MM
1070
1071static void
1072check_redeclaration_exception_specification (tree new_decl,
1073 tree old_decl)
1074{
1075 tree new_type;
1076 tree old_type;
1077 tree new_exceptions;
1078 tree old_exceptions;
1079
1080 new_type = TREE_TYPE (new_decl);
1081 new_exceptions = TYPE_RAISES_EXCEPTIONS (new_type);
1082 old_type = TREE_TYPE (old_decl);
1083 old_exceptions = TYPE_RAISES_EXCEPTIONS (old_type);
1084
1085 /* [except.spec]
1086
1087 If any declaration of a function has an exception-specification,
1088 all declarations, including the definition and an explicit
1089 specialization, of that function shall have an
1090 exception-specification with the same set of type-ids. */
1091 if ((pedantic || ! DECL_IN_SYSTEM_HEADER (old_decl))
1092 && ! DECL_IS_BUILTIN (old_decl)
1093 && flag_exceptions
3db45ab5 1094 && !comp_except_specs (new_exceptions, old_exceptions,
e1f0e7a6
MM
1095 /*exact=*/true))
1096 {
1097 error ("declaration of %qF throws different exceptions", new_decl);
1098 error ("from previous declaration %q+F", old_decl);
1099 }
1100}
1101
b1a19c7c
MM
1102/* If NEWDECL is a redeclaration of OLDDECL, merge the declarations.
1103 If the redeclaration is invalid, a diagnostic is issued, and the
1104 error_mark_node is returned. Otherwise, OLDDECL is returned.
8d08fdba 1105
b1a19c7c 1106 If NEWDECL is not a redeclaration of OLDDECL, NULL_TREE is
d63d5d0c
ILT
1107 returned.
1108
1109 NEWDECL_IS_FRIEND is true if NEWDECL was declared as a friend. */
8d08fdba 1110
b1a19c7c 1111tree
d63d5d0c 1112duplicate_decls (tree newdecl, tree olddecl, bool newdecl_is_friend)
8d08fdba 1113{
8d08fdba 1114 unsigned olddecl_uid = DECL_UID (olddecl);
d63d5d0c 1115 int olddecl_friend = 0, types_match = 0, hidden_friend = 0;
0b60dfe3 1116 int new_defines_function = 0;
50b56799 1117 tree new_template;
5566b478
MS
1118
1119 if (newdecl == olddecl)
b1a19c7c 1120 return olddecl;
8d08fdba 1121
8926095f 1122 types_match = decls_match (newdecl, olddecl);
8d08fdba 1123
8d08fdba
MS
1124 /* If either the type of the new decl or the type of the old decl is an
1125 error_mark_node, then that implies that we have already issued an
1126 error (earlier) for some bogus type specification, and in that case,
1127 it is rather pointless to harass the user with yet more error message
0b60dfe3 1128 about the same declaration, so just pretend the types match here. */
bd6dd845
MS
1129 if (TREE_TYPE (newdecl) == error_mark_node
1130 || TREE_TYPE (olddecl) == error_mark_node)
dc5395cc 1131 return error_mark_node;
68642fb6 1132
97055d5c
AO
1133 if (DECL_P (olddecl)
1134 && TREE_CODE (newdecl) == FUNCTION_DECL
1135 && TREE_CODE (olddecl) == FUNCTION_DECL
1136 && (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl)))
1137 {
1138 if (DECL_DECLARED_INLINE_P (newdecl)
1139 && DECL_UNINLINABLE (newdecl)
1140 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
1141 /* Already warned elsewhere. */;
1142 else if (DECL_DECLARED_INLINE_P (olddecl)
1143 && DECL_UNINLINABLE (olddecl)
1144 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
1145 /* Already warned. */;
1146 else if (DECL_DECLARED_INLINE_P (newdecl)
1147 && DECL_UNINLINABLE (olddecl)
1148 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
1149 {
dee15844
JM
1150 warning (OPT_Wattributes, "function %q+D redeclared as inline",
1151 newdecl);
1152 warning (OPT_Wattributes, "previous declaration of %q+D "
1153 "with attribute noinline", olddecl);
97055d5c
AO
1154 }
1155 else if (DECL_DECLARED_INLINE_P (olddecl)
1156 && DECL_UNINLINABLE (newdecl)
1157 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
1158 {
dee15844
JM
1159 warning (OPT_Wattributes, "function %q+D redeclared with "
1160 "attribute noinline", newdecl);
1161 warning (OPT_Wattributes, "previous declaration of %q+D was inline",
1162 olddecl);
97055d5c
AO
1163 }
1164 }
1165
a1c65f9f 1166 /* Check for redeclaration and other discrepancies. */
d22c8596 1167 if (TREE_CODE (olddecl) == FUNCTION_DECL
9f33663b
JM
1168 && DECL_ARTIFICIAL (olddecl))
1169 {
d63d5d0c 1170 gcc_assert (!DECL_HIDDEN_FRIEND_P (olddecl));
9f33663b
JM
1171 if (TREE_CODE (newdecl) != FUNCTION_DECL)
1172 {
d63d5d0c
ILT
1173 /* Avoid warnings redeclaring built-ins which have not been
1174 explicitly declared. */
0cbd7506
MS
1175 if (DECL_ANTICIPATED (olddecl))
1176 return NULL_TREE;
d52e4867 1177
9f33663b
JM
1178 /* If you declare a built-in or predefined function name as static,
1179 the old definition is overridden, but optionally warn this was a
1180 bad choice of name. */
1181 if (! TREE_PUBLIC (newdecl))
1182 {
b323323f
LM
1183 warning (OPT_Wshadow, "shadowing %s function %q#D",
1184 DECL_BUILT_IN (olddecl) ? "built-in" : "library",
1185 olddecl);
9f33663b 1186 /* Discard the old built-in function. */
b1a19c7c 1187 return NULL_TREE;
9f33663b
JM
1188 }
1189 /* If the built-in is not ansi, then programs can override
1190 it even globally without an error. */
1191 else if (! DECL_BUILT_IN (olddecl))
d4ee4d25 1192 warning (0, "library function %q#D redeclared as non-function %q#D",
0cbd7506 1193 olddecl, newdecl);
9f33663b
JM
1194 else
1195 {
2d01edd7
GDR
1196 error ("declaration of %q#D", newdecl);
1197 error ("conflicts with built-in declaration %q#D",
0cbd7506 1198 olddecl);
9f33663b 1199 }
b1a19c7c 1200 return NULL_TREE;
a4443a08 1201 }
9f33663b 1202 else if (!types_match)
8d08fdba 1203 {
d63d5d0c
ILT
1204 /* Avoid warnings redeclaring built-ins which have not been
1205 explicitly declared. */
0cbd7506 1206 if (DECL_ANTICIPATED (olddecl))
498c0f27
JJ
1207 {
1208 /* Deal with fileptr_type_node. FILE type is not known
1209 at the time we create the builtins. */
1210 tree t1, t2;
1211
1212 for (t1 = TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
1213 t2 = TYPE_ARG_TYPES (TREE_TYPE (olddecl));
1214 t1 || t2;
1215 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1216 if (!t1 || !t2)
1217 break;
1218 else if (TREE_VALUE (t2) == fileptr_type_node)
1219 {
1220 tree t = TREE_VALUE (t1);
1221
1222 if (TREE_CODE (t) == POINTER_TYPE
1223 && TYPE_NAME (TREE_TYPE (t))
1224 && DECL_NAME (TYPE_NAME (TREE_TYPE (t)))
1225 == get_identifier ("FILE")
1226 && compparms (TREE_CHAIN (t1), TREE_CHAIN (t2)))
1227 {
1228 tree oldargs = TYPE_ARG_TYPES (TREE_TYPE (olddecl));
1229
1230 TYPE_ARG_TYPES (TREE_TYPE (olddecl))
1231 = TYPE_ARG_TYPES (TREE_TYPE (newdecl));
1232 types_match = decls_match (newdecl, olddecl);
1233 if (types_match)
d63d5d0c
ILT
1234 return duplicate_decls (newdecl, olddecl,
1235 newdecl_is_friend);
498c0f27
JJ
1236 TYPE_ARG_TYPES (TREE_TYPE (olddecl)) = oldargs;
1237 }
1238 }
1239 else if (! same_type_p (TREE_VALUE (t1), TREE_VALUE (t2)))
1240 break;
1241 }
d52e4867 1242 else if ((DECL_EXTERN_C_P (newdecl)
7e99327d
MM
1243 && DECL_EXTERN_C_P (olddecl))
1244 || compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
1245 TYPE_ARG_TYPES (TREE_TYPE (olddecl))))
a4443a08 1246 {
9f33663b
JM
1247 /* A near match; override the builtin. */
1248
1249 if (TREE_PUBLIC (newdecl))
a4443a08 1250 {
d4ee4d25
DD
1251 warning (0, "new declaration %q#D", newdecl);
1252 warning (0, "ambiguates built-in declaration %q#D",
0cbd7506 1253 olddecl);
a4443a08 1254 }
b323323f
LM
1255 else
1256 warning (OPT_Wshadow, "shadowing %s function %q#D",
0cbd7506
MS
1257 DECL_BUILT_IN (olddecl) ? "built-in" : "library",
1258 olddecl);
a4443a08 1259 }
9f33663b
JM
1260 else
1261 /* Discard the old built-in function. */
b1a19c7c 1262 return NULL_TREE;
04a9d080
RS
1263
1264 /* Replace the old RTL to avoid problems with inlining. */
84b8b0e0 1265 COPY_DECL_RTL (newdecl, olddecl);
8d08fdba 1266 }
d63d5d0c
ILT
1267 /* Even if the types match, prefer the new declarations type for
1268 built-ins which have not been explicitly declared, for
1269 exception lists, etc... */
10326855 1270 else if (DECL_ANTICIPATED (olddecl))
7072018e 1271 {
74869a85
NS
1272 tree type = TREE_TYPE (newdecl);
1273 tree attribs = (*targetm.merge_type_attributes)
1274 (TREE_TYPE (olddecl), type);
1275
e9525111 1276 type = cp_build_type_attribute_variant (type, attribs);
74869a85 1277 TREE_TYPE (newdecl) = TREE_TYPE (olddecl) = type;
7072018e 1278 }
68642fb6 1279
7e99327d
MM
1280 /* Whether or not the builtin can throw exceptions has no
1281 bearing on this declarator. */
1282 TREE_NOTHROW (olddecl) = 0;
1283
24f30ed4
MM
1284 if (DECL_THIS_STATIC (newdecl) && !DECL_THIS_STATIC (olddecl))
1285 {
1286 /* If a builtin function is redeclared as `static', merge
1287 the declarations, but make the original one static. */
1288 DECL_THIS_STATIC (olddecl) = 1;
1289 TREE_PUBLIC (olddecl) = 0;
9bfadf57 1290
421844e7
MM
1291 /* Make the old declaration consistent with the new one so
1292 that all remnants of the builtin-ness of this function
1293 will be banished. */
5d2ed28c 1294 SET_DECL_LANGUAGE (olddecl, DECL_LANGUAGE (newdecl));
84b8b0e0 1295 COPY_DECL_RTL (newdecl, olddecl);
24f30ed4 1296 }
39211cd5
MS
1297 }
1298 else if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
1299 {
6936461f
OW
1300 /* C++ Standard, 3.3, clause 4:
1301 "[Note: a namespace name or a class template name must be unique
1302 in its declarative region (7.3.2, clause 14). ]" */
1303 if (TREE_CODE (olddecl) != NAMESPACE_DECL
1304 && TREE_CODE (newdecl) != NAMESPACE_DECL
1305 && (TREE_CODE (olddecl) != TEMPLATE_DECL
1306 || TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) != TYPE_DECL)
1307 && (TREE_CODE (newdecl) != TEMPLATE_DECL
1308 || TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) != TYPE_DECL))
9ed182dc 1309 {
6936461f
OW
1310 if ((TREE_CODE (olddecl) == TYPE_DECL && DECL_ARTIFICIAL (olddecl)
1311 && TREE_CODE (newdecl) != TYPE_DECL)
1312 || (TREE_CODE (newdecl) == TYPE_DECL && DECL_ARTIFICIAL (newdecl)
1313 && TREE_CODE (olddecl) != TYPE_DECL))
1314 {
1315 /* We do nothing special here, because C++ does such nasty
1316 things with TYPE_DECLs. Instead, just let the TYPE_DECL
1317 get shadowed, and know that if we need to find a TYPE_DECL
1318 for a given name, we can look in the IDENTIFIER_TYPE_VALUE
1319 slot of the identifier. */
1320 return NULL_TREE;
1321 }
1322
1323 if ((TREE_CODE (newdecl) == FUNCTION_DECL
1324 && DECL_FUNCTION_TEMPLATE_P (olddecl))
1325 || (TREE_CODE (olddecl) == FUNCTION_DECL
1326 && DECL_FUNCTION_TEMPLATE_P (newdecl)))
1327 return NULL_TREE;
9ed182dc
JM
1328 }
1329
2d01edd7 1330 error ("%q#D redeclared as different kind of symbol", newdecl);
39211cd5
MS
1331 if (TREE_CODE (olddecl) == TREE_LIST)
1332 olddecl = TREE_VALUE (olddecl);
dee15844 1333 error ("previous declaration of %q+#D", olddecl);
39211cd5 1334
c0694c4b 1335 return error_mark_node;
8d08fdba 1336 }
8d08fdba
MS
1337 else if (!types_match)
1338 {
4f1c5b7d 1339 if (CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl))
7bdbfa05
MM
1340 /* These are certainly not duplicate declarations; they're
1341 from different scopes. */
b1a19c7c 1342 return NULL_TREE;
7bdbfa05 1343
8926095f 1344 if (TREE_CODE (newdecl) == TEMPLATE_DECL)
f0e01782
MS
1345 {
1346 /* The name of a class template may not be declared to refer to
1347 any other template, class, function, object, namespace, value,
e92cc029 1348 or type in the same scope. */
5566b478
MS
1349 if (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) == TYPE_DECL
1350 || TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL)
f0e01782 1351 {
2d01edd7 1352 error ("declaration of template %q#D", newdecl);
dee15844 1353 error ("conflicts with previous declaration %q+#D", olddecl);
f0e01782 1354 }
ec255269
MS
1355 else if (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) == FUNCTION_DECL
1356 && TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == FUNCTION_DECL
1357 && compparms (TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (olddecl))),
91063b51 1358 TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (newdecl))))
75650646 1359 && comp_template_parms (DECL_TEMPLATE_PARMS (newdecl),
b9201622
NS
1360 DECL_TEMPLATE_PARMS (olddecl))
1361 /* Template functions can be disambiguated by
1362 return type. */
1363 && same_type_p (TREE_TYPE (TREE_TYPE (newdecl)),
1364 TREE_TYPE (TREE_TYPE (olddecl))))
ec255269 1365 {
2d01edd7 1366 error ("new declaration %q#D", newdecl);
dee15844 1367 error ("ambiguates old declaration %q+#D", olddecl);
ec255269 1368 }
b1a19c7c 1369 return NULL_TREE;
f0e01782 1370 }
8926095f
MS
1371 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1372 {
eb68cb58 1373 if (DECL_EXTERN_C_P (newdecl) && DECL_EXTERN_C_P (olddecl))
8926095f 1374 {
2d01edd7 1375 error ("declaration of C function %q#D conflicts with",
0cbd7506 1376 newdecl);
dee15844 1377 error ("previous declaration %q+#D here", olddecl);
8926095f 1378 }
00595019 1379 else if (compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
91063b51 1380 TYPE_ARG_TYPES (TREE_TYPE (olddecl))))
700f8a87 1381 {
2d01edd7 1382 error ("new declaration %q#D", newdecl);
dee15844 1383 error ("ambiguates old declaration %q+#D", olddecl);
492b73bd 1384 return error_mark_node;
700f8a87
MS
1385 }
1386 else
b1a19c7c 1387 return NULL_TREE;
8926095f 1388 }
58ec3cc5 1389 else
8d08fdba 1390 {
2d01edd7 1391 error ("conflicting declaration %q#D", newdecl);
dee15844 1392 error ("%q+D has a previous declaration as %q#D", olddecl, olddecl);
0cbd7506 1393 return error_mark_node;
8d08fdba
MS
1394 }
1395 }
68642fb6 1396 else if (TREE_CODE (newdecl) == FUNCTION_DECL
75650646
MM
1397 && ((DECL_TEMPLATE_SPECIALIZATION (olddecl)
1398 && (!DECL_TEMPLATE_INFO (newdecl)
68642fb6 1399 || (DECL_TI_TEMPLATE (newdecl)
75650646
MM
1400 != DECL_TI_TEMPLATE (olddecl))))
1401 || (DECL_TEMPLATE_SPECIALIZATION (newdecl)
1402 && (!DECL_TEMPLATE_INFO (olddecl)
68642fb6 1403 || (DECL_TI_TEMPLATE (olddecl)
75650646 1404 != DECL_TI_TEMPLATE (newdecl))))))
386b8a85
JM
1405 /* It's OK to have a template specialization and a non-template
1406 with the same type, or to have specializations of two
75650646
MM
1407 different templates with the same type. Note that if one is a
1408 specialization, and the other is an instantiation of the same
1409 template, that we do not exit at this point. That situation
1410 can occur if we instantiate a template class, and then
0e339752 1411 specialize one of its methods. This situation is valid, but
75650646 1412 the declarations must be merged in the usual way. */
b1a19c7c 1413 return NULL_TREE;
68642fb6
UD
1414 else if (TREE_CODE (newdecl) == FUNCTION_DECL
1415 && ((DECL_TEMPLATE_INSTANTIATION (olddecl)
75650646
MM
1416 && !DECL_USE_TEMPLATE (newdecl))
1417 || (DECL_TEMPLATE_INSTANTIATION (newdecl)
1418 && !DECL_USE_TEMPLATE (olddecl))))
1419 /* One of the declarations is a template instantiation, and the
1420 other is not a template at all. That's OK. */
b1a19c7c 1421 return NULL_TREE;
c30b4add
MM
1422 else if (TREE_CODE (newdecl) == NAMESPACE_DECL)
1423 {
1424 /* In [namespace.alias] we have:
c8094d83 1425
0cbd7506 1426 In a declarative region, a namespace-alias-definition can be
c30b4add
MM
1427 used to redefine a namespace-alias declared in that declarative
1428 region to refer only to the namespace to which it already
1429 refers.
c8094d83 1430
c30b4add
MM
1431 Therefore, if we encounter a second alias directive for the same
1432 alias, we can just ignore the second directive. */
1433 if (DECL_NAMESPACE_ALIAS (newdecl)
c8094d83 1434 && (DECL_NAMESPACE_ALIAS (newdecl)
c30b4add
MM
1435 == DECL_NAMESPACE_ALIAS (olddecl)))
1436 return olddecl;
1437 /* [namespace.alias]
1438
0cbd7506 1439 A namespace-name or namespace-alias shall not be declared as
c30b4add
MM
1440 the name of any other entity in the same declarative region.
1441 A namespace-name defined at global scope shall not be
77880ae4 1442 declared as the name of any other entity in any global scope
c30b4add 1443 of the program. */
2d01edd7 1444 error ("declaration of namespace %qD conflicts with", newdecl);
dee15844 1445 error ("previous declaration of namespace %q+D here", olddecl);
c30b4add
MM
1446 return error_mark_node;
1447 }
8d08fdba
MS
1448 else
1449 {
d8e178a0 1450 const char *errmsg = redeclaration_error_message (newdecl, olddecl);
8251199e 1451 if (errmsg)
8d08fdba 1452 {
33bd39a2 1453 error (errmsg, newdecl);
8d08fdba 1454 if (DECL_NAME (olddecl) != NULL_TREE)
dee15844
JM
1455 error ((DECL_INITIAL (olddecl) && namespace_bindings_p ())
1456 ? "%q+#D previously defined here"
1457 : "%q+#D previously declared here", olddecl);
b1a19c7c 1458 return error_mark_node;
8d08fdba
MS
1459 }
1460 else if (TREE_CODE (olddecl) == FUNCTION_DECL
1461 && DECL_INITIAL (olddecl) != NULL_TREE
1462 && TYPE_ARG_TYPES (TREE_TYPE (olddecl)) == NULL_TREE
1463 && TYPE_ARG_TYPES (TREE_TYPE (newdecl)) != NULL_TREE)
1464 {
1465 /* Prototype decl follows defn w/o prototype. */
dee15844 1466 warning (0, "prototype for %q+#D", newdecl);
d4ee4d25 1467 warning (0, "%Jfollows non-prototype definition here", olddecl);
8d08fdba 1468 }
ace4831d
MM
1469 else if ((TREE_CODE (olddecl) == FUNCTION_DECL
1470 || TREE_CODE (olddecl) == VAR_DECL)
8d08fdba 1471 && DECL_LANGUAGE (newdecl) != DECL_LANGUAGE (olddecl))
8926095f 1472 {
ace4831d
MM
1473 /* [dcl.link]
1474 If two declarations of the same function or object
1475 specify different linkage-specifications ..., the program
1476 is ill-formed.... Except for functions with C++ linkage,
1477 a function declaration without a linkage specification
1478 shall not precede the first linkage specification for
1479 that function. A function can be declared without a
1480 linkage specification after an explicit linkage
1481 specification has been seen; the linkage explicitly
1482 specified in the earlier declaration is not affected by
1483 such a function declaration.
1484
1485 DR 563 raises the question why the restrictions on
1486 functions should not also apply to objects. Older
1487 versions of G++ silently ignore the linkage-specification
1488 for this example:
1489
1490 namespace N {
1491 extern int i;
1492 extern "C" int i;
1493 }
1494
1495 which is clearly wrong. Therefore, we now treat objects
1496 like functions. */
46ccf50a 1497 if (current_lang_depth () == 0)
ace4831d
MM
1498 {
1499 /* There is no explicit linkage-specification, so we use
1500 the linkage from the previous declaration. */
1501 if (!DECL_LANG_SPECIFIC (newdecl))
1502 retrofit_lang_decl (newdecl);
1503 SET_DECL_LANGUAGE (newdecl, DECL_LANGUAGE (olddecl));
1504 }
8926095f
MS
1505 else
1506 {
dee15844
JM
1507 error ("previous declaration of %q+#D with %qL linkage",
1508 olddecl, DECL_LANGUAGE (olddecl));
2d01edd7 1509 error ("conflicts with new declaration with %qL linkage",
0cbd7506 1510 DECL_LANGUAGE (newdecl));
8926095f
MS
1511 }
1512 }
e1cd6e56 1513
a6f02587 1514 if (DECL_LANG_SPECIFIC (olddecl) && DECL_USE_TEMPLATE (olddecl))
c32381b1
JM
1515 ;
1516 else if (TREE_CODE (olddecl) == FUNCTION_DECL)
e1cd6e56
MS
1517 {
1518 tree t1 = TYPE_ARG_TYPES (TREE_TYPE (olddecl));
1519 tree t2 = TYPE_ARG_TYPES (TREE_TYPE (newdecl));
1520 int i = 1;
1521
1522 if (TREE_CODE (TREE_TYPE (newdecl)) == METHOD_TYPE)
1523 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2);
68642fb6 1524
e1cd6e56
MS
1525 for (; t1 && t1 != void_list_node;
1526 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2), i++)
1527 if (TREE_PURPOSE (t1) && TREE_PURPOSE (t2))
1528 {
2507f3b5
RK
1529 if (1 == simple_cst_equal (TREE_PURPOSE (t1),
1530 TREE_PURPOSE (t2)))
e1cd6e56 1531 {
2d01edd7 1532 pedwarn ("default argument given for parameter %d of %q#D",
624a5800 1533 i, newdecl);
dee15844 1534 pedwarn ("after previous specification in %q+#D", olddecl);
e1cd6e56
MS
1535 }
1536 else
1537 {
2d01edd7 1538 error ("default argument given for parameter %d of %q#D",
0cbd7506 1539 i, newdecl);
dee15844 1540 error ("after previous specification in %q+#D",
e1cd6e56
MS
1541 olddecl);
1542 }
1543 }
a5894242 1544
caf93cb0 1545 if (DECL_DECLARED_INLINE_P (newdecl)
79065db2 1546 && ! DECL_DECLARED_INLINE_P (olddecl)
7fcdf4c2 1547 && TREE_ADDRESSABLE (olddecl) && warn_inline)
dff6b454 1548 {
d4ee4d25
DD
1549 warning (0, "%q#D was used before it was declared inline", newdecl);
1550 warning (0, "%Jprevious non-inline declaration here", olddecl);
dff6b454 1551 }
e1cd6e56 1552 }
8d08fdba
MS
1553 }
1554
4782bd5b
RS
1555 /* Do not merge an implicit typedef with an explicit one. In:
1556
1557 class A;
1558 ...
1559 typedef class A A __attribute__ ((foo));
1560
1561 the attribute should apply only to the typedef. */
1562 if (TREE_CODE (olddecl) == TYPE_DECL
1563 && (DECL_IMPLICIT_TYPEDEF_P (olddecl)
1564 || DECL_IMPLICIT_TYPEDEF_P (newdecl)))
b1a19c7c 1565 return NULL_TREE;
4782bd5b 1566
8d08fdba
MS
1567 /* If new decl is `static' and an `extern' was seen previously,
1568 warn about it. */
1569 warn_extern_redeclared_static (newdecl, olddecl);
1570
e92cc029 1571 /* We have committed to returning 1 at this point. */
8d08fdba
MS
1572 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1573 {
1574 /* Now that functions must hold information normally held
1575 by field decls, there is extra work to do so that
1576 declaration information does not get destroyed during
1577 definition. */
1578 if (DECL_VINDEX (olddecl))
1579 DECL_VINDEX (newdecl) = DECL_VINDEX (olddecl);
1580 if (DECL_CONTEXT (olddecl))
1581 DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
e1cd6e56
MS
1582 DECL_STATIC_CONSTRUCTOR (newdecl) |= DECL_STATIC_CONSTRUCTOR (olddecl);
1583 DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
fee7654e 1584 DECL_PURE_VIRTUAL_P (newdecl) |= DECL_PURE_VIRTUAL_P (olddecl);
864b83b9 1585 DECL_VIRTUAL_P (newdecl) |= DECL_VIRTUAL_P (olddecl);
815951d8 1586 DECL_INVALID_OVERRIDER_P (newdecl) |= DECL_INVALID_OVERRIDER_P (olddecl);
963d5758 1587 DECL_THIS_STATIC (newdecl) |= DECL_THIS_STATIC (olddecl);
e0fff4b3
JM
1588 if (DECL_OVERLOADED_OPERATOR_P (olddecl) != ERROR_MARK)
1589 SET_OVERLOADED_OPERATOR_CODE
1590 (newdecl, DECL_OVERLOADED_OPERATOR_P (olddecl));
0b60dfe3 1591 new_defines_function = DECL_INITIAL (newdecl) != NULL_TREE;
68642fb6 1592
0b60dfe3 1593 /* Optionally warn about more than one declaration for the same
0cbd7506
MS
1594 name, but don't warn about a function declaration followed by a
1595 definition. */
0b60dfe3
BK
1596 if (warn_redundant_decls && ! DECL_ARTIFICIAL (olddecl)
1597 && !(new_defines_function && DECL_INITIAL (olddecl) == NULL_TREE)
a1c65f9f 1598 /* Don't warn about extern decl followed by definition. */
0b60dfe3 1599 && !(DECL_EXTERNAL (olddecl) && ! DECL_EXTERNAL (newdecl))
a1c65f9f 1600 /* Don't warn about friends, let add_friend take care of it. */
d63d5d0c 1601 && ! (newdecl_is_friend || DECL_FRIEND_P (olddecl)))
0b60dfe3 1602 {
b323323f
LM
1603 warning (OPT_Wredundant_decls, "redundant redeclaration of %qD in same scope", newdecl);
1604 warning (OPT_Wredundant_decls, "previous declaration of %q+D", olddecl);
0b60dfe3 1605 }
8d08fdba
MS
1606 }
1607
1608 /* Deal with C++: must preserve virtual function table size. */
1609 if (TREE_CODE (olddecl) == TYPE_DECL)
1610 {
926ce8bd
KH
1611 tree newtype = TREE_TYPE (newdecl);
1612 tree oldtype = TREE_TYPE (olddecl);
8d08fdba
MS
1613
1614 if (newtype != error_mark_node && oldtype != error_mark_node
1615 && TYPE_LANG_SPECIFIC (newtype) && TYPE_LANG_SPECIFIC (oldtype))
e93ee644
MM
1616 CLASSTYPE_FRIEND_CLASSES (newtype)
1617 = CLASSTYPE_FRIEND_CLASSES (oldtype);
7df87825 1618
970d6386 1619 DECL_ORIGINAL_TYPE (newdecl) = DECL_ORIGINAL_TYPE (olddecl);
8d08fdba
MS
1620 }
1621
8d08fdba
MS
1622 /* Copy all the DECL_... slots specified in the new decl
1623 except for any that we copy here from the old type. */
91d231cb 1624 DECL_ATTRIBUTES (newdecl)
f6897b10 1625 = (*targetm.merge_decl_attributes) (olddecl, newdecl);
8d08fdba 1626
5566b478
MS
1627 if (TREE_CODE (newdecl) == TEMPLATE_DECL)
1628 {
e1f0e7a6
MM
1629 tree old_result;
1630 tree new_result;
1631 old_result = DECL_TEMPLATE_RESULT (olddecl);
1632 new_result = DECL_TEMPLATE_RESULT (newdecl);
1633 TREE_TYPE (olddecl) = TREE_TYPE (old_result);
68642fb6 1634 DECL_TEMPLATE_SPECIALIZATIONS (olddecl)
4d85e00e
MM
1635 = chainon (DECL_TEMPLATE_SPECIALIZATIONS (olddecl),
1636 DECL_TEMPLATE_SPECIALIZATIONS (newdecl));
68642fb6 1637
e1f0e7a6
MM
1638 if (DECL_FUNCTION_TEMPLATE_P (newdecl))
1639 {
1640 DECL_INLINE (old_result)
1641 |= DECL_INLINE (new_result);
1642 DECL_DECLARED_INLINE_P (old_result)
1643 |= DECL_DECLARED_INLINE_P (new_result);
1644 check_redeclaration_exception_specification (newdecl, olddecl);
1645 }
1646
daa8df65 1647 /* If the new declaration is a definition, update the file and
72c4a4ca
GK
1648 line information on the declaration, and also make
1649 the old declaration the same definition. */
e1f0e7a6
MM
1650 if (DECL_INITIAL (old_result) == NULL_TREE
1651 && DECL_INITIAL (new_result) != NULL_TREE)
daa8df65 1652 {
caf93cb0 1653 DECL_SOURCE_LOCATION (olddecl)
e1f0e7a6 1654 = DECL_SOURCE_LOCATION (old_result)
f31686a3 1655 = DECL_SOURCE_LOCATION (newdecl);
72c4a4ca 1656 DECL_INITIAL (old_result) = DECL_INITIAL (new_result);
26bcf8fc 1657 if (DECL_FUNCTION_TEMPLATE_P (newdecl))
e1f0e7a6
MM
1658 DECL_ARGUMENTS (old_result)
1659 = DECL_ARGUMENTS (new_result);
c11889ce
MM
1660 }
1661
b1a19c7c 1662 return olddecl;
5566b478 1663 }
68642fb6 1664
8d08fdba
MS
1665 if (types_match)
1666 {
1667 /* Automatically handles default parameters. */
1668 tree oldtype = TREE_TYPE (olddecl);
e1cd6e56 1669 tree newtype;
8d08fdba 1670
e1cd6e56 1671 /* Merge the data types specified in the two decls. */
6da794e8 1672 newtype = merge_types (TREE_TYPE (newdecl), TREE_TYPE (olddecl));
e1cd6e56 1673
6da794e8 1674 /* If merge_types produces a non-typedef type, just use the old type. */
9076e292
JM
1675 if (TREE_CODE (newdecl) == TYPE_DECL
1676 && newtype == DECL_ORIGINAL_TYPE (newdecl))
1677 newtype = oldtype;
1678
8d08fdba 1679 if (TREE_CODE (newdecl) == VAR_DECL)
17bbb839
MM
1680 {
1681 DECL_THIS_EXTERN (newdecl) |= DECL_THIS_EXTERN (olddecl);
1682 DECL_INITIALIZED_P (newdecl) |= DECL_INITIALIZED_P (olddecl);
3db45ab5 1683 DECL_NONTRIVIALLY_INITIALIZED_P (newdecl)
6c06fbce 1684 |= DECL_NONTRIVIALLY_INITIALIZED_P (olddecl);
39703eb9
MM
1685 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (newdecl)
1686 |= DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (olddecl);
1799e5d5
RH
1687
1688 /* Merge the threadprivate attribute from OLDDECL into NEWDECL. */
1689 if (DECL_LANG_SPECIFIC (olddecl)
1690 && CP_DECL_THREADPRIVATE_P (olddecl))
1691 {
1692 /* Allocate a LANG_SPECIFIC structure for NEWDECL, if needed. */
1693 if (!DECL_LANG_SPECIFIC (newdecl))
1694 retrofit_lang_decl (newdecl);
1695
1696 DECL_TLS_MODEL (newdecl) = DECL_TLS_MODEL (olddecl);
1697 CP_DECL_THREADPRIVATE_P (newdecl) = 1;
1698 }
17bbb839
MM
1699 }
1700
6da794e8 1701 /* Do this after calling `merge_types' so that default
8d08fdba 1702 parameters don't confuse us. */
e1f0e7a6
MM
1703 else if (TREE_CODE (newdecl) == FUNCTION_DECL)
1704 check_redeclaration_exception_specification (newdecl, olddecl);
8d08fdba
MS
1705 TREE_TYPE (newdecl) = TREE_TYPE (olddecl) = newtype;
1706
4a2f6dc0
VR
1707 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1708 check_default_args (newdecl);
1709
8d08fdba 1710 /* Lay the type out, unless already done. */
407f03b8 1711 if (! same_type_p (newtype, oldtype)
5566b478 1712 && TREE_TYPE (newdecl) != error_mark_node
5156628f 1713 && !(processing_template_decl && uses_template_parms (newdecl)))
b7484fbe
MS
1714 layout_type (TREE_TYPE (newdecl));
1715
5566b478
MS
1716 if ((TREE_CODE (newdecl) == VAR_DECL
1717 || TREE_CODE (newdecl) == PARM_DECL
1718 || TREE_CODE (newdecl) == RESULT_DECL
1719 || TREE_CODE (newdecl) == FIELD_DECL
1720 || TREE_CODE (newdecl) == TYPE_DECL)
5156628f 1721 && !(processing_template_decl && uses_template_parms (newdecl)))
b7484fbe 1722 layout_decl (newdecl, 0);
8d08fdba
MS
1723
1724 /* Merge the type qualifiers. */
1725 if (TREE_READONLY (newdecl))
1726 TREE_READONLY (olddecl) = 1;
1727 if (TREE_THIS_VOLATILE (newdecl))
1728 TREE_THIS_VOLATILE (olddecl) = 1;
5cefa0d9
JM
1729 if (TREE_NOTHROW (newdecl))
1730 TREE_NOTHROW (olddecl) = 1;
8d08fdba 1731
302a2cc5
JM
1732 /* Merge deprecatedness. */
1733 if (TREE_DEPRECATED (newdecl))
1734 TREE_DEPRECATED (olddecl) = 1;
1735
8d08fdba 1736 /* Merge the initialization information. */
8926095f
MS
1737 if (DECL_INITIAL (newdecl) == NULL_TREE
1738 && DECL_INITIAL (olddecl) != NULL_TREE)
1739 {
1740 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
f31686a3 1741 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
b0d06515
MM
1742 if (CAN_HAVE_FULL_LANG_DECL_P (newdecl)
1743 && DECL_LANG_SPECIFIC (newdecl)
4d85e00e 1744 && DECL_LANG_SPECIFIC (olddecl))
69ee8086
RH
1745 {
1746 DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
1da326c3 1747 DECL_STRUCT_FUNCTION (newdecl) = DECL_STRUCT_FUNCTION (olddecl);
69ee8086 1748 }
8926095f 1749 }
39211cd5
MS
1750
1751 /* Merge the section attribute.
0cbd7506 1752 We want to issue an error if the sections conflict but that must be
39211cd5
MS
1753 done later in decl_attributes since we are called before attributes
1754 are assigned. */
1755 if (DECL_SECTION_NAME (newdecl) == NULL_TREE)
1756 DECL_SECTION_NAME (newdecl) = DECL_SECTION_NAME (olddecl);
1757
a157febd
GK
1758 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1759 {
1760 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
1761 |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
1331d16f 1762 DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
220a35cc
RS
1763 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1764 TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
1765 TREE_NOTHROW (newdecl) |= TREE_NOTHROW (olddecl);
1331d16f
RS
1766 DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
1767 DECL_IS_PURE (newdecl) |= DECL_IS_PURE (olddecl);
c740732f
MM
1768 /* Keep the old RTL. */
1769 COPY_DECL_RTL (olddecl, newdecl);
1770 }
caf93cb0 1771 else if (TREE_CODE (newdecl) == VAR_DECL
c740732f
MM
1772 && (DECL_SIZE (olddecl) || !DECL_SIZE (newdecl)))
1773 {
1774 /* Keep the old RTL. We cannot keep the old RTL if the old
1775 declaration was for an incomplete object and the new
1776 declaration is not since many attributes of the RTL will
1777 change. */
1778 COPY_DECL_RTL (olddecl, newdecl);
a157febd 1779 }
8d08fdba
MS
1780 }
1781 /* If cannot merge, then use the new type and qualifiers,
1782 and don't preserve the old rtl. */
1783 else
1784 {
1785 /* Clean out any memory we had of the old declaration. */
1786 tree oldstatic = value_member (olddecl, static_aggregates);
1787 if (oldstatic)
1788 TREE_VALUE (oldstatic) = error_mark_node;
1789
1790 TREE_TYPE (olddecl) = TREE_TYPE (newdecl);
1791 TREE_READONLY (olddecl) = TREE_READONLY (newdecl);
1792 TREE_THIS_VOLATILE (olddecl) = TREE_THIS_VOLATILE (newdecl);
1793 TREE_SIDE_EFFECTS (olddecl) = TREE_SIDE_EFFECTS (newdecl);
1794 }
1795
1796 /* Merge the storage class information. */
45806a3f
FS
1797 merge_weak (newdecl, olddecl);
1798
e92cc029 1799 DECL_ONE_ONLY (newdecl) |= DECL_ONE_ONLY (olddecl);
794d4a61 1800 DECL_DEFER_OUTPUT (newdecl) |= DECL_DEFER_OUTPUT (olddecl);
a9aedbc2
MS
1801 TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
1802 TREE_STATIC (olddecl) = TREE_STATIC (newdecl) |= TREE_STATIC (olddecl);
1803 if (! DECL_EXTERNAL (olddecl))
1804 DECL_EXTERNAL (newdecl) = 0;
68642fb6 1805
50b56799 1806 new_template = NULL_TREE;
0b60dfe3 1807 if (DECL_LANG_SPECIFIC (newdecl) && DECL_LANG_SPECIFIC (olddecl))
8d08fdba 1808 {
a9aedbc2
MS
1809 DECL_INTERFACE_KNOWN (newdecl) |= DECL_INTERFACE_KNOWN (olddecl);
1810 DECL_NOT_REALLY_EXTERN (newdecl) |= DECL_NOT_REALLY_EXTERN (olddecl);
77be6f82 1811 DECL_COMDAT (newdecl) |= DECL_COMDAT (olddecl);
68642fb6 1812 DECL_TEMPLATE_INSTANTIATED (newdecl)
2b0a63a3 1813 |= DECL_TEMPLATE_INSTANTIATED (olddecl);
c8094d83 1814
58fb06b4
NS
1815 /* If the OLDDECL is an instantiation and/or specialization,
1816 then the NEWDECL must be too. But, it may not yet be marked
1817 as such if the caller has created NEWDECL, but has not yet
1818 figured out that it is a redeclaration. */
1819 if (!DECL_USE_TEMPLATE (newdecl))
1820 DECL_USE_TEMPLATE (newdecl) = DECL_USE_TEMPLATE (olddecl);
c8094d83 1821
0b60dfe3
BK
1822 /* Don't really know how much of the language-specific
1823 values we should copy from old to new. */
1824 DECL_IN_AGGR_P (newdecl) = DECL_IN_AGGR_P (olddecl);
caf93cb0 1825 DECL_LANG_SPECIFIC (newdecl)->decl_flags.u2 =
e2500fed 1826 DECL_LANG_SPECIFIC (olddecl)->decl_flags.u2;
0b60dfe3 1827 DECL_NONCONVERTING_P (newdecl) = DECL_NONCONVERTING_P (olddecl);
4684cd27 1828 DECL_REPO_AVAILABLE_P (newdecl) = DECL_REPO_AVAILABLE_P (olddecl);
50b56799
MM
1829 if (DECL_TEMPLATE_INFO (newdecl))
1830 new_template = DECL_TI_TEMPLATE (newdecl);
fbf1c34b 1831 DECL_TEMPLATE_INFO (newdecl) = DECL_TEMPLATE_INFO (olddecl);
3febd123 1832 DECL_INITIALIZED_IN_CLASS_P (newdecl)
0cbd7506 1833 |= DECL_INITIALIZED_IN_CLASS_P (olddecl);
0b60dfe3 1834 olddecl_friend = DECL_FRIEND_P (olddecl);
d63d5d0c
ILT
1835 hidden_friend = (DECL_ANTICIPATED (olddecl)
1836 && DECL_HIDDEN_FRIEND_P (olddecl)
1837 && newdecl_is_friend);
6a629cac
MM
1838
1839 /* Only functions have DECL_BEFRIENDING_CLASSES. */
1840 if (TREE_CODE (newdecl) == FUNCTION_DECL
1841 || DECL_FUNCTION_TEMPLATE_P (newdecl))
bb5e8a7f
MM
1842 {
1843 DECL_BEFRIENDING_CLASSES (newdecl)
1844 = chainon (DECL_BEFRIENDING_CLASSES (newdecl),
1845 DECL_BEFRIENDING_CLASSES (olddecl));
ca9e1382
KL
1846 /* DECL_THUNKS is only valid for virtual functions,
1847 otherwise it is a DECL_FRIEND_CONTEXT. */
1848 if (DECL_VIRTUAL_P (newdecl))
1849 DECL_THUNKS (newdecl) = DECL_THUNKS (olddecl);
bb5e8a7f 1850 }
8d08fdba
MS
1851 }
1852
8d08fdba
MS
1853 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1854 {
68642fb6
UD
1855 if (DECL_TEMPLATE_INSTANTIATION (olddecl)
1856 && !DECL_TEMPLATE_INSTANTIATION (newdecl))
75650646
MM
1857 {
1858 /* If newdecl is not a specialization, then it is not a
1859 template-related function at all. And that means that we
bf7c02dd 1860 should have exited above, returning 0. */
50bc768d 1861 gcc_assert (DECL_TEMPLATE_SPECIALIZATION (newdecl));
75650646 1862
68642fb6 1863 if (TREE_USED (olddecl))
75650646 1864 /* From [temp.expl.spec]:
68642fb6 1865
75650646
MM
1866 If a template, a member template or the member of a class
1867 template is explicitly specialized then that
1868 specialization shall be declared before the first use of
1869 that specialization that would cause an implicit
1870 instantiation to take place, in every translation unit in
1871 which such a use occurs. */
9e637a26 1872 error ("explicit specialization of %qD after first use",
75650646
MM
1873 olddecl);
1874
1875 SET_DECL_TEMPLATE_SPECIALIZATION (olddecl);
d03d18e8 1876
b9e75696
JM
1877 /* Don't propagate visibility from the template to the
1878 specialization here. We'll do that in determine_visibility if
1879 appropriate. */
1880 DECL_VISIBILITY_SPECIFIED (olddecl) = 0;
1881
d03d18e8
KL
1882 /* [temp.expl.spec/14] We don't inline explicit specialization
1883 just because the primary template says so. */
75650646 1884 }
d03d18e8
KL
1885 else
1886 {
1887 if (DECL_PENDING_INLINE_INFO (newdecl) == 0)
1888 DECL_PENDING_INLINE_INFO (newdecl) = DECL_PENDING_INLINE_INFO (olddecl);
faae18ab 1889
d03d18e8 1890 DECL_DECLARED_INLINE_P (newdecl) |= DECL_DECLARED_INLINE_P (olddecl);
faae18ab 1891
caf93cb0 1892 /* If either decl says `inline', this fn is inline, unless
d03d18e8
KL
1893 its definition was passed already. */
1894 if (DECL_INLINE (newdecl) && DECL_INITIAL (olddecl) == NULL_TREE)
1895 DECL_INLINE (olddecl) = 1;
1896 DECL_INLINE (newdecl) = DECL_INLINE (olddecl);
1897
1898 DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
1899 = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
1900 }
97055d5c 1901
5daf7c0a
JM
1902 /* Preserve abstractness on cloned [cd]tors. */
1903 DECL_ABSTRACT (newdecl) = DECL_ABSTRACT (olddecl);
1904
700f8a87
MS
1905 if (! types_match)
1906 {
5d2ed28c 1907 SET_DECL_LANGUAGE (olddecl, DECL_LANGUAGE (newdecl));
92643fea 1908 COPY_DECL_ASSEMBLER_NAME (newdecl, olddecl);
84b8b0e0 1909 COPY_DECL_RTL (newdecl, olddecl);
5566b478
MS
1910 }
1911 if (! types_match || new_defines_function)
1912 {
6f1b4c42
JM
1913 /* These need to be copied so that the names are available.
1914 Note that if the types do match, we'll preserve inline
1915 info and other bits, but if not, we won't. */
700f8a87
MS
1916 DECL_ARGUMENTS (olddecl) = DECL_ARGUMENTS (newdecl);
1917 DECL_RESULT (olddecl) = DECL_RESULT (newdecl);
700f8a87 1918 }
8d08fdba
MS
1919 if (new_defines_function)
1920 /* If defining a function declared with other language
1921 linkage, use the previously declared language linkage. */
5d2ed28c 1922 SET_DECL_LANGUAGE (newdecl, DECL_LANGUAGE (olddecl));
6f1b4c42 1923 else if (types_match)
8d08fdba
MS
1924 {
1925 /* If redeclaring a builtin function, and not a definition,
1926 it stays built in. */
1927 if (DECL_BUILT_IN (olddecl))
1928 {
26db82d8 1929 DECL_BUILT_IN_CLASS (newdecl) = DECL_BUILT_IN_CLASS (olddecl);
39211cd5 1930 DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl);
8d08fdba
MS
1931 /* If we're keeping the built-in definition, keep the rtl,
1932 regardless of declaration matches. */
84b8b0e0 1933 COPY_DECL_RTL (olddecl, newdecl);
8d08fdba 1934 }
8d08fdba
MS
1935
1936 DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
8d08fdba
MS
1937 /* Don't clear out the arguments if we're redefining a function. */
1938 if (DECL_ARGUMENTS (olddecl))
1939 DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl);
1940 }
1941 }
0b1161fc
MM
1942 else if (TREE_CODE (newdecl) == NAMESPACE_DECL)
1943 NAMESPACE_LEVEL (newdecl) = NAMESPACE_LEVEL (olddecl);
a9aedbc2 1944
8d08fdba
MS
1945 /* Now preserve various other info from the definition. */
1946 TREE_ADDRESSABLE (newdecl) = TREE_ADDRESSABLE (olddecl);
1947 TREE_ASM_WRITTEN (newdecl) = TREE_ASM_WRITTEN (olddecl);
1948 DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
92643fea 1949 COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
8d08fdba 1950
d7afec4b 1951 /* Warn about conflicting visibility specifications. */
c8094d83 1952 if (DECL_VISIBILITY_SPECIFIED (olddecl)
73a8adb6 1953 && DECL_VISIBILITY_SPECIFIED (newdecl)
d7afec4b
ND
1954 && DECL_VISIBILITY (newdecl) != DECL_VISIBILITY (olddecl))
1955 {
dee15844
JM
1956 warning (OPT_Wattributes, "%q+D: visibility attribute ignored "
1957 "because it", newdecl);
5c498b10
DD
1958 warning (OPT_Wattributes, "%Jconflicts with previous "
1959 "declaration here", olddecl);
d7afec4b
ND
1960 }
1961 /* Choose the declaration which specified visibility. */
1962 if (DECL_VISIBILITY_SPECIFIED (olddecl))
facef326 1963 {
facef326 1964 DECL_VISIBILITY (newdecl) = DECL_VISIBILITY (olddecl);
d7afec4b
ND
1965 DECL_VISIBILITY_SPECIFIED (newdecl) = 1;
1966 }
3db45ab5 1967 /* Init priority used to be merged from newdecl to olddecl by the memcpy,
820cc88f
DB
1968 so keep this behavior. */
1969 if (TREE_CODE (newdecl) == VAR_DECL && DECL_HAS_INIT_PRIORITY_P (newdecl))
1970 {
1971 SET_DECL_INIT_PRIORITY (olddecl, DECL_INIT_PRIORITY (newdecl));
1972 DECL_HAS_INIT_PRIORITY_P (olddecl) = 1;
1973 }
facef326 1974
1d786913
MM
1975 /* The DECL_LANG_SPECIFIC information in OLDDECL will be replaced
1976 with that from NEWDECL below. */
1977 if (DECL_LANG_SPECIFIC (olddecl))
1978 {
c8094d83 1979 gcc_assert (DECL_LANG_SPECIFIC (olddecl)
1d786913
MM
1980 != DECL_LANG_SPECIFIC (newdecl));
1981 ggc_free (DECL_LANG_SPECIFIC (olddecl));
1982 }
1983
8d08fdba
MS
1984 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1985 {
1986 int function_size;
8d08fdba 1987
820cc88f 1988 function_size = sizeof (struct tree_decl_common);
8d08fdba 1989
730e1556
KG
1990 memcpy ((char *) olddecl + sizeof (struct tree_common),
1991 (char *) newdecl + sizeof (struct tree_common),
1992 function_size - sizeof (struct tree_common));
8d08fdba 1993
820cc88f
DB
1994 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
1995 (char *) newdecl + sizeof (struct tree_decl_common),
1996 sizeof (struct tree_function_decl) - sizeof (struct tree_decl_common));
50b56799 1997 if (new_template)
b3445994
MM
1998 /* If newdecl is a template instantiation, it is possible that
1999 the following sequence of events has occurred:
2000
2001 o A friend function was declared in a class template. The
2002 class template was instantiated.
2003
2004 o The instantiation of the friend declaration was
2005 recorded on the instantiation list, and is newdecl.
2006
2007 o Later, however, instantiate_class_template called pushdecl
2008 on the newdecl to perform name injection. But, pushdecl in
2009 turn called duplicate_decls when it discovered that another
2010 declaration of a global function with the same name already
2011 existed.
2012
2013 o Here, in duplicate_decls, we decided to clobber newdecl.
2014
2015 If we're going to do that, we'd better make sure that
2016 olddecl, and not newdecl, is on the list of
2017 instantiations so that if we try to do the instantiation
2018 again we won't get the clobbered declaration. */
caf93cb0 2019 reregister_specialization (newdecl,
50b56799 2020 new_template,
b3445994 2021 olddecl);
8d08fdba
MS
2022 }
2023 else
2024 {
820cc88f 2025 size_t size = tree_code_size (TREE_CODE (olddecl));
730e1556
KG
2026 memcpy ((char *) olddecl + sizeof (struct tree_common),
2027 (char *) newdecl + sizeof (struct tree_common),
820cc88f 2028 sizeof (struct tree_decl_common) - sizeof (struct tree_common));
3db45ab5 2029 switch (TREE_CODE (olddecl))
820cc88f
DB
2030 {
2031 case LABEL_DECL:
2032 case VAR_DECL:
2033 case RESULT_DECL:
2034 case PARM_DECL:
2035 case FIELD_DECL:
2036 case TYPE_DECL:
2037 case CONST_DECL:
2038 {
2039 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2040 (char *) newdecl + sizeof (struct tree_decl_common),
2041 size - sizeof (struct tree_decl_common)
2042 + TREE_CODE_LENGTH (TREE_CODE (newdecl)) * sizeof (char *));
2043 }
2044 break;
2045 default:
2046 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2047 (char *) newdecl + sizeof (struct tree_decl_common),
2048 sizeof (struct tree_decl_non_common) - sizeof (struct tree_decl_common)
2049 + TREE_CODE_LENGTH (TREE_CODE (newdecl)) * sizeof (char *));
2050 break;
2051 }
8d08fdba 2052 }
8d08fdba
MS
2053 DECL_UID (olddecl) = olddecl_uid;
2054 if (olddecl_friend)
2055 DECL_FRIEND_P (olddecl) = 1;
d63d5d0c
ILT
2056 if (hidden_friend)
2057 {
2058 DECL_ANTICIPATED (olddecl) = 1;
2059 DECL_HIDDEN_FRIEND_P (olddecl) = 1;
2060 }
8d08fdba 2061
d9525bec
BK
2062 /* NEWDECL contains the merged attribute lists.
2063 Update OLDDECL to be the same. */
91d231cb 2064 DECL_ATTRIBUTES (olddecl) = DECL_ATTRIBUTES (newdecl);
d9525bec 2065
16e31964
DS
2066 /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
2067 so that encode_section_info has a chance to look at the new decl
2068 flags and attributes. */
caf93cb0 2069 if (DECL_RTL_SET_P (olddecl)
16e31964
DS
2070 && (TREE_CODE (olddecl) == FUNCTION_DECL
2071 || (TREE_CODE (olddecl) == VAR_DECL
2072 && TREE_STATIC (olddecl))))
0e6df31e 2073 make_decl_rtl (olddecl);
16e31964 2074
1d786913
MM
2075 /* The NEWDECL will no longer be needed. Because every out-of-class
2076 declaration of a member results in a call to duplicate_decls,
2077 freeing these nodes represents in a significant savings. */
2078 ggc_free (newdecl);
2079
b1a19c7c 2080 return olddecl;
8d08fdba 2081}
8d08fdba 2082\f
8251199e 2083/* Return zero if the declaration NEWDECL is valid
8d08fdba
MS
2084 when the declaration OLDDECL (assumed to be for the same name)
2085 has already been seen.
2086 Otherwise return an error message format string with a %s
2087 where the identifier should go. */
2088
d8e178a0 2089static const char *
11f6b451 2090redeclaration_error_message (tree newdecl, tree olddecl)
8d08fdba
MS
2091{
2092 if (TREE_CODE (newdecl) == TYPE_DECL)
2093 {
2094 /* Because C++ can put things into name space for free,
2095 constructs like "typedef struct foo { ... } foo"
2096 would look like an erroneous redeclaration. */
3bfdc719 2097 if (same_type_p (TREE_TYPE (newdecl), TREE_TYPE (olddecl)))
6aed477a 2098 return NULL;
8d08fdba 2099 else
2d01edd7 2100 return "redefinition of %q#D";
8d08fdba
MS
2101 }
2102 else if (TREE_CODE (newdecl) == FUNCTION_DECL)
2103 {
2104 /* If this is a pure function, its olddecl will actually be
2105 the original initialization to `0' (which we force to call
2106 abort()). Don't complain about redefinition in this case. */
d1fc3bbd
AO
2107 if (DECL_LANG_SPECIFIC (olddecl) && DECL_PURE_VIRTUAL_P (olddecl)
2108 && DECL_INITIAL (olddecl) == NULL_TREE)
6aed477a 2109 return NULL;
8d08fdba 2110
2c73f9f5 2111 /* If both functions come from different namespaces, this is not
a1c65f9f 2112 a redeclaration - this is a conflict with a used function. */
6eb3bb27 2113 if (DECL_NAMESPACE_SCOPE_P (olddecl)
713101a6
AO
2114 && DECL_CONTEXT (olddecl) != DECL_CONTEXT (newdecl)
2115 && ! decls_match (olddecl, newdecl))
2d01edd7 2116 return "%qD conflicts with used function";
2c73f9f5 2117
db5ae43f 2118 /* We'll complain about linkage mismatches in
0cbd7506 2119 warn_extern_redeclared_static. */
db5ae43f 2120
2c73f9f5 2121 /* Defining the same name twice is no good. */
8d08fdba 2122 if (DECL_INITIAL (olddecl) != NULL_TREE
faae18ab 2123 && DECL_INITIAL (newdecl) != NULL_TREE)
8d08fdba
MS
2124 {
2125 if (DECL_NAME (olddecl) == NULL_TREE)
2d01edd7 2126 return "%q#D not declared in class";
8d08fdba 2127 else
2d01edd7 2128 return "redefinition of %q#D";
8d08fdba 2129 }
6aed477a 2130 return NULL;
8d08fdba 2131 }
51c184be
MS
2132 else if (TREE_CODE (newdecl) == TEMPLATE_DECL)
2133 {
5bd61841
MM
2134 tree nt, ot;
2135
2136 if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL)
2137 {
2138 if (COMPLETE_TYPE_P (TREE_TYPE (newdecl))
2139 && COMPLETE_TYPE_P (TREE_TYPE (olddecl)))
2d01edd7 2140 return "redefinition of %q#D";
5bd61841
MM
2141 return NULL;
2142 }
2143
2144 if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) != FUNCTION_DECL
caf93cb0 2145 || (DECL_TEMPLATE_RESULT (newdecl)
5bd61841
MM
2146 == DECL_TEMPLATE_RESULT (olddecl)))
2147 return NULL;
2148
2149 nt = DECL_TEMPLATE_RESULT (newdecl);
2150 if (DECL_TEMPLATE_INFO (nt))
2151 nt = DECL_TEMPLATE_RESULT (template_for_substitution (nt));
2152 ot = DECL_TEMPLATE_RESULT (olddecl);
2153 if (DECL_TEMPLATE_INFO (ot))
2154 ot = DECL_TEMPLATE_RESULT (template_for_substitution (ot));
2155 if (DECL_INITIAL (nt) && DECL_INITIAL (ot))
2d01edd7 2156 return "redefinition of %q#D";
5bd61841 2157
9b7dd5e8
DG
2158 /* Core issue #226 (C++0x):
2159
2160 If a friend function template declaration specifies a
2161 default template-argument, that declaration shall be a
2162 definition and shall be the only declaration of the
2163 function template in the translation unit. */
c1ae8be5 2164 if ((cxx_dialect != cxx98)
9b7dd5e8
DG
2165 && TREE_CODE (ot) == FUNCTION_DECL && DECL_FRIEND_P (ot)
2166 && !check_default_tmpl_args (nt, DECL_TEMPLATE_PARMS (newdecl),
2167 /*is_primary=*/1, /*is_partial=*/0,
2168 /*is_friend_decl=*/2))
2169 return "redeclaration of friend %q#D may not have default template arguments";
2170
5bd61841 2171 return NULL;
51c184be 2172 }
31a79236 2173 else if (TREE_CODE (newdecl) == VAR_DECL
1799e5d5
RH
2174 && DECL_THREAD_LOCAL_P (newdecl) != DECL_THREAD_LOCAL_P (olddecl)
2175 && (! DECL_LANG_SPECIFIC (olddecl)
2176 || ! CP_DECL_THREADPRIVATE_P (olddecl)
2177 || DECL_THREAD_LOCAL_P (newdecl)))
31a79236
JJ
2178 {
2179 /* Only variables can be thread-local, and all declarations must
2180 agree on this property. */
2181 if (DECL_THREAD_LOCAL_P (newdecl))
2182 return "thread-local declaration of %q#D follows "
2183 "non-thread-local declaration";
2184 else
2185 return "non-thread-local declaration of %q#D follows "
2186 "thread-local declaration";
2187 }
1f51a992 2188 else if (toplevel_bindings_p () || DECL_NAMESPACE_SCOPE_P (newdecl))
8d08fdba 2189 {
555551c2
MM
2190 /* The objects have been declared at namespace scope. If either
2191 is a member of an anonymous union, then this is an invalid
2192 redeclaration. For example:
2193
2194 int i;
2195 union { int i; };
2196
2197 is invalid. */
2198 if (DECL_ANON_UNION_VAR_P (newdecl)
2199 || DECL_ANON_UNION_VAR_P (olddecl))
2200 return "redeclaration of %q#D";
2201 /* If at least one declaration is a reference, there is no
2202 conflict. For example:
2203
2204 int i = 3;
2205 extern int i;
2206
2207 is valid. */
8d08fdba 2208 if (DECL_EXTERNAL (newdecl) || DECL_EXTERNAL (olddecl))
6aed477a 2209 return NULL;
8926095f 2210 /* Reject two definitions. */
2d01edd7 2211 return "redefinition of %q#D";
8d08fdba
MS
2212 }
2213 else
2214 {
2215 /* Objects declared with block scope: */
2216 /* Reject two definitions, and reject a definition
2217 together with an external reference. */
2218 if (!(DECL_EXTERNAL (newdecl) && DECL_EXTERNAL (olddecl)))
2d01edd7 2219 return "redeclaration of %q#D";
6aed477a 2220 return NULL;
8d08fdba
MS
2221 }
2222}
2223\f
1799e5d5
RH
2224/* Hash and equality functions for the named_label table. */
2225
2226static hashval_t
2227named_label_entry_hash (const void *data)
2228{
2229 const struct named_label_entry *ent = (const struct named_label_entry *) data;
2230 return DECL_UID (ent->label_decl);
2231}
2232
2233static int
2234named_label_entry_eq (const void *a, const void *b)
2235{
2236 const struct named_label_entry *ent_a = (const struct named_label_entry *) a;
2237 const struct named_label_entry *ent_b = (const struct named_label_entry *) b;
2238 return ent_a->label_decl == ent_b->label_decl;
2239}
2240
acef433b 2241/* Create a new label, named ID. */
8d08fdba 2242
acef433b 2243static tree
11f6b451 2244make_label_decl (tree id, int local_p)
8d08fdba 2245{
1799e5d5
RH
2246 struct named_label_entry *ent;
2247 void **slot;
acef433b 2248 tree decl;
8d08fdba 2249
acef433b 2250 decl = build_decl (LABEL_DECL, id, void_type_node);
acef433b
MM
2251
2252 DECL_CONTEXT (decl) = current_function_decl;
2253 DECL_MODE (decl) = VOIDmode;
2254 C_DECLARED_LABEL_FLAG (decl) = local_p;
8d08fdba 2255
acef433b
MM
2256 /* Say where one reference is to the label, for the sake of the
2257 error if it is not defined. */
f31686a3 2258 DECL_SOURCE_LOCATION (decl) = input_location;
acef433b
MM
2259
2260 /* Record the fact that this identifier is bound to this label. */
2261 SET_IDENTIFIER_LABEL_VALUE (id, decl);
2262
1799e5d5
RH
2263 /* Create the label htab for the function on demand. */
2264 if (!named_labels)
2265 named_labels = htab_create_ggc (13, named_label_entry_hash,
2266 named_label_entry_eq, NULL);
6625cdb5 2267
1799e5d5
RH
2268 /* Record this label on the list of labels used in this function.
2269 We do this before calling make_label_decl so that we get the
2270 IDENTIFIER_LABEL_VALUE before the new label is declared. */
2271 ent = GGC_CNEW (struct named_label_entry);
2272 ent->label_decl = decl;
6625cdb5 2273
1799e5d5
RH
2274 slot = htab_find_slot (named_labels, ent, INSERT);
2275 gcc_assert (*slot == NULL);
2276 *slot = ent;
2277
2278 return decl;
acef433b 2279}
8d08fdba 2280
acef433b
MM
2281/* Look for a label named ID in the current function. If one cannot
2282 be found, create one. (We keep track of used, but undefined,
2283 labels, and complain about them at the end of a function.) */
8d08fdba 2284
68642fb6 2285tree
11f6b451 2286lookup_label (tree id)
acef433b
MM
2287{
2288 tree decl;
8d08fdba 2289
22ffcc6f 2290 timevar_push (TV_NAME_LOOKUP);
acef433b
MM
2291 /* You can't use labels at global scope. */
2292 if (current_function_decl == NULL_TREE)
2293 {
2d01edd7 2294 error ("label %qE referenced outside of any function", id);
22ffcc6f 2295 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
acef433b 2296 }
68642fb6 2297
acef433b
MM
2298 /* See if we've already got this label. */
2299 decl = IDENTIFIER_LABEL_VALUE (id);
2300 if (decl != NULL_TREE && DECL_CONTEXT (decl) == current_function_decl)
22ffcc6f 2301 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
8d08fdba 2302
acef433b 2303 decl = make_label_decl (id, /*local_p=*/0);
22ffcc6f 2304 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
8d08fdba
MS
2305}
2306
acef433b 2307/* Declare a local label named ID. */
8d08fdba
MS
2308
2309tree
11f6b451 2310declare_local_label (tree id)
8d08fdba 2311{
1799e5d5 2312 tree decl, shadow;
8d08fdba 2313
acef433b 2314 /* Add a new entry to the SHADOWED_LABELS list so that when we leave
1799e5d5
RH
2315 this scope we can restore the old value of IDENTIFIER_TYPE_VALUE. */
2316 shadow = tree_cons (IDENTIFIER_LABEL_VALUE (id), NULL_TREE,
2317 current_binding_level->shadowed_labels);
2318 current_binding_level->shadowed_labels = shadow;
2319
acef433b 2320 decl = make_label_decl (id, /*local_p=*/1);
1799e5d5 2321 TREE_VALUE (shadow) = decl;
68642fb6 2322
acef433b 2323 return decl;
8d08fdba
MS
2324}
2325
6625cdb5
JM
2326/* Returns nonzero if it is ill-formed to jump past the declaration of
2327 DECL. Returns 2 if it's also a real problem. */
2328
2329static int
11f6b451 2330decl_jump_unsafe (tree decl)
6625cdb5 2331{
b88b7ced
AP
2332 if (TREE_CODE (decl) != VAR_DECL || TREE_STATIC (decl)
2333 || TREE_TYPE (decl) == error_mark_node)
6625cdb5
JM
2334 return 0;
2335
6c06fbce
MM
2336 if (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl))
2337 || DECL_NONTRIVIALLY_INITIALIZED_P (decl))
2338 return 2;
2339
2340 if (pod_type_p (TREE_TYPE (decl)))
6625cdb5
JM
2341 return 0;
2342
6c06fbce 2343 /* The POD stuff is just pedantry; why should it matter if the class
6625cdb5 2344 contains a field of pointer to member type? */
6625cdb5
JM
2345 return 1;
2346}
2347
1799e5d5
RH
2348/* A subroutine of check_previous_goto_1 to identify a branch to the user. */
2349
2350static void
2351identify_goto (tree decl, const location_t *locus)
2352{
2353 if (decl)
2354 pedwarn ("jump to label %qD", decl);
2355 else
2356 pedwarn ("jump to case label");
2357 if (locus)
2358 pedwarn ("%H from here", locus);
2359}
2360
6625cdb5
JM
2361/* Check that a single previously seen jump to a newly defined label
2362 is OK. DECL is the LABEL_DECL or 0; LEVEL is the binding_level for
2363 the jump context; NAMES are the names in scope in LEVEL at the jump
3db45ab5 2364 context; LOCUS is the source position of the jump or 0. Returns
1799e5d5 2365 true if all is well. */
6625cdb5 2366
1799e5d5
RH
2367static bool
2368check_previous_goto_1 (tree decl, struct cp_binding_level* level, tree names,
2369 bool exited_omp, const location_t *locus)
2370{
2371 struct cp_binding_level *b;
2372 bool identified = false, saw_eh = false, saw_omp = false;
2373
2374 if (exited_omp)
2375 {
2376 identify_goto (decl, locus);
2377 error (" exits OpenMP structured block");
2378 identified = saw_omp = true;
2379 }
2380
2381 for (b = current_binding_level; b ; b = b->level_chain)
2382 {
2383 tree new_decls, old_decls = (b == level ? names : NULL_TREE);
2384
2385 for (new_decls = b->names; new_decls != old_decls;
6625cdb5
JM
2386 new_decls = TREE_CHAIN (new_decls))
2387 {
2388 int problem = decl_jump_unsafe (new_decls);
2389 if (! problem)
2390 continue;
2391
1799e5d5 2392 if (!identified)
6625cdb5 2393 {
1799e5d5
RH
2394 identify_goto (decl, locus);
2395 identified = true;
6625cdb5 2396 }
826840d9 2397 if (problem > 1)
dee15844 2398 error (" crosses initialization of %q+#D", new_decls);
6625cdb5 2399 else
dee15844 2400 pedwarn (" enters scope of non-POD %q+#D", new_decls);
6625cdb5
JM
2401 }
2402
2403 if (b == level)
2404 break;
1799e5d5 2405 if ((b->kind == sk_try || b->kind == sk_catch) && !saw_eh)
6625cdb5 2406 {
1799e5d5 2407 if (!identified)
6625cdb5 2408 {
1799e5d5
RH
2409 identify_goto (decl, locus);
2410 identified = true;
6625cdb5 2411 }
a7e8c268 2412 if (b->kind == sk_try)
826840d9
RH
2413 error (" enters try block");
2414 else
2415 error (" enters catch block");
1799e5d5
RH
2416 saw_eh = true;
2417 }
2418 if (b->kind == sk_omp && !saw_omp)
2419 {
2420 if (!identified)
2421 {
2422 identify_goto (decl, locus);
2423 identified = true;
2424 }
2425 error (" enters OpenMP structured block");
2426 saw_omp = true;
6625cdb5
JM
2427 }
2428 }
6625cdb5 2429
1799e5d5 2430 return !identified;
6625cdb5
JM
2431}
2432
2433static void
1799e5d5 2434check_previous_goto (tree decl, struct named_label_use_entry *use)
6625cdb5 2435{
1799e5d5
RH
2436 check_previous_goto_1 (decl, use->binding_level,
2437 use->names_in_scope, use->in_omp_scope,
2438 &use->o_goto_locus);
6625cdb5
JM
2439}
2440
1799e5d5
RH
2441static bool
2442check_switch_goto (struct cp_binding_level* level)
6625cdb5 2443{
1799e5d5 2444 return check_previous_goto_1 (NULL_TREE, level, level->names, false, NULL);
6625cdb5
JM
2445}
2446
2447/* Check that a new jump to a label DECL is OK. Called by
2448 finish_goto_stmt. */
2449
2450void
11f6b451 2451check_goto (tree decl)
6625cdb5 2452{
1799e5d5
RH
2453 struct named_label_entry *ent, dummy;
2454 bool saw_catch = false, identified = false;
6625cdb5 2455 tree bad;
6625cdb5 2456
1799e5d5
RH
2457 /* We can't know where a computed goto is jumping.
2458 So we assume that it's OK. */
2459 if (TREE_CODE (decl) != LABEL_DECL)
2460 return;
2461
2462 /* We didn't record any information about this label when we created it,
2463 and there's not much point since it's trivial to analyze as a return. */
2464 if (decl == cdtor_label)
e3cd9945
APB
2465 return;
2466
1799e5d5
RH
2467 dummy.label_decl = decl;
2468 ent = (struct named_label_entry *) htab_find (named_labels, &dummy);
2469 gcc_assert (ent != NULL);
2470
6625cdb5
JM
2471 /* If the label hasn't been defined yet, defer checking. */
2472 if (! DECL_INITIAL (decl))
2473 {
1799e5d5 2474 struct named_label_use_entry *new_use;
6625cdb5 2475
1799e5d5
RH
2476 /* Don't bother creating another use if the last goto had the
2477 same data, and will therefore create the same set of errors. */
2478 if (ent->uses
2479 && ent->uses->names_in_scope == current_binding_level->names)
2480 return;
6625cdb5 2481
1799e5d5
RH
2482 new_use = GGC_NEW (struct named_label_use_entry);
2483 new_use->binding_level = current_binding_level;
2484 new_use->names_in_scope = current_binding_level->names;
2485 new_use->o_goto_locus = input_location;
2486 new_use->in_omp_scope = false;
2487
2488 new_use->next = ent->uses;
2489 ent->uses = new_use;
2490 return;
2491 }
6625cdb5 2492
1799e5d5
RH
2493 if (ent->in_try_scope || ent->in_catch_scope
2494 || ent->in_omp_scope || ent->bad_decls)
6625cdb5 2495 {
dee15844 2496 pedwarn ("jump to label %q+D", decl);
6625cdb5 2497 pedwarn (" from here");
1799e5d5 2498 identified = true;
6625cdb5
JM
2499 }
2500
1799e5d5 2501 for (bad = ent->bad_decls; bad; bad = TREE_CHAIN (bad))
6625cdb5
JM
2502 {
2503 tree b = TREE_VALUE (bad);
2504 int u = decl_jump_unsafe (b);
2505
2506 if (u > 1 && DECL_ARTIFICIAL (b))
1799e5d5
RH
2507 {
2508 /* Can't skip init of __exception_info. */
2509 error ("%J enters catch block", b);
2510 saw_catch = true;
2511 }
6625cdb5 2512 else if (u > 1)
dee15844 2513 error (" skips initialization of %q+#D", b);
6625cdb5 2514 else
dee15844 2515 pedwarn (" enters scope of non-POD %q+#D", b);
6625cdb5
JM
2516 }
2517
1799e5d5 2518 if (ent->in_try_scope)
6625cdb5 2519 error (" enters try block");
1799e5d5 2520 else if (ent->in_catch_scope && !saw_catch)
826840d9 2521 error (" enters catch block");
1799e5d5
RH
2522
2523 if (ent->in_omp_scope)
2524 error (" enters OpenMP structured block");
2525 else if (flag_openmp)
2526 {
2527 struct cp_binding_level *b;
2528 for (b = current_binding_level; b ; b = b->level_chain)
2529 {
2530 if (b == ent->binding_level)
2531 break;
2532 if (b->kind == sk_omp)
2533 {
2534 if (!identified)
2535 {
2536 pedwarn ("jump to label %q+D", decl);
2537 pedwarn (" from here");
2538 identified = true;
2539 }
2540 error (" exits OpenMP structured block");
2541 break;
2542 }
2543 }
2544 }
2545}
2546
2547/* Check that a return is ok wrt OpenMP structured blocks.
2548 Called by finish_return_stmt. Returns true if all is well. */
2549
2550bool
2551check_omp_return (void)
2552{
2553 struct cp_binding_level *b;
2554 for (b = current_binding_level; b ; b = b->level_chain)
2555 if (b->kind == sk_omp)
2556 {
2557 error ("invalid exit from OpenMP structured block");
2558 return false;
2559 }
2560 return true;
6625cdb5
JM
2561}
2562
8d08fdba 2563/* Define a label, specifying the location in the source file.
b9c87401 2564 Return the LABEL_DECL node for the label. */
8d08fdba
MS
2565
2566tree
5b030314 2567define_label (location_t location, tree name)
8d08fdba 2568{
1799e5d5 2569 struct named_label_entry *ent, dummy;
926ce8bd 2570 struct cp_binding_level *p;
1799e5d5 2571 tree decl;
6625cdb5 2572
22ffcc6f 2573 timevar_push (TV_NAME_LOOKUP);
1799e5d5
RH
2574
2575 decl = lookup_label (name);
2576
2577 dummy.label_decl = decl;
2578 ent = (struct named_label_entry *) htab_find (named_labels, &dummy);
2579 gcc_assert (ent != NULL);
8d08fdba 2580
3dc9aec6 2581 /* After labels, make any new cleanups in the function go into their
8d08fdba 2582 own new (temporary) binding contour. */
caf93cb0
EC
2583 for (p = current_binding_level;
2584 p->kind != sk_function_parms;
a7e8c268 2585 p = p->level_chain)
3dc9aec6 2586 p->more_cleanups_ok = 0;
8d08fdba 2587
e1cd6e56 2588 if (name == get_identifier ("wchar_t"))
33bd39a2 2589 pedwarn ("label named wchar_t");
e1cd6e56 2590
8d08fdba 2591 if (DECL_INITIAL (decl) != NULL_TREE)
417fa55b
LM
2592 {
2593 error ("duplicate label %qD", decl);
2594 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
2595 }
8d08fdba
MS
2596 else
2597 {
1799e5d5
RH
2598 struct named_label_use_entry *use;
2599
8d08fdba
MS
2600 /* Mark label as having been defined. */
2601 DECL_INITIAL (decl) = error_mark_node;
2602 /* Say where in the source. */
f31686a3 2603 DECL_SOURCE_LOCATION (decl) = location;
1799e5d5
RH
2604
2605 ent->binding_level = current_binding_level;
2606 ent->names_in_scope = current_binding_level->names;
2607
2608 for (use = ent->uses; use ; use = use->next)
2609 check_previous_goto (decl, use);
2610 ent->uses = NULL;
8d08fdba 2611 }
b9c87401 2612
be99edf8 2613 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
8d08fdba
MS
2614}
2615
a5894242
MS
2616struct cp_switch
2617{
e2500fed 2618 struct cp_binding_level *level;
a5894242 2619 struct cp_switch *next;
56cb9733
MM
2620 /* The SWITCH_STMT being built. */
2621 tree switch_stmt;
2622 /* A splay-tree mapping the low element of a case range to the high
2623 element, or NULL_TREE if there is no high element. Used to
2624 determine whether or not a new case label duplicates an old case
2625 label. We need a tree, rather than simply a hash table, because
2626 of the GNU case range extension. */
2627 splay_tree cases;
a5894242
MS
2628};
2629
56cb9733
MM
2630/* A stack of the currently active switch statements. The innermost
2631 switch statement is on the top of the stack. There is no need to
2632 mark the stack for garbage collection because it is only active
2633 during the processing of the body of a function, and we never
2634 collect at that point. */
5362b086 2635
a5894242
MS
2636static struct cp_switch *switch_stack;
2637
56cb9733
MM
2638/* Called right after a switch-statement condition is parsed.
2639 SWITCH_STMT is the switch statement being parsed. */
2640
a5894242 2641void
11f6b451 2642push_switch (tree switch_stmt)
a5894242 2643{
0ac1b889 2644 struct cp_switch *p = XNEW (struct cp_switch);
a5894242
MS
2645 p->level = current_binding_level;
2646 p->next = switch_stack;
56cb9733
MM
2647 p->switch_stmt = switch_stmt;
2648 p->cases = splay_tree_new (case_compare, NULL, NULL);
a5894242
MS
2649 switch_stack = p;
2650}
2651
2652void
11f6b451 2653pop_switch (void)
a5894242 2654{
6de9cd9a 2655 struct cp_switch *cs = switch_stack;
fbc315db 2656 location_t switch_location;
6de9cd9a
DN
2657
2658 /* Emit warnings as needed. */
fbc315db
ILT
2659 if (EXPR_HAS_LOCATION (cs->switch_stmt))
2660 switch_location = EXPR_LOCATION (cs->switch_stmt);
2661 else
2662 switch_location = input_location;
dddf9a0a
AP
2663 if (!processing_template_decl)
2664 c_do_switch_warnings (cs->cases, switch_location,
2665 SWITCH_STMT_TYPE (cs->switch_stmt),
2666 SWITCH_STMT_COND (cs->switch_stmt));
5362b086 2667
56cb9733 2668 splay_tree_delete (cs->cases);
a5894242 2669 switch_stack = switch_stack->next;
bedda2da 2670 free (cs);
a5894242
MS
2671}
2672
b0a1da19
JM
2673/* Note that we've seen a definition of a case label, and complain if this
2674 is a bad place for one. */
e92cc029 2675
3e4d04a1 2676tree
11f6b451 2677finish_case_label (tree low_value, tree high_value)
8d08fdba 2678{
3e4d04a1 2679 tree cond, r;
926ce8bd 2680 struct cp_binding_level *p;
a5894242 2681
56cb9733
MM
2682 if (processing_template_decl)
2683 {
8f17b5c5
MM
2684 tree label;
2685
56cb9733
MM
2686 /* For templates, just add the case label; we'll do semantic
2687 analysis at instantiation-time. */
8f17b5c5 2688 label = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE);
3e4d04a1 2689 return add_stmt (build_case_label (low_value, high_value, label));
56cb9733
MM
2690 }
2691
2692 /* Find the condition on which this switch statement depends. */
ebaae582 2693 cond = SWITCH_STMT_COND (switch_stack->switch_stmt);
56cb9733
MM
2694 if (cond && TREE_CODE (cond) == TREE_LIST)
2695 cond = TREE_VALUE (cond);
56cb9733 2696
1799e5d5
RH
2697 if (!check_switch_goto (switch_stack->level))
2698 return error_mark_node;
2699
a6c0a76c
SB
2700 r = c_add_case_label (switch_stack->cases, cond, TREE_TYPE (cond),
2701 low_value, high_value);
8d08fdba 2702
3dc9aec6 2703 /* After labels, make any new cleanups in the function go into their
8d08fdba 2704 own new (temporary) binding contour. */
caf93cb0
EC
2705 for (p = current_binding_level;
2706 p->kind != sk_function_parms;
a7e8c268 2707 p = p->level_chain)
3dc9aec6 2708 p->more_cleanups_ok = 0;
3e4d04a1
RH
2709
2710 return r;
8d08fdba
MS
2711}
2712\f
7ddedda4
MM
2713/* Hash a TYPENAME_TYPE. K is really of type `tree'. */
2714
e2500fed 2715static hashval_t
11f6b451 2716typename_hash (const void* k)
7ddedda4 2717{
e2500fed
GK
2718 hashval_t hash;
2719 tree t = (tree) k;
7ddedda4 2720
7bdfd72e
KG
2721 hash = (htab_hash_pointer (TYPE_CONTEXT (t))
2722 ^ htab_hash_pointer (DECL_NAME (TYPE_NAME (t))));
7ddedda4
MM
2723
2724 return hash;
2725}
2726
fc6a28d7
MM
2727typedef struct typename_info {
2728 tree scope;
2729 tree name;
2730 tree template_id;
2731 bool enum_p;
2732 bool class_p;
2733} typename_info;
2734
06d40de8
DG
2735/* Compare two TYPENAME_TYPEs. K1 is really of type `tree', K2 is
2736 really of type `typename_info*' */
7ddedda4 2737
e2500fed 2738static int
11f6b451 2739typename_compare (const void * k1, const void * k2)
7ddedda4
MM
2740{
2741 tree t1;
fc6a28d7 2742 const typename_info *t2;
7ddedda4
MM
2743
2744 t1 = (tree) k1;
fc6a28d7
MM
2745 t2 = (const typename_info *) k2;
2746
2747 return (DECL_NAME (TYPE_NAME (t1)) == t2->name
2748 && TYPE_CONTEXT (t1) == t2->scope
2749 && TYPENAME_TYPE_FULLNAME (t1) == t2->template_id
2750 && TYPENAME_IS_ENUM_P (t1) == t2->enum_p
2751 && TYPENAME_IS_CLASS_P (t1) == t2->class_p);
7ddedda4
MM
2752}
2753
45869a6c 2754/* Build a TYPENAME_TYPE. If the type is `typename T::t', CONTEXT is
fc6a28d7 2755 the type of `T', NAME is the IDENTIFIER_NODE for `t'.
c8094d83 2756
45869a6c
MM
2757 Returns the new TYPENAME_TYPE. */
2758
e2500fed
GK
2759static GTY ((param_is (union tree_node))) htab_t typename_htab;
2760
8ce33230 2761static tree
fc6a28d7
MM
2762build_typename_type (tree context, tree name, tree fullname,
2763 enum tag_types tag_type)
45869a6c
MM
2764{
2765 tree t;
2766 tree d;
fc6a28d7 2767 typename_info ti;
fad205ff 2768 void **e;
fc6a28d7 2769 hashval_t hash;
45869a6c 2770
e2500fed 2771 if (typename_htab == NULL)
fc6a28d7
MM
2772 typename_htab = htab_create_ggc (61, &typename_hash,
2773 &typename_compare, NULL);
2774
c8094d83 2775 ti.scope = FROB_CONTEXT (context);
fc6a28d7
MM
2776 ti.name = name;
2777 ti.template_id = fullname;
2778 ti.enum_p = tag_type == enum_type;
2779 ti.class_p = (tag_type == class_type
2780 || tag_type == record_type
2781 || tag_type == union_type);
2782 hash = (htab_hash_pointer (ti.scope)
2783 ^ htab_hash_pointer (ti.name));
45869a6c 2784
7ddedda4 2785 /* See if we already have this type. */
fc6a28d7 2786 e = htab_find_slot_with_hash (typename_htab, &ti, hash, INSERT);
e2500fed
GK
2787 if (*e)
2788 t = (tree) *e;
7ddedda4 2789 else
fc6a28d7
MM
2790 {
2791 /* Build the TYPENAME_TYPE. */
2792 t = make_aggr_type (TYPENAME_TYPE);
2793 TYPE_CONTEXT (t) = ti.scope;
2794 TYPENAME_TYPE_FULLNAME (t) = ti.template_id;
2795 TYPENAME_IS_ENUM_P (t) = ti.enum_p;
2796 TYPENAME_IS_CLASS_P (t) = ti.class_p;
c8094d83 2797
fc6a28d7
MM
2798 /* Build the corresponding TYPE_DECL. */
2799 d = build_decl (TYPE_DECL, name, t);
2800 TYPE_NAME (TREE_TYPE (d)) = d;
2801 TYPE_STUB_DECL (TREE_TYPE (d)) = d;
2802 DECL_CONTEXT (d) = FROB_CONTEXT (context);
2803 DECL_ARTIFICIAL (d) = 1;
2804
2805 /* Store it in the hash table. */
2806 *e = t;
06d40de8
DG
2807
2808 /* TYPENAME_TYPEs must always be compared structurally, because
2809 they may or may not resolve down to another type depending on
2810 the currently open classes. */
2811 SET_TYPE_STRUCTURAL_EQUALITY (t);
fc6a28d7 2812 }
c8094d83 2813
45869a6c
MM
2814 return t;
2815}
2816
fc6a28d7
MM
2817/* Resolve `typename CONTEXT::NAME'. TAG_TYPE indicates the tag
2818 provided to name the type. Returns an appropriate type, unless an
2819 error occurs, in which case error_mark_node is returned. If we
2820 locate a non-artificial TYPE_DECL and TF_KEEP_TYPE_DECL is set, we
2821 return that, rather than the _TYPE it corresponds to, in other
2822 cases we look through the type decl. If TF_ERROR is set, complain
2823 about errors, otherwise be quiet. */
3baa501d 2824
5566b478 2825tree
fc6a28d7
MM
2826make_typename_type (tree context, tree name, enum tag_types tag_type,
2827 tsubst_flags_t complain)
5566b478 2828{
b2b7d40a 2829 tree fullname;
7d3bec9d
NS
2830 tree t;
2831 bool want_template;
a80e4195 2832
5dc5d13c
KL
2833 if (name == error_mark_node
2834 || context == NULL_TREE
2835 || context == error_mark_node)
2836 return error_mark_node;
2837
2f939d94 2838 if (TYPE_P (name))
78638e24 2839 {
68642fb6
UD
2840 if (!(TYPE_LANG_SPECIFIC (name)
2841 && (CLASSTYPE_IS_TEMPLATE (name)
78638e24
MM
2842 || CLASSTYPE_USE_TEMPLATE (name))))
2843 name = TYPE_IDENTIFIER (name);
2844 else
2845 /* Create a TEMPLATE_ID_EXPR for the type. */
2846 name = build_nt (TEMPLATE_ID_EXPR,
2847 CLASSTYPE_TI_TEMPLATE (name),
2848 CLASSTYPE_TI_ARGS (name));
2849 }
653cc74a 2850 else if (TREE_CODE (name) == TYPE_DECL)
a80e4195 2851 name = DECL_NAME (name);
b2b7d40a
JM
2852
2853 fullname = name;
2854
2855 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
11686454
JM
2856 {
2857 name = TREE_OPERAND (name, 0);
2858 if (TREE_CODE (name) == TEMPLATE_DECL)
2859 name = TREE_OPERAND (fullname, 0) = DECL_NAME (name);
50ef2c18
MM
2860 else if (TREE_CODE (name) == OVERLOAD)
2861 {
2862 error ("%qD is not a type", name);
2863 return error_mark_node;
2864 }
11686454 2865 }
8a2b77e7
JM
2866 if (TREE_CODE (name) == TEMPLATE_DECL)
2867 {
2d01edd7 2868 error ("%qD used without template parameters", name);
8a2b77e7
JM
2869 return error_mark_node;
2870 }
50bc768d 2871 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
0c88d886 2872 gcc_assert (TYPE_P (context));
04ddee1b 2873
7d3bec9d
NS
2874 /* When the CONTEXT is a dependent type, NAME could refer to a
2875 dependent base class of CONTEXT. So we cannot peek inside it,
2876 even if CONTEXT is a currently open scope. */
2877 if (dependent_type_p (context))
2878 return build_typename_type (context, name, fullname, tag_type);
caf93cb0 2879
7d3bec9d
NS
2880 if (!IS_AGGR_TYPE (context))
2881 {
2882 if (complain & tf_error)
2883 error ("%q#T is not a class", context);
2884 return error_mark_node;
5566b478 2885 }
7d3bec9d
NS
2886
2887 want_template = TREE_CODE (fullname) == TEMPLATE_ID_EXPR;
2888
2889 /* We should only set WANT_TYPE when we're a nested typename type.
2890 Then we can give better diagnostics if we find a non-type. */
2891 t = lookup_field (context, name, 0, /*want_type=*/true);
2892 if (!t)
11249cf0 2893 {
4f2b0fb2 2894 if (complain & tf_error)
7d3bec9d
NS
2895 error (want_template ? "no class template named %q#T in %q#T"
2896 : "no type named %q#T in %q#T", name, context);
11249cf0
MM
2897 return error_mark_node;
2898 }
7d3bec9d
NS
2899
2900 if (want_template && !DECL_CLASS_TEMPLATE_P (t))
2901 {
2902 if (complain & tf_error)
2903 error ("%<typename %T::%D%> names %q#T, which is not a class template",
2904 context, name, t);
2905 return error_mark_node;
2906 }
2907 if (!want_template && TREE_CODE (t) != TYPE_DECL)
2908 {
2909 if (complain & tf_error)
2910 error ("%<typename %T::%D%> names %q#T, which is not a type",
2911 context, name, t);
2912 return error_mark_node;
2913 }
2914
2915 if (complain & tf_error)
02022f3a 2916 perform_or_defer_access_check (TYPE_BINFO (context), t, t);
7d3bec9d
NS
2917
2918 if (want_template)
2919 return lookup_template_class (t, TREE_OPERAND (fullname, 1),
2920 NULL_TREE, context,
2921 /*entering_scope=*/0,
2922 tf_warning_or_error | tf_user);
2923
2924 if (DECL_ARTIFICIAL (t) || !(complain & tf_keep_type_decl))
2925 t = TREE_TYPE (t);
2926
2927 return t;
5566b478
MS
2928}
2929
b939a023 2930/* Resolve `CONTEXT::template NAME'. Returns a TEMPLATE_DECL if the name
c8094d83 2931 can be resolved or an UNBOUND_CLASS_TEMPLATE, unless an error occurs,
b939a023
KL
2932 in which case error_mark_node is returned.
2933
2934 If PARM_LIST is non-NULL, also make sure that the template parameter
2935 list of TEMPLATE_DECL matches.
2936
2937 If COMPLAIN zero, don't complain about any errors that occur. */
b8c6534b
KL
2938
2939tree
b939a023
KL
2940make_unbound_class_template (tree context, tree name, tree parm_list,
2941 tsubst_flags_t complain)
b8c6534b
KL
2942{
2943 tree t;
2944 tree d;
2945
2946 if (TYPE_P (name))
2947 name = TYPE_IDENTIFIER (name);
2948 else if (DECL_P (name))
2949 name = DECL_NAME (name);
8dc2b103 2950 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
b8c6534b 2951
9579624e 2952 if (!dependent_type_p (context)
b8c6534b
KL
2953 || currently_open_class (context))
2954 {
2955 tree tmpl = NULL_TREE;
2956
2957 if (IS_AGGR_TYPE (context))
86ac0575 2958 tmpl = lookup_field (context, name, 0, false);
b8c6534b
KL
2959
2960 if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl))
2961 {
4f09be91 2962 if (complain & tf_error)
2d01edd7 2963 error ("no class template named %q#T in %q#T", name, context);
b8c6534b
KL
2964 return error_mark_node;
2965 }
caf93cb0 2966
b939a023
KL
2967 if (parm_list
2968 && !comp_template_parms (DECL_TEMPLATE_PARMS (tmpl), parm_list))
2969 {
2970 if (complain & tf_error)
2971 {
2972 error ("template parameters do not match template");
dee15844 2973 error ("%q+D declared here", tmpl);
b939a023
KL
2974 }
2975 return error_mark_node;
2976 }
2977
2b59fc25 2978 if (complain & tf_error)
02022f3a 2979 perform_or_defer_access_check (TYPE_BINFO (context), tmpl, tmpl);
b8c6534b
KL
2980
2981 return tmpl;
2982 }
2983
2984 /* Build the UNBOUND_CLASS_TEMPLATE. */
2985 t = make_aggr_type (UNBOUND_CLASS_TEMPLATE);
2986 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
2987 TREE_TYPE (t) = NULL_TREE;
06d40de8 2988 SET_TYPE_STRUCTURAL_EQUALITY (t);
b8c6534b
KL
2989
2990 /* Build the corresponding TEMPLATE_DECL. */
2991 d = build_decl (TEMPLATE_DECL, name, t);
2992 TYPE_NAME (TREE_TYPE (d)) = d;
2993 TYPE_STUB_DECL (TREE_TYPE (d)) = d;
2994 DECL_CONTEXT (d) = FROB_CONTEXT (context);
2995 DECL_ARTIFICIAL (d) = 1;
b939a023 2996 DECL_TEMPLATE_PARMS (d) = parm_list;
b8c6534b
KL
2997
2998 return t;
2999}
3000
8d08fdba 3001\f
47aa0df4 3002
8d08fdba 3003/* Push the declarations of builtin types into the namespace.
4b0d3cbe
MM
3004 RID_INDEX is the index of the builtin type in the array
3005 RID_POINTERS. NAME is the name used when looking up the builtin
3006 type. TYPE is the _TYPE node for the builtin type. */
8d08fdba 3007
eaa7c03f 3008void
caf93cb0 3009record_builtin_type (enum rid rid_index,
0cbd7506
MS
3010 const char* name,
3011 tree type)
8d08fdba
MS
3012{
3013 tree rname = NULL_TREE, tname = NULL_TREE;
a703fb38 3014 tree tdecl = NULL_TREE;
8d08fdba 3015
0e5921e8 3016 if ((int) rid_index < (int) RID_MAX)
8d08fdba
MS
3017 rname = ridpointers[(int) rid_index];
3018 if (name)
3019 tname = get_identifier (name);
3020
4b0d3cbe
MM
3021 /* The calls to SET_IDENTIFIER_GLOBAL_VALUE below should be
3022 eliminated. Built-in types should not be looked up name; their
3023 names are keywords that the parser can recognize. However, there
3024 is code in c-common.c that uses identifier_global_value to look
3025 up built-in types by name. */
8d08fdba
MS
3026 if (tname)
3027 {
4b0d3cbe
MM
3028 tdecl = build_decl (TYPE_DECL, tname, type);
3029 DECL_ARTIFICIAL (tdecl) = 1;
a7e8c268 3030 SET_IDENTIFIER_GLOBAL_VALUE (tname, tdecl);
8d08fdba 3031 }
4b0d3cbe 3032 if (rname)
8d08fdba 3033 {
4b0d3cbe 3034 if (!tdecl)
8d08fdba 3035 {
4b0d3cbe
MM
3036 tdecl = build_decl (TYPE_DECL, rname, type);
3037 DECL_ARTIFICIAL (tdecl) = 1;
8d08fdba 3038 }
4b0d3cbe 3039 SET_IDENTIFIER_GLOBAL_VALUE (rname, tdecl);
8d08fdba 3040 }
4b0d3cbe
MM
3041
3042 if (!TYPE_NAME (type))
3043 TYPE_NAME (type) = tdecl;
47aa0df4
MM
3044
3045 if (tdecl)
21d13d83 3046 debug_hooks->type_decl (tdecl, 0);
8d08fdba
MS
3047}
3048
eff71ab0 3049/* Record one of the standard Java types.
4d8a1dd6
MM
3050 * Declare it as having the given NAME.
3051 * If SIZE > 0, it is the size of one of the integral types;
3052 * otherwise it is the negative of the size of one of the other types. */
eff71ab0
PB
3053
3054static tree
11f6b451 3055record_builtin_java_type (const char* name, int size)
eff71ab0
PB
3056{
3057 tree type, decl;
3058 if (size > 0)
3059 type = make_signed_type (size);
3060 else if (size > -32)
a1c65f9f 3061 { /* "__java_char" or ""__java_boolean". */
eff71ab0
PB
3062 type = make_unsigned_type (-size);
3063 /*if (size == -1) TREE_SET_CODE (type, BOOLEAN_TYPE);*/
3064 }
3065 else
a1c65f9f 3066 { /* "__java_float" or ""__java_double". */
eff71ab0
PB
3067 type = make_node (REAL_TYPE);
3068 TYPE_PRECISION (type) = - size;
3069 layout_type (type);
3070 }
0e5921e8 3071 record_builtin_type (RID_MAX, name, type);
eff71ab0 3072 decl = TYPE_NAME (type);
e229f2cd
PB
3073
3074 /* Suppress generate debug symbol entries for these types,
3075 since for normal C++ they are just clutter.
a1c65f9f 3076 However, push_lang_context undoes this if extern "Java" is seen. */
eff71ab0 3077 DECL_IGNORED_P (decl) = 1;
e229f2cd 3078
eff71ab0
PB
3079 TYPE_FOR_JAVA (type) = 1;
3080 return type;
3081}
3082
3b426391 3083/* Push a type into the namespace so that the back ends ignore it. */
036407f7
ML
3084
3085static void
11f6b451 3086record_unknown_type (tree type, const char* name)
036407f7
ML
3087{
3088 tree decl = pushdecl (build_decl (TYPE_DECL, get_identifier (name), type));
3089 /* Make sure the "unknown type" typedecl gets ignored for debug info. */
3090 DECL_IGNORED_P (decl) = 1;
3091 TYPE_DECL_SUPPRESS_DEBUG (decl) = 1;
3092 TYPE_SIZE (type) = TYPE_SIZE (void_type_node);
3093 TYPE_ALIGN (type) = 1;
11cf4d18 3094 TYPE_USER_ALIGN (type) = 0;
036407f7 3095 TYPE_MODE (type) = TYPE_MODE (void_type_node);
68642fb6 3096}
036407f7 3097
13a44ee0 3098/* A string for which we should create an IDENTIFIER_NODE at
d43829f9
MM
3099 startup. */
3100
3101typedef struct predefined_identifier
3102{
3103 /* The name of the identifier. */
8b60264b 3104 const char *const name;
d43829f9 3105 /* The place where the IDENTIFIER_NODE should be stored. */
8b60264b 3106 tree *const node;
838dfd8a 3107 /* Nonzero if this is the name of a constructor or destructor. */
8b60264b 3108 const int ctor_or_dtor_p;
d43829f9
MM
3109} predefined_identifier;
3110
3111/* Create all the predefined identifiers. */
3112
3113static void
11f6b451 3114initialize_predefined_identifiers (void)
d43829f9 3115{
8b60264b 3116 const predefined_identifier *pid;
d43829f9
MM
3117
3118 /* A table of identifiers to create at startup. */
8b60264b 3119 static const predefined_identifier predefined_identifiers[] = {
298d6f60
MM
3120 { "C++", &lang_name_cplusplus, 0 },
3121 { "C", &lang_name_c, 0 },
3122 { "Java", &lang_name_java, 0 },
d6eec208
MM
3123 /* Some of these names have a trailing space so that it is
3124 impossible for them to conflict with names written by users. */
3125 { "__ct ", &ctor_identifier, 1 },
3126 { "__base_ctor ", &base_ctor_identifier, 1 },
3127 { "__comp_ctor ", &complete_ctor_identifier, 1 },
3128 { "__dt ", &dtor_identifier, 1 },
3129 { "__comp_dtor ", &complete_dtor_identifier, 1 },
3130 { "__base_dtor ", &base_dtor_identifier, 1 },
3131 { "__deleting_dtor ", &deleting_dtor_identifier, 1 },
298d6f60 3132 { IN_CHARGE_NAME, &in_charge_identifier, 0 },
298d6f60
MM
3133 { "nelts", &nelts_identifier, 0 },
3134 { THIS_NAME, &this_identifier, 0 },
c4372ef4 3135 { VTABLE_DELTA_NAME, &delta_identifier, 0 },
298d6f60 3136 { VTABLE_PFN_NAME, &pfn_identifier, 0 },
298d6f60 3137 { "_vptr", &vptr_identifier, 0 },
3ec6bad3 3138 { "__vtt_parm", &vtt_parm_identifier, 0 },
82a2669e 3139 { "::", &global_scope_name, 0 },
1f6e1acc 3140 { "std", &std_identifier, 0 },
298d6f60 3141 { NULL, NULL, 0 }
d43829f9
MM
3142 };
3143
3144 for (pid = predefined_identifiers; pid->name; ++pid)
298d6f60
MM
3145 {
3146 *pid->node = get_identifier (pid->name);
3147 if (pid->ctor_or_dtor_p)
3148 IDENTIFIER_CTOR_OR_DTOR_P (*pid->node) = 1;
3149 }
d43829f9
MM
3150}
3151
8d08fdba
MS
3152/* Create the predefined scalar types of C,
3153 and some nodes representing standard constants (0, 1, (void *)0).
3154 Initialize the global binding level.
3155 Make definitions for built-in primitive functions. */
3156
3157void
11f6b451 3158cxx_init_decl_processing (void)
8d08fdba 3159{
10841285
MM
3160 tree void_ftype;
3161 tree void_ftype_ptr;
8d08fdba 3162
8c1d6d62 3163 build_common_tree_nodes (flag_signed_char, false);
6de66680 3164
d43829f9
MM
3165 /* Create all the identifiers we need. */
3166 initialize_predefined_identifiers ();
8d08fdba 3167
9cd64686
MM
3168 /* Create the global variables. */
3169 push_to_top_level ();
8012c983 3170
82a2669e 3171 current_function_decl = NULL_TREE;
1ec57cf0 3172 current_binding_level = NULL;
a1c65f9f 3173 /* Enter the global namespace. */
50bc768d 3174 gcc_assert (global_namespace == NULL_TREE);
82a2669e 3175 global_namespace = build_lang_decl (NAMESPACE_DECL, global_scope_name,
0cbd7506 3176 void_type_node);
b9e75696 3177 TREE_PUBLIC (global_namespace) = 1;
ac20c67a 3178 begin_scope (sk_namespace, global_namespace);
82a2669e 3179
30394414
JM
3180 current_lang_name = NULL_TREE;
3181
ddbfd28d
GK
3182 if (flag_visibility_ms_compat)
3183 default_visibility = VISIBILITY_HIDDEN;
3184
8d08fdba
MS
3185 /* Initially, C. */
3186 current_lang_name = lang_name_c;
3187
6bcedb4e 3188 /* Create the `std' namespace. */
1dbb6023
NS
3189 push_namespace (std_identifier);
3190 std_node = current_namespace;
3191 pop_namespace ();
5362b086 3192
eaa7c03f 3193 c_common_nodes_and_builtins ();
37c46b43 3194
4d8a1dd6
MM
3195 java_byte_type_node = record_builtin_java_type ("__java_byte", 8);
3196 java_short_type_node = record_builtin_java_type ("__java_short", 16);
3197 java_int_type_node = record_builtin_java_type ("__java_int", 32);
3198 java_long_type_node = record_builtin_java_type ("__java_long", 64);
3199 java_float_type_node = record_builtin_java_type ("__java_float", -32);
3200 java_double_type_node = record_builtin_java_type ("__java_double", -64);
3201 java_char_type_node = record_builtin_java_type ("__java_char", -16);
3202 java_boolean_type_node = record_builtin_java_type ("__java_boolean", -1);
eff71ab0 3203
7d60be94
NS
3204 integer_two_node = build_int_cst (NULL_TREE, 2);
3205 integer_three_node = build_int_cst (NULL_TREE, 3);
8d08fdba 3206
255512c1 3207 record_builtin_type (RID_BOOL, "bool", boolean_type_node);
de7df9eb
JM
3208 truthvalue_type_node = boolean_type_node;
3209 truthvalue_false_node = boolean_false_node;
3210 truthvalue_true_node = boolean_true_node;
2986ae00 3211
4cc1d462 3212 empty_except_spec = build_tree_list (NULL_TREE, NULL_TREE);
5fd8e536
JM
3213
3214#if 0
3e411c3f 3215 record_builtin_type (RID_MAX, NULL, string_type_node);
5fd8e536
JM
3216#endif
3217
1f84ec23
MM
3218 delta_type_node = ptrdiff_type_node;
3219 vtable_index_type = ptrdiff_type_node;
c7e266a6 3220
3ec6bad3 3221 vtt_parm_type = build_pointer_type (const_ptr_type_node);
10841285
MM
3222 void_ftype = build_function_type (void_type_node, void_list_node);
3223 void_ftype_ptr = build_function_type (void_type_node,
3224 tree_cons (NULL_TREE,
caf93cb0 3225 ptr_type_node,
10841285 3226 void_list_node));
824b9a4c 3227 void_ftype_ptr
4cc1d462 3228 = build_exception_variant (void_ftype_ptr, empty_except_spec);
8d08fdba 3229
8d08fdba
MS
3230 /* C++ extensions */
3231
3232 unknown_type_node = make_node (UNKNOWN_TYPE);
036407f7
ML
3233 record_unknown_type (unknown_type_node, "unknown type");
3234
8d08fdba
MS
3235 /* Indirecting an UNKNOWN_TYPE node yields an UNKNOWN_TYPE node. */
3236 TREE_TYPE (unknown_type_node) = unknown_type_node;
a6967cc0 3237
a6967cc0
JM
3238 /* Looking up TYPE_POINTER_TO and TYPE_REFERENCE_TO yield the same
3239 result. */
8d08fdba
MS
3240 TYPE_POINTER_TO (unknown_type_node) = unknown_type_node;
3241 TYPE_REFERENCE_TO (unknown_type_node) = unknown_type_node;
3242
c4372ef4
NS
3243 {
3244 /* Make sure we get a unique function type, so we can give
3245 its pointer type a name. (This wins for gdb.) */
3246 tree vfunc_type = make_node (FUNCTION_TYPE);
3247 TREE_TYPE (vfunc_type) = integer_type_node;
3248 TYPE_ARG_TYPES (vfunc_type) = NULL_TREE;
3249 layout_type (vfunc_type);
3250
3251 vtable_entry_type = build_pointer_type (vfunc_type);
3252 }
0e5921e8 3253 record_builtin_type (RID_MAX, VTBL_PTR_TYPE, vtable_entry_type);
8d08fdba 3254
8d08fdba 3255 vtbl_type_node
52bf7d5d 3256 = build_cplus_array_type (vtable_entry_type, NULL_TREE);
8d08fdba 3257 layout_type (vtbl_type_node);
91063b51 3258 vtbl_type_node = build_qualified_type (vtbl_type_node, TYPE_QUAL_CONST);
3e411c3f 3259 record_builtin_type (RID_MAX, NULL, vtbl_type_node);
849da744
MM
3260 vtbl_ptr_type_node = build_pointer_type (vtable_entry_type);
3261 layout_type (vtbl_ptr_type_node);
3e411c3f 3262 record_builtin_type (RID_MAX, NULL, vtbl_ptr_type_node);
8d08fdba 3263
1f84ec23
MM
3264 push_namespace (get_identifier ("__cxxabiv1"));
3265 abi_node = current_namespace;
3266 pop_namespace ();
6633d636 3267
2c73f9f5 3268 global_type_node = make_node (LANG_TYPE);
036407f7 3269 record_unknown_type (global_type_node, "global type");
2c73f9f5 3270
db5ae43f
MS
3271 /* Now, C++. */
3272 current_lang_name = lang_name_cplusplus;
8d08fdba 3273
ced78d8b 3274 {
4b0d3cbe
MM
3275 tree bad_alloc_id;
3276 tree bad_alloc_type_node;
3277 tree bad_alloc_decl;
3278 tree newtype, deltype;
10841285
MM
3279 tree ptr_ftype_sizetype;
3280
1dbb6023 3281 push_namespace (std_identifier);
4b0d3cbe
MM
3282 bad_alloc_id = get_identifier ("bad_alloc");
3283 bad_alloc_type_node = make_aggr_type (RECORD_TYPE);
3284 TYPE_CONTEXT (bad_alloc_type_node) = current_namespace;
caf93cb0 3285 bad_alloc_decl
4b0d3cbe
MM
3286 = create_implicit_typedef (bad_alloc_id, bad_alloc_type_node);
3287 DECL_CONTEXT (bad_alloc_decl) = current_namespace;
3288 TYPE_STUB_DECL (bad_alloc_type_node) = bad_alloc_decl;
1dbb6023 3289 pop_namespace ();
caf93cb0
EC
3290
3291 ptr_ftype_sizetype
10841285
MM
3292 = build_function_type (ptr_type_node,
3293 tree_cons (NULL_TREE,
c9f8536c 3294 size_type_node,
10841285 3295 void_list_node));
2c73f9f5 3296 newtype = build_exception_variant
0ba8a114
NS
3297 (ptr_ftype_sizetype, add_exception_specifier
3298 (NULL_TREE, bad_alloc_type_node, -1));
4cc1d462 3299 deltype = build_exception_variant (void_ftype_ptr, empty_except_spec);
596ea4e5
AS
3300 push_cp_library_fn (NEW_EXPR, newtype);
3301 push_cp_library_fn (VEC_NEW_EXPR, newtype);
3302 global_delete_fndecl = push_cp_library_fn (DELETE_EXPR, deltype);
3303 push_cp_library_fn (VEC_DELETE_EXPR, deltype);
ced78d8b 3304 }
8d08fdba
MS
3305
3306 abort_fndecl
1f84ec23 3307 = build_library_fn_ptr ("__cxa_pure_virtual", void_ftype);
8d08fdba 3308
8d08fdba
MS
3309 /* Perform other language dependent initializations. */
3310 init_class_processing ();
db48b831 3311 init_rtti_processing ();
8d08fdba 3312
6467930b 3313 if (flag_exceptions)
8d2733ca 3314 init_exception_processing ();
9e9ff709 3315
7fcdf4c2 3316 if (! supports_one_only ())
72b7eeff 3317 flag_weak = 0;
8d08fdba 3318
2ce07e2d 3319 make_fname_decl = cp_make_fname_decl;
0ba8a114 3320 start_fname_decls ();
8d08fdba 3321
e9a25f70 3322 /* Show we use EH for cleanups. */
6de9cd9a
DN
3323 if (flag_exceptions)
3324 using_eh_for_cleanups ();
62c154ed
JM
3325}
3326
0ba8a114 3327/* Generate an initializer for a function naming variable from
bb885938 3328 NAME. NAME may be NULL, to indicate a dependent name. TYPE_P is
f4f206f4 3329 filled in with the type of the init. */
0ba8a114
NS
3330
3331tree
bb885938 3332cp_fname_init (const char* name, tree *type_p)
0ba8a114
NS
3333{
3334 tree domain = NULL_TREE;
3335 tree type;
3336 tree init = NULL_TREE;
3337 size_t length = 0;
3338
3339 if (name)
3340 {
3341 length = strlen (name);
3342 domain = build_index_type (size_int (length));
3343 init = build_string (length + 1, name);
3344 }
caf93cb0 3345
0ba8a114
NS
3346 type = build_qualified_type (char_type_node, TYPE_QUAL_CONST);
3347 type = build_cplus_array_type (type, domain);
3348
bb885938 3349 *type_p = type;
caf93cb0 3350
0ba8a114
NS
3351 if (init)
3352 TREE_TYPE (init) = type;
3353 else
bb885938 3354 init = error_mark_node;
caf93cb0 3355
0ba8a114
NS
3356 return init;
3357}
3358
2ce07e2d
NS
3359/* Create the VAR_DECL for __FUNCTION__ etc. ID is the name to give the
3360 decl, NAME is the initialization string and TYPE_DEP indicates whether
3361 NAME depended on the type of the function. We make use of that to detect
0ba8a114 3362 __PRETTY_FUNCTION__ inside a template fn. This is being done
a692ad2e 3363 lazily at the point of first use, so we mustn't push the decl now. */
2ce07e2d
NS
3364
3365static tree
11f6b451 3366cp_make_fname_decl (tree id, int type_dep)
2ce07e2d 3367{
83182544 3368 const char *const name = (type_dep && processing_template_decl
e913996d 3369 ? NULL : fname_as_string (type_dep));
bb885938
NS
3370 tree type;
3371 tree init = cp_fname_init (name, &type);
3372 tree decl = build_decl (VAR_DECL, id, type);
2ce07e2d 3373
9e6f2e7d
AH
3374 if (name)
3375 free ((char *) name);
3376
6cce57b0 3377 /* As we're using pushdecl_with_scope, we must set the context. */
0ba8a114
NS
3378 DECL_CONTEXT (decl) = current_function_decl;
3379 DECL_PRETTY_FUNCTION_P (decl) = type_dep;
caf93cb0 3380
2ce07e2d
NS
3381 TREE_STATIC (decl) = 1;
3382 TREE_READONLY (decl) = 1;
2ce07e2d 3383 DECL_ARTIFICIAL (decl) = 1;
caf93cb0 3384
0ba8a114 3385 TREE_USED (decl) = 1;
5362b086 3386
6cce57b0
JM
3387 if (current_function_decl)
3388 {
3389 struct cp_binding_level *b = current_binding_level;
a7e8c268 3390 while (b->level_chain->kind != sk_function_parms)
6cce57b0 3391 b = b->level_chain;
d63d5d0c 3392 pushdecl_with_scope (decl, b, /*is_friend=*/false);
3db45ab5 3393 cp_finish_decl (decl, init, /*init_const_expr_p=*/false, NULL_TREE,
d174af6c 3394 LOOKUP_ONLYCONVERTING);
e913996d 3395 }
ad16ae7f
MM
3396 else
3397 pushdecl_top_level_and_finish (decl, init);
caf93cb0 3398
2ce07e2d
NS
3399 return decl;
3400}
3401
c79efc4d
RÁE
3402static tree
3403builtin_function_1 (tree decl, tree context)
3404{
3405 tree id = DECL_NAME (decl);
3406 const char *name = IDENTIFIER_POINTER (id);
8d08fdba 3407
c79efc4d 3408 retrofit_lang_decl (decl);
0c11ada6 3409
c79efc4d
RÁE
3410 /* All nesting of C++ functions is lexical; there is never a "static
3411 chain" in the sense of GNU C nested functions. */
3412 DECL_NO_STATIC_CHAIN (decl) = 1;
3413
3414 DECL_ARTIFICIAL (decl) = 1;
3415 SET_OVERLOADED_OPERATOR_CODE (decl, ERROR_MARK);
3416 SET_DECL_LANGUAGE (decl, lang_c);
3417 /* Runtime library routines are, by definition, available in an
3418 external shared object. */
3419 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
3420 DECL_VISIBILITY_SPECIFIED (decl) = 1;
8d08fdba 3421
d52e4867 3422 DECL_CONTEXT (decl) = context;
12a22e76 3423
6bcedb4e 3424 pushdecl (decl);
6bcedb4e 3425
d63d5d0c
ILT
3426 /* A function in the user's namespace should have an explicit
3427 declaration before it is used. Mark the built-in function as
3428 anticipated but not actually declared. */
935d1834
ZW
3429 if (name[0] != '_' || name[1] != '_')
3430 DECL_ANTICIPATED (decl) = 1;
3431
8d08fdba
MS
3432 return decl;
3433}
7f4edbcb 3434
d52e4867 3435tree
c79efc4d 3436cxx_builtin_function (tree decl)
d52e4867 3437{
c79efc4d
RÁE
3438 tree id = DECL_NAME (decl);
3439 const char *name = IDENTIFIER_POINTER (id);
d52e4867
RS
3440 /* All builtins that don't begin with an '_' should additionally
3441 go in the 'std' namespace. */
3442 if (name[0] != '_')
3443 {
e5b44dfb 3444 tree decl2 = copy_node(decl);
d52e4867 3445 push_namespace (std_identifier);
e5b44dfb 3446 builtin_function_1 (decl2, std_node);
d52e4867
RS
3447 pop_namespace ();
3448 }
3449
e5b44dfb 3450 return builtin_function_1 (decl, NULL_TREE);
d52e4867
RS
3451}
3452
0c11ada6
JM
3453/* Generate a FUNCTION_DECL with the typical flags for a runtime library
3454 function. Not called directly. */
3455
3456static tree
11f6b451 3457build_library_fn_1 (tree name, enum tree_code operator_code, tree type)
0c11ada6
JM
3458{
3459 tree fn = build_lang_decl (FUNCTION_DECL, name, type);
3460 DECL_EXTERNAL (fn) = 1;
3461 TREE_PUBLIC (fn) = 1;
3462 DECL_ARTIFICIAL (fn) = 1;
596ea4e5 3463 SET_OVERLOADED_OPERATOR_CODE (fn, operator_code);
5d2ed28c 3464 SET_DECL_LANGUAGE (fn, lang_c);
73a8adb6
MM
3465 /* Runtime library routines are, by definition, available in an
3466 external shared object. */
3467 DECL_VISIBILITY (fn) = VISIBILITY_DEFAULT;
3468 DECL_VISIBILITY_SPECIFIED (fn) = 1;
0c11ada6
JM
3469 return fn;
3470}
c2a37c55 3471
0c11ada6
JM
3472/* Returns the _DECL for a library function with C linkage.
3473 We assume that such functions never throw; if this is incorrect,
3474 callers should unset TREE_NOTHROW. */
c2a37c55 3475
7f4edbcb 3476tree
11f6b451 3477build_library_fn (tree name, tree type)
0c11ada6 3478{
f59d2aad
RH
3479 tree fn = build_library_fn_1 (name, ERROR_MARK, type);
3480 TREE_NOTHROW (fn) = 1;
3481 return fn;
0c11ada6
JM
3482}
3483
3484/* Returns the _DECL for a library function with C++ linkage. */
3485
596ea4e5 3486static tree
11f6b451 3487build_cp_library_fn (tree name, enum tree_code operator_code, tree type)
0c11ada6 3488{
596ea4e5 3489 tree fn = build_library_fn_1 (name, operator_code, type);
0c11ada6 3490 TREE_NOTHROW (fn) = TYPE_NOTHROW_P (type);
6bbf1598 3491 DECL_CONTEXT (fn) = FROB_CONTEXT (current_namespace);
5d2ed28c 3492 SET_DECL_LANGUAGE (fn, lang_cplusplus);
0c11ada6
JM
3493 return fn;
3494}
3495
3496/* Like build_library_fn, but takes a C string instead of an
3497 IDENTIFIER_NODE. */
3498
3499tree
11f6b451 3500build_library_fn_ptr (const char* name, tree type)
7f4edbcb 3501{
0c11ada6
JM
3502 return build_library_fn (get_identifier (name), type);
3503}
3504
3505/* Like build_cp_library_fn, but takes a C string instead of an
3506 IDENTIFIER_NODE. */
3507
3508tree
11f6b451 3509build_cp_library_fn_ptr (const char* name, tree type)
0c11ada6 3510{
596ea4e5 3511 return build_cp_library_fn (get_identifier (name), ERROR_MARK, type);
0c11ada6
JM
3512}
3513
3514/* Like build_library_fn, but also pushes the function so that we will
3515 be able to find it via IDENTIFIER_GLOBAL_VALUE. */
3516
3517tree
11f6b451 3518push_library_fn (tree name, tree type)
0c11ada6
JM
3519{
3520 tree fn = build_library_fn (name, type);
3521 pushdecl_top_level (fn);
3522 return fn;
3523}
3524
3525/* Like build_cp_library_fn, but also pushes the function so that it
3526 will be found by normal lookup. */
3527
596ea4e5 3528static tree
11f6b451 3529push_cp_library_fn (enum tree_code operator_code, tree type)
0c11ada6 3530{
5362b086 3531 tree fn = build_cp_library_fn (ansi_opname (operator_code),
596ea4e5
AS
3532 operator_code,
3533 type);
0c11ada6
JM
3534 pushdecl (fn);
3535 return fn;
3536}
3537
3538/* Like push_library_fn, but takes a TREE_LIST of parm types rather than
3539 a FUNCTION_TYPE. */
3540
3541tree
11f6b451 3542push_void_library_fn (tree name, tree parmtypes)
0c11ada6
JM
3543{
3544 tree type = build_function_type (void_type_node, parmtypes);
3545 return push_library_fn (name, type);
3546}
3547
cf74fb86 3548/* Like push_library_fn, but also note that this function throws
0c11ada6
JM
3549 and does not return. Used for __throw_foo and the like. */
3550
3551tree
11f6b451 3552push_throw_library_fn (tree name, tree type)
0c11ada6 3553{
cf74fb86 3554 tree fn = push_library_fn (name, type);
0c11ada6
JM
3555 TREE_THIS_VOLATILE (fn) = 1;
3556 TREE_NOTHROW (fn) = 0;
3557 return fn;
7f4edbcb 3558}
8d08fdba 3559\f
61a127b3
MM
3560/* When we call finish_struct for an anonymous union, we create
3561 default copy constructors and such. But, an anonymous union
3562 shouldn't have such things; this function undoes the damage to the
3563 anonymous union type T.
3564
3565 (The reason that we create the synthesized methods is that we don't
3566 distinguish `union { int i; }' from `typedef union { int i; } U'.
3567 The first is an anonymous union; the second is just an ordinary
3568 union type.) */
3569
3570void
11f6b451 3571fixup_anonymous_aggr (tree t)
61a127b3
MM
3572{
3573 tree *q;
3574
f4f206f4 3575 /* Wipe out memory of synthesized methods. */
61a127b3
MM
3576 TYPE_HAS_CONSTRUCTOR (t) = 0;
3577 TYPE_HAS_DEFAULT_CONSTRUCTOR (t) = 0;
3578 TYPE_HAS_INIT_REF (t) = 0;
3579 TYPE_HAS_CONST_INIT_REF (t) = 0;
3580 TYPE_HAS_ASSIGN_REF (t) = 0;
61a127b3
MM
3581 TYPE_HAS_CONST_ASSIGN_REF (t) = 0;
3582
3583 /* Splice the implicitly generated functions out of the TYPE_METHODS
3584 list. */
3585 q = &TYPE_METHODS (t);
3586 while (*q)
3587 {
3588 if (DECL_ARTIFICIAL (*q))
3589 *q = TREE_CHAIN (*q);
3590 else
3591 q = &TREE_CHAIN (*q);
3592 }
3593
cab1f180 3594 /* ISO C++ 9.5.3. Anonymous unions may not have function members. */
61a127b3 3595 if (TYPE_METHODS (t))
ddd2d57e
RH
3596 error ("%Jan anonymous union cannot have function members",
3597 TYPE_MAIN_DECL (t));
a1c2b86d
JJ
3598
3599 /* Anonymous aggregates cannot have fields with ctors, dtors or complex
3600 assignment operators (because they cannot have these methods themselves).
3601 For anonymous unions this is already checked because they are not allowed
3602 in any union, otherwise we have to check it. */
3603 if (TREE_CODE (t) != UNION_TYPE)
3604 {
3605 tree field, type;
3606
3607 for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
3608 if (TREE_CODE (field) == FIELD_DECL)
3609 {
3610 type = TREE_TYPE (field);
3611 if (CLASS_TYPE_P (type))
3612 {
0cbd7506 3613 if (TYPE_NEEDS_CONSTRUCTING (type))
dee15844
JM
3614 error ("member %q+#D with constructor not allowed "
3615 "in anonymous aggregate", field);
a1c2b86d 3616 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
dee15844
JM
3617 error ("member %q+#D with destructor not allowed "
3618 "in anonymous aggregate", field);
a1c2b86d 3619 if (TYPE_HAS_COMPLEX_ASSIGN_REF (type))
dee15844
JM
3620 error ("member %q+#D with copy assignment operator "
3621 "not allowed in anonymous aggregate", field);
a1c2b86d
JJ
3622 }
3623 }
3624 }
61a127b3
MM
3625}
3626
72a93143 3627/* Make sure that a declaration with no declarator is well-formed, i.e.
a723baf1 3628 just declares a tagged type or anonymous union.
8d08fdba 3629
a723baf1 3630 Returns the type declared; or NULL_TREE if none. */
8d08fdba 3631
72a93143 3632tree
62d1db17 3633check_tag_decl (cp_decl_specifier_seq *declspecs)
8d08fdba 3634{
62d1db17
MM
3635 int saw_friend = declspecs->specs[(int)ds_friend] != 0;
3636 int saw_typedef = declspecs->specs[(int)ds_typedef] != 0;
a723baf1
MM
3637 /* If a class, struct, or enum type is declared by the DECLSPECS
3638 (i.e, if a class-specifier, enum-specifier, or non-typename
3639 elaborated-type-specifier appears in the DECLSPECS),
3640 DECLARED_TYPE is set to the corresponding type. */
3641 tree declared_type = NULL_TREE;
3642 bool error_p = false;
8d08fdba 3643
62d1db17 3644 if (declspecs->multiple_types_p)
72a93143 3645 error ("multiple types in one declaration");
62d1db17
MM
3646 else if (declspecs->redefined_builtin_type)
3647 {
3648 if (!in_system_header)
82b5e52a 3649 pedwarn ("redeclaration of C++ built-in type %qT",
62d1db17
MM
3650 declspecs->redefined_builtin_type);
3651 return NULL_TREE;
3652 }
7e2067ca 3653
c827f22f
MM
3654 if (declspecs->type
3655 && TYPE_P (declspecs->type)
caf93cb0 3656 && ((TREE_CODE (declspecs->type) != TYPENAME_TYPE
62d1db17
MM
3657 && IS_AGGR_TYPE (declspecs->type))
3658 || TREE_CODE (declspecs->type) == ENUMERAL_TYPE))
3659 declared_type = declspecs->type;
3660 else if (declspecs->type == error_mark_node)
3661 error_p = true;
a723baf1 3662 if (declared_type == NULL_TREE && ! saw_friend && !error_p)
7e2067ca 3663 pedwarn ("declaration does not declare anything");
1951a1b6 3664 /* Check for an anonymous union. */
a723baf1
MM
3665 else if (declared_type && IS_AGGR_TYPE_CODE (TREE_CODE (declared_type))
3666 && TYPE_ANONYMOUS_P (declared_type))
0dd3962d 3667 {
e8186ecf 3668 /* 7/3 In a simple-declaration, the optional init-declarator-list
0cbd7506
MS
3669 can be omitted only when declaring a class (clause 9) or
3670 enumeration (7.2), that is, when the decl-specifier-seq contains
3671 either a class-specifier, an elaborated-type-specifier with
3672 a class-key (9.1), or an enum-specifier. In these cases and
3673 whenever a class-specifier or enum-specifier is present in the
3674 decl-specifier-seq, the identifiers in these specifiers are among
3675 the names being declared by the declaration (as class-name,
3676 enum-names, or enumerators, depending on the syntax). In such
3677 cases, and except for the declaration of an unnamed bit-field (9.6),
3678 the decl-specifier-seq shall introduce one or more names into the
3679 program, or shall redeclare a name introduced by a previous
3680 declaration. [Example:
3681 enum { }; // ill-formed
3682 typedef class { }; // ill-formed
3683 --end example] */
e8186ecf 3684 if (saw_typedef)
0cbd7506
MS
3685 {
3686 error ("missing type-name in typedef-declaration");
3687 return NULL_TREE;
3688 }
0dd3962d 3689 /* Anonymous unions are objects, so they can have specifiers. */;
a723baf1 3690 SET_ANON_AGGR_TYPE_P (declared_type);
6bdb8141 3691
caf93cb0 3692 if (TREE_CODE (declared_type) != UNION_TYPE && pedantic
a723baf1 3693 && !in_system_header)
6bdb8141 3694 pedwarn ("ISO C++ prohibits anonymous structs");
0dd3962d
JM
3695 }
3696
62d1db17 3697 else
8d08fdba 3698 {
62d1db17
MM
3699 if (declspecs->specs[(int)ds_inline]
3700 || declspecs->specs[(int)ds_virtual])
2d01edd7 3701 error ("%qs can only be specified for functions",
caf93cb0 3702 declspecs->specs[(int)ds_inline]
62d1db17
MM
3703 ? "inline" : "virtual");
3704 else if (saw_friend
caf93cb0 3705 && (!current_class_type
62d1db17 3706 || current_scope () != current_class_type))
2d01edd7 3707 error ("%<friend%> can only be specified inside a class");
62d1db17 3708 else if (declspecs->specs[(int)ds_explicit])
2d01edd7 3709 error ("%<explicit%> can only be specified for constructors");
62d1db17
MM
3710 else if (declspecs->storage_class)
3711 error ("a storage class can only be specified for objects "
3712 "and functions");
3713 else if (declspecs->specs[(int)ds_const]
3714 || declspecs->specs[(int)ds_volatile]
3715 || declspecs->specs[(int)ds_restrict]
3716 || declspecs->specs[(int)ds_thread])
3717 error ("qualifiers can only be specified for objects "
3718 "and functions");
41dc91a8
SB
3719 else if (saw_typedef)
3720 warning (0, "%<typedef%> was ignored in this declaration");
72a93143 3721 }
8d08fdba 3722
a723baf1 3723 return declared_type;
72a93143
JM
3724}
3725
3726/* Called when a declaration is seen that contains no names to declare.
3727 If its type is a reference to a structure, union or enum inherited
3728 from a containing scope, shadow that tag name for the current scope
3729 with a forward reference.
3730 If its type defines a new named structure or union
3731 or defines an enum, it is valid but we need not do anything here.
3732 Otherwise, it is an error.
3733
3734 C++: may have to grok the declspecs to learn about static,
caf93cb0 3735 complain for anonymous unions.
72a93143 3736
a723baf1
MM
3737 Returns the TYPE declared -- or NULL_TREE if none. */
3738
3739tree
62d1db17 3740shadow_tag (cp_decl_specifier_seq *declspecs)
72a93143
JM
3741{
3742 tree t = check_tag_decl (declspecs);
3743
a723baf1
MM
3744 if (!t)
3745 return NULL_TREE;
3746
3cabd8f9
MA
3747 if (declspecs->attributes)
3748 {
dee15844
JM
3749 warning (0, "attribute ignored in declaration of %q+#T", t);
3750 warning (0, "attribute for %q+#T must follow the %qs keyword",
3751 t, class_key_or_enum_as_string (t));
3cabd8f9
MA
3752
3753 }
3754
d2a8ac2c
LM
3755 if (maybe_process_partial_specialization (t) == error_mark_node)
3756 return NULL_TREE;
72a93143
JM
3757
3758 /* This is where the variables in an anonymous union are
3759 declared. An anonymous union declaration looks like:
3760 union { ... } ;
3761 because there is no declarator after the union, the parser
3762 sends that declaration here. */
a723baf1 3763 if (ANON_AGGR_TYPE_P (t))
72a93143 3764 {
6bdb8141 3765 fixup_anonymous_aggr (t);
72a93143
JM
3766
3767 if (TYPE_FIELDS (t))
3768 {
caf93cb0 3769 tree decl = grokdeclarator (/*declarator=*/NULL,
058b15c1 3770 declspecs, NORMAL, 0, NULL);
72a93143
JM
3771 finish_anon_union (decl);
3772 }
8d08fdba 3773 }
a723baf1
MM
3774
3775 return t;
8d08fdba
MS
3776}
3777\f
3778/* Decode a "typename", such as "int **", returning a ..._TYPE node. */
3779
3780tree
caf93cb0 3781groktypename (cp_decl_specifier_seq *type_specifiers,
62d1db17 3782 const cp_declarator *declarator)
8d08fdba 3783{
62d1db17 3784 tree attrs;
98884b26 3785 tree type;
62d1db17
MM
3786 attrs = type_specifiers->attributes;
3787 type_specifiers->attributes = NULL_TREE;
3788 type = grokdeclarator (declarator, type_specifiers, TYPENAME, 0, &attrs);
98884b26
JM
3789 if (attrs)
3790 cplus_decl_attributes (&type, attrs, 0);
3791 return type;
8d08fdba
MS
3792}
3793
3794/* Decode a declarator in an ordinary declaration or data definition.
3795 This is called as soon as the type information and variable name
3796 have been parsed, before parsing the initializer if any.
3797 Here we create the ..._DECL node, fill in its type,
3798 and put it on the list of decls for the current context.
3799 The ..._DECL node is returned as the value.
3800
3801 Exception: for arrays where the length is not specified,
82580166 3802 the type is left null, to be filled in by `cp_finish_decl'.
8d08fdba
MS
3803
3804 Function definitions do not come here; they go to start_function
3805 instead. However, external and forward declarations of functions
3806 do go through here. Structure field declarations are done by
3807 grokfield and not through here. */
3808
8d08fdba 3809tree
caf93cb0 3810start_decl (const cp_declarator *declarator,
62d1db17 3811 cp_decl_specifier_seq *declspecs,
0cbd7506
MS
3812 int initialized,
3813 tree attributes,
3814 tree prefix_attributes,
4514aa8c 3815 tree *pushed_scope_p)
8d08fdba 3816{
59387d2e 3817 tree decl;
926ce8bd 3818 tree type, tem;
8d08fdba 3819 tree context;
c3b7031d 3820 bool was_public;
8d08fdba 3821
4514aa8c 3822 *pushed_scope_p = NULL_TREE;
c8094d83 3823
e23bd218
IR
3824 /* An object declared as __attribute__((deprecated)) suppresses
3825 warnings of uses of other deprecated items. */
3826 if (lookup_attribute ("deprecated", attributes))
3827 deprecated_state = DEPRECATED_SUPPRESS;
3828
91d231cb 3829 attributes = chainon (attributes, prefix_attributes);
b17e2870 3830
c11b6f21 3831 decl = grokdeclarator (declarator, declspecs, NORMAL, initialized,
91d231cb 3832 &attributes);
68642fb6 3833
e23bd218
IR
3834 deprecated_state = DEPRECATED_NORMAL;
3835
2d00b4f2
AP
3836 if (decl == NULL_TREE || TREE_CODE (decl) == VOID_TYPE
3837 || decl == error_mark_node)
44370687 3838 return error_mark_node;
8d08fdba
MS
3839
3840 type = TREE_TYPE (decl);
3841
4f1c5b7d 3842 context = DECL_CONTEXT (decl);
8d08fdba 3843
73a8adb6 3844 if (context)
4514aa8c
NS
3845 {
3846 *pushed_scope_p = push_scope (context);
c8094d83 3847
4514aa8c
NS
3848 /* We are only interested in class contexts, later. */
3849 if (TREE_CODE (context) == NAMESPACE_DECL)
3850 context = NULL_TREE;
3851 }
2c73f9f5 3852
8d08fdba
MS
3853 if (initialized)
3854 /* Is it valid for this decl to have an initializer at all?
3855 If not, set INITIALIZED to zero, which will indirectly
82580166 3856 tell `cp_finish_decl' to ignore the initializer once it is parsed. */
8d08fdba
MS
3857 switch (TREE_CODE (decl))
3858 {
3859 case TYPE_DECL:
2d01edd7 3860 error ("typedef %qD is initialized (use __typeof__ instead)", decl);
cc011e7f 3861 return error_mark_node;
8d08fdba
MS
3862
3863 case FUNCTION_DECL:
2d01edd7 3864 error ("function %q#D is initialized like a variable", decl);
650fcd07 3865 return error_mark_node;
8d08fdba
MS
3866
3867 default:
3e41d13b 3868 break;
8d08fdba
MS
3869 }
3870
8d08fdba
MS
3871 if (initialized)
3872 {
a9aedbc2 3873 if (! toplevel_bindings_p ()
8d08fdba 3874 && DECL_EXTERNAL (decl))
d4ee4d25 3875 warning (0, "declaration of %q#D has %<extern%> and is initialized",
0cbd7506 3876 decl);
8d08fdba 3877 DECL_EXTERNAL (decl) = 0;
5566b478 3878 if (toplevel_bindings_p ())
8d08fdba 3879 TREE_STATIC (decl) = 1;
8d08fdba
MS
3880 }
3881
fa20888b 3882 /* Set attributes here so if duplicate decl, will have proper attributes. */
91d231cb 3883 cplus_decl_attributes (&decl, attributes, 0);
fa20888b 3884
4e2bb0a4
DS
3885 /* Dllimported symbols cannot be defined. Static data members (which
3886 can be initialized in-class and dllimported) go through grokfield,
3887 not here, so we don't need to exclude those decls when checking for
3888 a definition. */
3889 if (initialized && DECL_DLLIMPORT_P (decl))
3890 {
3891 error ("definition of %q#D is marked %<dllimport%>", decl);
3892 DECL_DLLIMPORT_P (decl) = 0;
3893 }
3894
ecb0eece 3895 /* If #pragma weak was used, mark the decl weak now. */
3693d46c 3896 maybe_apply_pragma_weak (decl);
ecb0eece 3897
97055d5c
AO
3898 if (TREE_CODE (decl) == FUNCTION_DECL
3899 && DECL_DECLARED_INLINE_P (decl)
3900 && DECL_UNINLINABLE (decl)
3901 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
dee15844 3902 warning (0, "inline function %q+D given attribute noinline", decl);
97055d5c 3903
d0f062fb 3904 if (context && COMPLETE_TYPE_P (complete_type (context)))
5b605f68
MS
3905 {
3906 if (TREE_CODE (decl) == VAR_DECL)
3907 {
86ac0575 3908 tree field = lookup_field (context, DECL_NAME (decl), 0, false);
5b605f68 3909 if (field == NULL_TREE || TREE_CODE (field) != VAR_DECL)
2d01edd7 3910 error ("%q#D is not a static member of %q#T", decl, context);
e349ee73
MS
3911 else
3912 {
3913 if (DECL_CONTEXT (field) != context)
f2d773a2 3914 {
a723baf1 3915 if (!same_type_p (DECL_CONTEXT (field), context))
2d01edd7 3916 pedwarn ("ISO C++ does not permit %<%T::%D%> "
0cbd7506 3917 "to be defined as %<%T::%D%>",
a723baf1
MM
3918 DECL_CONTEXT (field), DECL_NAME (decl),
3919 context, DECL_NAME (decl));
f2d773a2
JM
3920 DECL_CONTEXT (decl) = DECL_CONTEXT (field);
3921 }
91146169
GB
3922 if (processing_specialization
3923 && template_class_depth (context) == 0
3924 && CLASSTYPE_TEMPLATE_SPECIALIZATION (context))
3925 error ("template header not allowed in member definition "
3926 "of explicitly specialized class");
75650646
MM
3927 /* Static data member are tricky; an in-class initialization
3928 still doesn't provide a definition, so the in-class
3929 declaration will have DECL_EXTERNAL set, but will have an
3930 initialization. Thus, duplicate_decls won't warn
3931 about this situation, and so we check here. */
e92fb501 3932 if (initialized && DECL_INITIALIZED_IN_CLASS_P (field))
9e637a26 3933 error ("duplicate initialization of %qD", decl);
d63d5d0c 3934 if (duplicate_decls (decl, field, /*newdecl_is_friend=*/false))
e349ee73
MS
3935 decl = field;
3936 }
5b605f68 3937 }
f30432d7
MS
3938 else
3939 {
d43f603d 3940 tree field = check_classfn (context, decl,
44021471
GB
3941 (processing_template_decl
3942 > template_class_depth (context))
3943 ? current_template_parms
3944 : NULL_TREE);
d63d5d0c
ILT
3945 if (field && duplicate_decls (decl, field,
3946 /*newdecl_is_friend=*/false))
f30432d7
MS
3947 decl = field;
3948 }
3949
3950 /* cp_finish_decl sets DECL_EXTERNAL if DECL_IN_AGGR_P is set. */
dff3e828 3951 DECL_IN_AGGR_P (decl) = 0;
9267ee62
NS
3952 /* Do not mark DECL as an explicit specialization if it was not
3953 already marked as an instantiation; a declaration should
3954 never be marked as a specialization unless we know what
c8094d83 3955 template is being specialized. */
9267ee62 3956 if (DECL_LANG_SPECIFIC (decl) && DECL_USE_TEMPLATE (decl))
84e6233f 3957 {
9267ee62 3958 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
c8094d83 3959
84e6233f
JM
3960 /* [temp.expl.spec] An explicit specialization of a static data
3961 member of a template is a definition if the declaration
3962 includes an initializer; otherwise, it is a declaration.
c8094d83 3963
84e6233f
JM
3964 We check for processing_specialization so this only applies
3965 to the new specialization syntax. */
e92fb501 3966 if (!initialized && processing_specialization)
84e6233f
JM
3967 DECL_EXTERNAL (decl) = 1;
3968 }
f30432d7 3969
b7698cf0 3970 if (DECL_EXTERNAL (decl) && ! DECL_TEMPLATE_SPECIALIZATION (decl))
2d01edd7 3971 pedwarn ("declaration of %q#D outside of class is not definition",
0cbd7506 3972 decl);
5b605f68
MS
3973 }
3974
c3b7031d
JM
3975 was_public = TREE_PUBLIC (decl);
3976
9188c363
MM
3977 /* Enter this declaration into the symbol table. */
3978 tem = maybe_push_decl (decl);
2ee887f2 3979
5156628f 3980 if (processing_template_decl)
cd9f6678 3981 tem = push_template_decl (tem);
156fc2bb
KL
3982 if (tem == error_mark_node)
3983 return error_mark_node;
5566b478 3984
3b426391 3985 /* Tell the back end to use or not use .common as appropriate. If we say
a50f0918
MS
3986 -fconserve-space, we want this to save .data space, at the expense of
3987 wrong semantics. If we say -fno-conserve-space, we want this to
3988 produce errors about redefs; to do this we force variables into the
3989 data segment. */
434aeebb
RS
3990 if (flag_conserve_space
3991 && TREE_CODE (tem) == VAR_DECL
3992 && TREE_PUBLIC (tem)
3993 && !DECL_THREAD_LOCAL_P (tem)
3994 && !have_global_bss_p ())
3995 DECL_COMMON (tem) = 1;
68642fb6 3996
c3b7031d
JM
3997 if (TREE_CODE (tem) == VAR_DECL
3998 && DECL_NAMESPACE_SCOPE_P (tem) && !TREE_PUBLIC (tem) && !was_public
3999 && !DECL_THIS_STATIC (tem) && !DECL_ARTIFICIAL (tem))
4000 {
4001 /* This is a const variable with implicit 'static'. Set
4002 DECL_THIS_STATIC so we can tell it from variables that are
4003 !TREE_PUBLIC because of the anonymous namespace. */
4004 gcc_assert (cp_type_readonly (TREE_TYPE (tem)));
4005 DECL_THIS_STATIC (tem) = 1;
4006 }
4007
e92fb501
MM
4008 if (!processing_template_decl && TREE_CODE (tem) == VAR_DECL)
4009 start_decl_1 (tem, initialized);
8d08fdba 4010
8d08fdba
MS
4011 return tem;
4012}
4013
5566b478 4014void
e92fb501 4015start_decl_1 (tree decl, bool initialized)
8d08fdba 4016{
e92fb501 4017 tree type;
8d08fdba 4018
e92fb501
MM
4019 gcc_assert (!processing_template_decl);
4020
4021 if (error_operand_p (decl))
44689c12
ML
4022 return;
4023
e92fb501
MM
4024 gcc_assert (TREE_CODE (decl) == VAR_DECL);
4025 type = TREE_TYPE (decl);
4026
5566b478
MS
4027 if (initialized)
4028 /* Is it valid for this decl to have an initializer at all?
4029 If not, set INITIALIZED to zero, which will indirectly
4030 tell `cp_finish_decl' to ignore the initializer once it is parsed. */
8d08fdba 4031 {
5566b478
MS
4032 /* Don't allow initializations for incomplete types except for
4033 arrays which might be completed by the initialization. */
d0f062fb 4034 if (COMPLETE_TYPE_P (complete_type (type)))
5566b478
MS
4035 ; /* A complete type is ok. */
4036 else if (TREE_CODE (type) != ARRAY_TYPE)
8d08fdba 4037 {
2d01edd7 4038 error ("variable %q#D has initializer but incomplete type", decl);
5566b478 4039 initialized = 0;
25eb19ff 4040 type = TREE_TYPE (decl) = error_mark_node;
5566b478 4041 }
d0f062fb 4042 else if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (type))))
5566b478
MS
4043 {
4044 if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
2d01edd7 4045 error ("elements of array %q#D have incomplete type", decl);
5566b478
MS
4046 /* else we already gave an error in start_decl. */
4047 initialized = 0;
8d08fdba 4048 }
8d08fdba 4049 }
e92fb501
MM
4050 else if (IS_AGGR_TYPE (type)
4051 && ! DECL_EXTERNAL (decl))
8d08fdba 4052 {
e92fb501 4053 if (!COMPLETE_TYPE_P (complete_type (type)))
5566b478 4054 {
2d01edd7 4055 error ("aggregate %q#D has incomplete type and cannot be defined",
5566b478
MS
4056 decl);
4057 /* Change the type so that assemble_variable will give
4058 DECL an rtl we can live with: (mem (const_int 0)). */
25eb19ff 4059 type = TREE_TYPE (decl) = error_mark_node;
5566b478
MS
4060 }
4061 else
4062 {
4063 /* If any base type in the hierarchy of TYPE needs a constructor,
4064 then we set initialized to 1. This way any nodes which are
4065 created for the purposes of initializing this aggregate
4066 will live as long as it does. This is necessary for global
4067 aggregates which do not have their initializers processed until
4068 the end of the file. */
4069 initialized = TYPE_NEEDS_CONSTRUCTING (type);
4070 }
4071 }
4072
c6671cbb
MM
4073 /* Create a new scope to hold this declaration if necessary.
4074 Whether or not a new scope is necessary cannot be determined
4075 until after the type has been completed; if the type is a
4076 specialization of a class template it is not until after
4077 instantiation has occurred that TYPE_HAS_NONTRIVIAL_DESTRUCTOR
4078 will be set correctly. */
4079 maybe_push_cleanup_level (type);
5566b478
MS
4080}
4081
7e99327d
MM
4082/* Handle initialization of references. DECL, TYPE, and INIT have the
4083 same meaning as in cp_finish_decl. *CLEANUP must be NULL on entry,
4084 but will be set to a new CLEANUP_STMT if a temporary is created
cd0be382 4085 that must be destroyed subsequently.
7e99327d
MM
4086
4087 Returns an initializer expression to use to initialize DECL, or
4088 NULL if the initialization can be performed statically.
e92cc029
MS
4089
4090 Quotes on semantics can be found in ARM 8.4.3. */
4091
8e4ce833 4092static tree
7e99327d 4093grok_reference_init (tree decl, tree type, tree init, tree *cleanup)
5566b478
MS
4094{
4095 tree tmp;
4096
4097 if (init == NULL_TREE)
4098 {
4099 if ((DECL_LANG_SPECIFIC (decl) == 0
4100 || DECL_IN_AGGR_P (decl) == 0)
4101 && ! DECL_THIS_EXTERN (decl))
c4f73174 4102 error ("%qD declared as reference but not initialized", decl);
8e4ce833 4103 return NULL_TREE;
5566b478
MS
4104 }
4105
ed5511d9 4106 if (TREE_CODE (init) == CONSTRUCTOR)
5566b478 4107 {
2d01edd7 4108 error ("ISO C++ forbids use of initializer list to "
0cbd7506 4109 "initialize reference %qD", decl);
8e4ce833 4110 return NULL_TREE;
8d08fdba
MS
4111 }
4112
4113 if (TREE_CODE (init) == TREE_LIST)
c7b62f14 4114 init = build_x_compound_expr_from_list (init, "initializer");
8d08fdba
MS
4115
4116 if (TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE
4117 && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE)
0a72704b
MM
4118 /* Note: default conversion is only called in very special cases. */
4119 init = decay_conversion (init);
68642fb6 4120
24bef158
MM
4121 /* Convert INIT to the reference type TYPE. This may involve the
4122 creation of a temporary, whose lifetime must be the same as that
350fae66
RK
4123 of the reference. If so, a DECL_EXPR for the temporary will be
4124 added just after the DECL_EXPR for DECL. That's why we don't set
24bef158
MM
4125 DECL_INITIAL for local references (instead assigning to them
4126 explicitly); we need to allow the temporary to be initialized
4127 first. */
7e99327d 4128 tmp = initialize_reference (type, init, decl, cleanup);
8d08fdba 4129
a3203465 4130 if (tmp == error_mark_node)
8e4ce833
JJ
4131 return NULL_TREE;
4132 else if (tmp == NULL_TREE)
8d08fdba 4133 {
2d01edd7 4134 error ("cannot initialize %qT from %qT", type, TREE_TYPE (init));
8e4ce833 4135 return NULL_TREE;
8d08fdba 4136 }
8d08fdba 4137
8e4ce833
JJ
4138 if (TREE_STATIC (decl) && !TREE_CONSTANT (tmp))
4139 return tmp;
4140
08ac397c 4141 DECL_INITIAL (decl) = tmp;
8e4ce833
JJ
4142
4143 return NULL_TREE;
8d08fdba
MS
4144}
4145
76239779
MM
4146/* Designated initializers in arrays are not supported in GNU C++.
4147 The parser cannot detect this error since it does not know whether
4148 a given brace-enclosed initializer is for a class type or for an
4149 array. This function checks that CE does not use a designated
4150 initializer. If it does, an error is issued. Returns true if CE
4151 is valid, i.e., does not have a designated initializer. */
4152
4153static bool
4154check_array_designated_initializer (const constructor_elt *ce)
4155{
48c9a7f0 4156 /* Designated initializers for array elements are not supported. */
76239779
MM
4157 if (ce->index)
4158 {
4159 /* The parser only allows identifiers as designated
4160 intializers. */
4161 gcc_assert (TREE_CODE (ce->index) == IDENTIFIER_NODE);
4162 error ("name %qD used in a GNU-style designated "
4163 "initializer for an array", ce->index);
4164 return false;
4165 }
4166
4167 return true;
4168}
4169
27778b73
MM
4170/* When parsing `int a[] = {1, 2};' we don't know the size of the
4171 array until we finish parsing the initializer. If that's the
4172 situation we're in, update DECL accordingly. */
4173
4174static void
11f6b451 4175maybe_deduce_size_from_array_init (tree decl, tree init)
27778b73
MM
4176{
4177 tree type = TREE_TYPE (decl);
4178
4179 if (TREE_CODE (type) == ARRAY_TYPE
4180 && TYPE_DOMAIN (type) == NULL_TREE
4181 && TREE_CODE (decl) != TYPE_DECL)
4182 {
f2ae0c45
JM
4183 /* do_default is really a C-ism to deal with tentative definitions.
4184 But let's leave it here to ease the eventual merge. */
4185 int do_default = !DECL_EXTERNAL (decl);
27778b73 4186 tree initializer = init ? init : DECL_INITIAL (decl);
76239779 4187 int failure = 0;
27778b73 4188
76239779
MM
4189 /* Check that there are no designated initializers in INIT, as
4190 those are not supported in GNU C++, and as the middle-end
4191 will crash if presented with a non-numeric designated
4192 initializer. */
4193 if (initializer && TREE_CODE (initializer) == CONSTRUCTOR)
2b643eda 4194 {
76239779
MM
4195 VEC(constructor_elt,gc) *v = CONSTRUCTOR_ELTS (initializer);
4196 constructor_elt *ce;
4197 HOST_WIDE_INT i;
4198 for (i = 0;
4199 VEC_iterate (constructor_elt, v, i, ce);
4200 ++i)
4201 if (!check_array_designated_initializer (ce))
4202 failure = 1;
2b643eda 4203 }
76239779
MM
4204
4205 if (!failure)
27778b73 4206 {
76239779
MM
4207 failure = cp_complete_array_type (&TREE_TYPE (decl), initializer,
4208 do_default);
4209 if (failure == 1)
2b643eda 4210 {
76239779
MM
4211 error ("initializer fails to determine size of %qD", decl);
4212 TREE_TYPE (decl) = error_mark_node;
4213 }
4214 else if (failure == 2)
4215 {
4216 if (do_default)
4217 {
4218 error ("array size missing in %qD", decl);
4219 TREE_TYPE (decl) = error_mark_node;
4220 }
4221 /* If a `static' var's size isn't known, make it extern as
4222 well as static, so it does not get allocated. If it's not
4223 `static', then don't mark it extern; finish_incomplete_decl
4224 will give it a default size and it will get allocated. */
4225 else if (!pedantic && TREE_STATIC (decl) && !TREE_PUBLIC (decl))
4226 DECL_EXTERNAL (decl) = 1;
4227 }
4228 else if (failure == 3)
4229 {
4230 error ("zero-size array %qD", decl);
2b643eda
MM
4231 TREE_TYPE (decl) = error_mark_node;
4232 }
2b643eda 4233 }
27778b73 4234
f23b8501
JJ
4235 cp_apply_type_quals_to_decl (cp_type_quals (TREE_TYPE (decl)), decl);
4236
27778b73
MM
4237 layout_decl (decl, 0);
4238 }
4239}
4240
4241/* Set DECL_SIZE, DECL_ALIGN, etc. for DECL (a VAR_DECL), and issue
57b52417 4242 any appropriate error messages regarding the layout. */
27778b73 4243
57b52417 4244static void
11f6b451 4245layout_var_decl (tree decl)
27778b73 4246{
2b643eda
MM
4247 tree type;
4248
2b643eda
MM
4249 type = TREE_TYPE (decl);
4250 if (type == error_mark_node)
4251 return;
57b52417
MM
4252
4253 /* If we haven't already layed out this declaration, do so now.
4254 Note that we must not call complete type for an external object
4255 because it's type might involve templates that we are not
34cd5ae7 4256 supposed to instantiate yet. (And it's perfectly valid to say
57b52417
MM
4257 `extern X x' for some incomplete type `X'.) */
4258 if (!DECL_EXTERNAL (decl))
4259 complete_type (type);
caf93cb0 4260 if (!DECL_SIZE (decl)
328de7c2 4261 && TREE_TYPE (decl) != error_mark_node
7de85f7e 4262 && (COMPLETE_TYPE_P (type)
caf93cb0 4263 || (TREE_CODE (type) == ARRAY_TYPE
7de85f7e
MM
4264 && !TYPE_DOMAIN (type)
4265 && COMPLETE_TYPE_P (TREE_TYPE (type)))))
27778b73
MM
4266 layout_decl (decl, 0);
4267
c82dbd95 4268 if (!DECL_EXTERNAL (decl) && DECL_SIZE (decl) == NULL_TREE)
27778b73
MM
4269 {
4270 /* An automatic variable with an incomplete type: that is an error.
4271 Don't talk about array types here, since we took care of that
4272 message in grokdeclarator. */
2d01edd7 4273 error ("storage size of %qD isn't known", decl);
27778b73
MM
4274 TREE_TYPE (decl) = error_mark_node;
4275 }
ae673f14
JM
4276#if 0
4277 /* Keep this code around in case we later want to control debug info
4278 based on whether a type is "used". (jason 1999-11-11) */
4279
27778b73
MM
4280 else if (!DECL_EXTERNAL (decl) && IS_AGGR_TYPE (ttype))
4281 /* Let debugger know it should output info for this type. */
4282 note_debug_info_needed (ttype);
4283
4284 if (TREE_STATIC (decl) && DECL_CLASS_SCOPE_P (decl))
4285 note_debug_info_needed (DECL_CONTEXT (decl));
ae673f14 4286#endif
27778b73
MM
4287
4288 if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl))
4289 && DECL_SIZE (decl) != NULL_TREE
4290 && ! TREE_CONSTANT (DECL_SIZE (decl)))
4291 {
4292 if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
4293 constant_expression_warning (DECL_SIZE (decl));
4294 else
2d01edd7 4295 error ("storage size of %qD isn't constant", decl);
27778b73
MM
4296 }
4297}
4298
27778b73
MM
4299/* If a local static variable is declared in an inline function, or if
4300 we have a weak definition, we must endeavor to create only one
4301 instance of the variable at link-time. */
4302
4303static void
11f6b451 4304maybe_commonize_var (tree decl)
27778b73
MM
4305{
4306 /* Static data in a function with comdat linkage also has comdat
4307 linkage. */
4308 if (TREE_STATIC (decl)
4309 /* Don't mess with __FUNCTION__. */
cf74fb86 4310 && ! DECL_ARTIFICIAL (decl)
0b50d7f1
MM
4311 && DECL_FUNCTION_SCOPE_P (decl)
4312 /* Unfortunately, import_export_decl has not always been called
4313 before the function is processed, so we cannot simply check
caf93cb0 4314 DECL_COMDAT. */
9e6aaf5e 4315 && (DECL_COMDAT (DECL_CONTEXT (decl))
0b50d7f1
MM
4316 || ((DECL_DECLARED_INLINE_P (DECL_CONTEXT (decl))
4317 || DECL_TEMPLATE_INSTANTIATION (DECL_CONTEXT (decl)))
4318 && TREE_PUBLIC (DECL_CONTEXT (decl)))))
27778b73 4319 {
0dbc5cd3 4320 if (flag_weak)
27778b73 4321 {
0dbc5cd3
MM
4322 /* With weak symbols, we simply make the variable COMDAT;
4323 that will cause copies in multiple translations units to
4324 be merged. */
4325 comdat_linkage (decl);
4326 }
4327 else
4328 {
4329 if (DECL_INITIAL (decl) == NULL_TREE
4330 || DECL_INITIAL (decl) == error_mark_node)
27778b73 4331 {
0dbc5cd3
MM
4332 /* Without weak symbols, we can use COMMON to merge
4333 uninitialized variables. */
27778b73
MM
4334 TREE_PUBLIC (decl) = 1;
4335 DECL_COMMON (decl) = 1;
4336 }
0dbc5cd3 4337 else
27778b73 4338 {
0dbc5cd3
MM
4339 /* While for initialized variables, we must use internal
4340 linkage -- which means that multiple copies will not
4341 be merged. */
4342 TREE_PUBLIC (decl) = 0;
4343 DECL_COMMON (decl) = 0;
dee15844
JM
4344 warning (0, "sorry: semantics of inline function static "
4345 "data %q+#D are wrong (you'll wind up "
4346 "with multiple copies)", decl);
d4ee4d25 4347 warning (0, "%J you can work around this by removing "
0cbd7506 4348 "the initializer",
ddd2d57e 4349 decl);
27778b73
MM
4350 }
4351 }
4352 }
4353 else if (DECL_LANG_SPECIFIC (decl) && DECL_COMDAT (decl))
4354 /* Set it up again; we might have set DECL_INITIAL since the last
4355 time. */
4356 comdat_linkage (decl);
4357}
4358
91063b51
MM
4359/* Issue an error message if DECL is an uninitialized const variable. */
4360
4361static void
11f6b451 4362check_for_uninitialized_const_var (tree decl)
91063b51
MM
4363{
4364 tree type = TREE_TYPE (decl);
4365
4366 /* ``Unless explicitly declared extern, a const object does not have
4367 external linkage and must be initialized. ($8.4; $12.1)'' ARM
4368 7.1.6 */
4369 if (TREE_CODE (decl) == VAR_DECL
4370 && TREE_CODE (type) != REFERENCE_TYPE
4371 && CP_TYPE_CONST_P (type)
4372 && !TYPE_NEEDS_CONSTRUCTING (type)
4373 && !DECL_INITIAL (decl))
2d01edd7 4374 error ("uninitialized const %qD", decl);
91063b51
MM
4375}
4376
4038c495
GB
4377\f
4378/* Structure holding the current initializer being processed by reshape_init.
4379 CUR is a pointer to the current element being processed, END is a pointer
4380 after the last element present in the initializer. */
4381typedef struct reshape_iterator_t
4382{
4383 constructor_elt *cur;
4384 constructor_elt *end;
4385} reshape_iter;
4386
4387static tree reshape_init_r (tree, reshape_iter *, bool);
4388
8e3df2de
MM
4389/* FIELD is a FIELD_DECL or NULL. In the former case, the value
4390 returned is the next FIELD_DECL (possibly FIELD itself) that can be
4391 initialized. If there are no more such fields, the return value
4392 will be NULL. */
4393
4394static tree
4395next_initializable_field (tree field)
4396{
4397 while (field
4398 && (TREE_CODE (field) != FIELD_DECL
4399 || (DECL_C_BIT_FIELD (field) && !DECL_NAME (field))
4400 || DECL_ARTIFICIAL (field)))
4401 field = TREE_CHAIN (field);
4402
4403 return field;
4404}
4405
4038c495
GB
4406/* Subroutine of reshape_init_array and reshape_init_vector, which does
4407 the actual work. ELT_TYPE is the element type of the array. MAX_INDEX is an
4408 INTEGER_CST representing the size of the array minus one (the maximum index),
4409 or NULL_TREE if the array was declared without specifying the size. D is
4410 the iterator within the constructor. */
4411
4412static tree
4413reshape_init_array_1 (tree elt_type, tree max_index, reshape_iter *d)
3d142be2 4414{
4038c495 4415 tree new_init;
3d142be2 4416 bool sized_array_p = (max_index != NULL_TREE);
004c400a
JJ
4417 unsigned HOST_WIDE_INT max_index_cst = 0;
4418 unsigned HOST_WIDE_INT index;
3d142be2 4419
4038c495
GB
4420 /* The initializer for an array is always a CONSTRUCTOR. */
4421 new_init = build_constructor (NULL_TREE, NULL);
4422
3d142be2 4423 if (sized_array_p)
004c400a 4424 {
d7d93837
AH
4425 /* Minus 1 is used for zero sized arrays. */
4426 if (integer_all_onesp (max_index))
4427 return new_init;
4428
004c400a
JJ
4429 if (host_integerp (max_index, 1))
4430 max_index_cst = tree_low_cst (max_index, 1);
4431 /* sizetype is sign extended, not zero extended. */
4432 else
4433 max_index_cst = tree_low_cst (fold_convert (size_type_node, max_index),
4434 1);
4435 }
3d142be2
GB
4436
4437 /* Loop until there are no more initializers. */
4438 for (index = 0;
4038c495 4439 d->cur != d->end && (!sized_array_p || index <= max_index_cst);
3d142be2
GB
4440 ++index)
4441 {
4038c495
GB
4442 tree elt_init;
4443
76239779 4444 check_array_designated_initializer (d->cur);
4038c495 4445 elt_init = reshape_init_r (elt_type, d, /*first_initializer_p=*/false);
07471dfb
MM
4446 if (elt_init == error_mark_node)
4447 return error_mark_node;
4038c495 4448 CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_init), NULL_TREE, elt_init);
3d142be2
GB
4449 }
4450
4038c495 4451 return new_init;
3d142be2
GB
4452}
4453
4038c495
GB
4454/* Subroutine of reshape_init_r, processes the initializers for arrays.
4455 Parameters are the same of reshape_init_r. */
8e3df2de 4456
4038c495
GB
4457static tree
4458reshape_init_array (tree type, reshape_iter *d)
4459{
4460 tree max_index = NULL_TREE;
caf93cb0 4461
4038c495 4462 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
8e3df2de 4463
4038c495
GB
4464 if (TYPE_DOMAIN (type))
4465 max_index = array_type_nelts (type);
4466
4467 return reshape_init_array_1 (TREE_TYPE (type), max_index, d);
4468}
4469
4470/* Subroutine of reshape_init_r, processes the initializers for vectors.
4471 Parameters are the same of reshape_init_r. */
8e3df2de
MM
4472
4473static tree
4038c495 4474reshape_init_vector (tree type, reshape_iter *d)
8e3df2de 4475{
4038c495
GB
4476 tree max_index = NULL_TREE;
4477 tree rtype;
4478
4479 gcc_assert (TREE_CODE (type) == VECTOR_TYPE);
4480
3b2db49f 4481 if (COMPOUND_LITERAL_P (d->cur->value))
4038c495
GB
4482 {
4483 tree value = d->cur->value;
4484 if (!same_type_p (TREE_TYPE (value), type))
4485 {
4486 error ("invalid type %qT as initializer for a vector of type %qT",
4487 TREE_TYPE (d->cur->value), type);
3db45ab5 4488 value = error_mark_node;
4038c495
GB
4489 }
4490 ++d->cur;
4491 return value;
4492 }
4493
4494 /* For a vector, the representation type is a struct
4495 containing a single member which is an array of the
4496 appropriate size. */
4497 rtype = TYPE_DEBUG_REPRESENTATION_TYPE (type);
4498 if (rtype && TYPE_DOMAIN (TREE_TYPE (TYPE_FIELDS (rtype))))
4499 max_index = array_type_nelts (TREE_TYPE (TYPE_FIELDS (rtype)));
4500
4501 return reshape_init_array_1 (TREE_TYPE (type), max_index, d);
4502}
4503
4504/* Subroutine of reshape_init_r, processes the initializers for classes
4505 or union. Parameters are the same of reshape_init_r. */
4506
4507static tree
4508reshape_init_class (tree type, reshape_iter *d, bool first_initializer_p)
4509{
4510 tree field;
8e3df2de 4511 tree new_init;
8e3df2de 4512
4038c495 4513 gcc_assert (CLASS_TYPE_P (type));
8e3df2de 4514
4038c495
GB
4515 /* The initializer for a class is always a CONSTRUCTOR. */
4516 new_init = build_constructor (NULL_TREE, NULL);
4517 field = next_initializable_field (TYPE_FIELDS (type));
ee7ecb29 4518
4038c495 4519 if (!field)
8e3df2de 4520 {
4038c495
GB
4521 /* [dcl.init.aggr]
4522
4523 An initializer for an aggregate member that is an
4524 empty class shall have the form of an empty
4525 initializer-list {}. */
4526 if (!first_initializer_p)
4527 {
4528 error ("initializer for %qT must be brace-enclosed", type);
4529 return error_mark_node;
4530 }
4531 return new_init;
8e3df2de 4532 }
4038c495
GB
4533
4534 /* Loop through the initializable fields, gathering initializers. */
4535 while (d->cur != d->end)
8e3df2de 4536 {
4038c495
GB
4537 tree field_init;
4538
4539 /* Handle designated initializers, as an extension. */
4540 if (d->cur->index)
4541 {
4038c495
GB
4542 field = lookup_field_1 (type, d->cur->index, /*want_type=*/false);
4543
4544 if (!field || TREE_CODE (field) != FIELD_DECL)
77bffd4c
SE
4545 {
4546 error ("%qT has no non-static data member named %qD", type,
4547 d->cur->index);
4548 return error_mark_node;
4549 }
4038c495
GB
4550 }
4551
4552 /* If we processed all the member of the class, we are done. */
4553 if (!field)
4554 break;
4555
4556 field_init = reshape_init_r (TREE_TYPE (field), d,
4557 /*first_initializer_p=*/false);
4558 CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_init), field, field_init);
4559
4560 /* [dcl.init.aggr]
4561
4562 When a union is initialized with a brace-enclosed
4563 initializer, the braces shall only contain an
4564 initializer for the first member of the union. */
4565 if (TREE_CODE (type) == UNION_TYPE)
4566 break;
4567
4568 field = next_initializable_field (TREE_CHAIN (field));
8e3df2de
MM
4569 }
4570
4038c495
GB
4571 return new_init;
4572}
4573
4574/* Subroutine of reshape_init, which processes a single initializer (part of
4575 a CONSTRUCTOR). TYPE is the type of the variable being initialized, D is the
4576 iterator within the CONSTRUCTOR which points to the initializer to process.
4577 FIRST_INITIALIZER_P is true if this is the first initializer of the
4578 CONSTRUCTOR node. */
4579
4580static tree
4581reshape_init_r (tree type, reshape_iter *d, bool first_initializer_p)
4582{
4583 tree init = d->cur->value;
4584
8e3df2de
MM
4585 /* A non-aggregate type is always initialized with a single
4586 initializer. */
4587 if (!CP_AGGREGATE_TYPE_P (type))
4038c495
GB
4588 {
4589 /* It is invalid to initialize a non-aggregate type with a
4590 brace-enclosed initializer.
4591 We need to check for BRACE_ENCLOSED_INITIALIZER_P here because
4592 of g++.old-deja/g++.mike/p7626.C: a pointer-to-member constant is
4593 a CONSTRUCTOR (with a record type). */
4594 if (TREE_CODE (init) == CONSTRUCTOR
4595 && BRACE_ENCLOSED_INITIALIZER_P (init)) /* p7626.C */
4596 {
4597 error ("braces around scalar initializer for type %qT", type);
4598 init = error_mark_node;
4599 }
3db45ab5 4600
4038c495
GB
4601 d->cur++;
4602 return init;
4603 }
8e3df2de
MM
4604
4605 /* [dcl.init.aggr]
4606
4607 All implicit type conversions (clause _conv_) are considered when
4608 initializing the aggregate member with an initializer from an
4609 initializer-list. If the initializer can initialize a member,
4610 the member is initialized. Otherwise, if the member is itself a
4611 non-empty subaggregate, brace elision is assumed and the
4612 initializer is considered for the initialization of the first
4613 member of the subaggregate. */
4038c495 4614 if (TREE_CODE (init) != CONSTRUCTOR
30f86ec3 4615 && can_convert_arg (type, TREE_TYPE (init), init, LOOKUP_NORMAL))
8e3df2de 4616 {
4038c495
GB
4617 d->cur++;
4618 return init;
8e3df2de
MM
4619 }
4620
4038c495
GB
4621 /* [dcl.init.string]
4622
4623 A char array (whether plain char, signed char, or unsigned char)
4624 can be initialized by a string-literal (optionally enclosed in
4625 braces); a wchar_t array can be initialized by a wide
4626 string-literal (optionally enclosed in braces). */
4627 if (TREE_CODE (type) == ARRAY_TYPE
42328048 4628 && char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type))))
8e3df2de 4629 {
4038c495 4630 tree str_init = init;
8e3df2de 4631
4038c495 4632 /* Strip one level of braces if and only if they enclose a single
3db45ab5 4633 element (as allowed by [dcl.init.string]). */
4038c495
GB
4634 if (!first_initializer_p
4635 && TREE_CODE (str_init) == CONSTRUCTOR
4636 && VEC_length (constructor_elt, CONSTRUCTOR_ELTS (str_init)) == 1)
8e3df2de 4637 {
4038c495
GB
4638 str_init = VEC_index (constructor_elt,
4639 CONSTRUCTOR_ELTS (str_init), 0)->value;
4640 }
3db45ab5 4641
4038c495 4642 /* If it's a string literal, then it's the initializer for the array
3db45ab5 4643 as a whole. Otherwise, continue with normal initialization for
4038c495
GB
4644 array types (one value per array element). */
4645 if (TREE_CODE (str_init) == STRING_CST)
4646 {
4647 d->cur++;
4648 return str_init;
4649 }
4650 }
8e3df2de 4651
4038c495
GB
4652 /* The following cases are about aggregates. If we are not within a full
4653 initializer already, and there is not a CONSTRUCTOR, it means that there
4654 is a missing set of braces (that is, we are processing the case for
4655 which reshape_init exists). */
4656 if (!first_initializer_p)
4657 {
4658 if (TREE_CODE (init) == CONSTRUCTOR)
4659 {
70ec16f7
AP
4660 if (TREE_TYPE (init) && TYPE_PTRMEMFUNC_P (TREE_TYPE (init)))
4661 /* There is no need to reshape pointer-to-member function
4662 initializers, as they are always constructed correctly
4663 by the front end. */
4664 ;
4665 else if (COMPOUND_LITERAL_P (init))
4038c495
GB
4666 /* For a nested compound literal, there is no need to reshape since
4667 brace elision is not allowed. Even if we decided to allow it,
4668 we should add a call to reshape_init in finish_compound_literal,
4669 before calling digest_init, so changing this code would still
4670 not be necessary. */
70ec16f7
AP
4671 gcc_assert (!BRACE_ENCLOSED_INITIALIZER_P (init));
4672 else
8e3df2de 4673 {
4038c495
GB
4674 ++d->cur;
4675 gcc_assert (BRACE_ENCLOSED_INITIALIZER_P (init));
4676 return reshape_init (type, init);
8e3df2de 4677 }
8e3df2de 4678 }
f8e7a389 4679
4038c495
GB
4680 warning (OPT_Wmissing_braces, "missing braces around initializer for %qT",
4681 type);
4682 }
8e3df2de 4683
4038c495
GB
4684 /* Dispatch to specialized routines. */
4685 if (CLASS_TYPE_P (type))
4686 return reshape_init_class (type, d, first_initializer_p);
4687 else if (TREE_CODE (type) == ARRAY_TYPE)
4688 return reshape_init_array (type, d);
4689 else if (TREE_CODE (type) == VECTOR_TYPE)
4690 return reshape_init_vector (type, d);
4691 else
4692 gcc_unreachable();
4693}
8e3df2de 4694
4038c495
GB
4695/* Undo the brace-elision allowed by [dcl.init.aggr] in a
4696 brace-enclosed aggregate initializer.
8e3df2de 4697
4038c495
GB
4698 INIT is the CONSTRUCTOR containing the list of initializers describing
4699 a brace-enclosed initializer for an entity of the indicated aggregate TYPE.
4700 It may not presently match the shape of the TYPE; for example:
4701
4702 struct S { int a; int b; };
4703 struct S a[] = { 1, 2, 3, 4 };
4704
4705 Here INIT will hold a VEC of four elements, rather than a
4706 VEC of two elements, each itself a VEC of two elements. This
4707 routine transforms INIT from the former form into the latter. The
4708 revised CONSTRUCTOR node is returned. */
4709
3b2db49f 4710tree
4038c495
GB
4711reshape_init (tree type, tree init)
4712{
4713 VEC(constructor_elt, gc) *v;
4714 reshape_iter d;
4715 tree new_init;
4716
4038c495
GB
4717 gcc_assert (BRACE_ENCLOSED_INITIALIZER_P (init));
4718
4719 v = CONSTRUCTOR_ELTS (init);
4720
4721 /* An empty constructor does not need reshaping, and it is always a valid
4722 initializer. */
4723 if (VEC_empty (constructor_elt, v))
4724 return init;
4725
4726 /* Recurse on this CONSTRUCTOR. */
4727 d.cur = VEC_index (constructor_elt, v, 0);
4728 d.end = d.cur + VEC_length (constructor_elt, v);
4729
4730 new_init = reshape_init_r (type, &d, true);
07471dfb
MM
4731 if (new_init == error_mark_node)
4732 return error_mark_node;
4038c495
GB
4733
4734 /* Make sure all the element of the constructor were used. Otherwise,
4735 issue an error about exceeding initializers. */
4736 if (d.cur != d.end)
4737 error ("too many initializers for %qT", type);
8e3df2de
MM
4738
4739 return new_init;
4740}
4741
c82dbd95 4742/* Verify INIT (the initializer for DECL), and record the
7e99327d
MM
4743 initialization in DECL_INITIAL, if appropriate. CLEANUP is as for
4744 grok_reference_init.
8e3df2de
MM
4745
4746 If the return value is non-NULL, it is an expression that must be
4747 evaluated dynamically to initialize DECL. */
27778b73 4748
c82dbd95 4749static tree
7e99327d 4750check_initializer (tree decl, tree init, int flags, tree *cleanup)
27778b73 4751{
17bbb839 4752 tree type = TREE_TYPE (decl);
25ebb82a 4753 tree init_code = NULL;
fc0e7bf5 4754
328de7c2
MM
4755 /* Things that are going to be initialized need to have complete
4756 type. */
4757 TREE_TYPE (decl) = type = complete_type (TREE_TYPE (decl));
c82dbd95 4758
328de7c2
MM
4759 if (type == error_mark_node)
4760 /* We will have already complained. */
f724eac4
MM
4761 return NULL_TREE;
4762
4763 if (TREE_CODE (type) == ARRAY_TYPE)
328de7c2 4764 {
f724eac4
MM
4765 tree element_type = TREE_TYPE (type);
4766
4767 /* The array type itself need not be complete, because the
4768 initializer may tell us how many elements are in the array.
4769 But, the elements of the array must be complete. */
4770 if (!COMPLETE_TYPE_P (complete_type (element_type)))
4771 {
4772 error ("elements of array %q#D have incomplete type", decl);
4773 return NULL_TREE;
4774 }
b9704fc5 4775 /* It is not valid to initialize a VLA. */
f724eac4
MM
4776 if (init
4777 && ((COMPLETE_TYPE_P (type) && !TREE_CONSTANT (TYPE_SIZE (type)))
4778 || !TREE_CONSTANT (TYPE_SIZE (element_type))))
4779 {
4780 error ("variable-sized object %qD may not be initialized", decl);
4781 return NULL_TREE;
4782 }
328de7c2 4783 }
f724eac4 4784 else if (!COMPLETE_TYPE_P (type))
328de7c2 4785 {
2d01edd7 4786 error ("%qD has incomplete type", decl);
328de7c2 4787 TREE_TYPE (decl) = error_mark_node;
f724eac4 4788 return NULL_TREE;
27778b73 4789 }
f724eac4
MM
4790 else
4791 /* There is no way to make a variable-sized class type in GNU C++. */
4792 gcc_assert (TREE_CONSTANT (TYPE_SIZE (type)));
c1ae8be5
SM
4793
4794 if (init && BRACE_ENCLOSED_INITIALIZER_P (init))
f47165c9 4795 {
c1ae8be5
SM
4796 int init_len = VEC_length (constructor_elt, CONSTRUCTOR_ELTS (init));
4797 if (SCALAR_TYPE_P (type))
4798 {
4799 if (init_len != 1)
4800 {
4801 error ("scalar object %qD requires one element in initializer",
4802 decl);
4803 TREE_TYPE (decl) = error_mark_node;
4804 return NULL_TREE;
4805 }
4806 }
4807 else if ((cxx_dialect == cxx98) && !CP_AGGREGATE_TYPE_P (type))
4808 {
4809 /* A non-aggregate that is not a scalar cannot be initialized
4810 via an initializer-list in C++98. */
4811 error ("braces around initializer for non-aggregate type %qT",
4812 type);
4813 TREE_TYPE (decl) = error_mark_node;
4814 return NULL_TREE;
4815 }
f47165c9 4816 }
27778b73
MM
4817
4818 if (TREE_CODE (decl) == CONST_DECL)
4819 {
7525db03 4820 gcc_assert (TREE_CODE (type) != REFERENCE_TYPE);
27778b73
MM
4821
4822 DECL_INITIAL (decl) = init;
4823
50bc768d 4824 gcc_assert (init != NULL_TREE);
27778b73
MM
4825 init = NULL_TREE;
4826 }
c82dbd95 4827 else if (!DECL_EXTERNAL (decl) && TREE_CODE (type) == REFERENCE_TYPE)
7e99327d 4828 init = grok_reference_init (decl, type, init, cleanup);
27778b73
MM
4829 else if (init)
4830 {
4038c495 4831 /* Do not reshape constructors of vectors (they don't need to be
3db45ab5 4832 reshaped. */
caf93cb0 4833 if (TREE_CODE (init) == CONSTRUCTOR
3b2db49f 4834 && !COMPOUND_LITERAL_P (init)
4038c495 4835 && !TREE_TYPE (init)) /* ptrmemfunc */
a16f2357 4836 {
4038c495 4837 init = reshape_init (type, init);
6e1b3a7c
AH
4838
4839 if ((*targetm.vector_opaque_p) (type))
4840 {
4841 error ("opaque vector types cannot be initialized");
4842 init = error_mark_node;
4843 }
a16f2357 4844 }
8e3df2de
MM
4845
4846 /* If DECL has an array type without a specific bound, deduce the
4847 array size from the initializer. */
4848 maybe_deduce_size_from_array_init (decl, init);
4849 type = TREE_TYPE (decl);
2b643eda
MM
4850 if (type == error_mark_node)
4851 return NULL_TREE;
8e3df2de 4852
27778b73
MM
4853 if (TYPE_HAS_CONSTRUCTOR (type) || TYPE_NEEDS_CONSTRUCTING (type))
4854 {
4855 if (TREE_CODE (type) == ARRAY_TYPE)
8e3df2de 4856 goto initialize_aggr;
4038c495 4857 else if (TREE_CODE (init) == CONSTRUCTOR)
27778b73
MM
4858 {
4859 if (TYPE_NON_AGGREGATE_CLASS (type))
4860 {
2d01edd7 4861 error ("%qD must be initialized by constructor, "
0cbd7506 4862 "not by %<{...}%>",
8e3df2de 4863 decl);
27778b73
MM
4864 init = error_mark_node;
4865 }
4866 else
4867 goto dont_use_constructor;
4868 }
8e3df2de
MM
4869 else
4870 {
4871 int saved_stmts_are_full_exprs_p;
4872
4873 initialize_aggr:
4874 saved_stmts_are_full_exprs_p = 0;
4875 if (building_stmt_tree ())
4876 {
4877 saved_stmts_are_full_exprs_p = stmts_are_full_exprs_p ();
4878 current_stmt_tree ()->stmts_are_full_exprs_p = 1;
4879 }
4880 init = build_aggr_init (decl, init, flags);
4881 if (building_stmt_tree ())
4882 current_stmt_tree ()->stmts_are_full_exprs_p =
4883 saved_stmts_are_full_exprs_p;
4884 return init;
4885 }
27778b73
MM
4886 }
4887 else
4888 {
4889 dont_use_constructor:
4890 if (TREE_CODE (init) != TREE_VEC)
25ebb82a
RH
4891 {
4892 init_code = store_init_value (decl, init);
7a8380ae
NS
4893 if (pedantic && TREE_CODE (type) == ARRAY_TYPE
4894 && DECL_INITIAL (decl)
4895 && TREE_CODE (DECL_INITIAL (decl)) == STRING_CST
4896 && PAREN_STRING_LITERAL_P (DECL_INITIAL (decl)))
d4ee4d25 4897 warning (0, "array %qD initialized by parenthesized string literal %qE",
7a8380ae 4898 decl, DECL_INITIAL (decl));
25ebb82a
RH
4899 init = NULL;
4900 }
27778b73 4901 }
27778b73
MM
4902 }
4903 else if (DECL_EXTERNAL (decl))
4904 ;
8e3df2de
MM
4905 else if (TYPE_P (type) && TYPE_NEEDS_CONSTRUCTING (type))
4906 goto initialize_aggr;
4907 else if (IS_AGGR_TYPE (type))
27778b73
MM
4908 {
4909 tree core_type = strip_array_types (type);
4910
8e3df2de 4911 if (CLASSTYPE_READONLY_FIELDS_NEED_INIT (core_type))
2d01edd7 4912 error ("structure %qD with uninitialized const members", decl);
8e3df2de 4913 if (CLASSTYPE_REF_FIELDS_NEED_INIT (core_type))
2d01edd7 4914 error ("structure %qD with uninitialized reference members", decl);
27778b73
MM
4915
4916 check_for_uninitialized_const_var (decl);
27778b73
MM
4917 }
4918 else
4919 check_for_uninitialized_const_var (decl);
68642fb6 4920
8e3df2de 4921 if (init && init != error_mark_node)
f293ce4b 4922 init_code = build2 (INIT_EXPR, type, decl, init);
8e3df2de 4923
25ebb82a 4924 return init_code;
27778b73
MM
4925}
4926
4927/* If DECL is not a local variable, give it RTL. */
4928
4929static void
11f6b451 4930make_rtl_for_nonlocal_decl (tree decl, tree init, const char* asmspec)
27778b73 4931{
95ee998c
MM
4932 int toplev = toplevel_bindings_p ();
4933 int defer_p;
9f12b095 4934 const char *filename;
27778b73 4935
0e6df31e
GK
4936 /* Set the DECL_ASSEMBLER_NAME for the object. */
4937 if (asmspec)
4938 {
4939 /* The `register' keyword, when used together with an
4940 asm-specification, indicates that the variable should be
4941 placed in a particular register. */
4942 if (TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
4943 {
11e3f4b6 4944 set_user_assembler_name (decl, asmspec);
0e6df31e
GK
4945 DECL_HARD_REGISTER (decl) = 1;
4946 }
4947 else
b482789c
MA
4948 {
4949 if (TREE_CODE (decl) == FUNCTION_DECL
4950 && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
4951 set_builtin_user_assembler_name (decl, asmspec);
4952 set_user_assembler_name (decl, asmspec);
4953 }
0e6df31e
GK
4954 }
4955
f39ee884
MM
4956 /* Handle non-variables up front. */
4957 if (TREE_CODE (decl) != VAR_DECL)
4958 {
0e6df31e 4959 rest_of_decl_compilation (decl, toplev, at_eof);
f39ee884
MM
4960 return;
4961 }
4962
95ee998c
MM
4963 /* If we see a class member here, it should be a static data
4964 member. */
4965 if (DECL_LANG_SPECIFIC (decl) && DECL_IN_AGGR_P (decl))
4966 {
50bc768d 4967 gcc_assert (TREE_STATIC (decl));
95ee998c
MM
4968 /* An in-class declaration of a static data member should be
4969 external; it is only a declaration, and not a definition. */
4970 if (init == NULL_TREE)
50bc768d 4971 gcc_assert (DECL_EXTERNAL (decl));
95ee998c
MM
4972 }
4973
95ee998c
MM
4974 /* We don't create any RTL for local variables. */
4975 if (DECL_FUNCTION_SCOPE_P (decl) && !TREE_STATIC (decl))
4976 return;
27778b73 4977
95ee998c 4978 /* We defer emission of local statics until the corresponding
350fae66 4979 DECL_EXPR is expanded. */
95ee998c
MM
4980 defer_p = DECL_FUNCTION_SCOPE_P (decl) || DECL_VIRTUAL_P (decl);
4981
b602511f
JM
4982 /* We try to defer namespace-scope static constants so that they are
4983 not emitted into the object file unnecessarily. */
9f12b095 4984 filename = input_filename;
b602511f
JM
4985 if (!DECL_VIRTUAL_P (decl)
4986 && TREE_READONLY (decl)
4987 && DECL_INITIAL (decl) != NULL_TREE
4988 && DECL_INITIAL (decl) != error_mark_node
9f12b095 4989 && filename != NULL
b602511f
JM
4990 && ! EMPTY_CONSTRUCTOR_P (DECL_INITIAL (decl))
4991 && toplev
4992 && !TREE_PUBLIC (decl))
5cc90635
JM
4993 {
4994 /* Fool with the linkage of static consts according to #pragma
4995 interface. */
c533e34d 4996 struct c_fileinfo *finfo = get_fileinfo (filename);
5d709b00 4997 if (!finfo->interface_unknown && !TREE_PUBLIC (decl))
27778b73 4998 {
95ee998c 4999 TREE_PUBLIC (decl) = 1;
5d709b00 5000 DECL_EXTERNAL (decl) = finfo->interface_only;
27778b73 5001 }
27778b73 5002
95ee998c 5003 defer_p = 1;
27778b73 5004 }
b602511f 5005 /* Likewise for template instantiations. */
4684cd27
MM
5006 else if (DECL_LANG_SPECIFIC (decl)
5007 && DECL_IMPLICIT_INSTANTIATION (decl))
b602511f 5008 defer_p = 1;
95ee998c 5009
95ee998c 5010 /* If we're not deferring, go ahead and assemble the variable. */
0e6df31e
GK
5011 if (!defer_p)
5012 rest_of_decl_compilation (decl, toplev, at_eof);
27778b73
MM
5013}
5014
ed5511d9 5015/* Generate code to initialize DECL (a local variable). */
27778b73 5016
8e3df2de 5017static void
11f6b451 5018initialize_local_var (tree decl, tree init)
27778b73 5019{
9ed9e79a 5020 tree type = TREE_TYPE (decl);
170b020f 5021 tree cleanup;
27778b73 5022
50bc768d
NS
5023 gcc_assert (TREE_CODE (decl) == VAR_DECL
5024 || TREE_CODE (decl) == RESULT_DECL);
5025 gcc_assert (!TREE_STATIC (decl));
b7b8bcd2 5026
8e3df2de 5027 if (DECL_SIZE (decl) == NULL_TREE)
b7b8bcd2
MM
5028 {
5029 /* If we used it already as memory, it must stay in memory. */
5030 DECL_INITIAL (decl) = NULL_TREE;
5031 TREE_ADDRESSABLE (decl) = TREE_USED (decl);
5032 }
5033
27778b73
MM
5034 if (DECL_SIZE (decl) && type != error_mark_node)
5035 {
5036 int already_used;
68642fb6 5037
27778b73 5038 /* Compute and store the initial value. */
27778b73
MM
5039 already_used = TREE_USED (decl) || TREE_USED (type);
5040
8e3df2de
MM
5041 /* Perform the initialization. */
5042 if (init)
27778b73 5043 {
24bef158
MM
5044 int saved_stmts_are_full_exprs_p;
5045
50bc768d 5046 gcc_assert (building_stmt_tree ());
f2c5f623 5047 saved_stmts_are_full_exprs_p = stmts_are_full_exprs_p ();
ae499cce 5048 current_stmt_tree ()->stmts_are_full_exprs_p = 1;
8e3df2de 5049 finish_expr_stmt (init);
5362b086 5050 current_stmt_tree ()->stmts_are_full_exprs_p =
ae499cce 5051 saved_stmts_are_full_exprs_p;
27778b73
MM
5052 }
5053
5054 /* Set this to 0 so we can tell whether an aggregate which was
5055 initialized was ever used. Don't do this if it has a
5056 destructor, so we don't complain about the 'resource
b7b8bcd2
MM
5057 allocation is initialization' idiom. Now set
5058 attribute((unused)) on types so decls of that type will be
5059 marked used. (see TREE_USED, above.) */
27778b73
MM
5060 if (TYPE_NEEDS_CONSTRUCTING (type)
5061 && ! already_used
834c6dff 5062 && TYPE_HAS_TRIVIAL_DESTRUCTOR (type)
27778b73
MM
5063 && DECL_NAME (decl))
5064 TREE_USED (decl) = 0;
b7b8bcd2 5065 else if (already_used)
27778b73
MM
5066 TREE_USED (decl) = 1;
5067 }
24bef158 5068
8e3df2de 5069 /* Generate a cleanup, if necessary. */
170b020f
MM
5070 cleanup = cxx_maybe_build_cleanup (decl);
5071 if (DECL_SIZE (decl) && cleanup)
5072 finish_decl_cleanup (decl, cleanup);
24bef158
MM
5073}
5074
19c29b2f
MM
5075/* DECL is a VAR_DECL for a compiler-generated variable with static
5076 storage duration (like a virtual table) whose initializer is a
3b2db49f
MM
5077 compile-time constant. INIT must be either a TREE_LIST of values,
5078 or a CONSTRUCTOR. Initialize the variable and provide it to the
5079 back end. */
19c29b2f
MM
5080
5081void
5082initialize_artificial_var (tree decl, tree init)
5083{
d23f3d60 5084 gcc_assert (DECL_ARTIFICIAL (decl));
3b2db49f
MM
5085 if (TREE_CODE (init) == TREE_LIST)
5086 init = build_constructor_from_list (NULL_TREE, init);
5087 gcc_assert (TREE_CODE (init) == CONSTRUCTOR);
5088 DECL_INITIAL (decl) = init;
19c29b2f
MM
5089 DECL_INITIALIZED_P (decl) = 1;
5090 determine_visibility (decl);
5091 layout_var_decl (decl);
5092 maybe_commonize_var (decl);
5093 make_rtl_for_nonlocal_decl (decl, init, /*asmspec=*/NULL);
5094}
5095
32a11c08
MM
5096/* INIT is the initializer for a variable, as represented by the
5097 parser. Returns true iff INIT is value-dependent. */
5098
5099static bool
5100value_dependent_init_p (tree init)
5101{
5102 if (TREE_CODE (init) == TREE_LIST)
5103 /* A parenthesized initializer, e.g.: int i (3, 2); ? */
5104 return any_value_dependent_elements_p (init);
5105 else if (TREE_CODE (init) == CONSTRUCTOR)
5106 /* A brace-enclosed initializer, e.g.: int i = { 3 }; ? */
5107 {
5108 VEC(constructor_elt, gc) *elts;
5109 size_t nelts;
5110 size_t i;
5111
5112 elts = CONSTRUCTOR_ELTS (init);
5113 nelts = VEC_length (constructor_elt, elts);
5114 for (i = 0; i < nelts; ++i)
5115 if (value_dependent_init_p (VEC_index (constructor_elt,
5116 elts, i)->value))
5117 return true;
5118 }
5119 else
5120 /* It must be a simple expression, e.g., int i = 3; */
5121 return value_dependent_expression_p (init);
5122
5123 return false;
5124}
5125
8d08fdba
MS
5126/* Finish processing of a declaration;
5127 install its line number and initial value.
5128 If the length of an array type is not known before,
5129 it must be determined now, from the initial value, or it is an error.
5130
d174af6c
MM
5131 INIT is the initializer (if any) for DECL. If INIT_CONST_EXPR_P is
5132 true, then INIT is an integral constant expression.
8d08fdba 5133
920f9474 5134 FLAGS is LOOKUP_ONLYCONVERTING if the = init syntax was used, else 0
f78c7bc6 5135 if the (init) syntax was used. */
8d08fdba
MS
5136
5137void
3db45ab5 5138cp_finish_decl (tree decl, tree init, bool init_const_expr_p,
d174af6c 5139 tree asmspec_tree, int flags)
8d08fdba 5140{
7e99327d 5141 tree type;
7e99327d 5142 tree cleanup;
9c0758dd 5143 const char *asmspec = NULL;
8d08fdba 5144 int was_readonly = 0;
745d26d9 5145 bool var_definition_p = false;
2b643eda 5146 int saved_processing_template_decl;
8d08fdba 5147
11325dcd
KL
5148 if (decl == error_mark_node)
5149 return;
5150 else if (! decl)
8d08fdba
MS
5151 {
5152 if (init)
8251199e 5153 error ("assignment (not initialization) in declaration");
8d08fdba
MS
5154 return;
5155 }
5156
50bc768d 5157 gcc_assert (TREE_CODE (decl) != RESULT_DECL);
2b643eda
MM
5158 /* Parameters are handled by store_parm_decls, not cp_finish_decl. */
5159 gcc_assert (TREE_CODE (decl) != PARM_DECL);
5160
5161 type = TREE_TYPE (decl);
5162 if (type == error_mark_node)
5163 return;
170b020f 5164
7e99327d
MM
5165 /* Assume no cleanup is required. */
5166 cleanup = NULL_TREE;
2b643eda 5167 saved_processing_template_decl = processing_template_decl;
7e99327d 5168
a4443a08 5169 /* If a name was specified, get the string. */
5f52c0e0 5170 if (global_scope_p (current_binding_level))
41c64394 5171 asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
4f543d15 5172 if (asmspec_tree && asmspec_tree != error_mark_node)
5cc90635 5173 asmspec = TREE_STRING_POINTER (asmspec_tree);
8d08fdba 5174
6ba89f8e 5175 if (current_class_type
4f1c5b7d 5176 && CP_DECL_CONTEXT (decl) == current_class_type
6ba89f8e
MM
5177 && TYPE_BEING_DEFINED (current_class_type)
5178 && (DECL_INITIAL (decl) || init))
3febd123 5179 DECL_INITIALIZED_IN_CLASS_P (decl) = 1;
6ba89f8e 5180
5156628f 5181 if (processing_template_decl)
5566b478 5182 {
2b643eda
MM
5183 bool type_dependent_p;
5184
08ac397c 5185 /* Add this declaration to the statement-tree. */
170b020f 5186 if (at_function_scope_p ())
350fae66 5187 add_decl_expr (decl);
08ac397c 5188
2b643eda
MM
5189 type_dependent_p = dependent_type_p (type);
5190
5191 if (init && init_const_expr_p)
d174af6c 5192 {
2b643eda
MM
5193 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1;
5194 if (DECL_INTEGRAL_CONSTANT_VAR_P (decl))
5195 TREE_CONSTANT (decl) = 1;
d174af6c
MM
5196 }
5197
32a11c08
MM
5198 /* Generally, initializers in templates are expanded when the
5199 template is instantiated. But, if DECL is an integral
5200 constant static data member, then it can be used in future
5201 integral constant expressions, and its value must be
5202 available. */
5203 if (!(init
5204 && DECL_CLASS_SCOPE_P (decl)
5205 && DECL_INTEGRAL_CONSTANT_VAR_P (decl)
5206 && !type_dependent_p
5207 && !value_dependent_init_p (init)))
2b643eda
MM
5208 {
5209 if (init)
5210 DECL_INITIAL (decl) = init;
5211 if (TREE_CODE (decl) == VAR_DECL
5212 && !DECL_PRETTY_FUNCTION_P (decl)
5213 && !type_dependent_p)
5214 maybe_deduce_size_from_array_init (decl, init);
5215 goto finish_end;
5216 }
c8094d83 5217
2b643eda
MM
5218 init = fold_non_dependent_expr (init);
5219 processing_template_decl = 0;
5566b478 5220 }
3e41d13b 5221
8d08fdba
MS
5222 /* Take care of TYPE_DECLs up front. */
5223 if (TREE_CODE (decl) == TYPE_DECL)
5224 {
a0a33927
MS
5225 if (type != error_mark_node
5226 && IS_AGGR_TYPE (type) && DECL_NAME (decl))
8d08fdba
MS
5227 {
5228 if (TREE_TYPE (DECL_NAME (decl)) && TREE_TYPE (decl) != type)
d4ee4d25 5229 warning (0, "shadowing previous type declaration of %q#D", decl);
4b0d3cbe 5230 set_identifier_type_value (DECL_NAME (decl), decl);
8d08fdba 5231 }
cffa8729
MS
5232
5233 /* If we have installed this as the canonical typedef for this
5234 type, and that type has not been defined yet, delay emitting
956d6950 5235 the debug information for it, as we will emit it later. */
d2e5ee5c 5236 if (TYPE_MAIN_DECL (TREE_TYPE (decl)) == decl
d0f062fb 5237 && !COMPLETE_TYPE_P (TREE_TYPE (decl)))
cffa8729
MS
5238 TYPE_DECL_SUPPRESS_DEBUG (decl) = 1;
5239
0e6df31e
GK
5240 rest_of_decl_compilation (decl, DECL_CONTEXT (decl) == NULL_TREE,
5241 at_eof);
8d08fdba
MS
5242 goto finish_end;
5243 }
3e41d13b 5244
67935995 5245 /* A reference will be modified here, as it is initialized. */
c8094d83 5246 if (! DECL_EXTERNAL (decl)
67935995
MM
5247 && TREE_READONLY (decl)
5248 && TREE_CODE (type) == REFERENCE_TYPE)
8d08fdba 5249 {
8d08fdba 5250 was_readonly = 1;
67935995 5251 TREE_READONLY (decl) = 0;
8d08fdba
MS
5252 }
5253
437081d7 5254 if (TREE_CODE (decl) == VAR_DECL)
17bbb839
MM
5255 {
5256 /* Only PODs can have thread-local storage. Other types may require
5257 various kinds of non-trivial initialization. */
c2f7fa15 5258 if (DECL_THREAD_LOCAL_P (decl) && !pod_type_p (TREE_TYPE (decl)))
2d01edd7 5259 error ("%qD cannot be thread-local because it has non-POD type %qT",
17bbb839 5260 decl, TREE_TYPE (decl));
d23f3d60
MM
5261 /* If this is a local variable that will need a mangled name,
5262 register it now. We must do this before processing the
5263 initializer for the variable, since the initialization might
5264 require a guard variable, and since the mangled name of the
5265 guard variable will depend on the mangled name of this
5266 variable. */
5267 if (!processing_template_decl
5268 && DECL_FUNCTION_SCOPE_P (decl)
5269 && TREE_STATIC (decl)
5270 && !DECL_ARTIFICIAL (decl))
5271 push_local_name (decl);
17bbb839
MM
5272 /* Convert the initializer to the type of DECL, if we have not
5273 already initialized DECL. */
5274 if (!DECL_INITIALIZED_P (decl)
5275 /* If !DECL_EXTERNAL then DECL is being defined. In the
8e3df2de 5276 case of a static data member initialized inside the
17bbb839
MM
5277 class-specifier, there can be an initializer even if DECL
5278 is *not* defined. */
5279 && (!DECL_EXTERNAL (decl) || init))
5280 {
6c06fbce 5281 if (init)
d174af6c
MM
5282 {
5283 DECL_NONTRIVIALLY_INITIALIZED_P (decl) = 1;
5284 if (init_const_expr_p)
5285 {
5286 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1;
5287 if (DECL_INTEGRAL_CONSTANT_VAR_P (decl))
5288 TREE_CONSTANT (decl) = 1;
5289 }
5290 }
7e99327d 5291 init = check_initializer (decl, init, flags, &cleanup);
b4c20e52 5292 /* Thread-local storage cannot be dynamically initialized. */
c2f7fa15 5293 if (DECL_THREAD_LOCAL_P (decl) && init)
b4c20e52 5294 {
2d01edd7 5295 error ("%qD is thread-local and so cannot be dynamically "
b4c20e52
MM
5296 "initialized", decl);
5297 init = NULL_TREE;
5298 }
b794e321
MM
5299
5300 /* Check that the initializer for a static data member was a
1634705d 5301 constant. Although we check in the parser that the
b794e321
MM
5302 initializer is an integral constant expression, we do not
5303 simplify division-by-zero at the point at which it
5304 occurs. Therefore, in:
5305
5306 struct S { static const int i = 7 / 0; };
3db45ab5 5307
b794e321
MM
5308 we issue an error at this point. It would
5309 probably be better to forbid division by zero in
5310 integral constant expressions. */
8fe4d24b
NS
5311 if (DECL_EXTERNAL (decl) && init)
5312 {
8fe4d24b
NS
5313 error ("%qD cannot be initialized by a non-constant expression"
5314 " when being declared", decl);
5315 DECL_INITIALIZED_IN_CLASS_P (decl) = 0;
5316 init = NULL_TREE;
5317 }
c8094d83 5318
17bbb839 5319 /* Handle:
caf93cb0 5320
17bbb839 5321 [dcl.init]
caf93cb0 5322
17bbb839
MM
5323 The memory occupied by any object of static storage
5324 duration is zero-initialized at program startup before
5325 any other initialization takes place.
caf93cb0 5326
17bbb839
MM
5327 We cannot create an appropriate initializer until after
5328 the type of DECL is finalized. If DECL_INITIAL is set,
5329 then the DECL is statically initialized, and any
5330 necessary zero-initialization has already been performed. */
5331 if (TREE_STATIC (decl) && !DECL_INITIAL (decl))
5332 DECL_INITIAL (decl) = build_zero_init (TREE_TYPE (decl),
1cb8292f 5333 /*nelts=*/NULL_TREE,
17bbb839
MM
5334 /*static_storage_p=*/true);
5335 /* Remember that the initialization for this variable has
5336 taken place. */
5337 DECL_INITIALIZED_P (decl) = 1;
745d26d9
MM
5338 /* This declaration is the definition of this variable,
5339 unless we are initializing a static data member within
5340 the class specifier. */
5341 if (!DECL_EXTERNAL (decl))
5342 var_definition_p = true;
17bbb839 5343 }
d881ff25
MM
5344 /* If the variable has an array type, lay out the type, even if
5345 there is no initializer. It is valid to index through the
5346 array, and we must get TYPE_ALIGN set correctly on the array
5347 type. */
5348 else if (TREE_CODE (type) == ARRAY_TYPE)
5349 layout_type (type);
17bbb839 5350 }
3e41d13b 5351
57cf57fb 5352 /* Add this declaration to the statement-tree. This needs to happen
350fae66
RK
5353 after the call to check_initializer so that the DECL_EXPR for a
5354 reference temp is added before the DECL_EXPR for the reference itself. */
170b020f 5355 if (at_function_scope_p ())
350fae66 5356 add_decl_expr (decl);
08ac397c 5357
da71e18c
MM
5358 /* Let the middle end know about variables and functions -- but not
5359 static data members in uninstantiated class templates. */
5360 if (!saved_processing_template_decl
5361 && (TREE_CODE (decl) == VAR_DECL
5362 || TREE_CODE (decl) == FUNCTION_DECL))
8d08fdba 5363 {
27778b73 5364 if (TREE_CODE (decl) == VAR_DECL)
da71e18c
MM
5365 {
5366 layout_var_decl (decl);
5367 maybe_commonize_var (decl);
5368 }
8d08fdba 5369
27778b73 5370 make_rtl_for_nonlocal_decl (decl, init, asmspec);
8d08fdba 5371
7fb213d8
GB
5372 /* Check for abstractness of the type. Notice that there is no
5373 need to strip array types here since the check for those types
5374 is already done within create_array_type_for_decl. */
68642fb6 5375 if (TREE_CODE (type) == FUNCTION_TYPE
27778b73 5376 || TREE_CODE (type) == METHOD_TYPE)
7fb213d8 5377 abstract_virtuals_error (decl, TREE_TYPE (type));
68642fb6 5378 else
cfb91b67 5379 abstract_virtuals_error (decl, type);
8d08fdba 5380
b9e75696
JM
5381 /* This needs to happen after the linkage is set. */
5382 determine_visibility (decl);
5383
caf93cb0 5384 if (TREE_CODE (decl) == FUNCTION_DECL
8e3df2de
MM
5385 || TREE_TYPE (decl) == error_mark_node)
5386 /* No initialization required. */
faae18ab 5387 ;
67d743fe
MS
5388 else if (DECL_EXTERNAL (decl)
5389 && ! (DECL_LANG_SPECIFIC (decl)
5390 && DECL_NOT_REALLY_EXTERN (decl)))
5566b478
MS
5391 {
5392 if (init)
5393 DECL_INITIAL (decl) = init;
5394 }
8e3df2de 5395 else
8d08fdba 5396 {
8e3df2de
MM
5397 /* A variable definition. */
5398 if (DECL_FUNCTION_SCOPE_P (decl))
24bef158 5399 {
8e3df2de
MM
5400 /* Initialize the local variable. */
5401 if (processing_template_decl)
e92fb501 5402 DECL_INITIAL (decl) = init;
8e3df2de
MM
5403 else if (!TREE_STATIC (decl))
5404 initialize_local_var (decl, init);
24bef158 5405 }
c8094d83 5406
745d26d9 5407 /* If a variable is defined, and then a subsequent
06b60445 5408 definition with external linkage is encountered, we will
745d26d9
MM
5409 get here twice for the same variable. We want to avoid
5410 calling expand_static_init more than once. For variables
5411 that are not static data members, we can call
5412 expand_static_init only when we actually process the
5413 initializer. It is not legal to redeclare a static data
5414 member, so this issue does not arise in that case. */
5415 if (var_definition_p && TREE_STATIC (decl))
27edb548
JJ
5416 {
5417 /* If a TREE_READONLY variable needs initialization
5418 at runtime, it is no longer readonly and we need to
5419 avoid MEM_READONLY_P being set on RTL created for it. */
32735518
JJ
5420 if (init)
5421 {
5422 if (TREE_READONLY (decl))
5423 TREE_READONLY (decl) = 0;
5424 was_readonly = 0;
5425 }
27edb548
JJ
5426 expand_static_init (decl, init);
5427 }
c8094d83 5428 }
8d08fdba
MS
5429 }
5430
7e99327d
MM
5431 /* If a CLEANUP_STMT was created to destroy a temporary bound to a
5432 reference, insert it in the statement-tree now. */
5433 if (cleanup)
325c3691 5434 push_cleanup (decl, cleanup, false);
7e99327d 5435
8d08fdba 5436 finish_end:
2b643eda 5437 processing_template_decl = saved_processing_template_decl;
8d08fdba 5438
8d08fdba
MS
5439 if (was_readonly)
5440 TREE_READONLY (decl) = 1;
d1bd0ded
GK
5441
5442 /* If this was marked 'used', be sure it will be output. */
5443 if (lookup_attribute ("used", DECL_ATTRIBUTES (decl)))
bb9a388d 5444 mark_decl_referenced (decl);
8d08fdba
MS
5445}
5446
f4f206f4 5447/* This is here for a midend callback from c-common.c. */
e92cc029 5448
82580166 5449void
11f6b451 5450finish_decl (tree decl, tree init, tree asmspec_tree)
82580166 5451{
d174af6c 5452 cp_finish_decl (decl, init, /*init_const_expr_p=*/false, asmspec_tree, 0);
82580166
MS
5453}
5454
db4283a0
MM
5455/* Returns a declaration for a VAR_DECL as if:
5456
5457 extern "C" TYPE NAME;
5458
5459 had been seen. Used to create compiler-generated global
5460 variables. */
5461
993acaec 5462static tree
11f6b451 5463declare_global_var (tree name, tree type)
db4283a0
MM
5464{
5465 tree decl;
5466
5467 push_to_top_level ();
5468 decl = build_decl (VAR_DECL, name, type);
5469 TREE_PUBLIC (decl) = 1;
5470 DECL_EXTERNAL (decl) = 1;
5471 DECL_ARTIFICIAL (decl) = 1;
de3fe73c
MM
5472 /* If the user has explicitly declared this variable (perhaps
5473 because the code we are compiling is part of a low-level runtime
5474 library), then it is possible that our declaration will be merged
5475 with theirs by pushdecl. */
5476 decl = pushdecl (decl);
d174af6c 5477 finish_decl (decl, NULL_TREE, NULL_TREE);
db4283a0
MM
5478 pop_from_top_level ();
5479
5480 return decl;
5481}
5482
46a9e521
MM
5483/* Returns the type for the argument to "__cxa_atexit" (or "atexit",
5484 if "__cxa_atexit" is not being used) corresponding to the function
5485 to be called when the program exits. */
5486
5487static tree
5488get_atexit_fn_ptr_type (void)
5489{
5490 tree arg_types;
5491 tree fn_type;
5492
5493 if (!atexit_fn_ptr_type_node)
5494 {
5495 if (flag_use_cxa_atexit
5496 && !targetm.cxx.use_atexit_for_cxa_atexit ())
5497 /* The parameter to "__cxa_atexit" is "void (*)(void *)". */
5498 arg_types = tree_cons (NULL_TREE, ptr_type_node, void_list_node);
5499 else
5500 /* The parameter to "atexit" is "void (*)(void)". */
5501 arg_types = void_list_node;
5502
5503 fn_type = build_function_type (void_type_node, arg_types);
5504 atexit_fn_ptr_type_node = build_pointer_type (fn_type);
5505 }
5506
5507 return atexit_fn_ptr_type_node;
5508}
5509
db4283a0 5510/* Returns a pointer to the `atexit' function. Note that if
838dfd8a 5511 FLAG_USE_CXA_ATEXIT is nonzero, then this will actually be the new
db4283a0
MM
5512 `__cxa_atexit' function specified in the IA64 C++ ABI. */
5513
5514static tree
11f6b451 5515get_atexit_node (void)
db4283a0
MM
5516{
5517 tree atexit_fndecl;
5518 tree arg_types;
5519 tree fn_type;
5520 tree fn_ptr_type;
5521 const char *name;
9f62c3e3 5522 bool use_aeabi_atexit;
db4283a0
MM
5523
5524 if (atexit_node)
5525 return atexit_node;
5526
97388150 5527 if (flag_use_cxa_atexit && !targetm.cxx.use_atexit_for_cxa_atexit ())
db4283a0
MM
5528 {
5529 /* The declaration for `__cxa_atexit' is:
5530
5531 int __cxa_atexit (void (*)(void *), void *, void *)
5532
5533 We build up the argument types and then then function type
5534 itself. */
68642fb6 5535
9f62c3e3 5536 use_aeabi_atexit = targetm.cxx.use_aeabi_atexit ();
db4283a0
MM
5537 /* First, build the pointer-to-function type for the first
5538 argument. */
46a9e521 5539 fn_ptr_type = get_atexit_fn_ptr_type ();
db4283a0
MM
5540 /* Then, build the rest of the argument types. */
5541 arg_types = tree_cons (NULL_TREE, ptr_type_node, void_list_node);
9f62c3e3
PB
5542 if (use_aeabi_atexit)
5543 {
5544 arg_types = tree_cons (NULL_TREE, fn_ptr_type, arg_types);
5545 arg_types = tree_cons (NULL_TREE, ptr_type_node, arg_types);
5546 }
5547 else
5548 {
5549 arg_types = tree_cons (NULL_TREE, ptr_type_node, arg_types);
5550 arg_types = tree_cons (NULL_TREE, fn_ptr_type, arg_types);
5551 }
db4283a0
MM
5552 /* And the final __cxa_atexit type. */
5553 fn_type = build_function_type (integer_type_node, arg_types);
5554 fn_ptr_type = build_pointer_type (fn_type);
9f62c3e3
PB
5555 if (use_aeabi_atexit)
5556 name = "__aeabi_atexit";
5557 else
5558 name = "__cxa_atexit";
db4283a0
MM
5559 }
5560 else
5561 {
5562 /* The declaration for `atexit' is:
68642fb6 5563
0cbd7506 5564 int atexit (void (*)());
db4283a0
MM
5565
5566 We build up the argument types and then then function type
5567 itself. */
46a9e521 5568 fn_ptr_type = get_atexit_fn_ptr_type ();
db4283a0
MM
5569 arg_types = tree_cons (NULL_TREE, fn_ptr_type, void_list_node);
5570 /* Build the final atexit type. */
5571 fn_type = build_function_type (integer_type_node, arg_types);
5572 name = "atexit";
5573 }
5574
5575 /* Now, build the function declaration. */
5576 push_lang_context (lang_name_c);
0c11ada6 5577 atexit_fndecl = build_library_fn_ptr (name, fn_type);
db4283a0
MM
5578 mark_used (atexit_fndecl);
5579 pop_lang_context ();
0a72704b 5580 atexit_node = decay_conversion (atexit_fndecl);
db4283a0
MM
5581
5582 return atexit_node;
5583}
5584
5585/* Returns the __dso_handle VAR_DECL. */
5586
5587static tree
11f6b451 5588get_dso_handle_node (void)
db4283a0
MM
5589{
5590 if (dso_handle_node)
5591 return dso_handle_node;
5592
5593 /* Declare the variable. */
5594 dso_handle_node = declare_global_var (get_identifier ("__dso_handle"),
5595 ptr_type_node);
5596
5597 return dso_handle_node;
5598}
5599
5600/* Begin a new function with internal linkage whose job will be simply
5601 to destroy some particular variable. */
5602
d192828a
GK
5603static GTY(()) int start_cleanup_cnt;
5604
db4283a0 5605static tree
11f6b451 5606start_cleanup_fn (void)
db4283a0 5607{
db4283a0 5608 char name[32];
db4283a0
MM
5609 tree fntype;
5610 tree fndecl;
97388150
DS
5611 bool use_cxa_atexit = flag_use_cxa_atexit
5612 && !targetm.cxx.use_atexit_for_cxa_atexit ();
db4283a0
MM
5613
5614 push_to_top_level ();
5615
5616 /* No need to mangle this. */
5617 push_lang_context (lang_name_c);
5618
db4283a0 5619 /* Build the name of the function. */
d192828a 5620 sprintf (name, "__tcf_%d", start_cleanup_cnt++);
db4283a0 5621 /* Build the function declaration. */
46a9e521 5622 fntype = TREE_TYPE (get_atexit_fn_ptr_type ());
db4283a0
MM
5623 fndecl = build_lang_decl (FUNCTION_DECL, get_identifier (name), fntype);
5624 /* It's a function with internal linkage, generated by the
5625 compiler. */
5626 TREE_PUBLIC (fndecl) = 0;
5627 DECL_ARTIFICIAL (fndecl) = 1;
5a728aca
MM
5628 /* Make the function `inline' so that it is only emitted if it is
5629 actually needed. It is unlikely that it will be inlined, since
aba649ba 5630 it is only called via a function pointer, but we avoid unnecessary
5a728aca
MM
5631 emissions this way. */
5632 DECL_INLINE (fndecl) = 1;
d4d1ebc1
JH
5633 DECL_DECLARED_INLINE_P (fndecl) = 1;
5634 DECL_INTERFACE_KNOWN (fndecl) = 1;
db4283a0 5635 /* Build the parameter. */
97388150 5636 if (use_cxa_atexit)
db4283a0
MM
5637 {
5638 tree parmdecl;
5639
8e51619a 5640 parmdecl = cp_build_parm_decl (NULL_TREE, ptr_type_node);
db4283a0 5641 DECL_CONTEXT (parmdecl) = fndecl;
db4283a0
MM
5642 TREE_USED (parmdecl) = 1;
5643 DECL_ARGUMENTS (fndecl) = parmdecl;
5644 }
5645
09ed39ad 5646 pushdecl (fndecl);
058b15c1 5647 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
db4283a0 5648
db4283a0
MM
5649 pop_lang_context ();
5650
5651 return current_function_decl;
5652}
5653
5654/* Finish the cleanup function begun by start_cleanup_fn. */
5655
5656static void
11f6b451 5657end_cleanup_fn (void)
db4283a0 5658{
8cd2462c 5659 expand_or_defer_fn (finish_function (0));
db4283a0
MM
5660
5661 pop_from_top_level ();
5662}
5663
bf419747
MM
5664/* Generate code to handle the destruction of DECL, an object with
5665 static storage duration. */
f0105ed3 5666
40aac948 5667tree
11f6b451 5668register_dtor_fn (tree decl)
f0105ed3 5669{
db4283a0 5670 tree cleanup;
f0105ed3 5671 tree compound_stmt;
db4283a0
MM
5672 tree args;
5673 tree fcall;
46a9e521
MM
5674 tree type;
5675 bool use_dtor;
f0105ed3 5676
46a9e521
MM
5677 type = TREE_TYPE (decl);
5678 if (TYPE_HAS_TRIVIAL_DESTRUCTOR (type))
40aac948 5679 return void_zero_node;
bf419747 5680
46a9e521
MM
5681 /* If we're using "__cxa_atexit" (or "__aeabi_atexit"), and DECL is
5682 a class object, we can just pass the destructor to
5683 "__cxa_atexit"; we don't have to build a temporary function to do
5684 the cleanup. */
5685 use_dtor = (flag_use_cxa_atexit
5686 && !targetm.cxx.use_atexit_for_cxa_atexit ()
5687 && CLASS_TYPE_P (type));
5688 if (use_dtor)
5689 {
5690 int idx;
5691
5692 /* Find the destructor. */
5693 idx = lookup_fnfields_1 (type, complete_dtor_identifier);
5694 gcc_assert (idx >= 0);
5695 cleanup = VEC_index (tree, CLASSTYPE_METHOD_VEC (type), idx);
5696 /* Make sure it is accessible. */
5697 perform_or_defer_access_check (TYPE_BINFO (type), cleanup, cleanup);
5698 }
5699 else
5700 {
5701 /* Call build_cleanup before we enter the anonymous function so
5702 that any access checks will be done relative to the current
5703 scope, rather than the scope of the anonymous function. */
5704 build_cleanup (decl);
5705
5706 /* Now start the function. */
5707 cleanup = start_cleanup_fn ();
5708
5709 /* Now, recompute the cleanup. It may contain SAVE_EXPRs that refer
5710 to the original function, rather than the anonymous one. That
5711 will make the back end think that nested functions are in use,
5712 which causes confusion. */
5713 push_deferring_access_checks (dk_no_check);
5714 fcall = build_cleanup (decl);
5715 pop_deferring_access_checks ();
5716
5717 /* Create the body of the anonymous function. */
5718 compound_stmt = begin_compound_stmt (BCS_FN_BODY);
5719 finish_expr_stmt (fcall);
5720 finish_compound_stmt (compound_stmt);
5721 end_cleanup_fn ();
5722 }
f0105ed3
MM
5723
5724 /* Call atexit with the cleanup function. */
bb4f6e6b 5725 mark_used (cleanup);
46a9e521 5726 cleanup = build_address (cleanup);
97388150 5727 if (flag_use_cxa_atexit && !targetm.cxx.use_atexit_for_cxa_atexit ())
db4283a0 5728 {
46a9e521
MM
5729 tree addr;
5730
5731 if (use_dtor)
5732 {
5733 /* We must convert CLEANUP to the type that "__cxa_atexit"
5734 expects. */
5735 cleanup = build_nop (get_atexit_fn_ptr_type (), cleanup);
5736 /* "__cxa_atexit" will pass the address of DECL to the
5737 cleanup function. */
5738 mark_used (decl);
5739 addr = build_address (decl);
5740 /* The declared type of the parameter to "__cxa_atexit" is
5741 "void *". For plain "T*", we could just let the
5742 machinery in build_function_call convert it -- but if the
5743 type is "cv-qualified T *", then we need to convert it
5744 before passing it in, to avoid spurious errors. */
5745 addr = build_nop (ptr_type_node, addr);
5746 }
5747 else
5748 /* Since the cleanup functions we build ignore the address
5749 they're given, there's no reason to pass the actual address
5750 in, and, in general, it's cheaper to pass NULL than any
5751 other value. */
5752 addr = null_pointer_node;
caf93cb0 5753 args = tree_cons (NULL_TREE,
0f268005
MM
5754 build_unary_op (ADDR_EXPR, get_dso_handle_node (), 0),
5755 NULL_TREE);
9f62c3e3
PB
5756 if (targetm.cxx.use_aeabi_atexit ())
5757 {
5758 args = tree_cons (NULL_TREE, cleanup, args);
46a9e521 5759 args = tree_cons (NULL_TREE, addr, args);
9f62c3e3
PB
5760 }
5761 else
5762 {
46a9e521 5763 args = tree_cons (NULL_TREE, addr, args);
9f62c3e3
PB
5764 args = tree_cons (NULL_TREE, cleanup, args);
5765 }
db4283a0
MM
5766 }
5767 else
5768 args = tree_cons (NULL_TREE, cleanup, NULL_TREE);
40aac948 5769 return build_function_call (get_atexit_node (), args);
f0105ed3
MM
5770}
5771
8e3df2de
MM
5772/* DECL is a VAR_DECL with static storage duration. INIT, if present,
5773 is its initializer. Generate code to handle the construction
5774 and destruction of DECL. */
5775
5776static void
11f6b451 5777expand_static_init (tree decl, tree init)
8d08fdba 5778{
50bc768d
NS
5779 gcc_assert (TREE_CODE (decl) == VAR_DECL);
5780 gcc_assert (TREE_STATIC (decl));
8e3df2de
MM
5781
5782 /* Some variables require no initialization. */
caf93cb0 5783 if (!init
8e3df2de
MM
5784 && !TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl))
5785 && TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
5786 return;
5787
eba6cfb6 5788 if (DECL_FUNCTION_SCOPE_P (decl))
8d08fdba
MS
5789 {
5790 /* Emit code to perform this initialization but once. */
445cf5eb
JM
5791 tree if_stmt = NULL_TREE, inner_if_stmt = NULL_TREE;
5792 tree then_clause = NULL_TREE, inner_then_clause = NULL_TREE;
94a0dd7b 5793 tree guard, guard_addr;
40aac948
JM
5794 tree acquire_fn, release_fn, abort_fn;
5795 tree flag, begin;
8d08fdba 5796
2036a15c
MM
5797 /* Emit code to perform this initialization but once. This code
5798 looks like:
5799
0cbd7506
MS
5800 static <type> guard;
5801 if (!guard.first_byte) {
40aac948
JM
5802 if (__cxa_guard_acquire (&guard)) {
5803 bool flag = false;
5804 try {
0cbd7506
MS
5805 // Do initialization.
5806 flag = true; __cxa_guard_release (&guard);
5807 // Register variable for destruction at end of program.
40aac948 5808 } catch {
0cbd7506 5809 if (!flag) __cxa_guard_abort (&guard);
40aac948 5810 }
2036a15c
MM
5811 }
5812
40aac948 5813 Note that the `flag' variable is only set to 1 *after* the
2036a15c
MM
5814 initialization is complete. This ensures that an exception,
5815 thrown during the construction, will cause the variable to
5816 reinitialized when we pass through this code again, as per:
68642fb6 5817
2036a15c
MM
5818 [stmt.dcl]
5819
c8094d83 5820 If the initialization exits by throwing an exception, the
2036a15c
MM
5821 initialization is not complete, so it will be tried again
5822 the next time control enters the declaration.
5823
0cbd7506 5824 This process should be thread-safe, too; multiple threads
40aac948
JM
5825 should not be able to initialize the variable more than
5826 once. */
c395453c
MM
5827
5828 /* Create the guard variable. */
5829 guard = get_guard (decl);
2036a15c 5830
445cf5eb
JM
5831 /* This optimization isn't safe on targets with relaxed memory
5832 consistency. On such targets we force synchronization in
5833 __cxa_guard_acquire. */
5834 if (!targetm.relaxed_ordering || !flag_threadsafe_statics)
5835 {
5836 /* Begin the conditional initialization. */
5837 if_stmt = begin_if_stmt ();
5838 finish_if_stmt_cond (get_guard_cond (guard), if_stmt);
5839 then_clause = begin_compound_stmt (BCS_NO_SCOPE);
5840 }
40aac948
JM
5841
5842 if (flag_threadsafe_statics)
5843 {
5844 guard_addr = build_address (guard);
40aac948
JM
5845
5846 acquire_fn = get_identifier ("__cxa_guard_acquire");
5847 release_fn = get_identifier ("__cxa_guard_release");
5848 abort_fn = get_identifier ("__cxa_guard_abort");
5849 if (!get_global_value_if_present (acquire_fn, &acquire_fn))
5850 {
5851 tree argtypes = tree_cons (NULL_TREE, TREE_TYPE (guard_addr),
5852 void_list_node);
5853 tree vfntype = build_function_type (void_type_node, argtypes);
5854 acquire_fn = push_library_fn
5855 (acquire_fn, build_function_type (integer_type_node, argtypes));
5856 release_fn = push_library_fn (release_fn, vfntype);
5857 abort_fn = push_library_fn (abort_fn, vfntype);
5858 }
5859 else
5860 {
5861 release_fn = identifier_global_value (release_fn);
5862 abort_fn = identifier_global_value (abort_fn);
5863 }
5864
5865 inner_if_stmt = begin_if_stmt ();
94a0dd7b 5866 finish_if_stmt_cond (build_call_n (acquire_fn, 1, guard_addr),
40aac948
JM
5867 inner_if_stmt);
5868
5869 inner_then_clause = begin_compound_stmt (BCS_NO_SCOPE);
5870 begin = get_target_expr (boolean_false_node);
5871 flag = TARGET_EXPR_SLOT (begin);
5872
5873 TARGET_EXPR_CLEANUP (begin)
81eba3a8
KH
5874 = build3 (COND_EXPR, void_type_node, flag,
5875 void_zero_node,
94a0dd7b 5876 build_call_n (abort_fn, 1, guard_addr));
40aac948
JM
5877 CLEANUP_EH_ONLY (begin) = 1;
5878
5879 /* Do the initialization itself. */
5880 init = add_stmt_to_compound (begin, init);
5881 init = add_stmt_to_compound
81eba3a8 5882 (init, build2 (MODIFY_EXPR, void_type_node, flag, boolean_true_node));
40aac948 5883 init = add_stmt_to_compound
94a0dd7b 5884 (init, build_call_n (release_fn, 1, guard_addr));
40aac948 5885 }
f1dedc31 5886 else
40aac948 5887 init = add_stmt_to_compound (init, set_guard (guard));
72b7eeff 5888
2036a15c
MM
5889 /* Use atexit to register a function for destroying this static
5890 variable. */
40aac948
JM
5891 init = add_stmt_to_compound (init, register_dtor_fn (decl));
5892
5893 finish_expr_stmt (init);
5894
5895 if (flag_threadsafe_statics)
5896 {
5897 finish_compound_stmt (inner_then_clause);
5898 finish_then_clause (inner_if_stmt);
5899 finish_if_stmt (inner_if_stmt);
5900 }
72b7eeff 5901
445cf5eb
JM
5902 if (!targetm.relaxed_ordering || !flag_threadsafe_statics)
5903 {
5904 finish_compound_stmt (then_clause);
5905 finish_then_clause (if_stmt);
5906 finish_if_stmt (if_stmt);
5907 }
8d08fdba
MS
5908 }
5909 else
bbd15aac 5910 static_aggregates = tree_cons (init, decl, static_aggregates);
8d08fdba 5911}
3c5c0849 5912
8d08fdba
MS
5913\f
5914/* Make TYPE a complete type based on INITIAL_VALUE.
5915 Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
07900878
PC
5916 2 if there was no information (in which case assume 0 if DO_DEFAULT),
5917 3 if the initializer list is empty (in pedantic mode). */
8d08fdba
MS
5918
5919int
aab038d5 5920cp_complete_array_type (tree *ptype, tree initial_value, bool do_default)
8d08fdba 5921{
aab038d5
RH
5922 int failure;
5923 tree type, elt_type;
68642fb6 5924
8d08fdba
MS
5925 if (initial_value)
5926 {
7b019c19 5927 /* An array of character type can be initialized from a
4038c495
GB
5928 brace-enclosed string constant.
5929
5930 FIXME: this code is duplicated from reshape_init. Probably
5931 we should just call reshape_init here? */
aab038d5 5932 if (char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (*ptype)))
7b019c19 5933 && TREE_CODE (initial_value) == CONSTRUCTOR
4038c495
GB
5934 && !VEC_empty (constructor_elt, CONSTRUCTOR_ELTS (initial_value)))
5935 {
5936 VEC(constructor_elt,gc) *v = CONSTRUCTOR_ELTS (initial_value);
5937 tree value = VEC_index (constructor_elt, v, 0)->value;
5938
5939 if (TREE_CODE (value) == STRING_CST
5940 && VEC_length (constructor_elt, v) == 1)
5941 initial_value = value;
5942 }
8d08fdba
MS
5943 }
5944
aab038d5 5945 failure = complete_array_type (ptype, initial_value, do_default);
8d08fdba 5946
aab038d5
RH
5947 /* We can create the array before the element type is complete, which
5948 means that we didn't have these two bits set in the original type
5949 either. In completing the type, we are expected to propagate these
5950 bits. See also complete_type which does the same thing for arrays
5951 of fixed size. */
5952 type = *ptype;
5953 if (TYPE_DOMAIN (type))
8d08fdba 5954 {
81b4398d 5955 elt_type = TREE_TYPE (type);
aab038d5 5956 TYPE_NEEDS_CONSTRUCTING (type) = TYPE_NEEDS_CONSTRUCTING (elt_type);
81b4398d 5957 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
aab038d5 5958 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (elt_type);
8d08fdba
MS
5959 }
5960
aab038d5 5961 return failure;
8d08fdba
MS
5962}
5963\f
5964/* Return zero if something is declared to be a member of type
5965 CTYPE when in the context of CUR_TYPE. STRING is the error
5966 message to print in that case. Otherwise, quietly return 1. */
e92cc029 5967
8d08fdba 5968static int
11f6b451 5969member_function_or_else (tree ctype, tree cur_type, enum overload_flags flags)
8d08fdba
MS
5970{
5971 if (ctype && ctype != cur_type)
5972 {
4dacf5bd 5973 if (flags == DTOR_FLAG)
2d01edd7 5974 error ("destructor for alien class %qT cannot be a member", ctype);
4dacf5bd 5975 else
2d01edd7 5976 error ("constructor for alien class %qT cannot be a member", ctype);
8d08fdba
MS
5977 return 0;
5978 }
5979 return 1;
5980}
5981\f
5982/* Subroutine of `grokdeclarator'. */
5983
5984/* Generate errors possibly applicable for a given set of specifiers.
5985 This is for ARM $7.1.2. */
e92cc029 5986
8d08fdba 5987static void
11f6b451 5988bad_specifiers (tree object,
0cbd7506
MS
5989 const char* type,
5990 int virtualp,
5991 int quals,
5992 int inlinep,
5993 int friendp,
5994 int raises)
8d08fdba
MS
5995{
5996 if (virtualp)
2d01edd7 5997 error ("%qD declared as a %<virtual%> %s", object, type);
8d08fdba 5998 if (inlinep)
2d01edd7 5999 error ("%qD declared as an %<inline%> %s", object, type);
8d08fdba 6000 if (quals)
2d01edd7 6001 error ("%<const%> and %<volatile%> function specifiers on "
0cbd7506
MS
6002 "%qD invalid in %s declaration",
6003 object, type);
8d08fdba 6004 if (friendp)
dee15844 6005 error ("%q+D declared as a friend", object);
e1be26f4
RS
6006 if (raises
6007 && (TREE_CODE (object) == TYPE_DECL
6008 || (!TYPE_PTRFN_P (TREE_TYPE (object))
742a37d5 6009 && !TYPE_REFFN_P (TREE_TYPE (object))
e1be26f4 6010 && !TYPE_PTRMEMFUNC_P (TREE_TYPE (object)))))
dee15844 6011 error ("%q+D declared with an exception specification", object);
8d08fdba
MS
6012}
6013
9c12301f
MM
6014/* DECL is a member function or static data member and is presently
6015 being defined. Check that the definition is taking place in a
6016 valid namespace. */
6017
6018static void
6019check_class_member_definition_namespace (tree decl)
6020{
6021 /* These checks only apply to member functions and static data
6022 members. */
6023 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
6024 || TREE_CODE (decl) == VAR_DECL);
6025 /* We check for problems with specializations in pt.c in
6026 check_specialization_namespace, where we can issue better
6027 diagnostics. */
6028 if (processing_specialization)
6029 return;
6030 /* There are no restrictions on the placement of
6031 explicit instantiations. */
6032 if (processing_explicit_instantiation)
6033 return;
6034 /* [class.mfct]
6035
6036 A member function definition that appears outside of the
6037 class definition shall appear in a namespace scope enclosing
6038 the class definition.
6039
6040 [class.static.data]
6041
6042 The definition for a static data member shall appear in a
6043 namespace scope enclosing the member's class definition. */
6044 if (!is_ancestor (current_namespace, DECL_CONTEXT (decl)))
6045 pedwarn ("definition of %qD is not in namespace enclosing %qT",
6046 decl, DECL_CONTEXT (decl));
6047}
6048
e2537f2c
MM
6049/* Build a PARM_DECL for the "this" parameter. TYPE is the
6050 METHOD_TYPE for a non-static member function; QUALS are the
6051 cv-qualifiers that apply to the function. */
3db45ab5 6052
e2537f2c
MM
6053tree
6054build_this_parm (tree type, cp_cv_quals quals)
6055{
6056 tree this_type;
6057 tree qual_type;
6058 tree parm;
6059 cp_cv_quals this_quals;
6060
6061 this_type = TREE_VALUE (TYPE_ARG_TYPES (type));
6062 /* The `this' parameter is implicitly `const'; it cannot be
6063 assigned to. */
6064 this_quals = (quals & TYPE_QUAL_RESTRICT) | TYPE_QUAL_CONST;
6065 qual_type = cp_build_qualified_type (this_type, this_quals);
6066 parm = build_artificial_parm (this_identifier, qual_type);
6067 cp_apply_type_quals_to_decl (this_quals, parm);
6068 return parm;
3db45ab5 6069}
e2537f2c 6070
8d08fdba
MS
6071/* CTYPE is class type, or null if non-class.
6072 TYPE is type this FUNCTION_DECL should have, either FUNCTION_TYPE
6073 or METHOD_TYPE.
6074 DECLARATOR is the function's name.
4546865e 6075 PARMS is a chain of PARM_DECLs for the function.
8d08fdba
MS
6076 VIRTUALP is truthvalue of whether the function is virtual or not.
6077 FLAGS are to be passed through to `grokclassfn'.
6078 QUALS are qualifiers indicating whether the function is `const'
6079 or `volatile'.
6080 RAISES is a list of exceptions that this function can raise.
6081 CHECK is 1 if we must find this method in CTYPE, 0 if we should
68642fb6 6082 not look, and -1 if we should not call `grokclassfn' at all.
3ddfb0e6 6083
27d6592c
MM
6084 SFK is the kind of special function (if any) for the new function.
6085
20496fa2 6086 Returns `NULL_TREE' if something goes wrong, after issuing
3ddfb0e6 6087 applicable error messages. */
e92cc029 6088
8d08fdba 6089static tree
caf93cb0 6090grokfndecl (tree ctype,
0cbd7506
MS
6091 tree type,
6092 tree declarator,
4546865e 6093 tree parms,
0cbd7506
MS
6094 tree orig_declarator,
6095 int virtualp,
6096 enum overload_flags flags,
3c01e5df 6097 cp_cv_quals quals,
0cbd7506
MS
6098 tree raises,
6099 int check,
6100 int friendp,
6101 int publicp,
6102 int inlinep,
27d6592c 6103 special_function_kind sfk,
357d956e 6104 bool funcdef_flag,
0cbd7506
MS
6105 int template_count,
6106 tree in_namespace,
037cc9c5 6107 tree* attrlist)
8d08fdba 6108{
1951a1b6 6109 tree decl;
8d08fdba 6110 int staticp = ctype && TREE_CODE (type) == FUNCTION_TYPE;
42976354 6111 tree t;
8d08fdba 6112
8d08fdba 6113 if (raises)
271e6f02 6114 type = build_exception_variant (type, raises);
c11b6f21 6115
8d08fdba 6116 decl = build_lang_decl (FUNCTION_DECL, declarator, type);
e2537f2c
MM
6117 if (TREE_CODE (type) == METHOD_TYPE)
6118 {
6119 tree parm;
6120 parm = build_this_parm (type, quals);
6121 TREE_CHAIN (parm) = parms;
6122 parms = parm;
6123 }
4546865e 6124 DECL_ARGUMENTS (decl) = parms;
a1c65f9f 6125 /* Propagate volatile out from type to decl. */
8d08fdba 6126 if (TYPE_VOLATILE (type))
893de33c 6127 TREE_THIS_VOLATILE (decl) = 1;
8d08fdba 6128
71aea5f2
SM
6129 /* Setup decl according to sfk. */
6130 switch (sfk)
6131 {
6132 case sfk_constructor:
6133 case sfk_copy_constructor:
6134 DECL_CONSTRUCTOR_P (decl) = 1;
6135 break;
6136 case sfk_destructor:
6137 DECL_DESTRUCTOR_P (decl) = 1;
6138 break;
6139 default:
6140 break;
6141 }
6142
837edd5f
GK
6143 /* If pointers to member functions use the least significant bit to
6144 indicate whether a function is virtual, ensure a pointer
6145 to this function will have that bit clear. */
6146 if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_pfn
6147 && TREE_CODE (type) == METHOD_TYPE
6148 && DECL_ALIGN (decl) < 2 * BITS_PER_UNIT)
6149 DECL_ALIGN (decl) = 2 * BITS_PER_UNIT;
6150
abc088aa
MM
6151 if (friendp
6152 && TREE_CODE (orig_declarator) == TEMPLATE_ID_EXPR)
6153 {
6154 if (funcdef_flag)
6155 error
6156 ("defining explicit specialization %qD in friend declaration",
6157 orig_declarator);
6158 else
6159 {
6160 tree fns = TREE_OPERAND (orig_declarator, 0);
6161 tree args = TREE_OPERAND (orig_declarator, 1);
6162
6163 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
6164 {
6165 /* Something like `template <class T> friend void f<T>()'. */
6166 error ("invalid use of template-id %qD in declaration "
6167 "of primary template",
6168 orig_declarator);
6169 return NULL_TREE;
6170 }
6171
6172
6173 /* A friend declaration of the form friend void f<>(). Record
6174 the information in the TEMPLATE_ID_EXPR. */
6175 SET_DECL_IMPLICIT_INSTANTIATION (decl);
6176
6177 if (TREE_CODE (fns) == COMPONENT_REF)
6178 {
6179 /* Due to bison parser ickiness, we will have already looked
6180 up an operator_name or PFUNCNAME within the current class
6181 (see template_id in parse.y). If the current class contains
6182 such a name, we'll get a COMPONENT_REF here. Undo that. */
6183
6184 gcc_assert (TREE_TYPE (TREE_OPERAND (fns, 0))
6185 == current_class_type);
6186 fns = TREE_OPERAND (fns, 1);
6187 }
6188 gcc_assert (TREE_CODE (fns) == IDENTIFIER_NODE
6189 || TREE_CODE (fns) == OVERLOAD);
6190 DECL_TEMPLATE_INFO (decl) = tree_cons (fns, args, NULL_TREE);
6191
6192 for (t = TYPE_ARG_TYPES (TREE_TYPE (decl)); t; t = TREE_CHAIN (t))
6193 if (TREE_PURPOSE (t)
6194 && TREE_CODE (TREE_PURPOSE (t)) == DEFAULT_ARG)
6195 {
6196 error ("default arguments are not allowed in declaration "
6197 "of friend template specialization %qD",
6198 decl);
6199 return NULL_TREE;
6200 }
6201
6202 if (inlinep)
6203 {
6204 error ("%<inline%> is not allowed in declaration of friend "
6205 "template specialization %qD",
6206 decl);
6207 return NULL_TREE;
6208 }
6209 }
6210 }
6211
79c4d4b7 6212 /* If this decl has namespace scope, set that up. */
2c73f9f5 6213 if (in_namespace)
b262d64c 6214 set_decl_namespace (decl, in_namespace, friendp);
adae082f 6215 else if (!ctype)
79c4d4b7 6216 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
2c73f9f5 6217
0f8766b8
JM
6218 /* `main' and builtins have implicit 'C' linkage. */
6219 if ((MAIN_NAME_P (declarator)
6220 || (IDENTIFIER_LENGTH (declarator) > 10
6221 && IDENTIFIER_POINTER (declarator)[0] == '_'
6222 && IDENTIFIER_POINTER (declarator)[1] == '_'
6223 && strncmp (IDENTIFIER_POINTER (declarator)+2, "builtin_", 8) == 0))
6224 && current_lang_name == lang_name_cplusplus
94706a5c 6225 && ctype == NULL_TREE
79c4d4b7
JM
6226 /* NULL_TREE means global namespace. */
6227 && DECL_CONTEXT (decl) == NULL_TREE)
5d2ed28c 6228 SET_DECL_LANGUAGE (decl, lang_c);
0f8766b8 6229
8d08fdba
MS
6230 /* Should probably propagate const out from type to decl I bet (mrs). */
6231 if (staticp)
6232 {
6233 DECL_STATIC_FUNCTION_P (decl) = 1;
6234 DECL_CONTEXT (decl) = ctype;
8d08fdba
MS
6235 }
6236
e76a2646 6237 if (ctype)
9c12301f
MM
6238 {
6239 DECL_CONTEXT (decl) = ctype;
6240 if (funcdef_flag)
6241 check_class_member_definition_namespace (decl);
6242 }
e76a2646 6243
0f8766b8 6244 if (ctype == NULL_TREE && DECL_MAIN_P (decl))
faae18ab 6245 {
848b92e1 6246 if (processing_template_decl)
2d01edd7 6247 error ("cannot declare %<::main%> to be a template");
faae18ab 6248 if (inlinep)
2d01edd7 6249 error ("cannot declare %<::main%> to be inline");
f22967f3 6250 if (!publicp)
2d01edd7 6251 error ("cannot declare %<::main%> to be static");
faae18ab
MS
6252 inlinep = 0;
6253 publicp = 1;
6254 }
50a6dbd7 6255
59e76fc6 6256 /* Members of anonymous types and local classes have no linkage; make
9fc444cc 6257 them internal. If a typedef is made later, this will be changed. */
1951a1b6 6258 if (ctype && (TYPE_ANONYMOUS_P (ctype)
4f1c5b7d 6259 || decl_function_context (TYPE_MAIN_DECL (ctype))))
50a6dbd7
JM
6260 publicp = 0;
6261
6262 if (publicp)
6263 {
6264 /* [basic.link]: A name with no linkage (notably, the name of a class
6265 or enumeration declared in a local scope) shall not be used to
6266 declare an entity with linkage.
6267
50b97e0f 6268 Only check this for public decls for now. See core 319, 389. */
4684cd27
MM
6269 t = no_linkage_check (TREE_TYPE (decl),
6270 /*relaxed_p=*/false);
50a6dbd7
JM
6271 if (t)
6272 {
1951a1b6 6273 if (TYPE_ANONYMOUS_P (t))
7f7c930e 6274 {
eb68cb58 6275 if (DECL_EXTERN_C_P (decl))
7f7c930e
JM
6276 /* Allow this; it's pretty common in C. */;
6277 else
1951a1b6 6278 {
2d01edd7 6279 pedwarn ("non-local function %q#D uses anonymous type",
1951a1b6
JM
6280 decl);
6281 if (DECL_ORIGINAL_TYPE (TYPE_NAME (t)))
dee15844
JM
6282 pedwarn ("%q+#D does not refer to the unqualified "
6283 "type, so it is not used for linkage",
6284 TYPE_NAME (t));
1951a1b6 6285 }
7f7c930e 6286 }
50a6dbd7 6287 else
2d01edd7 6288 pedwarn ("non-local function %q#D uses local type %qT", decl, t);
50a6dbd7
JM
6289 }
6290 }
6291
893de33c 6292 TREE_PUBLIC (decl) = publicp;
faae18ab 6293 if (! publicp)
893de33c
JM
6294 {
6295 DECL_INTERFACE_KNOWN (decl) = 1;
6296 DECL_NOT_REALLY_EXTERN (decl) = 1;
6297 }
faae18ab 6298
acc72c37 6299 /* If the declaration was declared inline, mark it as such. */
faae18ab 6300 if (inlinep)
acc72c37
MM
6301 DECL_DECLARED_INLINE_P (decl) = 1;
6302 /* We inline functions that are explicitly declared inline, or, when
6303 the user explicitly asks us to, all functions. */
b3c3af2f
SB
6304 if (DECL_DECLARED_INLINE_P (decl)
6305 || (flag_inline_trees == 2 && !DECL_INLINE (decl) && funcdef_flag))
acc72c37 6306 DECL_INLINE (decl) = 1;
8d08fdba
MS
6307
6308 DECL_EXTERNAL (decl) = 1;
3c01e5df 6309 if (quals && TREE_CODE (type) == FUNCTION_TYPE)
8d08fdba 6310 {
e7c41c99
DM
6311 error (ctype
6312 ? G_("static member function %qD cannot have cv-qualifier")
32b1d579 6313 : G_("non-member function %qD cannot have cv-qualifier"),
e7c41c99 6314 decl);
3c01e5df 6315 quals = TYPE_UNQUALIFIED;
8d08fdba
MS
6316 }
6317
398cd199
VR
6318 if (IDENTIFIER_OPNAME_P (DECL_NAME (decl))
6319 && !grok_op_properties (decl, /*complain=*/true))
6320 return NULL_TREE;
8d08fdba 6321
4f1c5b7d 6322 if (ctype && decl_function_context (decl))
893de33c 6323 DECL_NO_STATIC_CHAIN (decl) = 1;
e76a2646 6324
1eb0072d
JM
6325 if (funcdef_flag)
6326 /* Make the init_value nonzero so pushdecl knows this is not
6327 tentative. error_mark_node is replaced later with the BLOCK. */
6328 DECL_INITIAL (decl) = error_mark_node;
6329
93ca4ba7 6330 if (TYPE_NOTHROW_P (type) || nothrow_libfn_p (decl))
1660cb3a
JM
6331 TREE_NOTHROW (decl) = 1;
6332
75650646 6333 /* Caller will do the rest of this. */
8d08fdba
MS
6334 if (check < 0)
6335 return decl;
6336
74b846e0 6337 if (ctype != NULL_TREE)
71aea5f2 6338 grokclassfn (ctype, decl, flags);
74b846e0
MM
6339
6340 decl = check_explicit_specialization (orig_declarator, decl,
6341 template_count,
357d956e 6342 2 * funcdef_flag +
74b846e0
MM
6343 4 * (friendp != 0));
6344 if (decl == error_mark_node)
6345 return NULL_TREE;
98c1c668 6346
037cc9c5
FJ
6347 if (attrlist)
6348 {
6349 cplus_decl_attributes (&decl, *attrlist, 0);
6350 *attrlist = NULL_TREE;
6351 }
6352
da337784
JJ
6353 /* Check main's type after attributes have been applied. */
6354 if (ctype == NULL_TREE && DECL_MAIN_P (decl))
6355 {
6356 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
6357 integer_type_node))
6358 {
6359 tree oldtypeargs = TYPE_ARG_TYPES (TREE_TYPE (decl));
6360 tree newtype;
6361 error ("%<::main%> must return %<int%>");
6362 newtype = build_function_type (integer_type_node, oldtypeargs);
6363 TREE_TYPE (decl) = newtype;
6364 }
6365 check_main_parameter_types (decl);
6366 }
6367
74b846e0
MM
6368 if (ctype != NULL_TREE
6369 && (! TYPE_FOR_JAVA (ctype) || check_java_method (decl))
6370 && check)
8d08fdba 6371 {
74b846e0 6372 tree old_decl;
8d08fdba 6373
d43f603d 6374 old_decl = check_classfn (ctype, decl,
44021471
GB
6375 (processing_template_decl
6376 > template_class_depth (ctype))
6377 ? current_template_parms
c8094d83 6378 : NULL_TREE);
74b846e0 6379 if (old_decl)
8d08fdba 6380 {
79ae853c 6381 tree ok;
4514aa8c 6382 tree pushed_scope;
316a2456 6383
357d956e
MM
6384 if (TREE_CODE (old_decl) == TEMPLATE_DECL)
6385 /* Because grokfndecl is always supposed to return a
6386 FUNCTION_DECL, we pull out the DECL_TEMPLATE_RESULT
6387 here. We depend on our callers to figure out that its
6388 really a template that's being returned. */
6389 old_decl = DECL_TEMPLATE_RESULT (old_decl);
6390
6391 if (DECL_STATIC_FUNCTION_P (old_decl)
6392 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE)
6393 /* Remove the `this' parm added by grokclassfn.
6394 XXX Isn't this done in start_function, too? */
6395 revert_static_member_fn (decl);
6396 if (DECL_ARTIFICIAL (old_decl))
6397 error ("definition of implicitly-declared %qD", old_decl);
6398
74b846e0
MM
6399 /* Since we've smashed OLD_DECL to its
6400 DECL_TEMPLATE_RESULT, we must do the same to DECL. */
6401 if (TREE_CODE (decl) == TEMPLATE_DECL)
6402 decl = DECL_TEMPLATE_RESULT (decl);
6403
6404 /* Attempt to merge the declarations. This can fail, in
0e339752 6405 the case of some invalid specialization declarations. */
4514aa8c 6406 pushed_scope = push_scope (ctype);
d63d5d0c 6407 ok = duplicate_decls (decl, old_decl, friendp);
4514aa8c
NS
6408 if (pushed_scope)
6409 pop_scope (pushed_scope);
316a2456
MM
6410 if (!ok)
6411 {
2d01edd7 6412 error ("no %q#D member function declared in class %qT",
316a2456
MM
6413 decl, ctype);
6414 return NULL_TREE;
6415 }
74b846e0 6416 return old_decl;
8d08fdba
MS
6417 }
6418 }
74b846e0
MM
6419
6420 if (DECL_CONSTRUCTOR_P (decl) && !grok_ctor_properties (ctype, decl))
6421 return NULL_TREE;
6422
6423 if (ctype == NULL_TREE || check)
6424 return decl;
6425
6426 if (virtualp)
cbb40945 6427 DECL_VIRTUAL_P (decl) = 1;
74b846e0 6428
8d08fdba
MS
6429 return decl;
6430}
6431
4684cd27
MM
6432/* DECL is a VAR_DECL for a static data member. Set flags to reflect
6433 the linkage that DECL will receive in the object file. */
6434
6435static void
6436set_linkage_for_static_data_member (tree decl)
6437{
6438 /* A static data member always has static storage duration and
6439 external linkage. Note that static data members are forbidden in
6440 local classes -- the only situation in which a class has
6441 non-external linkage. */
6442 TREE_PUBLIC (decl) = 1;
6443 TREE_STATIC (decl) = 1;
6444 /* For non-template classes, static data members are always put
6445 out in exactly those files where they are defined, just as
77880ae4 6446 with ordinary namespace-scope variables. */
4684cd27
MM
6447 if (!processing_template_decl)
6448 DECL_INTERFACE_KNOWN (decl) = 1;
6449}
6450
caf93cb0 6451/* Create a VAR_DECL named NAME with the indicated TYPE.
9e259dd1
MM
6452
6453 If SCOPE is non-NULL, it is the class type or namespace containing
6454 the variable. If SCOPE is NULL, the variable should is created in
6455 the innermost enclosings scope. */
6456
8d08fdba 6457static tree
11f6b451 6458grokvardecl (tree type,
0cbd7506 6459 tree name,
1ff3c076 6460 const cp_decl_specifier_seq *declspecs,
0cbd7506
MS
6461 int initialized,
6462 int constp,
6463 tree scope)
8d08fdba
MS
6464{
6465 tree decl;
5ae9ba3e 6466 tree explicit_scope;
f7da6097 6467
50bc768d 6468 gcc_assert (!name || TREE_CODE (name) == IDENTIFIER_NODE);
9e259dd1 6469
5ae9ba3e
MM
6470 /* Compute the scope in which to place the variable, but remember
6471 whether or not that scope was explicitly specified by the user. */
6472 explicit_scope = scope;
9e259dd1 6473 if (!scope)
8d08fdba 6474 {
9e259dd1
MM
6475 /* An explicit "extern" specifier indicates a namespace-scope
6476 variable. */
62d1db17 6477 if (declspecs->storage_class == sc_extern)
9e259dd1
MM
6478 scope = current_namespace;
6479 else if (!at_function_scope_p ())
a5201a91 6480 scope = current_scope ();
9e259dd1
MM
6481 }
6482
6483 if (scope
6484 && (/* If the variable is a namespace-scope variable declared in a
6485 template, we need DECL_LANG_SPECIFIC. */
6486 (TREE_CODE (scope) == NAMESPACE_DECL && processing_template_decl)
6487 /* Similarly for namespace-scope variables with language linkage
6488 other than C++. */
caf93cb0 6489 || (TREE_CODE (scope) == NAMESPACE_DECL
9e259dd1
MM
6490 && current_lang_name != lang_name_cplusplus)
6491 /* Similarly for static data members. */
6492 || TYPE_P (scope)))
6493 decl = build_lang_decl (VAR_DECL, name, type);
8d08fdba 6494 else
9e259dd1 6495 decl = build_decl (VAR_DECL, name, type);
79c4d4b7 6496
5ae9ba3e
MM
6497 if (explicit_scope && TREE_CODE (explicit_scope) == NAMESPACE_DECL)
6498 set_decl_namespace (decl, explicit_scope, 0);
9e259dd1 6499 else
74b0d490 6500 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
6060a796 6501
62d1db17 6502 if (declspecs->storage_class == sc_extern)
8d08fdba
MS
6503 {
6504 DECL_THIS_EXTERN (decl) = 1;
6505 DECL_EXTERNAL (decl) = !initialized;
6506 }
6507
2b9dc906 6508 if (DECL_CLASS_SCOPE_P (decl))
8d08fdba 6509 {
4684cd27
MM
6510 set_linkage_for_static_data_member (decl);
6511 /* This function is only called with out-of-class definitions. */
5b605f68 6512 DECL_EXTERNAL (decl) = 0;
9c12301f 6513 check_class_member_definition_namespace (decl);
8d08fdba
MS
6514 }
6515 /* At top level, either `static' or no s.c. makes a definition
6516 (perhaps tentative), and absence of `static' makes it public. */
a9aedbc2 6517 else if (toplevel_bindings_p ())
8d08fdba 6518 {
62d1db17 6519 TREE_PUBLIC (decl) = (declspecs->storage_class != sc_static
9fffd093 6520 && (DECL_THIS_EXTERN (decl) || ! constp));
8d08fdba
MS
6521 TREE_STATIC (decl) = ! DECL_EXTERNAL (decl);
6522 }
6523 /* Not at top level, only `static' makes a static definition. */
6524 else
6525 {
62d1db17 6526 TREE_STATIC (decl) = declspecs->storage_class == sc_static;
8d08fdba
MS
6527 TREE_PUBLIC (decl) = DECL_EXTERNAL (decl);
6528 }
50a6dbd7 6529
62d1db17 6530 if (declspecs->specs[(int)ds_thread])
8893239d 6531 DECL_TLS_MODEL (decl) = decl_default_tls_model (decl);
7a1f3f5f 6532
50a6dbd7
JM
6533 if (TREE_PUBLIC (decl))
6534 {
6535 /* [basic.link]: A name with no linkage (notably, the name of a class
6536 or enumeration declared in a local scope) shall not be used to
6537 declare an entity with linkage.
6538
03fd3f84 6539 Only check this for public decls for now. */
6b211746 6540 tree t = no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/false);
50a6dbd7
JM
6541 if (t)
6542 {
1951a1b6 6543 if (TYPE_ANONYMOUS_P (t))
9fc444cc
GK
6544 {
6545 if (DECL_EXTERN_C_P (decl))
6bdb98d1
MA
6546 /* Allow this; it's pretty common in C. */
6547 ;
9fc444cc
GK
6548 else
6549 {
6b211746
AO
6550 /* DRs 132, 319 and 389 seem to indicate types with
6551 no linkage can only be used to declare extern "C"
6552 entities. Since it's not always an error in the
6553 ISO C++ 90 Standard, we only issue a warning. */
d4ee4d25 6554 warning (0, "non-local variable %q#D uses anonymous type",
9fc444cc
GK
6555 decl);
6556 if (DECL_ORIGINAL_TYPE (TYPE_NAME (t)))
dee15844
JM
6557 warning (0, "%q+#D does not refer to the unqualified "
6558 "type, so it is not used for linkage",
6559 TYPE_NAME (t));
9fc444cc
GK
6560 }
6561 }
50a6dbd7 6562 else
d4ee4d25 6563 warning (0, "non-local variable %q#D uses local type %qT", decl, t);
50a6dbd7
JM
6564 }
6565 }
1ceb02be
MM
6566 else
6567 DECL_INTERFACE_KNOWN (decl) = 1;
50a6dbd7 6568
8d08fdba
MS
6569 return decl;
6570}
6571
d8f8dca1
MM
6572/* Create and return a canonical pointer to member function type, for
6573 TYPE, which is a POINTER_TYPE to a METHOD_TYPE. */
8d08fdba
MS
6574
6575tree
11f6b451 6576build_ptrmemfunc_type (tree type)
8d08fdba 6577{
4977bab6 6578 tree field, fields;
8d08fdba 6579 tree t;
46cbda4a 6580 tree unqualified_variant = NULL_TREE;
8d08fdba 6581
d48ebde1
NS
6582 if (type == error_mark_node)
6583 return type;
d363e7bf 6584
8d08fdba
MS
6585 /* If a canonical type already exists for this type, use it. We use
6586 this method instead of type_hash_canon, because it only does a
6587 simple equality check on the list of field members. */
6588
6589 if ((t = TYPE_GET_PTRMEMFUNC_TYPE (type)))
6590 return t;
6591
46cbda4a
MM
6592 /* Make sure that we always have the unqualified pointer-to-member
6593 type first. */
89d684bb 6594 if (cp_type_quals (type) != TYPE_UNQUALIFIED)
68642fb6 6595 unqualified_variant
46cbda4a
MM
6596 = build_ptrmemfunc_type (TYPE_MAIN_VARIANT (type));
6597
33848bb0 6598 t = make_aggr_type (RECORD_TYPE);
cad7e87b 6599 xref_basetypes (t, NULL_TREE);
caf93cb0 6600
3b426391 6601 /* Let the front end know this is a pointer to member function... */
db5ae43f 6602 TYPE_PTRMEMFUNC_FLAG (t) = 1;
2c73f9f5 6603 /* ... and not really an aggregate. */
7ddedda4 6604 SET_IS_AGGR_TYPE (t, 0);
8d08fdba 6605
4977bab6
ZW
6606 field = build_decl (FIELD_DECL, pfn_identifier, type);
6607 fields = field;
caf93cb0 6608
4977bab6
ZW
6609 field = build_decl (FIELD_DECL, delta_identifier, delta_type_node);
6610 TREE_CHAIN (field) = fields;
6611 fields = field;
caf93cb0 6612
4977bab6 6613 finish_builtin_struct (t, "__ptrmemfunc_type", fields, ptr_type_node);
8d08fdba 6614
3b426391 6615 /* Zap out the name so that the back end will give us the debugging
8d08fdba
MS
6616 information for this anonymous RECORD_TYPE. */
6617 TYPE_NAME (t) = NULL_TREE;
6618
46cbda4a
MM
6619 /* If this is not the unqualified form of this pointer-to-member
6620 type, set the TYPE_MAIN_VARIANT for this type to be the
6621 unqualified type. Since they are actually RECORD_TYPEs that are
6622 not variants of each other, we must do this manually. */
89d684bb 6623 if (cp_type_quals (type) != TYPE_UNQUALIFIED)
46cbda4a 6624 {
89d684bb 6625 t = build_qualified_type (t, cp_type_quals (type));
46cbda4a
MM
6626 TYPE_MAIN_VARIANT (t) = unqualified_variant;
6627 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (unqualified_variant);
6628 TYPE_NEXT_VARIANT (unqualified_variant) = t;
6629 }
6630
6631 /* Cache this pointer-to-member type so that we can find it again
6632 later. */
8d08fdba
MS
6633 TYPE_SET_PTRMEMFUNC_TYPE (type, t);
6634
8de08f4c
DG
6635 /* Managing canonical types for the RECORD_TYPE behind a
6636 pointer-to-member function is a nightmare, so use structural
6637 equality for now. */
6638 SET_TYPE_STRUCTURAL_EQUALITY (t);
06d40de8 6639
8d08fdba
MS
6640 return t;
6641}
6642
9e259dd1
MM
6643/* Create and return a pointer to data member type. */
6644
6645tree
6646build_ptrmem_type (tree class_type, tree member_type)
6647{
a5ac359a
MM
6648 if (TREE_CODE (member_type) == METHOD_TYPE)
6649 {
6650 tree arg_types;
6651
6652 arg_types = TYPE_ARG_TYPES (member_type);
caf93cb0 6653 class_type = (cp_build_qualified_type
a5ac359a
MM
6654 (class_type,
6655 cp_type_quals (TREE_TYPE (TREE_VALUE (arg_types)))));
caf93cb0
EC
6656 member_type
6657 = build_method_type_directly (class_type,
43dc123f
MM
6658 TREE_TYPE (member_type),
6659 TREE_CHAIN (arg_types));
a5ac359a
MM
6660 return build_ptrmemfunc_type (build_pointer_type (member_type));
6661 }
6662 else
6663 {
50bc768d 6664 gcc_assert (TREE_CODE (member_type) != FUNCTION_TYPE);
a5ac359a
MM
6665 return build_offset_type (class_type, member_type);
6666 }
9e259dd1
MM
6667}
6668
b17e2870
JM
6669/* DECL is a VAR_DECL defined in-class, whose TYPE is also given.
6670 Check to see that the definition is valid. Issue appropriate error
6671 messages. Return 1 if the definition is particularly bad, or 0
6672 otherwise. */
6673
6674int
11f6b451 6675check_static_variable_definition (tree decl, tree type)
b17e2870
JM
6676{
6677 /* Motion 10 at San Diego: If a static const integral data member is
6678 initialized with an integral constant expression, the initializer
6679 may appear either in the declaration (within the class), or in
6680 the definition, but not both. If it appears in the class, the
6681 member is a member constant. The file-scope definition is always
6682 required. */
dcba9b0f 6683 if (!ARITHMETIC_TYPE_P (type) && TREE_CODE (type) != ENUMERAL_TYPE)
b17e2870 6684 {
2d01edd7 6685 error ("invalid in-class initialization of static data member "
0cbd7506 6686 "of non-integral type %qT",
dcba9b0f 6687 type);
b17e2870 6688 /* If we just return the declaration, crashes will sometimes
852dcbdd 6689 occur. We therefore return void_type_node, as if this were a
b17e2870
JM
6690 friend declaration, to cause callers to completely ignore
6691 this declaration. */
6692 return 1;
6693 }
6694 else if (!CP_TYPE_CONST_P (type))
2d01edd7 6695 error ("ISO C++ forbids in-class initialization of non-const "
0cbd7506
MS
6696 "static member %qD",
6697 decl);
b17e2870 6698 else if (pedantic && !INTEGRAL_TYPE_P (type))
2d01edd7 6699 pedwarn ("ISO C++ forbids initialization of member constant "
0cbd7506 6700 "%qD of non-integral type %qT", decl, type);
b17e2870
JM
6701
6702 return 0;
6703}
6704
2848ad0f
MM
6705/* Given the SIZE (i.e., number of elements) in an array, compute an
6706 appropriate index type for the array. If non-NULL, NAME is the
6707 name of the thing being declared. */
6708
c95cd22e 6709tree
11f6b451 6710compute_array_index_type (tree name, tree size)
2848ad0f 6711{
d174af6c 6712 tree type;
2848ad0f 6713 tree itype;
06d40de8 6714 tree abi_1_itype = NULL_TREE;
2848ad0f 6715
d174af6c
MM
6716 if (error_operand_p (size))
6717 return error_mark_node;
6718
6719 type = TREE_TYPE (size);
d63b5e9f
NS
6720 /* The array bound must be an integer type. */
6721 if (!dependent_type_p (type) && !INTEGRAL_TYPE_P (type))
2848ad0f 6722 {
d63b5e9f 6723 if (name)
2d01edd7 6724 error ("size of array %qD has non-integral type %qT", name, type);
d63b5e9f 6725 else
2d01edd7 6726 error ("size of array has non-integral type %qT", type);
d63b5e9f
NS
6727 size = integer_one_node;
6728 type = TREE_TYPE (size);
2848ad0f
MM
6729 }
6730
06d40de8
DG
6731 if (value_dependent_expression_p (size))
6732 {
6733 /* We cannot do any checking for a value-dependent SIZE. Just
6734 build the index type and mark that it requires structural
6735 equality checks. */
6736 itype = build_index_type (build_min (MINUS_EXPR, sizetype,
6737 size, integer_one_node));
6738 SET_TYPE_STRUCTURAL_EQUALITY (itype);
6739 return itype;
6740 }
6741
6742 if (!abi_version_at_least (2) && processing_template_decl)
6743 /* For abi-1, we handled all instances in templates the same way,
6744 even when they were non-dependent. This effects the manglings
6745 produced. So, we do the normal checking for non-dependent
6746 sizes, but at the end we'll return the same type that abi-1
6747 would have, but with TYPE_CANONICAL set to the "right"
6748 value that the current ABI would provide. */
6749 abi_1_itype = build_index_type (build_min (MINUS_EXPR, sizetype,
6750 size, integer_one_node));
d63b5e9f 6751
a1c65f9f 6752 /* The size might be the result of a cast. */
80f5bb34
MM
6753 STRIP_TYPE_NOPS (size);
6754
6755 /* It might be a const variable or enumeration constant. */
8a784e4a 6756 size = integral_constant_value (size);
80f5bb34 6757
2848ad0f 6758 /* Normally, the array-bound will be a constant. */
2bb5d995 6759 if (TREE_CODE (size) == INTEGER_CST)
2848ad0f
MM
6760 {
6761 /* Check to see if the array bound overflowed. Make that an
6762 error, no matter how generous we're being. */
6763 int old_flag_pedantic_errors = flag_pedantic_errors;
6764 int old_pedantic = pedantic;
6765 pedantic = flag_pedantic_errors = 1;
6766 constant_expression_warning (size);
6767 pedantic = old_pedantic;
6768 flag_pedantic_errors = old_flag_pedantic_errors;
6769
6770 /* An array must have a positive number of elements. */
6771 if (INT_CST_LT (size, integer_zero_node))
6772 {
d67cdbc3 6773 if (name)
2d01edd7 6774 error ("size of array %qD is negative", name);
d67cdbc3 6775 else
33bd39a2 6776 error ("size of array is negative");
2848ad0f
MM
6777 size = integer_one_node;
6778 }
a8e6c82a
MM
6779 /* As an extension we allow zero-sized arrays. We always allow
6780 them in system headers because glibc uses them. */
2848ad0f 6781 else if (integer_zerop (size) && pedantic && !in_system_header)
d67cdbc3
JM
6782 {
6783 if (name)
2d01edd7 6784 pedwarn ("ISO C++ forbids zero-size array %qD", name);
d67cdbc3 6785 else
33bd39a2 6786 pedwarn ("ISO C++ forbids zero-size array");
d67cdbc3 6787 }
2848ad0f 6788 }
2bb5d995
JM
6789 else if (TREE_CONSTANT (size))
6790 {
6791 /* `(int) &fn' is not a valid array bound. */
6792 if (name)
2d01edd7 6793 error ("size of array %qD is not an integral constant-expression",
0cbd7506 6794 name);
2bb5d995 6795 else
33bd39a2 6796 error ("size of array is not an integral constant-expression");
3aa2ddb8 6797 size = integer_one_node;
2bb5d995 6798 }
50d50fc7 6799 else if (pedantic && warn_vla != 0)
d63b5e9f
NS
6800 {
6801 if (name)
50d50fc7 6802 pedwarn ("ISO C++ forbids variable length array %qD", name);
d63b5e9f 6803 else
50d50fc7
SP
6804 pedwarn ("ISO C++ forbids variable length array");
6805 }
6806 else if (warn_vla > 0)
6807 {
6808 if (name)
6809 warning (OPT_Wvla,
6810 "variable length array %qD is used", name);
6811 else
6812 warning (OPT_Wvla,
6813 "variable length array is used");
d63b5e9f 6814 }
2848ad0f 6815
d63b5e9f
NS
6816 if (processing_template_decl && !TREE_CONSTANT (size))
6817 /* A variable sized array. */
6818 itype = build_min (MINUS_EXPR, sizetype, size, integer_one_node);
6819 else
2848ad0f 6820 {
455f19cb
MM
6821 HOST_WIDE_INT saved_processing_template_decl;
6822
d63b5e9f 6823 /* Compute the index of the largest element in the array. It is
0cbd7506
MS
6824 one less than the number of elements in the array. We save
6825 and restore PROCESSING_TEMPLATE_DECL so that computations in
6826 cp_build_binary_op will be appropriately folded. */
455f19cb
MM
6827 saved_processing_template_decl = processing_template_decl;
6828 processing_template_decl = 0;
6829 itype = cp_build_binary_op (MINUS_EXPR,
6830 cp_convert (ssizetype, size),
6831 cp_convert (ssizetype, integer_one_node));
6832 itype = fold (itype);
6833 processing_template_decl = saved_processing_template_decl;
6834
d63b5e9f 6835 if (!TREE_CONSTANT (itype))
f4f206f4 6836 /* A variable sized array. */
d63b5e9f
NS
6837 itype = variable_size (itype);
6838 /* Make sure that there was no overflow when creating to a signed
0cbd7506
MS
6839 index type. (For example, on a 32-bit machine, an array with
6840 size 2^32 - 1 is too big.) */
9116d529
RS
6841 else if (TREE_CODE (itype) == INTEGER_CST
6842 && TREE_OVERFLOW (itype))
2848ad0f 6843 {
d63b5e9f
NS
6844 error ("overflow in array dimension");
6845 TREE_OVERFLOW (itype) = 0;
2848ad0f 6846 }
2848ad0f 6847 }
68642fb6 6848
2848ad0f 6849 /* Create and return the appropriate index type. */
06d40de8
DG
6850 if (abi_1_itype)
6851 {
6852 tree t = build_index_type (itype);
6853 TYPE_CANONICAL (abi_1_itype) = TYPE_CANONICAL (t);
6854 return abi_1_itype;
6855 }
6856 else
6857 return build_index_type (itype);
2848ad0f
MM
6858}
6859
a723baf1
MM
6860/* Returns the scope (if any) in which the entity declared by
6861 DECLARATOR will be located. If the entity was declared with an
6862 unqualified name, NULL_TREE is returned. */
6863
6864tree
058b15c1 6865get_scope_of_declarator (const cp_declarator *declarator)
a723baf1 6866{
058b15c1
MM
6867 while (declarator && declarator->kind != cdk_id)
6868 declarator = declarator->declarator;
6869
6870 /* If the declarator-id is a SCOPE_REF, the scope in which the
6871 declaration occurs is the first operand. */
6872 if (declarator
1d786913
MM
6873 && declarator->u.id.qualifying_scope)
6874 return declarator->u.id.qualifying_scope;
058b15c1 6875
77880ae4 6876 /* Otherwise, the declarator is not a qualified name; the entity will
058b15c1
MM
6877 be declared in the current scope. */
6878 return NULL_TREE;
a723baf1
MM
6879}
6880
2848ad0f
MM
6881/* Returns an ARRAY_TYPE for an array with SIZE elements of the
6882 indicated TYPE. If non-NULL, NAME is the NAME of the declaration
6883 with this type. */
6884
6885static tree
11f6b451 6886create_array_type_for_decl (tree name, tree type, tree size)
2848ad0f
MM
6887{
6888 tree itype = NULL_TREE;
6889 const char* error_msg;
6890
6891 /* If things have already gone awry, bail now. */
6892 if (type == error_mark_node || size == error_mark_node)
6893 return error_mark_node;
6894
6895 /* Assume that everything will go OK. */
6896 error_msg = NULL;
6897
6898 /* There are some types which cannot be array elements. */
6899 switch (TREE_CODE (type))
6900 {
6901 case VOID_TYPE:
6902 error_msg = "array of void";
6903 break;
6904
6905 case FUNCTION_TYPE:
6906 error_msg = "array of functions";
6907 break;
6908
6909 case REFERENCE_TYPE:
6910 error_msg = "array of references";
6911 break;
6912
2848ad0f
MM
6913 case METHOD_TYPE:
6914 error_msg = "array of function members";
6915 break;
6916
6917 default:
6918 break;
6919 }
6920
6921 /* If something went wrong, issue an error-message and return. */
6922 if (error_msg)
6923 {
6924 if (name)
2d01edd7 6925 error ("declaration of %qD as %s", name, error_msg);
2848ad0f 6926 else
33bd39a2 6927 error ("creating %s", error_msg);
2848ad0f
MM
6928
6929 return error_mark_node;
6930 }
6931
6932 /* [dcl.array]
68642fb6 6933
2848ad0f
MM
6934 The constant expressions that specify the bounds of the arrays
6935 can be omitted only for the first member of the sequence. */
6936 if (TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type))
6937 {
b3faacfd 6938 if (name)
2d01edd7 6939 error ("declaration of %qD as multidimensional array must "
0cbd7506
MS
6940 "have bounds for all dimensions except the first",
6941 name);
b3faacfd 6942 else
2d01edd7 6943 error ("multidimensional array must have bounds for all "
0cbd7506 6944 "dimensions except the first");
2848ad0f
MM
6945
6946 return error_mark_node;
6947 }
6948
6949 /* Figure out the index type for the array. */
6950 if (size)
6951 itype = compute_array_index_type (name, size);
6952
7fb213d8
GB
6953 /* [dcl.array]
6954 T is called the array element type; this type shall not be [...] an
6955 abstract class type. */
6956 abstract_virtuals_error (name, type);
6957
2848ad0f
MM
6958 return build_cplus_array_type (type, itype);
6959}
6960
3dbc07b6
MM
6961/* Check that it's OK to declare a function with the indicated TYPE.
6962 SFK indicates the kind of special function (if any) that this
1f84ec23 6963 function is. OPTYPE is the type given in a conversion operator
44d10c10
PB
6964 declaration, or the class type for a constructor/destructor.
6965 Returns the actual return type of the function; that
3dbc07b6
MM
6966 may be different than TYPE if an error occurs, or for certain
6967 special functions. */
6968
6969static tree
11f6b451 6970check_special_function_return_type (special_function_kind sfk,
0cbd7506
MS
6971 tree type,
6972 tree optype)
3dbc07b6
MM
6973{
6974 switch (sfk)
6975 {
6976 case sfk_constructor:
6977 if (type)
33bd39a2 6978 error ("return type specification for constructor invalid");
5362b086 6979
44d10c10
PB
6980 if (targetm.cxx.cdtor_returns_this () && !TYPE_FOR_JAVA (optype))
6981 type = build_pointer_type (optype);
6982 else
6983 type = void_type_node;
3dbc07b6
MM
6984 break;
6985
6986 case sfk_destructor:
6987 if (type)
33bd39a2 6988 error ("return type specification for destructor invalid");
44d10c10 6989 /* We can't use the proper return type here because we run into
77880ae4 6990 problems with ambiguous bases and covariant returns.
44d10c10
PB
6991 Java classes are left unchanged because (void *) isn't a valid
6992 Java type, and we don't want to change the Java ABI. */
6993 if (targetm.cxx.cdtor_returns_this () && !TYPE_FOR_JAVA (optype))
6994 type = build_pointer_type (void_type_node);
6995 else
6996 type = void_type_node;
3dbc07b6
MM
6997 break;
6998
6999 case sfk_conversion:
7000 if (type && !same_type_p (type, optype))
2d01edd7 7001 error ("operator %qT declared to return %qT", optype, type);
3dbc07b6 7002 else if (type)
2d01edd7 7003 pedwarn ("return type specified for %<operator %T%>", optype);
3dbc07b6
MM
7004 type = optype;
7005 break;
7006
7007 default:
8dc2b103 7008 gcc_unreachable ();
3dbc07b6
MM
7009 }
7010
7011 return type;
7012}
7013
62e19030
MM
7014/* A variable or data member (whose unqualified name is IDENTIFIER)
7015 has been declared with the indicated TYPE. If the TYPE is not
7016 acceptable, issue an error message and return a type to use for
03fd3f84 7017 error-recovery purposes. */
62e19030
MM
7018
7019tree
7020check_var_type (tree identifier, tree type)
7021{
7022 if (VOID_TYPE_P (type))
7023 {
7024 if (!identifier)
7025 error ("unnamed variable or field declared void");
7026 else if (TREE_CODE (identifier) == IDENTIFIER_NODE)
7027 {
7028 gcc_assert (!IDENTIFIER_OPNAME_P (identifier));
7029 error ("variable or field %qE declared void", identifier);
7030 }
7031 else
7032 error ("variable or field declared void");
650fcd07 7033 type = error_mark_node;
62e19030 7034 }
c8094d83 7035
62e19030
MM
7036 return type;
7037}
7038
a723baf1
MM
7039/* Given declspecs and a declarator (abstract or otherwise), determine
7040 the name and type of the object declared and construct a DECL node
7041 for it.
8d08fdba 7042
5e32a5cf
GDR
7043 DECLSPECS points to the representation of declaration-specifier
7044 sequence that precedes declarator.
8d08fdba
MS
7045
7046 DECL_CONTEXT says which syntactic context this declaration is in:
7047 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
7048 FUNCDEF for a function definition. Like NORMAL but a few different
7049 error messages in each case. Return value may be zero meaning
7050 this definition is too screwy to try to parse.
7051 MEMFUNCDEF for a function definition. Like FUNCDEF but prepares to
7052 handle member functions (which have FIELD context).
7053 Return value may be zero meaning this definition is too screwy to
7054 try to parse.
7055 PARM for a parameter declaration (either within a function prototype
7056 or before a function body). Make a PARM_DECL, or return void_type_node.
db5ae43f 7057 CATCHPARM for a parameter declaration before a catch clause.
8d08fdba
MS
7058 TYPENAME if for a typename (in a cast or sizeof).
7059 Don't make a DECL node; just return the ..._TYPE node.
7060 FIELD for a struct or union field; make a FIELD_DECL.
7061 BITFIELD for a field with specified width.
7062 INITIALIZED is 1 if the decl has an initializer.
7063
91d231cb
JM
7064 ATTRLIST is a pointer to the list of attributes, which may be NULL
7065 if there are none; *ATTRLIST may be modified if attributes from inside
7066 the declarator should be applied to the declaration.
b17e2870 7067
a723baf1
MM
7068 When this function is called, scoping variables (such as
7069 CURRENT_CLASS_TYPE) should reflect the scope in which the
7070 declaration occurs, not the scope in which the new declaration will
7071 be placed. For example, on:
8d08fdba 7072
a723baf1 7073 void S::f() { ... }
8d08fdba 7074
a723baf1 7075 when grokdeclarator is called for `S::f', the CURRENT_CLASS_TYPE
344f237b
LM
7076 should not be `S'.
7077
7078 Returns a DECL (if a declarator is present), a TYPE (if there is no
7079 declarator, in cases like "struct S;"), or the ERROR_MARK_NODE if an
7080 error occurs. */
8d08fdba 7081
8d08fdba 7082tree
058b15c1 7083grokdeclarator (const cp_declarator *declarator,
1ff3c076 7084 const cp_decl_specifier_seq *declspecs,
0cbd7506
MS
7085 enum decl_context decl_context,
7086 int initialized,
7087 tree* attrlist)
8d08fdba 7088{
8d08fdba
MS
7089 tree type = NULL_TREE;
7090 int longlong = 0;
db5ae43f 7091 int virtualp, explicitp, friendp, inlinep, staticp;
8d08fdba
MS
7092 int explicit_int = 0;
7093 int explicit_char = 0;
37c46b43 7094 int defaulted_int = 0;
d1c78882 7095 tree dependent_name = NULL_TREE;
caf93cb0 7096
8d08fdba 7097 tree typedef_decl = NULL_TREE;
058b15c1 7098 const char *name = NULL;
8d08fdba 7099 tree typedef_type = NULL_TREE;
357d956e
MM
7100 /* True if this declarator is a function definition. */
7101 bool funcdef_flag = false;
058b15c1 7102 cp_declarator_kind innermost_code = cdk_error;
8d08fdba 7103 int bitfield = 0;
6125f3be
DE
7104#if 0
7105 /* See the code below that used this. */
91d231cb 7106 tree decl_attr = NULL_TREE;
6125f3be 7107#endif
8d08fdba
MS
7108
7109 /* Keep track of what sort of function is being processed
7110 so that we can warn about default return values, or explicit
7111 return values which do not match prescribed defaults. */
3dbc07b6 7112 special_function_kind sfk = sfk_none;
8d08fdba
MS
7113
7114 tree dname = NULL_TREE;
8d08fdba
MS
7115 tree ctor_return_type = NULL_TREE;
7116 enum overload_flags flags = NO_SPECIAL;
e2537f2c
MM
7117 /* cv-qualifiers that apply to the declarator, for a declaration of
7118 a member function. */
7119 cp_cv_quals memfn_quals = TYPE_UNQUALIFIED;
7120 /* cv-qualifiers that apply to the type specified by the DECLSPECS. */
7121 int type_quals;
c11b6f21 7122 tree raises = NULL_TREE;
386b8a85 7123 int template_count = 0;
91d231cb 7124 tree returned_attrs = NULL_TREE;
4546865e 7125 tree parms = NULL_TREE;
058b15c1
MM
7126 const cp_declarator *id_declarator;
7127 /* The unqualified name of the declarator; either an
7128 IDENTIFIER_NODE, BIT_NOT_EXPR, or TEMPLATE_ID_EXPR. */
7129 tree unqualified_id;
7130 /* The class type, if any, in which this entity is located,
7131 or NULL_TREE if none. Note that this value may be different from
7132 the current class type; for example if an attempt is made to declare
7133 "A::f" inside "B", this value will be "A". */
7134 tree ctype = current_class_type;
7135 /* The NAMESPACE_DECL for the namespace in which this entity is
7136 located. If an unqualified name is used to declare the entity,
7137 this value will be NULL_TREE, even if the entity is located at
caf93cb0 7138 namespace scope. */
058b15c1 7139 tree in_namespace = NULL_TREE;
1ff3c076
MM
7140 cp_storage_class storage_class;
7141 bool unsigned_p, signed_p, short_p, long_p, thread_p;
2cfe82fe 7142 bool type_was_error_mark_node = false;
5d80a306 7143 bool parameter_pack_p = declarator? declarator->parameter_pack_p : false;
1891dec4 7144 bool set_no_warning = false;
1ff3c076
MM
7145
7146 signed_p = declspecs->specs[(int)ds_signed];
7147 unsigned_p = declspecs->specs[(int)ds_unsigned];
7148 short_p = declspecs->specs[(int)ds_short];
7149 long_p = declspecs->specs[(int)ds_long];
28c84d63 7150 longlong = declspecs->specs[(int)ds_long] >= 2;
1ff3c076 7151 thread_p = declspecs->specs[(int)ds_thread];
8d08fdba 7152
8d08fdba 7153 if (decl_context == FUNCDEF)
357d956e 7154 funcdef_flag = true, decl_context = NORMAL;
8d08fdba 7155 else if (decl_context == MEMFUNCDEF)
357d956e 7156 funcdef_flag = true, decl_context = FIELD;
8d08fdba
MS
7157 else if (decl_context == BITFIELD)
7158 bitfield = 1, decl_context = FIELD;
7159
8d08fdba
MS
7160 /* Look inside a declarator for the name being declared
7161 and get it as a string, for an error message. */
caf93cb0
EC
7162 for (id_declarator = declarator;
7163 id_declarator;
058b15c1
MM
7164 id_declarator = id_declarator->declarator)
7165 {
7166 if (id_declarator->kind != cdk_id)
7167 innermost_code = id_declarator->kind;
8d08fdba 7168
058b15c1
MM
7169 switch (id_declarator->kind)
7170 {
7171 case cdk_function:
7172 if (id_declarator->declarator
7173 && id_declarator->declarator->kind == cdk_id)
8d08fdba 7174 {
058b15c1
MM
7175 sfk = id_declarator->declarator->u.id.sfk;
7176 if (sfk == sfk_destructor)
7177 flags = DTOR_FLAG;
51c184be 7178 }
058b15c1 7179 break;
be99da77 7180
058b15c1
MM
7181 case cdk_id:
7182 {
1d786913
MM
7183 tree qualifying_scope = id_declarator->u.id.qualifying_scope;
7184 tree decl = id_declarator->u.id.unqualified_name;
058b15c1
MM
7185 if (!decl)
7186 break;
1d786913 7187 if (qualifying_scope)
8d08fdba 7188 {
2884e22c
MM
7189 if (at_function_scope_p ())
7190 {
7191 /* [dcl.meaning]
7192
7193 A declarator-id shall not be qualified except
7194 for ...
7195
7196 None of the cases are permitted in block
7197 scope. */
7198 if (qualifying_scope == global_namespace)
7199 error ("invalid use of qualified-name %<::%D%>",
7200 decl);
7201 else if (TYPE_P (qualifying_scope))
7202 error ("invalid use of qualified-name %<%T::%D%>",
7203 qualifying_scope, decl);
7204 else
7205 error ("invalid use of qualified-name %<%D::%D%>",
7206 qualifying_scope, decl);
7207 return error_mark_node;
7208 }
7209 else if (TYPE_P (qualifying_scope))
058b15c1 7210 {
1d786913 7211 ctype = qualifying_scope;
058b15c1
MM
7212 if (innermost_code != cdk_function
7213 && current_class_type
caf93cb0 7214 && !UNIQUELY_DERIVED_FROM_P (ctype,
058b15c1
MM
7215 current_class_type))
7216 {
2d01edd7 7217 error ("type %qT is not derived from type %qT",
058b15c1 7218 ctype, current_class_type);
1d786913 7219 return error_mark_node;
058b15c1 7220 }
058b15c1
MM
7221 }
7222 else if (TREE_CODE (qualifying_scope) == NAMESPACE_DECL)
7223 in_namespace = qualifying_scope;
058b15c1 7224 }
058b15c1
MM
7225 switch (TREE_CODE (decl))
7226 {
7227 case BIT_NOT_EXPR:
be99da77 7228 {
88e95ee3
MM
7229 tree type;
7230
7231 if (innermost_code != cdk_function)
7232 {
7233 error ("declaration of %qD as non-function", decl);
7234 return error_mark_node;
7235 }
c8094d83 7236 else if (!qualifying_scope
88e95ee3
MM
7237 && !(current_class_type && at_class_scope_p ()))
7238 {
7239 error ("declaration of %qD as non-member", decl);
7240 return error_mark_node;
7241 }
c8094d83 7242
88e95ee3
MM
7243 type = TREE_OPERAND (decl, 0);
7244 name = IDENTIFIER_POINTER (constructor_name (type));
af02935e 7245 dname = decl;
058b15c1
MM
7246 }
7247 break;
633221db 7248
058b15c1
MM
7249 case TEMPLATE_ID_EXPR:
7250 {
7251 tree fns = TREE_OPERAND (decl, 0);
633221db 7252
058b15c1 7253 dname = fns;
058b15c1 7254 if (TREE_CODE (dname) != IDENTIFIER_NODE)
be99da77 7255 {
50bc768d 7256 gcc_assert (is_overloaded_fn (dname));
058b15c1 7257 dname = DECL_NAME (get_first_fn (dname));
be99da77
MS
7258 }
7259 }
058b15c1 7260 /* Fall through. */
be99da77 7261
058b15c1
MM
7262 case IDENTIFIER_NODE:
7263 if (TREE_CODE (decl) == IDENTIFIER_NODE)
7264 dname = decl;
be99da77 7265
058b15c1
MM
7266 if (C_IS_RESERVED_WORD (dname))
7267 {
2d01edd7 7268 error ("declarator-id missing; using reserved word %qD",
058b15c1
MM
7269 dname);
7270 name = IDENTIFIER_POINTER (dname);
7271 }
7272 else if (!IDENTIFIER_TYPENAME_P (dname))
7273 name = IDENTIFIER_POINTER (dname);
7274 else
7275 {
50bc768d 7276 gcc_assert (flags == NO_SPECIAL);
058b15c1
MM
7277 flags = TYPENAME_FLAG;
7278 ctor_return_type = TREE_TYPE (dname);
7279 sfk = sfk_conversion;
7280 if (is_typename_at_global_scope (dname))
7281 name = IDENTIFIER_POINTER (dname);
7282 else
7283 name = "<invalid operator>";
7284 }
7285 break;
7286
058b15c1 7287 default:
8dc2b103 7288 gcc_unreachable ();
058b15c1 7289 }
45537677 7290 break;
b08f991d 7291 }
45537677 7292
b08f991d
SB
7293 case cdk_array:
7294 case cdk_pointer:
7295 case cdk_reference:
7296 case cdk_ptrmem:
7297 break;
2ee366b5 7298
b08f991d
SB
7299 case cdk_error:
7300 return error_mark_node;
058b15c1 7301
b08f991d
SB
7302 default:
7303 gcc_unreachable ();
058b15c1
MM
7304 }
7305 if (id_declarator->kind == cdk_id)
7306 break;
7307 }
8d08fdba 7308
fa6098f8 7309 /* [dcl.fct.edf]
3db45ab5 7310
fa6098f8
MM
7311 The declarator in a function-definition shall have the form
7312 D1 ( parameter-declaration-clause) ... */
058b15c1 7313 if (funcdef_flag && innermost_code != cdk_function)
fa6098f8
MM
7314 {
7315 error ("function definition does not declare parameters");
7316 return error_mark_node;
7317 }
8d08fdba 7318
e1cd6e56 7319 if (((dname && IDENTIFIER_OPNAME_P (dname)) || flags == TYPENAME_FLAG)
058b15c1 7320 && innermost_code != cdk_function
62d1db17 7321 && ! (ctype && !declspecs->any_specifiers_p))
e1cd6e56 7322 {
2d01edd7 7323 error ("declaration of %qD as non-function", dname);
943e3ede 7324 return error_mark_node;
e1cd6e56
MS
7325 }
7326
8d08fdba
MS
7327 /* Anything declared one level down from the top level
7328 must be one of the parameters of a function
7329 (because the body is at least two levels down). */
7330
7331 /* This heuristic cannot be applied to C++ nodes! Fixed, however,
7332 by not allowing C++ class definitions to specify their parameters
7333 with xdecls (must be spec.d in the parmlist).
7334
7335 Since we now wait to push a class scope until we are sure that
7336 we are in a legitimate method context, we must set oldcname
a9aedbc2
MS
7337 explicitly (since current_class_name is not yet alive).
7338
7339 We also want to avoid calling this a PARM if it is in a namespace. */
8d08fdba 7340
9188c363 7341 if (decl_context == NORMAL && !toplevel_bindings_p ())
a9aedbc2 7342 {
e2500fed 7343 struct cp_binding_level *b = current_binding_level;
ff955512 7344 current_binding_level = b->level_chain;
a9aedbc2
MS
7345 if (current_binding_level != 0 && toplevel_bindings_p ())
7346 decl_context = PARM;
ff955512 7347 current_binding_level = b;
a9aedbc2 7348 }
8d08fdba 7349
34fc957d
NS
7350 if (name == NULL)
7351 name = decl_context == PARM ? "parameter" : "type name";
68642fb6 7352
62d1db17
MM
7353 /* If there were multiple types specified in the decl-specifier-seq,
7354 issue an error message. */
7355 if (declspecs->multiple_types_p)
32273f9f
LM
7356 {
7357 error ("two or more data types in declaration of %qs", name);
7358 return error_mark_node;
7359 }
7360
62d1db17
MM
7361 /* Extract the basic type from the decl-specifier-seq. */
7362 type = declspecs->type;
7363 if (type == error_mark_node)
2cfe82fe
ZW
7364 {
7365 type = NULL_TREE;
7366 type_was_error_mark_node = true;
7367 }
62d1db17
MM
7368 /* If the entire declaration is itself tagged as deprecated then
7369 suppress reports of deprecated items. */
7370 if (type && TREE_DEPRECATED (type)
7371 && deprecated_state != DEPRECATED_SUPPRESS)
7372 warn_deprecated_use (type);
7373 if (type && TREE_CODE (type) == TYPE_DECL)
8d08fdba 7374 {
62d1db17
MM
7375 typedef_decl = type;
7376 type = TREE_TYPE (typedef_decl);
7377 }
7378 /* No type at all: default to `int', and set DEFAULTED_INT
7379 because it was not a user-defined typedef. */
1ff3c076 7380 if (type == NULL_TREE && (signed_p || unsigned_p || long_p || short_p))
62d1db17
MM
7381 {
7382 /* These imply 'int'. */
7383 type = integer_type_node;
7384 defaulted_int = 1;
7385 }
7386 /* Gather flags. */
7387 explicit_int = declspecs->explicit_int_p;
7388 explicit_char = declspecs->explicit_char_p;
7a1f3f5f 7389
8d6e459d
NS
7390#if 0
7391 /* See the code below that used this. */
7392 if (typedef_decl)
7393 decl_attr = DECL_ATTRIBUTES (typedef_decl);
7394#endif
8d08fdba
MS
7395 typedef_type = type;
7396
a3203465 7397
44d10c10
PB
7398 if (sfk != sfk_conversion)
7399 ctor_return_type = ctype;
7400
3dbc07b6
MM
7401 if (sfk != sfk_none)
7402 type = check_special_function_return_type (sfk, type,
3dbc07b6
MM
7403 ctor_return_type);
7404 else if (type == NULL_TREE)
8d08fdba 7405 {
3dbc07b6
MM
7406 int is_main;
7407
8d08fdba 7408 explicit_int = -1;
3dbc07b6 7409
0fd0b7ee
JM
7410 /* We handle `main' specially here, because 'main () { }' is so
7411 common. With no options, it is allowed. With -Wreturn-type,
7412 it is a warning. It is only an error with -pedantic-errors. */
3dbc07b6 7413 is_main = (funcdef_flag
7437519c 7414 && dname && MAIN_NAME_P (dname)
3dbc07b6
MM
7415 && ctype == NULL_TREE
7416 && in_namespace == NULL_TREE
7417 && current_namespace == global_namespace);
7418
2cfe82fe
ZW
7419 if (type_was_error_mark_node)
7420 /* We've already issued an error, don't complain more. */;
7421 else if (in_system_header || flag_ms_extensions)
3dbc07b6
MM
7422 /* Allow it, sigh. */;
7423 else if (pedantic || ! is_main)
2d01edd7 7424 pedwarn ("ISO C++ forbids declaration of %qs with no type", name);
b10fc6f5
GDR
7425 else
7426 warning (OPT_Wreturn_type,
7427 "ISO C++ forbids declaration of %qs with no type", name);
41eff652 7428
3dbc07b6 7429 type = integer_type_node;
51c184be 7430 }
caf93cb0 7431
8d08fdba
MS
7432 ctype = NULL_TREE;
7433
7434 /* Now process the modifiers that were specified
7435 and check for invalid combinations. */
7436
7437 /* Long double is a special combination. */
3cc189f5 7438 if (long_p && !longlong && TYPE_MAIN_VARIANT (type) == double_type_node)
8d08fdba 7439 {
1ff3c076 7440 long_p = false;
68642fb6 7441 type = build_qualified_type (long_double_type_node,
89d684bb 7442 cp_type_quals (type));
8d08fdba
MS
7443 }
7444
7445 /* Check all other uses of type modifiers. */
7446
1ff3c076 7447 if (unsigned_p || signed_p || long_p || short_p)
8d08fdba
MS
7448 {
7449 int ok = 0;
7450
3cc189f5
VR
7451 if ((signed_p || unsigned_p) && TREE_CODE (type) != INTEGER_TYPE)
7452 error ("%<signed%> or %<unsigned%> invalid for %qs", name);
1ff3c076 7453 else if (signed_p && unsigned_p)
3cc189f5
VR
7454 error ("%<signed%> and %<unsigned%> specified together for %qs", name);
7455 else if (longlong && TREE_CODE (type) != INTEGER_TYPE)
7456 error ("%<long long%> invalid for %qs", name);
7457 else if (long_p && TREE_CODE (type) == REAL_TYPE)
7458 error ("%<long%> invalid for %qs", name);
7459 else if (short_p && TREE_CODE (type) == REAL_TYPE)
7460 error ("%<short%> invalid for %qs", name);
5d48268f
VR
7461 else if ((long_p || short_p) && TREE_CODE (type) != INTEGER_TYPE)
7462 error ("%<long%> or %<short%> invalid for %qs", name);
3cc189f5
VR
7463 else if ((long_p || short_p) && explicit_char)
7464 error ("%<long%> or %<short%> specified with char for %qs", name);
7465 else if (long_p && short_p)
7466 error ("%<long%> and %<short%> specified together for %qs", name);
8d08fdba
MS
7467 else
7468 {
7469 ok = 1;
37c46b43 7470 if (!explicit_int && !defaulted_int && !explicit_char && pedantic)
8d08fdba 7471 {
2d01edd7 7472 pedwarn ("long, short, signed or unsigned used invalidly for %qs",
8d08fdba
MS
7473 name);
7474 if (flag_pedantic_errors)
7475 ok = 0;
7476 }
7477 }
7478
7479 /* Discard the type modifiers if they are invalid. */
7480 if (! ok)
7481 {
1ff3c076
MM
7482 unsigned_p = false;
7483 signed_p = false;
7484 long_p = false;
7485 short_p = false;
8d08fdba
MS
7486 longlong = 0;
7487 }
7488 }
7489
7490 /* Decide whether an integer type is signed or not.
7491 Optionally treat bitfields as signed by default. */
1ff3c076 7492 if (unsigned_p
b89c5a7b
MM
7493 /* [class.bit]
7494
7495 It is implementation-defined whether a plain (neither
7496 explicitly signed or unsigned) char, short, int, or long
7497 bit-field is signed or unsigned.
68642fb6 7498
b89c5a7b
MM
7499 Naturally, we extend this to long long as well. Note that
7500 this does not include wchar_t. */
7501 || (bitfield && !flag_signed_bitfields
1ff3c076 7502 && !signed_p
b89c5a7b
MM
7503 /* A typedef for plain `int' without `signed' can be
7504 controlled just like plain `int', but a typedef for
7505 `signed int' cannot be so controlled. */
68642fb6 7506 && !(typedef_decl
29bbeb1c 7507 && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl))
b38a05d0 7508 && TREE_CODE (type) == INTEGER_TYPE
29bbeb1c 7509 && !same_type_p (TYPE_MAIN_VARIANT (type), wchar_type_node)))
8d08fdba
MS
7510 {
7511 if (longlong)
7512 type = long_long_unsigned_type_node;
1ff3c076 7513 else if (long_p)
8d08fdba 7514 type = long_unsigned_type_node;
1ff3c076 7515 else if (short_p)
8d08fdba
MS
7516 type = short_unsigned_type_node;
7517 else if (type == char_type_node)
7518 type = unsigned_char_type_node;
7519 else if (typedef_decl)
ca5ba2a3 7520 type = unsigned_type_for (type);
8d08fdba
MS
7521 else
7522 type = unsigned_type_node;
7523 }
1ff3c076 7524 else if (signed_p && type == char_type_node)
8d08fdba
MS
7525 type = signed_char_type_node;
7526 else if (longlong)
7527 type = long_long_integer_type_node;
1ff3c076 7528 else if (long_p)
8d08fdba 7529 type = long_integer_type_node;
1ff3c076 7530 else if (short_p)
8d08fdba
MS
7531 type = short_integer_type_node;
7532
62d1db17 7533 if (declspecs->specs[(int)ds_complex])
37c46b43 7534 {
1ff3c076 7535 if (TREE_CODE (type) != INTEGER_TYPE && TREE_CODE (type) != REAL_TYPE)
2d01edd7 7536 error ("complex invalid for %qs", name);
37c46b43
MS
7537 /* If we just have "complex", it is equivalent to
7538 "complex double", but if any modifiers at all are specified it is
7539 the complex form of TYPE. E.g, "complex short" is
7540 "complex short int". */
7541
1ff3c076
MM
7542 else if (defaulted_int && ! longlong
7543 && ! (long_p || short_p || signed_p || unsigned_p))
37c46b43
MS
7544 type = complex_double_type_node;
7545 else if (type == integer_type_node)
7546 type = complex_integer_type_node;
7547 else if (type == float_type_node)
7548 type = complex_float_type_node;
7549 else if (type == double_type_node)
7550 type = complex_double_type_node;
7551 else if (type == long_double_type_node)
7552 type = complex_long_double_type_node;
7553 else
7554 type = build_complex_type (type);
7555 }
7556
4f2b0fb2 7557 type_quals = TYPE_UNQUALIFIED;
62d1db17 7558 if (declspecs->specs[(int)ds_const])
4f2b0fb2 7559 type_quals |= TYPE_QUAL_CONST;
62d1db17 7560 if (declspecs->specs[(int)ds_volatile])
4f2b0fb2 7561 type_quals |= TYPE_QUAL_VOLATILE;
62d1db17 7562 if (declspecs->specs[(int)ds_restrict])
4f2b0fb2
NS
7563 type_quals |= TYPE_QUAL_RESTRICT;
7564 if (sfk == sfk_conversion && type_quals != TYPE_UNQUALIFIED)
2d01edd7 7565 error ("qualifiers are not allowed on declaration of %<operator %T%>",
0cbd7506 7566 ctor_return_type);
3ac3d9ea 7567
c8094d83 7568 if (TREE_CODE (type) == FUNCTION_TYPE
9804209d
DG
7569 && type_quals != TYPE_UNQUALIFIED)
7570 {
7571 /* This was an error in C++98 (cv-qualifiers cannot be added to
0cbd7506
MS
7572 a function type), but DR 295 makes the code well-formed by
7573 dropping the extra qualifiers. */
9804209d 7574 if (pedantic)
0cbd7506
MS
7575 {
7576 tree bad_type = build_qualified_type (type, type_quals);
7577 pedwarn ("ignoring %qV qualifiers added to function type %qT",
7578 bad_type, type);
7579 }
9804209d
DG
7580 type_quals = TYPE_UNQUALIFIED;
7581 }
4f2b0fb2
NS
7582 type_quals |= cp_type_quals (type);
7583 type = cp_build_qualified_type_real
7584 (type, type_quals, ((typedef_decl && !DECL_ARTIFICIAL (typedef_decl)
23fca1f5 7585 ? tf_ignore_bad_quals : 0) | tf_warning_or_error));
4f2b0fb2
NS
7586 /* We might have ignored or rejected some of the qualifiers. */
7587 type_quals = cp_type_quals (type);
caf93cb0 7588
8d08fdba 7589 staticp = 0;
62d1db17
MM
7590 inlinep = !! declspecs->specs[(int)ds_inline];
7591 virtualp = !! declspecs->specs[(int)ds_virtual];
7592 explicitp = !! declspecs->specs[(int)ds_explicit];
8d08fdba 7593
1ff3c076
MM
7594 storage_class = declspecs->storage_class;
7595 if (storage_class == sc_static)
8d08fdba
MS
7596 staticp = 1 + (decl_context == FIELD);
7597
7598 if (virtualp && staticp == 2)
7599 {
2d01edd7 7600 error ("member %qD cannot be declared both virtual and static", dname);
7e1e0765 7601 storage_class = sc_none;
8d08fdba
MS
7602 staticp = 0;
7603 }
62d1db17 7604 friendp = !! declspecs->specs[(int)ds_friend];
8d08fdba 7605
d1c78882 7606 if (dependent_name && !friendp)
d5614afb 7607 {
d1c78882 7608 error ("%<%T::%D%> is not a valid declarator", ctype, dependent_name);
13dbe691 7609 return error_mark_node;
d5614afb 7610 }
8d08fdba 7611
62d1db17
MM
7612 /* Issue errors about use of storage classes for parameters. */
7613 if (decl_context == PARM)
8d08fdba 7614 {
62d1db17 7615 if (declspecs->specs[(int)ds_typedef])
709f9dcf
VR
7616 {
7617 error ("typedef declaration invalid in parameter declaration");
7618 return error_mark_node;
7619 }
1ff3c076
MM
7620 else if (storage_class == sc_static
7621 || storage_class == sc_extern
7622 || thread_p)
8251199e 7623 error ("storage class specifiers invalid in parameter declarations");
8d08fdba
MS
7624 }
7625
7626 /* Give error if `virtual' is used outside of class declaration. */
b7484fbe
MS
7627 if (virtualp
7628 && (current_class_name == NULL_TREE || decl_context != FIELD))
8d08fdba 7629 {
8251199e 7630 error ("virtual outside class declaration");
8d08fdba
MS
7631 virtualp = 0;
7632 }
8d08fdba
MS
7633
7634 /* Static anonymous unions are dealt with here. */
7635 if (staticp && decl_context == TYPENAME
62d1db17
MM
7636 && declspecs->type
7637 && ANON_AGGR_TYPE_P (declspecs->type))
8d08fdba
MS
7638 decl_context = FIELD;
7639
8d08fdba
MS
7640 /* Warn about storage classes that are invalid for certain
7641 kinds of declarations (parameters, typenames, etc.). */
3e66d096
JJ
7642 if (thread_p
7643 && ((storage_class
7644 && storage_class != sc_extern
7645 && storage_class != sc_static)
7646 || declspecs->specs[(int)ds_typedef]))
7647 {
7648 error ("multiple storage classes in declaration of %qs", name);
7649 thread_p = false;
7650 }
26106542 7651 if (declspecs->conflicting_specifiers_p)
a23e08d9 7652 {
26106542 7653 error ("conflicting specifiers in declaration of %qs", name);
a23e08d9
VR
7654 storage_class = sc_none;
7655 }
caf93cb0 7656 else if (decl_context != NORMAL
1ff3c076
MM
7657 && ((storage_class != sc_none
7658 && storage_class != sc_mutable)
7659 || thread_p))
8d08fdba 7660 {
db5ae43f 7661 if ((decl_context == PARM || decl_context == CATCHPARM)
1ff3c076
MM
7662 && (storage_class == sc_register
7663 || storage_class == sc_auto))
8d08fdba 7664 ;
62d1db17 7665 else if (declspecs->specs[(int)ds_typedef])
fc378698 7666 ;
8d08fdba 7667 else if (decl_context == FIELD
f4f206f4 7668 /* C++ allows static class elements. */
1ff3c076 7669 && storage_class == sc_static)
d363e7bf
AJ
7670 /* C++ also allows inlines and signed and unsigned elements,
7671 but in those cases we don't come in here. */
8d08fdba
MS
7672 ;
7673 else
7674 {
7675 if (decl_context == FIELD)
389c6c8b 7676 error ("storage class specified for %qs", name);
8d08fdba 7677 else
b9d12519
KG
7678 {
7679 if (decl_context == PARM || decl_context == CATCHPARM)
2d01edd7 7680 error ("storage class specified for parameter %qs", name);
b9d12519
KG
7681 else
7682 error ("storage class specified for typename");
7683 }
1ff3c076
MM
7684 if (storage_class == sc_register
7685 || storage_class == sc_auto
7686 || storage_class == sc_extern
7687 || thread_p)
7688 storage_class = sc_none;
8d08fdba
MS
7689 }
7690 }
caf93cb0 7691 else if (storage_class == sc_extern && initialized
62d1db17 7692 && !funcdef_flag)
8d08fdba 7693 {
a9aedbc2 7694 if (toplevel_bindings_p ())
8d08fdba 7695 {
59be0cdd 7696 /* It's common practice (and completely valid) to have a const
8d08fdba 7697 be initialized and declared extern. */
950ad3c3 7698 if (!(type_quals & TYPE_QUAL_CONST))
d4ee4d25 7699 warning (0, "%qs initialized and declared %<extern%>", name);
8d08fdba
MS
7700 }
7701 else
2d01edd7 7702 error ("%qs has both %<extern%> and initializer", name);
8d08fdba 7703 }
1ff3c076 7704 else if (storage_class == sc_extern && funcdef_flag
a9aedbc2 7705 && ! toplevel_bindings_p ())
2d01edd7 7706 error ("nested function %qs declared %<extern%>", name);
a9aedbc2 7707 else if (toplevel_bindings_p ())
8d08fdba 7708 {
1ff3c076 7709 if (storage_class == sc_auto)
2d01edd7 7710 error ("top-level declaration of %qs specifies %<auto%>", name);
8d08fdba 7711 }
1ff3c076
MM
7712 else if (thread_p
7713 && storage_class != sc_extern
7714 && storage_class != sc_static)
7a1f3f5f 7715 {
2d01edd7 7716 error ("function-scope %qs implicitly auto and declared %<__thread%>",
7a1f3f5f 7717 name);
1ff3c076 7718 thread_p = false;
7a1f3f5f 7719 }
8d08fdba 7720
1ff3c076 7721 if (storage_class && friendp)
8251199e 7722 error ("storage class specifiers invalid in friend function declarations");
909e536a 7723
058b15c1
MM
7724 if (!id_declarator)
7725 unqualified_id = NULL_TREE;
7726 else
8d08fdba 7727 {
1d786913 7728 unqualified_id = id_declarator->u.id.unqualified_name;
058b15c1 7729 switch (TREE_CODE (unqualified_id))
8d08fdba 7730 {
058b15c1 7731 case BIT_NOT_EXPR:
caf93cb0 7732 unqualified_id
058b15c1
MM
7733 = constructor_name (TREE_OPERAND (unqualified_id, 0));
7734 break;
caf93cb0 7735
058b15c1
MM
7736 case IDENTIFIER_NODE:
7737 case TEMPLATE_ID_EXPR:
52fbc847 7738 break;
caf93cb0 7739
058b15c1 7740 default:
8dc2b103 7741 gcc_unreachable ();
058b15c1
MM
7742 }
7743 }
52fbc847 7744
058b15c1
MM
7745 /* Determine the type of the entity declared by recurring on the
7746 declarator. */
f4f18103 7747 for (; declarator; declarator = declarator->declarator)
058b15c1
MM
7748 {
7749 const cp_declarator *inner_declarator;
7750 tree attrs;
8d08fdba 7751
058b15c1
MM
7752 if (type == error_mark_node)
7753 return error_mark_node;
c1b177ec 7754
058b15c1
MM
7755 attrs = declarator->attributes;
7756 if (attrs)
7757 {
7758 int attr_flags;
caf93cb0 7759
058b15c1
MM
7760 attr_flags = 0;
7761 if (declarator == NULL || declarator->kind == cdk_id)
7762 attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
7763 if (declarator->kind == cdk_function)
7764 attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
7765 if (declarator->kind == cdk_array)
7766 attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
7767 returned_attrs = decl_attributes (&type,
7768 chainon (returned_attrs, attrs),
7769 attr_flags);
7770 }
7771
f4f18103
MM
7772 if (declarator->kind == cdk_id)
7773 break;
7774
7775 inner_declarator = declarator->declarator;
7776
058b15c1
MM
7777 switch (declarator->kind)
7778 {
7779 case cdk_array:
caf93cb0 7780 type = create_array_type_for_decl (dname, type,
058b15c1 7781 declarator->u.array.bounds);
8d08fdba
MS
7782 break;
7783
058b15c1 7784 case cdk_function:
8d08fdba
MS
7785 {
7786 tree arg_types;
f376e137 7787 int funcdecl_p;
8d08fdba
MS
7788
7789 /* Declaring a function type.
7790 Make sure we have a valid type for the function to return. */
8d08fdba 7791
1891dec4
DM
7792 if (type_quals != TYPE_UNQUALIFIED)
7793 {
7794 if (SCALAR_TYPE_P (type) || VOID_TYPE_P (type))
7795 warning (OPT_Wreturn_type,
7796 "type qualifiers ignored on function return type");
7797 /* We now know that the TYPE_QUALS don't apply to the
7798 decl, but to its return type. */
7799 type_quals = TYPE_UNQUALIFIED;
7800 set_no_warning = true;
7801 }
8d08fdba
MS
7802
7803 /* Warn about some types functions can't return. */
7804
7805 if (TREE_CODE (type) == FUNCTION_TYPE)
7806 {
2d01edd7 7807 error ("%qs declared as function returning a function", name);
8d08fdba
MS
7808 type = integer_type_node;
7809 }
7810 if (TREE_CODE (type) == ARRAY_TYPE)
7811 {
2d01edd7 7812 error ("%qs declared as function returning an array", name);
8d08fdba
MS
7813 type = integer_type_node;
7814 }
7815
b7484fbe 7816 /* Pick up type qualifiers which should be applied to `this'. */
e2537f2c 7817 memfn_quals = declarator->u.function.qualifiers;
b7484fbe 7818
c11b6f21 7819 /* Pick up the exception specifications. */
058b15c1 7820 raises = declarator->u.function.exception_specification;
c11b6f21 7821
f376e137
MS
7822 /* Say it's a definition only for the CALL_EXPR
7823 closest to the identifier. */
058b15c1 7824 funcdecl_p = inner_declarator && inner_declarator->kind == cdk_id;
68642fb6 7825
8d08fdba
MS
7826 if (ctype == NULL_TREE
7827 && decl_context == FIELD
f376e137 7828 && funcdecl_p
8d08fdba
MS
7829 && (friendp == 0 || dname == current_class_name))
7830 ctype = current_class_type;
7831
058b15c1
MM
7832 if (ctype && (sfk == sfk_constructor
7833 || sfk == sfk_destructor))
8d08fdba
MS
7834 {
7835 /* We are within a class's scope. If our declarator name
7836 is the same as the class name, and we are defining
7837 a function, then it is a constructor/destructor, and
7838 therefore returns a void type. */
3db45ab5 7839
e2537f2c
MM
7840 /* ISO C++ 12.4/2. A destructor may not be declared
7841 const or volatile. A destructor may not be
7842 static.
3db45ab5 7843
e2537f2c
MM
7844 ISO C++ 12.1. A constructor may not be declared
7845 const or volatile. A constructor may not be
7846 virtual. A constructor may not be static. */
3db45ab5 7847 if (staticp == 2)
e2537f2c
MM
7848 error ((flags == DTOR_FLAG)
7849 ? "destructor cannot be static member function"
7850 : "constructor cannot be static member function");
7851 if (memfn_quals)
8d08fdba 7852 {
e2537f2c
MM
7853 error ((flags == DTOR_FLAG)
7854 ? "destructors may not be cv-qualified"
7855 : "constructors may not be cv-qualified");
7856 memfn_quals = TYPE_UNQUALIFIED;
8d08fdba 7857 }
e2537f2c
MM
7858
7859 if (decl_context == FIELD
7860 && !member_function_or_else (ctype,
7861 current_class_type,
7862 flags))
13dbe691 7863 return error_mark_node;
e2537f2c
MM
7864
7865 if (flags != DTOR_FLAG)
8d08fdba 7866 {
e2537f2c 7867 /* It's a constructor. */
db5ae43f
MS
7868 if (explicitp == 1)
7869 explicitp = 2;
8d08fdba
MS
7870 if (virtualp)
7871 {
8251199e 7872 pedwarn ("constructors cannot be declared virtual");
8d08fdba
MS
7873 virtualp = 0;
7874 }
7137605e
MM
7875 if (decl_context == FIELD
7876 && sfk != sfk_constructor)
13dbe691 7877 return error_mark_node;
8d08fdba
MS
7878 }
7879 if (decl_context == FIELD)
7880 staticp = 0;
7881 }
b7484fbe 7882 else if (friendp)
8d08fdba 7883 {
b7484fbe 7884 if (initialized)
2d01edd7 7885 error ("can't initialize friend function %qs", name);
b7484fbe
MS
7886 if (virtualp)
7887 {
7888 /* Cannot be both friend and virtual. */
8251199e 7889 error ("virtual functions cannot be friends");
b7484fbe
MS
7890 friendp = 0;
7891 }
28cbf42c 7892 if (decl_context == NORMAL)
8251199e 7893 error ("friend declaration not in class definition");
28cbf42c 7894 if (current_function_decl && funcdef_flag)
2d01edd7 7895 error ("can't define friend function %qs in a local "
0cbd7506
MS
7896 "class definition",
7897 name);
8d08fdba
MS
7898 }
7899
caf93cb0 7900 arg_types = grokparms (declarator->u.function.parameters,
058b15c1 7901 &parms);
8d08fdba 7902
058b15c1
MM
7903 if (inner_declarator
7904 && inner_declarator->kind == cdk_id
7905 && inner_declarator->u.id.sfk == sfk_destructor
7906 && arg_types != void_list_node)
8d08fdba 7907 {
058b15c1
MM
7908 error ("destructors may not have parameters");
7909 arg_types = void_list_node;
7910 parms = NULL_TREE;
8d08fdba 7911 }
caf93cb0 7912
d22c8596 7913 type = build_function_type (type, arg_types);
8d08fdba
MS
7914 }
7915 break;
7916
058b15c1
MM
7917 case cdk_pointer:
7918 case cdk_reference:
7919 case cdk_ptrmem:
8d08fdba
MS
7920 /* Filter out pointers-to-references and references-to-references.
7921 We can get these if a TYPE_DECL is used. */
7922
7923 if (TREE_CODE (type) == REFERENCE_TYPE)
7924 {
8af2fec4
RY
7925 if (declarator->kind != cdk_reference)
7926 {
7927 error ("cannot declare pointer to %q#T", type);
7928 type = TREE_TYPE (type);
7929 }
7930
7931 /* In C++0x, we allow reference to reference declarations
7932 that occur indirectly through typedefs [7.1.3/8 dcl.typedef]
7933 and template type arguments [14.3.1/4 temp.arg.type]. The
7934 check for direct reference to reference declarations, which
7935 are still forbidden, occurs below. Reasoning behind the change
7936 can be found in DR106, DR540, and the rvalue reference
7937 proposals. */
c1ae8be5 7938 else if (cxx_dialect == cxx98)
8af2fec4
RY
7939 {
7940 error ("cannot declare reference to %q#T", type);
7941 type = TREE_TYPE (type);
7942 }
a5894242 7943 }
058b15c1
MM
7944 else if (VOID_TYPE_P (type))
7945 {
7946 if (declarator->kind == cdk_reference)
2d01edd7 7947 error ("cannot declare reference to %q#T", type);
058b15c1 7948 else if (declarator->kind == cdk_ptrmem)
2d01edd7 7949 error ("cannot declare pointer to %q#T member", type);
058b15c1 7950 }
61a127b3 7951
91063b51
MM
7952 /* We now know that the TYPE_QUALS don't apply to the decl,
7953 but to the target of the pointer. */
7954 type_quals = TYPE_UNQUALIFIED;
8d08fdba 7955
058b15c1 7956 if (declarator->kind == cdk_ptrmem
e2537f2c 7957 && (TREE_CODE (type) == FUNCTION_TYPE || memfn_quals))
058b15c1 7958 {
e2537f2c 7959 memfn_quals |= cp_type_quals (type);
3db45ab5 7960 type = build_memfn_type (type,
e2537f2c
MM
7961 declarator->u.pointer.class_type,
7962 memfn_quals);
7963 memfn_quals = TYPE_UNQUALIFIED;
058b15c1
MM
7964 }
7965
7966 if (declarator->kind == cdk_reference)
8d08fdba 7967 {
8af2fec4
RY
7968 /* In C++0x, the type we are creating a reference to might be
7969 a typedef which is itself a reference type. In that case,
7970 we follow the reference collapsing rules in
7971 [7.1.3/8 dcl.typedef] to create the final reference type:
7972
7973 "If a typedef TD names a type that is a reference to a type
7974 T, an attempt to create the type 'lvalue reference to cv TD'
7975 creates the type 'lvalue reference to T,' while an attempt
7976 to create the type "rvalue reference to cv TD' creates the
7977 type TD."
7978 */
dd29188b 7979 if (!VOID_TYPE_P (type))
8af2fec4
RY
7980 type = cp_build_reference_type
7981 ((TREE_CODE (type) == REFERENCE_TYPE
7982 ? TREE_TYPE (type) : type),
7983 (declarator->u.reference.rvalue_ref
7984 && (TREE_CODE(type) != REFERENCE_TYPE
7985 || TYPE_REF_IS_RVALUE (type))));
7986
7987 /* In C++0x, we need this check for direct reference to
7988 reference declarations, which are forbidden by
7989 [8.3.2/5 dcl.ref]. Reference to reference declarations
7990 are only allowed indirectly through typedefs and template
7991 type arguments. Example:
7992
7993 void foo(int & &); // invalid ref-to-ref decl
7994
7995 typedef int & int_ref;
7996 void foo(int_ref &); // valid ref-to-ref decl
7997 */
7998 if (inner_declarator && inner_declarator->kind == cdk_reference)
7999 error ("cannot declare reference to %q#T, which is not "
8000 "a typedef or a template type argument", type);
8d08fdba
MS
8001 }
8002 else if (TREE_CODE (type) == METHOD_TYPE)
d8f8dca1 8003 type = build_ptrmemfunc_type (build_pointer_type (type));
058b15c1 8004 else if (declarator->kind == cdk_ptrmem)
f4ed7d21 8005 {
63c9a190
MM
8006 gcc_assert (TREE_CODE (declarator->u.pointer.class_type)
8007 != NAMESPACE_DECL);
8008 if (declarator->u.pointer.class_type == error_mark_node)
4230cec2
NS
8009 /* We will already have complained. */
8010 type = error_mark_node;
f4ed7d21
NS
8011 else
8012 type = build_ptrmem_type (declarator->u.pointer.class_type,
8013 type);
8014 }
8d08fdba
MS
8015 else
8016 type = build_pointer_type (type);
8017
8018 /* Process a list of type modifier keywords (such as
8019 const or volatile) that were given inside the `*' or `&'. */
8020
058b15c1 8021 if (declarator->u.pointer.qualifiers)
8d08fdba 8022 {
caf93cb0
EC
8023 type
8024 = cp_build_qualified_type (type,
3c01e5df 8025 declarator->u.pointer.qualifiers);
4f2b0fb2 8026 type_quals = cp_type_quals (type);
8d08fdba 8027 }
8d08fdba
MS
8028 ctype = NULL_TREE;
8029 break;
8030
058b15c1
MM
8031 case cdk_error:
8032 break;
8d08fdba 8033
058b15c1 8034 default:
8dc2b103 8035 gcc_unreachable ();
058b15c1
MM
8036 }
8037 }
caf93cb0 8038
058b15c1
MM
8039 if (unqualified_id && TREE_CODE (unqualified_id) == TEMPLATE_ID_EXPR
8040 && TREE_CODE (type) != FUNCTION_TYPE
8041 && TREE_CODE (type) != METHOD_TYPE)
8042 {
2d01edd7 8043 error ("template-id %qD used as a declarator",
058b15c1
MM
8044 unqualified_id);
8045 unqualified_id = dname;
8046 }
386b8a85 8047
9c12301f
MM
8048 /* If TYPE is a FUNCTION_TYPE, but the function name was explicitly
8049 qualified with a class-name, turn it into a METHOD_TYPE, unless
8050 we know that the function is static. We take advantage of this
8051 opportunity to do other processing that pertains to entities
8052 explicitly declared to be class members. Note that if DECLARATOR
8053 is non-NULL, we know it is a cdk_id declarator; otherwise, we
8054 would not have exited the loop above. */
caf93cb0 8055 if (declarator
1d786913
MM
8056 && declarator->u.id.qualifying_scope
8057 && TYPE_P (declarator->u.id.qualifying_scope))
058b15c1
MM
8058 {
8059 tree t;
8d08fdba 8060
1d786913
MM
8061 ctype = declarator->u.id.qualifying_scope;
8062 ctype = TYPE_MAIN_VARIANT (ctype);
058b15c1
MM
8063 t = ctype;
8064 while (t != NULL_TREE && CLASS_TYPE_P (t))
8065 {
8066 /* You're supposed to have one `template <...>' for every
8067 template class, but you don't need one for a full
8068 specialization. For example:
8d08fdba 8069
058b15c1
MM
8070 template <class T> struct S{};
8071 template <> struct S<int> { void f(); };
8072 void S<int>::f () {}
28cbf42c 8073
058b15c1
MM
8074 is correct; there shouldn't be a `template <>' for the
8075 definition of `S<int>::f'. */
f0d60e22
MM
8076 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t)
8077 && !any_dependent_template_arguments_p (CLASSTYPE_TI_ARGS (t)))
8078 /* T is an explicit (not partial) specialization. All
8079 containing classes must therefore also be explicitly
8080 specialized. */
8081 break;
8082 if ((CLASSTYPE_USE_TEMPLATE (t) || CLASSTYPE_IS_TEMPLATE (t))
058b15c1
MM
8083 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t)))
8084 template_count += 1;
28cbf42c 8085
058b15c1
MM
8086 t = TYPE_MAIN_DECL (t);
8087 t = DECL_CONTEXT (t);
8088 }
8d08fdba 8089
058b15c1 8090 if (ctype == current_class_type)
59e7c84c
VR
8091 {
8092 if (friendp)
b46ce77f
SM
8093 {
8094 pedwarn ("member functions are implicitly friends of their class");
8095 friendp = 0;
8096 }
59e7c84c
VR
8097 else
8098 pedwarn ("extra qualification %<%T::%> on member %qs",
8099 ctype, name);
8100 }
fa6098f8
MM
8101 else if (/* If the qualifying type is already complete, then we
8102 can skip the following checks. */
8103 !COMPLETE_TYPE_P (ctype)
cc328145
MM
8104 && (/* If the function is being defined, then
8105 qualifying type must certainly be complete. */
3db45ab5 8106 funcdef_flag
cc328145
MM
8107 /* A friend declaration of "T::f" is OK, even if
8108 "T" is a template parameter. But, if this
8109 function is not a friend, the qualifying type
8110 must be a class. */
8111 || (!friendp && !CLASS_TYPE_P (ctype))
8112 /* For a declaration, the type need not be
8113 complete, if either it is dependent (since there
8114 is no meaningful definition of complete in that
8115 case) or the qualifying class is currently being
8116 defined. */
fa6098f8
MM
8117 || !(dependent_type_p (ctype)
8118 || currently_open_class (ctype)))
9f9a713e 8119 /* Check that the qualifying type is complete. */
fa6098f8
MM
8120 && !complete_type_or_else (ctype, NULL_TREE))
8121 return error_mark_node;
058b15c1
MM
8122 else if (TREE_CODE (type) == FUNCTION_TYPE)
8123 {
1d786913 8124 tree sname = declarator->u.id.unqualified_name;
50ad9642 8125
357d956e
MM
8126 if (current_class_type
8127 && (!friendp || funcdef_flag))
8128 {
8129 error (funcdef_flag
8130 ? "cannot define member function %<%T::%s%> within %<%T%>"
8131 : "cannot declare member function %<%T::%s%> within %<%T%>",
8132 ctype, name, current_class_type);
8133 return error_mark_node;
8134 }
8135
058b15c1
MM
8136 if (TREE_CODE (sname) == IDENTIFIER_NODE
8137 && NEW_DELETE_OPNAME_P (sname))
8138 /* Overloaded operator new and operator delete
8139 are always static functions. */
8140 ;
058b15c1 8141 else
e2537f2c 8142 type = build_memfn_type (type, ctype, memfn_quals);
058b15c1 8143 }
62d1db17 8144 else if (declspecs->specs[(int)ds_typedef]
fa6098f8 8145 && current_class_type)
058b15c1 8146 {
fa6098f8
MM
8147 error ("cannot declare member %<%T::%s%> within %qT",
8148 ctype, name, current_class_type);
058b15c1 8149 return error_mark_node;
8d08fdba
MS
8150 }
8151 }
8152
9c12301f
MM
8153 /* Now TYPE has the actual type. */
8154
91d231cb 8155 if (returned_attrs)
1eab9b56 8156 {
91d231cb
JM
8157 if (attrlist)
8158 *attrlist = chainon (returned_attrs, *attrlist);
1eab9b56 8159 else
91d231cb 8160 attrlist = &returned_attrs;
1eab9b56
JM
8161 }
8162
5d80a306
DG
8163 /* Handle parameter packs. */
8164 if (parameter_pack_p)
8165 {
8166 if (decl_context == PARM)
8167 /* Turn the type into a pack expansion.*/
8168 type = make_pack_expansion (type);
8169 else
8170 error ("non-parameter %qs cannot be a parameter pack", name);
8171 }
8172
55b3d665
JM
8173 /* Did array size calculations overflow? */
8174
8175 if (TREE_CODE (type) == ARRAY_TYPE
8176 && COMPLETE_TYPE_P (type)
96ce2ac9
JW
8177 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST
8178 && TREE_OVERFLOW (TYPE_SIZE_UNIT (type)))
55b3d665 8179 {
2d01edd7 8180 error ("size of array %qs is too large", name);
aba649ba 8181 /* If we proceed with the array type as it is, we'll eventually
55b3d665
JM
8182 crash in tree_low_cst(). */
8183 type = error_mark_node;
8184 }
8185
2fff6d71 8186 if ((decl_context == FIELD || decl_context == PARM)
caf93cb0 8187 && !processing_template_decl
5377d5ba 8188 && variably_modified_type_p (type, NULL_TREE))
dac45b5c 8189 {
2fff6d71 8190 if (decl_context == FIELD)
2d01edd7 8191 error ("data member may not have variably modified type %qT", type);
2fff6d71 8192 else
2d01edd7 8193 error ("parameter may not have variably modified type %qT", type);
dac45b5c
MM
8194 type = error_mark_node;
8195 }
8196
34fc957d 8197 if (explicitp == 1 || (explicitp && friendp))
db5ae43f 8198 {
34fc957d 8199 /* [dcl.fct.spec] The explicit specifier shall only be used in
0cbd7506 8200 declarations of constructors within a class definition. */
2d01edd7 8201 error ("only declarations of constructors can be %<explicit%>");
db5ae43f
MS
8202 explicitp = 0;
8203 }
8204
1ff3c076 8205 if (storage_class == sc_mutable)
f30432d7 8206 {
4223f82f 8207 if (decl_context != FIELD || friendp)
0cbd7506 8208 {
2d01edd7 8209 error ("non-member %qs cannot be declared %<mutable%>", name);
1ff3c076 8210 storage_class = sc_none;
0cbd7506 8211 }
62d1db17 8212 else if (decl_context == TYPENAME || declspecs->specs[(int)ds_typedef])
f30432d7 8213 {
2d01edd7 8214 error ("non-object member %qs cannot be declared %<mutable%>", name);
1ff3c076 8215 storage_class = sc_none;
f30432d7 8216 }
34fc957d 8217 else if (TREE_CODE (type) == FUNCTION_TYPE
0cbd7506
MS
8218 || TREE_CODE (type) == METHOD_TYPE)
8219 {
2d01edd7 8220 error ("function %qs cannot be declared %<mutable%>", name);
1ff3c076 8221 storage_class = sc_none;
0cbd7506 8222 }
f30432d7
MS
8223 else if (staticp)
8224 {
2d01edd7 8225 error ("static %qs cannot be declared %<mutable%>", name);
1ff3c076 8226 storage_class = sc_none;
f30432d7 8227 }
34fc957d
NS
8228 else if (type_quals & TYPE_QUAL_CONST)
8229 {
2d01edd7 8230 error ("const %qs cannot be declared %<mutable%>", name);
1ff3c076 8231 storage_class = sc_none;
34fc957d 8232 }
f30432d7
MS
8233 }
8234
419c6212 8235 /* If this is declaring a typedef name, return a TYPE_DECL. */
62d1db17 8236 if (declspecs->specs[(int)ds_typedef] && decl_context != TYPENAME)
8d08fdba
MS
8237 {
8238 tree decl;
8239
8240 /* Note that the grammar rejects storage classes
8241 in typenames, fields or parameters. */
eff71ab0
PB
8242 if (current_lang_name == lang_name_java)
8243 TYPE_FOR_JAVA (type) = 1;
3db45ab5 8244
e2537f2c
MM
8245 /* This declaration:
8246
3db45ab5 8247 typedef void f(int) const;
e2537f2c 8248
3db45ab5 8249 declares a function type which is not a member of any
e2537f2c 8250 particular class, but which is cv-qualified; for
3db45ab5 8251 example "f S::*" declares a pointer to a const-qualified
e2537f2c
MM
8252 member function of S. We record the cv-qualification in the
8253 function type. */
8254 if (memfn_quals && TREE_CODE (type) == FUNCTION_TYPE)
771026dd
DG
8255 {
8256 type = cp_build_qualified_type (type, memfn_quals);
8257
8258 /* We have now dealt with these qualifiers. */
8259 memfn_quals = TYPE_UNQUALIFIED;
8260 }
8d08fdba 8261
d2e5ee5c 8262 if (decl_context == FIELD)
e3016344 8263 decl = build_lang_decl (TYPE_DECL, unqualified_id, type);
d2e5ee5c 8264 else
e3016344
MM
8265 decl = build_decl (TYPE_DECL, unqualified_id, type);
8266 if (id_declarator && declarator->u.id.qualifying_scope)
8267 error ("%Jtypedef name may not be a nested-name-specifier", decl);
8268
8269 if (decl_context != FIELD)
6bda7a5e 8270 {
6bda7a5e
NS
8271 if (!current_function_decl)
8272 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
1dc82a99 8273 else if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (current_function_decl)
c8094d83 8274 || (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P
1dc82a99
MM
8275 (current_function_decl)))
8276 /* The TYPE_DECL is "abstract" because there will be
8277 clones of this constructor/destructor, and there will
8278 be copies of this TYPE_DECL generated in those
8279 clones. */
8280 DECL_ABSTRACT (decl) = 1;
6bda7a5e 8281 }
e3016344
MM
8282 else if (constructor_name_p (unqualified_id, current_class_type))
8283 pedwarn ("ISO C++ forbids nested type %qD with same name "
8284 "as enclosing class",
8285 unqualified_id);
caf93cb0 8286
9188c363
MM
8287 /* If the user declares "typedef struct {...} foo" then the
8288 struct will have an anonymous name. Fill that name in now.
8289 Nothing can refer to it, so nothing needs know about the name
8290 change. */
8d08fdba 8291 if (type != error_mark_node
058b15c1 8292 && unqualified_id
8d08fdba
MS
8293 && TYPE_NAME (type)
8294 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
1951a1b6 8295 && TYPE_ANONYMOUS_P (type)
ac905924
JM
8296 /* Don't do this if there are attributes. */
8297 && (!attrlist || !*attrlist)
89d684bb 8298 && cp_type_quals (type) == TYPE_UNQUALIFIED)
8d08fdba 8299 {
dcd08efc
JM
8300 tree oldname = TYPE_NAME (type);
8301 tree t;
8302
2c73f9f5 8303 /* Replace the anonymous name with the real name everywhere. */
dcd08efc
JM
8304 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
8305 if (TYPE_NAME (t) == oldname)
8306 TYPE_NAME (t) = decl;
8d08fdba
MS
8307
8308 if (TYPE_LANG_SPECIFIC (type))
8309 TYPE_WAS_ANONYMOUS (type) = 1;
8310
33964bf4
MM
8311 /* If this is a typedef within a template class, the nested
8312 type is a (non-primary) template. The name for the
8313 template needs updating as well. */
8314 if (TYPE_LANG_SPECIFIC (type) && CLASSTYPE_TEMPLATE_INFO (type))
68642fb6 8315 DECL_NAME (CLASSTYPE_TI_TEMPLATE (type))
33964bf4
MM
8316 = TYPE_IDENTIFIER (type);
8317
50a6dbd7
JM
8318 /* FIXME remangle member functions; member functions of a
8319 type with external linkage have external linkage. */
fc378698 8320 }
fc378698 8321
1ff3c076 8322 if (signed_p
8d08fdba
MS
8323 || (typedef_decl && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl)))
8324 C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
8325
3db45ab5 8326 bad_specifiers (decl, "type", virtualp,
e2537f2c 8327 memfn_quals != TYPE_UNQUALIFIED,
c91a56d2
MS
8328 inlinep, friendp, raises != NULL_TREE);
8329
8d08fdba
MS
8330 return decl;
8331 }
8332
8333 /* Detect the case of an array type of unspecified size
8334 which came, as such, direct from a typedef name.
8d6e459d
NS
8335 We must copy the type, so that the array's domain can be
8336 individually set by the object's initializer. */
8d08fdba 8337
8d6e459d
NS
8338 if (type && typedef_type
8339 && TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type)
55b3d665 8340 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (typedef_type))
8d6e459d 8341 type = build_cplus_array_type (TREE_TYPE (type), NULL_TREE);
8d08fdba 8342
969fd501 8343 /* Detect where we're using a typedef of function type to declare a
4546865e 8344 function. PARMS will not be set, so we must create it now. */
caf93cb0 8345
969fd501
NS
8346 if (type == typedef_type && TREE_CODE (type) == FUNCTION_TYPE)
8347 {
8348 tree decls = NULL_TREE;
8349 tree args;
8350
8351 for (args = TYPE_ARG_TYPES (type); args; args = TREE_CHAIN (args))
8352 {
8e51619a 8353 tree decl = cp_build_parm_decl (NULL_TREE, TREE_VALUE (args));
969fd501
NS
8354
8355 TREE_CHAIN (decl) = decls;
8356 decls = decl;
8357 }
caf93cb0 8358
4546865e 8359 parms = nreverse (decls);
9804209d
DG
8360
8361 if (decl_context != TYPENAME)
0cbd7506
MS
8362 {
8363 /* A cv-qualifier-seq shall only be part of the function type
8364 for a non-static member function. [8.3.5/4 dcl.fct] */
8365 if (cp_type_quals (type) != TYPE_UNQUALIFIED
8366 && (current_class_type == NULL_TREE || staticp) )
8367 {
ebbc3ce1 8368 error (staticp
f88d05c0
DM
8369 ? G_("qualified function types cannot be used to "
8370 "declare static member functions")
8371 : G_("qualified function types cannot be used to "
8372 "declare free functions"));
0cbd7506
MS
8373 type = TYPE_MAIN_VARIANT (type);
8374 }
8375
8376 /* The qualifiers on the function type become the qualifiers on
8377 the non-static member function. */
e2537f2c 8378 memfn_quals |= cp_type_quals (type);
0cbd7506 8379 }
969fd501
NS
8380 }
8381
8d08fdba
MS
8382 /* If this is a type name (such as, in a cast or sizeof),
8383 compute the type and return it now. */
8384
8385 if (decl_context == TYPENAME)
8386 {
8387 /* Note that the grammar rejects storage classes
8388 in typenames, fields or parameters. */
91063b51 8389 if (type_quals != TYPE_UNQUALIFIED)
6eabb241 8390 type_quals = TYPE_UNQUALIFIED;
8d08fdba
MS
8391
8392 /* Special case: "friend class foo" looks like a TYPENAME context. */
8393 if (friendp)
8394 {
91063b51 8395 if (type_quals != TYPE_UNQUALIFIED)
b7484fbe 8396 {
33bd39a2 8397 error ("type qualifiers specified for friend class declaration");
91063b51 8398 type_quals = TYPE_UNQUALIFIED;
b7484fbe
MS
8399 }
8400 if (inlinep)
8401 {
2d01edd7 8402 error ("%<inline%> specified for friend class declaration");
b7484fbe
MS
8403 inlinep = 0;
8404 }
f2ae0c45 8405
218e0eb6 8406 if (!current_aggr)
4b054b80 8407 {
218e0eb6 8408 /* Don't allow friend declaration without a class-key. */
4b054b80 8409 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
218e0eb6 8410 pedwarn ("template parameters cannot be friends");
f2ae0c45 8411 else if (TREE_CODE (type) == TYPENAME_TYPE)
0cbd7506 8412 pedwarn ("friend declaration requires class-key, "
2d01edd7 8413 "i.e. %<friend class %T::%D%>",
218e0eb6 8414 TYPE_CONTEXT (type), TYPENAME_TYPE_FULLNAME (type));
4b054b80 8415 else
0cbd7506 8416 pedwarn ("friend declaration requires class-key, "
2d01edd7 8417 "i.e. %<friend %#T%>",
218e0eb6 8418 type);
4b054b80 8419 }
b7484fbe
MS
8420
8421 /* Only try to do this stuff if we didn't already give up. */
8422 if (type != integer_type_node)
8423 {
8424 /* A friendly class? */
8425 if (current_class_type)
19db77ce
KL
8426 make_friend_class (current_class_type, TYPE_MAIN_VARIANT (type),
8427 /*complain=*/true);
b7484fbe 8428 else
2d01edd7 8429 error ("trying to make class %qT a friend of global scope",
0cbd7506 8430 type);
d363e7bf 8431
b7484fbe
MS
8432 type = void_type_node;
8433 }
8d08fdba 8434 }
e2537f2c 8435 else if (memfn_quals)
8d08fdba 8436 {
8d08fdba
MS
8437 if (ctype == NULL_TREE)
8438 {
41cbc04c 8439 if (TREE_CODE (type) != METHOD_TYPE)
0cbd7506 8440 error ("invalid qualifiers on non-member function type");
41cbc04c 8441 else
0cbd7506 8442 ctype = TYPE_METHOD_BASETYPE (type);
41cbc04c
NS
8443 }
8444 if (ctype)
e2537f2c 8445 type = build_memfn_type (type, ctype, memfn_quals);
8d08fdba
MS
8446 }
8447
8448 return type;
8449 }
058b15c1 8450 else if (unqualified_id == NULL_TREE && decl_context != PARM
db5ae43f 8451 && decl_context != CATCHPARM
8d08fdba
MS
8452 && TREE_CODE (type) != UNION_TYPE
8453 && ! bitfield)
8454 {
2d01edd7 8455 error ("abstract declarator %qT used as declaration", type);
22ab714d 8456 return error_mark_node;
8d08fdba
MS
8457 }
8458
62e19030
MM
8459 /* Only functions may be declared using an operator-function-id. */
8460 if (unqualified_id
8461 && IDENTIFIER_OPNAME_P (unqualified_id)
8462 && TREE_CODE (type) != FUNCTION_TYPE
8463 && TREE_CODE (type) != METHOD_TYPE)
8d08fdba 8464 {
62e19030
MM
8465 error ("declaration of %qD as non-function", unqualified_id);
8466 return error_mark_node;
8d08fdba
MS
8467 }
8468
62e19030
MM
8469 /* We don't check parameter types here because we can emit a better
8470 error message later. */
8471 if (decl_context != PARM)
650fcd07
LM
8472 {
8473 type = check_var_type (unqualified_id, type);
8474 if (type == error_mark_node)
8475 return error_mark_node;
8476 }
62e19030 8477
8d08fdba
MS
8478 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
8479 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
8480
14ae7e7d
JM
8481 if (decl_context == PARM || decl_context == CATCHPARM)
8482 {
8483 if (ctype || in_namespace)
2d01edd7 8484 error ("cannot use %<::%> in parameter declaration");
14ae7e7d
JM
8485
8486 /* A parameter declared as an array of T is really a pointer to T.
8487 One declared as a function is really a pointer to a function.
8488 One declared as a member is really a pointer to member. */
8489
8490 if (TREE_CODE (type) == ARRAY_TYPE)
8491 {
8492 /* Transfer const-ness of array into that of type pointed to. */
8493 type = build_pointer_type (TREE_TYPE (type));
91063b51 8494 type_quals = TYPE_UNQUALIFIED;
14ae7e7d
JM
8495 }
8496 else if (TREE_CODE (type) == FUNCTION_TYPE)
8497 type = build_pointer_type (type);
14ae7e7d 8498 }
68642fb6 8499
8d08fdba 8500 {
926ce8bd 8501 tree decl;
8d08fdba
MS
8502
8503 if (decl_context == PARM)
8504 {
058b15c1 8505 decl = cp_build_parm_decl (unqualified_id, type);
8d08fdba 8506
3db45ab5 8507 bad_specifiers (decl, "parameter", virtualp,
e2537f2c 8508 memfn_quals != TYPE_UNQUALIFIED,
8d08fdba 8509 inlinep, friendp, raises != NULL_TREE);
8d08fdba
MS
8510 }
8511 else if (decl_context == FIELD)
8512 {
01bf0f3e
JM
8513 /* The C99 flexible array extension. */
8514 if (!staticp && TREE_CODE (type) == ARRAY_TYPE
8515 && TYPE_DOMAIN (type) == NULL_TREE)
8516 {
8517 tree itype = compute_array_index_type (dname, integer_zero_node);
8518 type = build_cplus_array_type (TREE_TYPE (type), itype);
8519 }
8520
8d08fdba
MS
8521 if (type == error_mark_node)
8522 {
8523 /* Happens when declaring arrays of sizes which
8524 are error_mark_node, for example. */
8525 decl = NULL_TREE;
8526 }
997a088c 8527 else if (in_namespace && !friendp)
05008fb9
MM
8528 {
8529 /* Something like struct S { int N::j; }; */
2d01edd7 8530 error ("invalid use of %<::%>");
9a171ca4 8531 return error_mark_node;
05008fb9 8532 }
8d08fdba
MS
8533 else if (TREE_CODE (type) == FUNCTION_TYPE)
8534 {
8535 int publicp = 0;
e1467ff2 8536 tree function_context;
8d08fdba
MS
8537
8538 if (friendp == 0)
8539 {
8540 if (ctype == NULL_TREE)
8541 ctype = current_class_type;
8542
8543 if (ctype == NULL_TREE)
8544 {
2d01edd7 8545 error ("can't make %qD into a method -- not in a class",
058b15c1 8546 unqualified_id);
13dbe691 8547 return error_mark_node;
8d08fdba
MS
8548 }
8549
8550 /* ``A union may [ ... ] not [ have ] virtual functions.''
8551 ARM 9.5 */
8552 if (virtualp && TREE_CODE (ctype) == UNION_TYPE)
8553 {
2d01edd7 8554 error ("function %qD declared virtual inside a union",
058b15c1 8555 unqualified_id);
13dbe691 8556 return error_mark_node;
8d08fdba
MS
8557 }
8558
058b15c1 8559 if (NEW_DELETE_OPNAME_P (unqualified_id))
8d08fdba
MS
8560 {
8561 if (virtualp)
8562 {
2d01edd7 8563 error ("%qD cannot be declared virtual, since it "
0cbd7506 8564 "is always static",
058b15c1 8565 unqualified_id);
8d08fdba
MS
8566 virtualp = 0;
8567 }
8568 }
8569 else if (staticp < 2)
e2537f2c 8570 type = build_memfn_type (type, ctype, memfn_quals);
8d08fdba
MS
8571 }
8572
88e95ee3 8573 /* Check that the name used for a destructor makes sense. */
6d2989e1 8574 if (sfk == sfk_destructor)
88e95ee3 8575 {
6d2989e1
VR
8576 if (!ctype)
8577 {
8578 gcc_assert (friendp);
8579 error ("expected qualified name in friend declaration "
8580 "for destructor %qD",
8581 id_declarator->u.id.unqualified_name);
8582 return error_mark_node;
8583 }
8584
8585 if (!same_type_p (TREE_OPERAND
8586 (id_declarator->u.id.unqualified_name, 0),
8587 ctype))
8588 {
8589 error ("declaration of %qD as member of %qT",
8590 id_declarator->u.id.unqualified_name, ctype);
8591 return error_mark_node;
8592 }
88e95ee3
MM
8593 }
8594
8d08fdba 8595 /* Tell grokfndecl if it needs to set TREE_PUBLIC on the node. */
68642fb6 8596 function_context = (ctype != NULL_TREE) ?
4f1c5b7d 8597 decl_function_context (TYPE_MAIN_DECL (ctype)) : NULL_TREE;
e1467ff2
MM
8598 publicp = (! friendp || ! staticp)
8599 && function_context == NULL_TREE;
68642fb6 8600 decl = grokfndecl (ctype, type,
058b15c1
MM
8601 TREE_CODE (unqualified_id) != TEMPLATE_ID_EXPR
8602 ? unqualified_id : dname,
4546865e 8603 parms,
058b15c1 8604 unqualified_id,
e2537f2c 8605 virtualp, flags, memfn_quals, raises,
386b8a85 8606 friendp ? -1 : 0, friendp, publicp, inlinep,
27d6592c 8607 sfk,
037cc9c5 8608 funcdef_flag, template_count, in_namespace, attrlist);
20496fa2 8609 if (decl == NULL_TREE)
ba5719d9 8610 return error_mark_node;
6125f3be
DE
8611#if 0
8612 /* This clobbers the attrs stored in `decl' from `attrlist'. */
91d231cb
JM
8613 /* The decl and setting of decl_attr is also turned off. */
8614 decl = build_decl_attribute_variant (decl, decl_attr);
6125f3be 8615#endif
f0e01782 8616
cc804e51
MM
8617 /* [class.conv.ctor]
8618
8619 A constructor declared without the function-specifier
8620 explicit that can be called with a single parameter
8621 specifies a conversion from the type of its first
8622 parameter to the type of its class. Such a constructor
8623 is called a converting constructor. */
db5ae43f
MS
8624 if (explicitp == 2)
8625 DECL_NONCONVERTING_P (decl) = 1;
cc804e51
MM
8626 else if (DECL_CONSTRUCTOR_P (decl))
8627 {
8628 /* The constructor can be called with exactly one
8629 parameter if there is at least one parameter, and
8630 any subsequent parameters have default arguments.
e0fff4b3
JM
8631 Ignore any compiler-added parms. */
8632 tree arg_types = FUNCTION_FIRST_USER_PARMTYPE (decl);
cc804e51
MM
8633
8634 if (arg_types == void_list_node
68642fb6
UD
8635 || (arg_types
8636 && TREE_CHAIN (arg_types)
cc804e51
MM
8637 && TREE_CHAIN (arg_types) != void_list_node
8638 && !TREE_PURPOSE (TREE_CHAIN (arg_types))))
8639 DECL_NONCONVERTING_P (decl) = 1;
8640 }
8d08fdba
MS
8641 }
8642 else if (TREE_CODE (type) == METHOD_TYPE)
8643 {
faae18ab
MS
8644 /* We only get here for friend declarations of
8645 members of other classes. */
8d08fdba
MS
8646 /* All method decls are public, so tell grokfndecl to set
8647 TREE_PUBLIC, also. */
866eb556 8648 decl = grokfndecl (ctype, type,
058b15c1
MM
8649 TREE_CODE (unqualified_id) != TEMPLATE_ID_EXPR
8650 ? unqualified_id : dname,
4546865e 8651 parms,
058b15c1 8652 unqualified_id,
e2537f2c 8653 virtualp, flags, memfn_quals, raises,
27d6592c 8654 friendp ? -1 : 0, friendp, 1, 0, sfk,
c8094d83
MS
8655 funcdef_flag, template_count, in_namespace,
8656 attrlist);
f0e01782 8657 if (decl == NULL_TREE)
13dbe691 8658 return error_mark_node;
8d08fdba 8659 }
a9f46cbb 8660 else if (!staticp && !dependent_type_p (type)
d0f062fb 8661 && !COMPLETE_TYPE_P (complete_type (type))
8d08fdba
MS
8662 && (TREE_CODE (type) != ARRAY_TYPE || initialized == 0))
8663 {
058b15c1 8664 if (unqualified_id)
2d01edd7 8665 error ("field %qD has incomplete type", unqualified_id);
b7484fbe 8666 else
2d01edd7 8667 error ("name %qT has incomplete type", type);
8d08fdba
MS
8668
8669 /* If we're instantiating a template, tell them which
8670 instantiation made the field's type be incomplete. */
8671 if (current_class_type
8672 && TYPE_NAME (current_class_type)
d2e5ee5c 8673 && IDENTIFIER_TEMPLATE (TYPE_IDENTIFIER (current_class_type))
62d1db17
MM
8674 && declspecs->type
8675 && declspecs->type == type)
2d01edd7 8676 error (" in instantiation of template %qT",
0cbd7506 8677 current_class_type);
db5ae43f 8678
623c65f1 8679 return error_mark_node;
8d08fdba
MS
8680 }
8681 else
8682 {
8683 if (friendp)
8684 {
2d01edd7 8685 error ("%qE is neither function nor member function; "
0cbd7506 8686 "cannot be declared friend", unqualified_id);
8d08fdba
MS
8687 friendp = 0;
8688 }
8689 decl = NULL_TREE;
8690 }
8691
8692 if (friendp)
8693 {
8694 /* Friends are treated specially. */
8695 if (ctype == current_class_type)
59e7c84c 8696 ; /* We already issued a pedwarn. */
0cbd7506 8697 else if (decl && DECL_NAME (decl))
8db1028e
NS
8698 {
8699 if (template_class_depth (current_class_type) == 0)
8700 {
8701 decl = check_explicit_specialization
058b15c1 8702 (unqualified_id, decl, template_count,
357d956e 8703 2 * funcdef_flag + 4);
8db1028e
NS
8704 if (decl == error_mark_node)
8705 return error_mark_node;
8706 }
caf93cb0 8707
058b15c1 8708 decl = do_friend (ctype, unqualified_id, decl,
3db45ab5 8709 *attrlist, flags,
e2537f2c 8710 funcdef_flag);
8db1028e
NS
8711 return decl;
8712 }
8713 else
13dbe691 8714 return error_mark_node;
8d08fdba
MS
8715 }
8716
f4f206f4 8717 /* Structure field. It may not be a function, except for C++. */
8d08fdba
MS
8718
8719 if (decl == NULL_TREE)
8720 {
8d08fdba
MS
8721 if (initialized)
8722 {
3ac3d9ea
MM
8723 if (!staticp)
8724 {
8725 /* An attempt is being made to initialize a non-static
8726 member. But, from [class.mem]:
68642fb6 8727
3ac3d9ea
MM
8728 4 A member-declarator can contain a
8729 constant-initializer only if it declares a static
8730 member (_class.static_) of integral or enumeration
68642fb6 8731 type, see _class.static.data_.
3ac3d9ea
MM
8732
8733 This used to be relatively common practice, but
8734 the rest of the compiler does not correctly
8735 handle the initialization unless the member is
8736 static so we make it static below. */
2d01edd7 8737 pedwarn ("ISO C++ forbids initialization of member %qD",
058b15c1 8738 unqualified_id);
2d01edd7 8739 pedwarn ("making %qD static", unqualified_id);
3ac3d9ea
MM
8740 staticp = 1;
8741 }
8742
6ba89f8e
MM
8743 if (uses_template_parms (type))
8744 /* We'll check at instantiation time. */
8745 ;
058b15c1 8746 else if (check_static_variable_definition (unqualified_id,
6ba89f8e
MM
8747 type))
8748 /* If we just return the declaration, crashes
8749 will sometimes occur. We therefore return
72a93143
JM
8750 void_type_node, as if this was a friend
8751 declaration, to cause callers to completely
8752 ignore this declaration. */
13dbe691 8753 return error_mark_node;
8d08fdba
MS
8754 }
8755
3ac3d9ea 8756 if (staticp)
8d08fdba 8757 {
f18a14bc
MM
8758 /* C++ allows static class members. All other work
8759 for this is done by grokfield. */
058b15c1 8760 decl = build_lang_decl (VAR_DECL, unqualified_id, type);
4684cd27
MM
8761 set_linkage_for_static_data_member (decl);
8762 /* Even if there is an in-class initialization, DECL
8763 is considered undefined until an out-of-class
8764 definition is provided. */
8765 DECL_EXTERNAL (decl) = 1;
31a79236
JJ
8766
8767 if (thread_p)
8893239d 8768 DECL_TLS_MODEL (decl) = decl_default_tls_model (decl);
8d08fdba
MS
8769 }
8770 else
8771 {
058b15c1 8772 decl = build_decl (FIELD_DECL, unqualified_id, type);
2bf105ab 8773 DECL_NONADDRESSABLE_P (decl) = bitfield;
ab53bae2
AO
8774 if (bitfield && !unqualified_id)
8775 TREE_NO_WARNING (decl) = 1;
8776
1ff3c076 8777 if (storage_class == sc_mutable)
8d08fdba
MS
8778 {
8779 DECL_MUTABLE_P (decl) = 1;
1ff3c076 8780 storage_class = sc_none;
8d08fdba
MS
8781 }
8782 }
8783
3db45ab5 8784 bad_specifiers (decl, "field", virtualp,
e2537f2c 8785 memfn_quals != TYPE_UNQUALIFIED,
8d08fdba
MS
8786 inlinep, friendp, raises != NULL_TREE);
8787 }
8788 }
fd9aef9d
NS
8789 else if (TREE_CODE (type) == FUNCTION_TYPE
8790 || TREE_CODE (type) == METHOD_TYPE)
8d08fdba 8791 {
386b8a85 8792 tree original_name;
8d08fdba
MS
8793 int publicp = 0;
8794
058b15c1 8795 if (!unqualified_id)
13dbe691 8796 return error_mark_node;
8d08fdba 8797
058b15c1 8798 if (TREE_CODE (unqualified_id) == TEMPLATE_ID_EXPR)
386b8a85
JM
8799 original_name = dname;
8800 else
058b15c1 8801 original_name = unqualified_id;
386b8a85 8802
1ff3c076 8803 if (storage_class == sc_auto)
2d01edd7 8804 error ("storage class %<auto%> invalid for function %qs", name);
1ff3c076 8805 else if (storage_class == sc_register)
2d01edd7 8806 error ("storage class %<register%> invalid for function %qs", name);
1ff3c076 8807 else if (thread_p)
2d01edd7 8808 error ("storage class %<__thread%> invalid for function %qs", name);
8d08fdba
MS
8809
8810 /* Function declaration not at top level.
8811 Storage classes other than `extern' are not allowed
8812 and `extern' makes no difference. */
a9aedbc2 8813 if (! toplevel_bindings_p ()
1ff3c076 8814 && (storage_class == sc_static
62d1db17 8815 || declspecs->specs[(int)ds_inline])
8d08fdba 8816 && pedantic)
8926095f 8817 {
1ff3c076 8818 if (storage_class == sc_static)
2d01edd7 8819 pedwarn ("%<static%> specified invalid for function %qs "
0cbd7506 8820 "declared out of global scope", name);
8926095f 8821 else
2d01edd7 8822 pedwarn ("%<inline%> specifier invalid for function %qs "
0cbd7506 8823 "declared out of global scope", name);
8926095f 8824 }
68642fb6 8825
8d08fdba
MS
8826 if (ctype == NULL_TREE)
8827 {
8828 if (virtualp)
8829 {
2d01edd7 8830 error ("virtual non-class function %qs", name);
8d08fdba
MS
8831 virtualp = 0;
8832 }
8d08fdba 8833 }
4546865e
MM
8834 else if (TREE_CODE (type) == FUNCTION_TYPE && staticp < 2
8835 && !NEW_DELETE_OPNAME_P (original_name))
caf93cb0 8836 type = build_method_type_directly (ctype,
43dc123f
MM
8837 TREE_TYPE (type),
8838 TYPE_ARG_TYPES (type));
8d08fdba 8839
eb66be0e 8840 /* Record presence of `static'. */
faae18ab 8841 publicp = (ctype != NULL_TREE
1ff3c076
MM
8842 || storage_class == sc_extern
8843 || storage_class != sc_static);
8d08fdba 8844
058b15c1 8845 decl = grokfndecl (ctype, type, original_name, parms, unqualified_id,
e2537f2c 8846 virtualp, flags, memfn_quals, raises,
75650646 8847 1, friendp,
27d6592c 8848 publicp, inlinep, sfk, funcdef_flag,
037cc9c5 8849 template_count, in_namespace, attrlist);
f0e01782 8850 if (decl == NULL_TREE)
13dbe691 8851 return error_mark_node;
8d08fdba 8852
8d08fdba
MS
8853 if (staticp == 1)
8854 {
0e339752 8855 int invalid_static = 0;
8d08fdba
MS
8856
8857 /* Don't allow a static member function in a class, and forbid
8858 declaring main to be static. */
8859 if (TREE_CODE (type) == METHOD_TYPE)
8860 {
2d01edd7 8861 pedwarn ("cannot declare member function %qD to have "
0cbd7506 8862 "static linkage", decl);
0e339752 8863 invalid_static = 1;
8d08fdba 8864 }
8d08fdba
MS
8865 else if (current_function_decl)
8866 {
8867 /* FIXME need arm citation */
8251199e 8868 error ("cannot declare static function inside another function");
0e339752 8869 invalid_static = 1;
8d08fdba
MS
8870 }
8871
0e339752 8872 if (invalid_static)
8d08fdba
MS
8873 {
8874 staticp = 0;
1ff3c076 8875 storage_class = sc_none;
8d08fdba
MS
8876 }
8877 }
8d08fdba
MS
8878 }
8879 else
8880 {
8881 /* It's a variable. */
8882
8883 /* An uninitialized decl with `extern' is a reference. */
caf93cb0 8884 decl = grokvardecl (type, unqualified_id,
62d1db17 8885 declspecs,
68642fb6
UD
8886 initialized,
8887 (type_quals & TYPE_QUAL_CONST) != 0,
9e259dd1 8888 ctype ? ctype : in_namespace);
3db45ab5 8889 bad_specifiers (decl, "variable", virtualp,
e2537f2c 8890 memfn_quals != TYPE_UNQUALIFIED,
8d08fdba
MS
8891 inlinep, friendp, raises != NULL_TREE);
8892
8893 if (ctype)
8894 {
f0e01782 8895 DECL_CONTEXT (decl) = ctype;
8d08fdba
MS
8896 if (staticp == 1)
8897 {
0cbd7506
MS
8898 pedwarn ("%<static%> may not be used when defining "
8899 "(as opposed to declaring) a static data member");
8900 staticp = 0;
1ff3c076 8901 storage_class = sc_none;
8d08fdba 8902 }
1ff3c076 8903 if (storage_class == sc_register && TREE_STATIC (decl))
b7484fbe 8904 {
2d01edd7 8905 error ("static member %qD declared %<register%>", decl);
1ff3c076 8906 storage_class = sc_none;
b7484fbe 8907 }
1ff3c076 8908 if (storage_class == sc_extern && pedantic)
8d08fdba 8909 {
0cbd7506
MS
8910 pedwarn ("cannot explicitly declare member %q#D to have "
8911 "extern linkage",
8912 decl);
1ff3c076 8913 storage_class = sc_none;
8d08fdba
MS
8914 }
8915 }
8916 }
8917
8d08fdba
MS
8918 /* Record `register' declaration for warnings on &
8919 and in case doing stupid register allocation. */
8920
1ff3c076 8921 if (storage_class == sc_register)
8d08fdba 8922 DECL_REGISTER (decl) = 1;
1ff3c076 8923 else if (storage_class == sc_extern)
8926095f 8924 DECL_THIS_EXTERN (decl) = 1;
1ff3c076 8925 else if (storage_class == sc_static)
faae18ab
MS
8926 DECL_THIS_STATIC (decl) = 1;
8927
adecb3f4
MM
8928 /* Record constancy and volatility. There's no need to do this
8929 when processing a template; we'll do this for the instantiated
8930 declaration based on the type of DECL. */
8931 if (!processing_template_decl)
9804209d 8932 cp_apply_type_quals_to_decl (type_quals, decl);
8d08fdba 8933
1891dec4
DM
8934 if (set_no_warning)
8935 TREE_NO_WARNING (decl) = 1;
8936
8d08fdba
MS
8937 return decl;
8938 }
8939}
8940\f
f181d4ae
MM
8941/* Subroutine of start_function. Ensure that each of the parameter
8942 types (as listed in PARMS) is complete, as is required for a
8943 function definition. */
e92cc029 8944
8d08fdba 8945static void
11f6b451 8946require_complete_types_for_parms (tree parms)
8d08fdba 8947{
07c88314 8948 for (; parms; parms = TREE_CHAIN (parms))
8d08fdba 8949 {
c9387915
GB
8950 if (dependent_type_p (TREE_TYPE (parms)))
8951 continue;
dff1b563
VR
8952 if (!VOID_TYPE_P (TREE_TYPE (parms))
8953 && complete_type_or_else (TREE_TYPE (parms), parms))
753225c1 8954 {
7523dc31 8955 relayout_decl (parms);
753225c1
JM
8956 DECL_ARG_TYPE (parms) = type_passed_as (TREE_TYPE (parms));
8957 }
dff1b563
VR
8958 else
8959 /* grokparms or complete_type_or_else will have already issued
8960 an error. */
8961 TREE_TYPE (parms) = error_mark_node;
8d08fdba
MS
8962 }
8963}
8964
838dfd8a 8965/* Returns nonzero if T is a local variable. */
297e73d8 8966
46e8c075 8967int
11f6b451 8968local_variable_p (tree t)
297e73d8 8969{
68642fb6 8970 if ((TREE_CODE (t) == VAR_DECL
297e73d8
MM
8971 /* A VAR_DECL with a context that is a _TYPE is a static data
8972 member. */
8973 && !TYPE_P (CP_DECL_CONTEXT (t))
8974 /* Any other non-local variable must be at namespace scope. */
46e8c075 8975 && !DECL_NAMESPACE_SCOPE_P (t))
297e73d8 8976 || (TREE_CODE (t) == PARM_DECL))
46e8c075 8977 return 1;
297e73d8 8978
46e8c075
MM
8979 return 0;
8980}
8981
838dfd8a 8982/* Returns nonzero if T is an automatic local variable or a label.
46e8c075
MM
8983 (These are the declarations that need to be remapped when the code
8984 containing them is duplicated.) */
8985
8986int
11f6b451 8987nonstatic_local_decl_p (tree t)
46e8c075
MM
8988{
8989 return ((local_variable_p (t) && !TREE_STATIC (t))
8990 || TREE_CODE (t) == LABEL_DECL
8991 || TREE_CODE (t) == RESULT_DECL);
8992}
8993
8994/* Like local_variable_p, but suitable for use as a tree-walking
8995 function. */
8996
8997static tree
44de5aeb
RK
8998local_variable_p_walkfn (tree *tp, int *walk_subtrees,
8999 void *data ATTRIBUTE_UNUSED)
46e8c075 9000{
44de5aeb
RK
9001 if (local_variable_p (*tp) && !DECL_ARTIFICIAL (*tp))
9002 return *tp;
9003 else if (TYPE_P (*tp))
9004 *walk_subtrees = 0;
9005
9006 return NULL_TREE;
297e73d8
MM
9007}
9008
44de5aeb 9009
297e73d8 9010/* Check that ARG, which is a default-argument expression for a
0e339752 9011 parameter DECL, is valid. Returns ARG, or ERROR_MARK_NODE, if
297e73d8
MM
9012 something goes wrong. DECL may also be a _TYPE node, rather than a
9013 DECL, if there is no DECL available. */
9014
9015tree
11f6b451 9016check_default_argument (tree decl, tree arg)
297e73d8
MM
9017{
9018 tree var;
9019 tree decl_type;
9020
9021 if (TREE_CODE (arg) == DEFAULT_ARG)
9022 /* We get a DEFAULT_ARG when looking at an in-class declaration
9023 with a default argument. Ignore the argument for now; we'll
9024 deal with it after the class is complete. */
9025 return arg;
9026
297e73d8
MM
9027 if (TYPE_P (decl))
9028 {
9029 decl_type = decl;
9030 decl = NULL_TREE;
9031 }
9032 else
9033 decl_type = TREE_TYPE (decl);
9034
68642fb6 9035 if (arg == error_mark_node
297e73d8
MM
9036 || decl == error_mark_node
9037 || TREE_TYPE (arg) == error_mark_node
9038 || decl_type == error_mark_node)
9039 /* Something already went wrong. There's no need to check
9040 further. */
9041 return error_mark_node;
9042
9043 /* [dcl.fct.default]
68642fb6 9044
297e73d8
MM
9045 A default argument expression is implicitly converted to the
9046 parameter type. */
9047 if (!TREE_TYPE (arg)
30f86ec3 9048 || !can_convert_arg (decl_type, TREE_TYPE (arg), arg, LOOKUP_NORMAL))
297e73d8
MM
9049 {
9050 if (decl)
2d01edd7 9051 error ("default argument for %q#D has type %qT",
0cbd7506 9052 decl, TREE_TYPE (arg));
297e73d8 9053 else
2d01edd7 9054 error ("default argument for parameter of type %qT has type %qT",
0cbd7506 9055 decl_type, TREE_TYPE (arg));
297e73d8
MM
9056
9057 return error_mark_node;
9058 }
9059
9060 /* [dcl.fct.default]
9061
9062 Local variables shall not be used in default argument
68642fb6 9063 expressions.
297e73d8
MM
9064
9065 The keyword `this' shall not be used in a default argument of a
9066 member function. */
5362b086 9067 var = walk_tree_without_duplicates (&arg, local_variable_p_walkfn,
ee94fce6 9068 NULL);
297e73d8
MM
9069 if (var)
9070 {
2d01edd7 9071 error ("default argument %qE uses local variable %qD", arg, var);
297e73d8
MM
9072 return error_mark_node;
9073 }
9074
9075 /* All is well. */
9076 return arg;
9077}
9078
8d08fdba
MS
9079/* Decode the list of parameter types for a function type.
9080 Given the list of things declared inside the parens,
9081 return a list of types.
9082
058b15c1
MM
9083 If this parameter does not end with an ellipsis, we append
9084 void_list_node.
5cce22b6 9085
4546865e 9086 *PARMS is set to the chain of PARM_DECLs created. */
8d08fdba
MS
9087
9088static tree
62d1db17 9089grokparms (cp_parameter_declarator *first_parm, tree *parms)
8d08fdba
MS
9090{
9091 tree result = NULL_TREE;
9092 tree decls = NULL_TREE;
058b15c1 9093 int ellipsis = !first_parm || first_parm->ellipsis_p;
62d1db17 9094 cp_parameter_declarator *parm;
5cce22b6 9095 int any_error = 0;
0d83bf5a 9096 struct pointer_set_t *unique_decls = pointer_set_create ();
8d08fdba 9097
058b15c1 9098 for (parm = first_parm; parm != NULL; parm = parm->next)
8d08fdba 9099 {
0657c69c 9100 tree type = NULL_TREE;
058b15c1 9101 tree init = parm->default_argument;
62d1db17 9102 tree attrs;
058b15c1 9103 tree decl;
8d08fdba 9104
058b15c1 9105 if (parm == no_parameters)
0cbd7506 9106 break;
8d08fdba 9107
62d1db17
MM
9108 attrs = parm->decl_specifiers.attributes;
9109 parm->decl_specifiers.attributes = NULL_TREE;
9110 decl = grokdeclarator (parm->declarator, &parm->decl_specifiers,
98884b26 9111 PARM, init != NULL_TREE, &attrs);
5cce22b6 9112 if (! decl || TREE_TYPE (decl) == error_mark_node)
0cbd7506 9113 continue;
d363e7bf 9114
98884b26
JM
9115 if (attrs)
9116 cplus_decl_attributes (&decl, attrs, 0);
9117
5cce22b6
NS
9118 type = TREE_TYPE (decl);
9119 if (VOID_TYPE_P (type))
0cbd7506
MS
9120 {
9121 if (same_type_p (type, void_type_node)
e7e53192 9122 && DECL_SELF_REFERENCE_P (type)
0cbd7506
MS
9123 && !DECL_NAME (decl) && !result && !parm->next && !ellipsis)
9124 /* this is a parmlist of `(void)', which is ok. */
9125 break;
9126 cxx_incomplete_type_error (decl, type);
0657c69c
MM
9127 /* It's not a good idea to actually create parameters of
9128 type `void'; other parts of the compiler assume that a
9129 void type terminates the parameter list. */
04f3dc2b 9130 type = error_mark_node;
0657c69c 9131 TREE_TYPE (decl) = error_mark_node;
0cbd7506 9132 }
8d08fdba 9133
d363e7bf 9134 if (type != error_mark_node)
04f3dc2b
MM
9135 {
9136 /* Top-level qualifiers on the parameters are
9137 ignored for function types. */
79a1a736 9138 type = cp_build_qualified_type (type, 0);
04f3dc2b
MM
9139 if (TREE_CODE (type) == METHOD_TYPE)
9140 {
2d01edd7 9141 error ("parameter %qD invalidly declared method type", decl);
04f3dc2b
MM
9142 type = build_pointer_type (type);
9143 TREE_TYPE (decl) = type;
9144 }
04f3dc2b 9145 else if (abstract_virtuals_error (decl, type))
a1c65f9f 9146 any_error = 1; /* Seems like a good idea. */
04f3dc2b
MM
9147 else if (POINTER_TYPE_P (type))
9148 {
9149 /* [dcl.fct]/6, parameter types cannot contain pointers
9150 (references) to arrays of unknown bound. */
98979fe0
NS
9151 tree t = TREE_TYPE (type);
9152 int ptr = TYPE_PTR_P (type);
9153
0cbd7506
MS
9154 while (1)
9155 {
9156 if (TYPE_PTR_P (t))
9157 ptr = 1;
9158 else if (TREE_CODE (t) != ARRAY_TYPE)
9159 break;
9160 else if (!TYPE_DOMAIN (t))
9161 break;
9162 t = TREE_TYPE (t);
9163 }
04f3dc2b 9164 if (TREE_CODE (t) == ARRAY_TYPE)
2d01edd7 9165 error ("parameter %qD includes %s to array of unknown "
0cbd7506
MS
9166 "bound %qT",
9167 decl, ptr ? "pointer" : "reference", t);
04f3dc2b
MM
9168 }
9169
c3ee4651 9170 if (any_error)
04f3dc2b 9171 init = NULL_TREE;
c3ee4651
NS
9172 else if (init && !processing_template_decl)
9173 init = check_default_argument (decl, init);
04f3dc2b 9174 }
8d08fdba 9175
5d80a306
DG
9176 if (TREE_CODE (decl) == PARM_DECL
9177 && FUNCTION_PARAMETER_PACK_P (decl)
9178 && parm->next)
9179 error ("parameter packs must be at the end of the parameter list");
9180
0d83bf5a
SB
9181 if (DECL_NAME (decl))
9182 {
9183 if (pointer_set_contains (unique_decls, DECL_NAME (decl)))
631ee856 9184 error ("multiple parameters named %qE", DECL_NAME (decl));
0d83bf5a
SB
9185 else
9186 pointer_set_insert (unique_decls, DECL_NAME (decl));
9187 }
9188
5cce22b6
NS
9189 TREE_CHAIN (decl) = decls;
9190 decls = decl;
0657c69c 9191 result = tree_cons (init, type, result);
8d08fdba 9192 }
5cce22b6
NS
9193 decls = nreverse (decls);
9194 result = nreverse (result);
9195 if (!ellipsis)
9196 result = chainon (result, void_list_node);
4546865e 9197 *parms = decls;
8d08fdba 9198
0d83bf5a 9199 pointer_set_destroy (unique_decls);
8d08fdba
MS
9200 return result;
9201}
42976354 9202
8d08fdba 9203\f
271e6f02
NS
9204/* D is a constructor or overloaded `operator='.
9205
9206 Let T be the class in which D is declared. Then, this function
9207 returns:
9208
9209 -1 if D's is an ill-formed constructor or copy assignment operator
9210 whose first parameter is of type `T'.
9211 0 if D is not a copy constructor or copy assignment
9212 operator.
9213 1 if D is a copy constructor or copy assignment operator whose
9214 first parameter is a reference to const qualified T.
9215 2 if D is a copy constructor or copy assignment operator whose
9216 first parameter is a reference to non-const qualified T.
9217
9218 This function can be used as a predicate. Positive values indicate
838dfd8a 9219 a copy constructor and nonzero values indicate a copy assignment
4f1c5b7d
MM
9220 operator. */
9221
c11b6f21 9222int
11f6b451 9223copy_fn_p (tree d)
c11b6f21 9224{
271e6f02
NS
9225 tree args;
9226 tree arg_type;
9227 int result = 1;
caf93cb0 9228
398cd199 9229 gcc_assert (DECL_FUNCTION_MEMBER_P (d));
4f1c5b7d 9230
7137605e
MM
9231 if (TREE_CODE (d) == TEMPLATE_DECL
9232 || (DECL_TEMPLATE_INFO (d)
9233 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (d))))
271e6f02
NS
9234 /* Instantiations of template member functions are never copy
9235 functions. Note that member functions of templated classes are
9236 represented as template functions internally, and we must
9237 accept those as copy functions. */
9238 return 0;
caf93cb0 9239
271e6f02
NS
9240 args = FUNCTION_FIRST_USER_PARMTYPE (d);
9241 if (!args)
4f1c5b7d
MM
9242 return 0;
9243
271e6f02 9244 arg_type = TREE_VALUE (args);
139a78c7
VR
9245 if (arg_type == error_mark_node)
9246 return 0;
271e6f02
NS
9247
9248 if (TYPE_MAIN_VARIANT (arg_type) == DECL_CONTEXT (d))
9249 {
9250 /* Pass by value copy assignment operator. */
9251 result = -1;
9252 }
9253 else if (TREE_CODE (arg_type) == REFERENCE_TYPE
8af2fec4 9254 && !TYPE_REF_IS_RVALUE (arg_type)
271e6f02
NS
9255 && TYPE_MAIN_VARIANT (TREE_TYPE (arg_type)) == DECL_CONTEXT (d))
9256 {
9257 if (CP_TYPE_CONST_P (TREE_TYPE (arg_type)))
9258 result = 2;
9259 }
9260 else
9261 return 0;
caf93cb0 9262
271e6f02
NS
9263 args = TREE_CHAIN (args);
9264
9265 if (args && args != void_list_node && !TREE_PURPOSE (args))
9266 /* There are more non-optional args. */
9267 return 0;
9268
9269 return result;
9270}
9271
8af2fec4
RY
9272/* D is a constructor or overloaded `operator='.
9273
9274 Let T be the class in which D is declared. Then, this function
9275 returns true when D is a move constructor or move assignment
9276 operator, false otherwise. */
9277
9278bool
9279move_fn_p (tree d)
9280{
9281 tree args;
9282 tree arg_type;
9283 bool result = false;
9284
9285 gcc_assert (DECL_FUNCTION_MEMBER_P (d));
9286
c1ae8be5
SM
9287 if (cxx_dialect == cxx98)
9288 /* There are no move constructors if we are in C++98 mode. */
8af2fec4
RY
9289 return false;
9290
9291 if (TREE_CODE (d) == TEMPLATE_DECL
9292 || (DECL_TEMPLATE_INFO (d)
9293 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (d))))
9294 /* Instantiations of template member functions are never copy
9295 functions. Note that member functions of templated classes are
9296 represented as template functions internally, and we must
9297 accept those as copy functions. */
9298 return 0;
9299
9300 args = FUNCTION_FIRST_USER_PARMTYPE (d);
9301 if (!args)
9302 return 0;
9303
9304 arg_type = TREE_VALUE (args);
9305 if (arg_type == error_mark_node)
9306 return 0;
9307
9308 if (TREE_CODE (arg_type) == REFERENCE_TYPE
9309 && TYPE_REF_IS_RVALUE (arg_type)
9310 && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (arg_type)),
9311 DECL_CONTEXT (d)))
9312 result = true;
9313
9314 args = TREE_CHAIN (args);
9315
9316 if (args && args != void_list_node && !TREE_PURPOSE (args))
9317 /* There are more non-optional args. */
9318 return false;
9319
9320 return result;
9321}
9322
271e6f02
NS
9323/* Remember any special properties of member function DECL. */
9324
11f6b451 9325void grok_special_member_properties (tree decl)
271e6f02 9326{
7137605e
MM
9327 tree class_type;
9328
6d69fe27 9329 if (!DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
7137605e
MM
9330 return;
9331
9332 class_type = DECL_CONTEXT (decl);
9333 if (DECL_CONSTRUCTOR_P (decl))
271e6f02
NS
9334 {
9335 int ctor = copy_fn_p (decl);
caf93cb0 9336
7137605e
MM
9337 TYPE_HAS_CONSTRUCTOR (class_type) = 1;
9338
271e6f02
NS
9339 if (ctor > 0)
9340 {
9341 /* [class.copy]
caf93cb0 9342
0cbd7506
MS
9343 A non-template constructor for class X is a copy
9344 constructor if its first parameter is of type X&, const
9345 X&, volatile X& or const volatile X&, and either there
9346 are no other parameters or else all other parameters have
9347 default arguments. */
7137605e 9348 TYPE_HAS_INIT_REF (class_type) = 1;
271e6f02 9349 if (ctor > 1)
7137605e 9350 TYPE_HAS_CONST_INIT_REF (class_type) = 1;
271e6f02
NS
9351 }
9352 else if (sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (decl)))
7137605e 9353 TYPE_HAS_DEFAULT_CONSTRUCTOR (class_type) = 1;
271e6f02
NS
9354 }
9355 else if (DECL_OVERLOADED_OPERATOR_P (decl) == NOP_EXPR)
9356 {
9357 /* [class.copy]
caf93cb0 9358
0cbd7506
MS
9359 A non-template assignment operator for class X is a copy
9360 assignment operator if its parameter is of type X, X&, const
9361 X&, volatile X& or const volatile X&. */
caf93cb0 9362
271e6f02 9363 int assop = copy_fn_p (decl);
caf93cb0 9364
271e6f02
NS
9365 if (assop)
9366 {
7137605e 9367 TYPE_HAS_ASSIGN_REF (class_type) = 1;
271e6f02 9368 if (assop != 1)
7137605e 9369 TYPE_HAS_CONST_ASSIGN_REF (class_type) = 1;
271e6f02
NS
9370 }
9371 }
c11b6f21
MS
9372}
9373
271e6f02
NS
9374/* Check a constructor DECL has the correct form. Complains
9375 if the class has a constructor of the form X(X). */
e92cc029 9376
a0a33927 9377int
11f6b451 9378grok_ctor_properties (tree ctype, tree decl)
8d08fdba 9379{
271e6f02
NS
9380 int ctor_parm = copy_fn_p (decl);
9381
9382 if (ctor_parm < 0)
9383 {
9384 /* [class.copy]
caf93cb0 9385
0cbd7506
MS
9386 A declaration of a constructor for a class X is ill-formed if
9387 its first parameter is of type (optionally cv-qualified) X
9388 and either there are no other parameters or else all other
9389 parameters have default arguments.
9390
9391 We *don't* complain about member template instantiations that
9392 have this form, though; they can occur as we try to decide
9393 what constructor to use during overload resolution. Since
9394 overload resolution will never prefer such a constructor to
9395 the non-template copy constructor (which is either explicitly
9396 or implicitly defined), there's no need to worry about their
9397 existence. Theoretically, they should never even be
9398 instantiated, but that's hard to forestall. */
2d01edd7 9399 error ("invalid constructor; you probably meant %<%T (const %T&)%>",
0b41abe6 9400 ctype, ctype);
0b41abe6 9401 return 0;
8d08fdba 9402 }
caf93cb0 9403
a0a33927 9404 return 1;
8d08fdba
MS
9405}
9406
596ea4e5 9407/* An operator with this code is unary, but can also be binary. */
e92cc029 9408
a28e3c7f 9409static int
11f6b451 9410ambi_op_p (enum tree_code code)
8d08fdba 9411{
596ea4e5
AS
9412 return (code == INDIRECT_REF
9413 || code == ADDR_EXPR
392e3d51 9414 || code == UNARY_PLUS_EXPR
596ea4e5
AS
9415 || code == NEGATE_EXPR
9416 || code == PREINCREMENT_EXPR
9417 || code == PREDECREMENT_EXPR);
8d08fdba
MS
9418}
9419
9420/* An operator with this name can only be unary. */
e92cc029 9421
a28e3c7f 9422static int
11f6b451 9423unary_op_p (enum tree_code code)
8d08fdba 9424{
596ea4e5
AS
9425 return (code == TRUTH_NOT_EXPR
9426 || code == BIT_NOT_EXPR
9427 || code == COMPONENT_REF
9428 || code == TYPE_EXPR);
8d08fdba
MS
9429}
9430
f2a79152 9431/* DECL is a declaration for an overloaded operator. If COMPLAIN is true,
4b0d3cbe 9432 errors are issued for invalid declarations. */
e92cc029 9433
398cd199 9434bool
7e45bd18 9435grok_op_properties (tree decl, bool complain)
8d08fdba
MS
9436{
9437 tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
596ea4e5 9438 tree argtype;
8d08fdba
MS
9439 int methodp = (TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE);
9440 tree name = DECL_NAME (decl);
596ea4e5
AS
9441 enum tree_code operator_code;
9442 int arity;
64844139 9443 bool ellipsis_p;
7e45bd18 9444 tree class_type;
596ea4e5 9445
64844139 9446 /* Count the number of arguments and check for ellipsis. */
596ea4e5
AS
9447 for (argtype = argtypes, arity = 0;
9448 argtype && argtype != void_list_node;
9449 argtype = TREE_CHAIN (argtype))
9450 ++arity;
64844139 9451 ellipsis_p = !argtype;
8d08fdba 9452
7e45bd18
MM
9453 class_type = DECL_CONTEXT (decl);
9454 if (class_type && !CLASS_TYPE_P (class_type))
9455 class_type = NULL_TREE;
8d08fdba 9456
596ea4e5
AS
9457 if (DECL_CONV_FN_P (decl))
9458 operator_code = TYPE_EXPR;
9459 else
9460 do
9461 {
0c918ce5
MM
9462#define DEF_OPERATOR(NAME, CODE, MANGLING, ARITY, ASSN_P) \
9463 if (ansi_opname (CODE) == name) \
9464 { \
75ac8dec 9465 operator_code = (CODE); \
0c918ce5
MM
9466 break; \
9467 } \
9468 else if (ansi_assopname (CODE) == name) \
9469 { \
75ac8dec 9470 operator_code = (CODE); \
0c918ce5
MM
9471 DECL_ASSIGNMENT_OPERATOR_P (decl) = 1; \
9472 break; \
596ea4e5
AS
9473 }
9474
9475#include "operators.def"
9476#undef DEF_OPERATOR
9477
8dc2b103 9478 gcc_unreachable ();
596ea4e5
AS
9479 }
9480 while (0);
50bc768d 9481 gcc_assert (operator_code != LAST_CPLUS_TREE_CODE);
596ea4e5
AS
9482 SET_OVERLOADED_OPERATOR_CODE (decl, operator_code);
9483
7e45bd18
MM
9484 if (class_type)
9485 switch (operator_code)
9486 {
9487 case NEW_EXPR:
9488 TYPE_HAS_NEW_OPERATOR (class_type) = 1;
9489 break;
5362b086 9490
7e45bd18
MM
9491 case DELETE_EXPR:
9492 TYPE_GETS_DELETE (class_type) |= 1;
9493 break;
5362b086 9494
7e45bd18
MM
9495 case VEC_NEW_EXPR:
9496 TYPE_HAS_ARRAY_NEW_OPERATOR (class_type) = 1;
9497 break;
5362b086 9498
7e45bd18
MM
9499 case VEC_DELETE_EXPR:
9500 TYPE_GETS_DELETE (class_type) |= 2;
9501 break;
596ea4e5 9502
7e45bd18
MM
9503 default:
9504 break;
9505 }
596ea4e5 9506
3143d517
GB
9507 /* [basic.std.dynamic.allocation]/1:
9508
9509 A program is ill-formed if an allocation function is declared
9510 in a namespace scope other than global scope or declared static
9511 in global scope.
9512
9513 The same also holds true for deallocation functions. */
9514 if (operator_code == NEW_EXPR || operator_code == VEC_NEW_EXPR
9515 || operator_code == DELETE_EXPR || operator_code == VEC_DELETE_EXPR)
9516 {
9517 if (DECL_NAMESPACE_SCOPE_P (decl))
9518 {
9519 if (CP_DECL_CONTEXT (decl) != global_namespace)
398cd199
VR
9520 {
9521 error ("%qD may not be declared within a namespace", decl);
9522 return false;
9523 }
3143d517 9524 else if (!TREE_PUBLIC (decl))
398cd199
VR
9525 {
9526 error ("%qD may not be declared as static", decl);
9527 return false;
9528 }
3143d517
GB
9529 }
9530 }
9531
596ea4e5 9532 if (operator_code == NEW_EXPR || operator_code == VEC_NEW_EXPR)
4546865e 9533 TREE_TYPE (decl) = coerce_new_type (TREE_TYPE (decl));
596ea4e5 9534 else if (operator_code == DELETE_EXPR || operator_code == VEC_DELETE_EXPR)
4546865e 9535 TREE_TYPE (decl) = coerce_delete_type (TREE_TYPE (decl));
8d08fdba
MS
9536 else
9537 {
9538 /* An operator function must either be a non-static member function
9539 or have at least one parameter of a class, a reference to a class,
9540 an enumeration, or a reference to an enumeration. 13.4.0.6 */
700f8a87 9541 if (! methodp || DECL_STATIC_FUNCTION_P (decl))
8d08fdba 9542 {
596ea4e5
AS
9543 if (operator_code == TYPE_EXPR
9544 || operator_code == CALL_EXPR
9545 || operator_code == COMPONENT_REF
9546 || operator_code == ARRAY_REF
9547 || operator_code == NOP_EXPR)
f2a79152
PC
9548 {
9549 error ("%qD must be a nonstatic member function", decl);
398cd199 9550 return false;
f2a79152 9551 }
8d08fdba
MS
9552 else
9553 {
4b0d3cbe 9554 tree p;
8d08fdba 9555
700f8a87 9556 if (DECL_STATIC_FUNCTION_P (decl))
f2a79152
PC
9557 {
9558 error ("%qD must be either a non-static member "
9559 "function or a non-member function", decl);
398cd199 9560 return false;
f2a79152 9561 }
700f8a87 9562
4b0d3cbe
MM
9563 for (p = argtypes; p && p != void_list_node; p = TREE_CHAIN (p))
9564 {
9565 tree arg = non_reference (TREE_VALUE (p));
85990800 9566 if (arg == error_mark_node)
398cd199 9567 return false;
85990800 9568
4b0d3cbe
MM
9569 /* IS_AGGR_TYPE, rather than CLASS_TYPE_P, is used
9570 because these checks are performed even on
9571 template functions. */
9572 if (IS_AGGR_TYPE (arg) || TREE_CODE (arg) == ENUMERAL_TYPE)
9573 break;
9574 }
9575
9576 if (!p || p == void_list_node)
9577 {
398cd199
VR
9578 if (complain)
9579 error ("%qD must have an argument of class or "
9580 "enumerated type", decl);
9581 return false;
4b0d3cbe 9582 }
8d08fdba
MS
9583 }
9584 }
68642fb6 9585
4b0d3cbe
MM
9586 /* There are no restrictions on the arguments to an overloaded
9587 "operator ()". */
596ea4e5 9588 if (operator_code == CALL_EXPR)
398cd199 9589 return true;
8d08fdba 9590
7e45bd18 9591 /* Warn about conversion operators that will never be used. */
c8094d83 9592 if (IDENTIFIER_TYPENAME_P (name)
7e45bd18
MM
9593 && ! DECL_TEMPLATE_INFO (decl)
9594 && warn_conversion
9595 /* Warn only declaring the function; there is no need to
9596 warn again about out-of-class definitions. */
9597 && class_type == current_class_type)
a0a33927
MS
9598 {
9599 tree t = TREE_TYPE (name);
7e45bd18
MM
9600 int ref = (TREE_CODE (t) == REFERENCE_TYPE);
9601 const char *what = 0;
5362b086 9602
7e45bd18
MM
9603 if (ref)
9604 t = TYPE_MAIN_VARIANT (TREE_TYPE (t));
a0a33927 9605
7e45bd18
MM
9606 if (TREE_CODE (t) == VOID_TYPE)
9607 what = "void";
9608 else if (class_type)
9609 {
9610 if (t == class_type)
a0a33927 9611 what = "the same type";
9a3b49ac 9612 /* Don't force t to be complete here. */
a0a33927 9613 else if (IS_AGGR_TYPE (t)
d0f062fb 9614 && COMPLETE_TYPE_P (t)
7e45bd18 9615 && DERIVED_FROM_P (t, class_type))
a0a33927 9616 what = "a base class";
a0a33927 9617 }
7e45bd18
MM
9618
9619 if (what)
b323323f 9620 warning (OPT_Wconversion, "conversion to %s%s will never use a type "
7e45bd18
MM
9621 "conversion operator",
9622 ref ? "a reference to " : "", what);
a0a33927 9623 }
64844139 9624
271e6f02 9625 if (operator_code == COND_EXPR)
8d08fdba
MS
9626 {
9627 /* 13.4.0.3 */
33bd39a2 9628 error ("ISO C++ prohibits overloading operator ?:");
398cd199 9629 return false;
68642fb6 9630 }
64844139 9631 else if (ellipsis_p)
398cd199
VR
9632 {
9633 error ("%qD must not have variable number of arguments", decl);
9634 return false;
9635 }
596ea4e5 9636 else if (ambi_op_p (operator_code))
8d08fdba 9637 {
596ea4e5
AS
9638 if (arity == 1)
9639 /* We pick the one-argument operator codes by default, so
9640 we don't have to change anything. */
9641 ;
9642 else if (arity == 2)
8d08fdba 9643 {
596ea4e5
AS
9644 /* If we thought this was a unary operator, we now know
9645 it to be a binary operator. */
9646 switch (operator_code)
9647 {
9648 case INDIRECT_REF:
9649 operator_code = MULT_EXPR;
9650 break;
9651
9652 case ADDR_EXPR:
9653 operator_code = BIT_AND_EXPR;
9654 break;
9655
392e3d51 9656 case UNARY_PLUS_EXPR:
596ea4e5
AS
9657 operator_code = PLUS_EXPR;
9658 break;
9659
9660 case NEGATE_EXPR:
9661 operator_code = MINUS_EXPR;
9662 break;
9663
9664 case PREINCREMENT_EXPR:
9665 operator_code = POSTINCREMENT_EXPR;
9666 break;
9667
9668 case PREDECREMENT_EXPR:
655dc6ee 9669 operator_code = POSTDECREMENT_EXPR;
596ea4e5
AS
9670 break;
9671
9672 default:
8dc2b103 9673 gcc_unreachable ();
596ea4e5
AS
9674 }
9675
9676 SET_OVERLOADED_OPERATOR_CODE (decl, operator_code);
9677
9678 if ((operator_code == POSTINCREMENT_EXPR
9679 || operator_code == POSTDECREMENT_EXPR)
5156628f 9680 && ! processing_template_decl
007e5fea 9681 && ! same_type_p (TREE_VALUE (TREE_CHAIN (argtypes)), integer_type_node))
8d08fdba
MS
9682 {
9683 if (methodp)
2d01edd7 9684 error ("postfix %qD must take %<int%> as its argument",
398cd199 9685 decl);
8d08fdba 9686 else
398cd199
VR
9687 error ("postfix %qD must take %<int%> as its second "
9688 "argument", decl);
9689 return false;
8d08fdba
MS
9690 }
9691 }
9692 else
9693 {
9694 if (methodp)
2d01edd7 9695 error ("%qD must take either zero or one argument", decl);
8d08fdba 9696 else
2d01edd7 9697 error ("%qD must take either one or two arguments", decl);
398cd199 9698 return false;
8d08fdba 9699 }
824b9a4c
MS
9700
9701 /* More Effective C++ rule 6. */
eb448459 9702 if (warn_ecpp
596ea4e5
AS
9703 && (operator_code == POSTINCREMENT_EXPR
9704 || operator_code == POSTDECREMENT_EXPR
9705 || operator_code == PREINCREMENT_EXPR
9706 || operator_code == PREDECREMENT_EXPR))
824b9a4c
MS
9707 {
9708 tree arg = TREE_VALUE (argtypes);
9709 tree ret = TREE_TYPE (TREE_TYPE (decl));
9710 if (methodp || TREE_CODE (arg) == REFERENCE_TYPE)
9711 arg = TREE_TYPE (arg);
9712 arg = TYPE_MAIN_VARIANT (arg);
596ea4e5
AS
9713 if (operator_code == PREINCREMENT_EXPR
9714 || operator_code == PREDECREMENT_EXPR)
824b9a4c
MS
9715 {
9716 if (TREE_CODE (ret) != REFERENCE_TYPE
3bfdc719
MM
9717 || !same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (ret)),
9718 arg))
b323323f 9719 warning (OPT_Weffc__, "prefix %qD should return %qT", decl,
0cbd7506 9720 build_reference_type (arg));
824b9a4c
MS
9721 }
9722 else
9723 {
3bfdc719 9724 if (!same_type_p (TYPE_MAIN_VARIANT (ret), arg))
b323323f 9725 warning (OPT_Weffc__, "postfix %qD should return %qT", decl, arg);
824b9a4c
MS
9726 }
9727 }
8d08fdba 9728 }
596ea4e5 9729 else if (unary_op_p (operator_code))
8d08fdba 9730 {
596ea4e5 9731 if (arity != 1)
8d08fdba
MS
9732 {
9733 if (methodp)
2d01edd7 9734 error ("%qD must take %<void%>", decl);
8d08fdba 9735 else
2d01edd7 9736 error ("%qD must take exactly one argument", decl);
398cd199 9737 return false;
8d08fdba
MS
9738 }
9739 }
596ea4e5 9740 else /* if (binary_op_p (operator_code)) */
8d08fdba 9741 {
596ea4e5 9742 if (arity != 2)
8d08fdba
MS
9743 {
9744 if (methodp)
2d01edd7 9745 error ("%qD must take exactly one argument", decl);
8d08fdba 9746 else
2d01edd7 9747 error ("%qD must take exactly two arguments", decl);
398cd199 9748 return false;
8d08fdba 9749 }
824b9a4c
MS
9750
9751 /* More Effective C++ rule 7. */
eb448459 9752 if (warn_ecpp
596ea4e5
AS
9753 && (operator_code == TRUTH_ANDIF_EXPR
9754 || operator_code == TRUTH_ORIF_EXPR
9755 || operator_code == COMPOUND_EXPR))
b323323f 9756 warning (OPT_Weffc__, "user-defined %qD always evaluates both arguments",
0cbd7506 9757 decl);
824b9a4c
MS
9758 }
9759
9760 /* Effective C++ rule 23. */
eb448459 9761 if (warn_ecpp
596ea4e5 9762 && arity == 2
4bd7c270 9763 && !DECL_ASSIGNMENT_OPERATOR_P (decl)
596ea4e5
AS
9764 && (operator_code == PLUS_EXPR
9765 || operator_code == MINUS_EXPR
9766 || operator_code == TRUNC_DIV_EXPR
4bd7c270
NS
9767 || operator_code == MULT_EXPR
9768 || operator_code == TRUNC_MOD_EXPR)
824b9a4c 9769 && TREE_CODE (TREE_TYPE (TREE_TYPE (decl))) == REFERENCE_TYPE)
b323323f 9770 warning (OPT_Weffc__, "%qD should return by value", decl);
8d08fdba 9771
271e6f02 9772 /* [over.oper]/8 */
34332678 9773 for (; argtypes && argtypes != void_list_node;
0cbd7506
MS
9774 argtypes = TREE_CHAIN (argtypes))
9775 if (TREE_PURPOSE (argtypes))
9776 {
9777 TREE_PURPOSE (argtypes) = NULL_TREE;
9778 if (operator_code == POSTINCREMENT_EXPR
596ea4e5 9779 || operator_code == POSTDECREMENT_EXPR)
0cbd7506
MS
9780 {
9781 if (pedantic)
9782 pedwarn ("%qD cannot have default arguments", decl);
9783 }
9784 else
398cd199
VR
9785 {
9786 error ("%qD cannot have default arguments", decl);
9787 return false;
9788 }
0cbd7506 9789 }
8d08fdba 9790 }
398cd199 9791 return true;
8d08fdba
MS
9792}
9793\f
0c88d886
MM
9794/* Return a string giving the keyword associate with CODE. */
9795
d8e178a0 9796static const char *
11f6b451 9797tag_name (enum tag_types code)
094fe153
JM
9798{
9799 switch (code)
9800 {
9801 case record_type:
9802 return "struct";
9803 case class_type:
9804 return "class";
9805 case union_type:
0c88d886 9806 return "union";
094fe153
JM
9807 case enum_type:
9808 return "enum";
0c88d886
MM
9809 case typename_type:
9810 return "typename";
094fe153 9811 default:
8dc2b103 9812 gcc_unreachable ();
094fe153
JM
9813 }
9814}
9815
befcd99b 9816/* Name lookup in an elaborated-type-specifier (after the keyword
4b0d3cbe 9817 indicated by TAG_CODE) has found the TYPE_DECL DECL. If the
befcd99b 9818 elaborated-type-specifier is invalid, issue a diagnostic and return
4b0d3cbe 9819 error_mark_node; otherwise, return the *_TYPE to which it referred.
cbd63935 9820 If ALLOW_TEMPLATE_P is true, TYPE may be a class template. */
befcd99b 9821
560ad596 9822tree
befcd99b 9823check_elaborated_type_specifier (enum tag_types tag_code,
4b0d3cbe 9824 tree decl,
cbd63935 9825 bool allow_template_p)
befcd99b 9826{
4b0d3cbe 9827 tree type;
befcd99b 9828
4b0d3cbe
MM
9829 /* In the case of:
9830
9831 struct S { struct S *p; };
9832
9833 name lookup will find the TYPE_DECL for the implicit "S::S"
9834 typedef. Adjust for that here. */
9835 if (DECL_SELF_REFERENCE_P (decl))
9836 decl = TYPE_NAME (TREE_TYPE (decl));
9837
9838 type = TREE_TYPE (decl);
9839
461c6fce
KL
9840 /* Check TEMPLATE_TYPE_PARM first because DECL_IMPLICIT_TYPEDEF_P
9841 is false for this case as well. */
9842 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
9843 {
9844 error ("using template type parameter %qT after %qs",
9845 type, tag_name (tag_code));
9846 return error_mark_node;
9847 }
caf93cb0 9848 /* [dcl.type.elab]
4b0d3cbe
MM
9849
9850 If the identifier resolves to a typedef-name or a template
9851 type-parameter, the elaborated-type-specifier is ill-formed.
9852
9853 In other words, the only legitimate declaration to use in the
9854 elaborated type specifier is the implicit typedef created when
9855 the type is declared. */
0c88d886
MM
9856 else if (!DECL_IMPLICIT_TYPEDEF_P (decl)
9857 && tag_code != typename_type)
befcd99b 9858 {
2d01edd7 9859 error ("using typedef-name %qD after %qs", decl, tag_name (tag_code));
dee15844 9860 error ("%q+D has a previous declaration here", decl);
4b0d3cbe 9861 return error_mark_node;
befcd99b 9862 }
cbd63935
KL
9863 else if (TREE_CODE (type) != RECORD_TYPE
9864 && TREE_CODE (type) != UNION_TYPE
0c88d886
MM
9865 && tag_code != enum_type
9866 && tag_code != typename_type)
cbd63935 9867 {
2d01edd7 9868 error ("%qT referred to as %qs", type, tag_name (tag_code));
dee15844 9869 error ("%q+T has a previous declaration here", type);
4b0d3cbe 9870 return error_mark_node;
cbd63935
KL
9871 }
9872 else if (TREE_CODE (type) != ENUMERAL_TYPE
4fe8db68 9873 && tag_code == enum_type)
cbd63935 9874 {
2d01edd7 9875 error ("%qT referred to as enum", type);
dee15844 9876 error ("%q+T has a previous declaration here", type);
4b0d3cbe 9877 return error_mark_node;
cbd63935
KL
9878 }
9879 else if (!allow_template_p
9880 && TREE_CODE (type) == RECORD_TYPE
9881 && CLASSTYPE_IS_TEMPLATE (type))
9882 {
9883 /* If a class template appears as elaborated type specifier
9884 without a template header such as:
9885
9886 template <class T> class C {};
9887 void f(class C); // No template header here
9888
9889 then the required template argument is missing. */
2d01edd7 9890 error ("template argument required for %<%s %T%>",
cbd63935
KL
9891 tag_name (tag_code),
9892 DECL_NAME (CLASSTYPE_TI_TEMPLATE (type)));
4b0d3cbe 9893 return error_mark_node;
cbd63935 9894 }
befcd99b 9895
4b0d3cbe 9896 return type;
befcd99b
MM
9897}
9898
461c6fce 9899/* Lookup NAME in elaborate type specifier in scope according to
29ef83de 9900 SCOPE and issue diagnostics if necessary.
461c6fce
KL
9901 Return *_TYPE node upon success, NULL_TREE when the NAME is not
9902 found, and ERROR_MARK_NODE for type error. */
9903
9904static tree
9905lookup_and_check_tag (enum tag_types tag_code, tree name,
29ef83de 9906 tag_scope scope, bool template_header_p)
461c6fce
KL
9907{
9908 tree t;
9909 tree decl;
29ef83de 9910 if (scope == ts_global)
bd3d082e
KL
9911 {
9912 /* First try ordinary name lookup, ignoring hidden class name
9913 injected via friend declaration. */
10e6657a 9914 decl = lookup_name_prefer_type (name, 2);
bd3d082e
KL
9915 /* If that fails, the name will be placed in the smallest
9916 non-class, non-function-prototype scope according to 3.3.1/5.
9917 We may already have a hidden name declared as friend in this
105d72c5 9918 scope. So lookup again but not ignoring hidden names.
bd3d082e
KL
9919 If we find one, that name will be made visible rather than
9920 creating a new tag. */
9921 if (!decl)
9922 decl = lookup_type_scope (name, ts_within_enclosing_non_class);
9923 }
461c6fce 9924 else
29ef83de 9925 decl = lookup_type_scope (name, scope);
461c6fce
KL
9926
9927 if (decl && DECL_CLASS_TEMPLATE_P (decl))
9928 decl = DECL_TEMPLATE_RESULT (decl);
9929
9930 if (decl && TREE_CODE (decl) == TYPE_DECL)
9931 {
4104f0f4
KL
9932 /* Look for invalid nested type:
9933 class C {
9934 class C {};
9935 }; */
9936 if (scope == ts_current && DECL_SELF_REFERENCE_P (decl))
9937 {
9938 error ("%qD has the same name as the class in which it is "
9939 "declared",
9940 decl);
9941 return error_mark_node;
9942 }
9943
461c6fce
KL
9944 /* Two cases we need to consider when deciding if a class
9945 template is allowed as an elaborated type specifier:
9946 1. It is a self reference to its own class.
9947 2. It comes with a template header.
9948
9949 For example:
9950
9951 template <class T> class C {
9952 class C *c1; // DECL_SELF_REFERENCE_P is true
9953 class D;
9954 };
9955 template <class U> class C; // template_header_p is true
9956 template <class T> class C<T>::D {
9957 class C *c2; // DECL_SELF_REFERENCE_P is true
9958 }; */
9959
9960 t = check_elaborated_type_specifier (tag_code,
9961 decl,
9962 template_header_p
9963 | DECL_SELF_REFERENCE_P (decl));
9964 return t;
9965 }
af92ab36
OW
9966 else if (decl && TREE_CODE (decl) == TREE_LIST)
9967 {
9968 error ("reference to %qD is ambiguous", name);
9969 print_candidates (decl);
9970 return error_mark_node;
9971 }
461c6fce
KL
9972 else
9973 return NULL_TREE;
9974}
9975
cbd63935 9976/* Get the struct, enum or union (TAG_CODE says which) with tag NAME.
8d08fdba
MS
9977 Define the tag as a forward-reference if it is not defined.
9978
cbd63935 9979 If a declaration is given, process it here, and report an error if
38b305d0 9980 multiple declarations are not identical.
8d08fdba 9981
29ef83de 9982 SCOPE is TS_CURRENT when this is also a definition. Only look in
8d08fdba 9983 the current frame for the name (since C++ allows new names in any
29ef83de
KL
9984 scope.) It is TS_WITHIN_ENCLOSING_NON_CLASS if this is a friend
9985 declaration. Only look beginning from the current scope outward up
9986 till the nearest non-class scope. Otherwise it is TS_GLOBAL.
cbd63935
KL
9987
9988 TEMPLATE_HEADER_P is true when this declaration is preceded by
9989 a set of template parameters. */
8d08fdba 9990
8d08fdba 9991tree
38b305d0 9992xref_tag (enum tag_types tag_code, tree name,
29ef83de 9993 tag_scope scope, bool template_header_p)
8d08fdba 9994{
8d08fdba 9995 enum tree_code code;
926ce8bd 9996 tree t;
25aab5d0 9997 tree context = NULL_TREE;
dc8263bc 9998
22ffcc6f 9999 timevar_push (TV_NAME_LOOKUP);
cbd63935 10000
50bc768d 10001 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
cbd63935 10002
8d08fdba
MS
10003 switch (tag_code)
10004 {
10005 case record_type:
10006 case class_type:
8d08fdba 10007 code = RECORD_TYPE;
8d08fdba
MS
10008 break;
10009 case union_type:
10010 code = UNION_TYPE;
8d08fdba
MS
10011 break;
10012 case enum_type:
10013 code = ENUMERAL_TYPE;
10014 break;
10015 default:
8dc2b103 10016 gcc_unreachable ();
8d08fdba
MS
10017 }
10018
461c6fce
KL
10019 /* In case of anonymous name, xref_tag is only called to
10020 make type node and push name. Name lookup is not required. */
10021 if (ANON_AGGRNAME_P (name))
10022 t = NULL_TREE;
8d08fdba 10023 else
461c6fce 10024 t = lookup_and_check_tag (tag_code, name,
29ef83de 10025 scope, template_header_p);
cbd63935 10026
461c6fce
KL
10027 if (t == error_mark_node)
10028 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
68642fb6 10029
29ef83de 10030 if (scope != ts_current && t && current_class_type
461c6fce
KL
10031 && template_class_depth (current_class_type)
10032 && template_header_p)
10033 {
29ef83de 10034 /* Since SCOPE is not TS_CURRENT, we are not looking at a
461c6fce
KL
10035 definition of this tag. Since, in addition, we are currently
10036 processing a (member) template declaration of a template
10037 class, we must be very careful; consider:
25aab5d0 10038
461c6fce
KL
10039 template <class X>
10040 struct S1
25aab5d0 10041
461c6fce
KL
10042 template <class U>
10043 struct S2
10044 { template <class V>
10045 friend struct S1; };
25aab5d0 10046
461c6fce
KL
10047 Here, the S2::S1 declaration should not be confused with the
10048 outer declaration. In particular, the inner version should
10049 have a template parameter of level 2, not level 1. This
10050 would be particularly important if the member declaration
10051 were instead:
25aab5d0 10052
461c6fce 10053 template <class V = U> friend struct S1;
25aab5d0 10054
461c6fce
KL
10055 say, when we should tsubst into `U' when instantiating
10056 S2. On the other hand, when presented with:
25aab5d0 10057
461c6fce
KL
10058 template <class T>
10059 struct S1 {
10060 template <class U>
10061 struct S2 {};
10062 template <class U>
10063 friend struct S2;
10064 };
25aab5d0 10065
461c6fce
KL
10066 we must find the inner binding eventually. We
10067 accomplish this by making sure that the new type we
10068 create to represent this declaration has the right
10069 TYPE_CONTEXT. */
10070 context = TYPE_CONTEXT (t);
10071 t = NULL_TREE;
8d08fdba
MS
10072 }
10073
cbd63935 10074 if (! t)
8d08fdba
MS
10075 {
10076 /* If no such tag is yet defined, create a forward-reference node
10077 and record it as the "definition".
10078 When a real declaration of this type is found,
10079 the forward-reference will be altered into a real type. */
8d08fdba
MS
10080 if (code == ENUMERAL_TYPE)
10081 {
2d01edd7 10082 error ("use of enum %q#D without previous declaration", name);
30fc3df7 10083 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
8d08fdba 10084 }
8d08fdba
MS
10085 else
10086 {
cbd63935
KL
10087 t = make_aggr_type (code);
10088 TYPE_CONTEXT (t) = context;
bd3d082e 10089 t = pushtag (name, t, scope);
8d08fdba
MS
10090 }
10091 }
10092 else
10093 {
29ef83de 10094 if (template_header_p && IS_AGGR_TYPE (t))
60feef2c
LM
10095 {
10096 if (!redeclare_class_template (t, current_template_parms))
10097 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
10098 }
caf93cb0 10099 else if (!processing_template_decl
9ce1594a
MM
10100 && CLASS_TYPE_P (t)
10101 && CLASSTYPE_IS_TEMPLATE (t))
10102 {
2d01edd7 10103 error ("redeclaration of %qT as a non-template", t);
b646edb8
VR
10104 error ("previous declaration %q+D", t);
10105 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
9ce1594a 10106 }
bd3d082e
KL
10107
10108 /* Make injected friend class visible. */
10109 if (scope != ts_within_enclosing_non_class
10110 && hidden_name_p (TYPE_NAME (t)))
10111 {
10112 DECL_ANTICIPATED (TYPE_NAME (t)) = 0;
10113 DECL_FRIEND_P (TYPE_NAME (t)) = 0;
10114
10115 if (TYPE_TEMPLATE_INFO (t))
10116 {
10117 DECL_ANTICIPATED (TYPE_TI_TEMPLATE (t)) = 0;
10118 DECL_FRIEND_P (TYPE_TI_TEMPLATE (t)) = 0;
10119 }
0cbd7506 10120 }
8d08fdba
MS
10121 }
10122
cbd63935 10123 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
8d08fdba 10124}
8ccc31eb 10125
fc378698 10126tree
29ef83de 10127xref_tag_from_type (tree old, tree id, tag_scope scope)
fc378698 10128{
88e5899c 10129 enum tag_types tag_kind;
fc378698
MS
10130
10131 if (TREE_CODE (old) == RECORD_TYPE)
88e5899c 10132 tag_kind = (CLASSTYPE_DECLARED_CLASS (old) ? class_type : record_type);
fc378698 10133 else
88e5899c 10134 tag_kind = union_type;
fc378698
MS
10135
10136 if (id == NULL_TREE)
10137 id = TYPE_IDENTIFIER (old);
10138
29ef83de 10139 return xref_tag (tag_kind, id, scope, false);
fc378698
MS
10140}
10141
48b45647
NS
10142/* Create the binfo hierarchy for REF with (possibly NULL) base list
10143 BASE_LIST. For each element on BASE_LIST the TREE_PURPOSE is an
10144 access_* node, and the TREE_VALUE is the type of the base-class.
72f8fc59
LM
10145 Non-NULL TREE_TYPE indicates virtual inheritance.
10146
bd6f7848 10147 Returns true if the binfo hierarchy was successfully created,
72f8fc59 10148 false if an error was detected. */
3fd71a52 10149
72f8fc59 10150bool
dbbf88d1 10151xref_basetypes (tree ref, tree base_list)
8ccc31eb 10152{
8fbc5ae7 10153 tree *basep;
fa743e8c 10154 tree binfo, base_binfo;
03fd3f84 10155 unsigned max_vbases = 0; /* Maximum direct & indirect virtual bases. */
77880ae4 10156 unsigned max_bases = 0; /* Maximum direct bases. */
8fbc5ae7 10157 int i;
48b45647
NS
10158 tree default_access;
10159 tree igo_prev; /* Track Inheritance Graph Order. */
a5d7c4a3 10160
bef89e9e 10161 if (ref == error_mark_node)
72f8fc59 10162 return false;
bef89e9e 10163
48b45647
NS
10164 /* The base of a derived class is private by default, all others are
10165 public. */
10166 default_access = (TREE_CODE (ref) == RECORD_TYPE
10167 && CLASSTYPE_DECLARED_CLASS (ref)
10168 ? access_private_node : access_public_node);
da15dae6 10169
d6479fe7
MM
10170 /* First, make sure that any templates in base-classes are
10171 instantiated. This ensures that if we call ourselves recursively
10172 we do not get confused about which classes are marked and which
10173 are not. */
caf93cb0
EC
10174 basep = &base_list;
10175 while (*basep)
8fbc5ae7
MM
10176 {
10177 tree basetype = TREE_VALUE (*basep);
caf93cb0 10178
8fbc5ae7
MM
10179 if (!(processing_template_decl && uses_template_parms (basetype))
10180 && !complete_type_or_else (basetype, NULL))
dbbf88d1 10181 /* An incomplete type. Remove it from the list. */
8fbc5ae7
MM
10182 *basep = TREE_CHAIN (*basep);
10183 else
48b45647
NS
10184 {
10185 max_bases++;
10186 if (TREE_TYPE (*basep))
10187 max_vbases++;
10188 if (CLASS_TYPE_P (basetype))
10189 max_vbases += VEC_length (tree, CLASSTYPE_VBASECLASSES (basetype));
10190 basep = &TREE_CHAIN (*basep);
10191 }
8fbc5ae7 10192 }
d6479fe7 10193
3543e114 10194 TYPE_MARKED_P (ref) = 1;
48b45647 10195
cad7e87b
NS
10196 /* The binfo slot should be empty, unless this is an (ill-formed)
10197 redefinition. */
50bc768d
NS
10198 gcc_assert (!TYPE_BINFO (ref) || TYPE_SIZE (ref));
10199 gcc_assert (TYPE_MAIN_VARIANT (ref) == ref);
caf93cb0 10200
fa743e8c 10201 binfo = make_tree_binfo (max_bases);
caf93cb0 10202
48b45647
NS
10203 TYPE_BINFO (ref) = binfo;
10204 BINFO_OFFSET (binfo) = size_zero_node;
10205 BINFO_TYPE (binfo) = ref;
caf93cb0 10206
48b45647
NS
10207 if (max_bases)
10208 {
d4e6fecb 10209 BINFO_BASE_ACCESSES (binfo) = VEC_alloc (tree, gc, max_bases);
48b45647
NS
10210 /* An aggregate cannot have baseclasses. */
10211 CLASSTYPE_NON_AGGREGATE (ref) = 1;
caf93cb0 10212
48b45647 10213 if (TREE_CODE (ref) == UNION_TYPE)
72f8fc59
LM
10214 {
10215 error ("derived union %qT invalid", ref);
10216 return false;
10217 }
48b45647 10218 }
caf93cb0 10219
48b45647 10220 if (max_bases > 1)
dbbf88d1 10221 {
48b45647 10222 if (TYPE_FOR_JAVA (ref))
72f8fc59
LM
10223 {
10224 error ("Java class %qT cannot have multiple bases", ref);
10225 return false;
10226 }
48b45647 10227 }
caf93cb0 10228
48b45647
NS
10229 if (max_vbases)
10230 {
d4e6fecb 10231 CLASSTYPE_VBASECLASSES (ref) = VEC_alloc (tree, gc, max_vbases);
caf93cb0 10232
48b45647 10233 if (TYPE_FOR_JAVA (ref))
72f8fc59
LM
10234 {
10235 error ("Java class %qT cannot have virtual bases", ref);
10236 return false;
10237 }
48b45647 10238 }
cad7e87b 10239
48b45647
NS
10240 for (igo_prev = binfo; base_list; base_list = TREE_CHAIN (base_list))
10241 {
10242 tree access = TREE_PURPOSE (base_list);
10243 int via_virtual = TREE_TYPE (base_list) != NULL_TREE;
10244 tree basetype = TREE_VALUE (base_list);
caf93cb0 10245
48b45647
NS
10246 if (access == access_default_node)
10247 access = default_access;
caf93cb0 10248
5d80a306
DG
10249 if (PACK_EXPANSION_P (basetype))
10250 basetype = PACK_EXPANSION_PATTERN (basetype);
48b45647
NS
10251 if (TREE_CODE (basetype) == TYPE_DECL)
10252 basetype = TREE_TYPE (basetype);
10253 if (TREE_CODE (basetype) != RECORD_TYPE
10254 && TREE_CODE (basetype) != TYPENAME_TYPE
10255 && TREE_CODE (basetype) != TEMPLATE_TYPE_PARM
10256 && TREE_CODE (basetype) != BOUND_TEMPLATE_TEMPLATE_PARM)
10257 {
2d01edd7 10258 error ("base type %qT fails to be a struct or class type",
48b45647 10259 basetype);
72f8fc59 10260 return false;
8ccc31eb 10261 }
caf93cb0 10262
48b45647
NS
10263 if (TYPE_FOR_JAVA (basetype) && (current_lang_depth () == 0))
10264 TYPE_FOR_JAVA (ref) = 1;
10265
fa743e8c 10266 base_binfo = NULL_TREE;
48b45647 10267 if (CLASS_TYPE_P (basetype) && !dependent_type_p (basetype))
dbbf88d1 10268 {
48b45647 10269 base_binfo = TYPE_BINFO (basetype);
77880ae4 10270 /* The original basetype could have been a typedef'd type. */
48b45647 10271 basetype = BINFO_TYPE (base_binfo);
caf93cb0 10272
48b45647
NS
10273 /* Inherit flags from the base. */
10274 TYPE_HAS_NEW_OPERATOR (ref)
10275 |= TYPE_HAS_NEW_OPERATOR (basetype);
10276 TYPE_HAS_ARRAY_NEW_OPERATOR (ref)
10277 |= TYPE_HAS_ARRAY_NEW_OPERATOR (basetype);
10278 TYPE_GETS_DELETE (ref) |= TYPE_GETS_DELETE (basetype);
48b45647 10279 TYPE_HAS_CONVERSION (ref) |= TYPE_HAS_CONVERSION (basetype);
3543e114
NS
10280 CLASSTYPE_DIAMOND_SHAPED_P (ref)
10281 |= CLASSTYPE_DIAMOND_SHAPED_P (basetype);
10282 CLASSTYPE_REPEATED_BASE_P (ref)
10283 |= CLASSTYPE_REPEATED_BASE_P (basetype);
8fbc5ae7 10284 }
c8094d83 10285
98d6e9af
NS
10286 /* We must do this test after we've seen through a typedef
10287 type. */
10288 if (TYPE_MARKED_P (basetype))
10289 {
10290 if (basetype == ref)
2d01edd7 10291 error ("recursive type %qT undefined", basetype);
98d6e9af 10292 else
2d01edd7 10293 error ("duplicate base type %qT invalid", basetype);
72f8fc59 10294 return false;
98d6e9af 10295 }
5d80a306
DG
10296
10297 if (PACK_EXPANSION_P (TREE_VALUE (base_list)))
10298 /* Regenerate the pack expansion for the bases. */
10299 basetype = make_pack_expansion (basetype);
10300
98d6e9af 10301 TYPE_MARKED_P (basetype) = 1;
caf93cb0 10302
48b45647
NS
10303 base_binfo = copy_binfo (base_binfo, basetype, ref,
10304 &igo_prev, via_virtual);
10305 if (!BINFO_INHERITANCE_CHAIN (base_binfo))
10306 BINFO_INHERITANCE_CHAIN (base_binfo) = binfo;
f5c28a15 10307
fa743e8c 10308 BINFO_BASE_APPEND (binfo, base_binfo);
63d1c7b3 10309 BINFO_BASE_ACCESS_APPEND (binfo, access);
cad7e87b
NS
10310 }
10311
3543e114
NS
10312 if (VEC_space (tree, CLASSTYPE_VBASECLASSES (ref), 1))
10313 /* If we have space in the vbase vector, we must have shared at
10314 least one of them, and are therefore diamond shaped. */
10315 CLASSTYPE_DIAMOND_SHAPED_P (ref) = 1;
10316
dbbf88d1 10317 /* Unmark all the types. */
fa743e8c 10318 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
3543e114
NS
10319 TYPE_MARKED_P (BINFO_TYPE (base_binfo)) = 0;
10320 TYPE_MARKED_P (ref) = 0;
10321
10322 /* Now see if we have a repeated base type. */
10323 if (!CLASSTYPE_REPEATED_BASE_P (ref))
10324 {
10325 for (base_binfo = binfo; base_binfo;
10326 base_binfo = TREE_CHAIN (base_binfo))
10327 {
10328 if (TYPE_MARKED_P (BINFO_TYPE (base_binfo)))
10329 {
10330 CLASSTYPE_REPEATED_BASE_P (ref) = 1;
10331 break;
10332 }
10333 TYPE_MARKED_P (BINFO_TYPE (base_binfo)) = 1;
10334 }
10335 for (base_binfo = binfo; base_binfo;
10336 base_binfo = TREE_CHAIN (base_binfo))
10337 if (TYPE_MARKED_P (BINFO_TYPE (base_binfo)))
10338 TYPE_MARKED_P (BINFO_TYPE (base_binfo)) = 0;
10339 else
10340 break;
10341 }
72f8fc59
LM
10342
10343 return true;
8ccc31eb 10344}
68642fb6 10345
8d08fdba 10346\f
8d08fdba 10347/* Begin compiling the definition of an enumeration type.
29ef83de 10348 NAME is its name.
8d08fdba
MS
10349 Returns the type object, as yet incomplete.
10350 Also records info about it so that build_enumerator
10351 may be used to declare the individual values as they are read. */
10352
10353tree
11f6b451 10354start_enum (tree name)
8d08fdba 10355{
29ef83de
KL
10356 tree enumtype;
10357
10358 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
8d08fdba
MS
10359
10360 /* If this is the real definition for a previous forward reference,
10361 fill in the contents in the same object that used to be the
10362 forward reference. */
10363
29ef83de
KL
10364 enumtype = lookup_and_check_tag (enum_type, name,
10365 /*tag_scope=*/ts_current,
10366 /*template_header_p=*/false);
8d08fdba
MS
10367
10368 if (enumtype != NULL_TREE && TREE_CODE (enumtype) == ENUMERAL_TYPE)
30ff8252 10369 {
2d01edd7 10370 error ("multiple definition of %q#T", enumtype);
ddd2d57e 10371 error ("%Jprevious definition here", TYPE_MAIN_DECL (enumtype));
58595203
MM
10372 /* Clear out TYPE_VALUES, and start again. */
10373 TYPE_VALUES (enumtype) = NULL_TREE;
30ff8252 10374 }
8d08fdba
MS
10375 else
10376 {
29ef83de
KL
10377 /* In case of error, make a dummy enum to allow parsing to
10378 continue. */
10379 if (enumtype == error_mark_node)
10380 name = make_anon_name ();
10381
8d08fdba 10382 enumtype = make_node (ENUMERAL_TYPE);
bd3d082e 10383 enumtype = pushtag (name, enumtype, /*tag_scope=*/ts_current);
8d08fdba
MS
10384 }
10385
8d08fdba
MS
10386 return enumtype;
10387}
10388
10389/* After processing and defining all the values of an enumeration type,
10390 install their decls in the enumeration type and finish it off.
968b956a 10391 ENUMTYPE is the type object and VALUES a list of name-value pairs. */
8d08fdba 10392
968b956a 10393void
11f6b451 10394finish_enum (tree enumtype)
8d08fdba 10395{
7b6d72fc
MM
10396 tree values;
10397 tree decl;
10398 tree value;
968b956a
MM
10399 tree minnode;
10400 tree maxnode;
10401 tree t;
10402 bool unsignedp;
f6af9a15 10403 bool use_short_enum;
968b956a 10404 int lowprec;
caf93cb0 10405 int highprec;
968b956a 10406 int precision;
7b6d72fc 10407 integer_type_kind itk;
ad96995b 10408 tree underlying_type = NULL_TREE;
968b956a
MM
10409
10410 /* We built up the VALUES in reverse order. */
10411 TYPE_VALUES (enumtype) = nreverse (TYPE_VALUES (enumtype));
10412
f5d70cc0
JM
10413 /* For an enum defined in a template, just set the type of the values;
10414 all further processing is postponed until the template is
10415 instantiated. We need to set the type so that tsubst of a CONST_DECL
10416 works. */
968b956a
MM
10417 if (processing_template_decl)
10418 {
caf93cb0
EC
10419 for (values = TYPE_VALUES (enumtype);
10420 values;
7b6d72fc
MM
10421 values = TREE_CHAIN (values))
10422 TREE_TYPE (TREE_VALUE (values)) = enumtype;
5f261ba9 10423 if (at_function_scope_p ())
968b956a 10424 add_stmt (build_min (TAG_DEFN, enumtype));
968b956a
MM
10425 return;
10426 }
10427
7b6d72fc 10428 /* Determine the minimum and maximum values of the enumerators. */
968b956a 10429 if (TYPE_VALUES (enumtype))
8d08fdba 10430 {
968b956a 10431 minnode = maxnode = NULL_TREE;
5566b478 10432
caf93cb0
EC
10433 for (values = TYPE_VALUES (enumtype);
10434 values;
7b6d72fc 10435 values = TREE_CHAIN (values))
8d08fdba 10436 {
7b6d72fc 10437 decl = TREE_VALUE (values);
f5d70cc0
JM
10438
10439 /* [dcl.enum]: Following the closing brace of an enum-specifier,
10440 each enumerator has the type of its enumeration. Prior to the
10441 closing brace, the type of each enumerator is the type of its
10442 initializing value. */
10443 TREE_TYPE (decl) = enumtype;
10444
7b6d72fc
MM
10445 /* Update the minimum and maximum values, if appropriate. */
10446 value = DECL_INITIAL (decl);
3c955a04
MM
10447 if (value == error_mark_node)
10448 value = integer_zero_node;
f5d70cc0
JM
10449 /* Figure out what the minimum and maximum values of the
10450 enumerators are. */
10451 if (!minnode)
10452 minnode = maxnode = value;
10453 else if (tree_int_cst_lt (maxnode, value))
10454 maxnode = value;
10455 else if (tree_int_cst_lt (value, minnode))
10456 minnode = value;
8d08fdba
MS
10457 }
10458 }
f376e137 10459 else
7b6d72fc
MM
10460 /* [dcl.enum]
10461
10462 If the enumerator-list is empty, the underlying type is as if
10463 the enumeration had a single enumerator with value 0. */
968b956a
MM
10464 minnode = maxnode = integer_zero_node;
10465
10466 /* Compute the number of bits require to represent all values of the
10467 enumeration. We must do this before the type of MINNODE and
10468 MAXNODE are transformed, since min_precision relies on the
10469 TREE_TYPE of the value it is passed. */
10470 unsignedp = tree_int_cst_sgn (minnode) >= 0;
10471 lowprec = min_precision (minnode, unsignedp);
10472 highprec = min_precision (maxnode, unsignedp);
10473 precision = MAX (lowprec, highprec);
10474
7b6d72fc
MM
10475 /* Determine the underlying type of the enumeration.
10476
10477 [dcl.enum]
10478
10479 The underlying type of an enumeration is an integral type that
10480 can represent all the enumerator values defined in the
10481 enumeration. It is implementation-defined which integral type is
10482 used as the underlying type for an enumeration except that the
10483 underlying type shall not be larger than int unless the value of
caf93cb0 10484 an enumerator cannot fit in an int or unsigned int.
7b6d72fc
MM
10485
10486 We use "int" or an "unsigned int" as the underlying type, even if
10487 a smaller integral type would work, unless the user has
f6af9a15
MA
10488 explicitly requested that we use the smallest possible type. The
10489 user can request that for all enumerations with a command line
03fd3f84 10490 flag, or for just one enumeration with an attribute. */
f6af9a15
MA
10491
10492 use_short_enum = flag_short_enums
10493 || lookup_attribute ("packed", TYPE_ATTRIBUTES (enumtype));
10494
10495 for (itk = (use_short_enum ? itk_char : itk_int);
caf93cb0 10496 itk != itk_none;
7b6d72fc 10497 itk++)
e455bc27 10498 {
7b6d72fc
MM
10499 underlying_type = integer_types[itk];
10500 if (TYPE_PRECISION (underlying_type) >= precision
8df83eae 10501 && TYPE_UNSIGNED (underlying_type) == unsignedp)
7b6d72fc
MM
10502 break;
10503 }
10504 if (itk == itk_none)
10505 {
10506 /* DR 377
10507
10508 IF no integral type can represent all the enumerator values, the
10509 enumeration is ill-formed. */
e455bc27 10510 error ("no integral type can represent all of the enumerator values "
2d01edd7 10511 "for %qT", enumtype);
e455bc27 10512 precision = TYPE_PRECISION (long_long_integer_type_node);
7b6d72fc 10513 underlying_type = integer_types[itk_unsigned_long_long];
e455bc27
MM
10514 }
10515
caf93cb0 10516 /* Compute the minium and maximum values for the type.
8d08fdba 10517
7b6d72fc
MM
10518 [dcl.enum]
10519
10520 For an enumeration where emin is the smallest enumerator and emax
10521 is the largest, the values of the enumeration are the values of the
10522 underlying type in the range bmin to bmax, where bmin and bmax are,
10523 respectively, the smallest and largest values of the smallest bit-
10524 field that can store emin and emax. */
d0c5c9b1
RS
10525
10526 /* The middle-end currently assumes that types with TYPE_PRECISION
10527 narrower than their underlying type are suitably zero or sign
10528 extended to fill their mode. g++ doesn't make these guarantees.
10529 Until the middle-end can represent such paradoxical types, we
77880ae4 10530 set the TYPE_PRECISION to the width of the underlying type. */
d0c5c9b1
RS
10531 TYPE_PRECISION (enumtype) = TYPE_PRECISION (underlying_type);
10532
7b6d72fc
MM
10533 set_min_and_max_values_for_integral_type (enumtype, precision, unsignedp);
10534
10535 /* [dcl.enum]
caf93cb0 10536
7b6d72fc
MM
10537 The value of sizeof() applied to an enumeration type, an object
10538 of an enumeration type, or an enumerator, is the value of sizeof()
10539 applied to the underlying type. */
10540 TYPE_SIZE (enumtype) = TYPE_SIZE (underlying_type);
10541 TYPE_SIZE_UNIT (enumtype) = TYPE_SIZE_UNIT (underlying_type);
10542 TYPE_MODE (enumtype) = TYPE_MODE (underlying_type);
10543 TYPE_ALIGN (enumtype) = TYPE_ALIGN (underlying_type);
10544 TYPE_USER_ALIGN (enumtype) = TYPE_USER_ALIGN (underlying_type);
8df83eae 10545 TYPE_UNSIGNED (enumtype) = TYPE_UNSIGNED (underlying_type);
7b6d72fc
MM
10546
10547 /* Convert each of the enumerators to the type of the underlying
10548 type of the enumeration. */
10549 for (values = TYPE_VALUES (enumtype); values; values = TREE_CHAIN (values))
10550 {
3c955a04
MM
10551 location_t saved_location;
10552
7b6d72fc 10553 decl = TREE_VALUE (values);
3c955a04
MM
10554 saved_location = input_location;
10555 input_location = DECL_SOURCE_LOCATION (decl);
7b6d72fc
MM
10556 value = perform_implicit_conversion (underlying_type,
10557 DECL_INITIAL (decl));
3c955a04 10558 input_location = saved_location;
89b0433e
NS
10559
10560 /* Do not clobber shared ints. */
10561 value = copy_node (value);
c8094d83 10562
7b6d72fc
MM
10563 TREE_TYPE (value) = enumtype;
10564 DECL_INITIAL (decl) = value;
10565 TREE_VALUE (values) = value;
e455bc27 10566 }
8d08fdba 10567
968b956a
MM
10568 /* Fix up all variant types of this enum type. */
10569 for (t = TYPE_MAIN_VARIANT (enumtype); t; t = TYPE_NEXT_VARIANT (t))
10570 {
10571 TYPE_VALUES (t) = TYPE_VALUES (enumtype);
10572 TYPE_MIN_VALUE (t) = TYPE_MIN_VALUE (enumtype);
10573 TYPE_MAX_VALUE (t) = TYPE_MAX_VALUE (enumtype);
10574 TYPE_SIZE (t) = TYPE_SIZE (enumtype);
10575 TYPE_SIZE_UNIT (t) = TYPE_SIZE_UNIT (enumtype);
10576 TYPE_MODE (t) = TYPE_MODE (enumtype);
10577 TYPE_PRECISION (t) = TYPE_PRECISION (enumtype);
10578 TYPE_ALIGN (t) = TYPE_ALIGN (enumtype);
10579 TYPE_USER_ALIGN (t) = TYPE_USER_ALIGN (enumtype);
8df83eae 10580 TYPE_UNSIGNED (t) = TYPE_UNSIGNED (enumtype);
cbf882af
MM
10581 }
10582
968b956a
MM
10583 /* Finish debugging output for this type. */
10584 rest_of_type_compilation (enumtype, namespace_bindings_p ());
8d08fdba
MS
10585}
10586
079e1098 10587/* Build and install a CONST_DECL for an enumeration constant of the
58595203 10588 enumeration type ENUMTYPE whose NAME and VALUE (if any) are provided.
8d08fdba
MS
10589 Assignment of sequential values by default is handled here. */
10590
58595203 10591void
11f6b451 10592build_enumerator (tree name, tree value, tree enumtype)
8d08fdba 10593{
58595203 10594 tree decl;
e8bd800e 10595 tree context;
58595203 10596 tree type;
8d08fdba 10597
93678513
MM
10598 /* If the VALUE was erroneous, pretend it wasn't there; that will
10599 result in the enum being assigned the next value in sequence. */
10600 if (value == error_mark_node)
10601 value = NULL_TREE;
10602
8d08fdba
MS
10603 /* Remove no-op casts from the value. */
10604 if (value)
10605 STRIP_TYPE_NOPS (value);
10606
58595203
MM
10607 if (! processing_template_decl)
10608 {
10609 /* Validate and default VALUE. */
10610 if (value != NULL_TREE)
10611 {
8a784e4a 10612 value = integral_constant_value (value);
58595203
MM
10613
10614 if (TREE_CODE (value) == INTEGER_CST)
10615 {
0a72704b 10616 value = perform_integral_promotions (value);
58595203
MM
10617 constant_expression_warning (value);
10618 }
10619 else
10620 {
253e0d15 10621 error ("enumerator value for %qD is not an integer constant", name);
58595203
MM
10622 value = NULL_TREE;
10623 }
10624 }
10625
10626 /* Default based on previous value. */
6a540f3c 10627 if (value == NULL_TREE)
58595203 10628 {
58595203
MM
10629 if (TYPE_VALUES (enumtype))
10630 {
ff4eb0b5
ZW
10631 HOST_WIDE_INT hi;
10632 unsigned HOST_WIDE_INT lo;
10633 tree prev_value;
10634 bool overflowed;
10635
10636 /* The next value is the previous value plus one. We can
0cbd7506 10637 safely assume that the previous value is an INTEGER_CST.
ff4eb0b5
ZW
10638 add_double doesn't know the type of the target expression,
10639 so we must check with int_fits_type_p as well. */
58595203 10640 prev_value = DECL_INITIAL (TREE_VALUE (TYPE_VALUES (enumtype)));
ff4eb0b5
ZW
10641 overflowed = add_double (TREE_INT_CST_LOW (prev_value),
10642 TREE_INT_CST_HIGH (prev_value),
10643 1, 0, &lo, &hi);
10644 value = build_int_cst_wide (TREE_TYPE (prev_value), lo, hi);
10645 overflowed |= !int_fits_type_p (value, TREE_TYPE (prev_value));
68642fb6 10646
ff4eb0b5 10647 if (overflowed)
3c955a04
MM
10648 {
10649 error ("overflow in enumeration values at %qD", name);
10650 value = error_mark_node;
10651 }
58595203
MM
10652 }
10653 else
10654 value = integer_zero_node;
10655 }
10656
10657 /* Remove no-op casts from the value. */
f5d70cc0 10658 STRIP_TYPE_NOPS (value);
58595203 10659 }
8d08fdba 10660
8d08fdba 10661 /* C++ associates enums with global, function, or class declarations. */
58595203
MM
10662 context = current_scope ();
10663
10664 /* Build the actual enumeration constant. Note that the enumeration
10665 constants have the type of their initializers until the
10666 enumeration is complete:
10667
10668 [ dcl.enum ]
10669
10670 Following the closing brace of an enum-specifier, each enumer-
10671 ator has the type of its enumeration. Prior to the closing
10672 brace, the type of each enumerator is the type of its
10673 initializing value.
10674
10675 In finish_enum we will reset the type. Of course, if we're
a1c65f9f 10676 processing a template, there may be no value. */
58595203
MM
10677 type = value ? TREE_TYPE (value) : NULL_TREE;
10678
10679 if (context && context == current_class_type)
10680 /* This enum declaration is local to the class. We need the full
8f17b5c5 10681 lang_decl so that we can record DECL_CLASS_CONTEXT, for example. */
58595203
MM
10682 decl = build_lang_decl (CONST_DECL, name, type);
10683 else
10684 /* It's a global enum, or it's local to a function. (Note local to
e8bd800e 10685 a function could mean local to a class method. */
58595203 10686 decl = build_decl (CONST_DECL, name, type);
e8bd800e 10687
58595203 10688 DECL_CONTEXT (decl) = FROB_CONTEXT (context);
6de9cd9a
DN
10689 TREE_CONSTANT (decl) = 1;
10690 TREE_INVARIANT (decl) = 1;
10691 TREE_READONLY (decl) = 1;
58595203 10692 DECL_INITIAL (decl) = value;
e8bd800e 10693
58595203
MM
10694 if (context && context == current_class_type)
10695 /* In something like `struct S { enum E { i = 7 }; };' we put `i'
7b6d72fc
MM
10696 on the TYPE_FIELDS list for `S'. (That's so that you can say
10697 things like `S::i' later.) */
58595203
MM
10698 finish_member_declaration (decl);
10699 else
9780c24f 10700 pushdecl (decl);
58595203
MM
10701
10702 /* Add this enumeration constant to the list for this type. */
10703 TYPE_VALUES (enumtype) = tree_cons (name, decl, TYPE_VALUES (enumtype));
8d08fdba
MS
10704}
10705
8d08fdba 10706\f
a8f73d4b
MM
10707/* We're defining DECL. Make sure that it's type is OK. */
10708
10709static void
11f6b451 10710check_function_type (tree decl, tree current_function_parms)
a8f73d4b
MM
10711{
10712 tree fntype = TREE_TYPE (decl);
d0f062fb 10713 tree return_type = complete_type (TREE_TYPE (fntype));
a8f73d4b
MM
10714
10715 /* In a function definition, arg types must be complete. */
10716 require_complete_types_for_parms (current_function_parms);
10717
c9387915
GB
10718 if (dependent_type_p (return_type))
10719 return;
d0f062fb 10720 if (!COMPLETE_OR_VOID_TYPE_P (return_type))
a8f73d4b 10721 {
51b15ede 10722 tree args = TYPE_ARG_TYPES (fntype);
3db45ab5 10723
51b15ede 10724 error ("return type %q#T is incomplete", return_type);
a8f73d4b 10725
51b15ede 10726 /* Make it return void instead. */
a8f73d4b 10727 if (TREE_CODE (fntype) == METHOD_TYPE)
51b15ede
NS
10728 fntype = build_method_type_directly (TREE_TYPE (TREE_VALUE (args)),
10729 void_type_node,
10730 TREE_CHAIN (args));
a8f73d4b 10731 else
51b15ede 10732 fntype = build_function_type (void_type_node, args);
68642fb6 10733 TREE_TYPE (decl)
a8f73d4b 10734 = build_exception_variant (fntype,
51b15ede 10735 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (decl)));
a8f73d4b
MM
10736 }
10737 else
10738 abstract_virtuals_error (decl, TREE_TYPE (fntype));
10739}
10740
8d08fdba
MS
10741/* Create the FUNCTION_DECL for a function definition.
10742 DECLSPECS and DECLARATOR are the parts of the declaration;
10743 they describe the function's name and the type it returns,
10744 but twisted together in a fashion that parallels the syntax of C.
10745
a8f73d4b
MM
10746 FLAGS is a bitwise or of SF_PRE_PARSED (indicating that the
10747 DECLARATOR is really the DECL for the function we are about to
10748 process and that DECLSPECS should be ignored), SF_INCLASS_INLINE
f444e36b 10749 indicating that the function is an inline defined in-class.
68642fb6 10750
8d08fdba
MS
10751 This function creates a binding context for the function body
10752 as well as setting up the FUNCTION_DECL in current_function_decl.
10753
8d08fdba
MS
10754 For C++, we must first check whether that datum makes any sense.
10755 For example, "class A local_a(1,2);" means that variable local_a
10756 is an aggregate of type A, which should have a constructor
72c4a4ca
GK
10757 applied to it with the argument list [1, 2].
10758
10759 On entry, DECL_INITIAL (decl1) should be NULL_TREE or error_mark_node,
10760 or may be a BLOCK if the function has been defined previously
10761 in this translation unit. On exit, DECL_INITIAL (decl1) will be
10762 error_mark_node if the function has never been defined, or
10763 a BLOCK if the function has been defined somewhere. */
8d08fdba 10764
058b15c1
MM
10765void
10766start_preparsed_function (tree decl1, tree attrs, int flags)
8d08fdba 10767{
8d08fdba
MS
10768 tree ctype = NULL_TREE;
10769 tree fntype;
10770 tree restype;
8d08fdba 10771 int doing_friend = 0;
e2500fed 10772 struct cp_binding_level *bl;
f444e36b 10773 tree current_function_parms;
3e3935a9 10774 struct c_fileinfo *finfo
c533e34d 10775 = get_fileinfo (LOCATION_FILE (DECL_SOURCE_LOCATION (decl1)));
1ef0df47 10776 bool honor_interface;
8d08fdba 10777
8d08fdba 10778 /* Sanity check. */
50bc768d
NS
10779 gcc_assert (TREE_CODE (TREE_VALUE (void_list_node)) == VOID_TYPE);
10780 gcc_assert (TREE_CHAIN (void_list_node) == NULL_TREE);
8d08fdba 10781
058b15c1
MM
10782 fntype = TREE_TYPE (decl1);
10783 if (TREE_CODE (fntype) == METHOD_TYPE)
10784 ctype = TYPE_METHOD_BASETYPE (fntype);
caf93cb0 10785
058b15c1
MM
10786 /* ISO C++ 11.4/5. A friend function defined in a class is in
10787 the (lexical) scope of the class in which it is defined. */
10788 if (!ctype && DECL_FRIEND_P (decl1))
8d08fdba 10789 {
058b15c1 10790 ctype = DECL_FRIEND_CONTEXT (decl1);
caf93cb0 10791
058b15c1
MM
10792 /* CTYPE could be null here if we're dealing with a template;
10793 for example, `inline friend float foo()' inside a template
10794 will have no CTYPE set. */
10795 if (ctype && TREE_CODE (ctype) != RECORD_TYPE)
10796 ctype = NULL_TREE;
10797 else
10798 doing_friend = 1;
8d08fdba 10799 }
68642fb6 10800
97055d5c
AO
10801 if (DECL_DECLARED_INLINE_P (decl1)
10802 && lookup_attribute ("noinline", attrs))
dee15844 10803 warning (0, "inline function %q+D given attribute noinline", decl1);
97055d5c 10804
5f6eeeb3
NS
10805 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (decl1))
10806 /* This is a constructor, we must ensure that any default args
10807 introduced by this definition are propagated to the clones
10808 now. The clones are used directly in overload resolution. */
10809 adjust_clone_args (decl1);
10810
b35d4555
MM
10811 /* Sometimes we don't notice that a function is a static member, and
10812 build a METHOD_TYPE for it. Fix that up now. */
10813 if (ctype != NULL_TREE && DECL_STATIC_FUNCTION_P (decl1)
10814 && TREE_CODE (TREE_TYPE (decl1)) == METHOD_TYPE)
10815 {
3afb32a4 10816 revert_static_member_fn (decl1);
b35d4555
MM
10817 ctype = NULL_TREE;
10818 }
8d08fdba 10819
f181d4ae
MM
10820 /* Set up current_class_type, and enter the scope of the class, if
10821 appropriate. */
10822 if (ctype)
14d22dd6 10823 push_nested_class (ctype);
f181d4ae 10824 else if (DECL_STATIC_FUNCTION_P (decl1))
14d22dd6 10825 push_nested_class (DECL_CONTEXT (decl1));
f181d4ae
MM
10826
10827 /* Now that we have entered the scope of the class, we must restore
10828 the bindings for any template parameters surrounding DECL1, if it
10829 is an inline member template. (Order is important; consider the
10830 case where a template parameter has the same name as a field of
10831 the class.) It is not until after this point that
10832 PROCESSING_TEMPLATE_DECL is guaranteed to be set up correctly. */
a8f73d4b 10833 if (flags & SF_INCLASS_INLINE)
f181d4ae
MM
10834 maybe_begin_member_template_processing (decl1);
10835
56cb9733 10836 /* Effective C++ rule 15. */
9188c363 10837 if (warn_ecpp
596ea4e5 10838 && DECL_OVERLOADED_OPERATOR_P (decl1) == NOP_EXPR
9188c363 10839 && TREE_CODE (TREE_TYPE (fntype)) == VOID_TYPE)
b323323f 10840 warning (OPT_Weffc__, "%<operator=%> should return a reference to %<*this%>");
9188c363
MM
10841
10842 /* Make the init_value nonzero so pushdecl knows this is not tentative.
10843 error_mark_node is replaced below (in poplevel) with the BLOCK. */
b35d4555
MM
10844 if (!DECL_INITIAL (decl1))
10845 DECL_INITIAL (decl1) = error_mark_node;
9188c363 10846
9188c363
MM
10847 /* This function exists in static storage.
10848 (This does not mean `static' in the C sense!) */
10849 TREE_STATIC (decl1) = 1;
10850
10851 /* We must call push_template_decl after current_class_type is set
10852 up. (If we are processing inline definitions after exiting a
10853 class scope, current_class_type will be NULL_TREE until set above
10854 by push_nested_class.) */
10855 if (processing_template_decl)
11e97737
VR
10856 {
10857 /* FIXME: Handle error_mark_node more gracefully. */
10858 tree newdecl1 = push_template_decl (decl1);
10859 if (newdecl1 != error_mark_node)
0cbd7506 10860 decl1 = newdecl1;
11e97737 10861 }
9188c363 10862
f181d4ae 10863 /* We are now in the scope of the function being defined. */
8d08fdba 10864 current_function_decl = decl1;
f181d4ae 10865
5566b478
MS
10866 /* Save the parm names or decls from this function's declarator
10867 where store_parm_decls will find them. */
4546865e 10868 current_function_parms = DECL_ARGUMENTS (decl1);
8d08fdba 10869
a8f73d4b
MM
10870 /* Make sure the parameter and return types are reasonable. When
10871 you declare a function, these types can be incomplete, but they
10872 must be complete when you define the function. */
c9387915 10873 check_function_type (decl1, current_function_parms);
f181d4ae 10874
a8f73d4b
MM
10875 /* Build the return declaration for the function. */
10876 restype = TREE_TYPE (fntype);
e89a6075 10877 if (DECL_RESULT (decl1) == NULL_TREE)
a8f73d4b 10878 {
b785f485
RH
10879 tree resdecl;
10880
10881 resdecl = build_decl (RESULT_DECL, 0, TYPE_MAIN_VARIANT (restype));
10882 DECL_ARTIFICIAL (resdecl) = 1;
10883 DECL_IGNORED_P (resdecl) = 1;
10884 DECL_RESULT (decl1) = resdecl;
10885
9804209d 10886 cp_apply_type_quals_to_decl (cp_type_quals (restype), resdecl);
5566b478 10887 }
a8f73d4b 10888
a8f73d4b 10889 /* Let the user know we're compiling this function. */
ea11ca7e 10890 announce_function (decl1);
b7484fbe 10891
878cd289
MS
10892 /* Record the decl so that the function name is defined.
10893 If we already have a decl for this name, and it is a FUNCTION_DECL,
10894 use the old decl. */
a8f73d4b 10895 if (!processing_template_decl && !(flags & SF_PRE_PARSED))
878cd289 10896 {
75650646 10897 /* A specialization is not used to guide overload resolution. */
18f5be99 10898 if (!DECL_FUNCTION_MEMBER_P (decl1)
caf93cb0 10899 && !(DECL_USE_TEMPLATE (decl1) &&
18f5be99 10900 PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl1))))
b1a19c7c
MM
10901 {
10902 tree olddecl = pushdecl (decl1);
10903
10904 if (olddecl == error_mark_node)
10905 /* If something went wrong when registering the declaration,
10906 use DECL1; we have to have a FUNCTION_DECL to use when
10907 parsing the body of the function. */
10908 ;
10909 else
dfdec7a7
ILT
10910 {
10911 /* Otherwise, OLDDECL is either a previous declaration
10912 of the same function or DECL1 itself. */
10913
10914 if (warn_missing_declarations
10915 && olddecl == decl1
10916 && !DECL_MAIN_P (decl1)
10917 && TREE_PUBLIC (decl1)
10918 && !DECL_DECLARED_INLINE_P (decl1))
10919 {
10920 tree context;
10921
10922 /* Check whether DECL1 is in an anonymous
10923 namespace. */
10924 for (context = DECL_CONTEXT (decl1);
10925 context;
10926 context = DECL_CONTEXT (context))
10927 {
10928 if (TREE_CODE (context) == NAMESPACE_DECL
10929 && DECL_NAME (context) == NULL_TREE)
10930 break;
10931 }
10932
10933 if (context == NULL)
10934 warning (OPT_Wmissing_declarations,
10935 "no previous declaration for %q+D", decl1);
10936 }
10937
10938 decl1 = olddecl;
10939 }
b1a19c7c 10940 }
2c73f9f5 10941 else
b7698cf0 10942 {
a1c65f9f 10943 /* We need to set the DECL_CONTEXT. */
b7698cf0
JM
10944 if (!DECL_CONTEXT (decl1) && DECL_TEMPLATE_INFO (decl1))
10945 DECL_CONTEXT (decl1) = DECL_CONTEXT (DECL_TI_TEMPLATE (decl1));
b7698cf0 10946 }
878cd289 10947 fntype = TREE_TYPE (decl1);
2ef06beb
MM
10948
10949 /* If #pragma weak applies, mark the decl appropriately now.
10950 The pragma only applies to global functions. Because
10951 determining whether or not the #pragma applies involves
10952 computing the mangled name for the declaration, we cannot
10953 apply the pragma until after we have merged this declaration
10954 with any previous declarations; if the original declaration
10955 has a linkage specification, that specification applies to
10956 the definition as well, and may affect the mangled name. */
10957 if (!DECL_CONTEXT (decl1))
10958 maybe_apply_pragma_weak (decl1);
878cd289 10959 }
5566b478 10960
72c4a4ca 10961 /* Reset this in case the call to pushdecl changed it. */
5566b478 10962 current_function_decl = decl1;
72c4a4ca
GK
10963
10964 gcc_assert (DECL_INITIAL (decl1));
10965
10966 /* This function may already have been parsed, in which case just
10967 return; our caller will skip over the body without parsing. */
10968 if (DECL_INITIAL (decl1) != error_mark_node)
10969 return;
10970
10971 /* Initialize RTL machinery. We cannot do this until
10972 CURRENT_FUNCTION_DECL and DECL_RESULT are set up. We do this
10973 even when processing a template; this is how we get
10974 CFUN set up, and our per-function variables initialized.
10975 FIXME factor out the non-RTL stuff. */
10976 bl = current_binding_level;
10977 allocate_struct_function (decl1);
10978 current_binding_level = bl;
10979
10980 /* Even though we're inside a function body, we still don't want to
10981 call expand_expr to calculate the size of a variable-sized array.
10982 We haven't necessarily assigned RTL to all variables yet, so it's
10983 not safe to try to expand expressions involving them. */
10984 cfun->x_dont_save_pending_sizes_p = 1;
10985
10986 /* Start the statement-tree, start the tree now. */
10987 DECL_SAVED_TREE (decl1) = push_stmt_list ();
878cd289 10988
78c120b5
MM
10989 /* If we are (erroneously) defining a function that we have already
10990 defined before, wipe out what we knew before. */
e2500fed
GK
10991 if (!DECL_PENDING_INLINE_P (decl1))
10992 DECL_SAVED_FUNCTION_DATA (decl1) = NULL;
b35d4555 10993
f444e36b 10994 if (ctype && !doing_friend && !DECL_STATIC_FUNCTION_P (decl1))
b35d4555
MM
10995 {
10996 /* We know that this was set up by `grokclassfn'. We do not
10997 wait until `store_parm_decls', since evil parse errors may
10998 never get us to that point. Here we keep the consistency
10999 between `current_class_type' and `current_class_ptr'. */
11000 tree t = DECL_ARGUMENTS (decl1);
68642fb6 11001
50bc768d
NS
11002 gcc_assert (t != NULL_TREE && TREE_CODE (t) == PARM_DECL);
11003 gcc_assert (TREE_CODE (TREE_TYPE (t)) == POINTER_TYPE);
68642fb6
UD
11004
11005 cp_function_chain->x_current_class_ref
3e411c3f 11006 = build_indirect_ref (t, NULL);
b35d4555
MM
11007 cp_function_chain->x_current_class_ptr = t;
11008
018fc244
MM
11009 /* Constructors and destructors need to know whether they're "in
11010 charge" of initializing virtual base classes. */
e0fff4b3 11011 t = TREE_CHAIN (t);
454fa7a7 11012 if (DECL_HAS_IN_CHARGE_PARM_P (decl1))
e0fff4b3
JM
11013 {
11014 current_in_charge_parm = t;
11015 t = TREE_CHAIN (t);
11016 }
11017 if (DECL_HAS_VTT_PARM_P (decl1))
11018 {
8dc2b103 11019 gcc_assert (DECL_NAME (t) == vtt_parm_identifier);
e0fff4b3
JM
11020 current_vtt_parm = t;
11021 }
b35d4555
MM
11022 }
11023
1ef0df47
MM
11024 honor_interface = (!DECL_TEMPLATE_INSTANTIATION (decl1)
11025 /* Implicitly-defined methods (like the
11026 destructor for a class in which no destructor
11027 is explicitly declared) must not be defined
11028 until their definition is needed. So, we
11029 ignore interface specifications for
11030 compiler-generated functions. */
11031 && !DECL_ARTIFICIAL (decl1));
3db45ab5 11032
db5ae43f 11033 if (DECL_INTERFACE_KNOWN (decl1))
faae18ab 11034 {
4f1c5b7d 11035 tree ctx = decl_function_context (decl1);
86052cc3 11036
faae18ab
MS
11037 if (DECL_NOT_REALLY_EXTERN (decl1))
11038 DECL_EXTERNAL (decl1) = 0;
86052cc3 11039
79065db2 11040 if (ctx != NULL_TREE && DECL_DECLARED_INLINE_P (ctx)
86052cc3
JM
11041 && TREE_PUBLIC (ctx))
11042 /* This is a function in a local class in an extern inline
11043 function. */
11044 comdat_linkage (decl1);
faae18ab 11045 }
8d08fdba
MS
11046 /* If this function belongs to an interface, it is public.
11047 If it belongs to someone else's interface, it is also external.
1f901793 11048 This only affects inlines and template instantiations. */
1ef0df47 11049 else if (!finfo->interface_unknown && honor_interface)
8d08fdba 11050 {
caf93cb0 11051 if (DECL_DECLARED_INLINE_P (decl1)
79065db2 11052 || DECL_TEMPLATE_INSTANTIATION (decl1)
5156628f 11053 || processing_template_decl)
1f901793
JM
11054 {
11055 DECL_EXTERNAL (decl1)
5d709b00 11056 = (finfo->interface_only
caf93cb0 11057 || (DECL_DECLARED_INLINE_P (decl1)
79065db2 11058 && ! flag_implement_inlines
9c73ec84 11059 && !DECL_VINDEX (decl1)));
1f901793
JM
11060
11061 /* For WIN32 we also want to put these in linkonce sections. */
11062 maybe_make_one_only (decl1);
11063 }
db5ae43f 11064 else
893de33c 11065 DECL_EXTERNAL (decl1) = 0;
db5ae43f 11066 DECL_INTERFACE_KNOWN (decl1) = 1;
4684cd27 11067 /* If this function is in an interface implemented in this file,
3b426391 11068 make sure that the back end knows to emit this function
4684cd27
MM
11069 here. */
11070 if (!DECL_EXTERNAL (decl1))
11071 mark_needed (decl1);
8d08fdba 11072 }
5d709b00 11073 else if (finfo->interface_unknown && finfo->interface_only
1ef0df47 11074 && honor_interface)
c16c47fb
JM
11075 {
11076 /* If MULTIPLE_SYMBOL_SPACES is defined and we saw a #pragma
5d709b00
ZW
11077 interface, we will have both finfo->interface_unknown and
11078 finfo->interface_only set. In that case, we don't want to
11079 use the normal heuristics because someone will supply a
11080 #pragma implementation elsewhere, and deducing it here would
11081 produce a conflict. */
c16c47fb
JM
11082 comdat_linkage (decl1);
11083 DECL_EXTERNAL (decl1) = 0;
11084 DECL_INTERFACE_KNOWN (decl1) = 1;
11085 DECL_DEFER_OUTPUT (decl1) = 1;
11086 }
8d08fdba 11087 else
a0a33927
MS
11088 {
11089 /* This is a definition, not a reference.
b7484fbe
MS
11090 So clear DECL_EXTERNAL. */
11091 DECL_EXTERNAL (decl1) = 0;
faae18ab 11092
caf93cb0 11093 if ((DECL_DECLARED_INLINE_P (decl1)
79065db2 11094 || DECL_TEMPLATE_INSTANTIATION (decl1))
5566b478
MS
11095 && ! DECL_INTERFACE_KNOWN (decl1)
11096 /* Don't try to defer nested functions for now. */
4f1c5b7d 11097 && ! decl_function_context (decl1))
878cd289
MS
11098 DECL_DEFER_OUTPUT (decl1) = 1;
11099 else
893de33c 11100 DECL_INTERFACE_KNOWN (decl1) = 1;
db5ae43f 11101 }
a9aedbc2 11102
0ed5edac
JM
11103 /* Determine the ELF visibility attribute for the function. We must not
11104 do this before calling "pushdecl", as we must allow "duplicate_decls"
11105 to merge any attributes appropriately. We also need to wait until
11106 linkage is set. */
11107 if (!DECL_CLONED_FUNCTION_P (decl1))
11108 determine_visibility (decl1);
11109
ac20c67a 11110 begin_scope (sk_function_parms, decl1);
8d08fdba 11111
5566b478
MS
11112 ++function_depth;
11113
44d10c10
PB
11114 if (DECL_DESTRUCTOR_P (decl1)
11115 || (DECL_CONSTRUCTOR_P (decl1)
11116 && targetm.cxx.cdtor_returns_this ()))
46e8c075 11117 {
44d10c10
PB
11118 cdtor_label = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE);
11119 DECL_CONTEXT (cdtor_label) = current_function_decl;
46e8c075 11120 }
8d08fdba 11121
0ba8a114 11122 start_fname_decls ();
caf93cb0 11123
f444e36b 11124 store_parm_decls (current_function_parms);
058b15c1
MM
11125}
11126
11127
11128/* Like start_preparsed_function, except that instead of a
11129 FUNCTION_DECL, this function takes DECLSPECS and DECLARATOR.
11130
11131 Returns 1 on success. If the DECLARATOR is not suitable for a function
11132 (it defines a datum instead), we return 0, which tells
11133 yyparse to report a parse error. */
11134
11135int
caf93cb0 11136start_function (cp_decl_specifier_seq *declspecs,
62d1db17 11137 const cp_declarator *declarator,
058b15c1
MM
11138 tree attrs)
11139{
11140 tree decl1;
11141
058b15c1
MM
11142 decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, 1, &attrs);
11143 /* If the declarator is not suitable for a function definition,
11144 cause a syntax error. */
11145 if (decl1 == NULL_TREE || TREE_CODE (decl1) != FUNCTION_DECL)
11146 return 0;
caf93cb0 11147
058b15c1 11148 if (DECL_MAIN_P (decl1))
2cfe82fe
ZW
11149 /* main must return int. grokfndecl should have corrected it
11150 (and issued a diagnostic) if the user got it wrong. */
11151 gcc_assert (same_type_p (TREE_TYPE (TREE_TYPE (decl1)),
11152 integer_type_node));
058b15c1
MM
11153
11154 start_preparsed_function (decl1, attrs, /*flags=*/SF_DEFAULT);
f444e36b 11155
8d08fdba
MS
11156 return 1;
11157}
11158\f
68d04c67
MM
11159/* Returns true iff an EH_SPEC_BLOCK should be created in the body of
11160 FN. */
11161
11162static bool
11163use_eh_spec_block (tree fn)
11164{
11165 return (flag_exceptions && flag_enforce_eh_specs
11166 && !processing_template_decl
11167 && TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn))
11168 /* We insert the EH_SPEC_BLOCK only in the original
11169 function; then, it is copied automatically to the
11170 clones. */
11171 && !DECL_CLONED_FUNCTION_P (fn)
11172 /* Implicitly-generated constructors and destructors have
11173 exception specifications. However, those specifications
11174 are the union of the possible exceptions specified by the
11175 constructors/destructors for bases and members, so no
11176 unallowed exception will ever reach this function. By
11177 not creating the EH_SPEC_BLOCK we save a little memory,
11178 and we avoid spurious warnings about unreachable
11179 code. */
11180 && !DECL_ARTIFICIAL (fn));
11181}
11182
8d08fdba
MS
11183/* Store the parameter declarations into the current function declaration.
11184 This is called after parsing the parameter declarations, before
11185 digesting the body of the function.
11186
11187 Also install to binding contour return value identifier, if any. */
11188
f444e36b 11189static void
11f6b451 11190store_parm_decls (tree current_function_parms)
8d08fdba 11191{
926ce8bd
KH
11192 tree fndecl = current_function_decl;
11193 tree parm;
8d08fdba 11194
8d08fdba
MS
11195 /* This is a chain of any other decls that came in among the parm
11196 declarations. If a parm is declared with enum {foo, bar} x;
11197 then CONST_DECLs for foo and bar are put here. */
11198 tree nonparms = NULL_TREE;
11199
b35d4555 11200 if (current_function_parms)
8d08fdba
MS
11201 {
11202 /* This case is when the function was defined with an ANSI prototype.
11203 The parms already have decls, so we need not do anything here
11204 except record them as in effect
11205 and complain if any redundant old-style parm decls were written. */
11206
b35d4555
MM
11207 tree specparms = current_function_parms;
11208 tree next;
11209
f444e36b 11210 /* Must clear this because it might contain TYPE_DECLs declared
b35d4555 11211 at class level. */
00e8de68 11212 current_binding_level->names = NULL;
8d08fdba 11213
f444e36b 11214 /* If we're doing semantic analysis, then we'll call pushdecl
b35d4555
MM
11215 for each of these. We must do them in reverse order so that
11216 they end in the correct forward order. */
f444e36b 11217 specparms = nreverse (specparms);
5566b478 11218
b35d4555 11219 for (parm = specparms; parm; parm = next)
8d08fdba
MS
11220 {
11221 next = TREE_CHAIN (parm);
11222 if (TREE_CODE (parm) == PARM_DECL)
11223 {
f444e36b
MM
11224 if (DECL_NAME (parm) == NULL_TREE
11225 || TREE_CODE (parm) != VOID_TYPE)
11226 pushdecl (parm);
11227 else
2d01edd7 11228 error ("parameter %qD declared void", parm);
8d08fdba
MS
11229 }
11230 else
11231 {
11232 /* If we find an enum constant or a type tag,
11233 put it aside for the moment. */
11234 TREE_CHAIN (parm) = NULL_TREE;
11235 nonparms = chainon (nonparms, parm);
11236 }
11237 }
11238
f444e36b
MM
11239 /* Get the decls in their original chain order and record in the
11240 function. This is all and only the PARM_DECLs that were
11241 pushed into scope by the loop above. */
11242 DECL_ARGUMENTS (fndecl) = getdecls ();
8d08fdba
MS
11243 }
11244 else
11245 DECL_ARGUMENTS (fndecl) = NULL_TREE;
11246
11247 /* Now store the final chain of decls for the arguments
11248 as the decl-chain of the current lexical scope.
11249 Put the enumerators in as well, at the front so that
11250 DECL_ARGUMENTS is not modified. */
00e8de68 11251 current_binding_level->names = chainon (nonparms, DECL_ARGUMENTS (fndecl));
eb448459 11252
68d04c67
MM
11253 if (use_eh_spec_block (current_function_decl))
11254 current_eh_spec_block = begin_eh_spec_block ();
8d08fdba
MS
11255}
11256
8d08fdba 11257\f
59026e79
MM
11258/* We have finished doing semantic analysis on DECL, but have not yet
11259 generated RTL for its body. Save away our current state, so that
11260 when we want to generate RTL later we know what to do. */
11261
11262static void
11f6b451 11263save_function_data (tree decl)
59026e79 11264{
e2500fed 11265 struct language_function *f;
59026e79
MM
11266
11267 /* Save the language-specific per-function data so that we can
11268 get it back when we really expand this function. */
50bc768d 11269 gcc_assert (!DECL_PENDING_INLINE_P (decl));
68642fb6 11270
59026e79 11271 /* Make a copy. */
99dd239f 11272 f = GGC_NEW (struct language_function);
e2500fed 11273 memcpy (f, cp_function_chain, sizeof (struct language_function));
59026e79
MM
11274 DECL_SAVED_FUNCTION_DATA (decl) = f;
11275
11276 /* Clear out the bits we don't need. */
325c3691 11277 f->base.x_stmt_tree.x_cur_stmt_list = NULL_TREE;
59026e79 11278 f->bindings = NULL;
8e4ce833 11279 f->x_local_names = NULL;
59026e79
MM
11280}
11281
cdd2559c 11282
44d10c10 11283/* Set the return value of the constructor (if present). */
efee38a9
MM
11284
11285static void
11f6b451 11286finish_constructor_body (void)
efee38a9 11287{
44d10c10
PB
11288 tree val;
11289 tree exprstmt;
11290
11291 if (targetm.cxx.cdtor_returns_this ())
11292 {
11293 /* Any return from a constructor will end up here. */
11294 add_stmt (build_stmt (LABEL_EXPR, cdtor_label));
11295
11296 val = DECL_ARGUMENTS (current_function_decl);
f293ce4b
RS
11297 val = build2 (MODIFY_EXPR, TREE_TYPE (val),
11298 DECL_RESULT (current_function_decl), val);
44d10c10
PB
11299 /* Return the address of the object. */
11300 exprstmt = build_stmt (RETURN_EXPR, val);
11301 add_stmt (exprstmt);
11302 }
efee38a9
MM
11303}
11304
cdd2559c
JM
11305/* Do all the processing for the beginning of a destructor; set up the
11306 vtable pointers and cleanups for bases and members. */
11307
11308static void
11f6b451 11309begin_destructor_body (void)
cdd2559c 11310{
cdd2559c
JM
11311 tree compound_stmt;
11312
0e686aa6
MM
11313 /* If the CURRENT_CLASS_TYPE is incomplete, we will have already
11314 issued an error message. We still want to try to process the
11315 body of the function, but initialize_vtbl_ptrs will crash if
11316 TYPE_BINFO is NULL. */
11317 if (COMPLETE_TYPE_P (current_class_type))
11318 {
11319 compound_stmt = begin_compound_stmt (0);
11320 /* Make all virtual function table pointers in non-virtual base
11321 classes point to CURRENT_CLASS_TYPE's virtual function
11322 tables. */
11323 initialize_vtbl_ptrs (current_class_ptr);
11324 finish_compound_stmt (compound_stmt);
3db45ab5 11325
0e686aa6
MM
11326 /* And insert cleanups for our bases and members so that they
11327 will be properly destroyed if we throw. */
11328 push_base_cleanups ();
11329 }
cdd2559c
JM
11330}
11331
ade3dc07
JM
11332/* At the end of every destructor we generate code to delete the object if
11333 necessary. Do that now. */
9bfadf57
MM
11334
11335static void
11f6b451 11336finish_destructor_body (void)
9bfadf57 11337{
9bfadf57
MM
11338 tree exprstmt;
11339
5633b37c
MM
11340 /* Any return from a destructor will end up here; that way all base
11341 and member cleanups will be run when the function returns. */
44d10c10 11342 add_stmt (build_stmt (LABEL_EXPR, cdtor_label));
5633b37c 11343
52682a1b
MM
11344 /* In a virtual destructor, we must call delete. */
11345 if (DECL_VIRTUAL_P (current_function_decl))
11346 {
11347 tree if_stmt;
fa72b064 11348 tree virtual_size = cxx_sizeof (current_class_type);
68642fb6 11349
52682a1b 11350 /* [class.dtor]
68642fb6 11351
ade3dc07
JM
11352 At the point of definition of a virtual destructor (including
11353 an implicit definition), non-placement operator delete shall
11354 be looked up in the scope of the destructor's class and if
11355 found shall be accessible and unambiguous. */
3db45ab5 11356 exprstmt = build_op_delete_call(DELETE_EXPR, current_class_ptr,
63c9a190 11357 virtual_size,
3db45ab5
MS
11358 /*global_p=*/false,
11359 /*placement=*/NULL_TREE,
63c9a190 11360 /*alloc_fn=*/NULL_TREE);
298d6f60 11361
52682a1b 11362 if_stmt = begin_if_stmt ();
f293ce4b
RS
11363 finish_if_stmt_cond (build2 (BIT_AND_EXPR, integer_type_node,
11364 current_in_charge_parm,
11365 integer_one_node),
52682a1b
MM
11366 if_stmt);
11367 finish_expr_stmt (exprstmt);
11368 finish_then_clause (if_stmt);
325c3691 11369 finish_if_stmt (if_stmt);
52682a1b 11370 }
44d10c10
PB
11371
11372 if (targetm.cxx.cdtor_returns_this ())
11373 {
11374 tree val;
11375
11376 val = DECL_ARGUMENTS (current_function_decl);
f293ce4b
RS
11377 val = build2 (MODIFY_EXPR, TREE_TYPE (val),
11378 DECL_RESULT (current_function_decl), val);
44d10c10
PB
11379 /* Return the address of the object. */
11380 exprstmt = build_stmt (RETURN_EXPR, val);
11381 add_stmt (exprstmt);
11382 }
ade3dc07 11383}
9bfadf57 11384
ade3dc07
JM
11385/* Do the necessary processing for the beginning of a function body, which
11386 in this case includes member-initializers, but not the catch clauses of
11387 a function-try-block. Currently, this means opening a binding level
86ad3aa9 11388 for the member-initializers (in a ctor) and member cleanups (in a dtor). */
ade3dc07
JM
11389
11390tree
11f6b451 11391begin_function_body (void)
ade3dc07 11392{
cdd2559c
JM
11393 tree stmt;
11394
86ad3aa9
JM
11395 if (! FUNCTION_NEEDS_BODY_BLOCK (current_function_decl))
11396 return NULL_TREE;
11397
b5856475
JM
11398 if (processing_template_decl)
11399 /* Do nothing now. */;
11400 else
11401 /* Always keep the BLOCK node associated with the outermost pair of
11402 curly braces of a function. These are needed for correct
11403 operation of dwarfout.c. */
ac20c67a 11404 keep_next_level (true);
b5856475 11405
325c3691 11406 stmt = begin_compound_stmt (BCS_FN_BODY);
cdd2559c
JM
11407
11408 if (processing_template_decl)
11409 /* Do nothing now. */;
cdd2559c
JM
11410 else if (DECL_DESTRUCTOR_P (current_function_decl))
11411 begin_destructor_body ();
11412
ade3dc07 11413 return stmt;
9bfadf57
MM
11414}
11415
ade3dc07
JM
11416/* Do the processing for the end of a function body. Currently, this means
11417 closing out the cleanups for fully-constructed bases and members, and in
11418 the case of the destructor, deleting the object if desired. Again, this
11419 is only meaningful for [cd]tors, since they are the only functions where
11420 there is a significant distinction between the main body and any
11421 function catch clauses. Handling, say, main() return semantics here
11422 would be wrong, as flowing off the end of a function catch clause for
11423 main() would also need to return 0. */
11424
11425void
11f6b451 11426finish_function_body (tree compstmt)
ade3dc07 11427{
86ad3aa9
JM
11428 if (compstmt == NULL_TREE)
11429 return;
3db45ab5 11430
5633b37c 11431 /* Close the block. */
7a3397c7 11432 finish_compound_stmt (compstmt);
ade3dc07
JM
11433
11434 if (processing_template_decl)
11435 /* Do nothing now. */;
11436 else if (DECL_CONSTRUCTOR_P (current_function_decl))
11437 finish_constructor_body ();
11438 else if (DECL_DESTRUCTOR_P (current_function_decl))
11439 finish_destructor_body ();
caf93cb0 11440}
ade3dc07 11441
86ad3aa9
JM
11442/* Given a function, returns the BLOCK corresponding to the outermost level
11443 of curly braces, skipping the artificial block created for constructor
11444 initializers. */
11445
11446static tree
11447outer_curly_brace_block (tree fndecl)
11448{
11449 tree block = BLOCK_SUBBLOCKS (DECL_INITIAL (fndecl));
11450 if (FUNCTION_NEEDS_BODY_BLOCK (current_function_decl))
11451 /* Skip the artificial function body block. */
11452 block = BLOCK_SUBBLOCKS (block);
11453 return block;
11454}
11455
8d08fdba
MS
11456/* Finish up a function declaration and compile that function
11457 all the way to assembler language output. The free the storage
11458 for the function definition.
11459
68642fb6 11460 FLAGS is a bitwise or of the following values:
f181d4ae
MM
11461 2 - INCLASS_INLINE
11462 We just finished processing the body of an in-class inline
11463 function definition. (This processing will have taken place
87e3dbc9 11464 after the class definition is complete.) */
8d08fdba 11465
4d6abc1c 11466tree
11f6b451 11467finish_function (int flags)
8d08fdba 11468{
926ce8bd 11469 tree fndecl = current_function_decl;
8d08fdba 11470 tree fntype, ctype = NULL_TREE;
f181d4ae 11471 int inclass_inline = (flags & 2) != 0;
87e3dbc9 11472 int nested;
8d08fdba
MS
11473
11474 /* When we get some parse errors, we can end up without a
11475 current_function_decl, so cope. */
11476 if (fndecl == NULL_TREE)
4d6abc1c 11477 return error_mark_node;
8d08fdba 11478
9aad8f83
MA
11479 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fndecl)
11480 && DECL_VIRTUAL_P (fndecl)
11481 && !processing_template_decl)
11482 {
11483 tree fnclass = DECL_CONTEXT (fndecl);
11484 if (fndecl == CLASSTYPE_KEY_METHOD (fnclass))
11485 keyed_classes = tree_cons (NULL_TREE, fnclass, keyed_classes);
11486 }
11487
87e3dbc9 11488 nested = function_depth > 1;
8d08fdba
MS
11489 fntype = TREE_TYPE (fndecl);
11490
9bfadf57
MM
11491 /* TREE_READONLY (fndecl) = 1;
11492 This caused &foo to be of type ptr-to-const-function
11493 which then got a warning when stored in a ptr-to-function variable. */
8d08fdba 11494
50bc768d 11495 gcc_assert (building_stmt_tree ());
72c4a4ca
GK
11496 /* The current function is being defined, so its DECL_INITIAL should
11497 be set, and unless there's a multiple definition, it should be
11498 error_mark_node. */
11499 gcc_assert (DECL_INITIAL (fndecl) == error_mark_node);
caf93cb0 11500
db9b2174
MM
11501 /* For a cloned function, we've already got all the code we need;
11502 there's no need to add any extra bits. */
f444e36b 11503 if (!DECL_CLONED_FUNCTION_P (fndecl))
8d08fdba 11504 {
ade3dc07 11505 if (DECL_MAIN_P (current_function_decl))
efee38a9 11506 {
43e05e45
SB
11507 tree stmt;
11508
11509 /* Make it so that `main' always returns 0 by default (or
11510 1 for VMS). */
08c7ae5a 11511#if VMS_TARGET
43e05e45 11512 stmt = finish_return_stmt (integer_one_node);
efee38a9 11513#else
43e05e45 11514 stmt = finish_return_stmt (integer_zero_node);
efee38a9 11515#endif
43e05e45
SB
11516 /* Hack. We don't want the middle-end to warn that this
11517 return is unreachable, so put the statement on the
11518 special line 0. */
176a0aad
PB
11519#ifdef USE_MAPPED_LOCATION
11520 SET_EXPR_LOCATION (stmt, UNKNOWN_LOCATION);
11521#else
43e05e45 11522 annotate_with_file_line (stmt, input_filename, 0);
176a0aad 11523#endif
efee38a9 11524 }
87e3dbc9 11525
68d04c67 11526 if (use_eh_spec_block (current_function_decl))
52a11cbf
RH
11527 finish_eh_spec_block (TYPE_RAISES_EXCEPTIONS
11528 (TREE_TYPE (current_function_decl)),
11529 current_eh_spec_block);
5566b478 11530 }
68642fb6 11531
558475f0 11532 /* If we're saving up tree structure, tie off the function now. */
325c3691
RH
11533 DECL_SAVED_TREE (fndecl) = pop_stmt_list (DECL_SAVED_TREE (fndecl));
11534
11535 finish_fname_decls ();
8d2733ca 11536
b2dd096b
MM
11537 /* If this function can't throw any exceptions, remember that. */
11538 if (!processing_template_decl
11539 && !cp_function_chain->can_throw
be0436d7 11540 && !flag_non_call_exceptions
56fd65c7 11541 && !DECL_REPLACEABLE_P (fndecl))
b2dd096b
MM
11542 TREE_NOTHROW (fndecl) = 1;
11543
8d08fdba
MS
11544 /* This must come after expand_function_end because cleanups might
11545 have declarations (from inline functions) that need to go into
11546 this function's blocks. */
caf93cb0 11547
7437519c
ZW
11548 /* If the current binding level isn't the outermost binding level
11549 for this function, either there is a bug, or we have experienced
11550 syntax errors and the statement tree is malformed. */
a7e8c268 11551 if (current_binding_level->kind != sk_function_parms)
7437519c
ZW
11552 {
11553 /* Make sure we have already experienced errors. */
8dc2b103 11554 gcc_assert (errorcount);
7437519c
ZW
11555
11556 /* Throw away the broken statement tree and extra binding
0cbd7506 11557 levels. */
5882f0f3 11558 DECL_SAVED_TREE (fndecl) = alloc_stmt_list ();
7437519c 11559
a7e8c268 11560 while (current_binding_level->kind != sk_function_parms)
7437519c 11561 {
a7e8c268 11562 if (current_binding_level->kind == sk_class)
7437519c
ZW
11563 pop_nested_class ();
11564 else
11565 poplevel (0, 0, 0);
11566 }
11567 }
f444e36b 11568 poplevel (1, 0, 1);
8d08fdba 11569
4985cde3
RH
11570 /* Statements should always be full-expressions at the outermost set
11571 of curly braces for a function. */
50bc768d 11572 gcc_assert (stmts_are_full_exprs_p ());
4985cde3 11573
6de9cd9a
DN
11574 /* Set up the named return value optimization, if we can. Candidate
11575 variables are selected in check_return_value. */
07b2f2fd
JM
11576 if (current_function_return_value)
11577 {
11578 tree r = current_function_return_value;
35e939ae
JM
11579 tree outer;
11580
07b2f2fd 11581 if (r != error_mark_node
35e939ae
JM
11582 /* This is only worth doing for fns that return in memory--and
11583 simpler, since we don't have to worry about promoted modes. */
61f71b34 11584 && aggregate_value_p (TREE_TYPE (TREE_TYPE (fndecl)), fndecl)
35e939ae
JM
11585 /* Only allow this for variables declared in the outer scope of
11586 the function so we know that their lifetime always ends with a
11587 return; see g++.dg/opt/nrv6.C. We could be more flexible if
11588 we were to do this optimization in tree-ssa. */
86ad3aa9 11589 && (outer = outer_curly_brace_block (fndecl))
325c3691 11590 && chain_member (r, BLOCK_VARS (outer)))
6de9cd9a
DN
11591 finalize_nrv (&DECL_SAVED_TREE (fndecl), r, DECL_RESULT (fndecl));
11592
11593 current_function_return_value = NULL_TREE;
07b2f2fd
JM
11594 }
11595
a8f73d4b 11596 /* Remember that we were in class scope. */
db5ae43f 11597 if (current_class_name)
a8f73d4b 11598 ctype = current_class_type;
db5ae43f 11599
1caa11d3
MM
11600 /* Must mark the RESULT_DECL as being in this function. */
11601 DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
11602
11603 /* Set the BLOCK_SUPERCONTEXT of the outermost function scope to point
11604 to the FUNCTION_DECL node itself. */
11605 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
11606
59026e79 11607 /* Save away current state, if appropriate. */
f444e36b 11608 if (!processing_template_decl)
59026e79
MM
11609 save_function_data (fndecl);
11610
efe49da0 11611 /* Complain if there's just no return statement. */
46cfb101 11612 if (warn_return_type
efe49da0 11613 && TREE_CODE (TREE_TYPE (fntype)) != VOID_TYPE
c1b83cc9 11614 && !dependent_type_p (TREE_TYPE (fntype))
57d5032b 11615 && !current_function_returns_value && !current_function_returns_null
efe49da0
JM
11616 /* Don't complain if we abort or throw. */
11617 && !current_function_returns_abnormally
55e99d52 11618 && !DECL_NAME (DECL_RESULT (fndecl))
46cfb101 11619 /* Normally, with -Wreturn-type, flow will complain. Unless we're an
efe49da0 11620 inline function, as we might never be compiled separately. */
44d10c10
PB
11621 && (DECL_INLINE (fndecl) || processing_template_decl)
11622 /* Structor return values (if any) are set by the compiler. */
11623 && !DECL_CONSTRUCTOR_P (fndecl)
11624 && !DECL_DESTRUCTOR_P (fndecl))
b323323f 11625 warning (OPT_Wreturn_type, "no return statement in function returning non-void");
4985cde3 11626
6de9cd9a
DN
11627 /* Store the end of the function, so that we get good line number
11628 info for the epilogue. */
11629 cfun->function_end_locus = input_location;
11630
11631 /* Genericize before inlining. */
11632 if (!processing_template_decl)
11633 {
37e6d6fe 11634 struct language_function *f = DECL_SAVED_FUNCTION_DATA (fndecl);
5a508662 11635 cp_genericize (fndecl);
37e6d6fe
JH
11636 /* Clear out the bits we don't need. */
11637 f->x_current_class_ptr = NULL;
11638 f->x_current_class_ref = NULL;
11639 f->x_eh_spec_block = NULL;
11640 f->x_in_charge_parm = NULL;
11641 f->x_vtt_parm = NULL;
11642 f->x_return_value = NULL;
11643 f->bindings = NULL;
10827cd8 11644 f->extern_decl_map = NULL;
6de9cd9a
DN
11645
11646 /* Handle attribute((warn_unused_result)). Relies on gimple input. */
11647 c_warn_unused_result (&DECL_SAVED_TREE (fndecl));
11648 }
37e6d6fe
JH
11649 /* Clear out the bits we don't need. */
11650 local_names = NULL;
6de9cd9a
DN
11651
11652 /* We're leaving the context of this function, so zap cfun. It's still in
11653 DECL_STRUCT_FUNCTION, and we'll restore it in tree_rest_of_compilation. */
f444e36b 11654 cfun = NULL;
c1f927e8 11655 current_function_decl = NULL;
a8f73d4b 11656
27631dae 11657 /* If this is an in-class inline definition, we may have to pop the
a8f73d4b
MM
11658 bindings for the template parameters that we added in
11659 maybe_begin_member_template_processing when start_function was
11660 called. */
11661 if (inclass_inline)
11662 maybe_end_member_template_processing ();
11663
11664 /* Leave the scope of the class. */
11665 if (ctype)
11666 pop_nested_class ();
5566b478
MS
11667
11668 --function_depth;
8d08fdba 11669
4d6abc1c 11670 /* Clean up. */
28cbf42c 11671 if (! nested)
1f8f4a0b
MM
11672 /* Let the error reporting routines know that we're outside a
11673 function. For a nested function, this value is used in
b03e38e1 11674 cxx_pop_function_context and then reset via pop_function_context. */
1f8f4a0b 11675 current_function_decl = NULL_TREE;
4d6abc1c
MM
11676
11677 return fndecl;
8d08fdba
MS
11678}
11679\f
11680/* Create the FUNCTION_DECL for a function definition.
8d08fdba
MS
11681 DECLSPECS and DECLARATOR are the parts of the declaration;
11682 they describe the return type and the name of the function,
11683 but twisted together in a fashion that parallels the syntax of C.
11684
11685 This function creates a binding context for the function body
11686 as well as setting up the FUNCTION_DECL in current_function_decl.
11687
11688 Returns a FUNCTION_DECL on success.
11689
11690 If the DECLARATOR is not suitable for a function (it defines a datum
11691 instead), we return 0, which tells yyparse to report a parse error.
11692
11693 May return void_type_node indicating that this method is actually
11694 a friend. See grokfield for more details.
11695
11696 Came here with a `.pushlevel' .
11697
11698 DO NOT MAKE ANY CHANGES TO THIS CODE WITHOUT MAKING CORRESPONDING
11699 CHANGES TO CODE IN `grokfield'. */
e92cc029 11700
8d08fdba 11701tree
caf93cb0 11702start_method (cp_decl_specifier_seq *declspecs,
0cbd7506 11703 const cp_declarator *declarator, tree attrlist)
8d08fdba 11704{
c11b6f21 11705 tree fndecl = grokdeclarator (declarator, declspecs, MEMFUNCDEF, 0,
91d231cb 11706 &attrlist);
8d08fdba 11707
a723baf1
MM
11708 if (fndecl == error_mark_node)
11709 return error_mark_node;
11710
11711 if (fndecl == NULL || TREE_CODE (fndecl) != FUNCTION_DECL)
11712 {
11713 error ("invalid member function declaration");
11714 return error_mark_node;
11715 }
8d08fdba 11716
195a5def
IR
11717 if (attrlist)
11718 cplus_decl_attributes (&fndecl, attrlist, 0);
11719
8d08fdba 11720 /* Pass friends other than inline friend functions back. */
a1774733 11721 if (fndecl == void_type_node)
8d08fdba
MS
11722 return fndecl;
11723
8d08fdba
MS
11724 if (DECL_IN_AGGR_P (fndecl))
11725 {
58ec3cc5 11726 if (DECL_CONTEXT (fndecl)
6d69fe27 11727 && TREE_CODE (DECL_CONTEXT (fndecl)) != NAMESPACE_DECL)
2d01edd7 11728 error ("%qD is already defined in class %qT", fndecl,
58ec3cc5 11729 DECL_CONTEXT (fndecl));
8ca4bf25 11730 return error_mark_node;
8d08fdba
MS
11731 }
11732
f3400fe2
JM
11733 check_template_shadow (fndecl);
11734
79065db2 11735 DECL_DECLARED_INLINE_P (fndecl) = 1;
8926095f 11736 if (flag_default_inline)
8d08fdba
MS
11737 DECL_INLINE (fndecl) = 1;
11738
36a117a5
MM
11739 /* We process method specializations in finish_struct_1. */
11740 if (processing_template_decl && !DECL_TEMPLATE_SPECIALIZATION (fndecl))
4b0d3cbe
MM
11741 {
11742 fndecl = push_template_decl (fndecl);
11743 if (fndecl == error_mark_node)
11744 return fndecl;
11745 }
a0a33927 11746
8d08fdba
MS
11747 if (! DECL_FRIEND_P (fndecl))
11748 {
8d08fdba
MS
11749 if (TREE_CHAIN (fndecl))
11750 {
11751 fndecl = copy_node (fndecl);
11752 TREE_CHAIN (fndecl) = NULL_TREE;
11753 }
8d08fdba
MS
11754 }
11755
d174af6c 11756 finish_decl (fndecl, NULL_TREE, NULL_TREE);
8d08fdba 11757
f4f206f4 11758 /* Make a place for the parms. */
ac20c67a 11759 begin_scope (sk_function_parms, fndecl);
68642fb6 11760
8d08fdba
MS
11761 DECL_IN_AGGR_P (fndecl) = 1;
11762 return fndecl;
11763}
11764
11765/* Go through the motions of finishing a function definition.
11766 We don't compile this method until after the whole class has
11767 been processed.
11768
11769 FINISH_METHOD must return something that looks as though it
11770 came from GROKFIELD (since we are defining a method, after all).
11771
11772 This is called after parsing the body of the function definition.
11773 STMTS is the chain of statements that makes up the function body.
11774
11775 DECL is the ..._DECL that `start_method' provided. */
11776
11777tree
11f6b451 11778finish_method (tree decl)
8d08fdba 11779{
926ce8bd 11780 tree fndecl = decl;
8d08fdba 11781 tree old_initial;
8d08fdba 11782
926ce8bd 11783 tree link;
8d08fdba 11784
a1774733 11785 if (decl == void_type_node)
8d08fdba
MS
11786 return decl;
11787
11788 old_initial = DECL_INITIAL (fndecl);
11789
11790 /* Undo the level for the parms (from start_method).
11791 This is like poplevel, but it causes nothing to be
11792 saved. Saving information here confuses symbol-table
11793 output routines. Besides, this information will
11794 be correctly output when this method is actually
11795 compiled. */
11796
11797 /* Clear out the meanings of the local variables of this level;
11798 also record in each decl which block it belongs to. */
11799
11800 for (link = current_binding_level->names; link; link = TREE_CHAIN (link))
11801 {
11802 if (DECL_NAME (link) != NULL_TREE)
d8f8dca1 11803 pop_binding (DECL_NAME (link), link);
50bc768d 11804 gcc_assert (TREE_CODE (link) != FUNCTION_DECL);
8d08fdba
MS
11805 DECL_CONTEXT (link) = NULL_TREE;
11806 }
11807
8d08fdba
MS
11808 poplevel (0, 0, 0);
11809
11810 DECL_INITIAL (fndecl) = old_initial;
11811
11812 /* We used to check if the context of FNDECL was different from
11813 current_class_type as another way to get inside here. This didn't work
11814 for String.cc in libg++. */
11815 if (DECL_FRIEND_P (fndecl))
11816 {
d4e6fecb 11817 VEC_safe_push (tree, gc, CLASSTYPE_INLINE_FRIENDS (current_class_type),
585b44d3 11818 fndecl);
8d08fdba
MS
11819 decl = void_type_node;
11820 }
11821
11822 return decl;
11823}
11824\f
0154eaa8
MM
11825
11826/* VAR is a VAR_DECL. If its type is incomplete, remember VAR so that
11827 we can lay it out later, when and if its type becomes complete. */
8d08fdba
MS
11828
11829void
11f6b451 11830maybe_register_incomplete_var (tree var)
8d08fdba 11831{
50bc768d 11832 gcc_assert (TREE_CODE (var) == VAR_DECL);
8d08fdba 11833
0154eaa8 11834 /* Keep track of variables with incomplete types. */
caf93cb0 11835 if (!processing_template_decl && TREE_TYPE (var) != error_mark_node
0154eaa8 11836 && DECL_EXTERNAL (var))
70adf8a9 11837 {
0154eaa8 11838 tree inner_type = TREE_TYPE (var);
caf93cb0 11839
0154eaa8
MM
11840 while (TREE_CODE (inner_type) == ARRAY_TYPE)
11841 inner_type = TREE_TYPE (inner_type);
11842 inner_type = TYPE_MAIN_VARIANT (inner_type);
caf93cb0 11843
0154eaa8
MM
11844 if ((!COMPLETE_TYPE_P (inner_type) && CLASS_TYPE_P (inner_type))
11845 /* RTTI TD entries are created while defining the type_info. */
11846 || (TYPE_LANG_SPECIFIC (inner_type)
11847 && TYPE_BEING_DEFINED (inner_type)))
11848 incomplete_vars = tree_cons (inner_type, var, incomplete_vars);
70adf8a9 11849 }
0154eaa8 11850}
70adf8a9 11851
0154eaa8
MM
11852/* Called when a class type (given by TYPE) is defined. If there are
11853 any existing VAR_DECLs whose type hsa been completed by this
11854 declaration, update them now. */
70adf8a9 11855
0154eaa8 11856void
11f6b451 11857complete_vars (tree type)
0154eaa8
MM
11858{
11859 tree *list = &incomplete_vars;
11860
50bc768d 11861 gcc_assert (CLASS_TYPE_P (type));
caf93cb0 11862 while (*list)
0154eaa8
MM
11863 {
11864 if (same_type_p (type, TREE_PURPOSE (*list)))
70adf8a9 11865 {
0154eaa8 11866 tree var = TREE_VALUE (*list);
67935995 11867 tree type = TREE_TYPE (var);
c740732f
MM
11868 /* Complete the type of the variable. The VAR_DECL itself
11869 will be laid out in expand_expr. */
67935995
MM
11870 complete_type (type);
11871 cp_apply_type_quals_to_decl (cp_type_quals (type), var);
0154eaa8
MM
11872 /* Remove this entry from the list. */
11873 *list = TREE_CHAIN (*list);
f30432d7
MS
11874 }
11875 else
0154eaa8 11876 list = &TREE_CHAIN (*list);
f30432d7 11877 }
7fb213d8
GB
11878
11879 /* Check for pending declarations which may have abstract type. */
11880 complete_type_check_abstract (type);
8d08fdba
MS
11881}
11882
5c113154
MM
11883/* If DECL is of a type which needs a cleanup, build and return an
11884 expression to perform that cleanup here. Return NULL_TREE if no
11885 cleanup need be done. */
e92cc029 11886
86f45d2c 11887tree
11f6b451 11888cxx_maybe_build_cleanup (tree decl)
8d08fdba 11889{
5c113154
MM
11890 tree type;
11891 tree attr;
11892 tree cleanup;
11893
11894 /* Assume no cleanup is required. */
11895 cleanup = NULL_TREE;
86f45d2c 11896
5c113154
MM
11897 if (error_operand_p (decl))
11898 return cleanup;
11899
11900 /* Handle "__attribute__((cleanup))". We run the cleanup function
11901 before the destructor since the destructor is what actually
11902 terminates the lifetime of the object. */
11903 attr = lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl));
11904 if (attr)
11905 {
11906 tree id;
11907 tree fn;
11908 tree arg;
11909
11910 /* Get the name specified by the user for the cleanup function. */
11911 id = TREE_VALUE (TREE_VALUE (attr));
11912 /* Look up the name to find the cleanup function to call. It is
11913 important to use lookup_name here because that is what is
11914 used in c-common.c:handle_cleanup_attribute when performing
11915 initial checks on the attribute. Note that those checks
11916 include ensuring that the function found is not an overloaded
11917 function, or an object with an overloaded call operator,
11918 etc.; we can rely on the fact that the functionfound is an
11919 ordinary FUNCTION_DECL. */
11920 fn = lookup_name (id);
11921 arg = build_address (decl);
11922 mark_used (decl);
11923 cleanup = build_function_call (fn, build_tree_list (NULL_TREE,
11924 arg));
11925 }
11926 /* Handle ordinary C++ destructors. */
11927 type = TREE_TYPE (decl);
11928 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
8d08fdba 11929 {
80048418 11930 int flags = LOOKUP_NORMAL|LOOKUP_DESTRUCTOR;
5775a06a
NS
11931 bool has_vbases = (TREE_CODE (type) == RECORD_TYPE
11932 && CLASSTYPE_VBASECLASSES (type));
5c113154
MM
11933 tree addr;
11934 tree call;
8d08fdba 11935
8d08fdba 11936 if (TREE_CODE (type) == ARRAY_TYPE)
5c113154 11937 addr = decl;
8d08fdba
MS
11938 else
11939 {
dffd7eb6 11940 cxx_mark_addressable (decl);
5c113154 11941 addr = build_unary_op (ADDR_EXPR, decl, 0);
8d08fdba
MS
11942 }
11943
11944 /* Optimize for space over speed here. */
5775a06a 11945 if (!has_vbases || flag_expensive_optimizations)
8d08fdba
MS
11946 flags |= LOOKUP_NONVIRTUAL;
11947
5c113154 11948 call = build_delete (TREE_TYPE (addr), addr,
86f45d2c 11949 sfk_complete_destructor, flags, 0);
5c113154
MM
11950 if (cleanup)
11951 cleanup = build_compound_expr (cleanup, call);
11952 else
11953 cleanup = call;
8d08fdba 11954 }
5c113154
MM
11955
11956 return cleanup;
8d08fdba
MS
11957}
11958\f
558475f0 11959/* When a stmt has been parsed, this function is called. */
8d08fdba
MS
11960
11961void
11f6b451 11962finish_stmt (void)
8d08fdba 11963{
8d08fdba
MS
11964}
11965
3afb32a4
MM
11966/* DECL was originally constructed as a non-static member function,
11967 but turned out to be static. Update it accordingly. */
700f8a87 11968
8857f91e 11969void
11f6b451 11970revert_static_member_fn (tree decl)
8d08fdba 11971{
700f8a87 11972 tree tmp;
3afb32a4
MM
11973 tree function = TREE_TYPE (decl);
11974 tree args = TYPE_ARG_TYPES (function);
8d08fdba 11975
89d684bb 11976 if (cp_type_quals (TREE_TYPE (TREE_VALUE (args)))
91063b51 11977 != TYPE_UNQUALIFIED)
c4f73174 11978 error ("static member function %q#D declared with type qualifiers", decl);
f30432d7 11979
700f8a87
MS
11980 args = TREE_CHAIN (args);
11981 tmp = build_function_type (TREE_TYPE (function), args);
89d684bb 11982 tmp = build_qualified_type (tmp, cp_type_quals (function));
f30432d7 11983 tmp = build_exception_variant (tmp,
8d08fdba 11984 TYPE_RAISES_EXCEPTIONS (function));
3afb32a4
MM
11985 TREE_TYPE (decl) = tmp;
11986 if (DECL_ARGUMENTS (decl))
11987 DECL_ARGUMENTS (decl) = TREE_CHAIN (DECL_ARGUMENTS (decl));
11988 DECL_STATIC_FUNCTION_P (decl) = 1;
8d08fdba 11989}
a4443a08 11990
68642fb6
UD
11991/* Initialize the variables used during compilation of a C++
11992 function. */
db5ae43f 11993
b03e38e1 11994void
11f6b451 11995cxx_push_function_context (struct function * f)
99dccabc 11996{
99dd239f 11997 struct language_function *p = GGC_CNEW (struct language_function);
e2500fed 11998 f->language = p;
db5ae43f 11999
f1dedc31
MM
12000 /* Whenever we start a new function, we destroy temporaries in the
12001 usual way. */
ae499cce 12002 current_stmt_tree ()->stmts_are_full_exprs_p = 1;
4985cde3
RH
12003
12004 if (f->decl)
12005 {
12006 tree fn = f->decl;
12007
4985cde3
RH
12008 if (DECL_SAVED_FUNCTION_DATA (fn))
12009 {
12010 /* If we already parsed this function, and we're just expanding it
12011 now, restore saved state. */
12012 *cp_function_chain = *DECL_SAVED_FUNCTION_DATA (fn);
12013
4985cde3 12014 /* We don't need the saved data anymore. Unless this is an inline
caf93cb0 12015 function; we need the named return value info for
7740f00d 12016 declare_return_variable. */
4985cde3
RH
12017 if (! DECL_INLINE (fn))
12018 DECL_SAVED_FUNCTION_DATA (fn) = NULL;
12019 }
12020 }
db5ae43f
MS
12021}
12022
a8f73d4b
MM
12023/* Free the language-specific parts of F, now that we've finished
12024 compiling the function. */
db5ae43f 12025
b03e38e1 12026void
11f6b451 12027cxx_pop_function_context (struct function * f)
db5ae43f 12028{
99dccabc 12029 f->language = 0;
db5ae43f 12030}
ebfc180f 12031
e2500fed
GK
12032/* Return which tree structure is used by T, or TS_CP_GENERIC if T is
12033 one of the language-independent trees. */
4519c0a8 12034
e2500fed 12035enum cp_tree_node_structure_enum
11f6b451 12036cp_tree_node_structure (union lang_tree_node * t)
4519c0a8 12037{
e2500fed 12038 switch (TREE_CODE (&t->generic))
4519c0a8 12039 {
a723baf1 12040 case DEFAULT_ARG: return TS_CP_DEFAULT_ARG;
e2500fed 12041 case IDENTIFIER_NODE: return TS_CP_IDENTIFIER;
e2500fed
GK
12042 case OVERLOAD: return TS_CP_OVERLOAD;
12043 case TEMPLATE_PARM_INDEX: return TS_CP_TPI;
406d77a4 12044 case TINST_LEVEL: return TS_CP_TINST_LEVEL;
e2500fed 12045 case PTRMEM_CST: return TS_CP_PTRMEM;
0cbd7506 12046 case BASELINK: return TS_CP_BASELINK;
54e67af0 12047 case STATIC_ASSERT: return TS_CP_STATIC_ASSERT;
5d80a306 12048 case ARGUMENT_PACK_SELECT: return TS_CP_ARGUMENT_PACK_SELECT;
cb68ec50 12049 case TRAIT_EXPR: return TS_CP_TRAIT_EXPR;
e2500fed 12050 default: return TS_CP_GENERIC;
4519c0a8
MM
12051 }
12052}
5fd8e536 12053
eaa7c03f
JM
12054/* Build the void_list_node (void_type_node having been created). */
12055tree
11f6b451 12056build_void_list_node (void)
eaa7c03f
JM
12057{
12058 tree t = build_tree_list (NULL_TREE, void_type_node);
eaa7c03f
JM
12059 return t;
12060}
12061
6de9cd9a 12062bool
11f6b451 12063cp_missing_noreturn_ok_p (tree decl)
d363e7bf
AJ
12064{
12065 /* A missing noreturn is ok for the `main' function. */
92643fea 12066 return DECL_MAIN_P (decl);
d363e7bf 12067}
e2500fed 12068
c18a5b6c
MM
12069/* Return the COMDAT group into which DECL should be placed. */
12070
12071const char *
12072cxx_comdat_group (tree decl)
12073{
12074 tree name;
12075
12076 /* Virtual tables, construction virtual tables, and virtual table
12077 tables all go in a single COMDAT group, named after the primary
12078 virtual table. */
12079 if (TREE_CODE (decl) == VAR_DECL && DECL_VTABLE_OR_VTT_P (decl))
12080 name = DECL_ASSEMBLER_NAME (CLASSTYPE_VTABLES (DECL_CONTEXT (decl)));
12081 /* For all other DECLs, the COMDAT group is the mangled name of the
12082 declaration itself. */
12083 else
5591e5f9
JJ
12084 {
12085 while (DECL_THUNK_P (decl))
12086 {
12087 /* If TARGET_USE_LOCAL_THUNK_ALIAS_P, use_thunk puts the thunk
12088 into the same section as the target function. In that case
12089 we must return target's name. */
12090 tree target = THUNK_TARGET (decl);
12091 if (TARGET_USE_LOCAL_THUNK_ALIAS_P (target)
12092 && DECL_SECTION_NAME (target) != NULL
12093 && DECL_ONE_ONLY (target))
12094 decl = target;
12095 else
12096 break;
12097 }
12098 name = DECL_ASSEMBLER_NAME (decl);
12099 }
c18a5b6c
MM
12100
12101 return IDENTIFIER_POINTER (name);
12102}
12103
e2500fed 12104#include "gt-cp-decl.h"
This page took 7.791431 seconds and 5 git commands to generate.