]> gcc.gnu.org Git - gcc.git/blame - gcc/cp/decl.c
* gcc/config/m68k/linux.h
[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 9it under the terms of the GNU General Public License as published by
e77f031d 10the Free Software Foundation; either version 3, or (at your option)
8d08fdba
MS
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
e77f031d
NC
19along with GCC; see the file COPYING3. If not see
20<http://www.gnu.org/licenses/>. */
8d08fdba
MS
21
22
1c313945 23/* Process declarations and symbol lookup for C++ front end.
8d08fdba
MS
24 Also constructs types; the standard scalar types at initialization,
25 and structure, union, array and enum types when they are declared. */
26
27/* ??? not all decl nodes are given the most useful possible
28 line numbers. For example, the CONST_DECLs for enum values. */
29
8d08fdba 30#include "config.h"
8d052bc7 31#include "system.h"
4977bab6
ZW
32#include "coretypes.h"
33#include "tm.h"
8d08fdba
MS
34#include "tree.h"
35#include "rtl.h"
3bdf5ad1 36#include "expr.h"
8d08fdba
MS
37#include "flags.h"
38#include "cp-tree.h"
25af8512 39#include "tree-inline.h"
8d08fdba 40#include "decl.h"
49c249e1
JM
41#include "output.h"
42#include "except.h"
54f92bfb 43#include "toplev.h"
e2500fed 44#include "hashtab.h"
0e9295cf 45#include "tm_p.h"
672a6f42 46#include "target.h"
26f943fd 47#include "c-common.h"
ecb0eece 48#include "c-pragma.h"
7437519c 49#include "diagnostic.h"
e7c41c99 50#include "intl.h"
e2500fed 51#include "debug.h"
22ffcc6f 52#include "timevar.h"
6de9cd9a 53#include "tree-flow.h"
0d83bf5a 54#include "pointer-set.h"
8d08fdba 55
62d1db17 56static tree grokparms (cp_parameter_declarator *, tree *);
11f6b451
NN
57static const char *redeclaration_error_message (tree, tree);
58
11f6b451 59static int decl_jump_unsafe (tree);
11f6b451
NN
60static void require_complete_types_for_parms (tree);
61static int ambi_op_p (enum tree_code);
62static int unary_op_p (enum tree_code);
11f6b451 63static void push_local_name (tree);
7e99327d 64static tree grok_reference_init (tree, tree, tree, tree *);
caf93cb0 65static tree grokvardecl (tree, tree, const cp_decl_specifier_seq *,
1ff3c076 66 int, int, tree);
11f6b451 67static void record_unknown_type (tree, const char *);
c79efc4d 68static tree builtin_function_1 (tree, tree);
11f6b451
NN
69static tree build_library_fn_1 (tree, enum tree_code, tree);
70static int member_function_or_else (tree, tree, enum overload_flags);
71static void bad_specifiers (tree, const char *, int, int, int, int,
72 int);
11f6b451
NN
73static void check_for_uninitialized_const_var (tree);
74static hashval_t typename_hash (const void *);
75static int typename_compare (const void *, const void *);
11f6b451 76static tree local_variable_p_walkfn (tree *, int *, void *);
11f6b451 77static tree record_builtin_java_type (const char *, int);
461c6fce 78static const char *tag_name (enum tag_types);
29ef83de 79static tree lookup_and_check_tag (enum tag_types, tree, tag_scope, bool);
11f6b451 80static int walk_namespaces_r (tree, walk_namespaces_fn, void *);
11f6b451
NN
81static void maybe_deduce_size_from_array_init (tree, tree);
82static void layout_var_decl (tree);
83static void maybe_commonize_var (tree);
7e99327d 84static tree check_initializer (tree, tree, int, tree *);
11f6b451
NN
85static void make_rtl_for_nonlocal_decl (tree, tree, const char *);
86static void save_function_data (tree);
87static void check_function_type (tree, tree);
11f6b451
NN
88static void finish_constructor_body (void);
89static void begin_destructor_body (void);
90static void finish_destructor_body (void);
91static tree create_array_type_for_decl (tree, tree, tree);
92static tree get_atexit_node (void);
93static tree get_dso_handle_node (void);
94static tree start_cleanup_fn (void);
95static void end_cleanup_fn (void);
96static tree cp_make_fname_decl (tree, int);
97static void initialize_predefined_identifiers (void);
caf93cb0 98static tree check_special_function_return_type
11f6b451
NN
99 (special_function_kind, tree, tree);
100static tree push_cp_library_fn (enum tree_code, tree);
101static tree build_cp_library_fn (tree, enum tree_code, tree);
102static void store_parm_decls (tree);
8e3df2de
MM
103static void initialize_local_var (tree, tree);
104static void expand_static_init (tree, tree);
105static tree next_initializable_field (tree);
8d08fdba 106
7f4edbcb 107/* The following symbols are subsumed in the cp_global_trees array, and
68642fb6 108 listed here individually for documentation purposes.
8d08fdba 109
7f4edbcb
BS
110 C++ extensions
111 tree wchar_decl_node;
37c46b43 112
7f4edbcb
BS
113 tree vtable_entry_type;
114 tree delta_type_node;
7f4edbcb 115 tree __t_desc_type_node;
8d08fdba 116
9e62871e 117 tree class_type_node;
7f4edbcb 118 tree unknown_type_node;
8d08fdba 119
7f4edbcb 120 Array type `vtable_entry_type[]'
8d08fdba 121
7f4edbcb
BS
122 tree vtbl_type_node;
123 tree vtbl_ptr_type_node;
8d08fdba 124
2854d3c6 125 Namespaces,
8d08fdba 126
7f4edbcb 127 tree std_node;
2854d3c6 128 tree abi_node;
8d08fdba 129
7f4edbcb
BS
130 A FUNCTION_DECL which can call `abort'. Not necessarily the
131 one that the user will declare, but sufficient to be called
132 by routines that want to abort the program.
8d08fdba 133
7f4edbcb 134 tree abort_fndecl;
8d08fdba 135
7f4edbcb 136 The FUNCTION_DECL for the default `::operator delete'.
2986ae00 137
7f4edbcb 138 tree global_delete_fndecl;
8d08fdba 139
7f4edbcb 140 Used by RTTI
669ec2b4 141 tree type_info_type_node, tinfo_decl_id, tinfo_decl_type;
3db45ab5 142 tree tinfo_var_id; */
8d08fdba 143
7f4edbcb 144tree cp_global_trees[CPTI_MAX];
8d08fdba 145
2c73f9f5 146/* Indicates that there is a type value in some namespace, although
7f4edbcb 147 that is not necessarily in scope at the moment. */
2c73f9f5 148
00e8de68 149tree global_type_node;
2c73f9f5 150
82a2669e 151/* The node that holds the "name" of the global scope. */
00e8de68 152tree global_scope_name;
82a2669e 153
8e4ce833
JJ
154#define local_names cp_function_chain->x_local_names
155
8d08fdba
MS
156/* A list of objects which have constructors or destructors
157 which reside in the global scope. The decl is stored in
158 the TREE_VALUE slot and the initializer is stored
159 in the TREE_PURPOSE slot. */
160tree static_aggregates;
161
8d08fdba
MS
162/* -- end of C++ */
163
81b3411c 164/* A node for the integer constants 2, and 3. */
d11ad92e 165
81b3411c 166tree integer_two_node, integer_three_node;
8d08fdba 167
1799e5d5
RH
168/* Used only for jumps to as-yet undefined labels, since jumps to
169 defined labels can have their validity checked immediately. */
170
171struct named_label_use_entry GTY(())
172{
173 struct named_label_use_entry *next;
174 /* The binding level to which this entry is *currently* attached.
175 This is initially the binding level in which the goto appeared,
176 but is modified as scopes are closed. */
177 struct cp_binding_level *binding_level;
178 /* The head of the names list that was current when the goto appeared,
179 or the inner scope popped. These are the decls that will *not* be
180 skipped when jumping to the label. */
181 tree names_in_scope;
182 /* The location of the goto, for error reporting. */
183 location_t o_goto_locus;
184 /* True if an OpenMP structured block scope has been closed since
185 the goto appeared. This means that the branch from the label will
186 illegally exit an OpenMP scope. */
187 bool in_omp_scope;
188};
189
6625cdb5
JM
190/* A list of all LABEL_DECLs in the function that have names. Here so
191 we can clear out their names' definitions at the end of the
192 function, and so we can check the validity of jumps to these labels. */
193
1799e5d5 194struct named_label_entry GTY(())
6625cdb5 195{
1799e5d5
RH
196 /* The decl itself. */
197 tree label_decl;
198
199 /* The binding level to which the label is *currently* attached.
200 This is initially set to the binding level in which the label
201 is defined, but is modified as scopes are closed. */
e2500fed 202 struct cp_binding_level *binding_level;
3db45ab5 203 /* The head of the names list that was current when the label was
1799e5d5
RH
204 defined, or the inner scope popped. These are the decls that will
205 be skipped when jumping to the label. */
6625cdb5 206 tree names_in_scope;
1799e5d5
RH
207 /* A tree list of all decls from all binding levels that would be
208 crossed by a backward branch to the label. */
6625cdb5 209 tree bad_decls;
1799e5d5
RH
210
211 /* A list of uses of the label, before the label is defined. */
212 struct named_label_use_entry *uses;
213
214 /* The following bits are set after the label is defined, and are
215 updated as scopes are popped. They indicate that a backward jump
ddd0b831 216 to the label will illegally enter a scope of the given flavor. */
1799e5d5
RH
217 bool in_try_scope;
218 bool in_catch_scope;
219 bool in_omp_scope;
6625cdb5 220};
8d08fdba 221
4519c0a8 222#define named_labels cp_function_chain->x_named_labels
8d08fdba 223\f
b2244c65
MM
224/* The number of function bodies which we are currently processing.
225 (Zero if we are at namespace scope, one inside the body of a
226 function, two inside the body of a function in a local class, etc.) */
227int function_depth;
e23bd218
IR
228
229/* States indicating how grokdeclarator() should handle declspecs marked
230 with __attribute__((deprecated)). An object declared as
231 __attribute__((deprecated)) suppresses warnings of uses of other
232 deprecated items. */
caf93cb0 233
e23bd218
IR
234enum deprecated_states {
235 DEPRECATED_NORMAL,
236 DEPRECATED_SUPPRESS
237};
238
239static enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
240
8d08fdba 241\f
0154eaa8
MM
242/* A TREE_LIST of VAR_DECLs. The TREE_PURPOSE is a RECORD_TYPE or
243 UNION_TYPE; the TREE_VALUE is a VAR_DECL with that type. At the
244 time the VAR_DECL was declared, the type was incomplete. */
245
e2500fed 246static GTY(()) tree incomplete_vars;
8d08fdba 247\f
74b846e0
MM
248/* Returns the kind of template specialization we are currently
249 processing, given that it's declaration contained N_CLASS_SCOPES
250 explicit scope qualifications. */
251
252tmpl_spec_kind
11f6b451 253current_tmpl_spec_kind (int n_class_scopes)
74b846e0
MM
254{
255 int n_template_parm_scopes = 0;
256 int seen_specialization_p = 0;
257 int innermost_specialization_p = 0;
e2500fed 258 struct cp_binding_level *b;
74b846e0
MM
259
260 /* Scan through the template parameter scopes. */
caf93cb0
EC
261 for (b = current_binding_level;
262 b->kind == sk_template_parms;
a7e8c268 263 b = b->level_chain)
74b846e0
MM
264 {
265 /* If we see a specialization scope inside a parameter scope,
266 then something is wrong. That corresponds to a declaration
267 like:
268
269 template <class T> template <> ...
270
0e339752 271 which is always invalid since [temp.expl.spec] forbids the
74b846e0
MM
272 specialization of a class member template if the enclosing
273 class templates are not explicitly specialized as well. */
a7e8c268 274 if (b->explicit_spec_p)
74b846e0
MM
275 {
276 if (n_template_parm_scopes == 0)
277 innermost_specialization_p = 1;
278 else
279 seen_specialization_p = 1;
280 }
281 else if (seen_specialization_p == 1)
282 return tsk_invalid_member_spec;
283
284 ++n_template_parm_scopes;
285 }
286
287 /* Handle explicit instantiations. */
288 if (processing_explicit_instantiation)
289 {
290 if (n_template_parm_scopes != 0)
291 /* We've seen a template parameter list during an explicit
292 instantiation. For example:
293
294 template <class T> template void f(int);
295
296 This is erroneous. */
297 return tsk_invalid_expl_inst;
298 else
299 return tsk_expl_inst;
300 }
301
302 if (n_template_parm_scopes < n_class_scopes)
303 /* We've not seen enough template headers to match all the
304 specialized classes present. For example:
305
0cbd7506 306 template <class T> void R<T>::S<T>::f(int);
74b846e0 307
0e339752 308 This is invalid; there needs to be one set of template
74b846e0
MM
309 parameters for each class. */
310 return tsk_insufficient_parms;
311 else if (n_template_parm_scopes == n_class_scopes)
312 /* We're processing a non-template declaration (even though it may
313 be a member of a template class.) For example:
314
0cbd7506 315 template <class T> void S<T>::f(int);
74b846e0
MM
316
317 The `class T' maches the `S<T>', leaving no template headers
318 corresponding to the `f'. */
319 return tsk_none;
320 else if (n_template_parm_scopes > n_class_scopes + 1)
321 /* We've got too many template headers. For example:
322
0cbd7506 323 template <> template <class T> void f (T);
74b846e0
MM
324
325 There need to be more enclosing classes. */
326 return tsk_excessive_parms;
327 else
328 /* This must be a template. It's of the form:
329
0cbd7506 330 template <class T> template <class U> void S<T>::f(U);
74b846e0
MM
331
332 This is a specialization if the innermost level was a
333 specialization; otherwise it's just a definition of the
334 template. */
335 return innermost_specialization_p ? tsk_expl_spec : tsk_template;
8d08fdba
MS
336}
337
74b846e0
MM
338/* Exit the current scope. */
339
340void
11f6b451 341finish_scope (void)
74b846e0
MM
342{
343 poplevel (0, 0, 0);
344}
345
acef433b
MM
346/* When a label goes out of scope, check to see if that label was used
347 in a valid manner, and issue any appropriate warnings or errors. */
348
349static void
11f6b451 350pop_label (tree label, tree old_value)
acef433b 351{
a7e8c268 352 if (!processing_template_decl)
acef433b 353 {
88848bde
MM
354 if (DECL_INITIAL (label) == NULL_TREE)
355 {
5b030314
NS
356 location_t location;
357
dee15844 358 error ("label %q+D used but not defined", label);
93409b8c
PB
359#ifdef USE_MAPPED_LOCATION
360 location = input_location; /* FIXME want (input_filename, (line)0) */
361#else
0cbd7506 362 location.file = input_filename;
5b030314 363 location.line = 0;
93409b8c 364#endif
88848bde 365 /* Avoid crashing later. */
5b030314 366 define_label (location, DECL_NAME (label));
88848bde 367 }
c616e51b
MLI
368 else
369 warn_for_unused_label (label);
acef433b 370 }
acef433b 371
6625cdb5 372 SET_IDENTIFIER_LABEL_VALUE (DECL_NAME (label), old_value);
acef433b
MM
373}
374
0811ea8f 375/* At the end of a function, all labels declared within the function
68642fb6 376 go out of scope. BLOCK is the top-level block for the
acef433b
MM
377 function. */
378
1799e5d5
RH
379static int
380pop_labels_1 (void **slot, void *data)
381{
382 struct named_label_entry *ent = (struct named_label_entry *) *slot;
383 tree block = (tree) data;
384
385 pop_label (ent->label_decl, NULL_TREE);
386
387 /* Put the labels into the "variables" of the top-level block,
388 so debugger can see them. */
389 TREE_CHAIN (ent->label_decl) = BLOCK_VARS (block);
390 BLOCK_VARS (block) = ent->label_decl;
391
392 htab_clear_slot (named_labels, slot);
393
394 return 1;
395}
396
acef433b 397static void
11f6b451 398pop_labels (tree block)
acef433b 399{
1799e5d5 400 if (named_labels)
acef433b 401 {
1799e5d5
RH
402 htab_traverse (named_labels, pop_labels_1, block);
403 named_labels = NULL;
acef433b 404 }
1799e5d5
RH
405}
406
407/* At the end of a block with local labels, restore the outer definition. */
acef433b 408
1799e5d5
RH
409static void
410pop_local_label (tree label, tree old_value)
411{
412 struct named_label_entry dummy;
413 void **slot;
414
415 pop_label (label, old_value);
416
417 dummy.label_decl = label;
418 slot = htab_find_slot (named_labels, &dummy, NO_INSERT);
419 htab_clear_slot (named_labels, slot);
acef433b
MM
420}
421
80aa8340
ZL
422/* The following two routines are used to interface to Objective-C++.
423 The binding level is purposely treated as an opaque type. */
424
425void *
426objc_get_current_scope (void)
427{
428 return current_binding_level;
429}
430
431/* The following routine is used by the NeXT-style SJLJ exceptions;
432 variables get marked 'volatile' so as to not be clobbered by
f749f3c2
ZL
433 _setjmp()/_longjmp() calls. All variables in the current scope,
434 as well as parent scopes up to (but not including) ENCLOSING_BLK
435 shall be thusly marked. */
80aa8340
ZL
436
437void
438objc_mark_locals_volatile (void *enclosing_blk)
439{
440 struct cp_binding_level *scope;
441
442 for (scope = current_binding_level;
e58a9aa1 443 scope && scope != enclosing_blk;
80aa8340
ZL
444 scope = scope->level_chain)
445 {
446 tree decl;
447
448 for (decl = scope->names; decl; decl = TREE_CHAIN (decl))
660845bf 449 objc_volatilize_decl (decl);
e58a9aa1
ZL
450
451 /* Do not climb up past the current function. */
452 if (scope->kind == sk_function_parms)
453 break;
80aa8340
ZL
454 }
455}
456
1799e5d5
RH
457/* Update data for defined and undefined labels when leaving a scope. */
458
459static int
460poplevel_named_label_1 (void **slot, void *data)
461{
462 struct named_label_entry *ent = (struct named_label_entry *) *slot;
463 struct cp_binding_level *bl = (struct cp_binding_level *) data;
464 struct cp_binding_level *obl = bl->level_chain;
465
466 if (ent->binding_level == bl)
467 {
468 tree decl;
469
470 for (decl = ent->names_in_scope; decl; decl = TREE_CHAIN (decl))
471 if (decl_jump_unsafe (decl))
472 ent->bad_decls = tree_cons (NULL, decl, ent->bad_decls);
473
474 ent->binding_level = obl;
475 ent->names_in_scope = obl->names;
476 switch (bl->kind)
477 {
478 case sk_try:
479 ent->in_try_scope = true;
480 break;
481 case sk_catch:
482 ent->in_catch_scope = true;
483 break;
484 case sk_omp:
485 ent->in_omp_scope = true;
486 break;
487 default:
488 break;
489 }
490 }
491 else if (ent->uses)
492 {
493 struct named_label_use_entry *use;
494
495 for (use = ent->uses; use ; use = use->next)
496 if (use->binding_level == bl)
497 {
498 use->binding_level = obl;
499 use->names_in_scope = obl->names;
500 if (bl->kind == sk_omp)
501 use->in_omp_scope = true;
502 }
503 }
504
505 return 1;
506}
507
8d08fdba
MS
508/* Exit a binding level.
509 Pop the level off, and restore the state of the identifier-decl mappings
510 that were in effect when this level was entered.
511
512 If KEEP == 1, this level had explicit declarations, so
513 and create a "block" (a BLOCK node) for the level
514 to record its declarations and subblocks for symbol table output.
515
8d08fdba
MS
516 If FUNCTIONBODY is nonzero, this level is the body of a function,
517 so create a block as if KEEP were set and also clear out all
518 label names.
519
520 If REVERSE is nonzero, reverse the order of decls before putting
521 them into the BLOCK. */
522
523tree
11f6b451 524poplevel (int keep, int reverse, int functionbody)
8d08fdba 525{
926ce8bd 526 tree link;
8d08fdba
MS
527 /* The chain of decls was accumulated in reverse order.
528 Put it into forward order, just for cleanliness. */
529 tree decls;
530 int tmp = functionbody;
b35d4555 531 int real_functionbody;
b35d4555 532 tree subblocks;
325c3691 533 tree block;
8d08fdba 534 tree decl;
f181d4ae 535 int leaving_for_scope;
ac20c67a 536 scope_kind kind;
f181d4ae 537
22ffcc6f 538 timevar_push (TV_NAME_LOOKUP);
325c3691
RH
539 restart:
540
541 block = NULL_TREE;
b35d4555 542
50bc768d 543 gcc_assert (current_binding_level->kind != sk_class);
b74a0560 544
ac20c67a 545 real_functionbody = (current_binding_level->kind == sk_cleanup
b35d4555 546 ? ((functionbody = 0), tmp) : functionbody);
b35d4555
MM
547 subblocks = functionbody >= 0 ? current_binding_level->blocks : 0;
548
50bc768d
NS
549 gcc_assert (!VEC_length(cp_class_binding,
550 current_binding_level->class_shadowed));
8d08fdba 551
536333d4
MM
552 /* We used to use KEEP == 2 to indicate that the new block should go
553 at the beginning of the list of blocks at this binding level,
554 rather than the end. This hack is no longer used. */
50bc768d 555 gcc_assert (keep == 0 || keep == 1);
536333d4 556
ac20c67a 557 if (current_binding_level->keep)
8d08fdba
MS
558 keep = 1;
559
6625cdb5
JM
560 /* Any uses of undefined labels, and any defined labels, now operate
561 under constraints of next binding contour. */
1799e5d5
RH
562 if (cfun && !functionbody && named_labels)
563 htab_traverse (named_labels, poplevel_named_label_1,
564 current_binding_level);
6625cdb5 565
8d08fdba
MS
566 /* Get the decls in the order they were written.
567 Usually current_binding_level->names is in reverse order.
568 But parameter decls were previously put in forward order. */
569
570 if (reverse)
571 current_binding_level->names
572 = decls = nreverse (current_binding_level->names);
573 else
574 decls = current_binding_level->names;
575
8d08fdba
MS
576 /* If there were any declarations or structure tags in that level,
577 or if this level is a function body,
578 create a BLOCK to record them for the life of this function. */
8d08fdba 579 block = NULL_TREE;
f444e36b 580 if (keep == 1 || functionbody)
8d08fdba
MS
581 block = make_node (BLOCK);
582 if (block != NULL_TREE)
583 {
f444e36b
MM
584 BLOCK_VARS (block) = decls;
585 BLOCK_SUBBLOCKS (block) = subblocks;
8d08fdba
MS
586 }
587
588 /* In each subblock, record that this is its superior. */
8d08fdba
MS
589 if (keep >= 0)
590 for (link = subblocks; link; link = TREE_CHAIN (link))
591 BLOCK_SUPERCONTEXT (link) = block;
592
f181d4ae
MM
593 /* We still support the old for-scope rules, whereby the variables
594 in a for-init statement were in scope after the for-statement
92bc1323 595 ended. We only use the new rules if flag_new_for_scope is
f181d4ae 596 nonzero. */
68642fb6 597 leaving_for_scope
a7e8c268 598 = current_binding_level->kind == sk_for && flag_new_for_scope == 1;
f181d4ae 599
caf93cb0
EC
600 /* Before we remove the declarations first check for unused variables. */
601 if (warn_unused_variable
602 && !processing_template_decl)
603 for (decl = getdecls (); decl; decl = TREE_CHAIN (decl))
604 if (TREE_CODE (decl) == VAR_DECL
605 && ! TREE_USED (decl)
606 && ! DECL_IN_SYSTEM_HEADER (decl)
607 && DECL_NAME (decl) && ! DECL_ARTIFICIAL (decl))
b323323f 608 warning (OPT_Wunused_variable, "unused variable %q+D", decl);
caf93cb0 609
f181d4ae
MM
610 /* Remove declarations for all the DECLs in this level. */
611 for (link = decls; link; link = TREE_CHAIN (link))
612 {
c3783399 613 if (leaving_for_scope && TREE_CODE (link) == VAR_DECL
0cbd7506 614 && DECL_NAME (link))
f181d4ae 615 {
90ea9897
MM
616 tree name = DECL_NAME (link);
617 cxx_binding *ob;
f181d4ae
MM
618 tree ns_binding;
619
90ea9897
MM
620 ob = outer_binding (name,
621 IDENTIFIER_BINDING (name),
622 /*class_p=*/true);
623 if (!ob)
624 ns_binding = IDENTIFIER_NAMESPACE_VALUE (name);
c7dda1e3
MM
625 else
626 ns_binding = NULL_TREE;
627
90ea9897 628 if (ob && ob->scope == current_binding_level->level_chain)
f181d4ae 629 /* We have something like:
68642fb6 630
0cbd7506
MS
631 int i;
632 for (int i; ;);
68642fb6 633
f181d4ae
MM
634 and we are leaving the `for' scope. There's no reason to
635 keep the binding of the inner `i' in this case. */
90ea9897
MM
636 pop_binding (name, link);
637 else if ((ob && (TREE_CODE (ob->value) == TYPE_DECL))
147135cc 638 || (ns_binding && TREE_CODE (ns_binding) == TYPE_DECL))
f181d4ae
MM
639 /* Here, we have something like:
640
641 typedef int I;
642
643 void f () {
644 for (int I; ;);
645 }
646
647 We must pop the for-scope binding so we know what's a
648 type and what isn't. */
90ea9897 649 pop_binding (name, link);
e76a2646 650 else
e76a2646 651 {
f181d4ae
MM
652 /* Mark this VAR_DECL as dead so that we can tell we left it
653 there only for backward compatibility. */
654 DECL_DEAD_FOR_LOCAL (link) = 1;
68642fb6 655
517f3315 656 /* Keep track of what should have happened when we
f181d4ae 657 popped the binding. */
90ea9897 658 if (ob && ob->value)
820cc88f
DB
659 {
660 SET_DECL_SHADOWED_FOR_VAR (link, ob->value);
661 DECL_HAS_SHADOWED_FOR_VAR_P (link) = 1;
662 }
f181d4ae
MM
663
664 /* Add it to the list of dead variables in the next
665 outermost binding to that we can remove these when we
666 leave that binding. */
667 current_binding_level->level_chain->dead_vars_from_for
668 = tree_cons (NULL_TREE, link,
669 current_binding_level->level_chain->
670 dead_vars_from_for);
671
af6fd53f 672 /* Although we don't pop the cxx_binding, we do clear
147135cc 673 its SCOPE since the scope is going away now. */
caf93cb0 674 IDENTIFIER_BINDING (name)->scope
90ea9897 675 = current_binding_level->level_chain;
e76a2646 676 }
2ee887f2 677 }
68642fb6 678 else
8d6e462b 679 {
8dc2b103 680 tree name;
c8094d83 681
f181d4ae 682 /* Remove the binding. */
0034cf72 683 decl = link;
caf93cb0 684
0034cf72
JM
685 if (TREE_CODE (decl) == TREE_LIST)
686 decl = TREE_VALUE (decl);
8dc2b103 687 name = decl;
c8094d83 688
8dc2b103
NS
689 if (TREE_CODE (name) == OVERLOAD)
690 name = OVL_FUNCTION (name);
caf93cb0 691
8dc2b103
NS
692 gcc_assert (DECL_P (name));
693 pop_binding (DECL_NAME (name), decl);
8d08fdba 694 }
f181d4ae 695 }
8d08fdba 696
f181d4ae
MM
697 /* Remove declarations for any `for' variables from inner scopes
698 that we kept around. */
699 for (link = current_binding_level->dead_vars_from_for;
700 link; link = TREE_CHAIN (link))
d8f8dca1 701 pop_binding (DECL_NAME (TREE_VALUE (link)), TREE_VALUE (link));
2ee887f2 702
f181d4ae
MM
703 /* Restore the IDENTIFIER_TYPE_VALUEs. */
704 for (link = current_binding_level->type_shadowed;
705 link; link = TREE_CHAIN (link))
706 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (link), TREE_VALUE (link));
acef433b
MM
707
708 /* Restore the IDENTIFIER_LABEL_VALUEs for local labels. */
709 for (link = current_binding_level->shadowed_labels;
68642fb6 710 link;
acef433b 711 link = TREE_CHAIN (link))
1799e5d5 712 pop_local_label (TREE_VALUE (link), TREE_PURPOSE (link));
acef433b 713
f181d4ae
MM
714 /* There may be OVERLOADs (wrapped in TREE_LISTs) on the BLOCK_VARs
715 list if a `using' declaration put them there. The debugging
3b426391 716 back ends won't understand OVERLOAD, so we remove them here.
f181d4ae
MM
717 Because the BLOCK_VARS are (temporarily) shared with
718 CURRENT_BINDING_LEVEL->NAMES we must do this fixup after we have
719 popped all the bindings. */
720 if (block)
721 {
722 tree* d;
8d6e462b 723
a06d48ef
JM
724 for (d = &BLOCK_VARS (block); *d; )
725 {
726 if (TREE_CODE (*d) == TREE_LIST)
727 *d = TREE_CHAIN (*d);
728 else
729 d = &TREE_CHAIN (*d);
730 }
8d6e462b 731 }
8d08fdba
MS
732
733 /* If the level being exited is the top level of a function,
734 check over all the labels. */
8d08fdba
MS
735 if (functionbody)
736 {
acef433b
MM
737 /* Since this is the top level block of a function, the vars are
738 the function's parameters. Don't leave them in the BLOCK
739 because they are found in the FUNCTION_DECL instead. */
8d08fdba 740 BLOCK_VARS (block) = 0;
acef433b 741 pop_labels (block);
8d08fdba
MS
742 }
743
ac20c67a 744 kind = current_binding_level->kind;
325c3691
RH
745 if (kind == sk_cleanup)
746 {
747 tree stmt;
748
749 /* If this is a temporary binding created for a cleanup, then we'll
750 have pushed a statement list level. Pop that, create a new
751 BIND_EXPR for the block, and insert it into the stream. */
752 stmt = pop_stmt_list (current_binding_level->statement_list);
753 stmt = c_build_bind_expr (block, stmt);
754 add_stmt (stmt);
755 }
8d08fdba 756
1a6daf15 757 leave_scope ();
8d08fdba 758 if (functionbody)
72c4a4ca
GK
759 {
760 /* The current function is being defined, so its DECL_INITIAL
761 should be error_mark_node. */
762 gcc_assert (DECL_INITIAL (current_function_decl) == error_mark_node);
763 DECL_INITIAL (current_function_decl) = block;
764 }
8d08fdba 765 else if (block)
f444e36b
MM
766 current_binding_level->blocks
767 = chainon (current_binding_level->blocks, block);
768
8d08fdba
MS
769 /* If we did not make a block for the level just exited,
770 any blocks made for inner levels
771 (since they cannot be recorded as subblocks in that level)
772 must be carried forward so they will later become subblocks
773 of something else. */
774 else if (subblocks)
536333d4
MM
775 current_binding_level->blocks
776 = chainon (current_binding_level->blocks, subblocks);
8d08fdba 777
d9b2d9da
MM
778 /* Each and every BLOCK node created here in `poplevel' is important
779 (e.g. for proper debugging information) so if we created one
780 earlier, mark it as "used". */
781 if (block)
782 TREE_USED (block) = 1;
783
325c3691 784 /* All temporary bindings created for cleanups are popped silently. */
ac20c67a 785 if (kind == sk_cleanup)
325c3691 786 goto restart;
8d08fdba 787
22ffcc6f 788 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, block);
8d08fdba
MS
789}
790
8d08fdba
MS
791/* Insert BLOCK at the end of the list of subblocks of the
792 current binding level. This is used when a BIND_EXPR is expanded,
793 to handle the BLOCK node inside the BIND_EXPR. */
794
795void
11f6b451 796insert_block (tree block)
8d08fdba
MS
797{
798 TREE_USED (block) = 1;
799 current_binding_level->blocks
800 = chainon (current_binding_level->blocks, block);
801}
802
0aafb128
MM
803/* Walk all the namespaces contained NAMESPACE, including NAMESPACE
804 itself, calling F for each. The DATA is passed to F as well. */
805
00e8de68
GDR
806static int
807walk_namespaces_r (tree namespace, walk_namespaces_fn f, void* data)
808{
809 int result = 0;
caf93cb0 810 tree current = NAMESPACE_LEVEL (namespace)->namespaces;
8d08fdba 811
00e8de68 812 result |= (*f) (namespace, data);
8d08fdba 813
00e8de68
GDR
814 for (; current; current = TREE_CHAIN (current))
815 result |= walk_namespaces_r (current, f, data);
fc0e7bf5 816
00e8de68 817 return result;
5566b478
MS
818}
819
00e8de68
GDR
820/* Walk all the namespaces, calling F for each. The DATA is passed to
821 F as well. */
822
823int
824walk_namespaces (walk_namespaces_fn f, void* data)
5566b478 825{
00e8de68 826 return walk_namespaces_r (global_namespace, f, data);
8d08fdba
MS
827}
828
00e8de68
GDR
829/* Call wrapup_globals_declarations for the globals in NAMESPACE. If
830 DATA is non-NULL, this is the last time we will call
831 wrapup_global_declarations for this NAMESPACE. */
39c01e4c 832
00e8de68
GDR
833int
834wrapup_globals_for_namespace (tree namespace, void* data)
39c01e4c 835{
00e8de68 836 struct cp_binding_level *level = NAMESPACE_LEVEL (namespace);
9857866d
KH
837 VEC(tree,gc) *statics = level->static_decls;
838 tree *vec = VEC_address (tree, statics);
839 int len = VEC_length (tree, statics);
00e8de68 840 int last_time = (data != 0);
68642fb6 841
00e8de68 842 if (last_time)
39c01e4c 843 {
00e8de68 844 check_global_declarations (vec, len);
db5f8b93 845 emit_debug_global_declarations (vec, len);
00e8de68 846 return 0;
39c01e4c
MM
847 }
848
00e8de68
GDR
849 /* Write out any globals that need to be output. */
850 return wrapup_global_declarations (vec, len);
39c01e4c
MM
851}
852
00e8de68 853\f
9188c363
MM
854/* In C++, you don't have to write `struct S' to refer to `S'; you
855 can just use `S'. We accomplish this by creating a TYPE_DECL as
856 if the user had written `typedef struct S S'. Create and return
857 the TYPE_DECL for TYPE. */
858
859tree
11f6b451 860create_implicit_typedef (tree name, tree type)
9188c363
MM
861{
862 tree decl;
863
864 decl = build_decl (TYPE_DECL, name, type);
c727aa5e 865 DECL_ARTIFICIAL (decl) = 1;
9188c363
MM
866 /* There are other implicit type declarations, like the one *within*
867 a class that allows you to write `S::S'. We must distinguish
868 amongst these. */
869 SET_DECL_IMPLICIT_TYPEDEF_P (decl);
870 TYPE_NAME (type) = decl;
871
872 return decl;
873}
874
8e4ce833
JJ
875/* Remember a local name for name-mangling purposes. */
876
877static void
11f6b451 878push_local_name (tree decl)
8e4ce833
JJ
879{
880 size_t i, nelts;
881 tree t, name;
882
22ffcc6f 883 timevar_push (TV_NAME_LOOKUP);
8e4ce833
JJ
884
885 name = DECL_NAME (decl);
886
9acaf84a 887 nelts = VEC_length (tree, local_names);
8e4ce833
JJ
888 for (i = 0; i < nelts; i++)
889 {
9acaf84a 890 t = VEC_index (tree, local_names, i);
8e4ce833
JJ
891 if (DECL_NAME (t) == name)
892 {
893 if (!DECL_LANG_SPECIFIC (decl))
894 retrofit_lang_decl (decl);
e2500fed 895 DECL_LANG_SPECIFIC (decl)->decl_flags.u2sel = 1;
8e4ce833
JJ
896 if (DECL_LANG_SPECIFIC (t))
897 DECL_DISCRIMINATOR (decl) = DECL_DISCRIMINATOR (t) + 1;
898 else
899 DECL_DISCRIMINATOR (decl) = 1;
900
9acaf84a 901 VEC_replace (tree, local_names, i, decl);
de94b46c
KG
902 timevar_pop (TV_NAME_LOOKUP);
903 return;
8e4ce833
JJ
904 }
905 }
906
9acaf84a 907 VEC_safe_push (tree, gc, local_names, decl);
22ffcc6f 908 timevar_pop (TV_NAME_LOOKUP);
8e4ce833 909}
8d08fdba
MS
910\f
911/* Subroutine of duplicate_decls: return truthvalue of whether
912 or not types of these decls match.
913
914 For C++, we must compare the parameter list so that `int' can match
915 `int&' in a parameter position, but `int&' is not confused with
916 `const int&'. */
e92cc029 917
6060a796 918int
11f6b451 919decls_match (tree newdecl, tree olddecl)
8d08fdba
MS
920{
921 int types_match;
922
347d73d7
ML
923 if (newdecl == olddecl)
924 return 1;
925
6b4b3deb
MM
926 if (TREE_CODE (newdecl) != TREE_CODE (olddecl))
927 /* If the two DECLs are not even the same kind of thing, we're not
928 interested in their types. */
929 return 0;
930
931 if (TREE_CODE (newdecl) == FUNCTION_DECL)
8d08fdba
MS
932 {
933 tree f1 = TREE_TYPE (newdecl);
934 tree f2 = TREE_TYPE (olddecl);
935 tree p1 = TYPE_ARG_TYPES (f1);
936 tree p2 = TYPE_ARG_TYPES (f2);
937
4f1c5b7d 938 if (CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl)
eb68cb58
MM
939 && ! (DECL_EXTERN_C_P (newdecl)
940 && DECL_EXTERN_C_P (olddecl)))
c5a6fc45
JM
941 return 0;
942
8d08fdba 943 if (TREE_CODE (f1) != TREE_CODE (f2))
0cbd7506 944 return 0;
8d08fdba 945
3bfdc719 946 if (same_type_p (TREE_TYPE (f1), TREE_TYPE (f2)))
8926095f 947 {
8b27e9ef
NS
948 if (p2 == NULL_TREE && DECL_EXTERN_C_P (olddecl)
949 && (DECL_BUILT_IN (olddecl)
950#ifndef NO_IMPLICIT_EXTERN_C
0cbd7506
MS
951 || (DECL_IN_SYSTEM_HEADER (newdecl) && !DECL_CLASS_SCOPE_P (newdecl))
952 || (DECL_IN_SYSTEM_HEADER (olddecl) && !DECL_CLASS_SCOPE_P (olddecl))
8b27e9ef
NS
953#endif
954 ))
a28e3c7f
MS
955 {
956 types_match = self_promoting_args_p (p1);
957 if (p1 == void_list_node)
958 TREE_TYPE (newdecl) = TREE_TYPE (olddecl);
959 }
8b27e9ef
NS
960#ifndef NO_IMPLICIT_EXTERN_C
961 else if (p1 == NULL_TREE
962 && (DECL_EXTERN_C_P (olddecl)
0cbd7506
MS
963 && DECL_IN_SYSTEM_HEADER (olddecl)
964 && !DECL_CLASS_SCOPE_P (olddecl))
8b27e9ef 965 && (DECL_EXTERN_C_P (newdecl)
0cbd7506
MS
966 && DECL_IN_SYSTEM_HEADER (newdecl)
967 && !DECL_CLASS_SCOPE_P (newdecl)))
a28e3c7f
MS
968 {
969 types_match = self_promoting_args_p (p2);
970 TREE_TYPE (newdecl) = TREE_TYPE (olddecl);
971 }
8b27e9ef 972#endif
8926095f 973 else
91063b51 974 types_match = compparms (p1, p2);
8926095f 975 }
8d08fdba
MS
976 else
977 types_match = 0;
978 }
6b4b3deb 979 else if (TREE_CODE (newdecl) == TEMPLATE_DECL)
51c184be 980 {
2bb5d995
JM
981 if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl))
982 != TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)))
983 return 0;
984
ee935db4
MM
985 if (!comp_template_parms (DECL_TEMPLATE_PARMS (newdecl),
986 DECL_TEMPLATE_PARMS (olddecl)))
987 return 0;
988
f84b4be9 989 if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL)
ee935db4
MM
990 types_match = same_type_p (TREE_TYPE (DECL_TEMPLATE_RESULT (olddecl)),
991 TREE_TYPE (DECL_TEMPLATE_RESULT (newdecl)));
f84b4be9
JM
992 else
993 types_match = decls_match (DECL_TEMPLATE_RESULT (olddecl),
994 DECL_TEMPLATE_RESULT (newdecl));
51c184be 995 }
8d08fdba
MS
996 else
997 {
91a5bc9f
KL
998 /* Need to check scope for variable declaration (VAR_DECL).
999 For typedef (TYPE_DECL), scope is ignored. */
1000 if (TREE_CODE (newdecl) == VAR_DECL
ace4831d
MM
1001 && CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl)
1002 /* [dcl.link]
1003 Two declarations for an object with C language linkage
1004 with the same name (ignoring the namespace that qualify
1005 it) that appear in different namespace scopes refer to
1006 the same object. */
1007 && !(DECL_EXTERN_C_P (olddecl) && DECL_EXTERN_C_P (newdecl)))
91a5bc9f
KL
1008 return 0;
1009
8d08fdba
MS
1010 if (TREE_TYPE (newdecl) == error_mark_node)
1011 types_match = TREE_TYPE (olddecl) == error_mark_node;
1012 else if (TREE_TYPE (olddecl) == NULL_TREE)
1013 types_match = TREE_TYPE (newdecl) == NULL_TREE;
a0a33927
MS
1014 else if (TREE_TYPE (newdecl) == NULL_TREE)
1015 types_match = 0;
8d08fdba 1016 else
01240200 1017 types_match = comptypes (TREE_TYPE (newdecl),
3bfdc719
MM
1018 TREE_TYPE (olddecl),
1019 COMPARE_REDECLARATION);
8d08fdba
MS
1020 }
1021
1022 return types_match;
1023}
1024
1025/* If NEWDECL is `static' and an `extern' was seen previously,
24f30ed4 1026 warn about it. OLDDECL is the previous declaration.
8d08fdba
MS
1027
1028 Note that this does not apply to the C++ case of declaring
1029 a variable `extern const' and then later `const'.
1030
8d08fdba
MS
1031 Don't complain about built-in functions, since they are beyond
1032 the user's control. */
1033
a5e6b29b 1034void
11f6b451 1035warn_extern_redeclared_static (tree newdecl, tree olddecl)
8d08fdba 1036{
24f30ed4
MM
1037 tree name;
1038
68642fb6 1039 if (TREE_CODE (newdecl) == TYPE_DECL
655dc6ee 1040 || TREE_CODE (newdecl) == TEMPLATE_DECL
a2a9e21c
GDR
1041 || TREE_CODE (newdecl) == CONST_DECL
1042 || TREE_CODE (newdecl) == NAMESPACE_DECL)
8d08fdba 1043 return;
68642fb6 1044
963d5758
MM
1045 /* Don't get confused by static member functions; that's a different
1046 use of `static'. */
1047 if (TREE_CODE (newdecl) == FUNCTION_DECL
1048 && DECL_STATIC_FUNCTION_P (newdecl))
1049 return;
8d08fdba 1050
24f30ed4
MM
1051 /* If the old declaration was `static', or the new one isn't, then
1052 then everything is OK. */
1053 if (DECL_THIS_STATIC (olddecl) || !DECL_THIS_STATIC (newdecl))
1054 return;
1055
1056 /* It's OK to declare a builtin function as `static'. */
1057 if (TREE_CODE (olddecl) == FUNCTION_DECL
1058 && DECL_ARTIFICIAL (olddecl))
1059 return;
1060
8d08fdba 1061 name = DECL_ASSEMBLER_NAME (newdecl);
2d01edd7 1062 pedwarn ("%qD was declared %<extern%> and later %<static%>", newdecl);
dee15844 1063 pedwarn ("previous declaration of %q+D", olddecl);
8d08fdba
MS
1064}
1065
e1f0e7a6
MM
1066/* NEW_DECL is a redeclaration of OLD_DECL; both are functions or
1067 function templates. If their exception specifications do not
b9704fc5 1068 match, issue a diagnostic. */
e1f0e7a6
MM
1069
1070static void
1071check_redeclaration_exception_specification (tree new_decl,
1072 tree old_decl)
1073{
1074 tree new_type;
1075 tree old_type;
1076 tree new_exceptions;
1077 tree old_exceptions;
1078
1079 new_type = TREE_TYPE (new_decl);
1080 new_exceptions = TYPE_RAISES_EXCEPTIONS (new_type);
1081 old_type = TREE_TYPE (old_decl);
1082 old_exceptions = TYPE_RAISES_EXCEPTIONS (old_type);
1083
1084 /* [except.spec]
1085
1086 If any declaration of a function has an exception-specification,
1087 all declarations, including the definition and an explicit
1088 specialization, of that function shall have an
1089 exception-specification with the same set of type-ids. */
1090 if ((pedantic || ! DECL_IN_SYSTEM_HEADER (old_decl))
1091 && ! DECL_IS_BUILTIN (old_decl)
1092 && flag_exceptions
3db45ab5 1093 && !comp_except_specs (new_exceptions, old_exceptions,
e1f0e7a6
MM
1094 /*exact=*/true))
1095 {
1096 error ("declaration of %qF throws different exceptions", new_decl);
1097 error ("from previous declaration %q+F", old_decl);
1098 }
1099}
1100
3a47c4e4
AO
1101#define GNU_INLINE_P(fn) (DECL_DECLARED_INLINE_P (fn) \
1102 && lookup_attribute ("gnu_inline", \
1103 DECL_ATTRIBUTES (fn)))
1104
b1a19c7c
MM
1105/* If NEWDECL is a redeclaration of OLDDECL, merge the declarations.
1106 If the redeclaration is invalid, a diagnostic is issued, and the
1107 error_mark_node is returned. Otherwise, OLDDECL is returned.
8d08fdba 1108
b1a19c7c 1109 If NEWDECL is not a redeclaration of OLDDECL, NULL_TREE is
d63d5d0c
ILT
1110 returned.
1111
1112 NEWDECL_IS_FRIEND is true if NEWDECL was declared as a friend. */
8d08fdba 1113
b1a19c7c 1114tree
d63d5d0c 1115duplicate_decls (tree newdecl, tree olddecl, bool newdecl_is_friend)
8d08fdba 1116{
8d08fdba 1117 unsigned olddecl_uid = DECL_UID (olddecl);
d63d5d0c 1118 int olddecl_friend = 0, types_match = 0, hidden_friend = 0;
0b60dfe3 1119 int new_defines_function = 0;
50b56799 1120 tree new_template;
5566b478
MS
1121
1122 if (newdecl == olddecl)
b1a19c7c 1123 return olddecl;
8d08fdba 1124
8926095f 1125 types_match = decls_match (newdecl, olddecl);
8d08fdba 1126
8d08fdba
MS
1127 /* If either the type of the new decl or the type of the old decl is an
1128 error_mark_node, then that implies that we have already issued an
1129 error (earlier) for some bogus type specification, and in that case,
1130 it is rather pointless to harass the user with yet more error message
0b60dfe3 1131 about the same declaration, so just pretend the types match here. */
bd6dd845
MS
1132 if (TREE_TYPE (newdecl) == error_mark_node
1133 || TREE_TYPE (olddecl) == error_mark_node)
dc5395cc 1134 return error_mark_node;
68642fb6 1135
97055d5c
AO
1136 if (DECL_P (olddecl)
1137 && TREE_CODE (newdecl) == FUNCTION_DECL
1138 && TREE_CODE (olddecl) == FUNCTION_DECL
1139 && (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl)))
1140 {
1141 if (DECL_DECLARED_INLINE_P (newdecl)
1142 && DECL_UNINLINABLE (newdecl)
1143 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
1144 /* Already warned elsewhere. */;
1145 else if (DECL_DECLARED_INLINE_P (olddecl)
1146 && DECL_UNINLINABLE (olddecl)
1147 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
1148 /* Already warned. */;
1149 else if (DECL_DECLARED_INLINE_P (newdecl)
1150 && DECL_UNINLINABLE (olddecl)
1151 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
1152 {
dee15844
JM
1153 warning (OPT_Wattributes, "function %q+D redeclared as inline",
1154 newdecl);
1155 warning (OPT_Wattributes, "previous declaration of %q+D "
1156 "with attribute noinline", olddecl);
97055d5c
AO
1157 }
1158 else if (DECL_DECLARED_INLINE_P (olddecl)
1159 && DECL_UNINLINABLE (newdecl)
1160 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
1161 {
dee15844
JM
1162 warning (OPT_Wattributes, "function %q+D redeclared with "
1163 "attribute noinline", newdecl);
1164 warning (OPT_Wattributes, "previous declaration of %q+D was inline",
1165 olddecl);
97055d5c
AO
1166 }
1167 }
1168
a1c65f9f 1169 /* Check for redeclaration and other discrepancies. */
d22c8596 1170 if (TREE_CODE (olddecl) == FUNCTION_DECL
9f33663b
JM
1171 && DECL_ARTIFICIAL (olddecl))
1172 {
d63d5d0c 1173 gcc_assert (!DECL_HIDDEN_FRIEND_P (olddecl));
9f33663b
JM
1174 if (TREE_CODE (newdecl) != FUNCTION_DECL)
1175 {
d63d5d0c
ILT
1176 /* Avoid warnings redeclaring built-ins which have not been
1177 explicitly declared. */
0cbd7506
MS
1178 if (DECL_ANTICIPATED (olddecl))
1179 return NULL_TREE;
d52e4867 1180
9f33663b
JM
1181 /* If you declare a built-in or predefined function name as static,
1182 the old definition is overridden, but optionally warn this was a
1183 bad choice of name. */
1184 if (! TREE_PUBLIC (newdecl))
1185 {
b323323f
LM
1186 warning (OPT_Wshadow, "shadowing %s function %q#D",
1187 DECL_BUILT_IN (olddecl) ? "built-in" : "library",
1188 olddecl);
9f33663b 1189 /* Discard the old built-in function. */
b1a19c7c 1190 return NULL_TREE;
9f33663b
JM
1191 }
1192 /* If the built-in is not ansi, then programs can override
1193 it even globally without an error. */
1194 else if (! DECL_BUILT_IN (olddecl))
d4ee4d25 1195 warning (0, "library function %q#D redeclared as non-function %q#D",
0cbd7506 1196 olddecl, newdecl);
9f33663b
JM
1197 else
1198 {
2d01edd7
GDR
1199 error ("declaration of %q#D", newdecl);
1200 error ("conflicts with built-in declaration %q#D",
0cbd7506 1201 olddecl);
9f33663b 1202 }
b1a19c7c 1203 return NULL_TREE;
a4443a08 1204 }
9f33663b 1205 else if (!types_match)
8d08fdba 1206 {
d63d5d0c
ILT
1207 /* Avoid warnings redeclaring built-ins which have not been
1208 explicitly declared. */
0cbd7506 1209 if (DECL_ANTICIPATED (olddecl))
498c0f27
JJ
1210 {
1211 /* Deal with fileptr_type_node. FILE type is not known
1212 at the time we create the builtins. */
1213 tree t1, t2;
1214
1215 for (t1 = TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
1216 t2 = TYPE_ARG_TYPES (TREE_TYPE (olddecl));
1217 t1 || t2;
1218 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1219 if (!t1 || !t2)
1220 break;
1221 else if (TREE_VALUE (t2) == fileptr_type_node)
1222 {
1223 tree t = TREE_VALUE (t1);
1224
1225 if (TREE_CODE (t) == POINTER_TYPE
1226 && TYPE_NAME (TREE_TYPE (t))
1227 && DECL_NAME (TYPE_NAME (TREE_TYPE (t)))
1228 == get_identifier ("FILE")
1229 && compparms (TREE_CHAIN (t1), TREE_CHAIN (t2)))
1230 {
1231 tree oldargs = TYPE_ARG_TYPES (TREE_TYPE (olddecl));
1232
1233 TYPE_ARG_TYPES (TREE_TYPE (olddecl))
1234 = TYPE_ARG_TYPES (TREE_TYPE (newdecl));
1235 types_match = decls_match (newdecl, olddecl);
1236 if (types_match)
d63d5d0c
ILT
1237 return duplicate_decls (newdecl, olddecl,
1238 newdecl_is_friend);
498c0f27
JJ
1239 TYPE_ARG_TYPES (TREE_TYPE (olddecl)) = oldargs;
1240 }
1241 }
1242 else if (! same_type_p (TREE_VALUE (t1), TREE_VALUE (t2)))
1243 break;
1244 }
d52e4867 1245 else if ((DECL_EXTERN_C_P (newdecl)
7e99327d
MM
1246 && DECL_EXTERN_C_P (olddecl))
1247 || compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
1248 TYPE_ARG_TYPES (TREE_TYPE (olddecl))))
a4443a08 1249 {
9f33663b
JM
1250 /* A near match; override the builtin. */
1251
1252 if (TREE_PUBLIC (newdecl))
a4443a08 1253 {
d4ee4d25
DD
1254 warning (0, "new declaration %q#D", newdecl);
1255 warning (0, "ambiguates built-in declaration %q#D",
0cbd7506 1256 olddecl);
a4443a08 1257 }
b323323f
LM
1258 else
1259 warning (OPT_Wshadow, "shadowing %s function %q#D",
0cbd7506
MS
1260 DECL_BUILT_IN (olddecl) ? "built-in" : "library",
1261 olddecl);
a4443a08 1262 }
9f33663b
JM
1263 else
1264 /* Discard the old built-in function. */
b1a19c7c 1265 return NULL_TREE;
04a9d080
RS
1266
1267 /* Replace the old RTL to avoid problems with inlining. */
84b8b0e0 1268 COPY_DECL_RTL (newdecl, olddecl);
8d08fdba 1269 }
d63d5d0c
ILT
1270 /* Even if the types match, prefer the new declarations type for
1271 built-ins which have not been explicitly declared, for
1272 exception lists, etc... */
10326855 1273 else if (DECL_ANTICIPATED (olddecl))
7072018e 1274 {
74869a85
NS
1275 tree type = TREE_TYPE (newdecl);
1276 tree attribs = (*targetm.merge_type_attributes)
1277 (TREE_TYPE (olddecl), type);
1278
e9525111 1279 type = cp_build_type_attribute_variant (type, attribs);
74869a85 1280 TREE_TYPE (newdecl) = TREE_TYPE (olddecl) = type;
7072018e 1281 }
68642fb6 1282
7e99327d
MM
1283 /* Whether or not the builtin can throw exceptions has no
1284 bearing on this declarator. */
1285 TREE_NOTHROW (olddecl) = 0;
1286
24f30ed4
MM
1287 if (DECL_THIS_STATIC (newdecl) && !DECL_THIS_STATIC (olddecl))
1288 {
1289 /* If a builtin function is redeclared as `static', merge
1290 the declarations, but make the original one static. */
1291 DECL_THIS_STATIC (olddecl) = 1;
1292 TREE_PUBLIC (olddecl) = 0;
9bfadf57 1293
421844e7
MM
1294 /* Make the old declaration consistent with the new one so
1295 that all remnants of the builtin-ness of this function
1296 will be banished. */
5d2ed28c 1297 SET_DECL_LANGUAGE (olddecl, DECL_LANGUAGE (newdecl));
84b8b0e0 1298 COPY_DECL_RTL (newdecl, olddecl);
24f30ed4 1299 }
39211cd5
MS
1300 }
1301 else if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
1302 {
6936461f
OW
1303 /* C++ Standard, 3.3, clause 4:
1304 "[Note: a namespace name or a class template name must be unique
1305 in its declarative region (7.3.2, clause 14). ]" */
1306 if (TREE_CODE (olddecl) != NAMESPACE_DECL
1307 && TREE_CODE (newdecl) != NAMESPACE_DECL
1308 && (TREE_CODE (olddecl) != TEMPLATE_DECL
1309 || TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) != TYPE_DECL)
1310 && (TREE_CODE (newdecl) != TEMPLATE_DECL
1311 || TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) != TYPE_DECL))
9ed182dc 1312 {
6936461f
OW
1313 if ((TREE_CODE (olddecl) == TYPE_DECL && DECL_ARTIFICIAL (olddecl)
1314 && TREE_CODE (newdecl) != TYPE_DECL)
1315 || (TREE_CODE (newdecl) == TYPE_DECL && DECL_ARTIFICIAL (newdecl)
1316 && TREE_CODE (olddecl) != TYPE_DECL))
1317 {
1318 /* We do nothing special here, because C++ does such nasty
1319 things with TYPE_DECLs. Instead, just let the TYPE_DECL
1320 get shadowed, and know that if we need to find a TYPE_DECL
1321 for a given name, we can look in the IDENTIFIER_TYPE_VALUE
1322 slot of the identifier. */
1323 return NULL_TREE;
1324 }
1325
1326 if ((TREE_CODE (newdecl) == FUNCTION_DECL
1327 && DECL_FUNCTION_TEMPLATE_P (olddecl))
1328 || (TREE_CODE (olddecl) == FUNCTION_DECL
1329 && DECL_FUNCTION_TEMPLATE_P (newdecl)))
1330 return NULL_TREE;
9ed182dc
JM
1331 }
1332
2d01edd7 1333 error ("%q#D redeclared as different kind of symbol", newdecl);
39211cd5
MS
1334 if (TREE_CODE (olddecl) == TREE_LIST)
1335 olddecl = TREE_VALUE (olddecl);
dee15844 1336 error ("previous declaration of %q+#D", olddecl);
39211cd5 1337
c0694c4b 1338 return error_mark_node;
8d08fdba 1339 }
8d08fdba
MS
1340 else if (!types_match)
1341 {
4f1c5b7d 1342 if (CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl))
7bdbfa05
MM
1343 /* These are certainly not duplicate declarations; they're
1344 from different scopes. */
b1a19c7c 1345 return NULL_TREE;
7bdbfa05 1346
8926095f 1347 if (TREE_CODE (newdecl) == TEMPLATE_DECL)
f0e01782
MS
1348 {
1349 /* The name of a class template may not be declared to refer to
1350 any other template, class, function, object, namespace, value,
e92cc029 1351 or type in the same scope. */
5566b478
MS
1352 if (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) == TYPE_DECL
1353 || TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL)
f0e01782 1354 {
2d01edd7 1355 error ("declaration of template %q#D", newdecl);
dee15844 1356 error ("conflicts with previous declaration %q+#D", olddecl);
f0e01782 1357 }
ec255269
MS
1358 else if (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) == FUNCTION_DECL
1359 && TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == FUNCTION_DECL
1360 && compparms (TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (olddecl))),
91063b51 1361 TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (newdecl))))
75650646 1362 && comp_template_parms (DECL_TEMPLATE_PARMS (newdecl),
b9201622
NS
1363 DECL_TEMPLATE_PARMS (olddecl))
1364 /* Template functions can be disambiguated by
1365 return type. */
1366 && same_type_p (TREE_TYPE (TREE_TYPE (newdecl)),
1367 TREE_TYPE (TREE_TYPE (olddecl))))
ec255269 1368 {
2d01edd7 1369 error ("new declaration %q#D", newdecl);
dee15844 1370 error ("ambiguates old declaration %q+#D", olddecl);
ec255269 1371 }
b1a19c7c 1372 return NULL_TREE;
f0e01782 1373 }
8926095f
MS
1374 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1375 {
eb68cb58 1376 if (DECL_EXTERN_C_P (newdecl) && DECL_EXTERN_C_P (olddecl))
8926095f 1377 {
2d01edd7 1378 error ("declaration of C function %q#D conflicts with",
0cbd7506 1379 newdecl);
dee15844 1380 error ("previous declaration %q+#D here", olddecl);
8926095f 1381 }
00595019 1382 else if (compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
91063b51 1383 TYPE_ARG_TYPES (TREE_TYPE (olddecl))))
700f8a87 1384 {
2d01edd7 1385 error ("new declaration %q#D", newdecl);
dee15844 1386 error ("ambiguates old declaration %q+#D", olddecl);
492b73bd 1387 return error_mark_node;
700f8a87
MS
1388 }
1389 else
b1a19c7c 1390 return NULL_TREE;
8926095f 1391 }
58ec3cc5 1392 else
8d08fdba 1393 {
2d01edd7 1394 error ("conflicting declaration %q#D", newdecl);
dee15844 1395 error ("%q+D has a previous declaration as %q#D", olddecl, olddecl);
0cbd7506 1396 return error_mark_node;
8d08fdba
MS
1397 }
1398 }
68642fb6 1399 else if (TREE_CODE (newdecl) == FUNCTION_DECL
75650646
MM
1400 && ((DECL_TEMPLATE_SPECIALIZATION (olddecl)
1401 && (!DECL_TEMPLATE_INFO (newdecl)
68642fb6 1402 || (DECL_TI_TEMPLATE (newdecl)
75650646
MM
1403 != DECL_TI_TEMPLATE (olddecl))))
1404 || (DECL_TEMPLATE_SPECIALIZATION (newdecl)
1405 && (!DECL_TEMPLATE_INFO (olddecl)
68642fb6 1406 || (DECL_TI_TEMPLATE (olddecl)
75650646 1407 != DECL_TI_TEMPLATE (newdecl))))))
386b8a85
JM
1408 /* It's OK to have a template specialization and a non-template
1409 with the same type, or to have specializations of two
75650646
MM
1410 different templates with the same type. Note that if one is a
1411 specialization, and the other is an instantiation of the same
1412 template, that we do not exit at this point. That situation
1413 can occur if we instantiate a template class, and then
0e339752 1414 specialize one of its methods. This situation is valid, but
75650646 1415 the declarations must be merged in the usual way. */
b1a19c7c 1416 return NULL_TREE;
68642fb6
UD
1417 else if (TREE_CODE (newdecl) == FUNCTION_DECL
1418 && ((DECL_TEMPLATE_INSTANTIATION (olddecl)
75650646
MM
1419 && !DECL_USE_TEMPLATE (newdecl))
1420 || (DECL_TEMPLATE_INSTANTIATION (newdecl)
1421 && !DECL_USE_TEMPLATE (olddecl))))
1422 /* One of the declarations is a template instantiation, and the
1423 other is not a template at all. That's OK. */
b1a19c7c 1424 return NULL_TREE;
c30b4add
MM
1425 else if (TREE_CODE (newdecl) == NAMESPACE_DECL)
1426 {
1427 /* In [namespace.alias] we have:
c8094d83 1428
0cbd7506 1429 In a declarative region, a namespace-alias-definition can be
c30b4add
MM
1430 used to redefine a namespace-alias declared in that declarative
1431 region to refer only to the namespace to which it already
1432 refers.
c8094d83 1433
c30b4add
MM
1434 Therefore, if we encounter a second alias directive for the same
1435 alias, we can just ignore the second directive. */
1436 if (DECL_NAMESPACE_ALIAS (newdecl)
c8094d83 1437 && (DECL_NAMESPACE_ALIAS (newdecl)
c30b4add
MM
1438 == DECL_NAMESPACE_ALIAS (olddecl)))
1439 return olddecl;
1440 /* [namespace.alias]
1441
0cbd7506 1442 A namespace-name or namespace-alias shall not be declared as
c30b4add
MM
1443 the name of any other entity in the same declarative region.
1444 A namespace-name defined at global scope shall not be
77880ae4 1445 declared as the name of any other entity in any global scope
c30b4add 1446 of the program. */
2d01edd7 1447 error ("declaration of namespace %qD conflicts with", newdecl);
dee15844 1448 error ("previous declaration of namespace %q+D here", olddecl);
c30b4add
MM
1449 return error_mark_node;
1450 }
8d08fdba
MS
1451 else
1452 {
d8e178a0 1453 const char *errmsg = redeclaration_error_message (newdecl, olddecl);
8251199e 1454 if (errmsg)
8d08fdba 1455 {
33bd39a2 1456 error (errmsg, newdecl);
8d08fdba 1457 if (DECL_NAME (olddecl) != NULL_TREE)
dee15844
JM
1458 error ((DECL_INITIAL (olddecl) && namespace_bindings_p ())
1459 ? "%q+#D previously defined here"
1460 : "%q+#D previously declared here", olddecl);
b1a19c7c 1461 return error_mark_node;
8d08fdba
MS
1462 }
1463 else if (TREE_CODE (olddecl) == FUNCTION_DECL
1464 && DECL_INITIAL (olddecl) != NULL_TREE
1465 && TYPE_ARG_TYPES (TREE_TYPE (olddecl)) == NULL_TREE
1466 && TYPE_ARG_TYPES (TREE_TYPE (newdecl)) != NULL_TREE)
1467 {
1468 /* Prototype decl follows defn w/o prototype. */
dee15844 1469 warning (0, "prototype for %q+#D", newdecl);
d4ee4d25 1470 warning (0, "%Jfollows non-prototype definition here", olddecl);
8d08fdba 1471 }
ace4831d
MM
1472 else if ((TREE_CODE (olddecl) == FUNCTION_DECL
1473 || TREE_CODE (olddecl) == VAR_DECL)
8d08fdba 1474 && DECL_LANGUAGE (newdecl) != DECL_LANGUAGE (olddecl))
8926095f 1475 {
ace4831d
MM
1476 /* [dcl.link]
1477 If two declarations of the same function or object
1478 specify different linkage-specifications ..., the program
1479 is ill-formed.... Except for functions with C++ linkage,
1480 a function declaration without a linkage specification
1481 shall not precede the first linkage specification for
1482 that function. A function can be declared without a
1483 linkage specification after an explicit linkage
1484 specification has been seen; the linkage explicitly
1485 specified in the earlier declaration is not affected by
1486 such a function declaration.
1487
1488 DR 563 raises the question why the restrictions on
1489 functions should not also apply to objects. Older
1490 versions of G++ silently ignore the linkage-specification
1491 for this example:
1492
1493 namespace N {
1494 extern int i;
1495 extern "C" int i;
1496 }
1497
1498 which is clearly wrong. Therefore, we now treat objects
1499 like functions. */
46ccf50a 1500 if (current_lang_depth () == 0)
ace4831d
MM
1501 {
1502 /* There is no explicit linkage-specification, so we use
1503 the linkage from the previous declaration. */
1504 if (!DECL_LANG_SPECIFIC (newdecl))
1505 retrofit_lang_decl (newdecl);
1506 SET_DECL_LANGUAGE (newdecl, DECL_LANGUAGE (olddecl));
1507 }
8926095f
MS
1508 else
1509 {
dee15844
JM
1510 error ("previous declaration of %q+#D with %qL linkage",
1511 olddecl, DECL_LANGUAGE (olddecl));
2d01edd7 1512 error ("conflicts with new declaration with %qL linkage",
0cbd7506 1513 DECL_LANGUAGE (newdecl));
8926095f
MS
1514 }
1515 }
e1cd6e56 1516
a6f02587 1517 if (DECL_LANG_SPECIFIC (olddecl) && DECL_USE_TEMPLATE (olddecl))
c32381b1
JM
1518 ;
1519 else if (TREE_CODE (olddecl) == FUNCTION_DECL)
e1cd6e56
MS
1520 {
1521 tree t1 = TYPE_ARG_TYPES (TREE_TYPE (olddecl));
1522 tree t2 = TYPE_ARG_TYPES (TREE_TYPE (newdecl));
1523 int i = 1;
1524
1525 if (TREE_CODE (TREE_TYPE (newdecl)) == METHOD_TYPE)
1526 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2);
68642fb6 1527
e1cd6e56
MS
1528 for (; t1 && t1 != void_list_node;
1529 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2), i++)
1530 if (TREE_PURPOSE (t1) && TREE_PURPOSE (t2))
1531 {
2507f3b5
RK
1532 if (1 == simple_cst_equal (TREE_PURPOSE (t1),
1533 TREE_PURPOSE (t2)))
e1cd6e56 1534 {
2d01edd7 1535 pedwarn ("default argument given for parameter %d of %q#D",
624a5800 1536 i, newdecl);
dee15844 1537 pedwarn ("after previous specification in %q+#D", olddecl);
e1cd6e56
MS
1538 }
1539 else
1540 {
2d01edd7 1541 error ("default argument given for parameter %d of %q#D",
0cbd7506 1542 i, newdecl);
dee15844 1543 error ("after previous specification in %q+#D",
e1cd6e56
MS
1544 olddecl);
1545 }
1546 }
a5894242 1547
caf93cb0 1548 if (DECL_DECLARED_INLINE_P (newdecl)
79065db2 1549 && ! DECL_DECLARED_INLINE_P (olddecl)
7fcdf4c2 1550 && TREE_ADDRESSABLE (olddecl) && warn_inline)
dff6b454 1551 {
d4ee4d25
DD
1552 warning (0, "%q#D was used before it was declared inline", newdecl);
1553 warning (0, "%Jprevious non-inline declaration here", olddecl);
dff6b454 1554 }
e1cd6e56 1555 }
8d08fdba
MS
1556 }
1557
4782bd5b
RS
1558 /* Do not merge an implicit typedef with an explicit one. In:
1559
1560 class A;
1561 ...
1562 typedef class A A __attribute__ ((foo));
1563
1564 the attribute should apply only to the typedef. */
1565 if (TREE_CODE (olddecl) == TYPE_DECL
1566 && (DECL_IMPLICIT_TYPEDEF_P (olddecl)
1567 || DECL_IMPLICIT_TYPEDEF_P (newdecl)))
b1a19c7c 1568 return NULL_TREE;
4782bd5b 1569
8d08fdba
MS
1570 /* If new decl is `static' and an `extern' was seen previously,
1571 warn about it. */
1572 warn_extern_redeclared_static (newdecl, olddecl);
1573
e92cc029 1574 /* We have committed to returning 1 at this point. */
8d08fdba
MS
1575 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1576 {
1577 /* Now that functions must hold information normally held
1578 by field decls, there is extra work to do so that
1579 declaration information does not get destroyed during
1580 definition. */
1581 if (DECL_VINDEX (olddecl))
1582 DECL_VINDEX (newdecl) = DECL_VINDEX (olddecl);
1583 if (DECL_CONTEXT (olddecl))
1584 DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
e1cd6e56
MS
1585 DECL_STATIC_CONSTRUCTOR (newdecl) |= DECL_STATIC_CONSTRUCTOR (olddecl);
1586 DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
fee7654e 1587 DECL_PURE_VIRTUAL_P (newdecl) |= DECL_PURE_VIRTUAL_P (olddecl);
864b83b9 1588 DECL_VIRTUAL_P (newdecl) |= DECL_VIRTUAL_P (olddecl);
815951d8 1589 DECL_INVALID_OVERRIDER_P (newdecl) |= DECL_INVALID_OVERRIDER_P (olddecl);
963d5758 1590 DECL_THIS_STATIC (newdecl) |= DECL_THIS_STATIC (olddecl);
e0fff4b3
JM
1591 if (DECL_OVERLOADED_OPERATOR_P (olddecl) != ERROR_MARK)
1592 SET_OVERLOADED_OPERATOR_CODE
1593 (newdecl, DECL_OVERLOADED_OPERATOR_P (olddecl));
0b60dfe3 1594 new_defines_function = DECL_INITIAL (newdecl) != NULL_TREE;
68642fb6 1595
0b60dfe3 1596 /* Optionally warn about more than one declaration for the same
0cbd7506
MS
1597 name, but don't warn about a function declaration followed by a
1598 definition. */
0b60dfe3
BK
1599 if (warn_redundant_decls && ! DECL_ARTIFICIAL (olddecl)
1600 && !(new_defines_function && DECL_INITIAL (olddecl) == NULL_TREE)
a1c65f9f 1601 /* Don't warn about extern decl followed by definition. */
0b60dfe3 1602 && !(DECL_EXTERNAL (olddecl) && ! DECL_EXTERNAL (newdecl))
a1c65f9f 1603 /* Don't warn about friends, let add_friend take care of it. */
d63d5d0c 1604 && ! (newdecl_is_friend || DECL_FRIEND_P (olddecl)))
0b60dfe3 1605 {
b323323f
LM
1606 warning (OPT_Wredundant_decls, "redundant redeclaration of %qD in same scope", newdecl);
1607 warning (OPT_Wredundant_decls, "previous declaration of %q+D", olddecl);
0b60dfe3 1608 }
8d08fdba
MS
1609 }
1610
1611 /* Deal with C++: must preserve virtual function table size. */
1612 if (TREE_CODE (olddecl) == TYPE_DECL)
1613 {
926ce8bd
KH
1614 tree newtype = TREE_TYPE (newdecl);
1615 tree oldtype = TREE_TYPE (olddecl);
8d08fdba
MS
1616
1617 if (newtype != error_mark_node && oldtype != error_mark_node
1618 && TYPE_LANG_SPECIFIC (newtype) && TYPE_LANG_SPECIFIC (oldtype))
e93ee644
MM
1619 CLASSTYPE_FRIEND_CLASSES (newtype)
1620 = CLASSTYPE_FRIEND_CLASSES (oldtype);
7df87825 1621
970d6386 1622 DECL_ORIGINAL_TYPE (newdecl) = DECL_ORIGINAL_TYPE (olddecl);
8d08fdba
MS
1623 }
1624
8d08fdba
MS
1625 /* Copy all the DECL_... slots specified in the new decl
1626 except for any that we copy here from the old type. */
91d231cb 1627 DECL_ATTRIBUTES (newdecl)
f6897b10 1628 = (*targetm.merge_decl_attributes) (olddecl, newdecl);
8d08fdba 1629
5566b478
MS
1630 if (TREE_CODE (newdecl) == TEMPLATE_DECL)
1631 {
e1f0e7a6
MM
1632 tree old_result;
1633 tree new_result;
1634 old_result = DECL_TEMPLATE_RESULT (olddecl);
1635 new_result = DECL_TEMPLATE_RESULT (newdecl);
1636 TREE_TYPE (olddecl) = TREE_TYPE (old_result);
68642fb6 1637 DECL_TEMPLATE_SPECIALIZATIONS (olddecl)
4d85e00e
MM
1638 = chainon (DECL_TEMPLATE_SPECIALIZATIONS (olddecl),
1639 DECL_TEMPLATE_SPECIALIZATIONS (newdecl));
68642fb6 1640
3a47c4e4
AO
1641 DECL_ATTRIBUTES (old_result)
1642 = (*targetm.merge_decl_attributes) (old_result, new_result);
1643
e1f0e7a6
MM
1644 if (DECL_FUNCTION_TEMPLATE_P (newdecl))
1645 {
3a47c4e4
AO
1646 if (GNU_INLINE_P (old_result) != GNU_INLINE_P (new_result)
1647 && DECL_INITIAL (new_result))
1648 {
1649 if (DECL_INITIAL (old_result))
1650 {
1651 DECL_INLINE (old_result) = 0;
1652 DECL_UNINLINABLE (old_result) = 1;
1653 }
1654 else
1655 {
1656 DECL_INLINE (old_result) = DECL_INLINE (new_result);
1657 DECL_UNINLINABLE (old_result) = DECL_UNINLINABLE (new_result);
1658 }
1659 DECL_EXTERNAL (old_result) = DECL_EXTERNAL (new_result);
1660 DECL_NOT_REALLY_EXTERN (old_result)
1661 = DECL_NOT_REALLY_EXTERN (new_result);
1662 DECL_INTERFACE_KNOWN (old_result)
1663 = DECL_INTERFACE_KNOWN (new_result);
1664 DECL_DECLARED_INLINE_P (old_result)
1665 = DECL_DECLARED_INLINE_P (new_result);
1666 }
1667 else
1668 {
1669 DECL_INLINE (old_result)
1670 |= DECL_INLINE (new_result);
1671 DECL_DECLARED_INLINE_P (old_result)
1672 |= DECL_DECLARED_INLINE_P (new_result);
1673 check_redeclaration_exception_specification (newdecl, olddecl);
1674 }
e1f0e7a6
MM
1675 }
1676
daa8df65 1677 /* If the new declaration is a definition, update the file and
72c4a4ca
GK
1678 line information on the declaration, and also make
1679 the old declaration the same definition. */
3a47c4e4 1680 if (DECL_INITIAL (new_result) != NULL_TREE)
daa8df65 1681 {
caf93cb0 1682 DECL_SOURCE_LOCATION (olddecl)
e1f0e7a6 1683 = DECL_SOURCE_LOCATION (old_result)
f31686a3 1684 = DECL_SOURCE_LOCATION (newdecl);
72c4a4ca 1685 DECL_INITIAL (old_result) = DECL_INITIAL (new_result);
26bcf8fc 1686 if (DECL_FUNCTION_TEMPLATE_P (newdecl))
e1f0e7a6
MM
1687 DECL_ARGUMENTS (old_result)
1688 = DECL_ARGUMENTS (new_result);
c11889ce
MM
1689 }
1690
b1a19c7c 1691 return olddecl;
5566b478 1692 }
68642fb6 1693
8d08fdba
MS
1694 if (types_match)
1695 {
1696 /* Automatically handles default parameters. */
1697 tree oldtype = TREE_TYPE (olddecl);
e1cd6e56 1698 tree newtype;
8d08fdba 1699
e1cd6e56 1700 /* Merge the data types specified in the two decls. */
6da794e8 1701 newtype = merge_types (TREE_TYPE (newdecl), TREE_TYPE (olddecl));
e1cd6e56 1702
6da794e8 1703 /* If merge_types produces a non-typedef type, just use the old type. */
9076e292
JM
1704 if (TREE_CODE (newdecl) == TYPE_DECL
1705 && newtype == DECL_ORIGINAL_TYPE (newdecl))
1706 newtype = oldtype;
1707
8d08fdba 1708 if (TREE_CODE (newdecl) == VAR_DECL)
17bbb839
MM
1709 {
1710 DECL_THIS_EXTERN (newdecl) |= DECL_THIS_EXTERN (olddecl);
1711 DECL_INITIALIZED_P (newdecl) |= DECL_INITIALIZED_P (olddecl);
3db45ab5 1712 DECL_NONTRIVIALLY_INITIALIZED_P (newdecl)
6c06fbce 1713 |= DECL_NONTRIVIALLY_INITIALIZED_P (olddecl);
39703eb9
MM
1714 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (newdecl)
1715 |= DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (olddecl);
1799e5d5
RH
1716
1717 /* Merge the threadprivate attribute from OLDDECL into NEWDECL. */
1718 if (DECL_LANG_SPECIFIC (olddecl)
1719 && CP_DECL_THREADPRIVATE_P (olddecl))
1720 {
1721 /* Allocate a LANG_SPECIFIC structure for NEWDECL, if needed. */
1722 if (!DECL_LANG_SPECIFIC (newdecl))
1723 retrofit_lang_decl (newdecl);
1724
1725 DECL_TLS_MODEL (newdecl) = DECL_TLS_MODEL (olddecl);
1726 CP_DECL_THREADPRIVATE_P (newdecl) = 1;
1727 }
17bbb839
MM
1728 }
1729
6da794e8 1730 /* Do this after calling `merge_types' so that default
8d08fdba 1731 parameters don't confuse us. */
e1f0e7a6
MM
1732 else if (TREE_CODE (newdecl) == FUNCTION_DECL)
1733 check_redeclaration_exception_specification (newdecl, olddecl);
8d08fdba
MS
1734 TREE_TYPE (newdecl) = TREE_TYPE (olddecl) = newtype;
1735
4a2f6dc0
VR
1736 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1737 check_default_args (newdecl);
1738
8d08fdba 1739 /* Lay the type out, unless already done. */
407f03b8 1740 if (! same_type_p (newtype, oldtype)
5566b478 1741 && TREE_TYPE (newdecl) != error_mark_node
5156628f 1742 && !(processing_template_decl && uses_template_parms (newdecl)))
b7484fbe
MS
1743 layout_type (TREE_TYPE (newdecl));
1744
5566b478
MS
1745 if ((TREE_CODE (newdecl) == VAR_DECL
1746 || TREE_CODE (newdecl) == PARM_DECL
1747 || TREE_CODE (newdecl) == RESULT_DECL
1748 || TREE_CODE (newdecl) == FIELD_DECL
1749 || TREE_CODE (newdecl) == TYPE_DECL)
5156628f 1750 && !(processing_template_decl && uses_template_parms (newdecl)))
b7484fbe 1751 layout_decl (newdecl, 0);
8d08fdba
MS
1752
1753 /* Merge the type qualifiers. */
1754 if (TREE_READONLY (newdecl))
1755 TREE_READONLY (olddecl) = 1;
1756 if (TREE_THIS_VOLATILE (newdecl))
1757 TREE_THIS_VOLATILE (olddecl) = 1;
5cefa0d9
JM
1758 if (TREE_NOTHROW (newdecl))
1759 TREE_NOTHROW (olddecl) = 1;
8d08fdba 1760
302a2cc5
JM
1761 /* Merge deprecatedness. */
1762 if (TREE_DEPRECATED (newdecl))
1763 TREE_DEPRECATED (olddecl) = 1;
1764
8d08fdba 1765 /* Merge the initialization information. */
8926095f
MS
1766 if (DECL_INITIAL (newdecl) == NULL_TREE
1767 && DECL_INITIAL (olddecl) != NULL_TREE)
1768 {
1769 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
f31686a3 1770 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
b0d06515
MM
1771 if (CAN_HAVE_FULL_LANG_DECL_P (newdecl)
1772 && DECL_LANG_SPECIFIC (newdecl)
4d85e00e 1773 && DECL_LANG_SPECIFIC (olddecl))
69ee8086
RH
1774 {
1775 DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
1da326c3 1776 DECL_STRUCT_FUNCTION (newdecl) = DECL_STRUCT_FUNCTION (olddecl);
69ee8086 1777 }
8926095f 1778 }
39211cd5
MS
1779
1780 /* Merge the section attribute.
0cbd7506 1781 We want to issue an error if the sections conflict but that must be
39211cd5
MS
1782 done later in decl_attributes since we are called before attributes
1783 are assigned. */
1784 if (DECL_SECTION_NAME (newdecl) == NULL_TREE)
1785 DECL_SECTION_NAME (newdecl) = DECL_SECTION_NAME (olddecl);
1786
a157febd
GK
1787 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1788 {
1789 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
1790 |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
1331d16f 1791 DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
220a35cc
RS
1792 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1793 TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
1794 TREE_NOTHROW (newdecl) |= TREE_NOTHROW (olddecl);
1331d16f
RS
1795 DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
1796 DECL_IS_PURE (newdecl) |= DECL_IS_PURE (olddecl);
c740732f
MM
1797 /* Keep the old RTL. */
1798 COPY_DECL_RTL (olddecl, newdecl);
1799 }
caf93cb0 1800 else if (TREE_CODE (newdecl) == VAR_DECL
c740732f
MM
1801 && (DECL_SIZE (olddecl) || !DECL_SIZE (newdecl)))
1802 {
1803 /* Keep the old RTL. We cannot keep the old RTL if the old
1804 declaration was for an incomplete object and the new
1805 declaration is not since many attributes of the RTL will
1806 change. */
1807 COPY_DECL_RTL (olddecl, newdecl);
a157febd 1808 }
8d08fdba
MS
1809 }
1810 /* If cannot merge, then use the new type and qualifiers,
1811 and don't preserve the old rtl. */
1812 else
1813 {
1814 /* Clean out any memory we had of the old declaration. */
1815 tree oldstatic = value_member (olddecl, static_aggregates);
1816 if (oldstatic)
1817 TREE_VALUE (oldstatic) = error_mark_node;
1818
1819 TREE_TYPE (olddecl) = TREE_TYPE (newdecl);
1820 TREE_READONLY (olddecl) = TREE_READONLY (newdecl);
1821 TREE_THIS_VOLATILE (olddecl) = TREE_THIS_VOLATILE (newdecl);
1822 TREE_SIDE_EFFECTS (olddecl) = TREE_SIDE_EFFECTS (newdecl);
1823 }
1824
1825 /* Merge the storage class information. */
45806a3f
FS
1826 merge_weak (newdecl, olddecl);
1827
e92cc029 1828 DECL_ONE_ONLY (newdecl) |= DECL_ONE_ONLY (olddecl);
794d4a61 1829 DECL_DEFER_OUTPUT (newdecl) |= DECL_DEFER_OUTPUT (olddecl);
a9aedbc2
MS
1830 TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
1831 TREE_STATIC (olddecl) = TREE_STATIC (newdecl) |= TREE_STATIC (olddecl);
1832 if (! DECL_EXTERNAL (olddecl))
1833 DECL_EXTERNAL (newdecl) = 0;
68642fb6 1834
50b56799 1835 new_template = NULL_TREE;
0b60dfe3 1836 if (DECL_LANG_SPECIFIC (newdecl) && DECL_LANG_SPECIFIC (olddecl))
8d08fdba 1837 {
3a47c4e4
AO
1838 bool old_decl_gnu_inline;
1839
1840 if ((DECL_INTERFACE_KNOWN (olddecl)
1841 && TREE_CODE (olddecl) == FUNCTION_DECL)
1842 || (TREE_CODE (olddecl) == TEMPLATE_DECL
1843 && TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) == FUNCTION_DECL))
1844 {
1845 tree fn = olddecl;
1846
1847 if (TREE_CODE (fn) == TEMPLATE_DECL)
1848 fn = DECL_TEMPLATE_RESULT (olddecl);
1849
1850 old_decl_gnu_inline = GNU_INLINE_P (fn) && DECL_INITIAL (fn);
1851 }
1852 else
1853 old_decl_gnu_inline = false;
1854
1855 if (!old_decl_gnu_inline)
1856 {
1857 DECL_INTERFACE_KNOWN (newdecl) |= DECL_INTERFACE_KNOWN (olddecl);
1858 DECL_INTERFACE_KNOWN (newdecl) |= DECL_INTERFACE_KNOWN (olddecl);
1859 DECL_NOT_REALLY_EXTERN (newdecl) |= DECL_NOT_REALLY_EXTERN (olddecl);
1860 DECL_COMDAT (newdecl) |= DECL_COMDAT (olddecl);
1861 }
68642fb6 1862 DECL_TEMPLATE_INSTANTIATED (newdecl)
2b0a63a3 1863 |= DECL_TEMPLATE_INSTANTIATED (olddecl);
c8094d83 1864
58fb06b4
NS
1865 /* If the OLDDECL is an instantiation and/or specialization,
1866 then the NEWDECL must be too. But, it may not yet be marked
1867 as such if the caller has created NEWDECL, but has not yet
1868 figured out that it is a redeclaration. */
1869 if (!DECL_USE_TEMPLATE (newdecl))
1870 DECL_USE_TEMPLATE (newdecl) = DECL_USE_TEMPLATE (olddecl);
c8094d83 1871
0b60dfe3
BK
1872 /* Don't really know how much of the language-specific
1873 values we should copy from old to new. */
1874 DECL_IN_AGGR_P (newdecl) = DECL_IN_AGGR_P (olddecl);
caf93cb0 1875 DECL_LANG_SPECIFIC (newdecl)->decl_flags.u2 =
e2500fed 1876 DECL_LANG_SPECIFIC (olddecl)->decl_flags.u2;
0b60dfe3 1877 DECL_NONCONVERTING_P (newdecl) = DECL_NONCONVERTING_P (olddecl);
4684cd27 1878 DECL_REPO_AVAILABLE_P (newdecl) = DECL_REPO_AVAILABLE_P (olddecl);
50b56799
MM
1879 if (DECL_TEMPLATE_INFO (newdecl))
1880 new_template = DECL_TI_TEMPLATE (newdecl);
fbf1c34b 1881 DECL_TEMPLATE_INFO (newdecl) = DECL_TEMPLATE_INFO (olddecl);
3febd123 1882 DECL_INITIALIZED_IN_CLASS_P (newdecl)
0cbd7506 1883 |= DECL_INITIALIZED_IN_CLASS_P (olddecl);
0b60dfe3 1884 olddecl_friend = DECL_FRIEND_P (olddecl);
d63d5d0c
ILT
1885 hidden_friend = (DECL_ANTICIPATED (olddecl)
1886 && DECL_HIDDEN_FRIEND_P (olddecl)
1887 && newdecl_is_friend);
6a629cac
MM
1888
1889 /* Only functions have DECL_BEFRIENDING_CLASSES. */
1890 if (TREE_CODE (newdecl) == FUNCTION_DECL
1891 || DECL_FUNCTION_TEMPLATE_P (newdecl))
bb5e8a7f
MM
1892 {
1893 DECL_BEFRIENDING_CLASSES (newdecl)
1894 = chainon (DECL_BEFRIENDING_CLASSES (newdecl),
1895 DECL_BEFRIENDING_CLASSES (olddecl));
ca9e1382
KL
1896 /* DECL_THUNKS is only valid for virtual functions,
1897 otherwise it is a DECL_FRIEND_CONTEXT. */
1898 if (DECL_VIRTUAL_P (newdecl))
1899 DECL_THUNKS (newdecl) = DECL_THUNKS (olddecl);
bb5e8a7f 1900 }
8d08fdba
MS
1901 }
1902
8d08fdba
MS
1903 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1904 {
68642fb6
UD
1905 if (DECL_TEMPLATE_INSTANTIATION (olddecl)
1906 && !DECL_TEMPLATE_INSTANTIATION (newdecl))
75650646
MM
1907 {
1908 /* If newdecl is not a specialization, then it is not a
1909 template-related function at all. And that means that we
bf7c02dd 1910 should have exited above, returning 0. */
50bc768d 1911 gcc_assert (DECL_TEMPLATE_SPECIALIZATION (newdecl));
75650646 1912
68642fb6 1913 if (TREE_USED (olddecl))
75650646 1914 /* From [temp.expl.spec]:
68642fb6 1915
75650646
MM
1916 If a template, a member template or the member of a class
1917 template is explicitly specialized then that
1918 specialization shall be declared before the first use of
1919 that specialization that would cause an implicit
1920 instantiation to take place, in every translation unit in
1921 which such a use occurs. */
9e637a26 1922 error ("explicit specialization of %qD after first use",
75650646
MM
1923 olddecl);
1924
1925 SET_DECL_TEMPLATE_SPECIALIZATION (olddecl);
d03d18e8 1926
b9e75696
JM
1927 /* Don't propagate visibility from the template to the
1928 specialization here. We'll do that in determine_visibility if
1929 appropriate. */
1930 DECL_VISIBILITY_SPECIFIED (olddecl) = 0;
1931
d03d18e8
KL
1932 /* [temp.expl.spec/14] We don't inline explicit specialization
1933 just because the primary template says so. */
75650646 1934 }
3a47c4e4
AO
1935 else if (new_defines_function && DECL_INITIAL (olddecl))
1936 {
1937 /* C++ is always in in unit-at-a-time mode, so we never
1938 inline re-defined extern inline functions. */
1939 DECL_INLINE (newdecl) = 0;
1940 DECL_UNINLINABLE (newdecl) = 1;
1941 }
d03d18e8
KL
1942 else
1943 {
1944 if (DECL_PENDING_INLINE_INFO (newdecl) == 0)
1945 DECL_PENDING_INLINE_INFO (newdecl) = DECL_PENDING_INLINE_INFO (olddecl);
faae18ab 1946
d03d18e8 1947 DECL_DECLARED_INLINE_P (newdecl) |= DECL_DECLARED_INLINE_P (olddecl);
faae18ab 1948
caf93cb0 1949 /* If either decl says `inline', this fn is inline, unless
d03d18e8
KL
1950 its definition was passed already. */
1951 if (DECL_INLINE (newdecl) && DECL_INITIAL (olddecl) == NULL_TREE)
1952 DECL_INLINE (olddecl) = 1;
1953 DECL_INLINE (newdecl) = DECL_INLINE (olddecl);
1954
1955 DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
1956 = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
c536a6a7
RG
1957
1958 DECL_DISREGARD_INLINE_LIMITS (newdecl)
1959 = DECL_DISREGARD_INLINE_LIMITS (olddecl)
1960 = (DECL_DISREGARD_INLINE_LIMITS (newdecl)
1961 || DECL_DISREGARD_INLINE_LIMITS (olddecl));
d03d18e8 1962 }
97055d5c 1963
5daf7c0a
JM
1964 /* Preserve abstractness on cloned [cd]tors. */
1965 DECL_ABSTRACT (newdecl) = DECL_ABSTRACT (olddecl);
1966
700f8a87
MS
1967 if (! types_match)
1968 {
5d2ed28c 1969 SET_DECL_LANGUAGE (olddecl, DECL_LANGUAGE (newdecl));
92643fea 1970 COPY_DECL_ASSEMBLER_NAME (newdecl, olddecl);
84b8b0e0 1971 COPY_DECL_RTL (newdecl, olddecl);
5566b478
MS
1972 }
1973 if (! types_match || new_defines_function)
1974 {
6f1b4c42
JM
1975 /* These need to be copied so that the names are available.
1976 Note that if the types do match, we'll preserve inline
1977 info and other bits, but if not, we won't. */
700f8a87
MS
1978 DECL_ARGUMENTS (olddecl) = DECL_ARGUMENTS (newdecl);
1979 DECL_RESULT (olddecl) = DECL_RESULT (newdecl);
700f8a87 1980 }
8d08fdba
MS
1981 if (new_defines_function)
1982 /* If defining a function declared with other language
1983 linkage, use the previously declared language linkage. */
5d2ed28c 1984 SET_DECL_LANGUAGE (newdecl, DECL_LANGUAGE (olddecl));
6f1b4c42 1985 else if (types_match)
8d08fdba
MS
1986 {
1987 /* If redeclaring a builtin function, and not a definition,
1988 it stays built in. */
1989 if (DECL_BUILT_IN (olddecl))
1990 {
26db82d8 1991 DECL_BUILT_IN_CLASS (newdecl) = DECL_BUILT_IN_CLASS (olddecl);
39211cd5 1992 DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl);
8d08fdba
MS
1993 /* If we're keeping the built-in definition, keep the rtl,
1994 regardless of declaration matches. */
84b8b0e0 1995 COPY_DECL_RTL (olddecl, newdecl);
8d08fdba 1996 }
8d08fdba
MS
1997
1998 DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
8d08fdba
MS
1999 /* Don't clear out the arguments if we're redefining a function. */
2000 if (DECL_ARGUMENTS (olddecl))
2001 DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl);
2002 }
2003 }
0b1161fc
MM
2004 else if (TREE_CODE (newdecl) == NAMESPACE_DECL)
2005 NAMESPACE_LEVEL (newdecl) = NAMESPACE_LEVEL (olddecl);
a9aedbc2 2006
8d08fdba
MS
2007 /* Now preserve various other info from the definition. */
2008 TREE_ADDRESSABLE (newdecl) = TREE_ADDRESSABLE (olddecl);
2009 TREE_ASM_WRITTEN (newdecl) = TREE_ASM_WRITTEN (olddecl);
2010 DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
92643fea 2011 COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
8d08fdba 2012
d7afec4b 2013 /* Warn about conflicting visibility specifications. */
c8094d83 2014 if (DECL_VISIBILITY_SPECIFIED (olddecl)
73a8adb6 2015 && DECL_VISIBILITY_SPECIFIED (newdecl)
d7afec4b
ND
2016 && DECL_VISIBILITY (newdecl) != DECL_VISIBILITY (olddecl))
2017 {
dee15844
JM
2018 warning (OPT_Wattributes, "%q+D: visibility attribute ignored "
2019 "because it", newdecl);
5c498b10
DD
2020 warning (OPT_Wattributes, "%Jconflicts with previous "
2021 "declaration here", olddecl);
d7afec4b
ND
2022 }
2023 /* Choose the declaration which specified visibility. */
2024 if (DECL_VISIBILITY_SPECIFIED (olddecl))
facef326 2025 {
facef326 2026 DECL_VISIBILITY (newdecl) = DECL_VISIBILITY (olddecl);
d7afec4b
ND
2027 DECL_VISIBILITY_SPECIFIED (newdecl) = 1;
2028 }
3db45ab5 2029 /* Init priority used to be merged from newdecl to olddecl by the memcpy,
820cc88f
DB
2030 so keep this behavior. */
2031 if (TREE_CODE (newdecl) == VAR_DECL && DECL_HAS_INIT_PRIORITY_P (newdecl))
2032 {
2033 SET_DECL_INIT_PRIORITY (olddecl, DECL_INIT_PRIORITY (newdecl));
2034 DECL_HAS_INIT_PRIORITY_P (olddecl) = 1;
2035 }
facef326 2036
1d786913
MM
2037 /* The DECL_LANG_SPECIFIC information in OLDDECL will be replaced
2038 with that from NEWDECL below. */
2039 if (DECL_LANG_SPECIFIC (olddecl))
2040 {
c8094d83 2041 gcc_assert (DECL_LANG_SPECIFIC (olddecl)
1d786913
MM
2042 != DECL_LANG_SPECIFIC (newdecl));
2043 ggc_free (DECL_LANG_SPECIFIC (olddecl));
2044 }
2045
8d08fdba
MS
2046 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2047 {
2048 int function_size;
8d08fdba 2049
820cc88f 2050 function_size = sizeof (struct tree_decl_common);
8d08fdba 2051
730e1556
KG
2052 memcpy ((char *) olddecl + sizeof (struct tree_common),
2053 (char *) newdecl + sizeof (struct tree_common),
2054 function_size - sizeof (struct tree_common));
8d08fdba 2055
820cc88f
DB
2056 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2057 (char *) newdecl + sizeof (struct tree_decl_common),
2058 sizeof (struct tree_function_decl) - sizeof (struct tree_decl_common));
50b56799 2059 if (new_template)
b3445994
MM
2060 /* If newdecl is a template instantiation, it is possible that
2061 the following sequence of events has occurred:
2062
2063 o A friend function was declared in a class template. The
2064 class template was instantiated.
2065
2066 o The instantiation of the friend declaration was
2067 recorded on the instantiation list, and is newdecl.
2068
2069 o Later, however, instantiate_class_template called pushdecl
2070 on the newdecl to perform name injection. But, pushdecl in
2071 turn called duplicate_decls when it discovered that another
2072 declaration of a global function with the same name already
2073 existed.
2074
2075 o Here, in duplicate_decls, we decided to clobber newdecl.
2076
2077 If we're going to do that, we'd better make sure that
2078 olddecl, and not newdecl, is on the list of
2079 instantiations so that if we try to do the instantiation
2080 again we won't get the clobbered declaration. */
caf93cb0 2081 reregister_specialization (newdecl,
50b56799 2082 new_template,
b3445994 2083 olddecl);
8d08fdba
MS
2084 }
2085 else
2086 {
820cc88f 2087 size_t size = tree_code_size (TREE_CODE (olddecl));
730e1556
KG
2088 memcpy ((char *) olddecl + sizeof (struct tree_common),
2089 (char *) newdecl + sizeof (struct tree_common),
820cc88f 2090 sizeof (struct tree_decl_common) - sizeof (struct tree_common));
3db45ab5 2091 switch (TREE_CODE (olddecl))
820cc88f
DB
2092 {
2093 case LABEL_DECL:
2094 case VAR_DECL:
2095 case RESULT_DECL:
2096 case PARM_DECL:
2097 case FIELD_DECL:
2098 case TYPE_DECL:
2099 case CONST_DECL:
2100 {
2101 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2102 (char *) newdecl + sizeof (struct tree_decl_common),
2103 size - sizeof (struct tree_decl_common)
2104 + TREE_CODE_LENGTH (TREE_CODE (newdecl)) * sizeof (char *));
2105 }
2106 break;
2107 default:
2108 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2109 (char *) newdecl + sizeof (struct tree_decl_common),
2110 sizeof (struct tree_decl_non_common) - sizeof (struct tree_decl_common)
2111 + TREE_CODE_LENGTH (TREE_CODE (newdecl)) * sizeof (char *));
2112 break;
2113 }
8d08fdba 2114 }
8d08fdba
MS
2115 DECL_UID (olddecl) = olddecl_uid;
2116 if (olddecl_friend)
2117 DECL_FRIEND_P (olddecl) = 1;
d63d5d0c
ILT
2118 if (hidden_friend)
2119 {
2120 DECL_ANTICIPATED (olddecl) = 1;
2121 DECL_HIDDEN_FRIEND_P (olddecl) = 1;
2122 }
8d08fdba 2123
d9525bec
BK
2124 /* NEWDECL contains the merged attribute lists.
2125 Update OLDDECL to be the same. */
91d231cb 2126 DECL_ATTRIBUTES (olddecl) = DECL_ATTRIBUTES (newdecl);
d9525bec 2127
16e31964
DS
2128 /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
2129 so that encode_section_info has a chance to look at the new decl
2130 flags and attributes. */
caf93cb0 2131 if (DECL_RTL_SET_P (olddecl)
16e31964
DS
2132 && (TREE_CODE (olddecl) == FUNCTION_DECL
2133 || (TREE_CODE (olddecl) == VAR_DECL
2134 && TREE_STATIC (olddecl))))
0e6df31e 2135 make_decl_rtl (olddecl);
16e31964 2136
1d786913
MM
2137 /* The NEWDECL will no longer be needed. Because every out-of-class
2138 declaration of a member results in a call to duplicate_decls,
2139 freeing these nodes represents in a significant savings. */
2140 ggc_free (newdecl);
2141
b1a19c7c 2142 return olddecl;
8d08fdba 2143}
8d08fdba 2144\f
8251199e 2145/* Return zero if the declaration NEWDECL is valid
8d08fdba
MS
2146 when the declaration OLDDECL (assumed to be for the same name)
2147 has already been seen.
2148 Otherwise return an error message format string with a %s
2149 where the identifier should go. */
2150
d8e178a0 2151static const char *
11f6b451 2152redeclaration_error_message (tree newdecl, tree olddecl)
8d08fdba
MS
2153{
2154 if (TREE_CODE (newdecl) == TYPE_DECL)
2155 {
2156 /* Because C++ can put things into name space for free,
2157 constructs like "typedef struct foo { ... } foo"
2158 would look like an erroneous redeclaration. */
3bfdc719 2159 if (same_type_p (TREE_TYPE (newdecl), TREE_TYPE (olddecl)))
6aed477a 2160 return NULL;
8d08fdba 2161 else
2d01edd7 2162 return "redefinition of %q#D";
8d08fdba
MS
2163 }
2164 else if (TREE_CODE (newdecl) == FUNCTION_DECL)
2165 {
2166 /* If this is a pure function, its olddecl will actually be
2167 the original initialization to `0' (which we force to call
2168 abort()). Don't complain about redefinition in this case. */
d1fc3bbd
AO
2169 if (DECL_LANG_SPECIFIC (olddecl) && DECL_PURE_VIRTUAL_P (olddecl)
2170 && DECL_INITIAL (olddecl) == NULL_TREE)
6aed477a 2171 return NULL;
8d08fdba 2172
2c73f9f5 2173 /* If both functions come from different namespaces, this is not
a1c65f9f 2174 a redeclaration - this is a conflict with a used function. */
6eb3bb27 2175 if (DECL_NAMESPACE_SCOPE_P (olddecl)
713101a6
AO
2176 && DECL_CONTEXT (olddecl) != DECL_CONTEXT (newdecl)
2177 && ! decls_match (olddecl, newdecl))
2d01edd7 2178 return "%qD conflicts with used function";
2c73f9f5 2179
db5ae43f 2180 /* We'll complain about linkage mismatches in
0cbd7506 2181 warn_extern_redeclared_static. */
db5ae43f 2182
2c73f9f5 2183 /* Defining the same name twice is no good. */
8d08fdba 2184 if (DECL_INITIAL (olddecl) != NULL_TREE
faae18ab 2185 && DECL_INITIAL (newdecl) != NULL_TREE)
8d08fdba
MS
2186 {
2187 if (DECL_NAME (olddecl) == NULL_TREE)
2d01edd7 2188 return "%q#D not declared in class";
3a47c4e4
AO
2189 else if (!GNU_INLINE_P (olddecl)
2190 || GNU_INLINE_P (newdecl))
2d01edd7 2191 return "redefinition of %q#D";
8d08fdba 2192 }
3a47c4e4
AO
2193
2194 if (DECL_DECLARED_INLINE_P (olddecl) && DECL_DECLARED_INLINE_P (newdecl))
2195 {
2196 bool olda = GNU_INLINE_P (olddecl);
2197 bool newa = GNU_INLINE_P (newdecl);
2198
2199 if (olda != newa)
2200 {
2201 if (newa)
2202 return "%q+D redeclared inline with %<gnu_inline%> attribute";
2203 else
2204 return "%q+D redeclared inline without %<gnu_inline%> attribute";
2205 }
2206 }
2207
6aed477a 2208 return NULL;
8d08fdba 2209 }
51c184be
MS
2210 else if (TREE_CODE (newdecl) == TEMPLATE_DECL)
2211 {
5bd61841
MM
2212 tree nt, ot;
2213
2214 if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL)
2215 {
2216 if (COMPLETE_TYPE_P (TREE_TYPE (newdecl))
2217 && COMPLETE_TYPE_P (TREE_TYPE (olddecl)))
2d01edd7 2218 return "redefinition of %q#D";
5bd61841
MM
2219 return NULL;
2220 }
2221
2222 if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) != FUNCTION_DECL
caf93cb0 2223 || (DECL_TEMPLATE_RESULT (newdecl)
5bd61841
MM
2224 == DECL_TEMPLATE_RESULT (olddecl)))
2225 return NULL;
2226
2227 nt = DECL_TEMPLATE_RESULT (newdecl);
2228 if (DECL_TEMPLATE_INFO (nt))
2229 nt = DECL_TEMPLATE_RESULT (template_for_substitution (nt));
2230 ot = DECL_TEMPLATE_RESULT (olddecl);
2231 if (DECL_TEMPLATE_INFO (ot))
2232 ot = DECL_TEMPLATE_RESULT (template_for_substitution (ot));
3a47c4e4
AO
2233 if (DECL_INITIAL (nt) && DECL_INITIAL (ot)
2234 && (!GNU_INLINE_P (ot) || GNU_INLINE_P (nt)))
2d01edd7 2235 return "redefinition of %q#D";
5bd61841 2236
3a47c4e4
AO
2237 if (DECL_DECLARED_INLINE_P (ot) && DECL_DECLARED_INLINE_P (nt))
2238 {
2239 bool olda = GNU_INLINE_P (ot);
2240 bool newa = GNU_INLINE_P (nt);
2241
2242 if (olda != newa)
2243 {
2244 if (newa)
2245 return "%q+D redeclared inline with %<gnu_inline%> attribute";
2246 else
2247 return "%q+D redeclared inline without %<gnu_inline%> attribute";
2248 }
2249 }
2250
9b7dd5e8
DG
2251 /* Core issue #226 (C++0x):
2252
2253 If a friend function template declaration specifies a
2254 default template-argument, that declaration shall be a
2255 definition and shall be the only declaration of the
2256 function template in the translation unit. */
c1ae8be5 2257 if ((cxx_dialect != cxx98)
9b7dd5e8
DG
2258 && TREE_CODE (ot) == FUNCTION_DECL && DECL_FRIEND_P (ot)
2259 && !check_default_tmpl_args (nt, DECL_TEMPLATE_PARMS (newdecl),
2260 /*is_primary=*/1, /*is_partial=*/0,
2261 /*is_friend_decl=*/2))
2262 return "redeclaration of friend %q#D may not have default template arguments";
2263
5bd61841 2264 return NULL;
51c184be 2265 }
31a79236 2266 else if (TREE_CODE (newdecl) == VAR_DECL
1799e5d5
RH
2267 && DECL_THREAD_LOCAL_P (newdecl) != DECL_THREAD_LOCAL_P (olddecl)
2268 && (! DECL_LANG_SPECIFIC (olddecl)
2269 || ! CP_DECL_THREADPRIVATE_P (olddecl)
2270 || DECL_THREAD_LOCAL_P (newdecl)))
31a79236
JJ
2271 {
2272 /* Only variables can be thread-local, and all declarations must
2273 agree on this property. */
2274 if (DECL_THREAD_LOCAL_P (newdecl))
2275 return "thread-local declaration of %q#D follows "
2276 "non-thread-local declaration";
2277 else
2278 return "non-thread-local declaration of %q#D follows "
2279 "thread-local declaration";
2280 }
1f51a992 2281 else if (toplevel_bindings_p () || DECL_NAMESPACE_SCOPE_P (newdecl))
8d08fdba 2282 {
555551c2
MM
2283 /* The objects have been declared at namespace scope. If either
2284 is a member of an anonymous union, then this is an invalid
2285 redeclaration. For example:
2286
2287 int i;
2288 union { int i; };
2289
2290 is invalid. */
aedfac0e
PC
2291 if ((TREE_CODE (newdecl) == VAR_DECL && DECL_ANON_UNION_VAR_P (newdecl))
2292 || (TREE_CODE (olddecl) == VAR_DECL && DECL_ANON_UNION_VAR_P (olddecl)))
555551c2
MM
2293 return "redeclaration of %q#D";
2294 /* If at least one declaration is a reference, there is no
2295 conflict. For example:
2296
2297 int i = 3;
2298 extern int i;
2299
2300 is valid. */
8d08fdba 2301 if (DECL_EXTERNAL (newdecl) || DECL_EXTERNAL (olddecl))
6aed477a 2302 return NULL;
8926095f 2303 /* Reject two definitions. */
2d01edd7 2304 return "redefinition of %q#D";
8d08fdba
MS
2305 }
2306 else
2307 {
2308 /* Objects declared with block scope: */
2309 /* Reject two definitions, and reject a definition
2310 together with an external reference. */
2311 if (!(DECL_EXTERNAL (newdecl) && DECL_EXTERNAL (olddecl)))
2d01edd7 2312 return "redeclaration of %q#D";
6aed477a 2313 return NULL;
8d08fdba
MS
2314 }
2315}
2316\f
1799e5d5
RH
2317/* Hash and equality functions for the named_label table. */
2318
2319static hashval_t
2320named_label_entry_hash (const void *data)
2321{
2322 const struct named_label_entry *ent = (const struct named_label_entry *) data;
2323 return DECL_UID (ent->label_decl);
2324}
2325
2326static int
2327named_label_entry_eq (const void *a, const void *b)
2328{
2329 const struct named_label_entry *ent_a = (const struct named_label_entry *) a;
2330 const struct named_label_entry *ent_b = (const struct named_label_entry *) b;
2331 return ent_a->label_decl == ent_b->label_decl;
2332}
2333
acef433b 2334/* Create a new label, named ID. */
8d08fdba 2335
acef433b 2336static tree
11f6b451 2337make_label_decl (tree id, int local_p)
8d08fdba 2338{
1799e5d5
RH
2339 struct named_label_entry *ent;
2340 void **slot;
acef433b 2341 tree decl;
8d08fdba 2342
acef433b 2343 decl = build_decl (LABEL_DECL, id, void_type_node);
acef433b
MM
2344
2345 DECL_CONTEXT (decl) = current_function_decl;
2346 DECL_MODE (decl) = VOIDmode;
2347 C_DECLARED_LABEL_FLAG (decl) = local_p;
8d08fdba 2348
acef433b
MM
2349 /* Say where one reference is to the label, for the sake of the
2350 error if it is not defined. */
f31686a3 2351 DECL_SOURCE_LOCATION (decl) = input_location;
acef433b
MM
2352
2353 /* Record the fact that this identifier is bound to this label. */
2354 SET_IDENTIFIER_LABEL_VALUE (id, decl);
2355
1799e5d5
RH
2356 /* Create the label htab for the function on demand. */
2357 if (!named_labels)
2358 named_labels = htab_create_ggc (13, named_label_entry_hash,
2359 named_label_entry_eq, NULL);
6625cdb5 2360
1799e5d5
RH
2361 /* Record this label on the list of labels used in this function.
2362 We do this before calling make_label_decl so that we get the
2363 IDENTIFIER_LABEL_VALUE before the new label is declared. */
2364 ent = GGC_CNEW (struct named_label_entry);
2365 ent->label_decl = decl;
6625cdb5 2366
1799e5d5
RH
2367 slot = htab_find_slot (named_labels, ent, INSERT);
2368 gcc_assert (*slot == NULL);
2369 *slot = ent;
2370
2371 return decl;
acef433b 2372}
8d08fdba 2373
acef433b
MM
2374/* Look for a label named ID in the current function. If one cannot
2375 be found, create one. (We keep track of used, but undefined,
2376 labels, and complain about them at the end of a function.) */
8d08fdba 2377
68642fb6 2378tree
11f6b451 2379lookup_label (tree id)
acef433b
MM
2380{
2381 tree decl;
8d08fdba 2382
22ffcc6f 2383 timevar_push (TV_NAME_LOOKUP);
acef433b
MM
2384 /* You can't use labels at global scope. */
2385 if (current_function_decl == NULL_TREE)
2386 {
2d01edd7 2387 error ("label %qE referenced outside of any function", id);
22ffcc6f 2388 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
acef433b 2389 }
68642fb6 2390
acef433b
MM
2391 /* See if we've already got this label. */
2392 decl = IDENTIFIER_LABEL_VALUE (id);
2393 if (decl != NULL_TREE && DECL_CONTEXT (decl) == current_function_decl)
22ffcc6f 2394 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
8d08fdba 2395
acef433b 2396 decl = make_label_decl (id, /*local_p=*/0);
22ffcc6f 2397 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
8d08fdba
MS
2398}
2399
acef433b 2400/* Declare a local label named ID. */
8d08fdba
MS
2401
2402tree
11f6b451 2403declare_local_label (tree id)
8d08fdba 2404{
1799e5d5 2405 tree decl, shadow;
8d08fdba 2406
acef433b 2407 /* Add a new entry to the SHADOWED_LABELS list so that when we leave
1799e5d5
RH
2408 this scope we can restore the old value of IDENTIFIER_TYPE_VALUE. */
2409 shadow = tree_cons (IDENTIFIER_LABEL_VALUE (id), NULL_TREE,
2410 current_binding_level->shadowed_labels);
2411 current_binding_level->shadowed_labels = shadow;
2412
acef433b 2413 decl = make_label_decl (id, /*local_p=*/1);
1799e5d5 2414 TREE_VALUE (shadow) = decl;
68642fb6 2415
acef433b 2416 return decl;
8d08fdba
MS
2417}
2418
6625cdb5
JM
2419/* Returns nonzero if it is ill-formed to jump past the declaration of
2420 DECL. Returns 2 if it's also a real problem. */
2421
2422static int
11f6b451 2423decl_jump_unsafe (tree decl)
6625cdb5 2424{
b88b7ced
AP
2425 if (TREE_CODE (decl) != VAR_DECL || TREE_STATIC (decl)
2426 || TREE_TYPE (decl) == error_mark_node)
6625cdb5
JM
2427 return 0;
2428
6c06fbce
MM
2429 if (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl))
2430 || DECL_NONTRIVIALLY_INITIALIZED_P (decl))
2431 return 2;
2432
2433 if (pod_type_p (TREE_TYPE (decl)))
6625cdb5
JM
2434 return 0;
2435
6c06fbce 2436 /* The POD stuff is just pedantry; why should it matter if the class
6625cdb5 2437 contains a field of pointer to member type? */
6625cdb5
JM
2438 return 1;
2439}
2440
1799e5d5
RH
2441/* A subroutine of check_previous_goto_1 to identify a branch to the user. */
2442
2443static void
2444identify_goto (tree decl, const location_t *locus)
2445{
2446 if (decl)
2447 pedwarn ("jump to label %qD", decl);
2448 else
2449 pedwarn ("jump to case label");
2450 if (locus)
2451 pedwarn ("%H from here", locus);
2452}
2453
6625cdb5
JM
2454/* Check that a single previously seen jump to a newly defined label
2455 is OK. DECL is the LABEL_DECL or 0; LEVEL is the binding_level for
2456 the jump context; NAMES are the names in scope in LEVEL at the jump
3db45ab5 2457 context; LOCUS is the source position of the jump or 0. Returns
1799e5d5 2458 true if all is well. */
6625cdb5 2459
1799e5d5
RH
2460static bool
2461check_previous_goto_1 (tree decl, struct cp_binding_level* level, tree names,
2462 bool exited_omp, const location_t *locus)
2463{
2464 struct cp_binding_level *b;
2465 bool identified = false, saw_eh = false, saw_omp = false;
2466
2467 if (exited_omp)
2468 {
2469 identify_goto (decl, locus);
2470 error (" exits OpenMP structured block");
2471 identified = saw_omp = true;
2472 }
2473
2474 for (b = current_binding_level; b ; b = b->level_chain)
2475 {
2476 tree new_decls, old_decls = (b == level ? names : NULL_TREE);
2477
2478 for (new_decls = b->names; new_decls != old_decls;
6625cdb5
JM
2479 new_decls = TREE_CHAIN (new_decls))
2480 {
2481 int problem = decl_jump_unsafe (new_decls);
2482 if (! problem)
2483 continue;
2484
1799e5d5 2485 if (!identified)
6625cdb5 2486 {
1799e5d5
RH
2487 identify_goto (decl, locus);
2488 identified = true;
6625cdb5 2489 }
826840d9 2490 if (problem > 1)
dee15844 2491 error (" crosses initialization of %q+#D", new_decls);
6625cdb5 2492 else
dee15844 2493 pedwarn (" enters scope of non-POD %q+#D", new_decls);
6625cdb5
JM
2494 }
2495
2496 if (b == level)
2497 break;
1799e5d5 2498 if ((b->kind == sk_try || b->kind == sk_catch) && !saw_eh)
6625cdb5 2499 {
1799e5d5 2500 if (!identified)
6625cdb5 2501 {
1799e5d5
RH
2502 identify_goto (decl, locus);
2503 identified = true;
6625cdb5 2504 }
a7e8c268 2505 if (b->kind == sk_try)
826840d9
RH
2506 error (" enters try block");
2507 else
2508 error (" enters catch block");
1799e5d5
RH
2509 saw_eh = true;
2510 }
2511 if (b->kind == sk_omp && !saw_omp)
2512 {
2513 if (!identified)
2514 {
2515 identify_goto (decl, locus);
2516 identified = true;
2517 }
2518 error (" enters OpenMP structured block");
2519 saw_omp = true;
6625cdb5
JM
2520 }
2521 }
6625cdb5 2522
1799e5d5 2523 return !identified;
6625cdb5
JM
2524}
2525
2526static void
1799e5d5 2527check_previous_goto (tree decl, struct named_label_use_entry *use)
6625cdb5 2528{
1799e5d5
RH
2529 check_previous_goto_1 (decl, use->binding_level,
2530 use->names_in_scope, use->in_omp_scope,
2531 &use->o_goto_locus);
6625cdb5
JM
2532}
2533
1799e5d5
RH
2534static bool
2535check_switch_goto (struct cp_binding_level* level)
6625cdb5 2536{
1799e5d5 2537 return check_previous_goto_1 (NULL_TREE, level, level->names, false, NULL);
6625cdb5
JM
2538}
2539
2540/* Check that a new jump to a label DECL is OK. Called by
2541 finish_goto_stmt. */
2542
2543void
11f6b451 2544check_goto (tree decl)
6625cdb5 2545{
1799e5d5
RH
2546 struct named_label_entry *ent, dummy;
2547 bool saw_catch = false, identified = false;
6625cdb5 2548 tree bad;
6625cdb5 2549
1799e5d5
RH
2550 /* We can't know where a computed goto is jumping.
2551 So we assume that it's OK. */
2552 if (TREE_CODE (decl) != LABEL_DECL)
2553 return;
2554
2555 /* We didn't record any information about this label when we created it,
2556 and there's not much point since it's trivial to analyze as a return. */
2557 if (decl == cdtor_label)
e3cd9945
APB
2558 return;
2559
1799e5d5
RH
2560 dummy.label_decl = decl;
2561 ent = (struct named_label_entry *) htab_find (named_labels, &dummy);
2562 gcc_assert (ent != NULL);
2563
6625cdb5
JM
2564 /* If the label hasn't been defined yet, defer checking. */
2565 if (! DECL_INITIAL (decl))
2566 {
1799e5d5 2567 struct named_label_use_entry *new_use;
6625cdb5 2568
1799e5d5
RH
2569 /* Don't bother creating another use if the last goto had the
2570 same data, and will therefore create the same set of errors. */
2571 if (ent->uses
2572 && ent->uses->names_in_scope == current_binding_level->names)
2573 return;
6625cdb5 2574
1799e5d5
RH
2575 new_use = GGC_NEW (struct named_label_use_entry);
2576 new_use->binding_level = current_binding_level;
2577 new_use->names_in_scope = current_binding_level->names;
2578 new_use->o_goto_locus = input_location;
2579 new_use->in_omp_scope = false;
2580
2581 new_use->next = ent->uses;
2582 ent->uses = new_use;
2583 return;
2584 }
6625cdb5 2585
1799e5d5
RH
2586 if (ent->in_try_scope || ent->in_catch_scope
2587 || ent->in_omp_scope || ent->bad_decls)
6625cdb5 2588 {
dee15844 2589 pedwarn ("jump to label %q+D", decl);
6625cdb5 2590 pedwarn (" from here");
1799e5d5 2591 identified = true;
6625cdb5
JM
2592 }
2593
1799e5d5 2594 for (bad = ent->bad_decls; bad; bad = TREE_CHAIN (bad))
6625cdb5
JM
2595 {
2596 tree b = TREE_VALUE (bad);
2597 int u = decl_jump_unsafe (b);
2598
2599 if (u > 1 && DECL_ARTIFICIAL (b))
1799e5d5
RH
2600 {
2601 /* Can't skip init of __exception_info. */
2602 error ("%J enters catch block", b);
2603 saw_catch = true;
2604 }
6625cdb5 2605 else if (u > 1)
dee15844 2606 error (" skips initialization of %q+#D", b);
6625cdb5 2607 else
dee15844 2608 pedwarn (" enters scope of non-POD %q+#D", b);
6625cdb5
JM
2609 }
2610
1799e5d5 2611 if (ent->in_try_scope)
6625cdb5 2612 error (" enters try block");
1799e5d5 2613 else if (ent->in_catch_scope && !saw_catch)
826840d9 2614 error (" enters catch block");
1799e5d5
RH
2615
2616 if (ent->in_omp_scope)
2617 error (" enters OpenMP structured block");
2618 else if (flag_openmp)
2619 {
2620 struct cp_binding_level *b;
2621 for (b = current_binding_level; b ; b = b->level_chain)
2622 {
2623 if (b == ent->binding_level)
2624 break;
2625 if (b->kind == sk_omp)
2626 {
2627 if (!identified)
2628 {
2629 pedwarn ("jump to label %q+D", decl);
2630 pedwarn (" from here");
2631 identified = true;
2632 }
2633 error (" exits OpenMP structured block");
2634 break;
2635 }
2636 }
2637 }
2638}
2639
2640/* Check that a return is ok wrt OpenMP structured blocks.
2641 Called by finish_return_stmt. Returns true if all is well. */
2642
2643bool
2644check_omp_return (void)
2645{
2646 struct cp_binding_level *b;
2647 for (b = current_binding_level; b ; b = b->level_chain)
2648 if (b->kind == sk_omp)
2649 {
2650 error ("invalid exit from OpenMP structured block");
2651 return false;
2652 }
2653 return true;
6625cdb5
JM
2654}
2655
8d08fdba 2656/* Define a label, specifying the location in the source file.
b9c87401 2657 Return the LABEL_DECL node for the label. */
8d08fdba
MS
2658
2659tree
5b030314 2660define_label (location_t location, tree name)
8d08fdba 2661{
1799e5d5 2662 struct named_label_entry *ent, dummy;
926ce8bd 2663 struct cp_binding_level *p;
1799e5d5 2664 tree decl;
6625cdb5 2665
22ffcc6f 2666 timevar_push (TV_NAME_LOOKUP);
1799e5d5
RH
2667
2668 decl = lookup_label (name);
2669
2670 dummy.label_decl = decl;
2671 ent = (struct named_label_entry *) htab_find (named_labels, &dummy);
2672 gcc_assert (ent != NULL);
8d08fdba 2673
3dc9aec6 2674 /* After labels, make any new cleanups in the function go into their
8d08fdba 2675 own new (temporary) binding contour. */
caf93cb0
EC
2676 for (p = current_binding_level;
2677 p->kind != sk_function_parms;
a7e8c268 2678 p = p->level_chain)
3dc9aec6 2679 p->more_cleanups_ok = 0;
8d08fdba 2680
e1cd6e56 2681 if (name == get_identifier ("wchar_t"))
33bd39a2 2682 pedwarn ("label named wchar_t");
e1cd6e56 2683
8d08fdba 2684 if (DECL_INITIAL (decl) != NULL_TREE)
417fa55b
LM
2685 {
2686 error ("duplicate label %qD", decl);
2687 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
2688 }
8d08fdba
MS
2689 else
2690 {
1799e5d5
RH
2691 struct named_label_use_entry *use;
2692
8d08fdba
MS
2693 /* Mark label as having been defined. */
2694 DECL_INITIAL (decl) = error_mark_node;
2695 /* Say where in the source. */
f31686a3 2696 DECL_SOURCE_LOCATION (decl) = location;
1799e5d5
RH
2697
2698 ent->binding_level = current_binding_level;
2699 ent->names_in_scope = current_binding_level->names;
2700
2701 for (use = ent->uses; use ; use = use->next)
2702 check_previous_goto (decl, use);
2703 ent->uses = NULL;
8d08fdba 2704 }
b9c87401 2705
be99edf8 2706 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
8d08fdba
MS
2707}
2708
a5894242
MS
2709struct cp_switch
2710{
e2500fed 2711 struct cp_binding_level *level;
a5894242 2712 struct cp_switch *next;
56cb9733
MM
2713 /* The SWITCH_STMT being built. */
2714 tree switch_stmt;
2715 /* A splay-tree mapping the low element of a case range to the high
2716 element, or NULL_TREE if there is no high element. Used to
2717 determine whether or not a new case label duplicates an old case
2718 label. We need a tree, rather than simply a hash table, because
2719 of the GNU case range extension. */
2720 splay_tree cases;
a5894242
MS
2721};
2722
56cb9733
MM
2723/* A stack of the currently active switch statements. The innermost
2724 switch statement is on the top of the stack. There is no need to
2725 mark the stack for garbage collection because it is only active
2726 during the processing of the body of a function, and we never
2727 collect at that point. */
5362b086 2728
a5894242
MS
2729static struct cp_switch *switch_stack;
2730
56cb9733
MM
2731/* Called right after a switch-statement condition is parsed.
2732 SWITCH_STMT is the switch statement being parsed. */
2733
a5894242 2734void
11f6b451 2735push_switch (tree switch_stmt)
a5894242 2736{
0ac1b889 2737 struct cp_switch *p = XNEW (struct cp_switch);
a5894242
MS
2738 p->level = current_binding_level;
2739 p->next = switch_stack;
56cb9733
MM
2740 p->switch_stmt = switch_stmt;
2741 p->cases = splay_tree_new (case_compare, NULL, NULL);
a5894242
MS
2742 switch_stack = p;
2743}
2744
2745void
11f6b451 2746pop_switch (void)
a5894242 2747{
6de9cd9a 2748 struct cp_switch *cs = switch_stack;
fbc315db 2749 location_t switch_location;
6de9cd9a
DN
2750
2751 /* Emit warnings as needed. */
fbc315db
ILT
2752 if (EXPR_HAS_LOCATION (cs->switch_stmt))
2753 switch_location = EXPR_LOCATION (cs->switch_stmt);
2754 else
2755 switch_location = input_location;
dddf9a0a
AP
2756 if (!processing_template_decl)
2757 c_do_switch_warnings (cs->cases, switch_location,
2758 SWITCH_STMT_TYPE (cs->switch_stmt),
2759 SWITCH_STMT_COND (cs->switch_stmt));
5362b086 2760
56cb9733 2761 splay_tree_delete (cs->cases);
a5894242 2762 switch_stack = switch_stack->next;
bedda2da 2763 free (cs);
a5894242
MS
2764}
2765
b0a1da19
JM
2766/* Note that we've seen a definition of a case label, and complain if this
2767 is a bad place for one. */
e92cc029 2768
3e4d04a1 2769tree
11f6b451 2770finish_case_label (tree low_value, tree high_value)
8d08fdba 2771{
3e4d04a1 2772 tree cond, r;
926ce8bd 2773 struct cp_binding_level *p;
a5894242 2774
56cb9733
MM
2775 if (processing_template_decl)
2776 {
8f17b5c5
MM
2777 tree label;
2778
56cb9733
MM
2779 /* For templates, just add the case label; we'll do semantic
2780 analysis at instantiation-time. */
8f17b5c5 2781 label = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE);
3e4d04a1 2782 return add_stmt (build_case_label (low_value, high_value, label));
56cb9733
MM
2783 }
2784
2785 /* Find the condition on which this switch statement depends. */
ebaae582 2786 cond = SWITCH_STMT_COND (switch_stack->switch_stmt);
56cb9733
MM
2787 if (cond && TREE_CODE (cond) == TREE_LIST)
2788 cond = TREE_VALUE (cond);
56cb9733 2789
1799e5d5
RH
2790 if (!check_switch_goto (switch_stack->level))
2791 return error_mark_node;
2792
a6c0a76c
SB
2793 r = c_add_case_label (switch_stack->cases, cond, TREE_TYPE (cond),
2794 low_value, high_value);
8d08fdba 2795
3dc9aec6 2796 /* After labels, make any new cleanups in the function go into their
8d08fdba 2797 own new (temporary) binding contour. */
caf93cb0
EC
2798 for (p = current_binding_level;
2799 p->kind != sk_function_parms;
a7e8c268 2800 p = p->level_chain)
3dc9aec6 2801 p->more_cleanups_ok = 0;
3e4d04a1
RH
2802
2803 return r;
8d08fdba
MS
2804}
2805\f
7ddedda4
MM
2806/* Hash a TYPENAME_TYPE. K is really of type `tree'. */
2807
e2500fed 2808static hashval_t
11f6b451 2809typename_hash (const void* k)
7ddedda4 2810{
e2500fed 2811 hashval_t hash;
741ac903 2812 const_tree const t = (const_tree) k;
7ddedda4 2813
7bdfd72e
KG
2814 hash = (htab_hash_pointer (TYPE_CONTEXT (t))
2815 ^ htab_hash_pointer (DECL_NAME (TYPE_NAME (t))));
7ddedda4
MM
2816
2817 return hash;
2818}
2819
fc6a28d7
MM
2820typedef struct typename_info {
2821 tree scope;
2822 tree name;
2823 tree template_id;
2824 bool enum_p;
2825 bool class_p;
2826} typename_info;
2827
06d40de8
DG
2828/* Compare two TYPENAME_TYPEs. K1 is really of type `tree', K2 is
2829 really of type `typename_info*' */
7ddedda4 2830
e2500fed 2831static int
11f6b451 2832typename_compare (const void * k1, const void * k2)
7ddedda4 2833{
741ac903
KG
2834 const_tree const t1 = (const_tree) k1;
2835 const typename_info *const t2 = (const typename_info *) k2;
fc6a28d7
MM
2836
2837 return (DECL_NAME (TYPE_NAME (t1)) == t2->name
2838 && TYPE_CONTEXT (t1) == t2->scope
2839 && TYPENAME_TYPE_FULLNAME (t1) == t2->template_id
2840 && TYPENAME_IS_ENUM_P (t1) == t2->enum_p
2841 && TYPENAME_IS_CLASS_P (t1) == t2->class_p);
7ddedda4
MM
2842}
2843
45869a6c 2844/* Build a TYPENAME_TYPE. If the type is `typename T::t', CONTEXT is
fc6a28d7 2845 the type of `T', NAME is the IDENTIFIER_NODE for `t'.
c8094d83 2846
45869a6c
MM
2847 Returns the new TYPENAME_TYPE. */
2848
e2500fed
GK
2849static GTY ((param_is (union tree_node))) htab_t typename_htab;
2850
8ce33230 2851static tree
fc6a28d7
MM
2852build_typename_type (tree context, tree name, tree fullname,
2853 enum tag_types tag_type)
45869a6c
MM
2854{
2855 tree t;
2856 tree d;
fc6a28d7 2857 typename_info ti;
fad205ff 2858 void **e;
fc6a28d7 2859 hashval_t hash;
45869a6c 2860
e2500fed 2861 if (typename_htab == NULL)
fc6a28d7
MM
2862 typename_htab = htab_create_ggc (61, &typename_hash,
2863 &typename_compare, NULL);
2864
c8094d83 2865 ti.scope = FROB_CONTEXT (context);
fc6a28d7
MM
2866 ti.name = name;
2867 ti.template_id = fullname;
2868 ti.enum_p = tag_type == enum_type;
2869 ti.class_p = (tag_type == class_type
2870 || tag_type == record_type
2871 || tag_type == union_type);
2872 hash = (htab_hash_pointer (ti.scope)
2873 ^ htab_hash_pointer (ti.name));
45869a6c 2874
7ddedda4 2875 /* See if we already have this type. */
fc6a28d7 2876 e = htab_find_slot_with_hash (typename_htab, &ti, hash, INSERT);
e2500fed
GK
2877 if (*e)
2878 t = (tree) *e;
7ddedda4 2879 else
fc6a28d7
MM
2880 {
2881 /* Build the TYPENAME_TYPE. */
2882 t = make_aggr_type (TYPENAME_TYPE);
2883 TYPE_CONTEXT (t) = ti.scope;
2884 TYPENAME_TYPE_FULLNAME (t) = ti.template_id;
2885 TYPENAME_IS_ENUM_P (t) = ti.enum_p;
2886 TYPENAME_IS_CLASS_P (t) = ti.class_p;
c8094d83 2887
fc6a28d7
MM
2888 /* Build the corresponding TYPE_DECL. */
2889 d = build_decl (TYPE_DECL, name, t);
2890 TYPE_NAME (TREE_TYPE (d)) = d;
2891 TYPE_STUB_DECL (TREE_TYPE (d)) = d;
2892 DECL_CONTEXT (d) = FROB_CONTEXT (context);
2893 DECL_ARTIFICIAL (d) = 1;
2894
2895 /* Store it in the hash table. */
2896 *e = t;
06d40de8
DG
2897
2898 /* TYPENAME_TYPEs must always be compared structurally, because
2899 they may or may not resolve down to another type depending on
2900 the currently open classes. */
2901 SET_TYPE_STRUCTURAL_EQUALITY (t);
fc6a28d7 2902 }
c8094d83 2903
45869a6c
MM
2904 return t;
2905}
2906
fc6a28d7
MM
2907/* Resolve `typename CONTEXT::NAME'. TAG_TYPE indicates the tag
2908 provided to name the type. Returns an appropriate type, unless an
2909 error occurs, in which case error_mark_node is returned. If we
2910 locate a non-artificial TYPE_DECL and TF_KEEP_TYPE_DECL is set, we
2911 return that, rather than the _TYPE it corresponds to, in other
2912 cases we look through the type decl. If TF_ERROR is set, complain
2913 about errors, otherwise be quiet. */
3baa501d 2914
5566b478 2915tree
fc6a28d7
MM
2916make_typename_type (tree context, tree name, enum tag_types tag_type,
2917 tsubst_flags_t complain)
5566b478 2918{
b2b7d40a 2919 tree fullname;
7d3bec9d
NS
2920 tree t;
2921 bool want_template;
a80e4195 2922
5dc5d13c
KL
2923 if (name == error_mark_node
2924 || context == NULL_TREE
2925 || context == error_mark_node)
2926 return error_mark_node;
2927
2f939d94 2928 if (TYPE_P (name))
78638e24 2929 {
68642fb6
UD
2930 if (!(TYPE_LANG_SPECIFIC (name)
2931 && (CLASSTYPE_IS_TEMPLATE (name)
78638e24
MM
2932 || CLASSTYPE_USE_TEMPLATE (name))))
2933 name = TYPE_IDENTIFIER (name);
2934 else
2935 /* Create a TEMPLATE_ID_EXPR for the type. */
2936 name = build_nt (TEMPLATE_ID_EXPR,
2937 CLASSTYPE_TI_TEMPLATE (name),
2938 CLASSTYPE_TI_ARGS (name));
2939 }
653cc74a 2940 else if (TREE_CODE (name) == TYPE_DECL)
a80e4195 2941 name = DECL_NAME (name);
b2b7d40a
JM
2942
2943 fullname = name;
2944
2945 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
11686454
JM
2946 {
2947 name = TREE_OPERAND (name, 0);
2948 if (TREE_CODE (name) == TEMPLATE_DECL)
2949 name = TREE_OPERAND (fullname, 0) = DECL_NAME (name);
50ef2c18
MM
2950 else if (TREE_CODE (name) == OVERLOAD)
2951 {
2952 error ("%qD is not a type", name);
2953 return error_mark_node;
2954 }
11686454 2955 }
8a2b77e7
JM
2956 if (TREE_CODE (name) == TEMPLATE_DECL)
2957 {
2d01edd7 2958 error ("%qD used without template parameters", name);
8a2b77e7
JM
2959 return error_mark_node;
2960 }
50bc768d 2961 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
0c88d886 2962 gcc_assert (TYPE_P (context));
04ddee1b 2963
7d3bec9d
NS
2964 /* When the CONTEXT is a dependent type, NAME could refer to a
2965 dependent base class of CONTEXT. So we cannot peek inside it,
2966 even if CONTEXT is a currently open scope. */
2967 if (dependent_type_p (context))
2968 return build_typename_type (context, name, fullname, tag_type);
caf93cb0 2969
7d3bec9d
NS
2970 if (!IS_AGGR_TYPE (context))
2971 {
2972 if (complain & tf_error)
2973 error ("%q#T is not a class", context);
2974 return error_mark_node;
5566b478 2975 }
7d3bec9d
NS
2976
2977 want_template = TREE_CODE (fullname) == TEMPLATE_ID_EXPR;
2978
2979 /* We should only set WANT_TYPE when we're a nested typename type.
2980 Then we can give better diagnostics if we find a non-type. */
2981 t = lookup_field (context, name, 0, /*want_type=*/true);
2982 if (!t)
11249cf0 2983 {
4f2b0fb2 2984 if (complain & tf_error)
7d3bec9d
NS
2985 error (want_template ? "no class template named %q#T in %q#T"
2986 : "no type named %q#T in %q#T", name, context);
11249cf0
MM
2987 return error_mark_node;
2988 }
7d3bec9d
NS
2989
2990 if (want_template && !DECL_CLASS_TEMPLATE_P (t))
2991 {
2992 if (complain & tf_error)
2993 error ("%<typename %T::%D%> names %q#T, which is not a class template",
2994 context, name, t);
2995 return error_mark_node;
2996 }
2997 if (!want_template && TREE_CODE (t) != TYPE_DECL)
2998 {
2999 if (complain & tf_error)
3000 error ("%<typename %T::%D%> names %q#T, which is not a type",
3001 context, name, t);
3002 return error_mark_node;
3003 }
3004
3005 if (complain & tf_error)
02022f3a 3006 perform_or_defer_access_check (TYPE_BINFO (context), t, t);
7d3bec9d
NS
3007
3008 if (want_template)
3009 return lookup_template_class (t, TREE_OPERAND (fullname, 1),
3010 NULL_TREE, context,
3011 /*entering_scope=*/0,
3012 tf_warning_or_error | tf_user);
3013
3014 if (DECL_ARTIFICIAL (t) || !(complain & tf_keep_type_decl))
3015 t = TREE_TYPE (t);
3016
3017 return t;
5566b478
MS
3018}
3019
b939a023 3020/* Resolve `CONTEXT::template NAME'. Returns a TEMPLATE_DECL if the name
c8094d83 3021 can be resolved or an UNBOUND_CLASS_TEMPLATE, unless an error occurs,
b939a023
KL
3022 in which case error_mark_node is returned.
3023
3024 If PARM_LIST is non-NULL, also make sure that the template parameter
3025 list of TEMPLATE_DECL matches.
3026
3027 If COMPLAIN zero, don't complain about any errors that occur. */
b8c6534b
KL
3028
3029tree
b939a023
KL
3030make_unbound_class_template (tree context, tree name, tree parm_list,
3031 tsubst_flags_t complain)
b8c6534b
KL
3032{
3033 tree t;
3034 tree d;
3035
3036 if (TYPE_P (name))
3037 name = TYPE_IDENTIFIER (name);
3038 else if (DECL_P (name))
3039 name = DECL_NAME (name);
8dc2b103 3040 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
b8c6534b 3041
9579624e 3042 if (!dependent_type_p (context)
b8c6534b
KL
3043 || currently_open_class (context))
3044 {
3045 tree tmpl = NULL_TREE;
3046
3047 if (IS_AGGR_TYPE (context))
86ac0575 3048 tmpl = lookup_field (context, name, 0, false);
b8c6534b
KL
3049
3050 if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl))
3051 {
4f09be91 3052 if (complain & tf_error)
2d01edd7 3053 error ("no class template named %q#T in %q#T", name, context);
b8c6534b
KL
3054 return error_mark_node;
3055 }
caf93cb0 3056
b939a023
KL
3057 if (parm_list
3058 && !comp_template_parms (DECL_TEMPLATE_PARMS (tmpl), parm_list))
3059 {
3060 if (complain & tf_error)
3061 {
3062 error ("template parameters do not match template");
dee15844 3063 error ("%q+D declared here", tmpl);
b939a023
KL
3064 }
3065 return error_mark_node;
3066 }
3067
2b59fc25 3068 if (complain & tf_error)
02022f3a 3069 perform_or_defer_access_check (TYPE_BINFO (context), tmpl, tmpl);
b8c6534b
KL
3070
3071 return tmpl;
3072 }
3073
3074 /* Build the UNBOUND_CLASS_TEMPLATE. */
3075 t = make_aggr_type (UNBOUND_CLASS_TEMPLATE);
3076 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
3077 TREE_TYPE (t) = NULL_TREE;
06d40de8 3078 SET_TYPE_STRUCTURAL_EQUALITY (t);
b8c6534b
KL
3079
3080 /* Build the corresponding TEMPLATE_DECL. */
3081 d = build_decl (TEMPLATE_DECL, name, t);
3082 TYPE_NAME (TREE_TYPE (d)) = d;
3083 TYPE_STUB_DECL (TREE_TYPE (d)) = d;
3084 DECL_CONTEXT (d) = FROB_CONTEXT (context);
3085 DECL_ARTIFICIAL (d) = 1;
b939a023 3086 DECL_TEMPLATE_PARMS (d) = parm_list;
b8c6534b
KL
3087
3088 return t;
3089}
3090
8d08fdba 3091\f
47aa0df4 3092
8d08fdba 3093/* Push the declarations of builtin types into the namespace.
4b0d3cbe
MM
3094 RID_INDEX is the index of the builtin type in the array
3095 RID_POINTERS. NAME is the name used when looking up the builtin
3096 type. TYPE is the _TYPE node for the builtin type. */
8d08fdba 3097
eaa7c03f 3098void
caf93cb0 3099record_builtin_type (enum rid rid_index,
0cbd7506
MS
3100 const char* name,
3101 tree type)
8d08fdba
MS
3102{
3103 tree rname = NULL_TREE, tname = NULL_TREE;
a703fb38 3104 tree tdecl = NULL_TREE;
8d08fdba 3105
0e5921e8 3106 if ((int) rid_index < (int) RID_MAX)
8d08fdba
MS
3107 rname = ridpointers[(int) rid_index];
3108 if (name)
3109 tname = get_identifier (name);
3110
4b0d3cbe
MM
3111 /* The calls to SET_IDENTIFIER_GLOBAL_VALUE below should be
3112 eliminated. Built-in types should not be looked up name; their
3113 names are keywords that the parser can recognize. However, there
3114 is code in c-common.c that uses identifier_global_value to look
3115 up built-in types by name. */
8d08fdba
MS
3116 if (tname)
3117 {
4b0d3cbe
MM
3118 tdecl = build_decl (TYPE_DECL, tname, type);
3119 DECL_ARTIFICIAL (tdecl) = 1;
a7e8c268 3120 SET_IDENTIFIER_GLOBAL_VALUE (tname, tdecl);
8d08fdba 3121 }
4b0d3cbe 3122 if (rname)
8d08fdba 3123 {
4b0d3cbe 3124 if (!tdecl)
8d08fdba 3125 {
4b0d3cbe
MM
3126 tdecl = build_decl (TYPE_DECL, rname, type);
3127 DECL_ARTIFICIAL (tdecl) = 1;
8d08fdba 3128 }
4b0d3cbe 3129 SET_IDENTIFIER_GLOBAL_VALUE (rname, tdecl);
8d08fdba 3130 }
4b0d3cbe
MM
3131
3132 if (!TYPE_NAME (type))
3133 TYPE_NAME (type) = tdecl;
47aa0df4
MM
3134
3135 if (tdecl)
21d13d83 3136 debug_hooks->type_decl (tdecl, 0);
8d08fdba
MS
3137}
3138
eff71ab0 3139/* Record one of the standard Java types.
4d8a1dd6
MM
3140 * Declare it as having the given NAME.
3141 * If SIZE > 0, it is the size of one of the integral types;
3142 * otherwise it is the negative of the size of one of the other types. */
eff71ab0
PB
3143
3144static tree
11f6b451 3145record_builtin_java_type (const char* name, int size)
eff71ab0
PB
3146{
3147 tree type, decl;
3148 if (size > 0)
3149 type = make_signed_type (size);
3150 else if (size > -32)
a1c65f9f 3151 { /* "__java_char" or ""__java_boolean". */
eff71ab0
PB
3152 type = make_unsigned_type (-size);
3153 /*if (size == -1) TREE_SET_CODE (type, BOOLEAN_TYPE);*/
3154 }
3155 else
a1c65f9f 3156 { /* "__java_float" or ""__java_double". */
eff71ab0
PB
3157 type = make_node (REAL_TYPE);
3158 TYPE_PRECISION (type) = - size;
3159 layout_type (type);
3160 }
0e5921e8 3161 record_builtin_type (RID_MAX, name, type);
eff71ab0 3162 decl = TYPE_NAME (type);
e229f2cd
PB
3163
3164 /* Suppress generate debug symbol entries for these types,
3165 since for normal C++ they are just clutter.
a1c65f9f 3166 However, push_lang_context undoes this if extern "Java" is seen. */
eff71ab0 3167 DECL_IGNORED_P (decl) = 1;
e229f2cd 3168
eff71ab0
PB
3169 TYPE_FOR_JAVA (type) = 1;
3170 return type;
3171}
3172
3b426391 3173/* Push a type into the namespace so that the back ends ignore it. */
036407f7
ML
3174
3175static void
11f6b451 3176record_unknown_type (tree type, const char* name)
036407f7
ML
3177{
3178 tree decl = pushdecl (build_decl (TYPE_DECL, get_identifier (name), type));
3179 /* Make sure the "unknown type" typedecl gets ignored for debug info. */
3180 DECL_IGNORED_P (decl) = 1;
3181 TYPE_DECL_SUPPRESS_DEBUG (decl) = 1;
3182 TYPE_SIZE (type) = TYPE_SIZE (void_type_node);
3183 TYPE_ALIGN (type) = 1;
11cf4d18 3184 TYPE_USER_ALIGN (type) = 0;
036407f7 3185 TYPE_MODE (type) = TYPE_MODE (void_type_node);
68642fb6 3186}
036407f7 3187
13a44ee0 3188/* A string for which we should create an IDENTIFIER_NODE at
d43829f9
MM
3189 startup. */
3190
3191typedef struct predefined_identifier
3192{
3193 /* The name of the identifier. */
8b60264b 3194 const char *const name;
d43829f9 3195 /* The place where the IDENTIFIER_NODE should be stored. */
8b60264b 3196 tree *const node;
838dfd8a 3197 /* Nonzero if this is the name of a constructor or destructor. */
8b60264b 3198 const int ctor_or_dtor_p;
d43829f9
MM
3199} predefined_identifier;
3200
3201/* Create all the predefined identifiers. */
3202
3203static void
11f6b451 3204initialize_predefined_identifiers (void)
d43829f9 3205{
8b60264b 3206 const predefined_identifier *pid;
d43829f9
MM
3207
3208 /* A table of identifiers to create at startup. */
8b60264b 3209 static const predefined_identifier predefined_identifiers[] = {
298d6f60
MM
3210 { "C++", &lang_name_cplusplus, 0 },
3211 { "C", &lang_name_c, 0 },
3212 { "Java", &lang_name_java, 0 },
d6eec208
MM
3213 /* Some of these names have a trailing space so that it is
3214 impossible for them to conflict with names written by users. */
3215 { "__ct ", &ctor_identifier, 1 },
3216 { "__base_ctor ", &base_ctor_identifier, 1 },
3217 { "__comp_ctor ", &complete_ctor_identifier, 1 },
3218 { "__dt ", &dtor_identifier, 1 },
3219 { "__comp_dtor ", &complete_dtor_identifier, 1 },
3220 { "__base_dtor ", &base_dtor_identifier, 1 },
3221 { "__deleting_dtor ", &deleting_dtor_identifier, 1 },
298d6f60 3222 { IN_CHARGE_NAME, &in_charge_identifier, 0 },
298d6f60
MM
3223 { "nelts", &nelts_identifier, 0 },
3224 { THIS_NAME, &this_identifier, 0 },
c4372ef4 3225 { VTABLE_DELTA_NAME, &delta_identifier, 0 },
298d6f60 3226 { VTABLE_PFN_NAME, &pfn_identifier, 0 },
298d6f60 3227 { "_vptr", &vptr_identifier, 0 },
3ec6bad3 3228 { "__vtt_parm", &vtt_parm_identifier, 0 },
82a2669e 3229 { "::", &global_scope_name, 0 },
1f6e1acc 3230 { "std", &std_identifier, 0 },
298d6f60 3231 { NULL, NULL, 0 }
d43829f9
MM
3232 };
3233
3234 for (pid = predefined_identifiers; pid->name; ++pid)
298d6f60
MM
3235 {
3236 *pid->node = get_identifier (pid->name);
3237 if (pid->ctor_or_dtor_p)
3238 IDENTIFIER_CTOR_OR_DTOR_P (*pid->node) = 1;
3239 }
d43829f9
MM
3240}
3241
8d08fdba
MS
3242/* Create the predefined scalar types of C,
3243 and some nodes representing standard constants (0, 1, (void *)0).
3244 Initialize the global binding level.
3245 Make definitions for built-in primitive functions. */
3246
3247void
11f6b451 3248cxx_init_decl_processing (void)
8d08fdba 3249{
10841285
MM
3250 tree void_ftype;
3251 tree void_ftype_ptr;
8d08fdba 3252
8c1d6d62 3253 build_common_tree_nodes (flag_signed_char, false);
6de66680 3254
d43829f9
MM
3255 /* Create all the identifiers we need. */
3256 initialize_predefined_identifiers ();
8d08fdba 3257
9cd64686
MM
3258 /* Create the global variables. */
3259 push_to_top_level ();
8012c983 3260
82a2669e 3261 current_function_decl = NULL_TREE;
1ec57cf0 3262 current_binding_level = NULL;
a1c65f9f 3263 /* Enter the global namespace. */
50bc768d 3264 gcc_assert (global_namespace == NULL_TREE);
82a2669e 3265 global_namespace = build_lang_decl (NAMESPACE_DECL, global_scope_name,
0cbd7506 3266 void_type_node);
b9e75696 3267 TREE_PUBLIC (global_namespace) = 1;
ac20c67a 3268 begin_scope (sk_namespace, global_namespace);
82a2669e 3269
30394414
JM
3270 current_lang_name = NULL_TREE;
3271
ddbfd28d
GK
3272 if (flag_visibility_ms_compat)
3273 default_visibility = VISIBILITY_HIDDEN;
3274
8d08fdba
MS
3275 /* Initially, C. */
3276 current_lang_name = lang_name_c;
3277
6bcedb4e 3278 /* Create the `std' namespace. */
1dbb6023
NS
3279 push_namespace (std_identifier);
3280 std_node = current_namespace;
3281 pop_namespace ();
5362b086 3282
eaa7c03f 3283 c_common_nodes_and_builtins ();
37c46b43 3284
4d8a1dd6
MM
3285 java_byte_type_node = record_builtin_java_type ("__java_byte", 8);
3286 java_short_type_node = record_builtin_java_type ("__java_short", 16);
3287 java_int_type_node = record_builtin_java_type ("__java_int", 32);
3288 java_long_type_node = record_builtin_java_type ("__java_long", 64);
3289 java_float_type_node = record_builtin_java_type ("__java_float", -32);
3290 java_double_type_node = record_builtin_java_type ("__java_double", -64);
3291 java_char_type_node = record_builtin_java_type ("__java_char", -16);
3292 java_boolean_type_node = record_builtin_java_type ("__java_boolean", -1);
eff71ab0 3293
7d60be94
NS
3294 integer_two_node = build_int_cst (NULL_TREE, 2);
3295 integer_three_node = build_int_cst (NULL_TREE, 3);
8d08fdba 3296
255512c1 3297 record_builtin_type (RID_BOOL, "bool", boolean_type_node);
de7df9eb
JM
3298 truthvalue_type_node = boolean_type_node;
3299 truthvalue_false_node = boolean_false_node;
3300 truthvalue_true_node = boolean_true_node;
2986ae00 3301
4cc1d462 3302 empty_except_spec = build_tree_list (NULL_TREE, NULL_TREE);
5fd8e536
JM
3303
3304#if 0
3e411c3f 3305 record_builtin_type (RID_MAX, NULL, string_type_node);
5fd8e536
JM
3306#endif
3307
1f84ec23
MM
3308 delta_type_node = ptrdiff_type_node;
3309 vtable_index_type = ptrdiff_type_node;
c7e266a6 3310
3ec6bad3 3311 vtt_parm_type = build_pointer_type (const_ptr_type_node);
10841285
MM
3312 void_ftype = build_function_type (void_type_node, void_list_node);
3313 void_ftype_ptr = build_function_type (void_type_node,
3314 tree_cons (NULL_TREE,
caf93cb0 3315 ptr_type_node,
10841285 3316 void_list_node));
824b9a4c 3317 void_ftype_ptr
4cc1d462 3318 = build_exception_variant (void_ftype_ptr, empty_except_spec);
8d08fdba 3319
8d08fdba
MS
3320 /* C++ extensions */
3321
3322 unknown_type_node = make_node (UNKNOWN_TYPE);
036407f7
ML
3323 record_unknown_type (unknown_type_node, "unknown type");
3324
8d08fdba
MS
3325 /* Indirecting an UNKNOWN_TYPE node yields an UNKNOWN_TYPE node. */
3326 TREE_TYPE (unknown_type_node) = unknown_type_node;
a6967cc0 3327
a6967cc0
JM
3328 /* Looking up TYPE_POINTER_TO and TYPE_REFERENCE_TO yield the same
3329 result. */
8d08fdba
MS
3330 TYPE_POINTER_TO (unknown_type_node) = unknown_type_node;
3331 TYPE_REFERENCE_TO (unknown_type_node) = unknown_type_node;
3332
c4372ef4
NS
3333 {
3334 /* Make sure we get a unique function type, so we can give
3335 its pointer type a name. (This wins for gdb.) */
3336 tree vfunc_type = make_node (FUNCTION_TYPE);
3337 TREE_TYPE (vfunc_type) = integer_type_node;
3338 TYPE_ARG_TYPES (vfunc_type) = NULL_TREE;
3339 layout_type (vfunc_type);
3340
3341 vtable_entry_type = build_pointer_type (vfunc_type);
3342 }
0e5921e8 3343 record_builtin_type (RID_MAX, VTBL_PTR_TYPE, vtable_entry_type);
8d08fdba 3344
8d08fdba 3345 vtbl_type_node
52bf7d5d 3346 = build_cplus_array_type (vtable_entry_type, NULL_TREE);
8d08fdba 3347 layout_type (vtbl_type_node);
91063b51 3348 vtbl_type_node = build_qualified_type (vtbl_type_node, TYPE_QUAL_CONST);
3e411c3f 3349 record_builtin_type (RID_MAX, NULL, vtbl_type_node);
849da744
MM
3350 vtbl_ptr_type_node = build_pointer_type (vtable_entry_type);
3351 layout_type (vtbl_ptr_type_node);
3e411c3f 3352 record_builtin_type (RID_MAX, NULL, vtbl_ptr_type_node);
8d08fdba 3353
1f84ec23
MM
3354 push_namespace (get_identifier ("__cxxabiv1"));
3355 abi_node = current_namespace;
3356 pop_namespace ();
6633d636 3357
2c73f9f5 3358 global_type_node = make_node (LANG_TYPE);
036407f7 3359 record_unknown_type (global_type_node, "global type");
2c73f9f5 3360
db5ae43f
MS
3361 /* Now, C++. */
3362 current_lang_name = lang_name_cplusplus;
8d08fdba 3363
ced78d8b 3364 {
4b0d3cbe
MM
3365 tree bad_alloc_id;
3366 tree bad_alloc_type_node;
3367 tree bad_alloc_decl;
3368 tree newtype, deltype;
10841285
MM
3369 tree ptr_ftype_sizetype;
3370
1dbb6023 3371 push_namespace (std_identifier);
4b0d3cbe
MM
3372 bad_alloc_id = get_identifier ("bad_alloc");
3373 bad_alloc_type_node = make_aggr_type (RECORD_TYPE);
3374 TYPE_CONTEXT (bad_alloc_type_node) = current_namespace;
caf93cb0 3375 bad_alloc_decl
4b0d3cbe
MM
3376 = create_implicit_typedef (bad_alloc_id, bad_alloc_type_node);
3377 DECL_CONTEXT (bad_alloc_decl) = current_namespace;
3378 TYPE_STUB_DECL (bad_alloc_type_node) = bad_alloc_decl;
1dbb6023 3379 pop_namespace ();
caf93cb0
EC
3380
3381 ptr_ftype_sizetype
10841285
MM
3382 = build_function_type (ptr_type_node,
3383 tree_cons (NULL_TREE,
c9f8536c 3384 size_type_node,
10841285 3385 void_list_node));
2c73f9f5 3386 newtype = build_exception_variant
0ba8a114
NS
3387 (ptr_ftype_sizetype, add_exception_specifier
3388 (NULL_TREE, bad_alloc_type_node, -1));
4cc1d462 3389 deltype = build_exception_variant (void_ftype_ptr, empty_except_spec);
596ea4e5
AS
3390 push_cp_library_fn (NEW_EXPR, newtype);
3391 push_cp_library_fn (VEC_NEW_EXPR, newtype);
3392 global_delete_fndecl = push_cp_library_fn (DELETE_EXPR, deltype);
3393 push_cp_library_fn (VEC_DELETE_EXPR, deltype);
ced78d8b 3394 }
8d08fdba
MS
3395
3396 abort_fndecl
1f84ec23 3397 = build_library_fn_ptr ("__cxa_pure_virtual", void_ftype);
8d08fdba 3398
8d08fdba
MS
3399 /* Perform other language dependent initializations. */
3400 init_class_processing ();
db48b831 3401 init_rtti_processing ();
8d08fdba 3402
6467930b 3403 if (flag_exceptions)
8d2733ca 3404 init_exception_processing ();
9e9ff709 3405
7fcdf4c2 3406 if (! supports_one_only ())
72b7eeff 3407 flag_weak = 0;
8d08fdba 3408
2ce07e2d 3409 make_fname_decl = cp_make_fname_decl;
0ba8a114 3410 start_fname_decls ();
8d08fdba 3411
e9a25f70 3412 /* Show we use EH for cleanups. */
6de9cd9a
DN
3413 if (flag_exceptions)
3414 using_eh_for_cleanups ();
62c154ed
JM
3415}
3416
0ba8a114 3417/* Generate an initializer for a function naming variable from
bb885938 3418 NAME. NAME may be NULL, to indicate a dependent name. TYPE_P is
f4f206f4 3419 filled in with the type of the init. */
0ba8a114
NS
3420
3421tree
bb885938 3422cp_fname_init (const char* name, tree *type_p)
0ba8a114
NS
3423{
3424 tree domain = NULL_TREE;
3425 tree type;
3426 tree init = NULL_TREE;
3427 size_t length = 0;
3428
3429 if (name)
3430 {
3431 length = strlen (name);
3432 domain = build_index_type (size_int (length));
3433 init = build_string (length + 1, name);
3434 }
caf93cb0 3435
0ba8a114
NS
3436 type = build_qualified_type (char_type_node, TYPE_QUAL_CONST);
3437 type = build_cplus_array_type (type, domain);
3438
bb885938 3439 *type_p = type;
caf93cb0 3440
0ba8a114
NS
3441 if (init)
3442 TREE_TYPE (init) = type;
3443 else
bb885938 3444 init = error_mark_node;
caf93cb0 3445
0ba8a114
NS
3446 return init;
3447}
3448
2ce07e2d
NS
3449/* Create the VAR_DECL for __FUNCTION__ etc. ID is the name to give the
3450 decl, NAME is the initialization string and TYPE_DEP indicates whether
3451 NAME depended on the type of the function. We make use of that to detect
0ba8a114 3452 __PRETTY_FUNCTION__ inside a template fn. This is being done
a692ad2e 3453 lazily at the point of first use, so we mustn't push the decl now. */
2ce07e2d
NS
3454
3455static tree
11f6b451 3456cp_make_fname_decl (tree id, int type_dep)
2ce07e2d 3457{
83182544 3458 const char *const name = (type_dep && processing_template_decl
e913996d 3459 ? NULL : fname_as_string (type_dep));
bb885938
NS
3460 tree type;
3461 tree init = cp_fname_init (name, &type);
3462 tree decl = build_decl (VAR_DECL, id, type);
2ce07e2d 3463
9e6f2e7d 3464 if (name)
68a57691 3465 free (CONST_CAST (name));
9e6f2e7d 3466
6cce57b0 3467 /* As we're using pushdecl_with_scope, we must set the context. */
0ba8a114
NS
3468 DECL_CONTEXT (decl) = current_function_decl;
3469 DECL_PRETTY_FUNCTION_P (decl) = type_dep;
caf93cb0 3470
2ce07e2d
NS
3471 TREE_STATIC (decl) = 1;
3472 TREE_READONLY (decl) = 1;
2ce07e2d 3473 DECL_ARTIFICIAL (decl) = 1;
caf93cb0 3474
0ba8a114 3475 TREE_USED (decl) = 1;
5362b086 3476
6cce57b0
JM
3477 if (current_function_decl)
3478 {
3479 struct cp_binding_level *b = current_binding_level;
a7e8c268 3480 while (b->level_chain->kind != sk_function_parms)
6cce57b0 3481 b = b->level_chain;
d63d5d0c 3482 pushdecl_with_scope (decl, b, /*is_friend=*/false);
3db45ab5 3483 cp_finish_decl (decl, init, /*init_const_expr_p=*/false, NULL_TREE,
d174af6c 3484 LOOKUP_ONLYCONVERTING);
e913996d 3485 }
ad16ae7f
MM
3486 else
3487 pushdecl_top_level_and_finish (decl, init);
caf93cb0 3488
2ce07e2d
NS
3489 return decl;
3490}
3491
c79efc4d
RÁE
3492static tree
3493builtin_function_1 (tree decl, tree context)
3494{
3495 tree id = DECL_NAME (decl);
3496 const char *name = IDENTIFIER_POINTER (id);
8d08fdba 3497
c79efc4d 3498 retrofit_lang_decl (decl);
0c11ada6 3499
c79efc4d
RÁE
3500 /* All nesting of C++ functions is lexical; there is never a "static
3501 chain" in the sense of GNU C nested functions. */
3502 DECL_NO_STATIC_CHAIN (decl) = 1;
3503
3504 DECL_ARTIFICIAL (decl) = 1;
3505 SET_OVERLOADED_OPERATOR_CODE (decl, ERROR_MARK);
3506 SET_DECL_LANGUAGE (decl, lang_c);
3507 /* Runtime library routines are, by definition, available in an
3508 external shared object. */
3509 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
3510 DECL_VISIBILITY_SPECIFIED (decl) = 1;
8d08fdba 3511
d52e4867 3512 DECL_CONTEXT (decl) = context;
12a22e76 3513
6bcedb4e 3514 pushdecl (decl);
6bcedb4e 3515
d63d5d0c
ILT
3516 /* A function in the user's namespace should have an explicit
3517 declaration before it is used. Mark the built-in function as
3518 anticipated but not actually declared. */
935d1834
ZW
3519 if (name[0] != '_' || name[1] != '_')
3520 DECL_ANTICIPATED (decl) = 1;
3521
8d08fdba
MS
3522 return decl;
3523}
7f4edbcb 3524
d52e4867 3525tree
c79efc4d 3526cxx_builtin_function (tree decl)
d52e4867 3527{
c79efc4d
RÁE
3528 tree id = DECL_NAME (decl);
3529 const char *name = IDENTIFIER_POINTER (id);
d52e4867
RS
3530 /* All builtins that don't begin with an '_' should additionally
3531 go in the 'std' namespace. */
3532 if (name[0] != '_')
3533 {
e5b44dfb 3534 tree decl2 = copy_node(decl);
d52e4867 3535 push_namespace (std_identifier);
e5b44dfb 3536 builtin_function_1 (decl2, std_node);
d52e4867
RS
3537 pop_namespace ();
3538 }
3539
e5b44dfb 3540 return builtin_function_1 (decl, NULL_TREE);
d52e4867
RS
3541}
3542
0c11ada6
JM
3543/* Generate a FUNCTION_DECL with the typical flags for a runtime library
3544 function. Not called directly. */
3545
3546static tree
11f6b451 3547build_library_fn_1 (tree name, enum tree_code operator_code, tree type)
0c11ada6
JM
3548{
3549 tree fn = build_lang_decl (FUNCTION_DECL, name, type);
3550 DECL_EXTERNAL (fn) = 1;
3551 TREE_PUBLIC (fn) = 1;
3552 DECL_ARTIFICIAL (fn) = 1;
596ea4e5 3553 SET_OVERLOADED_OPERATOR_CODE (fn, operator_code);
5d2ed28c 3554 SET_DECL_LANGUAGE (fn, lang_c);
73a8adb6
MM
3555 /* Runtime library routines are, by definition, available in an
3556 external shared object. */
3557 DECL_VISIBILITY (fn) = VISIBILITY_DEFAULT;
3558 DECL_VISIBILITY_SPECIFIED (fn) = 1;
0c11ada6
JM
3559 return fn;
3560}
c2a37c55 3561
0c11ada6
JM
3562/* Returns the _DECL for a library function with C linkage.
3563 We assume that such functions never throw; if this is incorrect,
3564 callers should unset TREE_NOTHROW. */
c2a37c55 3565
7f4edbcb 3566tree
11f6b451 3567build_library_fn (tree name, tree type)
0c11ada6 3568{
f59d2aad
RH
3569 tree fn = build_library_fn_1 (name, ERROR_MARK, type);
3570 TREE_NOTHROW (fn) = 1;
3571 return fn;
0c11ada6
JM
3572}
3573
3574/* Returns the _DECL for a library function with C++ linkage. */
3575
596ea4e5 3576static tree
11f6b451 3577build_cp_library_fn (tree name, enum tree_code operator_code, tree type)
0c11ada6 3578{
596ea4e5 3579 tree fn = build_library_fn_1 (name, operator_code, type);
0c11ada6 3580 TREE_NOTHROW (fn) = TYPE_NOTHROW_P (type);
6bbf1598 3581 DECL_CONTEXT (fn) = FROB_CONTEXT (current_namespace);
5d2ed28c 3582 SET_DECL_LANGUAGE (fn, lang_cplusplus);
0c11ada6
JM
3583 return fn;
3584}
3585
3586/* Like build_library_fn, but takes a C string instead of an
3587 IDENTIFIER_NODE. */
3588
3589tree
11f6b451 3590build_library_fn_ptr (const char* name, tree type)
7f4edbcb 3591{
0c11ada6
JM
3592 return build_library_fn (get_identifier (name), type);
3593}
3594
3595/* Like build_cp_library_fn, but takes a C string instead of an
3596 IDENTIFIER_NODE. */
3597
3598tree
11f6b451 3599build_cp_library_fn_ptr (const char* name, tree type)
0c11ada6 3600{
596ea4e5 3601 return build_cp_library_fn (get_identifier (name), ERROR_MARK, type);
0c11ada6
JM
3602}
3603
3604/* Like build_library_fn, but also pushes the function so that we will
3605 be able to find it via IDENTIFIER_GLOBAL_VALUE. */
3606
3607tree
11f6b451 3608push_library_fn (tree name, tree type)
0c11ada6
JM
3609{
3610 tree fn = build_library_fn (name, type);
3611 pushdecl_top_level (fn);
3612 return fn;
3613}
3614
3615/* Like build_cp_library_fn, but also pushes the function so that it
3616 will be found by normal lookup. */
3617
596ea4e5 3618static tree
11f6b451 3619push_cp_library_fn (enum tree_code operator_code, tree type)
0c11ada6 3620{
5362b086 3621 tree fn = build_cp_library_fn (ansi_opname (operator_code),
596ea4e5
AS
3622 operator_code,
3623 type);
0c11ada6
JM
3624 pushdecl (fn);
3625 return fn;
3626}
3627
3628/* Like push_library_fn, but takes a TREE_LIST of parm types rather than
3629 a FUNCTION_TYPE. */
3630
3631tree
11f6b451 3632push_void_library_fn (tree name, tree parmtypes)
0c11ada6
JM
3633{
3634 tree type = build_function_type (void_type_node, parmtypes);
3635 return push_library_fn (name, type);
3636}
3637
cf74fb86 3638/* Like push_library_fn, but also note that this function throws
0c11ada6
JM
3639 and does not return. Used for __throw_foo and the like. */
3640
3641tree
11f6b451 3642push_throw_library_fn (tree name, tree type)
0c11ada6 3643{
cf74fb86 3644 tree fn = push_library_fn (name, type);
0c11ada6
JM
3645 TREE_THIS_VOLATILE (fn) = 1;
3646 TREE_NOTHROW (fn) = 0;
3647 return fn;
7f4edbcb 3648}
8d08fdba 3649\f
61a127b3
MM
3650/* When we call finish_struct for an anonymous union, we create
3651 default copy constructors and such. But, an anonymous union
3652 shouldn't have such things; this function undoes the damage to the
3653 anonymous union type T.
3654
3655 (The reason that we create the synthesized methods is that we don't
3656 distinguish `union { int i; }' from `typedef union { int i; } U'.
3657 The first is an anonymous union; the second is just an ordinary
3658 union type.) */
3659
3660void
11f6b451 3661fixup_anonymous_aggr (tree t)
61a127b3
MM
3662{
3663 tree *q;
3664
f4f206f4 3665 /* Wipe out memory of synthesized methods. */
61a127b3
MM
3666 TYPE_HAS_CONSTRUCTOR (t) = 0;
3667 TYPE_HAS_DEFAULT_CONSTRUCTOR (t) = 0;
3668 TYPE_HAS_INIT_REF (t) = 0;
3669 TYPE_HAS_CONST_INIT_REF (t) = 0;
3670 TYPE_HAS_ASSIGN_REF (t) = 0;
61a127b3
MM
3671 TYPE_HAS_CONST_ASSIGN_REF (t) = 0;
3672
3673 /* Splice the implicitly generated functions out of the TYPE_METHODS
3674 list. */
3675 q = &TYPE_METHODS (t);
3676 while (*q)
3677 {
3678 if (DECL_ARTIFICIAL (*q))
3679 *q = TREE_CHAIN (*q);
3680 else
3681 q = &TREE_CHAIN (*q);
3682 }
3683
cab1f180 3684 /* ISO C++ 9.5.3. Anonymous unions may not have function members. */
61a127b3 3685 if (TYPE_METHODS (t))
ddd2d57e
RH
3686 error ("%Jan anonymous union cannot have function members",
3687 TYPE_MAIN_DECL (t));
a1c2b86d
JJ
3688
3689 /* Anonymous aggregates cannot have fields with ctors, dtors or complex
3690 assignment operators (because they cannot have these methods themselves).
3691 For anonymous unions this is already checked because they are not allowed
3692 in any union, otherwise we have to check it. */
3693 if (TREE_CODE (t) != UNION_TYPE)
3694 {
3695 tree field, type;
3696
3697 for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
3698 if (TREE_CODE (field) == FIELD_DECL)
3699 {
3700 type = TREE_TYPE (field);
3701 if (CLASS_TYPE_P (type))
3702 {
0cbd7506 3703 if (TYPE_NEEDS_CONSTRUCTING (type))
dee15844
JM
3704 error ("member %q+#D with constructor not allowed "
3705 "in anonymous aggregate", field);
a1c2b86d 3706 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
dee15844
JM
3707 error ("member %q+#D with destructor not allowed "
3708 "in anonymous aggregate", field);
a1c2b86d 3709 if (TYPE_HAS_COMPLEX_ASSIGN_REF (type))
dee15844
JM
3710 error ("member %q+#D with copy assignment operator "
3711 "not allowed in anonymous aggregate", field);
a1c2b86d
JJ
3712 }
3713 }
3714 }
61a127b3
MM
3715}
3716
72a93143 3717/* Make sure that a declaration with no declarator is well-formed, i.e.
a723baf1 3718 just declares a tagged type or anonymous union.
8d08fdba 3719
a723baf1 3720 Returns the type declared; or NULL_TREE if none. */
8d08fdba 3721
72a93143 3722tree
62d1db17 3723check_tag_decl (cp_decl_specifier_seq *declspecs)
8d08fdba 3724{
62d1db17
MM
3725 int saw_friend = declspecs->specs[(int)ds_friend] != 0;
3726 int saw_typedef = declspecs->specs[(int)ds_typedef] != 0;
a723baf1
MM
3727 /* If a class, struct, or enum type is declared by the DECLSPECS
3728 (i.e, if a class-specifier, enum-specifier, or non-typename
3729 elaborated-type-specifier appears in the DECLSPECS),
3730 DECLARED_TYPE is set to the corresponding type. */
3731 tree declared_type = NULL_TREE;
3732 bool error_p = false;
8d08fdba 3733
62d1db17 3734 if (declspecs->multiple_types_p)
72a93143 3735 error ("multiple types in one declaration");
62d1db17
MM
3736 else if (declspecs->redefined_builtin_type)
3737 {
3738 if (!in_system_header)
82b5e52a 3739 pedwarn ("redeclaration of C++ built-in type %qT",
62d1db17
MM
3740 declspecs->redefined_builtin_type);
3741 return NULL_TREE;
3742 }
7e2067ca 3743
c827f22f
MM
3744 if (declspecs->type
3745 && TYPE_P (declspecs->type)
caf93cb0 3746 && ((TREE_CODE (declspecs->type) != TYPENAME_TYPE
62d1db17
MM
3747 && IS_AGGR_TYPE (declspecs->type))
3748 || TREE_CODE (declspecs->type) == ENUMERAL_TYPE))
3749 declared_type = declspecs->type;
3750 else if (declspecs->type == error_mark_node)
3751 error_p = true;
a723baf1 3752 if (declared_type == NULL_TREE && ! saw_friend && !error_p)
7e2067ca 3753 pedwarn ("declaration does not declare anything");
1951a1b6 3754 /* Check for an anonymous union. */
a723baf1
MM
3755 else if (declared_type && IS_AGGR_TYPE_CODE (TREE_CODE (declared_type))
3756 && TYPE_ANONYMOUS_P (declared_type))
0dd3962d 3757 {
e8186ecf 3758 /* 7/3 In a simple-declaration, the optional init-declarator-list
0cbd7506
MS
3759 can be omitted only when declaring a class (clause 9) or
3760 enumeration (7.2), that is, when the decl-specifier-seq contains
3761 either a class-specifier, an elaborated-type-specifier with
3762 a class-key (9.1), or an enum-specifier. In these cases and
3763 whenever a class-specifier or enum-specifier is present in the
3764 decl-specifier-seq, the identifiers in these specifiers are among
3765 the names being declared by the declaration (as class-name,
3766 enum-names, or enumerators, depending on the syntax). In such
3767 cases, and except for the declaration of an unnamed bit-field (9.6),
3768 the decl-specifier-seq shall introduce one or more names into the
3769 program, or shall redeclare a name introduced by a previous
3770 declaration. [Example:
3771 enum { }; // ill-formed
3772 typedef class { }; // ill-formed
3773 --end example] */
e8186ecf 3774 if (saw_typedef)
0cbd7506
MS
3775 {
3776 error ("missing type-name in typedef-declaration");
3777 return NULL_TREE;
3778 }
0dd3962d 3779 /* Anonymous unions are objects, so they can have specifiers. */;
a723baf1 3780 SET_ANON_AGGR_TYPE_P (declared_type);
6bdb8141 3781
caf93cb0 3782 if (TREE_CODE (declared_type) != UNION_TYPE && pedantic
a723baf1 3783 && !in_system_header)
6bdb8141 3784 pedwarn ("ISO C++ prohibits anonymous structs");
0dd3962d
JM
3785 }
3786
62d1db17 3787 else
8d08fdba 3788 {
62d1db17
MM
3789 if (declspecs->specs[(int)ds_inline]
3790 || declspecs->specs[(int)ds_virtual])
2d01edd7 3791 error ("%qs can only be specified for functions",
caf93cb0 3792 declspecs->specs[(int)ds_inline]
62d1db17
MM
3793 ? "inline" : "virtual");
3794 else if (saw_friend
caf93cb0 3795 && (!current_class_type
62d1db17 3796 || current_scope () != current_class_type))
2d01edd7 3797 error ("%<friend%> can only be specified inside a class");
62d1db17 3798 else if (declspecs->specs[(int)ds_explicit])
2d01edd7 3799 error ("%<explicit%> can only be specified for constructors");
62d1db17
MM
3800 else if (declspecs->storage_class)
3801 error ("a storage class can only be specified for objects "
3802 "and functions");
3803 else if (declspecs->specs[(int)ds_const]
3804 || declspecs->specs[(int)ds_volatile]
3805 || declspecs->specs[(int)ds_restrict]
3806 || declspecs->specs[(int)ds_thread])
3807 error ("qualifiers can only be specified for objects "
3808 "and functions");
41dc91a8
SB
3809 else if (saw_typedef)
3810 warning (0, "%<typedef%> was ignored in this declaration");
72a93143 3811 }
8d08fdba 3812
a723baf1 3813 return declared_type;
72a93143
JM
3814}
3815
3816/* Called when a declaration is seen that contains no names to declare.
3817 If its type is a reference to a structure, union or enum inherited
3818 from a containing scope, shadow that tag name for the current scope
3819 with a forward reference.
3820 If its type defines a new named structure or union
3821 or defines an enum, it is valid but we need not do anything here.
3822 Otherwise, it is an error.
3823
3824 C++: may have to grok the declspecs to learn about static,
caf93cb0 3825 complain for anonymous unions.
72a93143 3826
a723baf1
MM
3827 Returns the TYPE declared -- or NULL_TREE if none. */
3828
3829tree
62d1db17 3830shadow_tag (cp_decl_specifier_seq *declspecs)
72a93143
JM
3831{
3832 tree t = check_tag_decl (declspecs);
3833
a723baf1
MM
3834 if (!t)
3835 return NULL_TREE;
3836
3cabd8f9
MA
3837 if (declspecs->attributes)
3838 {
dee15844
JM
3839 warning (0, "attribute ignored in declaration of %q+#T", t);
3840 warning (0, "attribute for %q+#T must follow the %qs keyword",
3841 t, class_key_or_enum_as_string (t));
3cabd8f9
MA
3842
3843 }
3844
d2a8ac2c
LM
3845 if (maybe_process_partial_specialization (t) == error_mark_node)
3846 return NULL_TREE;
72a93143
JM
3847
3848 /* This is where the variables in an anonymous union are
3849 declared. An anonymous union declaration looks like:
3850 union { ... } ;
3851 because there is no declarator after the union, the parser
3852 sends that declaration here. */
a723baf1 3853 if (ANON_AGGR_TYPE_P (t))
72a93143 3854 {
6bdb8141 3855 fixup_anonymous_aggr (t);
72a93143
JM
3856
3857 if (TYPE_FIELDS (t))
3858 {
caf93cb0 3859 tree decl = grokdeclarator (/*declarator=*/NULL,
058b15c1 3860 declspecs, NORMAL, 0, NULL);
72a93143
JM
3861 finish_anon_union (decl);
3862 }
8d08fdba 3863 }
a723baf1
MM
3864
3865 return t;
8d08fdba
MS
3866}
3867\f
3868/* Decode a "typename", such as "int **", returning a ..._TYPE node. */
3869
3870tree
caf93cb0 3871groktypename (cp_decl_specifier_seq *type_specifiers,
62d1db17 3872 const cp_declarator *declarator)
8d08fdba 3873{
62d1db17 3874 tree attrs;
98884b26 3875 tree type;
62d1db17
MM
3876 attrs = type_specifiers->attributes;
3877 type_specifiers->attributes = NULL_TREE;
3878 type = grokdeclarator (declarator, type_specifiers, TYPENAME, 0, &attrs);
98884b26
JM
3879 if (attrs)
3880 cplus_decl_attributes (&type, attrs, 0);
3881 return type;
8d08fdba
MS
3882}
3883
3884/* Decode a declarator in an ordinary declaration or data definition.
3885 This is called as soon as the type information and variable name
3886 have been parsed, before parsing the initializer if any.
3887 Here we create the ..._DECL node, fill in its type,
3888 and put it on the list of decls for the current context.
3889 The ..._DECL node is returned as the value.
3890
3891 Exception: for arrays where the length is not specified,
82580166 3892 the type is left null, to be filled in by `cp_finish_decl'.
8d08fdba
MS
3893
3894 Function definitions do not come here; they go to start_function
3895 instead. However, external and forward declarations of functions
3896 do go through here. Structure field declarations are done by
3897 grokfield and not through here. */
3898
8d08fdba 3899tree
caf93cb0 3900start_decl (const cp_declarator *declarator,
62d1db17 3901 cp_decl_specifier_seq *declspecs,
0cbd7506
MS
3902 int initialized,
3903 tree attributes,
3904 tree prefix_attributes,
4514aa8c 3905 tree *pushed_scope_p)
8d08fdba 3906{
59387d2e 3907 tree decl;
926ce8bd 3908 tree type, tem;
8d08fdba 3909 tree context;
c3b7031d 3910 bool was_public;
8d08fdba 3911
4514aa8c 3912 *pushed_scope_p = NULL_TREE;
c8094d83 3913
e23bd218
IR
3914 /* An object declared as __attribute__((deprecated)) suppresses
3915 warnings of uses of other deprecated items. */
3916 if (lookup_attribute ("deprecated", attributes))
3917 deprecated_state = DEPRECATED_SUPPRESS;
3918
91d231cb 3919 attributes = chainon (attributes, prefix_attributes);
b17e2870 3920
c11b6f21 3921 decl = grokdeclarator (declarator, declspecs, NORMAL, initialized,
91d231cb 3922 &attributes);
68642fb6 3923
e23bd218
IR
3924 deprecated_state = DEPRECATED_NORMAL;
3925
2d00b4f2
AP
3926 if (decl == NULL_TREE || TREE_CODE (decl) == VOID_TYPE
3927 || decl == error_mark_node)
44370687 3928 return error_mark_node;
8d08fdba
MS
3929
3930 type = TREE_TYPE (decl);
3931
4f1c5b7d 3932 context = DECL_CONTEXT (decl);
8d08fdba 3933
73a8adb6 3934 if (context)
4514aa8c
NS
3935 {
3936 *pushed_scope_p = push_scope (context);
c8094d83 3937
4514aa8c
NS
3938 /* We are only interested in class contexts, later. */
3939 if (TREE_CODE (context) == NAMESPACE_DECL)
3940 context = NULL_TREE;
3941 }
2c73f9f5 3942
8d08fdba
MS
3943 if (initialized)
3944 /* Is it valid for this decl to have an initializer at all?
3945 If not, set INITIALIZED to zero, which will indirectly
82580166 3946 tell `cp_finish_decl' to ignore the initializer once it is parsed. */
8d08fdba
MS
3947 switch (TREE_CODE (decl))
3948 {
3949 case TYPE_DECL:
2d01edd7 3950 error ("typedef %qD is initialized (use __typeof__ instead)", decl);
cc011e7f 3951 return error_mark_node;
8d08fdba
MS
3952
3953 case FUNCTION_DECL:
2d01edd7 3954 error ("function %q#D is initialized like a variable", decl);
650fcd07 3955 return error_mark_node;
8d08fdba
MS
3956
3957 default:
3e41d13b 3958 break;
8d08fdba
MS
3959 }
3960
8d08fdba
MS
3961 if (initialized)
3962 {
a9aedbc2 3963 if (! toplevel_bindings_p ()
8d08fdba 3964 && DECL_EXTERNAL (decl))
d4ee4d25 3965 warning (0, "declaration of %q#D has %<extern%> and is initialized",
0cbd7506 3966 decl);
8d08fdba 3967 DECL_EXTERNAL (decl) = 0;
5566b478 3968 if (toplevel_bindings_p ())
8d08fdba 3969 TREE_STATIC (decl) = 1;
8d08fdba
MS
3970 }
3971
fa20888b 3972 /* Set attributes here so if duplicate decl, will have proper attributes. */
91d231cb 3973 cplus_decl_attributes (&decl, attributes, 0);
fa20888b 3974
4e2bb0a4
DS
3975 /* Dllimported symbols cannot be defined. Static data members (which
3976 can be initialized in-class and dllimported) go through grokfield,
3977 not here, so we don't need to exclude those decls when checking for
3978 a definition. */
3979 if (initialized && DECL_DLLIMPORT_P (decl))
3980 {
3981 error ("definition of %q#D is marked %<dllimport%>", decl);
3982 DECL_DLLIMPORT_P (decl) = 0;
3983 }
3984
ecb0eece 3985 /* If #pragma weak was used, mark the decl weak now. */
3693d46c 3986 maybe_apply_pragma_weak (decl);
ecb0eece 3987
97055d5c
AO
3988 if (TREE_CODE (decl) == FUNCTION_DECL
3989 && DECL_DECLARED_INLINE_P (decl)
3990 && DECL_UNINLINABLE (decl)
3991 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
dee15844 3992 warning (0, "inline function %q+D given attribute noinline", decl);
97055d5c 3993
d0f062fb 3994 if (context && COMPLETE_TYPE_P (complete_type (context)))
5b605f68
MS
3995 {
3996 if (TREE_CODE (decl) == VAR_DECL)
3997 {
86ac0575 3998 tree field = lookup_field (context, DECL_NAME (decl), 0, false);
5b605f68 3999 if (field == NULL_TREE || TREE_CODE (field) != VAR_DECL)
2d01edd7 4000 error ("%q#D is not a static member of %q#T", decl, context);
e349ee73
MS
4001 else
4002 {
4003 if (DECL_CONTEXT (field) != context)
f2d773a2 4004 {
a723baf1 4005 if (!same_type_p (DECL_CONTEXT (field), context))
2d01edd7 4006 pedwarn ("ISO C++ does not permit %<%T::%D%> "
0cbd7506 4007 "to be defined as %<%T::%D%>",
a723baf1
MM
4008 DECL_CONTEXT (field), DECL_NAME (decl),
4009 context, DECL_NAME (decl));
f2d773a2
JM
4010 DECL_CONTEXT (decl) = DECL_CONTEXT (field);
4011 }
91146169
GB
4012 if (processing_specialization
4013 && template_class_depth (context) == 0
4014 && CLASSTYPE_TEMPLATE_SPECIALIZATION (context))
4015 error ("template header not allowed in member definition "
4016 "of explicitly specialized class");
75650646
MM
4017 /* Static data member are tricky; an in-class initialization
4018 still doesn't provide a definition, so the in-class
4019 declaration will have DECL_EXTERNAL set, but will have an
4020 initialization. Thus, duplicate_decls won't warn
4021 about this situation, and so we check here. */
e92fb501 4022 if (initialized && DECL_INITIALIZED_IN_CLASS_P (field))
9e637a26 4023 error ("duplicate initialization of %qD", decl);
d63d5d0c 4024 if (duplicate_decls (decl, field, /*newdecl_is_friend=*/false))
e349ee73
MS
4025 decl = field;
4026 }
5b605f68 4027 }
f30432d7
MS
4028 else
4029 {
d43f603d 4030 tree field = check_classfn (context, decl,
44021471
GB
4031 (processing_template_decl
4032 > template_class_depth (context))
4033 ? current_template_parms
4034 : NULL_TREE);
f8c3b097
PC
4035 if (field && field != error_mark_node
4036 && duplicate_decls (decl, field,
4037 /*newdecl_is_friend=*/false))
f30432d7
MS
4038 decl = field;
4039 }
4040
4041 /* cp_finish_decl sets DECL_EXTERNAL if DECL_IN_AGGR_P is set. */
dff3e828 4042 DECL_IN_AGGR_P (decl) = 0;
9267ee62
NS
4043 /* Do not mark DECL as an explicit specialization if it was not
4044 already marked as an instantiation; a declaration should
4045 never be marked as a specialization unless we know what
c8094d83 4046 template is being specialized. */
9267ee62 4047 if (DECL_LANG_SPECIFIC (decl) && DECL_USE_TEMPLATE (decl))
84e6233f 4048 {
9267ee62 4049 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
c8094d83 4050
84e6233f
JM
4051 /* [temp.expl.spec] An explicit specialization of a static data
4052 member of a template is a definition if the declaration
4053 includes an initializer; otherwise, it is a declaration.
c8094d83 4054
84e6233f
JM
4055 We check for processing_specialization so this only applies
4056 to the new specialization syntax. */
e92fb501 4057 if (!initialized && processing_specialization)
84e6233f
JM
4058 DECL_EXTERNAL (decl) = 1;
4059 }
f30432d7 4060
b7698cf0 4061 if (DECL_EXTERNAL (decl) && ! DECL_TEMPLATE_SPECIALIZATION (decl))
2d01edd7 4062 pedwarn ("declaration of %q#D outside of class is not definition",
0cbd7506 4063 decl);
5b605f68
MS
4064 }
4065
c3b7031d
JM
4066 was_public = TREE_PUBLIC (decl);
4067
9188c363
MM
4068 /* Enter this declaration into the symbol table. */
4069 tem = maybe_push_decl (decl);
2ee887f2 4070
5156628f 4071 if (processing_template_decl)
cd9f6678 4072 tem = push_template_decl (tem);
156fc2bb
KL
4073 if (tem == error_mark_node)
4074 return error_mark_node;
5566b478 4075
3b426391 4076 /* Tell the back end to use or not use .common as appropriate. If we say
a50f0918
MS
4077 -fconserve-space, we want this to save .data space, at the expense of
4078 wrong semantics. If we say -fno-conserve-space, we want this to
4079 produce errors about redefs; to do this we force variables into the
4080 data segment. */
434aeebb
RS
4081 if (flag_conserve_space
4082 && TREE_CODE (tem) == VAR_DECL
4083 && TREE_PUBLIC (tem)
4084 && !DECL_THREAD_LOCAL_P (tem)
4085 && !have_global_bss_p ())
4086 DECL_COMMON (tem) = 1;
68642fb6 4087
c3b7031d
JM
4088 if (TREE_CODE (tem) == VAR_DECL
4089 && DECL_NAMESPACE_SCOPE_P (tem) && !TREE_PUBLIC (tem) && !was_public
4090 && !DECL_THIS_STATIC (tem) && !DECL_ARTIFICIAL (tem))
4091 {
4092 /* This is a const variable with implicit 'static'. Set
4093 DECL_THIS_STATIC so we can tell it from variables that are
4094 !TREE_PUBLIC because of the anonymous namespace. */
4095 gcc_assert (cp_type_readonly (TREE_TYPE (tem)));
4096 DECL_THIS_STATIC (tem) = 1;
4097 }
4098
e92fb501
MM
4099 if (!processing_template_decl && TREE_CODE (tem) == VAR_DECL)
4100 start_decl_1 (tem, initialized);
8d08fdba 4101
8d08fdba
MS
4102 return tem;
4103}
4104
5566b478 4105void
e92fb501 4106start_decl_1 (tree decl, bool initialized)
8d08fdba 4107{
e92fb501 4108 tree type;
8d08fdba 4109
e92fb501
MM
4110 gcc_assert (!processing_template_decl);
4111
4112 if (error_operand_p (decl))
44689c12
ML
4113 return;
4114
e92fb501
MM
4115 gcc_assert (TREE_CODE (decl) == VAR_DECL);
4116 type = TREE_TYPE (decl);
4117
5566b478
MS
4118 if (initialized)
4119 /* Is it valid for this decl to have an initializer at all?
4120 If not, set INITIALIZED to zero, which will indirectly
4121 tell `cp_finish_decl' to ignore the initializer once it is parsed. */
8d08fdba 4122 {
5566b478
MS
4123 /* Don't allow initializations for incomplete types except for
4124 arrays which might be completed by the initialization. */
d0f062fb 4125 if (COMPLETE_TYPE_P (complete_type (type)))
5566b478
MS
4126 ; /* A complete type is ok. */
4127 else if (TREE_CODE (type) != ARRAY_TYPE)
8d08fdba 4128 {
2d01edd7 4129 error ("variable %q#D has initializer but incomplete type", decl);
5566b478 4130 initialized = 0;
25eb19ff 4131 type = TREE_TYPE (decl) = error_mark_node;
5566b478 4132 }
d0f062fb 4133 else if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (type))))
5566b478
MS
4134 {
4135 if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
2d01edd7 4136 error ("elements of array %q#D have incomplete type", decl);
5566b478
MS
4137 /* else we already gave an error in start_decl. */
4138 initialized = 0;
8d08fdba 4139 }
8d08fdba 4140 }
e92fb501
MM
4141 else if (IS_AGGR_TYPE (type)
4142 && ! DECL_EXTERNAL (decl))
8d08fdba 4143 {
e92fb501 4144 if (!COMPLETE_TYPE_P (complete_type (type)))
5566b478 4145 {
2d01edd7 4146 error ("aggregate %q#D has incomplete type and cannot be defined",
5566b478
MS
4147 decl);
4148 /* Change the type so that assemble_variable will give
4149 DECL an rtl we can live with: (mem (const_int 0)). */
25eb19ff 4150 type = TREE_TYPE (decl) = error_mark_node;
5566b478
MS
4151 }
4152 else
4153 {
4154 /* If any base type in the hierarchy of TYPE needs a constructor,
4155 then we set initialized to 1. This way any nodes which are
4156 created for the purposes of initializing this aggregate
4157 will live as long as it does. This is necessary for global
4158 aggregates which do not have their initializers processed until
4159 the end of the file. */
4160 initialized = TYPE_NEEDS_CONSTRUCTING (type);
4161 }
4162 }
4163
c6671cbb
MM
4164 /* Create a new scope to hold this declaration if necessary.
4165 Whether or not a new scope is necessary cannot be determined
4166 until after the type has been completed; if the type is a
4167 specialization of a class template it is not until after
4168 instantiation has occurred that TYPE_HAS_NONTRIVIAL_DESTRUCTOR
4169 will be set correctly. */
4170 maybe_push_cleanup_level (type);
5566b478
MS
4171}
4172
7e99327d
MM
4173/* Handle initialization of references. DECL, TYPE, and INIT have the
4174 same meaning as in cp_finish_decl. *CLEANUP must be NULL on entry,
4175 but will be set to a new CLEANUP_STMT if a temporary is created
cd0be382 4176 that must be destroyed subsequently.
7e99327d
MM
4177
4178 Returns an initializer expression to use to initialize DECL, or
4179 NULL if the initialization can be performed statically.
e92cc029
MS
4180
4181 Quotes on semantics can be found in ARM 8.4.3. */
4182
8e4ce833 4183static tree
7e99327d 4184grok_reference_init (tree decl, tree type, tree init, tree *cleanup)
5566b478
MS
4185{
4186 tree tmp;
4187
4188 if (init == NULL_TREE)
4189 {
4190 if ((DECL_LANG_SPECIFIC (decl) == 0
4191 || DECL_IN_AGGR_P (decl) == 0)
4192 && ! DECL_THIS_EXTERN (decl))
c4f73174 4193 error ("%qD declared as reference but not initialized", decl);
8e4ce833 4194 return NULL_TREE;
5566b478
MS
4195 }
4196
ed5511d9 4197 if (TREE_CODE (init) == CONSTRUCTOR)
5566b478 4198 {
2d01edd7 4199 error ("ISO C++ forbids use of initializer list to "
0cbd7506 4200 "initialize reference %qD", decl);
8e4ce833 4201 return NULL_TREE;
8d08fdba
MS
4202 }
4203
4204 if (TREE_CODE (init) == TREE_LIST)
c7b62f14 4205 init = build_x_compound_expr_from_list (init, "initializer");
8d08fdba
MS
4206
4207 if (TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE
4208 && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE)
0a72704b
MM
4209 /* Note: default conversion is only called in very special cases. */
4210 init = decay_conversion (init);
68642fb6 4211
24bef158
MM
4212 /* Convert INIT to the reference type TYPE. This may involve the
4213 creation of a temporary, whose lifetime must be the same as that
350fae66
RK
4214 of the reference. If so, a DECL_EXPR for the temporary will be
4215 added just after the DECL_EXPR for DECL. That's why we don't set
24bef158
MM
4216 DECL_INITIAL for local references (instead assigning to them
4217 explicitly); we need to allow the temporary to be initialized
4218 first. */
7e99327d 4219 tmp = initialize_reference (type, init, decl, cleanup);
8d08fdba 4220
a3203465 4221 if (tmp == error_mark_node)
8e4ce833
JJ
4222 return NULL_TREE;
4223 else if (tmp == NULL_TREE)
8d08fdba 4224 {
2d01edd7 4225 error ("cannot initialize %qT from %qT", type, TREE_TYPE (init));
8e4ce833 4226 return NULL_TREE;
8d08fdba 4227 }
8d08fdba 4228
8e4ce833
JJ
4229 if (TREE_STATIC (decl) && !TREE_CONSTANT (tmp))
4230 return tmp;
4231
08ac397c 4232 DECL_INITIAL (decl) = tmp;
8e4ce833
JJ
4233
4234 return NULL_TREE;
8d08fdba
MS
4235}
4236
76239779
MM
4237/* Designated initializers in arrays are not supported in GNU C++.
4238 The parser cannot detect this error since it does not know whether
4239 a given brace-enclosed initializer is for a class type or for an
4240 array. This function checks that CE does not use a designated
4241 initializer. If it does, an error is issued. Returns true if CE
4242 is valid, i.e., does not have a designated initializer. */
4243
4244static bool
4245check_array_designated_initializer (const constructor_elt *ce)
4246{
48c9a7f0 4247 /* Designated initializers for array elements are not supported. */
76239779
MM
4248 if (ce->index)
4249 {
4250 /* The parser only allows identifiers as designated
4251 intializers. */
4252 gcc_assert (TREE_CODE (ce->index) == IDENTIFIER_NODE);
4253 error ("name %qD used in a GNU-style designated "
4254 "initializer for an array", ce->index);
4255 return false;
4256 }
4257
4258 return true;
4259}
4260
27778b73
MM
4261/* When parsing `int a[] = {1, 2};' we don't know the size of the
4262 array until we finish parsing the initializer. If that's the
4263 situation we're in, update DECL accordingly. */
4264
4265static void
11f6b451 4266maybe_deduce_size_from_array_init (tree decl, tree init)
27778b73
MM
4267{
4268 tree type = TREE_TYPE (decl);
4269
4270 if (TREE_CODE (type) == ARRAY_TYPE
4271 && TYPE_DOMAIN (type) == NULL_TREE
4272 && TREE_CODE (decl) != TYPE_DECL)
4273 {
f2ae0c45
JM
4274 /* do_default is really a C-ism to deal with tentative definitions.
4275 But let's leave it here to ease the eventual merge. */
4276 int do_default = !DECL_EXTERNAL (decl);
27778b73 4277 tree initializer = init ? init : DECL_INITIAL (decl);
76239779 4278 int failure = 0;
27778b73 4279
76239779
MM
4280 /* Check that there are no designated initializers in INIT, as
4281 those are not supported in GNU C++, and as the middle-end
4282 will crash if presented with a non-numeric designated
4283 initializer. */
4284 if (initializer && TREE_CODE (initializer) == CONSTRUCTOR)
2b643eda 4285 {
76239779
MM
4286 VEC(constructor_elt,gc) *v = CONSTRUCTOR_ELTS (initializer);
4287 constructor_elt *ce;
4288 HOST_WIDE_INT i;
4289 for (i = 0;
4290 VEC_iterate (constructor_elt, v, i, ce);
4291 ++i)
4292 if (!check_array_designated_initializer (ce))
4293 failure = 1;
2b643eda 4294 }
76239779
MM
4295
4296 if (!failure)
27778b73 4297 {
76239779
MM
4298 failure = cp_complete_array_type (&TREE_TYPE (decl), initializer,
4299 do_default);
4300 if (failure == 1)
2b643eda 4301 {
76239779
MM
4302 error ("initializer fails to determine size of %qD", decl);
4303 TREE_TYPE (decl) = error_mark_node;
4304 }
4305 else if (failure == 2)
4306 {
4307 if (do_default)
4308 {
4309 error ("array size missing in %qD", decl);
4310 TREE_TYPE (decl) = error_mark_node;
4311 }
4312 /* If a `static' var's size isn't known, make it extern as
4313 well as static, so it does not get allocated. If it's not
4314 `static', then don't mark it extern; finish_incomplete_decl
4315 will give it a default size and it will get allocated. */
4316 else if (!pedantic && TREE_STATIC (decl) && !TREE_PUBLIC (decl))
4317 DECL_EXTERNAL (decl) = 1;
4318 }
4319 else if (failure == 3)
4320 {
4321 error ("zero-size array %qD", decl);
2b643eda
MM
4322 TREE_TYPE (decl) = error_mark_node;
4323 }
2b643eda 4324 }
27778b73 4325
f23b8501
JJ
4326 cp_apply_type_quals_to_decl (cp_type_quals (TREE_TYPE (decl)), decl);
4327
27778b73
MM
4328 layout_decl (decl, 0);
4329 }
4330}
4331
4332/* Set DECL_SIZE, DECL_ALIGN, etc. for DECL (a VAR_DECL), and issue
57b52417 4333 any appropriate error messages regarding the layout. */
27778b73 4334
57b52417 4335static void
11f6b451 4336layout_var_decl (tree decl)
27778b73 4337{
2b643eda
MM
4338 tree type;
4339
2b643eda
MM
4340 type = TREE_TYPE (decl);
4341 if (type == error_mark_node)
4342 return;
57b52417
MM
4343
4344 /* If we haven't already layed out this declaration, do so now.
4345 Note that we must not call complete type for an external object
4346 because it's type might involve templates that we are not
34cd5ae7 4347 supposed to instantiate yet. (And it's perfectly valid to say
57b52417
MM
4348 `extern X x' for some incomplete type `X'.) */
4349 if (!DECL_EXTERNAL (decl))
4350 complete_type (type);
caf93cb0 4351 if (!DECL_SIZE (decl)
328de7c2 4352 && TREE_TYPE (decl) != error_mark_node
7de85f7e 4353 && (COMPLETE_TYPE_P (type)
caf93cb0 4354 || (TREE_CODE (type) == ARRAY_TYPE
7de85f7e
MM
4355 && !TYPE_DOMAIN (type)
4356 && COMPLETE_TYPE_P (TREE_TYPE (type)))))
27778b73
MM
4357 layout_decl (decl, 0);
4358
c82dbd95 4359 if (!DECL_EXTERNAL (decl) && DECL_SIZE (decl) == NULL_TREE)
27778b73
MM
4360 {
4361 /* An automatic variable with an incomplete type: that is an error.
4362 Don't talk about array types here, since we took care of that
4363 message in grokdeclarator. */
2d01edd7 4364 error ("storage size of %qD isn't known", decl);
27778b73
MM
4365 TREE_TYPE (decl) = error_mark_node;
4366 }
ae673f14
JM
4367#if 0
4368 /* Keep this code around in case we later want to control debug info
4369 based on whether a type is "used". (jason 1999-11-11) */
4370
27778b73
MM
4371 else if (!DECL_EXTERNAL (decl) && IS_AGGR_TYPE (ttype))
4372 /* Let debugger know it should output info for this type. */
4373 note_debug_info_needed (ttype);
4374
4375 if (TREE_STATIC (decl) && DECL_CLASS_SCOPE_P (decl))
4376 note_debug_info_needed (DECL_CONTEXT (decl));
ae673f14 4377#endif
27778b73
MM
4378
4379 if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl))
4380 && DECL_SIZE (decl) != NULL_TREE
4381 && ! TREE_CONSTANT (DECL_SIZE (decl)))
4382 {
4383 if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
4384 constant_expression_warning (DECL_SIZE (decl));
4385 else
2d01edd7 4386 error ("storage size of %qD isn't constant", decl);
27778b73
MM
4387 }
4388}
4389
27778b73
MM
4390/* If a local static variable is declared in an inline function, or if
4391 we have a weak definition, we must endeavor to create only one
4392 instance of the variable at link-time. */
4393
4394static void
11f6b451 4395maybe_commonize_var (tree decl)
27778b73
MM
4396{
4397 /* Static data in a function with comdat linkage also has comdat
4398 linkage. */
4399 if (TREE_STATIC (decl)
4400 /* Don't mess with __FUNCTION__. */
cf74fb86 4401 && ! DECL_ARTIFICIAL (decl)
0b50d7f1
MM
4402 && DECL_FUNCTION_SCOPE_P (decl)
4403 /* Unfortunately, import_export_decl has not always been called
4404 before the function is processed, so we cannot simply check
caf93cb0 4405 DECL_COMDAT. */
9e6aaf5e 4406 && (DECL_COMDAT (DECL_CONTEXT (decl))
0b50d7f1
MM
4407 || ((DECL_DECLARED_INLINE_P (DECL_CONTEXT (decl))
4408 || DECL_TEMPLATE_INSTANTIATION (DECL_CONTEXT (decl)))
4409 && TREE_PUBLIC (DECL_CONTEXT (decl)))))
27778b73 4410 {
0dbc5cd3 4411 if (flag_weak)
27778b73 4412 {
0dbc5cd3
MM
4413 /* With weak symbols, we simply make the variable COMDAT;
4414 that will cause copies in multiple translations units to
4415 be merged. */
4416 comdat_linkage (decl);
4417 }
4418 else
4419 {
4420 if (DECL_INITIAL (decl) == NULL_TREE
4421 || DECL_INITIAL (decl) == error_mark_node)
27778b73 4422 {
0dbc5cd3
MM
4423 /* Without weak symbols, we can use COMMON to merge
4424 uninitialized variables. */
27778b73
MM
4425 TREE_PUBLIC (decl) = 1;
4426 DECL_COMMON (decl) = 1;
4427 }
0dbc5cd3 4428 else
27778b73 4429 {
0dbc5cd3
MM
4430 /* While for initialized variables, we must use internal
4431 linkage -- which means that multiple copies will not
4432 be merged. */
4433 TREE_PUBLIC (decl) = 0;
4434 DECL_COMMON (decl) = 0;
dee15844
JM
4435 warning (0, "sorry: semantics of inline function static "
4436 "data %q+#D are wrong (you'll wind up "
4437 "with multiple copies)", decl);
d4ee4d25 4438 warning (0, "%J you can work around this by removing "
0cbd7506 4439 "the initializer",
ddd2d57e 4440 decl);
27778b73
MM
4441 }
4442 }
4443 }
4444 else if (DECL_LANG_SPECIFIC (decl) && DECL_COMDAT (decl))
4445 /* Set it up again; we might have set DECL_INITIAL since the last
4446 time. */
4447 comdat_linkage (decl);
4448}
4449
91063b51
MM
4450/* Issue an error message if DECL is an uninitialized const variable. */
4451
4452static void
11f6b451 4453check_for_uninitialized_const_var (tree decl)
91063b51
MM
4454{
4455 tree type = TREE_TYPE (decl);
4456
4457 /* ``Unless explicitly declared extern, a const object does not have
4458 external linkage and must be initialized. ($8.4; $12.1)'' ARM
4459 7.1.6 */
4460 if (TREE_CODE (decl) == VAR_DECL
4461 && TREE_CODE (type) != REFERENCE_TYPE
4462 && CP_TYPE_CONST_P (type)
4463 && !TYPE_NEEDS_CONSTRUCTING (type)
4464 && !DECL_INITIAL (decl))
2d01edd7 4465 error ("uninitialized const %qD", decl);
91063b51
MM
4466}
4467
4038c495
GB
4468\f
4469/* Structure holding the current initializer being processed by reshape_init.
4470 CUR is a pointer to the current element being processed, END is a pointer
4471 after the last element present in the initializer. */
4472typedef struct reshape_iterator_t
4473{
4474 constructor_elt *cur;
4475 constructor_elt *end;
4476} reshape_iter;
4477
4478static tree reshape_init_r (tree, reshape_iter *, bool);
4479
8e3df2de
MM
4480/* FIELD is a FIELD_DECL or NULL. In the former case, the value
4481 returned is the next FIELD_DECL (possibly FIELD itself) that can be
4482 initialized. If there are no more such fields, the return value
4483 will be NULL. */
4484
4485static tree
4486next_initializable_field (tree field)
4487{
4488 while (field
4489 && (TREE_CODE (field) != FIELD_DECL
4490 || (DECL_C_BIT_FIELD (field) && !DECL_NAME (field))
4491 || DECL_ARTIFICIAL (field)))
4492 field = TREE_CHAIN (field);
4493
4494 return field;
4495}
4496
4038c495
GB
4497/* Subroutine of reshape_init_array and reshape_init_vector, which does
4498 the actual work. ELT_TYPE is the element type of the array. MAX_INDEX is an
4499 INTEGER_CST representing the size of the array minus one (the maximum index),
4500 or NULL_TREE if the array was declared without specifying the size. D is
4501 the iterator within the constructor. */
4502
4503static tree
4504reshape_init_array_1 (tree elt_type, tree max_index, reshape_iter *d)
3d142be2 4505{
4038c495 4506 tree new_init;
3d142be2 4507 bool sized_array_p = (max_index != NULL_TREE);
004c400a
JJ
4508 unsigned HOST_WIDE_INT max_index_cst = 0;
4509 unsigned HOST_WIDE_INT index;
3d142be2 4510
4038c495
GB
4511 /* The initializer for an array is always a CONSTRUCTOR. */
4512 new_init = build_constructor (NULL_TREE, NULL);
4513
3d142be2 4514 if (sized_array_p)
004c400a 4515 {
d7d93837
AH
4516 /* Minus 1 is used for zero sized arrays. */
4517 if (integer_all_onesp (max_index))
4518 return new_init;
4519
004c400a
JJ
4520 if (host_integerp (max_index, 1))
4521 max_index_cst = tree_low_cst (max_index, 1);
4522 /* sizetype is sign extended, not zero extended. */
4523 else
4524 max_index_cst = tree_low_cst (fold_convert (size_type_node, max_index),
4525 1);
4526 }
3d142be2
GB
4527
4528 /* Loop until there are no more initializers. */
4529 for (index = 0;
4038c495 4530 d->cur != d->end && (!sized_array_p || index <= max_index_cst);
3d142be2
GB
4531 ++index)
4532 {
4038c495
GB
4533 tree elt_init;
4534
76239779 4535 check_array_designated_initializer (d->cur);
4038c495 4536 elt_init = reshape_init_r (elt_type, d, /*first_initializer_p=*/false);
07471dfb
MM
4537 if (elt_init == error_mark_node)
4538 return error_mark_node;
4038c495 4539 CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_init), NULL_TREE, elt_init);
3d142be2
GB
4540 }
4541
4038c495 4542 return new_init;
3d142be2
GB
4543}
4544
4038c495
GB
4545/* Subroutine of reshape_init_r, processes the initializers for arrays.
4546 Parameters are the same of reshape_init_r. */
8e3df2de 4547
4038c495
GB
4548static tree
4549reshape_init_array (tree type, reshape_iter *d)
4550{
4551 tree max_index = NULL_TREE;
caf93cb0 4552
4038c495 4553 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
8e3df2de 4554
4038c495
GB
4555 if (TYPE_DOMAIN (type))
4556 max_index = array_type_nelts (type);
4557
4558 return reshape_init_array_1 (TREE_TYPE (type), max_index, d);
4559}
4560
4561/* Subroutine of reshape_init_r, processes the initializers for vectors.
4562 Parameters are the same of reshape_init_r. */
8e3df2de
MM
4563
4564static tree
4038c495 4565reshape_init_vector (tree type, reshape_iter *d)
8e3df2de 4566{
4038c495
GB
4567 tree max_index = NULL_TREE;
4568 tree rtype;
4569
4570 gcc_assert (TREE_CODE (type) == VECTOR_TYPE);
4571
3b2db49f 4572 if (COMPOUND_LITERAL_P (d->cur->value))
4038c495
GB
4573 {
4574 tree value = d->cur->value;
4575 if (!same_type_p (TREE_TYPE (value), type))
4576 {
4577 error ("invalid type %qT as initializer for a vector of type %qT",
4578 TREE_TYPE (d->cur->value), type);
3db45ab5 4579 value = error_mark_node;
4038c495
GB
4580 }
4581 ++d->cur;
4582 return value;
4583 }
4584
4585 /* For a vector, the representation type is a struct
4586 containing a single member which is an array of the
4587 appropriate size. */
4588 rtype = TYPE_DEBUG_REPRESENTATION_TYPE (type);
4589 if (rtype && TYPE_DOMAIN (TREE_TYPE (TYPE_FIELDS (rtype))))
4590 max_index = array_type_nelts (TREE_TYPE (TYPE_FIELDS (rtype)));
4591
4592 return reshape_init_array_1 (TREE_TYPE (type), max_index, d);
4593}
4594
4595/* Subroutine of reshape_init_r, processes the initializers for classes
4596 or union. Parameters are the same of reshape_init_r. */
4597
4598static tree
4599reshape_init_class (tree type, reshape_iter *d, bool first_initializer_p)
4600{
4601 tree field;
8e3df2de 4602 tree new_init;
8e3df2de 4603
4038c495 4604 gcc_assert (CLASS_TYPE_P (type));
8e3df2de 4605
4038c495
GB
4606 /* The initializer for a class is always a CONSTRUCTOR. */
4607 new_init = build_constructor (NULL_TREE, NULL);
4608 field = next_initializable_field (TYPE_FIELDS (type));
ee7ecb29 4609
4038c495 4610 if (!field)
8e3df2de 4611 {
4038c495
GB
4612 /* [dcl.init.aggr]
4613
4614 An initializer for an aggregate member that is an
4615 empty class shall have the form of an empty
4616 initializer-list {}. */
4617 if (!first_initializer_p)
4618 {
4619 error ("initializer for %qT must be brace-enclosed", type);
4620 return error_mark_node;
4621 }
4622 return new_init;
8e3df2de 4623 }
4038c495
GB
4624
4625 /* Loop through the initializable fields, gathering initializers. */
4626 while (d->cur != d->end)
8e3df2de 4627 {
4038c495
GB
4628 tree field_init;
4629
4630 /* Handle designated initializers, as an extension. */
4631 if (d->cur->index)
4632 {
4038c495
GB
4633 field = lookup_field_1 (type, d->cur->index, /*want_type=*/false);
4634
4635 if (!field || TREE_CODE (field) != FIELD_DECL)
77bffd4c
SE
4636 {
4637 error ("%qT has no non-static data member named %qD", type,
4638 d->cur->index);
4639 return error_mark_node;
4640 }
4038c495
GB
4641 }
4642
4643 /* If we processed all the member of the class, we are done. */
4644 if (!field)
4645 break;
4646
4647 field_init = reshape_init_r (TREE_TYPE (field), d,
4648 /*first_initializer_p=*/false);
4649 CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_init), field, field_init);
4650
4651 /* [dcl.init.aggr]
4652
4653 When a union is initialized with a brace-enclosed
4654 initializer, the braces shall only contain an
4655 initializer for the first member of the union. */
4656 if (TREE_CODE (type) == UNION_TYPE)
4657 break;
4658
4659 field = next_initializable_field (TREE_CHAIN (field));
8e3df2de
MM
4660 }
4661
4038c495
GB
4662 return new_init;
4663}
4664
4665/* Subroutine of reshape_init, which processes a single initializer (part of
4666 a CONSTRUCTOR). TYPE is the type of the variable being initialized, D is the
4667 iterator within the CONSTRUCTOR which points to the initializer to process.
4668 FIRST_INITIALIZER_P is true if this is the first initializer of the
4669 CONSTRUCTOR node. */
4670
4671static tree
4672reshape_init_r (tree type, reshape_iter *d, bool first_initializer_p)
4673{
4674 tree init = d->cur->value;
4675
8e3df2de
MM
4676 /* A non-aggregate type is always initialized with a single
4677 initializer. */
4678 if (!CP_AGGREGATE_TYPE_P (type))
4038c495
GB
4679 {
4680 /* It is invalid to initialize a non-aggregate type with a
4681 brace-enclosed initializer.
4682 We need to check for BRACE_ENCLOSED_INITIALIZER_P here because
4683 of g++.old-deja/g++.mike/p7626.C: a pointer-to-member constant is
4684 a CONSTRUCTOR (with a record type). */
4685 if (TREE_CODE (init) == CONSTRUCTOR
4686 && BRACE_ENCLOSED_INITIALIZER_P (init)) /* p7626.C */
4687 {
4688 error ("braces around scalar initializer for type %qT", type);
4689 init = error_mark_node;
4690 }
3db45ab5 4691
4038c495
GB
4692 d->cur++;
4693 return init;
4694 }
8e3df2de
MM
4695
4696 /* [dcl.init.aggr]
4697
4698 All implicit type conversions (clause _conv_) are considered when
4699 initializing the aggregate member with an initializer from an
4700 initializer-list. If the initializer can initialize a member,
4701 the member is initialized. Otherwise, if the member is itself a
4702 non-empty subaggregate, brace elision is assumed and the
4703 initializer is considered for the initialization of the first
4704 member of the subaggregate. */
4038c495 4705 if (TREE_CODE (init) != CONSTRUCTOR
30f86ec3 4706 && can_convert_arg (type, TREE_TYPE (init), init, LOOKUP_NORMAL))
8e3df2de 4707 {
4038c495
GB
4708 d->cur++;
4709 return init;
8e3df2de
MM
4710 }
4711
4038c495
GB
4712 /* [dcl.init.string]
4713
4714 A char array (whether plain char, signed char, or unsigned char)
4715 can be initialized by a string-literal (optionally enclosed in
4716 braces); a wchar_t array can be initialized by a wide
4717 string-literal (optionally enclosed in braces). */
4718 if (TREE_CODE (type) == ARRAY_TYPE
42328048 4719 && char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type))))
8e3df2de 4720 {
4038c495 4721 tree str_init = init;
8e3df2de 4722
4038c495 4723 /* Strip one level of braces if and only if they enclose a single
3db45ab5 4724 element (as allowed by [dcl.init.string]). */
4038c495
GB
4725 if (!first_initializer_p
4726 && TREE_CODE (str_init) == CONSTRUCTOR
4727 && VEC_length (constructor_elt, CONSTRUCTOR_ELTS (str_init)) == 1)
8e3df2de 4728 {
4038c495
GB
4729 str_init = VEC_index (constructor_elt,
4730 CONSTRUCTOR_ELTS (str_init), 0)->value;
4731 }
3db45ab5 4732
4038c495 4733 /* If it's a string literal, then it's the initializer for the array
3db45ab5 4734 as a whole. Otherwise, continue with normal initialization for
4038c495
GB
4735 array types (one value per array element). */
4736 if (TREE_CODE (str_init) == STRING_CST)
4737 {
4738 d->cur++;
4739 return str_init;
4740 }
4741 }
8e3df2de 4742
4038c495
GB
4743 /* The following cases are about aggregates. If we are not within a full
4744 initializer already, and there is not a CONSTRUCTOR, it means that there
4745 is a missing set of braces (that is, we are processing the case for
4746 which reshape_init exists). */
4747 if (!first_initializer_p)
4748 {
4749 if (TREE_CODE (init) == CONSTRUCTOR)
4750 {
70ec16f7
AP
4751 if (TREE_TYPE (init) && TYPE_PTRMEMFUNC_P (TREE_TYPE (init)))
4752 /* There is no need to reshape pointer-to-member function
4753 initializers, as they are always constructed correctly
4754 by the front end. */
4755 ;
4756 else if (COMPOUND_LITERAL_P (init))
4038c495
GB
4757 /* For a nested compound literal, there is no need to reshape since
4758 brace elision is not allowed. Even if we decided to allow it,
4759 we should add a call to reshape_init in finish_compound_literal,
4760 before calling digest_init, so changing this code would still
4761 not be necessary. */
70ec16f7
AP
4762 gcc_assert (!BRACE_ENCLOSED_INITIALIZER_P (init));
4763 else
8e3df2de 4764 {
4038c495
GB
4765 ++d->cur;
4766 gcc_assert (BRACE_ENCLOSED_INITIALIZER_P (init));
4767 return reshape_init (type, init);
8e3df2de 4768 }
8e3df2de 4769 }
f8e7a389 4770
4038c495
GB
4771 warning (OPT_Wmissing_braces, "missing braces around initializer for %qT",
4772 type);
4773 }
8e3df2de 4774
4038c495
GB
4775 /* Dispatch to specialized routines. */
4776 if (CLASS_TYPE_P (type))
4777 return reshape_init_class (type, d, first_initializer_p);
4778 else if (TREE_CODE (type) == ARRAY_TYPE)
4779 return reshape_init_array (type, d);
4780 else if (TREE_CODE (type) == VECTOR_TYPE)
4781 return reshape_init_vector (type, d);
4782 else
4783 gcc_unreachable();
4784}
8e3df2de 4785
4038c495
GB
4786/* Undo the brace-elision allowed by [dcl.init.aggr] in a
4787 brace-enclosed aggregate initializer.
8e3df2de 4788
4038c495
GB
4789 INIT is the CONSTRUCTOR containing the list of initializers describing
4790 a brace-enclosed initializer for an entity of the indicated aggregate TYPE.
4791 It may not presently match the shape of the TYPE; for example:
4792
4793 struct S { int a; int b; };
4794 struct S a[] = { 1, 2, 3, 4 };
4795
4796 Here INIT will hold a VEC of four elements, rather than a
4797 VEC of two elements, each itself a VEC of two elements. This
4798 routine transforms INIT from the former form into the latter. The
4799 revised CONSTRUCTOR node is returned. */
4800
3b2db49f 4801tree
4038c495
GB
4802reshape_init (tree type, tree init)
4803{
4804 VEC(constructor_elt, gc) *v;
4805 reshape_iter d;
4806 tree new_init;
4807
4038c495
GB
4808 gcc_assert (BRACE_ENCLOSED_INITIALIZER_P (init));
4809
4810 v = CONSTRUCTOR_ELTS (init);
4811
4812 /* An empty constructor does not need reshaping, and it is always a valid
4813 initializer. */
4814 if (VEC_empty (constructor_elt, v))
4815 return init;
4816
4817 /* Recurse on this CONSTRUCTOR. */
4818 d.cur = VEC_index (constructor_elt, v, 0);
4819 d.end = d.cur + VEC_length (constructor_elt, v);
4820
4821 new_init = reshape_init_r (type, &d, true);
07471dfb
MM
4822 if (new_init == error_mark_node)
4823 return error_mark_node;
4038c495
GB
4824
4825 /* Make sure all the element of the constructor were used. Otherwise,
4826 issue an error about exceeding initializers. */
4827 if (d.cur != d.end)
4828 error ("too many initializers for %qT", type);
8e3df2de
MM
4829
4830 return new_init;
4831}
4832
c82dbd95 4833/* Verify INIT (the initializer for DECL), and record the
7e99327d
MM
4834 initialization in DECL_INITIAL, if appropriate. CLEANUP is as for
4835 grok_reference_init.
8e3df2de
MM
4836
4837 If the return value is non-NULL, it is an expression that must be
4838 evaluated dynamically to initialize DECL. */
27778b73 4839
c82dbd95 4840static tree
7e99327d 4841check_initializer (tree decl, tree init, int flags, tree *cleanup)
27778b73 4842{
17bbb839 4843 tree type = TREE_TYPE (decl);
25ebb82a 4844 tree init_code = NULL;
fc0e7bf5 4845
328de7c2
MM
4846 /* Things that are going to be initialized need to have complete
4847 type. */
4848 TREE_TYPE (decl) = type = complete_type (TREE_TYPE (decl));
c82dbd95 4849
328de7c2
MM
4850 if (type == error_mark_node)
4851 /* We will have already complained. */
f724eac4
MM
4852 return NULL_TREE;
4853
4854 if (TREE_CODE (type) == ARRAY_TYPE)
328de7c2 4855 {
f724eac4
MM
4856 tree element_type = TREE_TYPE (type);
4857
4858 /* The array type itself need not be complete, because the
4859 initializer may tell us how many elements are in the array.
4860 But, the elements of the array must be complete. */
4861 if (!COMPLETE_TYPE_P (complete_type (element_type)))
4862 {
4863 error ("elements of array %q#D have incomplete type", decl);
4864 return NULL_TREE;
4865 }
b9704fc5 4866 /* It is not valid to initialize a VLA. */
f724eac4
MM
4867 if (init
4868 && ((COMPLETE_TYPE_P (type) && !TREE_CONSTANT (TYPE_SIZE (type)))
4869 || !TREE_CONSTANT (TYPE_SIZE (element_type))))
4870 {
4871 error ("variable-sized object %qD may not be initialized", decl);
4872 return NULL_TREE;
4873 }
328de7c2 4874 }
f724eac4 4875 else if (!COMPLETE_TYPE_P (type))
328de7c2 4876 {
2d01edd7 4877 error ("%qD has incomplete type", decl);
328de7c2 4878 TREE_TYPE (decl) = error_mark_node;
f724eac4 4879 return NULL_TREE;
27778b73 4880 }
f724eac4
MM
4881 else
4882 /* There is no way to make a variable-sized class type in GNU C++. */
4883 gcc_assert (TREE_CONSTANT (TYPE_SIZE (type)));
c1ae8be5
SM
4884
4885 if (init && BRACE_ENCLOSED_INITIALIZER_P (init))
f47165c9 4886 {
c1ae8be5
SM
4887 int init_len = VEC_length (constructor_elt, CONSTRUCTOR_ELTS (init));
4888 if (SCALAR_TYPE_P (type))
4889 {
4890 if (init_len != 1)
4891 {
4892 error ("scalar object %qD requires one element in initializer",
4893 decl);
4894 TREE_TYPE (decl) = error_mark_node;
4895 return NULL_TREE;
4896 }
4897 }
4898 else if ((cxx_dialect == cxx98) && !CP_AGGREGATE_TYPE_P (type))
4899 {
4900 /* A non-aggregate that is not a scalar cannot be initialized
4901 via an initializer-list in C++98. */
4902 error ("braces around initializer for non-aggregate type %qT",
4903 type);
4904 TREE_TYPE (decl) = error_mark_node;
4905 return NULL_TREE;
4906 }
f47165c9 4907 }
27778b73
MM
4908
4909 if (TREE_CODE (decl) == CONST_DECL)
4910 {
7525db03 4911 gcc_assert (TREE_CODE (type) != REFERENCE_TYPE);
27778b73
MM
4912
4913 DECL_INITIAL (decl) = init;
4914
50bc768d 4915 gcc_assert (init != NULL_TREE);
27778b73
MM
4916 init = NULL_TREE;
4917 }
c82dbd95 4918 else if (!DECL_EXTERNAL (decl) && TREE_CODE (type) == REFERENCE_TYPE)
7e99327d 4919 init = grok_reference_init (decl, type, init, cleanup);
27778b73
MM
4920 else if (init)
4921 {
4038c495 4922 /* Do not reshape constructors of vectors (they don't need to be
3db45ab5 4923 reshaped. */
caf93cb0 4924 if (TREE_CODE (init) == CONSTRUCTOR
3b2db49f 4925 && !COMPOUND_LITERAL_P (init)
4038c495 4926 && !TREE_TYPE (init)) /* ptrmemfunc */
a16f2357 4927 {
4038c495 4928 init = reshape_init (type, init);
6e1b3a7c
AH
4929
4930 if ((*targetm.vector_opaque_p) (type))
4931 {
4932 error ("opaque vector types cannot be initialized");
4933 init = error_mark_node;
4934 }
a16f2357 4935 }
8e3df2de
MM
4936
4937 /* If DECL has an array type without a specific bound, deduce the
4938 array size from the initializer. */
4939 maybe_deduce_size_from_array_init (decl, init);
4940 type = TREE_TYPE (decl);
2b643eda
MM
4941 if (type == error_mark_node)
4942 return NULL_TREE;
8e3df2de 4943
27778b73
MM
4944 if (TYPE_HAS_CONSTRUCTOR (type) || TYPE_NEEDS_CONSTRUCTING (type))
4945 {
4946 if (TREE_CODE (type) == ARRAY_TYPE)
8e3df2de 4947 goto initialize_aggr;
4038c495 4948 else if (TREE_CODE (init) == CONSTRUCTOR)
27778b73
MM
4949 {
4950 if (TYPE_NON_AGGREGATE_CLASS (type))
4951 {
2d01edd7 4952 error ("%qD must be initialized by constructor, "
0cbd7506 4953 "not by %<{...}%>",
8e3df2de 4954 decl);
27778b73
MM
4955 init = error_mark_node;
4956 }
4957 else
4958 goto dont_use_constructor;
4959 }
8e3df2de
MM
4960 else
4961 {
4962 int saved_stmts_are_full_exprs_p;
4963
4964 initialize_aggr:
4965 saved_stmts_are_full_exprs_p = 0;
4966 if (building_stmt_tree ())
4967 {
4968 saved_stmts_are_full_exprs_p = stmts_are_full_exprs_p ();
4969 current_stmt_tree ()->stmts_are_full_exprs_p = 1;
4970 }
4971 init = build_aggr_init (decl, init, flags);
4972 if (building_stmt_tree ())
4973 current_stmt_tree ()->stmts_are_full_exprs_p =
4974 saved_stmts_are_full_exprs_p;
4975 return init;
4976 }
27778b73
MM
4977 }
4978 else
4979 {
4980 dont_use_constructor:
4981 if (TREE_CODE (init) != TREE_VEC)
25ebb82a
RH
4982 {
4983 init_code = store_init_value (decl, init);
7a8380ae
NS
4984 if (pedantic && TREE_CODE (type) == ARRAY_TYPE
4985 && DECL_INITIAL (decl)
4986 && TREE_CODE (DECL_INITIAL (decl)) == STRING_CST
4987 && PAREN_STRING_LITERAL_P (DECL_INITIAL (decl)))
d4ee4d25 4988 warning (0, "array %qD initialized by parenthesized string literal %qE",
7a8380ae 4989 decl, DECL_INITIAL (decl));
25ebb82a
RH
4990 init = NULL;
4991 }
27778b73 4992 }
27778b73
MM
4993 }
4994 else if (DECL_EXTERNAL (decl))
4995 ;
8e3df2de
MM
4996 else if (TYPE_P (type) && TYPE_NEEDS_CONSTRUCTING (type))
4997 goto initialize_aggr;
4998 else if (IS_AGGR_TYPE (type))
27778b73
MM
4999 {
5000 tree core_type = strip_array_types (type);
5001
8e3df2de 5002 if (CLASSTYPE_READONLY_FIELDS_NEED_INIT (core_type))
2d01edd7 5003 error ("structure %qD with uninitialized const members", decl);
8e3df2de 5004 if (CLASSTYPE_REF_FIELDS_NEED_INIT (core_type))
2d01edd7 5005 error ("structure %qD with uninitialized reference members", decl);
27778b73
MM
5006
5007 check_for_uninitialized_const_var (decl);
27778b73
MM
5008 }
5009 else
5010 check_for_uninitialized_const_var (decl);
68642fb6 5011
8e3df2de 5012 if (init && init != error_mark_node)
f293ce4b 5013 init_code = build2 (INIT_EXPR, type, decl, init);
8e3df2de 5014
25ebb82a 5015 return init_code;
27778b73
MM
5016}
5017
5018/* If DECL is not a local variable, give it RTL. */
5019
5020static void
11f6b451 5021make_rtl_for_nonlocal_decl (tree decl, tree init, const char* asmspec)
27778b73 5022{
95ee998c
MM
5023 int toplev = toplevel_bindings_p ();
5024 int defer_p;
9f12b095 5025 const char *filename;
27778b73 5026
0e6df31e
GK
5027 /* Set the DECL_ASSEMBLER_NAME for the object. */
5028 if (asmspec)
5029 {
5030 /* The `register' keyword, when used together with an
5031 asm-specification, indicates that the variable should be
5032 placed in a particular register. */
5033 if (TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
5034 {
11e3f4b6 5035 set_user_assembler_name (decl, asmspec);
0e6df31e
GK
5036 DECL_HARD_REGISTER (decl) = 1;
5037 }
5038 else
b482789c
MA
5039 {
5040 if (TREE_CODE (decl) == FUNCTION_DECL
5041 && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
5042 set_builtin_user_assembler_name (decl, asmspec);
5043 set_user_assembler_name (decl, asmspec);
5044 }
0e6df31e
GK
5045 }
5046
f39ee884
MM
5047 /* Handle non-variables up front. */
5048 if (TREE_CODE (decl) != VAR_DECL)
5049 {
0e6df31e 5050 rest_of_decl_compilation (decl, toplev, at_eof);
f39ee884
MM
5051 return;
5052 }
5053
95ee998c
MM
5054 /* If we see a class member here, it should be a static data
5055 member. */
5056 if (DECL_LANG_SPECIFIC (decl) && DECL_IN_AGGR_P (decl))
5057 {
50bc768d 5058 gcc_assert (TREE_STATIC (decl));
95ee998c
MM
5059 /* An in-class declaration of a static data member should be
5060 external; it is only a declaration, and not a definition. */
5061 if (init == NULL_TREE)
50bc768d 5062 gcc_assert (DECL_EXTERNAL (decl));
95ee998c
MM
5063 }
5064
95ee998c
MM
5065 /* We don't create any RTL for local variables. */
5066 if (DECL_FUNCTION_SCOPE_P (decl) && !TREE_STATIC (decl))
5067 return;
27778b73 5068
95ee998c 5069 /* We defer emission of local statics until the corresponding
350fae66 5070 DECL_EXPR is expanded. */
95ee998c
MM
5071 defer_p = DECL_FUNCTION_SCOPE_P (decl) || DECL_VIRTUAL_P (decl);
5072
b602511f
JM
5073 /* We try to defer namespace-scope static constants so that they are
5074 not emitted into the object file unnecessarily. */
9f12b095 5075 filename = input_filename;
b602511f
JM
5076 if (!DECL_VIRTUAL_P (decl)
5077 && TREE_READONLY (decl)
5078 && DECL_INITIAL (decl) != NULL_TREE
5079 && DECL_INITIAL (decl) != error_mark_node
9f12b095 5080 && filename != NULL
b602511f
JM
5081 && ! EMPTY_CONSTRUCTOR_P (DECL_INITIAL (decl))
5082 && toplev
5083 && !TREE_PUBLIC (decl))
5cc90635
JM
5084 {
5085 /* Fool with the linkage of static consts according to #pragma
5086 interface. */
c533e34d 5087 struct c_fileinfo *finfo = get_fileinfo (filename);
5d709b00 5088 if (!finfo->interface_unknown && !TREE_PUBLIC (decl))
27778b73 5089 {
95ee998c 5090 TREE_PUBLIC (decl) = 1;
5d709b00 5091 DECL_EXTERNAL (decl) = finfo->interface_only;
27778b73 5092 }
27778b73 5093
95ee998c 5094 defer_p = 1;
27778b73 5095 }
b602511f 5096 /* Likewise for template instantiations. */
4684cd27
MM
5097 else if (DECL_LANG_SPECIFIC (decl)
5098 && DECL_IMPLICIT_INSTANTIATION (decl))
b602511f 5099 defer_p = 1;
95ee998c 5100
95ee998c 5101 /* If we're not deferring, go ahead and assemble the variable. */
0e6df31e
GK
5102 if (!defer_p)
5103 rest_of_decl_compilation (decl, toplev, at_eof);
27778b73
MM
5104}
5105
ed5511d9 5106/* Generate code to initialize DECL (a local variable). */
27778b73 5107
8e3df2de 5108static void
11f6b451 5109initialize_local_var (tree decl, tree init)
27778b73 5110{
9ed9e79a 5111 tree type = TREE_TYPE (decl);
170b020f 5112 tree cleanup;
27778b73 5113
50bc768d
NS
5114 gcc_assert (TREE_CODE (decl) == VAR_DECL
5115 || TREE_CODE (decl) == RESULT_DECL);
5116 gcc_assert (!TREE_STATIC (decl));
b7b8bcd2 5117
8e3df2de 5118 if (DECL_SIZE (decl) == NULL_TREE)
b7b8bcd2
MM
5119 {
5120 /* If we used it already as memory, it must stay in memory. */
5121 DECL_INITIAL (decl) = NULL_TREE;
5122 TREE_ADDRESSABLE (decl) = TREE_USED (decl);
5123 }
5124
27778b73
MM
5125 if (DECL_SIZE (decl) && type != error_mark_node)
5126 {
5127 int already_used;
68642fb6 5128
27778b73 5129 /* Compute and store the initial value. */
27778b73
MM
5130 already_used = TREE_USED (decl) || TREE_USED (type);
5131
8e3df2de
MM
5132 /* Perform the initialization. */
5133 if (init)
27778b73 5134 {
24bef158
MM
5135 int saved_stmts_are_full_exprs_p;
5136
50bc768d 5137 gcc_assert (building_stmt_tree ());
f2c5f623 5138 saved_stmts_are_full_exprs_p = stmts_are_full_exprs_p ();
ae499cce 5139 current_stmt_tree ()->stmts_are_full_exprs_p = 1;
8e3df2de 5140 finish_expr_stmt (init);
5362b086 5141 current_stmt_tree ()->stmts_are_full_exprs_p =
ae499cce 5142 saved_stmts_are_full_exprs_p;
27778b73
MM
5143 }
5144
5145 /* Set this to 0 so we can tell whether an aggregate which was
5146 initialized was ever used. Don't do this if it has a
5147 destructor, so we don't complain about the 'resource
b7b8bcd2
MM
5148 allocation is initialization' idiom. Now set
5149 attribute((unused)) on types so decls of that type will be
5150 marked used. (see TREE_USED, above.) */
27778b73
MM
5151 if (TYPE_NEEDS_CONSTRUCTING (type)
5152 && ! already_used
834c6dff 5153 && TYPE_HAS_TRIVIAL_DESTRUCTOR (type)
27778b73
MM
5154 && DECL_NAME (decl))
5155 TREE_USED (decl) = 0;
b7b8bcd2 5156 else if (already_used)
27778b73
MM
5157 TREE_USED (decl) = 1;
5158 }
24bef158 5159
8e3df2de 5160 /* Generate a cleanup, if necessary. */
170b020f
MM
5161 cleanup = cxx_maybe_build_cleanup (decl);
5162 if (DECL_SIZE (decl) && cleanup)
5163 finish_decl_cleanup (decl, cleanup);
24bef158
MM
5164}
5165
19c29b2f
MM
5166/* DECL is a VAR_DECL for a compiler-generated variable with static
5167 storage duration (like a virtual table) whose initializer is a
3b2db49f
MM
5168 compile-time constant. INIT must be either a TREE_LIST of values,
5169 or a CONSTRUCTOR. Initialize the variable and provide it to the
5170 back end. */
19c29b2f
MM
5171
5172void
5173initialize_artificial_var (tree decl, tree init)
5174{
d23f3d60 5175 gcc_assert (DECL_ARTIFICIAL (decl));
3b2db49f
MM
5176 if (TREE_CODE (init) == TREE_LIST)
5177 init = build_constructor_from_list (NULL_TREE, init);
5178 gcc_assert (TREE_CODE (init) == CONSTRUCTOR);
5179 DECL_INITIAL (decl) = init;
19c29b2f
MM
5180 DECL_INITIALIZED_P (decl) = 1;
5181 determine_visibility (decl);
5182 layout_var_decl (decl);
5183 maybe_commonize_var (decl);
5184 make_rtl_for_nonlocal_decl (decl, init, /*asmspec=*/NULL);
5185}
5186
32a11c08
MM
5187/* INIT is the initializer for a variable, as represented by the
5188 parser. Returns true iff INIT is value-dependent. */
5189
5190static bool
5191value_dependent_init_p (tree init)
5192{
5193 if (TREE_CODE (init) == TREE_LIST)
5194 /* A parenthesized initializer, e.g.: int i (3, 2); ? */
5195 return any_value_dependent_elements_p (init);
5196 else if (TREE_CODE (init) == CONSTRUCTOR)
5197 /* A brace-enclosed initializer, e.g.: int i = { 3 }; ? */
5198 {
5199 VEC(constructor_elt, gc) *elts;
5200 size_t nelts;
5201 size_t i;
5202
5203 elts = CONSTRUCTOR_ELTS (init);
5204 nelts = VEC_length (constructor_elt, elts);
5205 for (i = 0; i < nelts; ++i)
5206 if (value_dependent_init_p (VEC_index (constructor_elt,
5207 elts, i)->value))
5208 return true;
5209 }
5210 else
5211 /* It must be a simple expression, e.g., int i = 3; */
5212 return value_dependent_expression_p (init);
5213
5214 return false;
5215}
5216
8d08fdba
MS
5217/* Finish processing of a declaration;
5218 install its line number and initial value.
5219 If the length of an array type is not known before,
5220 it must be determined now, from the initial value, or it is an error.
5221
d174af6c
MM
5222 INIT is the initializer (if any) for DECL. If INIT_CONST_EXPR_P is
5223 true, then INIT is an integral constant expression.
8d08fdba 5224
920f9474 5225 FLAGS is LOOKUP_ONLYCONVERTING if the = init syntax was used, else 0
f78c7bc6 5226 if the (init) syntax was used. */
8d08fdba
MS
5227
5228void
3db45ab5 5229cp_finish_decl (tree decl, tree init, bool init_const_expr_p,
d174af6c 5230 tree asmspec_tree, int flags)
8d08fdba 5231{
7e99327d 5232 tree type;
7e99327d 5233 tree cleanup;
9c0758dd 5234 const char *asmspec = NULL;
8d08fdba 5235 int was_readonly = 0;
745d26d9 5236 bool var_definition_p = false;
2b643eda 5237 int saved_processing_template_decl;
8d08fdba 5238
11325dcd
KL
5239 if (decl == error_mark_node)
5240 return;
5241 else if (! decl)
8d08fdba
MS
5242 {
5243 if (init)
8251199e 5244 error ("assignment (not initialization) in declaration");
8d08fdba
MS
5245 return;
5246 }
5247
50bc768d 5248 gcc_assert (TREE_CODE (decl) != RESULT_DECL);
2b643eda
MM
5249 /* Parameters are handled by store_parm_decls, not cp_finish_decl. */
5250 gcc_assert (TREE_CODE (decl) != PARM_DECL);
5251
5252 type = TREE_TYPE (decl);
5253 if (type == error_mark_node)
5254 return;
170b020f 5255
7e99327d
MM
5256 /* Assume no cleanup is required. */
5257 cleanup = NULL_TREE;
2b643eda 5258 saved_processing_template_decl = processing_template_decl;
7e99327d 5259
a4443a08 5260 /* If a name was specified, get the string. */
5f52c0e0 5261 if (global_scope_p (current_binding_level))
41c64394 5262 asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
4f543d15 5263 if (asmspec_tree && asmspec_tree != error_mark_node)
5cc90635 5264 asmspec = TREE_STRING_POINTER (asmspec_tree);
8d08fdba 5265
6ba89f8e 5266 if (current_class_type
4f1c5b7d 5267 && CP_DECL_CONTEXT (decl) == current_class_type
6ba89f8e
MM
5268 && TYPE_BEING_DEFINED (current_class_type)
5269 && (DECL_INITIAL (decl) || init))
3febd123 5270 DECL_INITIALIZED_IN_CLASS_P (decl) = 1;
6ba89f8e 5271
5156628f 5272 if (processing_template_decl)
5566b478 5273 {
2b643eda
MM
5274 bool type_dependent_p;
5275
08ac397c 5276 /* Add this declaration to the statement-tree. */
170b020f 5277 if (at_function_scope_p ())
350fae66 5278 add_decl_expr (decl);
08ac397c 5279
2b643eda
MM
5280 type_dependent_p = dependent_type_p (type);
5281
5282 if (init && init_const_expr_p)
d174af6c 5283 {
2b643eda
MM
5284 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1;
5285 if (DECL_INTEGRAL_CONSTANT_VAR_P (decl))
5286 TREE_CONSTANT (decl) = 1;
d174af6c
MM
5287 }
5288
32a11c08
MM
5289 /* Generally, initializers in templates are expanded when the
5290 template is instantiated. But, if DECL is an integral
5291 constant static data member, then it can be used in future
5292 integral constant expressions, and its value must be
5293 available. */
5294 if (!(init
5295 && DECL_CLASS_SCOPE_P (decl)
5296 && DECL_INTEGRAL_CONSTANT_VAR_P (decl)
5297 && !type_dependent_p
5298 && !value_dependent_init_p (init)))
2b643eda
MM
5299 {
5300 if (init)
5301 DECL_INITIAL (decl) = init;
5302 if (TREE_CODE (decl) == VAR_DECL
5303 && !DECL_PRETTY_FUNCTION_P (decl)
5304 && !type_dependent_p)
5305 maybe_deduce_size_from_array_init (decl, init);
5306 goto finish_end;
5307 }
c8094d83 5308
2b643eda
MM
5309 init = fold_non_dependent_expr (init);
5310 processing_template_decl = 0;
5566b478 5311 }
3e41d13b 5312
8d08fdba
MS
5313 /* Take care of TYPE_DECLs up front. */
5314 if (TREE_CODE (decl) == TYPE_DECL)
5315 {
a0a33927
MS
5316 if (type != error_mark_node
5317 && IS_AGGR_TYPE (type) && DECL_NAME (decl))
8d08fdba
MS
5318 {
5319 if (TREE_TYPE (DECL_NAME (decl)) && TREE_TYPE (decl) != type)
d4ee4d25 5320 warning (0, "shadowing previous type declaration of %q#D", decl);
4b0d3cbe 5321 set_identifier_type_value (DECL_NAME (decl), decl);
8d08fdba 5322 }
cffa8729
MS
5323
5324 /* If we have installed this as the canonical typedef for this
5325 type, and that type has not been defined yet, delay emitting
956d6950 5326 the debug information for it, as we will emit it later. */
d2e5ee5c 5327 if (TYPE_MAIN_DECL (TREE_TYPE (decl)) == decl
d0f062fb 5328 && !COMPLETE_TYPE_P (TREE_TYPE (decl)))
cffa8729
MS
5329 TYPE_DECL_SUPPRESS_DEBUG (decl) = 1;
5330
0e6df31e
GK
5331 rest_of_decl_compilation (decl, DECL_CONTEXT (decl) == NULL_TREE,
5332 at_eof);
8d08fdba
MS
5333 goto finish_end;
5334 }
3e41d13b 5335
67935995 5336 /* A reference will be modified here, as it is initialized. */
c8094d83 5337 if (! DECL_EXTERNAL (decl)
67935995
MM
5338 && TREE_READONLY (decl)
5339 && TREE_CODE (type) == REFERENCE_TYPE)
8d08fdba 5340 {
8d08fdba 5341 was_readonly = 1;
67935995 5342 TREE_READONLY (decl) = 0;
8d08fdba
MS
5343 }
5344
437081d7 5345 if (TREE_CODE (decl) == VAR_DECL)
17bbb839
MM
5346 {
5347 /* Only PODs can have thread-local storage. Other types may require
5348 various kinds of non-trivial initialization. */
c2f7fa15 5349 if (DECL_THREAD_LOCAL_P (decl) && !pod_type_p (TREE_TYPE (decl)))
2d01edd7 5350 error ("%qD cannot be thread-local because it has non-POD type %qT",
17bbb839 5351 decl, TREE_TYPE (decl));
d23f3d60
MM
5352 /* If this is a local variable that will need a mangled name,
5353 register it now. We must do this before processing the
5354 initializer for the variable, since the initialization might
5355 require a guard variable, and since the mangled name of the
5356 guard variable will depend on the mangled name of this
5357 variable. */
5358 if (!processing_template_decl
5359 && DECL_FUNCTION_SCOPE_P (decl)
5360 && TREE_STATIC (decl)
5361 && !DECL_ARTIFICIAL (decl))
5362 push_local_name (decl);
17bbb839
MM
5363 /* Convert the initializer to the type of DECL, if we have not
5364 already initialized DECL. */
5365 if (!DECL_INITIALIZED_P (decl)
5366 /* If !DECL_EXTERNAL then DECL is being defined. In the
8e3df2de 5367 case of a static data member initialized inside the
17bbb839
MM
5368 class-specifier, there can be an initializer even if DECL
5369 is *not* defined. */
5370 && (!DECL_EXTERNAL (decl) || init))
5371 {
6c06fbce 5372 if (init)
d174af6c
MM
5373 {
5374 DECL_NONTRIVIALLY_INITIALIZED_P (decl) = 1;
5375 if (init_const_expr_p)
5376 {
5377 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1;
5378 if (DECL_INTEGRAL_CONSTANT_VAR_P (decl))
5379 TREE_CONSTANT (decl) = 1;
5380 }
5381 }
7e99327d 5382 init = check_initializer (decl, init, flags, &cleanup);
b4c20e52 5383 /* Thread-local storage cannot be dynamically initialized. */
c2f7fa15 5384 if (DECL_THREAD_LOCAL_P (decl) && init)
b4c20e52 5385 {
2d01edd7 5386 error ("%qD is thread-local and so cannot be dynamically "
b4c20e52
MM
5387 "initialized", decl);
5388 init = NULL_TREE;
5389 }
b794e321
MM
5390
5391 /* Check that the initializer for a static data member was a
1634705d 5392 constant. Although we check in the parser that the
b794e321
MM
5393 initializer is an integral constant expression, we do not
5394 simplify division-by-zero at the point at which it
5395 occurs. Therefore, in:
5396
5397 struct S { static const int i = 7 / 0; };
3db45ab5 5398
b794e321
MM
5399 we issue an error at this point. It would
5400 probably be better to forbid division by zero in
5401 integral constant expressions. */
8fe4d24b
NS
5402 if (DECL_EXTERNAL (decl) && init)
5403 {
8fe4d24b
NS
5404 error ("%qD cannot be initialized by a non-constant expression"
5405 " when being declared", decl);
5406 DECL_INITIALIZED_IN_CLASS_P (decl) = 0;
5407 init = NULL_TREE;
5408 }
c8094d83 5409
17bbb839 5410 /* Handle:
caf93cb0 5411
17bbb839 5412 [dcl.init]
caf93cb0 5413
17bbb839
MM
5414 The memory occupied by any object of static storage
5415 duration is zero-initialized at program startup before
5416 any other initialization takes place.
caf93cb0 5417
17bbb839
MM
5418 We cannot create an appropriate initializer until after
5419 the type of DECL is finalized. If DECL_INITIAL is set,
5420 then the DECL is statically initialized, and any
5421 necessary zero-initialization has already been performed. */
5422 if (TREE_STATIC (decl) && !DECL_INITIAL (decl))
5423 DECL_INITIAL (decl) = build_zero_init (TREE_TYPE (decl),
1cb8292f 5424 /*nelts=*/NULL_TREE,
17bbb839
MM
5425 /*static_storage_p=*/true);
5426 /* Remember that the initialization for this variable has
5427 taken place. */
5428 DECL_INITIALIZED_P (decl) = 1;
745d26d9
MM
5429 /* This declaration is the definition of this variable,
5430 unless we are initializing a static data member within
5431 the class specifier. */
5432 if (!DECL_EXTERNAL (decl))
5433 var_definition_p = true;
17bbb839 5434 }
d881ff25
MM
5435 /* If the variable has an array type, lay out the type, even if
5436 there is no initializer. It is valid to index through the
5437 array, and we must get TYPE_ALIGN set correctly on the array
5438 type. */
5439 else if (TREE_CODE (type) == ARRAY_TYPE)
5440 layout_type (type);
17bbb839 5441 }
3e41d13b 5442
57cf57fb 5443 /* Add this declaration to the statement-tree. This needs to happen
350fae66
RK
5444 after the call to check_initializer so that the DECL_EXPR for a
5445 reference temp is added before the DECL_EXPR for the reference itself. */
170b020f 5446 if (at_function_scope_p ())
350fae66 5447 add_decl_expr (decl);
08ac397c 5448
da71e18c
MM
5449 /* Let the middle end know about variables and functions -- but not
5450 static data members in uninstantiated class templates. */
5451 if (!saved_processing_template_decl
5452 && (TREE_CODE (decl) == VAR_DECL
5453 || TREE_CODE (decl) == FUNCTION_DECL))
8d08fdba 5454 {
27778b73 5455 if (TREE_CODE (decl) == VAR_DECL)
da71e18c
MM
5456 {
5457 layout_var_decl (decl);
5458 maybe_commonize_var (decl);
5459 }
8d08fdba 5460
27778b73 5461 make_rtl_for_nonlocal_decl (decl, init, asmspec);
8d08fdba 5462
7fb213d8
GB
5463 /* Check for abstractness of the type. Notice that there is no
5464 need to strip array types here since the check for those types
5465 is already done within create_array_type_for_decl. */
68642fb6 5466 if (TREE_CODE (type) == FUNCTION_TYPE
27778b73 5467 || TREE_CODE (type) == METHOD_TYPE)
7fb213d8 5468 abstract_virtuals_error (decl, TREE_TYPE (type));
68642fb6 5469 else
cfb91b67 5470 abstract_virtuals_error (decl, type);
8d08fdba 5471
b9e75696
JM
5472 /* This needs to happen after the linkage is set. */
5473 determine_visibility (decl);
5474
caf93cb0 5475 if (TREE_CODE (decl) == FUNCTION_DECL
8e3df2de
MM
5476 || TREE_TYPE (decl) == error_mark_node)
5477 /* No initialization required. */
faae18ab 5478 ;
67d743fe
MS
5479 else if (DECL_EXTERNAL (decl)
5480 && ! (DECL_LANG_SPECIFIC (decl)
5481 && DECL_NOT_REALLY_EXTERN (decl)))
5566b478
MS
5482 {
5483 if (init)
5484 DECL_INITIAL (decl) = init;
5485 }
8e3df2de 5486 else
8d08fdba 5487 {
8e3df2de
MM
5488 /* A variable definition. */
5489 if (DECL_FUNCTION_SCOPE_P (decl))
24bef158 5490 {
8e3df2de
MM
5491 /* Initialize the local variable. */
5492 if (processing_template_decl)
e92fb501 5493 DECL_INITIAL (decl) = init;
8e3df2de
MM
5494 else if (!TREE_STATIC (decl))
5495 initialize_local_var (decl, init);
24bef158 5496 }
c8094d83 5497
745d26d9 5498 /* If a variable is defined, and then a subsequent
06b60445 5499 definition with external linkage is encountered, we will
745d26d9
MM
5500 get here twice for the same variable. We want to avoid
5501 calling expand_static_init more than once. For variables
5502 that are not static data members, we can call
5503 expand_static_init only when we actually process the
5504 initializer. It is not legal to redeclare a static data
5505 member, so this issue does not arise in that case. */
5506 if (var_definition_p && TREE_STATIC (decl))
27edb548
JJ
5507 {
5508 /* If a TREE_READONLY variable needs initialization
5509 at runtime, it is no longer readonly and we need to
5510 avoid MEM_READONLY_P being set on RTL created for it. */
32735518
JJ
5511 if (init)
5512 {
5513 if (TREE_READONLY (decl))
5514 TREE_READONLY (decl) = 0;
5515 was_readonly = 0;
5516 }
27edb548
JJ
5517 expand_static_init (decl, init);
5518 }
c8094d83 5519 }
8d08fdba
MS
5520 }
5521
7e99327d
MM
5522 /* If a CLEANUP_STMT was created to destroy a temporary bound to a
5523 reference, insert it in the statement-tree now. */
5524 if (cleanup)
325c3691 5525 push_cleanup (decl, cleanup, false);
7e99327d 5526
8d08fdba 5527 finish_end:
2b643eda 5528 processing_template_decl = saved_processing_template_decl;
8d08fdba 5529
8d08fdba
MS
5530 if (was_readonly)
5531 TREE_READONLY (decl) = 1;
d1bd0ded
GK
5532
5533 /* If this was marked 'used', be sure it will be output. */
5534 if (lookup_attribute ("used", DECL_ATTRIBUTES (decl)))
bb9a388d 5535 mark_decl_referenced (decl);
8d08fdba
MS
5536}
5537
f4f206f4 5538/* This is here for a midend callback from c-common.c. */
e92cc029 5539
82580166 5540void
11f6b451 5541finish_decl (tree decl, tree init, tree asmspec_tree)
82580166 5542{
d174af6c 5543 cp_finish_decl (decl, init, /*init_const_expr_p=*/false, asmspec_tree, 0);
82580166
MS
5544}
5545
db4283a0
MM
5546/* Returns a declaration for a VAR_DECL as if:
5547
5548 extern "C" TYPE NAME;
5549
5550 had been seen. Used to create compiler-generated global
5551 variables. */
5552
993acaec 5553static tree
11f6b451 5554declare_global_var (tree name, tree type)
db4283a0
MM
5555{
5556 tree decl;
5557
5558 push_to_top_level ();
5559 decl = build_decl (VAR_DECL, name, type);
5560 TREE_PUBLIC (decl) = 1;
5561 DECL_EXTERNAL (decl) = 1;
5562 DECL_ARTIFICIAL (decl) = 1;
de3fe73c
MM
5563 /* If the user has explicitly declared this variable (perhaps
5564 because the code we are compiling is part of a low-level runtime
5565 library), then it is possible that our declaration will be merged
5566 with theirs by pushdecl. */
5567 decl = pushdecl (decl);
d174af6c 5568 finish_decl (decl, NULL_TREE, NULL_TREE);
db4283a0
MM
5569 pop_from_top_level ();
5570
5571 return decl;
5572}
5573
46a9e521
MM
5574/* Returns the type for the argument to "__cxa_atexit" (or "atexit",
5575 if "__cxa_atexit" is not being used) corresponding to the function
5576 to be called when the program exits. */
5577
5578static tree
5579get_atexit_fn_ptr_type (void)
5580{
5581 tree arg_types;
5582 tree fn_type;
5583
5584 if (!atexit_fn_ptr_type_node)
5585 {
5586 if (flag_use_cxa_atexit
5587 && !targetm.cxx.use_atexit_for_cxa_atexit ())
5588 /* The parameter to "__cxa_atexit" is "void (*)(void *)". */
5589 arg_types = tree_cons (NULL_TREE, ptr_type_node, void_list_node);
5590 else
5591 /* The parameter to "atexit" is "void (*)(void)". */
5592 arg_types = void_list_node;
5593
5594 fn_type = build_function_type (void_type_node, arg_types);
5595 atexit_fn_ptr_type_node = build_pointer_type (fn_type);
5596 }
5597
5598 return atexit_fn_ptr_type_node;
5599}
5600
db4283a0 5601/* Returns a pointer to the `atexit' function. Note that if
838dfd8a 5602 FLAG_USE_CXA_ATEXIT is nonzero, then this will actually be the new
db4283a0
MM
5603 `__cxa_atexit' function specified in the IA64 C++ ABI. */
5604
5605static tree
11f6b451 5606get_atexit_node (void)
db4283a0
MM
5607{
5608 tree atexit_fndecl;
5609 tree arg_types;
5610 tree fn_type;
5611 tree fn_ptr_type;
5612 const char *name;
9f62c3e3 5613 bool use_aeabi_atexit;
db4283a0
MM
5614
5615 if (atexit_node)
5616 return atexit_node;
5617
97388150 5618 if (flag_use_cxa_atexit && !targetm.cxx.use_atexit_for_cxa_atexit ())
db4283a0
MM
5619 {
5620 /* The declaration for `__cxa_atexit' is:
5621
5622 int __cxa_atexit (void (*)(void *), void *, void *)
5623
5624 We build up the argument types and then then function type
5625 itself. */
68642fb6 5626
9f62c3e3 5627 use_aeabi_atexit = targetm.cxx.use_aeabi_atexit ();
db4283a0
MM
5628 /* First, build the pointer-to-function type for the first
5629 argument. */
46a9e521 5630 fn_ptr_type = get_atexit_fn_ptr_type ();
db4283a0
MM
5631 /* Then, build the rest of the argument types. */
5632 arg_types = tree_cons (NULL_TREE, ptr_type_node, void_list_node);
9f62c3e3
PB
5633 if (use_aeabi_atexit)
5634 {
5635 arg_types = tree_cons (NULL_TREE, fn_ptr_type, arg_types);
5636 arg_types = tree_cons (NULL_TREE, ptr_type_node, arg_types);
5637 }
5638 else
5639 {
5640 arg_types = tree_cons (NULL_TREE, ptr_type_node, arg_types);
5641 arg_types = tree_cons (NULL_TREE, fn_ptr_type, arg_types);
5642 }
db4283a0
MM
5643 /* And the final __cxa_atexit type. */
5644 fn_type = build_function_type (integer_type_node, arg_types);
5645 fn_ptr_type = build_pointer_type (fn_type);
9f62c3e3
PB
5646 if (use_aeabi_atexit)
5647 name = "__aeabi_atexit";
5648 else
5649 name = "__cxa_atexit";
db4283a0
MM
5650 }
5651 else
5652 {
5653 /* The declaration for `atexit' is:
68642fb6 5654
0cbd7506 5655 int atexit (void (*)());
db4283a0
MM
5656
5657 We build up the argument types and then then function type
5658 itself. */
46a9e521 5659 fn_ptr_type = get_atexit_fn_ptr_type ();
db4283a0
MM
5660 arg_types = tree_cons (NULL_TREE, fn_ptr_type, void_list_node);
5661 /* Build the final atexit type. */
5662 fn_type = build_function_type (integer_type_node, arg_types);
5663 name = "atexit";
5664 }
5665
5666 /* Now, build the function declaration. */
5667 push_lang_context (lang_name_c);
0c11ada6 5668 atexit_fndecl = build_library_fn_ptr (name, fn_type);
db4283a0
MM
5669 mark_used (atexit_fndecl);
5670 pop_lang_context ();
0a72704b 5671 atexit_node = decay_conversion (atexit_fndecl);
db4283a0
MM
5672
5673 return atexit_node;
5674}
5675
5676/* Returns the __dso_handle VAR_DECL. */
5677
5678static tree
11f6b451 5679get_dso_handle_node (void)
db4283a0
MM
5680{
5681 if (dso_handle_node)
5682 return dso_handle_node;
5683
5684 /* Declare the variable. */
5685 dso_handle_node = declare_global_var (get_identifier ("__dso_handle"),
5686 ptr_type_node);
5687
5688 return dso_handle_node;
5689}
5690
5691/* Begin a new function with internal linkage whose job will be simply
5692 to destroy some particular variable. */
5693
d192828a
GK
5694static GTY(()) int start_cleanup_cnt;
5695
db4283a0 5696static tree
11f6b451 5697start_cleanup_fn (void)
db4283a0 5698{
db4283a0 5699 char name[32];
db4283a0
MM
5700 tree fntype;
5701 tree fndecl;
97388150
DS
5702 bool use_cxa_atexit = flag_use_cxa_atexit
5703 && !targetm.cxx.use_atexit_for_cxa_atexit ();
db4283a0
MM
5704
5705 push_to_top_level ();
5706
5707 /* No need to mangle this. */
5708 push_lang_context (lang_name_c);
5709
db4283a0 5710 /* Build the name of the function. */
d192828a 5711 sprintf (name, "__tcf_%d", start_cleanup_cnt++);
db4283a0 5712 /* Build the function declaration. */
46a9e521 5713 fntype = TREE_TYPE (get_atexit_fn_ptr_type ());
db4283a0
MM
5714 fndecl = build_lang_decl (FUNCTION_DECL, get_identifier (name), fntype);
5715 /* It's a function with internal linkage, generated by the
5716 compiler. */
5717 TREE_PUBLIC (fndecl) = 0;
5718 DECL_ARTIFICIAL (fndecl) = 1;
5a728aca
MM
5719 /* Make the function `inline' so that it is only emitted if it is
5720 actually needed. It is unlikely that it will be inlined, since
aba649ba 5721 it is only called via a function pointer, but we avoid unnecessary
5a728aca
MM
5722 emissions this way. */
5723 DECL_INLINE (fndecl) = 1;
d4d1ebc1
JH
5724 DECL_DECLARED_INLINE_P (fndecl) = 1;
5725 DECL_INTERFACE_KNOWN (fndecl) = 1;
db4283a0 5726 /* Build the parameter. */
97388150 5727 if (use_cxa_atexit)
db4283a0
MM
5728 {
5729 tree parmdecl;
5730
8e51619a 5731 parmdecl = cp_build_parm_decl (NULL_TREE, ptr_type_node);
db4283a0 5732 DECL_CONTEXT (parmdecl) = fndecl;
db4283a0
MM
5733 TREE_USED (parmdecl) = 1;
5734 DECL_ARGUMENTS (fndecl) = parmdecl;
5735 }
5736
09ed39ad 5737 pushdecl (fndecl);
058b15c1 5738 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
db4283a0 5739
db4283a0
MM
5740 pop_lang_context ();
5741
5742 return current_function_decl;
5743}
5744
5745/* Finish the cleanup function begun by start_cleanup_fn. */
5746
5747static void
11f6b451 5748end_cleanup_fn (void)
db4283a0 5749{
8cd2462c 5750 expand_or_defer_fn (finish_function (0));
db4283a0
MM
5751
5752 pop_from_top_level ();
5753}
5754
bf419747
MM
5755/* Generate code to handle the destruction of DECL, an object with
5756 static storage duration. */
f0105ed3 5757
40aac948 5758tree
11f6b451 5759register_dtor_fn (tree decl)
f0105ed3 5760{
db4283a0 5761 tree cleanup;
f0105ed3 5762 tree compound_stmt;
db4283a0
MM
5763 tree args;
5764 tree fcall;
46a9e521
MM
5765 tree type;
5766 bool use_dtor;
f0105ed3 5767
46a9e521
MM
5768 type = TREE_TYPE (decl);
5769 if (TYPE_HAS_TRIVIAL_DESTRUCTOR (type))
40aac948 5770 return void_zero_node;
bf419747 5771
46a9e521
MM
5772 /* If we're using "__cxa_atexit" (or "__aeabi_atexit"), and DECL is
5773 a class object, we can just pass the destructor to
5774 "__cxa_atexit"; we don't have to build a temporary function to do
5775 the cleanup. */
5776 use_dtor = (flag_use_cxa_atexit
5777 && !targetm.cxx.use_atexit_for_cxa_atexit ()
5778 && CLASS_TYPE_P (type));
5779 if (use_dtor)
5780 {
5781 int idx;
5782
5783 /* Find the destructor. */
5784 idx = lookup_fnfields_1 (type, complete_dtor_identifier);
5785 gcc_assert (idx >= 0);
5786 cleanup = VEC_index (tree, CLASSTYPE_METHOD_VEC (type), idx);
5787 /* Make sure it is accessible. */
5788 perform_or_defer_access_check (TYPE_BINFO (type), cleanup, cleanup);
5789 }
5790 else
5791 {
5792 /* Call build_cleanup before we enter the anonymous function so
5793 that any access checks will be done relative to the current
5794 scope, rather than the scope of the anonymous function. */
5795 build_cleanup (decl);
5796
5797 /* Now start the function. */
5798 cleanup = start_cleanup_fn ();
5799
5800 /* Now, recompute the cleanup. It may contain SAVE_EXPRs that refer
5801 to the original function, rather than the anonymous one. That
5802 will make the back end think that nested functions are in use,
5803 which causes confusion. */
5804 push_deferring_access_checks (dk_no_check);
5805 fcall = build_cleanup (decl);
5806 pop_deferring_access_checks ();
5807
5808 /* Create the body of the anonymous function. */
5809 compound_stmt = begin_compound_stmt (BCS_FN_BODY);
5810 finish_expr_stmt (fcall);
5811 finish_compound_stmt (compound_stmt);
5812 end_cleanup_fn ();
5813 }
f0105ed3
MM
5814
5815 /* Call atexit with the cleanup function. */
bb4f6e6b 5816 mark_used (cleanup);
46a9e521 5817 cleanup = build_address (cleanup);
97388150 5818 if (flag_use_cxa_atexit && !targetm.cxx.use_atexit_for_cxa_atexit ())
db4283a0 5819 {
46a9e521
MM
5820 tree addr;
5821
5822 if (use_dtor)
5823 {
5824 /* We must convert CLEANUP to the type that "__cxa_atexit"
5825 expects. */
5826 cleanup = build_nop (get_atexit_fn_ptr_type (), cleanup);
5827 /* "__cxa_atexit" will pass the address of DECL to the
5828 cleanup function. */
5829 mark_used (decl);
5830 addr = build_address (decl);
5831 /* The declared type of the parameter to "__cxa_atexit" is
5832 "void *". For plain "T*", we could just let the
5833 machinery in build_function_call convert it -- but if the
5834 type is "cv-qualified T *", then we need to convert it
5835 before passing it in, to avoid spurious errors. */
5836 addr = build_nop (ptr_type_node, addr);
5837 }
5838 else
5839 /* Since the cleanup functions we build ignore the address
5840 they're given, there's no reason to pass the actual address
5841 in, and, in general, it's cheaper to pass NULL than any
5842 other value. */
5843 addr = null_pointer_node;
caf93cb0 5844 args = tree_cons (NULL_TREE,
0f268005
MM
5845 build_unary_op (ADDR_EXPR, get_dso_handle_node (), 0),
5846 NULL_TREE);
9f62c3e3
PB
5847 if (targetm.cxx.use_aeabi_atexit ())
5848 {
5849 args = tree_cons (NULL_TREE, cleanup, args);
46a9e521 5850 args = tree_cons (NULL_TREE, addr, args);
9f62c3e3
PB
5851 }
5852 else
5853 {
46a9e521 5854 args = tree_cons (NULL_TREE, addr, args);
9f62c3e3
PB
5855 args = tree_cons (NULL_TREE, cleanup, args);
5856 }
db4283a0
MM
5857 }
5858 else
5859 args = tree_cons (NULL_TREE, cleanup, NULL_TREE);
40aac948 5860 return build_function_call (get_atexit_node (), args);
f0105ed3
MM
5861}
5862
8e3df2de
MM
5863/* DECL is a VAR_DECL with static storage duration. INIT, if present,
5864 is its initializer. Generate code to handle the construction
5865 and destruction of DECL. */
5866
5867static void
11f6b451 5868expand_static_init (tree decl, tree init)
8d08fdba 5869{
50bc768d
NS
5870 gcc_assert (TREE_CODE (decl) == VAR_DECL);
5871 gcc_assert (TREE_STATIC (decl));
8e3df2de
MM
5872
5873 /* Some variables require no initialization. */
caf93cb0 5874 if (!init
8e3df2de
MM
5875 && !TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl))
5876 && TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
5877 return;
5878
eba6cfb6 5879 if (DECL_FUNCTION_SCOPE_P (decl))
8d08fdba
MS
5880 {
5881 /* Emit code to perform this initialization but once. */
445cf5eb
JM
5882 tree if_stmt = NULL_TREE, inner_if_stmt = NULL_TREE;
5883 tree then_clause = NULL_TREE, inner_then_clause = NULL_TREE;
94a0dd7b 5884 tree guard, guard_addr;
40aac948
JM
5885 tree acquire_fn, release_fn, abort_fn;
5886 tree flag, begin;
8d08fdba 5887
2036a15c
MM
5888 /* Emit code to perform this initialization but once. This code
5889 looks like:
5890
0cbd7506
MS
5891 static <type> guard;
5892 if (!guard.first_byte) {
40aac948
JM
5893 if (__cxa_guard_acquire (&guard)) {
5894 bool flag = false;
5895 try {
0cbd7506
MS
5896 // Do initialization.
5897 flag = true; __cxa_guard_release (&guard);
5898 // Register variable for destruction at end of program.
40aac948 5899 } catch {
0cbd7506 5900 if (!flag) __cxa_guard_abort (&guard);
40aac948 5901 }
2036a15c
MM
5902 }
5903
40aac948 5904 Note that the `flag' variable is only set to 1 *after* the
2036a15c
MM
5905 initialization is complete. This ensures that an exception,
5906 thrown during the construction, will cause the variable to
5907 reinitialized when we pass through this code again, as per:
68642fb6 5908
2036a15c
MM
5909 [stmt.dcl]
5910
c8094d83 5911 If the initialization exits by throwing an exception, the
2036a15c
MM
5912 initialization is not complete, so it will be tried again
5913 the next time control enters the declaration.
5914
0cbd7506 5915 This process should be thread-safe, too; multiple threads
40aac948
JM
5916 should not be able to initialize the variable more than
5917 once. */
c395453c
MM
5918
5919 /* Create the guard variable. */
5920 guard = get_guard (decl);
2036a15c 5921
445cf5eb
JM
5922 /* This optimization isn't safe on targets with relaxed memory
5923 consistency. On such targets we force synchronization in
5924 __cxa_guard_acquire. */
5925 if (!targetm.relaxed_ordering || !flag_threadsafe_statics)
5926 {
5927 /* Begin the conditional initialization. */
5928 if_stmt = begin_if_stmt ();
5929 finish_if_stmt_cond (get_guard_cond (guard), if_stmt);
5930 then_clause = begin_compound_stmt (BCS_NO_SCOPE);
5931 }
40aac948
JM
5932
5933 if (flag_threadsafe_statics)
5934 {
5935 guard_addr = build_address (guard);
40aac948
JM
5936
5937 acquire_fn = get_identifier ("__cxa_guard_acquire");
5938 release_fn = get_identifier ("__cxa_guard_release");
5939 abort_fn = get_identifier ("__cxa_guard_abort");
5940 if (!get_global_value_if_present (acquire_fn, &acquire_fn))
5941 {
5942 tree argtypes = tree_cons (NULL_TREE, TREE_TYPE (guard_addr),
5943 void_list_node);
5944 tree vfntype = build_function_type (void_type_node, argtypes);
5945 acquire_fn = push_library_fn
5946 (acquire_fn, build_function_type (integer_type_node, argtypes));
5947 release_fn = push_library_fn (release_fn, vfntype);
5948 abort_fn = push_library_fn (abort_fn, vfntype);
5949 }
5950 else
5951 {
5952 release_fn = identifier_global_value (release_fn);
5953 abort_fn = identifier_global_value (abort_fn);
5954 }
5955
5956 inner_if_stmt = begin_if_stmt ();
94a0dd7b 5957 finish_if_stmt_cond (build_call_n (acquire_fn, 1, guard_addr),
40aac948
JM
5958 inner_if_stmt);
5959
5960 inner_then_clause = begin_compound_stmt (BCS_NO_SCOPE);
5961 begin = get_target_expr (boolean_false_node);
5962 flag = TARGET_EXPR_SLOT (begin);
5963
5964 TARGET_EXPR_CLEANUP (begin)
81eba3a8
KH
5965 = build3 (COND_EXPR, void_type_node, flag,
5966 void_zero_node,
94a0dd7b 5967 build_call_n (abort_fn, 1, guard_addr));
40aac948
JM
5968 CLEANUP_EH_ONLY (begin) = 1;
5969
5970 /* Do the initialization itself. */
5971 init = add_stmt_to_compound (begin, init);
5972 init = add_stmt_to_compound
81eba3a8 5973 (init, build2 (MODIFY_EXPR, void_type_node, flag, boolean_true_node));
40aac948 5974 init = add_stmt_to_compound
94a0dd7b 5975 (init, build_call_n (release_fn, 1, guard_addr));
40aac948 5976 }
f1dedc31 5977 else
40aac948 5978 init = add_stmt_to_compound (init, set_guard (guard));
72b7eeff 5979
2036a15c
MM
5980 /* Use atexit to register a function for destroying this static
5981 variable. */
40aac948
JM
5982 init = add_stmt_to_compound (init, register_dtor_fn (decl));
5983
5984 finish_expr_stmt (init);
5985
5986 if (flag_threadsafe_statics)
5987 {
5988 finish_compound_stmt (inner_then_clause);
5989 finish_then_clause (inner_if_stmt);
5990 finish_if_stmt (inner_if_stmt);
5991 }
72b7eeff 5992
445cf5eb
JM
5993 if (!targetm.relaxed_ordering || !flag_threadsafe_statics)
5994 {
5995 finish_compound_stmt (then_clause);
5996 finish_then_clause (if_stmt);
5997 finish_if_stmt (if_stmt);
5998 }
8d08fdba
MS
5999 }
6000 else
bbd15aac 6001 static_aggregates = tree_cons (init, decl, static_aggregates);
8d08fdba 6002}
3c5c0849 6003
8d08fdba
MS
6004\f
6005/* Make TYPE a complete type based on INITIAL_VALUE.
6006 Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
07900878
PC
6007 2 if there was no information (in which case assume 0 if DO_DEFAULT),
6008 3 if the initializer list is empty (in pedantic mode). */
8d08fdba
MS
6009
6010int
aab038d5 6011cp_complete_array_type (tree *ptype, tree initial_value, bool do_default)
8d08fdba 6012{
aab038d5
RH
6013 int failure;
6014 tree type, elt_type;
68642fb6 6015
8d08fdba
MS
6016 if (initial_value)
6017 {
7b019c19 6018 /* An array of character type can be initialized from a
4038c495
GB
6019 brace-enclosed string constant.
6020
6021 FIXME: this code is duplicated from reshape_init. Probably
6022 we should just call reshape_init here? */
aab038d5 6023 if (char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (*ptype)))
7b019c19 6024 && TREE_CODE (initial_value) == CONSTRUCTOR
4038c495
GB
6025 && !VEC_empty (constructor_elt, CONSTRUCTOR_ELTS (initial_value)))
6026 {
6027 VEC(constructor_elt,gc) *v = CONSTRUCTOR_ELTS (initial_value);
6028 tree value = VEC_index (constructor_elt, v, 0)->value;
6029
6030 if (TREE_CODE (value) == STRING_CST
6031 && VEC_length (constructor_elt, v) == 1)
6032 initial_value = value;
6033 }
8d08fdba
MS
6034 }
6035
aab038d5 6036 failure = complete_array_type (ptype, initial_value, do_default);
8d08fdba 6037
aab038d5
RH
6038 /* We can create the array before the element type is complete, which
6039 means that we didn't have these two bits set in the original type
6040 either. In completing the type, we are expected to propagate these
6041 bits. See also complete_type which does the same thing for arrays
6042 of fixed size. */
6043 type = *ptype;
6044 if (TYPE_DOMAIN (type))
8d08fdba 6045 {
81b4398d 6046 elt_type = TREE_TYPE (type);
aab038d5 6047 TYPE_NEEDS_CONSTRUCTING (type) = TYPE_NEEDS_CONSTRUCTING (elt_type);
81b4398d 6048 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
aab038d5 6049 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (elt_type);
8d08fdba
MS
6050 }
6051
aab038d5 6052 return failure;
8d08fdba
MS
6053}
6054\f
6055/* Return zero if something is declared to be a member of type
6056 CTYPE when in the context of CUR_TYPE. STRING is the error
6057 message to print in that case. Otherwise, quietly return 1. */
e92cc029 6058
8d08fdba 6059static int
11f6b451 6060member_function_or_else (tree ctype, tree cur_type, enum overload_flags flags)
8d08fdba
MS
6061{
6062 if (ctype && ctype != cur_type)
6063 {
4dacf5bd 6064 if (flags == DTOR_FLAG)
2d01edd7 6065 error ("destructor for alien class %qT cannot be a member", ctype);
4dacf5bd 6066 else
2d01edd7 6067 error ("constructor for alien class %qT cannot be a member", ctype);
8d08fdba
MS
6068 return 0;
6069 }
6070 return 1;
6071}
6072\f
6073/* Subroutine of `grokdeclarator'. */
6074
6075/* Generate errors possibly applicable for a given set of specifiers.
6076 This is for ARM $7.1.2. */
e92cc029 6077
8d08fdba 6078static void
11f6b451 6079bad_specifiers (tree object,
0cbd7506
MS
6080 const char* type,
6081 int virtualp,
6082 int quals,
6083 int inlinep,
6084 int friendp,
6085 int raises)
8d08fdba
MS
6086{
6087 if (virtualp)
2d01edd7 6088 error ("%qD declared as a %<virtual%> %s", object, type);
8d08fdba 6089 if (inlinep)
2d01edd7 6090 error ("%qD declared as an %<inline%> %s", object, type);
8d08fdba 6091 if (quals)
2d01edd7 6092 error ("%<const%> and %<volatile%> function specifiers on "
0cbd7506
MS
6093 "%qD invalid in %s declaration",
6094 object, type);
8d08fdba 6095 if (friendp)
dee15844 6096 error ("%q+D declared as a friend", object);
e1be26f4
RS
6097 if (raises
6098 && (TREE_CODE (object) == TYPE_DECL
6099 || (!TYPE_PTRFN_P (TREE_TYPE (object))
742a37d5 6100 && !TYPE_REFFN_P (TREE_TYPE (object))
e1be26f4 6101 && !TYPE_PTRMEMFUNC_P (TREE_TYPE (object)))))
dee15844 6102 error ("%q+D declared with an exception specification", object);
8d08fdba
MS
6103}
6104
9c12301f
MM
6105/* DECL is a member function or static data member and is presently
6106 being defined. Check that the definition is taking place in a
6107 valid namespace. */
6108
6109static void
6110check_class_member_definition_namespace (tree decl)
6111{
6112 /* These checks only apply to member functions and static data
6113 members. */
6114 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
6115 || TREE_CODE (decl) == VAR_DECL);
6116 /* We check for problems with specializations in pt.c in
6117 check_specialization_namespace, where we can issue better
6118 diagnostics. */
6119 if (processing_specialization)
6120 return;
6121 /* There are no restrictions on the placement of
6122 explicit instantiations. */
6123 if (processing_explicit_instantiation)
6124 return;
6125 /* [class.mfct]
6126
6127 A member function definition that appears outside of the
6128 class definition shall appear in a namespace scope enclosing
6129 the class definition.
6130
6131 [class.static.data]
6132
6133 The definition for a static data member shall appear in a
6134 namespace scope enclosing the member's class definition. */
6135 if (!is_ancestor (current_namespace, DECL_CONTEXT (decl)))
6136 pedwarn ("definition of %qD is not in namespace enclosing %qT",
6137 decl, DECL_CONTEXT (decl));
6138}
6139
e2537f2c
MM
6140/* Build a PARM_DECL for the "this" parameter. TYPE is the
6141 METHOD_TYPE for a non-static member function; QUALS are the
6142 cv-qualifiers that apply to the function. */
3db45ab5 6143
e2537f2c
MM
6144tree
6145build_this_parm (tree type, cp_cv_quals quals)
6146{
6147 tree this_type;
6148 tree qual_type;
6149 tree parm;
6150 cp_cv_quals this_quals;
6151
6152 this_type = TREE_VALUE (TYPE_ARG_TYPES (type));
6153 /* The `this' parameter is implicitly `const'; it cannot be
6154 assigned to. */
6155 this_quals = (quals & TYPE_QUAL_RESTRICT) | TYPE_QUAL_CONST;
6156 qual_type = cp_build_qualified_type (this_type, this_quals);
6157 parm = build_artificial_parm (this_identifier, qual_type);
6158 cp_apply_type_quals_to_decl (this_quals, parm);
6159 return parm;
3db45ab5 6160}
e2537f2c 6161
8d08fdba
MS
6162/* CTYPE is class type, or null if non-class.
6163 TYPE is type this FUNCTION_DECL should have, either FUNCTION_TYPE
6164 or METHOD_TYPE.
6165 DECLARATOR is the function's name.
4546865e 6166 PARMS is a chain of PARM_DECLs for the function.
8d08fdba
MS
6167 VIRTUALP is truthvalue of whether the function is virtual or not.
6168 FLAGS are to be passed through to `grokclassfn'.
6169 QUALS are qualifiers indicating whether the function is `const'
6170 or `volatile'.
6171 RAISES is a list of exceptions that this function can raise.
6172 CHECK is 1 if we must find this method in CTYPE, 0 if we should
68642fb6 6173 not look, and -1 if we should not call `grokclassfn' at all.
3ddfb0e6 6174
27d6592c
MM
6175 SFK is the kind of special function (if any) for the new function.
6176
20496fa2 6177 Returns `NULL_TREE' if something goes wrong, after issuing
3ddfb0e6 6178 applicable error messages. */
e92cc029 6179
8d08fdba 6180static tree
caf93cb0 6181grokfndecl (tree ctype,
0cbd7506
MS
6182 tree type,
6183 tree declarator,
4546865e 6184 tree parms,
0cbd7506
MS
6185 tree orig_declarator,
6186 int virtualp,
6187 enum overload_flags flags,
3c01e5df 6188 cp_cv_quals quals,
0cbd7506
MS
6189 tree raises,
6190 int check,
6191 int friendp,
6192 int publicp,
6193 int inlinep,
27d6592c 6194 special_function_kind sfk,
357d956e 6195 bool funcdef_flag,
0cbd7506
MS
6196 int template_count,
6197 tree in_namespace,
037cc9c5 6198 tree* attrlist)
8d08fdba 6199{
1951a1b6 6200 tree decl;
8d08fdba 6201 int staticp = ctype && TREE_CODE (type) == FUNCTION_TYPE;
42976354 6202 tree t;
8d08fdba 6203
8d08fdba 6204 if (raises)
271e6f02 6205 type = build_exception_variant (type, raises);
c11b6f21 6206
8d08fdba 6207 decl = build_lang_decl (FUNCTION_DECL, declarator, type);
e2537f2c
MM
6208 if (TREE_CODE (type) == METHOD_TYPE)
6209 {
6210 tree parm;
6211 parm = build_this_parm (type, quals);
6212 TREE_CHAIN (parm) = parms;
6213 parms = parm;
6214 }
4546865e 6215 DECL_ARGUMENTS (decl) = parms;
a1c65f9f 6216 /* Propagate volatile out from type to decl. */
8d08fdba 6217 if (TYPE_VOLATILE (type))
893de33c 6218 TREE_THIS_VOLATILE (decl) = 1;
8d08fdba 6219
71aea5f2
SM
6220 /* Setup decl according to sfk. */
6221 switch (sfk)
6222 {
6223 case sfk_constructor:
6224 case sfk_copy_constructor:
6225 DECL_CONSTRUCTOR_P (decl) = 1;
6226 break;
6227 case sfk_destructor:
6228 DECL_DESTRUCTOR_P (decl) = 1;
6229 break;
6230 default:
6231 break;
6232 }
6233
837edd5f
GK
6234 /* If pointers to member functions use the least significant bit to
6235 indicate whether a function is virtual, ensure a pointer
6236 to this function will have that bit clear. */
6237 if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_pfn
6238 && TREE_CODE (type) == METHOD_TYPE
6239 && DECL_ALIGN (decl) < 2 * BITS_PER_UNIT)
6240 DECL_ALIGN (decl) = 2 * BITS_PER_UNIT;
6241
abc088aa
MM
6242 if (friendp
6243 && TREE_CODE (orig_declarator) == TEMPLATE_ID_EXPR)
6244 {
6245 if (funcdef_flag)
6246 error
6247 ("defining explicit specialization %qD in friend declaration",
6248 orig_declarator);
6249 else
6250 {
6251 tree fns = TREE_OPERAND (orig_declarator, 0);
6252 tree args = TREE_OPERAND (orig_declarator, 1);
6253
6254 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
6255 {
6256 /* Something like `template <class T> friend void f<T>()'. */
6257 error ("invalid use of template-id %qD in declaration "
6258 "of primary template",
6259 orig_declarator);
6260 return NULL_TREE;
6261 }
6262
6263
6264 /* A friend declaration of the form friend void f<>(). Record
6265 the information in the TEMPLATE_ID_EXPR. */
6266 SET_DECL_IMPLICIT_INSTANTIATION (decl);
6267
6268 if (TREE_CODE (fns) == COMPONENT_REF)
6269 {
6270 /* Due to bison parser ickiness, we will have already looked
6271 up an operator_name or PFUNCNAME within the current class
6272 (see template_id in parse.y). If the current class contains
6273 such a name, we'll get a COMPONENT_REF here. Undo that. */
6274
6275 gcc_assert (TREE_TYPE (TREE_OPERAND (fns, 0))
6276 == current_class_type);
6277 fns = TREE_OPERAND (fns, 1);
6278 }
6279 gcc_assert (TREE_CODE (fns) == IDENTIFIER_NODE
6280 || TREE_CODE (fns) == OVERLOAD);
6281 DECL_TEMPLATE_INFO (decl) = tree_cons (fns, args, NULL_TREE);
6282
6283 for (t = TYPE_ARG_TYPES (TREE_TYPE (decl)); t; t = TREE_CHAIN (t))
6284 if (TREE_PURPOSE (t)
6285 && TREE_CODE (TREE_PURPOSE (t)) == DEFAULT_ARG)
6286 {
6287 error ("default arguments are not allowed in declaration "
6288 "of friend template specialization %qD",
6289 decl);
6290 return NULL_TREE;
6291 }
6292
6293 if (inlinep)
6294 {
6295 error ("%<inline%> is not allowed in declaration of friend "
6296 "template specialization %qD",
6297 decl);
6298 return NULL_TREE;
6299 }
6300 }
6301 }
6302
79c4d4b7 6303 /* If this decl has namespace scope, set that up. */
2c73f9f5 6304 if (in_namespace)
b262d64c 6305 set_decl_namespace (decl, in_namespace, friendp);
adae082f 6306 else if (!ctype)
79c4d4b7 6307 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
2c73f9f5 6308
0f8766b8
JM
6309 /* `main' and builtins have implicit 'C' linkage. */
6310 if ((MAIN_NAME_P (declarator)
6311 || (IDENTIFIER_LENGTH (declarator) > 10
6312 && IDENTIFIER_POINTER (declarator)[0] == '_'
6313 && IDENTIFIER_POINTER (declarator)[1] == '_'
6314 && strncmp (IDENTIFIER_POINTER (declarator)+2, "builtin_", 8) == 0))
6315 && current_lang_name == lang_name_cplusplus
94706a5c 6316 && ctype == NULL_TREE
79c4d4b7
JM
6317 /* NULL_TREE means global namespace. */
6318 && DECL_CONTEXT (decl) == NULL_TREE)
5d2ed28c 6319 SET_DECL_LANGUAGE (decl, lang_c);
0f8766b8 6320
8d08fdba
MS
6321 /* Should probably propagate const out from type to decl I bet (mrs). */
6322 if (staticp)
6323 {
6324 DECL_STATIC_FUNCTION_P (decl) = 1;
6325 DECL_CONTEXT (decl) = ctype;
8d08fdba
MS
6326 }
6327
e76a2646 6328 if (ctype)
9c12301f
MM
6329 {
6330 DECL_CONTEXT (decl) = ctype;
6331 if (funcdef_flag)
6332 check_class_member_definition_namespace (decl);
6333 }
e76a2646 6334
0f8766b8 6335 if (ctype == NULL_TREE && DECL_MAIN_P (decl))
faae18ab 6336 {
848b92e1 6337 if (processing_template_decl)
2d01edd7 6338 error ("cannot declare %<::main%> to be a template");
faae18ab 6339 if (inlinep)
2d01edd7 6340 error ("cannot declare %<::main%> to be inline");
f22967f3 6341 if (!publicp)
2d01edd7 6342 error ("cannot declare %<::main%> to be static");
faae18ab
MS
6343 inlinep = 0;
6344 publicp = 1;
6345 }
50a6dbd7 6346
59e76fc6 6347 /* Members of anonymous types and local classes have no linkage; make
9fc444cc 6348 them internal. If a typedef is made later, this will be changed. */
1951a1b6 6349 if (ctype && (TYPE_ANONYMOUS_P (ctype)
4f1c5b7d 6350 || decl_function_context (TYPE_MAIN_DECL (ctype))))
50a6dbd7
JM
6351 publicp = 0;
6352
6353 if (publicp)
6354 {
6355 /* [basic.link]: A name with no linkage (notably, the name of a class
6356 or enumeration declared in a local scope) shall not be used to
6357 declare an entity with linkage.
6358
50b97e0f 6359 Only check this for public decls for now. See core 319, 389. */
4684cd27
MM
6360 t = no_linkage_check (TREE_TYPE (decl),
6361 /*relaxed_p=*/false);
50a6dbd7
JM
6362 if (t)
6363 {
1951a1b6 6364 if (TYPE_ANONYMOUS_P (t))
7f7c930e 6365 {
eb68cb58 6366 if (DECL_EXTERN_C_P (decl))
7f7c930e
JM
6367 /* Allow this; it's pretty common in C. */;
6368 else
1951a1b6 6369 {
2d01edd7 6370 pedwarn ("non-local function %q#D uses anonymous type",
1951a1b6
JM
6371 decl);
6372 if (DECL_ORIGINAL_TYPE (TYPE_NAME (t)))
dee15844
JM
6373 pedwarn ("%q+#D does not refer to the unqualified "
6374 "type, so it is not used for linkage",
6375 TYPE_NAME (t));
1951a1b6 6376 }
7f7c930e 6377 }
50a6dbd7 6378 else
2d01edd7 6379 pedwarn ("non-local function %q#D uses local type %qT", decl, t);
50a6dbd7
JM
6380 }
6381 }
6382
893de33c 6383 TREE_PUBLIC (decl) = publicp;
faae18ab 6384 if (! publicp)
893de33c
JM
6385 {
6386 DECL_INTERFACE_KNOWN (decl) = 1;
6387 DECL_NOT_REALLY_EXTERN (decl) = 1;
6388 }
faae18ab 6389
acc72c37 6390 /* If the declaration was declared inline, mark it as such. */
faae18ab 6391 if (inlinep)
acc72c37
MM
6392 DECL_DECLARED_INLINE_P (decl) = 1;
6393 /* We inline functions that are explicitly declared inline, or, when
6394 the user explicitly asks us to, all functions. */
b3c3af2f
SB
6395 if (DECL_DECLARED_INLINE_P (decl)
6396 || (flag_inline_trees == 2 && !DECL_INLINE (decl) && funcdef_flag))
acc72c37 6397 DECL_INLINE (decl) = 1;
8d08fdba
MS
6398
6399 DECL_EXTERNAL (decl) = 1;
3c01e5df 6400 if (quals && TREE_CODE (type) == FUNCTION_TYPE)
8d08fdba 6401 {
e7c41c99
DM
6402 error (ctype
6403 ? G_("static member function %qD cannot have cv-qualifier")
32b1d579 6404 : G_("non-member function %qD cannot have cv-qualifier"),
e7c41c99 6405 decl);
3c01e5df 6406 quals = TYPE_UNQUALIFIED;
8d08fdba
MS
6407 }
6408
398cd199
VR
6409 if (IDENTIFIER_OPNAME_P (DECL_NAME (decl))
6410 && !grok_op_properties (decl, /*complain=*/true))
6411 return NULL_TREE;
8d08fdba 6412
4f1c5b7d 6413 if (ctype && decl_function_context (decl))
893de33c 6414 DECL_NO_STATIC_CHAIN (decl) = 1;
e76a2646 6415
1eb0072d
JM
6416 if (funcdef_flag)
6417 /* Make the init_value nonzero so pushdecl knows this is not
6418 tentative. error_mark_node is replaced later with the BLOCK. */
6419 DECL_INITIAL (decl) = error_mark_node;
6420
93ca4ba7 6421 if (TYPE_NOTHROW_P (type) || nothrow_libfn_p (decl))
1660cb3a
JM
6422 TREE_NOTHROW (decl) = 1;
6423
75650646 6424 /* Caller will do the rest of this. */
8d08fdba
MS
6425 if (check < 0)
6426 return decl;
6427
74b846e0 6428 if (ctype != NULL_TREE)
71aea5f2 6429 grokclassfn (ctype, decl, flags);
74b846e0
MM
6430
6431 decl = check_explicit_specialization (orig_declarator, decl,
6432 template_count,
357d956e 6433 2 * funcdef_flag +
74b846e0
MM
6434 4 * (friendp != 0));
6435 if (decl == error_mark_node)
6436 return NULL_TREE;
98c1c668 6437
037cc9c5
FJ
6438 if (attrlist)
6439 {
6440 cplus_decl_attributes (&decl, *attrlist, 0);
6441 *attrlist = NULL_TREE;
6442 }
6443
da337784
JJ
6444 /* Check main's type after attributes have been applied. */
6445 if (ctype == NULL_TREE && DECL_MAIN_P (decl))
6446 {
6447 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
6448 integer_type_node))
6449 {
6450 tree oldtypeargs = TYPE_ARG_TYPES (TREE_TYPE (decl));
6451 tree newtype;
6452 error ("%<::main%> must return %<int%>");
6453 newtype = build_function_type (integer_type_node, oldtypeargs);
6454 TREE_TYPE (decl) = newtype;
6455 }
6456 check_main_parameter_types (decl);
6457 }
6458
74b846e0
MM
6459 if (ctype != NULL_TREE
6460 && (! TYPE_FOR_JAVA (ctype) || check_java_method (decl))
6461 && check)
8d08fdba 6462 {
f8c3b097
PC
6463 tree old_decl = check_classfn (ctype, decl,
6464 (processing_template_decl
6465 > template_class_depth (ctype))
6466 ? current_template_parms
6467 : NULL_TREE);
6468
6469 if (old_decl == error_mark_node)
6470 return NULL_TREE;
8d08fdba 6471
74b846e0 6472 if (old_decl)
8d08fdba 6473 {
79ae853c 6474 tree ok;
4514aa8c 6475 tree pushed_scope;
316a2456 6476
357d956e
MM
6477 if (TREE_CODE (old_decl) == TEMPLATE_DECL)
6478 /* Because grokfndecl is always supposed to return a
6479 FUNCTION_DECL, we pull out the DECL_TEMPLATE_RESULT
6480 here. We depend on our callers to figure out that its
6481 really a template that's being returned. */
6482 old_decl = DECL_TEMPLATE_RESULT (old_decl);
6483
6484 if (DECL_STATIC_FUNCTION_P (old_decl)
6485 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE)
6486 /* Remove the `this' parm added by grokclassfn.
6487 XXX Isn't this done in start_function, too? */
6488 revert_static_member_fn (decl);
6489 if (DECL_ARTIFICIAL (old_decl))
6490 error ("definition of implicitly-declared %qD", old_decl);
6491
74b846e0
MM
6492 /* Since we've smashed OLD_DECL to its
6493 DECL_TEMPLATE_RESULT, we must do the same to DECL. */
6494 if (TREE_CODE (decl) == TEMPLATE_DECL)
6495 decl = DECL_TEMPLATE_RESULT (decl);
6496
6497 /* Attempt to merge the declarations. This can fail, in
0e339752 6498 the case of some invalid specialization declarations. */
4514aa8c 6499 pushed_scope = push_scope (ctype);
d63d5d0c 6500 ok = duplicate_decls (decl, old_decl, friendp);
4514aa8c
NS
6501 if (pushed_scope)
6502 pop_scope (pushed_scope);
316a2456
MM
6503 if (!ok)
6504 {
2d01edd7 6505 error ("no %q#D member function declared in class %qT",
316a2456
MM
6506 decl, ctype);
6507 return NULL_TREE;
6508 }
74b846e0 6509 return old_decl;
8d08fdba
MS
6510 }
6511 }
74b846e0
MM
6512
6513 if (DECL_CONSTRUCTOR_P (decl) && !grok_ctor_properties (ctype, decl))
6514 return NULL_TREE;
6515
6516 if (ctype == NULL_TREE || check)
6517 return decl;
6518
6519 if (virtualp)
cbb40945 6520 DECL_VIRTUAL_P (decl) = 1;
74b846e0 6521
8d08fdba
MS
6522 return decl;
6523}
6524
4684cd27
MM
6525/* DECL is a VAR_DECL for a static data member. Set flags to reflect
6526 the linkage that DECL will receive in the object file. */
6527
6528static void
6529set_linkage_for_static_data_member (tree decl)
6530{
6531 /* A static data member always has static storage duration and
6532 external linkage. Note that static data members are forbidden in
6533 local classes -- the only situation in which a class has
6534 non-external linkage. */
6535 TREE_PUBLIC (decl) = 1;
6536 TREE_STATIC (decl) = 1;
6537 /* For non-template classes, static data members are always put
6538 out in exactly those files where they are defined, just as
77880ae4 6539 with ordinary namespace-scope variables. */
4684cd27
MM
6540 if (!processing_template_decl)
6541 DECL_INTERFACE_KNOWN (decl) = 1;
6542}
6543
caf93cb0 6544/* Create a VAR_DECL named NAME with the indicated TYPE.
9e259dd1
MM
6545
6546 If SCOPE is non-NULL, it is the class type or namespace containing
6547 the variable. If SCOPE is NULL, the variable should is created in
6548 the innermost enclosings scope. */
6549
8d08fdba 6550static tree
11f6b451 6551grokvardecl (tree type,
0cbd7506 6552 tree name,
1ff3c076 6553 const cp_decl_specifier_seq *declspecs,
0cbd7506
MS
6554 int initialized,
6555 int constp,
6556 tree scope)
8d08fdba
MS
6557{
6558 tree decl;
5ae9ba3e 6559 tree explicit_scope;
f7da6097 6560
50bc768d 6561 gcc_assert (!name || TREE_CODE (name) == IDENTIFIER_NODE);
9e259dd1 6562
5ae9ba3e
MM
6563 /* Compute the scope in which to place the variable, but remember
6564 whether or not that scope was explicitly specified by the user. */
6565 explicit_scope = scope;
9e259dd1 6566 if (!scope)
8d08fdba 6567 {
9e259dd1
MM
6568 /* An explicit "extern" specifier indicates a namespace-scope
6569 variable. */
62d1db17 6570 if (declspecs->storage_class == sc_extern)
9e259dd1
MM
6571 scope = current_namespace;
6572 else if (!at_function_scope_p ())
a5201a91 6573 scope = current_scope ();
9e259dd1
MM
6574 }
6575
6576 if (scope
6577 && (/* If the variable is a namespace-scope variable declared in a
6578 template, we need DECL_LANG_SPECIFIC. */
6579 (TREE_CODE (scope) == NAMESPACE_DECL && processing_template_decl)
6580 /* Similarly for namespace-scope variables with language linkage
6581 other than C++. */
caf93cb0 6582 || (TREE_CODE (scope) == NAMESPACE_DECL
9e259dd1
MM
6583 && current_lang_name != lang_name_cplusplus)
6584 /* Similarly for static data members. */
6585 || TYPE_P (scope)))
6586 decl = build_lang_decl (VAR_DECL, name, type);
8d08fdba 6587 else
9e259dd1 6588 decl = build_decl (VAR_DECL, name, type);
79c4d4b7 6589
5ae9ba3e
MM
6590 if (explicit_scope && TREE_CODE (explicit_scope) == NAMESPACE_DECL)
6591 set_decl_namespace (decl, explicit_scope, 0);
9e259dd1 6592 else
74b0d490 6593 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
6060a796 6594
62d1db17 6595 if (declspecs->storage_class == sc_extern)
8d08fdba
MS
6596 {
6597 DECL_THIS_EXTERN (decl) = 1;
6598 DECL_EXTERNAL (decl) = !initialized;
6599 }
6600
2b9dc906 6601 if (DECL_CLASS_SCOPE_P (decl))
8d08fdba 6602 {
4684cd27
MM
6603 set_linkage_for_static_data_member (decl);
6604 /* This function is only called with out-of-class definitions. */
5b605f68 6605 DECL_EXTERNAL (decl) = 0;
9c12301f 6606 check_class_member_definition_namespace (decl);
8d08fdba
MS
6607 }
6608 /* At top level, either `static' or no s.c. makes a definition
6609 (perhaps tentative), and absence of `static' makes it public. */
a9aedbc2 6610 else if (toplevel_bindings_p ())
8d08fdba 6611 {
62d1db17 6612 TREE_PUBLIC (decl) = (declspecs->storage_class != sc_static
9fffd093 6613 && (DECL_THIS_EXTERN (decl) || ! constp));
8d08fdba
MS
6614 TREE_STATIC (decl) = ! DECL_EXTERNAL (decl);
6615 }
6616 /* Not at top level, only `static' makes a static definition. */
6617 else
6618 {
62d1db17 6619 TREE_STATIC (decl) = declspecs->storage_class == sc_static;
8d08fdba
MS
6620 TREE_PUBLIC (decl) = DECL_EXTERNAL (decl);
6621 }
50a6dbd7 6622
62d1db17 6623 if (declspecs->specs[(int)ds_thread])
8893239d 6624 DECL_TLS_MODEL (decl) = decl_default_tls_model (decl);
7a1f3f5f 6625
50a6dbd7
JM
6626 if (TREE_PUBLIC (decl))
6627 {
6628 /* [basic.link]: A name with no linkage (notably, the name of a class
6629 or enumeration declared in a local scope) shall not be used to
6630 declare an entity with linkage.
6631
03fd3f84 6632 Only check this for public decls for now. */
6b211746 6633 tree t = no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/false);
50a6dbd7
JM
6634 if (t)
6635 {
1951a1b6 6636 if (TYPE_ANONYMOUS_P (t))
9fc444cc
GK
6637 {
6638 if (DECL_EXTERN_C_P (decl))
6bdb98d1
MA
6639 /* Allow this; it's pretty common in C. */
6640 ;
9fc444cc
GK
6641 else
6642 {
6b211746
AO
6643 /* DRs 132, 319 and 389 seem to indicate types with
6644 no linkage can only be used to declare extern "C"
6645 entities. Since it's not always an error in the
6646 ISO C++ 90 Standard, we only issue a warning. */
d4ee4d25 6647 warning (0, "non-local variable %q#D uses anonymous type",
9fc444cc
GK
6648 decl);
6649 if (DECL_ORIGINAL_TYPE (TYPE_NAME (t)))
dee15844
JM
6650 warning (0, "%q+#D does not refer to the unqualified "
6651 "type, so it is not used for linkage",
6652 TYPE_NAME (t));
9fc444cc
GK
6653 }
6654 }
50a6dbd7 6655 else
d4ee4d25 6656 warning (0, "non-local variable %q#D uses local type %qT", decl, t);
50a6dbd7
JM
6657 }
6658 }
1ceb02be
MM
6659 else
6660 DECL_INTERFACE_KNOWN (decl) = 1;
50a6dbd7 6661
8d08fdba
MS
6662 return decl;
6663}
6664
d8f8dca1
MM
6665/* Create and return a canonical pointer to member function type, for
6666 TYPE, which is a POINTER_TYPE to a METHOD_TYPE. */
8d08fdba
MS
6667
6668tree
11f6b451 6669build_ptrmemfunc_type (tree type)
8d08fdba 6670{
4977bab6 6671 tree field, fields;
8d08fdba 6672 tree t;
46cbda4a 6673 tree unqualified_variant = NULL_TREE;
8d08fdba 6674
d48ebde1
NS
6675 if (type == error_mark_node)
6676 return type;
d363e7bf 6677
8d08fdba
MS
6678 /* If a canonical type already exists for this type, use it. We use
6679 this method instead of type_hash_canon, because it only does a
6680 simple equality check on the list of field members. */
6681
6682 if ((t = TYPE_GET_PTRMEMFUNC_TYPE (type)))
6683 return t;
6684
46cbda4a
MM
6685 /* Make sure that we always have the unqualified pointer-to-member
6686 type first. */
89d684bb 6687 if (cp_type_quals (type) != TYPE_UNQUALIFIED)
68642fb6 6688 unqualified_variant
46cbda4a
MM
6689 = build_ptrmemfunc_type (TYPE_MAIN_VARIANT (type));
6690
33848bb0 6691 t = make_aggr_type (RECORD_TYPE);
cad7e87b 6692 xref_basetypes (t, NULL_TREE);
caf93cb0 6693
3b426391 6694 /* Let the front end know this is a pointer to member function... */
db5ae43f 6695 TYPE_PTRMEMFUNC_FLAG (t) = 1;
2c73f9f5 6696 /* ... and not really an aggregate. */
7ddedda4 6697 SET_IS_AGGR_TYPE (t, 0);
8d08fdba 6698
4977bab6
ZW
6699 field = build_decl (FIELD_DECL, pfn_identifier, type);
6700 fields = field;
caf93cb0 6701
4977bab6
ZW
6702 field = build_decl (FIELD_DECL, delta_identifier, delta_type_node);
6703 TREE_CHAIN (field) = fields;
6704 fields = field;
caf93cb0 6705
4977bab6 6706 finish_builtin_struct (t, "__ptrmemfunc_type", fields, ptr_type_node);
8d08fdba 6707
3b426391 6708 /* Zap out the name so that the back end will give us the debugging
8d08fdba
MS
6709 information for this anonymous RECORD_TYPE. */
6710 TYPE_NAME (t) = NULL_TREE;
6711
46cbda4a
MM
6712 /* If this is not the unqualified form of this pointer-to-member
6713 type, set the TYPE_MAIN_VARIANT for this type to be the
6714 unqualified type. Since they are actually RECORD_TYPEs that are
6715 not variants of each other, we must do this manually. */
89d684bb 6716 if (cp_type_quals (type) != TYPE_UNQUALIFIED)
46cbda4a 6717 {
89d684bb 6718 t = build_qualified_type (t, cp_type_quals (type));
46cbda4a
MM
6719 TYPE_MAIN_VARIANT (t) = unqualified_variant;
6720 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (unqualified_variant);
6721 TYPE_NEXT_VARIANT (unqualified_variant) = t;
6722 }
6723
6724 /* Cache this pointer-to-member type so that we can find it again
6725 later. */
8d08fdba
MS
6726 TYPE_SET_PTRMEMFUNC_TYPE (type, t);
6727
8de08f4c
DG
6728 /* Managing canonical types for the RECORD_TYPE behind a
6729 pointer-to-member function is a nightmare, so use structural
6730 equality for now. */
6731 SET_TYPE_STRUCTURAL_EQUALITY (t);
06d40de8 6732
8d08fdba
MS
6733 return t;
6734}
6735
9e259dd1
MM
6736/* Create and return a pointer to data member type. */
6737
6738tree
6739build_ptrmem_type (tree class_type, tree member_type)
6740{
a5ac359a
MM
6741 if (TREE_CODE (member_type) == METHOD_TYPE)
6742 {
6743 tree arg_types;
6744
6745 arg_types = TYPE_ARG_TYPES (member_type);
caf93cb0 6746 class_type = (cp_build_qualified_type
a5ac359a
MM
6747 (class_type,
6748 cp_type_quals (TREE_TYPE (TREE_VALUE (arg_types)))));
caf93cb0
EC
6749 member_type
6750 = build_method_type_directly (class_type,
43dc123f
MM
6751 TREE_TYPE (member_type),
6752 TREE_CHAIN (arg_types));
a5ac359a
MM
6753 return build_ptrmemfunc_type (build_pointer_type (member_type));
6754 }
6755 else
6756 {
50bc768d 6757 gcc_assert (TREE_CODE (member_type) != FUNCTION_TYPE);
a5ac359a
MM
6758 return build_offset_type (class_type, member_type);
6759 }
9e259dd1
MM
6760}
6761
b17e2870
JM
6762/* DECL is a VAR_DECL defined in-class, whose TYPE is also given.
6763 Check to see that the definition is valid. Issue appropriate error
6764 messages. Return 1 if the definition is particularly bad, or 0
6765 otherwise. */
6766
6767int
11f6b451 6768check_static_variable_definition (tree decl, tree type)
b17e2870
JM
6769{
6770 /* Motion 10 at San Diego: If a static const integral data member is
6771 initialized with an integral constant expression, the initializer
6772 may appear either in the declaration (within the class), or in
6773 the definition, but not both. If it appears in the class, the
6774 member is a member constant. The file-scope definition is always
6775 required. */
dcba9b0f 6776 if (!ARITHMETIC_TYPE_P (type) && TREE_CODE (type) != ENUMERAL_TYPE)
b17e2870 6777 {
2d01edd7 6778 error ("invalid in-class initialization of static data member "
0cbd7506 6779 "of non-integral type %qT",
dcba9b0f 6780 type);
b17e2870 6781 /* If we just return the declaration, crashes will sometimes
852dcbdd 6782 occur. We therefore return void_type_node, as if this were a
b17e2870
JM
6783 friend declaration, to cause callers to completely ignore
6784 this declaration. */
6785 return 1;
6786 }
6787 else if (!CP_TYPE_CONST_P (type))
2d01edd7 6788 error ("ISO C++ forbids in-class initialization of non-const "
0cbd7506
MS
6789 "static member %qD",
6790 decl);
b17e2870 6791 else if (pedantic && !INTEGRAL_TYPE_P (type))
2d01edd7 6792 pedwarn ("ISO C++ forbids initialization of member constant "
0cbd7506 6793 "%qD of non-integral type %qT", decl, type);
b17e2870
JM
6794
6795 return 0;
6796}
6797
2848ad0f
MM
6798/* Given the SIZE (i.e., number of elements) in an array, compute an
6799 appropriate index type for the array. If non-NULL, NAME is the
6800 name of the thing being declared. */
6801
c95cd22e 6802tree
11f6b451 6803compute_array_index_type (tree name, tree size)
2848ad0f 6804{
d174af6c 6805 tree type;
2848ad0f 6806 tree itype;
06d40de8 6807 tree abi_1_itype = NULL_TREE;
2848ad0f 6808
d174af6c
MM
6809 if (error_operand_p (size))
6810 return error_mark_node;
6811
6812 type = TREE_TYPE (size);
d63b5e9f
NS
6813 /* The array bound must be an integer type. */
6814 if (!dependent_type_p (type) && !INTEGRAL_TYPE_P (type))
2848ad0f 6815 {
d63b5e9f 6816 if (name)
2d01edd7 6817 error ("size of array %qD has non-integral type %qT", name, type);
d63b5e9f 6818 else
2d01edd7 6819 error ("size of array has non-integral type %qT", type);
d63b5e9f
NS
6820 size = integer_one_node;
6821 type = TREE_TYPE (size);
2848ad0f
MM
6822 }
6823
06d40de8
DG
6824 if (value_dependent_expression_p (size))
6825 {
6826 /* We cannot do any checking for a value-dependent SIZE. Just
6827 build the index type and mark that it requires structural
6828 equality checks. */
6829 itype = build_index_type (build_min (MINUS_EXPR, sizetype,
6830 size, integer_one_node));
6831 SET_TYPE_STRUCTURAL_EQUALITY (itype);
6832 return itype;
6833 }
6834
6835 if (!abi_version_at_least (2) && processing_template_decl)
6836 /* For abi-1, we handled all instances in templates the same way,
6837 even when they were non-dependent. This effects the manglings
6838 produced. So, we do the normal checking for non-dependent
6839 sizes, but at the end we'll return the same type that abi-1
6840 would have, but with TYPE_CANONICAL set to the "right"
6841 value that the current ABI would provide. */
6842 abi_1_itype = build_index_type (build_min (MINUS_EXPR, sizetype,
6843 size, integer_one_node));
d63b5e9f 6844
a1c65f9f 6845 /* The size might be the result of a cast. */
80f5bb34
MM
6846 STRIP_TYPE_NOPS (size);
6847
6848 /* It might be a const variable or enumeration constant. */
8a784e4a 6849 size = integral_constant_value (size);
80f5bb34 6850
2848ad0f 6851 /* Normally, the array-bound will be a constant. */
2bb5d995 6852 if (TREE_CODE (size) == INTEGER_CST)
2848ad0f
MM
6853 {
6854 /* Check to see if the array bound overflowed. Make that an
6855 error, no matter how generous we're being. */
6856 int old_flag_pedantic_errors = flag_pedantic_errors;
6857 int old_pedantic = pedantic;
6858 pedantic = flag_pedantic_errors = 1;
6859 constant_expression_warning (size);
6860 pedantic = old_pedantic;
6861 flag_pedantic_errors = old_flag_pedantic_errors;
6862
6863 /* An array must have a positive number of elements. */
6864 if (INT_CST_LT (size, integer_zero_node))
6865 {
d67cdbc3 6866 if (name)
2d01edd7 6867 error ("size of array %qD is negative", name);
d67cdbc3 6868 else
33bd39a2 6869 error ("size of array is negative");
2848ad0f
MM
6870 size = integer_one_node;
6871 }
a8e6c82a
MM
6872 /* As an extension we allow zero-sized arrays. We always allow
6873 them in system headers because glibc uses them. */
2848ad0f 6874 else if (integer_zerop (size) && pedantic && !in_system_header)
d67cdbc3
JM
6875 {
6876 if (name)
2d01edd7 6877 pedwarn ("ISO C++ forbids zero-size array %qD", name);
d67cdbc3 6878 else
33bd39a2 6879 pedwarn ("ISO C++ forbids zero-size array");
d67cdbc3 6880 }
2848ad0f 6881 }
2bb5d995
JM
6882 else if (TREE_CONSTANT (size))
6883 {
6884 /* `(int) &fn' is not a valid array bound. */
6885 if (name)
2d01edd7 6886 error ("size of array %qD is not an integral constant-expression",
0cbd7506 6887 name);
2bb5d995 6888 else
33bd39a2 6889 error ("size of array is not an integral constant-expression");
3aa2ddb8 6890 size = integer_one_node;
2bb5d995 6891 }
50d50fc7 6892 else if (pedantic && warn_vla != 0)
d63b5e9f
NS
6893 {
6894 if (name)
50d50fc7 6895 pedwarn ("ISO C++ forbids variable length array %qD", name);
d63b5e9f 6896 else
50d50fc7
SP
6897 pedwarn ("ISO C++ forbids variable length array");
6898 }
6899 else if (warn_vla > 0)
6900 {
6901 if (name)
6902 warning (OPT_Wvla,
6903 "variable length array %qD is used", name);
6904 else
6905 warning (OPT_Wvla,
6906 "variable length array is used");
d63b5e9f 6907 }
2848ad0f 6908
d63b5e9f
NS
6909 if (processing_template_decl && !TREE_CONSTANT (size))
6910 /* A variable sized array. */
6911 itype = build_min (MINUS_EXPR, sizetype, size, integer_one_node);
6912 else
2848ad0f 6913 {
455f19cb
MM
6914 HOST_WIDE_INT saved_processing_template_decl;
6915
d63b5e9f 6916 /* Compute the index of the largest element in the array. It is
0cbd7506
MS
6917 one less than the number of elements in the array. We save
6918 and restore PROCESSING_TEMPLATE_DECL so that computations in
6919 cp_build_binary_op will be appropriately folded. */
455f19cb
MM
6920 saved_processing_template_decl = processing_template_decl;
6921 processing_template_decl = 0;
6922 itype = cp_build_binary_op (MINUS_EXPR,
6923 cp_convert (ssizetype, size),
6924 cp_convert (ssizetype, integer_one_node));
6925 itype = fold (itype);
6926 processing_template_decl = saved_processing_template_decl;
6927
d63b5e9f 6928 if (!TREE_CONSTANT (itype))
f4f206f4 6929 /* A variable sized array. */
d63b5e9f
NS
6930 itype = variable_size (itype);
6931 /* Make sure that there was no overflow when creating to a signed
0cbd7506
MS
6932 index type. (For example, on a 32-bit machine, an array with
6933 size 2^32 - 1 is too big.) */
9116d529
RS
6934 else if (TREE_CODE (itype) == INTEGER_CST
6935 && TREE_OVERFLOW (itype))
2848ad0f 6936 {
d63b5e9f
NS
6937 error ("overflow in array dimension");
6938 TREE_OVERFLOW (itype) = 0;
2848ad0f 6939 }
2848ad0f 6940 }
68642fb6 6941
2848ad0f 6942 /* Create and return the appropriate index type. */
06d40de8
DG
6943 if (abi_1_itype)
6944 {
6945 tree t = build_index_type (itype);
6946 TYPE_CANONICAL (abi_1_itype) = TYPE_CANONICAL (t);
6947 return abi_1_itype;
6948 }
6949 else
6950 return build_index_type (itype);
2848ad0f
MM
6951}
6952
a723baf1
MM
6953/* Returns the scope (if any) in which the entity declared by
6954 DECLARATOR will be located. If the entity was declared with an
6955 unqualified name, NULL_TREE is returned. */
6956
6957tree
058b15c1 6958get_scope_of_declarator (const cp_declarator *declarator)
a723baf1 6959{
058b15c1
MM
6960 while (declarator && declarator->kind != cdk_id)
6961 declarator = declarator->declarator;
6962
6963 /* If the declarator-id is a SCOPE_REF, the scope in which the
6964 declaration occurs is the first operand. */
6965 if (declarator
1d786913
MM
6966 && declarator->u.id.qualifying_scope)
6967 return declarator->u.id.qualifying_scope;
058b15c1 6968
77880ae4 6969 /* Otherwise, the declarator is not a qualified name; the entity will
058b15c1
MM
6970 be declared in the current scope. */
6971 return NULL_TREE;
a723baf1
MM
6972}
6973
2848ad0f
MM
6974/* Returns an ARRAY_TYPE for an array with SIZE elements of the
6975 indicated TYPE. If non-NULL, NAME is the NAME of the declaration
6976 with this type. */
6977
6978static tree
11f6b451 6979create_array_type_for_decl (tree name, tree type, tree size)
2848ad0f
MM
6980{
6981 tree itype = NULL_TREE;
6982 const char* error_msg;
6983
6984 /* If things have already gone awry, bail now. */
6985 if (type == error_mark_node || size == error_mark_node)
6986 return error_mark_node;
6987
6988 /* Assume that everything will go OK. */
6989 error_msg = NULL;
6990
6991 /* There are some types which cannot be array elements. */
6992 switch (TREE_CODE (type))
6993 {
6994 case VOID_TYPE:
6995 error_msg = "array of void";
6996 break;
6997
6998 case FUNCTION_TYPE:
6999 error_msg = "array of functions";
7000 break;
7001
7002 case REFERENCE_TYPE:
7003 error_msg = "array of references";
7004 break;
7005
2848ad0f
MM
7006 case METHOD_TYPE:
7007 error_msg = "array of function members";
7008 break;
7009
7010 default:
7011 break;
7012 }
7013
7014 /* If something went wrong, issue an error-message and return. */
7015 if (error_msg)
7016 {
7017 if (name)
2d01edd7 7018 error ("declaration of %qD as %s", name, error_msg);
2848ad0f 7019 else
33bd39a2 7020 error ("creating %s", error_msg);
2848ad0f
MM
7021
7022 return error_mark_node;
7023 }
7024
7025 /* [dcl.array]
68642fb6 7026
2848ad0f
MM
7027 The constant expressions that specify the bounds of the arrays
7028 can be omitted only for the first member of the sequence. */
7029 if (TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type))
7030 {
b3faacfd 7031 if (name)
2d01edd7 7032 error ("declaration of %qD as multidimensional array must "
0cbd7506
MS
7033 "have bounds for all dimensions except the first",
7034 name);
b3faacfd 7035 else
2d01edd7 7036 error ("multidimensional array must have bounds for all "
0cbd7506 7037 "dimensions except the first");
2848ad0f
MM
7038
7039 return error_mark_node;
7040 }
7041
7042 /* Figure out the index type for the array. */
7043 if (size)
7044 itype = compute_array_index_type (name, size);
7045
7fb213d8
GB
7046 /* [dcl.array]
7047 T is called the array element type; this type shall not be [...] an
7048 abstract class type. */
7049 abstract_virtuals_error (name, type);
7050
2848ad0f
MM
7051 return build_cplus_array_type (type, itype);
7052}
7053
3dbc07b6
MM
7054/* Check that it's OK to declare a function with the indicated TYPE.
7055 SFK indicates the kind of special function (if any) that this
1f84ec23 7056 function is. OPTYPE is the type given in a conversion operator
44d10c10
PB
7057 declaration, or the class type for a constructor/destructor.
7058 Returns the actual return type of the function; that
3dbc07b6
MM
7059 may be different than TYPE if an error occurs, or for certain
7060 special functions. */
7061
7062static tree
11f6b451 7063check_special_function_return_type (special_function_kind sfk,
0cbd7506
MS
7064 tree type,
7065 tree optype)
3dbc07b6
MM
7066{
7067 switch (sfk)
7068 {
7069 case sfk_constructor:
7070 if (type)
33bd39a2 7071 error ("return type specification for constructor invalid");
5362b086 7072
44d10c10
PB
7073 if (targetm.cxx.cdtor_returns_this () && !TYPE_FOR_JAVA (optype))
7074 type = build_pointer_type (optype);
7075 else
7076 type = void_type_node;
3dbc07b6
MM
7077 break;
7078
7079 case sfk_destructor:
7080 if (type)
33bd39a2 7081 error ("return type specification for destructor invalid");
44d10c10 7082 /* We can't use the proper return type here because we run into
77880ae4 7083 problems with ambiguous bases and covariant returns.
44d10c10
PB
7084 Java classes are left unchanged because (void *) isn't a valid
7085 Java type, and we don't want to change the Java ABI. */
7086 if (targetm.cxx.cdtor_returns_this () && !TYPE_FOR_JAVA (optype))
7087 type = build_pointer_type (void_type_node);
7088 else
7089 type = void_type_node;
3dbc07b6
MM
7090 break;
7091
7092 case sfk_conversion:
caba2081
PC
7093 if (type)
7094 error ("return type specified for %<operator %T%>", optype);
3dbc07b6
MM
7095 type = optype;
7096 break;
7097
7098 default:
8dc2b103 7099 gcc_unreachable ();
3dbc07b6
MM
7100 }
7101
7102 return type;
7103}
7104
62e19030
MM
7105/* A variable or data member (whose unqualified name is IDENTIFIER)
7106 has been declared with the indicated TYPE. If the TYPE is not
7107 acceptable, issue an error message and return a type to use for
03fd3f84 7108 error-recovery purposes. */
62e19030
MM
7109
7110tree
7111check_var_type (tree identifier, tree type)
7112{
7113 if (VOID_TYPE_P (type))
7114 {
7115 if (!identifier)
7116 error ("unnamed variable or field declared void");
7117 else if (TREE_CODE (identifier) == IDENTIFIER_NODE)
7118 {
7119 gcc_assert (!IDENTIFIER_OPNAME_P (identifier));
7120 error ("variable or field %qE declared void", identifier);
7121 }
7122 else
7123 error ("variable or field declared void");
650fcd07 7124 type = error_mark_node;
62e19030 7125 }
c8094d83 7126
62e19030
MM
7127 return type;
7128}
7129
a723baf1
MM
7130/* Given declspecs and a declarator (abstract or otherwise), determine
7131 the name and type of the object declared and construct a DECL node
7132 for it.
8d08fdba 7133
5e32a5cf
GDR
7134 DECLSPECS points to the representation of declaration-specifier
7135 sequence that precedes declarator.
8d08fdba
MS
7136
7137 DECL_CONTEXT says which syntactic context this declaration is in:
7138 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
7139 FUNCDEF for a function definition. Like NORMAL but a few different
7140 error messages in each case. Return value may be zero meaning
7141 this definition is too screwy to try to parse.
7142 MEMFUNCDEF for a function definition. Like FUNCDEF but prepares to
7143 handle member functions (which have FIELD context).
7144 Return value may be zero meaning this definition is too screwy to
7145 try to parse.
7146 PARM for a parameter declaration (either within a function prototype
7147 or before a function body). Make a PARM_DECL, or return void_type_node.
db5ae43f 7148 CATCHPARM for a parameter declaration before a catch clause.
8d08fdba
MS
7149 TYPENAME if for a typename (in a cast or sizeof).
7150 Don't make a DECL node; just return the ..._TYPE node.
7151 FIELD for a struct or union field; make a FIELD_DECL.
7152 BITFIELD for a field with specified width.
7153 INITIALIZED is 1 if the decl has an initializer.
7154
91d231cb
JM
7155 ATTRLIST is a pointer to the list of attributes, which may be NULL
7156 if there are none; *ATTRLIST may be modified if attributes from inside
7157 the declarator should be applied to the declaration.
b17e2870 7158
a723baf1
MM
7159 When this function is called, scoping variables (such as
7160 CURRENT_CLASS_TYPE) should reflect the scope in which the
7161 declaration occurs, not the scope in which the new declaration will
7162 be placed. For example, on:
8d08fdba 7163
a723baf1 7164 void S::f() { ... }
8d08fdba 7165
a723baf1 7166 when grokdeclarator is called for `S::f', the CURRENT_CLASS_TYPE
344f237b
LM
7167 should not be `S'.
7168
7169 Returns a DECL (if a declarator is present), a TYPE (if there is no
7170 declarator, in cases like "struct S;"), or the ERROR_MARK_NODE if an
7171 error occurs. */
8d08fdba 7172
8d08fdba 7173tree
058b15c1 7174grokdeclarator (const cp_declarator *declarator,
1ff3c076 7175 const cp_decl_specifier_seq *declspecs,
0cbd7506
MS
7176 enum decl_context decl_context,
7177 int initialized,
7178 tree* attrlist)
8d08fdba 7179{
8d08fdba
MS
7180 tree type = NULL_TREE;
7181 int longlong = 0;
db5ae43f 7182 int virtualp, explicitp, friendp, inlinep, staticp;
8d08fdba
MS
7183 int explicit_int = 0;
7184 int explicit_char = 0;
37c46b43 7185 int defaulted_int = 0;
d1c78882 7186 tree dependent_name = NULL_TREE;
caf93cb0 7187
8d08fdba 7188 tree typedef_decl = NULL_TREE;
058b15c1 7189 const char *name = NULL;
8d08fdba 7190 tree typedef_type = NULL_TREE;
357d956e
MM
7191 /* True if this declarator is a function definition. */
7192 bool funcdef_flag = false;
058b15c1 7193 cp_declarator_kind innermost_code = cdk_error;
8d08fdba 7194 int bitfield = 0;
6125f3be
DE
7195#if 0
7196 /* See the code below that used this. */
91d231cb 7197 tree decl_attr = NULL_TREE;
6125f3be 7198#endif
8d08fdba
MS
7199
7200 /* Keep track of what sort of function is being processed
7201 so that we can warn about default return values, or explicit
7202 return values which do not match prescribed defaults. */
3dbc07b6 7203 special_function_kind sfk = sfk_none;
8d08fdba
MS
7204
7205 tree dname = NULL_TREE;
8d08fdba
MS
7206 tree ctor_return_type = NULL_TREE;
7207 enum overload_flags flags = NO_SPECIAL;
e2537f2c
MM
7208 /* cv-qualifiers that apply to the declarator, for a declaration of
7209 a member function. */
7210 cp_cv_quals memfn_quals = TYPE_UNQUALIFIED;
7211 /* cv-qualifiers that apply to the type specified by the DECLSPECS. */
7212 int type_quals;
c11b6f21 7213 tree raises = NULL_TREE;
386b8a85 7214 int template_count = 0;
91d231cb 7215 tree returned_attrs = NULL_TREE;
4546865e 7216 tree parms = NULL_TREE;
058b15c1
MM
7217 const cp_declarator *id_declarator;
7218 /* The unqualified name of the declarator; either an
7219 IDENTIFIER_NODE, BIT_NOT_EXPR, or TEMPLATE_ID_EXPR. */
7220 tree unqualified_id;
7221 /* The class type, if any, in which this entity is located,
7222 or NULL_TREE if none. Note that this value may be different from
7223 the current class type; for example if an attempt is made to declare
7224 "A::f" inside "B", this value will be "A". */
7225 tree ctype = current_class_type;
7226 /* The NAMESPACE_DECL for the namespace in which this entity is
7227 located. If an unqualified name is used to declare the entity,
7228 this value will be NULL_TREE, even if the entity is located at
caf93cb0 7229 namespace scope. */
058b15c1 7230 tree in_namespace = NULL_TREE;
1ff3c076
MM
7231 cp_storage_class storage_class;
7232 bool unsigned_p, signed_p, short_p, long_p, thread_p;
2cfe82fe 7233 bool type_was_error_mark_node = false;
5d80a306 7234 bool parameter_pack_p = declarator? declarator->parameter_pack_p : false;
1891dec4 7235 bool set_no_warning = false;
1ff3c076
MM
7236
7237 signed_p = declspecs->specs[(int)ds_signed];
7238 unsigned_p = declspecs->specs[(int)ds_unsigned];
7239 short_p = declspecs->specs[(int)ds_short];
7240 long_p = declspecs->specs[(int)ds_long];
28c84d63 7241 longlong = declspecs->specs[(int)ds_long] >= 2;
1ff3c076 7242 thread_p = declspecs->specs[(int)ds_thread];
8d08fdba 7243
8d08fdba 7244 if (decl_context == FUNCDEF)
357d956e 7245 funcdef_flag = true, decl_context = NORMAL;
8d08fdba 7246 else if (decl_context == MEMFUNCDEF)
357d956e 7247 funcdef_flag = true, decl_context = FIELD;
8d08fdba
MS
7248 else if (decl_context == BITFIELD)
7249 bitfield = 1, decl_context = FIELD;
7250
8d08fdba
MS
7251 /* Look inside a declarator for the name being declared
7252 and get it as a string, for an error message. */
caf93cb0
EC
7253 for (id_declarator = declarator;
7254 id_declarator;
058b15c1
MM
7255 id_declarator = id_declarator->declarator)
7256 {
7257 if (id_declarator->kind != cdk_id)
7258 innermost_code = id_declarator->kind;
8d08fdba 7259
058b15c1
MM
7260 switch (id_declarator->kind)
7261 {
7262 case cdk_function:
7263 if (id_declarator->declarator
7264 && id_declarator->declarator->kind == cdk_id)
8d08fdba 7265 {
058b15c1
MM
7266 sfk = id_declarator->declarator->u.id.sfk;
7267 if (sfk == sfk_destructor)
7268 flags = DTOR_FLAG;
51c184be 7269 }
058b15c1 7270 break;
be99da77 7271
058b15c1
MM
7272 case cdk_id:
7273 {
1d786913
MM
7274 tree qualifying_scope = id_declarator->u.id.qualifying_scope;
7275 tree decl = id_declarator->u.id.unqualified_name;
058b15c1
MM
7276 if (!decl)
7277 break;
1d786913 7278 if (qualifying_scope)
8d08fdba 7279 {
2884e22c
MM
7280 if (at_function_scope_p ())
7281 {
7282 /* [dcl.meaning]
7283
7284 A declarator-id shall not be qualified except
7285 for ...
7286
7287 None of the cases are permitted in block
7288 scope. */
7289 if (qualifying_scope == global_namespace)
7290 error ("invalid use of qualified-name %<::%D%>",
7291 decl);
7292 else if (TYPE_P (qualifying_scope))
7293 error ("invalid use of qualified-name %<%T::%D%>",
7294 qualifying_scope, decl);
7295 else
7296 error ("invalid use of qualified-name %<%D::%D%>",
7297 qualifying_scope, decl);
7298 return error_mark_node;
7299 }
7300 else if (TYPE_P (qualifying_scope))
058b15c1 7301 {
1d786913 7302 ctype = qualifying_scope;
058b15c1
MM
7303 if (innermost_code != cdk_function
7304 && current_class_type
caf93cb0 7305 && !UNIQUELY_DERIVED_FROM_P (ctype,
058b15c1
MM
7306 current_class_type))
7307 {
2d01edd7 7308 error ("type %qT is not derived from type %qT",
058b15c1 7309 ctype, current_class_type);
1d786913 7310 return error_mark_node;
058b15c1 7311 }
058b15c1
MM
7312 }
7313 else if (TREE_CODE (qualifying_scope) == NAMESPACE_DECL)
7314 in_namespace = qualifying_scope;
058b15c1 7315 }
058b15c1
MM
7316 switch (TREE_CODE (decl))
7317 {
7318 case BIT_NOT_EXPR:
be99da77 7319 {
88e95ee3
MM
7320 tree type;
7321
7322 if (innermost_code != cdk_function)
7323 {
7324 error ("declaration of %qD as non-function", decl);
7325 return error_mark_node;
7326 }
c8094d83 7327 else if (!qualifying_scope
88e95ee3
MM
7328 && !(current_class_type && at_class_scope_p ()))
7329 {
7330 error ("declaration of %qD as non-member", decl);
7331 return error_mark_node;
7332 }
c8094d83 7333
88e95ee3
MM
7334 type = TREE_OPERAND (decl, 0);
7335 name = IDENTIFIER_POINTER (constructor_name (type));
af02935e 7336 dname = decl;
058b15c1
MM
7337 }
7338 break;
633221db 7339
058b15c1
MM
7340 case TEMPLATE_ID_EXPR:
7341 {
7342 tree fns = TREE_OPERAND (decl, 0);
633221db 7343
058b15c1 7344 dname = fns;
058b15c1 7345 if (TREE_CODE (dname) != IDENTIFIER_NODE)
be99da77 7346 {
50bc768d 7347 gcc_assert (is_overloaded_fn (dname));
058b15c1 7348 dname = DECL_NAME (get_first_fn (dname));
be99da77
MS
7349 }
7350 }
058b15c1 7351 /* Fall through. */
be99da77 7352
058b15c1
MM
7353 case IDENTIFIER_NODE:
7354 if (TREE_CODE (decl) == IDENTIFIER_NODE)
7355 dname = decl;
be99da77 7356
058b15c1
MM
7357 if (C_IS_RESERVED_WORD (dname))
7358 {
2d01edd7 7359 error ("declarator-id missing; using reserved word %qD",
058b15c1
MM
7360 dname);
7361 name = IDENTIFIER_POINTER (dname);
7362 }
7363 else if (!IDENTIFIER_TYPENAME_P (dname))
7364 name = IDENTIFIER_POINTER (dname);
7365 else
7366 {
50bc768d 7367 gcc_assert (flags == NO_SPECIAL);
058b15c1
MM
7368 flags = TYPENAME_FLAG;
7369 ctor_return_type = TREE_TYPE (dname);
7370 sfk = sfk_conversion;
7371 if (is_typename_at_global_scope (dname))
7372 name = IDENTIFIER_POINTER (dname);
7373 else
7374 name = "<invalid operator>";
7375 }
7376 break;
7377
058b15c1 7378 default:
8dc2b103 7379 gcc_unreachable ();
058b15c1 7380 }
45537677 7381 break;
b08f991d 7382 }
45537677 7383
b08f991d
SB
7384 case cdk_array:
7385 case cdk_pointer:
7386 case cdk_reference:
7387 case cdk_ptrmem:
7388 break;
2ee366b5 7389
b08f991d
SB
7390 case cdk_error:
7391 return error_mark_node;
058b15c1 7392
b08f991d
SB
7393 default:
7394 gcc_unreachable ();
058b15c1
MM
7395 }
7396 if (id_declarator->kind == cdk_id)
7397 break;
7398 }
8d08fdba 7399
fa6098f8 7400 /* [dcl.fct.edf]
3db45ab5 7401
fa6098f8
MM
7402 The declarator in a function-definition shall have the form
7403 D1 ( parameter-declaration-clause) ... */
058b15c1 7404 if (funcdef_flag && innermost_code != cdk_function)
fa6098f8
MM
7405 {
7406 error ("function definition does not declare parameters");
7407 return error_mark_node;
7408 }
8d08fdba 7409
e1cd6e56 7410 if (((dname && IDENTIFIER_OPNAME_P (dname)) || flags == TYPENAME_FLAG)
058b15c1 7411 && innermost_code != cdk_function
62d1db17 7412 && ! (ctype && !declspecs->any_specifiers_p))
e1cd6e56 7413 {
2d01edd7 7414 error ("declaration of %qD as non-function", dname);
943e3ede 7415 return error_mark_node;
e1cd6e56
MS
7416 }
7417
8d08fdba
MS
7418 /* Anything declared one level down from the top level
7419 must be one of the parameters of a function
7420 (because the body is at least two levels down). */
7421
7422 /* This heuristic cannot be applied to C++ nodes! Fixed, however,
7423 by not allowing C++ class definitions to specify their parameters
7424 with xdecls (must be spec.d in the parmlist).
7425
7426 Since we now wait to push a class scope until we are sure that
7427 we are in a legitimate method context, we must set oldcname
a9aedbc2
MS
7428 explicitly (since current_class_name is not yet alive).
7429
7430 We also want to avoid calling this a PARM if it is in a namespace. */
8d08fdba 7431
9188c363 7432 if (decl_context == NORMAL && !toplevel_bindings_p ())
a9aedbc2 7433 {
e2500fed 7434 struct cp_binding_level *b = current_binding_level;
ff955512 7435 current_binding_level = b->level_chain;
a9aedbc2
MS
7436 if (current_binding_level != 0 && toplevel_bindings_p ())
7437 decl_context = PARM;
ff955512 7438 current_binding_level = b;
a9aedbc2 7439 }
8d08fdba 7440
34fc957d
NS
7441 if (name == NULL)
7442 name = decl_context == PARM ? "parameter" : "type name";
68642fb6 7443
62d1db17
MM
7444 /* If there were multiple types specified in the decl-specifier-seq,
7445 issue an error message. */
7446 if (declspecs->multiple_types_p)
32273f9f
LM
7447 {
7448 error ("two or more data types in declaration of %qs", name);
7449 return error_mark_node;
7450 }
7451
62d1db17
MM
7452 /* Extract the basic type from the decl-specifier-seq. */
7453 type = declspecs->type;
7454 if (type == error_mark_node)
2cfe82fe
ZW
7455 {
7456 type = NULL_TREE;
7457 type_was_error_mark_node = true;
7458 }
62d1db17
MM
7459 /* If the entire declaration is itself tagged as deprecated then
7460 suppress reports of deprecated items. */
7461 if (type && TREE_DEPRECATED (type)
7462 && deprecated_state != DEPRECATED_SUPPRESS)
7463 warn_deprecated_use (type);
7464 if (type && TREE_CODE (type) == TYPE_DECL)
8d08fdba 7465 {
62d1db17
MM
7466 typedef_decl = type;
7467 type = TREE_TYPE (typedef_decl);
7468 }
7469 /* No type at all: default to `int', and set DEFAULTED_INT
7470 because it was not a user-defined typedef. */
1ff3c076 7471 if (type == NULL_TREE && (signed_p || unsigned_p || long_p || short_p))
62d1db17
MM
7472 {
7473 /* These imply 'int'. */
7474 type = integer_type_node;
7475 defaulted_int = 1;
7476 }
7477 /* Gather flags. */
7478 explicit_int = declspecs->explicit_int_p;
7479 explicit_char = declspecs->explicit_char_p;
7a1f3f5f 7480
8d6e459d
NS
7481#if 0
7482 /* See the code below that used this. */
7483 if (typedef_decl)
7484 decl_attr = DECL_ATTRIBUTES (typedef_decl);
7485#endif
8d08fdba
MS
7486 typedef_type = type;
7487
a3203465 7488
44d10c10
PB
7489 if (sfk != sfk_conversion)
7490 ctor_return_type = ctype;
7491
3dbc07b6
MM
7492 if (sfk != sfk_none)
7493 type = check_special_function_return_type (sfk, type,
3dbc07b6
MM
7494 ctor_return_type);
7495 else if (type == NULL_TREE)
8d08fdba 7496 {
3dbc07b6
MM
7497 int is_main;
7498
8d08fdba 7499 explicit_int = -1;
3dbc07b6 7500
0fd0b7ee
JM
7501 /* We handle `main' specially here, because 'main () { }' is so
7502 common. With no options, it is allowed. With -Wreturn-type,
7503 it is a warning. It is only an error with -pedantic-errors. */
3dbc07b6 7504 is_main = (funcdef_flag
7437519c 7505 && dname && MAIN_NAME_P (dname)
3dbc07b6
MM
7506 && ctype == NULL_TREE
7507 && in_namespace == NULL_TREE
7508 && current_namespace == global_namespace);
7509
2cfe82fe
ZW
7510 if (type_was_error_mark_node)
7511 /* We've already issued an error, don't complain more. */;
7512 else if (in_system_header || flag_ms_extensions)
3dbc07b6
MM
7513 /* Allow it, sigh. */;
7514 else if (pedantic || ! is_main)
2d01edd7 7515 pedwarn ("ISO C++ forbids declaration of %qs with no type", name);
b10fc6f5
GDR
7516 else
7517 warning (OPT_Wreturn_type,
7518 "ISO C++ forbids declaration of %qs with no type", name);
41eff652 7519
3dbc07b6 7520 type = integer_type_node;
51c184be 7521 }
caf93cb0 7522
8d08fdba
MS
7523 ctype = NULL_TREE;
7524
7525 /* Now process the modifiers that were specified
7526 and check for invalid combinations. */
7527
7528 /* Long double is a special combination. */
3cc189f5 7529 if (long_p && !longlong && TYPE_MAIN_VARIANT (type) == double_type_node)
8d08fdba 7530 {
1ff3c076 7531 long_p = false;
68642fb6 7532 type = build_qualified_type (long_double_type_node,
89d684bb 7533 cp_type_quals (type));
8d08fdba
MS
7534 }
7535
7536 /* Check all other uses of type modifiers. */
7537
1ff3c076 7538 if (unsigned_p || signed_p || long_p || short_p)
8d08fdba
MS
7539 {
7540 int ok = 0;
7541
3cc189f5
VR
7542 if ((signed_p || unsigned_p) && TREE_CODE (type) != INTEGER_TYPE)
7543 error ("%<signed%> or %<unsigned%> invalid for %qs", name);
1ff3c076 7544 else if (signed_p && unsigned_p)
3cc189f5
VR
7545 error ("%<signed%> and %<unsigned%> specified together for %qs", name);
7546 else if (longlong && TREE_CODE (type) != INTEGER_TYPE)
7547 error ("%<long long%> invalid for %qs", name);
7548 else if (long_p && TREE_CODE (type) == REAL_TYPE)
7549 error ("%<long%> invalid for %qs", name);
7550 else if (short_p && TREE_CODE (type) == REAL_TYPE)
7551 error ("%<short%> invalid for %qs", name);
5d48268f
VR
7552 else if ((long_p || short_p) && TREE_CODE (type) != INTEGER_TYPE)
7553 error ("%<long%> or %<short%> invalid for %qs", name);
3cc189f5
VR
7554 else if ((long_p || short_p) && explicit_char)
7555 error ("%<long%> or %<short%> specified with char for %qs", name);
7556 else if (long_p && short_p)
7557 error ("%<long%> and %<short%> specified together for %qs", name);
8d08fdba
MS
7558 else
7559 {
7560 ok = 1;
37c46b43 7561 if (!explicit_int && !defaulted_int && !explicit_char && pedantic)
8d08fdba 7562 {
2d01edd7 7563 pedwarn ("long, short, signed or unsigned used invalidly for %qs",
8d08fdba
MS
7564 name);
7565 if (flag_pedantic_errors)
7566 ok = 0;
7567 }
7568 }
7569
7570 /* Discard the type modifiers if they are invalid. */
7571 if (! ok)
7572 {
1ff3c076
MM
7573 unsigned_p = false;
7574 signed_p = false;
7575 long_p = false;
7576 short_p = false;
8d08fdba
MS
7577 longlong = 0;
7578 }
7579 }
7580
7581 /* Decide whether an integer type is signed or not.
7582 Optionally treat bitfields as signed by default. */
1ff3c076 7583 if (unsigned_p
b89c5a7b
MM
7584 /* [class.bit]
7585
7586 It is implementation-defined whether a plain (neither
7587 explicitly signed or unsigned) char, short, int, or long
7588 bit-field is signed or unsigned.
68642fb6 7589
b89c5a7b
MM
7590 Naturally, we extend this to long long as well. Note that
7591 this does not include wchar_t. */
7592 || (bitfield && !flag_signed_bitfields
1ff3c076 7593 && !signed_p
b89c5a7b
MM
7594 /* A typedef for plain `int' without `signed' can be
7595 controlled just like plain `int', but a typedef for
7596 `signed int' cannot be so controlled. */
68642fb6 7597 && !(typedef_decl
29bbeb1c 7598 && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl))
b38a05d0 7599 && TREE_CODE (type) == INTEGER_TYPE
29bbeb1c 7600 && !same_type_p (TYPE_MAIN_VARIANT (type), wchar_type_node)))
8d08fdba
MS
7601 {
7602 if (longlong)
7603 type = long_long_unsigned_type_node;
1ff3c076 7604 else if (long_p)
8d08fdba 7605 type = long_unsigned_type_node;
1ff3c076 7606 else if (short_p)
8d08fdba
MS
7607 type = short_unsigned_type_node;
7608 else if (type == char_type_node)
7609 type = unsigned_char_type_node;
7610 else if (typedef_decl)
ca5ba2a3 7611 type = unsigned_type_for (type);
8d08fdba
MS
7612 else
7613 type = unsigned_type_node;
7614 }
1ff3c076 7615 else if (signed_p && type == char_type_node)
8d08fdba
MS
7616 type = signed_char_type_node;
7617 else if (longlong)
7618 type = long_long_integer_type_node;
1ff3c076 7619 else if (long_p)
8d08fdba 7620 type = long_integer_type_node;
1ff3c076 7621 else if (short_p)
8d08fdba
MS
7622 type = short_integer_type_node;
7623
62d1db17 7624 if (declspecs->specs[(int)ds_complex])
37c46b43 7625 {
1ff3c076 7626 if (TREE_CODE (type) != INTEGER_TYPE && TREE_CODE (type) != REAL_TYPE)
2d01edd7 7627 error ("complex invalid for %qs", name);
37c46b43
MS
7628 /* If we just have "complex", it is equivalent to
7629 "complex double", but if any modifiers at all are specified it is
7630 the complex form of TYPE. E.g, "complex short" is
7631 "complex short int". */
7632
1ff3c076
MM
7633 else if (defaulted_int && ! longlong
7634 && ! (long_p || short_p || signed_p || unsigned_p))
37c46b43
MS
7635 type = complex_double_type_node;
7636 else if (type == integer_type_node)
7637 type = complex_integer_type_node;
7638 else if (type == float_type_node)
7639 type = complex_float_type_node;
7640 else if (type == double_type_node)
7641 type = complex_double_type_node;
7642 else if (type == long_double_type_node)
7643 type = complex_long_double_type_node;
7644 else
7645 type = build_complex_type (type);
7646 }
7647
4f2b0fb2 7648 type_quals = TYPE_UNQUALIFIED;
62d1db17 7649 if (declspecs->specs[(int)ds_const])
4f2b0fb2 7650 type_quals |= TYPE_QUAL_CONST;
62d1db17 7651 if (declspecs->specs[(int)ds_volatile])
4f2b0fb2 7652 type_quals |= TYPE_QUAL_VOLATILE;
62d1db17 7653 if (declspecs->specs[(int)ds_restrict])
4f2b0fb2
NS
7654 type_quals |= TYPE_QUAL_RESTRICT;
7655 if (sfk == sfk_conversion && type_quals != TYPE_UNQUALIFIED)
2d01edd7 7656 error ("qualifiers are not allowed on declaration of %<operator %T%>",
0cbd7506 7657 ctor_return_type);
3ac3d9ea 7658
c8094d83 7659 if (TREE_CODE (type) == FUNCTION_TYPE
9804209d
DG
7660 && type_quals != TYPE_UNQUALIFIED)
7661 {
7662 /* This was an error in C++98 (cv-qualifiers cannot be added to
0cbd7506
MS
7663 a function type), but DR 295 makes the code well-formed by
7664 dropping the extra qualifiers. */
9804209d 7665 if (pedantic)
0cbd7506
MS
7666 {
7667 tree bad_type = build_qualified_type (type, type_quals);
7668 pedwarn ("ignoring %qV qualifiers added to function type %qT",
7669 bad_type, type);
7670 }
9804209d
DG
7671 type_quals = TYPE_UNQUALIFIED;
7672 }
4f2b0fb2
NS
7673 type_quals |= cp_type_quals (type);
7674 type = cp_build_qualified_type_real
7675 (type, type_quals, ((typedef_decl && !DECL_ARTIFICIAL (typedef_decl)
23fca1f5 7676 ? tf_ignore_bad_quals : 0) | tf_warning_or_error));
4f2b0fb2
NS
7677 /* We might have ignored or rejected some of the qualifiers. */
7678 type_quals = cp_type_quals (type);
caf93cb0 7679
8d08fdba 7680 staticp = 0;
62d1db17
MM
7681 inlinep = !! declspecs->specs[(int)ds_inline];
7682 virtualp = !! declspecs->specs[(int)ds_virtual];
7683 explicitp = !! declspecs->specs[(int)ds_explicit];
8d08fdba 7684
1ff3c076
MM
7685 storage_class = declspecs->storage_class;
7686 if (storage_class == sc_static)
8d08fdba
MS
7687 staticp = 1 + (decl_context == FIELD);
7688
7689 if (virtualp && staticp == 2)
7690 {
2d01edd7 7691 error ("member %qD cannot be declared both virtual and static", dname);
7e1e0765 7692 storage_class = sc_none;
8d08fdba
MS
7693 staticp = 0;
7694 }
62d1db17 7695 friendp = !! declspecs->specs[(int)ds_friend];
8d08fdba 7696
d1c78882 7697 if (dependent_name && !friendp)
d5614afb 7698 {
d1c78882 7699 error ("%<%T::%D%> is not a valid declarator", ctype, dependent_name);
13dbe691 7700 return error_mark_node;
d5614afb 7701 }
8d08fdba 7702
62d1db17
MM
7703 /* Issue errors about use of storage classes for parameters. */
7704 if (decl_context == PARM)
8d08fdba 7705 {
62d1db17 7706 if (declspecs->specs[(int)ds_typedef])
709f9dcf
VR
7707 {
7708 error ("typedef declaration invalid in parameter declaration");
7709 return error_mark_node;
7710 }
1ff3c076
MM
7711 else if (storage_class == sc_static
7712 || storage_class == sc_extern
7713 || thread_p)
8251199e 7714 error ("storage class specifiers invalid in parameter declarations");
8d08fdba
MS
7715 }
7716
7717 /* Give error if `virtual' is used outside of class declaration. */
b7484fbe
MS
7718 if (virtualp
7719 && (current_class_name == NULL_TREE || decl_context != FIELD))
8d08fdba 7720 {
8251199e 7721 error ("virtual outside class declaration");
8d08fdba
MS
7722 virtualp = 0;
7723 }
8d08fdba
MS
7724
7725 /* Static anonymous unions are dealt with here. */
7726 if (staticp && decl_context == TYPENAME
62d1db17
MM
7727 && declspecs->type
7728 && ANON_AGGR_TYPE_P (declspecs->type))
8d08fdba
MS
7729 decl_context = FIELD;
7730
8d08fdba
MS
7731 /* Warn about storage classes that are invalid for certain
7732 kinds of declarations (parameters, typenames, etc.). */
3e66d096
JJ
7733 if (thread_p
7734 && ((storage_class
7735 && storage_class != sc_extern
7736 && storage_class != sc_static)
7737 || declspecs->specs[(int)ds_typedef]))
7738 {
7739 error ("multiple storage classes in declaration of %qs", name);
7740 thread_p = false;
7741 }
26106542 7742 if (declspecs->conflicting_specifiers_p)
a23e08d9 7743 {
26106542 7744 error ("conflicting specifiers in declaration of %qs", name);
a23e08d9
VR
7745 storage_class = sc_none;
7746 }
caf93cb0 7747 else if (decl_context != NORMAL
1ff3c076
MM
7748 && ((storage_class != sc_none
7749 && storage_class != sc_mutable)
7750 || thread_p))
8d08fdba 7751 {
db5ae43f 7752 if ((decl_context == PARM || decl_context == CATCHPARM)
1ff3c076
MM
7753 && (storage_class == sc_register
7754 || storage_class == sc_auto))
8d08fdba 7755 ;
62d1db17 7756 else if (declspecs->specs[(int)ds_typedef])
fc378698 7757 ;
8d08fdba 7758 else if (decl_context == FIELD
f4f206f4 7759 /* C++ allows static class elements. */
1ff3c076 7760 && storage_class == sc_static)
d363e7bf
AJ
7761 /* C++ also allows inlines and signed and unsigned elements,
7762 but in those cases we don't come in here. */
8d08fdba
MS
7763 ;
7764 else
7765 {
7766 if (decl_context == FIELD)
389c6c8b 7767 error ("storage class specified for %qs", name);
8d08fdba 7768 else
b9d12519
KG
7769 {
7770 if (decl_context == PARM || decl_context == CATCHPARM)
2d01edd7 7771 error ("storage class specified for parameter %qs", name);
b9d12519
KG
7772 else
7773 error ("storage class specified for typename");
7774 }
1ff3c076
MM
7775 if (storage_class == sc_register
7776 || storage_class == sc_auto
7777 || storage_class == sc_extern
7778 || thread_p)
7779 storage_class = sc_none;
8d08fdba
MS
7780 }
7781 }
caf93cb0 7782 else if (storage_class == sc_extern && initialized
62d1db17 7783 && !funcdef_flag)
8d08fdba 7784 {
a9aedbc2 7785 if (toplevel_bindings_p ())
8d08fdba 7786 {
59be0cdd 7787 /* It's common practice (and completely valid) to have a const
8d08fdba 7788 be initialized and declared extern. */
950ad3c3 7789 if (!(type_quals & TYPE_QUAL_CONST))
d4ee4d25 7790 warning (0, "%qs initialized and declared %<extern%>", name);
8d08fdba
MS
7791 }
7792 else
2d01edd7 7793 error ("%qs has both %<extern%> and initializer", name);
8d08fdba 7794 }
1ff3c076 7795 else if (storage_class == sc_extern && funcdef_flag
a9aedbc2 7796 && ! toplevel_bindings_p ())
2d01edd7 7797 error ("nested function %qs declared %<extern%>", name);
a9aedbc2 7798 else if (toplevel_bindings_p ())
8d08fdba 7799 {
1ff3c076 7800 if (storage_class == sc_auto)
2d01edd7 7801 error ("top-level declaration of %qs specifies %<auto%>", name);
8d08fdba 7802 }
1ff3c076
MM
7803 else if (thread_p
7804 && storage_class != sc_extern
7805 && storage_class != sc_static)
7a1f3f5f 7806 {
2d01edd7 7807 error ("function-scope %qs implicitly auto and declared %<__thread%>",
7a1f3f5f 7808 name);
1ff3c076 7809 thread_p = false;
7a1f3f5f 7810 }
8d08fdba 7811
1ff3c076 7812 if (storage_class && friendp)
8251199e 7813 error ("storage class specifiers invalid in friend function declarations");
909e536a 7814
058b15c1
MM
7815 if (!id_declarator)
7816 unqualified_id = NULL_TREE;
7817 else
8d08fdba 7818 {
1d786913 7819 unqualified_id = id_declarator->u.id.unqualified_name;
058b15c1 7820 switch (TREE_CODE (unqualified_id))
8d08fdba 7821 {
058b15c1 7822 case BIT_NOT_EXPR:
caf93cb0 7823 unqualified_id
058b15c1
MM
7824 = constructor_name (TREE_OPERAND (unqualified_id, 0));
7825 break;
caf93cb0 7826
058b15c1
MM
7827 case IDENTIFIER_NODE:
7828 case TEMPLATE_ID_EXPR:
52fbc847 7829 break;
caf93cb0 7830
058b15c1 7831 default:
8dc2b103 7832 gcc_unreachable ();
058b15c1
MM
7833 }
7834 }
52fbc847 7835
058b15c1
MM
7836 /* Determine the type of the entity declared by recurring on the
7837 declarator. */
f4f18103 7838 for (; declarator; declarator = declarator->declarator)
058b15c1
MM
7839 {
7840 const cp_declarator *inner_declarator;
7841 tree attrs;
8d08fdba 7842
058b15c1
MM
7843 if (type == error_mark_node)
7844 return error_mark_node;
c1b177ec 7845
058b15c1
MM
7846 attrs = declarator->attributes;
7847 if (attrs)
7848 {
7849 int attr_flags;
caf93cb0 7850
058b15c1
MM
7851 attr_flags = 0;
7852 if (declarator == NULL || declarator->kind == cdk_id)
7853 attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
7854 if (declarator->kind == cdk_function)
7855 attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
7856 if (declarator->kind == cdk_array)
7857 attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
7858 returned_attrs = decl_attributes (&type,
7859 chainon (returned_attrs, attrs),
7860 attr_flags);
7861 }
7862
f4f18103
MM
7863 if (declarator->kind == cdk_id)
7864 break;
7865
7866 inner_declarator = declarator->declarator;
7867
058b15c1
MM
7868 switch (declarator->kind)
7869 {
7870 case cdk_array:
caf93cb0 7871 type = create_array_type_for_decl (dname, type,
058b15c1 7872 declarator->u.array.bounds);
8d08fdba
MS
7873 break;
7874
058b15c1 7875 case cdk_function:
8d08fdba
MS
7876 {
7877 tree arg_types;
f376e137 7878 int funcdecl_p;
8d08fdba
MS
7879
7880 /* Declaring a function type.
7881 Make sure we have a valid type for the function to return. */
8d08fdba 7882
1891dec4
DM
7883 if (type_quals != TYPE_UNQUALIFIED)
7884 {
7885 if (SCALAR_TYPE_P (type) || VOID_TYPE_P (type))
7886 warning (OPT_Wreturn_type,
7887 "type qualifiers ignored on function return type");
7888 /* We now know that the TYPE_QUALS don't apply to the
7889 decl, but to its return type. */
7890 type_quals = TYPE_UNQUALIFIED;
7891 set_no_warning = true;
7892 }
8d08fdba
MS
7893
7894 /* Warn about some types functions can't return. */
7895
7896 if (TREE_CODE (type) == FUNCTION_TYPE)
7897 {
2d01edd7 7898 error ("%qs declared as function returning a function", name);
8d08fdba
MS
7899 type = integer_type_node;
7900 }
7901 if (TREE_CODE (type) == ARRAY_TYPE)
7902 {
2d01edd7 7903 error ("%qs declared as function returning an array", name);
8d08fdba
MS
7904 type = integer_type_node;
7905 }
7906
b7484fbe 7907 /* Pick up type qualifiers which should be applied to `this'. */
e2537f2c 7908 memfn_quals = declarator->u.function.qualifiers;
b7484fbe 7909
c11b6f21 7910 /* Pick up the exception specifications. */
058b15c1 7911 raises = declarator->u.function.exception_specification;
c11b6f21 7912
f376e137
MS
7913 /* Say it's a definition only for the CALL_EXPR
7914 closest to the identifier. */
058b15c1 7915 funcdecl_p = inner_declarator && inner_declarator->kind == cdk_id;
68642fb6 7916
8d08fdba
MS
7917 if (ctype == NULL_TREE
7918 && decl_context == FIELD
f376e137 7919 && funcdecl_p
8d08fdba
MS
7920 && (friendp == 0 || dname == current_class_name))
7921 ctype = current_class_type;
7922
058b15c1
MM
7923 if (ctype && (sfk == sfk_constructor
7924 || sfk == sfk_destructor))
8d08fdba
MS
7925 {
7926 /* We are within a class's scope. If our declarator name
7927 is the same as the class name, and we are defining
7928 a function, then it is a constructor/destructor, and
7929 therefore returns a void type. */
3db45ab5 7930
e2537f2c
MM
7931 /* ISO C++ 12.4/2. A destructor may not be declared
7932 const or volatile. A destructor may not be
7933 static.
3db45ab5 7934
e2537f2c
MM
7935 ISO C++ 12.1. A constructor may not be declared
7936 const or volatile. A constructor may not be
7937 virtual. A constructor may not be static. */
3db45ab5 7938 if (staticp == 2)
e2537f2c
MM
7939 error ((flags == DTOR_FLAG)
7940 ? "destructor cannot be static member function"
7941 : "constructor cannot be static member function");
7942 if (memfn_quals)
8d08fdba 7943 {
e2537f2c
MM
7944 error ((flags == DTOR_FLAG)
7945 ? "destructors may not be cv-qualified"
7946 : "constructors may not be cv-qualified");
7947 memfn_quals = TYPE_UNQUALIFIED;
8d08fdba 7948 }
e2537f2c
MM
7949
7950 if (decl_context == FIELD
7951 && !member_function_or_else (ctype,
7952 current_class_type,
7953 flags))
13dbe691 7954 return error_mark_node;
e2537f2c
MM
7955
7956 if (flags != DTOR_FLAG)
8d08fdba 7957 {
e2537f2c 7958 /* It's a constructor. */
db5ae43f
MS
7959 if (explicitp == 1)
7960 explicitp = 2;
8d08fdba
MS
7961 if (virtualp)
7962 {
8251199e 7963 pedwarn ("constructors cannot be declared virtual");
8d08fdba
MS
7964 virtualp = 0;
7965 }
7137605e
MM
7966 if (decl_context == FIELD
7967 && sfk != sfk_constructor)
13dbe691 7968 return error_mark_node;
8d08fdba
MS
7969 }
7970 if (decl_context == FIELD)
7971 staticp = 0;
7972 }
b7484fbe 7973 else if (friendp)
8d08fdba 7974 {
b7484fbe 7975 if (initialized)
2d01edd7 7976 error ("can't initialize friend function %qs", name);
b7484fbe
MS
7977 if (virtualp)
7978 {
7979 /* Cannot be both friend and virtual. */
8251199e 7980 error ("virtual functions cannot be friends");
b7484fbe
MS
7981 friendp = 0;
7982 }
28cbf42c 7983 if (decl_context == NORMAL)
8251199e 7984 error ("friend declaration not in class definition");
28cbf42c 7985 if (current_function_decl && funcdef_flag)
2d01edd7 7986 error ("can't define friend function %qs in a local "
0cbd7506
MS
7987 "class definition",
7988 name);
8d08fdba
MS
7989 }
7990
caf93cb0 7991 arg_types = grokparms (declarator->u.function.parameters,
058b15c1 7992 &parms);
8d08fdba 7993
058b15c1
MM
7994 if (inner_declarator
7995 && inner_declarator->kind == cdk_id
7996 && inner_declarator->u.id.sfk == sfk_destructor
7997 && arg_types != void_list_node)
8d08fdba 7998 {
058b15c1
MM
7999 error ("destructors may not have parameters");
8000 arg_types = void_list_node;
8001 parms = NULL_TREE;
8d08fdba 8002 }
caf93cb0 8003
d22c8596 8004 type = build_function_type (type, arg_types);
8d08fdba
MS
8005 }
8006 break;
8007
058b15c1
MM
8008 case cdk_pointer:
8009 case cdk_reference:
8010 case cdk_ptrmem:
8d08fdba
MS
8011 /* Filter out pointers-to-references and references-to-references.
8012 We can get these if a TYPE_DECL is used. */
8013
8014 if (TREE_CODE (type) == REFERENCE_TYPE)
8015 {
8af2fec4
RY
8016 if (declarator->kind != cdk_reference)
8017 {
8018 error ("cannot declare pointer to %q#T", type);
8019 type = TREE_TYPE (type);
8020 }
8021
8022 /* In C++0x, we allow reference to reference declarations
8023 that occur indirectly through typedefs [7.1.3/8 dcl.typedef]
8024 and template type arguments [14.3.1/4 temp.arg.type]. The
8025 check for direct reference to reference declarations, which
8026 are still forbidden, occurs below. Reasoning behind the change
8027 can be found in DR106, DR540, and the rvalue reference
8028 proposals. */
c1ae8be5 8029 else if (cxx_dialect == cxx98)
8af2fec4
RY
8030 {
8031 error ("cannot declare reference to %q#T", type);
8032 type = TREE_TYPE (type);
8033 }
a5894242 8034 }
058b15c1
MM
8035 else if (VOID_TYPE_P (type))
8036 {
8037 if (declarator->kind == cdk_reference)
2d01edd7 8038 error ("cannot declare reference to %q#T", type);
058b15c1 8039 else if (declarator->kind == cdk_ptrmem)
2d01edd7 8040 error ("cannot declare pointer to %q#T member", type);
058b15c1 8041 }
61a127b3 8042
91063b51
MM
8043 /* We now know that the TYPE_QUALS don't apply to the decl,
8044 but to the target of the pointer. */
8045 type_quals = TYPE_UNQUALIFIED;
8d08fdba 8046
058b15c1 8047 if (declarator->kind == cdk_ptrmem
e2537f2c 8048 && (TREE_CODE (type) == FUNCTION_TYPE || memfn_quals))
058b15c1 8049 {
e2537f2c 8050 memfn_quals |= cp_type_quals (type);
3db45ab5 8051 type = build_memfn_type (type,
e2537f2c
MM
8052 declarator->u.pointer.class_type,
8053 memfn_quals);
8054 memfn_quals = TYPE_UNQUALIFIED;
058b15c1
MM
8055 }
8056
8057 if (declarator->kind == cdk_reference)
8d08fdba 8058 {
8af2fec4
RY
8059 /* In C++0x, the type we are creating a reference to might be
8060 a typedef which is itself a reference type. In that case,
8061 we follow the reference collapsing rules in
8062 [7.1.3/8 dcl.typedef] to create the final reference type:
8063
8064 "If a typedef TD names a type that is a reference to a type
8065 T, an attempt to create the type 'lvalue reference to cv TD'
8066 creates the type 'lvalue reference to T,' while an attempt
8067 to create the type "rvalue reference to cv TD' creates the
8068 type TD."
8069 */
dd29188b 8070 if (!VOID_TYPE_P (type))
8af2fec4
RY
8071 type = cp_build_reference_type
8072 ((TREE_CODE (type) == REFERENCE_TYPE
8073 ? TREE_TYPE (type) : type),
8074 (declarator->u.reference.rvalue_ref
8075 && (TREE_CODE(type) != REFERENCE_TYPE
8076 || TYPE_REF_IS_RVALUE (type))));
8077
8078 /* In C++0x, we need this check for direct reference to
8079 reference declarations, which are forbidden by
8080 [8.3.2/5 dcl.ref]. Reference to reference declarations
8081 are only allowed indirectly through typedefs and template
8082 type arguments. Example:
8083
8084 void foo(int & &); // invalid ref-to-ref decl
8085
8086 typedef int & int_ref;
8087 void foo(int_ref &); // valid ref-to-ref decl
8088 */
8089 if (inner_declarator && inner_declarator->kind == cdk_reference)
8090 error ("cannot declare reference to %q#T, which is not "
8091 "a typedef or a template type argument", type);
8d08fdba
MS
8092 }
8093 else if (TREE_CODE (type) == METHOD_TYPE)
d8f8dca1 8094 type = build_ptrmemfunc_type (build_pointer_type (type));
058b15c1 8095 else if (declarator->kind == cdk_ptrmem)
f4ed7d21 8096 {
63c9a190
MM
8097 gcc_assert (TREE_CODE (declarator->u.pointer.class_type)
8098 != NAMESPACE_DECL);
8099 if (declarator->u.pointer.class_type == error_mark_node)
4230cec2
NS
8100 /* We will already have complained. */
8101 type = error_mark_node;
f4ed7d21
NS
8102 else
8103 type = build_ptrmem_type (declarator->u.pointer.class_type,
8104 type);
8105 }
8d08fdba
MS
8106 else
8107 type = build_pointer_type (type);
8108
8109 /* Process a list of type modifier keywords (such as
8110 const or volatile) that were given inside the `*' or `&'. */
8111
058b15c1 8112 if (declarator->u.pointer.qualifiers)
8d08fdba 8113 {
caf93cb0
EC
8114 type
8115 = cp_build_qualified_type (type,
3c01e5df 8116 declarator->u.pointer.qualifiers);
4f2b0fb2 8117 type_quals = cp_type_quals (type);
8d08fdba 8118 }
8d08fdba
MS
8119 ctype = NULL_TREE;
8120 break;
8121
058b15c1
MM
8122 case cdk_error:
8123 break;
8d08fdba 8124
058b15c1 8125 default:
8dc2b103 8126 gcc_unreachable ();
058b15c1
MM
8127 }
8128 }
caf93cb0 8129
058b15c1
MM
8130 if (unqualified_id && TREE_CODE (unqualified_id) == TEMPLATE_ID_EXPR
8131 && TREE_CODE (type) != FUNCTION_TYPE
8132 && TREE_CODE (type) != METHOD_TYPE)
8133 {
2d01edd7 8134 error ("template-id %qD used as a declarator",
058b15c1
MM
8135 unqualified_id);
8136 unqualified_id = dname;
8137 }
386b8a85 8138
9c12301f
MM
8139 /* If TYPE is a FUNCTION_TYPE, but the function name was explicitly
8140 qualified with a class-name, turn it into a METHOD_TYPE, unless
8141 we know that the function is static. We take advantage of this
8142 opportunity to do other processing that pertains to entities
8143 explicitly declared to be class members. Note that if DECLARATOR
8144 is non-NULL, we know it is a cdk_id declarator; otherwise, we
8145 would not have exited the loop above. */
caf93cb0 8146 if (declarator
1d786913
MM
8147 && declarator->u.id.qualifying_scope
8148 && TYPE_P (declarator->u.id.qualifying_scope))
058b15c1
MM
8149 {
8150 tree t;
8d08fdba 8151
1d786913
MM
8152 ctype = declarator->u.id.qualifying_scope;
8153 ctype = TYPE_MAIN_VARIANT (ctype);
058b15c1
MM
8154 t = ctype;
8155 while (t != NULL_TREE && CLASS_TYPE_P (t))
8156 {
8157 /* You're supposed to have one `template <...>' for every
8158 template class, but you don't need one for a full
8159 specialization. For example:
8d08fdba 8160
058b15c1
MM
8161 template <class T> struct S{};
8162 template <> struct S<int> { void f(); };
8163 void S<int>::f () {}
28cbf42c 8164
058b15c1
MM
8165 is correct; there shouldn't be a `template <>' for the
8166 definition of `S<int>::f'. */
f0d60e22
MM
8167 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t)
8168 && !any_dependent_template_arguments_p (CLASSTYPE_TI_ARGS (t)))
8169 /* T is an explicit (not partial) specialization. All
8170 containing classes must therefore also be explicitly
8171 specialized. */
8172 break;
8173 if ((CLASSTYPE_USE_TEMPLATE (t) || CLASSTYPE_IS_TEMPLATE (t))
058b15c1
MM
8174 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t)))
8175 template_count += 1;
28cbf42c 8176
058b15c1
MM
8177 t = TYPE_MAIN_DECL (t);
8178 t = DECL_CONTEXT (t);
8179 }
8d08fdba 8180
058b15c1 8181 if (ctype == current_class_type)
59e7c84c
VR
8182 {
8183 if (friendp)
b46ce77f
SM
8184 {
8185 pedwarn ("member functions are implicitly friends of their class");
8186 friendp = 0;
8187 }
59e7c84c
VR
8188 else
8189 pedwarn ("extra qualification %<%T::%> on member %qs",
8190 ctype, name);
8191 }
fa6098f8
MM
8192 else if (/* If the qualifying type is already complete, then we
8193 can skip the following checks. */
8194 !COMPLETE_TYPE_P (ctype)
cc328145
MM
8195 && (/* If the function is being defined, then
8196 qualifying type must certainly be complete. */
3db45ab5 8197 funcdef_flag
cc328145
MM
8198 /* A friend declaration of "T::f" is OK, even if
8199 "T" is a template parameter. But, if this
8200 function is not a friend, the qualifying type
8201 must be a class. */
8202 || (!friendp && !CLASS_TYPE_P (ctype))
8203 /* For a declaration, the type need not be
8204 complete, if either it is dependent (since there
8205 is no meaningful definition of complete in that
8206 case) or the qualifying class is currently being
8207 defined. */
fa6098f8
MM
8208 || !(dependent_type_p (ctype)
8209 || currently_open_class (ctype)))
9f9a713e 8210 /* Check that the qualifying type is complete. */
fa6098f8
MM
8211 && !complete_type_or_else (ctype, NULL_TREE))
8212 return error_mark_node;
058b15c1
MM
8213 else if (TREE_CODE (type) == FUNCTION_TYPE)
8214 {
1d786913 8215 tree sname = declarator->u.id.unqualified_name;
50ad9642 8216
357d956e
MM
8217 if (current_class_type
8218 && (!friendp || funcdef_flag))
8219 {
8220 error (funcdef_flag
8221 ? "cannot define member function %<%T::%s%> within %<%T%>"
8222 : "cannot declare member function %<%T::%s%> within %<%T%>",
8223 ctype, name, current_class_type);
8224 return error_mark_node;
8225 }
8226
058b15c1
MM
8227 if (TREE_CODE (sname) == IDENTIFIER_NODE
8228 && NEW_DELETE_OPNAME_P (sname))
8229 /* Overloaded operator new and operator delete
8230 are always static functions. */
8231 ;
058b15c1 8232 else
e2537f2c 8233 type = build_memfn_type (type, ctype, memfn_quals);
058b15c1 8234 }
62d1db17 8235 else if (declspecs->specs[(int)ds_typedef]
fa6098f8 8236 && current_class_type)
058b15c1 8237 {
fa6098f8
MM
8238 error ("cannot declare member %<%T::%s%> within %qT",
8239 ctype, name, current_class_type);
058b15c1 8240 return error_mark_node;
8d08fdba
MS
8241 }
8242 }
8243
9c12301f
MM
8244 /* Now TYPE has the actual type. */
8245
91d231cb 8246 if (returned_attrs)
1eab9b56 8247 {
91d231cb
JM
8248 if (attrlist)
8249 *attrlist = chainon (returned_attrs, *attrlist);
1eab9b56 8250 else
91d231cb 8251 attrlist = &returned_attrs;
1eab9b56
JM
8252 }
8253
5d80a306
DG
8254 /* Handle parameter packs. */
8255 if (parameter_pack_p)
8256 {
8257 if (decl_context == PARM)
8258 /* Turn the type into a pack expansion.*/
8259 type = make_pack_expansion (type);
8260 else
8261 error ("non-parameter %qs cannot be a parameter pack", name);
8262 }
8263
55b3d665
JM
8264 /* Did array size calculations overflow? */
8265
8266 if (TREE_CODE (type) == ARRAY_TYPE
8267 && COMPLETE_TYPE_P (type)
96ce2ac9
JW
8268 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST
8269 && TREE_OVERFLOW (TYPE_SIZE_UNIT (type)))
55b3d665 8270 {
2d01edd7 8271 error ("size of array %qs is too large", name);
aba649ba 8272 /* If we proceed with the array type as it is, we'll eventually
55b3d665
JM
8273 crash in tree_low_cst(). */
8274 type = error_mark_node;
8275 }
8276
2fff6d71 8277 if ((decl_context == FIELD || decl_context == PARM)
caf93cb0 8278 && !processing_template_decl
5377d5ba 8279 && variably_modified_type_p (type, NULL_TREE))
dac45b5c 8280 {
2fff6d71 8281 if (decl_context == FIELD)
2d01edd7 8282 error ("data member may not have variably modified type %qT", type);
2fff6d71 8283 else
2d01edd7 8284 error ("parameter may not have variably modified type %qT", type);
dac45b5c
MM
8285 type = error_mark_node;
8286 }
8287
34fc957d 8288 if (explicitp == 1 || (explicitp && friendp))
db5ae43f 8289 {
34fc957d 8290 /* [dcl.fct.spec] The explicit specifier shall only be used in
0cbd7506 8291 declarations of constructors within a class definition. */
2d01edd7 8292 error ("only declarations of constructors can be %<explicit%>");
db5ae43f
MS
8293 explicitp = 0;
8294 }
8295
1ff3c076 8296 if (storage_class == sc_mutable)
f30432d7 8297 {
4223f82f 8298 if (decl_context != FIELD || friendp)
0cbd7506 8299 {
2d01edd7 8300 error ("non-member %qs cannot be declared %<mutable%>", name);
1ff3c076 8301 storage_class = sc_none;
0cbd7506 8302 }
62d1db17 8303 else if (decl_context == TYPENAME || declspecs->specs[(int)ds_typedef])
f30432d7 8304 {
2d01edd7 8305 error ("non-object member %qs cannot be declared %<mutable%>", name);
1ff3c076 8306 storage_class = sc_none;
f30432d7 8307 }
34fc957d 8308 else if (TREE_CODE (type) == FUNCTION_TYPE
0cbd7506
MS
8309 || TREE_CODE (type) == METHOD_TYPE)
8310 {
2d01edd7 8311 error ("function %qs cannot be declared %<mutable%>", name);
1ff3c076 8312 storage_class = sc_none;
0cbd7506 8313 }
f30432d7
MS
8314 else if (staticp)
8315 {
2d01edd7 8316 error ("static %qs cannot be declared %<mutable%>", name);
1ff3c076 8317 storage_class = sc_none;
f30432d7 8318 }
34fc957d
NS
8319 else if (type_quals & TYPE_QUAL_CONST)
8320 {
2d01edd7 8321 error ("const %qs cannot be declared %<mutable%>", name);
1ff3c076 8322 storage_class = sc_none;
34fc957d 8323 }
f30432d7
MS
8324 }
8325
419c6212 8326 /* If this is declaring a typedef name, return a TYPE_DECL. */
62d1db17 8327 if (declspecs->specs[(int)ds_typedef] && decl_context != TYPENAME)
8d08fdba
MS
8328 {
8329 tree decl;
8330
8331 /* Note that the grammar rejects storage classes
8332 in typenames, fields or parameters. */
eff71ab0
PB
8333 if (current_lang_name == lang_name_java)
8334 TYPE_FOR_JAVA (type) = 1;
3db45ab5 8335
e2537f2c
MM
8336 /* This declaration:
8337
3db45ab5 8338 typedef void f(int) const;
e2537f2c 8339
3db45ab5 8340 declares a function type which is not a member of any
e2537f2c 8341 particular class, but which is cv-qualified; for
3db45ab5 8342 example "f S::*" declares a pointer to a const-qualified
e2537f2c
MM
8343 member function of S. We record the cv-qualification in the
8344 function type. */
8345 if (memfn_quals && TREE_CODE (type) == FUNCTION_TYPE)
771026dd
DG
8346 {
8347 type = cp_build_qualified_type (type, memfn_quals);
8348
8349 /* We have now dealt with these qualifiers. */
8350 memfn_quals = TYPE_UNQUALIFIED;
8351 }
8d08fdba 8352
d2e5ee5c 8353 if (decl_context == FIELD)
e3016344 8354 decl = build_lang_decl (TYPE_DECL, unqualified_id, type);
d2e5ee5c 8355 else
e3016344
MM
8356 decl = build_decl (TYPE_DECL, unqualified_id, type);
8357 if (id_declarator && declarator->u.id.qualifying_scope)
8358 error ("%Jtypedef name may not be a nested-name-specifier", decl);
8359
8360 if (decl_context != FIELD)
6bda7a5e 8361 {
6bda7a5e
NS
8362 if (!current_function_decl)
8363 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
1dc82a99 8364 else if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (current_function_decl)
c8094d83 8365 || (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P
1dc82a99
MM
8366 (current_function_decl)))
8367 /* The TYPE_DECL is "abstract" because there will be
8368 clones of this constructor/destructor, and there will
8369 be copies of this TYPE_DECL generated in those
8370 clones. */
8371 DECL_ABSTRACT (decl) = 1;
6bda7a5e 8372 }
e3016344
MM
8373 else if (constructor_name_p (unqualified_id, current_class_type))
8374 pedwarn ("ISO C++ forbids nested type %qD with same name "
8375 "as enclosing class",
8376 unqualified_id);
caf93cb0 8377
9188c363
MM
8378 /* If the user declares "typedef struct {...} foo" then the
8379 struct will have an anonymous name. Fill that name in now.
8380 Nothing can refer to it, so nothing needs know about the name
8381 change. */
8d08fdba 8382 if (type != error_mark_node
058b15c1 8383 && unqualified_id
8d08fdba
MS
8384 && TYPE_NAME (type)
8385 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
1951a1b6 8386 && TYPE_ANONYMOUS_P (type)
ac905924
JM
8387 /* Don't do this if there are attributes. */
8388 && (!attrlist || !*attrlist)
89d684bb 8389 && cp_type_quals (type) == TYPE_UNQUALIFIED)
8d08fdba 8390 {
dcd08efc
JM
8391 tree oldname = TYPE_NAME (type);
8392 tree t;
8393
2c73f9f5 8394 /* Replace the anonymous name with the real name everywhere. */
dcd08efc
JM
8395 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
8396 if (TYPE_NAME (t) == oldname)
8397 TYPE_NAME (t) = decl;
8d08fdba
MS
8398
8399 if (TYPE_LANG_SPECIFIC (type))
8400 TYPE_WAS_ANONYMOUS (type) = 1;
8401
33964bf4
MM
8402 /* If this is a typedef within a template class, the nested
8403 type is a (non-primary) template. The name for the
8404 template needs updating as well. */
8405 if (TYPE_LANG_SPECIFIC (type) && CLASSTYPE_TEMPLATE_INFO (type))
68642fb6 8406 DECL_NAME (CLASSTYPE_TI_TEMPLATE (type))
33964bf4
MM
8407 = TYPE_IDENTIFIER (type);
8408
50a6dbd7
JM
8409 /* FIXME remangle member functions; member functions of a
8410 type with external linkage have external linkage. */
fc378698 8411 }
fc378698 8412
1ff3c076 8413 if (signed_p
8d08fdba
MS
8414 || (typedef_decl && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl)))
8415 C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
8416
3db45ab5 8417 bad_specifiers (decl, "type", virtualp,
e2537f2c 8418 memfn_quals != TYPE_UNQUALIFIED,
c91a56d2
MS
8419 inlinep, friendp, raises != NULL_TREE);
8420
8d08fdba
MS
8421 return decl;
8422 }
8423
8424 /* Detect the case of an array type of unspecified size
8425 which came, as such, direct from a typedef name.
8d6e459d
NS
8426 We must copy the type, so that the array's domain can be
8427 individually set by the object's initializer. */
8d08fdba 8428
8d6e459d
NS
8429 if (type && typedef_type
8430 && TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type)
55b3d665 8431 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (typedef_type))
8d6e459d 8432 type = build_cplus_array_type (TREE_TYPE (type), NULL_TREE);
8d08fdba 8433
969fd501 8434 /* Detect where we're using a typedef of function type to declare a
4546865e 8435 function. PARMS will not be set, so we must create it now. */
caf93cb0 8436
969fd501
NS
8437 if (type == typedef_type && TREE_CODE (type) == FUNCTION_TYPE)
8438 {
8439 tree decls = NULL_TREE;
8440 tree args;
8441
8442 for (args = TYPE_ARG_TYPES (type); args; args = TREE_CHAIN (args))
8443 {
8e51619a 8444 tree decl = cp_build_parm_decl (NULL_TREE, TREE_VALUE (args));
969fd501
NS
8445
8446 TREE_CHAIN (decl) = decls;
8447 decls = decl;
8448 }
caf93cb0 8449
4546865e 8450 parms = nreverse (decls);
9804209d
DG
8451
8452 if (decl_context != TYPENAME)
0cbd7506
MS
8453 {
8454 /* A cv-qualifier-seq shall only be part of the function type
8455 for a non-static member function. [8.3.5/4 dcl.fct] */
8456 if (cp_type_quals (type) != TYPE_UNQUALIFIED
8457 && (current_class_type == NULL_TREE || staticp) )
8458 {
ebbc3ce1 8459 error (staticp
f88d05c0
DM
8460 ? G_("qualified function types cannot be used to "
8461 "declare static member functions")
8462 : G_("qualified function types cannot be used to "
8463 "declare free functions"));
0cbd7506
MS
8464 type = TYPE_MAIN_VARIANT (type);
8465 }
8466
8467 /* The qualifiers on the function type become the qualifiers on
8468 the non-static member function. */
e2537f2c 8469 memfn_quals |= cp_type_quals (type);
0cbd7506 8470 }
969fd501
NS
8471 }
8472
8d08fdba
MS
8473 /* If this is a type name (such as, in a cast or sizeof),
8474 compute the type and return it now. */
8475
8476 if (decl_context == TYPENAME)
8477 {
8478 /* Note that the grammar rejects storage classes
8479 in typenames, fields or parameters. */
91063b51 8480 if (type_quals != TYPE_UNQUALIFIED)
6eabb241 8481 type_quals = TYPE_UNQUALIFIED;
8d08fdba
MS
8482
8483 /* Special case: "friend class foo" looks like a TYPENAME context. */
8484 if (friendp)
8485 {
91063b51 8486 if (type_quals != TYPE_UNQUALIFIED)
b7484fbe 8487 {
33bd39a2 8488 error ("type qualifiers specified for friend class declaration");
91063b51 8489 type_quals = TYPE_UNQUALIFIED;
b7484fbe
MS
8490 }
8491 if (inlinep)
8492 {
2d01edd7 8493 error ("%<inline%> specified for friend class declaration");
b7484fbe
MS
8494 inlinep = 0;
8495 }
f2ae0c45 8496
218e0eb6 8497 if (!current_aggr)
4b054b80 8498 {
218e0eb6 8499 /* Don't allow friend declaration without a class-key. */
4b054b80 8500 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
218e0eb6 8501 pedwarn ("template parameters cannot be friends");
f2ae0c45 8502 else if (TREE_CODE (type) == TYPENAME_TYPE)
0cbd7506 8503 pedwarn ("friend declaration requires class-key, "
2d01edd7 8504 "i.e. %<friend class %T::%D%>",
218e0eb6 8505 TYPE_CONTEXT (type), TYPENAME_TYPE_FULLNAME (type));
4b054b80 8506 else
0cbd7506 8507 pedwarn ("friend declaration requires class-key, "
2d01edd7 8508 "i.e. %<friend %#T%>",
218e0eb6 8509 type);
4b054b80 8510 }
b7484fbe
MS
8511
8512 /* Only try to do this stuff if we didn't already give up. */
8513 if (type != integer_type_node)
8514 {
8515 /* A friendly class? */
8516 if (current_class_type)
19db77ce
KL
8517 make_friend_class (current_class_type, TYPE_MAIN_VARIANT (type),
8518 /*complain=*/true);
b7484fbe 8519 else
2d01edd7 8520 error ("trying to make class %qT a friend of global scope",
0cbd7506 8521 type);
d363e7bf 8522
b7484fbe
MS
8523 type = void_type_node;
8524 }
8d08fdba 8525 }
e2537f2c 8526 else if (memfn_quals)
8d08fdba 8527 {
8d08fdba
MS
8528 if (ctype == NULL_TREE)
8529 {
41cbc04c 8530 if (TREE_CODE (type) != METHOD_TYPE)
0cbd7506 8531 error ("invalid qualifiers on non-member function type");
41cbc04c 8532 else
0cbd7506 8533 ctype = TYPE_METHOD_BASETYPE (type);
41cbc04c
NS
8534 }
8535 if (ctype)
e2537f2c 8536 type = build_memfn_type (type, ctype, memfn_quals);
8d08fdba
MS
8537 }
8538
8539 return type;
8540 }
058b15c1 8541 else if (unqualified_id == NULL_TREE && decl_context != PARM
db5ae43f 8542 && decl_context != CATCHPARM
8d08fdba
MS
8543 && TREE_CODE (type) != UNION_TYPE
8544 && ! bitfield)
8545 {
2d01edd7 8546 error ("abstract declarator %qT used as declaration", type);
22ab714d 8547 return error_mark_node;
8d08fdba
MS
8548 }
8549
62e19030
MM
8550 /* Only functions may be declared using an operator-function-id. */
8551 if (unqualified_id
8552 && IDENTIFIER_OPNAME_P (unqualified_id)
8553 && TREE_CODE (type) != FUNCTION_TYPE
8554 && TREE_CODE (type) != METHOD_TYPE)
8d08fdba 8555 {
62e19030
MM
8556 error ("declaration of %qD as non-function", unqualified_id);
8557 return error_mark_node;
8d08fdba
MS
8558 }
8559
62e19030
MM
8560 /* We don't check parameter types here because we can emit a better
8561 error message later. */
8562 if (decl_context != PARM)
650fcd07
LM
8563 {
8564 type = check_var_type (unqualified_id, type);
8565 if (type == error_mark_node)
8566 return error_mark_node;
8567 }
62e19030 8568
8d08fdba
MS
8569 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
8570 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
8571
14ae7e7d
JM
8572 if (decl_context == PARM || decl_context == CATCHPARM)
8573 {
8574 if (ctype || in_namespace)
2d01edd7 8575 error ("cannot use %<::%> in parameter declaration");
14ae7e7d
JM
8576
8577 /* A parameter declared as an array of T is really a pointer to T.
8578 One declared as a function is really a pointer to a function.
8579 One declared as a member is really a pointer to member. */
8580
8581 if (TREE_CODE (type) == ARRAY_TYPE)
8582 {
8583 /* Transfer const-ness of array into that of type pointed to. */
8584 type = build_pointer_type (TREE_TYPE (type));
91063b51 8585 type_quals = TYPE_UNQUALIFIED;
14ae7e7d
JM
8586 }
8587 else if (TREE_CODE (type) == FUNCTION_TYPE)
8588 type = build_pointer_type (type);
14ae7e7d 8589 }
68642fb6 8590
8d08fdba 8591 {
926ce8bd 8592 tree decl;
8d08fdba
MS
8593
8594 if (decl_context == PARM)
8595 {
058b15c1 8596 decl = cp_build_parm_decl (unqualified_id, type);
8d08fdba 8597
3db45ab5 8598 bad_specifiers (decl, "parameter", virtualp,
e2537f2c 8599 memfn_quals != TYPE_UNQUALIFIED,
8d08fdba 8600 inlinep, friendp, raises != NULL_TREE);
8d08fdba
MS
8601 }
8602 else if (decl_context == FIELD)
8603 {
01bf0f3e
JM
8604 /* The C99 flexible array extension. */
8605 if (!staticp && TREE_CODE (type) == ARRAY_TYPE
8606 && TYPE_DOMAIN (type) == NULL_TREE)
8607 {
8608 tree itype = compute_array_index_type (dname, integer_zero_node);
8609 type = build_cplus_array_type (TREE_TYPE (type), itype);
8610 }
8611
8d08fdba
MS
8612 if (type == error_mark_node)
8613 {
8614 /* Happens when declaring arrays of sizes which
8615 are error_mark_node, for example. */
8616 decl = NULL_TREE;
8617 }
997a088c 8618 else if (in_namespace && !friendp)
05008fb9
MM
8619 {
8620 /* Something like struct S { int N::j; }; */
2d01edd7 8621 error ("invalid use of %<::%>");
9a171ca4 8622 return error_mark_node;
05008fb9 8623 }
8d08fdba
MS
8624 else if (TREE_CODE (type) == FUNCTION_TYPE)
8625 {
8626 int publicp = 0;
e1467ff2 8627 tree function_context;
8d08fdba
MS
8628
8629 if (friendp == 0)
8630 {
8631 if (ctype == NULL_TREE)
8632 ctype = current_class_type;
8633
8634 if (ctype == NULL_TREE)
8635 {
2d01edd7 8636 error ("can't make %qD into a method -- not in a class",
058b15c1 8637 unqualified_id);
13dbe691 8638 return error_mark_node;
8d08fdba
MS
8639 }
8640
8641 /* ``A union may [ ... ] not [ have ] virtual functions.''
8642 ARM 9.5 */
8643 if (virtualp && TREE_CODE (ctype) == UNION_TYPE)
8644 {
2d01edd7 8645 error ("function %qD declared virtual inside a union",
058b15c1 8646 unqualified_id);
13dbe691 8647 return error_mark_node;
8d08fdba
MS
8648 }
8649
058b15c1 8650 if (NEW_DELETE_OPNAME_P (unqualified_id))
8d08fdba
MS
8651 {
8652 if (virtualp)
8653 {
2d01edd7 8654 error ("%qD cannot be declared virtual, since it "
0cbd7506 8655 "is always static",
058b15c1 8656 unqualified_id);
8d08fdba
MS
8657 virtualp = 0;
8658 }
8659 }
8660 else if (staticp < 2)
e2537f2c 8661 type = build_memfn_type (type, ctype, memfn_quals);
8d08fdba
MS
8662 }
8663
88e95ee3 8664 /* Check that the name used for a destructor makes sense. */
6d2989e1 8665 if (sfk == sfk_destructor)
88e95ee3 8666 {
6d2989e1
VR
8667 if (!ctype)
8668 {
8669 gcc_assert (friendp);
8670 error ("expected qualified name in friend declaration "
8671 "for destructor %qD",
8672 id_declarator->u.id.unqualified_name);
8673 return error_mark_node;
8674 }
8675
8676 if (!same_type_p (TREE_OPERAND
8677 (id_declarator->u.id.unqualified_name, 0),
8678 ctype))
8679 {
8680 error ("declaration of %qD as member of %qT",
8681 id_declarator->u.id.unqualified_name, ctype);
8682 return error_mark_node;
8683 }
88e95ee3
MM
8684 }
8685
8d08fdba 8686 /* Tell grokfndecl if it needs to set TREE_PUBLIC on the node. */
68642fb6 8687 function_context = (ctype != NULL_TREE) ?
4f1c5b7d 8688 decl_function_context (TYPE_MAIN_DECL (ctype)) : NULL_TREE;
e1467ff2
MM
8689 publicp = (! friendp || ! staticp)
8690 && function_context == NULL_TREE;
68642fb6 8691 decl = grokfndecl (ctype, type,
058b15c1
MM
8692 TREE_CODE (unqualified_id) != TEMPLATE_ID_EXPR
8693 ? unqualified_id : dname,
4546865e 8694 parms,
058b15c1 8695 unqualified_id,
e2537f2c 8696 virtualp, flags, memfn_quals, raises,
386b8a85 8697 friendp ? -1 : 0, friendp, publicp, inlinep,
27d6592c 8698 sfk,
037cc9c5 8699 funcdef_flag, template_count, in_namespace, attrlist);
20496fa2 8700 if (decl == NULL_TREE)
ba5719d9 8701 return error_mark_node;
6125f3be
DE
8702#if 0
8703 /* This clobbers the attrs stored in `decl' from `attrlist'. */
91d231cb
JM
8704 /* The decl and setting of decl_attr is also turned off. */
8705 decl = build_decl_attribute_variant (decl, decl_attr);
6125f3be 8706#endif
f0e01782 8707
cc804e51
MM
8708 /* [class.conv.ctor]
8709
8710 A constructor declared without the function-specifier
8711 explicit that can be called with a single parameter
8712 specifies a conversion from the type of its first
8713 parameter to the type of its class. Such a constructor
8714 is called a converting constructor. */
db5ae43f
MS
8715 if (explicitp == 2)
8716 DECL_NONCONVERTING_P (decl) = 1;
cc804e51
MM
8717 else if (DECL_CONSTRUCTOR_P (decl))
8718 {
8719 /* The constructor can be called with exactly one
8720 parameter if there is at least one parameter, and
8721 any subsequent parameters have default arguments.
e0fff4b3
JM
8722 Ignore any compiler-added parms. */
8723 tree arg_types = FUNCTION_FIRST_USER_PARMTYPE (decl);
cc804e51
MM
8724
8725 if (arg_types == void_list_node
68642fb6
UD
8726 || (arg_types
8727 && TREE_CHAIN (arg_types)
cc804e51
MM
8728 && TREE_CHAIN (arg_types) != void_list_node
8729 && !TREE_PURPOSE (TREE_CHAIN (arg_types))))
8730 DECL_NONCONVERTING_P (decl) = 1;
8731 }
8d08fdba
MS
8732 }
8733 else if (TREE_CODE (type) == METHOD_TYPE)
8734 {
faae18ab
MS
8735 /* We only get here for friend declarations of
8736 members of other classes. */
8d08fdba
MS
8737 /* All method decls are public, so tell grokfndecl to set
8738 TREE_PUBLIC, also. */
866eb556 8739 decl = grokfndecl (ctype, type,
058b15c1
MM
8740 TREE_CODE (unqualified_id) != TEMPLATE_ID_EXPR
8741 ? unqualified_id : dname,
4546865e 8742 parms,
058b15c1 8743 unqualified_id,
e2537f2c 8744 virtualp, flags, memfn_quals, raises,
27d6592c 8745 friendp ? -1 : 0, friendp, 1, 0, sfk,
c8094d83
MS
8746 funcdef_flag, template_count, in_namespace,
8747 attrlist);
f0e01782 8748 if (decl == NULL_TREE)
13dbe691 8749 return error_mark_node;
8d08fdba 8750 }
a9f46cbb 8751 else if (!staticp && !dependent_type_p (type)
d0f062fb 8752 && !COMPLETE_TYPE_P (complete_type (type))
8d08fdba
MS
8753 && (TREE_CODE (type) != ARRAY_TYPE || initialized == 0))
8754 {
058b15c1 8755 if (unqualified_id)
2d01edd7 8756 error ("field %qD has incomplete type", unqualified_id);
b7484fbe 8757 else
2d01edd7 8758 error ("name %qT has incomplete type", type);
8d08fdba
MS
8759
8760 /* If we're instantiating a template, tell them which
8761 instantiation made the field's type be incomplete. */
8762 if (current_class_type
8763 && TYPE_NAME (current_class_type)
d2e5ee5c 8764 && IDENTIFIER_TEMPLATE (TYPE_IDENTIFIER (current_class_type))
62d1db17
MM
8765 && declspecs->type
8766 && declspecs->type == type)
2d01edd7 8767 error (" in instantiation of template %qT",
0cbd7506 8768 current_class_type);
db5ae43f 8769
623c65f1 8770 return error_mark_node;
8d08fdba
MS
8771 }
8772 else
8773 {
8774 if (friendp)
8775 {
2d01edd7 8776 error ("%qE is neither function nor member function; "
0cbd7506 8777 "cannot be declared friend", unqualified_id);
8d08fdba
MS
8778 friendp = 0;
8779 }
8780 decl = NULL_TREE;
8781 }
8782
8783 if (friendp)
8784 {
8785 /* Friends are treated specially. */
8786 if (ctype == current_class_type)
59e7c84c 8787 ; /* We already issued a pedwarn. */
0cbd7506 8788 else if (decl && DECL_NAME (decl))
8db1028e
NS
8789 {
8790 if (template_class_depth (current_class_type) == 0)
8791 {
8792 decl = check_explicit_specialization
058b15c1 8793 (unqualified_id, decl, template_count,
357d956e 8794 2 * funcdef_flag + 4);
8db1028e
NS
8795 if (decl == error_mark_node)
8796 return error_mark_node;
8797 }
caf93cb0 8798
058b15c1 8799 decl = do_friend (ctype, unqualified_id, decl,
3db45ab5 8800 *attrlist, flags,
e2537f2c 8801 funcdef_flag);
8db1028e
NS
8802 return decl;
8803 }
8804 else
13dbe691 8805 return error_mark_node;
8d08fdba
MS
8806 }
8807
f4f206f4 8808 /* Structure field. It may not be a function, except for C++. */
8d08fdba
MS
8809
8810 if (decl == NULL_TREE)
8811 {
8d08fdba
MS
8812 if (initialized)
8813 {
3ac3d9ea
MM
8814 if (!staticp)
8815 {
8816 /* An attempt is being made to initialize a non-static
8817 member. But, from [class.mem]:
68642fb6 8818
3ac3d9ea
MM
8819 4 A member-declarator can contain a
8820 constant-initializer only if it declares a static
8821 member (_class.static_) of integral or enumeration
68642fb6 8822 type, see _class.static.data_.
3ac3d9ea
MM
8823
8824 This used to be relatively common practice, but
8825 the rest of the compiler does not correctly
8826 handle the initialization unless the member is
8827 static so we make it static below. */
2d01edd7 8828 pedwarn ("ISO C++ forbids initialization of member %qD",
058b15c1 8829 unqualified_id);
2d01edd7 8830 pedwarn ("making %qD static", unqualified_id);
3ac3d9ea
MM
8831 staticp = 1;
8832 }
8833
6ba89f8e
MM
8834 if (uses_template_parms (type))
8835 /* We'll check at instantiation time. */
8836 ;
058b15c1 8837 else if (check_static_variable_definition (unqualified_id,
6ba89f8e
MM
8838 type))
8839 /* If we just return the declaration, crashes
8840 will sometimes occur. We therefore return
72a93143
JM
8841 void_type_node, as if this was a friend
8842 declaration, to cause callers to completely
8843 ignore this declaration. */
13dbe691 8844 return error_mark_node;
8d08fdba
MS
8845 }
8846
3ac3d9ea 8847 if (staticp)
8d08fdba 8848 {
f18a14bc
MM
8849 /* C++ allows static class members. All other work
8850 for this is done by grokfield. */
058b15c1 8851 decl = build_lang_decl (VAR_DECL, unqualified_id, type);
4684cd27
MM
8852 set_linkage_for_static_data_member (decl);
8853 /* Even if there is an in-class initialization, DECL
8854 is considered undefined until an out-of-class
8855 definition is provided. */
8856 DECL_EXTERNAL (decl) = 1;
31a79236
JJ
8857
8858 if (thread_p)
8893239d 8859 DECL_TLS_MODEL (decl) = decl_default_tls_model (decl);
8d08fdba
MS
8860 }
8861 else
8862 {
058b15c1 8863 decl = build_decl (FIELD_DECL, unqualified_id, type);
2bf105ab 8864 DECL_NONADDRESSABLE_P (decl) = bitfield;
ab53bae2
AO
8865 if (bitfield && !unqualified_id)
8866 TREE_NO_WARNING (decl) = 1;
8867
1ff3c076 8868 if (storage_class == sc_mutable)
8d08fdba
MS
8869 {
8870 DECL_MUTABLE_P (decl) = 1;
1ff3c076 8871 storage_class = sc_none;
8d08fdba
MS
8872 }
8873 }
8874
3db45ab5 8875 bad_specifiers (decl, "field", virtualp,
e2537f2c 8876 memfn_quals != TYPE_UNQUALIFIED,
8d08fdba
MS
8877 inlinep, friendp, raises != NULL_TREE);
8878 }
8879 }
fd9aef9d
NS
8880 else if (TREE_CODE (type) == FUNCTION_TYPE
8881 || TREE_CODE (type) == METHOD_TYPE)
8d08fdba 8882 {
386b8a85 8883 tree original_name;
8d08fdba
MS
8884 int publicp = 0;
8885
058b15c1 8886 if (!unqualified_id)
13dbe691 8887 return error_mark_node;
8d08fdba 8888
058b15c1 8889 if (TREE_CODE (unqualified_id) == TEMPLATE_ID_EXPR)
386b8a85
JM
8890 original_name = dname;
8891 else
058b15c1 8892 original_name = unqualified_id;
386b8a85 8893
1ff3c076 8894 if (storage_class == sc_auto)
2d01edd7 8895 error ("storage class %<auto%> invalid for function %qs", name);
1ff3c076 8896 else if (storage_class == sc_register)
2d01edd7 8897 error ("storage class %<register%> invalid for function %qs", name);
1ff3c076 8898 else if (thread_p)
2d01edd7 8899 error ("storage class %<__thread%> invalid for function %qs", name);
8d08fdba
MS
8900
8901 /* Function declaration not at top level.
8902 Storage classes other than `extern' are not allowed
8903 and `extern' makes no difference. */
a9aedbc2 8904 if (! toplevel_bindings_p ()
1ff3c076 8905 && (storage_class == sc_static
62d1db17 8906 || declspecs->specs[(int)ds_inline])
8d08fdba 8907 && pedantic)
8926095f 8908 {
1ff3c076 8909 if (storage_class == sc_static)
2d01edd7 8910 pedwarn ("%<static%> specified invalid for function %qs "
0cbd7506 8911 "declared out of global scope", name);
8926095f 8912 else
2d01edd7 8913 pedwarn ("%<inline%> specifier invalid for function %qs "
0cbd7506 8914 "declared out of global scope", name);
8926095f 8915 }
68642fb6 8916
8d08fdba
MS
8917 if (ctype == NULL_TREE)
8918 {
8919 if (virtualp)
8920 {
2d01edd7 8921 error ("virtual non-class function %qs", name);
8d08fdba
MS
8922 virtualp = 0;
8923 }
8d08fdba 8924 }
4546865e
MM
8925 else if (TREE_CODE (type) == FUNCTION_TYPE && staticp < 2
8926 && !NEW_DELETE_OPNAME_P (original_name))
caf93cb0 8927 type = build_method_type_directly (ctype,
43dc123f
MM
8928 TREE_TYPE (type),
8929 TYPE_ARG_TYPES (type));
8d08fdba 8930
eb66be0e 8931 /* Record presence of `static'. */
faae18ab 8932 publicp = (ctype != NULL_TREE
1ff3c076
MM
8933 || storage_class == sc_extern
8934 || storage_class != sc_static);
8d08fdba 8935
058b15c1 8936 decl = grokfndecl (ctype, type, original_name, parms, unqualified_id,
e2537f2c 8937 virtualp, flags, memfn_quals, raises,
75650646 8938 1, friendp,
27d6592c 8939 publicp, inlinep, sfk, funcdef_flag,
037cc9c5 8940 template_count, in_namespace, attrlist);
f0e01782 8941 if (decl == NULL_TREE)
13dbe691 8942 return error_mark_node;
8d08fdba 8943
8d08fdba
MS
8944 if (staticp == 1)
8945 {
0e339752 8946 int invalid_static = 0;
8d08fdba
MS
8947
8948 /* Don't allow a static member function in a class, and forbid
8949 declaring main to be static. */
8950 if (TREE_CODE (type) == METHOD_TYPE)
8951 {
2d01edd7 8952 pedwarn ("cannot declare member function %qD to have "
0cbd7506 8953 "static linkage", decl);
0e339752 8954 invalid_static = 1;
8d08fdba 8955 }
8d08fdba
MS
8956 else if (current_function_decl)
8957 {
8958 /* FIXME need arm citation */
8251199e 8959 error ("cannot declare static function inside another function");
0e339752 8960 invalid_static = 1;
8d08fdba
MS
8961 }
8962
0e339752 8963 if (invalid_static)
8d08fdba
MS
8964 {
8965 staticp = 0;
1ff3c076 8966 storage_class = sc_none;
8d08fdba
MS
8967 }
8968 }
8d08fdba
MS
8969 }
8970 else
8971 {
8972 /* It's a variable. */
8973
8974 /* An uninitialized decl with `extern' is a reference. */
caf93cb0 8975 decl = grokvardecl (type, unqualified_id,
62d1db17 8976 declspecs,
68642fb6
UD
8977 initialized,
8978 (type_quals & TYPE_QUAL_CONST) != 0,
9e259dd1 8979 ctype ? ctype : in_namespace);
3db45ab5 8980 bad_specifiers (decl, "variable", virtualp,
e2537f2c 8981 memfn_quals != TYPE_UNQUALIFIED,
8d08fdba
MS
8982 inlinep, friendp, raises != NULL_TREE);
8983
8984 if (ctype)
8985 {
f0e01782 8986 DECL_CONTEXT (decl) = ctype;
8d08fdba
MS
8987 if (staticp == 1)
8988 {
0cbd7506
MS
8989 pedwarn ("%<static%> may not be used when defining "
8990 "(as opposed to declaring) a static data member");
8991 staticp = 0;
1ff3c076 8992 storage_class = sc_none;
8d08fdba 8993 }
1ff3c076 8994 if (storage_class == sc_register && TREE_STATIC (decl))
b7484fbe 8995 {
2d01edd7 8996 error ("static member %qD declared %<register%>", decl);
1ff3c076 8997 storage_class = sc_none;
b7484fbe 8998 }
1ff3c076 8999 if (storage_class == sc_extern && pedantic)
8d08fdba 9000 {
0cbd7506
MS
9001 pedwarn ("cannot explicitly declare member %q#D to have "
9002 "extern linkage",
9003 decl);
1ff3c076 9004 storage_class = sc_none;
8d08fdba
MS
9005 }
9006 }
9007 }
9008
8d08fdba
MS
9009 /* Record `register' declaration for warnings on &
9010 and in case doing stupid register allocation. */
9011
1ff3c076 9012 if (storage_class == sc_register)
8d08fdba 9013 DECL_REGISTER (decl) = 1;
1ff3c076 9014 else if (storage_class == sc_extern)
8926095f 9015 DECL_THIS_EXTERN (decl) = 1;
1ff3c076 9016 else if (storage_class == sc_static)
faae18ab
MS
9017 DECL_THIS_STATIC (decl) = 1;
9018
adecb3f4
MM
9019 /* Record constancy and volatility. There's no need to do this
9020 when processing a template; we'll do this for the instantiated
9021 declaration based on the type of DECL. */
9022 if (!processing_template_decl)
9804209d 9023 cp_apply_type_quals_to_decl (type_quals, decl);
8d08fdba 9024
1891dec4
DM
9025 if (set_no_warning)
9026 TREE_NO_WARNING (decl) = 1;
9027
8d08fdba
MS
9028 return decl;
9029 }
9030}
9031\f
f181d4ae
MM
9032/* Subroutine of start_function. Ensure that each of the parameter
9033 types (as listed in PARMS) is complete, as is required for a
9034 function definition. */
e92cc029 9035
8d08fdba 9036static void
11f6b451 9037require_complete_types_for_parms (tree parms)
8d08fdba 9038{
07c88314 9039 for (; parms; parms = TREE_CHAIN (parms))
8d08fdba 9040 {
c9387915
GB
9041 if (dependent_type_p (TREE_TYPE (parms)))
9042 continue;
dff1b563
VR
9043 if (!VOID_TYPE_P (TREE_TYPE (parms))
9044 && complete_type_or_else (TREE_TYPE (parms), parms))
753225c1 9045 {
7523dc31 9046 relayout_decl (parms);
753225c1
JM
9047 DECL_ARG_TYPE (parms) = type_passed_as (TREE_TYPE (parms));
9048 }
dff1b563
VR
9049 else
9050 /* grokparms or complete_type_or_else will have already issued
9051 an error. */
9052 TREE_TYPE (parms) = error_mark_node;
8d08fdba
MS
9053 }
9054}
9055
838dfd8a 9056/* Returns nonzero if T is a local variable. */
297e73d8 9057
46e8c075 9058int
ac7d7749 9059local_variable_p (const_tree t)
297e73d8 9060{
68642fb6 9061 if ((TREE_CODE (t) == VAR_DECL
297e73d8
MM
9062 /* A VAR_DECL with a context that is a _TYPE is a static data
9063 member. */
9064 && !TYPE_P (CP_DECL_CONTEXT (t))
9065 /* Any other non-local variable must be at namespace scope. */
46e8c075 9066 && !DECL_NAMESPACE_SCOPE_P (t))
297e73d8 9067 || (TREE_CODE (t) == PARM_DECL))
46e8c075 9068 return 1;
297e73d8 9069
46e8c075
MM
9070 return 0;
9071}
9072
46e8c075
MM
9073/* Like local_variable_p, but suitable for use as a tree-walking
9074 function. */
9075
9076static tree
44de5aeb
RK
9077local_variable_p_walkfn (tree *tp, int *walk_subtrees,
9078 void *data ATTRIBUTE_UNUSED)
46e8c075 9079{
44de5aeb
RK
9080 if (local_variable_p (*tp) && !DECL_ARTIFICIAL (*tp))
9081 return *tp;
9082 else if (TYPE_P (*tp))
9083 *walk_subtrees = 0;
9084
9085 return NULL_TREE;
297e73d8
MM
9086}
9087
44de5aeb 9088
297e73d8 9089/* Check that ARG, which is a default-argument expression for a
0e339752 9090 parameter DECL, is valid. Returns ARG, or ERROR_MARK_NODE, if
297e73d8
MM
9091 something goes wrong. DECL may also be a _TYPE node, rather than a
9092 DECL, if there is no DECL available. */
9093
9094tree
11f6b451 9095check_default_argument (tree decl, tree arg)
297e73d8
MM
9096{
9097 tree var;
9098 tree decl_type;
9099
9100 if (TREE_CODE (arg) == DEFAULT_ARG)
9101 /* We get a DEFAULT_ARG when looking at an in-class declaration
9102 with a default argument. Ignore the argument for now; we'll
9103 deal with it after the class is complete. */
9104 return arg;
9105
297e73d8
MM
9106 if (TYPE_P (decl))
9107 {
9108 decl_type = decl;
9109 decl = NULL_TREE;
9110 }
9111 else
9112 decl_type = TREE_TYPE (decl);
9113
68642fb6 9114 if (arg == error_mark_node
297e73d8
MM
9115 || decl == error_mark_node
9116 || TREE_TYPE (arg) == error_mark_node
9117 || decl_type == error_mark_node)
9118 /* Something already went wrong. There's no need to check
9119 further. */
9120 return error_mark_node;
9121
9122 /* [dcl.fct.default]
68642fb6 9123
297e73d8
MM
9124 A default argument expression is implicitly converted to the
9125 parameter type. */
9126 if (!TREE_TYPE (arg)
30f86ec3 9127 || !can_convert_arg (decl_type, TREE_TYPE (arg), arg, LOOKUP_NORMAL))
297e73d8
MM
9128 {
9129 if (decl)
2d01edd7 9130 error ("default argument for %q#D has type %qT",
0cbd7506 9131 decl, TREE_TYPE (arg));
297e73d8 9132 else
2d01edd7 9133 error ("default argument for parameter of type %qT has type %qT",
0cbd7506 9134 decl_type, TREE_TYPE (arg));
297e73d8
MM
9135
9136 return error_mark_node;
9137 }
9138
9139 /* [dcl.fct.default]
9140
9141 Local variables shall not be used in default argument
68642fb6 9142 expressions.
297e73d8
MM
9143
9144 The keyword `this' shall not be used in a default argument of a
9145 member function. */
14588106 9146 var = cp_walk_tree_without_duplicates (&arg, local_variable_p_walkfn, NULL);
297e73d8
MM
9147 if (var)
9148 {
2d01edd7 9149 error ("default argument %qE uses local variable %qD", arg, var);
297e73d8
MM
9150 return error_mark_node;
9151 }
9152
9153 /* All is well. */
9154 return arg;
9155}
9156
8d08fdba
MS
9157/* Decode the list of parameter types for a function type.
9158 Given the list of things declared inside the parens,
9159 return a list of types.
9160
058b15c1
MM
9161 If this parameter does not end with an ellipsis, we append
9162 void_list_node.
5cce22b6 9163
4546865e 9164 *PARMS is set to the chain of PARM_DECLs created. */
8d08fdba
MS
9165
9166static tree
62d1db17 9167grokparms (cp_parameter_declarator *first_parm, tree *parms)
8d08fdba
MS
9168{
9169 tree result = NULL_TREE;
9170 tree decls = NULL_TREE;
058b15c1 9171 int ellipsis = !first_parm || first_parm->ellipsis_p;
62d1db17 9172 cp_parameter_declarator *parm;
5cce22b6 9173 int any_error = 0;
0d83bf5a 9174 struct pointer_set_t *unique_decls = pointer_set_create ();
8d08fdba 9175
058b15c1 9176 for (parm = first_parm; parm != NULL; parm = parm->next)
8d08fdba 9177 {
0657c69c 9178 tree type = NULL_TREE;
058b15c1 9179 tree init = parm->default_argument;
62d1db17 9180 tree attrs;
058b15c1 9181 tree decl;
8d08fdba 9182
058b15c1 9183 if (parm == no_parameters)
0cbd7506 9184 break;
8d08fdba 9185
62d1db17
MM
9186 attrs = parm->decl_specifiers.attributes;
9187 parm->decl_specifiers.attributes = NULL_TREE;
9188 decl = grokdeclarator (parm->declarator, &parm->decl_specifiers,
98884b26 9189 PARM, init != NULL_TREE, &attrs);
5cce22b6 9190 if (! decl || TREE_TYPE (decl) == error_mark_node)
0cbd7506 9191 continue;
d363e7bf 9192
98884b26
JM
9193 if (attrs)
9194 cplus_decl_attributes (&decl, attrs, 0);
9195
5cce22b6
NS
9196 type = TREE_TYPE (decl);
9197 if (VOID_TYPE_P (type))
0cbd7506
MS
9198 {
9199 if (same_type_p (type, void_type_node)
e7e53192 9200 && DECL_SELF_REFERENCE_P (type)
0cbd7506
MS
9201 && !DECL_NAME (decl) && !result && !parm->next && !ellipsis)
9202 /* this is a parmlist of `(void)', which is ok. */
9203 break;
9204 cxx_incomplete_type_error (decl, type);
0657c69c
MM
9205 /* It's not a good idea to actually create parameters of
9206 type `void'; other parts of the compiler assume that a
9207 void type terminates the parameter list. */
04f3dc2b 9208 type = error_mark_node;
0657c69c 9209 TREE_TYPE (decl) = error_mark_node;
0cbd7506 9210 }
8d08fdba 9211
d363e7bf 9212 if (type != error_mark_node)
04f3dc2b
MM
9213 {
9214 /* Top-level qualifiers on the parameters are
9215 ignored for function types. */
79a1a736 9216 type = cp_build_qualified_type (type, 0);
04f3dc2b
MM
9217 if (TREE_CODE (type) == METHOD_TYPE)
9218 {
2d01edd7 9219 error ("parameter %qD invalidly declared method type", decl);
04f3dc2b
MM
9220 type = build_pointer_type (type);
9221 TREE_TYPE (decl) = type;
9222 }
04f3dc2b 9223 else if (abstract_virtuals_error (decl, type))
a1c65f9f 9224 any_error = 1; /* Seems like a good idea. */
04f3dc2b
MM
9225 else if (POINTER_TYPE_P (type))
9226 {
9227 /* [dcl.fct]/6, parameter types cannot contain pointers
9228 (references) to arrays of unknown bound. */
98979fe0
NS
9229 tree t = TREE_TYPE (type);
9230 int ptr = TYPE_PTR_P (type);
9231
0cbd7506
MS
9232 while (1)
9233 {
9234 if (TYPE_PTR_P (t))
9235 ptr = 1;
9236 else if (TREE_CODE (t) != ARRAY_TYPE)
9237 break;
9238 else if (!TYPE_DOMAIN (t))
9239 break;
9240 t = TREE_TYPE (t);
9241 }
04f3dc2b 9242 if (TREE_CODE (t) == ARRAY_TYPE)
2d01edd7 9243 error ("parameter %qD includes %s to array of unknown "
0cbd7506
MS
9244 "bound %qT",
9245 decl, ptr ? "pointer" : "reference", t);
04f3dc2b
MM
9246 }
9247
c3ee4651 9248 if (any_error)
04f3dc2b 9249 init = NULL_TREE;
c3ee4651
NS
9250 else if (init && !processing_template_decl)
9251 init = check_default_argument (decl, init);
04f3dc2b 9252 }
8d08fdba 9253
5d80a306
DG
9254 if (TREE_CODE (decl) == PARM_DECL
9255 && FUNCTION_PARAMETER_PACK_P (decl)
9256 && parm->next)
9257 error ("parameter packs must be at the end of the parameter list");
9258
0d83bf5a
SB
9259 if (DECL_NAME (decl))
9260 {
9261 if (pointer_set_contains (unique_decls, DECL_NAME (decl)))
631ee856 9262 error ("multiple parameters named %qE", DECL_NAME (decl));
0d83bf5a
SB
9263 else
9264 pointer_set_insert (unique_decls, DECL_NAME (decl));
9265 }
9266
5cce22b6
NS
9267 TREE_CHAIN (decl) = decls;
9268 decls = decl;
0657c69c 9269 result = tree_cons (init, type, result);
8d08fdba 9270 }
5cce22b6
NS
9271 decls = nreverse (decls);
9272 result = nreverse (result);
9273 if (!ellipsis)
9274 result = chainon (result, void_list_node);
4546865e 9275 *parms = decls;
8d08fdba 9276
0d83bf5a 9277 pointer_set_destroy (unique_decls);
8d08fdba
MS
9278 return result;
9279}
42976354 9280
8d08fdba 9281\f
271e6f02
NS
9282/* D is a constructor or overloaded `operator='.
9283
9284 Let T be the class in which D is declared. Then, this function
9285 returns:
9286
9287 -1 if D's is an ill-formed constructor or copy assignment operator
9288 whose first parameter is of type `T'.
9289 0 if D is not a copy constructor or copy assignment
9290 operator.
9291 1 if D is a copy constructor or copy assignment operator whose
9292 first parameter is a reference to const qualified T.
9293 2 if D is a copy constructor or copy assignment operator whose
9294 first parameter is a reference to non-const qualified T.
9295
9296 This function can be used as a predicate. Positive values indicate
838dfd8a 9297 a copy constructor and nonzero values indicate a copy assignment
4f1c5b7d
MM
9298 operator. */
9299
c11b6f21 9300int
58f9752a 9301copy_fn_p (const_tree d)
c11b6f21 9302{
271e6f02
NS
9303 tree args;
9304 tree arg_type;
9305 int result = 1;
caf93cb0 9306
398cd199 9307 gcc_assert (DECL_FUNCTION_MEMBER_P (d));
4f1c5b7d 9308
7137605e
MM
9309 if (TREE_CODE (d) == TEMPLATE_DECL
9310 || (DECL_TEMPLATE_INFO (d)
9311 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (d))))
271e6f02
NS
9312 /* Instantiations of template member functions are never copy
9313 functions. Note that member functions of templated classes are
9314 represented as template functions internally, and we must
9315 accept those as copy functions. */
9316 return 0;
caf93cb0 9317
271e6f02
NS
9318 args = FUNCTION_FIRST_USER_PARMTYPE (d);
9319 if (!args)
4f1c5b7d
MM
9320 return 0;
9321
271e6f02 9322 arg_type = TREE_VALUE (args);
139a78c7
VR
9323 if (arg_type == error_mark_node)
9324 return 0;
271e6f02
NS
9325
9326 if (TYPE_MAIN_VARIANT (arg_type) == DECL_CONTEXT (d))
9327 {
9328 /* Pass by value copy assignment operator. */
9329 result = -1;
9330 }
9331 else if (TREE_CODE (arg_type) == REFERENCE_TYPE
8af2fec4 9332 && !TYPE_REF_IS_RVALUE (arg_type)
271e6f02
NS
9333 && TYPE_MAIN_VARIANT (TREE_TYPE (arg_type)) == DECL_CONTEXT (d))
9334 {
9335 if (CP_TYPE_CONST_P (TREE_TYPE (arg_type)))
9336 result = 2;
9337 }
9338 else
9339 return 0;
caf93cb0 9340
271e6f02
NS
9341 args = TREE_CHAIN (args);
9342
9343 if (args && args != void_list_node && !TREE_PURPOSE (args))
9344 /* There are more non-optional args. */
9345 return 0;
9346
9347 return result;
9348}
9349
8af2fec4
RY
9350/* D is a constructor or overloaded `operator='.
9351
9352 Let T be the class in which D is declared. Then, this function
9353 returns true when D is a move constructor or move assignment
9354 operator, false otherwise. */
9355
9356bool
58f9752a 9357move_fn_p (const_tree d)
8af2fec4
RY
9358{
9359 tree args;
9360 tree arg_type;
9361 bool result = false;
9362
9363 gcc_assert (DECL_FUNCTION_MEMBER_P (d));
9364
c1ae8be5
SM
9365 if (cxx_dialect == cxx98)
9366 /* There are no move constructors if we are in C++98 mode. */
8af2fec4
RY
9367 return false;
9368
9369 if (TREE_CODE (d) == TEMPLATE_DECL
9370 || (DECL_TEMPLATE_INFO (d)
9371 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (d))))
9372 /* Instantiations of template member functions are never copy
9373 functions. Note that member functions of templated classes are
9374 represented as template functions internally, and we must
9375 accept those as copy functions. */
9376 return 0;
9377
9378 args = FUNCTION_FIRST_USER_PARMTYPE (d);
9379 if (!args)
9380 return 0;
9381
9382 arg_type = TREE_VALUE (args);
9383 if (arg_type == error_mark_node)
9384 return 0;
9385
9386 if (TREE_CODE (arg_type) == REFERENCE_TYPE
9387 && TYPE_REF_IS_RVALUE (arg_type)
9388 && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (arg_type)),
9389 DECL_CONTEXT (d)))
9390 result = true;
9391
9392 args = TREE_CHAIN (args);
9393
9394 if (args && args != void_list_node && !TREE_PURPOSE (args))
9395 /* There are more non-optional args. */
9396 return false;
9397
9398 return result;
9399}
9400
271e6f02
NS
9401/* Remember any special properties of member function DECL. */
9402
11f6b451 9403void grok_special_member_properties (tree decl)
271e6f02 9404{
7137605e
MM
9405 tree class_type;
9406
6d69fe27 9407 if (!DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
7137605e
MM
9408 return;
9409
9410 class_type = DECL_CONTEXT (decl);
9411 if (DECL_CONSTRUCTOR_P (decl))
271e6f02
NS
9412 {
9413 int ctor = copy_fn_p (decl);
caf93cb0 9414
7137605e
MM
9415 TYPE_HAS_CONSTRUCTOR (class_type) = 1;
9416
271e6f02
NS
9417 if (ctor > 0)
9418 {
9419 /* [class.copy]
caf93cb0 9420
0cbd7506
MS
9421 A non-template constructor for class X is a copy
9422 constructor if its first parameter is of type X&, const
9423 X&, volatile X& or const volatile X&, and either there
9424 are no other parameters or else all other parameters have
9425 default arguments. */
7137605e 9426 TYPE_HAS_INIT_REF (class_type) = 1;
271e6f02 9427 if (ctor > 1)
7137605e 9428 TYPE_HAS_CONST_INIT_REF (class_type) = 1;
271e6f02
NS
9429 }
9430 else if (sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (decl)))
7137605e 9431 TYPE_HAS_DEFAULT_CONSTRUCTOR (class_type) = 1;
271e6f02
NS
9432 }
9433 else if (DECL_OVERLOADED_OPERATOR_P (decl) == NOP_EXPR)
9434 {
9435 /* [class.copy]
caf93cb0 9436
0cbd7506
MS
9437 A non-template assignment operator for class X is a copy
9438 assignment operator if its parameter is of type X, X&, const
9439 X&, volatile X& or const volatile X&. */
caf93cb0 9440
271e6f02 9441 int assop = copy_fn_p (decl);
caf93cb0 9442
271e6f02
NS
9443 if (assop)
9444 {
7137605e 9445 TYPE_HAS_ASSIGN_REF (class_type) = 1;
271e6f02 9446 if (assop != 1)
7137605e 9447 TYPE_HAS_CONST_ASSIGN_REF (class_type) = 1;
271e6f02
NS
9448 }
9449 }
c11b6f21
MS
9450}
9451
271e6f02
NS
9452/* Check a constructor DECL has the correct form. Complains
9453 if the class has a constructor of the form X(X). */
e92cc029 9454
a0a33927 9455int
58f9752a 9456grok_ctor_properties (const_tree ctype, const_tree decl)
8d08fdba 9457{
271e6f02
NS
9458 int ctor_parm = copy_fn_p (decl);
9459
9460 if (ctor_parm < 0)
9461 {
9462 /* [class.copy]
caf93cb0 9463
0cbd7506
MS
9464 A declaration of a constructor for a class X is ill-formed if
9465 its first parameter is of type (optionally cv-qualified) X
9466 and either there are no other parameters or else all other
9467 parameters have default arguments.
9468
9469 We *don't* complain about member template instantiations that
9470 have this form, though; they can occur as we try to decide
9471 what constructor to use during overload resolution. Since
9472 overload resolution will never prefer such a constructor to
9473 the non-template copy constructor (which is either explicitly
9474 or implicitly defined), there's no need to worry about their
9475 existence. Theoretically, they should never even be
9476 instantiated, but that's hard to forestall. */
2d01edd7 9477 error ("invalid constructor; you probably meant %<%T (const %T&)%>",
0b41abe6 9478 ctype, ctype);
0b41abe6 9479 return 0;
8d08fdba 9480 }
caf93cb0 9481
a0a33927 9482 return 1;
8d08fdba
MS
9483}
9484
596ea4e5 9485/* An operator with this code is unary, but can also be binary. */
e92cc029 9486
a28e3c7f 9487static int
11f6b451 9488ambi_op_p (enum tree_code code)
8d08fdba 9489{
596ea4e5
AS
9490 return (code == INDIRECT_REF
9491 || code == ADDR_EXPR
392e3d51 9492 || code == UNARY_PLUS_EXPR
596ea4e5
AS
9493 || code == NEGATE_EXPR
9494 || code == PREINCREMENT_EXPR
9495 || code == PREDECREMENT_EXPR);
8d08fdba
MS
9496}
9497
9498/* An operator with this name can only be unary. */
e92cc029 9499
a28e3c7f 9500static int
11f6b451 9501unary_op_p (enum tree_code code)
8d08fdba 9502{
596ea4e5
AS
9503 return (code == TRUTH_NOT_EXPR
9504 || code == BIT_NOT_EXPR
9505 || code == COMPONENT_REF
9506 || code == TYPE_EXPR);
8d08fdba
MS
9507}
9508
f2a79152 9509/* DECL is a declaration for an overloaded operator. If COMPLAIN is true,
4b0d3cbe 9510 errors are issued for invalid declarations. */
e92cc029 9511
398cd199 9512bool
7e45bd18 9513grok_op_properties (tree decl, bool complain)
8d08fdba
MS
9514{
9515 tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
596ea4e5 9516 tree argtype;
8d08fdba
MS
9517 int methodp = (TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE);
9518 tree name = DECL_NAME (decl);
596ea4e5
AS
9519 enum tree_code operator_code;
9520 int arity;
64844139 9521 bool ellipsis_p;
7e45bd18 9522 tree class_type;
596ea4e5 9523
64844139 9524 /* Count the number of arguments and check for ellipsis. */
596ea4e5
AS
9525 for (argtype = argtypes, arity = 0;
9526 argtype && argtype != void_list_node;
9527 argtype = TREE_CHAIN (argtype))
9528 ++arity;
64844139 9529 ellipsis_p = !argtype;
8d08fdba 9530
7e45bd18
MM
9531 class_type = DECL_CONTEXT (decl);
9532 if (class_type && !CLASS_TYPE_P (class_type))
9533 class_type = NULL_TREE;
8d08fdba 9534
596ea4e5
AS
9535 if (DECL_CONV_FN_P (decl))
9536 operator_code = TYPE_EXPR;
9537 else
9538 do
9539 {
0c918ce5
MM
9540#define DEF_OPERATOR(NAME, CODE, MANGLING, ARITY, ASSN_P) \
9541 if (ansi_opname (CODE) == name) \
9542 { \
75ac8dec 9543 operator_code = (CODE); \
0c918ce5
MM
9544 break; \
9545 } \
9546 else if (ansi_assopname (CODE) == name) \
9547 { \
75ac8dec 9548 operator_code = (CODE); \
0c918ce5
MM
9549 DECL_ASSIGNMENT_OPERATOR_P (decl) = 1; \
9550 break; \
596ea4e5
AS
9551 }
9552
9553#include "operators.def"
9554#undef DEF_OPERATOR
9555
8dc2b103 9556 gcc_unreachable ();
596ea4e5
AS
9557 }
9558 while (0);
50bc768d 9559 gcc_assert (operator_code != LAST_CPLUS_TREE_CODE);
596ea4e5
AS
9560 SET_OVERLOADED_OPERATOR_CODE (decl, operator_code);
9561
7e45bd18
MM
9562 if (class_type)
9563 switch (operator_code)
9564 {
9565 case NEW_EXPR:
9566 TYPE_HAS_NEW_OPERATOR (class_type) = 1;
9567 break;
5362b086 9568
7e45bd18
MM
9569 case DELETE_EXPR:
9570 TYPE_GETS_DELETE (class_type) |= 1;
9571 break;
5362b086 9572
7e45bd18
MM
9573 case VEC_NEW_EXPR:
9574 TYPE_HAS_ARRAY_NEW_OPERATOR (class_type) = 1;
9575 break;
5362b086 9576
7e45bd18
MM
9577 case VEC_DELETE_EXPR:
9578 TYPE_GETS_DELETE (class_type) |= 2;
9579 break;
596ea4e5 9580
7e45bd18
MM
9581 default:
9582 break;
9583 }
596ea4e5 9584
3143d517
GB
9585 /* [basic.std.dynamic.allocation]/1:
9586
9587 A program is ill-formed if an allocation function is declared
9588 in a namespace scope other than global scope or declared static
9589 in global scope.
9590
9591 The same also holds true for deallocation functions. */
9592 if (operator_code == NEW_EXPR || operator_code == VEC_NEW_EXPR
9593 || operator_code == DELETE_EXPR || operator_code == VEC_DELETE_EXPR)
9594 {
9595 if (DECL_NAMESPACE_SCOPE_P (decl))
9596 {
9597 if (CP_DECL_CONTEXT (decl) != global_namespace)
398cd199
VR
9598 {
9599 error ("%qD may not be declared within a namespace", decl);
9600 return false;
9601 }
3143d517 9602 else if (!TREE_PUBLIC (decl))
398cd199
VR
9603 {
9604 error ("%qD may not be declared as static", decl);
9605 return false;
9606 }
3143d517
GB
9607 }
9608 }
9609
596ea4e5 9610 if (operator_code == NEW_EXPR || operator_code == VEC_NEW_EXPR)
4546865e 9611 TREE_TYPE (decl) = coerce_new_type (TREE_TYPE (decl));
596ea4e5 9612 else if (operator_code == DELETE_EXPR || operator_code == VEC_DELETE_EXPR)
4546865e 9613 TREE_TYPE (decl) = coerce_delete_type (TREE_TYPE (decl));
8d08fdba
MS
9614 else
9615 {
9616 /* An operator function must either be a non-static member function
9617 or have at least one parameter of a class, a reference to a class,
9618 an enumeration, or a reference to an enumeration. 13.4.0.6 */
700f8a87 9619 if (! methodp || DECL_STATIC_FUNCTION_P (decl))
8d08fdba 9620 {
596ea4e5
AS
9621 if (operator_code == TYPE_EXPR
9622 || operator_code == CALL_EXPR
9623 || operator_code == COMPONENT_REF
9624 || operator_code == ARRAY_REF
9625 || operator_code == NOP_EXPR)
f2a79152
PC
9626 {
9627 error ("%qD must be a nonstatic member function", decl);
398cd199 9628 return false;
f2a79152 9629 }
8d08fdba
MS
9630 else
9631 {
4b0d3cbe 9632 tree p;
8d08fdba 9633
700f8a87 9634 if (DECL_STATIC_FUNCTION_P (decl))
f2a79152
PC
9635 {
9636 error ("%qD must be either a non-static member "
9637 "function or a non-member function", decl);
398cd199 9638 return false;
f2a79152 9639 }
700f8a87 9640
4b0d3cbe
MM
9641 for (p = argtypes; p && p != void_list_node; p = TREE_CHAIN (p))
9642 {
9643 tree arg = non_reference (TREE_VALUE (p));
85990800 9644 if (arg == error_mark_node)
398cd199 9645 return false;
85990800 9646
4b0d3cbe
MM
9647 /* IS_AGGR_TYPE, rather than CLASS_TYPE_P, is used
9648 because these checks are performed even on
9649 template functions. */
9650 if (IS_AGGR_TYPE (arg) || TREE_CODE (arg) == ENUMERAL_TYPE)
9651 break;
9652 }
9653
9654 if (!p || p == void_list_node)
9655 {
398cd199
VR
9656 if (complain)
9657 error ("%qD must have an argument of class or "
9658 "enumerated type", decl);
9659 return false;
4b0d3cbe 9660 }
8d08fdba
MS
9661 }
9662 }
68642fb6 9663
4b0d3cbe
MM
9664 /* There are no restrictions on the arguments to an overloaded
9665 "operator ()". */
596ea4e5 9666 if (operator_code == CALL_EXPR)
398cd199 9667 return true;
8d08fdba 9668
7e45bd18 9669 /* Warn about conversion operators that will never be used. */
c8094d83 9670 if (IDENTIFIER_TYPENAME_P (name)
7e45bd18
MM
9671 && ! DECL_TEMPLATE_INFO (decl)
9672 && warn_conversion
9673 /* Warn only declaring the function; there is no need to
9674 warn again about out-of-class definitions. */
9675 && class_type == current_class_type)
a0a33927
MS
9676 {
9677 tree t = TREE_TYPE (name);
7e45bd18
MM
9678 int ref = (TREE_CODE (t) == REFERENCE_TYPE);
9679 const char *what = 0;
5362b086 9680
7e45bd18
MM
9681 if (ref)
9682 t = TYPE_MAIN_VARIANT (TREE_TYPE (t));
a0a33927 9683
7e45bd18
MM
9684 if (TREE_CODE (t) == VOID_TYPE)
9685 what = "void";
9686 else if (class_type)
9687 {
9688 if (t == class_type)
a0a33927 9689 what = "the same type";
9a3b49ac 9690 /* Don't force t to be complete here. */
a0a33927 9691 else if (IS_AGGR_TYPE (t)
d0f062fb 9692 && COMPLETE_TYPE_P (t)
7e45bd18 9693 && DERIVED_FROM_P (t, class_type))
a0a33927 9694 what = "a base class";
a0a33927 9695 }
7e45bd18
MM
9696
9697 if (what)
b323323f 9698 warning (OPT_Wconversion, "conversion to %s%s will never use a type "
7e45bd18
MM
9699 "conversion operator",
9700 ref ? "a reference to " : "", what);
a0a33927 9701 }
64844139 9702
271e6f02 9703 if (operator_code == COND_EXPR)
8d08fdba
MS
9704 {
9705 /* 13.4.0.3 */
33bd39a2 9706 error ("ISO C++ prohibits overloading operator ?:");
398cd199 9707 return false;
68642fb6 9708 }
64844139 9709 else if (ellipsis_p)
398cd199
VR
9710 {
9711 error ("%qD must not have variable number of arguments", decl);
9712 return false;
9713 }
596ea4e5 9714 else if (ambi_op_p (operator_code))
8d08fdba 9715 {
596ea4e5
AS
9716 if (arity == 1)
9717 /* We pick the one-argument operator codes by default, so
9718 we don't have to change anything. */
9719 ;
9720 else if (arity == 2)
8d08fdba 9721 {
596ea4e5
AS
9722 /* If we thought this was a unary operator, we now know
9723 it to be a binary operator. */
9724 switch (operator_code)
9725 {
9726 case INDIRECT_REF:
9727 operator_code = MULT_EXPR;
9728 break;
9729
9730 case ADDR_EXPR:
9731 operator_code = BIT_AND_EXPR;
9732 break;
9733
392e3d51 9734 case UNARY_PLUS_EXPR:
596ea4e5
AS
9735 operator_code = PLUS_EXPR;
9736 break;
9737
9738 case NEGATE_EXPR:
9739 operator_code = MINUS_EXPR;
9740 break;
9741
9742 case PREINCREMENT_EXPR:
9743 operator_code = POSTINCREMENT_EXPR;
9744 break;
9745
9746 case PREDECREMENT_EXPR:
655dc6ee 9747 operator_code = POSTDECREMENT_EXPR;
596ea4e5
AS
9748 break;
9749
9750 default:
8dc2b103 9751 gcc_unreachable ();
596ea4e5
AS
9752 }
9753
9754 SET_OVERLOADED_OPERATOR_CODE (decl, operator_code);
9755
9756 if ((operator_code == POSTINCREMENT_EXPR
9757 || operator_code == POSTDECREMENT_EXPR)
5156628f 9758 && ! processing_template_decl
007e5fea 9759 && ! same_type_p (TREE_VALUE (TREE_CHAIN (argtypes)), integer_type_node))
8d08fdba
MS
9760 {
9761 if (methodp)
2d01edd7 9762 error ("postfix %qD must take %<int%> as its argument",
398cd199 9763 decl);
8d08fdba 9764 else
398cd199
VR
9765 error ("postfix %qD must take %<int%> as its second "
9766 "argument", decl);
9767 return false;
8d08fdba
MS
9768 }
9769 }
9770 else
9771 {
9772 if (methodp)
2d01edd7 9773 error ("%qD must take either zero or one argument", decl);
8d08fdba 9774 else
2d01edd7 9775 error ("%qD must take either one or two arguments", decl);
398cd199 9776 return false;
8d08fdba 9777 }
824b9a4c
MS
9778
9779 /* More Effective C++ rule 6. */
eb448459 9780 if (warn_ecpp
596ea4e5
AS
9781 && (operator_code == POSTINCREMENT_EXPR
9782 || operator_code == POSTDECREMENT_EXPR
9783 || operator_code == PREINCREMENT_EXPR
9784 || operator_code == PREDECREMENT_EXPR))
824b9a4c
MS
9785 {
9786 tree arg = TREE_VALUE (argtypes);
9787 tree ret = TREE_TYPE (TREE_TYPE (decl));
9788 if (methodp || TREE_CODE (arg) == REFERENCE_TYPE)
9789 arg = TREE_TYPE (arg);
9790 arg = TYPE_MAIN_VARIANT (arg);
596ea4e5
AS
9791 if (operator_code == PREINCREMENT_EXPR
9792 || operator_code == PREDECREMENT_EXPR)
824b9a4c
MS
9793 {
9794 if (TREE_CODE (ret) != REFERENCE_TYPE
3bfdc719
MM
9795 || !same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (ret)),
9796 arg))
b323323f 9797 warning (OPT_Weffc__, "prefix %qD should return %qT", decl,
0cbd7506 9798 build_reference_type (arg));
824b9a4c
MS
9799 }
9800 else
9801 {
3bfdc719 9802 if (!same_type_p (TYPE_MAIN_VARIANT (ret), arg))
b323323f 9803 warning (OPT_Weffc__, "postfix %qD should return %qT", decl, arg);
824b9a4c
MS
9804 }
9805 }
8d08fdba 9806 }
596ea4e5 9807 else if (unary_op_p (operator_code))
8d08fdba 9808 {
596ea4e5 9809 if (arity != 1)
8d08fdba
MS
9810 {
9811 if (methodp)
2d01edd7 9812 error ("%qD must take %<void%>", decl);
8d08fdba 9813 else
2d01edd7 9814 error ("%qD must take exactly one argument", decl);
398cd199 9815 return false;
8d08fdba
MS
9816 }
9817 }
596ea4e5 9818 else /* if (binary_op_p (operator_code)) */
8d08fdba 9819 {
596ea4e5 9820 if (arity != 2)
8d08fdba
MS
9821 {
9822 if (methodp)
2d01edd7 9823 error ("%qD must take exactly one argument", decl);
8d08fdba 9824 else
2d01edd7 9825 error ("%qD must take exactly two arguments", decl);
398cd199 9826 return false;
8d08fdba 9827 }
824b9a4c
MS
9828
9829 /* More Effective C++ rule 7. */
eb448459 9830 if (warn_ecpp
596ea4e5
AS
9831 && (operator_code == TRUTH_ANDIF_EXPR
9832 || operator_code == TRUTH_ORIF_EXPR
9833 || operator_code == COMPOUND_EXPR))
b323323f 9834 warning (OPT_Weffc__, "user-defined %qD always evaluates both arguments",
0cbd7506 9835 decl);
824b9a4c
MS
9836 }
9837
9838 /* Effective C++ rule 23. */
eb448459 9839 if (warn_ecpp
596ea4e5 9840 && arity == 2
4bd7c270 9841 && !DECL_ASSIGNMENT_OPERATOR_P (decl)
596ea4e5
AS
9842 && (operator_code == PLUS_EXPR
9843 || operator_code == MINUS_EXPR
9844 || operator_code == TRUNC_DIV_EXPR
4bd7c270
NS
9845 || operator_code == MULT_EXPR
9846 || operator_code == TRUNC_MOD_EXPR)
824b9a4c 9847 && TREE_CODE (TREE_TYPE (TREE_TYPE (decl))) == REFERENCE_TYPE)
b323323f 9848 warning (OPT_Weffc__, "%qD should return by value", decl);
8d08fdba 9849
271e6f02 9850 /* [over.oper]/8 */
34332678 9851 for (; argtypes && argtypes != void_list_node;
0cbd7506
MS
9852 argtypes = TREE_CHAIN (argtypes))
9853 if (TREE_PURPOSE (argtypes))
9854 {
9855 TREE_PURPOSE (argtypes) = NULL_TREE;
9856 if (operator_code == POSTINCREMENT_EXPR
596ea4e5 9857 || operator_code == POSTDECREMENT_EXPR)
0cbd7506
MS
9858 {
9859 if (pedantic)
9860 pedwarn ("%qD cannot have default arguments", decl);
9861 }
9862 else
398cd199
VR
9863 {
9864 error ("%qD cannot have default arguments", decl);
9865 return false;
9866 }
0cbd7506 9867 }
8d08fdba 9868 }
398cd199 9869 return true;
8d08fdba
MS
9870}
9871\f
0c88d886
MM
9872/* Return a string giving the keyword associate with CODE. */
9873
d8e178a0 9874static const char *
11f6b451 9875tag_name (enum tag_types code)
094fe153
JM
9876{
9877 switch (code)
9878 {
9879 case record_type:
9880 return "struct";
9881 case class_type:
9882 return "class";
9883 case union_type:
0c88d886 9884 return "union";
094fe153
JM
9885 case enum_type:
9886 return "enum";
0c88d886
MM
9887 case typename_type:
9888 return "typename";
094fe153 9889 default:
8dc2b103 9890 gcc_unreachable ();
094fe153
JM
9891 }
9892}
9893
befcd99b 9894/* Name lookup in an elaborated-type-specifier (after the keyword
4b0d3cbe 9895 indicated by TAG_CODE) has found the TYPE_DECL DECL. If the
befcd99b 9896 elaborated-type-specifier is invalid, issue a diagnostic and return
4b0d3cbe 9897 error_mark_node; otherwise, return the *_TYPE to which it referred.
cbd63935 9898 If ALLOW_TEMPLATE_P is true, TYPE may be a class template. */
befcd99b 9899
560ad596 9900tree
befcd99b 9901check_elaborated_type_specifier (enum tag_types tag_code,
4b0d3cbe 9902 tree decl,
cbd63935 9903 bool allow_template_p)
befcd99b 9904{
4b0d3cbe 9905 tree type;
befcd99b 9906
4b0d3cbe
MM
9907 /* In the case of:
9908
9909 struct S { struct S *p; };
9910
9911 name lookup will find the TYPE_DECL for the implicit "S::S"
9912 typedef. Adjust for that here. */
9913 if (DECL_SELF_REFERENCE_P (decl))
9914 decl = TYPE_NAME (TREE_TYPE (decl));
9915
9916 type = TREE_TYPE (decl);
9917
461c6fce
KL
9918 /* Check TEMPLATE_TYPE_PARM first because DECL_IMPLICIT_TYPEDEF_P
9919 is false for this case as well. */
9920 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
9921 {
9922 error ("using template type parameter %qT after %qs",
9923 type, tag_name (tag_code));
9924 return error_mark_node;
9925 }
caf93cb0 9926 /* [dcl.type.elab]
4b0d3cbe
MM
9927
9928 If the identifier resolves to a typedef-name or a template
9929 type-parameter, the elaborated-type-specifier is ill-formed.
9930
9931 In other words, the only legitimate declaration to use in the
9932 elaborated type specifier is the implicit typedef created when
9933 the type is declared. */
0c88d886
MM
9934 else if (!DECL_IMPLICIT_TYPEDEF_P (decl)
9935 && tag_code != typename_type)
befcd99b 9936 {
2d01edd7 9937 error ("using typedef-name %qD after %qs", decl, tag_name (tag_code));
dee15844 9938 error ("%q+D has a previous declaration here", decl);
4b0d3cbe 9939 return error_mark_node;
befcd99b 9940 }
cbd63935
KL
9941 else if (TREE_CODE (type) != RECORD_TYPE
9942 && TREE_CODE (type) != UNION_TYPE
0c88d886
MM
9943 && tag_code != enum_type
9944 && tag_code != typename_type)
cbd63935 9945 {
2d01edd7 9946 error ("%qT referred to as %qs", type, tag_name (tag_code));
dee15844 9947 error ("%q+T has a previous declaration here", type);
4b0d3cbe 9948 return error_mark_node;
cbd63935
KL
9949 }
9950 else if (TREE_CODE (type) != ENUMERAL_TYPE
4fe8db68 9951 && tag_code == enum_type)
cbd63935 9952 {
2d01edd7 9953 error ("%qT referred to as enum", type);
dee15844 9954 error ("%q+T has a previous declaration here", type);
4b0d3cbe 9955 return error_mark_node;
cbd63935
KL
9956 }
9957 else if (!allow_template_p
9958 && TREE_CODE (type) == RECORD_TYPE
9959 && CLASSTYPE_IS_TEMPLATE (type))
9960 {
9961 /* If a class template appears as elaborated type specifier
9962 without a template header such as:
9963
9964 template <class T> class C {};
9965 void f(class C); // No template header here
9966
9967 then the required template argument is missing. */
2d01edd7 9968 error ("template argument required for %<%s %T%>",
cbd63935
KL
9969 tag_name (tag_code),
9970 DECL_NAME (CLASSTYPE_TI_TEMPLATE (type)));
4b0d3cbe 9971 return error_mark_node;
cbd63935 9972 }
befcd99b 9973
4b0d3cbe 9974 return type;
befcd99b
MM
9975}
9976
461c6fce 9977/* Lookup NAME in elaborate type specifier in scope according to
29ef83de 9978 SCOPE and issue diagnostics if necessary.
461c6fce
KL
9979 Return *_TYPE node upon success, NULL_TREE when the NAME is not
9980 found, and ERROR_MARK_NODE for type error. */
9981
9982static tree
9983lookup_and_check_tag (enum tag_types tag_code, tree name,
29ef83de 9984 tag_scope scope, bool template_header_p)
461c6fce
KL
9985{
9986 tree t;
9987 tree decl;
29ef83de 9988 if (scope == ts_global)
bd3d082e
KL
9989 {
9990 /* First try ordinary name lookup, ignoring hidden class name
9991 injected via friend declaration. */
10e6657a 9992 decl = lookup_name_prefer_type (name, 2);
bd3d082e
KL
9993 /* If that fails, the name will be placed in the smallest
9994 non-class, non-function-prototype scope according to 3.3.1/5.
9995 We may already have a hidden name declared as friend in this
105d72c5 9996 scope. So lookup again but not ignoring hidden names.
bd3d082e
KL
9997 If we find one, that name will be made visible rather than
9998 creating a new tag. */
9999 if (!decl)
10000 decl = lookup_type_scope (name, ts_within_enclosing_non_class);
10001 }
461c6fce 10002 else
29ef83de 10003 decl = lookup_type_scope (name, scope);
461c6fce
KL
10004
10005 if (decl && DECL_CLASS_TEMPLATE_P (decl))
10006 decl = DECL_TEMPLATE_RESULT (decl);
10007
10008 if (decl && TREE_CODE (decl) == TYPE_DECL)
10009 {
4104f0f4
KL
10010 /* Look for invalid nested type:
10011 class C {
10012 class C {};
10013 }; */
10014 if (scope == ts_current && DECL_SELF_REFERENCE_P (decl))
10015 {
10016 error ("%qD has the same name as the class in which it is "
10017 "declared",
10018 decl);
10019 return error_mark_node;
10020 }
10021
461c6fce
KL
10022 /* Two cases we need to consider when deciding if a class
10023 template is allowed as an elaborated type specifier:
10024 1. It is a self reference to its own class.
10025 2. It comes with a template header.
10026
10027 For example:
10028
10029 template <class T> class C {
10030 class C *c1; // DECL_SELF_REFERENCE_P is true
10031 class D;
10032 };
10033 template <class U> class C; // template_header_p is true
10034 template <class T> class C<T>::D {
10035 class C *c2; // DECL_SELF_REFERENCE_P is true
10036 }; */
10037
10038 t = check_elaborated_type_specifier (tag_code,
10039 decl,
10040 template_header_p
10041 | DECL_SELF_REFERENCE_P (decl));
10042 return t;
10043 }
af92ab36
OW
10044 else if (decl && TREE_CODE (decl) == TREE_LIST)
10045 {
10046 error ("reference to %qD is ambiguous", name);
10047 print_candidates (decl);
10048 return error_mark_node;
10049 }
461c6fce
KL
10050 else
10051 return NULL_TREE;
10052}
10053
cbd63935 10054/* Get the struct, enum or union (TAG_CODE says which) with tag NAME.
8d08fdba
MS
10055 Define the tag as a forward-reference if it is not defined.
10056
cbd63935 10057 If a declaration is given, process it here, and report an error if
38b305d0 10058 multiple declarations are not identical.
8d08fdba 10059
29ef83de 10060 SCOPE is TS_CURRENT when this is also a definition. Only look in
8d08fdba 10061 the current frame for the name (since C++ allows new names in any
29ef83de
KL
10062 scope.) It is TS_WITHIN_ENCLOSING_NON_CLASS if this is a friend
10063 declaration. Only look beginning from the current scope outward up
10064 till the nearest non-class scope. Otherwise it is TS_GLOBAL.
cbd63935
KL
10065
10066 TEMPLATE_HEADER_P is true when this declaration is preceded by
10067 a set of template parameters. */
8d08fdba 10068
8d08fdba 10069tree
38b305d0 10070xref_tag (enum tag_types tag_code, tree name,
29ef83de 10071 tag_scope scope, bool template_header_p)
8d08fdba 10072{
8d08fdba 10073 enum tree_code code;
926ce8bd 10074 tree t;
25aab5d0 10075 tree context = NULL_TREE;
dc8263bc 10076
22ffcc6f 10077 timevar_push (TV_NAME_LOOKUP);
cbd63935 10078
50bc768d 10079 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
cbd63935 10080
8d08fdba
MS
10081 switch (tag_code)
10082 {
10083 case record_type:
10084 case class_type:
8d08fdba 10085 code = RECORD_TYPE;
8d08fdba
MS
10086 break;
10087 case union_type:
10088 code = UNION_TYPE;
8d08fdba
MS
10089 break;
10090 case enum_type:
10091 code = ENUMERAL_TYPE;
10092 break;
10093 default:
8dc2b103 10094 gcc_unreachable ();
8d08fdba
MS
10095 }
10096
461c6fce
KL
10097 /* In case of anonymous name, xref_tag is only called to
10098 make type node and push name. Name lookup is not required. */
10099 if (ANON_AGGRNAME_P (name))
10100 t = NULL_TREE;
8d08fdba 10101 else
461c6fce 10102 t = lookup_and_check_tag (tag_code, name,
29ef83de 10103 scope, template_header_p);
cbd63935 10104
461c6fce
KL
10105 if (t == error_mark_node)
10106 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
68642fb6 10107
29ef83de 10108 if (scope != ts_current && t && current_class_type
461c6fce
KL
10109 && template_class_depth (current_class_type)
10110 && template_header_p)
10111 {
29ef83de 10112 /* Since SCOPE is not TS_CURRENT, we are not looking at a
461c6fce
KL
10113 definition of this tag. Since, in addition, we are currently
10114 processing a (member) template declaration of a template
10115 class, we must be very careful; consider:
25aab5d0 10116
461c6fce
KL
10117 template <class X>
10118 struct S1
25aab5d0 10119
461c6fce
KL
10120 template <class U>
10121 struct S2
10122 { template <class V>
10123 friend struct S1; };
25aab5d0 10124
461c6fce
KL
10125 Here, the S2::S1 declaration should not be confused with the
10126 outer declaration. In particular, the inner version should
10127 have a template parameter of level 2, not level 1. This
10128 would be particularly important if the member declaration
10129 were instead:
25aab5d0 10130
461c6fce 10131 template <class V = U> friend struct S1;
25aab5d0 10132
461c6fce
KL
10133 say, when we should tsubst into `U' when instantiating
10134 S2. On the other hand, when presented with:
25aab5d0 10135
461c6fce
KL
10136 template <class T>
10137 struct S1 {
10138 template <class U>
10139 struct S2 {};
10140 template <class U>
10141 friend struct S2;
10142 };
25aab5d0 10143
461c6fce
KL
10144 we must find the inner binding eventually. We
10145 accomplish this by making sure that the new type we
10146 create to represent this declaration has the right
10147 TYPE_CONTEXT. */
10148 context = TYPE_CONTEXT (t);
10149 t = NULL_TREE;
8d08fdba
MS
10150 }
10151
cbd63935 10152 if (! t)
8d08fdba
MS
10153 {
10154 /* If no such tag is yet defined, create a forward-reference node
10155 and record it as the "definition".
10156 When a real declaration of this type is found,
10157 the forward-reference will be altered into a real type. */
8d08fdba
MS
10158 if (code == ENUMERAL_TYPE)
10159 {
2d01edd7 10160 error ("use of enum %q#D without previous declaration", name);
30fc3df7 10161 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
8d08fdba 10162 }
8d08fdba
MS
10163 else
10164 {
cbd63935
KL
10165 t = make_aggr_type (code);
10166 TYPE_CONTEXT (t) = context;
bd3d082e 10167 t = pushtag (name, t, scope);
8d08fdba
MS
10168 }
10169 }
10170 else
10171 {
29ef83de 10172 if (template_header_p && IS_AGGR_TYPE (t))
60feef2c
LM
10173 {
10174 if (!redeclare_class_template (t, current_template_parms))
10175 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
10176 }
caf93cb0 10177 else if (!processing_template_decl
9ce1594a
MM
10178 && CLASS_TYPE_P (t)
10179 && CLASSTYPE_IS_TEMPLATE (t))
10180 {
2d01edd7 10181 error ("redeclaration of %qT as a non-template", t);
b646edb8
VR
10182 error ("previous declaration %q+D", t);
10183 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
9ce1594a 10184 }
bd3d082e
KL
10185
10186 /* Make injected friend class visible. */
10187 if (scope != ts_within_enclosing_non_class
10188 && hidden_name_p (TYPE_NAME (t)))
10189 {
10190 DECL_ANTICIPATED (TYPE_NAME (t)) = 0;
10191 DECL_FRIEND_P (TYPE_NAME (t)) = 0;
10192
10193 if (TYPE_TEMPLATE_INFO (t))
10194 {
10195 DECL_ANTICIPATED (TYPE_TI_TEMPLATE (t)) = 0;
10196 DECL_FRIEND_P (TYPE_TI_TEMPLATE (t)) = 0;
10197 }
0cbd7506 10198 }
8d08fdba
MS
10199 }
10200
cbd63935 10201 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
8d08fdba 10202}
8ccc31eb 10203
fc378698 10204tree
29ef83de 10205xref_tag_from_type (tree old, tree id, tag_scope scope)
fc378698 10206{
88e5899c 10207 enum tag_types tag_kind;
fc378698
MS
10208
10209 if (TREE_CODE (old) == RECORD_TYPE)
88e5899c 10210 tag_kind = (CLASSTYPE_DECLARED_CLASS (old) ? class_type : record_type);
fc378698 10211 else
88e5899c 10212 tag_kind = union_type;
fc378698
MS
10213
10214 if (id == NULL_TREE)
10215 id = TYPE_IDENTIFIER (old);
10216
29ef83de 10217 return xref_tag (tag_kind, id, scope, false);
fc378698
MS
10218}
10219
48b45647
NS
10220/* Create the binfo hierarchy for REF with (possibly NULL) base list
10221 BASE_LIST. For each element on BASE_LIST the TREE_PURPOSE is an
10222 access_* node, and the TREE_VALUE is the type of the base-class.
72f8fc59
LM
10223 Non-NULL TREE_TYPE indicates virtual inheritance.
10224
bd6f7848 10225 Returns true if the binfo hierarchy was successfully created,
72f8fc59 10226 false if an error was detected. */
3fd71a52 10227
72f8fc59 10228bool
dbbf88d1 10229xref_basetypes (tree ref, tree base_list)
8ccc31eb 10230{
8fbc5ae7 10231 tree *basep;
fa743e8c 10232 tree binfo, base_binfo;
03fd3f84 10233 unsigned max_vbases = 0; /* Maximum direct & indirect virtual bases. */
77880ae4 10234 unsigned max_bases = 0; /* Maximum direct bases. */
8fbc5ae7 10235 int i;
48b45647
NS
10236 tree default_access;
10237 tree igo_prev; /* Track Inheritance Graph Order. */
a5d7c4a3 10238
bef89e9e 10239 if (ref == error_mark_node)
72f8fc59 10240 return false;
bef89e9e 10241
48b45647
NS
10242 /* The base of a derived class is private by default, all others are
10243 public. */
10244 default_access = (TREE_CODE (ref) == RECORD_TYPE
10245 && CLASSTYPE_DECLARED_CLASS (ref)
10246 ? access_private_node : access_public_node);
da15dae6 10247
d6479fe7
MM
10248 /* First, make sure that any templates in base-classes are
10249 instantiated. This ensures that if we call ourselves recursively
10250 we do not get confused about which classes are marked and which
10251 are not. */
caf93cb0
EC
10252 basep = &base_list;
10253 while (*basep)
8fbc5ae7
MM
10254 {
10255 tree basetype = TREE_VALUE (*basep);
caf93cb0 10256
8fbc5ae7
MM
10257 if (!(processing_template_decl && uses_template_parms (basetype))
10258 && !complete_type_or_else (basetype, NULL))
dbbf88d1 10259 /* An incomplete type. Remove it from the list. */
8fbc5ae7
MM
10260 *basep = TREE_CHAIN (*basep);
10261 else
48b45647
NS
10262 {
10263 max_bases++;
10264 if (TREE_TYPE (*basep))
10265 max_vbases++;
10266 if (CLASS_TYPE_P (basetype))
10267 max_vbases += VEC_length (tree, CLASSTYPE_VBASECLASSES (basetype));
10268 basep = &TREE_CHAIN (*basep);
10269 }
8fbc5ae7 10270 }
d6479fe7 10271
3543e114 10272 TYPE_MARKED_P (ref) = 1;
48b45647 10273
cad7e87b
NS
10274 /* The binfo slot should be empty, unless this is an (ill-formed)
10275 redefinition. */
50bc768d
NS
10276 gcc_assert (!TYPE_BINFO (ref) || TYPE_SIZE (ref));
10277 gcc_assert (TYPE_MAIN_VARIANT (ref) == ref);
caf93cb0 10278
fa743e8c 10279 binfo = make_tree_binfo (max_bases);
caf93cb0 10280
48b45647
NS
10281 TYPE_BINFO (ref) = binfo;
10282 BINFO_OFFSET (binfo) = size_zero_node;
10283 BINFO_TYPE (binfo) = ref;
caf93cb0 10284
48b45647
NS
10285 if (max_bases)
10286 {
d4e6fecb 10287 BINFO_BASE_ACCESSES (binfo) = VEC_alloc (tree, gc, max_bases);
48b45647
NS
10288 /* An aggregate cannot have baseclasses. */
10289 CLASSTYPE_NON_AGGREGATE (ref) = 1;
caf93cb0 10290
48b45647 10291 if (TREE_CODE (ref) == UNION_TYPE)
72f8fc59
LM
10292 {
10293 error ("derived union %qT invalid", ref);
10294 return false;
10295 }
48b45647 10296 }
caf93cb0 10297
48b45647 10298 if (max_bases > 1)
dbbf88d1 10299 {
48b45647 10300 if (TYPE_FOR_JAVA (ref))
72f8fc59
LM
10301 {
10302 error ("Java class %qT cannot have multiple bases", ref);
10303 return false;
10304 }
48b45647 10305 }
caf93cb0 10306
48b45647
NS
10307 if (max_vbases)
10308 {
d4e6fecb 10309 CLASSTYPE_VBASECLASSES (ref) = VEC_alloc (tree, gc, max_vbases);
caf93cb0 10310
48b45647 10311 if (TYPE_FOR_JAVA (ref))
72f8fc59
LM
10312 {
10313 error ("Java class %qT cannot have virtual bases", ref);
10314 return false;
10315 }
48b45647 10316 }
cad7e87b 10317
48b45647
NS
10318 for (igo_prev = binfo; base_list; base_list = TREE_CHAIN (base_list))
10319 {
10320 tree access = TREE_PURPOSE (base_list);
10321 int via_virtual = TREE_TYPE (base_list) != NULL_TREE;
10322 tree basetype = TREE_VALUE (base_list);
caf93cb0 10323
48b45647
NS
10324 if (access == access_default_node)
10325 access = default_access;
caf93cb0 10326
5d80a306
DG
10327 if (PACK_EXPANSION_P (basetype))
10328 basetype = PACK_EXPANSION_PATTERN (basetype);
48b45647
NS
10329 if (TREE_CODE (basetype) == TYPE_DECL)
10330 basetype = TREE_TYPE (basetype);
10331 if (TREE_CODE (basetype) != RECORD_TYPE
10332 && TREE_CODE (basetype) != TYPENAME_TYPE
10333 && TREE_CODE (basetype) != TEMPLATE_TYPE_PARM
10334 && TREE_CODE (basetype) != BOUND_TEMPLATE_TEMPLATE_PARM)
10335 {
2d01edd7 10336 error ("base type %qT fails to be a struct or class type",
48b45647 10337 basetype);
72f8fc59 10338 return false;
8ccc31eb 10339 }
caf93cb0 10340
48b45647
NS
10341 if (TYPE_FOR_JAVA (basetype) && (current_lang_depth () == 0))
10342 TYPE_FOR_JAVA (ref) = 1;
10343
fa743e8c 10344 base_binfo = NULL_TREE;
48b45647 10345 if (CLASS_TYPE_P (basetype) && !dependent_type_p (basetype))
dbbf88d1 10346 {
48b45647 10347 base_binfo = TYPE_BINFO (basetype);
77880ae4 10348 /* The original basetype could have been a typedef'd type. */
48b45647 10349 basetype = BINFO_TYPE (base_binfo);
caf93cb0 10350
48b45647
NS
10351 /* Inherit flags from the base. */
10352 TYPE_HAS_NEW_OPERATOR (ref)
10353 |= TYPE_HAS_NEW_OPERATOR (basetype);
10354 TYPE_HAS_ARRAY_NEW_OPERATOR (ref)
10355 |= TYPE_HAS_ARRAY_NEW_OPERATOR (basetype);
10356 TYPE_GETS_DELETE (ref) |= TYPE_GETS_DELETE (basetype);
48b45647 10357 TYPE_HAS_CONVERSION (ref) |= TYPE_HAS_CONVERSION (basetype);
3543e114
NS
10358 CLASSTYPE_DIAMOND_SHAPED_P (ref)
10359 |= CLASSTYPE_DIAMOND_SHAPED_P (basetype);
10360 CLASSTYPE_REPEATED_BASE_P (ref)
10361 |= CLASSTYPE_REPEATED_BASE_P (basetype);
8fbc5ae7 10362 }
c8094d83 10363
98d6e9af
NS
10364 /* We must do this test after we've seen through a typedef
10365 type. */
10366 if (TYPE_MARKED_P (basetype))
10367 {
10368 if (basetype == ref)
2d01edd7 10369 error ("recursive type %qT undefined", basetype);
98d6e9af 10370 else
2d01edd7 10371 error ("duplicate base type %qT invalid", basetype);
72f8fc59 10372 return false;
98d6e9af 10373 }
5d80a306
DG
10374
10375 if (PACK_EXPANSION_P (TREE_VALUE (base_list)))
10376 /* Regenerate the pack expansion for the bases. */
10377 basetype = make_pack_expansion (basetype);
10378
98d6e9af 10379 TYPE_MARKED_P (basetype) = 1;
caf93cb0 10380
48b45647
NS
10381 base_binfo = copy_binfo (base_binfo, basetype, ref,
10382 &igo_prev, via_virtual);
10383 if (!BINFO_INHERITANCE_CHAIN (base_binfo))
10384 BINFO_INHERITANCE_CHAIN (base_binfo) = binfo;
f5c28a15 10385
fa743e8c 10386 BINFO_BASE_APPEND (binfo, base_binfo);
63d1c7b3 10387 BINFO_BASE_ACCESS_APPEND (binfo, access);
cad7e87b
NS
10388 }
10389
3543e114
NS
10390 if (VEC_space (tree, CLASSTYPE_VBASECLASSES (ref), 1))
10391 /* If we have space in the vbase vector, we must have shared at
10392 least one of them, and are therefore diamond shaped. */
10393 CLASSTYPE_DIAMOND_SHAPED_P (ref) = 1;
10394
dbbf88d1 10395 /* Unmark all the types. */
fa743e8c 10396 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
3543e114
NS
10397 TYPE_MARKED_P (BINFO_TYPE (base_binfo)) = 0;
10398 TYPE_MARKED_P (ref) = 0;
10399
10400 /* Now see if we have a repeated base type. */
10401 if (!CLASSTYPE_REPEATED_BASE_P (ref))
10402 {
10403 for (base_binfo = binfo; base_binfo;
10404 base_binfo = TREE_CHAIN (base_binfo))
10405 {
10406 if (TYPE_MARKED_P (BINFO_TYPE (base_binfo)))
10407 {
10408 CLASSTYPE_REPEATED_BASE_P (ref) = 1;
10409 break;
10410 }
10411 TYPE_MARKED_P (BINFO_TYPE (base_binfo)) = 1;
10412 }
10413 for (base_binfo = binfo; base_binfo;
10414 base_binfo = TREE_CHAIN (base_binfo))
10415 if (TYPE_MARKED_P (BINFO_TYPE (base_binfo)))
10416 TYPE_MARKED_P (BINFO_TYPE (base_binfo)) = 0;
10417 else
10418 break;
10419 }
72f8fc59
LM
10420
10421 return true;
8ccc31eb 10422}
68642fb6 10423
8d08fdba 10424\f
8d08fdba 10425/* Begin compiling the definition of an enumeration type.
29ef83de 10426 NAME is its name.
8d08fdba
MS
10427 Returns the type object, as yet incomplete.
10428 Also records info about it so that build_enumerator
10429 may be used to declare the individual values as they are read. */
10430
10431tree
11f6b451 10432start_enum (tree name)
8d08fdba 10433{
29ef83de
KL
10434 tree enumtype;
10435
10436 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
8d08fdba
MS
10437
10438 /* If this is the real definition for a previous forward reference,
10439 fill in the contents in the same object that used to be the
10440 forward reference. */
10441
29ef83de
KL
10442 enumtype = lookup_and_check_tag (enum_type, name,
10443 /*tag_scope=*/ts_current,
10444 /*template_header_p=*/false);
8d08fdba
MS
10445
10446 if (enumtype != NULL_TREE && TREE_CODE (enumtype) == ENUMERAL_TYPE)
30ff8252 10447 {
2d01edd7 10448 error ("multiple definition of %q#T", enumtype);
ddd2d57e 10449 error ("%Jprevious definition here", TYPE_MAIN_DECL (enumtype));
58595203
MM
10450 /* Clear out TYPE_VALUES, and start again. */
10451 TYPE_VALUES (enumtype) = NULL_TREE;
30ff8252 10452 }
8d08fdba
MS
10453 else
10454 {
29ef83de
KL
10455 /* In case of error, make a dummy enum to allow parsing to
10456 continue. */
10457 if (enumtype == error_mark_node)
10458 name = make_anon_name ();
10459
8d08fdba 10460 enumtype = make_node (ENUMERAL_TYPE);
bd3d082e 10461 enumtype = pushtag (name, enumtype, /*tag_scope=*/ts_current);
8d08fdba
MS
10462 }
10463
8d08fdba
MS
10464 return enumtype;
10465}
10466
10467/* After processing and defining all the values of an enumeration type,
10468 install their decls in the enumeration type and finish it off.
968b956a 10469 ENUMTYPE is the type object and VALUES a list of name-value pairs. */
8d08fdba 10470
968b956a 10471void
11f6b451 10472finish_enum (tree enumtype)
8d08fdba 10473{
7b6d72fc
MM
10474 tree values;
10475 tree decl;
10476 tree value;
968b956a
MM
10477 tree minnode;
10478 tree maxnode;
10479 tree t;
10480 bool unsignedp;
f6af9a15 10481 bool use_short_enum;
968b956a 10482 int lowprec;
caf93cb0 10483 int highprec;
968b956a 10484 int precision;
7b6d72fc 10485 integer_type_kind itk;
ad96995b 10486 tree underlying_type = NULL_TREE;
968b956a
MM
10487
10488 /* We built up the VALUES in reverse order. */
10489 TYPE_VALUES (enumtype) = nreverse (TYPE_VALUES (enumtype));
10490
f5d70cc0
JM
10491 /* For an enum defined in a template, just set the type of the values;
10492 all further processing is postponed until the template is
10493 instantiated. We need to set the type so that tsubst of a CONST_DECL
10494 works. */
968b956a
MM
10495 if (processing_template_decl)
10496 {
caf93cb0
EC
10497 for (values = TYPE_VALUES (enumtype);
10498 values;
7b6d72fc
MM
10499 values = TREE_CHAIN (values))
10500 TREE_TYPE (TREE_VALUE (values)) = enumtype;
5f261ba9 10501 if (at_function_scope_p ())
968b956a 10502 add_stmt (build_min (TAG_DEFN, enumtype));
968b956a
MM
10503 return;
10504 }
10505
7b6d72fc 10506 /* Determine the minimum and maximum values of the enumerators. */
968b956a 10507 if (TYPE_VALUES (enumtype))
8d08fdba 10508 {
968b956a 10509 minnode = maxnode = NULL_TREE;
5566b478 10510
caf93cb0
EC
10511 for (values = TYPE_VALUES (enumtype);
10512 values;
7b6d72fc 10513 values = TREE_CHAIN (values))
8d08fdba 10514 {
7b6d72fc 10515 decl = TREE_VALUE (values);
f5d70cc0
JM
10516
10517 /* [dcl.enum]: Following the closing brace of an enum-specifier,
10518 each enumerator has the type of its enumeration. Prior to the
10519 closing brace, the type of each enumerator is the type of its
10520 initializing value. */
10521 TREE_TYPE (decl) = enumtype;
10522
7b6d72fc
MM
10523 /* Update the minimum and maximum values, if appropriate. */
10524 value = DECL_INITIAL (decl);
3c955a04
MM
10525 if (value == error_mark_node)
10526 value = integer_zero_node;
f5d70cc0
JM
10527 /* Figure out what the minimum and maximum values of the
10528 enumerators are. */
10529 if (!minnode)
10530 minnode = maxnode = value;
10531 else if (tree_int_cst_lt (maxnode, value))
10532 maxnode = value;
10533 else if (tree_int_cst_lt (value, minnode))
10534 minnode = value;
8d08fdba
MS
10535 }
10536 }
f376e137 10537 else
7b6d72fc
MM
10538 /* [dcl.enum]
10539
10540 If the enumerator-list is empty, the underlying type is as if
10541 the enumeration had a single enumerator with value 0. */
968b956a
MM
10542 minnode = maxnode = integer_zero_node;
10543
10544 /* Compute the number of bits require to represent all values of the
10545 enumeration. We must do this before the type of MINNODE and
10546 MAXNODE are transformed, since min_precision relies on the
10547 TREE_TYPE of the value it is passed. */
10548 unsignedp = tree_int_cst_sgn (minnode) >= 0;
10549 lowprec = min_precision (minnode, unsignedp);
10550 highprec = min_precision (maxnode, unsignedp);
10551 precision = MAX (lowprec, highprec);
10552
7b6d72fc
MM
10553 /* Determine the underlying type of the enumeration.
10554
10555 [dcl.enum]
10556
10557 The underlying type of an enumeration is an integral type that
10558 can represent all the enumerator values defined in the
10559 enumeration. It is implementation-defined which integral type is
10560 used as the underlying type for an enumeration except that the
10561 underlying type shall not be larger than int unless the value of
caf93cb0 10562 an enumerator cannot fit in an int or unsigned int.
7b6d72fc
MM
10563
10564 We use "int" or an "unsigned int" as the underlying type, even if
10565 a smaller integral type would work, unless the user has
f6af9a15
MA
10566 explicitly requested that we use the smallest possible type. The
10567 user can request that for all enumerations with a command line
03fd3f84 10568 flag, or for just one enumeration with an attribute. */
f6af9a15
MA
10569
10570 use_short_enum = flag_short_enums
10571 || lookup_attribute ("packed", TYPE_ATTRIBUTES (enumtype));
10572
10573 for (itk = (use_short_enum ? itk_char : itk_int);
caf93cb0 10574 itk != itk_none;
7b6d72fc 10575 itk++)
e455bc27 10576 {
7b6d72fc
MM
10577 underlying_type = integer_types[itk];
10578 if (TYPE_PRECISION (underlying_type) >= precision
8df83eae 10579 && TYPE_UNSIGNED (underlying_type) == unsignedp)
7b6d72fc
MM
10580 break;
10581 }
10582 if (itk == itk_none)
10583 {
10584 /* DR 377
10585
10586 IF no integral type can represent all the enumerator values, the
10587 enumeration is ill-formed. */
e455bc27 10588 error ("no integral type can represent all of the enumerator values "
2d01edd7 10589 "for %qT", enumtype);
e455bc27 10590 precision = TYPE_PRECISION (long_long_integer_type_node);
7b6d72fc 10591 underlying_type = integer_types[itk_unsigned_long_long];
e455bc27
MM
10592 }
10593
caf93cb0 10594 /* Compute the minium and maximum values for the type.
8d08fdba 10595
7b6d72fc
MM
10596 [dcl.enum]
10597
10598 For an enumeration where emin is the smallest enumerator and emax
10599 is the largest, the values of the enumeration are the values of the
10600 underlying type in the range bmin to bmax, where bmin and bmax are,
10601 respectively, the smallest and largest values of the smallest bit-
10602 field that can store emin and emax. */
d0c5c9b1
RS
10603
10604 /* The middle-end currently assumes that types with TYPE_PRECISION
10605 narrower than their underlying type are suitably zero or sign
10606 extended to fill their mode. g++ doesn't make these guarantees.
10607 Until the middle-end can represent such paradoxical types, we
77880ae4 10608 set the TYPE_PRECISION to the width of the underlying type. */
d0c5c9b1
RS
10609 TYPE_PRECISION (enumtype) = TYPE_PRECISION (underlying_type);
10610
7b6d72fc
MM
10611 set_min_and_max_values_for_integral_type (enumtype, precision, unsignedp);
10612
10613 /* [dcl.enum]
caf93cb0 10614
7b6d72fc
MM
10615 The value of sizeof() applied to an enumeration type, an object
10616 of an enumeration type, or an enumerator, is the value of sizeof()
10617 applied to the underlying type. */
10618 TYPE_SIZE (enumtype) = TYPE_SIZE (underlying_type);
10619 TYPE_SIZE_UNIT (enumtype) = TYPE_SIZE_UNIT (underlying_type);
10620 TYPE_MODE (enumtype) = TYPE_MODE (underlying_type);
10621 TYPE_ALIGN (enumtype) = TYPE_ALIGN (underlying_type);
10622 TYPE_USER_ALIGN (enumtype) = TYPE_USER_ALIGN (underlying_type);
8df83eae 10623 TYPE_UNSIGNED (enumtype) = TYPE_UNSIGNED (underlying_type);
7b6d72fc
MM
10624
10625 /* Convert each of the enumerators to the type of the underlying
10626 type of the enumeration. */
10627 for (values = TYPE_VALUES (enumtype); values; values = TREE_CHAIN (values))
10628 {
3c955a04
MM
10629 location_t saved_location;
10630
7b6d72fc 10631 decl = TREE_VALUE (values);
3c955a04
MM
10632 saved_location = input_location;
10633 input_location = DECL_SOURCE_LOCATION (decl);
7b6d72fc
MM
10634 value = perform_implicit_conversion (underlying_type,
10635 DECL_INITIAL (decl));
3c955a04 10636 input_location = saved_location;
89b0433e
NS
10637
10638 /* Do not clobber shared ints. */
10639 value = copy_node (value);
c8094d83 10640
7b6d72fc
MM
10641 TREE_TYPE (value) = enumtype;
10642 DECL_INITIAL (decl) = value;
10643 TREE_VALUE (values) = value;
e455bc27 10644 }
8d08fdba 10645
968b956a
MM
10646 /* Fix up all variant types of this enum type. */
10647 for (t = TYPE_MAIN_VARIANT (enumtype); t; t = TYPE_NEXT_VARIANT (t))
10648 {
10649 TYPE_VALUES (t) = TYPE_VALUES (enumtype);
10650 TYPE_MIN_VALUE (t) = TYPE_MIN_VALUE (enumtype);
10651 TYPE_MAX_VALUE (t) = TYPE_MAX_VALUE (enumtype);
10652 TYPE_SIZE (t) = TYPE_SIZE (enumtype);
10653 TYPE_SIZE_UNIT (t) = TYPE_SIZE_UNIT (enumtype);
10654 TYPE_MODE (t) = TYPE_MODE (enumtype);
10655 TYPE_PRECISION (t) = TYPE_PRECISION (enumtype);
10656 TYPE_ALIGN (t) = TYPE_ALIGN (enumtype);
10657 TYPE_USER_ALIGN (t) = TYPE_USER_ALIGN (enumtype);
8df83eae 10658 TYPE_UNSIGNED (t) = TYPE_UNSIGNED (enumtype);
cbf882af
MM
10659 }
10660
968b956a
MM
10661 /* Finish debugging output for this type. */
10662 rest_of_type_compilation (enumtype, namespace_bindings_p ());
8d08fdba
MS
10663}
10664
079e1098 10665/* Build and install a CONST_DECL for an enumeration constant of the
58595203 10666 enumeration type ENUMTYPE whose NAME and VALUE (if any) are provided.
8d08fdba
MS
10667 Assignment of sequential values by default is handled here. */
10668
58595203 10669void
11f6b451 10670build_enumerator (tree name, tree value, tree enumtype)
8d08fdba 10671{
58595203 10672 tree decl;
e8bd800e 10673 tree context;
58595203 10674 tree type;
8d08fdba 10675
93678513
MM
10676 /* If the VALUE was erroneous, pretend it wasn't there; that will
10677 result in the enum being assigned the next value in sequence. */
10678 if (value == error_mark_node)
10679 value = NULL_TREE;
10680
8d08fdba
MS
10681 /* Remove no-op casts from the value. */
10682 if (value)
10683 STRIP_TYPE_NOPS (value);
10684
58595203
MM
10685 if (! processing_template_decl)
10686 {
10687 /* Validate and default VALUE. */
10688 if (value != NULL_TREE)
10689 {
8a784e4a 10690 value = integral_constant_value (value);
58595203
MM
10691
10692 if (TREE_CODE (value) == INTEGER_CST)
10693 {
0a72704b 10694 value = perform_integral_promotions (value);
58595203
MM
10695 constant_expression_warning (value);
10696 }
10697 else
10698 {
253e0d15 10699 error ("enumerator value for %qD is not an integer constant", name);
58595203
MM
10700 value = NULL_TREE;
10701 }
10702 }
10703
10704 /* Default based on previous value. */
6a540f3c 10705 if (value == NULL_TREE)
58595203 10706 {
58595203
MM
10707 if (TYPE_VALUES (enumtype))
10708 {
ff4eb0b5
ZW
10709 HOST_WIDE_INT hi;
10710 unsigned HOST_WIDE_INT lo;
10711 tree prev_value;
10712 bool overflowed;
10713
10714 /* The next value is the previous value plus one. We can
0cbd7506 10715 safely assume that the previous value is an INTEGER_CST.
ff4eb0b5
ZW
10716 add_double doesn't know the type of the target expression,
10717 so we must check with int_fits_type_p as well. */
58595203 10718 prev_value = DECL_INITIAL (TREE_VALUE (TYPE_VALUES (enumtype)));
ff4eb0b5
ZW
10719 overflowed = add_double (TREE_INT_CST_LOW (prev_value),
10720 TREE_INT_CST_HIGH (prev_value),
10721 1, 0, &lo, &hi);
10722 value = build_int_cst_wide (TREE_TYPE (prev_value), lo, hi);
10723 overflowed |= !int_fits_type_p (value, TREE_TYPE (prev_value));
68642fb6 10724
ff4eb0b5 10725 if (overflowed)
3c955a04
MM
10726 {
10727 error ("overflow in enumeration values at %qD", name);
10728 value = error_mark_node;
10729 }
58595203
MM
10730 }
10731 else
10732 value = integer_zero_node;
10733 }
10734
10735 /* Remove no-op casts from the value. */
f5d70cc0 10736 STRIP_TYPE_NOPS (value);
58595203 10737 }
8d08fdba 10738
8d08fdba 10739 /* C++ associates enums with global, function, or class declarations. */
58595203
MM
10740 context = current_scope ();
10741
10742 /* Build the actual enumeration constant. Note that the enumeration
10743 constants have the type of their initializers until the
10744 enumeration is complete:
10745
10746 [ dcl.enum ]
10747
10748 Following the closing brace of an enum-specifier, each enumer-
10749 ator has the type of its enumeration. Prior to the closing
10750 brace, the type of each enumerator is the type of its
10751 initializing value.
10752
10753 In finish_enum we will reset the type. Of course, if we're
a1c65f9f 10754 processing a template, there may be no value. */
58595203
MM
10755 type = value ? TREE_TYPE (value) : NULL_TREE;
10756
10757 if (context && context == current_class_type)
10758 /* This enum declaration is local to the class. We need the full
8f17b5c5 10759 lang_decl so that we can record DECL_CLASS_CONTEXT, for example. */
58595203
MM
10760 decl = build_lang_decl (CONST_DECL, name, type);
10761 else
10762 /* It's a global enum, or it's local to a function. (Note local to
e8bd800e 10763 a function could mean local to a class method. */
58595203 10764 decl = build_decl (CONST_DECL, name, type);
e8bd800e 10765
58595203 10766 DECL_CONTEXT (decl) = FROB_CONTEXT (context);
6de9cd9a
DN
10767 TREE_CONSTANT (decl) = 1;
10768 TREE_INVARIANT (decl) = 1;
10769 TREE_READONLY (decl) = 1;
58595203 10770 DECL_INITIAL (decl) = value;
e8bd800e 10771
58595203
MM
10772 if (context && context == current_class_type)
10773 /* In something like `struct S { enum E { i = 7 }; };' we put `i'
7b6d72fc
MM
10774 on the TYPE_FIELDS list for `S'. (That's so that you can say
10775 things like `S::i' later.) */
58595203
MM
10776 finish_member_declaration (decl);
10777 else
9780c24f 10778 pushdecl (decl);
58595203
MM
10779
10780 /* Add this enumeration constant to the list for this type. */
10781 TYPE_VALUES (enumtype) = tree_cons (name, decl, TYPE_VALUES (enumtype));
8d08fdba
MS
10782}
10783
8d08fdba 10784\f
a8f73d4b
MM
10785/* We're defining DECL. Make sure that it's type is OK. */
10786
10787static void
11f6b451 10788check_function_type (tree decl, tree current_function_parms)
a8f73d4b
MM
10789{
10790 tree fntype = TREE_TYPE (decl);
d0f062fb 10791 tree return_type = complete_type (TREE_TYPE (fntype));
a8f73d4b
MM
10792
10793 /* In a function definition, arg types must be complete. */
10794 require_complete_types_for_parms (current_function_parms);
10795
c9387915
GB
10796 if (dependent_type_p (return_type))
10797 return;
d0f062fb 10798 if (!COMPLETE_OR_VOID_TYPE_P (return_type))
a8f73d4b 10799 {
51b15ede 10800 tree args = TYPE_ARG_TYPES (fntype);
3db45ab5 10801
51b15ede 10802 error ("return type %q#T is incomplete", return_type);
a8f73d4b 10803
51b15ede 10804 /* Make it return void instead. */
a8f73d4b 10805 if (TREE_CODE (fntype) == METHOD_TYPE)
51b15ede
NS
10806 fntype = build_method_type_directly (TREE_TYPE (TREE_VALUE (args)),
10807 void_type_node,
10808 TREE_CHAIN (args));
a8f73d4b 10809 else
51b15ede 10810 fntype = build_function_type (void_type_node, args);
68642fb6 10811 TREE_TYPE (decl)
a8f73d4b 10812 = build_exception_variant (fntype,
51b15ede 10813 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (decl)));
a8f73d4b
MM
10814 }
10815 else
10816 abstract_virtuals_error (decl, TREE_TYPE (fntype));
10817}
10818
8d08fdba
MS
10819/* Create the FUNCTION_DECL for a function definition.
10820 DECLSPECS and DECLARATOR are the parts of the declaration;
10821 they describe the function's name and the type it returns,
10822 but twisted together in a fashion that parallels the syntax of C.
10823
a8f73d4b
MM
10824 FLAGS is a bitwise or of SF_PRE_PARSED (indicating that the
10825 DECLARATOR is really the DECL for the function we are about to
10826 process and that DECLSPECS should be ignored), SF_INCLASS_INLINE
f444e36b 10827 indicating that the function is an inline defined in-class.
68642fb6 10828
8d08fdba
MS
10829 This function creates a binding context for the function body
10830 as well as setting up the FUNCTION_DECL in current_function_decl.
10831
8d08fdba
MS
10832 For C++, we must first check whether that datum makes any sense.
10833 For example, "class A local_a(1,2);" means that variable local_a
10834 is an aggregate of type A, which should have a constructor
72c4a4ca
GK
10835 applied to it with the argument list [1, 2].
10836
10837 On entry, DECL_INITIAL (decl1) should be NULL_TREE or error_mark_node,
10838 or may be a BLOCK if the function has been defined previously
10839 in this translation unit. On exit, DECL_INITIAL (decl1) will be
10840 error_mark_node if the function has never been defined, or
10841 a BLOCK if the function has been defined somewhere. */
8d08fdba 10842
058b15c1
MM
10843void
10844start_preparsed_function (tree decl1, tree attrs, int flags)
8d08fdba 10845{
8d08fdba
MS
10846 tree ctype = NULL_TREE;
10847 tree fntype;
10848 tree restype;
8d08fdba 10849 int doing_friend = 0;
e2500fed 10850 struct cp_binding_level *bl;
f444e36b 10851 tree current_function_parms;
3e3935a9 10852 struct c_fileinfo *finfo
c533e34d 10853 = get_fileinfo (LOCATION_FILE (DECL_SOURCE_LOCATION (decl1)));
1ef0df47 10854 bool honor_interface;
8d08fdba 10855
8d08fdba 10856 /* Sanity check. */
50bc768d
NS
10857 gcc_assert (TREE_CODE (TREE_VALUE (void_list_node)) == VOID_TYPE);
10858 gcc_assert (TREE_CHAIN (void_list_node) == NULL_TREE);
8d08fdba 10859
058b15c1
MM
10860 fntype = TREE_TYPE (decl1);
10861 if (TREE_CODE (fntype) == METHOD_TYPE)
10862 ctype = TYPE_METHOD_BASETYPE (fntype);
caf93cb0 10863
058b15c1
MM
10864 /* ISO C++ 11.4/5. A friend function defined in a class is in
10865 the (lexical) scope of the class in which it is defined. */
10866 if (!ctype && DECL_FRIEND_P (decl1))
8d08fdba 10867 {
058b15c1 10868 ctype = DECL_FRIEND_CONTEXT (decl1);
caf93cb0 10869
058b15c1
MM
10870 /* CTYPE could be null here if we're dealing with a template;
10871 for example, `inline friend float foo()' inside a template
10872 will have no CTYPE set. */
10873 if (ctype && TREE_CODE (ctype) != RECORD_TYPE)
10874 ctype = NULL_TREE;
10875 else
10876 doing_friend = 1;
8d08fdba 10877 }
68642fb6 10878
97055d5c
AO
10879 if (DECL_DECLARED_INLINE_P (decl1)
10880 && lookup_attribute ("noinline", attrs))
dee15844 10881 warning (0, "inline function %q+D given attribute noinline", decl1);
97055d5c 10882
3a47c4e4
AO
10883 /* Handle gnu_inline attribute. */
10884 if (GNU_INLINE_P (decl1))
10885 {
10886 DECL_EXTERNAL (decl1) = 1;
10887 DECL_NOT_REALLY_EXTERN (decl1) = 0;
10888 DECL_INTERFACE_KNOWN (decl1) = 1;
10889 }
10890
5f6eeeb3
NS
10891 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (decl1))
10892 /* This is a constructor, we must ensure that any default args
10893 introduced by this definition are propagated to the clones
10894 now. The clones are used directly in overload resolution. */
10895 adjust_clone_args (decl1);
10896
b35d4555
MM
10897 /* Sometimes we don't notice that a function is a static member, and
10898 build a METHOD_TYPE for it. Fix that up now. */
10899 if (ctype != NULL_TREE && DECL_STATIC_FUNCTION_P (decl1)
10900 && TREE_CODE (TREE_TYPE (decl1)) == METHOD_TYPE)
10901 {
3afb32a4 10902 revert_static_member_fn (decl1);
b35d4555
MM
10903 ctype = NULL_TREE;
10904 }
8d08fdba 10905
f181d4ae
MM
10906 /* Set up current_class_type, and enter the scope of the class, if
10907 appropriate. */
10908 if (ctype)
14d22dd6 10909 push_nested_class (ctype);
f181d4ae 10910 else if (DECL_STATIC_FUNCTION_P (decl1))
14d22dd6 10911 push_nested_class (DECL_CONTEXT (decl1));
f181d4ae
MM
10912
10913 /* Now that we have entered the scope of the class, we must restore
10914 the bindings for any template parameters surrounding DECL1, if it
10915 is an inline member template. (Order is important; consider the
10916 case where a template parameter has the same name as a field of
10917 the class.) It is not until after this point that
10918 PROCESSING_TEMPLATE_DECL is guaranteed to be set up correctly. */
a8f73d4b 10919 if (flags & SF_INCLASS_INLINE)
f181d4ae
MM
10920 maybe_begin_member_template_processing (decl1);
10921
56cb9733 10922 /* Effective C++ rule 15. */
9188c363 10923 if (warn_ecpp
596ea4e5 10924 && DECL_OVERLOADED_OPERATOR_P (decl1) == NOP_EXPR
9188c363 10925 && TREE_CODE (TREE_TYPE (fntype)) == VOID_TYPE)
b323323f 10926 warning (OPT_Weffc__, "%<operator=%> should return a reference to %<*this%>");
9188c363
MM
10927
10928 /* Make the init_value nonzero so pushdecl knows this is not tentative.
10929 error_mark_node is replaced below (in poplevel) with the BLOCK. */
b35d4555
MM
10930 if (!DECL_INITIAL (decl1))
10931 DECL_INITIAL (decl1) = error_mark_node;
9188c363 10932
9188c363
MM
10933 /* This function exists in static storage.
10934 (This does not mean `static' in the C sense!) */
10935 TREE_STATIC (decl1) = 1;
10936
10937 /* We must call push_template_decl after current_class_type is set
10938 up. (If we are processing inline definitions after exiting a
10939 class scope, current_class_type will be NULL_TREE until set above
10940 by push_nested_class.) */
10941 if (processing_template_decl)
11e97737
VR
10942 {
10943 /* FIXME: Handle error_mark_node more gracefully. */
10944 tree newdecl1 = push_template_decl (decl1);
10945 if (newdecl1 != error_mark_node)
0cbd7506 10946 decl1 = newdecl1;
11e97737 10947 }
9188c363 10948
f181d4ae 10949 /* We are now in the scope of the function being defined. */
8d08fdba 10950 current_function_decl = decl1;
f181d4ae 10951
5566b478
MS
10952 /* Save the parm names or decls from this function's declarator
10953 where store_parm_decls will find them. */
4546865e 10954 current_function_parms = DECL_ARGUMENTS (decl1);
8d08fdba 10955
a8f73d4b
MM
10956 /* Make sure the parameter and return types are reasonable. When
10957 you declare a function, these types can be incomplete, but they
10958 must be complete when you define the function. */
c9387915 10959 check_function_type (decl1, current_function_parms);
f181d4ae 10960
a8f73d4b
MM
10961 /* Build the return declaration for the function. */
10962 restype = TREE_TYPE (fntype);
e89a6075 10963 if (DECL_RESULT (decl1) == NULL_TREE)
a8f73d4b 10964 {
b785f485
RH
10965 tree resdecl;
10966
10967 resdecl = build_decl (RESULT_DECL, 0, TYPE_MAIN_VARIANT (restype));
10968 DECL_ARTIFICIAL (resdecl) = 1;
10969 DECL_IGNORED_P (resdecl) = 1;
10970 DECL_RESULT (decl1) = resdecl;
10971
9804209d 10972 cp_apply_type_quals_to_decl (cp_type_quals (restype), resdecl);
5566b478 10973 }
a8f73d4b 10974
a8f73d4b 10975 /* Let the user know we're compiling this function. */
ea11ca7e 10976 announce_function (decl1);
b7484fbe 10977
878cd289
MS
10978 /* Record the decl so that the function name is defined.
10979 If we already have a decl for this name, and it is a FUNCTION_DECL,
10980 use the old decl. */
a8f73d4b 10981 if (!processing_template_decl && !(flags & SF_PRE_PARSED))
878cd289 10982 {
75650646 10983 /* A specialization is not used to guide overload resolution. */
18f5be99 10984 if (!DECL_FUNCTION_MEMBER_P (decl1)
caf93cb0 10985 && !(DECL_USE_TEMPLATE (decl1) &&
18f5be99 10986 PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl1))))
b1a19c7c
MM
10987 {
10988 tree olddecl = pushdecl (decl1);
10989
10990 if (olddecl == error_mark_node)
10991 /* If something went wrong when registering the declaration,
10992 use DECL1; we have to have a FUNCTION_DECL to use when
10993 parsing the body of the function. */
10994 ;
10995 else
dfdec7a7
ILT
10996 {
10997 /* Otherwise, OLDDECL is either a previous declaration
10998 of the same function or DECL1 itself. */
10999
11000 if (warn_missing_declarations
11001 && olddecl == decl1
11002 && !DECL_MAIN_P (decl1)
11003 && TREE_PUBLIC (decl1)
11004 && !DECL_DECLARED_INLINE_P (decl1))
11005 {
11006 tree context;
11007
11008 /* Check whether DECL1 is in an anonymous
11009 namespace. */
11010 for (context = DECL_CONTEXT (decl1);
11011 context;
11012 context = DECL_CONTEXT (context))
11013 {
11014 if (TREE_CODE (context) == NAMESPACE_DECL
11015 && DECL_NAME (context) == NULL_TREE)
11016 break;
11017 }
11018
11019 if (context == NULL)
11020 warning (OPT_Wmissing_declarations,
11021 "no previous declaration for %q+D", decl1);
11022 }
11023
11024 decl1 = olddecl;
11025 }
b1a19c7c 11026 }
2c73f9f5 11027 else
b7698cf0 11028 {
a1c65f9f 11029 /* We need to set the DECL_CONTEXT. */
b7698cf0
JM
11030 if (!DECL_CONTEXT (decl1) && DECL_TEMPLATE_INFO (decl1))
11031 DECL_CONTEXT (decl1) = DECL_CONTEXT (DECL_TI_TEMPLATE (decl1));
b7698cf0 11032 }
878cd289 11033 fntype = TREE_TYPE (decl1);
2ef06beb
MM
11034
11035 /* If #pragma weak applies, mark the decl appropriately now.
11036 The pragma only applies to global functions. Because
11037 determining whether or not the #pragma applies involves
11038 computing the mangled name for the declaration, we cannot
11039 apply the pragma until after we have merged this declaration
11040 with any previous declarations; if the original declaration
11041 has a linkage specification, that specification applies to
11042 the definition as well, and may affect the mangled name. */
11043 if (!DECL_CONTEXT (decl1))
11044 maybe_apply_pragma_weak (decl1);
878cd289 11045 }
5566b478 11046
72c4a4ca 11047 /* Reset this in case the call to pushdecl changed it. */
5566b478 11048 current_function_decl = decl1;
72c4a4ca
GK
11049
11050 gcc_assert (DECL_INITIAL (decl1));
11051
11052 /* This function may already have been parsed, in which case just
11053 return; our caller will skip over the body without parsing. */
11054 if (DECL_INITIAL (decl1) != error_mark_node)
11055 return;
11056
11057 /* Initialize RTL machinery. We cannot do this until
11058 CURRENT_FUNCTION_DECL and DECL_RESULT are set up. We do this
11059 even when processing a template; this is how we get
11060 CFUN set up, and our per-function variables initialized.
11061 FIXME factor out the non-RTL stuff. */
11062 bl = current_binding_level;
11063 allocate_struct_function (decl1);
11064 current_binding_level = bl;
11065
11066 /* Even though we're inside a function body, we still don't want to
11067 call expand_expr to calculate the size of a variable-sized array.
11068 We haven't necessarily assigned RTL to all variables yet, so it's
11069 not safe to try to expand expressions involving them. */
11070 cfun->x_dont_save_pending_sizes_p = 1;
11071
11072 /* Start the statement-tree, start the tree now. */
11073 DECL_SAVED_TREE (decl1) = push_stmt_list ();
878cd289 11074
78c120b5
MM
11075 /* If we are (erroneously) defining a function that we have already
11076 defined before, wipe out what we knew before. */
e2500fed
GK
11077 if (!DECL_PENDING_INLINE_P (decl1))
11078 DECL_SAVED_FUNCTION_DATA (decl1) = NULL;
b35d4555 11079
f444e36b 11080 if (ctype && !doing_friend && !DECL_STATIC_FUNCTION_P (decl1))
b35d4555
MM
11081 {
11082 /* We know that this was set up by `grokclassfn'. We do not
11083 wait until `store_parm_decls', since evil parse errors may
11084 never get us to that point. Here we keep the consistency
11085 between `current_class_type' and `current_class_ptr'. */
11086 tree t = DECL_ARGUMENTS (decl1);
68642fb6 11087
50bc768d
NS
11088 gcc_assert (t != NULL_TREE && TREE_CODE (t) == PARM_DECL);
11089 gcc_assert (TREE_CODE (TREE_TYPE (t)) == POINTER_TYPE);
68642fb6
UD
11090
11091 cp_function_chain->x_current_class_ref
3e411c3f 11092 = build_indirect_ref (t, NULL);
b35d4555
MM
11093 cp_function_chain->x_current_class_ptr = t;
11094
018fc244
MM
11095 /* Constructors and destructors need to know whether they're "in
11096 charge" of initializing virtual base classes. */
e0fff4b3 11097 t = TREE_CHAIN (t);
454fa7a7 11098 if (DECL_HAS_IN_CHARGE_PARM_P (decl1))
e0fff4b3
JM
11099 {
11100 current_in_charge_parm = t;
11101 t = TREE_CHAIN (t);
11102 }
11103 if (DECL_HAS_VTT_PARM_P (decl1))
11104 {
8dc2b103 11105 gcc_assert (DECL_NAME (t) == vtt_parm_identifier);
e0fff4b3
JM
11106 current_vtt_parm = t;
11107 }
b35d4555
MM
11108 }
11109
1ef0df47
MM
11110 honor_interface = (!DECL_TEMPLATE_INSTANTIATION (decl1)
11111 /* Implicitly-defined methods (like the
11112 destructor for a class in which no destructor
11113 is explicitly declared) must not be defined
11114 until their definition is needed. So, we
11115 ignore interface specifications for
11116 compiler-generated functions. */
11117 && !DECL_ARTIFICIAL (decl1));
3db45ab5 11118
db5ae43f 11119 if (DECL_INTERFACE_KNOWN (decl1))
faae18ab 11120 {
4f1c5b7d 11121 tree ctx = decl_function_context (decl1);
86052cc3 11122
faae18ab
MS
11123 if (DECL_NOT_REALLY_EXTERN (decl1))
11124 DECL_EXTERNAL (decl1) = 0;
86052cc3 11125
79065db2 11126 if (ctx != NULL_TREE && DECL_DECLARED_INLINE_P (ctx)
86052cc3
JM
11127 && TREE_PUBLIC (ctx))
11128 /* This is a function in a local class in an extern inline
11129 function. */
11130 comdat_linkage (decl1);
faae18ab 11131 }
8d08fdba
MS
11132 /* If this function belongs to an interface, it is public.
11133 If it belongs to someone else's interface, it is also external.
1f901793 11134 This only affects inlines and template instantiations. */
1ef0df47 11135 else if (!finfo->interface_unknown && honor_interface)
8d08fdba 11136 {
caf93cb0 11137 if (DECL_DECLARED_INLINE_P (decl1)
79065db2 11138 || DECL_TEMPLATE_INSTANTIATION (decl1)
5156628f 11139 || processing_template_decl)
1f901793
JM
11140 {
11141 DECL_EXTERNAL (decl1)
5d709b00 11142 = (finfo->interface_only
caf93cb0 11143 || (DECL_DECLARED_INLINE_P (decl1)
79065db2 11144 && ! flag_implement_inlines
9c73ec84 11145 && !DECL_VINDEX (decl1)));
1f901793
JM
11146
11147 /* For WIN32 we also want to put these in linkonce sections. */
11148 maybe_make_one_only (decl1);
11149 }
db5ae43f 11150 else
893de33c 11151 DECL_EXTERNAL (decl1) = 0;
db5ae43f 11152 DECL_INTERFACE_KNOWN (decl1) = 1;
4684cd27 11153 /* If this function is in an interface implemented in this file,
3b426391 11154 make sure that the back end knows to emit this function
4684cd27
MM
11155 here. */
11156 if (!DECL_EXTERNAL (decl1))
11157 mark_needed (decl1);
8d08fdba 11158 }
5d709b00 11159 else if (finfo->interface_unknown && finfo->interface_only
1ef0df47 11160 && honor_interface)
c16c47fb
JM
11161 {
11162 /* If MULTIPLE_SYMBOL_SPACES is defined and we saw a #pragma
5d709b00
ZW
11163 interface, we will have both finfo->interface_unknown and
11164 finfo->interface_only set. In that case, we don't want to
11165 use the normal heuristics because someone will supply a
11166 #pragma implementation elsewhere, and deducing it here would
11167 produce a conflict. */
c16c47fb
JM
11168 comdat_linkage (decl1);
11169 DECL_EXTERNAL (decl1) = 0;
11170 DECL_INTERFACE_KNOWN (decl1) = 1;
11171 DECL_DEFER_OUTPUT (decl1) = 1;
11172 }
8d08fdba 11173 else
a0a33927
MS
11174 {
11175 /* This is a definition, not a reference.
3a47c4e4
AO
11176 So clear DECL_EXTERNAL, unless this is a GNU extern inline. */
11177 if (!GNU_INLINE_P (decl1))
11178 DECL_EXTERNAL (decl1) = 0;
faae18ab 11179
caf93cb0 11180 if ((DECL_DECLARED_INLINE_P (decl1)
79065db2 11181 || DECL_TEMPLATE_INSTANTIATION (decl1))
5566b478
MS
11182 && ! DECL_INTERFACE_KNOWN (decl1)
11183 /* Don't try to defer nested functions for now. */
4f1c5b7d 11184 && ! decl_function_context (decl1))
878cd289
MS
11185 DECL_DEFER_OUTPUT (decl1) = 1;
11186 else
893de33c 11187 DECL_INTERFACE_KNOWN (decl1) = 1;
db5ae43f 11188 }
a9aedbc2 11189
0ed5edac
JM
11190 /* Determine the ELF visibility attribute for the function. We must not
11191 do this before calling "pushdecl", as we must allow "duplicate_decls"
11192 to merge any attributes appropriately. We also need to wait until
11193 linkage is set. */
11194 if (!DECL_CLONED_FUNCTION_P (decl1))
11195 determine_visibility (decl1);
11196
ac20c67a 11197 begin_scope (sk_function_parms, decl1);
8d08fdba 11198
5566b478
MS
11199 ++function_depth;
11200
44d10c10
PB
11201 if (DECL_DESTRUCTOR_P (decl1)
11202 || (DECL_CONSTRUCTOR_P (decl1)
11203 && targetm.cxx.cdtor_returns_this ()))
46e8c075 11204 {
44d10c10
PB
11205 cdtor_label = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE);
11206 DECL_CONTEXT (cdtor_label) = current_function_decl;
46e8c075 11207 }
8d08fdba 11208
0ba8a114 11209 start_fname_decls ();
caf93cb0 11210
f444e36b 11211 store_parm_decls (current_function_parms);
058b15c1
MM
11212}
11213
11214
11215/* Like start_preparsed_function, except that instead of a
11216 FUNCTION_DECL, this function takes DECLSPECS and DECLARATOR.
11217
11218 Returns 1 on success. If the DECLARATOR is not suitable for a function
11219 (it defines a datum instead), we return 0, which tells
11220 yyparse to report a parse error. */
11221
11222int
caf93cb0 11223start_function (cp_decl_specifier_seq *declspecs,
62d1db17 11224 const cp_declarator *declarator,
058b15c1
MM
11225 tree attrs)
11226{
11227 tree decl1;
11228
058b15c1
MM
11229 decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, 1, &attrs);
11230 /* If the declarator is not suitable for a function definition,
11231 cause a syntax error. */
11232 if (decl1 == NULL_TREE || TREE_CODE (decl1) != FUNCTION_DECL)
11233 return 0;
caf93cb0 11234
058b15c1 11235 if (DECL_MAIN_P (decl1))
2cfe82fe
ZW
11236 /* main must return int. grokfndecl should have corrected it
11237 (and issued a diagnostic) if the user got it wrong. */
11238 gcc_assert (same_type_p (TREE_TYPE (TREE_TYPE (decl1)),
11239 integer_type_node));
058b15c1
MM
11240
11241 start_preparsed_function (decl1, attrs, /*flags=*/SF_DEFAULT);
f444e36b 11242
8d08fdba
MS
11243 return 1;
11244}
11245\f
68d04c67
MM
11246/* Returns true iff an EH_SPEC_BLOCK should be created in the body of
11247 FN. */
11248
11249static bool
11250use_eh_spec_block (tree fn)
11251{
11252 return (flag_exceptions && flag_enforce_eh_specs
11253 && !processing_template_decl
11254 && TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn))
11255 /* We insert the EH_SPEC_BLOCK only in the original
11256 function; then, it is copied automatically to the
11257 clones. */
11258 && !DECL_CLONED_FUNCTION_P (fn)
11259 /* Implicitly-generated constructors and destructors have
11260 exception specifications. However, those specifications
11261 are the union of the possible exceptions specified by the
11262 constructors/destructors for bases and members, so no
11263 unallowed exception will ever reach this function. By
11264 not creating the EH_SPEC_BLOCK we save a little memory,
11265 and we avoid spurious warnings about unreachable
11266 code. */
11267 && !DECL_ARTIFICIAL (fn));
11268}
11269
8d08fdba
MS
11270/* Store the parameter declarations into the current function declaration.
11271 This is called after parsing the parameter declarations, before
11272 digesting the body of the function.
11273
11274 Also install to binding contour return value identifier, if any. */
11275
f444e36b 11276static void
11f6b451 11277store_parm_decls (tree current_function_parms)
8d08fdba 11278{
926ce8bd
KH
11279 tree fndecl = current_function_decl;
11280 tree parm;
8d08fdba 11281
8d08fdba
MS
11282 /* This is a chain of any other decls that came in among the parm
11283 declarations. If a parm is declared with enum {foo, bar} x;
11284 then CONST_DECLs for foo and bar are put here. */
11285 tree nonparms = NULL_TREE;
11286
b35d4555 11287 if (current_function_parms)
8d08fdba
MS
11288 {
11289 /* This case is when the function was defined with an ANSI prototype.
11290 The parms already have decls, so we need not do anything here
11291 except record them as in effect
11292 and complain if any redundant old-style parm decls were written. */
11293
b35d4555
MM
11294 tree specparms = current_function_parms;
11295 tree next;
11296
f444e36b 11297 /* Must clear this because it might contain TYPE_DECLs declared
b35d4555 11298 at class level. */
00e8de68 11299 current_binding_level->names = NULL;
8d08fdba 11300
f444e36b 11301 /* If we're doing semantic analysis, then we'll call pushdecl
b35d4555
MM
11302 for each of these. We must do them in reverse order so that
11303 they end in the correct forward order. */
f444e36b 11304 specparms = nreverse (specparms);
5566b478 11305
b35d4555 11306 for (parm = specparms; parm; parm = next)
8d08fdba
MS
11307 {
11308 next = TREE_CHAIN (parm);
11309 if (TREE_CODE (parm) == PARM_DECL)
11310 {
f444e36b
MM
11311 if (DECL_NAME (parm) == NULL_TREE
11312 || TREE_CODE (parm) != VOID_TYPE)
11313 pushdecl (parm);
11314 else
2d01edd7 11315 error ("parameter %qD declared void", parm);
8d08fdba
MS
11316 }
11317 else
11318 {
11319 /* If we find an enum constant or a type tag,
11320 put it aside for the moment. */
11321 TREE_CHAIN (parm) = NULL_TREE;
11322 nonparms = chainon (nonparms, parm);
11323 }
11324 }
11325
f444e36b
MM
11326 /* Get the decls in their original chain order and record in the
11327 function. This is all and only the PARM_DECLs that were
11328 pushed into scope by the loop above. */
11329 DECL_ARGUMENTS (fndecl) = getdecls ();
8d08fdba
MS
11330 }
11331 else
11332 DECL_ARGUMENTS (fndecl) = NULL_TREE;
11333
11334 /* Now store the final chain of decls for the arguments
11335 as the decl-chain of the current lexical scope.
11336 Put the enumerators in as well, at the front so that
11337 DECL_ARGUMENTS is not modified. */
00e8de68 11338 current_binding_level->names = chainon (nonparms, DECL_ARGUMENTS (fndecl));
eb448459 11339
68d04c67
MM
11340 if (use_eh_spec_block (current_function_decl))
11341 current_eh_spec_block = begin_eh_spec_block ();
8d08fdba
MS
11342}
11343
8d08fdba 11344\f
59026e79
MM
11345/* We have finished doing semantic analysis on DECL, but have not yet
11346 generated RTL for its body. Save away our current state, so that
11347 when we want to generate RTL later we know what to do. */
11348
11349static void
11f6b451 11350save_function_data (tree decl)
59026e79 11351{
e2500fed 11352 struct language_function *f;
59026e79
MM
11353
11354 /* Save the language-specific per-function data so that we can
11355 get it back when we really expand this function. */
50bc768d 11356 gcc_assert (!DECL_PENDING_INLINE_P (decl));
68642fb6 11357
59026e79 11358 /* Make a copy. */
99dd239f 11359 f = GGC_NEW (struct language_function);
e2500fed 11360 memcpy (f, cp_function_chain, sizeof (struct language_function));
59026e79
MM
11361 DECL_SAVED_FUNCTION_DATA (decl) = f;
11362
11363 /* Clear out the bits we don't need. */
325c3691 11364 f->base.x_stmt_tree.x_cur_stmt_list = NULL_TREE;
59026e79 11365 f->bindings = NULL;
8e4ce833 11366 f->x_local_names = NULL;
59026e79
MM
11367}
11368
cdd2559c 11369
44d10c10 11370/* Set the return value of the constructor (if present). */
efee38a9
MM
11371
11372static void
11f6b451 11373finish_constructor_body (void)
efee38a9 11374{
44d10c10
PB
11375 tree val;
11376 tree exprstmt;
11377
11378 if (targetm.cxx.cdtor_returns_this ())
11379 {
11380 /* Any return from a constructor will end up here. */
11381 add_stmt (build_stmt (LABEL_EXPR, cdtor_label));
11382
11383 val = DECL_ARGUMENTS (current_function_decl);
f293ce4b
RS
11384 val = build2 (MODIFY_EXPR, TREE_TYPE (val),
11385 DECL_RESULT (current_function_decl), val);
44d10c10
PB
11386 /* Return the address of the object. */
11387 exprstmt = build_stmt (RETURN_EXPR, val);
11388 add_stmt (exprstmt);
11389 }
efee38a9
MM
11390}
11391
cdd2559c
JM
11392/* Do all the processing for the beginning of a destructor; set up the
11393 vtable pointers and cleanups for bases and members. */
11394
11395static void
11f6b451 11396begin_destructor_body (void)
cdd2559c 11397{
cdd2559c
JM
11398 tree compound_stmt;
11399
0e686aa6
MM
11400 /* If the CURRENT_CLASS_TYPE is incomplete, we will have already
11401 issued an error message. We still want to try to process the
11402 body of the function, but initialize_vtbl_ptrs will crash if
11403 TYPE_BINFO is NULL. */
11404 if (COMPLETE_TYPE_P (current_class_type))
11405 {
11406 compound_stmt = begin_compound_stmt (0);
11407 /* Make all virtual function table pointers in non-virtual base
11408 classes point to CURRENT_CLASS_TYPE's virtual function
11409 tables. */
11410 initialize_vtbl_ptrs (current_class_ptr);
11411 finish_compound_stmt (compound_stmt);
3db45ab5 11412
0e686aa6
MM
11413 /* And insert cleanups for our bases and members so that they
11414 will be properly destroyed if we throw. */
11415 push_base_cleanups ();
11416 }
cdd2559c
JM
11417}
11418
ade3dc07
JM
11419/* At the end of every destructor we generate code to delete the object if
11420 necessary. Do that now. */
9bfadf57
MM
11421
11422static void
11f6b451 11423finish_destructor_body (void)
9bfadf57 11424{
9bfadf57
MM
11425 tree exprstmt;
11426
5633b37c
MM
11427 /* Any return from a destructor will end up here; that way all base
11428 and member cleanups will be run when the function returns. */
44d10c10 11429 add_stmt (build_stmt (LABEL_EXPR, cdtor_label));
5633b37c 11430
52682a1b
MM
11431 /* In a virtual destructor, we must call delete. */
11432 if (DECL_VIRTUAL_P (current_function_decl))
11433 {
11434 tree if_stmt;
fa72b064 11435 tree virtual_size = cxx_sizeof (current_class_type);
68642fb6 11436
52682a1b 11437 /* [class.dtor]
68642fb6 11438
ade3dc07
JM
11439 At the point of definition of a virtual destructor (including
11440 an implicit definition), non-placement operator delete shall
11441 be looked up in the scope of the destructor's class and if
11442 found shall be accessible and unambiguous. */
3db45ab5 11443 exprstmt = build_op_delete_call(DELETE_EXPR, current_class_ptr,
63c9a190 11444 virtual_size,
3db45ab5
MS
11445 /*global_p=*/false,
11446 /*placement=*/NULL_TREE,
63c9a190 11447 /*alloc_fn=*/NULL_TREE);
298d6f60 11448
52682a1b 11449 if_stmt = begin_if_stmt ();
f293ce4b
RS
11450 finish_if_stmt_cond (build2 (BIT_AND_EXPR, integer_type_node,
11451 current_in_charge_parm,
11452 integer_one_node),
52682a1b
MM
11453 if_stmt);
11454 finish_expr_stmt (exprstmt);
11455 finish_then_clause (if_stmt);
325c3691 11456 finish_if_stmt (if_stmt);
52682a1b 11457 }
44d10c10
PB
11458
11459 if (targetm.cxx.cdtor_returns_this ())
11460 {
11461 tree val;
11462
11463 val = DECL_ARGUMENTS (current_function_decl);
f293ce4b
RS
11464 val = build2 (MODIFY_EXPR, TREE_TYPE (val),
11465 DECL_RESULT (current_function_decl), val);
44d10c10
PB
11466 /* Return the address of the object. */
11467 exprstmt = build_stmt (RETURN_EXPR, val);
11468 add_stmt (exprstmt);
11469 }
ade3dc07 11470}
9bfadf57 11471
ade3dc07
JM
11472/* Do the necessary processing for the beginning of a function body, which
11473 in this case includes member-initializers, but not the catch clauses of
11474 a function-try-block. Currently, this means opening a binding level
86ad3aa9 11475 for the member-initializers (in a ctor) and member cleanups (in a dtor). */
ade3dc07
JM
11476
11477tree
11f6b451 11478begin_function_body (void)
ade3dc07 11479{
cdd2559c
JM
11480 tree stmt;
11481
86ad3aa9
JM
11482 if (! FUNCTION_NEEDS_BODY_BLOCK (current_function_decl))
11483 return NULL_TREE;
11484
b5856475
JM
11485 if (processing_template_decl)
11486 /* Do nothing now. */;
11487 else
11488 /* Always keep the BLOCK node associated with the outermost pair of
11489 curly braces of a function. These are needed for correct
11490 operation of dwarfout.c. */
ac20c67a 11491 keep_next_level (true);
b5856475 11492
325c3691 11493 stmt = begin_compound_stmt (BCS_FN_BODY);
cdd2559c
JM
11494
11495 if (processing_template_decl)
11496 /* Do nothing now. */;
cdd2559c
JM
11497 else if (DECL_DESTRUCTOR_P (current_function_decl))
11498 begin_destructor_body ();
11499
ade3dc07 11500 return stmt;
9bfadf57
MM
11501}
11502
ade3dc07
JM
11503/* Do the processing for the end of a function body. Currently, this means
11504 closing out the cleanups for fully-constructed bases and members, and in
11505 the case of the destructor, deleting the object if desired. Again, this
11506 is only meaningful for [cd]tors, since they are the only functions where
11507 there is a significant distinction between the main body and any
11508 function catch clauses. Handling, say, main() return semantics here
11509 would be wrong, as flowing off the end of a function catch clause for
11510 main() would also need to return 0. */
11511
11512void
11f6b451 11513finish_function_body (tree compstmt)
ade3dc07 11514{
86ad3aa9
JM
11515 if (compstmt == NULL_TREE)
11516 return;
3db45ab5 11517
5633b37c 11518 /* Close the block. */
7a3397c7 11519 finish_compound_stmt (compstmt);
ade3dc07
JM
11520
11521 if (processing_template_decl)
11522 /* Do nothing now. */;
11523 else if (DECL_CONSTRUCTOR_P (current_function_decl))
11524 finish_constructor_body ();
11525 else if (DECL_DESTRUCTOR_P (current_function_decl))
11526 finish_destructor_body ();
caf93cb0 11527}
ade3dc07 11528
86ad3aa9
JM
11529/* Given a function, returns the BLOCK corresponding to the outermost level
11530 of curly braces, skipping the artificial block created for constructor
11531 initializers. */
11532
11533static tree
11534outer_curly_brace_block (tree fndecl)
11535{
11536 tree block = BLOCK_SUBBLOCKS (DECL_INITIAL (fndecl));
11537 if (FUNCTION_NEEDS_BODY_BLOCK (current_function_decl))
11538 /* Skip the artificial function body block. */
11539 block = BLOCK_SUBBLOCKS (block);
11540 return block;
11541}
11542
8d08fdba
MS
11543/* Finish up a function declaration and compile that function
11544 all the way to assembler language output. The free the storage
11545 for the function definition.
11546
68642fb6 11547 FLAGS is a bitwise or of the following values:
f181d4ae
MM
11548 2 - INCLASS_INLINE
11549 We just finished processing the body of an in-class inline
11550 function definition. (This processing will have taken place
87e3dbc9 11551 after the class definition is complete.) */
8d08fdba 11552
4d6abc1c 11553tree
11f6b451 11554finish_function (int flags)
8d08fdba 11555{
926ce8bd 11556 tree fndecl = current_function_decl;
8d08fdba 11557 tree fntype, ctype = NULL_TREE;
f181d4ae 11558 int inclass_inline = (flags & 2) != 0;
87e3dbc9 11559 int nested;
8d08fdba
MS
11560
11561 /* When we get some parse errors, we can end up without a
11562 current_function_decl, so cope. */
11563 if (fndecl == NULL_TREE)
4d6abc1c 11564 return error_mark_node;
8d08fdba 11565
9aad8f83
MA
11566 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fndecl)
11567 && DECL_VIRTUAL_P (fndecl)
11568 && !processing_template_decl)
11569 {
11570 tree fnclass = DECL_CONTEXT (fndecl);
11571 if (fndecl == CLASSTYPE_KEY_METHOD (fnclass))
11572 keyed_classes = tree_cons (NULL_TREE, fnclass, keyed_classes);
11573 }
11574
87e3dbc9 11575 nested = function_depth > 1;
8d08fdba
MS
11576 fntype = TREE_TYPE (fndecl);
11577
9bfadf57
MM
11578 /* TREE_READONLY (fndecl) = 1;
11579 This caused &foo to be of type ptr-to-const-function
11580 which then got a warning when stored in a ptr-to-function variable. */
8d08fdba 11581
50bc768d 11582 gcc_assert (building_stmt_tree ());
72c4a4ca
GK
11583 /* The current function is being defined, so its DECL_INITIAL should
11584 be set, and unless there's a multiple definition, it should be
11585 error_mark_node. */
11586 gcc_assert (DECL_INITIAL (fndecl) == error_mark_node);
caf93cb0 11587
db9b2174
MM
11588 /* For a cloned function, we've already got all the code we need;
11589 there's no need to add any extra bits. */
f444e36b 11590 if (!DECL_CLONED_FUNCTION_P (fndecl))
8d08fdba 11591 {
ade3dc07 11592 if (DECL_MAIN_P (current_function_decl))
efee38a9 11593 {
43e05e45
SB
11594 tree stmt;
11595
11596 /* Make it so that `main' always returns 0 by default (or
11597 1 for VMS). */
08c7ae5a 11598#if VMS_TARGET
43e05e45 11599 stmt = finish_return_stmt (integer_one_node);
efee38a9 11600#else
43e05e45 11601 stmt = finish_return_stmt (integer_zero_node);
efee38a9 11602#endif
43e05e45
SB
11603 /* Hack. We don't want the middle-end to warn that this
11604 return is unreachable, so put the statement on the
11605 special line 0. */
176a0aad
PB
11606#ifdef USE_MAPPED_LOCATION
11607 SET_EXPR_LOCATION (stmt, UNKNOWN_LOCATION);
11608#else
43e05e45 11609 annotate_with_file_line (stmt, input_filename, 0);
176a0aad 11610#endif
efee38a9 11611 }
87e3dbc9 11612
68d04c67 11613 if (use_eh_spec_block (current_function_decl))
52a11cbf
RH
11614 finish_eh_spec_block (TYPE_RAISES_EXCEPTIONS
11615 (TREE_TYPE (current_function_decl)),
11616 current_eh_spec_block);
5566b478 11617 }
68642fb6 11618
558475f0 11619 /* If we're saving up tree structure, tie off the function now. */
325c3691
RH
11620 DECL_SAVED_TREE (fndecl) = pop_stmt_list (DECL_SAVED_TREE (fndecl));
11621
11622 finish_fname_decls ();
8d2733ca 11623
b2dd096b
MM
11624 /* If this function can't throw any exceptions, remember that. */
11625 if (!processing_template_decl
11626 && !cp_function_chain->can_throw
be0436d7 11627 && !flag_non_call_exceptions
56fd65c7 11628 && !DECL_REPLACEABLE_P (fndecl))
b2dd096b
MM
11629 TREE_NOTHROW (fndecl) = 1;
11630
8d08fdba
MS
11631 /* This must come after expand_function_end because cleanups might
11632 have declarations (from inline functions) that need to go into
11633 this function's blocks. */
caf93cb0 11634
7437519c
ZW
11635 /* If the current binding level isn't the outermost binding level
11636 for this function, either there is a bug, or we have experienced
11637 syntax errors and the statement tree is malformed. */
a7e8c268 11638 if (current_binding_level->kind != sk_function_parms)
7437519c
ZW
11639 {
11640 /* Make sure we have already experienced errors. */
8dc2b103 11641 gcc_assert (errorcount);
7437519c
ZW
11642
11643 /* Throw away the broken statement tree and extra binding
0cbd7506 11644 levels. */
5882f0f3 11645 DECL_SAVED_TREE (fndecl) = alloc_stmt_list ();
7437519c 11646
a7e8c268 11647 while (current_binding_level->kind != sk_function_parms)
7437519c 11648 {
a7e8c268 11649 if (current_binding_level->kind == sk_class)
7437519c
ZW
11650 pop_nested_class ();
11651 else
11652 poplevel (0, 0, 0);
11653 }
11654 }
f444e36b 11655 poplevel (1, 0, 1);
8d08fdba 11656
4985cde3
RH
11657 /* Statements should always be full-expressions at the outermost set
11658 of curly braces for a function. */
50bc768d 11659 gcc_assert (stmts_are_full_exprs_p ());
4985cde3 11660
6de9cd9a 11661 /* Set up the named return value optimization, if we can. Candidate
59fe9274 11662 variables are selected in check_return_expr. */
07b2f2fd
JM
11663 if (current_function_return_value)
11664 {
11665 tree r = current_function_return_value;
35e939ae
JM
11666 tree outer;
11667
07b2f2fd 11668 if (r != error_mark_node
35e939ae
JM
11669 /* This is only worth doing for fns that return in memory--and
11670 simpler, since we don't have to worry about promoted modes. */
61f71b34 11671 && aggregate_value_p (TREE_TYPE (TREE_TYPE (fndecl)), fndecl)
35e939ae
JM
11672 /* Only allow this for variables declared in the outer scope of
11673 the function so we know that their lifetime always ends with a
11674 return; see g++.dg/opt/nrv6.C. We could be more flexible if
11675 we were to do this optimization in tree-ssa. */
86ad3aa9 11676 && (outer = outer_curly_brace_block (fndecl))
325c3691 11677 && chain_member (r, BLOCK_VARS (outer)))
6de9cd9a
DN
11678 finalize_nrv (&DECL_SAVED_TREE (fndecl), r, DECL_RESULT (fndecl));
11679
11680 current_function_return_value = NULL_TREE;
07b2f2fd
JM
11681 }
11682
a8f73d4b 11683 /* Remember that we were in class scope. */
db5ae43f 11684 if (current_class_name)
a8f73d4b 11685 ctype = current_class_type;
db5ae43f 11686
1caa11d3
MM
11687 /* Must mark the RESULT_DECL as being in this function. */
11688 DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
11689
11690 /* Set the BLOCK_SUPERCONTEXT of the outermost function scope to point
11691 to the FUNCTION_DECL node itself. */
11692 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
11693
59026e79 11694 /* Save away current state, if appropriate. */
f444e36b 11695 if (!processing_template_decl)
59026e79
MM
11696 save_function_data (fndecl);
11697
efe49da0 11698 /* Complain if there's just no return statement. */
46cfb101 11699 if (warn_return_type
efe49da0 11700 && TREE_CODE (TREE_TYPE (fntype)) != VOID_TYPE
c1b83cc9 11701 && !dependent_type_p (TREE_TYPE (fntype))
57d5032b 11702 && !current_function_returns_value && !current_function_returns_null
efe49da0
JM
11703 /* Don't complain if we abort or throw. */
11704 && !current_function_returns_abnormally
55e99d52 11705 && !DECL_NAME (DECL_RESULT (fndecl))
46cfb101 11706 /* Normally, with -Wreturn-type, flow will complain. Unless we're an
efe49da0 11707 inline function, as we might never be compiled separately. */
44d10c10
PB
11708 && (DECL_INLINE (fndecl) || processing_template_decl)
11709 /* Structor return values (if any) are set by the compiler. */
11710 && !DECL_CONSTRUCTOR_P (fndecl)
11711 && !DECL_DESTRUCTOR_P (fndecl))
b323323f 11712 warning (OPT_Wreturn_type, "no return statement in function returning non-void");
4985cde3 11713
6de9cd9a
DN
11714 /* Store the end of the function, so that we get good line number
11715 info for the epilogue. */
11716 cfun->function_end_locus = input_location;
11717
11718 /* Genericize before inlining. */
11719 if (!processing_template_decl)
11720 {
37e6d6fe 11721 struct language_function *f = DECL_SAVED_FUNCTION_DATA (fndecl);
5a508662 11722 cp_genericize (fndecl);
37e6d6fe
JH
11723 /* Clear out the bits we don't need. */
11724 f->x_current_class_ptr = NULL;
11725 f->x_current_class_ref = NULL;
11726 f->x_eh_spec_block = NULL;
11727 f->x_in_charge_parm = NULL;
11728 f->x_vtt_parm = NULL;
11729 f->x_return_value = NULL;
11730 f->bindings = NULL;
10827cd8 11731 f->extern_decl_map = NULL;
6de9cd9a
DN
11732
11733 /* Handle attribute((warn_unused_result)). Relies on gimple input. */
11734 c_warn_unused_result (&DECL_SAVED_TREE (fndecl));
11735 }
37e6d6fe
JH
11736 /* Clear out the bits we don't need. */
11737 local_names = NULL;
6de9cd9a
DN
11738
11739 /* We're leaving the context of this function, so zap cfun. It's still in
11740 DECL_STRUCT_FUNCTION, and we'll restore it in tree_rest_of_compilation. */
f444e36b 11741 cfun = NULL;
c1f927e8 11742 current_function_decl = NULL;
a8f73d4b 11743
27631dae 11744 /* If this is an in-class inline definition, we may have to pop the
a8f73d4b
MM
11745 bindings for the template parameters that we added in
11746 maybe_begin_member_template_processing when start_function was
11747 called. */
11748 if (inclass_inline)
11749 maybe_end_member_template_processing ();
11750
11751 /* Leave the scope of the class. */
11752 if (ctype)
11753 pop_nested_class ();
5566b478
MS
11754
11755 --function_depth;
8d08fdba 11756
4d6abc1c 11757 /* Clean up. */
28cbf42c 11758 if (! nested)
1f8f4a0b
MM
11759 /* Let the error reporting routines know that we're outside a
11760 function. For a nested function, this value is used in
b03e38e1 11761 cxx_pop_function_context and then reset via pop_function_context. */
1f8f4a0b 11762 current_function_decl = NULL_TREE;
4d6abc1c
MM
11763
11764 return fndecl;
8d08fdba
MS
11765}
11766\f
11767/* Create the FUNCTION_DECL for a function definition.
8d08fdba
MS
11768 DECLSPECS and DECLARATOR are the parts of the declaration;
11769 they describe the return type and the name of the function,
11770 but twisted together in a fashion that parallels the syntax of C.
11771
11772 This function creates a binding context for the function body
11773 as well as setting up the FUNCTION_DECL in current_function_decl.
11774
11775 Returns a FUNCTION_DECL on success.
11776
11777 If the DECLARATOR is not suitable for a function (it defines a datum
11778 instead), we return 0, which tells yyparse to report a parse error.
11779
11780 May return void_type_node indicating that this method is actually
11781 a friend. See grokfield for more details.
11782
11783 Came here with a `.pushlevel' .
11784
11785 DO NOT MAKE ANY CHANGES TO THIS CODE WITHOUT MAKING CORRESPONDING
11786 CHANGES TO CODE IN `grokfield'. */
e92cc029 11787
8d08fdba 11788tree
caf93cb0 11789start_method (cp_decl_specifier_seq *declspecs,
0cbd7506 11790 const cp_declarator *declarator, tree attrlist)
8d08fdba 11791{
c11b6f21 11792 tree fndecl = grokdeclarator (declarator, declspecs, MEMFUNCDEF, 0,
91d231cb 11793 &attrlist);
8d08fdba 11794
a723baf1
MM
11795 if (fndecl == error_mark_node)
11796 return error_mark_node;
11797
11798 if (fndecl == NULL || TREE_CODE (fndecl) != FUNCTION_DECL)
11799 {
11800 error ("invalid member function declaration");
11801 return error_mark_node;
11802 }
8d08fdba 11803
195a5def
IR
11804 if (attrlist)
11805 cplus_decl_attributes (&fndecl, attrlist, 0);
11806
8d08fdba 11807 /* Pass friends other than inline friend functions back. */
a1774733 11808 if (fndecl == void_type_node)
8d08fdba
MS
11809 return fndecl;
11810
8d08fdba
MS
11811 if (DECL_IN_AGGR_P (fndecl))
11812 {
58ec3cc5 11813 if (DECL_CONTEXT (fndecl)
6d69fe27 11814 && TREE_CODE (DECL_CONTEXT (fndecl)) != NAMESPACE_DECL)
2d01edd7 11815 error ("%qD is already defined in class %qT", fndecl,
58ec3cc5 11816 DECL_CONTEXT (fndecl));
8ca4bf25 11817 return error_mark_node;
8d08fdba
MS
11818 }
11819
f3400fe2
JM
11820 check_template_shadow (fndecl);
11821
79065db2 11822 DECL_DECLARED_INLINE_P (fndecl) = 1;
8926095f 11823 if (flag_default_inline)
8d08fdba
MS
11824 DECL_INLINE (fndecl) = 1;
11825
36a117a5
MM
11826 /* We process method specializations in finish_struct_1. */
11827 if (processing_template_decl && !DECL_TEMPLATE_SPECIALIZATION (fndecl))
4b0d3cbe
MM
11828 {
11829 fndecl = push_template_decl (fndecl);
11830 if (fndecl == error_mark_node)
11831 return fndecl;
11832 }
a0a33927 11833
8d08fdba
MS
11834 if (! DECL_FRIEND_P (fndecl))
11835 {
8d08fdba
MS
11836 if (TREE_CHAIN (fndecl))
11837 {
11838 fndecl = copy_node (fndecl);
11839 TREE_CHAIN (fndecl) = NULL_TREE;
11840 }
8d08fdba
MS
11841 }
11842
d174af6c 11843 finish_decl (fndecl, NULL_TREE, NULL_TREE);
8d08fdba 11844
f4f206f4 11845 /* Make a place for the parms. */
ac20c67a 11846 begin_scope (sk_function_parms, fndecl);
68642fb6 11847
8d08fdba
MS
11848 DECL_IN_AGGR_P (fndecl) = 1;
11849 return fndecl;
11850}
11851
11852/* Go through the motions of finishing a function definition.
11853 We don't compile this method until after the whole class has
11854 been processed.
11855
11856 FINISH_METHOD must return something that looks as though it
11857 came from GROKFIELD (since we are defining a method, after all).
11858
11859 This is called after parsing the body of the function definition.
11860 STMTS is the chain of statements that makes up the function body.
11861
11862 DECL is the ..._DECL that `start_method' provided. */
11863
11864tree
11f6b451 11865finish_method (tree decl)
8d08fdba 11866{
926ce8bd 11867 tree fndecl = decl;
8d08fdba 11868 tree old_initial;
8d08fdba 11869
926ce8bd 11870 tree link;
8d08fdba 11871
a1774733 11872 if (decl == void_type_node)
8d08fdba
MS
11873 return decl;
11874
11875 old_initial = DECL_INITIAL (fndecl);
11876
11877 /* Undo the level for the parms (from start_method).
11878 This is like poplevel, but it causes nothing to be
11879 saved. Saving information here confuses symbol-table
11880 output routines. Besides, this information will
11881 be correctly output when this method is actually
11882 compiled. */
11883
11884 /* Clear out the meanings of the local variables of this level;
11885 also record in each decl which block it belongs to. */
11886
11887 for (link = current_binding_level->names; link; link = TREE_CHAIN (link))
11888 {
11889 if (DECL_NAME (link) != NULL_TREE)
d8f8dca1 11890 pop_binding (DECL_NAME (link), link);
50bc768d 11891 gcc_assert (TREE_CODE (link) != FUNCTION_DECL);
8d08fdba
MS
11892 DECL_CONTEXT (link) = NULL_TREE;
11893 }
11894
8d08fdba
MS
11895 poplevel (0, 0, 0);
11896
11897 DECL_INITIAL (fndecl) = old_initial;
11898
11899 /* We used to check if the context of FNDECL was different from
11900 current_class_type as another way to get inside here. This didn't work
11901 for String.cc in libg++. */
11902 if (DECL_FRIEND_P (fndecl))
11903 {
d4e6fecb 11904 VEC_safe_push (tree, gc, CLASSTYPE_INLINE_FRIENDS (current_class_type),
585b44d3 11905 fndecl);
8d08fdba
MS
11906 decl = void_type_node;
11907 }
11908
11909 return decl;
11910}
11911\f
0154eaa8
MM
11912
11913/* VAR is a VAR_DECL. If its type is incomplete, remember VAR so that
11914 we can lay it out later, when and if its type becomes complete. */
8d08fdba
MS
11915
11916void
11f6b451 11917maybe_register_incomplete_var (tree var)
8d08fdba 11918{
50bc768d 11919 gcc_assert (TREE_CODE (var) == VAR_DECL);
8d08fdba 11920
0154eaa8 11921 /* Keep track of variables with incomplete types. */
caf93cb0 11922 if (!processing_template_decl && TREE_TYPE (var) != error_mark_node
0154eaa8 11923 && DECL_EXTERNAL (var))
70adf8a9 11924 {
0154eaa8 11925 tree inner_type = TREE_TYPE (var);
caf93cb0 11926
0154eaa8
MM
11927 while (TREE_CODE (inner_type) == ARRAY_TYPE)
11928 inner_type = TREE_TYPE (inner_type);
11929 inner_type = TYPE_MAIN_VARIANT (inner_type);
caf93cb0 11930
0154eaa8
MM
11931 if ((!COMPLETE_TYPE_P (inner_type) && CLASS_TYPE_P (inner_type))
11932 /* RTTI TD entries are created while defining the type_info. */
11933 || (TYPE_LANG_SPECIFIC (inner_type)
11934 && TYPE_BEING_DEFINED (inner_type)))
11935 incomplete_vars = tree_cons (inner_type, var, incomplete_vars);
70adf8a9 11936 }
0154eaa8 11937}
70adf8a9 11938
0154eaa8
MM
11939/* Called when a class type (given by TYPE) is defined. If there are
11940 any existing VAR_DECLs whose type hsa been completed by this
11941 declaration, update them now. */
70adf8a9 11942
0154eaa8 11943void
11f6b451 11944complete_vars (tree type)
0154eaa8
MM
11945{
11946 tree *list = &incomplete_vars;
11947
50bc768d 11948 gcc_assert (CLASS_TYPE_P (type));
caf93cb0 11949 while (*list)
0154eaa8
MM
11950 {
11951 if (same_type_p (type, TREE_PURPOSE (*list)))
70adf8a9 11952 {
0154eaa8 11953 tree var = TREE_VALUE (*list);
67935995 11954 tree type = TREE_TYPE (var);
c740732f
MM
11955 /* Complete the type of the variable. The VAR_DECL itself
11956 will be laid out in expand_expr. */
67935995
MM
11957 complete_type (type);
11958 cp_apply_type_quals_to_decl (cp_type_quals (type), var);
0154eaa8
MM
11959 /* Remove this entry from the list. */
11960 *list = TREE_CHAIN (*list);
f30432d7
MS
11961 }
11962 else
0154eaa8 11963 list = &TREE_CHAIN (*list);
f30432d7 11964 }
7fb213d8
GB
11965
11966 /* Check for pending declarations which may have abstract type. */
11967 complete_type_check_abstract (type);
8d08fdba
MS
11968}
11969
5c113154
MM
11970/* If DECL is of a type which needs a cleanup, build and return an
11971 expression to perform that cleanup here. Return NULL_TREE if no
11972 cleanup need be done. */
e92cc029 11973
86f45d2c 11974tree
11f6b451 11975cxx_maybe_build_cleanup (tree decl)
8d08fdba 11976{
5c113154
MM
11977 tree type;
11978 tree attr;
11979 tree cleanup;
11980
11981 /* Assume no cleanup is required. */
11982 cleanup = NULL_TREE;
86f45d2c 11983
5c113154
MM
11984 if (error_operand_p (decl))
11985 return cleanup;
11986
11987 /* Handle "__attribute__((cleanup))". We run the cleanup function
11988 before the destructor since the destructor is what actually
11989 terminates the lifetime of the object. */
11990 attr = lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl));
11991 if (attr)
11992 {
11993 tree id;
11994 tree fn;
11995 tree arg;
11996
11997 /* Get the name specified by the user for the cleanup function. */
11998 id = TREE_VALUE (TREE_VALUE (attr));
11999 /* Look up the name to find the cleanup function to call. It is
12000 important to use lookup_name here because that is what is
12001 used in c-common.c:handle_cleanup_attribute when performing
12002 initial checks on the attribute. Note that those checks
12003 include ensuring that the function found is not an overloaded
12004 function, or an object with an overloaded call operator,
12005 etc.; we can rely on the fact that the functionfound is an
12006 ordinary FUNCTION_DECL. */
12007 fn = lookup_name (id);
12008 arg = build_address (decl);
12009 mark_used (decl);
12010 cleanup = build_function_call (fn, build_tree_list (NULL_TREE,
12011 arg));
12012 }
12013 /* Handle ordinary C++ destructors. */
12014 type = TREE_TYPE (decl);
12015 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
8d08fdba 12016 {
80048418 12017 int flags = LOOKUP_NORMAL|LOOKUP_DESTRUCTOR;
5775a06a
NS
12018 bool has_vbases = (TREE_CODE (type) == RECORD_TYPE
12019 && CLASSTYPE_VBASECLASSES (type));
5c113154
MM
12020 tree addr;
12021 tree call;
8d08fdba 12022
8d08fdba 12023 if (TREE_CODE (type) == ARRAY_TYPE)
5c113154 12024 addr = decl;
8d08fdba
MS
12025 else
12026 {
dffd7eb6 12027 cxx_mark_addressable (decl);
5c113154 12028 addr = build_unary_op (ADDR_EXPR, decl, 0);
8d08fdba
MS
12029 }
12030
12031 /* Optimize for space over speed here. */
5775a06a 12032 if (!has_vbases || flag_expensive_optimizations)
8d08fdba
MS
12033 flags |= LOOKUP_NONVIRTUAL;
12034
5c113154 12035 call = build_delete (TREE_TYPE (addr), addr,
86f45d2c 12036 sfk_complete_destructor, flags, 0);
5c113154
MM
12037 if (cleanup)
12038 cleanup = build_compound_expr (cleanup, call);
12039 else
12040 cleanup = call;
8d08fdba 12041 }
5c113154
MM
12042
12043 return cleanup;
8d08fdba
MS
12044}
12045\f
558475f0 12046/* When a stmt has been parsed, this function is called. */
8d08fdba
MS
12047
12048void
11f6b451 12049finish_stmt (void)
8d08fdba 12050{
8d08fdba
MS
12051}
12052
3afb32a4
MM
12053/* DECL was originally constructed as a non-static member function,
12054 but turned out to be static. Update it accordingly. */
700f8a87 12055
8857f91e 12056void
11f6b451 12057revert_static_member_fn (tree decl)
8d08fdba 12058{
700f8a87 12059 tree tmp;
3afb32a4
MM
12060 tree function = TREE_TYPE (decl);
12061 tree args = TYPE_ARG_TYPES (function);
8d08fdba 12062
89d684bb 12063 if (cp_type_quals (TREE_TYPE (TREE_VALUE (args)))
91063b51 12064 != TYPE_UNQUALIFIED)
c4f73174 12065 error ("static member function %q#D declared with type qualifiers", decl);
f30432d7 12066
700f8a87
MS
12067 args = TREE_CHAIN (args);
12068 tmp = build_function_type (TREE_TYPE (function), args);
89d684bb 12069 tmp = build_qualified_type (tmp, cp_type_quals (function));
f30432d7 12070 tmp = build_exception_variant (tmp,
8d08fdba 12071 TYPE_RAISES_EXCEPTIONS (function));
3afb32a4
MM
12072 TREE_TYPE (decl) = tmp;
12073 if (DECL_ARGUMENTS (decl))
12074 DECL_ARGUMENTS (decl) = TREE_CHAIN (DECL_ARGUMENTS (decl));
12075 DECL_STATIC_FUNCTION_P (decl) = 1;
8d08fdba 12076}
a4443a08 12077
68642fb6
UD
12078/* Initialize the variables used during compilation of a C++
12079 function. */
db5ae43f 12080
b03e38e1 12081void
11f6b451 12082cxx_push_function_context (struct function * f)
99dccabc 12083{
99dd239f 12084 struct language_function *p = GGC_CNEW (struct language_function);
e2500fed 12085 f->language = p;
db5ae43f 12086
f1dedc31
MM
12087 /* Whenever we start a new function, we destroy temporaries in the
12088 usual way. */
ae499cce 12089 current_stmt_tree ()->stmts_are_full_exprs_p = 1;
4985cde3
RH
12090
12091 if (f->decl)
12092 {
12093 tree fn = f->decl;
12094
4985cde3
RH
12095 if (DECL_SAVED_FUNCTION_DATA (fn))
12096 {
12097 /* If we already parsed this function, and we're just expanding it
12098 now, restore saved state. */
12099 *cp_function_chain = *DECL_SAVED_FUNCTION_DATA (fn);
12100
4985cde3 12101 /* We don't need the saved data anymore. Unless this is an inline
caf93cb0 12102 function; we need the named return value info for
7740f00d 12103 declare_return_variable. */
4985cde3
RH
12104 if (! DECL_INLINE (fn))
12105 DECL_SAVED_FUNCTION_DATA (fn) = NULL;
12106 }
12107 }
db5ae43f
MS
12108}
12109
a8f73d4b
MM
12110/* Free the language-specific parts of F, now that we've finished
12111 compiling the function. */
db5ae43f 12112
b03e38e1 12113void
11f6b451 12114cxx_pop_function_context (struct function * f)
db5ae43f 12115{
99dccabc 12116 f->language = 0;
db5ae43f 12117}
ebfc180f 12118
e2500fed
GK
12119/* Return which tree structure is used by T, or TS_CP_GENERIC if T is
12120 one of the language-independent trees. */
4519c0a8 12121
e2500fed 12122enum cp_tree_node_structure_enum
11f6b451 12123cp_tree_node_structure (union lang_tree_node * t)
4519c0a8 12124{
e2500fed 12125 switch (TREE_CODE (&t->generic))
4519c0a8 12126 {
a723baf1 12127 case DEFAULT_ARG: return TS_CP_DEFAULT_ARG;
e2500fed 12128 case IDENTIFIER_NODE: return TS_CP_IDENTIFIER;
e2500fed
GK
12129 case OVERLOAD: return TS_CP_OVERLOAD;
12130 case TEMPLATE_PARM_INDEX: return TS_CP_TPI;
12131 case PTRMEM_CST: return TS_CP_PTRMEM;
0cbd7506 12132 case BASELINK: return TS_CP_BASELINK;
54e67af0 12133 case STATIC_ASSERT: return TS_CP_STATIC_ASSERT;
5d80a306 12134 case ARGUMENT_PACK_SELECT: return TS_CP_ARGUMENT_PACK_SELECT;
cb68ec50 12135 case TRAIT_EXPR: return TS_CP_TRAIT_EXPR;
e2500fed 12136 default: return TS_CP_GENERIC;
4519c0a8
MM
12137 }
12138}
5fd8e536 12139
eaa7c03f
JM
12140/* Build the void_list_node (void_type_node having been created). */
12141tree
11f6b451 12142build_void_list_node (void)
eaa7c03f
JM
12143{
12144 tree t = build_tree_list (NULL_TREE, void_type_node);
eaa7c03f
JM
12145 return t;
12146}
12147
6de9cd9a 12148bool
11f6b451 12149cp_missing_noreturn_ok_p (tree decl)
d363e7bf
AJ
12150{
12151 /* A missing noreturn is ok for the `main' function. */
92643fea 12152 return DECL_MAIN_P (decl);
d363e7bf 12153}
e2500fed 12154
c18a5b6c
MM
12155/* Return the COMDAT group into which DECL should be placed. */
12156
12157const char *
12158cxx_comdat_group (tree decl)
12159{
12160 tree name;
12161
12162 /* Virtual tables, construction virtual tables, and virtual table
12163 tables all go in a single COMDAT group, named after the primary
12164 virtual table. */
12165 if (TREE_CODE (decl) == VAR_DECL && DECL_VTABLE_OR_VTT_P (decl))
12166 name = DECL_ASSEMBLER_NAME (CLASSTYPE_VTABLES (DECL_CONTEXT (decl)));
12167 /* For all other DECLs, the COMDAT group is the mangled name of the
12168 declaration itself. */
12169 else
5591e5f9
JJ
12170 {
12171 while (DECL_THUNK_P (decl))
12172 {
12173 /* If TARGET_USE_LOCAL_THUNK_ALIAS_P, use_thunk puts the thunk
12174 into the same section as the target function. In that case
12175 we must return target's name. */
12176 tree target = THUNK_TARGET (decl);
12177 if (TARGET_USE_LOCAL_THUNK_ALIAS_P (target)
12178 && DECL_SECTION_NAME (target) != NULL
12179 && DECL_ONE_ONLY (target))
12180 decl = target;
12181 else
12182 break;
12183 }
12184 name = DECL_ASSEMBLER_NAME (decl);
12185 }
c18a5b6c
MM
12186
12187 return IDENTIFIER_POINTER (name);
12188}
12189
e2500fed 12190#include "gt-cp-decl.h"
This page took 7.865389 seconds and 5 git commands to generate.