Require target lra in gcc.c-torture/compile/asmgoto-6.c
[official-gcc.git] / gcc / cp / decl2.cc
blobb402befba6da41619b2106c38a0fed2accbf5ce6
1 /* Process declarations and variables for C++ compiler.
2 Copyright (C) 1988-2023 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"
54 /* Id for dumping the raw trees. */
55 int raw_dump_id;
57 extern cpp_reader *parse_in;
59 static tree start_objects (bool, unsigned, bool);
60 static tree finish_objects (bool, unsigned, tree, bool = true);
61 static tree start_partial_init_fini_fn (bool, unsigned, unsigned);
62 static void finish_partial_init_fini_fn (tree);
63 static void emit_partial_init_fini_fn (bool, unsigned, tree,
64 unsigned, location_t);
65 static void one_static_initialization_or_destruction (bool, tree, tree);
66 static void generate_ctor_or_dtor_function (bool, unsigned, tree, location_t);
67 static tree prune_vars_needing_no_initialization (tree *);
68 static void write_out_vars (tree);
69 static void import_export_class (tree);
70 static tree get_guard_bits (tree);
71 static void determine_visibility_from_class (tree, tree);
72 static bool determine_hidden_inline (tree);
74 /* A list of static class variables. This is needed, because a
75 static class variable can be declared inside the class without
76 an initializer, and then initialized, statically, outside the class. */
77 static GTY(()) vec<tree, va_gc> *pending_statics;
79 /* A list of functions which were declared inline, but which we
80 may need to emit outline anyway. */
81 static GTY(()) vec<tree, va_gc> *deferred_fns;
83 /* A list of decls that use types with no linkage, which we need to make
84 sure are defined. */
85 static GTY(()) vec<tree, va_gc> *no_linkage_decls;
87 /* A vector of alternating decls and identifiers, where the latter
88 is to be an alias for the former if the former is defined. */
89 static GTY(()) vec<tree, va_gc> *mangling_aliases;
91 /* hash traits for declarations. Hashes single decls via
92 DECL_ASSEMBLER_NAME_RAW. */
94 struct mangled_decl_hash : ggc_remove <tree>
96 typedef tree value_type; /* A DECL. */
97 typedef tree compare_type; /* An identifier. */
99 static hashval_t hash (const value_type decl)
101 return IDENTIFIER_HASH_VALUE (DECL_ASSEMBLER_NAME_RAW (decl));
103 static bool equal (const value_type existing, compare_type candidate)
105 tree name = DECL_ASSEMBLER_NAME_RAW (existing);
106 return candidate == name;
109 static const bool empty_zero_p = true;
110 static inline void mark_empty (value_type &p) {p = NULL_TREE;}
111 static inline bool is_empty (value_type p) {return !p;}
113 static bool is_deleted (value_type e)
115 return e == reinterpret_cast <value_type> (1);
117 static void mark_deleted (value_type &e)
119 e = reinterpret_cast <value_type> (1);
123 /* A hash table of decls keyed by mangled name. Used to figure out if
124 we need compatibility aliases. */
125 static GTY(()) hash_table<mangled_decl_hash> *mangled_decls;
127 // Hash table mapping priority to lists of variables or functions.
128 struct priority_map_traits
130 typedef unsigned key_type;
131 typedef tree value_type;
132 static const bool maybe_mx = true;
133 static hashval_t hash (key_type v)
135 return hashval_t (v);
137 static bool equal_keys (key_type k1, key_type k2)
139 return k1 == k2;
141 template <typename T> static void remove (T &)
144 // Zero is not a priority
145 static const bool empty_zero_p = true;
146 template <typename T> static bool is_empty (const T &entry)
148 return entry.m_key == 0;
150 template <typename T> static void mark_empty (T &entry)
152 entry.m_key = 0;
154 // Entries are not deleteable
155 template <typename T> static bool is_deleted (const T &)
157 return false;
159 template <typename T> static void mark_deleted (T &)
161 gcc_unreachable ();
165 typedef hash_map<unsigned/*Priority*/, tree/*List*/,
166 priority_map_traits> priority_map_t;
168 /* A pair of such hash tables, indexed by initp -- one for fini and
169 one for init. The fini table is only ever used when !cxa_atexit. */
170 static GTY(()) priority_map_t *static_init_fini_fns[2];
172 /* Nonzero if we're done parsing and into end-of-file activities. */
174 int at_eof;
176 /* True if note_mangling_alias should enqueue mangling aliases for
177 later generation, rather than emitting them right away. */
179 bool defer_mangling_aliases = true;
182 /* Return a member function type (a METHOD_TYPE), given FNTYPE (a
183 FUNCTION_TYPE), CTYPE (class type), and QUALS (the cv-qualifiers
184 that apply to the function). */
186 tree
187 build_memfn_type (tree fntype, tree ctype, cp_cv_quals quals,
188 cp_ref_qualifier rqual)
190 if (fntype == error_mark_node || ctype == error_mark_node)
191 return error_mark_node;
193 gcc_assert (FUNC_OR_METHOD_TYPE_P (fntype));
195 cp_cv_quals type_quals = quals & ~TYPE_QUAL_RESTRICT;
196 ctype = cp_build_qualified_type (ctype, type_quals);
198 tree newtype
199 = build_method_type_directly (ctype, TREE_TYPE (fntype),
200 (TREE_CODE (fntype) == METHOD_TYPE
201 ? TREE_CHAIN (TYPE_ARG_TYPES (fntype))
202 : TYPE_ARG_TYPES (fntype)));
203 if (tree attrs = TYPE_ATTRIBUTES (fntype))
204 newtype = cp_build_type_attribute_variant (newtype, attrs);
205 newtype = build_cp_fntype_variant (newtype, rqual,
206 TYPE_RAISES_EXCEPTIONS (fntype),
207 TYPE_HAS_LATE_RETURN_TYPE (fntype));
209 return newtype;
212 /* Return a variant of FNTYPE, a FUNCTION_TYPE or METHOD_TYPE, with its
213 return type changed to NEW_RET. */
215 tree
216 change_return_type (tree new_ret, tree fntype)
218 if (new_ret == error_mark_node)
219 return fntype;
221 if (same_type_p (new_ret, TREE_TYPE (fntype)))
222 return fntype;
224 tree newtype;
225 tree args = TYPE_ARG_TYPES (fntype);
227 if (TREE_CODE (fntype) == FUNCTION_TYPE)
229 newtype = build_function_type (new_ret, args);
230 newtype = apply_memfn_quals (newtype,
231 type_memfn_quals (fntype));
233 else
234 newtype = build_method_type_directly
235 (class_of_this_parm (fntype), new_ret, TREE_CHAIN (args));
237 if (tree attrs = TYPE_ATTRIBUTES (fntype))
238 newtype = cp_build_type_attribute_variant (newtype, attrs);
239 newtype = cxx_copy_lang_qualifiers (newtype, fntype);
241 return newtype;
244 /* Build a PARM_DECL of FN with NAME and TYPE, and set DECL_ARG_TYPE
245 appropriately. */
247 tree
248 cp_build_parm_decl (tree fn, tree name, tree type)
250 tree parm = build_decl (input_location,
251 PARM_DECL, name, type);
252 DECL_CONTEXT (parm) = fn;
254 /* DECL_ARG_TYPE is only used by the back end and the back end never
255 sees templates. */
256 if (!processing_template_decl)
257 DECL_ARG_TYPE (parm) = type_passed_as (type);
259 return parm;
262 /* Returns a PARM_DECL of FN for a parameter of the indicated TYPE, with the
263 indicated NAME. */
265 tree
266 build_artificial_parm (tree fn, tree name, tree type)
268 tree parm = cp_build_parm_decl (fn, name, type);
269 DECL_ARTIFICIAL (parm) = 1;
270 /* All our artificial parms are implicitly `const'; they cannot be
271 assigned to. */
272 TREE_READONLY (parm) = 1;
273 return parm;
276 /* Constructors for types with virtual baseclasses need an "in-charge" flag
277 saying whether this constructor is responsible for initialization of
278 virtual baseclasses or not. All destructors also need this "in-charge"
279 flag, which additionally determines whether or not the destructor should
280 free the memory for the object.
282 This function adds the "in-charge" flag to member function FN if
283 appropriate. It is called from grokclassfn and tsubst.
284 FN must be either a constructor or destructor.
286 The in-charge flag follows the 'this' parameter, and is followed by the
287 VTT parm (if any), then the user-written parms. */
289 void
290 maybe_retrofit_in_chrg (tree fn)
292 tree basetype, arg_types, parms, parm, fntype;
294 /* If we've already add the in-charge parameter don't do it again. */
295 if (DECL_HAS_IN_CHARGE_PARM_P (fn))
296 return;
298 /* When processing templates we can't know, in general, whether or
299 not we're going to have virtual baseclasses. */
300 if (processing_template_decl)
301 return;
303 /* We don't need an in-charge parameter for constructors that don't
304 have virtual bases. */
305 if (DECL_CONSTRUCTOR_P (fn)
306 && !CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fn)))
307 return;
309 arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
310 basetype = TREE_TYPE (TREE_VALUE (arg_types));
311 arg_types = TREE_CHAIN (arg_types);
313 parms = DECL_CHAIN (DECL_ARGUMENTS (fn));
315 /* If this is a subobject constructor or destructor, our caller will
316 pass us a pointer to our VTT. */
317 if (CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fn)))
319 parm = build_artificial_parm (fn, vtt_parm_identifier, vtt_parm_type);
321 /* First add it to DECL_ARGUMENTS between 'this' and the real args... */
322 DECL_CHAIN (parm) = parms;
323 parms = parm;
325 /* ...and then to TYPE_ARG_TYPES. */
326 arg_types = hash_tree_chain (vtt_parm_type, arg_types);
328 DECL_HAS_VTT_PARM_P (fn) = 1;
331 /* Then add the in-charge parm (before the VTT parm). */
332 parm = build_artificial_parm (fn, in_charge_identifier, integer_type_node);
333 DECL_CHAIN (parm) = parms;
334 parms = parm;
335 arg_types = hash_tree_chain (integer_type_node, arg_types);
337 /* Insert our new parameter(s) into the list. */
338 DECL_CHAIN (DECL_ARGUMENTS (fn)) = parms;
340 /* And rebuild the function type. */
341 fntype = build_method_type_directly (basetype, TREE_TYPE (TREE_TYPE (fn)),
342 arg_types);
343 if (TYPE_ATTRIBUTES (TREE_TYPE (fn)))
344 fntype = (cp_build_type_attribute_variant
345 (fntype, TYPE_ATTRIBUTES (TREE_TYPE (fn))));
346 fntype = cxx_copy_lang_qualifiers (fntype, TREE_TYPE (fn));
347 TREE_TYPE (fn) = fntype;
349 /* Now we've got the in-charge parameter. */
350 DECL_HAS_IN_CHARGE_PARM_P (fn) = 1;
353 /* Classes overload their constituent function names automatically.
354 When a function name is declared in a record structure,
355 its name is changed to it overloaded name. Since names for
356 constructors and destructors can conflict, we place a leading
357 '$' for destructors.
359 CNAME is the name of the class we are grokking for.
361 FUNCTION is a FUNCTION_DECL. It was created by `grokdeclarator'.
363 FLAGS contains bits saying what's special about today's
364 arguments. DTOR_FLAG == DESTRUCTOR.
366 If FUNCTION is a destructor, then we must add the `auto-delete' field
367 as a second parameter. There is some hair associated with the fact
368 that we must "declare" this variable in the manner consistent with the
369 way the rest of the arguments were declared.
371 QUALS are the qualifiers for the this pointer. */
373 void
374 grokclassfn (tree ctype, tree function, enum overload_flags flags)
376 tree fn_name = DECL_NAME (function);
378 /* Even within an `extern "C"' block, members get C++ linkage. See
379 [dcl.link] for details. */
380 SET_DECL_LANGUAGE (function, lang_cplusplus);
382 if (fn_name == NULL_TREE)
384 error ("name missing for member function");
385 fn_name = get_identifier ("<anonymous>");
386 DECL_NAME (function) = fn_name;
389 DECL_CONTEXT (function) = ctype;
391 if (flags == DTOR_FLAG)
392 DECL_CXX_DESTRUCTOR_P (function) = 1;
394 if (flags == DTOR_FLAG || DECL_CONSTRUCTOR_P (function))
395 maybe_retrofit_in_chrg (function);
398 /* Create an ARRAY_REF, checking for the user doing things backwards
399 along the way.
400 If INDEX_EXP is non-NULL, then that is the index expression,
401 otherwise INDEX_EXP_LIST is the list of index expressions. */
403 tree
404 grok_array_decl (location_t loc, tree array_expr, tree index_exp,
405 vec<tree, va_gc> **index_exp_list, tsubst_flags_t complain)
407 tree type;
408 tree expr;
409 tree orig_array_expr = array_expr;
410 tree orig_index_exp = index_exp;
411 vec<tree, va_gc> *orig_index_exp_list
412 = index_exp_list ? *index_exp_list : NULL;
413 tree overload = NULL_TREE;
415 if (error_operand_p (array_expr) || error_operand_p (index_exp))
416 return error_mark_node;
418 if (processing_template_decl)
420 if (type_dependent_expression_p (array_expr)
421 || (index_exp ? type_dependent_expression_p (index_exp)
422 : any_type_dependent_arguments_p (*index_exp_list)))
424 if (index_exp == NULL)
425 index_exp = build_min_nt_call_vec (ovl_op_identifier (ARRAY_REF),
426 *index_exp_list);
427 return build_min_nt_loc (loc, ARRAY_REF, array_expr, index_exp,
428 NULL_TREE, NULL_TREE);
430 array_expr = build_non_dependent_expr (array_expr);
431 if (index_exp)
432 index_exp = build_non_dependent_expr (index_exp);
433 else
435 orig_index_exp_list = make_tree_vector_copy (*index_exp_list);
436 make_args_non_dependent (*index_exp_list);
440 type = TREE_TYPE (array_expr);
441 gcc_assert (type);
442 type = non_reference (type);
444 /* If they have an `operator[]', use that. */
445 if (MAYBE_CLASS_TYPE_P (type)
446 || (index_exp && MAYBE_CLASS_TYPE_P (TREE_TYPE (index_exp)))
447 || (index_exp == NULL_TREE
448 && !(*index_exp_list)->is_empty ()
449 && MAYBE_CLASS_TYPE_P (TREE_TYPE ((*index_exp_list)->last ()))))
451 if (index_exp)
452 expr = build_new_op (loc, ARRAY_REF, LOOKUP_NORMAL, array_expr,
453 index_exp, NULL_TREE, NULL_TREE,
454 &overload, complain);
455 else if ((*index_exp_list)->is_empty ())
456 expr = build_op_subscript (loc, array_expr, index_exp_list, &overload,
457 complain);
458 else
460 expr = build_op_subscript (loc, array_expr, index_exp_list,
461 &overload, complain & tf_decltype);
462 if (expr == error_mark_node)
464 tree idx = build_x_compound_expr_from_vec (*index_exp_list, NULL,
465 tf_none);
466 if (idx != error_mark_node)
467 expr = build_new_op (loc, ARRAY_REF, LOOKUP_NORMAL, array_expr,
468 idx, NULL_TREE, NULL_TREE, &overload,
469 complain & tf_decltype);
470 if (expr == error_mark_node)
472 overload = NULL_TREE;
473 expr = build_op_subscript (loc, array_expr, index_exp_list,
474 &overload, complain);
476 else
478 /* If it would be valid albeit deprecated expression in
479 C++20, just pedwarn on it and treat it as if wrapped
480 in (). */
481 pedwarn (loc, OPT_Wcomma_subscript,
482 "top-level comma expression in array subscript "
483 "changed meaning in C++23");
484 if (processing_template_decl)
486 orig_index_exp
487 = build_x_compound_expr_from_vec (orig_index_exp_list,
488 NULL, complain);
489 if (orig_index_exp == error_mark_node)
490 expr = error_mark_node;
491 release_tree_vector (orig_index_exp_list);
497 else
499 tree p1, p2, i1, i2;
500 bool swapped = false;
502 /* Otherwise, create an ARRAY_REF for a pointer or array type.
503 It is a little-known fact that, if `a' is an array and `i' is
504 an int, you can write `i[a]', which means the same thing as
505 `a[i]'. */
506 if (TREE_CODE (type) == ARRAY_TYPE || VECTOR_TYPE_P (type))
507 p1 = array_expr;
508 else
509 p1 = build_expr_type_conversion (WANT_POINTER, array_expr, false);
511 if (index_exp == NULL_TREE)
513 if ((*index_exp_list)->is_empty ())
515 error_at (loc, "built-in subscript operator without expression "
516 "list");
517 return error_mark_node;
519 tree idx = build_x_compound_expr_from_vec (*index_exp_list, NULL,
520 tf_none);
521 if (idx != error_mark_node)
522 /* If it would be valid albeit deprecated expression in C++20,
523 just pedwarn on it and treat it as if wrapped in (). */
524 pedwarn (loc, OPT_Wcomma_subscript,
525 "top-level comma expression in array subscript "
526 "changed meaning in C++23");
527 else
529 error_at (loc, "built-in subscript operator with more than one "
530 "expression in expression list");
531 return error_mark_node;
533 index_exp = idx;
534 if (processing_template_decl)
536 orig_index_exp
537 = build_x_compound_expr_from_vec (orig_index_exp_list,
538 NULL, complain);
539 release_tree_vector (orig_index_exp_list);
540 if (orig_index_exp == error_mark_node)
541 return error_mark_node;
545 if (TREE_CODE (TREE_TYPE (index_exp)) == ARRAY_TYPE)
546 p2 = index_exp;
547 else
548 p2 = build_expr_type_conversion (WANT_POINTER, index_exp, false);
550 i1 = build_expr_type_conversion (WANT_INT | WANT_ENUM, array_expr,
551 false);
552 i2 = build_expr_type_conversion (WANT_INT | WANT_ENUM, index_exp,
553 false);
555 if ((p1 && i2) && (i1 && p2))
556 error ("ambiguous conversion for array subscript");
558 if (p1 && i2)
559 array_expr = p1, index_exp = i2;
560 else if (i1 && p2)
561 swapped = true, array_expr = p2, index_exp = i1;
562 else
564 error_at (loc, "invalid types %<%T[%T]%> for array subscript",
565 type, TREE_TYPE (index_exp));
566 return error_mark_node;
569 if (array_expr == error_mark_node || index_exp == error_mark_node)
570 error ("ambiguous conversion for array subscript");
572 if (TYPE_PTR_P (TREE_TYPE (array_expr)))
573 array_expr = mark_rvalue_use (array_expr);
574 else
575 array_expr = mark_lvalue_use_nonread (array_expr);
576 index_exp = mark_rvalue_use (index_exp);
577 if (swapped
578 && flag_strong_eval_order == 2
579 && (TREE_SIDE_EFFECTS (array_expr) || TREE_SIDE_EFFECTS (index_exp)))
580 expr = build_array_ref (input_location, index_exp, array_expr);
581 else
582 expr = build_array_ref (input_location, array_expr, index_exp);
584 if (processing_template_decl && expr != error_mark_node)
586 if (overload != NULL_TREE)
588 if (orig_index_exp == NULL_TREE)
590 expr = build_min_non_dep_op_overload (expr, overload,
591 orig_array_expr,
592 orig_index_exp_list);
593 release_tree_vector (orig_index_exp_list);
594 return expr;
596 return build_min_non_dep_op_overload (ARRAY_REF, expr, overload,
597 orig_array_expr,
598 orig_index_exp);
601 if (orig_index_exp == NULL_TREE)
603 orig_index_exp
604 = build_min_nt_call_vec (ovl_op_identifier (ARRAY_REF),
605 orig_index_exp_list);
606 release_tree_vector (orig_index_exp_list);
609 return build_min_non_dep (ARRAY_REF, expr, orig_array_expr,
610 orig_index_exp, NULL_TREE, NULL_TREE);
612 return expr;
615 /* Given the cast expression EXP, checking out its validity. Either return
616 an error_mark_node if there was an unavoidable error, return a cast to
617 void for trying to delete a pointer w/ the value 0, or return the
618 call to delete. If DOING_VEC is true, we handle things differently
619 for doing an array delete.
620 Implements ARM $5.3.4. This is called from the parser. */
622 tree
623 delete_sanity (location_t loc, tree exp, tree size, bool doing_vec,
624 int use_global_delete, tsubst_flags_t complain)
626 tree t, type;
628 if (exp == error_mark_node)
629 return exp;
631 if (processing_template_decl)
633 t = build_min (DELETE_EXPR, void_type_node, exp, size);
634 DELETE_EXPR_USE_GLOBAL (t) = use_global_delete;
635 DELETE_EXPR_USE_VEC (t) = doing_vec;
636 TREE_SIDE_EFFECTS (t) = 1;
637 SET_EXPR_LOCATION (t, loc);
638 return t;
641 location_t exp_loc = cp_expr_loc_or_loc (exp, loc);
643 /* An array can't have been allocated by new, so complain. */
644 if (TREE_CODE (TREE_TYPE (exp)) == ARRAY_TYPE
645 && (complain & tf_warning))
646 warning_at (exp_loc, 0, "deleting array %q#E", exp);
648 t = build_expr_type_conversion (WANT_POINTER, exp, true);
650 if (t == NULL_TREE || t == error_mark_node)
652 if (complain & tf_error)
653 error_at (exp_loc,
654 "type %q#T argument given to %<delete%>, expected pointer",
655 TREE_TYPE (exp));
656 return error_mark_node;
659 type = TREE_TYPE (t);
661 /* As of Valley Forge, you can delete a pointer to const. */
663 /* You can't delete functions. */
664 if (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
666 if (complain & tf_error)
667 error_at (exp_loc,
668 "cannot delete a function. Only pointer-to-objects are "
669 "valid arguments to %<delete%>");
670 return error_mark_node;
673 /* Deleting ptr to void is undefined behavior [expr.delete/3]. */
674 if (VOID_TYPE_P (TREE_TYPE (type)))
676 if (complain & tf_warning)
677 warning_at (exp_loc, OPT_Wdelete_incomplete,
678 "deleting %qT is undefined", type);
679 doing_vec = 0;
682 /* Deleting a pointer with the value zero is valid and has no effect. */
683 if (integer_zerop (t))
684 return build1_loc (loc, NOP_EXPR, void_type_node, t);
686 if (doing_vec)
687 return build_vec_delete (loc, t, /*maxindex=*/NULL_TREE,
688 sfk_deleting_destructor,
689 use_global_delete, complain);
690 else
691 return build_delete (loc, type, t, sfk_deleting_destructor,
692 LOOKUP_NORMAL, use_global_delete,
693 complain);
696 /* Report an error if the indicated template declaration is not the
697 sort of thing that should be a member template. */
699 void
700 check_member_template (tree tmpl)
702 tree decl;
704 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
705 decl = DECL_TEMPLATE_RESULT (tmpl);
707 if (TREE_CODE (decl) == FUNCTION_DECL
708 || DECL_ALIAS_TEMPLATE_P (tmpl)
709 || (TREE_CODE (decl) == TYPE_DECL
710 && MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))))
712 /* The parser rejects template declarations in local classes
713 (with the exception of generic lambdas). */
714 gcc_assert (!current_function_decl || LAMBDA_FUNCTION_P (decl));
715 /* The parser rejects any use of virtual in a function template. */
716 gcc_assert (!(TREE_CODE (decl) == FUNCTION_DECL
717 && DECL_VIRTUAL_P (decl)));
719 /* The debug-information generating code doesn't know what to do
720 with member templates. */
721 DECL_IGNORED_P (tmpl) = 1;
723 else if (variable_template_p (tmpl))
724 /* OK */;
725 else
726 error ("template declaration of %q#D", decl);
729 /* Sanity check: report error if this function FUNCTION is not
730 really a member of the class (CTYPE) it is supposed to belong to.
731 TEMPLATE_PARMS is used to specify the template parameters of a member
732 template passed as FUNCTION_DECL. If the member template is passed as a
733 TEMPLATE_DECL, it can be NULL since the parameters can be extracted
734 from the declaration. If the function is not a function template, it
735 must be NULL.
736 It returns the original declaration for the function, NULL_TREE if
737 no declaration was found, error_mark_node if an error was emitted. */
739 tree
740 check_classfn (tree ctype, tree function, tree template_parms)
742 if (DECL_USE_TEMPLATE (function)
743 && !(TREE_CODE (function) == TEMPLATE_DECL
744 && DECL_TEMPLATE_SPECIALIZATION (function))
745 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (function)))
746 /* Since this is a specialization of a member template,
747 we're not going to find the declaration in the class.
748 For example, in:
750 struct S { template <typename T> void f(T); };
751 template <> void S::f(int);
753 we're not going to find `S::f(int)', but there's no
754 reason we should, either. We let our callers know we didn't
755 find the method, but we don't complain. */
756 return NULL_TREE;
758 /* Basic sanity check: for a template function, the template parameters
759 either were not passed, or they are the same of DECL_TEMPLATE_PARMS. */
760 if (TREE_CODE (function) == TEMPLATE_DECL)
762 if (template_parms
763 && !comp_template_parms (template_parms,
764 DECL_TEMPLATE_PARMS (function)))
766 error ("template parameter lists provided don%'t match the "
767 "template parameters of %qD", function);
768 return error_mark_node;
770 template_parms = DECL_TEMPLATE_PARMS (function);
773 /* OK, is this a definition of a member template? */
774 bool is_template = (template_parms != NULL_TREE);
776 /* [temp.mem]
778 A destructor shall not be a member template. */
779 if (DECL_DESTRUCTOR_P (function) && is_template)
781 error ("destructor %qD declared as member template", function);
782 return error_mark_node;
785 /* We must enter the scope here, because conversion operators are
786 named by target type, and type equivalence relies on typenames
787 resolving within the scope of CTYPE. */
788 tree pushed_scope = push_scope (ctype);
789 tree matched = NULL_TREE;
790 tree fns = get_class_binding (ctype, DECL_NAME (function));
791 bool saw_template = false;
793 for (ovl_iterator iter (fns); !matched && iter; ++iter)
795 tree fndecl = *iter;
797 if (TREE_CODE (fndecl) == TEMPLATE_DECL)
798 saw_template = true;
800 /* A member template definition only matches a member template
801 declaration. */
802 if (is_template != (TREE_CODE (fndecl) == TEMPLATE_DECL))
803 continue;
805 if (!DECL_DECLARES_FUNCTION_P (fndecl))
806 continue;
808 tree p1 = TYPE_ARG_TYPES (TREE_TYPE (function));
809 tree p2 = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
811 /* We cannot simply call decls_match because this doesn't work
812 for static member functions that are pretending to be
813 methods, and because the name may have been changed by
814 asm("new_name"). */
816 /* Get rid of the this parameter on functions that become
817 static. */
818 if (DECL_STATIC_FUNCTION_P (fndecl)
819 && TREE_CODE (TREE_TYPE (function)) == METHOD_TYPE)
820 p1 = TREE_CHAIN (p1);
822 /* ref-qualifier or absence of same must match. */
823 if (type_memfn_rqual (TREE_TYPE (function))
824 != type_memfn_rqual (TREE_TYPE (fndecl)))
825 continue;
827 // Include constraints in the match.
828 tree c1 = get_constraints (function);
829 tree c2 = get_constraints (fndecl);
831 /* While finding a match, same types and params are not enough
832 if the function is versioned. Also check version ("target")
833 attributes. */
834 if (same_type_p (TREE_TYPE (TREE_TYPE (function)),
835 TREE_TYPE (TREE_TYPE (fndecl)))
836 && compparms (p1, p2)
837 && !targetm.target_option.function_versions (function, fndecl)
838 && (!is_template
839 || comp_template_parms (template_parms,
840 DECL_TEMPLATE_PARMS (fndecl)))
841 && equivalent_constraints (c1, c2)
842 && (DECL_TEMPLATE_SPECIALIZATION (function)
843 == DECL_TEMPLATE_SPECIALIZATION (fndecl))
844 && (!DECL_TEMPLATE_SPECIALIZATION (function)
845 || (DECL_TI_TEMPLATE (function) == DECL_TI_TEMPLATE (fndecl))))
846 matched = fndecl;
849 if (!matched && !is_template && saw_template
850 && !processing_template_decl && DECL_UNIQUE_FRIEND_P (function))
852 /* "[if no non-template match is found,] each remaining function template
853 is replaced with the specialization chosen by deduction from the
854 friend declaration or discarded if deduction fails."
856 So tell check_explicit_specialization to look for a match. */
857 SET_DECL_IMPLICIT_INSTANTIATION (function);
858 DECL_TEMPLATE_INFO (function) = build_template_info (fns, NULL_TREE);
859 matched = function;
862 if (!matched)
864 if (!COMPLETE_TYPE_P (ctype))
865 cxx_incomplete_type_error (DECL_SOURCE_LOCATION (function),
866 function, ctype);
867 else
869 if (DECL_CONV_FN_P (function))
870 fns = get_class_binding (ctype, conv_op_identifier);
872 error_at (DECL_SOURCE_LOCATION (function),
873 "no declaration matches %q#D", function);
874 if (fns)
875 print_candidates (fns);
876 else if (DECL_CONV_FN_P (function))
877 inform (DECL_SOURCE_LOCATION (function),
878 "no conversion operators declared");
879 else
880 inform (DECL_SOURCE_LOCATION (function),
881 "no functions named %qD", function);
882 inform (DECL_SOURCE_LOCATION (TYPE_NAME (ctype)),
883 "%#qT defined here", ctype);
885 matched = error_mark_node;
888 if (pushed_scope)
889 pop_scope (pushed_scope);
891 return matched;
894 /* DECL is a function with vague linkage. Remember it so that at the
895 end of the translation unit we can decide whether or not to emit
896 it. */
898 void
899 note_vague_linkage_fn (tree decl)
901 if (processing_template_decl)
902 return;
904 DECL_DEFER_OUTPUT (decl) = 1;
905 vec_safe_push (deferred_fns, decl);
908 /* As above, but for variable template instantiations. */
910 void
911 note_variable_template_instantiation (tree decl)
913 vec_safe_push (pending_statics, decl);
916 /* We have just processed the DECL, which is a static data member.
917 The other parameters are as for cp_finish_decl. */
919 void
920 finish_static_data_member_decl (tree decl,
921 tree init, bool init_const_expr_p,
922 tree asmspec_tree,
923 int flags)
925 if (DECL_TEMPLATE_INSTANTIATED (decl))
926 /* We already needed to instantiate this, so the processing in this
927 function is unnecessary/wrong. */
928 return;
930 DECL_CONTEXT (decl) = current_class_type;
932 /* We cannot call pushdecl here, because that would fill in the
933 TREE_CHAIN of our decl. Instead, we modify cp_finish_decl to do
934 the right thing, namely, to put this decl out straight away. */
936 if (! processing_template_decl)
937 vec_safe_push (pending_statics, decl);
939 if (LOCAL_CLASS_P (current_class_type)
940 /* We already complained about the template definition. */
941 && !DECL_TEMPLATE_INSTANTIATION (decl))
942 permerror (DECL_SOURCE_LOCATION (decl),
943 "local class %q#T shall not have static data member %q#D",
944 current_class_type, decl);
945 else
946 for (tree t = current_class_type; TYPE_P (t);
947 t = CP_TYPE_CONTEXT (t))
948 if (TYPE_UNNAMED_P (t))
950 auto_diagnostic_group d;
951 if (permerror (DECL_SOURCE_LOCATION (decl),
952 "static data member %qD in unnamed class", decl))
953 inform (DECL_SOURCE_LOCATION (TYPE_NAME (t)),
954 "unnamed class defined here");
955 break;
958 if (DECL_INLINE_VAR_P (decl) && !DECL_TEMPLATE_INSTANTIATION (decl))
959 /* An inline variable is immediately defined, so don't set DECL_IN_AGGR_P.
960 Except that if decl is a template instantiation, it isn't defined until
961 instantiate_decl. */;
962 else
963 DECL_IN_AGGR_P (decl) = 1;
965 if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
966 && TYPE_DOMAIN (TREE_TYPE (decl)) == NULL_TREE)
967 SET_VAR_HAD_UNKNOWN_BOUND (decl);
969 if (init)
971 /* Similarly to start_decl_1, we want to complete the type in order
972 to do the right thing in cp_apply_type_quals_to_decl, possibly
973 clear TYPE_QUAL_CONST (c++/65579). */
974 tree type = TREE_TYPE (decl) = complete_type (TREE_TYPE (decl));
975 cp_apply_type_quals_to_decl (cp_type_quals (type), decl);
978 cp_finish_decl (decl, init, init_const_expr_p, asmspec_tree, flags);
981 /* DECLARATOR and DECLSPECS correspond to a class member. The other
982 parameters are as for cp_finish_decl. Return the DECL for the
983 class member declared. */
985 tree
986 grokfield (const cp_declarator *declarator,
987 cp_decl_specifier_seq *declspecs,
988 tree init, bool init_const_expr_p,
989 tree asmspec_tree,
990 tree attrlist)
992 tree value;
993 const char *asmspec = 0;
994 int flags;
996 if (init
997 && TREE_CODE (init) == TREE_LIST
998 && TREE_VALUE (init) == error_mark_node
999 && TREE_CHAIN (init) == NULL_TREE)
1000 init = NULL_TREE;
1002 int initialized;
1003 if (init == ridpointers[(int)RID_DELETE])
1004 initialized = SD_DELETED;
1005 else if (init == ridpointers[(int)RID_DEFAULT])
1006 initialized = SD_DEFAULTED;
1007 else if (init)
1008 initialized = SD_INITIALIZED;
1009 else
1010 initialized = SD_UNINITIALIZED;
1012 value = grokdeclarator (declarator, declspecs, FIELD, initialized, &attrlist);
1013 if (! value || value == error_mark_node)
1014 /* friend or constructor went bad. */
1015 return error_mark_node;
1016 if (TREE_TYPE (value) == error_mark_node)
1017 return value;
1019 if (TREE_CODE (value) == TYPE_DECL && init)
1021 error_at (cp_expr_loc_or_loc (init, DECL_SOURCE_LOCATION (value)),
1022 "typedef %qD is initialized (use %qs instead)",
1023 value, "decltype");
1024 init = NULL_TREE;
1027 /* Pass friendly classes back. */
1028 if (value == void_type_node)
1029 return value;
1031 if (DECL_NAME (value)
1032 && TREE_CODE (DECL_NAME (value)) == TEMPLATE_ID_EXPR)
1034 error_at (declarator->id_loc,
1035 "explicit template argument list not allowed");
1036 return error_mark_node;
1039 /* Stash away type declarations. */
1040 if (TREE_CODE (value) == TYPE_DECL)
1042 DECL_NONLOCAL (value) = 1;
1043 DECL_CONTEXT (value) = current_class_type;
1045 if (attrlist)
1047 int attrflags = 0;
1049 /* If this is a typedef that names the class for linkage purposes
1050 (7.1.3p8), apply any attributes directly to the type. */
1051 if (OVERLOAD_TYPE_P (TREE_TYPE (value))
1052 && value == TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (value))))
1053 attrflags = ATTR_FLAG_TYPE_IN_PLACE;
1055 cplus_decl_attributes (&value, attrlist, attrflags);
1058 if (decl_spec_seq_has_spec_p (declspecs, ds_typedef)
1059 && TREE_TYPE (value) != error_mark_node
1060 && TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (value))) != value)
1061 set_underlying_type (value);
1063 /* It's important that push_template_decl below follows
1064 set_underlying_type above so that the created template
1065 carries the properly set type of VALUE. */
1066 if (processing_template_decl)
1067 value = push_template_decl (value);
1069 record_locally_defined_typedef (value);
1070 return value;
1073 int friendp = decl_spec_seq_has_spec_p (declspecs, ds_friend);
1075 if (!friendp && DECL_IN_AGGR_P (value))
1077 error ("%qD is already defined in %qT", value, DECL_CONTEXT (value));
1078 return void_type_node;
1081 if (asmspec_tree && asmspec_tree != error_mark_node)
1082 asmspec = TREE_STRING_POINTER (asmspec_tree);
1084 if (init)
1086 if (TREE_CODE (value) == FUNCTION_DECL)
1088 if (init == ridpointers[(int)RID_DELETE])
1090 DECL_DELETED_FN (value) = 1;
1091 DECL_DECLARED_INLINE_P (value) = 1;
1093 else if (init == ridpointers[(int)RID_DEFAULT])
1095 if (defaultable_fn_check (value))
1097 DECL_DEFAULTED_FN (value) = 1;
1098 DECL_INITIALIZED_IN_CLASS_P (value) = 1;
1099 DECL_DECLARED_INLINE_P (value) = 1;
1100 /* grokfndecl set this to error_mark_node, but we want to
1101 leave it unset until synthesize_method. */
1102 DECL_INITIAL (value) = NULL_TREE;
1105 else if (TREE_CODE (init) == DEFERRED_PARSE)
1106 error ("invalid initializer for member function %qD", value);
1107 else if (TREE_CODE (TREE_TYPE (value)) == METHOD_TYPE)
1109 if (integer_zerop (init))
1110 DECL_PURE_VIRTUAL_P (value) = 1;
1111 else if (error_operand_p (init))
1112 ; /* An error has already been reported. */
1113 else
1114 error ("invalid initializer for member function %qD",
1115 value);
1117 else
1119 gcc_assert (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE);
1120 location_t iloc
1121 = cp_expr_loc_or_loc (init, DECL_SOURCE_LOCATION (value));
1122 if (friendp)
1123 error_at (iloc, "initializer specified for friend "
1124 "function %qD", value);
1125 else
1126 error_at (iloc, "initializer specified for static "
1127 "member function %qD", value);
1130 else if (TREE_CODE (value) == FIELD_DECL)
1131 /* C++11 NSDMI, keep going. */;
1132 else if (!VAR_P (value))
1133 gcc_unreachable ();
1136 /* Pass friend decls back. */
1137 if ((TREE_CODE (value) == FUNCTION_DECL
1138 || TREE_CODE (value) == TEMPLATE_DECL)
1139 && DECL_CONTEXT (value) != current_class_type)
1141 if (attrlist)
1142 cplus_decl_attributes (&value, attrlist, 0);
1143 return value;
1146 /* Need to set this before push_template_decl. */
1147 if (VAR_P (value))
1148 DECL_CONTEXT (value) = current_class_type;
1150 if (processing_template_decl && VAR_OR_FUNCTION_DECL_P (value))
1152 value = push_template_decl (value);
1153 if (error_operand_p (value))
1154 return error_mark_node;
1157 if (attrlist)
1158 cplus_decl_attributes (&value, attrlist, 0);
1160 if (init && DIRECT_LIST_INIT_P (init))
1161 flags = LOOKUP_NORMAL;
1162 else
1163 flags = LOOKUP_IMPLICIT;
1165 switch (TREE_CODE (value))
1167 case VAR_DECL:
1168 finish_static_data_member_decl (value, init, init_const_expr_p,
1169 asmspec_tree, flags);
1170 return value;
1172 case FIELD_DECL:
1173 if (asmspec)
1174 error ("%<asm%> specifiers are not permitted on non-static data members");
1175 if (DECL_INITIAL (value) == error_mark_node)
1176 init = error_mark_node;
1177 cp_finish_decl (value, init, /*init_const_expr_p=*/false,
1178 NULL_TREE, flags);
1179 DECL_IN_AGGR_P (value) = 1;
1180 return value;
1182 case FUNCTION_DECL:
1183 if (asmspec)
1184 set_user_assembler_name (value, asmspec);
1186 cp_finish_decl (value,
1187 /*init=*/NULL_TREE,
1188 /*init_const_expr_p=*/false,
1189 asmspec_tree, flags);
1191 /* Pass friends back this way. */
1192 if (DECL_UNIQUE_FRIEND_P (value))
1193 return void_type_node;
1195 DECL_IN_AGGR_P (value) = 1;
1196 return value;
1198 default:
1199 gcc_unreachable ();
1201 return NULL_TREE;
1204 /* Like `grokfield', but for bitfields.
1205 WIDTH is the width of the bitfield, a constant expression.
1206 The other parameters are as for grokfield. */
1208 tree
1209 grokbitfield (const cp_declarator *declarator,
1210 cp_decl_specifier_seq *declspecs, tree width, tree init,
1211 tree attrlist)
1213 tree value = grokdeclarator (declarator, declspecs, BITFIELD,
1214 init != NULL_TREE, &attrlist);
1216 if (value == error_mark_node)
1217 return NULL_TREE; /* friends went bad. */
1219 tree type = TREE_TYPE (value);
1220 if (type == error_mark_node)
1221 return value;
1223 /* Pass friendly classes back. */
1224 if (VOID_TYPE_P (value))
1225 return void_type_node;
1227 if (!INTEGRAL_OR_ENUMERATION_TYPE_P (type)
1228 && (INDIRECT_TYPE_P (type) || !dependent_type_p (type)))
1230 error_at (DECL_SOURCE_LOCATION (value),
1231 "bit-field %qD with non-integral type %qT",
1232 value, type);
1233 return error_mark_node;
1236 if (TREE_CODE (value) == TYPE_DECL)
1238 error_at (DECL_SOURCE_LOCATION (value),
1239 "cannot declare %qD to be a bit-field type", value);
1240 return NULL_TREE;
1243 /* Usually, finish_struct_1 catches bitfields with invalid types.
1244 But, in the case of bitfields with function type, we confuse
1245 ourselves into thinking they are member functions, so we must
1246 check here. */
1247 if (TREE_CODE (value) == FUNCTION_DECL)
1249 error_at (DECL_SOURCE_LOCATION (value),
1250 "cannot declare bit-field %qD with function type", value);
1251 return NULL_TREE;
1254 if (TYPE_WARN_IF_NOT_ALIGN (type))
1256 error_at (DECL_SOURCE_LOCATION (value), "cannot declare bit-field "
1257 "%qD with %<warn_if_not_aligned%> type", value);
1258 return NULL_TREE;
1261 if (DECL_IN_AGGR_P (value))
1263 error ("%qD is already defined in the class %qT", value,
1264 DECL_CONTEXT (value));
1265 return void_type_node;
1268 if (TREE_STATIC (value))
1270 error_at (DECL_SOURCE_LOCATION (value),
1271 "static member %qD cannot be a bit-field", value);
1272 return NULL_TREE;
1275 int flags = LOOKUP_IMPLICIT;
1276 if (init && DIRECT_LIST_INIT_P (init))
1277 flags = LOOKUP_NORMAL;
1278 cp_finish_decl (value, init, false, NULL_TREE, flags);
1280 if (width != error_mark_node)
1282 /* The width must be an integer type. */
1283 if (!type_dependent_expression_p (width)
1284 && !INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (width)))
1285 error ("width of bit-field %qD has non-integral type %qT", value,
1286 TREE_TYPE (width));
1287 else if (!check_for_bare_parameter_packs (width))
1289 /* Temporarily stash the width in DECL_BIT_FIELD_REPRESENTATIVE.
1290 check_bitfield_decl picks it from there later and sets DECL_SIZE
1291 accordingly. */
1292 DECL_BIT_FIELD_REPRESENTATIVE (value) = width;
1293 SET_DECL_C_BIT_FIELD (value);
1297 DECL_IN_AGGR_P (value) = 1;
1299 if (attrlist)
1300 cplus_decl_attributes (&value, attrlist, /*flags=*/0);
1302 return value;
1306 /* Returns true iff ATTR is an attribute which needs to be applied at
1307 instantiation time rather than template definition time. */
1309 static bool
1310 is_late_template_attribute (tree attr, tree decl)
1312 tree name = get_attribute_name (attr);
1313 tree args = TREE_VALUE (attr);
1314 const struct attribute_spec *spec = lookup_attribute_spec (name);
1315 tree arg;
1317 if (!spec)
1318 /* Unknown attribute. */
1319 return false;
1321 /* Attribute weak handling wants to write out assembly right away. */
1322 if (is_attribute_p ("weak", name))
1323 return true;
1325 /* Attributes used and unused are applied directly to typedefs for the
1326 benefit of maybe_warn_unused_local_typedefs. */
1327 if (TREE_CODE (decl) == TYPE_DECL
1328 && (is_attribute_p ("unused", name)
1329 || is_attribute_p ("used", name)))
1330 return false;
1332 /* Attribute tls_model wants to modify the symtab. */
1333 if (is_attribute_p ("tls_model", name))
1334 return true;
1336 /* #pragma omp declare simd attribute needs to be always deferred. */
1337 if (flag_openmp
1338 && is_attribute_p ("omp declare simd", name))
1339 return true;
1341 if (args == error_mark_node)
1342 return false;
1344 /* An attribute pack is clearly dependent. */
1345 if (args && PACK_EXPANSION_P (args))
1346 return true;
1348 /* If any of the arguments are dependent expressions, we can't evaluate
1349 the attribute until instantiation time. */
1350 for (arg = args; arg; arg = TREE_CHAIN (arg))
1352 tree t = TREE_VALUE (arg);
1354 /* If the first attribute argument is an identifier, only consider
1355 second and following arguments. Attributes like mode, format,
1356 cleanup and several target specific attributes aren't late
1357 just because they have an IDENTIFIER_NODE as first argument. */
1358 if (arg == args && attribute_takes_identifier_p (name)
1359 && identifier_p (t))
1360 continue;
1362 if (value_dependent_expression_p (t))
1363 return true;
1366 if (TREE_CODE (decl) == TYPE_DECL
1367 || TYPE_P (decl)
1368 || spec->type_required)
1370 tree type = TYPE_P (decl) ? decl : TREE_TYPE (decl);
1372 if (!type)
1373 return true;
1375 /* We can't apply any attributes to a completely unknown type until
1376 instantiation time. */
1377 enum tree_code code = TREE_CODE (type);
1378 if (code == TEMPLATE_TYPE_PARM
1379 || code == BOUND_TEMPLATE_TEMPLATE_PARM
1380 || code == TYPENAME_TYPE)
1381 return true;
1382 /* Also defer most attributes on dependent types. This is not
1383 necessary in all cases, but is the better default. */
1384 else if (dependent_type_p (type)
1385 /* But some attributes specifically apply to templates. */
1386 && !is_attribute_p ("abi_tag", name)
1387 && !is_attribute_p ("deprecated", name)
1388 && !is_attribute_p ("unavailable", name)
1389 && !is_attribute_p ("visibility", name))
1390 return true;
1391 else
1392 return false;
1394 else
1395 return false;
1398 /* ATTR_P is a list of attributes. Remove any attributes which need to be
1399 applied at instantiation time and return them. If IS_DEPENDENT is true,
1400 the declaration itself is dependent, so all attributes should be applied
1401 at instantiation time. */
1403 tree
1404 splice_template_attributes (tree *attr_p, tree decl)
1406 tree *p = attr_p;
1407 tree late_attrs = NULL_TREE;
1408 tree *q = &late_attrs;
1410 if (!p || *p == error_mark_node)
1411 return NULL_TREE;
1413 for (; *p; )
1415 if (is_late_template_attribute (*p, decl))
1417 ATTR_IS_DEPENDENT (*p) = 1;
1418 *q = *p;
1419 *p = TREE_CHAIN (*p);
1420 q = &TREE_CHAIN (*q);
1421 *q = NULL_TREE;
1423 else
1424 p = &TREE_CHAIN (*p);
1427 return late_attrs;
1430 /* Attach any LATE_ATTRS to DECL_P, after the non-dependent attributes have
1431 been applied by a previous call to decl_attributes. */
1433 static void
1434 save_template_attributes (tree late_attrs, tree *decl_p, int flags)
1436 tree *q;
1438 if (!late_attrs)
1439 return;
1441 if (DECL_P (*decl_p))
1442 q = &DECL_ATTRIBUTES (*decl_p);
1443 else
1444 q = &TYPE_ATTRIBUTES (*decl_p);
1446 tree old_attrs = *q;
1448 /* Place the late attributes at the beginning of the attribute
1449 list. */
1450 late_attrs = chainon (late_attrs, *q);
1451 if (*q != late_attrs
1452 && !DECL_P (*decl_p)
1453 && !(flags & ATTR_FLAG_TYPE_IN_PLACE))
1455 if (!dependent_type_p (*decl_p))
1456 *decl_p = cp_build_type_attribute_variant (*decl_p, late_attrs);
1457 else
1459 *decl_p = build_variant_type_copy (*decl_p);
1460 TYPE_ATTRIBUTES (*decl_p) = late_attrs;
1463 else
1464 *q = late_attrs;
1466 if (!DECL_P (*decl_p) && *decl_p == TYPE_MAIN_VARIANT (*decl_p))
1468 /* We've added new attributes directly to the main variant, so
1469 now we need to update all of the other variants to include
1470 these new attributes. */
1471 tree variant;
1472 for (variant = TYPE_NEXT_VARIANT (*decl_p); variant;
1473 variant = TYPE_NEXT_VARIANT (variant))
1475 gcc_assert (TYPE_ATTRIBUTES (variant) == old_attrs);
1476 TYPE_ATTRIBUTES (variant) = TYPE_ATTRIBUTES (*decl_p);
1481 /* True if ATTRS contains any dependent attributes that affect type
1482 identity. */
1484 bool
1485 any_dependent_type_attributes_p (tree attrs)
1487 for (tree a = attrs; a; a = TREE_CHAIN (a))
1488 if (ATTR_IS_DEPENDENT (a))
1490 const attribute_spec *as = lookup_attribute_spec (TREE_PURPOSE (a));
1491 if (as && as->affects_type_identity)
1492 return true;
1494 return false;
1497 /* Return true iff ATTRS are acceptable attributes to be applied in-place
1498 to a typedef which gives a previously unnamed class or enum a name for
1499 linkage purposes. */
1501 bool
1502 attributes_naming_typedef_ok (tree attrs)
1504 for (; attrs; attrs = TREE_CHAIN (attrs))
1506 tree name = get_attribute_name (attrs);
1507 if (is_attribute_p ("vector_size", name))
1508 return false;
1510 return true;
1513 /* Like reconstruct_complex_type, but handle also template trees. */
1515 tree
1516 cp_reconstruct_complex_type (tree type, tree bottom)
1518 tree inner, outer;
1520 if (TYPE_PTR_P (type))
1522 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1523 outer = build_pointer_type_for_mode (inner, TYPE_MODE (type),
1524 TYPE_REF_CAN_ALIAS_ALL (type));
1526 else if (TYPE_REF_P (type))
1528 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1529 outer = build_reference_type_for_mode (inner, TYPE_MODE (type),
1530 TYPE_REF_CAN_ALIAS_ALL (type));
1532 else if (TREE_CODE (type) == ARRAY_TYPE)
1534 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1535 outer = build_cplus_array_type (inner, TYPE_DOMAIN (type));
1536 /* Don't call cp_build_qualified_type on ARRAY_TYPEs, the
1537 element type qualification will be handled by the recursive
1538 cp_reconstruct_complex_type call and cp_build_qualified_type
1539 for ARRAY_TYPEs changes the element type. */
1540 return outer;
1542 else if (TREE_CODE (type) == FUNCTION_TYPE)
1544 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1545 outer = build_function_type (inner, TYPE_ARG_TYPES (type));
1546 outer = apply_memfn_quals (outer, type_memfn_quals (type));
1548 else if (TREE_CODE (type) == METHOD_TYPE)
1550 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1551 /* The build_method_type_directly() routine prepends 'this' to argument list,
1552 so we must compensate by getting rid of it. */
1553 outer
1554 = build_method_type_directly
1555 (class_of_this_parm (type), inner,
1556 TREE_CHAIN (TYPE_ARG_TYPES (type)));
1558 else if (TREE_CODE (type) == OFFSET_TYPE)
1560 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1561 outer = build_offset_type (TYPE_OFFSET_BASETYPE (type), inner);
1563 else
1564 return bottom;
1566 if (TYPE_ATTRIBUTES (type))
1567 outer = cp_build_type_attribute_variant (outer, TYPE_ATTRIBUTES (type));
1568 outer = cp_build_qualified_type (outer, cp_type_quals (type));
1569 outer = cxx_copy_lang_qualifiers (outer, type);
1571 return outer;
1574 /* Replaces any constexpr expression that may be into the attributes
1575 arguments with their reduced value. */
1577 void
1578 cp_check_const_attributes (tree attributes)
1580 if (attributes == error_mark_node)
1581 return;
1583 tree attr;
1584 for (attr = attributes; attr; attr = TREE_CHAIN (attr))
1586 if (cxx_contract_attribute_p (attr))
1587 continue;
1589 tree arg;
1590 /* As we implement alignas using gnu::aligned attribute and
1591 alignas argument is a constant expression, force manifestly
1592 constant evaluation of aligned attribute argument. */
1593 bool manifestly_const_eval
1594 = is_attribute_p ("aligned", get_attribute_name (attr));
1595 for (arg = TREE_VALUE (attr); arg && TREE_CODE (arg) == TREE_LIST;
1596 arg = TREE_CHAIN (arg))
1598 tree expr = TREE_VALUE (arg);
1599 if (EXPR_P (expr))
1600 TREE_VALUE (arg)
1601 = fold_non_dependent_expr (expr, tf_warning_or_error,
1602 manifestly_const_eval);
1607 /* Return the last pushed declaration for the symbol DECL or NULL
1608 when no such declaration exists. */
1610 static tree
1611 find_last_decl (tree decl)
1613 tree last_decl = NULL_TREE;
1615 if (tree name = DECL_P (decl) ? DECL_NAME (decl) : NULL_TREE)
1617 /* Template specializations are matched elsewhere. */
1618 if (DECL_LANG_SPECIFIC (decl)
1619 && DECL_USE_TEMPLATE (decl))
1620 return NULL_TREE;
1622 /* Look up the declaration in its scope. */
1623 tree pushed_scope = NULL_TREE;
1624 if (tree ctype = DECL_CONTEXT (decl))
1625 pushed_scope = push_scope (ctype);
1627 last_decl = lookup_name (name);
1629 if (pushed_scope)
1630 pop_scope (pushed_scope);
1632 /* The declaration may be a member conversion operator
1633 or a bunch of overfloads (handle the latter below). */
1634 if (last_decl && BASELINK_P (last_decl))
1635 last_decl = BASELINK_FUNCTIONS (last_decl);
1638 if (!last_decl)
1639 return NULL_TREE;
1641 if (DECL_P (last_decl) || TREE_CODE (last_decl) == OVERLOAD)
1643 /* A set of overloads of the same function. */
1644 for (lkp_iterator iter (last_decl); iter; ++iter)
1646 if (TREE_CODE (*iter) == OVERLOAD)
1647 continue;
1649 tree d = *iter;
1651 /* We can't compare versions in the middle of processing the
1652 attribute that has the version. */
1653 if (TREE_CODE (d) == FUNCTION_DECL
1654 && DECL_FUNCTION_VERSIONED (d))
1655 return NULL_TREE;
1657 if (decls_match (decl, d, /*record_decls=*/false))
1658 return d;
1660 return NULL_TREE;
1663 return NULL_TREE;
1666 /* Like decl_attributes, but handle C++ complexity. */
1668 void
1669 cplus_decl_attributes (tree *decl, tree attributes, int flags)
1671 if (*decl == NULL_TREE || *decl == void_type_node
1672 || *decl == error_mark_node || attributes == error_mark_node)
1673 return;
1675 /* Add implicit "omp declare target" attribute if requested. */
1676 if (vec_safe_length (scope_chain->omp_declare_target_attribute)
1677 && ((VAR_P (*decl)
1678 && (TREE_STATIC (*decl) || DECL_EXTERNAL (*decl)))
1679 || TREE_CODE (*decl) == FUNCTION_DECL))
1681 if (VAR_P (*decl)
1682 && DECL_CLASS_SCOPE_P (*decl))
1683 error ("%q+D static data member inside of declare target directive",
1684 *decl);
1685 else
1687 if (VAR_P (*decl)
1688 && (processing_template_decl
1689 || !omp_mappable_type (TREE_TYPE (*decl))))
1690 attributes
1691 = tree_cons (get_identifier ("omp declare target implicit"),
1692 NULL_TREE, attributes);
1693 else
1695 attributes = tree_cons (get_identifier ("omp declare target"),
1696 NULL_TREE, attributes);
1697 attributes
1698 = tree_cons (get_identifier ("omp declare target block"),
1699 NULL_TREE, attributes);
1701 if (TREE_CODE (*decl) == FUNCTION_DECL)
1703 cp_omp_declare_target_attr &last
1704 = scope_chain->omp_declare_target_attribute->last ();
1705 int device_type = MAX (last.device_type, 0);
1706 if ((device_type & OMP_CLAUSE_DEVICE_TYPE_HOST) != 0
1707 && !lookup_attribute ("omp declare target host",
1708 attributes))
1709 attributes
1710 = tree_cons (get_identifier ("omp declare target host"),
1711 NULL_TREE, attributes);
1712 if ((device_type & OMP_CLAUSE_DEVICE_TYPE_NOHOST) != 0
1713 && !lookup_attribute ("omp declare target nohost",
1714 attributes))
1715 attributes
1716 = tree_cons (get_identifier ("omp declare target nohost"),
1717 NULL_TREE, attributes);
1722 tree late_attrs = NULL_TREE;
1723 if (processing_template_decl)
1725 if (check_for_bare_parameter_packs (attributes))
1726 return;
1727 late_attrs = splice_template_attributes (&attributes, *decl);
1730 cp_check_const_attributes (attributes);
1732 if (flag_openmp || flag_openmp_simd)
1734 bool diagnosed = false;
1735 for (tree *pa = &attributes; *pa; )
1737 if (get_attribute_namespace (*pa) == omp_identifier)
1739 tree name = get_attribute_name (*pa);
1740 if (is_attribute_p ("directive", name)
1741 || is_attribute_p ("sequence", name))
1743 if (!diagnosed)
1745 error ("%<omp::%E%> not allowed to be specified in this "
1746 "context", name);
1747 diagnosed = true;
1749 *pa = TREE_CHAIN (*pa);
1750 continue;
1753 pa = &TREE_CHAIN (*pa);
1757 if (TREE_CODE (*decl) == TEMPLATE_DECL)
1758 decl = &DECL_TEMPLATE_RESULT (*decl);
1760 if (TREE_TYPE (*decl) && TYPE_PTRMEMFUNC_P (TREE_TYPE (*decl)))
1762 attributes
1763 = decl_attributes (decl, attributes, flags | ATTR_FLAG_FUNCTION_NEXT);
1764 decl_attributes (&TYPE_PTRMEMFUNC_FN_TYPE_RAW (TREE_TYPE (*decl)),
1765 attributes, flags);
1767 else
1769 tree last_decl = find_last_decl (*decl);
1770 decl_attributes (decl, attributes, flags, last_decl);
1773 if (late_attrs)
1774 save_template_attributes (late_attrs, decl, flags);
1776 /* Propagate deprecation out to the template. */
1777 if (TREE_DEPRECATED (*decl))
1778 if (tree ti = get_template_info (*decl))
1780 tree tmpl = TI_TEMPLATE (ti);
1781 tree pattern = (TYPE_P (*decl) ? TREE_TYPE (tmpl)
1782 : DECL_TEMPLATE_RESULT (tmpl));
1783 if (*decl == pattern)
1784 TREE_DEPRECATED (tmpl) = true;
1787 /* Likewise, propagate unavailability out to the template. */
1788 if (TREE_UNAVAILABLE (*decl))
1789 if (tree ti = get_template_info (*decl))
1791 tree tmpl = TI_TEMPLATE (ti);
1792 tree pattern = (TYPE_P (*decl) ? TREE_TYPE (tmpl)
1793 : DECL_TEMPLATE_RESULT (tmpl));
1794 if (*decl == pattern)
1795 TREE_UNAVAILABLE (tmpl) = true;
1799 /* Walks through the namespace- or function-scope anonymous union
1800 OBJECT, with the indicated TYPE, building appropriate VAR_DECLs.
1801 Returns one of the fields for use in the mangled name. */
1803 static tree
1804 build_anon_union_vars (tree type, tree object)
1806 tree main_decl = NULL_TREE;
1807 tree field;
1809 /* Rather than write the code to handle the non-union case,
1810 just give an error. */
1811 if (TREE_CODE (type) != UNION_TYPE)
1813 error_at (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
1814 "anonymous struct not inside named type");
1815 return error_mark_node;
1818 for (field = TYPE_FIELDS (type);
1819 field != NULL_TREE;
1820 field = DECL_CHAIN (field))
1822 tree decl;
1823 tree ref;
1825 if (DECL_ARTIFICIAL (field))
1826 continue;
1827 if (TREE_CODE (field) != FIELD_DECL)
1829 permerror (DECL_SOURCE_LOCATION (field),
1830 "%q#D invalid; an anonymous union can only "
1831 "have non-static data members", field);
1832 continue;
1835 if (TREE_PRIVATE (field))
1836 permerror (DECL_SOURCE_LOCATION (field),
1837 "private member %q#D in anonymous union", field);
1838 else if (TREE_PROTECTED (field))
1839 permerror (DECL_SOURCE_LOCATION (field),
1840 "protected member %q#D in anonymous union", field);
1842 if (processing_template_decl)
1843 ref = build_min_nt_loc (UNKNOWN_LOCATION, COMPONENT_REF, object,
1844 DECL_NAME (field), NULL_TREE);
1845 else
1846 ref = build_class_member_access_expr (object, field, NULL_TREE,
1847 false, tf_warning_or_error);
1849 if (DECL_NAME (field))
1851 tree base;
1853 decl = build_decl (input_location,
1854 VAR_DECL, DECL_NAME (field), TREE_TYPE (field));
1855 DECL_ANON_UNION_VAR_P (decl) = 1;
1856 DECL_ARTIFICIAL (decl) = 1;
1858 base = get_base_address (object);
1859 TREE_PUBLIC (decl) = TREE_PUBLIC (base);
1860 TREE_STATIC (decl) = TREE_STATIC (base);
1861 DECL_EXTERNAL (decl) = DECL_EXTERNAL (base);
1863 SET_DECL_VALUE_EXPR (decl, ref);
1864 DECL_HAS_VALUE_EXPR_P (decl) = 1;
1866 decl = pushdecl (decl);
1868 else if (ANON_AGGR_TYPE_P (TREE_TYPE (field)))
1869 decl = build_anon_union_vars (TREE_TYPE (field), ref);
1870 else
1871 decl = 0;
1873 if (main_decl == NULL_TREE)
1874 main_decl = decl;
1877 return main_decl;
1880 /* Finish off the processing of a UNION_TYPE structure. If the union is an
1881 anonymous union, then all members must be laid out together. PUBLIC_P
1882 is nonzero if this union is not declared static. */
1884 void
1885 finish_anon_union (tree anon_union_decl)
1887 tree type;
1888 tree main_decl;
1889 bool public_p;
1891 if (anon_union_decl == error_mark_node)
1892 return;
1894 type = TREE_TYPE (anon_union_decl);
1895 public_p = TREE_PUBLIC (anon_union_decl);
1897 /* The VAR_DECL's context is the same as the TYPE's context. */
1898 DECL_CONTEXT (anon_union_decl) = DECL_CONTEXT (TYPE_NAME (type));
1900 if (TYPE_FIELDS (type) == NULL_TREE)
1901 return;
1903 if (public_p)
1905 error ("namespace-scope anonymous aggregates must be static");
1906 return;
1909 main_decl = build_anon_union_vars (type, anon_union_decl);
1910 if (main_decl == error_mark_node)
1911 return;
1912 if (main_decl == NULL_TREE)
1914 pedwarn (input_location, 0, "anonymous union with no members");
1915 return;
1918 if (!processing_template_decl)
1920 /* Use main_decl to set the mangled name. */
1921 DECL_NAME (anon_union_decl) = DECL_NAME (main_decl);
1922 maybe_commonize_var (anon_union_decl);
1923 if (TREE_STATIC (anon_union_decl) || DECL_EXTERNAL (anon_union_decl))
1925 if (DECL_DISCRIMINATOR_P (anon_union_decl))
1926 determine_local_discriminator (anon_union_decl);
1927 mangle_decl (anon_union_decl);
1929 DECL_NAME (anon_union_decl) = NULL_TREE;
1932 pushdecl (anon_union_decl);
1933 cp_finish_decl (anon_union_decl, NULL_TREE, false, NULL_TREE, 0);
1936 /* Auxiliary functions to make type signatures for
1937 `operator new' and `operator delete' correspond to
1938 what compiler will be expecting. */
1940 tree
1941 coerce_new_type (tree type, location_t loc)
1943 int e = 0;
1944 tree args = TYPE_ARG_TYPES (type);
1946 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
1948 if (!same_type_p (TREE_TYPE (type), ptr_type_node))
1950 e = 1;
1951 error_at (loc, "%<operator new%> must return type %qT",
1952 ptr_type_node);
1955 if (args && args != void_list_node)
1957 if (TREE_PURPOSE (args))
1959 /* [basic.stc.dynamic.allocation]
1961 The first parameter shall not have an associated default
1962 argument. */
1963 error_at (loc, "the first parameter of %<operator new%> cannot "
1964 "have a default argument");
1965 /* Throw away the default argument. */
1966 TREE_PURPOSE (args) = NULL_TREE;
1969 if (!same_type_p (TREE_VALUE (args), size_type_node))
1971 e = 2;
1972 args = TREE_CHAIN (args);
1975 else
1976 e = 2;
1978 if (e == 2)
1979 permerror (loc, "%<operator new%> takes type %<size_t%> (%qT) "
1980 "as first parameter", size_type_node);
1982 switch (e)
1984 case 2:
1985 args = tree_cons (NULL_TREE, size_type_node, args);
1986 /* Fall through. */
1987 case 1:
1988 type = (cxx_copy_lang_qualifiers
1989 (build_function_type (ptr_type_node, args),
1990 type));
1991 /* Fall through. */
1992 default:;
1994 return type;
1997 void
1998 coerce_delete_type (tree decl, location_t loc)
2000 int e = 0;
2001 tree type = TREE_TYPE (decl);
2002 tree args = TYPE_ARG_TYPES (type);
2004 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
2006 if (!same_type_p (TREE_TYPE (type), void_type_node))
2008 e = 1;
2009 error_at (loc, "%<operator delete%> must return type %qT",
2010 void_type_node);
2013 tree ptrtype = ptr_type_node;
2014 if (destroying_delete_p (decl))
2016 if (DECL_CLASS_SCOPE_P (decl))
2017 /* If the function is a destroying operator delete declared in class
2018 type C, the type of its first parameter shall be C*. */
2019 ptrtype = build_pointer_type (DECL_CONTEXT (decl));
2020 else
2021 /* A destroying operator delete shall be a class member function named
2022 operator delete. */
2023 error_at (loc,
2024 "destroying %<operator delete%> must be a member function");
2025 const ovl_op_info_t *op = IDENTIFIER_OVL_OP_INFO (DECL_NAME (decl));
2026 if (op->flags & OVL_OP_FLAG_VEC)
2027 error_at (loc, "%<operator delete[]%> cannot be a destroying delete");
2028 if (!usual_deallocation_fn_p (decl))
2029 error_at (loc, "destroying %<operator delete%> must be a usual "
2030 "deallocation function");
2033 if (!args || args == void_list_node
2034 || !same_type_p (TREE_VALUE (args), ptrtype))
2036 e = 2;
2037 if (args && args != void_list_node)
2038 args = TREE_CHAIN (args);
2039 error_at (loc, "%<operator delete%> takes type %qT as first parameter",
2040 ptrtype);
2042 switch (e)
2044 case 2:
2045 args = tree_cons (NULL_TREE, ptrtype, args);
2046 /* Fall through. */
2047 case 1:
2048 type = (cxx_copy_lang_qualifiers
2049 (build_function_type (void_type_node, args),
2050 type));
2051 /* Fall through. */
2052 default:;
2055 TREE_TYPE (decl) = type;
2058 /* DECL is a VAR_DECL for a vtable: walk through the entries in the vtable
2059 and mark them as needed. */
2061 static void
2062 mark_vtable_entries (tree decl, vec<tree> &consteval_vtables)
2064 /* It's OK for the vtable to refer to deprecated virtual functions. */
2065 warning_sentinel w(warn_deprecated_decl);
2067 bool consteval_seen = false;
2069 for (auto &e: CONSTRUCTOR_ELTS (DECL_INITIAL (decl)))
2071 tree fnaddr = e.value;
2073 STRIP_NOPS (fnaddr);
2075 if (TREE_CODE (fnaddr) != ADDR_EXPR
2076 && TREE_CODE (fnaddr) != FDESC_EXPR)
2077 /* This entry is an offset: a virtual base class offset, a
2078 virtual call offset, an RTTI offset, etc. */
2079 continue;
2081 tree fn = TREE_OPERAND (fnaddr, 0);
2082 if (TREE_CODE (fn) == FUNCTION_DECL && DECL_IMMEDIATE_FUNCTION_P (fn))
2084 if (!consteval_seen)
2086 consteval_seen = true;
2087 consteval_vtables.safe_push (decl);
2089 continue;
2091 TREE_ADDRESSABLE (fn) = 1;
2092 /* When we don't have vcall offsets, we output thunks whenever
2093 we output the vtables that contain them. With vcall offsets,
2094 we know all the thunks we'll need when we emit a virtual
2095 function, so we emit the thunks there instead. */
2096 if (DECL_THUNK_P (fn))
2097 use_thunk (fn, /*emit_p=*/0);
2098 /* Set the location, as marking the function could cause
2099 instantiation. We do not need to preserve the incoming
2100 location, as we're called from c_parse_final_cleanups, which
2101 takes care of that. */
2102 input_location = DECL_SOURCE_LOCATION (fn);
2103 mark_used (fn);
2107 /* Replace any consteval functions in vtables with null pointers. */
2109 static void
2110 clear_consteval_vfns (vec<tree> &consteval_vtables)
2112 for (tree vtable : consteval_vtables)
2113 for (constructor_elt &elt : CONSTRUCTOR_ELTS (DECL_INITIAL (vtable)))
2115 tree fn = cp_get_fndecl_from_callee (elt.value, /*fold*/false);
2116 if (fn && DECL_IMMEDIATE_FUNCTION_P (fn))
2117 elt.value = build_zero_cst (vtable_entry_type);
2121 /* Adjust the TLS model on variable DECL if need be, typically after
2122 the linkage of DECL has been modified. */
2124 static void
2125 adjust_var_decl_tls_model (tree decl)
2127 if (CP_DECL_THREAD_LOCAL_P (decl)
2128 && !lookup_attribute ("tls_model", DECL_ATTRIBUTES (decl)))
2129 set_decl_tls_model (decl, decl_default_tls_model (decl));
2132 /* Set DECL up to have the closest approximation of "initialized common"
2133 linkage available. */
2135 void
2136 comdat_linkage (tree decl)
2138 if (flag_weak)
2140 make_decl_one_only (decl, cxx_comdat_group (decl));
2141 if (HAVE_COMDAT_GROUP && flag_contracts && DECL_CONTRACTS (decl))
2143 symtab_node *n = symtab_node::get (decl);
2144 if (tree pre = DECL_PRE_FN (decl))
2145 cgraph_node::get_create (pre)->add_to_same_comdat_group (n);
2146 if (tree post = DECL_POST_FN (decl))
2147 cgraph_node::get_create (post)->add_to_same_comdat_group (n);
2150 else if (TREE_CODE (decl) == FUNCTION_DECL
2151 || (VAR_P (decl) && DECL_ARTIFICIAL (decl)))
2152 /* We can just emit function and compiler-generated variables
2153 statically; having multiple copies is (for the most part) only
2154 a waste of space.
2156 There are two correctness issues, however: the address of a
2157 template instantiation with external linkage should be the
2158 same, independent of what translation unit asks for the
2159 address, and this will not hold when we emit multiple copies of
2160 the function. However, there's little else we can do.
2162 Also, by default, the typeinfo implementation assumes that
2163 there will be only one copy of the string used as the name for
2164 each type. Therefore, if weak symbols are unavailable, the
2165 run-time library should perform a more conservative check; it
2166 should perform a string comparison, rather than an address
2167 comparison. */
2168 TREE_PUBLIC (decl) = 0;
2169 else
2171 /* Static data member template instantiations, however, cannot
2172 have multiple copies. */
2173 if (DECL_INITIAL (decl) == 0
2174 || DECL_INITIAL (decl) == error_mark_node)
2175 DECL_COMMON (decl) = 1;
2176 else if (EMPTY_CONSTRUCTOR_P (DECL_INITIAL (decl)))
2178 DECL_COMMON (decl) = 1;
2179 DECL_INITIAL (decl) = error_mark_node;
2181 else if (!DECL_EXPLICIT_INSTANTIATION (decl))
2183 /* We can't do anything useful; leave vars for explicit
2184 instantiation. */
2185 DECL_EXTERNAL (decl) = 1;
2186 DECL_NOT_REALLY_EXTERN (decl) = 0;
2190 if (TREE_PUBLIC (decl))
2191 DECL_COMDAT (decl) = 1;
2193 if (VAR_P (decl))
2194 adjust_var_decl_tls_model (decl);
2197 /* For win32 we also want to put explicit instantiations in
2198 linkonce sections, so that they will be merged with implicit
2199 instantiations; otherwise we get duplicate symbol errors.
2200 For Darwin we do not want explicit instantiations to be
2201 linkonce. */
2203 void
2204 maybe_make_one_only (tree decl)
2206 /* We used to say that this was not necessary on targets that support weak
2207 symbols, because the implicit instantiations will defer to the explicit
2208 one. However, that's not actually the case in SVR4; a strong definition
2209 after a weak one is an error. Also, not making explicit
2210 instantiations one_only means that we can end up with two copies of
2211 some template instantiations. */
2212 if (! flag_weak)
2213 return;
2215 /* We can't set DECL_COMDAT on functions, or cp_finish_file will think
2216 we can get away with not emitting them if they aren't used. We need
2217 to for variables so that cp_finish_decl will update their linkage,
2218 because their DECL_INITIAL may not have been set properly yet. */
2220 if (!TARGET_WEAK_NOT_IN_ARCHIVE_TOC
2221 || (! DECL_EXPLICIT_INSTANTIATION (decl)
2222 && ! DECL_TEMPLATE_SPECIALIZATION (decl)))
2224 make_decl_one_only (decl, cxx_comdat_group (decl));
2226 if (VAR_P (decl))
2228 varpool_node *node = varpool_node::get_create (decl);
2229 DECL_COMDAT (decl) = 1;
2230 /* Mark it needed so we don't forget to emit it. */
2231 node->forced_by_abi = true;
2232 TREE_USED (decl) = 1;
2234 adjust_var_decl_tls_model (decl);
2239 /* Returns true iff DECL, a FUNCTION_DECL or VAR_DECL, has vague linkage.
2240 This predicate will give the right answer during parsing of the
2241 function, which other tests may not. */
2243 bool
2244 vague_linkage_p (tree decl)
2246 if (!TREE_PUBLIC (decl))
2248 /* maybe_thunk_body clears TREE_PUBLIC and DECL_ABSTRACT_P on the
2249 maybe-in-charge 'tor variants; in that case we need to check one of
2250 the "clones" for the real linkage. But only in that case; before
2251 maybe_clone_body we haven't yet copied the linkage to the clones. */
2252 if (DECL_MAYBE_IN_CHARGE_CDTOR_P (decl)
2253 && !DECL_ABSTRACT_P (decl)
2254 && DECL_CHAIN (decl)
2255 && DECL_CLONED_FUNCTION_P (DECL_CHAIN (decl)))
2256 return vague_linkage_p (DECL_CHAIN (decl));
2258 gcc_checking_assert (!DECL_COMDAT (decl));
2259 return false;
2261 /* Unfortunately, import_export_decl has not always been called
2262 before the function is processed, so we cannot simply check
2263 DECL_COMDAT. */
2264 if (DECL_COMDAT (decl)
2265 || (TREE_CODE (decl) == FUNCTION_DECL
2266 && DECL_DECLARED_INLINE_P (decl))
2267 || (DECL_LANG_SPECIFIC (decl)
2268 && DECL_TEMPLATE_INSTANTIATION (decl))
2269 || (VAR_P (decl) && DECL_INLINE_VAR_P (decl)))
2270 return true;
2271 else if (DECL_FUNCTION_SCOPE_P (decl))
2272 /* A local static in an inline effectively has vague linkage. */
2273 return (TREE_STATIC (decl)
2274 && vague_linkage_p (DECL_CONTEXT (decl)));
2275 else
2276 return false;
2279 /* Determine whether or not we want to specifically import or export CTYPE,
2280 using various heuristics. */
2282 static void
2283 import_export_class (tree ctype)
2285 /* -1 for imported, 1 for exported. */
2286 int import_export = 0;
2288 /* It only makes sense to call this function at EOF. The reason is
2289 that this function looks at whether or not the first non-inline
2290 non-abstract virtual member function has been defined in this
2291 translation unit. But, we can't possibly know that until we've
2292 seen the entire translation unit. */
2293 gcc_assert (at_eof);
2295 if (CLASSTYPE_INTERFACE_KNOWN (ctype))
2296 return;
2298 /* If MULTIPLE_SYMBOL_SPACES is set and we saw a #pragma interface,
2299 we will have CLASSTYPE_INTERFACE_ONLY set but not
2300 CLASSTYPE_INTERFACE_KNOWN. In that case, we don't want to use this
2301 heuristic because someone will supply a #pragma implementation
2302 elsewhere, and deducing it here would produce a conflict. */
2303 if (CLASSTYPE_INTERFACE_ONLY (ctype))
2304 return;
2306 if (lookup_attribute ("dllimport", TYPE_ATTRIBUTES (ctype)))
2307 import_export = -1;
2308 else if (lookup_attribute ("dllexport", TYPE_ATTRIBUTES (ctype)))
2309 import_export = 1;
2310 else if (CLASSTYPE_IMPLICIT_INSTANTIATION (ctype)
2311 && !flag_implicit_templates)
2312 /* For a template class, without -fimplicit-templates, check the
2313 repository. If the virtual table is assigned to this
2314 translation unit, then export the class; otherwise, import
2315 it. */
2316 import_export = -1;
2317 else if (TYPE_POLYMORPHIC_P (ctype))
2319 /* The ABI specifies that the virtual table and associated
2320 information are emitted with the key method, if any. */
2321 tree method = CLASSTYPE_KEY_METHOD (ctype);
2322 /* If weak symbol support is not available, then we must be
2323 careful not to emit the vtable when the key function is
2324 inline. An inline function can be defined in multiple
2325 translation units. If we were to emit the vtable in each
2326 translation unit containing a definition, we would get
2327 multiple definition errors at link-time. */
2328 if (method && (flag_weak || ! DECL_DECLARED_INLINE_P (method)))
2329 import_export = (DECL_REALLY_EXTERN (method) ? -1 : 1);
2332 /* When MULTIPLE_SYMBOL_SPACES is set, we cannot count on seeing
2333 a definition anywhere else. */
2334 if (MULTIPLE_SYMBOL_SPACES && import_export == -1)
2335 import_export = 0;
2337 /* Allow back ends the chance to overrule the decision. */
2338 if (targetm.cxx.import_export_class)
2339 import_export = targetm.cxx.import_export_class (ctype, import_export);
2341 if (import_export)
2343 SET_CLASSTYPE_INTERFACE_KNOWN (ctype);
2344 CLASSTYPE_INTERFACE_ONLY (ctype) = (import_export < 0);
2348 /* Return true if VAR has already been provided to the back end; in that
2349 case VAR should not be modified further by the front end. */
2350 static bool
2351 var_finalized_p (tree var)
2353 return varpool_node::get_create (var)->definition;
2356 /* DECL is a VAR_DECL or FUNCTION_DECL which, for whatever reason,
2357 must be emitted in this translation unit. Mark it as such. */
2359 void
2360 mark_needed (tree decl)
2362 TREE_USED (decl) = 1;
2363 if (TREE_CODE (decl) == FUNCTION_DECL)
2365 /* Extern inline functions don't become needed when referenced.
2366 If we know a method will be emitted in other TU and no new
2367 functions can be marked reachable, just use the external
2368 definition. */
2369 struct cgraph_node *node = cgraph_node::get_create (decl);
2370 node->forced_by_abi = true;
2372 /* #pragma interface can call mark_needed for
2373 maybe-in-charge 'tors; mark the clones as well. */
2374 tree clone;
2375 FOR_EACH_CLONE (clone, decl)
2376 mark_needed (clone);
2378 else if (VAR_P (decl))
2380 varpool_node *node = varpool_node::get_create (decl);
2381 /* C++ frontend use mark_decl_references to force COMDAT variables
2382 to be output that might appear dead otherwise. */
2383 node->forced_by_abi = true;
2387 /* DECL is either a FUNCTION_DECL or a VAR_DECL. This function
2388 returns true if a definition of this entity should be provided in
2389 this object file. Callers use this function to determine whether
2390 or not to let the back end know that a definition of DECL is
2391 available in this translation unit. */
2393 bool
2394 decl_needed_p (tree decl)
2396 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
2397 /* This function should only be called at the end of the translation
2398 unit. We cannot be sure of whether or not something will be
2399 COMDAT until that point. */
2400 gcc_assert (at_eof);
2402 /* All entities with external linkage that are not COMDAT/EXTERN should be
2403 emitted; they may be referred to from other object files. */
2404 if (TREE_PUBLIC (decl) && !DECL_COMDAT (decl) && !DECL_REALLY_EXTERN (decl))
2405 return true;
2407 /* Functions marked "dllexport" must be emitted so that they are
2408 visible to other DLLs. */
2409 if (flag_keep_inline_dllexport
2410 && lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl)))
2411 return true;
2413 /* When not optimizing, do not bother to produce definitions for extern
2414 symbols. */
2415 if (DECL_REALLY_EXTERN (decl)
2416 && ((TREE_CODE (decl) != FUNCTION_DECL
2417 && !optimize)
2418 || (TREE_CODE (decl) == FUNCTION_DECL
2419 && !opt_for_fn (decl, optimize)))
2420 && !lookup_attribute ("always_inline", decl))
2421 return false;
2423 /* If this entity was used, let the back end see it; it will decide
2424 whether or not to emit it into the object file. */
2425 if (TREE_USED (decl))
2426 return true;
2428 /* Virtual functions might be needed for devirtualization. */
2429 if (flag_devirtualize
2430 && TREE_CODE (decl) == FUNCTION_DECL
2431 && DECL_VIRTUAL_P (decl))
2432 return true;
2434 /* Otherwise, DECL does not need to be emitted -- yet. A subsequent
2435 reference to DECL might cause it to be emitted later. */
2436 return false;
2439 /* If necessary, write out the vtables for the dynamic class CTYPE.
2440 Returns true if any vtables were emitted. */
2442 static bool
2443 maybe_emit_vtables (tree ctype, vec<tree> &consteval_vtables)
2445 tree vtbl;
2446 tree primary_vtbl;
2447 int needed = 0;
2448 varpool_node *current = NULL, *last = NULL;
2450 /* If the vtables for this class have already been emitted there is
2451 nothing more to do. */
2452 primary_vtbl = CLASSTYPE_VTABLES (ctype);
2453 if (var_finalized_p (primary_vtbl))
2454 return false;
2455 /* Ignore dummy vtables made by get_vtable_decl. */
2456 if (TREE_TYPE (primary_vtbl) == void_type_node)
2457 return false;
2459 /* On some targets, we cannot determine the key method until the end
2460 of the translation unit -- which is when this function is
2461 called. */
2462 if (!targetm.cxx.key_method_may_be_inline ())
2463 determine_key_method (ctype);
2465 /* See if any of the vtables are needed. */
2466 for (vtbl = CLASSTYPE_VTABLES (ctype); vtbl; vtbl = DECL_CHAIN (vtbl))
2468 import_export_decl (vtbl);
2469 if (DECL_NOT_REALLY_EXTERN (vtbl) && decl_needed_p (vtbl))
2470 needed = 1;
2472 if (!needed)
2474 /* If the references to this class' vtables are optimized away,
2475 still emit the appropriate debugging information. See
2476 dfs_debug_mark. */
2477 if (DECL_COMDAT (primary_vtbl)
2478 && CLASSTYPE_DEBUG_REQUESTED (ctype))
2479 note_debug_info_needed (ctype);
2480 return false;
2483 /* The ABI requires that we emit all of the vtables if we emit any
2484 of them. */
2485 for (vtbl = CLASSTYPE_VTABLES (ctype); vtbl; vtbl = DECL_CHAIN (vtbl))
2487 /* Mark entities references from the virtual table as used. */
2488 mark_vtable_entries (vtbl, consteval_vtables);
2490 if (TREE_TYPE (DECL_INITIAL (vtbl)) == 0)
2492 vec<tree, va_gc> *cleanups = NULL;
2493 tree expr = store_init_value (vtbl, DECL_INITIAL (vtbl), &cleanups,
2494 LOOKUP_NORMAL);
2496 /* It had better be all done at compile-time. */
2497 gcc_assert (!expr && !cleanups);
2500 /* Write it out. */
2501 DECL_EXTERNAL (vtbl) = 0;
2502 rest_of_decl_compilation (vtbl, 1, 1);
2504 /* Because we're only doing syntax-checking, we'll never end up
2505 actually marking the variable as written. */
2506 if (flag_syntax_only)
2507 TREE_ASM_WRITTEN (vtbl) = 1;
2508 else if (DECL_ONE_ONLY (vtbl))
2510 current = varpool_node::get_create (vtbl);
2511 if (last)
2512 current->add_to_same_comdat_group (last);
2513 last = current;
2517 /* For abstract classes, the destructor has been removed from the
2518 vtable (in class.cc's build_vtbl_initializer). For a compiler-
2519 generated destructor, it hence might not have been generated in
2520 this translation unit - and with '#pragma interface' it might
2521 never get generated. */
2522 if (CLASSTYPE_PURE_VIRTUALS (ctype)
2523 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (ctype)
2524 && !CLASSTYPE_LAZY_DESTRUCTOR (ctype)
2525 && DECL_DEFAULTED_IN_CLASS_P (CLASSTYPE_DESTRUCTOR (ctype)))
2526 note_vague_linkage_fn (CLASSTYPE_DESTRUCTOR (ctype));
2528 /* Since we're writing out the vtable here, also write the debug
2529 info. */
2530 note_debug_info_needed (ctype);
2532 return true;
2535 /* A special return value from type_visibility meaning internal
2536 linkage. */
2538 enum { VISIBILITY_ANON = VISIBILITY_INTERNAL+1 };
2540 static int expr_visibility (tree);
2541 static int type_visibility (tree);
2543 /* walk_tree helper function for type_visibility. */
2545 static tree
2546 min_vis_r (tree *tp, int *walk_subtrees, void *data)
2548 int *vis_p = (int *)data;
2549 int this_vis = VISIBILITY_DEFAULT;
2550 if (! TYPE_P (*tp))
2551 *walk_subtrees = 0;
2552 else if (OVERLOAD_TYPE_P (*tp)
2553 && !TREE_PUBLIC (TYPE_MAIN_DECL (*tp)))
2555 this_vis = VISIBILITY_ANON;
2556 *walk_subtrees = 0;
2558 else if (CLASS_TYPE_P (*tp))
2560 this_vis = CLASSTYPE_VISIBILITY (*tp);
2561 *walk_subtrees = 0;
2563 else if (TREE_CODE (*tp) == ARRAY_TYPE
2564 && uses_template_parms (TYPE_DOMAIN (*tp)))
2565 this_vis = expr_visibility (TYPE_MAX_VALUE (TYPE_DOMAIN (*tp)));
2567 if (this_vis > *vis_p)
2568 *vis_p = this_vis;
2570 /* Tell cp_walk_subtrees to look through typedefs. */
2571 if (*walk_subtrees == 1)
2572 *walk_subtrees = 2;
2574 return NULL;
2577 /* walk_tree helper function for expr_visibility. */
2579 static tree
2580 min_vis_expr_r (tree *tp, int */*walk_subtrees*/, void *data)
2582 int *vis_p = (int *)data;
2583 int tpvis = VISIBILITY_DEFAULT;
2585 switch (TREE_CODE (*tp))
2587 case CAST_EXPR:
2588 case IMPLICIT_CONV_EXPR:
2589 case STATIC_CAST_EXPR:
2590 case REINTERPRET_CAST_EXPR:
2591 case CONST_CAST_EXPR:
2592 case DYNAMIC_CAST_EXPR:
2593 case NEW_EXPR:
2594 case CONSTRUCTOR:
2595 case LAMBDA_EXPR:
2596 tpvis = type_visibility (TREE_TYPE (*tp));
2597 break;
2599 case VAR_DECL:
2600 case FUNCTION_DECL:
2601 if (! TREE_PUBLIC (*tp))
2602 tpvis = VISIBILITY_ANON;
2603 else
2604 tpvis = DECL_VISIBILITY (*tp);
2605 break;
2607 default:
2608 break;
2611 if (tpvis > *vis_p)
2612 *vis_p = tpvis;
2614 return NULL_TREE;
2617 /* Returns the visibility of TYPE, which is the minimum visibility of its
2618 component types. */
2620 static int
2621 type_visibility (tree type)
2623 int vis = VISIBILITY_DEFAULT;
2624 cp_walk_tree_without_duplicates (&type, min_vis_r, &vis);
2625 return vis;
2628 /* Returns the visibility of an expression EXPR that appears in the signature
2629 of a function template, which is the minimum visibility of names that appear
2630 in its mangling. */
2632 static int
2633 expr_visibility (tree expr)
2635 int vis = VISIBILITY_DEFAULT;
2636 cp_walk_tree_without_duplicates (&expr, min_vis_expr_r, &vis);
2637 return vis;
2640 /* Limit the visibility of DECL to VISIBILITY, if not explicitly
2641 specified (or if VISIBILITY is static). If TMPL is true, this
2642 constraint is for a template argument, and takes precedence
2643 over explicitly-specified visibility on the template. */
2645 static void
2646 constrain_visibility (tree decl, int visibility, bool tmpl)
2648 if (visibility == VISIBILITY_ANON)
2650 /* extern "C" declarations aren't affected by the anonymous
2651 namespace. */
2652 if (!DECL_EXTERN_C_P (decl))
2654 TREE_PUBLIC (decl) = 0;
2655 DECL_WEAK (decl) = 0;
2656 DECL_COMMON (decl) = 0;
2657 DECL_COMDAT (decl) = false;
2658 if (VAR_OR_FUNCTION_DECL_P (decl))
2660 struct symtab_node *snode = symtab_node::get (decl);
2662 if (snode)
2663 snode->set_comdat_group (NULL);
2665 DECL_INTERFACE_KNOWN (decl) = 1;
2666 if (DECL_LANG_SPECIFIC (decl))
2667 DECL_NOT_REALLY_EXTERN (decl) = 1;
2670 else if (visibility > DECL_VISIBILITY (decl)
2671 && (tmpl || !DECL_VISIBILITY_SPECIFIED (decl)))
2673 DECL_VISIBILITY (decl) = (enum symbol_visibility) visibility;
2674 /* This visibility was not specified. */
2675 DECL_VISIBILITY_SPECIFIED (decl) = false;
2679 /* Constrain the visibility of DECL based on the visibility of its template
2680 arguments. */
2682 static void
2683 constrain_visibility_for_template (tree decl, tree targs)
2685 /* If this is a template instantiation, check the innermost
2686 template args for visibility constraints. The outer template
2687 args are covered by the class check. */
2688 tree args = INNERMOST_TEMPLATE_ARGS (targs);
2689 int i;
2690 for (i = TREE_VEC_LENGTH (args); i > 0; --i)
2692 int vis = 0;
2694 tree arg = TREE_VEC_ELT (args, i-1);
2695 if (TYPE_P (arg))
2696 vis = type_visibility (arg);
2697 else
2698 vis = expr_visibility (arg);
2699 if (vis)
2700 constrain_visibility (decl, vis, true);
2704 /* Like c_determine_visibility, but with additional C++-specific
2705 behavior.
2707 Function-scope entities can rely on the function's visibility because
2708 it is set in start_preparsed_function.
2710 Class-scope entities cannot rely on the class's visibility until the end
2711 of the enclosing class definition.
2713 Note that because namespaces have multiple independent definitions,
2714 namespace visibility is handled elsewhere using the #pragma visibility
2715 machinery rather than by decorating the namespace declaration.
2717 The goal is for constraints from the type to give a diagnostic, and
2718 other constraints to be applied silently. */
2720 void
2721 determine_visibility (tree decl)
2723 /* Remember that all decls get VISIBILITY_DEFAULT when built. */
2725 /* Only relevant for names with external linkage. */
2726 if (!TREE_PUBLIC (decl))
2727 return;
2729 /* Cloned constructors and destructors get the same visibility as
2730 the underlying function. That should be set up in
2731 maybe_clone_body. */
2732 gcc_assert (!DECL_CLONED_FUNCTION_P (decl));
2734 bool orig_visibility_specified = DECL_VISIBILITY_SPECIFIED (decl);
2735 enum symbol_visibility orig_visibility = DECL_VISIBILITY (decl);
2737 /* The decl may be a template instantiation, which could influence
2738 visibilty. */
2739 tree template_decl = NULL_TREE;
2740 if (TREE_CODE (decl) == TYPE_DECL)
2742 if (CLASS_TYPE_P (TREE_TYPE (decl)))
2744 if (CLASSTYPE_USE_TEMPLATE (TREE_TYPE (decl)))
2745 template_decl = decl;
2747 else if (TYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
2748 template_decl = decl;
2750 else if (DECL_LANG_SPECIFIC (decl) && DECL_USE_TEMPLATE (decl))
2751 template_decl = decl;
2753 if (TREE_CODE (decl) == TYPE_DECL
2754 && LAMBDA_TYPE_P (TREE_TYPE (decl))
2755 && CLASSTYPE_LAMBDA_EXPR (TREE_TYPE (decl)) != error_mark_node)
2756 if (tree extra = LAMBDA_TYPE_EXTRA_SCOPE (TREE_TYPE (decl)))
2758 /* The lambda's visibility is limited by that of its extra
2759 scope. */
2760 int vis = 0;
2761 if (TYPE_P (extra))
2762 vis = type_visibility (extra);
2763 else
2764 vis = expr_visibility (extra);
2765 constrain_visibility (decl, vis, false);
2768 /* If DECL is a member of a class, visibility specifiers on the
2769 class can influence the visibility of the DECL. */
2770 tree class_type = NULL_TREE;
2771 if (DECL_CLASS_SCOPE_P (decl))
2772 class_type = DECL_CONTEXT (decl);
2773 else
2775 /* Not a class member. */
2777 /* Virtual tables have DECL_CONTEXT set to their associated class,
2778 so they are automatically handled above. */
2779 gcc_assert (!VAR_P (decl)
2780 || !DECL_VTABLE_OR_VTT_P (decl));
2782 if (DECL_FUNCTION_SCOPE_P (decl) && ! DECL_VISIBILITY_SPECIFIED (decl))
2784 /* Local statics and classes get the visibility of their
2785 containing function by default, except that
2786 -fvisibility-inlines-hidden doesn't affect them. */
2787 tree fn = DECL_CONTEXT (decl);
2788 if (DECL_VISIBILITY_SPECIFIED (fn))
2790 DECL_VISIBILITY (decl) = DECL_VISIBILITY (fn);
2791 DECL_VISIBILITY_SPECIFIED (decl) =
2792 DECL_VISIBILITY_SPECIFIED (fn);
2794 else
2796 if (DECL_CLASS_SCOPE_P (fn))
2797 determine_visibility_from_class (decl, DECL_CONTEXT (fn));
2798 else if (determine_hidden_inline (fn))
2800 DECL_VISIBILITY (decl) = default_visibility;
2801 DECL_VISIBILITY_SPECIFIED (decl) =
2802 visibility_options.inpragma;
2804 else
2806 DECL_VISIBILITY (decl) = DECL_VISIBILITY (fn);
2807 DECL_VISIBILITY_SPECIFIED (decl) =
2808 DECL_VISIBILITY_SPECIFIED (fn);
2812 /* Local classes in templates have CLASSTYPE_USE_TEMPLATE set,
2813 but have no TEMPLATE_INFO, so don't try to check it. */
2814 template_decl = NULL_TREE;
2816 else if (VAR_P (decl) && DECL_TINFO_P (decl)
2817 && flag_visibility_ms_compat)
2819 /* Under -fvisibility-ms-compat, types are visible by default,
2820 even though their contents aren't. */
2821 tree underlying_type = TREE_TYPE (DECL_NAME (decl));
2822 int underlying_vis = type_visibility (underlying_type);
2823 if (underlying_vis == VISIBILITY_ANON
2824 || (CLASS_TYPE_P (underlying_type)
2825 && CLASSTYPE_VISIBILITY_SPECIFIED (underlying_type)))
2826 constrain_visibility (decl, underlying_vis, false);
2827 else
2828 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
2830 else if (VAR_P (decl) && DECL_TINFO_P (decl))
2832 /* tinfo visibility is based on the type it's for. */
2833 constrain_visibility
2834 (decl, type_visibility (TREE_TYPE (DECL_NAME (decl))), false);
2836 /* Give the target a chance to override the visibility associated
2837 with DECL. */
2838 if (TREE_PUBLIC (decl)
2839 && !DECL_REALLY_EXTERN (decl)
2840 && CLASS_TYPE_P (TREE_TYPE (DECL_NAME (decl)))
2841 && !CLASSTYPE_VISIBILITY_SPECIFIED (TREE_TYPE (DECL_NAME (decl))))
2842 targetm.cxx.determine_class_data_visibility (decl);
2844 else if (template_decl)
2845 /* Template instantiations and specializations get visibility based
2846 on their template unless they override it with an attribute. */;
2847 else if (! DECL_VISIBILITY_SPECIFIED (decl))
2849 if (determine_hidden_inline (decl))
2850 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
2851 else
2853 /* Set default visibility to whatever the user supplied with
2854 #pragma GCC visibility or a namespace visibility attribute. */
2855 DECL_VISIBILITY (decl) = default_visibility;
2856 DECL_VISIBILITY_SPECIFIED (decl) = visibility_options.inpragma;
2861 if (template_decl)
2863 /* If the specialization doesn't specify visibility, use the
2864 visibility from the template. */
2865 tree tinfo = get_template_info (template_decl);
2866 tree args = TI_ARGS (tinfo);
2867 tree attribs = (TREE_CODE (decl) == TYPE_DECL
2868 ? TYPE_ATTRIBUTES (TREE_TYPE (decl))
2869 : DECL_ATTRIBUTES (decl));
2870 tree attr = lookup_attribute ("visibility", attribs);
2872 if (args != error_mark_node)
2874 tree pattern = DECL_TEMPLATE_RESULT (TI_TEMPLATE (tinfo));
2876 if (!DECL_VISIBILITY_SPECIFIED (decl))
2878 if (!attr
2879 && determine_hidden_inline (decl))
2880 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
2881 else
2883 DECL_VISIBILITY (decl) = DECL_VISIBILITY (pattern);
2884 DECL_VISIBILITY_SPECIFIED (decl)
2885 = DECL_VISIBILITY_SPECIFIED (pattern);
2889 if (args
2890 /* Template argument visibility outweighs #pragma or namespace
2891 visibility, but not an explicit attribute. */
2892 && !attr)
2894 int depth = TMPL_ARGS_DEPTH (args);
2895 if (DECL_VISIBILITY_SPECIFIED (decl))
2897 /* A class template member with explicit visibility
2898 overrides the class visibility, so we need to apply
2899 all the levels of template args directly. */
2900 int i;
2901 for (i = 1; i <= depth; ++i)
2903 tree lev = TMPL_ARGS_LEVEL (args, i);
2904 constrain_visibility_for_template (decl, lev);
2907 else if (PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo)))
2908 /* Limit visibility based on its template arguments. */
2909 constrain_visibility_for_template (decl, args);
2914 if (class_type)
2915 determine_visibility_from_class (decl, class_type);
2917 if (decl_internal_context_p (decl))
2918 /* Names in an anonymous namespace get internal linkage. */
2919 constrain_visibility (decl, VISIBILITY_ANON, false);
2920 else if (TREE_CODE (decl) != TYPE_DECL)
2922 /* Propagate anonymity from type to decl. */
2923 int tvis = type_visibility (TREE_TYPE (decl));
2924 if (tvis == VISIBILITY_ANON
2925 || ! DECL_VISIBILITY_SPECIFIED (decl))
2926 constrain_visibility (decl, tvis, false);
2928 else if (no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/true))
2929 /* DR 757: A type without linkage shall not be used as the type of a
2930 variable or function with linkage, unless
2931 o the variable or function has extern "C" linkage (7.5 [dcl.link]), or
2932 o the variable or function is not used (3.2 [basic.def.odr]) or is
2933 defined in the same translation unit.
2935 Since non-extern "C" decls need to be defined in the same
2936 translation unit, we can make the type internal. */
2937 constrain_visibility (decl, VISIBILITY_ANON, false);
2939 /* If visibility changed and DECL already has DECL_RTL, ensure
2940 symbol flags are updated. */
2941 if ((DECL_VISIBILITY (decl) != orig_visibility
2942 || DECL_VISIBILITY_SPECIFIED (decl) != orig_visibility_specified)
2943 && ((VAR_P (decl) && TREE_STATIC (decl))
2944 || TREE_CODE (decl) == FUNCTION_DECL)
2945 && DECL_RTL_SET_P (decl))
2946 make_decl_rtl (decl);
2949 /* By default, static data members and function members receive
2950 the visibility of their containing class. */
2952 static void
2953 determine_visibility_from_class (tree decl, tree class_type)
2955 if (DECL_VISIBILITY_SPECIFIED (decl))
2956 return;
2958 if (determine_hidden_inline (decl))
2959 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
2960 else
2962 /* Default to the class visibility. */
2963 DECL_VISIBILITY (decl) = CLASSTYPE_VISIBILITY (class_type);
2964 DECL_VISIBILITY_SPECIFIED (decl)
2965 = CLASSTYPE_VISIBILITY_SPECIFIED (class_type);
2968 /* Give the target a chance to override the visibility associated
2969 with DECL. */
2970 if (VAR_P (decl)
2971 && TREE_PUBLIC (decl)
2972 && (DECL_TINFO_P (decl) || DECL_VTABLE_OR_VTT_P (decl))
2973 && !DECL_REALLY_EXTERN (decl)
2974 && !CLASSTYPE_VISIBILITY_SPECIFIED (class_type))
2975 targetm.cxx.determine_class_data_visibility (decl);
2978 /* Returns true iff DECL is an inline that should get hidden visibility
2979 because of -fvisibility-inlines-hidden. */
2981 static bool
2982 determine_hidden_inline (tree decl)
2984 return (visibility_options.inlines_hidden
2985 /* Don't do this for inline templates; specializations might not be
2986 inline, and we don't want them to inherit the hidden
2987 visibility. We'll set it here for all inline instantiations. */
2988 && !processing_template_decl
2989 && TREE_CODE (decl) == FUNCTION_DECL
2990 && DECL_DECLARED_INLINE_P (decl)
2991 && (! DECL_LANG_SPECIFIC (decl)
2992 || ! DECL_EXPLICIT_INSTANTIATION (decl)));
2995 /* Constrain the visibility of a class TYPE based on the visibility of its
2996 field types. Warn if any fields require lesser visibility. */
2998 void
2999 constrain_class_visibility (tree type)
3001 tree binfo;
3002 tree t;
3003 int i;
3005 int vis = type_visibility (type);
3007 if (vis == VISIBILITY_ANON
3008 || DECL_IN_SYSTEM_HEADER (TYPE_MAIN_DECL (type)))
3009 return;
3011 /* Don't warn about visibility if the class has explicit visibility. */
3012 if (CLASSTYPE_VISIBILITY_SPECIFIED (type))
3013 vis = VISIBILITY_INTERNAL;
3015 for (t = TYPE_FIELDS (type); t; t = DECL_CHAIN (t))
3016 if (TREE_CODE (t) == FIELD_DECL && TREE_TYPE (t) != error_mark_node
3017 && !DECL_ARTIFICIAL (t))
3019 tree ftype = strip_pointer_or_array_types (TREE_TYPE (t));
3020 int subvis = type_visibility (ftype);
3022 if (subvis == VISIBILITY_ANON)
3024 if (!in_main_input_context())
3026 tree nlt = no_linkage_check (ftype, /*relaxed_p=*/false);
3027 if (nlt)
3029 if (same_type_p (TREE_TYPE (t), nlt))
3030 warning (OPT_Wsubobject_linkage, "\
3031 %qT has a field %q#D whose type has no linkage",
3032 type, t);
3033 else
3034 warning (OPT_Wsubobject_linkage, "\
3035 %qT has a field %qD whose type depends on the type %qT which has no linkage",
3036 type, t, nlt);
3038 else if (cxx_dialect > cxx98
3039 && !decl_anon_ns_mem_p (ftype))
3040 warning (OPT_Wsubobject_linkage, "\
3041 %qT has a field %q#D whose type has internal linkage",
3042 type, t);
3043 else // In C++98 this can only happen with unnamed namespaces.
3044 warning (OPT_Wsubobject_linkage, "\
3045 %qT has a field %q#D whose type uses the anonymous namespace",
3046 type, t);
3049 else if (MAYBE_CLASS_TYPE_P (ftype)
3050 && vis < VISIBILITY_HIDDEN
3051 && subvis >= VISIBILITY_HIDDEN)
3052 warning (OPT_Wattributes, "\
3053 %qT declared with greater visibility than the type of its field %qD",
3054 type, t);
3057 binfo = TYPE_BINFO (type);
3058 for (i = 0; BINFO_BASE_ITERATE (binfo, i, t); ++i)
3060 tree btype = BINFO_TYPE (t);
3061 int subvis = type_visibility (btype);
3063 if (subvis == VISIBILITY_ANON)
3065 if (!in_main_input_context())
3067 tree nlt = no_linkage_check (btype, /*relaxed_p=*/false);
3068 if (nlt)
3070 if (same_type_p (btype, nlt))
3071 warning (OPT_Wsubobject_linkage, "\
3072 %qT has a base %qT which has no linkage",
3073 type, btype);
3074 else
3075 warning (OPT_Wsubobject_linkage, "\
3076 %qT has a base %qT which depends on the type %qT which has no linkage",
3077 type, btype, nlt);
3079 else if (cxx_dialect > cxx98
3080 && !decl_anon_ns_mem_p (btype))
3081 warning (OPT_Wsubobject_linkage, "\
3082 %qT has a base %qT which has internal linkage",
3083 type, btype);
3084 else // In C++98 this can only happen with unnamed namespaces.
3085 warning (OPT_Wsubobject_linkage, "\
3086 %qT has a base %qT which uses the anonymous namespace",
3087 type, btype);
3090 else if (vis < VISIBILITY_HIDDEN
3091 && subvis >= VISIBILITY_HIDDEN)
3092 warning (OPT_Wattributes, "\
3093 %qT declared with greater visibility than its base %qT",
3094 type, TREE_TYPE (t));
3098 /* Functions for adjusting the visibility of a tagged type and its nested
3099 types and declarations when it gets a name for linkage purposes from a
3100 typedef. */
3101 // FIXME: It is now a DR for such a class type to contain anything
3102 // other than C. So at minium most of this can probably be deleted.
3104 /* First reset the visibility of all the types. */
3106 static void
3107 reset_type_linkage_1 (tree type)
3109 set_linkage_according_to_type (type, TYPE_MAIN_DECL (type));
3110 if (CLASS_TYPE_P (type))
3111 for (tree member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
3112 if (DECL_IMPLICIT_TYPEDEF_P (member))
3113 reset_type_linkage_1 (TREE_TYPE (member));
3116 /* Then reset the visibility of any static data members or member
3117 functions that use those types. */
3119 static void
3120 reset_decl_linkage (tree decl)
3122 if (TREE_PUBLIC (decl))
3123 return;
3124 if (DECL_CLONED_FUNCTION_P (decl))
3125 return;
3126 TREE_PUBLIC (decl) = true;
3127 DECL_INTERFACE_KNOWN (decl) = false;
3128 determine_visibility (decl);
3129 tentative_decl_linkage (decl);
3132 void
3133 reset_type_linkage (tree type)
3135 reset_type_linkage_1 (type);
3136 if (CLASS_TYPE_P (type))
3138 if (tree vt = CLASSTYPE_VTABLES (type))
3140 tree name = mangle_vtbl_for_type (type);
3141 DECL_NAME (vt) = name;
3142 SET_DECL_ASSEMBLER_NAME (vt, name);
3143 reset_decl_linkage (vt);
3145 if (!ANON_AGGR_TYPE_P (type))
3146 if (tree ti = CLASSTYPE_TYPEINFO_VAR (type))
3148 tree name = mangle_typeinfo_for_type (type);
3149 DECL_NAME (ti) = name;
3150 SET_DECL_ASSEMBLER_NAME (ti, name);
3151 TREE_TYPE (name) = type;
3152 reset_decl_linkage (ti);
3154 for (tree m = TYPE_FIELDS (type); m; m = DECL_CHAIN (m))
3156 tree mem = STRIP_TEMPLATE (m);
3157 if (TREE_CODE (mem) == VAR_DECL || TREE_CODE (mem) == FUNCTION_DECL)
3158 reset_decl_linkage (mem);
3159 else if (DECL_IMPLICIT_TYPEDEF_P (mem))
3160 reset_type_linkage (TREE_TYPE (mem));
3165 /* Set up our initial idea of what the linkage of DECL should be. */
3167 void
3168 tentative_decl_linkage (tree decl)
3170 if (DECL_INTERFACE_KNOWN (decl))
3171 /* We've already made a decision as to how this function will
3172 be handled. */;
3173 else if (vague_linkage_p (decl))
3175 if (TREE_CODE (decl) == FUNCTION_DECL
3176 && decl_defined_p (decl))
3178 DECL_EXTERNAL (decl) = 1;
3179 DECL_NOT_REALLY_EXTERN (decl) = 1;
3180 note_vague_linkage_fn (decl);
3181 /* A non-template inline function with external linkage will
3182 always be COMDAT. As we must eventually determine the
3183 linkage of all functions, and as that causes writes to
3184 the data mapped in from the PCH file, it's advantageous
3185 to mark the functions at this point. */
3186 if (DECL_DECLARED_INLINE_P (decl)
3187 && (!DECL_IMPLICIT_INSTANTIATION (decl)
3188 || DECL_DEFAULTED_FN (decl)))
3190 /* This function must have external linkage, as
3191 otherwise DECL_INTERFACE_KNOWN would have been
3192 set. */
3193 gcc_assert (TREE_PUBLIC (decl));
3194 comdat_linkage (decl);
3195 DECL_INTERFACE_KNOWN (decl) = 1;
3198 else if (VAR_P (decl))
3199 maybe_commonize_var (decl);
3203 /* DECL is a FUNCTION_DECL or VAR_DECL. If the object file linkage
3204 for DECL has not already been determined, do so now by setting
3205 DECL_EXTERNAL, DECL_COMDAT and other related flags. Until this
3206 function is called entities with vague linkage whose definitions
3207 are available must have TREE_PUBLIC set.
3209 If this function decides to place DECL in COMDAT, it will set
3210 appropriate flags -- but will not clear DECL_EXTERNAL. It is up to
3211 the caller to decide whether or not to clear DECL_EXTERNAL. Some
3212 callers defer that decision until it is clear that DECL is actually
3213 required. */
3215 void
3216 import_export_decl (tree decl)
3218 bool comdat_p;
3219 bool import_p;
3220 tree class_type = NULL_TREE;
3222 if (DECL_INTERFACE_KNOWN (decl))
3223 return;
3225 /* We cannot determine what linkage to give to an entity with vague
3226 linkage until the end of the file. For example, a virtual table
3227 for a class will be defined if and only if the key method is
3228 defined in this translation unit. */
3229 gcc_assert (at_eof);
3230 /* Object file linkage for explicit instantiations is handled in
3231 mark_decl_instantiated. For static variables in functions with
3232 vague linkage, maybe_commonize_var is used.
3234 Therefore, the only declarations that should be provided to this
3235 function are those with external linkage that are:
3237 * implicit instantiations of function templates
3239 * inline function
3241 * implicit instantiations of static data members of class
3242 templates
3244 * virtual tables
3246 * typeinfo objects
3248 Furthermore, all entities that reach this point must have a
3249 definition available in this translation unit.
3251 The following assertions check these conditions. */
3252 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
3253 /* Any code that creates entities with TREE_PUBLIC cleared should
3254 also set DECL_INTERFACE_KNOWN. */
3255 gcc_assert (TREE_PUBLIC (decl));
3256 if (TREE_CODE (decl) == FUNCTION_DECL)
3257 gcc_assert (DECL_IMPLICIT_INSTANTIATION (decl)
3258 || DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl)
3259 || DECL_DECLARED_INLINE_P (decl));
3260 else
3261 gcc_assert (DECL_IMPLICIT_INSTANTIATION (decl)
3262 || DECL_VTABLE_OR_VTT_P (decl)
3263 || DECL_TINFO_P (decl));
3264 /* Check that a definition of DECL is available in this translation
3265 unit. */
3266 gcc_assert (!DECL_REALLY_EXTERN (decl));
3268 /* Assume that DECL will not have COMDAT linkage. */
3269 comdat_p = false;
3270 /* Assume that DECL will not be imported into this translation
3271 unit. */
3272 import_p = false;
3274 if (VAR_P (decl) && DECL_VTABLE_OR_VTT_P (decl))
3276 class_type = DECL_CONTEXT (decl);
3277 import_export_class (class_type);
3278 if (CLASSTYPE_INTERFACE_KNOWN (class_type)
3279 && CLASSTYPE_INTERFACE_ONLY (class_type))
3280 import_p = true;
3281 else if ((!flag_weak || TARGET_WEAK_NOT_IN_ARCHIVE_TOC)
3282 && !CLASSTYPE_USE_TEMPLATE (class_type)
3283 && CLASSTYPE_KEY_METHOD (class_type)
3284 && !DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (class_type)))
3285 /* The ABI requires that all virtual tables be emitted with
3286 COMDAT linkage. However, on systems where COMDAT symbols
3287 don't show up in the table of contents for a static
3288 archive, or on systems without weak symbols (where we
3289 approximate COMDAT linkage by using internal linkage), the
3290 linker will report errors about undefined symbols because
3291 it will not see the virtual table definition. Therefore,
3292 in the case that we know that the virtual table will be
3293 emitted in only one translation unit, we make the virtual
3294 table an ordinary definition with external linkage. */
3295 DECL_EXTERNAL (decl) = 0;
3296 else if (CLASSTYPE_INTERFACE_KNOWN (class_type))
3298 /* CLASS_TYPE is being exported from this translation unit,
3299 so DECL should be defined here. */
3300 if (!flag_weak && CLASSTYPE_EXPLICIT_INSTANTIATION (class_type))
3301 /* If a class is declared in a header with the "extern
3302 template" extension, then it will not be instantiated,
3303 even in translation units that would normally require
3304 it. Often such classes are explicitly instantiated in
3305 one translation unit. Therefore, the explicit
3306 instantiation must be made visible to other translation
3307 units. */
3308 DECL_EXTERNAL (decl) = 0;
3309 else
3311 /* The generic C++ ABI says that class data is always
3312 COMDAT, even if there is a key function. Some
3313 variants (e.g., the ARM EABI) says that class data
3314 only has COMDAT linkage if the class data might be
3315 emitted in more than one translation unit. When the
3316 key method can be inline and is inline, we still have
3317 to arrange for comdat even though
3318 class_data_always_comdat is false. */
3319 if (!CLASSTYPE_KEY_METHOD (class_type)
3320 || DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (class_type))
3321 || targetm.cxx.class_data_always_comdat ())
3323 /* The ABI requires COMDAT linkage. Normally, we
3324 only emit COMDAT things when they are needed;
3325 make sure that we realize that this entity is
3326 indeed needed. */
3327 comdat_p = true;
3328 mark_needed (decl);
3332 else if (!flag_implicit_templates
3333 && CLASSTYPE_IMPLICIT_INSTANTIATION (class_type))
3334 import_p = true;
3335 else
3336 comdat_p = true;
3338 else if (VAR_P (decl) && DECL_TINFO_P (decl))
3340 tree type = TREE_TYPE (DECL_NAME (decl));
3341 if (CLASS_TYPE_P (type))
3343 class_type = type;
3344 import_export_class (type);
3345 if (CLASSTYPE_INTERFACE_KNOWN (type)
3346 && TYPE_POLYMORPHIC_P (type)
3347 && CLASSTYPE_INTERFACE_ONLY (type)
3348 /* If -fno-rtti was specified, then we cannot be sure
3349 that RTTI information will be emitted with the
3350 virtual table of the class, so we must emit it
3351 wherever it is used. */
3352 && flag_rtti)
3353 import_p = true;
3354 else
3356 if (CLASSTYPE_INTERFACE_KNOWN (type)
3357 && !CLASSTYPE_INTERFACE_ONLY (type))
3359 comdat_p = (targetm.cxx.class_data_always_comdat ()
3360 || (CLASSTYPE_KEY_METHOD (type)
3361 && DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (type))));
3362 mark_needed (decl);
3363 if (!flag_weak)
3365 comdat_p = false;
3366 DECL_EXTERNAL (decl) = 0;
3369 else
3370 comdat_p = true;
3373 else
3374 comdat_p = true;
3376 else if (DECL_TEMPLOID_INSTANTIATION (decl))
3378 /* DECL is an implicit instantiation of a function or static
3379 data member. */
3380 if (flag_implicit_templates
3381 || (flag_implicit_inline_templates
3382 && TREE_CODE (decl) == FUNCTION_DECL
3383 && DECL_DECLARED_INLINE_P (decl)))
3384 comdat_p = true;
3385 else
3386 /* If we are not implicitly generating templates, then mark
3387 this entity as undefined in this translation unit. */
3388 import_p = true;
3390 else if (DECL_FUNCTION_MEMBER_P (decl))
3392 if (!DECL_DECLARED_INLINE_P (decl))
3394 tree ctype = DECL_CONTEXT (decl);
3395 import_export_class (ctype);
3396 if (CLASSTYPE_INTERFACE_KNOWN (ctype))
3398 DECL_NOT_REALLY_EXTERN (decl)
3399 = ! (CLASSTYPE_INTERFACE_ONLY (ctype)
3400 || (DECL_DECLARED_INLINE_P (decl)
3401 && ! flag_implement_inlines
3402 && !DECL_VINDEX (decl)));
3404 if (!DECL_NOT_REALLY_EXTERN (decl))
3405 DECL_EXTERNAL (decl) = 1;
3407 /* Always make artificials weak. */
3408 if (DECL_ARTIFICIAL (decl) && flag_weak)
3409 comdat_p = true;
3410 else
3411 maybe_make_one_only (decl);
3414 else
3415 comdat_p = true;
3417 else
3418 comdat_p = true;
3420 if (import_p)
3422 /* If we are importing DECL into this translation unit, mark is
3423 an undefined here. */
3424 DECL_EXTERNAL (decl) = 1;
3425 DECL_NOT_REALLY_EXTERN (decl) = 0;
3427 else if (comdat_p)
3429 /* If we decided to put DECL in COMDAT, mark it accordingly at
3430 this point. */
3431 comdat_linkage (decl);
3434 DECL_INTERFACE_KNOWN (decl) = 1;
3437 /* Return an expression that performs the destruction of DECL, which
3438 must be a VAR_DECL whose type has a non-trivial destructor, or is
3439 an array whose (innermost) elements have a non-trivial destructor. */
3441 tree
3442 build_cleanup (tree decl)
3444 tree clean = cxx_maybe_build_cleanup (decl, tf_warning_or_error);
3445 gcc_assert (clean != NULL_TREE);
3446 return clean;
3449 /* GUARD is a helper variable for DECL; make them have the same linkage and
3450 visibility. */
3452 void
3453 copy_linkage (tree guard, tree decl)
3455 TREE_PUBLIC (guard) = TREE_PUBLIC (decl);
3456 TREE_STATIC (guard) = TREE_STATIC (decl);
3457 DECL_COMMON (guard) = DECL_COMMON (decl);
3458 DECL_COMDAT (guard) = DECL_COMDAT (decl);
3459 if (TREE_STATIC (guard))
3461 CP_DECL_THREAD_LOCAL_P (guard) = CP_DECL_THREAD_LOCAL_P (decl);
3462 set_decl_tls_model (guard, DECL_TLS_MODEL (decl));
3463 if (DECL_ONE_ONLY (decl))
3464 make_decl_one_only (guard, cxx_comdat_group (guard));
3465 if (TREE_PUBLIC (decl))
3466 DECL_WEAK (guard) = DECL_WEAK (decl);
3467 /* Also check vague_linkage_p, as DECL_WEAK and DECL_ONE_ONLY might not
3468 be set until import_export_decl at EOF. */
3469 if (vague_linkage_p (decl))
3470 comdat_linkage (guard);
3471 DECL_VISIBILITY (guard) = DECL_VISIBILITY (decl);
3472 DECL_VISIBILITY_SPECIFIED (guard) = DECL_VISIBILITY_SPECIFIED (decl);
3476 /* Returns the initialization guard variable for the variable DECL,
3477 which has static storage duration. */
3479 tree
3480 get_guard (tree decl)
3482 tree sname = mangle_guard_variable (decl);
3483 tree guard = get_global_binding (sname);
3484 if (! guard)
3486 tree guard_type;
3488 /* We use a type that is big enough to contain a mutex as well
3489 as an integer counter. */
3490 guard_type = targetm.cxx.guard_type ();
3491 guard = build_decl (DECL_SOURCE_LOCATION (decl),
3492 VAR_DECL, sname, guard_type);
3494 /* The guard should have the same linkage as what it guards. */
3495 copy_linkage (guard, decl);
3497 DECL_ARTIFICIAL (guard) = 1;
3498 DECL_IGNORED_P (guard) = 1;
3499 TREE_USED (guard) = 1;
3500 pushdecl_top_level_and_finish (guard, NULL_TREE);
3502 return guard;
3505 /* Returns true if accessing the GUARD atomic is expensive,
3506 i.e. involves a call to __sync_synchronize or similar.
3507 In this case let __cxa_guard_acquire handle the atomics. */
3509 static bool
3510 is_atomic_expensive_p (machine_mode mode)
3512 if (!flag_inline_atomics)
3513 return true;
3515 if (!can_compare_and_swap_p (mode, false) || !can_atomic_load_p (mode))
3516 return true;
3518 return false;
3521 /* Return an atomic load of src with the appropriate memory model. */
3523 static tree
3524 build_atomic_load_type (tree src, HOST_WIDE_INT model, tree type)
3526 tree ptr_type = build_pointer_type (type);
3527 tree mem_model = build_int_cst (integer_type_node, model);
3528 tree t, addr, val;
3529 unsigned int size;
3530 int fncode;
3532 size = tree_to_uhwi (TYPE_SIZE_UNIT (type));
3534 fncode = BUILT_IN_ATOMIC_LOAD_N + exact_log2 (size) + 1;
3535 t = builtin_decl_implicit ((enum built_in_function) fncode);
3537 addr = build1 (ADDR_EXPR, ptr_type, src);
3538 val = build_call_expr (t, 2, addr, mem_model);
3539 return val;
3542 /* Return those bits of the GUARD variable that should be set when the
3543 guarded entity is actually initialized. */
3545 static tree
3546 get_guard_bits (tree guard)
3548 if (!targetm.cxx.guard_mask_bit ())
3550 /* We only set the first byte of the guard, in order to leave room
3551 for a mutex in the high-order bits. */
3552 guard = build1 (ADDR_EXPR,
3553 build_pointer_type (TREE_TYPE (guard)),
3554 guard);
3555 guard = build1 (NOP_EXPR,
3556 build_pointer_type (char_type_node),
3557 guard);
3558 guard = build1 (INDIRECT_REF, char_type_node, guard);
3561 return guard;
3564 /* Return an expression which determines whether or not the GUARD
3565 variable has already been initialized. */
3567 tree
3568 get_guard_cond (tree guard, bool thread_safe)
3570 tree guard_value;
3572 if (!thread_safe)
3573 guard = get_guard_bits (guard);
3574 else
3576 tree type = targetm.cxx.guard_mask_bit ()
3577 ? TREE_TYPE (guard) : char_type_node;
3579 if (is_atomic_expensive_p (TYPE_MODE (type)))
3580 guard = integer_zero_node;
3581 else
3582 guard = build_atomic_load_type (guard, MEMMODEL_ACQUIRE, type);
3585 /* Mask off all but the low bit. */
3586 if (targetm.cxx.guard_mask_bit ())
3588 guard_value = integer_one_node;
3589 if (!same_type_p (TREE_TYPE (guard_value), TREE_TYPE (guard)))
3590 guard_value = fold_convert (TREE_TYPE (guard), guard_value);
3591 guard = cp_build_binary_op (input_location,
3592 BIT_AND_EXPR, guard, guard_value,
3593 tf_warning_or_error);
3596 guard_value = integer_zero_node;
3597 if (!same_type_p (TREE_TYPE (guard_value), TREE_TYPE (guard)))
3598 guard_value = fold_convert (TREE_TYPE (guard), guard_value);
3599 return cp_build_binary_op (input_location,
3600 EQ_EXPR, guard, guard_value,
3601 tf_warning_or_error);
3604 /* Return an expression which sets the GUARD variable, indicating that
3605 the variable being guarded has been initialized. */
3607 tree
3608 set_guard (tree guard)
3610 tree guard_init;
3612 /* Set the GUARD to one. */
3613 guard = get_guard_bits (guard);
3614 guard_init = integer_one_node;
3615 if (!same_type_p (TREE_TYPE (guard_init), TREE_TYPE (guard)))
3616 guard_init = fold_convert (TREE_TYPE (guard), guard_init);
3617 return cp_build_modify_expr (input_location, guard, NOP_EXPR, guard_init,
3618 tf_warning_or_error);
3621 /* Returns true iff we can tell that VAR does not have a dynamic
3622 initializer. */
3624 static bool
3625 var_defined_without_dynamic_init (tree var)
3627 /* constinit vars are guaranteed to not have dynamic initializer,
3628 but still registering the destructor counts as dynamic initialization. */
3629 if (DECL_DECLARED_CONSTINIT_P (var)
3630 && COMPLETE_TYPE_P (TREE_TYPE (var))
3631 && !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (var)))
3632 return true;
3633 /* If it's defined in another TU, we can't tell. */
3634 if (DECL_EXTERNAL (var))
3635 return false;
3636 /* If it has a non-trivial destructor, registering the destructor
3637 counts as dynamic initialization. */
3638 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (var)))
3639 return false;
3640 /* If it's in this TU, its initializer has been processed, unless
3641 it's a case of self-initialization, then DECL_INITIALIZED_P is
3642 false while the initializer is handled by finish_id_expression. */
3643 if (!DECL_INITIALIZED_P (var))
3644 return false;
3645 /* If it has no initializer or a constant one, it's not dynamic. */
3646 return (!DECL_NONTRIVIALLY_INITIALIZED_P (var)
3647 || DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (var));
3650 /* Returns true iff VAR is a variable that needs uses to be
3651 wrapped for possible dynamic initialization. */
3653 bool
3654 var_needs_tls_wrapper (tree var)
3656 return (!error_operand_p (var)
3657 && CP_DECL_THREAD_LOCAL_P (var)
3658 && !DECL_GNU_TLS_P (var)
3659 && !DECL_FUNCTION_SCOPE_P (var)
3660 && !var_defined_without_dynamic_init (var));
3663 /* Get the FUNCTION_DECL for the shared TLS init function for this
3664 translation unit. */
3666 static tree
3667 get_local_tls_init_fn (location_t loc)
3669 tree sname = get_identifier ("__tls_init");
3670 tree fn = get_global_binding (sname);
3671 if (!fn)
3673 fn = build_lang_decl_loc (loc, FUNCTION_DECL, sname,
3674 build_function_type (void_type_node,
3675 void_list_node));
3676 SET_DECL_LANGUAGE (fn, lang_c);
3677 TREE_PUBLIC (fn) = false;
3678 DECL_ARTIFICIAL (fn) = true;
3679 mark_used (fn);
3680 set_global_binding (fn);
3682 return fn;
3685 /* Get a FUNCTION_DECL for the init function for the thread_local
3686 variable VAR. The init function will be an alias to the function
3687 that initializes all the non-local TLS variables in the translation
3688 unit. The init function is only used by the wrapper function. */
3690 static tree
3691 get_tls_init_fn (tree var)
3693 /* Only C++11 TLS vars need this init fn. */
3694 if (!var_needs_tls_wrapper (var))
3695 return NULL_TREE;
3697 /* If -fno-extern-tls-init, assume that we don't need to call
3698 a tls init function for a variable defined in another TU. */
3699 if (!flag_extern_tls_init && DECL_EXTERNAL (var))
3700 return NULL_TREE;
3702 /* If the variable is internal, or if we can't generate aliases,
3703 call the local init function directly. */
3704 if (!TREE_PUBLIC (var) || !TARGET_SUPPORTS_ALIASES)
3705 return get_local_tls_init_fn (DECL_SOURCE_LOCATION (var));
3707 tree sname = mangle_tls_init_fn (var);
3708 tree fn = get_global_binding (sname);
3709 if (!fn)
3711 fn = build_lang_decl (FUNCTION_DECL, sname,
3712 build_function_type (void_type_node,
3713 void_list_node));
3714 SET_DECL_LANGUAGE (fn, lang_c);
3715 TREE_PUBLIC (fn) = TREE_PUBLIC (var);
3716 DECL_ARTIFICIAL (fn) = true;
3717 DECL_COMDAT (fn) = DECL_COMDAT (var);
3718 DECL_EXTERNAL (fn) = DECL_EXTERNAL (var);
3719 if (DECL_ONE_ONLY (var))
3720 make_decl_one_only (fn, cxx_comdat_group (fn));
3721 if (TREE_PUBLIC (var))
3723 tree obtype = strip_array_types (non_reference (TREE_TYPE (var)));
3724 /* If the variable is defined somewhere else and might have static
3725 initialization, make the init function a weak reference. */
3726 if ((!TYPE_NEEDS_CONSTRUCTING (obtype)
3727 || TYPE_HAS_CONSTEXPR_CTOR (obtype)
3728 || TYPE_HAS_TRIVIAL_DFLT (obtype))
3729 && TYPE_HAS_TRIVIAL_DESTRUCTOR (obtype)
3730 && DECL_EXTERNAL (var))
3731 declare_weak (fn);
3732 else
3733 DECL_WEAK (fn) = DECL_WEAK (var);
3735 DECL_VISIBILITY (fn) = DECL_VISIBILITY (var);
3736 DECL_VISIBILITY_SPECIFIED (fn) = DECL_VISIBILITY_SPECIFIED (var);
3737 DECL_DLLIMPORT_P (fn) = DECL_DLLIMPORT_P (var);
3738 DECL_IGNORED_P (fn) = 1;
3739 mark_used (fn);
3741 DECL_BEFRIENDING_CLASSES (fn) = var;
3743 set_global_binding (fn);
3745 return fn;
3748 /* Get a FUNCTION_DECL for the init wrapper function for the thread_local
3749 variable VAR. The wrapper function calls the init function (if any) for
3750 VAR and then returns a reference to VAR. The wrapper function is used
3751 in place of VAR everywhere VAR is mentioned. */
3753 static tree
3754 get_tls_wrapper_fn (tree var)
3756 /* Only C++11 TLS vars need this wrapper fn. */
3757 if (!var_needs_tls_wrapper (var))
3758 return NULL_TREE;
3760 tree sname = mangle_tls_wrapper_fn (var);
3761 tree fn = get_global_binding (sname);
3762 if (!fn)
3764 /* A named rvalue reference is an lvalue, so the wrapper should
3765 always return an lvalue reference. */
3766 tree type = non_reference (TREE_TYPE (var));
3767 type = build_reference_type (type);
3768 tree fntype = build_function_type (type, void_list_node);
3770 fn = build_lang_decl_loc (DECL_SOURCE_LOCATION (var),
3771 FUNCTION_DECL, sname, fntype);
3772 SET_DECL_LANGUAGE (fn, lang_c);
3773 TREE_PUBLIC (fn) = TREE_PUBLIC (var);
3774 DECL_ARTIFICIAL (fn) = true;
3775 DECL_IGNORED_P (fn) = 1;
3776 /* The wrapper is inline and emitted everywhere var is used. */
3777 DECL_DECLARED_INLINE_P (fn) = true;
3778 if (TREE_PUBLIC (var))
3780 comdat_linkage (fn);
3781 #ifdef HAVE_GAS_HIDDEN
3782 /* Make the wrapper bind locally; there's no reason to share
3783 the wrapper between multiple shared objects. */
3784 DECL_VISIBILITY (fn) = VISIBILITY_INTERNAL;
3785 DECL_VISIBILITY_SPECIFIED (fn) = true;
3786 #endif
3788 if (!TREE_PUBLIC (fn))
3789 DECL_INTERFACE_KNOWN (fn) = true;
3790 mark_used (fn);
3791 note_vague_linkage_fn (fn);
3793 #if 0
3794 /* We want CSE to commonize calls to the wrapper, but marking it as
3795 pure is unsafe since it has side-effects. I guess we need a new
3796 ECF flag even weaker than ECF_PURE. FIXME! */
3797 DECL_PURE_P (fn) = true;
3798 #endif
3800 DECL_BEFRIENDING_CLASSES (fn) = var;
3802 set_global_binding (fn);
3804 return fn;
3807 /* If EXPR is a thread_local variable that should be wrapped by init
3808 wrapper function, return a call to that function, otherwise return
3809 NULL. */
3811 tree
3812 maybe_get_tls_wrapper_call (tree expr)
3814 if (VAR_P (expr)
3815 && !processing_template_decl
3816 && !cp_unevaluated_operand
3817 && CP_DECL_THREAD_LOCAL_P (expr))
3818 if (tree wrap = get_tls_wrapper_fn (expr))
3819 return build_cxx_call (wrap, 0, NULL, tf_warning_or_error);
3820 return NULL;
3823 /* At EOF, generate the definition for the TLS wrapper function FN:
3825 T& var_wrapper() {
3826 if (init_fn) init_fn();
3827 return var;
3828 } */
3830 static void
3831 generate_tls_wrapper (tree fn)
3833 tree var = DECL_BEFRIENDING_CLASSES (fn);
3835 start_preparsed_function (fn, NULL_TREE, SF_DEFAULT | SF_PRE_PARSED);
3836 tree body = begin_function_body ();
3837 /* Only call the init fn if there might be one. */
3838 if (tree init_fn = get_tls_init_fn (var))
3840 tree if_stmt = NULL_TREE;
3841 /* If init_fn is a weakref, make sure it exists before calling. */
3842 if (lookup_attribute ("weak", DECL_ATTRIBUTES (init_fn)))
3844 if_stmt = begin_if_stmt ();
3845 tree addr = cp_build_addr_expr (init_fn, tf_warning_or_error);
3846 tree cond = cp_build_binary_op (DECL_SOURCE_LOCATION (var),
3847 NE_EXPR, addr, nullptr_node,
3848 tf_warning_or_error);
3849 finish_if_stmt_cond (cond, if_stmt);
3851 finish_expr_stmt (build_cxx_call
3852 (init_fn, 0, NULL, tf_warning_or_error));
3853 if (if_stmt)
3855 finish_then_clause (if_stmt);
3856 finish_if_stmt (if_stmt);
3859 else
3860 /* If there's no initialization, the wrapper is a constant function. */
3861 TREE_READONLY (fn) = true;
3862 finish_return_stmt (convert_from_reference (var));
3863 finish_function_body (body);
3864 expand_or_defer_fn (finish_function (/*inline_p=*/false));
3867 /* Start a global constructor or destructor function. */
3869 static tree
3870 start_objects (bool initp, unsigned priority, bool has_body)
3872 bool default_init = initp && priority == DEFAULT_INIT_PRIORITY;
3873 bool is_module_init = default_init && module_global_init_needed ();
3874 tree name = NULL_TREE;
3876 if (is_module_init)
3877 name = mangle_module_global_init (0);
3878 else
3880 char type[14];
3882 /* We use `I' to indicate initialization and `D' to indicate
3883 destruction. */
3884 unsigned len = sprintf (type, "sub_%c", initp ? 'I' : 'D');
3885 if (priority != DEFAULT_INIT_PRIORITY)
3887 char joiner = '_';
3888 #ifdef JOINER
3889 joiner = JOINER;
3890 #endif
3891 type[len++] = joiner;
3892 sprintf (type + len, "%.5u", priority);
3894 name = get_file_function_name (type);
3897 tree fntype = build_function_type (void_type_node, void_list_node);
3898 tree fndecl = build_lang_decl (FUNCTION_DECL, name, fntype);
3899 DECL_CONTEXT (fndecl) = FROB_CONTEXT (global_namespace);
3900 if (is_module_init)
3902 SET_DECL_ASSEMBLER_NAME (fndecl, name);
3903 TREE_PUBLIC (fndecl) = true;
3904 determine_visibility (fndecl);
3906 else
3907 TREE_PUBLIC (fndecl) = 0;
3908 start_preparsed_function (fndecl, /*attrs=*/NULL_TREE, SF_PRE_PARSED);
3910 /* Mark as artificial because it's not explicitly in the user's
3911 source code. */
3912 DECL_ARTIFICIAL (current_function_decl) = 1;
3914 /* Mark this declaration as used to avoid spurious warnings. */
3915 TREE_USED (current_function_decl) = 1;
3917 /* Mark this function as a global constructor or destructor. */
3918 if (initp)
3919 DECL_GLOBAL_CTOR_P (current_function_decl) = 1;
3920 else
3921 DECL_GLOBAL_DTOR_P (current_function_decl) = 1;
3923 tree body = begin_compound_stmt (BCS_FN_BODY);
3925 if (is_module_init && has_body)
3927 // If the function is going to be empty, don't emit idempotency.
3928 // 'static bool __in_chrg = false;
3929 // if (__inchrg) return;
3930 // __inchrg = true
3931 tree var = build_lang_decl (VAR_DECL, in_charge_identifier,
3932 boolean_type_node);
3933 DECL_CONTEXT (var) = fndecl;
3934 DECL_ARTIFICIAL (var) = true;
3935 TREE_STATIC (var) = true;
3936 pushdecl (var);
3937 cp_finish_decl (var, NULL_TREE, false, NULL_TREE, 0);
3939 tree if_stmt = begin_if_stmt ();
3940 finish_if_stmt_cond (var, if_stmt);
3941 finish_return_stmt (NULL_TREE);
3942 finish_then_clause (if_stmt);
3943 finish_if_stmt (if_stmt);
3945 tree assign = build2 (MODIFY_EXPR, boolean_type_node,
3946 var, boolean_true_node);
3947 TREE_SIDE_EFFECTS (assign) = true;
3948 finish_expr_stmt (assign);
3951 return body;
3954 /* Finish a global constructor or destructor. Add it to the global
3955 ctors or dtors, if STARTP is true. */
3957 static tree
3958 finish_objects (bool initp, unsigned priority, tree body, bool startp)
3960 /* Finish up. */
3961 finish_compound_stmt (body);
3962 tree fn = finish_function (/*inline_p=*/false);
3964 if (!startp)
3965 ; // Neither ctor nor dtor I be.
3966 else if (initp)
3968 DECL_STATIC_CONSTRUCTOR (fn) = 1;
3969 decl_init_priority_insert (fn, priority);
3971 else
3973 DECL_STATIC_DESTRUCTOR (fn) = 1;
3974 decl_fini_priority_insert (fn, priority);
3977 return fn;
3980 /* The name of the function we create to handle initializations and
3981 destructions for objects with static storage duration. */
3982 #define SSDF_IDENTIFIER "__static_initialization_and_destruction"
3984 /* Begins the generation of the function that will handle all
3985 initialization or destruction of objects with static storage
3986 duration at PRIORITY.
3988 It is assumed that this function will only be called once. */
3990 static tree
3991 start_partial_init_fini_fn (bool initp, unsigned priority, unsigned count)
3993 char id[sizeof (SSDF_IDENTIFIER) + 1 /* '\0' */ + 32];
3995 /* Create the identifier for this function. It will be of the form
3996 SSDF_IDENTIFIER_<number>. */
3997 sprintf (id, "%s_%u", SSDF_IDENTIFIER, count);
3999 tree type = build_function_type (void_type_node, void_list_node);
4001 /* Create the FUNCTION_DECL itself. */
4002 tree fn = build_lang_decl (FUNCTION_DECL, get_identifier (id), type);
4003 TREE_PUBLIC (fn) = 0;
4004 DECL_ARTIFICIAL (fn) = 1;
4006 /* Put this function in the list of functions to be called from the
4007 static constructors and destructors. */
4008 if (!static_init_fini_fns[initp])
4009 static_init_fini_fns[initp] = priority_map_t::create_ggc ();
4010 auto &slot = static_init_fini_fns[initp]->get_or_insert (priority);
4011 slot = tree_cons (fn, NULL_TREE, slot);
4013 /* Put the function in the global scope. */
4014 pushdecl (fn);
4016 /* Start the function itself. This is equivalent to declaring the
4017 function as:
4019 static void __ssdf (int __initialize_p, init __priority_p);
4021 It is static because we only need to call this function from the
4022 various constructor and destructor functions for this module. */
4023 start_preparsed_function (fn, /*attrs=*/NULL_TREE, SF_PRE_PARSED);
4025 /* Set up the scope of the outermost block in the function. */
4026 return begin_compound_stmt (BCS_FN_BODY);
4029 /* Finish the generation of the function which performs initialization
4030 or destruction of objects with static storage duration. */
4032 static void
4033 finish_partial_init_fini_fn (tree body)
4035 /* Close out the function. */
4036 finish_compound_stmt (body);
4037 expand_or_defer_fn (finish_function (/*inline_p=*/false));
4040 /* The effective initialization priority of a DECL. */
4042 #define DECL_EFFECTIVE_INIT_PRIORITY(decl) \
4043 ((!DECL_HAS_INIT_PRIORITY_P (decl) || DECL_INIT_PRIORITY (decl) == 0) \
4044 ? DEFAULT_INIT_PRIORITY : DECL_INIT_PRIORITY (decl))
4046 /* Whether a DECL needs a guard to protect it against multiple
4047 initialization. */
4049 #define NEEDS_GUARD_P(decl) (TREE_PUBLIC (decl) && (DECL_COMMON (decl) \
4050 || DECL_ONE_ONLY (decl) \
4051 || DECL_WEAK (decl)))
4053 /* Walks the initializer list of a global variable and looks for
4054 temporary variables (DECL_NAME() == NULL and DECL_ARTIFICIAL != 0)
4055 and that have their DECL_CONTEXT() == NULL. For each such
4056 temporary variable, set their DECL_CONTEXT() to CTX -- the
4057 initializing function. This is necessary because otherwise some
4058 optimizers (enabled by -O2 -fprofile-arcs) might crash when trying
4059 to refer to a temporary variable that does not have its
4060 DECL_CONTEXT() properly set. */
4062 static tree
4063 fix_temporary_vars_context_r (tree *node,
4064 int * /*unused*/,
4065 void *ctx)
4067 if (TREE_CODE (*node) == BIND_EXPR)
4068 for (tree var = BIND_EXPR_VARS (*node); var; var = DECL_CHAIN (var))
4069 if (VAR_P (var) && !DECL_NAME (var)
4070 && DECL_ARTIFICIAL (var) && !DECL_CONTEXT (var))
4071 DECL_CONTEXT (var) = tree (ctx);
4073 return NULL_TREE;
4076 /* Set up to handle the initialization or destruction of DECL. If
4077 INITP is nonzero, we are initializing the variable. Otherwise, we
4078 are destroying it. */
4080 static void
4081 one_static_initialization_or_destruction (bool initp, tree decl, tree init)
4083 /* If we are supposed to destruct and there's a trivial destructor,
4084 nothing has to be done. */
4085 gcc_checking_assert (init || !TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)));
4087 /* Trick the compiler into thinking we are at the file and line
4088 where DECL was declared so that error-messages make sense, and so
4089 that the debugger will show somewhat sensible file and line
4090 information. */
4091 input_location = DECL_SOURCE_LOCATION (decl);
4093 /* Make sure temporary variables in the initialiser all have
4094 their DECL_CONTEXT() set to a value different from NULL_TREE.
4095 This can happen when global variables initializers are built.
4096 In that case, the DECL_CONTEXT() of the global variables _AND_ of all
4097 the temporary variables that might have been generated in the
4098 accompanying initializers is NULL_TREE, meaning the variables have been
4099 declared in the global namespace.
4100 What we want to do here is to fix that and make sure the DECL_CONTEXT()
4101 of the temporaries are set to the current function decl. */
4102 cp_walk_tree_without_duplicates (&init,
4103 fix_temporary_vars_context_r,
4104 current_function_decl);
4106 /* Because of:
4108 [class.access.spec]
4110 Access control for implicit calls to the constructors,
4111 the conversion functions, or the destructor called to
4112 create and destroy a static data member is performed as
4113 if these calls appeared in the scope of the member's
4114 class.
4116 we pretend we are in a static member function of the class of
4117 which the DECL is a member. */
4118 if (member_p (decl))
4120 DECL_CONTEXT (current_function_decl) = DECL_CONTEXT (decl);
4121 DECL_STATIC_FUNCTION_P (current_function_decl) = 1;
4124 /* Assume we don't need a guard. */
4125 tree guard_if_stmt = NULL_TREE;
4127 /* We need a guard if this is an object with external linkage that
4128 might be initialized in more than one place. (For example, a
4129 static data member of a template, when the data member requires
4130 construction.) */
4131 if (NEEDS_GUARD_P (decl))
4133 tree guard = get_guard (decl);
4134 tree guard_cond;
4136 if (flag_use_cxa_atexit)
4138 /* When using __cxa_atexit, we just check the GUARD as we
4139 would for a local static. We never try to destroy
4140 anything from a static destructor. */
4141 gcc_assert (initp);
4142 guard_cond = get_guard_cond (guard, false);
4144 else
4146 /* If we don't have __cxa_atexit, then we will be running
4147 destructors from .fini sections, or their equivalents.
4148 So, we need to know how many times we've tried to
4149 initialize this object. We do initializations only if
4150 the GUARD was or becomes zero (initp vs !initp
4151 respectively). */
4152 guard_cond = cp_build_unary_op (initp ? POSTINCREMENT_EXPR
4153 : PREDECREMENT_EXPR,
4154 guard,
4155 /*noconvert=*/true,
4156 tf_warning_or_error);
4157 guard_cond = cp_build_binary_op (input_location, EQ_EXPR, guard_cond,
4158 integer_zero_node,
4159 tf_warning_or_error);
4162 guard_if_stmt = begin_if_stmt ();
4163 finish_if_stmt_cond (guard_cond, guard_if_stmt);
4165 if (flag_use_cxa_atexit)
4166 /* Set the GUARD now. */
4167 finish_expr_stmt (set_guard (guard));
4170 /* Perform the initialization or destruction. */
4171 if (initp)
4173 if (init)
4175 finish_expr_stmt (init);
4176 if (sanitize_flags_p (SANITIZE_ADDRESS, decl))
4177 if (varpool_node *vnode = varpool_node::get (decl))
4178 vnode->dynamically_initialized = 1;
4181 /* If we're using __cxa_atexit, register a function that calls the
4182 destructor for the object. */
4183 if (flag_use_cxa_atexit)
4184 finish_expr_stmt (register_dtor_fn (decl));
4186 else
4187 finish_expr_stmt (build_cleanup (decl));
4189 /* Finish the guard if-stmt, if necessary. */
4190 if (guard_if_stmt)
4192 finish_then_clause (guard_if_stmt);
4193 finish_if_stmt (guard_if_stmt);
4196 /* Now that we're done with DECL we don't need to pretend to be a
4197 member of its class any longer. */
4198 DECL_CONTEXT (current_function_decl) = NULL_TREE;
4199 DECL_STATIC_FUNCTION_P (current_function_decl) = 0;
4202 /* Generate code to do the initialization or destruction of the decls in VARS,
4203 a TREE_LIST of VAR_DECL with static storage duration.
4204 Whether initialization or destruction is performed is specified by INITP. */
4206 static void
4207 emit_partial_init_fini_fn (bool initp, unsigned priority, tree vars,
4208 unsigned counter, location_t locus)
4210 input_location = locus;
4211 tree body = start_partial_init_fini_fn (initp, priority, counter);
4213 for (tree node = vars; node; node = TREE_CHAIN (node))
4214 /* Do one initialization or destruction. */
4215 one_static_initialization_or_destruction (initp, TREE_VALUE (node),
4216 TREE_PURPOSE (node));
4218 /* Finish up the static storage duration function for this
4219 round. */
4220 input_location = locus;
4221 finish_partial_init_fini_fn (body);
4224 /* VARS is a list of variables with static storage duration which may
4225 need initialization and/or finalization. Remove those variables
4226 that don't really need to be initialized or finalized, and return
4227 the resulting list. The order in which the variables appear in
4228 VARS is in reverse order of the order in which they should actually
4229 be initialized. That order is preserved. */
4231 static tree
4232 prune_vars_needing_no_initialization (tree *vars)
4234 tree *var = vars;
4235 tree result = NULL_TREE;
4237 while (*var)
4239 tree t = *var;
4240 tree decl = TREE_VALUE (t);
4241 tree init = TREE_PURPOSE (t);
4243 /* Deal gracefully with error. */
4244 if (error_operand_p (decl))
4246 var = &TREE_CHAIN (t);
4247 continue;
4250 /* The only things that can be initialized are variables. */
4251 gcc_assert (VAR_P (decl));
4253 /* If this object is not defined, we don't need to do anything
4254 here. */
4255 if (DECL_EXTERNAL (decl))
4257 var = &TREE_CHAIN (t);
4258 continue;
4261 /* Also, if the initializer already contains errors, we can bail
4262 out now. */
4263 if (init && TREE_CODE (init) == TREE_LIST
4264 && value_member (error_mark_node, init))
4266 var = &TREE_CHAIN (t);
4267 continue;
4270 /* This variable is going to need initialization and/or
4271 finalization, so we add it to the list. */
4272 *var = TREE_CHAIN (t);
4273 TREE_CHAIN (t) = result;
4274 result = t;
4277 return result;
4280 /* Split VAR_LIST by init priority and add into PARTS hash table.
4281 This reverses the variable ordering. */
4283 void
4284 partition_vars_for_init_fini (tree var_list, priority_map_t *(&parts)[2])
4286 for (auto node = var_list; node; node = TREE_CHAIN (node))
4288 tree decl = TREE_VALUE (node);
4289 tree init = TREE_PURPOSE (node);
4290 bool has_cleanup = !TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl));
4291 unsigned priority = DECL_EFFECTIVE_INIT_PRIORITY (decl);
4293 if (init || (flag_use_cxa_atexit && has_cleanup))
4295 // Add to initialization list.
4296 if (!parts[true])
4297 parts[true] = priority_map_t::create_ggc ();
4298 auto &slot = parts[true]->get_or_insert (priority);
4299 slot = tree_cons (init, decl, slot);
4302 if (!flag_use_cxa_atexit && has_cleanup)
4304 // Add to finalization list.
4305 if (!parts[false])
4306 parts[false] = priority_map_t::create_ggc ();
4307 auto &slot = parts[false]->get_or_insert (priority);
4308 slot = tree_cons (NULL_TREE, decl, slot);
4313 /* Make sure we have told the back end about all the variables in
4314 VARS. */
4316 static void
4317 write_out_vars (tree vars)
4319 tree v;
4321 for (v = vars; v; v = TREE_CHAIN (v))
4323 tree var = TREE_VALUE (v);
4324 if (!var_finalized_p (var))
4326 import_export_decl (var);
4327 rest_of_decl_compilation (var, 1, 1);
4332 /* Generate a static constructor or destructor that calls the given
4333 init/fini fns at the indicated priority. */
4335 static void
4336 generate_ctor_or_dtor_function (bool initp, unsigned priority,
4337 tree fns, location_t locus)
4339 input_location = locus;
4340 tree body = start_objects (initp, priority, bool (fns));
4342 if (fns)
4344 /* To make sure dynamic construction doesn't access globals from
4345 other compilation units where they might not be yet
4346 constructed, for -fsanitize=address insert
4347 __asan_before_dynamic_init call that prevents access to
4348 either all global variables that need construction in other
4349 compilation units, or at least those that haven't been
4350 initialized yet. Variables that need dynamic construction in
4351 the current compilation unit are kept accessible. */
4352 if (initp && (flag_sanitize & SANITIZE_ADDRESS))
4353 finish_expr_stmt (asan_dynamic_init_call (/*after_p=*/false));
4355 /* Call the static init/fini functions. */
4356 for (tree node = fns; node; node = TREE_CHAIN (node))
4358 tree fn = TREE_PURPOSE (node);
4360 // We should never find a pure or constant cdtor.
4361 gcc_checking_assert (!(flags_from_decl_or_type (fn)
4362 & (ECF_CONST | ECF_PURE)));
4364 tree call = cp_build_function_call_nary (fn, tf_warning_or_error,
4365 NULL_TREE);
4366 finish_expr_stmt (call);
4369 /* Revert what __asan_before_dynamic_init did by calling
4370 __asan_after_dynamic_init. */
4371 if (initp && (flag_sanitize & SANITIZE_ADDRESS))
4372 finish_expr_stmt (asan_dynamic_init_call (/*after_p=*/true));
4375 /* Close out the function, and arrange for it to be called at init
4376 or fini time, if non-empty. (Even non-nop module initializer
4377 functions need this, as we cannot guarantee the module is
4378 imported somewhere in the program.) */
4379 expand_or_defer_fn (finish_objects (initp, priority, body, fns != NULL_TREE));
4382 /* Return C++ property of T, based on given operation OP. */
4384 static int
4385 cpp_check (tree t, cpp_operation op)
4387 switch (op)
4389 case HAS_DEPENDENT_TEMPLATE_ARGS:
4391 tree ti = CLASSTYPE_TEMPLATE_INFO (t);
4392 if (!ti)
4393 return 0;
4394 ++processing_template_decl;
4395 const bool dep = any_dependent_template_arguments_p (TI_ARGS (ti));
4396 --processing_template_decl;
4397 return dep;
4399 case IS_ABSTRACT:
4400 return DECL_PURE_VIRTUAL_P (t);
4401 case IS_ASSIGNMENT_OPERATOR:
4402 return DECL_ASSIGNMENT_OPERATOR_P (t);
4403 case IS_CONSTRUCTOR:
4404 return DECL_CONSTRUCTOR_P (t);
4405 case IS_DESTRUCTOR:
4406 return DECL_DESTRUCTOR_P (t);
4407 case IS_COPY_CONSTRUCTOR:
4408 return DECL_COPY_CONSTRUCTOR_P (t);
4409 case IS_MOVE_CONSTRUCTOR:
4410 return DECL_MOVE_CONSTRUCTOR_P (t);
4411 case IS_TEMPLATE:
4412 return TREE_CODE (t) == TEMPLATE_DECL;
4413 case IS_TRIVIAL:
4414 return trivial_type_p (t);
4415 default:
4416 return 0;
4420 /* Collect source file references recursively, starting from NAMESPC. */
4422 static void
4423 collect_source_refs (tree namespc)
4425 /* Iterate over names in this name space. */
4426 for (tree t = NAMESPACE_LEVEL (namespc)->names; t; t = TREE_CHAIN (t))
4427 if (DECL_IS_UNDECLARED_BUILTIN (t))
4429 else if (TREE_CODE (t) == NAMESPACE_DECL && !DECL_NAMESPACE_ALIAS (t))
4430 collect_source_refs (t);
4431 else
4432 collect_source_ref (DECL_SOURCE_FILE (t));
4435 /* Collect decls relevant to SOURCE_FILE from all namespaces recursively,
4436 starting from NAMESPC. */
4438 static void
4439 collect_ada_namespace (tree namespc, const char *source_file)
4441 tree decl = NAMESPACE_LEVEL (namespc)->names;
4443 /* Collect decls from this namespace. This will skip
4444 NAMESPACE_DECLs (both aliases and regular, it cannot tell). */
4445 collect_ada_nodes (decl, source_file);
4447 /* Now scan for namespace children, and dump them. */
4448 for (; decl; decl = TREE_CHAIN (decl))
4449 if (TREE_CODE (decl) == NAMESPACE_DECL && !DECL_NAMESPACE_ALIAS (decl))
4450 collect_ada_namespace (decl, source_file);
4453 /* Returns true iff there is a definition available for variable or
4454 function DECL. */
4456 bool
4457 decl_defined_p (tree decl)
4459 if (TREE_CODE (decl) == FUNCTION_DECL)
4460 return (DECL_INITIAL (decl) != NULL_TREE
4461 /* A pending instantiation of a friend temploid is defined. */
4462 || (DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl)
4463 && DECL_INITIAL (DECL_TEMPLATE_RESULT
4464 (DECL_TI_TEMPLATE (decl)))));
4465 else
4467 gcc_assert (VAR_P (decl));
4468 return !DECL_EXTERNAL (decl);
4472 /* Nonzero for a VAR_DECL whose value can be used in a constant expression.
4474 [expr.const]
4476 An integral constant-expression can only involve ... const
4477 variables of integral or enumeration types initialized with
4478 constant expressions ...
4480 C++0x also allows constexpr variables and temporaries initialized
4481 with constant expressions. We handle the former here, but the latter
4482 are just folded away in cxx_eval_constant_expression.
4484 The standard does not require that the expression be non-volatile.
4485 G++ implements the proposed correction in DR 457. */
4487 bool
4488 decl_constant_var_p (tree decl)
4490 if (!decl_maybe_constant_var_p (decl))
4491 return false;
4493 /* We don't know if a template static data member is initialized with
4494 a constant expression until we instantiate its initializer. Even
4495 in the case of a constexpr variable, we can't treat it as a
4496 constant until its initializer is complete in case it's used in
4497 its own initializer. */
4498 maybe_instantiate_decl (decl);
4499 return DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl);
4502 /* Returns true if DECL could be a symbolic constant variable, depending on
4503 its initializer. */
4505 bool
4506 decl_maybe_constant_var_p (tree decl)
4508 tree type = TREE_TYPE (decl);
4509 if (!VAR_P (decl))
4510 return false;
4511 if (DECL_DECLARED_CONSTEXPR_P (decl) && !TREE_THIS_VOLATILE (decl))
4512 return true;
4513 if (DECL_HAS_VALUE_EXPR_P (decl))
4514 /* A proxy isn't constant. */
4515 return false;
4516 if (TYPE_REF_P (type))
4517 /* References can be constant. */;
4518 else if (CP_TYPE_CONST_NON_VOLATILE_P (type)
4519 && INTEGRAL_OR_ENUMERATION_TYPE_P (type))
4520 /* And const integers. */;
4521 else
4522 return false;
4524 if (DECL_INITIAL (decl)
4525 && !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl))
4526 /* We know the initializer, and it isn't constant. */
4527 return false;
4528 else
4529 return true;
4532 /* Complain that DECL uses a type with no linkage. In C++98 mode this is
4533 called from grokfndecl and grokvardecl; in all modes it is called from
4534 cp_write_global_declarations. */
4536 void
4537 no_linkage_error (tree decl)
4539 if (cxx_dialect >= cxx11
4540 && (decl_defined_p (decl)
4541 /* Treat templates which limit_bad_template_recursion decided
4542 not to instantiate as if they were defined. */
4543 || (errorcount + sorrycount > 0
4544 && DECL_LANG_SPECIFIC (decl)
4545 && DECL_TEMPLATE_INFO (decl)
4546 && warning_suppressed_p (decl /* What warning? */))))
4547 /* In C++11 it's ok if the decl is defined. */
4548 return;
4550 if (DECL_LANG_SPECIFIC (decl) && DECL_MODULE_IMPORT_P (decl))
4551 /* An imported decl is ok. */
4552 return;
4554 tree t = no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/false);
4555 if (t == NULL_TREE)
4556 /* The type that got us on no_linkage_decls must have gotten a name for
4557 linkage purposes. */;
4558 else if (CLASS_TYPE_P (t) && TYPE_BEING_DEFINED (t))
4559 // FIXME: This is now invalid, as a DR to c++98
4560 /* The type might end up having a typedef name for linkage purposes. */
4561 vec_safe_push (no_linkage_decls, decl);
4562 else if (TYPE_UNNAMED_P (t))
4564 bool d = false;
4565 auto_diagnostic_group grp;
4566 if (cxx_dialect >= cxx11)
4567 d = permerror (DECL_SOURCE_LOCATION (decl), "%q#D, declared using "
4568 "unnamed type, is used but never defined", decl);
4569 else if (DECL_EXTERN_C_P (decl))
4570 /* Allow this; it's pretty common in C. */;
4571 else if (VAR_P (decl))
4572 /* DRs 132, 319 and 389 seem to indicate types with
4573 no linkage can only be used to declare extern "C"
4574 entities. Since it's not always an error in the
4575 ISO C++ 90 Standard, we only issue a warning. */
4576 d = warning_at (DECL_SOURCE_LOCATION (decl), 0, "unnamed type "
4577 "with no linkage used to declare variable %q#D with "
4578 "linkage", decl);
4579 else
4580 d = permerror (DECL_SOURCE_LOCATION (decl), "unnamed type with no "
4581 "linkage used to declare function %q#D with linkage",
4582 decl);
4583 if (d && is_typedef_decl (TYPE_NAME (t)))
4584 inform (DECL_SOURCE_LOCATION (TYPE_NAME (t)), "%q#D does not refer "
4585 "to the unqualified type, so it is not used for linkage",
4586 TYPE_NAME (t));
4588 else if (cxx_dialect >= cxx11)
4590 if (VAR_P (decl) || !DECL_PURE_VIRTUAL_P (decl))
4591 permerror (DECL_SOURCE_LOCATION (decl),
4592 "%q#D, declared using local type "
4593 "%qT, is used but never defined", decl, t);
4595 else if (VAR_P (decl))
4596 warning_at (DECL_SOURCE_LOCATION (decl), 0, "type %qT with no linkage "
4597 "used to declare variable %q#D with linkage", t, decl);
4598 else
4599 permerror (DECL_SOURCE_LOCATION (decl), "type %qT with no linkage used "
4600 "to declare function %q#D with linkage", t, decl);
4603 /* Collect declarations from all namespaces relevant to SOURCE_FILE. */
4605 static void
4606 collect_all_refs (const char *source_file)
4608 collect_ada_namespace (global_namespace, source_file);
4611 /* Clear DECL_EXTERNAL for NODE. */
4613 static bool
4614 clear_decl_external (struct cgraph_node *node, void * /*data*/)
4616 DECL_EXTERNAL (node->decl) = 0;
4617 return false;
4620 /* Build up the function to run dynamic initializers for thread_local
4621 variables in this translation unit and alias the init functions for the
4622 individual variables to it. */
4624 static void
4625 handle_tls_init (void)
4627 tree vars = prune_vars_needing_no_initialization (&tls_aggregates);
4628 if (vars == NULL_TREE)
4629 return;
4631 location_t loc = DECL_SOURCE_LOCATION (TREE_VALUE (vars));
4633 write_out_vars (vars);
4635 tree guard = build_decl (loc, VAR_DECL, get_identifier ("__tls_guard"),
4636 boolean_type_node);
4637 TREE_PUBLIC (guard) = false;
4638 TREE_STATIC (guard) = true;
4639 DECL_ARTIFICIAL (guard) = true;
4640 DECL_IGNORED_P (guard) = true;
4641 TREE_USED (guard) = true;
4642 CP_DECL_THREAD_LOCAL_P (guard) = true;
4643 set_decl_tls_model (guard, decl_default_tls_model (guard));
4644 pushdecl_top_level_and_finish (guard, NULL_TREE);
4646 tree fn = get_local_tls_init_fn (loc);
4647 start_preparsed_function (fn, NULL_TREE, SF_PRE_PARSED);
4648 tree body = begin_function_body ();
4649 tree if_stmt = begin_if_stmt ();
4650 tree cond = cp_build_unary_op (TRUTH_NOT_EXPR, guard, false,
4651 tf_warning_or_error);
4652 finish_if_stmt_cond (cond, if_stmt);
4653 finish_expr_stmt (cp_build_modify_expr (loc, guard, NOP_EXPR,
4654 boolean_true_node,
4655 tf_warning_or_error));
4656 for (; vars; vars = TREE_CHAIN (vars))
4658 tree var = TREE_VALUE (vars);
4659 tree init = TREE_PURPOSE (vars);
4660 one_static_initialization_or_destruction (/*initp=*/true, var, init);
4662 /* Output init aliases even with -fno-extern-tls-init. */
4663 if (TARGET_SUPPORTS_ALIASES && TREE_PUBLIC (var))
4665 tree single_init_fn = get_tls_init_fn (var);
4666 if (single_init_fn == NULL_TREE)
4667 continue;
4668 cgraph_node *alias
4669 = cgraph_node::get_create (fn)->create_same_body_alias
4670 (single_init_fn, fn);
4671 gcc_assert (alias != NULL);
4675 finish_then_clause (if_stmt);
4676 finish_if_stmt (if_stmt);
4677 finish_function_body (body);
4678 expand_or_defer_fn (finish_function (/*inline_p=*/false));
4681 /* We're at the end of compilation, so generate any mangling aliases that
4682 we've been saving up, if DECL is going to be output and ID2 isn't
4683 already taken by another declaration. */
4685 static void
4686 generate_mangling_alias (tree decl, tree id2)
4688 struct cgraph_node *n = NULL;
4690 if (TREE_CODE (decl) == FUNCTION_DECL)
4692 n = cgraph_node::get (decl);
4693 if (!n)
4694 /* Don't create an alias to an unreferenced function. */
4695 return;
4698 tree *slot
4699 = mangled_decls->find_slot_with_hash (id2, IDENTIFIER_HASH_VALUE (id2),
4700 INSERT);
4702 /* If there's a declaration already using this mangled name,
4703 don't create a compatibility alias that conflicts. */
4704 if (*slot)
4705 return;
4707 tree alias = make_alias_for (decl, id2);
4708 *slot = alias;
4710 DECL_IGNORED_P (alias) = 1;
4711 TREE_PUBLIC (alias) = TREE_PUBLIC (decl);
4712 DECL_VISIBILITY (alias) = DECL_VISIBILITY (decl);
4713 if (vague_linkage_p (decl))
4714 DECL_WEAK (alias) = 1;
4716 if (n)
4717 n->create_same_body_alias (alias, decl);
4718 else
4719 varpool_node::create_extra_name_alias (alias, decl);
4722 /* Note that we might want to emit an alias with the symbol ID2 for DECL at
4723 the end of translation, for compatibility across bugs in the mangling
4724 implementation. */
4726 void
4727 note_mangling_alias (tree decl, tree id2)
4729 if (TARGET_SUPPORTS_ALIASES)
4731 if (!defer_mangling_aliases)
4732 generate_mangling_alias (decl, id2);
4733 else
4735 vec_safe_push (mangling_aliases, decl);
4736 vec_safe_push (mangling_aliases, id2);
4741 /* Emit all mangling aliases that were deferred up to this point. */
4743 void
4744 generate_mangling_aliases ()
4746 while (!vec_safe_is_empty (mangling_aliases))
4748 tree id2 = mangling_aliases->pop();
4749 tree decl = mangling_aliases->pop();
4750 generate_mangling_alias (decl, id2);
4752 defer_mangling_aliases = false;
4755 /* Record a mangling of DECL, whose DECL_ASSEMBLER_NAME has just been
4756 set. NEED_WARNING is true if we must warn about collisions. We do
4757 this to spot changes in mangling that may require compatibility
4758 aliases. */
4760 void
4761 record_mangling (tree decl, bool need_warning)
4763 if (!mangled_decls)
4764 mangled_decls = hash_table<mangled_decl_hash>::create_ggc (499);
4766 gcc_checking_assert (DECL_ASSEMBLER_NAME_SET_P (decl));
4767 tree id = DECL_ASSEMBLER_NAME_RAW (decl);
4768 tree *slot
4769 = mangled_decls->find_slot_with_hash (id, IDENTIFIER_HASH_VALUE (id),
4770 INSERT);
4772 /* If this is already an alias, cancel the alias, because the real
4773 decl takes precedence. */
4774 if (*slot && DECL_ARTIFICIAL (*slot) && DECL_IGNORED_P (*slot))
4776 if (symtab_node *n = symtab_node::get (*slot))
4778 if (n->cpp_implicit_alias)
4779 /* Actually removing the node isn't safe if other code is already
4780 holding a pointer to it, so just neutralize it. */
4781 n->reset ();
4783 else
4784 /* analyze_functions might have already removed the alias from the
4785 symbol table if it's internal. */
4786 gcc_checking_assert (!TREE_PUBLIC (*slot));
4788 *slot = NULL_TREE;
4791 if (!*slot)
4792 *slot = decl;
4793 else if (need_warning)
4795 error_at (DECL_SOURCE_LOCATION (decl),
4796 "mangling of %q#D as %qE conflicts with a previous mangle",
4797 decl, id);
4798 inform (DECL_SOURCE_LOCATION (*slot),
4799 "previous mangling %q#D", *slot);
4800 inform (DECL_SOURCE_LOCATION (decl),
4801 "a later %<-fabi-version=%> (or =0)"
4802 " avoids this error with a change in mangling");
4803 *slot = decl;
4807 /* The mangled name of DECL is being forcibly changed to NAME. Remove
4808 any existing knowledge of DECL's mangled name meaning DECL. */
4810 void
4811 overwrite_mangling (tree decl, tree name)
4813 if (tree id = DECL_ASSEMBLER_NAME_RAW (decl))
4814 if ((TREE_CODE (decl) == VAR_DECL
4815 || TREE_CODE (decl) == FUNCTION_DECL)
4816 && mangled_decls)
4817 if (tree *slot
4818 = mangled_decls->find_slot_with_hash (id, IDENTIFIER_HASH_VALUE (id),
4819 NO_INSERT))
4820 if (*slot == decl)
4822 mangled_decls->clear_slot (slot);
4824 /* If this is an alias, remove it from the symbol table. */
4825 if (DECL_ARTIFICIAL (decl) && DECL_IGNORED_P (decl))
4826 if (symtab_node *n = symtab_node::get (decl))
4827 if (n->cpp_implicit_alias)
4828 n->remove ();
4831 DECL_ASSEMBLER_NAME_RAW (decl) = name;
4834 /* The entire file is now complete. If requested, dump everything
4835 to a file. */
4837 static void
4838 dump_tu (void)
4840 dump_flags_t flags;
4841 if (FILE *stream = dump_begin (raw_dump_id, &flags))
4843 dump_node (global_namespace, flags & ~TDF_SLIM, stream);
4844 dump_end (raw_dump_id, stream);
4848 static location_t locus_at_end_of_parsing;
4850 /* Check the deallocation functions for CODE to see if we want to warn that
4851 only one was defined. */
4853 static void
4854 maybe_warn_sized_delete (enum tree_code code)
4856 tree sized = NULL_TREE;
4857 tree unsized = NULL_TREE;
4859 for (ovl_iterator iter (get_global_binding (ovl_op_identifier (false, code)));
4860 iter; ++iter)
4862 tree fn = *iter;
4863 /* We're only interested in usual deallocation functions. */
4864 if (!usual_deallocation_fn_p (fn))
4865 continue;
4866 if (FUNCTION_ARG_CHAIN (fn) == void_list_node)
4867 unsized = fn;
4868 else
4869 sized = fn;
4871 if (DECL_INITIAL (unsized) && !DECL_INITIAL (sized))
4872 warning_at (DECL_SOURCE_LOCATION (unsized), OPT_Wsized_deallocation,
4873 "the program should also define %qD", sized);
4874 else if (!DECL_INITIAL (unsized) && DECL_INITIAL (sized))
4875 warning_at (DECL_SOURCE_LOCATION (sized), OPT_Wsized_deallocation,
4876 "the program should also define %qD", unsized);
4879 /* Check the global deallocation functions to see if we want to warn about
4880 defining unsized without sized (or vice versa). */
4882 static void
4883 maybe_warn_sized_delete ()
4885 if (!flag_sized_deallocation || !warn_sized_deallocation)
4886 return;
4887 maybe_warn_sized_delete (DELETE_EXPR);
4888 maybe_warn_sized_delete (VEC_DELETE_EXPR);
4891 /* Earlier we left PTRMEM_CST in variable initializers alone so that we could
4892 look them up when evaluating non-type template parameters. Now we need to
4893 lower them to something the back end can understand. */
4895 static void
4896 lower_var_init ()
4898 varpool_node *node;
4899 FOR_EACH_VARIABLE (node)
4901 tree d = node->decl;
4902 if (tree init = DECL_INITIAL (d))
4903 DECL_INITIAL (d) = cplus_expand_constant (init);
4907 /* This routine is called at the end of compilation.
4908 Its job is to create all the code needed to initialize and
4909 destroy the global aggregates. We do the destruction
4910 first, since that way we only need to reverse the decls once. */
4912 void
4913 c_parse_final_cleanups (void)
4915 size_t i;
4916 tree decl;
4918 locus_at_end_of_parsing = input_location;
4919 at_eof = 1;
4921 /* Bad parse errors. Just forget about it. */
4922 if (! global_bindings_p () || current_class_type
4923 || !vec_safe_is_empty (decl_namespace_list))
4924 return;
4926 /* This is the point to write out a PCH if we're doing that.
4927 In that case we do not want to do anything else. */
4928 if (pch_file)
4930 /* Mangle all symbols at PCH creation time. */
4931 symtab_node *node;
4932 FOR_EACH_SYMBOL (node)
4933 if (! is_a <varpool_node *> (node)
4934 || ! DECL_HARD_REGISTER (node->decl))
4935 DECL_ASSEMBLER_NAME (node->decl);
4936 c_common_write_pch ();
4937 dump_tu ();
4938 /* Ensure even the callers don't try to finalize the CU. */
4939 flag_syntax_only = 1;
4940 return;
4943 timevar_stop (TV_PHASE_PARSING);
4944 timevar_start (TV_PHASE_DEFERRED);
4946 symtab->process_same_body_aliases ();
4948 /* Handle -fdump-ada-spec[-slim] */
4949 if (flag_dump_ada_spec || flag_dump_ada_spec_slim)
4951 collect_source_ref (main_input_filename);
4952 if (!flag_dump_ada_spec_slim)
4953 collect_source_refs (global_namespace);
4955 dump_ada_specs (collect_all_refs, cpp_check);
4958 /* FIXME - huh? was input_line -= 1;*/
4960 /* We now have to write out all the stuff we put off writing out.
4961 These include:
4963 o Template specializations that we have not yet instantiated,
4964 but which are needed.
4965 o Initialization and destruction for non-local objects with
4966 static storage duration. (Local objects with static storage
4967 duration are initialized when their scope is first entered,
4968 and are cleaned up via atexit.)
4969 o Virtual function tables.
4971 All of these may cause others to be needed. For example,
4972 instantiating one function may cause another to be needed, and
4973 generating the initializer for an object may cause templates to be
4974 instantiated, etc., etc. */
4976 emit_support_tinfos ();
4978 /* Track vtables we want to emit that refer to consteval functions. */
4979 auto_vec<tree> consteval_vtables;
4981 int retries = 0;
4982 unsigned ssdf_count = 0;
4983 for (bool reconsider = true; reconsider; retries++)
4985 reconsider = false;
4987 /* If there are templates that we've put off instantiating, do
4988 them now. */
4989 instantiate_pending_templates (retries);
4990 ggc_collect ();
4992 if (header_module_p ())
4993 /* A header modules initializations are handled in its
4994 importer. */
4995 continue;
4997 /* Write out virtual tables as required. Writing out the
4998 virtual table for a template class may cause the
4999 instantiation of members of that class. If we write out
5000 vtables then we remove the class from our list so we don't
5001 have to look at it again. */
5002 tree t;
5003 for (i = keyed_classes->length ();
5004 keyed_classes->iterate (--i, &t);)
5005 if (maybe_emit_vtables (t, consteval_vtables))
5007 reconsider = true;
5008 keyed_classes->unordered_remove (i);
5010 /* The input_location may have been changed during marking of
5011 vtable entries. */
5012 input_location = locus_at_end_of_parsing;
5014 /* Write out needed type info variables. We have to be careful
5015 looping through unemitted decls, because emit_tinfo_decl may
5016 cause other variables to be needed. New elements will be
5017 appended, and we remove from the vector those that actually
5018 get emitted. */
5019 for (i = unemitted_tinfo_decls->length ();
5020 unemitted_tinfo_decls->iterate (--i, &t);)
5021 if (DECL_INITIAL (t) || emit_tinfo_decl (t))
5023 reconsider = true;
5024 unemitted_tinfo_decls->unordered_remove (i);
5027 /* The list of objects with static storage duration is built up
5028 in reverse order. We clear STATIC_AGGREGATES so that any new
5029 aggregates added during the initialization of these will be
5030 initialized in the correct order when we next come around the
5031 loop. */
5032 if (tree vars = prune_vars_needing_no_initialization (&static_aggregates))
5034 if (flag_openmp)
5035 /* Add initializer information from VARS into
5036 DYNAMIC_INITIALIZERS. */
5037 for (t = vars; t; t = TREE_CHAIN (t))
5038 hash_map_safe_put<hm_ggc> (dynamic_initializers,
5039 TREE_VALUE (t), TREE_PURPOSE (t));
5041 /* Make sure the back end knows about all the variables. */
5042 write_out_vars (vars);
5044 function_depth++; // Disable GC
5045 priority_map_t *parts[2] = {nullptr, nullptr};
5046 partition_vars_for_init_fini (vars, parts);
5048 for (unsigned initp = 2; initp--;)
5049 if (parts[initp])
5050 for (auto iter : *parts[initp])
5052 auto list = iter.second;
5053 if (initp)
5054 // Partitioning kept the vars in reverse order.
5055 // We only want that for dtors.
5056 list = nreverse (list);
5057 emit_partial_init_fini_fn (initp, iter.first, list,
5058 ssdf_count++,
5059 locus_at_end_of_parsing);
5061 function_depth--; // Re-enable GC
5063 /* All those initializations and finalizations might cause
5064 us to need more inline functions, more template
5065 instantiations, etc. */
5066 reconsider = true;
5069 /* Now do the same for thread_local variables. */
5070 handle_tls_init ();
5072 /* Go through the set of inline functions whose bodies have not
5073 been emitted yet. If out-of-line copies of these functions
5074 are required, emit them. */
5075 FOR_EACH_VEC_SAFE_ELT (deferred_fns, i, decl)
5077 /* Does it need synthesizing? */
5078 if (DECL_DEFAULTED_FN (decl) && ! DECL_INITIAL (decl)
5079 && (! DECL_REALLY_EXTERN (decl) || possibly_inlined_p (decl)))
5081 /* Even though we're already at the top-level, we push
5082 there again. That way, when we pop back a few lines
5083 hence, all of our state is restored. Otherwise,
5084 finish_function doesn't clean things up, and we end
5085 up with CURRENT_FUNCTION_DECL set. */
5086 push_to_top_level ();
5087 /* The decl's location will mark where it was first
5088 needed. Save that so synthesize method can indicate
5089 where it was needed from, in case of error */
5090 input_location = DECL_SOURCE_LOCATION (decl);
5091 synthesize_method (decl);
5092 pop_from_top_level ();
5093 reconsider = true;
5096 if (!DECL_INITIAL (decl) && decl_tls_wrapper_p (decl))
5097 generate_tls_wrapper (decl);
5099 if (!DECL_SAVED_TREE (decl))
5100 continue;
5102 cgraph_node *node = cgraph_node::get_create (decl);
5104 /* We lie to the back end, pretending that some functions
5105 are not defined when they really are. This keeps these
5106 functions from being put out unnecessarily. But, we must
5107 stop lying when the functions are referenced, or if they
5108 are not comdat since they need to be put out now. If
5109 DECL_INTERFACE_KNOWN, then we have already set
5110 DECL_EXTERNAL appropriately, so there's no need to check
5111 again, and we do not want to clear DECL_EXTERNAL if a
5112 previous call to import_export_decl set it.
5114 This is done in a separate for cycle, because if some
5115 deferred function is contained in another deferred
5116 function later in deferred_fns varray,
5117 rest_of_compilation would skip this function and we
5118 really cannot expand the same function twice. */
5119 import_export_decl (decl);
5120 if (DECL_NOT_REALLY_EXTERN (decl)
5121 && DECL_INITIAL (decl)
5122 && decl_needed_p (decl))
5124 if (node->cpp_implicit_alias)
5125 node = node->get_alias_target ();
5127 node->call_for_symbol_thunks_and_aliases (clear_decl_external,
5128 NULL, true);
5129 /* If we mark !DECL_EXTERNAL one of the symbols in some comdat
5130 group, we need to mark all symbols in the same comdat group
5131 that way. */
5132 if (node->same_comdat_group)
5133 for (cgraph_node *next
5134 = dyn_cast<cgraph_node *> (node->same_comdat_group);
5135 next != node;
5136 next = dyn_cast<cgraph_node *> (next->same_comdat_group))
5137 next->call_for_symbol_thunks_and_aliases (clear_decl_external,
5138 NULL, true);
5141 /* If we're going to need to write this function out, and
5142 there's already a body for it, create RTL for it now.
5143 (There might be no body if this is a method we haven't
5144 gotten around to synthesizing yet.) */
5145 if (!DECL_EXTERNAL (decl)
5146 && decl_needed_p (decl)
5147 && !TREE_ASM_WRITTEN (decl)
5148 && !DECL_IMMEDIATE_FUNCTION_P (decl)
5149 && !node->definition)
5151 /* We will output the function; no longer consider it in this
5152 loop. */
5153 DECL_DEFER_OUTPUT (decl) = 0;
5154 /* Generate RTL for this function now that we know we
5155 need it. */
5156 expand_or_defer_fn (decl);
5157 reconsider = true;
5161 if (wrapup_namespace_globals ())
5162 reconsider = true;
5164 /* Static data members are just like namespace-scope globals. */
5165 FOR_EACH_VEC_SAFE_ELT (pending_statics, i, decl)
5167 if (var_finalized_p (decl) || DECL_REALLY_EXTERN (decl)
5168 /* Don't write it out if we haven't seen a definition. */
5169 || DECL_IN_AGGR_P (decl))
5170 continue;
5171 import_export_decl (decl);
5172 /* If this static data member is needed, provide it to the
5173 back end. */
5174 if (DECL_NOT_REALLY_EXTERN (decl) && decl_needed_p (decl))
5175 DECL_EXTERNAL (decl) = 0;
5178 if (vec_safe_length (pending_statics) != 0
5179 && wrapup_global_declarations (pending_statics->address (),
5180 pending_statics->length ()))
5181 reconsider = true;
5184 void *module_cookie = finish_module_processing (parse_in);
5186 lower_var_init ();
5188 generate_mangling_aliases ();
5190 /* All used inline functions must have a definition at this point. */
5191 FOR_EACH_VEC_SAFE_ELT (deferred_fns, i, decl)
5193 if (/* Check online inline functions that were actually used. */
5194 DECL_ODR_USED (decl) && DECL_DECLARED_INLINE_P (decl)
5195 /* If the definition actually was available here, then the
5196 fact that the function was not defined merely represents
5197 that for some reason (use of a template repository,
5198 #pragma interface, etc.) we decided not to emit the
5199 definition here. */
5200 && !DECL_INITIAL (decl)
5201 /* A defaulted fn in a header module can be synthesized on
5202 demand later. (In non-header modules we should have
5203 synthesized it above.) */
5204 && !(DECL_DEFAULTED_FN (decl) && header_module_p ())
5205 /* Don't complain if the template was defined. */
5206 && !(DECL_TEMPLATE_INSTANTIATION (decl)
5207 && DECL_INITIAL (DECL_TEMPLATE_RESULT
5208 (template_for_substitution (decl))))
5209 && warning_at (DECL_SOURCE_LOCATION (decl), 0,
5210 "inline function %qD used but never defined", decl))
5211 /* Avoid a duplicate warning from check_global_declaration. */
5212 suppress_warning (decl, OPT_Wunused);
5215 /* So must decls that use a type with no linkage. */
5216 FOR_EACH_VEC_SAFE_ELT (no_linkage_decls, i, decl)
5217 no_linkage_error (decl);
5219 maybe_warn_sized_delete ();
5221 // Place the init fns in the right order. We need to do this now,
5222 // so that any module init will go at the start.
5223 if (static_init_fini_fns[true])
5224 for (auto iter : *static_init_fini_fns[true])
5225 iter.second = nreverse (iter.second);
5227 /* Then, do the Objective-C stuff. This is where all the
5228 Objective-C module stuff gets generated (symtab,
5229 class/protocol/selector lists etc). This must be done after C++
5230 templates, destructors etc. so that selectors used in C++
5231 templates are properly allocated. */
5232 if (c_dialect_objc ())
5233 objc_write_global_declarations ();
5235 bool has_module_inits = module_determine_import_inits ();
5236 bool has_objc_init = c_dialect_objc () && objc_static_init_needed_p ();
5237 if (has_module_inits || has_objc_init)
5239 input_location = locus_at_end_of_parsing;
5240 tree body = start_partial_init_fini_fn (true, DEFAULT_INIT_PRIORITY,
5241 ssdf_count++);
5242 /* For Objective-C++, we may need to initialize metadata found
5243 in this module. This must be done _before_ any other static
5244 initializations. */
5245 if (has_objc_init)
5246 objc_generate_static_init_call (NULL_TREE);
5247 if (has_module_inits)
5248 module_add_import_initializers ();
5249 input_location = locus_at_end_of_parsing;
5250 finish_partial_init_fini_fn (body);
5253 if (module_global_init_needed ())
5255 // Make sure there's a default priority entry.
5256 if (!static_init_fini_fns[true])
5257 static_init_fini_fns[true] = priority_map_t::create_ggc ();
5258 if (static_init_fini_fns[true]->get_or_insert (DEFAULT_INIT_PRIORITY))
5259 has_module_inits = true;
5262 /* Generate initialization and destruction functions for all
5263 priorities for which they are required. They have C-language
5264 linkage. */
5265 push_lang_context (lang_name_c);
5266 for (unsigned initp = 2; initp--;)
5267 if (static_init_fini_fns[initp])
5269 for (auto iter : *static_init_fini_fns[initp])
5270 generate_ctor_or_dtor_function (initp, iter.first, iter.second,
5271 locus_at_end_of_parsing);
5272 static_init_fini_fns[initp] = nullptr;
5274 pop_lang_context ();
5276 fini_modules (parse_in, module_cookie, has_module_inits);
5278 /* Generate any missing aliases. */
5279 maybe_apply_pending_pragma_weaks ();
5281 if (flag_vtable_verify)
5283 vtv_recover_class_info ();
5284 vtv_compute_class_hierarchy_transitive_closure ();
5285 vtv_build_vtable_verify_fndecl ();
5288 perform_deferred_noexcept_checks ();
5290 fini_constexpr ();
5291 cp_tree_c_finish_parsing ();
5292 clear_consteval_vfns (consteval_vtables);
5294 /* The entire file is now complete. If requested, dump everything
5295 to a file. */
5296 dump_tu ();
5298 if (flag_detailed_statistics)
5300 dump_tree_statistics ();
5301 dump_time_statistics ();
5304 timevar_stop (TV_PHASE_DEFERRED);
5305 timevar_start (TV_PHASE_PARSING);
5307 /* Indicate that we're done with front end processing. */
5308 at_eof = 2;
5311 /* Perform any post compilation-proper cleanups for the C++ front-end.
5312 This should really go away. No front-end should need to do
5313 anything past the compilation process. */
5315 void
5316 cxx_post_compilation_parsing_cleanups (void)
5318 timevar_start (TV_PHASE_LATE_PARSING_CLEANUPS);
5320 if (flag_vtable_verify)
5322 /* Generate the special constructor initialization function that
5323 calls __VLTRegisterPairs, and give it a very high
5324 initialization priority. This must be done after
5325 finalize_compilation_unit so that we have accurate
5326 information about which vtable will actually be emitted. */
5327 vtv_generate_init_routine ();
5330 input_location = locus_at_end_of_parsing;
5332 if (flag_checking)
5333 validate_conversion_obstack ();
5335 timevar_stop (TV_PHASE_LATE_PARSING_CLEANUPS);
5338 /* FN is an OFFSET_REF, DOTSTAR_EXPR or MEMBER_REF indicating the
5339 function to call in parse-tree form; it has not yet been
5340 semantically analyzed. ARGS are the arguments to the function.
5341 They have already been semantically analyzed. This may change
5342 ARGS. */
5344 tree
5345 build_offset_ref_call_from_tree (tree fn, vec<tree, va_gc> **args,
5346 tsubst_flags_t complain)
5348 tree orig_fn;
5349 vec<tree, va_gc> *orig_args = NULL;
5350 tree expr;
5351 tree object;
5353 orig_fn = fn;
5354 object = TREE_OPERAND (fn, 0);
5356 if (processing_template_decl)
5358 gcc_assert (TREE_CODE (fn) == DOTSTAR_EXPR
5359 || TREE_CODE (fn) == MEMBER_REF);
5360 if (type_dependent_expression_p (fn)
5361 || any_type_dependent_arguments_p (*args))
5362 return build_min_nt_call_vec (fn, *args);
5364 orig_args = make_tree_vector_copy (*args);
5366 /* Transform the arguments and add the implicit "this"
5367 parameter. That must be done before the FN is transformed
5368 because we depend on the form of FN. */
5369 make_args_non_dependent (*args);
5370 object = build_non_dependent_expr (object);
5371 if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
5373 if (TREE_CODE (fn) == DOTSTAR_EXPR)
5374 object = cp_build_addr_expr (object, complain);
5375 vec_safe_insert (*args, 0, object);
5377 /* Now that the arguments are done, transform FN. */
5378 fn = build_non_dependent_expr (fn);
5381 /* A qualified name corresponding to a bound pointer-to-member is
5382 represented as an OFFSET_REF:
5384 struct B { void g(); };
5385 void (B::*p)();
5386 void B::g() { (this->*p)(); } */
5387 if (TREE_CODE (fn) == OFFSET_REF)
5389 tree object_addr = cp_build_addr_expr (object, complain);
5390 fn = TREE_OPERAND (fn, 1);
5391 fn = get_member_function_from_ptrfunc (&object_addr, fn,
5392 complain);
5393 vec_safe_insert (*args, 0, object_addr);
5396 if (CLASS_TYPE_P (TREE_TYPE (fn)))
5397 expr = build_op_call (fn, args, complain);
5398 else
5399 expr = cp_build_function_call_vec (fn, args, complain);
5400 if (processing_template_decl && expr != error_mark_node)
5401 expr = build_min_non_dep_call_vec (expr, orig_fn, orig_args);
5403 if (orig_args != NULL)
5404 release_tree_vector (orig_args);
5406 return expr;
5410 void
5411 check_default_args (tree x)
5413 tree arg = TYPE_ARG_TYPES (TREE_TYPE (x));
5414 bool saw_def = false;
5415 bool noted_first_def = false;
5416 int idx_of_first_default_arg = 0;
5417 location_t loc_of_first_default_arg = UNKNOWN_LOCATION;
5418 int i = 0 - (TREE_CODE (TREE_TYPE (x)) == METHOD_TYPE);
5419 tree fndecl = STRIP_TEMPLATE (x);
5420 auto_diagnostic_group d;
5421 for (; arg && arg != void_list_node; arg = TREE_CHAIN (arg), ++i)
5423 if (TREE_PURPOSE (arg))
5425 if (!saw_def)
5427 saw_def = true;
5428 idx_of_first_default_arg = i;
5429 location_t loc = get_fndecl_argument_location (fndecl, i);
5430 if (loc != DECL_SOURCE_LOCATION (x))
5431 loc_of_first_default_arg = loc;
5434 else if (saw_def && !PACK_EXPANSION_P (TREE_VALUE (arg)))
5436 error_at (get_fndecl_argument_location (fndecl, i),
5437 "default argument missing for parameter %P of %q#D", i, x);
5438 if (loc_of_first_default_arg != UNKNOWN_LOCATION
5439 && !noted_first_def)
5441 inform (loc_of_first_default_arg,
5442 "...following parameter %P which has a default argument",
5443 idx_of_first_default_arg);
5444 noted_first_def = true;
5446 TREE_PURPOSE (arg) = error_mark_node;
5451 /* Return true if function DECL can be inlined. This is used to force
5452 instantiation of methods that might be interesting for inlining. */
5453 bool
5454 possibly_inlined_p (tree decl)
5456 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
5457 if (DECL_UNINLINABLE (decl))
5458 return false;
5459 if (!optimize)
5460 return DECL_DECLARED_INLINE_P (decl);
5461 /* When optimizing, we might inline everything when flatten
5462 attribute or heuristics inlining for size or autoinlining
5463 is used. */
5464 return true;
5467 /* If DECL is a function or variable template specialization, instantiate
5468 its definition now. */
5470 void
5471 maybe_instantiate_decl (tree decl)
5473 if (VAR_OR_FUNCTION_DECL_P (decl)
5474 && DECL_LANG_SPECIFIC (decl)
5475 && DECL_TEMPLATE_INFO (decl)
5476 && !DECL_DECLARED_CONCEPT_P (decl)
5477 && !uses_template_parms (DECL_TI_ARGS (decl)))
5479 /* Instantiating a function will result in garbage collection. We
5480 must treat this situation as if we were within the body of a
5481 function so as to avoid collecting live data only referenced from
5482 the stack (such as overload resolution candidates). */
5483 ++function_depth;
5484 instantiate_decl (decl, /*defer_ok=*/false,
5485 /*expl_inst_class_mem_p=*/false);
5486 --function_depth;
5490 /* Error if the DECL is unavailable (unless this is currently suppressed).
5491 Maybe warn if DECL is deprecated, subject to COMPLAIN. Returns true if
5492 an error or warning was emitted. */
5494 bool
5495 cp_handle_deprecated_or_unavailable (tree decl, tsubst_flags_t complain)
5497 if (!decl)
5498 return false;
5500 if ((complain & tf_error)
5501 && deprecated_state != UNAVAILABLE_DEPRECATED_SUPPRESS)
5503 if (TREE_UNAVAILABLE (decl))
5505 error_unavailable_use (decl, NULL_TREE);
5506 return true;
5508 else
5510 /* Perhaps this is an unavailable typedef. */
5511 if (TYPE_P (decl)
5512 && TYPE_NAME (decl)
5513 && TREE_UNAVAILABLE (TYPE_NAME (decl)))
5515 decl = TYPE_NAME (decl);
5516 /* Don't error within members of a unavailable type. */
5517 if (TYPE_P (decl)
5518 && currently_open_class (decl))
5519 return false;
5521 error_unavailable_use (decl, NULL_TREE);
5522 return true;
5525 /* Carry on to consider deprecatedness. */
5528 if (!(complain & tf_warning)
5529 || deprecated_state == DEPRECATED_SUPPRESS
5530 || deprecated_state == UNAVAILABLE_DEPRECATED_SUPPRESS)
5531 return false;
5533 if (!TREE_DEPRECATED (decl))
5535 /* Perhaps this is a deprecated typedef. */
5536 if (TYPE_P (decl) && TYPE_NAME (decl))
5537 decl = TYPE_NAME (decl);
5539 if (!TREE_DEPRECATED (decl))
5540 return false;
5543 /* Don't warn within members of a deprecated type. */
5544 if (TYPE_P (decl)
5545 && currently_open_class (decl))
5546 return false;
5548 bool warned = false;
5549 if (cxx_dialect >= cxx11
5550 && DECL_P (decl)
5551 && DECL_ARTIFICIAL (decl)
5552 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)
5553 && copy_fn_p (decl))
5555 /* Don't warn if the flag was disabled around the class definition
5556 (c++/94492). */
5557 if (warning_enabled_at (DECL_SOURCE_LOCATION (decl),
5558 OPT_Wdeprecated_copy))
5560 auto_diagnostic_group d;
5561 tree ctx = DECL_CONTEXT (decl);
5562 tree other = classtype_has_depr_implicit_copy (ctx);
5563 int opt = (DECL_DESTRUCTOR_P (other)
5564 ? OPT_Wdeprecated_copy_dtor
5565 : OPT_Wdeprecated_copy);
5566 warned = warning (opt, "implicitly-declared %qD is deprecated",
5567 decl);
5568 if (warned)
5569 inform (DECL_SOURCE_LOCATION (other),
5570 "because %qT has user-provided %qD",
5571 ctx, other);
5574 else
5575 warned = warn_deprecated_use (decl, NULL_TREE);
5577 return warned;
5580 /* Like above, but takes into account outer scopes. */
5582 void
5583 cp_warn_deprecated_use_scopes (tree scope)
5585 while (scope
5586 && scope != error_mark_node
5587 && scope != global_namespace)
5589 if ((TREE_CODE (scope) == NAMESPACE_DECL || OVERLOAD_TYPE_P (scope))
5590 && cp_handle_deprecated_or_unavailable (scope))
5591 return;
5592 if (TYPE_P (scope))
5593 scope = CP_TYPE_CONTEXT (scope);
5594 else
5595 scope = CP_DECL_CONTEXT (scope);
5599 /* True if DECL or its enclosing scope have unbound template parameters. */
5601 bool
5602 decl_dependent_p (tree decl)
5604 if (DECL_FUNCTION_SCOPE_P (decl)
5605 || TREE_CODE (decl) == CONST_DECL
5606 || TREE_CODE (decl) == USING_DECL
5607 || TREE_CODE (decl) == FIELD_DECL)
5608 decl = CP_DECL_CONTEXT (decl);
5609 if (tree tinfo = get_template_info (decl))
5610 if (any_dependent_template_arguments_p (TI_ARGS (tinfo)))
5611 return true;
5612 if (LAMBDA_FUNCTION_P (decl)
5613 && dependent_type_p (DECL_CONTEXT (decl)))
5614 return true;
5615 return false;
5618 /* [basic.def.odr] A function is named [and therefore odr-used] by an
5619 expression or conversion if it is the selected member of an overload set in
5620 an overload resolution performed as part of forming that expression or
5621 conversion, unless it is a pure virtual function and either the expression
5622 is not an id-expression naming the function with an explicitly qualified
5623 name or the expression forms a pointer to member.
5625 Mostly, we call mark_used in places that actually do something with a
5626 function, like build_over_call. But in a few places we end up with a
5627 non-overloaded FUNCTION_DECL that we aren't going to do any more with, like
5628 convert_to_void. resolve_nondeduced_context is called in those places,
5629 but it's also called in too many other places. */
5631 bool
5632 mark_single_function (tree expr, tsubst_flags_t complain)
5634 expr = maybe_undo_parenthesized_ref (expr);
5635 expr = tree_strip_any_location_wrapper (expr);
5637 if (is_overloaded_fn (expr) == 1
5638 && !mark_used (expr, complain)
5639 && !(complain & tf_error))
5640 return false;
5641 return true;
5644 /* Mark DECL (either a _DECL or a BASELINK) as "used" in the program.
5645 If DECL is a specialization or implicitly declared class member,
5646 generate the actual definition. Return false if something goes
5647 wrong, true otherwise. */
5649 bool
5650 mark_used (tree decl, tsubst_flags_t complain /* = tf_warning_or_error */)
5652 /* If we're just testing conversions or resolving overloads, we
5653 don't want any permanent effects like forcing functions to be
5654 output or instantiating templates. */
5655 if ((complain & tf_conv))
5656 return true;
5658 /* If DECL is a BASELINK for a single function, then treat it just
5659 like the DECL for the function. Otherwise, if the BASELINK is
5660 for an overloaded function, we don't know which function was
5661 actually used until after overload resolution. */
5662 if (BASELINK_P (decl))
5664 tree fns = BASELINK_FUNCTIONS (decl);
5665 if (really_overloaded_fn (fns))
5666 return true;
5667 fns = OVL_FIRST (fns);
5668 if (!mark_used (fns, complain))
5669 return false;
5670 /* We might have deduced its return type. */
5671 TREE_TYPE (decl) = TREE_TYPE (fns);
5672 return true;
5675 if (!DECL_P (decl))
5676 return true;
5678 /* Set TREE_USED for the benefit of -Wunused. */
5679 TREE_USED (decl) = true;
5681 /* And for structured bindings also the underlying decl. */
5682 if (DECL_DECOMPOSITION_P (decl) && DECL_DECOMP_BASE (decl))
5683 TREE_USED (DECL_DECOMP_BASE (decl)) = true;
5685 if (TREE_CODE (decl) == TEMPLATE_DECL)
5686 return true;
5688 if (DECL_CLONED_FUNCTION_P (decl))
5689 TREE_USED (DECL_CLONED_FUNCTION (decl)) = 1;
5691 /* Mark enumeration types as used. */
5692 if (TREE_CODE (decl) == CONST_DECL)
5693 used_types_insert (DECL_CONTEXT (decl));
5695 if (TREE_CODE (decl) == FUNCTION_DECL)
5697 if (DECL_MAYBE_DELETED (decl))
5699 ++function_depth;
5700 maybe_synthesize_method (decl);
5701 --function_depth;
5704 if (DECL_DELETED_FN (decl))
5706 if (DECL_ARTIFICIAL (decl)
5707 && DECL_CONV_FN_P (decl)
5708 && LAMBDA_TYPE_P (DECL_CONTEXT (decl)))
5709 /* We mark a lambda conversion op as deleted if we can't
5710 generate it properly; see maybe_add_lambda_conv_op. */
5711 sorry ("converting lambda that uses %<...%> to function pointer");
5712 else if (complain & tf_error)
5714 error ("use of deleted function %qD", decl);
5715 if (!maybe_explain_implicit_delete (decl))
5716 inform (DECL_SOURCE_LOCATION (decl), "declared here");
5718 return false;
5721 if (!maybe_instantiate_noexcept (decl, complain))
5722 return false;
5725 if (VAR_OR_FUNCTION_DECL_P (decl) && DECL_LOCAL_DECL_P (decl))
5727 if (!DECL_LANG_SPECIFIC (decl))
5728 /* An unresolved dependent local extern. */
5729 return true;
5731 DECL_ODR_USED (decl) = 1;
5732 auto alias = DECL_LOCAL_DECL_ALIAS (decl);
5733 if (!alias || alias == error_mark_node)
5734 return true;
5736 /* Process the underlying decl. */
5737 decl = alias;
5738 TREE_USED (decl) = true;
5741 cp_handle_deprecated_or_unavailable (decl, complain);
5743 /* We can only check DECL_ODR_USED on variables or functions with
5744 DECL_LANG_SPECIFIC set, and these are also the only decls that we
5745 might need special handling for. */
5746 if (!VAR_OR_FUNCTION_DECL_P (decl)
5747 || DECL_LANG_SPECIFIC (decl) == NULL
5748 || DECL_THUNK_P (decl))
5750 if (!decl_dependent_p (decl)
5751 && !require_deduced_type (decl, complain))
5752 return false;
5753 return true;
5756 /* We only want to do this processing once. We don't need to keep trying
5757 to instantiate inline templates, because unit-at-a-time will make sure
5758 we get them compiled before functions that want to inline them. */
5759 if (DECL_ODR_USED (decl))
5760 return true;
5762 if (flag_concepts && TREE_CODE (decl) == FUNCTION_DECL
5763 && !constraints_satisfied_p (decl))
5765 if (complain & tf_error)
5767 auto_diagnostic_group d;
5768 error ("use of function %qD with unsatisfied constraints",
5769 decl);
5770 location_t loc = DECL_SOURCE_LOCATION (decl);
5771 inform (loc, "declared here");
5772 diagnose_constraints (loc, decl, NULL_TREE);
5774 return false;
5777 /* If DECL has a deduced return type, we need to instantiate it now to
5778 find out its type. For OpenMP user defined reductions, we need them
5779 instantiated for reduction clauses which inline them by hand directly. */
5780 if (undeduced_auto_decl (decl)
5781 || (TREE_CODE (decl) == FUNCTION_DECL
5782 && DECL_OMP_DECLARE_REDUCTION_P (decl)))
5783 maybe_instantiate_decl (decl);
5785 if (processing_template_decl || in_template_context)
5786 return true;
5788 /* Check this too in case we're within instantiate_non_dependent_expr. */
5789 if (DECL_TEMPLATE_INFO (decl)
5790 && uses_template_parms (DECL_TI_ARGS (decl)))
5791 return true;
5793 if (!require_deduced_type (decl, complain))
5794 return false;
5796 if (builtin_pack_fn_p (decl))
5798 error ("use of built-in parameter pack %qD outside of a template",
5799 DECL_NAME (decl));
5800 return false;
5803 /* If we don't need a value, then we don't need to synthesize DECL. */
5804 if (cp_unevaluated_operand || in_discarded_stmt)
5805 return true;
5807 DECL_ODR_USED (decl) = 1;
5808 if (DECL_CLONED_FUNCTION_P (decl))
5809 DECL_ODR_USED (DECL_CLONED_FUNCTION (decl)) = 1;
5811 /* DR 757: A type without linkage shall not be used as the type of a
5812 variable or function with linkage, unless
5813 o the variable or function has extern "C" linkage (7.5 [dcl.link]), or
5814 o the variable or function is not used (3.2 [basic.def.odr]) or is
5815 defined in the same translation unit. */
5816 if (cxx_dialect > cxx98
5817 && decl_linkage (decl) != lk_none
5818 && !DECL_EXTERN_C_P (decl)
5819 && !DECL_ARTIFICIAL (decl)
5820 && !decl_defined_p (decl)
5821 && no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/false))
5822 vec_safe_push (no_linkage_decls, decl);
5824 if (TREE_CODE (decl) == FUNCTION_DECL
5825 && DECL_DECLARED_INLINE_P (decl)
5826 && !DECL_INITIAL (decl)
5827 && !DECL_ARTIFICIAL (decl)
5828 && !DECL_PURE_VIRTUAL_P (decl))
5829 /* Remember it, so we can check it was defined. */
5830 note_vague_linkage_fn (decl);
5832 /* Is it a synthesized method that needs to be synthesized? */
5833 if (TREE_CODE (decl) == FUNCTION_DECL
5834 && DECL_DEFAULTED_FN (decl)
5835 /* A function defaulted outside the class is synthesized either by
5836 cp_finish_decl or instantiate_decl. */
5837 && !DECL_DEFAULTED_OUTSIDE_CLASS_P (decl)
5838 && ! DECL_INITIAL (decl))
5840 /* Remember the current location for a function we will end up
5841 synthesizing. Then we can inform the user where it was
5842 required in the case of error. */
5843 if (decl_remember_implicit_trigger_p (decl))
5844 DECL_SOURCE_LOCATION (decl) = input_location;
5846 /* Synthesizing an implicitly defined member function will result in
5847 garbage collection. We must treat this situation as if we were
5848 within the body of a function so as to avoid collecting live data
5849 on the stack (such as overload resolution candidates).
5851 We could just let c_parse_final_cleanups handle synthesizing
5852 this function by adding it to deferred_fns, but doing
5853 it at the use site produces better error messages. */
5854 ++function_depth;
5855 synthesize_method (decl);
5856 --function_depth;
5857 /* If this is a synthesized method we don't need to
5858 do the instantiation test below. */
5860 else if (VAR_OR_FUNCTION_DECL_P (decl)
5861 && DECL_TEMPLATE_INFO (decl)
5862 && !DECL_DECLARED_CONCEPT_P (decl)
5863 && (!DECL_EXPLICIT_INSTANTIATION (decl)
5864 || always_instantiate_p (decl)))
5865 /* If this is a function or variable that is an instance of some
5866 template, we now know that we will need to actually do the
5867 instantiation. We check that DECL is not an explicit
5868 instantiation because that is not checked in instantiate_decl.
5870 We put off instantiating functions in order to improve compile
5871 times. Maintaining a stack of active functions is expensive,
5872 and the inliner knows to instantiate any functions it might
5873 need. Therefore, we always try to defer instantiation. */
5875 ++function_depth;
5876 instantiate_decl (decl, /*defer_ok=*/true,
5877 /*expl_inst_class_mem_p=*/false);
5878 --function_depth;
5881 return true;
5884 tree
5885 vtv_start_verification_constructor_init_function (void)
5887 return start_objects (/*initp=*/true, MAX_RESERVED_INIT_PRIORITY - 1, true);
5890 tree
5891 vtv_finish_verification_constructor_init_function (tree body)
5893 return finish_objects (/*initp=*/true, MAX_RESERVED_INIT_PRIORITY - 1, body);
5896 #include "gt-cp-decl2.h"