libcpp, c, middle-end: Optimize initializers using #embed in C
[official-gcc.git] / gcc / cp / decl2.cc
bloba0c319086d715a9bb5f80eba8fdcc0d92617e4f8
1 /* Process declarations and variables for C++ compiler.
2 Copyright (C) 1988-2024 Free Software Foundation, Inc.
3 Hacked 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 "memmodel.h"
33 #include "target.h"
34 #include "cp-tree.h"
35 #include "c-family/c-common.h"
36 #include "timevar.h"
37 #include "stringpool.h"
38 #include "cgraph.h"
39 #include "varasm.h"
40 #include "attribs.h"
41 #include "stor-layout.h"
42 #include "calls.h"
43 #include "decl.h"
44 #include "toplev.h"
45 #include "c-family/c-objc.h"
46 #include "c-family/c-pragma.h"
47 #include "dumpfile.h"
48 #include "intl.h"
49 #include "c-family/c-ada-spec.h"
50 #include "asan.h"
51 #include "optabs-query.h"
52 #include "omp-general.h"
53 #include "tree-inline.h"
54 #include "escaped_string.h"
56 /* Id for dumping the raw trees. */
57 int raw_dump_id;
59 extern cpp_reader *parse_in;
61 static tree start_objects (bool, unsigned, bool, bool);
62 static tree finish_objects (bool, unsigned, tree, bool = true);
63 static tree start_partial_init_fini_fn (bool, unsigned, unsigned, bool);
64 static void finish_partial_init_fini_fn (tree);
65 static tree emit_partial_init_fini_fn (bool, unsigned, tree,
66 unsigned, location_t, tree);
67 static void one_static_initialization_or_destruction (bool, tree, tree);
68 static void generate_ctor_or_dtor_function (bool, unsigned, tree, location_t,
69 bool);
70 static tree prune_vars_needing_no_initialization (tree *);
71 static void write_out_vars (tree);
72 static void import_export_class (tree);
73 static tree get_guard_bits (tree);
74 static void determine_visibility_from_class (tree, tree);
75 static bool determine_hidden_inline (tree);
77 /* A list of static class variables. This is needed, because a
78 static class variable can be declared inside the class without
79 an initializer, and then initialized, statically, outside the class. */
80 static GTY(()) vec<tree, va_gc> *pending_statics;
82 /* A list of functions which were declared inline, but which we
83 may need to emit outline anyway. */
84 static GTY(()) vec<tree, va_gc> *deferred_fns;
86 /* A list of decls that use types with no linkage, which we need to make
87 sure are defined. */
88 static GTY(()) vec<tree, va_gc> *no_linkage_decls;
90 /* A vector of alternating decls and identifiers, where the latter
91 is to be an alias for the former if the former is defined. */
92 static GTY(()) vec<tree, va_gc> *mangling_aliases;
94 /* hash traits for declarations. Hashes single decls via
95 DECL_ASSEMBLER_NAME_RAW. */
97 struct mangled_decl_hash : ggc_remove <tree>
99 typedef tree value_type; /* A DECL. */
100 typedef tree compare_type; /* An identifier. */
102 static hashval_t hash (const value_type decl)
104 return IDENTIFIER_HASH_VALUE (DECL_ASSEMBLER_NAME_RAW (decl));
106 static bool equal (const value_type existing, compare_type candidate)
108 tree name = DECL_ASSEMBLER_NAME_RAW (existing);
109 return candidate == name;
112 static const bool empty_zero_p = true;
113 static inline void mark_empty (value_type &p) {p = NULL_TREE;}
114 static inline bool is_empty (value_type p) {return !p;}
116 static bool is_deleted (value_type e)
118 return e == reinterpret_cast <value_type> (1);
120 static void mark_deleted (value_type &e)
122 e = reinterpret_cast <value_type> (1);
126 /* A hash table of decls keyed by mangled name. Used to figure out if
127 we need compatibility aliases. */
128 static GTY(()) hash_table<mangled_decl_hash> *mangled_decls;
130 // Hash table mapping priority to lists of variables or functions.
131 struct priority_map_traits
133 typedef unsigned key_type;
134 typedef tree value_type;
135 static const bool maybe_mx = true;
136 static hashval_t hash (key_type v)
138 return hashval_t (v);
140 static bool equal_keys (key_type k1, key_type k2)
142 return k1 == k2;
144 template <typename T> static void remove (T &)
147 // Zero is not a priority
148 static const bool empty_zero_p = true;
149 template <typename T> static bool is_empty (const T &entry)
151 return entry.m_key == 0;
153 template <typename T> static void mark_empty (T &entry)
155 entry.m_key = 0;
157 // Entries are not deleteable
158 template <typename T> static bool is_deleted (const T &)
160 return false;
162 template <typename T> static void mark_deleted (T &)
164 gcc_unreachable ();
168 typedef hash_map<unsigned/*Priority*/, tree/*List*/,
169 priority_map_traits> priority_map_t;
171 /* Two pairs of such hash tables, for the host and an OpenMP offload device.
172 Each pair has one priority map for fini and one for init. The fini tables
173 are only ever used when !cxa_atexit. */
174 static GTY(()) priority_map_t *static_init_fini_fns[4];
176 /* Nonzero if we're done parsing and into end-of-file activities.
177 2 if all templates have been instantiated.
178 3 if we're done with front-end processing. */
180 int at_eof;
182 /* True if note_mangling_alias should enqueue mangling aliases for
183 later generation, rather than emitting them right away. */
185 bool defer_mangling_aliases = true;
188 /* Return a member function type (a METHOD_TYPE), given FNTYPE (a
189 FUNCTION_TYPE), CTYPE (class type), and QUALS (the cv-qualifiers
190 that apply to the function). */
192 tree
193 build_memfn_type (tree fntype, tree ctype, cp_cv_quals quals,
194 cp_ref_qualifier rqual)
196 if (fntype == error_mark_node || ctype == error_mark_node)
197 return error_mark_node;
199 gcc_assert (FUNC_OR_METHOD_TYPE_P (fntype));
201 cp_cv_quals type_quals = quals & ~TYPE_QUAL_RESTRICT;
202 ctype = cp_build_qualified_type (ctype, type_quals);
204 tree newtype
205 = build_method_type_directly (ctype, TREE_TYPE (fntype),
206 (TREE_CODE (fntype) == METHOD_TYPE
207 ? TREE_CHAIN (TYPE_ARG_TYPES (fntype))
208 : TYPE_ARG_TYPES (fntype)));
209 if (tree attrs = TYPE_ATTRIBUTES (fntype))
210 newtype = cp_build_type_attribute_variant (newtype, attrs);
211 newtype = build_cp_fntype_variant (newtype, rqual,
212 TYPE_RAISES_EXCEPTIONS (fntype),
213 TYPE_HAS_LATE_RETURN_TYPE (fntype));
215 return newtype;
218 /* Return a variant of FNTYPE, a FUNCTION_TYPE or METHOD_TYPE, with its
219 return type changed to NEW_RET. */
221 tree
222 change_return_type (tree new_ret, tree fntype)
224 if (new_ret == error_mark_node)
225 return fntype;
227 if (same_type_p (new_ret, TREE_TYPE (fntype)))
228 return fntype;
230 tree newtype;
231 tree args = TYPE_ARG_TYPES (fntype);
233 if (TREE_CODE (fntype) == FUNCTION_TYPE)
235 newtype = build_function_type (new_ret, args);
236 newtype = apply_memfn_quals (newtype,
237 type_memfn_quals (fntype));
239 else
240 newtype = build_method_type_directly
241 (class_of_this_parm (fntype), new_ret, TREE_CHAIN (args));
243 if (tree attrs = TYPE_ATTRIBUTES (fntype))
244 newtype = cp_build_type_attribute_variant (newtype, attrs);
245 newtype = cxx_copy_lang_qualifiers (newtype, fntype);
247 return newtype;
250 /* Build a PARM_DECL of FN with NAME and TYPE, and set DECL_ARG_TYPE
251 appropriately. */
253 tree
254 cp_build_parm_decl (tree fn, tree name, tree type)
256 tree parm = build_decl (input_location,
257 PARM_DECL, name, type);
258 DECL_CONTEXT (parm) = fn;
260 /* DECL_ARG_TYPE is only used by the back end and the back end never
261 sees templates. */
262 if (!processing_template_decl)
263 DECL_ARG_TYPE (parm) = type_passed_as (type);
265 return parm;
268 /* Returns a PARM_DECL of FN for a parameter of the indicated TYPE, with the
269 indicated NAME. */
271 tree
272 build_artificial_parm (tree fn, tree name, tree type)
274 tree parm = cp_build_parm_decl (fn, name, type);
275 DECL_ARTIFICIAL (parm) = 1;
276 /* All our artificial parms are implicitly `const'; they cannot be
277 assigned to. */
278 TREE_READONLY (parm) = 1;
279 return parm;
282 /* 'structors for types with virtual baseclasses need an "in-charge" flag
283 saying whether this function is responsible for virtual baseclasses or not.
285 This function adds the "in-charge" flag to member function FN if
286 appropriate. It is called from grokclassfn and tsubst.
287 FN must be either a constructor or destructor.
289 The in-charge flag follows the 'this' parameter, and is followed by the
290 VTT parm (if any), then the user-written parms. */
292 void
293 maybe_retrofit_in_chrg (tree fn)
295 tree basetype, arg_types, parms, parm, fntype;
297 /* If we've already add the in-charge parameter don't do it again. */
298 if (DECL_HAS_IN_CHARGE_PARM_P (fn))
299 return;
301 /* When processing templates we can't know, in general, whether or
302 not we're going to have virtual baseclasses. */
303 if (processing_template_decl)
304 return;
306 /* We don't need an in-charge parameter for 'structors that don't
307 have virtual bases. */
308 if (!CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fn)))
309 return;
311 arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
312 basetype = TREE_TYPE (TREE_VALUE (arg_types));
313 arg_types = TREE_CHAIN (arg_types);
315 parms = DECL_CHAIN (DECL_ARGUMENTS (fn));
317 /* If this is a subobject constructor or destructor, our caller will
318 pass us a pointer to our VTT. */
319 if (CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fn)))
321 parm = build_artificial_parm (fn, vtt_parm_identifier, vtt_parm_type);
323 /* First add it to DECL_ARGUMENTS between 'this' and the real args... */
324 DECL_CHAIN (parm) = parms;
325 parms = parm;
327 /* ...and then to TYPE_ARG_TYPES. */
328 arg_types = hash_tree_chain (vtt_parm_type, arg_types);
330 DECL_HAS_VTT_PARM_P (fn) = 1;
333 /* Then add the in-charge parm (before the VTT parm). */
334 parm = build_artificial_parm (fn, in_charge_identifier, integer_type_node);
335 DECL_CHAIN (parm) = parms;
336 parms = parm;
337 arg_types = hash_tree_chain (integer_type_node, arg_types);
339 /* Insert our new parameter(s) into the list. */
340 DECL_CHAIN (DECL_ARGUMENTS (fn)) = parms;
342 /* And rebuild the function type. */
343 fntype = build_method_type_directly (basetype, TREE_TYPE (TREE_TYPE (fn)),
344 arg_types);
345 if (TYPE_ATTRIBUTES (TREE_TYPE (fn)))
346 fntype = (cp_build_type_attribute_variant
347 (fntype, TYPE_ATTRIBUTES (TREE_TYPE (fn))));
348 fntype = cxx_copy_lang_qualifiers (fntype, TREE_TYPE (fn));
349 TREE_TYPE (fn) = fntype;
351 /* Now we've got the in-charge parameter. */
352 DECL_HAS_IN_CHARGE_PARM_P (fn) = 1;
355 /* Classes overload their constituent function names automatically.
356 When a function name is declared in a record structure,
357 its name is changed to it overloaded name. Since names for
358 constructors and destructors can conflict, we place a leading
359 '$' for destructors.
361 CNAME is the name of the class we are grokking for.
363 FUNCTION is a FUNCTION_DECL. It was created by `grokdeclarator'.
365 FLAGS contains bits saying what's special about today's
366 arguments. DTOR_FLAG == DESTRUCTOR.
368 If FUNCTION is a destructor, then we must add the `auto-delete' field
369 as a second parameter. There is some hair associated with the fact
370 that we must "declare" this variable in the manner consistent with the
371 way the rest of the arguments were declared.
373 QUALS are the qualifiers for the this pointer. */
375 void
376 grokclassfn (tree ctype, tree function, enum overload_flags flags)
378 tree fn_name = DECL_NAME (function);
380 /* Even within an `extern "C"' block, members get C++ linkage. See
381 [dcl.link] for details. */
382 SET_DECL_LANGUAGE (function, lang_cplusplus);
384 if (fn_name == NULL_TREE)
386 error ("name missing for member function");
387 fn_name = get_identifier ("<anonymous>");
388 DECL_NAME (function) = fn_name;
391 DECL_CONTEXT (function) = ctype;
393 if (flags == DTOR_FLAG)
394 DECL_CXX_DESTRUCTOR_P (function) = 1;
396 if (flags == DTOR_FLAG || DECL_CONSTRUCTOR_P (function))
397 maybe_retrofit_in_chrg (function);
400 /* Create an ARRAY_REF, checking for the user doing things backwards
401 along the way.
402 If INDEX_EXP is non-NULL, then that is the index expression,
403 otherwise INDEX_EXP_LIST is the list of index expressions. */
405 tree
406 grok_array_decl (location_t loc, tree array_expr, tree index_exp,
407 vec<tree, va_gc> **index_exp_list, tsubst_flags_t complain)
409 tree type;
410 tree expr;
411 tree orig_array_expr = array_expr;
412 tree orig_index_exp = index_exp;
413 vec<tree, va_gc> *orig_index_exp_list
414 = index_exp_list ? *index_exp_list : NULL;
415 tree overload = NULL_TREE;
417 if (error_operand_p (array_expr) || error_operand_p (index_exp))
418 return error_mark_node;
420 if (processing_template_decl)
422 if (type_dependent_expression_p (array_expr)
423 || (index_exp ? type_dependent_expression_p (index_exp)
424 : any_type_dependent_arguments_p (*index_exp_list)))
426 if (index_exp == NULL)
427 index_exp = build_min_nt_call_vec (ovl_op_identifier (ARRAY_REF),
428 *index_exp_list);
429 return build_min_nt_loc (loc, ARRAY_REF, array_expr, index_exp,
430 NULL_TREE, NULL_TREE);
432 if (!index_exp)
433 orig_index_exp_list = make_tree_vector_copy (*index_exp_list);
436 type = TREE_TYPE (array_expr);
437 gcc_assert (type);
438 type = non_reference (type);
440 /* If they have an `operator[]', use that. */
441 if (MAYBE_CLASS_TYPE_P (type)
442 || (index_exp && MAYBE_CLASS_TYPE_P (TREE_TYPE (index_exp)))
443 || (index_exp == NULL_TREE
444 && !(*index_exp_list)->is_empty ()
445 && MAYBE_CLASS_TYPE_P (TREE_TYPE ((*index_exp_list)->last ()))))
447 if (index_exp)
448 expr = build_new_op (loc, ARRAY_REF, LOOKUP_NORMAL, array_expr,
449 index_exp, NULL_TREE, NULL_TREE,
450 &overload, complain);
451 else if ((*index_exp_list)->is_empty ())
452 expr = build_op_subscript (loc, array_expr, index_exp_list, &overload,
453 complain);
454 else
456 expr = build_op_subscript (loc, array_expr, index_exp_list,
457 &overload, complain & tf_decltype);
458 if (expr == error_mark_node
459 /* Don't do the backward compatibility fallback in a SFINAE
460 context. */
461 && (complain & tf_error))
463 tree idx = build_x_compound_expr_from_vec (*index_exp_list, NULL,
464 tf_none);
465 if (idx != error_mark_node)
466 expr = build_new_op (loc, ARRAY_REF, LOOKUP_NORMAL, array_expr,
467 idx, NULL_TREE, NULL_TREE, &overload,
468 complain & tf_decltype);
469 if (expr == error_mark_node)
471 overload = NULL_TREE;
472 expr = build_op_subscript (loc, array_expr, index_exp_list,
473 &overload, complain);
475 else
477 /* If it would be valid albeit deprecated expression in
478 C++20, just pedwarn on it and treat it as if wrapped
479 in (). */
480 pedwarn (loc, OPT_Wcomma_subscript,
481 "top-level comma expression in array subscript "
482 "changed meaning in C++23");
483 if (processing_template_decl)
485 orig_index_exp
486 = build_x_compound_expr_from_vec (orig_index_exp_list,
487 NULL, complain);
488 if (orig_index_exp == error_mark_node)
489 expr = error_mark_node;
490 release_tree_vector (orig_index_exp_list);
496 else
498 tree p1, p2, i1, i2;
499 bool swapped = false;
501 /* Otherwise, create an ARRAY_REF for a pointer or array type.
502 It is a little-known fact that, if `a' is an array and `i' is
503 an int, you can write `i[a]', which means the same thing as
504 `a[i]'. */
505 if (TREE_CODE (type) == ARRAY_TYPE || VECTOR_TYPE_P (type))
506 p1 = array_expr;
507 else
508 p1 = build_expr_type_conversion (WANT_POINTER, array_expr, false);
510 if (index_exp == NULL_TREE)
512 if (!(complain & tf_error))
513 /* Don't do the backward compatibility fallback in a SFINAE
514 context. */
515 return error_mark_node;
517 if ((*index_exp_list)->is_empty ())
519 error_at (loc, "built-in subscript operator without expression "
520 "list");
521 return error_mark_node;
523 tree idx = build_x_compound_expr_from_vec (*index_exp_list, NULL,
524 tf_none);
525 if (idx != error_mark_node)
526 /* If it would be valid albeit deprecated expression in C++20,
527 just pedwarn on it and treat it as if wrapped in (). */
528 pedwarn (loc, OPT_Wcomma_subscript,
529 "top-level comma expression in array subscript "
530 "changed meaning in C++23");
531 else
533 error_at (loc, "built-in subscript operator with more than one "
534 "expression in expression list");
535 return error_mark_node;
537 index_exp = idx;
538 if (processing_template_decl)
540 orig_index_exp
541 = build_x_compound_expr_from_vec (orig_index_exp_list,
542 NULL, complain);
543 release_tree_vector (orig_index_exp_list);
544 if (orig_index_exp == error_mark_node)
545 return error_mark_node;
549 if (TREE_CODE (TREE_TYPE (index_exp)) == ARRAY_TYPE)
550 p2 = index_exp;
551 else
552 p2 = build_expr_type_conversion (WANT_POINTER, index_exp, false);
554 i1 = build_expr_type_conversion (WANT_INT | WANT_ENUM, array_expr,
555 false);
556 i2 = build_expr_type_conversion (WANT_INT | WANT_ENUM, index_exp,
557 false);
559 if ((p1 && i2) && (i1 && p2))
560 error ("ambiguous conversion for array subscript");
562 if (p1 && i2)
563 array_expr = p1, index_exp = i2;
564 else if (i1 && p2)
565 swapped = true, array_expr = p2, index_exp = i1;
566 else
568 if (complain & tf_error)
569 error_at (loc, "invalid types %<%T[%T]%> for array subscript",
570 type, TREE_TYPE (index_exp));
571 return error_mark_node;
574 if (array_expr == error_mark_node || index_exp == error_mark_node)
575 error ("ambiguous conversion for array subscript");
577 if (TYPE_PTR_P (TREE_TYPE (array_expr)))
578 array_expr = mark_rvalue_use (array_expr);
579 else
580 array_expr = mark_lvalue_use_nonread (array_expr);
581 index_exp = mark_rvalue_use (index_exp);
582 if (swapped
583 && flag_strong_eval_order == 2
584 && (TREE_SIDE_EFFECTS (array_expr) || TREE_SIDE_EFFECTS (index_exp)))
585 expr = build_array_ref (input_location, index_exp, array_expr);
586 else
587 expr = build_array_ref (input_location, array_expr, index_exp);
589 if (processing_template_decl && expr != error_mark_node)
591 if (overload != NULL_TREE)
593 if (orig_index_exp == NULL_TREE)
595 expr = build_min_non_dep_op_overload (expr, overload,
596 orig_array_expr,
597 orig_index_exp_list);
598 release_tree_vector (orig_index_exp_list);
599 return expr;
601 return build_min_non_dep_op_overload (ARRAY_REF, expr, overload,
602 orig_array_expr,
603 orig_index_exp);
606 if (orig_index_exp == NULL_TREE)
608 orig_index_exp
609 = build_min_nt_call_vec (ovl_op_identifier (ARRAY_REF),
610 orig_index_exp_list);
611 release_tree_vector (orig_index_exp_list);
614 return build_min_non_dep (ARRAY_REF, expr, orig_array_expr,
615 orig_index_exp, NULL_TREE, NULL_TREE);
617 return expr;
620 /* Build an OMP_ARRAY_SECTION expression, handling usage in template
621 definitions, etc. */
623 tree
624 grok_omp_array_section (location_t loc, tree array_expr, tree index,
625 tree length)
627 tree orig_array_expr = array_expr;
628 tree orig_index = index;
629 tree orig_length = length;
631 if (error_operand_p (array_expr)
632 || error_operand_p (index)
633 || error_operand_p (length))
634 return error_mark_node;
636 if (processing_template_decl
637 && (type_dependent_expression_p (array_expr)
638 || type_dependent_expression_p (index)
639 || type_dependent_expression_p (length)))
640 return build_min_nt_loc (loc, OMP_ARRAY_SECTION, array_expr, index, length);
642 index = fold_non_dependent_expr (index);
643 length = fold_non_dependent_expr (length);
645 /* NOTE: We can pass through invalidly-typed index/length fields
646 here (e.g. if the user tries to use a floating-point index/length).
647 This is diagnosed later in semantics.cc:handle_omp_array_sections_1. */
649 tree expr = build_omp_array_section (loc, array_expr, index, length);
651 if (processing_template_decl)
652 expr = build_min_non_dep (OMP_ARRAY_SECTION, expr, orig_array_expr,
653 orig_index, orig_length);
654 return expr;
657 /* Given the cast expression EXP, checking out its validity. Either return
658 an error_mark_node if there was an unavoidable error, return a cast to
659 void for trying to delete a pointer w/ the value 0, or return the
660 call to delete. If DOING_VEC is true, we handle things differently
661 for doing an array delete.
662 Implements ARM $5.3.4. This is called from the parser. */
664 tree
665 delete_sanity (location_t loc, tree exp, tree size, bool doing_vec,
666 int use_global_delete, tsubst_flags_t complain)
668 tree t, type;
670 if (exp == error_mark_node)
671 return exp;
673 if (processing_template_decl)
675 t = build_min (DELETE_EXPR, void_type_node, exp, size);
676 DELETE_EXPR_USE_GLOBAL (t) = use_global_delete;
677 DELETE_EXPR_USE_VEC (t) = doing_vec;
678 TREE_SIDE_EFFECTS (t) = 1;
679 SET_EXPR_LOCATION (t, loc);
680 return t;
683 location_t exp_loc = cp_expr_loc_or_loc (exp, loc);
685 /* An array can't have been allocated by new, so complain. */
686 if (TREE_CODE (TREE_TYPE (exp)) == ARRAY_TYPE
687 && (complain & tf_warning))
688 warning_at (exp_loc, 0, "deleting array %q#E", exp);
690 t = build_expr_type_conversion (WANT_POINTER, exp, true);
692 if (t == NULL_TREE || t == error_mark_node)
694 if (complain & tf_error)
695 error_at (exp_loc,
696 "type %q#T argument given to %<delete%>, expected pointer",
697 TREE_TYPE (exp));
698 return error_mark_node;
701 type = TREE_TYPE (t);
703 /* As of Valley Forge, you can delete a pointer to const. */
705 /* You can't delete functions. */
706 if (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
708 if (complain & tf_error)
709 error_at (exp_loc,
710 "cannot delete a function. Only pointer-to-objects are "
711 "valid arguments to %<delete%>");
712 return error_mark_node;
715 /* Deleting ptr to void is undefined behavior [expr.delete/3]. */
716 if (VOID_TYPE_P (TREE_TYPE (type)))
718 if (complain & tf_warning)
719 warning_at (exp_loc, OPT_Wdelete_incomplete,
720 "deleting %qT is undefined", type);
721 doing_vec = 0;
724 /* Deleting a pointer with the value zero is valid and has no effect. */
725 if (integer_zerop (t))
726 return build1_loc (loc, NOP_EXPR, void_type_node, t);
728 if (doing_vec)
729 return build_vec_delete (loc, t, /*maxindex=*/NULL_TREE,
730 sfk_deleting_destructor,
731 use_global_delete, complain);
732 else
733 return build_delete (loc, type, t, sfk_deleting_destructor,
734 LOOKUP_NORMAL, use_global_delete,
735 complain);
738 /* Report an error if the indicated template declaration is not the
739 sort of thing that should be a member template. */
741 void
742 check_member_template (tree tmpl)
744 tree decl;
746 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
747 decl = DECL_TEMPLATE_RESULT (tmpl);
749 if (TREE_CODE (decl) == FUNCTION_DECL
750 || DECL_ALIAS_TEMPLATE_P (tmpl)
751 || (TREE_CODE (decl) == TYPE_DECL
752 && MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))))
754 /* The parser rejects template declarations in local classes
755 (with the exception of generic lambdas). */
756 gcc_assert (!current_function_decl || LAMBDA_FUNCTION_P (decl));
757 /* The parser rejects any use of virtual in a function template. */
758 gcc_assert (!(TREE_CODE (decl) == FUNCTION_DECL
759 && DECL_VIRTUAL_P (decl)));
761 /* The debug-information generating code doesn't know what to do
762 with member templates. */
763 DECL_IGNORED_P (tmpl) = 1;
765 else if (variable_template_p (tmpl))
766 /* OK */;
767 else
768 error ("template declaration of %q#D", decl);
771 /* Sanity check: report error if this function FUNCTION is not
772 really a member of the class (CTYPE) it is supposed to belong to.
773 TEMPLATE_PARMS is used to specify the template parameters of a member
774 template passed as FUNCTION_DECL. If the member template is passed as a
775 TEMPLATE_DECL, it can be NULL since the parameters can be extracted
776 from the declaration. If the function is not a function template, it
777 must be NULL.
778 It returns the original declaration for the function, NULL_TREE if
779 no declaration was found, error_mark_node if an error was emitted. */
781 tree
782 check_classfn (tree ctype, tree function, tree template_parms)
784 if (DECL_USE_TEMPLATE (function)
785 && !(TREE_CODE (function) == TEMPLATE_DECL
786 && DECL_TEMPLATE_SPECIALIZATION (function))
787 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (function)))
788 /* Since this is a specialization of a member template,
789 we're not going to find the declaration in the class.
790 For example, in:
792 struct S { template <typename T> void f(T); };
793 template <> void S::f(int);
795 we're not going to find `S::f(int)', but there's no
796 reason we should, either. We let our callers know we didn't
797 find the method, but we don't complain. */
798 return NULL_TREE;
800 /* Basic sanity check: for a template function, the template parameters
801 either were not passed, or they are the same of DECL_TEMPLATE_PARMS. */
802 if (TREE_CODE (function) == TEMPLATE_DECL)
804 if (template_parms
805 && !comp_template_parms (template_parms,
806 DECL_TEMPLATE_PARMS (function)))
808 error ("template parameter lists provided don%'t match the "
809 "template parameters of %qD", function);
810 return error_mark_node;
812 template_parms = DECL_TEMPLATE_PARMS (function);
815 /* OK, is this a definition of a member template? */
816 bool is_template = (template_parms != NULL_TREE);
818 /* [temp.mem]
820 A destructor shall not be a member template. */
821 if (DECL_DESTRUCTOR_P (function) && is_template)
823 error ("destructor %qD declared as member template", function);
824 return error_mark_node;
827 /* We must enter the scope here, because conversion operators are
828 named by target type, and type equivalence relies on typenames
829 resolving within the scope of CTYPE. */
830 tree pushed_scope = push_scope (ctype);
831 tree matched = NULL_TREE;
832 tree fns = get_class_binding (ctype, DECL_NAME (function));
833 bool saw_template = false;
835 for (ovl_iterator iter (fns); !matched && iter; ++iter)
837 tree fndecl = *iter;
839 if (TREE_CODE (fndecl) == TEMPLATE_DECL)
840 saw_template = true;
842 /* A member template definition only matches a member template
843 declaration. */
844 if (is_template != (TREE_CODE (fndecl) == TEMPLATE_DECL))
845 continue;
847 if (!DECL_DECLARES_FUNCTION_P (fndecl))
848 continue;
850 tree p1 = TYPE_ARG_TYPES (TREE_TYPE (function));
851 tree p2 = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
853 /* We cannot simply call decls_match because this doesn't work
854 for static member functions that are pretending to be
855 methods, and because the name may have been changed by
856 asm("new_name"). */
858 /* Get rid of the this parameter on functions that become
859 static. */
860 if (DECL_STATIC_FUNCTION_P (fndecl)
861 && TREE_CODE (TREE_TYPE (function)) == METHOD_TYPE)
862 p1 = TREE_CHAIN (p1);
864 /* ref-qualifier or absence of same must match. */
865 if (type_memfn_rqual (TREE_TYPE (function))
866 != type_memfn_rqual (TREE_TYPE (fndecl)))
867 continue;
869 // Include constraints in the match.
870 tree c1 = get_constraints (function);
871 tree c2 = get_constraints (fndecl);
873 /* While finding a match, same types and params are not enough
874 if the function is versioned. Also check for different target
875 specific attributes. */
876 if (same_type_p (TREE_TYPE (TREE_TYPE (function)),
877 TREE_TYPE (TREE_TYPE (fndecl)))
878 && compparms (p1, p2)
879 && !targetm.target_option.function_versions (function, fndecl)
880 && (!is_template
881 || comp_template_parms (template_parms,
882 DECL_TEMPLATE_PARMS (fndecl)))
883 && equivalent_constraints (c1, c2)
884 && (DECL_TEMPLATE_SPECIALIZATION (function)
885 == DECL_TEMPLATE_SPECIALIZATION (fndecl))
886 && (!DECL_TEMPLATE_SPECIALIZATION (function)
887 || (DECL_TI_TEMPLATE (function) == DECL_TI_TEMPLATE (fndecl))))
888 matched = fndecl;
891 if (!matched && !is_template && saw_template
892 && !processing_template_decl && DECL_UNIQUE_FRIEND_P (function))
894 /* "[if no non-template match is found,] each remaining function template
895 is replaced with the specialization chosen by deduction from the
896 friend declaration or discarded if deduction fails."
898 So tell check_explicit_specialization to look for a match. */
899 SET_DECL_IMPLICIT_INSTANTIATION (function);
900 DECL_TEMPLATE_INFO (function) = build_template_info (fns, NULL_TREE);
901 matched = function;
904 if (!matched)
906 if (!COMPLETE_TYPE_P (ctype))
907 cxx_incomplete_type_error (DECL_SOURCE_LOCATION (function),
908 function, ctype);
909 else
911 if (DECL_CONV_FN_P (function))
912 fns = get_class_binding (ctype, conv_op_identifier);
914 auto_diagnostic_group d;
915 error_at (DECL_SOURCE_LOCATION (function),
916 "no declaration matches %q#D", function);
917 if (fns)
918 print_candidates (fns);
919 else if (DECL_CONV_FN_P (function))
920 inform (DECL_SOURCE_LOCATION (function),
921 "no conversion operators declared");
922 else
923 inform (DECL_SOURCE_LOCATION (function),
924 "no functions named %qD", function);
925 inform (DECL_SOURCE_LOCATION (TYPE_NAME (ctype)),
926 "%#qT defined here", ctype);
928 matched = error_mark_node;
931 if (pushed_scope)
932 pop_scope (pushed_scope);
934 return matched;
937 /* DECL is a function with vague linkage. Remember it so that at the
938 end of the translation unit we can decide whether or not to emit
939 it. */
941 void
942 note_vague_linkage_fn (tree decl)
944 if (processing_template_decl)
945 return;
947 DECL_DEFER_OUTPUT (decl) = 1;
948 vec_safe_push (deferred_fns, decl);
951 /* As above, but for variables. */
953 void
954 note_vague_linkage_variable (tree decl)
956 vec_safe_push (pending_statics, decl);
959 /* We have just processed the DECL, which is a static data member.
960 The other parameters are as for cp_finish_decl. */
962 void
963 finish_static_data_member_decl (tree decl,
964 tree init, bool init_const_expr_p,
965 tree asmspec_tree,
966 int flags)
968 if (DECL_TEMPLATE_INSTANTIATED (decl))
969 /* We already needed to instantiate this, so the processing in this
970 function is unnecessary/wrong. */
971 return;
973 DECL_CONTEXT (decl) = current_class_type;
975 /* We cannot call pushdecl here, because that would fill in the
976 TREE_CHAIN of our decl. Instead, we modify cp_finish_decl to do
977 the right thing, namely, to put this decl out straight away. */
979 if (! processing_template_decl)
980 vec_safe_push (pending_statics, decl);
982 if (LOCAL_CLASS_P (current_class_type)
983 /* We already complained about the template definition. */
984 && !DECL_TEMPLATE_INSTANTIATION (decl))
985 permerror (DECL_SOURCE_LOCATION (decl),
986 "local class %q#T shall not have static data member %q#D",
987 current_class_type, decl);
988 else
989 for (tree t = current_class_type; TYPE_P (t);
990 t = CP_TYPE_CONTEXT (t))
991 if (TYPE_UNNAMED_P (t))
993 auto_diagnostic_group d;
994 if (permerror (DECL_SOURCE_LOCATION (decl),
995 "static data member %qD in unnamed class", decl))
996 inform (DECL_SOURCE_LOCATION (TYPE_NAME (t)),
997 "unnamed class defined here");
998 break;
1001 if (DECL_INLINE_VAR_P (decl) && !DECL_TEMPLATE_INSTANTIATION (decl))
1002 /* An inline variable is immediately defined, so don't set DECL_IN_AGGR_P.
1003 Except that if decl is a template instantiation, it isn't defined until
1004 instantiate_decl. */;
1005 else
1006 DECL_IN_AGGR_P (decl) = 1;
1008 if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
1009 && TYPE_DOMAIN (TREE_TYPE (decl)) == NULL_TREE)
1010 SET_VAR_HAD_UNKNOWN_BOUND (decl);
1012 if (init)
1014 /* Similarly to start_decl_1, we want to complete the type in order
1015 to do the right thing in cp_apply_type_quals_to_decl, possibly
1016 clear TYPE_QUAL_CONST (c++/65579). */
1017 tree type = TREE_TYPE (decl) = complete_type (TREE_TYPE (decl));
1018 cp_apply_type_quals_to_decl (cp_type_quals (type), decl);
1021 cp_finish_decl (decl, init, init_const_expr_p, asmspec_tree, flags);
1024 /* DECLARATOR and DECLSPECS correspond to a class member. The other
1025 parameters are as for cp_finish_decl. Return the DECL for the
1026 class member declared. */
1028 tree
1029 grokfield (const cp_declarator *declarator,
1030 cp_decl_specifier_seq *declspecs,
1031 tree init, bool init_const_expr_p,
1032 tree asmspec_tree,
1033 tree attrlist)
1035 tree value;
1036 const char *asmspec = 0;
1037 int flags;
1039 if (init
1040 && TREE_CODE (init) == TREE_LIST
1041 && TREE_VALUE (init) == error_mark_node
1042 && TREE_CHAIN (init) == NULL_TREE)
1043 init = NULL_TREE;
1045 int initialized;
1046 if (init == ridpointers[(int)RID_DELETE]
1047 || (init
1048 && TREE_CODE (init) == STRING_CST
1049 && TREE_TYPE (init) == ridpointers[(int)RID_DELETE]))
1050 initialized = SD_DELETED;
1051 else if (init == ridpointers[(int)RID_DEFAULT])
1052 initialized = SD_DEFAULTED;
1053 else if (init)
1054 initialized = SD_INITIALIZED;
1055 else
1056 initialized = SD_UNINITIALIZED;
1058 value = grokdeclarator (declarator, declspecs, FIELD, initialized, &attrlist);
1059 if (! value || value == error_mark_node)
1060 /* friend or constructor went bad. */
1061 return error_mark_node;
1062 if (TREE_TYPE (value) == error_mark_node)
1063 return value;
1065 if (TREE_CODE (value) == TYPE_DECL && init)
1067 error_at (cp_expr_loc_or_loc (init, DECL_SOURCE_LOCATION (value)),
1068 "typedef %qD is initialized (use %qs instead)",
1069 value, "decltype");
1070 init = NULL_TREE;
1073 /* Pass friendly classes back. */
1074 if (value == void_type_node)
1075 return value;
1077 if (DECL_NAME (value)
1078 && TREE_CODE (DECL_NAME (value)) == TEMPLATE_ID_EXPR)
1080 error_at (declarator->id_loc,
1081 "explicit template argument list not allowed");
1082 return error_mark_node;
1085 /* Stash away type declarations. */
1086 if (TREE_CODE (value) == TYPE_DECL)
1088 DECL_NONLOCAL (value) = 1;
1089 DECL_CONTEXT (value) = current_class_type;
1091 if (attrlist)
1093 int attrflags = 0;
1095 /* If this is a typedef that names the class for linkage purposes
1096 (7.1.3p8), apply any attributes directly to the type. */
1097 if (OVERLOAD_TYPE_P (TREE_TYPE (value))
1098 && value == TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (value))))
1099 attrflags = ATTR_FLAG_TYPE_IN_PLACE;
1101 cplus_decl_attributes (&value, attrlist, attrflags);
1104 if (decl_spec_seq_has_spec_p (declspecs, ds_typedef)
1105 && TREE_TYPE (value) != error_mark_node
1106 && TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (value))) != value)
1107 set_underlying_type (value);
1109 /* It's important that push_template_decl below follows
1110 set_underlying_type above so that the created template
1111 carries the properly set type of VALUE. */
1112 if (processing_template_decl)
1113 value = push_template_decl (value);
1115 record_locally_defined_typedef (value);
1116 return value;
1119 int friendp = decl_spec_seq_has_spec_p (declspecs, ds_friend);
1121 if (!friendp && DECL_IN_AGGR_P (value))
1123 error ("%qD is already defined in %qT", value, DECL_CONTEXT (value));
1124 return void_type_node;
1127 if (asmspec_tree && asmspec_tree != error_mark_node)
1128 asmspec = TREE_STRING_POINTER (asmspec_tree);
1130 if (init)
1132 if (TREE_CODE (value) == FUNCTION_DECL)
1134 if (init == ridpointers[(int)RID_DELETE]
1135 || (TREE_CODE (init) == STRING_CST
1136 && TREE_TYPE (init) == ridpointers[(int)RID_DELETE]))
1138 DECL_DELETED_FN (value) = 1;
1139 DECL_DECLARED_INLINE_P (value) = 1;
1140 if (TREE_CODE (init) == STRING_CST)
1141 DECL_INITIAL (value) = init;
1143 else if (init == ridpointers[(int)RID_DEFAULT])
1145 if (defaultable_fn_check (value))
1147 DECL_DEFAULTED_FN (value) = 1;
1148 DECL_INITIALIZED_IN_CLASS_P (value) = 1;
1149 DECL_DECLARED_INLINE_P (value) = 1;
1150 /* grokfndecl set this to error_mark_node, but we want to
1151 leave it unset until synthesize_method. */
1152 DECL_INITIAL (value) = NULL_TREE;
1155 else if (TREE_CODE (init) == DEFERRED_PARSE)
1156 error ("invalid initializer for member function %qD", value);
1157 else if (TREE_CODE (TREE_TYPE (value)) == METHOD_TYPE)
1159 if (integer_zerop (init))
1160 DECL_PURE_VIRTUAL_P (value) = 1;
1161 else if (error_operand_p (init))
1162 ; /* An error has already been reported. */
1163 else
1164 error ("invalid initializer for member function %qD",
1165 value);
1167 else
1169 gcc_assert (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE);
1170 location_t iloc
1171 = cp_expr_loc_or_loc (init, DECL_SOURCE_LOCATION (value));
1172 if (friendp)
1173 error_at (iloc, "initializer specified for friend "
1174 "function %qD", value);
1175 else
1176 error_at (iloc, "initializer specified for static "
1177 "member function %qD", value);
1180 else if (TREE_CODE (value) == FIELD_DECL)
1181 /* C++11 NSDMI, keep going. */;
1182 else if (!VAR_P (value))
1183 gcc_unreachable ();
1186 /* Pass friend decls back. */
1187 if ((TREE_CODE (value) == FUNCTION_DECL
1188 || TREE_CODE (value) == TEMPLATE_DECL)
1189 && DECL_CONTEXT (value) != current_class_type)
1191 if (attrlist)
1192 cplus_decl_attributes (&value, attrlist, 0);
1193 return value;
1196 /* Need to set this before push_template_decl. */
1197 if (VAR_P (value))
1198 DECL_CONTEXT (value) = current_class_type;
1200 if (processing_template_decl && VAR_OR_FUNCTION_DECL_P (value))
1202 value = push_template_decl (value);
1203 if (error_operand_p (value))
1204 return error_mark_node;
1207 if (attrlist)
1208 cplus_decl_attributes (&value, attrlist, 0);
1210 if (init && DIRECT_LIST_INIT_P (init))
1211 flags = LOOKUP_NORMAL;
1212 else
1213 flags = LOOKUP_IMPLICIT;
1215 switch (TREE_CODE (value))
1217 case VAR_DECL:
1218 finish_static_data_member_decl (value, init, init_const_expr_p,
1219 asmspec_tree, flags);
1220 return value;
1222 case FIELD_DECL:
1223 if (asmspec)
1224 error ("%<asm%> specifiers are not permitted on non-static data members");
1225 if (DECL_INITIAL (value) == error_mark_node)
1226 init = error_mark_node;
1227 cp_finish_decl (value, init, /*init_const_expr_p=*/false,
1228 NULL_TREE, flags);
1229 DECL_IN_AGGR_P (value) = 1;
1230 return value;
1232 case FUNCTION_DECL:
1233 if (asmspec)
1234 set_user_assembler_name (value, asmspec);
1236 cp_finish_decl (value,
1237 /*init=*/NULL_TREE,
1238 /*init_const_expr_p=*/false,
1239 asmspec_tree, flags);
1241 /* Pass friends back this way. */
1242 if (DECL_UNIQUE_FRIEND_P (value))
1243 return void_type_node;
1245 DECL_IN_AGGR_P (value) = 1;
1246 return value;
1248 default:
1249 gcc_unreachable ();
1251 return NULL_TREE;
1254 /* Like `grokfield', but for bitfields.
1255 WIDTH is the width of the bitfield, a constant expression.
1256 The other parameters are as for grokfield. */
1258 tree
1259 grokbitfield (const cp_declarator *declarator,
1260 cp_decl_specifier_seq *declspecs, tree width, tree init,
1261 tree attrlist)
1263 tree value = grokdeclarator (declarator, declspecs, BITFIELD,
1264 init != NULL_TREE, &attrlist);
1266 if (value == error_mark_node)
1267 return NULL_TREE; /* friends went bad. */
1269 tree type = TREE_TYPE (value);
1270 if (type == error_mark_node)
1271 return value;
1273 /* Pass friendly classes back. */
1274 if (VOID_TYPE_P (value))
1275 return void_type_node;
1277 if (!INTEGRAL_OR_ENUMERATION_TYPE_P (type)
1278 && (INDIRECT_TYPE_P (type) || !dependent_type_p (type)))
1280 error_at (DECL_SOURCE_LOCATION (value),
1281 "bit-field %qD with non-integral type %qT",
1282 value, type);
1283 return error_mark_node;
1286 if (TREE_CODE (value) == TYPE_DECL)
1288 error_at (DECL_SOURCE_LOCATION (value),
1289 "cannot declare %qD to be a bit-field type", value);
1290 return NULL_TREE;
1293 /* Usually, finish_struct_1 catches bitfields with invalid types.
1294 But, in the case of bitfields with function type, we confuse
1295 ourselves into thinking they are member functions, so we must
1296 check here. */
1297 if (TREE_CODE (value) == FUNCTION_DECL)
1299 error_at (DECL_SOURCE_LOCATION (value),
1300 "cannot declare bit-field %qD with function type", value);
1301 return NULL_TREE;
1304 if (TYPE_WARN_IF_NOT_ALIGN (type))
1306 error_at (DECL_SOURCE_LOCATION (value), "cannot declare bit-field "
1307 "%qD with %<warn_if_not_aligned%> type", value);
1308 return NULL_TREE;
1311 if (DECL_IN_AGGR_P (value))
1313 error ("%qD is already defined in the class %qT", value,
1314 DECL_CONTEXT (value));
1315 return void_type_node;
1318 if (TREE_STATIC (value))
1320 error_at (DECL_SOURCE_LOCATION (value),
1321 "static member %qD cannot be a bit-field", value);
1322 return NULL_TREE;
1325 int flags = LOOKUP_IMPLICIT;
1326 if (init && DIRECT_LIST_INIT_P (init))
1327 flags = LOOKUP_NORMAL;
1328 cp_finish_decl (value, init, false, NULL_TREE, flags);
1330 if (width != error_mark_node)
1332 /* The width must be an integer type. */
1333 if (!type_dependent_expression_p (width)
1334 && !INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (width)))
1335 error ("width of bit-field %qD has non-integral type %qT", value,
1336 TREE_TYPE (width));
1337 else if (!check_for_bare_parameter_packs (width))
1339 /* Temporarily stash the width in DECL_BIT_FIELD_REPRESENTATIVE.
1340 check_bitfield_decl picks it from there later and sets DECL_SIZE
1341 accordingly. */
1342 DECL_BIT_FIELD_REPRESENTATIVE (value) = width;
1343 SET_DECL_C_BIT_FIELD (value);
1347 DECL_IN_AGGR_P (value) = 1;
1349 if (attrlist)
1350 cplus_decl_attributes (&value, attrlist, /*flags=*/0);
1352 return value;
1356 /* Returns true iff ATTR is an attribute which needs to be applied at
1357 instantiation time rather than template definition time. */
1359 static bool
1360 is_late_template_attribute (tree attr, tree decl)
1362 tree name = get_attribute_name (attr);
1363 tree args = TREE_VALUE (attr);
1364 const struct attribute_spec *spec = lookup_attribute_spec (name);
1365 tree arg;
1367 if (!spec)
1368 /* Unknown attribute. */
1369 return false;
1371 /* Attribute weak handling wants to write out assembly right away. */
1372 if (is_attribute_p ("weak", name))
1373 return true;
1375 /* Attributes used and unused are applied directly to typedefs for the
1376 benefit of maybe_warn_unused_local_typedefs. */
1377 if (TREE_CODE (decl) == TYPE_DECL
1378 && (is_attribute_p ("unused", name)
1379 || is_attribute_p ("used", name)))
1380 return false;
1382 /* Attribute tls_model wants to modify the symtab. */
1383 if (is_attribute_p ("tls_model", name))
1384 return true;
1386 /* #pragma omp declare simd attribute needs to be always deferred. */
1387 if (flag_openmp
1388 && is_attribute_p ("omp declare simd", name))
1389 return true;
1391 if (args == error_mark_node)
1392 return false;
1394 /* An attribute pack is clearly dependent. */
1395 if (args && PACK_EXPANSION_P (args))
1396 return true;
1398 /* If any of the arguments are dependent expressions, we can't evaluate
1399 the attribute until instantiation time. */
1400 for (arg = args; arg; arg = TREE_CHAIN (arg))
1402 tree t = TREE_VALUE (arg);
1404 /* If the first attribute argument is an identifier, only consider
1405 second and following arguments. Attributes like mode, format,
1406 cleanup and several target specific attributes aren't late
1407 just because they have an IDENTIFIER_NODE as first argument. */
1408 if (arg == args && attribute_takes_identifier_p (name)
1409 && identifier_p (t))
1410 continue;
1412 if (value_dependent_expression_p (t))
1413 return true;
1416 if (TREE_CODE (decl) == TYPE_DECL
1417 || TYPE_P (decl)
1418 || spec->type_required)
1420 tree type = TYPE_P (decl) ? decl : TREE_TYPE (decl);
1422 if (!type)
1423 return true;
1425 /* We can't apply any attributes to a completely unknown type until
1426 instantiation time. */
1427 enum tree_code code = TREE_CODE (type);
1428 if (code == TEMPLATE_TYPE_PARM
1429 || code == BOUND_TEMPLATE_TEMPLATE_PARM
1430 || code == TYPENAME_TYPE)
1431 return true;
1432 /* Also defer most attributes on dependent types. This is not
1433 necessary in all cases, but is the better default. */
1434 else if (dependent_type_p (type)
1435 /* But some attributes specifically apply to templates. */
1436 && !is_attribute_p ("abi_tag", name)
1437 && !is_attribute_p ("deprecated", name)
1438 && !is_attribute_p ("unavailable", name)
1439 && !is_attribute_p ("visibility", name))
1440 return true;
1441 else
1442 return false;
1444 else
1445 return false;
1448 /* ATTR_P is a list of attributes. Remove any attributes which need to be
1449 applied at instantiation time and return them. If IS_DEPENDENT is true,
1450 the declaration itself is dependent, so all attributes should be applied
1451 at instantiation time. */
1453 tree
1454 splice_template_attributes (tree *attr_p, tree decl)
1456 tree *p = attr_p;
1457 tree late_attrs = NULL_TREE;
1458 tree *q = &late_attrs;
1460 if (!p || *p == error_mark_node)
1461 return NULL_TREE;
1463 for (; *p; )
1465 if (is_late_template_attribute (*p, decl))
1467 ATTR_IS_DEPENDENT (*p) = 1;
1468 *q = *p;
1469 *p = TREE_CHAIN (*p);
1470 q = &TREE_CHAIN (*q);
1471 *q = NULL_TREE;
1473 else
1474 p = &TREE_CHAIN (*p);
1477 return late_attrs;
1480 /* Attach any LATE_ATTRS to DECL_P, after the non-dependent attributes have
1481 been applied by a previous call to decl_attributes. */
1483 static void
1484 save_template_attributes (tree late_attrs, tree *decl_p, int flags)
1486 tree *q;
1488 if (!late_attrs)
1489 return;
1491 if (DECL_P (*decl_p))
1492 q = &DECL_ATTRIBUTES (*decl_p);
1493 else
1494 q = &TYPE_ATTRIBUTES (*decl_p);
1496 tree old_attrs = *q;
1498 /* Place the late attributes at the beginning of the attribute
1499 list. */
1500 late_attrs = chainon (late_attrs, *q);
1501 if (*q != late_attrs
1502 && !DECL_P (*decl_p)
1503 && !(flags & ATTR_FLAG_TYPE_IN_PLACE))
1505 if (!dependent_type_p (*decl_p))
1506 *decl_p = cp_build_type_attribute_variant (*decl_p, late_attrs);
1507 else
1509 *decl_p = build_variant_type_copy (*decl_p);
1510 TYPE_ATTRIBUTES (*decl_p) = late_attrs;
1513 else
1514 *q = late_attrs;
1516 if (!DECL_P (*decl_p) && *decl_p == TYPE_MAIN_VARIANT (*decl_p))
1518 /* We've added new attributes directly to the main variant, so
1519 now we need to update all of the other variants to include
1520 these new attributes. */
1521 tree variant;
1522 for (variant = TYPE_NEXT_VARIANT (*decl_p); variant;
1523 variant = TYPE_NEXT_VARIANT (variant))
1525 gcc_assert (TYPE_ATTRIBUTES (variant) == old_attrs);
1526 TYPE_ATTRIBUTES (variant) = TYPE_ATTRIBUTES (*decl_p);
1531 /* True if ATTRS contains any dependent attributes that affect type
1532 identity. */
1534 bool
1535 any_dependent_type_attributes_p (tree attrs)
1537 for (tree a = attrs; a; a = TREE_CHAIN (a))
1538 if (ATTR_IS_DEPENDENT (a))
1540 const attribute_spec *as = lookup_attribute_spec (TREE_PURPOSE (a));
1541 if (as && as->affects_type_identity)
1542 return true;
1544 return false;
1547 /* Return true iff ATTRS are acceptable attributes to be applied in-place
1548 to a typedef which gives a previously unnamed class or enum a name for
1549 linkage purposes. */
1551 bool
1552 attributes_naming_typedef_ok (tree attrs)
1554 for (; attrs; attrs = TREE_CHAIN (attrs))
1556 tree name = get_attribute_name (attrs);
1557 if (is_attribute_p ("vector_size", name))
1558 return false;
1560 return true;
1563 /* Like reconstruct_complex_type, but handle also template trees. */
1565 tree
1566 cp_reconstruct_complex_type (tree type, tree bottom)
1568 tree inner, outer;
1570 if (TYPE_PTR_P (type))
1572 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1573 outer = build_pointer_type_for_mode (inner, TYPE_MODE (type),
1574 TYPE_REF_CAN_ALIAS_ALL (type));
1576 else if (TYPE_REF_P (type))
1578 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1579 outer = build_reference_type_for_mode (inner, TYPE_MODE (type),
1580 TYPE_REF_CAN_ALIAS_ALL (type));
1582 else if (TREE_CODE (type) == ARRAY_TYPE)
1584 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1585 outer = build_cplus_array_type (inner, TYPE_DOMAIN (type));
1586 /* Don't call cp_build_qualified_type on ARRAY_TYPEs, the
1587 element type qualification will be handled by the recursive
1588 cp_reconstruct_complex_type call and cp_build_qualified_type
1589 for ARRAY_TYPEs changes the element type. */
1590 return outer;
1592 else if (TREE_CODE (type) == FUNCTION_TYPE)
1594 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1595 outer = build_function_type (inner, TYPE_ARG_TYPES (type));
1596 outer = apply_memfn_quals (outer, type_memfn_quals (type));
1598 else if (TREE_CODE (type) == METHOD_TYPE)
1600 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1601 /* The build_method_type_directly() routine prepends 'this' to argument list,
1602 so we must compensate by getting rid of it. */
1603 outer
1604 = build_method_type_directly
1605 (class_of_this_parm (type), inner,
1606 TREE_CHAIN (TYPE_ARG_TYPES (type)));
1608 else if (TREE_CODE (type) == OFFSET_TYPE)
1610 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1611 outer = build_offset_type (TYPE_OFFSET_BASETYPE (type), inner);
1613 else
1614 return bottom;
1616 if (TYPE_ATTRIBUTES (type))
1617 outer = cp_build_type_attribute_variant (outer, TYPE_ATTRIBUTES (type));
1618 outer = cp_build_qualified_type (outer, cp_type_quals (type));
1619 outer = cxx_copy_lang_qualifiers (outer, type);
1621 return outer;
1624 /* Replaces any constexpr expression that may be into the attributes
1625 arguments with their reduced value. */
1627 void
1628 cp_check_const_attributes (tree attributes)
1630 if (attributes == error_mark_node)
1631 return;
1633 tree attr;
1634 for (attr = attributes; attr; attr = TREE_CHAIN (attr))
1636 if (cxx_contract_attribute_p (attr))
1637 continue;
1639 tree arg;
1640 /* As we implement alignas using gnu::aligned attribute and
1641 alignas argument is a constant expression, force manifestly
1642 constant evaluation of aligned attribute argument. */
1643 bool manifestly_const_eval
1644 = is_attribute_p ("aligned", get_attribute_name (attr));
1645 for (arg = TREE_VALUE (attr); arg && TREE_CODE (arg) == TREE_LIST;
1646 arg = TREE_CHAIN (arg))
1648 tree expr = TREE_VALUE (arg);
1649 if (EXPR_P (expr))
1650 TREE_VALUE (arg)
1651 = fold_non_dependent_expr (expr, tf_warning_or_error,
1652 manifestly_const_eval);
1657 /* Copies hot or cold attributes to a function FN if present on the
1658 encapsulating class, struct, or union TYPE. */
1660 void
1661 maybe_propagate_warmth_attributes (tree fn, tree type)
1663 if (fn == NULL_TREE || type == NULL_TREE
1664 || !(TREE_CODE (type) == RECORD_TYPE
1665 || TREE_CODE (type) == UNION_TYPE))
1666 return;
1668 tree has_cold_attr = lookup_attribute ("cold", TYPE_ATTRIBUTES (type));
1669 tree has_hot_attr = lookup_attribute ("hot", TYPE_ATTRIBUTES (type));
1671 if (has_cold_attr || has_hot_attr)
1673 /* Transparently ignore the new warmth attribute if it
1674 conflicts with a present function attribute. Otherwise
1675 decl_attribute would still honour the present attribute,
1676 but producing an undesired warning in the process. */
1678 if (has_cold_attr)
1680 if (lookup_attribute ("hot", DECL_ATTRIBUTES (fn)) == NULL)
1682 tree cold_cons
1683 = tree_cons (get_identifier ("cold"), NULL, NULL);
1685 decl_attributes (&fn, cold_cons, 0);
1688 else if (has_hot_attr)
1690 if (lookup_attribute ("cold", DECL_ATTRIBUTES (fn)) == NULL)
1692 tree hot_cons
1693 = tree_cons (get_identifier ("hot"), NULL, NULL);
1695 decl_attributes (&fn, hot_cons, 0);
1701 /* Return the last pushed declaration for the symbol DECL or NULL
1702 when no such declaration exists. */
1704 static tree
1705 find_last_decl (tree decl)
1707 tree last_decl = NULL_TREE;
1709 if (tree name = DECL_P (decl) ? DECL_NAME (decl) : NULL_TREE)
1711 /* Template specializations are matched elsewhere. */
1712 if (DECL_LANG_SPECIFIC (decl)
1713 && DECL_USE_TEMPLATE (decl))
1714 return NULL_TREE;
1716 /* Look up the declaration in its scope. */
1717 tree pushed_scope = NULL_TREE;
1718 if (tree ctype = DECL_CONTEXT (decl))
1719 pushed_scope = push_scope (ctype);
1721 last_decl = lookup_name (name);
1723 if (pushed_scope)
1724 pop_scope (pushed_scope);
1726 /* The declaration may be a member conversion operator
1727 or a bunch of overfloads (handle the latter below). */
1728 if (last_decl && BASELINK_P (last_decl))
1729 last_decl = BASELINK_FUNCTIONS (last_decl);
1732 if (!last_decl)
1733 return NULL_TREE;
1735 if (DECL_P (last_decl) || TREE_CODE (last_decl) == OVERLOAD)
1737 /* A set of overloads of the same function. */
1738 for (lkp_iterator iter (last_decl); iter; ++iter)
1740 if (TREE_CODE (*iter) == OVERLOAD)
1741 continue;
1743 tree d = *iter;
1745 /* We can't compare versions in the middle of processing the
1746 attribute that has the version. */
1747 if (TREE_CODE (d) == FUNCTION_DECL
1748 && DECL_FUNCTION_VERSIONED (d))
1749 return NULL_TREE;
1751 if (decls_match (decl, d, /*record_decls=*/false))
1752 return d;
1754 return NULL_TREE;
1757 return NULL_TREE;
1760 /* Like decl_attributes, but handle C++ complexity. */
1762 void
1763 cplus_decl_attributes (tree *decl, tree attributes, int flags)
1765 if (*decl == NULL_TREE || *decl == void_type_node
1766 || *decl == error_mark_node || attributes == error_mark_node)
1767 return;
1769 /* Add implicit "omp declare target" attribute if requested. */
1770 if (vec_safe_length (scope_chain->omp_declare_target_attribute)
1771 && ((VAR_P (*decl)
1772 && (TREE_STATIC (*decl) || DECL_EXTERNAL (*decl)))
1773 || TREE_CODE (*decl) == FUNCTION_DECL))
1775 if (VAR_P (*decl)
1776 && DECL_CLASS_SCOPE_P (*decl))
1777 error ("%q+D static data member inside of declare target directive",
1778 *decl);
1779 else
1781 if (VAR_P (*decl)
1782 && (processing_template_decl
1783 || !omp_mappable_type (TREE_TYPE (*decl))))
1784 attributes
1785 = tree_cons (get_identifier ("omp declare target implicit"),
1786 NULL_TREE, attributes);
1787 else
1789 attributes = tree_cons (get_identifier ("omp declare target"),
1790 NULL_TREE, attributes);
1791 attributes
1792 = tree_cons (get_identifier ("omp declare target block"),
1793 NULL_TREE, attributes);
1795 if (TREE_CODE (*decl) == FUNCTION_DECL)
1797 cp_omp_declare_target_attr &last
1798 = scope_chain->omp_declare_target_attribute->last ();
1799 int device_type = MAX (last.device_type, 0);
1800 if ((device_type & OMP_CLAUSE_DEVICE_TYPE_HOST) != 0
1801 && !lookup_attribute ("omp declare target host",
1802 attributes))
1803 attributes
1804 = tree_cons (get_identifier ("omp declare target host"),
1805 NULL_TREE, attributes);
1806 if ((device_type & OMP_CLAUSE_DEVICE_TYPE_NOHOST) != 0
1807 && !lookup_attribute ("omp declare target nohost",
1808 attributes))
1809 attributes
1810 = tree_cons (get_identifier ("omp declare target nohost"),
1811 NULL_TREE, attributes);
1812 if (last.indirect
1813 && !lookup_attribute ("omp declare target indirect",
1814 attributes))
1815 attributes
1816 = tree_cons (get_identifier ("omp declare target indirect"),
1817 NULL_TREE, attributes);
1822 tree late_attrs = NULL_TREE;
1823 if (processing_template_decl)
1825 if (check_for_bare_parameter_packs (attributes))
1826 return;
1827 late_attrs = splice_template_attributes (&attributes, *decl);
1830 cp_check_const_attributes (attributes);
1832 if (flag_openmp || flag_openmp_simd)
1834 bool diagnosed = false;
1835 for (tree *pa = &attributes; *pa; )
1837 if (get_attribute_namespace (*pa) == omp_identifier)
1839 tree name = get_attribute_name (*pa);
1840 if (is_attribute_p ("directive", name)
1841 || is_attribute_p ("sequence", name)
1842 || is_attribute_p ("decl", name))
1844 const char *p = NULL;
1845 if (TREE_VALUE (*pa) == NULL_TREE)
1846 p = IDENTIFIER_POINTER (name);
1847 for (tree a = TREE_VALUE (*pa); a; a = TREE_CHAIN (a))
1849 tree d = TREE_VALUE (a);
1850 gcc_assert (TREE_CODE (d) == DEFERRED_PARSE);
1851 if (TREE_PUBLIC (d)
1852 && (VAR_P (*decl)
1853 || TREE_CODE (*decl) == FUNCTION_DECL)
1854 && cp_maybe_parse_omp_decl (*decl, d))
1855 continue;
1856 p = TREE_PUBLIC (d) ? "decl" : "directive";
1858 if (p && !diagnosed)
1860 error ("%<omp::%s%> not allowed to be specified in "
1861 "this context", p);
1862 diagnosed = true;
1864 if (p)
1866 *pa = TREE_CHAIN (*pa);
1867 continue;
1871 pa = &TREE_CHAIN (*pa);
1875 if (TREE_CODE (*decl) == TEMPLATE_DECL)
1876 decl = &DECL_TEMPLATE_RESULT (*decl);
1878 if (TREE_TYPE (*decl) && TYPE_PTRMEMFUNC_P (TREE_TYPE (*decl)))
1880 attributes
1881 = decl_attributes (decl, attributes, flags | ATTR_FLAG_FUNCTION_NEXT);
1882 decl_attributes (&TYPE_PTRMEMFUNC_FN_TYPE_RAW (TREE_TYPE (*decl)),
1883 attributes, flags);
1885 else
1887 tree last_decl = find_last_decl (*decl);
1888 decl_attributes (decl, attributes, flags, last_decl);
1891 if (late_attrs)
1892 save_template_attributes (late_attrs, decl, flags);
1894 /* Propagate deprecation out to the template. */
1895 if (TREE_DEPRECATED (*decl))
1896 if (tree ti = get_template_info (*decl))
1898 tree tmpl = TI_TEMPLATE (ti);
1899 tree pattern = (TYPE_P (*decl) ? TREE_TYPE (tmpl)
1900 : DECL_TEMPLATE_RESULT (tmpl));
1901 if (*decl == pattern)
1902 TREE_DEPRECATED (tmpl) = true;
1905 /* Likewise, propagate unavailability out to the template. */
1906 if (TREE_UNAVAILABLE (*decl))
1907 if (tree ti = get_template_info (*decl))
1909 tree tmpl = TI_TEMPLATE (ti);
1910 tree pattern = (TYPE_P (*decl) ? TREE_TYPE (tmpl)
1911 : DECL_TEMPLATE_RESULT (tmpl));
1912 if (*decl == pattern)
1913 TREE_UNAVAILABLE (tmpl) = true;
1917 /* Walks through the namespace- or function-scope anonymous union
1918 OBJECT, with the indicated TYPE, building appropriate VAR_DECLs.
1919 Returns one of the fields for use in the mangled name. */
1921 static tree
1922 build_anon_union_vars (tree type, tree object)
1924 tree main_decl = NULL_TREE;
1925 tree field;
1927 /* Rather than write the code to handle the non-union case,
1928 just give an error. */
1929 if (TREE_CODE (type) != UNION_TYPE)
1931 error_at (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
1932 "anonymous struct not inside named type");
1933 return error_mark_node;
1936 for (field = TYPE_FIELDS (type);
1937 field != NULL_TREE;
1938 field = DECL_CHAIN (field))
1940 tree decl;
1941 tree ref;
1943 if (DECL_ARTIFICIAL (field))
1944 continue;
1945 if (TREE_CODE (field) != FIELD_DECL)
1947 permerror (DECL_SOURCE_LOCATION (field),
1948 "%q#D invalid; an anonymous union can only "
1949 "have non-static data members", field);
1950 continue;
1953 if (TREE_PRIVATE (field))
1954 permerror (DECL_SOURCE_LOCATION (field),
1955 "private member %q#D in anonymous union", field);
1956 else if (TREE_PROTECTED (field))
1957 permerror (DECL_SOURCE_LOCATION (field),
1958 "protected member %q#D in anonymous union", field);
1960 if (processing_template_decl)
1961 ref = build_min_nt_loc (UNKNOWN_LOCATION, COMPONENT_REF, object,
1962 DECL_NAME (field), NULL_TREE);
1963 else
1964 ref = build_class_member_access_expr (object, field, NULL_TREE,
1965 false, tf_warning_or_error);
1967 if (DECL_NAME (field))
1969 tree base;
1971 decl = build_decl (input_location,
1972 VAR_DECL, DECL_NAME (field), TREE_TYPE (field));
1973 DECL_ANON_UNION_VAR_P (decl) = 1;
1974 DECL_ARTIFICIAL (decl) = 1;
1976 base = get_base_address (object);
1977 TREE_PUBLIC (decl) = TREE_PUBLIC (base);
1978 TREE_STATIC (decl) = TREE_STATIC (base);
1979 DECL_EXTERNAL (decl) = DECL_EXTERNAL (base);
1981 SET_DECL_VALUE_EXPR (decl, ref);
1982 DECL_HAS_VALUE_EXPR_P (decl) = 1;
1984 decl = pushdecl (decl);
1986 else if (ANON_AGGR_TYPE_P (TREE_TYPE (field)))
1987 decl = build_anon_union_vars (TREE_TYPE (field), ref);
1988 else
1989 decl = 0;
1991 if (main_decl == NULL_TREE)
1992 main_decl = decl;
1995 return main_decl;
1998 /* Finish off the processing of a UNION_TYPE structure. If the union is an
1999 anonymous union, then all members must be laid out together. PUBLIC_P
2000 is nonzero if this union is not declared static. */
2002 void
2003 finish_anon_union (tree anon_union_decl)
2005 tree type;
2006 tree main_decl;
2007 bool public_p;
2009 if (anon_union_decl == error_mark_node)
2010 return;
2012 type = TREE_TYPE (anon_union_decl);
2013 public_p = TREE_PUBLIC (anon_union_decl);
2015 /* The VAR_DECL's context is the same as the TYPE's context. */
2016 DECL_CONTEXT (anon_union_decl) = DECL_CONTEXT (TYPE_NAME (type));
2018 if (TYPE_FIELDS (type) == NULL_TREE)
2019 return;
2021 if (public_p)
2023 error ("namespace-scope anonymous aggregates must be static");
2024 return;
2027 main_decl = build_anon_union_vars (type, anon_union_decl);
2028 if (main_decl == error_mark_node)
2029 return;
2030 if (main_decl == NULL_TREE)
2032 pedwarn (input_location, 0, "anonymous union with no members");
2033 return;
2036 if (!processing_template_decl)
2038 /* Use main_decl to set the mangled name. */
2039 DECL_NAME (anon_union_decl) = DECL_NAME (main_decl);
2040 maybe_commonize_var (anon_union_decl);
2041 if (TREE_STATIC (anon_union_decl) || DECL_EXTERNAL (anon_union_decl))
2043 if (DECL_DISCRIMINATOR_P (anon_union_decl))
2044 determine_local_discriminator (anon_union_decl);
2045 mangle_decl (anon_union_decl);
2047 DECL_NAME (anon_union_decl) = NULL_TREE;
2050 pushdecl (anon_union_decl);
2051 cp_finish_decl (anon_union_decl, NULL_TREE, false, NULL_TREE, 0);
2054 /* Auxiliary functions to make type signatures for
2055 `operator new' and `operator delete' correspond to
2056 what compiler will be expecting. */
2058 tree
2059 coerce_new_type (tree type, location_t loc)
2061 int e = 0;
2062 tree args = TYPE_ARG_TYPES (type);
2064 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
2066 if (!same_type_p (TREE_TYPE (type), ptr_type_node))
2068 e = 1;
2069 error_at (loc, "%<operator new%> must return type %qT",
2070 ptr_type_node);
2073 if (args && args != void_list_node)
2075 if (TREE_PURPOSE (args))
2077 /* [basic.stc.dynamic.allocation]
2079 The first parameter shall not have an associated default
2080 argument. */
2081 error_at (loc, "the first parameter of %<operator new%> cannot "
2082 "have a default argument");
2083 /* Throw away the default argument. */
2084 TREE_PURPOSE (args) = NULL_TREE;
2087 if (!same_type_p (TREE_VALUE (args), size_type_node))
2089 e = 2;
2090 args = TREE_CHAIN (args);
2093 else
2094 e = 2;
2096 if (e == 2)
2097 permerror (loc, "%<operator new%> takes type %<size_t%> (%qT) "
2098 "as first parameter", size_type_node);
2100 switch (e)
2102 case 2:
2103 args = tree_cons (NULL_TREE, size_type_node, args);
2104 /* Fall through. */
2105 case 1:
2106 type = (cxx_copy_lang_qualifiers
2107 (build_function_type (ptr_type_node, args),
2108 type));
2109 /* Fall through. */
2110 default:;
2112 return type;
2115 void
2116 coerce_delete_type (tree decl, location_t loc)
2118 int e = 0;
2119 tree type = TREE_TYPE (decl);
2120 tree args = TYPE_ARG_TYPES (type);
2122 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
2124 if (!same_type_p (TREE_TYPE (type), void_type_node))
2126 e = 1;
2127 error_at (loc, "%<operator delete%> must return type %qT",
2128 void_type_node);
2131 tree ptrtype = ptr_type_node;
2132 if (destroying_delete_p (decl))
2134 if (DECL_CLASS_SCOPE_P (decl))
2135 /* If the function is a destroying operator delete declared in class
2136 type C, the type of its first parameter shall be C*. */
2137 ptrtype = build_pointer_type (DECL_CONTEXT (decl));
2138 else
2139 /* A destroying operator delete shall be a class member function named
2140 operator delete. */
2141 error_at (loc,
2142 "destroying %<operator delete%> must be a member function");
2143 const ovl_op_info_t *op = IDENTIFIER_OVL_OP_INFO (DECL_NAME (decl));
2144 if (op->flags & OVL_OP_FLAG_VEC)
2145 error_at (loc, "%<operator delete[]%> cannot be a destroying delete");
2146 if (!usual_deallocation_fn_p (decl))
2147 error_at (loc, "destroying %<operator delete%> must be a usual "
2148 "deallocation function");
2151 if (!args || args == void_list_node
2152 || !same_type_p (TREE_VALUE (args), ptrtype))
2154 e = 2;
2155 if (args && args != void_list_node)
2156 args = TREE_CHAIN (args);
2157 error_at (loc, "%<operator delete%> takes type %qT as first parameter",
2158 ptrtype);
2160 switch (e)
2162 case 2:
2163 args = tree_cons (NULL_TREE, ptrtype, args);
2164 /* Fall through. */
2165 case 1:
2166 type = (cxx_copy_lang_qualifiers
2167 (build_function_type (void_type_node, args),
2168 type));
2169 /* Fall through. */
2170 default:;
2173 TREE_TYPE (decl) = type;
2176 /* DECL is a VAR_DECL for a vtable: walk through the entries in the vtable
2177 and mark them as needed. */
2179 static void
2180 mark_vtable_entries (tree decl, vec<tree> &consteval_vtables)
2182 /* It's OK for the vtable to refer to deprecated virtual functions. */
2183 auto du = make_temp_override (deprecated_state,
2184 UNAVAILABLE_DEPRECATED_SUPPRESS);
2186 bool consteval_seen = false;
2188 for (auto &e: CONSTRUCTOR_ELTS (DECL_INITIAL (decl)))
2190 tree fnaddr = e.value;
2192 STRIP_NOPS (fnaddr);
2194 if (TREE_CODE (fnaddr) != ADDR_EXPR
2195 && TREE_CODE (fnaddr) != FDESC_EXPR)
2196 /* This entry is an offset: a virtual base class offset, a
2197 virtual call offset, an RTTI offset, etc. */
2198 continue;
2200 tree fn = TREE_OPERAND (fnaddr, 0);
2201 if (TREE_CODE (fn) == FUNCTION_DECL && DECL_IMMEDIATE_FUNCTION_P (fn))
2203 if (!consteval_seen)
2205 consteval_seen = true;
2206 consteval_vtables.safe_push (decl);
2208 continue;
2210 TREE_ADDRESSABLE (fn) = 1;
2211 /* When we don't have vcall offsets, we output thunks whenever
2212 we output the vtables that contain them. With vcall offsets,
2213 we know all the thunks we'll need when we emit a virtual
2214 function, so we emit the thunks there instead. */
2215 if (DECL_THUNK_P (fn))
2216 use_thunk (fn, /*emit_p=*/0);
2217 /* Set the location, as marking the function could cause
2218 instantiation. We do not need to preserve the incoming
2219 location, as we're called from c_parse_final_cleanups, which
2220 takes care of that. */
2221 input_location = DECL_SOURCE_LOCATION (fn);
2222 mark_used (fn);
2226 /* Replace any consteval functions in vtables with null pointers. */
2228 static void
2229 clear_consteval_vfns (vec<tree> &consteval_vtables)
2231 for (tree vtable : consteval_vtables)
2232 for (constructor_elt &elt : CONSTRUCTOR_ELTS (DECL_INITIAL (vtable)))
2234 tree fn = cp_get_fndecl_from_callee (elt.value, /*fold*/false);
2235 if (fn && DECL_IMMEDIATE_FUNCTION_P (fn))
2236 elt.value = build_zero_cst (vtable_entry_type);
2240 /* Adjust the TLS model on variable DECL if need be, typically after
2241 the linkage of DECL has been modified. */
2243 static void
2244 adjust_var_decl_tls_model (tree decl)
2246 if (CP_DECL_THREAD_LOCAL_P (decl)
2247 && !lookup_attribute ("tls_model", DECL_ATTRIBUTES (decl)))
2248 set_decl_tls_model (decl, decl_default_tls_model (decl));
2251 /* Set DECL up to have the closest approximation of "initialized common"
2252 linkage available. */
2254 void
2255 comdat_linkage (tree decl)
2257 if (flag_weak)
2259 make_decl_one_only (decl, cxx_comdat_group (decl));
2260 if (HAVE_COMDAT_GROUP && flag_contracts && DECL_CONTRACTS (decl))
2262 symtab_node *n = symtab_node::get (decl);
2263 if (tree pre = DECL_PRE_FN (decl))
2264 cgraph_node::get_create (pre)->add_to_same_comdat_group (n);
2265 if (tree post = DECL_POST_FN (decl))
2266 cgraph_node::get_create (post)->add_to_same_comdat_group (n);
2269 else if (TREE_CODE (decl) == FUNCTION_DECL
2270 || (VAR_P (decl) && DECL_ARTIFICIAL (decl)))
2271 /* We can just emit function and compiler-generated variables
2272 statically; having multiple copies is (for the most part) only
2273 a waste of space.
2275 There are two correctness issues, however: the address of a
2276 template instantiation with external linkage should be the
2277 same, independent of what translation unit asks for the
2278 address, and this will not hold when we emit multiple copies of
2279 the function. However, there's little else we can do.
2281 Also, by default, the typeinfo implementation assumes that
2282 there will be only one copy of the string used as the name for
2283 each type. Therefore, if weak symbols are unavailable, the
2284 run-time library should perform a more conservative check; it
2285 should perform a string comparison, rather than an address
2286 comparison. */
2287 TREE_PUBLIC (decl) = 0;
2288 else
2290 /* Static data member template instantiations, however, cannot
2291 have multiple copies. */
2292 if (DECL_INITIAL (decl) == 0
2293 || DECL_INITIAL (decl) == error_mark_node)
2294 DECL_COMMON (decl) = 1;
2295 else if (EMPTY_CONSTRUCTOR_P (DECL_INITIAL (decl)))
2297 DECL_COMMON (decl) = 1;
2298 DECL_INITIAL (decl) = error_mark_node;
2300 else if (!DECL_EXPLICIT_INSTANTIATION (decl))
2302 /* We can't do anything useful; leave vars for explicit
2303 instantiation. */
2304 DECL_EXTERNAL (decl) = 1;
2305 DECL_NOT_REALLY_EXTERN (decl) = 0;
2309 if (TREE_PUBLIC (decl))
2310 DECL_COMDAT (decl) = 1;
2312 if (VAR_P (decl))
2313 adjust_var_decl_tls_model (decl);
2316 /* For win32 we also want to put explicit instantiations in
2317 linkonce sections, so that they will be merged with implicit
2318 instantiations; otherwise we get duplicate symbol errors.
2319 For Darwin we do not want explicit instantiations to be
2320 linkonce. */
2322 void
2323 maybe_make_one_only (tree decl)
2325 /* We used to say that this was not necessary on targets that support weak
2326 symbols, because the implicit instantiations will defer to the explicit
2327 one. However, that's not actually the case in SVR4; a strong definition
2328 after a weak one is an error. Also, not making explicit
2329 instantiations one_only means that we can end up with two copies of
2330 some template instantiations. */
2331 if (! flag_weak)
2332 return;
2334 /* We can't set DECL_COMDAT on functions, or cp_finish_file will think
2335 we can get away with not emitting them if they aren't used. We need
2336 to for variables so that cp_finish_decl will update their linkage,
2337 because their DECL_INITIAL may not have been set properly yet. */
2339 if (!TARGET_WEAK_NOT_IN_ARCHIVE_TOC
2340 || (! DECL_EXPLICIT_INSTANTIATION (decl)
2341 && ! DECL_TEMPLATE_SPECIALIZATION (decl)))
2343 make_decl_one_only (decl, cxx_comdat_group (decl));
2345 if (VAR_P (decl))
2347 varpool_node *node = varpool_node::get_create (decl);
2348 DECL_COMDAT (decl) = 1;
2349 /* Mark it needed so we don't forget to emit it. */
2350 node->forced_by_abi = true;
2351 TREE_USED (decl) = 1;
2353 adjust_var_decl_tls_model (decl);
2358 /* Returns true iff DECL, a FUNCTION_DECL or VAR_DECL, has vague linkage.
2359 This predicate will give the right answer during parsing of the
2360 function, which other tests may not. */
2362 bool
2363 vague_linkage_p (tree decl)
2365 if (!TREE_PUBLIC (decl))
2367 /* maybe_thunk_body clears TREE_PUBLIC and DECL_ABSTRACT_P on the
2368 maybe-in-charge 'tor variants; in that case we need to check one of
2369 the "clones" for the real linkage. But only in that case; before
2370 maybe_clone_body we haven't yet copied the linkage to the clones. */
2371 if (DECL_MAYBE_IN_CHARGE_CDTOR_P (decl)
2372 && !DECL_ABSTRACT_P (decl)
2373 && DECL_CHAIN (decl)
2374 && DECL_CLONED_FUNCTION_P (DECL_CHAIN (decl)))
2375 return vague_linkage_p (DECL_CHAIN (decl));
2377 gcc_checking_assert (!DECL_COMDAT (decl));
2378 return false;
2380 /* Unfortunately, import_export_decl has not always been called
2381 before the function is processed, so we cannot simply check
2382 DECL_COMDAT. */
2383 if (DECL_COMDAT (decl)
2384 || (TREE_CODE (decl) == FUNCTION_DECL
2385 && DECL_DECLARED_INLINE_P (decl))
2386 || (DECL_LANG_SPECIFIC (decl)
2387 && DECL_TEMPLATE_INSTANTIATION (decl))
2388 || (VAR_P (decl) && DECL_INLINE_VAR_P (decl)))
2389 return true;
2390 else if (DECL_FUNCTION_SCOPE_P (decl))
2391 /* A local static in an inline effectively has vague linkage. */
2392 return (TREE_STATIC (decl)
2393 && vague_linkage_p (DECL_CONTEXT (decl)));
2394 else
2395 return false;
2398 /* Determine whether or not we want to specifically import or export CTYPE,
2399 using various heuristics. */
2401 static void
2402 import_export_class (tree ctype)
2404 /* -1 for imported, 1 for exported. */
2405 int import_export = 0;
2407 /* It only makes sense to call this function at EOF. The reason is
2408 that this function looks at whether or not the first non-inline
2409 non-abstract virtual member function has been defined in this
2410 translation unit. But, we can't possibly know that until we've
2411 seen the entire translation unit. */
2412 gcc_assert (at_eof);
2414 if (CLASSTYPE_INTERFACE_KNOWN (ctype))
2415 return;
2417 /* If MULTIPLE_SYMBOL_SPACES is set and we saw a #pragma interface,
2418 we will have CLASSTYPE_INTERFACE_ONLY set but not
2419 CLASSTYPE_INTERFACE_KNOWN. In that case, we don't want to use this
2420 heuristic because someone will supply a #pragma implementation
2421 elsewhere, and deducing it here would produce a conflict. */
2422 if (CLASSTYPE_INTERFACE_ONLY (ctype))
2423 return;
2425 if (lookup_attribute ("dllimport", TYPE_ATTRIBUTES (ctype)))
2426 import_export = -1;
2427 else if (lookup_attribute ("dllexport", TYPE_ATTRIBUTES (ctype)))
2428 import_export = 1;
2429 else if (CLASSTYPE_IMPLICIT_INSTANTIATION (ctype)
2430 && !flag_implicit_templates)
2431 /* For a template class, without -fimplicit-templates, check the
2432 repository. If the virtual table is assigned to this
2433 translation unit, then export the class; otherwise, import
2434 it. */
2435 import_export = -1;
2436 else if (TYPE_POLYMORPHIC_P (ctype))
2438 tree cdecl = TYPE_NAME (ctype);
2439 if (DECL_LANG_SPECIFIC (cdecl) && DECL_MODULE_ATTACH_P (cdecl))
2440 /* For class types attached to a named module, the ABI specifies
2441 that the tables are uniquely emitted in the object for the
2442 module unit in which it is defined. */
2443 import_export = (DECL_MODULE_IMPORT_P (cdecl) ? -1 : 1);
2444 else
2446 /* The ABI specifies that the virtual table and associated
2447 information are emitted with the key method, if any. */
2448 tree method = CLASSTYPE_KEY_METHOD (ctype);
2449 /* If weak symbol support is not available, then we must be
2450 careful not to emit the vtable when the key function is
2451 inline. An inline function can be defined in multiple
2452 translation units. If we were to emit the vtable in each
2453 translation unit containing a definition, we would get
2454 multiple definition errors at link-time. */
2455 if (method && (flag_weak || ! DECL_DECLARED_INLINE_P (method)))
2456 import_export = (DECL_REALLY_EXTERN (method) ? -1 : 1);
2460 /* When MULTIPLE_SYMBOL_SPACES is set, we cannot count on seeing
2461 a definition anywhere else. */
2462 if (MULTIPLE_SYMBOL_SPACES && import_export == -1)
2463 import_export = 0;
2465 /* Allow back ends the chance to overrule the decision. */
2466 if (targetm.cxx.import_export_class)
2467 import_export = targetm.cxx.import_export_class (ctype, import_export);
2469 if (import_export)
2471 SET_CLASSTYPE_INTERFACE_KNOWN (ctype);
2472 CLASSTYPE_INTERFACE_ONLY (ctype) = (import_export < 0);
2476 /* Return true if VAR has already been provided to the back end; in that
2477 case VAR should not be modified further by the front end. */
2478 static bool
2479 var_finalized_p (tree var)
2481 return varpool_node::get_create (var)->definition;
2484 /* DECL is a VAR_DECL or FUNCTION_DECL which, for whatever reason,
2485 must be emitted in this translation unit. Mark it as such. */
2487 void
2488 mark_needed (tree decl)
2490 TREE_USED (decl) = 1;
2491 if (TREE_CODE (decl) == FUNCTION_DECL)
2493 /* Extern inline functions don't become needed when referenced.
2494 If we know a method will be emitted in other TU and no new
2495 functions can be marked reachable, just use the external
2496 definition. */
2497 struct cgraph_node *node = cgraph_node::get_create (decl);
2498 node->forced_by_abi = true;
2500 /* #pragma interface can call mark_needed for
2501 maybe-in-charge 'tors; mark the clones as well. */
2502 tree clone;
2503 FOR_EACH_CLONE (clone, decl)
2504 mark_needed (clone);
2506 else if (VAR_P (decl))
2508 varpool_node *node = varpool_node::get_create (decl);
2509 /* C++ frontend use mark_decl_references to force COMDAT variables
2510 to be output that might appear dead otherwise. */
2511 node->forced_by_abi = true;
2515 /* DECL is either a FUNCTION_DECL or a VAR_DECL. This function
2516 returns true if a definition of this entity should be provided in
2517 this object file. Callers use this function to determine whether
2518 or not to let the back end know that a definition of DECL is
2519 available in this translation unit. */
2521 bool
2522 decl_needed_p (tree decl)
2524 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
2525 /* This function should only be called at the end of the translation
2526 unit. We cannot be sure of whether or not something will be
2527 COMDAT until that point. */
2528 gcc_assert (at_eof);
2530 /* All entities with external linkage that are not COMDAT/EXTERN should be
2531 emitted; they may be referred to from other object files. */
2532 if (TREE_PUBLIC (decl) && !DECL_COMDAT (decl) && !DECL_REALLY_EXTERN (decl))
2533 return true;
2535 /* Functions marked "dllexport" must be emitted so that they are
2536 visible to other DLLs. */
2537 if (flag_keep_inline_dllexport
2538 && lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl)))
2539 return true;
2541 /* When not optimizing, do not bother to produce definitions for extern
2542 symbols. */
2543 if (DECL_REALLY_EXTERN (decl)
2544 && ((TREE_CODE (decl) != FUNCTION_DECL
2545 && !optimize)
2546 || (TREE_CODE (decl) == FUNCTION_DECL
2547 && !opt_for_fn (decl, optimize)))
2548 && !lookup_attribute ("always_inline", decl))
2549 return false;
2551 /* If this entity was used, let the back end see it; it will decide
2552 whether or not to emit it into the object file. */
2553 if (TREE_USED (decl))
2554 return true;
2556 /* Virtual functions might be needed for devirtualization. */
2557 if (flag_devirtualize
2558 && TREE_CODE (decl) == FUNCTION_DECL
2559 && DECL_VIRTUAL_P (decl))
2560 return true;
2562 /* Otherwise, DECL does not need to be emitted -- yet. A subsequent
2563 reference to DECL might cause it to be emitted later. */
2564 return false;
2567 /* If necessary, write out the vtables for the dynamic class CTYPE.
2568 Returns true if any vtables were emitted. */
2570 static bool
2571 maybe_emit_vtables (tree ctype, vec<tree> &consteval_vtables)
2573 tree vtbl;
2574 tree primary_vtbl;
2575 int needed = 0;
2576 varpool_node *current = NULL, *last = NULL;
2578 /* If the vtables for this class have already been emitted there is
2579 nothing more to do. */
2580 primary_vtbl = CLASSTYPE_VTABLES (ctype);
2581 if (var_finalized_p (primary_vtbl))
2582 return false;
2583 /* Ignore dummy vtables made by get_vtable_decl. */
2584 if (TREE_TYPE (primary_vtbl) == void_type_node)
2585 return false;
2587 /* On some targets, we cannot determine the key method until the end
2588 of the translation unit -- which is when this function is
2589 called. */
2590 if (!targetm.cxx.key_method_may_be_inline ())
2591 determine_key_method (ctype);
2593 /* See if any of the vtables are needed. */
2594 for (vtbl = CLASSTYPE_VTABLES (ctype); vtbl; vtbl = DECL_CHAIN (vtbl))
2596 import_export_decl (vtbl);
2597 if (DECL_NOT_REALLY_EXTERN (vtbl) && decl_needed_p (vtbl))
2598 needed = 1;
2600 if (!needed)
2602 /* If the references to this class' vtables are optimized away,
2603 still emit the appropriate debugging information. See
2604 dfs_debug_mark. */
2605 if (DECL_COMDAT (primary_vtbl)
2606 && CLASSTYPE_DEBUG_REQUESTED (ctype))
2607 note_debug_info_needed (ctype);
2608 return false;
2611 /* The ABI requires that we emit all of the vtables if we emit any
2612 of them. */
2613 for (vtbl = CLASSTYPE_VTABLES (ctype); vtbl; vtbl = DECL_CHAIN (vtbl))
2615 /* Mark entities references from the virtual table as used. */
2616 mark_vtable_entries (vtbl, consteval_vtables);
2618 if (TREE_TYPE (DECL_INITIAL (vtbl)) == 0)
2620 vec<tree, va_gc> *cleanups = NULL;
2621 tree expr = store_init_value (vtbl, DECL_INITIAL (vtbl), &cleanups,
2622 LOOKUP_NORMAL);
2624 /* It had better be all done at compile-time. */
2625 gcc_assert (!expr && !cleanups);
2628 /* Write it out. */
2629 DECL_EXTERNAL (vtbl) = 0;
2630 rest_of_decl_compilation (vtbl, 1, 1);
2632 /* Because we're only doing syntax-checking, we'll never end up
2633 actually marking the variable as written. */
2634 if (flag_syntax_only)
2635 TREE_ASM_WRITTEN (vtbl) = 1;
2636 else if (DECL_ONE_ONLY (vtbl))
2638 current = varpool_node::get_create (vtbl);
2639 if (last)
2640 current->add_to_same_comdat_group (last);
2641 last = current;
2645 /* For abstract classes, the destructor has been removed from the
2646 vtable (in class.cc's build_vtbl_initializer). For a compiler-
2647 generated destructor, it hence might not have been generated in
2648 this translation unit - and with '#pragma interface' it might
2649 never get generated. */
2650 if (CLASSTYPE_PURE_VIRTUALS (ctype)
2651 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (ctype)
2652 && !CLASSTYPE_LAZY_DESTRUCTOR (ctype)
2653 && DECL_DEFAULTED_IN_CLASS_P (CLASSTYPE_DESTRUCTOR (ctype)))
2654 note_vague_linkage_fn (CLASSTYPE_DESTRUCTOR (ctype));
2656 /* Since we're writing out the vtable here, also write the debug
2657 info. */
2658 note_debug_info_needed (ctype);
2660 return true;
2663 /* A special return value from type_visibility meaning internal
2664 linkage. */
2666 enum { VISIBILITY_ANON = VISIBILITY_INTERNAL+1 };
2668 static int expr_visibility (tree);
2669 static int type_visibility (tree);
2671 /* walk_tree helper function for type_visibility. */
2673 static tree
2674 min_vis_r (tree *tp, int *walk_subtrees, void *data)
2676 int *vis_p = (int *)data;
2677 int this_vis = VISIBILITY_DEFAULT;
2678 if (! TYPE_P (*tp))
2679 *walk_subtrees = 0;
2680 else if (OVERLOAD_TYPE_P (*tp)
2681 && !TREE_PUBLIC (TYPE_MAIN_DECL (*tp)))
2683 this_vis = VISIBILITY_ANON;
2684 *walk_subtrees = 0;
2686 else if (CLASS_TYPE_P (*tp))
2688 this_vis = CLASSTYPE_VISIBILITY (*tp);
2689 *walk_subtrees = 0;
2691 else if (TREE_CODE (*tp) == ARRAY_TYPE
2692 && uses_template_parms (TYPE_DOMAIN (*tp)))
2693 this_vis = expr_visibility (TYPE_MAX_VALUE (TYPE_DOMAIN (*tp)));
2695 if (this_vis > *vis_p)
2696 *vis_p = this_vis;
2698 /* Tell cp_walk_subtrees to look through typedefs. */
2699 if (*walk_subtrees == 1)
2700 *walk_subtrees = 2;
2702 return NULL;
2705 /* walk_tree helper function for expr_visibility. */
2707 static tree
2708 min_vis_expr_r (tree *tp, int */*walk_subtrees*/, void *data)
2710 int *vis_p = (int *)data;
2711 int tpvis = VISIBILITY_DEFAULT;
2713 tree t = *tp;
2714 if (TREE_CODE (t) == PTRMEM_CST)
2715 t = PTRMEM_CST_MEMBER (t);
2716 switch (TREE_CODE (t))
2718 case CAST_EXPR:
2719 case IMPLICIT_CONV_EXPR:
2720 case STATIC_CAST_EXPR:
2721 case REINTERPRET_CAST_EXPR:
2722 case CONST_CAST_EXPR:
2723 case DYNAMIC_CAST_EXPR:
2724 case NEW_EXPR:
2725 case CONSTRUCTOR:
2726 case LAMBDA_EXPR:
2727 tpvis = type_visibility (TREE_TYPE (t));
2728 break;
2730 case TEMPLATE_DECL:
2731 if (DECL_ALIAS_TEMPLATE_P (t) || concept_definition_p (t))
2732 /* FIXME: We don't maintain TREE_PUBLIC / DECL_VISIBILITY for
2733 alias templates so we can't trust it here (PR107906). Ditto
2734 for concepts. */
2735 break;
2736 t = DECL_TEMPLATE_RESULT (t);
2737 /* Fall through. */
2738 case VAR_DECL:
2739 case FUNCTION_DECL:
2740 if (decl_constant_var_p (t))
2741 /* The ODR allows definitions in different TUs to refer to distinct
2742 constant variables with internal or no linkage, so such a reference
2743 shouldn't affect visibility (PR110323). FIXME but only if the
2744 lvalue-rvalue conversion is applied. */;
2745 else if (! TREE_PUBLIC (t))
2746 tpvis = VISIBILITY_ANON;
2747 else
2748 tpvis = DECL_VISIBILITY (t);
2749 break;
2751 case FIELD_DECL:
2752 tpvis = type_visibility (DECL_CONTEXT (t));
2753 break;
2755 default:
2756 break;
2759 if (tpvis > *vis_p)
2760 *vis_p = tpvis;
2762 return NULL_TREE;
2765 /* Returns the visibility of TYPE, which is the minimum visibility of its
2766 component types. */
2768 static int
2769 type_visibility (tree type)
2771 int vis = VISIBILITY_DEFAULT;
2772 cp_walk_tree_without_duplicates (&type, min_vis_r, &vis);
2773 return vis;
2776 /* Returns the visibility of an expression EXPR that appears in the signature
2777 of a function template, which is the minimum visibility of names that appear
2778 in its mangling. */
2780 static int
2781 expr_visibility (tree expr)
2783 int vis = VISIBILITY_DEFAULT;
2784 cp_walk_tree_without_duplicates (&expr, min_vis_expr_r, &vis);
2785 return vis;
2788 /* Limit the visibility of DECL to VISIBILITY, if not explicitly
2789 specified (or if VISIBILITY is static). If TMPL is true, this
2790 constraint is for a template argument, and takes precedence
2791 over explicitly-specified visibility on the template. */
2793 static void
2794 constrain_visibility (tree decl, int visibility, bool tmpl)
2796 if (visibility == VISIBILITY_ANON)
2798 /* extern "C" declarations aren't affected by the anonymous
2799 namespace. */
2800 if (!DECL_EXTERN_C_P (decl))
2802 TREE_PUBLIC (decl) = 0;
2803 DECL_WEAK (decl) = 0;
2804 DECL_COMMON (decl) = 0;
2805 DECL_COMDAT (decl) = false;
2806 if (VAR_OR_FUNCTION_DECL_P (decl))
2808 struct symtab_node *snode = symtab_node::get (decl);
2810 if (snode)
2811 snode->set_comdat_group (NULL);
2813 DECL_INTERFACE_KNOWN (decl) = 1;
2814 if (DECL_LANG_SPECIFIC (decl))
2815 DECL_NOT_REALLY_EXTERN (decl) = 1;
2818 else if (visibility > DECL_VISIBILITY (decl)
2819 && (tmpl || !DECL_VISIBILITY_SPECIFIED (decl)))
2821 DECL_VISIBILITY (decl) = (enum symbol_visibility) visibility;
2822 /* This visibility was not specified. */
2823 DECL_VISIBILITY_SPECIFIED (decl) = false;
2827 /* Constrain the visibility of DECL based on the visibility of its template
2828 arguments. */
2830 static void
2831 constrain_visibility_for_template (tree decl, tree targs)
2833 /* If this is a template instantiation, check the innermost
2834 template args for visibility constraints. The outer template
2835 args are covered by the class check. */
2836 tree args = INNERMOST_TEMPLATE_ARGS (targs);
2837 int i;
2838 for (i = TREE_VEC_LENGTH (args); i > 0; --i)
2840 int vis = 0;
2842 tree arg = TREE_VEC_ELT (args, i-1);
2843 if (TYPE_P (arg))
2844 vis = type_visibility (arg);
2845 else
2846 vis = expr_visibility (arg);
2847 if (vis)
2848 constrain_visibility (decl, vis, true);
2852 /* Like c_determine_visibility, but with additional C++-specific
2853 behavior.
2855 Function-scope entities can rely on the function's visibility because
2856 it is set in start_preparsed_function.
2858 Class-scope entities cannot rely on the class's visibility until the end
2859 of the enclosing class definition.
2861 Note that because namespaces have multiple independent definitions,
2862 namespace visibility is handled elsewhere using the #pragma visibility
2863 machinery rather than by decorating the namespace declaration.
2865 The goal is for constraints from the type to give a diagnostic, and
2866 other constraints to be applied silently. */
2868 void
2869 determine_visibility (tree decl)
2871 /* Remember that all decls get VISIBILITY_DEFAULT when built. */
2873 /* Only relevant for names with external linkage. */
2874 if (!TREE_PUBLIC (decl))
2875 return;
2877 /* Cloned constructors and destructors get the same visibility as
2878 the underlying function. That should be set up in
2879 maybe_clone_body. */
2880 gcc_assert (!DECL_CLONED_FUNCTION_P (decl));
2882 bool orig_visibility_specified = DECL_VISIBILITY_SPECIFIED (decl);
2883 enum symbol_visibility orig_visibility = DECL_VISIBILITY (decl);
2885 /* The decl may be a template instantiation, which could influence
2886 visibilty. */
2887 tree template_decl = NULL_TREE;
2888 if (TREE_CODE (decl) == TYPE_DECL)
2890 if (CLASS_TYPE_P (TREE_TYPE (decl)))
2892 if (CLASSTYPE_USE_TEMPLATE (TREE_TYPE (decl)))
2893 template_decl = decl;
2895 else if (TYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
2896 template_decl = decl;
2898 else if (DECL_LANG_SPECIFIC (decl) && DECL_USE_TEMPLATE (decl))
2899 template_decl = decl;
2901 if (TREE_CODE (decl) == TYPE_DECL
2902 && LAMBDA_TYPE_P (TREE_TYPE (decl))
2903 && CLASSTYPE_LAMBDA_EXPR (TREE_TYPE (decl)) != error_mark_node)
2904 if (tree extra = LAMBDA_TYPE_EXTRA_SCOPE (TREE_TYPE (decl)))
2906 /* The lambda's visibility is limited by that of its extra
2907 scope. */
2908 int vis = 0;
2909 if (TYPE_P (extra))
2910 vis = type_visibility (extra);
2911 else
2912 vis = expr_visibility (extra);
2913 constrain_visibility (decl, vis, false);
2916 /* If DECL is a member of a class, visibility specifiers on the
2917 class can influence the visibility of the DECL. */
2918 tree class_type = NULL_TREE;
2919 if (DECL_CLASS_SCOPE_P (decl))
2920 class_type = DECL_CONTEXT (decl);
2921 else
2923 /* Not a class member. */
2925 /* Virtual tables have DECL_CONTEXT set to their associated class,
2926 so they are automatically handled above. */
2927 gcc_assert (!VAR_P (decl)
2928 || !DECL_VTABLE_OR_VTT_P (decl));
2930 if (DECL_FUNCTION_SCOPE_P (decl) && ! DECL_VISIBILITY_SPECIFIED (decl))
2932 /* Local statics and classes get the visibility of their
2933 containing function by default, except that
2934 -fvisibility-inlines-hidden doesn't affect them. */
2935 tree fn = DECL_CONTEXT (decl);
2936 if (DECL_VISIBILITY_SPECIFIED (fn))
2938 DECL_VISIBILITY (decl) = DECL_VISIBILITY (fn);
2939 DECL_VISIBILITY_SPECIFIED (decl) =
2940 DECL_VISIBILITY_SPECIFIED (fn);
2942 else
2944 if (DECL_CLASS_SCOPE_P (fn))
2945 determine_visibility_from_class (decl, DECL_CONTEXT (fn));
2946 else if (determine_hidden_inline (fn))
2948 DECL_VISIBILITY (decl) = default_visibility;
2949 DECL_VISIBILITY_SPECIFIED (decl) =
2950 visibility_options.inpragma;
2952 else
2954 DECL_VISIBILITY (decl) = DECL_VISIBILITY (fn);
2955 DECL_VISIBILITY_SPECIFIED (decl) =
2956 DECL_VISIBILITY_SPECIFIED (fn);
2960 /* Local classes in templates have CLASSTYPE_USE_TEMPLATE set,
2961 but have no TEMPLATE_INFO, so don't try to check it. */
2962 template_decl = NULL_TREE;
2964 else if (VAR_P (decl) && DECL_TINFO_P (decl)
2965 && flag_visibility_ms_compat)
2967 /* Under -fvisibility-ms-compat, types are visible by default,
2968 even though their contents aren't. */
2969 tree underlying_type = TREE_TYPE (DECL_NAME (decl));
2970 int underlying_vis = type_visibility (underlying_type);
2971 if (underlying_vis == VISIBILITY_ANON
2972 || (CLASS_TYPE_P (underlying_type)
2973 && CLASSTYPE_VISIBILITY_SPECIFIED (underlying_type)))
2974 constrain_visibility (decl, underlying_vis, false);
2975 else
2976 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
2978 else if (VAR_P (decl) && DECL_TINFO_P (decl))
2980 /* tinfo visibility is based on the type it's for. */
2981 constrain_visibility
2982 (decl, type_visibility (TREE_TYPE (DECL_NAME (decl))), false);
2984 /* Give the target a chance to override the visibility associated
2985 with DECL. */
2986 if (TREE_PUBLIC (decl)
2987 && !DECL_REALLY_EXTERN (decl)
2988 && CLASS_TYPE_P (TREE_TYPE (DECL_NAME (decl)))
2989 && !CLASSTYPE_VISIBILITY_SPECIFIED (TREE_TYPE (DECL_NAME (decl))))
2990 targetm.cxx.determine_class_data_visibility (decl);
2992 else if (template_decl)
2993 /* Template instantiations and specializations get visibility based
2994 on their template unless they override it with an attribute. */;
2995 else if (! DECL_VISIBILITY_SPECIFIED (decl))
2997 if (determine_hidden_inline (decl))
2998 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
2999 else
3001 /* Set default visibility to whatever the user supplied with
3002 #pragma GCC visibility or a namespace visibility attribute. */
3003 DECL_VISIBILITY (decl) = default_visibility;
3004 DECL_VISIBILITY_SPECIFIED (decl) = visibility_options.inpragma;
3009 if (template_decl)
3011 /* If the specialization doesn't specify visibility, use the
3012 visibility from the template. */
3013 tree tinfo = get_template_info (template_decl);
3014 tree args = TI_ARGS (tinfo);
3015 tree attribs = (TREE_CODE (decl) == TYPE_DECL
3016 ? TYPE_ATTRIBUTES (TREE_TYPE (decl))
3017 : DECL_ATTRIBUTES (decl));
3018 tree attr = lookup_attribute ("visibility", attribs);
3020 if (args != error_mark_node)
3022 tree pattern = DECL_TEMPLATE_RESULT (TI_TEMPLATE (tinfo));
3024 if (!DECL_VISIBILITY_SPECIFIED (decl))
3026 if (!attr
3027 && determine_hidden_inline (decl))
3028 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
3029 else
3031 DECL_VISIBILITY (decl) = DECL_VISIBILITY (pattern);
3032 DECL_VISIBILITY_SPECIFIED (decl)
3033 = DECL_VISIBILITY_SPECIFIED (pattern);
3037 if (args
3038 /* Template argument visibility outweighs #pragma or namespace
3039 visibility, but not an explicit attribute. */
3040 && !attr)
3042 int depth = TMPL_ARGS_DEPTH (args);
3043 if (DECL_VISIBILITY_SPECIFIED (decl))
3045 /* A class template member with explicit visibility
3046 overrides the class visibility, so we need to apply
3047 all the levels of template args directly. */
3048 int i;
3049 for (i = 1; i <= depth; ++i)
3051 tree lev = TMPL_ARGS_LEVEL (args, i);
3052 constrain_visibility_for_template (decl, lev);
3055 else if (PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo)))
3056 /* Limit visibility based on its template arguments. */
3057 constrain_visibility_for_template (decl, args);
3062 if (class_type)
3063 determine_visibility_from_class (decl, class_type);
3065 if (decl_internal_context_p (decl))
3066 /* Names in an anonymous namespace get internal linkage. */
3067 constrain_visibility (decl, VISIBILITY_ANON, false);
3068 else if (TREE_CODE (decl) != TYPE_DECL)
3070 /* Propagate anonymity from type to decl. */
3071 int tvis = type_visibility (TREE_TYPE (decl));
3072 if (tvis == VISIBILITY_ANON
3073 || ! DECL_VISIBILITY_SPECIFIED (decl))
3074 constrain_visibility (decl, tvis, false);
3076 else if (no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/true))
3077 /* DR 757: A type without linkage shall not be used as the type of a
3078 variable or function with linkage, unless
3079 o the variable or function has extern "C" linkage (7.5 [dcl.link]), or
3080 o the variable or function is not used (3.2 [basic.def.odr]) or is
3081 defined in the same translation unit.
3083 Since non-extern "C" decls need to be defined in the same
3084 translation unit, we can make the type internal. */
3085 constrain_visibility (decl, VISIBILITY_ANON, false);
3087 /* If visibility changed and DECL already has DECL_RTL, ensure
3088 symbol flags are updated. */
3089 if ((DECL_VISIBILITY (decl) != orig_visibility
3090 || DECL_VISIBILITY_SPECIFIED (decl) != orig_visibility_specified)
3091 && ((VAR_P (decl) && TREE_STATIC (decl))
3092 || TREE_CODE (decl) == FUNCTION_DECL)
3093 && DECL_RTL_SET_P (decl))
3094 make_decl_rtl (decl);
3097 /* By default, static data members and function members receive
3098 the visibility of their containing class. */
3100 static void
3101 determine_visibility_from_class (tree decl, tree class_type)
3103 if (DECL_VISIBILITY_SPECIFIED (decl))
3104 return;
3106 if (determine_hidden_inline (decl))
3107 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
3108 else
3110 /* Default to the class visibility. */
3111 DECL_VISIBILITY (decl) = CLASSTYPE_VISIBILITY (class_type);
3112 DECL_VISIBILITY_SPECIFIED (decl)
3113 = CLASSTYPE_VISIBILITY_SPECIFIED (class_type);
3116 /* Give the target a chance to override the visibility associated
3117 with DECL. */
3118 if (VAR_P (decl)
3119 && TREE_PUBLIC (decl)
3120 && (DECL_TINFO_P (decl) || DECL_VTABLE_OR_VTT_P (decl))
3121 && !DECL_REALLY_EXTERN (decl)
3122 && !CLASSTYPE_VISIBILITY_SPECIFIED (class_type))
3123 targetm.cxx.determine_class_data_visibility (decl);
3126 /* Returns true iff DECL is an inline that should get hidden visibility
3127 because of -fvisibility-inlines-hidden. */
3129 static bool
3130 determine_hidden_inline (tree decl)
3132 return (visibility_options.inlines_hidden
3133 /* Don't do this for inline templates; specializations might not be
3134 inline, and we don't want them to inherit the hidden
3135 visibility. We'll set it here for all inline instantiations. */
3136 && !processing_template_decl
3137 && TREE_CODE (decl) == FUNCTION_DECL
3138 && DECL_DECLARED_INLINE_P (decl)
3139 && (! DECL_LANG_SPECIFIC (decl)
3140 || ! DECL_EXPLICIT_INSTANTIATION (decl)));
3143 /* Constrain the visibility of a class TYPE based on the visibility of its
3144 field types. Warn if any fields require lesser visibility. */
3146 void
3147 constrain_class_visibility (tree type)
3149 tree binfo;
3150 tree t;
3151 int i;
3153 int vis = type_visibility (type);
3155 if (vis == VISIBILITY_ANON
3156 || DECL_IN_SYSTEM_HEADER (TYPE_MAIN_DECL (type)))
3157 return;
3159 /* Don't warn about visibility if the class has explicit visibility. */
3160 if (CLASSTYPE_VISIBILITY_SPECIFIED (type))
3161 vis = VISIBILITY_INTERNAL;
3163 for (t = TYPE_FIELDS (type); t; t = DECL_CHAIN (t))
3164 if (TREE_CODE (t) == FIELD_DECL && TREE_TYPE (t) != error_mark_node
3165 && !DECL_ARTIFICIAL (t))
3167 tree ftype = strip_pointer_or_array_types (TREE_TYPE (t));
3168 int subvis = type_visibility (ftype);
3170 if (subvis == VISIBILITY_ANON)
3172 if (!in_main_input_context())
3174 tree nlt = no_linkage_check (ftype, /*relaxed_p=*/false);
3175 if (nlt)
3177 if (same_type_p (TREE_TYPE (t), nlt))
3178 warning (OPT_Wsubobject_linkage, "\
3179 %qT has a field %q#D whose type has no linkage",
3180 type, t);
3181 else
3182 warning (OPT_Wsubobject_linkage, "\
3183 %qT has a field %qD whose type depends on the type %qT which has no linkage",
3184 type, t, nlt);
3186 else if (cxx_dialect > cxx98
3187 && !decl_anon_ns_mem_p (ftype))
3188 warning (OPT_Wsubobject_linkage, "\
3189 %qT has a field %q#D whose type has internal linkage",
3190 type, t);
3191 else // In C++98 this can only happen with unnamed namespaces.
3192 warning (OPT_Wsubobject_linkage, "\
3193 %qT has a field %q#D whose type uses the anonymous namespace",
3194 type, t);
3197 else if (MAYBE_CLASS_TYPE_P (ftype)
3198 && vis < VISIBILITY_HIDDEN
3199 && subvis >= VISIBILITY_HIDDEN)
3200 warning (OPT_Wattributes, "\
3201 %qT declared with greater visibility than the type of its field %qD",
3202 type, t);
3205 binfo = TYPE_BINFO (type);
3206 for (i = 0; BINFO_BASE_ITERATE (binfo, i, t); ++i)
3208 tree btype = BINFO_TYPE (t);
3209 int subvis = type_visibility (btype);
3211 if (subvis == VISIBILITY_ANON)
3213 if (!in_main_input_context())
3215 tree nlt = no_linkage_check (btype, /*relaxed_p=*/false);
3216 if (nlt)
3218 if (same_type_p (btype, nlt))
3219 warning (OPT_Wsubobject_linkage, "\
3220 %qT has a base %qT which has no linkage",
3221 type, btype);
3222 else
3223 warning (OPT_Wsubobject_linkage, "\
3224 %qT has a base %qT which depends on the type %qT which has no linkage",
3225 type, btype, nlt);
3227 else if (cxx_dialect > cxx98
3228 && !decl_anon_ns_mem_p (btype))
3229 warning (OPT_Wsubobject_linkage, "\
3230 %qT has a base %qT which has internal linkage",
3231 type, btype);
3232 else // In C++98 this can only happen with unnamed namespaces.
3233 warning (OPT_Wsubobject_linkage, "\
3234 %qT has a base %qT which uses the anonymous namespace",
3235 type, btype);
3238 else if (vis < VISIBILITY_HIDDEN
3239 && subvis >= VISIBILITY_HIDDEN)
3240 warning (OPT_Wattributes, "\
3241 %qT declared with greater visibility than its base %qT",
3242 type, TREE_TYPE (t));
3246 /* Functions for adjusting the visibility of a tagged type and its nested
3247 types and declarations when it gets a name for linkage purposes from a
3248 typedef. */
3249 // FIXME: It is now a DR for such a class type to contain anything
3250 // other than C. So at minium most of this can probably be deleted.
3252 /* First reset the visibility of all the types. */
3254 static void
3255 reset_type_linkage_1 (tree type)
3257 set_linkage_according_to_type (type, TYPE_MAIN_DECL (type));
3258 if (CLASS_TYPE_P (type))
3259 for (tree member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
3260 if (DECL_IMPLICIT_TYPEDEF_P (member))
3261 reset_type_linkage_1 (TREE_TYPE (member));
3264 /* Then reset the visibility of any static data members or member
3265 functions that use those types. */
3267 static void
3268 reset_decl_linkage (tree decl)
3270 if (TREE_PUBLIC (decl))
3271 return;
3272 if (DECL_CLONED_FUNCTION_P (decl))
3273 return;
3274 TREE_PUBLIC (decl) = true;
3275 DECL_INTERFACE_KNOWN (decl) = false;
3276 determine_visibility (decl);
3277 tentative_decl_linkage (decl);
3280 void
3281 reset_type_linkage (tree type)
3283 reset_type_linkage_1 (type);
3284 if (CLASS_TYPE_P (type))
3286 if (tree vt = CLASSTYPE_VTABLES (type))
3288 tree name = mangle_vtbl_for_type (type);
3289 DECL_NAME (vt) = name;
3290 SET_DECL_ASSEMBLER_NAME (vt, name);
3291 reset_decl_linkage (vt);
3293 if (!ANON_AGGR_TYPE_P (type))
3294 if (tree ti = CLASSTYPE_TYPEINFO_VAR (type))
3296 tree name = mangle_typeinfo_for_type (type);
3297 DECL_NAME (ti) = name;
3298 SET_DECL_ASSEMBLER_NAME (ti, name);
3299 TREE_TYPE (name) = type;
3300 reset_decl_linkage (ti);
3302 for (tree m = TYPE_FIELDS (type); m; m = DECL_CHAIN (m))
3304 tree mem = STRIP_TEMPLATE (m);
3305 if (TREE_CODE (mem) == VAR_DECL || TREE_CODE (mem) == FUNCTION_DECL)
3306 reset_decl_linkage (mem);
3307 else if (DECL_IMPLICIT_TYPEDEF_P (mem))
3308 reset_type_linkage (TREE_TYPE (mem));
3313 /* Set up our initial idea of what the linkage of DECL should be. */
3315 void
3316 tentative_decl_linkage (tree decl)
3318 if (DECL_INTERFACE_KNOWN (decl))
3319 /* We've already made a decision as to how this function will
3320 be handled. */;
3321 else if (vague_linkage_p (decl))
3323 if (TREE_CODE (decl) == FUNCTION_DECL
3324 && decl_defined_p (decl))
3326 DECL_EXTERNAL (decl) = 1;
3327 DECL_NOT_REALLY_EXTERN (decl) = 1;
3328 note_vague_linkage_fn (decl);
3329 /* A non-template inline function with external linkage will
3330 always be COMDAT. As we must eventually determine the
3331 linkage of all functions, and as that causes writes to
3332 the data mapped in from the PCH file, it's advantageous
3333 to mark the functions at this point. */
3334 if (DECL_DECLARED_INLINE_P (decl))
3336 if (!DECL_IMPLICIT_INSTANTIATION (decl)
3337 || DECL_DEFAULTED_FN (decl))
3339 /* This function must have external linkage, as
3340 otherwise DECL_INTERFACE_KNOWN would have been
3341 set. */
3342 gcc_assert (TREE_PUBLIC (decl));
3343 comdat_linkage (decl);
3344 DECL_INTERFACE_KNOWN (decl) = 1;
3346 else if (DECL_MAYBE_IN_CHARGE_CDTOR_P (decl))
3347 /* For implicit instantiations of cdtors try to make
3348 it comdat, so that maybe_clone_body can use aliases.
3349 See PR113208. */
3350 maybe_make_one_only (decl);
3353 else if (VAR_P (decl))
3354 maybe_commonize_var (decl);
3358 /* For a polymorphic class type CTYPE, whether its vtables are emitted in a
3359 unique object as per the ABI. */
3361 static bool
3362 vtables_uniquely_emitted (tree ctype)
3364 /* If the class is templated, the tables are emitted in every object that
3365 references any of them. */
3366 if (CLASSTYPE_USE_TEMPLATE (ctype))
3367 return false;
3369 /* Otherwise, if the class is attached to a module, the tables are uniquely
3370 emitted in the object for the module unit in which it is defined. */
3371 tree cdecl = TYPE_NAME (ctype);
3372 if (DECL_LANG_SPECIFIC (cdecl) && DECL_MODULE_ATTACH_P (cdecl))
3373 return true;
3375 /* Otherwise, if the class has a key function, the tables are emitted in the
3376 object for the TU containing the definition of the key function. This is
3377 unique if the key function is not inline. */
3378 tree key_method = CLASSTYPE_KEY_METHOD (ctype);
3379 if (key_method && !DECL_DECLARED_INLINE_P (key_method))
3380 return true;
3382 /* Otherwise, the tables are emitted in every object that references
3383 any of them. */
3384 return false;
3387 /* DECL is a FUNCTION_DECL or VAR_DECL. If the object file linkage
3388 for DECL has not already been determined, do so now by setting
3389 DECL_EXTERNAL, DECL_COMDAT and other related flags. Until this
3390 function is called entities with vague linkage whose definitions
3391 are available must have TREE_PUBLIC set.
3393 If this function decides to place DECL in COMDAT, it will set
3394 appropriate flags -- but will not clear DECL_EXTERNAL. It is up to
3395 the caller to decide whether or not to clear DECL_EXTERNAL. Some
3396 callers defer that decision until it is clear that DECL is actually
3397 required. */
3399 void
3400 import_export_decl (tree decl)
3402 bool comdat_p;
3403 bool import_p;
3404 tree class_type = NULL_TREE;
3406 if (DECL_INTERFACE_KNOWN (decl))
3407 return;
3409 /* We cannot determine what linkage to give to an entity with vague
3410 linkage until the end of the file. For example, a virtual table
3411 for a class will be defined if and only if the key method is
3412 defined in this translation unit. */
3413 gcc_assert (at_eof);
3414 /* Object file linkage for explicit instantiations is handled in
3415 mark_decl_instantiated. For static variables in functions with
3416 vague linkage, maybe_commonize_var is used.
3418 Therefore, the only declarations that should be provided to this
3419 function are those with external linkage that are:
3421 * implicit instantiations of function templates
3423 * inline functions
3425 * inline variables
3427 * implicit instantiations of static data members of class
3428 templates
3430 * virtual tables
3432 * typeinfo objects
3434 Furthermore, all entities that reach this point must have a
3435 definition available in this translation unit.
3437 The following assertions check these conditions. */
3438 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
3439 /* Any code that creates entities with TREE_PUBLIC cleared should
3440 also set DECL_INTERFACE_KNOWN. */
3441 gcc_assert (TREE_PUBLIC (decl));
3442 if (TREE_CODE (decl) == FUNCTION_DECL)
3443 gcc_assert (DECL_IMPLICIT_INSTANTIATION (decl)
3444 || DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl)
3445 || DECL_DECLARED_INLINE_P (decl));
3446 else
3447 gcc_assert (DECL_IMPLICIT_INSTANTIATION (decl)
3448 || DECL_INLINE_VAR_P (decl)
3449 || DECL_VTABLE_OR_VTT_P (decl)
3450 || DECL_TINFO_P (decl));
3451 /* Check that a definition of DECL is available in this translation
3452 unit. */
3453 gcc_assert (!DECL_REALLY_EXTERN (decl));
3455 /* Assume that DECL will not have COMDAT linkage. */
3456 comdat_p = false;
3457 /* Assume that DECL will not be imported into this translation
3458 unit. */
3459 import_p = false;
3461 if (VAR_P (decl) && DECL_VTABLE_OR_VTT_P (decl))
3463 class_type = DECL_CONTEXT (decl);
3464 import_export_class (class_type);
3465 if (CLASSTYPE_INTERFACE_KNOWN (class_type)
3466 && CLASSTYPE_INTERFACE_ONLY (class_type))
3467 import_p = true;
3468 else if ((!flag_weak || TARGET_WEAK_NOT_IN_ARCHIVE_TOC)
3469 && !vtables_uniquely_emitted (class_type))
3470 /* The ABI historically required that all virtual tables be
3471 emitted with COMDAT linkage. However, on systems where
3472 COMDAT symbols don't show up in the table of contents for
3473 a static archive, or on systems without weak symbols (where
3474 we approximate COMDAT linkage by using internal linkage),
3475 the linker will report errors about undefined symbols because
3476 it will not see the virtual table definition. Therefore,
3477 in the case that we know that the virtual table will be
3478 emitted in only one translation unit, we make the virtual
3479 table an ordinary definition with external linkage. */
3480 DECL_EXTERNAL (decl) = 0;
3481 else if (CLASSTYPE_INTERFACE_KNOWN (class_type))
3483 /* CLASS_TYPE is being exported from this translation unit,
3484 so DECL should be defined here. */
3485 if (!flag_weak && CLASSTYPE_EXPLICIT_INSTANTIATION (class_type))
3486 /* If a class is declared in a header with the "extern
3487 template" extension, then it will not be instantiated,
3488 even in translation units that would normally require
3489 it. Often such classes are explicitly instantiated in
3490 one translation unit. Therefore, the explicit
3491 instantiation must be made visible to other translation
3492 units. */
3493 DECL_EXTERNAL (decl) = 0;
3494 else
3496 /* The generic C++ ABI used to say that class data is always
3497 COMDAT, even if emitted in a unique object. This is no
3498 longer true, but for now we continue to do so for
3499 compatibility with programs that are not strictly valid.
3500 However, some variants (e.g., the ARM EABI) explicitly say
3501 that class data only has COMDAT linkage if the class data
3502 might be emitted in more than one translation unit.
3503 When the key method can be inline and is inline, we still
3504 have to arrange for comdat even though
3505 class_data_always_comdat is false. */
3506 if (!vtables_uniquely_emitted (class_type)
3507 || targetm.cxx.class_data_always_comdat ())
3509 /* The ABI requires COMDAT linkage. Normally, we
3510 only emit COMDAT things when they are needed;
3511 make sure that we realize that this entity is
3512 indeed needed. */
3513 comdat_p = true;
3514 mark_needed (decl);
3518 else if (!flag_implicit_templates
3519 && CLASSTYPE_IMPLICIT_INSTANTIATION (class_type))
3520 import_p = true;
3521 else
3522 comdat_p = true;
3524 else if (VAR_P (decl) && DECL_TINFO_P (decl))
3526 tree type = TREE_TYPE (DECL_NAME (decl));
3527 if (CLASS_TYPE_P (type))
3529 class_type = type;
3530 import_export_class (type);
3531 if (CLASSTYPE_INTERFACE_KNOWN (type)
3532 && TYPE_POLYMORPHIC_P (type)
3533 && CLASSTYPE_INTERFACE_ONLY (type)
3534 /* If -fno-rtti was specified, then we cannot be sure
3535 that RTTI information will be emitted with the
3536 virtual table of the class, so we must emit it
3537 wherever it is used. */
3538 && flag_rtti)
3539 import_p = true;
3540 else
3542 if (CLASSTYPE_INTERFACE_KNOWN (type)
3543 && !CLASSTYPE_INTERFACE_ONLY (type))
3545 comdat_p = (targetm.cxx.class_data_always_comdat ()
3546 || !vtables_uniquely_emitted (class_type));
3547 mark_needed (decl);
3548 if (!flag_weak)
3550 comdat_p = false;
3551 DECL_EXTERNAL (decl) = 0;
3554 else
3555 comdat_p = true;
3558 else
3559 comdat_p = true;
3561 else if (DECL_TEMPLOID_INSTANTIATION (decl))
3563 /* DECL is an implicit instantiation of a function or static
3564 data member. */
3565 if (flag_implicit_templates
3566 || (flag_implicit_inline_templates
3567 && TREE_CODE (decl) == FUNCTION_DECL
3568 && DECL_DECLARED_INLINE_P (decl)))
3569 comdat_p = true;
3570 else
3571 /* If we are not implicitly generating templates, then mark
3572 this entity as undefined in this translation unit. */
3573 import_p = true;
3575 else if (TREE_CODE (decl) == FUNCTION_DECL && DECL_FUNCTION_MEMBER_P (decl))
3577 if (!DECL_DECLARED_INLINE_P (decl))
3579 tree ctype = DECL_CONTEXT (decl);
3580 import_export_class (ctype);
3581 if (CLASSTYPE_INTERFACE_KNOWN (ctype))
3583 DECL_NOT_REALLY_EXTERN (decl)
3584 = ! (CLASSTYPE_INTERFACE_ONLY (ctype)
3585 || (DECL_DECLARED_INLINE_P (decl)
3586 && ! flag_implement_inlines
3587 && !DECL_VINDEX (decl)));
3589 if (!DECL_NOT_REALLY_EXTERN (decl))
3590 DECL_EXTERNAL (decl) = 1;
3592 /* Always make artificials weak. */
3593 if (DECL_ARTIFICIAL (decl) && flag_weak)
3594 comdat_p = true;
3595 else
3596 maybe_make_one_only (decl);
3599 else
3600 comdat_p = true;
3602 else
3603 comdat_p = true;
3605 if (import_p)
3607 /* If we are importing DECL into this translation unit, mark is
3608 an undefined here. */
3609 DECL_EXTERNAL (decl) = 1;
3610 DECL_NOT_REALLY_EXTERN (decl) = 0;
3612 else if (comdat_p)
3614 /* If we decided to put DECL in COMDAT, mark it accordingly at
3615 this point. */
3616 comdat_linkage (decl);
3619 DECL_INTERFACE_KNOWN (decl) = 1;
3622 /* Return an expression that performs the destruction of DECL, which
3623 must be a VAR_DECL whose type has a non-trivial destructor, or is
3624 an array whose (innermost) elements have a non-trivial destructor. */
3626 tree
3627 build_cleanup (tree decl)
3629 tree clean = cxx_maybe_build_cleanup (decl, tf_warning_or_error);
3630 gcc_assert (clean != NULL_TREE);
3631 return clean;
3634 /* GUARD is a helper variable for DECL; make them have the same linkage and
3635 visibility. */
3637 void
3638 copy_linkage (tree guard, tree decl)
3640 TREE_PUBLIC (guard) = TREE_PUBLIC (decl);
3641 TREE_STATIC (guard) = TREE_STATIC (decl);
3642 DECL_COMMON (guard) = DECL_COMMON (decl);
3643 DECL_COMDAT (guard) = DECL_COMDAT (decl);
3644 if (TREE_STATIC (guard))
3646 CP_DECL_THREAD_LOCAL_P (guard) = CP_DECL_THREAD_LOCAL_P (decl);
3647 set_decl_tls_model (guard, DECL_TLS_MODEL (decl));
3648 if (DECL_ONE_ONLY (decl))
3649 make_decl_one_only (guard, cxx_comdat_group (guard));
3650 if (TREE_PUBLIC (decl))
3651 DECL_WEAK (guard) = DECL_WEAK (decl);
3652 /* Also check vague_linkage_p, as DECL_WEAK and DECL_ONE_ONLY might not
3653 be set until import_export_decl at EOF. */
3654 if (vague_linkage_p (decl))
3655 comdat_linkage (guard);
3656 DECL_VISIBILITY (guard) = DECL_VISIBILITY (decl);
3657 DECL_VISIBILITY_SPECIFIED (guard) = DECL_VISIBILITY_SPECIFIED (decl);
3661 /* Returns the initialization guard variable for the variable DECL,
3662 which has static storage duration. */
3664 tree
3665 get_guard (tree decl)
3667 tree sname = mangle_guard_variable (decl);
3668 tree guard = get_global_binding (sname);
3669 if (! guard)
3671 tree guard_type;
3673 /* We use a type that is big enough to contain a mutex as well
3674 as an integer counter. */
3675 guard_type = targetm.cxx.guard_type ();
3676 guard = build_decl (DECL_SOURCE_LOCATION (decl),
3677 VAR_DECL, sname, guard_type);
3679 /* The guard should have the same linkage as what it guards. */
3680 copy_linkage (guard, decl);
3682 DECL_ARTIFICIAL (guard) = 1;
3683 DECL_IGNORED_P (guard) = 1;
3684 TREE_USED (guard) = 1;
3685 pushdecl_top_level_and_finish (guard, NULL_TREE);
3687 return guard;
3690 /* Returns true if accessing the GUARD atomic is expensive,
3691 i.e. involves a call to __sync_synchronize or similar.
3692 In this case let __cxa_guard_acquire handle the atomics. */
3694 static bool
3695 is_atomic_expensive_p (machine_mode mode)
3697 if (!flag_inline_atomics)
3698 return true;
3700 if (!can_compare_and_swap_p (mode, false) || !can_atomic_load_p (mode))
3701 return true;
3703 return false;
3706 /* Return an atomic load of src with the appropriate memory model. */
3708 static tree
3709 build_atomic_load_type (tree src, HOST_WIDE_INT model, tree type)
3711 tree ptr_type = build_pointer_type (type);
3712 tree mem_model = build_int_cst (integer_type_node, model);
3713 tree t, addr, val;
3714 unsigned int size;
3715 int fncode;
3717 size = tree_to_uhwi (TYPE_SIZE_UNIT (type));
3719 fncode = BUILT_IN_ATOMIC_LOAD_N + exact_log2 (size) + 1;
3720 t = builtin_decl_implicit ((enum built_in_function) fncode);
3722 addr = build1 (ADDR_EXPR, ptr_type, src);
3723 val = build_call_expr (t, 2, addr, mem_model);
3724 return val;
3727 /* Return those bits of the GUARD variable that should be set when the
3728 guarded entity is actually initialized. */
3730 static tree
3731 get_guard_bits (tree guard)
3733 if (!targetm.cxx.guard_mask_bit ())
3735 /* We only set the first byte of the guard, in order to leave room
3736 for a mutex in the high-order bits. */
3737 guard = build1 (ADDR_EXPR,
3738 build_pointer_type (TREE_TYPE (guard)),
3739 guard);
3740 guard = build1 (NOP_EXPR,
3741 build_pointer_type (char_type_node),
3742 guard);
3743 guard = build1 (INDIRECT_REF, char_type_node, guard);
3746 return guard;
3749 /* Return an expression which determines whether or not the GUARD
3750 variable has already been initialized. */
3752 tree
3753 get_guard_cond (tree guard, bool thread_safe)
3755 tree guard_value;
3757 if (!thread_safe)
3758 guard = get_guard_bits (guard);
3759 else
3761 tree type = targetm.cxx.guard_mask_bit ()
3762 ? TREE_TYPE (guard) : char_type_node;
3764 if (is_atomic_expensive_p (TYPE_MODE (type)))
3765 guard = integer_zero_node;
3766 else
3767 guard = build_atomic_load_type (guard, MEMMODEL_ACQUIRE, type);
3770 /* Mask off all but the low bit. */
3771 if (targetm.cxx.guard_mask_bit ())
3773 guard_value = integer_one_node;
3774 if (!same_type_p (TREE_TYPE (guard_value), TREE_TYPE (guard)))
3775 guard_value = fold_convert (TREE_TYPE (guard), guard_value);
3776 guard = cp_build_binary_op (input_location,
3777 BIT_AND_EXPR, guard, guard_value,
3778 tf_warning_or_error);
3781 guard_value = integer_zero_node;
3782 if (!same_type_p (TREE_TYPE (guard_value), TREE_TYPE (guard)))
3783 guard_value = fold_convert (TREE_TYPE (guard), guard_value);
3784 return cp_build_binary_op (input_location,
3785 EQ_EXPR, guard, guard_value,
3786 tf_warning_or_error);
3789 /* Return an expression which sets the GUARD variable, indicating that
3790 the variable being guarded has been initialized. */
3792 tree
3793 set_guard (tree guard)
3795 tree guard_init;
3797 /* Set the GUARD to one. */
3798 guard = get_guard_bits (guard);
3799 guard_init = integer_one_node;
3800 if (!same_type_p (TREE_TYPE (guard_init), TREE_TYPE (guard)))
3801 guard_init = fold_convert (TREE_TYPE (guard), guard_init);
3802 return cp_build_modify_expr (input_location, guard, NOP_EXPR, guard_init,
3803 tf_warning_or_error);
3806 /* Returns true iff we can tell that VAR does not have a dynamic
3807 initializer. */
3809 static bool
3810 var_defined_without_dynamic_init (tree var)
3812 /* constinit vars are guaranteed to not have dynamic initializer,
3813 but still registering the destructor counts as dynamic initialization. */
3814 if (DECL_DECLARED_CONSTINIT_P (var)
3815 && COMPLETE_TYPE_P (TREE_TYPE (var))
3816 && !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (var)))
3817 return true;
3818 /* If it's defined in another TU, we can't tell. */
3819 if (DECL_EXTERNAL (var))
3820 return false;
3821 /* If it has a non-trivial destructor, registering the destructor
3822 counts as dynamic initialization. */
3823 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (var)))
3824 return false;
3825 /* If it's in this TU, its initializer has been processed, unless
3826 it's a case of self-initialization, then DECL_INITIALIZED_P is
3827 false while the initializer is handled by finish_id_expression. */
3828 if (!DECL_INITIALIZED_P (var))
3829 return false;
3830 /* If it has no initializer or a constant one, it's not dynamic. */
3831 return (!DECL_NONTRIVIALLY_INITIALIZED_P (var)
3832 || DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (var));
3835 /* Returns true iff VAR is a variable that needs uses to be
3836 wrapped for possible dynamic initialization. */
3838 bool
3839 var_needs_tls_wrapper (tree var)
3841 return (!error_operand_p (var)
3842 && CP_DECL_THREAD_LOCAL_P (var)
3843 && !DECL_GNU_TLS_P (var)
3844 && !DECL_FUNCTION_SCOPE_P (var)
3845 && !var_defined_without_dynamic_init (var));
3848 /* Get the FUNCTION_DECL for the shared TLS init function for this
3849 translation unit. */
3851 static tree
3852 get_local_tls_init_fn (location_t loc)
3854 tree sname = get_identifier ("__tls_init");
3855 tree fn = get_global_binding (sname);
3856 if (!fn)
3858 fn = build_lang_decl_loc (loc, FUNCTION_DECL, sname,
3859 build_function_type (void_type_node,
3860 void_list_node));
3861 SET_DECL_LANGUAGE (fn, lang_c);
3862 TREE_PUBLIC (fn) = false;
3863 DECL_ARTIFICIAL (fn) = true;
3864 mark_used (fn);
3865 set_global_binding (fn);
3867 return fn;
3870 /* Get a FUNCTION_DECL for the init function for the thread_local
3871 variable VAR. The init function will be an alias to the function
3872 that initializes all the non-local TLS variables in the translation
3873 unit. The init function is only used by the wrapper function. */
3875 static tree
3876 get_tls_init_fn (tree var)
3878 /* Only C++11 TLS vars need this init fn. */
3879 if (!var_needs_tls_wrapper (var))
3880 return NULL_TREE;
3882 /* If -fno-extern-tls-init, assume that we don't need to call
3883 a tls init function for a variable defined in another TU. */
3884 if (!flag_extern_tls_init && DECL_EXTERNAL (var))
3885 return NULL_TREE;
3887 /* If the variable is internal, or if we can't generate aliases,
3888 call the local init function directly. */
3889 if (!TREE_PUBLIC (var) || !TARGET_SUPPORTS_ALIASES)
3890 return get_local_tls_init_fn (DECL_SOURCE_LOCATION (var));
3892 tree sname = mangle_tls_init_fn (var);
3893 tree fn = get_global_binding (sname);
3894 if (!fn)
3896 fn = build_lang_decl (FUNCTION_DECL, sname,
3897 build_function_type (void_type_node,
3898 void_list_node));
3899 SET_DECL_LANGUAGE (fn, lang_c);
3900 TREE_PUBLIC (fn) = TREE_PUBLIC (var);
3901 DECL_ARTIFICIAL (fn) = true;
3902 DECL_COMDAT (fn) = DECL_COMDAT (var);
3903 DECL_EXTERNAL (fn) = DECL_EXTERNAL (var);
3904 if (DECL_ONE_ONLY (var))
3905 make_decl_one_only (fn, cxx_comdat_group (fn));
3906 if (TREE_PUBLIC (var))
3908 tree obtype = strip_array_types (non_reference (TREE_TYPE (var)));
3909 /* If the variable is defined somewhere else and might have static
3910 initialization, make the init function a weak reference. */
3911 if ((!TYPE_NEEDS_CONSTRUCTING (obtype)
3912 || TYPE_HAS_CONSTEXPR_CTOR (obtype)
3913 || TYPE_HAS_TRIVIAL_DFLT (obtype))
3914 && TYPE_HAS_TRIVIAL_DESTRUCTOR (obtype)
3915 && DECL_EXTERNAL (var))
3916 declare_weak (fn);
3917 else
3918 DECL_WEAK (fn) = DECL_WEAK (var);
3920 DECL_VISIBILITY (fn) = DECL_VISIBILITY (var);
3921 DECL_VISIBILITY_SPECIFIED (fn) = DECL_VISIBILITY_SPECIFIED (var);
3922 DECL_DLLIMPORT_P (fn) = DECL_DLLIMPORT_P (var);
3923 DECL_IGNORED_P (fn) = 1;
3924 mark_used (fn);
3926 DECL_BEFRIENDING_CLASSES (fn) = var;
3928 set_global_binding (fn);
3930 return fn;
3933 /* Get a FUNCTION_DECL for the init wrapper function for the thread_local
3934 variable VAR. The wrapper function calls the init function (if any) for
3935 VAR and then returns a reference to VAR. The wrapper function is used
3936 in place of VAR everywhere VAR is mentioned. */
3938 static tree
3939 get_tls_wrapper_fn (tree var)
3941 /* Only C++11 TLS vars need this wrapper fn. */
3942 if (!var_needs_tls_wrapper (var))
3943 return NULL_TREE;
3945 tree sname = mangle_tls_wrapper_fn (var);
3946 tree fn = get_global_binding (sname);
3947 if (!fn)
3949 /* A named rvalue reference is an lvalue, so the wrapper should
3950 always return an lvalue reference. */
3951 tree type = non_reference (TREE_TYPE (var));
3952 type = build_reference_type (type);
3953 tree fntype = build_function_type (type, void_list_node);
3955 fn = build_lang_decl_loc (DECL_SOURCE_LOCATION (var),
3956 FUNCTION_DECL, sname, fntype);
3957 SET_DECL_LANGUAGE (fn, lang_c);
3958 TREE_PUBLIC (fn) = TREE_PUBLIC (var);
3959 DECL_ARTIFICIAL (fn) = true;
3960 DECL_IGNORED_P (fn) = 1;
3961 DECL_CONTEXT (fn) = DECL_CONTEXT (var);
3962 /* The wrapper is inline and emitted everywhere var is used. */
3963 DECL_DECLARED_INLINE_P (fn) = true;
3964 if (TREE_PUBLIC (var))
3966 comdat_linkage (fn);
3967 #ifdef HAVE_GAS_HIDDEN
3968 /* Make the wrapper bind locally; there's no reason to share
3969 the wrapper between multiple shared objects. */
3970 DECL_VISIBILITY (fn) = VISIBILITY_INTERNAL;
3971 DECL_VISIBILITY_SPECIFIED (fn) = true;
3972 #endif
3974 if (!TREE_PUBLIC (fn))
3975 DECL_INTERFACE_KNOWN (fn) = true;
3976 mark_used (fn);
3977 note_vague_linkage_fn (fn);
3979 #if 0
3980 /* We want CSE to commonize calls to the wrapper, but marking it as
3981 pure is unsafe since it has side-effects. I guess we need a new
3982 ECF flag even weaker than ECF_PURE. FIXME! */
3983 DECL_PURE_P (fn) = true;
3984 #endif
3986 DECL_BEFRIENDING_CLASSES (fn) = var;
3988 set_global_binding (fn);
3990 return fn;
3993 /* If EXPR is a thread_local variable that should be wrapped by init
3994 wrapper function, return a call to that function, otherwise return
3995 NULL. */
3997 tree
3998 maybe_get_tls_wrapper_call (tree expr)
4000 if (VAR_P (expr)
4001 && !processing_template_decl
4002 && !cp_unevaluated_operand
4003 && CP_DECL_THREAD_LOCAL_P (expr))
4004 if (tree wrap = get_tls_wrapper_fn (expr))
4005 return build_cxx_call (wrap, 0, NULL, tf_warning_or_error);
4006 return NULL;
4009 /* At EOF, generate the definition for the TLS wrapper function FN:
4011 T& var_wrapper() {
4012 if (init_fn) init_fn();
4013 return var;
4014 } */
4016 static void
4017 generate_tls_wrapper (tree fn)
4019 tree var = DECL_BEFRIENDING_CLASSES (fn);
4021 start_preparsed_function (fn, NULL_TREE, SF_DEFAULT | SF_PRE_PARSED);
4022 tree body = begin_function_body ();
4023 /* Only call the init fn if there might be one. */
4024 if (tree init_fn = get_tls_init_fn (var))
4026 tree if_stmt = NULL_TREE;
4027 /* If init_fn is a weakref, make sure it exists before calling. */
4028 if (lookup_attribute ("weak", DECL_ATTRIBUTES (init_fn)))
4030 if_stmt = begin_if_stmt ();
4031 tree addr = cp_build_addr_expr (init_fn, tf_warning_or_error);
4032 tree cond = cp_build_binary_op (DECL_SOURCE_LOCATION (var),
4033 NE_EXPR, addr, nullptr_node,
4034 tf_warning_or_error);
4035 finish_if_stmt_cond (cond, if_stmt);
4037 finish_expr_stmt (build_cxx_call
4038 (init_fn, 0, NULL, tf_warning_or_error));
4039 if (if_stmt)
4041 finish_then_clause (if_stmt);
4042 finish_if_stmt (if_stmt);
4045 else
4046 /* If there's no initialization, the wrapper is a constant function. */
4047 TREE_READONLY (fn) = true;
4048 finish_return_stmt (convert_from_reference (var));
4049 finish_function_body (body);
4050 expand_or_defer_fn (finish_function (/*inline_p=*/false));
4053 /* Start a global constructor or destructor function. */
4055 static tree
4056 start_objects (bool initp, unsigned priority, bool has_body,
4057 bool omp_target = false)
4059 bool default_init = initp && priority == DEFAULT_INIT_PRIORITY;
4060 bool is_module_init = default_init && module_global_init_needed ();
4061 tree name = NULL_TREE;
4063 if (is_module_init)
4064 name = mangle_module_global_init (0);
4065 else
4067 char type[14];
4069 /* We use `I' to indicate initialization and `D' to indicate
4070 destruction. */
4071 unsigned len;
4072 if (omp_target)
4073 /* Use "off_" signifying "offload" here. The name must be distinct
4074 from the non-offload case. The format of the name is scanned in
4075 tree.cc/get_file_function_name, so stick to the same length for
4076 both name variants. */
4077 len = sprintf (type, "off_%c", initp ? 'I' : 'D');
4078 else
4079 len = sprintf (type, "sub_%c", initp ? 'I' : 'D');
4080 if (priority != DEFAULT_INIT_PRIORITY)
4082 char joiner = '_';
4083 #ifdef JOINER
4084 joiner = JOINER;
4085 #endif
4086 type[len++] = joiner;
4087 sprintf (type + len, "%.5u", priority);
4089 name = get_file_function_name (type);
4092 tree fntype = build_function_type (void_type_node, void_list_node);
4093 tree fndecl = build_lang_decl (FUNCTION_DECL, name, fntype);
4095 if (omp_target)
4097 DECL_ATTRIBUTES (fndecl)
4098 = tree_cons (get_identifier ("omp declare target"), NULL_TREE,
4099 DECL_ATTRIBUTES (fndecl));
4100 DECL_ATTRIBUTES (fndecl)
4101 = tree_cons (get_identifier ("omp declare target nohost"), NULL_TREE,
4102 DECL_ATTRIBUTES (fndecl));
4105 DECL_CONTEXT (fndecl) = FROB_CONTEXT (global_namespace);
4106 if (is_module_init)
4108 SET_DECL_ASSEMBLER_NAME (fndecl, name);
4109 TREE_PUBLIC (fndecl) = true;
4110 determine_visibility (fndecl);
4112 else
4113 TREE_PUBLIC (fndecl) = 0;
4114 start_preparsed_function (fndecl, /*attrs=*/NULL_TREE, SF_PRE_PARSED);
4116 /* Mark as artificial because it's not explicitly in the user's
4117 source code. */
4118 DECL_ARTIFICIAL (current_function_decl) = 1;
4120 /* Mark this declaration as used to avoid spurious warnings. */
4121 TREE_USED (current_function_decl) = 1;
4123 /* Mark this function as a global constructor or destructor. */
4124 if (initp)
4125 DECL_GLOBAL_CTOR_P (current_function_decl) = 1;
4126 else
4127 DECL_GLOBAL_DTOR_P (current_function_decl) = 1;
4129 tree body = begin_compound_stmt (BCS_FN_BODY);
4131 if (is_module_init && has_body)
4133 // If the function is going to be empty, don't emit idempotency.
4134 // 'static bool __in_chrg = false;
4135 // if (__inchrg) return;
4136 // __inchrg = true
4137 tree var = build_lang_decl (VAR_DECL, in_charge_identifier,
4138 boolean_type_node);
4139 DECL_CONTEXT (var) = fndecl;
4140 DECL_ARTIFICIAL (var) = true;
4141 TREE_STATIC (var) = true;
4142 pushdecl (var);
4143 cp_finish_decl (var, NULL_TREE, false, NULL_TREE, 0);
4145 tree if_stmt = begin_if_stmt ();
4146 finish_if_stmt_cond (var, if_stmt);
4147 finish_return_stmt (NULL_TREE);
4148 finish_then_clause (if_stmt);
4149 finish_if_stmt (if_stmt);
4151 tree assign = build2 (MODIFY_EXPR, boolean_type_node,
4152 var, boolean_true_node);
4153 TREE_SIDE_EFFECTS (assign) = true;
4154 finish_expr_stmt (assign);
4157 return body;
4160 /* Finish a global constructor or destructor. Add it to the global
4161 ctors or dtors, if STARTP is true. */
4163 static tree
4164 finish_objects (bool initp, unsigned priority, tree body, bool startp)
4166 /* Finish up. */
4167 finish_compound_stmt (body);
4168 tree fn = finish_function (/*inline_p=*/false);
4170 if (!startp)
4171 ; // Neither ctor nor dtor I be.
4172 else if (initp)
4174 DECL_STATIC_CONSTRUCTOR (fn) = 1;
4175 decl_init_priority_insert (fn, priority);
4177 else
4179 DECL_STATIC_DESTRUCTOR (fn) = 1;
4180 decl_fini_priority_insert (fn, priority);
4183 return fn;
4186 /* The name of the function we create to handle initializations and
4187 destructions for objects with static storage duration. */
4188 #define SSDF_IDENTIFIER "__static_initialization_and_destruction"
4189 #define OMP_SSDF_IDENTIFIER "__omp_target_static_init_and_destruction"
4191 /* Begins the generation of the function that will handle all
4192 initialization or destruction of objects with static storage
4193 duration at PRIORITY.
4195 It is assumed that this function will be called once for the host, and once
4196 for an OpenMP offload target. */
4198 static tree
4199 start_partial_init_fini_fn (bool initp, unsigned priority, unsigned count,
4200 bool omp_target)
4202 char id[MAX (sizeof (SSDF_IDENTIFIER), sizeof (OMP_SSDF_IDENTIFIER))
4203 + 1 /* \0 */ + 32];
4204 tree name;
4206 /* Create the identifier for this function. It will be of the form
4207 SSDF_IDENTIFIER_<number> if not omp_target and otherwise
4208 OMP_SSDF_IDENTIFIER_<number>. */
4209 sprintf (id, "%s_%u", omp_target ? OMP_SSDF_IDENTIFIER : SSDF_IDENTIFIER,
4210 count);
4211 name = get_identifier (id);
4212 tree type = build_function_type (void_type_node, void_list_node);
4214 /* Create the FUNCTION_DECL itself. */
4215 tree fn = build_lang_decl (FUNCTION_DECL, name, type);
4216 TREE_PUBLIC (fn) = 0;
4217 DECL_ARTIFICIAL (fn) = 1;
4219 if (omp_target)
4221 DECL_ATTRIBUTES (fn)
4222 = tree_cons (get_identifier ("omp declare target"), NULL_TREE,
4223 DECL_ATTRIBUTES (fn));
4224 DECL_ATTRIBUTES (fn)
4225 = tree_cons (get_identifier ("omp declare target nohost"), NULL_TREE,
4226 DECL_ATTRIBUTES (fn));
4229 int idx = initp + 2 * omp_target;
4231 /* Put this function in the list of functions to be called from the
4232 static constructors and destructors. */
4233 if (!static_init_fini_fns[idx])
4234 static_init_fini_fns[idx] = priority_map_t::create_ggc ();
4235 auto &slot = static_init_fini_fns[idx]->get_or_insert (priority);
4236 slot = tree_cons (fn, NULL_TREE, slot);
4238 /* Put the function in the global scope. */
4239 pushdecl (fn);
4241 /* Start the function itself. This is equivalent to declaring the
4242 function as:
4244 static void __ssdf (int __initialize_p, init __priority_p);
4246 It is static because we only need to call this function from the
4247 various constructor and destructor functions for this module. */
4248 start_preparsed_function (fn, /*attrs=*/NULL_TREE, SF_PRE_PARSED);
4250 /* Set up the scope of the outermost block in the function. */
4251 return begin_compound_stmt (BCS_FN_BODY);
4254 /* Finish the generation of the function which performs initialization
4255 or destruction of objects with static storage duration. */
4257 static void
4258 finish_partial_init_fini_fn (tree body)
4260 /* Close out the function. */
4261 finish_compound_stmt (body);
4262 expand_or_defer_fn (finish_function (/*inline_p=*/false));
4265 /* The effective initialization priority of a DECL. */
4267 #define DECL_EFFECTIVE_INIT_PRIORITY(decl) \
4268 ((!DECL_HAS_INIT_PRIORITY_P (decl) || DECL_INIT_PRIORITY (decl) == 0) \
4269 ? DEFAULT_INIT_PRIORITY : DECL_INIT_PRIORITY (decl))
4271 /* Whether a DECL needs a guard to protect it against multiple
4272 initialization. */
4274 #define NEEDS_GUARD_P(decl) (TREE_PUBLIC (decl) && (DECL_COMMON (decl) \
4275 || DECL_ONE_ONLY (decl) \
4276 || DECL_WEAK (decl)))
4278 /* Walks the initializer list of a global variable and looks for
4279 temporary variables (DECL_NAME() == NULL and DECL_ARTIFICIAL != 0)
4280 and that have their DECL_CONTEXT() == NULL. For each such
4281 temporary variable, set their DECL_CONTEXT() to CTX -- the
4282 initializing function. This is necessary because otherwise some
4283 optimizers (enabled by -O2 -fprofile-arcs) might crash when trying
4284 to refer to a temporary variable that does not have its
4285 DECL_CONTEXT() properly set. */
4287 static tree
4288 fix_temporary_vars_context_r (tree *node,
4289 int * /*unused*/,
4290 void *ctx)
4292 if (TREE_CODE (*node) == BIND_EXPR)
4293 for (tree var = BIND_EXPR_VARS (*node); var; var = DECL_CHAIN (var))
4294 if (VAR_P (var) && !DECL_NAME (var)
4295 && DECL_ARTIFICIAL (var) && !DECL_CONTEXT (var))
4296 DECL_CONTEXT (var) = tree (ctx);
4298 return NULL_TREE;
4301 /* Set up to handle the initialization or destruction of DECL. If
4302 INITP is nonzero, we are initializing the variable. Otherwise, we
4303 are destroying it. */
4305 static void
4306 one_static_initialization_or_destruction (bool initp, tree decl, tree init)
4308 /* If we are supposed to destruct and there's a trivial destructor,
4309 nothing has to be done. */
4310 gcc_checking_assert (init || !TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)));
4312 /* Trick the compiler into thinking we are at the file and line
4313 where DECL was declared so that error-messages make sense, and so
4314 that the debugger will show somewhat sensible file and line
4315 information. */
4316 input_location = DECL_SOURCE_LOCATION (decl);
4318 /* Make sure temporary variables in the initialiser all have
4319 their DECL_CONTEXT() set to a value different from NULL_TREE.
4320 This can happen when global variables initializers are built.
4321 In that case, the DECL_CONTEXT() of the global variables _AND_ of all
4322 the temporary variables that might have been generated in the
4323 accompanying initializers is NULL_TREE, meaning the variables have been
4324 declared in the global namespace.
4325 What we want to do here is to fix that and make sure the DECL_CONTEXT()
4326 of the temporaries are set to the current function decl. */
4327 cp_walk_tree_without_duplicates (&init,
4328 fix_temporary_vars_context_r,
4329 current_function_decl);
4331 /* Because of:
4333 [class.access.spec]
4335 Access control for implicit calls to the constructors,
4336 the conversion functions, or the destructor called to
4337 create and destroy a static data member is performed as
4338 if these calls appeared in the scope of the member's
4339 class.
4341 we pretend we are in a static member function of the class of
4342 which the DECL is a member. */
4343 if (member_p (decl))
4345 DECL_CONTEXT (current_function_decl) = DECL_CONTEXT (decl);
4346 DECL_STATIC_FUNCTION_P (current_function_decl) = 1;
4349 /* Assume we don't need a guard. */
4350 tree guard_if_stmt = NULL_TREE;
4352 /* We need a guard if this is an object with external linkage that
4353 might be initialized in more than one place. (For example, a
4354 static data member of a template, when the data member requires
4355 construction.) */
4356 if (NEEDS_GUARD_P (decl))
4358 tree guard = get_guard (decl);
4359 tree guard_cond;
4361 if (flag_use_cxa_atexit)
4363 /* When using __cxa_atexit, we just check the GUARD as we
4364 would for a local static. We never try to destroy
4365 anything from a static destructor. */
4366 gcc_assert (initp);
4367 guard_cond = get_guard_cond (guard, false);
4369 else
4371 /* If we don't have __cxa_atexit, then we will be running
4372 destructors from .fini sections, or their equivalents.
4373 So, we need to know how many times we've tried to
4374 initialize this object. We do initializations only if
4375 the GUARD was or becomes zero (initp vs !initp
4376 respectively). */
4377 guard_cond = cp_build_unary_op (initp ? POSTINCREMENT_EXPR
4378 : PREDECREMENT_EXPR,
4379 guard,
4380 /*noconvert=*/true,
4381 tf_warning_or_error);
4382 guard_cond = cp_build_binary_op (input_location, EQ_EXPR, guard_cond,
4383 integer_zero_node,
4384 tf_warning_or_error);
4387 guard_if_stmt = begin_if_stmt ();
4388 finish_if_stmt_cond (guard_cond, guard_if_stmt);
4390 if (flag_use_cxa_atexit)
4391 /* Set the GUARD now. */
4392 finish_expr_stmt (set_guard (guard));
4395 /* Perform the initialization or destruction. */
4396 if (initp)
4398 if (init)
4400 finish_expr_stmt (init);
4401 if (sanitize_flags_p (SANITIZE_ADDRESS, decl))
4402 if (varpool_node *vnode = varpool_node::get (decl))
4403 vnode->dynamically_initialized = 1;
4406 /* If we're using __cxa_atexit, register a function that calls the
4407 destructor for the object. */
4408 if (flag_use_cxa_atexit)
4409 finish_expr_stmt (register_dtor_fn (decl));
4411 else
4412 finish_expr_stmt (build_cleanup (decl));
4414 /* Finish the guard if-stmt, if necessary. */
4415 if (guard_if_stmt)
4417 finish_then_clause (guard_if_stmt);
4418 finish_if_stmt (guard_if_stmt);
4421 /* Now that we're done with DECL we don't need to pretend to be a
4422 member of its class any longer. */
4423 DECL_CONTEXT (current_function_decl) = NULL_TREE;
4424 DECL_STATIC_FUNCTION_P (current_function_decl) = 0;
4427 /* Generate code to do the initialization or destruction of the decls in VARS,
4428 a TREE_LIST of VAR_DECL with static storage duration.
4429 Whether initialization or destruction is performed is specified by INITP. */
4431 static tree
4432 emit_partial_init_fini_fn (bool initp, unsigned priority, tree vars,
4433 unsigned counter, location_t locus, tree host_fn)
4435 input_location = locus;
4436 bool omp_target = (host_fn != NULL_TREE);
4437 tree body = start_partial_init_fini_fn (initp, priority, counter, omp_target);
4438 tree fndecl = current_function_decl;
4440 tree nonhost_if_stmt = NULL_TREE;
4441 if (omp_target)
4443 nonhost_if_stmt = begin_if_stmt ();
4444 /* We add an "omp declare target nohost" attribute, but (for
4445 now) we still get a copy of the constructor/destructor on
4446 the host. Make sure it does nothing unless we're on the
4447 target device. */
4448 tree fn = builtin_decl_explicit (BUILT_IN_OMP_IS_INITIAL_DEVICE);
4449 tree initial_dev = build_call_expr (fn, 0);
4450 tree target_dev_p
4451 = cp_build_binary_op (input_location, NE_EXPR, initial_dev,
4452 integer_one_node, tf_warning_or_error);
4453 finish_if_stmt_cond (target_dev_p, nonhost_if_stmt);
4456 for (tree node = vars; node; node = TREE_CHAIN (node))
4458 tree decl = TREE_VALUE (node);
4459 tree init = TREE_PURPOSE (node);
4460 /* We will emit 'init' twice, and it is modified in-place during
4461 gimplification. Make a copy here. */
4462 if (omp_target)
4464 /* We've already emitted INIT in the host version of the ctor/dtor
4465 function. We need to deep-copy it (including new versions of
4466 local variables introduced, etc.) for use in the target
4467 ctor/dtor function. */
4468 copy_body_data id;
4469 hash_map<tree, tree> decl_map;
4470 memset (&id, 0, sizeof (id));
4471 id.src_fn = host_fn;
4472 id.dst_fn = current_function_decl;
4473 id.src_cfun = DECL_STRUCT_FUNCTION (id.src_fn);
4474 id.decl_map = &decl_map;
4475 id.copy_decl = copy_decl_no_change;
4476 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
4477 id.transform_new_cfg = true;
4478 id.transform_return_to_modify = false;
4479 id.eh_lp_nr = 0;
4480 walk_tree (&init, copy_tree_body_r, &id, NULL);
4482 /* Do one initialization or destruction. */
4483 one_static_initialization_or_destruction (initp, decl, init);
4486 if (omp_target)
4488 /* Finish up nonhost if-stmt body. */
4489 finish_then_clause (nonhost_if_stmt);
4490 finish_if_stmt (nonhost_if_stmt);
4493 /* Finish up the static storage duration function for this
4494 round. */
4495 input_location = locus;
4496 finish_partial_init_fini_fn (body);
4498 return fndecl;
4501 /* VARS is a list of variables with static storage duration which may
4502 need initialization and/or finalization. Remove those variables
4503 that don't really need to be initialized or finalized, and return
4504 the resulting list. The order in which the variables appear in
4505 VARS is in reverse order of the order in which they should actually
4506 be initialized. That order is preserved. */
4508 static tree
4509 prune_vars_needing_no_initialization (tree *vars)
4511 tree *var = vars;
4512 tree result = NULL_TREE;
4514 while (*var)
4516 tree t = *var;
4517 tree decl = TREE_VALUE (t);
4518 tree init = TREE_PURPOSE (t);
4520 /* Deal gracefully with error. */
4521 if (error_operand_p (decl))
4523 var = &TREE_CHAIN (t);
4524 continue;
4527 /* The only things that can be initialized are variables. */
4528 gcc_assert (VAR_P (decl));
4530 /* If this object is not defined, we don't need to do anything
4531 here. */
4532 if (DECL_EXTERNAL (decl))
4534 var = &TREE_CHAIN (t);
4535 continue;
4538 /* Also, if the initializer already contains errors, we can bail
4539 out now. */
4540 if (init && TREE_CODE (init) == TREE_LIST
4541 && value_member (error_mark_node, init))
4543 var = &TREE_CHAIN (t);
4544 continue;
4547 /* This variable is going to need initialization and/or
4548 finalization, so we add it to the list. */
4549 *var = TREE_CHAIN (t);
4550 TREE_CHAIN (t) = result;
4551 result = t;
4554 return result;
4557 /* Split VAR_LIST by init priority and add into PARTS hash table.
4558 This reverses the variable ordering. */
4560 void
4561 partition_vars_for_init_fini (tree var_list, priority_map_t *(&parts)[4])
4563 for (auto node = var_list; node; node = TREE_CHAIN (node))
4565 tree decl = TREE_VALUE (node);
4566 tree init = TREE_PURPOSE (node);
4567 bool has_cleanup = !TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl));
4568 unsigned priority = DECL_EFFECTIVE_INIT_PRIORITY (decl);
4570 if (init || (flag_use_cxa_atexit && has_cleanup))
4572 // Add to initialization list.
4573 if (!parts[true])
4574 parts[true] = priority_map_t::create_ggc ();
4575 auto &slot = parts[true]->get_or_insert (priority);
4576 slot = tree_cons (init, decl, slot);
4579 if (!flag_use_cxa_atexit && has_cleanup)
4581 // Add to finalization list.
4582 if (!parts[false])
4583 parts[false] = priority_map_t::create_ggc ();
4584 auto &slot = parts[false]->get_or_insert (priority);
4585 slot = tree_cons (NULL_TREE, decl, slot);
4588 if (flag_openmp
4589 && lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl)))
4591 priority_map_t **omp_parts = parts + 2;
4593 if (init || (flag_use_cxa_atexit && has_cleanup))
4595 // Add to initialization list.
4596 if (!omp_parts[true])
4597 omp_parts[true] = priority_map_t::create_ggc ();
4598 auto &slot = omp_parts[true]->get_or_insert (priority);
4599 slot = tree_cons (init, decl, slot);
4602 if (!flag_use_cxa_atexit && has_cleanup)
4604 // Add to finalization list.
4605 if (!omp_parts[false])
4606 omp_parts[false] = priority_map_t::create_ggc ();
4607 auto &slot = omp_parts[false]->get_or_insert (priority);
4608 slot = tree_cons (NULL_TREE, decl, slot);
4614 /* Make sure we have told the back end about all the variables in
4615 VARS. */
4617 static void
4618 write_out_vars (tree vars)
4620 tree v;
4622 for (v = vars; v; v = TREE_CHAIN (v))
4624 tree var = TREE_VALUE (v);
4625 if (!var_finalized_p (var))
4627 import_export_decl (var);
4628 rest_of_decl_compilation (var, 1, 1);
4633 /* Generate a static constructor or destructor that calls the given
4634 init/fini fns at the indicated priority. */
4636 static void
4637 generate_ctor_or_dtor_function (bool initp, unsigned priority,
4638 tree fns, location_t locus, bool omp_target)
4640 input_location = locus;
4641 tree body = start_objects (initp, priority, bool (fns), omp_target);
4643 if (fns)
4645 /* To make sure dynamic construction doesn't access globals from
4646 other compilation units where they might not be yet
4647 constructed, for -fsanitize=address insert
4648 __asan_before_dynamic_init call that prevents access to
4649 either all global variables that need construction in other
4650 compilation units, or at least those that haven't been
4651 initialized yet. Variables that need dynamic construction in
4652 the current compilation unit are kept accessible. */
4653 if (initp && (flag_sanitize & SANITIZE_ADDRESS))
4654 finish_expr_stmt (asan_dynamic_init_call (/*after_p=*/false));
4656 /* Call the static init/fini functions. */
4657 for (tree node = fns; node; node = TREE_CHAIN (node))
4659 tree fn = TREE_PURPOSE (node);
4661 // We should never find a pure or constant cdtor.
4662 gcc_checking_assert (!(flags_from_decl_or_type (fn)
4663 & (ECF_CONST | ECF_PURE)));
4665 tree call = cp_build_function_call_nary (fn, tf_warning_or_error,
4666 NULL_TREE);
4667 finish_expr_stmt (call);
4670 /* Revert what __asan_before_dynamic_init did by calling
4671 __asan_after_dynamic_init. */
4672 if (initp && (flag_sanitize & SANITIZE_ADDRESS))
4673 finish_expr_stmt (asan_dynamic_init_call (/*after_p=*/true));
4676 /* Close out the function, and arrange for it to be called at init
4677 or fini time, if non-empty. (Even non-nop module initializer
4678 functions need this, as we cannot guarantee the module is
4679 imported somewhere in the program.) */
4680 expand_or_defer_fn (finish_objects (initp, priority, body, fns != NULL_TREE));
4683 /* Return C++ property of T, based on given operation OP. */
4685 static int
4686 cpp_check (tree t, cpp_operation op)
4688 switch (op)
4690 case HAS_DEPENDENT_TEMPLATE_ARGS:
4692 tree ti = CLASSTYPE_TEMPLATE_INFO (t);
4693 if (!ti)
4694 return 0;
4695 ++processing_template_decl;
4696 const bool dep = any_dependent_template_arguments_p (TI_ARGS (ti));
4697 --processing_template_decl;
4698 return dep;
4700 case IS_ABSTRACT:
4701 return DECL_PURE_VIRTUAL_P (t);
4702 case IS_ASSIGNMENT_OPERATOR:
4703 return DECL_ASSIGNMENT_OPERATOR_P (t);
4704 case IS_CONSTRUCTOR:
4705 return DECL_CONSTRUCTOR_P (t);
4706 case IS_DESTRUCTOR:
4707 return DECL_DESTRUCTOR_P (t);
4708 case IS_COPY_CONSTRUCTOR:
4709 return DECL_COPY_CONSTRUCTOR_P (t);
4710 case IS_MOVE_CONSTRUCTOR:
4711 return DECL_MOVE_CONSTRUCTOR_P (t);
4712 case IS_TEMPLATE:
4713 return TREE_CODE (t) == TEMPLATE_DECL;
4714 case IS_TRIVIAL:
4715 return trivial_type_p (t);
4716 default:
4717 return 0;
4721 /* Collect source file references recursively, starting from NAMESPC. */
4723 static void
4724 collect_source_refs (tree namespc)
4726 /* Iterate over names in this name space. */
4727 for (tree t = NAMESPACE_LEVEL (namespc)->names; t; t = TREE_CHAIN (t))
4728 if (DECL_IS_UNDECLARED_BUILTIN (t))
4730 else if (TREE_CODE (t) == NAMESPACE_DECL && !DECL_NAMESPACE_ALIAS (t))
4731 collect_source_refs (t);
4732 else
4733 collect_source_ref (DECL_SOURCE_FILE (t));
4736 /* Collect decls relevant to SOURCE_FILE from all namespaces recursively,
4737 starting from NAMESPC. */
4739 static void
4740 collect_ada_namespace (tree namespc, const char *source_file)
4742 tree decl = NAMESPACE_LEVEL (namespc)->names;
4744 /* Collect decls from this namespace. This will skip
4745 NAMESPACE_DECLs (both aliases and regular, it cannot tell). */
4746 collect_ada_nodes (decl, source_file);
4748 /* Now scan for namespace children, and dump them. */
4749 for (; decl; decl = TREE_CHAIN (decl))
4750 if (TREE_CODE (decl) == NAMESPACE_DECL && !DECL_NAMESPACE_ALIAS (decl))
4751 collect_ada_namespace (decl, source_file);
4754 /* Returns true iff there is a definition available for variable or
4755 function DECL. */
4757 bool
4758 decl_defined_p (tree decl)
4760 if (TREE_CODE (decl) == FUNCTION_DECL)
4761 return (DECL_INITIAL (decl) != NULL_TREE
4762 /* A pending instantiation of a friend temploid is defined. */
4763 || (DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl)
4764 && DECL_INITIAL (DECL_TEMPLATE_RESULT
4765 (DECL_TI_TEMPLATE (decl)))));
4766 else
4768 gcc_assert (VAR_P (decl));
4769 return !DECL_EXTERNAL (decl);
4773 /* Nonzero for a VAR_DECL whose value can be used in a constant expression.
4775 [expr.const]
4777 An integral constant-expression can only involve ... const
4778 variables of integral or enumeration types initialized with
4779 constant expressions ...
4781 C++0x also allows constexpr variables and temporaries initialized
4782 with constant expressions. We handle the former here, but the latter
4783 are just folded away in cxx_eval_constant_expression.
4785 The standard does not require that the expression be non-volatile.
4786 G++ implements the proposed correction in DR 457. */
4788 bool
4789 decl_constant_var_p (tree decl)
4791 if (!decl_maybe_constant_var_p (decl))
4792 return false;
4794 /* We don't know if a template static data member is initialized with
4795 a constant expression until we instantiate its initializer. Even
4796 in the case of a constexpr variable, we can't treat it as a
4797 constant until its initializer is complete in case it's used in
4798 its own initializer. */
4799 maybe_instantiate_decl (decl);
4800 return DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl);
4803 /* Returns true if DECL could be a symbolic constant variable, depending on
4804 its initializer. */
4806 bool
4807 decl_maybe_constant_var_p (tree decl)
4809 tree type = TREE_TYPE (decl);
4810 if (!VAR_P (decl))
4811 return false;
4812 if (DECL_DECLARED_CONSTEXPR_P (decl) && !TREE_THIS_VOLATILE (decl))
4813 return true;
4814 if (DECL_HAS_VALUE_EXPR_P (decl))
4815 /* A proxy isn't constant. */
4816 return false;
4817 if (TYPE_REF_P (type))
4818 /* References can be constant. */;
4819 else if (CP_TYPE_CONST_NON_VOLATILE_P (type)
4820 && INTEGRAL_OR_ENUMERATION_TYPE_P (type))
4821 /* And const integers. */;
4822 else
4823 return false;
4825 if (DECL_INITIAL (decl)
4826 && !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl))
4827 /* We know the initializer, and it isn't constant. */
4828 return false;
4829 else
4830 return true;
4833 /* Complain that DECL uses a type with no linkage. In C++98 mode this is
4834 called from grokfndecl and grokvardecl; in all modes it is called from
4835 cp_write_global_declarations. */
4837 void
4838 no_linkage_error (tree decl)
4840 if (cxx_dialect >= cxx11
4841 && (decl_defined_p (decl)
4842 /* Treat templates which limit_bad_template_recursion decided
4843 not to instantiate as if they were defined. */
4844 || (errorcount + sorrycount > 0
4845 && DECL_LANG_SPECIFIC (decl)
4846 && DECL_TEMPLATE_INFO (decl)
4847 && warning_suppressed_p (decl /* What warning? */))))
4848 /* In C++11 it's ok if the decl is defined. */
4849 return;
4851 if (DECL_LANG_SPECIFIC (decl) && DECL_MODULE_IMPORT_P (decl))
4852 /* An imported decl is ok. */
4853 return;
4855 tree t = no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/false);
4856 if (t == NULL_TREE)
4857 /* The type that got us on no_linkage_decls must have gotten a name for
4858 linkage purposes. */;
4859 else if (CLASS_TYPE_P (t) && TYPE_BEING_DEFINED (t))
4860 // FIXME: This is now invalid, as a DR to c++98
4861 /* The type might end up having a typedef name for linkage purposes. */
4862 vec_safe_push (no_linkage_decls, decl);
4863 else if (TYPE_UNNAMED_P (t))
4865 bool d = false;
4866 auto_diagnostic_group grp;
4867 if (cxx_dialect >= cxx11)
4869 /* If t is declared in a module CMI, then decl could actually
4870 be defined in a different TU, so don't warn since C++20. */
4871 tree relaxed = no_linkage_check (t, /*relaxed_p=*/true);
4872 if (relaxed != NULL_TREE)
4873 d = permerror (DECL_SOURCE_LOCATION (decl),
4874 "%q#D, declared using an unnamed type, "
4875 "is used but never defined", decl);
4876 else if (cxx_dialect < cxx20)
4877 d = pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wc__20_extensions,
4878 "%q#D, declared using an unnamed type, "
4879 "is used but not defined", decl);
4881 else if (DECL_EXTERN_C_P (decl))
4882 /* Allow this; it's pretty common in C. */;
4883 else if (VAR_P (decl))
4884 /* DRs 132, 319 and 389 seem to indicate types with
4885 no linkage can only be used to declare extern "C"
4886 entities. Since it's not always an error in the
4887 ISO C++ 90 Standard, we only issue a warning. */
4888 d = warning_at (DECL_SOURCE_LOCATION (decl), 0, "unnamed type "
4889 "with no linkage used to declare variable %q#D with "
4890 "linkage", decl);
4891 else
4892 d = permerror (DECL_SOURCE_LOCATION (decl), "unnamed type with no "
4893 "linkage used to declare function %q#D with linkage",
4894 decl);
4895 if (d && is_typedef_decl (TYPE_NAME (t)))
4896 inform (DECL_SOURCE_LOCATION (TYPE_NAME (t)), "%q#D does not refer "
4897 "to the unqualified type, so it is not used for linkage",
4898 TYPE_NAME (t));
4899 /* Suppress warning from check_global_declaration if needed. */
4900 if (d)
4901 suppress_warning (decl, OPT_Wunused);
4903 else if (cxx_dialect >= cxx11)
4905 if (VAR_P (decl) || !DECL_PURE_VIRTUAL_P (decl))
4907 /* Similarly for local types in a function with vague linkage or
4908 defined in a module CMI, then decl could actually be defined
4909 in a different TU, so don't warn since C++20. */
4910 bool d = false;
4911 tree relaxed = no_linkage_check (t, /*relaxed_p=*/true);
4912 if (relaxed != NULL_TREE)
4913 d = permerror (DECL_SOURCE_LOCATION (decl),
4914 "%q#D, declared using local type "
4915 "%qT, is used but never defined", decl, t);
4916 else if (cxx_dialect < cxx20)
4917 d = pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wc__20_extensions,
4918 "%q#D, declared using local type "
4919 "%qT, is used but not defined here", decl, t);
4920 /* Suppress warning from check_global_declaration if needed. */
4921 if (d)
4922 suppress_warning (decl, OPT_Wunused);
4925 else if (VAR_P (decl))
4926 warning_at (DECL_SOURCE_LOCATION (decl), 0, "type %qT with no linkage "
4927 "used to declare variable %q#D with linkage", t, decl);
4928 else
4929 permerror (DECL_SOURCE_LOCATION (decl), "type %qT with no linkage used "
4930 "to declare function %q#D with linkage", t, decl);
4933 /* Collect declarations from all namespaces relevant to SOURCE_FILE. */
4935 static void
4936 collect_all_refs (const char *source_file)
4938 collect_ada_namespace (global_namespace, source_file);
4941 /* Clear DECL_EXTERNAL for NODE. */
4943 static bool
4944 clear_decl_external (struct cgraph_node *node, void * /*data*/)
4946 DECL_EXTERNAL (node->decl) = 0;
4947 return false;
4950 /* Build up the function to run dynamic initializers for thread_local
4951 variables in this translation unit and alias the init functions for the
4952 individual variables to it. */
4954 static void
4955 handle_tls_init (void)
4957 tree vars = prune_vars_needing_no_initialization (&tls_aggregates);
4958 if (vars == NULL_TREE)
4959 return;
4961 location_t loc = DECL_SOURCE_LOCATION (TREE_VALUE (vars));
4963 write_out_vars (vars);
4965 tree guard = build_decl (loc, VAR_DECL, get_identifier ("__tls_guard"),
4966 boolean_type_node);
4967 TREE_PUBLIC (guard) = false;
4968 TREE_STATIC (guard) = true;
4969 DECL_ARTIFICIAL (guard) = true;
4970 DECL_IGNORED_P (guard) = true;
4971 TREE_USED (guard) = true;
4972 CP_DECL_THREAD_LOCAL_P (guard) = true;
4973 set_decl_tls_model (guard, decl_default_tls_model (guard));
4974 pushdecl_top_level_and_finish (guard, NULL_TREE);
4976 tree fn = get_local_tls_init_fn (loc);
4977 start_preparsed_function (fn, NULL_TREE, SF_PRE_PARSED);
4978 tree body = begin_function_body ();
4979 tree if_stmt = begin_if_stmt ();
4980 tree cond = cp_build_unary_op (TRUTH_NOT_EXPR, guard, false,
4981 tf_warning_or_error);
4982 finish_if_stmt_cond (cond, if_stmt);
4983 finish_expr_stmt (cp_build_modify_expr (loc, guard, NOP_EXPR,
4984 boolean_true_node,
4985 tf_warning_or_error));
4986 for (; vars; vars = TREE_CHAIN (vars))
4988 tree var = TREE_VALUE (vars);
4989 tree init = TREE_PURPOSE (vars);
4990 one_static_initialization_or_destruction (/*initp=*/true, var, init);
4992 /* Output init aliases even with -fno-extern-tls-init. */
4993 if (TARGET_SUPPORTS_ALIASES && TREE_PUBLIC (var))
4995 tree single_init_fn = get_tls_init_fn (var);
4996 if (single_init_fn == NULL_TREE)
4997 continue;
4998 cgraph_node *alias
4999 = cgraph_node::get_create (fn)->create_same_body_alias
5000 (single_init_fn, fn);
5001 gcc_assert (alias != NULL);
5005 finish_then_clause (if_stmt);
5006 finish_if_stmt (if_stmt);
5007 finish_function_body (body);
5008 expand_or_defer_fn (finish_function (/*inline_p=*/false));
5011 /* We're at the end of compilation, so generate any mangling aliases that
5012 we've been saving up, if DECL is going to be output and ID2 isn't
5013 already taken by another declaration. */
5015 static void
5016 generate_mangling_alias (tree decl, tree id2)
5018 struct cgraph_node *n = NULL;
5020 if (TREE_CODE (decl) == FUNCTION_DECL)
5022 n = cgraph_node::get (decl);
5023 if (!n)
5024 /* Don't create an alias to an unreferenced function. */
5025 return;
5028 tree *slot
5029 = mangled_decls->find_slot_with_hash (id2, IDENTIFIER_HASH_VALUE (id2),
5030 INSERT);
5032 /* If there's a declaration already using this mangled name,
5033 don't create a compatibility alias that conflicts. */
5034 if (*slot)
5035 return;
5037 tree alias = make_alias_for (decl, id2);
5038 *slot = alias;
5040 DECL_IGNORED_P (alias) = 1;
5041 TREE_PUBLIC (alias) = TREE_PUBLIC (decl);
5042 DECL_VISIBILITY (alias) = DECL_VISIBILITY (decl);
5043 if (vague_linkage_p (decl))
5044 DECL_WEAK (alias) = 1;
5046 if (n)
5047 n->create_same_body_alias (alias, decl);
5048 else
5049 varpool_node::create_extra_name_alias (alias, decl);
5052 /* Note that we might want to emit an alias with the symbol ID2 for DECL at
5053 the end of translation, for compatibility across bugs in the mangling
5054 implementation. */
5056 void
5057 note_mangling_alias (tree decl, tree id2)
5059 if (TARGET_SUPPORTS_ALIASES)
5061 if (!defer_mangling_aliases)
5062 generate_mangling_alias (decl, id2);
5063 else
5065 vec_safe_push (mangling_aliases, decl);
5066 vec_safe_push (mangling_aliases, id2);
5071 /* Emit all mangling aliases that were deferred up to this point. */
5073 void
5074 generate_mangling_aliases ()
5076 while (!vec_safe_is_empty (mangling_aliases))
5078 tree id2 = mangling_aliases->pop();
5079 tree decl = mangling_aliases->pop();
5080 generate_mangling_alias (decl, id2);
5082 defer_mangling_aliases = false;
5085 /* Record a mangling of DECL, whose DECL_ASSEMBLER_NAME has just been
5086 set. NEED_WARNING is true if we must warn about collisions. We do
5087 this to spot changes in mangling that may require compatibility
5088 aliases. */
5090 void
5091 record_mangling (tree decl, bool need_warning)
5093 if (!mangled_decls)
5094 mangled_decls = hash_table<mangled_decl_hash>::create_ggc (499);
5096 gcc_checking_assert (DECL_ASSEMBLER_NAME_SET_P (decl));
5097 tree id = DECL_ASSEMBLER_NAME_RAW (decl);
5098 tree *slot
5099 = mangled_decls->find_slot_with_hash (id, IDENTIFIER_HASH_VALUE (id),
5100 INSERT);
5102 /* If this is already an alias, cancel the alias, because the real
5103 decl takes precedence. */
5104 if (*slot && DECL_ARTIFICIAL (*slot) && DECL_IGNORED_P (*slot))
5106 if (symtab_node *n = symtab_node::get (*slot))
5108 if (n->cpp_implicit_alias)
5109 /* Actually removing the node isn't safe if other code is already
5110 holding a pointer to it, so just neutralize it. */
5111 n->reset ();
5113 else
5114 /* analyze_functions might have already removed the alias from the
5115 symbol table if it's internal. */
5116 gcc_checking_assert (!TREE_PUBLIC (*slot));
5118 *slot = NULL_TREE;
5121 if (!*slot)
5122 *slot = decl;
5123 else if (need_warning)
5125 auto_diagnostic_group d;
5126 error_at (DECL_SOURCE_LOCATION (decl),
5127 "mangling of %q#D as %qE conflicts with a previous mangle",
5128 decl, id);
5129 inform (DECL_SOURCE_LOCATION (*slot),
5130 "previous mangling %q#D", *slot);
5131 inform (DECL_SOURCE_LOCATION (decl),
5132 "a later %<-fabi-version=%> (or =0)"
5133 " avoids this error with a change in mangling");
5134 *slot = decl;
5138 /* The mangled name of DECL is being forcibly changed to NAME. Remove
5139 any existing knowledge of DECL's mangled name meaning DECL. */
5141 void
5142 overwrite_mangling (tree decl, tree name)
5144 if (tree id = DECL_ASSEMBLER_NAME_RAW (decl))
5145 if ((TREE_CODE (decl) == VAR_DECL
5146 || TREE_CODE (decl) == FUNCTION_DECL)
5147 && mangled_decls)
5148 if (tree *slot
5149 = mangled_decls->find_slot_with_hash (id, IDENTIFIER_HASH_VALUE (id),
5150 NO_INSERT))
5151 if (*slot == decl)
5153 mangled_decls->clear_slot (slot);
5155 /* If this is an alias, remove it from the symbol table. */
5156 if (DECL_ARTIFICIAL (decl) && DECL_IGNORED_P (decl))
5157 if (symtab_node *n = symtab_node::get (decl))
5158 if (n->cpp_implicit_alias)
5159 n->remove ();
5162 DECL_ASSEMBLER_NAME_RAW (decl) = name;
5165 /* The entire file is now complete. If requested, dump everything
5166 to a file. */
5168 static void
5169 dump_tu (void)
5171 dump_flags_t flags;
5172 if (FILE *stream = dump_begin (raw_dump_id, &flags))
5174 dump_node (global_namespace, flags & ~TDF_SLIM, stream);
5175 dump_end (raw_dump_id, stream);
5179 static location_t locus_at_end_of_parsing;
5181 /* Check the deallocation functions for CODE to see if we want to warn that
5182 only one was defined. */
5184 static void
5185 maybe_warn_sized_delete (enum tree_code code)
5187 tree sized = NULL_TREE;
5188 tree unsized = NULL_TREE;
5190 for (ovl_iterator iter (get_global_binding (ovl_op_identifier (false, code)));
5191 iter; ++iter)
5193 tree fn = *iter;
5194 /* We're only interested in usual deallocation functions. */
5195 if (!usual_deallocation_fn_p (fn))
5196 continue;
5197 if (FUNCTION_ARG_CHAIN (fn) == void_list_node)
5198 unsized = fn;
5199 else
5200 sized = fn;
5202 if (DECL_INITIAL (unsized) && !DECL_INITIAL (sized))
5203 warning_at (DECL_SOURCE_LOCATION (unsized), OPT_Wsized_deallocation,
5204 "the program should also define %qD", sized);
5205 else if (!DECL_INITIAL (unsized) && DECL_INITIAL (sized))
5206 warning_at (DECL_SOURCE_LOCATION (sized), OPT_Wsized_deallocation,
5207 "the program should also define %qD", unsized);
5210 /* Check the global deallocation functions to see if we want to warn about
5211 defining unsized without sized (or vice versa). */
5213 static void
5214 maybe_warn_sized_delete ()
5216 if (!flag_sized_deallocation || !warn_sized_deallocation)
5217 return;
5218 maybe_warn_sized_delete (DELETE_EXPR);
5219 maybe_warn_sized_delete (VEC_DELETE_EXPR);
5222 /* Earlier we left PTRMEM_CST in variable initializers alone so that we could
5223 look them up when evaluating non-type template parameters. Now we need to
5224 lower them to something the back end can understand. */
5226 static void
5227 lower_var_init ()
5229 varpool_node *node;
5230 FOR_EACH_VARIABLE (node)
5232 tree d = node->decl;
5233 if (tree init = DECL_INITIAL (d))
5234 DECL_INITIAL (d) = cplus_expand_constant (init);
5238 /* This routine is called at the end of compilation.
5239 Its job is to create all the code needed to initialize and
5240 destroy the global aggregates. We do the destruction
5241 first, since that way we only need to reverse the decls once. */
5243 void
5244 c_parse_final_cleanups (void)
5246 size_t i;
5247 tree decl;
5249 locus_at_end_of_parsing = input_location;
5250 /* We're done parsing. */
5251 at_eof = 1;
5253 /* Bad parse errors. Just forget about it. */
5254 if (! global_bindings_p () || current_class_type
5255 || !vec_safe_is_empty (decl_namespace_list))
5256 return;
5258 /* This is the point to write out a PCH if we're doing that.
5259 In that case we do not want to do anything else. */
5260 if (pch_file)
5262 /* Mangle all symbols at PCH creation time. */
5263 symtab_node *node;
5264 FOR_EACH_SYMBOL (node)
5265 if (! is_a <varpool_node *> (node)
5266 || ! DECL_HARD_REGISTER (node->decl))
5267 DECL_ASSEMBLER_NAME (node->decl);
5268 c_common_write_pch ();
5269 dump_tu ();
5270 /* Ensure even the callers don't try to finalize the CU. */
5271 flag_syntax_only = 1;
5272 return;
5275 timevar_stop (TV_PHASE_PARSING);
5276 timevar_start (TV_PHASE_DEFERRED);
5278 symtab->process_same_body_aliases ();
5280 /* Handle -fdump-ada-spec[-slim] */
5281 if (flag_dump_ada_spec || flag_dump_ada_spec_slim)
5283 collect_source_ref (main_input_filename);
5284 if (!flag_dump_ada_spec_slim)
5285 collect_source_refs (global_namespace);
5287 dump_ada_specs (collect_all_refs, cpp_check);
5290 /* FIXME - huh? was input_line -= 1;*/
5292 /* We now have to write out all the stuff we put off writing out.
5293 These include:
5295 o Template specializations that we have not yet instantiated,
5296 but which are needed.
5297 o Initialization and destruction for non-local objects with
5298 static storage duration. (Local objects with static storage
5299 duration are initialized when their scope is first entered,
5300 and are cleaned up via atexit.)
5301 o Virtual function tables.
5303 All of these may cause others to be needed. For example,
5304 instantiating one function may cause another to be needed, and
5305 generating the initializer for an object may cause templates to be
5306 instantiated, etc., etc. */
5308 emit_support_tinfos ();
5310 /* Track vtables we want to emit that refer to consteval functions. */
5311 auto_vec<tree> consteval_vtables;
5313 int retries = 0;
5314 unsigned ssdf_count = 0, omp_ssdf_count = 0;
5315 for (bool reconsider = true; reconsider; retries++)
5317 reconsider = false;
5319 /* If there are templates that we've put off instantiating, do
5320 them now. */
5321 instantiate_pending_templates (retries);
5322 ggc_collect ();
5324 if (header_module_p ())
5325 /* A header modules initializations are handled in its
5326 importer. */
5327 continue;
5329 /* Write out virtual tables as required. Writing out the
5330 virtual table for a template class may cause the
5331 instantiation of members of that class. If we write out
5332 vtables then we remove the class from our list so we don't
5333 have to look at it again. */
5334 tree t;
5335 for (i = keyed_classes->length ();
5336 keyed_classes->iterate (--i, &t);)
5337 if (maybe_emit_vtables (t, consteval_vtables))
5339 reconsider = true;
5340 keyed_classes->unordered_remove (i);
5342 /* The input_location may have been changed during marking of
5343 vtable entries. */
5344 input_location = locus_at_end_of_parsing;
5346 /* Write out needed type info variables. We have to be careful
5347 looping through unemitted decls, because emit_tinfo_decl may
5348 cause other variables to be needed. New elements will be
5349 appended, and we remove from the vector those that actually
5350 get emitted. */
5351 for (i = unemitted_tinfo_decls->length ();
5352 unemitted_tinfo_decls->iterate (--i, &t);)
5353 if (DECL_INITIAL (t) || emit_tinfo_decl (t))
5355 reconsider = true;
5356 unemitted_tinfo_decls->unordered_remove (i);
5359 /* The list of objects with static storage duration is built up
5360 in reverse order. We clear STATIC_AGGREGATES so that any new
5361 aggregates added during the initialization of these will be
5362 initialized in the correct order when we next come around the
5363 loop. */
5364 if (tree vars = prune_vars_needing_no_initialization (&static_aggregates))
5366 if (flag_openmp)
5367 /* Add initializer information from VARS into
5368 DYNAMIC_INITIALIZERS. */
5369 for (t = vars; t; t = TREE_CHAIN (t))
5370 hash_map_safe_put<hm_ggc> (dynamic_initializers,
5371 TREE_VALUE (t), TREE_PURPOSE (t));
5373 /* Make sure the back end knows about all the variables. */
5374 write_out_vars (vars);
5376 function_depth++; // Disable GC
5377 priority_map_t *parts[4] = {nullptr, nullptr, nullptr, nullptr};
5378 partition_vars_for_init_fini (vars, parts);
5379 tree host_init_fini[2] = { NULL_TREE, NULL_TREE };
5381 for (unsigned initp = 2; initp--;)
5382 if (parts[initp])
5383 for (auto iter : *parts[initp])
5385 auto list = iter.second;
5386 if (initp)
5387 // Partitioning kept the vars in reverse order.
5388 // We only want that for dtors.
5389 list = nreverse (list);
5390 host_init_fini[initp]
5391 = emit_partial_init_fini_fn (initp, iter.first, list,
5392 ssdf_count++,
5393 locus_at_end_of_parsing,
5394 NULL_TREE);
5397 if (flag_openmp)
5399 priority_map_t **omp_parts = parts + 2;
5400 for (unsigned initp = 2; initp--;)
5401 if (omp_parts[initp])
5402 for (auto iter : *omp_parts[initp])
5404 auto list = iter.second;
5405 if (initp)
5406 // Partitioning kept the vars in reverse order.
5407 // We only want that for dtors.
5408 list = nreverse (list);
5409 emit_partial_init_fini_fn (initp, iter.first, list,
5410 omp_ssdf_count++,
5411 locus_at_end_of_parsing,
5412 host_init_fini[initp]);
5416 function_depth--; // Re-enable GC
5418 /* All those initializations and finalizations might cause
5419 us to need more inline functions, more template
5420 instantiations, etc. */
5421 reconsider = true;
5424 /* Now do the same for thread_local variables. */
5425 handle_tls_init ();
5427 /* Go through the set of inline functions whose bodies have not
5428 been emitted yet. If out-of-line copies of these functions
5429 are required, emit them. */
5430 FOR_EACH_VEC_SAFE_ELT (deferred_fns, i, decl)
5432 /* Does it need synthesizing? */
5433 if (DECL_DEFAULTED_FN (decl) && ! DECL_INITIAL (decl)
5434 && (! DECL_REALLY_EXTERN (decl) || possibly_inlined_p (decl)))
5436 /* Even though we're already at the top-level, we push
5437 there again. That way, when we pop back a few lines
5438 hence, all of our state is restored. Otherwise,
5439 finish_function doesn't clean things up, and we end
5440 up with CURRENT_FUNCTION_DECL set. */
5441 push_to_top_level ();
5442 /* The decl's location will mark where it was first
5443 needed. Save that so synthesize method can indicate
5444 where it was needed from, in case of error */
5445 input_location = DECL_SOURCE_LOCATION (decl);
5446 synthesize_method (decl);
5447 pop_from_top_level ();
5448 reconsider = true;
5451 if (!DECL_INITIAL (decl) && decl_tls_wrapper_p (decl))
5452 generate_tls_wrapper (decl);
5454 if (!DECL_SAVED_TREE (decl))
5455 continue;
5457 cgraph_node *node = cgraph_node::get_create (decl);
5459 /* We lie to the back end, pretending that some functions
5460 are not defined when they really are. This keeps these
5461 functions from being put out unnecessarily. But, we must
5462 stop lying when the functions are referenced, or if they
5463 are not comdat since they need to be put out now. If
5464 DECL_INTERFACE_KNOWN, then we have already set
5465 DECL_EXTERNAL appropriately, so there's no need to check
5466 again, and we do not want to clear DECL_EXTERNAL if a
5467 previous call to import_export_decl set it.
5469 This is done in a separate for cycle, because if some
5470 deferred function is contained in another deferred
5471 function later in deferred_fns varray,
5472 rest_of_compilation would skip this function and we
5473 really cannot expand the same function twice. */
5474 import_export_decl (decl);
5475 if (DECL_NOT_REALLY_EXTERN (decl)
5476 && DECL_INITIAL (decl)
5477 && decl_needed_p (decl))
5479 if (node->cpp_implicit_alias)
5480 node = node->get_alias_target ();
5482 node->call_for_symbol_thunks_and_aliases (clear_decl_external,
5483 NULL, true);
5484 /* If we mark !DECL_EXTERNAL one of the symbols in some comdat
5485 group, we need to mark all symbols in the same comdat group
5486 that way. */
5487 if (node->same_comdat_group)
5488 for (cgraph_node *next
5489 = dyn_cast<cgraph_node *> (node->same_comdat_group);
5490 next != node;
5491 next = dyn_cast<cgraph_node *> (next->same_comdat_group))
5492 next->call_for_symbol_thunks_and_aliases (clear_decl_external,
5493 NULL, true);
5496 /* If we're going to need to write this function out, and
5497 there's already a body for it, create RTL for it now.
5498 (There might be no body if this is a method we haven't
5499 gotten around to synthesizing yet.) */
5500 if (!DECL_EXTERNAL (decl)
5501 && decl_needed_p (decl)
5502 && !TREE_ASM_WRITTEN (decl)
5503 && !DECL_IMMEDIATE_FUNCTION_P (decl)
5504 && !node->definition)
5506 /* We will output the function; no longer consider it in this
5507 loop. */
5508 DECL_DEFER_OUTPUT (decl) = 0;
5509 /* Generate RTL for this function now that we know we
5510 need it. */
5511 expand_or_defer_fn (decl);
5512 reconsider = true;
5516 if (wrapup_namespace_globals ())
5517 reconsider = true;
5519 /* Static data members are just like namespace-scope globals. */
5520 FOR_EACH_VEC_SAFE_ELT (pending_statics, i, decl)
5522 if (var_finalized_p (decl) || DECL_REALLY_EXTERN (decl)
5523 /* Don't write it out if we haven't seen a definition. */
5524 || DECL_IN_AGGR_P (decl))
5525 continue;
5526 import_export_decl (decl);
5527 /* If this static data member is needed, provide it to the
5528 back end. */
5529 if (DECL_NOT_REALLY_EXTERN (decl) && decl_needed_p (decl))
5530 DECL_EXTERNAL (decl) = 0;
5533 if (vec_safe_length (pending_statics) != 0
5534 && wrapup_global_declarations (pending_statics->address (),
5535 pending_statics->length ()))
5536 reconsider = true;
5539 /* All templates have been instantiated. */
5540 at_eof = 2;
5542 void *module_cookie = finish_module_processing (parse_in);
5544 lower_var_init ();
5546 generate_mangling_aliases ();
5548 /* All used inline functions must have a definition at this point. */
5549 FOR_EACH_VEC_SAFE_ELT (deferred_fns, i, decl)
5551 if (/* Check online inline functions that were actually used. */
5552 DECL_ODR_USED (decl) && DECL_DECLARED_INLINE_P (decl)
5553 /* If the definition actually was available here, then the
5554 fact that the function was not defined merely represents
5555 that for some reason (use of a template repository,
5556 #pragma interface, etc.) we decided not to emit the
5557 definition here. */
5558 && !DECL_INITIAL (decl)
5559 /* A defaulted fn or TLS wrapper in a header module can be
5560 synthesized on demand later. (In non-header modules we
5561 should have synthesized it above.) */
5562 && !(header_module_p ()
5563 && (DECL_DEFAULTED_FN (decl) || decl_tls_wrapper_p (decl)))
5564 /* Don't complain if the template was defined. */
5565 && !((DECL_TEMPLATE_INSTANTIATION (decl)
5566 || DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl))
5567 && DECL_INITIAL (DECL_TEMPLATE_RESULT
5568 (template_for_substitution (decl))))
5569 && warning_at (DECL_SOURCE_LOCATION (decl), 0,
5570 "inline function %qD used but never defined", decl))
5571 /* Avoid a duplicate warning from check_global_declaration. */
5572 suppress_warning (decl, OPT_Wunused);
5575 /* So must decls that use a type with no linkage. */
5576 FOR_EACH_VEC_SAFE_ELT (no_linkage_decls, i, decl)
5577 no_linkage_error (decl);
5579 maybe_warn_sized_delete ();
5581 // Place the init fns in the right order. We need to do this now,
5582 // so that any module init will go at the start.
5583 if (static_init_fini_fns[true])
5584 for (auto iter : *static_init_fini_fns[true])
5585 iter.second = nreverse (iter.second);
5587 if (flag_openmp && static_init_fini_fns[2 + true])
5588 for (auto iter : *static_init_fini_fns[2 + true])
5589 iter.second = nreverse (iter.second);
5591 /* Now we've instantiated all templates. Now we can escalate the functions
5592 we squirreled away earlier. */
5593 process_and_check_pending_immediate_escalating_fns ();
5595 /* Then, do the Objective-C stuff. This is where all the
5596 Objective-C module stuff gets generated (symtab,
5597 class/protocol/selector lists etc). This must be done after C++
5598 templates, destructors etc. so that selectors used in C++
5599 templates are properly allocated. */
5600 if (c_dialect_objc ())
5601 objc_write_global_declarations ();
5603 bool has_module_inits = module_determine_import_inits ();
5604 bool has_objc_init = c_dialect_objc () && objc_static_init_needed_p ();
5605 if (has_module_inits || has_objc_init)
5607 input_location = locus_at_end_of_parsing;
5608 tree body = start_partial_init_fini_fn (true, DEFAULT_INIT_PRIORITY,
5609 ssdf_count++, false);
5610 /* For Objective-C++, we may need to initialize metadata found
5611 in this module. This must be done _before_ any other static
5612 initializations. */
5613 if (has_objc_init)
5614 objc_generate_static_init_call (NULL_TREE);
5615 if (has_module_inits)
5616 module_add_import_initializers ();
5617 input_location = locus_at_end_of_parsing;
5618 finish_partial_init_fini_fn (body);
5621 if (module_global_init_needed ())
5623 // Make sure there's a default priority entry.
5624 if (!static_init_fini_fns[true])
5625 static_init_fini_fns[true] = priority_map_t::create_ggc ();
5626 if (static_init_fini_fns[true]->get_or_insert (DEFAULT_INIT_PRIORITY))
5627 has_module_inits = true;
5629 if (flag_openmp)
5631 if (!static_init_fini_fns[2 + true])
5632 static_init_fini_fns[2 + true] = priority_map_t::create_ggc ();
5633 static_init_fini_fns[2 + true]->get_or_insert (DEFAULT_INIT_PRIORITY);
5637 /* Generate initialization and destruction functions for all
5638 priorities for which they are required. They have C-language
5639 linkage. */
5640 push_lang_context (lang_name_c);
5641 for (unsigned initp = 4; initp--;)
5642 if (static_init_fini_fns[initp])
5644 for (auto iter : *static_init_fini_fns[initp])
5645 generate_ctor_or_dtor_function (initp & 1, iter.first, iter.second,
5646 locus_at_end_of_parsing,
5647 (initp & 2) != 0);
5648 static_init_fini_fns[initp] = nullptr;
5650 pop_lang_context ();
5652 fini_modules (parse_in, module_cookie, has_module_inits);
5654 /* Generate any missing aliases. */
5655 maybe_apply_pending_pragma_weaks ();
5657 if (flag_vtable_verify)
5659 vtv_recover_class_info ();
5660 vtv_compute_class_hierarchy_transitive_closure ();
5661 vtv_build_vtable_verify_fndecl ();
5664 perform_deferred_noexcept_checks ();
5666 fini_constexpr ();
5667 cp_tree_c_finish_parsing ();
5668 clear_consteval_vfns (consteval_vtables);
5670 /* The entire file is now complete. If requested, dump everything
5671 to a file. */
5672 dump_tu ();
5674 if (flag_detailed_statistics)
5676 dump_tree_statistics ();
5677 dump_time_statistics ();
5680 timevar_stop (TV_PHASE_DEFERRED);
5681 timevar_start (TV_PHASE_PARSING);
5683 /* Indicate that we're done with front end processing. */
5684 at_eof = 3;
5687 /* Perform any post compilation-proper cleanups for the C++ front-end.
5688 This should really go away. No front-end should need to do
5689 anything past the compilation process. */
5691 void
5692 cxx_post_compilation_parsing_cleanups (void)
5694 timevar_start (TV_PHASE_LATE_PARSING_CLEANUPS);
5696 if (flag_vtable_verify)
5698 /* Generate the special constructor initialization function that
5699 calls __VLTRegisterPairs, and give it a very high
5700 initialization priority. This must be done after
5701 finalize_compilation_unit so that we have accurate
5702 information about which vtable will actually be emitted. */
5703 vtv_generate_init_routine ();
5706 input_location = locus_at_end_of_parsing;
5708 if (flag_checking)
5709 validate_conversion_obstack ();
5711 timevar_stop (TV_PHASE_LATE_PARSING_CLEANUPS);
5714 /* FN is an OFFSET_REF, DOTSTAR_EXPR or MEMBER_REF indicating the
5715 function to call in parse-tree form; it has not yet been
5716 semantically analyzed. ARGS are the arguments to the function.
5717 They have already been semantically analyzed. This may change
5718 ARGS. */
5720 tree
5721 build_offset_ref_call_from_tree (tree fn, vec<tree, va_gc> **args,
5722 tsubst_flags_t complain)
5724 tree orig_fn;
5725 vec<tree, va_gc> *orig_args = NULL;
5726 tree expr;
5727 tree object;
5729 orig_fn = fn;
5730 object = TREE_OPERAND (fn, 0);
5732 if (processing_template_decl)
5734 gcc_assert (TREE_CODE (fn) == DOTSTAR_EXPR
5735 || TREE_CODE (fn) == MEMBER_REF);
5736 if (type_dependent_expression_p (fn)
5737 || any_type_dependent_arguments_p (*args))
5738 return build_min_nt_call_vec (fn, *args);
5740 orig_args = make_tree_vector_copy (*args);
5742 /* Transform the arguments and add the implicit "this"
5743 parameter. */
5744 if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
5746 if (TREE_CODE (fn) == DOTSTAR_EXPR)
5747 object = cp_build_addr_expr (object, complain);
5748 vec_safe_insert (*args, 0, object);
5752 /* A qualified name corresponding to a bound pointer-to-member is
5753 represented as an OFFSET_REF:
5755 struct B { void g(); };
5756 void (B::*p)();
5757 void B::g() { (this->*p)(); } */
5758 if (TREE_CODE (fn) == OFFSET_REF)
5760 tree object_addr = cp_build_addr_expr (object, complain);
5761 fn = TREE_OPERAND (fn, 1);
5762 fn = get_member_function_from_ptrfunc (&object_addr, fn,
5763 complain);
5764 vec_safe_insert (*args, 0, object_addr);
5767 if (CLASS_TYPE_P (TREE_TYPE (fn)))
5768 expr = build_op_call (fn, args, complain);
5769 else
5770 expr = cp_build_function_call_vec (fn, args, complain);
5771 if (processing_template_decl && expr != error_mark_node)
5772 expr = build_min_non_dep_call_vec (expr, orig_fn, orig_args);
5774 if (orig_args != NULL)
5775 release_tree_vector (orig_args);
5777 return expr;
5781 void
5782 check_default_args (tree x)
5784 tree arg = TYPE_ARG_TYPES (TREE_TYPE (x));
5785 bool saw_def = false;
5786 bool noted_first_def = false;
5787 int idx_of_first_default_arg = 0;
5788 location_t loc_of_first_default_arg = UNKNOWN_LOCATION;
5789 int i = 0 - (TREE_CODE (TREE_TYPE (x)) == METHOD_TYPE);
5790 tree fndecl = STRIP_TEMPLATE (x);
5791 auto_diagnostic_group d;
5792 for (; arg && arg != void_list_node; arg = TREE_CHAIN (arg), ++i)
5794 if (TREE_PURPOSE (arg))
5796 if (!saw_def)
5798 saw_def = true;
5799 idx_of_first_default_arg = i;
5800 location_t loc = get_fndecl_argument_location (fndecl, i);
5801 if (loc != DECL_SOURCE_LOCATION (x))
5802 loc_of_first_default_arg = loc;
5805 else if (saw_def && !PACK_EXPANSION_P (TREE_VALUE (arg)))
5807 error_at (get_fndecl_argument_location (fndecl, i),
5808 "default argument missing for parameter %P of %q#D", i, x);
5809 if (loc_of_first_default_arg != UNKNOWN_LOCATION
5810 && !noted_first_def)
5812 inform (loc_of_first_default_arg,
5813 "...following parameter %P which has a default argument",
5814 idx_of_first_default_arg);
5815 noted_first_def = true;
5817 TREE_PURPOSE (arg) = error_mark_node;
5822 /* Return true if function DECL can be inlined. This is used to force
5823 instantiation of methods that might be interesting for inlining. */
5824 bool
5825 possibly_inlined_p (tree decl)
5827 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
5828 if (DECL_UNINLINABLE (decl))
5829 return false;
5830 if (!optimize)
5831 return DECL_DECLARED_INLINE_P (decl);
5832 /* When optimizing, we might inline everything when flatten
5833 attribute or heuristics inlining for size or autoinlining
5834 is used. */
5835 return true;
5838 /* If DECL is a function or variable template specialization, instantiate
5839 its definition now. */
5841 void
5842 maybe_instantiate_decl (tree decl)
5844 if (VAR_OR_FUNCTION_DECL_P (decl)
5845 && DECL_LANG_SPECIFIC (decl)
5846 && DECL_TEMPLATE_INFO (decl)
5847 && !uses_template_parms (DECL_TI_ARGS (decl)))
5849 /* Instantiating a function will result in garbage collection. We
5850 must treat this situation as if we were within the body of a
5851 function so as to avoid collecting live data only referenced from
5852 the stack (such as overload resolution candidates). */
5853 ++function_depth;
5854 instantiate_decl (decl, /*defer_ok=*/false,
5855 /*expl_inst_class_mem_p=*/false);
5856 --function_depth;
5860 /* Error if the DECL is unavailable (unless this is currently suppressed).
5861 Maybe warn if DECL is deprecated, subject to COMPLAIN. Returns true if
5862 an error or warning was emitted. */
5864 bool
5865 cp_handle_deprecated_or_unavailable (tree decl, tsubst_flags_t complain)
5867 if (!decl)
5868 return false;
5870 if ((complain & tf_error)
5871 && deprecated_state != UNAVAILABLE_DEPRECATED_SUPPRESS)
5873 if (TREE_UNAVAILABLE (decl))
5875 error_unavailable_use (decl, NULL_TREE);
5876 return true;
5878 else
5880 /* Perhaps this is an unavailable typedef. */
5881 if (TYPE_P (decl)
5882 && TYPE_NAME (decl)
5883 && TREE_UNAVAILABLE (TYPE_NAME (decl)))
5885 decl = TYPE_NAME (decl);
5886 /* Don't error within members of a unavailable type. */
5887 if (TYPE_P (decl)
5888 && currently_open_class (decl))
5889 return false;
5891 error_unavailable_use (decl, NULL_TREE);
5892 return true;
5895 /* Carry on to consider deprecatedness. */
5898 if (!(complain & tf_warning)
5899 || deprecated_state == DEPRECATED_SUPPRESS
5900 || deprecated_state == UNAVAILABLE_DEPRECATED_SUPPRESS)
5901 return false;
5903 if (!TREE_DEPRECATED (decl))
5905 /* Perhaps this is a deprecated typedef. */
5906 if (TYPE_P (decl) && TYPE_NAME (decl))
5907 decl = TYPE_NAME (decl);
5909 if (!TREE_DEPRECATED (decl))
5910 return false;
5913 /* Don't warn within members of a deprecated type. */
5914 if (TYPE_P (decl)
5915 && currently_open_class (decl))
5916 return false;
5918 bool warned = false;
5919 if (cxx_dialect >= cxx11
5920 && DECL_P (decl)
5921 && DECL_ARTIFICIAL (decl)
5922 && DECL_IOBJ_MEMBER_FUNCTION_P (decl)
5923 && copy_fn_p (decl))
5925 /* Don't warn if the flag was disabled around the class definition
5926 (c++/94492). */
5927 if (warning_enabled_at (DECL_SOURCE_LOCATION (decl),
5928 OPT_Wdeprecated_copy))
5930 auto_diagnostic_group d;
5931 tree ctx = DECL_CONTEXT (decl);
5932 tree other = classtype_has_depr_implicit_copy (ctx);
5933 int opt = (DECL_DESTRUCTOR_P (other)
5934 ? OPT_Wdeprecated_copy_dtor
5935 : OPT_Wdeprecated_copy);
5936 warned = warning (opt, "implicitly-declared %qD is deprecated",
5937 decl);
5938 if (warned)
5939 inform (DECL_SOURCE_LOCATION (other),
5940 "because %qT has user-provided %qD",
5941 ctx, other);
5944 else
5945 warned = warn_deprecated_use (decl, NULL_TREE);
5947 return warned;
5950 /* Like above, but takes into account outer scopes. */
5952 void
5953 cp_warn_deprecated_use_scopes (tree scope)
5955 while (scope
5956 && scope != error_mark_node
5957 && scope != global_namespace)
5959 if ((TREE_CODE (scope) == NAMESPACE_DECL || OVERLOAD_TYPE_P (scope))
5960 && cp_handle_deprecated_or_unavailable (scope))
5961 return;
5962 if (TYPE_P (scope))
5963 scope = CP_TYPE_CONTEXT (scope);
5964 else
5965 scope = CP_DECL_CONTEXT (scope);
5969 /* True if DECL or its enclosing scope have unbound template parameters. */
5971 bool
5972 decl_dependent_p (tree decl)
5974 if (DECL_FUNCTION_SCOPE_P (decl)
5975 || TREE_CODE (decl) == CONST_DECL
5976 || TREE_CODE (decl) == USING_DECL
5977 || TREE_CODE (decl) == FIELD_DECL)
5978 decl = CP_DECL_CONTEXT (decl);
5979 if (tree tinfo = get_template_info (decl))
5980 if (any_dependent_template_arguments_p (TI_ARGS (tinfo)))
5981 return true;
5982 if (LAMBDA_FUNCTION_P (decl)
5983 && dependent_type_p (DECL_CONTEXT (decl)))
5984 return true;
5985 return false;
5988 /* [basic.def.odr] A function is named [and therefore odr-used] by an
5989 expression or conversion if it is the selected member of an overload set in
5990 an overload resolution performed as part of forming that expression or
5991 conversion, unless it is a pure virtual function and either the expression
5992 is not an id-expression naming the function with an explicitly qualified
5993 name or the expression forms a pointer to member.
5995 Mostly, we call mark_used in places that actually do something with a
5996 function, like build_over_call. But in a few places we end up with a
5997 non-overloaded FUNCTION_DECL that we aren't going to do any more with, like
5998 convert_to_void. resolve_nondeduced_context is called in those places,
5999 but it's also called in too many other places. */
6001 bool
6002 mark_single_function (tree expr, tsubst_flags_t complain)
6004 expr = maybe_undo_parenthesized_ref (expr);
6005 expr = tree_strip_any_location_wrapper (expr);
6007 if (is_overloaded_fn (expr) == 1
6008 && !mark_used (expr, complain)
6009 && !(complain & tf_error))
6010 return false;
6011 return true;
6014 /* Mark DECL (either a _DECL or a BASELINK) as "used" in the program.
6015 If DECL is a specialization or implicitly declared class member,
6016 generate the actual definition. Return false if something goes
6017 wrong, true otherwise. */
6019 bool
6020 mark_used (tree decl, tsubst_flags_t complain /* = tf_warning_or_error */)
6022 /* If we're just testing conversions or resolving overloads, we
6023 don't want any permanent effects like forcing functions to be
6024 output or instantiating templates. */
6025 if ((complain & tf_conv))
6026 return true;
6028 /* If DECL is a BASELINK for a single function, then treat it just
6029 like the DECL for the function. Otherwise, if the BASELINK is
6030 for an overloaded function, we don't know which function was
6031 actually used until after overload resolution. */
6032 if (BASELINK_P (decl))
6034 tree fns = BASELINK_FUNCTIONS (decl);
6035 if (really_overloaded_fn (fns))
6036 return true;
6037 fns = OVL_FIRST (fns);
6038 if (!mark_used (fns, complain))
6039 return false;
6040 /* We might have deduced its return type. */
6041 TREE_TYPE (decl) = TREE_TYPE (fns);
6042 return true;
6045 if (!DECL_P (decl))
6046 return true;
6048 /* Set TREE_USED for the benefit of -Wunused. */
6049 TREE_USED (decl) = true;
6051 /* And for structured bindings also the underlying decl. */
6052 if (DECL_DECOMPOSITION_P (decl) && !DECL_DECOMP_IS_BASE (decl))
6053 TREE_USED (DECL_DECOMP_BASE (decl)) = true;
6055 if (TREE_CODE (decl) == TEMPLATE_DECL)
6056 return true;
6058 if (DECL_CLONED_FUNCTION_P (decl))
6059 TREE_USED (DECL_CLONED_FUNCTION (decl)) = 1;
6061 /* Mark enumeration types as used. */
6062 if (TREE_CODE (decl) == CONST_DECL)
6063 used_types_insert (DECL_CONTEXT (decl));
6065 if (TREE_CODE (decl) == FUNCTION_DECL)
6067 if (DECL_MAYBE_DELETED (decl))
6069 ++function_depth;
6070 maybe_synthesize_method (decl);
6071 --function_depth;
6074 if (DECL_DELETED_FN (decl))
6076 if (DECL_ARTIFICIAL (decl)
6077 && DECL_CONV_FN_P (decl)
6078 && LAMBDA_TYPE_P (DECL_CONTEXT (decl)))
6079 /* We mark a lambda conversion op as deleted if we can't
6080 generate it properly; see maybe_add_lambda_conv_op. */
6081 sorry ("converting lambda that uses %<...%> to function pointer");
6082 else if (complain & tf_error)
6084 auto_diagnostic_group d;
6085 if (DECL_INITIAL (decl)
6086 && TREE_CODE (DECL_INITIAL (decl)) == STRING_CST)
6088 escaped_string msg;
6089 msg.escape (TREE_STRING_POINTER (DECL_INITIAL (decl)));
6090 error ("use of deleted function %qD: %s",
6091 decl, (const char *) msg);
6093 else
6094 error ("use of deleted function %qD", decl);
6095 if (!maybe_explain_implicit_delete (decl))
6096 inform (DECL_SOURCE_LOCATION (decl), "declared here");
6098 return false;
6101 if (!maybe_instantiate_noexcept (decl, complain))
6102 return false;
6105 if (VAR_OR_FUNCTION_DECL_P (decl) && DECL_LOCAL_DECL_P (decl))
6107 if (!DECL_LANG_SPECIFIC (decl))
6108 /* An unresolved dependent local extern. */
6109 return true;
6111 DECL_ODR_USED (decl) = 1;
6112 auto alias = DECL_LOCAL_DECL_ALIAS (decl);
6113 if (!alias || alias == error_mark_node)
6114 return true;
6116 /* Process the underlying decl. */
6117 decl = alias;
6118 TREE_USED (decl) = true;
6121 cp_handle_deprecated_or_unavailable (decl, complain);
6123 /* We can only check DECL_ODR_USED on variables or functions with
6124 DECL_LANG_SPECIFIC set, and these are also the only decls that we
6125 might need special handling for. */
6126 if (!VAR_OR_FUNCTION_DECL_P (decl)
6127 || DECL_THUNK_P (decl))
6128 return true;
6130 /* We only want to do this processing once. We don't need to keep trying
6131 to instantiate inline templates, because unit-at-a-time will make sure
6132 we get them compiled before functions that want to inline them. */
6133 if (DECL_LANG_SPECIFIC (decl) && DECL_ODR_USED (decl))
6134 return true;
6136 if (flag_concepts && TREE_CODE (decl) == FUNCTION_DECL
6137 && !constraints_satisfied_p (decl))
6139 if (complain & tf_error)
6141 auto_diagnostic_group d;
6142 error ("use of function %qD with unsatisfied constraints",
6143 decl);
6144 location_t loc = DECL_SOURCE_LOCATION (decl);
6145 inform (loc, "declared here");
6146 diagnose_constraints (loc, decl, NULL_TREE);
6148 return false;
6151 /* If DECL has a deduced return type, we need to instantiate it now to
6152 find out its type. For OpenMP user defined reductions, we need them
6153 instantiated for reduction clauses which inline them by hand directly. */
6154 if (undeduced_auto_decl (decl)
6155 || (VAR_P (decl)
6156 && VAR_HAD_UNKNOWN_BOUND (decl))
6157 || (TREE_CODE (decl) == FUNCTION_DECL
6158 && DECL_OMP_DECLARE_REDUCTION_P (decl)))
6159 maybe_instantiate_decl (decl);
6161 if (!decl_dependent_p (decl)
6162 && !require_deduced_type (decl, complain))
6163 return false;
6165 if (DECL_LANG_SPECIFIC (decl) == NULL)
6166 return true;
6168 if (processing_template_decl || in_template_context)
6169 return true;
6171 /* Check this too in case we're within instantiate_non_dependent_expr. */
6172 if (DECL_TEMPLATE_INFO (decl)
6173 && uses_template_parms (DECL_TI_ARGS (decl)))
6174 return true;
6176 if (!require_deduced_type (decl, complain))
6177 return false;
6179 if (builtin_pack_fn_p (decl))
6181 error ("use of built-in parameter pack %qD outside of a template",
6182 DECL_NAME (decl));
6183 return false;
6186 /* If we don't need a value, then we don't need to synthesize DECL. */
6187 if (cp_unevaluated_operand || in_discarded_stmt)
6188 return true;
6190 DECL_ODR_USED (decl) = 1;
6191 if (DECL_CLONED_FUNCTION_P (decl))
6192 DECL_ODR_USED (DECL_CLONED_FUNCTION (decl)) = 1;
6194 /* DR 757: A type without linkage shall not be used as the type of a
6195 variable or function with linkage, unless
6196 o the variable or function has extern "C" linkage (7.5 [dcl.link]), or
6197 o the variable or function is not used (3.2 [basic.def.odr]) or is
6198 defined in the same translation unit. */
6199 if (cxx_dialect > cxx98
6200 && decl_linkage (decl) != lk_none
6201 && !DECL_EXTERN_C_P (decl)
6202 && !DECL_ARTIFICIAL (decl)
6203 && !decl_defined_p (decl)
6204 && no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/false))
6205 vec_safe_push (no_linkage_decls, decl);
6207 if (TREE_CODE (decl) == FUNCTION_DECL
6208 && DECL_DECLARED_INLINE_P (decl)
6209 && !DECL_INITIAL (decl)
6210 && !DECL_ARTIFICIAL (decl)
6211 && !DECL_PURE_VIRTUAL_P (decl))
6212 /* Remember it, so we can check it was defined. */
6213 note_vague_linkage_fn (decl);
6215 /* Is it a synthesized method that needs to be synthesized? */
6216 if (TREE_CODE (decl) == FUNCTION_DECL
6217 && DECL_DEFAULTED_FN (decl)
6218 /* A function defaulted outside the class is synthesized either by
6219 cp_finish_decl or instantiate_decl. */
6220 && !DECL_DEFAULTED_OUTSIDE_CLASS_P (decl)
6221 && ! DECL_INITIAL (decl))
6223 /* Remember the current location for a function we will end up
6224 synthesizing. Then we can inform the user where it was
6225 required in the case of error. */
6226 if (decl_remember_implicit_trigger_p (decl))
6227 DECL_SOURCE_LOCATION (decl) = input_location;
6229 /* Synthesizing an implicitly defined member function will result in
6230 garbage collection. We must treat this situation as if we were
6231 within the body of a function so as to avoid collecting live data
6232 on the stack (such as overload resolution candidates).
6234 We could just let c_parse_final_cleanups handle synthesizing
6235 this function by adding it to deferred_fns, but doing
6236 it at the use site produces better error messages. */
6237 ++function_depth;
6238 synthesize_method (decl);
6239 --function_depth;
6240 /* If this is a synthesized method we don't need to
6241 do the instantiation test below. */
6243 else if (VAR_OR_FUNCTION_DECL_P (decl)
6244 && DECL_TEMPLATE_INFO (decl)
6245 && (!DECL_EXPLICIT_INSTANTIATION (decl)
6246 || always_instantiate_p (decl)))
6247 /* If this is a function or variable that is an instance of some
6248 template, we now know that we will need to actually do the
6249 instantiation. We check that DECL is not an explicit
6250 instantiation because that is not checked in instantiate_decl.
6252 We put off instantiating functions in order to improve compile
6253 times. Maintaining a stack of active functions is expensive,
6254 and the inliner knows to instantiate any functions it might
6255 need. Therefore, we always try to defer instantiation. */
6257 ++function_depth;
6258 instantiate_decl (decl, /*defer_ok=*/true,
6259 /*expl_inst_class_mem_p=*/false);
6260 --function_depth;
6263 return true;
6266 tree
6267 vtv_start_verification_constructor_init_function (void)
6269 return start_objects (/*initp=*/true, MAX_RESERVED_INIT_PRIORITY - 1, true);
6272 tree
6273 vtv_finish_verification_constructor_init_function (tree body)
6275 return finish_objects (/*initp=*/true, MAX_RESERVED_INIT_PRIORITY - 1, body);
6278 #include "gt-cp-decl2.h"