Don't warn when alignment of global common data exceeds maximum alignment.
[official-gcc.git] / gcc / cp / decl2.c
blob0c9d2f468a02f2a487f3811f86e98c21e28ecc38
1 /* Process declarations and variables for C++ compiler.
2 Copyright (C) 1988-2021 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"
53 /* Id for dumping the raw trees. */
54 int raw_dump_id;
56 extern cpp_reader *parse_in;
58 /* This structure contains information about the initializations
59 and/or destructions required for a particular priority level. */
60 typedef struct priority_info_s {
61 /* Nonzero if there have been any initializations at this priority
62 throughout the translation unit. */
63 int initializations_p;
64 /* Nonzero if there have been any destructions at this priority
65 throughout the translation unit. */
66 int destructions_p;
67 } *priority_info;
69 static tree start_objects (int, int);
70 static void finish_objects (int, int, tree);
71 static tree start_static_storage_duration_function (unsigned);
72 static void finish_static_storage_duration_function (tree);
73 static priority_info get_priority_info (int);
74 static void do_static_initialization_or_destruction (tree, bool);
75 static void one_static_initialization_or_destruction (tree, tree, bool);
76 static void generate_ctor_or_dtor_function (bool, int, location_t *);
77 static int generate_ctor_and_dtor_functions_for_priority (splay_tree_node,
78 void *);
79 static tree prune_vars_needing_no_initialization (tree *);
80 static void write_out_vars (tree);
81 static void import_export_class (tree);
82 static tree get_guard_bits (tree);
83 static void determine_visibility_from_class (tree, tree);
84 static bool determine_hidden_inline (tree);
86 /* A list of static class variables. This is needed, because a
87 static class variable can be declared inside the class without
88 an initializer, and then initialized, statically, outside the class. */
89 static GTY(()) vec<tree, va_gc> *pending_statics;
91 /* A list of functions which were declared inline, but which we
92 may need to emit outline anyway. */
93 static GTY(()) vec<tree, va_gc> *deferred_fns;
95 /* A list of decls that use types with no linkage, which we need to make
96 sure are defined. */
97 static GTY(()) vec<tree, va_gc> *no_linkage_decls;
99 /* A vector of alternating decls and identifiers, where the latter
100 is to be an alias for the former if the former is defined. */
101 static GTY(()) vec<tree, va_gc> *mangling_aliases;
103 /* hash traits for declarations. Hashes single decls via
104 DECL_ASSEMBLER_NAME_RAW. */
106 struct mangled_decl_hash : ggc_remove <tree>
108 typedef tree value_type; /* A DECL. */
109 typedef tree compare_type; /* An identifier. */
111 static hashval_t hash (const value_type decl)
113 return IDENTIFIER_HASH_VALUE (DECL_ASSEMBLER_NAME_RAW (decl));
115 static bool equal (const value_type existing, compare_type candidate)
117 tree name = DECL_ASSEMBLER_NAME_RAW (existing);
118 return candidate == name;
121 static const bool empty_zero_p = true;
122 static inline void mark_empty (value_type &p) {p = NULL_TREE;}
123 static inline bool is_empty (value_type p) {return !p;}
125 static bool is_deleted (value_type e)
127 return e == reinterpret_cast <value_type> (1);
129 static void mark_deleted (value_type &e)
131 e = reinterpret_cast <value_type> (1);
135 /* A hash table of decls keyed by mangled name. Used to figure out if
136 we need compatibility aliases. */
137 static GTY(()) hash_table<mangled_decl_hash> *mangled_decls;
139 /* Nonzero if we're done parsing and into end-of-file activities. */
141 int at_eof;
143 /* True if note_mangling_alias should enqueue mangling aliases for
144 later generation, rather than emitting them right away. */
146 bool defer_mangling_aliases = true;
149 /* Return a member function type (a METHOD_TYPE), given FNTYPE (a
150 FUNCTION_TYPE), CTYPE (class type), and QUALS (the cv-qualifiers
151 that apply to the function). */
153 tree
154 build_memfn_type (tree fntype, tree ctype, cp_cv_quals quals,
155 cp_ref_qualifier rqual)
157 if (fntype == error_mark_node || ctype == error_mark_node)
158 return error_mark_node;
160 gcc_assert (FUNC_OR_METHOD_TYPE_P (fntype));
162 cp_cv_quals type_quals = quals & ~TYPE_QUAL_RESTRICT;
163 ctype = cp_build_qualified_type (ctype, type_quals);
165 tree newtype
166 = build_method_type_directly (ctype, TREE_TYPE (fntype),
167 (TREE_CODE (fntype) == METHOD_TYPE
168 ? TREE_CHAIN (TYPE_ARG_TYPES (fntype))
169 : TYPE_ARG_TYPES (fntype)));
170 if (tree attrs = TYPE_ATTRIBUTES (fntype))
171 newtype = cp_build_type_attribute_variant (newtype, attrs);
172 newtype = build_cp_fntype_variant (newtype, rqual,
173 TYPE_RAISES_EXCEPTIONS (fntype),
174 TYPE_HAS_LATE_RETURN_TYPE (fntype));
176 return newtype;
179 /* Return a variant of FNTYPE, a FUNCTION_TYPE or METHOD_TYPE, with its
180 return type changed to NEW_RET. */
182 tree
183 change_return_type (tree new_ret, tree fntype)
185 if (new_ret == error_mark_node)
186 return fntype;
188 if (same_type_p (new_ret, TREE_TYPE (fntype)))
189 return fntype;
191 tree newtype;
192 tree args = TYPE_ARG_TYPES (fntype);
194 if (TREE_CODE (fntype) == FUNCTION_TYPE)
196 newtype = build_function_type (new_ret, args);
197 newtype = apply_memfn_quals (newtype,
198 type_memfn_quals (fntype));
200 else
201 newtype = build_method_type_directly
202 (class_of_this_parm (fntype), new_ret, TREE_CHAIN (args));
204 if (tree attrs = TYPE_ATTRIBUTES (fntype))
205 newtype = cp_build_type_attribute_variant (newtype, attrs);
206 newtype = cxx_copy_lang_qualifiers (newtype, fntype);
208 return newtype;
211 /* Build a PARM_DECL of FN with NAME and TYPE, and set DECL_ARG_TYPE
212 appropriately. */
214 tree
215 cp_build_parm_decl (tree fn, tree name, tree type)
217 tree parm = build_decl (input_location,
218 PARM_DECL, name, type);
219 DECL_CONTEXT (parm) = fn;
221 /* DECL_ARG_TYPE is only used by the back end and the back end never
222 sees templates. */
223 if (!processing_template_decl)
224 DECL_ARG_TYPE (parm) = type_passed_as (type);
226 return parm;
229 /* Returns a PARM_DECL of FN for a parameter of the indicated TYPE, with the
230 indicated NAME. */
232 tree
233 build_artificial_parm (tree fn, tree name, tree type)
235 tree parm = cp_build_parm_decl (fn, name, type);
236 DECL_ARTIFICIAL (parm) = 1;
237 /* All our artificial parms are implicitly `const'; they cannot be
238 assigned to. */
239 TREE_READONLY (parm) = 1;
240 return parm;
243 /* Constructors for types with virtual baseclasses need an "in-charge" flag
244 saying whether this constructor is responsible for initialization of
245 virtual baseclasses or not. All destructors also need this "in-charge"
246 flag, which additionally determines whether or not the destructor should
247 free the memory for the object.
249 This function adds the "in-charge" flag to member function FN if
250 appropriate. It is called from grokclassfn and tsubst.
251 FN must be either a constructor or destructor.
253 The in-charge flag follows the 'this' parameter, and is followed by the
254 VTT parm (if any), then the user-written parms. */
256 void
257 maybe_retrofit_in_chrg (tree fn)
259 tree basetype, arg_types, parms, parm, fntype;
261 /* If we've already add the in-charge parameter don't do it again. */
262 if (DECL_HAS_IN_CHARGE_PARM_P (fn))
263 return;
265 /* When processing templates we can't know, in general, whether or
266 not we're going to have virtual baseclasses. */
267 if (processing_template_decl)
268 return;
270 /* We don't need an in-charge parameter for constructors that don't
271 have virtual bases. */
272 if (DECL_CONSTRUCTOR_P (fn)
273 && !CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fn)))
274 return;
276 arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
277 basetype = TREE_TYPE (TREE_VALUE (arg_types));
278 arg_types = TREE_CHAIN (arg_types);
280 parms = DECL_CHAIN (DECL_ARGUMENTS (fn));
282 /* If this is a subobject constructor or destructor, our caller will
283 pass us a pointer to our VTT. */
284 if (CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fn)))
286 parm = build_artificial_parm (fn, vtt_parm_identifier, vtt_parm_type);
288 /* First add it to DECL_ARGUMENTS between 'this' and the real args... */
289 DECL_CHAIN (parm) = parms;
290 parms = parm;
292 /* ...and then to TYPE_ARG_TYPES. */
293 arg_types = hash_tree_chain (vtt_parm_type, arg_types);
295 DECL_HAS_VTT_PARM_P (fn) = 1;
298 /* Then add the in-charge parm (before the VTT parm). */
299 parm = build_artificial_parm (fn, in_charge_identifier, integer_type_node);
300 DECL_CHAIN (parm) = parms;
301 parms = parm;
302 arg_types = hash_tree_chain (integer_type_node, arg_types);
304 /* Insert our new parameter(s) into the list. */
305 DECL_CHAIN (DECL_ARGUMENTS (fn)) = parms;
307 /* And rebuild the function type. */
308 fntype = build_method_type_directly (basetype, TREE_TYPE (TREE_TYPE (fn)),
309 arg_types);
310 if (TYPE_ATTRIBUTES (TREE_TYPE (fn)))
311 fntype = (cp_build_type_attribute_variant
312 (fntype, TYPE_ATTRIBUTES (TREE_TYPE (fn))));
313 fntype = cxx_copy_lang_qualifiers (fntype, TREE_TYPE (fn));
314 TREE_TYPE (fn) = fntype;
316 /* Now we've got the in-charge parameter. */
317 DECL_HAS_IN_CHARGE_PARM_P (fn) = 1;
320 /* Classes overload their constituent function names automatically.
321 When a function name is declared in a record structure,
322 its name is changed to it overloaded name. Since names for
323 constructors and destructors can conflict, we place a leading
324 '$' for destructors.
326 CNAME is the name of the class we are grokking for.
328 FUNCTION is a FUNCTION_DECL. It was created by `grokdeclarator'.
330 FLAGS contains bits saying what's special about today's
331 arguments. DTOR_FLAG == DESTRUCTOR.
333 If FUNCTION is a destructor, then we must add the `auto-delete' field
334 as a second parameter. There is some hair associated with the fact
335 that we must "declare" this variable in the manner consistent with the
336 way the rest of the arguments were declared.
338 QUALS are the qualifiers for the this pointer. */
340 void
341 grokclassfn (tree ctype, tree function, enum overload_flags flags)
343 tree fn_name = DECL_NAME (function);
345 /* Even within an `extern "C"' block, members get C++ linkage. See
346 [dcl.link] for details. */
347 SET_DECL_LANGUAGE (function, lang_cplusplus);
349 if (fn_name == NULL_TREE)
351 error ("name missing for member function");
352 fn_name = get_identifier ("<anonymous>");
353 DECL_NAME (function) = fn_name;
356 DECL_CONTEXT (function) = ctype;
358 if (flags == DTOR_FLAG)
359 DECL_CXX_DESTRUCTOR_P (function) = 1;
361 if (flags == DTOR_FLAG || DECL_CONSTRUCTOR_P (function))
362 maybe_retrofit_in_chrg (function);
365 /* Create an ARRAY_REF, checking for the user doing things backwards
366 along the way. DECLTYPE_P is for N3276, as in the parser. */
368 tree
369 grok_array_decl (location_t loc, tree array_expr, tree index_exp,
370 bool decltype_p)
372 tree type;
373 tree expr;
374 tree orig_array_expr = array_expr;
375 tree orig_index_exp = index_exp;
376 tree overload = NULL_TREE;
378 if (error_operand_p (array_expr) || error_operand_p (index_exp))
379 return error_mark_node;
381 if (processing_template_decl)
383 if (type_dependent_expression_p (array_expr)
384 || type_dependent_expression_p (index_exp))
385 return build_min_nt_loc (loc, ARRAY_REF, array_expr, index_exp,
386 NULL_TREE, NULL_TREE);
387 array_expr = build_non_dependent_expr (array_expr);
388 index_exp = build_non_dependent_expr (index_exp);
391 type = TREE_TYPE (array_expr);
392 gcc_assert (type);
393 type = non_reference (type);
395 /* If they have an `operator[]', use that. */
396 if (MAYBE_CLASS_TYPE_P (type) || MAYBE_CLASS_TYPE_P (TREE_TYPE (index_exp)))
398 tsubst_flags_t complain = tf_warning_or_error;
399 if (decltype_p)
400 complain |= tf_decltype;
401 expr = build_new_op (loc, ARRAY_REF, LOOKUP_NORMAL, array_expr,
402 index_exp, NULL_TREE, &overload, complain);
404 else
406 tree p1, p2, i1, i2;
407 bool swapped = false;
409 /* Otherwise, create an ARRAY_REF for a pointer or array type.
410 It is a little-known fact that, if `a' is an array and `i' is
411 an int, you can write `i[a]', which means the same thing as
412 `a[i]'. */
413 if (TREE_CODE (type) == ARRAY_TYPE || VECTOR_TYPE_P (type))
414 p1 = array_expr;
415 else
416 p1 = build_expr_type_conversion (WANT_POINTER, array_expr, false);
418 if (TREE_CODE (TREE_TYPE (index_exp)) == ARRAY_TYPE)
419 p2 = index_exp;
420 else
421 p2 = build_expr_type_conversion (WANT_POINTER, index_exp, false);
423 i1 = build_expr_type_conversion (WANT_INT | WANT_ENUM, array_expr,
424 false);
425 i2 = build_expr_type_conversion (WANT_INT | WANT_ENUM, index_exp,
426 false);
428 if ((p1 && i2) && (i1 && p2))
429 error ("ambiguous conversion for array subscript");
431 if (p1 && i2)
432 array_expr = p1, index_exp = i2;
433 else if (i1 && p2)
434 swapped = true, array_expr = p2, index_exp = i1;
435 else
437 error_at (loc, "invalid types %<%T[%T]%> for array subscript",
438 type, TREE_TYPE (index_exp));
439 return error_mark_node;
442 if (array_expr == error_mark_node || index_exp == error_mark_node)
443 error ("ambiguous conversion for array subscript");
445 if (TYPE_PTR_P (TREE_TYPE (array_expr)))
446 array_expr = mark_rvalue_use (array_expr);
447 else
448 array_expr = mark_lvalue_use_nonread (array_expr);
449 index_exp = mark_rvalue_use (index_exp);
450 if (swapped
451 && flag_strong_eval_order == 2
452 && (TREE_SIDE_EFFECTS (array_expr) || TREE_SIDE_EFFECTS (index_exp)))
453 expr = build_array_ref (input_location, index_exp, array_expr);
454 else
455 expr = build_array_ref (input_location, array_expr, index_exp);
457 if (processing_template_decl && expr != error_mark_node)
459 if (overload != NULL_TREE)
460 return (build_min_non_dep_op_overload
461 (ARRAY_REF, expr, overload, orig_array_expr, orig_index_exp));
463 return build_min_non_dep (ARRAY_REF, expr, orig_array_expr, orig_index_exp,
464 NULL_TREE, NULL_TREE);
466 return expr;
469 /* Given the cast expression EXP, checking out its validity. Either return
470 an error_mark_node if there was an unavoidable error, return a cast to
471 void for trying to delete a pointer w/ the value 0, or return the
472 call to delete. If DOING_VEC is true, we handle things differently
473 for doing an array delete.
474 Implements ARM $5.3.4. This is called from the parser. */
476 tree
477 delete_sanity (location_t loc, tree exp, tree size, bool doing_vec,
478 int use_global_delete, tsubst_flags_t complain)
480 tree t, type;
482 if (exp == error_mark_node)
483 return exp;
485 if (processing_template_decl)
487 t = build_min (DELETE_EXPR, void_type_node, exp, size);
488 DELETE_EXPR_USE_GLOBAL (t) = use_global_delete;
489 DELETE_EXPR_USE_VEC (t) = doing_vec;
490 TREE_SIDE_EFFECTS (t) = 1;
491 SET_EXPR_LOCATION (t, loc);
492 return t;
495 location_t exp_loc = cp_expr_loc_or_loc (exp, loc);
497 /* An array can't have been allocated by new, so complain. */
498 if (TREE_CODE (TREE_TYPE (exp)) == ARRAY_TYPE
499 && (complain & tf_warning))
500 warning_at (exp_loc, 0, "deleting array %q#E", exp);
502 t = build_expr_type_conversion (WANT_POINTER, exp, true);
504 if (t == NULL_TREE || t == error_mark_node)
506 if (complain & tf_error)
507 error_at (exp_loc,
508 "type %q#T argument given to %<delete%>, expected pointer",
509 TREE_TYPE (exp));
510 return error_mark_node;
513 type = TREE_TYPE (t);
515 /* As of Valley Forge, you can delete a pointer to const. */
517 /* You can't delete functions. */
518 if (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
520 if (complain & tf_error)
521 error_at (exp_loc,
522 "cannot delete a function. Only pointer-to-objects are "
523 "valid arguments to %<delete%>");
524 return error_mark_node;
527 /* Deleting ptr to void is undefined behavior [expr.delete/3]. */
528 if (VOID_TYPE_P (TREE_TYPE (type)))
530 if (complain & tf_warning)
531 warning_at (exp_loc, OPT_Wdelete_incomplete,
532 "deleting %qT is undefined", type);
533 doing_vec = 0;
536 /* Deleting a pointer with the value zero is valid and has no effect. */
537 if (integer_zerop (t))
538 return build1_loc (loc, NOP_EXPR, void_type_node, t);
540 if (doing_vec)
541 return build_vec_delete (loc, t, /*maxindex=*/NULL_TREE,
542 sfk_deleting_destructor,
543 use_global_delete, complain);
544 else
545 return build_delete (loc, type, t, sfk_deleting_destructor,
546 LOOKUP_NORMAL, use_global_delete,
547 complain);
550 /* Report an error if the indicated template declaration is not the
551 sort of thing that should be a member template. */
553 void
554 check_member_template (tree tmpl)
556 tree decl;
558 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
559 decl = DECL_TEMPLATE_RESULT (tmpl);
561 if (TREE_CODE (decl) == FUNCTION_DECL
562 || DECL_ALIAS_TEMPLATE_P (tmpl)
563 || (TREE_CODE (decl) == TYPE_DECL
564 && MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))))
566 /* The parser rejects template declarations in local classes
567 (with the exception of generic lambdas). */
568 gcc_assert (!current_function_decl || LAMBDA_FUNCTION_P (decl));
569 /* The parser rejects any use of virtual in a function template. */
570 gcc_assert (!(TREE_CODE (decl) == FUNCTION_DECL
571 && DECL_VIRTUAL_P (decl)));
573 /* The debug-information generating code doesn't know what to do
574 with member templates. */
575 DECL_IGNORED_P (tmpl) = 1;
577 else if (variable_template_p (tmpl))
578 /* OK */;
579 else
580 error ("template declaration of %q#D", decl);
583 /* Sanity check: report error if this function FUNCTION is not
584 really a member of the class (CTYPE) it is supposed to belong to.
585 TEMPLATE_PARMS is used to specify the template parameters of a member
586 template passed as FUNCTION_DECL. If the member template is passed as a
587 TEMPLATE_DECL, it can be NULL since the parameters can be extracted
588 from the declaration. If the function is not a function template, it
589 must be NULL.
590 It returns the original declaration for the function, NULL_TREE if
591 no declaration was found, error_mark_node if an error was emitted. */
593 tree
594 check_classfn (tree ctype, tree function, tree template_parms)
596 if (DECL_USE_TEMPLATE (function)
597 && !(TREE_CODE (function) == TEMPLATE_DECL
598 && DECL_TEMPLATE_SPECIALIZATION (function))
599 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (function)))
600 /* Since this is a specialization of a member template,
601 we're not going to find the declaration in the class.
602 For example, in:
604 struct S { template <typename T> void f(T); };
605 template <> void S::f(int);
607 we're not going to find `S::f(int)', but there's no
608 reason we should, either. We let our callers know we didn't
609 find the method, but we don't complain. */
610 return NULL_TREE;
612 /* Basic sanity check: for a template function, the template parameters
613 either were not passed, or they are the same of DECL_TEMPLATE_PARMS. */
614 if (TREE_CODE (function) == TEMPLATE_DECL)
616 if (template_parms
617 && !comp_template_parms (template_parms,
618 DECL_TEMPLATE_PARMS (function)))
620 error ("template parameter lists provided don%'t match the "
621 "template parameters of %qD", function);
622 return error_mark_node;
624 template_parms = DECL_TEMPLATE_PARMS (function);
627 /* OK, is this a definition of a member template? */
628 bool is_template = (template_parms != NULL_TREE);
630 /* [temp.mem]
632 A destructor shall not be a member template. */
633 if (DECL_DESTRUCTOR_P (function) && is_template)
635 error ("destructor %qD declared as member template", function);
636 return error_mark_node;
639 /* We must enter the scope here, because conversion operators are
640 named by target type, and type equivalence relies on typenames
641 resolving within the scope of CTYPE. */
642 tree pushed_scope = push_scope (ctype);
643 tree matched = NULL_TREE;
644 tree fns = get_class_binding (ctype, DECL_NAME (function));
646 for (ovl_iterator iter (fns); !matched && iter; ++iter)
648 tree fndecl = *iter;
650 /* A member template definition only matches a member template
651 declaration. */
652 if (is_template != (TREE_CODE (fndecl) == TEMPLATE_DECL))
653 continue;
655 if (!DECL_DECLARES_FUNCTION_P (fndecl))
656 continue;
658 tree p1 = TYPE_ARG_TYPES (TREE_TYPE (function));
659 tree p2 = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
661 /* We cannot simply call decls_match because this doesn't work
662 for static member functions that are pretending to be
663 methods, and because the name may have been changed by
664 asm("new_name"). */
666 /* Get rid of the this parameter on functions that become
667 static. */
668 if (DECL_STATIC_FUNCTION_P (fndecl)
669 && TREE_CODE (TREE_TYPE (function)) == METHOD_TYPE)
670 p1 = TREE_CHAIN (p1);
672 /* ref-qualifier or absence of same must match. */
673 if (type_memfn_rqual (TREE_TYPE (function))
674 != type_memfn_rqual (TREE_TYPE (fndecl)))
675 continue;
677 // Include constraints in the match.
678 tree c1 = get_constraints (function);
679 tree c2 = get_constraints (fndecl);
681 /* While finding a match, same types and params are not enough
682 if the function is versioned. Also check version ("target")
683 attributes. */
684 if (same_type_p (TREE_TYPE (TREE_TYPE (function)),
685 TREE_TYPE (TREE_TYPE (fndecl)))
686 && compparms (p1, p2)
687 && !targetm.target_option.function_versions (function, fndecl)
688 && (!is_template
689 || comp_template_parms (template_parms,
690 DECL_TEMPLATE_PARMS (fndecl)))
691 && equivalent_constraints (c1, c2)
692 && (DECL_TEMPLATE_SPECIALIZATION (function)
693 == DECL_TEMPLATE_SPECIALIZATION (fndecl))
694 && (!DECL_TEMPLATE_SPECIALIZATION (function)
695 || (DECL_TI_TEMPLATE (function) == DECL_TI_TEMPLATE (fndecl))))
696 matched = fndecl;
699 if (!matched)
701 if (!COMPLETE_TYPE_P (ctype))
702 cxx_incomplete_type_error (DECL_SOURCE_LOCATION (function),
703 function, ctype);
704 else
706 if (DECL_CONV_FN_P (function))
707 fns = get_class_binding (ctype, conv_op_identifier);
709 error_at (DECL_SOURCE_LOCATION (function),
710 "no declaration matches %q#D", function);
711 if (fns)
712 print_candidates (fns);
713 else if (DECL_CONV_FN_P (function))
714 inform (DECL_SOURCE_LOCATION (function),
715 "no conversion operators declared");
716 else
717 inform (DECL_SOURCE_LOCATION (function),
718 "no functions named %qD", function);
719 inform (DECL_SOURCE_LOCATION (TYPE_NAME (ctype)),
720 "%#qT defined here", ctype);
722 matched = error_mark_node;
725 if (pushed_scope)
726 pop_scope (pushed_scope);
728 return matched;
731 /* DECL is a function with vague linkage. Remember it so that at the
732 end of the translation unit we can decide whether or not to emit
733 it. */
735 void
736 note_vague_linkage_fn (tree decl)
738 if (processing_template_decl)
739 return;
741 DECL_DEFER_OUTPUT (decl) = 1;
742 vec_safe_push (deferred_fns, decl);
745 /* As above, but for variable template instantiations. */
747 void
748 note_variable_template_instantiation (tree decl)
750 vec_safe_push (pending_statics, decl);
753 /* We have just processed the DECL, which is a static data member.
754 The other parameters are as for cp_finish_decl. */
756 void
757 finish_static_data_member_decl (tree decl,
758 tree init, bool init_const_expr_p,
759 tree asmspec_tree,
760 int flags)
762 if (DECL_TEMPLATE_INSTANTIATED (decl))
763 /* We already needed to instantiate this, so the processing in this
764 function is unnecessary/wrong. */
765 return;
767 DECL_CONTEXT (decl) = current_class_type;
769 /* We cannot call pushdecl here, because that would fill in the
770 TREE_CHAIN of our decl. Instead, we modify cp_finish_decl to do
771 the right thing, namely, to put this decl out straight away. */
773 if (! processing_template_decl)
774 vec_safe_push (pending_statics, decl);
776 if (LOCAL_CLASS_P (current_class_type)
777 /* We already complained about the template definition. */
778 && !DECL_TEMPLATE_INSTANTIATION (decl))
779 permerror (DECL_SOURCE_LOCATION (decl),
780 "local class %q#T shall not have static data member %q#D",
781 current_class_type, decl);
782 else
783 for (tree t = current_class_type; TYPE_P (t);
784 t = CP_TYPE_CONTEXT (t))
785 if (TYPE_UNNAMED_P (t))
787 auto_diagnostic_group d;
788 if (permerror (DECL_SOURCE_LOCATION (decl),
789 "static data member %qD in unnamed class", decl))
790 inform (DECL_SOURCE_LOCATION (TYPE_NAME (t)),
791 "unnamed class defined here");
792 break;
795 if (DECL_INLINE_VAR_P (decl) && !DECL_TEMPLATE_INSTANTIATION (decl))
796 /* An inline variable is immediately defined, so don't set DECL_IN_AGGR_P.
797 Except that if decl is a template instantiation, it isn't defined until
798 instantiate_decl. */;
799 else
800 DECL_IN_AGGR_P (decl) = 1;
802 if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
803 && TYPE_DOMAIN (TREE_TYPE (decl)) == NULL_TREE)
804 SET_VAR_HAD_UNKNOWN_BOUND (decl);
806 if (init)
808 /* Similarly to start_decl_1, we want to complete the type in order
809 to do the right thing in cp_apply_type_quals_to_decl, possibly
810 clear TYPE_QUAL_CONST (c++/65579). */
811 tree type = TREE_TYPE (decl) = complete_type (TREE_TYPE (decl));
812 cp_apply_type_quals_to_decl (cp_type_quals (type), decl);
815 cp_finish_decl (decl, init, init_const_expr_p, asmspec_tree, flags);
818 /* DECLARATOR and DECLSPECS correspond to a class member. The other
819 parameters are as for cp_finish_decl. Return the DECL for the
820 class member declared. */
822 tree
823 grokfield (const cp_declarator *declarator,
824 cp_decl_specifier_seq *declspecs,
825 tree init, bool init_const_expr_p,
826 tree asmspec_tree,
827 tree attrlist)
829 tree value;
830 const char *asmspec = 0;
831 int flags;
833 if (init
834 && TREE_CODE (init) == TREE_LIST
835 && TREE_VALUE (init) == error_mark_node
836 && TREE_CHAIN (init) == NULL_TREE)
837 init = NULL_TREE;
839 int initialized;
840 if (init == ridpointers[(int)RID_DELETE])
841 initialized = SD_DELETED;
842 else if (init == ridpointers[(int)RID_DEFAULT])
843 initialized = SD_DEFAULTED;
844 else if (init)
845 initialized = SD_INITIALIZED;
846 else
847 initialized = SD_UNINITIALIZED;
849 value = grokdeclarator (declarator, declspecs, FIELD, initialized, &attrlist);
850 if (! value || value == error_mark_node)
851 /* friend or constructor went bad. */
852 return error_mark_node;
853 if (TREE_TYPE (value) == error_mark_node)
854 return value;
856 if (TREE_CODE (value) == TYPE_DECL && init)
858 error_at (cp_expr_loc_or_loc (init, DECL_SOURCE_LOCATION (value)),
859 "typedef %qD is initialized (use %qs instead)",
860 value, "decltype");
861 init = NULL_TREE;
864 /* Pass friendly classes back. */
865 if (value == void_type_node)
866 return value;
868 if (DECL_NAME (value)
869 && TREE_CODE (DECL_NAME (value)) == TEMPLATE_ID_EXPR)
871 error_at (declarator->id_loc,
872 "explicit template argument list not allowed");
873 return error_mark_node;
876 /* Stash away type declarations. */
877 if (TREE_CODE (value) == TYPE_DECL)
879 DECL_NONLOCAL (value) = 1;
880 DECL_CONTEXT (value) = current_class_type;
882 if (attrlist)
884 int attrflags = 0;
886 /* If this is a typedef that names the class for linkage purposes
887 (7.1.3p8), apply any attributes directly to the type. */
888 if (OVERLOAD_TYPE_P (TREE_TYPE (value))
889 && value == TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (value))))
890 attrflags = ATTR_FLAG_TYPE_IN_PLACE;
892 cplus_decl_attributes (&value, attrlist, attrflags);
895 if (decl_spec_seq_has_spec_p (declspecs, ds_typedef)
896 && TREE_TYPE (value) != error_mark_node
897 && TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (value))) != value)
898 set_underlying_type (value);
900 /* It's important that push_template_decl below follows
901 set_underlying_type above so that the created template
902 carries the properly set type of VALUE. */
903 if (processing_template_decl)
904 value = push_template_decl (value);
906 record_locally_defined_typedef (value);
907 return value;
910 int friendp = decl_spec_seq_has_spec_p (declspecs, ds_friend);
912 if (!friendp && DECL_IN_AGGR_P (value))
914 error ("%qD is already defined in %qT", value, DECL_CONTEXT (value));
915 return void_type_node;
918 if (asmspec_tree && asmspec_tree != error_mark_node)
919 asmspec = TREE_STRING_POINTER (asmspec_tree);
921 if (init)
923 if (TREE_CODE (value) == FUNCTION_DECL)
925 if (init == ridpointers[(int)RID_DELETE])
927 DECL_DELETED_FN (value) = 1;
928 DECL_DECLARED_INLINE_P (value) = 1;
930 else if (init == ridpointers[(int)RID_DEFAULT])
932 if (defaultable_fn_check (value))
934 DECL_DEFAULTED_FN (value) = 1;
935 DECL_INITIALIZED_IN_CLASS_P (value) = 1;
936 DECL_DECLARED_INLINE_P (value) = 1;
937 /* grokfndecl set this to error_mark_node, but we want to
938 leave it unset until synthesize_method. */
939 DECL_INITIAL (value) = NULL_TREE;
942 else if (TREE_CODE (init) == DEFERRED_PARSE)
943 error ("invalid initializer for member function %qD", value);
944 else if (TREE_CODE (TREE_TYPE (value)) == METHOD_TYPE)
946 if (integer_zerop (init))
947 DECL_PURE_VIRTUAL_P (value) = 1;
948 else if (error_operand_p (init))
949 ; /* An error has already been reported. */
950 else
951 error ("invalid initializer for member function %qD",
952 value);
954 else
956 gcc_assert (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE);
957 location_t iloc
958 = cp_expr_loc_or_loc (init, DECL_SOURCE_LOCATION (value));
959 if (friendp)
960 error_at (iloc, "initializer specified for friend "
961 "function %qD", value);
962 else
963 error_at (iloc, "initializer specified for static "
964 "member function %qD", value);
967 else if (TREE_CODE (value) == FIELD_DECL)
968 /* C++11 NSDMI, keep going. */;
969 else if (!VAR_P (value))
970 gcc_unreachable ();
973 /* Pass friend decls back. */
974 if ((TREE_CODE (value) == FUNCTION_DECL
975 || TREE_CODE (value) == TEMPLATE_DECL)
976 && DECL_CONTEXT (value) != current_class_type)
978 if (attrlist)
979 cplus_decl_attributes (&value, attrlist, 0);
980 return value;
983 /* Need to set this before push_template_decl. */
984 if (VAR_P (value))
985 DECL_CONTEXT (value) = current_class_type;
987 if (processing_template_decl && VAR_OR_FUNCTION_DECL_P (value))
989 value = push_template_decl (value);
990 if (error_operand_p (value))
991 return error_mark_node;
994 if (attrlist)
995 cplus_decl_attributes (&value, attrlist, 0);
997 if (init && DIRECT_LIST_INIT_P (init))
998 flags = LOOKUP_NORMAL;
999 else
1000 flags = LOOKUP_IMPLICIT;
1002 switch (TREE_CODE (value))
1004 case VAR_DECL:
1005 finish_static_data_member_decl (value, init, init_const_expr_p,
1006 asmspec_tree, flags);
1007 return value;
1009 case FIELD_DECL:
1010 if (asmspec)
1011 error ("%<asm%> specifiers are not permitted on non-static data members");
1012 if (DECL_INITIAL (value) == error_mark_node)
1013 init = error_mark_node;
1014 cp_finish_decl (value, init, /*init_const_expr_p=*/false,
1015 NULL_TREE, flags);
1016 DECL_IN_AGGR_P (value) = 1;
1017 return value;
1019 case FUNCTION_DECL:
1020 if (asmspec)
1021 set_user_assembler_name (value, asmspec);
1023 cp_finish_decl (value,
1024 /*init=*/NULL_TREE,
1025 /*init_const_expr_p=*/false,
1026 asmspec_tree, flags);
1028 /* Pass friends back this way. */
1029 if (DECL_UNIQUE_FRIEND_P (value))
1030 return void_type_node;
1032 DECL_IN_AGGR_P (value) = 1;
1033 return value;
1035 default:
1036 gcc_unreachable ();
1038 return NULL_TREE;
1041 /* Like `grokfield', but for bitfields.
1042 WIDTH is the width of the bitfield, a constant expression.
1043 The other parameters are as for grokfield. */
1045 tree
1046 grokbitfield (const cp_declarator *declarator,
1047 cp_decl_specifier_seq *declspecs, tree width, tree init,
1048 tree attrlist)
1050 tree value = grokdeclarator (declarator, declspecs, BITFIELD,
1051 init != NULL_TREE, &attrlist);
1053 if (value == error_mark_node)
1054 return NULL_TREE; /* friends went bad. */
1056 tree type = TREE_TYPE (value);
1057 if (type == error_mark_node)
1058 return value;
1060 /* Pass friendly classes back. */
1061 if (VOID_TYPE_P (value))
1062 return void_type_node;
1064 if (!INTEGRAL_OR_ENUMERATION_TYPE_P (type)
1065 && (INDIRECT_TYPE_P (type) || !dependent_type_p (type)))
1067 error_at (DECL_SOURCE_LOCATION (value),
1068 "bit-field %qD with non-integral type %qT",
1069 value, type);
1070 return error_mark_node;
1073 if (TREE_CODE (value) == TYPE_DECL)
1075 error_at (DECL_SOURCE_LOCATION (value),
1076 "cannot declare %qD to be a bit-field type", value);
1077 return NULL_TREE;
1080 /* Usually, finish_struct_1 catches bitfields with invalid types.
1081 But, in the case of bitfields with function type, we confuse
1082 ourselves into thinking they are member functions, so we must
1083 check here. */
1084 if (TREE_CODE (value) == FUNCTION_DECL)
1086 error_at (DECL_SOURCE_LOCATION (value),
1087 "cannot declare bit-field %qD with function type", value);
1088 return NULL_TREE;
1091 if (TYPE_WARN_IF_NOT_ALIGN (type))
1093 error_at (DECL_SOURCE_LOCATION (value), "cannot declare bit-field "
1094 "%qD with %<warn_if_not_aligned%> type", value);
1095 return NULL_TREE;
1098 if (DECL_IN_AGGR_P (value))
1100 error ("%qD is already defined in the class %qT", value,
1101 DECL_CONTEXT (value));
1102 return void_type_node;
1105 if (TREE_STATIC (value))
1107 error_at (DECL_SOURCE_LOCATION (value),
1108 "static member %qD cannot be a bit-field", value);
1109 return NULL_TREE;
1112 int flags = LOOKUP_IMPLICIT;
1113 if (init && DIRECT_LIST_INIT_P (init))
1114 flags = LOOKUP_NORMAL;
1115 cp_finish_decl (value, init, false, NULL_TREE, flags);
1117 if (width != error_mark_node)
1119 /* The width must be an integer type. */
1120 if (!type_dependent_expression_p (width)
1121 && !INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (width)))
1122 error ("width of bit-field %qD has non-integral type %qT", value,
1123 TREE_TYPE (width));
1124 else if (!check_for_bare_parameter_packs (width))
1126 /* Temporarily stash the width in DECL_BIT_FIELD_REPRESENTATIVE.
1127 check_bitfield_decl picks it from there later and sets DECL_SIZE
1128 accordingly. */
1129 DECL_BIT_FIELD_REPRESENTATIVE (value) = width;
1130 SET_DECL_C_BIT_FIELD (value);
1134 DECL_IN_AGGR_P (value) = 1;
1136 if (attrlist)
1137 cplus_decl_attributes (&value, attrlist, /*flags=*/0);
1139 return value;
1143 /* Returns true iff ATTR is an attribute which needs to be applied at
1144 instantiation time rather than template definition time. */
1146 static bool
1147 is_late_template_attribute (tree attr, tree decl)
1149 tree name = get_attribute_name (attr);
1150 tree args = TREE_VALUE (attr);
1151 const struct attribute_spec *spec = lookup_attribute_spec (name);
1152 tree arg;
1154 if (!spec)
1155 /* Unknown attribute. */
1156 return false;
1158 /* Attribute weak handling wants to write out assembly right away. */
1159 if (is_attribute_p ("weak", name))
1160 return true;
1162 /* Attributes used and unused are applied directly to typedefs for the
1163 benefit of maybe_warn_unused_local_typedefs. */
1164 if (TREE_CODE (decl) == TYPE_DECL
1165 && (is_attribute_p ("unused", name)
1166 || is_attribute_p ("used", name)))
1167 return false;
1169 /* Attribute tls_model wants to modify the symtab. */
1170 if (is_attribute_p ("tls_model", name))
1171 return true;
1173 /* #pragma omp declare simd attribute needs to be always deferred. */
1174 if (flag_openmp
1175 && is_attribute_p ("omp declare simd", name))
1176 return true;
1178 if (args == error_mark_node)
1179 return false;
1181 /* An attribute pack is clearly dependent. */
1182 if (args && PACK_EXPANSION_P (args))
1183 return true;
1185 /* If any of the arguments are dependent expressions, we can't evaluate
1186 the attribute until instantiation time. */
1187 for (arg = args; arg; arg = TREE_CHAIN (arg))
1189 tree t = TREE_VALUE (arg);
1191 /* If the first attribute argument is an identifier, only consider
1192 second and following arguments. Attributes like mode, format,
1193 cleanup and several target specific attributes aren't late
1194 just because they have an IDENTIFIER_NODE as first argument. */
1195 if (arg == args && attribute_takes_identifier_p (name)
1196 && identifier_p (t))
1197 continue;
1199 if (value_dependent_expression_p (t))
1200 return true;
1203 if (TREE_CODE (decl) == TYPE_DECL
1204 || TYPE_P (decl)
1205 || spec->type_required)
1207 tree type = TYPE_P (decl) ? decl : TREE_TYPE (decl);
1209 /* We can't apply any attributes to a completely unknown type until
1210 instantiation time. */
1211 enum tree_code code = TREE_CODE (type);
1212 if (code == TEMPLATE_TYPE_PARM
1213 || code == BOUND_TEMPLATE_TEMPLATE_PARM
1214 || code == TYPENAME_TYPE)
1215 return true;
1216 /* Also defer most attributes on dependent types. This is not
1217 necessary in all cases, but is the better default. */
1218 else if (dependent_type_p (type)
1219 /* But some attributes specifically apply to templates. */
1220 && !is_attribute_p ("abi_tag", name)
1221 && !is_attribute_p ("deprecated", name)
1222 && !is_attribute_p ("visibility", name))
1223 return true;
1224 else
1225 return false;
1227 else
1228 return false;
1231 /* ATTR_P is a list of attributes. Remove any attributes which need to be
1232 applied at instantiation time and return them. If IS_DEPENDENT is true,
1233 the declaration itself is dependent, so all attributes should be applied
1234 at instantiation time. */
1236 tree
1237 splice_template_attributes (tree *attr_p, tree decl)
1239 tree *p = attr_p;
1240 tree late_attrs = NULL_TREE;
1241 tree *q = &late_attrs;
1243 if (!p)
1244 return NULL_TREE;
1246 for (; *p; )
1248 if (is_late_template_attribute (*p, decl))
1250 ATTR_IS_DEPENDENT (*p) = 1;
1251 *q = *p;
1252 *p = TREE_CHAIN (*p);
1253 q = &TREE_CHAIN (*q);
1254 *q = NULL_TREE;
1256 else
1257 p = &TREE_CHAIN (*p);
1260 return late_attrs;
1263 /* Remove any late attributes from the list in ATTR_P and attach them to
1264 DECL_P. */
1266 static void
1267 save_template_attributes (tree *attr_p, tree *decl_p, int flags)
1269 tree *q;
1271 if (attr_p && *attr_p == error_mark_node)
1272 return;
1274 tree late_attrs = splice_template_attributes (attr_p, *decl_p);
1275 if (!late_attrs)
1276 return;
1278 if (DECL_P (*decl_p))
1279 q = &DECL_ATTRIBUTES (*decl_p);
1280 else
1281 q = &TYPE_ATTRIBUTES (*decl_p);
1283 tree old_attrs = *q;
1285 /* Place the late attributes at the beginning of the attribute
1286 list. */
1287 late_attrs = chainon (late_attrs, *q);
1288 if (*q != late_attrs
1289 && !DECL_P (*decl_p)
1290 && !(flags & ATTR_FLAG_TYPE_IN_PLACE))
1292 if (!dependent_type_p (*decl_p))
1293 *decl_p = cp_build_type_attribute_variant (*decl_p, late_attrs);
1294 else
1296 *decl_p = build_variant_type_copy (*decl_p);
1297 TYPE_ATTRIBUTES (*decl_p) = late_attrs;
1300 else
1301 *q = late_attrs;
1303 if (!DECL_P (*decl_p) && *decl_p == TYPE_MAIN_VARIANT (*decl_p))
1305 /* We've added new attributes directly to the main variant, so
1306 now we need to update all of the other variants to include
1307 these new attributes. */
1308 tree variant;
1309 for (variant = TYPE_NEXT_VARIANT (*decl_p); variant;
1310 variant = TYPE_NEXT_VARIANT (variant))
1312 gcc_assert (TYPE_ATTRIBUTES (variant) == old_attrs);
1313 TYPE_ATTRIBUTES (variant) = TYPE_ATTRIBUTES (*decl_p);
1318 /* True if ATTRS contains any dependent attributes that affect type
1319 identity. */
1321 bool
1322 any_dependent_type_attributes_p (tree attrs)
1324 for (tree a = attrs; a; a = TREE_CHAIN (a))
1325 if (ATTR_IS_DEPENDENT (a))
1327 const attribute_spec *as = lookup_attribute_spec (TREE_PURPOSE (a));
1328 if (as && as->affects_type_identity)
1329 return true;
1331 return false;
1334 /* Return true iff ATTRS are acceptable attributes to be applied in-place
1335 to a typedef which gives a previously unnamed class or enum a name for
1336 linkage purposes. */
1338 bool
1339 attributes_naming_typedef_ok (tree attrs)
1341 for (; attrs; attrs = TREE_CHAIN (attrs))
1343 tree name = get_attribute_name (attrs);
1344 if (is_attribute_p ("vector_size", name))
1345 return false;
1347 return true;
1350 /* Like reconstruct_complex_type, but handle also template trees. */
1352 tree
1353 cp_reconstruct_complex_type (tree type, tree bottom)
1355 tree inner, outer;
1357 if (TYPE_PTR_P (type))
1359 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1360 outer = build_pointer_type_for_mode (inner, TYPE_MODE (type),
1361 TYPE_REF_CAN_ALIAS_ALL (type));
1363 else if (TYPE_REF_P (type))
1365 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1366 outer = build_reference_type_for_mode (inner, TYPE_MODE (type),
1367 TYPE_REF_CAN_ALIAS_ALL (type));
1369 else if (TREE_CODE (type) == ARRAY_TYPE)
1371 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1372 outer = build_cplus_array_type (inner, TYPE_DOMAIN (type));
1373 /* Don't call cp_build_qualified_type on ARRAY_TYPEs, the
1374 element type qualification will be handled by the recursive
1375 cp_reconstruct_complex_type call and cp_build_qualified_type
1376 for ARRAY_TYPEs changes the element type. */
1377 return outer;
1379 else if (TREE_CODE (type) == FUNCTION_TYPE)
1381 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1382 outer = build_function_type (inner, TYPE_ARG_TYPES (type));
1383 outer = apply_memfn_quals (outer, type_memfn_quals (type));
1385 else if (TREE_CODE (type) == METHOD_TYPE)
1387 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1388 /* The build_method_type_directly() routine prepends 'this' to argument list,
1389 so we must compensate by getting rid of it. */
1390 outer
1391 = build_method_type_directly
1392 (class_of_this_parm (type), inner,
1393 TREE_CHAIN (TYPE_ARG_TYPES (type)));
1395 else if (TREE_CODE (type) == OFFSET_TYPE)
1397 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1398 outer = build_offset_type (TYPE_OFFSET_BASETYPE (type), inner);
1400 else
1401 return bottom;
1403 if (TYPE_ATTRIBUTES (type))
1404 outer = cp_build_type_attribute_variant (outer, TYPE_ATTRIBUTES (type));
1405 outer = cp_build_qualified_type (outer, cp_type_quals (type));
1406 outer = cxx_copy_lang_qualifiers (outer, type);
1408 return outer;
1411 /* Replaces any constexpr expression that may be into the attributes
1412 arguments with their reduced value. */
1414 void
1415 cp_check_const_attributes (tree attributes)
1417 if (attributes == error_mark_node)
1418 return;
1420 tree attr;
1421 for (attr = attributes; attr; attr = TREE_CHAIN (attr))
1423 tree arg;
1424 for (arg = TREE_VALUE (attr); arg && TREE_CODE (arg) == TREE_LIST;
1425 arg = TREE_CHAIN (arg))
1427 tree expr = TREE_VALUE (arg);
1428 if (EXPR_P (expr))
1429 TREE_VALUE (arg) = fold_non_dependent_expr (expr);
1434 /* Return true if TYPE is an OpenMP mappable type.
1435 If NOTES is non-zero, emit a note message for each problem. */
1436 static bool
1437 cp_omp_mappable_type_1 (tree type, bool notes)
1439 bool result = true;
1441 /* Mappable type has to be complete. */
1442 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
1444 if (notes && type != error_mark_node)
1446 tree decl = TYPE_MAIN_DECL (type);
1447 inform ((decl ? DECL_SOURCE_LOCATION (decl) : input_location),
1448 "incomplete type %qT is not mappable", type);
1450 result = false;
1452 /* Arrays have mappable type if the elements have mappable type. */
1453 while (TREE_CODE (type) == ARRAY_TYPE)
1454 type = TREE_TYPE (type);
1455 /* A mappable type cannot contain virtual members. */
1456 if (CLASS_TYPE_P (type) && CLASSTYPE_VTABLES (type))
1458 if (notes)
1459 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
1460 "type %qT with virtual members is not mappable", type);
1461 result = false;
1463 /* All data members must be non-static. */
1464 if (CLASS_TYPE_P (type))
1466 tree field;
1467 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
1468 if (VAR_P (field))
1470 if (notes)
1471 inform (DECL_SOURCE_LOCATION (field),
1472 "static field %qD is not mappable", field);
1473 result = false;
1475 /* All fields must have mappable types. */
1476 else if (TREE_CODE (field) == FIELD_DECL
1477 && !cp_omp_mappable_type_1 (TREE_TYPE (field), notes))
1478 result = false;
1480 return result;
1483 /* Return true if TYPE is an OpenMP mappable type. */
1484 bool
1485 cp_omp_mappable_type (tree type)
1487 return cp_omp_mappable_type_1 (type, false);
1490 /* Return true if TYPE is an OpenMP mappable type.
1491 Emit an error messages if not. */
1492 bool
1493 cp_omp_emit_unmappable_type_notes (tree type)
1495 return cp_omp_mappable_type_1 (type, true);
1498 /* Return the last pushed declaration for the symbol DECL or NULL
1499 when no such declaration exists. */
1501 static tree
1502 find_last_decl (tree decl)
1504 tree last_decl = NULL_TREE;
1506 if (tree name = DECL_P (decl) ? DECL_NAME (decl) : NULL_TREE)
1508 /* Look up the declaration in its scope. */
1509 tree pushed_scope = NULL_TREE;
1510 if (tree ctype = DECL_CONTEXT (decl))
1511 pushed_scope = push_scope (ctype);
1513 last_decl = lookup_name (name);
1515 if (pushed_scope)
1516 pop_scope (pushed_scope);
1518 /* The declaration may be a member conversion operator
1519 or a bunch of overfloads (handle the latter below). */
1520 if (last_decl && BASELINK_P (last_decl))
1521 last_decl = BASELINK_FUNCTIONS (last_decl);
1524 if (!last_decl)
1525 return NULL_TREE;
1527 if (DECL_P (last_decl) || TREE_CODE (last_decl) == OVERLOAD)
1529 /* A set of overloads of the same function. */
1530 for (lkp_iterator iter (last_decl); iter; ++iter)
1532 if (TREE_CODE (*iter) == OVERLOAD)
1533 continue;
1535 if (decls_match (decl, *iter, /*record_decls=*/false))
1536 return *iter;
1538 return NULL_TREE;
1541 return NULL_TREE;
1544 /* Like decl_attributes, but handle C++ complexity. */
1546 void
1547 cplus_decl_attributes (tree *decl, tree attributes, int flags)
1549 if (*decl == NULL_TREE || *decl == void_type_node
1550 || *decl == error_mark_node)
1551 return;
1553 /* Add implicit "omp declare target" attribute if requested. */
1554 if (vec_safe_length (scope_chain->omp_declare_target_attribute)
1555 && ((VAR_P (*decl)
1556 && (TREE_STATIC (*decl) || DECL_EXTERNAL (*decl)))
1557 || TREE_CODE (*decl) == FUNCTION_DECL))
1559 if (VAR_P (*decl)
1560 && DECL_CLASS_SCOPE_P (*decl))
1561 error ("%q+D static data member inside of declare target directive",
1562 *decl);
1563 else if (VAR_P (*decl)
1564 && (processing_template_decl
1565 || !cp_omp_mappable_type (TREE_TYPE (*decl))))
1566 attributes = tree_cons (get_identifier ("omp declare target implicit"),
1567 NULL_TREE, attributes);
1568 else
1570 attributes = tree_cons (get_identifier ("omp declare target"),
1571 NULL_TREE, attributes);
1572 attributes = tree_cons (get_identifier ("omp declare target block"),
1573 NULL_TREE, attributes);
1577 if (processing_template_decl)
1579 if (check_for_bare_parameter_packs (attributes))
1580 return;
1582 save_template_attributes (&attributes, decl, flags);
1585 cp_check_const_attributes (attributes);
1587 if ((flag_openmp || flag_openmp_simd) && attributes != error_mark_node)
1589 bool diagnosed = false;
1590 for (tree *pa = &attributes; *pa; )
1592 if (get_attribute_namespace (*pa) == omp_identifier)
1594 tree name = get_attribute_name (*pa);
1595 if (is_attribute_p ("directive", name)
1596 || is_attribute_p ("sequence", name))
1598 if (!diagnosed)
1600 error ("%<omp::%E%> not allowed to be specified in this "
1601 "context", name);
1602 diagnosed = true;
1604 *pa = TREE_CHAIN (*pa);
1605 continue;
1608 pa = &TREE_CHAIN (*pa);
1612 if (TREE_CODE (*decl) == TEMPLATE_DECL)
1613 decl = &DECL_TEMPLATE_RESULT (*decl);
1615 if (TREE_TYPE (*decl) && TYPE_PTRMEMFUNC_P (TREE_TYPE (*decl)))
1617 attributes
1618 = decl_attributes (decl, attributes, flags | ATTR_FLAG_FUNCTION_NEXT);
1619 decl_attributes (&TYPE_PTRMEMFUNC_FN_TYPE_RAW (TREE_TYPE (*decl)),
1620 attributes, flags);
1622 else
1624 tree last_decl = find_last_decl (*decl);
1625 decl_attributes (decl, attributes, flags, last_decl);
1628 /* Propagate deprecation out to the template. */
1629 if (TREE_DEPRECATED (*decl))
1630 if (tree ti = get_template_info (*decl))
1632 tree tmpl = TI_TEMPLATE (ti);
1633 tree pattern = (TYPE_P (*decl) ? TREE_TYPE (tmpl)
1634 : DECL_TEMPLATE_RESULT (tmpl));
1635 if (*decl == pattern)
1636 TREE_DEPRECATED (tmpl) = true;
1640 /* Walks through the namespace- or function-scope anonymous union
1641 OBJECT, with the indicated TYPE, building appropriate VAR_DECLs.
1642 Returns one of the fields for use in the mangled name. */
1644 static tree
1645 build_anon_union_vars (tree type, tree object)
1647 tree main_decl = NULL_TREE;
1648 tree field;
1650 /* Rather than write the code to handle the non-union case,
1651 just give an error. */
1652 if (TREE_CODE (type) != UNION_TYPE)
1654 error_at (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
1655 "anonymous struct not inside named type");
1656 return error_mark_node;
1659 for (field = TYPE_FIELDS (type);
1660 field != NULL_TREE;
1661 field = DECL_CHAIN (field))
1663 tree decl;
1664 tree ref;
1666 if (DECL_ARTIFICIAL (field))
1667 continue;
1668 if (TREE_CODE (field) != FIELD_DECL)
1670 permerror (DECL_SOURCE_LOCATION (field),
1671 "%q#D invalid; an anonymous union can only "
1672 "have non-static data members", field);
1673 continue;
1676 if (TREE_PRIVATE (field))
1677 permerror (DECL_SOURCE_LOCATION (field),
1678 "private member %q#D in anonymous union", field);
1679 else if (TREE_PROTECTED (field))
1680 permerror (DECL_SOURCE_LOCATION (field),
1681 "protected member %q#D in anonymous union", field);
1683 if (processing_template_decl)
1684 ref = build_min_nt_loc (UNKNOWN_LOCATION, COMPONENT_REF, object,
1685 DECL_NAME (field), NULL_TREE);
1686 else
1687 ref = build_class_member_access_expr (object, field, NULL_TREE,
1688 false, tf_warning_or_error);
1690 if (DECL_NAME (field))
1692 tree base;
1694 decl = build_decl (input_location,
1695 VAR_DECL, DECL_NAME (field), TREE_TYPE (field));
1696 DECL_ANON_UNION_VAR_P (decl) = 1;
1697 DECL_ARTIFICIAL (decl) = 1;
1699 base = get_base_address (object);
1700 TREE_PUBLIC (decl) = TREE_PUBLIC (base);
1701 TREE_STATIC (decl) = TREE_STATIC (base);
1702 DECL_EXTERNAL (decl) = DECL_EXTERNAL (base);
1704 SET_DECL_VALUE_EXPR (decl, ref);
1705 DECL_HAS_VALUE_EXPR_P (decl) = 1;
1707 decl = pushdecl (decl);
1709 else if (ANON_AGGR_TYPE_P (TREE_TYPE (field)))
1710 decl = build_anon_union_vars (TREE_TYPE (field), ref);
1711 else
1712 decl = 0;
1714 if (main_decl == NULL_TREE)
1715 main_decl = decl;
1718 return main_decl;
1721 /* Finish off the processing of a UNION_TYPE structure. If the union is an
1722 anonymous union, then all members must be laid out together. PUBLIC_P
1723 is nonzero if this union is not declared static. */
1725 void
1726 finish_anon_union (tree anon_union_decl)
1728 tree type;
1729 tree main_decl;
1730 bool public_p;
1732 if (anon_union_decl == error_mark_node)
1733 return;
1735 type = TREE_TYPE (anon_union_decl);
1736 public_p = TREE_PUBLIC (anon_union_decl);
1738 /* The VAR_DECL's context is the same as the TYPE's context. */
1739 DECL_CONTEXT (anon_union_decl) = DECL_CONTEXT (TYPE_NAME (type));
1741 if (TYPE_FIELDS (type) == NULL_TREE)
1742 return;
1744 if (public_p)
1746 error ("namespace-scope anonymous aggregates must be static");
1747 return;
1750 main_decl = build_anon_union_vars (type, anon_union_decl);
1751 if (main_decl == error_mark_node)
1752 return;
1753 if (main_decl == NULL_TREE)
1755 pedwarn (input_location, 0, "anonymous union with no members");
1756 return;
1759 if (!processing_template_decl)
1761 /* Use main_decl to set the mangled name. */
1762 DECL_NAME (anon_union_decl) = DECL_NAME (main_decl);
1763 maybe_commonize_var (anon_union_decl);
1764 if (TREE_STATIC (anon_union_decl) || DECL_EXTERNAL (anon_union_decl))
1766 if (DECL_DISCRIMINATOR_P (anon_union_decl))
1767 determine_local_discriminator (anon_union_decl);
1768 mangle_decl (anon_union_decl);
1770 DECL_NAME (anon_union_decl) = NULL_TREE;
1773 pushdecl (anon_union_decl);
1774 cp_finish_decl (anon_union_decl, NULL_TREE, false, NULL_TREE, 0);
1777 /* Auxiliary functions to make type signatures for
1778 `operator new' and `operator delete' correspond to
1779 what compiler will be expecting. */
1781 tree
1782 coerce_new_type (tree type, location_t loc)
1784 int e = 0;
1785 tree args = TYPE_ARG_TYPES (type);
1787 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
1789 if (!same_type_p (TREE_TYPE (type), ptr_type_node))
1791 e = 1;
1792 error_at (loc, "%<operator new%> must return type %qT",
1793 ptr_type_node);
1796 if (args && args != void_list_node)
1798 if (TREE_PURPOSE (args))
1800 /* [basic.stc.dynamic.allocation]
1802 The first parameter shall not have an associated default
1803 argument. */
1804 error_at (loc, "the first parameter of %<operator new%> cannot "
1805 "have a default argument");
1806 /* Throw away the default argument. */
1807 TREE_PURPOSE (args) = NULL_TREE;
1810 if (!same_type_p (TREE_VALUE (args), size_type_node))
1812 e = 2;
1813 args = TREE_CHAIN (args);
1816 else
1817 e = 2;
1819 if (e == 2)
1820 permerror (loc, "%<operator new%> takes type %<size_t%> (%qT) "
1821 "as first parameter", size_type_node);
1823 switch (e)
1825 case 2:
1826 args = tree_cons (NULL_TREE, size_type_node, args);
1827 /* Fall through. */
1828 case 1:
1829 type = (cxx_copy_lang_qualifiers
1830 (build_function_type (ptr_type_node, args),
1831 type));
1832 /* Fall through. */
1833 default:;
1835 return type;
1838 void
1839 coerce_delete_type (tree decl, location_t loc)
1841 int e = 0;
1842 tree type = TREE_TYPE (decl);
1843 tree args = TYPE_ARG_TYPES (type);
1845 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
1847 if (!same_type_p (TREE_TYPE (type), void_type_node))
1849 e = 1;
1850 error_at (loc, "%<operator delete%> must return type %qT",
1851 void_type_node);
1854 tree ptrtype = ptr_type_node;
1855 if (destroying_delete_p (decl))
1857 if (DECL_CLASS_SCOPE_P (decl))
1858 /* If the function is a destroying operator delete declared in class
1859 type C, the type of its first parameter shall be C*. */
1860 ptrtype = build_pointer_type (DECL_CONTEXT (decl));
1861 else
1862 /* A destroying operator delete shall be a class member function named
1863 operator delete. */
1864 error_at (loc,
1865 "destroying %<operator delete%> must be a member function");
1866 const ovl_op_info_t *op = IDENTIFIER_OVL_OP_INFO (DECL_NAME (decl));
1867 if (op->flags & OVL_OP_FLAG_VEC)
1868 error_at (loc, "%<operator delete[]%> cannot be a destroying delete");
1869 if (!usual_deallocation_fn_p (decl))
1870 error_at (loc, "destroying %<operator delete%> must be a usual "
1871 "deallocation function");
1874 if (!args || args == void_list_node
1875 || !same_type_p (TREE_VALUE (args), ptrtype))
1877 e = 2;
1878 if (args && args != void_list_node)
1879 args = TREE_CHAIN (args);
1880 error_at (loc, "%<operator delete%> takes type %qT as first parameter",
1881 ptrtype);
1883 switch (e)
1885 case 2:
1886 args = tree_cons (NULL_TREE, ptrtype, args);
1887 /* Fall through. */
1888 case 1:
1889 type = (cxx_copy_lang_qualifiers
1890 (build_function_type (void_type_node, args),
1891 type));
1892 /* Fall through. */
1893 default:;
1896 TREE_TYPE (decl) = type;
1899 /* DECL is a VAR_DECL for a vtable: walk through the entries in the vtable
1900 and mark them as needed. */
1902 static void
1903 mark_vtable_entries (tree decl, vec<tree> &consteval_vtables)
1905 tree fnaddr;
1906 unsigned HOST_WIDE_INT idx;
1908 /* It's OK for the vtable to refer to deprecated virtual functions. */
1909 warning_sentinel w(warn_deprecated_decl);
1911 bool consteval_seen = false;
1913 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (DECL_INITIAL (decl)),
1914 idx, fnaddr)
1916 tree fn;
1918 STRIP_NOPS (fnaddr);
1920 if (TREE_CODE (fnaddr) != ADDR_EXPR
1921 && TREE_CODE (fnaddr) != FDESC_EXPR)
1922 /* This entry is an offset: a virtual base class offset, a
1923 virtual call offset, an RTTI offset, etc. */
1924 continue;
1926 fn = TREE_OPERAND (fnaddr, 0);
1927 if (TREE_CODE (fn) == FUNCTION_DECL && DECL_IMMEDIATE_FUNCTION_P (fn))
1929 if (!consteval_seen)
1931 consteval_seen = true;
1932 consteval_vtables.safe_push (decl);
1934 continue;
1936 TREE_ADDRESSABLE (fn) = 1;
1937 /* When we don't have vcall offsets, we output thunks whenever
1938 we output the vtables that contain them. With vcall offsets,
1939 we know all the thunks we'll need when we emit a virtual
1940 function, so we emit the thunks there instead. */
1941 if (DECL_THUNK_P (fn))
1942 use_thunk (fn, /*emit_p=*/0);
1943 /* Set the location, as marking the function could cause
1944 instantiation. We do not need to preserve the incoming
1945 location, as we're called from c_parse_final_cleanups, which
1946 takes care of that. */
1947 input_location = DECL_SOURCE_LOCATION (fn);
1948 mark_used (fn);
1952 /* Replace any consteval functions in vtables with null pointers. */
1954 static void
1955 clear_consteval_vfns (vec<tree> &consteval_vtables)
1957 for (tree vtable : consteval_vtables)
1958 for (constructor_elt &elt : CONSTRUCTOR_ELTS (DECL_INITIAL (vtable)))
1960 tree fn = cp_get_fndecl_from_callee (elt.value, /*fold*/false);
1961 if (fn && DECL_IMMEDIATE_FUNCTION_P (fn))
1962 elt.value = build_zero_cst (vtable_entry_type);
1966 /* Adjust the TLS model on variable DECL if need be, typically after
1967 the linkage of DECL has been modified. */
1969 static void
1970 adjust_var_decl_tls_model (tree decl)
1972 if (CP_DECL_THREAD_LOCAL_P (decl)
1973 && !lookup_attribute ("tls_model", DECL_ATTRIBUTES (decl)))
1974 set_decl_tls_model (decl, decl_default_tls_model (decl));
1977 /* Set DECL up to have the closest approximation of "initialized common"
1978 linkage available. */
1980 void
1981 comdat_linkage (tree decl)
1983 if (flag_weak)
1984 make_decl_one_only (decl, cxx_comdat_group (decl));
1985 else if (TREE_CODE (decl) == FUNCTION_DECL
1986 || (VAR_P (decl) && DECL_ARTIFICIAL (decl)))
1987 /* We can just emit function and compiler-generated variables
1988 statically; having multiple copies is (for the most part) only
1989 a waste of space.
1991 There are two correctness issues, however: the address of a
1992 template instantiation with external linkage should be the
1993 same, independent of what translation unit asks for the
1994 address, and this will not hold when we emit multiple copies of
1995 the function. However, there's little else we can do.
1997 Also, by default, the typeinfo implementation assumes that
1998 there will be only one copy of the string used as the name for
1999 each type. Therefore, if weak symbols are unavailable, the
2000 run-time library should perform a more conservative check; it
2001 should perform a string comparison, rather than an address
2002 comparison. */
2003 TREE_PUBLIC (decl) = 0;
2004 else
2006 /* Static data member template instantiations, however, cannot
2007 have multiple copies. */
2008 if (DECL_INITIAL (decl) == 0
2009 || DECL_INITIAL (decl) == error_mark_node)
2010 DECL_COMMON (decl) = 1;
2011 else if (EMPTY_CONSTRUCTOR_P (DECL_INITIAL (decl)))
2013 DECL_COMMON (decl) = 1;
2014 DECL_INITIAL (decl) = error_mark_node;
2016 else if (!DECL_EXPLICIT_INSTANTIATION (decl))
2018 /* We can't do anything useful; leave vars for explicit
2019 instantiation. */
2020 DECL_EXTERNAL (decl) = 1;
2021 DECL_NOT_REALLY_EXTERN (decl) = 0;
2025 if (TREE_PUBLIC (decl))
2026 DECL_COMDAT (decl) = 1;
2028 if (VAR_P (decl))
2029 adjust_var_decl_tls_model (decl);
2032 /* For win32 we also want to put explicit instantiations in
2033 linkonce sections, so that they will be merged with implicit
2034 instantiations; otherwise we get duplicate symbol errors.
2035 For Darwin we do not want explicit instantiations to be
2036 linkonce. */
2038 void
2039 maybe_make_one_only (tree decl)
2041 /* We used to say that this was not necessary on targets that support weak
2042 symbols, because the implicit instantiations will defer to the explicit
2043 one. However, that's not actually the case in SVR4; a strong definition
2044 after a weak one is an error. Also, not making explicit
2045 instantiations one_only means that we can end up with two copies of
2046 some template instantiations. */
2047 if (! flag_weak)
2048 return;
2050 /* We can't set DECL_COMDAT on functions, or cp_finish_file will think
2051 we can get away with not emitting them if they aren't used. We need
2052 to for variables so that cp_finish_decl will update their linkage,
2053 because their DECL_INITIAL may not have been set properly yet. */
2055 if (!TARGET_WEAK_NOT_IN_ARCHIVE_TOC
2056 || (! DECL_EXPLICIT_INSTANTIATION (decl)
2057 && ! DECL_TEMPLATE_SPECIALIZATION (decl)))
2059 make_decl_one_only (decl, cxx_comdat_group (decl));
2061 if (VAR_P (decl))
2063 varpool_node *node = varpool_node::get_create (decl);
2064 DECL_COMDAT (decl) = 1;
2065 /* Mark it needed so we don't forget to emit it. */
2066 node->forced_by_abi = true;
2067 TREE_USED (decl) = 1;
2069 adjust_var_decl_tls_model (decl);
2074 /* Returns true iff DECL, a FUNCTION_DECL or VAR_DECL, has vague linkage.
2075 This predicate will give the right answer during parsing of the
2076 function, which other tests may not. */
2078 bool
2079 vague_linkage_p (tree decl)
2081 if (!TREE_PUBLIC (decl))
2083 /* maybe_thunk_body clears TREE_PUBLIC and DECL_ABSTRACT_P on the
2084 maybe-in-charge 'tor variants; in that case we need to check one of
2085 the "clones" for the real linkage. But only in that case; before
2086 maybe_clone_body we haven't yet copied the linkage to the clones. */
2087 if (DECL_MAYBE_IN_CHARGE_CDTOR_P (decl)
2088 && !DECL_ABSTRACT_P (decl)
2089 && DECL_CHAIN (decl)
2090 && DECL_CLONED_FUNCTION_P (DECL_CHAIN (decl)))
2091 return vague_linkage_p (DECL_CHAIN (decl));
2093 gcc_checking_assert (!DECL_COMDAT (decl));
2094 return false;
2096 /* Unfortunately, import_export_decl has not always been called
2097 before the function is processed, so we cannot simply check
2098 DECL_COMDAT. */
2099 if (DECL_COMDAT (decl)
2100 || (TREE_CODE (decl) == FUNCTION_DECL
2101 && DECL_DECLARED_INLINE_P (decl))
2102 || (DECL_LANG_SPECIFIC (decl)
2103 && DECL_TEMPLATE_INSTANTIATION (decl))
2104 || (VAR_P (decl) && DECL_INLINE_VAR_P (decl)))
2105 return true;
2106 else if (DECL_FUNCTION_SCOPE_P (decl))
2107 /* A local static in an inline effectively has vague linkage. */
2108 return (TREE_STATIC (decl)
2109 && vague_linkage_p (DECL_CONTEXT (decl)));
2110 else
2111 return false;
2114 /* Determine whether or not we want to specifically import or export CTYPE,
2115 using various heuristics. */
2117 static void
2118 import_export_class (tree ctype)
2120 /* -1 for imported, 1 for exported. */
2121 int import_export = 0;
2123 /* It only makes sense to call this function at EOF. The reason is
2124 that this function looks at whether or not the first non-inline
2125 non-abstract virtual member function has been defined in this
2126 translation unit. But, we can't possibly know that until we've
2127 seen the entire translation unit. */
2128 gcc_assert (at_eof);
2130 if (CLASSTYPE_INTERFACE_KNOWN (ctype))
2131 return;
2133 /* If MULTIPLE_SYMBOL_SPACES is set and we saw a #pragma interface,
2134 we will have CLASSTYPE_INTERFACE_ONLY set but not
2135 CLASSTYPE_INTERFACE_KNOWN. In that case, we don't want to use this
2136 heuristic because someone will supply a #pragma implementation
2137 elsewhere, and deducing it here would produce a conflict. */
2138 if (CLASSTYPE_INTERFACE_ONLY (ctype))
2139 return;
2141 if (lookup_attribute ("dllimport", TYPE_ATTRIBUTES (ctype)))
2142 import_export = -1;
2143 else if (lookup_attribute ("dllexport", TYPE_ATTRIBUTES (ctype)))
2144 import_export = 1;
2145 else if (CLASSTYPE_IMPLICIT_INSTANTIATION (ctype)
2146 && !flag_implicit_templates)
2147 /* For a template class, without -fimplicit-templates, check the
2148 repository. If the virtual table is assigned to this
2149 translation unit, then export the class; otherwise, import
2150 it. */
2151 import_export = -1;
2152 else if (TYPE_POLYMORPHIC_P (ctype))
2154 /* The ABI specifies that the virtual table and associated
2155 information are emitted with the key method, if any. */
2156 tree method = CLASSTYPE_KEY_METHOD (ctype);
2157 /* If weak symbol support is not available, then we must be
2158 careful not to emit the vtable when the key function is
2159 inline. An inline function can be defined in multiple
2160 translation units. If we were to emit the vtable in each
2161 translation unit containing a definition, we would get
2162 multiple definition errors at link-time. */
2163 if (method && (flag_weak || ! DECL_DECLARED_INLINE_P (method)))
2164 import_export = (DECL_REALLY_EXTERN (method) ? -1 : 1);
2167 /* When MULTIPLE_SYMBOL_SPACES is set, we cannot count on seeing
2168 a definition anywhere else. */
2169 if (MULTIPLE_SYMBOL_SPACES && import_export == -1)
2170 import_export = 0;
2172 /* Allow back ends the chance to overrule the decision. */
2173 if (targetm.cxx.import_export_class)
2174 import_export = targetm.cxx.import_export_class (ctype, import_export);
2176 if (import_export)
2178 SET_CLASSTYPE_INTERFACE_KNOWN (ctype);
2179 CLASSTYPE_INTERFACE_ONLY (ctype) = (import_export < 0);
2183 /* Return true if VAR has already been provided to the back end; in that
2184 case VAR should not be modified further by the front end. */
2185 static bool
2186 var_finalized_p (tree var)
2188 return varpool_node::get_create (var)->definition;
2191 /* DECL is a VAR_DECL or FUNCTION_DECL which, for whatever reason,
2192 must be emitted in this translation unit. Mark it as such. */
2194 void
2195 mark_needed (tree decl)
2197 TREE_USED (decl) = 1;
2198 if (TREE_CODE (decl) == FUNCTION_DECL)
2200 /* Extern inline functions don't become needed when referenced.
2201 If we know a method will be emitted in other TU and no new
2202 functions can be marked reachable, just use the external
2203 definition. */
2204 struct cgraph_node *node = cgraph_node::get_create (decl);
2205 node->forced_by_abi = true;
2207 /* #pragma interface can call mark_needed for
2208 maybe-in-charge 'tors; mark the clones as well. */
2209 tree clone;
2210 FOR_EACH_CLONE (clone, decl)
2211 mark_needed (clone);
2213 else if (VAR_P (decl))
2215 varpool_node *node = varpool_node::get_create (decl);
2216 /* C++ frontend use mark_decl_references to force COMDAT variables
2217 to be output that might appear dead otherwise. */
2218 node->forced_by_abi = true;
2222 /* DECL is either a FUNCTION_DECL or a VAR_DECL. This function
2223 returns true if a definition of this entity should be provided in
2224 this object file. Callers use this function to determine whether
2225 or not to let the back end know that a definition of DECL is
2226 available in this translation unit. */
2228 bool
2229 decl_needed_p (tree decl)
2231 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
2232 /* This function should only be called at the end of the translation
2233 unit. We cannot be sure of whether or not something will be
2234 COMDAT until that point. */
2235 gcc_assert (at_eof);
2237 /* All entities with external linkage that are not COMDAT/EXTERN should be
2238 emitted; they may be referred to from other object files. */
2239 if (TREE_PUBLIC (decl) && !DECL_COMDAT (decl) && !DECL_REALLY_EXTERN (decl))
2240 return true;
2242 /* Functions marked "dllexport" must be emitted so that they are
2243 visible to other DLLs. */
2244 if (flag_keep_inline_dllexport
2245 && lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl)))
2246 return true;
2248 /* When not optimizing, do not bother to produce definitions for extern
2249 symbols. */
2250 if (DECL_REALLY_EXTERN (decl)
2251 && ((TREE_CODE (decl) != FUNCTION_DECL
2252 && !optimize)
2253 || (TREE_CODE (decl) == FUNCTION_DECL
2254 && !opt_for_fn (decl, optimize)))
2255 && !lookup_attribute ("always_inline", decl))
2256 return false;
2258 /* If this entity was used, let the back end see it; it will decide
2259 whether or not to emit it into the object file. */
2260 if (TREE_USED (decl))
2261 return true;
2263 /* Virtual functions might be needed for devirtualization. */
2264 if (flag_devirtualize
2265 && TREE_CODE (decl) == FUNCTION_DECL
2266 && DECL_VIRTUAL_P (decl))
2267 return true;
2269 /* Otherwise, DECL does not need to be emitted -- yet. A subsequent
2270 reference to DECL might cause it to be emitted later. */
2271 return false;
2274 /* If necessary, write out the vtables for the dynamic class CTYPE.
2275 Returns true if any vtables were emitted. */
2277 static bool
2278 maybe_emit_vtables (tree ctype, vec<tree> &consteval_vtables)
2280 tree vtbl;
2281 tree primary_vtbl;
2282 int needed = 0;
2283 varpool_node *current = NULL, *last = NULL;
2285 /* If the vtables for this class have already been emitted there is
2286 nothing more to do. */
2287 primary_vtbl = CLASSTYPE_VTABLES (ctype);
2288 if (var_finalized_p (primary_vtbl))
2289 return false;
2290 /* Ignore dummy vtables made by get_vtable_decl. */
2291 if (TREE_TYPE (primary_vtbl) == void_type_node)
2292 return false;
2294 /* On some targets, we cannot determine the key method until the end
2295 of the translation unit -- which is when this function is
2296 called. */
2297 if (!targetm.cxx.key_method_may_be_inline ())
2298 determine_key_method (ctype);
2300 /* See if any of the vtables are needed. */
2301 for (vtbl = CLASSTYPE_VTABLES (ctype); vtbl; vtbl = DECL_CHAIN (vtbl))
2303 import_export_decl (vtbl);
2304 if (DECL_NOT_REALLY_EXTERN (vtbl) && decl_needed_p (vtbl))
2305 needed = 1;
2307 if (!needed)
2309 /* If the references to this class' vtables are optimized away,
2310 still emit the appropriate debugging information. See
2311 dfs_debug_mark. */
2312 if (DECL_COMDAT (primary_vtbl)
2313 && CLASSTYPE_DEBUG_REQUESTED (ctype))
2314 note_debug_info_needed (ctype);
2315 return false;
2318 /* The ABI requires that we emit all of the vtables if we emit any
2319 of them. */
2320 for (vtbl = CLASSTYPE_VTABLES (ctype); vtbl; vtbl = DECL_CHAIN (vtbl))
2322 /* Mark entities references from the virtual table as used. */
2323 mark_vtable_entries (vtbl, consteval_vtables);
2325 if (TREE_TYPE (DECL_INITIAL (vtbl)) == 0)
2327 vec<tree, va_gc> *cleanups = NULL;
2328 tree expr = store_init_value (vtbl, DECL_INITIAL (vtbl), &cleanups,
2329 LOOKUP_NORMAL);
2331 /* It had better be all done at compile-time. */
2332 gcc_assert (!expr && !cleanups);
2335 /* Write it out. */
2336 DECL_EXTERNAL (vtbl) = 0;
2337 rest_of_decl_compilation (vtbl, 1, 1);
2339 /* Because we're only doing syntax-checking, we'll never end up
2340 actually marking the variable as written. */
2341 if (flag_syntax_only)
2342 TREE_ASM_WRITTEN (vtbl) = 1;
2343 else if (DECL_ONE_ONLY (vtbl))
2345 current = varpool_node::get_create (vtbl);
2346 if (last)
2347 current->add_to_same_comdat_group (last);
2348 last = current;
2352 /* For abstract classes, the destructor has been removed from the
2353 vtable (in class.c's build_vtbl_initializer). For a compiler-
2354 generated destructor, it hence might not have been generated in
2355 this translation unit - and with '#pragma interface' it might
2356 never get generated. */
2357 if (CLASSTYPE_PURE_VIRTUALS (ctype)
2358 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (ctype)
2359 && !CLASSTYPE_LAZY_DESTRUCTOR (ctype)
2360 && DECL_DEFAULTED_IN_CLASS_P (CLASSTYPE_DESTRUCTOR (ctype)))
2361 note_vague_linkage_fn (CLASSTYPE_DESTRUCTOR (ctype));
2363 /* Since we're writing out the vtable here, also write the debug
2364 info. */
2365 note_debug_info_needed (ctype);
2367 return true;
2370 /* A special return value from type_visibility meaning internal
2371 linkage. */
2373 enum { VISIBILITY_ANON = VISIBILITY_INTERNAL+1 };
2375 static int expr_visibility (tree);
2376 static int type_visibility (tree);
2378 /* walk_tree helper function for type_visibility. */
2380 static tree
2381 min_vis_r (tree *tp, int *walk_subtrees, void *data)
2383 int *vis_p = (int *)data;
2384 int this_vis = VISIBILITY_DEFAULT;
2385 if (! TYPE_P (*tp))
2386 *walk_subtrees = 0;
2387 else if (OVERLOAD_TYPE_P (*tp)
2388 && !TREE_PUBLIC (TYPE_MAIN_DECL (*tp)))
2390 this_vis = VISIBILITY_ANON;
2391 *walk_subtrees = 0;
2393 else if (CLASS_TYPE_P (*tp))
2395 this_vis = CLASSTYPE_VISIBILITY (*tp);
2396 *walk_subtrees = 0;
2398 else if (TREE_CODE (*tp) == ARRAY_TYPE
2399 && uses_template_parms (TYPE_DOMAIN (*tp)))
2400 this_vis = expr_visibility (TYPE_MAX_VALUE (TYPE_DOMAIN (*tp)));
2402 if (this_vis > *vis_p)
2403 *vis_p = this_vis;
2405 /* Tell cp_walk_subtrees to look through typedefs. */
2406 if (*walk_subtrees == 1)
2407 *walk_subtrees = 2;
2409 return NULL;
2412 /* walk_tree helper function for expr_visibility. */
2414 static tree
2415 min_vis_expr_r (tree *tp, int */*walk_subtrees*/, void *data)
2417 int *vis_p = (int *)data;
2418 int tpvis = VISIBILITY_DEFAULT;
2420 switch (TREE_CODE (*tp))
2422 case CAST_EXPR:
2423 case IMPLICIT_CONV_EXPR:
2424 case STATIC_CAST_EXPR:
2425 case REINTERPRET_CAST_EXPR:
2426 case CONST_CAST_EXPR:
2427 case DYNAMIC_CAST_EXPR:
2428 case NEW_EXPR:
2429 case CONSTRUCTOR:
2430 case LAMBDA_EXPR:
2431 tpvis = type_visibility (TREE_TYPE (*tp));
2432 break;
2434 case VAR_DECL:
2435 case FUNCTION_DECL:
2436 if (! TREE_PUBLIC (*tp))
2437 tpvis = VISIBILITY_ANON;
2438 else
2439 tpvis = DECL_VISIBILITY (*tp);
2440 break;
2442 default:
2443 break;
2446 if (tpvis > *vis_p)
2447 *vis_p = tpvis;
2449 return NULL_TREE;
2452 /* Returns the visibility of TYPE, which is the minimum visibility of its
2453 component types. */
2455 static int
2456 type_visibility (tree type)
2458 int vis = VISIBILITY_DEFAULT;
2459 cp_walk_tree_without_duplicates (&type, min_vis_r, &vis);
2460 return vis;
2463 /* Returns the visibility of an expression EXPR that appears in the signature
2464 of a function template, which is the minimum visibility of names that appear
2465 in its mangling. */
2467 static int
2468 expr_visibility (tree expr)
2470 int vis = VISIBILITY_DEFAULT;
2471 cp_walk_tree_without_duplicates (&expr, min_vis_expr_r, &vis);
2472 return vis;
2475 /* Limit the visibility of DECL to VISIBILITY, if not explicitly
2476 specified (or if VISIBILITY is static). If TMPL is true, this
2477 constraint is for a template argument, and takes precedence
2478 over explicitly-specified visibility on the template. */
2480 static void
2481 constrain_visibility (tree decl, int visibility, bool tmpl)
2483 if (visibility == VISIBILITY_ANON)
2485 /* extern "C" declarations aren't affected by the anonymous
2486 namespace. */
2487 if (!DECL_EXTERN_C_P (decl))
2489 TREE_PUBLIC (decl) = 0;
2490 DECL_WEAK (decl) = 0;
2491 DECL_COMMON (decl) = 0;
2492 DECL_COMDAT (decl) = false;
2493 if (VAR_OR_FUNCTION_DECL_P (decl))
2495 struct symtab_node *snode = symtab_node::get (decl);
2497 if (snode)
2498 snode->set_comdat_group (NULL);
2500 DECL_INTERFACE_KNOWN (decl) = 1;
2501 if (DECL_LANG_SPECIFIC (decl))
2502 DECL_NOT_REALLY_EXTERN (decl) = 1;
2505 else if (visibility > DECL_VISIBILITY (decl)
2506 && (tmpl || !DECL_VISIBILITY_SPECIFIED (decl)))
2508 DECL_VISIBILITY (decl) = (enum symbol_visibility) visibility;
2509 /* This visibility was not specified. */
2510 DECL_VISIBILITY_SPECIFIED (decl) = false;
2514 /* Constrain the visibility of DECL based on the visibility of its template
2515 arguments. */
2517 static void
2518 constrain_visibility_for_template (tree decl, tree targs)
2520 /* If this is a template instantiation, check the innermost
2521 template args for visibility constraints. The outer template
2522 args are covered by the class check. */
2523 tree args = INNERMOST_TEMPLATE_ARGS (targs);
2524 int i;
2525 for (i = TREE_VEC_LENGTH (args); i > 0; --i)
2527 int vis = 0;
2529 tree arg = TREE_VEC_ELT (args, i-1);
2530 if (TYPE_P (arg))
2531 vis = type_visibility (arg);
2532 else
2533 vis = expr_visibility (arg);
2534 if (vis)
2535 constrain_visibility (decl, vis, true);
2539 /* Like c_determine_visibility, but with additional C++-specific
2540 behavior.
2542 Function-scope entities can rely on the function's visibility because
2543 it is set in start_preparsed_function.
2545 Class-scope entities cannot rely on the class's visibility until the end
2546 of the enclosing class definition.
2548 Note that because namespaces have multiple independent definitions,
2549 namespace visibility is handled elsewhere using the #pragma visibility
2550 machinery rather than by decorating the namespace declaration.
2552 The goal is for constraints from the type to give a diagnostic, and
2553 other constraints to be applied silently. */
2555 void
2556 determine_visibility (tree decl)
2558 /* Remember that all decls get VISIBILITY_DEFAULT when built. */
2560 /* Only relevant for names with external linkage. */
2561 if (!TREE_PUBLIC (decl))
2562 return;
2564 /* Cloned constructors and destructors get the same visibility as
2565 the underlying function. That should be set up in
2566 maybe_clone_body. */
2567 gcc_assert (!DECL_CLONED_FUNCTION_P (decl));
2569 bool orig_visibility_specified = DECL_VISIBILITY_SPECIFIED (decl);
2570 enum symbol_visibility orig_visibility = DECL_VISIBILITY (decl);
2572 /* The decl may be a template instantiation, which could influence
2573 visibilty. */
2574 tree template_decl = NULL_TREE;
2575 if (TREE_CODE (decl) == TYPE_DECL)
2577 if (CLASS_TYPE_P (TREE_TYPE (decl)))
2579 if (CLASSTYPE_USE_TEMPLATE (TREE_TYPE (decl)))
2580 template_decl = decl;
2582 else if (TYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
2583 template_decl = decl;
2585 else if (DECL_LANG_SPECIFIC (decl) && DECL_USE_TEMPLATE (decl))
2586 template_decl = decl;
2588 if (TREE_CODE (decl) == TYPE_DECL
2589 && LAMBDA_TYPE_P (TREE_TYPE (decl))
2590 && CLASSTYPE_LAMBDA_EXPR (TREE_TYPE (decl)) != error_mark_node)
2591 if (tree extra = LAMBDA_TYPE_EXTRA_SCOPE (TREE_TYPE (decl)))
2593 /* The lambda's visibility is limited by that of its extra
2594 scope. */
2595 int vis = 0;
2596 if (TYPE_P (extra))
2597 vis = type_visibility (extra);
2598 else
2599 vis = expr_visibility (extra);
2600 constrain_visibility (decl, vis, false);
2603 /* If DECL is a member of a class, visibility specifiers on the
2604 class can influence the visibility of the DECL. */
2605 tree class_type = NULL_TREE;
2606 if (DECL_CLASS_SCOPE_P (decl))
2607 class_type = DECL_CONTEXT (decl);
2608 else
2610 /* Not a class member. */
2612 /* Virtual tables have DECL_CONTEXT set to their associated class,
2613 so they are automatically handled above. */
2614 gcc_assert (!VAR_P (decl)
2615 || !DECL_VTABLE_OR_VTT_P (decl));
2617 if (DECL_FUNCTION_SCOPE_P (decl) && ! DECL_VISIBILITY_SPECIFIED (decl))
2619 /* Local statics and classes get the visibility of their
2620 containing function by default, except that
2621 -fvisibility-inlines-hidden doesn't affect them. */
2622 tree fn = DECL_CONTEXT (decl);
2623 if (DECL_VISIBILITY_SPECIFIED (fn))
2625 DECL_VISIBILITY (decl) = DECL_VISIBILITY (fn);
2626 DECL_VISIBILITY_SPECIFIED (decl) =
2627 DECL_VISIBILITY_SPECIFIED (fn);
2629 else
2631 if (DECL_CLASS_SCOPE_P (fn))
2632 determine_visibility_from_class (decl, DECL_CONTEXT (fn));
2633 else if (determine_hidden_inline (fn))
2635 DECL_VISIBILITY (decl) = default_visibility;
2636 DECL_VISIBILITY_SPECIFIED (decl) =
2637 visibility_options.inpragma;
2639 else
2641 DECL_VISIBILITY (decl) = DECL_VISIBILITY (fn);
2642 DECL_VISIBILITY_SPECIFIED (decl) =
2643 DECL_VISIBILITY_SPECIFIED (fn);
2647 /* Local classes in templates have CLASSTYPE_USE_TEMPLATE set,
2648 but have no TEMPLATE_INFO, so don't try to check it. */
2649 template_decl = NULL_TREE;
2651 else if (VAR_P (decl) && DECL_TINFO_P (decl)
2652 && flag_visibility_ms_compat)
2654 /* Under -fvisibility-ms-compat, types are visible by default,
2655 even though their contents aren't. */
2656 tree underlying_type = TREE_TYPE (DECL_NAME (decl));
2657 int underlying_vis = type_visibility (underlying_type);
2658 if (underlying_vis == VISIBILITY_ANON
2659 || (CLASS_TYPE_P (underlying_type)
2660 && CLASSTYPE_VISIBILITY_SPECIFIED (underlying_type)))
2661 constrain_visibility (decl, underlying_vis, false);
2662 else
2663 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
2665 else if (VAR_P (decl) && DECL_TINFO_P (decl))
2667 /* tinfo visibility is based on the type it's for. */
2668 constrain_visibility
2669 (decl, type_visibility (TREE_TYPE (DECL_NAME (decl))), false);
2671 /* Give the target a chance to override the visibility associated
2672 with DECL. */
2673 if (TREE_PUBLIC (decl)
2674 && !DECL_REALLY_EXTERN (decl)
2675 && CLASS_TYPE_P (TREE_TYPE (DECL_NAME (decl)))
2676 && !CLASSTYPE_VISIBILITY_SPECIFIED (TREE_TYPE (DECL_NAME (decl))))
2677 targetm.cxx.determine_class_data_visibility (decl);
2679 else if (template_decl)
2680 /* Template instantiations and specializations get visibility based
2681 on their template unless they override it with an attribute. */;
2682 else if (! DECL_VISIBILITY_SPECIFIED (decl))
2684 if (determine_hidden_inline (decl))
2685 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
2686 else
2688 /* Set default visibility to whatever the user supplied with
2689 #pragma GCC visibility or a namespace visibility attribute. */
2690 DECL_VISIBILITY (decl) = default_visibility;
2691 DECL_VISIBILITY_SPECIFIED (decl) = visibility_options.inpragma;
2696 if (template_decl)
2698 /* If the specialization doesn't specify visibility, use the
2699 visibility from the template. */
2700 tree tinfo = get_template_info (template_decl);
2701 tree args = TI_ARGS (tinfo);
2702 tree attribs = (TREE_CODE (decl) == TYPE_DECL
2703 ? TYPE_ATTRIBUTES (TREE_TYPE (decl))
2704 : DECL_ATTRIBUTES (decl));
2705 tree attr = lookup_attribute ("visibility", attribs);
2707 if (args != error_mark_node)
2709 tree pattern = DECL_TEMPLATE_RESULT (TI_TEMPLATE (tinfo));
2711 if (!DECL_VISIBILITY_SPECIFIED (decl))
2713 if (!attr
2714 && determine_hidden_inline (decl))
2715 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
2716 else
2718 DECL_VISIBILITY (decl) = DECL_VISIBILITY (pattern);
2719 DECL_VISIBILITY_SPECIFIED (decl)
2720 = DECL_VISIBILITY_SPECIFIED (pattern);
2724 if (args
2725 /* Template argument visibility outweighs #pragma or namespace
2726 visibility, but not an explicit attribute. */
2727 && !attr)
2729 int depth = TMPL_ARGS_DEPTH (args);
2730 if (DECL_VISIBILITY_SPECIFIED (decl))
2732 /* A class template member with explicit visibility
2733 overrides the class visibility, so we need to apply
2734 all the levels of template args directly. */
2735 int i;
2736 for (i = 1; i <= depth; ++i)
2738 tree lev = TMPL_ARGS_LEVEL (args, i);
2739 constrain_visibility_for_template (decl, lev);
2742 else if (PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo)))
2743 /* Limit visibility based on its template arguments. */
2744 constrain_visibility_for_template (decl, args);
2749 if (class_type)
2750 determine_visibility_from_class (decl, class_type);
2752 if (decl_anon_ns_mem_p (decl))
2753 /* Names in an anonymous namespace get internal linkage. */
2754 constrain_visibility (decl, VISIBILITY_ANON, false);
2755 else if (TREE_CODE (decl) != TYPE_DECL)
2757 /* Propagate anonymity from type to decl. */
2758 int tvis = type_visibility (TREE_TYPE (decl));
2759 if (tvis == VISIBILITY_ANON
2760 || ! DECL_VISIBILITY_SPECIFIED (decl))
2761 constrain_visibility (decl, tvis, false);
2763 else if (no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/true))
2764 /* DR 757: A type without linkage shall not be used as the type of a
2765 variable or function with linkage, unless
2766 o the variable or function has extern "C" linkage (7.5 [dcl.link]), or
2767 o the variable or function is not used (3.2 [basic.def.odr]) or is
2768 defined in the same translation unit.
2770 Since non-extern "C" decls need to be defined in the same
2771 translation unit, we can make the type internal. */
2772 constrain_visibility (decl, VISIBILITY_ANON, false);
2774 /* If visibility changed and DECL already has DECL_RTL, ensure
2775 symbol flags are updated. */
2776 if ((DECL_VISIBILITY (decl) != orig_visibility
2777 || DECL_VISIBILITY_SPECIFIED (decl) != orig_visibility_specified)
2778 && ((VAR_P (decl) && TREE_STATIC (decl))
2779 || TREE_CODE (decl) == FUNCTION_DECL)
2780 && DECL_RTL_SET_P (decl))
2781 make_decl_rtl (decl);
2784 /* By default, static data members and function members receive
2785 the visibility of their containing class. */
2787 static void
2788 determine_visibility_from_class (tree decl, tree class_type)
2790 if (DECL_VISIBILITY_SPECIFIED (decl))
2791 return;
2793 if (determine_hidden_inline (decl))
2794 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
2795 else
2797 /* Default to the class visibility. */
2798 DECL_VISIBILITY (decl) = CLASSTYPE_VISIBILITY (class_type);
2799 DECL_VISIBILITY_SPECIFIED (decl)
2800 = CLASSTYPE_VISIBILITY_SPECIFIED (class_type);
2803 /* Give the target a chance to override the visibility associated
2804 with DECL. */
2805 if (VAR_P (decl)
2806 && TREE_PUBLIC (decl)
2807 && (DECL_TINFO_P (decl) || DECL_VTABLE_OR_VTT_P (decl))
2808 && !DECL_REALLY_EXTERN (decl)
2809 && !CLASSTYPE_VISIBILITY_SPECIFIED (class_type))
2810 targetm.cxx.determine_class_data_visibility (decl);
2813 /* Returns true iff DECL is an inline that should get hidden visibility
2814 because of -fvisibility-inlines-hidden. */
2816 static bool
2817 determine_hidden_inline (tree decl)
2819 return (visibility_options.inlines_hidden
2820 /* Don't do this for inline templates; specializations might not be
2821 inline, and we don't want them to inherit the hidden
2822 visibility. We'll set it here for all inline instantiations. */
2823 && !processing_template_decl
2824 && TREE_CODE (decl) == FUNCTION_DECL
2825 && DECL_DECLARED_INLINE_P (decl)
2826 && (! DECL_LANG_SPECIFIC (decl)
2827 || ! DECL_EXPLICIT_INSTANTIATION (decl)));
2830 /* Constrain the visibility of a class TYPE based on the visibility of its
2831 field types. Warn if any fields require lesser visibility. */
2833 void
2834 constrain_class_visibility (tree type)
2836 tree binfo;
2837 tree t;
2838 int i;
2840 int vis = type_visibility (type);
2842 if (vis == VISIBILITY_ANON
2843 || DECL_IN_SYSTEM_HEADER (TYPE_MAIN_DECL (type)))
2844 return;
2846 /* Don't warn about visibility if the class has explicit visibility. */
2847 if (CLASSTYPE_VISIBILITY_SPECIFIED (type))
2848 vis = VISIBILITY_INTERNAL;
2850 for (t = TYPE_FIELDS (type); t; t = DECL_CHAIN (t))
2851 if (TREE_CODE (t) == FIELD_DECL && TREE_TYPE (t) != error_mark_node
2852 && !DECL_ARTIFICIAL (t))
2854 tree ftype = strip_pointer_or_array_types (TREE_TYPE (t));
2855 int subvis = type_visibility (ftype);
2857 if (subvis == VISIBILITY_ANON)
2859 if (!in_main_input_context())
2861 tree nlt = no_linkage_check (ftype, /*relaxed_p=*/false);
2862 if (nlt)
2864 if (same_type_p (TREE_TYPE (t), nlt))
2865 warning (OPT_Wsubobject_linkage, "\
2866 %qT has a field %qD whose type has no linkage",
2867 type, t);
2868 else
2869 warning (OPT_Wsubobject_linkage, "\
2870 %qT has a field %qD whose type depends on the type %qT which has no linkage",
2871 type, t, nlt);
2873 else
2874 warning (OPT_Wsubobject_linkage, "\
2875 %qT has a field %qD whose type uses the anonymous namespace",
2876 type, t);
2879 else if (MAYBE_CLASS_TYPE_P (ftype)
2880 && vis < VISIBILITY_HIDDEN
2881 && subvis >= VISIBILITY_HIDDEN)
2882 warning (OPT_Wattributes, "\
2883 %qT declared with greater visibility than the type of its field %qD",
2884 type, t);
2887 binfo = TYPE_BINFO (type);
2888 for (i = 0; BINFO_BASE_ITERATE (binfo, i, t); ++i)
2890 int subvis = type_visibility (TREE_TYPE (t));
2892 if (subvis == VISIBILITY_ANON)
2894 if (!in_main_input_context())
2896 tree nlt = no_linkage_check (TREE_TYPE (t), /*relaxed_p=*/false);
2897 if (nlt)
2899 if (same_type_p (TREE_TYPE (t), nlt))
2900 warning (OPT_Wsubobject_linkage, "\
2901 %qT has a base %qT whose type has no linkage",
2902 type, TREE_TYPE (t));
2903 else
2904 warning (OPT_Wsubobject_linkage, "\
2905 %qT has a base %qT whose type depends on the type %qT which has no linkage",
2906 type, TREE_TYPE (t), nlt);
2908 else
2909 warning (OPT_Wsubobject_linkage, "\
2910 %qT has a base %qT whose type uses the anonymous namespace",
2911 type, TREE_TYPE (t));
2914 else if (vis < VISIBILITY_HIDDEN
2915 && subvis >= VISIBILITY_HIDDEN)
2916 warning (OPT_Wattributes, "\
2917 %qT declared with greater visibility than its base %qT",
2918 type, TREE_TYPE (t));
2922 /* Functions for adjusting the visibility of a tagged type and its nested
2923 types and declarations when it gets a name for linkage purposes from a
2924 typedef. */
2925 // FIXME: It is now a DR for such a class type to contain anything
2926 // other than C. So at minium most of this can probably be deleted.
2928 /* First reset the visibility of all the types. */
2930 static void
2931 reset_type_linkage_1 (tree type)
2933 set_linkage_according_to_type (type, TYPE_MAIN_DECL (type));
2934 if (CLASS_TYPE_P (type))
2935 for (tree member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
2936 if (DECL_IMPLICIT_TYPEDEF_P (member))
2937 reset_type_linkage_1 (TREE_TYPE (member));
2940 /* Then reset the visibility of any static data members or member
2941 functions that use those types. */
2943 static void
2944 reset_decl_linkage (tree decl)
2946 if (TREE_PUBLIC (decl))
2947 return;
2948 if (DECL_CLONED_FUNCTION_P (decl))
2949 return;
2950 TREE_PUBLIC (decl) = true;
2951 DECL_INTERFACE_KNOWN (decl) = false;
2952 determine_visibility (decl);
2953 tentative_decl_linkage (decl);
2956 void
2957 reset_type_linkage (tree type)
2959 reset_type_linkage_1 (type);
2960 if (CLASS_TYPE_P (type))
2962 if (tree vt = CLASSTYPE_VTABLES (type))
2964 tree name = mangle_vtbl_for_type (type);
2965 DECL_NAME (vt) = name;
2966 SET_DECL_ASSEMBLER_NAME (vt, name);
2967 reset_decl_linkage (vt);
2969 if (tree ti = CLASSTYPE_TYPEINFO_VAR (type))
2971 tree name = mangle_typeinfo_for_type (type);
2972 DECL_NAME (ti) = name;
2973 SET_DECL_ASSEMBLER_NAME (ti, name);
2974 TREE_TYPE (name) = type;
2975 reset_decl_linkage (ti);
2977 for (tree m = TYPE_FIELDS (type); m; m = DECL_CHAIN (m))
2979 tree mem = STRIP_TEMPLATE (m);
2980 if (TREE_CODE (mem) == VAR_DECL || TREE_CODE (mem) == FUNCTION_DECL)
2981 reset_decl_linkage (mem);
2982 else if (DECL_IMPLICIT_TYPEDEF_P (mem))
2983 reset_type_linkage (TREE_TYPE (mem));
2988 /* Set up our initial idea of what the linkage of DECL should be. */
2990 void
2991 tentative_decl_linkage (tree decl)
2993 if (DECL_INTERFACE_KNOWN (decl))
2994 /* We've already made a decision as to how this function will
2995 be handled. */;
2996 else if (vague_linkage_p (decl))
2998 if (TREE_CODE (decl) == FUNCTION_DECL
2999 && decl_defined_p (decl))
3001 DECL_EXTERNAL (decl) = 1;
3002 DECL_NOT_REALLY_EXTERN (decl) = 1;
3003 note_vague_linkage_fn (decl);
3004 /* A non-template inline function with external linkage will
3005 always be COMDAT. As we must eventually determine the
3006 linkage of all functions, and as that causes writes to
3007 the data mapped in from the PCH file, it's advantageous
3008 to mark the functions at this point. */
3009 if (DECL_DECLARED_INLINE_P (decl)
3010 && (!DECL_IMPLICIT_INSTANTIATION (decl)
3011 || DECL_DEFAULTED_FN (decl)))
3013 /* This function must have external linkage, as
3014 otherwise DECL_INTERFACE_KNOWN would have been
3015 set. */
3016 gcc_assert (TREE_PUBLIC (decl));
3017 comdat_linkage (decl);
3018 DECL_INTERFACE_KNOWN (decl) = 1;
3021 else if (VAR_P (decl))
3022 maybe_commonize_var (decl);
3026 /* DECL is a FUNCTION_DECL or VAR_DECL. If the object file linkage
3027 for DECL has not already been determined, do so now by setting
3028 DECL_EXTERNAL, DECL_COMDAT and other related flags. Until this
3029 function is called entities with vague linkage whose definitions
3030 are available must have TREE_PUBLIC set.
3032 If this function decides to place DECL in COMDAT, it will set
3033 appropriate flags -- but will not clear DECL_EXTERNAL. It is up to
3034 the caller to decide whether or not to clear DECL_EXTERNAL. Some
3035 callers defer that decision until it is clear that DECL is actually
3036 required. */
3038 void
3039 import_export_decl (tree decl)
3041 bool comdat_p;
3042 bool import_p;
3043 tree class_type = NULL_TREE;
3045 if (DECL_INTERFACE_KNOWN (decl))
3046 return;
3048 /* We cannot determine what linkage to give to an entity with vague
3049 linkage until the end of the file. For example, a virtual table
3050 for a class will be defined if and only if the key method is
3051 defined in this translation unit. */
3052 gcc_assert (at_eof);
3053 /* Object file linkage for explicit instantiations is handled in
3054 mark_decl_instantiated. For static variables in functions with
3055 vague linkage, maybe_commonize_var is used.
3057 Therefore, the only declarations that should be provided to this
3058 function are those with external linkage that are:
3060 * implicit instantiations of function templates
3062 * inline function
3064 * implicit instantiations of static data members of class
3065 templates
3067 * virtual tables
3069 * typeinfo objects
3071 Furthermore, all entities that reach this point must have a
3072 definition available in this translation unit.
3074 The following assertions check these conditions. */
3075 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
3076 /* Any code that creates entities with TREE_PUBLIC cleared should
3077 also set DECL_INTERFACE_KNOWN. */
3078 gcc_assert (TREE_PUBLIC (decl));
3079 if (TREE_CODE (decl) == FUNCTION_DECL)
3080 gcc_assert (DECL_IMPLICIT_INSTANTIATION (decl)
3081 || DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl)
3082 || DECL_DECLARED_INLINE_P (decl));
3083 else
3084 gcc_assert (DECL_IMPLICIT_INSTANTIATION (decl)
3085 || DECL_VTABLE_OR_VTT_P (decl)
3086 || DECL_TINFO_P (decl));
3087 /* Check that a definition of DECL is available in this translation
3088 unit. */
3089 gcc_assert (!DECL_REALLY_EXTERN (decl));
3091 /* Assume that DECL will not have COMDAT linkage. */
3092 comdat_p = false;
3093 /* Assume that DECL will not be imported into this translation
3094 unit. */
3095 import_p = false;
3097 if (VAR_P (decl) && DECL_VTABLE_OR_VTT_P (decl))
3099 class_type = DECL_CONTEXT (decl);
3100 import_export_class (class_type);
3101 if (CLASSTYPE_INTERFACE_KNOWN (class_type)
3102 && CLASSTYPE_INTERFACE_ONLY (class_type))
3103 import_p = true;
3104 else if ((!flag_weak || TARGET_WEAK_NOT_IN_ARCHIVE_TOC)
3105 && !CLASSTYPE_USE_TEMPLATE (class_type)
3106 && CLASSTYPE_KEY_METHOD (class_type)
3107 && !DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (class_type)))
3108 /* The ABI requires that all virtual tables be emitted with
3109 COMDAT linkage. However, on systems where COMDAT symbols
3110 don't show up in the table of contents for a static
3111 archive, or on systems without weak symbols (where we
3112 approximate COMDAT linkage by using internal linkage), the
3113 linker will report errors about undefined symbols because
3114 it will not see the virtual table definition. Therefore,
3115 in the case that we know that the virtual table will be
3116 emitted in only one translation unit, we make the virtual
3117 table an ordinary definition with external linkage. */
3118 DECL_EXTERNAL (decl) = 0;
3119 else if (CLASSTYPE_INTERFACE_KNOWN (class_type))
3121 /* CLASS_TYPE is being exported from this translation unit,
3122 so DECL should be defined here. */
3123 if (!flag_weak && CLASSTYPE_EXPLICIT_INSTANTIATION (class_type))
3124 /* If a class is declared in a header with the "extern
3125 template" extension, then it will not be instantiated,
3126 even in translation units that would normally require
3127 it. Often such classes are explicitly instantiated in
3128 one translation unit. Therefore, the explicit
3129 instantiation must be made visible to other translation
3130 units. */
3131 DECL_EXTERNAL (decl) = 0;
3132 else
3134 /* The generic C++ ABI says that class data is always
3135 COMDAT, even if there is a key function. Some
3136 variants (e.g., the ARM EABI) says that class data
3137 only has COMDAT linkage if the class data might be
3138 emitted in more than one translation unit. When the
3139 key method can be inline and is inline, we still have
3140 to arrange for comdat even though
3141 class_data_always_comdat is false. */
3142 if (!CLASSTYPE_KEY_METHOD (class_type)
3143 || DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (class_type))
3144 || targetm.cxx.class_data_always_comdat ())
3146 /* The ABI requires COMDAT linkage. Normally, we
3147 only emit COMDAT things when they are needed;
3148 make sure that we realize that this entity is
3149 indeed needed. */
3150 comdat_p = true;
3151 mark_needed (decl);
3155 else if (!flag_implicit_templates
3156 && CLASSTYPE_IMPLICIT_INSTANTIATION (class_type))
3157 import_p = true;
3158 else
3159 comdat_p = true;
3161 else if (VAR_P (decl) && DECL_TINFO_P (decl))
3163 tree type = TREE_TYPE (DECL_NAME (decl));
3164 if (CLASS_TYPE_P (type))
3166 class_type = type;
3167 import_export_class (type);
3168 if (CLASSTYPE_INTERFACE_KNOWN (type)
3169 && TYPE_POLYMORPHIC_P (type)
3170 && CLASSTYPE_INTERFACE_ONLY (type)
3171 /* If -fno-rtti was specified, then we cannot be sure
3172 that RTTI information will be emitted with the
3173 virtual table of the class, so we must emit it
3174 wherever it is used. */
3175 && flag_rtti)
3176 import_p = true;
3177 else
3179 if (CLASSTYPE_INTERFACE_KNOWN (type)
3180 && !CLASSTYPE_INTERFACE_ONLY (type))
3182 comdat_p = (targetm.cxx.class_data_always_comdat ()
3183 || (CLASSTYPE_KEY_METHOD (type)
3184 && DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (type))));
3185 mark_needed (decl);
3186 if (!flag_weak)
3188 comdat_p = false;
3189 DECL_EXTERNAL (decl) = 0;
3192 else
3193 comdat_p = true;
3196 else
3197 comdat_p = true;
3199 else if (DECL_TEMPLOID_INSTANTIATION (decl))
3201 /* DECL is an implicit instantiation of a function or static
3202 data member. */
3203 if (flag_implicit_templates
3204 || (flag_implicit_inline_templates
3205 && TREE_CODE (decl) == FUNCTION_DECL
3206 && DECL_DECLARED_INLINE_P (decl)))
3207 comdat_p = true;
3208 else
3209 /* If we are not implicitly generating templates, then mark
3210 this entity as undefined in this translation unit. */
3211 import_p = true;
3213 else if (DECL_FUNCTION_MEMBER_P (decl))
3215 if (!DECL_DECLARED_INLINE_P (decl))
3217 tree ctype = DECL_CONTEXT (decl);
3218 import_export_class (ctype);
3219 if (CLASSTYPE_INTERFACE_KNOWN (ctype))
3221 DECL_NOT_REALLY_EXTERN (decl)
3222 = ! (CLASSTYPE_INTERFACE_ONLY (ctype)
3223 || (DECL_DECLARED_INLINE_P (decl)
3224 && ! flag_implement_inlines
3225 && !DECL_VINDEX (decl)));
3227 if (!DECL_NOT_REALLY_EXTERN (decl))
3228 DECL_EXTERNAL (decl) = 1;
3230 /* Always make artificials weak. */
3231 if (DECL_ARTIFICIAL (decl) && flag_weak)
3232 comdat_p = true;
3233 else
3234 maybe_make_one_only (decl);
3237 else
3238 comdat_p = true;
3240 else
3241 comdat_p = true;
3243 if (import_p)
3245 /* If we are importing DECL into this translation unit, mark is
3246 an undefined here. */
3247 DECL_EXTERNAL (decl) = 1;
3248 DECL_NOT_REALLY_EXTERN (decl) = 0;
3250 else if (comdat_p)
3252 /* If we decided to put DECL in COMDAT, mark it accordingly at
3253 this point. */
3254 comdat_linkage (decl);
3257 DECL_INTERFACE_KNOWN (decl) = 1;
3260 /* Return an expression that performs the destruction of DECL, which
3261 must be a VAR_DECL whose type has a non-trivial destructor, or is
3262 an array whose (innermost) elements have a non-trivial destructor. */
3264 tree
3265 build_cleanup (tree decl)
3267 tree clean = cxx_maybe_build_cleanup (decl, tf_warning_or_error);
3268 gcc_assert (clean != NULL_TREE);
3269 return clean;
3272 /* GUARD is a helper variable for DECL; make them have the same linkage and
3273 visibility. */
3275 void
3276 copy_linkage (tree guard, tree decl)
3278 TREE_PUBLIC (guard) = TREE_PUBLIC (decl);
3279 TREE_STATIC (guard) = TREE_STATIC (decl);
3280 DECL_COMMON (guard) = DECL_COMMON (decl);
3281 DECL_COMDAT (guard) = DECL_COMDAT (decl);
3282 if (TREE_STATIC (guard))
3284 CP_DECL_THREAD_LOCAL_P (guard) = CP_DECL_THREAD_LOCAL_P (decl);
3285 set_decl_tls_model (guard, DECL_TLS_MODEL (decl));
3286 if (DECL_ONE_ONLY (decl))
3287 make_decl_one_only (guard, cxx_comdat_group (guard));
3288 if (TREE_PUBLIC (decl))
3289 DECL_WEAK (guard) = DECL_WEAK (decl);
3290 /* Also check vague_linkage_p, as DECL_WEAK and DECL_ONE_ONLY might not
3291 be set until import_export_decl at EOF. */
3292 if (vague_linkage_p (decl))
3293 comdat_linkage (guard);
3294 DECL_VISIBILITY (guard) = DECL_VISIBILITY (decl);
3295 DECL_VISIBILITY_SPECIFIED (guard) = DECL_VISIBILITY_SPECIFIED (decl);
3299 /* Returns the initialization guard variable for the variable DECL,
3300 which has static storage duration. */
3302 tree
3303 get_guard (tree decl)
3305 tree sname = mangle_guard_variable (decl);
3306 tree guard = get_global_binding (sname);
3307 if (! guard)
3309 tree guard_type;
3311 /* We use a type that is big enough to contain a mutex as well
3312 as an integer counter. */
3313 guard_type = targetm.cxx.guard_type ();
3314 guard = build_decl (DECL_SOURCE_LOCATION (decl),
3315 VAR_DECL, sname, guard_type);
3317 /* The guard should have the same linkage as what it guards. */
3318 copy_linkage (guard, decl);
3320 DECL_ARTIFICIAL (guard) = 1;
3321 DECL_IGNORED_P (guard) = 1;
3322 TREE_USED (guard) = 1;
3323 pushdecl_top_level_and_finish (guard, NULL_TREE);
3325 return guard;
3328 /* Returns true if accessing the GUARD atomic is expensive,
3329 i.e. involves a call to __sync_synchronize or similar.
3330 In this case let __cxa_guard_acquire handle the atomics. */
3332 static bool
3333 is_atomic_expensive_p (machine_mode mode)
3335 if (!flag_inline_atomics)
3336 return true;
3338 if (!can_compare_and_swap_p (mode, false) || !can_atomic_load_p (mode))
3339 return true;
3341 return false;
3344 /* Return an atomic load of src with the appropriate memory model. */
3346 static tree
3347 build_atomic_load_type (tree src, HOST_WIDE_INT model, tree type)
3349 tree ptr_type = build_pointer_type (type);
3350 tree mem_model = build_int_cst (integer_type_node, model);
3351 tree t, addr, val;
3352 unsigned int size;
3353 int fncode;
3355 size = tree_to_uhwi (TYPE_SIZE_UNIT (type));
3357 fncode = BUILT_IN_ATOMIC_LOAD_N + exact_log2 (size) + 1;
3358 t = builtin_decl_implicit ((enum built_in_function) fncode);
3360 addr = build1 (ADDR_EXPR, ptr_type, src);
3361 val = build_call_expr (t, 2, addr, mem_model);
3362 return val;
3365 /* Return those bits of the GUARD variable that should be set when the
3366 guarded entity is actually initialized. */
3368 static tree
3369 get_guard_bits (tree guard)
3371 if (!targetm.cxx.guard_mask_bit ())
3373 /* We only set the first byte of the guard, in order to leave room
3374 for a mutex in the high-order bits. */
3375 guard = build1 (ADDR_EXPR,
3376 build_pointer_type (TREE_TYPE (guard)),
3377 guard);
3378 guard = build1 (NOP_EXPR,
3379 build_pointer_type (char_type_node),
3380 guard);
3381 guard = build1 (INDIRECT_REF, char_type_node, guard);
3384 return guard;
3387 /* Return an expression which determines whether or not the GUARD
3388 variable has already been initialized. */
3390 tree
3391 get_guard_cond (tree guard, bool thread_safe)
3393 tree guard_value;
3395 if (!thread_safe)
3396 guard = get_guard_bits (guard);
3397 else
3399 tree type = targetm.cxx.guard_mask_bit ()
3400 ? TREE_TYPE (guard) : char_type_node;
3402 if (is_atomic_expensive_p (TYPE_MODE (type)))
3403 guard = integer_zero_node;
3404 else
3405 guard = build_atomic_load_type (guard, MEMMODEL_ACQUIRE, type);
3408 /* Mask off all but the low bit. */
3409 if (targetm.cxx.guard_mask_bit ())
3411 guard_value = integer_one_node;
3412 if (!same_type_p (TREE_TYPE (guard_value), TREE_TYPE (guard)))
3413 guard_value = fold_convert (TREE_TYPE (guard), guard_value);
3414 guard = cp_build_binary_op (input_location,
3415 BIT_AND_EXPR, guard, guard_value,
3416 tf_warning_or_error);
3419 guard_value = integer_zero_node;
3420 if (!same_type_p (TREE_TYPE (guard_value), TREE_TYPE (guard)))
3421 guard_value = fold_convert (TREE_TYPE (guard), guard_value);
3422 return cp_build_binary_op (input_location,
3423 EQ_EXPR, guard, guard_value,
3424 tf_warning_or_error);
3427 /* Return an expression which sets the GUARD variable, indicating that
3428 the variable being guarded has been initialized. */
3430 tree
3431 set_guard (tree guard)
3433 tree guard_init;
3435 /* Set the GUARD to one. */
3436 guard = get_guard_bits (guard);
3437 guard_init = integer_one_node;
3438 if (!same_type_p (TREE_TYPE (guard_init), TREE_TYPE (guard)))
3439 guard_init = fold_convert (TREE_TYPE (guard), guard_init);
3440 return cp_build_modify_expr (input_location, guard, NOP_EXPR, guard_init,
3441 tf_warning_or_error);
3444 /* Returns true iff we can tell that VAR does not have a dynamic
3445 initializer. */
3447 static bool
3448 var_defined_without_dynamic_init (tree var)
3450 /* constinit vars are guaranteed to not have dynamic initializer,
3451 but still registering the destructor counts as dynamic initialization. */
3452 if (DECL_DECLARED_CONSTINIT_P (var)
3453 && COMPLETE_TYPE_P (TREE_TYPE (var))
3454 && !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (var)))
3455 return true;
3456 /* If it's defined in another TU, we can't tell. */
3457 if (DECL_EXTERNAL (var))
3458 return false;
3459 /* If it has a non-trivial destructor, registering the destructor
3460 counts as dynamic initialization. */
3461 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (var)))
3462 return false;
3463 /* If it's in this TU, its initializer has been processed, unless
3464 it's a case of self-initialization, then DECL_INITIALIZED_P is
3465 false while the initializer is handled by finish_id_expression. */
3466 if (!DECL_INITIALIZED_P (var))
3467 return false;
3468 /* If it has no initializer or a constant one, it's not dynamic. */
3469 return (!DECL_NONTRIVIALLY_INITIALIZED_P (var)
3470 || DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (var));
3473 /* Returns true iff VAR is a variable that needs uses to be
3474 wrapped for possible dynamic initialization. */
3476 static bool
3477 var_needs_tls_wrapper (tree var)
3479 return (!error_operand_p (var)
3480 && CP_DECL_THREAD_LOCAL_P (var)
3481 && !DECL_GNU_TLS_P (var)
3482 && !DECL_FUNCTION_SCOPE_P (var)
3483 && !var_defined_without_dynamic_init (var));
3486 /* Get the FUNCTION_DECL for the shared TLS init function for this
3487 translation unit. */
3489 static tree
3490 get_local_tls_init_fn (location_t loc)
3492 tree sname = get_identifier ("__tls_init");
3493 tree fn = get_global_binding (sname);
3494 if (!fn)
3496 fn = build_lang_decl_loc (loc, FUNCTION_DECL, sname,
3497 build_function_type (void_type_node,
3498 void_list_node));
3499 SET_DECL_LANGUAGE (fn, lang_c);
3500 TREE_PUBLIC (fn) = false;
3501 DECL_ARTIFICIAL (fn) = true;
3502 mark_used (fn);
3503 set_global_binding (fn);
3505 return fn;
3508 /* Get a FUNCTION_DECL for the init function for the thread_local
3509 variable VAR. The init function will be an alias to the function
3510 that initializes all the non-local TLS variables in the translation
3511 unit. The init function is only used by the wrapper function. */
3513 static tree
3514 get_tls_init_fn (tree var)
3516 /* Only C++11 TLS vars need this init fn. */
3517 if (!var_needs_tls_wrapper (var))
3518 return NULL_TREE;
3520 /* If -fno-extern-tls-init, assume that we don't need to call
3521 a tls init function for a variable defined in another TU. */
3522 if (!flag_extern_tls_init && DECL_EXTERNAL (var))
3523 return NULL_TREE;
3525 /* If the variable is internal, or if we can't generate aliases,
3526 call the local init function directly. */
3527 if (!TREE_PUBLIC (var) || !TARGET_SUPPORTS_ALIASES)
3528 return get_local_tls_init_fn (DECL_SOURCE_LOCATION (var));
3530 tree sname = mangle_tls_init_fn (var);
3531 tree fn = get_global_binding (sname);
3532 if (!fn)
3534 fn = build_lang_decl (FUNCTION_DECL, sname,
3535 build_function_type (void_type_node,
3536 void_list_node));
3537 SET_DECL_LANGUAGE (fn, lang_c);
3538 TREE_PUBLIC (fn) = TREE_PUBLIC (var);
3539 DECL_ARTIFICIAL (fn) = true;
3540 DECL_COMDAT (fn) = DECL_COMDAT (var);
3541 DECL_EXTERNAL (fn) = DECL_EXTERNAL (var);
3542 if (DECL_ONE_ONLY (var))
3543 make_decl_one_only (fn, cxx_comdat_group (fn));
3544 if (TREE_PUBLIC (var))
3546 tree obtype = strip_array_types (non_reference (TREE_TYPE (var)));
3547 /* If the variable is defined somewhere else and might have static
3548 initialization, make the init function a weak reference. */
3549 if ((!TYPE_NEEDS_CONSTRUCTING (obtype)
3550 || TYPE_HAS_CONSTEXPR_CTOR (obtype)
3551 || TYPE_HAS_TRIVIAL_DFLT (obtype))
3552 && TYPE_HAS_TRIVIAL_DESTRUCTOR (obtype)
3553 && DECL_EXTERNAL (var))
3554 declare_weak (fn);
3555 else
3556 DECL_WEAK (fn) = DECL_WEAK (var);
3558 DECL_VISIBILITY (fn) = DECL_VISIBILITY (var);
3559 DECL_VISIBILITY_SPECIFIED (fn) = DECL_VISIBILITY_SPECIFIED (var);
3560 DECL_DLLIMPORT_P (fn) = DECL_DLLIMPORT_P (var);
3561 DECL_IGNORED_P (fn) = 1;
3562 mark_used (fn);
3564 DECL_BEFRIENDING_CLASSES (fn) = var;
3566 set_global_binding (fn);
3568 return fn;
3571 /* Get a FUNCTION_DECL for the init wrapper function for the thread_local
3572 variable VAR. The wrapper function calls the init function (if any) for
3573 VAR and then returns a reference to VAR. The wrapper function is used
3574 in place of VAR everywhere VAR is mentioned. */
3576 static tree
3577 get_tls_wrapper_fn (tree var)
3579 /* Only C++11 TLS vars need this wrapper fn. */
3580 if (!var_needs_tls_wrapper (var))
3581 return NULL_TREE;
3583 tree sname = mangle_tls_wrapper_fn (var);
3584 tree fn = get_global_binding (sname);
3585 if (!fn)
3587 /* A named rvalue reference is an lvalue, so the wrapper should
3588 always return an lvalue reference. */
3589 tree type = non_reference (TREE_TYPE (var));
3590 type = build_reference_type (type);
3591 tree fntype = build_function_type (type, void_list_node);
3593 fn = build_lang_decl_loc (DECL_SOURCE_LOCATION (var),
3594 FUNCTION_DECL, sname, fntype);
3595 SET_DECL_LANGUAGE (fn, lang_c);
3596 TREE_PUBLIC (fn) = TREE_PUBLIC (var);
3597 DECL_ARTIFICIAL (fn) = true;
3598 DECL_IGNORED_P (fn) = 1;
3599 /* The wrapper is inline and emitted everywhere var is used. */
3600 DECL_DECLARED_INLINE_P (fn) = true;
3601 if (TREE_PUBLIC (var))
3603 comdat_linkage (fn);
3604 #ifdef HAVE_GAS_HIDDEN
3605 /* Make the wrapper bind locally; there's no reason to share
3606 the wrapper between multiple shared objects. */
3607 DECL_VISIBILITY (fn) = VISIBILITY_INTERNAL;
3608 DECL_VISIBILITY_SPECIFIED (fn) = true;
3609 #endif
3611 if (!TREE_PUBLIC (fn))
3612 DECL_INTERFACE_KNOWN (fn) = true;
3613 mark_used (fn);
3614 note_vague_linkage_fn (fn);
3616 #if 0
3617 /* We want CSE to commonize calls to the wrapper, but marking it as
3618 pure is unsafe since it has side-effects. I guess we need a new
3619 ECF flag even weaker than ECF_PURE. FIXME! */
3620 DECL_PURE_P (fn) = true;
3621 #endif
3623 DECL_BEFRIENDING_CLASSES (fn) = var;
3625 set_global_binding (fn);
3627 return fn;
3630 /* If EXPR is a thread_local variable that should be wrapped by init
3631 wrapper function, return a call to that function, otherwise return
3632 NULL. */
3634 tree
3635 maybe_get_tls_wrapper_call (tree expr)
3637 if (VAR_P (expr)
3638 && !processing_template_decl
3639 && !cp_unevaluated_operand
3640 && CP_DECL_THREAD_LOCAL_P (expr))
3641 if (tree wrap = get_tls_wrapper_fn (expr))
3642 return build_cxx_call (wrap, 0, NULL, tf_warning_or_error);
3643 return NULL;
3646 /* At EOF, generate the definition for the TLS wrapper function FN:
3648 T& var_wrapper() {
3649 if (init_fn) init_fn();
3650 return var;
3651 } */
3653 static void
3654 generate_tls_wrapper (tree fn)
3656 tree var = DECL_BEFRIENDING_CLASSES (fn);
3658 start_preparsed_function (fn, NULL_TREE, SF_DEFAULT | SF_PRE_PARSED);
3659 tree body = begin_function_body ();
3660 /* Only call the init fn if there might be one. */
3661 if (tree init_fn = get_tls_init_fn (var))
3663 tree if_stmt = NULL_TREE;
3664 /* If init_fn is a weakref, make sure it exists before calling. */
3665 if (lookup_attribute ("weak", DECL_ATTRIBUTES (init_fn)))
3667 if_stmt = begin_if_stmt ();
3668 tree addr = cp_build_addr_expr (init_fn, tf_warning_or_error);
3669 tree cond = cp_build_binary_op (DECL_SOURCE_LOCATION (var),
3670 NE_EXPR, addr, nullptr_node,
3671 tf_warning_or_error);
3672 finish_if_stmt_cond (cond, if_stmt);
3674 finish_expr_stmt (build_cxx_call
3675 (init_fn, 0, NULL, tf_warning_or_error));
3676 if (if_stmt)
3678 finish_then_clause (if_stmt);
3679 finish_if_stmt (if_stmt);
3682 else
3683 /* If there's no initialization, the wrapper is a constant function. */
3684 TREE_READONLY (fn) = true;
3685 finish_return_stmt (convert_from_reference (var));
3686 finish_function_body (body);
3687 expand_or_defer_fn (finish_function (/*inline_p=*/false));
3690 /* Start the process of running a particular set of global constructors
3691 or destructors. Subroutine of do_[cd]tors. Also called from
3692 vtv_start_verification_constructor_init_function. */
3694 static tree
3695 start_objects (int method_type, int initp)
3697 /* Make ctor or dtor function. METHOD_TYPE may be 'I' or 'D'. */
3698 int module_init = 0;
3700 if (initp == DEFAULT_INIT_PRIORITY && method_type == 'I')
3701 module_init = module_initializer_kind ();
3703 tree name = NULL_TREE;
3704 if (module_init > 0)
3705 name = mangle_module_global_init (0);
3706 else
3708 char type[14];
3710 unsigned len = sprintf (type, "sub_%c", method_type);
3711 if (initp != DEFAULT_INIT_PRIORITY)
3713 char joiner = '_';
3714 #ifdef JOINER
3715 joiner = JOINER;
3716 #endif
3717 type[len++] = joiner;
3718 sprintf (type + len, "%.5u", initp);
3720 name = get_file_function_name (type);
3723 tree fntype = build_function_type (void_type_node, void_list_node);
3724 tree fndecl = build_lang_decl (FUNCTION_DECL, name, fntype);
3725 DECL_CONTEXT (fndecl) = FROB_CONTEXT (global_namespace);
3726 if (module_init > 0)
3728 SET_DECL_ASSEMBLER_NAME (fndecl, name);
3729 TREE_PUBLIC (fndecl) = true;
3730 determine_visibility (fndecl);
3732 else
3733 TREE_PUBLIC (fndecl) = 0;
3734 start_preparsed_function (fndecl, /*attrs=*/NULL_TREE, SF_PRE_PARSED);
3736 /* Mark as artificial because it's not explicitly in the user's
3737 source code. */
3738 DECL_ARTIFICIAL (current_function_decl) = 1;
3740 /* Mark this declaration as used to avoid spurious warnings. */
3741 TREE_USED (current_function_decl) = 1;
3743 /* Mark this function as a global constructor or destructor. */
3744 if (method_type == 'I')
3745 DECL_GLOBAL_CTOR_P (current_function_decl) = 1;
3746 else
3747 DECL_GLOBAL_DTOR_P (current_function_decl) = 1;
3749 tree body = begin_compound_stmt (BCS_FN_BODY);
3751 if (module_init > 0)
3753 // 'static bool __in_chrg = false;
3754 // if (__inchrg) return;
3755 // __inchrg = true
3756 tree var = build_lang_decl (VAR_DECL, in_charge_identifier,
3757 boolean_type_node);
3758 DECL_CONTEXT (var) = fndecl;
3759 DECL_ARTIFICIAL (var) = true;
3760 TREE_STATIC (var) = true;
3761 pushdecl (var);
3762 cp_finish_decl (var, NULL_TREE, false, NULL_TREE, 0);
3764 tree if_stmt = begin_if_stmt ();
3765 finish_if_stmt_cond (var, if_stmt);
3766 finish_return_stmt (NULL_TREE);
3767 finish_then_clause (if_stmt);
3768 finish_if_stmt (if_stmt);
3770 tree assign = build2 (MODIFY_EXPR, boolean_type_node,
3771 var, boolean_true_node);
3772 TREE_SIDE_EFFECTS (assign) = true;
3773 finish_expr_stmt (assign);
3776 if (module_init)
3777 module_add_import_initializers ();
3779 return body;
3782 /* Finish the process of running a particular set of global constructors
3783 or destructors. Subroutine of do_[cd]tors. */
3785 static void
3786 finish_objects (int method_type, int initp, tree body)
3788 /* Finish up. */
3789 finish_compound_stmt (body);
3790 tree fn = finish_function (/*inline_p=*/false);
3792 if (method_type == 'I')
3794 DECL_STATIC_CONSTRUCTOR (fn) = 1;
3795 decl_init_priority_insert (fn, initp);
3797 else
3799 DECL_STATIC_DESTRUCTOR (fn) = 1;
3800 decl_fini_priority_insert (fn, initp);
3803 expand_or_defer_fn (fn);
3806 /* The names of the parameters to the function created to handle
3807 initializations and destructions for objects with static storage
3808 duration. */
3809 #define INITIALIZE_P_IDENTIFIER "__initialize_p"
3810 #define PRIORITY_IDENTIFIER "__priority"
3812 /* The name of the function we create to handle initializations and
3813 destructions for objects with static storage duration. */
3814 #define SSDF_IDENTIFIER "__static_initialization_and_destruction"
3816 /* The declaration for the __INITIALIZE_P argument. */
3817 static GTY(()) tree initialize_p_decl;
3819 /* The declaration for the __PRIORITY argument. */
3820 static GTY(()) tree priority_decl;
3822 /* The declaration for the static storage duration function. */
3823 static GTY(()) tree ssdf_decl;
3825 /* All the static storage duration functions created in this
3826 translation unit. */
3827 static GTY(()) vec<tree, va_gc> *ssdf_decls;
3829 /* A map from priority levels to information about that priority
3830 level. There may be many such levels, so efficient lookup is
3831 important. */
3832 static splay_tree priority_info_map;
3834 /* Begins the generation of the function that will handle all
3835 initialization and destruction of objects with static storage
3836 duration. The function generated takes two parameters of type
3837 `int': __INITIALIZE_P and __PRIORITY. If __INITIALIZE_P is
3838 nonzero, it performs initializations. Otherwise, it performs
3839 destructions. It only performs those initializations or
3840 destructions with the indicated __PRIORITY. The generated function
3841 returns no value.
3843 It is assumed that this function will only be called once per
3844 translation unit. */
3846 static tree
3847 start_static_storage_duration_function (unsigned count)
3849 tree type;
3850 tree body;
3851 char id[sizeof (SSDF_IDENTIFIER) + 1 /* '\0' */ + 32];
3853 /* Create the identifier for this function. It will be of the form
3854 SSDF_IDENTIFIER_<number>. */
3855 sprintf (id, "%s_%u", SSDF_IDENTIFIER, count);
3857 type = build_function_type_list (void_type_node,
3858 integer_type_node, integer_type_node,
3859 NULL_TREE);
3861 /* Create the FUNCTION_DECL itself. */
3862 ssdf_decl = build_lang_decl (FUNCTION_DECL,
3863 get_identifier (id),
3864 type);
3865 TREE_PUBLIC (ssdf_decl) = 0;
3866 DECL_ARTIFICIAL (ssdf_decl) = 1;
3868 /* Put this function in the list of functions to be called from the
3869 static constructors and destructors. */
3870 if (!ssdf_decls)
3872 vec_alloc (ssdf_decls, 32);
3874 /* Take this opportunity to initialize the map from priority
3875 numbers to information about that priority level. */
3876 priority_info_map = splay_tree_new (splay_tree_compare_ints,
3877 /*delete_key_fn=*/0,
3878 /*delete_value_fn=*/
3879 splay_tree_delete_pointers);
3881 /* We always need to generate functions for the
3882 DEFAULT_INIT_PRIORITY so enter it now. That way when we walk
3883 priorities later, we'll be sure to find the
3884 DEFAULT_INIT_PRIORITY. */
3885 get_priority_info (DEFAULT_INIT_PRIORITY);
3888 vec_safe_push (ssdf_decls, ssdf_decl);
3890 /* Create the argument list. */
3891 initialize_p_decl = cp_build_parm_decl
3892 (ssdf_decl, get_identifier (INITIALIZE_P_IDENTIFIER), integer_type_node);
3893 TREE_USED (initialize_p_decl) = 1;
3894 priority_decl = cp_build_parm_decl
3895 (ssdf_decl, get_identifier (PRIORITY_IDENTIFIER), integer_type_node);
3896 TREE_USED (priority_decl) = 1;
3898 DECL_CHAIN (initialize_p_decl) = priority_decl;
3899 DECL_ARGUMENTS (ssdf_decl) = initialize_p_decl;
3901 /* Put the function in the global scope. */
3902 pushdecl (ssdf_decl);
3904 /* Start the function itself. This is equivalent to declaring the
3905 function as:
3907 static void __ssdf (int __initialize_p, init __priority_p);
3909 It is static because we only need to call this function from the
3910 various constructor and destructor functions for this module. */
3911 start_preparsed_function (ssdf_decl,
3912 /*attrs=*/NULL_TREE,
3913 SF_PRE_PARSED);
3915 /* Set up the scope of the outermost block in the function. */
3916 body = begin_compound_stmt (BCS_FN_BODY);
3918 return body;
3921 /* Finish the generation of the function which performs initialization
3922 and destruction of objects with static storage duration. After
3923 this point, no more such objects can be created. */
3925 static void
3926 finish_static_storage_duration_function (tree body)
3928 /* Close out the function. */
3929 finish_compound_stmt (body);
3930 expand_or_defer_fn (finish_function (/*inline_p=*/false));
3933 /* Return the information about the indicated PRIORITY level. If no
3934 code to handle this level has yet been generated, generate the
3935 appropriate prologue. */
3937 static priority_info
3938 get_priority_info (int priority)
3940 priority_info pi;
3941 splay_tree_node n;
3943 n = splay_tree_lookup (priority_info_map,
3944 (splay_tree_key) priority);
3945 if (!n)
3947 /* Create a new priority information structure, and insert it
3948 into the map. */
3949 pi = XNEW (struct priority_info_s);
3950 pi->initializations_p = 0;
3951 pi->destructions_p = 0;
3952 splay_tree_insert (priority_info_map,
3953 (splay_tree_key) priority,
3954 (splay_tree_value) pi);
3956 else
3957 pi = (priority_info) n->value;
3959 return pi;
3962 /* The effective initialization priority of a DECL. */
3964 #define DECL_EFFECTIVE_INIT_PRIORITY(decl) \
3965 ((!DECL_HAS_INIT_PRIORITY_P (decl) || DECL_INIT_PRIORITY (decl) == 0) \
3966 ? DEFAULT_INIT_PRIORITY : DECL_INIT_PRIORITY (decl))
3968 /* Whether a DECL needs a guard to protect it against multiple
3969 initialization. */
3971 #define NEEDS_GUARD_P(decl) (TREE_PUBLIC (decl) && (DECL_COMMON (decl) \
3972 || DECL_ONE_ONLY (decl) \
3973 || DECL_WEAK (decl)))
3975 /* Called from one_static_initialization_or_destruction(),
3976 via walk_tree.
3977 Walks the initializer list of a global variable and looks for
3978 temporary variables (DECL_NAME() == NULL and DECL_ARTIFICIAL != 0)
3979 and that have their DECL_CONTEXT() == NULL.
3980 For each such temporary variable, set their DECL_CONTEXT() to
3981 the current function. This is necessary because otherwise
3982 some optimizers (enabled by -O2 -fprofile-arcs) might crash
3983 when trying to refer to a temporary variable that does not have
3984 it's DECL_CONTECT() properly set. */
3985 static tree
3986 fix_temporary_vars_context_r (tree *node,
3987 int * /*unused*/,
3988 void * /*unused1*/)
3990 gcc_assert (current_function_decl);
3992 if (TREE_CODE (*node) == BIND_EXPR)
3994 tree var;
3996 for (var = BIND_EXPR_VARS (*node); var; var = DECL_CHAIN (var))
3997 if (VAR_P (var)
3998 && !DECL_NAME (var)
3999 && DECL_ARTIFICIAL (var)
4000 && !DECL_CONTEXT (var))
4001 DECL_CONTEXT (var) = current_function_decl;
4004 return NULL_TREE;
4007 /* Set up to handle the initialization or destruction of DECL. If
4008 INITP is nonzero, we are initializing the variable. Otherwise, we
4009 are destroying it. */
4011 static void
4012 one_static_initialization_or_destruction (tree decl, tree init, bool initp)
4014 tree guard_if_stmt = NULL_TREE;
4015 tree guard;
4017 /* If we are supposed to destruct and there's a trivial destructor,
4018 nothing has to be done. */
4019 if (!initp
4020 && TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
4021 return;
4023 /* Trick the compiler into thinking we are at the file and line
4024 where DECL was declared so that error-messages make sense, and so
4025 that the debugger will show somewhat sensible file and line
4026 information. */
4027 input_location = DECL_SOURCE_LOCATION (decl);
4029 /* Make sure temporary variables in the initialiser all have
4030 their DECL_CONTEXT() set to a value different from NULL_TREE.
4031 This can happen when global variables initializers are built.
4032 In that case, the DECL_CONTEXT() of the global variables _AND_ of all
4033 the temporary variables that might have been generated in the
4034 accompanying initializers is NULL_TREE, meaning the variables have been
4035 declared in the global namespace.
4036 What we want to do here is to fix that and make sure the DECL_CONTEXT()
4037 of the temporaries are set to the current function decl. */
4038 cp_walk_tree_without_duplicates (&init,
4039 fix_temporary_vars_context_r,
4040 NULL);
4042 /* Because of:
4044 [class.access.spec]
4046 Access control for implicit calls to the constructors,
4047 the conversion functions, or the destructor called to
4048 create and destroy a static data member is performed as
4049 if these calls appeared in the scope of the member's
4050 class.
4052 we pretend we are in a static member function of the class of
4053 which the DECL is a member. */
4054 if (member_p (decl))
4056 DECL_CONTEXT (current_function_decl) = DECL_CONTEXT (decl);
4057 DECL_STATIC_FUNCTION_P (current_function_decl) = 1;
4060 /* Assume we don't need a guard. */
4061 guard = NULL_TREE;
4062 /* We need a guard if this is an object with external linkage that
4063 might be initialized in more than one place. (For example, a
4064 static data member of a template, when the data member requires
4065 construction.) */
4066 if (NEEDS_GUARD_P (decl))
4068 tree guard_cond;
4070 guard = get_guard (decl);
4072 /* When using __cxa_atexit, we just check the GUARD as we would
4073 for a local static. */
4074 if (flag_use_cxa_atexit)
4076 /* When using __cxa_atexit, we never try to destroy
4077 anything from a static destructor. */
4078 gcc_assert (initp);
4079 guard_cond = get_guard_cond (guard, false);
4081 /* If we don't have __cxa_atexit, then we will be running
4082 destructors from .fini sections, or their equivalents. So,
4083 we need to know how many times we've tried to initialize this
4084 object. We do initializations only if the GUARD is zero,
4085 i.e., if we are the first to initialize the variable. We do
4086 destructions only if the GUARD is one, i.e., if we are the
4087 last to destroy the variable. */
4088 else if (initp)
4089 guard_cond
4090 = cp_build_binary_op (input_location,
4091 EQ_EXPR,
4092 cp_build_unary_op (PREINCREMENT_EXPR,
4093 guard,
4094 /*noconvert=*/true,
4095 tf_warning_or_error),
4096 integer_one_node,
4097 tf_warning_or_error);
4098 else
4099 guard_cond
4100 = cp_build_binary_op (input_location,
4101 EQ_EXPR,
4102 cp_build_unary_op (PREDECREMENT_EXPR,
4103 guard,
4104 /*noconvert=*/true,
4105 tf_warning_or_error),
4106 integer_zero_node,
4107 tf_warning_or_error);
4109 guard_if_stmt = begin_if_stmt ();
4110 finish_if_stmt_cond (guard_cond, guard_if_stmt);
4114 /* If we're using __cxa_atexit, we have not already set the GUARD,
4115 so we must do so now. */
4116 if (guard && initp && flag_use_cxa_atexit)
4117 finish_expr_stmt (set_guard (guard));
4119 /* Perform the initialization or destruction. */
4120 if (initp)
4122 if (init)
4124 finish_expr_stmt (init);
4125 if (sanitize_flags_p (SANITIZE_ADDRESS, decl))
4127 varpool_node *vnode = varpool_node::get (decl);
4128 if (vnode)
4129 vnode->dynamically_initialized = 1;
4133 /* If we're using __cxa_atexit, register a function that calls the
4134 destructor for the object. */
4135 if (flag_use_cxa_atexit)
4136 finish_expr_stmt (register_dtor_fn (decl));
4138 else
4139 finish_expr_stmt (build_cleanup (decl));
4141 /* Finish the guard if-stmt, if necessary. */
4142 if (guard)
4144 finish_then_clause (guard_if_stmt);
4145 finish_if_stmt (guard_if_stmt);
4148 /* Now that we're done with DECL we don't need to pretend to be a
4149 member of its class any longer. */
4150 DECL_CONTEXT (current_function_decl) = NULL_TREE;
4151 DECL_STATIC_FUNCTION_P (current_function_decl) = 0;
4154 /* Generate code to do the initialization or destruction of the decls in VARS,
4155 a TREE_LIST of VAR_DECL with static storage duration.
4156 Whether initialization or destruction is performed is specified by INITP. */
4158 static void
4159 do_static_initialization_or_destruction (tree vars, bool initp)
4161 tree node, init_if_stmt, cond;
4163 /* Build the outer if-stmt to check for initialization or destruction. */
4164 init_if_stmt = begin_if_stmt ();
4165 cond = initp ? integer_one_node : integer_zero_node;
4166 cond = cp_build_binary_op (input_location,
4167 EQ_EXPR,
4168 initialize_p_decl,
4169 cond,
4170 tf_warning_or_error);
4171 finish_if_stmt_cond (cond, init_if_stmt);
4173 /* To make sure dynamic construction doesn't access globals from other
4174 compilation units where they might not be yet constructed, for
4175 -fsanitize=address insert __asan_before_dynamic_init call that
4176 prevents access to either all global variables that need construction
4177 in other compilation units, or at least those that haven't been
4178 initialized yet. Variables that need dynamic construction in
4179 the current compilation unit are kept accessible. */
4180 if (initp && (flag_sanitize & SANITIZE_ADDRESS))
4181 finish_expr_stmt (asan_dynamic_init_call (/*after_p=*/false));
4183 node = vars;
4184 do {
4185 tree decl = TREE_VALUE (node);
4186 tree priority_if_stmt;
4187 int priority;
4188 priority_info pi;
4190 /* If we don't need a destructor, there's nothing to do. Avoid
4191 creating a possibly empty if-stmt. */
4192 if (!initp && TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
4194 node = TREE_CHAIN (node);
4195 continue;
4198 /* Remember that we had an initialization or finalization at this
4199 priority. */
4200 priority = DECL_EFFECTIVE_INIT_PRIORITY (decl);
4201 pi = get_priority_info (priority);
4202 if (initp)
4203 pi->initializations_p = 1;
4204 else
4205 pi->destructions_p = 1;
4207 /* Conditionalize this initialization on being in the right priority
4208 and being initializing/finalizing appropriately. */
4209 priority_if_stmt = begin_if_stmt ();
4210 cond = cp_build_binary_op (input_location,
4211 EQ_EXPR,
4212 priority_decl,
4213 build_int_cst (NULL_TREE, priority),
4214 tf_warning_or_error);
4215 finish_if_stmt_cond (cond, priority_if_stmt);
4217 /* Process initializers with same priority. */
4218 for (; node
4219 && DECL_EFFECTIVE_INIT_PRIORITY (TREE_VALUE (node)) == priority;
4220 node = TREE_CHAIN (node))
4221 /* Do one initialization or destruction. */
4222 one_static_initialization_or_destruction (TREE_VALUE (node),
4223 TREE_PURPOSE (node), initp);
4225 /* Finish up the priority if-stmt body. */
4226 finish_then_clause (priority_if_stmt);
4227 finish_if_stmt (priority_if_stmt);
4229 } while (node);
4231 /* Revert what __asan_before_dynamic_init did by calling
4232 __asan_after_dynamic_init. */
4233 if (initp && (flag_sanitize & SANITIZE_ADDRESS))
4234 finish_expr_stmt (asan_dynamic_init_call (/*after_p=*/true));
4236 /* Finish up the init/destruct if-stmt body. */
4237 finish_then_clause (init_if_stmt);
4238 finish_if_stmt (init_if_stmt);
4241 /* VARS is a list of variables with static storage duration which may
4242 need initialization and/or finalization. Remove those variables
4243 that don't really need to be initialized or finalized, and return
4244 the resulting list. The order in which the variables appear in
4245 VARS is in reverse order of the order in which they should actually
4246 be initialized. The list we return is in the unreversed order;
4247 i.e., the first variable should be initialized first. */
4249 static tree
4250 prune_vars_needing_no_initialization (tree *vars)
4252 tree *var = vars;
4253 tree result = NULL_TREE;
4255 while (*var)
4257 tree t = *var;
4258 tree decl = TREE_VALUE (t);
4259 tree init = TREE_PURPOSE (t);
4261 /* Deal gracefully with error. */
4262 if (error_operand_p (decl))
4264 var = &TREE_CHAIN (t);
4265 continue;
4268 /* The only things that can be initialized are variables. */
4269 gcc_assert (VAR_P (decl));
4271 /* If this object is not defined, we don't need to do anything
4272 here. */
4273 if (DECL_EXTERNAL (decl))
4275 var = &TREE_CHAIN (t);
4276 continue;
4279 /* Also, if the initializer already contains errors, we can bail
4280 out now. */
4281 if (init && TREE_CODE (init) == TREE_LIST
4282 && value_member (error_mark_node, init))
4284 var = &TREE_CHAIN (t);
4285 continue;
4288 /* This variable is going to need initialization and/or
4289 finalization, so we add it to the list. */
4290 *var = TREE_CHAIN (t);
4291 TREE_CHAIN (t) = result;
4292 result = t;
4295 return result;
4298 /* Make sure we have told the back end about all the variables in
4299 VARS. */
4301 static void
4302 write_out_vars (tree vars)
4304 tree v;
4306 for (v = vars; v; v = TREE_CHAIN (v))
4308 tree var = TREE_VALUE (v);
4309 if (!var_finalized_p (var))
4311 import_export_decl (var);
4312 rest_of_decl_compilation (var, 1, 1);
4317 /* Generate a static constructor (if CONSTRUCTOR_P) or destructor
4318 (otherwise) that will initialize all global objects with static
4319 storage duration having the indicated PRIORITY. */
4321 static void
4322 generate_ctor_or_dtor_function (bool constructor_p, int priority,
4323 location_t *locus)
4325 input_location = *locus;
4327 /* We use `I' to indicate initialization and `D' to indicate
4328 destruction. */
4329 char function_key = constructor_p ? 'I' : 'D';
4331 /* We emit the function lazily, to avoid generating empty
4332 global constructors and destructors. */
4333 tree body = NULL_TREE;
4335 if (constructor_p && priority == DEFAULT_INIT_PRIORITY)
4337 bool objc = c_dialect_objc () && objc_static_init_needed_p ();
4339 /* We may have module initialization to emit and/or insert
4340 before other intializations. */
4341 if (module_initializer_kind () || objc)
4342 body = start_objects (function_key, priority);
4344 /* For Objective-C++, we may need to initialize metadata found
4345 in this module. This must be done _before_ any other static
4346 initializations. */
4347 if (objc)
4348 objc_generate_static_init_call (NULL_TREE);
4351 /* Call the static storage duration function with appropriate
4352 arguments. */
4353 tree fndecl;
4354 size_t i;
4355 FOR_EACH_VEC_SAFE_ELT (ssdf_decls, i, fndecl)
4357 /* Calls to pure or const functions will expand to nothing. */
4358 if (! (flags_from_decl_or_type (fndecl) & (ECF_CONST | ECF_PURE)))
4360 if (! body)
4361 body = start_objects (function_key, priority);
4363 tree call = cp_build_function_call_nary (fndecl, tf_warning_or_error,
4364 build_int_cst (NULL_TREE,
4365 constructor_p),
4366 build_int_cst (NULL_TREE,
4367 priority),
4368 NULL_TREE);
4369 finish_expr_stmt (call);
4373 /* Close out the function. */
4374 if (body)
4375 finish_objects (function_key, priority, body);
4378 /* Generate constructor and destructor functions for the priority
4379 indicated by N. */
4381 static int
4382 generate_ctor_and_dtor_functions_for_priority (splay_tree_node n, void * data)
4384 location_t *locus = (location_t *) data;
4385 int priority = (int) n->key;
4386 priority_info pi = (priority_info) n->value;
4388 /* Generate the functions themselves, but only if they are really
4389 needed. */
4390 if (pi->initializations_p)
4391 generate_ctor_or_dtor_function (/*constructor_p=*/true, priority, locus);
4392 if (pi->destructions_p)
4393 generate_ctor_or_dtor_function (/*constructor_p=*/false, priority, locus);
4395 /* Keep iterating. */
4396 return 0;
4399 /* Return C++ property of T, based on given operation OP. */
4401 static int
4402 cpp_check (tree t, cpp_operation op)
4404 switch (op)
4406 case HAS_DEPENDENT_TEMPLATE_ARGS:
4408 tree ti = CLASSTYPE_TEMPLATE_INFO (t);
4409 if (!ti)
4410 return 0;
4411 ++processing_template_decl;
4412 const bool dep = any_dependent_template_arguments_p (TI_ARGS (ti));
4413 --processing_template_decl;
4414 return dep;
4416 case IS_ABSTRACT:
4417 return DECL_PURE_VIRTUAL_P (t);
4418 case IS_ASSIGNMENT_OPERATOR:
4419 return DECL_ASSIGNMENT_OPERATOR_P (t);
4420 case IS_CONSTRUCTOR:
4421 return DECL_CONSTRUCTOR_P (t);
4422 case IS_DESTRUCTOR:
4423 return DECL_DESTRUCTOR_P (t);
4424 case IS_COPY_CONSTRUCTOR:
4425 return DECL_COPY_CONSTRUCTOR_P (t);
4426 case IS_MOVE_CONSTRUCTOR:
4427 return DECL_MOVE_CONSTRUCTOR_P (t);
4428 case IS_TEMPLATE:
4429 return TREE_CODE (t) == TEMPLATE_DECL;
4430 case IS_TRIVIAL:
4431 return trivial_type_p (t);
4432 default:
4433 return 0;
4437 /* Collect source file references recursively, starting from NAMESPC. */
4439 static void
4440 collect_source_refs (tree namespc)
4442 /* Iterate over names in this name space. */
4443 for (tree t = NAMESPACE_LEVEL (namespc)->names; t; t = TREE_CHAIN (t))
4444 if (DECL_IS_UNDECLARED_BUILTIN (t))
4446 else if (TREE_CODE (t) == NAMESPACE_DECL && !DECL_NAMESPACE_ALIAS (t))
4447 collect_source_refs (t);
4448 else
4449 collect_source_ref (DECL_SOURCE_FILE (t));
4452 /* Collect decls relevant to SOURCE_FILE from all namespaces recursively,
4453 starting from NAMESPC. */
4455 static void
4456 collect_ada_namespace (tree namespc, const char *source_file)
4458 tree decl = NAMESPACE_LEVEL (namespc)->names;
4460 /* Collect decls from this namespace. This will skip
4461 NAMESPACE_DECLs (both aliases and regular, it cannot tell). */
4462 collect_ada_nodes (decl, source_file);
4464 /* Now scan for namespace children, and dump them. */
4465 for (; decl; decl = TREE_CHAIN (decl))
4466 if (TREE_CODE (decl) == NAMESPACE_DECL && !DECL_NAMESPACE_ALIAS (decl))
4467 collect_ada_namespace (decl, source_file);
4470 /* Returns true iff there is a definition available for variable or
4471 function DECL. */
4473 bool
4474 decl_defined_p (tree decl)
4476 if (TREE_CODE (decl) == FUNCTION_DECL)
4477 return (DECL_INITIAL (decl) != NULL_TREE
4478 /* A pending instantiation of a friend temploid is defined. */
4479 || (DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl)
4480 && DECL_INITIAL (DECL_TEMPLATE_RESULT
4481 (DECL_TI_TEMPLATE (decl)))));
4482 else
4484 gcc_assert (VAR_P (decl));
4485 return !DECL_EXTERNAL (decl);
4489 /* Nonzero for a VAR_DECL whose value can be used in a constant expression.
4491 [expr.const]
4493 An integral constant-expression can only involve ... const
4494 variables of integral or enumeration types initialized with
4495 constant expressions ...
4497 C++0x also allows constexpr variables and temporaries initialized
4498 with constant expressions. We handle the former here, but the latter
4499 are just folded away in cxx_eval_constant_expression.
4501 The standard does not require that the expression be non-volatile.
4502 G++ implements the proposed correction in DR 457. */
4504 bool
4505 decl_constant_var_p (tree decl)
4507 if (!decl_maybe_constant_var_p (decl))
4508 return false;
4510 /* We don't know if a template static data member is initialized with
4511 a constant expression until we instantiate its initializer. Even
4512 in the case of a constexpr variable, we can't treat it as a
4513 constant until its initializer is complete in case it's used in
4514 its own initializer. */
4515 maybe_instantiate_decl (decl);
4516 return DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl);
4519 /* Returns true if DECL could be a symbolic constant variable, depending on
4520 its initializer. */
4522 bool
4523 decl_maybe_constant_var_p (tree decl)
4525 tree type = TREE_TYPE (decl);
4526 if (!VAR_P (decl))
4527 return false;
4528 if (DECL_DECLARED_CONSTEXPR_P (decl) && !TREE_THIS_VOLATILE (decl))
4529 return true;
4530 if (DECL_HAS_VALUE_EXPR_P (decl))
4531 /* A proxy isn't constant. */
4532 return false;
4533 if (TYPE_REF_P (type))
4534 /* References can be constant. */;
4535 else if (CP_TYPE_CONST_NON_VOLATILE_P (type)
4536 && INTEGRAL_OR_ENUMERATION_TYPE_P (type))
4537 /* And const integers. */;
4538 else
4539 return false;
4541 if (DECL_INITIAL (decl)
4542 && !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl))
4543 /* We know the initializer, and it isn't constant. */
4544 return false;
4545 else
4546 return true;
4549 /* Complain that DECL uses a type with no linkage. In C++98 mode this is
4550 called from grokfndecl and grokvardecl; in all modes it is called from
4551 cp_write_global_declarations. */
4553 void
4554 no_linkage_error (tree decl)
4556 if (cxx_dialect >= cxx11
4557 && (decl_defined_p (decl)
4558 /* Treat templates which limit_bad_template_recursion decided
4559 not to instantiate as if they were defined. */
4560 || (errorcount + sorrycount > 0
4561 && DECL_LANG_SPECIFIC (decl)
4562 && DECL_TEMPLATE_INFO (decl)
4563 && warning_suppressed_p (decl /* What warning? */))))
4564 /* In C++11 it's ok if the decl is defined. */
4565 return;
4567 if (DECL_LANG_SPECIFIC (decl) && DECL_MODULE_IMPORT_P (decl))
4568 /* An imported decl is ok. */
4569 return;
4571 tree t = no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/false);
4572 if (t == NULL_TREE)
4573 /* The type that got us on no_linkage_decls must have gotten a name for
4574 linkage purposes. */;
4575 else if (CLASS_TYPE_P (t) && TYPE_BEING_DEFINED (t))
4576 // FIXME: This is now invalid, as a DR to c++98
4577 /* The type might end up having a typedef name for linkage purposes. */
4578 vec_safe_push (no_linkage_decls, decl);
4579 else if (TYPE_UNNAMED_P (t))
4581 bool d = false;
4582 auto_diagnostic_group grp;
4583 if (cxx_dialect >= cxx11)
4584 d = permerror (DECL_SOURCE_LOCATION (decl), "%q#D, declared using "
4585 "unnamed type, is used but never defined", decl);
4586 else if (DECL_EXTERN_C_P (decl))
4587 /* Allow this; it's pretty common in C. */;
4588 else if (VAR_P (decl))
4589 /* DRs 132, 319 and 389 seem to indicate types with
4590 no linkage can only be used to declare extern "C"
4591 entities. Since it's not always an error in the
4592 ISO C++ 90 Standard, we only issue a warning. */
4593 d = warning_at (DECL_SOURCE_LOCATION (decl), 0, "unnamed type "
4594 "with no linkage used to declare variable %q#D with "
4595 "linkage", decl);
4596 else
4597 d = permerror (DECL_SOURCE_LOCATION (decl), "unnamed type with no "
4598 "linkage used to declare function %q#D with linkage",
4599 decl);
4600 if (d && is_typedef_decl (TYPE_NAME (t)))
4601 inform (DECL_SOURCE_LOCATION (TYPE_NAME (t)), "%q#D does not refer "
4602 "to the unqualified type, so it is not used for linkage",
4603 TYPE_NAME (t));
4605 else if (cxx_dialect >= cxx11)
4607 if (VAR_P (decl) || !DECL_PURE_VIRTUAL_P (decl))
4608 permerror (DECL_SOURCE_LOCATION (decl),
4609 "%q#D, declared using local type "
4610 "%qT, is used but never defined", decl, t);
4612 else if (VAR_P (decl))
4613 warning_at (DECL_SOURCE_LOCATION (decl), 0, "type %qT with no linkage "
4614 "used to declare variable %q#D with linkage", t, decl);
4615 else
4616 permerror (DECL_SOURCE_LOCATION (decl), "type %qT with no linkage used "
4617 "to declare function %q#D with linkage", t, decl);
4620 /* Collect declarations from all namespaces relevant to SOURCE_FILE. */
4622 static void
4623 collect_all_refs (const char *source_file)
4625 collect_ada_namespace (global_namespace, source_file);
4628 /* Clear DECL_EXTERNAL for NODE. */
4630 static bool
4631 clear_decl_external (struct cgraph_node *node, void * /*data*/)
4633 DECL_EXTERNAL (node->decl) = 0;
4634 return false;
4637 /* Build up the function to run dynamic initializers for thread_local
4638 variables in this translation unit and alias the init functions for the
4639 individual variables to it. */
4641 static void
4642 handle_tls_init (void)
4644 tree vars = prune_vars_needing_no_initialization (&tls_aggregates);
4645 if (vars == NULL_TREE)
4646 return;
4648 location_t loc = DECL_SOURCE_LOCATION (TREE_VALUE (vars));
4650 write_out_vars (vars);
4652 tree guard = build_decl (loc, VAR_DECL, get_identifier ("__tls_guard"),
4653 boolean_type_node);
4654 TREE_PUBLIC (guard) = false;
4655 TREE_STATIC (guard) = true;
4656 DECL_ARTIFICIAL (guard) = true;
4657 DECL_IGNORED_P (guard) = true;
4658 TREE_USED (guard) = true;
4659 CP_DECL_THREAD_LOCAL_P (guard) = true;
4660 set_decl_tls_model (guard, decl_default_tls_model (guard));
4661 pushdecl_top_level_and_finish (guard, NULL_TREE);
4663 tree fn = get_local_tls_init_fn (loc);
4664 start_preparsed_function (fn, NULL_TREE, SF_PRE_PARSED);
4665 tree body = begin_function_body ();
4666 tree if_stmt = begin_if_stmt ();
4667 tree cond = cp_build_unary_op (TRUTH_NOT_EXPR, guard, false,
4668 tf_warning_or_error);
4669 finish_if_stmt_cond (cond, if_stmt);
4670 finish_expr_stmt (cp_build_modify_expr (loc, guard, NOP_EXPR,
4671 boolean_true_node,
4672 tf_warning_or_error));
4673 for (; vars; vars = TREE_CHAIN (vars))
4675 tree var = TREE_VALUE (vars);
4676 tree init = TREE_PURPOSE (vars);
4677 one_static_initialization_or_destruction (var, init, true);
4679 /* Output init aliases even with -fno-extern-tls-init. */
4680 if (TARGET_SUPPORTS_ALIASES && TREE_PUBLIC (var))
4682 tree single_init_fn = get_tls_init_fn (var);
4683 if (single_init_fn == NULL_TREE)
4684 continue;
4685 cgraph_node *alias
4686 = cgraph_node::get_create (fn)->create_same_body_alias
4687 (single_init_fn, fn);
4688 gcc_assert (alias != NULL);
4692 finish_then_clause (if_stmt);
4693 finish_if_stmt (if_stmt);
4694 finish_function_body (body);
4695 expand_or_defer_fn (finish_function (/*inline_p=*/false));
4698 /* We're at the end of compilation, so generate any mangling aliases that
4699 we've been saving up, if DECL is going to be output and ID2 isn't
4700 already taken by another declaration. */
4702 static void
4703 generate_mangling_alias (tree decl, tree id2)
4705 struct cgraph_node *n = NULL;
4707 if (TREE_CODE (decl) == FUNCTION_DECL)
4709 n = cgraph_node::get (decl);
4710 if (!n)
4711 /* Don't create an alias to an unreferenced function. */
4712 return;
4715 tree *slot
4716 = mangled_decls->find_slot_with_hash (id2, IDENTIFIER_HASH_VALUE (id2),
4717 INSERT);
4719 /* If there's a declaration already using this mangled name,
4720 don't create a compatibility alias that conflicts. */
4721 if (*slot)
4722 return;
4724 tree alias = make_alias_for (decl, id2);
4725 *slot = alias;
4727 DECL_IGNORED_P (alias) = 1;
4728 TREE_PUBLIC (alias) = TREE_PUBLIC (decl);
4729 DECL_VISIBILITY (alias) = DECL_VISIBILITY (decl);
4730 if (vague_linkage_p (decl))
4731 DECL_WEAK (alias) = 1;
4733 if (n)
4734 n->create_same_body_alias (alias, decl);
4735 else
4736 varpool_node::create_extra_name_alias (alias, decl);
4739 /* Note that we might want to emit an alias with the symbol ID2 for DECL at
4740 the end of translation, for compatibility across bugs in the mangling
4741 implementation. */
4743 void
4744 note_mangling_alias (tree decl, tree id2)
4746 if (TARGET_SUPPORTS_ALIASES)
4748 if (!defer_mangling_aliases)
4749 generate_mangling_alias (decl, id2);
4750 else
4752 vec_safe_push (mangling_aliases, decl);
4753 vec_safe_push (mangling_aliases, id2);
4758 /* Emit all mangling aliases that were deferred up to this point. */
4760 void
4761 generate_mangling_aliases ()
4763 while (!vec_safe_is_empty (mangling_aliases))
4765 tree id2 = mangling_aliases->pop();
4766 tree decl = mangling_aliases->pop();
4767 generate_mangling_alias (decl, id2);
4769 defer_mangling_aliases = false;
4772 /* Record a mangling of DECL, whose DECL_ASSEMBLER_NAME has just been
4773 set. NEED_WARNING is true if we must warn about collisions. We do
4774 this to spot changes in mangling that may require compatibility
4775 aliases. */
4777 void
4778 record_mangling (tree decl, bool need_warning)
4780 if (!mangled_decls)
4781 mangled_decls = hash_table<mangled_decl_hash>::create_ggc (499);
4783 gcc_checking_assert (DECL_ASSEMBLER_NAME_SET_P (decl));
4784 tree id = DECL_ASSEMBLER_NAME_RAW (decl);
4785 tree *slot
4786 = mangled_decls->find_slot_with_hash (id, IDENTIFIER_HASH_VALUE (id),
4787 INSERT);
4789 /* If this is already an alias, remove the alias, because the real
4790 decl takes precedence. */
4791 if (*slot && DECL_ARTIFICIAL (*slot) && DECL_IGNORED_P (*slot))
4792 if (symtab_node *n = symtab_node::get (*slot))
4793 if (n->cpp_implicit_alias)
4795 n->remove ();
4796 *slot = NULL_TREE;
4799 if (!*slot)
4800 *slot = decl;
4801 else if (need_warning)
4803 error_at (DECL_SOURCE_LOCATION (decl),
4804 "mangling of %q#D as %qE conflicts with a previous mangle",
4805 decl, id);
4806 inform (DECL_SOURCE_LOCATION (*slot),
4807 "previous mangling %q#D", *slot);
4808 inform (DECL_SOURCE_LOCATION (decl),
4809 "a later %<-fabi-version=%> (or =0)"
4810 " avoids this error with a change in mangling");
4811 *slot = decl;
4815 /* The mangled name of DECL is being forcibly changed to NAME. Remove
4816 any existing knowledge of DECL's mangled name meaning DECL. */
4818 void
4819 overwrite_mangling (tree decl, tree name)
4821 if (tree id = DECL_ASSEMBLER_NAME_RAW (decl))
4822 if ((TREE_CODE (decl) == VAR_DECL
4823 || TREE_CODE (decl) == FUNCTION_DECL)
4824 && mangled_decls)
4825 if (tree *slot
4826 = mangled_decls->find_slot_with_hash (id, IDENTIFIER_HASH_VALUE (id),
4827 NO_INSERT))
4828 if (*slot == decl)
4830 mangled_decls->clear_slot (slot);
4832 /* If this is an alias, remove it from the symbol table. */
4833 if (DECL_ARTIFICIAL (decl) && DECL_IGNORED_P (decl))
4834 if (symtab_node *n = symtab_node::get (decl))
4835 if (n->cpp_implicit_alias)
4836 n->remove ();
4839 DECL_ASSEMBLER_NAME_RAW (decl) = name;
4842 /* The entire file is now complete. If requested, dump everything
4843 to a file. */
4845 static void
4846 dump_tu (void)
4848 dump_flags_t flags;
4849 if (FILE *stream = dump_begin (raw_dump_id, &flags))
4851 dump_node (global_namespace, flags & ~TDF_SLIM, stream);
4852 dump_end (raw_dump_id, stream);
4856 static location_t locus_at_end_of_parsing;
4858 /* Check the deallocation functions for CODE to see if we want to warn that
4859 only one was defined. */
4861 static void
4862 maybe_warn_sized_delete (enum tree_code code)
4864 tree sized = NULL_TREE;
4865 tree unsized = NULL_TREE;
4867 for (ovl_iterator iter (get_global_binding (ovl_op_identifier (false, code)));
4868 iter; ++iter)
4870 tree fn = *iter;
4871 /* We're only interested in usual deallocation functions. */
4872 if (!usual_deallocation_fn_p (fn))
4873 continue;
4874 if (FUNCTION_ARG_CHAIN (fn) == void_list_node)
4875 unsized = fn;
4876 else
4877 sized = fn;
4879 if (DECL_INITIAL (unsized) && !DECL_INITIAL (sized))
4880 warning_at (DECL_SOURCE_LOCATION (unsized), OPT_Wsized_deallocation,
4881 "the program should also define %qD", sized);
4882 else if (!DECL_INITIAL (unsized) && DECL_INITIAL (sized))
4883 warning_at (DECL_SOURCE_LOCATION (sized), OPT_Wsized_deallocation,
4884 "the program should also define %qD", unsized);
4887 /* Check the global deallocation functions to see if we want to warn about
4888 defining unsized without sized (or vice versa). */
4890 static void
4891 maybe_warn_sized_delete ()
4893 if (!flag_sized_deallocation || !warn_sized_deallocation)
4894 return;
4895 maybe_warn_sized_delete (DELETE_EXPR);
4896 maybe_warn_sized_delete (VEC_DELETE_EXPR);
4899 /* Earlier we left PTRMEM_CST in variable initializers alone so that we could
4900 look them up when evaluating non-type template parameters. Now we need to
4901 lower them to something the back end can understand. */
4903 static void
4904 lower_var_init ()
4906 varpool_node *node;
4907 FOR_EACH_VARIABLE (node)
4909 tree d = node->decl;
4910 if (tree init = DECL_INITIAL (d))
4911 DECL_INITIAL (d) = cplus_expand_constant (init);
4915 /* This routine is called at the end of compilation.
4916 Its job is to create all the code needed to initialize and
4917 destroy the global aggregates. We do the destruction
4918 first, since that way we only need to reverse the decls once. */
4920 void
4921 c_parse_final_cleanups (void)
4923 size_t i;
4924 tree decl;
4926 locus_at_end_of_parsing = input_location;
4927 at_eof = 1;
4929 /* Bad parse errors. Just forget about it. */
4930 if (! global_bindings_p () || current_class_type
4931 || !vec_safe_is_empty (decl_namespace_list))
4932 return;
4934 /* This is the point to write out a PCH if we're doing that.
4935 In that case we do not want to do anything else. */
4936 if (pch_file)
4938 /* Mangle all symbols at PCH creation time. */
4939 symtab_node *node;
4940 FOR_EACH_SYMBOL (node)
4941 if (! is_a <varpool_node *> (node)
4942 || ! DECL_HARD_REGISTER (node->decl))
4943 DECL_ASSEMBLER_NAME (node->decl);
4944 c_common_write_pch ();
4945 dump_tu ();
4946 /* Ensure even the callers don't try to finalize the CU. */
4947 flag_syntax_only = 1;
4948 return;
4951 timevar_stop (TV_PHASE_PARSING);
4952 timevar_start (TV_PHASE_DEFERRED);
4954 symtab->process_same_body_aliases ();
4956 /* Handle -fdump-ada-spec[-slim] */
4957 if (flag_dump_ada_spec || flag_dump_ada_spec_slim)
4959 collect_source_ref (main_input_filename);
4960 if (!flag_dump_ada_spec_slim)
4961 collect_source_refs (global_namespace);
4963 dump_ada_specs (collect_all_refs, cpp_check);
4966 /* FIXME - huh? was input_line -= 1;*/
4968 /* We now have to write out all the stuff we put off writing out.
4969 These include:
4971 o Template specializations that we have not yet instantiated,
4972 but which are needed.
4973 o Initialization and destruction for non-local objects with
4974 static storage duration. (Local objects with static storage
4975 duration are initialized when their scope is first entered,
4976 and are cleaned up via atexit.)
4977 o Virtual function tables.
4979 All of these may cause others to be needed. For example,
4980 instantiating one function may cause another to be needed, and
4981 generating the initializer for an object may cause templates to be
4982 instantiated, etc., etc. */
4984 emit_support_tinfos ();
4986 /* Track vtables we want to emit that refer to consteval functions. */
4987 auto_vec<tree> consteval_vtables;
4989 int retries = 0;
4990 unsigned ssdf_count = 0;
4991 for (bool reconsider = true; reconsider; retries++)
4993 reconsider = false;
4995 /* If there are templates that we've put off instantiating, do
4996 them now. */
4997 instantiate_pending_templates (retries);
4998 ggc_collect ();
5000 if (header_module_p ())
5001 /* A header modules initializations are handled in its
5002 importer. */
5003 continue;
5005 /* Write out virtual tables as required. Writing out the
5006 virtual table for a template class may cause the
5007 instantiation of members of that class. If we write out
5008 vtables then we remove the class from our list so we don't
5009 have to look at it again. */
5010 tree t;
5011 for (i = keyed_classes->length ();
5012 keyed_classes->iterate (--i, &t);)
5013 if (maybe_emit_vtables (t, consteval_vtables))
5015 reconsider = true;
5016 keyed_classes->unordered_remove (i);
5018 /* The input_location may have been changed during marking of
5019 vtable entries. */
5020 input_location = locus_at_end_of_parsing;
5022 /* Write out needed type info variables. We have to be careful
5023 looping through unemitted decls, because emit_tinfo_decl may
5024 cause other variables to be needed. New elements will be
5025 appended, and we remove from the vector those that actually
5026 get emitted. */
5027 for (i = unemitted_tinfo_decls->length ();
5028 unemitted_tinfo_decls->iterate (--i, &t);)
5029 if (emit_tinfo_decl (t))
5031 reconsider = true;
5032 unemitted_tinfo_decls->unordered_remove (i);
5035 /* The list of objects with static storage duration is built up
5036 in reverse order. We clear STATIC_AGGREGATES so that any new
5037 aggregates added during the initialization of these will be
5038 initialized in the correct order when we next come around the
5039 loop. */
5040 if (tree vars = prune_vars_needing_no_initialization (&static_aggregates))
5042 if (flag_openmp)
5043 /* Add initializer information from VARS into
5044 DYNAMIC_INITIALIZERS. */
5045 for (t = vars; t; t = TREE_CHAIN (t))
5046 hash_map_safe_put<hm_ggc> (dynamic_initializers,
5047 TREE_VALUE (t), TREE_PURPOSE (t));
5049 /* We need to start a new initialization function each time
5050 through the loop. That's because we need to know which
5051 vtables have been referenced, and TREE_SYMBOL_REFERENCED
5052 isn't computed until a function is finished, and written
5053 out. That's a deficiency in the back end. When this is
5054 fixed, these initialization functions could all become
5055 inline, with resulting performance improvements. */
5056 tree ssdf_body;
5058 /* Make sure the back end knows about all the variables. */
5059 write_out_vars (vars);
5061 /* Set the line and file, so that it is obviously not from
5062 the source file. */
5063 input_location = locus_at_end_of_parsing;
5064 ssdf_body = start_static_storage_duration_function (ssdf_count);
5066 /* First generate code to do all the initializations. */
5067 if (vars)
5068 do_static_initialization_or_destruction (vars, /*initp=*/true);
5070 /* Then, generate code to do all the destructions. Do these
5071 in reverse order so that the most recently constructed
5072 variable is the first destroyed. If we're using
5073 __cxa_atexit, then we don't need to do this; functions
5074 were registered at initialization time to destroy the
5075 local statics. */
5076 if (!flag_use_cxa_atexit && vars)
5078 vars = nreverse (vars);
5079 do_static_initialization_or_destruction (vars, /*initp=*/false);
5081 else
5082 vars = NULL_TREE;
5084 /* Finish up the static storage duration function for this
5085 round. */
5086 input_location = locus_at_end_of_parsing;
5087 finish_static_storage_duration_function (ssdf_body);
5089 /* All those initializations and finalizations might cause
5090 us to need more inline functions, more template
5091 instantiations, etc. */
5092 reconsider = true;
5093 ssdf_count++;
5096 /* Now do the same for thread_local variables. */
5097 handle_tls_init ();
5099 /* Go through the set of inline functions whose bodies have not
5100 been emitted yet. If out-of-line copies of these functions
5101 are required, emit them. */
5102 FOR_EACH_VEC_SAFE_ELT (deferred_fns, i, decl)
5104 /* Does it need synthesizing? */
5105 if (DECL_DEFAULTED_FN (decl) && ! DECL_INITIAL (decl)
5106 && (! DECL_REALLY_EXTERN (decl) || possibly_inlined_p (decl)))
5108 /* Even though we're already at the top-level, we push
5109 there again. That way, when we pop back a few lines
5110 hence, all of our state is restored. Otherwise,
5111 finish_function doesn't clean things up, and we end
5112 up with CURRENT_FUNCTION_DECL set. */
5113 push_to_top_level ();
5114 /* The decl's location will mark where it was first
5115 needed. Save that so synthesize method can indicate
5116 where it was needed from, in case of error */
5117 input_location = DECL_SOURCE_LOCATION (decl);
5118 synthesize_method (decl);
5119 pop_from_top_level ();
5120 reconsider = true;
5123 if (!DECL_INITIAL (decl) && decl_tls_wrapper_p (decl))
5124 generate_tls_wrapper (decl);
5126 if (!DECL_SAVED_TREE (decl))
5127 continue;
5129 cgraph_node *node = cgraph_node::get_create (decl);
5131 /* We lie to the back end, pretending that some functions
5132 are not defined when they really are. This keeps these
5133 functions from being put out unnecessarily. But, we must
5134 stop lying when the functions are referenced, or if they
5135 are not comdat since they need to be put out now. If
5136 DECL_INTERFACE_KNOWN, then we have already set
5137 DECL_EXTERNAL appropriately, so there's no need to check
5138 again, and we do not want to clear DECL_EXTERNAL if a
5139 previous call to import_export_decl set it.
5141 This is done in a separate for cycle, because if some
5142 deferred function is contained in another deferred
5143 function later in deferred_fns varray,
5144 rest_of_compilation would skip this function and we
5145 really cannot expand the same function twice. */
5146 import_export_decl (decl);
5147 if (DECL_NOT_REALLY_EXTERN (decl)
5148 && DECL_INITIAL (decl)
5149 && decl_needed_p (decl))
5151 if (node->cpp_implicit_alias)
5152 node = node->get_alias_target ();
5154 node->call_for_symbol_thunks_and_aliases (clear_decl_external,
5155 NULL, true);
5156 /* If we mark !DECL_EXTERNAL one of the symbols in some comdat
5157 group, we need to mark all symbols in the same comdat group
5158 that way. */
5159 if (node->same_comdat_group)
5160 for (cgraph_node *next
5161 = dyn_cast<cgraph_node *> (node->same_comdat_group);
5162 next != node;
5163 next = dyn_cast<cgraph_node *> (next->same_comdat_group))
5164 next->call_for_symbol_thunks_and_aliases (clear_decl_external,
5165 NULL, true);
5168 /* If we're going to need to write this function out, and
5169 there's already a body for it, create RTL for it now.
5170 (There might be no body if this is a method we haven't
5171 gotten around to synthesizing yet.) */
5172 if (!DECL_EXTERNAL (decl)
5173 && decl_needed_p (decl)
5174 && !TREE_ASM_WRITTEN (decl)
5175 && !node->definition)
5177 /* We will output the function; no longer consider it in this
5178 loop. */
5179 DECL_DEFER_OUTPUT (decl) = 0;
5180 /* Generate RTL for this function now that we know we
5181 need it. */
5182 expand_or_defer_fn (decl);
5183 reconsider = true;
5187 if (wrapup_namespace_globals ())
5188 reconsider = true;
5190 /* Static data members are just like namespace-scope globals. */
5191 FOR_EACH_VEC_SAFE_ELT (pending_statics, i, decl)
5193 if (var_finalized_p (decl) || DECL_REALLY_EXTERN (decl)
5194 /* Don't write it out if we haven't seen a definition. */
5195 || DECL_IN_AGGR_P (decl))
5196 continue;
5197 import_export_decl (decl);
5198 /* If this static data member is needed, provide it to the
5199 back end. */
5200 if (DECL_NOT_REALLY_EXTERN (decl) && decl_needed_p (decl))
5201 DECL_EXTERNAL (decl) = 0;
5204 if (vec_safe_length (pending_statics) != 0
5205 && wrapup_global_declarations (pending_statics->address (),
5206 pending_statics->length ()))
5207 reconsider = true;
5210 finish_module_processing (parse_in);
5212 lower_var_init ();
5214 generate_mangling_aliases ();
5216 /* All used inline functions must have a definition at this point. */
5217 FOR_EACH_VEC_SAFE_ELT (deferred_fns, i, decl)
5219 if (/* Check online inline functions that were actually used. */
5220 DECL_ODR_USED (decl) && DECL_DECLARED_INLINE_P (decl)
5221 /* If the definition actually was available here, then the
5222 fact that the function was not defined merely represents
5223 that for some reason (use of a template repository,
5224 #pragma interface, etc.) we decided not to emit the
5225 definition here. */
5226 && !DECL_INITIAL (decl)
5227 /* A defaulted fn in a header module can be synthesized on
5228 demand later. (In non-header modules we should have
5229 synthesized it above.) */
5230 && !(DECL_DEFAULTED_FN (decl) && header_module_p ())
5231 /* Don't complain if the template was defined. */
5232 && !(DECL_TEMPLATE_INSTANTIATION (decl)
5233 && DECL_INITIAL (DECL_TEMPLATE_RESULT
5234 (template_for_substitution (decl))))
5235 && warning_at (DECL_SOURCE_LOCATION (decl), 0,
5236 "inline function %qD used but never defined", decl))
5237 /* Avoid a duplicate warning from check_global_declaration. */
5238 suppress_warning (decl, OPT_Wunused);
5241 /* So must decls that use a type with no linkage. */
5242 FOR_EACH_VEC_SAFE_ELT (no_linkage_decls, i, decl)
5243 no_linkage_error (decl);
5245 maybe_warn_sized_delete ();
5247 /* Then, do the Objective-C stuff. This is where all the
5248 Objective-C module stuff gets generated (symtab,
5249 class/protocol/selector lists etc). This must be done after C++
5250 templates, destructors etc. so that selectors used in C++
5251 templates are properly allocated. */
5252 if (c_dialect_objc ())
5253 objc_write_global_declarations ();
5255 /* We give C linkage to static constructors and destructors. */
5256 push_lang_context (lang_name_c);
5258 /* Generate initialization and destruction functions for all
5259 priorities for which they are required. */
5260 if (priority_info_map)
5261 splay_tree_foreach (priority_info_map,
5262 generate_ctor_and_dtor_functions_for_priority,
5263 /*data=*/&locus_at_end_of_parsing);
5264 else if ((c_dialect_objc () && objc_static_init_needed_p ())
5265 || module_initializer_kind ())
5266 generate_ctor_or_dtor_function (/*constructor_p=*/true,
5267 DEFAULT_INIT_PRIORITY,
5268 &locus_at_end_of_parsing);
5270 /* We're done with the splay-tree now. */
5271 if (priority_info_map)
5272 splay_tree_delete (priority_info_map);
5274 fini_modules ();
5276 /* Generate any missing aliases. */
5277 maybe_apply_pending_pragma_weaks ();
5279 /* We're done with static constructors, so we can go back to "C++"
5280 linkage now. */
5281 pop_lang_context ();
5283 if (flag_vtable_verify)
5285 vtv_recover_class_info ();
5286 vtv_compute_class_hierarchy_transitive_closure ();
5287 vtv_build_vtable_verify_fndecl ();
5290 perform_deferred_noexcept_checks ();
5292 fini_constexpr ();
5293 cp_tree_c_finish_parsing ();
5294 clear_consteval_vfns (consteval_vtables);
5296 /* The entire file is now complete. If requested, dump everything
5297 to a file. */
5298 dump_tu ();
5300 if (flag_detailed_statistics)
5302 dump_tree_statistics ();
5303 dump_time_statistics ();
5306 timevar_stop (TV_PHASE_DEFERRED);
5307 timevar_start (TV_PHASE_PARSING);
5309 /* Indicate that we're done with front end processing. */
5310 at_eof = 2;
5313 /* Perform any post compilation-proper cleanups for the C++ front-end.
5314 This should really go away. No front-end should need to do
5315 anything past the compilation process. */
5317 void
5318 cxx_post_compilation_parsing_cleanups (void)
5320 timevar_start (TV_PHASE_LATE_PARSING_CLEANUPS);
5322 if (flag_vtable_verify)
5324 /* Generate the special constructor initialization function that
5325 calls __VLTRegisterPairs, and give it a very high
5326 initialization priority. This must be done after
5327 finalize_compilation_unit so that we have accurate
5328 information about which vtable will actually be emitted. */
5329 vtv_generate_init_routine ();
5332 input_location = locus_at_end_of_parsing;
5334 if (flag_checking)
5335 validate_conversion_obstack ();
5337 timevar_stop (TV_PHASE_LATE_PARSING_CLEANUPS);
5340 /* FN is an OFFSET_REF, DOTSTAR_EXPR or MEMBER_REF indicating the
5341 function to call in parse-tree form; it has not yet been
5342 semantically analyzed. ARGS are the arguments to the function.
5343 They have already been semantically analyzed. This may change
5344 ARGS. */
5346 tree
5347 build_offset_ref_call_from_tree (tree fn, vec<tree, va_gc> **args,
5348 tsubst_flags_t complain)
5350 tree orig_fn;
5351 vec<tree, va_gc> *orig_args = NULL;
5352 tree expr;
5353 tree object;
5355 orig_fn = fn;
5356 object = TREE_OPERAND (fn, 0);
5358 if (processing_template_decl)
5360 gcc_assert (TREE_CODE (fn) == DOTSTAR_EXPR
5361 || TREE_CODE (fn) == MEMBER_REF);
5362 if (type_dependent_expression_p (fn)
5363 || any_type_dependent_arguments_p (*args))
5364 return build_min_nt_call_vec (fn, *args);
5366 orig_args = make_tree_vector_copy (*args);
5368 /* Transform the arguments and add the implicit "this"
5369 parameter. That must be done before the FN is transformed
5370 because we depend on the form of FN. */
5371 make_args_non_dependent (*args);
5372 object = build_non_dependent_expr (object);
5373 if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
5375 if (TREE_CODE (fn) == DOTSTAR_EXPR)
5376 object = cp_build_addr_expr (object, complain);
5377 vec_safe_insert (*args, 0, object);
5379 /* Now that the arguments are done, transform FN. */
5380 fn = build_non_dependent_expr (fn);
5383 /* A qualified name corresponding to a bound pointer-to-member is
5384 represented as an OFFSET_REF:
5386 struct B { void g(); };
5387 void (B::*p)();
5388 void B::g() { (this->*p)(); } */
5389 if (TREE_CODE (fn) == OFFSET_REF)
5391 tree object_addr = cp_build_addr_expr (object, complain);
5392 fn = TREE_OPERAND (fn, 1);
5393 fn = get_member_function_from_ptrfunc (&object_addr, fn,
5394 complain);
5395 vec_safe_insert (*args, 0, object_addr);
5398 if (CLASS_TYPE_P (TREE_TYPE (fn)))
5399 expr = build_op_call (fn, args, complain);
5400 else
5401 expr = cp_build_function_call_vec (fn, args, complain);
5402 if (processing_template_decl && expr != error_mark_node)
5403 expr = build_min_non_dep_call_vec (expr, orig_fn, orig_args);
5405 if (orig_args != NULL)
5406 release_tree_vector (orig_args);
5408 return expr;
5412 void
5413 check_default_args (tree x)
5415 tree arg = TYPE_ARG_TYPES (TREE_TYPE (x));
5416 bool saw_def = false;
5417 bool noted_first_def = false;
5418 int idx_of_first_default_arg = 0;
5419 location_t loc_of_first_default_arg = UNKNOWN_LOCATION;
5420 int i = 0 - (TREE_CODE (TREE_TYPE (x)) == METHOD_TYPE);
5421 tree fndecl = STRIP_TEMPLATE (x);
5422 auto_diagnostic_group d;
5423 for (; arg && arg != void_list_node; arg = TREE_CHAIN (arg), ++i)
5425 if (TREE_PURPOSE (arg))
5427 if (!saw_def)
5429 saw_def = true;
5430 idx_of_first_default_arg = i;
5431 location_t loc = get_fndecl_argument_location (fndecl, i);
5432 if (loc != DECL_SOURCE_LOCATION (x))
5433 loc_of_first_default_arg = loc;
5436 else if (saw_def && !PACK_EXPANSION_P (TREE_VALUE (arg)))
5438 error_at (get_fndecl_argument_location (fndecl, i),
5439 "default argument missing for parameter %P of %q#D", i, x);
5440 if (loc_of_first_default_arg != UNKNOWN_LOCATION
5441 && !noted_first_def)
5443 inform (loc_of_first_default_arg,
5444 "...following parameter %P which has a default argument",
5445 idx_of_first_default_arg);
5446 noted_first_def = true;
5448 TREE_PURPOSE (arg) = error_mark_node;
5453 /* Return true if function DECL can be inlined. This is used to force
5454 instantiation of methods that might be interesting for inlining. */
5455 bool
5456 possibly_inlined_p (tree decl)
5458 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
5459 if (DECL_UNINLINABLE (decl))
5460 return false;
5461 if (!optimize)
5462 return DECL_DECLARED_INLINE_P (decl);
5463 /* When optimizing, we might inline everything when flatten
5464 attribute or heuristics inlining for size or autoinlining
5465 is used. */
5466 return true;
5469 /* Normally, we can wait until instantiation-time to synthesize DECL.
5470 However, if DECL is a static data member initialized with a constant
5471 or a constexpr function, we need it right now because a reference to
5472 such a data member or a call to such function is not value-dependent.
5473 For a function that uses auto in the return type, we need to instantiate
5474 it to find out its type. For OpenMP user defined reductions, we need
5475 them instantiated for reduction clauses which inline them by hand
5476 directly. */
5478 void
5479 maybe_instantiate_decl (tree decl)
5481 if (DECL_LANG_SPECIFIC (decl)
5482 && DECL_TEMPLATE_INFO (decl)
5483 && (decl_maybe_constant_var_p (decl)
5484 || (TREE_CODE (decl) == FUNCTION_DECL
5485 && DECL_OMP_DECLARE_REDUCTION_P (decl))
5486 || undeduced_auto_decl (decl))
5487 && !DECL_DECLARED_CONCEPT_P (decl)
5488 && !uses_template_parms (DECL_TI_ARGS (decl)))
5490 /* Instantiating a function will result in garbage collection. We
5491 must treat this situation as if we were within the body of a
5492 function so as to avoid collecting live data only referenced from
5493 the stack (such as overload resolution candidates). */
5494 ++function_depth;
5495 instantiate_decl (decl, /*defer_ok=*/false,
5496 /*expl_inst_class_mem_p=*/false);
5497 --function_depth;
5501 /* Maybe warn if DECL is deprecated, subject to COMPLAIN. Returns whether or
5502 not a warning was emitted. */
5504 bool
5505 cp_warn_deprecated_use (tree decl, tsubst_flags_t complain)
5507 if (!(complain & tf_warning) || !decl
5508 || deprecated_state == DEPRECATED_SUPPRESS)
5509 return false;
5511 if (!TREE_DEPRECATED (decl))
5513 /* Perhaps this is a deprecated typedef. */
5514 if (TYPE_P (decl) && TYPE_NAME (decl))
5515 decl = TYPE_NAME (decl);
5517 if (!TREE_DEPRECATED (decl))
5518 return false;
5521 /* Don't warn within members of a deprecated type. */
5522 if (TYPE_P (decl)
5523 && currently_open_class (decl))
5524 return false;
5526 bool warned = false;
5527 if (cxx_dialect >= cxx11
5528 && DECL_P (decl)
5529 && DECL_ARTIFICIAL (decl)
5530 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)
5531 && copy_fn_p (decl))
5533 /* Don't warn if the flag was disabled around the class definition
5534 (c++/94492). */
5535 if (warning_enabled_at (DECL_SOURCE_LOCATION (decl),
5536 OPT_Wdeprecated_copy))
5538 auto_diagnostic_group d;
5539 tree ctx = DECL_CONTEXT (decl);
5540 tree other = classtype_has_depr_implicit_copy (ctx);
5541 int opt = (DECL_DESTRUCTOR_P (other)
5542 ? OPT_Wdeprecated_copy_dtor
5543 : OPT_Wdeprecated_copy);
5544 warned = warning (opt, "implicitly-declared %qD is deprecated",
5545 decl);
5546 if (warned)
5547 inform (DECL_SOURCE_LOCATION (other),
5548 "because %qT has user-provided %qD",
5549 ctx, other);
5552 else
5553 warned = warn_deprecated_use (decl, NULL_TREE);
5555 return warned;
5558 /* Like above, but takes into account outer scopes. */
5560 void
5561 cp_warn_deprecated_use_scopes (tree scope)
5563 while (scope
5564 && scope != error_mark_node
5565 && scope != global_namespace)
5567 if ((TREE_CODE (scope) == NAMESPACE_DECL || OVERLOAD_TYPE_P (scope))
5568 && cp_warn_deprecated_use (scope))
5569 return;
5570 if (TYPE_P (scope))
5571 scope = CP_TYPE_CONTEXT (scope);
5572 else
5573 scope = CP_DECL_CONTEXT (scope);
5577 /* True if DECL or its enclosing scope have unbound template parameters. */
5579 bool
5580 decl_dependent_p (tree decl)
5582 if (DECL_FUNCTION_SCOPE_P (decl)
5583 || TREE_CODE (decl) == CONST_DECL
5584 || TREE_CODE (decl) == USING_DECL
5585 || TREE_CODE (decl) == FIELD_DECL)
5586 decl = CP_DECL_CONTEXT (decl);
5587 if (tree tinfo = get_template_info (decl))
5588 if (any_dependent_template_arguments_p (TI_ARGS (tinfo)))
5589 return true;
5590 if (LAMBDA_FUNCTION_P (decl)
5591 && dependent_type_p (DECL_CONTEXT (decl)))
5592 return true;
5593 return false;
5596 /* Mark DECL (either a _DECL or a BASELINK) as "used" in the program.
5597 If DECL is a specialization or implicitly declared class member,
5598 generate the actual definition. Return false if something goes
5599 wrong, true otherwise. */
5601 bool
5602 mark_used (tree decl, tsubst_flags_t complain)
5604 /* If we're just testing conversions or resolving overloads, we
5605 don't want any permanent effects like forcing functions to be
5606 output or instantiating templates. */
5607 if ((complain & tf_conv))
5608 return true;
5610 /* If DECL is a BASELINK for a single function, then treat it just
5611 like the DECL for the function. Otherwise, if the BASELINK is
5612 for an overloaded function, we don't know which function was
5613 actually used until after overload resolution. */
5614 if (BASELINK_P (decl))
5616 decl = BASELINK_FUNCTIONS (decl);
5617 if (really_overloaded_fn (decl))
5618 return true;
5619 decl = OVL_FIRST (decl);
5622 if (!DECL_P (decl))
5623 return true;
5625 /* Set TREE_USED for the benefit of -Wunused. */
5626 TREE_USED (decl) = true;
5628 /* And for structured bindings also the underlying decl. */
5629 if (DECL_DECOMPOSITION_P (decl) && DECL_DECOMP_BASE (decl))
5630 TREE_USED (DECL_DECOMP_BASE (decl)) = true;
5632 if (TREE_CODE (decl) == TEMPLATE_DECL)
5633 return true;
5635 if (DECL_CLONED_FUNCTION_P (decl))
5636 TREE_USED (DECL_CLONED_FUNCTION (decl)) = 1;
5638 /* Mark enumeration types as used. */
5639 if (TREE_CODE (decl) == CONST_DECL)
5640 used_types_insert (DECL_CONTEXT (decl));
5642 if (TREE_CODE (decl) == FUNCTION_DECL
5643 && !maybe_instantiate_noexcept (decl, complain))
5644 return false;
5646 if (TREE_CODE (decl) == FUNCTION_DECL
5647 && DECL_DELETED_FN (decl))
5649 if (DECL_ARTIFICIAL (decl)
5650 && DECL_CONV_FN_P (decl)
5651 && LAMBDA_TYPE_P (DECL_CONTEXT (decl)))
5652 /* We mark a lambda conversion op as deleted if we can't
5653 generate it properly; see maybe_add_lambda_conv_op. */
5654 sorry ("converting lambda that uses %<...%> to function pointer");
5655 else if (complain & tf_error)
5657 error ("use of deleted function %qD", decl);
5658 if (!maybe_explain_implicit_delete (decl))
5659 inform (DECL_SOURCE_LOCATION (decl), "declared here");
5661 return false;
5664 if (VAR_OR_FUNCTION_DECL_P (decl) && DECL_LOCAL_DECL_P (decl))
5666 if (!DECL_LANG_SPECIFIC (decl))
5667 /* An unresolved dependent local extern. */
5668 return true;
5670 DECL_ODR_USED (decl) = 1;
5671 auto alias = DECL_LOCAL_DECL_ALIAS (decl);
5672 if (!alias || alias == error_mark_node)
5673 return true;
5675 /* Process the underlying decl. */
5676 decl = alias;
5677 TREE_USED (decl) = true;
5680 cp_warn_deprecated_use (decl, complain);
5682 /* We can only check DECL_ODR_USED on variables or functions with
5683 DECL_LANG_SPECIFIC set, and these are also the only decls that we
5684 might need special handling for. */
5685 if (!VAR_OR_FUNCTION_DECL_P (decl)
5686 || DECL_LANG_SPECIFIC (decl) == NULL
5687 || DECL_THUNK_P (decl))
5689 if (!decl_dependent_p (decl)
5690 && !require_deduced_type (decl, complain))
5691 return false;
5692 return true;
5695 /* We only want to do this processing once. We don't need to keep trying
5696 to instantiate inline templates, because unit-at-a-time will make sure
5697 we get them compiled before functions that want to inline them. */
5698 if (DECL_ODR_USED (decl))
5699 return true;
5701 if (flag_concepts && TREE_CODE (decl) == FUNCTION_DECL
5702 && !constraints_satisfied_p (decl))
5704 if (complain & tf_error)
5706 auto_diagnostic_group d;
5707 error ("use of function %qD with unsatisfied constraints",
5708 decl);
5709 location_t loc = DECL_SOURCE_LOCATION (decl);
5710 inform (loc, "declared here");
5711 diagnose_constraints (loc, decl, NULL_TREE);
5713 return false;
5716 /* Normally, we can wait until instantiation-time to synthesize DECL.
5717 However, if DECL is a static data member initialized with a constant
5718 or a constexpr function, we need it right now because a reference to
5719 such a data member or a call to such function is not value-dependent.
5720 For a function that uses auto in the return type, we need to instantiate
5721 it to find out its type. For OpenMP user defined reductions, we need
5722 them instantiated for reduction clauses which inline them by hand
5723 directly. */
5724 maybe_instantiate_decl (decl);
5726 if (processing_template_decl || in_template_function ())
5727 return true;
5729 /* Check this too in case we're within instantiate_non_dependent_expr. */
5730 if (DECL_TEMPLATE_INFO (decl)
5731 && uses_template_parms (DECL_TI_ARGS (decl)))
5732 return true;
5734 if (!require_deduced_type (decl, complain))
5735 return false;
5737 if (builtin_pack_fn_p (decl))
5739 error ("use of built-in parameter pack %qD outside of a template",
5740 DECL_NAME (decl));
5741 return false;
5744 /* If we don't need a value, then we don't need to synthesize DECL. */
5745 if (cp_unevaluated_operand || in_discarded_stmt)
5746 return true;
5748 DECL_ODR_USED (decl) = 1;
5749 if (DECL_CLONED_FUNCTION_P (decl))
5750 DECL_ODR_USED (DECL_CLONED_FUNCTION (decl)) = 1;
5752 /* DR 757: A type without linkage shall not be used as the type of a
5753 variable or function with linkage, unless
5754 o the variable or function has extern "C" linkage (7.5 [dcl.link]), or
5755 o the variable or function is not used (3.2 [basic.def.odr]) or is
5756 defined in the same translation unit. */
5757 if (cxx_dialect > cxx98
5758 && decl_linkage (decl) != lk_none
5759 && !DECL_EXTERN_C_P (decl)
5760 && !DECL_ARTIFICIAL (decl)
5761 && !decl_defined_p (decl)
5762 && no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/false))
5763 vec_safe_push (no_linkage_decls, decl);
5765 if (TREE_CODE (decl) == FUNCTION_DECL
5766 && DECL_DECLARED_INLINE_P (decl)
5767 && !DECL_INITIAL (decl)
5768 && !DECL_ARTIFICIAL (decl)
5769 && !DECL_PURE_VIRTUAL_P (decl))
5770 /* Remember it, so we can check it was defined. */
5771 note_vague_linkage_fn (decl);
5773 /* Is it a synthesized method that needs to be synthesized? */
5774 if (TREE_CODE (decl) == FUNCTION_DECL
5775 && DECL_DEFAULTED_FN (decl)
5776 /* A function defaulted outside the class is synthesized either by
5777 cp_finish_decl or instantiate_decl. */
5778 && !DECL_DEFAULTED_OUTSIDE_CLASS_P (decl)
5779 && ! DECL_INITIAL (decl))
5781 /* Defer virtual destructors so that thunks get the right
5782 linkage. */
5783 if (DECL_VIRTUAL_P (decl) && !at_eof)
5785 note_vague_linkage_fn (decl);
5786 return true;
5789 /* Remember the current location for a function we will end up
5790 synthesizing. Then we can inform the user where it was
5791 required in the case of error. */
5792 if (decl_remember_implicit_trigger_p (decl))
5793 DECL_SOURCE_LOCATION (decl) = input_location;
5795 /* Synthesizing an implicitly defined member function will result in
5796 garbage collection. We must treat this situation as if we were
5797 within the body of a function so as to avoid collecting live data
5798 on the stack (such as overload resolution candidates).
5800 We could just let c_parse_final_cleanups handle synthesizing
5801 this function by adding it to deferred_fns, but doing
5802 it at the use site produces better error messages. */
5803 ++function_depth;
5804 synthesize_method (decl);
5805 --function_depth;
5806 /* If this is a synthesized method we don't need to
5807 do the instantiation test below. */
5809 else if (VAR_OR_FUNCTION_DECL_P (decl)
5810 && DECL_TEMPLATE_INFO (decl)
5811 && !DECL_DECLARED_CONCEPT_P (decl)
5812 && (!DECL_EXPLICIT_INSTANTIATION (decl)
5813 || always_instantiate_p (decl)))
5814 /* If this is a function or variable that is an instance of some
5815 template, we now know that we will need to actually do the
5816 instantiation. We check that DECL is not an explicit
5817 instantiation because that is not checked in instantiate_decl.
5819 We put off instantiating functions in order to improve compile
5820 times. Maintaining a stack of active functions is expensive,
5821 and the inliner knows to instantiate any functions it might
5822 need. Therefore, we always try to defer instantiation. */
5824 ++function_depth;
5825 instantiate_decl (decl, /*defer_ok=*/true,
5826 /*expl_inst_class_mem_p=*/false);
5827 --function_depth;
5830 return true;
5833 bool
5834 mark_used (tree decl)
5836 return mark_used (decl, tf_warning_or_error);
5839 tree
5840 vtv_start_verification_constructor_init_function (void)
5842 return start_objects ('I', MAX_RESERVED_INIT_PRIORITY - 1);
5845 tree
5846 vtv_finish_verification_constructor_init_function (tree function_body)
5848 tree fn;
5850 finish_compound_stmt (function_body);
5851 fn = finish_function (/*inline_p=*/false);
5852 DECL_STATIC_CONSTRUCTOR (fn) = 1;
5853 decl_init_priority_insert (fn, MAX_RESERVED_INIT_PRIORITY - 1);
5855 return fn;
5858 #include "gt-cp-decl2.h"