]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/cp/decl.c
PR c++/56237
[thirdparty/gcc.git] / gcc / cp / decl.c
1 /* Process declarations and variables for C++ compiler.
2 Copyright (C) 1988-2013 Free Software Foundation, Inc.
3 Contributed by Michael Tiemann (tiemann@cygnus.com)
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
11
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
20
21
22 /* Process declarations and symbol lookup for C++ front end.
23 Also constructs types; the standard scalar types at initialization,
24 and structure, union, array and enum types when they are declared. */
25
26 /* ??? not all decl nodes are given the most useful possible
27 line numbers. For example, the CONST_DECLs for enum values. */
28
29 #include "config.h"
30 #include "system.h"
31 #include "coretypes.h"
32 #include "tm.h"
33 #include "tree.h"
34 #include "flags.h"
35 #include "cp-tree.h"
36 #include "tree-iterator.h"
37 #include "tree-inline.h"
38 #include "decl.h"
39 #include "intl.h"
40 #include "toplev.h"
41 #include "hashtab.h"
42 #include "tm_p.h"
43 #include "target.h"
44 #include "c-family/c-common.h"
45 #include "c-family/c-objc.h"
46 #include "c-family/c-pragma.h"
47 #include "diagnostic.h"
48 #include "intl.h"
49 #include "debug.h"
50 #include "timevar.h"
51 #include "pointer-set.h"
52 #include "splay-tree.h"
53 #include "plugin.h"
54 #include "cgraph.h"
55
56 /* Possible cases of bad specifiers type used by bad_specifiers. */
57 enum bad_spec_place {
58 BSP_VAR, /* variable */
59 BSP_PARM, /* parameter */
60 BSP_TYPE, /* type */
61 BSP_FIELD /* field */
62 };
63
64 static tree grokparms (tree parmlist, tree *);
65 static const char *redeclaration_error_message (tree, tree);
66
67 static int decl_jump_unsafe (tree);
68 static void require_complete_types_for_parms (tree);
69 static int ambi_op_p (enum tree_code);
70 static int unary_op_p (enum tree_code);
71 static void push_local_name (tree);
72 static tree grok_reference_init (tree, tree, tree, int);
73 static tree grokvardecl (tree, tree, const cp_decl_specifier_seq *,
74 int, int, tree);
75 static int check_static_variable_definition (tree, tree);
76 static void record_unknown_type (tree, const char *);
77 static tree builtin_function_1 (tree, tree, bool);
78 static tree build_library_fn_1 (tree, enum tree_code, tree);
79 static int member_function_or_else (tree, tree, enum overload_flags);
80 static void bad_specifiers (tree, enum bad_spec_place, int, int, int, int,
81 int);
82 static void check_for_uninitialized_const_var (tree);
83 static hashval_t typename_hash (const void *);
84 static int typename_compare (const void *, const void *);
85 static tree local_variable_p_walkfn (tree *, int *, void *);
86 static tree record_builtin_java_type (const char *, int);
87 static const char *tag_name (enum tag_types);
88 static tree lookup_and_check_tag (enum tag_types, tree, tag_scope, bool);
89 static int walk_namespaces_r (tree, walk_namespaces_fn, void *);
90 static void maybe_deduce_size_from_array_init (tree, tree);
91 static void layout_var_decl (tree);
92 static tree check_initializer (tree, tree, int, vec<tree, va_gc> **);
93 static void make_rtl_for_nonlocal_decl (tree, tree, const char *);
94 static void save_function_data (tree);
95 static void copy_type_enum (tree , tree);
96 static void check_function_type (tree, tree);
97 static void finish_constructor_body (void);
98 static void begin_destructor_body (void);
99 static void finish_destructor_body (void);
100 static void record_key_method_defined (tree);
101 static tree create_array_type_for_decl (tree, tree, tree);
102 static tree get_atexit_node (void);
103 static tree get_dso_handle_node (void);
104 static tree start_cleanup_fn (void);
105 static void end_cleanup_fn (void);
106 static tree cp_make_fname_decl (location_t, tree, int);
107 static void initialize_predefined_identifiers (void);
108 static tree check_special_function_return_type
109 (special_function_kind, tree, tree);
110 static tree push_cp_library_fn (enum tree_code, tree);
111 static tree build_cp_library_fn (tree, enum tree_code, tree);
112 static void store_parm_decls (tree);
113 static void initialize_local_var (tree, tree);
114 static void expand_static_init (tree, tree);
115
116 /* The following symbols are subsumed in the cp_global_trees array, and
117 listed here individually for documentation purposes.
118
119 C++ extensions
120 tree wchar_decl_node;
121
122 tree vtable_entry_type;
123 tree delta_type_node;
124 tree __t_desc_type_node;
125
126 tree class_type_node;
127 tree unknown_type_node;
128
129 Array type `vtable_entry_type[]'
130
131 tree vtbl_type_node;
132 tree vtbl_ptr_type_node;
133
134 Namespaces,
135
136 tree std_node;
137 tree abi_node;
138
139 A FUNCTION_DECL which can call `abort'. Not necessarily the
140 one that the user will declare, but sufficient to be called
141 by routines that want to abort the program.
142
143 tree abort_fndecl;
144
145 The FUNCTION_DECL for the default `::operator delete'.
146
147 tree global_delete_fndecl;
148
149 Used by RTTI
150 tree type_info_type_node, tinfo_decl_id, tinfo_decl_type;
151 tree tinfo_var_id; */
152
153 tree cp_global_trees[CPTI_MAX];
154
155 /* Indicates that there is a type value in some namespace, although
156 that is not necessarily in scope at the moment. */
157
158 tree global_type_node;
159
160 /* The node that holds the "name" of the global scope. */
161 tree global_scope_name;
162
163 #define local_names cp_function_chain->x_local_names
164
165 /* A list of objects which have constructors or destructors
166 which reside in the global scope. The decl is stored in
167 the TREE_VALUE slot and the initializer is stored
168 in the TREE_PURPOSE slot. */
169 tree static_aggregates;
170
171 /* Like static_aggregates, but for thread_local variables. */
172 tree tls_aggregates;
173
174 /* -- end of C++ */
175
176 /* A node for the integer constant 2. */
177
178 tree integer_two_node;
179
180 /* Used only for jumps to as-yet undefined labels, since jumps to
181 defined labels can have their validity checked immediately. */
182
183 struct GTY((chain_next ("%h.next"))) named_label_use_entry {
184 struct named_label_use_entry *next;
185 /* The binding level to which this entry is *currently* attached.
186 This is initially the binding level in which the goto appeared,
187 but is modified as scopes are closed. */
188 cp_binding_level *binding_level;
189 /* The head of the names list that was current when the goto appeared,
190 or the inner scope popped. These are the decls that will *not* be
191 skipped when jumping to the label. */
192 tree names_in_scope;
193 /* The location of the goto, for error reporting. */
194 location_t o_goto_locus;
195 /* True if an OpenMP structured block scope has been closed since
196 the goto appeared. This means that the branch from the label will
197 illegally exit an OpenMP scope. */
198 bool in_omp_scope;
199 };
200
201 /* A list of all LABEL_DECLs in the function that have names. Here so
202 we can clear out their names' definitions at the end of the
203 function, and so we can check the validity of jumps to these labels. */
204
205 struct GTY(()) named_label_entry {
206 /* The decl itself. */
207 tree label_decl;
208
209 /* The binding level to which the label is *currently* attached.
210 This is initially set to the binding level in which the label
211 is defined, but is modified as scopes are closed. */
212 cp_binding_level *binding_level;
213 /* The head of the names list that was current when the label was
214 defined, or the inner scope popped. These are the decls that will
215 be skipped when jumping to the label. */
216 tree names_in_scope;
217 /* A vector of all decls from all binding levels that would be
218 crossed by a backward branch to the label. */
219 vec<tree, va_gc> *bad_decls;
220
221 /* A list of uses of the label, before the label is defined. */
222 struct named_label_use_entry *uses;
223
224 /* The following bits are set after the label is defined, and are
225 updated as scopes are popped. They indicate that a backward jump
226 to the label will illegally enter a scope of the given flavor. */
227 bool in_try_scope;
228 bool in_catch_scope;
229 bool in_omp_scope;
230 };
231
232 #define named_labels cp_function_chain->x_named_labels
233 \f
234 /* The number of function bodies which we are currently processing.
235 (Zero if we are at namespace scope, one inside the body of a
236 function, two inside the body of a function in a local class, etc.) */
237 int function_depth;
238
239 /* To avoid unwanted recursion, finish_function defers all mark_used calls
240 encountered during its execution until it finishes. */
241 bool defer_mark_used_calls;
242 vec<tree, va_gc> *deferred_mark_used_calls;
243
244 /* States indicating how grokdeclarator() should handle declspecs marked
245 with __attribute__((deprecated)). An object declared as
246 __attribute__((deprecated)) suppresses warnings of uses of other
247 deprecated items. */
248 enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
249
250 \f
251 /* A list of VAR_DECLs whose type was incomplete at the time the
252 variable was declared. */
253
254 typedef struct GTY(()) incomplete_var_d {
255 tree decl;
256 tree incomplete_type;
257 } incomplete_var;
258
259
260 static GTY(()) vec<incomplete_var, va_gc> *incomplete_vars;
261 \f
262 /* Returns the kind of template specialization we are currently
263 processing, given that it's declaration contained N_CLASS_SCOPES
264 explicit scope qualifications. */
265
266 tmpl_spec_kind
267 current_tmpl_spec_kind (int n_class_scopes)
268 {
269 int n_template_parm_scopes = 0;
270 int seen_specialization_p = 0;
271 int innermost_specialization_p = 0;
272 cp_binding_level *b;
273
274 /* Scan through the template parameter scopes. */
275 for (b = current_binding_level;
276 b->kind == sk_template_parms;
277 b = b->level_chain)
278 {
279 /* If we see a specialization scope inside a parameter scope,
280 then something is wrong. That corresponds to a declaration
281 like:
282
283 template <class T> template <> ...
284
285 which is always invalid since [temp.expl.spec] forbids the
286 specialization of a class member template if the enclosing
287 class templates are not explicitly specialized as well. */
288 if (b->explicit_spec_p)
289 {
290 if (n_template_parm_scopes == 0)
291 innermost_specialization_p = 1;
292 else
293 seen_specialization_p = 1;
294 }
295 else if (seen_specialization_p == 1)
296 return tsk_invalid_member_spec;
297
298 ++n_template_parm_scopes;
299 }
300
301 /* Handle explicit instantiations. */
302 if (processing_explicit_instantiation)
303 {
304 if (n_template_parm_scopes != 0)
305 /* We've seen a template parameter list during an explicit
306 instantiation. For example:
307
308 template <class T> template void f(int);
309
310 This is erroneous. */
311 return tsk_invalid_expl_inst;
312 else
313 return tsk_expl_inst;
314 }
315
316 if (n_template_parm_scopes < n_class_scopes)
317 /* We've not seen enough template headers to match all the
318 specialized classes present. For example:
319
320 template <class T> void R<T>::S<T>::f(int);
321
322 This is invalid; there needs to be one set of template
323 parameters for each class. */
324 return tsk_insufficient_parms;
325 else if (n_template_parm_scopes == n_class_scopes)
326 /* We're processing a non-template declaration (even though it may
327 be a member of a template class.) For example:
328
329 template <class T> void S<T>::f(int);
330
331 The `class T' matches the `S<T>', leaving no template headers
332 corresponding to the `f'. */
333 return tsk_none;
334 else if (n_template_parm_scopes > n_class_scopes + 1)
335 /* We've got too many template headers. For example:
336
337 template <> template <class T> void f (T);
338
339 There need to be more enclosing classes. */
340 return tsk_excessive_parms;
341 else
342 /* This must be a template. It's of the form:
343
344 template <class T> template <class U> void S<T>::f(U);
345
346 This is a specialization if the innermost level was a
347 specialization; otherwise it's just a definition of the
348 template. */
349 return innermost_specialization_p ? tsk_expl_spec : tsk_template;
350 }
351
352 /* Exit the current scope. */
353
354 void
355 finish_scope (void)
356 {
357 poplevel (0, 0, 0);
358 }
359
360 /* When a label goes out of scope, check to see if that label was used
361 in a valid manner, and issue any appropriate warnings or errors. */
362
363 static void
364 pop_label (tree label, tree old_value)
365 {
366 if (!processing_template_decl)
367 {
368 if (DECL_INITIAL (label) == NULL_TREE)
369 {
370 location_t location;
371
372 error ("label %q+D used but not defined", label);
373 location = input_location; /* FIXME want (input_filename, (line)0) */
374 /* Avoid crashing later. */
375 define_label (location, DECL_NAME (label));
376 }
377 else
378 warn_for_unused_label (label);
379 }
380
381 SET_IDENTIFIER_LABEL_VALUE (DECL_NAME (label), old_value);
382 }
383
384 /* At the end of a function, all labels declared within the function
385 go out of scope. BLOCK is the top-level block for the
386 function. */
387
388 static int
389 pop_labels_1 (void **slot, void *data)
390 {
391 struct named_label_entry *ent = (struct named_label_entry *) *slot;
392 tree block = (tree) data;
393
394 pop_label (ent->label_decl, NULL_TREE);
395
396 /* Put the labels into the "variables" of the top-level block,
397 so debugger can see them. */
398 DECL_CHAIN (ent->label_decl) = BLOCK_VARS (block);
399 BLOCK_VARS (block) = ent->label_decl;
400
401 htab_clear_slot (named_labels, slot);
402
403 return 1;
404 }
405
406 static void
407 pop_labels (tree block)
408 {
409 if (named_labels)
410 {
411 htab_traverse (named_labels, pop_labels_1, block);
412 named_labels = NULL;
413 }
414 }
415
416 /* At the end of a block with local labels, restore the outer definition. */
417
418 static void
419 pop_local_label (tree label, tree old_value)
420 {
421 struct named_label_entry dummy;
422 void **slot;
423
424 pop_label (label, old_value);
425
426 dummy.label_decl = label;
427 slot = htab_find_slot (named_labels, &dummy, NO_INSERT);
428 htab_clear_slot (named_labels, slot);
429 }
430
431 /* The following two routines are used to interface to Objective-C++.
432 The binding level is purposely treated as an opaque type. */
433
434 void *
435 objc_get_current_scope (void)
436 {
437 return current_binding_level;
438 }
439
440 /* The following routine is used by the NeXT-style SJLJ exceptions;
441 variables get marked 'volatile' so as to not be clobbered by
442 _setjmp()/_longjmp() calls. All variables in the current scope,
443 as well as parent scopes up to (but not including) ENCLOSING_BLK
444 shall be thusly marked. */
445
446 void
447 objc_mark_locals_volatile (void *enclosing_blk)
448 {
449 cp_binding_level *scope;
450
451 for (scope = current_binding_level;
452 scope && scope != enclosing_blk;
453 scope = scope->level_chain)
454 {
455 tree decl;
456
457 for (decl = scope->names; decl; decl = TREE_CHAIN (decl))
458 objc_volatilize_decl (decl);
459
460 /* Do not climb up past the current function. */
461 if (scope->kind == sk_function_parms)
462 break;
463 }
464 }
465
466 /* Update data for defined and undefined labels when leaving a scope. */
467
468 static int
469 poplevel_named_label_1 (void **slot, void *data)
470 {
471 struct named_label_entry *ent = (struct named_label_entry *) *slot;
472 cp_binding_level *bl = (cp_binding_level *) data;
473 cp_binding_level *obl = bl->level_chain;
474
475 if (ent->binding_level == bl)
476 {
477 tree decl;
478
479 /* ENT->NAMES_IN_SCOPE may contain a mixture of DECLs and
480 TREE_LISTs representing OVERLOADs, so be careful. */
481 for (decl = ent->names_in_scope; decl; decl = (DECL_P (decl)
482 ? DECL_CHAIN (decl)
483 : TREE_CHAIN (decl)))
484 if (decl_jump_unsafe (decl))
485 vec_safe_push (ent->bad_decls, decl);
486
487 ent->binding_level = obl;
488 ent->names_in_scope = obl->names;
489 switch (bl->kind)
490 {
491 case sk_try:
492 ent->in_try_scope = true;
493 break;
494 case sk_catch:
495 ent->in_catch_scope = true;
496 break;
497 case sk_omp:
498 ent->in_omp_scope = true;
499 break;
500 default:
501 break;
502 }
503 }
504 else if (ent->uses)
505 {
506 struct named_label_use_entry *use;
507
508 for (use = ent->uses; use ; use = use->next)
509 if (use->binding_level == bl)
510 {
511 use->binding_level = obl;
512 use->names_in_scope = obl->names;
513 if (bl->kind == sk_omp)
514 use->in_omp_scope = true;
515 }
516 }
517
518 return 1;
519 }
520
521 /* Saved errorcount to avoid -Wunused-but-set-{parameter,variable} warnings
522 when errors were reported, except for -Werror-unused-but-set-*. */
523 static int unused_but_set_errorcount;
524
525 /* Exit a binding level.
526 Pop the level off, and restore the state of the identifier-decl mappings
527 that were in effect when this level was entered.
528
529 If KEEP == 1, this level had explicit declarations, so
530 and create a "block" (a BLOCK node) for the level
531 to record its declarations and subblocks for symbol table output.
532
533 If FUNCTIONBODY is nonzero, this level is the body of a function,
534 so create a block as if KEEP were set and also clear out all
535 label names.
536
537 If REVERSE is nonzero, reverse the order of decls before putting
538 them into the BLOCK. */
539
540 tree
541 poplevel (int keep, int reverse, int functionbody)
542 {
543 tree link;
544 /* The chain of decls was accumulated in reverse order.
545 Put it into forward order, just for cleanliness. */
546 tree decls;
547 tree subblocks;
548 tree block;
549 tree decl;
550 int leaving_for_scope;
551 scope_kind kind;
552 unsigned ix;
553 cp_label_binding *label_bind;
554
555 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
556 restart:
557
558 block = NULL_TREE;
559
560 gcc_assert (current_binding_level->kind != sk_class);
561
562 if (current_binding_level->kind == sk_cleanup)
563 functionbody = 0;
564 subblocks = functionbody >= 0 ? current_binding_level->blocks : 0;
565
566 gcc_assert (!vec_safe_length (current_binding_level->class_shadowed));
567
568 /* We used to use KEEP == 2 to indicate that the new block should go
569 at the beginning of the list of blocks at this binding level,
570 rather than the end. This hack is no longer used. */
571 gcc_assert (keep == 0 || keep == 1);
572
573 if (current_binding_level->keep)
574 keep = 1;
575
576 /* Any uses of undefined labels, and any defined labels, now operate
577 under constraints of next binding contour. */
578 if (cfun && !functionbody && named_labels)
579 htab_traverse (named_labels, poplevel_named_label_1,
580 current_binding_level);
581
582 /* Get the decls in the order they were written.
583 Usually current_binding_level->names is in reverse order.
584 But parameter decls were previously put in forward order. */
585
586 if (reverse)
587 current_binding_level->names
588 = decls = nreverse (current_binding_level->names);
589 else
590 decls = current_binding_level->names;
591
592 /* If there were any declarations or structure tags in that level,
593 or if this level is a function body,
594 create a BLOCK to record them for the life of this function. */
595 block = NULL_TREE;
596 if (keep == 1 || functionbody)
597 block = make_node (BLOCK);
598 if (block != NULL_TREE)
599 {
600 BLOCK_VARS (block) = decls;
601 BLOCK_SUBBLOCKS (block) = subblocks;
602 }
603
604 /* In each subblock, record that this is its superior. */
605 if (keep >= 0)
606 for (link = subblocks; link; link = BLOCK_CHAIN (link))
607 BLOCK_SUPERCONTEXT (link) = block;
608
609 /* We still support the old for-scope rules, whereby the variables
610 in a for-init statement were in scope after the for-statement
611 ended. We only use the new rules if flag_new_for_scope is
612 nonzero. */
613 leaving_for_scope
614 = current_binding_level->kind == sk_for && flag_new_for_scope == 1;
615
616 /* Before we remove the declarations first check for unused variables. */
617 if ((warn_unused_variable || warn_unused_but_set_variable)
618 && !processing_template_decl)
619 for (tree d = getdecls (); d; d = TREE_CHAIN (d))
620 {
621 /* There are cases where D itself is a TREE_LIST. See in
622 push_local_binding where the list of decls returned by
623 getdecls is built. */
624 decl = TREE_CODE (d) == TREE_LIST ? TREE_VALUE (d) : d;
625 if (TREE_CODE (decl) == VAR_DECL
626 && (! TREE_USED (decl) || !DECL_READ_P (decl))
627 && ! DECL_IN_SYSTEM_HEADER (decl)
628 && DECL_NAME (decl) && ! DECL_ARTIFICIAL (decl)
629 && TREE_TYPE (decl) != error_mark_node
630 && (!CLASS_TYPE_P (TREE_TYPE (decl))
631 || !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (decl))))
632 {
633 if (! TREE_USED (decl))
634 warning (OPT_Wunused_variable, "unused variable %q+D", decl);
635 else if (DECL_CONTEXT (decl) == current_function_decl
636 && TREE_CODE (TREE_TYPE (decl)) != REFERENCE_TYPE
637 && errorcount == unused_but_set_errorcount)
638 {
639 warning (OPT_Wunused_but_set_variable,
640 "variable %q+D set but not used", decl);
641 unused_but_set_errorcount = errorcount;
642 }
643 }
644 }
645
646 /* Remove declarations for all the DECLs in this level. */
647 for (link = decls; link; link = TREE_CHAIN (link))
648 {
649 if (leaving_for_scope && TREE_CODE (link) == VAR_DECL
650 /* It's hard to make this ARM compatibility hack play nicely with
651 lambdas, and it really isn't necessary in C++11 mode. */
652 && cxx_dialect < cxx0x
653 && DECL_NAME (link))
654 {
655 tree name = DECL_NAME (link);
656 cxx_binding *ob;
657 tree ns_binding;
658
659 ob = outer_binding (name,
660 IDENTIFIER_BINDING (name),
661 /*class_p=*/true);
662 if (!ob)
663 ns_binding = IDENTIFIER_NAMESPACE_VALUE (name);
664 else
665 ns_binding = NULL_TREE;
666
667 if (ob && ob->scope == current_binding_level->level_chain)
668 /* We have something like:
669
670 int i;
671 for (int i; ;);
672
673 and we are leaving the `for' scope. There's no reason to
674 keep the binding of the inner `i' in this case. */
675 pop_binding (name, link);
676 else if ((ob && (TREE_CODE (ob->value) == TYPE_DECL))
677 || (ns_binding && TREE_CODE (ns_binding) == TYPE_DECL))
678 /* Here, we have something like:
679
680 typedef int I;
681
682 void f () {
683 for (int I; ;);
684 }
685
686 We must pop the for-scope binding so we know what's a
687 type and what isn't. */
688 pop_binding (name, link);
689 else
690 {
691 /* Mark this VAR_DECL as dead so that we can tell we left it
692 there only for backward compatibility. */
693 DECL_DEAD_FOR_LOCAL (link) = 1;
694
695 /* Keep track of what should have happened when we
696 popped the binding. */
697 if (ob && ob->value)
698 {
699 SET_DECL_SHADOWED_FOR_VAR (link, ob->value);
700 DECL_HAS_SHADOWED_FOR_VAR_P (link) = 1;
701 }
702
703 /* Add it to the list of dead variables in the next
704 outermost binding to that we can remove these when we
705 leave that binding. */
706 vec_safe_push (
707 current_binding_level->level_chain->dead_vars_from_for,
708 link);
709
710 /* Although we don't pop the cxx_binding, we do clear
711 its SCOPE since the scope is going away now. */
712 IDENTIFIER_BINDING (name)->scope
713 = current_binding_level->level_chain;
714 }
715 }
716 else
717 {
718 tree name;
719
720 /* Remove the binding. */
721 decl = link;
722
723 if (TREE_CODE (decl) == TREE_LIST)
724 decl = TREE_VALUE (decl);
725 name = decl;
726
727 if (TREE_CODE (name) == OVERLOAD)
728 name = OVL_FUNCTION (name);
729
730 gcc_assert (DECL_P (name));
731 pop_binding (DECL_NAME (name), decl);
732 }
733 }
734
735 /* Remove declarations for any `for' variables from inner scopes
736 that we kept around. */
737 FOR_EACH_VEC_SAFE_ELT_REVERSE (current_binding_level->dead_vars_from_for,
738 ix, decl)
739 pop_binding (DECL_NAME (decl), decl);
740
741 /* Restore the IDENTIFIER_TYPE_VALUEs. */
742 for (link = current_binding_level->type_shadowed;
743 link; link = TREE_CHAIN (link))
744 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (link), TREE_VALUE (link));
745
746 /* Restore the IDENTIFIER_LABEL_VALUEs for local labels. */
747 FOR_EACH_VEC_SAFE_ELT_REVERSE (current_binding_level->shadowed_labels,
748 ix, label_bind)
749 pop_local_label (label_bind->label, label_bind->prev_value);
750
751 /* There may be OVERLOADs (wrapped in TREE_LISTs) on the BLOCK_VARs
752 list if a `using' declaration put them there. The debugging
753 back ends won't understand OVERLOAD, so we remove them here.
754 Because the BLOCK_VARS are (temporarily) shared with
755 CURRENT_BINDING_LEVEL->NAMES we must do this fixup after we have
756 popped all the bindings. */
757 if (block)
758 {
759 tree* d;
760
761 for (d = &BLOCK_VARS (block); *d; )
762 {
763 if (TREE_CODE (*d) == TREE_LIST)
764 *d = TREE_CHAIN (*d);
765 else
766 d = &DECL_CHAIN (*d);
767 }
768 }
769
770 /* If the level being exited is the top level of a function,
771 check over all the labels. */
772 if (functionbody)
773 {
774 /* Since this is the top level block of a function, the vars are
775 the function's parameters. Don't leave them in the BLOCK
776 because they are found in the FUNCTION_DECL instead. */
777 BLOCK_VARS (block) = 0;
778 pop_labels (block);
779 }
780
781 kind = current_binding_level->kind;
782 if (kind == sk_cleanup)
783 {
784 tree stmt;
785
786 /* If this is a temporary binding created for a cleanup, then we'll
787 have pushed a statement list level. Pop that, create a new
788 BIND_EXPR for the block, and insert it into the stream. */
789 stmt = pop_stmt_list (current_binding_level->statement_list);
790 stmt = c_build_bind_expr (input_location, block, stmt);
791 add_stmt (stmt);
792 }
793
794 leave_scope ();
795 if (functionbody)
796 {
797 /* The current function is being defined, so its DECL_INITIAL
798 should be error_mark_node. */
799 gcc_assert (DECL_INITIAL (current_function_decl) == error_mark_node);
800 DECL_INITIAL (current_function_decl) = block;
801 }
802 else if (block)
803 current_binding_level->blocks
804 = block_chainon (current_binding_level->blocks, block);
805
806 /* If we did not make a block for the level just exited,
807 any blocks made for inner levels
808 (since they cannot be recorded as subblocks in that level)
809 must be carried forward so they will later become subblocks
810 of something else. */
811 else if (subblocks)
812 current_binding_level->blocks
813 = block_chainon (current_binding_level->blocks, subblocks);
814
815 /* Each and every BLOCK node created here in `poplevel' is important
816 (e.g. for proper debugging information) so if we created one
817 earlier, mark it as "used". */
818 if (block)
819 TREE_USED (block) = 1;
820
821 /* All temporary bindings created for cleanups are popped silently. */
822 if (kind == sk_cleanup)
823 goto restart;
824
825 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
826 return block;
827 }
828
829 /* Walk all the namespaces contained NAMESPACE, including NAMESPACE
830 itself, calling F for each. The DATA is passed to F as well. */
831
832 static int
833 walk_namespaces_r (tree name_space, walk_namespaces_fn f, void* data)
834 {
835 int result = 0;
836 tree current = NAMESPACE_LEVEL (name_space)->namespaces;
837
838 result |= (*f) (name_space, data);
839
840 for (; current; current = DECL_CHAIN (current))
841 result |= walk_namespaces_r (current, f, data);
842
843 return result;
844 }
845
846 /* Walk all the namespaces, calling F for each. The DATA is passed to
847 F as well. */
848
849 int
850 walk_namespaces (walk_namespaces_fn f, void* data)
851 {
852 return walk_namespaces_r (global_namespace, f, data);
853 }
854
855 /* Call wrapup_globals_declarations for the globals in NAMESPACE. If
856 DATA is non-NULL, this is the last time we will call
857 wrapup_global_declarations for this NAMESPACE. */
858
859 int
860 wrapup_globals_for_namespace (tree name_space, void* data)
861 {
862 cp_binding_level *level = NAMESPACE_LEVEL (name_space);
863 vec<tree, va_gc> *statics = level->static_decls;
864 tree *vec = statics->address ();
865 int len = statics->length ();
866 int last_time = (data != 0);
867
868 if (last_time)
869 {
870 check_global_declarations (vec, len);
871 emit_debug_global_declarations (vec, len);
872 return 0;
873 }
874
875 /* Write out any globals that need to be output. */
876 return wrapup_global_declarations (vec, len);
877 }
878
879 \f
880 /* In C++, you don't have to write `struct S' to refer to `S'; you
881 can just use `S'. We accomplish this by creating a TYPE_DECL as
882 if the user had written `typedef struct S S'. Create and return
883 the TYPE_DECL for TYPE. */
884
885 tree
886 create_implicit_typedef (tree name, tree type)
887 {
888 tree decl;
889
890 decl = build_decl (input_location, TYPE_DECL, name, type);
891 DECL_ARTIFICIAL (decl) = 1;
892 /* There are other implicit type declarations, like the one *within*
893 a class that allows you to write `S::S'. We must distinguish
894 amongst these. */
895 SET_DECL_IMPLICIT_TYPEDEF_P (decl);
896 TYPE_NAME (type) = decl;
897 TYPE_STUB_DECL (type) = decl;
898
899 return decl;
900 }
901
902 /* Remember a local name for name-mangling purposes. */
903
904 static void
905 push_local_name (tree decl)
906 {
907 size_t i, nelts;
908 tree t, name;
909
910 timevar_start (TV_NAME_LOOKUP);
911
912 name = DECL_NAME (decl);
913
914 nelts = vec_safe_length (local_names);
915 for (i = 0; i < nelts; i++)
916 {
917 t = (*local_names)[i];
918 if (DECL_NAME (t) == name)
919 {
920 if (!DECL_LANG_SPECIFIC (decl))
921 retrofit_lang_decl (decl);
922 DECL_LANG_SPECIFIC (decl)->u.base.u2sel = 1;
923 if (DECL_DISCRIMINATOR_SET_P (t))
924 DECL_DISCRIMINATOR (decl) = DECL_DISCRIMINATOR (t) + 1;
925 else
926 DECL_DISCRIMINATOR (decl) = 1;
927
928 (*local_names)[i] = decl;
929 timevar_stop (TV_NAME_LOOKUP);
930 return;
931 }
932 }
933
934 vec_safe_push (local_names, decl);
935 timevar_stop (TV_NAME_LOOKUP);
936 }
937 \f
938 /* Subroutine of duplicate_decls: return truthvalue of whether
939 or not types of these decls match.
940
941 For C++, we must compare the parameter list so that `int' can match
942 `int&' in a parameter position, but `int&' is not confused with
943 `const int&'. */
944
945 int
946 decls_match (tree newdecl, tree olddecl)
947 {
948 int types_match;
949
950 if (newdecl == olddecl)
951 return 1;
952
953 if (TREE_CODE (newdecl) != TREE_CODE (olddecl))
954 /* If the two DECLs are not even the same kind of thing, we're not
955 interested in their types. */
956 return 0;
957
958 gcc_assert (DECL_P (newdecl));
959
960 if (TREE_CODE (newdecl) == FUNCTION_DECL)
961 {
962 tree f1 = TREE_TYPE (newdecl);
963 tree f2 = TREE_TYPE (olddecl);
964 tree p1 = TYPE_ARG_TYPES (f1);
965 tree p2 = TYPE_ARG_TYPES (f2);
966 tree r2;
967
968 /* Specializations of different templates are different functions
969 even if they have the same type. */
970 tree t1 = (DECL_USE_TEMPLATE (newdecl)
971 ? DECL_TI_TEMPLATE (newdecl)
972 : NULL_TREE);
973 tree t2 = (DECL_USE_TEMPLATE (olddecl)
974 ? DECL_TI_TEMPLATE (olddecl)
975 : NULL_TREE);
976 if (t1 != t2)
977 return 0;
978
979 /* The decls dont match if they correspond to two different versions
980 of the same function. Disallow extern "C" functions to be
981 versions for now. */
982 if (compparms (p1, p2)
983 && same_type_p (TREE_TYPE (f1), TREE_TYPE (f2))
984 && !DECL_EXTERN_C_P (newdecl)
985 && !DECL_EXTERN_C_P (olddecl)
986 && targetm.target_option.function_versions (newdecl, olddecl))
987 {
988 /* Mark functions as versions if necessary. Modify the mangled decl
989 name if necessary. */
990 if (DECL_FUNCTION_VERSIONED (newdecl)
991 && DECL_FUNCTION_VERSIONED (olddecl))
992 return 0;
993 if (!DECL_FUNCTION_VERSIONED (newdecl))
994 {
995 DECL_FUNCTION_VERSIONED (newdecl) = 1;
996 if (DECL_ASSEMBLER_NAME_SET_P (newdecl))
997 mangle_decl (newdecl);
998 }
999 if (!DECL_FUNCTION_VERSIONED (olddecl))
1000 {
1001 DECL_FUNCTION_VERSIONED (olddecl) = 1;
1002 if (DECL_ASSEMBLER_NAME_SET_P (olddecl))
1003 mangle_decl (olddecl);
1004 }
1005 record_function_versions (olddecl, newdecl);
1006 return 0;
1007 }
1008
1009 if (CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl)
1010 && ! (DECL_EXTERN_C_P (newdecl)
1011 && DECL_EXTERN_C_P (olddecl)))
1012 return 0;
1013
1014 /* A new declaration doesn't match a built-in one unless it
1015 is also extern "C". */
1016 if (DECL_IS_BUILTIN (olddecl)
1017 && DECL_EXTERN_C_P (olddecl) && !DECL_EXTERN_C_P (newdecl))
1018 return 0;
1019
1020 if (TREE_CODE (f1) != TREE_CODE (f2))
1021 return 0;
1022
1023 /* A declaration with deduced return type should use its pre-deduction
1024 type for declaration matching. */
1025 if (FNDECL_USED_AUTO (olddecl))
1026 r2 = DECL_STRUCT_FUNCTION (olddecl)->language->x_auto_return_pattern;
1027 else
1028 r2 = TREE_TYPE (f2);
1029
1030 if (same_type_p (TREE_TYPE (f1), r2))
1031 {
1032 if (!prototype_p (f2) && DECL_EXTERN_C_P (olddecl)
1033 && (DECL_BUILT_IN (olddecl)
1034 #ifndef NO_IMPLICIT_EXTERN_C
1035 || (DECL_IN_SYSTEM_HEADER (newdecl) && !DECL_CLASS_SCOPE_P (newdecl))
1036 || (DECL_IN_SYSTEM_HEADER (olddecl) && !DECL_CLASS_SCOPE_P (olddecl))
1037 #endif
1038 ))
1039 {
1040 types_match = self_promoting_args_p (p1);
1041 if (p1 == void_list_node)
1042 TREE_TYPE (newdecl) = TREE_TYPE (olddecl);
1043 }
1044 #ifndef NO_IMPLICIT_EXTERN_C
1045 else if (!prototype_p (f1)
1046 && (DECL_EXTERN_C_P (olddecl)
1047 && DECL_IN_SYSTEM_HEADER (olddecl)
1048 && !DECL_CLASS_SCOPE_P (olddecl))
1049 && (DECL_EXTERN_C_P (newdecl)
1050 && DECL_IN_SYSTEM_HEADER (newdecl)
1051 && !DECL_CLASS_SCOPE_P (newdecl)))
1052 {
1053 types_match = self_promoting_args_p (p2);
1054 TREE_TYPE (newdecl) = TREE_TYPE (olddecl);
1055 }
1056 #endif
1057 else
1058 types_match =
1059 compparms (p1, p2)
1060 && (TYPE_ATTRIBUTES (TREE_TYPE (newdecl)) == NULL_TREE
1061 || comp_type_attributes (TREE_TYPE (newdecl),
1062 TREE_TYPE (olddecl)) != 0);
1063 }
1064 else
1065 types_match = 0;
1066 }
1067 else if (TREE_CODE (newdecl) == TEMPLATE_DECL)
1068 {
1069 if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl))
1070 != TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)))
1071 return 0;
1072
1073 if (!comp_template_parms (DECL_TEMPLATE_PARMS (newdecl),
1074 DECL_TEMPLATE_PARMS (olddecl)))
1075 return 0;
1076
1077 if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL)
1078 types_match = same_type_p (TREE_TYPE (DECL_TEMPLATE_RESULT (olddecl)),
1079 TREE_TYPE (DECL_TEMPLATE_RESULT (newdecl)));
1080 else
1081 types_match = decls_match (DECL_TEMPLATE_RESULT (olddecl),
1082 DECL_TEMPLATE_RESULT (newdecl));
1083 }
1084 else
1085 {
1086 /* Need to check scope for variable declaration (VAR_DECL).
1087 For typedef (TYPE_DECL), scope is ignored. */
1088 if (TREE_CODE (newdecl) == VAR_DECL
1089 && CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl)
1090 /* [dcl.link]
1091 Two declarations for an object with C language linkage
1092 with the same name (ignoring the namespace that qualify
1093 it) that appear in different namespace scopes refer to
1094 the same object. */
1095 && !(DECL_EXTERN_C_P (olddecl) && DECL_EXTERN_C_P (newdecl)))
1096 return 0;
1097
1098 if (TREE_TYPE (newdecl) == error_mark_node)
1099 types_match = TREE_TYPE (olddecl) == error_mark_node;
1100 else if (TREE_TYPE (olddecl) == NULL_TREE)
1101 types_match = TREE_TYPE (newdecl) == NULL_TREE;
1102 else if (TREE_TYPE (newdecl) == NULL_TREE)
1103 types_match = 0;
1104 else
1105 types_match = comptypes (TREE_TYPE (newdecl),
1106 TREE_TYPE (olddecl),
1107 COMPARE_REDECLARATION);
1108 }
1109
1110 return types_match;
1111 }
1112
1113 /* If NEWDECL is `static' and an `extern' was seen previously,
1114 warn about it. OLDDECL is the previous declaration.
1115
1116 Note that this does not apply to the C++ case of declaring
1117 a variable `extern const' and then later `const'.
1118
1119 Don't complain about built-in functions, since they are beyond
1120 the user's control. */
1121
1122 void
1123 warn_extern_redeclared_static (tree newdecl, tree olddecl)
1124 {
1125 if (TREE_CODE (newdecl) == TYPE_DECL
1126 || TREE_CODE (newdecl) == TEMPLATE_DECL
1127 || TREE_CODE (newdecl) == CONST_DECL
1128 || TREE_CODE (newdecl) == NAMESPACE_DECL)
1129 return;
1130
1131 /* Don't get confused by static member functions; that's a different
1132 use of `static'. */
1133 if (TREE_CODE (newdecl) == FUNCTION_DECL
1134 && DECL_STATIC_FUNCTION_P (newdecl))
1135 return;
1136
1137 /* If the old declaration was `static', or the new one isn't, then
1138 everything is OK. */
1139 if (DECL_THIS_STATIC (olddecl) || !DECL_THIS_STATIC (newdecl))
1140 return;
1141
1142 /* It's OK to declare a builtin function as `static'. */
1143 if (TREE_CODE (olddecl) == FUNCTION_DECL
1144 && DECL_ARTIFICIAL (olddecl))
1145 return;
1146
1147 permerror (input_location, "%qD was declared %<extern%> and later %<static%>", newdecl);
1148 permerror (input_location, "previous declaration of %q+D", olddecl);
1149 }
1150
1151 /* NEW_DECL is a redeclaration of OLD_DECL; both are functions or
1152 function templates. If their exception specifications do not
1153 match, issue a diagnostic. */
1154
1155 static void
1156 check_redeclaration_exception_specification (tree new_decl,
1157 tree old_decl)
1158 {
1159 tree new_type;
1160 tree old_type;
1161 tree new_exceptions;
1162 tree old_exceptions;
1163
1164 new_type = TREE_TYPE (new_decl);
1165 new_exceptions = TYPE_RAISES_EXCEPTIONS (new_type);
1166 old_type = TREE_TYPE (old_decl);
1167 old_exceptions = TYPE_RAISES_EXCEPTIONS (old_type);
1168
1169 /* [except.spec]
1170
1171 If any declaration of a function has an exception-specification,
1172 all declarations, including the definition and an explicit
1173 specialization, of that function shall have an
1174 exception-specification with the same set of type-ids. */
1175 if ((pedantic || ! DECL_IN_SYSTEM_HEADER (old_decl))
1176 && ! DECL_IS_BUILTIN (old_decl)
1177 && flag_exceptions
1178 && !comp_except_specs (new_exceptions, old_exceptions, ce_normal))
1179 {
1180 error ("declaration of %qF has a different exception specifier",
1181 new_decl);
1182 error ("from previous declaration %q+F", old_decl);
1183 }
1184 }
1185
1186 /* Return true if OLD_DECL and NEW_DECL agree on constexprness.
1187 Otherwise issue diagnostics. */
1188
1189 static bool
1190 validate_constexpr_redeclaration (tree old_decl, tree new_decl)
1191 {
1192 old_decl = STRIP_TEMPLATE (old_decl);
1193 new_decl = STRIP_TEMPLATE (new_decl);
1194 if (!VAR_OR_FUNCTION_DECL_P (old_decl)
1195 || !VAR_OR_FUNCTION_DECL_P (new_decl))
1196 return true;
1197 if (DECL_DECLARED_CONSTEXPR_P (old_decl)
1198 == DECL_DECLARED_CONSTEXPR_P (new_decl))
1199 return true;
1200 if (TREE_CODE (old_decl) == FUNCTION_DECL && DECL_BUILT_IN (old_decl))
1201 {
1202 /* Hide a built-in declaration. */
1203 DECL_DECLARED_CONSTEXPR_P (old_decl)
1204 = DECL_DECLARED_CONSTEXPR_P (new_decl);
1205 return true;
1206 }
1207 error ("redeclaration %qD differs in %<constexpr%>", new_decl);
1208 error ("from previous declaration %q+D", old_decl);
1209 return false;
1210 }
1211
1212 #define GNU_INLINE_P(fn) (DECL_DECLARED_INLINE_P (fn) \
1213 && lookup_attribute ("gnu_inline", \
1214 DECL_ATTRIBUTES (fn)))
1215
1216 /* If NEWDECL is a redeclaration of OLDDECL, merge the declarations.
1217 If the redeclaration is invalid, a diagnostic is issued, and the
1218 error_mark_node is returned. Otherwise, OLDDECL is returned.
1219
1220 If NEWDECL is not a redeclaration of OLDDECL, NULL_TREE is
1221 returned.
1222
1223 NEWDECL_IS_FRIEND is true if NEWDECL was declared as a friend. */
1224
1225 tree
1226 duplicate_decls (tree newdecl, tree olddecl, bool newdecl_is_friend)
1227 {
1228 unsigned olddecl_uid = DECL_UID (olddecl);
1229 int olddecl_friend = 0, types_match = 0, hidden_friend = 0;
1230 int new_defines_function = 0;
1231 tree new_template_info;
1232
1233 if (newdecl == olddecl)
1234 return olddecl;
1235
1236 types_match = decls_match (newdecl, olddecl);
1237
1238 /* If either the type of the new decl or the type of the old decl is an
1239 error_mark_node, then that implies that we have already issued an
1240 error (earlier) for some bogus type specification, and in that case,
1241 it is rather pointless to harass the user with yet more error message
1242 about the same declaration, so just pretend the types match here. */
1243 if (TREE_TYPE (newdecl) == error_mark_node
1244 || TREE_TYPE (olddecl) == error_mark_node)
1245 return error_mark_node;
1246
1247 if (UDLIT_OPER_P (DECL_NAME (newdecl))
1248 && UDLIT_OPER_P (DECL_NAME (olddecl)))
1249 {
1250 if (TREE_CODE (newdecl) == TEMPLATE_DECL
1251 && TREE_CODE (olddecl) != TEMPLATE_DECL
1252 && check_raw_literal_operator (olddecl))
1253 error ("literal operator template %q+D conflicts with"
1254 " raw literal operator %qD", newdecl, olddecl);
1255 else if (TREE_CODE (newdecl) != TEMPLATE_DECL
1256 && TREE_CODE (olddecl) == TEMPLATE_DECL
1257 && check_raw_literal_operator (newdecl))
1258 error ("raw literal operator %q+D conflicts with"
1259 " literal operator template %qD", newdecl, olddecl);
1260 }
1261
1262 if (DECL_P (olddecl)
1263 && TREE_CODE (newdecl) == FUNCTION_DECL
1264 && TREE_CODE (olddecl) == FUNCTION_DECL
1265 && (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl)))
1266 {
1267 if (DECL_DECLARED_INLINE_P (newdecl)
1268 && DECL_UNINLINABLE (newdecl)
1269 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
1270 /* Already warned elsewhere. */;
1271 else if (DECL_DECLARED_INLINE_P (olddecl)
1272 && DECL_UNINLINABLE (olddecl)
1273 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
1274 /* Already warned. */;
1275 else if (DECL_DECLARED_INLINE_P (newdecl)
1276 && DECL_UNINLINABLE (olddecl)
1277 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
1278 {
1279 warning (OPT_Wattributes, "function %q+D redeclared as inline",
1280 newdecl);
1281 warning (OPT_Wattributes, "previous declaration of %q+D "
1282 "with attribute noinline", olddecl);
1283 }
1284 else if (DECL_DECLARED_INLINE_P (olddecl)
1285 && DECL_UNINLINABLE (newdecl)
1286 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
1287 {
1288 warning (OPT_Wattributes, "function %q+D redeclared with "
1289 "attribute noinline", newdecl);
1290 warning (OPT_Wattributes, "previous declaration of %q+D was inline",
1291 olddecl);
1292 }
1293 }
1294
1295 /* Check for redeclaration and other discrepancies. */
1296 if (TREE_CODE (olddecl) == FUNCTION_DECL
1297 && DECL_ARTIFICIAL (olddecl))
1298 {
1299 gcc_assert (!DECL_HIDDEN_FRIEND_P (olddecl));
1300 if (TREE_CODE (newdecl) != FUNCTION_DECL)
1301 {
1302 /* Avoid warnings redeclaring built-ins which have not been
1303 explicitly declared. */
1304 if (DECL_ANTICIPATED (olddecl))
1305 return NULL_TREE;
1306
1307 /* If you declare a built-in or predefined function name as static,
1308 the old definition is overridden, but optionally warn this was a
1309 bad choice of name. */
1310 if (! TREE_PUBLIC (newdecl))
1311 {
1312 warning (OPT_Wshadow,
1313 DECL_BUILT_IN (olddecl)
1314 ? G_("shadowing built-in function %q#D")
1315 : G_("shadowing library function %q#D"), olddecl);
1316 /* Discard the old built-in function. */
1317 return NULL_TREE;
1318 }
1319 /* If the built-in is not ansi, then programs can override
1320 it even globally without an error. */
1321 else if (! DECL_BUILT_IN (olddecl))
1322 warning (0, "library function %q#D redeclared as non-function %q#D",
1323 olddecl, newdecl);
1324 else
1325 {
1326 error ("declaration of %q#D", newdecl);
1327 error ("conflicts with built-in declaration %q#D",
1328 olddecl);
1329 }
1330 return NULL_TREE;
1331 }
1332 else if (!types_match)
1333 {
1334 /* Avoid warnings redeclaring built-ins which have not been
1335 explicitly declared. */
1336 if (DECL_ANTICIPATED (olddecl))
1337 {
1338 /* Deal with fileptr_type_node. FILE type is not known
1339 at the time we create the builtins. */
1340 tree t1, t2;
1341
1342 for (t1 = TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
1343 t2 = TYPE_ARG_TYPES (TREE_TYPE (olddecl));
1344 t1 || t2;
1345 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1346 if (!t1 || !t2)
1347 break;
1348 else if (TREE_VALUE (t2) == fileptr_type_node)
1349 {
1350 tree t = TREE_VALUE (t1);
1351
1352 if (TREE_CODE (t) == POINTER_TYPE
1353 && TYPE_NAME (TREE_TYPE (t))
1354 && DECL_NAME (TYPE_NAME (TREE_TYPE (t)))
1355 == get_identifier ("FILE")
1356 && compparms (TREE_CHAIN (t1), TREE_CHAIN (t2)))
1357 {
1358 tree oldargs = TYPE_ARG_TYPES (TREE_TYPE (olddecl));
1359
1360 TYPE_ARG_TYPES (TREE_TYPE (olddecl))
1361 = TYPE_ARG_TYPES (TREE_TYPE (newdecl));
1362 types_match = decls_match (newdecl, olddecl);
1363 if (types_match)
1364 return duplicate_decls (newdecl, olddecl,
1365 newdecl_is_friend);
1366 TYPE_ARG_TYPES (TREE_TYPE (olddecl)) = oldargs;
1367 }
1368 }
1369 else if (! same_type_p (TREE_VALUE (t1), TREE_VALUE (t2)))
1370 break;
1371 }
1372 else if ((DECL_EXTERN_C_P (newdecl)
1373 && DECL_EXTERN_C_P (olddecl))
1374 || compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
1375 TYPE_ARG_TYPES (TREE_TYPE (olddecl))))
1376 {
1377 /* A near match; override the builtin. */
1378
1379 if (TREE_PUBLIC (newdecl))
1380 {
1381 warning (0, "new declaration %q#D", newdecl);
1382 warning (0, "ambiguates built-in declaration %q#D",
1383 olddecl);
1384 }
1385 else
1386 warning (OPT_Wshadow,
1387 DECL_BUILT_IN (olddecl)
1388 ? G_("shadowing built-in function %q#D")
1389 : G_("shadowing library function %q#D"), olddecl);
1390 }
1391 else
1392 /* Discard the old built-in function. */
1393 return NULL_TREE;
1394
1395 /* Replace the old RTL to avoid problems with inlining. */
1396 COPY_DECL_RTL (newdecl, olddecl);
1397 }
1398 /* Even if the types match, prefer the new declarations type for
1399 built-ins which have not been explicitly declared, for
1400 exception lists, etc... */
1401 else if (DECL_IS_BUILTIN (olddecl))
1402 {
1403 tree type = TREE_TYPE (newdecl);
1404 tree attribs = (*targetm.merge_type_attributes)
1405 (TREE_TYPE (olddecl), type);
1406
1407 type = cp_build_type_attribute_variant (type, attribs);
1408 TREE_TYPE (newdecl) = TREE_TYPE (olddecl) = type;
1409 }
1410
1411 /* If a function is explicitly declared "throw ()", propagate that to
1412 the corresponding builtin. */
1413 if (DECL_BUILT_IN_CLASS (olddecl) == BUILT_IN_NORMAL
1414 && DECL_ANTICIPATED (olddecl)
1415 && TREE_NOTHROW (newdecl)
1416 && !TREE_NOTHROW (olddecl))
1417 {
1418 enum built_in_function fncode = DECL_FUNCTION_CODE (olddecl);
1419 tree tmpdecl = builtin_decl_explicit (fncode);
1420 if (tmpdecl && tmpdecl != olddecl && types_match)
1421 TREE_NOTHROW (tmpdecl) = 1;
1422 }
1423
1424 /* Whether or not the builtin can throw exceptions has no
1425 bearing on this declarator. */
1426 TREE_NOTHROW (olddecl) = 0;
1427
1428 if (DECL_THIS_STATIC (newdecl) && !DECL_THIS_STATIC (olddecl))
1429 {
1430 /* If a builtin function is redeclared as `static', merge
1431 the declarations, but make the original one static. */
1432 DECL_THIS_STATIC (olddecl) = 1;
1433 TREE_PUBLIC (olddecl) = 0;
1434
1435 /* Make the old declaration consistent with the new one so
1436 that all remnants of the builtin-ness of this function
1437 will be banished. */
1438 SET_DECL_LANGUAGE (olddecl, DECL_LANGUAGE (newdecl));
1439 COPY_DECL_RTL (newdecl, olddecl);
1440 }
1441 }
1442 else if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
1443 {
1444 /* C++ Standard, 3.3, clause 4:
1445 "[Note: a namespace name or a class template name must be unique
1446 in its declarative region (7.3.2, clause 14). ]" */
1447 if (TREE_CODE (olddecl) != NAMESPACE_DECL
1448 && TREE_CODE (newdecl) != NAMESPACE_DECL
1449 && (TREE_CODE (olddecl) != TEMPLATE_DECL
1450 || TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) != TYPE_DECL)
1451 && (TREE_CODE (newdecl) != TEMPLATE_DECL
1452 || TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) != TYPE_DECL))
1453 {
1454 if ((TREE_CODE (olddecl) == TYPE_DECL && DECL_ARTIFICIAL (olddecl)
1455 && TREE_CODE (newdecl) != TYPE_DECL)
1456 || (TREE_CODE (newdecl) == TYPE_DECL && DECL_ARTIFICIAL (newdecl)
1457 && TREE_CODE (olddecl) != TYPE_DECL))
1458 {
1459 /* We do nothing special here, because C++ does such nasty
1460 things with TYPE_DECLs. Instead, just let the TYPE_DECL
1461 get shadowed, and know that if we need to find a TYPE_DECL
1462 for a given name, we can look in the IDENTIFIER_TYPE_VALUE
1463 slot of the identifier. */
1464 return NULL_TREE;
1465 }
1466
1467 if ((TREE_CODE (newdecl) == FUNCTION_DECL
1468 && DECL_FUNCTION_TEMPLATE_P (olddecl))
1469 || (TREE_CODE (olddecl) == FUNCTION_DECL
1470 && DECL_FUNCTION_TEMPLATE_P (newdecl)))
1471 return NULL_TREE;
1472 }
1473
1474 error ("%q#D redeclared as different kind of symbol", newdecl);
1475 if (TREE_CODE (olddecl) == TREE_LIST)
1476 olddecl = TREE_VALUE (olddecl);
1477 error ("previous declaration of %q+#D", olddecl);
1478
1479 return error_mark_node;
1480 }
1481 else if (!types_match)
1482 {
1483 if (CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl))
1484 /* These are certainly not duplicate declarations; they're
1485 from different scopes. */
1486 return NULL_TREE;
1487
1488 if (TREE_CODE (newdecl) == TEMPLATE_DECL)
1489 {
1490 /* The name of a class template may not be declared to refer to
1491 any other template, class, function, object, namespace, value,
1492 or type in the same scope. */
1493 if (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) == TYPE_DECL
1494 || TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL)
1495 {
1496 error ("declaration of template %q#D", newdecl);
1497 error ("conflicts with previous declaration %q+#D", olddecl);
1498 return error_mark_node;
1499 }
1500 else if (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) == FUNCTION_DECL
1501 && TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == FUNCTION_DECL
1502 && compparms (TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (olddecl))),
1503 TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (newdecl))))
1504 && comp_template_parms (DECL_TEMPLATE_PARMS (newdecl),
1505 DECL_TEMPLATE_PARMS (olddecl))
1506 /* Template functions can be disambiguated by
1507 return type. */
1508 && same_type_p (TREE_TYPE (TREE_TYPE (newdecl)),
1509 TREE_TYPE (TREE_TYPE (olddecl))))
1510 {
1511 error ("new declaration %q#D", newdecl);
1512 error ("ambiguates old declaration %q+#D", olddecl);
1513 }
1514 return NULL_TREE;
1515 }
1516 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1517 {
1518 if (DECL_EXTERN_C_P (newdecl) && DECL_EXTERN_C_P (olddecl))
1519 {
1520 error ("declaration of C function %q#D conflicts with",
1521 newdecl);
1522 error ("previous declaration %q+#D here", olddecl);
1523 return NULL_TREE;
1524 }
1525 /* For function versions, params and types match, but they
1526 are not ambiguous. */
1527 else if ((!DECL_FUNCTION_VERSIONED (newdecl)
1528 && !DECL_FUNCTION_VERSIONED (olddecl))
1529 && compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
1530 TYPE_ARG_TYPES (TREE_TYPE (olddecl))))
1531 {
1532 error ("new declaration %q#D", newdecl);
1533 if (FNDECL_USED_AUTO (olddecl))
1534 error_at (DECL_SOURCE_LOCATION (olddecl), "ambiguates old "
1535 "declaration with deduced return type");
1536 else
1537 error ("ambiguates old declaration %q+#D", olddecl);
1538 return error_mark_node;
1539 }
1540 else
1541 return NULL_TREE;
1542 }
1543 else
1544 {
1545 error ("conflicting declaration %q#D", newdecl);
1546 error ("%q+D has a previous declaration as %q#D", olddecl, olddecl);
1547 return error_mark_node;
1548 }
1549 }
1550 else if (TREE_CODE (newdecl) == FUNCTION_DECL
1551 && ((DECL_TEMPLATE_SPECIALIZATION (olddecl)
1552 && (!DECL_TEMPLATE_INFO (newdecl)
1553 || (DECL_TI_TEMPLATE (newdecl)
1554 != DECL_TI_TEMPLATE (olddecl))))
1555 || (DECL_TEMPLATE_SPECIALIZATION (newdecl)
1556 && (!DECL_TEMPLATE_INFO (olddecl)
1557 || (DECL_TI_TEMPLATE (olddecl)
1558 != DECL_TI_TEMPLATE (newdecl))))))
1559 /* It's OK to have a template specialization and a non-template
1560 with the same type, or to have specializations of two
1561 different templates with the same type. Note that if one is a
1562 specialization, and the other is an instantiation of the same
1563 template, that we do not exit at this point. That situation
1564 can occur if we instantiate a template class, and then
1565 specialize one of its methods. This situation is valid, but
1566 the declarations must be merged in the usual way. */
1567 return NULL_TREE;
1568 else if (TREE_CODE (newdecl) == FUNCTION_DECL
1569 && ((DECL_TEMPLATE_INSTANTIATION (olddecl)
1570 && !DECL_USE_TEMPLATE (newdecl))
1571 || (DECL_TEMPLATE_INSTANTIATION (newdecl)
1572 && !DECL_USE_TEMPLATE (olddecl))))
1573 /* One of the declarations is a template instantiation, and the
1574 other is not a template at all. That's OK. */
1575 return NULL_TREE;
1576 else if (TREE_CODE (newdecl) == NAMESPACE_DECL)
1577 {
1578 /* In [namespace.alias] we have:
1579
1580 In a declarative region, a namespace-alias-definition can be
1581 used to redefine a namespace-alias declared in that declarative
1582 region to refer only to the namespace to which it already
1583 refers.
1584
1585 Therefore, if we encounter a second alias directive for the same
1586 alias, we can just ignore the second directive. */
1587 if (DECL_NAMESPACE_ALIAS (newdecl)
1588 && (DECL_NAMESPACE_ALIAS (newdecl)
1589 == DECL_NAMESPACE_ALIAS (olddecl)))
1590 return olddecl;
1591 /* [namespace.alias]
1592
1593 A namespace-name or namespace-alias shall not be declared as
1594 the name of any other entity in the same declarative region.
1595 A namespace-name defined at global scope shall not be
1596 declared as the name of any other entity in any global scope
1597 of the program. */
1598 error ("declaration of namespace %qD conflicts with", newdecl);
1599 error ("previous declaration of namespace %q+D here", olddecl);
1600 return error_mark_node;
1601 }
1602 else
1603 {
1604 const char *errmsg = redeclaration_error_message (newdecl, olddecl);
1605 if (errmsg)
1606 {
1607 error_at (DECL_SOURCE_LOCATION (newdecl), errmsg, newdecl);
1608 if (DECL_NAME (olddecl) != NULL_TREE)
1609 error ((DECL_INITIAL (olddecl) && namespace_bindings_p ())
1610 ? G_("%q+#D previously defined here")
1611 : G_("%q+#D previously declared here"), olddecl);
1612 return error_mark_node;
1613 }
1614 else if (TREE_CODE (olddecl) == FUNCTION_DECL
1615 && DECL_INITIAL (olddecl) != NULL_TREE
1616 && !prototype_p (TREE_TYPE (olddecl))
1617 && prototype_p (TREE_TYPE (newdecl)))
1618 {
1619 /* Prototype decl follows defn w/o prototype. */
1620 warning_at (input_location, 0, "prototype for %q+#D", newdecl);
1621 warning_at (DECL_SOURCE_LOCATION (olddecl), 0,
1622 "follows non-prototype definition here");
1623 }
1624 else if ((TREE_CODE (olddecl) == FUNCTION_DECL
1625 || TREE_CODE (olddecl) == VAR_DECL)
1626 && DECL_LANGUAGE (newdecl) != DECL_LANGUAGE (olddecl))
1627 {
1628 /* [dcl.link]
1629 If two declarations of the same function or object
1630 specify different linkage-specifications ..., the program
1631 is ill-formed.... Except for functions with C++ linkage,
1632 a function declaration without a linkage specification
1633 shall not precede the first linkage specification for
1634 that function. A function can be declared without a
1635 linkage specification after an explicit linkage
1636 specification has been seen; the linkage explicitly
1637 specified in the earlier declaration is not affected by
1638 such a function declaration.
1639
1640 DR 563 raises the question why the restrictions on
1641 functions should not also apply to objects. Older
1642 versions of G++ silently ignore the linkage-specification
1643 for this example:
1644
1645 namespace N {
1646 extern int i;
1647 extern "C" int i;
1648 }
1649
1650 which is clearly wrong. Therefore, we now treat objects
1651 like functions. */
1652 if (current_lang_depth () == 0)
1653 {
1654 /* There is no explicit linkage-specification, so we use
1655 the linkage from the previous declaration. */
1656 if (!DECL_LANG_SPECIFIC (newdecl))
1657 retrofit_lang_decl (newdecl);
1658 SET_DECL_LANGUAGE (newdecl, DECL_LANGUAGE (olddecl));
1659 }
1660 else
1661 {
1662 error ("previous declaration of %q+#D with %qL linkage",
1663 olddecl, DECL_LANGUAGE (olddecl));
1664 error ("conflicts with new declaration with %qL linkage",
1665 DECL_LANGUAGE (newdecl));
1666 }
1667 }
1668
1669 if (DECL_LANG_SPECIFIC (olddecl) && DECL_USE_TEMPLATE (olddecl))
1670 ;
1671 else if (TREE_CODE (olddecl) == FUNCTION_DECL)
1672 {
1673 tree t1 = TYPE_ARG_TYPES (TREE_TYPE (olddecl));
1674 tree t2 = TYPE_ARG_TYPES (TREE_TYPE (newdecl));
1675 int i = 1;
1676
1677 if (TREE_CODE (TREE_TYPE (newdecl)) == METHOD_TYPE)
1678 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2);
1679
1680 for (; t1 && t1 != void_list_node;
1681 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2), i++)
1682 if (TREE_PURPOSE (t1) && TREE_PURPOSE (t2))
1683 {
1684 if (1 == simple_cst_equal (TREE_PURPOSE (t1),
1685 TREE_PURPOSE (t2)))
1686 {
1687 permerror (input_location, "default argument given for parameter %d of %q#D",
1688 i, newdecl);
1689 permerror (input_location, "after previous specification in %q+#D", olddecl);
1690 }
1691 else
1692 {
1693 error ("default argument given for parameter %d of %q#D",
1694 i, newdecl);
1695 error ("after previous specification in %q+#D",
1696 olddecl);
1697 }
1698 }
1699 }
1700 }
1701
1702 /* Do not merge an implicit typedef with an explicit one. In:
1703
1704 class A;
1705 ...
1706 typedef class A A __attribute__ ((foo));
1707
1708 the attribute should apply only to the typedef. */
1709 if (TREE_CODE (olddecl) == TYPE_DECL
1710 && (DECL_IMPLICIT_TYPEDEF_P (olddecl)
1711 || DECL_IMPLICIT_TYPEDEF_P (newdecl)))
1712 return NULL_TREE;
1713
1714 /* If new decl is `static' and an `extern' was seen previously,
1715 warn about it. */
1716 warn_extern_redeclared_static (newdecl, olddecl);
1717
1718 if (!validate_constexpr_redeclaration (olddecl, newdecl))
1719 return error_mark_node;
1720
1721 /* We have committed to returning 1 at this point. */
1722 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1723 {
1724 /* Now that functions must hold information normally held
1725 by field decls, there is extra work to do so that
1726 declaration information does not get destroyed during
1727 definition. */
1728 if (DECL_VINDEX (olddecl))
1729 DECL_VINDEX (newdecl) = DECL_VINDEX (olddecl);
1730 if (DECL_CONTEXT (olddecl))
1731 DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
1732 DECL_STATIC_CONSTRUCTOR (newdecl) |= DECL_STATIC_CONSTRUCTOR (olddecl);
1733 DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
1734 DECL_PURE_VIRTUAL_P (newdecl) |= DECL_PURE_VIRTUAL_P (olddecl);
1735 DECL_VIRTUAL_P (newdecl) |= DECL_VIRTUAL_P (olddecl);
1736 DECL_INVALID_OVERRIDER_P (newdecl) |= DECL_INVALID_OVERRIDER_P (olddecl);
1737 DECL_THIS_STATIC (newdecl) |= DECL_THIS_STATIC (olddecl);
1738 if (DECL_OVERLOADED_OPERATOR_P (olddecl) != ERROR_MARK)
1739 SET_OVERLOADED_OPERATOR_CODE
1740 (newdecl, DECL_OVERLOADED_OPERATOR_P (olddecl));
1741 new_defines_function = DECL_INITIAL (newdecl) != NULL_TREE;
1742
1743 /* Optionally warn about more than one declaration for the same
1744 name, but don't warn about a function declaration followed by a
1745 definition. */
1746 if (warn_redundant_decls && ! DECL_ARTIFICIAL (olddecl)
1747 && !(new_defines_function && DECL_INITIAL (olddecl) == NULL_TREE)
1748 /* Don't warn about extern decl followed by definition. */
1749 && !(DECL_EXTERNAL (olddecl) && ! DECL_EXTERNAL (newdecl))
1750 /* Don't warn about friends, let add_friend take care of it. */
1751 && ! (newdecl_is_friend || DECL_FRIEND_P (olddecl))
1752 /* Don't warn about declaration followed by specialization. */
1753 && (! DECL_TEMPLATE_SPECIALIZATION (newdecl)
1754 || DECL_TEMPLATE_SPECIALIZATION (olddecl)))
1755 {
1756 warning (OPT_Wredundant_decls, "redundant redeclaration of %qD in same scope", newdecl);
1757 warning (OPT_Wredundant_decls, "previous declaration of %q+D", olddecl);
1758 }
1759
1760 if (DECL_DELETED_FN (newdecl))
1761 {
1762 error ("deleted definition of %qD", newdecl);
1763 error ("after previous declaration %q+D", olddecl);
1764 }
1765 DECL_DELETED_FN (newdecl) |= DECL_DELETED_FN (olddecl);
1766 }
1767
1768 /* Deal with C++: must preserve virtual function table size. */
1769 if (TREE_CODE (olddecl) == TYPE_DECL)
1770 {
1771 tree newtype = TREE_TYPE (newdecl);
1772 tree oldtype = TREE_TYPE (olddecl);
1773
1774 if (newtype != error_mark_node && oldtype != error_mark_node
1775 && TYPE_LANG_SPECIFIC (newtype) && TYPE_LANG_SPECIFIC (oldtype))
1776 CLASSTYPE_FRIEND_CLASSES (newtype)
1777 = CLASSTYPE_FRIEND_CLASSES (oldtype);
1778
1779 DECL_ORIGINAL_TYPE (newdecl) = DECL_ORIGINAL_TYPE (olddecl);
1780 }
1781
1782 /* Copy all the DECL_... slots specified in the new decl
1783 except for any that we copy here from the old type. */
1784 DECL_ATTRIBUTES (newdecl)
1785 = (*targetm.merge_decl_attributes) (olddecl, newdecl);
1786
1787 if (TREE_CODE (newdecl) == TEMPLATE_DECL)
1788 {
1789 tree old_result;
1790 tree new_result;
1791 old_result = DECL_TEMPLATE_RESULT (olddecl);
1792 new_result = DECL_TEMPLATE_RESULT (newdecl);
1793 TREE_TYPE (olddecl) = TREE_TYPE (old_result);
1794 DECL_TEMPLATE_SPECIALIZATIONS (olddecl)
1795 = chainon (DECL_TEMPLATE_SPECIALIZATIONS (olddecl),
1796 DECL_TEMPLATE_SPECIALIZATIONS (newdecl));
1797
1798 DECL_ATTRIBUTES (old_result)
1799 = (*targetm.merge_decl_attributes) (old_result, new_result);
1800
1801 if (DECL_FUNCTION_TEMPLATE_P (newdecl))
1802 {
1803 if (GNU_INLINE_P (old_result) != GNU_INLINE_P (new_result)
1804 && DECL_INITIAL (new_result))
1805 {
1806 if (DECL_INITIAL (old_result))
1807 DECL_UNINLINABLE (old_result) = 1;
1808 else
1809 DECL_UNINLINABLE (old_result) = DECL_UNINLINABLE (new_result);
1810 DECL_EXTERNAL (old_result) = DECL_EXTERNAL (new_result);
1811 DECL_NOT_REALLY_EXTERN (old_result)
1812 = DECL_NOT_REALLY_EXTERN (new_result);
1813 DECL_INTERFACE_KNOWN (old_result)
1814 = DECL_INTERFACE_KNOWN (new_result);
1815 DECL_DECLARED_INLINE_P (old_result)
1816 = DECL_DECLARED_INLINE_P (new_result);
1817 DECL_DISREGARD_INLINE_LIMITS (old_result)
1818 |= DECL_DISREGARD_INLINE_LIMITS (new_result);
1819
1820 }
1821 else
1822 {
1823 DECL_DECLARED_INLINE_P (old_result)
1824 |= DECL_DECLARED_INLINE_P (new_result);
1825 DECL_DISREGARD_INLINE_LIMITS (old_result)
1826 |= DECL_DISREGARD_INLINE_LIMITS (new_result);
1827 check_redeclaration_exception_specification (newdecl, olddecl);
1828 }
1829 }
1830
1831 /* If the new declaration is a definition, update the file and
1832 line information on the declaration, and also make
1833 the old declaration the same definition. */
1834 if (DECL_INITIAL (new_result) != NULL_TREE)
1835 {
1836 DECL_SOURCE_LOCATION (olddecl)
1837 = DECL_SOURCE_LOCATION (old_result)
1838 = DECL_SOURCE_LOCATION (newdecl);
1839 DECL_INITIAL (old_result) = DECL_INITIAL (new_result);
1840 if (DECL_FUNCTION_TEMPLATE_P (newdecl))
1841 {
1842 tree parm;
1843 DECL_ARGUMENTS (old_result)
1844 = DECL_ARGUMENTS (new_result);
1845 for (parm = DECL_ARGUMENTS (old_result); parm;
1846 parm = DECL_CHAIN (parm))
1847 DECL_CONTEXT (parm) = old_result;
1848 }
1849 }
1850
1851 return olddecl;
1852 }
1853
1854 if (types_match)
1855 {
1856 /* Automatically handles default parameters. */
1857 tree oldtype = TREE_TYPE (olddecl);
1858 tree newtype;
1859
1860 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1861 maybe_instantiate_noexcept (olddecl);
1862
1863 /* Merge the data types specified in the two decls. */
1864 newtype = merge_types (TREE_TYPE (newdecl), TREE_TYPE (olddecl));
1865
1866 /* If merge_types produces a non-typedef type, just use the old type. */
1867 if (TREE_CODE (newdecl) == TYPE_DECL
1868 && newtype == DECL_ORIGINAL_TYPE (newdecl))
1869 newtype = oldtype;
1870
1871 if (TREE_CODE (newdecl) == VAR_DECL)
1872 {
1873 DECL_THIS_EXTERN (newdecl) |= DECL_THIS_EXTERN (olddecl);
1874 DECL_INITIALIZED_P (newdecl) |= DECL_INITIALIZED_P (olddecl);
1875 DECL_NONTRIVIALLY_INITIALIZED_P (newdecl)
1876 |= DECL_NONTRIVIALLY_INITIALIZED_P (olddecl);
1877 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (newdecl)
1878 |= DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (olddecl);
1879
1880 /* Merge the threadprivate attribute from OLDDECL into NEWDECL. */
1881 if (DECL_LANG_SPECIFIC (olddecl)
1882 && CP_DECL_THREADPRIVATE_P (olddecl))
1883 {
1884 /* Allocate a LANG_SPECIFIC structure for NEWDECL, if needed. */
1885 if (!DECL_LANG_SPECIFIC (newdecl))
1886 retrofit_lang_decl (newdecl);
1887
1888 DECL_TLS_MODEL (newdecl) = DECL_TLS_MODEL (olddecl);
1889 CP_DECL_THREADPRIVATE_P (newdecl) = 1;
1890 }
1891 }
1892
1893 /* Do this after calling `merge_types' so that default
1894 parameters don't confuse us. */
1895 else if (TREE_CODE (newdecl) == FUNCTION_DECL)
1896 check_redeclaration_exception_specification (newdecl, olddecl);
1897 TREE_TYPE (newdecl) = TREE_TYPE (olddecl) = newtype;
1898
1899 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1900 check_default_args (newdecl);
1901
1902 /* Lay the type out, unless already done. */
1903 if (! same_type_p (newtype, oldtype)
1904 && TREE_TYPE (newdecl) != error_mark_node
1905 && !(processing_template_decl && uses_template_parms (newdecl)))
1906 layout_type (TREE_TYPE (newdecl));
1907
1908 if ((TREE_CODE (newdecl) == VAR_DECL
1909 || TREE_CODE (newdecl) == PARM_DECL
1910 || TREE_CODE (newdecl) == RESULT_DECL
1911 || TREE_CODE (newdecl) == FIELD_DECL
1912 || TREE_CODE (newdecl) == TYPE_DECL)
1913 && !(processing_template_decl && uses_template_parms (newdecl)))
1914 layout_decl (newdecl, 0);
1915
1916 /* Merge the type qualifiers. */
1917 if (TREE_READONLY (newdecl))
1918 TREE_READONLY (olddecl) = 1;
1919 if (TREE_THIS_VOLATILE (newdecl))
1920 TREE_THIS_VOLATILE (olddecl) = 1;
1921 if (TREE_NOTHROW (newdecl))
1922 TREE_NOTHROW (olddecl) = 1;
1923
1924 /* Merge deprecatedness. */
1925 if (TREE_DEPRECATED (newdecl))
1926 TREE_DEPRECATED (olddecl) = 1;
1927
1928 /* Preserve function specific target and optimization options */
1929 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1930 {
1931 if (DECL_FUNCTION_SPECIFIC_TARGET (olddecl)
1932 && !DECL_FUNCTION_SPECIFIC_TARGET (newdecl))
1933 DECL_FUNCTION_SPECIFIC_TARGET (newdecl)
1934 = DECL_FUNCTION_SPECIFIC_TARGET (olddecl);
1935
1936 if (DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl)
1937 && !DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl))
1938 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl)
1939 = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl);
1940 }
1941
1942 /* Merge the initialization information. */
1943 if (DECL_INITIAL (newdecl) == NULL_TREE
1944 && DECL_INITIAL (olddecl) != NULL_TREE)
1945 {
1946 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
1947 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
1948 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1949 {
1950 DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
1951 DECL_STRUCT_FUNCTION (newdecl) = DECL_STRUCT_FUNCTION (olddecl);
1952 }
1953 }
1954
1955 /* Merge the section attribute.
1956 We want to issue an error if the sections conflict but that must be
1957 done later in decl_attributes since we are called before attributes
1958 are assigned. */
1959 if (DECL_SECTION_NAME (newdecl) == NULL_TREE)
1960 DECL_SECTION_NAME (newdecl) = DECL_SECTION_NAME (olddecl);
1961
1962 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1963 {
1964 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
1965 |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
1966 DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
1967 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1968 TREE_NOTHROW (newdecl) |= TREE_NOTHROW (olddecl);
1969 DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
1970 DECL_IS_OPERATOR_NEW (newdecl) |= DECL_IS_OPERATOR_NEW (olddecl);
1971 DECL_PURE_P (newdecl) |= DECL_PURE_P (olddecl);
1972 TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
1973 DECL_LOOPING_CONST_OR_PURE_P (newdecl)
1974 |= DECL_LOOPING_CONST_OR_PURE_P (olddecl);
1975 /* Keep the old RTL. */
1976 COPY_DECL_RTL (olddecl, newdecl);
1977 }
1978 else if (TREE_CODE (newdecl) == VAR_DECL
1979 && (DECL_SIZE (olddecl) || !DECL_SIZE (newdecl)))
1980 {
1981 /* Keep the old RTL. We cannot keep the old RTL if the old
1982 declaration was for an incomplete object and the new
1983 declaration is not since many attributes of the RTL will
1984 change. */
1985 COPY_DECL_RTL (olddecl, newdecl);
1986 }
1987 }
1988 /* If cannot merge, then use the new type and qualifiers,
1989 and don't preserve the old rtl. */
1990 else
1991 {
1992 /* Clean out any memory we had of the old declaration. */
1993 tree oldstatic = value_member (olddecl, static_aggregates);
1994 if (oldstatic)
1995 TREE_VALUE (oldstatic) = error_mark_node;
1996
1997 TREE_TYPE (olddecl) = TREE_TYPE (newdecl);
1998 TREE_READONLY (olddecl) = TREE_READONLY (newdecl);
1999 TREE_THIS_VOLATILE (olddecl) = TREE_THIS_VOLATILE (newdecl);
2000 TREE_SIDE_EFFECTS (olddecl) = TREE_SIDE_EFFECTS (newdecl);
2001 }
2002
2003 /* Merge the storage class information. */
2004 merge_weak (newdecl, olddecl);
2005
2006 if (DECL_ONE_ONLY (olddecl))
2007 DECL_COMDAT_GROUP (newdecl) = DECL_COMDAT_GROUP (olddecl);
2008
2009 DECL_DEFER_OUTPUT (newdecl) |= DECL_DEFER_OUTPUT (olddecl);
2010 TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
2011 TREE_STATIC (olddecl) = TREE_STATIC (newdecl) |= TREE_STATIC (olddecl);
2012 if (! DECL_EXTERNAL (olddecl))
2013 DECL_EXTERNAL (newdecl) = 0;
2014
2015 new_template_info = NULL_TREE;
2016 if (DECL_LANG_SPECIFIC (newdecl) && DECL_LANG_SPECIFIC (olddecl))
2017 {
2018 bool new_redefines_gnu_inline = false;
2019
2020 if (new_defines_function
2021 && ((DECL_INTERFACE_KNOWN (olddecl)
2022 && TREE_CODE (olddecl) == FUNCTION_DECL)
2023 || (TREE_CODE (olddecl) == TEMPLATE_DECL
2024 && (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl))
2025 == FUNCTION_DECL))))
2026 {
2027 tree fn = olddecl;
2028
2029 if (TREE_CODE (fn) == TEMPLATE_DECL)
2030 fn = DECL_TEMPLATE_RESULT (olddecl);
2031
2032 new_redefines_gnu_inline = GNU_INLINE_P (fn) && DECL_INITIAL (fn);
2033 }
2034
2035 if (!new_redefines_gnu_inline)
2036 {
2037 DECL_INTERFACE_KNOWN (newdecl) |= DECL_INTERFACE_KNOWN (olddecl);
2038 DECL_NOT_REALLY_EXTERN (newdecl) |= DECL_NOT_REALLY_EXTERN (olddecl);
2039 DECL_COMDAT (newdecl) |= DECL_COMDAT (olddecl);
2040 }
2041 DECL_TEMPLATE_INSTANTIATED (newdecl)
2042 |= DECL_TEMPLATE_INSTANTIATED (olddecl);
2043 DECL_ODR_USED (newdecl) |= DECL_ODR_USED (olddecl);
2044
2045 /* If the OLDDECL is an instantiation and/or specialization,
2046 then the NEWDECL must be too. But, it may not yet be marked
2047 as such if the caller has created NEWDECL, but has not yet
2048 figured out that it is a redeclaration. */
2049 if (!DECL_USE_TEMPLATE (newdecl))
2050 DECL_USE_TEMPLATE (newdecl) = DECL_USE_TEMPLATE (olddecl);
2051
2052 /* Don't really know how much of the language-specific
2053 values we should copy from old to new. */
2054 DECL_IN_AGGR_P (newdecl) = DECL_IN_AGGR_P (olddecl);
2055 DECL_REPO_AVAILABLE_P (newdecl) = DECL_REPO_AVAILABLE_P (olddecl);
2056 DECL_INITIALIZED_IN_CLASS_P (newdecl)
2057 |= DECL_INITIALIZED_IN_CLASS_P (olddecl);
2058
2059 if (LANG_DECL_HAS_MIN (newdecl))
2060 {
2061 DECL_LANG_SPECIFIC (newdecl)->u.min.u2 =
2062 DECL_LANG_SPECIFIC (olddecl)->u.min.u2;
2063 if (DECL_TEMPLATE_INFO (newdecl))
2064 new_template_info = DECL_TEMPLATE_INFO (newdecl);
2065 DECL_TEMPLATE_INFO (newdecl) = DECL_TEMPLATE_INFO (olddecl);
2066 }
2067 /* Only functions have these fields. */
2068 if (TREE_CODE (newdecl) == FUNCTION_DECL
2069 || DECL_FUNCTION_TEMPLATE_P (newdecl))
2070 {
2071 DECL_NONCONVERTING_P (newdecl) = DECL_NONCONVERTING_P (olddecl);
2072 olddecl_friend = DECL_FRIEND_P (olddecl);
2073 hidden_friend = (DECL_ANTICIPATED (olddecl)
2074 && DECL_HIDDEN_FRIEND_P (olddecl)
2075 && newdecl_is_friend);
2076 DECL_BEFRIENDING_CLASSES (newdecl)
2077 = chainon (DECL_BEFRIENDING_CLASSES (newdecl),
2078 DECL_BEFRIENDING_CLASSES (olddecl));
2079 /* DECL_THUNKS is only valid for virtual functions,
2080 otherwise it is a DECL_FRIEND_CONTEXT. */
2081 if (DECL_VIRTUAL_P (newdecl))
2082 SET_DECL_THUNKS (newdecl, DECL_THUNKS (olddecl));
2083 }
2084 /* Only variables have this field. */
2085 else if (TREE_CODE (newdecl) == VAR_DECL
2086 && VAR_HAD_UNKNOWN_BOUND (olddecl))
2087 SET_VAR_HAD_UNKNOWN_BOUND (newdecl);
2088 }
2089
2090 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2091 {
2092 tree parm;
2093
2094 /* Merge parameter attributes. */
2095 tree oldarg, newarg;
2096 for (oldarg = DECL_ARGUMENTS(olddecl),
2097 newarg = DECL_ARGUMENTS(newdecl);
2098 oldarg && newarg;
2099 oldarg = DECL_CHAIN(oldarg), newarg = DECL_CHAIN(newarg)) {
2100 DECL_ATTRIBUTES (newarg)
2101 = (*targetm.merge_decl_attributes) (oldarg, newarg);
2102 DECL_ATTRIBUTES (oldarg) = DECL_ATTRIBUTES (newarg);
2103 }
2104
2105 if (DECL_TEMPLATE_INSTANTIATION (olddecl)
2106 && !DECL_TEMPLATE_INSTANTIATION (newdecl))
2107 {
2108 /* If newdecl is not a specialization, then it is not a
2109 template-related function at all. And that means that we
2110 should have exited above, returning 0. */
2111 gcc_assert (DECL_TEMPLATE_SPECIALIZATION (newdecl));
2112
2113 if (DECL_ODR_USED (olddecl))
2114 /* From [temp.expl.spec]:
2115
2116 If a template, a member template or the member of a class
2117 template is explicitly specialized then that
2118 specialization shall be declared before the first use of
2119 that specialization that would cause an implicit
2120 instantiation to take place, in every translation unit in
2121 which such a use occurs. */
2122 error ("explicit specialization of %qD after first use",
2123 olddecl);
2124
2125 SET_DECL_TEMPLATE_SPECIALIZATION (olddecl);
2126
2127 /* Don't propagate visibility from the template to the
2128 specialization here. We'll do that in determine_visibility if
2129 appropriate. */
2130 DECL_VISIBILITY_SPECIFIED (olddecl) = 0;
2131
2132 /* [temp.expl.spec/14] We don't inline explicit specialization
2133 just because the primary template says so. */
2134
2135 /* But still keep DECL_DISREGARD_INLINE_LIMITS in sync with
2136 the always_inline attribute. */
2137 if (DECL_DISREGARD_INLINE_LIMITS (olddecl)
2138 && !DECL_DISREGARD_INLINE_LIMITS (newdecl))
2139 {
2140 if (DECL_DECLARED_INLINE_P (newdecl))
2141 DECL_DISREGARD_INLINE_LIMITS (newdecl) = true;
2142 else
2143 DECL_ATTRIBUTES (newdecl)
2144 = remove_attribute ("always_inline",
2145 DECL_ATTRIBUTES (newdecl));
2146 }
2147 }
2148 else if (new_defines_function && DECL_INITIAL (olddecl))
2149 {
2150 /* Never inline re-defined extern inline functions.
2151 FIXME: this could be better handled by keeping both
2152 function as separate declarations. */
2153 DECL_UNINLINABLE (newdecl) = 1;
2154 }
2155 else
2156 {
2157 if (DECL_PENDING_INLINE_INFO (newdecl) == 0)
2158 DECL_PENDING_INLINE_INFO (newdecl) = DECL_PENDING_INLINE_INFO (olddecl);
2159
2160 DECL_DECLARED_INLINE_P (newdecl) |= DECL_DECLARED_INLINE_P (olddecl);
2161
2162 DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
2163 = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
2164
2165 DECL_DISREGARD_INLINE_LIMITS (newdecl)
2166 = DECL_DISREGARD_INLINE_LIMITS (olddecl)
2167 = (DECL_DISREGARD_INLINE_LIMITS (newdecl)
2168 || DECL_DISREGARD_INLINE_LIMITS (olddecl));
2169 }
2170
2171 /* Preserve abstractness on cloned [cd]tors. */
2172 DECL_ABSTRACT (newdecl) = DECL_ABSTRACT (olddecl);
2173
2174 /* Update newdecl's parms to point at olddecl. */
2175 for (parm = DECL_ARGUMENTS (newdecl); parm;
2176 parm = DECL_CHAIN (parm))
2177 DECL_CONTEXT (parm) = olddecl;
2178
2179 if (! types_match)
2180 {
2181 SET_DECL_LANGUAGE (olddecl, DECL_LANGUAGE (newdecl));
2182 COPY_DECL_ASSEMBLER_NAME (newdecl, olddecl);
2183 COPY_DECL_RTL (newdecl, olddecl);
2184 }
2185 if (! types_match || new_defines_function)
2186 {
2187 /* These need to be copied so that the names are available.
2188 Note that if the types do match, we'll preserve inline
2189 info and other bits, but if not, we won't. */
2190 DECL_ARGUMENTS (olddecl) = DECL_ARGUMENTS (newdecl);
2191 DECL_RESULT (olddecl) = DECL_RESULT (newdecl);
2192 }
2193 /* If redeclaring a builtin function, it stays built in
2194 if newdecl is a gnu_inline definition, or if newdecl is just
2195 a declaration. */
2196 if (DECL_BUILT_IN (olddecl)
2197 && (new_defines_function ? GNU_INLINE_P (newdecl) : types_match))
2198 {
2199 DECL_BUILT_IN_CLASS (newdecl) = DECL_BUILT_IN_CLASS (olddecl);
2200 DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl);
2201 /* If we're keeping the built-in definition, keep the rtl,
2202 regardless of declaration matches. */
2203 COPY_DECL_RTL (olddecl, newdecl);
2204 if (DECL_BUILT_IN_CLASS (newdecl) == BUILT_IN_NORMAL)
2205 {
2206 enum built_in_function fncode = DECL_FUNCTION_CODE (newdecl);
2207 switch (fncode)
2208 {
2209 /* If a compatible prototype of these builtin functions
2210 is seen, assume the runtime implements it with the
2211 expected semantics. */
2212 case BUILT_IN_STPCPY:
2213 if (builtin_decl_explicit_p (fncode))
2214 set_builtin_decl_implicit_p (fncode, true);
2215 break;
2216 default:
2217 break;
2218 }
2219 }
2220 }
2221 if (new_defines_function)
2222 /* If defining a function declared with other language
2223 linkage, use the previously declared language linkage. */
2224 SET_DECL_LANGUAGE (newdecl, DECL_LANGUAGE (olddecl));
2225 else if (types_match)
2226 {
2227 DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
2228 /* Don't clear out the arguments if we're just redeclaring a
2229 function. */
2230 if (DECL_ARGUMENTS (olddecl))
2231 DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl);
2232 }
2233 }
2234 else if (TREE_CODE (newdecl) == NAMESPACE_DECL)
2235 NAMESPACE_LEVEL (newdecl) = NAMESPACE_LEVEL (olddecl);
2236
2237 /* Now preserve various other info from the definition. */
2238 TREE_ADDRESSABLE (newdecl) = TREE_ADDRESSABLE (olddecl);
2239 TREE_ASM_WRITTEN (newdecl) = TREE_ASM_WRITTEN (olddecl);
2240 DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
2241 COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
2242
2243 /* Warn about conflicting visibility specifications. */
2244 if (DECL_VISIBILITY_SPECIFIED (olddecl)
2245 && DECL_VISIBILITY_SPECIFIED (newdecl)
2246 && DECL_VISIBILITY (newdecl) != DECL_VISIBILITY (olddecl))
2247 {
2248 warning_at (input_location, OPT_Wattributes,
2249 "%q+D: visibility attribute ignored because it", newdecl);
2250 warning_at (DECL_SOURCE_LOCATION (olddecl), OPT_Wattributes,
2251 "conflicts with previous declaration here");
2252 }
2253 /* Choose the declaration which specified visibility. */
2254 if (DECL_VISIBILITY_SPECIFIED (olddecl))
2255 {
2256 DECL_VISIBILITY (newdecl) = DECL_VISIBILITY (olddecl);
2257 DECL_VISIBILITY_SPECIFIED (newdecl) = 1;
2258 }
2259 /* Init priority used to be merged from newdecl to olddecl by the memcpy,
2260 so keep this behavior. */
2261 if (TREE_CODE (newdecl) == VAR_DECL && DECL_HAS_INIT_PRIORITY_P (newdecl))
2262 {
2263 SET_DECL_INIT_PRIORITY (olddecl, DECL_INIT_PRIORITY (newdecl));
2264 DECL_HAS_INIT_PRIORITY_P (olddecl) = 1;
2265 }
2266 /* Likewise for DECL_ALIGN, DECL_USER_ALIGN and DECL_PACKED. */
2267 if (DECL_ALIGN (olddecl) > DECL_ALIGN (newdecl))
2268 {
2269 DECL_ALIGN (newdecl) = DECL_ALIGN (olddecl);
2270 DECL_USER_ALIGN (newdecl) |= DECL_USER_ALIGN (olddecl);
2271 }
2272 DECL_USER_ALIGN (olddecl) = DECL_USER_ALIGN (newdecl);
2273 if (TREE_CODE (newdecl) == FIELD_DECL)
2274 DECL_PACKED (olddecl) = DECL_PACKED (newdecl);
2275
2276 /* The DECL_LANG_SPECIFIC information in OLDDECL will be replaced
2277 with that from NEWDECL below. */
2278 if (DECL_LANG_SPECIFIC (olddecl))
2279 {
2280 gcc_assert (DECL_LANG_SPECIFIC (olddecl)
2281 != DECL_LANG_SPECIFIC (newdecl));
2282 ggc_free (DECL_LANG_SPECIFIC (olddecl));
2283 }
2284
2285 /* Merge the USED information. */
2286 if (TREE_USED (olddecl))
2287 TREE_USED (newdecl) = 1;
2288 else if (TREE_USED (newdecl))
2289 TREE_USED (olddecl) = 1;
2290 if (TREE_CODE (newdecl) == VAR_DECL)
2291 {
2292 if (DECL_READ_P (olddecl))
2293 DECL_READ_P (newdecl) = 1;
2294 else if (DECL_READ_P (newdecl))
2295 DECL_READ_P (olddecl) = 1;
2296 }
2297 if (DECL_PRESERVE_P (olddecl))
2298 DECL_PRESERVE_P (newdecl) = 1;
2299 else if (DECL_PRESERVE_P (newdecl))
2300 DECL_PRESERVE_P (olddecl) = 1;
2301
2302 /* Merge the DECL_FUNCTION_VERSIONED information. newdecl will be copied
2303 to olddecl and deleted. */
2304 if (TREE_CODE (newdecl) == FUNCTION_DECL
2305 && DECL_FUNCTION_VERSIONED (olddecl))
2306 {
2307 /* Set the flag for newdecl so that it gets copied to olddecl. */
2308 DECL_FUNCTION_VERSIONED (newdecl) = 1;
2309 /* newdecl will be purged after copying to olddecl and is no longer
2310 a version. */
2311 delete_function_version (newdecl);
2312 }
2313
2314 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2315 {
2316 int function_size;
2317
2318 function_size = sizeof (struct tree_decl_common);
2319
2320 memcpy ((char *) olddecl + sizeof (struct tree_common),
2321 (char *) newdecl + sizeof (struct tree_common),
2322 function_size - sizeof (struct tree_common));
2323
2324 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2325 (char *) newdecl + sizeof (struct tree_decl_common),
2326 sizeof (struct tree_function_decl) - sizeof (struct tree_decl_common));
2327 if (new_template_info)
2328 /* If newdecl is a template instantiation, it is possible that
2329 the following sequence of events has occurred:
2330
2331 o A friend function was declared in a class template. The
2332 class template was instantiated.
2333
2334 o The instantiation of the friend declaration was
2335 recorded on the instantiation list, and is newdecl.
2336
2337 o Later, however, instantiate_class_template called pushdecl
2338 on the newdecl to perform name injection. But, pushdecl in
2339 turn called duplicate_decls when it discovered that another
2340 declaration of a global function with the same name already
2341 existed.
2342
2343 o Here, in duplicate_decls, we decided to clobber newdecl.
2344
2345 If we're going to do that, we'd better make sure that
2346 olddecl, and not newdecl, is on the list of
2347 instantiations so that if we try to do the instantiation
2348 again we won't get the clobbered declaration. */
2349 reregister_specialization (newdecl,
2350 new_template_info,
2351 olddecl);
2352 }
2353 else
2354 {
2355 size_t size = tree_code_size (TREE_CODE (olddecl));
2356 memcpy ((char *) olddecl + sizeof (struct tree_common),
2357 (char *) newdecl + sizeof (struct tree_common),
2358 sizeof (struct tree_decl_common) - sizeof (struct tree_common));
2359 switch (TREE_CODE (olddecl))
2360 {
2361 case LABEL_DECL:
2362 case VAR_DECL:
2363 case RESULT_DECL:
2364 case PARM_DECL:
2365 case FIELD_DECL:
2366 case TYPE_DECL:
2367 case CONST_DECL:
2368 {
2369 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2370 (char *) newdecl + sizeof (struct tree_decl_common),
2371 size - sizeof (struct tree_decl_common)
2372 + TREE_CODE_LENGTH (TREE_CODE (newdecl)) * sizeof (char *));
2373 }
2374 break;
2375 default:
2376 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2377 (char *) newdecl + sizeof (struct tree_decl_common),
2378 sizeof (struct tree_decl_non_common) - sizeof (struct tree_decl_common)
2379 + TREE_CODE_LENGTH (TREE_CODE (newdecl)) * sizeof (char *));
2380 break;
2381 }
2382 }
2383 DECL_UID (olddecl) = olddecl_uid;
2384 if (olddecl_friend)
2385 DECL_FRIEND_P (olddecl) = 1;
2386 if (hidden_friend)
2387 {
2388 DECL_ANTICIPATED (olddecl) = 1;
2389 DECL_HIDDEN_FRIEND_P (olddecl) = 1;
2390 }
2391
2392 /* NEWDECL contains the merged attribute lists.
2393 Update OLDDECL to be the same. */
2394 DECL_ATTRIBUTES (olddecl) = DECL_ATTRIBUTES (newdecl);
2395
2396 /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
2397 so that encode_section_info has a chance to look at the new decl
2398 flags and attributes. */
2399 if (DECL_RTL_SET_P (olddecl)
2400 && (TREE_CODE (olddecl) == FUNCTION_DECL
2401 || (TREE_CODE (olddecl) == VAR_DECL
2402 && TREE_STATIC (olddecl))))
2403 make_decl_rtl (olddecl);
2404
2405 /* The NEWDECL will no longer be needed. Because every out-of-class
2406 declaration of a member results in a call to duplicate_decls,
2407 freeing these nodes represents in a significant savings. */
2408 ggc_free (newdecl);
2409
2410 return olddecl;
2411 }
2412 \f
2413 /* Return zero if the declaration NEWDECL is valid
2414 when the declaration OLDDECL (assumed to be for the same name)
2415 has already been seen.
2416 Otherwise return an error message format string with a %s
2417 where the identifier should go. */
2418
2419 static const char *
2420 redeclaration_error_message (tree newdecl, tree olddecl)
2421 {
2422 if (TREE_CODE (newdecl) == TYPE_DECL)
2423 {
2424 /* Because C++ can put things into name space for free,
2425 constructs like "typedef struct foo { ... } foo"
2426 would look like an erroneous redeclaration. */
2427 if (same_type_p (TREE_TYPE (newdecl), TREE_TYPE (olddecl)))
2428 return NULL;
2429 else
2430 return G_("redefinition of %q#D");
2431 }
2432 else if (TREE_CODE (newdecl) == FUNCTION_DECL)
2433 {
2434 /* If this is a pure function, its olddecl will actually be
2435 the original initialization to `0' (which we force to call
2436 abort()). Don't complain about redefinition in this case. */
2437 if (DECL_LANG_SPECIFIC (olddecl) && DECL_PURE_VIRTUAL_P (olddecl)
2438 && DECL_INITIAL (olddecl) == NULL_TREE)
2439 return NULL;
2440
2441 /* If both functions come from different namespaces, this is not
2442 a redeclaration - this is a conflict with a used function. */
2443 if (DECL_NAMESPACE_SCOPE_P (olddecl)
2444 && DECL_CONTEXT (olddecl) != DECL_CONTEXT (newdecl)
2445 && ! decls_match (olddecl, newdecl))
2446 return G_("%qD conflicts with used function");
2447
2448 /* We'll complain about linkage mismatches in
2449 warn_extern_redeclared_static. */
2450
2451 /* Defining the same name twice is no good. */
2452 if (DECL_INITIAL (olddecl) != NULL_TREE
2453 && DECL_INITIAL (newdecl) != NULL_TREE)
2454 {
2455 if (DECL_NAME (olddecl) == NULL_TREE)
2456 return G_("%q#D not declared in class");
2457 else if (!GNU_INLINE_P (olddecl)
2458 || GNU_INLINE_P (newdecl))
2459 return G_("redefinition of %q#D");
2460 }
2461
2462 if (DECL_DECLARED_INLINE_P (olddecl) && DECL_DECLARED_INLINE_P (newdecl))
2463 {
2464 bool olda = GNU_INLINE_P (olddecl);
2465 bool newa = GNU_INLINE_P (newdecl);
2466
2467 if (olda != newa)
2468 {
2469 if (newa)
2470 return G_("%q+D redeclared inline with "
2471 "%<gnu_inline%> attribute");
2472 else
2473 return G_("%q+D redeclared inline without "
2474 "%<gnu_inline%> attribute");
2475 }
2476 }
2477
2478 check_abi_tag_redeclaration
2479 (olddecl, lookup_attribute ("abi_tag", DECL_ATTRIBUTES (olddecl)),
2480 lookup_attribute ("abi_tag", DECL_ATTRIBUTES (newdecl)));
2481
2482 return NULL;
2483 }
2484 else if (TREE_CODE (newdecl) == TEMPLATE_DECL)
2485 {
2486 tree nt, ot;
2487
2488 if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL)
2489 {
2490 if (COMPLETE_TYPE_P (TREE_TYPE (newdecl))
2491 && COMPLETE_TYPE_P (TREE_TYPE (olddecl)))
2492 return G_("redefinition of %q#D");
2493 return NULL;
2494 }
2495
2496 if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) != FUNCTION_DECL
2497 || (DECL_TEMPLATE_RESULT (newdecl)
2498 == DECL_TEMPLATE_RESULT (olddecl)))
2499 return NULL;
2500
2501 nt = DECL_TEMPLATE_RESULT (newdecl);
2502 if (DECL_TEMPLATE_INFO (nt))
2503 nt = DECL_TEMPLATE_RESULT (template_for_substitution (nt));
2504 ot = DECL_TEMPLATE_RESULT (olddecl);
2505 if (DECL_TEMPLATE_INFO (ot))
2506 ot = DECL_TEMPLATE_RESULT (template_for_substitution (ot));
2507 if (DECL_INITIAL (nt) && DECL_INITIAL (ot)
2508 && (!GNU_INLINE_P (ot) || GNU_INLINE_P (nt)))
2509 return G_("redefinition of %q#D");
2510
2511 if (DECL_DECLARED_INLINE_P (ot) && DECL_DECLARED_INLINE_P (nt))
2512 {
2513 bool olda = GNU_INLINE_P (ot);
2514 bool newa = GNU_INLINE_P (nt);
2515
2516 if (olda != newa)
2517 {
2518 if (newa)
2519 return G_("%q+D redeclared inline with "
2520 "%<gnu_inline%> attribute");
2521 else
2522 return G_("%q+D redeclared inline without "
2523 "%<gnu_inline%> attribute");
2524 }
2525 }
2526
2527 /* Core issue #226 (C++0x):
2528
2529 If a friend function template declaration specifies a
2530 default template-argument, that declaration shall be a
2531 definition and shall be the only declaration of the
2532 function template in the translation unit. */
2533 if ((cxx_dialect != cxx98)
2534 && TREE_CODE (ot) == FUNCTION_DECL && DECL_FRIEND_P (ot)
2535 && !check_default_tmpl_args (nt, DECL_TEMPLATE_PARMS (newdecl),
2536 /*is_primary=*/true,
2537 /*is_partial=*/false,
2538 /*is_friend_decl=*/2))
2539 return G_("redeclaration of friend %q#D "
2540 "may not have default template arguments");
2541
2542 return NULL;
2543 }
2544 else if (TREE_CODE (newdecl) == VAR_DECL
2545 && DECL_THREAD_LOCAL_P (newdecl) != DECL_THREAD_LOCAL_P (olddecl)
2546 && (! DECL_LANG_SPECIFIC (olddecl)
2547 || ! CP_DECL_THREADPRIVATE_P (olddecl)
2548 || DECL_THREAD_LOCAL_P (newdecl)))
2549 {
2550 /* Only variables can be thread-local, and all declarations must
2551 agree on this property. */
2552 if (DECL_THREAD_LOCAL_P (newdecl))
2553 return G_("thread-local declaration of %q#D follows "
2554 "non-thread-local declaration");
2555 else
2556 return G_("non-thread-local declaration of %q#D follows "
2557 "thread-local declaration");
2558 }
2559 else if (toplevel_bindings_p () || DECL_NAMESPACE_SCOPE_P (newdecl))
2560 {
2561 /* The objects have been declared at namespace scope. If either
2562 is a member of an anonymous union, then this is an invalid
2563 redeclaration. For example:
2564
2565 int i;
2566 union { int i; };
2567
2568 is invalid. */
2569 if ((TREE_CODE (newdecl) == VAR_DECL && DECL_ANON_UNION_VAR_P (newdecl))
2570 || (TREE_CODE (olddecl) == VAR_DECL && DECL_ANON_UNION_VAR_P (olddecl)))
2571 return G_("redeclaration of %q#D");
2572 /* If at least one declaration is a reference, there is no
2573 conflict. For example:
2574
2575 int i = 3;
2576 extern int i;
2577
2578 is valid. */
2579 if (DECL_EXTERNAL (newdecl) || DECL_EXTERNAL (olddecl))
2580 return NULL;
2581 /* Reject two definitions. */
2582 return G_("redefinition of %q#D");
2583 }
2584 else
2585 {
2586 /* Objects declared with block scope: */
2587 /* Reject two definitions, and reject a definition
2588 together with an external reference. */
2589 if (!(DECL_EXTERNAL (newdecl) && DECL_EXTERNAL (olddecl)))
2590 return G_("redeclaration of %q#D");
2591 return NULL;
2592 }
2593 }
2594 \f
2595 /* Hash and equality functions for the named_label table. */
2596
2597 static hashval_t
2598 named_label_entry_hash (const void *data)
2599 {
2600 const struct named_label_entry *ent = (const struct named_label_entry *) data;
2601 return DECL_UID (ent->label_decl);
2602 }
2603
2604 static int
2605 named_label_entry_eq (const void *a, const void *b)
2606 {
2607 const struct named_label_entry *ent_a = (const struct named_label_entry *) a;
2608 const struct named_label_entry *ent_b = (const struct named_label_entry *) b;
2609 return ent_a->label_decl == ent_b->label_decl;
2610 }
2611
2612 /* Create a new label, named ID. */
2613
2614 static tree
2615 make_label_decl (tree id, int local_p)
2616 {
2617 struct named_label_entry *ent;
2618 void **slot;
2619 tree decl;
2620
2621 decl = build_decl (input_location, LABEL_DECL, id, void_type_node);
2622
2623 DECL_CONTEXT (decl) = current_function_decl;
2624 DECL_MODE (decl) = VOIDmode;
2625 C_DECLARED_LABEL_FLAG (decl) = local_p;
2626
2627 /* Say where one reference is to the label, for the sake of the
2628 error if it is not defined. */
2629 DECL_SOURCE_LOCATION (decl) = input_location;
2630
2631 /* Record the fact that this identifier is bound to this label. */
2632 SET_IDENTIFIER_LABEL_VALUE (id, decl);
2633
2634 /* Create the label htab for the function on demand. */
2635 if (!named_labels)
2636 named_labels = htab_create_ggc (13, named_label_entry_hash,
2637 named_label_entry_eq, NULL);
2638
2639 /* Record this label on the list of labels used in this function.
2640 We do this before calling make_label_decl so that we get the
2641 IDENTIFIER_LABEL_VALUE before the new label is declared. */
2642 ent = ggc_alloc_cleared_named_label_entry ();
2643 ent->label_decl = decl;
2644
2645 slot = htab_find_slot (named_labels, ent, INSERT);
2646 gcc_assert (*slot == NULL);
2647 *slot = ent;
2648
2649 return decl;
2650 }
2651
2652 /* Look for a label named ID in the current function. If one cannot
2653 be found, create one. (We keep track of used, but undefined,
2654 labels, and complain about them at the end of a function.) */
2655
2656 static tree
2657 lookup_label_1 (tree id)
2658 {
2659 tree decl;
2660
2661 /* You can't use labels at global scope. */
2662 if (current_function_decl == NULL_TREE)
2663 {
2664 error ("label %qE referenced outside of any function", id);
2665 return NULL_TREE;
2666 }
2667
2668 /* See if we've already got this label. */
2669 decl = IDENTIFIER_LABEL_VALUE (id);
2670 if (decl != NULL_TREE && DECL_CONTEXT (decl) == current_function_decl)
2671 return decl;
2672
2673 decl = make_label_decl (id, /*local_p=*/0);
2674 return decl;
2675 }
2676
2677 /* Wrapper for lookup_label_1. */
2678
2679 tree
2680 lookup_label (tree id)
2681 {
2682 tree ret;
2683 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
2684 ret = lookup_label_1 (id);
2685 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
2686 return ret;
2687 }
2688
2689 /* Declare a local label named ID. */
2690
2691 tree
2692 declare_local_label (tree id)
2693 {
2694 tree decl;
2695 cp_label_binding bind;
2696
2697 /* Add a new entry to the SHADOWED_LABELS list so that when we leave
2698 this scope we can restore the old value of IDENTIFIER_TYPE_VALUE. */
2699 bind.prev_value = IDENTIFIER_LABEL_VALUE (id);
2700
2701 decl = make_label_decl (id, /*local_p=*/1);
2702 bind.label = decl;
2703 vec_safe_push (current_binding_level->shadowed_labels, bind);
2704
2705 return decl;
2706 }
2707
2708 /* Returns nonzero if it is ill-formed to jump past the declaration of
2709 DECL. Returns 2 if it's also a real problem. */
2710
2711 static int
2712 decl_jump_unsafe (tree decl)
2713 {
2714 /* [stmt.dcl]/3: A program that jumps from a point where a local variable
2715 with automatic storage duration is not in scope to a point where it is
2716 in scope is ill-formed unless the variable has scalar type, class type
2717 with a trivial default constructor and a trivial destructor, a
2718 cv-qualified version of one of these types, or an array of one of the
2719 preceding types and is declared without an initializer (8.5). */
2720 tree type = TREE_TYPE (decl);
2721
2722 if (TREE_CODE (decl) != VAR_DECL || TREE_STATIC (decl)
2723 || type == error_mark_node)
2724 return 0;
2725
2726 type = strip_array_types (type);
2727
2728 if (DECL_NONTRIVIALLY_INITIALIZED_P (decl))
2729 return 2;
2730
2731 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
2732 return 1;
2733
2734 return 0;
2735 }
2736
2737 /* A subroutine of check_previous_goto_1 to identify a branch to the user. */
2738
2739 static void
2740 identify_goto (tree decl, const location_t *locus)
2741 {
2742 if (decl)
2743 permerror (input_location, "jump to label %qD", decl);
2744 else
2745 permerror (input_location, "jump to case label");
2746 if (locus)
2747 permerror (*locus, " from here");
2748 }
2749
2750 /* Check that a single previously seen jump to a newly defined label
2751 is OK. DECL is the LABEL_DECL or 0; LEVEL is the binding_level for
2752 the jump context; NAMES are the names in scope in LEVEL at the jump
2753 context; LOCUS is the source position of the jump or 0. Returns
2754 true if all is well. */
2755
2756 static bool
2757 check_previous_goto_1 (tree decl, cp_binding_level* level, tree names,
2758 bool exited_omp, const location_t *locus)
2759 {
2760 cp_binding_level *b;
2761 bool identified = false, saw_eh = false, saw_omp = false;
2762
2763 if (exited_omp)
2764 {
2765 identify_goto (decl, locus);
2766 error (" exits OpenMP structured block");
2767 identified = saw_omp = true;
2768 }
2769
2770 for (b = current_binding_level; b ; b = b->level_chain)
2771 {
2772 tree new_decls, old_decls = (b == level ? names : NULL_TREE);
2773
2774 for (new_decls = b->names; new_decls != old_decls;
2775 new_decls = (DECL_P (new_decls) ? DECL_CHAIN (new_decls)
2776 : TREE_CHAIN (new_decls)))
2777 {
2778 int problem = decl_jump_unsafe (new_decls);
2779 if (! problem)
2780 continue;
2781
2782 if (!identified)
2783 {
2784 identify_goto (decl, locus);
2785 identified = true;
2786 }
2787 if (problem > 1)
2788 error (" crosses initialization of %q+#D", new_decls);
2789 else
2790 permerror (input_location, " enters scope of %q+#D which has "
2791 "non-trivial destructor", new_decls);
2792 }
2793
2794 if (b == level)
2795 break;
2796 if ((b->kind == sk_try || b->kind == sk_catch) && !saw_eh)
2797 {
2798 if (!identified)
2799 {
2800 identify_goto (decl, locus);
2801 identified = true;
2802 }
2803 if (b->kind == sk_try)
2804 error (" enters try block");
2805 else
2806 error (" enters catch block");
2807 saw_eh = true;
2808 }
2809 if (b->kind == sk_omp && !saw_omp)
2810 {
2811 if (!identified)
2812 {
2813 identify_goto (decl, locus);
2814 identified = true;
2815 }
2816 error (" enters OpenMP structured block");
2817 saw_omp = true;
2818 }
2819 }
2820
2821 return !identified;
2822 }
2823
2824 static void
2825 check_previous_goto (tree decl, struct named_label_use_entry *use)
2826 {
2827 check_previous_goto_1 (decl, use->binding_level,
2828 use->names_in_scope, use->in_omp_scope,
2829 &use->o_goto_locus);
2830 }
2831
2832 static bool
2833 check_switch_goto (cp_binding_level* level)
2834 {
2835 return check_previous_goto_1 (NULL_TREE, level, level->names, false, NULL);
2836 }
2837
2838 /* Check that a new jump to a label DECL is OK. Called by
2839 finish_goto_stmt. */
2840
2841 void
2842 check_goto (tree decl)
2843 {
2844 struct named_label_entry *ent, dummy;
2845 bool saw_catch = false, identified = false;
2846 tree bad;
2847 unsigned ix;
2848
2849 /* We can't know where a computed goto is jumping.
2850 So we assume that it's OK. */
2851 if (TREE_CODE (decl) != LABEL_DECL)
2852 return;
2853
2854 /* We didn't record any information about this label when we created it,
2855 and there's not much point since it's trivial to analyze as a return. */
2856 if (decl == cdtor_label)
2857 return;
2858
2859 dummy.label_decl = decl;
2860 ent = (struct named_label_entry *) htab_find (named_labels, &dummy);
2861 gcc_assert (ent != NULL);
2862
2863 /* If the label hasn't been defined yet, defer checking. */
2864 if (! DECL_INITIAL (decl))
2865 {
2866 struct named_label_use_entry *new_use;
2867
2868 /* Don't bother creating another use if the last goto had the
2869 same data, and will therefore create the same set of errors. */
2870 if (ent->uses
2871 && ent->uses->names_in_scope == current_binding_level->names)
2872 return;
2873
2874 new_use = ggc_alloc_named_label_use_entry ();
2875 new_use->binding_level = current_binding_level;
2876 new_use->names_in_scope = current_binding_level->names;
2877 new_use->o_goto_locus = input_location;
2878 new_use->in_omp_scope = false;
2879
2880 new_use->next = ent->uses;
2881 ent->uses = new_use;
2882 return;
2883 }
2884
2885 if (ent->in_try_scope || ent->in_catch_scope
2886 || ent->in_omp_scope || !vec_safe_is_empty (ent->bad_decls))
2887 {
2888 permerror (input_location, "jump to label %q+D", decl);
2889 permerror (input_location, " from here");
2890 identified = true;
2891 }
2892
2893 FOR_EACH_VEC_SAFE_ELT (ent->bad_decls, ix, bad)
2894 {
2895 int u = decl_jump_unsafe (bad);
2896
2897 if (u > 1 && DECL_ARTIFICIAL (bad))
2898 {
2899 /* Can't skip init of __exception_info. */
2900 error_at (DECL_SOURCE_LOCATION (bad), " enters catch block");
2901 saw_catch = true;
2902 }
2903 else if (u > 1)
2904 error (" skips initialization of %q+#D", bad);
2905 else
2906 permerror (input_location, " enters scope of %q+#D which has "
2907 "non-trivial destructor", bad);
2908 }
2909
2910 if (ent->in_try_scope)
2911 error (" enters try block");
2912 else if (ent->in_catch_scope && !saw_catch)
2913 error (" enters catch block");
2914
2915 if (ent->in_omp_scope)
2916 error (" enters OpenMP structured block");
2917 else if (flag_openmp)
2918 {
2919 cp_binding_level *b;
2920 for (b = current_binding_level; b ; b = b->level_chain)
2921 {
2922 if (b == ent->binding_level)
2923 break;
2924 if (b->kind == sk_omp)
2925 {
2926 if (!identified)
2927 {
2928 permerror (input_location, "jump to label %q+D", decl);
2929 permerror (input_location, " from here");
2930 identified = true;
2931 }
2932 error (" exits OpenMP structured block");
2933 break;
2934 }
2935 }
2936 }
2937 }
2938
2939 /* Check that a return is ok wrt OpenMP structured blocks.
2940 Called by finish_return_stmt. Returns true if all is well. */
2941
2942 bool
2943 check_omp_return (void)
2944 {
2945 cp_binding_level *b;
2946 for (b = current_binding_level; b ; b = b->level_chain)
2947 if (b->kind == sk_omp)
2948 {
2949 error ("invalid exit from OpenMP structured block");
2950 return false;
2951 }
2952 else if (b->kind == sk_function_parms)
2953 break;
2954 return true;
2955 }
2956
2957 /* Define a label, specifying the location in the source file.
2958 Return the LABEL_DECL node for the label. */
2959
2960 static tree
2961 define_label_1 (location_t location, tree name)
2962 {
2963 struct named_label_entry *ent, dummy;
2964 cp_binding_level *p;
2965 tree decl;
2966
2967 decl = lookup_label (name);
2968
2969 dummy.label_decl = decl;
2970 ent = (struct named_label_entry *) htab_find (named_labels, &dummy);
2971 gcc_assert (ent != NULL);
2972
2973 /* After labels, make any new cleanups in the function go into their
2974 own new (temporary) binding contour. */
2975 for (p = current_binding_level;
2976 p->kind != sk_function_parms;
2977 p = p->level_chain)
2978 p->more_cleanups_ok = 0;
2979
2980 if (name == get_identifier ("wchar_t"))
2981 permerror (input_location, "label named wchar_t");
2982
2983 if (DECL_INITIAL (decl) != NULL_TREE)
2984 {
2985 error ("duplicate label %qD", decl);
2986 return error_mark_node;
2987 }
2988 else
2989 {
2990 struct named_label_use_entry *use;
2991
2992 /* Mark label as having been defined. */
2993 DECL_INITIAL (decl) = error_mark_node;
2994 /* Say where in the source. */
2995 DECL_SOURCE_LOCATION (decl) = location;
2996
2997 ent->binding_level = current_binding_level;
2998 ent->names_in_scope = current_binding_level->names;
2999
3000 for (use = ent->uses; use ; use = use->next)
3001 check_previous_goto (decl, use);
3002 ent->uses = NULL;
3003 }
3004
3005 return decl;
3006 }
3007
3008 /* Wrapper for define_label_1. */
3009
3010 tree
3011 define_label (location_t location, tree name)
3012 {
3013 tree ret;
3014 bool running = timevar_cond_start (TV_NAME_LOOKUP);
3015 ret = define_label_1 (location, name);
3016 timevar_cond_stop (TV_NAME_LOOKUP, running);
3017 return ret;
3018 }
3019
3020
3021 struct cp_switch
3022 {
3023 cp_binding_level *level;
3024 struct cp_switch *next;
3025 /* The SWITCH_STMT being built. */
3026 tree switch_stmt;
3027 /* A splay-tree mapping the low element of a case range to the high
3028 element, or NULL_TREE if there is no high element. Used to
3029 determine whether or not a new case label duplicates an old case
3030 label. We need a tree, rather than simply a hash table, because
3031 of the GNU case range extension. */
3032 splay_tree cases;
3033 };
3034
3035 /* A stack of the currently active switch statements. The innermost
3036 switch statement is on the top of the stack. There is no need to
3037 mark the stack for garbage collection because it is only active
3038 during the processing of the body of a function, and we never
3039 collect at that point. */
3040
3041 static struct cp_switch *switch_stack;
3042
3043 /* Called right after a switch-statement condition is parsed.
3044 SWITCH_STMT is the switch statement being parsed. */
3045
3046 void
3047 push_switch (tree switch_stmt)
3048 {
3049 struct cp_switch *p = XNEW (struct cp_switch);
3050 p->level = current_binding_level;
3051 p->next = switch_stack;
3052 p->switch_stmt = switch_stmt;
3053 p->cases = splay_tree_new (case_compare, NULL, NULL);
3054 switch_stack = p;
3055 }
3056
3057 void
3058 pop_switch (void)
3059 {
3060 struct cp_switch *cs = switch_stack;
3061 location_t switch_location;
3062
3063 /* Emit warnings as needed. */
3064 switch_location = EXPR_LOC_OR_HERE (cs->switch_stmt);
3065 if (!processing_template_decl)
3066 c_do_switch_warnings (cs->cases, switch_location,
3067 SWITCH_STMT_TYPE (cs->switch_stmt),
3068 SWITCH_STMT_COND (cs->switch_stmt));
3069
3070 splay_tree_delete (cs->cases);
3071 switch_stack = switch_stack->next;
3072 free (cs);
3073 }
3074
3075 /* Convert a case constant VALUE in a switch to the type TYPE of the switch
3076 condition. Note that if TYPE and VALUE are already integral we don't
3077 really do the conversion because the language-independent
3078 warning/optimization code will work better that way. */
3079
3080 static tree
3081 case_conversion (tree type, tree value)
3082 {
3083 if (value == NULL_TREE)
3084 return value;
3085
3086 if (cxx_dialect >= cxx0x
3087 && (SCOPED_ENUM_P (type)
3088 || !INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (value))))
3089 {
3090 if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type))
3091 type = type_promotes_to (type);
3092 value = perform_implicit_conversion (type, value, tf_warning_or_error);
3093 }
3094 return cxx_constant_value (value);
3095 }
3096
3097 /* Note that we've seen a definition of a case label, and complain if this
3098 is a bad place for one. */
3099
3100 tree
3101 finish_case_label (location_t loc, tree low_value, tree high_value)
3102 {
3103 tree cond, r;
3104 cp_binding_level *p;
3105 tree type;
3106
3107 if (processing_template_decl)
3108 {
3109 tree label;
3110
3111 /* For templates, just add the case label; we'll do semantic
3112 analysis at instantiation-time. */
3113 label = build_decl (loc, LABEL_DECL, NULL_TREE, NULL_TREE);
3114 return add_stmt (build_case_label (low_value, high_value, label));
3115 }
3116
3117 /* Find the condition on which this switch statement depends. */
3118 cond = SWITCH_STMT_COND (switch_stack->switch_stmt);
3119 if (cond && TREE_CODE (cond) == TREE_LIST)
3120 cond = TREE_VALUE (cond);
3121
3122 if (!check_switch_goto (switch_stack->level))
3123 return error_mark_node;
3124
3125 type = SWITCH_STMT_TYPE (switch_stack->switch_stmt);
3126
3127 low_value = case_conversion (type, low_value);
3128 high_value = case_conversion (type, high_value);
3129
3130 r = c_add_case_label (loc, switch_stack->cases, cond, type,
3131 low_value, high_value);
3132
3133 /* After labels, make any new cleanups in the function go into their
3134 own new (temporary) binding contour. */
3135 for (p = current_binding_level;
3136 p->kind != sk_function_parms;
3137 p = p->level_chain)
3138 p->more_cleanups_ok = 0;
3139
3140 return r;
3141 }
3142 \f
3143 /* Hash a TYPENAME_TYPE. K is really of type `tree'. */
3144
3145 static hashval_t
3146 typename_hash (const void* k)
3147 {
3148 hashval_t hash;
3149 const_tree const t = (const_tree) k;
3150
3151 hash = (htab_hash_pointer (TYPE_CONTEXT (t))
3152 ^ htab_hash_pointer (DECL_NAME (TYPE_NAME (t))));
3153
3154 return hash;
3155 }
3156
3157 typedef struct typename_info {
3158 tree scope;
3159 tree name;
3160 tree template_id;
3161 bool enum_p;
3162 bool class_p;
3163 } typename_info;
3164
3165 /* Compare two TYPENAME_TYPEs. K1 is really of type `tree', K2 is
3166 really of type `typename_info*' */
3167
3168 static int
3169 typename_compare (const void * k1, const void * k2)
3170 {
3171 const_tree const t1 = (const_tree) k1;
3172 const typename_info *const t2 = (const typename_info *) k2;
3173
3174 return (DECL_NAME (TYPE_NAME (t1)) == t2->name
3175 && TYPE_CONTEXT (t1) == t2->scope
3176 && TYPENAME_TYPE_FULLNAME (t1) == t2->template_id
3177 && TYPENAME_IS_ENUM_P (t1) == t2->enum_p
3178 && TYPENAME_IS_CLASS_P (t1) == t2->class_p);
3179 }
3180
3181 /* Build a TYPENAME_TYPE. If the type is `typename T::t', CONTEXT is
3182 the type of `T', NAME is the IDENTIFIER_NODE for `t'.
3183
3184 Returns the new TYPENAME_TYPE. */
3185
3186 static GTY ((param_is (union tree_node))) htab_t typename_htab;
3187
3188 static tree
3189 build_typename_type (tree context, tree name, tree fullname,
3190 enum tag_types tag_type)
3191 {
3192 tree t;
3193 tree d;
3194 typename_info ti;
3195 void **e;
3196 hashval_t hash;
3197
3198 if (typename_htab == NULL)
3199 typename_htab = htab_create_ggc (61, &typename_hash,
3200 &typename_compare, NULL);
3201
3202 ti.scope = FROB_CONTEXT (context);
3203 ti.name = name;
3204 ti.template_id = fullname;
3205 ti.enum_p = tag_type == enum_type;
3206 ti.class_p = (tag_type == class_type
3207 || tag_type == record_type
3208 || tag_type == union_type);
3209 hash = (htab_hash_pointer (ti.scope)
3210 ^ htab_hash_pointer (ti.name));
3211
3212 /* See if we already have this type. */
3213 e = htab_find_slot_with_hash (typename_htab, &ti, hash, INSERT);
3214 if (*e)
3215 t = (tree) *e;
3216 else
3217 {
3218 /* Build the TYPENAME_TYPE. */
3219 t = cxx_make_type (TYPENAME_TYPE);
3220 TYPE_CONTEXT (t) = ti.scope;
3221 TYPENAME_TYPE_FULLNAME (t) = ti.template_id;
3222 TYPENAME_IS_ENUM_P (t) = ti.enum_p;
3223 TYPENAME_IS_CLASS_P (t) = ti.class_p;
3224
3225 /* Build the corresponding TYPE_DECL. */
3226 d = build_decl (input_location, TYPE_DECL, name, t);
3227 TYPE_NAME (TREE_TYPE (d)) = d;
3228 TYPE_STUB_DECL (TREE_TYPE (d)) = d;
3229 DECL_CONTEXT (d) = FROB_CONTEXT (context);
3230 DECL_ARTIFICIAL (d) = 1;
3231
3232 /* Store it in the hash table. */
3233 *e = t;
3234
3235 /* TYPENAME_TYPEs must always be compared structurally, because
3236 they may or may not resolve down to another type depending on
3237 the currently open classes. */
3238 SET_TYPE_STRUCTURAL_EQUALITY (t);
3239 }
3240
3241 return t;
3242 }
3243
3244 /* Resolve `typename CONTEXT::NAME'. TAG_TYPE indicates the tag
3245 provided to name the type. Returns an appropriate type, unless an
3246 error occurs, in which case error_mark_node is returned. If we
3247 locate a non-artificial TYPE_DECL and TF_KEEP_TYPE_DECL is set, we
3248 return that, rather than the _TYPE it corresponds to, in other
3249 cases we look through the type decl. If TF_ERROR is set, complain
3250 about errors, otherwise be quiet. */
3251
3252 tree
3253 make_typename_type (tree context, tree name, enum tag_types tag_type,
3254 tsubst_flags_t complain)
3255 {
3256 tree fullname;
3257 tree t;
3258 bool want_template;
3259
3260 if (name == error_mark_node
3261 || context == NULL_TREE
3262 || context == error_mark_node)
3263 return error_mark_node;
3264
3265 if (TYPE_P (name))
3266 {
3267 if (!(TYPE_LANG_SPECIFIC (name)
3268 && (CLASSTYPE_IS_TEMPLATE (name)
3269 || CLASSTYPE_USE_TEMPLATE (name))))
3270 name = TYPE_IDENTIFIER (name);
3271 else
3272 /* Create a TEMPLATE_ID_EXPR for the type. */
3273 name = build_nt (TEMPLATE_ID_EXPR,
3274 CLASSTYPE_TI_TEMPLATE (name),
3275 CLASSTYPE_TI_ARGS (name));
3276 }
3277 else if (TREE_CODE (name) == TYPE_DECL)
3278 name = DECL_NAME (name);
3279
3280 fullname = name;
3281
3282 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
3283 {
3284 name = TREE_OPERAND (name, 0);
3285 if (TREE_CODE (name) == TEMPLATE_DECL)
3286 name = TREE_OPERAND (fullname, 0) = DECL_NAME (name);
3287 else if (TREE_CODE (name) == OVERLOAD)
3288 {
3289 if (complain & tf_error)
3290 error ("%qD is not a type", name);
3291 return error_mark_node;
3292 }
3293 }
3294 if (TREE_CODE (name) == TEMPLATE_DECL)
3295 {
3296 if (complain & tf_error)
3297 error ("%qD used without template parameters", name);
3298 return error_mark_node;
3299 }
3300 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
3301 gcc_assert (TYPE_P (context));
3302
3303 if (!MAYBE_CLASS_TYPE_P (context))
3304 {
3305 if (complain & tf_error)
3306 error ("%q#T is not a class", context);
3307 return error_mark_node;
3308 }
3309
3310 /* When the CONTEXT is a dependent type, NAME could refer to a
3311 dependent base class of CONTEXT. But look inside it anyway
3312 if CONTEXT is a currently open scope, in case it refers to a
3313 member of the current instantiation or a non-dependent base;
3314 lookup will stop when we hit a dependent base. */
3315 if (!dependent_scope_p (context))
3316 /* We should only set WANT_TYPE when we're a nested typename type.
3317 Then we can give better diagnostics if we find a non-type. */
3318 t = lookup_field (context, name, 2, /*want_type=*/true);
3319 else
3320 t = NULL_TREE;
3321
3322 if ((!t || TREE_CODE (t) == TREE_LIST) && dependent_type_p (context))
3323 return build_typename_type (context, name, fullname, tag_type);
3324
3325 want_template = TREE_CODE (fullname) == TEMPLATE_ID_EXPR;
3326
3327 if (!t)
3328 {
3329 if (complain & tf_error)
3330 error (want_template ? G_("no class template named %q#T in %q#T")
3331 : G_("no type named %q#T in %q#T"), name, context);
3332 return error_mark_node;
3333 }
3334
3335 /* Pull out the template from an injected-class-name (or multiple). */
3336 if (want_template)
3337 t = maybe_get_template_decl_from_type_decl (t);
3338
3339 if (TREE_CODE (t) == TREE_LIST)
3340 {
3341 if (complain & tf_error)
3342 {
3343 error ("lookup of %qT in %qT is ambiguous", name, context);
3344 print_candidates (t);
3345 }
3346 return error_mark_node;
3347 }
3348
3349 if (want_template && !DECL_TYPE_TEMPLATE_P (t))
3350 {
3351 if (complain & tf_error)
3352 error ("%<typename %T::%D%> names %q#T, which is not a class template",
3353 context, name, t);
3354 return error_mark_node;
3355 }
3356 if (!want_template && TREE_CODE (t) != TYPE_DECL)
3357 {
3358 if (complain & tf_error)
3359 error ("%<typename %T::%D%> names %q#T, which is not a type",
3360 context, name, t);
3361 return error_mark_node;
3362 }
3363
3364 if (!perform_or_defer_access_check (TYPE_BINFO (context), t, t, complain))
3365 return error_mark_node;
3366
3367 /* If we are currently parsing a template and if T is a typedef accessed
3368 through CONTEXT then we need to remember and check access of T at
3369 template instantiation time. */
3370 add_typedef_to_current_template_for_access_check (t, context, input_location);
3371
3372 if (want_template)
3373 return lookup_template_class (t, TREE_OPERAND (fullname, 1),
3374 NULL_TREE, context,
3375 /*entering_scope=*/0,
3376 tf_warning_or_error | tf_user);
3377
3378 if (DECL_ARTIFICIAL (t) || !(complain & tf_keep_type_decl))
3379 t = TREE_TYPE (t);
3380
3381 maybe_record_typedef_use (t);
3382
3383 return t;
3384 }
3385
3386 /* Resolve `CONTEXT::template NAME'. Returns a TEMPLATE_DECL if the name
3387 can be resolved or an UNBOUND_CLASS_TEMPLATE, unless an error occurs,
3388 in which case error_mark_node is returned.
3389
3390 If PARM_LIST is non-NULL, also make sure that the template parameter
3391 list of TEMPLATE_DECL matches.
3392
3393 If COMPLAIN zero, don't complain about any errors that occur. */
3394
3395 tree
3396 make_unbound_class_template (tree context, tree name, tree parm_list,
3397 tsubst_flags_t complain)
3398 {
3399 tree t;
3400 tree d;
3401
3402 if (TYPE_P (name))
3403 name = TYPE_IDENTIFIER (name);
3404 else if (DECL_P (name))
3405 name = DECL_NAME (name);
3406 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
3407
3408 if (!dependent_type_p (context)
3409 || currently_open_class (context))
3410 {
3411 tree tmpl = NULL_TREE;
3412
3413 if (MAYBE_CLASS_TYPE_P (context))
3414 tmpl = lookup_field (context, name, 0, false);
3415
3416 if (tmpl && TREE_CODE (tmpl) == TYPE_DECL)
3417 tmpl = maybe_get_template_decl_from_type_decl (tmpl);
3418
3419 if (!tmpl || !DECL_TYPE_TEMPLATE_P (tmpl))
3420 {
3421 if (complain & tf_error)
3422 error ("no class template named %q#T in %q#T", name, context);
3423 return error_mark_node;
3424 }
3425
3426 if (parm_list
3427 && !comp_template_parms (DECL_TEMPLATE_PARMS (tmpl), parm_list))
3428 {
3429 if (complain & tf_error)
3430 {
3431 error ("template parameters do not match template");
3432 error ("%q+D declared here", tmpl);
3433 }
3434 return error_mark_node;
3435 }
3436
3437 if (!perform_or_defer_access_check (TYPE_BINFO (context), tmpl, tmpl,
3438 complain))
3439 return error_mark_node;
3440
3441 return tmpl;
3442 }
3443
3444 /* Build the UNBOUND_CLASS_TEMPLATE. */
3445 t = cxx_make_type (UNBOUND_CLASS_TEMPLATE);
3446 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
3447 TREE_TYPE (t) = NULL_TREE;
3448 SET_TYPE_STRUCTURAL_EQUALITY (t);
3449
3450 /* Build the corresponding TEMPLATE_DECL. */
3451 d = build_decl (input_location, TEMPLATE_DECL, name, t);
3452 TYPE_NAME (TREE_TYPE (d)) = d;
3453 TYPE_STUB_DECL (TREE_TYPE (d)) = d;
3454 DECL_CONTEXT (d) = FROB_CONTEXT (context);
3455 DECL_ARTIFICIAL (d) = 1;
3456 DECL_TEMPLATE_PARMS (d) = parm_list;
3457
3458 return t;
3459 }
3460
3461 \f
3462
3463 /* Push the declarations of builtin types into the namespace.
3464 RID_INDEX is the index of the builtin type in the array
3465 RID_POINTERS. NAME is the name used when looking up the builtin
3466 type. TYPE is the _TYPE node for the builtin type. */
3467
3468 void
3469 record_builtin_type (enum rid rid_index,
3470 const char* name,
3471 tree type)
3472 {
3473 tree rname = NULL_TREE, tname = NULL_TREE;
3474 tree tdecl = NULL_TREE;
3475
3476 if ((int) rid_index < (int) RID_MAX)
3477 rname = ridpointers[(int) rid_index];
3478 if (name)
3479 tname = get_identifier (name);
3480
3481 /* The calls to SET_IDENTIFIER_GLOBAL_VALUE below should be
3482 eliminated. Built-in types should not be looked up name; their
3483 names are keywords that the parser can recognize. However, there
3484 is code in c-common.c that uses identifier_global_value to look
3485 up built-in types by name. */
3486 if (tname)
3487 {
3488 tdecl = build_decl (BUILTINS_LOCATION, TYPE_DECL, tname, type);
3489 DECL_ARTIFICIAL (tdecl) = 1;
3490 SET_IDENTIFIER_GLOBAL_VALUE (tname, tdecl);
3491 }
3492 if (rname)
3493 {
3494 if (!tdecl)
3495 {
3496 tdecl = build_decl (BUILTINS_LOCATION, TYPE_DECL, rname, type);
3497 DECL_ARTIFICIAL (tdecl) = 1;
3498 }
3499 SET_IDENTIFIER_GLOBAL_VALUE (rname, tdecl);
3500 }
3501
3502 if (!TYPE_NAME (type))
3503 TYPE_NAME (type) = tdecl;
3504
3505 if (tdecl)
3506 debug_hooks->type_decl (tdecl, 0);
3507 }
3508
3509 /* Record one of the standard Java types.
3510 * Declare it as having the given NAME.
3511 * If SIZE > 0, it is the size of one of the integral types;
3512 * otherwise it is the negative of the size of one of the other types. */
3513
3514 static tree
3515 record_builtin_java_type (const char* name, int size)
3516 {
3517 tree type, decl;
3518 if (size > 0)
3519 {
3520 type = build_nonstandard_integer_type (size, 0);
3521 type = build_distinct_type_copy (type);
3522 }
3523 else if (size > -32)
3524 {
3525 tree stype;
3526 /* "__java_char" or ""__java_boolean". */
3527 type = build_nonstandard_integer_type (-size, 1);
3528 type = build_distinct_type_copy (type);
3529 /* Get the signed type cached and attached to the unsigned type,
3530 so it doesn't get garbage-collected at "random" times,
3531 causing potential codegen differences out of different UIDs
3532 and different alias set numbers. */
3533 stype = build_nonstandard_integer_type (-size, 0);
3534 stype = build_distinct_type_copy (stype);
3535 TREE_CHAIN (type) = stype;
3536 /*if (size == -1) TREE_SET_CODE (type, BOOLEAN_TYPE);*/
3537 }
3538 else
3539 { /* "__java_float" or ""__java_double". */
3540 type = make_node (REAL_TYPE);
3541 TYPE_PRECISION (type) = - size;
3542 layout_type (type);
3543 }
3544 record_builtin_type (RID_MAX, name, type);
3545 decl = TYPE_NAME (type);
3546
3547 /* Suppress generate debug symbol entries for these types,
3548 since for normal C++ they are just clutter.
3549 However, push_lang_context undoes this if extern "Java" is seen. */
3550 DECL_IGNORED_P (decl) = 1;
3551
3552 TYPE_FOR_JAVA (type) = 1;
3553 return type;
3554 }
3555
3556 /* Push a type into the namespace so that the back ends ignore it. */
3557
3558 static void
3559 record_unknown_type (tree type, const char* name)
3560 {
3561 tree decl = pushdecl (build_decl (UNKNOWN_LOCATION,
3562 TYPE_DECL, get_identifier (name), type));
3563 /* Make sure the "unknown type" typedecl gets ignored for debug info. */
3564 DECL_IGNORED_P (decl) = 1;
3565 TYPE_DECL_SUPPRESS_DEBUG (decl) = 1;
3566 TYPE_SIZE (type) = TYPE_SIZE (void_type_node);
3567 TYPE_ALIGN (type) = 1;
3568 TYPE_USER_ALIGN (type) = 0;
3569 SET_TYPE_MODE (type, TYPE_MODE (void_type_node));
3570 }
3571
3572 /* A string for which we should create an IDENTIFIER_NODE at
3573 startup. */
3574
3575 typedef struct predefined_identifier
3576 {
3577 /* The name of the identifier. */
3578 const char *const name;
3579 /* The place where the IDENTIFIER_NODE should be stored. */
3580 tree *const node;
3581 /* Nonzero if this is the name of a constructor or destructor. */
3582 const int ctor_or_dtor_p;
3583 } predefined_identifier;
3584
3585 /* Create all the predefined identifiers. */
3586
3587 static void
3588 initialize_predefined_identifiers (void)
3589 {
3590 const predefined_identifier *pid;
3591
3592 /* A table of identifiers to create at startup. */
3593 static const predefined_identifier predefined_identifiers[] = {
3594 { "C++", &lang_name_cplusplus, 0 },
3595 { "C", &lang_name_c, 0 },
3596 { "Java", &lang_name_java, 0 },
3597 /* Some of these names have a trailing space so that it is
3598 impossible for them to conflict with names written by users. */
3599 { "__ct ", &ctor_identifier, 1 },
3600 { "__base_ctor ", &base_ctor_identifier, 1 },
3601 { "__comp_ctor ", &complete_ctor_identifier, 1 },
3602 { "__dt ", &dtor_identifier, 1 },
3603 { "__comp_dtor ", &complete_dtor_identifier, 1 },
3604 { "__base_dtor ", &base_dtor_identifier, 1 },
3605 { "__deleting_dtor ", &deleting_dtor_identifier, 1 },
3606 { IN_CHARGE_NAME, &in_charge_identifier, 0 },
3607 { "nelts", &nelts_identifier, 0 },
3608 { THIS_NAME, &this_identifier, 0 },
3609 { VTABLE_DELTA_NAME, &delta_identifier, 0 },
3610 { VTABLE_PFN_NAME, &pfn_identifier, 0 },
3611 { "_vptr", &vptr_identifier, 0 },
3612 { "__vtt_parm", &vtt_parm_identifier, 0 },
3613 { "::", &global_scope_name, 0 },
3614 { "std", &std_identifier, 0 },
3615 { NULL, NULL, 0 }
3616 };
3617
3618 for (pid = predefined_identifiers; pid->name; ++pid)
3619 {
3620 *pid->node = get_identifier (pid->name);
3621 if (pid->ctor_or_dtor_p)
3622 IDENTIFIER_CTOR_OR_DTOR_P (*pid->node) = 1;
3623 }
3624 }
3625
3626 /* Create the predefined scalar types of C,
3627 and some nodes representing standard constants (0, 1, (void *)0).
3628 Initialize the global binding level.
3629 Make definitions for built-in primitive functions. */
3630
3631 void
3632 cxx_init_decl_processing (void)
3633 {
3634 tree void_ftype;
3635 tree void_ftype_ptr;
3636
3637 /* Create all the identifiers we need. */
3638 initialize_predefined_identifiers ();
3639
3640 /* Create the global variables. */
3641 push_to_top_level ();
3642
3643 current_function_decl = NULL_TREE;
3644 current_binding_level = NULL;
3645 /* Enter the global namespace. */
3646 gcc_assert (global_namespace == NULL_TREE);
3647 global_namespace = build_lang_decl (NAMESPACE_DECL, global_scope_name,
3648 void_type_node);
3649 DECL_CONTEXT (global_namespace) = build_translation_unit_decl (NULL_TREE);
3650 TREE_PUBLIC (global_namespace) = 1;
3651 begin_scope (sk_namespace, global_namespace);
3652
3653 if (flag_visibility_ms_compat)
3654 default_visibility = VISIBILITY_HIDDEN;
3655
3656 /* Initially, C. */
3657 current_lang_name = lang_name_c;
3658
3659 /* Create the `std' namespace. */
3660 push_namespace (std_identifier);
3661 std_node = current_namespace;
3662 pop_namespace ();
3663
3664 c_common_nodes_and_builtins ();
3665
3666 java_byte_type_node = record_builtin_java_type ("__java_byte", 8);
3667 java_short_type_node = record_builtin_java_type ("__java_short", 16);
3668 java_int_type_node = record_builtin_java_type ("__java_int", 32);
3669 java_long_type_node = record_builtin_java_type ("__java_long", 64);
3670 java_float_type_node = record_builtin_java_type ("__java_float", -32);
3671 java_double_type_node = record_builtin_java_type ("__java_double", -64);
3672 java_char_type_node = record_builtin_java_type ("__java_char", -16);
3673 java_boolean_type_node = record_builtin_java_type ("__java_boolean", -1);
3674
3675 integer_two_node = build_int_cst (NULL_TREE, 2);
3676
3677 record_builtin_type (RID_BOOL, "bool", boolean_type_node);
3678 truthvalue_type_node = boolean_type_node;
3679 truthvalue_false_node = boolean_false_node;
3680 truthvalue_true_node = boolean_true_node;
3681
3682 empty_except_spec = build_tree_list (NULL_TREE, NULL_TREE);
3683 noexcept_true_spec = build_tree_list (boolean_true_node, NULL_TREE);
3684 noexcept_false_spec = build_tree_list (boolean_false_node, NULL_TREE);
3685
3686 #if 0
3687 record_builtin_type (RID_MAX, NULL, string_type_node);
3688 #endif
3689
3690 delta_type_node = ptrdiff_type_node;
3691 vtable_index_type = ptrdiff_type_node;
3692
3693 vtt_parm_type = build_pointer_type (const_ptr_type_node);
3694 void_ftype = build_function_type_list (void_type_node, NULL_TREE);
3695 void_ftype_ptr = build_function_type_list (void_type_node,
3696 ptr_type_node, NULL_TREE);
3697 void_ftype_ptr
3698 = build_exception_variant (void_ftype_ptr, empty_except_spec);
3699
3700 /* C++ extensions */
3701
3702 unknown_type_node = make_node (LANG_TYPE);
3703 record_unknown_type (unknown_type_node, "unknown type");
3704
3705 /* Indirecting an UNKNOWN_TYPE node yields an UNKNOWN_TYPE node. */
3706 TREE_TYPE (unknown_type_node) = unknown_type_node;
3707
3708 /* Looking up TYPE_POINTER_TO and TYPE_REFERENCE_TO yield the same
3709 result. */
3710 TYPE_POINTER_TO (unknown_type_node) = unknown_type_node;
3711 TYPE_REFERENCE_TO (unknown_type_node) = unknown_type_node;
3712
3713 init_list_type_node = make_node (LANG_TYPE);
3714 record_unknown_type (init_list_type_node, "init list");
3715
3716 {
3717 /* Make sure we get a unique function type, so we can give
3718 its pointer type a name. (This wins for gdb.) */
3719 tree vfunc_type = make_node (FUNCTION_TYPE);
3720 TREE_TYPE (vfunc_type) = integer_type_node;
3721 TYPE_ARG_TYPES (vfunc_type) = NULL_TREE;
3722 layout_type (vfunc_type);
3723
3724 vtable_entry_type = build_pointer_type (vfunc_type);
3725 }
3726 record_builtin_type (RID_MAX, VTBL_PTR_TYPE, vtable_entry_type);
3727
3728 vtbl_type_node
3729 = build_cplus_array_type (vtable_entry_type, NULL_TREE);
3730 layout_type (vtbl_type_node);
3731 vtbl_type_node = cp_build_qualified_type (vtbl_type_node, TYPE_QUAL_CONST);
3732 record_builtin_type (RID_MAX, NULL, vtbl_type_node);
3733 vtbl_ptr_type_node = build_pointer_type (vtable_entry_type);
3734 layout_type (vtbl_ptr_type_node);
3735 record_builtin_type (RID_MAX, NULL, vtbl_ptr_type_node);
3736
3737 push_namespace (get_identifier ("__cxxabiv1"));
3738 abi_node = current_namespace;
3739 pop_namespace ();
3740
3741 global_type_node = make_node (LANG_TYPE);
3742 record_unknown_type (global_type_node, "global type");
3743
3744 /* Now, C++. */
3745 current_lang_name = lang_name_cplusplus;
3746
3747 {
3748 tree newattrs, extvisattr;
3749 tree newtype, deltype;
3750 tree ptr_ftype_sizetype;
3751 tree new_eh_spec;
3752
3753 ptr_ftype_sizetype
3754 = build_function_type_list (ptr_type_node, size_type_node, NULL_TREE);
3755 if (cxx_dialect == cxx98)
3756 {
3757 tree bad_alloc_id;
3758 tree bad_alloc_type_node;
3759 tree bad_alloc_decl;
3760
3761 push_namespace (std_identifier);
3762 bad_alloc_id = get_identifier ("bad_alloc");
3763 bad_alloc_type_node = make_class_type (RECORD_TYPE);
3764 TYPE_CONTEXT (bad_alloc_type_node) = current_namespace;
3765 bad_alloc_decl
3766 = create_implicit_typedef (bad_alloc_id, bad_alloc_type_node);
3767 DECL_CONTEXT (bad_alloc_decl) = current_namespace;
3768 pop_namespace ();
3769
3770 new_eh_spec
3771 = add_exception_specifier (NULL_TREE, bad_alloc_type_node, -1);
3772 }
3773 else
3774 new_eh_spec = noexcept_false_spec;
3775
3776 /* Ensure attribs.c is initialized. */
3777 init_attributes ();
3778 extvisattr = build_tree_list (get_identifier ("externally_visible"),
3779 NULL_TREE);
3780 newattrs = tree_cons (get_identifier ("alloc_size"),
3781 build_tree_list (NULL_TREE, integer_one_node),
3782 extvisattr);
3783 newtype = cp_build_type_attribute_variant (ptr_ftype_sizetype, newattrs);
3784 newtype = build_exception_variant (newtype, new_eh_spec);
3785 deltype = cp_build_type_attribute_variant (void_ftype_ptr, extvisattr);
3786 deltype = build_exception_variant (deltype, empty_except_spec);
3787 push_cp_library_fn (NEW_EXPR, newtype);
3788 push_cp_library_fn (VEC_NEW_EXPR, newtype);
3789 global_delete_fndecl = push_cp_library_fn (DELETE_EXPR, deltype);
3790 push_cp_library_fn (VEC_DELETE_EXPR, deltype);
3791
3792 nullptr_type_node = make_node (NULLPTR_TYPE);
3793 TYPE_SIZE (nullptr_type_node) = bitsize_int (GET_MODE_BITSIZE (ptr_mode));
3794 TYPE_SIZE_UNIT (nullptr_type_node) = size_int (GET_MODE_SIZE (ptr_mode));
3795 TYPE_UNSIGNED (nullptr_type_node) = 1;
3796 TYPE_PRECISION (nullptr_type_node) = GET_MODE_BITSIZE (ptr_mode);
3797 SET_TYPE_MODE (nullptr_type_node, ptr_mode);
3798 record_builtin_type (RID_MAX, "decltype(nullptr)", nullptr_type_node);
3799 nullptr_node = build_int_cst (nullptr_type_node, 0);
3800 }
3801
3802 abort_fndecl
3803 = build_library_fn_ptr ("__cxa_pure_virtual", void_ftype);
3804
3805 /* Perform other language dependent initializations. */
3806 init_class_processing ();
3807 init_rtti_processing ();
3808 init_template_processing ();
3809
3810 if (flag_exceptions)
3811 init_exception_processing ();
3812
3813 if (! supports_one_only ())
3814 flag_weak = 0;
3815
3816 make_fname_decl = cp_make_fname_decl;
3817 start_fname_decls ();
3818
3819 /* Show we use EH for cleanups. */
3820 if (flag_exceptions)
3821 using_eh_for_cleanups ();
3822 }
3823
3824 /* Generate an initializer for a function naming variable from
3825 NAME. NAME may be NULL, to indicate a dependent name. TYPE_P is
3826 filled in with the type of the init. */
3827
3828 tree
3829 cp_fname_init (const char* name, tree *type_p)
3830 {
3831 tree domain = NULL_TREE;
3832 tree type;
3833 tree init = NULL_TREE;
3834 size_t length = 0;
3835
3836 if (name)
3837 {
3838 length = strlen (name);
3839 domain = build_index_type (size_int (length));
3840 init = build_string (length + 1, name);
3841 }
3842
3843 type = cp_build_qualified_type (char_type_node, TYPE_QUAL_CONST);
3844 type = build_cplus_array_type (type, domain);
3845
3846 *type_p = type;
3847
3848 if (init)
3849 TREE_TYPE (init) = type;
3850 else
3851 init = error_mark_node;
3852
3853 return init;
3854 }
3855
3856 /* Create the VAR_DECL for __FUNCTION__ etc. ID is the name to give
3857 the decl, LOC is the location to give the decl, NAME is the
3858 initialization string and TYPE_DEP indicates whether NAME depended
3859 on the type of the function. We make use of that to detect
3860 __PRETTY_FUNCTION__ inside a template fn. This is being done lazily
3861 at the point of first use, so we mustn't push the decl now. */
3862
3863 static tree
3864 cp_make_fname_decl (location_t loc, tree id, int type_dep)
3865 {
3866 const char *const name = (type_dep && processing_template_decl
3867 ? NULL : fname_as_string (type_dep));
3868 tree type;
3869 tree init = cp_fname_init (name, &type);
3870 tree decl = build_decl (loc, VAR_DECL, id, type);
3871
3872 if (name)
3873 free (CONST_CAST (char *, name));
3874
3875 /* As we're using pushdecl_with_scope, we must set the context. */
3876 DECL_CONTEXT (decl) = current_function_decl;
3877
3878 TREE_STATIC (decl) = 1;
3879 TREE_READONLY (decl) = 1;
3880 DECL_ARTIFICIAL (decl) = 1;
3881
3882 TREE_USED (decl) = 1;
3883
3884 if (current_function_decl)
3885 {
3886 cp_binding_level *b = current_binding_level;
3887 if (b->kind == sk_function_parms)
3888 return error_mark_node;
3889 while (b->level_chain->kind != sk_function_parms)
3890 b = b->level_chain;
3891 pushdecl_with_scope (decl, b, /*is_friend=*/false);
3892 cp_finish_decl (decl, init, /*init_const_expr_p=*/false, NULL_TREE,
3893 LOOKUP_ONLYCONVERTING);
3894 }
3895 else
3896 {
3897 DECL_THIS_STATIC (decl) = true;
3898 pushdecl_top_level_and_finish (decl, init);
3899 }
3900
3901 return decl;
3902 }
3903
3904 static tree
3905 builtin_function_1 (tree decl, tree context, bool is_global)
3906 {
3907 tree id = DECL_NAME (decl);
3908 const char *name = IDENTIFIER_POINTER (id);
3909
3910 retrofit_lang_decl (decl);
3911
3912 DECL_ARTIFICIAL (decl) = 1;
3913 SET_OVERLOADED_OPERATOR_CODE (decl, ERROR_MARK);
3914 SET_DECL_LANGUAGE (decl, lang_c);
3915 /* Runtime library routines are, by definition, available in an
3916 external shared object. */
3917 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
3918 DECL_VISIBILITY_SPECIFIED (decl) = 1;
3919
3920 DECL_CONTEXT (decl) = context;
3921
3922 if (is_global)
3923 pushdecl_top_level (decl);
3924 else
3925 pushdecl (decl);
3926
3927 /* A function in the user's namespace should have an explicit
3928 declaration before it is used. Mark the built-in function as
3929 anticipated but not actually declared. */
3930 if (name[0] != '_' || name[1] != '_')
3931 DECL_ANTICIPATED (decl) = 1;
3932 else if (strncmp (name + 2, "builtin_", strlen ("builtin_")) != 0)
3933 {
3934 size_t len = strlen (name);
3935
3936 /* Treat __*_chk fortification functions as anticipated as well,
3937 unless they are __builtin_*. */
3938 if (len > strlen ("___chk")
3939 && memcmp (name + len - strlen ("_chk"),
3940 "_chk", strlen ("_chk") + 1) == 0)
3941 DECL_ANTICIPATED (decl) = 1;
3942 }
3943
3944 return decl;
3945 }
3946
3947 tree
3948 cxx_builtin_function (tree decl)
3949 {
3950 tree id = DECL_NAME (decl);
3951 const char *name = IDENTIFIER_POINTER (id);
3952 /* All builtins that don't begin with an '_' should additionally
3953 go in the 'std' namespace. */
3954 if (name[0] != '_')
3955 {
3956 tree decl2 = copy_node(decl);
3957 push_namespace (std_identifier);
3958 builtin_function_1 (decl2, std_node, false);
3959 pop_namespace ();
3960 }
3961
3962 return builtin_function_1 (decl, NULL_TREE, false);
3963 }
3964
3965 /* Like cxx_builtin_function, but guarantee the function is added to the global
3966 scope. This is to allow function specific options to add new machine
3967 dependent builtins when the target ISA changes via attribute((target(...)))
3968 which saves space on program startup if the program does not use non-generic
3969 ISAs. */
3970
3971 tree
3972 cxx_builtin_function_ext_scope (tree decl)
3973 {
3974
3975 tree id = DECL_NAME (decl);
3976 const char *name = IDENTIFIER_POINTER (id);
3977 /* All builtins that don't begin with an '_' should additionally
3978 go in the 'std' namespace. */
3979 if (name[0] != '_')
3980 {
3981 tree decl2 = copy_node(decl);
3982 push_namespace (std_identifier);
3983 builtin_function_1 (decl2, std_node, true);
3984 pop_namespace ();
3985 }
3986
3987 return builtin_function_1 (decl, NULL_TREE, true);
3988 }
3989
3990 /* Generate a FUNCTION_DECL with the typical flags for a runtime library
3991 function. Not called directly. */
3992
3993 static tree
3994 build_library_fn_1 (tree name, enum tree_code operator_code, tree type)
3995 {
3996 tree fn = build_lang_decl (FUNCTION_DECL, name, type);
3997 DECL_EXTERNAL (fn) = 1;
3998 TREE_PUBLIC (fn) = 1;
3999 DECL_ARTIFICIAL (fn) = 1;
4000 SET_OVERLOADED_OPERATOR_CODE (fn, operator_code);
4001 SET_DECL_LANGUAGE (fn, lang_c);
4002 /* Runtime library routines are, by definition, available in an
4003 external shared object. */
4004 DECL_VISIBILITY (fn) = VISIBILITY_DEFAULT;
4005 DECL_VISIBILITY_SPECIFIED (fn) = 1;
4006 return fn;
4007 }
4008
4009 /* Returns the _DECL for a library function with C linkage.
4010 We assume that such functions never throw; if this is incorrect,
4011 callers should unset TREE_NOTHROW. */
4012
4013 static tree
4014 build_library_fn (tree name, tree type)
4015 {
4016 tree fn = build_library_fn_1 (name, ERROR_MARK, type);
4017 TREE_NOTHROW (fn) = 1;
4018 return fn;
4019 }
4020
4021 /* Returns the _DECL for a library function with C++ linkage. */
4022
4023 static tree
4024 build_cp_library_fn (tree name, enum tree_code operator_code, tree type)
4025 {
4026 tree fn = build_library_fn_1 (name, operator_code, type);
4027 TREE_NOTHROW (fn) = TYPE_NOTHROW_P (type);
4028 DECL_CONTEXT (fn) = FROB_CONTEXT (current_namespace);
4029 SET_DECL_LANGUAGE (fn, lang_cplusplus);
4030 return fn;
4031 }
4032
4033 /* Like build_library_fn, but takes a C string instead of an
4034 IDENTIFIER_NODE. */
4035
4036 tree
4037 build_library_fn_ptr (const char* name, tree type)
4038 {
4039 return build_library_fn (get_identifier (name), type);
4040 }
4041
4042 /* Like build_cp_library_fn, but takes a C string instead of an
4043 IDENTIFIER_NODE. */
4044
4045 tree
4046 build_cp_library_fn_ptr (const char* name, tree type)
4047 {
4048 return build_cp_library_fn (get_identifier (name), ERROR_MARK, type);
4049 }
4050
4051 /* Like build_library_fn, but also pushes the function so that we will
4052 be able to find it via IDENTIFIER_GLOBAL_VALUE. Also, the function
4053 may throw exceptions listed in RAISES. */
4054
4055 tree
4056 push_library_fn (tree name, tree type, tree raises)
4057 {
4058 tree fn;
4059
4060 if (raises)
4061 type = build_exception_variant (type, raises);
4062
4063 fn = build_library_fn (name, type);
4064 pushdecl_top_level (fn);
4065 return fn;
4066 }
4067
4068 /* Like build_cp_library_fn, but also pushes the function so that it
4069 will be found by normal lookup. */
4070
4071 static tree
4072 push_cp_library_fn (enum tree_code operator_code, tree type)
4073 {
4074 tree fn = build_cp_library_fn (ansi_opname (operator_code),
4075 operator_code,
4076 type);
4077 pushdecl (fn);
4078 if (flag_tm)
4079 apply_tm_attr (fn, get_identifier ("transaction_safe"));
4080 return fn;
4081 }
4082
4083 /* Like push_library_fn, but takes a TREE_LIST of parm types rather than
4084 a FUNCTION_TYPE. */
4085
4086 tree
4087 push_void_library_fn (tree name, tree parmtypes)
4088 {
4089 tree type = build_function_type (void_type_node, parmtypes);
4090 return push_library_fn (name, type, NULL_TREE);
4091 }
4092
4093 /* Like push_library_fn, but also note that this function throws
4094 and does not return. Used for __throw_foo and the like. */
4095
4096 tree
4097 push_throw_library_fn (tree name, tree type)
4098 {
4099 tree fn = push_library_fn (name, type, NULL_TREE);
4100 TREE_THIS_VOLATILE (fn) = 1;
4101 TREE_NOTHROW (fn) = 0;
4102 return fn;
4103 }
4104 \f
4105 /* When we call finish_struct for an anonymous union, we create
4106 default copy constructors and such. But, an anonymous union
4107 shouldn't have such things; this function undoes the damage to the
4108 anonymous union type T.
4109
4110 (The reason that we create the synthesized methods is that we don't
4111 distinguish `union { int i; }' from `typedef union { int i; } U'.
4112 The first is an anonymous union; the second is just an ordinary
4113 union type.) */
4114
4115 void
4116 fixup_anonymous_aggr (tree t)
4117 {
4118 tree *q;
4119
4120 /* Wipe out memory of synthesized methods. */
4121 TYPE_HAS_USER_CONSTRUCTOR (t) = 0;
4122 TYPE_HAS_DEFAULT_CONSTRUCTOR (t) = 0;
4123 TYPE_HAS_COPY_CTOR (t) = 0;
4124 TYPE_HAS_CONST_COPY_CTOR (t) = 0;
4125 TYPE_HAS_COPY_ASSIGN (t) = 0;
4126 TYPE_HAS_CONST_COPY_ASSIGN (t) = 0;
4127
4128 /* Splice the implicitly generated functions out of the TYPE_METHODS
4129 list. */
4130 q = &TYPE_METHODS (t);
4131 while (*q)
4132 {
4133 if (DECL_ARTIFICIAL (*q))
4134 *q = TREE_CHAIN (*q);
4135 else
4136 q = &DECL_CHAIN (*q);
4137 }
4138
4139 /* ISO C++ 9.5.3. Anonymous unions may not have function members. */
4140 if (TYPE_METHODS (t))
4141 {
4142 tree decl = TYPE_MAIN_DECL (t);
4143
4144 if (TREE_CODE (t) != UNION_TYPE)
4145 error_at (DECL_SOURCE_LOCATION (decl),
4146 "an anonymous struct cannot have function members");
4147 else
4148 error_at (DECL_SOURCE_LOCATION (decl),
4149 "an anonymous union cannot have function members");
4150 }
4151
4152 /* Anonymous aggregates cannot have fields with ctors, dtors or complex
4153 assignment operators (because they cannot have these methods themselves).
4154 For anonymous unions this is already checked because they are not allowed
4155 in any union, otherwise we have to check it. */
4156 if (TREE_CODE (t) != UNION_TYPE)
4157 {
4158 tree field, type;
4159
4160 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
4161 if (TREE_CODE (field) == FIELD_DECL)
4162 {
4163 type = TREE_TYPE (field);
4164 if (CLASS_TYPE_P (type))
4165 {
4166 if (TYPE_NEEDS_CONSTRUCTING (type))
4167 error ("member %q+#D with constructor not allowed "
4168 "in anonymous aggregate", field);
4169 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
4170 error ("member %q+#D with destructor not allowed "
4171 "in anonymous aggregate", field);
4172 if (TYPE_HAS_COMPLEX_COPY_ASSIGN (type))
4173 error ("member %q+#D with copy assignment operator "
4174 "not allowed in anonymous aggregate", field);
4175 }
4176 }
4177 }
4178 }
4179
4180 /* Warn for an attribute located at LOCATION that appertains to the
4181 class type CLASS_TYPE that has not been properly placed after its
4182 class-key, in it class-specifier. */
4183
4184 void
4185 warn_misplaced_attr_for_class_type (source_location location,
4186 tree class_type)
4187 {
4188 gcc_assert (TAGGED_TYPE_P (class_type));
4189
4190 warning_at (location, OPT_Wattributes,
4191 "attribute ignored in declaration "
4192 "of %q#T", class_type);
4193 inform (location,
4194 "attribute for %q#T must follow the %qs keyword",
4195 class_type, class_key_or_enum_as_string (class_type));
4196 }
4197
4198 /* Make sure that a declaration with no declarator is well-formed, i.e.
4199 just declares a tagged type or anonymous union.
4200
4201 Returns the type declared; or NULL_TREE if none. */
4202
4203 tree
4204 check_tag_decl (cp_decl_specifier_seq *declspecs,
4205 bool explicit_type_instantiation_p)
4206 {
4207 int saw_friend = decl_spec_seq_has_spec_p (declspecs, ds_friend);
4208 int saw_typedef = decl_spec_seq_has_spec_p (declspecs, ds_typedef);
4209 /* If a class, struct, or enum type is declared by the DECLSPECS
4210 (i.e, if a class-specifier, enum-specifier, or non-typename
4211 elaborated-type-specifier appears in the DECLSPECS),
4212 DECLARED_TYPE is set to the corresponding type. */
4213 tree declared_type = NULL_TREE;
4214 bool error_p = false;
4215
4216 if (declspecs->multiple_types_p)
4217 error ("multiple types in one declaration");
4218 else if (declspecs->redefined_builtin_type)
4219 {
4220 if (!in_system_header)
4221 permerror (declspecs->locations[ds_redefined_builtin_type_spec],
4222 "redeclaration of C++ built-in type %qT",
4223 declspecs->redefined_builtin_type);
4224 return NULL_TREE;
4225 }
4226
4227 if (declspecs->type
4228 && TYPE_P (declspecs->type)
4229 && ((TREE_CODE (declspecs->type) != TYPENAME_TYPE
4230 && MAYBE_CLASS_TYPE_P (declspecs->type))
4231 || TREE_CODE (declspecs->type) == ENUMERAL_TYPE))
4232 declared_type = declspecs->type;
4233 else if (declspecs->type == error_mark_node)
4234 error_p = true;
4235 if (declared_type == NULL_TREE && ! saw_friend && !error_p)
4236 permerror (input_location, "declaration does not declare anything");
4237 else if (declared_type != NULL_TREE && type_uses_auto (declared_type))
4238 {
4239 error ("%<auto%> can only be specified for variables "
4240 "or function declarations");
4241 return error_mark_node;
4242 }
4243 /* Check for an anonymous union. */
4244 else if (declared_type && RECORD_OR_UNION_CODE_P (TREE_CODE (declared_type))
4245 && TYPE_ANONYMOUS_P (declared_type))
4246 {
4247 /* 7/3 In a simple-declaration, the optional init-declarator-list
4248 can be omitted only when declaring a class (clause 9) or
4249 enumeration (7.2), that is, when the decl-specifier-seq contains
4250 either a class-specifier, an elaborated-type-specifier with
4251 a class-key (9.1), or an enum-specifier. In these cases and
4252 whenever a class-specifier or enum-specifier is present in the
4253 decl-specifier-seq, the identifiers in these specifiers are among
4254 the names being declared by the declaration (as class-name,
4255 enum-names, or enumerators, depending on the syntax). In such
4256 cases, and except for the declaration of an unnamed bit-field (9.6),
4257 the decl-specifier-seq shall introduce one or more names into the
4258 program, or shall redeclare a name introduced by a previous
4259 declaration. [Example:
4260 enum { }; // ill-formed
4261 typedef class { }; // ill-formed
4262 --end example] */
4263 if (saw_typedef)
4264 {
4265 error ("missing type-name in typedef-declaration");
4266 return NULL_TREE;
4267 }
4268 /* Anonymous unions are objects, so they can have specifiers. */;
4269 SET_ANON_AGGR_TYPE_P (declared_type);
4270
4271 if (TREE_CODE (declared_type) != UNION_TYPE && !in_system_header)
4272 pedwarn (input_location, OPT_Wpedantic, "ISO C++ prohibits anonymous structs");
4273 }
4274
4275 else
4276 {
4277 if (decl_spec_seq_has_spec_p (declspecs, ds_inline)
4278 || decl_spec_seq_has_spec_p (declspecs, ds_virtual))
4279 error ("%qs can only be specified for functions",
4280 decl_spec_seq_has_spec_p (declspecs, ds_inline)
4281 ? "inline" : "virtual");
4282 else if (saw_friend
4283 && (!current_class_type
4284 || current_scope () != current_class_type))
4285 error ("%<friend%> can only be specified inside a class");
4286 else if (decl_spec_seq_has_spec_p (declspecs, ds_explicit))
4287 error ("%<explicit%> can only be specified for constructors");
4288 else if (declspecs->storage_class)
4289 error ("a storage class can only be specified for objects "
4290 "and functions");
4291 else if (decl_spec_seq_has_spec_p (declspecs, ds_const)
4292 || decl_spec_seq_has_spec_p (declspecs, ds_volatile)
4293 || decl_spec_seq_has_spec_p (declspecs, ds_restrict)
4294 || decl_spec_seq_has_spec_p (declspecs, ds_thread))
4295 error ("qualifiers can only be specified for objects "
4296 "and functions");
4297 else if (saw_typedef)
4298 warning (0, "%<typedef%> was ignored in this declaration");
4299 else if (decl_spec_seq_has_spec_p (declspecs, ds_constexpr))
4300 error ("%<constexpr%> cannot be used for type declarations");
4301 }
4302
4303 if (declspecs->attributes && warn_attributes && declared_type)
4304 {
4305 location_t loc;
4306 if (!CLASS_TYPE_P (declared_type)
4307 || !CLASSTYPE_TEMPLATE_INSTANTIATION (declared_type))
4308 /* For a non-template class, use the name location. */
4309 loc = location_of (declared_type);
4310 else
4311 /* For a template class (an explicit instantiation), use the
4312 current location. */
4313 loc = input_location;
4314
4315 if (explicit_type_instantiation_p)
4316 /* [dcl.attr.grammar]/4:
4317
4318 No attribute-specifier-seq shall appertain to an explicit
4319 instantiation. */
4320 {
4321 warning_at (loc, OPT_Wattributes,
4322 "attribute ignored in explicit instantiation %q#T",
4323 declared_type);
4324 inform (loc,
4325 "no attribute can be applied to "
4326 "an explicit instantiation");
4327 }
4328 else
4329 warn_misplaced_attr_for_class_type (loc, declared_type);
4330 }
4331
4332 return declared_type;
4333 }
4334
4335 /* Called when a declaration is seen that contains no names to declare.
4336 If its type is a reference to a structure, union or enum inherited
4337 from a containing scope, shadow that tag name for the current scope
4338 with a forward reference.
4339 If its type defines a new named structure or union
4340 or defines an enum, it is valid but we need not do anything here.
4341 Otherwise, it is an error.
4342
4343 C++: may have to grok the declspecs to learn about static,
4344 complain for anonymous unions.
4345
4346 Returns the TYPE declared -- or NULL_TREE if none. */
4347
4348 tree
4349 shadow_tag (cp_decl_specifier_seq *declspecs)
4350 {
4351 tree t = check_tag_decl (declspecs,
4352 /*explicit_type_instantiation_p=*/false);
4353
4354 if (!t)
4355 return NULL_TREE;
4356
4357 if (maybe_process_partial_specialization (t) == error_mark_node)
4358 return NULL_TREE;
4359
4360 /* This is where the variables in an anonymous union are
4361 declared. An anonymous union declaration looks like:
4362 union { ... } ;
4363 because there is no declarator after the union, the parser
4364 sends that declaration here. */
4365 if (ANON_AGGR_TYPE_P (t))
4366 {
4367 fixup_anonymous_aggr (t);
4368
4369 if (TYPE_FIELDS (t))
4370 {
4371 tree decl = grokdeclarator (/*declarator=*/NULL,
4372 declspecs, NORMAL, 0, NULL);
4373 finish_anon_union (decl);
4374 }
4375 }
4376
4377 return t;
4378 }
4379 \f
4380 /* Decode a "typename", such as "int **", returning a ..._TYPE node. */
4381
4382 tree
4383 groktypename (cp_decl_specifier_seq *type_specifiers,
4384 const cp_declarator *declarator,
4385 bool is_template_arg)
4386 {
4387 tree attrs;
4388 tree type;
4389 enum decl_context context
4390 = is_template_arg ? TEMPLATE_TYPE_ARG : TYPENAME;
4391 attrs = type_specifiers->attributes;
4392 type_specifiers->attributes = NULL_TREE;
4393 type = grokdeclarator (declarator, type_specifiers, context, 0, &attrs);
4394 if (attrs && type != error_mark_node)
4395 {
4396 if (CLASS_TYPE_P (type))
4397 warning (OPT_Wattributes, "ignoring attributes applied to class type %qT "
4398 "outside of definition", type);
4399 else if (MAYBE_CLASS_TYPE_P (type))
4400 /* A template type parameter or other dependent type. */
4401 warning (OPT_Wattributes, "ignoring attributes applied to dependent "
4402 "type %qT without an associated declaration", type);
4403 else
4404 cplus_decl_attributes (&type, attrs, 0);
4405 }
4406 return type;
4407 }
4408
4409 /* Process a DECLARATOR for a function-scope variable declaration,
4410 namespace-scope variable declaration, or function declaration.
4411 (Function definitions go through start_function; class member
4412 declarations appearing in the body of the class go through
4413 grokfield.) The DECL corresponding to the DECLARATOR is returned.
4414 If an error occurs, the error_mark_node is returned instead.
4415
4416 DECLSPECS are the decl-specifiers for the declaration. INITIALIZED is
4417 SD_INITIALIZED if an explicit initializer is present, or SD_DEFAULTED
4418 for an explicitly defaulted function, or SD_DELETED for an explicitly
4419 deleted function, but 0 (SD_UNINITIALIZED) if this is a variable
4420 implicitly initialized via a default constructor. ATTRIBUTES and
4421 PREFIX_ATTRIBUTES are GNU attributes associated with this declaration.
4422
4423 The scope represented by the context of the returned DECL is pushed
4424 (if it is not the global namespace) and is assigned to
4425 *PUSHED_SCOPE_P. The caller is then responsible for calling
4426 pop_scope on *PUSHED_SCOPE_P if it is set. */
4427
4428 tree
4429 start_decl (const cp_declarator *declarator,
4430 cp_decl_specifier_seq *declspecs,
4431 int initialized,
4432 tree attributes,
4433 tree prefix_attributes,
4434 tree *pushed_scope_p)
4435 {
4436 tree decl;
4437 tree context;
4438 bool was_public;
4439 int flags;
4440 bool alias;
4441
4442 *pushed_scope_p = NULL_TREE;
4443
4444 /* An object declared as __attribute__((deprecated)) suppresses
4445 warnings of uses of other deprecated items. */
4446 if (lookup_attribute ("deprecated", attributes))
4447 deprecated_state = DEPRECATED_SUPPRESS;
4448
4449 attributes = chainon (attributes, prefix_attributes);
4450
4451 decl = grokdeclarator (declarator, declspecs, NORMAL, initialized,
4452 &attributes);
4453
4454 deprecated_state = DEPRECATED_NORMAL;
4455
4456 if (decl == NULL_TREE || TREE_CODE (decl) == VOID_TYPE
4457 || decl == error_mark_node)
4458 return error_mark_node;
4459
4460 context = CP_DECL_CONTEXT (decl);
4461 if (context != global_namespace)
4462 *pushed_scope_p = push_scope (context);
4463
4464 if (initialized)
4465 /* Is it valid for this decl to have an initializer at all?
4466 If not, set INITIALIZED to zero, which will indirectly
4467 tell `cp_finish_decl' to ignore the initializer once it is parsed. */
4468 switch (TREE_CODE (decl))
4469 {
4470 case TYPE_DECL:
4471 error ("typedef %qD is initialized (use decltype instead)", decl);
4472 return error_mark_node;
4473
4474 case FUNCTION_DECL:
4475 if (initialized == SD_DELETED)
4476 /* We'll handle the rest of the semantics later, but we need to
4477 set this now so it's visible to duplicate_decls. */
4478 DECL_DELETED_FN (decl) = 1;
4479 break;
4480
4481 default:
4482 break;
4483 }
4484
4485 if (initialized)
4486 {
4487 if (! toplevel_bindings_p ()
4488 && DECL_EXTERNAL (decl))
4489 warning (0, "declaration of %q#D has %<extern%> and is initialized",
4490 decl);
4491 DECL_EXTERNAL (decl) = 0;
4492 if (toplevel_bindings_p ())
4493 TREE_STATIC (decl) = 1;
4494 }
4495 alias = lookup_attribute ("alias", DECL_ATTRIBUTES (decl)) != 0;
4496
4497 if (alias && TREE_CODE (decl) == FUNCTION_DECL)
4498 record_key_method_defined (decl);
4499
4500 /* If this is a typedef that names the class for linkage purposes
4501 (7.1.3p8), apply any attributes directly to the type. */
4502 if (TREE_CODE (decl) == TYPE_DECL
4503 && TAGGED_TYPE_P (TREE_TYPE (decl))
4504 && decl == TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (decl))))
4505 flags = ATTR_FLAG_TYPE_IN_PLACE;
4506 else
4507 flags = 0;
4508
4509 /* Set attributes here so if duplicate decl, will have proper attributes. */
4510 cplus_decl_attributes (&decl, attributes, flags);
4511
4512 /* Dllimported symbols cannot be defined. Static data members (which
4513 can be initialized in-class and dllimported) go through grokfield,
4514 not here, so we don't need to exclude those decls when checking for
4515 a definition. */
4516 if (initialized && DECL_DLLIMPORT_P (decl))
4517 {
4518 error ("definition of %q#D is marked %<dllimport%>", decl);
4519 DECL_DLLIMPORT_P (decl) = 0;
4520 }
4521
4522 /* If #pragma weak was used, mark the decl weak now. */
4523 if (!processing_template_decl)
4524 maybe_apply_pragma_weak (decl);
4525
4526 if (TREE_CODE (decl) == FUNCTION_DECL
4527 && DECL_DECLARED_INLINE_P (decl)
4528 && DECL_UNINLINABLE (decl)
4529 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
4530 warning (0, "inline function %q+D given attribute noinline", decl);
4531
4532 if (TYPE_P (context) && COMPLETE_TYPE_P (complete_type (context)))
4533 {
4534 if (TREE_CODE (decl) == VAR_DECL)
4535 {
4536 tree field = lookup_field (context, DECL_NAME (decl), 0, false);
4537 if (field == NULL_TREE || TREE_CODE (field) != VAR_DECL)
4538 error ("%q#D is not a static member of %q#T", decl, context);
4539 else
4540 {
4541 if (DECL_CONTEXT (field) != context)
4542 {
4543 if (!same_type_p (DECL_CONTEXT (field), context))
4544 permerror (input_location, "ISO C++ does not permit %<%T::%D%> "
4545 "to be defined as %<%T::%D%>",
4546 DECL_CONTEXT (field), DECL_NAME (decl),
4547 context, DECL_NAME (decl));
4548 DECL_CONTEXT (decl) = DECL_CONTEXT (field);
4549 }
4550 /* Static data member are tricky; an in-class initialization
4551 still doesn't provide a definition, so the in-class
4552 declaration will have DECL_EXTERNAL set, but will have an
4553 initialization. Thus, duplicate_decls won't warn
4554 about this situation, and so we check here. */
4555 if (initialized && DECL_INITIALIZED_IN_CLASS_P (field))
4556 error ("duplicate initialization of %qD", decl);
4557 if (duplicate_decls (decl, field, /*newdecl_is_friend=*/false))
4558 decl = field;
4559 if (decl_spec_seq_has_spec_p (declspecs, ds_constexpr)
4560 && !DECL_DECLARED_CONSTEXPR_P (field))
4561 error ("%qD declared %<constexpr%> outside its class", field);
4562 }
4563 }
4564 else
4565 {
4566 tree field = check_classfn (context, decl,
4567 (processing_template_decl
4568 > template_class_depth (context))
4569 ? current_template_parms
4570 : NULL_TREE);
4571 if (field && field != error_mark_node
4572 && duplicate_decls (decl, field,
4573 /*newdecl_is_friend=*/false))
4574 decl = field;
4575 }
4576
4577 /* cp_finish_decl sets DECL_EXTERNAL if DECL_IN_AGGR_P is set. */
4578 DECL_IN_AGGR_P (decl) = 0;
4579 /* Do not mark DECL as an explicit specialization if it was not
4580 already marked as an instantiation; a declaration should
4581 never be marked as a specialization unless we know what
4582 template is being specialized. */
4583 if (DECL_LANG_SPECIFIC (decl) && DECL_USE_TEMPLATE (decl))
4584 {
4585 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
4586
4587 /* [temp.expl.spec] An explicit specialization of a static data
4588 member of a template is a definition if the declaration
4589 includes an initializer; otherwise, it is a declaration.
4590
4591 We check for processing_specialization so this only applies
4592 to the new specialization syntax. */
4593 if (!initialized && processing_specialization)
4594 DECL_EXTERNAL (decl) = 1;
4595 }
4596
4597 if (DECL_EXTERNAL (decl) && ! DECL_TEMPLATE_SPECIALIZATION (decl)
4598 /* Aliases are definitions. */
4599 && !alias)
4600 permerror (input_location, "declaration of %q#D outside of class is not definition",
4601 decl);
4602 }
4603
4604 was_public = TREE_PUBLIC (decl);
4605
4606 /* Enter this declaration into the symbol table. */
4607 decl = maybe_push_decl (decl);
4608
4609 if (processing_template_decl)
4610 decl = push_template_decl (decl);
4611 if (decl == error_mark_node)
4612 return error_mark_node;
4613
4614 if (TREE_CODE (decl) == VAR_DECL
4615 && DECL_NAMESPACE_SCOPE_P (decl) && !TREE_PUBLIC (decl) && !was_public
4616 && !DECL_THIS_STATIC (decl) && !DECL_ARTIFICIAL (decl))
4617 {
4618 /* This is a const variable with implicit 'static'. Set
4619 DECL_THIS_STATIC so we can tell it from variables that are
4620 !TREE_PUBLIC because of the anonymous namespace. */
4621 gcc_assert (CP_TYPE_CONST_P (TREE_TYPE (decl)) || errorcount);
4622 DECL_THIS_STATIC (decl) = 1;
4623 }
4624
4625 if (!processing_template_decl && TREE_CODE (decl) == VAR_DECL)
4626 start_decl_1 (decl, initialized);
4627
4628 return decl;
4629 }
4630
4631 /* Process the declaration of a variable DECL. INITIALIZED is true
4632 iff DECL is explicitly initialized. (INITIALIZED is false if the
4633 variable is initialized via an implicitly-called constructor.)
4634 This function must be called for ordinary variables (including, for
4635 example, implicit instantiations of templates), but must not be
4636 called for template declarations. */
4637
4638 void
4639 start_decl_1 (tree decl, bool initialized)
4640 {
4641 tree type;
4642 bool complete_p;
4643 bool aggregate_definition_p;
4644
4645 gcc_assert (!processing_template_decl);
4646
4647 if (error_operand_p (decl))
4648 return;
4649
4650 gcc_assert (TREE_CODE (decl) == VAR_DECL);
4651
4652 type = TREE_TYPE (decl);
4653 complete_p = COMPLETE_TYPE_P (type);
4654 aggregate_definition_p = MAYBE_CLASS_TYPE_P (type) && !DECL_EXTERNAL (decl);
4655
4656 /* If an explicit initializer is present, or if this is a definition
4657 of an aggregate, then we need a complete type at this point.
4658 (Scalars are always complete types, so there is nothing to
4659 check.) This code just sets COMPLETE_P; errors (if necessary)
4660 are issued below. */
4661 if ((initialized || aggregate_definition_p)
4662 && !complete_p
4663 && COMPLETE_TYPE_P (complete_type (type)))
4664 {
4665 complete_p = true;
4666 /* We will not yet have set TREE_READONLY on DECL if the type
4667 was "const", but incomplete, before this point. But, now, we
4668 have a complete type, so we can try again. */
4669 cp_apply_type_quals_to_decl (cp_type_quals (type), decl);
4670 }
4671
4672 if (initialized)
4673 /* Is it valid for this decl to have an initializer at all? */
4674 {
4675 /* Don't allow initializations for incomplete types except for
4676 arrays which might be completed by the initialization. */
4677 if (complete_p)
4678 ; /* A complete type is ok. */
4679 else if (type_uses_auto (type))
4680 ; /* An auto type is ok. */
4681 else if (TREE_CODE (type) != ARRAY_TYPE)
4682 {
4683 error ("variable %q#D has initializer but incomplete type", decl);
4684 type = TREE_TYPE (decl) = error_mark_node;
4685 }
4686 else if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (type))))
4687 {
4688 if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
4689 error ("elements of array %q#D have incomplete type", decl);
4690 /* else we already gave an error in start_decl. */
4691 }
4692 }
4693 else if (aggregate_definition_p && !complete_p)
4694 {
4695 if (type_uses_auto (type))
4696 error ("declaration of %q#D has no initializer", decl);
4697 else
4698 error ("aggregate %q#D has incomplete type and cannot be defined",
4699 decl);
4700 /* Change the type so that assemble_variable will give
4701 DECL an rtl we can live with: (mem (const_int 0)). */
4702 type = TREE_TYPE (decl) = error_mark_node;
4703 }
4704
4705 /* Create a new scope to hold this declaration if necessary.
4706 Whether or not a new scope is necessary cannot be determined
4707 until after the type has been completed; if the type is a
4708 specialization of a class template it is not until after
4709 instantiation has occurred that TYPE_HAS_NONTRIVIAL_DESTRUCTOR
4710 will be set correctly. */
4711 maybe_push_cleanup_level (type);
4712 }
4713
4714 /* Handle initialization of references. DECL, TYPE, and INIT have the
4715 same meaning as in cp_finish_decl. *CLEANUP must be NULL on entry,
4716 but will be set to a new CLEANUP_STMT if a temporary is created
4717 that must be destroyed subsequently.
4718
4719 Returns an initializer expression to use to initialize DECL, or
4720 NULL if the initialization can be performed statically.
4721
4722 Quotes on semantics can be found in ARM 8.4.3. */
4723
4724 static tree
4725 grok_reference_init (tree decl, tree type, tree init, int flags)
4726 {
4727 if (init == NULL_TREE)
4728 {
4729 if ((DECL_LANG_SPECIFIC (decl) == 0
4730 || DECL_IN_AGGR_P (decl) == 0)
4731 && ! DECL_THIS_EXTERN (decl))
4732 error ("%qD declared as reference but not initialized", decl);
4733 return NULL_TREE;
4734 }
4735
4736 if (TREE_CODE (init) == TREE_LIST)
4737 init = build_x_compound_expr_from_list (init, ELK_INIT,
4738 tf_warning_or_error);
4739
4740 if (TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE
4741 && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE)
4742 /* Note: default conversion is only called in very special cases. */
4743 init = decay_conversion (init, tf_warning_or_error);
4744
4745 /* Convert INIT to the reference type TYPE. This may involve the
4746 creation of a temporary, whose lifetime must be the same as that
4747 of the reference. If so, a DECL_EXPR for the temporary will be
4748 added just after the DECL_EXPR for DECL. That's why we don't set
4749 DECL_INITIAL for local references (instead assigning to them
4750 explicitly); we need to allow the temporary to be initialized
4751 first. */
4752 return initialize_reference (type, init, flags,
4753 tf_warning_or_error);
4754 }
4755
4756 /* Designated initializers in arrays are not supported in GNU C++.
4757 The parser cannot detect this error since it does not know whether
4758 a given brace-enclosed initializer is for a class type or for an
4759 array. This function checks that CE does not use a designated
4760 initializer. If it does, an error is issued. Returns true if CE
4761 is valid, i.e., does not have a designated initializer. */
4762
4763 static bool
4764 check_array_designated_initializer (const constructor_elt *ce,
4765 unsigned HOST_WIDE_INT index)
4766 {
4767 /* Designated initializers for array elements are not supported. */
4768 if (ce->index)
4769 {
4770 /* The parser only allows identifiers as designated
4771 initializers. */
4772 if (ce->index == error_mark_node)
4773 error ("name used in a GNU-style designated "
4774 "initializer for an array");
4775 else if (TREE_CODE (ce->index) == INTEGER_CST)
4776 {
4777 /* A C99 designator is OK if it matches the current index. */
4778 if (TREE_INT_CST_LOW (ce->index) == index)
4779 return true;
4780 else
4781 sorry ("non-trivial designated initializers not supported");
4782 }
4783 else
4784 {
4785 gcc_assert (TREE_CODE (ce->index) == IDENTIFIER_NODE);
4786 error ("name %qD used in a GNU-style designated "
4787 "initializer for an array", ce->index);
4788 }
4789 return false;
4790 }
4791
4792 return true;
4793 }
4794
4795 /* When parsing `int a[] = {1, 2};' we don't know the size of the
4796 array until we finish parsing the initializer. If that's the
4797 situation we're in, update DECL accordingly. */
4798
4799 static void
4800 maybe_deduce_size_from_array_init (tree decl, tree init)
4801 {
4802 tree type = TREE_TYPE (decl);
4803
4804 if (TREE_CODE (type) == ARRAY_TYPE
4805 && TYPE_DOMAIN (type) == NULL_TREE
4806 && TREE_CODE (decl) != TYPE_DECL)
4807 {
4808 /* do_default is really a C-ism to deal with tentative definitions.
4809 But let's leave it here to ease the eventual merge. */
4810 int do_default = !DECL_EXTERNAL (decl);
4811 tree initializer = init ? init : DECL_INITIAL (decl);
4812 int failure = 0;
4813
4814 /* Check that there are no designated initializers in INIT, as
4815 those are not supported in GNU C++, and as the middle-end
4816 will crash if presented with a non-numeric designated
4817 initializer. */
4818 if (initializer && TREE_CODE (initializer) == CONSTRUCTOR)
4819 {
4820 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (initializer);
4821 constructor_elt *ce;
4822 HOST_WIDE_INT i;
4823 FOR_EACH_VEC_SAFE_ELT (v, i, ce)
4824 if (!check_array_designated_initializer (ce, i))
4825 failure = 1;
4826 }
4827
4828 if (!failure)
4829 {
4830 failure = cp_complete_array_type (&TREE_TYPE (decl), initializer,
4831 do_default);
4832 if (failure == 1)
4833 {
4834 error ("initializer fails to determine size of %qD", decl);
4835 }
4836 else if (failure == 2)
4837 {
4838 if (do_default)
4839 {
4840 error ("array size missing in %qD", decl);
4841 }
4842 /* If a `static' var's size isn't known, make it extern as
4843 well as static, so it does not get allocated. If it's not
4844 `static', then don't mark it extern; finish_incomplete_decl
4845 will give it a default size and it will get allocated. */
4846 else if (!pedantic && TREE_STATIC (decl) && !TREE_PUBLIC (decl))
4847 DECL_EXTERNAL (decl) = 1;
4848 }
4849 else if (failure == 3)
4850 {
4851 error ("zero-size array %qD", decl);
4852 }
4853 }
4854
4855 cp_apply_type_quals_to_decl (cp_type_quals (TREE_TYPE (decl)), decl);
4856
4857 relayout_decl (decl);
4858 }
4859 }
4860
4861 /* Set DECL_SIZE, DECL_ALIGN, etc. for DECL (a VAR_DECL), and issue
4862 any appropriate error messages regarding the layout. */
4863
4864 static void
4865 layout_var_decl (tree decl)
4866 {
4867 tree type;
4868
4869 type = TREE_TYPE (decl);
4870 if (type == error_mark_node)
4871 return;
4872
4873 /* If we haven't already layed out this declaration, do so now.
4874 Note that we must not call complete type for an external object
4875 because it's type might involve templates that we are not
4876 supposed to instantiate yet. (And it's perfectly valid to say
4877 `extern X x' for some incomplete type `X'.) */
4878 if (!DECL_EXTERNAL (decl))
4879 complete_type (type);
4880 if (!DECL_SIZE (decl)
4881 && TREE_TYPE (decl) != error_mark_node
4882 && (COMPLETE_TYPE_P (type)
4883 || (TREE_CODE (type) == ARRAY_TYPE
4884 && !TYPE_DOMAIN (type)
4885 && COMPLETE_TYPE_P (TREE_TYPE (type)))))
4886 layout_decl (decl, 0);
4887
4888 if (!DECL_EXTERNAL (decl) && DECL_SIZE (decl) == NULL_TREE)
4889 {
4890 /* An automatic variable with an incomplete type: that is an error.
4891 Don't talk about array types here, since we took care of that
4892 message in grokdeclarator. */
4893 error ("storage size of %qD isn%'t known", decl);
4894 TREE_TYPE (decl) = error_mark_node;
4895 }
4896 #if 0
4897 /* Keep this code around in case we later want to control debug info
4898 based on whether a type is "used". (jason 1999-11-11) */
4899
4900 else if (!DECL_EXTERNAL (decl) && MAYBE_CLASS_TYPE_P (ttype))
4901 /* Let debugger know it should output info for this type. */
4902 note_debug_info_needed (ttype);
4903
4904 if (TREE_STATIC (decl) && DECL_CLASS_SCOPE_P (decl))
4905 note_debug_info_needed (DECL_CONTEXT (decl));
4906 #endif
4907
4908 if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl))
4909 && DECL_SIZE (decl) != NULL_TREE
4910 && ! TREE_CONSTANT (DECL_SIZE (decl)))
4911 {
4912 if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
4913 constant_expression_warning (DECL_SIZE (decl));
4914 else
4915 {
4916 error ("storage size of %qD isn%'t constant", decl);
4917 TREE_TYPE (decl) = error_mark_node;
4918 }
4919 }
4920 }
4921
4922 /* If a local static variable is declared in an inline function, or if
4923 we have a weak definition, we must endeavor to create only one
4924 instance of the variable at link-time. */
4925
4926 void
4927 maybe_commonize_var (tree decl)
4928 {
4929 /* Static data in a function with comdat linkage also has comdat
4930 linkage. */
4931 if (TREE_STATIC (decl)
4932 /* Don't mess with __FUNCTION__. */
4933 && ! DECL_ARTIFICIAL (decl)
4934 && DECL_FUNCTION_SCOPE_P (decl)
4935 && vague_linkage_p (DECL_CONTEXT (decl)))
4936 {
4937 if (flag_weak)
4938 {
4939 /* With weak symbols, we simply make the variable COMDAT;
4940 that will cause copies in multiple translations units to
4941 be merged. */
4942 comdat_linkage (decl);
4943 }
4944 else
4945 {
4946 if (DECL_INITIAL (decl) == NULL_TREE
4947 || DECL_INITIAL (decl) == error_mark_node)
4948 {
4949 /* Without weak symbols, we can use COMMON to merge
4950 uninitialized variables. */
4951 TREE_PUBLIC (decl) = 1;
4952 DECL_COMMON (decl) = 1;
4953 }
4954 else
4955 {
4956 /* While for initialized variables, we must use internal
4957 linkage -- which means that multiple copies will not
4958 be merged. */
4959 TREE_PUBLIC (decl) = 0;
4960 DECL_COMMON (decl) = 0;
4961 warning_at (input_location, 0,
4962 "sorry: semantics of inline function static "
4963 "data %q+#D are wrong (you%'ll wind up "
4964 "with multiple copies)", decl);
4965 warning_at (DECL_SOURCE_LOCATION (decl), 0,
4966 " you can work around this by removing "
4967 "the initializer");
4968 }
4969 }
4970 }
4971 else if (DECL_LANG_SPECIFIC (decl) && DECL_COMDAT (decl))
4972 /* Set it up again; we might have set DECL_INITIAL since the last
4973 time. */
4974 comdat_linkage (decl);
4975 }
4976
4977 /* Issue an error message if DECL is an uninitialized const variable. */
4978
4979 static void
4980 check_for_uninitialized_const_var (tree decl)
4981 {
4982 tree type = strip_array_types (TREE_TYPE (decl));
4983
4984 /* ``Unless explicitly declared extern, a const object does not have
4985 external linkage and must be initialized. ($8.4; $12.1)'' ARM
4986 7.1.6 */
4987 if (TREE_CODE (decl) == VAR_DECL
4988 && TREE_CODE (type) != REFERENCE_TYPE
4989 && CP_TYPE_CONST_P (type)
4990 && !DECL_INITIAL (decl))
4991 {
4992 tree field = default_init_uninitialized_part (type);
4993 if (!field)
4994 return;
4995
4996 permerror (DECL_SOURCE_LOCATION (decl),
4997 "uninitialized const %qD", decl);
4998
4999 if (CLASS_TYPE_P (type))
5000 {
5001 tree defaulted_ctor;
5002
5003 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
5004 "%q#T has no user-provided default constructor", type);
5005 defaulted_ctor = in_class_defaulted_default_constructor (type);
5006 if (defaulted_ctor)
5007 inform (DECL_SOURCE_LOCATION (defaulted_ctor),
5008 "constructor is not user-provided because it is "
5009 "explicitly defaulted in the class body");
5010 inform (0, "and the implicitly-defined constructor does not "
5011 "initialize %q+#D", field);
5012 }
5013 }
5014 }
5015 \f
5016 /* Structure holding the current initializer being processed by reshape_init.
5017 CUR is a pointer to the current element being processed, END is a pointer
5018 after the last element present in the initializer. */
5019 typedef struct reshape_iterator_t
5020 {
5021 constructor_elt *cur;
5022 constructor_elt *end;
5023 } reshape_iter;
5024
5025 static tree reshape_init_r (tree, reshape_iter *, bool, tsubst_flags_t);
5026
5027 /* FIELD is a FIELD_DECL or NULL. In the former case, the value
5028 returned is the next FIELD_DECL (possibly FIELD itself) that can be
5029 initialized. If there are no more such fields, the return value
5030 will be NULL. */
5031
5032 tree
5033 next_initializable_field (tree field)
5034 {
5035 while (field
5036 && (TREE_CODE (field) != FIELD_DECL
5037 || (DECL_C_BIT_FIELD (field) && !DECL_NAME (field))
5038 || DECL_ARTIFICIAL (field)))
5039 field = DECL_CHAIN (field);
5040
5041 return field;
5042 }
5043
5044 /* Subroutine of reshape_init_array and reshape_init_vector, which does
5045 the actual work. ELT_TYPE is the element type of the array. MAX_INDEX is an
5046 INTEGER_CST representing the size of the array minus one (the maximum index),
5047 or NULL_TREE if the array was declared without specifying the size. D is
5048 the iterator within the constructor. */
5049
5050 static tree
5051 reshape_init_array_1 (tree elt_type, tree max_index, reshape_iter *d,
5052 tsubst_flags_t complain)
5053 {
5054 tree new_init;
5055 bool sized_array_p = (max_index != NULL_TREE);
5056 unsigned HOST_WIDE_INT max_index_cst = 0;
5057 unsigned HOST_WIDE_INT index;
5058
5059 /* The initializer for an array is always a CONSTRUCTOR. */
5060 new_init = build_constructor (init_list_type_node, NULL);
5061
5062 if (sized_array_p)
5063 {
5064 /* Minus 1 is used for zero sized arrays. */
5065 if (integer_all_onesp (max_index))
5066 return new_init;
5067
5068 if (host_integerp (max_index, 1))
5069 max_index_cst = tree_low_cst (max_index, 1);
5070 /* sizetype is sign extended, not zero extended. */
5071 else
5072 max_index_cst = tree_low_cst (fold_convert (size_type_node, max_index),
5073 1);
5074 }
5075
5076 /* Loop until there are no more initializers. */
5077 for (index = 0;
5078 d->cur != d->end && (!sized_array_p || index <= max_index_cst);
5079 ++index)
5080 {
5081 tree elt_init;
5082 constructor_elt *old_cur = d->cur;
5083
5084 check_array_designated_initializer (d->cur, index);
5085 elt_init = reshape_init_r (elt_type, d, /*first_initializer_p=*/false,
5086 complain);
5087 if (elt_init == error_mark_node)
5088 return error_mark_node;
5089 CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_init),
5090 size_int (index), elt_init);
5091 if (!TREE_CONSTANT (elt_init))
5092 TREE_CONSTANT (new_init) = false;
5093
5094 /* This can happen with an invalid initializer (c++/54501). */
5095 if (d->cur == old_cur && !sized_array_p)
5096 break;
5097 }
5098
5099 return new_init;
5100 }
5101
5102 /* Subroutine of reshape_init_r, processes the initializers for arrays.
5103 Parameters are the same of reshape_init_r. */
5104
5105 static tree
5106 reshape_init_array (tree type, reshape_iter *d, tsubst_flags_t complain)
5107 {
5108 tree max_index = NULL_TREE;
5109
5110 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
5111
5112 if (TYPE_DOMAIN (type))
5113 max_index = array_type_nelts (type);
5114
5115 return reshape_init_array_1 (TREE_TYPE (type), max_index, d, complain);
5116 }
5117
5118 /* Subroutine of reshape_init_r, processes the initializers for vectors.
5119 Parameters are the same of reshape_init_r. */
5120
5121 static tree
5122 reshape_init_vector (tree type, reshape_iter *d, tsubst_flags_t complain)
5123 {
5124 tree max_index = NULL_TREE;
5125
5126 gcc_assert (TREE_CODE (type) == VECTOR_TYPE);
5127
5128 if (COMPOUND_LITERAL_P (d->cur->value))
5129 {
5130 tree value = d->cur->value;
5131 if (!same_type_p (TREE_TYPE (value), type))
5132 {
5133 if (complain & tf_error)
5134 error ("invalid type %qT as initializer for a vector of type %qT",
5135 TREE_TYPE (d->cur->value), type);
5136 value = error_mark_node;
5137 }
5138 ++d->cur;
5139 return value;
5140 }
5141
5142 /* For a vector, we initialize it as an array of the appropriate size. */
5143 if (TREE_CODE (type) == VECTOR_TYPE)
5144 max_index = size_int (TYPE_VECTOR_SUBPARTS (type) - 1);
5145
5146 return reshape_init_array_1 (TREE_TYPE (type), max_index, d, complain);
5147 }
5148
5149 /* Subroutine of reshape_init_r, processes the initializers for classes
5150 or union. Parameters are the same of reshape_init_r. */
5151
5152 static tree
5153 reshape_init_class (tree type, reshape_iter *d, bool first_initializer_p,
5154 tsubst_flags_t complain)
5155 {
5156 tree field;
5157 tree new_init;
5158
5159 gcc_assert (CLASS_TYPE_P (type));
5160
5161 /* The initializer for a class is always a CONSTRUCTOR. */
5162 new_init = build_constructor (init_list_type_node, NULL);
5163 field = next_initializable_field (TYPE_FIELDS (type));
5164
5165 if (!field)
5166 {
5167 /* [dcl.init.aggr]
5168
5169 An initializer for an aggregate member that is an
5170 empty class shall have the form of an empty
5171 initializer-list {}. */
5172 if (!first_initializer_p)
5173 {
5174 if (complain & tf_error)
5175 error ("initializer for %qT must be brace-enclosed", type);
5176 return error_mark_node;
5177 }
5178 return new_init;
5179 }
5180
5181 /* Loop through the initializable fields, gathering initializers. */
5182 while (d->cur != d->end)
5183 {
5184 tree field_init;
5185 constructor_elt *old_cur = d->cur;
5186
5187 /* Handle designated initializers, as an extension. */
5188 if (d->cur->index)
5189 {
5190 if (TREE_CODE (d->cur->index) == INTEGER_CST)
5191 {
5192 if (complain & tf_error)
5193 error ("%<[%E] =%> used in a GNU-style designated initializer"
5194 " for class %qT", d->cur->index, type);
5195 return error_mark_node;
5196 }
5197
5198 if (TREE_CODE (d->cur->index) == FIELD_DECL)
5199 /* We already reshaped this. */
5200 gcc_assert (d->cur->index == field);
5201 else
5202 field = lookup_field_1 (type, d->cur->index, /*want_type=*/false);
5203
5204 if (!field || TREE_CODE (field) != FIELD_DECL)
5205 {
5206 if (complain & tf_error)
5207 error ("%qT has no non-static data member named %qD", type,
5208 d->cur->index);
5209 return error_mark_node;
5210 }
5211 }
5212
5213 /* If we processed all the member of the class, we are done. */
5214 if (!field)
5215 break;
5216
5217 field_init = reshape_init_r (TREE_TYPE (field), d,
5218 /*first_initializer_p=*/false, complain);
5219 if (field_init == error_mark_node)
5220 return error_mark_node;
5221
5222 if (d->cur == old_cur && d->cur->index)
5223 {
5224 /* This can happen with an invalid initializer for a flexible
5225 array member (c++/54441). */
5226 if (complain & tf_error)
5227 error ("invalid initializer for %q#D", field);
5228 return error_mark_node;
5229 }
5230
5231 CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_init), field, field_init);
5232
5233 /* [dcl.init.aggr]
5234
5235 When a union is initialized with a brace-enclosed
5236 initializer, the braces shall only contain an
5237 initializer for the first member of the union. */
5238 if (TREE_CODE (type) == UNION_TYPE)
5239 break;
5240
5241 field = next_initializable_field (DECL_CHAIN (field));
5242 }
5243
5244 return new_init;
5245 }
5246
5247 /* Subroutine of reshape_init_r. We're in a context where C99 initializer
5248 designators are not valid; either complain or return true to indicate
5249 that reshape_init_r should return error_mark_node. */
5250
5251 static bool
5252 has_designator_problem (reshape_iter *d, tsubst_flags_t complain)
5253 {
5254 if (d->cur->index)
5255 {
5256 if (complain & tf_error)
5257 error ("C99 designator %qE outside aggregate initializer",
5258 d->cur->index);
5259 else
5260 return true;
5261 }
5262 return false;
5263 }
5264
5265 /* Subroutine of reshape_init, which processes a single initializer (part of
5266 a CONSTRUCTOR). TYPE is the type of the variable being initialized, D is the
5267 iterator within the CONSTRUCTOR which points to the initializer to process.
5268 FIRST_INITIALIZER_P is true if this is the first initializer of the
5269 outermost CONSTRUCTOR node. */
5270
5271 static tree
5272 reshape_init_r (tree type, reshape_iter *d, bool first_initializer_p,
5273 tsubst_flags_t complain)
5274 {
5275 tree init = d->cur->value;
5276
5277 if (error_operand_p (init))
5278 return error_mark_node;
5279
5280 if (first_initializer_p && !CP_AGGREGATE_TYPE_P (type)
5281 && has_designator_problem (d, complain))
5282 return error_mark_node;
5283
5284 if (TREE_CODE (type) == COMPLEX_TYPE)
5285 {
5286 /* A complex type can be initialized from one or two initializers,
5287 but braces are not elided. */
5288 d->cur++;
5289 if (BRACE_ENCLOSED_INITIALIZER_P (init))
5290 {
5291 if (CONSTRUCTOR_NELTS (init) > 2)
5292 {
5293 if (complain & tf_error)
5294 error ("too many initializers for %qT", type);
5295 else
5296 return error_mark_node;
5297 }
5298 }
5299 else if (first_initializer_p && d->cur != d->end)
5300 {
5301 vec<constructor_elt, va_gc> *v = 0;
5302 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, init);
5303 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, d->cur->value);
5304 if (has_designator_problem (d, complain))
5305 return error_mark_node;
5306 d->cur++;
5307 init = build_constructor (init_list_type_node, v);
5308 }
5309 return init;
5310 }
5311
5312 /* A non-aggregate type is always initialized with a single
5313 initializer. */
5314 if (!CP_AGGREGATE_TYPE_P (type))
5315 {
5316 /* It is invalid to initialize a non-aggregate type with a
5317 brace-enclosed initializer before C++0x.
5318 We need to check for BRACE_ENCLOSED_INITIALIZER_P here because
5319 of g++.old-deja/g++.mike/p7626.C: a pointer-to-member constant is
5320 a CONSTRUCTOR (with a record type). */
5321 if (TREE_CODE (init) == CONSTRUCTOR
5322 && BRACE_ENCLOSED_INITIALIZER_P (init)) /* p7626.C */
5323 {
5324 if (SCALAR_TYPE_P (type))
5325 {
5326 if (complain & tf_error)
5327 error ("braces around scalar initializer for type %qT", type);
5328 init = error_mark_node;
5329 }
5330 else
5331 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
5332 }
5333
5334 d->cur++;
5335 return init;
5336 }
5337
5338 /* [dcl.init.aggr]
5339
5340 All implicit type conversions (clause _conv_) are considered when
5341 initializing the aggregate member with an initializer from an
5342 initializer-list. If the initializer can initialize a member,
5343 the member is initialized. Otherwise, if the member is itself a
5344 non-empty subaggregate, brace elision is assumed and the
5345 initializer is considered for the initialization of the first
5346 member of the subaggregate. */
5347 if (TREE_CODE (init) != CONSTRUCTOR
5348 /* But don't try this for the first initializer, since that would be
5349 looking through the outermost braces; A a2 = { a1 }; is not a
5350 valid aggregate initialization. */
5351 && !first_initializer_p
5352 && (same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (init))
5353 || can_convert_arg (type, TREE_TYPE (init), init, LOOKUP_NORMAL,
5354 complain)))
5355 {
5356 d->cur++;
5357 return init;
5358 }
5359
5360 /* [dcl.init.string]
5361
5362 A char array (whether plain char, signed char, or unsigned char)
5363 can be initialized by a string-literal (optionally enclosed in
5364 braces); a wchar_t array can be initialized by a wide
5365 string-literal (optionally enclosed in braces). */
5366 if (TREE_CODE (type) == ARRAY_TYPE
5367 && char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type))))
5368 {
5369 tree str_init = init;
5370
5371 /* Strip one level of braces if and only if they enclose a single
5372 element (as allowed by [dcl.init.string]). */
5373 if (!first_initializer_p
5374 && TREE_CODE (str_init) == CONSTRUCTOR
5375 && vec_safe_length (CONSTRUCTOR_ELTS (str_init)) == 1)
5376 {
5377 str_init = (*CONSTRUCTOR_ELTS (str_init))[0].value;
5378 }
5379
5380 /* If it's a string literal, then it's the initializer for the array
5381 as a whole. Otherwise, continue with normal initialization for
5382 array types (one value per array element). */
5383 if (TREE_CODE (str_init) == STRING_CST)
5384 {
5385 if (has_designator_problem (d, complain))
5386 return error_mark_node;
5387 d->cur++;
5388 return str_init;
5389 }
5390 }
5391
5392 /* The following cases are about aggregates. If we are not within a full
5393 initializer already, and there is not a CONSTRUCTOR, it means that there
5394 is a missing set of braces (that is, we are processing the case for
5395 which reshape_init exists). */
5396 if (!first_initializer_p)
5397 {
5398 if (TREE_CODE (init) == CONSTRUCTOR)
5399 {
5400 if (TREE_TYPE (init) && TYPE_PTRMEMFUNC_P (TREE_TYPE (init)))
5401 /* There is no need to reshape pointer-to-member function
5402 initializers, as they are always constructed correctly
5403 by the front end. */
5404 ;
5405 else if (COMPOUND_LITERAL_P (init))
5406 /* For a nested compound literal, there is no need to reshape since
5407 brace elision is not allowed. Even if we decided to allow it,
5408 we should add a call to reshape_init in finish_compound_literal,
5409 before calling digest_init, so changing this code would still
5410 not be necessary. */
5411 gcc_assert (!BRACE_ENCLOSED_INITIALIZER_P (init));
5412 else
5413 {
5414 ++d->cur;
5415 gcc_assert (BRACE_ENCLOSED_INITIALIZER_P (init));
5416 return reshape_init (type, init, complain);
5417 }
5418 }
5419
5420 warning (OPT_Wmissing_braces, "missing braces around initializer for %qT",
5421 type);
5422 }
5423
5424 /* Dispatch to specialized routines. */
5425 if (CLASS_TYPE_P (type))
5426 return reshape_init_class (type, d, first_initializer_p, complain);
5427 else if (TREE_CODE (type) == ARRAY_TYPE)
5428 return reshape_init_array (type, d, complain);
5429 else if (TREE_CODE (type) == VECTOR_TYPE)
5430 return reshape_init_vector (type, d, complain);
5431 else
5432 gcc_unreachable();
5433 }
5434
5435 /* Undo the brace-elision allowed by [dcl.init.aggr] in a
5436 brace-enclosed aggregate initializer.
5437
5438 INIT is the CONSTRUCTOR containing the list of initializers describing
5439 a brace-enclosed initializer for an entity of the indicated aggregate TYPE.
5440 It may not presently match the shape of the TYPE; for example:
5441
5442 struct S { int a; int b; };
5443 struct S a[] = { 1, 2, 3, 4 };
5444
5445 Here INIT will hold a vector of four elements, rather than a
5446 vector of two elements, each itself a vector of two elements. This
5447 routine transforms INIT from the former form into the latter. The
5448 revised CONSTRUCTOR node is returned. */
5449
5450 tree
5451 reshape_init (tree type, tree init, tsubst_flags_t complain)
5452 {
5453 vec<constructor_elt, va_gc> *v;
5454 reshape_iter d;
5455 tree new_init;
5456
5457 gcc_assert (BRACE_ENCLOSED_INITIALIZER_P (init));
5458
5459 v = CONSTRUCTOR_ELTS (init);
5460
5461 /* An empty constructor does not need reshaping, and it is always a valid
5462 initializer. */
5463 if (vec_safe_is_empty (v))
5464 return init;
5465
5466 /* Recurse on this CONSTRUCTOR. */
5467 d.cur = &(*v)[0];
5468 d.end = d.cur + v->length ();
5469
5470 new_init = reshape_init_r (type, &d, true, complain);
5471 if (new_init == error_mark_node)
5472 return error_mark_node;
5473
5474 /* Make sure all the element of the constructor were used. Otherwise,
5475 issue an error about exceeding initializers. */
5476 if (d.cur != d.end)
5477 {
5478 if (complain & tf_error)
5479 error ("too many initializers for %qT", type);
5480 else
5481 return error_mark_node;
5482 }
5483
5484 return new_init;
5485 }
5486
5487 /* Verify array initializer. Returns true if errors have been reported. */
5488
5489 bool
5490 check_array_initializer (tree decl, tree type, tree init)
5491 {
5492 tree element_type = TREE_TYPE (type);
5493
5494 /* The array type itself need not be complete, because the
5495 initializer may tell us how many elements are in the array.
5496 But, the elements of the array must be complete. */
5497 if (!COMPLETE_TYPE_P (complete_type (element_type)))
5498 {
5499 if (decl)
5500 error ("elements of array %q#D have incomplete type", decl);
5501 else
5502 error ("elements of array %q#T have incomplete type", type);
5503 return true;
5504 }
5505 /* It is not valid to initialize a VLA. */
5506 if (init
5507 && ((COMPLETE_TYPE_P (type) && !TREE_CONSTANT (TYPE_SIZE (type)))
5508 || !TREE_CONSTANT (TYPE_SIZE (element_type))))
5509 {
5510 if (decl)
5511 error ("variable-sized object %qD may not be initialized", decl);
5512 else
5513 error ("variable-sized compound literal");
5514 return true;
5515 }
5516 return false;
5517 }
5518
5519 /* Subroutine of check_initializer; args are passed down from that function.
5520 Set stmts_are_full_exprs_p to 1 across a call to build_aggr_init. */
5521
5522 static tree
5523 build_aggr_init_full_exprs (tree decl, tree init, int flags)
5524
5525 {
5526 gcc_assert (stmts_are_full_exprs_p ());
5527 return build_aggr_init (decl, init, flags, tf_warning_or_error);
5528 }
5529
5530 /* Verify INIT (the initializer for DECL), and record the
5531 initialization in DECL_INITIAL, if appropriate. CLEANUP is as for
5532 grok_reference_init.
5533
5534 If the return value is non-NULL, it is an expression that must be
5535 evaluated dynamically to initialize DECL. */
5536
5537 static tree
5538 check_initializer (tree decl, tree init, int flags, vec<tree, va_gc> **cleanups)
5539 {
5540 tree type = TREE_TYPE (decl);
5541 tree init_code = NULL;
5542 tree extra_init = NULL_TREE;
5543 tree core_type;
5544
5545 /* Things that are going to be initialized need to have complete
5546 type. */
5547 TREE_TYPE (decl) = type = complete_type (TREE_TYPE (decl));
5548
5549 if (DECL_HAS_VALUE_EXPR_P (decl))
5550 {
5551 /* A variable with DECL_HAS_VALUE_EXPR_P set is just a placeholder,
5552 it doesn't have storage to be initialized. */
5553 gcc_assert (init == NULL_TREE);
5554 return NULL_TREE;
5555 }
5556
5557 if (type == error_mark_node)
5558 /* We will have already complained. */
5559 return NULL_TREE;
5560
5561 if (TREE_CODE (type) == ARRAY_TYPE)
5562 {
5563 if (check_array_initializer (decl, type, init))
5564 return NULL_TREE;
5565 }
5566 else if (!COMPLETE_TYPE_P (type))
5567 {
5568 error ("%q#D has incomplete type", decl);
5569 TREE_TYPE (decl) = error_mark_node;
5570 return NULL_TREE;
5571 }
5572 else
5573 /* There is no way to make a variable-sized class type in GNU C++. */
5574 gcc_assert (TREE_CONSTANT (TYPE_SIZE (type)));
5575
5576 if (init && BRACE_ENCLOSED_INITIALIZER_P (init))
5577 {
5578 int init_len = vec_safe_length (CONSTRUCTOR_ELTS (init));
5579 if (SCALAR_TYPE_P (type))
5580 {
5581 if (init_len == 0)
5582 {
5583 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
5584 init = build_zero_init (type, NULL_TREE, false);
5585 }
5586 else if (init_len != 1 && TREE_CODE (type) != COMPLEX_TYPE)
5587 {
5588 error ("scalar object %qD requires one element in initializer",
5589 decl);
5590 TREE_TYPE (decl) = error_mark_node;
5591 return NULL_TREE;
5592 }
5593 }
5594 }
5595
5596 if (TREE_CODE (decl) == CONST_DECL)
5597 {
5598 gcc_assert (TREE_CODE (type) != REFERENCE_TYPE);
5599
5600 DECL_INITIAL (decl) = init;
5601
5602 gcc_assert (init != NULL_TREE);
5603 init = NULL_TREE;
5604 }
5605 else if (!init && DECL_REALLY_EXTERN (decl))
5606 ;
5607 else if (init || type_build_ctor_call (type)
5608 || TREE_CODE (type) == REFERENCE_TYPE)
5609 {
5610 if (TREE_CODE (type) == REFERENCE_TYPE)
5611 {
5612 init = grok_reference_init (decl, type, init, flags);
5613 flags |= LOOKUP_ALREADY_DIGESTED;
5614 }
5615 else if (!init)
5616 check_for_uninitialized_const_var (decl);
5617 /* Do not reshape constructors of vectors (they don't need to be
5618 reshaped. */
5619 else if (BRACE_ENCLOSED_INITIALIZER_P (init))
5620 {
5621 if (is_std_init_list (type))
5622 {
5623 init = perform_implicit_conversion (type, init,
5624 tf_warning_or_error);
5625 flags |= LOOKUP_ALREADY_DIGESTED;
5626 }
5627 else if (TYPE_NON_AGGREGATE_CLASS (type))
5628 {
5629 /* Don't reshape if the class has constructors. */
5630 if (cxx_dialect == cxx98)
5631 error ("in C++98 %qD must be initialized by constructor, "
5632 "not by %<{...}%>",
5633 decl);
5634 }
5635 else if (TREE_CODE (type) == VECTOR_TYPE && TYPE_VECTOR_OPAQUE (type))
5636 {
5637 error ("opaque vector types cannot be initialized");
5638 init = error_mark_node;
5639 }
5640 else
5641 {
5642 init = reshape_init (type, init, tf_warning_or_error);
5643 if (SCALAR_TYPE_P (type))
5644 check_narrowing (type, init);
5645 }
5646 }
5647
5648 /* If DECL has an array type without a specific bound, deduce the
5649 array size from the initializer. */
5650 maybe_deduce_size_from_array_init (decl, init);
5651 type = TREE_TYPE (decl);
5652 if (type == error_mark_node)
5653 return NULL_TREE;
5654
5655 if ((type_build_ctor_call (type) || CLASS_TYPE_P (type))
5656 && !(flags & LOOKUP_ALREADY_DIGESTED)
5657 && !(init && BRACE_ENCLOSED_INITIALIZER_P (init)
5658 && CP_AGGREGATE_TYPE_P (type)
5659 && (CLASS_TYPE_P (type)
5660 || type_has_extended_temps (type))))
5661 {
5662 init_code = build_aggr_init_full_exprs (decl, init, flags);
5663
5664 /* A constructor call is a non-trivial initializer even if
5665 it isn't explicitly written. */
5666 if (TREE_SIDE_EFFECTS (init_code))
5667 DECL_NONTRIVIALLY_INITIALIZED_P (decl) = true;
5668
5669 /* If this is a constexpr initializer, expand_default_init will
5670 have returned an INIT_EXPR rather than a CALL_EXPR. In that
5671 case, pull the initializer back out and pass it down into
5672 store_init_value. */
5673 while (TREE_CODE (init_code) == EXPR_STMT
5674 || TREE_CODE (init_code) == CONVERT_EXPR)
5675 init_code = TREE_OPERAND (init_code, 0);
5676 if (TREE_CODE (init_code) == INIT_EXPR)
5677 {
5678 init = TREE_OPERAND (init_code, 1);
5679 init_code = NULL_TREE;
5680 /* Don't call digest_init; it's unnecessary and will complain
5681 about aggregate initialization of non-aggregate classes. */
5682 flags |= LOOKUP_ALREADY_DIGESTED;
5683 }
5684 else if (DECL_DECLARED_CONSTEXPR_P (decl))
5685 {
5686 /* Declared constexpr, but no suitable initializer; massage
5687 init appropriately so we can pass it into store_init_value
5688 for the error. */
5689 if (init && BRACE_ENCLOSED_INITIALIZER_P (init))
5690 init = finish_compound_literal (type, init,
5691 tf_warning_or_error);
5692 else if (CLASS_TYPE_P (type)
5693 && (!init || TREE_CODE (init) == TREE_LIST))
5694 {
5695 init = build_functional_cast (type, init, tf_none);
5696 if (TREE_CODE (init) == TARGET_EXPR)
5697 TARGET_EXPR_DIRECT_INIT_P (init) = true;
5698 }
5699 init_code = NULL_TREE;
5700 }
5701 else
5702 init = NULL_TREE;
5703 }
5704
5705 if (init && TREE_CODE (init) != TREE_VEC)
5706 {
5707 /* In aggregate initialization of a variable, each element
5708 initialization is a full-expression because there is no
5709 enclosing expression. */
5710 gcc_assert (stmts_are_full_exprs_p ());
5711
5712 init_code = store_init_value (decl, init, cleanups, flags);
5713
5714 if (pedantic && TREE_CODE (type) == ARRAY_TYPE
5715 && DECL_INITIAL (decl)
5716 && TREE_CODE (DECL_INITIAL (decl)) == STRING_CST
5717 && PAREN_STRING_LITERAL_P (DECL_INITIAL (decl)))
5718 warning (0, "array %qD initialized by parenthesized string literal %qE",
5719 decl, DECL_INITIAL (decl));
5720 init = NULL;
5721 }
5722 }
5723 else
5724 {
5725 if (CLASS_TYPE_P (core_type = strip_array_types (type))
5726 && (CLASSTYPE_READONLY_FIELDS_NEED_INIT (core_type)
5727 || CLASSTYPE_REF_FIELDS_NEED_INIT (core_type)))
5728 diagnose_uninitialized_cst_or_ref_member (core_type, /*using_new=*/false,
5729 /*complain=*/true);
5730
5731 check_for_uninitialized_const_var (decl);
5732 }
5733
5734 if (init && init != error_mark_node)
5735 init_code = build2 (INIT_EXPR, type, decl, init);
5736
5737 if (extra_init)
5738 init_code = add_stmt_to_compound (extra_init, init_code);
5739
5740 if (init_code && DECL_IN_AGGR_P (decl))
5741 {
5742 static int explained = 0;
5743
5744 if (cxx_dialect < cxx0x)
5745 error ("initializer invalid for static member with constructor");
5746 else
5747 error ("non-constant in-class initialization invalid for static "
5748 "member %qD", decl);
5749 if (!explained)
5750 {
5751 error ("(an out of class initialization is required)");
5752 explained = 1;
5753 }
5754 }
5755
5756 return init_code;
5757 }
5758
5759 /* If DECL is not a local variable, give it RTL. */
5760
5761 static void
5762 make_rtl_for_nonlocal_decl (tree decl, tree init, const char* asmspec)
5763 {
5764 int toplev = toplevel_bindings_p ();
5765 int defer_p;
5766 const char *filename;
5767
5768 /* Set the DECL_ASSEMBLER_NAME for the object. */
5769 if (asmspec)
5770 {
5771 /* The `register' keyword, when used together with an
5772 asm-specification, indicates that the variable should be
5773 placed in a particular register. */
5774 if (TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
5775 {
5776 set_user_assembler_name (decl, asmspec);
5777 DECL_HARD_REGISTER (decl) = 1;
5778 }
5779 else
5780 {
5781 if (TREE_CODE (decl) == FUNCTION_DECL
5782 && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
5783 set_builtin_user_assembler_name (decl, asmspec);
5784 set_user_assembler_name (decl, asmspec);
5785 }
5786 }
5787
5788 /* Handle non-variables up front. */
5789 if (TREE_CODE (decl) != VAR_DECL)
5790 {
5791 rest_of_decl_compilation (decl, toplev, at_eof);
5792 return;
5793 }
5794
5795 /* If we see a class member here, it should be a static data
5796 member. */
5797 if (DECL_LANG_SPECIFIC (decl) && DECL_IN_AGGR_P (decl))
5798 {
5799 gcc_assert (TREE_STATIC (decl));
5800 /* An in-class declaration of a static data member should be
5801 external; it is only a declaration, and not a definition. */
5802 if (init == NULL_TREE)
5803 gcc_assert (DECL_EXTERNAL (decl) || !TREE_PUBLIC (decl));
5804 }
5805
5806 /* We don't create any RTL for local variables. */
5807 if (DECL_FUNCTION_SCOPE_P (decl) && !TREE_STATIC (decl))
5808 return;
5809
5810 /* We defer emission of local statics until the corresponding
5811 DECL_EXPR is expanded. */
5812 defer_p = DECL_FUNCTION_SCOPE_P (decl) || DECL_VIRTUAL_P (decl);
5813
5814 /* We try to defer namespace-scope static constants so that they are
5815 not emitted into the object file unnecessarily. */
5816 filename = input_filename;
5817 if (!DECL_VIRTUAL_P (decl)
5818 && TREE_READONLY (decl)
5819 && DECL_INITIAL (decl) != NULL_TREE
5820 && DECL_INITIAL (decl) != error_mark_node
5821 && filename != NULL
5822 && ! EMPTY_CONSTRUCTOR_P (DECL_INITIAL (decl))
5823 && toplev
5824 && !TREE_PUBLIC (decl))
5825 {
5826 /* Fool with the linkage of static consts according to #pragma
5827 interface. */
5828 struct c_fileinfo *finfo = get_fileinfo (filename);
5829 if (!finfo->interface_unknown && !TREE_PUBLIC (decl))
5830 {
5831 TREE_PUBLIC (decl) = 1;
5832 DECL_EXTERNAL (decl) = finfo->interface_only;
5833 }
5834
5835 defer_p = 1;
5836 }
5837 /* Likewise for template instantiations. */
5838 else if (DECL_LANG_SPECIFIC (decl)
5839 && DECL_IMPLICIT_INSTANTIATION (decl))
5840 defer_p = 1;
5841
5842 /* If we're not deferring, go ahead and assemble the variable. */
5843 if (!defer_p)
5844 rest_of_decl_compilation (decl, toplev, at_eof);
5845 }
5846
5847 /* walk_tree helper for wrap_temporary_cleanups, below. */
5848
5849 static tree
5850 wrap_cleanups_r (tree *stmt_p, int *walk_subtrees, void *data)
5851 {
5852 /* Stop at types or full-expression boundaries. */
5853 if (TYPE_P (*stmt_p)
5854 || TREE_CODE (*stmt_p) == CLEANUP_POINT_EXPR)
5855 {
5856 *walk_subtrees = 0;
5857 return NULL_TREE;
5858 }
5859
5860 if (TREE_CODE (*stmt_p) == TARGET_EXPR)
5861 {
5862 tree guard = (tree)data;
5863 tree tcleanup = TARGET_EXPR_CLEANUP (*stmt_p);
5864
5865 tcleanup = build2 (TRY_CATCH_EXPR, void_type_node, tcleanup, guard);
5866 /* Tell honor_protect_cleanup_actions to handle this as a separate
5867 cleanup. */
5868 TRY_CATCH_IS_CLEANUP (tcleanup) = 1;
5869
5870 TARGET_EXPR_CLEANUP (*stmt_p) = tcleanup;
5871 }
5872
5873 return NULL_TREE;
5874 }
5875
5876 /* We're initializing a local variable which has a cleanup GUARD. If there
5877 are any temporaries used in the initializer INIT of this variable, we
5878 need to wrap their cleanups with TRY_CATCH_EXPR (, GUARD) so that the
5879 variable will be cleaned up properly if one of them throws.
5880
5881 Unfortunately, there's no way to express this properly in terms of
5882 nesting, as the regions for the temporaries overlap the region for the
5883 variable itself; if there are two temporaries, the variable needs to be
5884 the first thing destroyed if either of them throws. However, we only
5885 want to run the variable's cleanup if it actually got constructed. So
5886 we need to guard the temporary cleanups with the variable's cleanup if
5887 they are run on the normal path, but not if they are run on the
5888 exceptional path. We implement this by telling
5889 honor_protect_cleanup_actions to strip the variable cleanup from the
5890 exceptional path. */
5891
5892 static void
5893 wrap_temporary_cleanups (tree init, tree guard)
5894 {
5895 cp_walk_tree_without_duplicates (&init, wrap_cleanups_r, (void *)guard);
5896 }
5897
5898 /* Generate code to initialize DECL (a local variable). */
5899
5900 static void
5901 initialize_local_var (tree decl, tree init)
5902 {
5903 tree type = TREE_TYPE (decl);
5904 tree cleanup;
5905 int already_used;
5906
5907 gcc_assert (TREE_CODE (decl) == VAR_DECL
5908 || TREE_CODE (decl) == RESULT_DECL);
5909 gcc_assert (!TREE_STATIC (decl));
5910
5911 if (DECL_SIZE (decl) == NULL_TREE)
5912 {
5913 /* If we used it already as memory, it must stay in memory. */
5914 DECL_INITIAL (decl) = NULL_TREE;
5915 TREE_ADDRESSABLE (decl) = TREE_USED (decl);
5916 return;
5917 }
5918
5919 if (type == error_mark_node)
5920 return;
5921
5922 /* Compute and store the initial value. */
5923 already_used = TREE_USED (decl) || TREE_USED (type);
5924 if (TREE_USED (type))
5925 DECL_READ_P (decl) = 1;
5926
5927 /* Generate a cleanup, if necessary. */
5928 cleanup = cxx_maybe_build_cleanup (decl, tf_warning_or_error);
5929
5930 /* Perform the initialization. */
5931 if (init)
5932 {
5933 if (TREE_CODE (init) == INIT_EXPR
5934 && !TREE_SIDE_EFFECTS (TREE_OPERAND (init, 1)))
5935 {
5936 /* Stick simple initializers in DECL_INITIAL so that
5937 -Wno-init-self works (c++/34772). */
5938 gcc_assert (TREE_OPERAND (init, 0) == decl);
5939 DECL_INITIAL (decl) = TREE_OPERAND (init, 1);
5940 }
5941 else
5942 {
5943 int saved_stmts_are_full_exprs_p;
5944
5945 /* If we're only initializing a single object, guard the
5946 destructors of any temporaries used in its initializer with
5947 its destructor. This isn't right for arrays because each
5948 element initialization is a full-expression. */
5949 if (cleanup && TREE_CODE (type) != ARRAY_TYPE)
5950 wrap_temporary_cleanups (init, cleanup);
5951
5952 gcc_assert (building_stmt_list_p ());
5953 saved_stmts_are_full_exprs_p = stmts_are_full_exprs_p ();
5954 current_stmt_tree ()->stmts_are_full_exprs_p = 1;
5955 finish_expr_stmt (init);
5956 current_stmt_tree ()->stmts_are_full_exprs_p =
5957 saved_stmts_are_full_exprs_p;
5958 }
5959 }
5960
5961 /* Set this to 0 so we can tell whether an aggregate which was
5962 initialized was ever used. Don't do this if it has a
5963 destructor, so we don't complain about the 'resource
5964 allocation is initialization' idiom. Now set
5965 attribute((unused)) on types so decls of that type will be
5966 marked used. (see TREE_USED, above.) */
5967 if (TYPE_NEEDS_CONSTRUCTING (type)
5968 && ! already_used
5969 && TYPE_HAS_TRIVIAL_DESTRUCTOR (type)
5970 && DECL_NAME (decl))
5971 TREE_USED (decl) = 0;
5972 else if (already_used)
5973 TREE_USED (decl) = 1;
5974
5975 if (cleanup)
5976 finish_decl_cleanup (decl, cleanup);
5977 }
5978
5979 /* DECL is a VAR_DECL for a compiler-generated variable with static
5980 storage duration (like a virtual table) whose initializer is a
5981 compile-time constant. Initialize the variable and provide it to the
5982 back end. */
5983
5984 void
5985 initialize_artificial_var (tree decl, vec<constructor_elt, va_gc> *v)
5986 {
5987 tree init;
5988 gcc_assert (DECL_ARTIFICIAL (decl));
5989 init = build_constructor (TREE_TYPE (decl), v);
5990 gcc_assert (TREE_CODE (init) == CONSTRUCTOR);
5991 DECL_INITIAL (decl) = init;
5992 DECL_INITIALIZED_P (decl) = 1;
5993 determine_visibility (decl);
5994 layout_var_decl (decl);
5995 maybe_commonize_var (decl);
5996 make_rtl_for_nonlocal_decl (decl, init, /*asmspec=*/NULL);
5997 }
5998
5999 /* INIT is the initializer for a variable, as represented by the
6000 parser. Returns true iff INIT is type-dependent. */
6001
6002 static bool
6003 type_dependent_init_p (tree init)
6004 {
6005 if (TREE_CODE (init) == TREE_LIST)
6006 /* A parenthesized initializer, e.g.: int i (3, 2); ? */
6007 return any_type_dependent_elements_p (init);
6008 else if (TREE_CODE (init) == CONSTRUCTOR)
6009 /* A brace-enclosed initializer, e.g.: int i = { 3 }; ? */
6010 {
6011 vec<constructor_elt, va_gc> *elts;
6012 size_t nelts;
6013 size_t i;
6014
6015 elts = CONSTRUCTOR_ELTS (init);
6016 nelts = vec_safe_length (elts);
6017 for (i = 0; i < nelts; ++i)
6018 if (type_dependent_init_p ((*elts)[i].value))
6019 return true;
6020 }
6021 else
6022 /* It must be a simple expression, e.g., int i = 3; */
6023 return type_dependent_expression_p (init);
6024
6025 return false;
6026 }
6027
6028 /* INIT is the initializer for a variable, as represented by the
6029 parser. Returns true iff INIT is value-dependent. */
6030
6031 static bool
6032 value_dependent_init_p (tree init)
6033 {
6034 if (TREE_CODE (init) == TREE_LIST)
6035 /* A parenthesized initializer, e.g.: int i (3, 2); ? */
6036 return any_value_dependent_elements_p (init);
6037 else if (TREE_CODE (init) == CONSTRUCTOR)
6038 /* A brace-enclosed initializer, e.g.: int i = { 3 }; ? */
6039 {
6040 vec<constructor_elt, va_gc> *elts;
6041 size_t nelts;
6042 size_t i;
6043
6044 elts = CONSTRUCTOR_ELTS (init);
6045 nelts = vec_safe_length (elts);
6046 for (i = 0; i < nelts; ++i)
6047 if (value_dependent_init_p ((*elts)[i].value))
6048 return true;
6049 }
6050 else
6051 /* It must be a simple expression, e.g., int i = 3; */
6052 return value_dependent_expression_p (init);
6053
6054 return false;
6055 }
6056
6057 /* Finish processing of a declaration;
6058 install its line number and initial value.
6059 If the length of an array type is not known before,
6060 it must be determined now, from the initial value, or it is an error.
6061
6062 INIT is the initializer (if any) for DECL. If INIT_CONST_EXPR_P is
6063 true, then INIT is an integral constant expression.
6064
6065 FLAGS is LOOKUP_ONLYCONVERTING if the = init syntax was used, else 0
6066 if the (init) syntax was used. */
6067
6068 void
6069 cp_finish_decl (tree decl, tree init, bool init_const_expr_p,
6070 tree asmspec_tree, int flags)
6071 {
6072 tree type;
6073 vec<tree, va_gc> *cleanups = NULL;
6074 const char *asmspec = NULL;
6075 int was_readonly = 0;
6076 bool var_definition_p = false;
6077 tree auto_node;
6078
6079 if (decl == error_mark_node)
6080 return;
6081 else if (! decl)
6082 {
6083 if (init)
6084 error ("assignment (not initialization) in declaration");
6085 return;
6086 }
6087
6088 gcc_assert (TREE_CODE (decl) != RESULT_DECL);
6089 /* Parameters are handled by store_parm_decls, not cp_finish_decl. */
6090 gcc_assert (TREE_CODE (decl) != PARM_DECL);
6091
6092 type = TREE_TYPE (decl);
6093 if (type == error_mark_node)
6094 return;
6095
6096 /* If a name was specified, get the string. */
6097 if (at_namespace_scope_p ())
6098 asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
6099 if (asmspec_tree && asmspec_tree != error_mark_node)
6100 asmspec = TREE_STRING_POINTER (asmspec_tree);
6101
6102 if (current_class_type
6103 && CP_DECL_CONTEXT (decl) == current_class_type
6104 && TYPE_BEING_DEFINED (current_class_type)
6105 && !CLASSTYPE_TEMPLATE_INSTANTIATION (current_class_type)
6106 && (DECL_INITIAL (decl) || init))
6107 DECL_INITIALIZED_IN_CLASS_P (decl) = 1;
6108
6109 if (TREE_CODE (decl) != FUNCTION_DECL
6110 && (auto_node = type_uses_auto (type)))
6111 {
6112 tree d_init;
6113 if (init == NULL_TREE)
6114 {
6115 error ("declaration of %q#D has no initializer", decl);
6116 TREE_TYPE (decl) = error_mark_node;
6117 return;
6118 }
6119 d_init = init;
6120 if (TREE_CODE (d_init) == TREE_LIST)
6121 d_init = build_x_compound_expr_from_list (d_init, ELK_INIT,
6122 tf_warning_or_error);
6123 d_init = resolve_nondeduced_context (d_init);
6124 type = TREE_TYPE (decl) = do_auto_deduction (type, d_init,
6125 auto_node);
6126 if (type == error_mark_node)
6127 return;
6128 }
6129
6130 if (!ensure_literal_type_for_constexpr_object (decl))
6131 DECL_DECLARED_CONSTEXPR_P (decl) = 0;
6132
6133 if (TREE_CODE (decl) == VAR_DECL
6134 && DECL_CLASS_SCOPE_P (decl)
6135 && DECL_INITIALIZED_IN_CLASS_P (decl))
6136 check_static_variable_definition (decl, type);
6137
6138 if (init && TREE_CODE (decl) == FUNCTION_DECL)
6139 {
6140 tree clone;
6141 if (init == ridpointers[(int)RID_DELETE])
6142 {
6143 /* FIXME check this is 1st decl. */
6144 DECL_DELETED_FN (decl) = 1;
6145 DECL_DECLARED_INLINE_P (decl) = 1;
6146 DECL_INITIAL (decl) = error_mark_node;
6147 FOR_EACH_CLONE (clone, decl)
6148 {
6149 DECL_DELETED_FN (clone) = 1;
6150 DECL_DECLARED_INLINE_P (clone) = 1;
6151 DECL_INITIAL (clone) = error_mark_node;
6152 }
6153 init = NULL_TREE;
6154 }
6155 else if (init == ridpointers[(int)RID_DEFAULT])
6156 {
6157 if (defaultable_fn_check (decl))
6158 DECL_DEFAULTED_FN (decl) = 1;
6159 else
6160 DECL_INITIAL (decl) = NULL_TREE;
6161 }
6162 }
6163
6164 if (init && TREE_CODE (decl) == VAR_DECL)
6165 {
6166 DECL_NONTRIVIALLY_INITIALIZED_P (decl) = 1;
6167 /* If DECL is a reference, then we want to know whether init is a
6168 reference constant; init_const_expr_p as passed tells us whether
6169 it's an rvalue constant. */
6170 if (TREE_CODE (type) == REFERENCE_TYPE)
6171 init_const_expr_p = potential_constant_expression (init);
6172 if (init_const_expr_p)
6173 {
6174 /* Set these flags now for templates. We'll update the flags in
6175 store_init_value for instantiations. */
6176 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1;
6177 if (decl_maybe_constant_var_p (decl))
6178 TREE_CONSTANT (decl) = 1;
6179 }
6180 }
6181
6182 if (processing_template_decl)
6183 {
6184 bool type_dependent_p;
6185
6186 /* Add this declaration to the statement-tree. */
6187 if (at_function_scope_p ())
6188 add_decl_expr (decl);
6189
6190 type_dependent_p = dependent_type_p (type);
6191
6192 if (check_for_bare_parameter_packs (init))
6193 {
6194 init = NULL_TREE;
6195 DECL_INITIAL (decl) = NULL_TREE;
6196 }
6197
6198 /* Generally, initializers in templates are expanded when the
6199 template is instantiated. But, if DECL is a variable constant
6200 then it can be used in future constant expressions, so its value
6201 must be available. */
6202
6203 if (TREE_CODE (decl) != VAR_DECL || dependent_type_p (type))
6204 /* We can't do anything if the decl has dependent type. */;
6205 else if (init
6206 && init_const_expr_p
6207 && !type_dependent_p
6208 && decl_maybe_constant_var_p (decl)
6209 && !type_dependent_init_p (init)
6210 && !value_dependent_init_p (init))
6211 {
6212 /* This variable seems to be a non-dependent constant, so process
6213 its initializer. If check_initializer returns non-null the
6214 initialization wasn't constant after all. */
6215 tree init_code;
6216 cleanups = make_tree_vector ();
6217 init_code = check_initializer (decl, init, flags, &cleanups);
6218 if (init_code == NULL_TREE)
6219 init = NULL_TREE;
6220 release_tree_vector (cleanups);
6221 }
6222 else if (!DECL_PRETTY_FUNCTION_P (decl))
6223 {
6224 /* Deduce array size even if the initializer is dependent. */
6225 maybe_deduce_size_from_array_init (decl, init);
6226 /* And complain about multiple initializers. */
6227 if (init && TREE_CODE (init) == TREE_LIST && TREE_CHAIN (init)
6228 && !MAYBE_CLASS_TYPE_P (type))
6229 init = build_x_compound_expr_from_list (init, ELK_INIT,
6230 tf_warning_or_error);
6231 }
6232
6233 if (init)
6234 DECL_INITIAL (decl) = init;
6235 return;
6236 }
6237
6238 /* Just store non-static data member initializers for later. */
6239 if (init && TREE_CODE (decl) == FIELD_DECL)
6240 DECL_INITIAL (decl) = init;
6241
6242 /* Take care of TYPE_DECLs up front. */
6243 if (TREE_CODE (decl) == TYPE_DECL)
6244 {
6245 if (type != error_mark_node
6246 && MAYBE_CLASS_TYPE_P (type) && DECL_NAME (decl))
6247 {
6248 if (TREE_TYPE (DECL_NAME (decl)) && TREE_TYPE (decl) != type)
6249 warning (0, "shadowing previous type declaration of %q#D", decl);
6250 set_identifier_type_value (DECL_NAME (decl), decl);
6251 }
6252
6253 /* If we have installed this as the canonical typedef for this
6254 type, and that type has not been defined yet, delay emitting
6255 the debug information for it, as we will emit it later. */
6256 if (TYPE_MAIN_DECL (TREE_TYPE (decl)) == decl
6257 && !COMPLETE_TYPE_P (TREE_TYPE (decl)))
6258 TYPE_DECL_SUPPRESS_DEBUG (decl) = 1;
6259
6260 rest_of_decl_compilation (decl, DECL_FILE_SCOPE_P (decl),
6261 at_eof);
6262 return;
6263 }
6264
6265 /* A reference will be modified here, as it is initialized. */
6266 if (! DECL_EXTERNAL (decl)
6267 && TREE_READONLY (decl)
6268 && TREE_CODE (type) == REFERENCE_TYPE)
6269 {
6270 was_readonly = 1;
6271 TREE_READONLY (decl) = 0;
6272 }
6273
6274 if (TREE_CODE (decl) == VAR_DECL)
6275 {
6276 /* If this is a local variable that will need a mangled name,
6277 register it now. We must do this before processing the
6278 initializer for the variable, since the initialization might
6279 require a guard variable, and since the mangled name of the
6280 guard variable will depend on the mangled name of this
6281 variable. */
6282 if (DECL_FUNCTION_SCOPE_P (decl)
6283 && TREE_STATIC (decl)
6284 && !DECL_ARTIFICIAL (decl))
6285 {
6286 push_local_name (decl);
6287 if (DECL_CONSTRUCTOR_P (current_function_decl)
6288 || DECL_DESTRUCTOR_P (current_function_decl))
6289 /* Normally local_decls is populated during GIMPLE lowering,
6290 but [cd]tors are never actually compiled directly. We need
6291 to put statics on the list so we can deal with the label
6292 address extension. FIXME. */
6293 add_local_decl (cfun, decl);
6294 }
6295
6296 /* Convert the initializer to the type of DECL, if we have not
6297 already initialized DECL. */
6298 if (!DECL_INITIALIZED_P (decl)
6299 /* If !DECL_EXTERNAL then DECL is being defined. In the
6300 case of a static data member initialized inside the
6301 class-specifier, there can be an initializer even if DECL
6302 is *not* defined. */
6303 && (!DECL_EXTERNAL (decl) || init))
6304 {
6305 if (TYPE_FOR_JAVA (type) && MAYBE_CLASS_TYPE_P (type))
6306 {
6307 tree jclass
6308 = IDENTIFIER_GLOBAL_VALUE (get_identifier ("jclass"));
6309 /* Allow libjava/prims.cc define primitive classes. */
6310 if (init != NULL_TREE
6311 || jclass == NULL_TREE
6312 || TREE_CODE (jclass) != TYPE_DECL
6313 || !POINTER_TYPE_P (TREE_TYPE (jclass))
6314 || !same_type_ignoring_top_level_qualifiers_p
6315 (type, TREE_TYPE (TREE_TYPE (jclass))))
6316 error ("Java object %qD not allocated with %<new%>", decl);
6317 init = NULL_TREE;
6318 }
6319 cleanups = make_tree_vector ();
6320 init = check_initializer (decl, init, flags, &cleanups);
6321
6322 /* Check that the initializer for a static data member was a
6323 constant. Although we check in the parser that the
6324 initializer is an integral constant expression, we do not
6325 simplify division-by-zero at the point at which it
6326 occurs. Therefore, in:
6327
6328 struct S { static const int i = 7 / 0; };
6329
6330 we issue an error at this point. It would
6331 probably be better to forbid division by zero in
6332 integral constant expressions. */
6333 if (DECL_EXTERNAL (decl) && init)
6334 {
6335 error ("%qD cannot be initialized by a non-constant expression"
6336 " when being declared", decl);
6337 DECL_INITIALIZED_IN_CLASS_P (decl) = 0;
6338 init = NULL_TREE;
6339 }
6340
6341 /* Handle:
6342
6343 [dcl.init]
6344
6345 The memory occupied by any object of static storage
6346 duration is zero-initialized at program startup before
6347 any other initialization takes place.
6348
6349 We cannot create an appropriate initializer until after
6350 the type of DECL is finalized. If DECL_INITIAL is set,
6351 then the DECL is statically initialized, and any
6352 necessary zero-initialization has already been performed. */
6353 if (TREE_STATIC (decl) && !DECL_INITIAL (decl))
6354 DECL_INITIAL (decl) = build_zero_init (TREE_TYPE (decl),
6355 /*nelts=*/NULL_TREE,
6356 /*static_storage_p=*/true);
6357 /* Remember that the initialization for this variable has
6358 taken place. */
6359 DECL_INITIALIZED_P (decl) = 1;
6360 /* This declaration is the definition of this variable,
6361 unless we are initializing a static data member within
6362 the class specifier. */
6363 if (!DECL_EXTERNAL (decl))
6364 var_definition_p = true;
6365 }
6366 /* If the variable has an array type, lay out the type, even if
6367 there is no initializer. It is valid to index through the
6368 array, and we must get TYPE_ALIGN set correctly on the array
6369 type. */
6370 else if (TREE_CODE (type) == ARRAY_TYPE)
6371 layout_type (type);
6372
6373 if (TREE_STATIC (decl)
6374 && !at_function_scope_p ()
6375 && current_function_decl == NULL)
6376 /* So decl is a global variable or a static member of a
6377 non local class. Record the types it uses
6378 so that we can decide later to emit debug info for them. */
6379 record_types_used_by_current_var_decl (decl);
6380 }
6381 else if (TREE_CODE (decl) == FIELD_DECL
6382 && TYPE_FOR_JAVA (type) && MAYBE_CLASS_TYPE_P (type))
6383 error ("non-static data member %qD has Java class type", decl);
6384
6385 /* Add this declaration to the statement-tree. This needs to happen
6386 after the call to check_initializer so that the DECL_EXPR for a
6387 reference temp is added before the DECL_EXPR for the reference itself. */
6388 if (DECL_FUNCTION_SCOPE_P (decl))
6389 add_decl_expr (decl);
6390
6391 /* Let the middle end know about variables and functions -- but not
6392 static data members in uninstantiated class templates. */
6393 if (TREE_CODE (decl) == VAR_DECL
6394 || TREE_CODE (decl) == FUNCTION_DECL)
6395 {
6396 if (TREE_CODE (decl) == VAR_DECL)
6397 {
6398 layout_var_decl (decl);
6399 maybe_commonize_var (decl);
6400 }
6401
6402 /* This needs to happen after the linkage is set. */
6403 determine_visibility (decl);
6404
6405 if (var_definition_p && TREE_STATIC (decl))
6406 {
6407 /* If a TREE_READONLY variable needs initialization
6408 at runtime, it is no longer readonly and we need to
6409 avoid MEM_READONLY_P being set on RTL created for it. */
6410 if (init)
6411 {
6412 if (TREE_READONLY (decl))
6413 TREE_READONLY (decl) = 0;
6414 was_readonly = 0;
6415 }
6416 else if (was_readonly)
6417 TREE_READONLY (decl) = 1;
6418
6419 /* Likewise if it needs destruction. */
6420 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
6421 TREE_READONLY (decl) = 0;
6422 }
6423
6424 make_rtl_for_nonlocal_decl (decl, init, asmspec);
6425
6426 /* Check for abstractness of the type. Notice that there is no
6427 need to strip array types here since the check for those types
6428 is already done within create_array_type_for_decl. */
6429 if (TREE_CODE (type) == FUNCTION_TYPE
6430 || TREE_CODE (type) == METHOD_TYPE)
6431 abstract_virtuals_error (decl, TREE_TYPE (type));
6432 else
6433 abstract_virtuals_error (decl, type);
6434
6435 if (TREE_TYPE (decl) == error_mark_node)
6436 /* No initialization required. */
6437 ;
6438 else if (TREE_CODE (decl) == FUNCTION_DECL)
6439 {
6440 if (init)
6441 {
6442 if (init == ridpointers[(int)RID_DEFAULT])
6443 {
6444 /* An out-of-class default definition is defined at
6445 the point where it is explicitly defaulted. */
6446 if (DECL_DELETED_FN (decl))
6447 maybe_explain_implicit_delete (decl);
6448 else if (DECL_INITIAL (decl) == error_mark_node)
6449 synthesize_method (decl);
6450 }
6451 else
6452 error ("function %q#D is initialized like a variable", decl);
6453 }
6454 /* else no initialization required. */
6455 }
6456 else if (DECL_EXTERNAL (decl)
6457 && ! (DECL_LANG_SPECIFIC (decl)
6458 && DECL_NOT_REALLY_EXTERN (decl)))
6459 {
6460 if (init)
6461 DECL_INITIAL (decl) = init;
6462 }
6463 /* A variable definition. */
6464 else if (DECL_FUNCTION_SCOPE_P (decl) && !TREE_STATIC (decl))
6465 /* Initialize the local variable. */
6466 initialize_local_var (decl, init);
6467
6468 /* If a variable is defined, and then a subsequent
6469 definition with external linkage is encountered, we will
6470 get here twice for the same variable. We want to avoid
6471 calling expand_static_init more than once. For variables
6472 that are not static data members, we can call
6473 expand_static_init only when we actually process the
6474 initializer. It is not legal to redeclare a static data
6475 member, so this issue does not arise in that case. */
6476 else if (var_definition_p && TREE_STATIC (decl))
6477 expand_static_init (decl, init);
6478 }
6479
6480 /* If a CLEANUP_STMT was created to destroy a temporary bound to a
6481 reference, insert it in the statement-tree now. */
6482 if (cleanups)
6483 {
6484 unsigned i; tree t;
6485 FOR_EACH_VEC_ELT (*cleanups, i, t)
6486 push_cleanup (decl, t, false);
6487 release_tree_vector (cleanups);
6488 }
6489
6490 if (was_readonly)
6491 TREE_READONLY (decl) = 1;
6492
6493 invoke_plugin_callbacks (PLUGIN_FINISH_DECL, decl);
6494 }
6495
6496 /* Returns a declaration for a VAR_DECL as if:
6497
6498 extern "C" TYPE NAME;
6499
6500 had been seen. Used to create compiler-generated global
6501 variables. */
6502
6503 static tree
6504 declare_global_var (tree name, tree type)
6505 {
6506 tree decl;
6507
6508 push_to_top_level ();
6509 decl = build_decl (input_location, VAR_DECL, name, type);
6510 TREE_PUBLIC (decl) = 1;
6511 DECL_EXTERNAL (decl) = 1;
6512 DECL_ARTIFICIAL (decl) = 1;
6513 /* If the user has explicitly declared this variable (perhaps
6514 because the code we are compiling is part of a low-level runtime
6515 library), then it is possible that our declaration will be merged
6516 with theirs by pushdecl. */
6517 decl = pushdecl (decl);
6518 cp_finish_decl (decl, NULL_TREE, false, NULL_TREE, 0);
6519 pop_from_top_level ();
6520
6521 return decl;
6522 }
6523
6524 /* Returns the type for the argument to "__cxa_atexit" (or "atexit",
6525 if "__cxa_atexit" is not being used) corresponding to the function
6526 to be called when the program exits. */
6527
6528 static tree
6529 get_atexit_fn_ptr_type (void)
6530 {
6531 tree fn_type;
6532
6533 if (!atexit_fn_ptr_type_node)
6534 {
6535 tree arg_type;
6536 if (flag_use_cxa_atexit
6537 && !targetm.cxx.use_atexit_for_cxa_atexit ())
6538 /* The parameter to "__cxa_atexit" is "void (*)(void *)". */
6539 arg_type = ptr_type_node;
6540 else
6541 /* The parameter to "atexit" is "void (*)(void)". */
6542 arg_type = NULL_TREE;
6543
6544 fn_type = build_function_type_list (void_type_node,
6545 arg_type, NULL_TREE);
6546 atexit_fn_ptr_type_node = build_pointer_type (fn_type);
6547 }
6548
6549 return atexit_fn_ptr_type_node;
6550 }
6551
6552 /* Returns a pointer to the `atexit' function. Note that if
6553 FLAG_USE_CXA_ATEXIT is nonzero, then this will actually be the new
6554 `__cxa_atexit' function specified in the IA64 C++ ABI. */
6555
6556 static tree
6557 get_atexit_node (void)
6558 {
6559 tree atexit_fndecl;
6560 tree fn_type;
6561 tree fn_ptr_type;
6562 const char *name;
6563 bool use_aeabi_atexit;
6564
6565 if (atexit_node)
6566 return atexit_node;
6567
6568 if (flag_use_cxa_atexit && !targetm.cxx.use_atexit_for_cxa_atexit ())
6569 {
6570 /* The declaration for `__cxa_atexit' is:
6571
6572 int __cxa_atexit (void (*)(void *), void *, void *)
6573
6574 We build up the argument types and then the function type
6575 itself. */
6576 tree argtype0, argtype1, argtype2;
6577
6578 use_aeabi_atexit = targetm.cxx.use_aeabi_atexit ();
6579 /* First, build the pointer-to-function type for the first
6580 argument. */
6581 fn_ptr_type = get_atexit_fn_ptr_type ();
6582 /* Then, build the rest of the argument types. */
6583 argtype2 = ptr_type_node;
6584 if (use_aeabi_atexit)
6585 {
6586 argtype1 = fn_ptr_type;
6587 argtype0 = ptr_type_node;
6588 }
6589 else
6590 {
6591 argtype1 = ptr_type_node;
6592 argtype0 = fn_ptr_type;
6593 }
6594 /* And the final __cxa_atexit type. */
6595 fn_type = build_function_type_list (integer_type_node,
6596 argtype0, argtype1, argtype2,
6597 NULL_TREE);
6598 if (use_aeabi_atexit)
6599 name = "__aeabi_atexit";
6600 else
6601 name = "__cxa_atexit";
6602 }
6603 else
6604 {
6605 /* The declaration for `atexit' is:
6606
6607 int atexit (void (*)());
6608
6609 We build up the argument types and then the function type
6610 itself. */
6611 fn_ptr_type = get_atexit_fn_ptr_type ();
6612 /* Build the final atexit type. */
6613 fn_type = build_function_type_list (integer_type_node,
6614 fn_ptr_type, NULL_TREE);
6615 name = "atexit";
6616 }
6617
6618 /* Now, build the function declaration. */
6619 push_lang_context (lang_name_c);
6620 atexit_fndecl = build_library_fn_ptr (name, fn_type);
6621 mark_used (atexit_fndecl);
6622 pop_lang_context ();
6623 atexit_node = decay_conversion (atexit_fndecl, tf_warning_or_error);
6624
6625 return atexit_node;
6626 }
6627
6628 /* Like get_atexit_node, but for thread-local cleanups. */
6629
6630 static tree
6631 get_thread_atexit_node (void)
6632 {
6633 /* The declaration for `__cxa_thread_atexit' is:
6634
6635 int __cxa_thread_atexit (void (*)(void *), void *, void *) */
6636 tree fn_type = build_function_type_list (integer_type_node,
6637 get_atexit_fn_ptr_type (),
6638 ptr_type_node, ptr_type_node,
6639 NULL_TREE);
6640
6641 /* Now, build the function declaration. */
6642 tree atexit_fndecl = build_library_fn_ptr ("__cxa_thread_atexit", fn_type);
6643 return decay_conversion (atexit_fndecl, tf_warning_or_error);
6644 }
6645
6646 /* Returns the __dso_handle VAR_DECL. */
6647
6648 static tree
6649 get_dso_handle_node (void)
6650 {
6651 if (dso_handle_node)
6652 return dso_handle_node;
6653
6654 /* Declare the variable. */
6655 dso_handle_node = declare_global_var (get_identifier ("__dso_handle"),
6656 ptr_type_node);
6657
6658 #ifdef HAVE_GAS_HIDDEN
6659 DECL_VISIBILITY (dso_handle_node) = VISIBILITY_HIDDEN;
6660 DECL_VISIBILITY_SPECIFIED (dso_handle_node) = 1;
6661 #endif
6662
6663 return dso_handle_node;
6664 }
6665
6666 /* Begin a new function with internal linkage whose job will be simply
6667 to destroy some particular variable. */
6668
6669 static GTY(()) int start_cleanup_cnt;
6670
6671 static tree
6672 start_cleanup_fn (void)
6673 {
6674 char name[32];
6675 tree fntype;
6676 tree fndecl;
6677 bool use_cxa_atexit = flag_use_cxa_atexit
6678 && !targetm.cxx.use_atexit_for_cxa_atexit ();
6679
6680 push_to_top_level ();
6681
6682 /* No need to mangle this. */
6683 push_lang_context (lang_name_c);
6684
6685 /* Build the name of the function. */
6686 sprintf (name, "__tcf_%d", start_cleanup_cnt++);
6687 /* Build the function declaration. */
6688 fntype = TREE_TYPE (get_atexit_fn_ptr_type ());
6689 fndecl = build_lang_decl (FUNCTION_DECL, get_identifier (name), fntype);
6690 /* It's a function with internal linkage, generated by the
6691 compiler. */
6692 TREE_PUBLIC (fndecl) = 0;
6693 DECL_ARTIFICIAL (fndecl) = 1;
6694 /* Make the function `inline' so that it is only emitted if it is
6695 actually needed. It is unlikely that it will be inlined, since
6696 it is only called via a function pointer, but we avoid unnecessary
6697 emissions this way. */
6698 DECL_DECLARED_INLINE_P (fndecl) = 1;
6699 DECL_INTERFACE_KNOWN (fndecl) = 1;
6700 /* Build the parameter. */
6701 if (use_cxa_atexit)
6702 {
6703 tree parmdecl;
6704
6705 parmdecl = cp_build_parm_decl (NULL_TREE, ptr_type_node);
6706 DECL_CONTEXT (parmdecl) = fndecl;
6707 TREE_USED (parmdecl) = 1;
6708 DECL_READ_P (parmdecl) = 1;
6709 DECL_ARGUMENTS (fndecl) = parmdecl;
6710 }
6711
6712 pushdecl (fndecl);
6713 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
6714
6715 pop_lang_context ();
6716
6717 return current_function_decl;
6718 }
6719
6720 /* Finish the cleanup function begun by start_cleanup_fn. */
6721
6722 static void
6723 end_cleanup_fn (void)
6724 {
6725 expand_or_defer_fn (finish_function (0));
6726
6727 pop_from_top_level ();
6728 }
6729
6730 /* Generate code to handle the destruction of DECL, an object with
6731 static storage duration. */
6732
6733 tree
6734 register_dtor_fn (tree decl)
6735 {
6736 tree cleanup;
6737 tree addr;
6738 tree compound_stmt;
6739 tree fcall;
6740 tree type;
6741 bool ob_parm, dso_parm, use_dtor;
6742 tree arg0, arg1, arg2;
6743 tree atex_node;
6744
6745 type = TREE_TYPE (decl);
6746 if (TYPE_HAS_TRIVIAL_DESTRUCTOR (type))
6747 return void_zero_node;
6748
6749 /* If we're using "__cxa_atexit" (or "__cxa_thread_atexit" or
6750 "__aeabi_atexit"), and DECL is a class object, we can just pass the
6751 destructor to "__cxa_atexit"; we don't have to build a temporary
6752 function to do the cleanup. */
6753 ob_parm = (DECL_THREAD_LOCAL_P (decl)
6754 || (flag_use_cxa_atexit
6755 && !targetm.cxx.use_atexit_for_cxa_atexit ()));
6756 dso_parm = ob_parm;
6757 use_dtor = ob_parm && CLASS_TYPE_P (type);
6758 if (use_dtor)
6759 {
6760 int idx;
6761
6762 /* Find the destructor. */
6763 idx = lookup_fnfields_1 (type, complete_dtor_identifier);
6764 gcc_assert (idx >= 0);
6765 cleanup = (*CLASSTYPE_METHOD_VEC (type))[idx];
6766 /* Make sure it is accessible. */
6767 perform_or_defer_access_check (TYPE_BINFO (type), cleanup, cleanup,
6768 tf_warning_or_error);
6769 }
6770 else
6771 {
6772 /* Call build_cleanup before we enter the anonymous function so
6773 that any access checks will be done relative to the current
6774 scope, rather than the scope of the anonymous function. */
6775 build_cleanup (decl);
6776
6777 /* Now start the function. */
6778 cleanup = start_cleanup_fn ();
6779
6780 /* Now, recompute the cleanup. It may contain SAVE_EXPRs that refer
6781 to the original function, rather than the anonymous one. That
6782 will make the back end think that nested functions are in use,
6783 which causes confusion. */
6784 push_deferring_access_checks (dk_no_check);
6785 fcall = build_cleanup (decl);
6786 pop_deferring_access_checks ();
6787
6788 /* Create the body of the anonymous function. */
6789 compound_stmt = begin_compound_stmt (BCS_FN_BODY);
6790 finish_expr_stmt (fcall);
6791 finish_compound_stmt (compound_stmt);
6792 end_cleanup_fn ();
6793 }
6794
6795 /* Call atexit with the cleanup function. */
6796 mark_used (cleanup);
6797 cleanup = build_address (cleanup);
6798
6799 if (DECL_THREAD_LOCAL_P (decl))
6800 atex_node = get_thread_atexit_node ();
6801 else
6802 atex_node = get_atexit_node ();
6803
6804 if (use_dtor)
6805 {
6806 /* We must convert CLEANUP to the type that "__cxa_atexit"
6807 expects. */
6808 cleanup = build_nop (get_atexit_fn_ptr_type (), cleanup);
6809 /* "__cxa_atexit" will pass the address of DECL to the
6810 cleanup function. */
6811 mark_used (decl);
6812 addr = build_address (decl);
6813 /* The declared type of the parameter to "__cxa_atexit" is
6814 "void *". For plain "T*", we could just let the
6815 machinery in cp_build_function_call convert it -- but if the
6816 type is "cv-qualified T *", then we need to convert it
6817 before passing it in, to avoid spurious errors. */
6818 addr = build_nop (ptr_type_node, addr);
6819 }
6820 else if (ob_parm)
6821 /* Since the cleanup functions we build ignore the address
6822 they're given, there's no reason to pass the actual address
6823 in, and, in general, it's cheaper to pass NULL than any
6824 other value. */
6825 addr = null_pointer_node;
6826
6827 if (dso_parm)
6828 arg2 = cp_build_addr_expr (get_dso_handle_node (),
6829 tf_warning_or_error);
6830 else
6831 arg2 = NULL_TREE;
6832
6833 if (ob_parm)
6834 {
6835 if (!DECL_THREAD_LOCAL_P (decl)
6836 && targetm.cxx.use_aeabi_atexit ())
6837 {
6838 arg1 = cleanup;
6839 arg0 = addr;
6840 }
6841 else
6842 {
6843 arg1 = addr;
6844 arg0 = cleanup;
6845 }
6846 }
6847 else
6848 {
6849 arg0 = cleanup;
6850 arg1 = NULL_TREE;
6851 }
6852 return cp_build_function_call_nary (atex_node, tf_warning_or_error,
6853 arg0, arg1, arg2, NULL_TREE);
6854 }
6855
6856 /* DECL is a VAR_DECL with static storage duration. INIT, if present,
6857 is its initializer. Generate code to handle the construction
6858 and destruction of DECL. */
6859
6860 static void
6861 expand_static_init (tree decl, tree init)
6862 {
6863 gcc_assert (TREE_CODE (decl) == VAR_DECL);
6864 gcc_assert (TREE_STATIC (decl));
6865
6866 /* Some variables require no dynamic initialization. */
6867 if (!init
6868 && TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
6869 return;
6870
6871 if (DECL_THREAD_LOCAL_P (decl) && DECL_GNU_TLS_P (decl)
6872 && !DECL_FUNCTION_SCOPE_P (decl))
6873 {
6874 if (init)
6875 error ("non-local variable %qD declared %<__thread%> "
6876 "needs dynamic initialization", decl);
6877 else
6878 error ("non-local variable %qD declared %<__thread%> "
6879 "has a non-trivial destructor", decl);
6880 static bool informed;
6881 if (!informed)
6882 {
6883 inform (DECL_SOURCE_LOCATION (decl),
6884 "C++11 %<thread_local%> allows dynamic initialization "
6885 "and destruction");
6886 informed = true;
6887 }
6888 return;
6889 }
6890
6891 if (DECL_FUNCTION_SCOPE_P (decl))
6892 {
6893 /* Emit code to perform this initialization but once. */
6894 tree if_stmt = NULL_TREE, inner_if_stmt = NULL_TREE;
6895 tree then_clause = NULL_TREE, inner_then_clause = NULL_TREE;
6896 tree guard, guard_addr;
6897 tree flag, begin;
6898 /* We don't need thread-safety code for thread-local vars. */
6899 bool thread_guard = (flag_threadsafe_statics
6900 && !DECL_THREAD_LOCAL_P (decl));
6901
6902 /* Emit code to perform this initialization but once. This code
6903 looks like:
6904
6905 static <type> guard;
6906 if (!guard.first_byte) {
6907 if (__cxa_guard_acquire (&guard)) {
6908 bool flag = false;
6909 try {
6910 // Do initialization.
6911 flag = true; __cxa_guard_release (&guard);
6912 // Register variable for destruction at end of program.
6913 } catch {
6914 if (!flag) __cxa_guard_abort (&guard);
6915 }
6916 }
6917
6918 Note that the `flag' variable is only set to 1 *after* the
6919 initialization is complete. This ensures that an exception,
6920 thrown during the construction, will cause the variable to
6921 reinitialized when we pass through this code again, as per:
6922
6923 [stmt.dcl]
6924
6925 If the initialization exits by throwing an exception, the
6926 initialization is not complete, so it will be tried again
6927 the next time control enters the declaration.
6928
6929 This process should be thread-safe, too; multiple threads
6930 should not be able to initialize the variable more than
6931 once. */
6932
6933 /* Create the guard variable. */
6934 guard = get_guard (decl);
6935
6936 /* This optimization isn't safe on targets with relaxed memory
6937 consistency. On such targets we force synchronization in
6938 __cxa_guard_acquire. */
6939 if (!targetm.relaxed_ordering || !thread_guard)
6940 {
6941 /* Begin the conditional initialization. */
6942 if_stmt = begin_if_stmt ();
6943 finish_if_stmt_cond (get_guard_cond (guard), if_stmt);
6944 then_clause = begin_compound_stmt (BCS_NO_SCOPE);
6945 }
6946
6947 if (thread_guard)
6948 {
6949 tree vfntype = NULL_TREE;
6950 tree acquire_name, release_name, abort_name;
6951 tree acquire_fn, release_fn, abort_fn;
6952 guard_addr = build_address (guard);
6953
6954 acquire_name = get_identifier ("__cxa_guard_acquire");
6955 release_name = get_identifier ("__cxa_guard_release");
6956 abort_name = get_identifier ("__cxa_guard_abort");
6957 acquire_fn = identifier_global_value (acquire_name);
6958 release_fn = identifier_global_value (release_name);
6959 abort_fn = identifier_global_value (abort_name);
6960 if (!acquire_fn)
6961 acquire_fn = push_library_fn
6962 (acquire_name, build_function_type_list (integer_type_node,
6963 TREE_TYPE (guard_addr),
6964 NULL_TREE),
6965 NULL_TREE);
6966 if (!release_fn || !abort_fn)
6967 vfntype = build_function_type_list (void_type_node,
6968 TREE_TYPE (guard_addr),
6969 NULL_TREE);
6970 if (!release_fn)
6971 release_fn = push_library_fn (release_name, vfntype, NULL_TREE);
6972 if (!abort_fn)
6973 abort_fn = push_library_fn (abort_name, vfntype, NULL_TREE);
6974
6975 inner_if_stmt = begin_if_stmt ();
6976 finish_if_stmt_cond (build_call_n (acquire_fn, 1, guard_addr),
6977 inner_if_stmt);
6978
6979 inner_then_clause = begin_compound_stmt (BCS_NO_SCOPE);
6980 begin = get_target_expr (boolean_false_node);
6981 flag = TARGET_EXPR_SLOT (begin);
6982
6983 TARGET_EXPR_CLEANUP (begin)
6984 = build3 (COND_EXPR, void_type_node, flag,
6985 void_zero_node,
6986 build_call_n (abort_fn, 1, guard_addr));
6987 CLEANUP_EH_ONLY (begin) = 1;
6988
6989 /* Do the initialization itself. */
6990 init = add_stmt_to_compound (begin, init);
6991 init = add_stmt_to_compound
6992 (init, build2 (MODIFY_EXPR, void_type_node, flag, boolean_true_node));
6993 init = add_stmt_to_compound
6994 (init, build_call_n (release_fn, 1, guard_addr));
6995 }
6996 else
6997 init = add_stmt_to_compound (init, set_guard (guard));
6998
6999 /* Use atexit to register a function for destroying this static
7000 variable. */
7001 init = add_stmt_to_compound (init, register_dtor_fn (decl));
7002
7003 finish_expr_stmt (init);
7004
7005 if (thread_guard)
7006 {
7007 finish_compound_stmt (inner_then_clause);
7008 finish_then_clause (inner_if_stmt);
7009 finish_if_stmt (inner_if_stmt);
7010 }
7011
7012 if (!targetm.relaxed_ordering || !thread_guard)
7013 {
7014 finish_compound_stmt (then_clause);
7015 finish_then_clause (if_stmt);
7016 finish_if_stmt (if_stmt);
7017 }
7018 }
7019 else if (DECL_THREAD_LOCAL_P (decl))
7020 tls_aggregates = tree_cons (init, decl, tls_aggregates);
7021 else
7022 static_aggregates = tree_cons (init, decl, static_aggregates);
7023 }
7024
7025 \f
7026 /* Make TYPE a complete type based on INITIAL_VALUE.
7027 Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
7028 2 if there was no information (in which case assume 0 if DO_DEFAULT),
7029 3 if the initializer list is empty (in pedantic mode). */
7030
7031 int
7032 cp_complete_array_type (tree *ptype, tree initial_value, bool do_default)
7033 {
7034 int failure;
7035 tree type, elt_type;
7036
7037 if (initial_value)
7038 {
7039 unsigned HOST_WIDE_INT i;
7040 tree value;
7041
7042 /* An array of character type can be initialized from a
7043 brace-enclosed string constant.
7044
7045 FIXME: this code is duplicated from reshape_init. Probably
7046 we should just call reshape_init here? */
7047 if (char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (*ptype)))
7048 && TREE_CODE (initial_value) == CONSTRUCTOR
7049 && !vec_safe_is_empty (CONSTRUCTOR_ELTS (initial_value)))
7050 {
7051 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (initial_value);
7052 tree value = (*v)[0].value;
7053
7054 if (TREE_CODE (value) == STRING_CST
7055 && v->length () == 1)
7056 initial_value = value;
7057 }
7058
7059 /* If any of the elements are parameter packs, we can't actually
7060 complete this type now because the array size is dependent. */
7061 if (TREE_CODE (initial_value) == CONSTRUCTOR)
7062 {
7063 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (initial_value),
7064 i, value)
7065 {
7066 if (PACK_EXPANSION_P (value))
7067 return 0;
7068 }
7069 }
7070 }
7071
7072 failure = complete_array_type (ptype, initial_value, do_default);
7073
7074 /* We can create the array before the element type is complete, which
7075 means that we didn't have these two bits set in the original type
7076 either. In completing the type, we are expected to propagate these
7077 bits. See also complete_type which does the same thing for arrays
7078 of fixed size. */
7079 type = *ptype;
7080 if (TYPE_DOMAIN (type))
7081 {
7082 elt_type = TREE_TYPE (type);
7083 TYPE_NEEDS_CONSTRUCTING (type) = TYPE_NEEDS_CONSTRUCTING (elt_type);
7084 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
7085 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (elt_type);
7086 }
7087
7088 return failure;
7089 }
7090
7091 /* As above, but either give an error or reject zero-size arrays, depending
7092 on COMPLAIN. */
7093
7094 int
7095 cp_complete_array_type_or_error (tree *ptype, tree initial_value,
7096 bool do_default, tsubst_flags_t complain)
7097 {
7098 int failure;
7099 bool sfinae = !(complain & tf_error);
7100 /* In SFINAE context we can't be lenient about zero-size arrays. */
7101 if (sfinae)
7102 ++pedantic;
7103 failure = cp_complete_array_type (ptype, initial_value, do_default);
7104 if (sfinae)
7105 --pedantic;
7106 if (failure)
7107 {
7108 if (sfinae)
7109 /* Not an error. */;
7110 else if (failure == 1)
7111 error ("initializer fails to determine size of %qT", *ptype);
7112 else if (failure == 2)
7113 {
7114 if (do_default)
7115 error ("array size missing in %qT", *ptype);
7116 }
7117 else if (failure == 3)
7118 error ("zero-size array %qT", *ptype);
7119 *ptype = error_mark_node;
7120 }
7121 return failure;
7122 }
7123 \f
7124 /* Return zero if something is declared to be a member of type
7125 CTYPE when in the context of CUR_TYPE. STRING is the error
7126 message to print in that case. Otherwise, quietly return 1. */
7127
7128 static int
7129 member_function_or_else (tree ctype, tree cur_type, enum overload_flags flags)
7130 {
7131 if (ctype && ctype != cur_type)
7132 {
7133 if (flags == DTOR_FLAG)
7134 error ("destructor for alien class %qT cannot be a member", ctype);
7135 else
7136 error ("constructor for alien class %qT cannot be a member", ctype);
7137 return 0;
7138 }
7139 return 1;
7140 }
7141 \f
7142 /* Subroutine of `grokdeclarator'. */
7143
7144 /* Generate errors possibly applicable for a given set of specifiers.
7145 This is for ARM $7.1.2. */
7146
7147 static void
7148 bad_specifiers (tree object,
7149 enum bad_spec_place type,
7150 int virtualp,
7151 int quals,
7152 int inlinep,
7153 int friendp,
7154 int raises)
7155 {
7156 switch (type)
7157 {
7158 case BSP_VAR:
7159 if (virtualp)
7160 error ("%qD declared as a %<virtual%> variable", object);
7161 if (inlinep)
7162 error ("%qD declared as an %<inline%> variable", object);
7163 if (quals)
7164 error ("%<const%> and %<volatile%> function specifiers on "
7165 "%qD invalid in variable declaration", object);
7166 break;
7167 case BSP_PARM:
7168 if (virtualp)
7169 error ("%qD declared as a %<virtual%> parameter", object);
7170 if (inlinep)
7171 error ("%qD declared as an %<inline%> parameter", object);
7172 if (quals)
7173 error ("%<const%> and %<volatile%> function specifiers on "
7174 "%qD invalid in parameter declaration", object);
7175 break;
7176 case BSP_TYPE:
7177 if (virtualp)
7178 error ("%qD declared as a %<virtual%> type", object);
7179 if (inlinep)
7180 error ("%qD declared as an %<inline%> type", object);
7181 if (quals)
7182 error ("%<const%> and %<volatile%> function specifiers on "
7183 "%qD invalid in type declaration", object);
7184 break;
7185 case BSP_FIELD:
7186 if (virtualp)
7187 error ("%qD declared as a %<virtual%> field", object);
7188 if (inlinep)
7189 error ("%qD declared as an %<inline%> field", object);
7190 if (quals)
7191 error ("%<const%> and %<volatile%> function specifiers on "
7192 "%qD invalid in field declaration", object);
7193 break;
7194 default:
7195 gcc_unreachable();
7196 }
7197 if (friendp)
7198 error ("%q+D declared as a friend", object);
7199 if (raises
7200 && (TREE_CODE (object) == TYPE_DECL
7201 || (!TYPE_PTRFN_P (TREE_TYPE (object))
7202 && !TYPE_REFFN_P (TREE_TYPE (object))
7203 && !TYPE_PTRMEMFUNC_P (TREE_TYPE (object)))))
7204 error ("%q+D declared with an exception specification", object);
7205 }
7206
7207 /* DECL is a member function or static data member and is presently
7208 being defined. Check that the definition is taking place in a
7209 valid namespace. */
7210
7211 static void
7212 check_class_member_definition_namespace (tree decl)
7213 {
7214 /* These checks only apply to member functions and static data
7215 members. */
7216 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
7217 || TREE_CODE (decl) == VAR_DECL);
7218 /* We check for problems with specializations in pt.c in
7219 check_specialization_namespace, where we can issue better
7220 diagnostics. */
7221 if (processing_specialization)
7222 return;
7223 /* There are no restrictions on the placement of
7224 explicit instantiations. */
7225 if (processing_explicit_instantiation)
7226 return;
7227 /* [class.mfct]
7228
7229 A member function definition that appears outside of the
7230 class definition shall appear in a namespace scope enclosing
7231 the class definition.
7232
7233 [class.static.data]
7234
7235 The definition for a static data member shall appear in a
7236 namespace scope enclosing the member's class definition. */
7237 if (!is_ancestor (current_namespace, DECL_CONTEXT (decl)))
7238 permerror (input_location, "definition of %qD is not in namespace enclosing %qT",
7239 decl, DECL_CONTEXT (decl));
7240 }
7241
7242 /* Build a PARM_DECL for the "this" parameter. TYPE is the
7243 METHOD_TYPE for a non-static member function; QUALS are the
7244 cv-qualifiers that apply to the function. */
7245
7246 tree
7247 build_this_parm (tree type, cp_cv_quals quals)
7248 {
7249 tree this_type;
7250 tree qual_type;
7251 tree parm;
7252 cp_cv_quals this_quals;
7253
7254 if (CLASS_TYPE_P (type))
7255 {
7256 this_type
7257 = cp_build_qualified_type (type, quals & ~TYPE_QUAL_RESTRICT);
7258 this_type = build_pointer_type (this_type);
7259 }
7260 else
7261 this_type = type_of_this_parm (type);
7262 /* The `this' parameter is implicitly `const'; it cannot be
7263 assigned to. */
7264 this_quals = (quals & TYPE_QUAL_RESTRICT) | TYPE_QUAL_CONST;
7265 qual_type = cp_build_qualified_type (this_type, this_quals);
7266 parm = build_artificial_parm (this_identifier, qual_type);
7267 cp_apply_type_quals_to_decl (this_quals, parm);
7268 return parm;
7269 }
7270
7271 /* DECL is a static member function. Complain if it was declared
7272 with function-cv-quals. */
7273
7274 static void
7275 check_static_quals (tree decl, cp_cv_quals quals)
7276 {
7277 if (quals != TYPE_UNQUALIFIED)
7278 error ("static member function %q#D declared with type qualifiers",
7279 decl);
7280 }
7281
7282 /* CTYPE is class type, or null if non-class.
7283 TYPE is type this FUNCTION_DECL should have, either FUNCTION_TYPE
7284 or METHOD_TYPE.
7285 DECLARATOR is the function's name.
7286 PARMS is a chain of PARM_DECLs for the function.
7287 VIRTUALP is truthvalue of whether the function is virtual or not.
7288 FLAGS are to be passed through to `grokclassfn'.
7289 QUALS are qualifiers indicating whether the function is `const'
7290 or `volatile'.
7291 RAISES is a list of exceptions that this function can raise.
7292 CHECK is 1 if we must find this method in CTYPE, 0 if we should
7293 not look, and -1 if we should not call `grokclassfn' at all.
7294
7295 SFK is the kind of special function (if any) for the new function.
7296
7297 Returns `NULL_TREE' if something goes wrong, after issuing
7298 applicable error messages. */
7299
7300 static tree
7301 grokfndecl (tree ctype,
7302 tree type,
7303 tree declarator,
7304 tree parms,
7305 tree orig_declarator,
7306 int virtualp,
7307 enum overload_flags flags,
7308 cp_cv_quals quals,
7309 tree raises,
7310 int check,
7311 int friendp,
7312 int publicp,
7313 int inlinep,
7314 special_function_kind sfk,
7315 bool funcdef_flag,
7316 int template_count,
7317 tree in_namespace,
7318 tree* attrlist,
7319 location_t location)
7320 {
7321 tree decl;
7322 int staticp = ctype && TREE_CODE (type) == FUNCTION_TYPE;
7323 tree t;
7324
7325 if (raises)
7326 type = build_exception_variant (type, raises);
7327
7328 decl = build_lang_decl (FUNCTION_DECL, declarator, type);
7329
7330 /* If we have an explicit location, use it, otherwise use whatever
7331 build_lang_decl used (probably input_location). */
7332 if (location != UNKNOWN_LOCATION)
7333 DECL_SOURCE_LOCATION (decl) = location;
7334
7335 if (TREE_CODE (type) == METHOD_TYPE)
7336 {
7337 tree parm;
7338 parm = build_this_parm (type, quals);
7339 DECL_CHAIN (parm) = parms;
7340 parms = parm;
7341 }
7342 DECL_ARGUMENTS (decl) = parms;
7343 for (t = parms; t; t = DECL_CHAIN (t))
7344 DECL_CONTEXT (t) = decl;
7345 /* Propagate volatile out from type to decl. */
7346 if (TYPE_VOLATILE (type))
7347 TREE_THIS_VOLATILE (decl) = 1;
7348
7349 /* Setup decl according to sfk. */
7350 switch (sfk)
7351 {
7352 case sfk_constructor:
7353 case sfk_copy_constructor:
7354 case sfk_move_constructor:
7355 DECL_CONSTRUCTOR_P (decl) = 1;
7356 break;
7357 case sfk_destructor:
7358 DECL_DESTRUCTOR_P (decl) = 1;
7359 break;
7360 default:
7361 break;
7362 }
7363
7364 /* If pointers to member functions use the least significant bit to
7365 indicate whether a function is virtual, ensure a pointer
7366 to this function will have that bit clear. */
7367 if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_pfn
7368 && TREE_CODE (type) == METHOD_TYPE
7369 && DECL_ALIGN (decl) < 2 * BITS_PER_UNIT)
7370 DECL_ALIGN (decl) = 2 * BITS_PER_UNIT;
7371
7372 if (friendp
7373 && TREE_CODE (orig_declarator) == TEMPLATE_ID_EXPR)
7374 {
7375 if (funcdef_flag)
7376 error
7377 ("defining explicit specialization %qD in friend declaration",
7378 orig_declarator);
7379 else
7380 {
7381 tree fns = TREE_OPERAND (orig_declarator, 0);
7382 tree args = TREE_OPERAND (orig_declarator, 1);
7383
7384 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
7385 {
7386 /* Something like `template <class T> friend void f<T>()'. */
7387 error ("invalid use of template-id %qD in declaration "
7388 "of primary template",
7389 orig_declarator);
7390 return NULL_TREE;
7391 }
7392
7393
7394 /* A friend declaration of the form friend void f<>(). Record
7395 the information in the TEMPLATE_ID_EXPR. */
7396 SET_DECL_IMPLICIT_INSTANTIATION (decl);
7397
7398 if (TREE_CODE (fns) == COMPONENT_REF)
7399 {
7400 /* Due to bison parser ickiness, we will have already looked
7401 up an operator_name or PFUNCNAME within the current class
7402 (see template_id in parse.y). If the current class contains
7403 such a name, we'll get a COMPONENT_REF here. Undo that. */
7404
7405 gcc_assert (TREE_TYPE (TREE_OPERAND (fns, 0))
7406 == current_class_type);
7407 fns = TREE_OPERAND (fns, 1);
7408 }
7409 gcc_assert (TREE_CODE (fns) == IDENTIFIER_NODE
7410 || TREE_CODE (fns) == OVERLOAD);
7411 DECL_TEMPLATE_INFO (decl) = build_template_info (fns, args);
7412
7413 for (t = TYPE_ARG_TYPES (TREE_TYPE (decl)); t; t = TREE_CHAIN (t))
7414 if (TREE_PURPOSE (t)
7415 && TREE_CODE (TREE_PURPOSE (t)) == DEFAULT_ARG)
7416 {
7417 error ("default arguments are not allowed in declaration "
7418 "of friend template specialization %qD",
7419 decl);
7420 return NULL_TREE;
7421 }
7422
7423 if (inlinep)
7424 {
7425 error ("%<inline%> is not allowed in declaration of friend "
7426 "template specialization %qD",
7427 decl);
7428 return NULL_TREE;
7429 }
7430 }
7431 }
7432
7433 /* If this decl has namespace scope, set that up. */
7434 if (in_namespace)
7435 set_decl_namespace (decl, in_namespace, friendp);
7436 else if (!ctype)
7437 DECL_CONTEXT (decl) = FROB_CONTEXT (current_decl_namespace ());
7438
7439 /* `main' and builtins have implicit 'C' linkage. */
7440 if ((MAIN_NAME_P (declarator)
7441 || (IDENTIFIER_LENGTH (declarator) > 10
7442 && IDENTIFIER_POINTER (declarator)[0] == '_'
7443 && IDENTIFIER_POINTER (declarator)[1] == '_'
7444 && strncmp (IDENTIFIER_POINTER (declarator)+2, "builtin_", 8) == 0))
7445 && current_lang_name == lang_name_cplusplus
7446 && ctype == NULL_TREE
7447 && DECL_FILE_SCOPE_P (decl))
7448 SET_DECL_LANGUAGE (decl, lang_c);
7449
7450 /* Should probably propagate const out from type to decl I bet (mrs). */
7451 if (staticp)
7452 {
7453 DECL_STATIC_FUNCTION_P (decl) = 1;
7454 DECL_CONTEXT (decl) = ctype;
7455 }
7456
7457 if (ctype)
7458 {
7459 DECL_CONTEXT (decl) = ctype;
7460 if (funcdef_flag)
7461 check_class_member_definition_namespace (decl);
7462 }
7463
7464 if (ctype == NULL_TREE && DECL_MAIN_P (decl))
7465 {
7466 if (PROCESSING_REAL_TEMPLATE_DECL_P())
7467 error ("cannot declare %<::main%> to be a template");
7468 if (inlinep)
7469 error ("cannot declare %<::main%> to be inline");
7470 if (!publicp)
7471 error ("cannot declare %<::main%> to be static");
7472 inlinep = 0;
7473 publicp = 1;
7474 }
7475
7476 /* Members of anonymous types and local classes have no linkage; make
7477 them internal. If a typedef is made later, this will be changed. */
7478 if (ctype && (TYPE_ANONYMOUS_P (ctype)
7479 || decl_function_context (TYPE_MAIN_DECL (ctype))))
7480 publicp = 0;
7481
7482 if (publicp && cxx_dialect == cxx98)
7483 {
7484 /* [basic.link]: A name with no linkage (notably, the name of a class
7485 or enumeration declared in a local scope) shall not be used to
7486 declare an entity with linkage.
7487
7488 DR 757 relaxes this restriction for C++0x. */
7489 t = no_linkage_check (TREE_TYPE (decl),
7490 /*relaxed_p=*/false);
7491 if (t)
7492 {
7493 if (TYPE_ANONYMOUS_P (t))
7494 {
7495 if (DECL_EXTERN_C_P (decl))
7496 /* Allow this; it's pretty common in C. */;
7497 else
7498 {
7499 permerror (input_location, "anonymous type with no linkage "
7500 "used to declare function %q#D with linkage",
7501 decl);
7502 if (DECL_ORIGINAL_TYPE (TYPE_NAME (t)))
7503 permerror (input_location, "%q+#D does not refer to the unqualified "
7504 "type, so it is not used for linkage",
7505 TYPE_NAME (t));
7506 }
7507 }
7508 else
7509 permerror (input_location, "type %qT with no linkage used to "
7510 "declare function %q#D with linkage", t, decl);
7511 }
7512 }
7513
7514 TREE_PUBLIC (decl) = publicp;
7515 if (! publicp)
7516 {
7517 DECL_INTERFACE_KNOWN (decl) = 1;
7518 DECL_NOT_REALLY_EXTERN (decl) = 1;
7519 }
7520
7521 /* If the declaration was declared inline, mark it as such. */
7522 if (inlinep)
7523 DECL_DECLARED_INLINE_P (decl) = 1;
7524 if (inlinep & 2)
7525 DECL_DECLARED_CONSTEXPR_P (decl) = true;
7526
7527 DECL_EXTERNAL (decl) = 1;
7528 if (quals && TREE_CODE (type) == FUNCTION_TYPE)
7529 {
7530 error (ctype
7531 ? G_("static member function %qD cannot have cv-qualifier")
7532 : G_("non-member function %qD cannot have cv-qualifier"),
7533 decl);
7534 quals = TYPE_UNQUALIFIED;
7535 }
7536
7537 if (IDENTIFIER_OPNAME_P (DECL_NAME (decl))
7538 && !grok_op_properties (decl, /*complain=*/true))
7539 return NULL_TREE;
7540 else if (UDLIT_OPER_P (DECL_NAME (decl)))
7541 {
7542 bool long_long_unsigned_p;
7543 bool long_double_p;
7544 const char *suffix = NULL;
7545 /* [over.literal]/6: Literal operators shall not have C linkage. */
7546 if (DECL_LANGUAGE (decl) == lang_c)
7547 {
7548 error ("literal operator with C linkage");
7549 return NULL_TREE;
7550 }
7551
7552 if (DECL_NAMESPACE_SCOPE_P (decl))
7553 {
7554 if (!check_literal_operator_args (decl, &long_long_unsigned_p,
7555 &long_double_p))
7556 {
7557 error ("%qD has invalid argument list", decl);
7558 return NULL_TREE;
7559 }
7560
7561 suffix = UDLIT_OP_SUFFIX (DECL_NAME (decl));
7562 if (long_long_unsigned_p)
7563 {
7564 if (cpp_interpret_int_suffix (parse_in, suffix, strlen (suffix)))
7565 warning (0, "integer suffix %<%s%>"
7566 " shadowed by implementation", suffix);
7567 }
7568 else if (long_double_p)
7569 {
7570 if (cpp_interpret_float_suffix (parse_in, suffix, strlen (suffix)))
7571 warning (0, "floating point suffix %<%s%>"
7572 " shadowed by implementation", suffix);
7573 }
7574 }
7575 else
7576 {
7577 error ("%qD must be a non-member function", decl);
7578 return NULL_TREE;
7579 }
7580 }
7581
7582 if (funcdef_flag)
7583 /* Make the init_value nonzero so pushdecl knows this is not
7584 tentative. error_mark_node is replaced later with the BLOCK. */
7585 DECL_INITIAL (decl) = error_mark_node;
7586
7587 if (TYPE_NOTHROW_P (type) || nothrow_libfn_p (decl))
7588 TREE_NOTHROW (decl) = 1;
7589
7590 /* Caller will do the rest of this. */
7591 if (check < 0)
7592 return decl;
7593
7594 if (ctype != NULL_TREE)
7595 grokclassfn (ctype, decl, flags);
7596
7597 /* 12.4/3 */
7598 if (cxx_dialect >= cxx0x
7599 && DECL_DESTRUCTOR_P (decl)
7600 && !TYPE_BEING_DEFINED (DECL_CONTEXT (decl))
7601 && !processing_template_decl)
7602 deduce_noexcept_on_destructor (decl);
7603
7604 decl = check_explicit_specialization (orig_declarator, decl,
7605 template_count,
7606 2 * funcdef_flag +
7607 4 * (friendp != 0));
7608 if (decl == error_mark_node)
7609 return NULL_TREE;
7610
7611 if (DECL_STATIC_FUNCTION_P (decl))
7612 check_static_quals (decl, quals);
7613
7614 if (attrlist)
7615 {
7616 cplus_decl_attributes (&decl, *attrlist, 0);
7617 *attrlist = NULL_TREE;
7618 }
7619
7620 /* Check main's type after attributes have been applied. */
7621 if (ctype == NULL_TREE && DECL_MAIN_P (decl))
7622 {
7623 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
7624 integer_type_node))
7625 {
7626 tree oldtypeargs = TYPE_ARG_TYPES (TREE_TYPE (decl));
7627 tree newtype;
7628 error ("%<::main%> must return %<int%>");
7629 newtype = build_function_type (integer_type_node, oldtypeargs);
7630 TREE_TYPE (decl) = newtype;
7631 }
7632 if (warn_main)
7633 check_main_parameter_types (decl);
7634 }
7635
7636 if (ctype != NULL_TREE
7637 && (! TYPE_FOR_JAVA (ctype) || check_java_method (decl))
7638 && check)
7639 {
7640 tree old_decl = check_classfn (ctype, decl,
7641 (processing_template_decl
7642 > template_class_depth (ctype))
7643 ? current_template_parms
7644 : NULL_TREE);
7645
7646 if (old_decl == error_mark_node)
7647 return NULL_TREE;
7648
7649 if (old_decl)
7650 {
7651 tree ok;
7652 tree pushed_scope;
7653
7654 if (TREE_CODE (old_decl) == TEMPLATE_DECL)
7655 /* Because grokfndecl is always supposed to return a
7656 FUNCTION_DECL, we pull out the DECL_TEMPLATE_RESULT
7657 here. We depend on our callers to figure out that its
7658 really a template that's being returned. */
7659 old_decl = DECL_TEMPLATE_RESULT (old_decl);
7660
7661 if (DECL_STATIC_FUNCTION_P (old_decl)
7662 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE)
7663 {
7664 /* Remove the `this' parm added by grokclassfn. */
7665 revert_static_member_fn (decl);
7666 check_static_quals (decl, quals);
7667 }
7668 if (DECL_ARTIFICIAL (old_decl))
7669 {
7670 error ("definition of implicitly-declared %qD", old_decl);
7671 return NULL_TREE;
7672 }
7673 else if (DECL_DEFAULTED_FN (old_decl))
7674 {
7675 error ("definition of explicitly-defaulted %q+D", decl);
7676 error ("%q+#D explicitly defaulted here", old_decl);
7677 return NULL_TREE;
7678 }
7679
7680 /* Since we've smashed OLD_DECL to its
7681 DECL_TEMPLATE_RESULT, we must do the same to DECL. */
7682 if (TREE_CODE (decl) == TEMPLATE_DECL)
7683 decl = DECL_TEMPLATE_RESULT (decl);
7684
7685 /* Attempt to merge the declarations. This can fail, in
7686 the case of some invalid specialization declarations. */
7687 pushed_scope = push_scope (ctype);
7688 ok = duplicate_decls (decl, old_decl, friendp);
7689 if (pushed_scope)
7690 pop_scope (pushed_scope);
7691 if (!ok)
7692 {
7693 error ("no %q#D member function declared in class %qT",
7694 decl, ctype);
7695 return NULL_TREE;
7696 }
7697 return old_decl;
7698 }
7699 }
7700
7701 if (DECL_CONSTRUCTOR_P (decl) && !grok_ctor_properties (ctype, decl))
7702 return NULL_TREE;
7703
7704 if (ctype == NULL_TREE || check)
7705 return decl;
7706
7707 if (virtualp)
7708 DECL_VIRTUAL_P (decl) = 1;
7709
7710 return decl;
7711 }
7712
7713 /* decl is a FUNCTION_DECL.
7714 specifiers are the parsed virt-specifiers.
7715
7716 Set flags to reflect the virt-specifiers.
7717
7718 Returns decl. */
7719
7720 static tree
7721 set_virt_specifiers (tree decl, cp_virt_specifiers specifiers)
7722 {
7723 if (decl == NULL_TREE)
7724 return decl;
7725 if (specifiers & VIRT_SPEC_OVERRIDE)
7726 DECL_OVERRIDE_P (decl) = 1;
7727 if (specifiers & VIRT_SPEC_FINAL)
7728 DECL_FINAL_P (decl) = 1;
7729 return decl;
7730 }
7731
7732 /* DECL is a VAR_DECL for a static data member. Set flags to reflect
7733 the linkage that DECL will receive in the object file. */
7734
7735 static void
7736 set_linkage_for_static_data_member (tree decl)
7737 {
7738 /* A static data member always has static storage duration and
7739 external linkage. Note that static data members are forbidden in
7740 local classes -- the only situation in which a class has
7741 non-external linkage. */
7742 TREE_PUBLIC (decl) = 1;
7743 TREE_STATIC (decl) = 1;
7744 /* For non-template classes, static data members are always put
7745 out in exactly those files where they are defined, just as
7746 with ordinary namespace-scope variables. */
7747 if (!processing_template_decl)
7748 DECL_INTERFACE_KNOWN (decl) = 1;
7749 }
7750
7751 /* Create a VAR_DECL named NAME with the indicated TYPE.
7752
7753 If SCOPE is non-NULL, it is the class type or namespace containing
7754 the variable. If SCOPE is NULL, the variable should is created in
7755 the innermost enclosings scope. */
7756
7757 static tree
7758 grokvardecl (tree type,
7759 tree name,
7760 const cp_decl_specifier_seq *declspecs,
7761 int initialized,
7762 int constp,
7763 tree scope)
7764 {
7765 tree decl;
7766 tree explicit_scope;
7767
7768 gcc_assert (!name || TREE_CODE (name) == IDENTIFIER_NODE);
7769
7770 /* Compute the scope in which to place the variable, but remember
7771 whether or not that scope was explicitly specified by the user. */
7772 explicit_scope = scope;
7773 if (!scope)
7774 {
7775 /* An explicit "extern" specifier indicates a namespace-scope
7776 variable. */
7777 if (declspecs->storage_class == sc_extern)
7778 scope = current_decl_namespace ();
7779 else if (!at_function_scope_p ())
7780 scope = current_scope ();
7781 }
7782
7783 if (scope
7784 && (/* If the variable is a namespace-scope variable declared in a
7785 template, we need DECL_LANG_SPECIFIC. */
7786 (TREE_CODE (scope) == NAMESPACE_DECL && processing_template_decl)
7787 /* Similarly for namespace-scope variables with language linkage
7788 other than C++. */
7789 || (TREE_CODE (scope) == NAMESPACE_DECL
7790 && current_lang_name != lang_name_cplusplus)
7791 /* Similarly for static data members. */
7792 || TYPE_P (scope)))
7793 decl = build_lang_decl (VAR_DECL, name, type);
7794 else
7795 decl = build_decl (input_location, VAR_DECL, name, type);
7796
7797 if (explicit_scope && TREE_CODE (explicit_scope) == NAMESPACE_DECL)
7798 set_decl_namespace (decl, explicit_scope, 0);
7799 else
7800 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
7801
7802 if (declspecs->storage_class == sc_extern)
7803 {
7804 DECL_THIS_EXTERN (decl) = 1;
7805 DECL_EXTERNAL (decl) = !initialized;
7806 }
7807
7808 if (DECL_CLASS_SCOPE_P (decl))
7809 {
7810 set_linkage_for_static_data_member (decl);
7811 /* This function is only called with out-of-class definitions. */
7812 DECL_EXTERNAL (decl) = 0;
7813 check_class_member_definition_namespace (decl);
7814 }
7815 /* At top level, either `static' or no s.c. makes a definition
7816 (perhaps tentative), and absence of `static' makes it public. */
7817 else if (toplevel_bindings_p ())
7818 {
7819 TREE_PUBLIC (decl) = (declspecs->storage_class != sc_static
7820 && (DECL_THIS_EXTERN (decl) || ! constp));
7821 TREE_STATIC (decl) = ! DECL_EXTERNAL (decl);
7822 }
7823 /* Not at top level, only `static' makes a static definition. */
7824 else
7825 {
7826 TREE_STATIC (decl) = declspecs->storage_class == sc_static;
7827 TREE_PUBLIC (decl) = DECL_EXTERNAL (decl);
7828 }
7829
7830 if (decl_spec_seq_has_spec_p (declspecs, ds_thread))
7831 {
7832 DECL_TLS_MODEL (decl) = decl_default_tls_model (decl);
7833 if (declspecs->gnu_thread_keyword_p)
7834 DECL_GNU_TLS_P (decl) = true;
7835 }
7836
7837 /* If the type of the decl has no linkage, make sure that we'll
7838 notice that in mark_used. */
7839 if (cxx_dialect > cxx98
7840 && decl_linkage (decl) != lk_none
7841 && DECL_LANG_SPECIFIC (decl) == NULL
7842 && !DECL_EXTERN_C_P (decl)
7843 && no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/false))
7844 retrofit_lang_decl (decl);
7845
7846 if (TREE_PUBLIC (decl))
7847 {
7848 /* [basic.link]: A name with no linkage (notably, the name of a class
7849 or enumeration declared in a local scope) shall not be used to
7850 declare an entity with linkage.
7851
7852 DR 757 relaxes this restriction for C++0x. */
7853 tree t = (cxx_dialect > cxx98 ? NULL_TREE
7854 : no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/false));
7855 if (t)
7856 {
7857 if (TYPE_ANONYMOUS_P (t))
7858 {
7859 if (DECL_EXTERN_C_P (decl))
7860 /* Allow this; it's pretty common in C. */
7861 ;
7862 else
7863 {
7864 /* DRs 132, 319 and 389 seem to indicate types with
7865 no linkage can only be used to declare extern "C"
7866 entities. Since it's not always an error in the
7867 ISO C++ 90 Standard, we only issue a warning. */
7868 warning (0, "anonymous type with no linkage used to declare "
7869 "variable %q#D with linkage", decl);
7870 if (DECL_ORIGINAL_TYPE (TYPE_NAME (t)))
7871 warning (0, "%q+#D does not refer to the unqualified "
7872 "type, so it is not used for linkage",
7873 TYPE_NAME (t));
7874 }
7875 }
7876 else
7877 warning (0, "type %qT with no linkage used to declare variable "
7878 "%q#D with linkage", t, decl);
7879 }
7880 }
7881 else
7882 DECL_INTERFACE_KNOWN (decl) = 1;
7883
7884 return decl;
7885 }
7886
7887 /* Create and return a canonical pointer to member function type, for
7888 TYPE, which is a POINTER_TYPE to a METHOD_TYPE. */
7889
7890 tree
7891 build_ptrmemfunc_type (tree type)
7892 {
7893 tree field, fields;
7894 tree t;
7895 tree unqualified_variant = NULL_TREE;
7896
7897 if (type == error_mark_node)
7898 return type;
7899
7900 /* If a canonical type already exists for this type, use it. We use
7901 this method instead of type_hash_canon, because it only does a
7902 simple equality check on the list of field members. */
7903
7904 if ((t = TYPE_GET_PTRMEMFUNC_TYPE (type)))
7905 return t;
7906
7907 /* Make sure that we always have the unqualified pointer-to-member
7908 type first. */
7909 if (cp_type_quals (type) != TYPE_UNQUALIFIED)
7910 unqualified_variant
7911 = build_ptrmemfunc_type (TYPE_MAIN_VARIANT (type));
7912
7913 t = make_class_type (RECORD_TYPE);
7914 xref_basetypes (t, NULL_TREE);
7915
7916 /* Let the front end know this is a pointer to member function... */
7917 TYPE_PTRMEMFUNC_FLAG (t) = 1;
7918 /* ... and not really a class type. */
7919 SET_CLASS_TYPE_P (t, 0);
7920
7921 field = build_decl (input_location, FIELD_DECL, pfn_identifier, type);
7922 fields = field;
7923
7924 field = build_decl (input_location, FIELD_DECL, delta_identifier,
7925 delta_type_node);
7926 DECL_CHAIN (field) = fields;
7927 fields = field;
7928
7929 finish_builtin_struct (t, "__ptrmemfunc_type", fields, ptr_type_node);
7930
7931 /* Zap out the name so that the back end will give us the debugging
7932 information for this anonymous RECORD_TYPE. */
7933 TYPE_NAME (t) = NULL_TREE;
7934
7935 /* If this is not the unqualified form of this pointer-to-member
7936 type, set the TYPE_MAIN_VARIANT for this type to be the
7937 unqualified type. Since they are actually RECORD_TYPEs that are
7938 not variants of each other, we must do this manually.
7939 As we just built a new type there is no need to do yet another copy. */
7940 if (cp_type_quals (type) != TYPE_UNQUALIFIED)
7941 {
7942 int type_quals = cp_type_quals (type);
7943 TYPE_READONLY (t) = (type_quals & TYPE_QUAL_CONST) != 0;
7944 TYPE_VOLATILE (t) = (type_quals & TYPE_QUAL_VOLATILE) != 0;
7945 TYPE_RESTRICT (t) = (type_quals & TYPE_QUAL_RESTRICT) != 0;
7946 TYPE_MAIN_VARIANT (t) = unqualified_variant;
7947 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (unqualified_variant);
7948 TYPE_NEXT_VARIANT (unqualified_variant) = t;
7949 TREE_TYPE (TYPE_BINFO (t)) = t;
7950 }
7951
7952 /* Cache this pointer-to-member type so that we can find it again
7953 later. */
7954 TYPE_SET_PTRMEMFUNC_TYPE (type, t);
7955
7956 if (TYPE_STRUCTURAL_EQUALITY_P (type))
7957 SET_TYPE_STRUCTURAL_EQUALITY (t);
7958 else if (TYPE_CANONICAL (type) != type)
7959 TYPE_CANONICAL (t) = build_ptrmemfunc_type (TYPE_CANONICAL (type));
7960
7961 return t;
7962 }
7963
7964 /* Create and return a pointer to data member type. */
7965
7966 tree
7967 build_ptrmem_type (tree class_type, tree member_type)
7968 {
7969 if (TREE_CODE (member_type) == METHOD_TYPE)
7970 {
7971 cp_cv_quals quals = type_memfn_quals (member_type);
7972 member_type = build_memfn_type (member_type, class_type, quals);
7973 return build_ptrmemfunc_type (build_pointer_type (member_type));
7974 }
7975 else
7976 {
7977 gcc_assert (TREE_CODE (member_type) != FUNCTION_TYPE);
7978 return build_offset_type (class_type, member_type);
7979 }
7980 }
7981
7982 /* DECL is a VAR_DECL defined in-class, whose TYPE is also given.
7983 Check to see that the definition is valid. Issue appropriate error
7984 messages. Return 1 if the definition is particularly bad, or 0
7985 otherwise. */
7986
7987 static int
7988 check_static_variable_definition (tree decl, tree type)
7989 {
7990 /* Can't check yet if we don't know the type. */
7991 if (dependent_type_p (type))
7992 return 0;
7993 /* If DECL is declared constexpr, we'll do the appropriate checks
7994 in check_initializer. */
7995 if (DECL_P (decl) && DECL_DECLARED_CONSTEXPR_P (decl))
7996 return 0;
7997 else if (cxx_dialect >= cxx0x && !INTEGRAL_OR_ENUMERATION_TYPE_P (type))
7998 {
7999 if (!COMPLETE_TYPE_P (type))
8000 error ("in-class initialization of static data member %q#D of "
8001 "incomplete type", decl);
8002 else if (literal_type_p (type))
8003 permerror (input_location,
8004 "%<constexpr%> needed for in-class initialization of "
8005 "static data member %q#D of non-integral type", decl);
8006 else
8007 error ("in-class initialization of static data member %q#D of "
8008 "non-literal type", decl);
8009 return 1;
8010 }
8011
8012 /* Motion 10 at San Diego: If a static const integral data member is
8013 initialized with an integral constant expression, the initializer
8014 may appear either in the declaration (within the class), or in
8015 the definition, but not both. If it appears in the class, the
8016 member is a member constant. The file-scope definition is always
8017 required. */
8018 if (!ARITHMETIC_TYPE_P (type) && TREE_CODE (type) != ENUMERAL_TYPE)
8019 {
8020 error ("invalid in-class initialization of static data member "
8021 "of non-integral type %qT",
8022 type);
8023 return 1;
8024 }
8025 else if (!CP_TYPE_CONST_P (type))
8026 error ("ISO C++ forbids in-class initialization of non-const "
8027 "static member %qD",
8028 decl);
8029 else if (!INTEGRAL_OR_ENUMERATION_TYPE_P (type))
8030 pedwarn (input_location, OPT_Wpedantic, "ISO C++ forbids initialization of member constant "
8031 "%qD of non-integral type %qT", decl, type);
8032
8033 return 0;
8034 }
8035
8036 /* *expr_p is part of the TYPE_SIZE of a variably-sized array. If any
8037 SAVE_EXPRs in *expr_p wrap expressions with side-effects, break those
8038 expressions out into temporary variables so that walk_tree doesn't
8039 step into them (c++/15764). */
8040
8041 static tree
8042 stabilize_save_expr_r (tree *expr_p, int *walk_subtrees, void *data)
8043 {
8044 struct pointer_set_t *pset = (struct pointer_set_t *)data;
8045 tree expr = *expr_p;
8046 if (TREE_CODE (expr) == SAVE_EXPR)
8047 {
8048 tree op = TREE_OPERAND (expr, 0);
8049 cp_walk_tree (&op, stabilize_save_expr_r, data, pset);
8050 if (TREE_SIDE_EFFECTS (op))
8051 TREE_OPERAND (expr, 0) = get_temp_regvar (TREE_TYPE (op), op);
8052 *walk_subtrees = 0;
8053 }
8054 else if (!EXPR_P (expr) || !TREE_SIDE_EFFECTS (expr))
8055 *walk_subtrees = 0;
8056 return NULL;
8057 }
8058
8059 /* Entry point for the above. */
8060
8061 static void
8062 stabilize_vla_size (tree size)
8063 {
8064 struct pointer_set_t *pset = pointer_set_create ();
8065 /* Break out any function calls into temporary variables. */
8066 cp_walk_tree (&size, stabilize_save_expr_r, pset, pset);
8067 pointer_set_destroy (pset);
8068 }
8069
8070 /* Helper function for compute_array_index_type. Look for SIZEOF_EXPR
8071 not inside of SAVE_EXPR and fold them. */
8072
8073 static tree
8074 fold_sizeof_expr_r (tree *expr_p, int *walk_subtrees, void *data)
8075 {
8076 tree expr = *expr_p;
8077 if (TREE_CODE (expr) == SAVE_EXPR || TYPE_P (expr))
8078 *walk_subtrees = 0;
8079 else if (TREE_CODE (expr) == SIZEOF_EXPR)
8080 {
8081 *(bool *)data = true;
8082 if (SIZEOF_EXPR_TYPE_P (expr))
8083 expr = cxx_sizeof_or_alignof_type (TREE_TYPE (TREE_OPERAND (expr, 0)),
8084 SIZEOF_EXPR, false);
8085 else if (TYPE_P (TREE_OPERAND (expr, 0)))
8086 expr = cxx_sizeof_or_alignof_type (TREE_OPERAND (expr, 0), SIZEOF_EXPR,
8087 false);
8088 else
8089 expr = cxx_sizeof_or_alignof_expr (TREE_OPERAND (expr, 0), SIZEOF_EXPR,
8090 false);
8091 if (expr == error_mark_node)
8092 expr = size_one_node;
8093 *expr_p = expr;
8094 *walk_subtrees = 0;
8095 }
8096 return NULL;
8097 }
8098
8099 /* Given the SIZE (i.e., number of elements) in an array, compute an
8100 appropriate index type for the array. If non-NULL, NAME is the
8101 name of the thing being declared. */
8102
8103 tree
8104 compute_array_index_type (tree name, tree size, tsubst_flags_t complain)
8105 {
8106 tree itype;
8107 tree osize = size;
8108 tree abi_1_itype = NULL_TREE;
8109
8110 if (error_operand_p (size))
8111 return error_mark_node;
8112
8113 if (!type_dependent_expression_p (size))
8114 {
8115 tree type = TREE_TYPE (size);
8116
8117 mark_rvalue_use (size);
8118
8119 if (cxx_dialect < cxx0x && TREE_CODE (size) == NOP_EXPR
8120 && TREE_SIDE_EFFECTS (size))
8121 /* In C++98, we mark a non-constant array bound with a magic
8122 NOP_EXPR with TREE_SIDE_EFFECTS; don't fold in that case. */;
8123 else
8124 {
8125 size = fold_non_dependent_expr_sfinae (size, complain);
8126
8127 if (CLASS_TYPE_P (type)
8128 && CLASSTYPE_LITERAL_P (type))
8129 {
8130 size = build_expr_type_conversion (WANT_INT, size, true);
8131 if (!size)
8132 {
8133 if (!(complain & tf_error))
8134 return error_mark_node;
8135 if (name)
8136 error ("size of array %qD has non-integral type %qT",
8137 name, type);
8138 else
8139 error ("size of array has non-integral type %qT", type);
8140 size = integer_one_node;
8141 }
8142 if (size == error_mark_node)
8143 return error_mark_node;
8144 type = TREE_TYPE (size);
8145 /* We didn't support this case in GCC 3.2, so don't bother
8146 trying to model it now in ABI v1. */
8147 abi_1_itype = error_mark_node;
8148 }
8149
8150 size = maybe_constant_value (size);
8151 if (!TREE_CONSTANT (size))
8152 size = osize;
8153 }
8154
8155 if (error_operand_p (size))
8156 return error_mark_node;
8157
8158 /* The array bound must be an integer type. */
8159 if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type))
8160 {
8161 if (!(complain & tf_error))
8162 return error_mark_node;
8163 if (name)
8164 error ("size of array %qD has non-integral type %qT", name, type);
8165 else
8166 error ("size of array has non-integral type %qT", type);
8167 size = integer_one_node;
8168 type = TREE_TYPE (size);
8169 }
8170 }
8171
8172 /* A type is dependent if it is...an array type constructed from any
8173 dependent type or whose size is specified by a constant expression
8174 that is value-dependent. */
8175 /* We can only call value_dependent_expression_p on integral constant
8176 expressions; treat non-constant expressions as dependent, too. */
8177 if (processing_template_decl
8178 && (type_dependent_expression_p (size)
8179 || !TREE_CONSTANT (size) || value_dependent_expression_p (size)))
8180 {
8181 /* We cannot do any checking for a SIZE that isn't known to be
8182 constant. Just build the index type and mark that it requires
8183 structural equality checks. */
8184 itype = build_index_type (build_min (MINUS_EXPR, sizetype,
8185 size, integer_one_node));
8186 TYPE_DEPENDENT_P (itype) = 1;
8187 TYPE_DEPENDENT_P_VALID (itype) = 1;
8188 SET_TYPE_STRUCTURAL_EQUALITY (itype);
8189 return itype;
8190 }
8191
8192 if (!abi_version_at_least (2) && processing_template_decl
8193 && abi_1_itype == NULL_TREE)
8194 /* For abi-1, we handled all instances in templates the same way,
8195 even when they were non-dependent. This affects the manglings
8196 produced. So, we do the normal checking for non-dependent
8197 sizes, but at the end we'll return the same type that abi-1
8198 would have, but with TYPE_CANONICAL set to the "right"
8199 value that the current ABI would provide. */
8200 abi_1_itype = build_index_type (build_min (MINUS_EXPR, sizetype,
8201 osize, integer_one_node));
8202
8203 /* Normally, the array-bound will be a constant. */
8204 if (TREE_CODE (size) == INTEGER_CST)
8205 {
8206 /* Check to see if the array bound overflowed. Make that an
8207 error, no matter how generous we're being. */
8208 constant_expression_error (size);
8209
8210 /* An array must have a positive number of elements. */
8211 if (INT_CST_LT (size, integer_zero_node))
8212 {
8213 if (!(complain & tf_error))
8214 return error_mark_node;
8215 if (name)
8216 error ("size of array %qD is negative", name);
8217 else
8218 error ("size of array is negative");
8219 size = integer_one_node;
8220 }
8221 /* As an extension we allow zero-sized arrays. */
8222 else if (integer_zerop (size))
8223 {
8224 if (!(complain & tf_error))
8225 /* We must fail if performing argument deduction (as
8226 indicated by the state of complain), so that
8227 another substitution can be found. */
8228 return error_mark_node;
8229 else if (in_system_header)
8230 /* Allow them in system headers because glibc uses them. */;
8231 else if (name)
8232 pedwarn (input_location, OPT_Wpedantic, "ISO C++ forbids zero-size array %qD", name);
8233 else
8234 pedwarn (input_location, OPT_Wpedantic, "ISO C++ forbids zero-size array");
8235 }
8236 }
8237 else if (TREE_CONSTANT (size)
8238 /* We don't allow VLAs at non-function scopes, or during
8239 tentative template substitution. */
8240 || !at_function_scope_p () || !(complain & tf_error))
8241 {
8242 if (!(complain & tf_error))
8243 return error_mark_node;
8244 /* `(int) &fn' is not a valid array bound. */
8245 if (name)
8246 error ("size of array %qD is not an integral constant-expression",
8247 name);
8248 else
8249 error ("size of array is not an integral constant-expression");
8250 size = integer_one_node;
8251 }
8252 else if (pedantic && warn_vla != 0)
8253 {
8254 if (name)
8255 pedwarn (input_location, OPT_Wvla, "ISO C++ forbids variable length array %qD", name);
8256 else
8257 pedwarn (input_location, OPT_Wvla, "ISO C++ forbids variable length array");
8258 }
8259 else if (warn_vla > 0)
8260 {
8261 if (name)
8262 warning (OPT_Wvla,
8263 "variable length array %qD is used", name);
8264 else
8265 warning (OPT_Wvla,
8266 "variable length array is used");
8267 }
8268
8269 if (processing_template_decl && !TREE_CONSTANT (size))
8270 /* A variable sized array. */
8271 itype = build_min (MINUS_EXPR, sizetype, size, integer_one_node);
8272 else
8273 {
8274 HOST_WIDE_INT saved_processing_template_decl;
8275
8276 /* Compute the index of the largest element in the array. It is
8277 one less than the number of elements in the array. We save
8278 and restore PROCESSING_TEMPLATE_DECL so that computations in
8279 cp_build_binary_op will be appropriately folded. */
8280 saved_processing_template_decl = processing_template_decl;
8281 processing_template_decl = 0;
8282 itype = cp_build_binary_op (input_location,
8283 MINUS_EXPR,
8284 cp_convert (ssizetype, size, complain),
8285 cp_convert (ssizetype, integer_one_node,
8286 complain),
8287 complain);
8288 itype = fold (itype);
8289 processing_template_decl = saved_processing_template_decl;
8290
8291 if (!TREE_CONSTANT (itype))
8292 {
8293 /* A variable sized array. */
8294 itype = variable_size (itype);
8295 if (TREE_CODE (itype) != SAVE_EXPR)
8296 {
8297 /* Look for SIZEOF_EXPRs in itype and fold them, otherwise
8298 they might survive till gimplification. */
8299 tree newitype = itype;
8300 bool found = false;
8301 cp_walk_tree_without_duplicates (&newitype,
8302 fold_sizeof_expr_r, &found);
8303 if (found)
8304 itype = variable_size (fold (newitype));
8305 }
8306 }
8307 /* Make sure that there was no overflow when creating to a signed
8308 index type. (For example, on a 32-bit machine, an array with
8309 size 2^32 - 1 is too big.) */
8310 else if (TREE_CODE (itype) == INTEGER_CST
8311 && TREE_OVERFLOW (itype))
8312 {
8313 if (!(complain & tf_error))
8314 return error_mark_node;
8315 error ("overflow in array dimension");
8316 TREE_OVERFLOW (itype) = 0;
8317 }
8318 }
8319
8320 /* Create and return the appropriate index type. */
8321 if (abi_1_itype && abi_1_itype != error_mark_node)
8322 {
8323 tree t = build_index_type (itype);
8324 TYPE_CANONICAL (abi_1_itype) = TYPE_CANONICAL (t);
8325 itype = abi_1_itype;
8326 }
8327 else
8328 itype = build_index_type (itype);
8329
8330 /* If the index type were dependent, we would have returned early, so
8331 remember that it isn't. */
8332 TYPE_DEPENDENT_P (itype) = 0;
8333 TYPE_DEPENDENT_P_VALID (itype) = 1;
8334 return itype;
8335 }
8336
8337 /* Returns the scope (if any) in which the entity declared by
8338 DECLARATOR will be located. If the entity was declared with an
8339 unqualified name, NULL_TREE is returned. */
8340
8341 tree
8342 get_scope_of_declarator (const cp_declarator *declarator)
8343 {
8344 while (declarator && declarator->kind != cdk_id)
8345 declarator = declarator->declarator;
8346
8347 /* If the declarator-id is a SCOPE_REF, the scope in which the
8348 declaration occurs is the first operand. */
8349 if (declarator
8350 && declarator->u.id.qualifying_scope)
8351 return declarator->u.id.qualifying_scope;
8352
8353 /* Otherwise, the declarator is not a qualified name; the entity will
8354 be declared in the current scope. */
8355 return NULL_TREE;
8356 }
8357
8358 /* Returns an ARRAY_TYPE for an array with SIZE elements of the
8359 indicated TYPE. If non-NULL, NAME is the NAME of the declaration
8360 with this type. */
8361
8362 static tree
8363 create_array_type_for_decl (tree name, tree type, tree size)
8364 {
8365 tree itype = NULL_TREE;
8366
8367 /* If things have already gone awry, bail now. */
8368 if (type == error_mark_node || size == error_mark_node)
8369 return error_mark_node;
8370
8371 /* 8.3.4/1: If the type of the identifier of D contains the auto
8372 type-specifier, the program is ill-formed. */
8373 if (pedantic && type_uses_auto (type))
8374 pedwarn (input_location, OPT_Wpedantic,
8375 "declaration of %qD as array of %<auto%>", name);
8376
8377 /* If there are some types which cannot be array elements,
8378 issue an error-message and return. */
8379 switch (TREE_CODE (type))
8380 {
8381 case VOID_TYPE:
8382 if (name)
8383 error ("declaration of %qD as array of void", name);
8384 else
8385 error ("creating array of void");
8386 return error_mark_node;
8387
8388 case FUNCTION_TYPE:
8389 if (name)
8390 error ("declaration of %qD as array of functions", name);
8391 else
8392 error ("creating array of functions");
8393 return error_mark_node;
8394
8395 case REFERENCE_TYPE:
8396 if (name)
8397 error ("declaration of %qD as array of references", name);
8398 else
8399 error ("creating array of references");
8400 return error_mark_node;
8401
8402 case METHOD_TYPE:
8403 if (name)
8404 error ("declaration of %qD as array of function members", name);
8405 else
8406 error ("creating array of function members");
8407 return error_mark_node;
8408
8409 default:
8410 break;
8411 }
8412
8413 /* [dcl.array]
8414
8415 The constant expressions that specify the bounds of the arrays
8416 can be omitted only for the first member of the sequence. */
8417 if (TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type))
8418 {
8419 if (name)
8420 error ("declaration of %qD as multidimensional array must "
8421 "have bounds for all dimensions except the first",
8422 name);
8423 else
8424 error ("multidimensional array must have bounds for all "
8425 "dimensions except the first");
8426
8427 return error_mark_node;
8428 }
8429
8430 /* Figure out the index type for the array. */
8431 if (size)
8432 itype = compute_array_index_type (name, size, tf_warning_or_error);
8433
8434 /* [dcl.array]
8435 T is called the array element type; this type shall not be [...] an
8436 abstract class type. */
8437 abstract_virtuals_error (name, type);
8438
8439 return build_cplus_array_type (type, itype);
8440 }
8441
8442 /* Check that it's OK to declare a function with the indicated TYPE.
8443 SFK indicates the kind of special function (if any) that this
8444 function is. OPTYPE is the type given in a conversion operator
8445 declaration, or the class type for a constructor/destructor.
8446 Returns the actual return type of the function; that
8447 may be different than TYPE if an error occurs, or for certain
8448 special functions. */
8449
8450 static tree
8451 check_special_function_return_type (special_function_kind sfk,
8452 tree type,
8453 tree optype)
8454 {
8455 switch (sfk)
8456 {
8457 case sfk_constructor:
8458 if (type)
8459 error ("return type specification for constructor invalid");
8460
8461 if (targetm.cxx.cdtor_returns_this () && !TYPE_FOR_JAVA (optype))
8462 type = build_pointer_type (optype);
8463 else
8464 type = void_type_node;
8465 break;
8466
8467 case sfk_destructor:
8468 if (type)
8469 error ("return type specification for destructor invalid");
8470 /* We can't use the proper return type here because we run into
8471 problems with ambiguous bases and covariant returns.
8472 Java classes are left unchanged because (void *) isn't a valid
8473 Java type, and we don't want to change the Java ABI. */
8474 if (targetm.cxx.cdtor_returns_this () && !TYPE_FOR_JAVA (optype))
8475 type = build_pointer_type (void_type_node);
8476 else
8477 type = void_type_node;
8478 break;
8479
8480 case sfk_conversion:
8481 if (type)
8482 error ("return type specified for %<operator %T%>", optype);
8483 type = optype;
8484 break;
8485
8486 default:
8487 gcc_unreachable ();
8488 }
8489
8490 return type;
8491 }
8492
8493 /* A variable or data member (whose unqualified name is IDENTIFIER)
8494 has been declared with the indicated TYPE. If the TYPE is not
8495 acceptable, issue an error message and return a type to use for
8496 error-recovery purposes. */
8497
8498 tree
8499 check_var_type (tree identifier, tree type)
8500 {
8501 if (VOID_TYPE_P (type))
8502 {
8503 if (!identifier)
8504 error ("unnamed variable or field declared void");
8505 else if (TREE_CODE (identifier) == IDENTIFIER_NODE)
8506 {
8507 gcc_assert (!IDENTIFIER_OPNAME_P (identifier));
8508 error ("variable or field %qE declared void", identifier);
8509 }
8510 else
8511 error ("variable or field declared void");
8512 type = error_mark_node;
8513 }
8514
8515 return type;
8516 }
8517
8518 /* Functions for adjusting the visibility of a tagged type and its nested
8519 types when it gets a name for linkage purposes from a typedef. */
8520
8521 static void bt_reset_linkage (binding_entry, void *);
8522 static void
8523 reset_type_linkage (tree type)
8524 {
8525 set_linkage_according_to_type (type, TYPE_MAIN_DECL (type));
8526 if (CLASS_TYPE_P (type))
8527 binding_table_foreach (CLASSTYPE_NESTED_UTDS (type), bt_reset_linkage, NULL);
8528 }
8529 static void
8530 bt_reset_linkage (binding_entry b, void */*data*/)
8531 {
8532 reset_type_linkage (b->type);
8533 }
8534
8535 /* Given declspecs and a declarator (abstract or otherwise), determine
8536 the name and type of the object declared and construct a DECL node
8537 for it.
8538
8539 DECLSPECS points to the representation of declaration-specifier
8540 sequence that precedes declarator.
8541
8542 DECL_CONTEXT says which syntactic context this declaration is in:
8543 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
8544 FUNCDEF for a function definition. Like NORMAL but a few different
8545 error messages in each case. Return value may be zero meaning
8546 this definition is too screwy to try to parse.
8547 MEMFUNCDEF for a function definition. Like FUNCDEF but prepares to
8548 handle member functions (which have FIELD context).
8549 Return value may be zero meaning this definition is too screwy to
8550 try to parse.
8551 PARM for a parameter declaration (either within a function prototype
8552 or before a function body). Make a PARM_DECL, or return void_type_node.
8553 TPARM for a template parameter declaration.
8554 CATCHPARM for a parameter declaration before a catch clause.
8555 TYPENAME if for a typename (in a cast or sizeof).
8556 Don't make a DECL node; just return the ..._TYPE node.
8557 FIELD for a struct or union field; make a FIELD_DECL.
8558 BITFIELD for a field with specified width.
8559
8560 INITIALIZED is as for start_decl.
8561
8562 ATTRLIST is a pointer to the list of attributes, which may be NULL
8563 if there are none; *ATTRLIST may be modified if attributes from inside
8564 the declarator should be applied to the declaration.
8565
8566 When this function is called, scoping variables (such as
8567 CURRENT_CLASS_TYPE) should reflect the scope in which the
8568 declaration occurs, not the scope in which the new declaration will
8569 be placed. For example, on:
8570
8571 void S::f() { ... }
8572
8573 when grokdeclarator is called for `S::f', the CURRENT_CLASS_TYPE
8574 should not be `S'.
8575
8576 Returns a DECL (if a declarator is present), a TYPE (if there is no
8577 declarator, in cases like "struct S;"), or the ERROR_MARK_NODE if an
8578 error occurs. */
8579
8580 tree
8581 grokdeclarator (const cp_declarator *declarator,
8582 cp_decl_specifier_seq *declspecs,
8583 enum decl_context decl_context,
8584 int initialized,
8585 tree* attrlist)
8586 {
8587 tree type = NULL_TREE;
8588 int longlong = 0;
8589 int explicit_int128 = 0;
8590 int virtualp, explicitp, friendp, inlinep, staticp;
8591 int explicit_int = 0;
8592 int explicit_char = 0;
8593 int defaulted_int = 0;
8594 tree dependent_name = NULL_TREE;
8595
8596 tree typedef_decl = NULL_TREE;
8597 const char *name = NULL;
8598 tree typedef_type = NULL_TREE;
8599 /* True if this declarator is a function definition. */
8600 bool funcdef_flag = false;
8601 cp_declarator_kind innermost_code = cdk_error;
8602 int bitfield = 0;
8603 #if 0
8604 /* See the code below that used this. */
8605 tree decl_attr = NULL_TREE;
8606 #endif
8607
8608 /* Keep track of what sort of function is being processed
8609 so that we can warn about default return values, or explicit
8610 return values which do not match prescribed defaults. */
8611 special_function_kind sfk = sfk_none;
8612
8613 tree dname = NULL_TREE;
8614 tree ctor_return_type = NULL_TREE;
8615 enum overload_flags flags = NO_SPECIAL;
8616 /* cv-qualifiers that apply to the declarator, for a declaration of
8617 a member function. */
8618 cp_cv_quals memfn_quals = TYPE_UNQUALIFIED;
8619 /* virt-specifiers that apply to the declarator, for a declaration of
8620 a member function. */
8621 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
8622 /* cv-qualifiers that apply to the type specified by the DECLSPECS. */
8623 int type_quals;
8624 tree raises = NULL_TREE;
8625 int template_count = 0;
8626 tree returned_attrs = NULL_TREE;
8627 tree parms = NULL_TREE;
8628 const cp_declarator *id_declarator;
8629 /* The unqualified name of the declarator; either an
8630 IDENTIFIER_NODE, BIT_NOT_EXPR, or TEMPLATE_ID_EXPR. */
8631 tree unqualified_id;
8632 /* The class type, if any, in which this entity is located,
8633 or NULL_TREE if none. Note that this value may be different from
8634 the current class type; for example if an attempt is made to declare
8635 "A::f" inside "B", this value will be "A". */
8636 tree ctype = current_class_type;
8637 /* The NAMESPACE_DECL for the namespace in which this entity is
8638 located. If an unqualified name is used to declare the entity,
8639 this value will be NULL_TREE, even if the entity is located at
8640 namespace scope. */
8641 tree in_namespace = NULL_TREE;
8642 cp_storage_class storage_class;
8643 bool unsigned_p, signed_p, short_p, long_p, thread_p;
8644 bool type_was_error_mark_node = false;
8645 bool parameter_pack_p = declarator? declarator->parameter_pack_p : false;
8646 bool template_type_arg = false;
8647 bool template_parm_flag = false;
8648 bool constexpr_p = decl_spec_seq_has_spec_p (declspecs, ds_constexpr);
8649 const char *errmsg;
8650
8651 signed_p = decl_spec_seq_has_spec_p (declspecs, ds_signed);
8652 unsigned_p = decl_spec_seq_has_spec_p (declspecs, ds_unsigned);
8653 short_p = decl_spec_seq_has_spec_p (declspecs, ds_short);
8654 long_p = decl_spec_seq_has_spec_p (declspecs, ds_long);
8655 longlong = decl_spec_seq_has_spec_p (declspecs, ds_long_long);
8656 explicit_int128 = declspecs->explicit_int128_p;
8657 thread_p = decl_spec_seq_has_spec_p (declspecs, ds_thread);
8658
8659 if (decl_context == FUNCDEF)
8660 funcdef_flag = true, decl_context = NORMAL;
8661 else if (decl_context == MEMFUNCDEF)
8662 funcdef_flag = true, decl_context = FIELD;
8663 else if (decl_context == BITFIELD)
8664 bitfield = 1, decl_context = FIELD;
8665 else if (decl_context == TEMPLATE_TYPE_ARG)
8666 template_type_arg = true, decl_context = TYPENAME;
8667 else if (decl_context == TPARM)
8668 template_parm_flag = true, decl_context = PARM;
8669
8670 if (initialized > 1)
8671 funcdef_flag = true;
8672
8673 /* Look inside a declarator for the name being declared
8674 and get it as a string, for an error message. */
8675 for (id_declarator = declarator;
8676 id_declarator;
8677 id_declarator = id_declarator->declarator)
8678 {
8679 if (id_declarator->kind != cdk_id)
8680 innermost_code = id_declarator->kind;
8681
8682 switch (id_declarator->kind)
8683 {
8684 case cdk_function:
8685 if (id_declarator->declarator
8686 && id_declarator->declarator->kind == cdk_id)
8687 {
8688 sfk = id_declarator->declarator->u.id.sfk;
8689 if (sfk == sfk_destructor)
8690 flags = DTOR_FLAG;
8691 }
8692 break;
8693
8694 case cdk_id:
8695 {
8696 tree qualifying_scope = id_declarator->u.id.qualifying_scope;
8697 tree decl = id_declarator->u.id.unqualified_name;
8698 if (!decl)
8699 break;
8700 if (qualifying_scope)
8701 {
8702 if (at_function_scope_p ())
8703 {
8704 /* [dcl.meaning]
8705
8706 A declarator-id shall not be qualified except
8707 for ...
8708
8709 None of the cases are permitted in block
8710 scope. */
8711 if (qualifying_scope == global_namespace)
8712 error ("invalid use of qualified-name %<::%D%>",
8713 decl);
8714 else if (TYPE_P (qualifying_scope))
8715 error ("invalid use of qualified-name %<%T::%D%>",
8716 qualifying_scope, decl);
8717 else
8718 error ("invalid use of qualified-name %<%D::%D%>",
8719 qualifying_scope, decl);
8720 return error_mark_node;
8721 }
8722 else if (TYPE_P (qualifying_scope))
8723 {
8724 ctype = qualifying_scope;
8725 if (!MAYBE_CLASS_TYPE_P (ctype))
8726 {
8727 error ("%q#T is not a class or a namespace", ctype);
8728 ctype = NULL_TREE;
8729 }
8730 else if (innermost_code != cdk_function
8731 && current_class_type
8732 && !uniquely_derived_from_p (ctype,
8733 current_class_type))
8734 {
8735 error ("type %qT is not derived from type %qT",
8736 ctype, current_class_type);
8737 return error_mark_node;
8738 }
8739 }
8740 else if (TREE_CODE (qualifying_scope) == NAMESPACE_DECL)
8741 in_namespace = qualifying_scope;
8742 }
8743 switch (TREE_CODE (decl))
8744 {
8745 case BIT_NOT_EXPR:
8746 {
8747 tree type;
8748
8749 if (innermost_code != cdk_function)
8750 {
8751 error ("declaration of %qD as non-function", decl);
8752 return error_mark_node;
8753 }
8754 else if (!qualifying_scope
8755 && !(current_class_type && at_class_scope_p ()))
8756 {
8757 error ("declaration of %qD as non-member", decl);
8758 return error_mark_node;
8759 }
8760
8761 type = TREE_OPERAND (decl, 0);
8762 if (TYPE_P (type))
8763 type = constructor_name (type);
8764 name = identifier_to_locale (IDENTIFIER_POINTER (type));
8765 dname = decl;
8766 }
8767 break;
8768
8769 case TEMPLATE_ID_EXPR:
8770 {
8771 tree fns = TREE_OPERAND (decl, 0);
8772
8773 dname = fns;
8774 if (TREE_CODE (dname) != IDENTIFIER_NODE)
8775 {
8776 gcc_assert (is_overloaded_fn (dname));
8777 dname = DECL_NAME (get_first_fn (dname));
8778 }
8779 }
8780 /* Fall through. */
8781
8782 case IDENTIFIER_NODE:
8783 if (TREE_CODE (decl) == IDENTIFIER_NODE)
8784 dname = decl;
8785
8786 if (C_IS_RESERVED_WORD (dname))
8787 {
8788 error ("declarator-id missing; using reserved word %qD",
8789 dname);
8790 name = identifier_to_locale (IDENTIFIER_POINTER (dname));
8791 }
8792 else if (!IDENTIFIER_TYPENAME_P (dname))
8793 name = identifier_to_locale (IDENTIFIER_POINTER (dname));
8794 else
8795 {
8796 gcc_assert (flags == NO_SPECIAL);
8797 flags = TYPENAME_FLAG;
8798 ctor_return_type = TREE_TYPE (dname);
8799 sfk = sfk_conversion;
8800 if (is_typename_at_global_scope (dname))
8801 name = identifier_to_locale (IDENTIFIER_POINTER (dname));
8802 else
8803 name = "<invalid operator>";
8804 }
8805 break;
8806
8807 default:
8808 gcc_unreachable ();
8809 }
8810 break;
8811 }
8812
8813 case cdk_array:
8814 case cdk_pointer:
8815 case cdk_reference:
8816 case cdk_ptrmem:
8817 break;
8818
8819 case cdk_error:
8820 return error_mark_node;
8821
8822 default:
8823 gcc_unreachable ();
8824 }
8825 if (id_declarator->kind == cdk_id)
8826 break;
8827 }
8828
8829 /* [dcl.fct.edf]
8830
8831 The declarator in a function-definition shall have the form
8832 D1 ( parameter-declaration-clause) ... */
8833 if (funcdef_flag && innermost_code != cdk_function)
8834 {
8835 error ("function definition does not declare parameters");
8836 return error_mark_node;
8837 }
8838
8839 if (((dname && IDENTIFIER_OPNAME_P (dname)) || flags == TYPENAME_FLAG)
8840 && innermost_code != cdk_function
8841 && ! (ctype && !declspecs->any_specifiers_p))
8842 {
8843 error ("declaration of %qD as non-function", dname);
8844 return error_mark_node;
8845 }
8846
8847 if (dname
8848 && TREE_CODE (dname) == IDENTIFIER_NODE
8849 && UDLIT_OPER_P (dname)
8850 && innermost_code != cdk_function)
8851 {
8852 error ("declaration of %qD as non-function", dname);
8853 return error_mark_node;
8854 }
8855
8856 if (dname && IDENTIFIER_OPNAME_P (dname))
8857 {
8858 if (decl_spec_seq_has_spec_p (declspecs, ds_typedef))
8859 {
8860 error ("declaration of %qD as %<typedef%>", dname);
8861 return error_mark_node;
8862 }
8863 else if (decl_context == PARM || decl_context == CATCHPARM)
8864 {
8865 error ("declaration of %qD as parameter", dname);
8866 return error_mark_node;
8867 }
8868 }
8869
8870 /* Anything declared one level down from the top level
8871 must be one of the parameters of a function
8872 (because the body is at least two levels down). */
8873
8874 /* This heuristic cannot be applied to C++ nodes! Fixed, however,
8875 by not allowing C++ class definitions to specify their parameters
8876 with xdecls (must be spec.d in the parmlist).
8877
8878 Since we now wait to push a class scope until we are sure that
8879 we are in a legitimate method context, we must set oldcname
8880 explicitly (since current_class_name is not yet alive).
8881
8882 We also want to avoid calling this a PARM if it is in a namespace. */
8883
8884 if (decl_context == NORMAL && !toplevel_bindings_p ())
8885 {
8886 cp_binding_level *b = current_binding_level;
8887 current_binding_level = b->level_chain;
8888 if (current_binding_level != 0 && toplevel_bindings_p ())
8889 decl_context = PARM;
8890 current_binding_level = b;
8891 }
8892
8893 if (name == NULL)
8894 name = decl_context == PARM ? "parameter" : "type name";
8895
8896 if (constexpr_p && decl_spec_seq_has_spec_p (declspecs, ds_typedef))
8897 {
8898 error ("%<constexpr%> cannot appear in a typedef declaration");
8899 return error_mark_node;
8900 }
8901
8902 /* If there were multiple types specified in the decl-specifier-seq,
8903 issue an error message. */
8904 if (declspecs->multiple_types_p)
8905 {
8906 error ("two or more data types in declaration of %qs", name);
8907 return error_mark_node;
8908 }
8909
8910 if (declspecs->conflicting_specifiers_p)
8911 {
8912 error ("conflicting specifiers in declaration of %qs", name);
8913 return error_mark_node;
8914 }
8915
8916 /* Extract the basic type from the decl-specifier-seq. */
8917 type = declspecs->type;
8918 if (type == error_mark_node)
8919 {
8920 type = NULL_TREE;
8921 type_was_error_mark_node = true;
8922 }
8923 /* If the entire declaration is itself tagged as deprecated then
8924 suppress reports of deprecated items. */
8925 if (type && TREE_DEPRECATED (type)
8926 && deprecated_state != DEPRECATED_SUPPRESS)
8927 warn_deprecated_use (type, NULL_TREE);
8928 if (type && TREE_CODE (type) == TYPE_DECL)
8929 {
8930 typedef_decl = type;
8931 type = TREE_TYPE (typedef_decl);
8932 if (TREE_DEPRECATED (type)
8933 && DECL_ARTIFICIAL (typedef_decl)
8934 && deprecated_state != DEPRECATED_SUPPRESS)
8935 warn_deprecated_use (type, NULL_TREE);
8936 }
8937 /* No type at all: default to `int', and set DEFAULTED_INT
8938 because it was not a user-defined typedef. */
8939 if (type == NULL_TREE && (signed_p || unsigned_p || long_p || short_p))
8940 {
8941 /* These imply 'int'. */
8942 type = integer_type_node;
8943 defaulted_int = 1;
8944 }
8945 /* Gather flags. */
8946 explicit_int = declspecs->explicit_int_p;
8947 explicit_char = declspecs->explicit_char_p;
8948
8949 #if 0
8950 /* See the code below that used this. */
8951 if (typedef_decl)
8952 decl_attr = DECL_ATTRIBUTES (typedef_decl);
8953 #endif
8954 typedef_type = type;
8955
8956
8957 if (sfk != sfk_conversion)
8958 ctor_return_type = ctype;
8959
8960 if (sfk != sfk_none)
8961 type = check_special_function_return_type (sfk, type,
8962 ctor_return_type);
8963 else if (type == NULL_TREE)
8964 {
8965 int is_main;
8966
8967 explicit_int = -1;
8968
8969 /* We handle `main' specially here, because 'main () { }' is so
8970 common. With no options, it is allowed. With -Wreturn-type,
8971 it is a warning. It is only an error with -pedantic-errors. */
8972 is_main = (funcdef_flag
8973 && dname && TREE_CODE (dname) == IDENTIFIER_NODE
8974 && MAIN_NAME_P (dname)
8975 && ctype == NULL_TREE
8976 && in_namespace == NULL_TREE
8977 && current_namespace == global_namespace);
8978
8979 if (type_was_error_mark_node)
8980 /* We've already issued an error, don't complain more. */;
8981 else if (in_system_header || flag_ms_extensions)
8982 /* Allow it, sigh. */;
8983 else if (! is_main)
8984 permerror (input_location, "ISO C++ forbids declaration of %qs with no type", name);
8985 else if (pedantic)
8986 pedwarn (input_location, OPT_Wpedantic,
8987 "ISO C++ forbids declaration of %qs with no type", name);
8988 else
8989 warning (OPT_Wreturn_type,
8990 "ISO C++ forbids declaration of %qs with no type", name);
8991
8992 type = integer_type_node;
8993 }
8994
8995 ctype = NULL_TREE;
8996
8997 if (explicit_int128)
8998 {
8999 if (int128_integer_type_node == NULL_TREE)
9000 {
9001 error ("%<__int128%> is not supported by this target");
9002 explicit_int128 = false;
9003 }
9004 else if (pedantic && ! in_system_header)
9005 pedwarn (input_location, OPT_Wpedantic,
9006 "ISO C++ does not support %<__int128%> for %qs", name);
9007 }
9008
9009 /* Now process the modifiers that were specified
9010 and check for invalid combinations. */
9011
9012 /* Long double is a special combination. */
9013 if (long_p && !longlong && TYPE_MAIN_VARIANT (type) == double_type_node)
9014 {
9015 long_p = false;
9016 type = cp_build_qualified_type (long_double_type_node,
9017 cp_type_quals (type));
9018 }
9019
9020 /* Check all other uses of type modifiers. */
9021
9022 if (unsigned_p || signed_p || long_p || short_p)
9023 {
9024 int ok = 0;
9025
9026 if ((signed_p || unsigned_p) && TREE_CODE (type) != INTEGER_TYPE)
9027 error ("%<signed%> or %<unsigned%> invalid for %qs", name);
9028 else if (signed_p && unsigned_p)
9029 error ("%<signed%> and %<unsigned%> specified together for %qs", name);
9030 else if (longlong && TREE_CODE (type) != INTEGER_TYPE)
9031 error ("%<long long%> invalid for %qs", name);
9032 else if (long_p && TREE_CODE (type) == REAL_TYPE)
9033 error ("%<long%> invalid for %qs", name);
9034 else if (short_p && TREE_CODE (type) == REAL_TYPE)
9035 error ("%<short%> invalid for %qs", name);
9036 else if ((long_p || short_p) && TREE_CODE (type) != INTEGER_TYPE)
9037 error ("%<long%> or %<short%> invalid for %qs", name);
9038 else if ((long_p || short_p || explicit_char || explicit_int) && explicit_int128)
9039 error ("%<long%>, %<int%>, %<short%>, or %<char%> invalid for %qs", name);
9040 else if ((long_p || short_p) && explicit_char)
9041 error ("%<long%> or %<short%> specified with char for %qs", name);
9042 else if (long_p && short_p)
9043 error ("%<long%> and %<short%> specified together for %qs", name);
9044 else if (type == char16_type_node || type == char32_type_node)
9045 {
9046 if (signed_p || unsigned_p)
9047 error ("%<signed%> or %<unsigned%> invalid for %qs", name);
9048 else if (short_p || long_p)
9049 error ("%<short%> or %<long%> invalid for %qs", name);
9050 }
9051 else
9052 {
9053 ok = 1;
9054 if (!explicit_int && !defaulted_int && !explicit_char && !explicit_int128 && pedantic)
9055 {
9056 pedwarn (input_location, OPT_Wpedantic,
9057 "long, short, signed or unsigned used invalidly for %qs",
9058 name);
9059 if (flag_pedantic_errors)
9060 ok = 0;
9061 }
9062 }
9063
9064 /* Discard the type modifiers if they are invalid. */
9065 if (! ok)
9066 {
9067 unsigned_p = false;
9068 signed_p = false;
9069 long_p = false;
9070 short_p = false;
9071 longlong = 0;
9072 }
9073 }
9074
9075 /* Decide whether an integer type is signed or not.
9076 Optionally treat bitfields as signed by default. */
9077 if (unsigned_p
9078 /* [class.bit]
9079
9080 It is implementation-defined whether a plain (neither
9081 explicitly signed or unsigned) char, short, int, or long
9082 bit-field is signed or unsigned.
9083
9084 Naturally, we extend this to long long as well. Note that
9085 this does not include wchar_t. */
9086 || (bitfield && !flag_signed_bitfields
9087 && !signed_p
9088 /* A typedef for plain `int' without `signed' can be
9089 controlled just like plain `int', but a typedef for
9090 `signed int' cannot be so controlled. */
9091 && !(typedef_decl
9092 && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl))
9093 && TREE_CODE (type) == INTEGER_TYPE
9094 && !same_type_p (TYPE_MAIN_VARIANT (type), wchar_type_node)))
9095 {
9096 if (explicit_int128)
9097 type = int128_unsigned_type_node;
9098 else if (longlong)
9099 type = long_long_unsigned_type_node;
9100 else if (long_p)
9101 type = long_unsigned_type_node;
9102 else if (short_p)
9103 type = short_unsigned_type_node;
9104 else if (type == char_type_node)
9105 type = unsigned_char_type_node;
9106 else if (typedef_decl)
9107 type = unsigned_type_for (type);
9108 else
9109 type = unsigned_type_node;
9110 }
9111 else if (signed_p && type == char_type_node)
9112 type = signed_char_type_node;
9113 else if (explicit_int128)
9114 type = int128_integer_type_node;
9115 else if (longlong)
9116 type = long_long_integer_type_node;
9117 else if (long_p)
9118 type = long_integer_type_node;
9119 else if (short_p)
9120 type = short_integer_type_node;
9121
9122 if (decl_spec_seq_has_spec_p (declspecs, ds_complex))
9123 {
9124 if (TREE_CODE (type) != INTEGER_TYPE && TREE_CODE (type) != REAL_TYPE)
9125 error ("complex invalid for %qs", name);
9126 /* If we just have "complex", it is equivalent to
9127 "complex double", but if any modifiers at all are specified it is
9128 the complex form of TYPE. E.g, "complex short" is
9129 "complex short int". */
9130 else if (defaulted_int && ! longlong && ! explicit_int128
9131 && ! (long_p || short_p || signed_p || unsigned_p))
9132 type = complex_double_type_node;
9133 else if (type == integer_type_node)
9134 type = complex_integer_type_node;
9135 else if (type == float_type_node)
9136 type = complex_float_type_node;
9137 else if (type == double_type_node)
9138 type = complex_double_type_node;
9139 else if (type == long_double_type_node)
9140 type = complex_long_double_type_node;
9141 else
9142 type = build_complex_type (type);
9143 }
9144
9145 type_quals = TYPE_UNQUALIFIED;
9146 if (decl_spec_seq_has_spec_p (declspecs, ds_const))
9147 type_quals |= TYPE_QUAL_CONST;
9148 if (decl_spec_seq_has_spec_p (declspecs, ds_volatile))
9149 type_quals |= TYPE_QUAL_VOLATILE;
9150 if (decl_spec_seq_has_spec_p (declspecs, ds_restrict))
9151 type_quals |= TYPE_QUAL_RESTRICT;
9152 if (sfk == sfk_conversion && type_quals != TYPE_UNQUALIFIED)
9153 error ("qualifiers are not allowed on declaration of %<operator %T%>",
9154 ctor_return_type);
9155
9156 /* If we're using the injected-class-name to form a compound type or a
9157 declaration, replace it with the underlying class so we don't get
9158 redundant typedefs in the debug output. But if we are returning the
9159 type unchanged, leave it alone so that it's available to
9160 maybe_get_template_decl_from_type_decl. */
9161 if (CLASS_TYPE_P (type)
9162 && DECL_SELF_REFERENCE_P (TYPE_NAME (type))
9163 && type == TREE_TYPE (TYPE_NAME (type))
9164 && (declarator || type_quals))
9165 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
9166
9167 type_quals |= cp_type_quals (type);
9168 type = cp_build_qualified_type_real
9169 (type, type_quals, ((typedef_decl && !DECL_ARTIFICIAL (typedef_decl)
9170 ? tf_ignore_bad_quals : 0) | tf_warning_or_error));
9171 /* We might have ignored or rejected some of the qualifiers. */
9172 type_quals = cp_type_quals (type);
9173
9174 staticp = 0;
9175 inlinep = decl_spec_seq_has_spec_p (declspecs, ds_inline);
9176 virtualp = decl_spec_seq_has_spec_p (declspecs, ds_virtual);
9177 explicitp = decl_spec_seq_has_spec_p (declspecs, ds_explicit);
9178
9179 storage_class = declspecs->storage_class;
9180 if (storage_class == sc_static)
9181 staticp = 1 + (decl_context == FIELD);
9182
9183 if (virtualp && staticp == 2)
9184 {
9185 error ("member %qD cannot be declared both virtual and static", dname);
9186 storage_class = sc_none;
9187 staticp = 0;
9188 }
9189 friendp = decl_spec_seq_has_spec_p (declspecs, ds_friend);
9190
9191 if (dependent_name && !friendp)
9192 {
9193 error ("%<%T::%D%> is not a valid declarator", ctype, dependent_name);
9194 return error_mark_node;
9195 }
9196
9197 /* Issue errors about use of storage classes for parameters. */
9198 if (decl_context == PARM)
9199 {
9200 if (decl_spec_seq_has_spec_p (declspecs, ds_typedef))
9201 {
9202 error ("typedef declaration invalid in parameter declaration");
9203 return error_mark_node;
9204 }
9205 else if (template_parm_flag && storage_class != sc_none)
9206 {
9207 error ("storage class specified for template parameter %qs", name);
9208 return error_mark_node;
9209 }
9210 else if (storage_class == sc_static
9211 || storage_class == sc_extern
9212 || thread_p)
9213 error ("storage class specifiers invalid in parameter declarations");
9214
9215 /* Function parameters cannot be constexpr. If we saw one, moan
9216 and pretend it wasn't there. */
9217 if (constexpr_p)
9218 {
9219 error ("a parameter cannot be declared %<constexpr%>");
9220 constexpr_p = 0;
9221 }
9222 }
9223
9224 /* Give error if `virtual' is used outside of class declaration. */
9225 if (virtualp
9226 && (current_class_name == NULL_TREE || decl_context != FIELD))
9227 {
9228 error ("%<virtual%> outside class declaration");
9229 virtualp = 0;
9230 }
9231
9232 /* Static anonymous unions are dealt with here. */
9233 if (staticp && decl_context == TYPENAME
9234 && declspecs->type
9235 && ANON_AGGR_TYPE_P (declspecs->type))
9236 decl_context = FIELD;
9237
9238 /* Warn about storage classes that are invalid for certain
9239 kinds of declarations (parameters, typenames, etc.). */
9240 if (thread_p
9241 && ((storage_class
9242 && storage_class != sc_extern
9243 && storage_class != sc_static)
9244 || decl_spec_seq_has_spec_p (declspecs, ds_typedef)))
9245 {
9246 error ("multiple storage classes in declaration of %qs", name);
9247 thread_p = false;
9248 }
9249 if (decl_context != NORMAL
9250 && ((storage_class != sc_none
9251 && storage_class != sc_mutable)
9252 || thread_p))
9253 {
9254 if ((decl_context == PARM || decl_context == CATCHPARM)
9255 && (storage_class == sc_register
9256 || storage_class == sc_auto))
9257 ;
9258 else if (decl_spec_seq_has_spec_p (declspecs, ds_typedef))
9259 ;
9260 else if (decl_context == FIELD
9261 /* C++ allows static class elements. */
9262 && storage_class == sc_static)
9263 /* C++ also allows inlines and signed and unsigned elements,
9264 but in those cases we don't come in here. */
9265 ;
9266 else
9267 {
9268 if (decl_context == FIELD)
9269 error ("storage class specified for %qs", name);
9270 else
9271 {
9272 if (decl_context == PARM || decl_context == CATCHPARM)
9273 error ("storage class specified for parameter %qs", name);
9274 else
9275 error ("storage class specified for typename");
9276 }
9277 if (storage_class == sc_register
9278 || storage_class == sc_auto
9279 || storage_class == sc_extern
9280 || thread_p)
9281 storage_class = sc_none;
9282 }
9283 }
9284 else if (storage_class == sc_extern && funcdef_flag
9285 && ! toplevel_bindings_p ())
9286 error ("nested function %qs declared %<extern%>", name);
9287 else if (toplevel_bindings_p ())
9288 {
9289 if (storage_class == sc_auto)
9290 error ("top-level declaration of %qs specifies %<auto%>", name);
9291 }
9292 else if (thread_p
9293 && storage_class != sc_extern
9294 && storage_class != sc_static)
9295 {
9296 if (declspecs->gnu_thread_keyword_p)
9297 pedwarn (input_location, 0, "function-scope %qs implicitly auto and "
9298 "declared %<__thread%>", name);
9299
9300 /* When thread_local is applied to a variable of block scope the
9301 storage-class-specifier static is implied if it does not appear
9302 explicitly. */
9303 storage_class = declspecs->storage_class = sc_static;
9304 staticp = 1;
9305 }
9306
9307 if (storage_class && friendp)
9308 {
9309 error ("storage class specifiers invalid in friend function declarations");
9310 storage_class = sc_none;
9311 staticp = 0;
9312 }
9313
9314 if (!id_declarator)
9315 unqualified_id = NULL_TREE;
9316 else
9317 {
9318 unqualified_id = id_declarator->u.id.unqualified_name;
9319 switch (TREE_CODE (unqualified_id))
9320 {
9321 case BIT_NOT_EXPR:
9322 unqualified_id = TREE_OPERAND (unqualified_id, 0);
9323 if (TYPE_P (unqualified_id))
9324 unqualified_id = constructor_name (unqualified_id);
9325 break;
9326
9327 case IDENTIFIER_NODE:
9328 case TEMPLATE_ID_EXPR:
9329 break;
9330
9331 default:
9332 gcc_unreachable ();
9333 }
9334 }
9335
9336 if (declspecs->std_attributes)
9337 {
9338 /* Apply the c++11 attributes to the type preceding them. */
9339 source_location saved_loc = input_location;
9340 input_location = declspecs->locations[ds_std_attribute];
9341 decl_attributes (&type, declspecs->std_attributes, 0);
9342 input_location = saved_loc;
9343 }
9344
9345 /* Determine the type of the entity declared by recurring on the
9346 declarator. */
9347 for (; declarator; declarator = declarator->declarator)
9348 {
9349 const cp_declarator *inner_declarator;
9350 tree attrs;
9351
9352 if (type == error_mark_node)
9353 return error_mark_node;
9354
9355 attrs = declarator->attributes;
9356 if (attrs)
9357 {
9358 int attr_flags;
9359
9360 attr_flags = 0;
9361 if (declarator == NULL || declarator->kind == cdk_id)
9362 attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
9363 if (declarator->kind == cdk_function)
9364 attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
9365 if (declarator->kind == cdk_array)
9366 attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
9367 returned_attrs = decl_attributes (&type,
9368 chainon (returned_attrs, attrs),
9369 attr_flags);
9370 }
9371
9372 if (declarator->kind == cdk_id)
9373 break;
9374
9375 inner_declarator = declarator->declarator;
9376
9377 switch (declarator->kind)
9378 {
9379 case cdk_array:
9380 type = create_array_type_for_decl (dname, type,
9381 declarator->u.array.bounds);
9382 if (declarator->std_attributes)
9383 /* [dcl.array]/1:
9384
9385 The optional attribute-specifier-seq appertains to the
9386 array. */
9387 returned_attrs = chainon (returned_attrs,
9388 declarator->std_attributes);
9389 break;
9390
9391 case cdk_function:
9392 {
9393 tree arg_types;
9394 int funcdecl_p;
9395
9396 /* Declaring a function type.
9397 Make sure we have a valid type for the function to return. */
9398
9399 if (type_quals != TYPE_UNQUALIFIED)
9400 {
9401 if (SCALAR_TYPE_P (type) || VOID_TYPE_P (type))
9402 warning (OPT_Wignored_qualifiers,
9403 "type qualifiers ignored on function return type");
9404 /* We now know that the TYPE_QUALS don't apply to the
9405 decl, but to its return type. */
9406 type_quals = TYPE_UNQUALIFIED;
9407 }
9408 errmsg = targetm.invalid_return_type (type);
9409 if (errmsg)
9410 {
9411 error (errmsg);
9412 type = integer_type_node;
9413 }
9414
9415 /* Error about some types functions can't return. */
9416
9417 if (TREE_CODE (type) == FUNCTION_TYPE)
9418 {
9419 error ("%qs declared as function returning a function", name);
9420 return error_mark_node;
9421 }
9422 if (TREE_CODE (type) == ARRAY_TYPE)
9423 {
9424 error ("%qs declared as function returning an array", name);
9425 return error_mark_node;
9426 }
9427 /* When decl_context == NORMAL we emit a better error message
9428 later in abstract_virtuals_error. */
9429 if (decl_context == TYPENAME && ABSTRACT_CLASS_TYPE_P (type))
9430 error ("%qs declared as function returning an abstract "
9431 "class type", name);
9432
9433 /* Pick up type qualifiers which should be applied to `this'. */
9434 memfn_quals = declarator->u.function.qualifiers;
9435 /* Pick up virt-specifiers. */
9436 virt_specifiers = declarator->u.function.virt_specifiers;
9437 /* Pick up the exception specifications. */
9438 raises = declarator->u.function.exception_specification;
9439 /* If the exception-specification is ill-formed, let's pretend
9440 there wasn't one. */
9441 if (raises == error_mark_node)
9442 raises = NULL_TREE;
9443
9444 /* Say it's a definition only for the CALL_EXPR
9445 closest to the identifier. */
9446 funcdecl_p = inner_declarator && inner_declarator->kind == cdk_id;
9447
9448 /* Handle a late-specified return type. */
9449 if (funcdecl_p)
9450 {
9451 if (type_uses_auto (type))
9452 {
9453 if (!declarator->u.function.late_return_type)
9454 {
9455 if (current_class_type
9456 && LAMBDA_TYPE_P (current_class_type))
9457 /* OK for C++11 lambdas. */;
9458 else if (cxx_dialect < cxx1y)
9459 pedwarn (input_location, 0, "%qs function uses "
9460 "%<auto%> type specifier without trailing "
9461 "return type", name);
9462 }
9463 else if (!is_auto (type))
9464 {
9465 error ("%qs function with trailing return type has"
9466 " %qT as its type rather than plain %<auto%>",
9467 name, type);
9468 return error_mark_node;
9469 }
9470 }
9471 else if (declarator->u.function.late_return_type)
9472 {
9473 if (cxx_dialect < cxx0x)
9474 /* Not using maybe_warn_cpp0x because this should
9475 always be an error. */
9476 error ("trailing return type only available with "
9477 "-std=c++11 or -std=gnu++11");
9478 else
9479 error ("%qs function with trailing return type not "
9480 "declared with %<auto%> type specifier", name);
9481 return error_mark_node;
9482 }
9483 }
9484 type = splice_late_return_type
9485 (type, declarator->u.function.late_return_type);
9486 if (type == error_mark_node)
9487 return error_mark_node;
9488
9489 if (ctype == NULL_TREE
9490 && decl_context == FIELD
9491 && funcdecl_p
9492 && (friendp == 0 || dname == current_class_name))
9493 ctype = current_class_type;
9494
9495 if (ctype && (sfk == sfk_constructor
9496 || sfk == sfk_destructor))
9497 {
9498 /* We are within a class's scope. If our declarator name
9499 is the same as the class name, and we are defining
9500 a function, then it is a constructor/destructor, and
9501 therefore returns a void type. */
9502
9503 /* ISO C++ 12.4/2. A destructor may not be declared
9504 const or volatile. A destructor may not be
9505 static.
9506
9507 ISO C++ 12.1. A constructor may not be declared
9508 const or volatile. A constructor may not be
9509 virtual. A constructor may not be static. */
9510 if (staticp == 2)
9511 error ((flags == DTOR_FLAG)
9512 ? G_("destructor cannot be static member function")
9513 : G_("constructor cannot be static member function"));
9514 if (memfn_quals)
9515 {
9516 error ((flags == DTOR_FLAG)
9517 ? G_("destructors may not be cv-qualified")
9518 : G_("constructors may not be cv-qualified"));
9519 memfn_quals = TYPE_UNQUALIFIED;
9520 }
9521
9522 if (decl_context == FIELD
9523 && !member_function_or_else (ctype,
9524 current_class_type,
9525 flags))
9526 return error_mark_node;
9527
9528 if (flags != DTOR_FLAG)
9529 {
9530 /* It's a constructor. */
9531 if (explicitp == 1)
9532 explicitp = 2;
9533 if (virtualp)
9534 {
9535 permerror (input_location, "constructors cannot be declared virtual");
9536 virtualp = 0;
9537 }
9538 if (decl_context == FIELD
9539 && sfk != sfk_constructor)
9540 return error_mark_node;
9541 }
9542 if (decl_context == FIELD)
9543 staticp = 0;
9544 }
9545 else if (friendp)
9546 {
9547 if (initialized)
9548 error ("can%'t initialize friend function %qs", name);
9549 if (virtualp)
9550 {
9551 /* Cannot be both friend and virtual. */
9552 error ("virtual functions cannot be friends");
9553 friendp = 0;
9554 }
9555 if (decl_context == NORMAL)
9556 error ("friend declaration not in class definition");
9557 if (current_function_decl && funcdef_flag)
9558 error ("can%'t define friend function %qs in a local "
9559 "class definition",
9560 name);
9561 }
9562 else if (ctype && sfk == sfk_conversion)
9563 {
9564 if (explicitp == 1)
9565 {
9566 maybe_warn_cpp0x (CPP0X_EXPLICIT_CONVERSION);
9567 explicitp = 2;
9568 }
9569 }
9570
9571 arg_types = grokparms (declarator->u.function.parameters,
9572 &parms);
9573
9574 if (inner_declarator
9575 && inner_declarator->kind == cdk_id
9576 && inner_declarator->u.id.sfk == sfk_destructor
9577 && arg_types != void_list_node)
9578 {
9579 error ("destructors may not have parameters");
9580 arg_types = void_list_node;
9581 parms = NULL_TREE;
9582 }
9583
9584 type = build_function_type (type, arg_types);
9585 if (declarator->std_attributes)
9586 /* [dcl.fct]/2:
9587
9588 The optional attribute-specifier-seq appertains to
9589 the function type. */
9590 decl_attributes (&type, declarator->std_attributes,
9591 0);
9592 }
9593 break;
9594
9595 case cdk_pointer:
9596 case cdk_reference:
9597 case cdk_ptrmem:
9598 /* Filter out pointers-to-references and references-to-references.
9599 We can get these if a TYPE_DECL is used. */
9600
9601 if (TREE_CODE (type) == REFERENCE_TYPE)
9602 {
9603 if (declarator->kind != cdk_reference)
9604 {
9605 error ("cannot declare pointer to %q#T", type);
9606 type = TREE_TYPE (type);
9607 }
9608
9609 /* In C++0x, we allow reference to reference declarations
9610 that occur indirectly through typedefs [7.1.3/8 dcl.typedef]
9611 and template type arguments [14.3.1/4 temp.arg.type]. The
9612 check for direct reference to reference declarations, which
9613 are still forbidden, occurs below. Reasoning behind the change
9614 can be found in DR106, DR540, and the rvalue reference
9615 proposals. */
9616 else if (cxx_dialect == cxx98)
9617 {
9618 error ("cannot declare reference to %q#T", type);
9619 type = TREE_TYPE (type);
9620 }
9621 }
9622 else if (VOID_TYPE_P (type))
9623 {
9624 if (declarator->kind == cdk_reference)
9625 error ("cannot declare reference to %q#T", type);
9626 else if (declarator->kind == cdk_ptrmem)
9627 error ("cannot declare pointer to %q#T member", type);
9628 }
9629
9630 /* We now know that the TYPE_QUALS don't apply to the decl,
9631 but to the target of the pointer. */
9632 type_quals = TYPE_UNQUALIFIED;
9633
9634 if (declarator->kind == cdk_ptrmem
9635 && (TREE_CODE (type) == FUNCTION_TYPE
9636 || (memfn_quals && TREE_CODE (type) == METHOD_TYPE)))
9637 {
9638 memfn_quals |= type_memfn_quals (type);
9639 type = build_memfn_type (type,
9640 declarator->u.pointer.class_type,
9641 memfn_quals);
9642 if (type == error_mark_node)
9643 return error_mark_node;
9644 memfn_quals = TYPE_UNQUALIFIED;
9645 }
9646
9647 if (TREE_CODE (type) == FUNCTION_TYPE
9648 && type_memfn_quals (type) != TYPE_UNQUALIFIED)
9649 error (declarator->kind == cdk_reference
9650 ? G_("cannot declare reference to qualified function type %qT")
9651 : G_("cannot declare pointer to qualified function type %qT"),
9652 type);
9653
9654 /* When the pointed-to type involves components of variable size,
9655 care must be taken to ensure that the size evaluation code is
9656 emitted early enough to dominate all the possible later uses
9657 and late enough for the variables on which it depends to have
9658 been assigned.
9659
9660 This is expected to happen automatically when the pointed-to
9661 type has a name/declaration of it's own, but special attention
9662 is required if the type is anonymous.
9663
9664 We handle the NORMAL and FIELD contexts here by inserting a
9665 dummy statement that just evaluates the size at a safe point
9666 and ensures it is not deferred until e.g. within a deeper
9667 conditional context (c++/43555).
9668
9669 We expect nothing to be needed here for PARM or TYPENAME.
9670 Evaluating the size at this point for TYPENAME would
9671 actually be incorrect, as we might be in the middle of an
9672 expression with side effects on the pointed-to type size
9673 "arguments" prior to the pointer declaration point and the
9674 size evaluation could end up prior to the side effects. */
9675
9676 if (!TYPE_NAME (type)
9677 && (decl_context == NORMAL || decl_context == FIELD)
9678 && at_function_scope_p ()
9679 && variably_modified_type_p (type, NULL_TREE))
9680 {
9681 /* First break out any side-effects. */
9682 stabilize_vla_size (TYPE_SIZE (type));
9683 /* And then force evaluation of the SAVE_EXPR. */
9684 finish_expr_stmt (TYPE_SIZE (type));
9685 }
9686
9687 if (declarator->kind == cdk_reference)
9688 {
9689 /* In C++0x, the type we are creating a reference to might be
9690 a typedef which is itself a reference type. In that case,
9691 we follow the reference collapsing rules in
9692 [7.1.3/8 dcl.typedef] to create the final reference type:
9693
9694 "If a typedef TD names a type that is a reference to a type
9695 T, an attempt to create the type 'lvalue reference to cv TD'
9696 creates the type 'lvalue reference to T,' while an attempt
9697 to create the type "rvalue reference to cv TD' creates the
9698 type TD."
9699 */
9700 if (VOID_TYPE_P (type))
9701 /* We already gave an error. */;
9702 else if (TREE_CODE (type) == REFERENCE_TYPE)
9703 {
9704 if (declarator->u.reference.rvalue_ref)
9705 /* Leave type alone. */;
9706 else
9707 type = cp_build_reference_type (TREE_TYPE (type), false);
9708 }
9709 else
9710 type = cp_build_reference_type
9711 (type, declarator->u.reference.rvalue_ref);
9712
9713 /* In C++0x, we need this check for direct reference to
9714 reference declarations, which are forbidden by
9715 [8.3.2/5 dcl.ref]. Reference to reference declarations
9716 are only allowed indirectly through typedefs and template
9717 type arguments. Example:
9718
9719 void foo(int & &); // invalid ref-to-ref decl
9720
9721 typedef int & int_ref;
9722 void foo(int_ref &); // valid ref-to-ref decl
9723 */
9724 if (inner_declarator && inner_declarator->kind == cdk_reference)
9725 error ("cannot declare reference to %q#T, which is not "
9726 "a typedef or a template type argument", type);
9727 }
9728 else if (TREE_CODE (type) == METHOD_TYPE)
9729 type = build_ptrmemfunc_type (build_pointer_type (type));
9730 else if (declarator->kind == cdk_ptrmem)
9731 {
9732 gcc_assert (TREE_CODE (declarator->u.pointer.class_type)
9733 != NAMESPACE_DECL);
9734 if (declarator->u.pointer.class_type == error_mark_node)
9735 /* We will already have complained. */
9736 type = error_mark_node;
9737 else
9738 type = build_ptrmem_type (declarator->u.pointer.class_type,
9739 type);
9740 }
9741 else
9742 type = build_pointer_type (type);
9743
9744 /* Process a list of type modifier keywords (such as
9745 const or volatile) that were given inside the `*' or `&'. */
9746
9747 if (declarator->u.pointer.qualifiers)
9748 {
9749 type
9750 = cp_build_qualified_type (type,
9751 declarator->u.pointer.qualifiers);
9752 type_quals = cp_type_quals (type);
9753 }
9754
9755 /* Apply C++11 attributes to the pointer, and not to the
9756 type pointed to. This is unlike what is done for GNU
9757 attributes above. It is to comply with [dcl.ptr]/1:
9758
9759 [the optional attribute-specifier-seq (7.6.1) appertains
9760 to the pointer and not to the object pointed to]. */
9761 if (declarator->std_attributes)
9762 decl_attributes (&type, declarator->std_attributes,
9763 0);
9764
9765 ctype = NULL_TREE;
9766 break;
9767
9768 case cdk_error:
9769 break;
9770
9771 default:
9772 gcc_unreachable ();
9773 }
9774 }
9775
9776 /* We need to stabilize side-effects in VLA sizes for regular array
9777 declarations too, not just pointers to arrays. */
9778 if (type != error_mark_node && !TYPE_NAME (type)
9779 && (decl_context == NORMAL || decl_context == FIELD)
9780 && at_function_scope_p ()
9781 && variably_modified_type_p (type, NULL_TREE))
9782 stabilize_vla_size (TYPE_SIZE (type));
9783
9784 /* A `constexpr' specifier used in an object declaration declares
9785 the object as `const'. */
9786 if (constexpr_p && innermost_code != cdk_function)
9787 {
9788 if (type_quals & TYPE_QUAL_VOLATILE)
9789 error ("both %<volatile%> and %<constexpr%> cannot be used here");
9790 if (TREE_CODE (type) != REFERENCE_TYPE)
9791 {
9792 type_quals |= TYPE_QUAL_CONST;
9793 type = cp_build_qualified_type (type, type_quals);
9794 }
9795 }
9796
9797 if (unqualified_id && TREE_CODE (unqualified_id) == TEMPLATE_ID_EXPR
9798 && TREE_CODE (type) != FUNCTION_TYPE
9799 && TREE_CODE (type) != METHOD_TYPE)
9800 {
9801 error ("template-id %qD used as a declarator",
9802 unqualified_id);
9803 unqualified_id = dname;
9804 }
9805
9806 /* If TYPE is a FUNCTION_TYPE, but the function name was explicitly
9807 qualified with a class-name, turn it into a METHOD_TYPE, unless
9808 we know that the function is static. We take advantage of this
9809 opportunity to do other processing that pertains to entities
9810 explicitly declared to be class members. Note that if DECLARATOR
9811 is non-NULL, we know it is a cdk_id declarator; otherwise, we
9812 would not have exited the loop above. */
9813 if (declarator
9814 && declarator->u.id.qualifying_scope
9815 && MAYBE_CLASS_TYPE_P (declarator->u.id.qualifying_scope))
9816 {
9817 ctype = declarator->u.id.qualifying_scope;
9818 ctype = TYPE_MAIN_VARIANT (ctype);
9819 template_count = num_template_headers_for_class (ctype);
9820
9821 if (ctype == current_class_type)
9822 {
9823 if (friendp)
9824 {
9825 permerror (input_location, "member functions are implicitly friends of their class");
9826 friendp = 0;
9827 }
9828 else
9829 permerror (declarator->id_loc,
9830 "extra qualification %<%T::%> on member %qs",
9831 ctype, name);
9832 }
9833 else if (/* If the qualifying type is already complete, then we
9834 can skip the following checks. */
9835 !COMPLETE_TYPE_P (ctype)
9836 && (/* If the function is being defined, then
9837 qualifying type must certainly be complete. */
9838 funcdef_flag
9839 /* A friend declaration of "T::f" is OK, even if
9840 "T" is a template parameter. But, if this
9841 function is not a friend, the qualifying type
9842 must be a class. */
9843 || (!friendp && !CLASS_TYPE_P (ctype))
9844 /* For a declaration, the type need not be
9845 complete, if either it is dependent (since there
9846 is no meaningful definition of complete in that
9847 case) or the qualifying class is currently being
9848 defined. */
9849 || !(dependent_type_p (ctype)
9850 || currently_open_class (ctype)))
9851 /* Check that the qualifying type is complete. */
9852 && !complete_type_or_else (ctype, NULL_TREE))
9853 return error_mark_node;
9854 else if (TREE_CODE (type) == FUNCTION_TYPE)
9855 {
9856 if (current_class_type
9857 && (!friendp || funcdef_flag))
9858 {
9859 error (funcdef_flag
9860 ? G_("cannot define member function %<%T::%s%> "
9861 "within %<%T%>")
9862 : G_("cannot declare member function %<%T::%s%> "
9863 "within %<%T%>"),
9864 ctype, name, current_class_type);
9865 return error_mark_node;
9866 }
9867 }
9868 else if (decl_spec_seq_has_spec_p (declspecs, ds_typedef)
9869 && current_class_type)
9870 {
9871 error ("cannot declare member %<%T::%s%> within %qT",
9872 ctype, name, current_class_type);
9873 return error_mark_node;
9874 }
9875 }
9876
9877 if (ctype == NULL_TREE && decl_context == FIELD && friendp == 0)
9878 ctype = current_class_type;
9879
9880 /* Now TYPE has the actual type. */
9881
9882 if (returned_attrs)
9883 {
9884 if (attrlist)
9885 *attrlist = chainon (returned_attrs, *attrlist);
9886 else
9887 attrlist = &returned_attrs;
9888 }
9889
9890 if (declarator
9891 && declarator->kind == cdk_id
9892 && declarator->std_attributes)
9893 /* [dcl.meaning]/1: The optional attribute-specifier-seq following
9894 a declarator-id appertains to the entity that is declared. */
9895 *attrlist = chainon (*attrlist, declarator->std_attributes);
9896
9897 /* Handle parameter packs. */
9898 if (parameter_pack_p)
9899 {
9900 if (decl_context == PARM)
9901 /* Turn the type into a pack expansion.*/
9902 type = make_pack_expansion (type);
9903 else
9904 error ("non-parameter %qs cannot be a parameter pack", name);
9905 }
9906
9907 /* Did array size calculations overflow or does the array cover more
9908 than half of the address-space? */
9909 if (TREE_CODE (type) == ARRAY_TYPE
9910 && COMPLETE_TYPE_P (type)
9911 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST
9912 && ! valid_constant_size_p (TYPE_SIZE_UNIT (type)))
9913 {
9914 error ("size of array %qs is too large", name);
9915 /* If we proceed with the array type as it is, we'll eventually
9916 crash in tree_low_cst(). */
9917 type = error_mark_node;
9918 }
9919
9920 if ((decl_context == FIELD || decl_context == PARM)
9921 && !processing_template_decl
9922 && variably_modified_type_p (type, NULL_TREE))
9923 {
9924 if (decl_context == FIELD)
9925 error ("data member may not have variably modified type %qT", type);
9926 else
9927 error ("parameter may not have variably modified type %qT", type);
9928 type = error_mark_node;
9929 }
9930
9931 if (explicitp == 1 || (explicitp && friendp))
9932 {
9933 /* [dcl.fct.spec] The explicit specifier shall only be used in
9934 declarations of constructors within a class definition. */
9935 error ("only declarations of constructors can be %<explicit%>");
9936 explicitp = 0;
9937 }
9938
9939 if (storage_class == sc_mutable)
9940 {
9941 if (decl_context != FIELD || friendp)
9942 {
9943 error ("non-member %qs cannot be declared %<mutable%>", name);
9944 storage_class = sc_none;
9945 }
9946 else if (decl_context == TYPENAME
9947 || decl_spec_seq_has_spec_p (declspecs, ds_typedef))
9948 {
9949 error ("non-object member %qs cannot be declared %<mutable%>", name);
9950 storage_class = sc_none;
9951 }
9952 else if (TREE_CODE (type) == FUNCTION_TYPE
9953 || TREE_CODE (type) == METHOD_TYPE)
9954 {
9955 error ("function %qs cannot be declared %<mutable%>", name);
9956 storage_class = sc_none;
9957 }
9958 else if (staticp)
9959 {
9960 error ("static %qs cannot be declared %<mutable%>", name);
9961 storage_class = sc_none;
9962 }
9963 else if (type_quals & TYPE_QUAL_CONST)
9964 {
9965 error ("const %qs cannot be declared %<mutable%>", name);
9966 storage_class = sc_none;
9967 }
9968 else if (TREE_CODE (type) == REFERENCE_TYPE)
9969 {
9970 permerror (input_location, "reference %qs cannot be declared "
9971 "%<mutable%>", name);
9972 storage_class = sc_none;
9973 }
9974 }
9975
9976 /* If this is declaring a typedef name, return a TYPE_DECL. */
9977 if (decl_spec_seq_has_spec_p (declspecs, ds_typedef) && decl_context != TYPENAME)
9978 {
9979 tree decl;
9980
9981 /* Note that the grammar rejects storage classes
9982 in typenames, fields or parameters. */
9983 if (current_lang_name == lang_name_java)
9984 TYPE_FOR_JAVA (type) = 1;
9985
9986 /* This declaration:
9987
9988 typedef void f(int) const;
9989
9990 declares a function type which is not a member of any
9991 particular class, but which is cv-qualified; for
9992 example "f S::*" declares a pointer to a const-qualified
9993 member function of S. We record the cv-qualification in the
9994 function type. */
9995 if (memfn_quals && TREE_CODE (type) == FUNCTION_TYPE)
9996 {
9997 type = apply_memfn_quals (type, memfn_quals);
9998
9999 /* We have now dealt with these qualifiers. */
10000 memfn_quals = TYPE_UNQUALIFIED;
10001 }
10002
10003 if (type_uses_auto (type))
10004 {
10005 error ("typedef declared %<auto%>");
10006 type = error_mark_node;
10007 }
10008
10009 if (decl_context == FIELD)
10010 decl = build_lang_decl (TYPE_DECL, unqualified_id, type);
10011 else
10012 decl = build_decl (input_location, TYPE_DECL, unqualified_id, type);
10013 if (id_declarator && declarator->u.id.qualifying_scope) {
10014 error_at (DECL_SOURCE_LOCATION (decl),
10015 "typedef name may not be a nested-name-specifier");
10016 TREE_TYPE (decl) = error_mark_node;
10017 }
10018
10019 if (decl_context != FIELD)
10020 {
10021 if (!current_function_decl)
10022 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
10023 else if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (current_function_decl)
10024 || (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P
10025 (current_function_decl)))
10026 /* The TYPE_DECL is "abstract" because there will be
10027 clones of this constructor/destructor, and there will
10028 be copies of this TYPE_DECL generated in those
10029 clones. */
10030 DECL_ABSTRACT (decl) = 1;
10031 }
10032 else if (current_class_type
10033 && constructor_name_p (unqualified_id, current_class_type))
10034 permerror (input_location, "ISO C++ forbids nested type %qD with same name "
10035 "as enclosing class",
10036 unqualified_id);
10037
10038 /* If the user declares "typedef struct {...} foo" then the
10039 struct will have an anonymous name. Fill that name in now.
10040 Nothing can refer to it, so nothing needs know about the name
10041 change. */
10042 if (type != error_mark_node
10043 && unqualified_id
10044 && TYPE_NAME (type)
10045 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
10046 && TYPE_ANONYMOUS_P (type)
10047 && declspecs->type_definition_p
10048 && cp_type_quals (type) == TYPE_UNQUALIFIED)
10049 {
10050 tree t;
10051
10052 /* Replace the anonymous name with the real name everywhere. */
10053 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
10054 {
10055 if (ANON_AGGRNAME_P (TYPE_IDENTIFIER (t)))
10056 /* We do not rename the debug info representing the
10057 anonymous tagged type because the standard says in
10058 [dcl.typedef] that the naming applies only for
10059 linkage purposes. */
10060 /*debug_hooks->set_name (t, decl);*/
10061 TYPE_NAME (t) = decl;
10062 }
10063
10064 if (TYPE_LANG_SPECIFIC (type))
10065 TYPE_WAS_ANONYMOUS (type) = 1;
10066
10067 /* If this is a typedef within a template class, the nested
10068 type is a (non-primary) template. The name for the
10069 template needs updating as well. */
10070 if (TYPE_LANG_SPECIFIC (type) && CLASSTYPE_TEMPLATE_INFO (type))
10071 DECL_NAME (CLASSTYPE_TI_TEMPLATE (type))
10072 = TYPE_IDENTIFIER (type);
10073
10074 /* Adjust linkage now that we aren't anonymous anymore. */
10075 reset_type_linkage (type);
10076
10077 /* FIXME remangle member functions; member functions of a
10078 type with external linkage have external linkage. */
10079 }
10080
10081 if (signed_p
10082 || (typedef_decl && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl)))
10083 C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
10084
10085 bad_specifiers (decl, BSP_TYPE, virtualp,
10086 memfn_quals != TYPE_UNQUALIFIED,
10087 inlinep, friendp, raises != NULL_TREE);
10088
10089 if (decl_spec_seq_has_spec_p (declspecs, ds_alias))
10090 /* Acknowledge that this was written:
10091 `using analias = atype;'. */
10092 TYPE_DECL_ALIAS_P (decl) = 1;
10093
10094 return decl;
10095 }
10096
10097 /* Detect the case of an array type of unspecified size
10098 which came, as such, direct from a typedef name.
10099 We must copy the type, so that the array's domain can be
10100 individually set by the object's initializer. */
10101
10102 if (type && typedef_type
10103 && TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type)
10104 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (typedef_type))
10105 type = build_cplus_array_type (TREE_TYPE (type), NULL_TREE);
10106
10107 /* Detect where we're using a typedef of function type to declare a
10108 function. PARMS will not be set, so we must create it now. */
10109
10110 if (type == typedef_type && TREE_CODE (type) == FUNCTION_TYPE)
10111 {
10112 tree decls = NULL_TREE;
10113 tree args;
10114
10115 for (args = TYPE_ARG_TYPES (type);
10116 args && args != void_list_node;
10117 args = TREE_CHAIN (args))
10118 {
10119 tree decl = cp_build_parm_decl (NULL_TREE, TREE_VALUE (args));
10120
10121 DECL_CHAIN (decl) = decls;
10122 decls = decl;
10123 }
10124
10125 parms = nreverse (decls);
10126
10127 if (decl_context != TYPENAME)
10128 {
10129 /* A cv-qualifier-seq shall only be part of the function type
10130 for a non-static member function. [8.3.5/4 dcl.fct] */
10131 if (type_memfn_quals (type) != TYPE_UNQUALIFIED
10132 && (current_class_type == NULL_TREE || staticp) )
10133 {
10134 error (staticp
10135 ? G_("qualified function types cannot be used to "
10136 "declare static member functions")
10137 : G_("qualified function types cannot be used to "
10138 "declare free functions"));
10139 type = TYPE_MAIN_VARIANT (type);
10140 }
10141
10142 /* The qualifiers on the function type become the qualifiers on
10143 the non-static member function. */
10144 memfn_quals |= type_memfn_quals (type);
10145 type_quals = TYPE_UNQUALIFIED;
10146 }
10147 }
10148
10149 /* If this is a type name (such as, in a cast or sizeof),
10150 compute the type and return it now. */
10151
10152 if (decl_context == TYPENAME)
10153 {
10154 /* Note that the grammar rejects storage classes
10155 in typenames, fields or parameters. */
10156 if (type_quals != TYPE_UNQUALIFIED)
10157 type_quals = TYPE_UNQUALIFIED;
10158
10159 /* Special case: "friend class foo" looks like a TYPENAME context. */
10160 if (friendp)
10161 {
10162 if (type_quals != TYPE_UNQUALIFIED)
10163 {
10164 error ("type qualifiers specified for friend class declaration");
10165 type_quals = TYPE_UNQUALIFIED;
10166 }
10167 if (inlinep)
10168 {
10169 error ("%<inline%> specified for friend class declaration");
10170 inlinep = 0;
10171 }
10172
10173 if (!current_aggr)
10174 {
10175 /* Don't allow friend declaration without a class-key. */
10176 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
10177 permerror (input_location, "template parameters cannot be friends");
10178 else if (TREE_CODE (type) == TYPENAME_TYPE)
10179 permerror (input_location, "friend declaration requires class-key, "
10180 "i.e. %<friend class %T::%D%>",
10181 TYPE_CONTEXT (type), TYPENAME_TYPE_FULLNAME (type));
10182 else
10183 permerror (input_location, "friend declaration requires class-key, "
10184 "i.e. %<friend %#T%>",
10185 type);
10186 }
10187
10188 /* Only try to do this stuff if we didn't already give up. */
10189 if (type != integer_type_node)
10190 {
10191 /* A friendly class? */
10192 if (current_class_type)
10193 make_friend_class (current_class_type, TYPE_MAIN_VARIANT (type),
10194 /*complain=*/true);
10195 else
10196 error ("trying to make class %qT a friend of global scope",
10197 type);
10198
10199 type = void_type_node;
10200 }
10201 }
10202 else if (memfn_quals)
10203 {
10204 if (ctype == NULL_TREE
10205 && TREE_CODE (type) == METHOD_TYPE)
10206 ctype = TYPE_METHOD_BASETYPE (type);
10207
10208 if (ctype)
10209 type = build_memfn_type (type, ctype, memfn_quals);
10210 /* Core issue #547: need to allow this in template type args. */
10211 else if (template_type_arg && TREE_CODE (type) == FUNCTION_TYPE)
10212 type = apply_memfn_quals (type, memfn_quals);
10213 else
10214 error ("invalid qualifiers on non-member function type");
10215 }
10216
10217 return type;
10218 }
10219 else if (unqualified_id == NULL_TREE && decl_context != PARM
10220 && decl_context != CATCHPARM
10221 && TREE_CODE (type) != UNION_TYPE
10222 && ! bitfield)
10223 {
10224 error ("abstract declarator %qT used as declaration", type);
10225 return error_mark_node;
10226 }
10227
10228 /* Only functions may be declared using an operator-function-id. */
10229 if (unqualified_id
10230 && IDENTIFIER_OPNAME_P (unqualified_id)
10231 && TREE_CODE (type) != FUNCTION_TYPE
10232 && TREE_CODE (type) != METHOD_TYPE)
10233 {
10234 error ("declaration of %qD as non-function", unqualified_id);
10235 return error_mark_node;
10236 }
10237
10238 /* We don't check parameter types here because we can emit a better
10239 error message later. */
10240 if (decl_context != PARM)
10241 {
10242 type = check_var_type (unqualified_id, type);
10243 if (type == error_mark_node)
10244 return error_mark_node;
10245 }
10246
10247 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
10248 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
10249
10250 if (decl_context == PARM || decl_context == CATCHPARM)
10251 {
10252 if (ctype || in_namespace)
10253 error ("cannot use %<::%> in parameter declaration");
10254
10255 if (type_uses_auto (type))
10256 {
10257 error ("parameter declared %<auto%>");
10258 type = error_mark_node;
10259 }
10260
10261 /* A parameter declared as an array of T is really a pointer to T.
10262 One declared as a function is really a pointer to a function.
10263 One declared as a member is really a pointer to member. */
10264
10265 if (TREE_CODE (type) == ARRAY_TYPE)
10266 {
10267 /* Transfer const-ness of array into that of type pointed to. */
10268 type = build_pointer_type (TREE_TYPE (type));
10269 type_quals = TYPE_UNQUALIFIED;
10270 }
10271 else if (TREE_CODE (type) == FUNCTION_TYPE)
10272 type = build_pointer_type (type);
10273 }
10274
10275 if (ctype && TREE_CODE (type) == FUNCTION_TYPE && staticp < 2
10276 && !NEW_DELETE_OPNAME_P (unqualified_id))
10277 {
10278 cp_cv_quals real_quals = memfn_quals;
10279 if (constexpr_p && sfk != sfk_constructor && sfk != sfk_destructor)
10280 real_quals |= TYPE_QUAL_CONST;
10281 type = build_memfn_type (type, ctype, real_quals);
10282 }
10283
10284 {
10285 tree decl;
10286
10287 if (decl_context == PARM)
10288 {
10289 decl = cp_build_parm_decl (unqualified_id, type);
10290
10291 bad_specifiers (decl, BSP_PARM, virtualp,
10292 memfn_quals != TYPE_UNQUALIFIED,
10293 inlinep, friendp, raises != NULL_TREE);
10294 }
10295 else if (decl_context == FIELD)
10296 {
10297 if (!staticp && TREE_CODE (type) != METHOD_TYPE
10298 && type_uses_auto (type))
10299 {
10300 error ("non-static data member declared %<auto%>");
10301 type = error_mark_node;
10302 }
10303
10304 /* The C99 flexible array extension. */
10305 if (!staticp && TREE_CODE (type) == ARRAY_TYPE
10306 && TYPE_DOMAIN (type) == NULL_TREE)
10307 {
10308 tree itype = compute_array_index_type (dname, integer_zero_node,
10309 tf_warning_or_error);
10310 type = build_cplus_array_type (TREE_TYPE (type), itype);
10311 }
10312
10313 if (type == error_mark_node)
10314 {
10315 /* Happens when declaring arrays of sizes which
10316 are error_mark_node, for example. */
10317 decl = NULL_TREE;
10318 }
10319 else if (in_namespace && !friendp)
10320 {
10321 /* Something like struct S { int N::j; }; */
10322 error ("invalid use of %<::%>");
10323 return error_mark_node;
10324 }
10325 else if (TREE_CODE (type) == FUNCTION_TYPE
10326 || TREE_CODE (type) == METHOD_TYPE)
10327 {
10328 int publicp = 0;
10329 tree function_context;
10330
10331 if (friendp == 0)
10332 {
10333 /* This should never happen in pure C++ (the check
10334 could be an assert). It could happen in
10335 Objective-C++ if someone writes invalid code that
10336 uses a function declaration for an instance
10337 variable or property (instance variables and
10338 properties are parsed as FIELD_DECLs, but they are
10339 part of an Objective-C class, not a C++ class).
10340 That code is invalid and is caught by this
10341 check. */
10342 if (!ctype)
10343 {
10344 error ("declaration of function %qD in invalid context",
10345 unqualified_id);
10346 return error_mark_node;
10347 }
10348
10349 /* ``A union may [ ... ] not [ have ] virtual functions.''
10350 ARM 9.5 */
10351 if (virtualp && TREE_CODE (ctype) == UNION_TYPE)
10352 {
10353 error ("function %qD declared virtual inside a union",
10354 unqualified_id);
10355 return error_mark_node;
10356 }
10357
10358 if (NEW_DELETE_OPNAME_P (unqualified_id))
10359 {
10360 if (virtualp)
10361 {
10362 error ("%qD cannot be declared virtual, since it "
10363 "is always static",
10364 unqualified_id);
10365 virtualp = 0;
10366 }
10367 }
10368 }
10369
10370 /* Check that the name used for a destructor makes sense. */
10371 if (sfk == sfk_destructor)
10372 {
10373 tree uqname = id_declarator->u.id.unqualified_name;
10374
10375 if (!ctype)
10376 {
10377 gcc_assert (friendp);
10378 error ("expected qualified name in friend declaration "
10379 "for destructor %qD", uqname);
10380 return error_mark_node;
10381 }
10382
10383 if (!check_dtor_name (ctype, TREE_OPERAND (uqname, 0)))
10384 {
10385 error ("declaration of %qD as member of %qT",
10386 uqname, ctype);
10387 return error_mark_node;
10388 }
10389 if (constexpr_p)
10390 {
10391 error ("a destructor cannot be %<constexpr%>");
10392 return error_mark_node;
10393 }
10394 }
10395 else if (sfk == sfk_constructor && friendp && !ctype)
10396 {
10397 error ("expected qualified name in friend declaration "
10398 "for constructor %qD",
10399 id_declarator->u.id.unqualified_name);
10400 return error_mark_node;
10401 }
10402
10403 /* Tell grokfndecl if it needs to set TREE_PUBLIC on the node. */
10404 function_context = (ctype != NULL_TREE) ?
10405 decl_function_context (TYPE_MAIN_DECL (ctype)) : NULL_TREE;
10406 publicp = (! friendp || ! staticp)
10407 && function_context == NULL_TREE;
10408 decl = grokfndecl (ctype, type,
10409 TREE_CODE (unqualified_id) != TEMPLATE_ID_EXPR
10410 ? unqualified_id : dname,
10411 parms,
10412 unqualified_id,
10413 virtualp, flags, memfn_quals, raises,
10414 friendp ? -1 : 0, friendp, publicp,
10415 inlinep | (2 * constexpr_p),
10416 sfk,
10417 funcdef_flag, template_count, in_namespace,
10418 attrlist, declarator->id_loc);
10419 decl = set_virt_specifiers (decl, virt_specifiers);
10420 if (decl == NULL_TREE)
10421 return error_mark_node;
10422 #if 0
10423 /* This clobbers the attrs stored in `decl' from `attrlist'. */
10424 /* The decl and setting of decl_attr is also turned off. */
10425 decl = build_decl_attribute_variant (decl, decl_attr);
10426 #endif
10427
10428 /* [class.conv.ctor]
10429
10430 A constructor declared without the function-specifier
10431 explicit that can be called with a single parameter
10432 specifies a conversion from the type of its first
10433 parameter to the type of its class. Such a constructor
10434 is called a converting constructor. */
10435 if (explicitp == 2)
10436 DECL_NONCONVERTING_P (decl) = 1;
10437 }
10438 else if (!staticp && !dependent_type_p (type)
10439 && !COMPLETE_TYPE_P (complete_type (type))
10440 && (TREE_CODE (type) != ARRAY_TYPE || initialized == 0))
10441 {
10442 if (unqualified_id)
10443 error ("field %qD has incomplete type", unqualified_id);
10444 else
10445 error ("name %qT has incomplete type", type);
10446
10447 /* If we're instantiating a template, tell them which
10448 instantiation made the field's type be incomplete. */
10449 if (current_class_type
10450 && TYPE_NAME (current_class_type)
10451 && IDENTIFIER_TEMPLATE (current_class_name)
10452 && declspecs->type
10453 && declspecs->type == type)
10454 error (" in instantiation of template %qT",
10455 current_class_type);
10456
10457 return error_mark_node;
10458 }
10459 else
10460 {
10461 if (friendp)
10462 {
10463 error ("%qE is neither function nor member function; "
10464 "cannot be declared friend", unqualified_id);
10465 friendp = 0;
10466 }
10467 decl = NULL_TREE;
10468 }
10469
10470 if (friendp)
10471 {
10472 /* Friends are treated specially. */
10473 if (ctype == current_class_type)
10474 ; /* We already issued a permerror. */
10475 else if (decl && DECL_NAME (decl))
10476 {
10477 if (template_class_depth (current_class_type) == 0)
10478 {
10479 decl = check_explicit_specialization
10480 (unqualified_id, decl, template_count,
10481 2 * funcdef_flag + 4);
10482 if (decl == error_mark_node)
10483 return error_mark_node;
10484 }
10485
10486 decl = do_friend (ctype, unqualified_id, decl,
10487 *attrlist, flags,
10488 funcdef_flag);
10489 return decl;
10490 }
10491 else
10492 return error_mark_node;
10493 }
10494
10495 /* Structure field. It may not be a function, except for C++. */
10496
10497 if (decl == NULL_TREE)
10498 {
10499 if (staticp)
10500 {
10501 /* C++ allows static class members. All other work
10502 for this is done by grokfield. */
10503 decl = build_lang_decl_loc (declarator->id_loc,
10504 VAR_DECL, unqualified_id, type);
10505 set_linkage_for_static_data_member (decl);
10506 /* Even if there is an in-class initialization, DECL
10507 is considered undefined until an out-of-class
10508 definition is provided. */
10509 DECL_EXTERNAL (decl) = 1;
10510
10511 if (thread_p)
10512 {
10513 DECL_TLS_MODEL (decl) = decl_default_tls_model (decl);
10514 if (declspecs->gnu_thread_keyword_p)
10515 DECL_GNU_TLS_P (decl) = true;
10516 }
10517
10518 if (constexpr_p && !initialized)
10519 {
10520 error ("constexpr static data member %qD must have an "
10521 "initializer", decl);
10522 constexpr_p = false;
10523 }
10524 }
10525 else
10526 {
10527 if (constexpr_p)
10528 {
10529 error ("non-static data member %qE declared %<constexpr%>",
10530 unqualified_id);
10531 constexpr_p = false;
10532 }
10533 decl = build_decl (input_location,
10534 FIELD_DECL, unqualified_id, type);
10535 DECL_NONADDRESSABLE_P (decl) = bitfield;
10536 if (bitfield && !unqualified_id)
10537 TREE_NO_WARNING (decl) = 1;
10538
10539 if (storage_class == sc_mutable)
10540 {
10541 DECL_MUTABLE_P (decl) = 1;
10542 storage_class = sc_none;
10543 }
10544
10545 if (initialized)
10546 {
10547 /* An attempt is being made to initialize a non-static
10548 member. This is new in C++11. */
10549 maybe_warn_cpp0x (CPP0X_NSDMI);
10550
10551 /* If this has been parsed with static storage class, but
10552 errors forced staticp to be cleared, ensure NSDMI is
10553 not present. */
10554 if (declspecs->storage_class == sc_static)
10555 DECL_INITIAL (decl) = error_mark_node;
10556 }
10557 }
10558
10559 bad_specifiers (decl, BSP_FIELD, virtualp,
10560 memfn_quals != TYPE_UNQUALIFIED,
10561 inlinep, friendp, raises != NULL_TREE);
10562 }
10563 }
10564 else if (TREE_CODE (type) == FUNCTION_TYPE
10565 || TREE_CODE (type) == METHOD_TYPE)
10566 {
10567 tree original_name;
10568 int publicp = 0;
10569
10570 if (!unqualified_id)
10571 return error_mark_node;
10572
10573 if (TREE_CODE (unqualified_id) == TEMPLATE_ID_EXPR)
10574 original_name = dname;
10575 else
10576 original_name = unqualified_id;
10577
10578 if (storage_class == sc_auto)
10579 error ("storage class %<auto%> invalid for function %qs", name);
10580 else if (storage_class == sc_register)
10581 error ("storage class %<register%> invalid for function %qs", name);
10582 else if (thread_p)
10583 {
10584 if (declspecs->gnu_thread_keyword_p)
10585 error ("storage class %<__thread%> invalid for function %qs",
10586 name);
10587 else
10588 error ("storage class %<thread_local%> invalid for function %qs",
10589 name);
10590 }
10591
10592 if (virt_specifiers)
10593 error ("virt-specifiers in %qs not allowed outside a class definition", name);
10594 /* Function declaration not at top level.
10595 Storage classes other than `extern' are not allowed
10596 and `extern' makes no difference. */
10597 if (! toplevel_bindings_p ()
10598 && (storage_class == sc_static
10599 || decl_spec_seq_has_spec_p (declspecs, ds_inline))
10600 && pedantic)
10601 {
10602 if (storage_class == sc_static)
10603 pedwarn (input_location, OPT_Wpedantic,
10604 "%<static%> specified invalid for function %qs "
10605 "declared out of global scope", name);
10606 else
10607 pedwarn (input_location, OPT_Wpedantic,
10608 "%<inline%> specifier invalid for function %qs "
10609 "declared out of global scope", name);
10610 }
10611
10612 if (ctype == NULL_TREE)
10613 {
10614 if (virtualp)
10615 {
10616 error ("virtual non-class function %qs", name);
10617 virtualp = 0;
10618 }
10619 else if (sfk == sfk_constructor
10620 || sfk == sfk_destructor)
10621 {
10622 error (funcdef_flag
10623 ? G_("%qs defined in a non-class scope")
10624 : G_("%qs declared in a non-class scope"), name);
10625 sfk = sfk_none;
10626 }
10627 }
10628
10629 /* Record presence of `static'. */
10630 publicp = (ctype != NULL_TREE
10631 || storage_class == sc_extern
10632 || storage_class != sc_static);
10633
10634 decl = grokfndecl (ctype, type, original_name, parms, unqualified_id,
10635 virtualp, flags, memfn_quals, raises,
10636 1, friendp,
10637 publicp, inlinep | (2 * constexpr_p), sfk,
10638 funcdef_flag,
10639 template_count, in_namespace, attrlist,
10640 declarator->id_loc);
10641 if (decl == NULL_TREE)
10642 return error_mark_node;
10643
10644 if (staticp == 1)
10645 {
10646 int invalid_static = 0;
10647
10648 /* Don't allow a static member function in a class, and forbid
10649 declaring main to be static. */
10650 if (TREE_CODE (type) == METHOD_TYPE)
10651 {
10652 permerror (input_location, "cannot declare member function %qD to have "
10653 "static linkage", decl);
10654 invalid_static = 1;
10655 }
10656 else if (current_function_decl)
10657 {
10658 /* FIXME need arm citation */
10659 error ("cannot declare static function inside another function");
10660 invalid_static = 1;
10661 }
10662
10663 if (invalid_static)
10664 {
10665 staticp = 0;
10666 storage_class = sc_none;
10667 }
10668 }
10669 }
10670 else
10671 {
10672 /* It's a variable. */
10673
10674 /* An uninitialized decl with `extern' is a reference. */
10675 decl = grokvardecl (type, unqualified_id,
10676 declspecs,
10677 initialized,
10678 (type_quals & TYPE_QUAL_CONST) != 0,
10679 ctype ? ctype : in_namespace);
10680 bad_specifiers (decl, BSP_VAR, virtualp,
10681 memfn_quals != TYPE_UNQUALIFIED,
10682 inlinep, friendp, raises != NULL_TREE);
10683
10684 if (ctype)
10685 {
10686 DECL_CONTEXT (decl) = ctype;
10687 if (staticp == 1)
10688 {
10689 permerror (input_location, "%<static%> may not be used when defining "
10690 "(as opposed to declaring) a static data member");
10691 staticp = 0;
10692 storage_class = sc_none;
10693 }
10694 if (storage_class == sc_register && TREE_STATIC (decl))
10695 {
10696 error ("static member %qD declared %<register%>", decl);
10697 storage_class = sc_none;
10698 }
10699 if (storage_class == sc_extern && pedantic)
10700 {
10701 pedwarn (input_location, OPT_Wpedantic,
10702 "cannot explicitly declare member %q#D to have "
10703 "extern linkage", decl);
10704 storage_class = sc_none;
10705 }
10706 }
10707 else if (constexpr_p && DECL_EXTERNAL (decl))
10708 {
10709 error ("declaration of constexpr variable %qD is not a definition",
10710 decl);
10711 constexpr_p = false;
10712 }
10713 }
10714
10715 if (storage_class == sc_extern && initialized && !funcdef_flag)
10716 {
10717 if (toplevel_bindings_p ())
10718 {
10719 /* It's common practice (and completely valid) to have a const
10720 be initialized and declared extern. */
10721 if (!(type_quals & TYPE_QUAL_CONST))
10722 warning (0, "%qs initialized and declared %<extern%>", name);
10723 }
10724 else
10725 {
10726 error ("%qs has both %<extern%> and initializer", name);
10727 return error_mark_node;
10728 }
10729 }
10730
10731 /* Record `register' declaration for warnings on &
10732 and in case doing stupid register allocation. */
10733
10734 if (storage_class == sc_register)
10735 DECL_REGISTER (decl) = 1;
10736 else if (storage_class == sc_extern)
10737 DECL_THIS_EXTERN (decl) = 1;
10738 else if (storage_class == sc_static)
10739 DECL_THIS_STATIC (decl) = 1;
10740
10741 /* Set constexpr flag on vars (functions got it in grokfndecl). */
10742 if (constexpr_p && TREE_CODE (decl) == VAR_DECL)
10743 DECL_DECLARED_CONSTEXPR_P (decl) = true;
10744
10745 /* Record constancy and volatility on the DECL itself . There's
10746 no need to do this when processing a template; we'll do this
10747 for the instantiated declaration based on the type of DECL. */
10748 if (!processing_template_decl)
10749 cp_apply_type_quals_to_decl (type_quals, decl);
10750
10751 return decl;
10752 }
10753 }
10754 \f
10755 /* Subroutine of start_function. Ensure that each of the parameter
10756 types (as listed in PARMS) is complete, as is required for a
10757 function definition. */
10758
10759 static void
10760 require_complete_types_for_parms (tree parms)
10761 {
10762 for (; parms; parms = DECL_CHAIN (parms))
10763 {
10764 if (dependent_type_p (TREE_TYPE (parms)))
10765 continue;
10766 if (!VOID_TYPE_P (TREE_TYPE (parms))
10767 && complete_type_or_else (TREE_TYPE (parms), parms))
10768 {
10769 relayout_decl (parms);
10770 DECL_ARG_TYPE (parms) = type_passed_as (TREE_TYPE (parms));
10771 }
10772 else
10773 /* grokparms or complete_type_or_else will have already issued
10774 an error. */
10775 TREE_TYPE (parms) = error_mark_node;
10776 }
10777 }
10778
10779 /* Returns nonzero if T is a local variable. */
10780
10781 int
10782 local_variable_p (const_tree t)
10783 {
10784 if ((TREE_CODE (t) == VAR_DECL
10785 /* A VAR_DECL with a context that is a _TYPE is a static data
10786 member. */
10787 && !TYPE_P (CP_DECL_CONTEXT (t))
10788 /* Any other non-local variable must be at namespace scope. */
10789 && !DECL_NAMESPACE_SCOPE_P (t))
10790 || (TREE_CODE (t) == PARM_DECL))
10791 return 1;
10792
10793 return 0;
10794 }
10795
10796 /* Like local_variable_p, but suitable for use as a tree-walking
10797 function. */
10798
10799 static tree
10800 local_variable_p_walkfn (tree *tp, int *walk_subtrees,
10801 void * /*data*/)
10802 {
10803 /* Check DECL_NAME to avoid including temporaries. We don't check
10804 DECL_ARTIFICIAL because we do want to complain about 'this'. */
10805 if (local_variable_p (*tp) && DECL_NAME (*tp))
10806 return *tp;
10807 else if (TYPE_P (*tp))
10808 *walk_subtrees = 0;
10809
10810 return NULL_TREE;
10811 }
10812
10813 /* Check that ARG, which is a default-argument expression for a
10814 parameter DECL, is valid. Returns ARG, or ERROR_MARK_NODE, if
10815 something goes wrong. DECL may also be a _TYPE node, rather than a
10816 DECL, if there is no DECL available. */
10817
10818 tree
10819 check_default_argument (tree decl, tree arg)
10820 {
10821 tree var;
10822 tree decl_type;
10823
10824 if (TREE_CODE (arg) == DEFAULT_ARG)
10825 /* We get a DEFAULT_ARG when looking at an in-class declaration
10826 with a default argument. Ignore the argument for now; we'll
10827 deal with it after the class is complete. */
10828 return arg;
10829
10830 if (TYPE_P (decl))
10831 {
10832 decl_type = decl;
10833 decl = NULL_TREE;
10834 }
10835 else
10836 decl_type = TREE_TYPE (decl);
10837
10838 if (arg == error_mark_node
10839 || decl == error_mark_node
10840 || TREE_TYPE (arg) == error_mark_node
10841 || decl_type == error_mark_node)
10842 /* Something already went wrong. There's no need to check
10843 further. */
10844 return error_mark_node;
10845
10846 /* [dcl.fct.default]
10847
10848 A default argument expression is implicitly converted to the
10849 parameter type. */
10850 ++cp_unevaluated_operand;
10851 perform_implicit_conversion_flags (decl_type, arg, tf_warning_or_error,
10852 LOOKUP_IMPLICIT);
10853 --cp_unevaluated_operand;
10854
10855 if (warn_zero_as_null_pointer_constant
10856 && c_inhibit_evaluation_warnings == 0
10857 && TYPE_PTR_OR_PTRMEM_P (decl_type)
10858 && null_ptr_cst_p (arg)
10859 && !NULLPTR_TYPE_P (TREE_TYPE (arg)))
10860 {
10861 warning (OPT_Wzero_as_null_pointer_constant,
10862 "zero as null pointer constant");
10863 return nullptr_node;
10864 }
10865
10866 /* [dcl.fct.default]
10867
10868 Local variables shall not be used in default argument
10869 expressions.
10870
10871 The keyword `this' shall not be used in a default argument of a
10872 member function. */
10873 var = cp_walk_tree_without_duplicates (&arg, local_variable_p_walkfn, NULL);
10874 if (var)
10875 {
10876 if (DECL_NAME (var) == this_identifier)
10877 permerror (input_location, "default argument %qE uses %qD", arg, var);
10878 else
10879 error ("default argument %qE uses local variable %qD", arg, var);
10880 return error_mark_node;
10881 }
10882
10883 /* All is well. */
10884 return arg;
10885 }
10886
10887 /* Returns a deprecated type used within TYPE, or NULL_TREE if none. */
10888
10889 static tree
10890 type_is_deprecated (tree type)
10891 {
10892 enum tree_code code;
10893 if (TREE_DEPRECATED (type))
10894 return type;
10895 if (TYPE_NAME (type)
10896 && TREE_DEPRECATED (TYPE_NAME (type)))
10897 return type;
10898
10899 /* Do warn about using typedefs to a deprecated class. */
10900 if (TAGGED_TYPE_P (type) && type != TYPE_MAIN_VARIANT (type))
10901 return type_is_deprecated (TYPE_MAIN_VARIANT (type));
10902
10903 code = TREE_CODE (type);
10904
10905 if (code == POINTER_TYPE || code == REFERENCE_TYPE
10906 || code == OFFSET_TYPE || code == FUNCTION_TYPE
10907 || code == METHOD_TYPE || code == ARRAY_TYPE)
10908 return type_is_deprecated (TREE_TYPE (type));
10909
10910 if (TYPE_PTRMEMFUNC_P (type))
10911 return type_is_deprecated
10912 (TREE_TYPE (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (type))));
10913
10914 return NULL_TREE;
10915 }
10916
10917 /* Decode the list of parameter types for a function type.
10918 Given the list of things declared inside the parens,
10919 return a list of types.
10920
10921 If this parameter does not end with an ellipsis, we append
10922 void_list_node.
10923
10924 *PARMS is set to the chain of PARM_DECLs created. */
10925
10926 static tree
10927 grokparms (tree parmlist, tree *parms)
10928 {
10929 tree result = NULL_TREE;
10930 tree decls = NULL_TREE;
10931 tree parm;
10932 int any_error = 0;
10933
10934 for (parm = parmlist; parm != NULL_TREE; parm = TREE_CHAIN (parm))
10935 {
10936 tree type = NULL_TREE;
10937 tree init = TREE_PURPOSE (parm);
10938 tree decl = TREE_VALUE (parm);
10939 const char *errmsg;
10940
10941 if (parm == void_list_node)
10942 break;
10943
10944 if (! decl || TREE_TYPE (decl) == error_mark_node)
10945 continue;
10946
10947 type = TREE_TYPE (decl);
10948 if (VOID_TYPE_P (type))
10949 {
10950 if (same_type_p (type, void_type_node)
10951 && DECL_SELF_REFERENCE_P (type)
10952 && !DECL_NAME (decl) && !result && TREE_CHAIN (parm) == void_list_node)
10953 /* this is a parmlist of `(void)', which is ok. */
10954 break;
10955 cxx_incomplete_type_error (decl, type);
10956 /* It's not a good idea to actually create parameters of
10957 type `void'; other parts of the compiler assume that a
10958 void type terminates the parameter list. */
10959 type = error_mark_node;
10960 TREE_TYPE (decl) = error_mark_node;
10961 }
10962
10963 if (type != error_mark_node
10964 && TYPE_FOR_JAVA (type)
10965 && MAYBE_CLASS_TYPE_P (type))
10966 {
10967 error ("parameter %qD has Java class type", decl);
10968 type = error_mark_node;
10969 TREE_TYPE (decl) = error_mark_node;
10970 init = NULL_TREE;
10971 }
10972
10973 if (type != error_mark_node
10974 && (errmsg = targetm.invalid_parameter_type (type)))
10975 {
10976 error (errmsg);
10977 type = error_mark_node;
10978 TREE_TYPE (decl) = error_mark_node;
10979 }
10980
10981 if (type != error_mark_node)
10982 {
10983 if (deprecated_state != DEPRECATED_SUPPRESS)
10984 {
10985 tree deptype = type_is_deprecated (type);
10986 if (deptype)
10987 warn_deprecated_use (deptype, NULL_TREE);
10988 }
10989
10990 /* Top-level qualifiers on the parameters are
10991 ignored for function types. */
10992 type = cp_build_qualified_type (type, 0);
10993 if (TREE_CODE (type) == METHOD_TYPE)
10994 {
10995 error ("parameter %qD invalidly declared method type", decl);
10996 type = build_pointer_type (type);
10997 TREE_TYPE (decl) = type;
10998 }
10999 else if (abstract_virtuals_error (decl, type))
11000 any_error = 1; /* Seems like a good idea. */
11001 else if (POINTER_TYPE_P (type))
11002 {
11003 /* [dcl.fct]/6, parameter types cannot contain pointers
11004 (references) to arrays of unknown bound. */
11005 tree t = TREE_TYPE (type);
11006 int ptr = TYPE_PTR_P (type);
11007
11008 while (1)
11009 {
11010 if (TYPE_PTR_P (t))
11011 ptr = 1;
11012 else if (TREE_CODE (t) != ARRAY_TYPE)
11013 break;
11014 else if (!TYPE_DOMAIN (t))
11015 break;
11016 t = TREE_TYPE (t);
11017 }
11018 if (TREE_CODE (t) == ARRAY_TYPE)
11019 error (ptr
11020 ? G_("parameter %qD includes pointer to array of "
11021 "unknown bound %qT")
11022 : G_("parameter %qD includes reference to array of "
11023 "unknown bound %qT"),
11024 decl, t);
11025 }
11026
11027 if (any_error)
11028 init = NULL_TREE;
11029 else if (init && !processing_template_decl)
11030 init = check_default_argument (decl, init);
11031 }
11032
11033 DECL_CHAIN (decl) = decls;
11034 decls = decl;
11035 result = tree_cons (init, type, result);
11036 }
11037 decls = nreverse (decls);
11038 result = nreverse (result);
11039 if (parm)
11040 result = chainon (result, void_list_node);
11041 *parms = decls;
11042
11043 return result;
11044 }
11045
11046 \f
11047 /* D is a constructor or overloaded `operator='.
11048
11049 Let T be the class in which D is declared. Then, this function
11050 returns:
11051
11052 -1 if D's is an ill-formed constructor or copy assignment operator
11053 whose first parameter is of type `T'.
11054 0 if D is not a copy constructor or copy assignment
11055 operator.
11056 1 if D is a copy constructor or copy assignment operator whose
11057 first parameter is a reference to non-const qualified T.
11058 2 if D is a copy constructor or copy assignment operator whose
11059 first parameter is a reference to const qualified T.
11060
11061 This function can be used as a predicate. Positive values indicate
11062 a copy constructor and nonzero values indicate a copy assignment
11063 operator. */
11064
11065 int
11066 copy_fn_p (const_tree d)
11067 {
11068 tree args;
11069 tree arg_type;
11070 int result = 1;
11071
11072 gcc_assert (DECL_FUNCTION_MEMBER_P (d));
11073
11074 if (TREE_CODE (d) == TEMPLATE_DECL
11075 || (DECL_TEMPLATE_INFO (d)
11076 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (d))))
11077 /* Instantiations of template member functions are never copy
11078 functions. Note that member functions of templated classes are
11079 represented as template functions internally, and we must
11080 accept those as copy functions. */
11081 return 0;
11082
11083 args = FUNCTION_FIRST_USER_PARMTYPE (d);
11084 if (!args)
11085 return 0;
11086
11087 arg_type = TREE_VALUE (args);
11088 if (arg_type == error_mark_node)
11089 return 0;
11090
11091 if (TYPE_MAIN_VARIANT (arg_type) == DECL_CONTEXT (d))
11092 {
11093 /* Pass by value copy assignment operator. */
11094 result = -1;
11095 }
11096 else if (TREE_CODE (arg_type) == REFERENCE_TYPE
11097 && !TYPE_REF_IS_RVALUE (arg_type)
11098 && TYPE_MAIN_VARIANT (TREE_TYPE (arg_type)) == DECL_CONTEXT (d))
11099 {
11100 if (CP_TYPE_CONST_P (TREE_TYPE (arg_type)))
11101 result = 2;
11102 }
11103 else
11104 return 0;
11105
11106 args = TREE_CHAIN (args);
11107
11108 if (args && args != void_list_node && !TREE_PURPOSE (args))
11109 /* There are more non-optional args. */
11110 return 0;
11111
11112 return result;
11113 }
11114
11115 /* D is a constructor or overloaded `operator='.
11116
11117 Let T be the class in which D is declared. Then, this function
11118 returns true when D is a move constructor or move assignment
11119 operator, false otherwise. */
11120
11121 bool
11122 move_fn_p (const_tree d)
11123 {
11124 gcc_assert (DECL_FUNCTION_MEMBER_P (d));
11125
11126 if (cxx_dialect == cxx98)
11127 /* There are no move constructors if we are in C++98 mode. */
11128 return false;
11129
11130 if (TREE_CODE (d) == TEMPLATE_DECL
11131 || (DECL_TEMPLATE_INFO (d)
11132 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (d))))
11133 /* Instantiations of template member functions are never move
11134 functions. Note that member functions of templated classes are
11135 represented as template functions internally, and we must
11136 accept those as move functions. */
11137 return 0;
11138
11139 return move_signature_fn_p (d);
11140 }
11141
11142 /* D is a constructor or overloaded `operator='.
11143
11144 Then, this function returns true when D has the same signature as a move
11145 constructor or move assignment operator (because either it is such a
11146 ctor/op= or it is a template specialization with the same signature),
11147 false otherwise. */
11148
11149 bool
11150 move_signature_fn_p (const_tree d)
11151 {
11152 tree args;
11153 tree arg_type;
11154 bool result = false;
11155
11156 args = FUNCTION_FIRST_USER_PARMTYPE (d);
11157 if (!args)
11158 return 0;
11159
11160 arg_type = TREE_VALUE (args);
11161 if (arg_type == error_mark_node)
11162 return 0;
11163
11164 if (TREE_CODE (arg_type) == REFERENCE_TYPE
11165 && TYPE_REF_IS_RVALUE (arg_type)
11166 && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (arg_type)),
11167 DECL_CONTEXT (d)))
11168 result = true;
11169
11170 args = TREE_CHAIN (args);
11171
11172 if (args && args != void_list_node && !TREE_PURPOSE (args))
11173 /* There are more non-optional args. */
11174 return false;
11175
11176 return result;
11177 }
11178
11179 /* Remember any special properties of member function DECL. */
11180
11181 void
11182 grok_special_member_properties (tree decl)
11183 {
11184 tree class_type;
11185
11186 if (!DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
11187 return;
11188
11189 class_type = DECL_CONTEXT (decl);
11190 if (DECL_CONSTRUCTOR_P (decl))
11191 {
11192 int ctor = copy_fn_p (decl);
11193
11194 if (!DECL_ARTIFICIAL (decl))
11195 TYPE_HAS_USER_CONSTRUCTOR (class_type) = 1;
11196
11197 if (ctor > 0)
11198 {
11199 /* [class.copy]
11200
11201 A non-template constructor for class X is a copy
11202 constructor if its first parameter is of type X&, const
11203 X&, volatile X& or const volatile X&, and either there
11204 are no other parameters or else all other parameters have
11205 default arguments. */
11206 TYPE_HAS_COPY_CTOR (class_type) = 1;
11207 if (user_provided_p (decl))
11208 TYPE_HAS_COMPLEX_COPY_CTOR (class_type) = 1;
11209 if (ctor > 1)
11210 TYPE_HAS_CONST_COPY_CTOR (class_type) = 1;
11211 }
11212 else if (sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (decl)))
11213 {
11214 TYPE_HAS_DEFAULT_CONSTRUCTOR (class_type) = 1;
11215 if (user_provided_p (decl))
11216 TYPE_HAS_COMPLEX_DFLT (class_type) = 1;
11217 }
11218 else if (move_fn_p (decl) && user_provided_p (decl))
11219 TYPE_HAS_COMPLEX_MOVE_CTOR (class_type) = 1;
11220 else if (is_list_ctor (decl))
11221 TYPE_HAS_LIST_CTOR (class_type) = 1;
11222
11223 if (DECL_DECLARED_CONSTEXPR_P (decl)
11224 && !copy_fn_p (decl) && !move_fn_p (decl))
11225 TYPE_HAS_CONSTEXPR_CTOR (class_type) = 1;
11226 }
11227 else if (DECL_OVERLOADED_OPERATOR_P (decl) == NOP_EXPR)
11228 {
11229 /* [class.copy]
11230
11231 A non-template assignment operator for class X is a copy
11232 assignment operator if its parameter is of type X, X&, const
11233 X&, volatile X& or const volatile X&. */
11234
11235 int assop = copy_fn_p (decl);
11236
11237 if (assop)
11238 {
11239 TYPE_HAS_COPY_ASSIGN (class_type) = 1;
11240 if (user_provided_p (decl))
11241 TYPE_HAS_COMPLEX_COPY_ASSIGN (class_type) = 1;
11242 if (assop != 1)
11243 TYPE_HAS_CONST_COPY_ASSIGN (class_type) = 1;
11244 }
11245 else if (move_fn_p (decl) && user_provided_p (decl))
11246 TYPE_HAS_COMPLEX_MOVE_ASSIGN (class_type) = 1;
11247 }
11248 /* Destructors are handled in check_methods. */
11249 }
11250
11251 /* Check a constructor DECL has the correct form. Complains
11252 if the class has a constructor of the form X(X). */
11253
11254 int
11255 grok_ctor_properties (const_tree ctype, const_tree decl)
11256 {
11257 int ctor_parm = copy_fn_p (decl);
11258
11259 if (ctor_parm < 0)
11260 {
11261 /* [class.copy]
11262
11263 A declaration of a constructor for a class X is ill-formed if
11264 its first parameter is of type (optionally cv-qualified) X
11265 and either there are no other parameters or else all other
11266 parameters have default arguments.
11267
11268 We *don't* complain about member template instantiations that
11269 have this form, though; they can occur as we try to decide
11270 what constructor to use during overload resolution. Since
11271 overload resolution will never prefer such a constructor to
11272 the non-template copy constructor (which is either explicitly
11273 or implicitly defined), there's no need to worry about their
11274 existence. Theoretically, they should never even be
11275 instantiated, but that's hard to forestall. */
11276 error ("invalid constructor; you probably meant %<%T (const %T&)%>",
11277 ctype, ctype);
11278 return 0;
11279 }
11280
11281 return 1;
11282 }
11283
11284 /* An operator with this code is unary, but can also be binary. */
11285
11286 static int
11287 ambi_op_p (enum tree_code code)
11288 {
11289 return (code == INDIRECT_REF
11290 || code == ADDR_EXPR
11291 || code == UNARY_PLUS_EXPR
11292 || code == NEGATE_EXPR
11293 || code == PREINCREMENT_EXPR
11294 || code == PREDECREMENT_EXPR);
11295 }
11296
11297 /* An operator with this name can only be unary. */
11298
11299 static int
11300 unary_op_p (enum tree_code code)
11301 {
11302 return (code == TRUTH_NOT_EXPR
11303 || code == BIT_NOT_EXPR
11304 || code == COMPONENT_REF
11305 || code == TYPE_EXPR);
11306 }
11307
11308 /* DECL is a declaration for an overloaded operator. If COMPLAIN is true,
11309 errors are issued for invalid declarations. */
11310
11311 bool
11312 grok_op_properties (tree decl, bool complain)
11313 {
11314 tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
11315 tree argtype;
11316 int methodp = (TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE);
11317 tree name = DECL_NAME (decl);
11318 enum tree_code operator_code;
11319 int arity;
11320 bool ellipsis_p;
11321 tree class_type;
11322
11323 /* Count the number of arguments and check for ellipsis. */
11324 for (argtype = argtypes, arity = 0;
11325 argtype && argtype != void_list_node;
11326 argtype = TREE_CHAIN (argtype))
11327 ++arity;
11328 ellipsis_p = !argtype;
11329
11330 class_type = DECL_CONTEXT (decl);
11331 if (class_type && !CLASS_TYPE_P (class_type))
11332 class_type = NULL_TREE;
11333
11334 if (DECL_CONV_FN_P (decl))
11335 operator_code = TYPE_EXPR;
11336 else
11337 do
11338 {
11339 #define DEF_OPERATOR(NAME, CODE, MANGLING, ARITY, ASSN_P) \
11340 if (ansi_opname (CODE) == name) \
11341 { \
11342 operator_code = (CODE); \
11343 break; \
11344 } \
11345 else if (ansi_assopname (CODE) == name) \
11346 { \
11347 operator_code = (CODE); \
11348 DECL_ASSIGNMENT_OPERATOR_P (decl) = 1; \
11349 break; \
11350 }
11351
11352 #include "operators.def"
11353 #undef DEF_OPERATOR
11354
11355 gcc_unreachable ();
11356 }
11357 while (0);
11358 gcc_assert (operator_code != MAX_TREE_CODES);
11359 SET_OVERLOADED_OPERATOR_CODE (decl, operator_code);
11360
11361 if (class_type)
11362 switch (operator_code)
11363 {
11364 case NEW_EXPR:
11365 TYPE_HAS_NEW_OPERATOR (class_type) = 1;
11366 break;
11367
11368 case DELETE_EXPR:
11369 TYPE_GETS_DELETE (class_type) |= 1;
11370 break;
11371
11372 case VEC_NEW_EXPR:
11373 TYPE_HAS_ARRAY_NEW_OPERATOR (class_type) = 1;
11374 break;
11375
11376 case VEC_DELETE_EXPR:
11377 TYPE_GETS_DELETE (class_type) |= 2;
11378 break;
11379
11380 default:
11381 break;
11382 }
11383
11384 /* [basic.std.dynamic.allocation]/1:
11385
11386 A program is ill-formed if an allocation function is declared
11387 in a namespace scope other than global scope or declared static
11388 in global scope.
11389
11390 The same also holds true for deallocation functions. */
11391 if (operator_code == NEW_EXPR || operator_code == VEC_NEW_EXPR
11392 || operator_code == DELETE_EXPR || operator_code == VEC_DELETE_EXPR)
11393 {
11394 if (DECL_NAMESPACE_SCOPE_P (decl))
11395 {
11396 if (CP_DECL_CONTEXT (decl) != global_namespace)
11397 {
11398 error ("%qD may not be declared within a namespace", decl);
11399 return false;
11400 }
11401 else if (!TREE_PUBLIC (decl))
11402 {
11403 error ("%qD may not be declared as static", decl);
11404 return false;
11405 }
11406 }
11407 }
11408
11409 if (operator_code == NEW_EXPR || operator_code == VEC_NEW_EXPR)
11410 {
11411 TREE_TYPE (decl) = coerce_new_type (TREE_TYPE (decl));
11412 DECL_IS_OPERATOR_NEW (decl) = 1;
11413 }
11414 else if (operator_code == DELETE_EXPR || operator_code == VEC_DELETE_EXPR)
11415 TREE_TYPE (decl) = coerce_delete_type (TREE_TYPE (decl));
11416 else
11417 {
11418 /* An operator function must either be a non-static member function
11419 or have at least one parameter of a class, a reference to a class,
11420 an enumeration, or a reference to an enumeration. 13.4.0.6 */
11421 if (! methodp || DECL_STATIC_FUNCTION_P (decl))
11422 {
11423 if (operator_code == TYPE_EXPR
11424 || operator_code == CALL_EXPR
11425 || operator_code == COMPONENT_REF
11426 || operator_code == ARRAY_REF
11427 || operator_code == NOP_EXPR)
11428 {
11429 error ("%qD must be a nonstatic member function", decl);
11430 return false;
11431 }
11432 else
11433 {
11434 tree p;
11435
11436 if (DECL_STATIC_FUNCTION_P (decl))
11437 {
11438 error ("%qD must be either a non-static member "
11439 "function or a non-member function", decl);
11440 return false;
11441 }
11442
11443 for (p = argtypes; p && p != void_list_node; p = TREE_CHAIN (p))
11444 {
11445 tree arg = non_reference (TREE_VALUE (p));
11446 if (arg == error_mark_node)
11447 return false;
11448
11449 /* MAYBE_CLASS_TYPE_P, rather than CLASS_TYPE_P, is used
11450 because these checks are performed even on
11451 template functions. */
11452 if (MAYBE_CLASS_TYPE_P (arg)
11453 || TREE_CODE (arg) == ENUMERAL_TYPE)
11454 break;
11455 }
11456
11457 if (!p || p == void_list_node)
11458 {
11459 if (complain)
11460 error ("%qD must have an argument of class or "
11461 "enumerated type", decl);
11462 return false;
11463 }
11464 }
11465 }
11466
11467 /* There are no restrictions on the arguments to an overloaded
11468 "operator ()". */
11469 if (operator_code == CALL_EXPR)
11470 return true;
11471
11472 /* Warn about conversion operators that will never be used. */
11473 if (IDENTIFIER_TYPENAME_P (name)
11474 && ! DECL_TEMPLATE_INFO (decl)
11475 && warn_conversion
11476 /* Warn only declaring the function; there is no need to
11477 warn again about out-of-class definitions. */
11478 && class_type == current_class_type)
11479 {
11480 tree t = TREE_TYPE (name);
11481 int ref = (TREE_CODE (t) == REFERENCE_TYPE);
11482
11483 if (ref)
11484 t = TYPE_MAIN_VARIANT (TREE_TYPE (t));
11485
11486 if (TREE_CODE (t) == VOID_TYPE)
11487 warning (OPT_Wconversion,
11488 ref
11489 ? G_("conversion to a reference to void "
11490 "will never use a type conversion operator")
11491 : G_("conversion to void "
11492 "will never use a type conversion operator"));
11493 else if (class_type)
11494 {
11495 if (t == class_type)
11496 warning (OPT_Wconversion,
11497 ref
11498 ? G_("conversion to a reference to the same type "
11499 "will never use a type conversion operator")
11500 : G_("conversion to the same type "
11501 "will never use a type conversion operator"));
11502 /* Don't force t to be complete here. */
11503 else if (MAYBE_CLASS_TYPE_P (t)
11504 && COMPLETE_TYPE_P (t)
11505 && DERIVED_FROM_P (t, class_type))
11506 warning (OPT_Wconversion,
11507 ref
11508 ? G_("conversion to a reference to a base class "
11509 "will never use a type conversion operator")
11510 : G_("conversion to a base class "
11511 "will never use a type conversion operator"));
11512 }
11513
11514 }
11515
11516 if (operator_code == COND_EXPR)
11517 {
11518 /* 13.4.0.3 */
11519 error ("ISO C++ prohibits overloading operator ?:");
11520 return false;
11521 }
11522 else if (ellipsis_p)
11523 {
11524 error ("%qD must not have variable number of arguments", decl);
11525 return false;
11526 }
11527 else if (ambi_op_p (operator_code))
11528 {
11529 if (arity == 1)
11530 /* We pick the one-argument operator codes by default, so
11531 we don't have to change anything. */
11532 ;
11533 else if (arity == 2)
11534 {
11535 /* If we thought this was a unary operator, we now know
11536 it to be a binary operator. */
11537 switch (operator_code)
11538 {
11539 case INDIRECT_REF:
11540 operator_code = MULT_EXPR;
11541 break;
11542
11543 case ADDR_EXPR:
11544 operator_code = BIT_AND_EXPR;
11545 break;
11546
11547 case UNARY_PLUS_EXPR:
11548 operator_code = PLUS_EXPR;
11549 break;
11550
11551 case NEGATE_EXPR:
11552 operator_code = MINUS_EXPR;
11553 break;
11554
11555 case PREINCREMENT_EXPR:
11556 operator_code = POSTINCREMENT_EXPR;
11557 break;
11558
11559 case PREDECREMENT_EXPR:
11560 operator_code = POSTDECREMENT_EXPR;
11561 break;
11562
11563 default:
11564 gcc_unreachable ();
11565 }
11566
11567 SET_OVERLOADED_OPERATOR_CODE (decl, operator_code);
11568
11569 if ((operator_code == POSTINCREMENT_EXPR
11570 || operator_code == POSTDECREMENT_EXPR)
11571 && ! processing_template_decl
11572 && ! same_type_p (TREE_VALUE (TREE_CHAIN (argtypes)), integer_type_node))
11573 {
11574 if (methodp)
11575 error ("postfix %qD must take %<int%> as its argument",
11576 decl);
11577 else
11578 error ("postfix %qD must take %<int%> as its second "
11579 "argument", decl);
11580 return false;
11581 }
11582 }
11583 else
11584 {
11585 if (methodp)
11586 error ("%qD must take either zero or one argument", decl);
11587 else
11588 error ("%qD must take either one or two arguments", decl);
11589 return false;
11590 }
11591
11592 /* More Effective C++ rule 6. */
11593 if (warn_ecpp
11594 && (operator_code == POSTINCREMENT_EXPR
11595 || operator_code == POSTDECREMENT_EXPR
11596 || operator_code == PREINCREMENT_EXPR
11597 || operator_code == PREDECREMENT_EXPR))
11598 {
11599 tree arg = TREE_VALUE (argtypes);
11600 tree ret = TREE_TYPE (TREE_TYPE (decl));
11601 if (methodp || TREE_CODE (arg) == REFERENCE_TYPE)
11602 arg = TREE_TYPE (arg);
11603 arg = TYPE_MAIN_VARIANT (arg);
11604 if (operator_code == PREINCREMENT_EXPR
11605 || operator_code == PREDECREMENT_EXPR)
11606 {
11607 if (TREE_CODE (ret) != REFERENCE_TYPE
11608 || !same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (ret)),
11609 arg))
11610 warning (OPT_Weffc__, "prefix %qD should return %qT", decl,
11611 build_reference_type (arg));
11612 }
11613 else
11614 {
11615 if (!same_type_p (TYPE_MAIN_VARIANT (ret), arg))
11616 warning (OPT_Weffc__, "postfix %qD should return %qT", decl, arg);
11617 }
11618 }
11619 }
11620 else if (unary_op_p (operator_code))
11621 {
11622 if (arity != 1)
11623 {
11624 if (methodp)
11625 error ("%qD must take %<void%>", decl);
11626 else
11627 error ("%qD must take exactly one argument", decl);
11628 return false;
11629 }
11630 }
11631 else /* if (binary_op_p (operator_code)) */
11632 {
11633 if (arity != 2)
11634 {
11635 if (methodp)
11636 error ("%qD must take exactly one argument", decl);
11637 else
11638 error ("%qD must take exactly two arguments", decl);
11639 return false;
11640 }
11641
11642 /* More Effective C++ rule 7. */
11643 if (warn_ecpp
11644 && (operator_code == TRUTH_ANDIF_EXPR
11645 || operator_code == TRUTH_ORIF_EXPR
11646 || operator_code == COMPOUND_EXPR))
11647 warning (OPT_Weffc__, "user-defined %qD always evaluates both arguments",
11648 decl);
11649 }
11650
11651 /* Effective C++ rule 23. */
11652 if (warn_ecpp
11653 && arity == 2
11654 && !DECL_ASSIGNMENT_OPERATOR_P (decl)
11655 && (operator_code == PLUS_EXPR
11656 || operator_code == MINUS_EXPR
11657 || operator_code == TRUNC_DIV_EXPR
11658 || operator_code == MULT_EXPR
11659 || operator_code == TRUNC_MOD_EXPR)
11660 && TREE_CODE (TREE_TYPE (TREE_TYPE (decl))) == REFERENCE_TYPE)
11661 warning (OPT_Weffc__, "%qD should return by value", decl);
11662
11663 /* [over.oper]/8 */
11664 for (; argtypes && argtypes != void_list_node;
11665 argtypes = TREE_CHAIN (argtypes))
11666 if (TREE_PURPOSE (argtypes))
11667 {
11668 TREE_PURPOSE (argtypes) = NULL_TREE;
11669 if (operator_code == POSTINCREMENT_EXPR
11670 || operator_code == POSTDECREMENT_EXPR)
11671 {
11672 pedwarn (input_location, OPT_Wpedantic, "%qD cannot have default arguments",
11673 decl);
11674 }
11675 else
11676 {
11677 error ("%qD cannot have default arguments", decl);
11678 return false;
11679 }
11680 }
11681 }
11682 return true;
11683 }
11684 \f
11685 /* Return a string giving the keyword associate with CODE. */
11686
11687 static const char *
11688 tag_name (enum tag_types code)
11689 {
11690 switch (code)
11691 {
11692 case record_type:
11693 return "struct";
11694 case class_type:
11695 return "class";
11696 case union_type:
11697 return "union";
11698 case enum_type:
11699 return "enum";
11700 case typename_type:
11701 return "typename";
11702 default:
11703 gcc_unreachable ();
11704 }
11705 }
11706
11707 /* Name lookup in an elaborated-type-specifier (after the keyword
11708 indicated by TAG_CODE) has found the TYPE_DECL DECL. If the
11709 elaborated-type-specifier is invalid, issue a diagnostic and return
11710 error_mark_node; otherwise, return the *_TYPE to which it referred.
11711 If ALLOW_TEMPLATE_P is true, TYPE may be a class template. */
11712
11713 tree
11714 check_elaborated_type_specifier (enum tag_types tag_code,
11715 tree decl,
11716 bool allow_template_p)
11717 {
11718 tree type;
11719
11720 if (decl == error_mark_node)
11721 return error_mark_node;
11722
11723 /* In the case of:
11724
11725 struct S { struct S *p; };
11726
11727 name lookup will find the TYPE_DECL for the implicit "S::S"
11728 typedef. Adjust for that here. */
11729 if (DECL_SELF_REFERENCE_P (decl))
11730 decl = TYPE_NAME (TREE_TYPE (decl));
11731
11732 type = TREE_TYPE (decl);
11733
11734 /* Check TEMPLATE_TYPE_PARM first because DECL_IMPLICIT_TYPEDEF_P
11735 is false for this case as well. */
11736 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
11737 {
11738 error ("using template type parameter %qT after %qs",
11739 type, tag_name (tag_code));
11740 return error_mark_node;
11741 }
11742 /* Accept template template parameters. */
11743 else if (allow_template_p
11744 && (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM
11745 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM))
11746 ;
11747 /* [dcl.type.elab]
11748
11749 If the identifier resolves to a typedef-name or the
11750 simple-template-id resolves to an alias template
11751 specialization, the elaborated-type-specifier is ill-formed.
11752
11753 In other words, the only legitimate declaration to use in the
11754 elaborated type specifier is the implicit typedef created when
11755 the type is declared. */
11756 else if (!DECL_IMPLICIT_TYPEDEF_P (decl)
11757 && !DECL_SELF_REFERENCE_P (decl)
11758 && tag_code != typename_type)
11759 {
11760 if (alias_template_specialization_p (type))
11761 error ("using alias template specialization %qT after %qs",
11762 type, tag_name (tag_code));
11763 else
11764 error ("using typedef-name %qD after %qs", decl, tag_name (tag_code));
11765 inform (DECL_SOURCE_LOCATION (decl),
11766 "%qD has a previous declaration here", decl);
11767 return error_mark_node;
11768 }
11769 else if (TREE_CODE (type) != RECORD_TYPE
11770 && TREE_CODE (type) != UNION_TYPE
11771 && tag_code != enum_type
11772 && tag_code != typename_type)
11773 {
11774 error ("%qT referred to as %qs", type, tag_name (tag_code));
11775 error ("%q+T has a previous declaration here", type);
11776 return error_mark_node;
11777 }
11778 else if (TREE_CODE (type) != ENUMERAL_TYPE
11779 && tag_code == enum_type)
11780 {
11781 error ("%qT referred to as enum", type);
11782 error ("%q+T has a previous declaration here", type);
11783 return error_mark_node;
11784 }
11785 else if (!allow_template_p
11786 && TREE_CODE (type) == RECORD_TYPE
11787 && CLASSTYPE_IS_TEMPLATE (type))
11788 {
11789 /* If a class template appears as elaborated type specifier
11790 without a template header such as:
11791
11792 template <class T> class C {};
11793 void f(class C); // No template header here
11794
11795 then the required template argument is missing. */
11796 error ("template argument required for %<%s %T%>",
11797 tag_name (tag_code),
11798 DECL_NAME (CLASSTYPE_TI_TEMPLATE (type)));
11799 return error_mark_node;
11800 }
11801
11802 return type;
11803 }
11804
11805 /* Lookup NAME in elaborate type specifier in scope according to
11806 SCOPE and issue diagnostics if necessary.
11807 Return *_TYPE node upon success, NULL_TREE when the NAME is not
11808 found, and ERROR_MARK_NODE for type error. */
11809
11810 static tree
11811 lookup_and_check_tag (enum tag_types tag_code, tree name,
11812 tag_scope scope, bool template_header_p)
11813 {
11814 tree t;
11815 tree decl;
11816 if (scope == ts_global)
11817 {
11818 /* First try ordinary name lookup, ignoring hidden class name
11819 injected via friend declaration. */
11820 decl = lookup_name_prefer_type (name, 2);
11821 /* If that fails, the name will be placed in the smallest
11822 non-class, non-function-prototype scope according to 3.3.1/5.
11823 We may already have a hidden name declared as friend in this
11824 scope. So lookup again but not ignoring hidden names.
11825 If we find one, that name will be made visible rather than
11826 creating a new tag. */
11827 if (!decl)
11828 decl = lookup_type_scope (name, ts_within_enclosing_non_class);
11829 }
11830 else
11831 decl = lookup_type_scope (name, scope);
11832
11833 if (decl
11834 && (DECL_CLASS_TEMPLATE_P (decl)
11835 || DECL_TEMPLATE_TEMPLATE_PARM_P (decl)))
11836 decl = DECL_TEMPLATE_RESULT (decl);
11837
11838 if (decl && TREE_CODE (decl) == TYPE_DECL)
11839 {
11840 /* Look for invalid nested type:
11841 class C {
11842 class C {};
11843 }; */
11844 if (scope == ts_current && DECL_SELF_REFERENCE_P (decl))
11845 {
11846 error ("%qD has the same name as the class in which it is "
11847 "declared",
11848 decl);
11849 return error_mark_node;
11850 }
11851
11852 /* Two cases we need to consider when deciding if a class
11853 template is allowed as an elaborated type specifier:
11854 1. It is a self reference to its own class.
11855 2. It comes with a template header.
11856
11857 For example:
11858
11859 template <class T> class C {
11860 class C *c1; // DECL_SELF_REFERENCE_P is true
11861 class D;
11862 };
11863 template <class U> class C; // template_header_p is true
11864 template <class T> class C<T>::D {
11865 class C *c2; // DECL_SELF_REFERENCE_P is true
11866 }; */
11867
11868 t = check_elaborated_type_specifier (tag_code,
11869 decl,
11870 template_header_p
11871 | DECL_SELF_REFERENCE_P (decl));
11872 return t;
11873 }
11874 else if (decl && TREE_CODE (decl) == TREE_LIST)
11875 {
11876 error ("reference to %qD is ambiguous", name);
11877 print_candidates (decl);
11878 return error_mark_node;
11879 }
11880 else
11881 return NULL_TREE;
11882 }
11883
11884 /* Get the struct, enum or union (TAG_CODE says which) with tag NAME.
11885 Define the tag as a forward-reference if it is not defined.
11886
11887 If a declaration is given, process it here, and report an error if
11888 multiple declarations are not identical.
11889
11890 SCOPE is TS_CURRENT when this is also a definition. Only look in
11891 the current frame for the name (since C++ allows new names in any
11892 scope.) It is TS_WITHIN_ENCLOSING_NON_CLASS if this is a friend
11893 declaration. Only look beginning from the current scope outward up
11894 till the nearest non-class scope. Otherwise it is TS_GLOBAL.
11895
11896 TEMPLATE_HEADER_P is true when this declaration is preceded by
11897 a set of template parameters. */
11898
11899 static tree
11900 xref_tag_1 (enum tag_types tag_code, tree name,
11901 tag_scope scope, bool template_header_p)
11902 {
11903 enum tree_code code;
11904 tree t;
11905 tree context = NULL_TREE;
11906
11907 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
11908
11909 switch (tag_code)
11910 {
11911 case record_type:
11912 case class_type:
11913 code = RECORD_TYPE;
11914 break;
11915 case union_type:
11916 code = UNION_TYPE;
11917 break;
11918 case enum_type:
11919 code = ENUMERAL_TYPE;
11920 break;
11921 default:
11922 gcc_unreachable ();
11923 }
11924
11925 /* In case of anonymous name, xref_tag is only called to
11926 make type node and push name. Name lookup is not required. */
11927 if (ANON_AGGRNAME_P (name))
11928 t = NULL_TREE;
11929 else
11930 t = lookup_and_check_tag (tag_code, name,
11931 scope, template_header_p);
11932
11933 if (t == error_mark_node)
11934 return error_mark_node;
11935
11936 if (scope != ts_current && t && current_class_type
11937 && template_class_depth (current_class_type)
11938 && template_header_p)
11939 {
11940 if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM)
11941 return t;
11942
11943 /* Since SCOPE is not TS_CURRENT, we are not looking at a
11944 definition of this tag. Since, in addition, we are currently
11945 processing a (member) template declaration of a template
11946 class, we must be very careful; consider:
11947
11948 template <class X>
11949 struct S1
11950
11951 template <class U>
11952 struct S2
11953 { template <class V>
11954 friend struct S1; };
11955
11956 Here, the S2::S1 declaration should not be confused with the
11957 outer declaration. In particular, the inner version should
11958 have a template parameter of level 2, not level 1. This
11959 would be particularly important if the member declaration
11960 were instead:
11961
11962 template <class V = U> friend struct S1;
11963
11964 say, when we should tsubst into `U' when instantiating
11965 S2. On the other hand, when presented with:
11966
11967 template <class T>
11968 struct S1 {
11969 template <class U>
11970 struct S2 {};
11971 template <class U>
11972 friend struct S2;
11973 };
11974
11975 we must find the inner binding eventually. We
11976 accomplish this by making sure that the new type we
11977 create to represent this declaration has the right
11978 TYPE_CONTEXT. */
11979 context = TYPE_CONTEXT (t);
11980 t = NULL_TREE;
11981 }
11982
11983 if (! t)
11984 {
11985 /* If no such tag is yet defined, create a forward-reference node
11986 and record it as the "definition".
11987 When a real declaration of this type is found,
11988 the forward-reference will be altered into a real type. */
11989 if (code == ENUMERAL_TYPE)
11990 {
11991 error ("use of enum %q#D without previous declaration", name);
11992 return error_mark_node;
11993 }
11994 else
11995 {
11996 t = make_class_type (code);
11997 TYPE_CONTEXT (t) = context;
11998 t = pushtag (name, t, scope);
11999 }
12000 }
12001 else
12002 {
12003 if (template_header_p && MAYBE_CLASS_TYPE_P (t))
12004 {
12005 if (!redeclare_class_template (t, current_template_parms))
12006 return error_mark_node;
12007 }
12008 else if (!processing_template_decl
12009 && CLASS_TYPE_P (t)
12010 && CLASSTYPE_IS_TEMPLATE (t))
12011 {
12012 error ("redeclaration of %qT as a non-template", t);
12013 error ("previous declaration %q+D", t);
12014 return error_mark_node;
12015 }
12016
12017 /* Make injected friend class visible. */
12018 if (scope != ts_within_enclosing_non_class
12019 && hidden_name_p (TYPE_NAME (t)))
12020 {
12021 DECL_ANTICIPATED (TYPE_NAME (t)) = 0;
12022 DECL_FRIEND_P (TYPE_NAME (t)) = 0;
12023
12024 if (TYPE_TEMPLATE_INFO (t))
12025 {
12026 DECL_ANTICIPATED (TYPE_TI_TEMPLATE (t)) = 0;
12027 DECL_FRIEND_P (TYPE_TI_TEMPLATE (t)) = 0;
12028 }
12029 }
12030 }
12031
12032 return t;
12033 }
12034
12035 /* Wrapper for xref_tag_1. */
12036
12037 tree
12038 xref_tag (enum tag_types tag_code, tree name,
12039 tag_scope scope, bool template_header_p)
12040 {
12041 tree ret;
12042 bool subtime;
12043 subtime = timevar_cond_start (TV_NAME_LOOKUP);
12044 ret = xref_tag_1 (tag_code, name, scope, template_header_p);
12045 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
12046 return ret;
12047 }
12048
12049
12050 tree
12051 xref_tag_from_type (tree old, tree id, tag_scope scope)
12052 {
12053 enum tag_types tag_kind;
12054
12055 if (TREE_CODE (old) == RECORD_TYPE)
12056 tag_kind = (CLASSTYPE_DECLARED_CLASS (old) ? class_type : record_type);
12057 else
12058 tag_kind = union_type;
12059
12060 if (id == NULL_TREE)
12061 id = TYPE_IDENTIFIER (old);
12062
12063 return xref_tag (tag_kind, id, scope, false);
12064 }
12065
12066 /* Create the binfo hierarchy for REF with (possibly NULL) base list
12067 BASE_LIST. For each element on BASE_LIST the TREE_PURPOSE is an
12068 access_* node, and the TREE_VALUE is the type of the base-class.
12069 Non-NULL TREE_TYPE indicates virtual inheritance.
12070
12071 Returns true if the binfo hierarchy was successfully created,
12072 false if an error was detected. */
12073
12074 bool
12075 xref_basetypes (tree ref, tree base_list)
12076 {
12077 tree *basep;
12078 tree binfo, base_binfo;
12079 unsigned max_vbases = 0; /* Maximum direct & indirect virtual bases. */
12080 unsigned max_bases = 0; /* Maximum direct bases. */
12081 int i;
12082 tree default_access;
12083 tree igo_prev; /* Track Inheritance Graph Order. */
12084
12085 if (ref == error_mark_node)
12086 return false;
12087
12088 /* The base of a derived class is private by default, all others are
12089 public. */
12090 default_access = (TREE_CODE (ref) == RECORD_TYPE
12091 && CLASSTYPE_DECLARED_CLASS (ref)
12092 ? access_private_node : access_public_node);
12093
12094 /* First, make sure that any templates in base-classes are
12095 instantiated. This ensures that if we call ourselves recursively
12096 we do not get confused about which classes are marked and which
12097 are not. */
12098 basep = &base_list;
12099 while (*basep)
12100 {
12101 tree basetype = TREE_VALUE (*basep);
12102
12103 /* The dependent_type_p call below should really be dependent_scope_p
12104 so that we give a hard error about using an incomplete type as a
12105 base, but we allow it with a pedwarn for backward
12106 compatibility. */
12107 if (processing_template_decl
12108 && CLASS_TYPE_P (basetype) && TYPE_BEING_DEFINED (basetype))
12109 cxx_incomplete_type_diagnostic (NULL_TREE, basetype, DK_PEDWARN);
12110 if (!dependent_type_p (basetype)
12111 && !complete_type_or_else (basetype, NULL))
12112 /* An incomplete type. Remove it from the list. */
12113 *basep = TREE_CHAIN (*basep);
12114 else
12115 {
12116 max_bases++;
12117 if (TREE_TYPE (*basep))
12118 max_vbases++;
12119 if (CLASS_TYPE_P (basetype))
12120 max_vbases += vec_safe_length (CLASSTYPE_VBASECLASSES (basetype));
12121 basep = &TREE_CHAIN (*basep);
12122 }
12123 }
12124
12125 TYPE_MARKED_P (ref) = 1;
12126
12127 /* The binfo slot should be empty, unless this is an (ill-formed)
12128 redefinition. */
12129 if (TYPE_BINFO (ref) && !TYPE_SIZE (ref))
12130 {
12131 error ("redefinition of %q#T", ref);
12132 return false;
12133 }
12134
12135 gcc_assert (TYPE_MAIN_VARIANT (ref) == ref);
12136
12137 binfo = make_tree_binfo (max_bases);
12138
12139 TYPE_BINFO (ref) = binfo;
12140 BINFO_OFFSET (binfo) = size_zero_node;
12141 BINFO_TYPE (binfo) = ref;
12142
12143 /* Apply base-class info set up to the variants of this type. */
12144 fixup_type_variants (ref);
12145
12146 if (max_bases)
12147 {
12148 vec_alloc (BINFO_BASE_ACCESSES (binfo), max_bases);
12149 /* An aggregate cannot have baseclasses. */
12150 CLASSTYPE_NON_AGGREGATE (ref) = 1;
12151
12152 if (TREE_CODE (ref) == UNION_TYPE)
12153 {
12154 error ("derived union %qT invalid", ref);
12155 return false;
12156 }
12157 }
12158
12159 if (max_bases > 1)
12160 {
12161 if (TYPE_FOR_JAVA (ref))
12162 {
12163 error ("Java class %qT cannot have multiple bases", ref);
12164 return false;
12165 }
12166 }
12167
12168 if (max_vbases)
12169 {
12170 vec_alloc (CLASSTYPE_VBASECLASSES (ref), max_vbases);
12171
12172 if (TYPE_FOR_JAVA (ref))
12173 {
12174 error ("Java class %qT cannot have virtual bases", ref);
12175 return false;
12176 }
12177 }
12178
12179 for (igo_prev = binfo; base_list; base_list = TREE_CHAIN (base_list))
12180 {
12181 tree access = TREE_PURPOSE (base_list);
12182 int via_virtual = TREE_TYPE (base_list) != NULL_TREE;
12183 tree basetype = TREE_VALUE (base_list);
12184
12185 if (access == access_default_node)
12186 access = default_access;
12187
12188 if (PACK_EXPANSION_P (basetype))
12189 basetype = PACK_EXPANSION_PATTERN (basetype);
12190 if (TREE_CODE (basetype) == TYPE_DECL)
12191 basetype = TREE_TYPE (basetype);
12192 if (!MAYBE_CLASS_TYPE_P (basetype) || TREE_CODE (basetype) == UNION_TYPE)
12193 {
12194 error ("base type %qT fails to be a struct or class type",
12195 basetype);
12196 return false;
12197 }
12198
12199 if (TYPE_FOR_JAVA (basetype) && (current_lang_depth () == 0))
12200 TYPE_FOR_JAVA (ref) = 1;
12201
12202 base_binfo = NULL_TREE;
12203 if (CLASS_TYPE_P (basetype) && !dependent_scope_p (basetype))
12204 {
12205 base_binfo = TYPE_BINFO (basetype);
12206 /* The original basetype could have been a typedef'd type. */
12207 basetype = BINFO_TYPE (base_binfo);
12208
12209 /* Inherit flags from the base. */
12210 TYPE_HAS_NEW_OPERATOR (ref)
12211 |= TYPE_HAS_NEW_OPERATOR (basetype);
12212 TYPE_HAS_ARRAY_NEW_OPERATOR (ref)
12213 |= TYPE_HAS_ARRAY_NEW_OPERATOR (basetype);
12214 TYPE_GETS_DELETE (ref) |= TYPE_GETS_DELETE (basetype);
12215 TYPE_HAS_CONVERSION (ref) |= TYPE_HAS_CONVERSION (basetype);
12216 CLASSTYPE_DIAMOND_SHAPED_P (ref)
12217 |= CLASSTYPE_DIAMOND_SHAPED_P (basetype);
12218 CLASSTYPE_REPEATED_BASE_P (ref)
12219 |= CLASSTYPE_REPEATED_BASE_P (basetype);
12220 }
12221
12222 /* We must do this test after we've seen through a typedef
12223 type. */
12224 if (TYPE_MARKED_P (basetype))
12225 {
12226 if (basetype == ref)
12227 error ("recursive type %qT undefined", basetype);
12228 else
12229 error ("duplicate base type %qT invalid", basetype);
12230 return false;
12231 }
12232
12233 if (PACK_EXPANSION_P (TREE_VALUE (base_list)))
12234 /* Regenerate the pack expansion for the bases. */
12235 basetype = make_pack_expansion (basetype);
12236
12237 TYPE_MARKED_P (basetype) = 1;
12238
12239 base_binfo = copy_binfo (base_binfo, basetype, ref,
12240 &igo_prev, via_virtual);
12241 if (!BINFO_INHERITANCE_CHAIN (base_binfo))
12242 BINFO_INHERITANCE_CHAIN (base_binfo) = binfo;
12243
12244 BINFO_BASE_APPEND (binfo, base_binfo);
12245 BINFO_BASE_ACCESS_APPEND (binfo, access);
12246 }
12247
12248 if (vec_safe_length (CLASSTYPE_VBASECLASSES (ref)) < max_vbases)
12249 /* If we didn't get max_vbases vbases, we must have shared at
12250 least one of them, and are therefore diamond shaped. */
12251 CLASSTYPE_DIAMOND_SHAPED_P (ref) = 1;
12252
12253 /* Unmark all the types. */
12254 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
12255 TYPE_MARKED_P (BINFO_TYPE (base_binfo)) = 0;
12256 TYPE_MARKED_P (ref) = 0;
12257
12258 /* Now see if we have a repeated base type. */
12259 if (!CLASSTYPE_REPEATED_BASE_P (ref))
12260 {
12261 for (base_binfo = binfo; base_binfo;
12262 base_binfo = TREE_CHAIN (base_binfo))
12263 {
12264 if (TYPE_MARKED_P (BINFO_TYPE (base_binfo)))
12265 {
12266 CLASSTYPE_REPEATED_BASE_P (ref) = 1;
12267 break;
12268 }
12269 TYPE_MARKED_P (BINFO_TYPE (base_binfo)) = 1;
12270 }
12271 for (base_binfo = binfo; base_binfo;
12272 base_binfo = TREE_CHAIN (base_binfo))
12273 if (TYPE_MARKED_P (BINFO_TYPE (base_binfo)))
12274 TYPE_MARKED_P (BINFO_TYPE (base_binfo)) = 0;
12275 else
12276 break;
12277 }
12278
12279 return true;
12280 }
12281
12282 \f
12283 /* Copies the enum-related properties from type SRC to type DST.
12284 Used with the underlying type of an enum and the enum itself. */
12285 static void
12286 copy_type_enum (tree dst, tree src)
12287 {
12288 tree t;
12289 for (t = dst; t; t = TYPE_NEXT_VARIANT (t))
12290 {
12291 TYPE_MIN_VALUE (t) = TYPE_MIN_VALUE (src);
12292 TYPE_MAX_VALUE (t) = TYPE_MAX_VALUE (src);
12293 TYPE_SIZE (t) = TYPE_SIZE (src);
12294 TYPE_SIZE_UNIT (t) = TYPE_SIZE_UNIT (src);
12295 SET_TYPE_MODE (dst, TYPE_MODE (src));
12296 TYPE_PRECISION (t) = TYPE_PRECISION (src);
12297 TYPE_ALIGN (t) = TYPE_ALIGN (src);
12298 TYPE_USER_ALIGN (t) = TYPE_USER_ALIGN (src);
12299 TYPE_UNSIGNED (t) = TYPE_UNSIGNED (src);
12300 }
12301 }
12302
12303 /* Begin compiling the definition of an enumeration type.
12304 NAME is its name,
12305
12306 if ENUMTYPE is not NULL_TREE then the type has alredy been found.
12307
12308 UNDERLYING_TYPE is the type that will be used as the storage for
12309 the enumeration type. This should be NULL_TREE if no storage type
12310 was specified.
12311
12312 SCOPED_ENUM_P is true if this is a scoped enumeration type.
12313
12314 if IS_NEW is not NULL, gets TRUE iff a new type is created.
12315
12316 Returns the type object, as yet incomplete.
12317 Also records info about it so that build_enumerator
12318 may be used to declare the individual values as they are read. */
12319
12320 tree
12321 start_enum (tree name, tree enumtype, tree underlying_type,
12322 bool scoped_enum_p, bool *is_new)
12323 {
12324 tree prevtype = NULL_TREE;
12325 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
12326
12327 if (is_new)
12328 *is_new = false;
12329 /* [C++0x dcl.enum]p5:
12330
12331 If not explicitly specified, the underlying type of a scoped
12332 enumeration type is int. */
12333 if (!underlying_type && scoped_enum_p)
12334 underlying_type = integer_type_node;
12335
12336 if (underlying_type)
12337 underlying_type = cv_unqualified (underlying_type);
12338
12339 /* If this is the real definition for a previous forward reference,
12340 fill in the contents in the same object that used to be the
12341 forward reference. */
12342 if (!enumtype)
12343 enumtype = lookup_and_check_tag (enum_type, name,
12344 /*tag_scope=*/ts_current,
12345 /*template_header_p=*/false);
12346
12347 /* In case of a template_decl, the only check that should be deferred
12348 to instantiation time is the comparison of underlying types. */
12349 if (enumtype && TREE_CODE (enumtype) == ENUMERAL_TYPE)
12350 {
12351 if (scoped_enum_p != SCOPED_ENUM_P (enumtype))
12352 {
12353 error_at (input_location, "scoped/unscoped mismatch "
12354 "in enum %q#T", enumtype);
12355 error_at (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype)),
12356 "previous definition here");
12357 enumtype = error_mark_node;
12358 }
12359 else if (ENUM_FIXED_UNDERLYING_TYPE_P (enumtype) != !! underlying_type)
12360 {
12361 error_at (input_location, "underlying type mismatch "
12362 "in enum %q#T", enumtype);
12363 error_at (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype)),
12364 "previous definition here");
12365 enumtype = error_mark_node;
12366 }
12367 else if (underlying_type && ENUM_UNDERLYING_TYPE (enumtype)
12368 && !dependent_type_p (underlying_type)
12369 && !dependent_type_p (ENUM_UNDERLYING_TYPE (enumtype))
12370 && !same_type_p (underlying_type,
12371 ENUM_UNDERLYING_TYPE (enumtype)))
12372 {
12373 error_at (input_location, "different underlying type "
12374 "in enum %q#T", enumtype);
12375 error_at (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype)),
12376 "previous definition here");
12377 underlying_type = NULL_TREE;
12378 }
12379 }
12380
12381 if (!enumtype || TREE_CODE (enumtype) != ENUMERAL_TYPE
12382 || processing_template_decl)
12383 {
12384 /* In case of error, make a dummy enum to allow parsing to
12385 continue. */
12386 if (enumtype == error_mark_node)
12387 {
12388 name = make_anon_name ();
12389 enumtype = NULL_TREE;
12390 }
12391
12392 /* enumtype may be an ENUMERAL_TYPE if this is a redefinition
12393 of an opaque enum, or an opaque enum of an already defined
12394 enumeration (C++0x only).
12395 In any other case, it'll be NULL_TREE. */
12396 if (!enumtype)
12397 {
12398 if (is_new)
12399 *is_new = true;
12400 }
12401 prevtype = enumtype;
12402
12403 /* Do not push the decl more than once, unless we need to
12404 compare underlying types at instantiation time */
12405 if (!enumtype
12406 || TREE_CODE (enumtype) != ENUMERAL_TYPE
12407 || (underlying_type
12408 && dependent_type_p (underlying_type))
12409 || (ENUM_UNDERLYING_TYPE (enumtype)
12410 && dependent_type_p (ENUM_UNDERLYING_TYPE (enumtype))))
12411 {
12412 enumtype = cxx_make_type (ENUMERAL_TYPE);
12413 enumtype = pushtag (name, enumtype, /*tag_scope=*/ts_current);
12414 }
12415 else
12416 enumtype = xref_tag (enum_type, name, /*tag_scope=*/ts_current,
12417 false);
12418
12419 if (enumtype == error_mark_node)
12420 return error_mark_node;
12421
12422 /* The enum is considered opaque until the opening '{' of the
12423 enumerator list. */
12424 SET_OPAQUE_ENUM_P (enumtype, true);
12425 ENUM_FIXED_UNDERLYING_TYPE_P (enumtype) = !! underlying_type;
12426 }
12427
12428 SET_SCOPED_ENUM_P (enumtype, scoped_enum_p);
12429
12430 if (underlying_type)
12431 {
12432 if (CP_INTEGRAL_TYPE_P (underlying_type))
12433 {
12434 copy_type_enum (enumtype, underlying_type);
12435 ENUM_UNDERLYING_TYPE (enumtype) = underlying_type;
12436 }
12437 else if (dependent_type_p (underlying_type))
12438 ENUM_UNDERLYING_TYPE (enumtype) = underlying_type;
12439 else
12440 error ("underlying type %<%T%> of %<%T%> must be an integral type",
12441 underlying_type, enumtype);
12442 }
12443
12444 /* If into a template class, the returned enum is always the first
12445 declaration (opaque or not) seen. This way all the references to
12446 this type will be to the same declaration. The following ones are used
12447 only to check for definition errors. */
12448 if (prevtype && processing_template_decl)
12449 return prevtype;
12450 else
12451 return enumtype;
12452 }
12453
12454 /* After processing and defining all the values of an enumeration type,
12455 install their decls in the enumeration type.
12456 ENUMTYPE is the type object. */
12457
12458 void
12459 finish_enum_value_list (tree enumtype)
12460 {
12461 tree values;
12462 tree underlying_type;
12463 tree decl;
12464 tree value;
12465 tree minnode, maxnode;
12466 tree t;
12467
12468 bool fixed_underlying_type_p
12469 = ENUM_UNDERLYING_TYPE (enumtype) != NULL_TREE;
12470
12471 /* We built up the VALUES in reverse order. */
12472 TYPE_VALUES (enumtype) = nreverse (TYPE_VALUES (enumtype));
12473
12474 /* For an enum defined in a template, just set the type of the values;
12475 all further processing is postponed until the template is
12476 instantiated. We need to set the type so that tsubst of a CONST_DECL
12477 works. */
12478 if (processing_template_decl)
12479 {
12480 for (values = TYPE_VALUES (enumtype);
12481 values;
12482 values = TREE_CHAIN (values))
12483 TREE_TYPE (TREE_VALUE (values)) = enumtype;
12484 return;
12485 }
12486
12487 /* Determine the minimum and maximum values of the enumerators. */
12488 if (TYPE_VALUES (enumtype))
12489 {
12490 minnode = maxnode = NULL_TREE;
12491
12492 for (values = TYPE_VALUES (enumtype);
12493 values;
12494 values = TREE_CHAIN (values))
12495 {
12496 decl = TREE_VALUE (values);
12497
12498 /* [dcl.enum]: Following the closing brace of an enum-specifier,
12499 each enumerator has the type of its enumeration. Prior to the
12500 closing brace, the type of each enumerator is the type of its
12501 initializing value. */
12502 TREE_TYPE (decl) = enumtype;
12503
12504 /* Update the minimum and maximum values, if appropriate. */
12505 value = DECL_INITIAL (decl);
12506 if (value == error_mark_node)
12507 value = integer_zero_node;
12508 /* Figure out what the minimum and maximum values of the
12509 enumerators are. */
12510 if (!minnode)
12511 minnode = maxnode = value;
12512 else if (tree_int_cst_lt (maxnode, value))
12513 maxnode = value;
12514 else if (tree_int_cst_lt (value, minnode))
12515 minnode = value;
12516 }
12517 }
12518 else
12519 /* [dcl.enum]
12520
12521 If the enumerator-list is empty, the underlying type is as if
12522 the enumeration had a single enumerator with value 0. */
12523 minnode = maxnode = integer_zero_node;
12524
12525 if (!fixed_underlying_type_p)
12526 {
12527 /* Compute the number of bits require to represent all values of the
12528 enumeration. We must do this before the type of MINNODE and
12529 MAXNODE are transformed, since tree_int_cst_min_precision relies
12530 on the TREE_TYPE of the value it is passed. */
12531 bool unsignedp = tree_int_cst_sgn (minnode) >= 0;
12532 int lowprec = tree_int_cst_min_precision (minnode, unsignedp);
12533 int highprec = tree_int_cst_min_precision (maxnode, unsignedp);
12534 int precision = MAX (lowprec, highprec);
12535 unsigned int itk;
12536 bool use_short_enum;
12537
12538 /* Determine the underlying type of the enumeration.
12539
12540 [dcl.enum]
12541
12542 The underlying type of an enumeration is an integral type that
12543 can represent all the enumerator values defined in the
12544 enumeration. It is implementation-defined which integral type is
12545 used as the underlying type for an enumeration except that the
12546 underlying type shall not be larger than int unless the value of
12547 an enumerator cannot fit in an int or unsigned int.
12548
12549 We use "int" or an "unsigned int" as the underlying type, even if
12550 a smaller integral type would work, unless the user has
12551 explicitly requested that we use the smallest possible type. The
12552 user can request that for all enumerations with a command line
12553 flag, or for just one enumeration with an attribute. */
12554
12555 use_short_enum = flag_short_enums
12556 || lookup_attribute ("packed", TYPE_ATTRIBUTES (enumtype));
12557
12558 for (itk = (use_short_enum ? itk_char : itk_int);
12559 itk != itk_none;
12560 itk++)
12561 {
12562 underlying_type = integer_types[itk];
12563 if (underlying_type != NULL_TREE
12564 && TYPE_PRECISION (underlying_type) >= precision
12565 && TYPE_UNSIGNED (underlying_type) == unsignedp)
12566 break;
12567 }
12568 if (itk == itk_none)
12569 {
12570 /* DR 377
12571
12572 IF no integral type can represent all the enumerator values, the
12573 enumeration is ill-formed. */
12574 error ("no integral type can represent all of the enumerator values "
12575 "for %qT", enumtype);
12576 precision = TYPE_PRECISION (long_long_integer_type_node);
12577 underlying_type = integer_types[itk_unsigned_long_long];
12578 }
12579
12580 /* [dcl.enum]
12581
12582 The value of sizeof() applied to an enumeration type, an object
12583 of an enumeration type, or an enumerator, is the value of sizeof()
12584 applied to the underlying type. */
12585 copy_type_enum (enumtype, underlying_type);
12586
12587 /* Compute the minimum and maximum values for the type.
12588
12589 [dcl.enum]
12590
12591 For an enumeration where emin is the smallest enumerator and emax
12592 is the largest, the values of the enumeration are the values of the
12593 underlying type in the range bmin to bmax, where bmin and bmax are,
12594 respectively, the smallest and largest values of the smallest bit-
12595 field that can store emin and emax. */
12596
12597 /* The middle-end currently assumes that types with TYPE_PRECISION
12598 narrower than their underlying type are suitably zero or sign
12599 extended to fill their mode. Similarly, it assumes that the front
12600 end assures that a value of a particular type must be within
12601 TYPE_MIN_VALUE and TYPE_MAX_VALUE.
12602
12603 We used to set these fields based on bmin and bmax, but that led
12604 to invalid assumptions like optimizing away bounds checking. So
12605 now we just set the TYPE_PRECISION, TYPE_MIN_VALUE, and
12606 TYPE_MAX_VALUE to the values for the mode above and only restrict
12607 the ENUM_UNDERLYING_TYPE for the benefit of diagnostics. */
12608 ENUM_UNDERLYING_TYPE (enumtype)
12609 = build_distinct_type_copy (underlying_type);
12610 TYPE_PRECISION (ENUM_UNDERLYING_TYPE (enumtype)) = precision;
12611 set_min_and_max_values_for_integral_type
12612 (ENUM_UNDERLYING_TYPE (enumtype), precision, unsignedp);
12613
12614 /* If -fstrict-enums, still constrain TYPE_MIN/MAX_VALUE. */
12615 if (flag_strict_enums)
12616 set_min_and_max_values_for_integral_type (enumtype, precision,
12617 unsignedp);
12618 }
12619 else
12620 underlying_type = ENUM_UNDERLYING_TYPE (enumtype);
12621
12622 /* Convert each of the enumerators to the type of the underlying
12623 type of the enumeration. */
12624 for (values = TYPE_VALUES (enumtype); values; values = TREE_CHAIN (values))
12625 {
12626 location_t saved_location;
12627
12628 decl = TREE_VALUE (values);
12629 saved_location = input_location;
12630 input_location = DECL_SOURCE_LOCATION (decl);
12631 if (fixed_underlying_type_p)
12632 /* If the enumeration type has a fixed underlying type, we
12633 already checked all of the enumerator values. */
12634 value = DECL_INITIAL (decl);
12635 else
12636 value = perform_implicit_conversion (underlying_type,
12637 DECL_INITIAL (decl),
12638 tf_warning_or_error);
12639 input_location = saved_location;
12640
12641 /* Do not clobber shared ints. */
12642 value = copy_node (value);
12643
12644 TREE_TYPE (value) = enumtype;
12645 DECL_INITIAL (decl) = value;
12646 }
12647
12648 /* Fix up all variant types of this enum type. */
12649 for (t = TYPE_MAIN_VARIANT (enumtype); t; t = TYPE_NEXT_VARIANT (t))
12650 TYPE_VALUES (t) = TYPE_VALUES (enumtype);
12651
12652 if (at_class_scope_p ()
12653 && COMPLETE_TYPE_P (current_class_type)
12654 && UNSCOPED_ENUM_P (enumtype))
12655 insert_late_enum_def_into_classtype_sorted_fields (enumtype,
12656 current_class_type);
12657
12658 /* Finish debugging output for this type. */
12659 rest_of_type_compilation (enumtype, namespace_bindings_p ());
12660 }
12661
12662 /* Finishes the enum type. This is called only the first time an
12663 enumeration is seen, be it opaque or odinary.
12664 ENUMTYPE is the type object. */
12665
12666 void
12667 finish_enum (tree enumtype)
12668 {
12669 if (processing_template_decl)
12670 {
12671 if (at_function_scope_p ())
12672 add_stmt (build_min (TAG_DEFN, enumtype));
12673 return;
12674 }
12675
12676 /* If this is a forward declaration, there should not be any variants,
12677 though we can get a variant in the middle of an enum-specifier with
12678 wacky code like 'enum E { e = sizeof(const E*) };' */
12679 gcc_assert (enumtype == TYPE_MAIN_VARIANT (enumtype)
12680 && (TYPE_VALUES (enumtype)
12681 || !TYPE_NEXT_VARIANT (enumtype)));
12682 }
12683
12684 /* Build and install a CONST_DECL for an enumeration constant of the
12685 enumeration type ENUMTYPE whose NAME and VALUE (if any) are provided.
12686 LOC is the location of NAME.
12687 Assignment of sequential values by default is handled here. */
12688
12689 void
12690 build_enumerator (tree name, tree value, tree enumtype, location_t loc)
12691 {
12692 tree decl;
12693 tree context;
12694 tree type;
12695
12696 /* If the VALUE was erroneous, pretend it wasn't there; that will
12697 result in the enum being assigned the next value in sequence. */
12698 if (value == error_mark_node)
12699 value = NULL_TREE;
12700
12701 /* Remove no-op casts from the value. */
12702 if (value)
12703 STRIP_TYPE_NOPS (value);
12704
12705 if (! processing_template_decl)
12706 {
12707 /* Validate and default VALUE. */
12708 if (value != NULL_TREE)
12709 {
12710 value = cxx_constant_value (value);
12711
12712 if (TREE_CODE (value) != INTEGER_CST
12713 || ! INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (value)))
12714 {
12715 error ("enumerator value for %qD is not an integer constant",
12716 name);
12717 value = NULL_TREE;
12718 }
12719 }
12720
12721 /* Default based on previous value. */
12722 if (value == NULL_TREE)
12723 {
12724 if (TYPE_VALUES (enumtype))
12725 {
12726 tree prev_value;
12727 bool overflowed;
12728
12729 /* C++03 7.2/4: If no initializer is specified for the first
12730 enumerator, the type is an unspecified integral
12731 type. Otherwise the type is the same as the type of the
12732 initializing value of the preceding enumerator unless the
12733 incremented value is not representable in that type, in
12734 which case the type is an unspecified integral type
12735 sufficient to contain the incremented value. */
12736 prev_value = DECL_INITIAL (TREE_VALUE (TYPE_VALUES (enumtype)));
12737 if (error_operand_p (prev_value))
12738 value = error_mark_node;
12739 else
12740 {
12741 double_int di = TREE_INT_CST (prev_value)
12742 .add_with_sign (double_int_one,
12743 false, &overflowed);
12744 if (!overflowed)
12745 {
12746 tree type = TREE_TYPE (prev_value);
12747 bool pos = TYPE_UNSIGNED (type) || !di.is_negative ();
12748 if (!double_int_fits_to_tree_p (type, di))
12749 {
12750 unsigned int itk;
12751 for (itk = itk_int; itk != itk_none; itk++)
12752 {
12753 type = integer_types[itk];
12754 if (type != NULL_TREE
12755 && (pos || !TYPE_UNSIGNED (type))
12756 && double_int_fits_to_tree_p (type, di))
12757 break;
12758 }
12759 if (type && cxx_dialect < cxx0x
12760 && itk > itk_unsigned_long)
12761 pedwarn (input_location, OPT_Wlong_long, pos ? "\
12762 incremented enumerator value is too large for %<unsigned long%>" : "\
12763 incremented enumerator value is too large for %<long%>");
12764 }
12765 if (type == NULL_TREE)
12766 overflowed = true;
12767 else
12768 value = double_int_to_tree (type, di);
12769 }
12770
12771 if (overflowed)
12772 {
12773 error ("overflow in enumeration values at %qD", name);
12774 value = error_mark_node;
12775 }
12776 }
12777 }
12778 else
12779 value = integer_zero_node;
12780 }
12781
12782 /* Remove no-op casts from the value. */
12783 STRIP_TYPE_NOPS (value);
12784
12785 /* If the underlying type of the enum is fixed, check whether
12786 the enumerator values fits in the underlying type. If it
12787 does not fit, the program is ill-formed [C++0x dcl.enum]. */
12788 if (ENUM_UNDERLYING_TYPE (enumtype)
12789 && value
12790 && TREE_CODE (value) == INTEGER_CST
12791 && !int_fits_type_p (value, ENUM_UNDERLYING_TYPE (enumtype)))
12792 {
12793 error ("enumerator value %E is too large for underlying type %<%T%>",
12794 value, ENUM_UNDERLYING_TYPE (enumtype));
12795
12796 /* Silently convert the value so that we can continue. */
12797 value = perform_implicit_conversion (ENUM_UNDERLYING_TYPE (enumtype),
12798 value, tf_none);
12799 }
12800 }
12801
12802 /* C++ associates enums with global, function, or class declarations. */
12803 context = current_scope ();
12804
12805 /* Build the actual enumeration constant. Note that the enumeration
12806 constants have the underlying type of the enum (if it is fixed)
12807 or the type of their initializer (if the underlying type of the
12808 enum is not fixed):
12809
12810 [ C++0x dcl.enum ]
12811
12812 If the underlying type is fixed, the type of each enumerator
12813 prior to the closing brace is the underlying type; if the
12814 initializing value of an enumerator cannot be represented by
12815 the underlying type, the program is ill-formed. If the
12816 underlying type is not fixed, the type of each enumerator is
12817 the type of its initializing value.
12818
12819 If the underlying type is not fixed, it will be computed by
12820 finish_enum and we will reset the type of this enumerator. Of
12821 course, if we're processing a template, there may be no value. */
12822 type = value ? TREE_TYPE (value) : NULL_TREE;
12823
12824 decl = build_decl (loc, CONST_DECL, name, type);
12825
12826 DECL_CONTEXT (decl) = enumtype;
12827 TREE_CONSTANT (decl) = 1;
12828 TREE_READONLY (decl) = 1;
12829 DECL_INITIAL (decl) = value;
12830
12831 if (context && context == current_class_type && !SCOPED_ENUM_P (enumtype))
12832 /* In something like `struct S { enum E { i = 7 }; };' we put `i'
12833 on the TYPE_FIELDS list for `S'. (That's so that you can say
12834 things like `S::i' later.) */
12835 finish_member_declaration (decl);
12836 else
12837 pushdecl (decl);
12838
12839 /* Add this enumeration constant to the list for this type. */
12840 TYPE_VALUES (enumtype) = tree_cons (name, decl, TYPE_VALUES (enumtype));
12841 }
12842
12843 /* Look for an enumerator with the given NAME within the enumeration
12844 type ENUMTYPE. This routine is used primarily for qualified name
12845 lookup into an enumerator in C++0x, e.g.,
12846
12847 enum class Color { Red, Green, Blue };
12848
12849 Color color = Color::Red;
12850
12851 Returns the value corresponding to the enumerator, or
12852 NULL_TREE if no such enumerator was found. */
12853 tree
12854 lookup_enumerator (tree enumtype, tree name)
12855 {
12856 tree e;
12857 gcc_assert (enumtype && TREE_CODE (enumtype) == ENUMERAL_TYPE);
12858
12859 e = purpose_member (name, TYPE_VALUES (enumtype));
12860 return e? TREE_VALUE (e) : NULL_TREE;
12861 }
12862
12863 \f
12864 /* We're defining DECL. Make sure that its type is OK. */
12865
12866 static void
12867 check_function_type (tree decl, tree current_function_parms)
12868 {
12869 tree fntype = TREE_TYPE (decl);
12870 tree return_type = complete_type (TREE_TYPE (fntype));
12871
12872 /* In a function definition, arg types must be complete. */
12873 require_complete_types_for_parms (current_function_parms);
12874
12875 if (dependent_type_p (return_type)
12876 || type_uses_auto (return_type))
12877 return;
12878 if (!COMPLETE_OR_VOID_TYPE_P (return_type)
12879 || (TYPE_FOR_JAVA (return_type) && MAYBE_CLASS_TYPE_P (return_type)))
12880 {
12881 tree args = TYPE_ARG_TYPES (fntype);
12882
12883 if (!COMPLETE_OR_VOID_TYPE_P (return_type))
12884 error ("return type %q#T is incomplete", return_type);
12885 else
12886 error ("return type has Java class type %q#T", return_type);
12887
12888 /* Make it return void instead. */
12889 if (TREE_CODE (fntype) == METHOD_TYPE)
12890 fntype = build_method_type_directly (TREE_TYPE (TREE_VALUE (args)),
12891 void_type_node,
12892 TREE_CHAIN (args));
12893 else
12894 fntype = build_function_type (void_type_node, args);
12895 fntype
12896 = build_exception_variant (fntype,
12897 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (decl)));
12898 fntype = (cp_build_type_attribute_variant
12899 (fntype, TYPE_ATTRIBUTES (TREE_TYPE (decl))));
12900 TREE_TYPE (decl) = fntype;
12901 }
12902 else
12903 abstract_virtuals_error (decl, TREE_TYPE (fntype));
12904 }
12905
12906 /* Create the FUNCTION_DECL for a function definition.
12907 DECLSPECS and DECLARATOR are the parts of the declaration;
12908 they describe the function's name and the type it returns,
12909 but twisted together in a fashion that parallels the syntax of C.
12910
12911 FLAGS is a bitwise or of SF_PRE_PARSED (indicating that the
12912 DECLARATOR is really the DECL for the function we are about to
12913 process and that DECLSPECS should be ignored), SF_INCLASS_INLINE
12914 indicating that the function is an inline defined in-class.
12915
12916 This function creates a binding context for the function body
12917 as well as setting up the FUNCTION_DECL in current_function_decl.
12918
12919 For C++, we must first check whether that datum makes any sense.
12920 For example, "class A local_a(1,2);" means that variable local_a
12921 is an aggregate of type A, which should have a constructor
12922 applied to it with the argument list [1, 2].
12923
12924 On entry, DECL_INITIAL (decl1) should be NULL_TREE or error_mark_node,
12925 or may be a BLOCK if the function has been defined previously
12926 in this translation unit. On exit, DECL_INITIAL (decl1) will be
12927 error_mark_node if the function has never been defined, or
12928 a BLOCK if the function has been defined somewhere. */
12929
12930 void
12931 start_preparsed_function (tree decl1, tree attrs, int flags)
12932 {
12933 tree ctype = NULL_TREE;
12934 tree fntype;
12935 tree restype;
12936 int doing_friend = 0;
12937 cp_binding_level *bl;
12938 tree current_function_parms;
12939 struct c_fileinfo *finfo
12940 = get_fileinfo (LOCATION_FILE (DECL_SOURCE_LOCATION (decl1)));
12941 bool honor_interface;
12942
12943 /* Sanity check. */
12944 gcc_assert (TREE_CODE (TREE_VALUE (void_list_node)) == VOID_TYPE);
12945 gcc_assert (TREE_CHAIN (void_list_node) == NULL_TREE);
12946
12947 fntype = TREE_TYPE (decl1);
12948 if (TREE_CODE (fntype) == METHOD_TYPE)
12949 ctype = TYPE_METHOD_BASETYPE (fntype);
12950
12951 /* ISO C++ 11.4/5. A friend function defined in a class is in
12952 the (lexical) scope of the class in which it is defined. */
12953 if (!ctype && DECL_FRIEND_P (decl1))
12954 {
12955 ctype = DECL_FRIEND_CONTEXT (decl1);
12956
12957 /* CTYPE could be null here if we're dealing with a template;
12958 for example, `inline friend float foo()' inside a template
12959 will have no CTYPE set. */
12960 if (ctype && TREE_CODE (ctype) != RECORD_TYPE)
12961 ctype = NULL_TREE;
12962 else
12963 doing_friend = 1;
12964 }
12965
12966 if (DECL_DECLARED_INLINE_P (decl1)
12967 && lookup_attribute ("noinline", attrs))
12968 warning (0, "inline function %q+D given attribute noinline", decl1);
12969
12970 /* Handle gnu_inline attribute. */
12971 if (GNU_INLINE_P (decl1))
12972 {
12973 DECL_EXTERNAL (decl1) = 1;
12974 DECL_NOT_REALLY_EXTERN (decl1) = 0;
12975 DECL_INTERFACE_KNOWN (decl1) = 1;
12976 DECL_DISREGARD_INLINE_LIMITS (decl1) = 1;
12977 }
12978
12979 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (decl1))
12980 /* This is a constructor, we must ensure that any default args
12981 introduced by this definition are propagated to the clones
12982 now. The clones are used directly in overload resolution. */
12983 adjust_clone_args (decl1);
12984
12985 /* Sometimes we don't notice that a function is a static member, and
12986 build a METHOD_TYPE for it. Fix that up now. */
12987 gcc_assert (!(ctype != NULL_TREE && DECL_STATIC_FUNCTION_P (decl1)
12988 && TREE_CODE (TREE_TYPE (decl1)) == METHOD_TYPE));
12989
12990 /* Set up current_class_type, and enter the scope of the class, if
12991 appropriate. */
12992 if (ctype)
12993 push_nested_class (ctype);
12994 else if (DECL_STATIC_FUNCTION_P (decl1))
12995 push_nested_class (DECL_CONTEXT (decl1));
12996
12997 /* Now that we have entered the scope of the class, we must restore
12998 the bindings for any template parameters surrounding DECL1, if it
12999 is an inline member template. (Order is important; consider the
13000 case where a template parameter has the same name as a field of
13001 the class.) It is not until after this point that
13002 PROCESSING_TEMPLATE_DECL is guaranteed to be set up correctly. */
13003 if (flags & SF_INCLASS_INLINE)
13004 maybe_begin_member_template_processing (decl1);
13005
13006 /* Effective C++ rule 15. */
13007 if (warn_ecpp
13008 && DECL_OVERLOADED_OPERATOR_P (decl1) == NOP_EXPR
13009 && TREE_CODE (TREE_TYPE (fntype)) == VOID_TYPE)
13010 warning (OPT_Weffc__, "%<operator=%> should return a reference to %<*this%>");
13011
13012 /* Make the init_value nonzero so pushdecl knows this is not tentative.
13013 error_mark_node is replaced below (in poplevel) with the BLOCK. */
13014 if (!DECL_INITIAL (decl1))
13015 DECL_INITIAL (decl1) = error_mark_node;
13016
13017 /* This function exists in static storage.
13018 (This does not mean `static' in the C sense!) */
13019 TREE_STATIC (decl1) = 1;
13020
13021 /* We must call push_template_decl after current_class_type is set
13022 up. (If we are processing inline definitions after exiting a
13023 class scope, current_class_type will be NULL_TREE until set above
13024 by push_nested_class.) */
13025 if (processing_template_decl)
13026 {
13027 /* FIXME: Handle error_mark_node more gracefully. */
13028 tree newdecl1 = push_template_decl (decl1);
13029 if (newdecl1 != error_mark_node)
13030 decl1 = newdecl1;
13031 }
13032
13033 /* We are now in the scope of the function being defined. */
13034 current_function_decl = decl1;
13035
13036 /* Save the parm names or decls from this function's declarator
13037 where store_parm_decls will find them. */
13038 current_function_parms = DECL_ARGUMENTS (decl1);
13039
13040 /* Make sure the parameter and return types are reasonable. When
13041 you declare a function, these types can be incomplete, but they
13042 must be complete when you define the function. */
13043 check_function_type (decl1, current_function_parms);
13044
13045 /* Build the return declaration for the function. */
13046 restype = TREE_TYPE (fntype);
13047
13048 if (DECL_RESULT (decl1) == NULL_TREE)
13049 {
13050 tree resdecl;
13051
13052 resdecl = build_decl (input_location, RESULT_DECL, 0, restype);
13053 DECL_ARTIFICIAL (resdecl) = 1;
13054 DECL_IGNORED_P (resdecl) = 1;
13055 DECL_RESULT (decl1) = resdecl;
13056
13057 cp_apply_type_quals_to_decl (cp_type_quals (restype), resdecl);
13058 }
13059
13060 /* Let the user know we're compiling this function. */
13061 announce_function (decl1);
13062
13063 /* Record the decl so that the function name is defined.
13064 If we already have a decl for this name, and it is a FUNCTION_DECL,
13065 use the old decl. */
13066 if (!processing_template_decl && !(flags & SF_PRE_PARSED))
13067 {
13068 /* A specialization is not used to guide overload resolution. */
13069 if (!DECL_FUNCTION_MEMBER_P (decl1)
13070 && !(DECL_USE_TEMPLATE (decl1) &&
13071 PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl1))))
13072 {
13073 tree olddecl = pushdecl (decl1);
13074
13075 if (olddecl == error_mark_node)
13076 /* If something went wrong when registering the declaration,
13077 use DECL1; we have to have a FUNCTION_DECL to use when
13078 parsing the body of the function. */
13079 ;
13080 else
13081 {
13082 /* Otherwise, OLDDECL is either a previous declaration
13083 of the same function or DECL1 itself. */
13084
13085 if (warn_missing_declarations
13086 && olddecl == decl1
13087 && !DECL_MAIN_P (decl1)
13088 && TREE_PUBLIC (decl1)
13089 && !DECL_DECLARED_INLINE_P (decl1))
13090 {
13091 tree context;
13092
13093 /* Check whether DECL1 is in an anonymous
13094 namespace. */
13095 for (context = DECL_CONTEXT (decl1);
13096 context;
13097 context = DECL_CONTEXT (context))
13098 {
13099 if (TREE_CODE (context) == NAMESPACE_DECL
13100 && DECL_NAME (context) == NULL_TREE)
13101 break;
13102 }
13103
13104 if (context == NULL)
13105 warning (OPT_Wmissing_declarations,
13106 "no previous declaration for %q+D", decl1);
13107 }
13108
13109 decl1 = olddecl;
13110 }
13111 }
13112 else
13113 {
13114 /* We need to set the DECL_CONTEXT. */
13115 if (!DECL_CONTEXT (decl1) && DECL_TEMPLATE_INFO (decl1))
13116 DECL_CONTEXT (decl1) = DECL_CONTEXT (DECL_TI_TEMPLATE (decl1));
13117 }
13118 fntype = TREE_TYPE (decl1);
13119 restype = TREE_TYPE (fntype);
13120
13121 /* If #pragma weak applies, mark the decl appropriately now.
13122 The pragma only applies to global functions. Because
13123 determining whether or not the #pragma applies involves
13124 computing the mangled name for the declaration, we cannot
13125 apply the pragma until after we have merged this declaration
13126 with any previous declarations; if the original declaration
13127 has a linkage specification, that specification applies to
13128 the definition as well, and may affect the mangled name. */
13129 if (DECL_FILE_SCOPE_P (decl1))
13130 maybe_apply_pragma_weak (decl1);
13131 }
13132
13133 /* Reset this in case the call to pushdecl changed it. */
13134 current_function_decl = decl1;
13135
13136 gcc_assert (DECL_INITIAL (decl1));
13137
13138 /* This function may already have been parsed, in which case just
13139 return; our caller will skip over the body without parsing. */
13140 if (DECL_INITIAL (decl1) != error_mark_node)
13141 return;
13142
13143 /* Initialize RTL machinery. We cannot do this until
13144 CURRENT_FUNCTION_DECL and DECL_RESULT are set up. We do this
13145 even when processing a template; this is how we get
13146 CFUN set up, and our per-function variables initialized.
13147 FIXME factor out the non-RTL stuff. */
13148 bl = current_binding_level;
13149 allocate_struct_function (decl1, processing_template_decl);
13150
13151 /* Initialize the language data structures. Whenever we start
13152 a new function, we destroy temporaries in the usual way. */
13153 cfun->language = ggc_alloc_cleared_language_function ();
13154 current_stmt_tree ()->stmts_are_full_exprs_p = 1;
13155 current_binding_level = bl;
13156
13157 if (!processing_template_decl && type_uses_auto (restype))
13158 {
13159 FNDECL_USED_AUTO (decl1) = true;
13160 current_function_auto_return_pattern = restype;
13161 }
13162
13163 /* Start the statement-tree, start the tree now. */
13164 DECL_SAVED_TREE (decl1) = push_stmt_list ();
13165
13166 /* If we are (erroneously) defining a function that we have already
13167 defined before, wipe out what we knew before. */
13168 if (!DECL_PENDING_INLINE_P (decl1))
13169 DECL_SAVED_FUNCTION_DATA (decl1) = NULL;
13170
13171 if (ctype && !doing_friend && !DECL_STATIC_FUNCTION_P (decl1))
13172 {
13173 /* We know that this was set up by `grokclassfn'. We do not
13174 wait until `store_parm_decls', since evil parse errors may
13175 never get us to that point. Here we keep the consistency
13176 between `current_class_type' and `current_class_ptr'. */
13177 tree t = DECL_ARGUMENTS (decl1);
13178
13179 gcc_assert (t != NULL_TREE && TREE_CODE (t) == PARM_DECL);
13180 gcc_assert (TREE_CODE (TREE_TYPE (t)) == POINTER_TYPE);
13181
13182 cp_function_chain->x_current_class_ref
13183 = cp_build_indirect_ref (t, RO_NULL, tf_warning_or_error);
13184 /* Set this second to avoid shortcut in cp_build_indirect_ref. */
13185 cp_function_chain->x_current_class_ptr = t;
13186
13187 /* Constructors and destructors need to know whether they're "in
13188 charge" of initializing virtual base classes. */
13189 t = DECL_CHAIN (t);
13190 if (DECL_HAS_IN_CHARGE_PARM_P (decl1))
13191 {
13192 current_in_charge_parm = t;
13193 t = DECL_CHAIN (t);
13194 }
13195 if (DECL_HAS_VTT_PARM_P (decl1))
13196 {
13197 gcc_assert (DECL_NAME (t) == vtt_parm_identifier);
13198 current_vtt_parm = t;
13199 }
13200 }
13201
13202 honor_interface = (!DECL_TEMPLATE_INSTANTIATION (decl1)
13203 /* Implicitly-defined methods (like the
13204 destructor for a class in which no destructor
13205 is explicitly declared) must not be defined
13206 until their definition is needed. So, we
13207 ignore interface specifications for
13208 compiler-generated functions. */
13209 && !DECL_ARTIFICIAL (decl1));
13210
13211 if (processing_template_decl)
13212 /* Don't mess with interface flags. */;
13213 else if (DECL_INTERFACE_KNOWN (decl1))
13214 {
13215 tree ctx = decl_function_context (decl1);
13216
13217 if (DECL_NOT_REALLY_EXTERN (decl1))
13218 DECL_EXTERNAL (decl1) = 0;
13219
13220 if (ctx != NULL_TREE && vague_linkage_p (ctx))
13221 /* This is a function in a local class in an extern inline
13222 or template function. */
13223 comdat_linkage (decl1);
13224 }
13225 /* If this function belongs to an interface, it is public.
13226 If it belongs to someone else's interface, it is also external.
13227 This only affects inlines and template instantiations. */
13228 else if (!finfo->interface_unknown && honor_interface)
13229 {
13230 if (DECL_DECLARED_INLINE_P (decl1)
13231 || DECL_TEMPLATE_INSTANTIATION (decl1))
13232 {
13233 DECL_EXTERNAL (decl1)
13234 = (finfo->interface_only
13235 || (DECL_DECLARED_INLINE_P (decl1)
13236 && ! flag_implement_inlines
13237 && !DECL_VINDEX (decl1)));
13238
13239 /* For WIN32 we also want to put these in linkonce sections. */
13240 maybe_make_one_only (decl1);
13241 }
13242 else
13243 DECL_EXTERNAL (decl1) = 0;
13244 DECL_INTERFACE_KNOWN (decl1) = 1;
13245 /* If this function is in an interface implemented in this file,
13246 make sure that the back end knows to emit this function
13247 here. */
13248 if (!DECL_EXTERNAL (decl1))
13249 mark_needed (decl1);
13250 }
13251 else if (finfo->interface_unknown && finfo->interface_only
13252 && honor_interface)
13253 {
13254 /* If MULTIPLE_SYMBOL_SPACES is defined and we saw a #pragma
13255 interface, we will have both finfo->interface_unknown and
13256 finfo->interface_only set. In that case, we don't want to
13257 use the normal heuristics because someone will supply a
13258 #pragma implementation elsewhere, and deducing it here would
13259 produce a conflict. */
13260 comdat_linkage (decl1);
13261 DECL_EXTERNAL (decl1) = 0;
13262 DECL_INTERFACE_KNOWN (decl1) = 1;
13263 DECL_DEFER_OUTPUT (decl1) = 1;
13264 }
13265 else
13266 {
13267 /* This is a definition, not a reference.
13268 So clear DECL_EXTERNAL, unless this is a GNU extern inline. */
13269 if (!GNU_INLINE_P (decl1))
13270 DECL_EXTERNAL (decl1) = 0;
13271
13272 if ((DECL_DECLARED_INLINE_P (decl1)
13273 || DECL_TEMPLATE_INSTANTIATION (decl1))
13274 && ! DECL_INTERFACE_KNOWN (decl1))
13275 DECL_DEFER_OUTPUT (decl1) = 1;
13276 else
13277 DECL_INTERFACE_KNOWN (decl1) = 1;
13278 }
13279
13280 /* Determine the ELF visibility attribute for the function. We must not
13281 do this before calling "pushdecl", as we must allow "duplicate_decls"
13282 to merge any attributes appropriately. We also need to wait until
13283 linkage is set. */
13284 if (!DECL_CLONED_FUNCTION_P (decl1))
13285 determine_visibility (decl1);
13286
13287 begin_scope (sk_function_parms, decl1);
13288
13289 ++function_depth;
13290
13291 if (DECL_DESTRUCTOR_P (decl1)
13292 || (DECL_CONSTRUCTOR_P (decl1)
13293 && targetm.cxx.cdtor_returns_this ()))
13294 {
13295 cdtor_label = build_decl (input_location,
13296 LABEL_DECL, NULL_TREE, NULL_TREE);
13297 DECL_CONTEXT (cdtor_label) = current_function_decl;
13298 }
13299
13300 start_fname_decls ();
13301
13302 store_parm_decls (current_function_parms);
13303 }
13304
13305
13306 /* Like start_preparsed_function, except that instead of a
13307 FUNCTION_DECL, this function takes DECLSPECS and DECLARATOR.
13308
13309 Returns 1 on success. If the DECLARATOR is not suitable for a function
13310 (it defines a datum instead), we return 0, which tells
13311 yyparse to report a parse error. */
13312
13313 int
13314 start_function (cp_decl_specifier_seq *declspecs,
13315 const cp_declarator *declarator,
13316 tree attrs)
13317 {
13318 tree decl1;
13319
13320 decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, 1, &attrs);
13321 if (decl1 == error_mark_node)
13322 return 0;
13323 /* If the declarator is not suitable for a function definition,
13324 cause a syntax error. */
13325 if (decl1 == NULL_TREE || TREE_CODE (decl1) != FUNCTION_DECL)
13326 {
13327 error ("invalid function declaration");
13328 return 0;
13329 }
13330
13331 if (DECL_MAIN_P (decl1))
13332 /* main must return int. grokfndecl should have corrected it
13333 (and issued a diagnostic) if the user got it wrong. */
13334 gcc_assert (same_type_p (TREE_TYPE (TREE_TYPE (decl1)),
13335 integer_type_node));
13336
13337 start_preparsed_function (decl1, attrs, /*flags=*/SF_DEFAULT);
13338
13339 return 1;
13340 }
13341 \f
13342 /* Returns true iff an EH_SPEC_BLOCK should be created in the body of
13343 FN. */
13344
13345 static bool
13346 use_eh_spec_block (tree fn)
13347 {
13348 return (flag_exceptions && flag_enforce_eh_specs
13349 && !processing_template_decl
13350 && !type_throw_all_p (TREE_TYPE (fn))
13351 /* We insert the EH_SPEC_BLOCK only in the original
13352 function; then, it is copied automatically to the
13353 clones. */
13354 && !DECL_CLONED_FUNCTION_P (fn)
13355 /* Implicitly-generated constructors and destructors have
13356 exception specifications. However, those specifications
13357 are the union of the possible exceptions specified by the
13358 constructors/destructors for bases and members, so no
13359 unallowed exception will ever reach this function. By
13360 not creating the EH_SPEC_BLOCK we save a little memory,
13361 and we avoid spurious warnings about unreachable
13362 code. */
13363 && !DECL_DEFAULTED_FN (fn));
13364 }
13365
13366 /* Store the parameter declarations into the current function declaration.
13367 This is called after parsing the parameter declarations, before
13368 digesting the body of the function.
13369
13370 Also install to binding contour return value identifier, if any. */
13371
13372 static void
13373 store_parm_decls (tree current_function_parms)
13374 {
13375 tree fndecl = current_function_decl;
13376 tree parm;
13377
13378 /* This is a chain of any other decls that came in among the parm
13379 declarations. If a parm is declared with enum {foo, bar} x;
13380 then CONST_DECLs for foo and bar are put here. */
13381 tree nonparms = NULL_TREE;
13382
13383 if (current_function_parms)
13384 {
13385 /* This case is when the function was defined with an ANSI prototype.
13386 The parms already have decls, so we need not do anything here
13387 except record them as in effect
13388 and complain if any redundant old-style parm decls were written. */
13389
13390 tree specparms = current_function_parms;
13391 tree next;
13392
13393 /* Must clear this because it might contain TYPE_DECLs declared
13394 at class level. */
13395 current_binding_level->names = NULL;
13396
13397 /* If we're doing semantic analysis, then we'll call pushdecl
13398 for each of these. We must do them in reverse order so that
13399 they end in the correct forward order. */
13400 specparms = nreverse (specparms);
13401
13402 for (parm = specparms; parm; parm = next)
13403 {
13404 next = DECL_CHAIN (parm);
13405 if (TREE_CODE (parm) == PARM_DECL)
13406 {
13407 if (DECL_NAME (parm) == NULL_TREE
13408 || TREE_CODE (parm) != VOID_TYPE)
13409 pushdecl (parm);
13410 else
13411 error ("parameter %qD declared void", parm);
13412 }
13413 else
13414 {
13415 /* If we find an enum constant or a type tag,
13416 put it aside for the moment. */
13417 TREE_CHAIN (parm) = NULL_TREE;
13418 nonparms = chainon (nonparms, parm);
13419 }
13420 }
13421
13422 /* Get the decls in their original chain order and record in the
13423 function. This is all and only the PARM_DECLs that were
13424 pushed into scope by the loop above. */
13425 DECL_ARGUMENTS (fndecl) = getdecls ();
13426 }
13427 else
13428 DECL_ARGUMENTS (fndecl) = NULL_TREE;
13429
13430 /* Now store the final chain of decls for the arguments
13431 as the decl-chain of the current lexical scope.
13432 Put the enumerators in as well, at the front so that
13433 DECL_ARGUMENTS is not modified. */
13434 current_binding_level->names = chainon (nonparms, DECL_ARGUMENTS (fndecl));
13435
13436 if (use_eh_spec_block (current_function_decl))
13437 current_eh_spec_block = begin_eh_spec_block ();
13438 }
13439
13440 \f
13441 /* We have finished doing semantic analysis on DECL, but have not yet
13442 generated RTL for its body. Save away our current state, so that
13443 when we want to generate RTL later we know what to do. */
13444
13445 static void
13446 save_function_data (tree decl)
13447 {
13448 struct language_function *f;
13449
13450 /* Save the language-specific per-function data so that we can
13451 get it back when we really expand this function. */
13452 gcc_assert (!DECL_PENDING_INLINE_P (decl));
13453
13454 /* Make a copy. */
13455 f = ggc_alloc_language_function ();
13456 memcpy (f, cp_function_chain, sizeof (struct language_function));
13457 DECL_SAVED_FUNCTION_DATA (decl) = f;
13458
13459 /* Clear out the bits we don't need. */
13460 f->base.x_stmt_tree.x_cur_stmt_list = NULL;
13461 f->bindings = NULL;
13462 f->x_local_names = NULL;
13463 f->base.local_typedefs = NULL;
13464 }
13465
13466
13467 /* Set the return value of the constructor (if present). */
13468
13469 static void
13470 finish_constructor_body (void)
13471 {
13472 tree val;
13473 tree exprstmt;
13474
13475 if (targetm.cxx.cdtor_returns_this ()
13476 && (! TYPE_FOR_JAVA (current_class_type)))
13477 {
13478 /* Any return from a constructor will end up here. */
13479 add_stmt (build_stmt (input_location, LABEL_EXPR, cdtor_label));
13480
13481 val = DECL_ARGUMENTS (current_function_decl);
13482 val = build2 (MODIFY_EXPR, TREE_TYPE (val),
13483 DECL_RESULT (current_function_decl), val);
13484 /* Return the address of the object. */
13485 exprstmt = build_stmt (input_location, RETURN_EXPR, val);
13486 add_stmt (exprstmt);
13487 }
13488 }
13489
13490 /* Do all the processing for the beginning of a destructor; set up the
13491 vtable pointers and cleanups for bases and members. */
13492
13493 static void
13494 begin_destructor_body (void)
13495 {
13496 tree compound_stmt;
13497
13498 /* If the CURRENT_CLASS_TYPE is incomplete, we will have already
13499 issued an error message. We still want to try to process the
13500 body of the function, but initialize_vtbl_ptrs will crash if
13501 TYPE_BINFO is NULL. */
13502 if (COMPLETE_TYPE_P (current_class_type))
13503 {
13504 compound_stmt = begin_compound_stmt (0);
13505 /* Make all virtual function table pointers in non-virtual base
13506 classes point to CURRENT_CLASS_TYPE's virtual function
13507 tables. */
13508 initialize_vtbl_ptrs (current_class_ptr);
13509 finish_compound_stmt (compound_stmt);
13510
13511 /* And insert cleanups for our bases and members so that they
13512 will be properly destroyed if we throw. */
13513 push_base_cleanups ();
13514 }
13515 }
13516
13517 /* At the end of every destructor we generate code to delete the object if
13518 necessary. Do that now. */
13519
13520 static void
13521 finish_destructor_body (void)
13522 {
13523 tree exprstmt;
13524
13525 /* Any return from a destructor will end up here; that way all base
13526 and member cleanups will be run when the function returns. */
13527 add_stmt (build_stmt (input_location, LABEL_EXPR, cdtor_label));
13528
13529 /* In a virtual destructor, we must call delete. */
13530 if (DECL_VIRTUAL_P (current_function_decl))
13531 {
13532 tree if_stmt;
13533 tree virtual_size = cxx_sizeof (current_class_type);
13534
13535 /* [class.dtor]
13536
13537 At the point of definition of a virtual destructor (including
13538 an implicit definition), non-placement operator delete shall
13539 be looked up in the scope of the destructor's class and if
13540 found shall be accessible and unambiguous. */
13541 exprstmt = build_op_delete_call (DELETE_EXPR, current_class_ptr,
13542 virtual_size,
13543 /*global_p=*/false,
13544 /*placement=*/NULL_TREE,
13545 /*alloc_fn=*/NULL_TREE,
13546 tf_warning_or_error);
13547
13548 if_stmt = begin_if_stmt ();
13549 finish_if_stmt_cond (build2 (BIT_AND_EXPR, integer_type_node,
13550 current_in_charge_parm,
13551 integer_one_node),
13552 if_stmt);
13553 finish_expr_stmt (exprstmt);
13554 finish_then_clause (if_stmt);
13555 finish_if_stmt (if_stmt);
13556 }
13557
13558 if (targetm.cxx.cdtor_returns_this ())
13559 {
13560 tree val;
13561
13562 val = DECL_ARGUMENTS (current_function_decl);
13563 val = build2 (MODIFY_EXPR, TREE_TYPE (val),
13564 DECL_RESULT (current_function_decl), val);
13565 /* Return the address of the object. */
13566 exprstmt = build_stmt (input_location, RETURN_EXPR, val);
13567 add_stmt (exprstmt);
13568 }
13569 }
13570
13571 /* Do the necessary processing for the beginning of a function body, which
13572 in this case includes member-initializers, but not the catch clauses of
13573 a function-try-block. Currently, this means opening a binding level
13574 for the member-initializers (in a ctor), member cleanups (in a dtor),
13575 and capture proxies (in a lambda operator()). */
13576
13577 tree
13578 begin_function_body (void)
13579 {
13580 tree stmt;
13581
13582 if (! FUNCTION_NEEDS_BODY_BLOCK (current_function_decl))
13583 return NULL_TREE;
13584
13585 if (processing_template_decl)
13586 /* Do nothing now. */;
13587 else
13588 /* Always keep the BLOCK node associated with the outermost pair of
13589 curly braces of a function. These are needed for correct
13590 operation of dwarfout.c. */
13591 keep_next_level (true);
13592
13593 stmt = begin_compound_stmt (BCS_FN_BODY);
13594
13595 if (processing_template_decl)
13596 /* Do nothing now. */;
13597 else if (DECL_DESTRUCTOR_P (current_function_decl))
13598 begin_destructor_body ();
13599
13600 return stmt;
13601 }
13602
13603 /* Do the processing for the end of a function body. Currently, this means
13604 closing out the cleanups for fully-constructed bases and members, and in
13605 the case of the destructor, deleting the object if desired. Again, this
13606 is only meaningful for [cd]tors, since they are the only functions where
13607 there is a significant distinction between the main body and any
13608 function catch clauses. Handling, say, main() return semantics here
13609 would be wrong, as flowing off the end of a function catch clause for
13610 main() would also need to return 0. */
13611
13612 void
13613 finish_function_body (tree compstmt)
13614 {
13615 if (compstmt == NULL_TREE)
13616 return;
13617
13618 /* Close the block. */
13619 finish_compound_stmt (compstmt);
13620
13621 if (processing_template_decl)
13622 /* Do nothing now. */;
13623 else if (DECL_CONSTRUCTOR_P (current_function_decl))
13624 finish_constructor_body ();
13625 else if (DECL_DESTRUCTOR_P (current_function_decl))
13626 finish_destructor_body ();
13627 }
13628
13629 /* Given a function, returns the BLOCK corresponding to the outermost level
13630 of curly braces, skipping the artificial block created for constructor
13631 initializers. */
13632
13633 tree
13634 outer_curly_brace_block (tree fndecl)
13635 {
13636 tree block = BLOCK_SUBBLOCKS (DECL_INITIAL (fndecl));
13637 if (FUNCTION_NEEDS_BODY_BLOCK (current_function_decl))
13638 /* Skip the artificial function body block. */
13639 block = BLOCK_SUBBLOCKS (block);
13640 return block;
13641 }
13642
13643 /* If FNDECL is a class's key method, add the class to the list of
13644 keyed classes that should be emitted. */
13645
13646 static void
13647 record_key_method_defined (tree fndecl)
13648 {
13649 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fndecl)
13650 && DECL_VIRTUAL_P (fndecl)
13651 && !processing_template_decl)
13652 {
13653 tree fnclass = DECL_CONTEXT (fndecl);
13654 if (fndecl == CLASSTYPE_KEY_METHOD (fnclass))
13655 keyed_classes = tree_cons (NULL_TREE, fnclass, keyed_classes);
13656 }
13657 }
13658
13659 /* Subroutine of finish_function.
13660 Save the body of constexpr functions for possible
13661 future compile time evaluation. */
13662
13663 static void
13664 maybe_save_function_definition (tree fun)
13665 {
13666 if (!processing_template_decl
13667 && DECL_DECLARED_CONSTEXPR_P (fun)
13668 && !DECL_CLONED_FUNCTION_P (fun))
13669 register_constexpr_fundef (fun, DECL_SAVED_TREE (fun));
13670 }
13671
13672 /* Finish up a function declaration and compile that function
13673 all the way to assembler language output. The free the storage
13674 for the function definition.
13675
13676 FLAGS is a bitwise or of the following values:
13677 2 - INCLASS_INLINE
13678 We just finished processing the body of an in-class inline
13679 function definition. (This processing will have taken place
13680 after the class definition is complete.) */
13681
13682 tree
13683 finish_function (int flags)
13684 {
13685 tree fndecl = current_function_decl;
13686 tree fntype, ctype = NULL_TREE;
13687 int inclass_inline = (flags & 2) != 0;
13688
13689 /* When we get some parse errors, we can end up without a
13690 current_function_decl, so cope. */
13691 if (fndecl == NULL_TREE)
13692 return error_mark_node;
13693
13694 if (c_dialect_objc ())
13695 objc_finish_function ();
13696
13697 gcc_assert (!defer_mark_used_calls);
13698 defer_mark_used_calls = true;
13699
13700 record_key_method_defined (fndecl);
13701
13702 fntype = TREE_TYPE (fndecl);
13703
13704 /* TREE_READONLY (fndecl) = 1;
13705 This caused &foo to be of type ptr-to-const-function
13706 which then got a warning when stored in a ptr-to-function variable. */
13707
13708 gcc_assert (building_stmt_list_p ());
13709 /* The current function is being defined, so its DECL_INITIAL should
13710 be set, and unless there's a multiple definition, it should be
13711 error_mark_node. */
13712 gcc_assert (DECL_INITIAL (fndecl) == error_mark_node);
13713
13714 /* For a cloned function, we've already got all the code we need;
13715 there's no need to add any extra bits. */
13716 if (!DECL_CLONED_FUNCTION_P (fndecl))
13717 {
13718 /* Make it so that `main' always returns 0 by default. */
13719 if (DECL_MAIN_P (current_function_decl))
13720 finish_return_stmt (integer_zero_node);
13721
13722 if (use_eh_spec_block (current_function_decl))
13723 finish_eh_spec_block (TYPE_RAISES_EXCEPTIONS
13724 (TREE_TYPE (current_function_decl)),
13725 current_eh_spec_block);
13726 }
13727
13728 /* If we're saving up tree structure, tie off the function now. */
13729 DECL_SAVED_TREE (fndecl) = pop_stmt_list (DECL_SAVED_TREE (fndecl));
13730
13731 finish_fname_decls ();
13732
13733 /* If this function can't throw any exceptions, remember that. */
13734 if (!processing_template_decl
13735 && !cp_function_chain->can_throw
13736 && !flag_non_call_exceptions
13737 && !decl_replaceable_p (fndecl))
13738 TREE_NOTHROW (fndecl) = 1;
13739
13740 /* This must come after expand_function_end because cleanups might
13741 have declarations (from inline functions) that need to go into
13742 this function's blocks. */
13743
13744 /* If the current binding level isn't the outermost binding level
13745 for this function, either there is a bug, or we have experienced
13746 syntax errors and the statement tree is malformed. */
13747 if (current_binding_level->kind != sk_function_parms)
13748 {
13749 /* Make sure we have already experienced errors. */
13750 gcc_assert (errorcount);
13751
13752 /* Throw away the broken statement tree and extra binding
13753 levels. */
13754 DECL_SAVED_TREE (fndecl) = alloc_stmt_list ();
13755
13756 while (current_binding_level->kind != sk_function_parms)
13757 {
13758 if (current_binding_level->kind == sk_class)
13759 pop_nested_class ();
13760 else
13761 poplevel (0, 0, 0);
13762 }
13763 }
13764 poplevel (1, 0, 1);
13765
13766 /* Statements should always be full-expressions at the outermost set
13767 of curly braces for a function. */
13768 gcc_assert (stmts_are_full_exprs_p ());
13769
13770 /* If there are no return statements in a function with auto return type,
13771 the return type is void. But if the declared type is something like
13772 auto*, this is an error. */
13773 if (!processing_template_decl && FNDECL_USED_AUTO (fndecl)
13774 && TREE_TYPE (fntype) == current_function_auto_return_pattern)
13775 {
13776 if (!is_auto (current_function_auto_return_pattern)
13777 && !current_function_returns_value && !current_function_returns_null)
13778 {
13779 error ("no return statements in function returning %qT",
13780 current_function_auto_return_pattern);
13781 inform (input_location, "only plain %<auto%> return type can be "
13782 "deduced to %<void%>");
13783 }
13784 apply_deduced_return_type (fndecl, void_type_node);
13785 fntype = TREE_TYPE (fndecl);
13786 }
13787
13788 /* Save constexpr function body before it gets munged by
13789 the NRV transformation. */
13790 maybe_save_function_definition (fndecl);
13791
13792 /* Set up the named return value optimization, if we can. Candidate
13793 variables are selected in check_return_expr. */
13794 if (current_function_return_value)
13795 {
13796 tree r = current_function_return_value;
13797 tree outer;
13798
13799 if (r != error_mark_node
13800 /* This is only worth doing for fns that return in memory--and
13801 simpler, since we don't have to worry about promoted modes. */
13802 && aggregate_value_p (TREE_TYPE (TREE_TYPE (fndecl)), fndecl)
13803 /* Only allow this for variables declared in the outer scope of
13804 the function so we know that their lifetime always ends with a
13805 return; see g++.dg/opt/nrv6.C. We could be more flexible if
13806 we were to do this optimization in tree-ssa. */
13807 && (outer = outer_curly_brace_block (fndecl))
13808 && chain_member (r, BLOCK_VARS (outer)))
13809 finalize_nrv (&DECL_SAVED_TREE (fndecl), r, DECL_RESULT (fndecl));
13810
13811 current_function_return_value = NULL_TREE;
13812 }
13813
13814 /* Remember that we were in class scope. */
13815 if (current_class_name)
13816 ctype = current_class_type;
13817
13818 /* Must mark the RESULT_DECL as being in this function. */
13819 DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
13820
13821 /* Set the BLOCK_SUPERCONTEXT of the outermost function scope to point
13822 to the FUNCTION_DECL node itself. */
13823 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
13824
13825 /* Save away current state, if appropriate. */
13826 if (!processing_template_decl)
13827 save_function_data (fndecl);
13828
13829 /* Complain if there's just no return statement. */
13830 if (warn_return_type
13831 && TREE_CODE (TREE_TYPE (fntype)) != VOID_TYPE
13832 && !dependent_type_p (TREE_TYPE (fntype))
13833 && !current_function_returns_value && !current_function_returns_null
13834 /* Don't complain if we abort or throw. */
13835 && !current_function_returns_abnormally
13836 /* Don't complain if we are declared noreturn. */
13837 && !TREE_THIS_VOLATILE (fndecl)
13838 && !DECL_NAME (DECL_RESULT (fndecl))
13839 && !TREE_NO_WARNING (fndecl)
13840 /* Structor return values (if any) are set by the compiler. */
13841 && !DECL_CONSTRUCTOR_P (fndecl)
13842 && !DECL_DESTRUCTOR_P (fndecl)
13843 && targetm.warn_func_return (fndecl))
13844 {
13845 warning (OPT_Wreturn_type,
13846 "no return statement in function returning non-void");
13847 TREE_NO_WARNING (fndecl) = 1;
13848 }
13849
13850 /* Store the end of the function, so that we get good line number
13851 info for the epilogue. */
13852 cfun->function_end_locus = input_location;
13853
13854 /* Complain about parameters that are only set, but never otherwise used. */
13855 if (warn_unused_but_set_parameter
13856 && !processing_template_decl
13857 && errorcount == unused_but_set_errorcount
13858 && !DECL_CLONED_FUNCTION_P (fndecl))
13859 {
13860 tree decl;
13861
13862 for (decl = DECL_ARGUMENTS (fndecl);
13863 decl;
13864 decl = DECL_CHAIN (decl))
13865 if (TREE_USED (decl)
13866 && TREE_CODE (decl) == PARM_DECL
13867 && !DECL_READ_P (decl)
13868 && DECL_NAME (decl)
13869 && !DECL_ARTIFICIAL (decl)
13870 && !TREE_NO_WARNING (decl)
13871 && !DECL_IN_SYSTEM_HEADER (decl)
13872 && TREE_TYPE (decl) != error_mark_node
13873 && TREE_CODE (TREE_TYPE (decl)) != REFERENCE_TYPE
13874 && (!CLASS_TYPE_P (TREE_TYPE (decl))
13875 || !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (decl))))
13876 warning (OPT_Wunused_but_set_parameter,
13877 "parameter %q+D set but not used", decl);
13878 unused_but_set_errorcount = errorcount;
13879 }
13880
13881 /* Complain about locally defined typedefs that are not used in this
13882 function. */
13883 maybe_warn_unused_local_typedefs ();
13884
13885 /* Genericize before inlining. */
13886 if (!processing_template_decl)
13887 {
13888 struct language_function *f = DECL_SAVED_FUNCTION_DATA (fndecl);
13889 invoke_plugin_callbacks (PLUGIN_PRE_GENERICIZE, fndecl);
13890 cp_genericize (fndecl);
13891 /* Clear out the bits we don't need. */
13892 f->x_current_class_ptr = NULL;
13893 f->x_current_class_ref = NULL;
13894 f->x_eh_spec_block = NULL;
13895 f->x_in_charge_parm = NULL;
13896 f->x_vtt_parm = NULL;
13897 f->x_return_value = NULL;
13898 f->bindings = NULL;
13899 f->extern_decl_map = NULL;
13900 }
13901 /* Clear out the bits we don't need. */
13902 local_names = NULL;
13903
13904 /* We're leaving the context of this function, so zap cfun. It's still in
13905 DECL_STRUCT_FUNCTION, and we'll restore it in tree_rest_of_compilation. */
13906 set_cfun (NULL);
13907 current_function_decl = NULL;
13908
13909 /* If this is an in-class inline definition, we may have to pop the
13910 bindings for the template parameters that we added in
13911 maybe_begin_member_template_processing when start_function was
13912 called. */
13913 if (inclass_inline)
13914 maybe_end_member_template_processing ();
13915
13916 /* Leave the scope of the class. */
13917 if (ctype)
13918 pop_nested_class ();
13919
13920 --function_depth;
13921
13922 /* Clean up. */
13923 current_function_decl = NULL_TREE;
13924
13925 defer_mark_used_calls = false;
13926 if (deferred_mark_used_calls)
13927 {
13928 unsigned int i;
13929 tree decl;
13930
13931 FOR_EACH_VEC_SAFE_ELT (deferred_mark_used_calls, i, decl)
13932 mark_used (decl);
13933 vec_free (deferred_mark_used_calls);
13934 }
13935
13936 return fndecl;
13937 }
13938 \f
13939 /* Create the FUNCTION_DECL for a function definition.
13940 DECLSPECS and DECLARATOR are the parts of the declaration;
13941 they describe the return type and the name of the function,
13942 but twisted together in a fashion that parallels the syntax of C.
13943
13944 This function creates a binding context for the function body
13945 as well as setting up the FUNCTION_DECL in current_function_decl.
13946
13947 Returns a FUNCTION_DECL on success.
13948
13949 If the DECLARATOR is not suitable for a function (it defines a datum
13950 instead), we return 0, which tells yyparse to report a parse error.
13951
13952 May return void_type_node indicating that this method is actually
13953 a friend. See grokfield for more details.
13954
13955 Came here with a `.pushlevel' .
13956
13957 DO NOT MAKE ANY CHANGES TO THIS CODE WITHOUT MAKING CORRESPONDING
13958 CHANGES TO CODE IN `grokfield'. */
13959
13960 tree
13961 grokmethod (cp_decl_specifier_seq *declspecs,
13962 const cp_declarator *declarator, tree attrlist)
13963 {
13964 tree fndecl = grokdeclarator (declarator, declspecs, MEMFUNCDEF, 0,
13965 &attrlist);
13966
13967 if (fndecl == error_mark_node)
13968 return error_mark_node;
13969
13970 if (fndecl == NULL || TREE_CODE (fndecl) != FUNCTION_DECL)
13971 {
13972 error ("invalid member function declaration");
13973 return error_mark_node;
13974 }
13975
13976 if (attrlist)
13977 cplus_decl_attributes (&fndecl, attrlist, 0);
13978
13979 /* Pass friends other than inline friend functions back. */
13980 if (fndecl == void_type_node)
13981 return fndecl;
13982
13983 if (DECL_IN_AGGR_P (fndecl))
13984 {
13985 if (DECL_CLASS_SCOPE_P (fndecl))
13986 error ("%qD is already defined in class %qT", fndecl,
13987 DECL_CONTEXT (fndecl));
13988 return error_mark_node;
13989 }
13990
13991 check_template_shadow (fndecl);
13992
13993 DECL_DECLARED_INLINE_P (fndecl) = 1;
13994 DECL_NO_INLINE_WARNING_P (fndecl) = 1;
13995
13996 /* We process method specializations in finish_struct_1. */
13997 if (processing_template_decl && !DECL_TEMPLATE_SPECIALIZATION (fndecl))
13998 {
13999 fndecl = push_template_decl (fndecl);
14000 if (fndecl == error_mark_node)
14001 return fndecl;
14002 }
14003
14004 if (! DECL_FRIEND_P (fndecl))
14005 {
14006 if (DECL_CHAIN (fndecl))
14007 {
14008 fndecl = copy_node (fndecl);
14009 TREE_CHAIN (fndecl) = NULL_TREE;
14010 }
14011 }
14012
14013 cp_finish_decl (fndecl, NULL_TREE, false, NULL_TREE, 0);
14014
14015 DECL_IN_AGGR_P (fndecl) = 1;
14016 return fndecl;
14017 }
14018 \f
14019
14020 /* VAR is a VAR_DECL. If its type is incomplete, remember VAR so that
14021 we can lay it out later, when and if its type becomes complete. */
14022
14023 void
14024 maybe_register_incomplete_var (tree var)
14025 {
14026 gcc_assert (TREE_CODE (var) == VAR_DECL);
14027
14028 /* Keep track of variables with incomplete types. */
14029 if (!processing_template_decl && TREE_TYPE (var) != error_mark_node
14030 && DECL_EXTERNAL (var))
14031 {
14032 tree inner_type = TREE_TYPE (var);
14033
14034 while (TREE_CODE (inner_type) == ARRAY_TYPE)
14035 inner_type = TREE_TYPE (inner_type);
14036 inner_type = TYPE_MAIN_VARIANT (inner_type);
14037
14038 if ((!COMPLETE_TYPE_P (inner_type) && CLASS_TYPE_P (inner_type))
14039 /* RTTI TD entries are created while defining the type_info. */
14040 || (TYPE_LANG_SPECIFIC (inner_type)
14041 && TYPE_BEING_DEFINED (inner_type)))
14042 {
14043 incomplete_var iv = {var, inner_type};
14044 vec_safe_push (incomplete_vars, iv);
14045 }
14046 }
14047 }
14048
14049 /* Called when a class type (given by TYPE) is defined. If there are
14050 any existing VAR_DECLs whose type has been completed by this
14051 declaration, update them now. */
14052
14053 void
14054 complete_vars (tree type)
14055 {
14056 unsigned ix;
14057 incomplete_var *iv;
14058
14059 for (ix = 0; vec_safe_iterate (incomplete_vars, ix, &iv); )
14060 {
14061 if (same_type_p (type, iv->incomplete_type))
14062 {
14063 tree var = iv->decl;
14064 tree type = TREE_TYPE (var);
14065 /* Complete the type of the variable. The VAR_DECL itself
14066 will be laid out in expand_expr. */
14067 complete_type (type);
14068 cp_apply_type_quals_to_decl (cp_type_quals (type), var);
14069 /* Remove this entry from the list. */
14070 incomplete_vars->unordered_remove (ix);
14071 }
14072 else
14073 ix++;
14074 }
14075
14076 /* Check for pending declarations which may have abstract type. */
14077 complete_type_check_abstract (type);
14078 }
14079
14080 /* If DECL is of a type which needs a cleanup, build and return an
14081 expression to perform that cleanup here. Return NULL_TREE if no
14082 cleanup need be done. */
14083
14084 tree
14085 cxx_maybe_build_cleanup (tree decl, tsubst_flags_t complain)
14086 {
14087 tree type;
14088 tree attr;
14089 tree cleanup;
14090
14091 /* Assume no cleanup is required. */
14092 cleanup = NULL_TREE;
14093
14094 if (error_operand_p (decl))
14095 return cleanup;
14096
14097 /* Handle "__attribute__((cleanup))". We run the cleanup function
14098 before the destructor since the destructor is what actually
14099 terminates the lifetime of the object. */
14100 attr = lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl));
14101 if (attr)
14102 {
14103 tree id;
14104 tree fn;
14105 tree arg;
14106
14107 /* Get the name specified by the user for the cleanup function. */
14108 id = TREE_VALUE (TREE_VALUE (attr));
14109 /* Look up the name to find the cleanup function to call. It is
14110 important to use lookup_name here because that is what is
14111 used in c-common.c:handle_cleanup_attribute when performing
14112 initial checks on the attribute. Note that those checks
14113 include ensuring that the function found is not an overloaded
14114 function, or an object with an overloaded call operator,
14115 etc.; we can rely on the fact that the function found is an
14116 ordinary FUNCTION_DECL. */
14117 fn = lookup_name (id);
14118 arg = build_address (decl);
14119 mark_used (decl);
14120 cleanup = cp_build_function_call_nary (fn, complain, arg, NULL_TREE);
14121 if (cleanup == error_mark_node)
14122 return error_mark_node;
14123 }
14124 /* Handle ordinary C++ destructors. */
14125 type = TREE_TYPE (decl);
14126 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
14127 {
14128 int flags = LOOKUP_NORMAL|LOOKUP_DESTRUCTOR;
14129 bool has_vbases = (TREE_CODE (type) == RECORD_TYPE
14130 && CLASSTYPE_VBASECLASSES (type));
14131 tree addr;
14132 tree call;
14133
14134 if (TREE_CODE (type) == ARRAY_TYPE)
14135 addr = decl;
14136 else
14137 addr = build_address (decl);
14138
14139 /* Optimize for space over speed here. */
14140 if (!has_vbases || flag_expensive_optimizations)
14141 flags |= LOOKUP_NONVIRTUAL;
14142
14143 call = build_delete (TREE_TYPE (addr), addr,
14144 sfk_complete_destructor, flags, 0, complain);
14145 if (call == error_mark_node)
14146 cleanup = error_mark_node;
14147 else if (cleanup)
14148 cleanup = cp_build_compound_expr (cleanup, call, complain);
14149 else
14150 cleanup = call;
14151 }
14152
14153 /* build_delete sets the location of the destructor call to the
14154 current location, even though the destructor is going to be
14155 called later, at the end of the current scope. This can lead to
14156 a "jumpy" behaviour for users of debuggers when they step around
14157 the end of the block. So let's unset the location of the
14158 destructor call instead. */
14159 if (cleanup != NULL && EXPR_P (cleanup))
14160 SET_EXPR_LOCATION (cleanup, UNKNOWN_LOCATION);
14161 return cleanup;
14162 }
14163
14164 \f
14165 /* When a stmt has been parsed, this function is called. */
14166
14167 void
14168 finish_stmt (void)
14169 {
14170 }
14171
14172 /* Return the FUNCTION_TYPE that corresponds to MEMFNTYPE, which can be a
14173 FUNCTION_DECL, METHOD_TYPE, FUNCTION_TYPE, pointer or reference to
14174 METHOD_TYPE or FUNCTION_TYPE, or pointer to member function. */
14175
14176 tree
14177 static_fn_type (tree memfntype)
14178 {
14179 tree fntype;
14180 tree args;
14181
14182 if (TYPE_PTRMEMFUNC_P (memfntype))
14183 memfntype = TYPE_PTRMEMFUNC_FN_TYPE (memfntype);
14184 if (POINTER_TYPE_P (memfntype)
14185 || TREE_CODE (memfntype) == FUNCTION_DECL)
14186 memfntype = TREE_TYPE (memfntype);
14187 if (TREE_CODE (memfntype) == FUNCTION_TYPE)
14188 return memfntype;
14189 gcc_assert (TREE_CODE (memfntype) == METHOD_TYPE);
14190 args = TYPE_ARG_TYPES (memfntype);
14191 fntype = build_function_type (TREE_TYPE (memfntype), TREE_CHAIN (args));
14192 fntype = apply_memfn_quals (fntype, type_memfn_quals (memfntype));
14193 fntype = (cp_build_type_attribute_variant
14194 (fntype, TYPE_ATTRIBUTES (memfntype)));
14195 fntype = (build_exception_variant
14196 (fntype, TYPE_RAISES_EXCEPTIONS (memfntype)));
14197 return fntype;
14198 }
14199
14200 /* DECL was originally constructed as a non-static member function,
14201 but turned out to be static. Update it accordingly. */
14202
14203 void
14204 revert_static_member_fn (tree decl)
14205 {
14206 tree stype = static_fn_type (decl);
14207 cp_cv_quals quals = type_memfn_quals (stype);
14208
14209 if (quals != TYPE_UNQUALIFIED)
14210 stype = apply_memfn_quals (stype, TYPE_UNQUALIFIED);
14211
14212 TREE_TYPE (decl) = stype;
14213
14214 if (DECL_ARGUMENTS (decl))
14215 DECL_ARGUMENTS (decl) = DECL_CHAIN (DECL_ARGUMENTS (decl));
14216 DECL_STATIC_FUNCTION_P (decl) = 1;
14217 }
14218
14219 /* Return which tree structure is used by T, or TS_CP_GENERIC if T is
14220 one of the language-independent trees. */
14221
14222 enum cp_tree_node_structure_enum
14223 cp_tree_node_structure (union lang_tree_node * t)
14224 {
14225 switch (TREE_CODE (&t->generic))
14226 {
14227 case DEFAULT_ARG: return TS_CP_DEFAULT_ARG;
14228 case DEFERRED_NOEXCEPT: return TS_CP_DEFERRED_NOEXCEPT;
14229 case IDENTIFIER_NODE: return TS_CP_IDENTIFIER;
14230 case OVERLOAD: return TS_CP_OVERLOAD;
14231 case TEMPLATE_PARM_INDEX: return TS_CP_TPI;
14232 case PTRMEM_CST: return TS_CP_PTRMEM;
14233 case BASELINK: return TS_CP_BASELINK;
14234 case STATIC_ASSERT: return TS_CP_STATIC_ASSERT;
14235 case ARGUMENT_PACK_SELECT: return TS_CP_ARGUMENT_PACK_SELECT;
14236 case TRAIT_EXPR: return TS_CP_TRAIT_EXPR;
14237 case LAMBDA_EXPR: return TS_CP_LAMBDA_EXPR;
14238 case TEMPLATE_INFO: return TS_CP_TEMPLATE_INFO;
14239 case USERDEF_LITERAL: return TS_CP_USERDEF_LITERAL;
14240 default: return TS_CP_GENERIC;
14241 }
14242 }
14243
14244 /* Build the void_list_node (void_type_node having been created). */
14245 tree
14246 build_void_list_node (void)
14247 {
14248 tree t = build_tree_list (NULL_TREE, void_type_node);
14249 return t;
14250 }
14251
14252 bool
14253 cp_missing_noreturn_ok_p (tree decl)
14254 {
14255 /* A missing noreturn is ok for the `main' function. */
14256 return DECL_MAIN_P (decl);
14257 }
14258
14259 /* Return the COMDAT group into which DECL should be placed. */
14260
14261 tree
14262 cxx_comdat_group (tree decl)
14263 {
14264 tree name;
14265
14266 /* Virtual tables, construction virtual tables, and virtual table
14267 tables all go in a single COMDAT group, named after the primary
14268 virtual table. */
14269 if (TREE_CODE (decl) == VAR_DECL && DECL_VTABLE_OR_VTT_P (decl))
14270 name = DECL_ASSEMBLER_NAME (CLASSTYPE_VTABLES (DECL_CONTEXT (decl)));
14271 /* For all other DECLs, the COMDAT group is the mangled name of the
14272 declaration itself. */
14273 else
14274 {
14275 while (DECL_THUNK_P (decl))
14276 {
14277 /* If TARGET_USE_LOCAL_THUNK_ALIAS_P, use_thunk puts the thunk
14278 into the same section as the target function. In that case
14279 we must return target's name. */
14280 tree target = THUNK_TARGET (decl);
14281 if (TARGET_USE_LOCAL_THUNK_ALIAS_P (target)
14282 && DECL_SECTION_NAME (target) != NULL
14283 && DECL_ONE_ONLY (target))
14284 decl = target;
14285 else
14286 break;
14287 }
14288 name = DECL_ASSEMBLER_NAME (decl);
14289 }
14290
14291 return name;
14292 }
14293
14294 #include "gt-cp-decl.h"