Revert "[testsuite/guality] Run guality tests with Og"
[official-gcc.git] / gcc / cp / decl.c
blob3c1e2ef36984129e2fa2d561908bbe3113f7d6e5
1 /* Process declarations and variables for C++ compiler.
2 Copyright (C) 1988-2018 Free Software Foundation, Inc.
3 Contributed by Michael Tiemann (tiemann@cygnus.com)
5 This file is part of GCC.
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.
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.
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/>. */
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. */
26 /* ??? not all decl nodes are given the most useful possible
27 line numbers. For example, the CONST_DECLs for enum values. */
29 #include "config.h"
30 #include "system.h"
31 #include "coretypes.h"
32 #include "target.h"
33 #include "c-family/c-target.h"
34 #include "cp-tree.h"
35 #include "timevar.h"
36 #include "stringpool.h"
37 #include "cgraph.h"
38 #include "stor-layout.h"
39 #include "varasm.h"
40 #include "attribs.h"
41 #include "flags.h"
42 #include "tree-iterator.h"
43 #include "decl.h"
44 #include "intl.h"
45 #include "toplev.h"
46 #include "c-family/c-objc.h"
47 #include "c-family/c-pragma.h"
48 #include "c-family/c-ubsan.h"
49 #include "debug.h"
50 #include "plugin.h"
51 #include "builtins.h"
52 #include "gimplify.h"
53 #include "asan.h"
54 #include "gcc-rich-location.h"
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 */
64 static const char *redeclaration_error_message (tree, tree);
66 static int decl_jump_unsafe (tree);
67 static void require_complete_types_for_parms (tree);
68 static void push_local_name (tree);
69 static tree grok_reference_init (tree, tree, tree, int);
70 static tree grokvardecl (tree, tree, tree, const cp_decl_specifier_seq *,
71 int, int, int, bool, int, tree);
72 static int check_static_variable_definition (tree, tree);
73 static void record_unknown_type (tree, const char *);
74 static tree builtin_function_1 (tree, tree, bool);
75 static int member_function_or_else (tree, tree, enum overload_flags);
76 static tree local_variable_p_walkfn (tree *, int *, void *);
77 static const char *tag_name (enum tag_types);
78 static tree lookup_and_check_tag (enum tag_types, tree, tag_scope, bool);
79 static void maybe_deduce_size_from_array_init (tree, tree);
80 static void layout_var_decl (tree);
81 static tree check_initializer (tree, tree, int, vec<tree, va_gc> **);
82 static void make_rtl_for_nonlocal_decl (tree, tree, const char *);
83 static void save_function_data (tree);
84 static void copy_type_enum (tree , tree);
85 static void check_function_type (tree, tree);
86 static void finish_constructor_body (void);
87 static void begin_destructor_body (void);
88 static void finish_destructor_body (void);
89 static void record_key_method_defined (tree);
90 static tree create_array_type_for_decl (tree, tree, tree);
91 static tree get_atexit_node (void);
92 static tree get_dso_handle_node (void);
93 static tree start_cleanup_fn (void);
94 static void end_cleanup_fn (void);
95 static tree cp_make_fname_decl (location_t, tree, int);
96 static void initialize_predefined_identifiers (void);
97 static tree check_special_function_return_type
98 (special_function_kind, tree, tree, int, const location_t*);
99 static tree push_cp_library_fn (enum tree_code, tree, int);
100 static tree build_cp_library_fn (tree, enum tree_code, tree, int);
101 static void store_parm_decls (tree);
102 static void initialize_local_var (tree, tree);
103 static void expand_static_init (tree, tree);
105 /* The following symbols are subsumed in the cp_global_trees array, and
106 listed here individually for documentation purposes.
108 C++ extensions
109 tree wchar_decl_node;
111 tree vtable_entry_type;
112 tree delta_type_node;
113 tree __t_desc_type_node;
115 tree class_type_node;
116 tree unknown_type_node;
118 Array type `vtable_entry_type[]'
120 tree vtbl_type_node;
121 tree vtbl_ptr_type_node;
123 Namespaces,
125 tree std_node;
126 tree abi_node;
128 A FUNCTION_DECL which can call `abort'. Not necessarily the
129 one that the user will declare, but sufficient to be called
130 by routines that want to abort the program.
132 tree abort_fndecl;
134 Used by RTTI
135 tree type_info_type_node, tinfo_decl_id, tinfo_decl_type;
136 tree tinfo_var_id; */
138 tree cp_global_trees[CPTI_MAX];
140 #define local_names cp_function_chain->x_local_names
142 /* A list of objects which have constructors or destructors
143 which reside in the global scope. The decl is stored in
144 the TREE_VALUE slot and the initializer is stored
145 in the TREE_PURPOSE slot. */
146 tree static_aggregates;
148 /* Like static_aggregates, but for thread_local variables. */
149 tree tls_aggregates;
151 /* -- end of C++ */
153 /* A node for the integer constant 2. */
155 tree integer_two_node;
157 /* vector of static decls. */
158 vec<tree, va_gc> *static_decls;
160 /* vector of keyed classes. */
161 vec<tree, va_gc> *keyed_classes;
163 /* Used only for jumps to as-yet undefined labels, since jumps to
164 defined labels can have their validity checked immediately. */
166 struct GTY((chain_next ("%h.next"))) named_label_use_entry {
167 struct named_label_use_entry *next;
168 /* The binding level to which this entry is *currently* attached.
169 This is initially the binding level in which the goto appeared,
170 but is modified as scopes are closed. */
171 cp_binding_level *binding_level;
172 /* The head of the names list that was current when the goto appeared,
173 or the inner scope popped. These are the decls that will *not* be
174 skipped when jumping to the label. */
175 tree names_in_scope;
176 /* The location of the goto, for error reporting. */
177 location_t o_goto_locus;
178 /* True if an OpenMP structured block scope has been closed since
179 the goto appeared. This means that the branch from the label will
180 illegally exit an OpenMP scope. */
181 bool in_omp_scope;
184 /* A list of all LABEL_DECLs in the function that have names. Here so
185 we can clear out their names' definitions at the end of the
186 function, and so we can check the validity of jumps to these labels. */
188 struct GTY((for_user)) named_label_entry {
190 tree name; /* Name of decl. */
192 tree label_decl; /* LABEL_DECL, unless deleted local label. */
194 named_label_entry *outer; /* Outer shadowed chain. */
196 /* The binding level to which the label is *currently* attached.
197 This is initially set to the binding level in which the label
198 is defined, but is modified as scopes are closed. */
199 cp_binding_level *binding_level;
201 /* The head of the names list that was current when the label was
202 defined, or the inner scope popped. These are the decls that will
203 be skipped when jumping to the label. */
204 tree names_in_scope;
206 /* A vector of all decls from all binding levels that would be
207 crossed by a backward branch to the label. */
208 vec<tree, va_gc> *bad_decls;
210 /* A list of uses of the label, before the label is defined. */
211 named_label_use_entry *uses;
213 /* The following bits are set after the label is defined, and are
214 updated as scopes are popped. They indicate that a jump to the
215 label will illegally enter a scope of the given flavor. */
216 bool in_try_scope;
217 bool in_catch_scope;
218 bool in_omp_scope;
219 bool in_transaction_scope;
220 bool in_constexpr_if;
223 #define named_labels cp_function_chain->x_named_labels
225 /* The number of function bodies which we are currently processing.
226 (Zero if we are at namespace scope, one inside the body of a
227 function, two inside the body of a function in a local class, etc.) */
228 int function_depth;
230 /* Whether the exception-specifier is part of a function type (i.e. C++17). */
231 bool flag_noexcept_type;
233 /* States indicating how grokdeclarator() should handle declspecs marked
234 with __attribute__((deprecated)). An object declared as
235 __attribute__((deprecated)) suppresses warnings of uses of other
236 deprecated items. */
237 enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
240 /* A list of VAR_DECLs whose type was incomplete at the time the
241 variable was declared. */
243 struct GTY(()) incomplete_var {
244 tree decl;
245 tree incomplete_type;
249 static GTY(()) vec<incomplete_var, va_gc> *incomplete_vars;
251 /* Returns the kind of template specialization we are currently
252 processing, given that it's declaration contained N_CLASS_SCOPES
253 explicit scope qualifications. */
255 tmpl_spec_kind
256 current_tmpl_spec_kind (int n_class_scopes)
258 int n_template_parm_scopes = 0;
259 int seen_specialization_p = 0;
260 int innermost_specialization_p = 0;
261 cp_binding_level *b;
263 /* Scan through the template parameter scopes. */
264 for (b = current_binding_level;
265 b->kind == sk_template_parms;
266 b = b->level_chain)
268 /* If we see a specialization scope inside a parameter scope,
269 then something is wrong. That corresponds to a declaration
270 like:
272 template <class T> template <> ...
274 which is always invalid since [temp.expl.spec] forbids the
275 specialization of a class member template if the enclosing
276 class templates are not explicitly specialized as well. */
277 if (b->explicit_spec_p)
279 if (n_template_parm_scopes == 0)
280 innermost_specialization_p = 1;
281 else
282 seen_specialization_p = 1;
284 else if (seen_specialization_p == 1)
285 return tsk_invalid_member_spec;
287 ++n_template_parm_scopes;
290 /* Handle explicit instantiations. */
291 if (processing_explicit_instantiation)
293 if (n_template_parm_scopes != 0)
294 /* We've seen a template parameter list during an explicit
295 instantiation. For example:
297 template <class T> template void f(int);
299 This is erroneous. */
300 return tsk_invalid_expl_inst;
301 else
302 return tsk_expl_inst;
305 if (n_template_parm_scopes < n_class_scopes)
306 /* We've not seen enough template headers to match all the
307 specialized classes present. For example:
309 template <class T> void R<T>::S<T>::f(int);
311 This is invalid; there needs to be one set of template
312 parameters for each class. */
313 return tsk_insufficient_parms;
314 else if (n_template_parm_scopes == n_class_scopes)
315 /* We're processing a non-template declaration (even though it may
316 be a member of a template class.) For example:
318 template <class T> void S<T>::f(int);
320 The `class T' matches the `S<T>', leaving no template headers
321 corresponding to the `f'. */
322 return tsk_none;
323 else if (n_template_parm_scopes > n_class_scopes + 1)
324 /* We've got too many template headers. For example:
326 template <> template <class T> void f (T);
328 There need to be more enclosing classes. */
329 return tsk_excessive_parms;
330 else
331 /* This must be a template. It's of the form:
333 template <class T> template <class U> void S<T>::f(U);
335 This is a specialization if the innermost level was a
336 specialization; otherwise it's just a definition of the
337 template. */
338 return innermost_specialization_p ? tsk_expl_spec : tsk_template;
341 /* Exit the current scope. */
343 void
344 finish_scope (void)
346 poplevel (0, 0, 0);
349 /* When a label goes out of scope, check to see if that label was used
350 in a valid manner, and issue any appropriate warnings or errors. */
352 static void
353 check_label_used (tree label)
355 if (!processing_template_decl)
357 if (DECL_INITIAL (label) == NULL_TREE)
359 location_t location;
361 error ("label %q+D used but not defined", label);
362 location = input_location;
363 /* FIXME want (LOCATION_FILE (input_location), (line)0) */
364 /* Avoid crashing later. */
365 define_label (location, DECL_NAME (label));
367 else
368 warn_for_unused_label (label);
372 /* Helper function to sort named label entries in a vector by DECL_UID. */
374 static int
375 sort_labels (const void *a, const void *b)
377 tree label1 = *(tree const *) a;
378 tree label2 = *(tree const *) b;
380 /* DECL_UIDs can never be equal. */
381 return DECL_UID (label1) > DECL_UID (label2) ? -1 : +1;
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. */
388 static void
389 pop_labels (tree block)
391 if (!named_labels)
392 return;
394 /* We need to add the labels to the block chain, so debug
395 information is emitted. But, we want the order to be stable so
396 need to sort them first. Otherwise the debug output could be
397 randomly ordered. I guess it's mostly stable, unless the hash
398 table implementation changes. */
399 auto_vec<tree, 32> labels (named_labels->elements ());
400 hash_table<named_label_hash>::iterator end (named_labels->end ());
401 for (hash_table<named_label_hash>::iterator iter
402 (named_labels->begin ()); iter != end; ++iter)
404 named_label_entry *ent = *iter;
406 gcc_checking_assert (!ent->outer);
407 if (ent->label_decl)
408 labels.quick_push (ent->label_decl);
409 ggc_free (ent);
411 named_labels = NULL;
412 labels.qsort (sort_labels);
414 while (labels.length ())
416 tree label = labels.pop ();
418 DECL_CHAIN (label) = BLOCK_VARS (block);
419 BLOCK_VARS (block) = label;
421 check_label_used (label);
425 /* At the end of a block with local labels, restore the outer definition. */
427 static void
428 pop_local_label (tree id, tree label)
430 check_label_used (label);
431 named_label_entry **slot = named_labels->find_slot_with_hash
432 (id, IDENTIFIER_HASH_VALUE (id), NO_INSERT);
433 named_label_entry *ent = *slot;
435 if (ent->outer)
436 ent = ent->outer;
437 else
439 ent = ggc_cleared_alloc<named_label_entry> ();
440 ent->name = id;
442 *slot = ent;
445 /* The following two routines are used to interface to Objective-C++.
446 The binding level is purposely treated as an opaque type. */
448 void *
449 objc_get_current_scope (void)
451 return current_binding_level;
454 /* The following routine is used by the NeXT-style SJLJ exceptions;
455 variables get marked 'volatile' so as to not be clobbered by
456 _setjmp()/_longjmp() calls. All variables in the current scope,
457 as well as parent scopes up to (but not including) ENCLOSING_BLK
458 shall be thusly marked. */
460 void
461 objc_mark_locals_volatile (void *enclosing_blk)
463 cp_binding_level *scope;
465 for (scope = current_binding_level;
466 scope && scope != enclosing_blk;
467 scope = scope->level_chain)
469 tree decl;
471 for (decl = scope->names; decl; decl = TREE_CHAIN (decl))
472 objc_volatilize_decl (decl);
474 /* Do not climb up past the current function. */
475 if (scope->kind == sk_function_parms)
476 break;
480 /* True if B is the level for the condition of a constexpr if. */
482 static bool
483 level_for_constexpr_if (cp_binding_level *b)
485 return (b->kind == sk_cond && b->this_entity
486 && TREE_CODE (b->this_entity) == IF_STMT
487 && IF_STMT_CONSTEXPR_P (b->this_entity));
490 /* Update data for defined and undefined labels when leaving a scope. */
493 poplevel_named_label_1 (named_label_entry **slot, cp_binding_level *bl)
495 named_label_entry *ent = *slot;
496 cp_binding_level *obl = bl->level_chain;
498 if (ent->binding_level == bl)
500 tree decl;
502 /* ENT->NAMES_IN_SCOPE may contain a mixture of DECLs and
503 TREE_LISTs representing OVERLOADs, so be careful. */
504 for (decl = ent->names_in_scope; decl; decl = (DECL_P (decl)
505 ? DECL_CHAIN (decl)
506 : TREE_CHAIN (decl)))
507 if (decl_jump_unsafe (decl))
508 vec_safe_push (ent->bad_decls, decl);
510 ent->binding_level = obl;
511 ent->names_in_scope = obl->names;
512 switch (bl->kind)
514 case sk_try:
515 ent->in_try_scope = true;
516 break;
517 case sk_catch:
518 ent->in_catch_scope = true;
519 break;
520 case sk_omp:
521 ent->in_omp_scope = true;
522 break;
523 case sk_transaction:
524 ent->in_transaction_scope = true;
525 break;
526 case sk_block:
527 if (level_for_constexpr_if (bl->level_chain))
528 ent->in_constexpr_if = true;
529 break;
530 default:
531 break;
534 else if (ent->uses)
536 struct named_label_use_entry *use;
538 for (use = ent->uses; use ; use = use->next)
539 if (use->binding_level == bl)
541 use->binding_level = obl;
542 use->names_in_scope = obl->names;
543 if (bl->kind == sk_omp)
544 use->in_omp_scope = true;
548 return 1;
551 /* Saved errorcount to avoid -Wunused-but-set-{parameter,variable} warnings
552 when errors were reported, except for -Werror-unused-but-set-*. */
553 static int unused_but_set_errorcount;
555 /* Exit a binding level.
556 Pop the level off, and restore the state of the identifier-decl mappings
557 that were in effect when this level was entered.
559 If KEEP == 1, this level had explicit declarations, so
560 and create a "block" (a BLOCK node) for the level
561 to record its declarations and subblocks for symbol table output.
563 If FUNCTIONBODY is nonzero, this level is the body of a function,
564 so create a block as if KEEP were set and also clear out all
565 label names.
567 If REVERSE is nonzero, reverse the order of decls before putting
568 them into the BLOCK. */
570 tree
571 poplevel (int keep, int reverse, int functionbody)
573 tree link;
574 /* The chain of decls was accumulated in reverse order.
575 Put it into forward order, just for cleanliness. */
576 tree decls;
577 tree subblocks;
578 tree block;
579 tree decl;
580 scope_kind kind;
582 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
583 restart:
585 block = NULL_TREE;
587 gcc_assert (current_binding_level->kind != sk_class
588 && current_binding_level->kind != sk_namespace);
590 if (current_binding_level->kind == sk_cleanup)
591 functionbody = 0;
592 subblocks = functionbody >= 0 ? current_binding_level->blocks : 0;
594 gcc_assert (!vec_safe_length (current_binding_level->class_shadowed));
596 /* We used to use KEEP == 2 to indicate that the new block should go
597 at the beginning of the list of blocks at this binding level,
598 rather than the end. This hack is no longer used. */
599 gcc_assert (keep == 0 || keep == 1);
601 if (current_binding_level->keep)
602 keep = 1;
604 /* Any uses of undefined labels, and any defined labels, now operate
605 under constraints of next binding contour. */
606 if (cfun && !functionbody && named_labels)
607 named_labels->traverse<cp_binding_level *, poplevel_named_label_1>
608 (current_binding_level);
610 /* Get the decls in the order they were written.
611 Usually current_binding_level->names is in reverse order.
612 But parameter decls were previously put in forward order. */
614 decls = current_binding_level->names;
615 if (reverse)
617 decls = nreverse (decls);
618 current_binding_level->names = decls;
621 /* If there were any declarations or structure tags in that level,
622 or if this level is a function body,
623 create a BLOCK to record them for the life of this function. */
624 block = NULL_TREE;
625 /* Avoid function body block if possible. */
626 if (functionbody && subblocks && BLOCK_CHAIN (subblocks) == NULL_TREE)
627 keep = 0;
628 else if (keep == 1 || functionbody)
629 block = make_node (BLOCK);
630 if (block != NULL_TREE)
632 BLOCK_VARS (block) = decls;
633 BLOCK_SUBBLOCKS (block) = subblocks;
636 /* In each subblock, record that this is its superior. */
637 if (keep >= 0)
638 for (link = subblocks; link; link = BLOCK_CHAIN (link))
639 BLOCK_SUPERCONTEXT (link) = block;
641 /* Before we remove the declarations first check for unused variables. */
642 if ((warn_unused_variable || warn_unused_but_set_variable)
643 && current_binding_level->kind != sk_template_parms
644 && !processing_template_decl)
645 for (tree d = get_local_decls (); d; d = TREE_CHAIN (d))
647 /* There are cases where D itself is a TREE_LIST. See in
648 push_local_binding where the list of decls returned by
649 getdecls is built. */
650 decl = TREE_CODE (d) == TREE_LIST ? TREE_VALUE (d) : d;
652 tree type = TREE_TYPE (decl);
653 if (VAR_P (decl)
654 && (! TREE_USED (decl) || !DECL_READ_P (decl))
655 && ! DECL_IN_SYSTEM_HEADER (decl)
656 /* For structured bindings, consider only real variables, not
657 subobjects. */
658 && (DECL_DECOMPOSITION_P (decl) ? !DECL_DECOMP_BASE (decl)
659 : (DECL_NAME (decl) && !DECL_ARTIFICIAL (decl)))
660 && type != error_mark_node
661 && (!CLASS_TYPE_P (type)
662 || !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
663 || lookup_attribute ("warn_unused",
664 TYPE_ATTRIBUTES (TREE_TYPE (decl)))))
666 if (! TREE_USED (decl))
668 if (!DECL_NAME (decl) && DECL_DECOMPOSITION_P (decl))
669 warning_at (DECL_SOURCE_LOCATION (decl),
670 OPT_Wunused_variable,
671 "unused structured binding declaration");
672 else
673 warning_at (DECL_SOURCE_LOCATION (decl),
674 OPT_Wunused_variable, "unused variable %qD", decl);
676 else if (DECL_CONTEXT (decl) == current_function_decl
677 // For -Wunused-but-set-variable leave references alone.
678 && !TYPE_REF_P (TREE_TYPE (decl))
679 && errorcount == unused_but_set_errorcount)
681 if (!DECL_NAME (decl) && DECL_DECOMPOSITION_P (decl))
682 warning_at (DECL_SOURCE_LOCATION (decl),
683 OPT_Wunused_but_set_variable, "structured "
684 "binding declaration set but not used");
685 else
686 warning_at (DECL_SOURCE_LOCATION (decl),
687 OPT_Wunused_but_set_variable,
688 "variable %qD set but not used", decl);
689 unused_but_set_errorcount = errorcount;
694 /* Remove declarations for all the DECLs in this level. */
695 for (link = decls; link; link = TREE_CHAIN (link))
697 decl = TREE_CODE (link) == TREE_LIST ? TREE_VALUE (link) : link;
698 tree name = OVL_NAME (decl);
700 /* Remove the binding. */
701 if (TREE_CODE (decl) == LABEL_DECL)
702 pop_local_label (name, decl);
703 else
704 pop_local_binding (name, decl);
707 /* Restore the IDENTIFIER_TYPE_VALUEs. */
708 for (link = current_binding_level->type_shadowed;
709 link; link = TREE_CHAIN (link))
710 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (link), TREE_VALUE (link));
712 /* There may be OVERLOADs (wrapped in TREE_LISTs) on the BLOCK_VARs
713 list if a `using' declaration put them there. The debugging
714 back ends won't understand OVERLOAD, so we remove them here.
715 Because the BLOCK_VARS are (temporarily) shared with
716 CURRENT_BINDING_LEVEL->NAMES we must do this fixup after we have
717 popped all the bindings. Also remove undeduced 'auto' decls,
718 which LTO doesn't understand, and can't have been used by anything. */
719 if (block)
721 tree* d;
723 for (d = &BLOCK_VARS (block); *d; )
725 if (TREE_CODE (*d) == TREE_LIST
726 || (!processing_template_decl
727 && undeduced_auto_decl (*d)))
728 *d = TREE_CHAIN (*d);
729 else
730 d = &DECL_CHAIN (*d);
734 /* If the level being exited is the top level of a function,
735 check over all the labels. */
736 if (functionbody)
738 if (block)
740 /* Since this is the top level block of a function, the vars are
741 the function's parameters. Don't leave them in the BLOCK
742 because they are found in the FUNCTION_DECL instead. */
743 BLOCK_VARS (block) = 0;
744 pop_labels (block);
746 else
747 pop_labels (subblocks);
750 kind = current_binding_level->kind;
751 if (kind == sk_cleanup)
753 tree stmt;
755 /* If this is a temporary binding created for a cleanup, then we'll
756 have pushed a statement list level. Pop that, create a new
757 BIND_EXPR for the block, and insert it into the stream. */
758 stmt = pop_stmt_list (current_binding_level->statement_list);
759 stmt = c_build_bind_expr (input_location, block, stmt);
760 add_stmt (stmt);
763 leave_scope ();
764 if (functionbody)
766 /* The current function is being defined, so its DECL_INITIAL
767 should be error_mark_node. */
768 gcc_assert (DECL_INITIAL (current_function_decl) == error_mark_node);
769 DECL_INITIAL (current_function_decl) = block ? block : subblocks;
770 if (subblocks)
772 if (FUNCTION_NEEDS_BODY_BLOCK (current_function_decl))
774 if (BLOCK_SUBBLOCKS (subblocks))
775 BLOCK_OUTER_CURLY_BRACE_P (BLOCK_SUBBLOCKS (subblocks)) = 1;
777 else
778 BLOCK_OUTER_CURLY_BRACE_P (subblocks) = 1;
781 else if (block)
782 current_binding_level->blocks
783 = block_chainon (current_binding_level->blocks, block);
785 /* If we did not make a block for the level just exited,
786 any blocks made for inner levels
787 (since they cannot be recorded as subblocks in that level)
788 must be carried forward so they will later become subblocks
789 of something else. */
790 else if (subblocks)
791 current_binding_level->blocks
792 = block_chainon (current_binding_level->blocks, subblocks);
794 /* Each and every BLOCK node created here in `poplevel' is important
795 (e.g. for proper debugging information) so if we created one
796 earlier, mark it as "used". */
797 if (block)
798 TREE_USED (block) = 1;
800 /* All temporary bindings created for cleanups are popped silently. */
801 if (kind == sk_cleanup)
802 goto restart;
804 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
805 return block;
808 /* Call wrapup_globals_declarations for the globals in NAMESPACE. */
809 /* Diagnose odr-used extern inline variables without definitions
810 in the current TU. */
813 wrapup_namespace_globals ()
815 if (vec<tree, va_gc> *statics = static_decls)
817 tree decl;
818 unsigned int i;
819 FOR_EACH_VEC_ELT (*statics, i, decl)
821 if (warn_unused_function
822 && TREE_CODE (decl) == FUNCTION_DECL
823 && DECL_INITIAL (decl) == 0
824 && DECL_EXTERNAL (decl)
825 && !TREE_PUBLIC (decl)
826 && !DECL_ARTIFICIAL (decl)
827 && !DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl)
828 && !TREE_NO_WARNING (decl))
829 warning_at (DECL_SOURCE_LOCATION (decl),
830 OPT_Wunused_function,
831 "%qF declared %<static%> but never defined", decl);
833 if (VAR_P (decl)
834 && DECL_EXTERNAL (decl)
835 && DECL_INLINE_VAR_P (decl)
836 && DECL_ODR_USED (decl))
837 error_at (DECL_SOURCE_LOCATION (decl),
838 "odr-used inline variable %qD is not defined", decl);
841 /* Clear out the list, so we don't rescan next time. */
842 static_decls = NULL;
844 /* Write out any globals that need to be output. */
845 return wrapup_global_declarations (statics->address (),
846 statics->length ());
848 return 0;
851 /* In C++, you don't have to write `struct S' to refer to `S'; you
852 can just use `S'. We accomplish this by creating a TYPE_DECL as
853 if the user had written `typedef struct S S'. Create and return
854 the TYPE_DECL for TYPE. */
856 tree
857 create_implicit_typedef (tree name, tree type)
859 tree decl;
861 decl = build_decl (input_location, TYPE_DECL, name, type);
862 DECL_ARTIFICIAL (decl) = 1;
863 /* There are other implicit type declarations, like the one *within*
864 a class that allows you to write `S::S'. We must distinguish
865 amongst these. */
866 SET_DECL_IMPLICIT_TYPEDEF_P (decl);
867 TYPE_NAME (type) = decl;
868 TYPE_STUB_DECL (type) = decl;
870 return decl;
873 /* Remember a local name for name-mangling purposes. */
875 static void
876 push_local_name (tree decl)
878 size_t i, nelts;
879 tree t, name;
881 timevar_start (TV_NAME_LOOKUP);
883 name = DECL_NAME (decl);
885 nelts = vec_safe_length (local_names);
886 for (i = 0; i < nelts; i++)
888 t = (*local_names)[i];
889 if (DECL_NAME (t) == name)
891 retrofit_lang_decl (decl);
892 DECL_LANG_SPECIFIC (decl)->u.base.u2sel = 1;
893 if (DECL_DISCRIMINATOR_SET_P (t))
894 DECL_DISCRIMINATOR (decl) = DECL_DISCRIMINATOR (t) + 1;
895 else
896 DECL_DISCRIMINATOR (decl) = 1;
898 (*local_names)[i] = decl;
899 timevar_stop (TV_NAME_LOOKUP);
900 return;
904 vec_safe_push (local_names, decl);
905 timevar_stop (TV_NAME_LOOKUP);
908 /* Subroutine of duplicate_decls: return truthvalue of whether
909 or not types of these decls match.
911 For C++, we must compare the parameter list so that `int' can match
912 `int&' in a parameter position, but `int&' is not confused with
913 `const int&'. */
916 decls_match (tree newdecl, tree olddecl, bool record_versions /* = true */)
918 int types_match;
920 if (newdecl == olddecl)
921 return 1;
923 if (TREE_CODE (newdecl) != TREE_CODE (olddecl))
924 /* If the two DECLs are not even the same kind of thing, we're not
925 interested in their types. */
926 return 0;
928 gcc_assert (DECL_P (newdecl));
930 if (TREE_CODE (newdecl) == FUNCTION_DECL)
932 tree f1 = TREE_TYPE (newdecl);
933 tree f2 = TREE_TYPE (olddecl);
934 tree p1 = TYPE_ARG_TYPES (f1);
935 tree p2 = TYPE_ARG_TYPES (f2);
936 tree r2;
938 /* Specializations of different templates are different functions
939 even if they have the same type. */
940 tree t1 = (DECL_USE_TEMPLATE (newdecl)
941 ? DECL_TI_TEMPLATE (newdecl)
942 : NULL_TREE);
943 tree t2 = (DECL_USE_TEMPLATE (olddecl)
944 ? DECL_TI_TEMPLATE (olddecl)
945 : NULL_TREE);
946 if (t1 != t2)
947 return 0;
949 if (CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl)
950 && ! (DECL_EXTERN_C_P (newdecl)
951 && DECL_EXTERN_C_P (olddecl)))
952 return 0;
954 /* A new declaration doesn't match a built-in one unless it
955 is also extern "C". */
956 if (DECL_IS_BUILTIN (olddecl)
957 && DECL_EXTERN_C_P (olddecl) && !DECL_EXTERN_C_P (newdecl))
958 return 0;
960 if (TREE_CODE (f1) != TREE_CODE (f2))
961 return 0;
963 /* A declaration with deduced return type should use its pre-deduction
964 type for declaration matching. */
965 r2 = fndecl_declared_return_type (olddecl);
967 if (same_type_p (TREE_TYPE (f1), r2))
969 if (!prototype_p (f2) && DECL_EXTERN_C_P (olddecl)
970 && (DECL_BUILT_IN (olddecl)
971 #ifdef SYSTEM_IMPLICIT_EXTERN_C
972 || (DECL_IN_SYSTEM_HEADER (newdecl) && !DECL_CLASS_SCOPE_P (newdecl))
973 || (DECL_IN_SYSTEM_HEADER (olddecl) && !DECL_CLASS_SCOPE_P (olddecl))
974 #endif
977 types_match = self_promoting_args_p (p1);
978 if (p1 == void_list_node)
979 TREE_TYPE (newdecl) = TREE_TYPE (olddecl);
981 #ifdef SYSTEM_IMPLICIT_EXTERN_C
982 else if (!prototype_p (f1)
983 && (DECL_EXTERN_C_P (olddecl)
984 && DECL_IN_SYSTEM_HEADER (olddecl)
985 && !DECL_CLASS_SCOPE_P (olddecl))
986 && (DECL_EXTERN_C_P (newdecl)
987 && DECL_IN_SYSTEM_HEADER (newdecl)
988 && !DECL_CLASS_SCOPE_P (newdecl)))
990 types_match = self_promoting_args_p (p2);
991 TREE_TYPE (newdecl) = TREE_TYPE (olddecl);
993 #endif
994 else
995 types_match =
996 compparms (p1, p2)
997 && type_memfn_rqual (f1) == type_memfn_rqual (f2)
998 && (TYPE_ATTRIBUTES (TREE_TYPE (newdecl)) == NULL_TREE
999 || comp_type_attributes (TREE_TYPE (newdecl),
1000 TREE_TYPE (olddecl)) != 0);
1002 else
1003 types_match = 0;
1005 /* The decls dont match if they correspond to two different versions
1006 of the same function. Disallow extern "C" functions to be
1007 versions for now. */
1008 if (types_match
1009 && !DECL_EXTERN_C_P (newdecl)
1010 && !DECL_EXTERN_C_P (olddecl)
1011 && record_versions
1012 && maybe_version_functions (newdecl, olddecl,
1013 (!DECL_FUNCTION_VERSIONED (newdecl)
1014 || !DECL_FUNCTION_VERSIONED (olddecl))))
1015 return 0;
1017 else if (TREE_CODE (newdecl) == TEMPLATE_DECL)
1019 tree oldres = DECL_TEMPLATE_RESULT (olddecl);
1020 tree newres = DECL_TEMPLATE_RESULT (newdecl);
1022 if (TREE_CODE (newres) != TREE_CODE (oldres))
1023 return 0;
1025 if (!comp_template_parms (DECL_TEMPLATE_PARMS (newdecl),
1026 DECL_TEMPLATE_PARMS (olddecl)))
1027 return 0;
1029 if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL)
1030 types_match = (same_type_p (TREE_TYPE (oldres), TREE_TYPE (newres))
1031 && equivalently_constrained (olddecl, newdecl));
1032 else
1033 // We don't need to check equivalently_constrained for variable and
1034 // function templates because we check it on the results.
1035 types_match = decls_match (oldres, newres);
1037 else
1039 /* Need to check scope for variable declaration (VAR_DECL).
1040 For typedef (TYPE_DECL), scope is ignored. */
1041 if (VAR_P (newdecl)
1042 && CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl)
1043 /* [dcl.link]
1044 Two declarations for an object with C language linkage
1045 with the same name (ignoring the namespace that qualify
1046 it) that appear in different namespace scopes refer to
1047 the same object. */
1048 && !(DECL_EXTERN_C_P (olddecl) && DECL_EXTERN_C_P (newdecl)))
1049 return 0;
1051 if (TREE_TYPE (newdecl) == error_mark_node)
1052 types_match = TREE_TYPE (olddecl) == error_mark_node;
1053 else if (TREE_TYPE (olddecl) == NULL_TREE)
1054 types_match = TREE_TYPE (newdecl) == NULL_TREE;
1055 else if (TREE_TYPE (newdecl) == NULL_TREE)
1056 types_match = 0;
1057 else
1058 types_match = comptypes (TREE_TYPE (newdecl),
1059 TREE_TYPE (olddecl),
1060 COMPARE_REDECLARATION);
1063 // Normal functions can be constrained, as can variable partial
1064 // specializations.
1065 if (types_match && VAR_OR_FUNCTION_DECL_P (newdecl))
1066 types_match = equivalently_constrained (newdecl, olddecl);
1068 return types_match;
1071 /* NEWDECL and OLDDECL have identical signatures. If they are
1072 different versions adjust them and return true.
1073 If RECORD is set to true, record function versions. */
1075 bool
1076 maybe_version_functions (tree newdecl, tree olddecl, bool record)
1078 if (!targetm.target_option.function_versions (newdecl, olddecl))
1079 return false;
1081 if (!DECL_FUNCTION_VERSIONED (olddecl))
1083 DECL_FUNCTION_VERSIONED (olddecl) = 1;
1084 if (DECL_ASSEMBLER_NAME_SET_P (olddecl))
1085 mangle_decl (olddecl);
1088 if (!DECL_FUNCTION_VERSIONED (newdecl))
1090 DECL_FUNCTION_VERSIONED (newdecl) = 1;
1091 if (DECL_ASSEMBLER_NAME_SET_P (newdecl))
1092 mangle_decl (newdecl);
1095 if (record)
1096 cgraph_node::record_function_versions (olddecl, newdecl);
1098 return true;
1101 /* If NEWDECL is `static' and an `extern' was seen previously,
1102 warn about it. OLDDECL is the previous declaration.
1104 Note that this does not apply to the C++ case of declaring
1105 a variable `extern const' and then later `const'.
1107 Don't complain about built-in functions, since they are beyond
1108 the user's control. */
1110 void
1111 warn_extern_redeclared_static (tree newdecl, tree olddecl)
1113 if (TREE_CODE (newdecl) == TYPE_DECL
1114 || TREE_CODE (newdecl) == TEMPLATE_DECL
1115 || TREE_CODE (newdecl) == CONST_DECL
1116 || TREE_CODE (newdecl) == NAMESPACE_DECL)
1117 return;
1119 /* Don't get confused by static member functions; that's a different
1120 use of `static'. */
1121 if (TREE_CODE (newdecl) == FUNCTION_DECL
1122 && DECL_STATIC_FUNCTION_P (newdecl))
1123 return;
1125 /* If the old declaration was `static', or the new one isn't, then
1126 everything is OK. */
1127 if (DECL_THIS_STATIC (olddecl) || !DECL_THIS_STATIC (newdecl))
1128 return;
1130 /* It's OK to declare a builtin function as `static'. */
1131 if (TREE_CODE (olddecl) == FUNCTION_DECL
1132 && DECL_ARTIFICIAL (olddecl))
1133 return;
1135 if (permerror (DECL_SOURCE_LOCATION (newdecl),
1136 "%qD was declared %<extern%> and later %<static%>", newdecl))
1137 inform (DECL_SOURCE_LOCATION (olddecl),
1138 "previous declaration of %qD", olddecl);
1141 /* NEW_DECL is a redeclaration of OLD_DECL; both are functions or
1142 function templates. If their exception specifications do not
1143 match, issue a diagnostic. */
1145 static void
1146 check_redeclaration_exception_specification (tree new_decl,
1147 tree old_decl)
1149 tree new_exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (new_decl));
1150 tree old_exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (old_decl));
1152 /* Two default specs are equivalent, don't force evaluation. */
1153 if (UNEVALUATED_NOEXCEPT_SPEC_P (new_exceptions)
1154 && UNEVALUATED_NOEXCEPT_SPEC_P (old_exceptions))
1155 return;
1157 if (!type_dependent_expression_p (old_decl))
1159 maybe_instantiate_noexcept (new_decl);
1160 maybe_instantiate_noexcept (old_decl);
1162 new_exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (new_decl));
1163 old_exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (old_decl));
1165 /* [except.spec]
1167 If any declaration of a function has an exception-specification,
1168 all declarations, including the definition and an explicit
1169 specialization, of that function shall have an
1170 exception-specification with the same set of type-ids. */
1171 if (! DECL_IS_BUILTIN (old_decl)
1172 && !comp_except_specs (new_exceptions, old_exceptions, ce_normal))
1174 const char *const msg
1175 = G_("declaration of %qF has a different exception specifier");
1176 bool complained = true;
1177 location_t new_loc = DECL_SOURCE_LOCATION (new_decl);
1178 if (DECL_IN_SYSTEM_HEADER (old_decl))
1179 complained = pedwarn (new_loc, OPT_Wsystem_headers, msg, new_decl);
1180 else if (!flag_exceptions)
1181 /* We used to silently permit mismatched eh specs with
1182 -fno-exceptions, so make them a pedwarn now. */
1183 complained = pedwarn (new_loc, OPT_Wpedantic, msg, new_decl);
1184 else
1185 error_at (new_loc, msg, new_decl);
1186 if (complained)
1187 inform (DECL_SOURCE_LOCATION (old_decl),
1188 "from previous declaration %qF", old_decl);
1192 /* Return true if OLD_DECL and NEW_DECL agree on constexprness.
1193 Otherwise issue diagnostics. */
1195 static bool
1196 validate_constexpr_redeclaration (tree old_decl, tree new_decl)
1198 old_decl = STRIP_TEMPLATE (old_decl);
1199 new_decl = STRIP_TEMPLATE (new_decl);
1200 if (!VAR_OR_FUNCTION_DECL_P (old_decl)
1201 || !VAR_OR_FUNCTION_DECL_P (new_decl))
1202 return true;
1203 if (DECL_DECLARED_CONSTEXPR_P (old_decl)
1204 == DECL_DECLARED_CONSTEXPR_P (new_decl))
1205 return true;
1206 if (TREE_CODE (old_decl) == FUNCTION_DECL)
1208 if (DECL_BUILT_IN (old_decl))
1210 /* Hide a built-in declaration. */
1211 DECL_DECLARED_CONSTEXPR_P (old_decl)
1212 = DECL_DECLARED_CONSTEXPR_P (new_decl);
1213 return true;
1215 /* 7.1.5 [dcl.constexpr]
1216 Note: An explicit specialization can differ from the template
1217 declaration with respect to the constexpr specifier. */
1218 if (! DECL_TEMPLATE_SPECIALIZATION (old_decl)
1219 && DECL_TEMPLATE_SPECIALIZATION (new_decl))
1220 return true;
1222 error_at (DECL_SOURCE_LOCATION (new_decl),
1223 "redeclaration %qD differs in %<constexpr%> "
1224 "from previous declaration", new_decl);
1225 inform (DECL_SOURCE_LOCATION (old_decl),
1226 "previous declaration %qD", old_decl);
1227 return false;
1229 return true;
1232 // If OLDDECL and NEWDECL are concept declarations with the same type
1233 // (i.e., and template parameters), but different requirements,
1234 // emit diagnostics and return true. Otherwise, return false.
1235 static inline bool
1236 check_concept_refinement (tree olddecl, tree newdecl)
1238 if (!DECL_DECLARED_CONCEPT_P (olddecl) || !DECL_DECLARED_CONCEPT_P (newdecl))
1239 return false;
1241 tree d1 = DECL_TEMPLATE_RESULT (olddecl);
1242 tree d2 = DECL_TEMPLATE_RESULT (newdecl);
1243 if (TREE_CODE (d1) != TREE_CODE (d2))
1244 return false;
1246 tree t1 = TREE_TYPE (d1);
1247 tree t2 = TREE_TYPE (d2);
1248 if (TREE_CODE (d1) == FUNCTION_DECL)
1250 if (compparms (TYPE_ARG_TYPES (t1), TYPE_ARG_TYPES (t2))
1251 && comp_template_parms (DECL_TEMPLATE_PARMS (olddecl),
1252 DECL_TEMPLATE_PARMS (newdecl))
1253 && !equivalently_constrained (olddecl, newdecl))
1255 error ("cannot specialize concept %q#D", olddecl);
1256 return true;
1259 return false;
1262 /* DECL is a redeclaration of a function or function template. If
1263 it does have default arguments issue a diagnostic. Note: this
1264 function is used to enforce the requirements in C++11 8.3.6 about
1265 no default arguments in redeclarations. */
1267 static void
1268 check_redeclaration_no_default_args (tree decl)
1270 gcc_assert (DECL_DECLARES_FUNCTION_P (decl));
1272 for (tree t = FUNCTION_FIRST_USER_PARMTYPE (decl);
1273 t && t != void_list_node; t = TREE_CHAIN (t))
1274 if (TREE_PURPOSE (t))
1276 permerror (DECL_SOURCE_LOCATION (decl),
1277 "redeclaration of %q#D may not have default "
1278 "arguments", decl);
1279 return;
1283 /* Merge tree bits that correspond to attributes noreturn, nothrow,
1284 const, malloc, and pure from NEWDECL with those of OLDDECL. */
1286 static void
1287 merge_attribute_bits (tree newdecl, tree olddecl)
1289 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1290 TREE_THIS_VOLATILE (olddecl) |= TREE_THIS_VOLATILE (newdecl);
1291 TREE_NOTHROW (newdecl) |= TREE_NOTHROW (olddecl);
1292 TREE_NOTHROW (olddecl) |= TREE_NOTHROW (newdecl);
1293 TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
1294 TREE_READONLY (olddecl) |= TREE_READONLY (newdecl);
1295 DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
1296 DECL_IS_MALLOC (olddecl) |= DECL_IS_MALLOC (newdecl);
1297 DECL_PURE_P (newdecl) |= DECL_PURE_P (olddecl);
1298 DECL_PURE_P (olddecl) |= DECL_PURE_P (newdecl);
1299 DECL_UNINLINABLE (newdecl) |= DECL_UNINLINABLE (olddecl);
1300 DECL_UNINLINABLE (olddecl) |= DECL_UNINLINABLE (newdecl);
1303 #define GNU_INLINE_P(fn) (DECL_DECLARED_INLINE_P (fn) \
1304 && lookup_attribute ("gnu_inline", \
1305 DECL_ATTRIBUTES (fn)))
1307 /* If NEWDECL is a redeclaration of OLDDECL, merge the declarations.
1308 If the redeclaration is invalid, a diagnostic is issued, and the
1309 error_mark_node is returned. Otherwise, OLDDECL is returned.
1311 If NEWDECL is not a redeclaration of OLDDECL, NULL_TREE is
1312 returned.
1314 NEWDECL_IS_FRIEND is true if NEWDECL was declared as a friend. */
1316 tree
1317 duplicate_decls (tree newdecl, tree olddecl, bool newdecl_is_friend)
1319 unsigned olddecl_uid = DECL_UID (olddecl);
1320 int olddecl_friend = 0, types_match = 0, hidden_friend = 0;
1321 int new_defines_function = 0;
1322 tree new_template_info;
1323 location_t olddecl_loc = DECL_SOURCE_LOCATION (olddecl);
1324 location_t newdecl_loc = DECL_SOURCE_LOCATION (newdecl);
1326 if (newdecl == olddecl)
1327 return olddecl;
1329 types_match = decls_match (newdecl, olddecl);
1331 /* If either the type of the new decl or the type of the old decl is an
1332 error_mark_node, then that implies that we have already issued an
1333 error (earlier) for some bogus type specification, and in that case,
1334 it is rather pointless to harass the user with yet more error message
1335 about the same declaration, so just pretend the types match here. */
1336 if (TREE_TYPE (newdecl) == error_mark_node
1337 || TREE_TYPE (olddecl) == error_mark_node)
1338 return error_mark_node;
1340 if (DECL_NAME (newdecl)
1341 && DECL_NAME (olddecl)
1342 && UDLIT_OPER_P (DECL_NAME (newdecl))
1343 && UDLIT_OPER_P (DECL_NAME (olddecl)))
1345 if (TREE_CODE (newdecl) == TEMPLATE_DECL
1346 && TREE_CODE (olddecl) != TEMPLATE_DECL
1347 && check_raw_literal_operator (olddecl))
1348 error_at (newdecl_loc,
1349 "literal operator template %qD conflicts with"
1350 " raw literal operator %qD", newdecl, olddecl);
1351 else if (TREE_CODE (newdecl) != TEMPLATE_DECL
1352 && TREE_CODE (olddecl) == TEMPLATE_DECL
1353 && check_raw_literal_operator (newdecl))
1354 error_at (newdecl_loc,
1355 "raw literal operator %qD conflicts with"
1356 " literal operator template %qD", newdecl, olddecl);
1359 /* True to merge attributes between the declarations, false to
1360 set OLDDECL's attributes to those of NEWDECL (for template
1361 explicit specializations that specify their own attributes
1362 independent of those specified for the primary template). */
1363 const bool merge_attr = (TREE_CODE (newdecl) != FUNCTION_DECL
1364 || !DECL_TEMPLATE_SPECIALIZATION (newdecl)
1365 || DECL_TEMPLATE_SPECIALIZATION (olddecl));
1367 if (DECL_P (olddecl)
1368 && TREE_CODE (newdecl) == FUNCTION_DECL
1369 && TREE_CODE (olddecl) == FUNCTION_DECL
1370 && merge_attr
1371 && diagnose_mismatched_attributes (olddecl, newdecl))
1373 if (DECL_INITIAL (olddecl))
1374 inform (olddecl_loc,
1375 "previous definition of %qD was here", olddecl);
1376 else
1377 inform (olddecl_loc,
1378 "previous declaration of %qD was here", olddecl);
1381 /* Check for redeclaration and other discrepancies. */
1382 if (TREE_CODE (olddecl) == FUNCTION_DECL
1383 && DECL_ARTIFICIAL (olddecl))
1385 gcc_assert (!DECL_HIDDEN_FRIEND_P (olddecl));
1386 if (TREE_CODE (newdecl) != FUNCTION_DECL)
1388 /* Avoid warnings redeclaring built-ins which have not been
1389 explicitly declared. */
1390 if (DECL_ANTICIPATED (olddecl))
1392 if (TREE_PUBLIC (newdecl)
1393 && CP_DECL_CONTEXT (newdecl) == global_namespace)
1394 warning_at (newdecl_loc,
1395 OPT_Wbuiltin_declaration_mismatch,
1396 "built-in function %qD declared as non-function",
1397 newdecl);
1398 return NULL_TREE;
1401 /* If you declare a built-in or predefined function name as static,
1402 the old definition is overridden, but optionally warn this was a
1403 bad choice of name. */
1404 if (! TREE_PUBLIC (newdecl))
1406 warning_at (newdecl_loc,
1407 OPT_Wshadow,
1408 DECL_BUILT_IN (olddecl)
1409 ? G_("shadowing built-in function %q#D")
1410 : G_("shadowing library function %q#D"), olddecl);
1411 /* Discard the old built-in function. */
1412 return NULL_TREE;
1414 /* If the built-in is not ansi, then programs can override
1415 it even globally without an error. */
1416 else if (! DECL_BUILT_IN (olddecl))
1417 warning_at (newdecl_loc, 0,
1418 "library function %q#D redeclared as non-function %q#D",
1419 olddecl, newdecl);
1420 else
1421 error_at (newdecl_loc,
1422 "declaration of %q#D conflicts with built-in "
1423 "declaration %q#D", newdecl, olddecl);
1424 return NULL_TREE;
1426 else if (DECL_OMP_DECLARE_REDUCTION_P (olddecl))
1428 gcc_assert (DECL_OMP_DECLARE_REDUCTION_P (newdecl));
1429 error_at (newdecl_loc,
1430 "redeclaration of %<pragma omp declare reduction%>");
1431 inform (olddecl_loc,
1432 "previous %<pragma omp declare reduction%> declaration");
1433 return error_mark_node;
1435 else if (!types_match)
1437 /* Avoid warnings redeclaring built-ins which have not been
1438 explicitly declared. */
1439 if (DECL_ANTICIPATED (olddecl))
1441 tree t1, t2;
1443 /* A new declaration doesn't match a built-in one unless it
1444 is also extern "C". */
1445 gcc_assert (DECL_IS_BUILTIN (olddecl));
1446 gcc_assert (DECL_EXTERN_C_P (olddecl));
1447 if (!DECL_EXTERN_C_P (newdecl))
1448 return NULL_TREE;
1450 for (t1 = TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
1451 t2 = TYPE_ARG_TYPES (TREE_TYPE (olddecl));
1452 t1 || t2;
1453 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1455 if (!t1 || !t2)
1456 break;
1457 /* FILE, tm types are not known at the time
1458 we create the builtins. */
1459 for (unsigned i = 0;
1460 i < sizeof (builtin_structptr_types)
1461 / sizeof (builtin_structptr_type);
1462 ++i)
1463 if (TREE_VALUE (t2) == builtin_structptr_types[i].node)
1465 tree t = TREE_VALUE (t1);
1467 if (TYPE_PTR_P (t)
1468 && TYPE_IDENTIFIER (TREE_TYPE (t))
1469 == get_identifier (builtin_structptr_types[i].str)
1470 && compparms (TREE_CHAIN (t1), TREE_CHAIN (t2)))
1472 tree oldargs = TYPE_ARG_TYPES (TREE_TYPE (olddecl));
1474 TYPE_ARG_TYPES (TREE_TYPE (olddecl))
1475 = TYPE_ARG_TYPES (TREE_TYPE (newdecl));
1476 types_match = decls_match (newdecl, olddecl);
1477 if (types_match)
1478 return duplicate_decls (newdecl, olddecl,
1479 newdecl_is_friend);
1480 TYPE_ARG_TYPES (TREE_TYPE (olddecl)) = oldargs;
1482 goto next_arg;
1485 if (! same_type_p (TREE_VALUE (t1), TREE_VALUE (t2)))
1486 break;
1487 next_arg:;
1490 warning_at (newdecl_loc,
1491 OPT_Wbuiltin_declaration_mismatch,
1492 "declaration of %q#D conflicts with built-in "
1493 "declaration %q#D", newdecl, olddecl);
1495 else if ((DECL_EXTERN_C_P (newdecl)
1496 && DECL_EXTERN_C_P (olddecl))
1497 || compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
1498 TYPE_ARG_TYPES (TREE_TYPE (olddecl))))
1500 /* Don't really override olddecl for __* prefixed builtins
1501 except for __[^b]*_chk, the compiler might be using those
1502 explicitly. */
1503 if (DECL_BUILT_IN (olddecl))
1505 tree id = DECL_NAME (olddecl);
1506 const char *name = IDENTIFIER_POINTER (id);
1507 size_t len;
1509 if (name[0] == '_'
1510 && name[1] == '_'
1511 && (strncmp (name + 2, "builtin_",
1512 strlen ("builtin_")) == 0
1513 || (len = strlen (name)) <= strlen ("___chk")
1514 || memcmp (name + len - strlen ("_chk"),
1515 "_chk", strlen ("_chk") + 1) != 0))
1517 if (DECL_INITIAL (newdecl))
1519 error_at (newdecl_loc,
1520 "definition of %q#D ambiguates built-in "
1521 "declaration %q#D", newdecl, olddecl);
1522 return error_mark_node;
1524 if (permerror (newdecl_loc,
1525 "new declaration %q#D ambiguates built-in"
1526 " declaration %q#D", newdecl, olddecl)
1527 && flag_permissive)
1528 inform (newdecl_loc,
1529 "ignoring the %q#D declaration", newdecl);
1530 return flag_permissive ? olddecl : error_mark_node;
1534 /* A near match; override the builtin. */
1536 if (TREE_PUBLIC (newdecl))
1537 warning_at (newdecl_loc,
1538 OPT_Wbuiltin_declaration_mismatch,
1539 "new declaration %q#D ambiguates built-in "
1540 "declaration %q#D", newdecl, olddecl);
1541 else
1542 warning (OPT_Wshadow,
1543 DECL_BUILT_IN (olddecl)
1544 ? G_("shadowing built-in function %q#D")
1545 : G_("shadowing library function %q#D"), olddecl);
1547 else
1548 /* Discard the old built-in function. */
1549 return NULL_TREE;
1551 /* Replace the old RTL to avoid problems with inlining. */
1552 COPY_DECL_RTL (newdecl, olddecl);
1554 /* Even if the types match, prefer the new declarations type for
1555 built-ins which have not been explicitly declared, for
1556 exception lists, etc... */
1557 else if (DECL_IS_BUILTIN (olddecl))
1559 tree type = TREE_TYPE (newdecl);
1560 tree attribs = (*targetm.merge_type_attributes)
1561 (TREE_TYPE (olddecl), type);
1563 type = cp_build_type_attribute_variant (type, attribs);
1564 TREE_TYPE (newdecl) = TREE_TYPE (olddecl) = type;
1567 /* If a function is explicitly declared "throw ()", propagate that to
1568 the corresponding builtin. */
1569 if (DECL_BUILT_IN_CLASS (olddecl) == BUILT_IN_NORMAL
1570 && DECL_ANTICIPATED (olddecl)
1571 && TREE_NOTHROW (newdecl)
1572 && !TREE_NOTHROW (olddecl))
1574 enum built_in_function fncode = DECL_FUNCTION_CODE (olddecl);
1575 tree tmpdecl = builtin_decl_explicit (fncode);
1576 if (tmpdecl && tmpdecl != olddecl && types_match)
1577 TREE_NOTHROW (tmpdecl) = 1;
1580 /* Whether or not the builtin can throw exceptions has no
1581 bearing on this declarator. */
1582 TREE_NOTHROW (olddecl) = 0;
1584 if (DECL_THIS_STATIC (newdecl) && !DECL_THIS_STATIC (olddecl))
1586 /* If a builtin function is redeclared as `static', merge
1587 the declarations, but make the original one static. */
1588 DECL_THIS_STATIC (olddecl) = 1;
1589 TREE_PUBLIC (olddecl) = 0;
1591 /* Make the old declaration consistent with the new one so
1592 that all remnants of the builtin-ness of this function
1593 will be banished. */
1594 SET_DECL_LANGUAGE (olddecl, DECL_LANGUAGE (newdecl));
1595 COPY_DECL_RTL (newdecl, olddecl);
1598 else if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
1600 /* C++ Standard, 3.3, clause 4:
1601 "[Note: a namespace name or a class template name must be unique
1602 in its declarative region (7.3.2, clause 14). ]" */
1603 if (TREE_CODE (olddecl) != NAMESPACE_DECL
1604 && TREE_CODE (newdecl) != NAMESPACE_DECL
1605 && (TREE_CODE (olddecl) != TEMPLATE_DECL
1606 || TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) != TYPE_DECL)
1607 && (TREE_CODE (newdecl) != TEMPLATE_DECL
1608 || TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) != TYPE_DECL))
1610 if ((TREE_CODE (olddecl) == TYPE_DECL && DECL_ARTIFICIAL (olddecl)
1611 && TREE_CODE (newdecl) != TYPE_DECL)
1612 || (TREE_CODE (newdecl) == TYPE_DECL && DECL_ARTIFICIAL (newdecl)
1613 && TREE_CODE (olddecl) != TYPE_DECL))
1615 /* We do nothing special here, because C++ does such nasty
1616 things with TYPE_DECLs. Instead, just let the TYPE_DECL
1617 get shadowed, and know that if we need to find a TYPE_DECL
1618 for a given name, we can look in the IDENTIFIER_TYPE_VALUE
1619 slot of the identifier. */
1620 return NULL_TREE;
1623 if ((TREE_CODE (newdecl) == FUNCTION_DECL
1624 && DECL_FUNCTION_TEMPLATE_P (olddecl))
1625 || (TREE_CODE (olddecl) == FUNCTION_DECL
1626 && DECL_FUNCTION_TEMPLATE_P (newdecl)))
1627 return NULL_TREE;
1630 error ("%q#D redeclared as different kind of symbol", newdecl);
1631 if (TREE_CODE (olddecl) == TREE_LIST)
1632 olddecl = TREE_VALUE (olddecl);
1633 inform (olddecl_loc,
1634 "previous declaration %q#D", olddecl);
1636 return error_mark_node;
1638 else if (!types_match)
1640 if (CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl))
1641 /* These are certainly not duplicate declarations; they're
1642 from different scopes. */
1643 return NULL_TREE;
1645 if (TREE_CODE (newdecl) == TEMPLATE_DECL)
1647 /* The name of a class template may not be declared to refer to
1648 any other template, class, function, object, namespace, value,
1649 or type in the same scope. */
1650 if (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) == TYPE_DECL
1651 || TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL)
1653 error_at (newdecl_loc,
1654 "conflicting declaration of template %q#D", newdecl);
1655 inform (olddecl_loc,
1656 "previous declaration %q#D", olddecl);
1657 return error_mark_node;
1659 else if (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) == FUNCTION_DECL
1660 && TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == FUNCTION_DECL
1661 && compparms (TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (olddecl))),
1662 TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (newdecl))))
1663 && comp_template_parms (DECL_TEMPLATE_PARMS (newdecl),
1664 DECL_TEMPLATE_PARMS (olddecl))
1665 /* Template functions can be disambiguated by
1666 return type. */
1667 && same_type_p (TREE_TYPE (TREE_TYPE (newdecl)),
1668 TREE_TYPE (TREE_TYPE (olddecl)))
1669 // Template functions can also be disambiguated by
1670 // constraints.
1671 && equivalently_constrained (olddecl, newdecl))
1673 error_at (newdecl_loc, "ambiguating new declaration %q#D",
1674 newdecl);
1675 inform (olddecl_loc,
1676 "old declaration %q#D", olddecl);
1678 else if (check_concept_refinement (olddecl, newdecl))
1679 return error_mark_node;
1680 return NULL_TREE;
1682 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1684 if (DECL_EXTERN_C_P (newdecl) && DECL_EXTERN_C_P (olddecl))
1686 error_at (newdecl_loc,
1687 "conflicting declaration of C function %q#D",
1688 newdecl);
1689 inform (olddecl_loc,
1690 "previous declaration %q#D", olddecl);
1691 return NULL_TREE;
1693 /* For function versions, params and types match, but they
1694 are not ambiguous. */
1695 else if ((!DECL_FUNCTION_VERSIONED (newdecl)
1696 && !DECL_FUNCTION_VERSIONED (olddecl))
1697 // The functions have the same parameter types.
1698 && compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
1699 TYPE_ARG_TYPES (TREE_TYPE (olddecl)))
1700 // And the same constraints.
1701 && equivalently_constrained (newdecl, olddecl))
1703 error_at (newdecl_loc,
1704 "ambiguating new declaration of %q#D", newdecl);
1705 inform (olddecl_loc,
1706 "old declaration %q#D", olddecl);
1707 return error_mark_node;
1709 else
1710 return NULL_TREE;
1712 else
1714 error_at (newdecl_loc, "conflicting declaration %q#D", newdecl);
1715 inform (olddecl_loc,
1716 "previous declaration as %q#D", olddecl);
1717 return error_mark_node;
1720 else if (TREE_CODE (newdecl) == FUNCTION_DECL
1721 && ((DECL_TEMPLATE_SPECIALIZATION (olddecl)
1722 && (!DECL_TEMPLATE_INFO (newdecl)
1723 || (DECL_TI_TEMPLATE (newdecl)
1724 != DECL_TI_TEMPLATE (olddecl))))
1725 || (DECL_TEMPLATE_SPECIALIZATION (newdecl)
1726 && (!DECL_TEMPLATE_INFO (olddecl)
1727 || (DECL_TI_TEMPLATE (olddecl)
1728 != DECL_TI_TEMPLATE (newdecl))))))
1729 /* It's OK to have a template specialization and a non-template
1730 with the same type, or to have specializations of two
1731 different templates with the same type. Note that if one is a
1732 specialization, and the other is an instantiation of the same
1733 template, that we do not exit at this point. That situation
1734 can occur if we instantiate a template class, and then
1735 specialize one of its methods. This situation is valid, but
1736 the declarations must be merged in the usual way. */
1737 return NULL_TREE;
1738 else if (TREE_CODE (newdecl) == FUNCTION_DECL
1739 && ((DECL_TEMPLATE_INSTANTIATION (olddecl)
1740 && !DECL_USE_TEMPLATE (newdecl))
1741 || (DECL_TEMPLATE_INSTANTIATION (newdecl)
1742 && !DECL_USE_TEMPLATE (olddecl))))
1743 /* One of the declarations is a template instantiation, and the
1744 other is not a template at all. That's OK. */
1745 return NULL_TREE;
1746 else if (TREE_CODE (newdecl) == NAMESPACE_DECL)
1748 /* In [namespace.alias] we have:
1750 In a declarative region, a namespace-alias-definition can be
1751 used to redefine a namespace-alias declared in that declarative
1752 region to refer only to the namespace to which it already
1753 refers.
1755 Therefore, if we encounter a second alias directive for the same
1756 alias, we can just ignore the second directive. */
1757 if (DECL_NAMESPACE_ALIAS (newdecl)
1758 && (DECL_NAMESPACE_ALIAS (newdecl)
1759 == DECL_NAMESPACE_ALIAS (olddecl)))
1760 return olddecl;
1762 /* Leave it to update_binding to merge or report error. */
1763 return NULL_TREE;
1765 else
1767 const char *errmsg = redeclaration_error_message (newdecl, olddecl);
1768 if (errmsg)
1770 error_at (newdecl_loc, errmsg, newdecl);
1771 if (DECL_NAME (olddecl) != NULL_TREE)
1772 inform (olddecl_loc,
1773 (DECL_INITIAL (olddecl) && namespace_bindings_p ())
1774 ? G_("%q#D previously defined here")
1775 : G_("%q#D previously declared here"), olddecl);
1776 return error_mark_node;
1778 else if (TREE_CODE (olddecl) == FUNCTION_DECL
1779 && DECL_INITIAL (olddecl) != NULL_TREE
1780 && !prototype_p (TREE_TYPE (olddecl))
1781 && prototype_p (TREE_TYPE (newdecl)))
1783 /* Prototype decl follows defn w/o prototype. */
1784 if (warning_at (newdecl_loc, 0,
1785 "prototype specified for %q#D", newdecl))
1786 inform (olddecl_loc,
1787 "previous non-prototype definition here");
1789 else if (VAR_OR_FUNCTION_DECL_P (olddecl)
1790 && DECL_LANGUAGE (newdecl) != DECL_LANGUAGE (olddecl))
1792 /* [dcl.link]
1793 If two declarations of the same function or object
1794 specify different linkage-specifications ..., the program
1795 is ill-formed.... Except for functions with C++ linkage,
1796 a function declaration without a linkage specification
1797 shall not precede the first linkage specification for
1798 that function. A function can be declared without a
1799 linkage specification after an explicit linkage
1800 specification has been seen; the linkage explicitly
1801 specified in the earlier declaration is not affected by
1802 such a function declaration.
1804 DR 563 raises the question why the restrictions on
1805 functions should not also apply to objects. Older
1806 versions of G++ silently ignore the linkage-specification
1807 for this example:
1809 namespace N {
1810 extern int i;
1811 extern "C" int i;
1814 which is clearly wrong. Therefore, we now treat objects
1815 like functions. */
1816 if (current_lang_depth () == 0)
1818 /* There is no explicit linkage-specification, so we use
1819 the linkage from the previous declaration. */
1820 retrofit_lang_decl (newdecl);
1821 SET_DECL_LANGUAGE (newdecl, DECL_LANGUAGE (olddecl));
1823 else
1825 error_at (newdecl_loc,
1826 "conflicting declaration of %q#D with %qL linkage",
1827 newdecl, DECL_LANGUAGE (newdecl));
1828 inform (olddecl_loc,
1829 "previous declaration with %qL linkage",
1830 DECL_LANGUAGE (olddecl));
1834 if (DECL_LANG_SPECIFIC (olddecl) && DECL_USE_TEMPLATE (olddecl))
1836 else if (TREE_CODE (olddecl) == FUNCTION_DECL)
1838 /* Note: free functions, as TEMPLATE_DECLs, are handled below. */
1839 if (DECL_FUNCTION_MEMBER_P (olddecl)
1840 && (/* grokfndecl passes member function templates too
1841 as FUNCTION_DECLs. */
1842 DECL_TEMPLATE_INFO (olddecl)
1843 /* C++11 8.3.6/6.
1844 Default arguments for a member function of a class
1845 template shall be specified on the initial declaration
1846 of the member function within the class template. */
1847 || CLASSTYPE_TEMPLATE_INFO (CP_DECL_CONTEXT (olddecl))))
1848 check_redeclaration_no_default_args (newdecl);
1849 else
1851 tree t1 = FUNCTION_FIRST_USER_PARMTYPE (olddecl);
1852 tree t2 = FUNCTION_FIRST_USER_PARMTYPE (newdecl);
1853 int i = 1;
1855 for (; t1 && t1 != void_list_node;
1856 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2), i++)
1857 if (TREE_PURPOSE (t1) && TREE_PURPOSE (t2))
1859 if (simple_cst_equal (TREE_PURPOSE (t1),
1860 TREE_PURPOSE (t2)) == 1)
1862 if (permerror (newdecl_loc,
1863 "default argument given for parameter "
1864 "%d of %q#D", i, newdecl))
1865 inform (olddecl_loc,
1866 "previous specification in %q#D here",
1867 olddecl);
1869 else
1871 error_at (newdecl_loc,
1872 "default argument given for parameter %d "
1873 "of %q#D", i, newdecl);
1874 inform (olddecl_loc,
1875 "previous specification in %q#D here",
1876 olddecl);
1883 /* Do not merge an implicit typedef with an explicit one. In:
1885 class A;
1887 typedef class A A __attribute__ ((foo));
1889 the attribute should apply only to the typedef. */
1890 if (TREE_CODE (olddecl) == TYPE_DECL
1891 && (DECL_IMPLICIT_TYPEDEF_P (olddecl)
1892 || DECL_IMPLICIT_TYPEDEF_P (newdecl)))
1893 return NULL_TREE;
1895 /* If new decl is `static' and an `extern' was seen previously,
1896 warn about it. */
1897 warn_extern_redeclared_static (newdecl, olddecl);
1899 if (!validate_constexpr_redeclaration (olddecl, newdecl))
1900 return error_mark_node;
1902 /* We have committed to returning 1 at this point. */
1903 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1905 /* Now that functions must hold information normally held
1906 by field decls, there is extra work to do so that
1907 declaration information does not get destroyed during
1908 definition. */
1909 if (DECL_VINDEX (olddecl))
1910 DECL_VINDEX (newdecl) = DECL_VINDEX (olddecl);
1911 if (DECL_CONTEXT (olddecl))
1912 DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
1913 DECL_STATIC_CONSTRUCTOR (newdecl) |= DECL_STATIC_CONSTRUCTOR (olddecl);
1914 DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
1915 DECL_PURE_VIRTUAL_P (newdecl) |= DECL_PURE_VIRTUAL_P (olddecl);
1916 DECL_VIRTUAL_P (newdecl) |= DECL_VIRTUAL_P (olddecl);
1917 DECL_INVALID_OVERRIDER_P (newdecl) |= DECL_INVALID_OVERRIDER_P (olddecl);
1918 DECL_FINAL_P (newdecl) |= DECL_FINAL_P (olddecl);
1919 DECL_OVERRIDE_P (newdecl) |= DECL_OVERRIDE_P (olddecl);
1920 DECL_THIS_STATIC (newdecl) |= DECL_THIS_STATIC (olddecl);
1921 if (DECL_OVERLOADED_OPERATOR_P (olddecl))
1922 DECL_OVERLOADED_OPERATOR_CODE_RAW (newdecl)
1923 = DECL_OVERLOADED_OPERATOR_CODE_RAW (olddecl);
1924 new_defines_function = DECL_INITIAL (newdecl) != NULL_TREE;
1926 /* Optionally warn about more than one declaration for the same
1927 name, but don't warn about a function declaration followed by a
1928 definition. */
1929 if (warn_redundant_decls && ! DECL_ARTIFICIAL (olddecl)
1930 && !(new_defines_function && DECL_INITIAL (olddecl) == NULL_TREE)
1931 /* Don't warn about extern decl followed by definition. */
1932 && !(DECL_EXTERNAL (olddecl) && ! DECL_EXTERNAL (newdecl))
1933 /* Don't warn about friends, let add_friend take care of it. */
1934 && ! (newdecl_is_friend || DECL_FRIEND_P (olddecl))
1935 /* Don't warn about declaration followed by specialization. */
1936 && (! DECL_TEMPLATE_SPECIALIZATION (newdecl)
1937 || DECL_TEMPLATE_SPECIALIZATION (olddecl)))
1939 if (warning_at (newdecl_loc,
1940 OPT_Wredundant_decls,
1941 "redundant redeclaration of %qD in same scope",
1942 newdecl))
1943 inform (olddecl_loc,
1944 "previous declaration of %qD", olddecl);
1947 if (!(DECL_TEMPLATE_INSTANTIATION (olddecl)
1948 && DECL_TEMPLATE_SPECIALIZATION (newdecl)))
1950 if (DECL_DELETED_FN (newdecl))
1952 error_at (newdecl_loc, "deleted definition of %qD", newdecl);
1953 inform (olddecl_loc,
1954 "previous declaration of %qD", olddecl);
1956 DECL_DELETED_FN (newdecl) |= DECL_DELETED_FN (olddecl);
1960 /* Deal with C++: must preserve virtual function table size. */
1961 if (TREE_CODE (olddecl) == TYPE_DECL)
1963 tree newtype = TREE_TYPE (newdecl);
1964 tree oldtype = TREE_TYPE (olddecl);
1966 if (newtype != error_mark_node && oldtype != error_mark_node
1967 && TYPE_LANG_SPECIFIC (newtype) && TYPE_LANG_SPECIFIC (oldtype))
1968 CLASSTYPE_FRIEND_CLASSES (newtype)
1969 = CLASSTYPE_FRIEND_CLASSES (oldtype);
1971 DECL_ORIGINAL_TYPE (newdecl) = DECL_ORIGINAL_TYPE (olddecl);
1974 /* Copy all the DECL_... slots specified in the new decl except for
1975 any that we copy here from the old type. */
1976 if (merge_attr)
1977 DECL_ATTRIBUTES (newdecl)
1978 = (*targetm.merge_decl_attributes) (olddecl, newdecl);
1979 else
1980 DECL_ATTRIBUTES (olddecl) = DECL_ATTRIBUTES (newdecl);
1982 if (DECL_DECLARES_FUNCTION_P (olddecl) && DECL_DECLARES_FUNCTION_P (newdecl))
1984 olddecl_friend = DECL_FRIEND_P (olddecl);
1985 hidden_friend = (DECL_ANTICIPATED (olddecl)
1986 && DECL_HIDDEN_FRIEND_P (olddecl)
1987 && newdecl_is_friend);
1988 if (!hidden_friend)
1990 DECL_ANTICIPATED (olddecl) = 0;
1991 DECL_HIDDEN_FRIEND_P (olddecl) = 0;
1995 if (TREE_CODE (newdecl) == TEMPLATE_DECL)
1997 tree old_result;
1998 tree new_result;
1999 old_result = DECL_TEMPLATE_RESULT (olddecl);
2000 new_result = DECL_TEMPLATE_RESULT (newdecl);
2001 TREE_TYPE (olddecl) = TREE_TYPE (old_result);
2002 DECL_TEMPLATE_SPECIALIZATIONS (olddecl)
2003 = chainon (DECL_TEMPLATE_SPECIALIZATIONS (olddecl),
2004 DECL_TEMPLATE_SPECIALIZATIONS (newdecl));
2006 DECL_ATTRIBUTES (old_result)
2007 = (*targetm.merge_decl_attributes) (old_result, new_result);
2009 if (DECL_FUNCTION_TEMPLATE_P (newdecl))
2011 /* Per C++11 8.3.6/4, default arguments cannot be added in later
2012 declarations of a function template. */
2013 if (DECL_SOURCE_LOCATION (newdecl)
2014 != DECL_SOURCE_LOCATION (olddecl))
2015 check_redeclaration_no_default_args (newdecl);
2017 check_default_args (newdecl);
2019 if (GNU_INLINE_P (old_result) != GNU_INLINE_P (new_result)
2020 && DECL_INITIAL (new_result))
2022 if (DECL_INITIAL (old_result))
2023 DECL_UNINLINABLE (old_result) = 1;
2024 else
2025 DECL_UNINLINABLE (old_result) = DECL_UNINLINABLE (new_result);
2026 DECL_EXTERNAL (old_result) = DECL_EXTERNAL (new_result);
2027 DECL_NOT_REALLY_EXTERN (old_result)
2028 = DECL_NOT_REALLY_EXTERN (new_result);
2029 DECL_INTERFACE_KNOWN (old_result)
2030 = DECL_INTERFACE_KNOWN (new_result);
2031 DECL_DECLARED_INLINE_P (old_result)
2032 = DECL_DECLARED_INLINE_P (new_result);
2033 DECL_DISREGARD_INLINE_LIMITS (old_result)
2034 |= DECL_DISREGARD_INLINE_LIMITS (new_result);
2037 else
2039 DECL_DECLARED_INLINE_P (old_result)
2040 |= DECL_DECLARED_INLINE_P (new_result);
2041 DECL_DISREGARD_INLINE_LIMITS (old_result)
2042 |= DECL_DISREGARD_INLINE_LIMITS (new_result);
2043 check_redeclaration_exception_specification (newdecl, olddecl);
2045 merge_attribute_bits (new_result, old_result);
2049 /* If the new declaration is a definition, update the file and
2050 line information on the declaration, and also make
2051 the old declaration the same definition. */
2052 if (DECL_INITIAL (new_result) != NULL_TREE)
2054 DECL_SOURCE_LOCATION (olddecl)
2055 = DECL_SOURCE_LOCATION (old_result)
2056 = DECL_SOURCE_LOCATION (newdecl);
2057 DECL_INITIAL (old_result) = DECL_INITIAL (new_result);
2058 if (DECL_FUNCTION_TEMPLATE_P (newdecl))
2060 tree parm;
2061 DECL_ARGUMENTS (old_result)
2062 = DECL_ARGUMENTS (new_result);
2063 for (parm = DECL_ARGUMENTS (old_result); parm;
2064 parm = DECL_CHAIN (parm))
2065 DECL_CONTEXT (parm) = old_result;
2069 return olddecl;
2072 if (types_match)
2074 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2075 check_redeclaration_exception_specification (newdecl, olddecl);
2077 /* Automatically handles default parameters. */
2078 tree oldtype = TREE_TYPE (olddecl);
2079 tree newtype;
2081 /* For typedefs use the old type, as the new type's DECL_NAME points
2082 at newdecl, which will be ggc_freed. */
2083 if (TREE_CODE (newdecl) == TYPE_DECL)
2085 /* But NEWTYPE might have an attribute, honor that. */
2086 tree tem = TREE_TYPE (newdecl);
2087 newtype = oldtype;
2089 if (TYPE_USER_ALIGN (tem))
2091 if (TYPE_ALIGN (tem) > TYPE_ALIGN (newtype))
2092 SET_TYPE_ALIGN (newtype, TYPE_ALIGN (tem));
2093 TYPE_USER_ALIGN (newtype) = true;
2096 /* And remove the new type from the variants list. */
2097 if (TYPE_NAME (TREE_TYPE (newdecl)) == newdecl)
2099 tree remove = TREE_TYPE (newdecl);
2100 for (tree t = TYPE_MAIN_VARIANT (remove); ;
2101 t = TYPE_NEXT_VARIANT (t))
2102 if (TYPE_NEXT_VARIANT (t) == remove)
2104 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (remove);
2105 break;
2109 else if (merge_attr)
2110 newtype = merge_types (TREE_TYPE (newdecl), TREE_TYPE (olddecl));
2111 else
2112 newtype = TREE_TYPE (newdecl);
2114 if (VAR_P (newdecl))
2116 DECL_THIS_EXTERN (newdecl) |= DECL_THIS_EXTERN (olddecl);
2117 /* For already initialized vars, TREE_READONLY could have been
2118 cleared in cp_finish_decl, because the var needs runtime
2119 initialization or destruction. Make sure not to set
2120 TREE_READONLY on it again. */
2121 if (DECL_INITIALIZED_P (olddecl)
2122 && !DECL_EXTERNAL (olddecl)
2123 && !TREE_READONLY (olddecl))
2124 TREE_READONLY (newdecl) = 0;
2125 DECL_INITIALIZED_P (newdecl) |= DECL_INITIALIZED_P (olddecl);
2126 DECL_NONTRIVIALLY_INITIALIZED_P (newdecl)
2127 |= DECL_NONTRIVIALLY_INITIALIZED_P (olddecl);
2128 if (DECL_DEPENDENT_INIT_P (olddecl))
2129 SET_DECL_DEPENDENT_INIT_P (newdecl, true);
2130 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (newdecl)
2131 |= DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (olddecl);
2132 if (DECL_CLASS_SCOPE_P (olddecl))
2133 DECL_DECLARED_CONSTEXPR_P (newdecl)
2134 |= DECL_DECLARED_CONSTEXPR_P (olddecl);
2136 /* Merge the threadprivate attribute from OLDDECL into NEWDECL. */
2137 if (DECL_LANG_SPECIFIC (olddecl)
2138 && CP_DECL_THREADPRIVATE_P (olddecl))
2140 /* Allocate a LANG_SPECIFIC structure for NEWDECL, if needed. */
2141 retrofit_lang_decl (newdecl);
2142 CP_DECL_THREADPRIVATE_P (newdecl) = 1;
2146 /* An explicit specialization of a function template or of a member
2147 function of a class template can be declared transaction_safe
2148 independently of whether the corresponding template entity is declared
2149 transaction_safe. */
2150 if (flag_tm && TREE_CODE (newdecl) == FUNCTION_DECL
2151 && DECL_TEMPLATE_INSTANTIATION (olddecl)
2152 && DECL_TEMPLATE_SPECIALIZATION (newdecl)
2153 && tx_safe_fn_type_p (newtype)
2154 && !tx_safe_fn_type_p (TREE_TYPE (newdecl)))
2155 newtype = tx_unsafe_fn_variant (newtype);
2157 TREE_TYPE (newdecl) = TREE_TYPE (olddecl) = newtype;
2159 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2160 check_default_args (newdecl);
2162 /* Lay the type out, unless already done. */
2163 if (! same_type_p (newtype, oldtype)
2164 && TREE_TYPE (newdecl) != error_mark_node
2165 && !(processing_template_decl && uses_template_parms (newdecl)))
2166 layout_type (TREE_TYPE (newdecl));
2168 if ((VAR_P (newdecl)
2169 || TREE_CODE (newdecl) == PARM_DECL
2170 || TREE_CODE (newdecl) == RESULT_DECL
2171 || TREE_CODE (newdecl) == FIELD_DECL
2172 || TREE_CODE (newdecl) == TYPE_DECL)
2173 && !(processing_template_decl && uses_template_parms (newdecl)))
2174 layout_decl (newdecl, 0);
2176 /* Merge deprecatedness. */
2177 if (TREE_DEPRECATED (newdecl))
2178 TREE_DEPRECATED (olddecl) = 1;
2180 /* Preserve function specific target and optimization options */
2181 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2183 if (DECL_FUNCTION_SPECIFIC_TARGET (olddecl)
2184 && !DECL_FUNCTION_SPECIFIC_TARGET (newdecl))
2185 DECL_FUNCTION_SPECIFIC_TARGET (newdecl)
2186 = DECL_FUNCTION_SPECIFIC_TARGET (olddecl);
2188 if (DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl)
2189 && !DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl))
2190 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl)
2191 = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl);
2193 else
2195 /* Merge the const type qualifier. */
2196 if (TREE_READONLY (newdecl))
2197 TREE_READONLY (olddecl) = 1;
2198 /* Merge the volatile type qualifier. */
2199 if (TREE_THIS_VOLATILE (newdecl))
2200 TREE_THIS_VOLATILE (olddecl) = 1;
2203 /* Merge the initialization information. */
2204 if (DECL_INITIAL (newdecl) == NULL_TREE
2205 && DECL_INITIAL (olddecl) != NULL_TREE)
2207 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
2208 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
2209 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2211 DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
2212 DECL_STRUCT_FUNCTION (newdecl) = DECL_STRUCT_FUNCTION (olddecl);
2216 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2218 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
2219 |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
2220 DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
2221 DECL_IS_OPERATOR_NEW (newdecl) |= DECL_IS_OPERATOR_NEW (olddecl);
2222 DECL_LOOPING_CONST_OR_PURE_P (newdecl)
2223 |= DECL_LOOPING_CONST_OR_PURE_P (olddecl);
2225 if (merge_attr)
2226 merge_attribute_bits (newdecl, olddecl);
2227 else
2229 /* Merge the noreturn bit. */
2230 TREE_THIS_VOLATILE (olddecl) = TREE_THIS_VOLATILE (newdecl);
2231 TREE_READONLY (olddecl) = TREE_READONLY (newdecl);
2232 TREE_NOTHROW (olddecl) = TREE_NOTHROW (newdecl);
2233 DECL_IS_MALLOC (olddecl) = DECL_IS_MALLOC (newdecl);
2234 DECL_PURE_P (olddecl) = DECL_PURE_P (newdecl);
2236 /* Keep the old RTL. */
2237 COPY_DECL_RTL (olddecl, newdecl);
2239 else if (VAR_P (newdecl)
2240 && (DECL_SIZE (olddecl) || !DECL_SIZE (newdecl)))
2242 /* Keep the old RTL. We cannot keep the old RTL if the old
2243 declaration was for an incomplete object and the new
2244 declaration is not since many attributes of the RTL will
2245 change. */
2246 COPY_DECL_RTL (olddecl, newdecl);
2249 /* If cannot merge, then use the new type and qualifiers,
2250 and don't preserve the old rtl. */
2251 else
2253 /* Clean out any memory we had of the old declaration. */
2254 tree oldstatic = value_member (olddecl, static_aggregates);
2255 if (oldstatic)
2256 TREE_VALUE (oldstatic) = error_mark_node;
2258 TREE_TYPE (olddecl) = TREE_TYPE (newdecl);
2259 TREE_READONLY (olddecl) = TREE_READONLY (newdecl);
2260 TREE_THIS_VOLATILE (olddecl) = TREE_THIS_VOLATILE (newdecl);
2261 TREE_NOTHROW (olddecl) = TREE_NOTHROW (newdecl);
2262 TREE_SIDE_EFFECTS (olddecl) = TREE_SIDE_EFFECTS (newdecl);
2265 /* Merge the storage class information. */
2266 merge_weak (newdecl, olddecl);
2268 DECL_DEFER_OUTPUT (newdecl) |= DECL_DEFER_OUTPUT (olddecl);
2269 TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
2270 TREE_STATIC (olddecl) = TREE_STATIC (newdecl) |= TREE_STATIC (olddecl);
2271 if (! DECL_EXTERNAL (olddecl))
2272 DECL_EXTERNAL (newdecl) = 0;
2273 if (! DECL_COMDAT (olddecl))
2274 DECL_COMDAT (newdecl) = 0;
2276 new_template_info = NULL_TREE;
2277 if (DECL_LANG_SPECIFIC (newdecl) && DECL_LANG_SPECIFIC (olddecl))
2279 bool new_redefines_gnu_inline = false;
2281 if (new_defines_function
2282 && ((DECL_INTERFACE_KNOWN (olddecl)
2283 && TREE_CODE (olddecl) == FUNCTION_DECL)
2284 || (TREE_CODE (olddecl) == TEMPLATE_DECL
2285 && (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl))
2286 == FUNCTION_DECL))))
2288 tree fn = olddecl;
2290 if (TREE_CODE (fn) == TEMPLATE_DECL)
2291 fn = DECL_TEMPLATE_RESULT (olddecl);
2293 new_redefines_gnu_inline = GNU_INLINE_P (fn) && DECL_INITIAL (fn);
2296 if (!new_redefines_gnu_inline)
2298 DECL_INTERFACE_KNOWN (newdecl) |= DECL_INTERFACE_KNOWN (olddecl);
2299 DECL_NOT_REALLY_EXTERN (newdecl) |= DECL_NOT_REALLY_EXTERN (olddecl);
2300 DECL_COMDAT (newdecl) |= DECL_COMDAT (olddecl);
2302 DECL_TEMPLATE_INSTANTIATED (newdecl)
2303 |= DECL_TEMPLATE_INSTANTIATED (olddecl);
2304 DECL_ODR_USED (newdecl) |= DECL_ODR_USED (olddecl);
2306 /* If the OLDDECL is an instantiation and/or specialization,
2307 then the NEWDECL must be too. But, it may not yet be marked
2308 as such if the caller has created NEWDECL, but has not yet
2309 figured out that it is a redeclaration. */
2310 if (!DECL_USE_TEMPLATE (newdecl))
2311 DECL_USE_TEMPLATE (newdecl) = DECL_USE_TEMPLATE (olddecl);
2313 /* Don't really know how much of the language-specific
2314 values we should copy from old to new. */
2315 DECL_IN_AGGR_P (newdecl) = DECL_IN_AGGR_P (olddecl);
2316 DECL_REPO_AVAILABLE_P (newdecl) = DECL_REPO_AVAILABLE_P (olddecl);
2317 DECL_INITIALIZED_IN_CLASS_P (newdecl)
2318 |= DECL_INITIALIZED_IN_CLASS_P (olddecl);
2320 if (LANG_DECL_HAS_MIN (newdecl))
2322 DECL_LANG_SPECIFIC (newdecl)->u.min.u2 =
2323 DECL_LANG_SPECIFIC (olddecl)->u.min.u2;
2324 if (DECL_TEMPLATE_INFO (newdecl))
2326 new_template_info = DECL_TEMPLATE_INFO (newdecl);
2327 if (DECL_TEMPLATE_INSTANTIATION (olddecl)
2328 && DECL_TEMPLATE_SPECIALIZATION (newdecl))
2329 /* Remember the presence of explicit specialization args. */
2330 TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (olddecl))
2331 = TINFO_USED_TEMPLATE_ID (new_template_info);
2333 DECL_TEMPLATE_INFO (newdecl) = DECL_TEMPLATE_INFO (olddecl);
2335 /* Only functions have these fields. */
2336 if (DECL_DECLARES_FUNCTION_P (newdecl))
2338 DECL_NONCONVERTING_P (newdecl) = DECL_NONCONVERTING_P (olddecl);
2339 DECL_BEFRIENDING_CLASSES (newdecl)
2340 = chainon (DECL_BEFRIENDING_CLASSES (newdecl),
2341 DECL_BEFRIENDING_CLASSES (olddecl));
2342 /* DECL_THUNKS is only valid for virtual functions,
2343 otherwise it is a DECL_FRIEND_CONTEXT. */
2344 if (DECL_VIRTUAL_P (newdecl))
2345 SET_DECL_THUNKS (newdecl, DECL_THUNKS (olddecl));
2347 /* Only variables have this field. */
2348 else if (VAR_P (newdecl)
2349 && VAR_HAD_UNKNOWN_BOUND (olddecl))
2350 SET_VAR_HAD_UNKNOWN_BOUND (newdecl);
2353 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2355 tree parm;
2357 /* Merge parameter attributes. */
2358 tree oldarg, newarg;
2359 for (oldarg = DECL_ARGUMENTS(olddecl),
2360 newarg = DECL_ARGUMENTS(newdecl);
2361 oldarg && newarg;
2362 oldarg = DECL_CHAIN(oldarg), newarg = DECL_CHAIN(newarg)) {
2363 DECL_ATTRIBUTES (newarg)
2364 = (*targetm.merge_decl_attributes) (oldarg, newarg);
2365 DECL_ATTRIBUTES (oldarg) = DECL_ATTRIBUTES (newarg);
2368 if (DECL_TEMPLATE_INSTANTIATION (olddecl)
2369 && !DECL_TEMPLATE_INSTANTIATION (newdecl))
2371 /* If newdecl is not a specialization, then it is not a
2372 template-related function at all. And that means that we
2373 should have exited above, returning 0. */
2374 gcc_assert (DECL_TEMPLATE_SPECIALIZATION (newdecl));
2376 if (DECL_ODR_USED (olddecl))
2377 /* From [temp.expl.spec]:
2379 If a template, a member template or the member of a class
2380 template is explicitly specialized then that
2381 specialization shall be declared before the first use of
2382 that specialization that would cause an implicit
2383 instantiation to take place, in every translation unit in
2384 which such a use occurs. */
2385 error ("explicit specialization of %qD after first use",
2386 olddecl);
2388 SET_DECL_TEMPLATE_SPECIALIZATION (olddecl);
2389 DECL_COMDAT (newdecl) = (TREE_PUBLIC (newdecl)
2390 && DECL_DECLARED_INLINE_P (newdecl));
2392 /* Don't propagate visibility from the template to the
2393 specialization here. We'll do that in determine_visibility if
2394 appropriate. */
2395 DECL_VISIBILITY_SPECIFIED (olddecl) = 0;
2397 /* [temp.expl.spec/14] We don't inline explicit specialization
2398 just because the primary template says so. */
2399 gcc_assert (!merge_attr);
2401 DECL_DECLARED_INLINE_P (olddecl)
2402 = DECL_DECLARED_INLINE_P (newdecl);
2404 DECL_DISREGARD_INLINE_LIMITS (olddecl)
2405 = DECL_DISREGARD_INLINE_LIMITS (newdecl);
2407 DECL_UNINLINABLE (olddecl) = DECL_UNINLINABLE (newdecl);
2409 else if (new_defines_function && DECL_INITIAL (olddecl))
2411 /* Never inline re-defined extern inline functions.
2412 FIXME: this could be better handled by keeping both
2413 function as separate declarations. */
2414 DECL_UNINLINABLE (newdecl) = 1;
2416 else
2418 if (DECL_PENDING_INLINE_P (olddecl))
2420 DECL_PENDING_INLINE_P (newdecl) = 1;
2421 DECL_PENDING_INLINE_INFO (newdecl)
2422 = DECL_PENDING_INLINE_INFO (olddecl);
2424 else if (DECL_PENDING_INLINE_P (newdecl))
2426 else if (DECL_SAVED_FUNCTION_DATA (newdecl) == NULL)
2427 DECL_SAVED_FUNCTION_DATA (newdecl)
2428 = DECL_SAVED_FUNCTION_DATA (olddecl);
2430 DECL_DECLARED_INLINE_P (newdecl) |= DECL_DECLARED_INLINE_P (olddecl);
2432 DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
2433 = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
2435 DECL_DISREGARD_INLINE_LIMITS (newdecl)
2436 = DECL_DISREGARD_INLINE_LIMITS (olddecl)
2437 = (DECL_DISREGARD_INLINE_LIMITS (newdecl)
2438 || DECL_DISREGARD_INLINE_LIMITS (olddecl));
2441 /* Preserve abstractness on cloned [cd]tors. */
2442 DECL_ABSTRACT_P (newdecl) = DECL_ABSTRACT_P (olddecl);
2444 /* Update newdecl's parms to point at olddecl. */
2445 for (parm = DECL_ARGUMENTS (newdecl); parm;
2446 parm = DECL_CHAIN (parm))
2447 DECL_CONTEXT (parm) = olddecl;
2449 if (! types_match)
2451 SET_DECL_LANGUAGE (olddecl, DECL_LANGUAGE (newdecl));
2452 COPY_DECL_ASSEMBLER_NAME (newdecl, olddecl);
2453 COPY_DECL_RTL (newdecl, olddecl);
2455 if (! types_match || new_defines_function)
2457 /* These need to be copied so that the names are available.
2458 Note that if the types do match, we'll preserve inline
2459 info and other bits, but if not, we won't. */
2460 DECL_ARGUMENTS (olddecl) = DECL_ARGUMENTS (newdecl);
2461 DECL_RESULT (olddecl) = DECL_RESULT (newdecl);
2463 /* If redeclaring a builtin function, it stays built in
2464 if newdecl is a gnu_inline definition, or if newdecl is just
2465 a declaration. */
2466 if (DECL_BUILT_IN (olddecl)
2467 && (new_defines_function ? GNU_INLINE_P (newdecl) : types_match))
2469 DECL_BUILT_IN_CLASS (newdecl) = DECL_BUILT_IN_CLASS (olddecl);
2470 DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl);
2471 /* If we're keeping the built-in definition, keep the rtl,
2472 regardless of declaration matches. */
2473 COPY_DECL_RTL (olddecl, newdecl);
2474 if (DECL_BUILT_IN_CLASS (newdecl) == BUILT_IN_NORMAL)
2476 enum built_in_function fncode = DECL_FUNCTION_CODE (newdecl);
2477 switch (fncode)
2479 /* If a compatible prototype of these builtin functions
2480 is seen, assume the runtime implements it with the
2481 expected semantics. */
2482 case BUILT_IN_STPCPY:
2483 if (builtin_decl_explicit_p (fncode))
2484 set_builtin_decl_implicit_p (fncode, true);
2485 break;
2486 default:
2487 if (builtin_decl_explicit_p (fncode))
2488 set_builtin_decl_declared_p (fncode, true);
2489 break;
2493 copy_attributes_to_builtin (newdecl);
2495 if (new_defines_function)
2496 /* If defining a function declared with other language
2497 linkage, use the previously declared language linkage. */
2498 SET_DECL_LANGUAGE (newdecl, DECL_LANGUAGE (olddecl));
2499 else if (types_match)
2501 DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
2502 /* Don't clear out the arguments if we're just redeclaring a
2503 function. */
2504 if (DECL_ARGUMENTS (olddecl))
2505 DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl);
2508 else if (TREE_CODE (newdecl) == NAMESPACE_DECL)
2509 NAMESPACE_LEVEL (newdecl) = NAMESPACE_LEVEL (olddecl);
2511 /* Now preserve various other info from the definition. */
2512 TREE_ADDRESSABLE (newdecl) = TREE_ADDRESSABLE (olddecl);
2513 TREE_ASM_WRITTEN (newdecl) = TREE_ASM_WRITTEN (olddecl);
2514 DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
2515 COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
2517 /* Warn about conflicting visibility specifications. */
2518 if (DECL_VISIBILITY_SPECIFIED (olddecl)
2519 && DECL_VISIBILITY_SPECIFIED (newdecl)
2520 && DECL_VISIBILITY (newdecl) != DECL_VISIBILITY (olddecl))
2522 if (warning_at (newdecl_loc, OPT_Wattributes,
2523 "%qD: visibility attribute ignored because it "
2524 "conflicts with previous declaration", newdecl))
2525 inform (olddecl_loc,
2526 "previous declaration of %qD", olddecl);
2528 /* Choose the declaration which specified visibility. */
2529 if (DECL_VISIBILITY_SPECIFIED (olddecl))
2531 DECL_VISIBILITY (newdecl) = DECL_VISIBILITY (olddecl);
2532 DECL_VISIBILITY_SPECIFIED (newdecl) = 1;
2534 /* Init priority used to be merged from newdecl to olddecl by the memcpy,
2535 so keep this behavior. */
2536 if (VAR_P (newdecl) && DECL_HAS_INIT_PRIORITY_P (newdecl))
2538 SET_DECL_INIT_PRIORITY (olddecl, DECL_INIT_PRIORITY (newdecl));
2539 DECL_HAS_INIT_PRIORITY_P (olddecl) = 1;
2541 /* Likewise for DECL_ALIGN, DECL_USER_ALIGN and DECL_PACKED. */
2542 if (DECL_ALIGN (olddecl) > DECL_ALIGN (newdecl))
2544 SET_DECL_ALIGN (newdecl, DECL_ALIGN (olddecl));
2545 DECL_USER_ALIGN (newdecl) |= DECL_USER_ALIGN (olddecl);
2547 DECL_USER_ALIGN (olddecl) = DECL_USER_ALIGN (newdecl);
2548 if (DECL_WARN_IF_NOT_ALIGN (olddecl)
2549 > DECL_WARN_IF_NOT_ALIGN (newdecl))
2550 SET_DECL_WARN_IF_NOT_ALIGN (newdecl,
2551 DECL_WARN_IF_NOT_ALIGN (olddecl));
2552 if (TREE_CODE (newdecl) == FIELD_DECL)
2553 DECL_PACKED (olddecl) = DECL_PACKED (newdecl);
2555 /* The DECL_LANG_SPECIFIC information in OLDDECL will be replaced
2556 with that from NEWDECL below. */
2557 if (DECL_LANG_SPECIFIC (olddecl))
2559 gcc_assert (DECL_LANG_SPECIFIC (olddecl)
2560 != DECL_LANG_SPECIFIC (newdecl));
2561 ggc_free (DECL_LANG_SPECIFIC (olddecl));
2564 /* Merge the USED information. */
2565 if (TREE_USED (olddecl))
2566 TREE_USED (newdecl) = 1;
2567 else if (TREE_USED (newdecl))
2568 TREE_USED (olddecl) = 1;
2569 if (VAR_P (newdecl))
2571 if (DECL_READ_P (olddecl))
2572 DECL_READ_P (newdecl) = 1;
2573 else if (DECL_READ_P (newdecl))
2574 DECL_READ_P (olddecl) = 1;
2576 if (DECL_PRESERVE_P (olddecl))
2577 DECL_PRESERVE_P (newdecl) = 1;
2578 else if (DECL_PRESERVE_P (newdecl))
2579 DECL_PRESERVE_P (olddecl) = 1;
2581 /* Merge the DECL_FUNCTION_VERSIONED information. newdecl will be copied
2582 to olddecl and deleted. */
2583 if (TREE_CODE (newdecl) == FUNCTION_DECL
2584 && DECL_FUNCTION_VERSIONED (olddecl))
2586 /* Set the flag for newdecl so that it gets copied to olddecl. */
2587 DECL_FUNCTION_VERSIONED (newdecl) = 1;
2588 /* newdecl will be purged after copying to olddecl and is no longer
2589 a version. */
2590 cgraph_node::delete_function_version_by_decl (newdecl);
2593 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2595 int function_size;
2596 struct symtab_node *snode = symtab_node::get (olddecl);
2598 function_size = sizeof (struct tree_decl_common);
2600 memcpy ((char *) olddecl + sizeof (struct tree_common),
2601 (char *) newdecl + sizeof (struct tree_common),
2602 function_size - sizeof (struct tree_common));
2604 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2605 (char *) newdecl + sizeof (struct tree_decl_common),
2606 sizeof (struct tree_function_decl) - sizeof (struct tree_decl_common));
2608 /* Preserve symtab node mapping. */
2609 olddecl->decl_with_vis.symtab_node = snode;
2611 if (new_template_info)
2612 /* If newdecl is a template instantiation, it is possible that
2613 the following sequence of events has occurred:
2615 o A friend function was declared in a class template. The
2616 class template was instantiated.
2618 o The instantiation of the friend declaration was
2619 recorded on the instantiation list, and is newdecl.
2621 o Later, however, instantiate_class_template called pushdecl
2622 on the newdecl to perform name injection. But, pushdecl in
2623 turn called duplicate_decls when it discovered that another
2624 declaration of a global function with the same name already
2625 existed.
2627 o Here, in duplicate_decls, we decided to clobber newdecl.
2629 If we're going to do that, we'd better make sure that
2630 olddecl, and not newdecl, is on the list of
2631 instantiations so that if we try to do the instantiation
2632 again we won't get the clobbered declaration. */
2633 reregister_specialization (newdecl,
2634 new_template_info,
2635 olddecl);
2637 else
2639 size_t size = tree_code_size (TREE_CODE (newdecl));
2641 memcpy ((char *) olddecl + sizeof (struct tree_common),
2642 (char *) newdecl + sizeof (struct tree_common),
2643 sizeof (struct tree_decl_common) - sizeof (struct tree_common));
2644 switch (TREE_CODE (newdecl))
2646 case LABEL_DECL:
2647 case VAR_DECL:
2648 case RESULT_DECL:
2649 case PARM_DECL:
2650 case FIELD_DECL:
2651 case TYPE_DECL:
2652 case CONST_DECL:
2654 struct symtab_node *snode = NULL;
2656 if (VAR_P (newdecl)
2657 && (TREE_STATIC (olddecl) || TREE_PUBLIC (olddecl)
2658 || DECL_EXTERNAL (olddecl)))
2659 snode = symtab_node::get (olddecl);
2660 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2661 (char *) newdecl + sizeof (struct tree_decl_common),
2662 size - sizeof (struct tree_decl_common)
2663 + TREE_CODE_LENGTH (TREE_CODE (newdecl)) * sizeof (char *));
2664 if (VAR_P (newdecl))
2665 olddecl->decl_with_vis.symtab_node = snode;
2667 break;
2668 default:
2669 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2670 (char *) newdecl + sizeof (struct tree_decl_common),
2671 sizeof (struct tree_decl_non_common) - sizeof (struct tree_decl_common)
2672 + TREE_CODE_LENGTH (TREE_CODE (newdecl)) * sizeof (char *));
2673 break;
2677 if (VAR_OR_FUNCTION_DECL_P (newdecl))
2679 if (DECL_EXTERNAL (olddecl)
2680 || TREE_PUBLIC (olddecl)
2681 || TREE_STATIC (olddecl))
2683 /* Merge the section attribute.
2684 We want to issue an error if the sections conflict but that must be
2685 done later in decl_attributes since we are called before attributes
2686 are assigned. */
2687 if (DECL_SECTION_NAME (newdecl) != NULL)
2688 set_decl_section_name (olddecl, DECL_SECTION_NAME (newdecl));
2690 if (DECL_ONE_ONLY (newdecl))
2692 struct symtab_node *oldsym, *newsym;
2693 if (TREE_CODE (olddecl) == FUNCTION_DECL)
2694 oldsym = cgraph_node::get_create (olddecl);
2695 else
2696 oldsym = varpool_node::get_create (olddecl);
2697 newsym = symtab_node::get (newdecl);
2698 oldsym->set_comdat_group (newsym->get_comdat_group ());
2702 if (VAR_P (newdecl)
2703 && CP_DECL_THREAD_LOCAL_P (newdecl))
2705 CP_DECL_THREAD_LOCAL_P (olddecl) = true;
2706 if (!processing_template_decl)
2707 set_decl_tls_model (olddecl, DECL_TLS_MODEL (newdecl));
2711 DECL_UID (olddecl) = olddecl_uid;
2712 if (olddecl_friend)
2713 DECL_FRIEND_P (olddecl) = 1;
2714 if (hidden_friend)
2716 DECL_ANTICIPATED (olddecl) = 1;
2717 DECL_HIDDEN_FRIEND_P (olddecl) = 1;
2720 /* NEWDECL contains the merged attribute lists.
2721 Update OLDDECL to be the same. */
2722 DECL_ATTRIBUTES (olddecl) = DECL_ATTRIBUTES (newdecl);
2724 /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
2725 so that encode_section_info has a chance to look at the new decl
2726 flags and attributes. */
2727 if (DECL_RTL_SET_P (olddecl)
2728 && (TREE_CODE (olddecl) == FUNCTION_DECL
2729 || (VAR_P (olddecl)
2730 && TREE_STATIC (olddecl))))
2731 make_decl_rtl (olddecl);
2733 /* The NEWDECL will no longer be needed. Because every out-of-class
2734 declaration of a member results in a call to duplicate_decls,
2735 freeing these nodes represents in a significant savings.
2737 Before releasing the node, be sore to remove function from symbol
2738 table that might have been inserted there to record comdat group.
2739 Be sure to however do not free DECL_STRUCT_FUNCTION because this
2740 structure is shared in between newdecl and oldecl. */
2741 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2742 DECL_STRUCT_FUNCTION (newdecl) = NULL;
2743 if (VAR_OR_FUNCTION_DECL_P (newdecl))
2745 struct symtab_node *snode = symtab_node::get (newdecl);
2746 if (snode)
2747 snode->remove ();
2750 /* Remove the associated constraints for newdecl, if any, before
2751 reclaiming memory. */
2752 if (flag_concepts)
2753 remove_constraints (newdecl);
2755 ggc_free (newdecl);
2757 return olddecl;
2760 /* Return zero if the declaration NEWDECL is valid
2761 when the declaration OLDDECL (assumed to be for the same name)
2762 has already been seen.
2763 Otherwise return an error message format string with a %s
2764 where the identifier should go. */
2766 static const char *
2767 redeclaration_error_message (tree newdecl, tree olddecl)
2769 if (TREE_CODE (newdecl) == TYPE_DECL)
2771 /* Because C++ can put things into name space for free,
2772 constructs like "typedef struct foo { ... } foo"
2773 would look like an erroneous redeclaration. */
2774 if (same_type_p (TREE_TYPE (newdecl), TREE_TYPE (olddecl)))
2775 return NULL;
2776 else
2777 return G_("redefinition of %q#D");
2779 else if (TREE_CODE (newdecl) == FUNCTION_DECL)
2781 /* If this is a pure function, its olddecl will actually be
2782 the original initialization to `0' (which we force to call
2783 abort()). Don't complain about redefinition in this case. */
2784 if (DECL_LANG_SPECIFIC (olddecl) && DECL_PURE_VIRTUAL_P (olddecl)
2785 && DECL_INITIAL (olddecl) == NULL_TREE)
2786 return NULL;
2788 /* If both functions come from different namespaces, this is not
2789 a redeclaration - this is a conflict with a used function. */
2790 if (DECL_NAMESPACE_SCOPE_P (olddecl)
2791 && DECL_CONTEXT (olddecl) != DECL_CONTEXT (newdecl)
2792 && ! decls_match (olddecl, newdecl))
2793 return G_("%qD conflicts with used function");
2795 /* We'll complain about linkage mismatches in
2796 warn_extern_redeclared_static. */
2798 /* Defining the same name twice is no good. */
2799 if (decl_defined_p (olddecl)
2800 && decl_defined_p (newdecl))
2802 if (DECL_NAME (olddecl) == NULL_TREE)
2803 return G_("%q#D not declared in class");
2804 else if (!GNU_INLINE_P (olddecl)
2805 || GNU_INLINE_P (newdecl))
2806 return G_("redefinition of %q#D");
2809 if (DECL_DECLARED_INLINE_P (olddecl) && DECL_DECLARED_INLINE_P (newdecl))
2811 bool olda = GNU_INLINE_P (olddecl);
2812 bool newa = GNU_INLINE_P (newdecl);
2814 if (olda != newa)
2816 if (newa)
2817 return G_("%q+D redeclared inline with "
2818 "%<gnu_inline%> attribute");
2819 else
2820 return G_("%q+D redeclared inline without "
2821 "%<gnu_inline%> attribute");
2825 check_abi_tag_redeclaration
2826 (olddecl, lookup_attribute ("abi_tag", DECL_ATTRIBUTES (olddecl)),
2827 lookup_attribute ("abi_tag", DECL_ATTRIBUTES (newdecl)));
2829 return NULL;
2831 else if (TREE_CODE (newdecl) == TEMPLATE_DECL)
2833 tree nt, ot;
2835 if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL)
2837 if (COMPLETE_TYPE_P (TREE_TYPE (newdecl))
2838 && COMPLETE_TYPE_P (TREE_TYPE (olddecl)))
2839 return G_("redefinition of %q#D");
2840 return NULL;
2843 if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) != FUNCTION_DECL
2844 || (DECL_TEMPLATE_RESULT (newdecl)
2845 == DECL_TEMPLATE_RESULT (olddecl)))
2846 return NULL;
2848 nt = DECL_TEMPLATE_RESULT (newdecl);
2849 if (DECL_TEMPLATE_INFO (nt))
2850 nt = DECL_TEMPLATE_RESULT (template_for_substitution (nt));
2851 ot = DECL_TEMPLATE_RESULT (olddecl);
2852 if (DECL_TEMPLATE_INFO (ot))
2853 ot = DECL_TEMPLATE_RESULT (template_for_substitution (ot));
2854 if (DECL_INITIAL (nt) && DECL_INITIAL (ot)
2855 && (!GNU_INLINE_P (ot) || GNU_INLINE_P (nt)))
2856 return G_("redefinition of %q#D");
2858 if (DECL_DECLARED_INLINE_P (ot) && DECL_DECLARED_INLINE_P (nt))
2860 bool olda = GNU_INLINE_P (ot);
2861 bool newa = GNU_INLINE_P (nt);
2863 if (olda != newa)
2865 if (newa)
2866 return G_("%q+D redeclared inline with "
2867 "%<gnu_inline%> attribute");
2868 else
2869 return G_("%q+D redeclared inline without "
2870 "%<gnu_inline%> attribute");
2874 /* Core issue #226 (C++0x):
2876 If a friend function template declaration specifies a
2877 default template-argument, that declaration shall be a
2878 definition and shall be the only declaration of the
2879 function template in the translation unit. */
2880 if ((cxx_dialect != cxx98)
2881 && TREE_CODE (ot) == FUNCTION_DECL && DECL_FRIEND_P (ot)
2882 && !check_default_tmpl_args (nt, DECL_TEMPLATE_PARMS (newdecl),
2883 /*is_primary=*/true,
2884 /*is_partial=*/false,
2885 /*is_friend_decl=*/2))
2886 return G_("redeclaration of friend %q#D "
2887 "may not have default template arguments");
2889 return NULL;
2891 else if (VAR_P (newdecl)
2892 && CP_DECL_THREAD_LOCAL_P (newdecl) != CP_DECL_THREAD_LOCAL_P (olddecl)
2893 && (! DECL_LANG_SPECIFIC (olddecl)
2894 || ! CP_DECL_THREADPRIVATE_P (olddecl)
2895 || CP_DECL_THREAD_LOCAL_P (newdecl)))
2897 /* Only variables can be thread-local, and all declarations must
2898 agree on this property. */
2899 if (CP_DECL_THREAD_LOCAL_P (newdecl))
2900 return G_("thread-local declaration of %q#D follows "
2901 "non-thread-local declaration");
2902 else
2903 return G_("non-thread-local declaration of %q#D follows "
2904 "thread-local declaration");
2906 else if (toplevel_bindings_p () || DECL_NAMESPACE_SCOPE_P (newdecl))
2908 /* The objects have been declared at namespace scope. If either
2909 is a member of an anonymous union, then this is an invalid
2910 redeclaration. For example:
2912 int i;
2913 union { int i; };
2915 is invalid. */
2916 if ((VAR_P (newdecl) && DECL_ANON_UNION_VAR_P (newdecl))
2917 || (VAR_P (olddecl) && DECL_ANON_UNION_VAR_P (olddecl)))
2918 return G_("redeclaration of %q#D");
2919 /* If at least one declaration is a reference, there is no
2920 conflict. For example:
2922 int i = 3;
2923 extern int i;
2925 is valid. */
2926 if (DECL_EXTERNAL (newdecl) || DECL_EXTERNAL (olddecl))
2927 return NULL;
2929 /* Static data member declared outside a class definition
2930 if the variable is defined within the class with constexpr
2931 specifier is declaration rather than definition (and
2932 deprecated). */
2933 if (cxx_dialect >= cxx17
2934 && VAR_P (olddecl)
2935 && DECL_CLASS_SCOPE_P (olddecl)
2936 && DECL_DECLARED_CONSTEXPR_P (olddecl)
2937 && !DECL_INITIAL (newdecl))
2939 DECL_EXTERNAL (newdecl) = 1;
2940 /* For now, only warn with explicit -Wdeprecated. */
2941 if (global_options_set.x_warn_deprecated
2942 && warning_at (DECL_SOURCE_LOCATION (newdecl), OPT_Wdeprecated,
2943 "redundant redeclaration of %<constexpr%> static "
2944 "data member %qD", newdecl))
2945 inform (DECL_SOURCE_LOCATION (olddecl),
2946 "previous declaration of %qD", olddecl);
2947 return NULL;
2950 /* Reject two definitions. */
2951 return G_("redefinition of %q#D");
2953 else
2955 /* Objects declared with block scope: */
2956 /* Reject two definitions, and reject a definition
2957 together with an external reference. */
2958 if (!(DECL_EXTERNAL (newdecl) && DECL_EXTERNAL (olddecl)))
2959 return G_("redeclaration of %q#D");
2960 return NULL;
2965 /* Hash and equality functions for the named_label table. */
2967 hashval_t
2968 named_label_hash::hash (const value_type entry)
2970 return IDENTIFIER_HASH_VALUE (entry->name);
2973 bool
2974 named_label_hash::equal (const value_type entry, compare_type name)
2976 return name == entry->name;
2979 /* Look for a label named ID in the current function. If one cannot
2980 be found, create one. Return the named_label_entry, or NULL on
2981 failure. */
2983 static named_label_entry *
2984 lookup_label_1 (tree id, bool making_local_p)
2986 /* You can't use labels at global scope. */
2987 if (current_function_decl == NULL_TREE)
2989 error ("label %qE referenced outside of any function", id);
2990 return NULL;
2993 if (!named_labels)
2994 named_labels = hash_table<named_label_hash>::create_ggc (13);
2996 hashval_t hash = IDENTIFIER_HASH_VALUE (id);
2997 named_label_entry **slot
2998 = named_labels->find_slot_with_hash (id, hash, INSERT);
2999 named_label_entry *old = *slot;
3001 if (old && old->label_decl)
3003 if (!making_local_p)
3004 return old;
3006 if (old->binding_level == current_binding_level)
3008 error ("local label %qE conflicts with existing label", id);
3009 inform (DECL_SOURCE_LOCATION (old->label_decl), "previous label");
3010 return NULL;
3014 /* We are making a new decl, create or reuse the named_label_entry */
3015 named_label_entry *ent = NULL;
3016 if (old && !old->label_decl)
3017 ent = old;
3018 else
3020 ent = ggc_cleared_alloc<named_label_entry> ();
3021 ent->name = id;
3022 ent->outer = old;
3023 *slot = ent;
3026 /* Now create the LABEL_DECL. */
3027 tree decl = build_decl (input_location, LABEL_DECL, id, void_type_node);
3029 DECL_CONTEXT (decl) = current_function_decl;
3030 SET_DECL_MODE (decl, VOIDmode);
3031 if (making_local_p)
3033 C_DECLARED_LABEL_FLAG (decl) = true;
3034 DECL_CHAIN (decl) = current_binding_level->names;
3035 current_binding_level->names = decl;
3038 ent->label_decl = decl;
3040 return ent;
3043 /* Wrapper for lookup_label_1. */
3045 tree
3046 lookup_label (tree id)
3048 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
3049 named_label_entry *ent = lookup_label_1 (id, false);
3050 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3051 return ent ? ent->label_decl : NULL_TREE;
3054 tree
3055 declare_local_label (tree id)
3057 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
3058 named_label_entry *ent = lookup_label_1 (id, true);
3059 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3060 return ent ? ent->label_decl : NULL_TREE;
3063 /* Returns nonzero if it is ill-formed to jump past the declaration of
3064 DECL. Returns 2 if it's also a real problem. */
3066 static int
3067 decl_jump_unsafe (tree decl)
3069 /* [stmt.dcl]/3: A program that jumps from a point where a local variable
3070 with automatic storage duration is not in scope to a point where it is
3071 in scope is ill-formed unless the variable has scalar type, class type
3072 with a trivial default constructor and a trivial destructor, a
3073 cv-qualified version of one of these types, or an array of one of the
3074 preceding types and is declared without an initializer (8.5). */
3075 tree type = TREE_TYPE (decl);
3077 if (!VAR_P (decl) || TREE_STATIC (decl)
3078 || type == error_mark_node)
3079 return 0;
3081 if (DECL_NONTRIVIALLY_INITIALIZED_P (decl)
3082 || variably_modified_type_p (type, NULL_TREE))
3083 return 2;
3085 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
3086 return 1;
3088 return 0;
3091 /* A subroutine of check_previous_goto_1 and check_goto to identify a branch
3092 to the user. */
3094 static bool
3095 identify_goto (tree decl, location_t loc, const location_t *locus,
3096 diagnostic_t diag_kind)
3098 bool complained
3099 = emit_diagnostic (diag_kind, loc, 0,
3100 decl ? N_("jump to label %qD")
3101 : N_("jump to case label"), decl);
3102 if (complained && locus)
3103 inform (*locus, " from here");
3104 return complained;
3107 /* Check that a single previously seen jump to a newly defined label
3108 is OK. DECL is the LABEL_DECL or 0; LEVEL is the binding_level for
3109 the jump context; NAMES are the names in scope in LEVEL at the jump
3110 context; LOCUS is the source position of the jump or 0. Returns
3111 true if all is well. */
3113 static bool
3114 check_previous_goto_1 (tree decl, cp_binding_level* level, tree names,
3115 bool exited_omp, const location_t *locus)
3117 cp_binding_level *b;
3118 bool complained = false;
3119 int identified = 0;
3120 bool saw_eh = false, saw_omp = false, saw_tm = false, saw_cxif = false;
3122 if (exited_omp)
3124 complained = identify_goto (decl, input_location, locus, DK_ERROR);
3125 if (complained)
3126 inform (input_location, " exits OpenMP structured block");
3127 saw_omp = true;
3128 identified = 2;
3131 for (b = current_binding_level; b ; b = b->level_chain)
3133 tree new_decls, old_decls = (b == level ? names : NULL_TREE);
3135 for (new_decls = b->names; new_decls != old_decls;
3136 new_decls = (DECL_P (new_decls) ? DECL_CHAIN (new_decls)
3137 : TREE_CHAIN (new_decls)))
3139 int problem = decl_jump_unsafe (new_decls);
3140 if (! problem)
3141 continue;
3143 if (!identified)
3145 complained = identify_goto (decl, input_location, locus,
3146 DK_PERMERROR);
3147 identified = 1;
3149 if (complained)
3151 if (problem > 1)
3152 inform (DECL_SOURCE_LOCATION (new_decls),
3153 " crosses initialization of %q#D", new_decls);
3154 else
3155 inform (DECL_SOURCE_LOCATION (new_decls),
3156 " enters scope of %q#D, which has "
3157 "non-trivial destructor", new_decls);
3161 if (b == level)
3162 break;
3164 const char *inf = NULL;
3165 location_t loc = input_location;
3166 switch (b->kind)
3168 case sk_try:
3169 if (!saw_eh)
3170 inf = N_("enters try block");
3171 saw_eh = true;
3172 break;
3174 case sk_catch:
3175 if (!saw_eh)
3176 inf = N_("enters catch block");
3177 saw_eh = true;
3178 break;
3180 case sk_omp:
3181 if (!saw_omp)
3182 inf = N_("enters OpenMP structured block");
3183 saw_omp = true;
3184 break;
3186 case sk_transaction:
3187 if (!saw_tm)
3188 inf = N_("enters synchronized or atomic statement");
3189 saw_tm = true;
3190 break;
3192 case sk_block:
3193 if (!saw_cxif && level_for_constexpr_if (b->level_chain))
3195 inf = N_("enters constexpr if statement");
3196 loc = EXPR_LOCATION (b->level_chain->this_entity);
3197 saw_cxif = true;
3199 break;
3201 default:
3202 break;
3205 if (inf)
3207 if (identified < 2)
3208 complained = identify_goto (decl, input_location, locus, DK_ERROR);
3209 identified = 2;
3210 if (complained)
3211 inform (loc, " %s", inf);
3215 return !identified;
3218 static void
3219 check_previous_goto (tree decl, struct named_label_use_entry *use)
3221 check_previous_goto_1 (decl, use->binding_level,
3222 use->names_in_scope, use->in_omp_scope,
3223 &use->o_goto_locus);
3226 static bool
3227 check_switch_goto (cp_binding_level* level)
3229 return check_previous_goto_1 (NULL_TREE, level, level->names, false, NULL);
3232 /* Check that a new jump to a label DECL is OK. Called by
3233 finish_goto_stmt. */
3235 void
3236 check_goto (tree decl)
3238 /* We can't know where a computed goto is jumping.
3239 So we assume that it's OK. */
3240 if (TREE_CODE (decl) != LABEL_DECL)
3241 return;
3243 /* We didn't record any information about this label when we created it,
3244 and there's not much point since it's trivial to analyze as a return. */
3245 if (decl == cdtor_label)
3246 return;
3248 hashval_t hash = IDENTIFIER_HASH_VALUE (DECL_NAME (decl));
3249 named_label_entry **slot
3250 = named_labels->find_slot_with_hash (DECL_NAME (decl), hash, NO_INSERT);
3251 named_label_entry *ent = *slot;
3253 /* If the label hasn't been defined yet, defer checking. */
3254 if (! DECL_INITIAL (decl))
3256 /* Don't bother creating another use if the last goto had the
3257 same data, and will therefore create the same set of errors. */
3258 if (ent->uses
3259 && ent->uses->names_in_scope == current_binding_level->names)
3260 return;
3262 named_label_use_entry *new_use
3263 = ggc_alloc<named_label_use_entry> ();
3264 new_use->binding_level = current_binding_level;
3265 new_use->names_in_scope = current_binding_level->names;
3266 new_use->o_goto_locus = input_location;
3267 new_use->in_omp_scope = false;
3269 new_use->next = ent->uses;
3270 ent->uses = new_use;
3271 return;
3274 bool saw_catch = false, complained = false;
3275 int identified = 0;
3276 tree bad;
3277 unsigned ix;
3279 if (ent->in_try_scope || ent->in_catch_scope || ent->in_transaction_scope
3280 || ent->in_constexpr_if
3281 || ent->in_omp_scope || !vec_safe_is_empty (ent->bad_decls))
3283 diagnostic_t diag_kind = DK_PERMERROR;
3284 if (ent->in_try_scope || ent->in_catch_scope || ent->in_constexpr_if
3285 || ent->in_transaction_scope || ent->in_omp_scope)
3286 diag_kind = DK_ERROR;
3287 complained = identify_goto (decl, DECL_SOURCE_LOCATION (decl),
3288 &input_location, diag_kind);
3289 identified = 1 + (diag_kind == DK_ERROR);
3292 FOR_EACH_VEC_SAFE_ELT (ent->bad_decls, ix, bad)
3294 int u = decl_jump_unsafe (bad);
3296 if (u > 1 && DECL_ARTIFICIAL (bad))
3298 /* Can't skip init of __exception_info. */
3299 if (identified == 1)
3301 complained = identify_goto (decl, DECL_SOURCE_LOCATION (decl),
3302 &input_location, DK_ERROR);
3303 identified = 2;
3305 if (complained)
3306 inform (DECL_SOURCE_LOCATION (bad), " enters catch block");
3307 saw_catch = true;
3309 else if (complained)
3311 if (u > 1)
3312 inform (DECL_SOURCE_LOCATION (bad),
3313 " skips initialization of %q#D", bad);
3314 else
3315 inform (DECL_SOURCE_LOCATION (bad),
3316 " enters scope of %q#D which has "
3317 "non-trivial destructor", bad);
3321 if (complained)
3323 if (ent->in_try_scope)
3324 inform (input_location, " enters try block");
3325 else if (ent->in_catch_scope && !saw_catch)
3326 inform (input_location, " enters catch block");
3327 else if (ent->in_transaction_scope)
3328 inform (input_location, " enters synchronized or atomic statement");
3329 else if (ent->in_constexpr_if)
3330 inform (input_location, " enters %<constexpr%> if statement");
3333 if (ent->in_omp_scope)
3335 if (complained)
3336 inform (input_location, " enters OpenMP structured block");
3338 else if (flag_openmp)
3339 for (cp_binding_level *b = current_binding_level; b ; b = b->level_chain)
3341 if (b == ent->binding_level)
3342 break;
3343 if (b->kind == sk_omp)
3345 if (identified < 2)
3347 complained = identify_goto (decl,
3348 DECL_SOURCE_LOCATION (decl),
3349 &input_location, DK_ERROR);
3350 identified = 2;
3352 if (complained)
3353 inform (input_location, " exits OpenMP structured block");
3354 break;
3359 /* Check that a return is ok wrt OpenMP structured blocks.
3360 Called by finish_return_stmt. Returns true if all is well. */
3362 bool
3363 check_omp_return (void)
3365 for (cp_binding_level *b = current_binding_level; b ; b = b->level_chain)
3366 if (b->kind == sk_omp)
3368 error ("invalid exit from OpenMP structured block");
3369 return false;
3371 else if (b->kind == sk_function_parms)
3372 break;
3373 return true;
3376 /* Define a label, specifying the location in the source file.
3377 Return the LABEL_DECL node for the label. */
3379 static tree
3380 define_label_1 (location_t location, tree name)
3382 /* After labels, make any new cleanups in the function go into their
3383 own new (temporary) binding contour. */
3384 for (cp_binding_level *p = current_binding_level;
3385 p->kind != sk_function_parms;
3386 p = p->level_chain)
3387 p->more_cleanups_ok = 0;
3389 named_label_entry *ent = lookup_label_1 (name, false);
3390 tree decl = ent->label_decl;
3392 if (DECL_INITIAL (decl) != NULL_TREE)
3394 error ("duplicate label %qD", decl);
3395 return error_mark_node;
3397 else
3399 /* Mark label as having been defined. */
3400 DECL_INITIAL (decl) = error_mark_node;
3401 /* Say where in the source. */
3402 DECL_SOURCE_LOCATION (decl) = location;
3404 ent->binding_level = current_binding_level;
3405 ent->names_in_scope = current_binding_level->names;
3407 for (named_label_use_entry *use = ent->uses; use; use = use->next)
3408 check_previous_goto (decl, use);
3409 ent->uses = NULL;
3412 return decl;
3415 /* Wrapper for define_label_1. */
3417 tree
3418 define_label (location_t location, tree name)
3420 bool running = timevar_cond_start (TV_NAME_LOOKUP);
3421 tree ret = define_label_1 (location, name);
3422 timevar_cond_stop (TV_NAME_LOOKUP, running);
3423 return ret;
3427 struct cp_switch
3429 cp_binding_level *level;
3430 struct cp_switch *next;
3431 /* The SWITCH_STMT being built. */
3432 tree switch_stmt;
3433 /* A splay-tree mapping the low element of a case range to the high
3434 element, or NULL_TREE if there is no high element. Used to
3435 determine whether or not a new case label duplicates an old case
3436 label. We need a tree, rather than simply a hash table, because
3437 of the GNU case range extension. */
3438 splay_tree cases;
3439 /* Remember whether there was a case value that is outside the
3440 range of the original type of the controlling expression. */
3441 bool outside_range_p;
3442 /* Remember whether a default: case label has been seen. */
3443 bool has_default_p;
3444 /* Remember whether a BREAK_STMT has been seen in this SWITCH_STMT. */
3445 bool break_stmt_seen_p;
3446 /* Set if inside of {FOR,DO,WHILE}_BODY nested inside of a switch,
3447 where BREAK_STMT doesn't belong to the SWITCH_STMT. */
3448 bool in_loop_body_p;
3451 /* A stack of the currently active switch statements. The innermost
3452 switch statement is on the top of the stack. There is no need to
3453 mark the stack for garbage collection because it is only active
3454 during the processing of the body of a function, and we never
3455 collect at that point. */
3457 static struct cp_switch *switch_stack;
3459 /* Called right after a switch-statement condition is parsed.
3460 SWITCH_STMT is the switch statement being parsed. */
3462 void
3463 push_switch (tree switch_stmt)
3465 struct cp_switch *p = XNEW (struct cp_switch);
3466 p->level = current_binding_level;
3467 p->next = switch_stack;
3468 p->switch_stmt = switch_stmt;
3469 p->cases = splay_tree_new (case_compare, NULL, NULL);
3470 p->outside_range_p = false;
3471 p->has_default_p = false;
3472 p->break_stmt_seen_p = false;
3473 p->in_loop_body_p = false;
3474 switch_stack = p;
3477 void
3478 pop_switch (void)
3480 struct cp_switch *cs = switch_stack;
3481 location_t switch_location;
3483 /* Emit warnings as needed. */
3484 switch_location = cp_expr_loc_or_loc (cs->switch_stmt, input_location);
3485 const bool bool_cond_p
3486 = (SWITCH_STMT_TYPE (cs->switch_stmt)
3487 && TREE_CODE (SWITCH_STMT_TYPE (cs->switch_stmt)) == BOOLEAN_TYPE);
3488 if (!processing_template_decl)
3489 c_do_switch_warnings (cs->cases, switch_location,
3490 SWITCH_STMT_TYPE (cs->switch_stmt),
3491 SWITCH_STMT_COND (cs->switch_stmt),
3492 bool_cond_p, cs->outside_range_p);
3494 /* For the benefit of block_may_fallthru remember if the switch body
3495 case labels cover all possible values and if there are break; stmts. */
3496 if (cs->has_default_p
3497 || (!processing_template_decl
3498 && c_switch_covers_all_cases_p (cs->cases,
3499 SWITCH_STMT_TYPE (cs->switch_stmt))))
3500 SWITCH_STMT_ALL_CASES_P (cs->switch_stmt) = 1;
3501 if (!cs->break_stmt_seen_p)
3502 SWITCH_STMT_NO_BREAK_P (cs->switch_stmt) = 1;
3503 gcc_assert (!cs->in_loop_body_p);
3504 splay_tree_delete (cs->cases);
3505 switch_stack = switch_stack->next;
3506 free (cs);
3509 /* Note that a BREAK_STMT is about to be added. If it is inside of
3510 a SWITCH_STMT and not inside of a loop body inside of it, note
3511 in switch_stack we've seen a BREAK_STMT. */
3513 void
3514 note_break_stmt (void)
3516 if (switch_stack && !switch_stack->in_loop_body_p)
3517 switch_stack->break_stmt_seen_p = true;
3520 /* Note the start of processing of an iteration statement's body.
3521 The note_break_stmt function will do nothing while processing it.
3522 Return a flag that should be passed to note_iteration_stmt_body_end. */
3524 bool
3525 note_iteration_stmt_body_start (void)
3527 if (!switch_stack)
3528 return false;
3529 bool ret = switch_stack->in_loop_body_p;
3530 switch_stack->in_loop_body_p = true;
3531 return ret;
3534 /* Note the end of processing of an iteration statement's body. */
3536 void
3537 note_iteration_stmt_body_end (bool prev)
3539 if (switch_stack)
3540 switch_stack->in_loop_body_p = prev;
3543 /* Convert a case constant VALUE in a switch to the type TYPE of the switch
3544 condition. Note that if TYPE and VALUE are already integral we don't
3545 really do the conversion because the language-independent
3546 warning/optimization code will work better that way. */
3548 static tree
3549 case_conversion (tree type, tree value)
3551 if (value == NULL_TREE)
3552 return value;
3554 value = mark_rvalue_use (value);
3556 if (cxx_dialect >= cxx11
3557 && (SCOPED_ENUM_P (type)
3558 || !INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (value))))
3560 if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type))
3561 type = type_promotes_to (type);
3562 value = (perform_implicit_conversion_flags
3563 (type, value, tf_warning_or_error,
3564 LOOKUP_IMPLICIT | LOOKUP_NO_NON_INTEGRAL));
3566 return cxx_constant_value (value);
3569 /* Note that we've seen a definition of a case label, and complain if this
3570 is a bad place for one. */
3572 tree
3573 finish_case_label (location_t loc, tree low_value, tree high_value)
3575 tree cond, r;
3576 cp_binding_level *p;
3577 tree type;
3579 if (low_value == NULL_TREE && high_value == NULL_TREE)
3580 switch_stack->has_default_p = true;
3582 if (processing_template_decl)
3584 tree label;
3586 /* For templates, just add the case label; we'll do semantic
3587 analysis at instantiation-time. */
3588 label = build_decl (loc, LABEL_DECL, NULL_TREE, NULL_TREE);
3589 return add_stmt (build_case_label (low_value, high_value, label));
3592 /* Find the condition on which this switch statement depends. */
3593 cond = SWITCH_STMT_COND (switch_stack->switch_stmt);
3594 if (cond && TREE_CODE (cond) == TREE_LIST)
3595 cond = TREE_VALUE (cond);
3597 if (!check_switch_goto (switch_stack->level))
3598 return error_mark_node;
3600 type = SWITCH_STMT_TYPE (switch_stack->switch_stmt);
3602 low_value = case_conversion (type, low_value);
3603 high_value = case_conversion (type, high_value);
3605 r = c_add_case_label (loc, switch_stack->cases, cond, type,
3606 low_value, high_value,
3607 &switch_stack->outside_range_p);
3609 /* After labels, make any new cleanups in the function go into their
3610 own new (temporary) binding contour. */
3611 for (p = current_binding_level;
3612 p->kind != sk_function_parms;
3613 p = p->level_chain)
3614 p->more_cleanups_ok = 0;
3616 return r;
3619 struct typename_info {
3620 tree scope;
3621 tree name;
3622 tree template_id;
3623 bool enum_p;
3624 bool class_p;
3627 struct typename_hasher : ggc_ptr_hash<tree_node>
3629 typedef typename_info *compare_type;
3631 /* Hash a TYPENAME_TYPE. */
3633 static hashval_t
3634 hash (tree t)
3636 hashval_t hash;
3638 hash = (htab_hash_pointer (TYPE_CONTEXT (t))
3639 ^ htab_hash_pointer (TYPE_IDENTIFIER (t)));
3641 return hash;
3644 /* Compare two TYPENAME_TYPEs. */
3646 static bool
3647 equal (tree t1, const typename_info *t2)
3649 return (TYPE_IDENTIFIER (t1) == t2->name
3650 && TYPE_CONTEXT (t1) == t2->scope
3651 && TYPENAME_TYPE_FULLNAME (t1) == t2->template_id
3652 && TYPENAME_IS_ENUM_P (t1) == t2->enum_p
3653 && TYPENAME_IS_CLASS_P (t1) == t2->class_p);
3657 /* Build a TYPENAME_TYPE. If the type is `typename T::t', CONTEXT is
3658 the type of `T', NAME is the IDENTIFIER_NODE for `t'.
3660 Returns the new TYPENAME_TYPE. */
3662 static GTY (()) hash_table<typename_hasher> *typename_htab;
3664 tree
3665 build_typename_type (tree context, tree name, tree fullname,
3666 enum tag_types tag_type)
3668 tree t;
3669 tree d;
3670 typename_info ti;
3671 tree *e;
3672 hashval_t hash;
3674 if (typename_htab == NULL)
3675 typename_htab = hash_table<typename_hasher>::create_ggc (61);
3677 ti.scope = FROB_CONTEXT (context);
3678 ti.name = name;
3679 ti.template_id = fullname;
3680 ti.enum_p = tag_type == enum_type;
3681 ti.class_p = (tag_type == class_type
3682 || tag_type == record_type
3683 || tag_type == union_type);
3684 hash = (htab_hash_pointer (ti.scope)
3685 ^ htab_hash_pointer (ti.name));
3687 /* See if we already have this type. */
3688 e = typename_htab->find_slot_with_hash (&ti, hash, INSERT);
3689 if (*e)
3690 t = *e;
3691 else
3693 /* Build the TYPENAME_TYPE. */
3694 t = cxx_make_type (TYPENAME_TYPE);
3695 TYPE_CONTEXT (t) = ti.scope;
3696 TYPENAME_TYPE_FULLNAME (t) = ti.template_id;
3697 TYPENAME_IS_ENUM_P (t) = ti.enum_p;
3698 TYPENAME_IS_CLASS_P (t) = ti.class_p;
3700 /* Build the corresponding TYPE_DECL. */
3701 d = build_decl (input_location, TYPE_DECL, name, t);
3702 TYPE_NAME (TREE_TYPE (d)) = d;
3703 TYPE_STUB_DECL (TREE_TYPE (d)) = d;
3704 DECL_CONTEXT (d) = FROB_CONTEXT (context);
3705 DECL_ARTIFICIAL (d) = 1;
3707 /* Store it in the hash table. */
3708 *e = t;
3710 /* TYPENAME_TYPEs must always be compared structurally, because
3711 they may or may not resolve down to another type depending on
3712 the currently open classes. */
3713 SET_TYPE_STRUCTURAL_EQUALITY (t);
3716 return t;
3719 /* Resolve `typename CONTEXT::NAME'. TAG_TYPE indicates the tag
3720 provided to name the type. Returns an appropriate type, unless an
3721 error occurs, in which case error_mark_node is returned. If we
3722 locate a non-artificial TYPE_DECL and TF_KEEP_TYPE_DECL is set, we
3723 return that, rather than the _TYPE it corresponds to, in other
3724 cases we look through the type decl. If TF_ERROR is set, complain
3725 about errors, otherwise be quiet. */
3727 tree
3728 make_typename_type (tree context, tree name, enum tag_types tag_type,
3729 tsubst_flags_t complain)
3731 tree fullname;
3732 tree t;
3733 bool want_template;
3735 if (name == error_mark_node
3736 || context == NULL_TREE
3737 || context == error_mark_node)
3738 return error_mark_node;
3740 if (TYPE_P (name))
3742 if (!(TYPE_LANG_SPECIFIC (name)
3743 && (CLASSTYPE_IS_TEMPLATE (name)
3744 || CLASSTYPE_USE_TEMPLATE (name))))
3745 name = TYPE_IDENTIFIER (name);
3746 else
3747 /* Create a TEMPLATE_ID_EXPR for the type. */
3748 name = build_nt (TEMPLATE_ID_EXPR,
3749 CLASSTYPE_TI_TEMPLATE (name),
3750 CLASSTYPE_TI_ARGS (name));
3752 else if (TREE_CODE (name) == TYPE_DECL)
3753 name = DECL_NAME (name);
3755 fullname = name;
3757 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
3759 name = TREE_OPERAND (name, 0);
3760 if (DECL_TYPE_TEMPLATE_P (name))
3761 name = TREE_OPERAND (fullname, 0) = DECL_NAME (name);
3762 if (TREE_CODE (name) != IDENTIFIER_NODE)
3764 if (complain & tf_error)
3765 error ("%qD is not a type", name);
3766 return error_mark_node;
3769 if (TREE_CODE (name) == TEMPLATE_DECL)
3771 if (complain & tf_error)
3772 error ("%qD used without template arguments", name);
3773 return error_mark_node;
3775 gcc_assert (identifier_p (name));
3776 gcc_assert (TYPE_P (context));
3778 if (!MAYBE_CLASS_TYPE_P (context))
3780 if (complain & tf_error)
3781 error ("%q#T is not a class", context);
3782 return error_mark_node;
3785 /* When the CONTEXT is a dependent type, NAME could refer to a
3786 dependent base class of CONTEXT. But look inside it anyway
3787 if CONTEXT is a currently open scope, in case it refers to a
3788 member of the current instantiation or a non-dependent base;
3789 lookup will stop when we hit a dependent base. */
3790 if (!dependent_scope_p (context))
3791 /* We should only set WANT_TYPE when we're a nested typename type.
3792 Then we can give better diagnostics if we find a non-type. */
3793 t = lookup_field (context, name, 2, /*want_type=*/true);
3794 else
3795 t = NULL_TREE;
3797 if ((!t || TREE_CODE (t) == TREE_LIST) && dependent_type_p (context))
3798 return build_typename_type (context, name, fullname, tag_type);
3800 want_template = TREE_CODE (fullname) == TEMPLATE_ID_EXPR;
3802 if (!t)
3804 if (complain & tf_error)
3806 if (!COMPLETE_TYPE_P (context))
3807 cxx_incomplete_type_error (NULL_TREE, context);
3808 else
3809 error (want_template ? G_("no class template named %q#T in %q#T")
3810 : G_("no type named %q#T in %q#T"), name, context);
3812 return error_mark_node;
3815 /* Pull out the template from an injected-class-name (or multiple). */
3816 if (want_template)
3817 t = maybe_get_template_decl_from_type_decl (t);
3819 if (TREE_CODE (t) == TREE_LIST)
3821 if (complain & tf_error)
3823 error ("lookup of %qT in %qT is ambiguous", name, context);
3824 print_candidates (t);
3826 return error_mark_node;
3829 if (want_template && !DECL_TYPE_TEMPLATE_P (t))
3831 if (complain & tf_error)
3832 error ("%<typename %T::%D%> names %q#T, which is not a class template",
3833 context, name, t);
3834 return error_mark_node;
3836 if (!want_template && TREE_CODE (t) != TYPE_DECL)
3838 if (complain & tf_error)
3839 error ("%<typename %T::%D%> names %q#T, which is not a type",
3840 context, name, t);
3841 return error_mark_node;
3844 if (!perform_or_defer_access_check (TYPE_BINFO (context), t, t, complain))
3845 return error_mark_node;
3847 /* If we are currently parsing a template and if T is a typedef accessed
3848 through CONTEXT then we need to remember and check access of T at
3849 template instantiation time. */
3850 add_typedef_to_current_template_for_access_check (t, context, input_location);
3852 if (want_template)
3853 return lookup_template_class (t, TREE_OPERAND (fullname, 1),
3854 NULL_TREE, context,
3855 /*entering_scope=*/0,
3856 complain | tf_user);
3858 if (DECL_ARTIFICIAL (t) || !(complain & tf_keep_type_decl))
3859 t = TREE_TYPE (t);
3861 maybe_record_typedef_use (t);
3863 return t;
3866 /* Resolve `CONTEXT::template NAME'. Returns a TEMPLATE_DECL if the name
3867 can be resolved or an UNBOUND_CLASS_TEMPLATE, unless an error occurs,
3868 in which case error_mark_node is returned.
3870 If PARM_LIST is non-NULL, also make sure that the template parameter
3871 list of TEMPLATE_DECL matches.
3873 If COMPLAIN zero, don't complain about any errors that occur. */
3875 tree
3876 make_unbound_class_template (tree context, tree name, tree parm_list,
3877 tsubst_flags_t complain)
3879 tree t;
3880 tree d;
3882 if (TYPE_P (name))
3883 name = TYPE_IDENTIFIER (name);
3884 else if (DECL_P (name))
3885 name = DECL_NAME (name);
3886 gcc_assert (identifier_p (name));
3888 if (!dependent_type_p (context)
3889 || currently_open_class (context))
3891 tree tmpl = NULL_TREE;
3893 if (MAYBE_CLASS_TYPE_P (context))
3894 tmpl = lookup_field (context, name, 0, false);
3896 if (tmpl && TREE_CODE (tmpl) == TYPE_DECL)
3897 tmpl = maybe_get_template_decl_from_type_decl (tmpl);
3899 if (!tmpl || !DECL_TYPE_TEMPLATE_P (tmpl))
3901 if (complain & tf_error)
3902 error ("no class template named %q#T in %q#T", name, context);
3903 return error_mark_node;
3906 if (parm_list
3907 && !comp_template_parms (DECL_TEMPLATE_PARMS (tmpl), parm_list))
3909 if (complain & tf_error)
3911 error ("template parameters do not match template %qD", tmpl);
3912 inform (DECL_SOURCE_LOCATION (tmpl),
3913 "%qD declared here", tmpl);
3915 return error_mark_node;
3918 if (!perform_or_defer_access_check (TYPE_BINFO (context), tmpl, tmpl,
3919 complain))
3920 return error_mark_node;
3922 return tmpl;
3925 /* Build the UNBOUND_CLASS_TEMPLATE. */
3926 t = cxx_make_type (UNBOUND_CLASS_TEMPLATE);
3927 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
3928 TREE_TYPE (t) = NULL_TREE;
3929 SET_TYPE_STRUCTURAL_EQUALITY (t);
3931 /* Build the corresponding TEMPLATE_DECL. */
3932 d = build_decl (input_location, TEMPLATE_DECL, name, t);
3933 TYPE_NAME (TREE_TYPE (d)) = d;
3934 TYPE_STUB_DECL (TREE_TYPE (d)) = d;
3935 DECL_CONTEXT (d) = FROB_CONTEXT (context);
3936 DECL_ARTIFICIAL (d) = 1;
3937 DECL_TEMPLATE_PARMS (d) = parm_list;
3939 return t;
3944 /* Push the declarations of builtin types into the global namespace.
3945 RID_INDEX is the index of the builtin type in the array
3946 RID_POINTERS. NAME is the name used when looking up the builtin
3947 type. TYPE is the _TYPE node for the builtin type.
3949 The calls to set_global_binding below should be
3950 eliminated. Built-in types should not be looked up name; their
3951 names are keywords that the parser can recognize. However, there
3952 is code in c-common.c that uses identifier_global_value to look up
3953 built-in types by name. */
3955 void
3956 record_builtin_type (enum rid rid_index,
3957 const char* name,
3958 tree type)
3960 tree decl = NULL_TREE;
3962 if (name)
3964 tree tname = get_identifier (name);
3965 tree tdecl = build_decl (BUILTINS_LOCATION, TYPE_DECL, tname, type);
3966 DECL_ARTIFICIAL (tdecl) = 1;
3967 set_global_binding (tdecl);
3968 decl = tdecl;
3971 if ((int) rid_index < (int) RID_MAX)
3972 if (tree rname = ridpointers[(int) rid_index])
3973 if (!decl || DECL_NAME (decl) != rname)
3975 tree rdecl = build_decl (BUILTINS_LOCATION, TYPE_DECL, rname, type);
3976 DECL_ARTIFICIAL (rdecl) = 1;
3977 set_global_binding (rdecl);
3978 if (!decl)
3979 decl = rdecl;
3982 if (decl)
3984 if (!TYPE_NAME (type))
3985 TYPE_NAME (type) = decl;
3986 debug_hooks->type_decl (decl, 0);
3990 /* Push a type into the namespace so that the back ends ignore it. */
3992 static void
3993 record_unknown_type (tree type, const char* name)
3995 tree decl = pushdecl (build_decl (UNKNOWN_LOCATION,
3996 TYPE_DECL, get_identifier (name), type));
3997 /* Make sure the "unknown type" typedecl gets ignored for debug info. */
3998 DECL_IGNORED_P (decl) = 1;
3999 TYPE_DECL_SUPPRESS_DEBUG (decl) = 1;
4000 TYPE_SIZE (type) = TYPE_SIZE (void_type_node);
4001 SET_TYPE_ALIGN (type, 1);
4002 TYPE_USER_ALIGN (type) = 0;
4003 SET_TYPE_MODE (type, TYPE_MODE (void_type_node));
4006 /* Create all the predefined identifiers. */
4008 static void
4009 initialize_predefined_identifiers (void)
4011 struct predefined_identifier
4013 const char *name; /* Name. */
4014 tree *node; /* Node to store it in. */
4015 cp_identifier_kind kind; /* Kind of identifier. */
4018 /* A table of identifiers to create at startup. */
4019 static const predefined_identifier predefined_identifiers[] = {
4020 {"C++", &lang_name_cplusplus, cik_normal},
4021 {"C", &lang_name_c, cik_normal},
4022 /* Some of these names have a trailing space so that it is
4023 impossible for them to conflict with names written by users. */
4024 {"__ct ", &ctor_identifier, cik_ctor},
4025 {"__ct_base ", &base_ctor_identifier, cik_ctor},
4026 {"__ct_comp ", &complete_ctor_identifier, cik_ctor},
4027 {"__dt ", &dtor_identifier, cik_dtor},
4028 {"__dt_base ", &base_dtor_identifier, cik_dtor},
4029 {"__dt_comp ", &complete_dtor_identifier, cik_dtor},
4030 {"__dt_del ", &deleting_dtor_identifier, cik_dtor},
4031 {"__conv_op ", &conv_op_identifier, cik_conv_op},
4032 {"__in_chrg", &in_charge_identifier, cik_normal},
4033 {"this", &this_identifier, cik_normal},
4034 {"__delta", &delta_identifier, cik_normal},
4035 {"__pfn", &pfn_identifier, cik_normal},
4036 {"_vptr", &vptr_identifier, cik_normal},
4037 {"__vtt_parm", &vtt_parm_identifier, cik_normal},
4038 {"::", &global_identifier, cik_normal},
4039 {"std", &std_identifier, cik_normal},
4040 /* The demangler expects anonymous namespaces to be called
4041 something starting with '_GLOBAL__N_'. It no longer needs
4042 to be unique to the TU. */
4043 {"_GLOBAL__N_1", &anon_identifier, cik_normal},
4044 {"auto", &auto_identifier, cik_normal},
4045 {"decltype(auto)", &decltype_auto_identifier, cik_normal},
4046 {"initializer_list", &init_list_identifier, cik_normal},
4047 {NULL, NULL, cik_normal}
4050 for (const predefined_identifier *pid = predefined_identifiers;
4051 pid->name; ++pid)
4053 *pid->node = get_identifier (pid->name);
4054 /* Some of these identifiers already have a special kind. */
4055 if (pid->kind != cik_normal)
4056 set_identifier_kind (*pid->node, pid->kind);
4060 /* Create the predefined scalar types of C,
4061 and some nodes representing standard constants (0, 1, (void *)0).
4062 Initialize the global binding level.
4063 Make definitions for built-in primitive functions. */
4065 void
4066 cxx_init_decl_processing (void)
4068 tree void_ftype;
4069 tree void_ftype_ptr;
4071 /* Create all the identifiers we need. */
4072 initialize_predefined_identifiers ();
4074 /* Create the global variables. */
4075 push_to_top_level ();
4077 current_function_decl = NULL_TREE;
4078 current_binding_level = NULL;
4079 /* Enter the global namespace. */
4080 gcc_assert (global_namespace == NULL_TREE);
4081 global_namespace = build_lang_decl (NAMESPACE_DECL, global_identifier,
4082 void_type_node);
4083 TREE_PUBLIC (global_namespace) = 1;
4084 DECL_CONTEXT (global_namespace)
4085 = build_translation_unit_decl (get_identifier (main_input_filename));
4086 /* Remember whether we want the empty class passing ABI change warning
4087 in this TU. */
4088 TRANSLATION_UNIT_WARN_EMPTY_P (DECL_CONTEXT (global_namespace))
4089 = warn_abi && abi_version_crosses (12);
4090 debug_hooks->register_main_translation_unit
4091 (DECL_CONTEXT (global_namespace));
4092 begin_scope (sk_namespace, global_namespace);
4093 current_namespace = global_namespace;
4095 if (flag_visibility_ms_compat)
4096 default_visibility = VISIBILITY_HIDDEN;
4098 /* Initially, C. */
4099 current_lang_name = lang_name_c;
4101 /* Create the `std' namespace. */
4102 push_namespace (std_identifier);
4103 std_node = current_namespace;
4104 pop_namespace ();
4106 flag_noexcept_type = (cxx_dialect >= cxx17);
4108 c_common_nodes_and_builtins ();
4110 integer_two_node = build_int_cst (NULL_TREE, 2);
4112 /* Guess at the initial static decls size. */
4113 vec_alloc (static_decls, 500);
4115 /* ... and keyed classes. */
4116 vec_alloc (keyed_classes, 100);
4118 record_builtin_type (RID_BOOL, "bool", boolean_type_node);
4119 truthvalue_type_node = boolean_type_node;
4120 truthvalue_false_node = boolean_false_node;
4121 truthvalue_true_node = boolean_true_node;
4123 empty_except_spec = build_tree_list (NULL_TREE, NULL_TREE);
4124 noexcept_true_spec = build_tree_list (boolean_true_node, NULL_TREE);
4125 noexcept_false_spec = build_tree_list (boolean_false_node, NULL_TREE);
4126 noexcept_deferred_spec = build_tree_list (make_node (DEFERRED_NOEXCEPT),
4127 NULL_TREE);
4129 #if 0
4130 record_builtin_type (RID_MAX, NULL, string_type_node);
4131 #endif
4133 delta_type_node = ptrdiff_type_node;
4134 vtable_index_type = ptrdiff_type_node;
4136 vtt_parm_type = build_pointer_type (const_ptr_type_node);
4137 void_ftype = build_function_type_list (void_type_node, NULL_TREE);
4138 void_ftype_ptr = build_function_type_list (void_type_node,
4139 ptr_type_node, NULL_TREE);
4140 void_ftype_ptr
4141 = build_exception_variant (void_ftype_ptr, empty_except_spec);
4143 /* Create the conversion operator marker. This operator's DECL_NAME
4144 is in the identifier table, so we can use identifier equality to
4145 find it. */
4146 conv_op_marker = build_lang_decl (FUNCTION_DECL, conv_op_identifier,
4147 void_ftype);
4149 /* C++ extensions */
4151 unknown_type_node = make_node (LANG_TYPE);
4152 record_unknown_type (unknown_type_node, "unknown type");
4154 /* Indirecting an UNKNOWN_TYPE node yields an UNKNOWN_TYPE node. */
4155 TREE_TYPE (unknown_type_node) = unknown_type_node;
4157 /* Looking up TYPE_POINTER_TO and TYPE_REFERENCE_TO yield the same
4158 result. */
4159 TYPE_POINTER_TO (unknown_type_node) = unknown_type_node;
4160 TYPE_REFERENCE_TO (unknown_type_node) = unknown_type_node;
4162 init_list_type_node = make_node (LANG_TYPE);
4163 record_unknown_type (init_list_type_node, "init list");
4166 /* Make sure we get a unique function type, so we can give
4167 its pointer type a name. (This wins for gdb.) */
4168 tree vfunc_type = make_node (FUNCTION_TYPE);
4169 TREE_TYPE (vfunc_type) = integer_type_node;
4170 TYPE_ARG_TYPES (vfunc_type) = NULL_TREE;
4171 layout_type (vfunc_type);
4173 vtable_entry_type = build_pointer_type (vfunc_type);
4175 record_builtin_type (RID_MAX, "__vtbl_ptr_type", vtable_entry_type);
4177 vtbl_type_node
4178 = build_cplus_array_type (vtable_entry_type, NULL_TREE);
4179 layout_type (vtbl_type_node);
4180 vtbl_type_node = cp_build_qualified_type (vtbl_type_node, TYPE_QUAL_CONST);
4181 record_builtin_type (RID_MAX, NULL, vtbl_type_node);
4182 vtbl_ptr_type_node = build_pointer_type (vtable_entry_type);
4183 layout_type (vtbl_ptr_type_node);
4184 record_builtin_type (RID_MAX, NULL, vtbl_ptr_type_node);
4186 push_namespace (get_identifier ("__cxxabiv1"));
4187 abi_node = current_namespace;
4188 pop_namespace ();
4190 global_type_node = make_node (LANG_TYPE);
4191 record_unknown_type (global_type_node, "global type");
4193 any_targ_node = make_node (LANG_TYPE);
4194 record_unknown_type (any_targ_node, "any type");
4196 /* Now, C++. */
4197 current_lang_name = lang_name_cplusplus;
4199 if (aligned_new_threshold > 1
4200 && !pow2p_hwi (aligned_new_threshold))
4202 error ("-faligned-new=%d is not a power of two", aligned_new_threshold);
4203 aligned_new_threshold = 1;
4205 if (aligned_new_threshold == -1)
4206 aligned_new_threshold = (cxx_dialect >= cxx17) ? 1 : 0;
4207 if (aligned_new_threshold == 1)
4208 aligned_new_threshold = malloc_alignment () / BITS_PER_UNIT;
4211 tree newattrs, extvisattr;
4212 tree newtype, deltype;
4213 tree ptr_ftype_sizetype;
4214 tree new_eh_spec;
4216 ptr_ftype_sizetype
4217 = build_function_type_list (ptr_type_node, size_type_node, NULL_TREE);
4218 if (cxx_dialect == cxx98)
4220 tree bad_alloc_id;
4221 tree bad_alloc_type_node;
4222 tree bad_alloc_decl;
4224 push_namespace (std_identifier);
4225 bad_alloc_id = get_identifier ("bad_alloc");
4226 bad_alloc_type_node = make_class_type (RECORD_TYPE);
4227 TYPE_CONTEXT (bad_alloc_type_node) = current_namespace;
4228 bad_alloc_decl
4229 = create_implicit_typedef (bad_alloc_id, bad_alloc_type_node);
4230 DECL_CONTEXT (bad_alloc_decl) = current_namespace;
4231 pop_namespace ();
4233 new_eh_spec
4234 = add_exception_specifier (NULL_TREE, bad_alloc_type_node, -1);
4236 else
4237 new_eh_spec = noexcept_false_spec;
4239 /* Ensure attribs.c is initialized. */
4240 init_attributes ();
4242 /* Ensure constraint.cc is initialized. */
4243 init_constraint_processing ();
4245 extvisattr = build_tree_list (get_identifier ("externally_visible"),
4246 NULL_TREE);
4247 newattrs = tree_cons (get_identifier ("alloc_size"),
4248 build_tree_list (NULL_TREE, integer_one_node),
4249 extvisattr);
4250 newtype = cp_build_type_attribute_variant (ptr_ftype_sizetype, newattrs);
4251 newtype = build_exception_variant (newtype, new_eh_spec);
4252 deltype = cp_build_type_attribute_variant (void_ftype_ptr, extvisattr);
4253 deltype = build_exception_variant (deltype, empty_except_spec);
4254 tree opnew = push_cp_library_fn (NEW_EXPR, newtype, 0);
4255 DECL_IS_MALLOC (opnew) = 1;
4256 DECL_IS_OPERATOR_NEW (opnew) = 1;
4257 opnew = push_cp_library_fn (VEC_NEW_EXPR, newtype, 0);
4258 DECL_IS_MALLOC (opnew) = 1;
4259 DECL_IS_OPERATOR_NEW (opnew) = 1;
4260 push_cp_library_fn (DELETE_EXPR, deltype, ECF_NOTHROW);
4261 push_cp_library_fn (VEC_DELETE_EXPR, deltype, ECF_NOTHROW);
4262 if (flag_sized_deallocation)
4264 /* Also push the sized deallocation variants:
4265 void operator delete(void*, std::size_t) throw();
4266 void operator delete[](void*, std::size_t) throw(); */
4267 tree void_ftype_ptr_size
4268 = build_function_type_list (void_type_node, ptr_type_node,
4269 size_type_node, NULL_TREE);
4270 deltype = cp_build_type_attribute_variant (void_ftype_ptr_size,
4271 extvisattr);
4272 deltype = build_exception_variant (deltype, empty_except_spec);
4273 push_cp_library_fn (DELETE_EXPR, deltype, ECF_NOTHROW);
4274 push_cp_library_fn (VEC_DELETE_EXPR, deltype, ECF_NOTHROW);
4277 if (aligned_new_threshold)
4279 push_namespace (std_identifier);
4280 tree align_id = get_identifier ("align_val_t");
4281 align_type_node = start_enum (align_id, NULL_TREE, size_type_node,
4282 NULL_TREE, /*scoped*/true, NULL);
4283 pop_namespace ();
4285 /* operator new (size_t, align_val_t); */
4286 newtype = build_function_type_list (ptr_type_node, size_type_node,
4287 align_type_node, NULL_TREE);
4288 newtype = cp_build_type_attribute_variant (newtype, newattrs);
4289 newtype = build_exception_variant (newtype, new_eh_spec);
4290 opnew = push_cp_library_fn (NEW_EXPR, newtype, 0);
4291 DECL_IS_MALLOC (opnew) = 1;
4292 DECL_IS_OPERATOR_NEW (opnew) = 1;
4293 opnew = push_cp_library_fn (VEC_NEW_EXPR, newtype, 0);
4294 DECL_IS_MALLOC (opnew) = 1;
4295 DECL_IS_OPERATOR_NEW (opnew) = 1;
4297 /* operator delete (void *, align_val_t); */
4298 deltype = build_function_type_list (void_type_node, ptr_type_node,
4299 align_type_node, NULL_TREE);
4300 deltype = cp_build_type_attribute_variant (deltype, extvisattr);
4301 deltype = build_exception_variant (deltype, empty_except_spec);
4302 push_cp_library_fn (DELETE_EXPR, deltype, ECF_NOTHROW);
4303 push_cp_library_fn (VEC_DELETE_EXPR, deltype, ECF_NOTHROW);
4305 if (flag_sized_deallocation)
4307 /* operator delete (void *, size_t, align_val_t); */
4308 deltype = build_function_type_list (void_type_node, ptr_type_node,
4309 size_type_node, align_type_node,
4310 NULL_TREE);
4311 deltype = cp_build_type_attribute_variant (deltype, extvisattr);
4312 deltype = build_exception_variant (deltype, empty_except_spec);
4313 push_cp_library_fn (DELETE_EXPR, deltype, ECF_NOTHROW);
4314 push_cp_library_fn (VEC_DELETE_EXPR, deltype, ECF_NOTHROW);
4318 nullptr_type_node = make_node (NULLPTR_TYPE);
4319 TYPE_SIZE (nullptr_type_node) = bitsize_int (GET_MODE_BITSIZE (ptr_mode));
4320 TYPE_SIZE_UNIT (nullptr_type_node) = size_int (GET_MODE_SIZE (ptr_mode));
4321 TYPE_UNSIGNED (nullptr_type_node) = 1;
4322 TYPE_PRECISION (nullptr_type_node) = GET_MODE_BITSIZE (ptr_mode);
4323 if (abi_version_at_least (9))
4324 SET_TYPE_ALIGN (nullptr_type_node, GET_MODE_ALIGNMENT (ptr_mode));
4325 SET_TYPE_MODE (nullptr_type_node, ptr_mode);
4326 record_builtin_type (RID_MAX, "decltype(nullptr)", nullptr_type_node);
4327 nullptr_node = build_int_cst (nullptr_type_node, 0);
4330 abort_fndecl
4331 = build_library_fn_ptr ("__cxa_pure_virtual", void_ftype,
4332 ECF_NORETURN | ECF_NOTHROW | ECF_COLD);
4334 /* Perform other language dependent initializations. */
4335 init_class_processing ();
4336 init_rtti_processing ();
4337 init_template_processing ();
4339 if (flag_exceptions)
4340 init_exception_processing ();
4342 if (! supports_one_only ())
4343 flag_weak = 0;
4345 make_fname_decl = cp_make_fname_decl;
4346 start_fname_decls ();
4348 /* Show we use EH for cleanups. */
4349 if (flag_exceptions)
4350 using_eh_for_cleanups ();
4353 /* Generate an initializer for a function naming variable from
4354 NAME. NAME may be NULL, to indicate a dependent name. TYPE_P is
4355 filled in with the type of the init. */
4357 tree
4358 cp_fname_init (const char* name, tree *type_p)
4360 tree domain = NULL_TREE;
4361 tree type;
4362 tree init = NULL_TREE;
4363 size_t length = 0;
4365 if (name)
4367 length = strlen (name);
4368 domain = build_index_type (size_int (length));
4369 init = build_string (length + 1, name);
4372 type = cp_build_qualified_type (char_type_node, TYPE_QUAL_CONST);
4373 type = build_cplus_array_type (type, domain);
4375 *type_p = type;
4377 if (init)
4378 TREE_TYPE (init) = type;
4379 else
4380 init = error_mark_node;
4382 return init;
4385 /* Create the VAR_DECL for __FUNCTION__ etc. ID is the name to give
4386 the decl, LOC is the location to give the decl, NAME is the
4387 initialization string and TYPE_DEP indicates whether NAME depended
4388 on the type of the function. We make use of that to detect
4389 __PRETTY_FUNCTION__ inside a template fn. This is being done lazily
4390 at the point of first use, so we mustn't push the decl now. */
4392 static tree
4393 cp_make_fname_decl (location_t loc, tree id, int type_dep)
4395 const char *const name = (type_dep && processing_template_decl
4396 ? NULL : fname_as_string (type_dep));
4397 tree type;
4398 tree init = cp_fname_init (name, &type);
4399 tree decl = build_decl (loc, VAR_DECL, id, type);
4401 if (name)
4402 free (CONST_CAST (char *, name));
4404 TREE_STATIC (decl) = 1;
4405 TREE_READONLY (decl) = 1;
4406 DECL_ARTIFICIAL (decl) = 1;
4408 TREE_USED (decl) = 1;
4410 if (current_function_decl)
4412 DECL_CONTEXT (decl) = current_function_decl;
4413 decl = pushdecl_outermost_localscope (decl);
4414 cp_finish_decl (decl, init, /*init_const_expr_p=*/false, NULL_TREE,
4415 LOOKUP_ONLYCONVERTING);
4417 else
4419 DECL_THIS_STATIC (decl) = true;
4420 pushdecl_top_level_and_finish (decl, init);
4423 return decl;
4426 static tree
4427 builtin_function_1 (tree decl, tree context, bool is_global)
4429 tree id = DECL_NAME (decl);
4430 const char *name = IDENTIFIER_POINTER (id);
4432 retrofit_lang_decl (decl);
4434 DECL_ARTIFICIAL (decl) = 1;
4435 SET_DECL_LANGUAGE (decl, lang_c);
4436 /* Runtime library routines are, by definition, available in an
4437 external shared object. */
4438 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
4439 DECL_VISIBILITY_SPECIFIED (decl) = 1;
4441 DECL_CONTEXT (decl) = context;
4443 /* A function in the user's namespace should have an explicit
4444 declaration before it is used. Mark the built-in function as
4445 anticipated but not actually declared. */
4446 if (name[0] != '_' || name[1] != '_')
4447 DECL_ANTICIPATED (decl) = 1;
4448 else if (strncmp (name + 2, "builtin_", strlen ("builtin_")) != 0)
4450 size_t len = strlen (name);
4452 /* Treat __*_chk fortification functions as anticipated as well,
4453 unless they are __builtin_*. */
4454 if (len > strlen ("___chk")
4455 && memcmp (name + len - strlen ("_chk"),
4456 "_chk", strlen ("_chk") + 1) == 0)
4457 DECL_ANTICIPATED (decl) = 1;
4460 if (is_global)
4461 pushdecl_top_level (decl);
4462 else
4463 pushdecl (decl);
4465 return decl;
4468 tree
4469 cxx_builtin_function (tree decl)
4471 tree id = DECL_NAME (decl);
4472 const char *name = IDENTIFIER_POINTER (id);
4473 /* All builtins that don't begin with an '_' should additionally
4474 go in the 'std' namespace. */
4475 if (name[0] != '_')
4477 tree decl2 = copy_node(decl);
4478 push_namespace (std_identifier);
4479 builtin_function_1 (decl2, std_node, false);
4480 pop_namespace ();
4483 return builtin_function_1 (decl, NULL_TREE, false);
4486 /* Like cxx_builtin_function, but guarantee the function is added to the global
4487 scope. This is to allow function specific options to add new machine
4488 dependent builtins when the target ISA changes via attribute((target(...)))
4489 which saves space on program startup if the program does not use non-generic
4490 ISAs. */
4492 tree
4493 cxx_builtin_function_ext_scope (tree decl)
4496 tree id = DECL_NAME (decl);
4497 const char *name = IDENTIFIER_POINTER (id);
4498 /* All builtins that don't begin with an '_' should additionally
4499 go in the 'std' namespace. */
4500 if (name[0] != '_')
4502 tree decl2 = copy_node(decl);
4503 push_namespace (std_identifier);
4504 builtin_function_1 (decl2, std_node, true);
4505 pop_namespace ();
4508 return builtin_function_1 (decl, NULL_TREE, true);
4511 /* Generate a FUNCTION_DECL with the typical flags for a runtime library
4512 function. Not called directly. */
4514 static tree
4515 build_library_fn (tree name, enum tree_code operator_code, tree type,
4516 int ecf_flags)
4518 tree fn = build_lang_decl (FUNCTION_DECL, name, type);
4519 DECL_EXTERNAL (fn) = 1;
4520 TREE_PUBLIC (fn) = 1;
4521 DECL_ARTIFICIAL (fn) = 1;
4522 DECL_OVERLOADED_OPERATOR_CODE_RAW (fn)
4523 = OVL_OP_INFO (false, operator_code)->ovl_op_code;
4524 SET_DECL_LANGUAGE (fn, lang_c);
4525 /* Runtime library routines are, by definition, available in an
4526 external shared object. */
4527 DECL_VISIBILITY (fn) = VISIBILITY_DEFAULT;
4528 DECL_VISIBILITY_SPECIFIED (fn) = 1;
4529 set_call_expr_flags (fn, ecf_flags);
4530 return fn;
4533 /* Returns the _DECL for a library function with C++ linkage. */
4535 static tree
4536 build_cp_library_fn (tree name, enum tree_code operator_code, tree type,
4537 int ecf_flags)
4539 tree fn = build_library_fn (name, operator_code, type, ecf_flags);
4540 DECL_CONTEXT (fn) = FROB_CONTEXT (current_namespace);
4541 SET_DECL_LANGUAGE (fn, lang_cplusplus);
4542 return fn;
4545 /* Like build_library_fn, but takes a C string instead of an
4546 IDENTIFIER_NODE. */
4548 tree
4549 build_library_fn_ptr (const char* name, tree type, int ecf_flags)
4551 return build_library_fn (get_identifier (name), ERROR_MARK, type, ecf_flags);
4554 /* Like build_cp_library_fn, but takes a C string instead of an
4555 IDENTIFIER_NODE. */
4557 tree
4558 build_cp_library_fn_ptr (const char* name, tree type, int ecf_flags)
4560 return build_cp_library_fn (get_identifier (name), ERROR_MARK, type,
4561 ecf_flags);
4564 /* Like build_library_fn, but also pushes the function so that we will
4565 be able to find it via get_global_binding. Also, the function
4566 may throw exceptions listed in RAISES. */
4568 tree
4569 push_library_fn (tree name, tree type, tree raises, int ecf_flags)
4571 tree fn;
4573 if (raises)
4574 type = build_exception_variant (type, raises);
4576 fn = build_library_fn (name, ERROR_MARK, type, ecf_flags);
4577 pushdecl_top_level (fn);
4578 return fn;
4581 /* Like build_cp_library_fn, but also pushes the function so that it
4582 will be found by normal lookup. */
4584 static tree
4585 push_cp_library_fn (enum tree_code operator_code, tree type,
4586 int ecf_flags)
4588 tree fn = build_cp_library_fn (ovl_op_identifier (false, operator_code),
4589 operator_code, type, ecf_flags);
4590 pushdecl (fn);
4591 if (flag_tm)
4592 apply_tm_attr (fn, get_identifier ("transaction_safe"));
4593 return fn;
4596 /* Like push_library_fn, but takes a TREE_LIST of parm types rather than
4597 a FUNCTION_TYPE. */
4599 tree
4600 push_void_library_fn (tree name, tree parmtypes, int ecf_flags)
4602 tree type = build_function_type (void_type_node, parmtypes);
4603 return push_library_fn (name, type, NULL_TREE, ecf_flags);
4606 /* Like push_library_fn, but also note that this function throws
4607 and does not return. Used for __throw_foo and the like. */
4609 tree
4610 push_throw_library_fn (tree name, tree type)
4612 tree fn = push_library_fn (name, type, NULL_TREE, ECF_NORETURN | ECF_COLD);
4613 return fn;
4616 /* When we call finish_struct for an anonymous union, we create
4617 default copy constructors and such. But, an anonymous union
4618 shouldn't have such things; this function undoes the damage to the
4619 anonymous union type T.
4621 (The reason that we create the synthesized methods is that we don't
4622 distinguish `union { int i; }' from `typedef union { int i; } U'.
4623 The first is an anonymous union; the second is just an ordinary
4624 union type.) */
4626 void
4627 fixup_anonymous_aggr (tree t)
4629 /* Wipe out memory of synthesized methods. */
4630 TYPE_HAS_USER_CONSTRUCTOR (t) = 0;
4631 TYPE_HAS_DEFAULT_CONSTRUCTOR (t) = 0;
4632 TYPE_HAS_COPY_CTOR (t) = 0;
4633 TYPE_HAS_CONST_COPY_CTOR (t) = 0;
4634 TYPE_HAS_COPY_ASSIGN (t) = 0;
4635 TYPE_HAS_CONST_COPY_ASSIGN (t) = 0;
4637 /* Splice the implicitly generated functions out of TYPE_FIELDS. */
4638 for (tree probe, *prev_p = &TYPE_FIELDS (t); (probe = *prev_p);)
4639 if (TREE_CODE (probe) == FUNCTION_DECL && DECL_ARTIFICIAL (probe))
4640 *prev_p = DECL_CHAIN (probe);
4641 else
4642 prev_p = &DECL_CHAIN (probe);
4644 /* Anonymous aggregates cannot have fields with ctors, dtors or complex
4645 assignment operators (because they cannot have these methods themselves).
4646 For anonymous unions this is already checked because they are not allowed
4647 in any union, otherwise we have to check it. */
4648 if (TREE_CODE (t) != UNION_TYPE)
4650 tree field, type;
4652 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
4653 if (TREE_CODE (field) == FIELD_DECL)
4655 type = TREE_TYPE (field);
4656 if (CLASS_TYPE_P (type))
4658 if (TYPE_NEEDS_CONSTRUCTING (type))
4659 error ("member %q+#D with constructor not allowed "
4660 "in anonymous aggregate", field);
4661 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
4662 error ("member %q+#D with destructor not allowed "
4663 "in anonymous aggregate", field);
4664 if (TYPE_HAS_COMPLEX_COPY_ASSIGN (type))
4665 error ("member %q+#D with copy assignment operator "
4666 "not allowed in anonymous aggregate", field);
4672 /* Warn for an attribute located at LOCATION that appertains to the
4673 class type CLASS_TYPE that has not been properly placed after its
4674 class-key, in it class-specifier. */
4676 void
4677 warn_misplaced_attr_for_class_type (source_location location,
4678 tree class_type)
4680 gcc_assert (OVERLOAD_TYPE_P (class_type));
4682 if (warning_at (location, OPT_Wattributes,
4683 "attribute ignored in declaration "
4684 "of %q#T", class_type))
4685 inform (location,
4686 "attribute for %q#T must follow the %qs keyword",
4687 class_type, class_key_or_enum_as_string (class_type));
4690 /* Make sure that a declaration with no declarator is well-formed, i.e.
4691 just declares a tagged type or anonymous union.
4693 Returns the type declared; or NULL_TREE if none. */
4695 tree
4696 check_tag_decl (cp_decl_specifier_seq *declspecs,
4697 bool explicit_type_instantiation_p)
4699 int saw_friend = decl_spec_seq_has_spec_p (declspecs, ds_friend);
4700 int saw_typedef = decl_spec_seq_has_spec_p (declspecs, ds_typedef);
4701 /* If a class, struct, or enum type is declared by the DECLSPECS
4702 (i.e, if a class-specifier, enum-specifier, or non-typename
4703 elaborated-type-specifier appears in the DECLSPECS),
4704 DECLARED_TYPE is set to the corresponding type. */
4705 tree declared_type = NULL_TREE;
4706 bool error_p = false;
4708 if (declspecs->multiple_types_p)
4709 error ("multiple types in one declaration");
4710 else if (declspecs->redefined_builtin_type)
4712 if (!in_system_header_at (input_location))
4713 permerror (declspecs->locations[ds_redefined_builtin_type_spec],
4714 "redeclaration of C++ built-in type %qT",
4715 declspecs->redefined_builtin_type);
4716 return NULL_TREE;
4719 if (declspecs->type
4720 && TYPE_P (declspecs->type)
4721 && ((TREE_CODE (declspecs->type) != TYPENAME_TYPE
4722 && MAYBE_CLASS_TYPE_P (declspecs->type))
4723 || TREE_CODE (declspecs->type) == ENUMERAL_TYPE))
4724 declared_type = declspecs->type;
4725 else if (declspecs->type == error_mark_node)
4726 error_p = true;
4727 if (declared_type == NULL_TREE && ! saw_friend && !error_p)
4728 permerror (input_location, "declaration does not declare anything");
4729 else if (declared_type != NULL_TREE && type_uses_auto (declared_type))
4731 error_at (declspecs->locations[ds_type_spec],
4732 "%<auto%> can only be specified for variables "
4733 "or function declarations");
4734 return error_mark_node;
4736 /* Check for an anonymous union. */
4737 else if (declared_type && RECORD_OR_UNION_CODE_P (TREE_CODE (declared_type))
4738 && TYPE_UNNAMED_P (declared_type))
4740 /* 7/3 In a simple-declaration, the optional init-declarator-list
4741 can be omitted only when declaring a class (clause 9) or
4742 enumeration (7.2), that is, when the decl-specifier-seq contains
4743 either a class-specifier, an elaborated-type-specifier with
4744 a class-key (9.1), or an enum-specifier. In these cases and
4745 whenever a class-specifier or enum-specifier is present in the
4746 decl-specifier-seq, the identifiers in these specifiers are among
4747 the names being declared by the declaration (as class-name,
4748 enum-names, or enumerators, depending on the syntax). In such
4749 cases, and except for the declaration of an unnamed bit-field (9.6),
4750 the decl-specifier-seq shall introduce one or more names into the
4751 program, or shall redeclare a name introduced by a previous
4752 declaration. [Example:
4753 enum { }; // ill-formed
4754 typedef class { }; // ill-formed
4755 --end example] */
4756 if (saw_typedef)
4758 error ("missing type-name in typedef-declaration");
4759 return NULL_TREE;
4761 /* Anonymous unions are objects, so they can have specifiers. */;
4762 SET_ANON_AGGR_TYPE_P (declared_type);
4764 if (TREE_CODE (declared_type) != UNION_TYPE
4765 && !in_system_header_at (input_location))
4766 pedwarn (input_location, OPT_Wpedantic, "ISO C++ prohibits anonymous structs");
4769 else
4771 if (decl_spec_seq_has_spec_p (declspecs, ds_inline))
4772 error_at (declspecs->locations[ds_inline],
4773 "%<inline%> can only be specified for functions");
4774 else if (decl_spec_seq_has_spec_p (declspecs, ds_virtual))
4775 error_at (declspecs->locations[ds_virtual],
4776 "%<virtual%> can only be specified for functions");
4777 else if (saw_friend
4778 && (!current_class_type
4779 || current_scope () != current_class_type))
4780 error_at (declspecs->locations[ds_friend],
4781 "%<friend%> can only be specified inside a class");
4782 else if (decl_spec_seq_has_spec_p (declspecs, ds_explicit))
4783 error_at (declspecs->locations[ds_explicit],
4784 "%<explicit%> can only be specified for constructors");
4785 else if (declspecs->storage_class)
4786 error_at (declspecs->locations[ds_storage_class],
4787 "a storage class can only be specified for objects "
4788 "and functions");
4789 else if (decl_spec_seq_has_spec_p (declspecs, ds_const))
4790 error_at (declspecs->locations[ds_const],
4791 "%<const%> can only be specified for objects and "
4792 "functions");
4793 else if (decl_spec_seq_has_spec_p (declspecs, ds_volatile))
4794 error_at (declspecs->locations[ds_volatile],
4795 "%<volatile%> can only be specified for objects and "
4796 "functions");
4797 else if (decl_spec_seq_has_spec_p (declspecs, ds_restrict))
4798 error_at (declspecs->locations[ds_restrict],
4799 "%<__restrict%> can only be specified for objects and "
4800 "functions");
4801 else if (decl_spec_seq_has_spec_p (declspecs, ds_thread))
4802 error_at (declspecs->locations[ds_thread],
4803 "%<__thread%> can only be specified for objects "
4804 "and functions");
4805 else if (saw_typedef)
4806 warning_at (declspecs->locations[ds_typedef], 0,
4807 "%<typedef%> was ignored in this declaration");
4808 else if (decl_spec_seq_has_spec_p (declspecs, ds_constexpr))
4809 error_at (declspecs->locations[ds_constexpr],
4810 "%<constexpr%> cannot be used for type declarations");
4813 if (declspecs->attributes && warn_attributes && declared_type)
4815 location_t loc;
4816 if (!CLASS_TYPE_P (declared_type)
4817 || !CLASSTYPE_TEMPLATE_INSTANTIATION (declared_type))
4818 /* For a non-template class, use the name location. */
4819 loc = location_of (declared_type);
4820 else
4821 /* For a template class (an explicit instantiation), use the
4822 current location. */
4823 loc = input_location;
4825 if (explicit_type_instantiation_p)
4826 /* [dcl.attr.grammar]/4:
4828 No attribute-specifier-seq shall appertain to an explicit
4829 instantiation. */
4831 if (warning_at (loc, OPT_Wattributes,
4832 "attribute ignored in explicit instantiation %q#T",
4833 declared_type))
4834 inform (loc,
4835 "no attribute can be applied to "
4836 "an explicit instantiation");
4838 else
4839 warn_misplaced_attr_for_class_type (loc, declared_type);
4842 return declared_type;
4845 /* Called when a declaration is seen that contains no names to declare.
4846 If its type is a reference to a structure, union or enum inherited
4847 from a containing scope, shadow that tag name for the current scope
4848 with a forward reference.
4849 If its type defines a new named structure or union
4850 or defines an enum, it is valid but we need not do anything here.
4851 Otherwise, it is an error.
4853 C++: may have to grok the declspecs to learn about static,
4854 complain for anonymous unions.
4856 Returns the TYPE declared -- or NULL_TREE if none. */
4858 tree
4859 shadow_tag (cp_decl_specifier_seq *declspecs)
4861 tree t = check_tag_decl (declspecs,
4862 /*explicit_type_instantiation_p=*/false);
4864 if (!t)
4865 return NULL_TREE;
4867 if (maybe_process_partial_specialization (t) == error_mark_node)
4868 return NULL_TREE;
4870 /* This is where the variables in an anonymous union are
4871 declared. An anonymous union declaration looks like:
4872 union { ... } ;
4873 because there is no declarator after the union, the parser
4874 sends that declaration here. */
4875 if (ANON_AGGR_TYPE_P (t))
4877 fixup_anonymous_aggr (t);
4879 if (TYPE_FIELDS (t))
4881 tree decl = grokdeclarator (/*declarator=*/NULL,
4882 declspecs, NORMAL, 0, NULL);
4883 finish_anon_union (decl);
4887 return t;
4890 /* Decode a "typename", such as "int **", returning a ..._TYPE node. */
4892 tree
4893 groktypename (cp_decl_specifier_seq *type_specifiers,
4894 const cp_declarator *declarator,
4895 bool is_template_arg)
4897 tree attrs;
4898 tree type;
4899 enum decl_context context
4900 = is_template_arg ? TEMPLATE_TYPE_ARG : TYPENAME;
4901 attrs = type_specifiers->attributes;
4902 type_specifiers->attributes = NULL_TREE;
4903 type = grokdeclarator (declarator, type_specifiers, context, 0, &attrs);
4904 if (attrs && type != error_mark_node)
4906 if (CLASS_TYPE_P (type))
4907 warning (OPT_Wattributes, "ignoring attributes applied to class type %qT "
4908 "outside of definition", type);
4909 else if (MAYBE_CLASS_TYPE_P (type))
4910 /* A template type parameter or other dependent type. */
4911 warning (OPT_Wattributes, "ignoring attributes applied to dependent "
4912 "type %qT without an associated declaration", type);
4913 else
4914 cplus_decl_attributes (&type, attrs, 0);
4916 return type;
4919 /* Process a DECLARATOR for a function-scope variable declaration,
4920 namespace-scope variable declaration, or function declaration.
4921 (Function definitions go through start_function; class member
4922 declarations appearing in the body of the class go through
4923 grokfield.) The DECL corresponding to the DECLARATOR is returned.
4924 If an error occurs, the error_mark_node is returned instead.
4926 DECLSPECS are the decl-specifiers for the declaration. INITIALIZED is
4927 SD_INITIALIZED if an explicit initializer is present, or SD_DEFAULTED
4928 for an explicitly defaulted function, or SD_DELETED for an explicitly
4929 deleted function, but 0 (SD_UNINITIALIZED) if this is a variable
4930 implicitly initialized via a default constructor. ATTRIBUTES and
4931 PREFIX_ATTRIBUTES are GNU attributes associated with this declaration.
4933 The scope represented by the context of the returned DECL is pushed
4934 (if it is not the global namespace) and is assigned to
4935 *PUSHED_SCOPE_P. The caller is then responsible for calling
4936 pop_scope on *PUSHED_SCOPE_P if it is set. */
4938 tree
4939 start_decl (const cp_declarator *declarator,
4940 cp_decl_specifier_seq *declspecs,
4941 int initialized,
4942 tree attributes,
4943 tree prefix_attributes,
4944 tree *pushed_scope_p)
4946 tree decl;
4947 tree context;
4948 bool was_public;
4949 int flags;
4950 bool alias;
4952 *pushed_scope_p = NULL_TREE;
4954 /* An object declared as __attribute__((deprecated)) suppresses
4955 warnings of uses of other deprecated items. */
4956 if (lookup_attribute ("deprecated", attributes))
4957 deprecated_state = DEPRECATED_SUPPRESS;
4959 attributes = chainon (attributes, prefix_attributes);
4961 decl = grokdeclarator (declarator, declspecs, NORMAL, initialized,
4962 &attributes);
4964 deprecated_state = DEPRECATED_NORMAL;
4966 if (decl == NULL_TREE || VOID_TYPE_P (decl)
4967 || decl == error_mark_node)
4968 return error_mark_node;
4970 context = CP_DECL_CONTEXT (decl);
4971 if (context != global_namespace)
4972 *pushed_scope_p = push_scope (context);
4974 /* Is it valid for this decl to have an initializer at all?
4975 If not, set INITIALIZED to zero, which will indirectly
4976 tell `cp_finish_decl' to ignore the initializer once it is parsed. */
4977 if (initialized
4978 && TREE_CODE (decl) == TYPE_DECL)
4980 error ("typedef %qD is initialized (use decltype instead)", decl);
4981 return error_mark_node;
4984 if (initialized)
4986 if (! toplevel_bindings_p ()
4987 && DECL_EXTERNAL (decl))
4988 warning (0, "declaration of %q#D has %<extern%> and is initialized",
4989 decl);
4990 DECL_EXTERNAL (decl) = 0;
4991 if (toplevel_bindings_p ())
4992 TREE_STATIC (decl) = 1;
4994 alias = lookup_attribute ("alias", DECL_ATTRIBUTES (decl)) != 0;
4996 if (alias && TREE_CODE (decl) == FUNCTION_DECL)
4997 record_key_method_defined (decl);
4999 /* If this is a typedef that names the class for linkage purposes
5000 (7.1.3p8), apply any attributes directly to the type. */
5001 if (TREE_CODE (decl) == TYPE_DECL
5002 && OVERLOAD_TYPE_P (TREE_TYPE (decl))
5003 && decl == TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (decl))))
5004 flags = ATTR_FLAG_TYPE_IN_PLACE;
5005 else
5006 flags = 0;
5008 /* Set attributes here so if duplicate decl, will have proper attributes. */
5009 cplus_decl_attributes (&decl, attributes, flags);
5011 /* Dllimported symbols cannot be defined. Static data members (which
5012 can be initialized in-class and dllimported) go through grokfield,
5013 not here, so we don't need to exclude those decls when checking for
5014 a definition. */
5015 if (initialized && DECL_DLLIMPORT_P (decl))
5017 error ("definition of %q#D is marked %<dllimport%>", decl);
5018 DECL_DLLIMPORT_P (decl) = 0;
5021 /* If #pragma weak was used, mark the decl weak now. */
5022 if (!processing_template_decl && !DECL_DECOMPOSITION_P (decl))
5023 maybe_apply_pragma_weak (decl);
5025 if (TREE_CODE (decl) == FUNCTION_DECL
5026 && DECL_DECLARED_INLINE_P (decl)
5027 && DECL_UNINLINABLE (decl)
5028 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
5029 warning_at (DECL_SOURCE_LOCATION (decl), 0,
5030 "inline function %qD given attribute noinline", decl);
5032 if (TYPE_P (context) && COMPLETE_TYPE_P (complete_type (context)))
5034 bool this_tmpl = (processing_template_decl
5035 > template_class_depth (context));
5036 if (VAR_P (decl))
5038 tree field = lookup_field (context, DECL_NAME (decl), 0, false);
5039 if (field == NULL_TREE
5040 || !(VAR_P (field) || variable_template_p (field)))
5041 error ("%q+#D is not a static data member of %q#T", decl, context);
5042 else if (variable_template_p (field)
5043 && (DECL_LANG_SPECIFIC (decl)
5044 && DECL_TEMPLATE_SPECIALIZATION (decl)))
5045 /* OK, specialization was already checked. */;
5046 else if (variable_template_p (field) && !this_tmpl)
5048 error_at (DECL_SOURCE_LOCATION (decl),
5049 "non-member-template declaration of %qD", decl);
5050 inform (DECL_SOURCE_LOCATION (field), "does not match "
5051 "member template declaration here");
5052 return error_mark_node;
5054 else
5056 if (variable_template_p (field))
5057 field = DECL_TEMPLATE_RESULT (field);
5059 if (DECL_CONTEXT (field) != context)
5061 if (!same_type_p (DECL_CONTEXT (field), context))
5062 permerror (input_location, "ISO C++ does not permit %<%T::%D%> "
5063 "to be defined as %<%T::%D%>",
5064 DECL_CONTEXT (field), DECL_NAME (decl),
5065 context, DECL_NAME (decl));
5066 DECL_CONTEXT (decl) = DECL_CONTEXT (field);
5068 /* Static data member are tricky; an in-class initialization
5069 still doesn't provide a definition, so the in-class
5070 declaration will have DECL_EXTERNAL set, but will have an
5071 initialization. Thus, duplicate_decls won't warn
5072 about this situation, and so we check here. */
5073 if (initialized && DECL_INITIALIZED_IN_CLASS_P (field))
5074 error ("duplicate initialization of %qD", decl);
5075 field = duplicate_decls (decl, field,
5076 /*newdecl_is_friend=*/false);
5077 if (field == error_mark_node)
5078 return error_mark_node;
5079 else if (field)
5080 decl = field;
5083 else
5085 tree field = check_classfn (context, decl,
5086 this_tmpl
5087 ? current_template_parms
5088 : NULL_TREE);
5089 if (field && field != error_mark_node
5090 && duplicate_decls (decl, field,
5091 /*newdecl_is_friend=*/false))
5092 decl = field;
5095 /* cp_finish_decl sets DECL_EXTERNAL if DECL_IN_AGGR_P is set. */
5096 DECL_IN_AGGR_P (decl) = 0;
5097 /* Do not mark DECL as an explicit specialization if it was not
5098 already marked as an instantiation; a declaration should
5099 never be marked as a specialization unless we know what
5100 template is being specialized. */
5101 if (DECL_LANG_SPECIFIC (decl) && DECL_USE_TEMPLATE (decl))
5103 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
5104 if (TREE_CODE (decl) == FUNCTION_DECL)
5105 DECL_COMDAT (decl) = (TREE_PUBLIC (decl)
5106 && DECL_DECLARED_INLINE_P (decl));
5107 else
5108 DECL_COMDAT (decl) = false;
5110 /* [temp.expl.spec] An explicit specialization of a static data
5111 member of a template is a definition if the declaration
5112 includes an initializer; otherwise, it is a declaration.
5114 We check for processing_specialization so this only applies
5115 to the new specialization syntax. */
5116 if (!initialized && processing_specialization)
5117 DECL_EXTERNAL (decl) = 1;
5120 if (DECL_EXTERNAL (decl) && ! DECL_TEMPLATE_SPECIALIZATION (decl)
5121 /* Aliases are definitions. */
5122 && !alias)
5123 permerror (input_location, "declaration of %q#D outside of class is not definition",
5124 decl);
5127 was_public = TREE_PUBLIC (decl);
5129 /* Enter this declaration into the symbol table. Don't push the plain
5130 VAR_DECL for a variable template. */
5131 if (!template_parm_scope_p ()
5132 || !VAR_P (decl))
5133 decl = maybe_push_decl (decl);
5135 if (processing_template_decl)
5136 decl = push_template_decl (decl);
5137 if (decl == error_mark_node)
5138 return error_mark_node;
5140 if (VAR_P (decl)
5141 && DECL_NAMESPACE_SCOPE_P (decl) && !TREE_PUBLIC (decl) && !was_public
5142 && !DECL_THIS_STATIC (decl) && !DECL_ARTIFICIAL (decl))
5144 /* This is a const variable with implicit 'static'. Set
5145 DECL_THIS_STATIC so we can tell it from variables that are
5146 !TREE_PUBLIC because of the anonymous namespace. */
5147 gcc_assert (CP_TYPE_CONST_P (TREE_TYPE (decl)) || errorcount);
5148 DECL_THIS_STATIC (decl) = 1;
5151 if (current_function_decl && VAR_P (decl)
5152 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
5154 bool ok = false;
5155 if (CP_DECL_THREAD_LOCAL_P (decl))
5156 error ("%qD declared %<thread_local%> in %<constexpr%> function",
5157 decl);
5158 else if (TREE_STATIC (decl))
5159 error ("%qD declared %<static%> in %<constexpr%> function", decl);
5160 else
5161 ok = true;
5162 if (!ok)
5163 cp_function_chain->invalid_constexpr = true;
5166 if (!processing_template_decl && VAR_P (decl))
5167 start_decl_1 (decl, initialized);
5169 return decl;
5172 /* Process the declaration of a variable DECL. INITIALIZED is true
5173 iff DECL is explicitly initialized. (INITIALIZED is false if the
5174 variable is initialized via an implicitly-called constructor.)
5175 This function must be called for ordinary variables (including, for
5176 example, implicit instantiations of templates), but must not be
5177 called for template declarations. */
5179 void
5180 start_decl_1 (tree decl, bool initialized)
5182 tree type;
5183 bool complete_p;
5184 bool aggregate_definition_p;
5186 gcc_assert (!processing_template_decl);
5188 if (error_operand_p (decl))
5189 return;
5191 gcc_assert (VAR_P (decl));
5193 type = TREE_TYPE (decl);
5194 complete_p = COMPLETE_TYPE_P (type);
5195 aggregate_definition_p = MAYBE_CLASS_TYPE_P (type) && !DECL_EXTERNAL (decl);
5197 /* If an explicit initializer is present, or if this is a definition
5198 of an aggregate, then we need a complete type at this point.
5199 (Scalars are always complete types, so there is nothing to
5200 check.) This code just sets COMPLETE_P; errors (if necessary)
5201 are issued below. */
5202 if ((initialized || aggregate_definition_p)
5203 && !complete_p
5204 && COMPLETE_TYPE_P (complete_type (type)))
5206 complete_p = true;
5207 /* We will not yet have set TREE_READONLY on DECL if the type
5208 was "const", but incomplete, before this point. But, now, we
5209 have a complete type, so we can try again. */
5210 cp_apply_type_quals_to_decl (cp_type_quals (type), decl);
5213 if (initialized)
5214 /* Is it valid for this decl to have an initializer at all? */
5216 /* Don't allow initializations for incomplete types except for
5217 arrays which might be completed by the initialization. */
5218 if (complete_p)
5219 ; /* A complete type is ok. */
5220 else if (type_uses_auto (type))
5221 ; /* An auto type is ok. */
5222 else if (TREE_CODE (type) != ARRAY_TYPE)
5224 error ("variable %q#D has initializer but incomplete type", decl);
5225 type = TREE_TYPE (decl) = error_mark_node;
5227 else if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (type))))
5229 if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
5230 error ("elements of array %q#D have incomplete type", decl);
5231 /* else we already gave an error in start_decl. */
5234 else if (aggregate_definition_p && !complete_p)
5236 if (type_uses_auto (type))
5237 gcc_assert (CLASS_PLACEHOLDER_TEMPLATE (type));
5238 else
5240 error ("aggregate %q#D has incomplete type and cannot be defined",
5241 decl);
5242 /* Change the type so that assemble_variable will give
5243 DECL an rtl we can live with: (mem (const_int 0)). */
5244 type = TREE_TYPE (decl) = error_mark_node;
5248 /* Create a new scope to hold this declaration if necessary.
5249 Whether or not a new scope is necessary cannot be determined
5250 until after the type has been completed; if the type is a
5251 specialization of a class template it is not until after
5252 instantiation has occurred that TYPE_HAS_NONTRIVIAL_DESTRUCTOR
5253 will be set correctly. */
5254 maybe_push_cleanup_level (type);
5257 /* Handle initialization of references. DECL, TYPE, and INIT have the
5258 same meaning as in cp_finish_decl. *CLEANUP must be NULL on entry,
5259 but will be set to a new CLEANUP_STMT if a temporary is created
5260 that must be destroyed subsequently.
5262 Returns an initializer expression to use to initialize DECL, or
5263 NULL if the initialization can be performed statically.
5265 Quotes on semantics can be found in ARM 8.4.3. */
5267 static tree
5268 grok_reference_init (tree decl, tree type, tree init, int flags)
5270 if (init == NULL_TREE)
5272 if ((DECL_LANG_SPECIFIC (decl) == 0
5273 || DECL_IN_AGGR_P (decl) == 0)
5274 && ! DECL_THIS_EXTERN (decl))
5275 error ("%qD declared as reference but not initialized", decl);
5276 return NULL_TREE;
5279 if (TREE_CODE (init) == TREE_LIST)
5280 init = build_x_compound_expr_from_list (init, ELK_INIT,
5281 tf_warning_or_error);
5283 tree ttype = TREE_TYPE (type);
5284 if (TREE_CODE (ttype) != ARRAY_TYPE
5285 && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE)
5286 /* Note: default conversion is only called in very special cases. */
5287 init = decay_conversion (init, tf_warning_or_error);
5289 /* check_initializer handles this for non-reference variables, but for
5290 references we need to do it here or the initializer will get the
5291 incomplete array type and confuse later calls to
5292 cp_complete_array_type. */
5293 if (TREE_CODE (ttype) == ARRAY_TYPE
5294 && TYPE_DOMAIN (ttype) == NULL_TREE
5295 && (BRACE_ENCLOSED_INITIALIZER_P (init)
5296 || TREE_CODE (init) == STRING_CST))
5298 cp_complete_array_type (&ttype, init, false);
5299 if (ttype != TREE_TYPE (type))
5300 type = cp_build_reference_type (ttype, TYPE_REF_IS_RVALUE (type));
5303 /* Convert INIT to the reference type TYPE. This may involve the
5304 creation of a temporary, whose lifetime must be the same as that
5305 of the reference. If so, a DECL_EXPR for the temporary will be
5306 added just after the DECL_EXPR for DECL. That's why we don't set
5307 DECL_INITIAL for local references (instead assigning to them
5308 explicitly); we need to allow the temporary to be initialized
5309 first. */
5310 return initialize_reference (type, init, flags,
5311 tf_warning_or_error);
5314 /* Designated initializers in arrays are not supported in GNU C++.
5315 The parser cannot detect this error since it does not know whether
5316 a given brace-enclosed initializer is for a class type or for an
5317 array. This function checks that CE does not use a designated
5318 initializer. If it does, an error is issued. Returns true if CE
5319 is valid, i.e., does not have a designated initializer. */
5321 bool
5322 check_array_designated_initializer (constructor_elt *ce,
5323 unsigned HOST_WIDE_INT index)
5325 /* Designated initializers for array elements are not supported. */
5326 if (ce->index)
5328 /* The parser only allows identifiers as designated
5329 initializers. */
5330 if (ce->index == error_mark_node)
5332 error ("name used in a GNU-style designated "
5333 "initializer for an array");
5334 return false;
5336 else if (identifier_p (ce->index))
5338 error ("name %qD used in a GNU-style designated "
5339 "initializer for an array", ce->index);
5340 return false;
5343 tree ce_index = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5344 ce->index, true);
5345 if (ce_index
5346 && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (ce_index))
5347 && (TREE_CODE (ce_index = fold_non_dependent_expr (ce_index))
5348 == INTEGER_CST))
5350 /* A C99 designator is OK if it matches the current index. */
5351 if (wi::to_wide (ce_index) == index)
5353 ce->index = ce_index;
5354 return true;
5356 else
5357 sorry ("non-trivial designated initializers not supported");
5359 else
5360 error ("C99 designator %qE is not an integral constant-expression",
5361 ce->index);
5363 return false;
5366 return true;
5369 /* When parsing `int a[] = {1, 2};' we don't know the size of the
5370 array until we finish parsing the initializer. If that's the
5371 situation we're in, update DECL accordingly. */
5373 static void
5374 maybe_deduce_size_from_array_init (tree decl, tree init)
5376 tree type = TREE_TYPE (decl);
5378 if (TREE_CODE (type) == ARRAY_TYPE
5379 && TYPE_DOMAIN (type) == NULL_TREE
5380 && TREE_CODE (decl) != TYPE_DECL)
5382 /* do_default is really a C-ism to deal with tentative definitions.
5383 But let's leave it here to ease the eventual merge. */
5384 int do_default = !DECL_EXTERNAL (decl);
5385 tree initializer = init ? init : DECL_INITIAL (decl);
5386 int failure = 0;
5388 /* Check that there are no designated initializers in INIT, as
5389 those are not supported in GNU C++, and as the middle-end
5390 will crash if presented with a non-numeric designated
5391 initializer. */
5392 if (initializer && BRACE_ENCLOSED_INITIALIZER_P (initializer))
5394 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (initializer);
5395 constructor_elt *ce;
5396 HOST_WIDE_INT i;
5397 FOR_EACH_VEC_SAFE_ELT (v, i, ce)
5399 if (instantiation_dependent_expression_p (ce->index))
5400 return;
5401 if (!check_array_designated_initializer (ce, i))
5402 failure = 1;
5406 if (failure)
5407 TREE_TYPE (decl) = error_mark_node;
5408 else
5410 failure = cp_complete_array_type (&TREE_TYPE (decl), initializer,
5411 do_default);
5412 if (failure == 1)
5414 error_at (cp_expr_loc_or_loc (initializer,
5415 DECL_SOURCE_LOCATION (decl)),
5416 "initializer fails to determine size of %qD", decl);
5418 else if (failure == 2)
5420 if (do_default)
5422 error_at (DECL_SOURCE_LOCATION (decl),
5423 "array size missing in %qD", decl);
5425 /* If a `static' var's size isn't known, make it extern as
5426 well as static, so it does not get allocated. If it's not
5427 `static', then don't mark it extern; finish_incomplete_decl
5428 will give it a default size and it will get allocated. */
5429 else if (!pedantic && TREE_STATIC (decl) && !TREE_PUBLIC (decl))
5430 DECL_EXTERNAL (decl) = 1;
5432 else if (failure == 3)
5434 error_at (DECL_SOURCE_LOCATION (decl),
5435 "zero-size array %qD", decl);
5439 cp_apply_type_quals_to_decl (cp_type_quals (TREE_TYPE (decl)), decl);
5441 relayout_decl (decl);
5445 /* Set DECL_SIZE, DECL_ALIGN, etc. for DECL (a VAR_DECL), and issue
5446 any appropriate error messages regarding the layout. */
5448 static void
5449 layout_var_decl (tree decl)
5451 tree type;
5453 type = TREE_TYPE (decl);
5454 if (type == error_mark_node)
5455 return;
5457 /* If we haven't already laid out this declaration, do so now.
5458 Note that we must not call complete type for an external object
5459 because it's type might involve templates that we are not
5460 supposed to instantiate yet. (And it's perfectly valid to say
5461 `extern X x' for some incomplete type `X'.) */
5462 if (!DECL_EXTERNAL (decl))
5463 complete_type (type);
5464 if (!DECL_SIZE (decl)
5465 && TREE_TYPE (decl) != error_mark_node
5466 && complete_or_array_type_p (type))
5467 layout_decl (decl, 0);
5469 if (!DECL_EXTERNAL (decl) && DECL_SIZE (decl) == NULL_TREE)
5471 /* An automatic variable with an incomplete type: that is an error.
5472 Don't talk about array types here, since we took care of that
5473 message in grokdeclarator. */
5474 error_at (DECL_SOURCE_LOCATION (decl),
5475 "storage size of %qD isn%'t known", decl);
5476 TREE_TYPE (decl) = error_mark_node;
5478 #if 0
5479 /* Keep this code around in case we later want to control debug info
5480 based on whether a type is "used". (jason 1999-11-11) */
5482 else if (!DECL_EXTERNAL (decl) && MAYBE_CLASS_TYPE_P (ttype))
5483 /* Let debugger know it should output info for this type. */
5484 note_debug_info_needed (ttype);
5486 if (TREE_STATIC (decl) && DECL_CLASS_SCOPE_P (decl))
5487 note_debug_info_needed (DECL_CONTEXT (decl));
5488 #endif
5490 if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl))
5491 && DECL_SIZE (decl) != NULL_TREE
5492 && ! TREE_CONSTANT (DECL_SIZE (decl)))
5494 if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
5495 constant_expression_warning (DECL_SIZE (decl));
5496 else
5498 error_at (DECL_SOURCE_LOCATION (decl),
5499 "storage size of %qD isn%'t constant", decl);
5500 TREE_TYPE (decl) = error_mark_node;
5505 /* If a local static variable is declared in an inline function, or if
5506 we have a weak definition, we must endeavor to create only one
5507 instance of the variable at link-time. */
5509 void
5510 maybe_commonize_var (tree decl)
5512 /* Static data in a function with comdat linkage also has comdat
5513 linkage. */
5514 if ((TREE_STATIC (decl)
5515 /* Don't mess with __FUNCTION__. */
5516 && ! DECL_ARTIFICIAL (decl)
5517 && DECL_FUNCTION_SCOPE_P (decl)
5518 && vague_linkage_p (DECL_CONTEXT (decl)))
5519 || (TREE_PUBLIC (decl) && DECL_INLINE_VAR_P (decl)))
5521 if (flag_weak)
5523 /* With weak symbols, we simply make the variable COMDAT;
5524 that will cause copies in multiple translations units to
5525 be merged. */
5526 comdat_linkage (decl);
5528 else
5530 if (DECL_INITIAL (decl) == NULL_TREE
5531 || DECL_INITIAL (decl) == error_mark_node)
5533 /* Without weak symbols, we can use COMMON to merge
5534 uninitialized variables. */
5535 TREE_PUBLIC (decl) = 1;
5536 DECL_COMMON (decl) = 1;
5538 else
5540 /* While for initialized variables, we must use internal
5541 linkage -- which means that multiple copies will not
5542 be merged. */
5543 TREE_PUBLIC (decl) = 0;
5544 DECL_COMMON (decl) = 0;
5545 const char *msg;
5546 if (DECL_INLINE_VAR_P (decl))
5547 msg = G_("sorry: semantics of inline variable "
5548 "%q#D are wrong (you%'ll wind up with "
5549 "multiple copies)");
5550 else
5551 msg = G_("sorry: semantics of inline function "
5552 "static data %q#D are wrong (you%'ll wind "
5553 "up with multiple copies)");
5554 if (warning_at (DECL_SOURCE_LOCATION (decl), 0,
5555 msg, decl))
5556 inform (DECL_SOURCE_LOCATION (decl),
5557 "you can work around this by removing the initializer");
5563 /* Issue an error message if DECL is an uninitialized const variable.
5564 CONSTEXPR_CONTEXT_P is true when the function is called in a constexpr
5565 context from potential_constant_expression. Returns true if all is well,
5566 false otherwise. */
5568 bool
5569 check_for_uninitialized_const_var (tree decl, bool constexpr_context_p,
5570 tsubst_flags_t complain)
5572 tree type = strip_array_types (TREE_TYPE (decl));
5574 /* ``Unless explicitly declared extern, a const object does not have
5575 external linkage and must be initialized. ($8.4; $12.1)'' ARM
5576 7.1.6 */
5577 if (VAR_P (decl)
5578 && !TYPE_REF_P (type)
5579 && (constexpr_context_p
5580 || CP_TYPE_CONST_P (type) || var_in_constexpr_fn (decl))
5581 && !DECL_NONTRIVIALLY_INITIALIZED_P (decl))
5583 tree field = default_init_uninitialized_part (type);
5584 if (!field)
5585 return true;
5587 if (!constexpr_context_p)
5589 if (CP_TYPE_CONST_P (type))
5591 if (complain & tf_error)
5592 permerror (DECL_SOURCE_LOCATION (decl),
5593 "uninitialized const %qD", decl);
5595 else
5597 if (!is_instantiation_of_constexpr (current_function_decl)
5598 && (complain & tf_error))
5599 error_at (DECL_SOURCE_LOCATION (decl),
5600 "uninitialized variable %qD in %<constexpr%> "
5601 "function", decl);
5602 cp_function_chain->invalid_constexpr = true;
5605 else if (complain & tf_error)
5606 error_at (DECL_SOURCE_LOCATION (decl),
5607 "uninitialized variable %qD in %<constexpr%> context",
5608 decl);
5610 if (CLASS_TYPE_P (type) && (complain & tf_error))
5612 tree defaulted_ctor;
5614 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
5615 "%q#T has no user-provided default constructor", type);
5616 defaulted_ctor = in_class_defaulted_default_constructor (type);
5617 if (defaulted_ctor)
5618 inform (DECL_SOURCE_LOCATION (defaulted_ctor),
5619 "constructor is not user-provided because it is "
5620 "explicitly defaulted in the class body");
5621 inform (DECL_SOURCE_LOCATION (field),
5622 "and the implicitly-defined constructor does not "
5623 "initialize %q#D", field);
5626 return false;
5629 return true;
5632 /* Structure holding the current initializer being processed by reshape_init.
5633 CUR is a pointer to the current element being processed, END is a pointer
5634 after the last element present in the initializer. */
5635 struct reshape_iter
5637 constructor_elt *cur;
5638 constructor_elt *end;
5641 static tree reshape_init_r (tree, reshape_iter *, bool, tsubst_flags_t);
5643 /* FIELD is a FIELD_DECL or NULL. In the former case, the value
5644 returned is the next FIELD_DECL (possibly FIELD itself) that can be
5645 initialized. If there are no more such fields, the return value
5646 will be NULL. */
5648 tree
5649 next_initializable_field (tree field)
5651 while (field
5652 && (TREE_CODE (field) != FIELD_DECL
5653 || DECL_UNNAMED_BIT_FIELD (field)
5654 || (DECL_ARTIFICIAL (field)
5655 && !(cxx_dialect >= cxx17 && DECL_FIELD_IS_BASE (field)))))
5656 field = DECL_CHAIN (field);
5658 return field;
5661 /* Return true for [dcl.init.list] direct-list-initialization from
5662 single element of enumeration with a fixed underlying type. */
5664 bool
5665 is_direct_enum_init (tree type, tree init)
5667 if (cxx_dialect >= cxx17
5668 && TREE_CODE (type) == ENUMERAL_TYPE
5669 && ENUM_FIXED_UNDERLYING_TYPE_P (type)
5670 && TREE_CODE (init) == CONSTRUCTOR
5671 && CONSTRUCTOR_IS_DIRECT_INIT (init)
5672 && CONSTRUCTOR_NELTS (init) == 1)
5673 return true;
5674 return false;
5677 /* Subroutine of reshape_init_array and reshape_init_vector, which does
5678 the actual work. ELT_TYPE is the element type of the array. MAX_INDEX is an
5679 INTEGER_CST representing the size of the array minus one (the maximum index),
5680 or NULL_TREE if the array was declared without specifying the size. D is
5681 the iterator within the constructor. */
5683 static tree
5684 reshape_init_array_1 (tree elt_type, tree max_index, reshape_iter *d,
5685 tsubst_flags_t complain)
5687 tree new_init;
5688 bool sized_array_p = (max_index && TREE_CONSTANT (max_index));
5689 unsigned HOST_WIDE_INT max_index_cst = 0;
5690 unsigned HOST_WIDE_INT index;
5692 /* The initializer for an array is always a CONSTRUCTOR. */
5693 new_init = build_constructor (init_list_type_node, NULL);
5695 if (sized_array_p)
5697 /* Minus 1 is used for zero sized arrays. */
5698 if (integer_all_onesp (max_index))
5699 return new_init;
5701 if (tree_fits_uhwi_p (max_index))
5702 max_index_cst = tree_to_uhwi (max_index);
5703 /* sizetype is sign extended, not zero extended. */
5704 else
5705 max_index_cst = tree_to_uhwi (fold_convert (size_type_node, max_index));
5708 /* Loop until there are no more initializers. */
5709 for (index = 0;
5710 d->cur != d->end && (!sized_array_p || index <= max_index_cst);
5711 ++index)
5713 tree elt_init;
5714 constructor_elt *old_cur = d->cur;
5716 check_array_designated_initializer (d->cur, index);
5717 elt_init = reshape_init_r (elt_type, d, /*first_initializer_p=*/false,
5718 complain);
5719 if (elt_init == error_mark_node)
5720 return error_mark_node;
5721 CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_init),
5722 size_int (index), elt_init);
5723 if (!TREE_CONSTANT (elt_init))
5724 TREE_CONSTANT (new_init) = false;
5726 /* This can happen with an invalid initializer (c++/54501). */
5727 if (d->cur == old_cur && !sized_array_p)
5728 break;
5731 return new_init;
5734 /* Subroutine of reshape_init_r, processes the initializers for arrays.
5735 Parameters are the same of reshape_init_r. */
5737 static tree
5738 reshape_init_array (tree type, reshape_iter *d, tsubst_flags_t complain)
5740 tree max_index = NULL_TREE;
5742 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
5744 if (TYPE_DOMAIN (type))
5745 max_index = array_type_nelts (type);
5747 return reshape_init_array_1 (TREE_TYPE (type), max_index, d, complain);
5750 /* Subroutine of reshape_init_r, processes the initializers for vectors.
5751 Parameters are the same of reshape_init_r. */
5753 static tree
5754 reshape_init_vector (tree type, reshape_iter *d, tsubst_flags_t complain)
5756 tree max_index = NULL_TREE;
5758 gcc_assert (VECTOR_TYPE_P (type));
5760 if (COMPOUND_LITERAL_P (d->cur->value))
5762 tree value = d->cur->value;
5763 if (!same_type_p (TREE_TYPE (value), type))
5765 if (complain & tf_error)
5766 error ("invalid type %qT as initializer for a vector of type %qT",
5767 TREE_TYPE (d->cur->value), type);
5768 value = error_mark_node;
5770 ++d->cur;
5771 return value;
5774 /* For a vector, we initialize it as an array of the appropriate size. */
5775 if (VECTOR_TYPE_P (type))
5776 max_index = size_int (TYPE_VECTOR_SUBPARTS (type) - 1);
5778 return reshape_init_array_1 (TREE_TYPE (type), max_index, d, complain);
5781 /* Subroutine of reshape_init_r, processes the initializers for classes
5782 or union. Parameters are the same of reshape_init_r. */
5784 static tree
5785 reshape_init_class (tree type, reshape_iter *d, bool first_initializer_p,
5786 tsubst_flags_t complain)
5788 tree field;
5789 tree new_init;
5791 gcc_assert (CLASS_TYPE_P (type));
5793 /* The initializer for a class is always a CONSTRUCTOR. */
5794 new_init = build_constructor (init_list_type_node, NULL);
5795 field = next_initializable_field (TYPE_FIELDS (type));
5797 if (!field)
5799 /* [dcl.init.aggr]
5801 An initializer for an aggregate member that is an
5802 empty class shall have the form of an empty
5803 initializer-list {}. */
5804 if (!first_initializer_p)
5806 if (complain & tf_error)
5807 error ("initializer for %qT must be brace-enclosed", type);
5808 return error_mark_node;
5810 return new_init;
5813 /* Loop through the initializable fields, gathering initializers. */
5814 while (d->cur != d->end)
5816 tree field_init;
5817 constructor_elt *old_cur = d->cur;
5819 /* Handle designated initializers, as an extension. */
5820 if (d->cur->index)
5822 if (d->cur->index == error_mark_node)
5823 return error_mark_node;
5825 if (TREE_CODE (d->cur->index) == FIELD_DECL)
5827 /* We already reshaped this. */
5828 if (field != d->cur->index)
5830 tree id = DECL_NAME (d->cur->index);
5831 gcc_assert (id);
5832 gcc_checking_assert (d->cur->index
5833 == get_class_binding (type, id, false));
5834 field = d->cur->index;
5837 else if (TREE_CODE (d->cur->index) == IDENTIFIER_NODE)
5838 field = get_class_binding (type, d->cur->index, false);
5839 else
5841 if (complain & tf_error)
5842 error ("%<[%E] =%> used in a GNU-style designated initializer"
5843 " for class %qT", d->cur->index, type);
5844 return error_mark_node;
5847 if (!field || TREE_CODE (field) != FIELD_DECL)
5849 if (complain & tf_error)
5850 error ("%qT has no non-static data member named %qD", type,
5851 d->cur->index);
5852 return error_mark_node;
5856 /* If we processed all the member of the class, we are done. */
5857 if (!field)
5858 break;
5860 field_init = reshape_init_r (TREE_TYPE (field), d,
5861 /*first_initializer_p=*/false, complain);
5862 if (field_init == error_mark_node)
5863 return error_mark_node;
5865 if (d->cur == old_cur && d->cur->index)
5867 /* This can happen with an invalid initializer for a flexible
5868 array member (c++/54441). */
5869 if (complain & tf_error)
5870 error ("invalid initializer for %q#D", field);
5871 return error_mark_node;
5874 CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_init), field, field_init);
5876 /* [dcl.init.aggr]
5878 When a union is initialized with a brace-enclosed
5879 initializer, the braces shall only contain an
5880 initializer for the first member of the union. */
5881 if (TREE_CODE (type) == UNION_TYPE)
5882 break;
5884 field = next_initializable_field (DECL_CHAIN (field));
5887 return new_init;
5890 /* Subroutine of reshape_init_r. We're in a context where C99 initializer
5891 designators are not valid; either complain or return true to indicate
5892 that reshape_init_r should return error_mark_node. */
5894 static bool
5895 has_designator_problem (reshape_iter *d, tsubst_flags_t complain)
5897 if (d->cur->index)
5899 if (complain & tf_error)
5900 error ("C99 designator %qE outside aggregate initializer",
5901 d->cur->index);
5902 else
5903 return true;
5905 return false;
5908 /* Subroutine of reshape_init, which processes a single initializer (part of
5909 a CONSTRUCTOR). TYPE is the type of the variable being initialized, D is the
5910 iterator within the CONSTRUCTOR which points to the initializer to process.
5911 FIRST_INITIALIZER_P is true if this is the first initializer of the
5912 outermost CONSTRUCTOR node. */
5914 static tree
5915 reshape_init_r (tree type, reshape_iter *d, bool first_initializer_p,
5916 tsubst_flags_t complain)
5918 tree init = d->cur->value;
5920 if (error_operand_p (init))
5921 return error_mark_node;
5923 if (first_initializer_p && !CP_AGGREGATE_TYPE_P (type)
5924 && has_designator_problem (d, complain))
5925 return error_mark_node;
5927 if (TREE_CODE (type) == COMPLEX_TYPE)
5929 /* A complex type can be initialized from one or two initializers,
5930 but braces are not elided. */
5931 d->cur++;
5932 if (BRACE_ENCLOSED_INITIALIZER_P (init))
5934 if (CONSTRUCTOR_NELTS (init) > 2)
5936 if (complain & tf_error)
5937 error ("too many initializers for %qT", type);
5938 else
5939 return error_mark_node;
5942 else if (first_initializer_p && d->cur != d->end)
5944 vec<constructor_elt, va_gc> *v = 0;
5945 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, init);
5946 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, d->cur->value);
5947 if (has_designator_problem (d, complain))
5948 return error_mark_node;
5949 d->cur++;
5950 init = build_constructor (init_list_type_node, v);
5952 return init;
5955 /* A non-aggregate type is always initialized with a single
5956 initializer. */
5957 if (!CP_AGGREGATE_TYPE_P (type))
5959 /* It is invalid to initialize a non-aggregate type with a
5960 brace-enclosed initializer before C++0x.
5961 We need to check for BRACE_ENCLOSED_INITIALIZER_P here because
5962 of g++.old-deja/g++.mike/p7626.C: a pointer-to-member constant is
5963 a CONSTRUCTOR (with a record type). */
5964 if (TREE_CODE (init) == CONSTRUCTOR
5965 /* Don't complain about a capture-init. */
5966 && !CONSTRUCTOR_IS_DIRECT_INIT (init)
5967 && BRACE_ENCLOSED_INITIALIZER_P (init)) /* p7626.C */
5969 if (SCALAR_TYPE_P (type))
5971 if (cxx_dialect < cxx11
5972 /* Isn't value-initialization. */
5973 || CONSTRUCTOR_NELTS (init) > 0)
5975 if (complain & tf_error)
5976 error ("braces around scalar initializer for type %qT",
5977 type);
5978 init = error_mark_node;
5981 else
5982 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
5985 d->cur++;
5986 return init;
5989 /* "If T is a class type and the initializer list has a single element of
5990 type cv U, where U is T or a class derived from T, the object is
5991 initialized from that element." Even if T is an aggregate. */
5992 if (cxx_dialect >= cxx11 && (CLASS_TYPE_P (type) || VECTOR_TYPE_P (type))
5993 && first_initializer_p
5994 && d->end - d->cur == 1
5995 && reference_related_p (type, TREE_TYPE (init)))
5997 d->cur++;
5998 return init;
6001 /* [dcl.init.aggr]
6003 All implicit type conversions (clause _conv_) are considered when
6004 initializing the aggregate member with an initializer from an
6005 initializer-list. If the initializer can initialize a member,
6006 the member is initialized. Otherwise, if the member is itself a
6007 non-empty subaggregate, brace elision is assumed and the
6008 initializer is considered for the initialization of the first
6009 member of the subaggregate. */
6010 if (TREE_CODE (init) != CONSTRUCTOR
6011 /* But don't try this for the first initializer, since that would be
6012 looking through the outermost braces; A a2 = { a1 }; is not a
6013 valid aggregate initialization. */
6014 && !first_initializer_p
6015 && (same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (init))
6016 || can_convert_arg (type, TREE_TYPE (init), init, LOOKUP_NORMAL,
6017 complain)))
6019 d->cur++;
6020 return init;
6023 /* [dcl.init.string]
6025 A char array (whether plain char, signed char, or unsigned char)
6026 can be initialized by a string-literal (optionally enclosed in
6027 braces); a wchar_t array can be initialized by a wide
6028 string-literal (optionally enclosed in braces). */
6029 if (TREE_CODE (type) == ARRAY_TYPE
6030 && char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type))))
6032 tree str_init = init;
6034 /* Strip one level of braces if and only if they enclose a single
6035 element (as allowed by [dcl.init.string]). */
6036 if (!first_initializer_p
6037 && TREE_CODE (str_init) == CONSTRUCTOR
6038 && CONSTRUCTOR_NELTS (str_init) == 1)
6040 str_init = (*CONSTRUCTOR_ELTS (str_init))[0].value;
6043 /* If it's a string literal, then it's the initializer for the array
6044 as a whole. Otherwise, continue with normal initialization for
6045 array types (one value per array element). */
6046 if (TREE_CODE (str_init) == STRING_CST)
6048 if (has_designator_problem (d, complain))
6049 return error_mark_node;
6050 d->cur++;
6051 return str_init;
6055 /* The following cases are about aggregates. If we are not within a full
6056 initializer already, and there is not a CONSTRUCTOR, it means that there
6057 is a missing set of braces (that is, we are processing the case for
6058 which reshape_init exists). */
6059 if (!first_initializer_p)
6061 if (TREE_CODE (init) == CONSTRUCTOR)
6063 if (TREE_TYPE (init) && TYPE_PTRMEMFUNC_P (TREE_TYPE (init)))
6064 /* There is no need to reshape pointer-to-member function
6065 initializers, as they are always constructed correctly
6066 by the front end. */
6068 else if (COMPOUND_LITERAL_P (init))
6069 /* For a nested compound literal, there is no need to reshape since
6070 brace elision is not allowed. Even if we decided to allow it,
6071 we should add a call to reshape_init in finish_compound_literal,
6072 before calling digest_init, so changing this code would still
6073 not be necessary. */
6074 gcc_assert (!BRACE_ENCLOSED_INITIALIZER_P (init));
6075 else
6077 ++d->cur;
6078 gcc_assert (BRACE_ENCLOSED_INITIALIZER_P (init));
6079 return reshape_init (type, init, complain);
6083 if (complain & tf_warning)
6084 warning (OPT_Wmissing_braces,
6085 "missing braces around initializer for %qT",
6086 type);
6089 /* Dispatch to specialized routines. */
6090 if (CLASS_TYPE_P (type))
6091 return reshape_init_class (type, d, first_initializer_p, complain);
6092 else if (TREE_CODE (type) == ARRAY_TYPE)
6093 return reshape_init_array (type, d, complain);
6094 else if (VECTOR_TYPE_P (type))
6095 return reshape_init_vector (type, d, complain);
6096 else
6097 gcc_unreachable();
6100 /* Undo the brace-elision allowed by [dcl.init.aggr] in a
6101 brace-enclosed aggregate initializer.
6103 INIT is the CONSTRUCTOR containing the list of initializers describing
6104 a brace-enclosed initializer for an entity of the indicated aggregate TYPE.
6105 It may not presently match the shape of the TYPE; for example:
6107 struct S { int a; int b; };
6108 struct S a[] = { 1, 2, 3, 4 };
6110 Here INIT will hold a vector of four elements, rather than a
6111 vector of two elements, each itself a vector of two elements. This
6112 routine transforms INIT from the former form into the latter. The
6113 revised CONSTRUCTOR node is returned. */
6115 tree
6116 reshape_init (tree type, tree init, tsubst_flags_t complain)
6118 vec<constructor_elt, va_gc> *v;
6119 reshape_iter d;
6120 tree new_init;
6122 gcc_assert (BRACE_ENCLOSED_INITIALIZER_P (init));
6124 v = CONSTRUCTOR_ELTS (init);
6126 /* An empty constructor does not need reshaping, and it is always a valid
6127 initializer. */
6128 if (vec_safe_is_empty (v))
6129 return init;
6131 /* Handle [dcl.init.list] direct-list-initialization from
6132 single element of enumeration with a fixed underlying type. */
6133 if (is_direct_enum_init (type, init))
6135 tree elt = CONSTRUCTOR_ELT (init, 0)->value;
6136 type = cv_unqualified (type);
6137 if (check_narrowing (ENUM_UNDERLYING_TYPE (type), elt, complain))
6139 warning_sentinel w (warn_useless_cast);
6140 warning_sentinel w2 (warn_ignored_qualifiers);
6141 return cp_build_c_cast (type, elt, tf_warning_or_error);
6143 else
6144 return error_mark_node;
6147 /* Recurse on this CONSTRUCTOR. */
6148 d.cur = &(*v)[0];
6149 d.end = d.cur + v->length ();
6151 new_init = reshape_init_r (type, &d, true, complain);
6152 if (new_init == error_mark_node)
6153 return error_mark_node;
6155 /* Make sure all the element of the constructor were used. Otherwise,
6156 issue an error about exceeding initializers. */
6157 if (d.cur != d.end)
6159 if (complain & tf_error)
6160 error ("too many initializers for %qT", type);
6161 return error_mark_node;
6164 if (CONSTRUCTOR_IS_DIRECT_INIT (init)
6165 && BRACE_ENCLOSED_INITIALIZER_P (new_init))
6166 CONSTRUCTOR_IS_DIRECT_INIT (new_init) = true;
6168 return new_init;
6171 /* Verify array initializer. Returns true if errors have been reported. */
6173 bool
6174 check_array_initializer (tree decl, tree type, tree init)
6176 tree element_type = TREE_TYPE (type);
6178 /* The array type itself need not be complete, because the
6179 initializer may tell us how many elements are in the array.
6180 But, the elements of the array must be complete. */
6181 if (!COMPLETE_TYPE_P (complete_type (element_type)))
6183 if (decl)
6184 error_at (DECL_SOURCE_LOCATION (decl),
6185 "elements of array %q#D have incomplete type", decl);
6186 else
6187 error ("elements of array %q#T have incomplete type", type);
6188 return true;
6190 /* A compound literal can't have variable size. */
6191 if (init && !decl
6192 && ((COMPLETE_TYPE_P (type) && !TREE_CONSTANT (TYPE_SIZE (type)))
6193 || !TREE_CONSTANT (TYPE_SIZE (element_type))))
6195 error ("variable-sized compound literal");
6196 return true;
6198 return false;
6201 /* Subroutine of check_initializer; args are passed down from that function.
6202 Set stmts_are_full_exprs_p to 1 across a call to build_aggr_init. */
6204 static tree
6205 build_aggr_init_full_exprs (tree decl, tree init, int flags)
6208 gcc_assert (stmts_are_full_exprs_p ());
6209 return build_aggr_init (decl, init, flags, tf_warning_or_error);
6212 /* Verify INIT (the initializer for DECL), and record the
6213 initialization in DECL_INITIAL, if appropriate. CLEANUP is as for
6214 grok_reference_init.
6216 If the return value is non-NULL, it is an expression that must be
6217 evaluated dynamically to initialize DECL. */
6219 static tree
6220 check_initializer (tree decl, tree init, int flags, vec<tree, va_gc> **cleanups)
6222 tree type = TREE_TYPE (decl);
6223 tree init_code = NULL;
6224 tree core_type;
6226 /* Things that are going to be initialized need to have complete
6227 type. */
6228 TREE_TYPE (decl) = type = complete_type (TREE_TYPE (decl));
6230 if (DECL_HAS_VALUE_EXPR_P (decl))
6232 /* A variable with DECL_HAS_VALUE_EXPR_P set is just a placeholder,
6233 it doesn't have storage to be initialized. */
6234 gcc_assert (init == NULL_TREE);
6235 return NULL_TREE;
6238 if (type == error_mark_node)
6239 /* We will have already complained. */
6240 return NULL_TREE;
6242 if (TREE_CODE (type) == ARRAY_TYPE)
6244 if (check_array_initializer (decl, type, init))
6245 return NULL_TREE;
6247 else if (!COMPLETE_TYPE_P (type))
6249 error_at (DECL_SOURCE_LOCATION (decl),
6250 "%q#D has incomplete type", decl);
6251 TREE_TYPE (decl) = error_mark_node;
6252 return NULL_TREE;
6254 else
6255 /* There is no way to make a variable-sized class type in GNU C++. */
6256 gcc_assert (TREE_CONSTANT (TYPE_SIZE (type)));
6258 if (init && BRACE_ENCLOSED_INITIALIZER_P (init))
6260 int init_len = CONSTRUCTOR_NELTS (init);
6261 if (SCALAR_TYPE_P (type))
6263 if (init_len == 0)
6265 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6266 init = build_zero_init (type, NULL_TREE, false);
6268 else if (init_len != 1 && TREE_CODE (type) != COMPLEX_TYPE)
6270 error_at (cp_expr_loc_or_loc (init, DECL_SOURCE_LOCATION (decl)),
6271 "scalar object %qD requires one element in "
6272 "initializer", decl);
6273 TREE_TYPE (decl) = error_mark_node;
6274 return NULL_TREE;
6279 if (TREE_CODE (decl) == CONST_DECL)
6281 gcc_assert (!TYPE_REF_P (type));
6283 DECL_INITIAL (decl) = init;
6285 gcc_assert (init != NULL_TREE);
6286 init = NULL_TREE;
6288 else if (!init && DECL_REALLY_EXTERN (decl))
6290 else if (init || type_build_ctor_call (type)
6291 || TYPE_REF_P (type))
6293 if (TYPE_REF_P (type))
6295 init = grok_reference_init (decl, type, init, flags);
6296 flags |= LOOKUP_ALREADY_DIGESTED;
6298 else if (!init)
6299 check_for_uninitialized_const_var (decl, /*constexpr_context_p=*/false,
6300 tf_warning_or_error);
6301 /* Do not reshape constructors of vectors (they don't need to be
6302 reshaped. */
6303 else if (BRACE_ENCLOSED_INITIALIZER_P (init))
6305 if (is_std_init_list (type))
6307 init = perform_implicit_conversion (type, init,
6308 tf_warning_or_error);
6309 flags |= LOOKUP_ALREADY_DIGESTED;
6311 else if (TYPE_NON_AGGREGATE_CLASS (type))
6313 /* Don't reshape if the class has constructors. */
6314 if (cxx_dialect == cxx98)
6315 error_at (cp_expr_loc_or_loc (init, DECL_SOURCE_LOCATION (decl)),
6316 "in C++98 %qD must be initialized by "
6317 "constructor, not by %<{...}%>",
6318 decl);
6320 else if (VECTOR_TYPE_P (type) && TYPE_VECTOR_OPAQUE (type))
6322 error ("opaque vector types cannot be initialized");
6323 init = error_mark_node;
6325 else
6327 init = reshape_init (type, init, tf_warning_or_error);
6328 flags |= LOOKUP_NO_NARROWING;
6331 else if (TREE_CODE (init) == TREE_LIST
6332 && TREE_TYPE (init) != unknown_type_node
6333 && !MAYBE_CLASS_TYPE_P (type))
6335 gcc_assert (TREE_CODE (decl) != RESULT_DECL);
6337 /* We get here with code like `int a (2);' */
6338 init = build_x_compound_expr_from_list (init, ELK_INIT,
6339 tf_warning_or_error);
6342 /* If DECL has an array type without a specific bound, deduce the
6343 array size from the initializer. */
6344 maybe_deduce_size_from_array_init (decl, init);
6345 type = TREE_TYPE (decl);
6346 if (type == error_mark_node)
6347 return NULL_TREE;
6349 if (((type_build_ctor_call (type) || CLASS_TYPE_P (type))
6350 && !(flags & LOOKUP_ALREADY_DIGESTED)
6351 && !(init && BRACE_ENCLOSED_INITIALIZER_P (init)
6352 && CP_AGGREGATE_TYPE_P (type)
6353 && (CLASS_TYPE_P (type)
6354 || !TYPE_NEEDS_CONSTRUCTING (type)
6355 || type_has_extended_temps (type))))
6356 || (DECL_DECOMPOSITION_P (decl) && TREE_CODE (type) == ARRAY_TYPE))
6358 init_code = build_aggr_init_full_exprs (decl, init, flags);
6360 /* A constructor call is a non-trivial initializer even if
6361 it isn't explicitly written. */
6362 if (TREE_SIDE_EFFECTS (init_code))
6363 DECL_NONTRIVIALLY_INITIALIZED_P (decl) = true;
6365 /* If this is a constexpr initializer, expand_default_init will
6366 have returned an INIT_EXPR rather than a CALL_EXPR. In that
6367 case, pull the initializer back out and pass it down into
6368 store_init_value. */
6369 while (TREE_CODE (init_code) == EXPR_STMT
6370 || TREE_CODE (init_code) == CONVERT_EXPR)
6371 init_code = TREE_OPERAND (init_code, 0);
6372 if (TREE_CODE (init_code) == INIT_EXPR)
6374 init = TREE_OPERAND (init_code, 1);
6375 init_code = NULL_TREE;
6376 /* Don't call digest_init; it's unnecessary and will complain
6377 about aggregate initialization of non-aggregate classes. */
6378 flags |= LOOKUP_ALREADY_DIGESTED;
6380 else if (DECL_DECLARED_CONSTEXPR_P (decl))
6382 /* Declared constexpr, but no suitable initializer; massage
6383 init appropriately so we can pass it into store_init_value
6384 for the error. */
6385 if (CLASS_TYPE_P (type)
6386 && (!init || TREE_CODE (init) == TREE_LIST))
6388 init = build_functional_cast (type, init, tf_none);
6389 if (TREE_CODE (init) == TARGET_EXPR)
6390 TARGET_EXPR_DIRECT_INIT_P (init) = true;
6392 init_code = NULL_TREE;
6394 else
6395 init = NULL_TREE;
6398 if (init && TREE_CODE (init) != TREE_VEC)
6400 /* In aggregate initialization of a variable, each element
6401 initialization is a full-expression because there is no
6402 enclosing expression. */
6403 gcc_assert (stmts_are_full_exprs_p ());
6405 init_code = store_init_value (decl, init, cleanups, flags);
6407 if (pedantic && TREE_CODE (type) == ARRAY_TYPE
6408 && DECL_INITIAL (decl)
6409 && TREE_CODE (DECL_INITIAL (decl)) == STRING_CST
6410 && PAREN_STRING_LITERAL_P (DECL_INITIAL (decl)))
6411 warning_at (cp_expr_loc_or_loc (DECL_INITIAL (decl),
6412 DECL_SOURCE_LOCATION (decl)),
6413 0, "array %qD initialized by parenthesized "
6414 "string literal %qE",
6415 decl, DECL_INITIAL (decl));
6416 init = NULL;
6419 else
6421 if (CLASS_TYPE_P (core_type = strip_array_types (type))
6422 && (CLASSTYPE_READONLY_FIELDS_NEED_INIT (core_type)
6423 || CLASSTYPE_REF_FIELDS_NEED_INIT (core_type)))
6424 diagnose_uninitialized_cst_or_ref_member (core_type, /*using_new=*/false,
6425 /*complain=*/true);
6427 check_for_uninitialized_const_var (decl, /*constexpr_context_p=*/false,
6428 tf_warning_or_error);
6431 if (init && init != error_mark_node)
6432 init_code = build2 (INIT_EXPR, type, decl, init);
6434 if (init_code)
6436 /* We might have set these in cp_finish_decl. */
6437 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = false;
6438 TREE_CONSTANT (decl) = false;
6441 if (init_code
6442 && (DECL_IN_AGGR_P (decl)
6443 && DECL_INITIALIZED_IN_CLASS_P (decl)
6444 && !DECL_VAR_DECLARED_INLINE_P (decl)))
6446 static int explained = 0;
6448 if (cxx_dialect < cxx11)
6449 error ("initializer invalid for static member with constructor");
6450 else if (cxx_dialect < cxx17)
6451 error ("non-constant in-class initialization invalid for static "
6452 "member %qD", decl);
6453 else
6454 error ("non-constant in-class initialization invalid for non-inline "
6455 "static member %qD", decl);
6456 if (!explained)
6458 inform (input_location,
6459 "(an out of class initialization is required)");
6460 explained = 1;
6462 return NULL_TREE;
6465 return init_code;
6468 /* If DECL is not a local variable, give it RTL. */
6470 static void
6471 make_rtl_for_nonlocal_decl (tree decl, tree init, const char* asmspec)
6473 int toplev = toplevel_bindings_p ();
6474 int defer_p;
6476 /* Set the DECL_ASSEMBLER_NAME for the object. */
6477 if (asmspec)
6479 /* The `register' keyword, when used together with an
6480 asm-specification, indicates that the variable should be
6481 placed in a particular register. */
6482 if (VAR_P (decl) && DECL_REGISTER (decl))
6484 set_user_assembler_name (decl, asmspec);
6485 DECL_HARD_REGISTER (decl) = 1;
6487 else
6489 if (TREE_CODE (decl) == FUNCTION_DECL
6490 && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
6491 set_builtin_user_assembler_name (decl, asmspec);
6492 set_user_assembler_name (decl, asmspec);
6496 /* Handle non-variables up front. */
6497 if (!VAR_P (decl))
6499 rest_of_decl_compilation (decl, toplev, at_eof);
6500 return;
6503 /* If we see a class member here, it should be a static data
6504 member. */
6505 if (DECL_LANG_SPECIFIC (decl) && DECL_IN_AGGR_P (decl))
6507 gcc_assert (TREE_STATIC (decl));
6508 /* An in-class declaration of a static data member should be
6509 external; it is only a declaration, and not a definition. */
6510 if (init == NULL_TREE)
6511 gcc_assert (DECL_EXTERNAL (decl)
6512 || !TREE_PUBLIC (decl)
6513 || DECL_INLINE_VAR_P (decl));
6516 /* We don't create any RTL for local variables. */
6517 if (DECL_FUNCTION_SCOPE_P (decl) && !TREE_STATIC (decl))
6518 return;
6520 /* We defer emission of local statics until the corresponding
6521 DECL_EXPR is expanded. But with constexpr its function might never
6522 be expanded, so go ahead and tell cgraph about the variable now. */
6523 defer_p = ((DECL_FUNCTION_SCOPE_P (decl)
6524 && !var_in_maybe_constexpr_fn (decl))
6525 || DECL_VIRTUAL_P (decl));
6527 /* Defer template instantiations. */
6528 if (DECL_LANG_SPECIFIC (decl)
6529 && DECL_IMPLICIT_INSTANTIATION (decl))
6530 defer_p = 1;
6532 /* If we're not deferring, go ahead and assemble the variable. */
6533 if (!defer_p)
6534 rest_of_decl_compilation (decl, toplev, at_eof);
6537 /* walk_tree helper for wrap_temporary_cleanups, below. */
6539 static tree
6540 wrap_cleanups_r (tree *stmt_p, int *walk_subtrees, void *data)
6542 /* Stop at types or full-expression boundaries. */
6543 if (TYPE_P (*stmt_p)
6544 || TREE_CODE (*stmt_p) == CLEANUP_POINT_EXPR)
6546 *walk_subtrees = 0;
6547 return NULL_TREE;
6550 if (TREE_CODE (*stmt_p) == TARGET_EXPR)
6552 tree guard = (tree)data;
6553 tree tcleanup = TARGET_EXPR_CLEANUP (*stmt_p);
6555 tcleanup = build2 (TRY_CATCH_EXPR, void_type_node, tcleanup, guard);
6556 /* Tell honor_protect_cleanup_actions to handle this as a separate
6557 cleanup. */
6558 TRY_CATCH_IS_CLEANUP (tcleanup) = 1;
6560 TARGET_EXPR_CLEANUP (*stmt_p) = tcleanup;
6563 return NULL_TREE;
6566 /* We're initializing a local variable which has a cleanup GUARD. If there
6567 are any temporaries used in the initializer INIT of this variable, we
6568 need to wrap their cleanups with TRY_CATCH_EXPR (, GUARD) so that the
6569 variable will be cleaned up properly if one of them throws.
6571 Unfortunately, there's no way to express this properly in terms of
6572 nesting, as the regions for the temporaries overlap the region for the
6573 variable itself; if there are two temporaries, the variable needs to be
6574 the first thing destroyed if either of them throws. However, we only
6575 want to run the variable's cleanup if it actually got constructed. So
6576 we need to guard the temporary cleanups with the variable's cleanup if
6577 they are run on the normal path, but not if they are run on the
6578 exceptional path. We implement this by telling
6579 honor_protect_cleanup_actions to strip the variable cleanup from the
6580 exceptional path. */
6582 static void
6583 wrap_temporary_cleanups (tree init, tree guard)
6585 cp_walk_tree_without_duplicates (&init, wrap_cleanups_r, (void *)guard);
6588 /* Generate code to initialize DECL (a local variable). */
6590 static void
6591 initialize_local_var (tree decl, tree init)
6593 tree type = TREE_TYPE (decl);
6594 tree cleanup;
6595 int already_used;
6597 gcc_assert (VAR_P (decl)
6598 || TREE_CODE (decl) == RESULT_DECL);
6599 gcc_assert (!TREE_STATIC (decl));
6601 if (DECL_SIZE (decl) == NULL_TREE)
6603 /* If we used it already as memory, it must stay in memory. */
6604 DECL_INITIAL (decl) = NULL_TREE;
6605 TREE_ADDRESSABLE (decl) = TREE_USED (decl);
6606 return;
6609 if (type == error_mark_node)
6610 return;
6612 /* Compute and store the initial value. */
6613 already_used = TREE_USED (decl) || TREE_USED (type);
6614 if (TREE_USED (type))
6615 DECL_READ_P (decl) = 1;
6617 /* Generate a cleanup, if necessary. */
6618 cleanup = cxx_maybe_build_cleanup (decl, tf_warning_or_error);
6620 /* Perform the initialization. */
6621 if (init)
6623 tree rinit = (TREE_CODE (init) == INIT_EXPR
6624 ? TREE_OPERAND (init, 1) : NULL_TREE);
6625 if (rinit && !TREE_SIDE_EFFECTS (rinit))
6627 /* Stick simple initializers in DECL_INITIAL so that
6628 -Wno-init-self works (c++/34772). */
6629 gcc_assert (TREE_OPERAND (init, 0) == decl);
6630 DECL_INITIAL (decl) = rinit;
6632 if (warn_init_self && TYPE_REF_P (type))
6634 STRIP_NOPS (rinit);
6635 if (rinit == decl)
6636 warning_at (DECL_SOURCE_LOCATION (decl),
6637 OPT_Winit_self,
6638 "reference %qD is initialized with itself", decl);
6641 else
6643 int saved_stmts_are_full_exprs_p;
6645 /* If we're only initializing a single object, guard the
6646 destructors of any temporaries used in its initializer with
6647 its destructor. This isn't right for arrays because each
6648 element initialization is a full-expression. */
6649 if (cleanup && TREE_CODE (type) != ARRAY_TYPE)
6650 wrap_temporary_cleanups (init, cleanup);
6652 gcc_assert (building_stmt_list_p ());
6653 saved_stmts_are_full_exprs_p = stmts_are_full_exprs_p ();
6654 current_stmt_tree ()->stmts_are_full_exprs_p = 1;
6655 finish_expr_stmt (init);
6656 current_stmt_tree ()->stmts_are_full_exprs_p =
6657 saved_stmts_are_full_exprs_p;
6661 /* Set this to 0 so we can tell whether an aggregate which was
6662 initialized was ever used. Don't do this if it has a
6663 destructor, so we don't complain about the 'resource
6664 allocation is initialization' idiom. Now set
6665 attribute((unused)) on types so decls of that type will be
6666 marked used. (see TREE_USED, above.) */
6667 if (TYPE_NEEDS_CONSTRUCTING (type)
6668 && ! already_used
6669 && TYPE_HAS_TRIVIAL_DESTRUCTOR (type)
6670 && DECL_NAME (decl))
6671 TREE_USED (decl) = 0;
6672 else if (already_used)
6673 TREE_USED (decl) = 1;
6675 if (cleanup)
6676 finish_decl_cleanup (decl, cleanup);
6679 /* DECL is a VAR_DECL for a compiler-generated variable with static
6680 storage duration (like a virtual table) whose initializer is a
6681 compile-time constant. Initialize the variable and provide it to the
6682 back end. */
6684 void
6685 initialize_artificial_var (tree decl, vec<constructor_elt, va_gc> *v)
6687 tree init;
6688 gcc_assert (DECL_ARTIFICIAL (decl));
6689 init = build_constructor (TREE_TYPE (decl), v);
6690 gcc_assert (TREE_CODE (init) == CONSTRUCTOR);
6691 DECL_INITIAL (decl) = init;
6692 DECL_INITIALIZED_P (decl) = 1;
6693 determine_visibility (decl);
6694 layout_var_decl (decl);
6695 maybe_commonize_var (decl);
6696 make_rtl_for_nonlocal_decl (decl, init, /*asmspec=*/NULL);
6699 /* INIT is the initializer for a variable, as represented by the
6700 parser. Returns true iff INIT is value-dependent. */
6702 static bool
6703 value_dependent_init_p (tree init)
6705 if (TREE_CODE (init) == TREE_LIST)
6706 /* A parenthesized initializer, e.g.: int i (3, 2); ? */
6707 return any_value_dependent_elements_p (init);
6708 else if (TREE_CODE (init) == CONSTRUCTOR)
6709 /* A brace-enclosed initializer, e.g.: int i = { 3 }; ? */
6711 if (dependent_type_p (TREE_TYPE (init)))
6712 return true;
6714 vec<constructor_elt, va_gc> *elts;
6715 size_t nelts;
6716 size_t i;
6718 elts = CONSTRUCTOR_ELTS (init);
6719 nelts = vec_safe_length (elts);
6720 for (i = 0; i < nelts; ++i)
6721 if (value_dependent_init_p ((*elts)[i].value))
6722 return true;
6724 else
6725 /* It must be a simple expression, e.g., int i = 3; */
6726 return value_dependent_expression_p (init);
6728 return false;
6731 // Returns true if a DECL is VAR_DECL with the concept specifier.
6732 static inline bool
6733 is_concept_var (tree decl)
6735 return (VAR_P (decl)
6736 // Not all variables have DECL_LANG_SPECIFIC.
6737 && DECL_LANG_SPECIFIC (decl)
6738 && DECL_DECLARED_CONCEPT_P (decl));
6741 /* A helper function to be called via walk_tree. If any label exists
6742 under *TP, it is (going to be) forced. Set has_forced_label_in_static. */
6744 static tree
6745 notice_forced_label_r (tree *tp, int *walk_subtrees, void *)
6747 if (TYPE_P (*tp))
6748 *walk_subtrees = 0;
6749 if (TREE_CODE (*tp) == LABEL_DECL)
6750 cfun->has_forced_label_in_static = 1;
6751 return NULL_TREE;
6754 /* Finish processing of a declaration;
6755 install its line number and initial value.
6756 If the length of an array type is not known before,
6757 it must be determined now, from the initial value, or it is an error.
6759 INIT is the initializer (if any) for DECL. If INIT_CONST_EXPR_P is
6760 true, then INIT is an integral constant expression.
6762 FLAGS is LOOKUP_ONLYCONVERTING if the = init syntax was used, else 0
6763 if the (init) syntax was used. */
6765 void
6766 cp_finish_decl (tree decl, tree init, bool init_const_expr_p,
6767 tree asmspec_tree, int flags)
6769 tree type;
6770 vec<tree, va_gc> *cleanups = NULL;
6771 const char *asmspec = NULL;
6772 int was_readonly = 0;
6773 bool var_definition_p = false;
6774 tree auto_node;
6776 if (decl == error_mark_node)
6777 return;
6778 else if (! decl)
6780 if (init)
6781 error ("assignment (not initialization) in declaration");
6782 return;
6785 gcc_assert (TREE_CODE (decl) != RESULT_DECL);
6786 /* Parameters are handled by store_parm_decls, not cp_finish_decl. */
6787 gcc_assert (TREE_CODE (decl) != PARM_DECL);
6789 type = TREE_TYPE (decl);
6790 if (type == error_mark_node)
6791 return;
6793 /* Warn about register storage specifiers except when in GNU global
6794 or local register variable extension. */
6795 if (VAR_P (decl) && DECL_REGISTER (decl) && asmspec_tree == NULL_TREE)
6797 if (cxx_dialect >= cxx17)
6798 pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wregister,
6799 "ISO C++17 does not allow %<register%> storage "
6800 "class specifier");
6801 else
6802 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wregister,
6803 "%<register%> storage class specifier used");
6806 /* If a name was specified, get the string. */
6807 if (at_namespace_scope_p ())
6808 asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
6809 if (asmspec_tree && asmspec_tree != error_mark_node)
6810 asmspec = TREE_STRING_POINTER (asmspec_tree);
6812 if (current_class_type
6813 && CP_DECL_CONTEXT (decl) == current_class_type
6814 && TYPE_BEING_DEFINED (current_class_type)
6815 && !CLASSTYPE_TEMPLATE_INSTANTIATION (current_class_type)
6816 && (DECL_INITIAL (decl) || init))
6817 DECL_INITIALIZED_IN_CLASS_P (decl) = 1;
6819 /* Do auto deduction unless decl is a function or an uninstantiated
6820 template specialization. */
6821 if (TREE_CODE (decl) != FUNCTION_DECL
6822 && !(init == NULL_TREE
6823 && DECL_LANG_SPECIFIC (decl)
6824 && DECL_TEMPLATE_INSTANTIATION (decl)
6825 && !DECL_TEMPLATE_INSTANTIATED (decl))
6826 && (auto_node = type_uses_auto (type)))
6828 tree d_init;
6829 if (init == NULL_TREE)
6830 gcc_assert (CLASS_PLACEHOLDER_TEMPLATE (auto_node));
6831 d_init = init;
6832 if (d_init)
6834 if (TREE_CODE (d_init) == TREE_LIST
6835 && !CLASS_PLACEHOLDER_TEMPLATE (auto_node))
6836 d_init = build_x_compound_expr_from_list (d_init, ELK_INIT,
6837 tf_warning_or_error);
6838 d_init = resolve_nondeduced_context (d_init, tf_warning_or_error);
6840 enum auto_deduction_context adc = adc_variable_type;
6841 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
6842 adc = adc_decomp_type;
6843 type = TREE_TYPE (decl) = do_auto_deduction (type, d_init, auto_node,
6844 tf_warning_or_error, adc,
6845 NULL_TREE, flags);
6846 if (type == error_mark_node)
6847 return;
6848 if (TREE_CODE (type) == FUNCTION_TYPE)
6850 error ("initializer for %<decltype(auto) %D%> has function type "
6851 "(did you forget the %<()%> ?)", decl);
6852 TREE_TYPE (decl) = error_mark_node;
6853 return;
6855 cp_apply_type_quals_to_decl (cp_type_quals (type), decl);
6858 if (ensure_literal_type_for_constexpr_object (decl) == error_mark_node)
6860 DECL_DECLARED_CONSTEXPR_P (decl) = 0;
6861 if (VAR_P (decl) && DECL_CLASS_SCOPE_P (decl))
6863 init = NULL_TREE;
6864 DECL_EXTERNAL (decl) = 1;
6868 if (VAR_P (decl)
6869 && DECL_CLASS_SCOPE_P (decl)
6870 && DECL_INITIALIZED_IN_CLASS_P (decl))
6871 check_static_variable_definition (decl, type);
6873 if (init && TREE_CODE (decl) == FUNCTION_DECL)
6875 tree clone;
6876 if (init == ridpointers[(int)RID_DELETE])
6878 /* FIXME check this is 1st decl. */
6879 DECL_DELETED_FN (decl) = 1;
6880 DECL_DECLARED_INLINE_P (decl) = 1;
6881 DECL_INITIAL (decl) = error_mark_node;
6882 FOR_EACH_CLONE (clone, decl)
6884 DECL_DELETED_FN (clone) = 1;
6885 DECL_DECLARED_INLINE_P (clone) = 1;
6886 DECL_INITIAL (clone) = error_mark_node;
6888 init = NULL_TREE;
6890 else if (init == ridpointers[(int)RID_DEFAULT])
6892 if (defaultable_fn_check (decl))
6893 DECL_DEFAULTED_FN (decl) = 1;
6894 else
6895 DECL_INITIAL (decl) = NULL_TREE;
6899 if (init && VAR_P (decl))
6901 DECL_NONTRIVIALLY_INITIALIZED_P (decl) = 1;
6902 /* If DECL is a reference, then we want to know whether init is a
6903 reference constant; init_const_expr_p as passed tells us whether
6904 it's an rvalue constant. */
6905 if (TYPE_REF_P (type))
6906 init_const_expr_p = potential_constant_expression (init);
6907 if (init_const_expr_p)
6909 /* Set these flags now for templates. We'll update the flags in
6910 store_init_value for instantiations. */
6911 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1;
6912 if (decl_maybe_constant_var_p (decl)
6913 /* FIXME setting TREE_CONSTANT on refs breaks the back end. */
6914 && !TYPE_REF_P (type))
6915 TREE_CONSTANT (decl) = 1;
6919 if (processing_template_decl)
6921 bool type_dependent_p;
6923 /* Add this declaration to the statement-tree. */
6924 if (at_function_scope_p ())
6925 add_decl_expr (decl);
6927 type_dependent_p = dependent_type_p (type);
6929 if (check_for_bare_parameter_packs (init))
6931 init = NULL_TREE;
6932 DECL_INITIAL (decl) = NULL_TREE;
6935 /* Generally, initializers in templates are expanded when the
6936 template is instantiated. But, if DECL is a variable constant
6937 then it can be used in future constant expressions, so its value
6938 must be available. */
6940 bool dep_init = false;
6942 if (!VAR_P (decl) || type_dependent_p)
6943 /* We can't do anything if the decl has dependent type. */;
6944 else if (!init && is_concept_var (decl))
6946 error ("variable concept has no initializer");
6947 init = boolean_true_node;
6949 else if (init
6950 && init_const_expr_p
6951 && !TYPE_REF_P (type)
6952 && decl_maybe_constant_var_p (decl)
6953 && !(dep_init = value_dependent_init_p (init)))
6955 /* This variable seems to be a non-dependent constant, so process
6956 its initializer. If check_initializer returns non-null the
6957 initialization wasn't constant after all. */
6958 tree init_code;
6959 cleanups = make_tree_vector ();
6960 init_code = check_initializer (decl, init, flags, &cleanups);
6961 if (init_code == NULL_TREE)
6962 init = NULL_TREE;
6963 release_tree_vector (cleanups);
6965 else if (!DECL_PRETTY_FUNCTION_P (decl))
6967 /* Deduce array size even if the initializer is dependent. */
6968 maybe_deduce_size_from_array_init (decl, init);
6969 /* And complain about multiple initializers. */
6970 if (init && TREE_CODE (init) == TREE_LIST && TREE_CHAIN (init)
6971 && !MAYBE_CLASS_TYPE_P (type))
6972 init = build_x_compound_expr_from_list (init, ELK_INIT,
6973 tf_warning_or_error);
6976 if (init)
6977 DECL_INITIAL (decl) = init;
6979 if (dep_init)
6981 retrofit_lang_decl (decl);
6982 SET_DECL_DEPENDENT_INIT_P (decl, true);
6984 return;
6987 /* Just store non-static data member initializers for later. */
6988 if (init && TREE_CODE (decl) == FIELD_DECL)
6989 DECL_INITIAL (decl) = init;
6991 /* Take care of TYPE_DECLs up front. */
6992 if (TREE_CODE (decl) == TYPE_DECL)
6994 if (type != error_mark_node
6995 && MAYBE_CLASS_TYPE_P (type) && DECL_NAME (decl))
6997 if (TREE_TYPE (DECL_NAME (decl)) && TREE_TYPE (decl) != type)
6998 warning (0, "shadowing previous type declaration of %q#D", decl);
6999 set_identifier_type_value (DECL_NAME (decl), decl);
7002 /* If we have installed this as the canonical typedef for this
7003 type, and that type has not been defined yet, delay emitting
7004 the debug information for it, as we will emit it later. */
7005 if (TYPE_MAIN_DECL (TREE_TYPE (decl)) == decl
7006 && !COMPLETE_TYPE_P (TREE_TYPE (decl)))
7007 TYPE_DECL_SUPPRESS_DEBUG (decl) = 1;
7009 rest_of_decl_compilation (decl, DECL_FILE_SCOPE_P (decl),
7010 at_eof);
7011 return;
7014 /* A reference will be modified here, as it is initialized. */
7015 if (! DECL_EXTERNAL (decl)
7016 && TREE_READONLY (decl)
7017 && TYPE_REF_P (type))
7019 was_readonly = 1;
7020 TREE_READONLY (decl) = 0;
7023 if (VAR_P (decl))
7025 /* If this is a local variable that will need a mangled name,
7026 register it now. We must do this before processing the
7027 initializer for the variable, since the initialization might
7028 require a guard variable, and since the mangled name of the
7029 guard variable will depend on the mangled name of this
7030 variable. */
7031 if (DECL_FUNCTION_SCOPE_P (decl)
7032 && TREE_STATIC (decl)
7033 && !DECL_ARTIFICIAL (decl))
7035 push_local_name (decl);
7036 /* Normally has_forced_label_in_static is set during GIMPLE
7037 lowering, but [cd]tors are never actually compiled directly.
7038 We need to set this early so we can deal with the label
7039 address extension. */
7040 if ((DECL_CONSTRUCTOR_P (current_function_decl)
7041 || DECL_DESTRUCTOR_P (current_function_decl))
7042 && init)
7044 walk_tree (&init, notice_forced_label_r, NULL, NULL);
7045 add_local_decl (cfun, decl);
7047 /* And make sure it's in the symbol table for
7048 c_parse_final_cleanups to find. */
7049 varpool_node::get_create (decl);
7052 /* Convert the initializer to the type of DECL, if we have not
7053 already initialized DECL. */
7054 if (!DECL_INITIALIZED_P (decl)
7055 /* If !DECL_EXTERNAL then DECL is being defined. In the
7056 case of a static data member initialized inside the
7057 class-specifier, there can be an initializer even if DECL
7058 is *not* defined. */
7059 && (!DECL_EXTERNAL (decl) || init))
7061 cleanups = make_tree_vector ();
7062 init = check_initializer (decl, init, flags, &cleanups);
7064 /* Handle:
7066 [dcl.init]
7068 The memory occupied by any object of static storage
7069 duration is zero-initialized at program startup before
7070 any other initialization takes place.
7072 We cannot create an appropriate initializer until after
7073 the type of DECL is finalized. If DECL_INITIAL is set,
7074 then the DECL is statically initialized, and any
7075 necessary zero-initialization has already been performed. */
7076 if (TREE_STATIC (decl) && !DECL_INITIAL (decl))
7077 DECL_INITIAL (decl) = build_zero_init (TREE_TYPE (decl),
7078 /*nelts=*/NULL_TREE,
7079 /*static_storage_p=*/true);
7080 /* Remember that the initialization for this variable has
7081 taken place. */
7082 DECL_INITIALIZED_P (decl) = 1;
7083 /* This declaration is the definition of this variable,
7084 unless we are initializing a static data member within
7085 the class specifier. */
7086 if (!DECL_EXTERNAL (decl))
7087 var_definition_p = true;
7089 /* If the variable has an array type, lay out the type, even if
7090 there is no initializer. It is valid to index through the
7091 array, and we must get TYPE_ALIGN set correctly on the array
7092 type. */
7093 else if (TREE_CODE (type) == ARRAY_TYPE)
7094 layout_type (type);
7096 if (TREE_STATIC (decl)
7097 && !at_function_scope_p ()
7098 && current_function_decl == NULL)
7099 /* So decl is a global variable or a static member of a
7100 non local class. Record the types it uses
7101 so that we can decide later to emit debug info for them. */
7102 record_types_used_by_current_var_decl (decl);
7105 /* Add this declaration to the statement-tree. This needs to happen
7106 after the call to check_initializer so that the DECL_EXPR for a
7107 reference temp is added before the DECL_EXPR for the reference itself. */
7108 if (DECL_FUNCTION_SCOPE_P (decl))
7110 /* If we're building a variable sized type, and we might be
7111 reachable other than via the top of the current binding
7112 level, then create a new BIND_EXPR so that we deallocate
7113 the object at the right time. */
7114 if (VAR_P (decl)
7115 && DECL_SIZE (decl)
7116 && !TREE_CONSTANT (DECL_SIZE (decl))
7117 && STATEMENT_LIST_HAS_LABEL (cur_stmt_list))
7119 tree bind;
7120 bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
7121 TREE_SIDE_EFFECTS (bind) = 1;
7122 add_stmt (bind);
7123 BIND_EXPR_BODY (bind) = push_stmt_list ();
7125 add_decl_expr (decl);
7128 /* Let the middle end know about variables and functions -- but not
7129 static data members in uninstantiated class templates. */
7130 if (VAR_OR_FUNCTION_DECL_P (decl))
7132 if (VAR_P (decl))
7134 layout_var_decl (decl);
7135 maybe_commonize_var (decl);
7138 /* This needs to happen after the linkage is set. */
7139 determine_visibility (decl);
7141 if (var_definition_p && TREE_STATIC (decl))
7143 /* If a TREE_READONLY variable needs initialization
7144 at runtime, it is no longer readonly and we need to
7145 avoid MEM_READONLY_P being set on RTL created for it. */
7146 if (init)
7148 if (TREE_READONLY (decl))
7149 TREE_READONLY (decl) = 0;
7150 was_readonly = 0;
7152 else if (was_readonly)
7153 TREE_READONLY (decl) = 1;
7155 /* Likewise if it needs destruction. */
7156 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
7157 TREE_READONLY (decl) = 0;
7160 make_rtl_for_nonlocal_decl (decl, init, asmspec);
7162 /* Check for abstractness of the type. Notice that there is no
7163 need to strip array types here since the check for those types
7164 is already done within create_array_type_for_decl. */
7165 abstract_virtuals_error (decl, type);
7167 if (TREE_TYPE (decl) == error_mark_node)
7168 /* No initialization required. */
7170 else if (TREE_CODE (decl) == FUNCTION_DECL)
7172 if (init)
7174 if (init == ridpointers[(int)RID_DEFAULT])
7176 /* An out-of-class default definition is defined at
7177 the point where it is explicitly defaulted. */
7178 if (DECL_DELETED_FN (decl))
7179 maybe_explain_implicit_delete (decl);
7180 else if (DECL_INITIAL (decl) == error_mark_node)
7181 synthesize_method (decl);
7183 else
7184 error ("function %q#D is initialized like a variable", decl);
7186 /* else no initialization required. */
7188 else if (DECL_EXTERNAL (decl)
7189 && ! (DECL_LANG_SPECIFIC (decl)
7190 && DECL_NOT_REALLY_EXTERN (decl)))
7192 if (init)
7193 DECL_INITIAL (decl) = init;
7195 /* A variable definition. */
7196 else if (DECL_FUNCTION_SCOPE_P (decl) && !TREE_STATIC (decl))
7197 /* Initialize the local variable. */
7198 initialize_local_var (decl, init);
7200 /* If a variable is defined, and then a subsequent
7201 definition with external linkage is encountered, we will
7202 get here twice for the same variable. We want to avoid
7203 calling expand_static_init more than once. For variables
7204 that are not static data members, we can call
7205 expand_static_init only when we actually process the
7206 initializer. It is not legal to redeclare a static data
7207 member, so this issue does not arise in that case. */
7208 else if (var_definition_p && TREE_STATIC (decl))
7209 expand_static_init (decl, init);
7212 /* If a CLEANUP_STMT was created to destroy a temporary bound to a
7213 reference, insert it in the statement-tree now. */
7214 if (cleanups)
7216 unsigned i; tree t;
7217 FOR_EACH_VEC_ELT (*cleanups, i, t)
7218 push_cleanup (decl, t, false);
7219 release_tree_vector (cleanups);
7222 if (was_readonly)
7223 TREE_READONLY (decl) = 1;
7225 if (flag_openmp
7226 && VAR_P (decl)
7227 && lookup_attribute ("omp declare target implicit",
7228 DECL_ATTRIBUTES (decl)))
7230 DECL_ATTRIBUTES (decl)
7231 = remove_attribute ("omp declare target implicit",
7232 DECL_ATTRIBUTES (decl));
7233 complete_type (TREE_TYPE (decl));
7234 if (!cp_omp_mappable_type (TREE_TYPE (decl)))
7235 error ("%q+D in declare target directive does not have mappable type",
7236 decl);
7237 else if (!lookup_attribute ("omp declare target",
7238 DECL_ATTRIBUTES (decl))
7239 && !lookup_attribute ("omp declare target link",
7240 DECL_ATTRIBUTES (decl)))
7241 DECL_ATTRIBUTES (decl)
7242 = tree_cons (get_identifier ("omp declare target"),
7243 NULL_TREE, DECL_ATTRIBUTES (decl));
7246 invoke_plugin_callbacks (PLUGIN_FINISH_DECL, decl);
7249 /* For class TYPE return itself or some its bases that contain
7250 any direct non-static data members. Return error_mark_node if an
7251 error has been diagnosed. */
7253 static tree
7254 find_decomp_class_base (location_t loc, tree type, tree ret)
7256 bool member_seen = false;
7257 for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
7258 if (TREE_CODE (field) != FIELD_DECL
7259 || DECL_ARTIFICIAL (field)
7260 || DECL_UNNAMED_BIT_FIELD (field))
7261 continue;
7262 else if (ret)
7263 return type;
7264 else if (ANON_AGGR_TYPE_P (TREE_TYPE (field)))
7266 if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE)
7267 error_at (loc, "cannot decompose class type %qT because it has an "
7268 "anonymous struct member", type);
7269 else
7270 error_at (loc, "cannot decompose class type %qT because it has an "
7271 "anonymous union member", type);
7272 inform (DECL_SOURCE_LOCATION (field), "declared here");
7273 return error_mark_node;
7275 else if (!accessible_p (type, field, true))
7277 error_at (loc, "cannot decompose inaccessible member %qD of %qT",
7278 field, type);
7279 inform (DECL_SOURCE_LOCATION (field),
7280 TREE_PRIVATE (field)
7281 ? G_("declared private here")
7282 : G_("declared protected here"));
7283 return error_mark_node;
7285 else
7286 member_seen = true;
7288 tree base_binfo, binfo;
7289 tree orig_ret = ret;
7290 int i;
7291 if (member_seen)
7292 ret = type;
7293 for (binfo = TYPE_BINFO (type), i = 0;
7294 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
7296 tree t = find_decomp_class_base (loc, TREE_TYPE (base_binfo), ret);
7297 if (t == error_mark_node)
7298 return error_mark_node;
7299 if (t != NULL_TREE && t != ret)
7301 if (ret == type)
7303 error_at (loc, "cannot decompose class type %qT: both it and "
7304 "its base class %qT have non-static data members",
7305 type, t);
7306 return error_mark_node;
7308 else if (orig_ret != NULL_TREE)
7309 return t;
7310 else if (ret != NULL_TREE)
7312 error_at (loc, "cannot decompose class type %qT: its base "
7313 "classes %qT and %qT have non-static data "
7314 "members", type, ret, t);
7315 return error_mark_node;
7317 else
7318 ret = t;
7321 return ret;
7324 /* Return std::tuple_size<TYPE>::value. */
7326 static tree
7327 get_tuple_size (tree type)
7329 tree args = make_tree_vec (1);
7330 TREE_VEC_ELT (args, 0) = type;
7331 tree inst = lookup_template_class (get_identifier ("tuple_size"), args,
7332 /*in_decl*/NULL_TREE,
7333 /*context*/std_node,
7334 /*entering_scope*/false, tf_none);
7335 inst = complete_type (inst);
7336 if (inst == error_mark_node || !COMPLETE_TYPE_P (inst))
7337 return NULL_TREE;
7338 tree val = lookup_qualified_name (inst, get_identifier ("value"),
7339 /*type*/false, /*complain*/false);
7340 if (TREE_CODE (val) == VAR_DECL || TREE_CODE (val) == CONST_DECL)
7341 val = maybe_constant_value (val);
7342 if (TREE_CODE (val) == INTEGER_CST)
7343 return val;
7344 else
7345 return error_mark_node;
7348 /* Return std::tuple_element<I,TYPE>::type. */
7350 static tree
7351 get_tuple_element_type (tree type, unsigned i)
7353 tree args = make_tree_vec (2);
7354 TREE_VEC_ELT (args, 0) = build_int_cst (integer_type_node, i);
7355 TREE_VEC_ELT (args, 1) = type;
7356 tree inst = lookup_template_class (get_identifier ("tuple_element"), args,
7357 /*in_decl*/NULL_TREE,
7358 /*context*/std_node,
7359 /*entering_scope*/false,
7360 tf_warning_or_error);
7361 return make_typename_type (inst, get_identifier ("type"),
7362 none_type, tf_warning_or_error);
7365 /* Return e.get<i>() or get<i>(e). */
7367 static tree
7368 get_tuple_decomp_init (tree decl, unsigned i)
7370 tree get_id = get_identifier ("get");
7371 tree targs = make_tree_vec (1);
7372 TREE_VEC_ELT (targs, 0) = build_int_cst (integer_type_node, i);
7374 tree etype = TREE_TYPE (decl);
7375 tree e = convert_from_reference (decl);
7377 /* [The id-expression] e is an lvalue if the type of the entity e is an
7378 lvalue reference and an xvalue otherwise. */
7379 if (!TYPE_REF_P (etype)
7380 || TYPE_REF_IS_RVALUE (etype))
7381 e = move (e);
7383 tree fns = lookup_qualified_name (TREE_TYPE (e), get_id,
7384 /*type*/false, /*complain*/false);
7385 bool use_member_get = false;
7387 /* To use a member get, member lookup must find at least one
7388 declaration that is a function template
7389 whose first template parameter is a non-type parameter. */
7390 for (lkp_iterator iter (MAYBE_BASELINK_FUNCTIONS (fns)); iter; ++iter)
7392 tree fn = *iter;
7393 if (TREE_CODE (fn) == TEMPLATE_DECL)
7395 tree tparms = DECL_TEMPLATE_PARMS (fn);
7396 tree parm = TREE_VEC_ELT (INNERMOST_TEMPLATE_PARMS (tparms), 0);
7397 if (TREE_CODE (TREE_VALUE (parm)) == PARM_DECL)
7399 use_member_get = true;
7400 break;
7405 if (use_member_get)
7407 fns = lookup_template_function (fns, targs);
7408 return build_new_method_call (e, fns, /*args*/NULL,
7409 /*path*/NULL_TREE, LOOKUP_NORMAL,
7410 /*fn_p*/NULL, tf_warning_or_error);
7412 else
7414 vec<tree,va_gc> *args = make_tree_vector_single (e);
7415 fns = lookup_template_function (get_id, targs);
7416 fns = perform_koenig_lookup (fns, args, tf_warning_or_error);
7417 return finish_call_expr (fns, &args, /*novirt*/false,
7418 /*koenig*/true, tf_warning_or_error);
7422 /* It's impossible to recover the decltype of a tuple decomposition variable
7423 based on the actual type of the variable, so store it in a hash table. */
7425 static GTY((cache)) tree_cache_map *decomp_type_table;
7426 static void
7427 store_decomp_type (tree v, tree t)
7429 if (!decomp_type_table)
7430 decomp_type_table = tree_cache_map::create_ggc (13);
7431 decomp_type_table->put (v, t);
7434 tree
7435 lookup_decomp_type (tree v)
7437 return *decomp_type_table->get (v);
7440 /* Mangle a decomposition declaration if needed. Arguments like
7441 in cp_finish_decomp. */
7443 void
7444 cp_maybe_mangle_decomp (tree decl, tree first, unsigned int count)
7446 if (!processing_template_decl
7447 && !error_operand_p (decl)
7448 && DECL_NAMESPACE_SCOPE_P (decl))
7450 auto_vec<tree, 16> v;
7451 v.safe_grow (count);
7452 tree d = first;
7453 for (unsigned int i = 0; i < count; i++, d = DECL_CHAIN (d))
7454 v[count - i - 1] = d;
7455 SET_DECL_ASSEMBLER_NAME (decl, mangle_decomp (decl, v));
7456 maybe_apply_pragma_weak (decl);
7460 /* Finish a decomposition declaration. DECL is the underlying declaration
7461 "e", FIRST is the head of a chain of decls for the individual identifiers
7462 chained through DECL_CHAIN in reverse order and COUNT is the number of
7463 those decls. */
7465 void
7466 cp_finish_decomp (tree decl, tree first, unsigned int count)
7468 if (error_operand_p (decl))
7470 error_out:
7471 while (count--)
7473 TREE_TYPE (first) = error_mark_node;
7474 if (DECL_HAS_VALUE_EXPR_P (first))
7476 SET_DECL_VALUE_EXPR (first, NULL_TREE);
7477 DECL_HAS_VALUE_EXPR_P (first) = 0;
7479 first = DECL_CHAIN (first);
7481 if (DECL_P (decl) && DECL_NAMESPACE_SCOPE_P (decl))
7482 SET_DECL_ASSEMBLER_NAME (decl, get_identifier ("<decomp>"));
7483 return;
7486 location_t loc = DECL_SOURCE_LOCATION (decl);
7487 if (type_dependent_expression_p (decl)
7488 /* This happens for range for when not in templates.
7489 Still add the DECL_VALUE_EXPRs for later processing. */
7490 || (!processing_template_decl
7491 && type_uses_auto (TREE_TYPE (decl))))
7493 for (unsigned int i = 0; i < count; i++)
7495 if (!DECL_HAS_VALUE_EXPR_P (first))
7497 tree v = build_nt (ARRAY_REF, decl,
7498 size_int (count - i - 1),
7499 NULL_TREE, NULL_TREE);
7500 SET_DECL_VALUE_EXPR (first, v);
7501 DECL_HAS_VALUE_EXPR_P (first) = 1;
7503 if (processing_template_decl)
7504 fit_decomposition_lang_decl (first, decl);
7505 first = DECL_CHAIN (first);
7507 return;
7510 auto_vec<tree, 16> v;
7511 v.safe_grow (count);
7512 tree d = first;
7513 for (unsigned int i = 0; i < count; i++, d = DECL_CHAIN (d))
7515 v[count - i - 1] = d;
7516 fit_decomposition_lang_decl (d, decl);
7519 tree type = TREE_TYPE (decl);
7520 tree dexp = decl;
7522 if (TYPE_REF_P (type))
7524 dexp = convert_from_reference (dexp);
7525 type = complete_type (TREE_TYPE (type));
7526 if (type == error_mark_node)
7527 goto error_out;
7528 if (!COMPLETE_TYPE_P (type))
7530 error_at (loc, "structured binding refers to incomplete type %qT",
7531 type);
7532 goto error_out;
7536 tree eltype = NULL_TREE;
7537 unsigned HOST_WIDE_INT eltscnt = 0;
7538 if (TREE_CODE (type) == ARRAY_TYPE)
7540 tree nelts;
7541 nelts = array_type_nelts_top (type);
7542 if (nelts == error_mark_node)
7543 goto error_out;
7544 if (!tree_fits_uhwi_p (nelts))
7546 error_at (loc, "cannot decompose variable length array %qT", type);
7547 goto error_out;
7549 eltscnt = tree_to_uhwi (nelts);
7550 if (count != eltscnt)
7552 cnt_mismatch:
7553 if (count > eltscnt)
7554 error_n (loc, count,
7555 "%u name provided for structured binding",
7556 "%u names provided for structured binding", count);
7557 else
7558 error_n (loc, count,
7559 "only %u name provided for structured binding",
7560 "only %u names provided for structured binding", count);
7561 inform_n (loc, eltscnt,
7562 "while %qT decomposes into %wu element",
7563 "while %qT decomposes into %wu elements",
7564 type, eltscnt);
7565 goto error_out;
7567 eltype = TREE_TYPE (type);
7568 for (unsigned int i = 0; i < count; i++)
7570 TREE_TYPE (v[i]) = eltype;
7571 layout_decl (v[i], 0);
7572 if (processing_template_decl)
7573 continue;
7574 tree t = unshare_expr (dexp);
7575 t = build4_loc (DECL_SOURCE_LOCATION (v[i]), ARRAY_REF,
7576 eltype, t, size_int (i), NULL_TREE,
7577 NULL_TREE);
7578 SET_DECL_VALUE_EXPR (v[i], t);
7579 DECL_HAS_VALUE_EXPR_P (v[i]) = 1;
7582 /* 2 GNU extensions. */
7583 else if (TREE_CODE (type) == COMPLEX_TYPE)
7585 eltscnt = 2;
7586 if (count != eltscnt)
7587 goto cnt_mismatch;
7588 eltype = cp_build_qualified_type (TREE_TYPE (type), TYPE_QUALS (type));
7589 for (unsigned int i = 0; i < count; i++)
7591 TREE_TYPE (v[i]) = eltype;
7592 layout_decl (v[i], 0);
7593 if (processing_template_decl)
7594 continue;
7595 tree t = unshare_expr (dexp);
7596 t = build1_loc (DECL_SOURCE_LOCATION (v[i]),
7597 i ? IMAGPART_EXPR : REALPART_EXPR, eltype,
7599 SET_DECL_VALUE_EXPR (v[i], t);
7600 DECL_HAS_VALUE_EXPR_P (v[i]) = 1;
7603 else if (TREE_CODE (type) == VECTOR_TYPE)
7605 if (!TYPE_VECTOR_SUBPARTS (type).is_constant (&eltscnt))
7607 error_at (loc, "cannot decompose variable length vector %qT", type);
7608 goto error_out;
7610 if (count != eltscnt)
7611 goto cnt_mismatch;
7612 eltype = cp_build_qualified_type (TREE_TYPE (type), TYPE_QUALS (type));
7613 for (unsigned int i = 0; i < count; i++)
7615 TREE_TYPE (v[i]) = eltype;
7616 layout_decl (v[i], 0);
7617 if (processing_template_decl)
7618 continue;
7619 tree t = unshare_expr (dexp);
7620 convert_vector_to_array_for_subscript (DECL_SOURCE_LOCATION (v[i]),
7621 &t, size_int (i));
7622 t = build4_loc (DECL_SOURCE_LOCATION (v[i]), ARRAY_REF,
7623 eltype, t, size_int (i), NULL_TREE,
7624 NULL_TREE);
7625 SET_DECL_VALUE_EXPR (v[i], t);
7626 DECL_HAS_VALUE_EXPR_P (v[i]) = 1;
7629 else if (tree tsize = get_tuple_size (type))
7631 if (tsize == error_mark_node)
7633 error_at (loc, "%<std::tuple_size<%T>::value%> is not an integral "
7634 "constant expression", type);
7635 goto error_out;
7637 if (!tree_fits_uhwi_p (tsize))
7639 error_n (loc, count,
7640 "%u name provided for structured binding",
7641 "%u names provided for structured binding", count);
7642 inform (loc, "while %qT decomposes into %E elements",
7643 type, tsize);
7644 goto error_out;
7646 eltscnt = tree_to_uhwi (tsize);
7647 if (count != eltscnt)
7648 goto cnt_mismatch;
7649 int save_read = DECL_READ_P (decl);
7650 for (unsigned i = 0; i < count; ++i)
7652 location_t sloc = input_location;
7653 location_t dloc = DECL_SOURCE_LOCATION (v[i]);
7655 input_location = dloc;
7656 tree init = get_tuple_decomp_init (decl, i);
7657 tree eltype = (init == error_mark_node ? error_mark_node
7658 : get_tuple_element_type (type, i));
7659 input_location = sloc;
7661 if (init == error_mark_node || eltype == error_mark_node)
7663 inform (dloc, "in initialization of structured binding "
7664 "variable %qD", v[i]);
7665 goto error_out;
7667 /* Save the decltype away before reference collapse. */
7668 store_decomp_type (v[i], eltype);
7669 eltype = cp_build_reference_type (eltype, !lvalue_p (init));
7670 TREE_TYPE (v[i]) = eltype;
7671 layout_decl (v[i], 0);
7672 if (DECL_HAS_VALUE_EXPR_P (v[i]))
7674 /* In this case the names are variables, not just proxies. */
7675 SET_DECL_VALUE_EXPR (v[i], NULL_TREE);
7676 DECL_HAS_VALUE_EXPR_P (v[i]) = 0;
7678 if (!processing_template_decl)
7679 cp_finish_decl (v[i], init, /*constexpr*/false,
7680 /*asm*/NULL_TREE, LOOKUP_NORMAL);
7682 /* Ignore reads from the underlying decl performed during initialization
7683 of the individual variables. If those will be read, we'll mark
7684 the underlying decl as read at that point. */
7685 DECL_READ_P (decl) = save_read;
7687 else if (TREE_CODE (type) == UNION_TYPE)
7689 error_at (loc, "cannot decompose union type %qT", type);
7690 goto error_out;
7692 else if (!CLASS_TYPE_P (type))
7694 error_at (loc, "cannot decompose non-array non-class type %qT", type);
7695 goto error_out;
7697 else if (LAMBDA_TYPE_P (type))
7699 error_at (loc, "cannot decompose lambda closure type %qT", type);
7700 goto error_out;
7702 else if (processing_template_decl && !COMPLETE_TYPE_P (type))
7703 pedwarn (loc, 0, "structured binding refers to incomplete class type %qT",
7704 type);
7705 else
7707 tree btype = find_decomp_class_base (loc, type, NULL_TREE);
7708 if (btype == error_mark_node)
7709 goto error_out;
7710 else if (btype == NULL_TREE)
7712 error_at (loc, "cannot decompose class type %qT without non-static "
7713 "data members", type);
7714 goto error_out;
7716 for (tree field = TYPE_FIELDS (btype); field; field = TREE_CHAIN (field))
7717 if (TREE_CODE (field) != FIELD_DECL
7718 || DECL_ARTIFICIAL (field)
7719 || DECL_UNNAMED_BIT_FIELD (field))
7720 continue;
7721 else
7722 eltscnt++;
7723 if (count != eltscnt)
7724 goto cnt_mismatch;
7725 tree t = dexp;
7726 if (type != btype)
7728 t = convert_to_base (t, btype, /*check_access*/true,
7729 /*nonnull*/false, tf_warning_or_error);
7730 type = btype;
7732 unsigned int i = 0;
7733 for (tree field = TYPE_FIELDS (btype); field; field = TREE_CHAIN (field))
7734 if (TREE_CODE (field) != FIELD_DECL
7735 || DECL_ARTIFICIAL (field)
7736 || DECL_UNNAMED_BIT_FIELD (field))
7737 continue;
7738 else
7740 tree tt = finish_non_static_data_member (field, unshare_expr (t),
7741 NULL_TREE);
7742 if (REFERENCE_REF_P (tt))
7743 tt = TREE_OPERAND (tt, 0);
7744 TREE_TYPE (v[i]) = TREE_TYPE (tt);
7745 layout_decl (v[i], 0);
7746 if (!processing_template_decl)
7748 SET_DECL_VALUE_EXPR (v[i], tt);
7749 DECL_HAS_VALUE_EXPR_P (v[i]) = 1;
7751 i++;
7754 if (processing_template_decl)
7756 for (unsigned int i = 0; i < count; i++)
7757 if (!DECL_HAS_VALUE_EXPR_P (v[i]))
7759 tree a = build_nt (ARRAY_REF, decl, size_int (i),
7760 NULL_TREE, NULL_TREE);
7761 SET_DECL_VALUE_EXPR (v[i], a);
7762 DECL_HAS_VALUE_EXPR_P (v[i]) = 1;
7767 /* Returns a declaration for a VAR_DECL as if:
7769 extern "C" TYPE NAME;
7771 had been seen. Used to create compiler-generated global
7772 variables. */
7774 static tree
7775 declare_global_var (tree name, tree type)
7777 tree decl;
7779 push_to_top_level ();
7780 decl = build_decl (input_location, VAR_DECL, name, type);
7781 TREE_PUBLIC (decl) = 1;
7782 DECL_EXTERNAL (decl) = 1;
7783 DECL_ARTIFICIAL (decl) = 1;
7784 DECL_CONTEXT (decl) = FROB_CONTEXT (global_namespace);
7785 /* If the user has explicitly declared this variable (perhaps
7786 because the code we are compiling is part of a low-level runtime
7787 library), then it is possible that our declaration will be merged
7788 with theirs by pushdecl. */
7789 decl = pushdecl (decl);
7790 cp_finish_decl (decl, NULL_TREE, false, NULL_TREE, 0);
7791 pop_from_top_level ();
7793 return decl;
7796 /* Returns the type for the argument to "__cxa_atexit" (or "atexit",
7797 if "__cxa_atexit" is not being used) corresponding to the function
7798 to be called when the program exits. */
7800 static tree
7801 get_atexit_fn_ptr_type (void)
7803 tree fn_type;
7805 if (!atexit_fn_ptr_type_node)
7807 tree arg_type;
7808 if (flag_use_cxa_atexit
7809 && !targetm.cxx.use_atexit_for_cxa_atexit ())
7810 /* The parameter to "__cxa_atexit" is "void (*)(void *)". */
7811 arg_type = ptr_type_node;
7812 else
7813 /* The parameter to "atexit" is "void (*)(void)". */
7814 arg_type = NULL_TREE;
7816 fn_type = build_function_type_list (void_type_node,
7817 arg_type, NULL_TREE);
7818 atexit_fn_ptr_type_node = build_pointer_type (fn_type);
7821 return atexit_fn_ptr_type_node;
7824 /* Returns a pointer to the `atexit' function. Note that if
7825 FLAG_USE_CXA_ATEXIT is nonzero, then this will actually be the new
7826 `__cxa_atexit' function specified in the IA64 C++ ABI. */
7828 static tree
7829 get_atexit_node (void)
7831 tree atexit_fndecl;
7832 tree fn_type;
7833 tree fn_ptr_type;
7834 const char *name;
7835 bool use_aeabi_atexit;
7837 if (atexit_node)
7838 return atexit_node;
7840 if (flag_use_cxa_atexit && !targetm.cxx.use_atexit_for_cxa_atexit ())
7842 /* The declaration for `__cxa_atexit' is:
7844 int __cxa_atexit (void (*)(void *), void *, void *)
7846 We build up the argument types and then the function type
7847 itself. */
7848 tree argtype0, argtype1, argtype2;
7850 use_aeabi_atexit = targetm.cxx.use_aeabi_atexit ();
7851 /* First, build the pointer-to-function type for the first
7852 argument. */
7853 fn_ptr_type = get_atexit_fn_ptr_type ();
7854 /* Then, build the rest of the argument types. */
7855 argtype2 = ptr_type_node;
7856 if (use_aeabi_atexit)
7858 argtype1 = fn_ptr_type;
7859 argtype0 = ptr_type_node;
7861 else
7863 argtype1 = ptr_type_node;
7864 argtype0 = fn_ptr_type;
7866 /* And the final __cxa_atexit type. */
7867 fn_type = build_function_type_list (integer_type_node,
7868 argtype0, argtype1, argtype2,
7869 NULL_TREE);
7870 if (use_aeabi_atexit)
7871 name = "__aeabi_atexit";
7872 else
7873 name = "__cxa_atexit";
7875 else
7877 /* The declaration for `atexit' is:
7879 int atexit (void (*)());
7881 We build up the argument types and then the function type
7882 itself. */
7883 fn_ptr_type = get_atexit_fn_ptr_type ();
7884 /* Build the final atexit type. */
7885 fn_type = build_function_type_list (integer_type_node,
7886 fn_ptr_type, NULL_TREE);
7887 name = "atexit";
7890 /* Now, build the function declaration. */
7891 push_lang_context (lang_name_c);
7892 atexit_fndecl = build_library_fn_ptr (name, fn_type, ECF_LEAF | ECF_NOTHROW);
7893 mark_used (atexit_fndecl);
7894 pop_lang_context ();
7895 atexit_node = decay_conversion (atexit_fndecl, tf_warning_or_error);
7897 return atexit_node;
7900 /* Like get_atexit_node, but for thread-local cleanups. */
7902 static tree
7903 get_thread_atexit_node (void)
7905 /* The declaration for `__cxa_thread_atexit' is:
7907 int __cxa_thread_atexit (void (*)(void *), void *, void *) */
7908 tree fn_type = build_function_type_list (integer_type_node,
7909 get_atexit_fn_ptr_type (),
7910 ptr_type_node, ptr_type_node,
7911 NULL_TREE);
7913 /* Now, build the function declaration. */
7914 tree atexit_fndecl = build_library_fn_ptr ("__cxa_thread_atexit", fn_type,
7915 ECF_LEAF | ECF_NOTHROW);
7916 return decay_conversion (atexit_fndecl, tf_warning_or_error);
7919 /* Returns the __dso_handle VAR_DECL. */
7921 static tree
7922 get_dso_handle_node (void)
7924 if (dso_handle_node)
7925 return dso_handle_node;
7927 /* Declare the variable. */
7928 dso_handle_node = declare_global_var (get_identifier ("__dso_handle"),
7929 ptr_type_node);
7931 #ifdef HAVE_GAS_HIDDEN
7932 if (dso_handle_node != error_mark_node)
7934 DECL_VISIBILITY (dso_handle_node) = VISIBILITY_HIDDEN;
7935 DECL_VISIBILITY_SPECIFIED (dso_handle_node) = 1;
7937 #endif
7939 return dso_handle_node;
7942 /* Begin a new function with internal linkage whose job will be simply
7943 to destroy some particular variable. */
7945 static GTY(()) int start_cleanup_cnt;
7947 static tree
7948 start_cleanup_fn (void)
7950 char name[32];
7951 tree fntype;
7952 tree fndecl;
7953 bool use_cxa_atexit = flag_use_cxa_atexit
7954 && !targetm.cxx.use_atexit_for_cxa_atexit ();
7956 push_to_top_level ();
7958 /* No need to mangle this. */
7959 push_lang_context (lang_name_c);
7961 /* Build the name of the function. */
7962 sprintf (name, "__tcf_%d", start_cleanup_cnt++);
7963 /* Build the function declaration. */
7964 fntype = TREE_TYPE (get_atexit_fn_ptr_type ());
7965 fndecl = build_lang_decl (FUNCTION_DECL, get_identifier (name), fntype);
7966 /* It's a function with internal linkage, generated by the
7967 compiler. */
7968 TREE_PUBLIC (fndecl) = 0;
7969 DECL_ARTIFICIAL (fndecl) = 1;
7970 /* Make the function `inline' so that it is only emitted if it is
7971 actually needed. It is unlikely that it will be inlined, since
7972 it is only called via a function pointer, but we avoid unnecessary
7973 emissions this way. */
7974 DECL_DECLARED_INLINE_P (fndecl) = 1;
7975 DECL_INTERFACE_KNOWN (fndecl) = 1;
7976 /* Build the parameter. */
7977 if (use_cxa_atexit)
7979 tree parmdecl = cp_build_parm_decl (fndecl, NULL_TREE, ptr_type_node);
7980 TREE_USED (parmdecl) = 1;
7981 DECL_READ_P (parmdecl) = 1;
7982 DECL_ARGUMENTS (fndecl) = parmdecl;
7985 pushdecl (fndecl);
7986 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
7988 pop_lang_context ();
7990 return current_function_decl;
7993 /* Finish the cleanup function begun by start_cleanup_fn. */
7995 static void
7996 end_cleanup_fn (void)
7998 expand_or_defer_fn (finish_function (/*inline_p=*/false));
8000 pop_from_top_level ();
8003 /* Generate code to handle the destruction of DECL, an object with
8004 static storage duration. */
8006 tree
8007 register_dtor_fn (tree decl)
8009 tree cleanup;
8010 tree addr;
8011 tree compound_stmt;
8012 tree fcall;
8013 tree type;
8014 bool ob_parm, dso_parm, use_dtor;
8015 tree arg0, arg1, arg2;
8016 tree atex_node;
8018 type = TREE_TYPE (decl);
8019 if (TYPE_HAS_TRIVIAL_DESTRUCTOR (type))
8020 return void_node;
8022 /* If we're using "__cxa_atexit" (or "__cxa_thread_atexit" or
8023 "__aeabi_atexit"), and DECL is a class object, we can just pass the
8024 destructor to "__cxa_atexit"; we don't have to build a temporary
8025 function to do the cleanup. */
8026 dso_parm = (flag_use_cxa_atexit
8027 && !targetm.cxx.use_atexit_for_cxa_atexit ());
8028 ob_parm = (CP_DECL_THREAD_LOCAL_P (decl) || dso_parm);
8029 use_dtor = ob_parm && CLASS_TYPE_P (type);
8030 if (use_dtor)
8032 cleanup = get_class_binding (type, complete_dtor_identifier);
8034 /* Make sure it is accessible. */
8035 perform_or_defer_access_check (TYPE_BINFO (type), cleanup, cleanup,
8036 tf_warning_or_error);
8038 else
8040 /* Call build_cleanup before we enter the anonymous function so
8041 that any access checks will be done relative to the current
8042 scope, rather than the scope of the anonymous function. */
8043 build_cleanup (decl);
8045 /* Now start the function. */
8046 cleanup = start_cleanup_fn ();
8048 /* Now, recompute the cleanup. It may contain SAVE_EXPRs that refer
8049 to the original function, rather than the anonymous one. That
8050 will make the back end think that nested functions are in use,
8051 which causes confusion. */
8052 push_deferring_access_checks (dk_no_check);
8053 fcall = build_cleanup (decl);
8054 pop_deferring_access_checks ();
8056 /* Create the body of the anonymous function. */
8057 compound_stmt = begin_compound_stmt (BCS_FN_BODY);
8058 finish_expr_stmt (fcall);
8059 finish_compound_stmt (compound_stmt);
8060 end_cleanup_fn ();
8063 /* Call atexit with the cleanup function. */
8064 mark_used (cleanup);
8065 cleanup = build_address (cleanup);
8067 if (CP_DECL_THREAD_LOCAL_P (decl))
8068 atex_node = get_thread_atexit_node ();
8069 else
8070 atex_node = get_atexit_node ();
8072 if (use_dtor)
8074 /* We must convert CLEANUP to the type that "__cxa_atexit"
8075 expects. */
8076 cleanup = build_nop (get_atexit_fn_ptr_type (), cleanup);
8077 /* "__cxa_atexit" will pass the address of DECL to the
8078 cleanup function. */
8079 mark_used (decl);
8080 addr = build_address (decl);
8081 /* The declared type of the parameter to "__cxa_atexit" is
8082 "void *". For plain "T*", we could just let the
8083 machinery in cp_build_function_call convert it -- but if the
8084 type is "cv-qualified T *", then we need to convert it
8085 before passing it in, to avoid spurious errors. */
8086 addr = build_nop (ptr_type_node, addr);
8088 else
8089 /* Since the cleanup functions we build ignore the address
8090 they're given, there's no reason to pass the actual address
8091 in, and, in general, it's cheaper to pass NULL than any
8092 other value. */
8093 addr = null_pointer_node;
8095 if (dso_parm)
8096 arg2 = cp_build_addr_expr (get_dso_handle_node (),
8097 tf_warning_or_error);
8098 else if (ob_parm)
8099 /* Just pass NULL to the dso handle parm if we don't actually
8100 have a DSO handle on this target. */
8101 arg2 = null_pointer_node;
8102 else
8103 arg2 = NULL_TREE;
8105 if (ob_parm)
8107 if (!CP_DECL_THREAD_LOCAL_P (decl)
8108 && targetm.cxx.use_aeabi_atexit ())
8110 arg1 = cleanup;
8111 arg0 = addr;
8113 else
8115 arg1 = addr;
8116 arg0 = cleanup;
8119 else
8121 arg0 = cleanup;
8122 arg1 = NULL_TREE;
8124 return cp_build_function_call_nary (atex_node, tf_warning_or_error,
8125 arg0, arg1, arg2, NULL_TREE);
8128 /* DECL is a VAR_DECL with static storage duration. INIT, if present,
8129 is its initializer. Generate code to handle the construction
8130 and destruction of DECL. */
8132 static void
8133 expand_static_init (tree decl, tree init)
8135 gcc_assert (VAR_P (decl));
8136 gcc_assert (TREE_STATIC (decl));
8138 /* Some variables require no dynamic initialization. */
8139 if (TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
8141 /* Make sure the destructor is callable. */
8142 cxx_maybe_build_cleanup (decl, tf_warning_or_error);
8143 if (!init)
8144 return;
8147 if (CP_DECL_THREAD_LOCAL_P (decl) && DECL_GNU_TLS_P (decl)
8148 && !DECL_FUNCTION_SCOPE_P (decl))
8150 if (init)
8151 error ("non-local variable %qD declared %<__thread%> "
8152 "needs dynamic initialization", decl);
8153 else
8154 error ("non-local variable %qD declared %<__thread%> "
8155 "has a non-trivial destructor", decl);
8156 static bool informed;
8157 if (!informed)
8159 inform (DECL_SOURCE_LOCATION (decl),
8160 "C++11 %<thread_local%> allows dynamic initialization "
8161 "and destruction");
8162 informed = true;
8164 return;
8167 if (DECL_FUNCTION_SCOPE_P (decl))
8169 /* Emit code to perform this initialization but once. */
8170 tree if_stmt = NULL_TREE, inner_if_stmt = NULL_TREE;
8171 tree then_clause = NULL_TREE, inner_then_clause = NULL_TREE;
8172 tree guard, guard_addr;
8173 tree flag, begin;
8174 /* We don't need thread-safety code for thread-local vars. */
8175 bool thread_guard = (flag_threadsafe_statics
8176 && !CP_DECL_THREAD_LOCAL_P (decl));
8178 /* Emit code to perform this initialization but once. This code
8179 looks like:
8181 static <type> guard;
8182 if (!__atomic_load (guard.first_byte)) {
8183 if (__cxa_guard_acquire (&guard)) {
8184 bool flag = false;
8185 try {
8186 // Do initialization.
8187 flag = true; __cxa_guard_release (&guard);
8188 // Register variable for destruction at end of program.
8189 } catch {
8190 if (!flag) __cxa_guard_abort (&guard);
8195 Note that the `flag' variable is only set to 1 *after* the
8196 initialization is complete. This ensures that an exception,
8197 thrown during the construction, will cause the variable to
8198 reinitialized when we pass through this code again, as per:
8200 [stmt.dcl]
8202 If the initialization exits by throwing an exception, the
8203 initialization is not complete, so it will be tried again
8204 the next time control enters the declaration.
8206 This process should be thread-safe, too; multiple threads
8207 should not be able to initialize the variable more than
8208 once. */
8210 /* Create the guard variable. */
8211 guard = get_guard (decl);
8213 /* Begin the conditional initialization. */
8214 if_stmt = begin_if_stmt ();
8216 finish_if_stmt_cond (get_guard_cond (guard, thread_guard), if_stmt);
8217 then_clause = begin_compound_stmt (BCS_NO_SCOPE);
8219 if (thread_guard)
8221 tree vfntype = NULL_TREE;
8222 tree acquire_name, release_name, abort_name;
8223 tree acquire_fn, release_fn, abort_fn;
8224 guard_addr = build_address (guard);
8226 acquire_name = get_identifier ("__cxa_guard_acquire");
8227 release_name = get_identifier ("__cxa_guard_release");
8228 abort_name = get_identifier ("__cxa_guard_abort");
8229 acquire_fn = get_global_binding (acquire_name);
8230 release_fn = get_global_binding (release_name);
8231 abort_fn = get_global_binding (abort_name);
8232 if (!acquire_fn)
8233 acquire_fn = push_library_fn
8234 (acquire_name, build_function_type_list (integer_type_node,
8235 TREE_TYPE (guard_addr),
8236 NULL_TREE),
8237 NULL_TREE, ECF_NOTHROW | ECF_LEAF);
8238 if (!release_fn || !abort_fn)
8239 vfntype = build_function_type_list (void_type_node,
8240 TREE_TYPE (guard_addr),
8241 NULL_TREE);
8242 if (!release_fn)
8243 release_fn = push_library_fn (release_name, vfntype, NULL_TREE,
8244 ECF_NOTHROW | ECF_LEAF);
8245 if (!abort_fn)
8246 abort_fn = push_library_fn (abort_name, vfntype, NULL_TREE,
8247 ECF_NOTHROW | ECF_LEAF);
8249 inner_if_stmt = begin_if_stmt ();
8250 finish_if_stmt_cond (build_call_n (acquire_fn, 1, guard_addr),
8251 inner_if_stmt);
8253 inner_then_clause = begin_compound_stmt (BCS_NO_SCOPE);
8254 begin = get_target_expr (boolean_false_node);
8255 flag = TARGET_EXPR_SLOT (begin);
8257 TARGET_EXPR_CLEANUP (begin)
8258 = build3 (COND_EXPR, void_type_node, flag,
8259 void_node,
8260 build_call_n (abort_fn, 1, guard_addr));
8261 CLEANUP_EH_ONLY (begin) = 1;
8263 /* Do the initialization itself. */
8264 init = add_stmt_to_compound (begin, init);
8265 init = add_stmt_to_compound
8266 (init, build2 (MODIFY_EXPR, void_type_node, flag, boolean_true_node));
8267 init = add_stmt_to_compound
8268 (init, build_call_n (release_fn, 1, guard_addr));
8270 else
8271 init = add_stmt_to_compound (init, set_guard (guard));
8273 /* Use atexit to register a function for destroying this static
8274 variable. */
8275 init = add_stmt_to_compound (init, register_dtor_fn (decl));
8277 finish_expr_stmt (init);
8279 if (thread_guard)
8281 finish_compound_stmt (inner_then_clause);
8282 finish_then_clause (inner_if_stmt);
8283 finish_if_stmt (inner_if_stmt);
8286 finish_compound_stmt (then_clause);
8287 finish_then_clause (if_stmt);
8288 finish_if_stmt (if_stmt);
8290 else if (CP_DECL_THREAD_LOCAL_P (decl))
8291 tls_aggregates = tree_cons (init, decl, tls_aggregates);
8292 else
8293 static_aggregates = tree_cons (init, decl, static_aggregates);
8297 /* Make TYPE a complete type based on INITIAL_VALUE.
8298 Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
8299 2 if there was no information (in which case assume 0 if DO_DEFAULT),
8300 3 if the initializer list is empty (in pedantic mode). */
8303 cp_complete_array_type (tree *ptype, tree initial_value, bool do_default)
8305 int failure;
8306 tree type, elt_type;
8308 /* Don't get confused by a CONSTRUCTOR for some other type. */
8309 if (initial_value && TREE_CODE (initial_value) == CONSTRUCTOR
8310 && !BRACE_ENCLOSED_INITIALIZER_P (initial_value)
8311 && TREE_CODE (TREE_TYPE (initial_value)) != ARRAY_TYPE)
8312 return 1;
8314 if (initial_value)
8316 unsigned HOST_WIDE_INT i;
8317 tree value;
8319 /* An array of character type can be initialized from a
8320 brace-enclosed string constant.
8322 FIXME: this code is duplicated from reshape_init. Probably
8323 we should just call reshape_init here? */
8324 if (char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (*ptype)))
8325 && TREE_CODE (initial_value) == CONSTRUCTOR
8326 && !vec_safe_is_empty (CONSTRUCTOR_ELTS (initial_value)))
8328 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (initial_value);
8329 tree value = (*v)[0].value;
8331 if (TREE_CODE (value) == STRING_CST
8332 && v->length () == 1)
8333 initial_value = value;
8336 /* If any of the elements are parameter packs, we can't actually
8337 complete this type now because the array size is dependent. */
8338 if (TREE_CODE (initial_value) == CONSTRUCTOR)
8340 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (initial_value),
8341 i, value)
8343 if (PACK_EXPANSION_P (value))
8344 return 0;
8349 failure = complete_array_type (ptype, initial_value, do_default);
8351 /* We can create the array before the element type is complete, which
8352 means that we didn't have these two bits set in the original type
8353 either. In completing the type, we are expected to propagate these
8354 bits. See also complete_type which does the same thing for arrays
8355 of fixed size. */
8356 type = *ptype;
8357 if (type != error_mark_node && TYPE_DOMAIN (type))
8359 elt_type = TREE_TYPE (type);
8360 TYPE_NEEDS_CONSTRUCTING (type) = TYPE_NEEDS_CONSTRUCTING (elt_type);
8361 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
8362 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (elt_type);
8365 return failure;
8368 /* As above, but either give an error or reject zero-size arrays, depending
8369 on COMPLAIN. */
8372 cp_complete_array_type_or_error (tree *ptype, tree initial_value,
8373 bool do_default, tsubst_flags_t complain)
8375 int failure;
8376 bool sfinae = !(complain & tf_error);
8377 /* In SFINAE context we can't be lenient about zero-size arrays. */
8378 if (sfinae)
8379 ++pedantic;
8380 failure = cp_complete_array_type (ptype, initial_value, do_default);
8381 if (sfinae)
8382 --pedantic;
8383 if (failure)
8385 if (sfinae)
8386 /* Not an error. */;
8387 else if (failure == 1)
8388 error ("initializer fails to determine size of %qT", *ptype);
8389 else if (failure == 2)
8391 if (do_default)
8392 error ("array size missing in %qT", *ptype);
8394 else if (failure == 3)
8395 error ("zero-size array %qT", *ptype);
8396 *ptype = error_mark_node;
8398 return failure;
8401 /* Return zero if something is declared to be a member of type
8402 CTYPE when in the context of CUR_TYPE. STRING is the error
8403 message to print in that case. Otherwise, quietly return 1. */
8405 static int
8406 member_function_or_else (tree ctype, tree cur_type, enum overload_flags flags)
8408 if (ctype && ctype != cur_type)
8410 if (flags == DTOR_FLAG)
8411 error ("destructor for alien class %qT cannot be a member", ctype);
8412 else
8413 error ("constructor for alien class %qT cannot be a member", ctype);
8414 return 0;
8416 return 1;
8419 /* Subroutine of `grokdeclarator'. */
8421 /* Generate errors possibly applicable for a given set of specifiers.
8422 This is for ARM $7.1.2. */
8424 static void
8425 bad_specifiers (tree object,
8426 enum bad_spec_place type,
8427 int virtualp,
8428 int quals,
8429 int inlinep,
8430 int friendp,
8431 int raises,
8432 const location_t* locations)
8434 switch (type)
8436 case BSP_VAR:
8437 if (virtualp)
8438 error_at (locations[ds_virtual],
8439 "%qD declared as a %<virtual%> variable", object);
8440 if (quals)
8441 error ("%<const%> and %<volatile%> function specifiers on "
8442 "%qD invalid in variable declaration", object);
8443 break;
8444 case BSP_PARM:
8445 if (virtualp)
8446 error_at (locations[ds_virtual],
8447 "%qD declared as a %<virtual%> parameter", object);
8448 if (inlinep)
8449 error_at (locations[ds_inline],
8450 "%qD declared as an %<inline%> parameter", object);
8451 if (quals)
8452 error ("%<const%> and %<volatile%> function specifiers on "
8453 "%qD invalid in parameter declaration", object);
8454 break;
8455 case BSP_TYPE:
8456 if (virtualp)
8457 error_at (locations[ds_virtual],
8458 "%qD declared as a %<virtual%> type", object);
8459 if (inlinep)
8460 error_at (locations[ds_inline],
8461 "%qD declared as an %<inline%> type", object);
8462 if (quals)
8463 error ("%<const%> and %<volatile%> function specifiers on "
8464 "%qD invalid in type declaration", object);
8465 break;
8466 case BSP_FIELD:
8467 if (virtualp)
8468 error_at (locations[ds_virtual],
8469 "%qD declared as a %<virtual%> field", object);
8470 if (inlinep)
8471 error_at (locations[ds_inline],
8472 "%qD declared as an %<inline%> field", object);
8473 if (quals)
8474 error ("%<const%> and %<volatile%> function specifiers on "
8475 "%qD invalid in field declaration", object);
8476 break;
8477 default:
8478 gcc_unreachable();
8480 if (friendp)
8481 error ("%q+D declared as a friend", object);
8482 if (raises
8483 && !flag_noexcept_type
8484 && (TREE_CODE (object) == TYPE_DECL
8485 || (!TYPE_PTRFN_P (TREE_TYPE (object))
8486 && !TYPE_REFFN_P (TREE_TYPE (object))
8487 && !TYPE_PTRMEMFUNC_P (TREE_TYPE (object)))))
8488 error ("%q+D declared with an exception specification", object);
8491 /* DECL is a member function or static data member and is presently
8492 being defined. Check that the definition is taking place in a
8493 valid namespace. */
8495 static void
8496 check_class_member_definition_namespace (tree decl)
8498 /* These checks only apply to member functions and static data
8499 members. */
8500 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
8501 /* We check for problems with specializations in pt.c in
8502 check_specialization_namespace, where we can issue better
8503 diagnostics. */
8504 if (processing_specialization)
8505 return;
8506 /* We check this in check_explicit_instantiation_namespace. */
8507 if (processing_explicit_instantiation)
8508 return;
8509 /* [class.mfct]
8511 A member function definition that appears outside of the
8512 class definition shall appear in a namespace scope enclosing
8513 the class definition.
8515 [class.static.data]
8517 The definition for a static data member shall appear in a
8518 namespace scope enclosing the member's class definition. */
8519 if (!is_ancestor (current_namespace, DECL_CONTEXT (decl)))
8520 permerror (input_location, "definition of %qD is not in namespace enclosing %qT",
8521 decl, DECL_CONTEXT (decl));
8524 /* Build a PARM_DECL for the "this" parameter of FN. TYPE is the
8525 METHOD_TYPE for a non-static member function; QUALS are the
8526 cv-qualifiers that apply to the function. */
8528 tree
8529 build_this_parm (tree fn, tree type, cp_cv_quals quals)
8531 tree this_type;
8532 tree qual_type;
8533 tree parm;
8534 cp_cv_quals this_quals;
8536 if (CLASS_TYPE_P (type))
8538 this_type
8539 = cp_build_qualified_type (type, quals & ~TYPE_QUAL_RESTRICT);
8540 this_type = build_pointer_type (this_type);
8542 else
8543 this_type = type_of_this_parm (type);
8544 /* The `this' parameter is implicitly `const'; it cannot be
8545 assigned to. */
8546 this_quals = (quals & TYPE_QUAL_RESTRICT) | TYPE_QUAL_CONST;
8547 qual_type = cp_build_qualified_type (this_type, this_quals);
8548 parm = build_artificial_parm (fn, this_identifier, qual_type);
8549 cp_apply_type_quals_to_decl (this_quals, parm);
8550 return parm;
8553 /* DECL is a static member function. Complain if it was declared
8554 with function-cv-quals. */
8556 static void
8557 check_static_quals (tree decl, cp_cv_quals quals)
8559 if (quals != TYPE_UNQUALIFIED)
8560 error ("static member function %q#D declared with type qualifiers",
8561 decl);
8564 // Check that FN takes no arguments and returns bool.
8565 static void
8566 check_concept_fn (tree fn)
8568 // A constraint is nullary.
8569 if (DECL_ARGUMENTS (fn))
8570 error_at (DECL_SOURCE_LOCATION (fn),
8571 "concept %q#D declared with function parameters", fn);
8573 // The declared return type of the concept shall be bool, and
8574 // it shall not be deduced from it definition.
8575 tree type = TREE_TYPE (TREE_TYPE (fn));
8576 if (is_auto (type))
8577 error_at (DECL_SOURCE_LOCATION (fn),
8578 "concept %q#D declared with a deduced return type", fn);
8579 else if (type != boolean_type_node)
8580 error_at (DECL_SOURCE_LOCATION (fn),
8581 "concept %q#D with non-%<bool%> return type %qT", fn, type);
8584 /* Helper function. Replace the temporary this parameter injected
8585 during cp_finish_omp_declare_simd with the real this parameter. */
8587 static tree
8588 declare_simd_adjust_this (tree *tp, int *walk_subtrees, void *data)
8590 tree this_parm = (tree) data;
8591 if (TREE_CODE (*tp) == PARM_DECL
8592 && DECL_NAME (*tp) == this_identifier
8593 && *tp != this_parm)
8594 *tp = this_parm;
8595 else if (TYPE_P (*tp))
8596 *walk_subtrees = 0;
8597 return NULL_TREE;
8600 /* CTYPE is class type, or null if non-class.
8601 TYPE is type this FUNCTION_DECL should have, either FUNCTION_TYPE
8602 or METHOD_TYPE.
8603 DECLARATOR is the function's name.
8604 PARMS is a chain of PARM_DECLs for the function.
8605 VIRTUALP is truthvalue of whether the function is virtual or not.
8606 FLAGS are to be passed through to `grokclassfn'.
8607 QUALS are qualifiers indicating whether the function is `const'
8608 or `volatile'.
8609 RAISES is a list of exceptions that this function can raise.
8610 CHECK is 1 if we must find this method in CTYPE, 0 if we should
8611 not look, and -1 if we should not call `grokclassfn' at all.
8613 SFK is the kind of special function (if any) for the new function.
8615 Returns `NULL_TREE' if something goes wrong, after issuing
8616 applicable error messages. */
8618 static tree
8619 grokfndecl (tree ctype,
8620 tree type,
8621 tree declarator,
8622 tree parms,
8623 tree orig_declarator,
8624 const cp_decl_specifier_seq *declspecs,
8625 tree decl_reqs,
8626 int virtualp,
8627 enum overload_flags flags,
8628 cp_cv_quals quals,
8629 cp_ref_qualifier rqual,
8630 tree raises,
8631 int check,
8632 int friendp,
8633 int publicp,
8634 int inlinep,
8635 bool deletedp,
8636 special_function_kind sfk,
8637 bool funcdef_flag,
8638 bool late_return_type_p,
8639 int template_count,
8640 tree in_namespace,
8641 tree* attrlist,
8642 location_t location)
8644 tree decl;
8645 int staticp = ctype && TREE_CODE (type) == FUNCTION_TYPE;
8646 tree t;
8648 if (location == UNKNOWN_LOCATION)
8649 location = input_location;
8651 // Was the concept specifier present?
8652 bool concept_p = inlinep & 4;
8654 // Concept declarations must have a corresponding definition.
8655 if (concept_p && !funcdef_flag)
8657 error_at (location, "concept %qD has no definition", declarator);
8658 return NULL_TREE;
8661 type = build_cp_fntype_variant (type, rqual, raises, late_return_type_p);
8663 decl = build_lang_decl_loc (location, FUNCTION_DECL, declarator, type);
8665 /* Set the constraints on the declaration. */
8666 if (flag_concepts)
8668 tree tmpl_reqs = NULL_TREE;
8669 if (processing_template_decl > template_class_depth (ctype))
8670 tmpl_reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
8672 /* Adjust the required expression into a constraint. */
8673 if (decl_reqs)
8674 decl_reqs = normalize_expression (decl_reqs);
8676 tree ci = build_constraints (tmpl_reqs, decl_reqs);
8677 set_constraints (decl, ci);
8680 if (TREE_CODE (type) == METHOD_TYPE)
8682 tree parm = build_this_parm (decl, type, quals);
8683 DECL_CHAIN (parm) = parms;
8684 parms = parm;
8686 /* Allocate space to hold the vptr bit if needed. */
8687 SET_DECL_ALIGN (decl, MINIMUM_METHOD_BOUNDARY);
8690 DECL_ARGUMENTS (decl) = parms;
8691 for (t = parms; t; t = DECL_CHAIN (t))
8692 DECL_CONTEXT (t) = decl;
8694 /* Propagate volatile out from type to decl. */
8695 if (TYPE_VOLATILE (type))
8696 TREE_THIS_VOLATILE (decl) = 1;
8698 /* Setup decl according to sfk. */
8699 switch (sfk)
8701 case sfk_constructor:
8702 case sfk_copy_constructor:
8703 case sfk_move_constructor:
8704 DECL_CXX_CONSTRUCTOR_P (decl) = 1;
8705 DECL_NAME (decl) = ctor_identifier;
8706 break;
8707 case sfk_destructor:
8708 DECL_CXX_DESTRUCTOR_P (decl) = 1;
8709 DECL_NAME (decl) = dtor_identifier;
8710 break;
8711 default:
8712 break;
8715 if (friendp && TREE_CODE (orig_declarator) == TEMPLATE_ID_EXPR)
8717 if (funcdef_flag)
8718 error_at (location,
8719 "defining explicit specialization %qD in friend declaration",
8720 orig_declarator);
8721 else
8723 tree fns = TREE_OPERAND (orig_declarator, 0);
8724 tree args = TREE_OPERAND (orig_declarator, 1);
8726 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
8728 /* Something like `template <class T> friend void f<T>()'. */
8729 error_at (location,
8730 "invalid use of template-id %qD in declaration "
8731 "of primary template",
8732 orig_declarator);
8733 return NULL_TREE;
8737 /* A friend declaration of the form friend void f<>(). Record
8738 the information in the TEMPLATE_ID_EXPR. */
8739 SET_DECL_IMPLICIT_INSTANTIATION (decl);
8741 gcc_assert (identifier_p (fns) || TREE_CODE (fns) == OVERLOAD);
8742 DECL_TEMPLATE_INFO (decl) = build_template_info (fns, args);
8744 for (t = TYPE_ARG_TYPES (TREE_TYPE (decl)); t; t = TREE_CHAIN (t))
8745 if (TREE_PURPOSE (t)
8746 && TREE_CODE (TREE_PURPOSE (t)) == DEFAULT_ARG)
8748 error_at (defarg_location (TREE_PURPOSE (t)),
8749 "default arguments are not allowed in declaration "
8750 "of friend template specialization %qD",
8751 decl);
8752 return NULL_TREE;
8755 if (inlinep & 1)
8757 error_at (declspecs->locations[ds_inline],
8758 "%<inline%> is not allowed in declaration of friend "
8759 "template specialization %qD",
8760 decl);
8761 return NULL_TREE;
8766 /* If this decl has namespace scope, set that up. */
8767 if (in_namespace)
8768 set_decl_namespace (decl, in_namespace, friendp);
8769 else if (!ctype)
8770 DECL_CONTEXT (decl) = FROB_CONTEXT (current_decl_namespace ());
8772 /* `main' and builtins have implicit 'C' linkage. */
8773 if (ctype == NULL_TREE
8774 && DECL_FILE_SCOPE_P (decl)
8775 && current_lang_name == lang_name_cplusplus
8776 && (MAIN_NAME_P (declarator)
8777 || (IDENTIFIER_LENGTH (declarator) > 10
8778 && IDENTIFIER_POINTER (declarator)[0] == '_'
8779 && IDENTIFIER_POINTER (declarator)[1] == '_'
8780 && strncmp (IDENTIFIER_POINTER (declarator)+2,
8781 "builtin_", 8) == 0)
8782 || (targetcm.cxx_implicit_extern_c
8783 && (targetcm.cxx_implicit_extern_c
8784 (IDENTIFIER_POINTER (declarator))))))
8785 SET_DECL_LANGUAGE (decl, lang_c);
8787 /* Should probably propagate const out from type to decl I bet (mrs). */
8788 if (staticp)
8790 DECL_STATIC_FUNCTION_P (decl) = 1;
8791 DECL_CONTEXT (decl) = ctype;
8794 if (deletedp)
8795 DECL_DELETED_FN (decl) = 1;
8797 if (ctype)
8799 DECL_CONTEXT (decl) = ctype;
8800 if (funcdef_flag)
8801 check_class_member_definition_namespace (decl);
8804 if (ctype == NULL_TREE && DECL_MAIN_P (decl))
8806 if (PROCESSING_REAL_TEMPLATE_DECL_P())
8807 error_at (location, "cannot declare %<::main%> to be a template");
8808 if (inlinep & 1)
8809 error_at (declspecs->locations[ds_inline],
8810 "cannot declare %<::main%> to be inline");
8811 if (inlinep & 2)
8812 error_at (declspecs->locations[ds_constexpr],
8813 "cannot declare %<::main%> to be %<constexpr%>");
8814 if (!publicp)
8815 error_at (location, "cannot declare %<::main%> to be static");
8816 inlinep = 0;
8817 publicp = 1;
8820 /* Members of anonymous types and local classes have no linkage; make
8821 them internal. If a typedef is made later, this will be changed. */
8822 if (ctype && (!TREE_PUBLIC (TYPE_MAIN_DECL (ctype))
8823 || decl_function_context (TYPE_MAIN_DECL (ctype))))
8824 publicp = 0;
8826 if (publicp && cxx_dialect == cxx98)
8828 /* [basic.link]: A name with no linkage (notably, the name of a class
8829 or enumeration declared in a local scope) shall not be used to
8830 declare an entity with linkage.
8832 DR 757 relaxes this restriction for C++0x. */
8833 no_linkage_error (decl);
8836 TREE_PUBLIC (decl) = publicp;
8837 if (! publicp)
8839 DECL_INTERFACE_KNOWN (decl) = 1;
8840 DECL_NOT_REALLY_EXTERN (decl) = 1;
8843 /* If the declaration was declared inline, mark it as such. */
8844 if (inlinep)
8846 DECL_DECLARED_INLINE_P (decl) = 1;
8847 if (publicp)
8848 DECL_COMDAT (decl) = 1;
8850 if (inlinep & 2)
8851 DECL_DECLARED_CONSTEXPR_P (decl) = true;
8853 // If the concept declaration specifier was found, check
8854 // that the declaration satisfies the necessary requirements.
8855 if (concept_p)
8857 DECL_DECLARED_CONCEPT_P (decl) = true;
8858 check_concept_fn (decl);
8861 DECL_EXTERNAL (decl) = 1;
8862 if (TREE_CODE (type) == FUNCTION_TYPE)
8864 if (quals || rqual)
8865 TREE_TYPE (decl) = apply_memfn_quals (TREE_TYPE (decl),
8866 TYPE_UNQUALIFIED,
8867 REF_QUAL_NONE);
8869 if (quals)
8871 error (ctype
8872 ? G_("static member function %qD cannot have cv-qualifier")
8873 : G_("non-member function %qD cannot have cv-qualifier"),
8874 decl);
8875 quals = TYPE_UNQUALIFIED;
8878 if (rqual)
8880 error (ctype
8881 ? G_("static member function %qD cannot have ref-qualifier")
8882 : G_("non-member function %qD cannot have ref-qualifier"),
8883 decl);
8884 rqual = REF_QUAL_NONE;
8888 if (deduction_guide_p (decl))
8890 if (!DECL_NAMESPACE_SCOPE_P (decl))
8892 error_at (location, "deduction guide %qD must be declared at "
8893 "namespace scope", decl);
8894 return NULL_TREE;
8896 if (funcdef_flag)
8897 error_at (location,
8898 "deduction guide %qD must not have a function body", decl);
8900 else if (IDENTIFIER_ANY_OP_P (DECL_NAME (decl))
8901 && !grok_op_properties (decl, /*complain=*/true))
8902 return NULL_TREE;
8903 else if (UDLIT_OPER_P (DECL_NAME (decl)))
8905 bool long_long_unsigned_p;
8906 bool long_double_p;
8907 const char *suffix = NULL;
8908 /* [over.literal]/6: Literal operators shall not have C linkage. */
8909 if (DECL_LANGUAGE (decl) == lang_c)
8911 error_at (location, "literal operator with C linkage");
8912 maybe_show_extern_c_location ();
8913 return NULL_TREE;
8916 if (DECL_NAMESPACE_SCOPE_P (decl))
8918 if (!check_literal_operator_args (decl, &long_long_unsigned_p,
8919 &long_double_p))
8921 error_at (location, "%qD has invalid argument list", decl);
8922 return NULL_TREE;
8925 suffix = UDLIT_OP_SUFFIX (DECL_NAME (decl));
8926 if (long_long_unsigned_p)
8928 if (cpp_interpret_int_suffix (parse_in, suffix, strlen (suffix)))
8929 warning_at (location, 0, "integer suffix %qs"
8930 " shadowed by implementation", suffix);
8932 else if (long_double_p)
8934 if (cpp_interpret_float_suffix (parse_in, suffix, strlen (suffix)))
8935 warning_at (location, 0, "floating point suffix %qs"
8936 " shadowed by implementation", suffix);
8938 /* 17.6.3.3.5 */
8939 if (suffix[0] != '_'
8940 && !in_system_header_at (location)
8941 && !current_function_decl && !(friendp && !funcdef_flag))
8942 warning_at (location, OPT_Wliteral_suffix,
8943 "literal operator suffixes not preceded by %<_%>"
8944 " are reserved for future standardization");
8946 else
8948 error_at (location, "%qD must be a non-member function", decl);
8949 return NULL_TREE;
8953 if (funcdef_flag)
8954 /* Make the init_value nonzero so pushdecl knows this is not
8955 tentative. error_mark_node is replaced later with the BLOCK. */
8956 DECL_INITIAL (decl) = error_mark_node;
8958 if (TYPE_NOTHROW_P (type) || nothrow_libfn_p (decl))
8959 TREE_NOTHROW (decl) = 1;
8961 if (flag_openmp || flag_openmp_simd)
8963 /* Adjust "omp declare simd" attributes. */
8964 tree ods = lookup_attribute ("omp declare simd", *attrlist);
8965 if (ods)
8967 tree attr;
8968 for (attr = ods; attr;
8969 attr = lookup_attribute ("omp declare simd", TREE_CHAIN (attr)))
8971 if (TREE_CODE (type) == METHOD_TYPE)
8972 walk_tree (&TREE_VALUE (attr), declare_simd_adjust_this,
8973 DECL_ARGUMENTS (decl), NULL);
8974 if (TREE_VALUE (attr) != NULL_TREE)
8976 tree cl = TREE_VALUE (TREE_VALUE (attr));
8977 cl = c_omp_declare_simd_clauses_to_numbers
8978 (DECL_ARGUMENTS (decl), cl);
8979 if (cl)
8980 TREE_VALUE (TREE_VALUE (attr)) = cl;
8981 else
8982 TREE_VALUE (attr) = NULL_TREE;
8988 /* Caller will do the rest of this. */
8989 if (check < 0)
8990 return decl;
8992 if (ctype != NULL_TREE)
8993 grokclassfn (ctype, decl, flags);
8995 /* 12.4/3 */
8996 if (cxx_dialect >= cxx11
8997 && DECL_DESTRUCTOR_P (decl)
8998 && !TYPE_BEING_DEFINED (DECL_CONTEXT (decl))
8999 && !processing_template_decl)
9000 deduce_noexcept_on_destructor (decl);
9002 decl = check_explicit_specialization (orig_declarator, decl,
9003 template_count,
9004 2 * funcdef_flag +
9005 4 * (friendp != 0) +
9006 8 * concept_p,
9007 *attrlist);
9008 if (decl == error_mark_node)
9009 return NULL_TREE;
9011 if (DECL_STATIC_FUNCTION_P (decl))
9012 check_static_quals (decl, quals);
9014 if (attrlist)
9016 cplus_decl_attributes (&decl, *attrlist, 0);
9017 *attrlist = NULL_TREE;
9020 /* Check main's type after attributes have been applied. */
9021 if (ctype == NULL_TREE && DECL_MAIN_P (decl))
9023 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
9024 integer_type_node))
9026 tree oldtypeargs = TYPE_ARG_TYPES (TREE_TYPE (decl));
9027 tree newtype;
9028 error_at (declspecs->locations[ds_type_spec],
9029 "%<::main%> must return %<int%>");
9030 newtype = build_function_type (integer_type_node, oldtypeargs);
9031 TREE_TYPE (decl) = newtype;
9033 if (warn_main)
9034 check_main_parameter_types (decl);
9037 if (ctype != NULL_TREE && check)
9039 tree old_decl = check_classfn (ctype, decl,
9040 (processing_template_decl
9041 > template_class_depth (ctype))
9042 ? current_template_parms
9043 : NULL_TREE);
9045 if (old_decl == error_mark_node)
9046 return NULL_TREE;
9048 if (old_decl)
9050 tree ok;
9051 tree pushed_scope;
9053 if (TREE_CODE (old_decl) == TEMPLATE_DECL)
9054 /* Because grokfndecl is always supposed to return a
9055 FUNCTION_DECL, we pull out the DECL_TEMPLATE_RESULT
9056 here. We depend on our callers to figure out that its
9057 really a template that's being returned. */
9058 old_decl = DECL_TEMPLATE_RESULT (old_decl);
9060 if (DECL_STATIC_FUNCTION_P (old_decl)
9061 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE)
9063 /* Remove the `this' parm added by grokclassfn. */
9064 revert_static_member_fn (decl);
9065 check_static_quals (decl, quals);
9067 if (DECL_ARTIFICIAL (old_decl))
9069 error ("definition of implicitly-declared %qD", old_decl);
9070 return NULL_TREE;
9072 else if (DECL_DEFAULTED_FN (old_decl))
9074 error ("definition of explicitly-defaulted %q+D", decl);
9075 inform (DECL_SOURCE_LOCATION (old_decl),
9076 "%q#D explicitly defaulted here", old_decl);
9077 return NULL_TREE;
9080 /* Since we've smashed OLD_DECL to its
9081 DECL_TEMPLATE_RESULT, we must do the same to DECL. */
9082 if (TREE_CODE (decl) == TEMPLATE_DECL)
9083 decl = DECL_TEMPLATE_RESULT (decl);
9085 /* Attempt to merge the declarations. This can fail, in
9086 the case of some invalid specialization declarations. */
9087 pushed_scope = push_scope (ctype);
9088 ok = duplicate_decls (decl, old_decl, friendp);
9089 if (pushed_scope)
9090 pop_scope (pushed_scope);
9091 if (!ok)
9093 error ("no %q#D member function declared in class %qT",
9094 decl, ctype);
9095 return NULL_TREE;
9097 if (ok == error_mark_node)
9098 return NULL_TREE;
9099 return old_decl;
9103 if (DECL_CONSTRUCTOR_P (decl) && !grok_ctor_properties (ctype, decl))
9104 return NULL_TREE;
9106 if (ctype == NULL_TREE || check)
9107 return decl;
9109 if (virtualp)
9110 DECL_VIRTUAL_P (decl) = 1;
9112 return decl;
9115 /* decl is a FUNCTION_DECL.
9116 specifiers are the parsed virt-specifiers.
9118 Set flags to reflect the virt-specifiers.
9120 Returns decl. */
9122 static tree
9123 set_virt_specifiers (tree decl, cp_virt_specifiers specifiers)
9125 if (decl == NULL_TREE)
9126 return decl;
9127 if (specifiers & VIRT_SPEC_OVERRIDE)
9128 DECL_OVERRIDE_P (decl) = 1;
9129 if (specifiers & VIRT_SPEC_FINAL)
9130 DECL_FINAL_P (decl) = 1;
9131 return decl;
9134 /* DECL is a VAR_DECL for a static data member. Set flags to reflect
9135 the linkage that DECL will receive in the object file. */
9137 static void
9138 set_linkage_for_static_data_member (tree decl)
9140 /* A static data member always has static storage duration and
9141 external linkage. Note that static data members are forbidden in
9142 local classes -- the only situation in which a class has
9143 non-external linkage. */
9144 TREE_PUBLIC (decl) = 1;
9145 TREE_STATIC (decl) = 1;
9146 /* For non-template classes, static data members are always put
9147 out in exactly those files where they are defined, just as
9148 with ordinary namespace-scope variables. */
9149 if (!processing_template_decl)
9150 DECL_INTERFACE_KNOWN (decl) = 1;
9153 /* Create a VAR_DECL named NAME with the indicated TYPE.
9155 If SCOPE is non-NULL, it is the class type or namespace containing
9156 the variable. If SCOPE is NULL, the variable should is created in
9157 the innermost enclosing scope. */
9159 static tree
9160 grokvardecl (tree type,
9161 tree name,
9162 tree orig_declarator,
9163 const cp_decl_specifier_seq *declspecs,
9164 int initialized,
9165 int type_quals,
9166 int inlinep,
9167 bool conceptp,
9168 int template_count,
9169 tree scope)
9171 tree decl;
9172 tree explicit_scope;
9174 gcc_assert (!name || identifier_p (name));
9176 bool constp = (type_quals & TYPE_QUAL_CONST) != 0;
9177 bool volatilep = (type_quals & TYPE_QUAL_VOLATILE) != 0;
9179 /* Compute the scope in which to place the variable, but remember
9180 whether or not that scope was explicitly specified by the user. */
9181 explicit_scope = scope;
9182 if (!scope)
9184 /* An explicit "extern" specifier indicates a namespace-scope
9185 variable. */
9186 if (declspecs->storage_class == sc_extern)
9187 scope = current_decl_namespace ();
9188 else if (!at_function_scope_p ())
9189 scope = current_scope ();
9192 if (scope
9193 && (/* If the variable is a namespace-scope variable declared in a
9194 template, we need DECL_LANG_SPECIFIC. */
9195 (TREE_CODE (scope) == NAMESPACE_DECL && processing_template_decl)
9196 /* Similarly for namespace-scope variables with language linkage
9197 other than C++. */
9198 || (TREE_CODE (scope) == NAMESPACE_DECL
9199 && current_lang_name != lang_name_cplusplus)
9200 /* Similarly for static data members. */
9201 || TYPE_P (scope)
9202 /* Similarly for explicit specializations. */
9203 || (orig_declarator
9204 && TREE_CODE (orig_declarator) == TEMPLATE_ID_EXPR)))
9205 decl = build_lang_decl (VAR_DECL, name, type);
9206 else
9207 decl = build_decl (input_location, VAR_DECL, name, type);
9209 if (explicit_scope && TREE_CODE (explicit_scope) == NAMESPACE_DECL)
9210 set_decl_namespace (decl, explicit_scope, 0);
9211 else
9212 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
9214 if (declspecs->storage_class == sc_extern)
9216 DECL_THIS_EXTERN (decl) = 1;
9217 DECL_EXTERNAL (decl) = !initialized;
9220 if (DECL_CLASS_SCOPE_P (decl))
9222 set_linkage_for_static_data_member (decl);
9223 /* This function is only called with out-of-class definitions. */
9224 DECL_EXTERNAL (decl) = 0;
9225 check_class_member_definition_namespace (decl);
9227 /* At top level, either `static' or no s.c. makes a definition
9228 (perhaps tentative), and absence of `static' makes it public. */
9229 else if (toplevel_bindings_p ())
9231 TREE_PUBLIC (decl) = (declspecs->storage_class != sc_static
9232 && (DECL_THIS_EXTERN (decl)
9233 || ! constp
9234 || volatilep
9235 || inlinep));
9236 TREE_STATIC (decl) = ! DECL_EXTERNAL (decl);
9238 /* Not at top level, only `static' makes a static definition. */
9239 else
9241 TREE_STATIC (decl) = declspecs->storage_class == sc_static;
9242 TREE_PUBLIC (decl) = DECL_EXTERNAL (decl);
9245 if (decl_spec_seq_has_spec_p (declspecs, ds_thread))
9247 if (DECL_EXTERNAL (decl) || TREE_STATIC (decl))
9249 CP_DECL_THREAD_LOCAL_P (decl) = true;
9250 if (!processing_template_decl)
9251 set_decl_tls_model (decl, decl_default_tls_model (decl));
9253 if (declspecs->gnu_thread_keyword_p)
9254 SET_DECL_GNU_TLS_P (decl);
9257 /* If the type of the decl has no linkage, make sure that we'll
9258 notice that in mark_used. */
9259 if (cxx_dialect > cxx98
9260 && decl_linkage (decl) != lk_none
9261 && DECL_LANG_SPECIFIC (decl) == NULL
9262 && !DECL_EXTERN_C_P (decl)
9263 && no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/false))
9264 retrofit_lang_decl (decl);
9266 if (TREE_PUBLIC (decl))
9268 /* [basic.link]: A name with no linkage (notably, the name of a class
9269 or enumeration declared in a local scope) shall not be used to
9270 declare an entity with linkage.
9272 DR 757 relaxes this restriction for C++0x. */
9273 if (cxx_dialect < cxx11)
9274 no_linkage_error (decl);
9276 else
9277 DECL_INTERFACE_KNOWN (decl) = 1;
9279 if (DECL_NAME (decl)
9280 && MAIN_NAME_P (DECL_NAME (decl))
9281 && scope == global_namespace)
9282 error ("cannot declare %<::main%> to be a global variable");
9284 /* Check that the variable can be safely declared as a concept.
9285 Note that this also forbids explicit specializations. */
9286 if (conceptp)
9288 if (!processing_template_decl)
9290 error_at (declspecs->locations[ds_concept],
9291 "a non-template variable cannot be %<concept%>");
9292 return NULL_TREE;
9294 else
9295 DECL_DECLARED_CONCEPT_P (decl) = true;
9296 if (!same_type_ignoring_top_level_qualifiers_p (type, boolean_type_node))
9297 error_at (declspecs->locations[ds_type_spec],
9298 "concept must have type %<bool%>");
9300 else if (flag_concepts
9301 && processing_template_decl > template_class_depth (scope))
9303 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
9304 tree ci = build_constraints (reqs, NULL_TREE);
9305 set_constraints (decl, ci);
9308 // Handle explicit specializations and instantiations of variable templates.
9309 if (orig_declarator)
9310 decl = check_explicit_specialization (orig_declarator, decl,
9311 template_count, conceptp * 8);
9313 return decl != error_mark_node ? decl : NULL_TREE;
9316 /* Create and return a canonical pointer to member function type, for
9317 TYPE, which is a POINTER_TYPE to a METHOD_TYPE. */
9319 tree
9320 build_ptrmemfunc_type (tree type)
9322 tree field, fields;
9323 tree t;
9325 if (type == error_mark_node)
9326 return type;
9328 /* Make sure that we always have the unqualified pointer-to-member
9329 type first. */
9330 if (cp_cv_quals quals = cp_type_quals (type))
9332 tree unqual = build_ptrmemfunc_type (TYPE_MAIN_VARIANT (type));
9333 return cp_build_qualified_type (unqual, quals);
9336 /* If a canonical type already exists for this type, use it. We use
9337 this method instead of type_hash_canon, because it only does a
9338 simple equality check on the list of field members. */
9340 t = TYPE_PTRMEMFUNC_TYPE (type);
9341 if (t)
9342 return t;
9344 t = make_node (RECORD_TYPE);
9346 /* Let the front end know this is a pointer to member function. */
9347 TYPE_PTRMEMFUNC_FLAG (t) = 1;
9349 field = build_decl (input_location, FIELD_DECL, pfn_identifier, type);
9350 fields = field;
9352 field = build_decl (input_location, FIELD_DECL, delta_identifier,
9353 delta_type_node);
9354 DECL_CHAIN (field) = fields;
9355 fields = field;
9357 finish_builtin_struct (t, "__ptrmemfunc_type", fields, ptr_type_node);
9359 /* Zap out the name so that the back end will give us the debugging
9360 information for this anonymous RECORD_TYPE. */
9361 TYPE_NAME (t) = NULL_TREE;
9363 /* Cache this pointer-to-member type so that we can find it again
9364 later. */
9365 TYPE_PTRMEMFUNC_TYPE (type) = t;
9367 if (TYPE_STRUCTURAL_EQUALITY_P (type))
9368 SET_TYPE_STRUCTURAL_EQUALITY (t);
9369 else if (TYPE_CANONICAL (type) != type)
9370 TYPE_CANONICAL (t) = build_ptrmemfunc_type (TYPE_CANONICAL (type));
9372 return t;
9375 /* Create and return a pointer to data member type. */
9377 tree
9378 build_ptrmem_type (tree class_type, tree member_type)
9380 if (TREE_CODE (member_type) == METHOD_TYPE)
9382 cp_cv_quals quals = type_memfn_quals (member_type);
9383 cp_ref_qualifier rqual = type_memfn_rqual (member_type);
9384 member_type = build_memfn_type (member_type, class_type, quals, rqual);
9385 return build_ptrmemfunc_type (build_pointer_type (member_type));
9387 else
9389 gcc_assert (TREE_CODE (member_type) != FUNCTION_TYPE);
9390 return build_offset_type (class_type, member_type);
9394 /* DECL is a VAR_DECL defined in-class, whose TYPE is also given.
9395 Check to see that the definition is valid. Issue appropriate error
9396 messages. Return 1 if the definition is particularly bad, or 0
9397 otherwise. */
9399 static int
9400 check_static_variable_definition (tree decl, tree type)
9402 /* Avoid redundant diagnostics on out-of-class definitions. */
9403 if (!current_class_type || !TYPE_BEING_DEFINED (current_class_type))
9404 return 0;
9405 /* Can't check yet if we don't know the type. */
9406 if (dependent_type_p (type))
9407 return 0;
9408 /* If DECL is declared constexpr, we'll do the appropriate checks
9409 in check_initializer. Similarly for inline static data members. */
9410 if (DECL_P (decl)
9411 && (DECL_DECLARED_CONSTEXPR_P (decl)
9412 || undeduced_auto_decl (decl)
9413 || DECL_VAR_DECLARED_INLINE_P (decl)))
9414 return 0;
9415 else if (cxx_dialect >= cxx11 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type))
9417 if (!COMPLETE_TYPE_P (type))
9418 error_at (DECL_SOURCE_LOCATION (decl),
9419 "in-class initialization of static data member %q#D of "
9420 "incomplete type", decl);
9421 else if (literal_type_p (type))
9422 permerror (DECL_SOURCE_LOCATION (decl),
9423 "%<constexpr%> needed for in-class initialization of "
9424 "static data member %q#D of non-integral type", decl);
9425 else
9426 error_at (DECL_SOURCE_LOCATION (decl),
9427 "in-class initialization of static data member %q#D of "
9428 "non-literal type", decl);
9429 return 1;
9432 /* Motion 10 at San Diego: If a static const integral data member is
9433 initialized with an integral constant expression, the initializer
9434 may appear either in the declaration (within the class), or in
9435 the definition, but not both. If it appears in the class, the
9436 member is a member constant. The file-scope definition is always
9437 required. */
9438 if (!ARITHMETIC_TYPE_P (type) && TREE_CODE (type) != ENUMERAL_TYPE)
9440 error_at (DECL_SOURCE_LOCATION (decl),
9441 "invalid in-class initialization of static data member "
9442 "of non-integral type %qT",
9443 type);
9444 return 1;
9446 else if (!CP_TYPE_CONST_P (type))
9447 error_at (DECL_SOURCE_LOCATION (decl),
9448 "ISO C++ forbids in-class initialization of non-const "
9449 "static member %qD",
9450 decl);
9451 else if (!INTEGRAL_OR_ENUMERATION_TYPE_P (type))
9452 pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wpedantic,
9453 "ISO C++ forbids initialization of member constant "
9454 "%qD of non-integral type %qT", decl, type);
9456 return 0;
9459 /* *expr_p is part of the TYPE_SIZE of a variably-sized array. If any
9460 SAVE_EXPRs in *expr_p wrap expressions with side-effects, break those
9461 expressions out into temporary variables so that walk_tree doesn't
9462 step into them (c++/15764). */
9464 static tree
9465 stabilize_save_expr_r (tree *expr_p, int *walk_subtrees, void *data)
9467 hash_set<tree> *pset = (hash_set<tree> *)data;
9468 tree expr = *expr_p;
9469 if (TREE_CODE (expr) == SAVE_EXPR)
9471 tree op = TREE_OPERAND (expr, 0);
9472 cp_walk_tree (&op, stabilize_save_expr_r, data, pset);
9473 if (TREE_SIDE_EFFECTS (op))
9474 TREE_OPERAND (expr, 0) = get_temp_regvar (TREE_TYPE (op), op);
9475 *walk_subtrees = 0;
9477 else if (!EXPR_P (expr) || !TREE_SIDE_EFFECTS (expr))
9478 *walk_subtrees = 0;
9479 return NULL;
9482 /* Entry point for the above. */
9484 static void
9485 stabilize_vla_size (tree size)
9487 hash_set<tree> pset;
9488 /* Break out any function calls into temporary variables. */
9489 cp_walk_tree (&size, stabilize_save_expr_r, &pset, &pset);
9492 /* Reduce a SIZEOF_EXPR to its value. */
9494 tree
9495 fold_sizeof_expr (tree t)
9497 tree r;
9498 if (SIZEOF_EXPR_TYPE_P (t))
9499 r = cxx_sizeof_or_alignof_type (TREE_TYPE (TREE_OPERAND (t, 0)),
9500 SIZEOF_EXPR, false, false);
9501 else if (TYPE_P (TREE_OPERAND (t, 0)))
9502 r = cxx_sizeof_or_alignof_type (TREE_OPERAND (t, 0), SIZEOF_EXPR,
9503 false, false);
9504 else
9505 r = cxx_sizeof_or_alignof_expr (TREE_OPERAND (t, 0), SIZEOF_EXPR,
9506 false);
9507 if (r == error_mark_node)
9508 r = size_one_node;
9509 return r;
9512 /* Given the SIZE (i.e., number of elements) in an array, compute
9513 an appropriate index type for the array. If non-NULL, NAME is
9514 the name of the entity being declared. */
9516 tree
9517 compute_array_index_type (tree name, tree size, tsubst_flags_t complain)
9519 tree itype;
9520 tree osize = size;
9522 if (error_operand_p (size))
9523 return error_mark_node;
9525 if (!type_dependent_expression_p (size))
9527 osize = size = mark_rvalue_use (size);
9529 if (cxx_dialect < cxx11 && TREE_CODE (size) == NOP_EXPR
9530 && TREE_SIDE_EFFECTS (size))
9531 /* In C++98, we mark a non-constant array bound with a magic
9532 NOP_EXPR with TREE_SIDE_EFFECTS; don't fold in that case. */;
9533 else
9535 size = instantiate_non_dependent_expr_sfinae (size, complain);
9536 size = build_converted_constant_expr (size_type_node, size, complain);
9537 size = maybe_constant_value (size);
9539 if (!TREE_CONSTANT (size))
9540 size = osize;
9543 if (error_operand_p (size))
9544 return error_mark_node;
9546 /* The array bound must be an integer type. */
9547 tree type = TREE_TYPE (size);
9548 if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type))
9550 if (!(complain & tf_error))
9551 return error_mark_node;
9552 if (name)
9553 error ("size of array %qD has non-integral type %qT", name, type);
9554 else
9555 error ("size of array has non-integral type %qT", type);
9556 size = integer_one_node;
9560 /* A type is dependent if it is...an array type constructed from any
9561 dependent type or whose size is specified by a constant expression
9562 that is value-dependent. */
9563 /* We can only call value_dependent_expression_p on integral constant
9564 expressions; treat non-constant expressions as dependent, too. */
9565 if (processing_template_decl
9566 && (type_dependent_expression_p (size)
9567 || !TREE_CONSTANT (size) || value_dependent_expression_p (size)))
9569 /* We cannot do any checking for a SIZE that isn't known to be
9570 constant. Just build the index type and mark that it requires
9571 structural equality checks. */
9572 itype = build_index_type (build_min (MINUS_EXPR, sizetype,
9573 size, size_one_node));
9574 TYPE_DEPENDENT_P (itype) = 1;
9575 TYPE_DEPENDENT_P_VALID (itype) = 1;
9576 SET_TYPE_STRUCTURAL_EQUALITY (itype);
9577 return itype;
9580 if (TREE_CODE (size) != INTEGER_CST)
9582 tree folded = cp_fully_fold (size);
9583 if (TREE_CODE (folded) == INTEGER_CST)
9584 pedwarn (location_of (size), OPT_Wpedantic,
9585 "size of array is not an integral constant-expression");
9586 /* Use the folded result for VLAs, too; it will have resolved
9587 SIZEOF_EXPR. */
9588 size = folded;
9591 /* Normally, the array-bound will be a constant. */
9592 if (TREE_CODE (size) == INTEGER_CST)
9594 /* An array must have a positive number of elements. */
9595 if (!valid_constant_size_p (size))
9597 if (!(complain & tf_error))
9598 return error_mark_node;
9600 if (name)
9601 error ("size of array %qD is negative", name);
9602 else
9603 error ("size of array is negative");
9604 size = integer_one_node;
9606 /* As an extension we allow zero-sized arrays. */
9607 else if (integer_zerop (size))
9609 if (!(complain & tf_error))
9610 /* We must fail if performing argument deduction (as
9611 indicated by the state of complain), so that
9612 another substitution can be found. */
9613 return error_mark_node;
9614 else if (in_system_header_at (input_location))
9615 /* Allow them in system headers because glibc uses them. */;
9616 else if (name)
9617 pedwarn (input_location, OPT_Wpedantic, "ISO C++ forbids zero-size array %qD", name);
9618 else
9619 pedwarn (input_location, OPT_Wpedantic, "ISO C++ forbids zero-size array");
9622 else if (TREE_CONSTANT (size)
9623 /* We don't allow VLAs at non-function scopes, or during
9624 tentative template substitution. */
9625 || !at_function_scope_p ()
9626 || !(complain & tf_error))
9628 if (!(complain & tf_error))
9629 return error_mark_node;
9630 /* `(int) &fn' is not a valid array bound. */
9631 if (name)
9632 error ("size of array %qD is not an integral constant-expression",
9633 name);
9634 else
9635 error ("size of array is not an integral constant-expression");
9636 size = integer_one_node;
9638 else if (pedantic && warn_vla != 0)
9640 if (name)
9641 pedwarn (input_location, OPT_Wvla, "ISO C++ forbids variable length array %qD", name);
9642 else
9643 pedwarn (input_location, OPT_Wvla, "ISO C++ forbids variable length array");
9645 else if (warn_vla > 0)
9647 if (name)
9648 warning (OPT_Wvla,
9649 "variable length array %qD is used", name);
9650 else
9651 warning (OPT_Wvla,
9652 "variable length array is used");
9655 if (processing_template_decl && !TREE_CONSTANT (size))
9656 /* A variable sized array. */
9657 itype = build_min (MINUS_EXPR, sizetype, size, integer_one_node);
9658 else
9660 /* Compute the index of the largest element in the array. It is
9661 one less than the number of elements in the array. We save
9662 and restore PROCESSING_TEMPLATE_DECL so that computations in
9663 cp_build_binary_op will be appropriately folded. */
9665 processing_template_decl_sentinel s;
9666 itype = cp_build_binary_op (input_location,
9667 MINUS_EXPR,
9668 cp_convert (ssizetype, size, complain),
9669 cp_convert (ssizetype, integer_one_node,
9670 complain),
9671 complain);
9672 itype = maybe_constant_value (itype);
9675 if (!TREE_CONSTANT (itype))
9677 /* A variable sized array. */
9678 itype = variable_size (itype);
9680 stabilize_vla_size (itype);
9682 if (sanitize_flags_p (SANITIZE_VLA)
9683 && current_function_decl != NULL_TREE)
9685 /* We have to add 1 -- in the ubsan routine we generate
9686 LE_EXPR rather than LT_EXPR. */
9687 tree t = fold_build2 (PLUS_EXPR, TREE_TYPE (itype), itype,
9688 build_one_cst (TREE_TYPE (itype)));
9689 t = ubsan_instrument_vla (input_location, t);
9690 finish_expr_stmt (t);
9693 /* Make sure that there was no overflow when creating to a signed
9694 index type. (For example, on a 32-bit machine, an array with
9695 size 2^32 - 1 is too big.) */
9696 else if (TREE_CODE (itype) == INTEGER_CST
9697 && TREE_OVERFLOW (itype))
9699 if (!(complain & tf_error))
9700 return error_mark_node;
9701 error ("overflow in array dimension");
9702 TREE_OVERFLOW (itype) = 0;
9706 /* Create and return the appropriate index type. */
9707 itype = build_index_type (itype);
9709 /* If the index type were dependent, we would have returned early, so
9710 remember that it isn't. */
9711 TYPE_DEPENDENT_P (itype) = 0;
9712 TYPE_DEPENDENT_P_VALID (itype) = 1;
9713 return itype;
9716 /* Returns the scope (if any) in which the entity declared by
9717 DECLARATOR will be located. If the entity was declared with an
9718 unqualified name, NULL_TREE is returned. */
9720 tree
9721 get_scope_of_declarator (const cp_declarator *declarator)
9723 while (declarator && declarator->kind != cdk_id)
9724 declarator = declarator->declarator;
9726 /* If the declarator-id is a SCOPE_REF, the scope in which the
9727 declaration occurs is the first operand. */
9728 if (declarator
9729 && declarator->u.id.qualifying_scope)
9730 return declarator->u.id.qualifying_scope;
9732 /* Otherwise, the declarator is not a qualified name; the entity will
9733 be declared in the current scope. */
9734 return NULL_TREE;
9737 /* Returns an ARRAY_TYPE for an array with SIZE elements of the
9738 indicated TYPE. If non-NULL, NAME is the NAME of the declaration
9739 with this type. */
9741 static tree
9742 create_array_type_for_decl (tree name, tree type, tree size)
9744 tree itype = NULL_TREE;
9746 /* If things have already gone awry, bail now. */
9747 if (type == error_mark_node || size == error_mark_node)
9748 return error_mark_node;
9750 /* 8.3.4/1: If the type of the identifier of D contains the auto
9751 type-specifier, the program is ill-formed. */
9752 if (type_uses_auto (type))
9754 error ("%qD declared as array of %qT", name, type);
9755 return error_mark_node;
9758 /* If there are some types which cannot be array elements,
9759 issue an error-message and return. */
9760 switch (TREE_CODE (type))
9762 case VOID_TYPE:
9763 if (name)
9764 error ("declaration of %qD as array of void", name);
9765 else
9766 error ("creating array of void");
9767 return error_mark_node;
9769 case FUNCTION_TYPE:
9770 if (name)
9771 error ("declaration of %qD as array of functions", name);
9772 else
9773 error ("creating array of functions");
9774 return error_mark_node;
9776 case REFERENCE_TYPE:
9777 if (name)
9778 error ("declaration of %qD as array of references", name);
9779 else
9780 error ("creating array of references");
9781 return error_mark_node;
9783 case METHOD_TYPE:
9784 if (name)
9785 error ("declaration of %qD as array of function members", name);
9786 else
9787 error ("creating array of function members");
9788 return error_mark_node;
9790 default:
9791 break;
9794 /* [dcl.array]
9796 The constant expressions that specify the bounds of the arrays
9797 can be omitted only for the first member of the sequence. */
9798 if (TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type))
9800 if (name)
9801 error ("declaration of %qD as multidimensional array must "
9802 "have bounds for all dimensions except the first",
9803 name);
9804 else
9805 error ("multidimensional array must have bounds for all "
9806 "dimensions except the first");
9808 return error_mark_node;
9811 /* Figure out the index type for the array. */
9812 if (size)
9813 itype = compute_array_index_type (name, size, tf_warning_or_error);
9815 /* [dcl.array]
9816 T is called the array element type; this type shall not be [...] an
9817 abstract class type. */
9818 abstract_virtuals_error (name, type);
9820 return build_cplus_array_type (type, itype);
9823 /* Returns the smallest location that is not UNKNOWN_LOCATION. */
9825 static location_t
9826 min_location (location_t loca, location_t locb)
9828 if (loca == UNKNOWN_LOCATION
9829 || (locb != UNKNOWN_LOCATION
9830 && linemap_location_before_p (line_table, locb, loca)))
9831 return locb;
9832 return loca;
9835 /* Returns the smallest location != UNKNOWN_LOCATION among the
9836 three stored in LOCATIONS[ds_const], LOCATIONS[ds_volatile],
9837 and LOCATIONS[ds_restrict]. */
9839 static location_t
9840 smallest_type_quals_location (int type_quals, const location_t* locations)
9842 location_t loc = UNKNOWN_LOCATION;
9844 if (type_quals & TYPE_QUAL_CONST)
9845 loc = locations[ds_const];
9847 if (type_quals & TYPE_QUAL_VOLATILE)
9848 loc = min_location (loc, locations[ds_volatile]);
9850 if (type_quals & TYPE_QUAL_RESTRICT)
9851 loc = min_location (loc, locations[ds_restrict]);
9853 return loc;
9856 /* Check that it's OK to declare a function with the indicated TYPE
9857 and TYPE_QUALS. SFK indicates the kind of special function (if any)
9858 that this function is. OPTYPE is the type given in a conversion
9859 operator declaration, or the class type for a constructor/destructor.
9860 Returns the actual return type of the function; that may be different
9861 than TYPE if an error occurs, or for certain special functions. */
9863 static tree
9864 check_special_function_return_type (special_function_kind sfk,
9865 tree type,
9866 tree optype,
9867 int type_quals,
9868 const location_t* locations)
9870 switch (sfk)
9872 case sfk_constructor:
9873 if (type)
9874 error ("return type specification for constructor invalid");
9875 else if (type_quals != TYPE_UNQUALIFIED)
9876 error_at (smallest_type_quals_location (type_quals, locations),
9877 "qualifiers are not allowed on constructor declaration");
9879 if (targetm.cxx.cdtor_returns_this ())
9880 type = build_pointer_type (optype);
9881 else
9882 type = void_type_node;
9883 break;
9885 case sfk_destructor:
9886 if (type)
9887 error ("return type specification for destructor invalid");
9888 else if (type_quals != TYPE_UNQUALIFIED)
9889 error_at (smallest_type_quals_location (type_quals, locations),
9890 "qualifiers are not allowed on destructor declaration");
9892 /* We can't use the proper return type here because we run into
9893 problems with ambiguous bases and covariant returns. */
9894 if (targetm.cxx.cdtor_returns_this ())
9895 type = build_pointer_type (void_type_node);
9896 else
9897 type = void_type_node;
9898 break;
9900 case sfk_conversion:
9901 if (type)
9902 error ("return type specified for %<operator %T%>", optype);
9903 else if (type_quals != TYPE_UNQUALIFIED)
9904 error_at (smallest_type_quals_location (type_quals, locations),
9905 "qualifiers are not allowed on declaration of "
9906 "%<operator %T%>", optype);
9908 type = optype;
9909 break;
9911 case sfk_deduction_guide:
9912 if (type)
9913 error ("return type specified for deduction guide");
9914 else if (type_quals != TYPE_UNQUALIFIED)
9915 error_at (smallest_type_quals_location (type_quals, locations),
9916 "qualifiers are not allowed on declaration of "
9917 "deduction guide");
9918 if (TREE_CODE (optype) == TEMPLATE_TEMPLATE_PARM)
9920 error ("template template parameter %qT in declaration of "
9921 "deduction guide", optype);
9922 type = error_mark_node;
9924 else
9925 type = make_template_placeholder (CLASSTYPE_TI_TEMPLATE (optype));
9926 for (int i = 0; i < ds_last; ++i)
9927 if (i != ds_explicit && locations[i])
9928 error_at (locations[i],
9929 "decl-specifier in declaration of deduction guide");
9930 break;
9932 default:
9933 gcc_unreachable ();
9936 return type;
9939 /* A variable or data member (whose unqualified name is IDENTIFIER)
9940 has been declared with the indicated TYPE. If the TYPE is not
9941 acceptable, issue an error message and return a type to use for
9942 error-recovery purposes. */
9944 tree
9945 check_var_type (tree identifier, tree type)
9947 if (VOID_TYPE_P (type))
9949 if (!identifier)
9950 error ("unnamed variable or field declared void");
9951 else if (identifier_p (identifier))
9953 gcc_assert (!IDENTIFIER_ANY_OP_P (identifier));
9954 error ("variable or field %qE declared void", identifier);
9956 else
9957 error ("variable or field declared void");
9958 type = error_mark_node;
9961 return type;
9964 /* Handle declaring DECL as an inline variable. */
9966 static void
9967 mark_inline_variable (tree decl, location_t loc)
9969 bool inlinep = true;
9970 if (! toplevel_bindings_p ())
9972 error_at (loc, "%<inline%> specifier invalid for variable "
9973 "%qD declared at block scope", decl);
9974 inlinep = false;
9976 else if (cxx_dialect < cxx17)
9977 pedwarn (loc, 0, "inline variables are only available "
9978 "with -std=c++17 or -std=gnu++17");
9979 if (inlinep)
9981 retrofit_lang_decl (decl);
9982 SET_DECL_VAR_DECLARED_INLINE_P (decl);
9987 /* Assign a typedef-given name to a class or enumeration type declared
9988 as anonymous at first. This was split out of grokdeclarator
9989 because it is also used in libcc1. */
9991 void
9992 name_unnamed_type (tree type, tree decl)
9994 gcc_assert (TYPE_UNNAMED_P (type));
9996 /* Replace the anonymous name with the real name everywhere. */
9997 for (tree t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
9999 if (anon_aggrname_p (TYPE_IDENTIFIER (t)))
10000 /* We do not rename the debug info representing the
10001 unnamed tagged type because the standard says in
10002 [dcl.typedef] that the naming applies only for
10003 linkage purposes. */
10004 /*debug_hooks->set_name (t, decl);*/
10005 TYPE_NAME (t) = decl;
10008 if (TYPE_LANG_SPECIFIC (type))
10009 TYPE_WAS_UNNAMED (type) = 1;
10011 /* If this is a typedef within a template class, the nested
10012 type is a (non-primary) template. The name for the
10013 template needs updating as well. */
10014 if (TYPE_LANG_SPECIFIC (type) && CLASSTYPE_TEMPLATE_INFO (type))
10015 DECL_NAME (CLASSTYPE_TI_TEMPLATE (type))
10016 = TYPE_IDENTIFIER (type);
10018 /* Adjust linkage now that we aren't unnamed anymore. */
10019 reset_type_linkage (type);
10021 /* FIXME remangle member functions; member functions of a
10022 type with external linkage have external linkage. */
10024 /* Check that our job is done, and that it would fail if we
10025 attempted to do it again. */
10026 gcc_assert (!TYPE_UNNAMED_P (type));
10029 /* Given declspecs and a declarator (abstract or otherwise), determine
10030 the name and type of the object declared and construct a DECL node
10031 for it.
10033 DECLSPECS points to the representation of declaration-specifier
10034 sequence that precedes declarator.
10036 DECL_CONTEXT says which syntactic context this declaration is in:
10037 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
10038 FUNCDEF for a function definition. Like NORMAL but a few different
10039 error messages in each case. Return value may be zero meaning
10040 this definition is too screwy to try to parse.
10041 MEMFUNCDEF for a function definition. Like FUNCDEF but prepares to
10042 handle member functions (which have FIELD context).
10043 Return value may be zero meaning this definition is too screwy to
10044 try to parse.
10045 PARM for a parameter declaration (either within a function prototype
10046 or before a function body). Make a PARM_DECL, or return void_type_node.
10047 TPARM for a template parameter declaration.
10048 CATCHPARM for a parameter declaration before a catch clause.
10049 TYPENAME if for a typename (in a cast or sizeof).
10050 Don't make a DECL node; just return the ..._TYPE node.
10051 FIELD for a struct or union field; make a FIELD_DECL.
10052 BITFIELD for a field with specified width.
10054 INITIALIZED is as for start_decl.
10056 ATTRLIST is a pointer to the list of attributes, which may be NULL
10057 if there are none; *ATTRLIST may be modified if attributes from inside
10058 the declarator should be applied to the declaration.
10060 When this function is called, scoping variables (such as
10061 CURRENT_CLASS_TYPE) should reflect the scope in which the
10062 declaration occurs, not the scope in which the new declaration will
10063 be placed. For example, on:
10065 void S::f() { ... }
10067 when grokdeclarator is called for `S::f', the CURRENT_CLASS_TYPE
10068 should not be `S'.
10070 Returns a DECL (if a declarator is present), a TYPE (if there is no
10071 declarator, in cases like "struct S;"), or the ERROR_MARK_NODE if an
10072 error occurs. */
10074 tree
10075 grokdeclarator (const cp_declarator *declarator,
10076 cp_decl_specifier_seq *declspecs,
10077 enum decl_context decl_context,
10078 int initialized,
10079 tree* attrlist)
10081 tree type = NULL_TREE;
10082 int longlong = 0;
10083 int explicit_intN = 0;
10084 int virtualp, explicitp, friendp, inlinep, staticp;
10085 int explicit_int = 0;
10086 int explicit_char = 0;
10087 int defaulted_int = 0;
10089 tree typedef_decl = NULL_TREE;
10090 const char *name = NULL;
10091 tree typedef_type = NULL_TREE;
10092 /* True if this declarator is a function definition. */
10093 bool funcdef_flag = false;
10094 cp_declarator_kind innermost_code = cdk_error;
10095 int bitfield = 0;
10096 #if 0
10097 /* See the code below that used this. */
10098 tree decl_attr = NULL_TREE;
10099 #endif
10101 /* Keep track of what sort of function is being processed
10102 so that we can warn about default return values, or explicit
10103 return values which do not match prescribed defaults. */
10104 special_function_kind sfk = sfk_none;
10106 tree dname = NULL_TREE;
10107 tree ctor_return_type = NULL_TREE;
10108 enum overload_flags flags = NO_SPECIAL;
10109 /* cv-qualifiers that apply to the declarator, for a declaration of
10110 a member function. */
10111 cp_cv_quals memfn_quals = TYPE_UNQUALIFIED;
10112 /* virt-specifiers that apply to the declarator, for a declaration of
10113 a member function. */
10114 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
10115 /* ref-qualifier that applies to the declarator, for a declaration of
10116 a member function. */
10117 cp_ref_qualifier rqual = REF_QUAL_NONE;
10118 /* cv-qualifiers that apply to the type specified by the DECLSPECS. */
10119 int type_quals = TYPE_UNQUALIFIED;
10120 tree raises = NULL_TREE;
10121 int template_count = 0;
10122 tree returned_attrs = NULL_TREE;
10123 tree parms = NULL_TREE;
10124 const cp_declarator *id_declarator;
10125 /* The unqualified name of the declarator; either an
10126 IDENTIFIER_NODE, BIT_NOT_EXPR, or TEMPLATE_ID_EXPR. */
10127 tree unqualified_id;
10128 /* The class type, if any, in which this entity is located,
10129 or NULL_TREE if none. Note that this value may be different from
10130 the current class type; for example if an attempt is made to declare
10131 "A::f" inside "B", this value will be "A". */
10132 tree ctype = current_class_type;
10133 /* The NAMESPACE_DECL for the namespace in which this entity is
10134 located. If an unqualified name is used to declare the entity,
10135 this value will be NULL_TREE, even if the entity is located at
10136 namespace scope. */
10137 tree in_namespace = NULL_TREE;
10138 cp_storage_class storage_class;
10139 bool unsigned_p, signed_p, short_p, long_p, thread_p;
10140 bool type_was_error_mark_node = false;
10141 bool parameter_pack_p = declarator ? declarator->parameter_pack_p : false;
10142 bool template_type_arg = false;
10143 bool template_parm_flag = false;
10144 bool typedef_p = decl_spec_seq_has_spec_p (declspecs, ds_typedef);
10145 bool constexpr_p = decl_spec_seq_has_spec_p (declspecs, ds_constexpr);
10146 bool late_return_type_p = false;
10147 bool array_parameter_p = false;
10148 source_location saved_loc = input_location;
10149 tree reqs = NULL_TREE;
10151 signed_p = decl_spec_seq_has_spec_p (declspecs, ds_signed);
10152 unsigned_p = decl_spec_seq_has_spec_p (declspecs, ds_unsigned);
10153 short_p = decl_spec_seq_has_spec_p (declspecs, ds_short);
10154 long_p = decl_spec_seq_has_spec_p (declspecs, ds_long);
10155 longlong = decl_spec_seq_has_spec_p (declspecs, ds_long_long);
10156 explicit_intN = declspecs->explicit_intN_p;
10157 thread_p = decl_spec_seq_has_spec_p (declspecs, ds_thread);
10159 // Was concept_p specified? Note that ds_concept
10160 // implies ds_constexpr!
10161 bool concept_p = decl_spec_seq_has_spec_p (declspecs, ds_concept);
10162 if (concept_p)
10163 constexpr_p = true;
10165 if (decl_spec_seq_has_spec_p (declspecs, ds_const))
10166 type_quals |= TYPE_QUAL_CONST;
10167 if (decl_spec_seq_has_spec_p (declspecs, ds_volatile))
10168 type_quals |= TYPE_QUAL_VOLATILE;
10169 if (decl_spec_seq_has_spec_p (declspecs, ds_restrict))
10170 type_quals |= TYPE_QUAL_RESTRICT;
10172 if (decl_context == FUNCDEF)
10173 funcdef_flag = true, decl_context = NORMAL;
10174 else if (decl_context == MEMFUNCDEF)
10175 funcdef_flag = true, decl_context = FIELD;
10176 else if (decl_context == BITFIELD)
10177 bitfield = 1, decl_context = FIELD;
10178 else if (decl_context == TEMPLATE_TYPE_ARG)
10179 template_type_arg = true, decl_context = TYPENAME;
10180 else if (decl_context == TPARM)
10181 template_parm_flag = true, decl_context = PARM;
10183 if (initialized > 1)
10184 funcdef_flag = true;
10186 location_t typespec_loc = smallest_type_quals_location (type_quals,
10187 declspecs->locations);
10188 if (typespec_loc == UNKNOWN_LOCATION)
10189 typespec_loc = declspecs->locations[ds_type_spec];
10190 if (typespec_loc == UNKNOWN_LOCATION)
10191 typespec_loc = input_location;
10193 /* Look inside a declarator for the name being declared
10194 and get it as a string, for an error message. */
10195 for (id_declarator = declarator;
10196 id_declarator;
10197 id_declarator = id_declarator->declarator)
10199 if (id_declarator->kind != cdk_id)
10200 innermost_code = id_declarator->kind;
10202 switch (id_declarator->kind)
10204 case cdk_function:
10205 if (id_declarator->declarator
10206 && id_declarator->declarator->kind == cdk_id)
10208 sfk = id_declarator->declarator->u.id.sfk;
10209 if (sfk == sfk_destructor)
10210 flags = DTOR_FLAG;
10212 break;
10214 case cdk_id:
10216 tree qualifying_scope = id_declarator->u.id.qualifying_scope;
10217 tree decl = id_declarator->u.id.unqualified_name;
10218 if (!decl)
10219 break;
10220 if (qualifying_scope)
10222 if (check_for_bare_parameter_packs (qualifying_scope,
10223 id_declarator->id_loc))
10224 return error_mark_node;
10225 if (at_function_scope_p ())
10227 /* [dcl.meaning]
10229 A declarator-id shall not be qualified except
10230 for ...
10232 None of the cases are permitted in block
10233 scope. */
10234 if (qualifying_scope == global_namespace)
10235 error ("invalid use of qualified-name %<::%D%>",
10236 decl);
10237 else if (TYPE_P (qualifying_scope))
10238 error ("invalid use of qualified-name %<%T::%D%>",
10239 qualifying_scope, decl);
10240 else
10241 error ("invalid use of qualified-name %<%D::%D%>",
10242 qualifying_scope, decl);
10243 return error_mark_node;
10245 else if (TYPE_P (qualifying_scope))
10247 ctype = qualifying_scope;
10248 if (!MAYBE_CLASS_TYPE_P (ctype))
10250 error ("%q#T is not a class or a namespace", ctype);
10251 ctype = NULL_TREE;
10253 else if (innermost_code != cdk_function
10254 && current_class_type
10255 && !uniquely_derived_from_p (ctype,
10256 current_class_type))
10258 error ("invalid use of qualified-name %<%T::%D%>",
10259 qualifying_scope, decl);
10260 return error_mark_node;
10263 else if (TREE_CODE (qualifying_scope) == NAMESPACE_DECL)
10264 in_namespace = qualifying_scope;
10266 switch (TREE_CODE (decl))
10268 case BIT_NOT_EXPR:
10270 if (innermost_code != cdk_function)
10272 error ("declaration of %qD as non-function", decl);
10273 return error_mark_node;
10275 else if (!qualifying_scope
10276 && !(current_class_type && at_class_scope_p ()))
10278 error ("declaration of %qD as non-member", decl);
10279 return error_mark_node;
10282 tree type = TREE_OPERAND (decl, 0);
10283 if (TYPE_P (type))
10284 type = constructor_name (type);
10285 name = identifier_to_locale (IDENTIFIER_POINTER (type));
10286 dname = decl;
10288 break;
10290 case TEMPLATE_ID_EXPR:
10292 tree fns = TREE_OPERAND (decl, 0);
10294 dname = fns;
10295 if (!identifier_p (dname))
10296 dname = OVL_NAME (dname);
10298 /* Fall through. */
10300 case IDENTIFIER_NODE:
10301 if (identifier_p (decl))
10302 dname = decl;
10304 if (IDENTIFIER_KEYWORD_P (dname))
10306 error ("declarator-id missing; using reserved word %qD",
10307 dname);
10308 name = identifier_to_locale (IDENTIFIER_POINTER (dname));
10310 else if (!IDENTIFIER_CONV_OP_P (dname))
10311 name = identifier_to_locale (IDENTIFIER_POINTER (dname));
10312 else
10314 gcc_assert (flags == NO_SPECIAL);
10315 flags = TYPENAME_FLAG;
10316 sfk = sfk_conversion;
10317 tree glob = get_global_binding (dname);
10318 if (glob && TREE_CODE (glob) == TYPE_DECL)
10319 name = identifier_to_locale (IDENTIFIER_POINTER (dname));
10320 else
10321 name = "<invalid operator>";
10323 break;
10325 default:
10326 gcc_unreachable ();
10328 break;
10331 case cdk_array:
10332 case cdk_pointer:
10333 case cdk_reference:
10334 case cdk_ptrmem:
10335 break;
10337 case cdk_decomp:
10338 name = "structured binding";
10339 break;
10341 case cdk_error:
10342 return error_mark_node;
10344 default:
10345 gcc_unreachable ();
10347 if (id_declarator->kind == cdk_id)
10348 break;
10351 /* [dcl.fct.edf]
10353 The declarator in a function-definition shall have the form
10354 D1 ( parameter-declaration-clause) ... */
10355 if (funcdef_flag && innermost_code != cdk_function)
10357 error ("function definition does not declare parameters");
10358 return error_mark_node;
10361 if (flags == TYPENAME_FLAG
10362 && innermost_code != cdk_function
10363 && ! (ctype && !declspecs->any_specifiers_p))
10365 error ("declaration of %qD as non-function", dname);
10366 return error_mark_node;
10369 if (dname && identifier_p (dname))
10371 if (UDLIT_OPER_P (dname)
10372 && innermost_code != cdk_function)
10374 error ("declaration of %qD as non-function", dname);
10375 return error_mark_node;
10378 if (IDENTIFIER_ANY_OP_P (dname))
10380 if (typedef_p)
10382 error ("declaration of %qD as %<typedef%>", dname);
10383 return error_mark_node;
10385 else if (decl_context == PARM || decl_context == CATCHPARM)
10387 error ("declaration of %qD as parameter", dname);
10388 return error_mark_node;
10393 /* Anything declared one level down from the top level
10394 must be one of the parameters of a function
10395 (because the body is at least two levels down). */
10397 /* This heuristic cannot be applied to C++ nodes! Fixed, however,
10398 by not allowing C++ class definitions to specify their parameters
10399 with xdecls (must be spec.d in the parmlist).
10401 Since we now wait to push a class scope until we are sure that
10402 we are in a legitimate method context, we must set oldcname
10403 explicitly (since current_class_name is not yet alive).
10405 We also want to avoid calling this a PARM if it is in a namespace. */
10407 if (decl_context == NORMAL && !toplevel_bindings_p ())
10409 cp_binding_level *b = current_binding_level;
10410 current_binding_level = b->level_chain;
10411 if (current_binding_level != 0 && toplevel_bindings_p ())
10412 decl_context = PARM;
10413 current_binding_level = b;
10416 if (name == NULL)
10417 name = decl_context == PARM ? "parameter" : "type name";
10419 if (concept_p && typedef_p)
10421 error_at (declspecs->locations[ds_concept],
10422 "%<concept%> cannot appear in a typedef declaration");
10423 return error_mark_node;
10426 if (constexpr_p && typedef_p)
10428 error_at (declspecs->locations[ds_constexpr],
10429 "%<constexpr%> cannot appear in a typedef declaration");
10430 return error_mark_node;
10433 /* If there were multiple types specified in the decl-specifier-seq,
10434 issue an error message. */
10435 if (declspecs->multiple_types_p)
10437 error ("two or more data types in declaration of %qs", name);
10438 return error_mark_node;
10441 if (declspecs->conflicting_specifiers_p)
10443 error ("conflicting specifiers in declaration of %qs", name);
10444 return error_mark_node;
10447 /* Extract the basic type from the decl-specifier-seq. */
10448 type = declspecs->type;
10449 if (type == error_mark_node)
10451 type = NULL_TREE;
10452 type_was_error_mark_node = true;
10454 cp_warn_deprecated_use (type);
10455 if (type && TREE_CODE (type) == TYPE_DECL)
10457 typedef_decl = type;
10458 type = TREE_TYPE (typedef_decl);
10459 if (DECL_ARTIFICIAL (typedef_decl))
10460 cp_warn_deprecated_use (type);
10462 /* No type at all: default to `int', and set DEFAULTED_INT
10463 because it was not a user-defined typedef. */
10464 if (type == NULL_TREE)
10466 if (signed_p || unsigned_p || long_p || short_p)
10468 /* These imply 'int'. */
10469 type = integer_type_node;
10470 defaulted_int = 1;
10472 /* If we just have "complex", it is equivalent to "complex double". */
10473 else if (!longlong && !explicit_intN
10474 && decl_spec_seq_has_spec_p (declspecs, ds_complex))
10476 type = double_type_node;
10477 pedwarn (declspecs->locations[ds_complex], OPT_Wpedantic,
10478 "ISO C++ does not support plain %<complex%> meaning "
10479 "%<double complex%>");
10482 /* Gather flags. */
10483 explicit_int = declspecs->explicit_int_p;
10484 explicit_char = declspecs->explicit_char_p;
10486 #if 0
10487 /* See the code below that used this. */
10488 if (typedef_decl)
10489 decl_attr = DECL_ATTRIBUTES (typedef_decl);
10490 #endif
10491 typedef_type = type;
10493 if (sfk == sfk_conversion || sfk == sfk_deduction_guide)
10494 ctor_return_type = TREE_TYPE (dname);
10495 else
10496 ctor_return_type = ctype;
10498 if (sfk != sfk_none)
10500 type = check_special_function_return_type (sfk, type,
10501 ctor_return_type,
10502 type_quals,
10503 declspecs->locations);
10504 type_quals = TYPE_UNQUALIFIED;
10506 else if (type == NULL_TREE)
10508 int is_main;
10510 explicit_int = -1;
10512 /* We handle `main' specially here, because 'main () { }' is so
10513 common. With no options, it is allowed. With -Wreturn-type,
10514 it is a warning. It is only an error with -pedantic-errors. */
10515 is_main = (funcdef_flag
10516 && dname && identifier_p (dname)
10517 && MAIN_NAME_P (dname)
10518 && ctype == NULL_TREE
10519 && in_namespace == NULL_TREE
10520 && current_namespace == global_namespace);
10522 if (type_was_error_mark_node)
10523 /* We've already issued an error, don't complain more. */;
10524 else if (in_system_header_at (input_location) || flag_ms_extensions)
10525 /* Allow it, sigh. */;
10526 else if (! is_main)
10527 permerror (input_location, "ISO C++ forbids declaration of %qs with no type", name);
10528 else if (pedantic)
10529 pedwarn (input_location, OPT_Wpedantic,
10530 "ISO C++ forbids declaration of %qs with no type", name);
10531 else
10532 warning (OPT_Wreturn_type,
10533 "ISO C++ forbids declaration of %qs with no type", name);
10535 if (type_was_error_mark_node && template_parm_flag)
10536 /* FIXME we should be able to propagate the error_mark_node as is
10537 for other contexts too. */
10538 type = error_mark_node;
10539 else
10540 type = integer_type_node;
10543 ctype = NULL_TREE;
10545 if (explicit_intN)
10547 if (! int_n_enabled_p[declspecs->int_n_idx])
10549 error ("%<__int%d%> is not supported by this target",
10550 int_n_data[declspecs->int_n_idx].bitsize);
10551 explicit_intN = false;
10553 else if (pedantic && ! in_system_header_at (input_location))
10554 pedwarn (input_location, OPT_Wpedantic,
10555 "ISO C++ does not support %<__int%d%> for %qs",
10556 int_n_data[declspecs->int_n_idx].bitsize, name);
10559 /* Now process the modifiers that were specified
10560 and check for invalid combinations. */
10562 /* Long double is a special combination. */
10563 if (long_p && !longlong && TYPE_MAIN_VARIANT (type) == double_type_node)
10565 long_p = false;
10566 type = cp_build_qualified_type (long_double_type_node,
10567 cp_type_quals (type));
10570 /* Check all other uses of type modifiers. */
10572 if (unsigned_p || signed_p || long_p || short_p)
10574 location_t loc;
10575 const char *key;
10576 if (unsigned_p)
10578 key = "unsigned";
10579 loc = declspecs->locations[ds_unsigned];
10581 else if (signed_p)
10583 key = "signed";
10584 loc = declspecs->locations[ds_signed];
10586 else if (longlong)
10588 key = "long long";
10589 loc = declspecs->locations[ds_long_long];
10591 else if (long_p)
10593 key = "long";
10594 loc = declspecs->locations[ds_long];
10596 else /* if (short_p) */
10598 key = "short";
10599 loc = declspecs->locations[ds_short];
10602 int ok = 0;
10604 if (signed_p && unsigned_p)
10606 gcc_rich_location richloc (declspecs->locations[ds_signed]);
10607 richloc.add_range (declspecs->locations[ds_unsigned], false);
10608 error_at (&richloc,
10609 "%<signed%> and %<unsigned%> specified together");
10611 else if (long_p && short_p)
10613 gcc_rich_location richloc (declspecs->locations[ds_long]);
10614 richloc.add_range (declspecs->locations[ds_short], false);
10615 error_at (&richloc, "%<long%> and %<short%> specified together");
10617 else if (TREE_CODE (type) != INTEGER_TYPE
10618 || type == char16_type_node || type == char32_type_node
10619 || ((long_p || short_p)
10620 && (explicit_char || explicit_intN)))
10621 error_at (loc, "%qs specified with %qT", key, type);
10622 else if (!explicit_int && !defaulted_int
10623 && !explicit_char && !explicit_intN)
10625 if (typedef_decl)
10627 pedwarn (loc, OPT_Wpedantic, "%qs specified with %qT",
10628 key, type);
10629 ok = !flag_pedantic_errors;
10631 else if (declspecs->decltype_p)
10632 error_at (loc, "%qs specified with %<decltype%>", key);
10633 else
10634 error_at (loc, "%qs specified with %<typeof%>", key);
10636 else
10637 ok = 1;
10639 /* Discard the type modifiers if they are invalid. */
10640 if (! ok)
10642 unsigned_p = false;
10643 signed_p = false;
10644 long_p = false;
10645 short_p = false;
10646 longlong = 0;
10650 /* Decide whether an integer type is signed or not.
10651 Optionally treat bitfields as signed by default. */
10652 if (unsigned_p
10653 /* [class.bit]
10655 It is implementation-defined whether a plain (neither
10656 explicitly signed or unsigned) char, short, int, or long
10657 bit-field is signed or unsigned.
10659 Naturally, we extend this to long long as well. Note that
10660 this does not include wchar_t. */
10661 || (bitfield && !flag_signed_bitfields
10662 && !signed_p
10663 /* A typedef for plain `int' without `signed' can be
10664 controlled just like plain `int', but a typedef for
10665 `signed int' cannot be so controlled. */
10666 && !(typedef_decl
10667 && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl))
10668 && TREE_CODE (type) == INTEGER_TYPE
10669 && !same_type_p (TYPE_MAIN_VARIANT (type), wchar_type_node)))
10671 if (explicit_intN)
10672 type = int_n_trees[declspecs->int_n_idx].unsigned_type;
10673 else if (longlong)
10674 type = long_long_unsigned_type_node;
10675 else if (long_p)
10676 type = long_unsigned_type_node;
10677 else if (short_p)
10678 type = short_unsigned_type_node;
10679 else if (type == char_type_node)
10680 type = unsigned_char_type_node;
10681 else if (typedef_decl)
10682 type = unsigned_type_for (type);
10683 else
10684 type = unsigned_type_node;
10686 else if (signed_p && type == char_type_node)
10687 type = signed_char_type_node;
10688 else if (explicit_intN)
10689 type = int_n_trees[declspecs->int_n_idx].signed_type;
10690 else if (longlong)
10691 type = long_long_integer_type_node;
10692 else if (long_p)
10693 type = long_integer_type_node;
10694 else if (short_p)
10695 type = short_integer_type_node;
10697 if (decl_spec_seq_has_spec_p (declspecs, ds_complex))
10699 if (TREE_CODE (type) != INTEGER_TYPE && TREE_CODE (type) != REAL_TYPE)
10700 error ("complex invalid for %qs", name);
10701 /* If a modifier is specified, the resulting complex is the complex
10702 form of TYPE. E.g, "complex short" is "complex short int". */
10703 else if (type == integer_type_node)
10704 type = complex_integer_type_node;
10705 else if (type == float_type_node)
10706 type = complex_float_type_node;
10707 else if (type == double_type_node)
10708 type = complex_double_type_node;
10709 else if (type == long_double_type_node)
10710 type = complex_long_double_type_node;
10711 else
10712 type = build_complex_type (type);
10715 /* If we're using the injected-class-name to form a compound type or a
10716 declaration, replace it with the underlying class so we don't get
10717 redundant typedefs in the debug output. But if we are returning the
10718 type unchanged, leave it alone so that it's available to
10719 maybe_get_template_decl_from_type_decl. */
10720 if (CLASS_TYPE_P (type)
10721 && DECL_SELF_REFERENCE_P (TYPE_NAME (type))
10722 && type == TREE_TYPE (TYPE_NAME (type))
10723 && (declarator || type_quals))
10724 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
10726 type_quals |= cp_type_quals (type);
10727 type = cp_build_qualified_type_real
10728 (type, type_quals, ((((typedef_decl && !DECL_ARTIFICIAL (typedef_decl))
10729 || declspecs->decltype_p)
10730 ? tf_ignore_bad_quals : 0) | tf_warning_or_error));
10731 /* We might have ignored or rejected some of the qualifiers. */
10732 type_quals = cp_type_quals (type);
10734 if (cxx_dialect >= cxx17 && type && is_auto (type)
10735 && innermost_code != cdk_function
10736 && id_declarator && declarator != id_declarator)
10737 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (type))
10739 error_at (typespec_loc, "template placeholder type %qT must be followed "
10740 "by a simple declarator-id", type);
10741 inform (DECL_SOURCE_LOCATION (tmpl), "%qD declared here", tmpl);
10744 staticp = 0;
10745 inlinep = decl_spec_seq_has_spec_p (declspecs, ds_inline);
10746 virtualp = decl_spec_seq_has_spec_p (declspecs, ds_virtual);
10747 explicitp = decl_spec_seq_has_spec_p (declspecs, ds_explicit);
10749 storage_class = declspecs->storage_class;
10750 if (storage_class == sc_static)
10751 staticp = 1 + (decl_context == FIELD);
10753 if (virtualp)
10755 if (staticp == 2)
10757 gcc_rich_location richloc (declspecs->locations[ds_virtual]);
10758 richloc.add_range (declspecs->locations[ds_storage_class], false);
10759 error_at (&richloc, "member %qD cannot be declared both %<virtual%> "
10760 "and %<static%>", dname);
10761 storage_class = sc_none;
10762 staticp = 0;
10764 if (constexpr_p)
10766 gcc_rich_location richloc (declspecs->locations[ds_virtual]);
10767 richloc.add_range (declspecs->locations[ds_constexpr], false);
10768 error_at (&richloc, "member %qD cannot be declared both %<virtual%> "
10769 "and %<constexpr%>", dname);
10772 friendp = decl_spec_seq_has_spec_p (declspecs, ds_friend);
10774 /* Issue errors about use of storage classes for parameters. */
10775 if (decl_context == PARM)
10777 if (typedef_p)
10779 error_at (declspecs->locations[ds_typedef],
10780 "typedef declaration invalid in parameter declaration");
10781 return error_mark_node;
10783 else if (template_parm_flag && storage_class != sc_none)
10785 error_at (min_location (declspecs->locations[ds_thread],
10786 declspecs->locations[ds_storage_class]),
10787 "storage class specified for template parameter %qs",
10788 name);
10789 return error_mark_node;
10791 else if (storage_class == sc_static
10792 || storage_class == sc_extern
10793 || thread_p)
10795 error_at (min_location (declspecs->locations[ds_thread],
10796 declspecs->locations[ds_storage_class]),
10797 "storage class specified for parameter %qs", name);
10798 return error_mark_node;
10801 /* Function parameters cannot be concept. */
10802 if (concept_p)
10803 error_at (declspecs->locations[ds_concept],
10804 "a parameter cannot be declared %<concept%>");
10805 /* Function parameters cannot be constexpr. If we saw one, moan
10806 and pretend it wasn't there. */
10807 else if (constexpr_p)
10809 error_at (declspecs->locations[ds_constexpr],
10810 "a parameter cannot be declared %<constexpr%>");
10811 constexpr_p = 0;
10815 /* Give error if `virtual' is used outside of class declaration. */
10816 if (virtualp
10817 && (current_class_name == NULL_TREE || decl_context != FIELD))
10819 error_at (declspecs->locations[ds_virtual],
10820 "%<virtual%> outside class declaration");
10821 virtualp = 0;
10824 if (innermost_code == cdk_decomp)
10826 location_t loc = (declarator->kind == cdk_reference
10827 ? declarator->declarator->id_loc : declarator->id_loc);
10828 if (inlinep)
10829 error_at (declspecs->locations[ds_inline],
10830 "structured binding declaration cannot be %<inline%>");
10831 if (typedef_p)
10832 error_at (declspecs->locations[ds_typedef],
10833 "structured binding declaration cannot be %<typedef%>");
10834 if (constexpr_p)
10835 error_at (declspecs->locations[ds_constexpr], "structured "
10836 "binding declaration cannot be %<constexpr%>");
10837 if (thread_p)
10838 error_at (declspecs->locations[ds_thread],
10839 "structured binding declaration cannot be %qs",
10840 declspecs->gnu_thread_keyword_p
10841 ? "__thread" : "thread_local");
10842 if (concept_p)
10843 error_at (declspecs->locations[ds_concept],
10844 "structured binding declaration cannot be %<concept%>");
10845 switch (storage_class)
10847 case sc_none:
10848 break;
10849 case sc_register:
10850 error_at (loc, "structured binding declaration cannot be "
10851 "%<register%>");
10852 break;
10853 case sc_static:
10854 error_at (loc, "structured binding declaration cannot be "
10855 "%<static%>");
10856 break;
10857 case sc_extern:
10858 error_at (loc, "structured binding declaration cannot be "
10859 "%<extern%>");
10860 break;
10861 case sc_mutable:
10862 error_at (loc, "structured binding declaration cannot be "
10863 "%<mutable%>");
10864 break;
10865 case sc_auto:
10866 error_at (loc, "structured binding declaration cannot be "
10867 "C++98 %<auto%>");
10868 break;
10869 default:
10870 gcc_unreachable ();
10872 if (TREE_CODE (type) != TEMPLATE_TYPE_PARM
10873 || TYPE_IDENTIFIER (type) != auto_identifier)
10875 if (type != error_mark_node)
10877 error_at (loc, "structured binding declaration cannot have "
10878 "type %qT", type);
10879 inform (loc,
10880 "type must be cv-qualified %<auto%> or reference to "
10881 "cv-qualified %<auto%>");
10883 type = build_qualified_type (make_auto (), type_quals);
10884 declspecs->type = type;
10886 inlinep = 0;
10887 typedef_p = 0;
10888 constexpr_p = 0;
10889 thread_p = 0;
10890 concept_p = 0;
10891 storage_class = sc_none;
10892 staticp = 0;
10893 declspecs->storage_class = sc_none;
10894 declspecs->locations[ds_thread] = UNKNOWN_LOCATION;
10897 /* Static anonymous unions are dealt with here. */
10898 if (staticp && decl_context == TYPENAME
10899 && declspecs->type
10900 && ANON_AGGR_TYPE_P (declspecs->type))
10901 decl_context = FIELD;
10903 /* Warn about storage classes that are invalid for certain
10904 kinds of declarations (parameters, typenames, etc.). */
10905 if (thread_p
10906 && ((storage_class
10907 && storage_class != sc_extern
10908 && storage_class != sc_static)
10909 || typedef_p))
10911 error ("multiple storage classes in declaration of %qs", name);
10912 thread_p = false;
10914 if (decl_context != NORMAL
10915 && ((storage_class != sc_none
10916 && storage_class != sc_mutable)
10917 || thread_p))
10919 if ((decl_context == PARM || decl_context == CATCHPARM)
10920 && (storage_class == sc_register
10921 || storage_class == sc_auto))
10923 else if (typedef_p)
10925 else if (decl_context == FIELD
10926 /* C++ allows static class elements. */
10927 && storage_class == sc_static)
10928 /* C++ also allows inlines and signed and unsigned elements,
10929 but in those cases we don't come in here. */
10931 else
10933 location_t loc
10934 = min_location (declspecs->locations[ds_thread],
10935 declspecs->locations[ds_storage_class]);
10936 if (decl_context == FIELD)
10937 error_at (loc, "storage class specified for %qs", name);
10938 else if (decl_context == PARM || decl_context == CATCHPARM)
10939 error_at (loc, "storage class specified for parameter %qs", name);
10940 else
10941 error_at (loc, "storage class specified for typename");
10942 if (storage_class == sc_register
10943 || storage_class == sc_auto
10944 || storage_class == sc_extern
10945 || thread_p)
10946 storage_class = sc_none;
10949 else if (storage_class == sc_extern && funcdef_flag
10950 && ! toplevel_bindings_p ())
10951 error ("nested function %qs declared %<extern%>", name);
10952 else if (toplevel_bindings_p ())
10954 if (storage_class == sc_auto)
10955 error ("top-level declaration of %qs specifies %<auto%>", name);
10957 else if (thread_p
10958 && storage_class != sc_extern
10959 && storage_class != sc_static)
10961 if (declspecs->gnu_thread_keyword_p)
10962 pedwarn (declspecs->locations[ds_thread],
10963 0, "function-scope %qs implicitly auto and "
10964 "declared %<__thread%>", name);
10966 /* When thread_local is applied to a variable of block scope the
10967 storage-class-specifier static is implied if it does not appear
10968 explicitly. */
10969 storage_class = declspecs->storage_class = sc_static;
10970 staticp = 1;
10973 if (storage_class && friendp)
10975 error_at (min_location (declspecs->locations[ds_thread],
10976 declspecs->locations[ds_storage_class]),
10977 "storage class specifiers invalid in friend function "
10978 "declarations");
10979 storage_class = sc_none;
10980 staticp = 0;
10983 if (!id_declarator)
10984 unqualified_id = NULL_TREE;
10985 else
10987 unqualified_id = id_declarator->u.id.unqualified_name;
10988 switch (TREE_CODE (unqualified_id))
10990 case BIT_NOT_EXPR:
10991 unqualified_id = TREE_OPERAND (unqualified_id, 0);
10992 if (TYPE_P (unqualified_id))
10993 unqualified_id = constructor_name (unqualified_id);
10994 break;
10996 case IDENTIFIER_NODE:
10997 case TEMPLATE_ID_EXPR:
10998 break;
11000 default:
11001 gcc_unreachable ();
11005 if (declspecs->std_attributes)
11007 location_t attr_loc = declspecs->locations[ds_std_attribute];
11008 if (warning_at (attr_loc, OPT_Wattributes, "attribute ignored"))
11009 inform (attr_loc, "an attribute that appertains to a type-specifier "
11010 "is ignored");
11013 /* Determine the type of the entity declared by recurring on the
11014 declarator. */
11015 for (; declarator; declarator = declarator->declarator)
11017 const cp_declarator *inner_declarator;
11018 tree attrs;
11020 if (type == error_mark_node)
11021 return error_mark_node;
11023 attrs = declarator->attributes;
11024 if (attrs)
11026 int attr_flags;
11028 attr_flags = 0;
11029 if (declarator == NULL || declarator->kind == cdk_id)
11030 attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
11031 if (declarator->kind == cdk_function)
11032 attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
11033 if (declarator->kind == cdk_array)
11034 attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
11035 returned_attrs = decl_attributes (&type,
11036 chainon (returned_attrs, attrs),
11037 attr_flags);
11040 inner_declarator = declarator->declarator;
11042 /* We don't want to warn in parameter context because we don't
11043 yet know if the parse will succeed, and this might turn out
11044 to be a constructor call. */
11045 if (decl_context != PARM
11046 && decl_context != TYPENAME
11047 && !typedef_p
11048 && declarator->parenthesized != UNKNOWN_LOCATION
11049 /* If the type is class-like and the inner name used a
11050 global namespace qualifier, we need the parens.
11051 Unfortunately all we can tell is whether a qualified name
11052 was used or not. */
11053 && !(inner_declarator
11054 && inner_declarator->kind == cdk_id
11055 && inner_declarator->u.id.qualifying_scope
11056 && (MAYBE_CLASS_TYPE_P (type)
11057 || TREE_CODE (type) == ENUMERAL_TYPE)))
11058 warning_at (declarator->parenthesized, OPT_Wparentheses,
11059 "unnecessary parentheses in declaration of %qs", name);
11060 if (declarator->kind == cdk_id || declarator->kind == cdk_decomp)
11061 break;
11063 switch (declarator->kind)
11065 case cdk_array:
11066 type = create_array_type_for_decl (dname, type,
11067 declarator->u.array.bounds);
11068 if (!valid_array_size_p (input_location, type, dname))
11069 type = error_mark_node;
11071 if (declarator->std_attributes)
11072 /* [dcl.array]/1:
11074 The optional attribute-specifier-seq appertains to the
11075 array. */
11076 returned_attrs = chainon (returned_attrs,
11077 declarator->std_attributes);
11078 break;
11080 case cdk_function:
11082 tree arg_types;
11083 int funcdecl_p;
11085 /* Declaring a function type. */
11087 input_location = declspecs->locations[ds_type_spec];
11088 abstract_virtuals_error (ACU_RETURN, type);
11089 input_location = saved_loc;
11091 /* Pick up type qualifiers which should be applied to `this'. */
11092 memfn_quals = declarator->u.function.qualifiers;
11093 /* Pick up virt-specifiers. */
11094 virt_specifiers = declarator->u.function.virt_specifiers;
11095 /* And ref-qualifier, too */
11096 rqual = declarator->u.function.ref_qualifier;
11097 /* And tx-qualifier. */
11098 tree tx_qual = declarator->u.function.tx_qualifier;
11099 /* Pick up the exception specifications. */
11100 raises = declarator->u.function.exception_specification;
11101 /* If the exception-specification is ill-formed, let's pretend
11102 there wasn't one. */
11103 if (raises == error_mark_node)
11104 raises = NULL_TREE;
11106 if (reqs)
11107 error_at (location_of (reqs), "requires-clause on return type");
11108 reqs = declarator->u.function.requires_clause;
11110 /* Say it's a definition only for the CALL_EXPR
11111 closest to the identifier. */
11112 funcdecl_p = inner_declarator && inner_declarator->kind == cdk_id;
11114 /* Handle a late-specified return type. */
11115 tree late_return_type = declarator->u.function.late_return_type;
11116 if (funcdecl_p)
11118 if (tree auto_node = type_uses_auto (type))
11120 if (!late_return_type)
11122 if (current_class_type
11123 && LAMBDA_TYPE_P (current_class_type))
11124 /* OK for C++11 lambdas. */;
11125 else if (cxx_dialect < cxx14)
11127 error ("%qs function uses "
11128 "%<auto%> type specifier without trailing "
11129 "return type", name);
11130 inform (input_location, "deduced return type "
11131 "only available with -std=c++14 or "
11132 "-std=gnu++14");
11134 else if (virtualp)
11136 error ("virtual function cannot "
11137 "have deduced return type");
11138 virtualp = false;
11141 else if (!is_auto (type) && sfk != sfk_conversion)
11143 error ("%qs function with trailing return type has"
11144 " %qT as its type rather than plain %<auto%>",
11145 name, type);
11146 return error_mark_node;
11148 tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node);
11149 if (!tmpl)
11150 if (tree late_auto = type_uses_auto (late_return_type))
11151 tmpl = CLASS_PLACEHOLDER_TEMPLATE (late_auto);
11152 if (tmpl)
11154 if (!dguide_name_p (unqualified_id))
11156 error_at (declarator->id_loc, "deduced class "
11157 "type %qD in function return type",
11158 DECL_NAME (tmpl));
11159 inform (DECL_SOURCE_LOCATION (tmpl),
11160 "%qD declared here", tmpl);
11161 return error_mark_node;
11163 else if (!late_return_type)
11165 error_at (declarator->id_loc, "deduction guide "
11166 "for %qT must have trailing return "
11167 "type", TREE_TYPE (tmpl));
11168 inform (DECL_SOURCE_LOCATION (tmpl),
11169 "%qD declared here", tmpl);
11170 return error_mark_node;
11172 else if (CLASS_TYPE_P (late_return_type)
11173 && CLASSTYPE_TEMPLATE_INFO (late_return_type)
11174 && (CLASSTYPE_TI_TEMPLATE (late_return_type)
11175 == tmpl))
11176 /* OK */;
11177 else
11178 error ("trailing return type %qT of deduction guide "
11179 "is not a specialization of %qT",
11180 late_return_type, TREE_TYPE (tmpl));
11183 else if (late_return_type
11184 && sfk != sfk_conversion)
11186 if (cxx_dialect < cxx11)
11187 /* Not using maybe_warn_cpp0x because this should
11188 always be an error. */
11189 error ("trailing return type only available with "
11190 "-std=c++11 or -std=gnu++11");
11191 else
11192 error ("%qs function with trailing return type not "
11193 "declared with %<auto%> type specifier", name);
11194 return error_mark_node;
11197 type = splice_late_return_type (type, late_return_type);
11198 if (type == error_mark_node)
11199 return error_mark_node;
11201 if (late_return_type)
11203 late_return_type_p = true;
11204 type_quals = cp_type_quals (type);
11207 if (type_quals != TYPE_UNQUALIFIED)
11209 if (SCALAR_TYPE_P (type) || VOID_TYPE_P (type))
11210 warning_at (typespec_loc, OPT_Wignored_qualifiers, "type "
11211 "qualifiers ignored on function return type");
11212 /* We now know that the TYPE_QUALS don't apply to the
11213 decl, but to its return type. */
11214 type_quals = TYPE_UNQUALIFIED;
11217 /* Error about some types functions can't return. */
11219 if (TREE_CODE (type) == FUNCTION_TYPE)
11221 error_at (typespec_loc, "%qs declared as function returning "
11222 "a function", name);
11223 return error_mark_node;
11225 if (TREE_CODE (type) == ARRAY_TYPE)
11227 error_at (typespec_loc, "%qs declared as function returning "
11228 "an array", name);
11229 return error_mark_node;
11232 if (ctype == NULL_TREE
11233 && decl_context == FIELD
11234 && funcdecl_p
11235 && friendp == 0)
11236 ctype = current_class_type;
11238 if (ctype && (sfk == sfk_constructor
11239 || sfk == sfk_destructor))
11241 /* We are within a class's scope. If our declarator name
11242 is the same as the class name, and we are defining
11243 a function, then it is a constructor/destructor, and
11244 therefore returns a void type. */
11246 /* ISO C++ 12.4/2. A destructor may not be declared
11247 const or volatile. A destructor may not be static.
11248 A destructor may not be declared with ref-qualifier.
11250 ISO C++ 12.1. A constructor may not be declared
11251 const or volatile. A constructor may not be
11252 virtual. A constructor may not be static.
11253 A constructor may not be declared with ref-qualifier. */
11254 if (staticp == 2)
11255 error ((flags == DTOR_FLAG)
11256 ? G_("destructor cannot be static member function")
11257 : G_("constructor cannot be static member function"));
11258 if (memfn_quals)
11260 error ((flags == DTOR_FLAG)
11261 ? G_("destructors may not be cv-qualified")
11262 : G_("constructors may not be cv-qualified"));
11263 memfn_quals = TYPE_UNQUALIFIED;
11266 if (rqual)
11268 maybe_warn_cpp0x (CPP0X_REF_QUALIFIER);
11269 error ((flags == DTOR_FLAG)
11270 ? G_("destructors may not be ref-qualified")
11271 : G_("constructors may not be ref-qualified"));
11272 rqual = REF_QUAL_NONE;
11275 if (decl_context == FIELD
11276 && !member_function_or_else (ctype,
11277 current_class_type,
11278 flags))
11279 return error_mark_node;
11281 if (flags != DTOR_FLAG)
11283 /* It's a constructor. */
11284 if (explicitp == 1)
11285 explicitp = 2;
11286 if (virtualp)
11288 permerror (declspecs->locations[ds_virtual],
11289 "constructors cannot be declared %<virtual%>");
11290 virtualp = 0;
11292 if (decl_context == FIELD
11293 && sfk != sfk_constructor)
11294 return error_mark_node;
11296 if (decl_context == FIELD)
11297 staticp = 0;
11299 else if (friendp)
11301 if (virtualp)
11303 /* Cannot be both friend and virtual. */
11304 gcc_rich_location richloc (declspecs->locations[ds_virtual]);
11305 richloc.add_range (declspecs->locations[ds_friend], false);
11306 error_at (&richloc, "virtual functions cannot be friends");
11307 friendp = 0;
11309 if (decl_context == NORMAL)
11310 error ("friend declaration not in class definition");
11311 if (current_function_decl && funcdef_flag)
11313 error ("can%'t define friend function %qs in a local "
11314 "class definition", name);
11315 friendp = 0;
11318 else if (ctype && sfk == sfk_conversion)
11320 if (explicitp == 1)
11322 maybe_warn_cpp0x (CPP0X_EXPLICIT_CONVERSION);
11323 explicitp = 2;
11325 if (late_return_type_p)
11326 error ("a conversion function cannot have a trailing return type");
11328 else if (sfk == sfk_deduction_guide)
11330 if (explicitp == 1)
11331 explicitp = 2;
11334 tree pushed_scope = NULL_TREE;
11335 if (funcdecl_p
11336 && decl_context != FIELD
11337 && inner_declarator->u.id.qualifying_scope
11338 && CLASS_TYPE_P (inner_declarator->u.id.qualifying_scope))
11339 pushed_scope
11340 = push_scope (inner_declarator->u.id.qualifying_scope);
11342 arg_types = grokparms (declarator->u.function.parameters, &parms);
11344 if (pushed_scope)
11345 pop_scope (pushed_scope);
11347 if (inner_declarator
11348 && inner_declarator->kind == cdk_id
11349 && inner_declarator->u.id.sfk == sfk_destructor
11350 && arg_types != void_list_node)
11352 error ("destructors may not have parameters");
11353 arg_types = void_list_node;
11354 parms = NULL_TREE;
11357 type = build_function_type (type, arg_types);
11359 tree attrs = declarator->std_attributes;
11360 if (tx_qual)
11362 tree att = build_tree_list (tx_qual, NULL_TREE);
11363 /* transaction_safe applies to the type, but
11364 transaction_safe_dynamic applies to the function. */
11365 if (is_attribute_p ("transaction_safe", tx_qual))
11366 attrs = chainon (attrs, att);
11367 else
11368 returned_attrs = chainon (returned_attrs, att);
11370 if (attrs)
11371 /* [dcl.fct]/2:
11373 The optional attribute-specifier-seq appertains to
11374 the function type. */
11375 decl_attributes (&type, attrs, 0);
11377 if (raises)
11378 type = build_exception_variant (type, raises);
11380 break;
11382 case cdk_pointer:
11383 case cdk_reference:
11384 case cdk_ptrmem:
11385 /* Filter out pointers-to-references and references-to-references.
11386 We can get these if a TYPE_DECL is used. */
11388 if (TYPE_REF_P (type))
11390 if (declarator->kind != cdk_reference)
11392 error ("cannot declare pointer to %q#T", type);
11393 type = TREE_TYPE (type);
11396 /* In C++0x, we allow reference to reference declarations
11397 that occur indirectly through typedefs [7.1.3/8 dcl.typedef]
11398 and template type arguments [14.3.1/4 temp.arg.type]. The
11399 check for direct reference to reference declarations, which
11400 are still forbidden, occurs below. Reasoning behind the change
11401 can be found in DR106, DR540, and the rvalue reference
11402 proposals. */
11403 else if (cxx_dialect == cxx98)
11405 error ("cannot declare reference to %q#T", type);
11406 type = TREE_TYPE (type);
11409 else if (VOID_TYPE_P (type))
11411 if (declarator->kind == cdk_reference)
11412 error ("cannot declare reference to %q#T", type);
11413 else if (declarator->kind == cdk_ptrmem)
11414 error ("cannot declare pointer to %q#T member", type);
11417 /* We now know that the TYPE_QUALS don't apply to the decl,
11418 but to the target of the pointer. */
11419 type_quals = TYPE_UNQUALIFIED;
11421 /* This code used to handle METHOD_TYPE, but I don't think it's
11422 possible to get it here anymore. */
11423 gcc_assert (TREE_CODE (type) != METHOD_TYPE);
11424 if (declarator->kind == cdk_ptrmem
11425 && TREE_CODE (type) == FUNCTION_TYPE)
11427 memfn_quals |= type_memfn_quals (type);
11428 type = build_memfn_type (type,
11429 declarator->u.pointer.class_type,
11430 memfn_quals,
11431 rqual);
11432 if (type == error_mark_node)
11433 return error_mark_node;
11435 rqual = REF_QUAL_NONE;
11436 memfn_quals = TYPE_UNQUALIFIED;
11439 if (TREE_CODE (type) == FUNCTION_TYPE
11440 && (type_memfn_quals (type) != TYPE_UNQUALIFIED
11441 || type_memfn_rqual (type) != REF_QUAL_NONE))
11442 error (declarator->kind == cdk_reference
11443 ? G_("cannot declare reference to qualified function type %qT")
11444 : G_("cannot declare pointer to qualified function type %qT"),
11445 type);
11447 /* When the pointed-to type involves components of variable size,
11448 care must be taken to ensure that the size evaluation code is
11449 emitted early enough to dominate all the possible later uses
11450 and late enough for the variables on which it depends to have
11451 been assigned.
11453 This is expected to happen automatically when the pointed-to
11454 type has a name/declaration of it's own, but special attention
11455 is required if the type is anonymous.
11457 We handle the NORMAL and FIELD contexts here by inserting a
11458 dummy statement that just evaluates the size at a safe point
11459 and ensures it is not deferred until e.g. within a deeper
11460 conditional context (c++/43555).
11462 We expect nothing to be needed here for PARM or TYPENAME.
11463 Evaluating the size at this point for TYPENAME would
11464 actually be incorrect, as we might be in the middle of an
11465 expression with side effects on the pointed-to type size
11466 "arguments" prior to the pointer declaration point and the
11467 size evaluation could end up prior to the side effects. */
11469 if (!TYPE_NAME (type)
11470 && (decl_context == NORMAL || decl_context == FIELD)
11471 && at_function_scope_p ()
11472 && variably_modified_type_p (type, NULL_TREE))
11474 TYPE_NAME (type) = build_decl (UNKNOWN_LOCATION, TYPE_DECL,
11475 NULL_TREE, type);
11476 add_decl_expr (TYPE_NAME (type));
11479 if (declarator->kind == cdk_reference)
11481 /* In C++0x, the type we are creating a reference to might be
11482 a typedef which is itself a reference type. In that case,
11483 we follow the reference collapsing rules in
11484 [7.1.3/8 dcl.typedef] to create the final reference type:
11486 "If a typedef TD names a type that is a reference to a type
11487 T, an attempt to create the type 'lvalue reference to cv TD'
11488 creates the type 'lvalue reference to T,' while an attempt
11489 to create the type "rvalue reference to cv TD' creates the
11490 type TD."
11492 if (VOID_TYPE_P (type))
11493 /* We already gave an error. */;
11494 else if (TYPE_REF_P (type))
11496 if (declarator->u.reference.rvalue_ref)
11497 /* Leave type alone. */;
11498 else
11499 type = cp_build_reference_type (TREE_TYPE (type), false);
11501 else
11502 type = cp_build_reference_type
11503 (type, declarator->u.reference.rvalue_ref);
11505 /* In C++0x, we need this check for direct reference to
11506 reference declarations, which are forbidden by
11507 [8.3.2/5 dcl.ref]. Reference to reference declarations
11508 are only allowed indirectly through typedefs and template
11509 type arguments. Example:
11511 void foo(int & &); // invalid ref-to-ref decl
11513 typedef int & int_ref;
11514 void foo(int_ref &); // valid ref-to-ref decl
11516 if (inner_declarator && inner_declarator->kind == cdk_reference)
11517 error ("cannot declare reference to %q#T, which is not "
11518 "a typedef or a template type argument", type);
11520 else if (TREE_CODE (type) == METHOD_TYPE)
11521 type = build_ptrmemfunc_type (build_pointer_type (type));
11522 else if (declarator->kind == cdk_ptrmem)
11524 gcc_assert (TREE_CODE (declarator->u.pointer.class_type)
11525 != NAMESPACE_DECL);
11526 if (declarator->u.pointer.class_type == error_mark_node)
11527 /* We will already have complained. */
11528 type = error_mark_node;
11529 else
11530 type = build_ptrmem_type (declarator->u.pointer.class_type,
11531 type);
11533 else
11534 type = build_pointer_type (type);
11536 /* Process a list of type modifier keywords (such as
11537 const or volatile) that were given inside the `*' or `&'. */
11539 if (declarator->u.pointer.qualifiers)
11541 type
11542 = cp_build_qualified_type (type,
11543 declarator->u.pointer.qualifiers);
11544 type_quals = cp_type_quals (type);
11547 /* Apply C++11 attributes to the pointer, and not to the
11548 type pointed to. This is unlike what is done for GNU
11549 attributes above. It is to comply with [dcl.ptr]/1:
11551 [the optional attribute-specifier-seq (7.6.1) appertains
11552 to the pointer and not to the object pointed to]. */
11553 if (declarator->std_attributes)
11554 decl_attributes (&type, declarator->std_attributes,
11557 ctype = NULL_TREE;
11558 break;
11560 case cdk_error:
11561 break;
11563 default:
11564 gcc_unreachable ();
11568 /* A `constexpr' specifier used in an object declaration declares
11569 the object as `const'. */
11570 if (constexpr_p && innermost_code != cdk_function)
11572 /* DR1688 says that a `constexpr' specifier in combination with
11573 `volatile' is valid. */
11575 if (!TYPE_REF_P (type))
11577 type_quals |= TYPE_QUAL_CONST;
11578 type = cp_build_qualified_type (type, type_quals);
11582 if (unqualified_id && TREE_CODE (unqualified_id) == TEMPLATE_ID_EXPR
11583 && TREE_CODE (type) != FUNCTION_TYPE
11584 && TREE_CODE (type) != METHOD_TYPE
11585 && !variable_template_p (TREE_OPERAND (unqualified_id, 0)))
11587 error ("template-id %qD used as a declarator",
11588 unqualified_id);
11589 unqualified_id = dname;
11592 /* If TYPE is a FUNCTION_TYPE, but the function name was explicitly
11593 qualified with a class-name, turn it into a METHOD_TYPE, unless
11594 we know that the function is static. We take advantage of this
11595 opportunity to do other processing that pertains to entities
11596 explicitly declared to be class members. Note that if DECLARATOR
11597 is non-NULL, we know it is a cdk_id declarator; otherwise, we
11598 would not have exited the loop above. */
11599 if (declarator
11600 && declarator->kind == cdk_id
11601 && declarator->u.id.qualifying_scope
11602 && MAYBE_CLASS_TYPE_P (declarator->u.id.qualifying_scope))
11604 ctype = declarator->u.id.qualifying_scope;
11605 ctype = TYPE_MAIN_VARIANT (ctype);
11606 template_count = num_template_headers_for_class (ctype);
11608 if (ctype == current_class_type)
11610 if (friendp)
11612 permerror (input_location, "member functions are implicitly "
11613 "friends of their class");
11614 friendp = 0;
11616 else
11617 permerror (declarator->id_loc,
11618 "extra qualification %<%T::%> on member %qs",
11619 ctype, name);
11621 else if (/* If the qualifying type is already complete, then we
11622 can skip the following checks. */
11623 !COMPLETE_TYPE_P (ctype)
11624 && (/* If the function is being defined, then
11625 qualifying type must certainly be complete. */
11626 funcdef_flag
11627 /* A friend declaration of "T::f" is OK, even if
11628 "T" is a template parameter. But, if this
11629 function is not a friend, the qualifying type
11630 must be a class. */
11631 || (!friendp && !CLASS_TYPE_P (ctype))
11632 /* For a declaration, the type need not be
11633 complete, if either it is dependent (since there
11634 is no meaningful definition of complete in that
11635 case) or the qualifying class is currently being
11636 defined. */
11637 || !(dependent_type_p (ctype)
11638 || currently_open_class (ctype)))
11639 /* Check that the qualifying type is complete. */
11640 && !complete_type_or_else (ctype, NULL_TREE))
11641 return error_mark_node;
11642 else if (TREE_CODE (type) == FUNCTION_TYPE)
11644 if (current_class_type
11645 && (!friendp || funcdef_flag || initialized))
11647 error (funcdef_flag || initialized
11648 ? G_("cannot define member function %<%T::%s%> "
11649 "within %qT")
11650 : G_("cannot declare member function %<%T::%s%> "
11651 "within %qT"),
11652 ctype, name, current_class_type);
11653 return error_mark_node;
11656 else if (typedef_p && current_class_type)
11658 error ("cannot declare member %<%T::%s%> within %qT",
11659 ctype, name, current_class_type);
11660 return error_mark_node;
11664 if (ctype == NULL_TREE && decl_context == FIELD && friendp == 0)
11665 ctype = current_class_type;
11667 /* Now TYPE has the actual type. */
11669 if (returned_attrs)
11671 if (attrlist)
11672 *attrlist = chainon (returned_attrs, *attrlist);
11673 else
11674 attrlist = &returned_attrs;
11677 if (declarator
11678 && declarator->kind == cdk_id
11679 && declarator->std_attributes
11680 && attrlist != NULL)
11682 /* [dcl.meaning]/1: The optional attribute-specifier-seq following
11683 a declarator-id appertains to the entity that is declared. */
11684 if (declarator->std_attributes != error_mark_node)
11685 *attrlist = chainon (*attrlist, declarator->std_attributes);
11686 else
11687 /* We should have already diagnosed the issue (c++/78344). */
11688 gcc_assert (seen_error ());
11691 /* Handle parameter packs. */
11692 if (parameter_pack_p)
11694 if (decl_context == PARM)
11695 /* Turn the type into a pack expansion.*/
11696 type = make_pack_expansion (type);
11697 else
11698 error ("non-parameter %qs cannot be a parameter pack", name);
11701 if ((decl_context == FIELD || decl_context == PARM)
11702 && !processing_template_decl
11703 && variably_modified_type_p (type, NULL_TREE))
11705 if (decl_context == FIELD)
11706 error ("data member may not have variably modified type %qT", type);
11707 else
11708 error ("parameter may not have variably modified type %qT", type);
11709 type = error_mark_node;
11712 if (explicitp == 1 || (explicitp && friendp))
11714 /* [dcl.fct.spec] (C++11) The explicit specifier shall be used only
11715 in the declaration of a constructor or conversion function within
11716 a class definition. */
11717 if (!current_class_type)
11718 error_at (declspecs->locations[ds_explicit],
11719 "%<explicit%> outside class declaration");
11720 else if (friendp)
11721 error_at (declspecs->locations[ds_explicit],
11722 "%<explicit%> in friend declaration");
11723 else
11724 error_at (declspecs->locations[ds_explicit],
11725 "only declarations of constructors and conversion operators "
11726 "can be %<explicit%>");
11727 explicitp = 0;
11730 if (storage_class == sc_mutable)
11732 if (decl_context != FIELD || friendp)
11734 error ("non-member %qs cannot be declared %<mutable%>", name);
11735 storage_class = sc_none;
11737 else if (decl_context == TYPENAME || typedef_p)
11739 error ("non-object member %qs cannot be declared %<mutable%>", name);
11740 storage_class = sc_none;
11742 else if (TREE_CODE (type) == FUNCTION_TYPE
11743 || TREE_CODE (type) == METHOD_TYPE)
11745 error ("function %qs cannot be declared %<mutable%>", name);
11746 storage_class = sc_none;
11748 else if (staticp)
11750 error ("static %qs cannot be declared %<mutable%>", name);
11751 storage_class = sc_none;
11753 else if (type_quals & TYPE_QUAL_CONST)
11755 error ("const %qs cannot be declared %<mutable%>", name);
11756 storage_class = sc_none;
11758 else if (TYPE_REF_P (type))
11760 permerror (input_location, "reference %qs cannot be declared "
11761 "%<mutable%>", name);
11762 storage_class = sc_none;
11766 /* If this is declaring a typedef name, return a TYPE_DECL. */
11767 if (typedef_p && decl_context != TYPENAME)
11769 tree decl;
11771 /* This declaration:
11773 typedef void f(int) const;
11775 declares a function type which is not a member of any
11776 particular class, but which is cv-qualified; for
11777 example "f S::*" declares a pointer to a const-qualified
11778 member function of S. We record the cv-qualification in the
11779 function type. */
11780 if ((rqual || memfn_quals) && TREE_CODE (type) == FUNCTION_TYPE)
11782 type = apply_memfn_quals (type, memfn_quals, rqual);
11784 /* We have now dealt with these qualifiers. */
11785 memfn_quals = TYPE_UNQUALIFIED;
11786 rqual = REF_QUAL_NONE;
11789 if (type_uses_auto (type))
11791 error ("typedef declared %<auto%>");
11792 type = error_mark_node;
11795 if (reqs)
11796 error_at (location_of (reqs), "requires-clause on typedef");
11798 if (id_declarator && declarator->u.id.qualifying_scope)
11800 error ("typedef name may not be a nested-name-specifier");
11801 type = error_mark_node;
11804 if (decl_context == FIELD)
11805 decl = build_lang_decl (TYPE_DECL, unqualified_id, type);
11806 else
11807 decl = build_decl (input_location, TYPE_DECL, unqualified_id, type);
11809 if (decl_context != FIELD)
11811 if (!current_function_decl)
11812 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
11813 else if (DECL_MAYBE_IN_CHARGE_CDTOR_P (current_function_decl))
11814 /* The TYPE_DECL is "abstract" because there will be
11815 clones of this constructor/destructor, and there will
11816 be copies of this TYPE_DECL generated in those
11817 clones. The decloning optimization (for space) may
11818 revert this subsequently if it determines that
11819 the clones should share a common implementation. */
11820 DECL_ABSTRACT_P (decl) = true;
11822 else if (current_class_type
11823 && constructor_name_p (unqualified_id, current_class_type))
11824 permerror (input_location, "ISO C++ forbids nested type %qD with same name "
11825 "as enclosing class",
11826 unqualified_id);
11828 /* If the user declares "typedef struct {...} foo" then the
11829 struct will have an anonymous name. Fill that name in now.
11830 Nothing can refer to it, so nothing needs know about the name
11831 change. */
11832 if (type != error_mark_node
11833 && unqualified_id
11834 && TYPE_NAME (type)
11835 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
11836 && TYPE_UNNAMED_P (type)
11837 && declspecs->type_definition_p
11838 && attributes_naming_typedef_ok (*attrlist)
11839 && cp_type_quals (type) == TYPE_UNQUALIFIED)
11840 name_unnamed_type (type, decl);
11842 if (signed_p
11843 || (typedef_decl && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl)))
11844 C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
11846 bad_specifiers (decl, BSP_TYPE, virtualp,
11847 memfn_quals != TYPE_UNQUALIFIED,
11848 inlinep, friendp, raises != NULL_TREE,
11849 declspecs->locations);
11851 if (decl_spec_seq_has_spec_p (declspecs, ds_alias))
11852 /* Acknowledge that this was written:
11853 `using analias = atype;'. */
11854 TYPE_DECL_ALIAS_P (decl) = 1;
11856 return decl;
11859 /* Detect the case of an array type of unspecified size
11860 which came, as such, direct from a typedef name.
11861 We must copy the type, so that the array's domain can be
11862 individually set by the object's initializer. */
11864 if (type && typedef_type
11865 && TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type)
11866 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (typedef_type))
11867 type = build_cplus_array_type (TREE_TYPE (type), NULL_TREE);
11869 /* Detect where we're using a typedef of function type to declare a
11870 function. PARMS will not be set, so we must create it now. */
11872 if (type == typedef_type && TREE_CODE (type) == FUNCTION_TYPE)
11874 tree decls = NULL_TREE;
11875 tree args;
11877 for (args = TYPE_ARG_TYPES (type);
11878 args && args != void_list_node;
11879 args = TREE_CHAIN (args))
11881 tree decl = cp_build_parm_decl (NULL_TREE, NULL_TREE,
11882 TREE_VALUE (args));
11884 DECL_CHAIN (decl) = decls;
11885 decls = decl;
11888 parms = nreverse (decls);
11890 if (decl_context != TYPENAME)
11892 /* The qualifiers on the function type become the qualifiers on
11893 the non-static member function. */
11894 memfn_quals |= type_memfn_quals (type);
11895 rqual = type_memfn_rqual (type);
11896 type_quals = TYPE_UNQUALIFIED;
11900 /* If this is a type name (such as, in a cast or sizeof),
11901 compute the type and return it now. */
11903 if (decl_context == TYPENAME)
11905 /* Note that here we don't care about type_quals. */
11907 /* Special case: "friend class foo" looks like a TYPENAME context. */
11908 if (friendp)
11910 if (inlinep)
11912 error ("%<inline%> specified for friend class declaration");
11913 inlinep = 0;
11916 if (!current_aggr)
11918 /* Don't allow friend declaration without a class-key. */
11919 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
11920 permerror (input_location, "template parameters cannot be friends");
11921 else if (TREE_CODE (type) == TYPENAME_TYPE)
11922 permerror (input_location, "friend declaration requires class-key, "
11923 "i.e. %<friend class %T::%D%>",
11924 TYPE_CONTEXT (type), TYPENAME_TYPE_FULLNAME (type));
11925 else
11926 permerror (input_location, "friend declaration requires class-key, "
11927 "i.e. %<friend %#T%>",
11928 type);
11931 /* Only try to do this stuff if we didn't already give up. */
11932 if (type != integer_type_node)
11934 /* A friendly class? */
11935 if (current_class_type)
11936 make_friend_class (current_class_type, TYPE_MAIN_VARIANT (type),
11937 /*complain=*/true);
11938 else
11939 error ("trying to make class %qT a friend of global scope",
11940 type);
11942 type = void_type_node;
11945 else if (memfn_quals || rqual)
11947 if (ctype == NULL_TREE
11948 && TREE_CODE (type) == METHOD_TYPE)
11949 ctype = TYPE_METHOD_BASETYPE (type);
11951 if (ctype)
11952 type = build_memfn_type (type, ctype, memfn_quals, rqual);
11953 /* Core issue #547: need to allow this in template type args.
11954 Allow it in general in C++11 for alias-declarations. */
11955 else if ((template_type_arg || cxx_dialect >= cxx11)
11956 && TREE_CODE (type) == FUNCTION_TYPE)
11957 type = apply_memfn_quals (type, memfn_quals, rqual);
11958 else
11959 error ("invalid qualifiers on non-member function type");
11962 if (reqs)
11963 error_at (location_of (reqs), "requires-clause on type-id");
11965 return type;
11967 else if (unqualified_id == NULL_TREE && decl_context != PARM
11968 && decl_context != CATCHPARM
11969 && TREE_CODE (type) != UNION_TYPE
11970 && ! bitfield
11971 && innermost_code != cdk_decomp)
11973 error ("abstract declarator %qT used as declaration", type);
11974 return error_mark_node;
11977 if (!FUNC_OR_METHOD_TYPE_P (type))
11979 /* Only functions may be declared using an operator-function-id. */
11980 if (dname && IDENTIFIER_ANY_OP_P (dname))
11982 error ("declaration of %qD as non-function", dname);
11983 return error_mark_node;
11986 if (reqs)
11987 error_at (location_of (reqs),
11988 "requires-clause on declaration of non-function type %qT",
11989 type);
11992 /* We don't check parameter types here because we can emit a better
11993 error message later. */
11994 if (decl_context != PARM)
11996 type = check_var_type (unqualified_id, type);
11997 if (type == error_mark_node)
11998 return error_mark_node;
12001 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
12002 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
12004 if (decl_context == PARM || decl_context == CATCHPARM)
12006 if (ctype || in_namespace)
12007 error ("cannot use %<::%> in parameter declaration");
12009 if (type_uses_auto (type)
12010 && !(cxx_dialect >= cxx17 && template_parm_flag))
12012 if (cxx_dialect >= cxx14)
12013 error ("%<auto%> parameter not permitted in this context");
12014 else
12015 error ("parameter declared %<auto%>");
12016 type = error_mark_node;
12019 /* A parameter declared as an array of T is really a pointer to T.
12020 One declared as a function is really a pointer to a function.
12021 One declared as a member is really a pointer to member. */
12023 if (TREE_CODE (type) == ARRAY_TYPE)
12025 /* Transfer const-ness of array into that of type pointed to. */
12026 type = build_pointer_type (TREE_TYPE (type));
12027 type_quals = TYPE_UNQUALIFIED;
12028 array_parameter_p = true;
12030 else if (TREE_CODE (type) == FUNCTION_TYPE)
12031 type = build_pointer_type (type);
12034 if (ctype && TREE_CODE (type) == FUNCTION_TYPE && staticp < 2
12035 && !(identifier_p (unqualified_id)
12036 && IDENTIFIER_NEWDEL_OP_P (unqualified_id)))
12038 cp_cv_quals real_quals = memfn_quals;
12039 if (cxx_dialect < cxx14 && constexpr_p
12040 && sfk != sfk_constructor && sfk != sfk_destructor)
12041 real_quals |= TYPE_QUAL_CONST;
12042 type = build_memfn_type (type, ctype, real_quals, rqual);
12046 tree decl = NULL_TREE;
12048 if (decl_context == PARM)
12050 decl = cp_build_parm_decl (NULL_TREE, unqualified_id, type);
12051 DECL_ARRAY_PARAMETER_P (decl) = array_parameter_p;
12053 bad_specifiers (decl, BSP_PARM, virtualp,
12054 memfn_quals != TYPE_UNQUALIFIED,
12055 inlinep, friendp, raises != NULL_TREE,
12056 declspecs->locations);
12058 else if (decl_context == FIELD)
12060 if (!staticp && !friendp && TREE_CODE (type) != METHOD_TYPE)
12061 if (tree auto_node = type_uses_auto (type))
12063 location_t loc = declspecs->locations[ds_type_spec];
12064 if (CLASS_PLACEHOLDER_TEMPLATE (auto_node))
12065 error_at (loc, "invalid use of template-name %qE without an "
12066 "argument list",
12067 CLASS_PLACEHOLDER_TEMPLATE (auto_node));
12068 else
12069 error_at (loc, "non-static data member declared with "
12070 "placeholder %qT", auto_node);
12071 type = error_mark_node;
12074 /* The C99 flexible array extension. */
12075 if (!staticp && TREE_CODE (type) == ARRAY_TYPE
12076 && TYPE_DOMAIN (type) == NULL_TREE)
12078 if (ctype
12079 && (TREE_CODE (ctype) == UNION_TYPE
12080 || TREE_CODE (ctype) == QUAL_UNION_TYPE))
12082 error ("flexible array member in union");
12083 type = error_mark_node;
12085 else
12087 /* Array is a flexible member. */
12088 if (in_system_header_at (input_location))
12089 /* Do not warn on flexible array members in system
12090 headers because glibc uses them. */;
12091 else if (name)
12092 pedwarn (input_location, OPT_Wpedantic,
12093 "ISO C++ forbids flexible array member %qs", name);
12094 else
12095 pedwarn (input_location, OPT_Wpedantic,
12096 "ISO C++ forbids flexible array members");
12098 /* Flexible array member has a null domain. */
12099 type = build_cplus_array_type (TREE_TYPE (type), NULL_TREE);
12103 if (type == error_mark_node)
12105 /* Happens when declaring arrays of sizes which
12106 are error_mark_node, for example. */
12107 decl = NULL_TREE;
12109 else if (in_namespace && !friendp)
12111 /* Something like struct S { int N::j; }; */
12112 error ("invalid use of %<::%>");
12113 return error_mark_node;
12115 else if (TREE_CODE (type) == FUNCTION_TYPE
12116 || TREE_CODE (type) == METHOD_TYPE)
12118 int publicp = 0;
12119 tree function_context;
12121 if (friendp == 0)
12123 /* This should never happen in pure C++ (the check
12124 could be an assert). It could happen in
12125 Objective-C++ if someone writes invalid code that
12126 uses a function declaration for an instance
12127 variable or property (instance variables and
12128 properties are parsed as FIELD_DECLs, but they are
12129 part of an Objective-C class, not a C++ class).
12130 That code is invalid and is caught by this
12131 check. */
12132 if (!ctype)
12134 error ("declaration of function %qD in invalid context",
12135 unqualified_id);
12136 return error_mark_node;
12139 /* ``A union may [ ... ] not [ have ] virtual functions.''
12140 ARM 9.5 */
12141 if (virtualp && TREE_CODE (ctype) == UNION_TYPE)
12143 error_at (declspecs->locations[ds_virtual],
12144 "function %qD declared %<virtual%> inside a union",
12145 unqualified_id);
12146 return error_mark_node;
12149 if (virtualp
12150 && identifier_p (unqualified_id)
12151 && IDENTIFIER_NEWDEL_OP_P (unqualified_id))
12153 error_at (declspecs->locations[ds_virtual],
12154 "%qD cannot be declared %<virtual%>, since it "
12155 "is always static", unqualified_id);
12156 virtualp = 0;
12160 /* Check that the name used for a destructor makes sense. */
12161 if (sfk == sfk_destructor)
12163 tree uqname = id_declarator->u.id.unqualified_name;
12165 if (!ctype)
12167 gcc_assert (friendp);
12168 error ("expected qualified name in friend declaration "
12169 "for destructor %qD", uqname);
12170 return error_mark_node;
12173 if (!check_dtor_name (ctype, TREE_OPERAND (uqname, 0)))
12175 error ("declaration of %qD as member of %qT",
12176 uqname, ctype);
12177 return error_mark_node;
12179 if (concept_p)
12181 error_at (declspecs->locations[ds_concept],
12182 "a destructor cannot be %<concept%>");
12183 return error_mark_node;
12185 if (constexpr_p)
12187 error_at (declspecs->locations[ds_constexpr],
12188 "a destructor cannot be %<constexpr%>");
12189 return error_mark_node;
12192 else if (sfk == sfk_constructor && friendp && !ctype)
12194 error ("expected qualified name in friend declaration "
12195 "for constructor %qD",
12196 id_declarator->u.id.unqualified_name);
12197 return error_mark_node;
12199 if (sfk == sfk_constructor)
12200 if (concept_p)
12202 error_at (declspecs->locations[ds_concept],
12203 "a constructor cannot be %<concept%>");
12204 return error_mark_node;
12206 if (concept_p)
12208 error_at (declspecs->locations[ds_concept],
12209 "a concept cannot be a member function");
12210 concept_p = false;
12213 if (TREE_CODE (unqualified_id) == TEMPLATE_ID_EXPR)
12215 tree tmpl = TREE_OPERAND (unqualified_id, 0);
12216 if (variable_template_p (tmpl))
12218 error ("specialization of variable template %qD "
12219 "declared as function", tmpl);
12220 inform (DECL_SOURCE_LOCATION (tmpl),
12221 "variable template declared here");
12222 return error_mark_node;
12226 /* Tell grokfndecl if it needs to set TREE_PUBLIC on the node. */
12227 function_context = (ctype != NULL_TREE) ?
12228 decl_function_context (TYPE_MAIN_DECL (ctype)) : NULL_TREE;
12229 publicp = (! friendp || ! staticp)
12230 && function_context == NULL_TREE;
12232 decl = grokfndecl (ctype, type,
12233 TREE_CODE (unqualified_id) != TEMPLATE_ID_EXPR
12234 ? unqualified_id : dname,
12235 parms,
12236 unqualified_id,
12237 declspecs,
12238 reqs,
12239 virtualp, flags, memfn_quals, rqual, raises,
12240 friendp ? -1 : 0, friendp, publicp,
12241 inlinep | (2 * constexpr_p) | (4 * concept_p),
12242 initialized == SD_DELETED, sfk,
12243 funcdef_flag, late_return_type_p,
12244 template_count, in_namespace,
12245 attrlist, declarator->id_loc);
12246 decl = set_virt_specifiers (decl, virt_specifiers);
12247 if (decl == NULL_TREE)
12248 return error_mark_node;
12249 #if 0
12250 /* This clobbers the attrs stored in `decl' from `attrlist'. */
12251 /* The decl and setting of decl_attr is also turned off. */
12252 decl = build_decl_attribute_variant (decl, decl_attr);
12253 #endif
12255 /* [class.conv.ctor]
12257 A constructor declared without the function-specifier
12258 explicit that can be called with a single parameter
12259 specifies a conversion from the type of its first
12260 parameter to the type of its class. Such a constructor
12261 is called a converting constructor. */
12262 if (explicitp == 2)
12263 DECL_NONCONVERTING_P (decl) = 1;
12265 else if (!staticp && !dependent_type_p (type)
12266 && !COMPLETE_TYPE_P (complete_type (type))
12267 && (!complete_or_array_type_p (type)
12268 || initialized == 0))
12270 if (TREE_CODE (type) != ARRAY_TYPE
12271 || !COMPLETE_TYPE_P (TREE_TYPE (type)))
12273 if (unqualified_id)
12275 error ("field %qD has incomplete type %qT",
12276 unqualified_id, type);
12277 cxx_incomplete_type_inform (strip_array_types (type));
12279 else
12280 error ("name %qT has incomplete type", type);
12282 type = error_mark_node;
12283 decl = NULL_TREE;
12286 else
12288 if (friendp)
12290 error ("%qE is neither function nor member function; "
12291 "cannot be declared friend", unqualified_id);
12292 return error_mark_node;
12294 decl = NULL_TREE;
12297 if (friendp)
12299 /* Friends are treated specially. */
12300 if (ctype == current_class_type)
12301 ; /* We already issued a permerror. */
12302 else if (decl && DECL_NAME (decl))
12304 if (template_class_depth (current_class_type) == 0)
12306 decl = check_explicit_specialization
12307 (unqualified_id, decl, template_count,
12308 2 * funcdef_flag + 4);
12309 if (decl == error_mark_node)
12310 return error_mark_node;
12313 decl = do_friend (ctype, unqualified_id, decl,
12314 *attrlist, flags,
12315 funcdef_flag);
12316 return decl;
12318 else
12319 return error_mark_node;
12322 /* Structure field. It may not be a function, except for C++. */
12324 if (decl == NULL_TREE)
12326 if (staticp)
12328 /* C++ allows static class members. All other work
12329 for this is done by grokfield. */
12330 decl = build_lang_decl_loc (declarator
12331 ? declarator->id_loc
12332 : input_location,
12333 VAR_DECL, unqualified_id, type);
12334 set_linkage_for_static_data_member (decl);
12335 if (concept_p)
12336 error_at (declspecs->locations[ds_concept],
12337 "static data member %qE declared %<concept%>",
12338 unqualified_id);
12339 else if (constexpr_p && !initialized)
12341 error ("%<constexpr%> static data member %qD must have an "
12342 "initializer", decl);
12343 constexpr_p = false;
12346 if (inlinep)
12347 mark_inline_variable (decl, declspecs->locations[ds_inline]);
12349 if (!DECL_VAR_DECLARED_INLINE_P (decl)
12350 && !(cxx_dialect >= cxx17 && constexpr_p))
12351 /* Even if there is an in-class initialization, DECL
12352 is considered undefined until an out-of-class
12353 definition is provided, unless this is an inline
12354 variable. */
12355 DECL_EXTERNAL (decl) = 1;
12357 if (thread_p)
12359 CP_DECL_THREAD_LOCAL_P (decl) = true;
12360 if (!processing_template_decl)
12361 set_decl_tls_model (decl, decl_default_tls_model (decl));
12362 if (declspecs->gnu_thread_keyword_p)
12363 SET_DECL_GNU_TLS_P (decl);
12366 else
12368 if (concept_p)
12369 error_at (declspecs->locations[ds_concept],
12370 "non-static data member %qE declared %<concept%>",
12371 unqualified_id);
12372 else if (constexpr_p)
12374 error_at (declspecs->locations[ds_constexpr],
12375 "non-static data member %qE declared %<constexpr%>",
12376 unqualified_id);
12377 constexpr_p = false;
12379 decl = build_decl (input_location,
12380 FIELD_DECL, unqualified_id, type);
12381 DECL_NONADDRESSABLE_P (decl) = bitfield;
12382 if (bitfield && !unqualified_id)
12384 TREE_NO_WARNING (decl) = 1;
12385 DECL_PADDING_P (decl) = 1;
12388 if (storage_class == sc_mutable)
12390 DECL_MUTABLE_P (decl) = 1;
12391 storage_class = sc_none;
12394 if (initialized)
12396 /* An attempt is being made to initialize a non-static
12397 member. This is new in C++11. */
12398 maybe_warn_cpp0x (CPP0X_NSDMI);
12400 /* If this has been parsed with static storage class, but
12401 errors forced staticp to be cleared, ensure NSDMI is
12402 not present. */
12403 if (declspecs->storage_class == sc_static)
12404 DECL_INITIAL (decl) = error_mark_node;
12408 bad_specifiers (decl, BSP_FIELD, virtualp,
12409 memfn_quals != TYPE_UNQUALIFIED,
12410 staticp ? false : inlinep, friendp,
12411 raises != NULL_TREE,
12412 declspecs->locations);
12415 else if (TREE_CODE (type) == FUNCTION_TYPE
12416 || TREE_CODE (type) == METHOD_TYPE)
12418 tree original_name;
12419 int publicp = 0;
12421 if (!unqualified_id)
12422 return error_mark_node;
12424 if (TREE_CODE (unqualified_id) == TEMPLATE_ID_EXPR)
12425 original_name = dname;
12426 else
12427 original_name = unqualified_id;
12428 // FIXME:gcc_assert (original_name == dname);
12430 if (storage_class == sc_auto)
12431 error ("storage class %<auto%> invalid for function %qs", name);
12432 else if (storage_class == sc_register)
12433 error ("storage class %<register%> invalid for function %qs", name);
12434 else if (thread_p)
12436 if (declspecs->gnu_thread_keyword_p)
12437 error_at (declspecs->locations[ds_thread],
12438 "storage class %<__thread%> invalid for function %qs",
12439 name);
12440 else
12441 error_at (declspecs->locations[ds_thread],
12442 "storage class %<thread_local%> invalid for "
12443 "function %qs", name);
12446 if (virt_specifiers)
12447 error ("virt-specifiers in %qs not allowed outside a class "
12448 "definition", name);
12449 /* Function declaration not at top level.
12450 Storage classes other than `extern' are not allowed
12451 and `extern' makes no difference. */
12452 if (! toplevel_bindings_p ()
12453 && (storage_class == sc_static
12454 || decl_spec_seq_has_spec_p (declspecs, ds_inline))
12455 && pedantic)
12457 if (storage_class == sc_static)
12458 pedwarn (declspecs->locations[ds_storage_class], OPT_Wpedantic,
12459 "%<static%> specifier invalid for function %qs "
12460 "declared out of global scope", name);
12461 else
12462 pedwarn (declspecs->locations[ds_inline], OPT_Wpedantic,
12463 "%<inline%> specifier invalid for function %qs "
12464 "declared out of global scope", name);
12467 if (ctype == NULL_TREE)
12469 if (virtualp)
12471 error ("virtual non-class function %qs", name);
12472 virtualp = 0;
12474 else if (sfk == sfk_constructor
12475 || sfk == sfk_destructor)
12477 error (funcdef_flag
12478 ? G_("%qs defined in a non-class scope")
12479 : G_("%qs declared in a non-class scope"), name);
12480 sfk = sfk_none;
12484 /* Record whether the function is public. */
12485 publicp = (ctype != NULL_TREE
12486 || storage_class != sc_static);
12488 decl = grokfndecl (ctype, type, original_name, parms, unqualified_id,
12489 declspecs,
12490 reqs, virtualp, flags, memfn_quals, rqual, raises,
12491 1, friendp,
12492 publicp,
12493 inlinep | (2 * constexpr_p) | (4 * concept_p),
12494 initialized == SD_DELETED,
12495 sfk,
12496 funcdef_flag,
12497 late_return_type_p,
12498 template_count, in_namespace, attrlist,
12499 declarator->id_loc);
12500 if (decl == NULL_TREE)
12501 return error_mark_node;
12503 if (explicitp == 2)
12504 DECL_NONCONVERTING_P (decl) = 1;
12505 if (staticp == 1)
12507 int invalid_static = 0;
12509 /* Don't allow a static member function in a class, and forbid
12510 declaring main to be static. */
12511 if (TREE_CODE (type) == METHOD_TYPE)
12513 permerror (input_location, "cannot declare member function %qD to have "
12514 "static linkage", decl);
12515 invalid_static = 1;
12517 else if (current_function_decl)
12519 /* 7.1.1: There can be no static function declarations within a
12520 block. */
12521 error_at (declspecs->locations[ds_storage_class],
12522 "cannot declare static function inside another function");
12523 invalid_static = 1;
12526 if (invalid_static)
12528 staticp = 0;
12529 storage_class = sc_none;
12533 else
12535 /* It's a variable. */
12537 /* An uninitialized decl with `extern' is a reference. */
12538 decl = grokvardecl (type, dname, unqualified_id,
12539 declspecs,
12540 initialized,
12541 type_quals,
12542 inlinep,
12543 concept_p,
12544 template_count,
12545 ctype ? ctype : in_namespace);
12546 if (decl == NULL_TREE)
12547 return error_mark_node;
12549 bad_specifiers (decl, BSP_VAR, virtualp,
12550 memfn_quals != TYPE_UNQUALIFIED,
12551 inlinep, friendp, raises != NULL_TREE,
12552 declspecs->locations);
12554 if (ctype)
12556 DECL_CONTEXT (decl) = ctype;
12557 if (staticp == 1)
12559 permerror (input_location, "%<static%> may not be used when defining "
12560 "(as opposed to declaring) a static data member");
12561 staticp = 0;
12562 storage_class = sc_none;
12564 if (storage_class == sc_register && TREE_STATIC (decl))
12566 error ("static member %qD declared %<register%>", decl);
12567 storage_class = sc_none;
12569 if (storage_class == sc_extern && pedantic)
12571 pedwarn (input_location, OPT_Wpedantic,
12572 "cannot explicitly declare member %q#D to have "
12573 "extern linkage", decl);
12574 storage_class = sc_none;
12577 else if (constexpr_p && DECL_EXTERNAL (decl))
12579 error ("declaration of %<constexpr%> variable %qD "
12580 "is not a definition", decl);
12581 constexpr_p = false;
12584 if (inlinep)
12585 mark_inline_variable (decl, declspecs->locations[ds_inline]);
12586 if (innermost_code == cdk_decomp)
12588 gcc_assert (declarator && declarator->kind == cdk_decomp);
12589 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
12590 DECL_ARTIFICIAL (decl) = 1;
12591 fit_decomposition_lang_decl (decl, NULL_TREE);
12595 if (VAR_P (decl) && !initialized)
12596 if (tree auto_node = type_uses_auto (type))
12597 if (!CLASS_PLACEHOLDER_TEMPLATE (auto_node))
12599 location_t loc = declspecs->locations[ds_type_spec];
12600 error_at (loc, "declaration of %q#D has no initializer", decl);
12601 TREE_TYPE (decl) = error_mark_node;
12604 if (storage_class == sc_extern && initialized && !funcdef_flag)
12606 if (toplevel_bindings_p ())
12608 /* It's common practice (and completely valid) to have a const
12609 be initialized and declared extern. */
12610 if (!(type_quals & TYPE_QUAL_CONST))
12611 warning (0, "%qs initialized and declared %<extern%>", name);
12613 else
12615 error ("%qs has both %<extern%> and initializer", name);
12616 return error_mark_node;
12620 /* Record `register' declaration for warnings on &
12621 and in case doing stupid register allocation. */
12623 if (storage_class == sc_register)
12625 DECL_REGISTER (decl) = 1;
12626 /* Warn about register storage specifiers on PARM_DECLs. */
12627 if (TREE_CODE (decl) == PARM_DECL)
12629 if (cxx_dialect >= cxx17)
12630 pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wregister,
12631 "ISO C++17 does not allow %<register%> storage "
12632 "class specifier");
12633 else
12634 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wregister,
12635 "%<register%> storage class specifier used");
12638 else if (storage_class == sc_extern)
12639 DECL_THIS_EXTERN (decl) = 1;
12640 else if (storage_class == sc_static)
12641 DECL_THIS_STATIC (decl) = 1;
12643 /* Set constexpr flag on vars (functions got it in grokfndecl). */
12644 if (constexpr_p && VAR_P (decl))
12645 DECL_DECLARED_CONSTEXPR_P (decl) = true;
12647 /* Record constancy and volatility on the DECL itself . There's
12648 no need to do this when processing a template; we'll do this
12649 for the instantiated declaration based on the type of DECL. */
12650 if (!processing_template_decl)
12651 cp_apply_type_quals_to_decl (type_quals, decl);
12653 return decl;
12657 /* Subroutine of start_function. Ensure that each of the parameter
12658 types (as listed in PARMS) is complete, as is required for a
12659 function definition. */
12661 static void
12662 require_complete_types_for_parms (tree parms)
12664 for (; parms; parms = DECL_CHAIN (parms))
12666 if (dependent_type_p (TREE_TYPE (parms)))
12667 continue;
12668 if (!VOID_TYPE_P (TREE_TYPE (parms))
12669 && complete_type_or_else (TREE_TYPE (parms), parms))
12671 relayout_decl (parms);
12672 DECL_ARG_TYPE (parms) = type_passed_as (TREE_TYPE (parms));
12674 maybe_warn_parm_abi (TREE_TYPE (parms),
12675 DECL_SOURCE_LOCATION (parms));
12677 else
12678 /* grokparms or complete_type_or_else will have already issued
12679 an error. */
12680 TREE_TYPE (parms) = error_mark_node;
12684 /* Returns nonzero if T is a local variable. */
12687 local_variable_p (const_tree t)
12689 if ((VAR_P (t)
12690 /* A VAR_DECL with a context that is a _TYPE is a static data
12691 member. */
12692 && !TYPE_P (CP_DECL_CONTEXT (t))
12693 /* Any other non-local variable must be at namespace scope. */
12694 && !DECL_NAMESPACE_SCOPE_P (t))
12695 || (TREE_CODE (t) == PARM_DECL))
12696 return 1;
12698 return 0;
12701 /* Like local_variable_p, but suitable for use as a tree-walking
12702 function. */
12704 static tree
12705 local_variable_p_walkfn (tree *tp, int *walk_subtrees,
12706 void * /*data*/)
12708 if (local_variable_p (*tp)
12709 && (!DECL_ARTIFICIAL (*tp) || DECL_NAME (*tp) == this_identifier))
12710 return *tp;
12711 else if (TYPE_P (*tp))
12712 *walk_subtrees = 0;
12714 return NULL_TREE;
12717 /* Check that ARG, which is a default-argument expression for a
12718 parameter DECL, is valid. Returns ARG, or ERROR_MARK_NODE, if
12719 something goes wrong. DECL may also be a _TYPE node, rather than a
12720 DECL, if there is no DECL available. */
12722 tree
12723 check_default_argument (tree decl, tree arg, tsubst_flags_t complain)
12725 tree var;
12726 tree decl_type;
12728 if (TREE_CODE (arg) == DEFAULT_ARG)
12729 /* We get a DEFAULT_ARG when looking at an in-class declaration
12730 with a default argument. Ignore the argument for now; we'll
12731 deal with it after the class is complete. */
12732 return arg;
12734 if (TYPE_P (decl))
12736 decl_type = decl;
12737 decl = NULL_TREE;
12739 else
12740 decl_type = TREE_TYPE (decl);
12742 if (arg == error_mark_node
12743 || decl == error_mark_node
12744 || TREE_TYPE (arg) == error_mark_node
12745 || decl_type == error_mark_node)
12746 /* Something already went wrong. There's no need to check
12747 further. */
12748 return error_mark_node;
12750 /* [dcl.fct.default]
12752 A default argument expression is implicitly converted to the
12753 parameter type. */
12754 ++cp_unevaluated_operand;
12755 /* Avoid digest_init clobbering the initializer. */
12756 tree carg = BRACE_ENCLOSED_INITIALIZER_P (arg) ? unshare_expr (arg): arg;
12757 perform_implicit_conversion_flags (decl_type, carg, complain,
12758 LOOKUP_IMPLICIT);
12759 --cp_unevaluated_operand;
12761 /* Avoid redundant -Wzero-as-null-pointer-constant warnings at
12762 the call sites. */
12763 if (TYPE_PTR_OR_PTRMEM_P (decl_type)
12764 && null_ptr_cst_p (arg))
12765 return nullptr_node;
12767 /* [dcl.fct.default]
12769 Local variables shall not be used in default argument
12770 expressions.
12772 The keyword `this' shall not be used in a default argument of a
12773 member function. */
12774 var = cp_walk_tree_without_duplicates (&arg, local_variable_p_walkfn, NULL);
12775 if (var)
12777 if (complain & tf_warning_or_error)
12779 if (DECL_NAME (var) == this_identifier)
12780 permerror (input_location, "default argument %qE uses %qD",
12781 arg, var);
12782 else
12783 error ("default argument %qE uses local variable %qD", arg, var);
12785 return error_mark_node;
12788 /* All is well. */
12789 return arg;
12792 /* Returns a deprecated type used within TYPE, or NULL_TREE if none. */
12794 static tree
12795 type_is_deprecated (tree type)
12797 enum tree_code code;
12798 if (TREE_DEPRECATED (type))
12799 return type;
12800 if (TYPE_NAME (type))
12802 if (TREE_DEPRECATED (TYPE_NAME (type)))
12803 return type;
12804 else
12805 return NULL_TREE;
12808 /* Do warn about using typedefs to a deprecated class. */
12809 if (OVERLOAD_TYPE_P (type) && type != TYPE_MAIN_VARIANT (type))
12810 return type_is_deprecated (TYPE_MAIN_VARIANT (type));
12812 code = TREE_CODE (type);
12814 if (code == POINTER_TYPE || code == REFERENCE_TYPE
12815 || code == OFFSET_TYPE || code == FUNCTION_TYPE
12816 || code == METHOD_TYPE || code == ARRAY_TYPE)
12817 return type_is_deprecated (TREE_TYPE (type));
12819 if (TYPE_PTRMEMFUNC_P (type))
12820 return type_is_deprecated
12821 (TREE_TYPE (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (type))));
12823 return NULL_TREE;
12826 /* Decode the list of parameter types for a function type.
12827 Given the list of things declared inside the parens,
12828 return a list of types.
12830 If this parameter does not end with an ellipsis, we append
12831 void_list_node.
12833 *PARMS is set to the chain of PARM_DECLs created. */
12835 tree
12836 grokparms (tree parmlist, tree *parms)
12838 tree result = NULL_TREE;
12839 tree decls = NULL_TREE;
12840 tree parm;
12841 int any_error = 0;
12843 for (parm = parmlist; parm != NULL_TREE; parm = TREE_CHAIN (parm))
12845 tree type = NULL_TREE;
12846 tree init = TREE_PURPOSE (parm);
12847 tree decl = TREE_VALUE (parm);
12849 if (parm == void_list_node)
12850 break;
12852 if (! decl || TREE_TYPE (decl) == error_mark_node)
12853 continue;
12855 type = TREE_TYPE (decl);
12856 if (VOID_TYPE_P (type))
12858 if (same_type_p (type, void_type_node)
12859 && !init
12860 && !DECL_NAME (decl) && !result
12861 && TREE_CHAIN (parm) == void_list_node)
12862 /* DR 577: A parameter list consisting of a single
12863 unnamed parameter of non-dependent type 'void'. */
12864 break;
12865 else if (cv_qualified_p (type))
12866 error_at (DECL_SOURCE_LOCATION (decl),
12867 "invalid use of cv-qualified type %qT in "
12868 "parameter declaration", type);
12869 else
12870 error_at (DECL_SOURCE_LOCATION (decl),
12871 "invalid use of type %<void%> in parameter "
12872 "declaration");
12873 /* It's not a good idea to actually create parameters of
12874 type `void'; other parts of the compiler assume that a
12875 void type terminates the parameter list. */
12876 type = error_mark_node;
12877 TREE_TYPE (decl) = error_mark_node;
12880 if (type != error_mark_node)
12882 if (deprecated_state != DEPRECATED_SUPPRESS)
12884 tree deptype = type_is_deprecated (type);
12885 if (deptype)
12886 cp_warn_deprecated_use (deptype);
12889 /* Top-level qualifiers on the parameters are
12890 ignored for function types. */
12891 type = cp_build_qualified_type (type, 0);
12892 if (TREE_CODE (type) == METHOD_TYPE)
12894 error ("parameter %qD invalidly declared method type", decl);
12895 type = build_pointer_type (type);
12896 TREE_TYPE (decl) = type;
12898 else if (abstract_virtuals_error (decl, type))
12899 any_error = 1; /* Seems like a good idea. */
12900 else if (cxx_dialect < cxx17 && INDIRECT_TYPE_P (type))
12902 /* Before C++17 DR 393:
12903 [dcl.fct]/6, parameter types cannot contain pointers
12904 (references) to arrays of unknown bound. */
12905 tree t = TREE_TYPE (type);
12906 int ptr = TYPE_PTR_P (type);
12908 while (1)
12910 if (TYPE_PTR_P (t))
12911 ptr = 1;
12912 else if (TREE_CODE (t) != ARRAY_TYPE)
12913 break;
12914 else if (!TYPE_DOMAIN (t))
12915 break;
12916 t = TREE_TYPE (t);
12918 if (TREE_CODE (t) == ARRAY_TYPE)
12919 pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wpedantic,
12921 ? G_("parameter %qD includes pointer to array of "
12922 "unknown bound %qT")
12923 : G_("parameter %qD includes reference to array of "
12924 "unknown bound %qT"),
12925 decl, t);
12928 if (any_error)
12929 init = NULL_TREE;
12930 else if (init && !processing_template_decl)
12931 init = check_default_argument (decl, init, tf_warning_or_error);
12934 DECL_CHAIN (decl) = decls;
12935 decls = decl;
12936 result = tree_cons (init, type, result);
12938 decls = nreverse (decls);
12939 result = nreverse (result);
12940 if (parm)
12941 result = chainon (result, void_list_node);
12942 *parms = decls;
12944 return result;
12948 /* D is a constructor or overloaded `operator='.
12950 Let T be the class in which D is declared. Then, this function
12951 returns:
12953 -1 if D's is an ill-formed constructor or copy assignment operator
12954 whose first parameter is of type `T'.
12955 0 if D is not a copy constructor or copy assignment
12956 operator.
12957 1 if D is a copy constructor or copy assignment operator whose
12958 first parameter is a reference to non-const qualified T.
12959 2 if D is a copy constructor or copy assignment operator whose
12960 first parameter is a reference to const qualified T.
12962 This function can be used as a predicate. Positive values indicate
12963 a copy constructor and nonzero values indicate a copy assignment
12964 operator. */
12967 copy_fn_p (const_tree d)
12969 tree args;
12970 tree arg_type;
12971 int result = 1;
12973 gcc_assert (DECL_FUNCTION_MEMBER_P (d));
12975 if (TREE_CODE (d) == TEMPLATE_DECL
12976 || (DECL_TEMPLATE_INFO (d)
12977 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (d))))
12978 /* Instantiations of template member functions are never copy
12979 functions. Note that member functions of templated classes are
12980 represented as template functions internally, and we must
12981 accept those as copy functions. */
12982 return 0;
12984 args = FUNCTION_FIRST_USER_PARMTYPE (d);
12985 if (!args)
12986 return 0;
12988 arg_type = TREE_VALUE (args);
12989 if (arg_type == error_mark_node)
12990 return 0;
12992 if (TYPE_MAIN_VARIANT (arg_type) == DECL_CONTEXT (d))
12994 /* Pass by value copy assignment operator. */
12995 result = -1;
12997 else if (TYPE_REF_P (arg_type)
12998 && !TYPE_REF_IS_RVALUE (arg_type)
12999 && TYPE_MAIN_VARIANT (TREE_TYPE (arg_type)) == DECL_CONTEXT (d))
13001 if (CP_TYPE_CONST_P (TREE_TYPE (arg_type)))
13002 result = 2;
13004 else
13005 return 0;
13007 args = TREE_CHAIN (args);
13009 if (args && args != void_list_node && !TREE_PURPOSE (args))
13010 /* There are more non-optional args. */
13011 return 0;
13013 return result;
13016 /* D is a constructor or overloaded `operator='.
13018 Let T be the class in which D is declared. Then, this function
13019 returns true when D is a move constructor or move assignment
13020 operator, false otherwise. */
13022 bool
13023 move_fn_p (const_tree d)
13025 gcc_assert (DECL_FUNCTION_MEMBER_P (d));
13027 if (cxx_dialect == cxx98)
13028 /* There are no move constructors if we are in C++98 mode. */
13029 return false;
13031 if (TREE_CODE (d) == TEMPLATE_DECL
13032 || (DECL_TEMPLATE_INFO (d)
13033 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (d))))
13034 /* Instantiations of template member functions are never move
13035 functions. Note that member functions of templated classes are
13036 represented as template functions internally, and we must
13037 accept those as move functions. */
13038 return 0;
13040 return move_signature_fn_p (d);
13043 /* D is a constructor or overloaded `operator='.
13045 Then, this function returns true when D has the same signature as a move
13046 constructor or move assignment operator (because either it is such a
13047 ctor/op= or it is a template specialization with the same signature),
13048 false otherwise. */
13050 bool
13051 move_signature_fn_p (const_tree d)
13053 tree args;
13054 tree arg_type;
13055 bool result = false;
13057 args = FUNCTION_FIRST_USER_PARMTYPE (d);
13058 if (!args)
13059 return 0;
13061 arg_type = TREE_VALUE (args);
13062 if (arg_type == error_mark_node)
13063 return 0;
13065 if (TYPE_REF_P (arg_type)
13066 && TYPE_REF_IS_RVALUE (arg_type)
13067 && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (arg_type)),
13068 DECL_CONTEXT (d)))
13069 result = true;
13071 args = TREE_CHAIN (args);
13073 if (args && args != void_list_node && !TREE_PURPOSE (args))
13074 /* There are more non-optional args. */
13075 return false;
13077 return result;
13080 /* Remember any special properties of member function DECL. */
13082 void
13083 grok_special_member_properties (tree decl)
13085 tree class_type;
13087 if (!DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
13088 return;
13090 class_type = DECL_CONTEXT (decl);
13091 if (IDENTIFIER_CTOR_P (DECL_NAME (decl)))
13093 int ctor = copy_fn_p (decl);
13095 if (!DECL_ARTIFICIAL (decl))
13096 TYPE_HAS_USER_CONSTRUCTOR (class_type) = 1;
13098 if (ctor > 0)
13100 /* [class.copy]
13102 A non-template constructor for class X is a copy
13103 constructor if its first parameter is of type X&, const
13104 X&, volatile X& or const volatile X&, and either there
13105 are no other parameters or else all other parameters have
13106 default arguments. */
13107 TYPE_HAS_COPY_CTOR (class_type) = 1;
13108 if (user_provided_p (decl))
13109 TYPE_HAS_COMPLEX_COPY_CTOR (class_type) = 1;
13110 if (ctor > 1)
13111 TYPE_HAS_CONST_COPY_CTOR (class_type) = 1;
13113 else if (sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (decl)))
13114 TYPE_HAS_DEFAULT_CONSTRUCTOR (class_type) = 1;
13115 else if (move_fn_p (decl) && user_provided_p (decl))
13116 TYPE_HAS_COMPLEX_MOVE_CTOR (class_type) = 1;
13117 else if (is_list_ctor (decl))
13118 TYPE_HAS_LIST_CTOR (class_type) = 1;
13120 if (DECL_DECLARED_CONSTEXPR_P (decl)
13121 && !ctor && !move_fn_p (decl))
13122 TYPE_HAS_CONSTEXPR_CTOR (class_type) = 1;
13124 else if (DECL_NAME (decl) == assign_op_identifier)
13126 /* [class.copy]
13128 A non-template assignment operator for class X is a copy
13129 assignment operator if its parameter is of type X, X&, const
13130 X&, volatile X& or const volatile X&. */
13132 int assop = copy_fn_p (decl);
13134 if (assop)
13136 TYPE_HAS_COPY_ASSIGN (class_type) = 1;
13137 if (user_provided_p (decl))
13138 TYPE_HAS_COMPLEX_COPY_ASSIGN (class_type) = 1;
13139 if (assop != 1)
13140 TYPE_HAS_CONST_COPY_ASSIGN (class_type) = 1;
13142 else if (move_fn_p (decl) && user_provided_p (decl))
13143 TYPE_HAS_COMPLEX_MOVE_ASSIGN (class_type) = 1;
13145 else if (IDENTIFIER_CONV_OP_P (DECL_NAME (decl)))
13146 TYPE_HAS_CONVERSION (class_type) = true;
13148 /* Destructors are handled in check_methods. */
13151 /* Check a constructor DECL has the correct form. Complains
13152 if the class has a constructor of the form X(X). */
13154 bool
13155 grok_ctor_properties (const_tree ctype, const_tree decl)
13157 int ctor_parm = copy_fn_p (decl);
13159 if (ctor_parm < 0)
13161 /* [class.copy]
13163 A declaration of a constructor for a class X is ill-formed if
13164 its first parameter is of type (optionally cv-qualified) X
13165 and either there are no other parameters or else all other
13166 parameters have default arguments.
13168 We *don't* complain about member template instantiations that
13169 have this form, though; they can occur as we try to decide
13170 what constructor to use during overload resolution. Since
13171 overload resolution will never prefer such a constructor to
13172 the non-template copy constructor (which is either explicitly
13173 or implicitly defined), there's no need to worry about their
13174 existence. Theoretically, they should never even be
13175 instantiated, but that's hard to forestall. */
13176 error ("invalid constructor; you probably meant %<%T (const %T&)%>",
13177 ctype, ctype);
13178 return false;
13181 return true;
13184 /* DECL is a declaration for an overloaded or conversion operator. If
13185 COMPLAIN is true, errors are issued for invalid declarations. */
13187 bool
13188 grok_op_properties (tree decl, bool complain)
13190 tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
13191 bool methodp = TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE;
13192 tree name = DECL_NAME (decl);
13193 location_t loc = DECL_SOURCE_LOCATION (decl);
13195 tree class_type = DECL_CONTEXT (decl);
13196 if (class_type && !CLASS_TYPE_P (class_type))
13197 class_type = NULL_TREE;
13199 tree_code operator_code;
13200 unsigned op_flags;
13201 if (IDENTIFIER_CONV_OP_P (name))
13203 /* Conversion operators are TYPE_EXPR for the purposes of this
13204 function. */
13205 operator_code = TYPE_EXPR;
13206 op_flags = OVL_OP_FLAG_UNARY;
13208 else
13210 const ovl_op_info_t *ovl_op = IDENTIFIER_OVL_OP_INFO (name);
13212 operator_code = ovl_op->tree_code;
13213 op_flags = ovl_op->flags;
13214 gcc_checking_assert (operator_code != ERROR_MARK);
13215 DECL_OVERLOADED_OPERATOR_CODE_RAW (decl) = ovl_op->ovl_op_code;
13218 if (op_flags & OVL_OP_FLAG_ALLOC)
13220 /* operator new and operator delete are quite special. */
13221 if (class_type)
13222 switch (op_flags)
13224 case OVL_OP_FLAG_ALLOC:
13225 TYPE_HAS_NEW_OPERATOR (class_type) = 1;
13226 break;
13228 case OVL_OP_FLAG_ALLOC | OVL_OP_FLAG_DELETE:
13229 TYPE_GETS_DELETE (class_type) |= 1;
13230 break;
13232 case OVL_OP_FLAG_ALLOC | OVL_OP_FLAG_VEC:
13233 TYPE_HAS_ARRAY_NEW_OPERATOR (class_type) = 1;
13234 break;
13236 case OVL_OP_FLAG_ALLOC | OVL_OP_FLAG_DELETE | OVL_OP_FLAG_VEC:
13237 TYPE_GETS_DELETE (class_type) |= 2;
13238 break;
13240 default:
13241 gcc_unreachable ();
13244 /* [basic.std.dynamic.allocation]/1:
13246 A program is ill-formed if an allocation function is declared
13247 in a namespace scope other than global scope or declared
13248 static in global scope.
13250 The same also holds true for deallocation functions. */
13251 if (DECL_NAMESPACE_SCOPE_P (decl))
13253 if (CP_DECL_CONTEXT (decl) != global_namespace)
13255 error_at (loc, "%qD may not be declared within a namespace",
13256 decl);
13257 return false;
13260 if (!TREE_PUBLIC (decl))
13262 error_at (loc, "%qD may not be declared as static", decl);
13263 return false;
13267 if (op_flags & OVL_OP_FLAG_DELETE)
13268 TREE_TYPE (decl) = coerce_delete_type (TREE_TYPE (decl), loc);
13269 else
13271 DECL_IS_OPERATOR_NEW (decl) = 1;
13272 TREE_TYPE (decl) = coerce_new_type (TREE_TYPE (decl), loc);
13275 return true;
13278 /* An operator function must either be a non-static member function
13279 or have at least one parameter of a class, a reference to a class,
13280 an enumeration, or a reference to an enumeration. 13.4.0.6 */
13281 if (! methodp || DECL_STATIC_FUNCTION_P (decl))
13283 if (operator_code == TYPE_EXPR
13284 || operator_code == CALL_EXPR
13285 || operator_code == COMPONENT_REF
13286 || operator_code == ARRAY_REF
13287 || operator_code == NOP_EXPR)
13289 error_at (loc, "%qD must be a nonstatic member function", decl);
13290 return false;
13293 if (DECL_STATIC_FUNCTION_P (decl))
13295 error_at (loc, "%qD must be either a non-static member "
13296 "function or a non-member function", decl);
13297 return false;
13300 for (tree arg = argtypes; ; arg = TREE_CHAIN (arg))
13302 if (!arg || arg == void_list_node)
13304 if (complain)
13305 error_at(loc, "%qD must have an argument of class or "
13306 "enumerated type", decl);
13307 return false;
13310 tree type = non_reference (TREE_VALUE (arg));
13311 if (type == error_mark_node)
13312 return false;
13314 /* MAYBE_CLASS_TYPE_P, rather than CLASS_TYPE_P, is used
13315 because these checks are performed even on template
13316 functions. */
13317 if (MAYBE_CLASS_TYPE_P (type)
13318 || TREE_CODE (type) == ENUMERAL_TYPE)
13319 break;
13323 if (operator_code == CALL_EXPR)
13324 /* There are no further restrictions on the arguments to an overloaded
13325 "operator ()". */
13326 return true;
13328 if (operator_code == COND_EXPR)
13330 /* 13.4.0.3 */
13331 error_at (loc, "ISO C++ prohibits overloading operator ?:");
13332 return false;
13335 /* Count the number of arguments and check for ellipsis. */
13336 int arity = 0;
13337 for (tree arg = argtypes; arg != void_list_node; arg = TREE_CHAIN (arg))
13339 if (!arg)
13341 /* Variadic. */
13342 error_at (loc, "%qD must not have variable number of arguments",
13343 decl);
13344 return false;
13346 ++arity;
13349 /* Verify correct number of arguments. */
13350 switch (op_flags)
13352 case OVL_OP_FLAG_AMBIARY:
13353 if (arity == 1)
13355 /* We have a unary instance of an ambi-ary op. Remap to the
13356 unary one. */
13357 unsigned alt = ovl_op_alternate[ovl_op_mapping [operator_code]];
13358 const ovl_op_info_t *ovl_op = &ovl_op_info[false][alt];
13359 gcc_checking_assert (ovl_op->flags == OVL_OP_FLAG_UNARY);
13360 operator_code = ovl_op->tree_code;
13361 DECL_OVERLOADED_OPERATOR_CODE_RAW (decl) = ovl_op->ovl_op_code;
13363 else if (arity != 2)
13365 /* This was an ambiguous operator but is invalid. */
13366 error_at (loc,
13367 methodp
13368 ? G_("%qD must have either zero or one argument")
13369 : G_("%qD must have either one or two arguments"), decl);
13370 return false;
13372 else if ((operator_code == POSTINCREMENT_EXPR
13373 || operator_code == POSTDECREMENT_EXPR)
13374 && ! processing_template_decl
13375 /* x++ and x--'s second argument must be an int. */
13376 && ! same_type_p (TREE_VALUE (TREE_CHAIN (argtypes)),
13377 integer_type_node))
13379 error_at (loc,
13380 methodp
13381 ? G_("postfix %qD must have %<int%> as its argument")
13382 : G_("postfix %qD must have %<int%> as its second argument"),
13383 decl);
13384 return false;
13386 break;
13388 case OVL_OP_FLAG_UNARY:
13389 if (arity != 1)
13391 error_at (loc,
13392 methodp
13393 ? G_("%qD must have no arguments")
13394 : G_("%qD must have exactly one argument"), decl);
13395 return false;
13397 break;
13399 case OVL_OP_FLAG_BINARY:
13400 if (arity != 2)
13402 error_at (loc,
13403 methodp
13404 ? G_("%qD must have exactly one argument")
13405 : G_("%qD must have exactly two arguments"), decl);
13406 return false;
13408 break;
13410 default:
13411 gcc_unreachable ();
13414 /* There can be no default arguments. */
13415 for (tree arg = argtypes; arg != void_list_node; arg = TREE_CHAIN (arg))
13416 if (TREE_PURPOSE (arg))
13418 TREE_PURPOSE (arg) = NULL_TREE;
13419 error_at (loc, "%qD cannot have default arguments", decl);
13420 return false;
13423 /* At this point the declaration is well-formed. It may not be
13424 sensible though. */
13426 /* Check member function warnings only on the in-class declaration.
13427 There's no point warning on an out-of-class definition. */
13428 if (class_type && class_type != current_class_type)
13429 return true;
13431 /* Warn about conversion operators that will never be used. */
13432 if (IDENTIFIER_CONV_OP_P (name)
13433 && ! DECL_TEMPLATE_INFO (decl)
13434 && warn_conversion)
13436 tree t = TREE_TYPE (name);
13437 int ref = TYPE_REF_P (t);
13439 if (ref)
13440 t = TYPE_MAIN_VARIANT (TREE_TYPE (t));
13442 if (VOID_TYPE_P (t))
13443 warning_at (loc, OPT_Wconversion,
13445 ? G_("conversion to a reference to void "
13446 "will never use a type conversion operator")
13447 : G_("conversion to void "
13448 "will never use a type conversion operator"));
13449 else if (class_type)
13451 if (t == class_type)
13452 warning_at (loc, OPT_Wconversion,
13454 ? G_("conversion to a reference to the same type "
13455 "will never use a type conversion operator")
13456 : G_("conversion to the same type "
13457 "will never use a type conversion operator"));
13458 /* Don't force t to be complete here. */
13459 else if (MAYBE_CLASS_TYPE_P (t)
13460 && COMPLETE_TYPE_P (t)
13461 && DERIVED_FROM_P (t, class_type))
13462 warning_at (loc, OPT_Wconversion,
13464 ? G_("conversion to a reference to a base class "
13465 "will never use a type conversion operator")
13466 : G_("conversion to a base class "
13467 "will never use a type conversion operator"));
13471 if (!warn_ecpp)
13472 return true;
13474 /* Effective C++ rules below. */
13476 /* More Effective C++ rule 7. */
13477 if (operator_code == TRUTH_ANDIF_EXPR
13478 || operator_code == TRUTH_ORIF_EXPR
13479 || operator_code == COMPOUND_EXPR)
13480 warning_at (loc, OPT_Weffc__,
13481 "user-defined %qD always evaluates both arguments", decl);
13483 /* More Effective C++ rule 6. */
13484 if (operator_code == POSTINCREMENT_EXPR
13485 || operator_code == POSTDECREMENT_EXPR
13486 || operator_code == PREINCREMENT_EXPR
13487 || operator_code == PREDECREMENT_EXPR)
13489 tree arg = TREE_VALUE (argtypes);
13490 tree ret = TREE_TYPE (TREE_TYPE (decl));
13491 if (methodp || TYPE_REF_P (arg))
13492 arg = TREE_TYPE (arg);
13493 arg = TYPE_MAIN_VARIANT (arg);
13495 if (operator_code == PREINCREMENT_EXPR
13496 || operator_code == PREDECREMENT_EXPR)
13498 if (!TYPE_REF_P (ret)
13499 || !same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (ret)), arg))
13500 warning_at (loc, OPT_Weffc__, "prefix %qD should return %qT", decl,
13501 build_reference_type (arg));
13503 else
13505 if (!same_type_p (TYPE_MAIN_VARIANT (ret), arg))
13506 warning_at (loc, OPT_Weffc__,
13507 "postfix %qD should return %qT", decl, arg);
13511 /* Effective C++ rule 23. */
13512 if (!DECL_ASSIGNMENT_OPERATOR_P (decl)
13513 && (operator_code == PLUS_EXPR
13514 || operator_code == MINUS_EXPR
13515 || operator_code == TRUNC_DIV_EXPR
13516 || operator_code == MULT_EXPR
13517 || operator_code == TRUNC_MOD_EXPR)
13518 && TYPE_REF_P (TREE_TYPE (TREE_TYPE (decl))))
13519 warning_at (loc, OPT_Weffc__, "%qD should return by value", decl);
13521 return true;
13524 /* Return a string giving the keyword associate with CODE. */
13526 static const char *
13527 tag_name (enum tag_types code)
13529 switch (code)
13531 case record_type:
13532 return "struct";
13533 case class_type:
13534 return "class";
13535 case union_type:
13536 return "union";
13537 case enum_type:
13538 return "enum";
13539 case typename_type:
13540 return "typename";
13541 default:
13542 gcc_unreachable ();
13546 /* Name lookup in an elaborated-type-specifier (after the keyword
13547 indicated by TAG_CODE) has found the TYPE_DECL DECL. If the
13548 elaborated-type-specifier is invalid, issue a diagnostic and return
13549 error_mark_node; otherwise, return the *_TYPE to which it referred.
13550 If ALLOW_TEMPLATE_P is true, TYPE may be a class template. */
13552 tree
13553 check_elaborated_type_specifier (enum tag_types tag_code,
13554 tree decl,
13555 bool allow_template_p)
13557 tree type;
13559 /* In the case of:
13561 struct S { struct S *p; };
13563 name lookup will find the TYPE_DECL for the implicit "S::S"
13564 typedef. Adjust for that here. */
13565 if (DECL_SELF_REFERENCE_P (decl))
13566 decl = TYPE_NAME (TREE_TYPE (decl));
13568 type = TREE_TYPE (decl);
13570 /* Check TEMPLATE_TYPE_PARM first because DECL_IMPLICIT_TYPEDEF_P
13571 is false for this case as well. */
13572 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
13574 error ("using template type parameter %qT after %qs",
13575 type, tag_name (tag_code));
13576 return error_mark_node;
13578 /* Accept template template parameters. */
13579 else if (allow_template_p
13580 && (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM
13581 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM))
13583 /* [dcl.type.elab]
13585 If the identifier resolves to a typedef-name or the
13586 simple-template-id resolves to an alias template
13587 specialization, the elaborated-type-specifier is ill-formed.
13589 In other words, the only legitimate declaration to use in the
13590 elaborated type specifier is the implicit typedef created when
13591 the type is declared. */
13592 else if (!DECL_IMPLICIT_TYPEDEF_P (decl)
13593 && !DECL_SELF_REFERENCE_P (decl)
13594 && tag_code != typename_type)
13596 if (alias_template_specialization_p (type))
13597 error ("using alias template specialization %qT after %qs",
13598 type, tag_name (tag_code));
13599 else
13600 error ("using typedef-name %qD after %qs", decl, tag_name (tag_code));
13601 inform (DECL_SOURCE_LOCATION (decl),
13602 "%qD has a previous declaration here", decl);
13603 return error_mark_node;
13605 else if (TREE_CODE (type) != RECORD_TYPE
13606 && TREE_CODE (type) != UNION_TYPE
13607 && tag_code != enum_type
13608 && tag_code != typename_type)
13610 error ("%qT referred to as %qs", type, tag_name (tag_code));
13611 inform (location_of (type), "%qT has a previous declaration here", type);
13612 return error_mark_node;
13614 else if (TREE_CODE (type) != ENUMERAL_TYPE
13615 && tag_code == enum_type)
13617 error ("%qT referred to as enum", type);
13618 inform (location_of (type), "%qT has a previous declaration here", type);
13619 return error_mark_node;
13621 else if (!allow_template_p
13622 && TREE_CODE (type) == RECORD_TYPE
13623 && CLASSTYPE_IS_TEMPLATE (type))
13625 /* If a class template appears as elaborated type specifier
13626 without a template header such as:
13628 template <class T> class C {};
13629 void f(class C); // No template header here
13631 then the required template argument is missing. */
13632 error ("template argument required for %<%s %T%>",
13633 tag_name (tag_code),
13634 DECL_NAME (CLASSTYPE_TI_TEMPLATE (type)));
13635 return error_mark_node;
13638 return type;
13641 /* Lookup NAME in elaborate type specifier in scope according to
13642 SCOPE and issue diagnostics if necessary.
13643 Return *_TYPE node upon success, NULL_TREE when the NAME is not
13644 found, and ERROR_MARK_NODE for type error. */
13646 static tree
13647 lookup_and_check_tag (enum tag_types tag_code, tree name,
13648 tag_scope scope, bool template_header_p)
13650 tree t;
13651 tree decl;
13652 if (scope == ts_global)
13654 /* First try ordinary name lookup, ignoring hidden class name
13655 injected via friend declaration. */
13656 decl = lookup_name_prefer_type (name, 2);
13657 decl = strip_using_decl (decl);
13658 /* If that fails, the name will be placed in the smallest
13659 non-class, non-function-prototype scope according to 3.3.1/5.
13660 We may already have a hidden name declared as friend in this
13661 scope. So lookup again but not ignoring hidden names.
13662 If we find one, that name will be made visible rather than
13663 creating a new tag. */
13664 if (!decl)
13665 decl = lookup_type_scope (name, ts_within_enclosing_non_class);
13667 else
13668 decl = lookup_type_scope (name, scope);
13670 if (decl
13671 && (DECL_CLASS_TEMPLATE_P (decl)
13672 /* If scope is ts_current we're defining a class, so ignore a
13673 template template parameter. */
13674 || (scope != ts_current
13675 && DECL_TEMPLATE_TEMPLATE_PARM_P (decl))))
13676 decl = DECL_TEMPLATE_RESULT (decl);
13678 if (decl && TREE_CODE (decl) == TYPE_DECL)
13680 /* Look for invalid nested type:
13681 class C {
13682 class C {};
13683 }; */
13684 if (scope == ts_current && DECL_SELF_REFERENCE_P (decl))
13686 error ("%qD has the same name as the class in which it is "
13687 "declared",
13688 decl);
13689 return error_mark_node;
13692 /* Two cases we need to consider when deciding if a class
13693 template is allowed as an elaborated type specifier:
13694 1. It is a self reference to its own class.
13695 2. It comes with a template header.
13697 For example:
13699 template <class T> class C {
13700 class C *c1; // DECL_SELF_REFERENCE_P is true
13701 class D;
13703 template <class U> class C; // template_header_p is true
13704 template <class T> class C<T>::D {
13705 class C *c2; // DECL_SELF_REFERENCE_P is true
13706 }; */
13708 t = check_elaborated_type_specifier (tag_code,
13709 decl,
13710 template_header_p
13711 | DECL_SELF_REFERENCE_P (decl));
13712 if (template_header_p && t && CLASS_TYPE_P (t)
13713 && (!CLASSTYPE_TEMPLATE_INFO (t)
13714 || (!PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t)))))
13716 error ("%qT is not a template", t);
13717 inform (location_of (t), "previous declaration here");
13718 if (TYPE_CLASS_SCOPE_P (t)
13719 && CLASSTYPE_TEMPLATE_INFO (TYPE_CONTEXT (t)))
13720 inform (input_location,
13721 "perhaps you want to explicitly add %<%T::%>",
13722 TYPE_CONTEXT (t));
13723 t = error_mark_node;
13726 return t;
13728 else if (decl && TREE_CODE (decl) == TREE_LIST)
13730 error ("reference to %qD is ambiguous", name);
13731 print_candidates (decl);
13732 return error_mark_node;
13734 else
13735 return NULL_TREE;
13738 /* Get the struct, enum or union (TAG_CODE says which) with tag NAME.
13739 Define the tag as a forward-reference if it is not defined.
13741 If a declaration is given, process it here, and report an error if
13742 multiple declarations are not identical.
13744 SCOPE is TS_CURRENT when this is also a definition. Only look in
13745 the current frame for the name (since C++ allows new names in any
13746 scope.) It is TS_WITHIN_ENCLOSING_NON_CLASS if this is a friend
13747 declaration. Only look beginning from the current scope outward up
13748 till the nearest non-class scope. Otherwise it is TS_GLOBAL.
13750 TEMPLATE_HEADER_P is true when this declaration is preceded by
13751 a set of template parameters. */
13753 static tree
13754 xref_tag_1 (enum tag_types tag_code, tree name,
13755 tag_scope scope, bool template_header_p)
13757 enum tree_code code;
13758 tree context = NULL_TREE;
13760 gcc_assert (identifier_p (name));
13762 switch (tag_code)
13764 case record_type:
13765 case class_type:
13766 code = RECORD_TYPE;
13767 break;
13768 case union_type:
13769 code = UNION_TYPE;
13770 break;
13771 case enum_type:
13772 code = ENUMERAL_TYPE;
13773 break;
13774 default:
13775 gcc_unreachable ();
13778 /* In case of anonymous name, xref_tag is only called to
13779 make type node and push name. Name lookup is not required. */
13780 tree t = NULL_TREE;
13781 if (scope != ts_lambda && !anon_aggrname_p (name))
13782 t = lookup_and_check_tag (tag_code, name, scope, template_header_p);
13784 if (t == error_mark_node)
13785 return error_mark_node;
13787 if (scope != ts_current && t && current_class_type
13788 && template_class_depth (current_class_type)
13789 && template_header_p)
13791 if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM)
13792 return t;
13794 /* Since SCOPE is not TS_CURRENT, we are not looking at a
13795 definition of this tag. Since, in addition, we are currently
13796 processing a (member) template declaration of a template
13797 class, we must be very careful; consider:
13799 template <class X> struct S1
13801 template <class U> struct S2
13803 template <class V> friend struct S1;
13806 Here, the S2::S1 declaration should not be confused with the
13807 outer declaration. In particular, the inner version should
13808 have a template parameter of level 2, not level 1.
13810 On the other hand, when presented with:
13812 template <class T> struct S1
13814 template <class U> struct S2 {};
13815 template <class U> friend struct S2;
13818 the friend must find S1::S2 eventually. We accomplish this
13819 by making sure that the new type we create to represent this
13820 declaration has the right TYPE_CONTEXT. */
13821 context = TYPE_CONTEXT (t);
13822 t = NULL_TREE;
13825 if (! t)
13827 /* If no such tag is yet defined, create a forward-reference node
13828 and record it as the "definition".
13829 When a real declaration of this type is found,
13830 the forward-reference will be altered into a real type. */
13831 if (code == ENUMERAL_TYPE)
13833 error ("use of enum %q#D without previous declaration", name);
13834 return error_mark_node;
13836 else
13838 t = make_class_type (code);
13839 TYPE_CONTEXT (t) = context;
13840 if (scope == ts_lambda)
13842 /* Mark it as a lambda type. */
13843 CLASSTYPE_LAMBDA_EXPR (t) = error_mark_node;
13844 /* And push it into current scope. */
13845 scope = ts_current;
13847 t = pushtag (name, t, scope);
13850 else
13852 if (template_header_p && MAYBE_CLASS_TYPE_P (t))
13854 /* Check that we aren't trying to overload a class with different
13855 constraints. */
13856 tree constr = NULL_TREE;
13857 if (current_template_parms)
13859 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
13860 constr = build_constraints (reqs, NULL_TREE);
13862 if (!redeclare_class_template (t, current_template_parms, constr))
13863 return error_mark_node;
13865 else if (!processing_template_decl
13866 && CLASS_TYPE_P (t)
13867 && CLASSTYPE_IS_TEMPLATE (t))
13869 error ("redeclaration of %qT as a non-template", t);
13870 inform (location_of (t), "previous declaration %qD", t);
13871 return error_mark_node;
13874 if (scope != ts_within_enclosing_non_class && TYPE_HIDDEN_P (t))
13876 /* This is no longer an invisible friend. Make it
13877 visible. */
13878 tree decl = TYPE_NAME (t);
13880 DECL_ANTICIPATED (decl) = false;
13881 DECL_FRIEND_P (decl) = false;
13883 if (TYPE_TEMPLATE_INFO (t))
13885 tree tmpl = TYPE_TI_TEMPLATE (t);
13886 DECL_ANTICIPATED (tmpl) = false;
13887 DECL_FRIEND_P (tmpl) = false;
13892 return t;
13895 /* Wrapper for xref_tag_1. */
13897 tree
13898 xref_tag (enum tag_types tag_code, tree name,
13899 tag_scope scope, bool template_header_p)
13901 tree ret;
13902 bool subtime;
13903 subtime = timevar_cond_start (TV_NAME_LOOKUP);
13904 ret = xref_tag_1 (tag_code, name, scope, template_header_p);
13905 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
13906 return ret;
13910 tree
13911 xref_tag_from_type (tree old, tree id, tag_scope scope)
13913 enum tag_types tag_kind;
13915 if (TREE_CODE (old) == RECORD_TYPE)
13916 tag_kind = (CLASSTYPE_DECLARED_CLASS (old) ? class_type : record_type);
13917 else
13918 tag_kind = union_type;
13920 if (id == NULL_TREE)
13921 id = TYPE_IDENTIFIER (old);
13923 return xref_tag (tag_kind, id, scope, false);
13926 /* Create the binfo hierarchy for REF with (possibly NULL) base list
13927 BASE_LIST. For each element on BASE_LIST the TREE_PURPOSE is an
13928 access_* node, and the TREE_VALUE is the type of the base-class.
13929 Non-NULL TREE_TYPE indicates virtual inheritance. */
13931 void
13932 xref_basetypes (tree ref, tree base_list)
13934 tree *basep;
13935 tree binfo, base_binfo;
13936 unsigned max_vbases = 0; /* Maximum direct & indirect virtual bases. */
13937 unsigned max_bases = 0; /* Maximum direct bases. */
13938 unsigned max_dvbases = 0; /* Maximum direct virtual bases. */
13939 int i;
13940 tree default_access;
13941 tree igo_prev; /* Track Inheritance Graph Order. */
13943 if (ref == error_mark_node)
13944 return;
13946 /* The base of a derived class is private by default, all others are
13947 public. */
13948 default_access = (TREE_CODE (ref) == RECORD_TYPE
13949 && CLASSTYPE_DECLARED_CLASS (ref)
13950 ? access_private_node : access_public_node);
13952 /* First, make sure that any templates in base-classes are
13953 instantiated. This ensures that if we call ourselves recursively
13954 we do not get confused about which classes are marked and which
13955 are not. */
13956 basep = &base_list;
13957 while (*basep)
13959 tree basetype = TREE_VALUE (*basep);
13961 /* The dependent_type_p call below should really be dependent_scope_p
13962 so that we give a hard error about using an incomplete type as a
13963 base, but we allow it with a pedwarn for backward
13964 compatibility. */
13965 if (processing_template_decl
13966 && CLASS_TYPE_P (basetype) && TYPE_BEING_DEFINED (basetype))
13967 cxx_incomplete_type_diagnostic (NULL_TREE, basetype, DK_PEDWARN);
13968 if (!dependent_type_p (basetype)
13969 && !complete_type_or_else (basetype, NULL))
13970 /* An incomplete type. Remove it from the list. */
13971 *basep = TREE_CHAIN (*basep);
13972 else
13974 max_bases++;
13975 if (TREE_TYPE (*basep))
13976 max_dvbases++;
13977 if (CLASS_TYPE_P (basetype))
13978 max_vbases += vec_safe_length (CLASSTYPE_VBASECLASSES (basetype));
13979 basep = &TREE_CHAIN (*basep);
13982 max_vbases += max_dvbases;
13984 TYPE_MARKED_P (ref) = 1;
13986 /* The binfo slot should be empty, unless this is an (ill-formed)
13987 redefinition. */
13988 gcc_assert (!TYPE_BINFO (ref) || TYPE_SIZE (ref));
13990 gcc_assert (TYPE_MAIN_VARIANT (ref) == ref);
13992 binfo = make_tree_binfo (max_bases);
13994 TYPE_BINFO (ref) = binfo;
13995 BINFO_OFFSET (binfo) = size_zero_node;
13996 BINFO_TYPE (binfo) = ref;
13998 /* Apply base-class info set up to the variants of this type. */
13999 fixup_type_variants (ref);
14001 if (max_bases)
14003 vec_alloc (BINFO_BASE_ACCESSES (binfo), max_bases);
14004 /* A C++98 POD cannot have base classes. */
14005 CLASSTYPE_NON_LAYOUT_POD_P (ref) = true;
14007 if (TREE_CODE (ref) == UNION_TYPE)
14009 error ("derived union %qT invalid", ref);
14010 return;
14014 if (max_bases > 1)
14015 warning (OPT_Wmultiple_inheritance,
14016 "%qT defined with multiple direct bases", ref);
14018 if (max_vbases)
14020 /* An aggregate can't have virtual base classes. */
14021 CLASSTYPE_NON_AGGREGATE (ref) = true;
14023 vec_alloc (CLASSTYPE_VBASECLASSES (ref), max_vbases);
14025 if (max_dvbases)
14026 warning (OPT_Wvirtual_inheritance,
14027 "%qT defined with direct virtual base", ref);
14030 for (igo_prev = binfo; base_list; base_list = TREE_CHAIN (base_list))
14032 tree access = TREE_PURPOSE (base_list);
14033 int via_virtual = TREE_TYPE (base_list) != NULL_TREE;
14034 tree basetype = TREE_VALUE (base_list);
14036 if (access == access_default_node)
14037 access = default_access;
14039 /* Before C++17, an aggregate cannot have base classes. In C++17, an
14040 aggregate can't have virtual, private, or protected base classes. */
14041 if (cxx_dialect < cxx17
14042 || access != access_public_node
14043 || via_virtual)
14044 CLASSTYPE_NON_AGGREGATE (ref) = true;
14046 if (PACK_EXPANSION_P (basetype))
14047 basetype = PACK_EXPANSION_PATTERN (basetype);
14048 if (TREE_CODE (basetype) == TYPE_DECL)
14049 basetype = TREE_TYPE (basetype);
14050 if (!MAYBE_CLASS_TYPE_P (basetype) || TREE_CODE (basetype) == UNION_TYPE)
14052 error ("base type %qT fails to be a struct or class type",
14053 basetype);
14054 goto dropped_base;
14057 base_binfo = NULL_TREE;
14058 if (CLASS_TYPE_P (basetype) && !dependent_scope_p (basetype))
14060 base_binfo = TYPE_BINFO (basetype);
14061 /* The original basetype could have been a typedef'd type. */
14062 basetype = BINFO_TYPE (base_binfo);
14064 /* Inherit flags from the base. */
14065 TYPE_HAS_NEW_OPERATOR (ref)
14066 |= TYPE_HAS_NEW_OPERATOR (basetype);
14067 TYPE_HAS_ARRAY_NEW_OPERATOR (ref)
14068 |= TYPE_HAS_ARRAY_NEW_OPERATOR (basetype);
14069 TYPE_GETS_DELETE (ref) |= TYPE_GETS_DELETE (basetype);
14070 TYPE_HAS_CONVERSION (ref) |= TYPE_HAS_CONVERSION (basetype);
14071 CLASSTYPE_DIAMOND_SHAPED_P (ref)
14072 |= CLASSTYPE_DIAMOND_SHAPED_P (basetype);
14073 CLASSTYPE_REPEATED_BASE_P (ref)
14074 |= CLASSTYPE_REPEATED_BASE_P (basetype);
14077 /* We must do this test after we've seen through a typedef
14078 type. */
14079 if (TYPE_MARKED_P (basetype))
14081 if (basetype == ref)
14082 error ("recursive type %qT undefined", basetype);
14083 else
14084 error ("duplicate base type %qT invalid", basetype);
14085 goto dropped_base;
14088 if (PACK_EXPANSION_P (TREE_VALUE (base_list)))
14089 /* Regenerate the pack expansion for the bases. */
14090 basetype = make_pack_expansion (basetype);
14092 TYPE_MARKED_P (basetype) = 1;
14094 base_binfo = copy_binfo (base_binfo, basetype, ref,
14095 &igo_prev, via_virtual);
14096 if (!BINFO_INHERITANCE_CHAIN (base_binfo))
14097 BINFO_INHERITANCE_CHAIN (base_binfo) = binfo;
14099 BINFO_BASE_APPEND (binfo, base_binfo);
14100 BINFO_BASE_ACCESS_APPEND (binfo, access);
14101 continue;
14103 dropped_base:
14104 /* Update max_vbases to reflect the reality that we are dropping
14105 this base: if it reaches zero we want to undo the vec_alloc
14106 above to avoid inconsistencies during error-recovery: eg, in
14107 build_special_member_call, CLASSTYPE_VBASECLASSES non null
14108 and vtt null (c++/27952). */
14109 if (via_virtual)
14110 max_vbases--;
14111 if (CLASS_TYPE_P (basetype))
14112 max_vbases
14113 -= vec_safe_length (CLASSTYPE_VBASECLASSES (basetype));
14116 if (CLASSTYPE_VBASECLASSES (ref)
14117 && max_vbases == 0)
14118 vec_free (CLASSTYPE_VBASECLASSES (ref));
14120 if (vec_safe_length (CLASSTYPE_VBASECLASSES (ref)) < max_vbases)
14121 /* If we didn't get max_vbases vbases, we must have shared at
14122 least one of them, and are therefore diamond shaped. */
14123 CLASSTYPE_DIAMOND_SHAPED_P (ref) = 1;
14125 /* Unmark all the types. */
14126 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
14127 TYPE_MARKED_P (BINFO_TYPE (base_binfo)) = 0;
14128 TYPE_MARKED_P (ref) = 0;
14130 /* Now see if we have a repeated base type. */
14131 if (!CLASSTYPE_REPEATED_BASE_P (ref))
14133 for (base_binfo = binfo; base_binfo;
14134 base_binfo = TREE_CHAIN (base_binfo))
14136 if (TYPE_MARKED_P (BINFO_TYPE (base_binfo)))
14138 CLASSTYPE_REPEATED_BASE_P (ref) = 1;
14139 break;
14141 TYPE_MARKED_P (BINFO_TYPE (base_binfo)) = 1;
14143 for (base_binfo = binfo; base_binfo;
14144 base_binfo = TREE_CHAIN (base_binfo))
14145 if (TYPE_MARKED_P (BINFO_TYPE (base_binfo)))
14146 TYPE_MARKED_P (BINFO_TYPE (base_binfo)) = 0;
14147 else
14148 break;
14153 /* Copies the enum-related properties from type SRC to type DST.
14154 Used with the underlying type of an enum and the enum itself. */
14155 static void
14156 copy_type_enum (tree dst, tree src)
14158 tree t;
14159 for (t = dst; t; t = TYPE_NEXT_VARIANT (t))
14161 TYPE_MIN_VALUE (t) = TYPE_MIN_VALUE (src);
14162 TYPE_MAX_VALUE (t) = TYPE_MAX_VALUE (src);
14163 TYPE_SIZE (t) = TYPE_SIZE (src);
14164 TYPE_SIZE_UNIT (t) = TYPE_SIZE_UNIT (src);
14165 SET_TYPE_MODE (dst, TYPE_MODE (src));
14166 TYPE_PRECISION (t) = TYPE_PRECISION (src);
14167 unsigned valign = TYPE_ALIGN (src);
14168 if (TYPE_USER_ALIGN (t))
14169 valign = MAX (valign, TYPE_ALIGN (t));
14170 else
14171 TYPE_USER_ALIGN (t) = TYPE_USER_ALIGN (src);
14172 SET_TYPE_ALIGN (t, valign);
14173 TYPE_UNSIGNED (t) = TYPE_UNSIGNED (src);
14177 /* Begin compiling the definition of an enumeration type.
14178 NAME is its name,
14180 if ENUMTYPE is not NULL_TREE then the type has alredy been found.
14182 UNDERLYING_TYPE is the type that will be used as the storage for
14183 the enumeration type. This should be NULL_TREE if no storage type
14184 was specified.
14186 ATTRIBUTES are any attributes specified after the enum-key.
14188 SCOPED_ENUM_P is true if this is a scoped enumeration type.
14190 if IS_NEW is not NULL, gets TRUE iff a new type is created.
14192 Returns the type object, as yet incomplete.
14193 Also records info about it so that build_enumerator
14194 may be used to declare the individual values as they are read. */
14196 tree
14197 start_enum (tree name, tree enumtype, tree underlying_type,
14198 tree attributes, bool scoped_enum_p, bool *is_new)
14200 tree prevtype = NULL_TREE;
14201 gcc_assert (identifier_p (name));
14203 if (is_new)
14204 *is_new = false;
14205 /* [C++0x dcl.enum]p5:
14207 If not explicitly specified, the underlying type of a scoped
14208 enumeration type is int. */
14209 if (!underlying_type && scoped_enum_p)
14210 underlying_type = integer_type_node;
14212 if (underlying_type)
14213 underlying_type = cv_unqualified (underlying_type);
14215 /* If this is the real definition for a previous forward reference,
14216 fill in the contents in the same object that used to be the
14217 forward reference. */
14218 if (!enumtype)
14219 enumtype = lookup_and_check_tag (enum_type, name,
14220 /*tag_scope=*/ts_current,
14221 /*template_header_p=*/false);
14223 /* In case of a template_decl, the only check that should be deferred
14224 to instantiation time is the comparison of underlying types. */
14225 if (enumtype && TREE_CODE (enumtype) == ENUMERAL_TYPE)
14227 if (scoped_enum_p != SCOPED_ENUM_P (enumtype))
14229 error_at (input_location, "scoped/unscoped mismatch "
14230 "in enum %q#T", enumtype);
14231 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype)),
14232 "previous definition here");
14233 enumtype = error_mark_node;
14235 else if (ENUM_FIXED_UNDERLYING_TYPE_P (enumtype) != !! underlying_type)
14237 error_at (input_location, "underlying type mismatch "
14238 "in enum %q#T", enumtype);
14239 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype)),
14240 "previous definition here");
14241 enumtype = error_mark_node;
14243 else if (underlying_type && ENUM_UNDERLYING_TYPE (enumtype)
14244 && !same_type_p (underlying_type,
14245 ENUM_UNDERLYING_TYPE (enumtype)))
14247 error_at (input_location, "different underlying type "
14248 "in enum %q#T", enumtype);
14249 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype)),
14250 "previous definition here");
14251 underlying_type = NULL_TREE;
14255 if (!enumtype || TREE_CODE (enumtype) != ENUMERAL_TYPE
14256 || processing_template_decl)
14258 /* In case of error, make a dummy enum to allow parsing to
14259 continue. */
14260 if (enumtype == error_mark_node)
14262 name = make_anon_name ();
14263 enumtype = NULL_TREE;
14266 /* enumtype may be an ENUMERAL_TYPE if this is a redefinition
14267 of an opaque enum, or an opaque enum of an already defined
14268 enumeration (C++11).
14269 In any other case, it'll be NULL_TREE. */
14270 if (!enumtype)
14272 if (is_new)
14273 *is_new = true;
14275 prevtype = enumtype;
14277 /* Do not push the decl more than once. */
14278 if (!enumtype
14279 || TREE_CODE (enumtype) != ENUMERAL_TYPE)
14281 enumtype = cxx_make_type (ENUMERAL_TYPE);
14282 enumtype = pushtag (name, enumtype, /*tag_scope=*/ts_current);
14284 /* std::byte aliases anything. */
14285 if (enumtype != error_mark_node
14286 && TYPE_CONTEXT (enumtype) == std_node
14287 && !strcmp ("byte", TYPE_NAME_STRING (enumtype)))
14288 TYPE_ALIAS_SET (enumtype) = 0;
14290 else
14291 enumtype = xref_tag (enum_type, name, /*tag_scope=*/ts_current,
14292 false);
14294 if (enumtype == error_mark_node)
14295 return error_mark_node;
14297 /* The enum is considered opaque until the opening '{' of the
14298 enumerator list. */
14299 SET_OPAQUE_ENUM_P (enumtype, true);
14300 ENUM_FIXED_UNDERLYING_TYPE_P (enumtype) = !! underlying_type;
14303 SET_SCOPED_ENUM_P (enumtype, scoped_enum_p);
14305 cplus_decl_attributes (&enumtype, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
14307 if (underlying_type)
14309 if (ENUM_UNDERLYING_TYPE (enumtype))
14310 /* We already checked that it matches, don't change it to a different
14311 typedef variant. */;
14312 else if (CP_INTEGRAL_TYPE_P (underlying_type))
14314 copy_type_enum (enumtype, underlying_type);
14315 ENUM_UNDERLYING_TYPE (enumtype) = underlying_type;
14317 else if (dependent_type_p (underlying_type))
14318 ENUM_UNDERLYING_TYPE (enumtype) = underlying_type;
14319 else
14320 error ("underlying type %qT of %qT must be an integral type",
14321 underlying_type, enumtype);
14324 /* If into a template class, the returned enum is always the first
14325 declaration (opaque or not) seen. This way all the references to
14326 this type will be to the same declaration. The following ones are used
14327 only to check for definition errors. */
14328 if (prevtype && processing_template_decl)
14329 return prevtype;
14330 else
14331 return enumtype;
14334 /* After processing and defining all the values of an enumeration type,
14335 install their decls in the enumeration type.
14336 ENUMTYPE is the type object. */
14338 void
14339 finish_enum_value_list (tree enumtype)
14341 tree values;
14342 tree underlying_type;
14343 tree decl;
14344 tree value;
14345 tree minnode, maxnode;
14346 tree t;
14348 bool fixed_underlying_type_p
14349 = ENUM_UNDERLYING_TYPE (enumtype) != NULL_TREE;
14351 /* We built up the VALUES in reverse order. */
14352 TYPE_VALUES (enumtype) = nreverse (TYPE_VALUES (enumtype));
14354 /* For an enum defined in a template, just set the type of the values;
14355 all further processing is postponed until the template is
14356 instantiated. We need to set the type so that tsubst of a CONST_DECL
14357 works. */
14358 if (processing_template_decl)
14360 for (values = TYPE_VALUES (enumtype);
14361 values;
14362 values = TREE_CHAIN (values))
14363 TREE_TYPE (TREE_VALUE (values)) = enumtype;
14364 return;
14367 /* Determine the minimum and maximum values of the enumerators. */
14368 if (TYPE_VALUES (enumtype))
14370 minnode = maxnode = NULL_TREE;
14372 for (values = TYPE_VALUES (enumtype);
14373 values;
14374 values = TREE_CHAIN (values))
14376 decl = TREE_VALUE (values);
14378 /* [dcl.enum]: Following the closing brace of an enum-specifier,
14379 each enumerator has the type of its enumeration. Prior to the
14380 closing brace, the type of each enumerator is the type of its
14381 initializing value. */
14382 TREE_TYPE (decl) = enumtype;
14384 /* Update the minimum and maximum values, if appropriate. */
14385 value = DECL_INITIAL (decl);
14386 if (value == error_mark_node)
14387 value = integer_zero_node;
14388 /* Figure out what the minimum and maximum values of the
14389 enumerators are. */
14390 if (!minnode)
14391 minnode = maxnode = value;
14392 else if (tree_int_cst_lt (maxnode, value))
14393 maxnode = value;
14394 else if (tree_int_cst_lt (value, minnode))
14395 minnode = value;
14398 else
14399 /* [dcl.enum]
14401 If the enumerator-list is empty, the underlying type is as if
14402 the enumeration had a single enumerator with value 0. */
14403 minnode = maxnode = integer_zero_node;
14405 if (!fixed_underlying_type_p)
14407 /* Compute the number of bits require to represent all values of the
14408 enumeration. We must do this before the type of MINNODE and
14409 MAXNODE are transformed, since tree_int_cst_min_precision relies
14410 on the TREE_TYPE of the value it is passed. */
14411 signop sgn = tree_int_cst_sgn (minnode) >= 0 ? UNSIGNED : SIGNED;
14412 int lowprec = tree_int_cst_min_precision (minnode, sgn);
14413 int highprec = tree_int_cst_min_precision (maxnode, sgn);
14414 int precision = MAX (lowprec, highprec);
14415 unsigned int itk;
14416 bool use_short_enum;
14418 /* Determine the underlying type of the enumeration.
14420 [dcl.enum]
14422 The underlying type of an enumeration is an integral type that
14423 can represent all the enumerator values defined in the
14424 enumeration. It is implementation-defined which integral type is
14425 used as the underlying type for an enumeration except that the
14426 underlying type shall not be larger than int unless the value of
14427 an enumerator cannot fit in an int or unsigned int.
14429 We use "int" or an "unsigned int" as the underlying type, even if
14430 a smaller integral type would work, unless the user has
14431 explicitly requested that we use the smallest possible type. The
14432 user can request that for all enumerations with a command line
14433 flag, or for just one enumeration with an attribute. */
14435 use_short_enum = flag_short_enums
14436 || lookup_attribute ("packed", TYPE_ATTRIBUTES (enumtype));
14438 /* If the precision of the type was specified with an attribute and it
14439 was too small, give an error. Otherwise, use it. */
14440 if (TYPE_PRECISION (enumtype))
14442 if (precision > TYPE_PRECISION (enumtype))
14443 error ("specified mode too small for enumeral values");
14444 else
14446 use_short_enum = true;
14447 precision = TYPE_PRECISION (enumtype);
14451 for (itk = (use_short_enum ? itk_char : itk_int);
14452 itk != itk_none;
14453 itk++)
14455 underlying_type = integer_types[itk];
14456 if (underlying_type != NULL_TREE
14457 && TYPE_PRECISION (underlying_type) >= precision
14458 && TYPE_SIGN (underlying_type) == sgn)
14459 break;
14461 if (itk == itk_none)
14463 /* DR 377
14465 IF no integral type can represent all the enumerator values, the
14466 enumeration is ill-formed. */
14467 error ("no integral type can represent all of the enumerator values "
14468 "for %qT", enumtype);
14469 precision = TYPE_PRECISION (long_long_integer_type_node);
14470 underlying_type = integer_types[itk_unsigned_long_long];
14473 /* [dcl.enum]
14475 The value of sizeof() applied to an enumeration type, an object
14476 of an enumeration type, or an enumerator, is the value of sizeof()
14477 applied to the underlying type. */
14478 copy_type_enum (enumtype, underlying_type);
14480 /* Compute the minimum and maximum values for the type.
14482 [dcl.enum]
14484 For an enumeration where emin is the smallest enumerator and emax
14485 is the largest, the values of the enumeration are the values of the
14486 underlying type in the range bmin to bmax, where bmin and bmax are,
14487 respectively, the smallest and largest values of the smallest bit-
14488 field that can store emin and emax. */
14490 /* The middle-end currently assumes that types with TYPE_PRECISION
14491 narrower than their underlying type are suitably zero or sign
14492 extended to fill their mode. Similarly, it assumes that the front
14493 end assures that a value of a particular type must be within
14494 TYPE_MIN_VALUE and TYPE_MAX_VALUE.
14496 We used to set these fields based on bmin and bmax, but that led
14497 to invalid assumptions like optimizing away bounds checking. So
14498 now we just set the TYPE_PRECISION, TYPE_MIN_VALUE, and
14499 TYPE_MAX_VALUE to the values for the mode above and only restrict
14500 the ENUM_UNDERLYING_TYPE for the benefit of diagnostics. */
14501 ENUM_UNDERLYING_TYPE (enumtype)
14502 = build_distinct_type_copy (underlying_type);
14503 TYPE_PRECISION (ENUM_UNDERLYING_TYPE (enumtype)) = precision;
14504 set_min_and_max_values_for_integral_type
14505 (ENUM_UNDERLYING_TYPE (enumtype), precision, sgn);
14507 /* If -fstrict-enums, still constrain TYPE_MIN/MAX_VALUE. */
14508 if (flag_strict_enums)
14509 set_min_and_max_values_for_integral_type (enumtype, precision, sgn);
14511 else
14512 underlying_type = ENUM_UNDERLYING_TYPE (enumtype);
14514 /* Convert each of the enumerators to the type of the underlying
14515 type of the enumeration. */
14516 for (values = TYPE_VALUES (enumtype); values; values = TREE_CHAIN (values))
14518 location_t saved_location;
14520 decl = TREE_VALUE (values);
14521 saved_location = input_location;
14522 input_location = DECL_SOURCE_LOCATION (decl);
14523 if (fixed_underlying_type_p)
14524 /* If the enumeration type has a fixed underlying type, we
14525 already checked all of the enumerator values. */
14526 value = DECL_INITIAL (decl);
14527 else
14528 value = perform_implicit_conversion (underlying_type,
14529 DECL_INITIAL (decl),
14530 tf_warning_or_error);
14531 input_location = saved_location;
14533 /* Do not clobber shared ints. */
14534 if (value != error_mark_node)
14536 value = copy_node (value);
14538 TREE_TYPE (value) = enumtype;
14540 DECL_INITIAL (decl) = value;
14543 /* Fix up all variant types of this enum type. */
14544 for (t = TYPE_MAIN_VARIANT (enumtype); t; t = TYPE_NEXT_VARIANT (t))
14545 TYPE_VALUES (t) = TYPE_VALUES (enumtype);
14547 if (at_class_scope_p ()
14548 && COMPLETE_TYPE_P (current_class_type)
14549 && UNSCOPED_ENUM_P (enumtype))
14551 insert_late_enum_def_bindings (current_class_type, enumtype);
14552 /* TYPE_FIELDS needs fixup. */
14553 fixup_type_variants (current_class_type);
14556 /* Finish debugging output for this type. */
14557 rest_of_type_compilation (enumtype, namespace_bindings_p ());
14559 /* Each enumerator now has the type of its enumeration. Clear the cache
14560 so that this change in types doesn't confuse us later on. */
14561 clear_cv_and_fold_caches ();
14564 /* Finishes the enum type. This is called only the first time an
14565 enumeration is seen, be it opaque or odinary.
14566 ENUMTYPE is the type object. */
14568 void
14569 finish_enum (tree enumtype)
14571 if (processing_template_decl)
14573 if (at_function_scope_p ())
14574 add_stmt (build_min (TAG_DEFN, enumtype));
14575 return;
14578 /* If this is a forward declaration, there should not be any variants,
14579 though we can get a variant in the middle of an enum-specifier with
14580 wacky code like 'enum E { e = sizeof(const E*) };' */
14581 gcc_assert (enumtype == TYPE_MAIN_VARIANT (enumtype)
14582 && (TYPE_VALUES (enumtype)
14583 || !TYPE_NEXT_VARIANT (enumtype)));
14586 /* Build and install a CONST_DECL for an enumeration constant of the
14587 enumeration type ENUMTYPE whose NAME and VALUE (if any) are provided.
14588 Apply ATTRIBUTES if available. LOC is the location of NAME.
14589 Assignment of sequential values by default is handled here. */
14591 void
14592 build_enumerator (tree name, tree value, tree enumtype, tree attributes,
14593 location_t loc)
14595 tree decl;
14596 tree context;
14597 tree type;
14599 /* scalar_constant_value will pull out this expression, so make sure
14600 it's folded as appropriate. */
14601 if (processing_template_decl)
14602 value = fold_non_dependent_expr (value);
14604 /* If the VALUE was erroneous, pretend it wasn't there; that will
14605 result in the enum being assigned the next value in sequence. */
14606 if (value == error_mark_node)
14607 value = NULL_TREE;
14609 /* Remove no-op casts from the value. */
14610 if (value)
14611 STRIP_TYPE_NOPS (value);
14613 if (! processing_template_decl)
14615 /* Validate and default VALUE. */
14616 if (value != NULL_TREE)
14618 if (!ENUM_UNDERLYING_TYPE (enumtype))
14620 tree tmp_value = build_expr_type_conversion (WANT_INT | WANT_ENUM,
14621 value, true);
14622 if (tmp_value)
14623 value = tmp_value;
14625 else if (! INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P
14626 (TREE_TYPE (value)))
14627 value = perform_implicit_conversion_flags
14628 (ENUM_UNDERLYING_TYPE (enumtype), value, tf_warning_or_error,
14629 LOOKUP_IMPLICIT | LOOKUP_NO_NARROWING);
14631 if (value == error_mark_node)
14632 value = NULL_TREE;
14634 if (value != NULL_TREE)
14636 if (! INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P
14637 (TREE_TYPE (value)))
14639 error ("enumerator value for %qD must have integral or "
14640 "unscoped enumeration type", name);
14641 value = NULL_TREE;
14643 else
14645 value = cxx_constant_value (value);
14647 if (TREE_CODE (value) != INTEGER_CST)
14649 error ("enumerator value for %qD is not an integer "
14650 "constant", name);
14651 value = NULL_TREE;
14657 /* Default based on previous value. */
14658 if (value == NULL_TREE)
14660 if (TYPE_VALUES (enumtype))
14662 tree prev_value;
14664 /* C++03 7.2/4: If no initializer is specified for the first
14665 enumerator, the type is an unspecified integral
14666 type. Otherwise the type is the same as the type of the
14667 initializing value of the preceding enumerator unless the
14668 incremented value is not representable in that type, in
14669 which case the type is an unspecified integral type
14670 sufficient to contain the incremented value. */
14671 prev_value = DECL_INITIAL (TREE_VALUE (TYPE_VALUES (enumtype)));
14672 if (error_operand_p (prev_value))
14673 value = error_mark_node;
14674 else
14676 wi::overflow_type overflowed;
14677 tree type = TREE_TYPE (prev_value);
14678 signop sgn = TYPE_SIGN (type);
14679 widest_int wi = wi::add (wi::to_widest (prev_value), 1, sgn,
14680 &overflowed);
14681 if (!overflowed)
14683 bool pos = !wi::neg_p (wi, sgn);
14684 if (!wi::fits_to_tree_p (wi, type))
14686 unsigned int itk;
14687 for (itk = itk_int; itk != itk_none; itk++)
14689 type = integer_types[itk];
14690 if (type != NULL_TREE
14691 && (pos || !TYPE_UNSIGNED (type))
14692 && wi::fits_to_tree_p (wi, type))
14693 break;
14695 if (type && cxx_dialect < cxx11
14696 && itk > itk_unsigned_long)
14697 pedwarn (input_location, OPT_Wlong_long,
14698 pos ? G_("\
14699 incremented enumerator value is too large for %<unsigned long%>") : G_("\
14700 incremented enumerator value is too large for %<long%>"));
14702 if (type == NULL_TREE)
14703 overflowed = wi::OVF_UNKNOWN;
14704 else
14705 value = wide_int_to_tree (type, wi);
14708 if (overflowed)
14710 error ("overflow in enumeration values at %qD", name);
14711 value = error_mark_node;
14715 else
14716 value = integer_zero_node;
14719 /* Remove no-op casts from the value. */
14720 STRIP_TYPE_NOPS (value);
14722 /* If the underlying type of the enum is fixed, check whether
14723 the enumerator values fits in the underlying type. If it
14724 does not fit, the program is ill-formed [C++0x dcl.enum]. */
14725 if (ENUM_UNDERLYING_TYPE (enumtype)
14726 && value
14727 && TREE_CODE (value) == INTEGER_CST)
14729 if (!int_fits_type_p (value, ENUM_UNDERLYING_TYPE (enumtype)))
14730 error ("enumerator value %qE is outside the range of underlying "
14731 "type %qT", value, ENUM_UNDERLYING_TYPE (enumtype));
14733 /* Convert the value to the appropriate type. */
14734 value = fold_convert (ENUM_UNDERLYING_TYPE (enumtype), value);
14738 /* C++ associates enums with global, function, or class declarations. */
14739 context = current_scope ();
14741 /* Build the actual enumeration constant. Note that the enumeration
14742 constants have the underlying type of the enum (if it is fixed)
14743 or the type of their initializer (if the underlying type of the
14744 enum is not fixed):
14746 [ C++0x dcl.enum ]
14748 If the underlying type is fixed, the type of each enumerator
14749 prior to the closing brace is the underlying type; if the
14750 initializing value of an enumerator cannot be represented by
14751 the underlying type, the program is ill-formed. If the
14752 underlying type is not fixed, the type of each enumerator is
14753 the type of its initializing value.
14755 If the underlying type is not fixed, it will be computed by
14756 finish_enum and we will reset the type of this enumerator. Of
14757 course, if we're processing a template, there may be no value. */
14758 type = value ? TREE_TYPE (value) : NULL_TREE;
14760 decl = build_decl (loc, CONST_DECL, name, type);
14762 DECL_CONTEXT (decl) = enumtype;
14763 TREE_CONSTANT (decl) = 1;
14764 TREE_READONLY (decl) = 1;
14765 DECL_INITIAL (decl) = value;
14767 if (attributes)
14768 cplus_decl_attributes (&decl, attributes, 0);
14770 if (context && context == current_class_type && !SCOPED_ENUM_P (enumtype))
14772 /* In something like `struct S { enum E { i = 7 }; };' we put `i'
14773 on the TYPE_FIELDS list for `S'. (That's so that you can say
14774 things like `S::i' later.) */
14776 /* The enumerator may be getting declared outside of its enclosing
14777 class, like so:
14779 class S { public: enum E : int; }; enum S::E : int { i = 7; };
14781 For which case we need to make sure that the access of `S::i'
14782 matches the access of `S::E'. */
14783 tree saved_cas = current_access_specifier;
14784 if (TREE_PRIVATE (TYPE_NAME (enumtype)))
14785 current_access_specifier = access_private_node;
14786 else if (TREE_PROTECTED (TYPE_NAME (enumtype)))
14787 current_access_specifier = access_protected_node;
14788 else
14789 current_access_specifier = access_public_node;
14791 finish_member_declaration (decl);
14793 current_access_specifier = saved_cas;
14795 else
14796 pushdecl (decl);
14798 /* Add this enumeration constant to the list for this type. */
14799 TYPE_VALUES (enumtype) = tree_cons (name, decl, TYPE_VALUES (enumtype));
14802 /* Look for an enumerator with the given NAME within the enumeration
14803 type ENUMTYPE. This routine is used primarily for qualified name
14804 lookup into an enumerator in C++0x, e.g.,
14806 enum class Color { Red, Green, Blue };
14808 Color color = Color::Red;
14810 Returns the value corresponding to the enumerator, or
14811 NULL_TREE if no such enumerator was found. */
14812 tree
14813 lookup_enumerator (tree enumtype, tree name)
14815 tree e;
14816 gcc_assert (enumtype && TREE_CODE (enumtype) == ENUMERAL_TYPE);
14818 e = purpose_member (name, TYPE_VALUES (enumtype));
14819 return e? TREE_VALUE (e) : NULL_TREE;
14823 /* We're defining DECL. Make sure that its type is OK. */
14825 static void
14826 check_function_type (tree decl, tree current_function_parms)
14828 tree fntype = TREE_TYPE (decl);
14829 tree return_type = complete_type (TREE_TYPE (fntype));
14831 /* In a function definition, arg types must be complete. */
14832 require_complete_types_for_parms (current_function_parms);
14834 if (dependent_type_p (return_type)
14835 || type_uses_auto (return_type))
14836 return;
14837 if (!COMPLETE_OR_VOID_TYPE_P (return_type))
14839 tree args = TYPE_ARG_TYPES (fntype);
14841 error ("return type %q#T is incomplete", return_type);
14843 /* Make it return void instead. */
14844 if (TREE_CODE (fntype) == METHOD_TYPE)
14845 fntype = build_method_type_directly (TREE_TYPE (TREE_VALUE (args)),
14846 void_type_node,
14847 TREE_CHAIN (args));
14848 else
14849 fntype = build_function_type (void_type_node, args);
14850 fntype = (cp_build_type_attribute_variant
14851 (fntype, TYPE_ATTRIBUTES (TREE_TYPE (decl))));
14852 fntype = cxx_copy_lang_qualifiers (fntype, TREE_TYPE (decl));
14853 TREE_TYPE (decl) = fntype;
14855 else
14857 abstract_virtuals_error (decl, TREE_TYPE (fntype));
14858 maybe_warn_parm_abi (TREE_TYPE (fntype),
14859 DECL_SOURCE_LOCATION (decl));
14863 /* True iff FN is an implicitly-defined default constructor. */
14865 static bool
14866 implicit_default_ctor_p (tree fn)
14868 return (DECL_CONSTRUCTOR_P (fn)
14869 && !user_provided_p (fn)
14870 && sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (fn)));
14873 /* Clobber the contents of *this to let the back end know that the object
14874 storage is dead when we enter the constructor or leave the destructor. */
14876 static tree
14877 build_clobber_this ()
14879 /* Clobbering an empty base is pointless, and harmful if its one byte
14880 TYPE_SIZE overlays real data. */
14881 if (is_empty_class (current_class_type))
14882 return void_node;
14884 /* If we have virtual bases, clobber the whole object, but only if we're in
14885 charge. If we don't have virtual bases, clobber the as-base type so we
14886 don't mess with tail padding. */
14887 bool vbases = CLASSTYPE_VBASECLASSES (current_class_type);
14889 tree ctype = current_class_type;
14890 if (!vbases)
14891 ctype = CLASSTYPE_AS_BASE (ctype);
14893 tree clobber = build_clobber (ctype);
14895 tree thisref = current_class_ref;
14896 if (ctype != current_class_type)
14898 thisref = build_nop (build_reference_type (ctype), current_class_ptr);
14899 thisref = convert_from_reference (thisref);
14902 tree exprstmt = build2 (MODIFY_EXPR, void_type_node, thisref, clobber);
14903 if (vbases)
14904 exprstmt = build_if_in_charge (exprstmt);
14906 return exprstmt;
14909 /* Create the FUNCTION_DECL for a function definition.
14910 DECLSPECS and DECLARATOR are the parts of the declaration;
14911 they describe the function's name and the type it returns,
14912 but twisted together in a fashion that parallels the syntax of C.
14914 FLAGS is a bitwise or of SF_PRE_PARSED (indicating that the
14915 DECLARATOR is really the DECL for the function we are about to
14916 process and that DECLSPECS should be ignored), SF_INCLASS_INLINE
14917 indicating that the function is an inline defined in-class.
14919 This function creates a binding context for the function body
14920 as well as setting up the FUNCTION_DECL in current_function_decl.
14922 For C++, we must first check whether that datum makes any sense.
14923 For example, "class A local_a(1,2);" means that variable local_a
14924 is an aggregate of type A, which should have a constructor
14925 applied to it with the argument list [1, 2].
14927 On entry, DECL_INITIAL (decl1) should be NULL_TREE or error_mark_node,
14928 or may be a BLOCK if the function has been defined previously
14929 in this translation unit. On exit, DECL_INITIAL (decl1) will be
14930 error_mark_node if the function has never been defined, or
14931 a BLOCK if the function has been defined somewhere. */
14933 bool
14934 start_preparsed_function (tree decl1, tree attrs, int flags)
14936 tree ctype = NULL_TREE;
14937 tree fntype;
14938 tree restype;
14939 int doing_friend = 0;
14940 cp_binding_level *bl;
14941 tree current_function_parms;
14942 struct c_fileinfo *finfo
14943 = get_fileinfo (LOCATION_FILE (DECL_SOURCE_LOCATION (decl1)));
14944 bool honor_interface;
14946 /* Sanity check. */
14947 gcc_assert (VOID_TYPE_P (TREE_VALUE (void_list_node)));
14948 gcc_assert (TREE_CHAIN (void_list_node) == NULL_TREE);
14950 fntype = TREE_TYPE (decl1);
14951 if (TREE_CODE (fntype) == METHOD_TYPE)
14952 ctype = TYPE_METHOD_BASETYPE (fntype);
14954 /* ISO C++ 11.4/5. A friend function defined in a class is in
14955 the (lexical) scope of the class in which it is defined. */
14956 if (!ctype && DECL_FRIEND_P (decl1))
14958 ctype = DECL_FRIEND_CONTEXT (decl1);
14960 /* CTYPE could be null here if we're dealing with a template;
14961 for example, `inline friend float foo()' inside a template
14962 will have no CTYPE set. */
14963 if (ctype && TREE_CODE (ctype) != RECORD_TYPE)
14964 ctype = NULL_TREE;
14965 else
14966 doing_friend = 1;
14969 if (DECL_DECLARED_INLINE_P (decl1)
14970 && lookup_attribute ("noinline", attrs))
14971 warning_at (DECL_SOURCE_LOCATION (decl1), 0,
14972 "inline function %qD given attribute noinline", decl1);
14974 /* Handle gnu_inline attribute. */
14975 if (GNU_INLINE_P (decl1))
14977 DECL_EXTERNAL (decl1) = 1;
14978 DECL_NOT_REALLY_EXTERN (decl1) = 0;
14979 DECL_INTERFACE_KNOWN (decl1) = 1;
14980 DECL_DISREGARD_INLINE_LIMITS (decl1) = 1;
14983 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (decl1))
14984 /* This is a constructor, we must ensure that any default args
14985 introduced by this definition are propagated to the clones
14986 now. The clones are used directly in overload resolution. */
14987 adjust_clone_args (decl1);
14989 /* Sometimes we don't notice that a function is a static member, and
14990 build a METHOD_TYPE for it. Fix that up now. */
14991 gcc_assert (!(ctype != NULL_TREE && DECL_STATIC_FUNCTION_P (decl1)
14992 && TREE_CODE (TREE_TYPE (decl1)) == METHOD_TYPE));
14994 /* Set up current_class_type, and enter the scope of the class, if
14995 appropriate. */
14996 if (ctype)
14997 push_nested_class (ctype);
14998 else if (DECL_STATIC_FUNCTION_P (decl1))
14999 push_nested_class (DECL_CONTEXT (decl1));
15001 /* Now that we have entered the scope of the class, we must restore
15002 the bindings for any template parameters surrounding DECL1, if it
15003 is an inline member template. (Order is important; consider the
15004 case where a template parameter has the same name as a field of
15005 the class.) It is not until after this point that
15006 PROCESSING_TEMPLATE_DECL is guaranteed to be set up correctly. */
15007 if (flags & SF_INCLASS_INLINE)
15008 maybe_begin_member_template_processing (decl1);
15010 /* Effective C++ rule 15. */
15011 if (warn_ecpp
15012 && DECL_ASSIGNMENT_OPERATOR_P (decl1)
15013 && DECL_OVERLOADED_OPERATOR_IS (decl1, NOP_EXPR)
15014 && VOID_TYPE_P (TREE_TYPE (fntype)))
15015 warning (OPT_Weffc__,
15016 "%<operator=%> should return a reference to %<*this%>");
15018 /* Make the init_value nonzero so pushdecl knows this is not tentative.
15019 error_mark_node is replaced below (in poplevel) with the BLOCK. */
15020 if (!DECL_INITIAL (decl1))
15021 DECL_INITIAL (decl1) = error_mark_node;
15023 /* This function exists in static storage.
15024 (This does not mean `static' in the C sense!) */
15025 TREE_STATIC (decl1) = 1;
15027 /* We must call push_template_decl after current_class_type is set
15028 up. (If we are processing inline definitions after exiting a
15029 class scope, current_class_type will be NULL_TREE until set above
15030 by push_nested_class.) */
15031 if (processing_template_decl)
15033 tree newdecl1 = push_template_decl (decl1);
15034 if (newdecl1 == error_mark_node)
15036 if (ctype || DECL_STATIC_FUNCTION_P (decl1))
15037 pop_nested_class ();
15038 return false;
15040 decl1 = newdecl1;
15043 /* Make sure the parameter and return types are reasonable. When
15044 you declare a function, these types can be incomplete, but they
15045 must be complete when you define the function. */
15046 check_function_type (decl1, DECL_ARGUMENTS (decl1));
15048 /* Build the return declaration for the function. */
15049 restype = TREE_TYPE (fntype);
15051 if (DECL_RESULT (decl1) == NULL_TREE)
15053 tree resdecl;
15055 resdecl = build_decl (input_location, RESULT_DECL, 0, restype);
15056 DECL_ARTIFICIAL (resdecl) = 1;
15057 DECL_IGNORED_P (resdecl) = 1;
15058 DECL_RESULT (decl1) = resdecl;
15060 cp_apply_type_quals_to_decl (cp_type_quals (restype), resdecl);
15063 /* Record the decl so that the function name is defined.
15064 If we already have a decl for this name, and it is a FUNCTION_DECL,
15065 use the old decl. */
15066 if (!processing_template_decl && !(flags & SF_PRE_PARSED))
15068 /* A specialization is not used to guide overload resolution. */
15069 if (!DECL_FUNCTION_MEMBER_P (decl1)
15070 && !(DECL_USE_TEMPLATE (decl1) &&
15071 PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl1))))
15073 tree olddecl = pushdecl (decl1);
15075 if (olddecl == error_mark_node)
15076 /* If something went wrong when registering the declaration,
15077 use DECL1; we have to have a FUNCTION_DECL to use when
15078 parsing the body of the function. */
15080 else
15082 /* Otherwise, OLDDECL is either a previous declaration
15083 of the same function or DECL1 itself. */
15085 if (warn_missing_declarations
15086 && olddecl == decl1
15087 && !DECL_MAIN_P (decl1)
15088 && TREE_PUBLIC (decl1)
15089 && !DECL_DECLARED_INLINE_P (decl1))
15091 tree context;
15093 /* Check whether DECL1 is in an anonymous
15094 namespace. */
15095 for (context = DECL_CONTEXT (decl1);
15096 context;
15097 context = DECL_CONTEXT (context))
15099 if (TREE_CODE (context) == NAMESPACE_DECL
15100 && DECL_NAME (context) == NULL_TREE)
15101 break;
15104 if (context == NULL)
15105 warning_at (DECL_SOURCE_LOCATION (decl1),
15106 OPT_Wmissing_declarations,
15107 "no previous declaration for %qD", decl1);
15110 decl1 = olddecl;
15113 else
15115 /* We need to set the DECL_CONTEXT. */
15116 if (!DECL_CONTEXT (decl1) && DECL_TEMPLATE_INFO (decl1))
15117 DECL_CONTEXT (decl1) = DECL_CONTEXT (DECL_TI_TEMPLATE (decl1));
15119 fntype = TREE_TYPE (decl1);
15120 restype = TREE_TYPE (fntype);
15122 /* If #pragma weak applies, mark the decl appropriately now.
15123 The pragma only applies to global functions. Because
15124 determining whether or not the #pragma applies involves
15125 computing the mangled name for the declaration, we cannot
15126 apply the pragma until after we have merged this declaration
15127 with any previous declarations; if the original declaration
15128 has a linkage specification, that specification applies to
15129 the definition as well, and may affect the mangled name. */
15130 if (DECL_FILE_SCOPE_P (decl1))
15131 maybe_apply_pragma_weak (decl1);
15134 /* We are now in the scope of the function being defined. */
15135 current_function_decl = decl1;
15137 /* Save the parm names or decls from this function's declarator
15138 where store_parm_decls will find them. */
15139 current_function_parms = DECL_ARGUMENTS (decl1);
15141 /* Let the user know we're compiling this function. */
15142 announce_function (decl1);
15144 gcc_assert (DECL_INITIAL (decl1));
15146 /* This function may already have been parsed, in which case just
15147 return; our caller will skip over the body without parsing. */
15148 if (DECL_INITIAL (decl1) != error_mark_node)
15149 return true;
15151 /* Initialize RTL machinery. We cannot do this until
15152 CURRENT_FUNCTION_DECL and DECL_RESULT are set up. We do this
15153 even when processing a template; this is how we get
15154 CFUN set up, and our per-function variables initialized.
15155 FIXME factor out the non-RTL stuff. */
15156 bl = current_binding_level;
15157 allocate_struct_function (decl1, processing_template_decl);
15159 /* Initialize the language data structures. Whenever we start
15160 a new function, we destroy temporaries in the usual way. */
15161 cfun->language = ggc_cleared_alloc<language_function> ();
15162 current_stmt_tree ()->stmts_are_full_exprs_p = 1;
15163 current_binding_level = bl;
15165 if (!processing_template_decl && type_uses_auto (restype))
15167 FNDECL_USED_AUTO (decl1) = true;
15168 current_function_auto_return_pattern = restype;
15171 /* Start the statement-tree, start the tree now. */
15172 DECL_SAVED_TREE (decl1) = push_stmt_list ();
15174 /* If we are (erroneously) defining a function that we have already
15175 defined before, wipe out what we knew before. */
15176 if (!DECL_PENDING_INLINE_P (decl1))
15177 DECL_SAVED_FUNCTION_DATA (decl1) = NULL;
15179 if (ctype && !doing_friend && !DECL_STATIC_FUNCTION_P (decl1))
15181 /* We know that this was set up by `grokclassfn'. We do not
15182 wait until `store_parm_decls', since evil parse errors may
15183 never get us to that point. Here we keep the consistency
15184 between `current_class_type' and `current_class_ptr'. */
15185 tree t = DECL_ARGUMENTS (decl1);
15187 gcc_assert (t != NULL_TREE && TREE_CODE (t) == PARM_DECL);
15188 gcc_assert (TYPE_PTR_P (TREE_TYPE (t)));
15190 cp_function_chain->x_current_class_ref
15191 = cp_build_fold_indirect_ref (t);
15192 /* Set this second to avoid shortcut in cp_build_indirect_ref. */
15193 cp_function_chain->x_current_class_ptr = t;
15195 /* Constructors and destructors need to know whether they're "in
15196 charge" of initializing virtual base classes. */
15197 t = DECL_CHAIN (t);
15198 if (DECL_HAS_IN_CHARGE_PARM_P (decl1))
15200 current_in_charge_parm = t;
15201 t = DECL_CHAIN (t);
15203 if (DECL_HAS_VTT_PARM_P (decl1))
15205 gcc_assert (DECL_NAME (t) == vtt_parm_identifier);
15206 current_vtt_parm = t;
15210 honor_interface = (!DECL_TEMPLATE_INSTANTIATION (decl1)
15211 /* Implicitly-defined methods (like the
15212 destructor for a class in which no destructor
15213 is explicitly declared) must not be defined
15214 until their definition is needed. So, we
15215 ignore interface specifications for
15216 compiler-generated functions. */
15217 && !DECL_ARTIFICIAL (decl1));
15219 if (processing_template_decl)
15220 /* Don't mess with interface flags. */;
15221 else if (DECL_INTERFACE_KNOWN (decl1))
15223 tree ctx = decl_function_context (decl1);
15225 if (DECL_NOT_REALLY_EXTERN (decl1))
15226 DECL_EXTERNAL (decl1) = 0;
15228 if (ctx != NULL_TREE && vague_linkage_p (ctx))
15229 /* This is a function in a local class in an extern inline
15230 or template function. */
15231 comdat_linkage (decl1);
15233 /* If this function belongs to an interface, it is public.
15234 If it belongs to someone else's interface, it is also external.
15235 This only affects inlines and template instantiations. */
15236 else if (!finfo->interface_unknown && honor_interface)
15238 if (DECL_DECLARED_INLINE_P (decl1)
15239 || DECL_TEMPLATE_INSTANTIATION (decl1))
15241 DECL_EXTERNAL (decl1)
15242 = (finfo->interface_only
15243 || (DECL_DECLARED_INLINE_P (decl1)
15244 && ! flag_implement_inlines
15245 && !DECL_VINDEX (decl1)));
15247 /* For WIN32 we also want to put these in linkonce sections. */
15248 maybe_make_one_only (decl1);
15250 else
15251 DECL_EXTERNAL (decl1) = 0;
15252 DECL_INTERFACE_KNOWN (decl1) = 1;
15253 /* If this function is in an interface implemented in this file,
15254 make sure that the back end knows to emit this function
15255 here. */
15256 if (!DECL_EXTERNAL (decl1))
15257 mark_needed (decl1);
15259 else if (finfo->interface_unknown && finfo->interface_only
15260 && honor_interface)
15262 /* If MULTIPLE_SYMBOL_SPACES is defined and we saw a #pragma
15263 interface, we will have both finfo->interface_unknown and
15264 finfo->interface_only set. In that case, we don't want to
15265 use the normal heuristics because someone will supply a
15266 #pragma implementation elsewhere, and deducing it here would
15267 produce a conflict. */
15268 comdat_linkage (decl1);
15269 DECL_EXTERNAL (decl1) = 0;
15270 DECL_INTERFACE_KNOWN (decl1) = 1;
15271 DECL_DEFER_OUTPUT (decl1) = 1;
15273 else
15275 /* This is a definition, not a reference.
15276 So clear DECL_EXTERNAL, unless this is a GNU extern inline. */
15277 if (!GNU_INLINE_P (decl1))
15278 DECL_EXTERNAL (decl1) = 0;
15280 if ((DECL_DECLARED_INLINE_P (decl1)
15281 || DECL_TEMPLATE_INSTANTIATION (decl1))
15282 && ! DECL_INTERFACE_KNOWN (decl1))
15283 DECL_DEFER_OUTPUT (decl1) = 1;
15284 else
15285 DECL_INTERFACE_KNOWN (decl1) = 1;
15288 /* Determine the ELF visibility attribute for the function. We must not
15289 do this before calling "pushdecl", as we must allow "duplicate_decls"
15290 to merge any attributes appropriately. We also need to wait until
15291 linkage is set. */
15292 if (!DECL_CLONED_FUNCTION_P (decl1))
15293 determine_visibility (decl1);
15295 if (!processing_template_decl)
15296 maybe_instantiate_noexcept (decl1);
15298 begin_scope (sk_function_parms, decl1);
15300 ++function_depth;
15302 if (DECL_DESTRUCTOR_P (decl1)
15303 || (DECL_CONSTRUCTOR_P (decl1)
15304 && targetm.cxx.cdtor_returns_this ()))
15306 cdtor_label = create_artificial_label (input_location);
15307 LABEL_DECL_CDTOR (cdtor_label) = true;
15310 start_fname_decls ();
15312 store_parm_decls (current_function_parms);
15314 if (!processing_template_decl
15315 && (flag_lifetime_dse > 1)
15316 && DECL_CONSTRUCTOR_P (decl1)
15317 && !DECL_CLONED_FUNCTION_P (decl1)
15318 /* Clobbering an empty base is harmful if it overlays real data. */
15319 && !is_empty_class (current_class_type)
15320 /* We can't clobber safely for an implicitly-defined default constructor
15321 because part of the initialization might happen before we enter the
15322 constructor, via AGGR_INIT_ZERO_FIRST (c++/68006). */
15323 && !implicit_default_ctor_p (decl1))
15324 finish_expr_stmt (build_clobber_this ());
15326 if (!processing_template_decl
15327 && DECL_CONSTRUCTOR_P (decl1)
15328 && sanitize_flags_p (SANITIZE_VPTR)
15329 && !DECL_CLONED_FUNCTION_P (decl1)
15330 && !implicit_default_ctor_p (decl1))
15331 cp_ubsan_maybe_initialize_vtbl_ptrs (current_class_ptr);
15333 start_lambda_scope (decl1);
15335 return true;
15339 /* Like start_preparsed_function, except that instead of a
15340 FUNCTION_DECL, this function takes DECLSPECS and DECLARATOR.
15342 Returns true on success. If the DECLARATOR is not suitable
15343 for a function, we return false, which tells the parser to
15344 skip the entire function. */
15346 bool
15347 start_function (cp_decl_specifier_seq *declspecs,
15348 const cp_declarator *declarator,
15349 tree attrs)
15351 tree decl1;
15353 decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, 1, &attrs);
15354 invoke_plugin_callbacks (PLUGIN_START_PARSE_FUNCTION, decl1);
15355 if (decl1 == error_mark_node)
15356 return false;
15357 /* If the declarator is not suitable for a function definition,
15358 cause a syntax error. */
15359 if (decl1 == NULL_TREE || TREE_CODE (decl1) != FUNCTION_DECL)
15361 error ("invalid function declaration");
15362 return false;
15365 if (DECL_MAIN_P (decl1))
15366 /* main must return int. grokfndecl should have corrected it
15367 (and issued a diagnostic) if the user got it wrong. */
15368 gcc_assert (same_type_p (TREE_TYPE (TREE_TYPE (decl1)),
15369 integer_type_node));
15371 return start_preparsed_function (decl1, attrs, /*flags=*/SF_DEFAULT);
15374 /* Returns true iff an EH_SPEC_BLOCK should be created in the body of
15375 FN. */
15377 static bool
15378 use_eh_spec_block (tree fn)
15380 return (flag_exceptions && flag_enforce_eh_specs
15381 && !processing_template_decl
15382 && !type_throw_all_p (TREE_TYPE (fn))
15383 /* We insert the EH_SPEC_BLOCK only in the original
15384 function; then, it is copied automatically to the
15385 clones. */
15386 && !DECL_CLONED_FUNCTION_P (fn)
15387 /* Implicitly-generated constructors and destructors have
15388 exception specifications. However, those specifications
15389 are the union of the possible exceptions specified by the
15390 constructors/destructors for bases and members, so no
15391 unallowed exception will ever reach this function. By
15392 not creating the EH_SPEC_BLOCK we save a little memory,
15393 and we avoid spurious warnings about unreachable
15394 code. */
15395 && !DECL_DEFAULTED_FN (fn));
15398 /* Store the parameter declarations into the current function declaration.
15399 This is called after parsing the parameter declarations, before
15400 digesting the body of the function.
15402 Also install to binding contour return value identifier, if any. */
15404 static void
15405 store_parm_decls (tree current_function_parms)
15407 tree fndecl = current_function_decl;
15408 tree parm;
15410 /* This is a chain of any other decls that came in among the parm
15411 declarations. If a parm is declared with enum {foo, bar} x;
15412 then CONST_DECLs for foo and bar are put here. */
15413 tree nonparms = NULL_TREE;
15415 if (current_function_parms)
15417 /* This case is when the function was defined with an ANSI prototype.
15418 The parms already have decls, so we need not do anything here
15419 except record them as in effect
15420 and complain if any redundant old-style parm decls were written. */
15422 tree specparms = current_function_parms;
15423 tree next;
15425 /* Must clear this because it might contain TYPE_DECLs declared
15426 at class level. */
15427 current_binding_level->names = NULL;
15429 /* If we're doing semantic analysis, then we'll call pushdecl
15430 for each of these. We must do them in reverse order so that
15431 they end in the correct forward order. */
15432 specparms = nreverse (specparms);
15434 for (parm = specparms; parm; parm = next)
15436 next = DECL_CHAIN (parm);
15437 if (TREE_CODE (parm) == PARM_DECL)
15438 pushdecl (parm);
15439 else
15441 /* If we find an enum constant or a type tag,
15442 put it aside for the moment. */
15443 TREE_CHAIN (parm) = NULL_TREE;
15444 nonparms = chainon (nonparms, parm);
15448 /* Get the decls in their original chain order and record in the
15449 function. This is all and only the PARM_DECLs that were
15450 pushed into scope by the loop above. */
15451 DECL_ARGUMENTS (fndecl) = get_local_decls ();
15453 else
15454 DECL_ARGUMENTS (fndecl) = NULL_TREE;
15456 /* Now store the final chain of decls for the arguments
15457 as the decl-chain of the current lexical scope.
15458 Put the enumerators in as well, at the front so that
15459 DECL_ARGUMENTS is not modified. */
15460 current_binding_level->names = chainon (nonparms, DECL_ARGUMENTS (fndecl));
15462 if (use_eh_spec_block (current_function_decl))
15463 current_eh_spec_block = begin_eh_spec_block ();
15467 /* We have finished doing semantic analysis on DECL, but have not yet
15468 generated RTL for its body. Save away our current state, so that
15469 when we want to generate RTL later we know what to do. */
15471 static void
15472 save_function_data (tree decl)
15474 struct language_function *f;
15476 /* Save the language-specific per-function data so that we can
15477 get it back when we really expand this function. */
15478 gcc_assert (!DECL_PENDING_INLINE_P (decl));
15480 /* Make a copy. */
15481 f = ggc_alloc<language_function> ();
15482 memcpy (f, cp_function_chain, sizeof (struct language_function));
15483 DECL_SAVED_FUNCTION_DATA (decl) = f;
15485 /* Clear out the bits we don't need. */
15486 f->base.x_stmt_tree.x_cur_stmt_list = NULL;
15487 f->bindings = NULL;
15488 f->x_local_names = NULL;
15489 f->base.local_typedefs = NULL;
15493 /* Set the return value of the constructor (if present). */
15495 static void
15496 finish_constructor_body (void)
15498 tree val;
15499 tree exprstmt;
15501 if (targetm.cxx.cdtor_returns_this ())
15503 /* Any return from a constructor will end up here. */
15504 add_stmt (build_stmt (input_location, LABEL_EXPR, cdtor_label));
15506 val = DECL_ARGUMENTS (current_function_decl);
15507 val = build2 (MODIFY_EXPR, TREE_TYPE (val),
15508 DECL_RESULT (current_function_decl), val);
15509 /* Return the address of the object. */
15510 exprstmt = build_stmt (input_location, RETURN_EXPR, val);
15511 add_stmt (exprstmt);
15515 /* Do all the processing for the beginning of a destructor; set up the
15516 vtable pointers and cleanups for bases and members. */
15518 static void
15519 begin_destructor_body (void)
15521 tree compound_stmt;
15523 /* If the CURRENT_CLASS_TYPE is incomplete, we will have already
15524 issued an error message. We still want to try to process the
15525 body of the function, but initialize_vtbl_ptrs will crash if
15526 TYPE_BINFO is NULL. */
15527 if (COMPLETE_TYPE_P (current_class_type))
15529 compound_stmt = begin_compound_stmt (0);
15530 /* Make all virtual function table pointers in non-virtual base
15531 classes point to CURRENT_CLASS_TYPE's virtual function
15532 tables. */
15533 initialize_vtbl_ptrs (current_class_ptr);
15534 finish_compound_stmt (compound_stmt);
15536 if (flag_lifetime_dse
15537 /* Clobbering an empty base is harmful if it overlays real data. */
15538 && !is_empty_class (current_class_type))
15540 if (sanitize_flags_p (SANITIZE_VPTR)
15541 && (flag_sanitize_recover & SANITIZE_VPTR) == 0
15542 && TYPE_CONTAINS_VPTR_P (current_class_type))
15544 tree binfo = TYPE_BINFO (current_class_type);
15545 tree ref
15546 = cp_build_fold_indirect_ref (current_class_ptr);
15548 tree vtbl_ptr = build_vfield_ref (ref, TREE_TYPE (binfo));
15549 tree vtbl = build_zero_cst (TREE_TYPE (vtbl_ptr));
15550 tree stmt = cp_build_modify_expr (input_location, vtbl_ptr,
15551 NOP_EXPR, vtbl,
15552 tf_warning_or_error);
15553 finish_decl_cleanup (NULL_TREE, stmt);
15555 else
15556 finish_decl_cleanup (NULL_TREE, build_clobber_this ());
15559 /* And insert cleanups for our bases and members so that they
15560 will be properly destroyed if we throw. */
15561 push_base_cleanups ();
15565 /* At the end of every destructor we generate code to delete the object if
15566 necessary. Do that now. */
15568 static void
15569 finish_destructor_body (void)
15571 tree exprstmt;
15573 /* Any return from a destructor will end up here; that way all base
15574 and member cleanups will be run when the function returns. */
15575 add_stmt (build_stmt (input_location, LABEL_EXPR, cdtor_label));
15577 if (targetm.cxx.cdtor_returns_this ())
15579 tree val;
15581 val = DECL_ARGUMENTS (current_function_decl);
15582 val = build2 (MODIFY_EXPR, TREE_TYPE (val),
15583 DECL_RESULT (current_function_decl), val);
15584 /* Return the address of the object. */
15585 exprstmt = build_stmt (input_location, RETURN_EXPR, val);
15586 add_stmt (exprstmt);
15590 /* Do the necessary processing for the beginning of a function body, which
15591 in this case includes member-initializers, but not the catch clauses of
15592 a function-try-block. Currently, this means opening a binding level
15593 for the member-initializers (in a ctor), member cleanups (in a dtor),
15594 and capture proxies (in a lambda operator()). */
15596 tree
15597 begin_function_body (void)
15599 tree stmt;
15601 if (! FUNCTION_NEEDS_BODY_BLOCK (current_function_decl))
15602 return NULL_TREE;
15604 if (processing_template_decl)
15605 /* Do nothing now. */;
15606 else
15607 /* Always keep the BLOCK node associated with the outermost pair of
15608 curly braces of a function. These are needed for correct
15609 operation of dwarfout.c. */
15610 keep_next_level (true);
15612 stmt = begin_compound_stmt (BCS_FN_BODY);
15614 if (processing_template_decl)
15615 /* Do nothing now. */;
15616 else if (DECL_DESTRUCTOR_P (current_function_decl))
15617 begin_destructor_body ();
15619 return stmt;
15622 /* Do the processing for the end of a function body. Currently, this means
15623 closing out the cleanups for fully-constructed bases and members, and in
15624 the case of the destructor, deleting the object if desired. Again, this
15625 is only meaningful for [cd]tors, since they are the only functions where
15626 there is a significant distinction between the main body and any
15627 function catch clauses. Handling, say, main() return semantics here
15628 would be wrong, as flowing off the end of a function catch clause for
15629 main() would also need to return 0. */
15631 void
15632 finish_function_body (tree compstmt)
15634 if (compstmt == NULL_TREE)
15635 return;
15637 /* Close the block. */
15638 finish_compound_stmt (compstmt);
15640 if (processing_template_decl)
15641 /* Do nothing now. */;
15642 else if (DECL_CONSTRUCTOR_P (current_function_decl))
15643 finish_constructor_body ();
15644 else if (DECL_DESTRUCTOR_P (current_function_decl))
15645 finish_destructor_body ();
15648 /* Given a function, returns the BLOCK corresponding to the outermost level
15649 of curly braces, skipping the artificial block created for constructor
15650 initializers. */
15652 tree
15653 outer_curly_brace_block (tree fndecl)
15655 tree block = DECL_INITIAL (fndecl);
15656 if (BLOCK_OUTER_CURLY_BRACE_P (block))
15657 return block;
15658 block = BLOCK_SUBBLOCKS (block);
15659 if (BLOCK_OUTER_CURLY_BRACE_P (block))
15660 return block;
15661 block = BLOCK_SUBBLOCKS (block);
15662 gcc_assert (BLOCK_OUTER_CURLY_BRACE_P (block));
15663 return block;
15666 /* If FNDECL is a class's key method, add the class to the list of
15667 keyed classes that should be emitted. */
15669 static void
15670 record_key_method_defined (tree fndecl)
15672 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fndecl)
15673 && DECL_VIRTUAL_P (fndecl)
15674 && !processing_template_decl)
15676 tree fnclass = DECL_CONTEXT (fndecl);
15677 if (fndecl == CLASSTYPE_KEY_METHOD (fnclass))
15678 vec_safe_push (keyed_classes, fnclass);
15682 /* Subroutine of finish_function.
15683 Save the body of constexpr functions for possible
15684 future compile time evaluation. */
15686 static void
15687 maybe_save_function_definition (tree fun)
15689 if (!processing_template_decl
15690 && DECL_DECLARED_CONSTEXPR_P (fun)
15691 && !cp_function_chain->invalid_constexpr
15692 && !DECL_CLONED_FUNCTION_P (fun))
15693 register_constexpr_fundef (fun, DECL_SAVED_TREE (fun));
15696 /* Finish up a function declaration and compile that function
15697 all the way to assembler language output. The free the storage
15698 for the function definition. INLINE_P is TRUE if we just
15699 finished processing the body of an in-class inline function
15700 definition. (This processing will have taken place after the
15701 class definition is complete.) */
15703 tree
15704 finish_function (bool inline_p)
15706 tree fndecl = current_function_decl;
15707 tree fntype, ctype = NULL_TREE;
15709 /* When we get some parse errors, we can end up without a
15710 current_function_decl, so cope. */
15711 if (fndecl == NULL_TREE)
15712 return error_mark_node;
15714 finish_lambda_scope ();
15716 if (c_dialect_objc ())
15717 objc_finish_function ();
15719 record_key_method_defined (fndecl);
15721 fntype = TREE_TYPE (fndecl);
15723 /* TREE_READONLY (fndecl) = 1;
15724 This caused &foo to be of type ptr-to-const-function
15725 which then got a warning when stored in a ptr-to-function variable. */
15727 gcc_assert (building_stmt_list_p ());
15728 /* The current function is being defined, so its DECL_INITIAL should
15729 be set, and unless there's a multiple definition, it should be
15730 error_mark_node. */
15731 gcc_assert (DECL_INITIAL (fndecl) == error_mark_node);
15733 /* For a cloned function, we've already got all the code we need;
15734 there's no need to add any extra bits. */
15735 if (!DECL_CLONED_FUNCTION_P (fndecl))
15737 /* Make it so that `main' always returns 0 by default. */
15738 if (DECL_MAIN_P (current_function_decl))
15739 finish_return_stmt (integer_zero_node);
15741 if (use_eh_spec_block (current_function_decl))
15742 finish_eh_spec_block (TYPE_RAISES_EXCEPTIONS
15743 (TREE_TYPE (current_function_decl)),
15744 current_eh_spec_block);
15747 /* If we're saving up tree structure, tie off the function now. */
15748 DECL_SAVED_TREE (fndecl) = pop_stmt_list (DECL_SAVED_TREE (fndecl));
15750 finish_fname_decls ();
15752 /* If this function can't throw any exceptions, remember that. */
15753 if (!processing_template_decl
15754 && !cp_function_chain->can_throw
15755 && !flag_non_call_exceptions
15756 && !decl_replaceable_p (fndecl))
15757 TREE_NOTHROW (fndecl) = 1;
15759 /* This must come after expand_function_end because cleanups might
15760 have declarations (from inline functions) that need to go into
15761 this function's blocks. */
15763 /* If the current binding level isn't the outermost binding level
15764 for this function, either there is a bug, or we have experienced
15765 syntax errors and the statement tree is malformed. */
15766 if (current_binding_level->kind != sk_function_parms)
15768 /* Make sure we have already experienced errors. */
15769 gcc_assert (errorcount);
15771 /* Throw away the broken statement tree and extra binding
15772 levels. */
15773 DECL_SAVED_TREE (fndecl) = alloc_stmt_list ();
15775 while (current_binding_level->kind != sk_function_parms)
15777 if (current_binding_level->kind == sk_class)
15778 pop_nested_class ();
15779 else
15780 poplevel (0, 0, 0);
15783 poplevel (1, 0, 1);
15785 /* Statements should always be full-expressions at the outermost set
15786 of curly braces for a function. */
15787 gcc_assert (stmts_are_full_exprs_p ());
15789 /* If there are no return statements in a function with auto return type,
15790 the return type is void. But if the declared type is something like
15791 auto*, this is an error. */
15792 if (!processing_template_decl && FNDECL_USED_AUTO (fndecl)
15793 && TREE_TYPE (fntype) == current_function_auto_return_pattern)
15795 if (is_auto (current_function_auto_return_pattern))
15797 apply_deduced_return_type (fndecl, void_type_node);
15798 fntype = TREE_TYPE (fndecl);
15800 else if (!current_function_returns_value
15801 && !current_function_returns_null)
15803 error ("no return statements in function returning %qT",
15804 current_function_auto_return_pattern);
15805 inform (input_location, "only plain %<auto%> return type can be "
15806 "deduced to %<void%>");
15810 // If this is a concept, check that the definition is reasonable.
15811 if (DECL_DECLARED_CONCEPT_P (fndecl))
15812 check_function_concept (fndecl);
15814 /* Lambda closure members are implicitly constexpr if possible. */
15815 if (cxx_dialect >= cxx17
15816 && LAMBDA_TYPE_P (CP_DECL_CONTEXT (fndecl)))
15817 DECL_DECLARED_CONSTEXPR_P (fndecl)
15818 = ((processing_template_decl
15819 || is_valid_constexpr_fn (fndecl, /*complain*/false))
15820 && potential_constant_expression (DECL_SAVED_TREE (fndecl)));
15822 /* Save constexpr function body before it gets munged by
15823 the NRV transformation. */
15824 maybe_save_function_definition (fndecl);
15826 /* Invoke the pre-genericize plugin before we start munging things. */
15827 if (!processing_template_decl)
15828 invoke_plugin_callbacks (PLUGIN_PRE_GENERICIZE, fndecl);
15830 /* Perform delayed folding before NRV transformation. */
15831 if (!processing_template_decl)
15832 cp_fold_function (fndecl);
15834 /* Set up the named return value optimization, if we can. Candidate
15835 variables are selected in check_return_expr. */
15836 if (current_function_return_value)
15838 tree r = current_function_return_value;
15839 tree outer;
15841 if (r != error_mark_node
15842 /* This is only worth doing for fns that return in memory--and
15843 simpler, since we don't have to worry about promoted modes. */
15844 && aggregate_value_p (TREE_TYPE (TREE_TYPE (fndecl)), fndecl)
15845 /* Only allow this for variables declared in the outer scope of
15846 the function so we know that their lifetime always ends with a
15847 return; see g++.dg/opt/nrv6.C. We could be more flexible if
15848 we were to do this optimization in tree-ssa. */
15849 && (outer = outer_curly_brace_block (fndecl))
15850 && chain_member (r, BLOCK_VARS (outer)))
15851 finalize_nrv (&DECL_SAVED_TREE (fndecl), r, DECL_RESULT (fndecl));
15853 current_function_return_value = NULL_TREE;
15856 /* Remember that we were in class scope. */
15857 if (current_class_name)
15858 ctype = current_class_type;
15860 /* Must mark the RESULT_DECL as being in this function. */
15861 DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
15863 /* Set the BLOCK_SUPERCONTEXT of the outermost function scope to point
15864 to the FUNCTION_DECL node itself. */
15865 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
15867 /* Save away current state, if appropriate. */
15868 if (!processing_template_decl)
15869 save_function_data (fndecl);
15871 /* Complain if there's just no return statement. */
15872 if (warn_return_type
15873 && !VOID_TYPE_P (TREE_TYPE (fntype))
15874 && !dependent_type_p (TREE_TYPE (fntype))
15875 && !current_function_returns_value && !current_function_returns_null
15876 /* Don't complain if we abort or throw. */
15877 && !current_function_returns_abnormally
15878 /* Don't complain if there's an infinite loop. */
15879 && !current_function_infinite_loop
15880 /* Don't complain if we are declared noreturn. */
15881 && !TREE_THIS_VOLATILE (fndecl)
15882 && !DECL_NAME (DECL_RESULT (fndecl))
15883 && !TREE_NO_WARNING (fndecl)
15884 /* Structor return values (if any) are set by the compiler. */
15885 && !DECL_CONSTRUCTOR_P (fndecl)
15886 && !DECL_DESTRUCTOR_P (fndecl)
15887 && targetm.warn_func_return (fndecl))
15889 warning (OPT_Wreturn_type,
15890 "no return statement in function returning non-void");
15891 TREE_NO_WARNING (fndecl) = 1;
15894 /* Store the end of the function, so that we get good line number
15895 info for the epilogue. */
15896 cfun->function_end_locus = input_location;
15898 /* Complain about parameters that are only set, but never otherwise used. */
15899 if (warn_unused_but_set_parameter
15900 && !processing_template_decl
15901 && errorcount == unused_but_set_errorcount
15902 && !DECL_CLONED_FUNCTION_P (fndecl))
15904 tree decl;
15906 for (decl = DECL_ARGUMENTS (fndecl);
15907 decl;
15908 decl = DECL_CHAIN (decl))
15909 if (TREE_USED (decl)
15910 && TREE_CODE (decl) == PARM_DECL
15911 && !DECL_READ_P (decl)
15912 && DECL_NAME (decl)
15913 && !DECL_ARTIFICIAL (decl)
15914 && !TREE_NO_WARNING (decl)
15915 && !DECL_IN_SYSTEM_HEADER (decl)
15916 && TREE_TYPE (decl) != error_mark_node
15917 && !TYPE_REF_P (TREE_TYPE (decl))
15918 && (!CLASS_TYPE_P (TREE_TYPE (decl))
15919 || !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (decl))))
15920 warning_at (DECL_SOURCE_LOCATION (decl),
15921 OPT_Wunused_but_set_parameter,
15922 "parameter %qD set but not used", decl);
15923 unused_but_set_errorcount = errorcount;
15926 /* Complain about locally defined typedefs that are not used in this
15927 function. */
15928 maybe_warn_unused_local_typedefs ();
15930 /* Possibly warn about unused parameters. */
15931 if (warn_unused_parameter
15932 && !processing_template_decl
15933 && !DECL_CLONED_FUNCTION_P (fndecl))
15934 do_warn_unused_parameter (fndecl);
15936 /* Genericize before inlining. */
15937 if (!processing_template_decl)
15939 struct language_function *f = DECL_SAVED_FUNCTION_DATA (fndecl);
15940 cp_genericize (fndecl);
15941 /* Clear out the bits we don't need. */
15942 f->x_current_class_ptr = NULL;
15943 f->x_current_class_ref = NULL;
15944 f->x_eh_spec_block = NULL;
15945 f->x_in_charge_parm = NULL;
15946 f->x_vtt_parm = NULL;
15947 f->x_return_value = NULL;
15948 f->bindings = NULL;
15949 f->extern_decl_map = NULL;
15950 f->infinite_loops = NULL;
15952 /* Clear out the bits we don't need. */
15953 local_names = NULL;
15955 /* We're leaving the context of this function, so zap cfun. It's still in
15956 DECL_STRUCT_FUNCTION, and we'll restore it in tree_rest_of_compilation. */
15957 set_cfun (NULL);
15958 current_function_decl = NULL;
15960 /* If this is an in-class inline definition, we may have to pop the
15961 bindings for the template parameters that we added in
15962 maybe_begin_member_template_processing when start_function was
15963 called. */
15964 if (inline_p)
15965 maybe_end_member_template_processing ();
15967 /* Leave the scope of the class. */
15968 if (ctype)
15969 pop_nested_class ();
15971 --function_depth;
15973 /* Clean up. */
15974 current_function_decl = NULL_TREE;
15976 invoke_plugin_callbacks (PLUGIN_FINISH_PARSE_FUNCTION, fndecl);
15977 return fndecl;
15980 /* Create the FUNCTION_DECL for a function definition.
15981 DECLSPECS and DECLARATOR are the parts of the declaration;
15982 they describe the return type and the name of the function,
15983 but twisted together in a fashion that parallels the syntax of C.
15985 This function creates a binding context for the function body
15986 as well as setting up the FUNCTION_DECL in current_function_decl.
15988 Returns a FUNCTION_DECL on success.
15990 If the DECLARATOR is not suitable for a function (it defines a datum
15991 instead), we return 0, which tells yyparse to report a parse error.
15993 May return void_type_node indicating that this method is actually
15994 a friend. See grokfield for more details.
15996 Came here with a `.pushlevel' .
15998 DO NOT MAKE ANY CHANGES TO THIS CODE WITHOUT MAKING CORRESPONDING
15999 CHANGES TO CODE IN `grokfield'. */
16001 tree
16002 grokmethod (cp_decl_specifier_seq *declspecs,
16003 const cp_declarator *declarator, tree attrlist)
16005 tree fndecl = grokdeclarator (declarator, declspecs, MEMFUNCDEF, 0,
16006 &attrlist);
16008 if (fndecl == error_mark_node)
16009 return error_mark_node;
16011 if (fndecl == NULL || TREE_CODE (fndecl) != FUNCTION_DECL)
16013 error ("invalid member function declaration");
16014 return error_mark_node;
16017 if (attrlist)
16018 cplus_decl_attributes (&fndecl, attrlist, 0);
16020 /* Pass friends other than inline friend functions back. */
16021 if (fndecl == void_type_node)
16022 return fndecl;
16024 if (DECL_IN_AGGR_P (fndecl))
16026 if (DECL_CLASS_SCOPE_P (fndecl))
16027 error ("%qD is already defined in class %qT", fndecl,
16028 DECL_CONTEXT (fndecl));
16029 return error_mark_node;
16032 check_template_shadow (fndecl);
16034 if (TREE_PUBLIC (fndecl))
16035 DECL_COMDAT (fndecl) = 1;
16036 DECL_DECLARED_INLINE_P (fndecl) = 1;
16037 DECL_NO_INLINE_WARNING_P (fndecl) = 1;
16039 /* We process method specializations in finish_struct_1. */
16040 if (processing_template_decl && !DECL_TEMPLATE_SPECIALIZATION (fndecl))
16042 fndecl = push_template_decl (fndecl);
16043 if (fndecl == error_mark_node)
16044 return fndecl;
16047 if (! DECL_FRIEND_P (fndecl))
16049 if (DECL_CHAIN (fndecl))
16051 fndecl = copy_node (fndecl);
16052 TREE_CHAIN (fndecl) = NULL_TREE;
16056 cp_finish_decl (fndecl, NULL_TREE, false, NULL_TREE, 0);
16058 DECL_IN_AGGR_P (fndecl) = 1;
16059 return fndecl;
16063 /* VAR is a VAR_DECL. If its type is incomplete, remember VAR so that
16064 we can lay it out later, when and if its type becomes complete.
16066 Also handle constexpr variables where the initializer involves
16067 an unlowered PTRMEM_CST because the class isn't complete yet. */
16069 void
16070 maybe_register_incomplete_var (tree var)
16072 gcc_assert (VAR_P (var));
16074 /* Keep track of variables with incomplete types. */
16075 if (!processing_template_decl && TREE_TYPE (var) != error_mark_node
16076 && DECL_EXTERNAL (var))
16078 tree inner_type = TREE_TYPE (var);
16080 while (TREE_CODE (inner_type) == ARRAY_TYPE)
16081 inner_type = TREE_TYPE (inner_type);
16082 inner_type = TYPE_MAIN_VARIANT (inner_type);
16084 if ((!COMPLETE_TYPE_P (inner_type) && CLASS_TYPE_P (inner_type))
16085 /* RTTI TD entries are created while defining the type_info. */
16086 || (TYPE_LANG_SPECIFIC (inner_type)
16087 && TYPE_BEING_DEFINED (inner_type)))
16089 incomplete_var iv = {var, inner_type};
16090 vec_safe_push (incomplete_vars, iv);
16092 else if (!(DECL_LANG_SPECIFIC (var) && DECL_TEMPLATE_INFO (var))
16093 && decl_constant_var_p (var)
16094 && (TYPE_PTRMEM_P (inner_type) || CLASS_TYPE_P (inner_type)))
16096 /* When the outermost open class is complete we can resolve any
16097 pointers-to-members. */
16098 tree context = outermost_open_class ();
16099 incomplete_var iv = {var, context};
16100 vec_safe_push (incomplete_vars, iv);
16105 /* Called when a class type (given by TYPE) is defined. If there are
16106 any existing VAR_DECLs whose type has been completed by this
16107 declaration, update them now. */
16109 void
16110 complete_vars (tree type)
16112 unsigned ix;
16113 incomplete_var *iv;
16115 for (ix = 0; vec_safe_iterate (incomplete_vars, ix, &iv); )
16117 if (same_type_p (type, iv->incomplete_type))
16119 tree var = iv->decl;
16120 tree type = TREE_TYPE (var);
16122 if (type != error_mark_node
16123 && (TYPE_MAIN_VARIANT (strip_array_types (type))
16124 == iv->incomplete_type))
16126 /* Complete the type of the variable. The VAR_DECL itself
16127 will be laid out in expand_expr. */
16128 complete_type (type);
16129 cp_apply_type_quals_to_decl (cp_type_quals (type), var);
16132 /* Remove this entry from the list. */
16133 incomplete_vars->unordered_remove (ix);
16135 else
16136 ix++;
16139 /* Check for pending declarations which may have abstract type. */
16140 complete_type_check_abstract (type);
16143 /* If DECL is of a type which needs a cleanup, build and return an
16144 expression to perform that cleanup here. Return NULL_TREE if no
16145 cleanup need be done. DECL can also be a _REF when called from
16146 split_nonconstant_init_1. */
16148 tree
16149 cxx_maybe_build_cleanup (tree decl, tsubst_flags_t complain)
16151 tree type;
16152 tree attr;
16153 tree cleanup;
16155 /* Assume no cleanup is required. */
16156 cleanup = NULL_TREE;
16158 if (error_operand_p (decl))
16159 return cleanup;
16161 /* Handle "__attribute__((cleanup))". We run the cleanup function
16162 before the destructor since the destructor is what actually
16163 terminates the lifetime of the object. */
16164 if (DECL_P (decl))
16165 attr = lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl));
16166 else
16167 attr = NULL_TREE;
16168 if (attr)
16170 tree id;
16171 tree fn;
16172 tree arg;
16174 /* Get the name specified by the user for the cleanup function. */
16175 id = TREE_VALUE (TREE_VALUE (attr));
16176 /* Look up the name to find the cleanup function to call. It is
16177 important to use lookup_name here because that is what is
16178 used in c-common.c:handle_cleanup_attribute when performing
16179 initial checks on the attribute. Note that those checks
16180 include ensuring that the function found is not an overloaded
16181 function, or an object with an overloaded call operator,
16182 etc.; we can rely on the fact that the function found is an
16183 ordinary FUNCTION_DECL. */
16184 fn = lookup_name (id);
16185 arg = build_address (decl);
16186 if (!mark_used (decl, complain) && !(complain & tf_error))
16187 return error_mark_node;
16188 cleanup = cp_build_function_call_nary (fn, complain, arg, NULL_TREE);
16189 if (cleanup == error_mark_node)
16190 return error_mark_node;
16192 /* Handle ordinary C++ destructors. */
16193 type = TREE_TYPE (decl);
16194 if (type_build_dtor_call (type))
16196 int flags = LOOKUP_NORMAL|LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR;
16197 tree addr;
16198 tree call;
16200 if (TREE_CODE (type) == ARRAY_TYPE)
16201 addr = decl;
16202 else
16203 addr = build_address (decl);
16205 call = build_delete (TREE_TYPE (addr), addr,
16206 sfk_complete_destructor, flags, 0, complain);
16207 if (call == error_mark_node)
16208 cleanup = error_mark_node;
16209 else if (TYPE_HAS_TRIVIAL_DESTRUCTOR (type))
16210 /* Discard the call. */;
16211 else if (cleanup)
16212 cleanup = cp_build_compound_expr (cleanup, call, complain);
16213 else
16214 cleanup = call;
16217 /* build_delete sets the location of the destructor call to the
16218 current location, even though the destructor is going to be
16219 called later, at the end of the current scope. This can lead to
16220 a "jumpy" behavior for users of debuggers when they step around
16221 the end of the block. So let's unset the location of the
16222 destructor call instead. */
16223 protected_set_expr_location (cleanup, UNKNOWN_LOCATION);
16225 if (cleanup
16226 && DECL_P (decl)
16227 && !lookup_attribute ("warn_unused", TYPE_ATTRIBUTES (TREE_TYPE (decl)))
16228 /* Treat objects with destructors as used; the destructor may do
16229 something substantive. */
16230 && !mark_used (decl, complain) && !(complain & tf_error))
16231 return error_mark_node;
16233 return cleanup;
16237 /* Return the FUNCTION_TYPE that corresponds to MEMFNTYPE, which can be a
16238 FUNCTION_DECL, METHOD_TYPE, FUNCTION_TYPE, pointer or reference to
16239 METHOD_TYPE or FUNCTION_TYPE, or pointer to member function. */
16241 tree
16242 static_fn_type (tree memfntype)
16244 tree fntype;
16245 tree args;
16247 if (TYPE_PTRMEMFUNC_P (memfntype))
16248 memfntype = TYPE_PTRMEMFUNC_FN_TYPE (memfntype);
16249 if (INDIRECT_TYPE_P (memfntype)
16250 || TREE_CODE (memfntype) == FUNCTION_DECL)
16251 memfntype = TREE_TYPE (memfntype);
16252 if (TREE_CODE (memfntype) == FUNCTION_TYPE)
16253 return memfntype;
16254 gcc_assert (TREE_CODE (memfntype) == METHOD_TYPE);
16255 args = TYPE_ARG_TYPES (memfntype);
16256 fntype = build_function_type (TREE_TYPE (memfntype), TREE_CHAIN (args));
16257 fntype = apply_memfn_quals (fntype, type_memfn_quals (memfntype));
16258 fntype = (cp_build_type_attribute_variant
16259 (fntype, TYPE_ATTRIBUTES (memfntype)));
16260 fntype = cxx_copy_lang_qualifiers (fntype, memfntype);
16261 return fntype;
16264 /* DECL was originally constructed as a non-static member function,
16265 but turned out to be static. Update it accordingly. */
16267 void
16268 revert_static_member_fn (tree decl)
16270 tree stype = static_fn_type (decl);
16271 cp_cv_quals quals = type_memfn_quals (stype);
16272 cp_ref_qualifier rqual = type_memfn_rqual (stype);
16274 if (quals != TYPE_UNQUALIFIED || rqual != REF_QUAL_NONE)
16275 stype = apply_memfn_quals (stype, TYPE_UNQUALIFIED, REF_QUAL_NONE);
16277 TREE_TYPE (decl) = stype;
16279 if (DECL_ARGUMENTS (decl))
16280 DECL_ARGUMENTS (decl) = DECL_CHAIN (DECL_ARGUMENTS (decl));
16281 DECL_STATIC_FUNCTION_P (decl) = 1;
16284 /* Return which tree structure is used by T, or TS_CP_GENERIC if T is
16285 one of the language-independent trees. */
16287 enum cp_tree_node_structure_enum
16288 cp_tree_node_structure (union lang_tree_node * t)
16290 switch (TREE_CODE (&t->generic))
16292 case DEFAULT_ARG: return TS_CP_DEFAULT_ARG;
16293 case DEFERRED_NOEXCEPT: return TS_CP_DEFERRED_NOEXCEPT;
16294 case IDENTIFIER_NODE: return TS_CP_IDENTIFIER;
16295 case OVERLOAD: return TS_CP_OVERLOAD;
16296 case TEMPLATE_PARM_INDEX: return TS_CP_TPI;
16297 case PTRMEM_CST: return TS_CP_PTRMEM;
16298 case BASELINK: return TS_CP_BASELINK;
16299 case TEMPLATE_DECL: return TS_CP_TEMPLATE_DECL;
16300 case STATIC_ASSERT: return TS_CP_STATIC_ASSERT;
16301 case ARGUMENT_PACK_SELECT: return TS_CP_ARGUMENT_PACK_SELECT;
16302 case TRAIT_EXPR: return TS_CP_TRAIT_EXPR;
16303 case LAMBDA_EXPR: return TS_CP_LAMBDA_EXPR;
16304 case TEMPLATE_INFO: return TS_CP_TEMPLATE_INFO;
16305 case CONSTRAINT_INFO: return TS_CP_CONSTRAINT_INFO;
16306 case USERDEF_LITERAL: return TS_CP_USERDEF_LITERAL;
16307 default: return TS_CP_GENERIC;
16311 /* Build the void_list_node (void_type_node having been created). */
16312 tree
16313 build_void_list_node (void)
16315 tree t = build_tree_list (NULL_TREE, void_type_node);
16316 return t;
16319 bool
16320 cp_missing_noreturn_ok_p (tree decl)
16322 /* A missing noreturn is ok for the `main' function. */
16323 return DECL_MAIN_P (decl);
16326 /* Return the decl used to identify the COMDAT group into which DECL should
16327 be placed. */
16329 tree
16330 cxx_comdat_group (tree decl)
16332 /* Virtual tables, construction virtual tables, and virtual table
16333 tables all go in a single COMDAT group, named after the primary
16334 virtual table. */
16335 if (VAR_P (decl) && DECL_VTABLE_OR_VTT_P (decl))
16336 decl = CLASSTYPE_VTABLES (DECL_CONTEXT (decl));
16337 /* For all other DECLs, the COMDAT group is the mangled name of the
16338 declaration itself. */
16339 else
16341 while (DECL_THUNK_P (decl))
16343 /* If TARGET_USE_LOCAL_THUNK_ALIAS_P, use_thunk puts the thunk
16344 into the same section as the target function. In that case
16345 we must return target's name. */
16346 tree target = THUNK_TARGET (decl);
16347 if (TARGET_USE_LOCAL_THUNK_ALIAS_P (target)
16348 && DECL_SECTION_NAME (target) != NULL
16349 && DECL_ONE_ONLY (target))
16350 decl = target;
16351 else
16352 break;
16356 return decl;
16359 /* Returns the return type for FN as written by the user, which may include
16360 a placeholder for a deduced return type. */
16362 tree
16363 fndecl_declared_return_type (tree fn)
16365 fn = STRIP_TEMPLATE (fn);
16366 if (FNDECL_USED_AUTO (fn))
16368 struct language_function *f = NULL;
16369 if (DECL_STRUCT_FUNCTION (fn))
16370 f = DECL_STRUCT_FUNCTION (fn)->language;
16371 if (f == NULL)
16372 f = DECL_SAVED_FUNCTION_DATA (fn);
16373 return f->x_auto_return_pattern;
16375 return TREE_TYPE (TREE_TYPE (fn));
16378 /* Returns true iff DECL is a variable or function declared with an auto type
16379 that has not yet been deduced to a real type. */
16381 bool
16382 undeduced_auto_decl (tree decl)
16384 if (cxx_dialect < cxx11)
16385 return false;
16386 return ((VAR_OR_FUNCTION_DECL_P (decl)
16387 || TREE_CODE (decl) == TEMPLATE_DECL)
16388 && type_uses_auto (TREE_TYPE (decl)));
16391 /* Complain if DECL has an undeduced return type. */
16393 bool
16394 require_deduced_type (tree decl, tsubst_flags_t complain)
16396 if (undeduced_auto_decl (decl))
16398 if (complain & tf_error)
16399 error ("use of %qD before deduction of %<auto%>", decl);
16400 return false;
16402 return true;
16405 #include "gt-cp-decl.h"